Skip to content

Commit 633fb6d

Browse files
committed
feat: Make Client::from_env safe to call for any number of time
So that it can be used in different crates without causing UB. Signed-off-by: Jiahao XU <[email protected]>
1 parent 35a2d2e commit 633fb6d

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/lib.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,6 @@ impl Client {
231231
/// result with the connected client will be returned. In other cases
232232
/// result will contain `Err(FromEnvErr)`.
233233
///
234-
/// Note that on Unix the `Client` returned **takes ownership of the file
235-
/// descriptors specified in the environment**. Jobservers on Unix are
236-
/// implemented with `pipe` file descriptors, and they're inherited from
237-
/// parent processes. This `Client` returned takes ownership of the file
238-
/// descriptors for this process and will close the file descriptors after
239-
/// this value is dropped.
240-
///
241234
/// Additionally on Unix this function will configure the file descriptors
242235
/// with `CLOEXEC` so they're not automatically inherited by spawned
243236
/// children.
@@ -256,11 +249,7 @@ impl Client {
256249
/// make sure to take ownership properly of the file descriptors passed
257250
/// down, if any.
258251
///
259-
/// It's generally unsafe to call this function twice in a program if the
260-
/// previous invocation returned `Some`.
261-
///
262-
/// Note, though, that on Windows it should be safe to call this function
263-
/// any number of times.
252+
/// It is ok to call this function any number of times.
264253
pub unsafe fn from_env_ext(check_pipe: bool) -> FromEnv {
265254
let (env, var_os) = match ["CARGO_MAKEFLAGS", "MAKEFLAGS", "MFLAGS"]
266255
.iter()
@@ -299,6 +288,19 @@ impl Client {
299288
/// environment.
300289
///
301290
/// Wraps `from_env_ext` and discards error details.
291+
///
292+
/// # Safety
293+
///
294+
/// This function is `unsafe` to call on Unix specifically as it
295+
/// transitively requires usage of the `from_raw_fd` function, which is
296+
/// itself unsafe in some circumstances.
297+
///
298+
/// It's recommended to call this function very early in the lifetime of a
299+
/// program before any other file descriptors are opened. That way you can
300+
/// make sure to take ownership properly of the file descriptors passed
301+
/// down, if any.
302+
///
303+
/// It is ok to call this function any number of times.
302304
pub unsafe fn from_env() -> Option<Client> {
303305
Self::from_env_ext(false).client.ok()
304306
}

src/unix.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ impl Client {
149149
}
150150
}
151151

152-
drop(set_cloexec(read, true));
153-
drop(set_cloexec(write, true));
154-
Ok(Some(Client::from_fds(read, write)))
152+
Ok(Some(Client::Pipe {
153+
read: clone_fd_and_set_cloexec(read)?,
154+
write: clone_fd_and_set_cloexec(write)?,
155+
}))
155156
}
156157

157158
unsafe fn from_fds(read: c_int, write: c_int) -> Client {
@@ -435,6 +436,14 @@ unsafe fn fd_check(fd: c_int, check_pipe: bool) -> Result<(), FromEnvErrorInner>
435436
}
436437
}
437438

439+
fn clone_fd_and_set_cloexec(fd: c_int) -> Result<File, FromEnvErrorInner> {
440+
// Safety: File is wrapped in `ManuallyDrop` to prevent closing on drop
441+
// since we don't own the fd.
442+
mem::ManuallyDrop::new(unsafe { File::from_raw_fd(fd) })
443+
.try_clone()
444+
.map_err(|err| FromEnvErrorInner::CannotOpenFd(fd, err))
445+
}
446+
438447
fn set_cloexec(fd: c_int, set: bool) -> io::Result<()> {
439448
unsafe {
440449
let previous = cvt(libc::fcntl(fd, libc::F_GETFD))?;

0 commit comments

Comments
 (0)