Skip to content

Commit 766e489

Browse files
committed
using SOCK_CLOEXEC path instead
2 parents 7a878ea + 8131774 commit 766e489

File tree

1 file changed

+10
-3
lines changed
  • library/std/src/sys/pal/unix

1 file changed

+10
-3
lines changed

library/std/src/sys/pal/unix/net.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ impl Socket {
7474
if #[cfg(any(
7575
target_os = "android",
7676
target_os = "dragonfly",
77+
target_os = "freebsd",
7778
target_os = "illumos",
7879
target_os = "hurd",
7980
target_os = "linux",
@@ -85,16 +86,22 @@ impl Socket {
8586
// flag to atomically create the socket and set it as
8687
// CLOEXEC. On Linux this was added in 2.6.27.
8788
let fd = cvt(libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0))?;
88-
Ok(Socket(FileDesc::from_raw_fd(fd)))
89+
let socket = Socket(FileDesc::from_raw_fd(fd));
90+
91+
// DragonFlyBSD, FreeBSD and NetBSD use `SO_NOSIGPIPE` as a `setsockopt`
92+
// flag to disable `SIGPIPE` emission on socket.
93+
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd"))]
94+
setsockopt(&socket, libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1)?;
95+
Ok(socket)
8996
} else {
9097
let fd = cvt(libc::socket(fam, ty, 0))?;
9198
let fd = FileDesc::from_raw_fd(fd);
9299
fd.set_cloexec()?;
93100
let socket = Socket(fd);
94101

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

100107
Ok(socket)

0 commit comments

Comments
 (0)