Skip to content

Commit c3bff0c

Browse files
committed
ufat: let SIGPIPE end the command line tool
Rust starts programs with SIGPIPE ignored. So when the reader of ufat's output goes away, as in `ufat describe IMAGE | head -1`, the next write fails with EPIPE and print!() panics. Restore the default disposition in the command line tool, so it ends quietly like other tools do. The service keeps ignoring SIGPIPE since it writes its replies to the socket with plain writes. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
1 parent 18f4c98 commit c3bff0c

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

‎src/bin/ufat/cli.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,14 @@ fn run(argv: &[OsString]) -> Result<ExitCode> {
10991099

11001100
/// Runs the command line interface.
11011101
pub(crate) fn main() -> ExitCode {
1102+
// Rust starts programs with SIGPIPE ignored, so writing to a pipe whose
1103+
// reader is gone fails with EPIPE, and print!() panics on that, as in
1104+
// `ufat describe IMAGE | head -1`. Let the signal end the tool quietly
1105+
// instead, as it ends other command line tools. The service keeps
1106+
// ignoring it: it writes its replies to the socket with plain writes.
1107+
// SAFETY: signal() only sets how SIGPIPE is handled, and SIG_DFL is a
1108+
// valid disposition.
1109+
unsafe { libc::signal(libc::SIGPIPE, libc::SIG_DFL) };
11021110
let argv: Vec<OsString> = std::env::args_os().skip(1).collect();
11031111
match run(&argv) {
11041112
Ok(code) => code,

‎tests/cli.rs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,58 @@ fn help_and_short_options() {
9999
fails(&bin, &["list", "-l=1", "x.img"], "option -l takes no value");
100100
}
101101

102+
/// Output into a pipe whose reader is gone ends ufat with SIGPIPE, as it
103+
/// ends other command line tools, without a panic.
104+
#[test]
105+
fn a_closed_pipe_ends_ufat_quietly() {
106+
use std::os::fd::{FromRawFd, OwnedFd};
107+
use std::os::unix::process::ExitStatusExt;
108+
use std::process::Stdio;
109+
110+
let _serial = common::serial();
111+
let Some(bin) = ufat() else {
112+
return eprintln!("skipping: no ufat binary");
113+
};
114+
let dir = Temp::dir();
115+
let image = dir.path().join("a.img");
116+
let image = image.to_str().unwrap();
117+
ok(&bin, &["assemble", "--create", "--size", "40M", image]);
118+
for args in [
119+
&["describe", image][..],
120+
&["capabilities"],
121+
&["--help"],
122+
&["list", "-h"],
123+
] {
124+
let mut fds = [0; 2];
125+
// SAFETY: pipe2() stores two new descriptors in the array.
126+
assert_eq!(unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) }, 0);
127+
// SAFETY: both descriptors were just created and nothing else owns
128+
// them.
129+
let (read, write) = unsafe { (OwnedFd::from_raw_fd(fds[0]), OwnedFd::from_raw_fd(fds[1])) };
130+
// The reader is gone before ufat starts, so its first write fails.
131+
drop(read);
132+
let out = Command::new(&bin)
133+
.args(args)
134+
.stdout(Stdio::from(write))
135+
.stderr(Stdio::piped())
136+
.output()
137+
.expect("ufat runs");
138+
assert_eq!(
139+
out.status.signal(),
140+
Some(libc::SIGPIPE),
141+
"ufat {:?}: {:?}",
142+
args,
143+
out.status
144+
);
145+
assert!(
146+
out.stderr.is_empty(),
147+
"ufat {:?}: {}",
148+
args,
149+
String::from_utf8_lossy(&out.stderr)
150+
);
151+
}
152+
}
153+
102154
#[test]
103155
fn assemble_and_populate_take_trees_and_manifests() {
104156
let _serial = common::serial();

0 commit comments

Comments
 (0)