summaryrefslogtreecommitdiff
path: root/src/9p_io.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/9p_io.zig')
-rw-r--r--src/9p_io.zig121
1 files changed, 71 insertions, 50 deletions
diff --git a/src/9p_io.zig b/src/9p_io.zig
index 446c24de..87972b43 100644
--- a/src/9p_io.zig
+++ b/src/9p_io.zig
@@ -174,8 +174,15 @@ pub const Listener = struct {
io: std.Io,
/// The core that answers; `reset` puts a replacement in.
core: *pardes.Pardes,
- runner: Runner = undefined,
+ /// cloud9's runner, sized for every connection's buffers and parks (some
+ /// 136 MiB): pages of its own (`allocRunner`), written only as clients
+ /// take slots, so an idle session does not hold them resident.
+ runner: *Runner = undefined,
stopping: std.atomic.Value(bool) = .init(false),
+ /// The watchers of held Edit writes (watchEdit): canceled and awaited
+ /// by `deinit`, once the turn lets a waiting one through, so none wakes
+ /// into a freed Listener.
+ edit_watchers: std.Io.Group = .init,
/// A connection wrote the Restore and has yet to answer it.
restore_writer: std.atomic.Value(bool) = .init(false),
/// Clients turned away with every slot taken, since the log last said so.
@@ -354,8 +361,7 @@ pub const Listener = struct {
const later: pardes.ctlfs.Reply = .{ .tag = req.tag, .status = .again };
const ticket = conn.hold(&later) orelse return;
core.fs.edit_hold = .{ .asker = conn, .slot = ticket.slot, .seq = ticket.seq, .tag = req.tag, .node = req.node, .handle = req.handle, .written = reply.written };
- const watcher = std.Thread.spawn(.{}, watchEdit, .{ l, conn, ticket }) catch return;
- watcher.detach();
+ l.edit_watchers.concurrent(l.io, watchEdit, .{ l, conn, ticket }) catch {};
return;
}
const restoring = core.restore_req != null;
@@ -457,13 +463,13 @@ pub const Listener = struct {
/// flush, so it is looked for, every 100 ms; a park out for a retry is
/// not waiting either, so only three looks in a row with no write being
/// served count.
- fn watchEdit(l: *Listener, conn: *Runner.Conn, ticket: cloud9.fs.Ticket) void {
- const io = pardes.turn.io orelse return;
+ fn watchEdit(l: *Listener, conn: *Runner.Conn, ticket: cloud9.fs.Ticket) error{Canceled}!void {
var misses: u8 = 0;
while (!l.stopping.load(.acquire)) {
- io.sleep(.fromMilliseconds(100), .awake) catch return;
+ try l.io.sleep(.fromMilliseconds(100), .awake);
_ = pardes.turn.take();
defer pardes.turn.give();
+ if (l.stopping.load(.acquire)) return;
const core = l.core;
const h = core.fs.edit_hold orelse return;
if (h.slot != ticket.slot or h.seq != ticket.seq or h.asker != @as(*anyopaque, conn)) return;
@@ -914,6 +920,7 @@ pub const Listener = struct {
// A connection task may be waiting for the turn; it answers
// nothing more once stopping, but the wait itself must end.
pardes.turn.rest();
+ l.edit_watchers.cancel(l.io);
// An answer given as the editor quit (a Kill written to /ctl)
// may still be on its way out: let it go before the cut.
const deadline = nowMs() +| 200;
@@ -942,10 +949,26 @@ pub const Listener = struct {
z[l.path_len] = 0;
_ = libc.unlink(z[0..l.path_len :0]);
}
+ freeRunner(l.runner);
gpa.destroy(l);
}
};
+/// The runner's memory, from the page allocator with no fill: an Allocator's
+/// `create` writes `undefined` over all of it in a safe build, which would make
+/// every page resident at startup.
+fn allocRunner() ?*Runner {
+ if (comptime !supported) return @constCast(&@as(Runner, {}));
+ const bytes = std.heap.page_allocator.rawAlloc(@sizeOf(Runner), .of(Runner), @returnAddress()) orelse return null;
+ @import("memory.zig").sparse(bytes[0..@sizeOf(Runner)]);
+ return @ptrCast(@alignCast(bytes));
+}
+
+fn freeRunner(runner: *Runner) void {
+ if (comptime !supported) return;
+ std.heap.page_allocator.rawFree(@as([*]u8, @ptrCast(runner))[0..@sizeOf(Runner)], .of(Runner), @returnAddress());
+}
+
/// The socket this process listens on, kept where a fatal signal's handler
/// can reach it without allocating (unlinkOwnSocket); empty when none.
var own_socket: [sun_path_len:0]u8 = undefined;
@@ -1065,6 +1088,10 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [
}
const l = gpa.create(Listener) catch return null;
l.* = .{ .io = io, .core = core };
+ l.runner = allocRunner() orelse {
+ gpa.destroy(l);
+ return null;
+ };
pardes.turn.start(io);
pardes.turn.wake_parked = Listener.wakeParked;
pardes.turn.answer_held = Listener.answerHeld;
@@ -1551,7 +1578,7 @@ test "a socket path too long for sun_path is no listener, and leaves the editor'
defer if (old_copy) |o| {
_ = setenv("XDG_RUNTIME_DIR", o, 1);
} else {
- _ = unsetenv("XDG_RUNTIME_DIR");
+ _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR");
};
const long = &@as([120]u8, @splat('n'));
try testing.expect(listen(testing.io, testing.allocator, p, long, "", null, null) == null);
@@ -1572,7 +1599,7 @@ test "a listening editor posts itself into the 9P registry and unposts on stop"
if (old_runtime) |v| {
_ = setenv("XDG_RUNTIME_DIR", v, 1);
gpa.free(v);
- } else _ = unsetenv("XDG_RUNTIME_DIR");
+ } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR");
}
try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1));
@@ -1633,7 +1660,7 @@ test "Unix TCP and QUIC share one listener through reads writes reconnects and r
if (old_runtime) |v| {
_ = setenv("XDG_RUNTIME_DIR", v, 1);
gpa.free(v);
- } else _ = unsetenv("XDG_RUNTIME_DIR");
+ } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR");
}
try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1));
const replacement = try gpa.alloc(u8, 3 * msize + 27);
@@ -1754,7 +1781,7 @@ test "a change waits while the editor is out mid-step, a read does not, and the
if (old_runtime) |v| {
_ = setenv("XDG_RUNTIME_DIR", v, 1);
gpa.free(v);
- } else _ = unsetenv("XDG_RUNTIME_DIR");
+ } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR");
}
try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1));
@@ -1829,7 +1856,7 @@ test "a held read is answered when the log has news, and a flushed one spends no
if (old_runtime) |v| {
_ = setenv("XDG_RUNTIME_DIR", v, 1);
gpa.free(v);
- } else _ = unsetenv("XDG_RUNTIME_DIR");
+ } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR");
}
try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1));
@@ -2001,28 +2028,22 @@ pub fn exportSessionEnv(serial: u32, adopts: bool) void {
exportEnv(if (own_socket_len > 0) own_socket[0..own_socket_len] else null, serial, adopts);
}
+/// Told to children through host_io.ChildEnv, never set into this process's
+/// own environment (see ChildEnv.exportVar for why).
fn exportEnv(socket: ?[]const u8, serial: u32, adopts: bool) void {
- var announced = false;
- if (adopts) announcing: {
- var buf: [16]u8 = undefined;
- const text = std.fmt.bufPrintSentinel(&buf, "{d}", .{@as(u32, @intCast(libc.getpid()))}, 0) catch
- break :announcing;
- announced = setenv("PARDES_PID", text, 1) == 0;
- }
- if (!announced) _ = unsetenv("PARDES_PID");
-
- if (socket) |own| exporting: {
- var sock: [sun_path_len]u8 = undefined;
- const path = std.fmt.bufPrintSentinel(&sock, "{s}", .{own}, 0) catch break :exporting;
+ const ChildEnv = @import("host_io.zig").ChildEnv;
+ var pid_buf: [16]u8 = undefined;
+ ChildEnv.exportVar("PARDES_PID", if (adopts) std.fmt.bufPrint(&pid_buf, "{d}", .{@as(u32, @intCast(libc.getpid()))}) catch null else null);
+ if (socket) |own| {
var buf: [16]u8 = undefined;
- const id = std.fmt.bufPrintSentinel(&buf, "{d}", .{serial}, 0) catch break :exporting;
- if (setenv("PARDES_9P", path, 1) != 0) break :exporting;
+ ChildEnv.exportVar("PARDES_9P", own);
exportMount(own);
- if (setenv("PARDES_PANE", id, 1) == 0) return;
+ ChildEnv.exportVar("PARDES_PANE", std.fmt.bufPrint(&buf, "{d}", .{serial}) catch unreachable);
+ return;
}
- _ = unsetenv("PARDES_9P");
- _ = unsetenv("PARDES_PANE");
- _ = unsetenv("PARDES_MOUNT");
+ ChildEnv.exportVar("PARDES_9P", null);
+ ChildEnv.exportVar("PARDES_PANE", null);
+ ChildEnv.exportVar("PARDES_MOUNT", null);
}
/// $PARDES_MOUNT, the session's own directory, when the environment says
@@ -2040,11 +2061,11 @@ fn exportMount(sock_path: []const u8) void {
const suffix = ".sock";
if (mntgen and root.len > 0 and std.mem.startsWith(u8, base, sock_prefix) and std.mem.endsWith(u8, base, suffix) and base.len > sock_prefix.len + suffix.len) {
var buf: [4096]u8 = undefined;
- if (std.fmt.bufPrintSentinel(&buf, "{s}/pardes/{s}", .{ std.mem.trimEnd(u8, root, "/"), base[sock_prefix.len .. base.len - suffix.len] }, 0)) |mount| {
- if (setenv("PARDES_MOUNT", mount, 1) == 0) return;
+ if (std.fmt.bufPrint(&buf, "{s}/pardes/{s}", .{ std.mem.trimEnd(u8, root, "/"), base[sock_prefix.len .. base.len - suffix.len] })) |mount| {
+ return @import("host_io.zig").ChildEnv.exportVar("PARDES_MOUNT", mount);
} else |_| {}
}
- _ = unsetenv("PARDES_MOUNT");
+ @import("host_io.zig").ChildEnv.exportVar("PARDES_MOUNT", null);
}
test "a pane's PARDES_MOUNT is the session's directory under 9ns --mntgen, and unset when not known" {
@@ -2057,16 +2078,16 @@ test "a pane's PARDES_MOUNT is the session's directory under 9ns --mntgen, and u
if (value) |old| {
_ = setenv(name, old, 1);
testing.allocator.free(old);
- } else _ = unsetenv(name);
+ } else _ = @import("host_io.zig").testUnsetenv(name);
};
_ = setenv("NINE_MOUNT", "/mnt/9p", 1);
_ = setenv("NINE_MNTGEN", "1", 1);
exportMount("/run/user/1000/pardes-9p-work.sock");
- try testing.expectEqualStrings("/mnt/9p/pardes/work", std.mem.span(libc.getenv("PARDES_MOUNT").?));
+ try testing.expectEqualStrings("/mnt/9p/pardes/work", @import("host_io.zig").ChildEnv.exported("PARDES_MOUNT").?);
// A mount of another server (9ns --unix): not this session's.
- _ = unsetenv("NINE_MNTGEN");
+ _ = @import("host_io.zig").testUnsetenv("NINE_MNTGEN");
exportMount("/run/user/1000/pardes-9p-work.sock");
- try testing.expect(libc.getenv("PARDES_MOUNT") == null);
+ try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_MOUNT") == null);
}
test "9P shell environment states being inside pardes apart from how to reach it" {
@@ -2079,16 +2100,16 @@ test "9P shell environment states being inside pardes apart from how to reach it
if (value) |old| {
_ = setenv(name, old, 1);
testing.allocator.free(old);
- } else _ = unsetenv(name);
+ } else _ = @import("host_io.zig").testUnsetenv(name);
};
var dir: [sun_path_len:0]u8 = undefined;
_ = setenv("XDG_RUNTIME_DIR", "/run/user/1000", 1);
try testing.expectEqualStrings("/run/user/1000", socketDir(&dir).?);
- _ = unsetenv("XDG_RUNTIME_DIR");
+ _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR");
_ = setenv("HOME", "/home/example", 1);
try testing.expectEqualStrings("/home/example/.local/state/pardes", socketDir(&dir).?);
- _ = unsetenv("HOME");
+ _ = @import("host_io.zig").testUnsetenv("HOME");
try testing.expect(socketDir(&dir) == null);
const listener = try testing.allocator.create(Listener);
@@ -2104,25 +2125,25 @@ test "9P shell environment states being inside pardes apart from how to reach it
// parent, so a pardes started in one of its shells runs a session of
// its own instead of handing its argument over.
exportPaneEnv(listener, 7, false);
- try testing.expectEqualStrings(path, std.mem.span(libc.getenv("PARDES_9P").?));
- try testing.expectEqualStrings("7", std.mem.span(libc.getenv("PARDES_PANE").?));
- try testing.expect(libc.getenv("PARDES_PID") == null);
+ try testing.expectEqualStrings(path, @import("host_io.zig").ChildEnv.exported("PARDES_9P").?);
+ try testing.expectEqualStrings("7", @import("host_io.zig").ChildEnv.exported("PARDES_PANE").?);
+ try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PID") == null);
exportPaneEnv(listener, 8, true);
- try testing.expectEqualStrings("8", std.mem.span(libc.getenv("PARDES_PANE").?));
- try testing.expectEqualStrings(own_pid, std.mem.span(libc.getenv("PARDES_PID").?));
+ try testing.expectEqualStrings("8", @import("host_io.zig").ChildEnv.exported("PARDES_PANE").?);
+ try testing.expectEqualStrings(own_pid, @import("host_io.zig").ChildEnv.exported("PARDES_PID").?);
// A session whose listener never came up is still the session this shell
// is inside: the pid stands on its own, and only the address is missing.
exportPaneEnv(null, 0, true);
- try testing.expect(libc.getenv("PARDES_9P") == null);
- try testing.expect(libc.getenv("PARDES_PANE") == null);
- try testing.expectEqualStrings(own_pid, std.mem.span(libc.getenv("PARDES_PID").?));
+ try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_9P") == null);
+ try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PANE") == null);
+ try testing.expectEqualStrings(own_pid, @import("host_io.zig").ChildEnv.exported("PARDES_PID").?);
exportPaneEnv(null, 0, false);
- try testing.expect(libc.getenv("PARDES_9P") == null);
- try testing.expect(libc.getenv("PARDES_PANE") == null);
- try testing.expect(libc.getenv("PARDES_PID") == null);
+ try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_9P") == null);
+ try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PANE") == null);
+ try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PID") == null);
}
pub const Client = struct {