Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/bin/ufat/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,14 @@ fn run(argv: &[OsString]) -> Result<ExitCode> {

/// Runs the command line interface.
pub(crate) fn main() -> ExitCode {
// Rust starts programs with SIGPIPE ignored, so writing to a pipe whose
// reader is gone fails with EPIPE, and print!() panics on that, as in
// `ufat describe IMAGE | head -1`. Let the signal end the tool quietly
// instead, as it ends other command line tools. The service keeps
// ignoring it: it writes its replies to the socket with plain writes.
// SAFETY: signal() only sets how SIGPIPE is handled, and SIG_DFL is a
// valid disposition.
unsafe { libc::signal(libc::SIGPIPE, libc::SIG_DFL) };
let argv: Vec<OsString> = std::env::args_os().skip(1).collect();
match run(&argv) {
Ok(code) => code,
Expand Down
52 changes: 52 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,58 @@ fn help_after_the_options_is_an_argument() {
);
}

/// Output into a pipe whose reader is gone ends ufat with SIGPIPE, as it
/// ends other command line tools, without a panic.
#[test]
fn a_closed_pipe_ends_ufat_quietly() {
use std::os::fd::{FromRawFd, OwnedFd};
use std::os::unix::process::ExitStatusExt;
use std::process::Stdio;

let _serial = common::serial();
let Some(bin) = ufat() else {
return eprintln!("skipping: no ufat binary");
};
let dir = Temp::dir();
let image = dir.path().join("a.img");
let image = image.to_str().unwrap();
ok(&bin, &["assemble", "--create", "--size", "40M", image]);
for args in [
&["describe", image][..],
&["capabilities"],
&["--help"],
&["list", "-h"],
] {
let mut fds = [0; 2];
// SAFETY: pipe2() stores two new descriptors in the array.
assert_eq!(unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) }, 0);
// SAFETY: both descriptors were just created and nothing else owns
// them.
let (read, write) = unsafe { (OwnedFd::from_raw_fd(fds[0]), OwnedFd::from_raw_fd(fds[1])) };
// The reader is gone before ufat starts, so its first write fails.
drop(read);
let out = Command::new(&bin)
.args(args)
.stdout(Stdio::from(write))
.stderr(Stdio::piped())
.output()
.expect("ufat runs");
assert_eq!(
out.status.signal(),
Some(libc::SIGPIPE),
"ufat {:?}: {:?}",
args,
out.status
);
assert!(
out.stderr.is_empty(),
"ufat {:?}: {}",
args,
String::from_utf8_lossy(&out.stderr)
);
}
}

#[test]
fn assemble_and_populate_take_trees_and_manifests() {
let _serial = common::serial();
Expand Down