Skip to content

Commit 6de7f60

Browse files
committed
Atomically open files with O_CLOEXEC where possible
On Linux the flag is just ignored if it is not supported: https://lwn.net/Articles/588444/ Touches #24237.
1 parent 63ba780 commit 6de7f60

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/liblibc/lib.rs

+21
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub use types::os::arch::extra::*;
102102
pub use consts::os::c95::*;
103103
pub use consts::os::posix88::*;
104104
pub use consts::os::posix01::*;
105+
pub use consts::os::posix08::*;
105106
pub use consts::os::bsd44::*;
106107
pub use consts::os::extra::*;
107108

@@ -3608,6 +3609,8 @@ pub mod consts {
36083609
pub const RUSAGE_THREAD: c_int = 1;
36093610
}
36103611
pub mod posix08 {
3612+
use types::os::arch::c95::c_int;
3613+
pub const O_CLOEXEC: c_int = 0x80000;
36113614
}
36123615
#[cfg(any(target_arch = "arm",
36133616
target_arch = "aarch64",
@@ -4267,7 +4270,15 @@ pub mod consts {
42674270
pub const RUSAGE_CHILDREN: c_int = -1;
42684271
pub const RUSAGE_THREAD: c_int = 1;
42694272
}
4273+
#[cfg(target_os = "freebsd")]
42704274
pub mod posix08 {
4275+
use types::os::arch::c95::c_int;
4276+
pub const O_CLOEXEC: c_int = 0x100000;
4277+
}
4278+
#[cfg(target_os = "dragonfly")]
4279+
pub mod posix08 {
4280+
use types::os::arch::c95::c_int;
4281+
pub const O_CLOEXEC: c_int = 0x20000;
42714282
}
42724283
pub mod bsd44 {
42734284
use types::os::arch::c95::c_int;
@@ -4710,7 +4721,15 @@ pub mod consts {
47104721
pub const RUSAGE_CHILDREN: c_int = -1;
47114722
pub const RUSAGE_THREAD: c_int = 1;
47124723
}
4724+
#[cfg(any(target_os = "bitrig", target_os = "openbsd"))]
47134725
pub mod posix08 {
4726+
use types::os::arch::c95::c_int;
4727+
pub const O_CLOEXEC: c_int = 0x10000;
4728+
}
4729+
#[cfg(target_os = "netbsd")]
4730+
pub mod posix08 {
4731+
use types::os::arch::c95::c_int;
4732+
pub const O_CLOEXEC: c_int = 0x400000;
47144733
}
47154734
pub mod bsd44 {
47164735
use types::os::arch::c95::c_int;
@@ -5148,6 +5167,8 @@ pub mod consts {
51485167
pub const RUSAGE_THREAD: c_int = 1;
51495168
}
51505169
pub mod posix08 {
5170+
use types::os::arch::c95::c_int;
5171+
pub const O_CLOEXEC: c_int = 0x1000000;
51515172
}
51525173
pub mod bsd44 {
51535174
use types::os::arch::c95::c_int;

src/libstd/sys/unix/fs.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl DirEntry {
212212
impl OpenOptions {
213213
pub fn new() -> OpenOptions {
214214
OpenOptions {
215-
flags: 0,
215+
flags: libc::O_CLOEXEC,
216216
read: false,
217217
write: false,
218218
mode: 0o666,
@@ -269,6 +269,9 @@ impl File {
269269
libc::open(path.as_ptr(), flags, opts.mode)
270270
}));
271271
let fd = FileDesc::new(fd);
272+
// Even though we open with the O_CLOEXEC flag, still set CLOEXEC here,
273+
// in case the open flag is not supported (it's just ignored by the OS
274+
// in that case).
272275
fd.set_cloexec();
273276
Ok(File(fd))
274277
}

0 commit comments

Comments
 (0)