-
Notifications
You must be signed in to change notification settings - Fork 42
feat: Make Client::from_env
safe to call for any number of times
#57
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -231,13 +231,6 @@ impl Client { | |||||||||||||||||||||||||||
/// result with the connected client will be returned. In other cases | ||||||||||||||||||||||||||||
/// result will contain `Err(FromEnvErr)`. | ||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||
/// Note that on Unix the `Client` returned **takes ownership of the file | ||||||||||||||||||||||||||||
/// descriptors specified in the environment**. Jobservers on Unix are | ||||||||||||||||||||||||||||
/// implemented with `pipe` file descriptors, and they're inherited from | ||||||||||||||||||||||||||||
/// parent processes. This `Client` returned takes ownership of the file | ||||||||||||||||||||||||||||
/// descriptors for this process and will close the file descriptors after | ||||||||||||||||||||||||||||
/// this value is dropped. | ||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||
/// Additionally on Unix this function will configure the file descriptors | ||||||||||||||||||||||||||||
/// with `CLOEXEC` so they're not automatically inherited by spawned | ||||||||||||||||||||||||||||
/// children. | ||||||||||||||||||||||||||||
|
@@ -256,11 +249,7 @@ impl Client { | |||||||||||||||||||||||||||
/// make sure to take ownership properly of the file descriptors passed | ||||||||||||||||||||||||||||
/// down, if any. | ||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||
/// It's generally unsafe to call this function twice in a program if the | ||||||||||||||||||||||||||||
/// previous invocation returned `Some`. | ||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||
/// Note, though, that on Windows it should be safe to call this function | ||||||||||||||||||||||||||||
/// any number of times. | ||||||||||||||||||||||||||||
/// It is ok to call this function any number of times. | ||||||||||||||||||||||||||||
pub unsafe fn from_env_ext(check_pipe: bool) -> FromEnv { | ||||||||||||||||||||||||||||
let (env, var_os) = match ["CARGO_MAKEFLAGS", "MAKEFLAGS", "MFLAGS"] | ||||||||||||||||||||||||||||
.iter() | ||||||||||||||||||||||||||||
|
@@ -293,6 +282,19 @@ impl Client { | |||||||||||||||||||||||||||
/// environment. | ||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||
/// Wraps `from_env_ext` and discards error details. | ||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||
/// # Safety | ||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||
/// This function is `unsafe` to call on Unix specifically as it | ||||||||||||||||||||||||||||
/// transitively requires usage of the `from_raw_fd` function, which is | ||||||||||||||||||||||||||||
/// itself unsafe in some circumstances. | ||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||
/// It's recommended to call this function very early in the lifetime of a | ||||||||||||||||||||||||||||
/// program before any other file descriptors are opened. That way you can | ||||||||||||||||||||||||||||
/// make sure to take ownership properly of the file descriptors passed | ||||||||||||||||||||||||||||
/// down, if any. | ||||||||||||||||||||||||||||
Comment on lines
+286
to
+295
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: How about this?
Suggested change
|
||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||
/// It is ok to call this function any number of times. | ||||||||||||||||||||||||||||
pub unsafe fn from_env() -> Option<Client> { | ||||||||||||||||||||||||||||
Self::from_env_ext(false).client.ok() | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -158,9 +158,10 @@ impl Client { | |||||
} | ||||||
} | ||||||
|
||||||
drop(set_cloexec(read, true)); | ||||||
drop(set_cloexec(write, true)); | ||||||
Ok(Some(Client::from_fds(read, write))) | ||||||
Ok(Some(Client::Pipe { | ||||||
read: clone_fd_and_set_cloexec(read)?, | ||||||
write: clone_fd_and_set_cloexec(write)?, | ||||||
})) | ||||||
} | ||||||
|
||||||
unsafe fn from_fds(read: c_int, write: c_int) -> Client { | ||||||
|
@@ -444,6 +445,14 @@ unsafe fn fd_check(fd: c_int, check_pipe: bool) -> Result<(), FromEnvErrorInner> | |||||
} | ||||||
} | ||||||
|
||||||
fn clone_fd_and_set_cloexec(fd: c_int) -> Result<File, FromEnvErrorInner> { | ||||||
NobodyXu marked this conversation as resolved.
Show resolved
Hide resolved
NobodyXu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
// Safety: fd is a valid fd dand it remains open until returns | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
unsafe { BorrowedFd::borrow_raw(fd) } | ||||||
.try_clone_to_owned() | ||||||
.map(File::from) | ||||||
.map_err(|err| FromEnvErrorInner::CannotOpenFd(fd, err)) | ||||||
} | ||||||
|
||||||
fn set_cloexec(fd: c_int, set: bool) -> io::Result<()> { | ||||||
unsafe { | ||||||
let previous = cvt(libc::fcntl(fd, libc::F_GETFD))?; | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.