1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#![cfg(unix)]
#[cfg(doctest)]
doc_comment::doctest!("../README.md");
use nix::sys::ptrace;
use nix::sys::signal::Signal;
use nix::sys::wait::{waitpid, WaitStatus};
use nix::unistd::Pid;
use std::io::{self, Result};
use std::os::unix::process::CommandExt;
use std::process::{Child, Command};
pub trait CommandPtraceSpawn {
fn spawn_ptrace(&mut self) -> Result<Child>;
}
impl CommandPtraceSpawn for Command {
fn spawn_ptrace(&mut self) -> Result<Child> {
let child = unsafe {
self.pre_exec(|| {
ptrace::traceme().map_err(|e| match e {
nix::Error::Sys(e) => io::Error::from_raw_os_error(e as i32),
_ => io::Error::new(io::ErrorKind::Other, "unknown PTRACE_TRACEME error"),
})
})
.spawn()?
};
match waitpid(Some(Pid::from_raw(child.id() as i32)), None) {
Ok(WaitStatus::Stopped(_, Signal::SIGTRAP)) => Ok(child),
_ => Err(io::Error::new(
io::ErrorKind::Other,
"Child state not correct",
)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::env;
use std::path::PathBuf;
fn test_process_path() -> Option<PathBuf> {
env::current_exe().ok().and_then(|p| {
p.parent().map(|p| {
p.with_file_name("test")
.with_extension(env::consts::EXE_EXTENSION)
})
})
}
#[test]
fn test_spawn_ptrace() {
let path = test_process_path().expect("Failed to get test process path");
let child = Command::new(&path)
.spawn_ptrace()
.expect("Error spawning test process");
let pid = Pid::from_raw(child.id() as i32);
ptrace::cont(pid, None).expect("Error continuing child process");
match waitpid(pid, None) {
Ok(WaitStatus::Exited(_, code)) => assert_eq!(code, 0),
Ok(s) => panic!("Unexpected stop status: {:?}", s),
Err(e) => panic!("Unexpected waitpid error: {:?}", e),
}
}
}