diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/9p_io.zig | 133 |
1 files changed, 17 insertions, 116 deletions
diff --git a/src/9p_io.zig b/src/9p_io.zig index abfc20ae..62b6c37e 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -814,8 +814,7 @@ fn release(fd: c_int, path: [:0]const u8) void { } /// What `sock`'s lock says of its owner: held is `.live`, free `.stale`, and -/// none at all `.none`, a socket of a pardes from before the locks, which a -/// connect judges. Anything unclear is `.live`. +/// none at all `.none`. Anything unclear is `.live`. fn lockState(sock: []const u8) Probe { var buf: [lock_path_len:0]u8 = undefined; const path = lockPath(&buf, sock) orelse return .live; @@ -826,13 +825,12 @@ fn lockState(sock: []const u8) Probe { } /// A dead owner's socket removed, and its lock, under that lock: no new -/// owner can come between the judging and the unlink. Under the lock, an -/// owner that took it is gone, and a socket of a pardes from before the -/// locks still has its connect to answer. +/// owner can come between the judging and the unlink, and one that took +/// it is gone. fn removeDead(sock: [:0]const u8) void { var buf: [lock_path_len:0]u8 = undefined; const fd = claim(sock, &buf) orelse return; - if (connectProbe(sock) == .stale) _ = libc.unlink(sock); + _ = libc.unlink(sock); release(fd, std.mem.sliceTo(&buf, 0)); } @@ -919,8 +917,7 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [ 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 - // there is a dead owner's, or one from a pardes before the locks, which - // the connects below still ask. + // there is a dead owner's. l.lock = claim(p, &l.lock_buf) orelse { log.warn("something is already listening on {s}", .{p}); return error.InUse; @@ -932,7 +929,7 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [ // 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))) { + if (existing != null and existing.?.kind != .unix_domain_socket) { log.warn("something is already listening on {s}", .{p}); return error.InUse; } @@ -968,8 +965,6 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [ return l; } -const alive = transport.isListening; - /// Whether a live session listens under `name` already: a second /// `--detach=<name>` is refused before anything is made. pub fn taken(name: []const u8) bool { @@ -981,63 +976,23 @@ pub fn taken(name: []const u8) bool { return probe(p) == .live; } -/// What is at a socket path, in cloud9's three answers (`post.Probe`). -/// Asked here with libc rather than through `cloud9.post.probe`, whose -/// raw Linux syscalls darwin cannot compile, but the classification is -/// that file's and must not become a second opinion: the one definite -/// refusal is `.stale`, nothing at the path at all is `.none`, and -/// everything else — connected, busy, refused permission, a surprise — -/// is `.live`. Uncertainty belongs to the server that owns the socket, -/// never to a sweeper deciding what to delete. +/// What a socket's lock says of its owner (`lockState`): `.live`, `.stale`, +/// or `.none`, no lock at all. Uncertainty belongs to the server that owns +/// the socket, never to a sweeper deciding what to delete: anything unclear +/// is `.live`. const Probe = enum { none, stale, live }; -/// `SOCK.STREAM`, and the kernel's own non-blocking bit where there is -/// one. Darwin's `SOCK.NONBLOCK` is a Zig shim for `std.posix.socket` -/// to unpack, not an ABI value, so handing it to the raw libc call -/// would ask a kernel that has never heard of it; there it is an -/// `fcntl` instead. -const probe_socket_kind: c_uint = libc.SOCK.STREAM | (if (darwin) 0 else libc.SOCK.NONBLOCK); - /// Whether whoever made the socket at `path` (or the one a registry entry -/// there names) is alive: its lock decides (`lockState`), and a connect -/// only for a socket that has none. +/// there names) is alive: its lock decides. With none, no owner holds one, +/// since each claims it before it binds and lets it go after it unlinks: +/// a socket there is a dead one's (a pardes from before the locks, +/// e9328a417, is one to restart). fn probe(path: [:0]const u8) Probe { var link_buf: [sun_path_len]u8 = undefined; const n = libc.readlink(path, &link_buf, link_buf.len); const sock = if (n > 0 and n < link_buf.len and link_buf[0] == '/') link_buf[0..@intCast(n)] else path; const locked = lockState(sock); - return if (locked == .none) connectProbe(path) else locked; -} - -fn connectProbe(path: [:0]const u8) Probe { - if (path.len + 1 > sun_path_len) return .live; // cannot ask; assume occupied - var addr: libc.sockaddr.un = .{ .path = @splat(0) }; - @memcpy(addr.path[0 .. path.len + 1], path[0 .. path.len + 1]); - const fd = libc.socket(libc.AF.UNIX, probe_socket_kind, 0); - if (fd < 0) return .live; - defer _ = libc.close(fd); - - // Non-blocking is the whole safety of this function, so it is read - // back rather than assumed: a BLOCKING connect to a live server - // whose backlog is full parks in the kernel with no timeout to end - // it — measured, it simply never returns — and a sweep that parks - // takes the editor's startup with it. An fd that cannot be proven - // non-blocking is never connected at all, which lands on `.live`, - // the answer that deletes nothing. - if (comptime darwin) _ = setNonblock(fd); - if (!isNonblocking(fd)) return .live; - - if (libc.connect(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr))) == 0) return .live; - // cloud9's `post.probe` mapping, answer for answer. The connection - // is never wanted: EAGAIN (a full Unix backlog) and EINPROGRESS say - // somebody is listening, which is all that was asked, and the fd - // closes without ever waiting for it to become writable. - return switch (libc.errno(-1)) { - .AGAIN, .INPROGRESS, .PERM, .ACCES => .live, - .CONNREFUSED => .stale, - .NOENT, .NOTDIR => .none, - else => .live, - }; + return if (locked == .none) .stale else locked; } /// Reaps the registry entries whose editor is gone, once per post. @@ -1049,8 +1004,7 @@ fn connectProbe(path: [:0]const u8) Probe { /// the somebody. It is only ever a symlink that is followed or /// unlinked, because that is the only thing `postToRegistry` makes and /// anything else under a name belongs to whoever put it there; and only -/// a lock let go (or, with none, a definite refusal) counts as gone -/// (`probe`). The socket a stale entry points +/// a lock let go, or none at all, counts as gone (`probe`). The socket a stale entry points /// at goes too, but not before a stat agrees it is a socket of ours: a /// plain file answers a connect with the same refusal. /// Is this the kind of path this editor is allowed to delete? A registry @@ -1107,23 +1061,6 @@ fn sweepRegistry(io: std.Io, svc: [:0]const u8, sockets: []const u8) void { if (reaped != 0) log.info("reaped {d} stale registry entries under {s}", .{ reaped, svc }); } -/// Whether it worked, because `probe` is not allowed to find out the -/// hard way: a caller that needs the guarantee has to be able to check. -fn setNonblock(fd: c_int) bool { - const flags = libc.fcntl(fd, libc.F.GETFL, @as(c_int, 0)); - if (flags < 0) return false; - var o: libc.O = @bitCast(@as(u32, @bitCast(flags))); - o.NONBLOCK = true; - return libc.fcntl(fd, libc.F.SETFL, @as(c_int, @bitCast(@as(u32, @bitCast(o))))) >= 0; -} - -fn isNonblocking(fd: c_int) bool { - const flags = libc.fcntl(fd, libc.F.GETFL, @as(c_int, 0)); - if (flags < 0) return false; - const o: libc.O = @bitCast(@as(u32, @bitCast(flags))); - return o.NONBLOCK; -} - const testing = std.testing; test "the socket name is a third prefix in the shared directory" { @@ -1250,47 +1187,11 @@ test "the registry sweep takes the dead entries and leaves everything else" { // connecting to it is refused exactly like the dead socket. try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = stranger, .data = "" }); - // The guarantee itself, asserted rather than inferred from the fact - // that the test finished: if a probe socket ever stops being - // non-blocking the connect below parks instead of failing, and a - // parked test costs whoever is building far more than a red one. - const checking = libc.socket(libc.AF.UNIX, probe_socket_kind, 0); - try testing.expect(checking >= 0); - if (comptime darwin) try testing.expect(setNonblock(checking)); - try testing.expect(isNonblocking(checking)); - _ = libc.close(checking); - - // The listener's backlog is filled before anything is asked, because - // a full backlog is the one place a blocking connect parks forever - // (`unix_wait_for_peer`, no timeout) and a sweep that only answers - // while nobody is queued is the sweep that hangs a build. Nothing - // below accepts any of these, so the queue stays full throughout. - var queued: [8]c_int = @splat(-1); - defer for (queued) |fd| { - if (fd >= 0) _ = libc.close(fd); - }; - for (&queued) |*slot| { - const fd = libc.socket(libc.AF.UNIX, probe_socket_kind, 0); - if (fd < 0) break; - if (comptime darwin) _ = setNonblock(fd); - var addr: libc.sockaddr.un = .{ .path = @splat(0) }; - @memcpy(addr.path[0 .. live_sock.len + 1], live_sock[0 .. live_sock.len + 1]); - _ = libc.connect(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr))); - slot.* = fd; - } - - const started = transport.nowMs(); try testing.expectEqual(Probe.live, probe(live)); try testing.expectEqual(Probe.stale, probe(dead)); sweepRegistry(testing.io, svc, base); - // Promptly, and not "eventually": a blocking probe never comes back - // at all, so any wall-clock bound at all is the assertion that - // matters. A second is several thousand times what three connects - // and a readdir cost. - try testing.expect(transport.nowMs() - started < 1000); - try testing.expect(statNoFollow(dead) == null); try testing.expect(statNoFollow(dead_sock) == null); try testing.expect(statNoFollow(dead_lock) == null); @@ -1404,7 +1305,7 @@ test "a listening editor posts itself into the 9P registry and unposts on stop" try testing.expect(statNoFollow(sock) != null); // It appeared already listening, renamed into place from a name of // its own, which is gone; its lock held all the while. - try testing.expect(alive(sock)); + try testing.expect(transport.isListening(sock)); try testing.expectEqual(Probe.live, lockState(sock)); var tmp_buf: [sun_path_len:0]u8 = undefined; try testing.expect(statNoFollow(try std.fmt.bufPrintSentinel(&tmp_buf, "{s}.{d}", .{ sock, libc.getpid() }, 0)) == null); |
