summaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig113
1 files changed, 112 insertions, 1 deletions
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.