diff --git a/src/main/Cargo.toml b/src/main/Cargo.toml index 8fe34953067c03b1937927194c652a602902b5b0..18d0ca50ebcf8dce7601ee7ffbb749119f133b40 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 7dfdc111aa8244e226912ceba80e182b3e88483b..58645239a442b877bbaea56329ce141524ed51c1 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(()); }