diff options
Diffstat (limited to 'src/daemon.zig')
| -rw-r--r-- | src/daemon.zig | 61 |
1 files changed, 59 insertions, 2 deletions
diff --git a/src/daemon.zig b/src/daemon.zig index d2ae012c..c6cd7f85 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -489,6 +489,17 @@ pub fn attach(k: Kind, named: []const u8, fds: []const c_int, buf: []u8) Answer return .{ .no = if (std.mem.startsWith(u8, got.words, "no ")) got.words[3..] else got.words }; } +/// The daemon's answer to `words` (`list`, `kill <name>`, `killall`), in +/// `buf`; null with no daemon running, or none answering. +pub fn ask(words: []const u8, buf: []u8) ?[]const u8 { + const fd = connect(false) orelse return null; + defer _ = libc.close(fd); + if (!send(fd, words, &.{}) or !readable(fd, 2000)) return null; + const got = recv(fd, buf) orelse return null; + for (got.fds[0..got.nfds]) |f| _ = libc.close(f); + return got.words; +} + // ---- a terminal's stub ---- var stub_child: libc.pid_t = 0; @@ -665,6 +676,8 @@ const Conn = struct { detached: bool = false, /// Its build's wire.semantics: a replica's must be the same. semantics: u64 = 0, + /// `pardes --kill` asked its end: its shells go with it, not kept. + killed: bool = false, name: [128]u8 = undefined, name_len: usize = 0, @@ -709,12 +722,53 @@ const Daemon = struct { fn hangUp(d: *Daemon, i: u8) void { const c = &d.conns[i]; + const killed = c.killed; _ = libc.close(c.fd); c.* = .{}; - // Whatever it still held, it died holding: kept for an Adopt. + // Whatever it still held, it died holding: kept for an Adopt, unless + // it was killed on purpose, when its shells hang up with it. for (&d.held) |*h| if (h.fd >= 0 and h.owner == i) { - h.owner = null; + if (killed) d.drop(h) else h.owner = null; + }; + } + + /// `pardes --kill`: each session named (every one, for null) sent a + /// TERM and marked, its pids answered; the shells of none kept, for + /// all of them. + fn kill(d: *Daemon, i: u8, named: ?[]const u8) void { + var line: [msg_max]u8 = undefined; + var w: std.Io.Writer = .fixed(&line); + w.writeAll("ok") catch {}; + var n: usize = 0; + for (&d.conns) |*s| if (s.fd >= 0 and s.kind != null) { + if (named) |want| if (!std.mem.eql(u8, s.named(), want)) continue; + s.killed = true; + _ = libc.kill(s.pid, .TERM); + w.print(" {d}", .{s.pid}) catch {}; + n += 1; + }; + if (named == null) { + for (&d.held) |*h| if (h.fd >= 0 and h.owner == null) d.drop(h); + } else if (n == 0) { + var why: [256]u8 = undefined; + return d.reply(i, std.fmt.bufPrint(&why, "no no session called '{s}'", .{named.?}) catch "no no such session", &.{}); + } + d.reply(i, w.buffered(), &.{}); + } + + /// `pardes --ps`: a line a session (pid, kind, state, name), a line + /// for each shell it holds after it, then the shells nobody holds. + /// ponytail: one message, so a registry past msg_max is cut short. + fn list(d: *Daemon, i: u8) void { + var line: [msg_max]u8 = undefined; + var w: std.Io.Writer = .fixed(&line); + w.print("daemon {d}\n", .{libc.getpid()}) catch {}; + for (&d.conns, 0..) |*s, k| if (s.fd >= 0 and s.kind != null) { + w.print("session {d} {t} {s} {s}\n", .{ s.pid, s.kind.?, if (s.detached) "detached" else "attached", s.named() }) catch break; + for (d.held) |h| if (h.fd >= 0 and h.owner == @as(u8, @intCast(k))) w.print("shell {d}\n", .{h.pid}) catch break; }; + for (d.held) |h| if (h.fd >= 0 and h.owner == null) w.print("orphan {d}\n", .{h.pid}) catch break; + d.reply(i, w.buffered(), &.{}); } fn reply(d: *Daemon, i: u8, words: []const u8, fds: []const c_int) void { @@ -776,6 +830,9 @@ const Daemon = struct { d.upgrade_asked = true; return; } + if (std.mem.eql(u8, verb, "list")) return d.list(i); + if (std.mem.eql(u8, verb, "kill")) return d.kill(i, std.mem.trim(u8, words.rest(), " ")); + if (std.mem.eql(u8, verb, "killall")) return d.kill(i, null); if (std.mem.eql(u8, verb, "adopt")) { var line: [msg_max]u8 = undefined; var w: std.Io.Writer = .fixed(&line); |
