From 3921cb0d8d46c59411b6c8f35f5d271c26fba14f Mon Sep 17 00:00:00 2001 From: stf <7o5rfu92t@ctrlc.hu> Date: Wed, 7 Oct 2020 18:51:04 +0000 Subject: [PATCH 1/2] std.os.waitpid: also return pid of child closes #6581 --- lib/std/child_process.zig | 4 ++-- lib/std/os.zig | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig index 335574485..10241b8eb 100644 --- a/lib/std/child_process.zig +++ b/lib/std/child_process.zig @@ -269,9 +269,9 @@ pub const ChildProcess = struct { } fn waitUnwrapped(self: *ChildProcess) void { - const status = os.waitpid(self.pid, 0); + const ret = os.waitpid(self.pid, 0); self.cleanupStreams(); - self.handleWaitResult(status); + self.handleWaitResult(ret.status); } fn handleWaitResult(self: *ChildProcess, status: u32) void { diff --git a/lib/std/os.zig b/lib/std/os.zig index bc7dae031..48c1cee6f 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -3121,13 +3121,18 @@ pub fn getsockoptError(sockfd: fd_t) ConnectError!void { } } -pub fn waitpid(pid: i32, flags: u32) u32 { +pub const WaitpidRet = struct { + pid: pid_t, status: u32 +}; + +pub fn waitpid(pid: pid_t, flags: u32) WaitpidRet { // TODO allow implicit pointer cast from *u32 to *c_uint ? const Status = if (builtin.link_libc) c_uint else u32; var status: Status = undefined; while (true) { - switch (errno(system.waitpid(pid, &status, flags))) { - 0 => return @bitCast(u32, status), + const rc = system.waitpid(pid, &status, flags); + switch (errno(rc)) { + 0 => return WaitpidRet{ .pid = @intCast(pid_t, rc), .status = @bitCast(u32, status) }, EINTR => continue, ECHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error. EINVAL => unreachable, // The options argument was invalid From 5c16022c81effc2d684aaf21217dce0f7708946a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 16 Oct 2020 18:14:39 -0700 Subject: [PATCH 2/2] rename WaitpidRet to WaitPidResult --- lib/std/child_process.zig | 4 ++-- lib/std/os.zig | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig index 10241b8eb..3b8913af5 100644 --- a/lib/std/child_process.zig +++ b/lib/std/child_process.zig @@ -269,9 +269,9 @@ pub const ChildProcess = struct { } fn waitUnwrapped(self: *ChildProcess) void { - const ret = os.waitpid(self.pid, 0); + const status = os.waitpid(self.pid, 0).status; self.cleanupStreams(); - self.handleWaitResult(ret.status); + self.handleWaitResult(status); } fn handleWaitResult(self: *ChildProcess, status: u32) void { diff --git a/lib/std/os.zig b/lib/std/os.zig index 48c1cee6f..4a06d3c0b 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -3121,21 +3121,24 @@ pub fn getsockoptError(sockfd: fd_t) ConnectError!void { } } -pub const WaitpidRet = struct { - pid: pid_t, status: u32 +pub const WaitPidResult = struct { + pid: pid_t, + status: u32, }; -pub fn waitpid(pid: pid_t, flags: u32) WaitpidRet { - // TODO allow implicit pointer cast from *u32 to *c_uint ? +pub fn waitpid(pid: pid_t, flags: u32) WaitPidResult { const Status = if (builtin.link_libc) c_uint else u32; var status: Status = undefined; while (true) { const rc = system.waitpid(pid, &status, flags); switch (errno(rc)) { - 0 => return WaitpidRet{ .pid = @intCast(pid_t, rc), .status = @bitCast(u32, status) }, + 0 => return .{ + .pid = @intCast(pid_t, rc), + .status = @bitCast(u32, status), + }, EINTR => continue, ECHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error. - EINVAL => unreachable, // The options argument was invalid + EINVAL => unreachable, // Invalid flags. else => unreachable, } } @@ -5439,9 +5442,7 @@ pub fn getrlimit(resource: rlimit_resource) GetrlimitError!rlimit { } } -pub const SetrlimitError = error{ - PermissionDenied, -} || UnexpectedError; +pub const SetrlimitError = error{PermissionDenied} || UnexpectedError; pub fn setrlimit(resource: rlimit_resource, limits: rlimit) SetrlimitError!void { // TODO implement for systems other than linux and enable test