Skip to content

Commit f738e7c

Browse files
authored
Set MSG_NOSIGNAL for UnixSteam
#139956
1 parent 1f76d21 commit f738e7c

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

Diff for: library/std/src/os/unix/net/stream.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,35 @@ use crate::sys::cvt;
2323
use crate::sys::net::Socket;
2424
use crate::sys_common::{AsInner, FromInner};
2525
use crate::time::Duration;
26+
use crate::cmp;
27+
#[cfg(any(
28+
target_os = "linux",
29+
target_os = "android",
30+
target_os = "dragonfly",
31+
target_os = "freebsd",
32+
target_os = "openbsd",
33+
target_os = "netbsd",
34+
target_os = "solaris",
35+
target_os = "illumos",
36+
target_os = "haiku",
37+
target_os = "nto",
38+
target_os = "cygwin"
39+
))]
40+
use libc::MSG_NOSIGNAL;
41+
#[cfg(not(any(
42+
target_os = "linux",
43+
target_os = "android",
44+
target_os = "dragonfly",
45+
target_os = "freebsd",
46+
target_os = "openbsd",
47+
target_os = "netbsd",
48+
target_os = "solaris",
49+
target_os = "illumos",
50+
target_os = "haiku",
51+
target_os = "nto",
52+
target_os = "cygwin"
53+
)))]
54+
const MSG_NOSIGNAL: core::ffi::c_int = 0x0;
2655

2756
/// A Unix stream socket.
2857
///
@@ -633,7 +662,11 @@ impl io::Write for UnixStream {
633662
#[stable(feature = "unix_socket", since = "1.10.0")]
634663
impl<'a> io::Write for &'a UnixStream {
635664
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
636-
self.0.write(buf)
665+
let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
666+
let ret = cvt(unsafe {
667+
c::send(self.0.as_raw(), buf.as_ptr() as *const c_void, len, MSG_NOSIGNAL)
668+
})?;
669+
Ok(ret as usize)
637670
}
638671

639672
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {

0 commit comments

Comments
 (0)