summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-04 23:23:11 -0300
committerGabriel Schneider <[email protected]>2026-11-05 00:58:21 -0300
commit7b14309694138c7813cfd657184fb2b91699fb12 (patch)
treee760622f0ecbda0452e0003b8d5e0c3c5261c2b7
parentbbe3f72f216d5cc5aff8e06c168efd6cf49b24a6 (diff)
downloadpardes-7b14309694138c7813cfd657184fb2b91699fb12.tar.gz
pardes-7b14309694138c7813cfd657184fb2b91699fb12.zip
9p_io listen: an error union with one errdefer, where eleven exits each called l.deinit and returned null (start still reports ListenFailed); and a name whose temporary bind name, <socket>.<pid>, does not fit sun_path is refused up front, as one too long already was, instead of bound in place by an untested path that reopened the race the rename closes
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
-rw-r--r--src/9p_io.zig102
1 files changed, 38 insertions, 64 deletions
diff --git a/src/9p_io.zig b/src/9p_io.zig
index aadcd1f1..abfc20ae 100644
--- a/src/9p_io.zig
+++ b/src/9p_io.zig
@@ -876,31 +876,34 @@ pub fn socketPath(buf: *[sun_path_len]u8, dir: []const u8, name: []const u8) ?[:
/// Serves `core`. From here on the calling thread has the editor's turn
/// (`pardes.turn`) and must give it up while it waits.
-pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: []const u8, fallback: []const u8, tcp_dial: ?[]const u8) ?*Listener {
- if (comptime !supported) return null;
+pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: []const u8, fallback: []const u8, tcp_dial: ?[]const u8) !*Listener {
+ if (comptime !supported) return error.Unsupported;
var dir_buf: [sun_path_len:0]u8 = undefined;
const dir = socketDir(&dir_buf) orelse {
log.warn("no runtime directory for the socket", .{});
- return null;
+ return error.NoRuntimeDir;
};
- if (!ensureSocketDir(dir)) return null;
+ if (!ensureSocketDir(dir)) return error.NoRuntimeDir;
// What dead editors left behind goes first, never a live one's.
sweepDeadSockets(io, dir);
const entry_name = if (named.len != 0) named else fallback;
// Checked before anything is made: the turn's hooks point at the
// listener from here on, and a path too long for sun_path used to free
// it with them still set, so the editor's next rest called into freed
- // memory.
+ // memory. The name the socket is first bound at, `<path>.<pid>` (it is
+ // renamed into place once it listens), must fit too.
var path_check: [sun_path_len]u8 = undefined;
- if (socketPath(&path_check, dir, entry_name) == null) {
+ var tmp_buf: [sun_path_len]u8 = undefined;
+ const check = socketPath(&path_check, dir, entry_name);
+ if (check == null or (std.fmt.bufPrintSentinel(&tmp_buf, "{s}.{d}", .{ check.?, libc.getpid() }, 0) catch null) == null) {
log.warn("no socket for {s}: the path is too long for a unix socket", .{entry_name});
- return null;
+ return error.NameTooLong;
}
- const l = gpa.create(Listener) catch return null;
+ const l = try gpa.create(Listener);
l.* = .{ .io = io, .core = core };
l.runner = allocRunner() orelse {
gpa.destroy(l);
- return null;
+ return error.OutOfMemory;
};
pardes.turn.start(io);
pardes.turn.wake_parked = Listener.wakeParked;
@@ -912,6 +915,7 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [
.handler = .{ .ctx = l, .serve = Listener.onServe, .opened = Listener.onOpened, .refused = Listener.onRefused },
.greet_timeout_ms = Listener.greet_deadline_ms,
});
+ errdefer l.deinit(gpa);
const p = socketPath(&l.path_buf, dir, entry_name).?;
// The name's lock first, held from here to the end: whoever else holds
// it is alive, however its socket answers a connect. Held, a socket
@@ -919,71 +923,41 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [
// the connects below still ask.
l.lock = claim(p, &l.lock_buf) orelse {
log.warn("something is already listening on {s}", .{p});
- l.deinit(gpa);
- return null;
+ return error.InUse;
};
// Listened on under a name of its own, then renamed into place: a client
// that waits for the socket to appear finds it listening already. Bound
// at its own name, it was there a moment before listen(2), and a connect
- // then was refused. (No room for the longer name: bound in place.)
- var tmp_buf: [sun_path_len]u8 = undefined;
- if (std.fmt.bufPrintSentinel(&tmp_buf, "{s}.{d}", .{ p, libc.getpid() }, 0) catch null) |tmp| {
- const existing = @import("fs.zig").statPath(io, p, .{ .follow_symlinks = false }) catch null;
- if (existing != null and (existing.?.kind != .unix_domain_socket or alive(p))) {
- log.warn("something is already listening on {s}", .{p});
- l.deinit(gpa);
- return null;
- }
+ // then was refused.
+ // unreachable: checked above, before anything was made
+ const tmp = std.fmt.bufPrintSentinel(&tmp_buf, "{s}.{d}", .{ p, libc.getpid() }, 0) catch unreachable;
+ const existing = @import("fs.zig").statPath(io, p, .{ .follow_symlinks = false }) catch null;
+ if (existing != null and (existing.?.kind != .unix_domain_socket or alive(p))) {
+ log.warn("something is already listening on {s}", .{p});
+ return error.InUse;
+ }
+ _ = libc.unlink(tmp);
+ _ = try l.runner.listen(.{ .unix = tmp }, max_conns);
+ if (libc.chmod(tmp, 0o600) != 0 or libc.rename(tmp, p) != 0) {
_ = libc.unlink(tmp);
- _ = l.runner.listen(.{ .unix = tmp }, max_conns) catch {
- l.deinit(gpa);
- return null;
- };
- if (libc.chmod(tmp, 0o600) != 0 or libc.rename(tmp, p) != 0) {
- _ = libc.unlink(tmp);
- l.deinit(gpa);
- return null;
- }
- } else _ = l.runner.listen(.{ .unix = p }, max_conns) catch |err| retry: {
- const existing = @import("fs.zig").statPath(io, p, .{ .follow_symlinks = false }) catch null;
- if (err != error.AddressInUse or existing == null or existing.?.kind != .unix_domain_socket or alive(p)) {
- log.warn("something is already listening on {s}", .{p});
- l.deinit(gpa);
- return null;
- }
- if (libc.unlink(p) != 0) {
- l.deinit(gpa);
- return null;
- }
- break :retry l.runner.listen(.{ .unix = p }, max_conns) catch {
- l.deinit(gpa);
- return null;
- };
- };
- l.path_len = p.len;
- if (libc.chmod(p, 0o600) != 0) {
- l.deinit(gpa);
- return null;
+ return error.ListenFailed;
}
+ l.path_len = p.len;
+ if (libc.chmod(p, 0o600) != 0) return error.ListenFailed;
@memcpy(own_socket[0..p.len], p);
own_socket[p.len] = 0;
own_socket_len = p.len;
armSocketCleanup();
l.postToRegistry(entry_name);
if (tcp_dial) |dial| {
- if (!std.mem.startsWith(u8, dial, "tcp!")) {
- l.deinit(gpa);
- return null;
- }
+ if (!std.mem.startsWith(u8, dial, "tcp!")) return error.BadDial;
const address = networkAddress(dial, true) catch {
log.warn("invalid TCP address {s}", .{dial});
- l.deinit(gpa);
- return null;
+ return error.BadDial;
};
const bound = l.runner.listen(.{ .tcp = address }, max_conns) catch |err| {
log.warn("cannot listen on {s}: {s}", .{ dial, @errorName(err) });
- l.deinit(gpa);
- return null;
+ return err;
};
l.tcp_address = canonicalIp(bound);
log.info("serving 9P2000 over TCP on {f}", .{l.tcp_address.?});
@@ -1400,7 +1374,7 @@ test "a socket path too long for sun_path is no listener, and leaves the editor'
var fits: [sun_path_len]u8 = undefined;
try testing.expect(socketPath(&fits, std.mem.sliceTo(&rt.directory, 0), "short") != null);
const long = &@as([120]u8, @splat('n'));
- try testing.expect(listen(testing.io, testing.allocator, p, long, "", null) == null);
+ try testing.expectError(error.NameTooLong, listen(testing.io, testing.allocator, p, long, "", null));
try testing.expect(pardes.turn.answer_held == null);
try testing.expect(pardes.turn.wake_parked == null);
try testing.expect(pardes.turn.io == null);
@@ -1423,7 +1397,7 @@ test "a listening editor posts itself into the 9P registry and unposts on stop"
{
const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 });
defer p.deinit();
- const l = listen(testing.io, gpa, p, "unit", "", null) orelse return error.ListenFailed;
+ const l = try listen(testing.io, gpa, p, "unit", "", null);
defer l.deinit(gpa);
// The socket stays exactly where pardes has always bound it:
// adopting the registry moves nothing, it only advertises.
@@ -1515,7 +1489,7 @@ test "Unix and TCP share one listener through reads writes reconnects and reset"
while (p.nextEffect()) |_| {}
var bind_buf: [64]u8 = undefined;
const bind = try std.fmt.bufPrint(&bind_buf, "tcp!{s}!0", .{host});
- const l = listen(testing.io, gpa, p, "roundtrip", "", bind) orelse return error.ListenFailed;
+ const l = try listen(testing.io, gpa, p, "roundtrip", "", bind);
defer {
l.reset(p);
l.deinit(gpa);
@@ -1568,7 +1542,7 @@ test "a change waits while the editor is out mid-step, a read does not, and the
const pane = try p.setTestFile("before\n");
while (p.nextEffect()) |_| {}
// This thread is the editor's: it has the turn from here on.
- const l = listen(testing.io, gpa, p, "park", "", null) orelse return error.ListenFailed;
+ const l = try listen(testing.io, gpa, p, "park", "", null);
defer {
l.reset(p);
l.deinit(gpa);
@@ -1635,7 +1609,7 @@ test "a mount's connection is kept between operations, and one the peer hung up
defer p.deinit();
const pane = try p.setTestFile("before\n");
while (p.nextEffect()) |_| {}
- const l = listen(testing.io, gpa, p, "kept", "", null) orelse return error.ListenFailed;
+ const l = try listen(testing.io, gpa, p, "kept", "", null);
defer {
l.reset(p);
l.deinit(gpa);
@@ -1713,7 +1687,7 @@ test "a held read is answered when the log has news, and a flushed one spends no
_ = try p.setTestFile("held\n");
p.update(.tick); // the pane is logged, before the log is opened
while (p.nextEffect()) |_| {}
- const l = listen(testing.io, gpa, p, "held", "", null) orelse return error.ListenFailed;
+ const l = try listen(testing.io, gpa, p, "held", "", null);
defer {
l.reset(p);
l.deinit(gpa);
@@ -1848,7 +1822,7 @@ pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes) ?*Listene
var name: [16]u8 = undefined;
// unreachable: a u32 is at most 10 digits
const fallback = std.fmt.bufPrint(&name, "{d}", .{@as(u32, @intCast(libc.getpid()))}) catch unreachable;
- return listen(io, gpa, core, core.opts.ninep_name, fallback, core.opts.ninep_tcp) orelse {
+ return listen(io, gpa, core, core.opts.ninep_name, fallback, core.opts.ninep_tcp) catch {
core.reportError(0, "9p listener", error.ListenFailed);
return null;
};