diff options
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/client.zig | 9 | ||||
| -rw-r--r-- | runtime/daemon.zig | 57 | ||||
| -rw-r--r-- | runtime/main.zig | 65 | ||||
| -rw-r--r-- | runtime/session.zig | 11 |
4 files changed, 137 insertions, 5 deletions
diff --git a/runtime/client.zig b/runtime/client.zig index 75518552..1314d3fb 100644 --- a/runtime/client.zig +++ b/runtime/client.zig @@ -109,9 +109,10 @@ pub const Start = struct { } }; -/// How a client's joins ended: it quit, the session let it go, or it could -/// not join again (`Start.said`). -pub const Ended = enum { done, gone, failed }; +/// How a client's joins ended: it quit, the session let it go, it could +/// not join again (`Start.said`), it left the session running (a Detach in +/// it), or the session ended, with its code. +pub const Ended = union(enum) { done, gone, failed, left, ended: u8 }; pub fn Loop(comptime Frontend: type) type { return struct { @@ -170,6 +171,8 @@ pub fn Loop(comptime Frontend: type) type { // What it made since it joined, kept for its next start. if (s.have) |h| h.keep(s.r.core()); s.r.deinit(); + if (link.left) return .left; + if (link.ended) |code| return .{ .ended = code }; return if (link.gone) .gone else .done; } // The session again, the core it leaves held meanwhile: its diff --git a/runtime/daemon.zig b/runtime/daemon.zig index 80e1cc09..a2e890f9 100644 --- a/runtime/daemon.zig +++ b/runtime/daemon.zig @@ -600,6 +600,12 @@ fn stubWait(session: libc.pid_t, pipe: c_int) noreturn { const said = buf[0..len]; const out = std.Io.File.stderr(); const io = std.Io.Threaded.global_single_threaded.io(); + // `new --detach`'s session runs (`launched`): its name, for a script. + if (said.len > 0 and said[0] == 'r') { + var text: [520]u8 = undefined; + std.Io.File.stdout().writeStreamingAll(io, std.fmt.bufPrint(&text, "{s}\n", .{said[1..]}) catch said[1..]) catch {}; + libc._exit(0); + } if (said.len > 0 and said[0] == 'd') { var text: [700]u8 = undefined; const who = said[1..]; @@ -695,6 +701,57 @@ pub fn forkSession(conn: c_int) ?void { stubWait(pid, pipe[0]); } +/// `pardes new --detach`: the session forked off in a session of its own, +/// its stdin /dev/null, and this process its stub until the session runs +/// (`launched`): it then prints the name and exits 0, or with the +/// session's status if it ended first. Returns in the session, called +/// `named` (else by its pid); when no fork can be made, here, which is +/// then the session. +pub fn launch(named: []const u8) void { + var pipe: [2]c_int = undefined; + if (libc.pipe(&pipe) != 0) return; + ninep_io.setCloexec(pipe[0]); + ninep_io.setCloexec(pipe[1]); + const pid = libc.fork(); + if (pid < 0) { + _ = libc.close(pipe[0]); + _ = libc.close(pipe[1]); + return; + } + if (pid == 0) { + _ = libc.close(pipe[0]); + _ = libc.setsid(); + const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(libc.mode_t, 0)); + if (null_fd >= 0) _ = libc.dup2(null_fd, 0); + if (null_fd > 0) _ = libc.close(null_fd); + stub = pipe[1]; + launching = true; + setName(named); + return; + } + _ = libc.close(pipe[1]); + stubWait(pid, pipe[0]); +} + +/// Forked by `launch`, and its stub not told yet. +var launching = false; + +/// The session of a `new --detach` runs: registered (the daemon took its +/// hello, when there is one) and listening. Its stub told, with its name; +/// its stdout and stderr, still the launch's until now, let go. +pub fn launched() void { + if (!launching) return; + launching = false; + _ = kept(); + tellStub(stub, "r{s}", .{name()}); + stub = -1; + const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(libc.mode_t, 0)); + if (null_fd < 0) return; + _ = libc.dup2(null_fd, 1); + _ = libc.dup2(null_fd, 2); + if (null_fd > 2) _ = libc.close(null_fd); +} + // ---- the daemon ---- const Conn = struct { diff --git a/runtime/main.zig b/runtime/main.zig index 36c25f2b..29c19bc0 100644 --- a/runtime/main.zig +++ b/runtime/main.zig @@ -323,8 +323,10 @@ const cmds = std.EnumArray(Cmd, Doc).init(.{ .doc = \\Start a session here, even in a pane of another: in this terminal for \\pardes, a window for pardes-gui, or with --detach none until one - \\attaches. Bare `pardes` takes the same flags, and starts one unless - \\it is in a pane (where it hands its FILE to that session). + \\attaches, which returns once the session runs and prints its name. + \\Bare `pardes` takes the same flags, and starts one unless it is in a + \\pane (where it hands its FILE to that session); its --detach is the + \\session itself, until it ends (a supervisor's way). , .examples = &.{ .{ "pardes new", "a session here, its shell pane in tty mode" }, @@ -1157,6 +1159,8 @@ fn nativeMain(std_init: std.process.Init) !void { const resuming = upgrade.resumed != null; var session_only = true; var new_session = false; + // `pardes new`: with --detach it returns once the session runs. + var new_verb = false; var explicit_tty = false; var detach: ?[]const u8 = null; var attach: ?[]const u8 = null; @@ -1168,6 +1172,7 @@ fn nativeMain(std_init: std.process.Init) !void { if (!resuming and args.len > 1) if (std.meta.stringToEnum(Cmd, args[1])) |cmd| switch (cmd) { .new => { new_session = true; + new_verb = true; i = 2; }, .attach => { @@ -1531,6 +1536,9 @@ fn nativeMain(std_init: std.process.Init) !void { // socket holds already (a plain session is named by its pid too). if (name.len != 0 and !resuming and ninep_io.taken(name)) die(init.io, "a session named {s} is already running", .{name}); if (opts.ninep_name.len == 0) opts.ninep_name = name; + // `new --detach` forks the session and returns once it runs; the + // flag alone is the session itself (a supervisor's, a harness's). + if (new_verb and !resuming) daemon.launch(opts.ninep_name); // Said where it was started (stderr, as the log says it): no pane // anyone sees would show it. The session runs on, with no 9P. var pid_buf: [16]u8 = undefined; @@ -1559,6 +1567,11 @@ fn nativeMain(std_init: std.process.Init) !void { }; detached = if (pardes.platform == .gui) !rec.window else rec.nttys == 0; daemon.resumeLink(rec.link, rec.stub, kind, opts.ninep_name, detached); + } else if (pardes.platform == .tui and detach == null and std.c.getenv("PARDES_PAIR") != null) { + // The last step's way, behind its variable until it is the default: + // this terminal a client of a session forked off for it (`pair`). + pair(init, &opts); + term = .detached; } else if (daemon.wanted()) { if (detach != null or pardes.platform == .gui) { _ = daemon.register(null, kind, opts.ninep_name, detach != null); @@ -1695,6 +1708,54 @@ fn commOf(io: std.Io, pid: []const u8, buf: *[64]u8) []const u8 { return std.mem.trimEnd(u8, std.Io.Dir.cwd().readFile(io, path, buf) catch return "", "\n"); } +/// PARDES_PAIR: `pardes` in a terminal as a session and its first client +/// over a socket pair (docs/next-run.md; the last step's design, 1.1). The +/// daemon dialled first, as for a stub; then a fork: the parent the CLIENT +/// (the shell's job, the terminal its own; signals reach what draws), its +/// first join on its end of the pair; the child the SESSION (setsid, 0, 1 +/// and 2 on /dev/null, detached), its end of the pair its first client, +/// taken before its first effects (runtime/session.zig). Returns in the +/// session; the client exits. No fork to be had: returns, and this process +/// is the session, with no terminal: said by the caller's run. +fn pair(init: std.process.Init, opts: *pardes.Options) void { + const conn: ?c_int = if (daemon.wanted()) daemon.connect(true) else null; + var fds: [2]c_int = undefined; + if (std.c.socketpair(std.c.AF.UNIX, std.c.SOCK.STREAM | std.c.SOCK.CLOEXEC, 0, &fds) != 0) + die(init.io, "no socket pair for a session and its terminal", .{}); + // The session laid out at the terminal's size: no reflow at the join. + var ws: std.posix.winsize = undefined; + if (std.c.ioctl(0, std.posix.T.IOCGWINSZ, &ws) == 0 and ws.col > 0 and ws.row > 0) { + opts.cols = ws.col; + opts.rows = ws.row; + } + const pid = std.c.fork(); + if (pid < 0) die(init.io, "no fork for a session and its terminal", .{}); + if (pid == 0) { + _ = std.c.close(fds[1]); + _ = std.c.setsid(); + const null_fd = std.c.open("/dev/null", .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(std.c.mode_t, 0)); + if (null_fd >= 0) for (0..3) |i| { + _ = std.c.dup2(null_fd, @intCast(i)); + }; + if (null_fd > 2) _ = std.c.close(null_fd); + opts.pair = fds[0]; + // The daemon is the client's to start; the hello is this one's. + daemon.startPending(false); + if (conn) |c| _ = daemon.register(c, .tty, opts.ninep_name, true); + return; + } + _ = std.c.close(fds[0]); + if (conn) |c| _ = std.c.close(c); + daemon.startPending(true); + var name_buf: [16]u8 = undefined; + const name = if (opts.ninep_name.len != 0) opts.ninep_name else std.fmt.bufPrint(&name_buf, "{d}", .{pid}) catch unreachable; // a pid fits 16 + var sock_buf: [ninep_io.sun_path_len:0]u8 = undefined; + var o = opts.*; + o.pair = fds[1]; + @import("dev").tui.runClient(init, o, daemon.replicaSocket(&sock_buf) orelse "", name, false) catch |err| die(init.io, "{t}", .{err}); + std.process.exit(0); +} + /// The replica client of this build's frontend: a terminal's (tui.zig) or a /// window's (gui.zig). fn clientOf(init: std.process.Init, opts: pardes.Options, path: []const u8, name: []const u8, far: bool) !void { diff --git a/runtime/session.zig b/runtime/session.zig index fdd72c2b..5d7eff78 100644 --- a/runtime/session.zig +++ b/runtime/session.zig @@ -103,6 +103,10 @@ pub fn Loop(comptime Frontend: type) type { defer l.peers.deinit(); // Before the core goes: each client sees its link end. defer l.peers.dropAll(l.core); + // A first client on the socket it was started with, before its + // first effects: it has every terminal from its first byte, and + // no reload (Core.openView). + if (o.pair) |fd| l.peers.join(l.core, .{ .fd = fd }); l.effects(); // ...and its shells, once a fresh session's own has a directory // to adopt them in. @@ -113,12 +117,17 @@ pub fn Loop(comptime Frontend: type) type { l.ptys.start(l.core); l.watches.start(); try fe.start(&l); + // A `new --detach`'s launch returns now. + daemon.launched(); while (!l.core.quit) { pardes.turn.bump(&pardes.turn.restores); try l.core.pump(l.host); // Its unit to each replica client, and theirs taken in. l.peers.boundary(l.core); + // A pair's session with no daemon ends with its last link: + // nothing could attach to it again. + if (o.pair != null and daemon.link < 0 and l.peers.count() == 0) break; fe.sent(); upgrade.locate(); // Asked in the step (the word), done between steps: the @@ -132,6 +141,8 @@ pub fn Loop(comptime Frontend: type) type { if (l.core.quit) break; if (l.core.takeRestore()) |rp| l.restore(rp); } + // The clients' last word: the session ended (Exit). + l.peers.ended(0); // Its serve threads read the core: they end first. if (l.fs) |f| { f.deinit(l.gpa); |
