authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-23 23:26:58-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-08-23 23:26:58-04:00
log4d7013f7d3e44fc1763011bdf63f5431678ef946
treefb74dabb7c3ee48b0e7685ccfd49baefb6356c78
parent385786eedc7927a76006741218b949a87d42ad76
parent129d3e274dbc3e11a30e3d54c5da7a944ddcf4f0
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6146 from daurnimator/no_ctty

Use O_NOCTTY open flag

2 files changed, 23 insertions(+), 12 deletions(-)

lib/std/fs.zig+19-12
......@@ -686,21 +686,28 @@ pub const Dir = struct {
686686 return self.openFileW(path_w.span(), flags);
687687 }
688688
689 var os_flags: u32 = os.O_CLOEXEC;
689690 // Use the O_ locking flags if the os supports them
690691 // (Or if it's darwin, as darwin's `open` doesn't support the O_SYNC flag)
691692 const has_flock_open_flags = @hasDecl(os, "O_EXLOCK") and !is_darwin;
692 const nonblocking_lock_flag = if (has_flock_open_flags and flags.lock_nonblocking)
693 os.O_NONBLOCK | os.O_SYNC
694 else
695 @as(u32, 0);
696 const lock_flag: u32 = if (has_flock_open_flags) switch (flags.lock) {
697 .None => @as(u32, 0),
698 .Shared => os.O_SHLOCK | nonblocking_lock_flag,
699 .Exclusive => os.O_EXLOCK | nonblocking_lock_flag,
700 } else 0;
701
702 const O_LARGEFILE = if (@hasDecl(os, "O_LARGEFILE")) os.O_LARGEFILE else 0;
703 const os_flags = lock_flag | O_LARGEFILE | os.O_CLOEXEC | if (flags.write and flags.read)
693 if (has_flock_open_flags) {
694 const nonblocking_lock_flag = if (flags.lock_nonblocking)
695 os.O_NONBLOCK | os.O_SYNC
696 else
697 @as(u32, 0);
698 os_flags |= switch (flags.lock) {
699 .None => @as(u32, 0),
700 .Shared => os.O_SHLOCK | nonblocking_lock_flag,
701 .Exclusive => os.O_EXLOCK | nonblocking_lock_flag,
702 };
703 }
704 if (@hasDecl(os, "O_LARGEFILE")) {
705 os_flags |= os.O_LARGEFILE;
706 }
707 if (!flags.allow_ctty) {
708 os_flags |= os.O_NOCTTY;
709 }
710 os_flags |= if (flags.write and flags.read)
704711 @as(u32, os.O_RDWR)
705712 else if (flags.write)
706713 @as(u32, os.O_WRONLY)
lib/std/fs/file.zig+4
......@@ -101,6 +101,10 @@ pub const File = struct {
101101 /// if `std.io.is_async`. It allows the use of `nosuspend` when calling functions
102102 /// related to opening the file, reading, writing, and locking.
103103 intended_io_mode: io.ModeOverride = io.default_mode,
104
105 /// Set this to allow the opened file to automatically become the
106 /// controlling TTY for the current process.
107 allow_ctty: bool = false,
104108 };
105109
106110 /// TODO https://github.com/ziglang/zig/issues/3802