summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/9p_io.zig71
-rw-r--r--src/main.zig9
2 files changed, 68 insertions, 12 deletions
diff --git a/src/9p_io.zig b/src/9p_io.zig
index 75f55296..0ba5cba3 100644
--- a/src/9p_io.zig
+++ b/src/9p_io.zig
@@ -23,7 +23,7 @@ pub fn setCloexec(fd: c_int) void {
_ = libc.fcntl(fd, libc.F.SETFD, @as(c_int, 1));
}
-pub fn socketDir(buf: *[sun_path_len:0]u8) ?[:0]const u8 {
+pub fn socketDir(buf: []u8) ?[:0]const u8 {
if (libc.getenv("XDG_RUNTIME_DIR")) |path|
return std.fmt.bufPrintSentinel(buf, "{s}", .{std.mem.span(path)}, 0) catch null;
const home = libc.getenv("HOME") orelse return null;
@@ -864,6 +864,18 @@ pub fn socketPath(buf: *[sun_path_len]u8, dir: []const u8, name: []const u8) ?[:
/// (`pardes.turn`) and must give it up while it waits.
pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: []const u8, fallback: []const u8, tcp_dial: ?[]const u8) !*Listener {
if (comptime !supported) return error.Unsupported;
+ const entry_name = if (named.len != 0) named else fallback;
+ // Checked before anything is made: the turn's hooks point at the
+ // listener from here on, and a path too long for sun_path used to free
+ // it with them still set, so the editor's next rest called into freed
+ // memory. The name the socket is first bound at, `<path>.<pid>` (it is
+ // renamed into place once it listens), must fit too.
+ refusal = "";
+ if (tooLong(entry_name, &refusal_buf)) |words| {
+ log.warn("{s}", .{words});
+ refusal = words;
+ return error.NameTooLong;
+ }
var dir_buf: [sun_path_len:0]u8 = undefined;
const dir = socketDir(&dir_buf) orelse {
log.warn("no runtime directory for the socket", .{});
@@ -872,17 +884,9 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [
if (!ensureSocketDir(dir)) return error.NoRuntimeDir;
// What dead editors left behind goes first, never a live one's.
sweepDeadSockets(io, dir);
- const entry_name = if (named.len != 0) named else fallback;
- // Checked before anything is made: the turn's hooks point at the
- // listener from here on, and a path too long for sun_path used to free
- // it with them still set, so the editor's next rest called into freed
- // memory. The name the socket is first bound at, `<path>.<pid>` (it is
- // renamed into place once it listens), must fit too.
- var path_check: [sun_path_len]u8 = undefined;
var tmp_buf: [sun_path_len]u8 = undefined;
- const check = socketPath(&path_check, dir, entry_name);
- if (check == null or (std.fmt.bufPrintSentinel(&tmp_buf, "{s}.{d}", .{ check.?, libc.getpid() }, 0) catch null) == null) {
- log.warn("no socket for {s}: the path is too long for a unix socket", .{entry_name});
+ if (std.mem.indexOfAny(u8, entry_name, "/\x00") != null or entry_name.len == 0) {
+ log.warn("no socket for {s}: not one path component", .{entry_name});
return error.NameTooLong;
}
const l = try gpa.create(Listener);
@@ -953,6 +957,25 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [
return l;
}
+/// `tooLong`'s words, kept for `start`'s message.
+var refusal_buf: [160]u8 = undefined;
+var refusal: []const u8 = "";
+
+/// Why a session named `name` gets no socket, in words in `buf` (null: it
+/// fits, or there is no directory, said where it is looked for): the name
+/// it is first bound at, `<socketDir>/pardes-9p-<name>.sock.<pid>` and its
+/// 0, must fit sun_path. Asked at a detached start too, whose stderr is
+/// the only place anyone reads it.
+pub fn tooLong(name: []const u8, buf: []u8) ?[]const u8 {
+ var dir_buf: [4096]u8 = undefined;
+ const dir = socketDir(&dir_buf) orelse return null;
+ var digits: [16]u8 = undefined;
+ const pid = std.fmt.bufPrint(&digits, "{d}", .{libc.getpid()}) catch unreachable; // a pid fits 16
+ const n = dir.len + "/".len + prefix.len + name.len + ".sock.".len + pid.len + 1;
+ if (n <= sun_path_len) return null;
+ return std.fmt.bufPrint(buf, "9p: the socket path is {d} bytes, {d} is the most: set XDG_RUNTIME_DIR shorter", .{ n, sun_path_len }) catch unreachable; // two numbers fit 160
+}
+
/// Whether a live session listens under `name` already: a second
/// `--detach=<name>` is refused before anything is made.
pub fn taken(name: []const u8) bool {
@@ -1252,6 +1275,30 @@ const TempRuntime = struct {
}
};
+test "a runtime directory that makes the socket's path pass sun_path is refused in words, the editor running on" {
+ if (comptime !supported) return error.SkipZigTest;
+ const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true });
+ defer p.deinit();
+ var rt: TempRuntime = .{};
+ try rt.init("words");
+ defer rt.deinit();
+ var buf: [160]u8 = undefined;
+ try testing.expect(tooLong("x", &buf) == null);
+ // A directory of 91 bytes (the ledger's was 83, a pid's digits from the
+ // edge): with a 1-byte name and a pid the socket's path passes sun_path.
+ var long_buf: [128:0]u8 = undefined;
+ const long = try std.fmt.bufPrintSentinel(&long_buf, "{s}/{s}", .{ std.mem.sliceTo(&rt.directory, 0), &@as([66]u8, @splat('d')) }, 0);
+ try testing.expectEqual(@as(usize, 91), long.len);
+ try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", long, 1));
+ const words = tooLong("x", &buf) orelse return error.NotRefused;
+ try testing.expect(std.mem.startsWith(u8, words, "9p: the socket path is "));
+ try testing.expect(std.mem.endsWith(u8, words, " bytes, 108 is the most: set XDG_RUNTIME_DIR shorter"));
+ // The session's own words, where its pane shows them; no listener.
+ try testing.expect(start(testing.io, testing.allocator, p) == null);
+ const pane = p.panes[0].?;
+ try testing.expect(std.mem.startsWith(u8, pane.msg[0..pane.msg_len], "9p: the socket path is "));
+}
+
test "a socket path too long for sun_path is no listener, and leaves the editor's turn as it was" {
if (comptime !supported) return error.SkipZigTest;
const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true });
@@ -1712,7 +1759,7 @@ pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes) ?*Listene
// unreachable: a u32 is at most 10 digits
const fallback = std.fmt.bufPrint(&name, "{d}", .{@as(u32, @intCast(libc.getpid()))}) catch unreachable;
return listen(io, gpa, core, core.opts.ninep_name, fallback, core.opts.ninep_tcp) catch {
- core.reportError(0, "9p listener", error.ListenFailed);
+ if (refusal.len > 0) core.setMessage(0, refusal) else core.reportError(0, "9p listener", error.ListenFailed);
return null;
};
}
diff --git a/src/main.zig b/src/main.zig
index 7f6d1a05..b6f57873 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -743,6 +743,15 @@ fn nativeMain(std_init: std.process.Init) !void {
// socket holds already (a plain session is named by its pid too).
if (name.len != 0 and !resuming and ninep_io.taken(name)) die(init.io, "a session named {s} is already running", .{name});
if (opts.ninep_name.len == 0) opts.ninep_name = name;
+ // Said where it was started (stderr, as the log says it): no pane
+ // anyone sees would show it. The session runs on, with no 9P.
+ var pid_buf: [16]u8 = undefined;
+ var words_buf: [160]u8 = undefined;
+ const entry = if (opts.ninep_name.len != 0) opts.ninep_name else std.fmt.bufPrint(&pid_buf, "{d}", .{std.c.getpid()}) catch unreachable; // a pid fits 16
+ if (ninep_io.tooLong(entry, &words_buf)) |words| {
+ var line_buf: [200]u8 = undefined;
+ std.Io.File.stderr().writeStreamingAll(init.io, std.fmt.bufPrint(&line_buf, "pardes: {s}\n", .{words}) catch unreachable) catch {}; // 160 fits 200
+ }
// Nobody's size yet: one roomy enough for a script's panes; `size`
// sets another, and an attached terminal or window its own.
opts.cols = 160;