summaryrefslogtreecommitdiff
path: root/test/sys/test_wait.rs
diff options
context:
space:
mode:
Diffstat (limited to 'test/sys/test_wait.rs')
-rw-r--r--test/sys/test_wait.rs137
1 files changed, 86 insertions, 51 deletions
diff --git a/test/sys/test_wait.rs b/test/sys/test_wait.rs
index 058573a1..1a4a0f87 100644
--- a/test/sys/test_wait.rs
+++ b/test/sys/test_wait.rs
@@ -1,9 +1,9 @@
+use libc::_exit;
use nix::errno::Errno;
-use nix::unistd::*;
-use nix::unistd::ForkResult::*;
use nix::sys::signal::*;
use nix::sys::wait::*;
-use libc::_exit;
+use nix::unistd::ForkResult::*;
+use nix::unistd::*;
#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
@@ -11,15 +11,18 @@ 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)));
- },
+ 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))
+ );
+ }
}
}
@@ -35,18 +38,18 @@ 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)),
- );
- },
+ 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)),
+ );
+ }
}
}
@@ -55,11 +58,13 @@ 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)));
- },
+ match unsafe { fork() }.expect("Error: Fork Failed") {
+ Child => unsafe {
+ _exit(12);
+ },
+ Parent { child } => {
+ assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 12)));
+ }
}
}
@@ -76,22 +81,30 @@ 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)),
- );
- }
+ 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, 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));
}
@@ -99,7 +112,7 @@ fn test_waitstatus_from_raw() {
fn test_waitstatus_pid() {
let _m = crate::FORK_MTX.lock();
- match unsafe{fork()}.unwrap() {
+ match unsafe { fork() }.unwrap() {
Child => unsafe { _exit(0) },
Parent { child } => {
let status = waitpid(child, None).unwrap();
@@ -131,13 +144,13 @@ fn test_waitid_pid() {
// FIXME: qemu-user doesn't implement ptrace on most arches
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
mod ptrace {
- use nix::sys::ptrace::{self, Options, Event};
+ use crate::*;
+ use libc::_exit;
+ use nix::sys::ptrace::{self, Event, Options};
use nix::sys::signal::*;
use nix::sys::wait::*;
- use nix::unistd::*;
use nix::unistd::ForkResult::*;
- use libc::_exit;
- use crate::*;
+ use nix::unistd::*;
fn ptrace_child() -> ! {
ptrace::traceme().unwrap();
@@ -149,16 +162,30 @@ mod ptrace {
fn ptrace_wait_parent(child: Pid) {
// Wait for the raised SIGTRAP
- assert_eq!(waitpid(child, None), Ok(WaitStatus::Stopped(child, SIGTRAP)));
+ assert_eq!(
+ waitpid(child, None),
+ Ok(WaitStatus::Stopped(child, SIGTRAP))
+ );
// We want to test a syscall stop and a PTRACE_EVENT stop
- assert!(ptrace::setoptions(child, Options::PTRACE_O_TRACESYSGOOD | Options::PTRACE_O_TRACEEXIT).is_ok());
+ assert!(ptrace::setoptions(
+ child,
+ Options::PTRACE_O_TRACESYSGOOD | Options::PTRACE_O_TRACEEXIT
+ )
+ .is_ok());
// First, stop on the next system call, which will be exit()
assert!(ptrace::syscall(child, None).is_ok());
assert_eq!(waitpid(child, None), Ok(WaitStatus::PtraceSyscall(child)));
// Then get the ptrace event for the process exiting
assert!(ptrace::cont(child, None).is_ok());
- assert_eq!(waitpid(child, None), Ok(WaitStatus::PtraceEvent(child, SIGTRAP, Event::PTRACE_EVENT_EXIT as i32)));
+ 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
assert!(ptrace::cont(child, None).is_ok());
assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 0)));
@@ -175,7 +202,11 @@ mod ptrace {
Ok(WaitStatus::PtraceEvent(child, SIGTRAP, 0)),
);
// We want to test a syscall stop and a PTRACE_EVENT stop
- assert!(ptrace::setoptions(child, Options::PTRACE_O_TRACESYSGOOD | Options::PTRACE_O_TRACEEXIT).is_ok());
+ assert!(ptrace::setoptions(
+ child,
+ Options::PTRACE_O_TRACESYSGOOD | Options::PTRACE_O_TRACEEXIT
+ )
+ .is_ok());
// First, stop on the next system call, which will be exit()
assert!(ptrace::syscall(child, None).is_ok());
@@ -187,7 +218,11 @@ mod ptrace {
assert!(ptrace::cont(child, None).is_ok());
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
- Ok(WaitStatus::PtraceEvent(child, SIGTRAP, Event::PTRACE_EVENT_EXIT as i32)),
+ Ok(WaitStatus::PtraceEvent(
+ child,
+ SIGTRAP,
+ Event::PTRACE_EVENT_EXIT as i32
+ )),
);
// Finally get the normal wait() result, now that the process has exited
assert!(ptrace::cont(child, None).is_ok());
@@ -202,7 +237,7 @@ mod ptrace {
require_capability!("test_wait_ptrace", CAP_SYS_PTRACE);
let _m = crate::FORK_MTX.lock();
- match unsafe{fork()}.expect("Error: Fork Failed") {
+ match unsafe { fork() }.expect("Error: Fork Failed") {
Child => ptrace_child(),
Parent { child } => ptrace_wait_parent(child),
}
@@ -214,7 +249,7 @@ mod ptrace {
require_capability!("test_waitid_ptrace", CAP_SYS_PTRACE);
let _m = crate::FORK_MTX.lock();
- match unsafe{fork()}.expect("Error: Fork Failed") {
+ match unsafe { fork() }.expect("Error: Fork Failed") {
Child => ptrace_child(),
Parent { child } => ptrace_waitid_parent(child),
}