pub struct Daemonize<T> { /* private fields */ }
Expand description
Daemonization options.
Fork the process in the background, disassociate from its process group and the control terminal.
Change umask value to 0o027
, redirect all standard streams to /dev/null
. Change working
directory to /
or provided value.
Optionally:
- maintain and lock the pid-file;
- drop user privileges;
- drop group privileges;
- change root directory;
- change the pid-file ownership to provided user (and/or) group;
- execute any provided action just before dropping privileges.
Implementations§
Source§impl Daemonize<()>
impl Daemonize<()>
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
7fn main() {
8 let stdout = File::create("/tmp/daemon.out").unwrap();
9 let stderr = File::create("/tmp/daemon.err").unwrap();
10
11 let daemonize = Daemonize::new()
12 .pid_file("/tmp/test.pid") // Every method except `new` and `start`
13 .chown_pid_file(true) // is optional, see `Daemonize` documentation
14 .working_directory("/tmp") // for default behaviour.
15 .user("nobody")
16 .group("daemon") // Group name
17 .group(2) // or group id.
18 .umask(0o777) // Set umask, `0o027` by default.
19 .stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
20 .stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
21 .privileged_action(|| "Executed before drop privileges");
22
23 match daemonize.start() {
24 Ok(_) => println!("Success, daemonized"),
25 Err(e) => eprintln!("Error, {}", e),
26 }
27}
Source§impl<T> Daemonize<T>
impl<T> Daemonize<T>
Sourcepub fn pid_file<F: AsRef<Path>>(self, path: F) -> Self
pub fn pid_file<F: AsRef<Path>>(self, path: F) -> Self
Create pid-file at path
, lock it exclusive and write daemon pid.
Examples found in repository?
7fn main() {
8 let stdout = File::create("/tmp/daemon.out").unwrap();
9 let stderr = File::create("/tmp/daemon.err").unwrap();
10
11 let daemonize = Daemonize::new()
12 .pid_file("/tmp/test.pid") // Every method except `new` and `start`
13 .chown_pid_file(true) // is optional, see `Daemonize` documentation
14 .working_directory("/tmp") // for default behaviour.
15 .user("nobody")
16 .group("daemon") // Group name
17 .group(2) // or group id.
18 .umask(0o777) // Set umask, `0o027` by default.
19 .stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
20 .stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
21 .privileged_action(|| "Executed before drop privileges");
22
23 match daemonize.start() {
24 Ok(_) => println!("Success, daemonized"),
25 Err(e) => eprintln!("Error, {}", e),
26 }
27}
Sourcepub fn chown_pid_file(self, chown: bool) -> Self
pub fn chown_pid_file(self, chown: bool) -> Self
If chown
is true, daemonize will change the pid-file ownership, if user or group are provided
Examples found in repository?
7fn main() {
8 let stdout = File::create("/tmp/daemon.out").unwrap();
9 let stderr = File::create("/tmp/daemon.err").unwrap();
10
11 let daemonize = Daemonize::new()
12 .pid_file("/tmp/test.pid") // Every method except `new` and `start`
13 .chown_pid_file(true) // is optional, see `Daemonize` documentation
14 .working_directory("/tmp") // for default behaviour.
15 .user("nobody")
16 .group("daemon") // Group name
17 .group(2) // or group id.
18 .umask(0o777) // Set umask, `0o027` by default.
19 .stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
20 .stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
21 .privileged_action(|| "Executed before drop privileges");
22
23 match daemonize.start() {
24 Ok(_) => println!("Success, daemonized"),
25 Err(e) => eprintln!("Error, {}", e),
26 }
27}
Sourcepub fn working_directory<F: AsRef<Path>>(self, path: F) -> Self
pub fn working_directory<F: AsRef<Path>>(self, path: F) -> Self
Change working directory to path
or /
by default.
Examples found in repository?
7fn main() {
8 let stdout = File::create("/tmp/daemon.out").unwrap();
9 let stderr = File::create("/tmp/daemon.err").unwrap();
10
11 let daemonize = Daemonize::new()
12 .pid_file("/tmp/test.pid") // Every method except `new` and `start`
13 .chown_pid_file(true) // is optional, see `Daemonize` documentation
14 .working_directory("/tmp") // for default behaviour.
15 .user("nobody")
16 .group("daemon") // Group name
17 .group(2) // or group id.
18 .umask(0o777) // Set umask, `0o027` by default.
19 .stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
20 .stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
21 .privileged_action(|| "Executed before drop privileges");
22
23 match daemonize.start() {
24 Ok(_) => println!("Success, daemonized"),
25 Err(e) => eprintln!("Error, {}", e),
26 }
27}
Sourcepub fn user<U: Into<User>>(self, user: U) -> Self
pub fn user<U: Into<User>>(self, user: U) -> Self
Drop privileges to user
.
Examples found in repository?
7fn main() {
8 let stdout = File::create("/tmp/daemon.out").unwrap();
9 let stderr = File::create("/tmp/daemon.err").unwrap();
10
11 let daemonize = Daemonize::new()
12 .pid_file("/tmp/test.pid") // Every method except `new` and `start`
13 .chown_pid_file(true) // is optional, see `Daemonize` documentation
14 .working_directory("/tmp") // for default behaviour.
15 .user("nobody")
16 .group("daemon") // Group name
17 .group(2) // or group id.
18 .umask(0o777) // Set umask, `0o027` by default.
19 .stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
20 .stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
21 .privileged_action(|| "Executed before drop privileges");
22
23 match daemonize.start() {
24 Ok(_) => println!("Success, daemonized"),
25 Err(e) => eprintln!("Error, {}", e),
26 }
27}
Sourcepub fn group<G: Into<Group>>(self, group: G) -> Self
pub fn group<G: Into<Group>>(self, group: G) -> Self
Drop privileges to group
.
Examples found in repository?
7fn main() {
8 let stdout = File::create("/tmp/daemon.out").unwrap();
9 let stderr = File::create("/tmp/daemon.err").unwrap();
10
11 let daemonize = Daemonize::new()
12 .pid_file("/tmp/test.pid") // Every method except `new` and `start`
13 .chown_pid_file(true) // is optional, see `Daemonize` documentation
14 .working_directory("/tmp") // for default behaviour.
15 .user("nobody")
16 .group("daemon") // Group name
17 .group(2) // or group id.
18 .umask(0o777) // Set umask, `0o027` by default.
19 .stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
20 .stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
21 .privileged_action(|| "Executed before drop privileges");
22
23 match daemonize.start() {
24 Ok(_) => println!("Success, daemonized"),
25 Err(e) => eprintln!("Error, {}", e),
26 }
27}
Sourcepub fn umask<M: Into<Mask>>(self, mask: M) -> Self
pub fn umask<M: Into<Mask>>(self, mask: M) -> Self
Change umask to mask
or 0o027
by default.
Examples found in repository?
7fn main() {
8 let stdout = File::create("/tmp/daemon.out").unwrap();
9 let stderr = File::create("/tmp/daemon.err").unwrap();
10
11 let daemonize = Daemonize::new()
12 .pid_file("/tmp/test.pid") // Every method except `new` and `start`
13 .chown_pid_file(true) // is optional, see `Daemonize` documentation
14 .working_directory("/tmp") // for default behaviour.
15 .user("nobody")
16 .group("daemon") // Group name
17 .group(2) // or group id.
18 .umask(0o777) // Set umask, `0o027` by default.
19 .stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
20 .stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
21 .privileged_action(|| "Executed before drop privileges");
22
23 match daemonize.start() {
24 Ok(_) => println!("Success, daemonized"),
25 Err(e) => eprintln!("Error, {}", e),
26 }
27}
Sourcepub fn privileged_action<N, F: FnOnce() -> N + 'static>(
self,
action: F,
) -> Daemonize<N>
pub fn privileged_action<N, F: FnOnce() -> N + 'static>( self, action: F, ) -> Daemonize<N>
Execute action
just before dropping privileges. Most common use case is to open
listening socket. Result of action
execution will be returned by start
method.
Examples found in repository?
7fn main() {
8 let stdout = File::create("/tmp/daemon.out").unwrap();
9 let stderr = File::create("/tmp/daemon.err").unwrap();
10
11 let daemonize = Daemonize::new()
12 .pid_file("/tmp/test.pid") // Every method except `new` and `start`
13 .chown_pid_file(true) // is optional, see `Daemonize` documentation
14 .working_directory("/tmp") // for default behaviour.
15 .user("nobody")
16 .group("daemon") // Group name
17 .group(2) // or group id.
18 .umask(0o777) // Set umask, `0o027` by default.
19 .stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
20 .stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
21 .privileged_action(|| "Executed before drop privileges");
22
23 match daemonize.start() {
24 Ok(_) => println!("Success, daemonized"),
25 Err(e) => eprintln!("Error, {}", e),
26 }
27}
Sourcepub fn stdout<S: Into<Stdio>>(self, stdio: S) -> Self
pub fn stdout<S: Into<Stdio>>(self, stdio: S) -> Self
Configuration for the child process’s standard output stream.
Examples found in repository?
7fn main() {
8 let stdout = File::create("/tmp/daemon.out").unwrap();
9 let stderr = File::create("/tmp/daemon.err").unwrap();
10
11 let daemonize = Daemonize::new()
12 .pid_file("/tmp/test.pid") // Every method except `new` and `start`
13 .chown_pid_file(true) // is optional, see `Daemonize` documentation
14 .working_directory("/tmp") // for default behaviour.
15 .user("nobody")
16 .group("daemon") // Group name
17 .group(2) // or group id.
18 .umask(0o777) // Set umask, `0o027` by default.
19 .stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
20 .stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
21 .privileged_action(|| "Executed before drop privileges");
22
23 match daemonize.start() {
24 Ok(_) => println!("Success, daemonized"),
25 Err(e) => eprintln!("Error, {}", e),
26 }
27}
Sourcepub fn stderr<S: Into<Stdio>>(self, stdio: S) -> Self
pub fn stderr<S: Into<Stdio>>(self, stdio: S) -> Self
Configuration for the child process’s standard error stream.
Examples found in repository?
7fn main() {
8 let stdout = File::create("/tmp/daemon.out").unwrap();
9 let stderr = File::create("/tmp/daemon.err").unwrap();
10
11 let daemonize = Daemonize::new()
12 .pid_file("/tmp/test.pid") // Every method except `new` and `start`
13 .chown_pid_file(true) // is optional, see `Daemonize` documentation
14 .working_directory("/tmp") // for default behaviour.
15 .user("nobody")
16 .group("daemon") // Group name
17 .group(2) // or group id.
18 .umask(0o777) // Set umask, `0o027` by default.
19 .stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
20 .stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
21 .privileged_action(|| "Executed before drop privileges");
22
23 match daemonize.start() {
24 Ok(_) => println!("Success, daemonized"),
25 Err(e) => eprintln!("Error, {}", e),
26 }
27}
Sourcepub fn start(self) -> Result<T, Error>
pub fn start(self) -> Result<T, Error>
Start daemonization process, terminate parent after first fork, returns privileged action result to the child.
Examples found in repository?
7fn main() {
8 let stdout = File::create("/tmp/daemon.out").unwrap();
9 let stderr = File::create("/tmp/daemon.err").unwrap();
10
11 let daemonize = Daemonize::new()
12 .pid_file("/tmp/test.pid") // Every method except `new` and `start`
13 .chown_pid_file(true) // is optional, see `Daemonize` documentation
14 .working_directory("/tmp") // for default behaviour.
15 .user("nobody")
16 .group("daemon") // Group name
17 .group(2) // or group id.
18 .umask(0o777) // Set umask, `0o027` by default.
19 .stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
20 .stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
21 .privileged_action(|| "Executed before drop privileges");
22
23 match daemonize.start() {
24 Ok(_) => println!("Success, daemonized"),
25 Err(e) => eprintln!("Error, {}", e),
26 }
27}