diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/daemon.zig | 32 | ||||
| -rw-r--r-- | src/main.zig | 40 |
2 files changed, 59 insertions, 13 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 <name>": 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 <its name>". + 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=<name> ...named <name> 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=<sock>:<name>. Returns when the + \\ session detaches or ends \\ --attach=<name> ...the session called <name> (its --9p or - \\ --detach name, else its pid) + \\ --detach name, else its pid; see --ps) \\ --attach=<host>:<name> a client of session <name> 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-")) { |
