summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/host_io.zig44
1 files changed, 18 insertions, 26 deletions
diff --git a/src/host_io.zig b/src/host_io.zig
index 51699986..21f6387b 100644
--- a/src/host_io.zig
+++ b/src/host_io.zig
@@ -2858,10 +2858,16 @@ pub fn reapExited(pid: libc.pid_t) ?struct { status: ?u8 } {
const got = libc.waitpid(pid, &status, libc.W.NOHANG);
if (got == 0) return null;
if (got != pid) return .{ .status = null };
+ return .{ .status = statusOf(status) };
+}
+
+/// A wait status as a shell says it: the exit code, or 128 and the signal
+/// that ended it; null for neither.
+fn statusOf(status: c_int) ?u8 {
const s: u32 = @bitCast(status);
- if (libc.W.IFEXITED(s)) return .{ .status = @truncate(libc.W.EXITSTATUS(s)) };
- if (libc.W.IFSIGNALED(s)) return .{ .status = @truncate(128 + @as(u32, @backingInt(libc.W.TERMSIG(s)))) };
- return .{ .status = null };
+ if (libc.W.IFEXITED(s)) return @truncate(libc.W.EXITSTATUS(s));
+ if (libc.W.IFSIGNALED(s)) return @truncate(128 + @as(u32, @backingInt(libc.W.TERMSIG(s))));
+ return null;
}
/// A watched child that has exited, not yet reaped, or null.
@@ -2884,12 +2890,7 @@ pub fn exitStatus(pid: libc.pid_t, wait_ms: u32) ?u8 {
while (true) : (waited += 2) {
var status: c_int = 0;
const got = libc.waitpid(pid, &status, libc.W.NOHANG);
- if (got == pid) {
- const s: u32 = @bitCast(status);
- if (libc.W.IFEXITED(s)) return @truncate(libc.W.EXITSTATUS(s));
- if (libc.W.IFSIGNALED(s)) return @truncate(128 + @as(u32, @backingInt(libc.W.TERMSIG(s))));
- return null;
- }
+ if (got == pid) return statusOf(status);
if (got < 0 or waited >= wait_ms) return null;
const ts: libc.timespec = .{ .sec = 0, .nsec = 2 * std.time.ns_per_ms };
_ = libc.nanosleep(&ts, null);
@@ -3007,16 +3008,7 @@ fn procExe(pid: libc.pid_t, buf: *[std.fs.max_path_bytes]u8) ?[]const u8 {
fn procPgrp(pid: libc.pid_t, buf: *[4096]u8) ?libc.pid_t {
var name: [64:0]u8 = undefined;
const path = std.fmt.bufPrintSentinel(&name, "/proc/{d}/stat", .{@as(u32, @intCast(pid))}, 0) catch return null;
- return parsePgrp(readProc(path, buf) orelse return null);
-}
-
-fn parsePgrp(stat: []const u8) ?libc.pid_t {
- const close = std.mem.lastIndexOfScalar(u8, stat, ')') orelse return null;
- var fields = std.mem.tokenizeAny(u8, stat[close + 1 ..], " \t\n");
- _ = fields.next() orelse return null; // 3: state
- _ = fields.next() orelse return null; // 4: ppid
- const pgrp = fields.next() orelse return null; // 5: pgrp
- return std.fmt.parseInt(libc.pid_t, pgrp, 10) catch null;
+ return (parseGroupSession(readProc(path, buf) orelse return null) orelse return null).pgrp;
}
fn offTty(pid: libc.pid_t, buf: *[4096]u8) bool {
@@ -3091,13 +3083,13 @@ test "the children blob parses to pids, and a garbage token never becomes one" {
test "the process group comes off the last ')', not a comm-shifted stat field" {
const shifted = "1234 (sh (a b)) S 991 992 993 34816 992 4194560 " ++
"1729 0 0 0 1 0 0 0 20 0 1 0 8244630 9887744 1131";
- try std.testing.expectEqual(@as(libc.pid_t, 992), parsePgrp(shifted).?);
- try std.testing.expectEqual(@as(libc.pid_t, 7), parsePgrp("42 (bash) S 1 7 7 34816 7 4194304").?);
- try std.testing.expectEqual(@as(libc.pid_t, 42), parsePgrp("42 (sleep) S 7 42 7 0 -1").?);
- try std.testing.expect(parsePgrp("") == null);
- try std.testing.expect(parsePgrp("1234 (bash) S 991") == null);
- try std.testing.expect(parsePgrp("1234 (bash) S 991 notanumber") == null);
- try std.testing.expect(parsePgrp("no parens here at all") == null);
+ try std.testing.expectEqual(@as(libc.pid_t, 992), parseGroupSession(shifted).?.pgrp);
+ try std.testing.expectEqual(@as(libc.pid_t, 7), parseGroupSession("42 (bash) S 1 7 7 34816 7 4194304").?.pgrp);
+ try std.testing.expectEqual(@as(libc.pid_t, 42), parseGroupSession("42 (sleep) S 7 42 7 0 -1").?.pgrp);
+ try std.testing.expect(parseGroupSession("") == null);
+ try std.testing.expect(parseGroupSession("1234 (bash) S 991") == null);
+ try std.testing.expect(parseGroupSession("1234 (bash) S 991 notanumber") == null);
+ try std.testing.expect(parseGroupSession("no parens here at all") == null);
}
test "the zombie state comes off its own status line" {