about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-10-29 13:45:56 -0700
committerAlex Crichton <alex@alexcrichton.com>2015-11-06 14:40:43 -0800
commit94aee5b7e6d1eda4c872133787ba238cbe29ecb4 (patch)
tree66464de207d82d7883827d66ea9b3a02a349b5c0 /src
parent01fd4d622746f03334a6543c1476e5e65712b1cc (diff)
downloadrust-94aee5b7e6d1eda4c872133787ba238cbe29ecb4.tar.gz
rust-94aee5b7e6d1eda4c872133787ba238cbe29ecb4.zip
std: Refactor process exit code handling slightly
* Store the native representation directly in the `ExitStatus` structure instead
  of a "parsed version" (mostly for Unix).
* On Windows, be more robust against processes exiting with the status of 259.
  Unfortunately this exit code corresponds to `STILL_ACTIVE`, causing libstd to
  think the process was still alive, causing an infinite loop. Instead the loop
  is removed altogether and `WaitForSingleObject` is used to wait for the
  process to exit.
Diffstat (limited to 'src')
-rw-r--r--src/libstd/sys/unix/ext/process.rs5
-rw-r--r--src/libstd/sys/unix/process.rs95
-rw-r--r--src/libstd/sys/windows/process.rs21
-rw-r--r--src/test/run-pass/weird-exit-code.rs37
4 files changed, 94 insertions, 64 deletions
diff --git a/src/libstd/sys/unix/ext/process.rs b/src/libstd/sys/unix/ext/process.rs
index dcfa376c81e..3e7260f0757 100644
--- a/src/libstd/sys/unix/ext/process.rs
+++ b/src/libstd/sys/unix/ext/process.rs
@@ -75,10 +75,7 @@ pub trait ExitStatusExt {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl ExitStatusExt for process::ExitStatus {
     fn signal(&self) -> Option<i32> {
-        match *self.as_inner() {
-            sys::process::ExitStatus::Signal(s) => Some(s),
-            _ => None
-        }
+        self.as_inner().signal()
     }
 }
 
diff --git a/src/libstd/sys/unix/process.rs b/src/libstd/sys/unix/process.rs
index 75ae9310b21..7e37238c23b 100644
--- a/src/libstd/sys/unix/process.rs
+++ b/src/libstd/sys/unix/process.rs
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![allow(non_snake_case)]
+
 use prelude::v1::*;
 use os::unix::prelude::*;
 
@@ -84,33 +86,62 @@ impl Command {
 
 /// Unix exit statuses
 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
-pub enum ExitStatus {
-    /// Normal termination with an exit code.
-    Code(i32),
-
-    /// Termination by signal, with the signal number.
-    ///
-    /// Never generated on Windows.
-    Signal(i32),
+pub struct ExitStatus(c_int);
+
+#[cfg(any(target_os = "linux", target_os = "android",
+          target_os = "nacl"))]
+mod status_imp {
+    pub fn WIFEXITED(status: i32) -> bool { (status & 0xff) == 0 }
+    pub fn WEXITSTATUS(status: i32) -> i32 { (status >> 8) & 0xff }
+    pub fn WTERMSIG(status: i32) -> i32 { status & 0x7f }
+}
+
+#[cfg(any(target_os = "macos",
+          target_os = "ios",
+          target_os = "freebsd",
+          target_os = "dragonfly",
+          target_os = "bitrig",
+          target_os = "netbsd",
+          target_os = "openbsd"))]
+mod status_imp {
+    pub fn WIFEXITED(status: i32) -> bool { (status & 0x7f) == 0 }
+    pub fn WEXITSTATUS(status: i32) -> i32 { status >> 8 }
+    pub fn WTERMSIG(status: i32) -> i32 { status & 0o177 }
 }
 
 impl ExitStatus {
+    fn exited(&self) -> bool {
+        status_imp::WIFEXITED(self.0)
+    }
+
     pub fn success(&self) -> bool {
-        *self == ExitStatus::Code(0)
+        self.code() == Some(0)
     }
+
     pub fn code(&self) -> Option<i32> {
-        match *self {
-            ExitStatus::Code(c) => Some(c),
-            _ => None
+        if self.exited() {
+            Some(status_imp::WEXITSTATUS(self.0))
+        } else {
+            None
+        }
+    }
+
+    pub fn signal(&self) -> Option<i32> {
+        if !self.exited() {
+            Some(status_imp::WTERMSIG(self.0))
+        } else {
+            None
         }
     }
 }
 
 impl fmt::Display for ExitStatus {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        match *self {
-            ExitStatus::Code(code) =>  write!(f, "exit code: {}", code),
-            ExitStatus::Signal(code) =>  write!(f, "signal: {}", code),
+        if let Some(code) = self.code() {
+            write!(f, "exit code: {}", code)
+        } else {
+            let signal = self.signal().unwrap();
+            write!(f, "signal: {}", signal)
         }
     }
 }
@@ -351,7 +382,7 @@ impl Process {
     pub fn wait(&self) -> io::Result<ExitStatus> {
         let mut status = 0 as c_int;
         try!(cvt_r(|| unsafe { c::waitpid(self.pid, &mut status, 0) }));
-        Ok(translate_status(status))
+        Ok(ExitStatus(status))
     }
 
     pub fn try_wait(&self) -> Option<ExitStatus> {
@@ -360,7 +391,7 @@ impl Process {
             c::waitpid(self.pid, &mut status, c::WNOHANG)
         }) {
             Ok(0) => None,
-            Ok(n) if n == self.pid => Some(translate_status(status)),
+            Ok(n) if n == self.pid => Some(ExitStatus(status)),
             Ok(n) => panic!("unknown pid: {}", n),
             Err(e) => panic!("unknown waitpid error: {}", e),
         }
@@ -418,36 +449,6 @@ fn make_envp(env: Option<&HashMap<OsString, OsString>>)
     }
 }
 
-fn translate_status(status: c_int) -> ExitStatus {
-    #![allow(non_snake_case)]
-    #[cfg(any(target_os = "linux", target_os = "android",
-              target_os = "nacl"))]
-    mod imp {
-        pub fn WIFEXITED(status: i32) -> bool { (status & 0xff) == 0 }
-        pub fn WEXITSTATUS(status: i32) -> i32 { (status >> 8) & 0xff }
-        pub fn WTERMSIG(status: i32) -> i32 { status & 0x7f }
-    }
-
-    #[cfg(any(target_os = "macos",
-              target_os = "ios",
-              target_os = "freebsd",
-              target_os = "dragonfly",
-              target_os = "bitrig",
-              target_os = "netbsd",
-              target_os = "openbsd"))]
-    mod imp {
-        pub fn WIFEXITED(status: i32) -> bool { (status & 0x7f) == 0 }
-        pub fn WEXITSTATUS(status: i32) -> i32 { status >> 8 }
-        pub fn WTERMSIG(status: i32) -> i32 { status & 0o177 }
-    }
-
-    if imp::WIFEXITED(status) {
-        ExitStatus::Code(imp::WEXITSTATUS(status))
-    } else {
-        ExitStatus::Signal(imp::WTERMSIG(status))
-    }
-}
-
 #[cfg(test)]
 mod tests {
     use super::*;
diff --git a/src/libstd/sys/windows/process.rs b/src/libstd/sys/windows/process.rs
index ca33e11eea0..e8cc160fde7 100644
--- a/src/libstd/sys/windows/process.rs
+++ b/src/libstd/sys/windows/process.rs
@@ -201,21 +201,16 @@ impl Process {
     }
 
     pub fn wait(&self) -> io::Result<ExitStatus> {
-        use libc::{STILL_ACTIVE, INFINITE, WAIT_OBJECT_0};
+        use libc::{INFINITE, WAIT_OBJECT_0};
         use libc::{GetExitCodeProcess, WaitForSingleObject};
 
         unsafe {
-            loop {
-                let mut status = 0;
-                try!(cvt(GetExitCodeProcess(self.handle.raw(), &mut status)));
-                if status != STILL_ACTIVE {
-                    return Ok(ExitStatus(status as i32));
-                }
-                match WaitForSingleObject(self.handle.raw(), INFINITE) {
-                    WAIT_OBJECT_0 => {}
-                    _ => return Err(Error::last_os_error()),
-                }
+            if WaitForSingleObject(self.handle.raw(), INFINITE) != WAIT_OBJECT_0 {
+                return Err(Error::last_os_error())
             }
+            let mut status = 0;
+            try!(cvt(GetExitCodeProcess(self.handle.raw(), &mut status)));
+            Ok(ExitStatus(status))
         }
     }
 
@@ -225,14 +220,14 @@ impl Process {
 }
 
 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
-pub struct ExitStatus(i32);
+pub struct ExitStatus(libc::DWORD);
 
 impl ExitStatus {
     pub fn success(&self) -> bool {
         self.0 == 0
     }
     pub fn code(&self) -> Option<i32> {
-        Some(self.0)
+        Some(self.0 as i32)
     }
 }
 
diff --git a/src/test/run-pass/weird-exit-code.rs b/src/test/run-pass/weird-exit-code.rs
new file mode 100644
index 00000000000..27c7f9bef15
--- /dev/null
+++ b/src/test/run-pass/weird-exit-code.rs
@@ -0,0 +1,37 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// On Windows the GetExitCodeProcess API is used to get the exit code of a
+// process, but it's easy to mistake a process exiting with the code 259 as
+// "still running" because this is the value of the STILL_ACTIVE constant. Make
+// sure we handle this case in the standard library and correctly report the
+// status.
+//
+// Note that this is disabled on unix as processes exiting with 259 will have
+// their exit status truncated to 3 (only the lower 8 bits are used).
+
+use std::process::{self, Command};
+use std::env;
+
+fn main() {
+    if !cfg!(windows) {
+        return
+    }
+
+    if env::args().len() == 1 {
+        let status = Command::new(env::current_exe().unwrap())
+                             .arg("foo")
+                             .status()
+                             .unwrap();
+        assert_eq!(status.code(), Some(259));
+    } else {
+        process::exit(259);
+    }
+}