Skip to content

Commit ed5e542

Browse files
committed
MSG_NOSIGNAL on linux
1 parent 6f6e261 commit ed5e542

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/libstd/sys/common/net.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ use sys::net::netc::IPV6_LEAVE_GROUP as IPV6_DROP_MEMBERSHIP;
4242
target_os = "solaris", target_os = "haiku")))]
4343
use sys::net::netc::IPV6_DROP_MEMBERSHIP;
4444

45+
#[cfg(target_os = "linux")]
46+
const MSG_NOSIGNAL: c_int = 0x4000;
47+
#[cfg(not(target_os = "linux"))]
48+
const MSG_NOSIGNAL: c_int = 0x0; // unused dummy value
49+
4550
////////////////////////////////////////////////////////////////////////////////
4651
// sockaddr and misc bindings
4752
////////////////////////////////////////////////////////////////////////////////
@@ -221,11 +226,12 @@ impl TcpStream {
221226

222227
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
223228
let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
229+
let flags = if cfg!(target_os = "linux") { MSG_NOSIGNAL } else { 0 };
224230
let ret = cvt(unsafe {
225231
c::send(*self.inner.as_inner(),
226232
buf.as_ptr() as *const c_void,
227233
len,
228-
0)
234+
flags)
229235
})?;
230236
Ok(ret as usize)
231237
}
@@ -446,10 +452,11 @@ impl UdpSocket {
446452
pub fn send_to(&self, buf: &[u8], dst: &SocketAddr) -> io::Result<usize> {
447453
let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
448454
let (dstp, dstlen) = dst.into_inner();
455+
let flags = if cfg!(target_os = "linux") { MSG_NOSIGNAL } else { 0 };
449456
let ret = cvt(unsafe {
450457
c::sendto(*self.inner.as_inner(),
451458
buf.as_ptr() as *const c_void, len,
452-
0, dstp, dstlen)
459+
flags, dstp, dstlen)
453460
})?;
454461
Ok(ret as usize)
455462
}
@@ -569,11 +576,12 @@ impl UdpSocket {
569576

570577
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
571578
let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
579+
let flags = if cfg!(target_os = "linux") { MSG_NOSIGNAL } else { 0 };
572580
let ret = cvt(unsafe {
573581
c::send(*self.inner.as_inner(),
574582
buf.as_ptr() as *const c_void,
575583
len,
576-
0)
584+
flags)
577585
})?;
578586
Ok(ret as usize)
579587
}

src/libstd/sys/unix/ext/net.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ use sys::cvt;
2828
use sys::net::Socket;
2929
use sys_common::{AsInner, FromInner, IntoInner};
3030

31+
#[cfg(target_os = "linux")]
32+
const MSG_NOSIGNAL: libc::c_int = 0x4000;
33+
#[cfg(not(target_os = "linux"))]
34+
const MSG_NOSIGNAL: libc::c_int = 0x0; // unused dummy value
35+
3136
fn sun_path_offset() -> usize {
3237
unsafe {
3338
// Work with an actual instance of the type since using a null pointer is UB
@@ -686,11 +691,12 @@ impl UnixDatagram {
686691
fn inner(d: &UnixDatagram, buf: &[u8], path: &Path) -> io::Result<usize> {
687692
unsafe {
688693
let (addr, len) = sockaddr_un(path)?;
694+
let flags = if cfg!(target_os = "linux") { MSG_NOSIGNAL } else { 0 };
689695

690696
let count = cvt(libc::sendto(*d.0.as_inner(),
691697
buf.as_ptr() as *const _,
692698
buf.len(),
693-
0,
699+
flags,
694700
&addr as *const _ as *const _,
695701
len))?;
696702
Ok(count as usize)

0 commit comments

Comments
 (0)