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>
impl<'a> Daemon<'a>
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
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
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}
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
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?
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
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}
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 /
Examples found in repository?
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
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}
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
Examples found in repository?
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}
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, you mut provide a group if you provide an user
Examples found in repository?
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}
pub fn group_copy_user(self) -> Result<Self>
Sourcepub fn umask(self, mask: u16) -> Self
pub fn umask(self, mask: u16) -> Self
Examples found in repository?
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
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}
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
Examples found in repository?
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
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}
Sourcepub fn stderr<T: Into<Stdio>>(self, stdio: T) -> Self
pub fn stderr<T: Into<Stdio>>(self, stdio: T) -> Self
Examples found in repository?
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
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}
pub fn name(self, name: &OsStr) -> Self
pub fn setup_pre_fork_hook(self, pre_fork_hook: fn(pid: i32)) -> Self
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
Examples found in repository?
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}
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
Examples found in repository?
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}
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
Examples found in repository?
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}
Sourcepub fn start(self) -> Result<()>
pub fn start(self) -> Result<()>
Using the parameters set, daemonize the process
Examples found in repository?
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
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}