diff options
| author | namilsk <namilsk@namilsk.tech> | 2026-02-02 22:33:30 +0300 |
|---|---|---|
| committer | namilsk <namilsk@namilsk.tech> | 2026-02-02 22:33:30 +0300 |
| commit | edf1c07498fca80e09579586aaa4dda914f088b5 (patch) | |
| tree | 020d29bbb41c2b65548293783610252f10c3b02a | |
| parent | 913da896fcc161b138932655c3c7864cbde3d6a7 (diff) | |
Fixed `.unwrap()` call in units.rs, fixed all clippy warnings, refactor child exit-status check
| -rw-r--r-- | init/src/host/locale.rs | 4 | ||||
| -rw-r--r-- | init/src/kmods/load.rs | 7 | ||||
| -rw-r--r-- | init/src/main.rs | 2 | ||||
| -rw-r--r-- | init/src/mounts/fstab.rs | 2 | ||||
| -rw-r--r-- | init/src/services/units.rs | 31 |
5 files changed, 31 insertions, 15 deletions
diff --git a/init/src/host/locale.rs b/init/src/host/locale.rs index 3d9b11d..9ce0a3c 100644 --- a/init/src/host/locale.rs +++ b/init/src/host/locale.rs @@ -13,9 +13,9 @@ use std::process::Command; /// /// Logic && Checks /// 1. Reading /etc/default/locale to find needed language. -/// If it broken/has syntax errors skipping this step +/// If it broken/has syntax errors skipping this step /// 2. Checking for locale avalible (also switching `-` and `_`), -/// if not uses fallback locale. +/// If not uses fallback locale. /// pub fn set_locale(locale: Option<String>) -> Result<(), Box<dyn std::error::Error>> { let loc = match locale { diff --git a/init/src/kmods/load.rs b/init/src/kmods/load.rs index 374ce36..8ff0a59 100644 --- a/init/src/kmods/load.rs +++ b/init/src/kmods/load.rs @@ -1,5 +1,4 @@ -use liblmod::Selection; -use liblmod::modprobe; +use liblmod::{modprobe, Selection}; use std::fs; fn parse_modules() -> Result<Vec<String>, Box<dyn std::error::Error>> { @@ -14,8 +13,8 @@ fn parse_modules() -> Result<Vec<String>, Box<dyn std::error::Error>> { pub fn load_modules() -> Result<(), Box<dyn std::error::Error>> { let modules = parse_modules()?; - for i in 0..modules.len() { - modprobe(modules[i].to_string(), "".to_string(), Selection::Current)?; + for kmod in &modules { + modprobe(kmod.to_string(), "".to_string(), Selection::Current)?; } Ok(()) diff --git a/init/src/main.rs b/init/src/main.rs index fa7d086..6821bd6 100644 --- a/init/src/main.rs +++ b/init/src/main.rs @@ -79,7 +79,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { let _ = sigchld::setup_sigchld_handler(); - thread::spawn(move || services_mainloop()); + thread::spawn(services_mainloop); Ok(()) } diff --git a/init/src/mounts/fstab.rs b/init/src/mounts/fstab.rs index 7d71520..5fe505f 100644 --- a/init/src/mounts/fstab.rs +++ b/init/src/mounts/fstab.rs @@ -173,7 +173,7 @@ impl FstabEntry { let target_c = CString::new(&*self.mountpoint)?; let fstype_c = CString::new(&*self.fstype)?; - let data_c = data.map(|s| CString::new(s)).transpose()?; + let data_c = data.map(CString::new).transpose()?; let data_ptr = data_c .as_ref() .map_or(std::ptr::null(), |s| s.as_ptr() as *const libc::c_void); diff --git a/init/src/services/units.rs b/init/src/services/units.rs index 6e3223e..20561e9 100644 --- a/init/src/services/units.rs +++ b/init/src/services/units.rs @@ -10,11 +10,11 @@ // or logs in such init systems are just taking the stdout+stderr // of service and showing its output? idk 4 now, look into how its supposed to be -use crate::{RUNLEVEL_STATE, log_warning}; +use crate::{log_warning, RUNLEVEL_STATE}; use serde::Deserialize; use std::{ fs::{read_dir, read_to_string}, - process::{Child, ExitStatus}, + process::Child, time::Duration, }; @@ -42,18 +42,25 @@ enum Restart { #[derive(Deserialize, PartialEq, Eq)] pub enum Runlevel { /// The system is shutting down, runlevel int: 0 + #[serde(alias = "shutdown")] Shutdown, /// One-user system debug-mode, runlevel int: 1 + #[serde(alias = "one-user")] OneUser, /// Multi-user CLI (TTY) with no network, runlevel int: 2 + #[serde(alias = "multiuser-no-network")] MultiNoNetwork, /// Multi-user CLI with network, runlevel int: 3 + #[serde(alias = "multiuser-network")] MultiNetwork, /// Multi-user mode with GUI, runlevel int: 5 + #[serde(alias = "multiuser-network-gui")] MultiGUINetwork, /// Runlevel is not critical for running the service, runlevel int: 4 + #[serde(alias = "undefiled")] Undefined, /// System going too reboot, runlevel int: 6 + #[serde(alias = "reboot")] Reboot, } @@ -87,7 +94,7 @@ pub struct Unit { config: ServiceConfig, } -#[allow(dead_code)] +/* impl Restart { pub fn as_str(&self) -> &'static str { match self { @@ -96,11 +103,12 @@ impl Restart { } } } +*/ + /// Function: /// 1. starting services declared in `/etc/vigil/units/` -/// 2. checks services for dropping and if ts true to restarting it -/// max restart count: 3 +/// 2. checks services for dropping and if ts true to restarting it max restart count: 3 /// /// Based on the global runlevel variable, should be runned as second thread // TODO: More logs && better errors processing && fix clippy warnings @@ -108,8 +116,17 @@ pub fn services_mainloop() -> Result<(), Box<dyn std::error::Error + Send>> { let unit_list = parse_all_units()?; let mut pids: Vec<(Child, String, Restart, u8)> = vec![]; + let guard = match RUNLEVEL_STATE.lock() { + Ok(g) => g, + Err(poisoned) => { + log_warning("Mutex was poisoned. Recovering"); + poisoned.into_inner() + } + }; + loop { - match *RUNLEVEL_STATE.lock().unwrap() { + /* *RUNLEVEL_STATE.lock().unwrap() */ + match *guard { Runlevel::Undefined => { for unit in &unit_list { if unit.config.runlevel == Runlevel::Undefined { @@ -226,7 +243,7 @@ pub fn services_mainloop() -> Result<(), Box<dyn std::error::Error + Send>> { for i in 0..pids.len() { match pids[i].0.try_wait() { Ok(Some(status)) => { - if pids[i].2 == Restart::Always && pids[i].3 < 3 && status.code() != Some(0) { + if pids[i].2 == Restart::Always && pids[i].3 < 3 && !status.success() { let new_child = std::process::Command::new(pids[i].1.clone()) .spawn() .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send>)?; |
