authorgravatar for 7o5rfu92t@ctrlc.hustf <7o5rfu92t@ctrlc.hu> 2020-10-07 18:51:04+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-16 17:26:50-07:00
log3921cb0d8d46c59411b6c8f35f5d271c26fba14f
treedec306becc1dfcc32e7276c635eb09dbcd0e5773
parent0e4c3934a02cda80a72a730593ece41c7fa8026a

std.os.waitpid: also return pid of child

closes #6581

2 files changed, 10 insertions(+), 5 deletions(-)

lib/std/child_process.zig+2-2
......@@ -269,9 +269,9 @@ pub const ChildProcess = struct {
269269 }
270270
271271 fn waitUnwrapped(self: *ChildProcess) void {
272 const status = os.waitpid(self.pid, 0);
272 const ret = os.waitpid(self.pid, 0);
273273 self.cleanupStreams();
274 self.handleWaitResult(status);
274 self.handleWaitResult(ret.status);
275275 }
276276
277277 fn handleWaitResult(self: *ChildProcess, status: u32) void {
lib/std/os.zig+8-3
......@@ -3121,13 +3121,18 @@ pub fn getsockoptError(sockfd: fd_t) ConnectError!void {
31213121 }
31223122}
31233123
3124pub fn waitpid(pid: i32, flags: u32) u32 {
3124pub const WaitpidRet = struct {
3125 pid: pid_t, status: u32
3126};
3127
3128pub fn waitpid(pid: pid_t, flags: u32) WaitpidRet {
31253129 // TODO allow implicit pointer cast from *u32 to *c_uint ?
31263130 const Status = if (builtin.link_libc) c_uint else u32;
31273131 var status: Status = undefined;
31283132 while (true) {
3129 switch (errno(system.waitpid(pid, &status, flags))) {
3130 0 => return @bitCast(u32, status),
3133 const rc = system.waitpid(pid, &status, flags);
3134 switch (errno(rc)) {
3135 0 => return WaitpidRet{ .pid = @intCast(pid_t, rc), .status = @bitCast(u32, status) },
31313136 EINTR => continue,
31323137 ECHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error.
31333138 EINVAL => unreachable, // The options argument was invalid