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
extern crate libc;
use std::io;
use super::{Handle, HandleInner, PipeHandle, ThenHandle};
use shared_child::unix::SharedChildExt;
pub trait HandleExt {
fn send_signal(&self, signal: libc::c_int) -> io::Result<()>;
}
impl HandleExt for Handle {
fn send_signal(&self, signal: libc::c_int) -> io::Result<()> {
self.inner.send_signal(signal)
}
}
impl HandleExt for HandleInner {
fn send_signal(&self, signal: libc::c_int) -> io::Result<()> {
match *self {
HandleInner::Child(ref child_handle) => child_handle.child.send_signal(signal),
HandleInner::Pipe(ref pipe_handle) => pipe_handle.send_signal(signal),
HandleInner::Then(ref then_handle) => then_handle.send_signal(signal),
HandleInner::Input(ref input_handle) => input_handle.inner_handle.send_signal(signal),
HandleInner::Unchecked(ref inner_handle) => inner_handle.send_signal(signal),
}
}
}
impl HandleExt for PipeHandle {
fn send_signal(&self, signal: libc::c_int) -> io::Result<()> {
let left_result = self.left_handle.send_signal(signal);
let right_result = self.right_start_result
.as_ref()
.map(|right_handle| right_handle.send_signal(signal))
.unwrap_or(Ok(()));
left_result.and(right_result)
}
}
impl HandleExt for ThenHandle {
fn send_signal(&self, signal: libc::c_int) -> io::Result<()> {
match self.shared_state.right_cell.borrow() {
Some(&Ok(ref right_handle)) => right_handle.send_signal(signal),
Some(&Err(_)) => Ok(()),
None => self.shared_state.left_handle.send_signal(signal),
}
}
}
#[cfg(test)]
mod tests {
use test::*;
use cmd;
use super::{libc, HandleExt};
use std::os::unix::process::ExitStatusExt;
use std::sync::Arc;
use std::thread;
#[test]
fn test_send_signal_from_other_thread() {
let sleep_cmd = cmd(path_to_exe("sleep"), &["1000000"]);
let handle = Arc::new(sleep_cmd.unchecked().start().unwrap());
let handle_clone = handle.clone();
thread::spawn(move || handle_clone.send_signal(libc::SIGABRT).unwrap());
let status = handle.wait().unwrap();
assert_eq!(Some(libc::SIGABRT), status.status.signal());
}
#[test]
fn test_send_signal_to_pipe() {
let sleep_cmd = cmd(path_to_exe("sleep"), &["1000000"]);
let handle = sleep_cmd
.pipe(sleep_cmd.clone())
.unchecked()
.start()
.unwrap();
handle.send_signal(libc::SIGABRT).unwrap();
let status = handle.output().unwrap();
assert_eq!(Some(libc::SIGABRT), status.status.signal());
}
#[test]
fn test_signal_then_expression() {
let sleep_cmd = cmd(path_to_exe("sleep"), &["1000000"]);
let handle = sleep_cmd
.unchecked()
.then(true_cmd())
.unchecked()
.start()
.unwrap();
handle.send_signal(libc::SIGTERM).unwrap();
let status = handle.wait().unwrap();
assert_eq!(None, status.status.signal());
assert!(status.status.success());
}
}