| 1 |
//! Event source for tracking Unix signals |
| 2 |
//! |
| 3 |
//! Only available on Linux. |
| 4 |
//! |
| 5 |
//! This allows you to track and receive Unix signals through the event loop |
| 6 |
//! rather than by registering signal handlers. It uses `signalfd` under the hood. |
| 7 |
//! |
| 8 |
//! The source will take care of masking and unmasking signals for the thread it runs on, |
| 9 |
//! but you are responsible for masking them on other threads if you run them. The simplest |
| 10 |
//! way to ensure that is to setup the signal event source before spawning any thread, as |
| 11 |
//! they'll inherit their parent signal mask. |
| 12 |
|
| 13 |
use std::convert::TryFrom; |
| 14 |
use std::os::raw::c_int; |
| 15 |
|
| 16 |
use nix::sys::signal::SigSet; |
| 17 |
pub use nix::sys::signal::Signal; |
| 18 |
pub use nix::sys::signalfd::siginfo; |
| 19 |
use nix::sys::signalfd::{SfdFlags, SignalFd}; |
| 20 |
|
| 21 |
use super::generic::Generic; |
| 22 |
use crate::{EventSource, Interest, Mode, Poll, PostAction, Readiness, Token, TokenFactory}; |
| 23 |
|
| 24 |
/// An event generated by the signal event source |
| 25 |
#[derive(Copy, Clone, Debug)] |
| 26 |
pub struct Event { |
| 27 |
info: siginfo, |
| 28 |
} |
| 29 |
|
| 30 |
impl Event { |
| 31 |
/// Retrieve the signal number that was receive |
| 32 |
pub fn signal(&self) -> Signal { |
| 33 |
Signal::try_from(self.info.ssi_signo as c_int).unwrap() |
| 34 |
} |
| 35 |
|
| 36 |
/// Access the full `siginfo_t` associated with this signal event |
| 37 |
pub fn full_info(&self) -> siginfo { |
| 38 |
self.info |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/// An event source for receiving Unix signals |
| 43 |
#[derive(Debug)] |
| 44 |
pub struct Signals { |
| 45 |
sfd: Generic<SignalFd>, |
| 46 |
mask: SigSet, |
| 47 |
} |
| 48 |
|
| •49 |
trait MySetMask { |
| 50 |
fn my_set_mask(&mut self, mask: &SigSet) -> nix::Result<()>; |
| 51 |
} |
| 52 |
|
| 53 |
use std::os::fd::AsFd; |
| 54 |
//workaround for https://github.com/nix-rust/nix/issues/2116 |
| 55 |
impl MySetMask for SignalFd { |
| 56 |
fn my_set_mask(&mut self, mask: &SigSet) -> nix::Result<()> { |
| 57 |
nix::sys::signalfd::signalfd(Some(self.as_fd()), &mask, SfdFlags::empty()) |
| 58 |
.map(std::mem::forget) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
impl Signals { |
| 63 |
/// Create a new signal event source listening on the specified list of signals |
| 64 |
pub fn new(signals: &[Signal]) -> crate::Result<Signals> { |
| 65 |
let mut mask = SigSet::empty(); |
| 66 |
for &s in signals { |
| 67 |
mask.add(s); |
| 68 |
} |
| 69 |
|
| 70 |
// Mask the signals for this thread |
| 71 |
mask.thread_block()?; |
| 72 |
// Create the SignalFd |
| 73 |
let sfd = SignalFd::with_flags(&mask, SfdFlags::SFD_NONBLOCK | SfdFlags::SFD_CLOEXEC)?; |
| 74 |
|
| 75 |
Ok(Signals { |
| 76 |
sfd: Generic::new(sfd, Interest::READ, Mode::Level), |
| 77 |
mask, |
| 78 |
}) |
| 79 |
} |
| 80 |
|
| 81 |
/// Add a list of signals to the signals source |
| 82 |
/// |
| 83 |
/// If this function returns an error, the signal mask of the thread may |
| 84 |
/// have still been changed. |
| 85 |
pub fn add_signals(&mut self, signals: &[Signal]) -> crate::Result<()> { |
| 86 |
for &s in signals { |
| 87 |
self.mask.add(s); |
| 88 |
} |
| 89 |
self.mask.thread_block()?; |
| •90 |
self.sfd.file.my_set_mask(&self.mask)?; |
| 91 |
Ok(()) |
| 92 |
} |
| 93 |
|
| 94 |
/// Remove a list of signals from the signals source |
| 95 |
/// |
| 96 |
/// If this function returns an error, the signal mask of the thread may |
| 97 |
/// have still been changed. |
| 98 |
pub fn remove_signals(&mut self, signals: &[Signal]) -> crate::Result<()> { |
| 99 |
let mut removed = SigSet::empty(); |
| 100 |
for &s in signals { |
| 101 |
self.mask.remove(s); |
| 102 |
removed.add(s); |
| 103 |
} |
| 104 |
removed.thread_unblock()?; |
| •105 |
self.sfd.file.my_set_mask(&self.mask)?; |
| 106 |
Ok(()) |
| 107 |
} |
| 108 |
|
| 109 |
/// Replace the list of signals of the source |
| 110 |
/// |
| 111 |
/// If this function returns an error, the signal mask of the thread may |
| 112 |
/// have still been changed. |
| 113 |
pub fn set_signals(&mut self, signals: &[Signal]) -> crate::Result<()> { |
| 114 |
let mut new_mask = SigSet::empty(); |
| 115 |
for &s in signals { |
| 116 |
new_mask.add(s); |
| 117 |
} |
| 118 |
|
| 119 |
self.mask.thread_unblock()?; |
| 120 |
new_mask.thread_block()?; |
| •121 |
self.sfd.file.my_set_mask(&new_mask)?; |
| 122 |
self.mask = new_mask; |
| 123 |
|
| 124 |
Ok(()) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
impl Drop for Signals { |
| 129 |
fn drop(&mut self) { |
| 130 |
// we cannot handle error here |
| 131 |
if let Err(e) = self.mask.thread_unblock() { |
| 132 |
log::warn!("[calloop] Failed to unmask signals: {:?}", e); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
impl EventSource for Signals { |
| 138 |
type Event = Event; |
| 139 |
type Metadata = (); |
| 140 |
type Ret = (); |
| 141 |
type Error = SignalError; |
| 142 |
|
| 143 |
fn process_events<C>( |
| 144 |
&mut self, |
| 145 |
readiness: Readiness, |
| 146 |
token: Token, |
| 147 |
mut callback: C, |
| 148 |
) -> Result<PostAction, Self::Error> |
| 149 |
where |
| 150 |
C: FnMut(Self::Event, &mut Self::Metadata) -> Self::Ret, |
| 151 |
{ |
| 152 |
self.sfd |
| 153 |
.process_events(readiness, token, |_, sfd| { |
| 154 |
loop { |
| 155 |
match sfd.read_signal() { |
| 156 |
Ok(Some(info)) => callback(Event { info }, &mut ()), |
| 157 |
Ok(None) => break, |
| 158 |
Err(e) => { |
| 159 |
log::warn!("[callop] Error reading from signalfd: {}", e); |
| 160 |
return Err(e.into()); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
Ok(PostAction::Continue) |
| 165 |
}) |
| 166 |
.map_err(|e| SignalError(e.into())) |
| 167 |
} |
| 168 |
|
| 169 |
fn register(&mut self, poll: &mut Poll, token_factory: &mut TokenFactory) -> crate::Result<()> { |
| 170 |
self.sfd.register(poll, token_factory) |
| 171 |
} |
| 172 |
|
| 173 |
fn reregister( |
| 174 |
&mut self, |
| 175 |
poll: &mut Poll, |
| 176 |
token_factory: &mut TokenFactory, |
| 177 |
) -> crate::Result<()> { |
| 178 |
self.sfd.reregister(poll, token_factory) |
| 179 |
} |
| 180 |
|
| 181 |
fn unregister(&mut self, poll: &mut Poll) -> crate::Result<()> { |
| 182 |
self.sfd.unregister(poll) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/// An error arising from processing events for a process signal. |
| 187 |
#[derive(thiserror::Error, Debug)] |
| 188 |
#[error(transparent)] |
| 189 |
pub struct SignalError(Box<dyn std::error::Error + Sync + Send>); |