Skip to content

Commit

Permalink
Merge #1741
Browse files Browse the repository at this point in the history
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 <gmaglione@redhat.com>

Co-authored-by: German Maglione <gmaglione@redhat.com>
  • Loading branch information
bors[bot] and germag authored Jul 8, 2022
2 parents caebe66 + ab6e19c commit 59d55f7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
(#[1697](https://github.com/nix-rust/nix/pull/1697))
- Added `getrusage` and helper types `UsageWho` and `Usage`
(#[1747](https://github.com/nix-rust/nix/pull/1747))
- Added `from_sigset_t_unchecked()` to `signal::SigSet`.
(#[1741](https://github.com/nix-rust/nix/pull/1741))

### Changed

Expand All @@ -32,6 +34,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
(#[1713](https://github.com/nix-rust/nix/pull/1713))
- `nix::poll::ppoll`: `sigmask` parameter is now optional.
(#[1739](https://github.com/nix-rust/nix/pull/1739))
- `signal:SigSet` it's now marked as `repr(trasparent)`.
(#[1741](https://github.com/nix-rust/nix/pull/1741))

### Fixed

Expand Down
34 changes: 34 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 {
SigSet { sigset }
}
}

impl AsRef<libc::sigset_t> for SigSet {
Expand Down Expand Up @@ -1311,4 +1327,22 @@ 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));
}
}

}

0 comments on commit 59d55f7

Please sign in to comment.