Struct daemonize_me::Daemon

source ·
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§

source§

impl<'a> Daemon<'a>

source

pub fn new() -> Self

source

pub fn pid_file<T: AsRef<Path>>(self, path: T, chmod: Option<bool>) -> Self

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)
source

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 /

source

pub fn user<T: Into<User>>(self, user: T) -> Self

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

source

pub fn group<T: Into<Group>>(self, group: T) -> Self

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

source

pub fn group_copy_user(self) -> Result<Self>

source

pub fn umask(self, mask: u16) -> Self

source

pub fn stdin<T: Into<Stdio>>(self, stdio: T) -> Self

source

pub fn stdout<T: Into<Stdio>>(self, stdio: T) -> Self

source

pub fn stderr<T: Into<Stdio>>(self, stdio: T) -> Self

source

pub fn name(self, name: &OsStr) -> Self

source

pub fn setup_pre_fork_hook(self, pre_fork_hook: fn(pid: i32)) -> Self

source

pub fn setup_post_fork_parent_hook( self, post_fork_parent_hook: fn(parent_pid: i32, child_pid: i32) -> !, ) -> Self

source

pub fn setup_post_fork_child_hook( self, post_fork_child_hook: fn(parent_pid: i32, child_pid: i32), ) -> Self

source

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

source

pub fn start(self) -> Result<()>

Using the parameters set, daemonize the process

Auto Trait Implementations§

§

impl<'a> Freeze for Daemon<'a>

§

impl<'a> !RefUnwindSafe for Daemon<'a>

§

impl<'a> !Send for Daemon<'a>

§

impl<'a> !Sync for Daemon<'a>

§

impl<'a> Unpin for Daemon<'a>

§

impl<'a> !UnwindSafe for Daemon<'a>

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>,

§

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>,

§

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.