diff options
Diffstat (limited to 'runtime/main.zig')
| -rw-r--r-- | runtime/main.zig | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/runtime/main.zig b/runtime/main.zig index a21eb83f..dc351c30 100644 --- a/runtime/main.zig +++ b/runtime/main.zig @@ -269,10 +269,12 @@ const cmds = std.EnumArray(Cmd, Doc).init(.{ .line = "the daemon's sessions, their clients and shells", .doc = \\What the per-user daemon lists: each session with its name, pid, kind - \\(tty or gui) and whether it has a frontend of its own or is detached; - \\under it its clients and its shells; then the shells no session holds, - \\which Adopt takes. /sessions is the same list, read from inside a - \\session (where a session with a frontend says attached). + \\(tty or gui) and whether it has a frontend of its own or is detached + \\(under PARDES_PAIR: kept, or nothing when its last client's close + \\ends it); under it its clients and its shells; then the shells no + \\session holds, which Adopt takes. /sessions is the same list, read + \\from inside a session (where a session with a frontend says + \\attached, and a pair's not kept says pair). , .examples = &.{.{ "pardes ps", "every session, client and shell" }}, .see = "attach, kill, /sessions", @@ -1570,14 +1572,14 @@ fn nativeMain(std_init: std.process.Init) !void { term = .{ .fd = .{ .fd = t.fd, .termios = t.termios } }; }; detached = if (pardes.platform == .gui) !rec.window else rec.nttys == 0; - daemon.resumeLink(rec.link, rec.stub, kind, opts.ninep_name, detached); + daemon.resumeLink(rec.link, rec.stub, kind, opts.ninep_name, rec.state orelse if (detached) .detached else .attached); } else if (paired) |fd| { // A window's pair's session: its first client on `fd`, the daemon // its own to dial (and to start, when none runs). opts.pair = fd; term = .detached; detached = true; - if (daemon.wanted()) _ = daemon.register(null, kind, opts.ninep_name, true); + if (daemon.wanted()) _ = daemon.register(null, kind, opts.ninep_name, .pair); } else if (detach == null and std.c.getenv("PARDES_PAIR") != null) { // The last step's way, behind its variable until it is the default: // this terminal or window a client of a session forked off for it @@ -1586,7 +1588,9 @@ fn nativeMain(std_init: std.process.Init) !void { term = .detached; } else if (daemon.wanted()) { if (detach != null or pardes.platform == .gui) { - _ = daemon.register(null, kind, opts.ninep_name, detach != null); + // A session started detached is kept from birth: a pair's word. + const s: daemon.State = if (detach == null) .attached else if (std.c.getenv("PARDES_PAIR") != null) .kept else .detached; + _ = daemon.register(null, kind, opts.ninep_name, s); } else if (pardes.platform == .tui) fork: { // A terminal's session is forked off its stub, which stays the // job the shell waits on: Detach (or the terminal's end) ends @@ -1610,7 +1614,7 @@ fn nativeMain(std_init: std.process.Init) !void { _ = std.c.close(conn); break :fork; }; - _ = daemon.register(conn, .tty, opts.ninep_name, false); + _ = daemon.register(conn, .tty, opts.ninep_name, .attached); term = .{ .fd = .{ .fd = fd } }; } } @@ -1662,7 +1666,12 @@ fn manage(io: std.Io, verb: enum { list, kill, killall }, name: []const u8) nore .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, if (std.mem.eql(u8, state, "attached")) "has a frontend" else state }) catch break; + const named = std.mem.trim(u8, it.rest(), " "); + // A pair's not kept: its clients are all there is to it. + if (std.mem.eql(u8, state, "pair")) + w.print(" session {s} {s} ({s})\n", .{ named, pid, kind }) catch break + else + w.print(" session {s} {s} ({s}, {s})\n", .{ named, pid, kind, if (std.mem.eql(u8, state, "attached")) "has a frontend" else state }) catch break; }, .client => w.print(" client {s} {s}\n", .{ pid, comm }) catch break, .shell => w.print(" shell {s} {s}\n", .{ pid, comm }) catch break, @@ -1784,7 +1793,7 @@ fn pair(init: std.process.Init, opts: *pardes.Options, args: []const [:0]const u opts.pair = fds[0]; // The daemon is the client's to start; the hello is this one's. daemon.startPending(false); - if (conn) |c| _ = daemon.register(c, .tty, opts.ninep_name, true); + if (conn) |c| _ = daemon.register(c, .tty, opts.ninep_name, .pair); return; } _ = std.c.close(fds[0]); @@ -1941,7 +1950,7 @@ fn bareAttach(list: []const u8, names: *std.Io.Writer) ?[]const u8 { one = std.mem.trim(u8, words.rest(), " "); n += 1; names.print(" {s}", .{one.?}) catch {}; // cut short - if (!std.mem.eql(u8, state, "detached")) continue; + if (std.mem.eql(u8, state, "attached")) continue; detached = one; n_detached += 1; } @@ -1954,6 +1963,8 @@ test "a bare attach takes the one detached session, else the one there is, else var names: std.Io.Writer = .fixed(&buf); try std.testing.expectEqualStrings("b", bareAttach("daemon 1\nsession 2 tty attached a\nshell 9\nsession 3 gui detached b\n", &names).?); try std.testing.expectEqualStrings("a", bareAttach("daemon 1\nsession 2 tty attached a\n", &names).?); + // A pair's, kept or not, has no frontend of its own either. + try std.testing.expectEqualStrings("c", bareAttach("session 2 tty attached a\nsession 3 tty pair c\n", &names).?); names = .fixed(&buf); try std.testing.expect(bareAttach("daemon 1\norphan 4\n", &names) == null); try std.testing.expectEqual(@as(usize, 0), names.end); |
