Skip to content

Make sigaction handling more portable #830

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 2 commits into from
Feb 23, 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
7 changes: 7 additions & 0 deletions src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,3 +894,10 @@ mod tests {
assert_eq!(status.exit_status(), Some(0));
}
}

pub fn make_zeroed_sigaction() -> libc::sigaction {
// SAFETY: since sigaction is a C struct, all-zeroes is a valid representation
// We cannot use a "literal struct" initialization method since the exact representation
// of libc::sigaction is not fixed, see e.g. https://github.com/memorysafety/sudo-rs/issues/829
unsafe { std::mem::zeroed() }
}
17 changes: 8 additions & 9 deletions src/system/signal/set.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cutils::cerr;
use crate::{cutils::cerr, system::make_zeroed_sigaction};

use super::{handler::SignalHandlerBehavior, SignalNumber};

Expand Down Expand Up @@ -30,14 +30,13 @@ impl SignalAction {
}
};

Ok(Self {
raw: libc::sigaction {
sa_sigaction,
sa_mask: sa_mask.raw,
sa_flags,
sa_restorer: None,
},
})
let mut raw: libc::sigaction = make_zeroed_sigaction();
raw.sa_sigaction = sa_sigaction;
raw.sa_mask = sa_mask.raw;
raw.sa_flags = sa_flags;
raw.sa_restorer = None;

Ok(Self { raw })
}

pub(super) fn register(&self, signal: SignalNumber) -> io::Result<Self> {
Expand Down
33 changes: 20 additions & 13 deletions src/system/term/user_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use libc::{
};

use super::{TermSize, Terminal};
use crate::{cutils::cerr, system::interface::ProcessId};
use crate::{
cutils::cerr,
system::{interface::ProcessId, make_zeroed_sigaction},
};

const INPUT_FLAGS: tcflag_t = IGNPAR
| PARMRK
Expand Down Expand Up @@ -64,17 +67,19 @@ fn tcsetattr_nobg(fd: c_int, flags: c_int, tp: *const termios) -> io::Result<()>

let mut original_action = MaybeUninit::<sigaction>::uninit();

let action = sigaction {
let action = {
let mut raw: libc::sigaction = make_zeroed_sigaction();
// Call `on_sigttou` if `SIGTTOU` arrives.
sa_sigaction: on_sigttou as sighandler_t,
raw.sa_sigaction = on_sigttou as sighandler_t;
// Exclude any other signals from the set
sa_mask: {
raw.sa_mask = {
let mut sa_mask = MaybeUninit::<sigset_t>::uninit();
unsafe { sigemptyset(sa_mask.as_mut_ptr()) };
unsafe { sa_mask.assume_init() }
},
sa_flags: 0,
sa_restorer: None,
};
raw.sa_flags = 0;
raw.sa_restorer = None;
raw
};
// Reset `GOT_SIGTTOU`.
GOT_SIGTTOU.store(false, Ordering::SeqCst);
Expand Down Expand Up @@ -225,17 +230,19 @@ impl UserTerm {

let mut original_action = MaybeUninit::<sigaction>::uninit();

let action = sigaction {
let action = {
let mut raw: libc::sigaction = make_zeroed_sigaction();
// Call `on_sigttou` if `SIGTTOU` arrives.
sa_sigaction: on_sigttou as sighandler_t,
raw.sa_sigaction = on_sigttou as sighandler_t;
// Exclude any other signals from the set
sa_mask: {
raw.sa_mask = {
let mut sa_mask = MaybeUninit::<sigset_t>::uninit();
unsafe { sigemptyset(sa_mask.as_mut_ptr()) };
unsafe { sa_mask.assume_init() }
},
sa_flags: 0,
sa_restorer: None,
};
raw.sa_flags = 0;
raw.sa_restorer = None;
raw
};
// Reset `GOT_SIGTTOU`.
GOT_SIGTTOU.store(false, Ordering::SeqCst);
Expand Down