summaryrefslogtreecommitdiff
path: root/src/9p_io.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-23 11:00:44 -0300
committerGabriel Schneider <[email protected]>2026-10-23 17:10:58 -0300
commit80314064f6e81c8828340e010202b446f0e1f36d (patch)
treeaa56e86238ac9397055f601db924868bb6511538 /src/9p_io.zig
parenta53a1aafb9804e943bdcd720c2bb688ecb90a888 (diff)
downloadpardes-80314064f6e81c8828340e010202b446f0e1f36d.tar.gz
pardes-80314064f6e81c8828340e010202b446f0e1f36d.zip
Audit (misc): std mkdir -p and JSON escaping, a 9P late failure's errno from ctl.failureErrno, dead switches and dumpHex out
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/9p_io.zig')
-rw-r--r--src/9p_io.zig112
1 files changed, 46 insertions, 66 deletions
diff --git a/src/9p_io.zig b/src/9p_io.zig
index a825475f..a089a7cf 100644
--- a/src/9p_io.zig
+++ b/src/9p_io.zig
@@ -11,10 +11,7 @@ const quic = if (quic_enabled) @import("9p_quic.zig") else struct {};
const log = std.log.scoped(.ninep);
-pub const darwin = switch (builtin.os.tag) {
- .macos, .ios, .tvos, .watchos, .visionos => true,
- else => false,
-};
+pub const darwin = builtin.os.tag.isDarwin();
pub const supported = builtin.os.tag == .linux or darwin;
pub const sun_path_len = @typeInfo(@FieldType(libc.sockaddr.un, "path")).array.len;
@@ -47,15 +44,8 @@ pub fn statNoFollow(path: [:0]const u8) ?FileFacts {
pub fn ensureSocketDir(dir: [:0]const u8) bool {
if (dir.len == 0) return false;
- var partial: [sun_path_len:0]u8 = undefined;
- @memcpy(partial[0 .. dir.len + 1], dir[0 .. dir.len + 1]);
- for (1..dir.len) |i| {
- if (dir[i] != '/') continue;
- partial[i] = 0;
- _ = libc.mkdir(partial[0..i :0], 0o700);
- partial[i] = '/';
- }
- _ = libc.mkdir(dir, 0o700);
+ // A failure shows in the stat below.
+ _ = std.Io.Dir.cwd().createDirPathStatus(std.Io.Threaded.global_single_threaded.io(), dir, .fromMode(0o700)) catch {};
const stat = statNoFollow(dir) orelse return false;
return stat.mode & 0o170000 == 0o040000 and stat.uid == libc.getuid() and stat.mode & 0o077 == 0;
}
@@ -394,11 +384,11 @@ pub const Listener = struct {
// settle in the same wait is told to this write too.
if (l.core == core and core.fs.late_failure_len != 0) {
const late = core.fs.late_failure[0..core.fs.late_failure_len];
- // What is not there (`no such directory`) is ENOENT, as a
- // builtin's failure saying so is (ctl.failureErrno).
- const errno = if (std.mem.indexOf(u8, late, "no such") != null or std.mem.indexOf(u8, late, "not found") != null)
- pardes.ctlfs.E.NOENT
- else if (std.mem.indexOf(u8, late, "no space") != null) pardes.ctlfs.E.NOSPC else if (std.mem.indexOf(u8, late, "permission denied") != null) pardes.ctlfs.E.PERM else if (std.mem.indexOf(u8, late, "invalid") != null) pardes.ctlfs.E.INVAL else pardes.ctlfs.E.IO;
+ // The errno a builtin's failure in the same words has; what
+ // that leaves EIO and says `no space` (a full disk under a
+ // Save) is ENOSPC.
+ const said = @import("ninep/ctl.zig").failureErrno(late);
+ const errno = if (said == pardes.ctlfs.E.IO and std.mem.indexOf(u8, late, "no space") != null) pardes.ctlfs.E.NOSPC else said;
const failed = pardes.ctlfs.failText(req.tag, errno, late);
// Its err record says it (the path in it), the one record: it
// was said with no msg while this waited (fs.write_waits).
@@ -1017,21 +1007,11 @@ fn onFatalSignal(s: std.posix.SIG) callconv(.c) void {
pub fn sweepDeadSockets(io: std.Io, dir: [:0]const u8) void {
if (comptime !supported) return;
var names: [8192]u8 = undefined;
- var staged: usize = 0;
- {
- const d = std.Io.Dir.openDirAbsolute(io, dir, .{ .iterate = true }) catch return;
- defer d.close(io);
- var read_buf: [std.Io.Dir.Iterator.reader_buffer_len]u8 align(@alignOf(usize)) = undefined;
- var reader: std.Io.Dir.Reader = .init(d, &read_buf);
- while (true) {
- const listed = (reader.next(io) catch break) orelse break;
- if (deadSocketPid(listed.name) == null) continue;
- if (staged + 1 + listed.name.len > names.len) break;
- names[staged] = @intCast(listed.name.len);
- @memcpy(names[staged + 1 ..][0..listed.name.len], listed.name);
- staged += 1 + listed.name.len;
+ const staged = stageNames(io, dir, &names, struct {
+ fn keep(name: []const u8) bool {
+ return deadSocketPid(name) != null;
}
- }
+ }.keep);
var i: usize = 0;
while (i < staged) {
const name = names[i + 1 ..][0..names[i]];
@@ -1048,6 +1028,28 @@ pub fn sweepDeadSockets(io: std.Io, dir: [:0]const u8) void {
}
}
+/// The names in `dir` that `keep` takes (all with none), into `names` as a length byte and
+/// the name each, until it is full; the bytes staged. They are staged
+/// before anything is unlinked, so a sweep never asks a directory to keep
+/// reading while it is being edited.
+fn stageNames(io: std.Io, dir: [:0]const u8, names: []u8, comptime keep: ?fn ([]const u8) bool) usize {
+ const d = std.Io.Dir.openDirAbsolute(io, dir, .{ .iterate = true }) catch return 0;
+ defer d.close(io);
+ var read_buf: [std.Io.Dir.Iterator.reader_buffer_len]u8 align(@alignOf(usize)) = undefined;
+ var reader: std.Io.Dir.Reader = .init(d, &read_buf);
+ var staged: usize = 0;
+ while (true) {
+ const listed = (reader.next(io) catch break) orelse break;
+ if (listed.name.len == 0 or listed.name.len > 255) continue;
+ if (keep) |takes| if (!takes(listed.name)) continue;
+ if (staged + 1 + listed.name.len > names.len) break;
+ names[staged] = @intCast(listed.name.len);
+ @memcpy(names[staged + 1 ..][0..listed.name.len], listed.name);
+ staged += 1 + listed.name.len;
+ }
+ return staged;
+}
+
/// The pid a `pardes-9p-<pid>.sock` name carries, or null for any other.
fn deadSocketPid(name: []const u8) ?std.c.pid_t {
if (!std.mem.startsWith(u8, name, prefix) or !std.mem.endsWith(u8, name, ".sock")) return null;
@@ -1279,24 +1281,8 @@ fn ourSocket(target: []const u8, sockets: []const u8) bool {
fn sweepRegistry(io: std.Io, svc: [:0]const u8, sockets: []const u8) void {
if (comptime !supported) return;
- // The names are staged before anything is unlinked, so the sweep
- // never asks a directory to keep reading while it is being edited.
var names: [4096]u8 = undefined;
- var staged: usize = 0;
- {
- const dir = std.Io.Dir.openDirAbsolute(io, svc, .{ .iterate = true }) catch return;
- defer dir.close(io);
- var read_buf: [std.Io.Dir.Iterator.reader_buffer_len]u8 align(@alignOf(usize)) = undefined;
- var reader: std.Io.Dir.Reader = .init(dir, &read_buf);
- while (true) {
- const listed = (reader.next(io) catch break) orelse break;
- if (listed.name.len == 0 or listed.name.len > 255) continue;
- if (staged + 1 + listed.name.len > names.len) break;
- names[staged] = @intCast(listed.name.len);
- @memcpy(names[staged + 1 ..][0..listed.name.len], listed.name);
- staged += 1 + listed.name.len;
- }
- }
+ const staged = stageNames(io, svc, &names, null);
var reaped: usize = 0;
var i: usize = 0;
@@ -2179,10 +2165,8 @@ pub const Client = struct {
if (comptime !supported) return error.Unsupported;
var names: [max_depth][]const u8 = undefined;
const n = try elements(path, &names);
- var sock_buf: [sun_path_len]u8 = undefined;
- const sock = try resolve(&sock_buf, dial);
var remote: RemoteError = .{};
- return fetchBytes(gpa, sock, names[0..n], &remote, null, display_path, @min(max_bytes, limits.max_file_bytes)) catch |err| {
+ return fetchBytes(gpa, dial, names[0..n], &remote, null, display_path, @min(max_bytes, limits.max_file_bytes)) catch |err| {
if (err == error.Remote) said = remote;
return err;
};
@@ -2209,10 +2193,8 @@ pub const Client = struct {
if (comptime !supported) return error.Unsupported;
var names: [max_depth][]const u8 = undefined;
const n = try elements(path, &names);
- var sock_buf: [sun_path_len]u8 = undefined;
- const sock = try resolve(&sock_buf, dial);
var remote: RemoteError = .{};
- const result = fetchBytes(gpa, sock, names[0..n], &remote, bytes, path, limits.max_file_bytes) catch |err| {
+ const result = fetchBytes(gpa, dial, names[0..n], &remote, bytes, path, limits.max_file_bytes) catch |err| {
if (err == error.Remote) said = remote;
return err;
};
@@ -2470,7 +2452,7 @@ pub const Client = struct {
fn fetchBytes(
gpa: std.mem.Allocator,
- sock: Dial,
+ dial: []const u8,
names: []const []const u8,
remote: *RemoteError,
write_bytes: ?[]const u8,
@@ -2478,7 +2460,7 @@ pub const Client = struct {
read_limit: usize,
) ![]u8 {
if (comptime !supported) return Error.Dial;
- const s = try startSession(gpa, sock, display_path);
+ const s = try startSession(gpa, dial, display_path);
defer endSession(gpa, s);
var out: std.Io.Writer.Allocating = .init(gpa);
errdefer out.deinit();
@@ -2486,8 +2468,10 @@ pub const Client = struct {
return out.toOwnedSlice();
}
- /// A connected session whose requests have the usual deadline.
- fn startSession(gpa: std.mem.Allocator, sock: Dial, display_path: []const u8) !*Session {
+ /// A connected session at `dial` whose requests have the usual deadline.
+ fn startSession(gpa: std.mem.Allocator, dial: []const u8, display_path: []const u8) !*Session {
+ var sock_buf: [sun_path_len]u8 = undefined;
+ const sock = try resolve(&sock_buf, dial);
const deadline = nowMs() +| budget_ms;
const s = try gpa.create(Session);
s.* = .{ .fd = -1, .deadline = deadline, .display_path = display_path };
@@ -2518,11 +2502,9 @@ pub const Client = struct {
if (comptime !supported) return error.Unsupported;
var names: [max_depth][]const u8 = undefined;
const n = try elements(path, &names);
- var sock_buf: [sun_path_len]u8 = undefined;
- const sock = try resolve(&sock_buf, dial);
var remote: RemoteError = .{};
errdefer said = remote;
- const s = try startSession(gpa, sock, path);
+ const s = try startSession(gpa, dial, path);
defer endSession(gpa, s);
const fid = (try walkTo(s, names[0..n], &remote)).fid;
_ = try s.ask(.{ .open = .{ .fid = fid, .mode = ninep.ordwr } }, &remote);
@@ -2562,10 +2544,8 @@ pub const Client = struct {
if (comptime !supported) return error.Unsupported;
var names: [max_depth][]const u8 = undefined;
const n = try elements(path, &names);
- var sock_buf: [sun_path_len]u8 = undefined;
- const sock = try resolve(&sock_buf, dial);
var remote: RemoteError = .{};
- const s = try startSession(gpa, sock, path);
+ const s = try startSession(gpa, dial, path);
defer endSession(gpa, s);
const fid = (try walkTo(s, names[0..n], &remote)).fid;
_ = try s.ask(.{ .open = .{ .fid = fid, .mode = ninep.ordwr } }, &remote);
@@ -2642,7 +2622,7 @@ pub const Client = struct {
const before = nowMs();
try testing.expectError(
Error.Dial,
- fetchBytes(testing.allocator, .{ .unix = "/tmp/pardes-9p-no-such-socket.sock" }, names[0..n], &remote, null, "/pane/1/body", limits.max_file_bytes),
+ fetchBytes(testing.allocator, "/tmp/pardes-9p-no-such-socket.sock", names[0..n], &remote, null, "/pane/1/body", limits.max_file_bytes),
);
try testing.expect(nowMs() - before < budget_ms);
}