Skip to content

Allow use of SignalFd through shared reference #2367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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