| ... | @@ -216,9 +216,9 @@ pub fn init(argv: []const []const u8, allocator: mem.Allocator) ChildProcess { | ... | @@ -216,9 +216,9 @@ pub fn init(argv: []const []const u8, allocator: mem.Allocator) ChildProcess { |
| 216 | .stdin = null, | 216 | .stdin = null, |
| 217 | .stdout = null, | 217 | .stdout = null, |
| 218 | .stderr = null, | 218 | .stderr = null, |
| 219 | .stdin_behavior = StdIo.Inherit, | 219 | .stdin_behavior = .Inherit, |
| 220 | .stdout_behavior = StdIo.Inherit, | 220 | .stdout_behavior = .Inherit, |
| 221 | .stderr_behavior = StdIo.Inherit, | 221 | .stderr_behavior = .Inherit, |
| 222 | .expand_arg0 = .no_expand, | 222 | .expand_arg0 = .no_expand, |
| 223 | }; | 223 | }; |
| 224 | } | 224 | } |
| ... | @@ -549,22 +549,22 @@ fn spawnPosix(self: *ChildProcess) SpawnError!void { | ... | @@ -549,22 +549,22 @@ fn spawnPosix(self: *ChildProcess) SpawnError!void { |
| 549 | // turns out, we `dup2` everything anyway, so there's no need! | 549 | // turns out, we `dup2` everything anyway, so there's no need! |
| 550 | const pipe_flags: posix.O = .{ .CLOEXEC = true }; | 550 | const pipe_flags: posix.O = .{ .CLOEXEC = true }; |
| 551 | | 551 | |
| 552 | const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try posix.pipe2(pipe_flags) else undefined; | 552 | const stdin_pipe = if (self.stdin_behavior == .Pipe) try posix.pipe2(pipe_flags) else undefined; |
| 553 | errdefer if (self.stdin_behavior == StdIo.Pipe) { | 553 | errdefer if (self.stdin_behavior == .Pipe) { |
| 554 | destroyPipe(stdin_pipe); | 554 | destroyPipe(stdin_pipe); |
| 555 | }; | 555 | }; |
| 556 | | 556 | |
| 557 | const stdout_pipe = if (self.stdout_behavior == StdIo.Pipe) try posix.pipe2(pipe_flags) else undefined; | 557 | const stdout_pipe = if (self.stdout_behavior == .Pipe) try posix.pipe2(pipe_flags) else undefined; |
| 558 | errdefer if (self.stdout_behavior == StdIo.Pipe) { | 558 | errdefer if (self.stdout_behavior == .Pipe) { |
| 559 | destroyPipe(stdout_pipe); | 559 | destroyPipe(stdout_pipe); |
| 560 | }; | 560 | }; |
| 561 | | 561 | |
| 562 | const stderr_pipe = if (self.stderr_behavior == StdIo.Pipe) try posix.pipe2(pipe_flags) else undefined; | 562 | const stderr_pipe = if (self.stderr_behavior == .Pipe) try posix.pipe2(pipe_flags) else undefined; |
| 563 | errdefer if (self.stderr_behavior == StdIo.Pipe) { | 563 | errdefer if (self.stderr_behavior == .Pipe) { |
| 564 | destroyPipe(stderr_pipe); | 564 | destroyPipe(stderr_pipe); |
| 565 | }; | 565 | }; |
| 566 | | 566 | |
| 567 | const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore); | 567 | const any_ignore = (self.stdin_behavior == .Ignore or self.stdout_behavior == .Ignore or self.stderr_behavior == .Ignore); |
| 568 | const dev_null_fd = if (any_ignore) | 568 | const dev_null_fd = if (any_ignore) |
| 569 | posix.openZ("/dev/null", .{ .ACCMODE = .RDWR }, 0) catch |err| switch (err) { | 569 | posix.openZ("/dev/null", .{ .ACCMODE = .RDWR }, 0) catch |err| switch (err) { |
| 570 | error.PathAlreadyExists => unreachable, | 570 | error.PathAlreadyExists => unreachable, |
| ... | @@ -609,35 +609,24 @@ fn spawnPosix(self: *ChildProcess) SpawnError!void { | ... | @@ -609,35 +609,24 @@ fn spawnPosix(self: *ChildProcess) SpawnError!void { |
| 609 | const argv_buf = try arena.allocSentinel(?[*:0]const u8, self.argv.len, null); | 609 | const argv_buf = try arena.allocSentinel(?[*:0]const u8, self.argv.len, null); |
| 610 | for (self.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; | 610 | for (self.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; |
| 611 | | 611 | |
| | 612 | const prog_fileno = 3; |
| | 613 | |
| 612 | const envp: [*:null]const ?[*:0]const u8 = m: { | 614 | const envp: [*:null]const ?[*:0]const u8 = m: { |
| 613 | const extra_usizes: []const process.CreateEnvironOptions.ExtraUsize = if (prog_pipe[1] == -1) &.{} else &.{ | 615 | const prog_fd: i32 = if (prog_pipe[1] == -1) -1 else prog_fileno; |
| 614 | .{ .name = "ZIG_PROGRESS", .value = @intCast(prog_pipe[1]) }, | | |
| 615 | }; | | |
| 616 | if (self.env_map) |env_map| { | 616 | if (self.env_map) |env_map| { |
| 617 | break :m (try process.createEnviron(arena, .{ | 617 | break :m (try process.createEnvironFromMap(arena, env_map, .{ |
| 618 | .env_map = env_map, | 618 | .zig_progress_fd = prog_fd, |
| 619 | .extra_usizes = extra_usizes, | | |
| 620 | })).ptr; | 619 | })).ptr; |
| 621 | } else if (builtin.link_libc) { | 620 | } else if (builtin.link_libc) { |
| 622 | if (extra_usizes.len == 0) { | 621 | break :m (try process.createEnvironFromExisting(arena, std.c.environ, .{ |
| 623 | break :m std.c.environ; | 622 | .zig_progress_fd = prog_fd, |
| 624 | } else { | 623 | })).ptr; |
| 625 | break :m (try process.createEnviron(arena, .{ | | |
| 626 | .existing = std.c.environ, | | |
| 627 | .extra_usizes = extra_usizes, | | |
| 628 | })).ptr; | | |
| 629 | } | | |
| 630 | } else if (builtin.output_mode == .Exe) { | 624 | } else if (builtin.output_mode == .Exe) { |
| 631 | // Then we have Zig start code and this works. | 625 | // Then we have Zig start code and this works. |
| 632 | if (extra_usizes.len == 0) { | 626 | // TODO type-safety for null-termination of `os.environ`. |
| 633 | break :m @ptrCast(std.os.environ.ptr); | 627 | break :m (try process.createEnvironFromExisting(arena, @ptrCast(std.os.environ.ptr), .{ |
| 634 | } else { | 628 | .zig_progress_fd = prog_fd, |
| 635 | break :m (try process.createEnviron(arena, .{ | 629 | })).ptr; |
| 636 | // TODO type-safety for null-termination of `os.environ`. | | |
| 637 | .existing = @ptrCast(std.os.environ.ptr), | | |
| 638 | .extra_usizes = extra_usizes, | | |
| 639 | })).ptr; | | |
| 640 | } | | |
| 641 | } else { | 630 | } else { |
| 642 | // TODO come up with a solution for this. | 631 | // TODO come up with a solution for this. |
| 643 | @compileError("missing std lib enhancement: ChildProcess implementation has no way to collect the environment variables to forward to the child process"); | 632 | @compileError("missing std lib enhancement: ChildProcess implementation has no way to collect the environment variables to forward to the child process"); |
| ... | @@ -664,6 +653,12 @@ fn spawnPosix(self: *ChildProcess) SpawnError!void { | ... | @@ -664,6 +653,12 @@ fn spawnPosix(self: *ChildProcess) SpawnError!void { |
| 664 | setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); | 653 | setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); |
| 665 | setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); | 654 | setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); |
| 666 | setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); | 655 | setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); |
| | 656 | if (prog_pipe[1] != -1) posix.dup2(prog_pipe[1], prog_fileno) catch |err| forkChildErrReport(err_pipe[1], err); |
| | 657 | |
| | 658 | if (prog_pipe[1] != -1) { |
| | 659 | if (prog_pipe[0] != prog_fileno) posix.close(prog_pipe[0]); |
| | 660 | if (prog_pipe[1] != prog_fileno) posix.close(prog_pipe[1]); |
| | 661 | } |
| 667 | | 662 | |
| 668 | if (self.cwd_dir) |cwd| { | 663 | if (self.cwd_dir) |cwd| { |
| 669 | posix.fchdir(cwd.fd) catch |err| forkChildErrReport(err_pipe[1], err); | 664 | posix.fchdir(cwd.fd) catch |err| forkChildErrReport(err_pipe[1], err); |
| ... | @@ -718,6 +713,9 @@ fn spawnPosix(self: *ChildProcess) SpawnError!void { | ... | @@ -718,6 +713,9 @@ fn spawnPosix(self: *ChildProcess) SpawnError!void { |
| 718 | posix.close(stderr_pipe[1]); | 713 | posix.close(stderr_pipe[1]); |
| 719 | } | 714 | } |
| 720 | | 715 | |
| | 716 | if (prog_pipe[1] != -1) { |
| | 717 | posix.close(prog_pipe[1]); |
| | 718 | } |
| 721 | self.progress_node.setIpcFd(prog_pipe[0]); | 719 | self.progress_node.setIpcFd(prog_pipe[0]); |
| 722 | } | 720 | } |
| 723 | | 721 | |