Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when IO error for some cpu features #558

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,16 @@ pub fn daemon_init(settings: Settings, config: Config) -> Arc<Mutex<Daemon>> {
}

// Make a cpu struct for each cpu listed
for cpu in list_cpus() {
daemon.cpus.push(cpu);
match list_cpus() {
Ok(cpus) => {
for cpu in cpus {
daemon.cpus.push(cpu);
}
}
Err(e) => daemon.logger.log(
&format!("Failed to read from CPUs: {:?}", e),
logger::Severity::Error,
),
}

let daemon_mutex = Arc::new(Mutex::new(daemon));
Expand Down
70 changes: 57 additions & 13 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,15 @@ pub trait Getter {

impl Getter for Get {
fn freq(&self, raw: bool) {
let f = check_cpu_freq(&list_cpus());
print_freq(f, raw);
match list_cpus() {
Ok(cpus) => {
let f = check_cpu_freq(&cpus);
print_freq(f, raw);
}
Err(e) => {
eprint!("Failed to get cpu information, an error occured: {:?}", e);
}
}
}

fn power(&self, raw: bool) {
Expand Down Expand Up @@ -230,26 +237,63 @@ impl Getter for Get {
}

fn cpus(&self, raw: bool) {
let cpus = list_cpus();
match check_cpu_name() {
Ok(name) => print_cpus(cpus, name, raw),
Err(_) => println!("Failed get list of cpus"),
};
let cpus_result = list_cpus();
match cpus_result {
Ok(cpus) => {
match check_cpu_name() {
Ok(name) => print_cpus(cpus, name, raw),
Err(_) => println!("Failed get list of cpus"),
};
}
Err(e) => {
eprintln!("Failed to get cpu information, an error occured: {:?}", e);
}
}
}

fn speeds(&self, raw: bool) {
let speeds = list_cpu_speeds();
print_cpu_speeds(speeds, raw);
let speeds_result = list_cpu_speeds();
match speeds_result {
Ok(speeds) => {
print_cpu_speeds(speeds, raw);
}
Err(e) => {
eprintln!(
"Failed to get cpu speed information, an error occured: {:?}",
e
);
}
}
}

fn temp(&self, raw: bool) {
let cpu_temp = list_cpu_temp();
print_cpu_temp(cpu_temp, raw);
let cpu_temp_result = list_cpu_temp();
match cpu_temp_result {
Ok(cpu_temp) => {
print_cpu_temp(cpu_temp, raw);
}
Err(e) => {
eprintln!(
"Failed to get cpu temperature information, an error occured: {:?}",
e
);
}
}
}

fn govs(&self, raw: bool) {
let govs = list_cpu_governors();
print_cpu_governors(govs, raw);
let govs_result = list_cpu_governors();
match govs_result {
Ok(govs) => {
print_cpu_governors(govs, raw);
}
Err(e) => {
eprintln!(
"Failed to get cpu governor information, an error occured: {:?}",
e
);
}
}
}

fn bat_cond(&self, raw: bool) {
Expand Down
49 changes: 28 additions & 21 deletions src/system.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use cached::proc_macro::once;
use std::fs::{self, read_dir};
use std::path::Path;
use std::string::String;
Expand Down Expand Up @@ -182,8 +181,7 @@ pub fn check_available_governors() -> Result<Vec<String>, Error> {
}

/// Get all the cpus (cores), returns cpus from 0 to the (amount of cores -1) the machine has
#[once]
pub fn list_cpus() -> Vec<CPU> {
pub fn list_cpus() -> Result<Vec<CPU>, Error> {
let mut cpus: Vec<String> = Vec::<String>::new();

// Get each item in the cpu directory
Expand Down Expand Up @@ -228,30 +226,30 @@ pub fn list_cpus() -> Vec<CPU> {
gov: "Unknown".to_string(),
};

new.init_cpu().unwrap();
new.init_cpu()?;

new.update().unwrap();
new.update()?;

to_return.push(new)
}

to_return.sort_by(|a, b| a.number.cmp(&b.number));
to_return
Ok(to_return)
}

/// Get a vector of speeds reported from each cpu from list_cpus
pub fn list_cpu_speeds() -> Vec<i32> {
list_cpus().into_iter().map(|x| x.cur_freq).collect()
pub fn list_cpu_speeds() -> Result<Vec<i32>, Error> {
Ok(list_cpus()?.into_iter().map(|x| x.cur_freq).collect())
}

/// Get a vector of temperatures reported from each cpu from list_cpus
pub fn list_cpu_temp() -> Vec<i32> {
list_cpus().into_iter().map(|x| x.cur_temp).collect()
pub fn list_cpu_temp() -> Result<Vec<i32>, Error> {
Ok(list_cpus()?.into_iter().map(|x| x.cur_temp).collect())
}

/// Get a vector of the governors that the cpus from list_cpus
pub fn list_cpu_governors() -> Vec<String> {
list_cpus().into_iter().map(|x| x.gov).collect()
pub fn list_cpu_governors() -> Result<Vec<String>, Error> {
Ok(list_cpus()?.into_iter().map(|x| x.gov).collect())
}

pub fn read_int(path: &str) -> Result<i32, Error> {
Expand Down Expand Up @@ -282,7 +280,7 @@ mod tests {

#[test]
fn check_cpu_freq_acs_test() {
assert!(check_cpu_freq(&list_cpus()) > 0.0);
assert!(check_cpu_freq(&list_cpus().unwrap()) > 0.0);
}

#[test]
Expand Down Expand Up @@ -521,9 +519,9 @@ microcode : 0xea

#[test]
fn list_cpus_acs_test() {
assert_eq!(type_of(list_cpus()), type_of(Vec::<CPU>::new()));
assert_eq!(type_of(list_cpus().unwrap()), type_of(Vec::<CPU>::new()));

for x in list_cpus() {
for x in list_cpus().unwrap() {
assert!(!x.name.is_empty());
assert!(x.max_freq > 0);
assert!(x.min_freq > 0);
Expand All @@ -538,9 +536,12 @@ microcode : 0xea
#[test]
fn list_cpu_speeds_acs_test() -> Result<(), Error> {
// Type check
assert_eq!(type_of(list_cpu_speeds()), type_of(Vec::<i32>::new()));
assert_eq!(
type_of(list_cpu_speeds().unwrap()),
type_of(Vec::<i32>::new())
);

for x in list_cpu_speeds() {
for x in list_cpu_speeds().unwrap() {
assert!(x > 0);
}
Ok(())
Expand All @@ -549,19 +550,25 @@ microcode : 0xea
#[test]
fn list_cpu_temp_acs_test() {
// Type check
assert_eq!(type_of(list_cpu_temp()), type_of(Vec::<i32>::new()));
assert_eq!(
type_of(list_cpu_temp().unwrap()),
type_of(Vec::<i32>::new())
);

for x in list_cpu_temp() {
for x in list_cpu_temp().unwrap() {
assert!(x > -100);
}
}

#[test]
fn list_cpu_governors_acs_test() {
// Type check
assert_eq!(type_of(list_cpu_governors()), type_of(Vec::<String>::new()));
assert_eq!(
type_of(list_cpu_governors().unwrap()),
type_of(Vec::<String>::new())
);

for x in list_cpu_governors() {
for x in list_cpu_governors().unwrap() {
assert!(x == "powersave" || x == "performance" || x == "schedutil");
}
}
Expand Down
Loading