Skip to content

Commit ced3f42

Browse files
committed
Conditionally mark fork as safe in linux
1 parent bb709e5 commit ced3f42

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lib/sudo-exec/src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,7 @@ pub fn run_command(
7777

7878
let (pty_leader, pty_follower) = openpty()?;
7979
let (rx, tx) = pipe()?;
80-
// SAFETY: we don't call any function that is not `async-signal-safe` inside this `fork` as all
81-
// the signal handling is done by `signal_hook` which protects us from it.
82-
#[allow(unsafe_code)]
83-
let monitor_pid = unsafe { fork() }?;
80+
let monitor_pid = fork()?;
8481
// Monitor logic. Based on `exec_monitor`.
8582
if monitor_pid == 0 {
8683
match monitor::MonitorRelay::new(command, pty_follower, tx)?.run()? {}

lib/sudo-system/src/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ pub fn pipe() -> io::Result<(OwnedFd, OwnedFd)> {
4040
Ok(unsafe { (OwnedFd::from_raw_fd(fds[0]), OwnedFd::from_raw_fd(fds[1])) })
4141
}
4242

43+
#[cfg(target_os = "linux")]
44+
/// Create a new process.
45+
pub fn fork() -> io::Result<ProcessId> {
46+
// SAFETY: `fork` is implemented using `clone` in linux so we don't need to worry about signal
47+
// safety.
48+
cerr(unsafe { libc::fork() })
49+
}
50+
51+
#[cfg(not(target_os = "linux"))]
52+
/// Create a new process.
53+
///
54+
/// # Safety
55+
///
56+
/// In a multithreaded program, only async-signal-safe functions are guaranteed to work in the
57+
/// child process until a call to `execve` or a similar function is done.
4358
pub unsafe fn fork() -> io::Result<ProcessId> {
4459
cerr(unsafe { libc::fork() })
4560
}

0 commit comments

Comments
 (0)