From 4e575e2ef5dde7348b64e59244bebdd2b2b75284 Mon Sep 17 00:00:00 2001 From: Shinwell Hu Date: Tue, 27 Jul 2021 10:38:16 +0000 Subject: [PATCH] execute command or standalone --- src/main/Cargo.toml | 5 +++++ src/main/src/main.rs | 46 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/main/Cargo.toml b/src/main/Cargo.toml index 8fe34953..18d0ca50 100644 --- a/src/main/Cargo.toml +++ b/src/main/Cargo.toml @@ -10,6 +10,11 @@ license = "MulanPSL-2.0" [dependencies] signal-hook = { version = "0.3.7", features = ["extended-siginfo"] } +signal-hook-registry = "" +#socket2 = { version = "0.3.8"} +#async-std = { version = "", features = ["unstable"] } +#async-std = { version = "1.9" } +#async-attributes = "" utils = { path = "../../libs/utils" } libc = { version = "^0.2" } diff --git a/src/main/src/main.rs b/src/main/src/main.rs index 7dfdc111..58645239 100644 --- a/src/main/src/main.rs +++ b/src/main/src/main.rs @@ -8,6 +8,29 @@ use signal_hook::consts::signal::SIGCHLD; use signal_hook::iterator::SignalsInfo; use signal_hook::iterator::exfiltrator::WithOrigin; + +#[derive(Debug)] +enum Proc1Error { + IOError(std::io::Error), + NoOption, + ChildPidTooBig(u32, std::num::TryFromIntError), +} + +impl std::convert::From for Proc1Error { + fn from(e: std::io::Error) -> Self { + Proc1Error::IOError(e) + } +} + +fn get_command() -> Result<(String, Vec), Proc1Error> { + let mut args = std::env::args(); + let _me = args.next(); + match args.next() { + None => Err(Proc1Error::NoOption), + Some(cmd) => Ok((cmd, args.collect())), + } +} + fn parse_inittab() -> Result<(), Error> { let inittab_file = File::open("/etc/inittab")?; let buf_reader = std::io::BufReader::new(inittab_file); @@ -26,7 +49,26 @@ fn parse_inittab() -> Result<(), Error> { return Ok(()) } -fn main() -> Result<(), Error> { + +fn main() -> Result<(), Proc1Error> { + match (get_command()) { + Ok((cmd, args)) => execute_mode(cmd, args), + Err(Proc1Error::NoOption) => standalone_mode(), + Err(_) => Ok(()), + }; + + Ok(()) +} + +fn execute_mode(s: String, ar: Vec) -> Result<(), Proc1Error> { + println!("running to execute specific command"); + println!("{:?}", s); + println!("{:?}", ar); + return Ok(()) +} + +fn standalone_mode() -> Result<(), Proc1Error> { + println!("running as standalone pid1"); // parse system init configuration // inittab is obsolated by systemd, should we honor the legacy config in that ? // systemd use /etc/systemd/system/default.target, which is a sym link to actual target. @@ -55,5 +97,5 @@ fn main() -> Result<(), Error> { } } } - Ok(()) + return Ok(()); } -- Gitee