summaryrefslogtreecommitdiff
path: root/src/host
diff options
context:
space:
mode:
Diffstat (limited to 'src/host')
-rw-r--r--src/host/library.zig44
1 files changed, 32 insertions, 12 deletions
diff --git a/src/host/library.zig b/src/host/library.zig
index 6516704..6619d5b 100644
--- a/src/host/library.zig
+++ b/src/host/library.zig
@@ -3,10 +3,11 @@
//! `dlopen` returns the handle it already has for a path it loaded before, so
//! every load goes through a symlink with a fresh name to an fd open on the
//! library: what loads is the file that was opened, whatever a build renames
-//! over it meanwhile. The link is deleted right after loading: the mapping
-//! keeps the code alive, and nothing is left behind if the host dies. A
-//! library rewritten in place instead (same inode) would not load again, and
-//! would change the code under the running host.
+//! over it meanwhile. Link and fd live as long as the library is loaded: the
+//! name dlopen was given is where a panic's trace reads the sketch's debug
+//! info, so a host that dies leaves its dangling links behind. A library
+//! rewritten in place instead (same inode) would not load again, and would
+//! change the code under the running host.
const std = @import("std");
const linux = std.os.linux;
const c = @import("c");
@@ -15,6 +16,11 @@ const abi = @import("abi");
pub const Library = struct {
object: *c.SDL_SharedObject,
sketch: abi.Sketch,
+ /// What the link names (the library, or its fixed copy) and where the
+ /// link is, for `openFresh`'s libraries; -1 for `open`'s.
+ fd: i32 = -1,
+ scratch_dir: []const u8 = "",
+ serial: usize = 0,
/// Loads the library at `path`; `source` names it in messages.
pub fn open(path: [:0]const u8, source: []const u8) !Library {
@@ -47,22 +53,36 @@ pub const Library = struct {
pub fn openFresh(io: std.Io, source: []const u8, scratch_dir: []const u8, serial: usize) !Library {
const cwd = std.Io.Dir.cwd();
const file = try cwd.openFile(io, source, .{});
- defer file.close(io);
- const fixed = try withoutVersym(file.handle);
- defer if (fixed) |fd| {
- _ = linux.close(fd);
+ const fixed = withoutVersym(file.handle) catch |err| {
+ file.close(io);
+ return err;
};
+ if (fixed != null) file.close(io);
+ const fd = fixed orelse file.handle;
+ errdefer _ = linux.close(fd);
var target_buf: [32]u8 = undefined;
- const target = try std.mem.print(&target_buf, "/proc/self/fd/{d}", .{fixed orelse file.handle});
+ const target = try std.mem.print(&target_buf, "/proc/self/fd/{d}", .{fd});
var buf: [std.fs.max_path_bytes]u8 = undefined;
- const link = try std.mem.printSentinel(&buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0);
+ const link = try linkName(&buf, scratch_dir, serial);
try cwd.symLink(io, target, link, .{});
- defer cwd.deleteFile(io, link) catch {};
- return open(link, source);
+ errdefer cwd.deleteFile(io, link) catch {};
+ var library = try open(link, source);
+ library.fd = fd;
+ library.scratch_dir = scratch_dir;
+ library.serial = serial;
+ return library;
}
pub fn close(self: *Library) void {
c.SDL_UnloadObject(self.object);
+ if (self.fd < 0) return;
+ var buf: [std.fs.max_path_bytes]u8 = undefined;
+ _ = linux.unlink(linkName(&buf, self.scratch_dir, self.serial) catch unreachable);
+ _ = linux.close(self.fd);
+ }
+
+ fn linkName(buf: []u8, scratch_dir: []const u8, serial: usize) ![:0]u8 {
+ return std.mem.printSentinel(buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0);
}
};