summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/next-daemon.md19
-rw-r--r--src/9p_io.zig156
2 files changed, 149 insertions, 26 deletions
diff --git a/docs/next-daemon.md b/docs/next-daemon.md
index e63f69c6..159e66e7 100644
--- a/docs/next-daemon.md
+++ b/docs/next-daemon.md
@@ -360,16 +360,19 @@ Everything of ours lives in `/root/pardes-test`, nothing outside it.
- A kitty `t=f` naming a FIFO hung the open everywhere but Linux; a
stat by path now comes first.
- A Unix socket whose listen queue is full refuses a connect with
- ECONNREFUSED, exactly as a socket nobody listens on does. The
- registry sweep reads that refusal as a dead session, so it could
- remove a live session's socket while its queue overflows. **Open:**
- the unit test "the registry sweep takes the dead entries" fails on
- it.
+ ECONNREFUSED, exactly as a socket nobody listens on does, so a sweep
+ that judged by connecting could remove a live session's socket while
+ its queue overflowed. A session's liveness is now its lock: it holds
+ `<socket>.lock` flocked from before it binds until after it unlinks
+ (9p_io.zig `claim`), the sweeps and `taken` ask that lock, and they
+ unlink only under it. A connect judges only a socket with no lock, one
+ of a pardes from before the locks. The daemon's own start was already
+ decided by its flock.
- **The tests there.** The box runs them as root, with no zls, fish or
fonts installed.
- - main: 1138 passed, 36 skipped, 4 failed: the sweep above; the LSP
- format retry (no zls); and two tests that make a file or directory
- unreadable, which root reads anyway.
+ - main: 1139 passed, 36 skipped, 3 failed: the LSP format retry (no
+ zls), and two tests that make a file or directory unreadable, which
+ root reads anyway.
- panes: 224 passed, 1 failed (`Shell fish`: no fish). kitty: 43
passed. The small programs all pass but fonts (no fonts).
- Off Linux, the parts of tests whose refusing path is Linux's
diff --git a/src/9p_io.zig b/src/9p_io.zig
index 174d3f89..5f22b326 100644
--- a/src/9p_io.zig
+++ b/src/9p_io.zig
@@ -195,6 +195,9 @@ pub const Listener = struct {
paused_ms: i64 = 0,
path_buf: [sun_path_len]u8 = undefined,
path_len: usize = 0,
+ /// The socket's lock (`claim`), held for the listener's life; -1 when none.
+ lock: c_int = -1,
+ lock_buf: [lock_path_len:0]u8 = undefined,
/// The registry entry this editor posted
/// (`$XDG_RUNTIME_DIR/9p/pardes/<name>`), zero when it did not.
posted_buf: [sun_path_len]u8 = undefined,
@@ -939,6 +942,8 @@ pub const Listener = struct {
z[l.path_len] = 0;
_ = libc.unlink(z[0..l.path_len :0]);
}
+ // The lock after the socket: a socket there never lacks a held lock.
+ if (l.lock >= 0) release(l.lock, std.mem.sliceTo(&l.lock_buf, 0));
freeRunner(l.runner);
gpa.destroy(l);
}
@@ -961,18 +966,22 @@ fn freeRunner(runner: *Runner) void {
/// 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;
+var own_socket: [lock_path_len:0]u8 = undefined;
var own_socket_len: usize = 0;
-/// Removes the socket this process listens on. Async-signal-safe (one
-/// unlink): SIGTERM and SIGHUP call it before the signal's own death, so a
-/// killed editor leaves no socket behind.
+/// Removes the socket this process listens on, then its lock (the death
+/// lets go of it). Async-signal-safe (two unlinks): SIGTERM and SIGHUP call
+/// it before the signal's own death, so a killed editor leaves no socket
+/// behind.
pub fn unlinkOwnSocket() void {
if (comptime !supported) return;
const n = own_socket_len;
if (n == 0) return;
own_socket_len = 0;
_ = libc.unlink(own_socket[0..n :0]);
+ own_socket[n..][0..lock_suffix.len].* = lock_suffix.*;
+ own_socket[n + lock_suffix.len] = 0;
+ _ = libc.unlink(own_socket[0 .. n + lock_suffix.len :0]);
}
/// SIGTERM, SIGHUP and SIGINT with no handler of their own (a GUI's, a
@@ -1027,10 +1036,76 @@ pub fn sweepDeadSockets(io: std.Io, dir: [:0]const u8) void {
const facts = statNoFollow(path) orelse continue;
if (facts.mode & 0o170000 != 0o140000 or facts.uid != libc.getuid()) continue;
if (probe(path) == .live) continue;
- _ = libc.unlink(path);
+ removeDead(path);
}
}
+/// A socket's lock, `<socket>.lock` beside it: flocked by the socket's owner
+/// from before it binds to after it unlinks (`claim`, `release`), so held
+/// is alive and free is dead. A connect cannot say that: FreeBSD refuses one
+/// to a listener whose queue is full exactly as to a socket nobody listens
+/// on, and a starting owner's socket refuses until it listens.
+const lock_suffix = ".lock";
+const lock_path_len = sun_path_len + lock_suffix.len;
+
+fn lockPath(buf: *[lock_path_len:0]u8, sock: []const u8) ?[:0]const u8 {
+ return std.fmt.bufPrintSentinel(buf, "{s}" ++ lock_suffix, .{sock}, 0) catch null;
+}
+
+/// `sock`'s lock taken (its path in `buf`), made when it is missing: null
+/// while someone else holds it. A probe holds it shared for a moment, so a
+/// refusal is asked again for a while before it counts. One taken on a file
+/// its owner unlinked as it let go is taken again on the one at the path.
+fn claim(sock: []const u8, buf: *[lock_path_len:0]u8) ?c_int {
+ const path = lockPath(buf, sock) orelse return null;
+ var tries: u32 = 0;
+ while (tries < 50) : (tries += 1) {
+ const fd = libc.open(path, .{ .ACCMODE = .RDWR, .CREAT = true, .CLOEXEC = true }, @as(libc.mode_t, 0o600));
+ if (fd < 0) return null;
+ if (libc.flock(fd, std.posix.LOCK.EX | std.posix.LOCK.NB) != 0) {
+ _ = libc.close(fd);
+ Client.nap(1);
+ continue;
+ }
+ const io = std.Io.Threaded.global_single_threaded.io();
+ const held = (std.Io.File{ .handle = fd, .flags = .{ .nonblocking = false } }).stat(io) catch null;
+ const named = std.Io.Dir.cwd().statFile(io, path, .{ .follow_symlinks = false }) catch null;
+ if (held != null and named != null and held.?.inode == named.?.inode) return fd;
+ _ = libc.close(fd);
+ }
+ return null;
+}
+
+/// A claim let go, its lock file removed while still held: nobody else
+/// unlinks it, and a claim on the unlinked file is taken again (`claim`).
+fn release(fd: c_int, path: [:0]const u8) void {
+ _ = libc.unlink(path);
+ _ = libc.close(fd);
+}
+
+/// 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`.
+fn lockState(sock: []const u8) Probe {
+ var buf: [lock_path_len:0]u8 = undefined;
+ const path = lockPath(&buf, sock) orelse return .live;
+ const fd = libc.open(path, .{ .ACCMODE = .RDONLY, .CLOEXEC = true }, @as(libc.mode_t, 0));
+ if (fd < 0) return if (libc.errno(fd) == .NOENT) .none else .live;
+ defer _ = libc.close(fd);
+ return if (libc.flock(fd, std.posix.LOCK.SH | std.posix.LOCK.NB) == 0) .stale else .live;
+}
+
+/// 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.
+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);
+ release(fd, std.mem.sliceTo(&buf, 0));
+}
+
/// The names in `dir` that `keep` takes (all with none), into `names` as a length byte and
/// the name each, until it is full; the bytes staged. They are staged
/// before anything is unlinked, so a sweep never asks a directory to keep
@@ -1108,6 +1183,15 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [
.greet_timeout_ms = Listener.greet_deadline_ms,
});
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.
+ l.lock = claim(p, &l.lock_buf) orelse {
+ log.warn("something is already listening on {s}", .{p});
+ l.deinit(gpa);
+ return null;
+ };
// 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
@@ -1215,7 +1299,7 @@ pub fn taken(name: []const u8) bool {
const dir = socketDir(&dir_buf) orelse return false;
var buf: [sun_path_len]u8 = undefined;
const p = socketPath(&buf, dir, name) orelse return false;
- return statNoFollow(p) != null and alive(p);
+ return probe(p) == .live;
}
/// What is at a socket path, in cloud9's three answers (`post.Probe`).
@@ -1235,7 +1319,18 @@ const Probe = enum { none, stale, live };
/// `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.
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]);
@@ -1275,7 +1370,8 @@ fn probe(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 definite refusal counts as gone. The socket a stale entry points
+/// a lock let go (or, with none, a definite refusal) 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
@@ -1324,12 +1420,10 @@ fn sweepRegistry(io: std.Io, svc: [:0]const u8, sockets: []const u8) void {
if (!ourSocket(target, sockets)) continue;
const t = statNoFollow(target) orelse continue;
if (t.mode & 0o170000 != 0o140000 or t.uid != libc.getuid()) continue;
- // Ask again, immediately before deleting. The first probe was of the
- // ENTRY and is by now several syscalls old; a socket that is bound but
- // has not reached listen(2) yet answers ECONNREFUSED exactly like a
- // dead one, and that window is every server's startup.
- if (probe(target) != .stale) continue;
- _ = libc.unlink(target);
+ // Judged again, under its lock: the first probe was of the ENTRY and
+ // is by now several syscalls old, and a new owner may have claimed
+ // the name since.
+ removeDead(target);
}
if (reaped != 0) log.info("reaped {d} stale registry entries under {s}", .{ reaped, svc });
}
@@ -1428,21 +1522,42 @@ test "the registry sweep takes the dead entries and leaves everything else" {
const outsider_sock = try std.fmt.bufPrintSentinel(&paths[5], "{s}/sweep-outsider-{d}.sock", .{ base, pid }, 0);
var outsider_buf: [sun_path_len:0]u8 = undefined;
const outsider = try std.fmt.bufPrintSentinel(&outsider_buf, "{s}/outsider", .{svc}, 0);
+ // Live, its lock held, but bound and not listening: a connect is
+ // refused, on every system, as FreeBSD refuses one to a live session
+ // whose queue is full. Its lock is what says it lives.
+ var starting_paths: [2][sun_path_len:0]u8 = undefined;
+ const starting_sock = try std.fmt.bufPrintSentinel(&starting_paths[0], "{s}/pardes-9p-sweepstarting-{d}.sock", .{ base, pid }, 0);
+ const starting = try std.fmt.bufPrintSentinel(&starting_paths[1], "{s}/starting", .{svc}, 0);
+ var locks: [3][lock_path_len:0]u8 = undefined;
+ const dead_lock = lockPath(&locks[2], dead_sock).?;
defer {
- for ([_][:0]const u8{ live_sock, dead_sock, live, dead, stranger, outsider_sock, outsider }) |p| _ = libc.unlink(p);
+ for ([_][:0]const u8{ live_sock, dead_sock, live, dead, stranger, outsider_sock, outsider, starting_sock, starting, dead_lock }) |p| _ = libc.unlink(p);
_ = libc.rmdir(svc);
}
+ const live_lock = claim(live_sock, &locks[0]) orelse return error.LockFailed;
+ defer release(live_lock, std.mem.sliceTo(&locks[0], 0));
const listening = bindSocket(live_sock);
try testing.expect(listening >= 0);
defer _ = libc.close(listening);
try testing.expectEqual(@as(c_int, 0), libc.listen(listening, 1));
- // Bound and then dropped: the file stays, and nobody answers it —
- // exactly what an aborted test leaves behind.
+ const starting_lock = claim(starting_sock, &locks[1]) orelse return error.LockFailed;
+ defer release(starting_lock, std.mem.sliceTo(&locks[1], 0));
+ const bound = bindSocket(starting_sock);
+ try testing.expect(bound >= 0);
+ defer _ = libc.close(bound);
+ try testing.expectEqual(@as(c_int, 0), libc.symlink(starting_sock, starting));
+
+ // Bound and then dropped, its lock let go: the files stay, and nobody
+ // answers it -- exactly what a killed session leaves behind.
const abandoned = bindSocket(dead_sock);
try testing.expect(abandoned >= 0);
_ = libc.close(abandoned);
+ const dropped = libc.open(dead_lock, .{ .ACCMODE = .RDWR, .CREAT = true, .CLOEXEC = true }, @as(libc.mode_t, 0o600));
+ try testing.expect(dropped >= 0);
+ try testing.expectEqual(@as(c_int, 0), libc.flock(dropped, std.posix.LOCK.EX));
+ _ = libc.close(dropped);
try testing.expectEqual(@as(c_int, 0), libc.symlink(live_sock, live));
try testing.expectEqual(@as(c_int, 0), libc.symlink(dead_sock, dead));
@@ -1499,6 +1614,9 @@ test "the registry sweep takes the dead entries and leaves everything else" {
try testing.expect(statNoFollow(dead) == null);
try testing.expect(statNoFollow(dead_sock) == null);
+ try testing.expect(statNoFollow(dead_lock) == null);
+ try testing.expect(statNoFollow(starting) != null);
+ try testing.expect(statNoFollow(starting_sock) != null);
try testing.expect(statNoFollow(live) != null);
try testing.expect(statNoFollow(live_sock) != null);
try testing.expect(statNoFollow(stranger) != null);
@@ -1630,8 +1748,9 @@ test "a listening editor posts itself into the 9P registry and unposts on stop"
// adopting the registry moves nothing, it only advertises.
try testing.expect(statNoFollow(sock) != null);
// It appeared already listening, renamed into place from a name of
- // its own, which is gone.
+ // its own, which is gone; its lock held all the while.
try testing.expect(alive(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);
// And the registry holds a symlink to it one directory down, so
@@ -1643,8 +1762,9 @@ test "a listening editor posts itself into the 9P registry and unposts on stop"
try testing.expectEqualStrings(sock, link[0..@intCast(n)]);
}
// Stopping takes the name back out, so the next editor of that name
- // is not refused by its own corpse.
+ // is not refused by its own corpse; the lock goes with the socket.
try testing.expect(libc.readlink(entry, &link, link.len) < 0);
+ try testing.expectEqual(Probe.none, lockState(sock));
}
test "Unix TCP and QUIC share one listener through reads writes reconnects and reset" {