diff --git a/Cargo.toml b/Cargo.toml index 1ba57bc..3734388 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "phenix" description = "Utility to restart PCIe devices when link is lost." -version = "0.1.4" +version = "0.2.1" edition = "2024" license = "GPL-3.0-or-later" repository="https://code.edgarpierre.fr/edpibu/phenix" diff --git a/src/main.rs b/src/main.rs index e13669e..a9e001c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,20 @@ #![feature(file_buffered)] +use std::env; use std::fs::File; use std::io::BufRead; use std::io::Write; +use std::io::Seek; +use std::io::SeekFrom; fn main() { println!("Reading kernel messages..."); - let kmsg_file = File::open_buffered("/dev/kmsg").expect("Failed to open /dev/kmsg"); + let mut kmsg_file = + File::open_buffered(env::var("PHENIX_KMSG").unwrap_or(String::from("/dev/kmsg"))) + .expect("Failed to open /dev/kmsg"); + + kmsg_file.seek(SeekFrom::End(0)).expect("Failed to seek to end"); let lines = kmsg_file.lines(); for line in lines { match line { @@ -23,8 +30,9 @@ fn manage_line(line: String) { println!("PCIe link lost {prefix}"); let id = line.splitn(3, " ").nth(1); - if let Some(id) = id { - reset_device(id); + match id { + Some(id) => reset_device(id), + None => println!("No device id found in message {line}"), } } } @@ -33,6 +41,7 @@ fn manage_line(line: String) { fn reset_device(id: &str) { println!("Resetting {id}"); echo1(rm_file_path(&id)); + println!("Removed device {id}"); echo1(String::from("/sys/bus/pci/rescan")); println!("Rescanned PCI bus"); } @@ -44,7 +53,6 @@ fn echo1(path: String) { Ok(mut file) => { file.write_all(b"1") .expect("Failed to write to remove file"); - println!("Removed device {path}"); } } }