diff options
Diffstat (limited to 'init/src/services')
| -rw-r--r-- | init/src/services/units.rs | 31 |
1 files changed, 24 insertions, 7 deletions
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>)?; |
