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
use std::io;
pub trait SharedChildExt {
fn send_signal(&self, signal: libc::c_int) -> io::Result<()>;
}
impl SharedChildExt for super::SharedChild {
fn send_signal(&self, signal: libc::c_int) -> io::Result<()> {
let status = self.state_lock.lock().unwrap();
if let super::ChildState::Exited(_) = *status {
return Ok(());
}
let pid = self.id() as libc::pid_t;
match unsafe { libc::kill(pid, signal) } {
-1 => Err(io::Error::last_os_error()),
_ => Ok(()),
}
}
}
#[cfg(test)]
mod tests {
use super::SharedChildExt;
use crate::tests::*;
use crate::SharedChild;
use std::os::unix::process::ExitStatusExt;
#[test]
fn test_send_signal() {
let child = SharedChild::spawn(&mut sleep_forever_cmd()).unwrap();
child.send_signal(libc::SIGABRT).unwrap();
let status = child.wait().unwrap();
assert_eq!(Some(libc::SIGABRT), status.signal());
}
}