]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/process.rs
rollup merge of #22438: dotdash/iter_vec_loop
[rust.git] / src / libstd / sys / unix / process.rs
1 // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use prelude::v1::*;
12 use self::Req::*;
13
14 use collections::HashMap;
15 use collections::hash_map::Hasher;
16 use ffi::CString;
17 use hash::Hash;
18 use old_io::process::{ProcessExit, ExitStatus, ExitSignal};
19 use old_io::{self, IoResult, IoError, EndOfFile};
20 use libc::{self, pid_t, c_void, c_int};
21 use mem;
22 use os;
23 use old_path::BytesContainer;
24 use ptr;
25 use sync::mpsc::{channel, Sender, Receiver};
26 use sys::fs::FileDesc;
27 use sys::{self, retry, c, wouldblock, set_nonblocking, ms_to_timeval};
28 use sys_common::helper_thread::Helper;
29 use sys_common::{AsInner, mkerr_libc, timeout};
30
31 pub use sys_common::ProcessConfig;
32
33 helper_init! { static HELPER: Helper<Req> }
34
35 /// Unix-specific extensions to the Command builder
36 pub struct CommandExt {
37     uid: Option<u32>,
38     gid: Option<u32>,
39 }
40
41 /// The unique id of the process (this should never be negative).
42 pub struct Process {
43     pub pid: pid_t
44 }
45
46 enum Req {
47     NewChild(libc::pid_t, Sender<ProcessExit>, u64),
48 }
49
50 const CLOEXEC_MSG_FOOTER: &'static [u8] = b"NOEX";
51
52 impl Process {
53     pub fn id(&self) -> pid_t {
54         self.pid
55     }
56
57     pub unsafe fn kill(&self, signal: int) -> IoResult<()> {
58         Process::killpid(self.pid, signal)
59     }
60
61     pub unsafe fn killpid(pid: pid_t, signal: int) -> IoResult<()> {
62         let r = libc::funcs::posix88::signal::kill(pid, signal as c_int);
63         mkerr_libc(r)
64     }
65
66     pub fn spawn<K, V, C, P>(cfg: &C, in_fd: Option<P>,
67                               out_fd: Option<P>, err_fd: Option<P>)
68                               -> IoResult<Process>
69         where C: ProcessConfig<K, V>, P: AsInner<FileDesc>,
70               K: BytesContainer + Eq + Hash<Hasher>, V: BytesContainer
71     {
72         use libc::funcs::posix88::unistd::{fork, dup2, close, chdir, execvp};
73
74         mod rustrt {
75             extern {
76                 pub fn rust_unset_sigprocmask();
77             }
78         }
79
80         #[cfg(all(target_os = "android", target_arch = "aarch64"))]
81         unsafe fn getdtablesize() -> c_int {
82             libc::sysconf(libc::consts::os::sysconf::_SC_OPEN_MAX) as c_int
83         }
84         #[cfg(not(all(target_os = "android", target_arch = "aarch64")))]
85         unsafe fn getdtablesize() -> c_int {
86             libc::funcs::bsd44::getdtablesize()
87         }
88
89         unsafe fn set_cloexec(fd: c_int) {
90             let ret = c::ioctl(fd, c::FIOCLEX);
91             assert_eq!(ret, 0);
92         }
93
94         let dirp = cfg.cwd().map(|c| c.as_ptr()).unwrap_or(ptr::null());
95
96         // temporary until unboxed closures land
97         let cfg = unsafe {
98             mem::transmute::<&ProcessConfig<K,V>,&'static ProcessConfig<K,V>>(cfg)
99         };
100
101         with_envp(cfg.env(), move|envp: *const c_void| {
102             with_argv(cfg.program(), cfg.args(), move|argv: *const *const libc::c_char| unsafe {
103                 let (input, mut output) = try!(sys::os::pipe());
104
105                 // We may use this in the child, so perform allocations before the
106                 // fork
107                 let devnull = b"/dev/null\0";
108
109                 set_cloexec(output.fd());
110
111                 let pid = fork();
112                 if pid < 0 {
113                     return Err(super::last_error())
114                 } else if pid > 0 {
115                     #[inline]
116                     fn combine(arr: &[u8]) -> i32 {
117                         let a = arr[0] as u32;
118                         let b = arr[1] as u32;
119                         let c = arr[2] as u32;
120                         let d = arr[3] as u32;
121
122                         ((a << 24) | (b << 16) | (c << 8) | (d << 0)) as i32
123                     }
124
125                     let p = Process{ pid: pid };
126                     drop(output);
127                     let mut bytes = [0; 8];
128                     return match input.read(&mut bytes) {
129                         Ok(8) => {
130                             assert!(combine(CLOEXEC_MSG_FOOTER) == combine(&bytes[4.. 8]),
131                                 "Validation on the CLOEXEC pipe failed: {:?}", bytes);
132                             let errno = combine(&bytes[0.. 4]);
133                             assert!(p.wait(0).is_ok(), "wait(0) should either return Ok or panic");
134                             Err(super::decode_error(errno))
135                         }
136                         Err(ref e) if e.kind == EndOfFile => Ok(p),
137                         Err(e) => {
138                             assert!(p.wait(0).is_ok(), "wait(0) should either return Ok or panic");
139                             panic!("the CLOEXEC pipe failed: {:?}", e)
140                         },
141                         Ok(..) => { // pipe I/O up to PIPE_BUF bytes should be atomic
142                             assert!(p.wait(0).is_ok(), "wait(0) should either return Ok or panic");
143                             panic!("short read on the CLOEXEC pipe")
144                         }
145                     };
146                 }
147
148                 // And at this point we've reached a special time in the life of the
149                 // child. The child must now be considered hamstrung and unable to
150                 // do anything other than syscalls really. Consider the following
151                 // scenario:
152                 //
153                 //      1. Thread A of process 1 grabs the malloc() mutex
154                 //      2. Thread B of process 1 forks(), creating thread C
155                 //      3. Thread C of process 2 then attempts to malloc()
156                 //      4. The memory of process 2 is the same as the memory of
157                 //         process 1, so the mutex is locked.
158                 //
159                 // This situation looks a lot like deadlock, right? It turns out
160                 // that this is what pthread_atfork() takes care of, which is
161                 // presumably implemented across platforms. The first thing that
162                 // threads to *before* forking is to do things like grab the malloc
163                 // mutex, and then after the fork they unlock it.
164                 //
165                 // Despite this information, libnative's spawn has been witnessed to
166                 // deadlock on both OSX and FreeBSD. I'm not entirely sure why, but
167                 // all collected backtraces point at malloc/free traffic in the
168                 // child spawned process.
169                 //
170                 // For this reason, the block of code below should contain 0
171                 // invocations of either malloc of free (or their related friends).
172                 //
173                 // As an example of not having malloc/free traffic, we don't close
174                 // this file descriptor by dropping the FileDesc (which contains an
175                 // allocation). Instead we just close it manually. This will never
176                 // have the drop glue anyway because this code never returns (the
177                 // child will either exec() or invoke libc::exit)
178                 let _ = libc::close(input.fd());
179
180                 fn fail(output: &mut FileDesc) -> ! {
181                     let errno = sys::os::errno() as u32;
182                     let bytes = [
183                         (errno >> 24) as u8,
184                         (errno >> 16) as u8,
185                         (errno >>  8) as u8,
186                         (errno >>  0) as u8,
187                         CLOEXEC_MSG_FOOTER[0], CLOEXEC_MSG_FOOTER[1],
188                         CLOEXEC_MSG_FOOTER[2], CLOEXEC_MSG_FOOTER[3]
189                     ];
190                     // pipe I/O up to PIPE_BUF bytes should be atomic
191                     assert!(output.write(&bytes).is_ok());
192                     unsafe { libc::_exit(1) }
193                 }
194
195                 rustrt::rust_unset_sigprocmask();
196
197                 // If a stdio file descriptor is set to be ignored (via a -1 file
198                 // descriptor), then we don't actually close it, but rather open
199                 // up /dev/null into that file descriptor. Otherwise, the first file
200                 // descriptor opened up in the child would be numbered as one of the
201                 // stdio file descriptors, which is likely to wreak havoc.
202                 let setup = |src: Option<P>, dst: c_int| {
203                     let src = match src {
204                         None => {
205                             let flags = if dst == libc::STDIN_FILENO {
206                                 libc::O_RDONLY
207                             } else {
208                                 libc::O_RDWR
209                             };
210                             libc::open(devnull.as_ptr() as *const _, flags, 0)
211                         }
212                         Some(obj) => {
213                             let fd = obj.as_inner().fd();
214                             // Leak the memory and the file descriptor. We're in the
215                             // child now an all our resources are going to be
216                             // cleaned up very soon
217                             mem::forget(obj);
218                             fd
219                         }
220                     };
221                     src != -1 && retry(|| dup2(src, dst)) != -1
222                 };
223
224                 if !setup(in_fd, libc::STDIN_FILENO) { fail(&mut output) }
225                 if !setup(out_fd, libc::STDOUT_FILENO) { fail(&mut output) }
226                 if !setup(err_fd, libc::STDERR_FILENO) { fail(&mut output) }
227
228                 // close all other fds
229                 for fd in (3..getdtablesize()).rev() {
230                     if fd != output.fd() {
231                         let _ = close(fd as c_int);
232                     }
233                 }
234
235                 match cfg.gid() {
236                     Some(u) => {
237                         if libc::setgid(u as libc::gid_t) != 0 {
238                             fail(&mut output);
239                         }
240                     }
241                     None => {}
242                 }
243                 match cfg.uid() {
244                     Some(u) => {
245                         // When dropping privileges from root, the `setgroups` call
246                         // will remove any extraneous groups. If we don't call this,
247                         // then even though our uid has dropped, we may still have
248                         // groups that enable us to do super-user things. This will
249                         // fail if we aren't root, so don't bother checking the
250                         // return value, this is just done as an optimistic
251                         // privilege dropping function.
252                         extern {
253                             fn setgroups(ngroups: libc::c_int,
254                                          ptr: *const libc::c_void) -> libc::c_int;
255                         }
256                         let _ = setgroups(0, ptr::null());
257
258                         if libc::setuid(u as libc::uid_t) != 0 {
259                             fail(&mut output);
260                         }
261                     }
262                     None => {}
263                 }
264                 if cfg.detach() {
265                     // Don't check the error of setsid because it fails if we're the
266                     // process leader already. We just forked so it shouldn't return
267                     // error, but ignore it anyway.
268                     let _ = libc::setsid();
269                 }
270                 if !dirp.is_null() && chdir(dirp) == -1 {
271                     fail(&mut output);
272                 }
273                 if !envp.is_null() {
274                     *sys::os::environ() = envp as *const _;
275                 }
276                 let _ = execvp(*argv, argv as *mut _);
277                 fail(&mut output);
278             })
279         })
280     }
281
282     pub fn wait(&self, deadline: u64) -> IoResult<ProcessExit> {
283         use cmp;
284         use sync::mpsc::TryRecvError;
285
286         static mut WRITE_FD: libc::c_int = 0;
287
288         let mut status = 0 as c_int;
289         if deadline == 0 {
290             return match retry(|| unsafe { c::waitpid(self.pid, &mut status, 0) }) {
291                 -1 => panic!("unknown waitpid error: {:?}", super::last_error()),
292                 _ => Ok(translate_status(status)),
293             }
294         }
295
296         // On unix, wait() and its friends have no timeout parameters, so there is
297         // no way to time out a thread in wait(). From some googling and some
298         // thinking, it appears that there are a few ways to handle timeouts in
299         // wait(), but the only real reasonable one for a multi-threaded program is
300         // to listen for SIGCHLD.
301         //
302         // With this in mind, the waiting mechanism with a timeout barely uses
303         // waitpid() at all. There are a few times that waitpid() is invoked with
304         // WNOHANG, but otherwise all the necessary blocking is done by waiting for
305         // a SIGCHLD to arrive (and that blocking has a timeout). Note, however,
306         // that waitpid() is still used to actually reap the child.
307         //
308         // Signal handling is super tricky in general, and this is no exception. Due
309         // to the async nature of SIGCHLD, we use the self-pipe trick to transmit
310         // data out of the signal handler to the rest of the application. The first
311         // idea would be to have each thread waiting with a timeout to read this
312         // output file descriptor, but a write() is akin to a signal(), not a
313         // broadcast(), so it would only wake up one thread, and possibly the wrong
314         // thread. Hence a helper thread is used.
315         //
316         // The helper thread here is responsible for farming requests for a
317         // waitpid() with a timeout, and then processing all of the wait requests.
318         // By guaranteeing that only this helper thread is reading half of the
319         // self-pipe, we're sure that we'll never lose a SIGCHLD. This helper thread
320         // is also responsible for select() to wait for incoming messages or
321         // incoming SIGCHLD messages, along with passing an appropriate timeout to
322         // select() to wake things up as necessary.
323         //
324         // The ordering of the following statements is also very purposeful. First,
325         // we must be guaranteed that the helper thread is booted and available to
326         // receive SIGCHLD signals, and then we must also ensure that we do a
327         // nonblocking waitpid() at least once before we go ask the sigchld helper.
328         // This prevents the race where the child exits, we boot the helper, and
329         // then we ask for the child's exit status (never seeing a sigchld).
330         //
331         // The actual communication between the helper thread and this thread is
332         // quite simple, just a channel moving data around.
333
334         unsafe { HELPER.boot(register_sigchld, waitpid_helper) }
335
336         match self.try_wait() {
337             Some(ret) => return Ok(ret),
338             None => {}
339         }
340
341         let (tx, rx) = channel();
342         unsafe { HELPER.send(NewChild(self.pid, tx, deadline)); }
343         return match rx.recv() {
344             Ok(e) => Ok(e),
345             Err(..) => Err(timeout("wait timed out")),
346         };
347
348         // Register a new SIGCHLD handler, returning the reading half of the
349         // self-pipe plus the old handler registered (return value of sigaction).
350         //
351         // Be sure to set up the self-pipe first because as soon as we register a
352         // handler we're going to start receiving signals.
353         fn register_sigchld() -> (libc::c_int, c::sigaction) {
354             unsafe {
355                 let mut pipes = [0; 2];
356                 assert_eq!(libc::pipe(pipes.as_mut_ptr()), 0);
357                 set_nonblocking(pipes[0], true).ok().unwrap();
358                 set_nonblocking(pipes[1], true).ok().unwrap();
359                 WRITE_FD = pipes[1];
360
361                 let mut old: c::sigaction = mem::zeroed();
362                 let mut new: c::sigaction = mem::zeroed();
363                 new.sa_handler = sigchld_handler;
364                 new.sa_flags = c::SA_NOCLDSTOP;
365                 assert_eq!(c::sigaction(c::SIGCHLD, &new, &mut old), 0);
366                 (pipes[0], old)
367             }
368         }
369
370         // Helper thread for processing SIGCHLD messages
371         fn waitpid_helper(input: libc::c_int,
372                           messages: Receiver<Req>,
373                           (read_fd, old): (libc::c_int, c::sigaction)) {
374             set_nonblocking(input, true).ok().unwrap();
375             let mut set: c::fd_set = unsafe { mem::zeroed() };
376             let mut tv: libc::timeval;
377             let mut active = Vec::<(libc::pid_t, Sender<ProcessExit>, u64)>::new();
378             let max = cmp::max(input, read_fd) + 1;
379
380             'outer: loop {
381                 // Figure out the timeout of our syscall-to-happen. If we're waiting
382                 // for some processes, then they'll have a timeout, otherwise we
383                 // wait indefinitely for a message to arrive.
384                 //
385                 // FIXME: sure would be nice to not have to scan the entire array
386                 let min = active.iter().map(|a| a.2).enumerate().min_by(|p| {
387                     p.1
388                 });
389                 let (p, idx) = match min {
390                     Some((idx, deadline)) => {
391                         let now = sys::timer::now();
392                         let ms = if now < deadline {deadline - now} else {0};
393                         tv = ms_to_timeval(ms);
394                         (&mut tv as *mut _, idx)
395                     }
396                     None => (ptr::null_mut(), -1),
397                 };
398
399                 // Wait for something to happen
400                 c::fd_set(&mut set, input);
401                 c::fd_set(&mut set, read_fd);
402                 match unsafe { c::select(max, &mut set, ptr::null_mut(),
403                                          ptr::null_mut(), p) } {
404                     // interrupted, retry
405                     -1 if os::errno() == libc::EINTR as i32 => continue,
406
407                     // We read something, break out and process
408                     1 | 2 => {}
409
410                     // Timeout, the pending request is removed
411                     0 => {
412                         drop(active.remove(idx));
413                         continue
414                     }
415
416                     n => panic!("error in select {:?} ({:?})", os::errno(), n),
417                 }
418
419                 // Process any pending messages
420                 if drain(input) {
421                     loop {
422                         match messages.try_recv() {
423                             Ok(NewChild(pid, tx, deadline)) => {
424                                 active.push((pid, tx, deadline));
425                             }
426                             Err(TryRecvError::Disconnected) => {
427                                 assert!(active.len() == 0);
428                                 break 'outer;
429                             }
430                             Err(TryRecvError::Empty) => break,
431                         }
432                     }
433                 }
434
435                 // If a child exited (somehow received SIGCHLD), then poll all
436                 // children to see if any of them exited.
437                 //
438                 // We also attempt to be responsible netizens when dealing with
439                 // SIGCHLD by invoking any previous SIGCHLD handler instead of just
440                 // ignoring any previous SIGCHLD handler. Note that we don't provide
441                 // a 1:1 mapping of our handler invocations to the previous handler
442                 // invocations because we drain the `read_fd` entirely. This is
443                 // probably OK because the kernel is already allowed to coalesce
444                 // simultaneous signals, we're just doing some extra coalescing.
445                 //
446                 // Another point of note is that this likely runs the signal handler
447                 // on a different thread than the one that received the signal. I
448                 // *think* this is ok at this time.
449                 //
450                 // The main reason for doing this is to allow stdtest to run native
451                 // tests as well. Both libgreen and libnative are running around
452                 // with process timeouts, but libgreen should get there first
453                 // (currently libuv doesn't handle old signal handlers).
454                 if drain(read_fd) {
455                     let i: uint = unsafe { mem::transmute(old.sa_handler) };
456                     if i != 0 {
457                         assert!(old.sa_flags & c::SA_SIGINFO == 0);
458                         (old.sa_handler)(c::SIGCHLD);
459                     }
460
461                     // FIXME: sure would be nice to not have to scan the entire
462                     //        array...
463                     active.retain(|&(pid, ref tx, _)| {
464                         let pr = Process { pid: pid };
465                         match pr.try_wait() {
466                             Some(msg) => { tx.send(msg).unwrap(); false }
467                             None => true,
468                         }
469                     });
470                 }
471             }
472
473             // Once this helper thread is done, we re-register the old sigchld
474             // handler and close our intermediate file descriptors.
475             unsafe {
476                 assert_eq!(c::sigaction(c::SIGCHLD, &old, ptr::null_mut()), 0);
477                 let _ = libc::close(read_fd);
478                 let _ = libc::close(WRITE_FD);
479                 WRITE_FD = -1;
480             }
481         }
482
483         // Drain all pending data from the file descriptor, returning if any data
484         // could be drained. This requires that the file descriptor is in
485         // nonblocking mode.
486         fn drain(fd: libc::c_int) -> bool {
487             let mut ret = false;
488             loop {
489                 let mut buf = [0u8; 1];
490                 match unsafe {
491                     libc::read(fd, buf.as_mut_ptr() as *mut libc::c_void,
492                                buf.len() as libc::size_t)
493                 } {
494                     n if n > 0 => { ret = true; }
495                     0 => return true,
496                     -1 if wouldblock() => return ret,
497                     n => panic!("bad read {:?} ({:?})", os::last_os_error(), n),
498                 }
499             }
500         }
501
502         // Signal handler for SIGCHLD signals, must be async-signal-safe!
503         //
504         // This function will write to the writing half of the "self pipe" to wake
505         // up the helper thread if it's waiting. Note that this write must be
506         // nonblocking because if it blocks and the reader is the thread we
507         // interrupted, then we'll deadlock.
508         //
509         // When writing, if the write returns EWOULDBLOCK then we choose to ignore
510         // it. At that point we're guaranteed that there's something in the pipe
511         // which will wake up the other end at some point, so we just allow this
512         // signal to be coalesced with the pending signals on the pipe.
513         extern fn sigchld_handler(_signum: libc::c_int) {
514             let msg = 1;
515             match unsafe {
516                 libc::write(WRITE_FD, &msg as *const _ as *const libc::c_void, 1)
517             } {
518                 1 => {}
519                 -1 if wouldblock() => {} // see above comments
520                 n => panic!("bad error on write fd: {:?} {:?}", n, os::errno()),
521             }
522         }
523     }
524
525     pub fn try_wait(&self) -> Option<ProcessExit> {
526         let mut status = 0 as c_int;
527         match retry(|| unsafe {
528             c::waitpid(self.pid, &mut status, c::WNOHANG)
529         }) {
530             n if n == self.pid => Some(translate_status(status)),
531             0 => None,
532             n => panic!("unknown waitpid error `{:?}`: {:?}", n,
533                        super::last_error()),
534         }
535     }
536 }
537
538 fn with_argv<T,F>(prog: &CString, args: &[CString],
539                   cb: F)
540                   -> T
541     where F : FnOnce(*const *const libc::c_char) -> T
542 {
543     let mut ptrs: Vec<*const libc::c_char> = Vec::with_capacity(args.len()+1);
544
545     // Convert the CStrings into an array of pointers. Note: the
546     // lifetime of the various CStrings involved is guaranteed to be
547     // larger than the lifetime of our invocation of cb, but this is
548     // technically unsafe as the callback could leak these pointers
549     // out of our scope.
550     ptrs.push(prog.as_ptr());
551     ptrs.extend(args.iter().map(|tmp| tmp.as_ptr()));
552
553     // Add a terminating null pointer (required by libc).
554     ptrs.push(ptr::null());
555
556     cb(ptrs.as_ptr())
557 }
558
559 fn with_envp<K,V,T,F>(env: Option<&HashMap<K, V>>,
560                       cb: F)
561                       -> T
562     where F : FnOnce(*const c_void) -> T,
563           K : BytesContainer + Eq + Hash<Hasher>,
564           V : BytesContainer
565 {
566     // On posixy systems we can pass a char** for envp, which is a
567     // null-terminated array of "k=v\0" strings. Since we must create
568     // these strings locally, yet expose a raw pointer to them, we
569     // create a temporary vector to own the CStrings that outlives the
570     // call to cb.
571     match env {
572         Some(env) => {
573             let mut tmps = Vec::with_capacity(env.len());
574
575             for pair in env {
576                 let mut kv = Vec::new();
577                 kv.push_all(pair.0.container_as_bytes());
578                 kv.push('=' as u8);
579                 kv.push_all(pair.1.container_as_bytes());
580                 kv.push(0); // terminating null
581                 tmps.push(kv);
582             }
583
584             // As with `with_argv`, this is unsafe, since cb could leak the pointers.
585             let mut ptrs: Vec<*const libc::c_char> =
586                 tmps.iter()
587                     .map(|tmp| tmp.as_ptr() as *const libc::c_char)
588                     .collect();
589             ptrs.push(ptr::null());
590
591             cb(ptrs.as_ptr() as *const c_void)
592         }
593         _ => cb(ptr::null())
594     }
595 }
596
597 fn translate_status(status: c_int) -> ProcessExit {
598     #![allow(non_snake_case)]
599     #[cfg(any(target_os = "linux", target_os = "android"))]
600     mod imp {
601         pub fn WIFEXITED(status: i32) -> bool { (status & 0xff) == 0 }
602         pub fn WEXITSTATUS(status: i32) -> i32 { (status >> 8) & 0xff }
603         pub fn WTERMSIG(status: i32) -> i32 { status & 0x7f }
604     }
605
606     #[cfg(any(target_os = "macos",
607               target_os = "ios",
608               target_os = "freebsd",
609               target_os = "dragonfly",
610               target_os = "openbsd"))]
611     mod imp {
612         pub fn WIFEXITED(status: i32) -> bool { (status & 0x7f) == 0 }
613         pub fn WEXITSTATUS(status: i32) -> i32 { status >> 8 }
614         pub fn WTERMSIG(status: i32) -> i32 { status & 0o177 }
615     }
616
617     if imp::WIFEXITED(status) {
618         ExitStatus(imp::WEXITSTATUS(status) as int)
619     } else {
620         ExitSignal(imp::WTERMSIG(status) as int)
621     }
622 }