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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
use libc::_exit;
use nix::errno::Errno;
use nix::sys::signal::*;
use nix::sys::wait::*;
use nix::unistd::ForkResult::*;
use nix::unistd::*;
#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn test_wait_signal() {
let _m = crate::FORK_MTX.lock();
// Safe: The child only calls `pause` and/or `_exit`, which are async-signal-safe.
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => {
pause();
unsafe { _exit(123) }
}
Parent { child } => {
kill(child, Some(SIGKILL)).expect("Error: Kill Failed");
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::Signaled(child, SIGKILL, false))
);
}
}
}
#[test]
#[cfg(any(
target_os = "android",
target_os = "freebsd",
//target_os = "haiku",
all(target_os = "linux", not(target_env = "uclibc")),
))]
#[cfg(not(any(
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
target_arch = "mips64r6"
)))]
fn test_waitid_signal() {
let _m = crate::FORK_MTX.lock();
// Safe: The child only calls `pause` and/or `_exit`, which are async-signal-safe.
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => {
pause();
unsafe { _exit(123) }
}
Parent { child } => {
kill(child, Some(SIGKILL)).expect("Error: Kill Failed");
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::Signaled(child, SIGKILL, false)),
);
}
}
}
#[test]
fn test_wait_exit() {
let _m = crate::FORK_MTX.lock();
// Safe: Child only calls `_exit`, which is async-signal-safe.
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => unsafe {
_exit(12);
},
Parent { child } => {
assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 12)));
}
}
}
#[cfg(not(target_os = "haiku"))]
#[test]
#[cfg(any(
target_os = "android",
target_os = "freebsd",
target_os = "haiku",
all(target_os = "linux", not(target_env = "uclibc")),
))]
#[cfg(not(any(
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
target_arch = "mips64r6"
)))]
fn test_waitid_exit() {
let _m = crate::FORK_MTX.lock();
// Safe: Child only calls `_exit`, which is async-signal-safe.
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => unsafe {
_exit(12);
},
Parent { child } => {
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::Exited(child, 12)),
);
}
}
}
#[test]
fn test_waitstatus_from_raw() {
let pid = Pid::from_raw(1);
assert_eq!(
WaitStatus::from_raw(pid, 0x0002),
Ok(WaitStatus::Signaled(pid, Signal::SIGINT, false))
);
assert_eq!(
WaitStatus::from_raw(pid, 0x0200),
Ok(WaitStatus::Exited(pid, 2))
);
assert_eq!(WaitStatus::from_raw(pid, 0x7f7f), Err(Errno::EINVAL));
}
#[test]
fn test_waitstatus_pid() {
let _m = crate::FORK_MTX.lock();
match unsafe { fork() }.unwrap() {
Child => unsafe { _exit(0) },
Parent { child } => {
let status = waitpid(child, None).unwrap();
assert_eq!(status.pid(), Some(child));
}
}
}
#[test]
#[cfg(any(
target_os = "android",
target_os = "freebsd",
target_os = "haiku",
all(target_os = "linux", not(target_env = "uclibc")),
))]
fn test_waitid_pid() {
let _m = crate::FORK_MTX.lock();
match unsafe { fork() }.unwrap() {
Child => unsafe { _exit(0) },
Parent { child } => {
let status = waitid(Id::Pid(child), WaitPidFlag::WEXITED).unwrap();
assert_eq!(status.pid(), Some(child));
}
}
}
#[cfg(linux_android)]
// FIXME: qemu-user doesn't implement ptrace on most arches
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
mod ptrace {
use crate::*;
use libc::_exit;
use nix::sys::ptrace::{self, Event, Options};
use nix::sys::signal::*;
use nix::sys::wait::*;
use nix::unistd::ForkResult::*;
use nix::unistd::*;
fn ptrace_child() -> ! {
ptrace::traceme().unwrap();
// As recommended by ptrace(2), raise SIGTRAP to pause the child
// until the parent is ready to continue
raise(SIGTRAP).unwrap();
unsafe { _exit(0) }
}
fn ptrace_wait_parent(child: Pid) {
// Wait for the raised SIGTRAP
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::Stopped(child, SIGTRAP))
);
// We want to test a syscall stop and a PTRACE_EVENT stop
ptrace::setoptions(
child,
Options::PTRACE_O_TRACESYSGOOD | Options::PTRACE_O_TRACEEXIT,
)
.expect("setoptions failed");
// First, stop on the next system call, which will be exit()
ptrace::syscall(child, None).expect("syscall failed");
assert_eq!(waitpid(child, None), Ok(WaitStatus::PtraceSyscall(child)));
// Then get the ptrace event for the process exiting
ptrace::cont(child, None).expect("cont failed");
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::PtraceEvent(
child,
SIGTRAP,
Event::PTRACE_EVENT_EXIT as i32
))
);
// Finally get the normal wait() result, now that the process has exited
ptrace::cont(child, None).expect("cont failed");
assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 0)));
}
#[cfg(not(target_env = "uclibc"))]
fn ptrace_waitid_parent(child: Pid) {
// Wait for the raised SIGTRAP
//
// Unlike waitpid(), waitid() can distinguish trap events from regular
// stop events, so unlike ptrace_wait_parent(), we get a PtraceEvent here
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::PtraceEvent(child, SIGTRAP, 0)),
);
// We want to test a syscall stop and a PTRACE_EVENT stop
ptrace::setoptions(
child,
Options::PTRACE_O_TRACESYSGOOD | Options::PTRACE_O_TRACEEXIT,
)
.expect("setopts failed");
// First, stop on the next system call, which will be exit()
ptrace::syscall(child, None).expect("syscall failed");
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::PtraceSyscall(child)),
);
// Then get the ptrace event for the process exiting
ptrace::cont(child, None).expect("cont failed");
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::PtraceEvent(
child,
SIGTRAP,
Event::PTRACE_EVENT_EXIT as i32
)),
);
// Finally get the normal wait() result, now that the process has exited
ptrace::cont(child, None).expect("cont failed");
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::Exited(child, 0)),
);
}
#[test]
fn test_wait_ptrace() {
require_capability!("test_wait_ptrace", CAP_SYS_PTRACE);
let _m = crate::FORK_MTX.lock();
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => ptrace_child(),
Parent { child } => ptrace_wait_parent(child),
}
}
#[test]
#[cfg(not(target_env = "uclibc"))]
fn test_waitid_ptrace() {
require_capability!("test_waitid_ptrace", CAP_SYS_PTRACE);
let _m = crate::FORK_MTX.lock();
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => ptrace_child(),
Parent { child } => ptrace_waitid_parent(child),
}
}
}
|