Initialize project with Rust setup, including .gitignore, LICENSE, README, and main functionality for PCIe device management.
This commit is contained in:
commit
2e7fe1afd3
5 changed files with 311 additions and 0 deletions
47
src/main.rs
Normal file
47
src/main.rs
Normal file
|
@ -0,0 +1,47 @@
|
|||
#![feature(file_buffered)]
|
||||
#![feature(buf_read_has_data_left)]
|
||||
use regex::Regex;
|
||||
use std::fs::File;
|
||||
use std::io::BufRead;
|
||||
use std::io::prelude::*;
|
||||
use std::time::Duration;
|
||||
|
||||
fn main() {
|
||||
println!("Reading kernel messages...");
|
||||
|
||||
let re = Regex::new(
|
||||
r"(?P<priority>\d+),(?P<sequence>\d+),(?P<timestamp>\d+),-;igb (?P<id>\d{4}:\d{2}:\d{2}.\d).*: PCIe link lost"
|
||||
).unwrap();
|
||||
let kmsg_file = File::open_buffered("/dev/kmsg").expect("Failed to open /dev/kmsg");
|
||||
|
||||
for line in kmsg_file.lines() {
|
||||
if let Ok(line) = line {
|
||||
let caps = re.captures(line.as_str());
|
||||
if let Some(caps) = caps {
|
||||
let timestamp = &caps["timestamp"];
|
||||
let id = &caps["id"];
|
||||
println!("Resetting {id}");
|
||||
|
||||
let rm_file_path = format!("/sys/bus/pci/devices/{id}/remove");
|
||||
let mut rm_file = File::create(&rm_file_path)
|
||||
.expect(format!("Failed to open remove file at {rm_file_path}").as_str());
|
||||
rm_file
|
||||
.write_all(b"1")
|
||||
.expect("Failed to write to remove file");
|
||||
println!("Removed device {id}");
|
||||
|
||||
let mut rescan_file =
|
||||
File::create("/sys/bus/pci/rescan").expect("Failed to open rescan file");
|
||||
rescan_file
|
||||
.write_all(b"1")
|
||||
.expect("Failed to write to rescan file");
|
||||
println!("Rescanned PCI bus");
|
||||
|
||||
let time = Duration::from_millis(
|
||||
timestamp.parse::<u64>().expect("Failed to parse timestamp"),
|
||||
);
|
||||
println!("Device {id} reset at {time:#?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue