summaryrefslogtreecommitdiff
path: root/9ns/src
diff options
context:
space:
mode:
Diffstat (limited to '9ns/src')
-rw-r--r--9ns/src/main.zig4
-rw-r--r--9ns/src/ns.zig63
2 files changed, 49 insertions, 18 deletions
diff --git a/9ns/src/main.zig b/9ns/src/main.zig
index eac0a5e..86763a7 100644
--- a/9ns/src/main.zig
+++ b/9ns/src/main.zig
@@ -38,7 +38,8 @@ const usage_text =
\\ --no-direct-io let the kernel cache file pages (trusts stat length)
\\ --debug trace FUSE and 9P operations on stderr
\\ --help, --version
- \\PROGRAM defaults to $SHELL (else /bin/sh). The mountpoint is exported as $NINE_MOUNT.
+ \\PROGRAM defaults to $SHELL (else /bin/sh). The mountpoint is exported as $NINE_MOUNT,
+ \\and under --mntgen NINE_MNTGEN=1 says it is the registry, a session at $NINE_MOUNT/<service>/<name>.
\\Default name: --unix PATH -> basename of PATH without .sock/.9p/.socket;
\\--tcp IP:PORT -> tcp-IP-PORT (':' becomes '-'); --spawn CMD -> basename of its
\\first word; --fd N -> fdN; 9p when nothing usable comes out of that.
@@ -391,6 +392,7 @@ fn runMntgen(init: std.process.Init, envp: [*:null]const ?[*:0]const u8, cfg: Co
.uid = uid,
.gid = gid,
.max_read = bridge.max_write,
+ .mntgen = true,
}) catch return own_failure;
bridge.serveMntgen(gpa, child.fuse_fd, stop_fd, .{
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].?));