Skip to content

Commit 00315e3

Browse files
committed
remove posix exposure in child_process
reason: nobody came up with a use case yet
1 parent 6a709f0 commit 00315e3

File tree

4 files changed

+14
-37
lines changed

4 files changed

+14
-37
lines changed

lib/std/child_process.zig

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@ pub const ChildProcess = struct {
5757

5858
expand_arg0: Arg0Expand,
5959

60-
/// Use this for additional posix spawn attributes.
61-
/// Do not set spawn attribute flags and do not modify stdin, stdout, stderr
62-
/// behavior, because those are set in the platform-specific spawn method.
63-
posix_attr: if (os.hasPosixSpawn) ?os.posix_spawn.Attr else void,
64-
posix_actions: if (os.hasPosixSpawn) ?os.posix_spawn.Actions else void,
65-
6660
/// Darwin-only. Disable ASLR for the child process.
6761
disable_aslr: bool = false,
6862

@@ -132,8 +126,6 @@ pub const ChildProcess = struct {
132126
.stdout_behavior = StdIo.Inherit,
133127
.stderr_behavior = StdIo.Inherit,
134128
.expand_arg0 = .no_expand,
135-
.posix_attr = if (os.hasPosixSpawn) null else undefined,
136-
.posix_actions = if (os.hasPosixSpawn) null else undefined,
137129
};
138130
}
139131

@@ -149,7 +141,7 @@ pub const ChildProcess = struct {
149141
@compileError("the target operating system cannot spawn processes");
150142
}
151143

152-
if (os.hasPosixSpawn) {
144+
if (comptime builtin.target.isDarwin()) {
153145
return self.spawnMacos();
154146
}
155147

@@ -481,7 +473,7 @@ pub const ChildProcess = struct {
481473
}
482474

483475
fn waitUnwrapped(self: *ChildProcess) !void {
484-
const res: os.WaitPidResult = if (os.hasPosixSpawn)
476+
const res: os.WaitPidResult = if (comptime builtin.target.isDarwin())
485477
try os.posix_spawn.waitpid(self.pid, 0)
486478
else
487479
os.waitpid(self.pid, 0);
@@ -561,10 +553,6 @@ pub const ChildProcess = struct {
561553
}
562554

563555
fn spawnMacos(self: *ChildProcess) SpawnError!void {
564-
// dont cleanup structure owned == initialized by user
565-
const user_attr: bool = self.posix_attr != null;
566-
const user_actions: bool = self.posix_actions != null;
567-
568556
const pipe_flags = if (io.is_async) os.O.NONBLOCK else 0;
569557
const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try os.pipe2(pipe_flags) else undefined;
570558
errdefer if (self.stdin_behavior == StdIo.Pipe) destroyPipe(stdin_pipe);
@@ -592,28 +580,28 @@ pub const ChildProcess = struct {
592580
undefined;
593581
defer if (any_ignore) os.close(dev_null_fd);
594582

595-
if (user_attr == false) self.posix_attr = try os.posix_spawn.Attr.init();
596-
defer if (user_attr == false) self.posix_attr.?.deinit();
583+
var attr = try os.posix_spawn.Attr.init();
584+
defer attr.deinit();
597585
var flags: u16 = os.darwin.POSIX_SPAWN_SETSIGDEF | os.darwin.POSIX_SPAWN_SETSIGMASK;
598586
if (self.disable_aslr) {
599587
flags |= os.darwin._POSIX_SPAWN_DISABLE_ASLR;
600588
}
601589
if (self.start_suspended) {
602590
flags |= os.darwin.POSIX_SPAWN_START_SUSPENDED;
603591
}
604-
try self.posix_attr.?.set(flags);
592+
try attr.set(flags);
605593

606-
if (user_actions == false) self.posix_actions = try os.posix_spawn.Actions.init();
607-
defer if (user_actions == false) self.posix_actions.?.deinit();
594+
var actions = try os.posix_spawn.Actions.init();
595+
defer actions.deinit();
608596

609-
try setUpChildIoPosixSpawn(self.stdin_behavior, &self.posix_actions.?, stdin_pipe, os.STDIN_FILENO, dev_null_fd);
610-
try setUpChildIoPosixSpawn(self.stdout_behavior, &self.posix_actions.?, stdout_pipe, os.STDOUT_FILENO, dev_null_fd);
611-
try setUpChildIoPosixSpawn(self.stderr_behavior, &self.posix_actions.?, stderr_pipe, os.STDERR_FILENO, dev_null_fd);
597+
try setUpChildIoPosixSpawn(self.stdin_behavior, &actions, stdin_pipe, os.STDIN_FILENO, dev_null_fd);
598+
try setUpChildIoPosixSpawn(self.stdout_behavior, &actions, stdout_pipe, os.STDOUT_FILENO, dev_null_fd);
599+
try setUpChildIoPosixSpawn(self.stderr_behavior, &actions, stderr_pipe, os.STDERR_FILENO, dev_null_fd);
612600

613601
if (self.cwd_dir) |cwd| {
614-
try self.posix_actions.?.fchdir(cwd.fd);
602+
try actions.fchdir(cwd.fd);
615603
} else if (self.cwd) |cwd| {
616-
try self.posix_actions.?.chdir(cwd);
604+
try actions.chdir(cwd);
617605
}
618606

619607
var arena_allocator = std.heap.ArenaAllocator.init(self.allocator);
@@ -628,7 +616,7 @@ pub const ChildProcess = struct {
628616
break :m envp_buf.ptr;
629617
} else std.c.environ;
630618

631-
const pid = try os.posix_spawn.spawnp(self.argv[0], self.posix_actions.?, self.posix_attr.?, argv_buf, envp);
619+
const pid = try os.posix_spawn.spawnp(self.argv[0], actions, attr, argv_buf, envp);
632620

633621
if (self.stdin_behavior == StdIo.Pipe) {
634622
self.stdin = File{ .handle = stdin_pipe[1] };

lib/std/os.zig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ pub const windows = @import("os/windows.zig");
4444
pub const posix_spawn = @import("os/posix_spawn.zig");
4545
pub const ptrace = @import("os/ptrace.zig");
4646

47-
// Zig support for posix spawn on the platform
48-
pub const hasPosixSpawn = builtin.target.isDarwin();
4947
/// Pipe read side
5048
pub const pipe_rd = 0;
5149
/// Pipe write side

test/standalone/childprocess_extrapipe/child.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ pub fn main() !void {
1111
_ = it.next() orelse unreachable; // skip binary name
1212
const s_handle = it.next() orelse unreachable;
1313
var file_handle = try std.os.stringToHandle(s_handle);
14-
// TODO: Is there a way on Windows to let the Kernel disable inheritance
15-
// after it is inherited in CreateProcess???
14+
1615
if (builtin.target.os.tag == .windows) {
1716
// windows.HANDLE_FLAG_INHERIT is enabled
1817
var handle_flags: windows.DWORD = undefined;

test/standalone/childprocess_extrapipe/parent.zig

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ pub fn main() !void {
3232
// enabling of file inheritance directly before and closing directly after spawn
3333
// less time to leak => better
3434
{
35-
// Besides being faster to spawn, posix_spawn enables to close pipe[pipe_wr]
36-
// within the child before it is closed from Kernel after execv.
37-
// Note, that posix_spawn is executed in the child, so it does not allow
38-
// to minimize the leaking time of the parent's handle side.
39-
if (os.hasPosixSpawn) child_proc.posix_actions = try os.posix_spawn.Actions.init();
40-
defer if (os.hasPosixSpawn) child_proc.posix_actions.?.deinit();
41-
if (os.hasPosixSpawn) try child_proc.posix_actions.?.close(pipe[pipe_wr]);
42-
4335
try os.enableInheritance(pipe[pipe_rd]);
4436
defer os.close(pipe[pipe_rd]);
4537

0 commit comments

Comments
 (0)