diff options
| -rw-r--r-- | src/9p_io.zig | 190 |
1 files changed, 43 insertions, 147 deletions
diff --git a/src/9p_io.zig b/src/9p_io.zig index c985845c..aadcd1f1 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -88,50 +88,6 @@ fn canonicalIp(address: std.Io.net.IpAddress) std.Io.net.IpAddress { return address; } -fn sockaddrIp(address: *const libc.sockaddr) ?std.Io.net.IpAddress { - return switch (address.family) { - libc.AF.INET => blk: { - const addr: *const libc.sockaddr.in = @ptrCast(@alignCast(address)); - break :blk .{ .ip4 = .{ .port = std.mem.bigToNative(u16, addr.port), .bytes = @bitCast(addr.addr) } }; - }, - libc.AF.INET6 => blk: { - const addr: *const libc.sockaddr.in6 = @ptrCast(@alignCast(address)); - break :blk canonicalIp(.{ .ip6 = .{ .port = std.mem.bigToNative(u16, addr.port), .bytes = addr.addr } }); - }, - else => null, - }; -} - -const IfAddr = extern struct { - next: ?*IfAddr, - name: ?[*:0]u8, - flags: c_uint, - address: ?*libc.sockaddr, - netmask: ?*libc.sockaddr, - destination: ?*libc.sockaddr, - data: ?*anyopaque, -}; - -extern "c" fn getifaddrs(out: *?*IfAddr) c_int; -extern "c" fn freeifaddrs(first: *IfAddr) void; - -fn localIp(address: std.Io.net.IpAddress) bool { - switch (address) { - .ip4 => |ip| if (ip.bytes[0] == 127 or std.mem.allEqual(u8, &ip.bytes, 0)) return true, - .ip6 => |ip| if (ip.isLoopBack() or std.mem.allEqual(u8, &ip.bytes, 0)) return true, - } - var first: ?*IfAddr = null; - if (getifaddrs(&first) != 0) return false; - defer if (first) |head| freeifaddrs(head); - var next = first; - while (next) |entry| : (next = entry.next) { - var local = sockaddrIp(entry.address orelse continue) orelse continue; - local.setPort(address.getPort()); - if (address.eql(&local)) return true; - } - return false; -} - /// The Unix and TCP listeners run on cloud9's `std.Io` runner: it accepts, /// reads frames, steps each connection's engine and writes replies on its /// own tasks, and every backend request is answered right there, on the @@ -144,7 +100,6 @@ const Runner = if (supported) cloud9.serve.Runner(pardes.ctlfs, ninep.editor, .{ .listeners = 2, }) else void; -const accept_pause_ms: i64 = 100; pub const Listener = struct { io: std.Io, @@ -176,7 +131,6 @@ pub const Listener = struct { /// Connections accepted before this count are being cut by a Restore. cut_before: u64 = 0, tcp_address: ?std.Io.net.IpAddress = null, - 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. @@ -1388,14 +1342,6 @@ fn bindSocket(path: [:0]const u8) c_int { return fd; } -test "one connection's buffers are sized from the one msize constant" { - try testing.expect(msize >= ninep.min_msize); - try testing.expectEqual(@as(u32, msize), Runner.msize); - try testing.expectEqual(@as(usize, msize), @typeInfo(@FieldType(Runner.Conn, "in")).array.len); - try testing.expectEqual(@as(usize, 2 * msize), @typeInfo(@FieldType(Runner.Conn, "out")).array.len); - try testing.expectEqual(@as(usize, max_conns), Runner.max_connections); -} - test "TCP addresses are numeric and normalize mapped IPv4" { const loopback = try networkAddress("tcp!127.0.0.1!5640", false); try testing.expectEqualDeep(loopback, try networkAddress("tcp!::ffff:127.0.0.1!5640", false)); @@ -1419,30 +1365,40 @@ test "TCP addresses are numeric and normalize mapped IPv4" { } extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8; -extern "c" fn rmdir(path: [*:0]const u8) c_int; + +/// A test's own runtime directory under /tmp, which XDG_RUNTIME_DIR names +/// until `deinit` puts the old value back and removes it, with what the +/// listener left in it. +const TempRuntime = struct { + directory: [64:0]u8 = undefined, + old: ?[:0]u8 = null, + + fn init(t: *TempRuntime, comptime name: []const u8) !void { + _ = try std.fmt.bufPrintSentinel(&t.directory, "/tmp/pardes-" ++ name ++ "-XXXXXX", .{}, 0); + if (mkdtemp(&t.directory) == null) return error.TempDirectoryFailed; + if (libc.getenv("XDG_RUNTIME_DIR")) |v| t.old = try testing.allocator.dupeSentinel(u8, std.mem.span(v), 0); + try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &t.directory, 1)); + } + + fn deinit(t: *TempRuntime) void { + if (t.old) |v| { + _ = setenv("XDG_RUNTIME_DIR", v, 1); + testing.allocator.free(v); + } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); + std.Io.Dir.cwd().deleteTree(testing.io, std.mem.sliceTo(&t.directory, 0)) catch |err| std.debug.print("deleteTree: {s}\n", .{@errorName(err)}); + } +}; test "a socket path too long for sun_path is no listener, and leaves the editor's turn as it was" { if (comptime !supported) return error.SkipZigTest; const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true }); defer p.deinit(); - var directory: [64:0]u8 = undefined; - _ = try std.fmt.bufPrintSentinel(&directory, "/tmp/pardes-long-XXXXXX", .{}, 0); - if (mkdtemp(&directory) == null) return error.TempDirectoryFailed; - const dir = std.mem.span(@as([*:0]const u8, &directory)); - defer _ = rmdir(&directory); + var rt: TempRuntime = .{}; + try rt.init("long"); + defer rt.deinit(); // A name that fits: the directory is one a listener takes. var fits: [sun_path_len]u8 = undefined; - try testing.expect(socketPath(&fits, dir, "short") != null); - const old = libc.getenv("XDG_RUNTIME_DIR"); - var old_buf: [4096]u8 = undefined; - const old_copy = if (old) |o| try std.fmt.bufPrintSentinel(&old_buf, "{s}", .{std.mem.span(o)}, 0) else null; - var z: [4096]u8 = undefined; - _ = setenv("XDG_RUNTIME_DIR", try std.fmt.bufPrintSentinel(&z, "{s}", .{dir}, 0), 1); - defer if (old_copy) |o| { - _ = setenv("XDG_RUNTIME_DIR", o, 1); - } else { - _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); - }; + 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.expect(pardes.turn.answer_held == null); @@ -1453,35 +1409,15 @@ test "a socket path too long for sun_path is no listener, and leaves the editor' test "a listening editor posts itself into the 9P registry and unposts on stop" { if (comptime !supported) return error.SkipZigTest; const gpa = testing.allocator; - var directory: [64:0]u8 = undefined; - _ = try std.fmt.bufPrintSentinel(&directory, "/tmp/pardes-post-XXXXXX", .{}, 0); - if (mkdtemp(&directory) == null) return error.TempDirectoryFailed; - const dir = std.mem.span(@as([*:0]const u8, &directory)); - const old_runtime = if (libc.getenv("XDG_RUNTIME_DIR")) |v| try gpa.dupeSentinel(u8, std.mem.span(v), 0) else null; - defer { - if (old_runtime) |v| { - _ = setenv("XDG_RUNTIME_DIR", v, 1); - gpa.free(v); - } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); - } - try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1)); + var rt: TempRuntime = .{}; + try rt.init("post"); + defer rt.deinit(); + const dir = std.mem.sliceTo(&rt.directory, 0); var entry_buf: [sun_path_len:0]u8 = undefined; const entry = try std.fmt.bufPrintSentinel(&entry_buf, "{s}/9p/pardes/unit", .{dir}, 0); - var svc_buf: [sun_path_len:0]u8 = undefined; - const svc = try std.fmt.bufPrintSentinel(&svc_buf, "{s}/9p/pardes", .{dir}, 0); var sock_buf: [sun_path_len:0]u8 = undefined; const sock = try std.fmt.bufPrintSentinel(&sock_buf, "{s}/" ++ prefix ++ "unit.sock", .{dir}, 0); - defer { - _ = libc.unlink(entry); - _ = rmdir(svc); - var reg_buf: [sun_path_len:0]u8 = undefined; - if (std.fmt.bufPrintSentinel(®_buf, "{s}/9p", .{dir}, 0)) |reg| { - _ = rmdir(reg); - } else |_| {} - _ = libc.unlink(sock); - _ = rmdir(&directory); - } var link: [sun_path_len]u8 = undefined; { @@ -1524,19 +1460,9 @@ test "a listening editor posts itself into the 9P registry and unposts on stop" test "Unix and TCP share one listener through reads writes reconnects and reset" { if (comptime !supported) return error.SkipZigTest; const gpa = testing.allocator; - var directory: [64:0]u8 = undefined; - _ = try std.fmt.bufPrintSentinel(&directory, "/tmp/pardes-tcp-XXXXXX", .{}, 0); - if (mkdtemp(&directory) == null) return error.TempDirectoryFailed; - // What the listener posted and left inside goes with it. - defer std.Io.Dir.cwd().deleteTree(testing.io, std.mem.sliceTo(&directory, 0)) catch |err| std.debug.print("deleteTree: {s}\n", .{@errorName(err)}); - const old_runtime = if (libc.getenv("XDG_RUNTIME_DIR")) |v| try gpa.dupeSentinel(u8, std.mem.span(v), 0) else null; - defer { - if (old_runtime) |v| { - _ = setenv("XDG_RUNTIME_DIR", v, 1); - gpa.free(v); - } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); - } - try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1)); + var rt: TempRuntime = .{}; + try rt.init("tcp"); + defer rt.deinit(); const replacement = try gpa.alloc(u8, 3 * msize + 27); defer gpa.free(replacement); @memset(replacement, 'x'); @@ -1633,19 +1559,9 @@ test "Unix and TCP share one listener through reads writes reconnects and reset" test "a change waits while the editor is out mid-step, a read does not, and the change lands when the editor rests" { if (comptime !supported) return error.SkipZigTest; const gpa = testing.allocator; - var directory: [64:0]u8 = undefined; - _ = try std.fmt.bufPrintSentinel(&directory, "/tmp/pardes-park-XXXXXX", .{}, 0); - if (mkdtemp(&directory) == null) return error.TempDirectoryFailed; - // What the listener posted and left inside goes with it. - defer std.Io.Dir.cwd().deleteTree(testing.io, std.mem.sliceTo(&directory, 0)) catch |err| std.debug.print("deleteTree: {s}\n", .{@errorName(err)}); - const old_runtime = if (libc.getenv("XDG_RUNTIME_DIR")) |v| try gpa.dupeSentinel(u8, std.mem.span(v), 0) else null; - defer { - if (old_runtime) |v| { - _ = setenv("XDG_RUNTIME_DIR", v, 1); - gpa.free(v); - } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); - } - try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1)); + var rt: TempRuntime = .{}; + try rt.init("park"); + defer rt.deinit(); const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); defer p.deinit(); @@ -1711,18 +1627,9 @@ test "a change waits while the editor is out mid-step, a read does not, and the test "a mount's connection is kept between operations, and one the peer hung up is dialled again" { if (comptime !supported) return error.SkipZigTest; const gpa = testing.allocator; - var directory: [64:0]u8 = undefined; - _ = try std.fmt.bufPrintSentinel(&directory, "/tmp/pardes-kept-XXXXXX", .{}, 0); - if (mkdtemp(&directory) == null) return error.TempDirectoryFailed; - defer std.Io.Dir.cwd().deleteTree(testing.io, std.mem.sliceTo(&directory, 0)) catch |err| std.debug.print("deleteTree: {s}\n", .{@errorName(err)}); - const old_runtime = if (libc.getenv("XDG_RUNTIME_DIR")) |v| try gpa.dupeSentinel(u8, std.mem.span(v), 0) else null; - defer { - if (old_runtime) |v| { - _ = setenv("XDG_RUNTIME_DIR", v, 1); - gpa.free(v); - } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); - } - try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1)); + var rt: TempRuntime = .{}; + try rt.init("kept"); + defer rt.deinit(); const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); defer p.deinit(); @@ -1797,19 +1704,9 @@ test "a connection holds up to 128 reads, and the next is refused in words" { test "a held read is answered when the log has news, and a flushed one spends nothing" { if (comptime !supported) return error.SkipZigTest; const gpa = testing.allocator; - var directory: [64:0]u8 = undefined; - _ = try std.fmt.bufPrintSentinel(&directory, "/tmp/pardes-held-XXXXXX", .{}, 0); - if (mkdtemp(&directory) == null) return error.TempDirectoryFailed; - // What the listener posted and left inside goes with it. - defer std.Io.Dir.cwd().deleteTree(testing.io, std.mem.sliceTo(&directory, 0)) catch |err| std.debug.print("deleteTree: {s}\n", .{@errorName(err)}); - const old_runtime = if (libc.getenv("XDG_RUNTIME_DIR")) |v| try gpa.dupeSentinel(u8, std.mem.span(v), 0) else null; - defer { - if (old_runtime) |v| { - _ = setenv("XDG_RUNTIME_DIR", v, 1); - gpa.free(v); - } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); - } - try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1)); + var rt: TempRuntime = .{}; + try rt.init("held"); + defer rt.deinit(); const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); defer p.deinit(); @@ -1946,7 +1843,6 @@ test "a held read is answered when the log has news, and a flushed one spends no } extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; -extern "c" fn unsetenv(name: [*:0]const u8) c_int; pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes) ?*Listener { var name: [16]u8 = undefined; |
