Skip to content

Commit e195aa8

Browse files
committed
Auto merge of #27971 - tbu-:pr_cloexec, r=alexcrichton
On Linux the flag is just ignored if it is not supported: https://lwn.net/Articles/588444/ Still needs the values of O_CLOEXEC on the BSDs. Touches #24237.
2 parents 656c3ac + 6de7f60 commit e195aa8

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

@@ -3611,6 +3612,8 @@ pub mod consts {
36113612
pub const RUSAGE_THREAD: c_int = 1;
36123613
}
36133614
pub mod posix08 {
3615+
use types::os::arch::c95::c_int;
3616+
pub const O_CLOEXEC: c_int = 0x80000;
36143617
}
36153618
#[cfg(any(target_arch = "arm",
36163619
target_arch = "aarch64",
@@ -4270,7 +4273,15 @@ pub mod consts {
42704273
pub const RUSAGE_CHILDREN: c_int = -1;
42714274
pub const RUSAGE_THREAD: c_int = 1;
42724275
}
4276+
#[cfg(target_os = "freebsd")]
42734277
pub mod posix08 {
4278+
use types::os::arch::c95::c_int;
4279+
pub const O_CLOEXEC: c_int = 0x100000;
4280+
}
4281+
#[cfg(target_os = "dragonfly")]
4282+
pub mod posix08 {
4283+
use types::os::arch::c95::c_int;
4284+
pub const O_CLOEXEC: c_int = 0x20000;
42744285
}
42754286
pub mod bsd44 {
42764287
use types::os::arch::c95::c_int;
@@ -4713,7 +4724,15 @@ pub mod consts {
47134724
pub const RUSAGE_CHILDREN: c_int = -1;
47144725
pub const RUSAGE_THREAD: c_int = 1;
47154726
}
4727+
#[cfg(any(target_os = "bitrig", target_os = "openbsd"))]
47164728
pub mod posix08 {
4729+
use types::os::arch::c95::c_int;
4730+
pub const O_CLOEXEC: c_int = 0x10000;
4731+
}
4732+
#[cfg(target_os = "netbsd")]
4733+
pub mod posix08 {
4734+
use types::os::arch::c95::c_int;
4735+
pub const O_CLOEXEC: c_int = 0x400000;
47174736
}
47184737
pub mod bsd44 {
47194738
use types::os::arch::c95::c_int;
@@ -5151,6 +5170,8 @@ pub mod consts {
51515170
pub const RUSAGE_THREAD: c_int = 1;
51525171
}
51535172
pub mod posix08 {
5173+
use types::os::arch::c95::c_int;
5174+
pub const O_CLOEXEC: c_int = 0x1000000;
51545175
}
51555176
pub mod bsd44 {
51565177
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)