11// Portions of this file are Copyright 2014 The Rust Project Developers.
22// See http://rust-lang.org/COPYRIGHT.
33
4+ ///! Operating system signals.
5+
46use libc;
57use { Error , Result } ;
68use errno:: Errno ;
@@ -353,24 +355,34 @@ impl AsRef<libc::sigset_t> for SigSet {
353355 }
354356}
355357
358+ /// A signal handler.
356359#[ allow( unknown_lints) ]
357360#[ derive( Debug , Clone , Copy , PartialEq ) ]
358361pub enum SigHandler {
362+ /// Default signal handling.
359363 SigDfl ,
364+ /// Request that the signal be ignored.
360365 SigIgn ,
366+ /// Use the given signal-catching function, which takes in the signal.
361367 Handler ( extern fn ( libc:: c_int ) ) ,
368+ /// Use the given signal-catching function, which takes in the signal, information about how
369+ /// the signal was generated, and a pointer to the threads `ucontext_t`.
362370 SigAction ( extern fn ( libc:: c_int , * mut libc:: siginfo_t , * mut libc:: c_void ) )
363371}
364372
373+ /// Action to take on receipt of a signal. Corresponds to `sigaction`.
365374#[ derive( Clone , Copy ) ]
366375#[ allow( missing_debug_implementations) ]
367376pub struct SigAction {
368377 sigaction : libc:: sigaction
369378}
370379
371380impl SigAction {
372- /// This function will set or unset the flag `SA_SIGINFO` depending on the
373- /// type of the `handler` argument.
381+ /// Creates a new action.
382+ ///
383+ /// The `SA_SIGINFO` bit in the `flags` argument is ignored (it will be set only if `handler`
384+ /// is the `SigAction` variant). `mask` specifies other signals to block during execution of
385+ /// the signal-catching function.
374386 pub fn new ( handler : SigHandler , flags : SaFlags , mask : SigSet ) -> SigAction {
375387 let mut s = unsafe { mem:: uninitialized :: < libc:: sigaction > ( ) } ;
376388 s. sa_sigaction = match handler {
@@ -388,14 +400,18 @@ impl SigAction {
388400 SigAction { sigaction : s }
389401 }
390402
403+ /// Returns the flags set on the action.
391404 pub fn flags ( & self ) -> SaFlags {
392405 SaFlags :: from_bits_truncate ( self . sigaction . sa_flags )
393406 }
394407
408+ /// Returns the set of signals that are blocked during execution of the action's
409+ /// signal-catching function.
395410 pub fn mask ( & self ) -> SigSet {
396411 SigSet { sigset : self . sigaction . sa_mask }
397412 }
398413
414+ /// Returns the action's handler.
399415 pub fn handler ( & self ) -> SigHandler {
400416 match self . sigaction . sa_sigaction {
401417 libc:: SIG_DFL => SigHandler :: SigDfl ,
@@ -407,6 +423,16 @@ impl SigAction {
407423 }
408424}
409425
426+ /// Changes the action taken by a process on receipt of a specific signal.
427+ ///
428+ /// `signal` can be any signal except `SIGKILL` or `SIGSTOP`. On success, it returns the previous
429+ /// action for the given signal. If `sigaction` fails, no new signal handler is installed.
430+ ///
431+ /// # Safety
432+ ///
433+ /// Signal handlers may be called at any point during execution, which limits what is safe to do in
434+ /// the body of the signal-catching function. Be certain to only make syscalls that are explicitly
435+ /// marked safe for signal handlers and only share global data using atomics.
410436pub unsafe fn sigaction ( signal : Signal , sigaction : & SigAction ) -> Result < SigAction > {
411437 let mut oldact = mem:: uninitialized :: < libc:: sigaction > ( ) ;
412438
0 commit comments