Commit 3921cb0d8d

stf <7o5rfu92t@ctrlc.hu>
2020-10-07 20:51:04
std.os.waitpid: also return pid of child
closes #6581
1 parent 0e4c393
Changed files (2)
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 {
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