authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-05-27 15:55:32-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-05-27 20:56:49-07:00
log947a3a1be92e0d5ddc5ad263d9434b31e8c170db
tree74eeb6c331e05164734ec7817c1e169d909adc0d
parentb7889f262a5bee642460eb33b1ae7f2b1f87864c

std.process.Child: fix spawning child proc with new cwd fd

Before this fix, the dup2 of the progress pipe was clobbering the cwd fd, causing the fchdir to return ENOTDIR in between fork() and exec().

1 files changed, 4 insertions(+), 1 deletions(-)

lib/std/process/Child.zig+4-1
......@@ -654,7 +654,6 @@ fn spawnPosix(self: *ChildProcess) SpawnError!void {
654654 setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
655655 setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
656656 setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
657 if (prog_pipe[1] != -1) posix.dup2(prog_pipe[1], prog_fileno) catch |err| forkChildErrReport(err_pipe[1], err);
658657
659658 if (self.cwd_dir) |cwd| {
660659 posix.fchdir(cwd.fd) catch |err| forkChildErrReport(err_pipe[1], err);
......@@ -662,6 +661,10 @@ fn spawnPosix(self: *ChildProcess) SpawnError!void {
662661 posix.chdir(cwd) catch |err| forkChildErrReport(err_pipe[1], err);
663662 }
664663
664 // Must happen after fchdir above, the cwd file descriptor might be
665 // equal to prog_fileno and be clobbered by this dup2 call.
666 if (prog_pipe[1] != -1) posix.dup2(prog_pipe[1], prog_fileno) catch |err| forkChildErrReport(err_pipe[1], err);
667
665668 if (self.gid) |gid| {
666669 posix.setregid(gid, gid) catch |err| forkChildErrReport(err_pipe[1], err);
667670 }