Skip to content

Commit

Permalink
Set SO_NOSIGPIPE on all sockets on Apple systems.
Browse files Browse the repository at this point in the history
Previously this option was only set for UDP sockets but is equally
important for TCP. Like `libstd` this PR enables the option on all
sockets if compiled for Apple systems.
  • Loading branch information
twittner authored and Thomasdezeeuw committed May 18, 2020
1 parent 40df934 commit b8bbdcb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
13 changes: 13 additions & 0 deletions src/sys/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ pub(crate) fn new_socket(
#[allow(clippy::let_and_return)]
let socket = syscall!(socket(domain, socket_type, 0));

// Mimick `libstd` and set `SO_NOSIGPIPE` on apple systems.
#[cfg(target_vendor = "apple")]
let socket = socket.and_then(|socket| {
syscall!(setsockopt(
socket,
libc::SOL_SOCKET,
libc::SO_NOSIGPIPE,
&1 as *const libc::c_int as *const libc::c_void,
std::mem::size_of::<libc::c_int>() as libc::socklen_t
))
.map(|_| socket)
});

// Darwin doesn't have SOCK_NONBLOCK or SOCK_CLOEXEC. Not sure about
// Solaris, couldn't find anything online.
#[cfg(any(target_os = "ios", target_os = "macos", target_os = "solaris"))]
Expand Down
13 changes: 0 additions & 13 deletions src/sys/unix/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@ pub fn bind(addr: SocketAddr) -> io::Result<net::UdpSocket> {
#[allow(clippy::let_and_return)]
let socket = new_ip_socket(addr, libc::SOCK_DGRAM);

// Set SO_NOSIGPIPE on iOS and macOS (mirrors what libstd does).
#[cfg(any(target_os = "ios", target_os = "macos"))]
let socket = socket.and_then(|socket| {
syscall!(setsockopt(
socket,
libc::SOL_SOCKET,
libc::SO_NOSIGPIPE,
&1 as *const libc::c_int as *const libc::c_void,
std::mem::size_of::<libc::c_int>() as libc::socklen_t,
))
.map(|_| socket)
});

socket.and_then(|socket| {
let (raw_addr, raw_addr_length) = socket_addr(&addr);
syscall!(bind(socket, raw_addr, raw_addr_length))
Expand Down

0 comments on commit b8bbdcb

Please sign in to comment.