From 6de7f609ddc6027b4148b7a458f9f766b44cd09f Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Sun, 23 Aug 2015 20:10:22 +0200 Subject: 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. --- src/libstd/sys/unix/fs.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/sys/unix/fs.rs b/src/libstd/sys/unix/fs.rs index 751b8e48263..cbbdd223dc2 100644 --- a/src/libstd/sys/unix/fs.rs +++ b/src/libstd/sys/unix/fs.rs @@ -212,7 +212,7 @@ impl DirEntry { impl OpenOptions { pub fn new() -> OpenOptions { OpenOptions { - flags: 0, + flags: libc::O_CLOEXEC, read: false, write: false, mode: 0o666, @@ -269,6 +269,9 @@ impl File { libc::open(path.as_ptr(), flags, opts.mode) })); let fd = FileDesc::new(fd); + // Even though we open with the O_CLOEXEC flag, still set CLOEXEC here, + // in case the open flag is not supported (it's just ignored by the OS + // in that case). fd.set_cloexec(); Ok(File(fd)) } -- cgit 1.4.1-3-g733a5