From cd2f59ea096ad1558a24573d2d8dfddc8221ff73 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Fri, 20 Nov 2026 11:11:18 +0000 Subject: [PATCH] feat: add sys.keepAboveStdio for descriptors below 3 When standard input, output, or error starts closed, the next open or pipe takes slot 0, 1, or 2. Later child setup or standard-stream writes can clobber that descriptor. Both places in proc moved low descriptors by hand, childExec with F_DUPFD and run's exec-failure pipe with F_DUPFD_CLOEXEC, and front ends holding flock files need the same step. Add sys.keepAboveStdio to duplicate a descriptor below 3 to 3 or above with F_DUPFD_CLOEXEC, retrying EINTR, and close the low slot. A descriptor already at 3 or above returns unchanged, and a failed call leaves the original descriptor open. Both sites in proc call keepAboveStdio, and childExec updates shared slots when standard input and standard error share /dev/null. Tests cover moving slots 0, 1, and 2 with FD_CLOEXEC set, leaving high descriptors unchanged, reporting EBADF on closed slots, and running a child with all three standard streams closed. --- changelog.d/+keep-above-stdio.feature.md | 4 ++ docs/orgmode/reference/api.org | 1 + src/proc.zig | 22 +++++------ src/proc_test.zig | 29 ++++++++++++++ src/sys.zig | 50 ++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 changelog.d/+keep-above-stdio.feature.md diff --git a/changelog.d/+keep-above-stdio.feature.md b/changelog.d/+keep-above-stdio.feature.md new file mode 100644 index 0000000..c2cd4c8 --- /dev/null +++ b/changelog.d/+keep-above-stdio.feature.md @@ -0,0 +1,4 @@ +`sys.keepAboveStdio` moves an open descriptor in slots 0, 1 or 2 to 3 +or above with `FD_CLOEXEC` set and closes the original slot, returning +descriptors already at 3 or above unchanged; `proc.run` uses it for its +child setup and exec-failure pipe. diff --git a/docs/orgmode/reference/api.org b/docs/orgmode/reference/api.org index 7bafcc4..5dc3ac3 100644 --- a/docs/orgmode/reference/api.org +++ b/docs/orgmode/reference/api.org @@ -30,6 +30,7 @@ Error set: [[zig:zig_common.sys.Error]]. | [[zig:zig_common.sys.z]] | [[zig:zig_common.sys.Error]] | | [[zig:zig_common.sys.openat]] | [[zig:zig_common.sys.Error]] | | [[zig:zig_common.sys.close]] | none | +| [[zig:zig_common.sys.keepAboveStdio]] | [[zig:zig_common.sys.Error]] | | [[zig:zig_common.sys.read]] | [[zig:zig_common.sys.Error]] | | [[zig:zig_common.sys.writeAll]] | [[zig:zig_common.sys.Error]] | | [[zig:zig_common.sys.fchmod]] | [[zig:zig_common.sys.Error]] | diff --git a/src/proc.zig b/src/proc.zig index 0a70b66..7b9e07d 100644 --- a/src/proc.zig +++ b/src/proc.zig @@ -315,10 +315,12 @@ fn childExec( // closed) moves above 2 first: a dup2 onto it would clobber it, and a // dup2 onto itself keeps its close-on-exec flag. var src = [3]sys.fd_t{ in_fd, out_fd, err_fd }; - for (&src) |*fd| if (fd.* < 3) { - const moved = linux.fcntl(fd.*, linux.F.DUPFD, 3); - if (linux.errno(moved) != .SUCCESS) linux.exit_group(127); - fd.* = @intCast(moved); + for (&src, 0..) |*fd, i| if (fd.* < 3) { + const old = fd.*; + fd.* = sys.keepAboveStdio(old) catch linux.exit_group(127); + for (src[i + 1 ..]) |*later| if (later.* == old) { + later.* = fd.*; + }; }; for (src, 0..) |fd, target| { if (linux.errno(linux.dup2(fd, @intCast(target))) != .SUCCESS) linux.exit_group(127); @@ -524,15 +526,11 @@ pub fn run(gpa: Allocator, exe: []const u8, argv: []const []const u8, envp: Envp defer if (exec_p) |p| sys.close(p.r); if (opts.exec_error) { var p = try pipe(); - if (p.w < 3) { - const moved = sys.call(linux.fcntl, .{ p.w, linux.F.DUPFD_CLOEXEC, @as(usize, 3) }) catch |e| { - sys.close(p.r); - sys.close(p.w); - return e; - }; + p.w = sys.keepAboveStdio(p.w) catch |e| { + sys.close(p.r); sys.close(p.w); - p.w = @intCast(moved); - } + return e; + }; exec_p = p; } diff --git a/src/proc_test.zig b/src/proc_test.zig index 2031ad3..3440b9f 100644 --- a/src/proc_test.zig +++ b/src/proc_test.zig @@ -466,3 +466,32 @@ test "stdin to a child that does not read it fails with EPIPE, not SIGPIPE" { _ = linux.sigaction(.PIPE, null, &after); try testing.expect(after.handler.handler == linux.SIG.DFL); } + +test "run with all standard streams closed moves shared devnull and pipes above 2" { + const pid: isize = @bitCast(linux.fork()); + if (pid == 0) { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const a = arena.allocator(); + sys.close(0); + sys.close(1); + sys.close(2); + const low = sys.keepAboveStdio(sys.openat(linux.AT.FDCWD, "/dev/null", .{}, 0) catch linux.exit_group(10)) catch linux.exit_group(11); + defer sys.close(low); + if (low < 3) linux.exit_group(12); + // With 0, 1 and 2 closed and stderr uncaptured, stdin and stderr share + // devnull at slot 0 while stdout's pipe occupies slots 1 and 2. + const res = proc.run(a, "/bin/sh", &.{ "sh", "-c", "printf ok && printf drop >&2" }, no_env, .{}) catch linux.exit_group(13); + if (res.term.code() != 0 or !std.mem.eql(u8, res.stdout, "ok")) linux.exit_group(14); + if (proc.run(a, "/no/such/zig-common-program", &.{"missing"}, no_env, .{ .exec_error = true })) |_| { + linux.exit_group(15); + } else |err| { + if (err != error.NotFound) linux.exit_group(16); + } + linux.exit_group(0); + } + try testing.expect(pid > 0); + var status: i32 = 0; + _ = try sys.call(linux.wait4, .{ @as(linux.pid_t, @intCast(pid)), &status, @as(u32, 0), @as(?*linux.rusage, null) }); + try testing.expectEqual(@as(i32, 0), status); +} diff --git a/src/sys.zig b/src/sys.zig index 6288c00..eb08161 100644 --- a/src/sys.zig +++ b/src/sys.zig @@ -353,6 +353,17 @@ pub fn close(fd: fd_t) void { _ = linux.close(fd); } +/// Move an open descriptor out of the standard-stream slots 0, 1 and 2. +/// A descriptor already at 3 or above is returned unchanged; one in 0..2 +/// is duplicated to 3 or above with `FD_CLOEXEC` set and closed at its +/// original slot. On error the original descriptor is left open. +pub fn keepAboveStdio(fd: fd_t) Error!fd_t { + if (fd >= 3) return fd; + const moved = try call(linux.fcntl, .{ fd, linux.F.DUPFD_CLOEXEC, @as(usize, 3) }); + close(fd); + return @intCast(moved); +} + /// read(2) into `buf`: the number of bytes read, 0 at the end of the file. pub fn read(fd: fd_t, buf: []u8) Error!usize { return call(linux.read, .{ fd, buf.ptr, buf.len }); @@ -486,3 +497,42 @@ test "errnoText names success, returns null for unknown errnos and sys wrappers try std.testing.expectError(error.NoSpace, writeAll(full_fd, "x")); } else |_| {} } + +test "keepAboveStdio moves descriptors 0, 1 and 2 above 2 with close-on-exec" { + const high_fd = try openat(linux.AT.FDCWD, "/dev/null", .{}, 0); + defer close(high_fd); + try std.testing.expect(high_fd >= 3); + try std.testing.expectEqual(high_fd, try keepAboveStdio(high_fd)); + try std.testing.expectError(error.InvalidArgument, keepAboveStdio(-1)); + try std.testing.expectEqual(linux.E.BADF, last_errno); + + const pid: isize = @bitCast(linux.fork()); + if (pid == 0) { + close(0); + close(1); + close(2); + const a = openat(linux.AT.FDCWD, "/dev/null", .{}, 0) catch linux.exit_group(10); + const b = openat(linux.AT.FDCWD, "/dev/null", .{}, 0) catch linux.exit_group(11); + const c = openat(linux.AT.FDCWD, "/dev/null", .{}, 0) catch linux.exit_group(12); + if (a != 0 or b != 1 or c != 2) linux.exit_group(13); + + for ([_]fd_t{ a, b, c }) |low| { + const moved = keepAboveStdio(low) catch linux.exit_group(14); + if (moved < 3) linux.exit_group(15); + const flags = linux.fcntl(moved, linux.F.GETFD, 0); + if (linux.errno(flags) != .SUCCESS or (flags & linux.FD_CLOEXEC) == 0) linux.exit_group(16); + if (linux.errno(linux.fcntl(low, linux.F.GETFD, 0)) != .BADF) linux.exit_group(17); + defer close(moved); + } + + _ = keepAboveStdio(0) catch |err| { + if (err != error.InvalidArgument or last_errno != .BADF) linux.exit_group(18); + linux.exit_group(0); + }; + linux.exit_group(19); + } + try std.testing.expect(pid > 0); + var status: i32 = 0; + _ = try call(linux.wait4, .{ @as(linux.pid_t, @intCast(pid)), &status, @as(u32, 0), @as(?*linux.rusage, null) }); + try std.testing.expectEqual(@as(i32, 0), status); +}