Skip to content

Set MSG_NOSIGNAL for UnixStream #140005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@
#![feature(allow_internal_unstable)]
#![feature(asm_experimental_arch)]
#![feature(autodiff)]
#![feature(cfg_match)]
#![feature(cfg_sanitizer_cfi)]
#![feature(cfg_target_thread_local)]
#![feature(cfi_encoding)]
Expand Down
6 changes: 6 additions & 0 deletions library/std/src/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ use crate::time::Duration;
/// Ok(())
/// } // the stream is closed here
/// ```
///
/// # SIGPIPE
///
/// Writes to the underlying socket in `SOCK_STREAM` mode are made with `MSG_NOSIGNAL` flag.
/// This suppresses the emission of the `SIGPIPE` signal when writing to disconnected socket.
/// In some cases getting a `SIGPIPE` would trigger process termination.
#[stable(feature = "rust1", since = "1.0.0")]
pub struct TcpStream(net_imp::TcpStream);

Expand Down
29 changes: 28 additions & 1 deletion library/std/src/os/unix/net/stream.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
cfg_match! {
any(
target_os = "linux",
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "solaris",
target_os = "illumos",
target_os = "haiku",
target_os = "nto",
target_os = "cygwin"
) => {
use libc::MSG_NOSIGNAL;
}
_ => {
const MSG_NOSIGNAL: core::ffi::c_int = 0x0;
}
}

use super::{SocketAddr, sockaddr_un};
#[cfg(any(doc, target_os = "android", target_os = "linux"))]
use super::{SocketAncillary, recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to};
Expand Down Expand Up @@ -41,6 +62,12 @@ use crate::time::Duration;
/// Ok(())
/// }
/// ```
///
/// # SIGPIPE
///
/// Writes to the underlying socket in `SOCK_STREAM` mode are made with `MSG_NOSIGNAL` flag.
/// This suppresses the emission of the `SIGPIPE` signal when writing to disconnected socket.
/// In some cases getting a `SIGPIPE` would trigger process termination.
#[stable(feature = "unix_socket", since = "1.10.0")]
pub struct UnixStream(pub(super) Socket);

Expand Down Expand Up @@ -633,7 +660,7 @@ impl io::Write for UnixStream {
#[stable(feature = "unix_socket", since = "1.10.0")]
impl<'a> io::Write for &'a UnixStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
self.0.send_with_flags(buf, MSG_NOSIGNAL)
}

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
Expand Down
8 changes: 8 additions & 0 deletions library/std/src/sys/net/connection/socket/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ impl Socket {
self.0.duplicate().map(Socket)
}

pub fn send_with_flags(&self, buf: &[u8], flags: c_int) -> io::Result<usize> {
let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
let ret = cvt(unsafe {
libc::send(self.as_raw_fd(), buf.as_ptr() as *const c_void, len, flags)
})?;
Ok(ret as usize)
}

fn recv_with_flags(&self, mut buf: BorrowedCursor<'_>, flags: c_int) -> io::Result<()> {
let ret = cvt(unsafe {
libc::recv(
Expand Down
Loading