Skip to content

Commit dbe2a5b

Browse files
authored
Rollup merge of rust-lang#78572 - de-vri-es:bsd-cloexec, r=m-ou-se
Use SOCK_CLOEXEC and accept4() on more platforms. This PR enables the use of `SOCK_CLOEXEC` and `accept4` on more platforms. ----- Android uses the linux kernel, so it should also support it. DragonflyBSD introduced them in 4.4 (December 2015): https://www.dragonflybsd.org/release44/ FreeBSD introduced them in 10.0 (January 2014): https://wiki.freebsd.org/AtomicCloseOnExec Illumos introduced them in a commit in April 2013, not sure when it was released. It is quite possible that is has always been in Illumos: illumos/illumos-gate@5dbfd19 https://illumos.org/man/3socket/socket https://illumos.org/man/3socket/accept4 NetBSD introduced them in 6.0 (Oktober 2012) and 8.0 (July 2018): https://man.netbsd.org/NetBSD-6.0/socket.2 https://man.netbsd.org/NetBSD-8.0/accept.2 OpenBSD introduced them in 5.7 (May 2015): https://man.openbsd.org/socket https://man.openbsd.org/accept
2 parents 44b1345 + 59c6ae6 commit dbe2a5b

File tree

1 file changed

+32
-6
lines changed
  • library/std/src/sys/unix

1 file changed

+32
-6
lines changed

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

+32-6
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,18 @@ impl Socket {
5555
pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
5656
unsafe {
5757
cfg_if::cfg_if! {
58-
if #[cfg(target_os = "linux")] {
59-
// On Linux we pass the SOCK_CLOEXEC flag to atomically create
60-
// the socket and set it as CLOEXEC, added in 2.6.27.
58+
if #[cfg(any(
59+
target_os = "android",
60+
target_os = "dragonfly",
61+
target_os = "freebsd",
62+
target_os = "illumos",
63+
target_os = "linux",
64+
target_os = "netbsd",
65+
target_os = "opensbd",
66+
))] {
67+
// On platforms that support it we pass the SOCK_CLOEXEC
68+
// flag to atomically create the socket and set it as
69+
// CLOEXEC. On Linux this was added in 2.6.27.
6170
let fd = cvt(libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0))?;
6271
Ok(Socket(FileDesc::new(fd)))
6372
} else {
@@ -83,7 +92,15 @@ impl Socket {
8392
let mut fds = [0, 0];
8493

8594
cfg_if::cfg_if! {
86-
if #[cfg(target_os = "linux")] {
95+
if #[cfg(any(
96+
target_os = "android",
97+
target_os = "dragonfly",
98+
target_os = "freebsd",
99+
target_os = "illumos",
100+
target_os = "linux",
101+
target_os = "netbsd",
102+
target_os = "opensbd",
103+
))] {
87104
// Like above, set cloexec atomically
88105
cvt(libc::socketpair(fam, ty | libc::SOCK_CLOEXEC, 0, fds.as_mut_ptr()))?;
89106
Ok((Socket(FileDesc::new(fds[0])), Socket(FileDesc::new(fds[1]))))
@@ -174,9 +191,18 @@ impl Socket {
174191
pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t) -> io::Result<Socket> {
175192
// Unfortunately the only known way right now to accept a socket and
176193
// atomically set the CLOEXEC flag is to use the `accept4` syscall on
177-
// Linux. This was added in 2.6.28, glibc 2.10 and musl 0.9.5.
194+
// platforms that support it. On Linux, this was added in 2.6.28,
195+
// glibc 2.10 and musl 0.9.5.
178196
cfg_if::cfg_if! {
179-
if #[cfg(target_os = "linux")] {
197+
if #[cfg(any(
198+
target_os = "android",
199+
target_os = "dragonfly",
200+
target_os = "freebsd",
201+
target_os = "illumos",
202+
target_os = "linux",
203+
target_os = "netbsd",
204+
target_os = "opensbd",
205+
))] {
180206
let fd = cvt_r(|| unsafe {
181207
libc::accept4(self.0.raw(), storage, len, libc::SOCK_CLOEXEC)
182208
})?;

0 commit comments

Comments
 (0)