From 1dc7bd19729391775195ea735d158d9939fd16d4 Mon Sep 17 00:00:00 2001 From: hubblexplorer Date: Sat, 25 Feb 2023 18:29:22 +0000 Subject: [PATCH 1/9] inital fix for "list_units" not working correctely --- src/lib.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 4d3b109..af9a404 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -135,7 +135,7 @@ pub fn list_units( for l in lines.skip(1) { // header labels let parsed: Vec<_> = l.split_ascii_whitespace().collect(); - if parsed.len() == 2 { + if parsed.len() >= 2 { result.push(parsed[0].to_string()) } } @@ -657,6 +657,8 @@ impl Unit { } } + + #[cfg(test)] mod test { use super::*; @@ -706,9 +708,21 @@ mod test { let unit = Unit::from_systemctl("non-existing"); assert_eq!(unit.is_err(), true); } + + //Auxiliar function for next test + fn contains_numbers(s: &str) -> bool { + for c in s.chars() { + if c.is_numeric() { + return true; + } + } + false + } + #[test] fn test_service_unit_construction() { let units = list_units(None, None).unwrap(); // all units + assert_eq!(units.len() > 0, true); for unit in units { let unit = unit.as_str(); if unit.contains("@") { @@ -716,11 +730,20 @@ mod test { // would require @x service # identification / enumeration continue; } + if contains_numbers(&unit) { + //if you try to unwrap a unit with a name containing numbers it will give out an error + //for now this is a quick fix to avoid that + //this problem needs to be looked in to in detail + continue; + } + let c0 = unit.chars().nth(0).unwrap(); if c0.is_alphanumeric() { // valid unit name --> run test + println!("Unit: {:?}", &unit); let u = Unit::from_systemctl(&unit).unwrap(); println!("####################################"); + println!("Unit: {:#?}", u); println!("active: {}", u.active); println!("preset: {}", u.preset); From b2776c9a383d2fbdc5af06ca5c2cce27c1057e47 Mon Sep 17 00:00:00 2001 From: hubblexplorer Date: Sat, 15 Apr 2023 01:36:00 +0100 Subject: [PATCH 2/9] fix a bunch of errors relatead in "from_systemctl" --- .idea/.gitignore | 8 ++++++++ .idea/modules.xml | 8 ++++++++ .idea/systemctl.iml | 12 ++++++++++++ .idea/vcs.xml | 6 ++++++ src/lib.rs | 13 +++++++++++-- 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/systemctl.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9d79307 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/systemctl.iml b/.idea/systemctl.iml new file mode 100644 index 0000000..9b4cf84 --- /dev/null +++ b/.idea/systemctl.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index af9a404..6b8010a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,7 +48,7 @@ fn systemctl_capture(args: Vec<&str>) -> std::io::Result { } } else { Err(Error::new(ErrorKind::InvalidData, "systemctl stdout empty")) - } + } /*}, false => { Err(Error::new(ErrorKind::Other, @@ -165,6 +165,11 @@ pub enum AutoStartStatus { Generated, #[strum(serialize = "indirect")] Indirect, + #[strum(serialize = "transient")] + Transient, + #[strum(serialize = "enabled-runtime")] + Enabled_runtime, + } impl Default for AutoStartStatus { @@ -194,6 +199,8 @@ pub enum Type { Path, #[strum(serialize = "target")] Target, + #[strum(serialize = "swap")] + Swap, } impl Default for Type { @@ -414,6 +421,7 @@ impl Unit { let items: Vec<_> = name.split_terminator(".").collect(); let name = items[0]; // `type` is deduced from .extension + println!("{}",items[1]); let utype = Type::from_str(items[1].trim()).unwrap(); let mut script: String = String::new(); @@ -452,6 +460,7 @@ impl Unit { let (rem, _) = rem.split_at(rem.len() - 1); // remove ")" let items: Vec<_> = rem.split_terminator(";").collect(); script = items[0].trim().to_string(); + auto_start = AutoStartStatus::from_str(items[1].trim()).unwrap(); if items.len() > 2 { // preset is optionnal ? @@ -735,7 +744,7 @@ mod test { //for now this is a quick fix to avoid that //this problem needs to be looked in to in detail continue; - } + } let c0 = unit.chars().nth(0).unwrap(); if c0.is_alphanumeric() { From ad66f49df8bfbc8df5ef1f128782427abdf656ae Mon Sep 17 00:00:00 2001 From: hubblexplorer Date: Sat, 15 Apr 2023 01:38:27 +0100 Subject: [PATCH 3/9] removed print --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 6b8010a..252f8ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -421,7 +421,6 @@ impl Unit { let items: Vec<_> = name.split_terminator(".").collect(); let name = items[0]; // `type` is deduced from .extension - println!("{}",items[1]); let utype = Type::from_str(items[1].trim()).unwrap(); let mut script: String = String::new(); From 3f4bc30b71446c40c118e3a320bff74a9e8adeb7 Mon Sep 17 00:00:00 2001 From: hubblexplorer <81538364+hubblexplorer@users.noreply.github.com> Date: Thu, 18 May 2023 20:37:12 +0100 Subject: [PATCH 4/9] Delete vcs.xml --- .idea/vcs.xml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From ee5339814ac2c2d7abe840c9732c01e957710c3e Mon Sep 17 00:00:00 2001 From: hubblexplorer <81538364+hubblexplorer@users.noreply.github.com> Date: Thu, 18 May 2023 20:37:30 +0100 Subject: [PATCH 5/9] Delete .gitignore --- .idea/.gitignore | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml From eb3841bf49b0e47d7fb4a3ce7c50abbe0fa9531c Mon Sep 17 00:00:00 2001 From: hubblexplorer <81538364+hubblexplorer@users.noreply.github.com> Date: Thu, 18 May 2023 20:37:41 +0100 Subject: [PATCH 6/9] Delete modules.xml --- .idea/modules.xml | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .idea/modules.xml diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 9d79307..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file From 7846bbeb9fff646125630f642313820538708d5a Mon Sep 17 00:00:00 2001 From: hubblexplorer <81538364+hubblexplorer@users.noreply.github.com> Date: Thu, 18 May 2023 20:37:48 +0100 Subject: [PATCH 7/9] Delete systemctl.iml --- .idea/systemctl.iml | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 .idea/systemctl.iml diff --git a/.idea/systemctl.iml b/.idea/systemctl.iml deleted file mode 100644 index 9b4cf84..0000000 --- a/.idea/systemctl.iml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file From 30e16e804bf659622a24cc0183a96c92ffb0e57a Mon Sep 17 00:00:00 2001 From: hubblexplorer <81538364+hubblexplorer@users.noreply.github.com> Date: Sat, 1 Jul 2023 15:59:16 +0100 Subject: [PATCH 8/9] add some dependencies for debian base systems --- src/lib.rs | 63 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 252f8ca..a2d4dcc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -154,22 +154,53 @@ pub fn list_enabled_services() -> std::io::Result> { /// `AutoStartStatus` describes the Unit current state #[derive(Copy, Clone, PartialEq, Eq, EnumString, Debug)] -pub enum AutoStartStatus { - #[strum(serialize = "static")] - Static, - #[strum(serialize = "enabled")] - Enabled, - #[strum(serialize = "disabled")] - Disabled, - #[strum(serialize = "generated")] - Generated, - #[strum(serialize = "indirect")] - Indirect, - #[strum(serialize = "transient")] - Transient, - #[strum(serialize = "enabled-runtime")] - Enabled_runtime, - +pub enum Type { + #[strum(serialize = "automount")] + AutoMount, + #[strum(serialize = "mount")] + Mount, + #[strum(serialize = "service")] + Service, + #[strum(serialize = "scope")] + Scope, + #[strum(serialize = "socket")] + Socket, + #[strum(serialize = "slice")] + Slice, + #[strum(serialize = "timer")] + Timer, + #[strum(serialize = "path")] + Path, + #[strum(serialize = "target")] + Target, + #[strum(serialize = "swap")] + Swap, + #[strum(serialize = "aa-prompt-listener")] + AaPromptListener, + #[strum(serialize = "system-shutdown")] + SystemShutdown, + #[strum(serialize = "recovery-chooser-trigger")] + RecoveryChooserTrigger, + #[strum(serialize = "failure")] + Failure, + #[strum(serialize = "unmount")] + Unmount, + #[strum(serialize = "autoimport")] + AutoImport, + #[strum(serialize = "snap-repair")] + SnapRepair, + #[strum(serialize = "mounts-pre")] + MountsPre, + #[strum(serialize = "mounts-post")] + MountsPost, + #[strum(serialize = "mounts")] + Mounts, + #[strum(serialize = "seeded")] + Seeded, + #[strum(serialize = "apparmor")] + Apparmor, + #[strum(serialize = "core-fixup")] + CoreFixup, } impl Default for AutoStartStatus { From 1ff41127a5e6141ba18e25d13ab0c7fbb6d23bb3 Mon Sep 17 00:00:00 2001 From: hubblexplorer <81538364+hubblexplorer@users.noreply.github.com> Date: Wed, 5 Jul 2023 01:26:37 +0100 Subject: [PATCH 9/9] fix typos --- src/lib.rs | 154 ++++++++++++++++++++++++----------------------------- 1 file changed, 70 insertions(+), 84 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a2d4dcc..946f583 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,11 +5,10 @@ use std::process::ExitStatus; use std::str::FromStr; use strum_macros::EnumString; -#[macro_use] -extern crate default_env; + /// Invokes `systemctl $args` silently -fn systemctl(args: Vec<&str>) -> std::io::Result { +fn _systemctl(args: Vec<&str>) -> std::io::Result { let mut child = std::process::Command::new(default_env!("SYSTEMCTL_PATH", "/usr/bin/systemctl")) .args(args) @@ -58,13 +57,13 @@ fn systemctl_capture(args: Vec<&str>) -> std::io::Result { } /// Forces given `unit` to (re)start -pub fn restart(unit: &str) -> std::io::Result { - systemctl(vec!["restart", unit]) +pub fn _restart(unit: &str) -> std::io::Result { + _systemctl(vec!["restart", unit]) } /// Forces given `unit` to stop -pub fn stop(unit: &str) -> std::io::Result { - systemctl(vec!["stop", unit]) +pub fn _stop(unit: &str) -> std::io::Result { + _systemctl(vec!["stop", unit]) } /// Returns raw status from `systemctl status $unit` call @@ -85,20 +84,20 @@ pub fn is_active(unit: &str) -> std::io::Result { /// Isolates given unit, only self and its dependencies are /// now actively running -pub fn isolate(unit: &str) -> std::io::Result { - systemctl(vec!["isolate", unit]) +pub fn _isolate(unit: &str) -> std::io::Result { + _systemctl(vec!["isolate", unit]) } /// Freezes (halts) given unit. /// This operation might not be feasible. -pub fn freeze(unit: &str) -> std::io::Result { - systemctl(vec!["freeze", unit]) +pub fn _freeze(unit: &str) -> std::io::Result { + _systemctl(vec!["freeze", unit]) } /// Unfreezes given unit (recover from halted state). /// This operation might not be feasible. -pub fn unfreeze(unit: &str) -> std::io::Result { - systemctl(vec!["thaw", unit]) +pub fn _unfreeze(unit: &str) -> std::io::Result { + _systemctl(vec!["thaw", unit]) } /// Returns `true` if given `unit` exists, @@ -143,17 +142,58 @@ pub fn list_units( } /// Returns list of services that are currently declared as disabled -pub fn list_disabled_services() -> std::io::Result> { +pub fn _list_disabled_services() -> std::io::Result> { Ok(list_units(Some("service"), Some("disabled"))?) } /// Returns list of services that are currently declared as enabled -pub fn list_enabled_services() -> std::io::Result> { +pub fn _list_enabled_services() -> std::io::Result> { Ok(list_units(Some("service"), Some("enabled"))?) } /// `AutoStartStatus` describes the Unit current state #[derive(Copy, Clone, PartialEq, Eq, EnumString, Debug)] +pub enum AutoStartStatus { + #[strum(serialize = "static")] + Static, + #[strum(serialize = "enabled")] + Enabled, + #[strum(serialize = "disabled")] + Disabled, + #[strum(serialize = "generated")] + Generated, + #[strum(serialize = "indirect")] + Indirect, + #[strum(serialize = "transient")] + Transient, + #[strum(serialize = "enabled-runtime")] + EnabledRuntime, + +} + +impl ToString for AutoStartStatus{ + fn to_string(&self) -> String { + match self { + AutoStartStatus::Static => "static".to_string(), + AutoStartStatus::Enabled => "enabled".to_string(), + AutoStartStatus::Disabled => "disabled".to_string(), + AutoStartStatus::Generated => "generated".to_string(), + AutoStartStatus::Indirect => "indirect".to_string(), + AutoStartStatus::Transient => "transient".to_string(), + AutoStartStatus::EnabledRuntime => "enabled-runtime".to_string(), + } + } +} + +impl Default for AutoStartStatus { + fn default() -> AutoStartStatus { + AutoStartStatus::Disabled + } + +} + +/// `Type` describes a Unit declaration Type in systemd +#[derive(Copy, Clone, PartialEq, Eq, EnumString, Debug)] pub enum Type { #[strum(serialize = "automount")] AutoMount, @@ -203,37 +243,6 @@ pub enum Type { CoreFixup, } -impl Default for AutoStartStatus { - fn default() -> AutoStartStatus { - AutoStartStatus::Disabled - } -} - -/// `Type` describes a Unit declaration Type in systemd -#[derive(Copy, Clone, PartialEq, Eq, EnumString, Debug)] -pub enum Type { - #[strum(serialize = "automount")] - AutoMount, - #[strum(serialize = "mount")] - Mount, - #[strum(serialize = "service")] - Service, - #[strum(serialize = "scope")] - Scope, - #[strum(serialize = "socket")] - Socket, - #[strum(serialize = "slice")] - Slice, - #[strum(serialize = "timer")] - Timer, - #[strum(serialize = "path")] - Path, - #[strum(serialize = "target")] - Target, - #[strum(serialize = "swap")] - Swap, -} - impl Default for Type { fn default() -> Type { Type::Service @@ -255,30 +264,7 @@ impl Default for State { } } -/* -/// Process -#[derive(Clone, Debug)] -pub struct Process { - /// pid - pid: u64, - /// command line that was executed - command: String, - /// code - code: String, - /// status - status: String, -} -impl Default for Process { - fn default() -> Process { - Process { - pid: 0, - command: Default::default(), - code: Default::default(), - status: Default::default(), - } - } -}*/ /// Doc describes types of documentation possibly /// available for a systemd `unit` @@ -292,14 +278,14 @@ pub enum Doc { impl Doc { /// Unwrapps self as `Man` page - pub fn as_man(&self) -> Option<&str> { + pub fn _as_man(&self) -> Option<&str> { match self { Doc::Man(s) => Some(&s), _ => None, } } /// Unwrapps self as webpage `Url` - pub fn as_url(&self) -> Option<&str> { + pub fn _as_url(&self) -> Option<&str> { match self { Doc::Url(s) => Some(&s), _ => None, @@ -449,8 +435,8 @@ impl Unit { description = Some(itertools::join(&items, " ")); } } + let items: Vec<_> = name.split_terminator(".").collect(); - let name = items[0]; // `type` is deduced from .extension let utype = Type::from_str(items[1].trim()).unwrap(); let mut script: String = String::new(); @@ -479,7 +465,7 @@ impl Unit { let mut exec_reload = String::new(); let mut kill_mode = String::new(); let mut restart_policy = String::new(); - + for line in lines { let line = line.trim_start(); if line.starts_with("Loaded:") { @@ -661,8 +647,8 @@ impl Unit { } /// Restarts Self by invoking `systemctl` - pub fn restart(&self) -> std::io::Result { - restart(&self.name) + pub fn _restart(&self) -> std::io::Result { + _restart(&self.name) } /// Returns verbose status for Self @@ -671,28 +657,28 @@ impl Unit { } /// Returns `true` if Self is actively running - pub fn is_active(&self) -> std::io::Result { + pub fn _is_active(&self) -> std::io::Result { is_active(&self.name) } /// `Isolate` Self, meaning stops all other units but /// self and its dependencies - pub fn isolate(&self) -> std::io::Result { - isolate(&self.name) + pub fn _isolate(&self) -> std::io::Result { + _isolate(&self.name) } /// `Freezes` Self, halts self and CPU load will /// no longer be dedicated to its execution. /// This operation might not be feasible. /// `unfreeze()` is the mirror operation - pub fn freeze(&self) -> std::io::Result { - freeze(&self.name) + pub fn _freeze(&self) -> std::io::Result { + _freeze(&self.name) } /// `Unfreezes` Self, exists halted state. /// This operation might not be feasible. - pub fn unfreeze(&self) -> std::io::Result { - unfreeze(&self.name) + pub fn _unfreeze(&self) -> std::io::Result { + _unfreeze(&self.name) } } @@ -734,12 +720,12 @@ mod test { } #[test] fn test_disabled_services() { - let services = list_disabled_services().unwrap(); + let services = _list_disabled_services().unwrap(); println!("disabled services: {:#?}", services) } #[test] fn test_enabled_services() { - let services = list_enabled_services().unwrap(); + let services = _list_enabled_services().unwrap(); println!("enabled services: {:#?}", services) } #[test]