Skip to content

Commit 1bcdcdf

Browse files
author
chloekek
committed
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. Fixes #2366.
1 parent 95d1285 commit 1bcdcdf

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

changelog/2367.changed.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Allow use of `SignalFd` through shared reference
2+
3+
Like with many other file descriptors, concurrent use of signalfds is safe.
4+
Changing the signal mask of and reading signals from a signalfd can now be done
5+
with the `SignalFd` API even if other references to it exist.

src/sys/signalfd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ impl SignalFd {
105105
Ok(SignalFd(fd))
106106
}
107107

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

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

115115
let size = mem::size_of_val(&buffer);

test/sys/test_signalfd.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn read_empty_signalfd() {
2828
};
2929

3030
let mask = SigSet::empty();
31-
let mut fd = SignalFd::with_flags(&mask, SfdFlags::SFD_NONBLOCK).unwrap();
31+
let fd = SignalFd::with_flags(&mask, SfdFlags::SFD_NONBLOCK).unwrap();
3232

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

50-
let mut fd = SignalFd::new(&mask).unwrap();
50+
let fd = SignalFd::new(&mask).unwrap();
5151

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

75-
let mut fd = SignalFd::new(&mask).unwrap();
75+
let fd = SignalFd::new(&mask).unwrap();
7676

7777
mask.add(signal::SIGUSR1);
7878
mask.thread_block().unwrap();

0 commit comments

Comments
 (0)