diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Fsys.zig | 49 | ||||
| -rw-r--r-- | src/verbs.zig | 4 |
2 files changed, 47 insertions, 6 deletions
diff --git a/src/Fsys.zig b/src/Fsys.zig index 224087b..745ebfd 100644 --- a/src/Fsys.zig +++ b/src/Fsys.zig @@ -11,6 +11,10 @@ //! fid is let go of without waiting for the answer: an open, a stat or a //! remove costs one round trip. The buffers are in the struct //! (2 × `msize`): an `Fsys` stays where it is while it is used. +//! +//! A call waits `timeout` for its answer (30 s from `init`), so a server +//! that accepts and then says nothing is `error.Timeout`, not a caller +//! blocked for ever. const std = @import("std"); const Io = std.Io; const c9 = @import("root.zig"); @@ -37,8 +41,15 @@ pub const Error = error{ /// The server broke the protocol, or a request could not be made /// (a name with a slash or longer than a message). Botch, + /// No answer within `timeout`. The answer may come yet, under a tag + /// the next call does not expect: `close` the connection. + Timeout, }; +/// What `init` sets `timeout` to: pardes's client waits this long for a +/// write that runs a command. +pub const default_timeout: Io.Timeout = .{ .duration = .{ .raw = .fromSeconds(30), .clock = .awake } }; + io: Io, stream: Io.net.Stream, cl: Client, @@ -54,6 +65,10 @@ walk_tag: u16, walk_asked: usize, words: [fs.errmax]u8, words_len: usize, +/// How long a call waits for its answer, from when it is made: `init` +/// sets `default_timeout`, before it speaks; `.none` waits for ever, as +/// following an event file wants. +timeout: Io.Timeout, /// Speaks 9P on `stream`, which is this one's from here (`close`), on a /// refusal too: the version, then an attach as `uname` to the tree's root. @@ -66,6 +81,7 @@ pub fn init(f: *Fsys, io: Io, stream: Io.net.Stream, uname: []const u8) Error!vo f.walk_tag = wire.notag; f.walk_asked = 0; f.words_len = 0; + f.timeout = default_timeout; errdefer f.close(); const v = (try f.rpc(.{ .version = .{ .msize = msize } })).version; if (!std.mem.eql(u8, v.version, "9P2000")) return f.refuse("the server does not speak 9P2000"); @@ -288,8 +304,9 @@ fn rpc(f: *Fsys, req: Client.Request) Error!Client.Result { return error.Botch; }; try f.ship(); + const deadline = f.timeout.toDeadline(f.io); while (true) { - const done = try f.next(); + const done = try f.next(deadline); if (f.walk_tag != wire.notag and done.tag == f.walk_tag) { f.walk_tag = wire.notag; switch (done.result) { @@ -314,9 +331,9 @@ fn rpc(f: *Fsys, req: Client.Request) Error!Client.Result { } } -/// The next answer, read from the socket as it takes; its slices are -/// good until the next. -fn next(f: *Fsys) Error!Client.Done { +/// The next answer, read from the socket until `deadline`; its slices +/// are good until the next. +fn next(f: *Fsys, deadline: Io.Timeout) Error!Client.Done { while (true) { if (f.cl.take()) |done| return done; if (f.cl.dead) return error.Botch; @@ -325,7 +342,10 @@ fn next(f: *Fsys) Error!Client.Done { // (what `push` would copy them to). var room = [1][]u8{f.cl.in[f.cl.in_len..]}; if (room[0].len == 0) return error.Botch; - const n = f.stream.read(f.io, &room) catch return error.Hangup; + const n = ((f.io.operateTimeout(.{ .net_read = .{ .socket_handle = f.stream.socket.handle, .data = &room } }, deadline) catch |err| switch (err) { + error.Timeout => return error.Timeout, + else => return error.Hangup, + }).net_read catch return error.Hangup).data_len; if (n == 0) return error.Hangup; f.cl.in_len += n; } @@ -517,6 +537,25 @@ test "fsys: a server that goes away mid-read is a hangup" { try feeder.await(testing.io); } +test "fsys: a server that says nothing is a timeout, not a caller held for ever" { + const t = try testing.allocator.create(TestTree); + defer testing.allocator.destroy(t); + try t.start(); + defer t.stop(); + const f = try dialed(t, .unix); + defer hangUp(f); + try testing.expectEqual(default_timeout, f.timeout); + // `event`'s reads are held until fed, and nothing feeds this one. + f.timeout = .{ .duration = .{ .raw = .fromMilliseconds(100), .clock = .awake } }; + var store: [64]u8 = undefined; + var out: Io.Writer = .fixed(&store); + const began = Io.Clock.Timestamp.now(testing.io, .awake); + try testing.expectError(error.Timeout, f.readFile("event", &out)); + const waited = began.durationTo(Io.Clock.Timestamp.now(testing.io, .awake)).raw.toMilliseconds(); + // poll(2) counts whole milliseconds, and may wake one early. + try testing.expect(waited >= 90 and waited < 5000); +} + test "fsys: create, write, remove" { const t = try testing.allocator.create(TestTree); defer testing.allocator.destroy(t); diff --git a/src/verbs.zig b/src/verbs.zig index c9174d3..722a0c2 100644 --- a/src/verbs.zig +++ b/src/verbs.zig @@ -31,7 +31,7 @@ pub const usage = \\ fs read PATH the file, to its end (a held read streams) \\ fs write PATH [WORDS...] the words and a newline, else stdin, as > would \\ fs ask PATH WORDS... write the words and read the answer on the same open - \\ fs follow PATH [WORDS...] ask, or read: what comes, as it comes + \\ fs follow PATH [WORDS...] ask, or read: what comes, as it comes, for as long \\ fs stat PATH mode length uid gid mtime name \\ fs help [PATH] what the tree's README says of PATH \\ fs complete [PREFIX] the paths that go on from PREFIX, for a shell @@ -49,6 +49,7 @@ pub fn run(f: *Fsys, args: []const []const u8, in: *Io.Reader, out: *Io.Writer, error.Refused => err.print("fs: {s}: {s}\n", .{ path, f.said() }), error.Hangup => err.print("fs: {s}: the server hung up\n", .{path}), error.Botch => err.print("fs: {s}: protocol botch\n", .{path}), + error.Timeout => err.print("fs: {s}: no answer in {d} ms; fs follow waits for ever\n", .{ path, if (f.timeout.toDurationFromNow(f.io)) |d| d.raw.toMilliseconds() else 0 }), error.Usage => err.writeAll(usage), error.TooLong => err.print("fs: {s}: the words are longer than a message\n", .{path}), // Our own stdin or stdout: nothing to say it on. @@ -106,6 +107,7 @@ fn runVerb(f: *Fsys, args: []const []const u8, in: *Io.Reader, out: *Io.Writer, }, .follow => { if (args.len < 2) return error.Usage; + f.timeout = .none; if (words.len == 0) try f.readFile(path, out) else try f.ask(path, try line(&line_buf, words), out); }, .stat => { |
