pub struct PidLockGuard { /* private fields */ }Expand description
Exclusive, flock(2)-backed guard on a pid file.
Acquiring the lock writes the current process PID to the file. Dropping the guard unlinks the file and then closes the file descriptor, releasing the lock.
The fd inheritance invariant: the file is opened with O_CLOEXEC, so child processes
spawned via Command do NOT inherit the lock. If you re-exec the binary, the new
process must call PidLockGuard::acquire independently.
§Examples
use zeph_common::pidfile::PidLockGuard;
let path = std::env::temp_dir().join(format!("zeph-pidfile-doctest-{}.pid", std::process::id()));
let guard = PidLockGuard::acquire(&path).expect("no other instance running");
assert_eq!(guard.path(), path.as_path());
drop(guard); // releases the lock and removes the pid file
assert!(!path.exists());Implementations§
Source§impl PidLockGuard
impl PidLockGuard
Sourcepub fn acquire(path: &Path) -> Result<Self, PidLockError>
pub fn acquire(path: &Path) -> Result<Self, PidLockError>
Open (or create) the pid file at path and acquire an exclusive advisory lock.
The sequence is:
open(O_RDWR | O_CREAT | O_CLOEXEC, 0o644)— atomic create-or-open.flock(LOCK_EX | LOCK_NB)— fails immediately if already locked.ftruncate(0)+ write current PID.
§Errors
PidLockError::AlreadyRunningif another process holds the lock.PidLockError::Iofor filesystem errors.
Trait Implementations§
Source§impl Debug for PidLockGuard
impl Debug for PidLockGuard
Source§impl Drop for PidLockGuard
impl Drop for PidLockGuard
Auto Trait Implementations§
impl Freeze for PidLockGuard
impl RefUnwindSafe for PidLockGuard
impl Send for PidLockGuard
impl Sync for PidLockGuard
impl Unpin for PidLockGuard
impl UnsafeUnpin for PidLockGuard
impl UnwindSafe for PidLockGuard
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more