[−][src]Struct pidfile_rs::Pidfile
A PID file protected with a lock.
An instance of Pidfile
can be used to manage a PID file: create it,
lock it, detect already running daemons. It is backed by pidfile
functions of libbsd
/libutil
which use flopen
to lock the PID
file.
When a PID file is created, the process ID of the current process is not written there, making it possible to lock the PID file before forking and only write the ID of the forked process when it is ready.
The PID file is deleted automatically when the Pidfile
comes out of
the scope. To close the PID file without deleting it, for example, in
the parent process of a forked daemon, call close()
.
Example
When the parent process exits without calling destructors, e.g. by
using exit
or when forking with daemon
(3), Pidfile
can
be used in the following way:
// This example uses daemon(3) wrapped by nix to daemonise: use nix::unistd::daemon; use pidfile_rs::Pidfile; // ... let pidfile = Some(Pidfile::new( &pidfile_path, Permissions::from_mode(0o600) )?); // do some pre-fork preparations // ... // in the parent process, the PID file is closed without deleting it daemon(false, true)?; pidfile.unwrap().write(); // do some work println!("The daemon’s work is done, now it’s time to go home."); // the PID file will be deleted when this function returns
Implementations
impl Pidfile
[src]
pub fn new(
path: &PathBuf,
permissions: Permissions
) -> Result<Pidfile, PidfileError>
[src]
path: &PathBuf,
permissions: Permissions
) -> Result<Pidfile, PidfileError>
Creates a new PID file and locks it.
If the PID file cannot be locked, returns PidfileError::AlreadyRunning
with
a PID of the already running process, or None
if no PID has been written to
the PID file yet.
pub fn write(&self) -> Result<(), PidfileError>
[src]
Writes the current process ID to the PID file.
The file is truncated before writing.
pub fn close(self)
[src]
Closes the PID file without removing it.
This function consumes the object, making it impossible to manipulated with the PID file after this function has been called.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Pidfile
impl !Send for Pidfile
impl !Sync for Pidfile
impl Unpin for Pidfile
impl UnwindSafe for Pidfile
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,