Update rust docs

This commit is contained in:
Adrian Woźniak 2022-02-27 21:58:34 +01:00
parent 511e1863a9
commit 06aca5e3ce
No known key found for this signature in database
GPG Key ID: 0012845A89C7352B

View File

@ -56,6 +56,7 @@ impl PidLock {
/// Create new lock file. File will be created if: /// Create new lock file. File will be created if:
/// * pid file does not exists /// * pid file does not exists
/// * pid file exists but process is dead /// * pid file exists but process is dead
/// * old pid and current pid have different names (lock file exists after reboot and PID was taken by other process)
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); log::debug!("PID LOCK acquiring {}", self.pid_path);
let pid = self.process_pid(); let pid = self.process_pid();
@ -98,7 +99,6 @@ impl PidLock {
Ok(()) Ok(())
} }
/// Remove lock file
/// Remove lock file /// Remove lock file
pub fn release(&mut self) -> Result<(), crate::error::AmdGpuError> { pub fn release(&mut self) -> Result<(), crate::error::AmdGpuError> {
if let Err(e) = std::fs::remove_file(&self.pid_path) { if let Err(e) = std::fs::remove_file(&self.pid_path) {
@ -107,6 +107,7 @@ impl PidLock {
Ok(()) Ok(())
} }
/// Read old pid value from file
fn old_pid(&self) -> Option<Result<Pid, LockFileError>> { fn old_pid(&self) -> Option<Result<Pid, LockFileError>> {
match std::fs::read_to_string(&self.pid_path) { match std::fs::read_to_string(&self.pid_path) {
Err(e) if e.kind() == std::io::ErrorKind::NotFound => None, Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
@ -121,6 +122,7 @@ impl PidLock {
} }
} }
/// Check if PID is alive
fn is_alive(&self, pid: Pid) -> bool { fn is_alive(&self, pid: Pid) -> bool {
unsafe { unsafe {
let result = libc::kill(pid.0, 0); let result = libc::kill(pid.0, 0);
@ -128,10 +130,12 @@ impl PidLock {
} }
} }
/// Get current process PID
fn process_pid(&self) -> Pid { fn process_pid(&self) -> Pid {
Pid(std::process::id() as i32) Pid(std::process::id() as i32)
} }
/// Read target process name
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)) { 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 => {
@ -142,6 +146,7 @@ impl PidLock {
} }
} }
/// Override pid lock file
fn enforce_pid_file(&self, pid: Pid) -> Result<(), LockFileError> { fn enforce_pid_file(&self, pid: Pid) -> Result<(), LockFileError> {
std::fs::write(&self.pid_path, format!("{}", pid.0)) std::fs::write(&self.pid_path, format!("{}", pid.0))
.map_err(|e| LockFileError::Io { pid, err: e }) .map_err(|e| LockFileError::Io { pid, err: e })