-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
process: add dup2 file action #153133
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
Open
avikivity
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
avikivity:unix-spawn-dup2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−0
Open
process: add dup2 file action #153133
Changes from all commits
Commits
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| //@ run-pass | ||
| //@ only-unix (this is a unix-specific test) | ||
| //@ needs-subprocess | ||
| //@ ignore-fuchsia no execvp syscall | ||
| //@ ignore-tvos execvp is prohibited | ||
| //@ ignore-watchos execvp is prohibited | ||
|
|
||
| // Test for CommandExt::dup2_file_action: verifies that a CLOEXEC pipe fd | ||
| // can be inherited by a child process when dup2_file_action(fd, fd) is used | ||
| // to clear the CLOEXEC flag via posix_spawn_file_actions_adddup2. | ||
|
|
||
| #![feature(rustc_private, process_file_actions)] | ||
|
|
||
| extern crate libc; | ||
|
|
||
| use std::env; | ||
| use std::os::unix::process::CommandExt; | ||
| use std::process::Command; | ||
|
|
||
| fn main() { | ||
| let args: Vec<String> = env::args().collect(); | ||
|
|
||
| if args.len() >= 3 && args[1] == "child" { | ||
| // Child mode: read from the inherited fd and print what we got | ||
| let fd: libc::c_int = args[2].parse().unwrap(); | ||
| let mut buf = [0u8; 64]; | ||
| let n = unsafe { libc::read(fd, buf.as_mut_ptr() as *mut _, buf.len()) }; | ||
| assert!(n > 0, "expected to read from inherited fd {}, got {}", fd, n); | ||
| let msg = std::str::from_utf8(&buf[..n as usize]).unwrap(); | ||
| assert_eq!(msg, "hello from parent"); | ||
| return; | ||
| } | ||
|
|
||
| // Parent mode: create a pipe (fds are CLOEXEC by default via pipe2), | ||
| // write to the write end, then spawn a child with dup2_file_action | ||
| // to clear CLOEXEC on the read end so the child can read it. | ||
|
|
||
| let mut pipe_fds = [0 as libc::c_int; 2]; | ||
| #[cfg(not(any(target_os = "macos", target_os = "ios")))] | ||
| { | ||
| let ret = unsafe { libc::pipe2(pipe_fds.as_mut_ptr(), libc::O_CLOEXEC) }; | ||
| assert_eq!(ret, 0, "pipe2 failed"); | ||
| } | ||
| #[cfg(any(target_os = "macos", target_os = "ios"))] | ||
| { | ||
| // macOS doesn't have pipe2, use pipe + fcntl | ||
| let ret = unsafe { libc::pipe(pipe_fds.as_mut_ptr()) }; | ||
| assert_eq!(ret, 0, "pipe failed"); | ||
| unsafe { | ||
| libc::fcntl(pipe_fds[0], libc::F_SETFD, libc::FD_CLOEXEC); | ||
| libc::fcntl(pipe_fds[1], libc::F_SETFD, libc::FD_CLOEXEC); | ||
| } | ||
| } | ||
|
|
||
| let read_fd = pipe_fds[0]; | ||
| let write_fd = pipe_fds[1]; | ||
|
|
||
| // Verify the read end has CLOEXEC set | ||
| let flags = unsafe { libc::fcntl(read_fd, libc::F_GETFD) }; | ||
| assert!(flags & libc::FD_CLOEXEC != 0, "expected CLOEXEC on read fd"); | ||
|
|
||
| // Write data to the pipe | ||
| let msg = b"hello from parent"; | ||
| let written = unsafe { libc::write(write_fd, msg.as_ptr() as *const _, msg.len()) }; | ||
| assert_eq!(written, msg.len() as isize); | ||
| unsafe { libc::close(write_fd) }; | ||
|
|
||
| // Spawn child with dup2_file_action(read_fd, read_fd) to clear CLOEXEC | ||
| let me = env::current_exe().unwrap(); | ||
| let output = unsafe { | ||
| Command::new(&me) | ||
| .arg("child") | ||
| .arg(read_fd.to_string()) | ||
| .dup2_file_action(read_fd, read_fd) | ||
| .output() | ||
| .unwrap() | ||
| }; | ||
|
|
||
| unsafe { libc::close(read_fd) }; | ||
|
|
||
| assert!( | ||
| output.status.success(), | ||
| "child failed: status={}, stderr={}", | ||
| output.status, | ||
| String::from_utf8_lossy(&output.stderr) | ||
| ); | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this accept an OwnedFd or BorrowedFd as input fd to make this function safe?