Skip to content

std (posix): lower getpid and getppid into std.posix #24027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/std/posix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4412,6 +4412,18 @@ pub fn waitpid(pid: pid_t, flags: u32) WaitPidResult {
}
}

pub fn getpid() pid_t {
if (@hasDecl(system, "getpid"))
return system.getpid();
@compileError("getpid() not yet implemented for target system");
}

pub fn getppid() pid_t {
if (@hasDecl(system, "getppid"))
return system.getppid();
@compileError("getppid() not yet implemented for target system");
}

pub fn wait4(pid: pid_t, flags: u32, ru: ?*rusage) WaitPidResult {
var status: if (builtin.link_libc) c_int else u32 = undefined;
while (true) {
Expand Down
21 changes: 21 additions & 0 deletions lib/std/posix/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,27 @@ test "POSIX file locking with fcntl" {
}
}

test "posix getpid" {
if (!@hasDecl(posix.system, "getpid"))
return error.SkipZigTest;

try expect(posix.getpid() != 0);
}

test "posix getppid" {
if (!@hasDecl(posix.system, "getppid"))
return error.SkipZigTest;

const parent = posix.getpid();
const child = try posix.fork();
if (child == 0)
posix.exit(if (parent == posix.getppid()) 0 else 1);

const result = posix.waitpid(child, 0);
try expect(result.pid != 0);
try expect(result.status == 0);
}

test "rename smoke test" {
if (native_os == .wasi) return error.SkipZigTest;
if (native_os == .windows) return error.SkipZigTest;
Expand Down