Skip to content

Commit b44daa1

Browse files
bors[bot]germag
andauthored
Merge #1741
1741: SigSet: A new unsafe helper method to create a SigSet from a sigset_t r=rtzoeller a=germag 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]> Co-authored-by: German Maglione <[email protected]>
2 parents 2556b78 + b207aae commit b44daa1

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
2828
(#[1747](https://github.com/nix-rust/nix/pull/1747))
2929
- Added the `DontRoute` SockOpt
3030
(#[1752](https://github.com/nix-rust/nix/pull/1752))
31+
- Added `signal::SigSet::from_sigset_t_unchecked()`.
32+
(#[1741](https://github.com/nix-rust/nix/pull/1741))
3133

3234
### Changed
3335

@@ -41,6 +43,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
4143
(#[1739](https://github.com/nix-rust/nix/pull/1739))
4244
- Changed `gethostname` to return an owned `OsString`.
4345
(#[1745](https://github.com/nix-rust/nix/pull/1745))
46+
- `signal:SigSet` is now marked as `repr(transparent)`.
47+
(#[1741](https://github.com/nix-rust/nix/pull/1741))
4448

4549
### Fixed
4650

src/sys/signal.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,9 @@ use std::iter::FromIterator;
465465
use std::iter::IntoIterator;
466466

467467
/// Specifies a set of [`Signal`]s that may be blocked, waited for, etc.
468+
// We are using `transparent` here to be super sure that `SigSet`
469+
// is represented exactly like the `sigset_t` struct from C.
470+
#[repr(transparent)]
468471
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
469472
pub struct SigSet {
470473
sigset: libc::sigset_t
@@ -567,6 +570,19 @@ impl SigSet {
567570
Signal::try_from(signum.assume_init()).unwrap()
568571
})
569572
}
573+
574+
/// Converts a `libc::sigset_t` object to a [`SigSet`] without checking whether the
575+
/// `libc::sigset_t` is already initialized.
576+
///
577+
/// # Safety
578+
///
579+
/// The `sigset` passed in must be a valid an initialized `libc::sigset_t` by calling either
580+
/// [`sigemptyset(3)`](https://man7.org/linux/man-pages/man3/sigemptyset.3p.html) or
581+
/// [`sigfillset(3)`](https://man7.org/linux/man-pages/man3/sigfillset.3p.html).
582+
/// Otherwise, the results are undefined.
583+
pub unsafe fn from_sigset_t_unchecked(sigset: libc::sigset_t) -> SigSet {
584+
SigSet { sigset }
585+
}
570586
}
571587

572588
impl AsRef<libc::sigset_t> for SigSet {
@@ -1311,4 +1327,21 @@ mod tests {
13111327
.join()
13121328
.unwrap();
13131329
}
1330+
1331+
#[test]
1332+
fn test_from_sigset_t_unchecked() {
1333+
let src_set = SigSet::empty();
1334+
let set = unsafe { SigSet::from_sigset_t_unchecked(src_set.sigset) };
1335+
1336+
for signal in Signal::iterator() {
1337+
assert!(!set.contains(signal));
1338+
}
1339+
1340+
let src_set = SigSet::all();
1341+
let set = unsafe { SigSet::from_sigset_t_unchecked(src_set.sigset) };
1342+
1343+
for signal in Signal::iterator() {
1344+
assert!(set.contains(signal));
1345+
}
1346+
}
13141347
}

0 commit comments

Comments
 (0)