Skip to content

Commit

Permalink
Avoid panics in main
Browse files Browse the repository at this point in the history
  • Loading branch information
boozec committed Oct 28, 2023
1 parent 529b6a5 commit 5c3c503
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sigma-trace"
version = "0.0.1-c"
version = "0.0.2"
edition = "2021"
authors = ["Santo Cariotti <[email protected]>"]
repository = "https://github.com/boozec/sigma"
Expand Down
32 changes: 20 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit 5c3c503

Please sign in to comment.