Skip to content

Commit 3d8e0bd

Browse files
committed
Change SIGPIPE ui from #[unix_sigpipe = "..."] to -Zon-broken-pipe=...
In the stabilization attempt of `#[unix_sigpipe = "sig_dfl"]`, a concern was raised 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 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, 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.
1 parent 212638f commit 3d8e0bd

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();
@@ -194,7 +194,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
194194
_ => unreachable!(),
195195
};
196196
if sigpipe_attr_specified {
197-
UNIX_SIGPIPE_ATTR_SPECIFIED.store(true, crate::sync::atomic::Ordering::Relaxed);
197+
ON_BROKEN_PIPE_FLAG_USED.store(true, crate::sync::atomic::Ordering::Relaxed);
198198
}
199199
if let Some(handler) = handler {
200200
rtassert!(signal(libc::SIGPIPE, handler) != libc::SIG_ERR);
@@ -214,7 +214,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
214214
target_os = "fuchsia",
215215
target_os = "horizon",
216216
)))]
217-
static UNIX_SIGPIPE_ATTR_SPECIFIED: crate::sync::atomic::AtomicBool =
217+
static ON_BROKEN_PIPE_FLAG_USED: crate::sync::atomic::AtomicBool =
218218
crate::sync::atomic::AtomicBool::new(false);
219219

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

230230
// 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();
@@ -455,7 +455,7 @@ impl Command {
455455
) -> io::Result<Option<Process>> {
456456
use crate::mem::MaybeUninit;
457457
use crate::sys::weak::weak;
458-
use crate::sys::{self, cvt_nz, unix_sigpipe_attr_specified};
458+
use crate::sys::{self, cvt_nz, on_broken_pipe_flag_used};
459459

460460
if self.get_gid().is_some()
461461
|| self.get_uid().is_some()
@@ -617,11 +617,11 @@ impl Command {
617617
// Inherit the signal mask from this process rather than resetting it (i.e. do not call
618618
// posix_spawnattr_setsigmask).
619619

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

0 commit comments

Comments
 (0)