pub struct Daemon<'a> { /* private fields */ }
Expand description

Basic daemonization consists of: forking the process, getting a new sid, setting the umask, changing the standard io streams to files and finally dropping privileges.

Options:

  • user [optional], if set will drop privileges to the specified user NOTE: This library is strict and makes no assumptions if you provide a user you must provide a group

  • group [optional(see note on user)], if set will drop privileges to specified group

  • umask [optional], umask for the process defaults to 0o027

  • pid_file [optional], if set a pid file will be created default is that no file is created *

  • stdio [optional][recommended], this determines where standard output will be piped to since daemons have no console it’s highly recommended to set this

  • stderr [optional][recommended], same as above but for standard error

  • chdir [optional], default is “/”

  • name [optional], set the daemon process name eg what shows in ps default is to not set a process name

  • before_fork_hook [optional], called before the fork with the current pid as argument

  • after_fork_parent_hook [optional], called after the fork with the parent pid as argument, can be used to continue some work on the parent after the fork (do not return)

  • after_fork_child_hook [optional], called after the fork with the parent and child pid as arguments

  • See the setter function documentation for more details

Beware there is no escalation back if dropping privileges

Implementations

This is a setter to give your daemon a pid file

Arguments
  • path - path to the file suggested /var/run/my_program_name.pid
  • chmod - 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)

As the last step the code will change the working directory to this one defaults to /

The code will attempt to drop privileges with setuid to the provided user

The code will attempt to drop privileges with setgid to the provided group, you mut provide a group if you provide an user

pub fn setup_post_fork_parent_hook(
    self,
    post_fork_parent_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

pub fn setup_post_init_hook(
    self,
    post_fork_child_hook: fn(ctx: Option<&'a dyn Any>),
    data: Option<&'a dyn Any>
) -> Self

Using the parameters set, daemonize the process

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.