Compare commits

...

2 commits

Author SHA1 Message Date
aprilthepink f2ec93e0f2 execute shell cmd 2025-02-22 00:29:35 +01:00
aprilthepink a1b79e742f add cli spawning 2025-02-20 22:18:14 +01:00
4 changed files with 210 additions and 30 deletions

104
Cargo.lock generated
View file

@ -110,6 +110,15 @@ dependencies = [
"winit", "winit",
] ]
[[package]]
name = "addr2line"
version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1"
dependencies = [
"gimli",
]
[[package]] [[package]]
name = "adler2" name = "adler2"
version = "2.0.0" version = "2.0.0"
@ -224,6 +233,12 @@ dependencies = [
"windows-sys 0.59.0", "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]] [[package]]
name = "arboard" name = "arboard"
version = "3.4.1" version = "3.4.1"
@ -475,6 +490,21 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" 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]] [[package]]
name = "base64" name = "base64"
version = "0.21.7" version = "0.21.7"
@ -1451,6 +1481,12 @@ dependencies = [
"weezl", "weezl",
] ]
[[package]]
name = "gimli"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
[[package]] [[package]]
name = "git2" name = "git2"
version = "0.20.0" version = "0.20.0"
@ -1596,13 +1632,16 @@ dependencies = [
name = "hai" name = "hai"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"bunt", "bunt",
"clap", "clap",
"eframe", "eframe",
"egui", "egui",
"egui_extras", "egui_extras",
"env_logger", "env_logger",
"log",
"shadow-rs", "shadow-rs",
"tokio",
] ]
[[package]] [[package]]
@ -2125,6 +2164,17 @@ dependencies = [
"simd-adler32", "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]] [[package]]
name = "naga" name = "naga"
version = "24.0.0" version = "24.0.0"
@ -2462,6 +2512,15 @@ dependencies = [
"objc2-foundation", "objc2-foundation",
] ]
[[package]]
name = "object"
version = "0.36.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.20.3" version = "1.20.3"
@ -2841,6 +2900,12 @@ version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cd14fd5e3b777a7422cca79358c57a8f6e3a703d9ac187448d0daf220c2407f" checksum = "3cd14fd5e3b777a7422cca79358c57a8f6e3a703d9ac187448d0daf220c2407f"
[[package]]
name = "rustc-demangle"
version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
[[package]] [[package]]
name = "rustc-hash" name = "rustc-hash"
version = "1.1.0" version = "1.1.0"
@ -3091,6 +3156,16 @@ dependencies = [
"serde", "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]] [[package]]
name = "spin" name = "spin"
version = "0.9.8" version = "0.9.8"
@ -3336,6 +3411,35 @@ dependencies = [
"zerovec", "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]] [[package]]
name = "toml_datetime" name = "toml_datetime"
version = "0.6.8" version = "0.6.8"

View file

@ -7,13 +7,16 @@ publish = false
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
[dependencies] [dependencies]
anyhow = "1.0.96"
bunt = "0.2.8" bunt = "0.2.8"
clap = { version = "4.5.30", features = ["derive"] } clap = { version = "4.5.30", features = ["derive"] }
eframe = { version = "0.31", features = ["default", "__screenshot"] } eframe = { version = "0.31", features = ["default", "__screenshot"] }
egui = "0.31.0" egui = "0.31.0"
egui_extras = { version = "0.31", features = ["default", "image", "all_loaders"] } egui_extras = { version = "0.31", features = ["default", "image", "all_loaders"] }
env_logger = "0.11.6" env_logger = "0.11.6"
log = "0.4.25"
shadow-rs = "0.38" shadow-rs = "0.38"
tokio = { version = "1.43", features = ["full"] }
[build-dependencies] [build-dependencies]
shadow-rs = "0.38" shadow-rs = "0.38"

View file

@ -5,16 +5,22 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
mod gui;
mod bininfo; mod bininfo;
mod gui;
mod pman; mod pman;
use clap::{Subcommand, Parser}; use bunt::println;
use pman::init_process_manager; use clap::{Parser, Subcommand};
use log::info;
use pman::{init_process_manager, ProcessCommand, ProcessManager};
use shadow_rs::shadow; use shadow_rs::shadow;
use std::sync::{Arc, Mutex, OnceLock};
use tokio::{signal, sync::mpsc::Sender};
shadow!(build); shadow!(build);
static PMAN_SENDER: OnceLock<Sender<ProcessCommand>> = OnceLock::new();
/// Simple program to greet a person /// Simple program to greet a person
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
@ -39,30 +45,26 @@ enum Commands {
remote: String, remote: String,
}, },
#[command(about = "List compile time backed info to audit binary")] #[command(about = "List compile time backed info to audit binary")]
Shadow { Shadow { outfile: Option<String> },
outfile: Option<String>,
},
#[command(about = "Show test gui")] #[command(about = "Show test gui")]
Gui, Gui,
Devtest,
} }
#[tokio::main]
async fn main() {
fn main() { env_logger::init();
let args = Args::parse(); let args = Args::parse();
init_process_manager(); let _ = init_process_manager();
match args.command { match args.command {
Commands::Clone { remote } => {}
Commands::Clone { remote } => {
}
Commands::Shadow { outfile } => { Commands::Shadow { outfile } => {
bininfo::print_info(); bininfo::print_info();
return; return;
} }
Commands::Gui => { Commands::Gui => {
@ -70,7 +72,23 @@ fn main() {
if let Err(e) = res { if let Err(e) = res {
println!("{}", e); println!("{}", e);
} }
return;
} }
}; Commands::Devtest => {
PMAN_SENDER.get().unwrap().send(ProcessCommand::SpawnShellCmd {
cmd: "sleep 100 && echo hello meow".to_string(),
}).await.expect("TODO: panic message");
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");
} }

View file

@ -3,14 +3,34 @@
* SPDX-License-Identifier: AGPL-3.0-or-later * 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() { pub fn init_process_manager() -> Result<()> {
let ProcessManager = ProcessManager::default(); let (tx, mut rx) = mpsc::channel::<ProcessCommand>(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 { pub struct ProcessManager {
processes: Vec<Command> processes: Vec<Child>,
} }
impl Default for ProcessManager { impl Default for ProcessManager {
@ -21,23 +41,58 @@ impl Default for ProcessManager {
} }
} }
#[derive(Debug)]
pub enum ProcessCommand {
SpawnShellCmd { cmd: String },
Shutdown,
}
impl ProcessManager { impl ProcessManager {
fn spawn_shell_command(self, command: String) { fn spawn_shell_command(&mut self, command: String) {
if cfg!(target_os = "windows") { if cfg!(target_os = "windows") {
return self.spawn_cmd_command(command); 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); return self.spawn_zsh_command(command);
} else { } else {
return self.spawn_bash_command(command); return self.spawn_bash_command(command);
} }
} }
fn spawn_bash_command(self, command: String) { fn spawn_bash_command(&mut self, command: String) {
debug!("Spawning bash cmd {}", command);
let mut cmd = Command::new("bash")
.arg("-c")
.arg(command)
.spawn().unwrap();
let _ = tokio::spawn(async move {
cmd.wait().await;
});
// Doesnt make sense to list cmd commands
// 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<ProcessCommand>) -> Result<()> {
let opt = rx.recv().await;
if let Some(message) = opt {
match message {
ProcessCommand::SpawnShellCmd { cmd } => {
self.spawn_shell_command(cmd);
} }
fn spawn_zsh_command(self, command: String) { ProcessCommand::Shutdown => {
info!("Close command");
anyhow::bail!("Close command");
}
}
} else {
anyhow::bail!("Channel closed");
}
Ok(())
}
async fn alive_check(&mut self) -> Result<()> {
for process in &mut self.processes {
}
Ok(())
} }
} }