diff options
author | Santo Cariotti <santo@dcariotti.me> | 2023-10-28 09:47:42 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2023-10-28 09:47:42 +0200 |
commit | 5c3c5033b36986641603253400e86850dc60c5e2 (patch) | |
tree | 1b3e1631ca357f15dbb6d8e2ce5074422a4e6c17 | |
parent | 529b6a5ddaada8551f45cfe10076a6a59e93f097 (diff) |
Avoid panics in main
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/main.rs | 32 |
3 files changed, 22 insertions, 14 deletions
@@ -434,7 +434,7 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "sigma-trace" -version = "0.0.1-c" +version = "0.0.2" dependencies = [ "anyhow", "byteorder", @@ -1,6 +1,6 @@ [package] name = "sigma-trace" -version = "0.0.1-c" +version = "0.0.2" edition = "2021" authors = ["Santo Cariotti <santo@dcariotti.me>"] repository = "https://github.com/boozec/sigma" diff --git a/src/main.rs b/src/main.rs index f03c271..84ba250 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,37 +11,45 @@ use crate::ui::UI; use clap::Parser; use fork::{fork, Fork}; use nix::unistd::Pid; +use owo_colors::OwoColorize; use trace::attach; /// Create a fork of the program and execute the process in the child. Parent gets the pid /// value and trace it. fn main() -> anyhow::Result<()> { let args = Args::parse(); - let pid = if args.command.is_some() { + let process: Result<Pid, String> = if args.command.is_some() { match fork() { Ok(Fork::Child) => return exec(&args.command.unwrap()), - Ok(Fork::Parent(child)) => Pid::from_raw(child), - Err(err) => panic!("fork() failed: {err}"), + Ok(Fork::Parent(child)) => Ok(Pid::from_raw(child)), + Err(err) => Err(format!("fork() failed: {err}")), } } else if args.attach.is_some() { let pid = Pid::from_raw(args.attach.unwrap()); if attach(pid).is_ok() { - pid + Ok(pid) } else { - panic!("Unable to attach to process {pid}"); + Err(format!("Unable to attach to process `{pid}`")) } } else { - panic!("You must define a command or a PID to attach"); + Err(format!("You must define a command or a PID to attach")) }; - if !args.no_tui { - let mut ui = UI::new(); + match process { + Ok(pid) => { + if !args.no_tui { + let mut ui = UI::new(); - ui.start(pid, &args)?; - } else { - trace(pid, &args)?; - } + ui.start(pid, &args)?; + } else { + trace(pid, &args)?; + } + } + Err(e) => { + eprintln!("{}", e.red()); + } + }; Ok(()) } |