Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
unix_sigpipe: Add docs for init() sigpipe param
  • Loading branch information
Enselic committed Aug 31, 2022
commit 3d1a4e4f2792948d78b3fed030e93c9c156fe35a
2 changes: 2 additions & 0 deletions library/std/src/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ macro_rules! rtunwrap {
// Runs before `main`.
// SAFETY: must be called only once during runtime initialization.
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
// The extra parameter `sigpipe` allows rustc to generate code that instructs std whether
// or not to ignore `SIGPIPE`.
#[cfg_attr(test, allow(dead_code))]
unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to have a comment here explaining the new argument (since unlike the other two it is not standard).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When just reading this file that comment remains rather mysterious though -- there are 256 possible values for sigpipe and it is unclear what they would all mean.

Looks like the concrete meaning is platform-dependent? That should be said, maybe with a reference to the file where unix defines its meanings.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah no actually compiler/rustc_session/src/config/sigpipe.rs is not target-dependent. So that should probably be referenced.

But then I don't understand why the std version of that file is inside unix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RalfJung If you would like to have more of a "full context" kind of doc at this location, that's perfectly fine with me. Would you mind us iterating the exact contents of that comment via GitHub comments rather than commits though?

Here is my proposal on what we could write here:

// # The `sigpipe` parameter
//
// Since 2014, the Rust runtime on Unix has set the `SIGPIPE` handler to
// `SIG_IGN`. Applications have good reasons to want a different behavior
// though, so there is a `#[unix_sigpipe = "..."]` attribute on `fn main()` that
// can be used to select how `SIGPIPE` shall be setup (if changed at all) before
// `fn main()` is called. See <https://github.com/rust-lang/rust/issues/97889>
// for more info.
//
// The `sigpipe` parameter to this function gets its value via the code that
// rustc generates to invoke `fn lang_start()`. The reason we have `sigpipe` for
// all platforms and not only Unix, is because std is not allowed to have `cfg`
// directives as this high level. See the module docs in
// `src/tools/tidy/src/pal.rs` for more info. On all other platforms, `sigpipe`
// has a value, but its value is ignored.
//
// Even though it is an `u8`, it only ever has 3 values. These are documented in
// `compiler/rustc_session/src/config/sigpipe.rs`.

I propose that we do not duplicate this comment also in library/std/src/sys/unix/mod.rs though, i.e. that we only put that comment at the location you made this comment.

Looking forward to your response.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that sounds good! I don't have a strong preference whether library/std/src/rt.rs points to library/std/src/sys/unix/mod.rs or vice versa.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Commit pushed.

unsafe {
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub fn init(argc: isize, argv: *const *const u8, _sigpipe: u8) {}
#[cfg(not(target_os = "espidf"))]
// SAFETY: must be called only once during runtime initialization.
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
// The extra parameter `sigpipe` allows rustc to generate code that instructs std whether
// or not to ignore `SIGPIPE`.
pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
// The standard streams might be closed on application startup. To prevent
// std::io::{stdin, stdout,stderr} objects from using other unrelated file
Expand Down