Skip to content

Commit bf15c79

Browse files
authored
Merge pull request #25820 from GiuseppeCesarano/process
Child.start_suspended ported to posix
2 parents 852a1f7 + f4159ef commit bf15c79

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

lib/std/posix.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,14 @@ pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void {
16991699
}
17001700
}
17011701

1702+
pub fn getpid() pid_t {
1703+
return system.getpid();
1704+
}
1705+
1706+
pub fn getppid() pid_t {
1707+
return system.getppid();
1708+
}
1709+
17021710
pub const ExecveError = error{
17031711
SystemResources,
17041712
AccessDenied,

lib/std/posix/test.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,21 @@ test "dup & dup2" {
621621
try testing.expectEqualStrings("dupdup2", try tmp.dir.readFile("os_dup_test", &buffer));
622622
}
623623

624+
test "getpid" {
625+
if (native_os == .wasi) return error.SkipZigTest;
626+
if (native_os == .windows) return error.SkipZigTest;
627+
628+
try expect(posix.getpid() != 0);
629+
}
630+
631+
test "getppid" {
632+
if (native_os == .wasi) return error.SkipZigTest;
633+
if (native_os == .windows) return error.SkipZigTest;
634+
if (native_os == .plan9 and !builtin.link_libc) return error.SkipZigTest;
635+
636+
try expect(posix.getppid() >= 0);
637+
}
638+
624639
test "writev longer than IOV_MAX" {
625640
if (native_os == .windows or native_os == .wasi) return error.SkipZigTest;
626641

lib/std/process/Child.zig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ expand_arg0: Arg0Expand,
8585
/// Darwin-only. Disable ASLR for the child process.
8686
disable_aslr: bool = false,
8787

88-
/// Darwin and Windows only. Start child process in suspended state. For Darwin it's started
89-
/// as if SIGSTOP was sent.
88+
/// Start child process in suspended state.
89+
/// For Posix systems it's started as if SIGSTOP was sent.
9090
start_suspended: bool = false,
9191

9292
/// Windows-only. Sets the CREATE_NO_WINDOW flag in CreateProcess.
@@ -669,6 +669,10 @@ fn spawnPosix(self: *ChildProcess) SpawnError!void {
669669
posix.setpgid(0, pid) catch |err| forkChildErrReport(err_pipe[1], err);
670670
}
671671

672+
if (self.start_suspended) {
673+
posix.kill(posix.getpid(), .STOP) catch |err| forkChildErrReport(err_pipe[1], err);
674+
}
675+
672676
const err = switch (self.expand_arg0) {
673677
.expand => posix.execvpeZ_expandArg0(.expand, argv_buf.ptr[0].?, argv_buf.ptr, envp),
674678
.no_expand => posix.execvpeZ_expandArg0(.no_expand, argv_buf.ptr[0].?, argv_buf.ptr, envp),

0 commit comments

Comments
 (0)