summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs17
-rw-r--r--src/trace.rs17
2 files changed, 22 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs
index 893b9f5..0694aa5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,7 +2,8 @@ mod trace;
use crate::trace::{exec, trace};
use clap::Parser;
-use std::process::Command;
+use fork::{fork, Fork};
+use nix::unistd::Pid;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
@@ -12,15 +13,13 @@ struct Args {
fn main() -> anyhow::Result<()> {
let args = Args::parse();
- let params = args.command.split(' ').collect::<Vec<&str>>();
- let mut command = Command::new(params[0]);
- if params.len() > 1 {
- for arg in &params[1..] {
- command.arg(arg);
- }
- }
- let pid = exec(&mut command)?;
+ let pid = match fork() {
+ Ok(Fork::Child) => return exec(&args.command),
+ Ok(Fork::Parent(child)) => Pid::from_raw(child as i32),
+ Err(err) => panic!("fork() failed: {err}"),
+ };
+
trace(pid)?;
Ok(())
diff --git a/src/trace.rs b/src/trace.rs
index 0031fcd..b00b9f9 100644
--- a/src/trace.rs
+++ b/src/trace.rs
@@ -8,16 +8,27 @@ use nix::{
};
use std::{os::unix::process::CommandExt, process::Command};
-pub fn exec(command: &mut Command) -> anyhow::Result<Pid> {
+pub fn exec(command: &String) -> anyhow::Result<()> {
+ let params: Vec<&str> = command.split(' ').collect();
+
+ let mut command = Command::new(params[0]);
+ command.args(params[1..].iter());
+
unsafe {
command.pre_exec(|| ptrace::traceme().map_err(|e| e.into()));
}
- let child = command.spawn()?;
- Ok(Pid::from_raw(child.id() as i32))
+
+ command.exec();
+
+ Ok(())
}
pub fn trace(pid: Pid) -> anyhow::Result<()> {
let mut have_to_print = true;
+
+ // First wait if for the parent process
+ _ = waitpid(pid, None)?;
+
loop {
have_to_print ^= true;
ptrace::syscall(pid, None)?;