diff --git a/Cargo.lock b/Cargo.lock index 517372b..07639be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -110,6 +110,15 @@ dependencies = [ "winit", ] +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + [[package]] name = "adler2" version = "2.0.0" @@ -224,6 +233,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "anyhow" +version = "1.0.96" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b964d184e89d9b6b67dd2715bc8e74cf3107fb2b529990c90cf517326150bf4" + [[package]] name = "arboard" version = "3.4.1" @@ -475,6 +490,21 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets 0.52.6", +] + [[package]] name = "base64" version = "0.21.7" @@ -1451,6 +1481,12 @@ dependencies = [ "weezl", ] +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + [[package]] name = "git2" version = "0.20.0" @@ -1596,13 +1632,16 @@ dependencies = [ name = "hai" version = "0.1.0" dependencies = [ + "anyhow", "bunt", "clap", "eframe", "egui", "egui_extras", "env_logger", + "log", "shadow-rs", + "tokio", ] [[package]] @@ -2125,6 +2164,17 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "mio" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" +dependencies = [ + "libc", + "wasi 0.11.0+wasi-snapshot-preview1", + "windows-sys 0.52.0", +] + [[package]] name = "naga" version = "24.0.0" @@ -2462,6 +2512,15 @@ dependencies = [ "objc2-foundation", ] +[[package]] +name = "object" +version = "0.36.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.20.3" @@ -2841,6 +2900,12 @@ version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3cd14fd5e3b777a7422cca79358c57a8f6e3a703d9ac187448d0daf220c2407f" +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + [[package]] name = "rustc-hash" version = "1.1.0" @@ -3091,6 +3156,16 @@ dependencies = [ "serde", ] +[[package]] +name = "socket2" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "spin" version = "0.9.8" @@ -3336,6 +3411,35 @@ dependencies = [ "zerovec", ] +[[package]] +name = "tokio" +version = "1.43.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d61fa4ffa3de412bfea335c6ecff681de2b609ba3c77ef3e00e521813a9ed9e" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.52.0", +] + +[[package]] +name = "tokio-macros" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "toml_datetime" version = "0.6.8" diff --git a/Cargo.toml b/Cargo.toml index f03f242..3cd09d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,13 +7,16 @@ publish = false license = "AGPL-3.0-or-later" [dependencies] +anyhow = "1.0.96" bunt = "0.2.8" clap = { version = "4.5.30", features = ["derive"] } eframe = { version = "0.31", features = ["default", "__screenshot"] } egui = "0.31.0" egui_extras = { version = "0.31", features = ["default", "image", "all_loaders"] } env_logger = "0.11.6" +log = "0.4.25" shadow-rs = "0.38" +tokio = { version = "1.43", features = ["full"] } [build-dependencies] shadow-rs = "0.38" diff --git a/src/main.rs b/src/main.rs index 4dab361..2aa4dfb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,26 @@ -/* - * SPDX-FileCopyrightText: 2025 April Faye John & Contributors +/* + * SPDX-FileCopyrightText: 2025 April Faye John & Contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release -mod gui; mod bininfo; +mod gui; mod pman; -use clap::{Subcommand, Parser}; -use pman::init_process_manager; +use bunt::println; +use clap::{Parser, Subcommand}; +use log::info; +use pman::{init_process_manager, ProcessCommand, ProcessManager}; use shadow_rs::shadow; +use std::sync::{Arc, Mutex, OnceLock}; +use tokio::{signal, sync::mpsc::Sender}; shadow!(build); +static PMAN_SENDER: OnceLock> = OnceLock::new(); + /// Simple program to greet a person #[derive(Parser, Debug)] #[command(version, about, long_about = None)] @@ -39,30 +45,26 @@ enum Commands { remote: String, }, #[command(about = "List compile time backed info to audit binary")] - Shadow { - outfile: Option, - }, + Shadow { outfile: Option }, #[command(about = "Show test gui")] Gui, + Devtest, } +#[tokio::main] +async fn main() { -fn main() { + env_logger::init(); let args = Args::parse(); - init_process_manager(); + let _ = init_process_manager(); match args.command { - - Commands::Clone { remote } => { - - } + Commands::Clone { remote } => {} Commands::Shadow { outfile } => { - bininfo::print_info(); return; - } Commands::Gui => { @@ -70,7 +72,20 @@ fn main() { if let Err(e) = res { println!("{}", e); } + return; } + Commands::Devtest => { + tokio::select! { + _ = signal::ctrl_c() => { + info!("Ctrl-c received"); + let sender = PMAN_SENDER.get().unwrap(); + let _ = sender.send(ProcessCommand::Shutdown).await; + } + } + } }; + + //handling anything here for gui wont work + println!("exit"); } diff --git a/src/pman.rs b/src/pman.rs index fdd374f..9565d28 100644 --- a/src/pman.rs +++ b/src/pman.rs @@ -1,16 +1,36 @@ -/* - * SPDX-FileCopyrightText: 2025 April Faye John & Contributors +/* + * SPDX-FileCopyrightText: 2025 April Faye John & Contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ -use clap::Command; +use crate::PMAN_SENDER; +use anyhow::{Ok, Result}; +use tokio::process::{Child, Command}; +use std::sync::{Arc, Mutex}; +use log::{debug, info}; +use tokio::sync::mpsc;use tokio::sync::mpsc::Receiver; -pub fn init_process_manager() { - let ProcessManager = ProcessManager::default(); +pub fn init_process_manager() -> Result<()> { + let (tx, mut rx) = mpsc::channel::(32); + let mut process_manager = ProcessManager::default(); + let _ = PMAN_SENDER.set(tx); + debug!("[pman-main] Spawning pman task"); + + tokio::spawn(async move { + info!("[pman-task] Hello from Pman"); + while process_manager.lifecycle(&mut rx).await.is_ok() { + let result = process_manager.alive_check().await; + if let Err(e) = result { + println!("{}", e); + } + } + }); + Ok(()) } +#[derive(Debug)] pub struct ProcessManager { - processes: Vec + processes: Vec, } impl Default for ProcessManager { @@ -21,23 +41,51 @@ impl Default for ProcessManager { } } +#[derive(Debug)] +pub enum ProcessCommand { + SpawnShellCmd { cmd: String }, + Shutdown, +} + impl ProcessManager { - fn spawn_shell_command(self, command: String) { + fn spawn_shell_command(&mut self, command: String) { if cfg!(target_os = "windows") { return self.spawn_cmd_command(command); - } else if cfg!(target_os = "macos") { + } else if cfg!(target_os = "macos") || cfg!(target_os = "ios") { + // TODO: reasearch if ios also has zsh return self.spawn_zsh_command(command); } else { return self.spawn_bash_command(command); } } - fn spawn_bash_command(self, command: String) { - + fn spawn_bash_command(&mut self, command: String) { + let cmd = Command::new("bash") + .arg("-c") + .arg(command) + .spawn().unwrap(); + self.processes.push(cmd); } - fn spawn_cmd_command(self, command: String) { - + fn spawn_cmd_command(&mut self, command: String) {} + fn spawn_zsh_command(&mut self, command: String) {} + pub async fn lifecycle(&mut self, rx: &mut Receiver) -> Result<()> { + let opt = rx.recv().await; + if let Some(message) = opt { + match message { + ProcessCommand::SpawnShellCmd { cmd } => {} + ProcessCommand::Shutdown => { + info!("Close command"); + anyhow::bail!("Close command"); + } + } + } else { + anyhow::bail!("Channel closed"); + } + Ok(()) } - fn spawn_zsh_command(self, command: String) { - + async fn alive_check(&mut self) -> Result<()> { + for process in &mut self.processes { + process.stdout + } + Ok(()) } } \ No newline at end of file