Skip to content

Commit e57a3f3

Browse files
committed
Move send_ctrlc into inner for doc purposes
1 parent c0296ad commit e57a3f3

4 files changed

Lines changed: 32 additions & 37 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "send_ctrlc"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
authors = ["Scott Meeuwsen <smeeuwsen@gmail.com>"]
55
license = "MIT OR Apache-2.0"
66
description = "A cross platform crate for sending ctrl-c to child processes"

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ async fn main() {
6161
child.send_ctrl_c().unwrap();
6262
child.wait().await.unwrap();
6363
}
64-
6564
```
6665

67-
6866
## Contributions
6967

7068
Contributions are welcome as long they align with the vision for this crate.

src/lib.rs

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,10 @@ const CREATE_NEW_PROCESS_GROUP: u32 = 0x00000200;
1515
/// Trait for sending interrupts/ctrl-c to child processes
1616
pub trait Interruptable: InterruptablePid {
1717
/// Send a ctrl-c interrupt to the child process
18-
#[cfg(all(not(windows), not(unix)))]
19-
fn send_ctrl_c(&self) -> io::Result<()> {
20-
unimplemented!("Not implemented for this platform");
21-
}
22-
23-
/// Send a ctrl-c interrupt to the child process
24-
#[cfg(unix)]
25-
fn send_ctrl_c(&self) -> io::Result<()> {
26-
if let Some(pid) = self.pid() {
27-
if unsafe { libc::kill(pid as i32, libc::SIGINT) } == 0 {
28-
Ok(())
29-
} else {
30-
Err(io::Error::last_os_error())
31-
}
32-
} else {
33-
Err(io::Error::new(io::ErrorKind::Other, "Process has no pid"))
34-
}
35-
}
36-
37-
/// Send a ctrl-c interrupt to the child process
38-
#[cfg(windows)]
3918
fn send_ctrl_c(&self) -> io::Result<()> {
40-
use windows_sys::Win32::System::Console::{CTRL_C_EVENT, GenerateConsoleCtrlEvent};
41-
if let Some(pid) = self.pid() {
42-
// NOTE: This only works if the process is in a new process group
43-
if unsafe { GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid) } != 0 {
44-
Ok(())
45-
} else {
46-
Err(io::Error::last_os_error())
47-
}
48-
} else {
49-
Err(io::Error::new(io::ErrorKind::Other, "Process has no pid"))
19+
match self.pid() {
20+
Some(pid) => inner::send_ctrl_c(pid),
21+
None => Err(io::Error::new(io::ErrorKind::Other, "Process has no pid")),
5022
}
5123
}
5224
}
@@ -78,7 +50,33 @@ pub fn new_command<S: AsRef<OsStr>>(program: S) -> Command {
7850
}
7951

8052
mod inner {
81-
use std::{ffi::OsStr, process::Command};
53+
use std::{ffi::OsStr, io, process::Command};
54+
55+
#[cfg(all(not(windows), not(unix)))]
56+
pub fn send_ctrl_c(_pid: u32) -> io::Result<()> {
57+
unimplemented!("Not implemented for this platform");
58+
}
59+
60+
#[cfg(unix)]
61+
pub fn send_ctrl_c(pid: u32) -> io::Result<()> {
62+
if unsafe { libc::kill(pid as i32, libc::SIGINT) } == 0 {
63+
Ok(())
64+
} else {
65+
Err(io::Error::last_os_error())
66+
}
67+
}
68+
69+
#[cfg(windows)]
70+
pub fn send_ctrl_c(pid: u32) -> io::Result<()> {
71+
use windows_sys::Win32::System::Console::{CTRL_C_EVENT, GenerateConsoleCtrlEvent};
72+
73+
// NOTE: This only works if the process is in a new process group
74+
if unsafe { GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid) } != 0 {
75+
Ok(())
76+
} else {
77+
Err(io::Error::last_os_error())
78+
}
79+
}
8280

8381
#[cfg(windows)]
8482
pub fn new_command<S: AsRef<OsStr>>(program: S) -> Command {
@@ -98,7 +96,6 @@ mod inner {
9896
}
9997

10098
/// Create a new interruptable tokio command
101-
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
10299
#[cfg(feature = "tokio")]
103100
pub fn new_tokio_command<S: AsRef<OsStr>>(program: S) -> tokio::process::Command {
104101
inner_tokio::new_tokio_command(program)

0 commit comments

Comments
 (0)