From 15bd47670102ceae36565ee299fd8a40625c3b21 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 31 Oct 2026 21:12:16 -0300 Subject: daemon: --attach= to a session of the other kind (pardes in a terminal for a window's session, pardes-gui for a terminal's) joins it as a replica client through this machine's pardes-replica.sock, where it was refused in words; the daemon answers `other ` (a named one, or with none of the client's kind the one session there is) and the client goes on as --attach=:; --help says attach takes an attached session too; test/daemon.py's window test attaches a terminal to the window session by name (the old build refused it, 'is a window') Co-Authored-By: Claude Opus 5.5 --- src/daemon.zig | 32 +++++++++++++++++++++++++++----- src/main.zig | 40 ++++++++++++++++++++++++++++++++-------- test/daemon.py | 14 +++++++++----- 3 files changed, 68 insertions(+), 18 deletions(-) diff --git a/src/daemon.zig b/src/daemon.zig index b93f5d43..2a70c772 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -473,7 +473,15 @@ pub fn quit() void { // ---- `pardes --attach`: a terminal, or a window, for a detached session ---- -pub const Answer = union(enum) { ok: libc.pid_t, no: []const u8 }; +/// `other`: the session is the other kind (a window's for a terminal, or a +/// terminal's for a window), named so: joined as a replica client instead +/// (`replicaSocket`). +pub const Answer = union(enum) { ok: libc.pid_t, no: []const u8, other: []const u8 }; + +/// This machine's replica socket, which joins a session of either kind. +pub fn replicaSocket(buf: *[sun_path_len:0]u8) ?[:0]const u8 { + return place(buf, "pardes-replica.sock"); +} /// Asks the daemon to hand `fds` (a terminal and a stub's pipe, or none for /// a window) to the detached session `named` (any one, when empty), and @@ -490,6 +498,7 @@ pub fn attach(k: Kind, named: []const u8, fds: []const c_int, buf: []u8) Answer const pid = std.fmt.parseInt(libc.pid_t, got.words[3..], 10) catch return .{ .no = "the daemon answered nonsense" }; return .{ .ok = pid }; } + if (std.mem.startsWith(u8, got.words, "other ")) return .{ .other = got.words["other ".len..] }; return .{ .no = if (std.mem.startsWith(u8, got.words, "no ")) got.words[3..] else got.words }; } @@ -860,7 +869,8 @@ const Daemon = struct { const wanted_name = std.mem.trim(u8, words.rest(), " "); var why: [512]u8 = undefined; const target = d.find(k, wanted_name, &why) catch |err| return d.reply(i, switch (err) { - error.Said => std.mem.sliceTo(&why, 0), + // "other ": the client joins it through the replica socket. + error.Said, error.Other => std.mem.sliceTo(&why, 0), }, &.{}); const t = &d.conns[target]; if (!send(t.fd, "attach", got.fds[0..got.nfds])) { @@ -875,7 +885,8 @@ const Daemon = struct { /// The detached session of kind `k` called `wanted_name`, or the one /// there is when it is empty; else why not, in `why` (0-terminated). - fn find(d: *Daemon, k: Kind, wanted_name: []const u8, why: *[512]u8) error{Said}!u8 { + /// One of the other kind is error.Other, `why` "other ". + fn find(d: *Daemon, k: Kind, wanted_name: []const u8, why: *[512]u8) error{ Said, Other }!u8 { @memset(why, 0); const other: []const u8 = switch (k) { .tty => "a window: attach it with `pardes-gui --attach", @@ -884,8 +895,8 @@ 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) { - _ = std.fmt.bufPrint(why, "no '{s}' is {s}={s}`", .{ wanted_name, other, wanted_name }) catch {}; - return error.Said; + _ = std.fmt.bufPrint(why, "other {s}", .{wanted_name}) catch {}; + return error.Other; } // Attached already: the session takes another view. return @intCast(i); @@ -914,6 +925,17 @@ const Daemon = struct { }; if (count == 1) return found.?; count = 0; + // None of this kind at all: the one session there is, the other kind's. + var only: ?*Conn = null; + var sessions: usize = 0; + for (&d.conns) |*s| if (s.fd >= 0 and s.kind != null) { + sessions += 1; + only = s; + }; + if (found == null and sessions == 1) { + _ = std.fmt.bufPrint(why, "other {s}", .{only.?.named()}) catch {}; + return error.Other; + } } if (count == 0 and others == 0) { w.writeAll("no no detached session is running") catch {}; diff --git a/src/main.zig b/src/main.zig index f9118dfc..a07991f5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -108,12 +108,16 @@ const help_text = \\ --detach= ...named rather than this process's pid, \\ as --9p names it. One path component: no '/' \\ and nothing empty - \\ --attach show the one detached session here: this - \\ terminal for `pardes`, a window again for - \\ `pardes-gui`, each of its own kind. Returns when - \\ the session detaches or ends + \\ --attach show the one detached session here (with + \\ none, the one session): in this terminal for + \\ `pardes`, a window for `pardes-gui`. One + \\ attached already gets another view; one of the + \\ other kind (a window's session in a terminal, + \\ or the reverse) is joined as a client, as + \\ --attach=:. Returns when the + \\ session detaches or ends \\ --attach= ...the session called (its --9p or - \\ --detach name, else its pid) + \\ --detach name, else its pid; see --ps) \\ --attach=: a client of session on another machine, \\ a terminal's or a window's (either kind's \\ session), over `ssh -L` to its pardes-replica.sock @@ -811,7 +815,7 @@ fn nativeMain(std_init: std.process.Init) !void { if (attach) |name| if (std.mem.lastIndexOfScalar(u8, name, ':')) |colon| { remoteAttach(init, opts, name[0..colon], name[colon + 1 ..]); }; - if (attach) |name| if (!resuming) attachTo(init.io, name); + if (attach) |name| if (!resuming) attachTo(init, opts, name); if (detach) |name| { // Said plainly, never a Zig error trace: a name a live session's // socket holds already (a plain session is named by its pid too). @@ -1099,8 +1103,11 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, /// `pardes --attach[=name]` in a terminal: this terminal handed (through /// the daemon) to the detached session of that name, or the only one, and /// this process its stub until the session leaves it. `pardes-gui -/// --attach`: the session asked to open its window again, and done. -fn attachTo(io: std.Io, name: []const u8) noreturn { +/// --attach`: the session asked to open its window again, and done. A +/// session of the other kind is joined as a replica client, through this +/// machine's replica socket. +fn attachTo(init: std.process.Init, opts: pardes.Options, name: []const u8) noreturn { + const io = init.io; var words: [600]u8 = undefined; const Say = struct { fn no(o: std.Io, why: []const u8) noreturn { @@ -1113,6 +1120,7 @@ fn attachTo(io: std.Io, name: []const u8) noreturn { if (pardes.platform == .gui) switch (daemon.attach(.gui, name, &.{}, &words)) { .ok => std.process.exit(0), .no => |why| Say.no(io, why), + .other => |named| crossAttach(init, opts, named), }; const fd = std.c.open("/dev/tty", .{ .ACCMODE = .RDWR, .CLOEXEC = true, .NOCTTY = true }, @as(std.c.mode_t, 0)); if (fd < 0) Say.no(io, "no terminal to attach: /dev/tty will not open"); @@ -1128,9 +1136,25 @@ fn attachTo(io: std.Io, name: []const u8) noreturn { daemon.stubWait(pid, false, pipe[0]); }, .no => |why| Say.no(io, why), + .other => |named| { + _ = std.c.close(fd); + _ = std.c.close(pipe[0]); + _ = std.c.close(pipe[1]); + crossAttach(init, opts, named); + }, } } +/// A session of the other kind (`--attach`'s): a replica client of it here. +fn crossAttach(init: std.process.Init, opts: pardes.Options, name: []const u8) noreturn { + var sock_buf: [ninep_io.sun_path_len:0]u8 = undefined; + const sock = daemon.replicaSocket(&sock_buf) orelse { + std.Io.File.stderr().writeStreamingAll(init.io, "pardes: --attach: no runtime directory for the replica socket\n") catch {}; + std.process.exit(1); + }; + remoteAttach(init, opts, sock, name); +} + fn parseCtrlKey(raw: []const u8) ?u21 { var value = std.mem.trim(u8, raw, " \t\r\n"); if (value.len >= 5 and std.ascii.eqlIgnoreCase(value[0..5], "ctrl-")) { diff --git a/test/daemon.py b/test/daemon.py index bf575fd7..2c2ee318 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -1288,16 +1288,20 @@ def gui(binary, cycles): assert not sized(client), 'the first window stays' assert proc.poll() is None print('daemon: a second window on an attached window session, detached alone') - # Another kind is refused in words, and so is a name nobody has. + # Another kind joins as a replica client: a terminal of the window's + # session, by its name alone. A name nobody has is refused in words. client.write(f'/pane/{shell}/exec', b'Detach\n') wait_for('the window closed', lambda: sized(client)) term = Term() tty_attach = term.spawn([binary.replace('pardes-gui', 'pardes'), '--attach=win'], env) - assert tty_attach.wait(timeout=10) == 1 - wait_for('the refusal in the terminal', lambda: b'is a window' in term.text()) - said = term.text() + wait_for('the window session drawn in the terminal', lambda: b'\x1b[?1049h' in term.text()) + time.sleep(1) + assert tty_attach.poll() is None, term.text()[-400:] + tty_attach.terminate() + tty_attach.wait(timeout=10) term.close() - assert b'is a window' in said, said + assert proc.poll() is None + print('daemon: a terminal attached to a window session by name, as a client') nobody = subprocess.run([binary, '--attach=nobody'], env=env, cwd=root, timeout=10, stdout=subprocess.PIPE, stderr=subprocess.PIPE) assert nobody.returncode == 1 and b"no session called 'nobody'" in nobody.stderr, nobody.stderr -- cgit v1.3