summaryrefslogtreecommitdiff
path: root/src/ninep
diff options
context:
space:
mode:
Diffstat (limited to 'src/ninep')
-rw-r--r--src/ninep/tree.zig43
1 files changed, 33 insertions, 10 deletions
diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig
index bd87f5bc..c3eb3636 100644
--- a/src/ninep/tree.zig
+++ b/src/ninep/tree.zig
@@ -4,7 +4,7 @@
//! extended here with the editor's reply payload; fs.zig keeps host access
//! and mounts.
//!
-//! /README /index /status /look /exec /log /screen /listeners
+//! /README /index /status /look /exec /log /screen /listeners /sessions
//! /pane/new an open makes a pane, a read names it
//! /pane/<serial> rmdir closes that pane
//! /pane/<serial>/{name,body,tag,ctl,addr,dot,limit,data,xdata,sel,
@@ -23,6 +23,7 @@ const layout = pardes.layout;
pub const pty = @import("pty.zig");
pub const sources = @import("sources.zig");
pub const screen = @import("screen.zig");
+const daemon = @import("../daemon.zig");
const Pardes = pardes.Pardes;
const Pane = pardes.Pane;
@@ -261,6 +262,10 @@ pub const TopFile = enum(u5) {
/// `pardes -`'s: a directory written, a new +Pager made there; a
/// read of the same open answers its serial.
pager,
+ /// The pardes daemon's sessions and shells, as `pardes --ps` lists
+ /// them: `daemon <pid>`, `session <pid> tty|gui attached|detached
+ /// <name>`, `shell <pid>` (the session above's), `orphan <pid>`.
+ sessions,
pub fn fileName(f: TopFile) []const u8 {
return if (f == .root) "/" else @tagName(f);
@@ -270,7 +275,7 @@ pub const TopFile = enum(u5) {
return switch (f) {
.root, .pane, .col => 0o755,
.look, .exec, .log, .focus, .ctl, .tag, .tagexec, .pager => 0o666,
- .README, .index, .status, .screen, .listeners, .new, .commands, .layout, .recent => 0o444,
+ .README, .index, .status, .screen, .listeners, .new, .commands, .layout, .recent, .sessions => 0o444,
};
}
@@ -463,6 +468,13 @@ pub fn stageDirent(out: *std.ArrayList(u8), gpa: std.mem.Allocator, node: u64, d
}
/// Answers a read from whatever the handler staged, windowed by offset and size.
+/// The daemon's registry (/sessions), its `list` answer as it gave it.
+fn daemonList(gpa: std.mem.Allocator) anyerror![]u8 {
+ var buf: [4096]u8 = undefined;
+ const said = daemon.ask("list", &buf) orelse return error.NoDaemon;
+ return gpa.dupe(u8, said);
+}
+
pub fn stagedReply(p: *Pardes, req: Req) Reply {
const out = &p.fs.out;
const off = @min(req.off, out.items.len);
@@ -669,7 +681,7 @@ fn topSize(p: *Pardes, f: TopFile) u64 {
// frame, /pane/new until an open has a pane to name, and the index,
// layout, recent list and listeners, which a stat would otherwise
// have to build to count.
- .root, .pane, .col, .screen, .new, .layout, .index, .recent, .listeners => 0,
+ .root, .pane, .col, .screen, .new, .layout, .index, .recent, .listeners, .sessions => 0,
.tag => cols.headerText(p, null).?.len,
.README => fs.help.len,
.status => ctl.statusLen(p),
@@ -777,7 +789,7 @@ fn readdir(p: *Pardes, req: Req, target: Target) Reply {
.root => {
// The guide, then what describes the session, then the
// files that act, then the directories.
- inline for (.{ TopFile.README, TopFile.index, TopFile.status, TopFile.focus, TopFile.ctl, TopFile.commands, TopFile.recent, TopFile.look, TopFile.exec, TopFile.pager, TopFile.log, TopFile.screen, TopFile.listeners, TopFile.layout, TopFile.tag, TopFile.tagexec, TopFile.pane, TopFile.col }) |t| {
+ inline for (.{ TopFile.README, TopFile.index, TopFile.status, TopFile.focus, TopFile.ctl, TopFile.commands, TopFile.recent, TopFile.look, TopFile.exec, TopFile.pager, TopFile.log, TopFile.screen, TopFile.listeners, TopFile.sessions, TopFile.layout, TopFile.tag, TopFile.tagexec, TopFile.pane, TopFile.col }) |t| {
if (skip > 0) skip -= 1 else stageDirent(out, p.gpa, @backingInt(t), t.dir(), t.fileName());
}
if (skip > 0) skip -= 1 else stageDirent(out, p.gpa, fs.os_root, true, "os");
@@ -856,7 +868,7 @@ fn open(p: *Pardes, req: Req, target: Target) Reply {
},
.screen => .{ .snapshot = null },
.log => .{ .log = .{} },
- .index, .layout, .recent, .commands, .status, .listeners => .{ .frozen = &.{} },
+ .index, .layout, .recent, .commands, .status, .listeners, .sessions => .{ .frozen = &.{} },
// The root's ctl opened only to read: its settings, frozen.
.ctl => if (linesOpen(target, req)) .lines else .{ .frozen = &.{} },
else => if (linesOpen(target, req)) .lines else return .{ .tag = req.tag },
@@ -1106,6 +1118,17 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply {
.log => events.readLog(p, req),
.screen => screen.readSnapshot(p, req, null),
.listeners => screen.readListeners(p, req),
+ .sessions => sessions: {
+ if (comptime !(pardes.hosted and daemon.supported)) break :sessions stagedReply(p, req);
+ // Out of the turn: the daemon answers within 2 s, or not at all.
+ const said = pardes.tape.site(.sessions, p.gpa, daemonList, .{p.gpa}) catch |err| break :sessions switch (err) {
+ error.OutOfMemory => Reply.fail(req.tag, E.NOMEM),
+ else => failText(req.tag, E.NOENT, "no pardes daemon answers"),
+ };
+ defer p.gpa.free(said);
+ p.fs.stage(p.gpa).appendSlice(p.gpa, said) catch break :sessions Reply.fail(req.tag, E.NOMEM);
+ break :sessions stagedReply(p, req);
+ },
.focus => ctl.readFocus(p, req),
.ctl => ctl.readRoot(p, req),
.commands => ctl.readCommands(p, req),
@@ -1534,13 +1557,13 @@ test "readdir lists the root and a pane directory without creating anything" {
@memcpy(top_bytes[0..top_reply.bytes.len], top_reply.bytes);
const top = th.dirents(top_bytes[0..top_reply.bytes.len], &buf);
try testing.expect(top.len >= 10);
- for ([_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "look", "exec", "pager", "log", "screen", "listeners", "layout", "tag", "tagexec", "pane", "col", "os" }, 0..) |name, i| {
+ for ([_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "look", "exec", "pager", "log", "screen", "listeners", "sessions", "layout", "tag", "tagexec", "pane", "col", "os" }, 0..) |name, i| {
try testing.expectEqualStrings(name, top[i].name);
- try testing.expectEqual(i >= 16, top[i].dir);
+ try testing.expectEqual(i >= 17, top[i].dir);
}
- try testing.expectEqual(@backingInt(TopFile.pane), top[16].node);
- try testing.expectEqual(@backingInt(TopFile.col), top[17].node);
- try testing.expectEqual(fs.os_root, top[18].node);
+ try testing.expectEqual(@backingInt(TopFile.pane), top[17].node);
+ try testing.expectEqual(@backingInt(TopFile.col), top[18].node);
+ try testing.expectEqual(fs.os_root, top[19].node);
var idbuf: [16]u8 = undefined;
try testing.expect(th.nameAt(top, try std.fmt.bufPrint(&idbuf, "{d}", .{serial})) == null);
try testing.expect(th.nameAt(top, "self") == null);