summaryrefslogtreecommitdiffstats
path: root/src/wait/mod.rs
blob: f87b95484b8aefafd60c06c754eaf48d454b3ec4 (plain) (blame)
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
use nix::libc::{self, c_int};
use nix::{errno::Errno, sys::wait::WaitStatus, unistd::Pid};
use std::convert::{TryFrom, TryInto};
use std::process::Child;
use std::sync::mpsc;
use std::thread;
use std::time::{Duration, Instant};

mod error;
mod rusage_ffi;
pub use rusage_ffi::Rusage;

use error::ProcessSignalInfo;
pub use error::WaitError;

#[derive(Debug)]
struct WaitInfo {
    pub status: WaitStatus,
    pub usage: Rusage,
    pub wall_time: Duration,
}

#[derive(Debug)]
pub struct ChildExitStatus {
    pub usage: Rusage,
    pub wall_time: Duration,
}

impl TryFrom<WaitInfo> for ChildExitStatus {
    type Error = WaitError;

    fn try_from(info: WaitInfo) -> Result<Self, Self::Error> {
        match info.status {
            WaitStatus::Exited(pid, ret) => match ret {
                0 => Ok(ChildExitStatus {
                    usage: info.usage,
                    wall_time: info.wall_time,
                }),
                _ => Err(WaitError::ReturnNonZero(ret, pid)),
            },
            WaitStatus::Signaled(pid, signal, coredump) => {
                Err(WaitError::Signaled(ProcessSignalInfo {
                    pid,
                    signal,
                    coredump,
                }))
            }
            _ => Err(WaitError::NotExited),
        }
    }
}

#[cfg(target_os = "macos")]
#[link(name = "c")]
extern "C" {
    fn wait4(
        pid: libc::pid_t,
        status: *mut c_int,
        options: c_int,
        rusage: *mut libc::rusage,
    ) -> libc::pid_t;
}

#[cfg(target_os = "linux")]
use libc::wait4;

fn wait4_pid(
    pid: Pid,
    chan: mpsc::Sender<std::result::Result<WaitInfo, nix::Error>>,
    timer: Instant,
) {
    let mut status: c_int = 0;
    let mut usg: libc::rusage;
    let wait_ret;

    unsafe {
        usg = std::mem::zeroed();
        wait_ret = wait4(pid.as_raw(), &mut status, 0 as c_int, &mut usg);
    }

    #[allow(unused_must_use)]
    {
        chan.send(match wait_ret {
            -1 => Err(nix::Error::Sys(Errno::last())),
            _ => WaitStatus::from_raw(pid, status).map(|nix_status| WaitInfo {
                status: nix_status,
                usage: usg.into(),
                wall_time: timer.elapsed(),
            }),
        });
    };
}

pub fn wait_child(
    mut child: Child,
    timeout: Duration,
    timer: Instant,
) -> Result<ChildExitStatus, WaitError> {
    let pid = Pid::from_raw(child.id() as i32);
    let (send, recv) = mpsc::channel();

    let thr = thread::spawn(move || wait4_pid(pid, send, timer));

    match recv.recv_timeout(timeout) {
        Ok(Ok(wait_info)) => wait_info.try_into(),
        Ok(Err(err)) => Err(err.into()),
        Err(mpsc::RecvTimeoutError::Timeout) => {
            drop(recv);
            drop(thr);

            #[allow(unused_must_use)]
            {
                child.kill();
            }

            Err(WaitError::TimedOut(timeout))
        }
        Err(mpsc::RecvTimeoutError::Disconnected) => unreachable!(),
    }
}