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

Make sigaction handling more portable #830

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 @@
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() }
}

Check warning on line 903 in src/system/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/system/mod.rs#L898-L903

Added lines #L898 - L903 were not covered by tests
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 @@
}
};

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 })

Check warning on line 39 in src/system/signal/set.rs

View check run for this annotation

Codecov / codecov/patch

src/system/signal/set.rs#L33-L39

Added lines #L33 - L39 were not covered by tests
}

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 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 @@

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

let action = sigaction {
let action = {
let mut raw: libc::sigaction = make_zeroed_sigaction();

Check warning on line 71 in src/system/term/user_term.rs

View check run for this annotation

Codecov / codecov/patch

src/system/term/user_term.rs#L70-L71

Added lines #L70 - L71 were not covered by tests
// Call `on_sigttou` if `SIGTTOU` arrives.
sa_sigaction: on_sigttou as sighandler_t,
raw.sa_sigaction = on_sigttou as sighandler_t;

Check warning on line 73 in src/system/term/user_term.rs

View check run for this annotation

Codecov / codecov/patch

src/system/term/user_term.rs#L73

Added line #L73 was not covered by tests
// Exclude any other signals from the set
sa_mask: {
raw.sa_mask = {

Check warning on line 75 in src/system/term/user_term.rs

View check run for this annotation

Codecov / codecov/patch

src/system/term/user_term.rs#L75

Added line #L75 was not covered by tests
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

Check warning on line 82 in src/system/term/user_term.rs

View check run for this annotation

Codecov / codecov/patch

src/system/term/user_term.rs#L79-L82

Added lines #L79 - L82 were not covered by tests
};
// Reset `GOT_SIGTTOU`.
GOT_SIGTTOU.store(false, Ordering::SeqCst);
Expand Down Expand Up @@ -225,17 +230,19 @@

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

let action = sigaction {
let action = {
let mut raw: libc::sigaction = make_zeroed_sigaction();

Check warning on line 234 in src/system/term/user_term.rs

View check run for this annotation

Codecov / codecov/patch

src/system/term/user_term.rs#L233-L234

Added lines #L233 - L234 were not covered by tests
// Call `on_sigttou` if `SIGTTOU` arrives.
sa_sigaction: on_sigttou as sighandler_t,
raw.sa_sigaction = on_sigttou as sighandler_t;

Check warning on line 236 in src/system/term/user_term.rs

View check run for this annotation

Codecov / codecov/patch

src/system/term/user_term.rs#L236

Added line #L236 was not covered by tests
// Exclude any other signals from the set
sa_mask: {
raw.sa_mask = {

Check warning on line 238 in src/system/term/user_term.rs

View check run for this annotation

Codecov / codecov/patch

src/system/term/user_term.rs#L238

Added line #L238 was not covered by tests
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

Check warning on line 245 in src/system/term/user_term.rs

View check run for this annotation

Codecov / codecov/patch

src/system/term/user_term.rs#L242-L245

Added lines #L242 - L245 were not covered by tests
};
// Reset `GOT_SIGTTOU`.
GOT_SIGTTOU.store(false, Ordering::SeqCst);
Expand Down
Loading