From e9b4f0e1c37c5c1f369d152d0a70b672433c94b5 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 00:02:06 -0300 Subject: kinds #3, addons: src/Addon.zig loads a library that brings a kind (each *.so of $XDG_DATA_HOME/pardes/addons, then /lib/pardes/addons at start; `Addon load `, `Addon unload ` refused while a pane of the kind is open, `Addon reload `; bare, the kinds here), from a copy of its bytes in a memfd so a reload is the file as it is now, its BLAKE3 kept; a load is marked on disk while it runs and a start that finds the mark skips that library until `Addon load` asks again; the crash record says `in addon , `; Kind.zig checks a head before taking it (size, ABI major, a paint, a name, words, leader paths and extensions none else has, the core's or another addon's: refused whole, in words; ctl with words; jobs paired), and a reload checks the new library beside the old (the old kind's own names looked past), opens every pane of the kind again in a fresh arena from the old state's dump and the new load (or its place and its words' last values), and only then swaps, dropping the old library's jobs; any failure says why and leaves the old one running; /addons lists ` static|loaded|refused: why`; the tail: an addon's words join /commands and Help marked `(addon )`, run from exec, a ctl and a leader path after the core's, a word that opens its kind opens a pane of it from anywhere and any other goes to a pane of its kind, its ctl read ends with the kind's fields, its body is the kind's text over 9P, its keys (all but Esc, as a raw terminal's) and clicks go to it, a 9P request steps its ticks and jobs as an event does; the dump record keeps the words' last values; the size of every record is asserted in the header and seen again from Kind.zig; pa_api gains `seed` (a number the same for a pane on every machine, for a game's board), and a zero cell is an unpainted one (PA_PAINTED, which pa_put sets), so a window is cleared by one memset; the hook in the settings agent's files is one arm each: exec.zig executeBuiltinLine's miss goes to Kind.runLine, builtins.zig gains the Addon word, config.zig its leader path (none); the tree gains /addons (README and reference.typ's tree, and an Addons section), builtins-.json the Addon row; no golden changes; tests: a crash mark skipped at two starts and cleared by a load (Addon.zig), the tree listing tests (tree.zig and the process suite's) with /addons; the README keeps its 45 lines Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/Addon.zig | 322 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 322 insertions(+) create mode 100644 src/Addon.zig (limited to 'src/Addon.zig') diff --git a/src/Addon.zig b/src/Addon.zig new file mode 100644 index 00000000..2a963cb2 --- /dev/null +++ b/src/Addon.zig @@ -0,0 +1,322 @@ +//! Addons: shared libraries that each bring a kind of pane (Kind.zig), +//! the host's side of it. Each `*.so` of `$XDG_DATA_HOME/pardes/addons` +//! and then `/lib/pardes/addons` at start, and `Addon load +//! ` after. An addon exports `pardes_addon`, a `pa_ops` +//! (src/kinds/pardes_addon.h); Kind.register checks it and takes it in. +//! +//! An addon runs in this process: one that crashes takes the session +//! with it. So a load is marked first (`addon-loading` in the data +//! directory, holding its path) and unmarked after; a start that finds +//! the mark moves it to `addon-crashed`, and skips that addon at every +//! start until `Addon load` asks for it by path again. +const std = @import("std"); +const Kind = @import("Kind.zig"); +const dump = @import("dump.zig"); +const pardes = @import("pardes.zig"); + +const Why = Kind.Buf(160); + +/// The core's `addon` effect performed, its answer the `addon` event: +/// `arg` a path to load (or reload), or the kind's name to let go. +pub fn perform(p: *pardes.Pardes, op: Kind.AddonOp, pane: u8, arg: []const u8) void { + var why: Why = .{}; + const ok = switch (op) { + .load => load(arg, &why), + .unload => ok: { + why.set(arg); + break :ok unload(arg); + }, + .reload => ok: { + if (!reload(p, arg, &why)) break :ok false; + why.set(arg); + break :ok true; + }, + }; + p.update(.{ .addon = .{ .op = op, .pane = pane, .ok = ok, .text = why.slice() } }); +} + +/// The addon at `path` loaded and its kind taken in: true, or false +/// with why not in `why`. +pub fn load(path: []const u8, why: *Why) bool { + var data_buf: [4096]u8 = undefined; + return loadMarked(dataDir(&data_buf), path, why); +} + +/// Where the crash mark goes: the data directory, none in a test (a +/// test's loads are no user's). +fn dataDir(buf: []u8) []const u8 { + if (@import("builtin").is_test) return ""; + return dump.defaultDirectory(buf) orelse ""; +} + +fn loadMarked(data: []const u8, path: []const u8, why: *Why) bool { + if (path.len >= 4096) return fail(why, "{s}: the path is too long", .{path}); + Kind.forgetRefused(path); + mark(data, path); + defer unmark(); + var hash: [32]u8 = undefined; + const lib = open(path, &hash, why) orelse { + Kind.note(path, why.slice()); + return false; + }; + const sym = std.c.dlsym(lib, "pardes_addon") orelse { + _ = std.c.dlclose(lib); + Kind.note(path, "refused: it has no pardes_addon"); + return fail(why, "{s}: refused: it has no pardes_addon", .{std.fs.path.basename(path)}); + }; + const ops: *const Kind.Ops = @ptrCast(@alignCast(sym)); + if (Kind.registerHashed(ops, path, lib, hash)) |refused| { + _ = std.c.dlclose(lib); + return fail(why, "{s}: {s}", .{ std.fs.path.basename(path), refused }); + } + why.set(Kind.headName(ops)); + return true; +} + +/// The library at `path` opened from a copy of its bytes in memory, so a +/// reload of a path already open is the file as it is now (dlopen hands +/// back the handle it has for a path it has), and their BLAKE3 in `hash`. +fn open(path: []const u8, hash: *[32]u8, why: *Why) ?*anyopaque { + var z: [4096]u8 = undefined; + const path_z = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return failNull(why, "the path is too long", .{}); + const f = std.c.fopen(path_z, "r") orelse return failNull(why, "cannot read it", .{}); + defer _ = std.c.fclose(f); + const gpa = std.heap.page_allocator; + var bytes: std.ArrayList(u8) = .empty; + defer bytes.deinit(gpa); + var chunk: [64 << 10]u8 = undefined; + while (true) { + const n = std.c.fread(&chunk, 1, chunk.len, f); + if (n == 0) break; + bytes.appendSlice(gpa, chunk[0..n]) catch return failNull(why, "out of memory", .{}); + if (bytes.items.len > 256 << 20) return failNull(why, "it is over 256 MiB", .{}); + } + std.crypto.hash.Blake3.hash(bytes.items, hash, .{}); + const fd = std.c.memfd_create("pardes-addon", 0); + if (fd < 0) return failNull(why, "no memfd for it", .{}); + // shortcut: the fd stays open while the process lives. dlopen hands + // back the library it has for a path it has, and /proc/self/fd/ of + // a closed fd is the next load's path too: an fd kept is a name kept + // (one an addon load; a dev loop of a hundred reloads is a hundred). + errdefer _ = std.c.close(fd); + var off: usize = 0; + while (off < bytes.items.len) { + const n = std.c.write(fd, bytes.items.ptr + off, bytes.items.len - off); + if (n <= 0) return failNull(why, "cannot copy it", .{}); + off += @intCast(n); + } + var proc: [64]u8 = undefined; + const at = std.fmt.bufPrintSentinel(&proc, "/proc/self/fd/{d}", .{fd}, 0) catch unreachable; + return std.c.dlopen(at, .{ .NOW = true }) orelse { + _ = std.c.close(fd); + return failNull(why, "{s}", .{if (std.c.dlerror()) |e| std.mem.span(e) else "cannot load it"}); + }; +} + +fn failNull(why: *Why, comptime fmt: []const u8, args: anytype) ?*anyopaque { + _ = why.print(fmt, args); + return null; +} + +/// `Addon reload `: its library loaded again from its path and +/// swapped in, or the old one left running and why not. +pub fn reload(p: *pardes.Pardes, name: []const u8, why: *Why) bool { + var path: [256]u8 = undefined; + const at = for (Kind.kinds()) |*l| { + if (l.ops != null and std.mem.eql(u8, l.nameOf(), name)) { + @memcpy(path[0..l.path.len], l.path.slice()); + break path[0..l.path.len]; + } + } else return fail(why, "no addon brought kind {s}", .{name}); + var data_buf: [4096]u8 = undefined; + mark(dataDir(&data_buf), at); + defer unmark(); + var hash: [32]u8 = undefined; + const lib = open(at, &hash, why) orelse return false; + const sym = std.c.dlsym(lib, "pardes_addon") orelse { + _ = std.c.dlclose(lib); + return fail(why, "refused: it has no pardes_addon", .{}); + }; + var old: ?*anyopaque = null; + if (!Kind.replace(p, name, @ptrCast(@alignCast(sym)), lib, hash, &old, why)) { + _ = std.c.dlclose(lib); + return false; + } + if (old) |o| _ = std.c.dlclose(o); + return true; +} + +/// Every addon let go, and the refused forgotten: the kinds a process +/// starts with (a test's session ends so, the kinds being the process's). +pub fn unloadAll() void { + for (0..Kind.max_kinds) |_| { + const l = for (Kind.kinds()) |*k| { + if (k.lib != null and k.ops != null) break k; + } else break; + var name: [16]u8 = l.name; + _ = unload(std.mem.sliceTo(&name, 0)); + } + for (0..Kind.max_kinds) |_| { + const l = for (Kind.kinds()) |*k| { + if (k.ops == null) break k; + } else break; + var path: [256]u8 = undefined; + @memcpy(path[0..l.path.len], l.path.slice()); + Kind.forgetRefused(path[0..l.path.len]); + } +} + +/// The kind `name` out, its library closed: false when none is loaded. +/// The caller has seen that no pane of it is open. +pub fn unload(name: []const u8) bool { + const gone = Kind.unregister(name) orelse return false; + if (gone.lib) |lib| _ = std.c.dlclose(lib); + return true; +} + +fn fail(why: *Why, comptime fmt: []const u8, args: anytype) bool { + _ = why.print(fmt, args); + return false; +} + +/// Every library of the two directories, in name order each, the user's +/// first (a kind of a name taken is refused, and listed so). A library +/// that crashed at its last load is listed, not loaded. +pub fn scan() void { + var data_buf: [4096]u8 = undefined; + const data = dataDir(&data_buf); + const crashed = crashedPath(data); + var dir_buf: [4096]u8 = undefined; + if (data.len > 0) scanDir(data, std.fmt.bufPrint(&dir_buf, "{s}/addons", .{data}) catch "", crashed.slice()); + var exe: [4096]u8 = undefined; + const n = std.c.readlink("/proc/self/exe", &exe, exe.len); + if (n > 0) { + const bin_dir = std.fs.path.dirname(exe[0..@intCast(n)]) orelse return; + const prefix = std.fs.path.dirname(bin_dir) orelse return; + scanDir(data, std.fmt.bufPrint(&dir_buf, "{s}/lib/pardes/addons", .{prefix}) catch "", crashed.slice()); + } +} + +fn scanDir(data: []const u8, dir: []const u8, crashed: []const u8) void { + if (dir.len == 0) return; + var z: [4096]u8 = undefined; + const dz = std.fmt.bufPrintSentinel(&z, "{s}", .{dir}, 0) catch return; + const d = std.c.opendir(dz) orelse return; + defer _ = std.c.closedir(d); + var names: [32][256]u8 = undefined; + var lens: [32]usize = undefined; + var count: usize = 0; + while (std.c.readdir(d)) |e| { + const name = std.mem.sliceTo(@as([*:0]const u8, @ptrCast(&e.name)), 0); + if (!std.mem.endsWith(u8, name, ".so") or name.len > 255 or count == names.len) continue; + @memcpy(names[count][0..name.len], name); + lens[count] = name.len; + count += 1; + } + var order: [32]usize = undefined; + for (0..count) |i| order[i] = i; + std.mem.sort(usize, order[0..count], Sort{ .names = &names, .lens = &lens }, Sort.lt); + for (order[0..count]) |i| { + var full: [4096]u8 = undefined; + const path = std.fmt.bufPrint(&full, "{s}/{s}", .{ dir, names[i][0..lens[i]] }) catch continue; + if (crashed.len > 0 and std.mem.eql(u8, path, crashed)) { + Kind.note(path, "skipped: it crashed its last load (Addon load asks for it again)"); + continue; + } + var why: Why = .{}; + _ = loadMarked(data, path, &why); + } +} + +const Sort = struct { + names: *const [32][256]u8, + lens: *const [32]usize, + fn lt(s: Sort, a: usize, b: usize) bool { + return std.mem.lessThan(u8, s.names[a][0..s.lens[a]], s.names[b][0..s.lens[b]]); + } +}; + +// ---- the crash mark ---- + +var marked: [4096]u8 = undefined; +var marked_len: usize = 0; + +fn markPath(buf: []u8, data: []const u8, comptime name: []const u8) ?[:0]const u8 { + return std.fmt.bufPrintSentinel(buf, "{s}/" ++ name, .{data}, 0) catch null; +} + +/// The load of `path` under way, on disk until `unmark`. +fn mark(data: []const u8, path: []const u8) void { + if (data.len == 0) return; + var buf: [4096]u8 = undefined; + const at = markPath(&buf, data, "addon-loading") orelse return; + const f = std.c.fopen(at, "w") orelse return; + _ = std.c.fwrite(path.ptr, 1, path.len, f); + _ = std.c.fclose(f); + @memcpy(marked[0..at.len], at); + marked_len = at.len; + // `Addon load` of the one that crashed asks for it again. + var cbuf: [4096]u8 = undefined; + const crashed = markPath(&cbuf, data, "addon-crashed") orelse return; + const was = readSmall(crashed); + if (std.mem.eql(u8, was.slice(), path)) _ = std.c.unlink(crashed); +} + +fn unmark() void { + if (marked_len == 0) return; + marked[marked_len] = 0; + _ = std.c.unlink(@ptrCast(&marked)); + marked_len = 0; +} + +/// The library that crashed its session's load: a mark left by a start +/// that never finished one becomes `kind-crashed`, which it stays. +fn crashedPath(data: []const u8) Kind.Buf(4096) { + var out: Kind.Buf(4096) = .{}; + if (data.len == 0) return out; + var lbuf: [4096]u8 = undefined; + var cbuf: [4096]u8 = undefined; + const loading = markPath(&lbuf, data, "addon-loading") orelse return out; + const crashed = markPath(&cbuf, data, "addon-crashed") orelse return out; + if (std.c.access(loading, 0) == 0) _ = std.c.rename(loading, crashed); + out.set(readSmall(crashed).slice()); + return out; +} + +fn readSmall(path: [*:0]const u8) Kind.Buf(4096) { + var out: Kind.Buf(4096) = .{}; + const f = std.c.fopen(path, "r") orelse return out; + defer _ = std.c.fclose(f); + const n = std.c.fread(&out.bytes, 1, out.bytes.len, f); + out.len = @intCast(n); + return out; +} + +test "a load that never finished is skipped at the next start, and every start after, until a load asks for it" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [4096]u8 = undefined; + const data = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + try tmp.dir.createDirPath(std.testing.io, "addons"); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "addons/crashy.so", .data = "not a library" }); + var lib_buf: [4096]u8 = undefined; + const lib = try std.fmt.bufPrint(&lib_buf, "{s}/addons/crashy.so", .{data}); + // The mark a load leaves when its session dies in it. + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "addon-loading", .data = lib }); + var dir_buf: [4096]u8 = undefined; + const dir = try std.fmt.bufPrint(&dir_buf, "{s}/addons", .{data}); + for (0..2) |_| { + scanDir(data, dir, crashedPath(data).slice()); + const last = Kind.kinds()[Kind.kinds().len - 1]; + try std.testing.expectEqualStrings(lib, last.path.slice()); + try std.testing.expect(std.mem.startsWith(u8, last.why.slice(), "skipped: it crashed its last load")); + Kind.forgetRefused(lib); + } + // Asked for by name, it is tried again (and refused here for what it is). + var why: Why = .{}; + try std.testing.expect(!loadMarked(data, lib, &why)); + try std.testing.expect(std.mem.indexOf(u8, Kind.kinds()[Kind.kinds().len - 1].why.slice(), "skipped") == null); + try std.testing.expectError(error.FileNotFound, tmp.dir.access(std.testing.io, "addon-crashed", .{})); + try std.testing.expectError(error.FileNotFound, tmp.dir.access(std.testing.io, "addon-loading", .{})); + Kind.forgetRefused(lib); +} -- cgit v1.3