From 19e63dfa191279d3e1bd99bcc2adca781ad9451d Mon Sep 17 00:00:00 2001 From: namilsk Date: Sun, 11 Jan 2026 14:56:17 +0300 Subject: Implemented `SIGCHLD` & again fixed naming --- Cargo.lock | 98 +++++++++++++++++++++++++++++++++++++++++++++ TODO.md | 6 +-- init/Cargo.toml | 1 + init/src/main.rs | 5 +++ init/src/processes/getty.rs | 0 init/src/signals/mod.rs | 1 + init/src/signals/sigchld.rs | 24 ++++++++++- vigilctl/Cargo.toml | 5 ++- 8 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 init/src/processes/getty.rs create mode 100644 init/src/signals/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 958566a..ebaca9b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,18 +98,41 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" + [[package]] name = "heck" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "indexmap" +version = "2.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "init" version = "0.1.0" dependencies = [ "libc", "liblmod", + "toml", ] [[package]] @@ -154,6 +177,35 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_spanned" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8bbf91e5a4d6315eee45e704372590b30e260ee83af6639d64557f51b067776" +dependencies = [ + "serde_core", +] + [[package]] name = "strsim" version = "0.11.1" @@ -171,6 +223,45 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "toml" +version = "0.9.11+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3afc9a848309fe1aaffaed6e1546a7a14de1f935dc9d89d32afd9a44bab7c46" +dependencies = [ + "indexmap", + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + +[[package]] +name = "toml_datetime" +version = "0.7.5+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92e1cfed4a3038bc5a127e35a2d360f145e1f4b971b551a2ba5fd7aedf7e1347" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_parser" +version = "1.0.6+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3198b4b0a8e11f09dd03e133c0280504d0801269e9afa46362ffde1cbeebf44" +dependencies = [ + "winnow", +] + +[[package]] +name = "toml_writer" +version = "1.0.6+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab16f14aed21ee8bfd8ec22513f7287cd4a91aa92e44edfe2c17ddd004e92607" + [[package]] name = "unicode-ident" version = "1.0.22" @@ -188,6 +279,7 @@ name = "vegilctl" version = "0.1.0" dependencies = [ "clap", + "toml", ] [[package]] @@ -204,3 +296,9 @@ checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" dependencies = [ "windows-link", ] + +[[package]] +name = "winnow" +version = "0.7.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5364e9d77fcdeeaa6062ced926ee3381faa2ee02d3eb83a5c27a8825540829" diff --git a/TODO.md b/TODO.md index fe5ded6..fe32042 100644 --- a/TODO.md +++ b/TODO.md @@ -24,9 +24,9 @@ - [ ] Start/stop/restart/status via `vigilctl` - [ ] Handle `Wants=`, `After=`, `Before=` dependencies - [ ] Auto-restart failed services (configurable via option `restart = always|on-failure|never`) -- [ ] **Child process reaping** - - [ ] Install `SIGCHLD` handler - - [ ] Call `waitpid(-1, ...)` in loop to reap zombies +- [x] **Child process reaping** + - [x] Install `SIGCHLD` handler + - [x] Call `waitpid(-1, ...)` in loop to reap zombies - [ ] Log exit status, signal, and runtime duration per service - [ ] **TTY & login** - [ ] Launch `getty` on configured TTYs (e.g. `tty1`–`tty6`) diff --git a/init/Cargo.toml b/init/Cargo.toml index 9ff5782..d68c550 100644 --- a/init/Cargo.toml +++ b/init/Cargo.toml @@ -7,3 +7,4 @@ description = "Lightweight and stable init system for Anthrill distro based on u [dependencies] libc = "0.2.178" liblmod = "0.2.0" +toml = { version = "0.9.11", features = ["display", "std"], default-features = false } diff --git a/init/src/main.rs b/init/src/main.rs index 7a77e31..630787e 100644 --- a/init/src/main.rs +++ b/init/src/main.rs @@ -4,6 +4,7 @@ mod log; mod mounts; mod pid_one; mod processes; +mod signals; use crate::host::locale; use crate::host::set::set_hostname; @@ -13,6 +14,7 @@ use crate::mounts::fstab::FstabEntry; use crate::mounts::rescue; use crate::pid_one::check_pid; use crate::processes::udev::spawn_udev; +use crate::signals::sigchld; // RULE: I will not use .expect() and .unwrap() in this project. This causes panic, // which will affect stability. @@ -60,10 +62,13 @@ fn main() -> Result<(), Box> { Err(e) => log_critical_error(&format!("Something went wrong while spawning udev: {}", e)), } + // TODO: Locale & tz parsing match locale::set_system_localization(None, None) { Ok(_) => log_success("Localization (timezone and locale) set successfully."), Err(e) => log_warning(&format!("Failed to set localization: {}", e)), } + sigchld::setup_sigchild_handler(); + Ok(()) } diff --git a/init/src/processes/getty.rs b/init/src/processes/getty.rs new file mode 100644 index 0000000..e69de29 diff --git a/init/src/signals/mod.rs b/init/src/signals/mod.rs new file mode 100644 index 0000000..0e86476 --- /dev/null +++ b/init/src/signals/mod.rs @@ -0,0 +1 @@ +pub mod sigchld; diff --git a/init/src/signals/sigchld.rs b/init/src/signals/sigchld.rs index 78b72d4..185e56e 100644 --- a/init/src/signals/sigchld.rs +++ b/init/src/signals/sigchld.rs @@ -1,10 +1,17 @@ -use libc::{signal, sighandler_t, SIGCHILD, SIG_DFL, SIG_IGN}; +use libc::{pid_t, + SIGCHLD, + waitpid, + WNOHANG, + sigaction, + sigemptyset, + SA_RESTART}; + use std::ffi::c_int; fn sigchild_handler(_signal: c_int) { loop { let mut status: c_int = 0; - ket pid: pid_t = unsafe { + let pid: pid_t = unsafe { waitpid(-1 , &mut status, WNOHANG) }; @@ -15,4 +22,17 @@ fn sigchild_handler(_signal: c_int) { } +pub fn sigchild_handler() -> Result<(), Box> { + unsafe { + let mut sigact: sigaction = std::mem::zeroed(); + sigact.sa_sigaction = sigchild_handler as usize; + sigemptyset(&mut sigact.sa_mask); + sigact.sa_flags = SA_RESTART; + if sigaction(SIGCHLD, &sigact, std::ptr::null_mut()) == -1 { + Err("Sigaction check returned -1".into()) + } else { + Ok(()) + } + } +} diff --git a/vigilctl/Cargo.toml b/vigilctl/Cargo.toml index 0a89458..9af2e5a 100644 --- a/vigilctl/Cargo.toml +++ b/vigilctl/Cargo.toml @@ -1,8 +1,9 @@ [package] -name = "vegilctl" +name = "vigilctl" version = "0.1.0" edition = "2024" -description = "Vegil init user control." +description = "Vigil init user control." [dependencies] clap = { version = "4.5.53", features = [ "derive"]} +toml = "0.9.11" -- cgit v1.2.3