From e2cc4c2b38153cca8c4447c69c01cd5bb9355bde Mon Sep 17 00:00:00 2001 From: Jeremy Whiting Date: Mon, 2 Oct 2023 12:31:28 -0600 Subject: [PATCH] Add basic running script functionality. Will likely move api methods out to be more generic, but this runs for now and gives true on success, false on failure, etc. May also need to change from process.poll to wait for longer running scripts. will do once tested more. --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/manager.rs | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 561ddfe..587fafb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -875,10 +875,21 @@ version = "0.1.0" dependencies = [ "async-std", "serde", + "subprocess", "zbus", "zbus_macros", ] +[[package]] +name = "subprocess" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c2e86926081dda636c546d8c5e641661049d7562a68f5488be4a1f7f66f6086" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "syn" version = "1.0.109" diff --git a/Cargo.toml b/Cargo.toml index 0360eb6..07c70b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,6 @@ edition = "2021" [dependencies] async-std = { version = "1.12.0", features = ["attributes"] } serde = { version = "1.0.188", features = ["derive"] } +subprocess = "0.2.9" zbus = "3.14.1" zbus_macros = "3.14.1" diff --git a/src/manager.rs b/src/manager.rs index 09cb464..c87a651 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -23,19 +23,39 @@ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +use subprocess::{ExitStatus::Exited, Popen, PopenConfig, Redirection}; use zbus_macros::dbus_interface; use zbus::{ObjectServer, SignalContext, MessageHeader}; pub struct SMManager { } +fn run_script(script: String) -> bool { + // Run given script and return true on success + let mut process = Popen::create(&[script], PopenConfig { + stdout: Redirection::Pipe, ..Default::default() + }).unwrap(); + let (out, err) = process.communicate(None).unwrap(); + if let Some(exit_status) = process.poll() { + return exit_status == Exited(0); + } else { + return false; + } +} + #[dbus_interface(name = "com.steampowered.SteamOSManager1")] impl SMManager { const API_VERSION: u32 = 1; + async fn say_hello(&self, name: &str) -> String { format!("Hello {}!", name) } + async fn factory_reset(&self) -> bool { + // Run steamos factory reset script and return true on success + return run_script("steamos-factory-reset".to_string()); + } + /// A version property. #[dbus_interface(property)] async fn version(&self) -> u32 {