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
impl Daemon
Sourcepub fn user(self, user: Option<Privileges>) -> Self
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.
Sourcepub fn working_directory<P: AsRef<Path>>(self, workdir: Option<P>) -> Self
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 (/).
Sourcepub fn pidfile<P: AsRef<Path>>(self, path: Option<P>) -> Self
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.
Sourcepub fn stdout<P: AsRef<Path>>(self, path: Option<P>) -> Self
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).
Sourcepub fn stderr<P: AsRef<Path>>(self, path: Option<P>) -> Self
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).
Sourcepub fn umask(self, mode: Option<Mode>) -> Self
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.
Sourcepub fn daemonize(self) -> Parent
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:
- The PID file is created.
- Privileges are dropped.
- The working directory is changed.
- The umask is set.
- Standard input is redirected to
/dev/null. - Standard output is redirected.
- 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.