Struct 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

Examples found in repository?
examples/example-lingering.rs (line 10)
7fn main() {
8    let stdout = File::create("info.log").unwrap();
9    let stderr = File::create("err.log").unwrap();
10    let daemon = Daemon::new()
11        .pid_file("example.pid", Some(false))
12        .user(User::try_from("daemon").unwrap())
13        .group(Group::try_from("daemon").unwrap())
14        .umask(0o000)
15        .work_dir(".")
16        .stdout(stdout)
17        .stderr(stderr)
18        .start();
19
20    match daemon {
21        Ok(_) => println!("Daemonized with success"),
22        Err(e) => eprintln!("Error, {}", e),
23    }
24
25    loop {
26        // You wil have to kill this process yourself
27    }
28}
More examples
Hide additional examples
examples/example.rs (line 32)
29fn main() {
30    let stdout = File::create("info.log").unwrap();
31    let stderr = File::create("err.log").unwrap();
32    let daemon = Daemon::new()
33        .pid_file("example.pid", Some(false))
34        .umask(0o000)
35        .work_dir(".")
36        .stdout(stdout)
37        .stderr(stderr)
38        // Hooks are optional
39        .setup_post_fork_parent_hook(post_fork_parent)
40        .setup_post_fork_child_hook(post_fork_child)
41        .setup_post_init_hook(after_init, None)
42        // Start the daemon and calls the hooks
43        .start();
44
45    match daemon {
46        Ok(_) => println!("Daemonized with success"),
47        Err(e) => {
48            eprintln!("Error, {}", e);
49            exit(-1);
50        },
51    }
52
53    for i in 0..=10000 {
54        println!("{}", i);
55    }
56}
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)
Examples found in repository?
examples/example-lingering.rs (line 11)
7fn main() {
8    let stdout = File::create("info.log").unwrap();
9    let stderr = File::create("err.log").unwrap();
10    let daemon = Daemon::new()
11        .pid_file("example.pid", Some(false))
12        .user(User::try_from("daemon").unwrap())
13        .group(Group::try_from("daemon").unwrap())
14        .umask(0o000)
15        .work_dir(".")
16        .stdout(stdout)
17        .stderr(stderr)
18        .start();
19
20    match daemon {
21        Ok(_) => println!("Daemonized with success"),
22        Err(e) => eprintln!("Error, {}", e),
23    }
24
25    loop {
26        // You wil have to kill this process yourself
27    }
28}
More examples
Hide additional examples
examples/example.rs (line 33)
29fn main() {
30    let stdout = File::create("info.log").unwrap();
31    let stderr = File::create("err.log").unwrap();
32    let daemon = Daemon::new()
33        .pid_file("example.pid", Some(false))
34        .umask(0o000)
35        .work_dir(".")
36        .stdout(stdout)
37        .stderr(stderr)
38        // Hooks are optional
39        .setup_post_fork_parent_hook(post_fork_parent)
40        .setup_post_fork_child_hook(post_fork_child)
41        .setup_post_init_hook(after_init, None)
42        // Start the daemon and calls the hooks
43        .start();
44
45    match daemon {
46        Ok(_) => println!("Daemonized with success"),
47        Err(e) => {
48            eprintln!("Error, {}", e);
49            exit(-1);
50        },
51    }
52
53    for i in 0..=10000 {
54        println!("{}", i);
55    }
56}
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 /

Examples found in repository?
examples/example-lingering.rs (line 15)
7fn main() {
8    let stdout = File::create("info.log").unwrap();
9    let stderr = File::create("err.log").unwrap();
10    let daemon = Daemon::new()
11        .pid_file("example.pid", Some(false))
12        .user(User::try_from("daemon").unwrap())
13        .group(Group::try_from("daemon").unwrap())
14        .umask(0o000)
15        .work_dir(".")
16        .stdout(stdout)
17        .stderr(stderr)
18        .start();
19
20    match daemon {
21        Ok(_) => println!("Daemonized with success"),
22        Err(e) => eprintln!("Error, {}", e),
23    }
24
25    loop {
26        // You wil have to kill this process yourself
27    }
28}
More examples
Hide additional examples
examples/example.rs (line 35)
29fn main() {
30    let stdout = File::create("info.log").unwrap();
31    let stderr = File::create("err.log").unwrap();
32    let daemon = Daemon::new()
33        .pid_file("example.pid", Some(false))
34        .umask(0o000)
35        .work_dir(".")
36        .stdout(stdout)
37        .stderr(stderr)
38        // Hooks are optional
39        .setup_post_fork_parent_hook(post_fork_parent)
40        .setup_post_fork_child_hook(post_fork_child)
41        .setup_post_init_hook(after_init, None)
42        // Start the daemon and calls the hooks
43        .start();
44
45    match daemon {
46        Ok(_) => println!("Daemonized with success"),
47        Err(e) => {
48            eprintln!("Error, {}", e);
49            exit(-1);
50        },
51    }
52
53    for i in 0..=10000 {
54        println!("{}", i);
55    }
56}
Source

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

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

Examples found in repository?
examples/example-lingering.rs (line 12)
7fn main() {
8    let stdout = File::create("info.log").unwrap();
9    let stderr = File::create("err.log").unwrap();
10    let daemon = Daemon::new()
11        .pid_file("example.pid", Some(false))
12        .user(User::try_from("daemon").unwrap())
13        .group(Group::try_from("daemon").unwrap())
14        .umask(0o000)
15        .work_dir(".")
16        .stdout(stdout)
17        .stderr(stderr)
18        .start();
19
20    match daemon {
21        Ok(_) => println!("Daemonized with success"),
22        Err(e) => eprintln!("Error, {}", e),
23    }
24
25    loop {
26        // You wil have to kill this process yourself
27    }
28}
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

Examples found in repository?
examples/example-lingering.rs (line 13)
7fn main() {
8    let stdout = File::create("info.log").unwrap();
9    let stderr = File::create("err.log").unwrap();
10    let daemon = Daemon::new()
11        .pid_file("example.pid", Some(false))
12        .user(User::try_from("daemon").unwrap())
13        .group(Group::try_from("daemon").unwrap())
14        .umask(0o000)
15        .work_dir(".")
16        .stdout(stdout)
17        .stderr(stderr)
18        .start();
19
20    match daemon {
21        Ok(_) => println!("Daemonized with success"),
22        Err(e) => eprintln!("Error, {}", e),
23    }
24
25    loop {
26        // You wil have to kill this process yourself
27    }
28}
Source

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

Source

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

Examples found in repository?
examples/example-lingering.rs (line 14)
7fn main() {
8    let stdout = File::create("info.log").unwrap();
9    let stderr = File::create("err.log").unwrap();
10    let daemon = Daemon::new()
11        .pid_file("example.pid", Some(false))
12        .user(User::try_from("daemon").unwrap())
13        .group(Group::try_from("daemon").unwrap())
14        .umask(0o000)
15        .work_dir(".")
16        .stdout(stdout)
17        .stderr(stderr)
18        .start();
19
20    match daemon {
21        Ok(_) => println!("Daemonized with success"),
22        Err(e) => eprintln!("Error, {}", e),
23    }
24
25    loop {
26        // You wil have to kill this process yourself
27    }
28}
More examples
Hide additional examples
examples/example.rs (line 34)
29fn main() {
30    let stdout = File::create("info.log").unwrap();
31    let stderr = File::create("err.log").unwrap();
32    let daemon = Daemon::new()
33        .pid_file("example.pid", Some(false))
34        .umask(0o000)
35        .work_dir(".")
36        .stdout(stdout)
37        .stderr(stderr)
38        // Hooks are optional
39        .setup_post_fork_parent_hook(post_fork_parent)
40        .setup_post_fork_child_hook(post_fork_child)
41        .setup_post_init_hook(after_init, None)
42        // Start the daemon and calls the hooks
43        .start();
44
45    match daemon {
46        Ok(_) => println!("Daemonized with success"),
47        Err(e) => {
48            eprintln!("Error, {}", e);
49            exit(-1);
50        },
51    }
52
53    for i in 0..=10000 {
54        println!("{}", i);
55    }
56}
Source

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

Source

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

Examples found in repository?
examples/example-lingering.rs (line 16)
7fn main() {
8    let stdout = File::create("info.log").unwrap();
9    let stderr = File::create("err.log").unwrap();
10    let daemon = Daemon::new()
11        .pid_file("example.pid", Some(false))
12        .user(User::try_from("daemon").unwrap())
13        .group(Group::try_from("daemon").unwrap())
14        .umask(0o000)
15        .work_dir(".")
16        .stdout(stdout)
17        .stderr(stderr)
18        .start();
19
20    match daemon {
21        Ok(_) => println!("Daemonized with success"),
22        Err(e) => eprintln!("Error, {}", e),
23    }
24
25    loop {
26        // You wil have to kill this process yourself
27    }
28}
More examples
Hide additional examples
examples/example.rs (line 36)
29fn main() {
30    let stdout = File::create("info.log").unwrap();
31    let stderr = File::create("err.log").unwrap();
32    let daemon = Daemon::new()
33        .pid_file("example.pid", Some(false))
34        .umask(0o000)
35        .work_dir(".")
36        .stdout(stdout)
37        .stderr(stderr)
38        // Hooks are optional
39        .setup_post_fork_parent_hook(post_fork_parent)
40        .setup_post_fork_child_hook(post_fork_child)
41        .setup_post_init_hook(after_init, None)
42        // Start the daemon and calls the hooks
43        .start();
44
45    match daemon {
46        Ok(_) => println!("Daemonized with success"),
47        Err(e) => {
48            eprintln!("Error, {}", e);
49            exit(-1);
50        },
51    }
52
53    for i in 0..=10000 {
54        println!("{}", i);
55    }
56}
Source

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

Examples found in repository?
examples/example-lingering.rs (line 17)
7fn main() {
8    let stdout = File::create("info.log").unwrap();
9    let stderr = File::create("err.log").unwrap();
10    let daemon = Daemon::new()
11        .pid_file("example.pid", Some(false))
12        .user(User::try_from("daemon").unwrap())
13        .group(Group::try_from("daemon").unwrap())
14        .umask(0o000)
15        .work_dir(".")
16        .stdout(stdout)
17        .stderr(stderr)
18        .start();
19
20    match daemon {
21        Ok(_) => println!("Daemonized with success"),
22        Err(e) => eprintln!("Error, {}", e),
23    }
24
25    loop {
26        // You wil have to kill this process yourself
27    }
28}
More examples
Hide additional examples
examples/example.rs (line 37)
29fn main() {
30    let stdout = File::create("info.log").unwrap();
31    let stderr = File::create("err.log").unwrap();
32    let daemon = Daemon::new()
33        .pid_file("example.pid", Some(false))
34        .umask(0o000)
35        .work_dir(".")
36        .stdout(stdout)
37        .stderr(stderr)
38        // Hooks are optional
39        .setup_post_fork_parent_hook(post_fork_parent)
40        .setup_post_fork_child_hook(post_fork_child)
41        .setup_post_init_hook(after_init, None)
42        // Start the daemon and calls the hooks
43        .start();
44
45    match daemon {
46        Ok(_) => println!("Daemonized with success"),
47        Err(e) => {
48            eprintln!("Error, {}", e);
49            exit(-1);
50        },
51    }
52
53    for i in 0..=10000 {
54        println!("{}", i);
55    }
56}
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

Examples found in repository?
examples/example.rs (line 39)
29fn main() {
30    let stdout = File::create("info.log").unwrap();
31    let stderr = File::create("err.log").unwrap();
32    let daemon = Daemon::new()
33        .pid_file("example.pid", Some(false))
34        .umask(0o000)
35        .work_dir(".")
36        .stdout(stdout)
37        .stderr(stderr)
38        // Hooks are optional
39        .setup_post_fork_parent_hook(post_fork_parent)
40        .setup_post_fork_child_hook(post_fork_child)
41        .setup_post_init_hook(after_init, None)
42        // Start the daemon and calls the hooks
43        .start();
44
45    match daemon {
46        Ok(_) => println!("Daemonized with success"),
47        Err(e) => {
48            eprintln!("Error, {}", e);
49            exit(-1);
50        },
51    }
52
53    for i in 0..=10000 {
54        println!("{}", i);
55    }
56}
Source

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

Examples found in repository?
examples/example.rs (line 40)
29fn main() {
30    let stdout = File::create("info.log").unwrap();
31    let stderr = File::create("err.log").unwrap();
32    let daemon = Daemon::new()
33        .pid_file("example.pid", Some(false))
34        .umask(0o000)
35        .work_dir(".")
36        .stdout(stdout)
37        .stderr(stderr)
38        // Hooks are optional
39        .setup_post_fork_parent_hook(post_fork_parent)
40        .setup_post_fork_child_hook(post_fork_child)
41        .setup_post_init_hook(after_init, None)
42        // Start the daemon and calls the hooks
43        .start();
44
45    match daemon {
46        Ok(_) => println!("Daemonized with success"),
47        Err(e) => {
48            eprintln!("Error, {}", e);
49            exit(-1);
50        },
51    }
52
53    for i in 0..=10000 {
54        println!("{}", i);
55    }
56}
Source

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

Examples found in repository?
examples/example.rs (line 41)
29fn main() {
30    let stdout = File::create("info.log").unwrap();
31    let stderr = File::create("err.log").unwrap();
32    let daemon = Daemon::new()
33        .pid_file("example.pid", Some(false))
34        .umask(0o000)
35        .work_dir(".")
36        .stdout(stdout)
37        .stderr(stderr)
38        // Hooks are optional
39        .setup_post_fork_parent_hook(post_fork_parent)
40        .setup_post_fork_child_hook(post_fork_child)
41        .setup_post_init_hook(after_init, None)
42        // Start the daemon and calls the hooks
43        .start();
44
45    match daemon {
46        Ok(_) => println!("Daemonized with success"),
47        Err(e) => {
48            eprintln!("Error, {}", e);
49            exit(-1);
50        },
51    }
52
53    for i in 0..=10000 {
54        println!("{}", i);
55    }
56}
Source

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

Using the parameters set, daemonize the process

Examples found in repository?
examples/example-lingering.rs (line 18)
7fn main() {
8    let stdout = File::create("info.log").unwrap();
9    let stderr = File::create("err.log").unwrap();
10    let daemon = Daemon::new()
11        .pid_file("example.pid", Some(false))
12        .user(User::try_from("daemon").unwrap())
13        .group(Group::try_from("daemon").unwrap())
14        .umask(0o000)
15        .work_dir(".")
16        .stdout(stdout)
17        .stderr(stderr)
18        .start();
19
20    match daemon {
21        Ok(_) => println!("Daemonized with success"),
22        Err(e) => eprintln!("Error, {}", e),
23    }
24
25    loop {
26        // You wil have to kill this process yourself
27    }
28}
More examples
Hide additional examples
examples/example.rs (line 43)
29fn main() {
30    let stdout = File::create("info.log").unwrap();
31    let stderr = File::create("err.log").unwrap();
32    let daemon = Daemon::new()
33        .pid_file("example.pid", Some(false))
34        .umask(0o000)
35        .work_dir(".")
36        .stdout(stdout)
37        .stderr(stderr)
38        // Hooks are optional
39        .setup_post_fork_parent_hook(post_fork_parent)
40        .setup_post_fork_child_hook(post_fork_child)
41        .setup_post_init_hook(after_init, None)
42        // Start the daemon and calls the hooks
43        .start();
44
45    match daemon {
46        Ok(_) => println!("Daemonized with success"),
47        Err(e) => {
48            eprintln!("Error, {}", e);
49            exit(-1);
50        },
51    }
52
53    for i in 0..=10000 {
54        println!("{}", i);
55    }
56}

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

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.