Struct Pidfile

Source
pub struct Pidfile { /* private fields */ }
Expand description

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§

Source§

impl Pidfile

Source

pub fn new( 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.

Source

pub fn write(&mut self) -> Result<(), PidfileError>

Writes the current process ID to the PID file.

The file is truncated before writing.

Source

pub fn close(self)

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§

Source§

impl Debug for Pidfile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Pidfile

Source§

fn drop(&mut self)

Closes the PID file and removes it.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.