sig/lib.rs
1// @adjivas - github.com/adjivas. See the LICENSE
2// file at the top-level directory of this distribution and at
3// https://github.com/adjivas/sig
4//
5// This file may not be copied, modified, or distributed
6// except according to those terms.
7
8//! # Sig
9//!
10//! [![Crate][crate-badge]][crate] [![license-badge][]][license] [![travis-badge][]][travis] [![circle-badge][]][circle]
11//!
12//! [crate-badge]: https://img.shields.io/badge/crates.io-v0.1.1-orange.svg?style=flat-square
13//! [crate]: https://crates.io/crates/sig
14//! [license-badge]: https://img.shields.io/crates/l/cublas.svg?style=flat-square
15//! [license]: https://github.com/adjivas/sig/blob/master/README.md#license
16//! [travis-badge]: https://travis-ci.org/adjivas/sig.svg?style=flat-square
17//! [travis]: https://travis-ci.org/adjivas/sig
18//! [circle-badge]: https://circleci.com/gh/adjivas/sig/tree/master.svg?style=svg
19//! [circle]: https://circleci.com/gh/adjivas/sig/tree/master
20
21extern crate libc;
22
23#[macro_use]
24mod macros;
25pub mod ffi;
26
27use libc::{sigaction, sighandler_t, sigfillset};
28use std::{mem, ptr};
29
30#[inline]
31pub unsafe fn set_signal_handler(signal: ffi::c_int,
32 handler: unsafe extern "C" fn(ffi::c_int)) {
33 let mut sigset = mem::uninitialized();
34
35 // Block all signals during the handler. This is the expected behavior, but
36 // it's not guaranteed by `signal()`.
37 if sigfillset(&mut sigset) != -1 {
38 // Done because sigaction has private members.
39 // This is safe because sa_restorer and sa_handlers are pointers that
40 // might be null (that is, zero).
41 let mut action: sigaction = mem::zeroed();
42
43 // action.sa_flags = 0;
44 action.sa_mask = sigset;
45 action.sa_sigaction = handler as sighandler_t;
46
47 sigaction(signal, &action, ptr::null_mut());
48 }
49}