Skip to content

Commit 8ed919c

Browse files
committed
Add fd3 support to runmake
1 parent 97091f2 commit 8ed919c

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3149,6 +3149,7 @@ dependencies = [
31493149
"bstr",
31503150
"build_helper",
31513151
"gimli 0.31.0",
3152+
"libc",
31523153
"object 0.36.2",
31533154
"regex",
31543155
"similar",

src/tools/run-make-support/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ similar = "2.5.0"
1010
wasmparser = { version = "0.214", default-features = false, features = ["std"] }
1111
regex = "1.8" # 1.8 to avoid memchr 2.6.0, as 2.5.0 is pinned in the workspace
1212
gimli = "0.31.0"
13+
libc = "0.2"
1314
build_helper = { path = "../build_helper" }

src/tools/run-make-support/src/command.rs

+25
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,31 @@ impl Command {
104104
self
105105
}
106106

107+
/// Set an auxiliary stream passed to the process, besides the stdio streams.
108+
#[cfg(unix)]
109+
pub fn set_aux_fd<F: Into<std::os::fd::OwnedFd>>(
110+
&mut self,
111+
newfd: std::os::fd::RawFd,
112+
fd: F,
113+
) -> &mut Self {
114+
use std::os::fd::AsRawFd;
115+
use std::os::unix::process::CommandExt;
116+
117+
let fd = fd.into();
118+
unsafe {
119+
self.cmd.pre_exec(move || {
120+
let fd = fd.as_raw_fd();
121+
let ret = if fd == newfd {
122+
libc::fcntl(fd, libc::F_SETFD, 0)
123+
} else {
124+
libc::dup2(fd, newfd)
125+
};
126+
if ret == -1 { Err(std::io::Error::last_os_error()) } else { Ok(()) }
127+
});
128+
}
129+
self
130+
}
131+
107132
/// Run the constructed command and assert that it is successfully run.
108133
#[track_caller]
109134
pub fn run(&mut self) -> CompletedProcess {

src/tools/run-make-support/src/macros.rs

+11
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ macro_rules! impl_common_helpers {
8080
self
8181
}
8282

83+
/// Set an auxiliary stream passed to the process, besides the stdio streams.
84+
#[cfg(unix)]
85+
pub fn set_aux_fd<F: Into<std::os::fd::OwnedFd>>(
86+
&mut self,
87+
newfd: std::os::fd::RawFd,
88+
fd: F,
89+
) -> &mut Self {
90+
self.cmd.set_aux_fd(newfd, fd);
91+
self
92+
}
93+
8394
/// Run the constructed command and assert that it is successfully run.
8495
#[track_caller]
8596
pub fn run(&mut self) -> crate::command::CompletedProcess {

tests/run-make/jobserver-error/rmake.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@ fn main() {
1919
.run_fail()
2020
.stderr_utf8();
2121
diff().expected_file("cannot_open_fd.stderr").actual_text("actual", out).run();
22-
// FIXME(Oneirical): Find how to use file descriptor "3" with run-make-support
23-
// and pipe /dev/null into it.
24-
// Original lines:
25-
//
26-
// bash -c 'echo "fn main() {}" | makeflags="--jobserver-auth=3,3" $(rustc) - 3</dev/null' \
27-
// 2>&1 | diff not_a_pipe.stderr -
22+
23+
let out = rustc()
24+
.stdin("fn main() {}")
25+
.input("-")
26+
.env("MAKEFLAGS", "--jobserver-auth=3,3")
27+
.set_aux_fd(3, std::fs::File::open("/dev/null").unwrap())
28+
.run()
29+
.stderr_utf8();
30+
diff().expected_file("not_a_pipe.stderr").actual_text("actual", out).run();
31+
2832
// # this test randomly fails, see https://github.com/rust-lang/rust/issues/110321
29-
// disabled:
30-
// bash -c 'echo "fn main() {}" | makeflags="--jobserver-auth=3,3" $(rustc) - \
31-
// 3< <(cat /dev/null)' 2>&1 | diff poisoned_pipe.stderr -
32-
//
33+
// let (readpipe, _) = std::pipe::pipe().unwrap();
3334
// let out = rustc()
3435
// .stdin("fn main() {}")
3536
// .input("-")
36-
// .env("MAKEFLAGS", "--jobserver-auth=0,0")
37+
// .env("MAKEFLAGS", "--jobserver-auth=3,3")
38+
// .set_fd3(readpipe)
3739
// .run()
3840
// .stderr_utf8();
39-
// diff().expected_file("not_a_pipe.stderr").actual_text("actual", out).run();
41+
// diff().expected_file("poisoned_pipe.stderr").actual_text("actual", out).run();
4042
}

0 commit comments

Comments
 (0)