Skip to content

Commit

Permalink
Allow use of SignalFd through shared reference (#2367)
Browse files Browse the repository at this point in the history
Like with many other file descriptors, concurrent use of signalfds is safe.
Changing the signal mask of and reading signals from a signalfd can now be done
with the `SignalFd` API even if other references to it exist.

Fixes #2366.
  • Loading branch information
zopsicle authored Apr 13, 2024
1 parent 95d1285 commit bae5fdb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions changelog/2367.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Allow use of `SignalFd` through shared reference

Like with many other file descriptors, concurrent use of signalfds is safe.
Changing the signal mask of and reading signals from a signalfd can now be done
with the `SignalFd` API even if other references to it exist.
4 changes: 2 additions & 2 deletions src/sys/signalfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ impl SignalFd {
Ok(SignalFd(fd))
}

pub fn set_mask(&mut self, mask: &SigSet) -> Result<()> {
pub fn set_mask(&self, mask: &SigSet) -> Result<()> {
self.update(mask, SfdFlags::empty())
}

pub fn read_signal(&mut self) -> Result<Option<siginfo>> {
pub fn read_signal(&self) -> Result<Option<siginfo>> {
let mut buffer = mem::MaybeUninit::<siginfo>::uninit();

let size = mem::size_of_val(&buffer);
Expand Down
6 changes: 3 additions & 3 deletions test/sys/test_signalfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn read_empty_signalfd() {
};

let mask = SigSet::empty();
let mut fd = SignalFd::with_flags(&mask, SfdFlags::SFD_NONBLOCK).unwrap();
let fd = SignalFd::with_flags(&mask, SfdFlags::SFD_NONBLOCK).unwrap();

let res = fd.read_signal();
assert!(res.unwrap().is_none());
Expand All @@ -47,7 +47,7 @@ fn test_signalfd() {
mask.add(signal::SIGUSR1);
mask.thread_block().unwrap();

let mut fd = SignalFd::new(&mask).unwrap();
let fd = SignalFd::new(&mask).unwrap();

// Send a SIGUSR1 signal to the current process. Note that this uses `raise` instead of `kill`
// because `kill` with `getpid` isn't correct during multi-threaded execution like during a
Expand All @@ -72,7 +72,7 @@ fn test_signalfd_setmask() {
// Block the SIGUSR1 signal from automatic processing for this thread
let mut mask = SigSet::empty();

let mut fd = SignalFd::new(&mask).unwrap();
let fd = SignalFd::new(&mask).unwrap();

mask.add(signal::SIGUSR1);
mask.thread_block().unwrap();
Expand Down

0 comments on commit bae5fdb

Please sign in to comment.