summaryrefslogtreecommitdiff
path: root/src/linux
diff options
context:
space:
mode:
Diffstat (limited to 'src/linux')
-rw-r--r--src/linux/v9fs.zig36
1 files changed, 13 insertions, 23 deletions
diff --git a/src/linux/v9fs.zig b/src/linux/v9fs.zig
index 0d1e2e33..47be298b 100644
--- a/src/linux/v9fs.zig
+++ b/src/linux/v9fs.zig
@@ -5,23 +5,13 @@ const builtin = @import("builtin");
extern "c" fn unshare(flags: c_int) c_int;
extern "c" fn mount(source: ?[*:0]const u8, target: [*:0]const u8, filesystemtype: ?[*:0]const u8, flags: c_ulong, data: ?*const anyopaque) c_int;
-extern "c" fn getuid() c_uint;
-extern "c" fn geteuid() c_uint;
extern "c" fn setgroups(size: usize, list: ?[*]const c_uint) c_int;
extern "c" fn initgroups(user: [*:0]const u8, group: c_uint) c_int;
-extern "c" fn setresgid(real: c_uint, effective: c_uint, saved: c_uint) c_int;
-extern "c" fn setresuid(real: c_uint, effective: c_uint, saved: c_uint) c_int;
extern "c" fn execv(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int;
extern "c" fn execvp(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int;
-extern "c" fn readlink(path: [*:0]const u8, buf: [*]u8, size: usize) isize;
extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8;
-extern "c" fn rmdir(path: [*:0]const u8) c_int;
extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int;
extern "c" fn unsetenv(name: [*:0]const u8) c_int;
-extern "c" fn fork() c_int;
-extern "c" fn waitpid(pid: c_int, status: *c_int, flags: c_int) c_int;
-extern "c" fn kill(pid: c_int, sig: c_int) c_int;
-extern "c" fn _exit(status: c_int) noreturn;
pub const executable = "pardes-v9fs";
@@ -51,7 +41,7 @@ fn quote(writer: *std.Io.Writer, value: []const u8) !void {
}
fn selfPath(buf: []u8) ![:0]u8 {
- const n = readlink("/proc/self/exe", buf.ptr, buf.len - 1);
+ const n = std.c.readlink("/proc/self/exe", buf.ptr, buf.len - 1);
if (n < 0 or n >= buf.len - 1) return error.ExecutablePathUnavailable;
const len: usize = @intCast(n);
buf[len] = 0;
@@ -85,22 +75,22 @@ fn usage() void {
, .{});
}
-var sudo_pid: std.atomic.Value(c_int) = .init(-1);
+var sudo_pid: std.atomic.Value(std.c.pid_t) = .init(-1);
fn forwardSignal(sig: std.posix.SIG) callconv(.c) void {
const pid = sudo_pid.load(.monotonic);
- if (pid > 0) _ = kill(pid, @intCast(@backingInt(sig)));
+ if (pid > 0) _ = std.c.kill(pid, sig);
}
fn launch(arena: std.mem.Allocator, args: []const [:0]const u8) !u8 {
if (args.len < 5 or !std.mem.eql(u8, args[3], "--")) return error.InvalidArguments;
- if (geteuid() == 0 or getuid() != geteuid()) return error.UnprivilegedLaunchRequired;
+ if (std.c.geteuid() == 0 or std.c.getuid() != std.c.geteuid()) return error.UnprivilegedLaunchRequired;
if (!std.fs.path.isAbsolute(args[2]) or !std.fs.path.isAbsolute(args[4])) return error.AbsolutePathRequired;
var own: [4096]u8 = undefined;
const helper = try selfPath(&own);
var target = "/tmp/pardes-v9fs-XXXXXX".*;
if (mkdtemp(&target) == null) return error.MountDirectoryFailed;
- defer _ = rmdir(&target);
+ defer _ = std.c.rmdir(&target);
try check(setenv("PARDES_MOUNT", &target, 1), "export mount path");
inline for (.{ "PATH", "HOME", "USER", "LOGNAME", "SHELL" }) |name| {
const saved = "PARDES_V9FS_" ++ name;
@@ -108,14 +98,14 @@ fn launch(arena: std.mem.Allocator, args: []const [:0]const u8) !u8 {
try check(setenv(saved, value, 1), "preserve shell environment");
} else _ = unsetenv(saved);
}
- const uid = try std.fmt.allocPrintSentinel(arena, "{d}", .{getuid()}, 0);
+ const uid = try std.fmt.allocPrintSentinel(arena, "{d}", .{std.c.getuid()}, 0);
const gid = try std.fmt.allocPrintSentinel(arena, "{d}", .{std.c.getgid()}, 0);
const prefix = [_]?[*:0]const u8{ "sudo", "-E", "--", helper.ptr, args[2].ptr, &target, uid.ptr, gid.ptr, "--" };
const argv = try arena.allocSentinel(?[*:0]const u8, prefix.len + args.len - 4, null);
@memcpy(argv[0..prefix.len], &prefix);
for (args[4..], prefix.len..) |arg, i| argv[i] = arg.ptr;
std.debug.print("Mounting Pardes at {s}\n", .{target});
- const pid = fork();
+ const pid = std.c.fork();
if (pid < 0) return error.ForkFailed;
if (pid == 0) {
// host_io's resetChildSignals, here in its own module: every signal
@@ -129,13 +119,13 @@ fn launch(arena: std.mem.Allocator, args: []const [:0]const u8) !u8 {
const none = std.posix.sigemptyset();
std.posix.sigprocmask(std.posix.SIG.SETMASK, &none, null);
_ = execvp("sudo", argv.ptr);
- _exit(127);
+ std.c._exit(127);
}
sudo_pid.store(pid, .monotonic);
const action: std.posix.Sigaction = .{ .handler = .{ .handler = forwardSignal }, .mask = std.posix.sigemptyset(), .flags = 0 };
for ([_]std.posix.SIG{ .HUP, .INT, .TERM }) |sig| std.posix.sigaction(sig, &action, null);
var status: c_int = 0;
- while (waitpid(pid, &status, 0) < 0) {
+ while (std.c.waitpid(pid, &status, 0) < 0) {
if (std.posix.errno(-1) != .INTR) return error.WaitFailed;
}
sudo_pid.store(-1, .monotonic);
@@ -176,8 +166,8 @@ pub fn main(init: std.process.Init) !void {
}
const uid = try userId(args[3]);
const gid = try userId(args[4]);
- if (getuid() != geteuid()) return error.SetuidInstallationUnsupported;
- if (geteuid() != 0) {
+ if (std.c.getuid() != std.c.geteuid()) return error.SetuidInstallationUnsupported;
+ if (std.c.geteuid() != 0) {
std.debug.print("v9fs: native 9P mounts need CAP_SYS_ADMIN in the initial user namespace; use --launch to ask sudo in this terminal.\n", .{});
return error.MountPrivilegeRequired;
}
@@ -194,8 +184,8 @@ pub fn main(init: std.process.Init) !void {
try check(setgroups(0, null), "clear supplementary groups");
const account = std.c.getpwuid(uid) orelse return error.UserAccountNotFound;
try check(initgroups(account.name orelse return error.UserAccountNotFound, gid), "restore user groups");
- try check(setresgid(gid, gid, gid), "drop group privileges");
- try check(setresuid(uid, uid, uid), "drop user privileges");
+ try check(std.c.setresgid(gid, gid, gid), "drop group privileges");
+ try check(std.c.setresuid(uid, uid, uid), "drop user privileges");
// sudo can replace identity variables and PATH even with -E. Restore
// those values only after dropping privileges.
inline for (.{ "PATH", "HOME", "USER", "LOGNAME", "SHELL" }) |name| {