Skip to content

Commit

Permalink
Allow setting PGID in std.process.Child.spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
McSinyx committed Jul 22, 2024
1 parent d6fa71c commit 759ab41
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/std/process/Child.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ uid: if (native_os == .windows or native_os == .wasi) void else ?posix.uid_t,
/// Set to change the group id when spawning the child process.
gid: if (native_os == .windows or native_os == .wasi) void else ?posix.gid_t,

/// Set to change the process group id when spawning the child process.
pgid: if (native_os == .windows or native_os == .wasi) void else ?posix.pid_t,

/// Set to change the current working directory when spawning the child process.
cwd: ?[]const u8,
/// Set to change the current working directory when spawning the child process.
Expand Down Expand Up @@ -168,6 +171,7 @@ pub const SpawnError = error{
} ||
posix.ExecveError ||
posix.SetIdError ||
posix.SetPgidError ||
posix.ChangeCurDirError ||
windows.CreateProcessError ||
windows.GetProcessMemoryInfoError ||
Expand Down Expand Up @@ -213,6 +217,7 @@ pub fn init(argv: []const []const u8, allocator: mem.Allocator) ChildProcess {
.cwd = null,
.uid = if (native_os == .windows or native_os == .wasi) {} else null,
.gid = if (native_os == .windows or native_os == .wasi) {} else null,
.pgid = if (native_os == .windows or native_os == .wasi) {} else null,
.stdin = null,
.stdout = null,
.stderr = null,
Expand Down Expand Up @@ -675,6 +680,10 @@ fn spawnPosix(self: *ChildProcess) SpawnError!void {
posix.setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err);
}

if (self.pgid) |pid| {
posix.setpgid(0, pid) catch |err| forkChildErrReport(err_pipe[1], err);
}

const err = switch (self.expand_arg0) {
.expand => posix.execvpeZ_expandArg0(.expand, argv_buf.ptr[0].?, argv_buf.ptr, envp),
.no_expand => posix.execvpeZ_expandArg0(.no_expand, argv_buf.ptr[0].?, argv_buf.ptr, envp),
Expand Down

0 comments on commit 759ab41

Please sign in to comment.