diff options
Diffstat (limited to 'web/catalog.zig')
| -rw-r--r-- | web/catalog.zig | 299 |
1 files changed, 283 insertions, 16 deletions
diff --git a/web/catalog.zig b/web/catalog.zig index 53bab19..87349bb 100644 --- a/web/catalog.zig +++ b/web/catalog.zig @@ -8,6 +8,7 @@ const fs = c9.fs; const Backend = struct { pub const Req = fs.Req; pub const Reply = fs.Reply; + pub const features: fs.Features = .{ .create = true, .remove = true, .wstat = true }; }; pub const Runner = c9.serve.Runner(Backend, .{ .fid_capacity = 512, .name_capacity = 255 }, .{ .msize = 65536, .connections = 16 }); @@ -22,7 +23,7 @@ pub const Site = struct { // Poll only these source paths for a managed rebuild watcher. inputs: []const []const u8 = &.{}, }; -const Settings = struct { sites: []const Site }; +const Settings = struct { sites: []const Site, filesystem: ?[]const u8 = null }; const State = struct { phase: []const u8 = "idle", watching: bool = false, @@ -55,12 +56,24 @@ pub const Catalog = struct { group: Io.Group = .init, runner: *Runner, http_port: u16 = 0, + host_filesystem: bool = false, + files: [16][512]?Io.File = @splat(@splat(null)), + reads: [16][32]u64 = @splat(@splat(0)), + read_handles: [16][32]u32 = @splat(@splat(0)), + read_groups: [16][32]Io.Group = @splat(@splat(.init)), cursors: [16][64]EventCursor = @splat(@splat(.{})), pub fn init(io: Io, config: []const u8, state_dir: []const u8) !*Catalog { const bytes = try Io.Dir.cwd().readFileAlloc(io, config, A, .limited(1024 * 1024)); defer A.free(bytes); const parsed = try std.json.parseFromSlice(Settings, A, bytes, .{ .allocate = .alloc_always }); + if (parsed.value.filesystem) |root| { + // This is intentionally a whole-host export, not a sandbox/chroot. + if (!std.mem.eql(u8, root, "/")) return error.FilesystemMustBeRoot; + if (Io.Dir.cwd().statFile(io, "/_9web", .{ .follow_symlinks = false })) |_| { + return error.CatalogMountCollision; + } else |err| if (err != error.FileNotFound) return err; + } // Config is immutable for the lifetime of the process. var names: std.StringHashMap(void) = .init(A); defer names.deinit(); @@ -75,9 +88,10 @@ pub const Catalog = struct { try Io.Dir.cwd().createDirPath(io, state_dir); const cat = try A.create(Catalog); cat.* = .{ .io = io, .sites = parsed.value.sites, .states = try A.alloc(State, parsed.value.sites.len), .state_dir = state_dir, .runner = try A.create(Runner) }; + cat.host_filesystem = parsed.value.filesystem != null; @memset(cat.states, .{}); try cat.nodes.append(A, ""); - cat.runner.init(.{ .io = io, .root = 1, .handler = .{ .ctx = cat, .serve = serve } }); + cat.runner.init(.{ .io = io, .root = 1, .handler = .{ .ctx = cat, .serve = serve, .closed = closed } }); return cat; } @@ -130,7 +144,7 @@ pub const Catalog = struct { const rows = try a.alloc(Row, cat.sites.len); for (cat.sites, 0..) |s, i| rows[i] = .{ .name = s.name, .title = s.title, .root = s.root, .port = s.port, .build = s.build.len != 0, .watch = s.watch.len != 0 or s.inputs.len != 0, .status = cat.states[i] }; const listeners = try std.json.parseFromSlice(std.json.Value, a, cat.listeners, .{}); - return std.json.Stringify.valueAlloc(a, .{ .sites = rows, .listeners = listeners.value, .discovery_error = cat.discovery_error }, .{}); + return std.json.Stringify.valueAlloc(a, .{ .sites = rows, .listeners = listeners.value, .discovery_error = cat.discovery_error, .namespace = if (cat.host_filesystem) "/_9web" else "" }, .{}); } fn discoverLoop(cat: *Catalog) void { @@ -304,7 +318,14 @@ pub const Catalog = struct { try cat.nodes.append(A, try A.dupe(u8, path)); return cat.nodes.items.len; } - fn sitePath(cat: *Catalog, path: []const u8) ?struct { i: usize, rest: []const u8 } { + fn virtualPath(cat: *Catalog, path: []const u8) ?[]const u8 { + if (!cat.host_filesystem) return path; + if (std.mem.eql(u8, path, "_9web")) return ""; + if (std.mem.startsWith(u8, path, "_9web/")) return path[6..]; + return null; + } + fn sitePath(cat: *Catalog, raw: []const u8) ?struct { i: usize, rest: []const u8 } { + const path = cat.virtualPath(raw) orelse return null; if (!std.mem.startsWith(u8, path, "sites/")) return null; const rest = path[6..]; const n = std.mem.indexOfScalar(u8, rest, '/') orelse rest.len; @@ -316,14 +337,15 @@ pub const Catalog = struct { result.node = try cat.intern(path); return result; } - fn attrData(cat: *Catalog, a: std.mem.Allocator, path: []const u8) !fs.Attr { - var result: fs.Attr = .{ .name = std.fs.path.basename(path), .mode = 0o444 }; + fn attrData(cat: *Catalog, a: std.mem.Allocator, raw: []const u8) !fs.Attr { + const path = cat.virtualPath(raw) orelse return cat.hostAttr(a, raw); + var result: fs.Attr = .{ .name = std.fs.path.basename(raw), .mode = 0o444 }; if (path.len == 0 or std.mem.eql(u8, path, "sites") or std.mem.eql(u8, path, "services") or std.mem.eql(u8, path, "listeners")) { result.dir = true; result.mode = 0o555; return result; } - if (cat.sitePath(path)) |p| { + if (cat.sitePath(raw)) |p| { if (p.rest.len == 0 or std.mem.eql(u8, p.rest, "public")) { result.dir = true; result.mode = 0o555; @@ -344,11 +366,12 @@ pub const Catalog = struct { return result; } } - const data = try cat.content(a, path); + const data = try cat.content(a, raw); result.size = data.len; return result; } - fn content(cat: *Catalog, a: std.mem.Allocator, path: []const u8) ![]const u8 { + fn content(cat: *Catalog, a: std.mem.Allocator, raw: []const u8) ![]const u8 { + const path = cat.virtualPath(raw) orelse return error.FileNotFound; if (std.mem.eql(u8, path, "index.html")) return @embedFile("static/index.html"); if (std.mem.eql(u8, path, "listeners/index.json")) return cat.listeners; if (std.mem.eql(u8, path, "services/index.json")) { @@ -359,7 +382,7 @@ pub const Catalog = struct { }; return std.json.Stringify.valueAlloc(a, known.items, .{}); } - if (cat.sitePath(path)) |p| { + if (cat.sitePath(raw)) |p| { if (std.mem.eql(u8, p.rest, "status") or std.mem.eql(u8, p.rest, "events")) return std.json.Stringify.valueAlloc(a, cat.states[p.i], .{}); if (std.mem.eql(u8, p.rest, "ctl")) return "build\nwatch on\nwatch off\ncancel\n"; if (std.mem.eql(u8, p.rest, "log")) { @@ -373,11 +396,20 @@ pub const Catalog = struct { } return error.FileNotFound; } - fn children(cat: *Catalog, a: std.mem.Allocator, path: []const u8) ![][]const u8 { + fn children(cat: *Catalog, a: std.mem.Allocator, raw: []const u8) ![][]const u8 { var names: std.ArrayList([]const u8) = .empty; + const path = cat.virtualPath(raw) orelse { + var dir = try Io.Dir.cwd().openDir(cat.io, try hostPath(a, raw), .{ .iterate = true }); + defer dir.close(cat.io); + var it = dir.iterate(); + while (try it.next(cat.io)) |entry| try names.append(a, try a.dupe(u8, entry.name)); + if (raw.len == 0) try names.append(a, "_9web"); + sortNames(names.items); + return names.items; + }; if (path.len == 0) try names.appendSlice(a, &.{ "index.html", "sites", "services", "listeners" }) else if (std.mem.eql(u8, path, "sites")) { for (cat.sites) |s| try names.append(a, s.name); - } else if (std.mem.eql(u8, path, "services") or std.mem.eql(u8, path, "listeners")) try names.append(a, "index.json") else if (cat.sitePath(path)) |p| { + } else if (std.mem.eql(u8, path, "services") or std.mem.eql(u8, path, "listeners")) try names.append(a, "index.json") else if (cat.sitePath(raw)) |p| { if (p.rest.len == 0) try names.appendSlice(a, &.{ "public", "ctl", "status", "log", "events" }) else if (std.mem.eql(u8, p.rest, "public") or std.mem.startsWith(u8, p.rest, "public/")) { const rel = if (p.rest.len == 6) "" else p.rest[7..]; const f = try openBeneath(cat.io, cat.sites[p.i].root, rel); @@ -389,17 +421,46 @@ pub const Catalog = struct { }; } else return error.NotDir; } else return error.NotDir; - std.mem.sort([]const u8, names.items, {}, struct { + sortNames(names.items); + return names.items; + } + fn sortNames(names: [][]const u8) void { + std.mem.sort([]const u8, names, {}, struct { fn less(_: void, x: []const u8, y: []const u8) bool { return std.mem.lessThan(u8, x, y); } }.less); - return names.items; } fn serve(ctx: ?*anyopaque, conn: *Runner.Conn, req: fs.Req) void { const cat: *Catalog = @ptrCast(@alignCast(ctx.?)); var arena = std.heap.ArenaAllocator.init(A); defer arena.deinit(); + if (req.op == .release and req.opened and req.handle != 0) { + cat.mutex.lockUncancelable(cat.io); + const host = req.node > 0 and req.node <= cat.nodes.items.len and cat.virtualPath(cat.nodes.items[@intCast(req.node - 1)]) == null; + var cancel: [32]bool = @splat(false); + if (host) for (cat.reads[conn.index], 0..) |tag, ri| { + cancel[ri] = tag != 0 and cat.read_handles[conn.index][ri] == req.handle; + }; + cat.mutex.unlock(cat.io); + // Flush followed by clunk must interrupt kernel/FUSE reads too, + // not just discard the eventual 9P reply and keep consuming events. + for (cancel, 0..) |yes, ri| if (yes) cat.read_groups[conn.index][ri].cancel(cat.io); + } + // Files may be blocking 9P event streams through FUSE. Never hold the + // catalogue lock while reading/writing them; other connections must run. + if (req.op == .read or req.op == .write) { + cat.mutex.lockUncancelable(cat.io); + const host = req.node > 0 and req.node <= cat.nodes.items.len and cat.virtualPath(cat.nodes.items[@intCast(req.node - 1)]) == null; + const file = if (host and req.handle > 0 and req.handle <= 512) cat.files[conn.index][req.handle - 1] else null; + cat.mutex.unlock(cat.io); + if (host) { + if (req.op == .read) { + cat.startRead(conn, req, file) catch |err| replyError(conn, req.tag, err); + } else cat.hostIo(arena.allocator(), conn, req, file) catch |err| replyError(conn, req.tag, err); + return; + } + } // Controls take their own lock; all other node operations are serialized. if (req.op == .write) { cat.mutex.lockUncancelable(cat.io); @@ -421,13 +482,217 @@ pub const Catalog = struct { } cat.mutex.lockUncancelable(cat.io); defer cat.mutex.unlock(cat.io); - cat.answer(arena.allocator(), conn, req) catch |err| conn.reply(&.{ .tag = req.tag, .status = .err, .ename = if (err == error.FileNotFound) "not found" else @errorName(err) }, ""); + cat.answer(arena.allocator(), conn, req) catch |err| replyError(conn, req.tag, err); + } + fn closed(ctx: ?*anyopaque, conn: *Runner.Conn) void { + const cat: *Catalog = @ptrCast(@alignCast(ctx.?)); + for (&cat.read_groups[conn.index]) |*group| group.cancel(cat.io); + cat.read_groups[conn.index] = @splat(.init); + cat.mutex.lockUncancelable(cat.io); + defer cat.mutex.unlock(cat.io); + cat.reads[conn.index] = @splat(0); + } + fn startRead(cat: *Catalog, conn: *Runner.Conn, req: fs.Req, file: ?Io.File) !void { + const f = file orelse return error.FileNotOpen; + cat.mutex.lockUncancelable(cat.io); + defer cat.mutex.unlock(cat.io); + // Park before dispatch, including retries caused by catalogue changes. + // A completed/flushed request has no ticket and must not be reissued. + _ = conn.hold(&.{ .tag = req.tag, .status = .again }) orelse return; + for (cat.reads[conn.index]) |tag| if (tag == req.tag) return; + var slot: usize = 0; + while (slot < 32 and cat.reads[conn.index][slot] != 0) : (slot += 1) {} + if (slot == 32) return error.TooManyPendingReads; + try cat.read_groups[conn.index][slot].await(cat.io); + cat.read_groups[conn.index][slot] = .init; + // Clunk/flush can release the original handle while this read waits. + const linux = std.os.linux; + const rc = linux.fcntl(f.handle, linux.F.DUPFD_CLOEXEC, 0); + if (linux.errno(rc) != .SUCCESS) return error.TooManyOpenFiles; + const duplicate: Io.File = .{ .handle = @intCast(rc), .flags = f.flags }; + errdefer duplicate.close(cat.io); + cat.reads[conn.index][slot] = req.tag; + cat.read_handles[conn.index][slot] = req.handle; + errdefer cat.reads[conn.index][slot] = 0; + try cat.read_groups[conn.index][slot].concurrent(cat.io, readTask, .{ cat, conn, req, duplicate, slot }); + } + fn readTask(cat: *Catalog, conn: *Runner.Conn, req: fs.Req, file: Io.File, slot: usize) void { + defer file.close(cat.io); + var arena = std.heap.ArenaAllocator.init(A); + defer arena.deinit(); + const b = arena.allocator().alloc(u8, req.size) catch { + cat.mutex.lockUncancelable(cat.io); + defer cat.mutex.unlock(cat.io); + replyError(conn, req.tag, error.OutOfMemory); + cat.reads[conn.index][slot] = 0; + return; + }; + const result = file.readPositional(cat.io, &.{b}, req.off) catch |err| switch (err) { + error.Unseekable => file.readStreaming(cat.io, &.{b}) catch |stream_err| switch (stream_err) { + error.EndOfStream => 0, + else => stream_err, + }, + else => err, + }; + cat.mutex.lockUncancelable(cat.io); + defer cat.mutex.unlock(cat.io); + if (result) |n| conn.reply(&.{ .tag = req.tag }, b[0..n]) else |err| replyError(conn, req.tag, err); + cat.reads[conn.index][slot] = 0; + } + fn replyError(conn: *Runner.Conn, tag: u64, err: anyerror) void { + const message = switch (err) { + error.FileNotFound => "not found", + error.AccessDenied, error.PermissionDenied => "permission denied", + error.PathAlreadyExists => "file exists", + else => @errorName(err), + }; + conn.reply(&.{ .tag = tag, .status = .err, .ename = message }, ""); + } + fn hostPath(a: std.mem.Allocator, path: []const u8) ![]const u8 { + if (!safeRelative(path)) return error.AccessDenied; + return std.fmt.allocPrint(a, "/{s}", .{path}); + } + fn hostAttr(cat: *Catalog, a: std.mem.Allocator, path: []const u8) !fs.Attr { + const full = try hostPath(a, path); + const st = Io.Dir.cwd().statFile(cat.io, full, .{}) catch |err| blk: { + // Keep dangling symlinks visible; an attempt to open reports the error. + if (err != error.FileNotFound) return err; + break :blk try Io.Dir.cwd().statFile(cat.io, full, .{ .follow_symlinks = false }); + }; + return .{ + .name = std.fs.path.basename(path), + .dir = st.kind == .directory, + .size = st.size, + .mtime = @intCast(@min(std.math.maxInt(u32), @max(0, st.mtime.toSeconds()))), + // The 9P engine has no POSIX uid/group/ACL context. The kernel is + // the authority for this export, operating as the service user. + .mode = if (st.kind == .directory) 0o777 else 0o666, + }; + } + fn hostOpen(cat: *Catalog, a: std.mem.Allocator, path: []const u8, mode: u8, truncate: bool) !Io.File { + // Pin the inode with O_PATH, inspect it without opening a device/FIFO, + // then reopen that exact inode. Symlinks and mount points are followed. + const ref = try Io.Dir.cwd().openFile(cat.io, try hostPath(a, path), .{ .path_only = true }); + defer ref.close(cat.io); + const st = try ref.stat(cat.io); + if (st.kind != .file and st.kind != .directory) return error.SpecialFileNotSupported; + const fd_path = try std.fmt.allocPrintSentinel(a, "/proc/self/fd/{d}", .{ref.handle}, 0); + const linux = std.os.linux; + const flags: linux.O = .{ .ACCMODE = switch (mode & 3) { + 1 => .WRONLY, + 2 => .RDWR, + else => .RDONLY, + }, .CLOEXEC = true, .NONBLOCK = true, .NOCTTY = true, .TRUNC = truncate }; + const rc = linux.open(fd_path, flags, 0); + switch (linux.errno(rc)) { + .SUCCESS => return .{ .handle = @intCast(rc), .flags = .{ .nonblocking = true } }, + .ACCES, .PERM => return error.AccessDenied, + .NOENT => return error.FileNotFound, + .ISDIR => return error.IsDir, + .ROFS => return error.PermissionDenied, + else => return error.HostOpenFailed, + } + } + fn hostIo(cat: *Catalog, a: std.mem.Allocator, conn: *Runner.Conn, req: fs.Req, file: ?Io.File) !void { + const f = file orelse return error.FileNotOpen; + if (req.op == .write) { + f.writePositionalAll(cat.io, req.data, req.off) catch |err| switch (err) { + error.Unseekable => try f.writeStreamingAll(cat.io, req.data), + else => return err, + }; + conn.reply(&.{ .tag = req.tag, .written = @intCast(req.data.len) }, ""); + } else { + const b = try a.alloc(u8, req.size); + // A short read is meaningful for synthetic files and event streams. + const n = try f.readPositional(cat.io, &.{b}, req.off); + conn.reply(&.{ .tag = req.tag }, b[0..n]); + } + } + fn hostOperation(cat: *Catalog, a: std.mem.Allocator, conn: *Runner.Conn, req: fs.Req, path: []const u8, reply: *fs.Reply) !void { + const full = try hostPath(a, path); + switch (req.op) { + .open => { + var slot: usize = 0; + while (slot < 512 and cat.files[conn.index][slot] != null) : (slot += 1) {} + if (slot == 512) return error.TooManyOpenFiles; + var next = path; + const f = if (req.create) blk: { + if (!safeName(req.data)) return error.AccessDenied; + next = try std.fs.path.join(a, &.{ path, req.data }); + if (cat.virtualPath(next) != null) return error.PermissionDenied; + const dest = try hostPath(a, next); + if (req.perm & c9.dmdir != 0) { + try Io.Dir.cwd().createDir(cat.io, dest, .fromMode(req.perm & 0o777)); + break :blk try cat.hostOpen(a, next, req.omode, false); + } + break :blk try Io.Dir.cwd().createFile(cat.io, dest, .{ .read = (req.omode & 3) != 1, .exclusive = true, .permissions = .fromMode(req.perm & 0o777) }); + } else try cat.hostOpen(a, path, req.omode, false); + errdefer f.close(cat.io); + reply.attr = try cat.attr(a, next); + reply.handle = @intCast(slot + 1); + cat.files[conn.index][slot] = f; + }, + .release => { + if (req.opened and req.handle > 0 and req.handle <= 512) { + if (cat.files[conn.index][req.handle - 1]) |f| f.close(cat.io); + cat.files[conn.index][req.handle - 1] = null; + } + if (req.remove) { + if (path.len == 0) return error.PermissionDenied; + const st = try Io.Dir.cwd().statFile(cat.io, full, .{ .follow_symlinks = false }); + if (st.kind == .directory) try Io.Dir.cwd().deleteDir(cat.io, full) else try Io.Dir.cwd().deleteFile(cat.io, full); + } + }, + .setattr => { + if (req.set.name and (req.set.mode or req.set.mtime or req.set.length)) return error.CombinedRenameNotSupported; + if (req.set.name) { + if (path.len == 0 or !safeName(req.data)) return error.PermissionDenied; + const next = try std.fs.path.join(a, &.{ std.fs.path.dirname(path) orelse "", req.data }); + if (cat.virtualPath(next) != null) return error.PermissionDenied; + // Allocate replacement names before changing anything on disk. + var replacements: std.ArrayList(struct { index: usize, name: []const u8 }) = .empty; + for (cat.nodes.items, 0..) |n, ni| { + if (std.mem.eql(u8, n, path) or (std.mem.startsWith(u8, n, path) and n.len > path.len and n[path.len] == '/')) + try replacements.append(a, .{ .index = ni, .name = try std.fmt.allocPrint(A, "{s}{s}", .{ next, n[path.len..] }) }); + } + var renamed = false; + errdefer if (!renamed) { + for (replacements.items) |r| A.free(r.name); + }; + try Io.Dir.cwd().renamePreserve(full, .cwd(), try hostPath(a, next), cat.io); + for (replacements.items) |r| { + A.free(cat.nodes.items[r.index]); + cat.nodes.items[r.index] = r.name; + } + renamed = true; + reply.attr = try cat.attr(a, next); + } else { + // OTRUNC must reach FUSE as an open flag, not ftruncate; + // synthetic 9P ctl files often do not implement setattr. + const f = try cat.hostOpen(a, path, if (req.truncate or req.set.length) 1 else 0, req.truncate); + defer f.close(cat.io); + if (req.set.length and !req.truncate) try f.setLength(cat.io, req.length); + if (req.set.mode) try f.setPermissions(cat.io, .fromMode(req.perm & 0o777)); + if (req.set.mtime) try f.setTimestamps(cat.io, .{ .modify_timestamp = .{ .new = .{ .nanoseconds = @as(i96, req.mtime) * std.time.ns_per_s } } }); + reply.attr = try cat.attr(a, path); + } + }, + else => unreachable, + } + } + fn safeName(name: []const u8) bool { + return name.len > 0 and safeRelative(name) and std.mem.indexOfScalar(u8, name, '/') == null; } fn answer(cat: *Catalog, a: std.mem.Allocator, conn: *Runner.Conn, req: fs.Req) !void { if (req.node == 0 or req.node > cat.nodes.items.len) return error.FileNotFound; const path = cat.nodes.items[@intCast(req.node - 1)]; var reply: fs.Reply = .{ .tag = req.tag }; var bytes: []const u8 = ""; + if (cat.virtualPath(path) == null and (req.op == .open or req.op == .release or req.op == .setattr)) { + try cat.hostOperation(a, conn, req, path, &reply); + conn.reply(&reply, ""); + return; + } switch (req.op) { .lookup => { const next = if (std.mem.eql(u8, req.data, ".")) path else if (std.mem.eql(u8, req.data, "..")) (std.fs.path.dirname(path) orelse "") else try std.fs.path.join(a, &.{ path, req.data }); @@ -435,10 +700,11 @@ pub const Catalog = struct { }, .getattr => reply.attr = try cat.attr(a, path), .setattr => { - if (!std.mem.endsWith(u8, path, "/ctl")) return error.PermissionDenied; + if (!std.mem.endsWith(u8, path, "/ctl") or req.set.name or req.set.mode or req.set.mtime or (req.set.length and req.length != 0)) return error.PermissionDenied; reply.attr = try cat.attr(a, path); }, .open => { + if (req.create) return error.PermissionDenied; if ((req.omode & 3) != 0 and !std.mem.endsWith(u8, path, "/ctl")) return error.PermissionDenied; if (std.mem.endsWith(u8, path, "/events")) { var found = false; @@ -452,6 +718,7 @@ pub const Catalog = struct { } }, .release => { + if (req.remove) return error.PermissionDenied; if (req.opened and req.handle != 0) cat.cursors[conn.index][req.handle - 1].used = false; }, .write => unreachable, |
