You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
902 B
47 lines
902 B
4 years ago
|
use nix::{
|
||
|
unistd::{
|
||
|
getppid,
|
||
|
setsid,
|
||
|
fork,
|
||
|
ForkResult,
|
||
|
close as fdclose,
|
||
|
chdir
|
||
|
},
|
||
|
sys::signal::{
|
||
|
signal,
|
||
|
Signal,
|
||
|
SigHandler
|
||
|
}
|
||
|
};
|
||
|
use std::process::exit;
|
||
|
|
||
|
|
||
|
pub fn daemonize() {
|
||
|
if getppid().as_raw() != 1 {
|
||
|
setsig(Signal::SIGTTOU, SigHandler::SigIgn);
|
||
|
setsig(Signal::SIGTTIN, SigHandler::SigIgn);
|
||
|
setsig(Signal::SIGTSTP, SigHandler::SigIgn);
|
||
|
}
|
||
|
for fd in 0..=2 {
|
||
|
match fdclose(fd) { _ => () }
|
||
|
}
|
||
|
match chdir("/") { _ => () };
|
||
|
|
||
|
match fork() {
|
||
|
Ok(ForkResult::Parent {..}) => {
|
||
|
exit(0);
|
||
|
},
|
||
|
Ok(ForkResult::Child) => {
|
||
|
match setsid() { _ => () }
|
||
|
}
|
||
|
Err(_) => {
|
||
|
exit(255)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn setsig(sig: Signal, hnd: SigHandler) {
|
||
|
unsafe {
|
||
|
signal(sig, hnd).unwrap();
|
||
|
}
|
||
|
}
|