Skip to content

Commit

Permalink
Add fd3 support to runmake
Browse files Browse the repository at this point in the history
  • Loading branch information
coolreader18 authored and Oneirical committed Sep 14, 2024
1 parent edd750e commit f6f9e10
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/tools/run-make-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ similar = "2.5.0"
wasmparser = { version = "0.216", default-features = false, features = ["std"] }
regex = "1.8" # 1.8 to avoid memchr 2.6.0, as 2.5.0 is pinned in the workspace
gimli = "0.31.0"
libc = "0.2"
build_helper = { path = "../build_helper" }
serde_json = "1.0"
libc = "0.2"
25 changes: 25 additions & 0 deletions src/tools/run-make-support/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,31 @@ impl Command {
self
}

/// Set an auxiliary stream passed to the process, besides the stdio streams.
#[cfg(unix)]
pub fn set_aux_fd<F: Into<std::os::fd::OwnedFd>>(
&mut self,
newfd: std::os::fd::RawFd,
fd: F,
) -> &mut Self {
use std::os::fd::AsRawFd;
use std::os::unix::process::CommandExt;

let fd = fd.into();
unsafe {
self.cmd.pre_exec(move || {
let fd = fd.as_raw_fd();
let ret = if fd == newfd {
libc::fcntl(fd, libc::F_SETFD, 0)
} else {
libc::dup2(fd, newfd)
};
if ret == -1 { Err(std::io::Error::last_os_error()) } else { Ok(()) }
});
}
self
}

/// Run the constructed command and assert that it is successfully run.
///
/// By default, std{in,out,err} are [`Stdio::piped()`].
Expand Down
11 changes: 11 additions & 0 deletions src/tools/run-make-support/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ macro_rules! impl_common_helpers {
self
}

/// Set an auxiliary stream passed to the process, besides the stdio streams.
#[cfg(unix)]
pub fn set_aux_fd<F: Into<std::os::fd::OwnedFd>>(
&mut self,
newfd: std::os::fd::RawFd,
fd: F,
) -> &mut Self {
self.cmd.set_aux_fd(newfd, fd);
self
}

/// Run the constructed command and assert that it is successfully run.
#[track_caller]
pub fn run(&mut self) -> crate::command::CompletedProcess {
Expand Down
26 changes: 14 additions & 12 deletions tests/run-make/jobserver-error/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ fn main() {
.run_fail()
.stderr_utf8();
diff().expected_file("cannot_open_fd.stderr").actual_text("actual", out).run();
// FIXME(Oneirical): Find how to use file descriptor "3" with run-make-support
// and pipe /dev/null into it.
// Original lines:
//
// bash -c 'echo "fn main() {}" | makeflags="--jobserver-auth=3,3" $(rustc) - 3</dev/null' \
// 2>&1 | diff not_a_pipe.stderr -

let out = rustc()
.stdin("fn main() {}")
.input("-")
.env("MAKEFLAGS", "--jobserver-auth=3,3")
.set_aux_fd(3, std::fs::File::open("/dev/null").unwrap())
.run()
.stderr_utf8();
diff().expected_file("not_a_pipe.stderr").actual_text("actual", out).run();

// # this test randomly fails, see https://github.com/rust-lang/rust/issues/110321
// disabled:
// bash -c 'echo "fn main() {}" | makeflags="--jobserver-auth=3,3" $(rustc) - \
// 3< <(cat /dev/null)' 2>&1 | diff poisoned_pipe.stderr -
//
// let (readpipe, _) = std::pipe::pipe().unwrap();
// let out = rustc()
// .stdin("fn main() {}")
// .input("-")
// .env("MAKEFLAGS", "--jobserver-auth=0,0")
// .env("MAKEFLAGS", "--jobserver-auth=3,3")
// .set_fd3(readpipe)
// .run()
// .stderr_utf8();
// diff().expected_file("not_a_pipe.stderr").actual_text("actual", out).run();
// diff().expected_file("poisoned_pipe.stderr").actual_text("actual", out).run();
}

0 comments on commit f6f9e10

Please sign in to comment.