diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/daemon.zig | 61 | ||||
| -rw-r--r-- | src/main.zig | 113 |
2 files changed, 171 insertions, 3 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); diff --git a/src/main.zig b/src/main.zig index fafafc08..d4edc74d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -111,11 +111,17 @@ const help_text = \\ --attach=<host>:<name> a client of session <name> on another machine, \\ over `ssh -L` to its pardes-replica.sock \\ ($PARDES_SSH: the ssh command, words, default - \\ `ssh -C`; and + \\ `ssh`; and \\ $PARDES_REMOTE_RUNTIME: its runtime directory, \\ if not this one's path). Its keys show at once \\ --attach=<sock>:<name> ...through that socket (a path, with a '/'), \\ one forwarded already, or this machine's + \\ --ps what the pardes daemon keeps: each session (its + \\ pid, kind, whether attached) and its shells, then + \\ the shells no session holds + \\ --kill=<name> end session <name> and its shells (TERM, then + \\ KILL 2 s later); unsaved changes are lost + \\ --kill-all end every session and every shell the daemon keeps \\ --geometry=WxH[+X+Y] the window's size in points, and where it goes \\ where the display lets a window say (X11) \\ -h, --help show this help and exit @@ -507,6 +513,14 @@ fn nativeMain(std_init: std.process.Init) !void { attach = ""; } else if (std.mem.startsWith(u8, a, "--attach=")) { attach = a["--attach=".len..]; + } else if (std.mem.eql(u8, a, "--ps")) { + manage(init.io, "list", ""); + } else if (std.mem.startsWith(u8, a, "--kill=")) { + const name = a["--kill=".len..]; + if (name.len == 0) badArgs(init.io, "--kill needs a session's name; --kill-all ends every one", .{}); + manage(init.io, "kill", name); + } else if (std.mem.eql(u8, a, "--kill-all")) { + manage(init.io, "killall", ""); } else if (std.mem.eql(u8, a, "-h") or std.mem.eql(u8, a, "--help")) { try std.Io.File.stdout().writeStreamingAll(init.io, help_text); return; @@ -870,6 +884,103 @@ fn nativeMain(std_init: std.process.Init) !void { extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; +/// `pardes --ps`: what the per-user daemon keeps, as a tree; `--kill=<name>` +/// and `--kill-all`: those sessions ended with their shells, a TERM and, +/// for one still there 2 s later, a KILL. +fn manage(io: std.Io, verb: []const u8, name: []const u8) noreturn { + const Say = struct { + fn no(o: std.Io, comptime fmt: []const u8, args: anytype) noreturn { + var buf: [700]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&buf, "pardes: " ++ fmt ++ "\n", args) catch "pardes: failed\n") catch {}; + std.process.exit(1); + } + }; + if (comptime !daemon.supported) Say.no(io, "this system has no pardes daemon", .{}); + var words_buf: [300]u8 = undefined; + const words = if (std.mem.eql(u8, verb, "kill")) + std.fmt.bufPrint(&words_buf, "kill {s}", .{name}) catch Say.no(io, "that name is too long", .{}) + else + verb; + var buf: [4096]u8 = undefined; + const said = daemon.ask(words, &buf) orelse Say.no(io, "no pardes daemon is running", .{}); + if (std.mem.startsWith(u8, said, "no ")) Say.no(io, "{s}", .{said[3..]}); + if (std.mem.eql(u8, verb, "list")) { + var out: [8192]u8 = undefined; + var w: std.Io.Writer = .fixed(&out); + var lines = std.mem.tokenizeScalar(u8, said, '\n'); + var orphans = false; + while (lines.next()) |line| { + var it = std.mem.tokenizeScalar(u8, line, ' '); + const what = it.next() orelse continue; + const pid = it.next() orelse continue; + var comm_buf: [64]u8 = undefined; + const comm = commOf(pid, &comm_buf); + if (std.mem.eql(u8, what, "daemon")) { + w.print("pardes daemon {s}\n", .{pid}) catch break; + } else if (std.mem.eql(u8, what, "session")) { + const kind = it.next() orelse ""; + const state = it.next() orelse ""; + w.print(" session {s} {s} ({s}, {s})\n", .{ std.mem.trim(u8, it.rest(), " "), pid, kind, state }) catch break; + } else if (std.mem.eql(u8, what, "shell")) { + w.print(" shell {s} {s}\n", .{ pid, comm }) catch break; + } else if (std.mem.eql(u8, what, "orphan")) { + if (!orphans) w.writeAll(" shells no session holds (Adopt takes them, --kill-all ends them)\n") catch break; + orphans = true; + w.print(" shell {s} {s}\n", .{ pid, comm }) catch break; + } + } + std.Io.File.stdout().writeStreamingAll(io, w.buffered()) catch {}; + std.process.exit(0); + } + var pids: [64]std.c.pid_t = undefined; + var n: usize = 0; + var it = std.mem.tokenizeScalar(u8, said, ' '); + _ = it.next(); // ok + while (it.next()) |p| if (n < pids.len) { + pids[n] = std.fmt.parseInt(std.c.pid_t, p, 10) catch continue; + n += 1; + }; + // Gone once the daemon no longer lists them: a session's link ends with + // its process, a zombie's too, which kill(pid, 0) would call alive. + const Gone = struct { + fn all(list: []const std.c.pid_t, ms: u32) bool { + var waited: u32 = 0; + while (true) : (waited += 20) { + var reply: [4096]u8 = undefined; + const now = daemon.ask("list", &reply) orelse return true; + var lines = std.mem.tokenizeScalar(u8, now, '\n'); + const left = while (lines.next()) |line| { + var fields = std.mem.tokenizeScalar(u8, line, ' '); + if (!std.mem.eql(u8, fields.next() orelse "", "session")) continue; + const pid = std.fmt.parseInt(std.c.pid_t, fields.next() orelse "", 10) catch continue; + if (std.mem.indexOfScalar(std.c.pid_t, list, pid) != null) break true; + } else false; + if (!left) return true; + if (waited >= ms) return false; + const ts: std.c.timespec = .{ .sec = 0, .nsec = 20 * std.time.ns_per_ms }; + _ = std.c.nanosleep(&ts, null); + } + } + }; + if (!Gone.all(pids[0..n], 2000)) { + for (pids[0..n]) |p| _ = std.c.kill(p, .KILL); + if (!Gone.all(pids[0..n], 1000)) Say.no(io, "a session would not end, even killed", .{}); + } + std.process.exit(0); +} + +/// The command `pid` runs, as /proc says it (Linux); empty where it does not. +fn commOf(pid: []const u8, buf: *[64]u8) []const u8 { + var path_buf: [64]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&path_buf, "/proc/{s}/comm", .{pid}, 0) catch return ""; + const fd = std.c.open(path, .{ .ACCMODE = .RDONLY, .CLOEXEC = true }, @as(std.c.mode_t, 0)); + if (fd < 0) return ""; + defer _ = std.c.close(fd); + const got = std.c.read(fd, buf, buf.len); + if (got <= 0) return ""; + return std.mem.trimEnd(u8, buf[0..@intCast(got)], "\n"); +} + /// `pardes --attach=<where>:<name>`: a replica client (tty.zig `remote`), /// through the socket `where` names, or through `ssh -L` of host `where`'s /// replica socket to one of this process's. |
