Skip to main content

Daemon

Struct Daemon 

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

Options which can be used to configure the daemon process.

This builder exposes the ability to configure various properties and enable common setup actions such as setting the working directory and creating a PID file.

Generally speaking, when using Daemon, you’ll first call Daemon::new, then chain calls to methods to set each option before ultimately calling Daemon::daemonize to fork the process and create the daemon. All code after the call to Daemon::daemonize will be run inside the daemon process. This method will give you a Parent that you can use to notify the original process of any immediate errors you encounter while setting up.

All of the configuration methods take values wrapped in Option. This is to make Daemon compatible with various external configuration methods such as command-line arguments and config files. Passing None to these methods resets them back to the default values. See the individual methods to find out what their default values are.

Although there is no option for configuring stdin redirection, the standard input stream will be redirected to /dev/null (any attempts to read from stdin will receive an immediate EOF).

§Examples

use dmon::{user::Privileges, Daemon};

struct Config {
    user: Option<Privileges>,
}

let config = Config {
    user: Some("daemon".parse().unwrap()),
};

let mut parent = Daemon::new()
                  .pidfile(Some("/run/mydaemon.pid"))
                  .working_directory(Some("/var/lib/mydaemon"))
                  .user(config.user)
                  .stdout(Some("mydaemon.out"))
                  .stderr(Some("mydaemon.err"))
                  .daemonize();

// Perform setup like listening on a port or socket, creating a file, etc...

parent.success().unwrap();

Implementations§

Source§

impl Daemon

Source

pub fn new() -> Self

Creates a new daemon configuration with default values.

Source

pub fn user(self, user: Option<Privileges>) -> Self

Sets the user and group the daemon will run as.

If this configuration value is present, the process will drop its privileges to the given user and group. Note that this will normally require the parent process to be started as the root user.

By default, no value is present and the daemon will run with the same privileges as the original process.

Source

pub fn working_directory<P: AsRef<Path>>(self, workdir: Option<P>) -> Self

Changes the daemon’s working directory to the specified path.

This is done after dropping privileges. Pass . to this method to avoid changing the working directory.

By default, this will be the root directory (/).

Source

pub fn pidfile<P: AsRef<Path>>(self, path: Option<P>) -> Self

Requests the daemon to create a PID file.

The daemon will write its process ID and a trailing newline to the specified file. The file must not already exist. This file is created before dropping privileges. If a relative path is given, it will be relative to the parent’s starting directory.

By default, no PID file is created.

Source

pub fn stdout<P: AsRef<Path>>(self, path: Option<P>) -> Self

Redirects the daemon’s standard output stream to the specified file.

The file will be created if it does not exist and will be appended to if it does. Relative paths are relative to the daemon’s working directory.

By default, stdout is redirected to /dev/null (writes will succeed and be immediately discarded).

Source

pub fn stderr<P: AsRef<Path>>(self, path: Option<P>) -> Self

Redirects the daemon’s standard error stream to the specified file.

The file will be created if it does not exist and will be appended to if it does. Relative paths are relative to the daemon’s working directory.

By default, stderr is redirected to /dev/null (writes will succeed and be immediately discarded).

Source

pub fn umask(self, mode: Option<Mode>) -> Self

Sets the daemon process’s file mode creation mask.

See umask(2) for more information.

The default value is DEFAULT_UMASK.

Source

pub fn daemonize(self) -> Parent

Creates the daemon by forking the process.

After the daemon process is created, the configuration is applied in the following order:

  1. The PID file is created.
  2. Privileges are dropped.
  3. The working directory is changed.
  4. The umask is set.
  5. Standard input is redirected to /dev/null.
  6. Standard output is redirected.
  7. Standard error is redirected.

All of the code that follows this call is run by the daemon. The original parent process waits for the child to prepare itself and is terminated by calling exit upon receiving the child’s status via the returned Parent object. It is left up to the caller to decide when the daemon can be considered “ready”. This could be immediately after forking or after some specific actions have been taken, such as listening on a port.

§Safety

This function is unsafe to call from a multi-threaded environment.

Trait Implementations§

Source§

impl Clone for Daemon

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Daemon

Source§

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

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

impl Default for Daemon

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.