Skip to content

Commit f6f9e10

Browse files
coolreader18Oneirical
authored andcommitted
Add fd3 support to runmake
1 parent edd750e commit f6f9e10

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ similar = "2.5.0"
1010
wasmparser = { version = "0.216", 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" }
1415
serde_json = "1.0"
1516
libc = "0.2"

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

+25
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,31 @@ impl Command {
151151
self
152152
}
153153

154+
/// Set an auxiliary stream passed to the process, besides the stdio streams.
155+
#[cfg(unix)]
156+
pub fn set_aux_fd<F: Into<std::os::fd::OwnedFd>>(
157+
&mut self,
158+
newfd: std::os::fd::RawFd,
159+
fd: F,
160+
) -> &mut Self {
161+
use std::os::fd::AsRawFd;
162+
use std::os::unix::process::CommandExt;
163+
164+
let fd = fd.into();
165+
unsafe {
166+
self.cmd.pre_exec(move || {
167+
let fd = fd.as_raw_fd();
168+
let ret = if fd == newfd {
169+
libc::fcntl(fd, libc::F_SETFD, 0)
170+
} else {
171+
libc::dup2(fd, newfd)
172+
};
173+
if ret == -1 { Err(std::io::Error::last_os_error()) } else { Ok(()) }
174+
});
175+
}
176+
self
177+
}
178+
154179
/// Run the constructed command and assert that it is successfully run.
155180
///
156181
/// By default, std{in,out,err} are [`Stdio::piped()`].

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)