pub struct Daemon<'a> { /* private fields */ }Expand description
Basic daemonization consists of: forking the process, getting a new Session ID (sid), setting the umask, changing the standard io streams to files and finally dropping privileges.
NOTE: Beware there is no escalation back if dropping privileges
Implementations§
Source§impl<'a> Daemon<'a>
impl<'a> Daemon<'a>
pub fn new() -> Self
Sourcepub fn pid_file<T: AsRef<Path>>(self, path: T, chmod: Option<bool>) -> Self
pub fn pid_file<T: AsRef<Path>>(self, path: T, chmod: Option<bool>) -> Self
Give your daemon a pid file
By default, no pid file is created.
§Arguments
path- path to the file suggested/var/run/my_program_name.pidchmod- if set a chmod of the file to the user and group passed will be attempted (this being true makes setting an user and group mandatory)
Sourcepub fn work_dir<T: AsRef<Path>>(self, path: T) -> Self
pub fn work_dir<T: AsRef<Path>>(self, path: T) -> Self
As the last step the code will change the working directory to this one defaults to /
Sourcepub fn user<T: Into<User>>(self, user: T) -> Self
pub fn user<T: Into<User>>(self, user: T) -> Self
The code will attempt to drop privileges with setuid to the provided user
NOTE: If you provide a user, you must also provide a group.
Sourcepub fn group<T: Into<Group>>(self, group: T) -> Self
pub fn group<T: Into<Group>>(self, group: T) -> Self
The code will attempt to drop privileges with setgid to the provided group
NOTE: You must provide a group if you provide an user.
pub fn group_copy_user(self) -> Result<Self>
pub fn stdin<T: Into<Stdio>>(self, stdio: T) -> Self
Sourcepub fn stdout<T: Into<Stdio>>(self, stdio: T) -> Self
pub fn stdout<T: Into<Stdio>>(self, stdio: T) -> Self
Determines where standard output will be piped to since daemons have no console attached
It’s highly recommended to set this to a file if you want to see output.
Sourcepub fn stderr<T: Into<Stdio>>(self, stdio: T) -> Self
pub fn stderr<T: Into<Stdio>>(self, stdio: T) -> Self
Determines where standard error will be piped to since daemons have no console attached
It’s highly recommended to set this to a file if you want to see output.
Sourcepub fn name(self, name: &OsStr) -> Self
pub fn name(self, name: &OsStr) -> Self
Set the daemon process name
For example, this is what shows up in ps.
Sourcepub fn setup_pre_fork_hook(self, pre_fork_hook: fn(pid: i32)) -> Self
pub fn setup_pre_fork_hook(self, pre_fork_hook: fn(pid: i32)) -> Self
Hook called before the fork with the current pid as argument
Sourcepub fn setup_post_fork_parent_hook(
self,
post_fork_parent_hook: fn(parent_pid: i32, child_pid: i32) -> !,
) -> Self
pub fn setup_post_fork_parent_hook( self, post_fork_parent_hook: fn(parent_pid: i32, child_pid: i32) -> !, ) -> Self
Hook called after the fork with the parent pid as argument
Can be used to continue some work on the parent after the fork.
NOTE: This hook must not return! For instance, you could call std::process::exit().
§Examples
fn post_fork_parent(ppid: i32, cpid: i32) -> ! {
println!("Parent pid: {}, Child pid {}", ppid, cpid);
println!("Exiting parent now");
std::process::exit(0);
}
let daemon = Daemon::new()
.setup_post_fork_parent_hook(post_fork_parent)
.start();Sourcepub fn setup_post_fork_child_hook(
self,
post_fork_child_hook: fn(parent_pid: i32, child_pid: i32),
) -> Self
pub fn setup_post_fork_child_hook( self, post_fork_child_hook: fn(parent_pid: i32, child_pid: i32), ) -> Self
Hook called after the fork with the parent and child pid as arguments
§Examples
fn post_fork_child(ppid: i32, cpid: i32) {
println!("Parent pid: {}, Child pid {}", ppid, cpid);
println!("This hook is called in the child");
// Child hook must return
return
}
let daemon = Daemon::new()
.setup_post_fork_child_hook(post_fork_child)
.start();Sourcepub fn setup_post_init_hook(
self,
post_fork_child_hook: fn(ctx: Option<&'a dyn Any>),
data: Option<&'a dyn Any>,
) -> Self
pub fn setup_post_init_hook( self, post_fork_child_hook: fn(ctx: Option<&'a dyn Any>), data: Option<&'a dyn Any>, ) -> Self
Hook called right before returning control to the caller, that is, right after start()
§Examples
fn after_init(_: Option<&dyn Any>) {
println!("Initialized the daemon!");
return
}
let daemon = Daemon::new()
.setup_post_init_hook(after_init, None)
.start();