Skip to content

Commit 45d8bee

Browse files
committed
Make signal argument to kill optional
1 parent a9f630e commit 45d8bee

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2222
([#457](https://github.com/nix-rust/nix/pull/457))
2323

2424
### Changed
25+
- `kill`'s signature, defined in `::nix::sys::signal`, changed, so that the
26+
signal parameter has type `T: Into<Option<Signal>>`. `None` as an argument
27+
for that parameter will result in a 0 passed to libc's `kill`, while a
28+
`Some`-argument will result in the previous behavior for the contained
29+
`Signal`.
30+
([#445](https://github.com/nix-rust/nix/pull/410))
2531
- The minimum supported version of rustc is now 1.7.0.
2632
([#444](https://github.com/nix-rust/nix/pull/444))
2733
- Implement `Send` for `KEvent`

src/sys/signal.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,12 @@ pub fn pthread_sigmask(how: SigFlags,
387387
Errno::result(res).map(drop)
388388
}
389389

390-
pub fn kill(pid: libc::pid_t, signal: Signal) -> Result<()> {
391-
let res = unsafe { libc::kill(pid, signal as libc::c_int) };
390+
pub fn kill<T: Into<Option<Signal>>>(pid: libc::pid_t, signal: T) -> Result<()> {
391+
let res = unsafe { libc::kill(pid,
392+
match signal.into() {
393+
Some(s) => s as libc::c_int,
394+
None => 0,
395+
}) };
392396

393397
Errno::result(res).map(drop)
394398
}

test/sys/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod test_signal;
12
mod test_socket;
23
mod test_sockopt;
34
mod test_termios;

test/sys/test_signal.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use nix::unistd::*;
2+
use nix::sys::signal::*;
3+
4+
#[test]
5+
fn test_kill_none() {
6+
kill(getpid(), None).ok().expect("Should be able to send signal to myself.");
7+
}

test/sys/test_wait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn test_wait_signal() {
99
match fork() {
1010
Ok(Child) => pause().unwrap_or(()),
1111
Ok(Parent { child }) => {
12-
kill(child, SIGKILL).ok().expect("Error: Kill Failed");
12+
kill(child, Some(SIGKILL)).ok().expect("Error: Kill Failed");
1313
assert_eq!(waitpid(child, None), Ok(WaitStatus::Signaled(child, SIGKILL, false)));
1414
},
1515
// panic, fork should never fail unless there is a serious problem with the OS

0 commit comments

Comments
 (0)