runny 1.2.5

Run a program inside its own process group
Documentation
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
extern crate nix;
extern crate kernel32;
extern crate user32;
extern crate winapi;

#[cfg(unix)]
use self::nix::sys::signal::{kill, SIGTERM, SIGKILL};

#[cfg(unix)]
use self::nix::unistd::Pid;

use std::process::Child;
use std::io::{self, Read, Result, Write};
use std::fs::File;
use std::fmt;
use std::thread::{self, JoinHandle};
use std::time::Duration;
use std::result;
use std::sync::{Arc, Mutex, Condvar};
use std::collections::HashMap;

pub struct RunningWaiter {
    result: Arc<(Mutex<Option<i32>>, Condvar)>,
    term_thr: Arc<Mutex<JoinHandle<()>>>,
    term_delay: Arc<Mutex<Option<Duration>>>,
}

impl fmt::Debug for RunningWaiter {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "<RunningWaiter>")
    }
}


pub struct RunningOutput {
    stream: File,
}

pub struct RunningInput {
    stream: File,
}

#[derive(PartialEq)]
enum ProcessState {
    Running,
    Exited,
}

// We must not drop "tty" until the process exits,
// however we never actually /use/ tty.
#[allow(dead_code)]
pub struct Running {
    child_pid: i32,
    input: Option<RunningInput>,
    output: Option<RunningOutput>,
    error: Option<RunningOutput>,
    term_thr: Arc<Mutex<JoinHandle<()>>>,
    term_delay: Arc<Mutex<Option<Duration>>>,
    wait_thr: JoinHandle<()>,
    result: Arc<(Mutex<Option<i32>>, Condvar)>,
    state: Arc<Mutex<ProcessState>>,
}

pub enum RunningError {
    RunningIoError(io::Error),
    #[cfg(unix)]
    RunningNixError(self::nix::Error),
}

impl From<io::Error> for RunningError {
    fn from(kind: io::Error) -> Self {
        RunningError::RunningIoError(kind)
    }
}

#[cfg(unix)]
impl From<self::nix::Error> for RunningError {
    fn from(kind: self::nix::Error) -> Self {
        RunningError::RunningNixError(kind)
    }
}

impl fmt::Debug for RunningError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &RunningError::RunningIoError(ref e) => write!(f, "Running I/O error: {:?}", e),
            #[cfg(unix)]
            &RunningError::RunningNixError(ref e) => write!(f, "Running Nix error: {:?}", e),
        }
    }
}

impl fmt::Debug for Running {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Running {}: {:?}", self.child_pid, self.result)
    }
}

#[cfg(windows)]
fn send_wmclose(process_id: self::winapi::LPWORD) -> self::winapi::minwindef::BOOL {
    use self::winapi::{HWND, LPARAM, DWORD};

    extern "system" fn enum_windows_callback(hwnd: HWND,
                                             target_pid: LPARAM)
                                             -> self::winapi::minwindef::BOOL {
        let mut found_process_id = 0;
        let target_pid = target_pid as DWORD;

        unsafe { self::user32::GetWindowThreadProcessId(hwnd, &mut found_process_id) };

        if found_process_id == target_pid {
            unsafe { self::user32::PostMessageW(hwnd, self::winapi::WM_CLOSE, 0, 0) };
        }

        // Continue enumerating windows
        1
    }

    // let enum_func_ptr = &mut enum_func as F;
    unsafe { self::user32::EnumWindows(Some(enum_windows_callback), process_id as LPARAM) }
}

impl Running {
    pub fn new(mut child: Child,
               input: File,
               output: File,
               timeout: Option<Duration>,
               mut handles: HashMap<String, File>)
               -> Running {

        // Drop stdin/stdout/stderr on the child, since we access it using
        // the "master" file instead.
        // On Windows, these handles will already be None.
        drop(child.stdin.take());
        drop(child.stdout.take());
        drop(child.stderr.take());

        let child_pid = child.id() as i32;
        let child_result = Arc::new((Mutex::new(None), Condvar::new()));
        let child_result_thr = child_result.clone();
        let term_delay: Arc<Mutex<Option<Duration>>> = Arc::new(Mutex::new(None));
        let process_state = Arc::new(Mutex::new(ProcessState::Running));

        let term_delay_thr = term_delay.clone();

        let term_thr = Arc::new(Mutex::new(thread::spawn(move || {

            // Allow the child process to run for a given amount of time,
            // or until we're woken up by a termination process.
            if let Some(t) = timeout {
                thread::park_timeout(t);
            } else {
                thread::park();
            }

            // We've been woken up, so it's time to terminate the child process.
            // Use a negative value to terminate all children in the process group.
            #[cfg(unix)]
            {
                kill(Pid::from_raw(-child_pid), SIGTERM).ok();

                if let Some(t) = *term_delay_thr.lock().unwrap() {
                    thread::park_timeout(t);
                }

                // Send a SIGKILL to all children, to ensure they're gone.
                kill(Pid::from_raw(-child_pid), SIGKILL).ok();
            }
            #[cfg(windows)]
            {
                // Post the WM_CLOSE message to each window
                send_wmclose(child_pid as self::winapi::LPWORD);

                if let Some(t) = *term_delay_thr.lock().unwrap() {
                    thread::park_timeout(t);
                }

                unsafe {
                    let handle = self::kernel32::OpenProcess(1, // PROCESS_TERMINATE
                                                             0,
                                                             child_pid as u32);
                    self::kernel32::TerminateProcess(handle, 1);
                }
            }
        })));

        // This thread just does a wait() on the child, and stores the result
        // in a variable.
        let term_thr_timeout_handle = term_thr.clone();
        let process_state_thr = process_state.clone();
        let wait_thr = thread::spawn(move || {
            // Finally, get the return code of the process.
            let &(ref lock, ref cvar) = &*child_result_thr;
            let mut child_result = lock.lock().unwrap();

            let result = match child.wait() {
                Err(_) => Some(-1),
                Ok(o) => {
                    match o.code() {
                        Some(c) => Some(c),
                        None => Some(-2),
                    }
                }
            };
            *process_state_thr.lock().unwrap() = ProcessState::Exited;

            *child_result = result;
            cvar.notify_all();

            // Stop the timeout handle thread, which should exit immediately.
            term_thr_timeout_handle.lock().unwrap().thread().unpark();
        });

        let stderr = match handles.remove("stderr") {
            Some(s) => Some(RunningOutput { stream: s }),
            None => panic!("No stderr found"),
        };

        Running {
            child_pid: child_pid,
            term_delay: term_delay,
            input: Some(RunningInput { stream: input }),
            output: Some(RunningOutput { stream: output }),
            error: stderr,
            term_thr: term_thr,
            wait_thr: wait_thr,
            result: child_result,
            state: process_state,
        }
    }

    pub fn take_output(&mut self) -> RunningOutput {
        let value = self.output.take();
        value.unwrap()
    }

    pub fn output(&self) -> &Option<RunningOutput> {
        &self.output
    }

    pub fn take_input(&mut self) -> RunningInput {
        let stream = self.input.take();
        stream.unwrap()
    }

    pub fn input(&self) -> &Option<RunningInput> {
        &self.input
    }

    pub fn take_error(&mut self) -> RunningOutput {
        let value = self.error.take();
        value.unwrap()
    }

    pub fn error(&self) -> &Option<RunningOutput> {
        &self.error
    }

    pub fn wait(&self) -> result::Result<i32, RunningError> {
        // Convert a None ExitStatus into -1, removing
        // the Option<> from the type chain.
        let &(ref lock, ref cvar) = &*self.result;
        let mut ret = lock.lock().unwrap();
        while ret.is_none() {
            ret = cvar.wait(ret).unwrap();
        }
        Ok(ret.unwrap())
    }

    pub fn waiter(&self) -> RunningWaiter {
        RunningWaiter {
            result: self.result.clone(),
            term_thr: self.term_thr.clone(),
            term_delay: self.term_delay.clone(),
        }
    }

    pub fn result(&self) -> i32 {
        self.wait().unwrap();

        // We can unwrap here, because wait() should always set Some
        // value, and if not it's a bad bug anyway.

        let &(ref lock, ref cvar) = &*self.result;
        let mut ret = lock.lock().unwrap();
        while ret.is_none() {
            ret = cvar.wait(ret).unwrap();
        }
        ret.unwrap()
    }

    pub fn terminate(&self, timeout: Option<Duration>) -> result::Result<i32, RunningError> {

        // If there's already a result, then the process has exited already.
        {
            let &(ref lock, _) = &*self.result;
            let ret = lock.try_lock();
            if let Ok(ref unlocked) = ret {
                if let Some(retval) = **unlocked {
                    return Ok(retval);
                }
            }
        }

        // Set up the delay, then wake up the termination thread.
        if let Ok(ref mut delay) = self.term_delay.try_lock() {
            **delay = timeout;
        }

        self.term_thr.lock().unwrap().thread().unpark();

        // Hand execution off to self.wait(), which shouldn't block now that the process is
        // being terminated.
        self.wait()
    }

    pub fn pid(&self) -> i32 {
        self.child_pid
    }
}


impl Read for Running {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        let output = match self.output {
            Some(ref mut s) => s,
            None => return Err(io::Error::from_raw_os_error(9 /* EBADF */)),
        };

        match output.read(buf) {
            Err(e) => {
                match e.raw_os_error() {
                    Some(5) => Ok(0),
                    _ => Err(e),
                }
            }
            Ok(n) => Ok(n),
        }
    }
}

impl Write for Running {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        let input = match self.input {
            Some(ref mut s) => s,
            None => return Err(io::Error::from_raw_os_error(9 /* EBADF */)),
        };
        input.write(buf)
    }

    fn flush(&mut self) -> Result<()> {
        let input = match self.input {
            Some(ref mut s) => s,
            None => return Err(io::Error::from_raw_os_error(9 /* EBADF */)),
        };
        input.flush()
    }
}

impl Drop for Running {
    fn drop(&mut self) {
        // Terminate immediately
        self.terminate(None).ok();
    }
}

impl Read for RunningOutput {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        match self.stream.read(buf) {
            Err(e) => {
                match e.raw_os_error() {
                    Some(5) => Ok(0),
                    _ => Err(e),
                }
            }
            Ok(n) => Ok(n),
        }
    }
}

impl Write for RunningInput {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        self.stream.write(buf)
    }

    fn flush(&mut self) -> Result<()> {
        self.stream.flush()
    }
}

impl RunningWaiter {
    pub fn wait(&self) {
        // waitpid(self.pid, None).ok();
        self.result();
    }

    pub fn result(&self) -> i32 {
        let &(ref lock, ref cvar) = &*self.result;
        let mut ret = lock.lock().unwrap();
        while ret.is_none() {
            ret = cvar.wait(ret).unwrap();
        }
        ret.unwrap()
    }

    pub fn terminate(&self, timeout: &Option<Duration>) {
        let mut lock = self.term_delay.try_lock();
        if let Ok(ref mut delay) = lock {
            **delay = *timeout;
        }
        drop(lock);
        self.term_thr.lock().unwrap().thread().unpark();
    }
}