diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-01 10:39:51 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-01 10:39:51 -0300 |
| commit | 34834165e2b54dd6ac26babd1e9d0443f1f53ee4 (patch) | |
| tree | 5dfa88e756e403f58f26ffb4d6f21b50972a9fbb /9ns/src/ns.zig | |
| parent | c4c75b7e4ecc9c664d1f778e5e7c5305dff65b12 (diff) | |
| download | cloud9-34834165e2b54dd6ac26babd1e9d0443f1f53ee4.tar.gz cloud9-34834165e2b54dd6ac26babd1e9d0443f1f53ee4.zip | |
9ns: under --mntgen the program also gets NINE_MNTGEN=1, so it can tell the registry from a mounted session
$NINE_MOUNT was the mountpoint either way: under --mntgen the registry,
each session at $NINE_MOUNT/<service>/<name>; under --unix the session
itself. A program working out its own session's directory (pardes's
$PARDES_MOUNT for its panes) could not tell which without statting the
mount. NINE_MNTGEN=1 now says so; an inherited one is dropped, so a 9ns
nested in a registry's program is not taken for one.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to '9ns/src/ns.zig')
| -rw-r--r-- | 9ns/src/ns.zig | 63 |
1 files changed, 46 insertions, 17 deletions
diff --git a/9ns/src/ns.zig b/9ns/src/ns.zig index b4342e5..db41eda 100644 --- a/9ns/src/ns.zig +++ b/9ns/src/ns.zig @@ -18,8 +18,13 @@ const E = linux.E; pub const Spawn = struct { /// argv[0] is PATH-searched unless it contains '/'. argv: []const []const u8, - /// Inherited environment; `NINE_MOUNT` is added or replaced. + /// Inherited environment; `NINE_MOUNT` is added or replaced, and + /// `NINE_MNTGEN` set when `mntgen`, removed when not. envp: [*:null]const ?[*:0]const u8, + /// The mountpoint is --mntgen's registry, each session under it at + /// `<service>/<name>` (pardes: `$NINE_MOUNT/pardes/<name>`), not one + /// session itself: the program is told so by `NINE_MNTGEN=1`. + mntgen: bool = false, /// Absolute mountpoint (see `resolveMountpoint`). mountpoint: []const u8, uid: u32, @@ -162,29 +167,45 @@ pub fn findInPath(gpa: Allocator, envp: [*:null]const ?[*:0]const u8, name: []co return error.FileNotFound; } -/// New envp block: every entry of `envp` except `NINE_MOUNT=...`, -/// followed by `NINE_MOUNT=<mountpoint>`. -fn buildEnvp(gpa: Allocator, envp: [*:null]const ?[*:0]const u8, mountpoint: []const u8) ![:null]?[*:0]const u8 { +/// The entry `NINE_MNTGEN=1`, static: never freed. +const mntgen_entry: [*:0]const u8 = "NINE_MNTGEN=1"; + +/// New envp block: every entry of `envp` except `NINE_MOUNT=...` and +/// `NINE_MNTGEN=...`, then `NINE_MOUNT=<mountpoint>` (allocated, last but +/// one when mntgen), then `NINE_MNTGEN=1` when `mntgen`. Free it with +/// `freeEnvp`. +fn buildEnvp(gpa: Allocator, envp: [*:null]const ?[*:0]const u8, mountpoint: []const u8, mntgen: bool) ![:null]?[*:0]const u8 { const key = "NINE_MOUNT="; + const gen_key = "NINE_MNTGEN="; var keep: usize = 0; var i: usize = 0; while (envp[i]) |entry| : (i += 1) { - if (!std.mem.startsWith(u8, std.mem.span(entry), key)) keep += 1; + const e = std.mem.span(entry); + if (!std.mem.startsWith(u8, e, key) and !std.mem.startsWith(u8, e, gen_key)) keep += 1; } - const out = try gpa.allocSentinel(?[*:0]const u8, keep + 1, null); + const out = try gpa.allocSentinel(?[*:0]const u8, keep + 1 + @intFromBool(mntgen), null); errdefer gpa.free(out); var j: usize = 0; i = 0; while (envp[i]) |entry| : (i += 1) { - if (std.mem.startsWith(u8, std.mem.span(entry), key)) continue; + const e = std.mem.span(entry); + if (std.mem.startsWith(u8, e, key) or std.mem.startsWith(u8, e, gen_key)) continue; out[j] = entry; j += 1; } const mount_entry = try std.fmt.allocPrintSentinel(gpa, key ++ "{s}", .{mountpoint}, 0); out[j] = mount_entry.ptr; + if (mntgen) out[j + 1] = mntgen_entry; return out; } +/// Frees what buildEnvp allocated: the block and its NINE_MOUNT entry. +fn freeEnvp(gpa: Allocator, envp: [:null]?[*:0]const u8) void { + const mount_at = if (envp.len > 0 and envp[envp.len - 1].? == mntgen_entry) envp.len - 2 else envp.len - 1; + gpa.free(std.mem.span(envp[mount_at].?)); + gpa.free(envp); +} + fn buildArgv(gpa: Allocator, argv: []const []const u8) ![:null]?[*:0]const u8 { const out = try gpa.allocSentinel(?[*:0]const u8, argv.len, null); for (argv, 0..) |a, i| out[i] = (try gpa.dupeZ(u8, a)).ptr; @@ -501,11 +522,8 @@ pub fn spawn(gpa: Allocator, s: Spawn) !Child { for (argv) |a| gpa.free(std.mem.span(a.?)); gpa.free(argv); } - const envp = try buildEnvp(gpa, s.envp, s.mountpoint); - defer { - gpa.free(std.mem.span(envp[envp.len - 1].?)); // the NINE_MOUNT entry we created - gpa.free(envp); - } + const envp = try buildEnvp(gpa, s.envp, s.mountpoint, s.mntgen); + defer freeEnvp(gpa, envp); const candidates = try pathCandidates(gpa, s.envp, s.argv[0]); defer { for (candidates) |c| gpa.free(c); @@ -1102,14 +1120,25 @@ test "findInPath finds sh" { try testing.expectError(error.FileNotFound, findInPath(gpa, &env, "definitely-not-a-program-9ns")); } +test "buildEnvp sets NINE_MNTGEN only for --mntgen's registry, never inherited" { + const gpa = testing.allocator; + const env = [_:null]?[*:0]const u8{ "A=1", "NINE_MNTGEN=1", "NINE_MOUNT=/old" }; + const single = try buildEnvp(gpa, &env, "/m", false); + defer freeEnvp(gpa, single); + try testing.expect(getenv(single.ptr, "NINE_MNTGEN") == null); + try testing.expectEqualStrings("/m", getenv(single.ptr, "NINE_MOUNT").?); + const registry = try buildEnvp(gpa, &env, "/mnt/9p", true); + defer freeEnvp(gpa, registry); + try testing.expectEqualStrings("1", getenv(registry.ptr, "NINE_MNTGEN").?); + try testing.expectEqualStrings("/mnt/9p", getenv(registry.ptr, "NINE_MOUNT").?); + try testing.expectEqual(@as(usize, 3), registry.len); +} + test "buildEnvp replaces NINE_MOUNT" { const gpa = testing.allocator; const env = [_:null]?[*:0]const u8{ "A=1", "NINE_MOUNT=/old", "B=2" }; - const out = try buildEnvp(gpa, &env, "/mnt/9p"); - defer { - gpa.free(std.mem.span(out[out.len - 1].?)); - gpa.free(out); - } + const out = try buildEnvp(gpa, &env, "/mnt/9p", false); + defer freeEnvp(gpa, out); try testing.expectEqual(@as(usize, 3), out.len); try testing.expectEqualStrings("A=1", std.mem.span(out[0].?)); try testing.expectEqualStrings("B=2", std.mem.span(out[1].?)); |
