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
.
Optionally:
- change working directory to provided value;
- 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?
33 pub fn main() {
34 let stdout = File::create("/tmp/daemon.out").unwrap();
35 let stderr = File::create("/tmp/daemon.err").unwrap();
36
37 // Every method except `new` and `start` is optional. See `Daemonize` documentation for
38 // default behaviour.
39 let mut daemonize = Daemonize::new();
40
41 daemonize = daemonize
42 .pid_file("/tmp/test.pid")
43 .chown_pid_file_user("nobody")
44 .chown_pid_file_group("daemon")
45 .working_directory("/tmp");
46
47 // User and group IDs can be either strings or integers.
48 daemonize = daemonize.user("nobody").group("daemon").group(2);
49
50 // Set umask. `0o027` by default.
51 daemonize = daemonize.umask(0o777);
52
53 // Redirect standard output and standard error.
54 daemonize = daemonize.stdout(stdout).stderr(stderr);
55
56 // Run a final privileged action.
57 let daemonize = daemonize.privileged_action(|| "Executed before drop privileges");
58
59 // Start the daemon.
60 match unsafe { daemonize.start() } {
61 Ok(_) => println!("Success, daemonized"),
62 Err(e) => eprintln!("Error, {}", e),
63 }
64 }
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?
33 pub fn main() {
34 let stdout = File::create("/tmp/daemon.out").unwrap();
35 let stderr = File::create("/tmp/daemon.err").unwrap();
36
37 // Every method except `new` and `start` is optional. See `Daemonize` documentation for
38 // default behaviour.
39 let mut daemonize = Daemonize::new();
40
41 daemonize = daemonize
42 .pid_file("/tmp/test.pid")
43 .chown_pid_file_user("nobody")
44 .chown_pid_file_group("daemon")
45 .working_directory("/tmp");
46
47 // User and group IDs can be either strings or integers.
48 daemonize = daemonize.user("nobody").group("daemon").group(2);
49
50 // Set umask. `0o027` by default.
51 daemonize = daemonize.umask(0o777);
52
53 // Redirect standard output and standard error.
54 daemonize = daemonize.stdout(stdout).stderr(stderr);
55
56 // Run a final privileged action.
57 let daemonize = daemonize.privileged_action(|| "Executed before drop privileges");
58
59 // Start the daemon.
60 match unsafe { daemonize.start() } {
61 Ok(_) => println!("Success, daemonized"),
62 Err(e) => eprintln!("Error, {}", e),
63 }
64 }
Sourcepub fn chown_pid_file_user<U: Into<User>>(self, user: U) -> Self
pub fn chown_pid_file_user<U: Into<User>>(self, user: U) -> Self
Changes the pid-file ownership to user
.
Examples found in repository?
33 pub fn main() {
34 let stdout = File::create("/tmp/daemon.out").unwrap();
35 let stderr = File::create("/tmp/daemon.err").unwrap();
36
37 // Every method except `new` and `start` is optional. See `Daemonize` documentation for
38 // default behaviour.
39 let mut daemonize = Daemonize::new();
40
41 daemonize = daemonize
42 .pid_file("/tmp/test.pid")
43 .chown_pid_file_user("nobody")
44 .chown_pid_file_group("daemon")
45 .working_directory("/tmp");
46
47 // User and group IDs can be either strings or integers.
48 daemonize = daemonize.user("nobody").group("daemon").group(2);
49
50 // Set umask. `0o027` by default.
51 daemonize = daemonize.umask(0o777);
52
53 // Redirect standard output and standard error.
54 daemonize = daemonize.stdout(stdout).stderr(stderr);
55
56 // Run a final privileged action.
57 let daemonize = daemonize.privileged_action(|| "Executed before drop privileges");
58
59 // Start the daemon.
60 match unsafe { daemonize.start() } {
61 Ok(_) => println!("Success, daemonized"),
62 Err(e) => eprintln!("Error, {}", e),
63 }
64 }
Sourcepub fn chown_pid_file_group<G: Into<Group>>(self, group: G) -> Self
pub fn chown_pid_file_group<G: Into<Group>>(self, group: G) -> Self
Changes the pid-file ownership to group
.
Examples found in repository?
33 pub fn main() {
34 let stdout = File::create("/tmp/daemon.out").unwrap();
35 let stderr = File::create("/tmp/daemon.err").unwrap();
36
37 // Every method except `new` and `start` is optional. See `Daemonize` documentation for
38 // default behaviour.
39 let mut daemonize = Daemonize::new();
40
41 daemonize = daemonize
42 .pid_file("/tmp/test.pid")
43 .chown_pid_file_user("nobody")
44 .chown_pid_file_group("daemon")
45 .working_directory("/tmp");
46
47 // User and group IDs can be either strings or integers.
48 daemonize = daemonize.user("nobody").group("daemon").group(2);
49
50 // Set umask. `0o027` by default.
51 daemonize = daemonize.umask(0o777);
52
53 // Redirect standard output and standard error.
54 daemonize = daemonize.stdout(stdout).stderr(stderr);
55
56 // Run a final privileged action.
57 let daemonize = daemonize.privileged_action(|| "Executed before drop privileges");
58
59 // Start the daemon.
60 match unsafe { daemonize.start() } {
61 Ok(_) => println!("Success, daemonized"),
62 Err(e) => eprintln!("Error, {}", e),
63 }
64 }
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
.
Examples found in repository?
33 pub fn main() {
34 let stdout = File::create("/tmp/daemon.out").unwrap();
35 let stderr = File::create("/tmp/daemon.err").unwrap();
36
37 // Every method except `new` and `start` is optional. See `Daemonize` documentation for
38 // default behaviour.
39 let mut daemonize = Daemonize::new();
40
41 daemonize = daemonize
42 .pid_file("/tmp/test.pid")
43 .chown_pid_file_user("nobody")
44 .chown_pid_file_group("daemon")
45 .working_directory("/tmp");
46
47 // User and group IDs can be either strings or integers.
48 daemonize = daemonize.user("nobody").group("daemon").group(2);
49
50 // Set umask. `0o027` by default.
51 daemonize = daemonize.umask(0o777);
52
53 // Redirect standard output and standard error.
54 daemonize = daemonize.stdout(stdout).stderr(stderr);
55
56 // Run a final privileged action.
57 let daemonize = daemonize.privileged_action(|| "Executed before drop privileges");
58
59 // Start the daemon.
60 match unsafe { daemonize.start() } {
61 Ok(_) => println!("Success, daemonized"),
62 Err(e) => eprintln!("Error, {}", e),
63 }
64 }
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?
33 pub fn main() {
34 let stdout = File::create("/tmp/daemon.out").unwrap();
35 let stderr = File::create("/tmp/daemon.err").unwrap();
36
37 // Every method except `new` and `start` is optional. See `Daemonize` documentation for
38 // default behaviour.
39 let mut daemonize = Daemonize::new();
40
41 daemonize = daemonize
42 .pid_file("/tmp/test.pid")
43 .chown_pid_file_user("nobody")
44 .chown_pid_file_group("daemon")
45 .working_directory("/tmp");
46
47 // User and group IDs can be either strings or integers.
48 daemonize = daemonize.user("nobody").group("daemon").group(2);
49
50 // Set umask. `0o027` by default.
51 daemonize = daemonize.umask(0o777);
52
53 // Redirect standard output and standard error.
54 daemonize = daemonize.stdout(stdout).stderr(stderr);
55
56 // Run a final privileged action.
57 let daemonize = daemonize.privileged_action(|| "Executed before drop privileges");
58
59 // Start the daemon.
60 match unsafe { daemonize.start() } {
61 Ok(_) => println!("Success, daemonized"),
62 Err(e) => eprintln!("Error, {}", e),
63 }
64 }
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?
33 pub fn main() {
34 let stdout = File::create("/tmp/daemon.out").unwrap();
35 let stderr = File::create("/tmp/daemon.err").unwrap();
36
37 // Every method except `new` and `start` is optional. See `Daemonize` documentation for
38 // default behaviour.
39 let mut daemonize = Daemonize::new();
40
41 daemonize = daemonize
42 .pid_file("/tmp/test.pid")
43 .chown_pid_file_user("nobody")
44 .chown_pid_file_group("daemon")
45 .working_directory("/tmp");
46
47 // User and group IDs can be either strings or integers.
48 daemonize = daemonize.user("nobody").group("daemon").group(2);
49
50 // Set umask. `0o027` by default.
51 daemonize = daemonize.umask(0o777);
52
53 // Redirect standard output and standard error.
54 daemonize = daemonize.stdout(stdout).stderr(stderr);
55
56 // Run a final privileged action.
57 let daemonize = daemonize.privileged_action(|| "Executed before drop privileges");
58
59 // Start the daemon.
60 match unsafe { daemonize.start() } {
61 Ok(_) => println!("Success, daemonized"),
62 Err(e) => eprintln!("Error, {}", e),
63 }
64 }
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?
33 pub fn main() {
34 let stdout = File::create("/tmp/daemon.out").unwrap();
35 let stderr = File::create("/tmp/daemon.err").unwrap();
36
37 // Every method except `new` and `start` is optional. See `Daemonize` documentation for
38 // default behaviour.
39 let mut daemonize = Daemonize::new();
40
41 daemonize = daemonize
42 .pid_file("/tmp/test.pid")
43 .chown_pid_file_user("nobody")
44 .chown_pid_file_group("daemon")
45 .working_directory("/tmp");
46
47 // User and group IDs can be either strings or integers.
48 daemonize = daemonize.user("nobody").group("daemon").group(2);
49
50 // Set umask. `0o027` by default.
51 daemonize = daemonize.umask(0o777);
52
53 // Redirect standard output and standard error.
54 daemonize = daemonize.stdout(stdout).stderr(stderr);
55
56 // Run a final privileged action.
57 let daemonize = daemonize.privileged_action(|| "Executed before drop privileges");
58
59 // Start the daemon.
60 match unsafe { daemonize.start() } {
61 Ok(_) => println!("Success, daemonized"),
62 Err(e) => eprintln!("Error, {}", e),
63 }
64 }
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?
33 pub fn main() {
34 let stdout = File::create("/tmp/daemon.out").unwrap();
35 let stderr = File::create("/tmp/daemon.err").unwrap();
36
37 // Every method except `new` and `start` is optional. See `Daemonize` documentation for
38 // default behaviour.
39 let mut daemonize = Daemonize::new();
40
41 daemonize = daemonize
42 .pid_file("/tmp/test.pid")
43 .chown_pid_file_user("nobody")
44 .chown_pid_file_group("daemon")
45 .working_directory("/tmp");
46
47 // User and group IDs can be either strings or integers.
48 daemonize = daemonize.user("nobody").group("daemon").group(2);
49
50 // Set umask. `0o027` by default.
51 daemonize = daemonize.umask(0o777);
52
53 // Redirect standard output and standard error.
54 daemonize = daemonize.stdout(stdout).stderr(stderr);
55
56 // Run a final privileged action.
57 let daemonize = daemonize.privileged_action(|| "Executed before drop privileges");
58
59 // Start the daemon.
60 match unsafe { daemonize.start() } {
61 Ok(_) => println!("Success, daemonized"),
62 Err(e) => eprintln!("Error, {}", e),
63 }
64 }
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?
33 pub fn main() {
34 let stdout = File::create("/tmp/daemon.out").unwrap();
35 let stderr = File::create("/tmp/daemon.err").unwrap();
36
37 // Every method except `new` and `start` is optional. See `Daemonize` documentation for
38 // default behaviour.
39 let mut daemonize = Daemonize::new();
40
41 daemonize = daemonize
42 .pid_file("/tmp/test.pid")
43 .chown_pid_file_user("nobody")
44 .chown_pid_file_group("daemon")
45 .working_directory("/tmp");
46
47 // User and group IDs can be either strings or integers.
48 daemonize = daemonize.user("nobody").group("daemon").group(2);
49
50 // Set umask. `0o027` by default.
51 daemonize = daemonize.umask(0o777);
52
53 // Redirect standard output and standard error.
54 daemonize = daemonize.stdout(stdout).stderr(stderr);
55
56 // Run a final privileged action.
57 let daemonize = daemonize.privileged_action(|| "Executed before drop privileges");
58
59 // Start the daemon.
60 match unsafe { daemonize.start() } {
61 Ok(_) => println!("Success, daemonized"),
62 Err(e) => eprintln!("Error, {}", e),
63 }
64 }
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?
33 pub fn main() {
34 let stdout = File::create("/tmp/daemon.out").unwrap();
35 let stderr = File::create("/tmp/daemon.err").unwrap();
36
37 // Every method except `new` and `start` is optional. See `Daemonize` documentation for
38 // default behaviour.
39 let mut daemonize = Daemonize::new();
40
41 daemonize = daemonize
42 .pid_file("/tmp/test.pid")
43 .chown_pid_file_user("nobody")
44 .chown_pid_file_group("daemon")
45 .working_directory("/tmp");
46
47 // User and group IDs can be either strings or integers.
48 daemonize = daemonize.user("nobody").group("daemon").group(2);
49
50 // Set umask. `0o027` by default.
51 daemonize = daemonize.umask(0o777);
52
53 // Redirect standard output and standard error.
54 daemonize = daemonize.stdout(stdout).stderr(stderr);
55
56 // Run a final privileged action.
57 let daemonize = daemonize.privileged_action(|| "Executed before drop privileges");
58
59 // Start the daemon.
60 match unsafe { daemonize.start() } {
61 Ok(_) => println!("Success, daemonized"),
62 Err(e) => eprintln!("Error, {}", e),
63 }
64 }
Sourcepub unsafe fn start(self) -> Result<T, Error>
pub unsafe fn start(self) -> Result<T, Error>
Start daemonization process. Terminate parent after first fork. Returns privileged action result to the child.
§Safety
This is not safe to call inside a multi-threaded process. Familiarize yourself with the documentation for fork(2).
Examples found in repository?
33 pub fn main() {
34 let stdout = File::create("/tmp/daemon.out").unwrap();
35 let stderr = File::create("/tmp/daemon.err").unwrap();
36
37 // Every method except `new` and `start` is optional. See `Daemonize` documentation for
38 // default behaviour.
39 let mut daemonize = Daemonize::new();
40
41 daemonize = daemonize
42 .pid_file("/tmp/test.pid")
43 .chown_pid_file_user("nobody")
44 .chown_pid_file_group("daemon")
45 .working_directory("/tmp");
46
47 // User and group IDs can be either strings or integers.
48 daemonize = daemonize.user("nobody").group("daemon").group(2);
49
50 // Set umask. `0o027` by default.
51 daemonize = daemonize.umask(0o777);
52
53 // Redirect standard output and standard error.
54 daemonize = daemonize.stdout(stdout).stderr(stderr);
55
56 // Run a final privileged action.
57 let daemonize = daemonize.privileged_action(|| "Executed before drop privileges");
58
59 // Start the daemon.
60 match unsafe { daemonize.start() } {
61 Ok(_) => println!("Success, daemonized"),
62 Err(e) => eprintln!("Error, {}", e),
63 }
64 }