//! 9P2000 session over a blocking file descriptor, shared by several threads. //! //! A thin RPC layer over `cloud9.Client` (push/take, allocation-free). Many //! requests may be outstanding at once, each under its own tag, and their //! replies may come back in any order: a read the server parks (a blocking //! `log` or `event` file) holds up only the thread that asked for it. //! //! The multiplexing follows Plan 9's mount driver (`mountio`, `mountmux` and //! `mntgate` in kernel/devices/sys/devmnt.c). Each thread that makes calls //! owns a `Caller`. A call writes its T-message and then waits; the first //! waiter to find nobody reading the socket takes the reading baton and //! reads for everyone, handing each R-message to the waiter its tag names; //! when its own reply has come in it passes the baton on. There is no reader //! thread and no lock held across a blocking read. //! //! Fids are handed out from a free list; fid 0 is reserved for the root. const std = @import("std"); const cloud9 = @import("cloud9"); const linux = std.os.linux; pub const Address = union(enum) { unix: []const u8, tcp: struct { host: []const u8, port: u16 }, fd: i32, }; /// A Stat whose every field means "leave unchanged" in a Twstat. pub const dontcare = cloud9.Stat{ .type = 0xFFFF, .dev = 0xFFFF_FFFF, .qid = .{ .type = 0xFF, .version = 0xFFFF_FFFF, .path = 0xFFFF_FFFF_FFFF_FFFF }, .mode = 0xFFFF_FFFF, .atime = 0xFFFF_FFFF, .mtime = 0xFFFF_FFFF, .length = 0xFFFF_FFFF_FFFF_FFFF, .name = "", .uid = "", .gid = "", .muid = "", }; /// Requests one session keeps in flight at once (one more tag is kept for a /// Tflush). Every parked read a mount serves holds one tag and pins one /// bridge worker (`bridge.max_workers` is one fewer), so the window must be /// wider than any server parks per connection: pardes parks 128 /// (`fs.Options.slot_capacity` in its editor options) and refuses the next /// with `too many reads waiting: 128`, which frees the worker. With 256 a /// mount can hold every read such a server parks and still have workers /// for the rest of its traffic (a `cat layout` beside 128 held event reads). pub const max_tags: usize = 256; pub const Client = cloud9.ClientWith(max_tags); /// The `Io` the locks here (and the bridge's) hand to `std.Io.Mutex` and /// `std.Io.Condition`. They only use it for futex waits and wakes, and the /// uncancelable paths used throughout ignore the instance and go straight to /// the futex syscall, which is thread-safe; no `Io` is threaded through the /// FUSE loop, so this one stands in. pub fn lockIo() std.Io { return std.Io.Threaded.global_single_threaded.io(); } /// How the owner of a caller (the FUSE bridge) gets a say while its rpc waits /// for a reply. `watch` names a descriptor to poll alongside the socket, or /// -1 to poll nothing extra right now; when it becomes readable `onReadable` /// consumes whatever is there and returns true if the request in flight should /// be cancelled with a Tflush. `armed` reports whether such a cancellation was /// requested earlier for the operation in progress; the chunked read/write /// loops stop between chunks when it is set (a reply that raced the flush still /// leaves the caller wanting out). pub const Interrupt = struct { ctx: *anyopaque, watch: *const fn (ctx: *anyopaque) i32, onReadable: *const fn (ctx: *anyopaque) Session.Error!bool, armed: *const fn (ctx: *anyopaque) bool, }; /// One connection: the socket, the protocol state, the fids, and the table /// of calls waiting for replies. Calls are made through a `Caller`. pub const Session = struct { pub const Error = error{ Nine, Protocol, Io, Closed, Stopped, Interrupted, TooLarge, OutOfMemory }; pub const Walk = struct { nwqid: u16, wqid: [cloud9.max_welem]cloud9.Qid }; pub const Open = struct { qid: cloud9.Qid, iounit: u32 }; gpa: std.mem.Allocator, fd: i32, client: Client, in_buf: []u8, out_buf: []u8, /// Negotiated maximum message size. msize: u32, /// Requests sent at once, at most `max_tags`: a call past it waits for a /// tag as one past the client's window does. Narrowed by tests whose /// fake server holds fewer. window: usize = max_tags, next_fid: u32 = 1, free_fids: std.ArrayList(u32) = .empty, /// Per-fid iounit learned from open/create (0 = none); used to chunk read/write. iounits: std.AutoHashMapUnmanaged(u32, u32) = .empty, /// Optional descriptor every waiting call watches: when it becomes /// readable (the bridge's "child exited" pipe) the call fails with /// `error.Stopped` instead of blocking on a server that never answers. stop_fd: i32 = -1, /// Milliseconds a Tflush may go unanswered before the server is declared /// wedged: the call fails with `error.Interrupted` and the session with it. /// The protocol says a client waits for the Rflush; a server that has not /// managed one in this long is not going to, and the process behind the /// interrupt is unkillable until we stop waiting. 0 waits forever. flush_grace_ms: i32 = 3000, /// The server is gone as far as this session is concerned (see /// `Caller.abort_on_cancel`, `flush_grace_ms`); every call answers /// `error.Closed`, the ones already waiting included. wedged: bool = false, /// The connection itself failed (EOF, a socket error, a reply that /// breaks the protocol): every call answers this. dead: ?Error = null, /// A send failed: the server hung up, perhaps just after answering. No /// more is sent, but the replies already in the socket are still read, /// to EOF or until `drain_until`, before the session dies of this. write_dead: ?Error = null, drain_until: i64 = 0, // -- multiplexing (devmnt's Mnt: `queue`, `rip`) ------------------------------------ /// Guards the client's tags and input side, the tables below, the fid /// tables, `wedged` and `dead`. Never held across a blocking syscall. mutex: std.Io.Mutex = .init, /// Serializes transmission: the client's output buffer and the write side /// of the socket. Taken before `mutex`, and never by the baton holder /// (see `Waiter.cancel`), so a write that blocks because the server is /// itself blocked writing replies cannot stop those replies being read. wlock: std.Io.Mutex = .init, /// The call waiting on each tag. A call that sent a Tflush sits under /// both tags until one of them answers. waiting: [max_tags + 1]?*Waiter = @splat(null), /// The Tversion's waiter (it travels under NOTAG). versioning: ?*Waiter = null, /// Calls that found every tag taken, waiting for a reply to free one. starved: ?*Waiter = null, /// The waiter holding the reading baton, if any (devmnt's `m->rip`). reader: ?*Waiter = null, /// Connect to `address`, then negotiate the protocol version. /// `msize` is the maximum message size to ask for (0 = the buffers' size). pub fn connect(gpa: std.mem.Allocator, address: Address, msize: u32) !Session { return connectWatched(gpa, address, msize, -1); } /// `connect`, with `stop_fd` watched for the whole handshake (the version /// rpc included). A server that accepts the connection and then never /// answers the Tversion would otherwise pin the caller in a blocking read /// with no way out; -1 disables the watch. The field stays set on the /// returned session. An `.fd` address is the caller's to close on failure. pub fn connectWatched(gpa: std.mem.Allocator, address: Address, msize: u32, stop_fd: i32) !Session { var s = try dial(gpa, address, msize, stop_fd); negotiate(&s) catch |e| { s.freeBuffers(); if (address != .fd) _ = linux.close(s.fd); return e; }; return s; } fn negotiate(s: *Session) !void { var c = try Caller.init(s); defer c.deinit(); try c.version(); } /// The transport and the buffers, no handshake: for a caller that wants /// its interrupt source in place before `version()` (9ns's mntgen /// worker, so a walk interrupted mid-dial can abandon the dial). Owns /// the descriptor from here: `deinit` closes it. pub fn dial(gpa: std.mem.Allocator, address: Address, msize: u32, stop_fd: i32) !Session { const want: u32 = if (msize == 0) 8192 else @max(msize, 24); const fd = try openTransport(address); errdefer if (address != .fd) { _ = linux.close(fd); }; const in_buf = try gpa.alloc(u8, want); errdefer gpa.free(in_buf); const out_buf = try gpa.alloc(u8, want); errdefer gpa.free(out_buf); return .{ .gpa = gpa, .fd = fd, .client = .init(.{ .in = in_buf, .out = out_buf }), .in_buf = in_buf, .out_buf = out_buf, .msize = want, .stop_fd = stop_fd, }; } fn freeBuffers(s: *Session) void { s.free_fids.deinit(s.gpa); s.iounits.deinit(s.gpa); s.gpa.free(s.in_buf); s.gpa.free(s.out_buf); } /// Closes the descriptor and frees the buffers. Fids are not clunked. /// Every `Caller` must be done with the session (and is best deinit first). pub fn deinit(s: *Session) void { _ = linux.close(s.fd); s.freeBuffers(); s.* = undefined; } /// Fid 0 is never handed out: it belongs to the root attach. pub fn allocFid(s: *Session) u32 { const io = lockIo(); s.mutex.lockUncancelable(io); defer s.mutex.unlock(io); if (s.free_fids.pop()) |fid| return fid; const fid = s.next_fid; s.next_fid += 1; return fid; } /// Fids currently bound (excluding fid 0); a debugging aid for leak hunting. pub fn fidsInUse(s: *Session) usize { const io = lockIo(); s.mutex.lockUncancelable(io); defer s.mutex.unlock(io); return (s.next_fid - 1) - s.free_fids.items.len; } pub fn freeFid(s: *Session, fid: u32) void { const io = lockIo(); s.mutex.lockUncancelable(io); defer s.mutex.unlock(io); _ = s.iounits.remove(fid); // If the free list cannot grow the fid is simply leaked; the counter keeps going. s.free_fids.append(s.gpa, fid) catch {}; } /// Whether the session was declared gone (`wedged`) or broke (`dead`): /// nothing more will come of it. pub fn over(s: *Session) bool { const io = lockIo(); s.mutex.lockUncancelable(io); defer s.mutex.unlock(io); return s.wedged or s.dead != null; } // -- internals: every function below runs with `mutex` held ------------------------- /// Wakes a caller from its poll: its reply is in, the baton is free, a /// tag was freed, or the session is over. Spurious pokes are harmless /// (the waiter rechecks under the mutex), so the counter is never trusted. fn poke(c: *Caller) void { const one: u64 = 1; _ = linux.write(c.wake, std.mem.asBytes(&one), 8); } /// Wakes every waiting call: the baton is free (devmnt's `mntgate`, which /// wakes one; here all of them recheck, so a waiter that turns out to be /// done or still writing its request cannot swallow the handoff), or the /// session is over. /// `me` is the caller doing it, which needs no poke (and a stale one /// would only cost it a spurious wake-up later). fn pokeAll(s: *Session, me: *Caller) void { for (s.waiting) |slot| if (slot) |w| if (w.caller != me) poke(w.caller); if (s.versioning) |w| if (w.caller != me) poke(w.caller); var it = s.starved; while (it) |w| : (it = w.next_starved) if (w.caller != me) poke(w.caller); } fn die(s: *Session, e: Error, me: *Caller) void { if (s.dead == null) s.dead = e; s.pokeAll(me); } fn wedge(s: *Session, me: *Caller) void { s.wedged = true; s.pokeAll(me); } /// Takes every complete R-message out of the client and hands each to the /// call its tag names (devmnt's `mountmux`). The reply is copied into the /// caller's own buffer: the client's input buffer belongs to whoever reads /// next. A reply nobody waits for any more (its call gave up, or a Rflush /// whose request was answered first) is dropped. fn deliver(s: *Session, me: *Caller) void { var freed = false; while (s.client.take()) |d| { freed = true; const frame = s.client.in[0..s.client.frame]; if (d.op == .version) { const w = s.versioning orelse continue; s.versioning = null; w.result = copyResult(d.result, frame, w.caller.reply); w.done = true; if (w.caller != me) poke(w.caller); continue; } const w = s.waiting[d.tag] orelse continue; s.waiting[d.tag] = null; if (d.op == .flush) { // The Rflush came first: the server dropped the request, and // the client has released its tag with this reply. w.flush_tag = null; if (s.waiting[w.tag] == w) s.waiting[w.tag] = null; if (!w.done) { w.done = true; w.flushed = true; } } else { w.result = copyResult(d.result, frame, w.caller.reply); w.done = true; } if (w.caller != me) poke(w.caller); } if (s.client.dead) s.die(error.Protocol, me); if (freed) { // A reply freed a tag: every starved call tries again. while (s.starved) |w| { s.starved = w.next_starved; w.next_starved = null; w.on_starved = false; w.tag_freed = true; if (w.caller != me) poke(w.caller); } } } /// Takes a waiter out of every table, and passes the baton on if it held it. fn leave(s: *Session, w: *Waiter) void { for (&s.waiting) |*slot| { if (slot.* == w) slot.* = null; } if (s.versioning == w) s.versioning = null; if (w.on_starved) { var link = &s.starved; while (link.*) |it| : (link = &it.next_starved) { if (it == w) { link.* = w.next_starved; break; } } w.on_starved = false; w.next_starved = null; } if (s.reader == w) { s.reader = null; s.pokeAll(w.caller); } } fn starve(s: *Session, w: *Waiter) void { if (w.on_starved) return; w.tag_freed = false; w.on_starved = true; w.next_starved = s.starved; s.starved = w; } /// Claims a tag for `req`, registers `w` under it and writes the message. /// When every tag is taken the call waits (reading, if it gets the baton) /// until a reply frees one; a cancellation meanwhile ends it with /// `error.Interrupted`, nothing having been sent. fn transmit(s: *Session, w: *Waiter, req: cloud9.Client.Request) Error!void { const io = lockIo(); const c = w.caller; while (true) { if (s.wedged) return error.Closed; if (s.dead) |e| return e; if (s.write_dead) |e| return e; // Lock order: wlock, then mutex. s.mutex.unlock(io); s.wlock.lockUncancelable(io); s.mutex.lockUncancelable(io); const full = s.client.pending() >= s.window; const tag = (if (full) error.NoTags else s.client.submit(req)) catch |e| { s.wlock.unlock(io); switch (e) { error.NoTags => { s.starve(w); try s.await(w, .tag); continue; }, error.Handshake, error.Dead => return error.Protocol, error.NoSpace, error.TooLarge => return error.TooLarge, error.BadRequest => { c.setEname("bad request"); return error.Nine; }, } }; w.tag = tag; if (req == .version) s.versioning = w else s.waiting[tag] = w; try s.send(); return; } } /// Writes out what `submit` left in the client's output buffer. Entered /// with `mutex` and `wlock` held; returns with `mutex` held and `wlock` /// released. The socket write happens with `mutex` released, so the /// reader keeps reading while it blocks. fn send(s: *Session) Error!void { const io = lockIo(); s.mutex.unlock(io); const sent = writeOut(s); s.wlock.unlock(io); s.mutex.lockUncancelable(io); sent catch |e| { // This call's request did not go out, so it fails; the calls // already sent may have their replies waiting to be read (a // server that answers and then hangs up), so the session is not // dead until the reader reaches the end of what came in. if (s.write_dead == null) { s.write_dead = e; s.drain_until = nowMs() + drain_ms; } return e; }; } const Until = enum { reply, tag }; // ponytail: a fixed drain window; a server that hangs up closes the // socket, so EOF normally ends the drain long before this. const drain_ms = 1000; /// Waits until `w` is answered (`.reply`: its reply or its Rflush is in) /// or a tag was freed (`.tag`). Takes the reading baton whenever nobody /// holds it (devmnt's `mountio` gate), reads for everyone while holding /// it, and lets go of it on the way out. /// /// While waiting it polls, besides its own wake-up descriptor, `stop_fd` /// (→ `error.Stopped`) and the caller's interrupt source. When the latter /// asks for a cancellation a Tflush for the request's tag goes out and the /// wait continues until either the original reply arrives (the flush lost /// the race: it is honoured, and its result returned, as flush(5) says; /// the later Rflush is dropped) or the Rflush does (the call ends in /// `error.Interrupted`), or neither within `flush_grace_ms` (→ /// `error.Interrupted`, and the session is wedged: the server stopped /// talking). Under `abort_on_cancel` the cancellation itself wedges the /// session, with nothing sent. fn await(s: *Session, w: *Waiter, until: Until) Error!void { const io = lockIo(); const c = w.caller; defer if (s.reader == w) { s.reader = null; s.pokeAll(c); }; var cancel = false; var flush_deadline: ?i64 = null; var tmp: [64 * 1024]u8 = undefined; while (true) { switch (until) { .reply => if (w.done) return, .tag => if (w.tag_freed) return, } if (s.wedged) return error.Closed; if (s.dead) |e| return e; // (On the starved list the Tflush waits for a reply to free a tag.) if (cancel and w.flush_tag == null and !w.on_starved and until == .reply) { // devmnt's `mntflushalloc`: the flush is an rpc of its own. // The baton goes first, so a write that blocks cannot stop // everyone's replies (the Rflush among them) being read. if (s.reader == w) { s.reader = null; s.pokeAll(c); } try s.sendFlush(w); continue; } if (s.reader == null) s.reader = w; const reading = s.reader == w; // Only the baton holder touches the input side, so `room` holds // across the unlocked read. After take() returned null the frame // in progress still fits (a frame is at most msize bytes). const room = s.client.in.len - s.client.in_len; if (reading and room == 0) { s.die(error.Protocol, c); continue; } var grace: i32 = if (flush_deadline) |d| @intCast(@max(d - nowMs(), 0)) else -1; if (s.write_dead != null) { const left: i32 = @intCast(@max(s.drain_until - nowMs(), 0)); grace = if (grace < 0) left else @min(grace, left); } s.mutex.unlock(io); var got: usize = 0; const ready = c.wait(if (reading) s.fd else -1, grace); const read_err: ?Error = if (ready) |r| (if (r == .socket) blk: { got = readSocket(s.fd, tmp[0..@min(room, tmp.len)]) catch |e| break :blk e; break :blk null; } else null) else |_| null; s.mutex.lockUncancelable(io); switch (try ready) { .socket => { if (read_err) |e| { s.die(e, c); continue; } if (got == 0) { s.die(error.Closed, c); continue; } if (s.client.push(tmp[0..got]) != got) { s.die(error.Protocol, c); continue; } s.deliver(c); }, .wake => {}, .cancel => { if (c.abort_on_cancel) { s.wedge(c); return error.Interrupted; } // Nothing on the wire yet: nothing to flush. if (until == .tag) return error.Interrupted; if (!cancel) { cancel = true; if (s.flush_grace_ms != 0) flush_deadline = nowMs() + s.flush_grace_ms; } }, .timeout => { // The drain after a failed send is over: nothing more is coming. if (s.write_dead) |e| if (nowMs() >= s.drain_until) { s.die(e, c); continue; }; s.wedge(c); return error.Interrupted; }, } } } /// Sends the Tflush for `w`'s request, unless its reply beat us to it. /// With no tag free it waits on the starved list: the next reply frees /// one (the client keeps a tag aside for flushes, but several calls may /// be flushing at once). fn sendFlush(s: *Session, w: *Waiter) Error!void { const io = lockIo(); s.mutex.unlock(io); s.wlock.lockUncancelable(io); s.mutex.lockUncancelable(io); if (w.done) { s.wlock.unlock(io); return; } const tag = s.client.submit(.{ .flush = .{ .oldtag = w.tag } }) catch |e| { s.wlock.unlock(io); if (e != error.NoTags) return error.Protocol; s.starve(w); return; }; w.flush_tag = tag; s.waiting[tag] = w; try s.send(); } }; /// One call in flight (devmnt's Mntrpc): lives on the calling thread's stack /// for the length of the rpc. const Waiter = struct { caller: *Caller, tag: u16 = cloud9.notag, /// The Tflush sent for this call, until its Rflush (or the reply) is in. flush_tag: ?u16 = null, /// The reply (or, after a Tflush, the Rflush) came in. done: bool = false, /// It was the Rflush: the server dropped the request. flushed: bool = false, result: cloud9.Client.Result = undefined, /// Starved-list link, and the flag a freed tag sets. on_starved: bool = false, next_starved: ?*Waiter = null, tag_freed: bool = false, }; /// One thread's side of a session: where its replies land, the descriptor /// that wakes it, its interrupt source and its last error. Every thread /// that makes calls on a session has its own; a Caller is used by one /// thread at a time. pub const Caller = struct { session: *Session, /// The reply to the latest call, copied out of the session's input /// buffer (msize bytes): result slices borrow it until the next call. reply: []u8, /// eventfd poked by whoever has something for this caller. wake: i32, /// After `error.Nine`, the server's Rerror text (copied, bounded). ename: [256]u8 = undefined, ename_len: usize = 0, /// Optional interrupt source consulted while a reply is outstanding; /// see `Interrupt`. interrupt: ?Interrupt = null, /// While set, a cancellation ends the rpc at once with `error.Interrupted` /// and wedges the session, with no Tflush: for the handshake (version, /// attach, the root stat), where there is no session yet to flush a /// request out of, and the honest answer to "stop waiting" is to hang up. abort_on_cancel: bool = false, pub fn init(s: *Session) !Caller { const rc = linux.eventfd(0, linux.EFD.CLOEXEC | linux.EFD.NONBLOCK); if (linux.errno(rc) != .SUCCESS) return error.SystemResources; const wake: i32 = @intCast(rc); errdefer _ = linux.close(wake); return .{ .session = s, .reply = try s.gpa.alloc(u8, s.in_buf.len), .wake = wake }; } pub fn deinit(c: *Caller) void { c.session.gpa.free(c.reply); _ = linux.close(c.wake); c.* = undefined; } /// Generic RPC. Result slices borrow the caller's reply buffer until its /// next call. See `Session.await` for how the wait ends. pub fn rpc(c: *Caller, req: cloud9.Client.Request) Session.Error!cloud9.Client.Result { const s = c.session; const io = lockIo(); c.ename_len = 0; var w: Waiter = .{ .caller = c }; s.mutex.lockUncancelable(io); defer s.mutex.unlock(io); defer s.leave(&w); try s.transmit(&w, req); try s.await(&w, .reply); if (w.flushed) return error.Interrupted; switch (w.result) { .fail => |ename| { c.setEname(ename); return error.Nine; }, else => return w.result, } } /// Negotiates the protocol version with the msize `dial` was given. pub fn version(c: *Caller) Session.Error!void { const s = c.session; const r = try c.rpc(.{ .version = .{ .msize = s.msize } }); if (!std.mem.eql(u8, r.version.version, "9P2000")) return error.Protocol; s.msize = r.version.msize; } pub fn attach(c: *Caller, fid: u32, uname: []const u8, aname: []const u8) Session.Error!cloud9.Qid { const r = try c.rpc(.{ .attach = .{ .fid = fid, .uname = uname, .aname = aname } }); return r.attach; } /// True when the interrupt source has asked for the operation in progress to /// stop; consulted between the chunks of a read or write. pub fn interruptArmed(c: *const Caller) bool { const i = c.interrupt orelse return false; return i.armed(i.ctx); } /// Walk `names` from `fid` to `newfid`. A partial walk leaves `newfid` unbound /// (9P semantics) and reports `error.Nine` with ename "file does not exist". pub fn walk(c: *Caller, fid: u32, newfid: u32, names: []const []const u8) Session.Error!Session.Walk { const r = try c.rpc(.{ .walk = .{ .fid = fid, .newfid = newfid, .names = names } }); if (r.walk.nwqid < names.len) { c.setEname("file does not exist"); return error.Nine; } return .{ .nwqid = r.walk.nwqid, .wqid = r.walk.wqid }; } /// allocFid + zero-element walk. The fid is released again on failure. pub fn clone(c: *Caller, fid: u32) Session.Error!u32 { const newfid = c.session.allocFid(); errdefer c.session.freeFid(newfid); _ = try c.walk(fid, newfid, &.{}); return newfid; } pub fn open(c: *Caller, fid: u32, mode: u8) Session.Error!Session.Open { const r = try c.rpc(.{ .open = .{ .fid = fid, .mode = mode } }); c.noteIounit(fid, r.open.iounit); return .{ .qid = r.open.qid, .iounit = r.open.iounit }; } pub fn create(c: *Caller, fid: u32, name: []const u8, perm: u32, mode: u8) Session.Error!Session.Open { const r = try c.rpc(.{ .create = .{ .fid = fid, .name = name, .perm = perm, .mode = mode } }); c.noteIounit(fid, r.create.iounit); return .{ .qid = r.create.qid, .iounit = r.create.iounit }; } /// Reads into `buf`, chunking by min(maxRead, iounit) and stopping at the first /// short read. Returns the number of bytes read (0 at end of file). pub fn read(c: *Caller, fid: u32, offset: u64, buf: []u8) Session.Error!usize { return readWith(c, rpc, fid, offset, buf, c.chunk(fid, .read)); } /// Writes `data`, chunking like `read` and stopping at the first short write. pub fn write(c: *Caller, fid: u32, offset: u64, data: []const u8) Session.Error!usize { return writeWith(c, rpc, fid, offset, data, c.chunk(fid, .write)); } /// The returned Stat's strings (name/uid/gid/muid) borrow the caller's /// reply buffer: they are valid only until its next rpc. Copy what must /// outlive it. pub fn stat(c: *Caller, fid: u32) Session.Error!cloud9.Stat { const r = try c.rpc(.{ .stat = .{ .fid = fid } }); return r.stat; } pub fn wstat(c: *Caller, fid: u32, st: cloud9.Stat) Session.Error!void { _ = try c.rpc(.{ .wstat = .{ .fid = fid, .stat = st } }); } /// Frees the fid locally even when the server reports an error. pub fn clunk(c: *Caller, fid: u32) Session.Error!void { defer c.session.freeFid(fid); _ = try c.rpc(.{ .clunk = .{ .fid = fid } }); } /// Frees the fid locally even when the server reports an error. pub fn remove(c: *Caller, fid: u32) Session.Error!void { defer c.session.freeFid(fid); _ = try c.rpc(.{ .remove = .{ .fid = fid } }); } /// Maps the last Rerror text to an errno (case-insensitive substring match). pub fn errno(c: *const Caller) linux.E { return enameToErrno(c.ename[0..c.ename_len]); } // -- internals -------------------------------------------------------------- fn setEname(c: *Caller, text: []const u8) void { const n = @min(text.len, 255); @memcpy(c.ename[0..n], text[0..n]); c.ename_len = n; } fn noteIounit(c: *Caller, fid: u32, iounit: u32) void { const s = c.session; const io = lockIo(); s.mutex.lockUncancelable(io); defer s.mutex.unlock(io); if (iounit == 0) { _ = s.iounits.remove(fid); } else { s.iounits.put(s.gpa, fid, iounit) catch {}; } } fn chunk(c: *Caller, fid: u32, dir: enum { read, write }) u32 { const s = c.session; const io = lockIo(); s.mutex.lockUncancelable(io); defer s.mutex.unlock(io); const max = switch (dir) { .read => s.client.maxRead(), .write => s.client.maxWrite(), }; return chunkSize(max, s.iounits.get(fid) orelse 0); } const Ready = enum { socket, wake, cancel, timeout }; /// Blocks until the socket is readable (`.socket`; `sock` is -1 when this /// caller does not hold the baton), the caller is poked (`.wake`), the /// interrupt source wants the request in flight cancelled (`.cancel`), /// `timeout_ms` passes with none of that (`.timeout`; -1 waits forever), /// or `stop_fd` fires (`error.Stopped`). Anything the interrupt source /// consumes without asking for a cancellation simply resumes the wait. fn wait(c: *Caller, sock: i32, timeout_ms: i32) Session.Error!Ready { const stop_fd = c.session.stop_fd; while (true) { var pfds: [4]linux.pollfd = undefined; var n: usize = 0; pfds[n] = .{ .fd = sock, .events = linux.POLL.IN, .revents = 0 }; // -1: ignored by poll n += 1; pfds[n] = .{ .fd = c.wake, .events = linux.POLL.IN, .revents = 0 }; n += 1; const stop_at: ?usize = if (stop_fd >= 0) n else null; if (stop_at != null) { pfds[n] = .{ .fd = stop_fd, .events = linux.POLL.IN, .revents = 0 }; n += 1; } const ifd: i32 = if (c.interrupt) |i| i.watch(i.ctx) else -1; const int_at: ?usize = if (ifd >= 0) n else null; if (int_at != null) { pfds[n] = .{ .fd = ifd, .events = linux.POLL.IN, .revents = 0 }; n += 1; } const prc = linux.poll(&pfds, @intCast(n), timeout_ms); switch (linux.errno(prc)) { .SUCCESS => {}, .INTR, .AGAIN => continue, else => return error.Io, } if (prc == 0) return .timeout; // A reply that is already there wins over everything else. if (pfds[0].revents != 0) return .socket; if (pfds[1].revents != 0) { var count: u64 = 0; _ = linux.read(c.wake, std.mem.asBytes(&count), 8); return .wake; } if (stop_at) |i| { if (pfds[i].revents != 0) return error.Stopped; } if (int_at) |i| { if (pfds[i].revents != 0) { const src = c.interrupt.?; if (try src.onReadable(src.ctx)) return .cancel; } } } } }; /// A result whose slices point into `from` (the frame in the client's input /// buffer), with those slices moved to the same offsets in `to`, where the /// frame is copied. fn copyResult(r: cloud9.Client.Result, from: []const u8, to: []u8) cloud9.Client.Result { @memcpy(to[0..from.len], from); const move = struct { fn f(slice: []const u8, src: []const u8, dst: []u8) []const u8 { const lo = @intFromPtr(src.ptr); const at = @intFromPtr(slice.ptr); if (at < lo or at + slice.len > lo + src.len) return slice; // a literal, not the frame return dst[at - lo ..][0..slice.len]; } }.f; var out = r; switch (out) { .fail => |*e| e.* = move(e.*, from, to), .version => |*v| v.version = move(v.version, from, to), .read => |*d| d.* = move(d.*, from, to), .stat => |*st| { st.name = move(st.name, from, to); st.uid = move(st.uid, from, to); st.gid = move(st.gid, from, to); st.muid = move(st.muid, from, to); }, else => {}, } return out; } /// Writes everything in the client's output buffer to the socket (with `wlock` held). fn writeOut(s: *Session) Session.Error!void { while (s.client.output().len != 0) { const out = s.client.output(); const rc = linux.write(s.fd, out.ptr, out.len); switch (linux.errno(rc)) { .SUCCESS => { if (rc == 0) return error.Closed; s.client.wrote(rc); }, .INTR, .AGAIN => continue, .PIPE, .CONNRESET => return error.Closed, else => return error.Io, } } } /// The monotonic clock in milliseconds: deadlines, not timestamps. fn nowMs() i64 { var ts: linux.timespec = undefined; _ = linux.clock_gettime(.MONOTONIC, &ts); return @as(i64, ts.sec) * 1000 + @divTrunc(@as(i64, ts.nsec), 1_000_000); } fn chunkSize(max: u32, iounit: u32) u32 { if (iounit != 0 and iounit < max) return iounit; return max; } /// Chunked read over any rpc-shaped function (injected so the loop is testable). fn readWith( s: anytype, comptime rpcFn: anytype, fid: u32, offset: u64, buf: []u8, max_chunk: u32, ) Session.Error!usize { if (max_chunk == 0) return error.Protocol; var done: usize = 0; while (done < buf.len) { // Like read(2): an interruption after some data arrived is a short read. if (done != 0 and s.interruptArmed()) break; const want: u32 = @intCast(@min(buf.len - done, max_chunk)); const r = rpcFn(s, .{ .read = .{ .fid = fid, .offset = offset + done, .count = want } }) catch |e| { if (e == error.Interrupted and done != 0) break; return e; }; const data = r.read; @memcpy(buf[done..][0..data.len], data); done += data.len; if (data.len < want) break; } return done; } /// Chunked write over any rpc-shaped function. fn writeWith( s: anytype, comptime rpcFn: anytype, fid: u32, offset: u64, data: []const u8, max_chunk: u32, ) Session.Error!usize { if (max_chunk == 0) return error.Protocol; var done: usize = 0; while (done < data.len) { if (done != 0 and s.interruptArmed()) break; const want: usize = @min(data.len - done, max_chunk); const r = rpcFn(s, .{ .write = .{ .fid = fid, .offset = offset + done, .data = data[done..][0..want] } }) catch |e| { if (e == error.Interrupted and done != 0) break; return e; }; done += r.write; if (r.write < want) break; } return done; } fn readSocket(fd: i32, buf: []u8) Session.Error!usize { while (true) { const rc = linux.read(fd, buf.ptr, buf.len); switch (linux.errno(rc)) { .SUCCESS => return rc, .INTR, .AGAIN => continue, .CONNRESET => return error.Closed, else => return error.Io, } } } /// Rerror text → errno, per docs/DESIGN.md (first match wins): cloud9's /// fs.enameErrno, which a server's own tests can ask too. pub fn enameToErrno(ename: []const u8) linux.E { return @enumFromInt(cloud9.fs.enameErrno(ename)); } // -- transport ------------------------------------------------------------------ fn openTransport(address: Address) !i32 { switch (address) { .fd => |fd| return fd, .unix => |path| { if (path.len == 0 or path.len >= 108) return error.NameTooLong; var sa: linux.sockaddr.un = .{ .path = @splat(0) }; @memcpy(sa.path[0..path.len], path); const fd = try newSocket(linux.AF.UNIX, 0); errdefer _ = linux.close(fd); try doConnect(fd, @ptrCast(&sa), @sizeOf(linux.sockaddr.un)); return fd; }, .tcp => |t| { const ip = std.Io.net.IpAddress.parse(t.host, t.port) catch return error.InvalidAddress; switch (ip) { .ip4 => |a| { const sa: linux.sockaddr.in = .{ .port = std.mem.nativeToBig(u16, t.port), .addr = @bitCast(a.bytes), }; const fd = try newSocket(linux.AF.INET, linux.IPPROTO.TCP); errdefer _ = linux.close(fd); setNodelay(fd); try doConnect(fd, @ptrCast(&sa), @sizeOf(linux.sockaddr.in)); return fd; }, .ip6 => |a| { const sa: linux.sockaddr.in6 = .{ .port = std.mem.nativeToBig(u16, t.port), .flowinfo = 0, .addr = a.bytes, .scope_id = 0, }; const fd = try newSocket(linux.AF.INET6, linux.IPPROTO.TCP); errdefer _ = linux.close(fd); setNodelay(fd); try doConnect(fd, @ptrCast(&sa), @sizeOf(linux.sockaddr.in6)); return fd; }, } }, } } fn newSocket(domain: u32, protocol: u32) !i32 { const rc = linux.socket(domain, linux.SOCK.STREAM | linux.SOCK.CLOEXEC, protocol); switch (linux.errno(rc)) { .SUCCESS => return @intCast(rc), .MFILE, .NFILE => return error.ProcessFdQuotaExceeded, .AFNOSUPPORT, .PROTONOSUPPORT => return error.AddressFamilyNotSupported, .ACCES => return error.AccessDenied, .NOMEM, .NOBUFS => return error.SystemResources, else => return error.Unexpected, } } fn setNodelay(fd: i32) void { const one: u32 = 1; _ = linux.setsockopt(fd, linux.IPPROTO.TCP, linux.TCP.NODELAY, @ptrCast(&one), @sizeOf(u32)); } fn doConnect(fd: i32, addr: *const linux.sockaddr, len: linux.socklen_t) !void { while (true) { const rc = linux.connect(fd, addr, len); switch (linux.errno(rc)) { .SUCCESS => return, .INTR => continue, .CONNREFUSED => return error.ConnectionRefused, .NOENT, .NOTDIR => return error.FileNotFound, .ACCES, .PERM => return error.AccessDenied, .TIMEDOUT => return error.ConnectionTimedOut, .NETUNREACH, .HOSTUNREACH => return error.NetworkUnreachable, .ADDRNOTAVAIL => return error.AddressNotAvailable, .AGAIN, .INPROGRESS => return error.WouldBlock, else => return error.Unexpected, } } } // -- tests ---------------------------------------------------------------------- const testing = std.testing; test { testing.refAllDecls(@This()); } test "acme's address and argument refusals are EINVAL, and remap nothing an older rule answers" { for ([_][]const u8{ "no match for regexp", "no previous regular expression", "address out of range", "address out of range: a column counts from 1", "addresses out of order", "range past end of body", "no address: the last one written to addr failed", "pane not locked by this open", "size: too small for the panes, each its tag and 2 rows \"160 57\"", "size: a frontend is attached and owns the size \"120 40\"", "no question asked \"answer\"", "answer takes k j or -", "answer takes a b or - \"answer\"", }) |ename| try testing.expectEqual(linux.E.INVAL, enameToErrno(ename)); // Earlier rules still win: a fid's own words stay EBADF. try testing.expectEqual(linux.E.BADF, enameToErrno("fid unknown or out of range")); try testing.expectEqual(linux.E.NOENT, enameToErrno("no such file: answer takes")); } test "ename → errno mapping" { try testing.expectEqual(linux.E.INTR, enameToErrno("Interrupted system call")); try testing.expectEqual(linux.E.INTR, enameToErrno("read interrupted")); try testing.expectEqual(linux.E.IO, enameToErrno("window shut down")); try testing.expectEqual(linux.E.NOENT, enameToErrno("file does not exist")); try testing.expectEqual(linux.E.NOENT, enameToErrno("No Such File")); try testing.expectEqual(linux.E.MFILE, enameToErrno("too many open files")); try testing.expectEqual(linux.E.NOENT, enameToErrno("directory entry not found")); try testing.expectEqual(linux.E.EXIST, enameToErrno("file already exists")); try testing.expectEqual(linux.E.NOTEMPTY, enameToErrno("directory not empty")); try testing.expectEqual(linux.E.NOTDIR, enameToErrno("not a directory")); try testing.expectEqual(linux.E.ISDIR, enameToErrno("is a directory")); try testing.expectEqual(linux.E.ACCES, enameToErrno("permission denied")); try testing.expectEqual(linux.E.ACCES, enameToErrno("access denied")); try testing.expectEqual(linux.E.ROFS, enameToErrno("read-only file system")); try testing.expectEqual(linux.E.NOSPC, enameToErrno("no space left")); try testing.expectEqual(linux.E.PERM, enameToErrno("operation not permitted")); try testing.expectEqual(linux.E.PERM, enameToErrno("cannot remove root")); try testing.expectEqual(linux.E.BADF, enameToErrno("unknown fid")); try testing.expectEqual(linux.E.BADF, enameToErrno("fid in use")); // "fid" precedes "in use" try testing.expectEqual(linux.E.INVAL, enameToErrno("bad offset")); try testing.expectEqual(linux.E.INVAL, enameToErrno("invalid argument")); try testing.expectEqual(linux.E.INVAL, enameToErrno("bad request")); try testing.expectEqual(linux.E.INVAL, enameToErrno("unknown control message \"Bogus 1\"")); try testing.expectEqual(linux.E.INVAL, enameToErrno("wrong #args in control message \"Newcol 2\"")); try testing.expectEqual(linux.E.INVAL, enameToErrno("ill-formed control message")); try testing.expectEqual(linux.E.INVAL, enameToErrno("unknown control message \"Find permission denied\"")); try testing.expectEqual(linux.E.BUSY, enameToErrno("device busy")); try testing.expectEqual(linux.E.NAMETOOLONG, enameToErrno("name too long")); try testing.expectEqual(linux.E.OPNOTSUPP, enameToErrno("operation not supported")); try testing.expectEqual(linux.E.IO, enameToErrno("something odd happened")); try testing.expectEqual(linux.E.IO, enameToErrno("")); } test "fid allocator recycles and never hands out 0" { var s: Session = undefined; s.gpa = testing.allocator; s.mutex = .init; s.next_fid = 1; s.free_fids = .empty; s.iounits = .empty; defer s.free_fids.deinit(s.gpa); defer s.iounits.deinit(s.gpa); const a = s.allocFid(); const b = s.allocFid(); const c = s.allocFid(); try testing.expectEqual(@as(u32, 1), a); try testing.expectEqual(@as(u32, 2), b); try testing.expectEqual(@as(u32, 3), c); s.freeFid(b); try testing.expectEqual(b, s.allocFid()); s.freeFid(a); s.freeFid(c); const x = s.allocFid(); const y = s.allocFid(); try testing.expect((x == a and y == c) or (x == c and y == a)); try testing.expectEqual(@as(u32, 4), s.allocFid()); try testing.expect(a != 0 and b != 0 and c != 0); } test "chunkSize honours iounit only when smaller" { try testing.expectEqual(@as(u32, 100), chunkSize(100, 0)); try testing.expectEqual(@as(u32, 40), chunkSize(100, 40)); try testing.expectEqual(@as(u32, 100), chunkSize(100, 400)); } /// Fake rpc for the chunked read/write loops: a file of `len` bytes where byte i == i & 0xff. const FakeFile = struct { len: usize, calls: usize = 0, max_count: u32 = 0, short_write_at: ?usize = null, /// Fail the call with `error.Interrupted` once this many calls were made. interrupt_at: ?usize = null, /// Report an armed interrupt once this many calls were made. armed_at: ?usize = null, scratch: [4096]u8 = undefined, fn interruptArmed(f: *const FakeFile) bool { return if (f.armed_at) |at| f.calls >= at else false; } fn rpc(f: *FakeFile, req: cloud9.Client.Request) Session.Error!cloud9.Client.Result { f.calls += 1; if (f.interrupt_at) |at| if (f.calls > at) return error.Interrupted; switch (req) { .read => |r| { f.max_count = @max(f.max_count, r.count); if (r.offset >= f.len) return .{ .read = "" }; const n: usize = @min(@as(usize, r.count), f.len - @as(usize, @intCast(r.offset))); for (f.scratch[0..n], 0..) |*b, i| b.* = @truncate(r.offset + i); return .{ .read = f.scratch[0..n] }; }, .write => |w| { f.max_count = @max(f.max_count, @as(u32, @intCast(w.data.len))); if (f.short_write_at) |at| { if (w.offset + w.data.len > at) { const n: usize = if (w.offset >= at) 0 else @intCast(at - w.offset); return .{ .write = @intCast(n) }; } } return .{ .write = @intCast(w.data.len) }; }, else => unreachable, } } }; test "read chunks by max_chunk and stops at a short read" { var f: FakeFile = .{ .len = 2500 }; var buf: [4000]u8 = undefined; const n = try readWith(&f, FakeFile.rpc, 7, 0, &buf, 1000); try testing.expectEqual(@as(usize, 2500), n); try testing.expectEqual(@as(usize, 3), f.calls); // 1000, 1000, 500 (short → stop) try testing.expectEqual(@as(u32, 1000), f.max_count); for (buf[0..n], 0..) |b, i| try testing.expectEqual(@as(u8, @truncate(i)), b); // Reading exactly up to a chunk boundary uses one call per chunk and no more. f = .{ .len = 2000 }; try testing.expectEqual(@as(usize, 2000), try readWith(&f, FakeFile.rpc, 7, 0, buf[0..2000], 1000)); try testing.expectEqual(@as(usize, 2), f.calls); // Offset past EOF → 0. f = .{ .len = 10 }; try testing.expectEqual(@as(usize, 0), try readWith(&f, FakeFile.rpc, 7, 50, &buf, 1000)); } test "write chunks and stops at a short write" { var f: FakeFile = .{ .len = 0 }; var data: [2500]u8 = undefined; for (&data, 0..) |*b, i| b.* = @truncate(i); try testing.expectEqual(@as(usize, 2500), try writeWith(&f, FakeFile.rpc, 7, 0, &data, 1000)); try testing.expectEqual(@as(usize, 3), f.calls); try testing.expectEqual(@as(u32, 1000), f.max_count); f = .{ .len = 0, .short_write_at = 1500 }; try testing.expectEqual(@as(usize, 1500), try writeWith(&f, FakeFile.rpc, 7, 0, &data, 1000)); try testing.expectEqual(@as(usize, 2), f.calls); } test "interrupted chunk loops: partial count if data moved, Interrupted otherwise" { var buf: [4000]u8 = undefined; // The second chunk's rpc is interrupted: the first chunk is returned. var f: FakeFile = .{ .len = 2500, .interrupt_at = 1 }; try testing.expectEqual(@as(usize, 1000), try readWith(&f, FakeFile.rpc, 7, 0, &buf, 1000)); // The first chunk's rpc is interrupted: nothing was transferred. f = .{ .len = 2500, .interrupt_at = 0 }; try testing.expectError(error.Interrupted, readWith(&f, FakeFile.rpc, 7, 0, &buf, 1000)); // A reply that raced the flush arms the interrupt: stop before the next chunk. f = .{ .len = 2500, .armed_at = 2 }; try testing.expectEqual(@as(usize, 2000), try readWith(&f, FakeFile.rpc, 7, 0, &buf, 1000)); try testing.expectEqual(@as(usize, 2), f.calls); // Same for writes. var data: [2500]u8 = undefined; for (&data, 0..) |*b, i| b.* = @truncate(i); f = .{ .len = 0, .interrupt_at = 2 }; try testing.expectEqual(@as(usize, 2000), try writeWith(&f, FakeFile.rpc, 7, 0, &data, 1000)); f = .{ .len = 0, .interrupt_at = 0 }; try testing.expectError(error.Interrupted, writeWith(&f, FakeFile.rpc, 7, 0, &data, 1000)); f = .{ .len = 0, .armed_at = 1 }; try testing.expectEqual(@as(usize, 1000), try writeWith(&f, FakeFile.rpc, 7, 0, &data, 1000)); } // -- in-process server test --------------------------------------------------------- test "flush grace: a Tflush the server never answers wedges the session after the grace" { var pi = try PipeInterrupt.init(7); defer pi.deinit(); var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .ignore, .on_hang_inject = &pi }; var pair = try fs.start(); defer pair.close(); const s = pair.caller; s.interrupt = pi.interface(); s.session.flush_grace_ms = 100; // The read at offset 0 hangs; the injected INTERRUPT sends a Tflush; the // server ignores it; the grace runs out. var buf: [100]u8 = undefined; try testing.expectError(error.Interrupted, s.read(1, 0, &buf)); try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst)); try testing.expect(s.session.wedged); // From here on the session is closed for business, without another // byte to the server. try testing.expectError(error.Closed, s.read(1, 10, &buf)); try testing.expectError(error.Closed, s.clunk(1)); try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst)); // The socket is still the session's to close: the server's thread ends // when `close` drops it. } test "flush grace: zero waits for the Rflush, however late" { var pi = try PipeInterrupt.init(7); defer pi.deinit(); var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush, .on_hang_inject = &pi, .read_delay_ns = 0 }; var pair = try fs.start(); defer pair.close(); const s = pair.caller; s.interrupt = pi.interface(); s.session.flush_grace_ms = 0; var buf: [100]u8 = undefined; try testing.expectError(error.Interrupted, s.read(1, 0, &buf)); try testing.expect(!s.session.wedged); // The session lives: the Rflush released the tag and reads go on. try testing.expectEqual(@as(usize, 40), try s.read(1, 10, buf[0..40])); } test "abort_on_cancel: a cancellation ends the rpc at once, sends no Tflush, wedges the session" { var pi = try PipeInterrupt.init(7); defer pi.deinit(); var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush, .on_hang_inject = &pi }; var pair = try fs.start(); defer pair.close(); const s = pair.caller; s.interrupt = pi.interface(); s.abort_on_cancel = true; var buf: [100]u8 = undefined; try testing.expectError(error.Interrupted, s.read(1, 0, &buf)); try testing.expect(s.session.wedged); try testing.expectEqual(@as(u32, 0), fs.flushes.load(.seq_cst)); try testing.expectError(error.Closed, s.read(1, 10, &buf)); } /// A tiny 9P2000 backend on a cloud9.Server: answers version/attach/walk/stat/open/ /// read/clunk/remove with canned data. Runs in its own thread over a socketpair. /// Test support only (bridge.zig's tests use it too). pub const FakeServer = struct { fd: i32, msize: u32, max_read_count: u32 = 0, file_len: usize, /// A Tread at this offset is never answered (a blocked stream read); the /// server keeps serving whatever else arrives, notably a Tflush. hang_offset: ?u64 = null, /// What a Tflush gets: the connection dropped (`.hangup`), an Rflush, /// first the Rread the flush was aimed at and then the Rflush (the /// race), or nothing at all (`.ignore`: counted, never answered, the /// read stays hung — a wedged server). on_flush: enum { hangup, rflush, reply_then_rflush, ignore } = .hangup, /// Observed by the test thread: number of Tflush seen and the last oldtag. flushes: std.atomic.Value(u32) = .init(0), flush_oldtag: std.atomic.Value(u32) = .init(0xFFFF), /// The tag of the hung Tread, for the test to compare with `flush_oldtag`. hung_tag: std.atomic.Value(u32) = .init(0xFFFF), /// How many Treads are hung right now (several callers can each park one). hung_now: std.atomic.Value(u32) = .init(0), /// A Tstat first answers every hung Tread (with data), then itself: lets a /// test release a crowd of parked readers. release_on_stat: bool = false, /// The first byte of every Rread is the fid it was read on (low byte): /// lets a test tell which fid, i.e. which open, a read went to. fid_in_data: bool = false, /// Tclunks seen. clunks: std.atomic.Value(u32) = .init(0), /// When set, the server injects that source's INTERRUPT the moment a read /// hangs, so the cancellation provably arrives while the wait is on. on_hang_inject: ?*PipeInterrupt = null, /// Delay before every Rread, so a test can be sure the client is waiting. read_delay_ns: u64 = 0, /// A Twrite is answered, and then the server hangs up (pardes answering /// a Restore and cutting every connection). hangup_after_write: bool = false, /// Set once the server has closed its end. gone: std.atomic.Value(bool) = .init(false), pub const file_qid: cloud9.Qid = .{ .type = 0, .version = 3, .path = 0x1234 }; pub const dir_qid: cloud9.Qid = .{ .type = cloud9.qtdir, .version = 1, .path = 0x1 }; pub fn run(fs: *FakeServer) void { fs.loop() catch |e| std.debug.print("fake server: {s}\n", .{@errorName(e)}); _ = linux.close(fs.fd); fs.gone.store(true, .release); } fn loop(fs: *FakeServer) !void { const gpa = testing.allocator; const in = try gpa.alloc(u8, fs.msize); defer gpa.free(in); const out = try gpa.alloc(u8, fs.msize * 2); defer gpa.free(out); var srv: cloud9.Server = .init(.{ .in = in, .out = out }); var tmp: [4096]u8 = undefined; var data: [8192]u8 = undefined; const Hung = struct { tag: u16, fid: u32, offset: u64, count: u32 }; var hung: [max_tags + 1]?Hung = @splat(null); while (true) { while (try srv.receive()) |req| { const tag = req.tag; switch (req.msg) { .tversion => |m| try srv.negotiate(m.msize, m.version), .tattach => try srv.reply(tag, .{ .rattach = .{ .qid = dir_qid } }), .twalk => |m| { var wq: [cloud9.max_welem]cloud9.Qid = @splat(dir_qid); var n: u16 = 0; for (m.wname[0..m.nwname]) |name| { if (std.mem.eql(u8, name, "file")) { wq[n] = file_qid; } else if (std.mem.eql(u8, name, "dir")) { wq[n] = dir_qid; } else break; n += 1; } if (n == 0 and m.nwname != 0) { try srv.reply(tag, .{ .rerror = .{ .ename = "file does not exist" } }); } else { try srv.reply(tag, .{ .rwalk = .{ .nwqid = n, .wqid = wq } }); } }, .tstat => { if (fs.release_on_stat) for (&hung) |*slot| if (slot.*) |h| { try srv.reply(h.tag, .{ .rread = .{ .data = fs.readData(&data, h.fid, h.offset, h.count) } }); slot.* = null; _ = fs.hung_now.fetchSub(1, .seq_cst); }; try srv.reply(tag, .{ .rstat = .{ .stat = .{ .type = 0, .dev = 0, .qid = file_qid, .mode = 0o644, .atime = 1, .mtime = 2, .length = fs.file_len, .name = "file", .uid = "u", .gid = "g", .muid = "u", } } }); }, .topen => |m| try srv.reply(tag, .{ .ropen = .{ .qid = file_qid, .iounit = if (m.mode == cloud9.owrite) 700 else 0 } }), .tread => |m| { fs.max_read_count = @max(fs.max_read_count, m.count); if (fs.hang_offset != null and fs.hang_offset.? == m.offset) { for (&hung) |*slot| if (slot.* == null) { slot.* = .{ .tag = tag, .fid = m.fid, .offset = m.offset, .count = m.count }; break; }; fs.hung_tag.store(tag, .seq_cst); _ = fs.hung_now.fetchAdd(1, .seq_cst); if (fs.on_hang_inject) |p| try p.inject(p.unique); } else { if (fs.read_delay_ns != 0) { const ts: linux.timespec = .{ .sec = @intCast(fs.read_delay_ns / std.time.ns_per_s), .nsec = @intCast(fs.read_delay_ns % std.time.ns_per_s) }; _ = linux.nanosleep(&ts, null); } try srv.reply(tag, .{ .rread = .{ .data = fs.readData(&data, m.fid, m.offset, m.count) } }); } }, .twrite => |m| { try srv.reply(tag, .{ .rwrite = .{ .count = @intCast(m.data.len) } }); if (fs.hangup_after_write) { while (srv.output().len != 0) { const o = srv.output(); const rc = linux.write(fs.fd, o.ptr, o.len); if (linux.errno(rc) != .SUCCESS) return error.Write; srv.wrote(rc); } return; } }, .tclunk => { _ = fs.clunks.fetchAdd(1, .seq_cst); try srv.reply(tag, .rclunk); }, .tremove => try srv.reply(tag, .{ .rerror = .{ .ename = "permission denied" } }), .twstat => try srv.reply(tag, .rwstat), .tflush => |m| { fs.flush_oldtag.store(m.oldtag, .seq_cst); _ = fs.flushes.fetchAdd(1, .seq_cst); switch (fs.on_flush) { // The test's "hang up now" signal. .hangup => return, .ignore => {}, .rflush => { for (&hung) |*slot| if (slot.*) |h| if (h.tag == m.oldtag) { slot.* = null; _ = fs.hung_now.fetchSub(1, .seq_cst); }; try srv.reply(tag, .rflush); }, .reply_then_rflush => { for (&hung) |*slot| if (slot.*) |h| if (h.tag == m.oldtag) { try srv.reply(h.tag, .{ .rread = .{ .data = fs.readData(&data, h.fid, h.offset, h.count) } }); slot.* = null; _ = fs.hung_now.fetchSub(1, .seq_cst); }; try srv.reply(tag, .rflush); }, } }, else => try srv.reply(tag, .{ .rerror = .{ .ename = "not supported" } }), } srv.release(); } while (srv.output().len != 0) { const o = srv.output(); const rc = linux.write(fs.fd, o.ptr, o.len); if (linux.errno(rc) != .SUCCESS) return error.Write; srv.wrote(rc); } const rc = linux.read(fs.fd, &tmp, tmp.len); if (linux.errno(rc) != .SUCCESS) return error.Read; if (rc == 0) return; if (srv.push(tmp[0..rc]) != rc) return error.Overflow; } } /// What a Tread on `fid` gets: `fill`, stamped with the fid under `fid_in_data`. fn readData(fs: *const FakeServer, data: []u8, fid: u32, offset: u64, count: u32) []const u8 { const got = fs.fill(data, offset, count); if (fs.fid_in_data and got.len != 0) data[0] = @truncate(fid); return got; } /// File contents: byte i == i & 0xff, `file_len` bytes long. fn fill(fs: *const FakeServer, data: []u8, offset: u64, count: u32) []const u8 { var n: usize = 0; if (offset < fs.file_len) n = @min(@as(usize, count), fs.file_len - @as(usize, @intCast(offset))); n = @min(n, data.len); for (data[0..n], 0..) |*b, i| b.* = @truncate(offset + i); return data[0..n]; } /// A connected session (fid 0 attached, fid 1 walked to "file" and opened /// for reading) plus the server thread; `close` when done. pub const Pair = struct { server: *FakeServer, session: *Session, /// The test thread's caller; `newCaller` makes more. caller: *Caller, thread: std.Thread, pub fn newCaller(p: *Pair) !*Caller { const c = try testing.allocator.create(Caller); errdefer testing.allocator.destroy(c); c.* = try Caller.init(p.session); return c; } pub fn freeCaller(c: *Caller) void { c.deinit(); testing.allocator.destroy(c); } pub fn close(p: *Pair) void { freeCaller(p.caller); p.session.deinit(); testing.allocator.destroy(p.session); p.thread.join(); } }; pub fn start(fs: *FakeServer) !Pair { var fds: [2]i32 = undefined; if (linux.errno(linux.socketpair(linux.AF.UNIX, linux.SOCK.STREAM | linux.SOCK.CLOEXEC, 0, &fds)) != .SUCCESS) return error.Io; fs.fd = fds[1]; const th = try std.Thread.spawn(.{}, FakeServer.run, .{fs}); const s = try testing.allocator.create(Session); errdefer testing.allocator.destroy(s); s.* = try Session.connect(testing.allocator, .{ .fd = fds[0] }, fs.msize); errdefer s.deinit(); var pair: Pair = .{ .server = fs, .session = s, .caller = undefined, .thread = th }; pair.caller = try pair.newCaller(); errdefer Pair.freeCaller(pair.caller); const c = pair.caller; _ = try c.attach(0, "me", ""); const fid = s.allocFid(); _ = try c.walk(0, fid, &.{"file"}); _ = try c.open(fid, cloud9.oread); return pair; } }; /// A fake interrupt source for the rpc wait loop: a SOCK_SEQPACKET pair stands /// in for the FUSE descriptor (one datagram per request, like /dev/fuse /// delivers one request per read). Mirrors the bridge's rules: an INTERRUPT for /// `unique` arms and cancels, anything else is consumed and ignored. pub const PipeInterrupt = struct { read_end: i32, write_end: i32, unique: u64, armed_flag: bool = false, /// Requests consumed that were not the matching INTERRUPT. ignored: usize = 0, const fuse_interrupt_opcode: u32 = 36; pub fn init(unique: u64) !PipeInterrupt { var fds: [2]i32 = undefined; const flags = linux.SOCK.SEQPACKET | linux.SOCK.CLOEXEC | linux.SOCK.NONBLOCK; if (linux.errno(linux.socketpair(linux.AF.UNIX, flags, 0, &fds)) != .SUCCESS) return error.Io; return .{ .read_end = fds[0], .write_end = fds[1], .unique = unique }; } pub fn deinit(p: *PipeInterrupt) void { _ = linux.close(p.read_end); _ = linux.close(p.write_end); } pub fn interface(p: *PipeInterrupt) Interrupt { return .{ .ctx = p, .watch = watch, .onReadable = onReadable, .armed = armed }; } /// Writes a FUSE_INTERRUPT request (InHeader + InterruptIn) naming `target`. pub fn inject(p: *PipeInterrupt, target: u64) !void { var wire: [48]u8 = undefined; std.mem.writeInt(u32, wire[0..4], 48, .little); // len std.mem.writeInt(u32, wire[4..8], fuse_interrupt_opcode, .little); // opcode std.mem.writeInt(u64, wire[8..16], 0x8000_0000_0000_0001, .little); // the interrupt's own unique @memset(wire[16..40], 0); // nodeid, uid, gid, pid, extlen, padding std.mem.writeInt(u64, wire[40..48], target, .little); // InterruptIn.unique if (linux.write(p.write_end, &wire, wire.len) != wire.len) return error.Io; } fn watch(ctx: *anyopaque) i32 { const p: *PipeInterrupt = @ptrCast(@alignCast(ctx)); return p.read_end; } fn onReadable(ctx: *anyopaque) Session.Error!bool { const p: *PipeInterrupt = @ptrCast(@alignCast(ctx)); var buf: [4096]u8 = undefined; const rc = linux.read(p.read_end, &buf, buf.len); if (linux.errno(rc) != .SUCCESS or rc != 48) return error.Io; const opcode = std.mem.readInt(u32, buf[4..8], .little); const target = std.mem.readInt(u64, buf[40..48], .little); if (opcode == fuse_interrupt_opcode and target == p.unique) { p.armed_flag = true; return true; } p.ignored += 1; return false; } fn armed(ctx: *anyopaque) bool { const p: *PipeInterrupt = @ptrCast(@alignCast(ctx)); return p.armed_flag; } }; test "rpc wait loop: INTERRUPT → Tflush → Rflush → error.Interrupted; session still usable" { var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush }; var pair = try fs.start(); defer pair.close(); const s = pair.caller; var pi = try PipeInterrupt.init(77); defer pi.deinit(); s.interrupt = pi.interface(); defer s.interrupt = null; // An INTERRUPT for some other request is consumed and ignored: the wait // goes on, and the reply (a read past the hang offset) arrives normally. var buf: [100]u8 = undefined; try pi.inject(78); try testing.expectEqual(@as(usize, 40), try s.read(1, 10, &buf)); try testing.expect(!pi.armed_flag); // The read at offset 0 hangs; the INTERRUPT for our request cancels it // (the stray one above is consumed along the way if the reply beat it). try pi.inject(77); try testing.expectError(error.Interrupted, s.read(1, 0, &buf)); try testing.expect(pi.armed_flag); try testing.expectEqual(@as(usize, 1), pi.ignored); try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst)); try testing.expectEqual(fs.hung_tag.load(.seq_cst), fs.flush_oldtag.load(.seq_cst)); // Both tags are free again: further rpcs work. const st = try s.stat(1); try testing.expectEqual(@as(u64, 50), st.length); try testing.expectEqual(@as(usize, 40), try s.read(1, 10, &buf)); try testing.expectEqual(@as(usize, 0), s.session.client.pending()); } test "rpc wait loop: the reply beats the Rflush → data returned, stray Rflush swallowed" { var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .reply_then_rflush }; var pair = try fs.start(); defer pair.close(); const s = pair.caller; var pi = try PipeInterrupt.init(5); defer pi.deinit(); s.interrupt = pi.interface(); defer s.interrupt = null; var buf: [40]u8 = undefined; try pi.inject(5); try testing.expectEqual(@as(usize, 30), try s.read(1, 0, buf[0..30])); for (buf[0..30], 0..) |b, i| try testing.expectEqual(@as(u8, @truncate(i)), b); try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst)); try testing.expectEqual(fs.hung_tag.load(.seq_cst), fs.flush_oldtag.load(.seq_cst)); // The Rflush is still in flight (or already buffered): the next rpcs must // step over it, and afterwards nothing is pending in the client. pi.armed_flag = false; const st = try s.stat(1); try testing.expectEqual(@as(u64, 50), st.length); try testing.expectEqual(@as(usize, 40), try s.read(1, 10, &buf)); try testing.expectEqual(@as(usize, 0), s.session.client.pending()); } test "rpc wait loop: a read interrupted after some data is a short read" { // Chunks of maxRead = 1013 (msize 1024); the third chunk (offset 2026) hangs // and the server fires the INTERRUPT at that moment. var pi = try PipeInterrupt.init(9); defer pi.deinit(); var fs: FakeServer = .{ .fd = -1, .msize = 1024, .file_len = 5000, .hang_offset = 2026, .on_flush = .rflush, .on_hang_inject = &pi }; var pair = try fs.start(); defer pair.close(); const s = pair.caller; s.interrupt = pi.interface(); defer s.interrupt = null; var buf: [4000]u8 = undefined; try testing.expectEqual(@as(usize, 2026), try s.read(1, 0, &buf)); for (buf[0..2026], 0..) |b, i| try testing.expectEqual(@as(u8, @truncate(i)), b); try testing.expect(pi.armed_flag); try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst)); try testing.expectEqual(@as(usize, 0), s.session.client.pending()); // An INTERRUPT already waiting when the read starts: the first chunk's // reply races the flush and wins, the armed flag then stops the loop. // (The delayed reply makes sure the INTERRUPT is seen first: with the // reply already readable the wait takes it and the flush falls on the // second chunk instead, which on a loaded machine happened.) pi.armed_flag = false; fs.read_delay_ns = 20 * std.time.ns_per_ms; try pi.inject(9); try testing.expectEqual(@as(usize, 1013), try s.read(1, 0, &buf)); // The stray Rflush is consumed by the next call. _ = try s.stat(1); try testing.expectEqual(@as(usize, 0), s.session.client.pending()); } test "connectWatched: a silent server cannot pin the handshake past stop_fd" { // A server that accepts and then never answers: the version handshake has // nothing to read. With a readable stop_fd the connect must come back with // error.Stopped instead of blocking in readSocket (the fd is blocking), and // the caller's descriptor must survive: `Address.fd` is not ours to close. var sv: [2]i32 = undefined; try testing.expectEqual(linux.E.SUCCESS, linux.errno(linux.socketpair(linux.AF.UNIX, linux.SOCK.STREAM | linux.SOCK.CLOEXEC, 0, &sv))); defer _ = linux.close(sv[0]); defer _ = linux.close(sv[1]); var p: [2]i32 = undefined; try testing.expectEqual(linux.E.SUCCESS, linux.errno(linux.pipe2(&p, .{ .CLOEXEC = true, .NONBLOCK = true }))); defer _ = linux.close(p[0]); defer _ = linux.close(p[1]); try testing.expectEqual(@as(usize, 1), linux.write(p[1], "x", 1)); const Probe = struct { const Self = @This(); done: std.atomic.Value(bool) = .init(false), stopped: std.atomic.Value(bool) = .init(false), fd_open: std.atomic.Value(bool) = .init(false), fn run(w: *Self, client: i32, stop: i32) void { if (Session.connectWatched(testing.allocator, .{ .fd = client }, 8192, stop)) |session| { var s = session; s.deinit(); } else |e| w.stopped.store(e == error.Stopped, .release); w.fd_open.store(linux.errno(linux.fcntl(client, linux.F.GETFD, 0)) == .SUCCESS, .release); w.done.store(true, .release); } }; var w: Probe = .{}; const th = try std.Thread.spawn(.{}, Probe.run, .{ &w, sv[0], p[0] }); var waited_ms: usize = 0; while (!w.done.load(.acquire) and waited_ms < 3000) : (waited_ms += 10) { const ts: linux.timespec = .{ .sec = 0, .nsec = 10 * std.time.ns_per_ms }; _ = linux.nanosleep(&ts, null); } try testing.expect(w.done.load(.acquire)); try testing.expect(w.stopped.load(.acquire)); try testing.expect(w.fd_open.load(.acquire)); th.join(); } test "session against an in-process cloud9.Server" { var fds: [2]i32 = undefined; try testing.expectEqual(linux.E.SUCCESS, linux.errno(linux.socketpair(linux.AF.UNIX, linux.SOCK.STREAM | linux.SOCK.CLOEXEC, 0, &fds))); var fs: FakeServer = .{ .fd = fds[1], .msize = 8192, .file_len = 20_000 }; const th = try std.Thread.spawn(.{}, FakeServer.run, .{&fs}); var session = try Session.connect(testing.allocator, .{ .fd = fds[0] }, 8192); defer { session.deinit(); th.join(); } try testing.expectEqual(@as(u32, 8192), session.msize); var caller = try Caller.init(&session); defer caller.deinit(); const s = &caller; const root = try s.attach(0, "me", ""); try testing.expectEqual(FakeServer.dir_qid.path, root.path); // Plain rpc + stat borrowing the input buffer. const fid = session.allocFid(); const w = try s.walk(0, fid, &.{"file"}); try testing.expectEqual(@as(u16, 1), w.nwqid); try testing.expectEqual(FakeServer.file_qid.path, w.wqid[0].path); const st = try s.stat(fid); try testing.expectEqualStrings("file", st.name); try testing.expectEqual(@as(u64, 20_000), st.length); // Chunked read: 20000 bytes at maxRead = msize - 11 = 8181 per chunk. _ = try s.open(fid, cloud9.oread); const buf = try testing.allocator.alloc(u8, 30_000); defer testing.allocator.free(buf); const n = try s.read(fid, 0, buf); try testing.expectEqual(@as(usize, 20_000), n); for (buf[0..n], 0..) |b, i| try testing.expectEqual(@as(u8, @truncate(i)), b); try testing.expectEqual(@as(u32, 8181), fs.max_read_count); try testing.expectEqual(@as(usize, 0), try s.read(fid, 20_000, buf)); // iounit from open bounds the chunk. const wfid = try s.clone(fid); _ = try s.open(wfid, cloud9.owrite); fs.max_read_count = 0; _ = try s.read(wfid, 0, buf[0..3000]); try testing.expectEqual(@as(u32, 700), fs.max_read_count); try testing.expectEqual(@as(usize, 3000), try s.write(wfid, 0, buf[0..3000])); // Partial walk → error.Nine with a "not exist" ename → ENOENT. const pfid = session.allocFid(); try testing.expectError(error.Nine, s.walk(0, pfid, &.{ "dir", "nope" })); try testing.expectEqual(linux.E.NOENT, s.errno()); try testing.expectEqualStrings("file does not exist", s.ename[0..s.ename_len]); session.freeFid(pfid); // Server Rerror → error.Nine, ename copied, fid freed by remove even on error. try testing.expectError(error.Nine, s.remove(wfid)); try testing.expectEqual(linux.E.ACCES, s.errno()); try testing.expectEqual(wfid, session.allocFid()); // recycled session.freeFid(wfid); // Unsupported op → "not supported" → ENOTSUP; a plain wstat succeeds. try testing.expectError(error.Nine, s.rpc(.{ .auth = .{ .afid = 5, .uname = "me" } })); try testing.expectEqual(linux.E.OPNOTSUPP, s.errno()); try s.wstat(fid, dontcare); try s.clunk(fid); try testing.expectEqual(fid, session.allocFid()); session.freeFid(fid); // A clone bound to a fid that then fails to walk must release the fid. const before = session.next_fid; const cfid = session.allocFid(); session.freeFid(cfid); try testing.expectError(error.Nine, s.walk(0, cfid, &.{"nope"})); try testing.expectEqual(before, session.next_fid); // The server hanging up makes the pending rpc fail with error.Closed. try testing.expectError(error.Closed, s.rpc(.{ .flush = .{ .oldtag = 0 } })); } // -- multiplexing ---------------------------------------------------------------------- /// Polls `cond` every millisecond for up to three seconds. fn eventually(cond: anytype) bool { for (0..3000) |_| { if (cond.check()) return true; const ts: linux.timespec = .{ .sec = 0, .nsec = std.time.ns_per_ms }; _ = linux.nanosleep(&ts, null); } return cond.check(); } const HungAtLeast = struct { fs: *FakeServer, n: u32, fn check(h: HungAtLeast) bool { return h.fs.hung_now.load(.seq_cst) >= h.n; } }; /// A caller reading `file` at offset 0 on its own thread (the offset the /// FakeServer parks), recording how the read ended. const ParkedRead = struct { caller: *Caller, thread: std.Thread = undefined, result: Session.Error!usize = 0, done: std.atomic.Value(bool) = .init(false), fn start(p: *ParkedRead) !void { p.thread = try std.Thread.spawn(.{}, run, .{p}); } fn run(p: *ParkedRead) void { var buf: [100]u8 = undefined; p.result = p.caller.read(1, 0, &buf); p.done.store(true, .release); } }; test "multiplexing: a parked read holds up only its own caller; its interrupt flushes exactly its tag" { var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush }; var pair = try fs.start(); defer pair.close(); var pi = try PipeInterrupt.init(21); defer pi.deinit(); var parked: ParkedRead = .{ .caller = try pair.newCaller() }; defer FakeServer.Pair.freeCaller(parked.caller); parked.caller.interrupt = pi.interface(); try parked.start(); try testing.expect(eventually(HungAtLeast{ .fs = &fs, .n = 1 })); // The other caller's requests go through while the read is parked; which // of the two threads holds the reading baton at any moment does not matter. const s = pair.caller; var buf: [100]u8 = undefined; const t0 = nowMs(); for (0..50) |_| { try testing.expectEqual(@as(usize, 40), try s.read(1, 10, buf[0..40])); const st = try s.stat(1); try testing.expectEqualStrings("file", st.name); } try testing.expect(nowMs() - t0 < 2000); try testing.expect(!parked.done.load(.acquire)); try testing.expectEqual(@as(u32, 0), fs.flushes.load(.seq_cst)); // Interrupting the parked read sends one Tflush, for the parked tag, and // the Rflush ends that read (and only it) in error.Interrupted. const hung_tag = fs.hung_tag.load(.seq_cst); try pi.inject(21); parked.thread.join(); try testing.expectError(error.Interrupted, parked.result); try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst)); try testing.expectEqual(hung_tag, fs.flush_oldtag.load(.seq_cst)); // Both callers carry on; nothing is left pending. try testing.expectEqual(@as(usize, 40), try s.read(1, 10, buf[0..40])); try testing.expectEqual(@as(usize, 40), try parked.caller.read(1, 10, buf[0..40])); try testing.expectEqual(@as(usize, 0), s.session.client.pending()); } test "a server that answers and then hangs up: the reply is read, though another call's send failed first" { // pardes answers a Restore's write and then cuts every connection. The // write's Rwrite is in the socket when another caller's request finds // the socket closed; that failed send must not lose the reply. const ign = linux.Sigaction{ .handler = .{ .handler = linux.SIG.IGN }, .mask = linux.sigemptyset(), .flags = 0 }; std.posix.sigaction(.PIPE, &ign, null); var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hangup_after_write = true }; var pair = try fs.start(); defer pair.close(); const s = pair.session; const io = lockIo(); const writer = try pair.newCaller(); defer FakeServer.Pair.freeCaller(writer); const wfid = try pair.caller.clone(1); _ = try pair.caller.open(wfid, cloud9.owrite); // The write goes out, and nobody reads its answer yet. var w: Waiter = .{ .caller = writer }; s.mutex.lockUncancelable(io); s.transmit(&w, .{ .write = .{ .fid = wfid, .offset = 0, .data = "Restore x\n" } }) catch |e| { s.leave(&w); s.mutex.unlock(io); return e; }; s.mutex.unlock(io); // The server answers and hangs up; then another call's send fails. const Gone = struct { fs: *FakeServer, fn check(g: @This()) bool { return g.fs.gone.load(.acquire); } }; try testing.expect(eventually(Gone{ .fs = &fs })); try testing.expectError(error.Closed, pair.caller.stat(0)); // The write's reply was already in: it is read, not failed. s.mutex.lockUncancelable(io); const got = s.await(&w, .reply); s.leave(&w); s.mutex.unlock(io); try got; try testing.expectEqual(@as(u32, "Restore x\n".len), w.result.write); // And then the session is over. try testing.expectError(error.Closed, pair.caller.stat(0)); } test "multiplexing: with every tag parked, a call waits for a tag instead of failing" { // max_tags reads park (the client's whole request window); the next call // finds no tag. Interrupting one parked read sends its Tflush on the tag // the client keeps for flushes; the Rflush frees a tag, the waiting stat // goes out, and (release_on_stat) the server answers the other reads. var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush, .release_on_stat = true }; var pair = try fs.start(); defer pair.close(); // The fake's cloud9.Server holds 64 requests; a window of 32 fills the // same way the whole one does. const window = 32; pair.session.window = window; var pi = try PipeInterrupt.init(33); defer pi.deinit(); var reads: [window]ParkedRead = undefined; for (&reads) |*r| r.* = .{ .caller = try pair.newCaller() }; defer for (&reads) |*r| FakeServer.Pair.freeCaller(r.caller); reads[0].caller.interrupt = pi.interface(); for (&reads) |*r| try r.start(); try testing.expect(eventually(HungAtLeast{ .fs = &fs, .n = window })); const Stat = struct { caller: *Caller, result: Session.Error!u64 = 0, done: std.atomic.Value(bool) = .init(false), fn run(st: *@This()) void { st.result = if (st.caller.stat(1)) |x| x.length else |e| e; st.done.store(true, .release); } }; var stat: Stat = .{ .caller = pair.caller }; const th = try std.Thread.spawn(.{}, Stat.run, .{&stat}); // It is waiting for a tag, not failed. const ts: linux.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms }; _ = linux.nanosleep(&ts, null); try testing.expect(!stat.done.load(.acquire)); try pi.inject(33); th.join(); try testing.expectEqual(@as(u64, 50), try stat.result); for (&reads) |*r| r.thread.join(); try testing.expectError(error.Interrupted, reads[0].result); for (reads[1..]) |r| try testing.expectEqual(@as(usize, 50), try r.result); try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst)); try testing.expectEqual(@as(usize, 0), pair.session.client.pending()); }