From 79311cd89746738a20be359c054dc2a7c84577db Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 00:19:33 -0300 Subject: 9p_io: exportPaneEnv and exportSessionEnv are one exportEnv, telling a child this process's own socket, which a listener's path is from its listen to its deinit; host_io's two calls name it, and forkShell's listener argument, read by nothing now, goes from the frontends' calls with them; the env test sets the socket rather than building a Listener Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/9p_io.zig | 40 ++++++++++++++++++---------------------- src/host_io.zig | 12 +++++++----- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/9p_io.zig b/src/9p_io.zig index 78a08275..8291a37f 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -1731,19 +1731,14 @@ pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes) ?*Listene /// to reach it, and a `--nested` session exports them too — its socket stays /// open to scripts and to mounted shells — while withholding `PARDES_PID`, /// which is the whole of what `--nested` means. -pub fn exportPaneEnv(listener: ?*const Listener, serial: u32, adopts: bool) void { - exportEnv(if (listener) |l| l.path() else null, serial, adopts); -} - -/// exportPaneEnv for a command run off the loop (an Edit's filter), with -/// this process's own socket: what a pane's command would be given. -pub fn exportSessionEnv(serial: u32, adopts: bool) void { - exportEnv(if (own_socket_len > 0) own_socket[0..own_socket_len] else null, serial, adopts); -} - -/// Told to children through host_io.ChildEnv, never set into this process's -/// own environment (see ChildEnv.exportVar for why). -fn exportEnv(socket: ?[]const u8, serial: u32, adopts: bool) void { +/// +/// The socket is this process's own (`listen` keeps it, its listener's +/// path, until `deinit`), for a pane's shell and for a command run off the +/// loop (an Edit's filter) alike. Told to children through +/// host_io.ChildEnv, never set into this process's own environment (see +/// ChildEnv.exportVar for why). +pub fn exportEnv(serial: u32, adopts: bool) void { + const socket: ?[]const u8 = if (own_socket_len > 0) own_socket[0..own_socket_len] else null; const ChildEnv = @import("host_io.zig").ChildEnv; var pid_buf: [16]u8 = undefined; ChildEnv.exportVar("PARDES_PID", if (adopts) std.fmt.bufPrint(&pid_buf, "{d}", .{@as(u32, @intCast(libc.getpid()))}) catch null else null); @@ -1825,35 +1820,36 @@ test "9P shell environment states being inside pardes apart from how to reach it _ = @import("host_io.zig").testUnsetenv("HOME"); try testing.expect(socketDir(&dir) == null); - const listener = try testing.allocator.create(Listener); - defer testing.allocator.destroy(listener); - listener.* = .{ .io = testing.io, .core = undefined }; // only its path is read + // The socket a listener keeps while it lives. const path = "/tmp/pardes-example.sock"; - @memcpy(listener.path_buf[0..path.len], path); - listener.path_len = path.len; + @memcpy(own_socket[0..path.len], path); + own_socket[path.len] = 0; + own_socket_len = path.len; + defer own_socket_len = 0; var own: [16]u8 = undefined; const own_pid = try std.fmt.bufPrint(&own, "{d}", .{@as(u32, @intCast(libc.getpid()))}); // A `--nested` session: reachable for scripts and mounts, but nobody's // parent, so a pardes started in one of its shells runs a session of // its own instead of handing its argument over. - exportPaneEnv(listener, 7, false); + exportEnv(7, false); try testing.expectEqualStrings(path, @import("host_io.zig").ChildEnv.exported("PARDES_9P").?); try testing.expectEqualStrings("7", @import("host_io.zig").ChildEnv.exported("PARDES_PANE").?); try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PID") == null); - exportPaneEnv(listener, 8, true); + exportEnv(8, true); try testing.expectEqualStrings("8", @import("host_io.zig").ChildEnv.exported("PARDES_PANE").?); try testing.expectEqualStrings(own_pid, @import("host_io.zig").ChildEnv.exported("PARDES_PID").?); // A session whose listener never came up is still the session this shell // is inside: the pid stands on its own, and only the address is missing. - exportPaneEnv(null, 0, true); + own_socket_len = 0; + exportEnv(0, true); try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_9P") == null); try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PANE") == null); try testing.expectEqualStrings(own_pid, @import("host_io.zig").ChildEnv.exported("PARDES_PID").?); - exportPaneEnv(null, 0, false); + exportEnv(0, false); try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_9P") == null); try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PANE") == null); try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PID") == null); diff --git a/src/host_io.zig b/src/host_io.zig index 8db03f95..97e6cbbc 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -1705,7 +1705,7 @@ pub const ChildEnv = struct { "TERM_PROGRAM=" ++ pardes.config.child_term_program, }; - /// What this editor tells its children of itself (exportPaneEnv, `$winid`), + /// What this editor tells its children of itself (exportEnv, `$winid`), /// kept here rather than set into this process's environment: std's Io read /// that block at startup, and setenv/unsetenv rewrite it in place under it /// (a spawn then scans a null inside it). `build` puts each exported one in @@ -1945,11 +1945,11 @@ pub fn testUnsetenv(name: [*:0]const u8) c_int { return unsetenv(name); } -/// The environment a pane's command is given (exportPaneEnv, `$winid`, +/// The environment a pane's command is given (exportEnv, `$winid`, /// ChildEnv), as `NAME=value` lines in `arena`, for a command that is not a /// pane's: an Edit's filters, run from pane `serial`. pub fn commandEnv(arena: std.mem.Allocator, serial: u32, adopts: bool) ![]const []const u8 { - ninep_io.exportSessionEnv(serial, adopts); + ninep_io.exportEnv(serial, adopts); exportWinid(serial); var slots: ChildEnv.Slots = undefined; const envp = ChildEnv.build(&slots, .command) orelse return error.EnvironmentTooLarge; @@ -2030,8 +2030,10 @@ pub fn forkShell( // The last argument is whether this session takes its children's Looks — // every session does but a `--nested` one, and a fork with no core at all // (the tests below) is nobody's parent. - ninep_io.exportPaneEnv( - fs, + // shortcut: `fs` is read by nothing now (exportEnv takes this process's + // own socket); its argument goes from the frontends' calls with them. + _ = fs; + ninep_io.exportEnv( if (core) |c| (if (c.panes[pane]) |pn| pn.serial else 0) else 0, if (core) |c| !c.opts.nested else false, ); -- cgit v1.3