Skip to content

Commit

Permalink
using SOCK_CLOEXEC path instead
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Apr 28, 2024
2 parents 7a878ea + 8131774 commit 766e489
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions library/std/src/sys/pal/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl Socket {
if #[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "illumos",
target_os = "hurd",
target_os = "linux",
Expand All @@ -85,16 +86,22 @@ impl Socket {
// flag to atomically create the socket and set it as
// CLOEXEC. On Linux this was added in 2.6.27.
let fd = cvt(libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0))?;
Ok(Socket(FileDesc::from_raw_fd(fd)))
let socket = Socket(FileDesc::from_raw_fd(fd));

// DragonFlyBSD, FreeBSD and NetBSD use `SO_NOSIGPIPE` as a `setsockopt`
// flag to disable `SIGPIPE` emission on socket.
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd"))]
setsockopt(&socket, libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1)?;
Ok(socket)
} else {
let fd = cvt(libc::socket(fam, ty, 0))?;
let fd = FileDesc::from_raw_fd(fd);
fd.set_cloexec()?;
let socket = Socket(fd);

// macOS, iOS and FreeBSD use `SO_NOSIGPIPE` as a `setsockopt`
// macOS and iOS use `SO_NOSIGPIPE` as a `setsockopt`
// flag to disable `SIGPIPE` emission on socket.
#[cfg(any(target_vendor = "apple", target_os = "freebsd"))]
#[cfg(target_vendor = "apple")]
setsockopt(&socket, libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1)?;

Ok(socket)
Expand Down

0 comments on commit 766e489

Please sign in to comment.