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 {...@@ -269,9 +269,9 @@ pub const ChildProcess = struct {
269 }269 }
270270
271 fn waitUnwrapped(self: *ChildProcess) void {271 fn waitUnwrapped(self: *ChildProcess) void {
272 const status = os.waitpid(self.pid, 0);272 const ret = os.waitpid(self.pid, 0);
273 self.cleanupStreams();273 self.cleanupStreams();
274 self.handleWaitResult(status);274 self.handleWaitResult(ret.status);
275 }275 }
276276
277 fn handleWaitResult(self: *ChildProcess, status: u32) void {277 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 {...@@ -3121,13 +3121,18 @@ pub fn getsockoptError(sockfd: fd_t) ConnectError!void {
3121 }3121 }
3122}3122}
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 {
3125 // TODO allow implicit pointer cast from *u32 to *c_uint ?3129 // TODO allow implicit pointer cast from *u32 to *c_uint ?
3126 const Status = if (builtin.link_libc) c_uint else u32;3130 const Status = if (builtin.link_libc) c_uint else u32;
3127 var status: Status = undefined;3131 var status: Status = undefined;
3128 while (true) {3132 while (true) {
3129 switch (errno(system.waitpid(pid, &status, flags))) {3133 const rc = system.waitpid(pid, &status, flags);
3130 0 => return @bitCast(u32, status),3134 switch (errno(rc)) {
3135 0 => return WaitpidRet{ .pid = @intCast(pid_t, rc), .status = @bitCast(u32, status) },
3131 EINTR => continue,3136 EINTR => continue,
3132 ECHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error.3137 ECHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error.
3133 EINVAL => unreachable, // The options argument was invalid3138 EINVAL => unreachable, // The options argument was invalid