//! Local sites, build supervision and passive listener discovery. All state is //! owned by 9web; HTTP and 9P use the same controls. Linux catalogue mode only. const std = @import("std"); const c9 = @import("cloud9"); const Io = std.Io; const A = std.heap.page_allocator; 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 }); pub const Site = struct { name: []const u8, title: []const u8, cwd: []const u8, root: []const u8, // Optional compatibility listener. New routes can share the gateway port. port: ?u16 = null, build: []const []const u8 = &.{}, watch: []const []const u8 = &.{}, // Poll only these source paths for a managed rebuild watcher. inputs: []const []const u8 = &.{}, }; const Settings = struct { sites: []const Site, filesystem: ?[]const u8 = null }; const State = struct { phase: []const u8 = "idle", watching: bool = false, queued: bool = false, cancel: bool = false, pid: ?i32 = null, exit_code: ?u8 = null, started: i64 = 0, finished: i64 = 0, generation: u64 = 0, fingerprint: u64 = 0, revision: u64 = 1, }; const EventCursor = struct { used: bool = false, revision: u64 = 0, bytes: [2048]u8 = undefined, len: usize = 0, pos: usize = 0, }; pub const Catalog = struct { io: Io, sites: []const Site, states: []State, state_dir: []const u8, mutex: Io.Mutex = .init, nodes: std.ArrayList([]const u8) = .empty, listeners: []const u8 = "[]", discovery_error: []const u8 = "", group: Io.Group = .init, runner: *Runner, http_port: u16 = 0, host_filesystem: bool = false, export_host: bool = false, namespace: []const u8 = "", host_export: ?*Catalog = null, 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(); for (parsed.value.sites, 0..) |s, i| { if (s.name.len == 0 or s.port == 0 or !std.fs.path.isAbsolute(s.root) or !std.fs.path.isAbsolute(s.cwd)) return error.InvalidSite; for (s.name) |ch| if (!std.ascii.isAlphanumeric(ch) and ch != '-' and ch != '_') return error.InvalidSiteName; if (names.contains(s.name)) return error.DuplicateSite; try names.put(s.name, {}); for (parsed.value.sites[0..i]) |other| if (s.port != null and other.port == s.port) return error.DuplicatePort; for (s.inputs) |input| if (!std.fs.path.isAbsolute(input) and !safeRelative(input)) return error.InvalidWatchInput; } 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.export_host = parsed.value.filesystem != null; @memset(cat.states, .{}); try cat.nodes.append(A, ""); cat.runner.init(.{ .io = io, .root = 1, .handler = .{ .ctx = cat, .serve = serve, .closed = closed } }); return cat; } pub fn start(cat: *Catalog, socket: [:0]const u8, env: c9.post.Env, post_name: []const u8) !void { if (cat.export_host) { // Only the pure service is posted. The host adapter has its own // runner, lock and fid/read tables, and NO supervision tasks. cat.namespace = try std.fmt.allocPrint(A, "/mnt/9p/{s}", .{post_name}); try cat.runner.listenPosted(env, post_name, 16); const host = try A.create(Catalog); host.* = .{ .io = cat.io, .sites = &.{}, .states = &.{}, .state_dir = "", .runner = try A.create(Runner), .host_filesystem = true, .namespace = cat.namespace }; try host.nodes.append(A, ""); host.runner.init(.{ .io = cat.io, .root = 1, .handler = .{ .ctx = host, .serve = serve, .closed = closed } }); cat.host_export = host; _ = try host.runner.listen(.{ .unix = socket }, 16); } else { _ = try cat.runner.listen(.{ .unix = socket }, 16); } try cat.group.concurrent(cat.io, discoverLoop, .{cat}); for (cat.sites, 0..) |_, i| try cat.group.concurrent(cat.io, siteLoop, .{ cat, i }); } pub fn stop(cat: *Catalog) void { if (cat.host_export) |host| host.stop(); cat.group.cancel(cat.io); cat.runner.stop(); } fn now(cat: *Catalog) i64 { return Io.Timestamp.now(cat.io, .real).toSeconds(); } fn logPath(cat: *Catalog, a: std.mem.Allocator, i: usize) ![]const u8 { return std.fmt.allocPrint(a, "{s}/{s}.log", .{ cat.state_dir, cat.sites[i].name }); } pub fn control(cat: *Catalog, i: usize, command: []const u8) !void { cat.mutex.lockUncancelable(cat.io); defer cat.mutex.unlock(cat.io); const cmd = std.mem.trim(u8, command, " \r\n\t"); const s = &cat.states[i]; if (std.mem.eql(u8, cmd, "build")) { if (cat.sites[i].build.len == 0) return error.NoBuildCommand; if (s.watching and cat.sites[i].watch.len != 0) return error.NativeWatcherRunning; s.queued = true; } else if (std.mem.eql(u8, cmd, "watch on")) { if (cat.sites[i].watch.len == 0 and (cat.sites[i].inputs.len == 0 or cat.sites[i].build.len == 0)) return error.NoWatchInputs; s.watching = true; s.queued = true; } else if (std.mem.eql(u8, cmd, "watch off")) { s.watching = false; if (cat.sites[i].watch.len != 0) s.cancel = true; } else if (std.mem.eql(u8, cmd, "cancel")) { s.watching = false; s.queued = false; s.cancel = true; } else return error.UnknownControl; s.revision += 1; cat.runner.wakeAll(); } pub fn snapshot(cat: *Catalog, a: std.mem.Allocator) ![]const u8 { cat.mutex.lockUncancelable(cat.io); defer cat.mutex.unlock(cat.io); return cat.snapshotLocked(a); } fn snapshotLocked(cat: *Catalog, a: std.mem.Allocator) ![]const u8 { const Row = struct { name: []const u8, title: []const u8, root: []const u8, port: ?u16, build: bool, watch: bool, status: State }; 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, .namespace = cat.namespace }, .{}); } fn discoverLoop(cat: *Catalog) void { while (true) { var arena = std.heap.ArenaAllocator.init(A); defer arena.deinit(); const a = arena.allocator(); const pid = std.fmt.allocPrint(a, "{d}", .{std.os.linux.getpid()}) catch return; var ports: std.Io.Writer.Allocating = .init(a); ports.writer.print("{d}", .{cat.http_port}) catch return; for (cat.sites) |site| if (site.port) |port| { ports.writer.print(",{d}", .{port}) catch return; }; const result = std.process.run(A, cat.io, .{ .argv = &.{ "python3", "-c", @embedFile("discover.py"), pid, ports.written() }, .stdout_limit = .limited(2 * 1024 * 1024), .stderr_limit = .limited(8192), .timeout = .{ .duration = .{ .raw = .fromSeconds(15), .clock = .awake } } }) catch |err| { if (err == error.Canceled) return; cat.mutex.lockUncancelable(cat.io); cat.discovery_error = @errorName(err); cat.mutex.unlock(cat.io); cat.io.sleep(.fromSeconds(5), .awake) catch return; continue; }; defer A.free(result.stderr); cat.mutex.lockUncancelable(cat.io); if (result.term == .exited and result.term.exited == 0) { if (cat.listeners.len > 2) A.free(cat.listeners); cat.listeners = result.stdout; cat.discovery_error = ""; } else { A.free(result.stdout); cat.discovery_error = "listener inventory failed"; } cat.mutex.unlock(cat.io); cat.io.sleep(.fromSeconds(5), .awake) catch return; } } fn fingerprint(cat: *Catalog, i: usize) u64 { var hash = std.hash.Wyhash.init(0); var arena = std.heap.ArenaAllocator.init(A); defer arena.deinit(); const a = arena.allocator(); for (cat.sites[i].inputs) |input| { const path = if (std.fs.path.isAbsolute(input)) input else std.fs.path.join(a, &.{ cat.sites[i].cwd, input }) catch continue; const st = Io.Dir.cwd().statFile(cat.io, path, .{ .follow_symlinks = false }) catch continue; hash.update(input); if (st.kind != .directory) { hash.update(std.mem.asBytes(&st.mtime)); hash.update(std.mem.asBytes(&st.size)); continue; } var dir = Io.Dir.cwd().openDir(cat.io, path, .{ .iterate = true, .follow_symlinks = false }) catch continue; defer dir.close(cat.io); var walker = dir.walkSelectively(a) catch continue; defer walker.deinit(); // XOR entry hashes so directory iteration ordering does not trigger builds. var tree: u64 = 0; while (walker.next(cat.io) catch null) |entry| { if (entry.basename[0] == '.' or std.mem.eql(u8, entry.basename, "node_modules") or std.mem.eql(u8, entry.basename, "out") or std.mem.eql(u8, entry.basename, "build") or std.mem.eql(u8, entry.basename, "dist") or std.mem.eql(u8, entry.basename, "__pycache__")) continue; if (entry.kind == .directory) { walker.enter(cat.io, entry) catch {}; continue; } if (entry.kind != .file) continue; const es = entry.dir.statFile(cat.io, entry.basename, .{ .follow_symlinks = false }) catch continue; var eh = std.hash.Wyhash.init(0); eh.update(entry.path); eh.update(std.mem.asBytes(&es.mtime)); eh.update(std.mem.asBytes(&es.size)); tree ^= eh.final(); } hash.update(std.mem.asBytes(&tree)); } return hash.final(); } fn siteLoop(cat: *Catalog, i: usize) void { while (true) { cat.mutex.lockUncancelable(cat.io); const scanning = cat.states[i].watching and cat.sites[i].watch.len == 0; const previous = cat.states[i].fingerprint; cat.mutex.unlock(cat.io); const fp = if (scanning) cat.fingerprint(i) else previous; cat.mutex.lockUncancelable(cat.io); const s = &cat.states[i]; if (s.watching and cat.sites[i].watch.len == 0 and fp != s.fingerprint) s.queued = true; s.fingerprint = fp; const run = s.queued; const native = s.watching and cat.sites[i].watch.len != 0; if (run) { s.queued = false; s.cancel = false; s.phase = if (native) "watching" else "building"; s.started = cat.now(); s.exit_code = null; s.revision += 1; cat.runner.wakeAll(); } cat.mutex.unlock(cat.io); if (run) cat.runBuild(i, native) catch |err| { if (err == error.Canceled) return; cat.mutex.lockUncancelable(cat.io); cat.states[i].phase = @errorName(err); cat.states[i].pid = null; cat.states[i].finished = cat.now(); cat.states[i].revision += 1; cat.runner.wakeAll(); cat.mutex.unlock(cat.io); }; cat.io.sleep(.fromSeconds(1), .awake) catch return; } } const Job = struct { child: std.process.Child, done: std.atomic.Value(bool) = .init(false), term: ?std.process.Child.Term = null, fn wait(job: *Job, io: Io) void { job.term = job.child.wait(io) catch null; job.done.store(true, .release); } }; fn runBuild(cat: *Catalog, i: usize, native: bool) !void { const path = try cat.logPath(A, i); defer A.free(path); const log = try Io.Dir.cwd().createFile(cat.io, path, .{}); defer log.close(cat.io); const cmd = if (native) cat.sites[i].watch else cat.sites[i].build; const argv = try A.alloc([]const u8, cmd.len + 1); defer A.free(argv); argv[0] = "setsid"; @memcpy(argv[1..], cmd); var job: Job = .{ .child = try std.process.spawn(cat.io, .{ .argv = argv, .cwd = .{ .path = cat.sites[i].cwd }, .stdin = .ignore, .stdout = .{ .file = log }, .stderr = .{ .file = log } }) }; const pid = job.child.id.?; var group: Io.Group = .init; // Cancel whole session, not just a shell, including on server shutdown. defer { _ = std.os.linux.kill(-pid, std.os.linux.SIG.KILL); group.cancel(cat.io); job.child.kill(cat.io); } cat.mutex.lockUncancelable(cat.io); cat.states[i].pid = pid; cat.mutex.unlock(cat.io); try group.concurrent(cat.io, Job.wait, .{ &job, cat.io }); var canceled = false; while (!job.done.load(.acquire)) { cat.mutex.lockUncancelable(cat.io); const cancel = cat.states[i].cancel; cat.mutex.unlock(cat.io); if (cancel) { canceled = true; _ = std.os.linux.kill(-pid, std.os.linux.SIG.KILL); } try cat.io.sleep(.fromMilliseconds(100), .awake); } cat.mutex.lockUncancelable(cat.io); defer cat.mutex.unlock(cat.io); const s = &cat.states[i]; const code: u8 = if (job.term) |term| (if (term == .exited) term.exited else 128) else 255; s.pid = null; s.exit_code = code; s.finished = cat.now(); s.phase = if (canceled) "canceled" else if (code == 0) "idle" else "failed"; if (code == 0 and !canceled) s.generation += 1; if (native) s.watching = false; s.revision += 1; cat.runner.wakeAll(); } fn intern(cat: *Catalog, path: []const u8) !u64 { for (cat.nodes.items, 0..) |n, i| if (std.mem.eql(u8, n, path)) return i + 1; if (cat.nodes.items.len >= 65536) return error.TooManyPaths; try cat.nodes.append(A, try A.dupe(u8, path)); return cat.nodes.items.len; } fn virtualPath(cat: *Catalog, path: []const u8) ?[]const u8 { if (!cat.host_filesystem) return path; 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; for (cat.sites, 0..) |s, i| if (std.mem.eql(u8, rest[0..n], s.name)) return .{ .i = i, .rest = if (n == rest.len) "" else rest[n + 1 ..] }; return null; } fn attr(cat: *Catalog, a: std.mem.Allocator, path: []const u8) !fs.Attr { var result = try cat.attrData(a, path); result.node = try cat.intern(path); return result; } 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(raw)) |p| { if (p.rest.len == 0 or std.mem.eql(u8, p.rest, "public")) { result.dir = true; result.mode = 0o555; return result; } if (std.mem.startsWith(u8, p.rest, "public/")) { const file = try openBeneath(cat.io, cat.sites[p.i].root, p.rest[7..]); defer file.close(cat.io); const st = try file.stat(cat.io); result.dir = st.kind == .directory; result.mode = if (result.dir) 0o555 else 0o444; result.size = st.size; result.mtime = @intCast(@max(0, st.mtime.toSeconds())); return result; } if (std.mem.eql(u8, p.rest, "ctl")) { result.mode = 0o666; return result; } } const data = try cat.content(a, raw); result.size = data.len; return result; } 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, "index.json")) return cat.snapshotLocked(a); if (std.mem.eql(u8, path, "listeners/index.json")) return cat.listeners; if (std.mem.eql(u8, path, "services/index.json")) { const parsed = try std.json.parseFromSlice([]std.json.Value, a, cat.listeners, .{}); var known: std.ArrayList(std.json.Value) = .empty; for (parsed.value) |v| if (!std.mem.eql(u8, v.object.get("protocol").?.string, "unknown")) { try known.append(a, v); }; return std.json.Stringify.valueAlloc(a, known.items, .{}); } 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")) { const f = Io.Dir.cwd().openFile(cat.io, try cat.logPath(a, p.i), .{}) catch return ""; defer f.close(cat.io); const st = try f.stat(cat.io); const n = @min(st.size, 256 * 1024); const b = try a.alloc(u8, @intCast(n)); return b[0..try f.readPositionalAll(cat.io, b, st.size - n)]; } } return error.FileNotFound; } 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 cat.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", "index.json", "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(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); defer f.close(cat.io); var dir: Io.Dir = .{ .handle = f.handle }; var it = dir.iterate(); while (try it.next(cat.io)) |e| if (e.kind == .file or e.kind == .directory) { try names.append(a, try a.dupe(u8, e.name)); }; } else return error.NotDir; } else return error.NotDir; 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); } 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); const path = if (req.node > 0 and req.node <= cat.nodes.items.len) cat.nodes.items[@intCast(req.node - 1)] else ""; const p = cat.sitePath(path); cat.mutex.unlock(cat.io); if (p) |s| { if (std.mem.eql(u8, s.rest, "ctl") and req.off == 0 and req.data.len <= 128) { cat.control(s.i, req.data) catch |err| { conn.reply(&.{ .tag = req.tag, .status = .err, .ename = @errorName(err) }, ""); return; }; conn.reply(&.{ .tag = req.tag, .written = @intCast(req.data.len) }, ""); return; } } conn.reply(&fs.Reply.fail(req.tag, fs.E.PERM), ""); return; } cat.mutex.lockUncancelable(cat.io); defer cat.mutex.unlock(cat.io); 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(cat: *Catalog, a: std.mem.Allocator, path: []const u8) ![]const u8 { if (!safeRelative(path)) return error.AccessDenied; // Compatibility alias is a namespace mapping, never a second service. if (std.mem.eql(u8, path, "_9web")) return a.dupe(u8, cat.namespace); if (std.mem.startsWith(u8, path, "_9web/")) return std.fmt.allocPrint(a, "{s}/{s}", .{ cat.namespace, path[6..] }); return std.fmt.allocPrint(a, "/{s}", .{path}); } fn hostAttr(cat: *Catalog, a: std.mem.Allocator, path: []const u8) !fs.Attr { const full = try cat.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 cat.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 cat.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 cat.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 cat.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 }); reply.attr = try cat.attr(a, next); }, .getattr => reply.attr = try cat.attr(a, path), .setattr => { 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; const site_path = cat.sitePath(path); if (site_path) |p| if (std.mem.startsWith(u8, p.rest, "public/")) { const f = try openBeneath(cat.io, cat.sites[p.i].root, p.rest[7..]); errdefer f.close(cat.io); const st = try f.stat(cat.io); if (st.kind == .directory) { f.close(cat.io); } else { if (st.kind != .file) return error.PermissionDenied; var slot: usize = 0; while (slot < 512 and cat.files[conn.index][slot] != null) : (slot += 1) {} if (slot == 512) return error.TooManyOpenFiles; cat.files[conn.index][slot] = f; reply.handle = @intCast(slot + 1); } }; if (site_path != null and std.mem.eql(u8, site_path.?.rest, "events")) { var found = false; for (&cat.cursors[conn.index], 0..) |*cursor, ci| if (!cursor.used) { cursor.* = .{ .used = true }; reply.handle = @intCast(ci + 1); found = true; break; }; if (!found) return error.TooManyEventReaders; } }, .release => { if (req.remove) return error.PermissionDenied; if (req.opened and req.handle != 0) { const p = cat.sitePath(path); if (p != null and std.mem.startsWith(u8, p.?.rest, "public/")) { if (cat.files[conn.index][req.handle - 1]) |f| f.close(cat.io); cat.files[conn.index][req.handle - 1] = null; } else cat.cursors[conn.index][req.handle - 1].used = false; } }, .write => unreachable, .read => { if (cat.sitePath(path)) |p| { if (std.mem.eql(u8, p.rest, "events") and req.handle != 0) { const cursor = &cat.cursors[conn.index][req.handle - 1]; if (cursor.pos == cursor.len) { if (cursor.revision == cat.states[p.i].revision) { conn.reply(&.{ .tag = req.tag, .status = .again }, ""); return; } const json = try std.json.Stringify.valueAlloc(a, cat.states[p.i], .{}); if (json.len + 1 > cursor.bytes.len) return error.EventTooLarge; @memcpy(cursor.bytes[0..json.len], json); cursor.bytes[json.len] = '\n'; cursor.len = json.len + 1; cursor.pos = 0; cursor.revision = cat.states[p.i].revision; } const end = @min(cursor.len, cursor.pos + req.size); bytes = cursor.bytes[cursor.pos..end]; cursor.pos = end; conn.reply(&reply, bytes); return; } if (std.mem.startsWith(u8, p.rest, "public/")) { if (req.handle == 0) return error.FileNotOpen; const f = cat.files[conn.index][req.handle - 1] orelse return error.FileNotOpen; const b = try a.alloc(u8, req.size); bytes = b[0..try f.readPositionalAll(cat.io, b, req.off)]; conn.reply(&reply, bytes); return; } } const all = try cat.content(a, path); const from = @min(req.off, all.len); bytes = all[@intCast(from)..@intCast(@min(all.len, from + req.size))]; }, .readdir => { var all: std.ArrayList(u8) = .empty; const names = try cat.children(a, path); // fs.Req.off is an ENTRY index, not the wire byte offset. // The engine consumes compact node/dir/name records and owns // Stat encoding plus the client's byte-offset cursor. for (names[@intCast(@min(req.off, names.len))..]) |name| { if (name.len > 255) continue; const child_path = try std.fs.path.join(a, &.{ path, name }); const at = cat.attr(a, child_path) catch continue; var header: [10]u8 = undefined; std.mem.writeInt(u64, header[0..8], at.node, .little); header[8] = @intFromBool(at.dir); header[9] = @intCast(name.len); try all.appendSlice(a, &header); try all.appendSlice(a, name); if (all.items.len >= req.size) break; } bytes = all.items; }, } conn.reply(&reply, bytes); } }; pub fn safeRelative(path: []const u8) bool { if (std.fs.path.isAbsolute(path) or std.mem.indexOfScalar(u8, path, 0) != null or std.mem.indexOfScalar(u8, path, '\\') != null) return false; var parts = std.mem.splitScalar(u8, path, '/'); while (parts.next()) |p| if (std.mem.eql(u8, p, "..") or std.mem.eql(u8, p, ".")) return false; return true; } /// Descriptor-relative traversal refuses symlinks in every component, including /// the leaf. This also prevents TOCTOU escapes when a build replaces a directory. pub fn openBeneath(io: Io, root: []const u8, relative: []const u8) !Io.File { if (!safeRelative(relative)) return error.AccessDenied; var dir = try Io.Dir.cwd().openDir(io, root, .{}); defer dir.close(io); var parts = std.mem.tokenizeScalar(u8, relative, '/'); var part = parts.next(); while (part) |p| { part = parts.next(); if (part == null) return dir.openFile(io, p, .{ .follow_symlinks = false }); const next = try dir.openDir(io, p, .{ .follow_symlinks = false }); dir.close(io); dir = next; } return dir.openFile(io, ".", .{ .follow_symlinks = false }); } pub fn mime(path: []const u8) []const u8 { const ext = std.fs.path.extension(path); const types = .{ .{ ".html", "text/html; charset=utf-8" }, .{ ".css", "text/css" }, .{ ".js", "text/javascript" }, .{ ".mjs", "text/javascript" }, .{ ".json", "application/json" }, .{ ".svg", "image/svg+xml" }, .{ ".png", "image/png" }, .{ ".jpg", "image/jpeg" }, .{ ".jpeg", "image/jpeg" }, .{ ".gif", "image/gif" }, .{ ".webp", "image/webp" }, .{ ".ico", "image/x-icon" }, .{ ".woff2", "font/woff2" }, .{ ".woff", "font/woff" }, .{ ".pdf", "application/pdf" }, .{ ".wasm", "application/wasm" }, .{ ".txt", "text/plain; charset=utf-8" } }; inline for (types) |pair| if (std.ascii.eqlIgnoreCase(ext, pair[0])) return pair[1]; return "application/octet-stream"; }