From 569b9da581deabc8d636c947195bd1cc43077ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hozda?= Date: Thu, 26 Sep 2019 13:53:06 +0200 Subject: [PATCH] normalize formatting and add rustfmt config --- rustfmt.toml | 16 ++++++++++ src/config.rs | 4 +-- src/main.rs | 82 ++++++++++++++++++++++++--------------------------- 3 files changed, 57 insertions(+), 45 deletions(-) create mode 100644 rustfmt.toml diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..8c4b70a --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,16 @@ +unstable_features = true +hard_tabs = true +inline_attribute_width = 30 +use_small_heuristics = "Max" +max_width = 95 +reorder_imports = false +reorder_modules = false +reorder_impl_items = true +report_todo = "Always" +report_fixme = "Always" +struct_field_align_threshold = 25 +use_field_init_shorthand = true +use_try_shorthand = true +match_arm_blocks = false +overflow_delimited_expr = true +edition = "2018" diff --git a/src/config.rs b/src/config.rs index cb450c0..507627f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,8 +2,8 @@ pub static TIMEOUT: u32 = 30; /// init script/command/program to be ran once initrs initializes -pub static INIT_CMD: &'static [&'static str] = &["/bin/rc.init"]; +pub static INIT_CMD: &'static [&'static str] = &["/bin/rc.init"]; /// command to reboot -pub static REBOOT_CMD: &'static [&'static str] = &["/bin/rc.shutdown", "reboot"]; +pub static REBOOT_CMD: &'static [&'static str] = &["/bin/rc.shutdown", "reboot"]; /// command to shutdown pub static POWEROFF_CMD: &'static [&'static str] = &["/bin/rc.shutdown", "poweroff"]; diff --git a/src/main.rs b/src/main.rs index bf16330..7cd201d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,51 +16,49 @@ fn sig_poweroff() { .output() .is_err() { - println!("[RS] poweroff failed"); + println!("[RS] poweroff failed"); } } fn sig_reap() { loop { - match waitpid(Pid::from_raw(-1), Some(WaitPidFlag::WNOHANG)) { - Err(_) => break, - Ok(status) => match status { - WaitStatus::Exited(pid, ..) | - WaitStatus::Signaled(pid, ..) | - WaitStatus::Stopped(pid, ..) | - WaitStatus::Continued(pid) => if pid.as_raw() <= 0 { - break - }, - _ => (), - } - } + match waitpid(Pid::from_raw(-1), Some(WaitPidFlag::WNOHANG)) { + Err(_) => break, + Ok(status) => match status { + WaitStatus::Exited(pid, ..) + | WaitStatus::Signaled(pid, ..) + | WaitStatus::Stopped(pid, ..) + | WaitStatus::Continued(pid) => + if pid.as_raw() <= 0 { + break; + }, + _ => (), + }, + } } alarm::set(config::TIMEOUT); } fn sig_reboot() { - if Command::new(config::REBOOT_CMD[0]) - .args(config::REBOOT_CMD.iter().skip(1)) - .output() - .is_err() - { - println!("[RS] reboot failed"); - } + if Command::new(config::REBOOT_CMD[0]) + .args(config::REBOOT_CMD.iter().skip(1)) + .output() + .is_err() + { + println!("[RS] reboot failed"); + } } fn main() { - if getpid().as_raw() != 1 { return } + if getpid().as_raw() != 1 { + return; + } - let signals = Signals::new(&[ - SIGUSR1, - SIGCHLD, - SIGALRM, - SIGINT, - ]).expect("[RS] failed to handle signals"); + let signals = Signals::new(&[SIGUSR1, SIGCHLD, SIGALRM, SIGINT]) + .expect("[RS] failed to handle signals"); - set_current_dir("/") - .expect("[RS] failed to chdir"); + set_current_dir("/").expect("[RS] failed to chdir"); let init = Command::new(config::INIT_CMD[0]) .args(config::INIT_CMD.iter().skip(1)) @@ -69,22 +67,20 @@ fn main() { .expect("[RS] failed to spawn init command"); thread::spawn(|| { - let c = init.wait_with_output() - .expect("[RS] failed to wait on init command"); + let c = init.wait_with_output().expect("[RS] failed to wait on init command"); - if !c.status.success() { - eprintln!("[RS] exited with a non-zero return code, see stderr below:"); - eprintln!("{}", String::from_utf8_lossy(&c.stdout)); - } - }); + if !c.status.success() { + eprintln!("[RS] exited with a non-zero return code, see stderr below:"); + eprintln!("{}", String::from_utf8_lossy(&c.stdout)); + } + }); for signal in signals.forever() { - match signal { - SIGUSR1 => sig_poweroff(), - SIGCHLD | - SIGALRM => sig_reap(), - SIGINT => sig_reboot(), - _ => unreachable!(), - } + match signal { + SIGUSR1 => sig_poweroff(), + SIGCHLD | SIGALRM => sig_reap(), + SIGINT => sig_reboot(), + _ => unreachable!(), + } } }