diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 02:07:32 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 83b9d45b8665a80bdb70934aa23bbde5cc03d202 (patch) | |
| tree | c902b557ef624b07cd139659d0a891cd4809e03e /src/host_io.zig | |
| parent | b3d32656413734952bc918c2d4cfd12fede6c214 (diff) | |
| download | pardes-83b9d45b8665a80bdb70934aa23bbde5cc03d202.tar.gz pardes-83b9d45b8665a80bdb70934aa23bbde5cc03d202.zip | |
plumber: a look sends what it looks at (docs/next-plumber.md, phase 2): after the pane's own readings and once a name already open in a pane is focused (never sent), lookAt sends the trimmed name, type text, src pardes (term from a raw terminal, a kind's name from its pane), wdir the pane's directory, with what resolveLookTarget found of it (Plumb.Seen: isfile and isdir take it, so no second stat and Look's own readings of a URL or `//` stay no file); a message the rules give back to `edit` as sent is Look's own (Sent.look) and runs lookAt's switch on that target, one nothing takes or a started word declines is Look's own too, whose `.url` arm is now the word's search; the edit port's open of another message runs with look_text, which now means "unplumbed", so the edit port's lookAt never sends again (the recursion trap); the three diff hooks become the default rules of the record's §5.2 (a bare .diff/.patch to `Diff $file`, a commit hash in the editor to `Diff $0`, after the two edit sets), and `Diff` takes a file or a revision, a rule's Diff declining in silence (Pardes.plumb_declined) where it cannot (no hunk, not readable, no .git above): hunks.lookedAt and lookedAtCommit go, their bodies in hunks.word, the `needs` check of the bare form in the word, PaneKind.diff gone; the old one-line `Plumb` word, Core.plumb, look.Port, plumbRuleError, plumbTarget and resolve's rules block go: bare, `Plumb` opens <config>/plumbing as Config opens init (both through builtins.openConfigFile; `include basic` in a new one), an argument is refused in words, Save applies it (host_io.writeFile beside reapplyInitFile, Plumb.saved), and boot reads it (Plumb.boot in applyStartupConfig); a started program now runs detached as Plan 9's plumber's (the coordinator's decision for the user's question 4): Effect.start, every host's host_io.startDetached (double fork, setsid, /dev/null for 0-2, every other fd closed, argv exec'd in wdir with no shell, the child's errno back through a close-on-exec pipe, said as `plumb start: <prog>: <why>`), so exec_dir goes; resolveLookTarget's and Plumb.Send.search's directory walk is one look.Dirs; test: look.zig's Plumb test rewritten onto the rules (a rule's word, an open pane unsent, the hover's resolve, a URL with no web rule searched for) and a hash inside and outside a git work tree; addons.zig's Plumb test onto plumb/rules (a port no kind reads, its kind gone: Look's own); Plumb.zig: the word, the save and boot; replicas.zig gains a URL (speculated whole, confirmed, no rollback) and a hash, each the authority's and a replica's own; every look golden unchanged; the reference quotes src/plumbing whole and says the order, the changelog says so
Diffstat (limited to 'src/host_io.zig')
| -rw-r--r-- | src/host_io.zig | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/src/host_io.zig b/src/host_io.zig index 3141ff64..9e503e26 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -59,6 +59,9 @@ pub const Host = struct { set_clipboard: ?*const fn (ctx: ?*anyopaque, text: []const u8) void = null, read_clipboard: ?*const fn (ctx: ?*anyopaque) void = null, open_link: ?*const fn (ctx: ?*anyopaque, url: []const u8) void = null, + /// A plumbing rule's program (Effect.start): every host's is + /// startDetached. Null once it runs, else why not. + start: ?*const fn (ctx: ?*anyopaque, line: []const u8, why: []u8) ?[]const u8 = null, lsp: ?*const fn (ctx: ?*anyopaque, req: Lsp.Request) void = null, pipe: ?*const fn (ctx: ?*anyopaque, id: u32) void = null, /// Start the streaming search of this generation on a worker @@ -268,6 +271,9 @@ pub const Fallback = struct { files: std.StringHashMapUnmanaged([]u8) = .empty, clipboard: std.ArrayListUnmanaged(u8) = .empty, link: std.ArrayListUnmanaged(u8) = .empty, + /// The programs plumbing rules started, as Effect.start packs them, + /// one a line. + started: std.ArrayListUnmanaged(u8) = .empty, spawned: [pardes.MAX_PANES]bool = @splat(false), watched: [pardes.MAX_PANES]bool = @splat(false), @@ -280,6 +286,7 @@ pub const Fallback = struct { f.files.deinit(f.gpa); f.clipboard.deinit(f.gpa); f.link.deinit(f.gpa); + f.started.deinit(f.gpa); } pub fn writeFile(f: *Fallback, path: []const u8, bytes: []const u8) bool { @@ -315,6 +322,12 @@ pub const Fallback = struct { f.link.clearRetainingCapacity(); f.link.appendSlice(f.gpa, url) catch {}; } + + pub fn start(f: *Fallback, line: []const u8) ?[]const u8 { + f.started.appendSlice(f.gpa, line) catch return "out of memory"; + f.started.append(f.gpa, '\n') catch return "out of memory"; + return null; + } }; /// What a pixel host's loop takes from its workers, in order: each pane's @@ -2456,6 +2469,108 @@ const darwin = struct { } }; +/// A program a plumbing rule starts (Effect.start), as Plan 9's plumber +/// starts one (match.c's startup): out of the editor's way, a session of +/// its own reaped by init, stdin, stdout and stderr /dev/null and no other +/// fd of the editor's, its argv exec'd (no shell: a rule that wants one +/// says `sh -c`) in its directory. `line` is its directory, then its argv, +/// each ended by a NUL. Null once it runs, else why not, as errno says it, +/// in `why` (as the tape replays a host's words): a pipe closed at the +/// exec carries the child's errno back. +pub fn startDetached(_: ?*anyopaque, line: []const u8, why: []u8) ?[]const u8 { + const words = startWords(line) orelse return null; + const n = @min(words.len, why.len); + @memcpy(why[0..n], words[0..n]); + return why[0..n]; +} + +fn startWords(line: []const u8) ?[]const u8 { + var buf: [pardes.effect_path_cap]u8 = undefined; + if (line.len == 0 or line.len > buf.len or line[line.len - 1] != 0) return "malformed"; + @memcpy(buf[0..line.len], line); + var argv: [64:null]?[*:0]const u8 = @splat(null); + var n: usize = 0; + var at: usize = 0; + var dir: [*:0]const u8 = undefined; + while (at < line.len) { + const end = std.mem.indexOfScalarPos(u8, line, at, 0).?; + const word: [*:0]const u8 = @ptrCast(&buf[at]); + if (at == 0) dir = word else if (n < argv.len) { + argv[n] = word; + n += 1; + } else return "more than 64 words"; + at = end + 1; + } + if (n == 0) return "no program"; + var fds: [2]c_int = undefined; + if (libc.pipe(&fds) != 0) return "no pipe"; + for (fds) |fd| _ = libc.fcntl(fd, libc.F.SETFD, @as(c_int, libc.FD_CLOEXEC)); + const first = libc.fork(); + if (first < 0) { + _ = libc.close(fds[0]); + _ = libc.close(fds[1]); + return "no fork"; + } + if (first == 0) { + // Only async-signal-safe calls between fork and exec. + _ = libc.setsid(); + if (libc.fork() != 0) libc._exit(0); + resetChildSignals(); + // What failed, then errno: 'd' the directory, 'x' the exec. + const Fail = struct { + fn say(fd: c_int, what: u8) noreturn { + var said: [5]u8 = undefined; + said[0] = what; + std.mem.writeInt(u32, said[1..5], @intFromEnum(libc.errno(-1)), .little); + _ = libc.write(fd, &said, said.len); + libc._exit(127); + } + }; + const err_fd = libc.fcntl(fds[1], libc.F.DUPFD_CLOEXEC, @as(c_int, 3)); + if (err_fd < 0) libc._exit(127); + if (dir[0] != 0 and libc.chdir(dir) != 0) Fail.say(err_fd, 'd'); + const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR }, @as(libc.mode_t, 0)); + if (null_fd >= 0) for (0..3) |i| { + _ = libc.dup2(null_fd, @intCast(i)); + }; + // Every fd of the editor's but the errno pipe's gone: a pty's + // master held open would keep its terminal alive. + if (libc.dup2(err_fd, 3) < 0) libc._exit(127); + _ = libc.fcntl(3, libc.F.SETFD, @as(c_int, libc.FD_CLOEXEC)); + const shut = if (comptime builtin.os.tag == .linux) + std.os.linux.close_range(4, std.math.maxInt(i32), .{ .UNSHARE = false, .CLOEXEC = false }) == 0 + else + false; + if (!shut) { + var fd: c_int = 4; + while (fd < 1024) : (fd += 1) _ = libc.close(fd); + } + _ = execvp(argv[0].?, &argv); + Fail.say(3, 'x'); + } + _ = libc.close(fds[1]); + while (libc.waitpid(first, null, 0) < 0 and libc.errno(-1) == .INTR) {} + var said: [5]u8 = undefined; + var got: usize = 0; + while (got < said.len) { + const r = libc.read(fds[0], said[got..].ptr, said.len - got); + if (r < 0 and libc.errno(r) == .INTR) continue; + if (r <= 0) break; + got += @intCast(r); + } + _ = libc.close(fds[0]); + if (got < said.len) return null; + const code = std.mem.readInt(u32, said[1..5], .little); + const E = libc.E; + if (code == @intFromEnum(E.NOENT)) return if (said[0] == 'd') "its directory: no such directory" else "not found"; + if (code == @intFromEnum(E.ACCES)) return "permission denied"; + if (code == @intFromEnum(E.NOTDIR)) return "not a directory"; + if (code == @intFromEnum(E.NOEXEC)) return "not an executable"; + return if (said[0] == 'd') "its directory: cannot go there" else "cannot start it"; +} + +extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; + /// A terminal pane's shell is done with (its pane gone, its pty closed or at /// end of file): say hangup, for a shell that ignores the tty's, and see it /// reaped, killed if it is still there 100 ms on. A thread does the waiting @@ -3004,6 +3119,8 @@ pub fn writeFile(core: *pardes.Pardes, watches: *file_watch.Table, pane: u8, pat core.setMessage(pane, pardes.Messages.Message.stamp(&mbuf, "saved", path)); // The init file, saved here (Config): its settings take effect now. pardes.exec.reapplyInitFile(core, path, bytes); + // The plumbing file (Plumb): its rules in force now. + pardes.Plumb.saved(core, pane, path, bytes); } /// A Dump's write: into the state file under a label of its own @@ -3863,3 +3980,12 @@ test "LSP query answers, and a newer one cancels it" { core.update(.{ .lsp_resp = .{ .id = last, .rows = batch.items[0].lsp.rows } }); try std.testing.expect(core.lsp_ask.wait == null); } + +test "a plumbing rule's program starts detached, or says why not: a program not found, a directory not there" { + if (comptime builtin.os.tag != .linux) return; + var why: [64]u8 = undefined; + try std.testing.expect(startDetached(null, "/\x00true\x00", &why) == null); + try std.testing.expectEqualStrings("not found", startDetached(null, "/\x00no-such-program-x\x00", &why).?); + try std.testing.expectEqualStrings("its directory: no such directory", startDetached(null, "/no/such/dir\x00true\x00", &why).?); + try std.testing.expectEqualStrings("malformed", startDetached(null, "/\x00true", &why).?); +} |
