Skip to main content

Daemon

Struct Daemon 

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

Source

pub fn new() -> Self

Source

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

NOTE: If you provide a user, you must also provide a group.

Source

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.

Source

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

Source

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

umask for the process, defaults to 0o027

Source

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

Set this to be able to give inputs via stdio to the child

Source

pub fn stdout<T: Into<Stdio<'a>>>(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.

Source

pub fn stderr<T: Into<Stdio<'a>>>(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.

Source

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

Set the daemon process name

For example, this is what shows up in ps.

Source

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

Source

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();
Source

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();
Source

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();
Source

pub fn is_child(self) -> bool

Source

pub fn get_parent_pid(self) -> i32

Source

pub fn get_child_pid(self) -> Option<i32>

Source

pub fn get_pids(&self) -> Result<PidPair>

Source

pub fn start(&mut self) -> Result<PidPair>

Using the parameters set, daemonize the process

Trait Implementations§

Source§

impl<'a> Clone for Daemon<'a>

Source§

fn clone(&self) -> Daemon<'a>

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<'a> Debug for Daemon<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<'a> UnsafeUnpin 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> 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 = 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>,

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.