Fix conflict recognition

This commit is contained in:
Adrian Woźniak 2022-02-27 21:49:08 +01:00
parent 592395bb48
commit 511e1863a9
No known key found for this signature in database
GPG Key ID: 0012845A89C7352B
4 changed files with 48 additions and 28 deletions

View File

@ -55,18 +55,8 @@ fn run(config: Config) -> Result<()> {
} }
match opts.command { match opts.command {
None => service::run(config), None => run_service(config, opts),
Some(FanCommand::Service(_)) => { Some(FanCommand::Service(_)) => run_service(config, opts),
let mut pid_file = PidLock::new(
"amdfand",
opts.pid_file
.unwrap_or_else(|| String::from(DEFAULT_PID_FILE_NAME)),
)?;
pid_file.acquire()?;
let res = service::run(config);
pid_file.release()?;
res
}
Some(FanCommand::SetAutomatic(switcher)) => { Some(FanCommand::SetAutomatic(switcher)) => {
change_mode::run(switcher, FanMode::Automatic, config) change_mode::run(switcher, FanMode::Automatic, config)
} }
@ -87,6 +77,18 @@ fn run(config: Config) -> Result<()> {
} }
} }
fn run_service(config: Config, opts: Opts) -> Result<()> {
let mut pid_file = PidLock::new(
"amdfand",
opts.pid_file
.unwrap_or_else(|| String::from(DEFAULT_PID_FILE_NAME)),
)?;
pid_file.acquire()?;
let res = service::run(config);
pid_file.release()?;
res
}
fn setup() -> Result<(String, Config)> { fn setup() -> Result<(String, Config)> {
if std::env::var("RUST_LOG").is_err() { if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "DEBUG"); std::env::set_var("RUST_LOG", "DEBUG");

View File

@ -49,6 +49,7 @@ impl PidLock {
.map(String::from) .map(String::from)
.unwrap() .unwrap()
}; };
log::debug!("Creating pid lock for path {:?}", pid_path);
Ok(Self { pid_path, name }) Ok(Self { pid_path, name })
} }
@ -56,26 +57,42 @@ impl PidLock {
/// * pid file does not exists /// * pid file does not exists
/// * pid file exists but process is dead /// * pid file exists but process is dead
pub fn acquire(&mut self) -> Result<(), crate::error::AmdGpuError> { pub fn acquire(&mut self) -> Result<(), crate::error::AmdGpuError> {
log::debug!("PID LOCK acquiring {}", self.pid_path);
let pid = self.process_pid(); let pid = self.process_pid();
if let Some(old) = self.old_pid() { if let Some(old) = self.old_pid() {
let old = old?; let old = old?;
if !self.is_alive(old) { if !self.is_alive(old) {
log::debug!("Old pid {:?} is dead, overriding...", old.0);
self.enforce_pid_file(pid)?; self.enforce_pid_file(pid)?;
return Ok(()); return Ok(());
} }
match self.process_name(old) { match self.process_name(old) {
Err(LockFileError::NotExists(..)) => { Err(LockFileError::NotExists(..)) => {
log::debug!(
"Old pid {:?} doesn't have process stat, overriding....",
old.0
);
self.enforce_pid_file(old)?; self.enforce_pid_file(old)?;
} }
Err(e) => return Err(e.into()), Err(e) => {
Ok(name) if name == self.name => { log::debug!("Lock error {:?}", e);
return Err(LockFileError::Conflict { pid: old, name }.into()) return Err(e.into());
} }
Ok(_ /*name isn't the same*/) => { Ok(name) if name.ends_with(&format!("/{}", self.name)) => {
log::warn!("Conflicting {} and {} for process {}", old.0, pid.0, name);
return Err(LockFileError::Conflict { pid: old, name }.into());
}
Ok(name /*name isn't the same*/) => {
log::debug!(
"Old process {:?} and current process {:?} have different names, overriding....",
name, self.name
);
self.enforce_pid_file(old)?; self.enforce_pid_file(old)?;
} }
} }
} else { } else {
log::debug!("No collision detected");
self.enforce_pid_file(pid)?; self.enforce_pid_file(pid)?;
} }
Ok(()) Ok(())
@ -116,12 +133,12 @@ impl PidLock {
} }
fn process_name(&self, pid: Pid) -> Result<String, LockFileError> { fn process_name(&self, pid: Pid) -> Result<String, LockFileError> {
match std::fs::read_to_string(format!("/proc/{}/cmdline", pid.to_string())) { match std::fs::read_to_string(format!("/proc/{}/cmdline", *pid)) {
Err(e) if e.kind() == std::io::ErrorKind::NotFound => { Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
Err(LockFileError::NotExists(pid)) Err(LockFileError::NotExists(pid))
} }
Err(err) => Err(LockFileError::Io { err, pid }), Err(err) => Err(LockFileError::Io { err, pid }),
Ok(s) => Ok(s), Ok(s) => Ok(String::from(s.split('\0').next().unwrap_or_default())),
} }
} }

View File

@ -62,11 +62,10 @@ pub fn read_cards() -> std::io::Result<Vec<Card>> {
pub fn hw_mons(filter: bool) -> std::io::Result<Vec<HwMon>> { pub fn hw_mons(filter: bool) -> std::io::Result<Vec<HwMon>> {
Ok(read_cards()? Ok(read_cards()?
.into_iter() .into_iter()
.map(|card| { .flat_map(|card| {
log::info!("opening hw mon for {:?}", card); log::info!("opening hw mon for {:?}", card);
hw_mon::open_hw_mon(card) hw_mon::open_hw_mon(card)
}) })
.flatten()
.filter(|hw_mon| { .filter(|hw_mon| {
!filter || { !filter || {
log::info!("is vendor ok? {}", hw_mon.is_amd()); log::info!("is vendor ok? {}", hw_mon.is_amd());

View File

@ -17,14 +17,16 @@ impl ConfigFile {
impl Widget for ConfigFile { impl Widget for ConfigFile {
fn ui(self, ui: &mut Ui) -> Response { fn ui(self, ui: &mut Ui) -> Response {
ui.vertical(|ui| { ui.vertical(|ui| {
let mut matrix = self let mut matrix = {
.config #[allow(clippy::unnecessary_to_owned)]
.lock() self.config
.speed_matrix() .lock()
.to_vec() .speed_matrix()
.into_iter() .to_vec()
.enumerate() .into_iter()
.peekable(); .enumerate()
.peekable()
};
let mut prev = None; let mut prev = None;