Skip to content

Commit b6c0009

Browse files
Rollup merge of rust-lang#124480 - Enselic:on-broken-pipe, r=jieyouxu
Change `SIGPIPE` ui from `#[unix_sigpipe = "..."]` to `-Zon-broken-pipe=...` In the stabilization [attempt](rust-lang#120832) of `#[unix_sigpipe = "sig_dfl"]`, a concern was [raised ](rust-lang#120832 (comment)) related to using a language attribute for the feature: Long term, we want `fn lang_start()` to be definable by any crate, not just libstd. Having a special language attribute in that case becomes awkward. So as a first step towards the next stabilization attempt, this PR changes the `#[unix_sigpipe = "..."]` attribute to a compiler flag `-Zon-broken-pipe=...` to remove that concern, since now the language is not "contaminated" by this feature. Another point was [also raised](rust-lang#120832 (comment)), namely that the ui should not leak **how** it does things, but rather what the **end effect** is. The new flag uses the proposed naming. This is of course something that can be iterated on further before stabilization. Tracking issue: rust-lang#97889
2 parents 13c7c9f + 3d8e0bd commit b6c0009

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

std/src/rt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ macro_rules! rtunwrap {
7474
//
7575
// Since 2014, the Rust runtime on Unix has set the `SIGPIPE` handler to
7676
// `SIG_IGN`. Applications have good reasons to want a different behavior
77-
// though, so there is a `#[unix_sigpipe = "..."]` attribute on `fn main()` that
77+
// though, so there is a `-Zon-broken-pipe` compiler flag that
7878
// can be used to select how `SIGPIPE` shall be setup (if changed at all) before
7979
// `fn main()` is called. See <https://github.com/rust-lang/rust/issues/97889>
8080
// for more info.

std/src/sys/pal/unix/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
5555
// want!
5656
//
5757
// Hence, we set SIGPIPE to ignore when the program starts up in order
58-
// to prevent this problem. Add `#[unix_sigpipe = "..."]` above `fn main()` to
59-
// alter this behavior.
58+
// to prevent this problem. Use `-Zon-broken-pipe=...` to alter this
59+
// behavior.
6060
reset_sigpipe(sigpipe);
6161

6262
stack_overflow::init();
@@ -190,7 +190,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
190190
_ => unreachable!(),
191191
};
192192
if sigpipe_attr_specified {
193-
UNIX_SIGPIPE_ATTR_SPECIFIED.store(true, crate::sync::atomic::Ordering::Relaxed);
193+
ON_BROKEN_PIPE_FLAG_USED.store(true, crate::sync::atomic::Ordering::Relaxed);
194194
}
195195
if let Some(handler) = handler {
196196
rtassert!(signal(libc::SIGPIPE, handler) != libc::SIG_ERR);
@@ -210,7 +210,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
210210
target_os = "fuchsia",
211211
target_os = "horizon",
212212
)))]
213-
static UNIX_SIGPIPE_ATTR_SPECIFIED: crate::sync::atomic::AtomicBool =
213+
static ON_BROKEN_PIPE_FLAG_USED: crate::sync::atomic::AtomicBool =
214214
crate::sync::atomic::AtomicBool::new(false);
215215

216216
#[cfg(not(any(
@@ -219,8 +219,8 @@ static UNIX_SIGPIPE_ATTR_SPECIFIED: crate::sync::atomic::AtomicBool =
219219
target_os = "fuchsia",
220220
target_os = "horizon",
221221
)))]
222-
pub(crate) fn unix_sigpipe_attr_specified() -> bool {
223-
UNIX_SIGPIPE_ATTR_SPECIFIED.load(crate::sync::atomic::Ordering::Relaxed)
222+
pub(crate) fn on_broken_pipe_flag_used() -> bool {
223+
ON_BROKEN_PIPE_FLAG_USED.load(crate::sync::atomic::Ordering::Relaxed)
224224
}
225225

226226
// SAFETY: must be called only once during runtime cleanup.

std/src/sys/pal/unix/process/process_unix.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,11 @@ impl Command {
353353
// Inherit the signal mask from the parent rather than resetting it (i.e. do not call
354354
// pthread_sigmask).
355355

356-
// If #[unix_sigpipe] is specified, don't reset SIGPIPE to SIG_DFL.
357-
// If #[unix_sigpipe] is not specified, reset SIGPIPE to SIG_DFL for backward compatibility.
356+
// If -Zon-broken-pipe is used, don't reset SIGPIPE to SIG_DFL.
357+
// If -Zon-broken-pipe is not used, reset SIGPIPE to SIG_DFL for backward compatibility.
358358
//
359-
// #[unix_sigpipe] is an opportunity to change the default here.
360-
if !crate::sys::pal::unix_sigpipe_attr_specified() {
359+
// -Zon-broken-pipe is an opportunity to change the default here.
360+
if !crate::sys::pal::on_broken_pipe_flag_used() {
361361
#[cfg(target_os = "android")] // see issue #88585
362362
{
363363
let mut action: libc::sigaction = mem::zeroed();
@@ -450,7 +450,7 @@ impl Command {
450450
) -> io::Result<Option<Process>> {
451451
use crate::mem::MaybeUninit;
452452
use crate::sys::weak::weak;
453-
use crate::sys::{self, cvt_nz, unix_sigpipe_attr_specified};
453+
use crate::sys::{self, cvt_nz, on_broken_pipe_flag_used};
454454

455455
if self.get_gid().is_some()
456456
|| self.get_uid().is_some()
@@ -612,11 +612,11 @@ impl Command {
612612
// Inherit the signal mask from this process rather than resetting it (i.e. do not call
613613
// posix_spawnattr_setsigmask).
614614

615-
// If #[unix_sigpipe] is specified, don't reset SIGPIPE to SIG_DFL.
616-
// If #[unix_sigpipe] is not specified, reset SIGPIPE to SIG_DFL for backward compatibility.
615+
// If -Zon-broken-pipe is used, don't reset SIGPIPE to SIG_DFL.
616+
// If -Zon-broken-pipe is not used, reset SIGPIPE to SIG_DFL for backward compatibility.
617617
//
618-
// #[unix_sigpipe] is an opportunity to change the default here.
619-
if !unix_sigpipe_attr_specified() {
618+
// -Zon-broken-pipe is an opportunity to change the default here.
619+
if !on_broken_pipe_flag_used() {
620620
let mut default_set = MaybeUninit::<libc::sigset_t>::uninit();
621621
cvt(sigemptyset(default_set.as_mut_ptr()))?;
622622
cvt(sigaddset(default_set.as_mut_ptr(), libc::SIGPIPE))?;

0 commit comments

Comments
 (0)