Skip to content
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
4 changes: 4 additions & 0 deletions changelog.d/+keep-above-stdio.feature.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions docs/orgmode/reference/api.org
Original file line number Diff line number Diff line change
Expand Up @@ -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]] |
Expand Down
22 changes: 10 additions & 12 deletions src/proc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
29 changes: 29 additions & 0 deletions src/proc_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
50 changes: 50 additions & 0 deletions src/sys.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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);
}
Loading