Skip to content

Commit 3d44d27

Browse files
committed
SigSet: A new unsafe helper method to create a SigSet from a sigset_t
Currently, the only way to create a `SigSet` from a `sigset_t` object is by using pointer casts, like: ``` unsafe { let sigset = *(&sigset as *const libc::sigset_t as *const SigSet) }; ``` This is un-ergonomic for library creators with interfaces to C. So, let's add a new unsafe method that creates a `SigSet` from a `libc::sigset_t` object. We can't implement `From` since converting from `libc::sigset_t` to `SigSet` is unsafe, because objects of type `libc::sigset_t` must be initialized by calling either `sigemptyset(3)` or `sigfillset(3)` before being used. In other case, the results are undefined. We can't implement `TryFrom` either, because there is no way to check if an object of type `libc::sigset_t` is initialized. Signed-off-by: German Maglione <[email protected]>
1 parent 5f859d1 commit 3d44d27

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
2424
(#[1747](https://github.com/nix-rust/nix/pull/1747))
2525
- Added the `DontRoute` SockOpt
2626
(#[1752](https://github.com/nix-rust/nix/pull/1752))
27+
- Added `signal::SigSet::from_sigset_t_unchecked()`.
28+
(#[1741](https://github.com/nix-rust/nix/pull/1741))
2729

2830
### Changed
2931

src/sys/signal.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,19 @@ impl SigSet {
567567
Signal::try_from(signum.assume_init()).unwrap()
568568
})
569569
}
570+
571+
/// Converts a `libc::sigset_t` object to a [`SigSet`] without checking whether the
572+
/// `libc::sigset_t` is already initialized.
573+
///
574+
/// # Safety
575+
///
576+
/// The `sigset` passed in must be a valid an initialized `libc::sigset_t` by calling either
577+
/// [`sigemptyset(3)`](https://man7.org/linux/man-pages/man3/sigemptyset.3p.html) or
578+
/// [`sigfillset(3)`](https://man7.org/linux/man-pages/man3/sigfillset.3p.html).
579+
/// Otherwise, the results are undefined.
580+
pub unsafe fn from_sigset_t_unchecked(sigset: libc::sigset_t) -> SigSet {
581+
SigSet { sigset }
582+
}
570583
}
571584

572585
impl AsRef<libc::sigset_t> for SigSet {
@@ -1311,4 +1324,21 @@ mod tests {
13111324
.join()
13121325
.unwrap();
13131326
}
1327+
1328+
#[test]
1329+
fn test_from_sigset_t_unchecked() {
1330+
let src_set = SigSet::empty();
1331+
let set = unsafe { SigSet::from_sigset_t_unchecked(src_set.sigset) };
1332+
1333+
for signal in Signal::iterator() {
1334+
assert!(!set.contains(signal));
1335+
}
1336+
1337+
let src_set = SigSet::all();
1338+
let set = unsafe { SigSet::from_sigset_t_unchecked(src_set.sigset) };
1339+
1340+
for signal in Signal::iterator() {
1341+
assert!(set.contains(signal));
1342+
}
1343+
}
13141344
}

0 commit comments

Comments
 (0)