1
Fork 0
mirror of https://github.com/RGBCube/superfreq synced 2025-07-27 17:07:44 +00:00

debug: validate output of is_systemd_service_active

This commit is contained in:
NotAShelf 2025-05-17 07:52:05 +03:00 committed by raf
parent 601735957b
commit c57c82ab71
2 changed files with 14 additions and 5 deletions

View file

@ -219,15 +219,13 @@ fn get_kernel_info() -> Result<String, AppError> {
/// Get system uptime /// Get system uptime
fn get_system_uptime() -> Result<Duration, AppError> { fn get_system_uptime() -> Result<Duration, AppError> {
let uptime_str = fs::read_to_string("/proc/uptime").map_err(|e| AppError::Io(e))?; let uptime_str = fs::read_to_string("/proc/uptime").map_err(AppError::Io)?;
let uptime_secs = uptime_str let uptime_secs = uptime_str
.split_whitespace() .split_whitespace()
.next() .next()
.ok_or_else(|| AppError::Generic("Invalid format in /proc/uptime file".to_string()))? .ok_or_else(|| AppError::Generic("Invalid format in /proc/uptime file".to_string()))?
.parse::<f64>() .parse::<f64>()
.map_err(|e| { .map_err(|e| AppError::Generic(format!("Failed to parse uptime from /proc/uptime: {e}")))?;
AppError::Generic(format!("Failed to parse uptime from /proc/uptime: {}", e))
})?;
Ok(Duration::from_secs_f64(uptime_secs)) Ok(Duration::from_secs_f64(uptime_secs))
} }
@ -253,7 +251,16 @@ fn is_systemd_service_active(service_name: &str) -> Result<bool, AppError> {
.output() .output()
.map_err(AppError::Io)?; .map_err(AppError::Io)?;
// Check if the command executed successfully
if !output.status.success() {
// Command failed - service is either not found or not active
return Ok(false);
}
// Command executed successfully, now check the output content
let status = String::from_utf8(output.stdout) let status = String::from_utf8(output.stdout)
.map_err(|e| AppError::Generic(format!("Failed to parse systemctl output: {e}")))?; .map_err(|e| AppError::Generic(format!("Failed to parse systemctl output: {e}")))?;
// Explicitly verify the output is "active"
Ok(status.trim() == "active") Ok(status.trim() == "active")
} }

View file

@ -464,7 +464,9 @@ fn init_logger() {
fn validate_freq(freq_mhz: u32, label: &str) -> Result<(), AppError> { fn validate_freq(freq_mhz: u32, label: &str) -> Result<(), AppError> {
if freq_mhz == 0 { if freq_mhz == 0 {
error!("{label} frequency cannot be zero"); error!("{label} frequency cannot be zero");
Err(AppError::Generic(format!("{label} frequency cannot be zero"))) Err(AppError::Generic(format!(
"{label} frequency cannot be zero"
)))
} else if freq_mhz > 10000 { } else if freq_mhz > 10000 {
// Extremely high value unlikely to be valid // Extremely high value unlikely to be valid
error!("{label} frequency ({freq_mhz} MHz) is unreasonably high"); error!("{label} frequency ({freq_mhz} MHz) is unreasonably high");