From 9a17a4d200b33f66a6d61dc06a9df7ddef01d735 Mon Sep 17 00:00:00 2001 From: Markus Jais Date: Sun, 1 Feb 2015 16:43:36 +0100 Subject: Changed options parameter for waitpid to Option --- src/sys/wait.rs | 9 +++++++-- tests/unistd.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/sys/wait.rs b/src/sys/wait.rs index c49765fd..e2273d2f 100644 --- a/src/sys/wait.rs +++ b/src/sys/wait.rs @@ -21,12 +21,17 @@ pub enum WaitStatus { StillAlive } -pub fn waitpid(pid: pid_t, options: WaitPidFlag) -> SysResult { +pub fn waitpid(pid: pid_t, options: Option) -> SysResult { use self::WaitStatus::*; let mut status: i32 = 0; - let res = unsafe { ffi::waitpid(pid as pid_t, &mut status as *mut c_int, options.bits()) }; + let option_bits = match options { + Some(bits) => bits.bits(), + None => 0 + }; + + let res = unsafe { ffi::waitpid(pid as pid_t, &mut status as *mut c_int, option_bits) }; if res < 0 { Err(SysError::last()) diff --git a/tests/unistd.rs b/tests/unistd.rs index 6ea9b149..254b6887 100644 --- a/tests/unistd.rs +++ b/tests/unistd.rs @@ -9,6 +9,10 @@ mod test { use std::iter::repeat; use std::rand::{thread_rng, Rng}; + use nix::unistd::{fork}; + use nix::sys::wait::{waitpid, WaitStatus}; + use nix::unistd::Fork::{Parent, Child}; + #[test] fn test_writev() { let mut to_write = Vec::with_capacity(16 * 128); @@ -98,4 +102,29 @@ mod test { } + #[test] + fn test_fork_and_waitpid() { + let pid = fork(); + match pid { + Ok(Child) => {} // ignore child here + Ok(Parent(child_pid)) => { + // assert that child was created and pid > 0 + assert!(child_pid > 0); + let wait_status = waitpid(child_pid, None); + match wait_status { + // assert that waitpid returned correct status and the pid is the one of the child + Ok(WaitStatus::Exited(pid_t)) => assert!(pid_t == child_pid), + + // panic, must never happen + Ok(WaitStatus::StillAlive) => panic!("Child still alive, should never happen"), + + // panic, waitpid should never fail + Err(_) => panic!("Error: waitpid Failed") + } + + }, + // panic, fork should never fail unless there is a serious problem with the OS + Err(_) => panic!("Error: Fork Failed") + } + } } -- cgit v1.2.3