Skip to content

Commit e0a3929

Browse files
committed
Replace UNIX Ctrl+C handler to use libc::raise
1 parent 552f268 commit e0a3929

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ crossterm = { version = "0.29.0", features = [
4242
], optional = true }
4343

4444
[target.'cfg(unix)'.dependencies]
45-
nix = { version = "0.29", features = ["signal"], optional = true }
45+
libc = { version = "0.2.174", optional = true }
4646

4747
[target.'cfg(windows)'.dependencies]
4848
windows-sys = { version = "0.59.0", features = ["Win32_System_Console"], optional = true }
@@ -55,7 +55,7 @@ serde_json = "1.0.108"
5555
tempfile = "3.8.1"
5656

5757
[features]
58-
clipboard = ["dep:crossterm", "dep:windows-sys", "dep:nix"]
58+
clipboard = ["dep:crossterm", "dep:windows-sys", "dep:libc"]
5959
default = ["clipboard"]
6060

6161
[patch.crates-io]

src/main.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -745,11 +745,13 @@ async fn send(args: SendArgs) -> anyhow::Result<()> {
745745
#[cfg(feature = "clipboard")]
746746
{
747747
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
748+
749+
#[cfg(any(unix, windows))]
750+
use std::io;
751+
748752
#[cfg(unix)]
749-
use nix::{
750-
sys::signal::{kill, Signal},
751-
unistd::Pid,
752-
};
753+
use libc::{raise, SIGINT};
754+
753755
#[cfg(windows)]
754756
use windows_sys::Win32::System::Console::{GenerateConsoleCtrlEvent, CTRL_C_EVENT};
755757

@@ -786,13 +788,20 @@ async fn send(args: SendArgs) -> anyhow::Result<()> {
786788
.unwrap_or_else(|e| eprintln!("Failed to disable raw mode: {e}"));
787789

788790
#[cfg(unix)]
789-
kill(Pid::from_raw(0), Some(Signal::SIGINT))
790-
.unwrap_or_else(|e| eprintln!("Failed to end process: {e}"));
791+
// Safety: Raw syscall to re-send the SIGINT signal to the console.
792+
// `raise` returns nonzero for failure.
793+
if unsafe { raise(SIGINT) } != 0 {
794+
eprintln!("Failed to raise signal: {}", io::Error::last_os_error());
795+
}
791796

792797
#[cfg(windows)]
793-
// Safety: Raw syscall to re-send the Ctrl+C event to the console
798+
// Safety: Raw syscall to re-send the Ctrl+C event to the console.
799+
// `GenerateConsoleCtrlEvent` returns 0 for failure.
794800
if unsafe { GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0) } == 0 {
795-
eprintln!("Failed to end process: {}", std::io::Error::last_os_error());
801+
eprintln!(
802+
"Failed to generate console event: {}",
803+
io::Error::last_os_error()
804+
);
796805
}
797806
}
798807
_ => {}

0 commit comments

Comments
 (0)