Skip to content

Commit 5045d4e

Browse files
authored
Auto merge of rust-lang#36824 - kali:master, r=alexcrichton
SO_NOSIGPIPE and MSG_NOSIGNAL (rebased rust-lang#36426) I'm not sure what happened when I pushed a rebased branch on rust-lang#36426 , github closed it...
2 parents 8b00355 + 5980d5b commit 5045d4e

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

src/libstd/sys/common/net.rs

+8-3
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+
use libc::MSG_NOSIGNAL;
47+
#[cfg(not(target_os = "linux"))]
48+
const MSG_NOSIGNAL: c_int = 0x0; // unused dummy value
49+
4550
////////////////////////////////////////////////////////////////////////////////
4651
// sockaddr and misc bindings
4752
////////////////////////////////////////////////////////////////////////////////
@@ -225,7 +230,7 @@ impl TcpStream {
225230
c::send(*self.inner.as_inner(),
226231
buf.as_ptr() as *const c_void,
227232
len,
228-
0)
233+
MSG_NOSIGNAL)
229234
})?;
230235
Ok(ret as usize)
231236
}
@@ -449,7 +454,7 @@ impl UdpSocket {
449454
let ret = cvt(unsafe {
450455
c::sendto(*self.inner.as_inner(),
451456
buf.as_ptr() as *const c_void, len,
452-
0, dstp, dstlen)
457+
MSG_NOSIGNAL, dstp, dstlen)
453458
})?;
454459
Ok(ret as usize)
455460
}
@@ -573,7 +578,7 @@ impl UdpSocket {
573578
c::send(*self.inner.as_inner(),
574579
buf.as_ptr() as *const c_void,
575580
len,
576-
0)
581+
MSG_NOSIGNAL)
577582
})?;
578583
Ok(ret as usize)
579584
}

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

+6-1
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+
use libc::MSG_NOSIGNAL;
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
@@ -690,7 +695,7 @@ impl UnixDatagram {
690695
let count = cvt(libc::sendto(*d.0.as_inner(),
691696
buf.as_ptr() as *const _,
692697
buf.len(),
693-
0,
698+
MSG_NOSIGNAL,
694699
&addr as *const _ as *const _,
695700
len))?;
696701
Ok(count as usize)

src/libstd/sys/unix/net.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ use libc::SOCK_CLOEXEC;
3333
#[cfg(not(target_os = "linux"))]
3434
const SOCK_CLOEXEC: c_int = 0;
3535

36+
// Another conditional contant for name resolution: Macos et iOS use
37+
// SO_NOSIGPIPE as a setsockopt flag to disable SIGPIPE emission on socket.
38+
// Other platforms do otherwise.
39+
#[cfg(target_vendor = "apple")]
40+
use libc::SO_NOSIGPIPE;
41+
#[cfg(not(target_vendor = "apple"))]
42+
const SO_NOSIGPIPE: c_int = 0;
43+
3644
pub struct Socket(FileDesc);
3745

3846
pub fn init() {}
@@ -81,7 +89,11 @@ impl Socket {
8189
let fd = cvt(libc::socket(fam, ty, 0))?;
8290
let fd = FileDesc::new(fd);
8391
fd.set_cloexec()?;
84-
Ok(Socket(fd))
92+
let socket = Socket(fd);
93+
if cfg!(target_vendor = "apple") {
94+
setsockopt(&socket, libc::SOL_SOCKET, SO_NOSIGPIPE, 1)?;
95+
}
96+
Ok(socket)
8597
}
8698
}
8799

0 commit comments

Comments
 (0)