Skip to content

Commit 6f6e261

Browse files
committed
set SO_NOSIGPIPE on apple platforms
1 parent eee2d04 commit 6f6e261

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/libstd/sys/unix/net.rs

Lines changed: 13 additions & 1 deletion
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)