|
|
@ -16,51 +16,49 @@ fn sig_poweroff() {
|
|
|
|
.output()
|
|
|
|
.output()
|
|
|
|
.is_err()
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
println!("[RS] poweroff failed");
|
|
|
|
println!("[RS] poweroff failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn sig_reap() {
|
|
|
|
fn sig_reap() {
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
match waitpid(Pid::from_raw(-1), Some(WaitPidFlag::WNOHANG)) {
|
|
|
|
match waitpid(Pid::from_raw(-1), Some(WaitPidFlag::WNOHANG)) {
|
|
|
|
Err(_) => break,
|
|
|
|
Err(_) => break,
|
|
|
|
Ok(status) => match status {
|
|
|
|
Ok(status) => match status {
|
|
|
|
WaitStatus::Exited(pid, ..) |
|
|
|
|
WaitStatus::Exited(pid, ..)
|
|
|
|
WaitStatus::Signaled(pid, ..) |
|
|
|
|
| WaitStatus::Signaled(pid, ..)
|
|
|
|
WaitStatus::Stopped(pid, ..) |
|
|
|
|
| WaitStatus::Stopped(pid, ..)
|
|
|
|
WaitStatus::Continued(pid) => if pid.as_raw() <= 0 {
|
|
|
|
| WaitStatus::Continued(pid) =>
|
|
|
|
break
|
|
|
|
if pid.as_raw() <= 0 {
|
|
|
|
},
|
|
|
|
break;
|
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
alarm::set(config::TIMEOUT);
|
|
|
|
alarm::set(config::TIMEOUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn sig_reboot() {
|
|
|
|
fn sig_reboot() {
|
|
|
|
if Command::new(config::REBOOT_CMD[0])
|
|
|
|
if Command::new(config::REBOOT_CMD[0])
|
|
|
|
.args(config::REBOOT_CMD.iter().skip(1))
|
|
|
|
.args(config::REBOOT_CMD.iter().skip(1))
|
|
|
|
.output()
|
|
|
|
.output()
|
|
|
|
.is_err()
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
println!("[RS] reboot failed");
|
|
|
|
println!("[RS] reboot failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
fn main() {
|
|
|
|
if getpid().as_raw() != 1 { return }
|
|
|
|
if getpid().as_raw() != 1 {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let signals = Signals::new(&[
|
|
|
|
let signals = Signals::new(&[SIGUSR1, SIGCHLD, SIGALRM, SIGINT])
|
|
|
|
SIGUSR1,
|
|
|
|
.expect("[RS] failed to handle signals");
|
|
|
|
SIGCHLD,
|
|
|
|
|
|
|
|
SIGALRM,
|
|
|
|
|
|
|
|
SIGINT,
|
|
|
|
|
|
|
|
]).expect("[RS] failed to handle signals");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set_current_dir("/")
|
|
|
|
set_current_dir("/").expect("[RS] failed to chdir");
|
|
|
|
.expect("[RS] failed to chdir");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let init = Command::new(config::INIT_CMD[0])
|
|
|
|
let init = Command::new(config::INIT_CMD[0])
|
|
|
|
.args(config::INIT_CMD.iter().skip(1))
|
|
|
|
.args(config::INIT_CMD.iter().skip(1))
|
|
|
@ -69,22 +67,20 @@ fn main() {
|
|
|
|
.expect("[RS] failed to spawn init command");
|
|
|
|
.expect("[RS] failed to spawn init command");
|
|
|
|
|
|
|
|
|
|
|
|
thread::spawn(|| {
|
|
|
|
thread::spawn(|| {
|
|
|
|
let c = init.wait_with_output()
|
|
|
|
let c = init.wait_with_output().expect("[RS] failed to wait on init command");
|
|
|
|
.expect("[RS] failed to wait on init command");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !c.status.success() {
|
|
|
|
if !c.status.success() {
|
|
|
|
eprintln!("[RS] exited with a non-zero return code, see stderr below:");
|
|
|
|
eprintln!("[RS] exited with a non-zero return code, see stderr below:");
|
|
|
|
eprintln!("{}", String::from_utf8_lossy(&c.stdout));
|
|
|
|
eprintln!("{}", String::from_utf8_lossy(&c.stdout));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
for signal in signals.forever() {
|
|
|
|
for signal in signals.forever() {
|
|
|
|
match signal {
|
|
|
|
match signal {
|
|
|
|
SIGUSR1 => sig_poweroff(),
|
|
|
|
SIGUSR1 => sig_poweroff(),
|
|
|
|
SIGCHLD |
|
|
|
|
SIGCHLD | SIGALRM => sig_reap(),
|
|
|
|
SIGALRM => sig_reap(),
|
|
|
|
SIGINT => sig_reboot(),
|
|
|
|
SIGINT => sig_reboot(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|