Skip to content

Commit

Permalink
unix_sigpipe: Add test for SIGPIPE disposition in child processes
Browse files Browse the repository at this point in the history
For robustness, also test the disposition in our own process even if
other tests in `tests/ui/attributes/unix_sigpipe` already covers it.
  • Loading branch information
Enselic committed Mar 11, 2024
1 parent bf60149 commit 0dc26de
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// It is UB to unwind out of `fn start()` according to
// https://doc.rust-lang.org/beta/unstable-book/language-features/start.html so
// panic with abort to avoid UB:
//@ compile-flags:-C panic=abort
//@ no-prefer-dynamic

#![crate_type = "bin"]
#![feature(start, rustc_private)]

extern crate libc;

// Use #[start] so we don't have a runtime that messes with SIGPIPE.
#[start]
fn start(argc: isize, argv: *const *const u8) -> isize {
assert_eq!(argc, 2, "Must pass SIG_IGN or SIG_DFL as first arg");
let arg1 = unsafe { std::ffi::CStr::from_ptr(*argv.offset(1) as *const libc::c_char) }
.to_str()
.unwrap();

let expected = match arg1 {
"SIG_IGN" => libc::SIG_IGN,
"SIG_DFL" => libc::SIG_DFL,
arg => panic!("Must pass SIG_IGN or SIG_DFL as first arg. Got: {}", arg),
};

let actual = unsafe {
let mut actual: libc::sigaction = std::mem::zeroed();
libc::sigaction(libc::SIGPIPE, std::ptr::null(), &mut actual);
actual.sa_sigaction
};

assert_eq!(actual, expected, "actual and expected SIGPIPE disposition in child differs");

0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//@ revisions: default sig_dfl sig_ign inherit
//@ aux-build:assert-sigpipe-disposition-bin.rs
//@ aux-build:sigpipe-utils.rs
//@ ignore-cross-compile because we run the compiled code
//@ only-unix because SIGPIPE is a unix thing
//@ run-pass

// Checks the signal disposition of `SIGPIPE` in child processes, and in our own
// process for robustness. Without any `unix_sigpipe` attribute, `SIG_IGN` is
// the default. But there is a difference in how `SIGPIPE` is treated in child
// processes with and without the attribute. Search for
// `unix_sigpipe_attr_specified()` in the code base to learn more.

#![feature(rustc_private)]
#![cfg_attr(any(sig_dfl, sig_ign, inherit), feature(unix_sigpipe))]

extern crate libc;

// We need `aux-build` for `assert-sigpipe-disposition-bin`, and `compiletest`
// gets confused if we use both `aux-build` and `aux-crate` in the same test, so
// we need to use the old style of bringing `sigpipe_utils` in scope.
extern crate sigpipe_utils;

use sigpipe_utils::*;

#[cfg_attr(sig_dfl, unix_sigpipe = "sig_dfl")]
#[cfg_attr(sig_ign, unix_sigpipe = "sig_ign")]
#[cfg_attr(inherit, unix_sigpipe = "inherit")]
fn main() {
// By default, we get SIG_IGN but the child gets SIG_DFL.
#[cfg(default)]
let (we_expect, child_expects) = (SignalHandler::Ignore, "SIG_DFL");

// With #[unix_sigpipe = "sig_dfl"] we get SIG_DFL and the child does too.
#[cfg(sig_dfl)]
let (we_expect, child_expects) = (SignalHandler::Default, "SIG_DFL");

// With #[unix_sigpipe = "sig_ign"] we get SIG_IGN and the child does too.
#[cfg(sig_ign)]
let (we_expect, child_expects) = (SignalHandler::Ignore, "SIG_IGN");

// With #[unix_sigpipe = "inherit"] we get SIG_DFL and the child does too.
#[cfg(inherit)]
let (we_expect, child_expects) = (SignalHandler::Default, "SIG_DFL");

assert_sigpipe_handler(we_expect);

assert!(
std::process::Command::new("./auxiliary/assert-sigpipe-disposition-bin")
.arg(child_expects)
.status()
.unwrap()
.success()
);
}

0 comments on commit 0dc26de

Please sign in to comment.