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 {
None => service::run(config),
Some(FanCommand::Service(_)) => {
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
}
None => run_service(config, opts),
Some(FanCommand::Service(_)) => run_service(config, opts),
Some(FanCommand::SetAutomatic(switcher)) => {
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)> {
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "DEBUG");

View File

@ -49,6 +49,7 @@ impl PidLock {
.map(String::from)
.unwrap()
};
log::debug!("Creating pid lock for path {:?}", pid_path);
Ok(Self { pid_path, name })
}
@ -56,26 +57,42 @@ impl PidLock {
/// * pid file does not exists
/// * pid file exists but process is dead
pub fn acquire(&mut self) -> Result<(), crate::error::AmdGpuError> {
log::debug!("PID LOCK acquiring {}", self.pid_path);
let pid = self.process_pid();
if let Some(old) = self.old_pid() {
let old = old?;
if !self.is_alive(old) {
log::debug!("Old pid {:?} is dead, overriding...", old.0);
self.enforce_pid_file(pid)?;
return Ok(());
}
match self.process_name(old) {
Err(LockFileError::NotExists(..)) => {
log::debug!(
"Old pid {:?} doesn't have process stat, overriding....",
old.0
);
self.enforce_pid_file(old)?;
}
Err(e) => return Err(e.into()),
Ok(name) if name == self.name => {
return Err(LockFileError::Conflict { pid: old, name }.into())
Err(e) => {
log::debug!("Lock error {:?}", e);
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)?;
}
}
} else {
log::debug!("No collision detected");
self.enforce_pid_file(pid)?;
}
Ok(())
@ -116,12 +133,12 @@ impl PidLock {
}
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(LockFileError::NotExists(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>> {
Ok(read_cards()?
.into_iter()
.map(|card| {
.flat_map(|card| {
log::info!("opening hw mon for {:?}", card);
hw_mon::open_hw_mon(card)
})
.flatten()
.filter(|hw_mon| {
!filter || {
log::info!("is vendor ok? {}", hw_mon.is_amd());

View File

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