diff options
Diffstat (limited to 'src/replica.zig')
| -rw-r--r-- | src/replica.zig | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/replica.zig b/src/replica.zig index 40353d89..622537d7 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -1443,11 +1443,24 @@ pub const Framer = struct { } }; +/// The process at the other end of unix socket `fd` (Linux's SO_PEERCRED), +/// or 0: a client's through ssh is ssh's. +fn peerPid(fd: c_int) libc.pid_t { + if (comptime builtin.os.tag != .linux) return 0; + const Cred = extern struct { pid: libc.pid_t, uid: libc.uid_t, gid: libc.gid_t }; + var cred: Cred = undefined; + var len: libc.socklen_t = @sizeOf(Cred); + if (libc.getsockopt(fd, libc.SOL.SOCKET, libc.SO.PEERCRED, &cred, &len) != 0) return 0; + return cred.pid; +} + pub const Peer = struct { peers: *Peers, fd: c_int, id: u16, view: u8, + /// Its process (its socket's peer), for `--ps`; 0 not known. + pid: libc.pid_t = 0, /// Its bye said it joins again for its view (a divergence's rejoin): /// kept for it (views.away), not closed. Its reader's, read after its end. again: bool = false, @@ -2011,6 +2024,8 @@ pub const Peers = struct { return; } slot.* = peer; + peer.pid = peerPid(fd); + ps.tellClients(); // Its terminals afresh, after its dump and before any unit. if (ps.screens.addPeer()) |sid| { peer.sid = sid; @@ -2025,6 +2040,7 @@ pub const Peers = struct { slot.* = null; }; log.info("client {d} left '{s}'", .{ peer.id, ps.name }); + ps.tellClients(); ps.screens.dropPeer(peer.sid); core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); ps.closeViewOf(core, peer, peer.again); @@ -2120,6 +2136,17 @@ pub const Peers = struct { peer.dump = &.{}; } + /// The daemon told the clients there are now (`pardes --ps`). + fn tellClients(ps: *const Peers) void { + var pids: [pardes.views.max]libc.pid_t = undefined; + var n: usize = 0; + for (ps.list) |slot| if (slot) |peer| { + pids[n] = peer.pid; + n += 1; + }; + daemon.clients(pids[0..n]); + } + /// Detach typed in client view `k`'s: its link ends (its reader says /// it is gone, and the next boundary lets it go). pub fn detach(ps: *Peers, k: u8) bool { @@ -2155,6 +2182,7 @@ pub const Peers = struct { ps.screens.dropPeer(peer.sid); ps.letGo(peer); }; + ps.tellClients(); ps.stop(core); ps.lock.lockUncancelable(ps.io); for (ps.inbox.items) |note| switch (note) { |
