summaryrefslogtreecommitdiff
path: root/src/daemon.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-06 03:20:04 -0300
committerGabriel Schneider <[email protected]>2026-11-06 04:13:33 -0300
commit4c2125ecf47588d33f9894c1fdf2833d0512f3b4 (patch)
treec76510fd390e20187554ea86a3fdabd2dfb65ca5 /src/daemon.zig
parentd1d5d3cfb18841604bba2057eeb3598f729b4475 (diff)
downloadpardes-4c2125ecf47588d33f9894c1fdf2833d0512f3b4.tar.gz
pardes-4c2125ecf47588d33f9894c1fdf2833d0512f3b4.zip
client #2, one frontend a session: the daemon answers `other <name>` for an attach to a session attached already (as it did for one of the other kind), so a second `pardes --attach` or `pardes-gui --attach` on this machine is a replica client through the replica socket, the path a remote one takes; a session tells the daemon its clients (SO_PEERCRED on each replica socket, at every join and leave) and `--ps` lists `client <pid> <command>` under it; the tty's per-terminal Screens (Screen.create/drop/destroy, primary, its stub and Killed slot), the Poster and its `from` input, the Gui's window-fields swap (window_fields, GuiWindow, copyWindow, freshWindow, follow, route, openAnotherWindow, closeView, windowsOpen, closeOtherWindows, resumeWindows), drawOthers and every harness's extra terminals (snapshot.zig's `start ... views=N` and `view <k>`, e2e_harness's initView and drainOthers, PARDES_TEST_VIEWS in both frontends, the GUI feed's `view;<k>`, the monkey's --views) go; the pump draws the one view this process shows (views.shown), through drawOne when a client's input was the last; an Upgrade from a build that kept a second terminal of its own lets it go (its stub told it is detached) and closes its view; deleted: views.snap, views-term.snap and views-pan.snap with their goldens (the in-process second terminal they drove is gone; views.zig's unit tests keep the per-view cursor, layout, register, sizing, closing and panning behaviour, test/replicas.zig and the soak the views of clients); the two views.zig tests that drew through drawOthers draw through drawOne; daemon.py's second terminal is a client listed by --ps, typed into in both terminals, detached from inside and closed; its second window a client listed by --ps and closed; the reference and next-daemon.md say so; no golden changes; gates: core-test 956/956 both, behaviour-test 248/248 both, unit-test 1487/1488 tui and 1530/1531 gui, pane-test 171/172 both, snap all 102 ok, process-test 0 of 37 failed on both, hxdiff 19 and hxparity 6 mismatches (the base's names), hxgolf 0, replicas, addon-test, gui-golden and monkey-gui 0 of 1 failed, daemon.py main, --window-clients and --gui ok, soak local pass (differ, mispred, view_bad, lost 0)
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/daemon.zig')
-rw-r--r--src/daemon.zig43
1 files changed, 37 insertions, 6 deletions
diff --git a/src/daemon.zig b/src/daemon.zig
index 93cc8fa1..bc9e9773 100644
--- a/src/daemon.zig
+++ b/src/daemon.zig
@@ -328,6 +328,9 @@ pub fn register(fd: ?c_int, k: Kind, named: []const u8, detached: bool) bool {
return true;
}
+/// A session's replica clients at once (replica.Peers): `--ps` lists them.
+const max_clients = 4;
+
fn tell(words: []const u8, fds: []const c_int) void {
if (link < 0) return;
_ = send(link, words, fds);
@@ -350,6 +353,16 @@ pub fn setState(detached: bool) void {
tell(if (detached) "state detached" else "state attached", &.{});
}
+/// This session's replica clients now, each its process's pid (0 when its
+/// socket's peer is not known), for `--ps`.
+pub fn clients(pids: []const libc.pid_t) void {
+ var buf: [64]u8 = undefined;
+ var w: std.Io.Writer = .fixed(&buf);
+ w.writeAll("clients") catch return;
+ for (pids[0..@min(pids.len, max_clients)]) |pid| w.print(" {d}", .{pid}) catch return;
+ tell(w.buffered(), &.{});
+}
+
/// Asks for the masters of sessions that died holding them; they come back
/// as an `adopted` note.
pub fn adopt() void {
@@ -684,6 +697,9 @@ const Conn = struct {
killed: bool = false,
name: [128]u8 = undefined,
name_len: usize = 0,
+ /// Its replica clients' pids, as it says them (`clients`); 0 not known.
+ clients: [max_clients]libc.pid_t = undefined,
+ nclients: u8 = 0,
fn named(c: *const Conn) []const u8 {
return c.name[0..c.name_len];
@@ -768,6 +784,7 @@ const Daemon = struct {
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 (s.clients[0..s.nclients]) |pid| w.print("client {d}\n", .{pid}) 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;
@@ -802,6 +819,7 @@ const Daemon = struct {
c.pid = pid;
c.detached = std.mem.eql(u8, state, "detached");
c.semantics = semantics;
+ c.nclients = 0;
c.name_len = @min(rest.len, c.name.len);
@memcpy(c.name[0..c.name_len], rest[0..c.name_len]);
return d.reply(i, "ok", &.{});
@@ -810,6 +828,14 @@ const Daemon = struct {
c.detached = std.mem.eql(u8, words.next() orelse "", "detached");
return;
}
+ if (std.mem.eql(u8, verb, "clients")) {
+ c.nclients = 0;
+ while (words.next()) |w| if (c.nclients < max_clients) {
+ c.clients[c.nclients] = std.fmt.parseInt(libc.pid_t, w, 10) catch 0;
+ c.nclients += 1;
+ };
+ return;
+ }
if (std.mem.eql(u8, verb, "pty")) {
const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch return;
if (got.nfds < 1) return;
@@ -872,7 +898,9 @@ const Daemon = struct {
/// The detached session of kind `k` called `wanted_name`, or the one
/// there is when it is empty; else null, and why not in `why`
- /// (0-terminated): "other <its name>" for one of the other kind.
+ /// (0-terminated): "other <its name>" for one to join as a replica
+ /// client (of the other kind, or attached already: a session shows one
+ /// frontend of its own).
fn find(d: *Daemon, k: Kind, wanted_name: []const u8, why: *[512]u8) ?u8 {
@memset(why, 0);
const other: []const u8 = switch (k) {
@@ -881,11 +909,10 @@ const Daemon = struct {
};
if (wanted_name.len != 0) {
for (&d.conns, 0..) |*s, i| if (s.fd >= 0 and s.kind != null and std.mem.eql(u8, s.named(), wanted_name)) {
- if (s.kind.? != k) {
+ if (s.kind.? != k or !s.detached) {
_ = std.fmt.bufPrint(why, "other {s}", .{wanted_name}) catch {};
return null;
}
- // Attached already: the session takes another view.
return @as(u8, @intCast(i));
};
_ = std.fmt.bufPrint(why, "no no session called '{s}'", .{wanted_name}) catch {};
@@ -904,13 +931,16 @@ const Daemon = struct {
found = @intCast(i);
};
if (count == 1) return found.?;
- // None detached: the one session there is, as another view of it.
+ // None detached: the one session there is, joined as a client.
if (count == 0) {
for (&d.conns, 0..) |*s, i| if (s.fd >= 0 and s.kind == k) {
count += 1;
found = @intCast(i);
};
- if (count == 1) return found.?;
+ if (count == 1) {
+ _ = std.fmt.bufPrint(why, "other {s}", .{d.conns[found.?].named()}) catch {};
+ return null;
+ }
count = 0;
// None of this kind at all: the one session there is, the other kind's.
var only: ?*Conn = null;
@@ -1061,7 +1091,8 @@ const Daemon = struct {
ninep_io.setCloexec(fd);
const c = &d.conns[i];
c.* = .{ .fd = fd, .kind = k, .pid = pid, .detached = detached, .semantics = semantics };
- c.name_len = @min(rest.len, c.name.len);
+ c.nclients = 0;
+ c.name_len = @min(rest.len, c.name.len);
@memcpy(c.name[0..c.name_len], rest[0..c.name_len]);
} else if (std.mem.eql(u8, what, "h")) {
const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue;