Skip to content
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

SigSet: A new unsafe helper method to create a SigSet from a sigset_t #1741

Merged
merged 2 commits into from
Jul 15, 2022
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
(#[1747](https://github.com/nix-rust/nix/pull/1747))
- Added the `DontRoute` SockOpt
(#[1752](https://github.com/nix-rust/nix/pull/1752))
- Added `signal::SigSet::from_sigset_t_unchecked()`.
(#[1741](https://github.com/nix-rust/nix/pull/1741))

### Changed

Expand All @@ -37,6 +39,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
(#[1739](https://github.com/nix-rust/nix/pull/1739))
- Changed `gethostname` to return an owned `OsString`.
(#[1745](https://github.com/nix-rust/nix/pull/1745))
- `signal:SigSet` is now marked as `repr(transparent)`.
(#[1741](https://github.com/nix-rust/nix/pull/1741))

### Fixed

Expand Down
33 changes: 33 additions & 0 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ use std::iter::FromIterator;
use std::iter::IntoIterator;

/// Specifies a set of [`Signal`]s that may be blocked, waited for, etc.
// We are using `transparent` here to be super sure that `SigSet`
// is represented exactly like the `sigset_t` struct from C.
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct SigSet {
sigset: libc::sigset_t
Expand Down Expand Up @@ -567,6 +570,19 @@ impl SigSet {
Signal::try_from(signum.assume_init()).unwrap()
})
}

/// Converts a `libc::sigset_t` object to a [`SigSet`] without checking whether the
/// `libc::sigset_t` is already initialized.
///
/// # Safety
///
/// The `sigset` passed in must be a valid an initialized `libc::sigset_t` by calling either
/// [`sigemptyset(3)`](https://man7.org/linux/man-pages/man3/sigemptyset.3p.html) or
/// [`sigfillset(3)`](https://man7.org/linux/man-pages/man3/sigfillset.3p.html).
/// Otherwise, the results are undefined.
pub unsafe fn from_sigset_t_unchecked(sigset: libc::sigset_t) -> SigSet {
rtzoeller marked this conversation as resolved.
Show resolved Hide resolved
SigSet { sigset }
}
}

impl AsRef<libc::sigset_t> for SigSet {
Expand Down Expand Up @@ -1311,4 +1327,21 @@ mod tests {
.join()
.unwrap();
}

#[test]
fn test_from_sigset_t_unchecked() {
let src_set = SigSet::empty();
let set = unsafe { SigSet::from_sigset_t_unchecked(src_set.sigset) };

for signal in Signal::iterator() {
assert!(!set.contains(signal));
}

let src_set = SigSet::all();
let set = unsafe { SigSet::from_sigset_t_unchecked(src_set.sigset) };

for signal in Signal::iterator() {
assert!(set.contains(signal));
}
}
}