From 8d0188435bb9dc38f19be5beec06cdc1884149cc Mon Sep 17 00:00:00 2001 From: DanSnow Date: Mon, 4 Mar 2019 10:25:05 +0800 Subject: [PATCH] feat: Add killpg --- CHANGELOG.md | 2 ++ src/sys/signal.rs | 16 ++++++++++++++++ test/sys/test_signal.rs | 6 ++++++ 3 files changed, 24 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bb653e747..aec1ca0ef8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). Android and Linux. ([#1016](https://github.com/nix-rust/nix/pull/1016)) - Add `ALG_SET_IV`, `ALG_SET_OP` and `ALG_SET_AEAD_ASSOCLEN` control messages and `AF_ALG` socket types on Linux and Android ([#1031](https://github.com/nix-rust/nix/pull/1031)) +- Add killpg + ([#1034](https://github.com/nix-rust/nix/pull/1034)) ### Changed - `PollFd` event flags renamed to `PollFlags` ([#1024](https://github.com/nix-rust/nix/pull/1024/)) diff --git a/src/sys/signal.rs b/src/sys/signal.rs index eeaabd0424..73f5f2d45e 100644 --- a/src/sys/signal.rs +++ b/src/sys/signal.rs @@ -680,6 +680,22 @@ pub fn kill>>(pid: ::unistd::Pid, signal: T) -> Result<() Errno::result(res).map(drop) } +/// Send a signal to a process group [(see +/// killpg(3))](http://pubs.opengroup.org/onlinepubs/9699919799/functions/killpg.html). +/// +/// If `pgrp` less then or equal 1, the behavior is platform-specific. +/// If `signal` is `None`, `killpg` will only preform error checking and won't +/// send any signal. +pub fn killpg>>(pgrp: ::unistd::Pid, signal: T) -> Result<()> { + let res = unsafe { libc::killpg(pgrp.into(), + match signal.into() { + Some(s) => s as libc::c_int, + None => 0, + }) }; + + Errno::result(res).map(drop) +} + pub fn raise(signal: Signal) -> Result<()> { let res = unsafe { libc::raise(signal as libc::c_int) }; diff --git a/test/sys/test_signal.rs b/test/sys/test_signal.rs index f5e4dfd9eb..8780763f77 100644 --- a/test/sys/test_signal.rs +++ b/test/sys/test_signal.rs @@ -9,6 +9,12 @@ fn test_kill_none() { kill(getpid(), None).expect("Should be able to send signal to myself."); } +#[test] +fn test_killpg_none() { + killpg(getpgrp(), None) + .expect("Should be able to send signal to my process group."); +} + #[test] fn test_old_sigaction_flags() { extern "C" fn handler(_: ::libc::c_int) {}