Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make signal argument to kill optional #445

Merged
merged 1 commit into from
Nov 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#457](https://github.com/nix-rust/nix/pull/457))

### Changed
- `kill`'s signature, defined in `::nix::sys::signal`, changed, so that the
signal parameter has type `T: Into<Option<Signal>>`. `None` as an argument
for that parameter will result in a 0 passed to libc's `kill`, while a
`Some`-argument will result in the previous behavior for the contained
`Signal`.
([#445](https://github.com/nix-rust/nix/pull/410))
- The minimum supported version of rustc is now 1.7.0.
([#444](https://github.com/nix-rust/nix/pull/444))
- Implement `Send` for `KEvent`
Expand Down
8 changes: 6 additions & 2 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,12 @@ pub fn pthread_sigmask(how: SigFlags,
Errno::result(res).map(drop)
}

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

Errno::result(res).map(drop)
}
Expand Down
1 change: 1 addition & 0 deletions test/sys/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod test_signal;
mod test_socket;
mod test_sockopt;
mod test_termios;
Expand Down
7 changes: 7 additions & 0 deletions test/sys/test_signal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use nix::unistd::*;
use nix::sys::signal::*;

#[test]
fn test_kill_none() {
kill(getpid(), None).ok().expect("Should be able to send signal to myself.");
}
2 changes: 1 addition & 1 deletion test/sys/test_wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn test_wait_signal() {
match fork() {
Ok(Child) => pause().unwrap_or(()),
Ok(Parent { child }) => {
kill(child, SIGKILL).ok().expect("Error: Kill Failed");
kill(child, Some(SIGKILL)).ok().expect("Error: Kill Failed");
assert_eq!(waitpid(child, None), Ok(WaitStatus::Signaled(child, SIGKILL, false)));
},
// panic, fork should never fail unless there is a serious problem with the OS
Expand Down