From 23a81b439638fee89054ad749f0348ab7d107619 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 24 Aug 2020 02:27:26 +1000 Subject: [PATCH 1/2] std: refactor fs.openFileZ flag handling --- lib/std/fs.zig | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/std/fs.zig b/lib/std/fs.zig index df368e070b..d9c949ef33 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -686,21 +686,25 @@ pub const Dir = struct { return self.openFileW(path_w.span(), flags); } + var os_flags: u32 = os.O_CLOEXEC; // Use the O_ locking flags if the os supports them // (Or if it's darwin, as darwin's `open` doesn't support the O_SYNC flag) const has_flock_open_flags = @hasDecl(os, "O_EXLOCK") and !is_darwin; - const nonblocking_lock_flag = if (has_flock_open_flags and flags.lock_nonblocking) - os.O_NONBLOCK | os.O_SYNC - else - @as(u32, 0); - const lock_flag: u32 = if (has_flock_open_flags) switch (flags.lock) { - .None => @as(u32, 0), - .Shared => os.O_SHLOCK | nonblocking_lock_flag, - .Exclusive => os.O_EXLOCK | nonblocking_lock_flag, - } else 0; - - const O_LARGEFILE = if (@hasDecl(os, "O_LARGEFILE")) os.O_LARGEFILE else 0; - const os_flags = lock_flag | O_LARGEFILE | os.O_CLOEXEC | if (flags.write and flags.read) + if (has_flock_open_flags) { + const nonblocking_lock_flag = if (flags.lock_nonblocking) + os.O_NONBLOCK | os.O_SYNC + else + @as(u32, 0); + os_flags |= switch (flags.lock) { + .None => @as(u32, 0), + .Shared => os.O_SHLOCK | nonblocking_lock_flag, + .Exclusive => os.O_EXLOCK | nonblocking_lock_flag, + }; + } + if (@hasDecl(os, "O_LARGEFILE")) { + os_flags |= os.O_LARGEFILE; + } + os_flags |= if (flags.write and flags.read) @as(u32, os.O_RDWR) else if (flags.write) @as(u32, os.O_WRONLY) From 129d3e274dbc3e11a30e3d54c5da7a944ddcf4f0 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 24 Aug 2020 02:28:31 +1000 Subject: [PATCH 2/2] std: use O_NOCTTY flag --- lib/std/fs.zig | 3 +++ lib/std/fs/file.zig | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/lib/std/fs.zig b/lib/std/fs.zig index d9c949ef33..21a00eeb1d 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -704,6 +704,9 @@ pub const Dir = struct { if (@hasDecl(os, "O_LARGEFILE")) { os_flags |= os.O_LARGEFILE; } + if (!flags.allow_ctty) { + os_flags |= os.O_NOCTTY; + } os_flags |= if (flags.write and flags.read) @as(u32, os.O_RDWR) else if (flags.write) diff --git a/lib/std/fs/file.zig b/lib/std/fs/file.zig index 3e22528ea9..6fb2385a85 100644 --- a/lib/std/fs/file.zig +++ b/lib/std/fs/file.zig @@ -101,6 +101,10 @@ pub const File = struct { /// if `std.io.is_async`. It allows the use of `nosuspend` when calling functions /// related to opening the file, reading, writing, and locking. intended_io_mode: io.ModeOverride = io.default_mode, + + /// Set this to allow the opened file to automatically become the + /// controlling TTY for the current process. + allow_ctty: bool = false, }; /// TODO https://github.com/ziglang/zig/issues/3802