diff options
Diffstat (limited to 'src/Addon.zig')
| -rw-r--r-- | src/Addon.zig | 322 |
1 files changed, 322 insertions, 0 deletions
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 `<prefix>/lib/pardes/addons` at start, and `Addon load +//! <path>` 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/<n> 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 <name>`: 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); +} |
