Skip to content

Commit 0fff73b

Browse files
committed
std: When duplicating fds, skip extra set_cloexec
Similar to the previous commit, if `F_DUPFD_CLOEXEC` succeeds then there's no need for us to then call `set_cloexec` on platforms other than Linux. The bug mentioned of kernels not actually setting the `CLOEXEC` flag has only been repored on Linux, not elsewhere.
1 parent 64d7eca commit 0fff73b

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/libstd/sys/unix/fd.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ impl FileDesc {
7777
// follow a strategy similar to musl [1] where if passing
7878
// F_DUPFD_CLOEXEC causes `fcntl` to return EINVAL it means it's not
7979
// supported (the third parameter, 0, is always valid), so we stop
80-
// trying that. We also *still* call the `set_cloexec` method as
81-
// apparently some kernel at some point stopped setting CLOEXEC even
82-
// though it reported doing so on F_DUPFD_CLOEXEC.
80+
// trying that.
8381
//
8482
// Also note that Android doesn't have F_DUPFD_CLOEXEC, but get it to
8583
// resolve so we at least compile this.
@@ -95,14 +93,25 @@ impl FileDesc {
9593
fd.set_cloexec();
9694
fd
9795
};
98-
static TRY_CLOEXEC: AtomicBool = AtomicBool::new(true);
96+
static TRY_CLOEXEC: AtomicBool =
97+
AtomicBool::new(!cfg!(target_os = "android"));
9998
let fd = self.raw();
100-
if !cfg!(target_os = "android") && TRY_CLOEXEC.load(Ordering::Relaxed) {
99+
if TRY_CLOEXEC.load(Ordering::Relaxed) {
101100
match cvt(unsafe { libc::fcntl(fd, F_DUPFD_CLOEXEC, 0) }) {
101+
// We *still* call the `set_cloexec` method as apparently some
102+
// linux kernel at some point stopped setting CLOEXEC even
103+
// though it reported doing so on F_DUPFD_CLOEXEC.
104+
Ok(fd) => {
105+
return Ok(if cfg!(target_os = "linux") {
106+
make_filedesc(fd)
107+
} else {
108+
FileDesc::new(fd)
109+
})
110+
}
102111
Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {
103112
TRY_CLOEXEC.store(false, Ordering::Relaxed);
104113
}
105-
res => return res.map(make_filedesc),
114+
Err(e) => return Err(e),
106115
}
107116
}
108117
cvt(unsafe { libc::fcntl(fd, libc::F_DUPFD, 0) }).map(make_filedesc)

0 commit comments

Comments
 (0)