diff options
Diffstat (limited to 'src/daemon.zig')
| -rw-r--r-- | src/daemon.zig | 43 |
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; |
