Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atomically open files with O_CLOEXEC where possible #27971

Merged
merged 1 commit into from
Aug 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/liblibc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub use types::os::arch::extra::*;
pub use consts::os::c95::*;
pub use consts::os::posix88::*;
pub use consts::os::posix01::*;
pub use consts::os::posix08::*;
pub use consts::os::bsd44::*;
pub use consts::os::extra::*;

Expand Down Expand Up @@ -3608,6 +3609,8 @@ pub mod consts {
pub const RUSAGE_THREAD: c_int = 1;
}
pub mod posix08 {
use types::os::arch::c95::c_int;
pub const O_CLOEXEC: c_int = 0x80000;
}
#[cfg(any(target_arch = "arm",
target_arch = "aarch64",
Expand Down Expand Up @@ -4267,7 +4270,15 @@ pub mod consts {
pub const RUSAGE_CHILDREN: c_int = -1;
pub const RUSAGE_THREAD: c_int = 1;
}
#[cfg(target_os = "freebsd")]
pub mod posix08 {
use types::os::arch::c95::c_int;
pub const O_CLOEXEC: c_int = 0x100000;
}
#[cfg(target_os = "dragonfly")]
pub mod posix08 {
use types::os::arch::c95::c_int;
pub const O_CLOEXEC: c_int = 0x20000;
}
pub mod bsd44 {
use types::os::arch::c95::c_int;
Expand Down Expand Up @@ -4710,7 +4721,15 @@ pub mod consts {
pub const RUSAGE_CHILDREN: c_int = -1;
pub const RUSAGE_THREAD: c_int = 1;
}
#[cfg(any(target_os = "bitrig", target_os = "openbsd"))]
pub mod posix08 {
use types::os::arch::c95::c_int;
pub const O_CLOEXEC: c_int = 0x10000;
}
#[cfg(target_os = "netbsd")]
pub mod posix08 {
use types::os::arch::c95::c_int;
pub const O_CLOEXEC: c_int = 0x400000;
}
pub mod bsd44 {
use types::os::arch::c95::c_int;
Expand Down Expand Up @@ -5148,6 +5167,8 @@ pub mod consts {
pub const RUSAGE_THREAD: c_int = 1;
}
pub mod posix08 {
use types::os::arch::c95::c_int;
pub const O_CLOEXEC: c_int = 0x1000000;
}
pub mod bsd44 {
use types::os::arch::c95::c_int;
Expand Down
5 changes: 4 additions & 1 deletion src/libstd/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, since @alexcrichton pointed out that there are tests for this, would it be appropriate to add [cfg(not(any(target_os = "freebsd", target_os = "dragonfly", ...)))]? perhaps pedantic, but it would assert that constants added here are correct

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some cases an older version of any OS might not implement atomic option even if the constant is correct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, thought of this and forgot to comment. Carry on :(

// in case the open flag is not supported (it's just ignored by the OS
// in that case).
fd.set_cloexec();
Ok(File(fd))
}
Expand Down