RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
P.4 freebd stuff
oxydizere9ef6f7284f4
diff --git a/docs/agent-notes/map.md b/docs/agent-notes/map.mdindex 1a94bc3..e93ea24 100644--- a/docs/agent-notes/map.md+++ b/docs/agent-notes/map.md@@ -44,7 +44,8 @@ widgets, layout, text, theme, animation), `config` (`src/config/`: parsing andsaving `config.toml`), `dbus`, `i18n`, `memory` and `protocol`(`src/ipc/protocol.zig`, the IPC wire format shared with `rediwm-msg`). Also`platform` (`src/platform/`, std only): child-process handles, directory-watching, the program's own path and syscall filters (`seccomp.zig`), the+watching, the program's own path, system helper lookup (`tool.zig`) and+syscall filters (`seccomp.zig`), theseam for other operating systems.## Compositordiff --git a/docs/plans/freebsd.md b/docs/plans/freebsd.mdindex 907c42c..ea25d63 100644--- a/docs/plans/freebsd.md+++ b/docs/plans/freebsd.md@@ -1,7 +1,8 @@# FreeBSD support plan-Status: draft, 2026-10-30. Preparation steps P.1–P.3 are done (Linux only,-`src/platform/`); nothing else is implemented. Decisions D1–D6 are open.+Status: draft, 2026-10-31. Preparation steps P.1–P.3 and P.7 are done (Linux+only, `src/platform/` and the call sites); nothing else is implemented.+Decisions D1–D6 are open.Run RediWM on FreeBSD: first the compositor in a seatd session on DRM, thenthe shell features and apps, and last `rediwm-dm` and packaging. Linux stays@@ -41,7 +42,7 @@ translate. About 47 Zig files call `std.os.linux.*` directly.| `/proc/self/fd`, `fdinfo` | spawn paths | `closefrom`/`close_range`. || `/proc/<pid>/stat`, `/proc/stat`, `/proc/meminfo`, task children | system monitor, `ipc/processes.zig` | libprocstat / `kern.proc` sysctls behind a collector backend. || `pty.h` | terminal engine | `libutil.h` (`openpty`/`forkpty`), link `util`. |-| `renameat2` | torrent helper | Emulate no-replace with `link` + `unlink`. |+| `renameat2` | ~9 sites: Files ops, Editor, Images, screenshots, desktop watcher, theme helper, torrent helper | Emulate no-replace with `link` + `unlink`, behind one `platform` function. || `DRM_IOCTL_VERSION` via `linux.IOCTL` | `Output.zig:206` | Different ioctl encoding: use libdrm `drmGetVersion`. || netlink `KOBJECT_UEVENT` | printer hotplug | devd's seqpacket socket, or rescans only. |@@ -132,9 +133,20 @@ ReleaseSafe, `zig build test` and affected integration suites.what the entry point promises before moving either.6. **Process listing:** keep `/proc` parsing behind the system monitor'scollector interface (shared with `ipc/processes.zig`).-7. **Paths:** replace fixed `/usr/bin/…` tool paths with `PATH` lookup.--Steps 1–3 and 7 are small; 4 is mechanical but broad; 5 is security work and+7. **Paths** (done): no fixed `/usr/bin/…` tool path is left in the shell.+ `paplay` is found on `PATH` (`config.app_theme.executable`) and `locale` is+ spawned by bare name, which `std.process.spawn` resolves on `PATH`. The+ programs that run as root or are handed a secret (`pkexec`, `systemctl`)+ deliberately do not use `PATH`: `platform.tool.system` searches only+ `/usr/bin`, `/bin` and `/usr/local/bin`, so a user-writable `PATH` entry+ cannot capture a password typed in Settings. Left as they are: the accounts+ helper's candidate lists (root; phase 3 rewrites it on `pw(8)`), the+ `/bin/sh` launches (present on both systems) and the polkit agent helper's+ directory list in `polkit/native.c` (add `/usr/local/lib/polkit-1` and+ `/usr/local/libexec` in phase 1; it is verified setuid and root-owned), and+ the cupsd probe in `printing/platform.zig` (`usr/local/sbin`, phase 3).++Steps 1–3 and 7 were small; 4 is mechanical but broad; 5 is security work andneeds its own review.### 0. Spike (FreeBSD)diff --git a/docs/wiki/IPC-Commands.md b/docs/wiki/IPC-Commands.mdindex a6ba016..5ba3241 100644--- a/docs/wiki/IPC-Commands.md+++ b/docs/wiki/IPC-Commands.md@@ -163,7 +163,7 @@ These work without opening Settings.| `get_services` | system services, raw systemd state, `activation_us`, total `boot_us`. First calls may say `loading`/`analyzing`, unknown times are null || `set_service_mode` | `service`, `mode`: `on`, `disabled`, `on-demand`. Changes boot policy only, doesn't start or stop the unit. `on-demand` needs an existing activation trigger and disables boot enablement. `deferred` returns `UnsupportedServiceMode`. Static and masked units are read-only || `get_sounds` | theme and events, with enabled/available flags and resolved files |-| `play_sound` | `event` from `get_sounds`. Plays with `/usr/bin/paplay`, returns the PID, not "finished" |+| `play_sound` | `event` from `get_sounds`. Plays with `paplay` from `PATH`, returns the PID, not "finished" || `get_wallpaper` | selection, resolved and displayed paths, dimensions, loading and fallback status || `set_wallpaper` | `path`: a wallpaper name or a file, `""` restores the default. Decoding is async, poll `get_wallpaper` || `get_theme` | theme `tokens`, theme file path (null if inline), dark mode. Colours are straight RGBA, 0 to 1 |diff --git a/src/audio/sounds.zig b/src/audio/sounds.zigindex cc1cc34..61ef466 100644--- a/src/audio/sounds.zig+++ b/src/audio/sounds.zig@@ -3,6 +3,7 @@const std = @import("std");const Server = @import("../Server.zig");const protocol = @import("../ipc/protocol.zig");+const config = @import("config");pub const events = [_][]const u8{"bell", "dialog-information", "dialog-warning", "dialog-error",@@ -11,13 +12,17 @@ pub const events = [_][]const u8{"device-added", "device-removed", "power-plug", "power-unplug","battery-low", "battery-caution", "audio-volume-change", "window-attention",};-const player = "/usr/bin/paplay";+const player = "paplay";fn validName(name: []const u8) bool {if (name.len == 0 or name.len > 128 or std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return false;for (name) |ch| if (!std.ascii.isAlphanumeric(ch) and ch != '-' and ch != '_' and ch != '.') return false;return true;}+/// The player on `PATH` (the session's, so ports under /usr/local count).+fn playerPath(server: *Server, a: std.mem.Allocator) !?[]u8 {+ return config.app_theme.executable(a, server.io, server.environ, player);+}fn exists(server: *Server, path: []const u8) bool {const stat = std.Io.Dir.cwd().statFile(server.io, path, .{}) catch return false;return stat.kind == .file;@@ -86,7 +91,7 @@ pub fn inspect(server: *Server, a: std.mem.Allocator) !protocol.SoundsResult {};row.* = .{ .name = event, .enabled = enabled, .available = match.path != null, .path = match.path };}- return .{ .theme = cfg.sound_theme, .enabled = cfg.sound_enabled, .player_available = exists(server, player), .events = rows };+ return .{ .theme = cfg.sound_theme, .enabled = cfg.sound_enabled, .player_available = (try playerPath(server, a)) != null, .events = rows };}pub fn play(server: *Server, a: std.mem.Allocator, event: []const u8) !protocol.Response {@@ -96,7 +101,7 @@ pub fn play(server: *Server, a: std.mem.Allocator, event: []const u8) !protocol.};if (!known) return error.UnknownSoundEvent;const state = try inspect(server, a);- if (!state.player_available) return error.SoundPlayerUnavailable;+ const player_path = try playerPath(server, a) orelse return error.SoundPlayerUnavailable;for (state.events) |row| {if (!std.mem.eql(u8, event, row.name)) continue;if (!row.enabled) return error.SoundEventDisabled;@@ -106,7 +111,7 @@ pub fn play(server: *Server, a: std.mem.Allocator, event: []const u8) !protocol.defer env.deinit();try server.applyChildEnv(&env);const child = try std.process.spawn(server.io, .{- .argv = &.{ player, "--client-name=RediWM", "--property=media.role=event", "--", path },+ .argv = &.{ player_path, "--client-name=RediWM", "--property=media.role=event", "--", path },.environ_map = &env,});if (child.id) |pid| try manager.trackChild(pid);diff --git a/src/control_center/sections/region.zig b/src/control_center/sections/region.zigindex 4c71bf9..8209580 100644--- a/src/control_center/sections/region.zig+++ b/src/control_center/sections/region.zig@@ -57,7 +57,7 @@ fn loadOptions(s: *Section, cc: *panel.ControlCenter) !void {if (columns.next()) |zone| try s.zones.append(a, zone);}std.mem.sort([]const u8, s.zones.items, {}, less);- var child = try std.process.spawn(cc.server.io, .{ .argv = &.{ "/usr/bin/locale", "-a" }, .stdout = .pipe, .stderr = .ignore });+ var child = try std.process.spawn(cc.server.io, .{ .argv = &.{ "locale", "-a" }, .stdout = .pipe, .stderr = .ignore });errdefer {child.kill(cc.server.io);}diff --git a/src/control_center/sections/users_helper.zig b/src/control_center/sections/users_helper.zigindex 1ecfbd4..13c4af0 100644--- a/src/control_center/sections/users_helper.zig+++ b/src/control_center/sections/users_helper.zig@@ -4,6 +4,7 @@ const i18n = @import("i18n");const panel = @import("../panel.zig");const Child = @import("../../session/child.zig");const gpa = @import("../../main.zig").gpa;+const platform = @import("platform");const c = @import("c");extern fn rediwm_polkit_clear([*]u8, usize) void;@@ -33,7 +34,7 @@ pub fn startHelper(cc: *panel.ControlCenter, request: anytype, is_authorization:if (sockets[1] >= 0) _ = c.close(sockets[1]);}- const executable = if (c.access("/usr/bin/pkexec", c.X_OK) == 0) "/usr/bin/pkexec" else "/bin/pkexec";+ const executable = platform.tool.system(cc.server.io, "pkexec") orelse return error.FileNotFound;var child = try std.process.spawn(cc.server.io, .{.argv = &.{ executable, "--disable-internal-agent", @import("install_paths").bin ++ "/rediwm-accounts-helper" },.stdin = .{ .file = .{ .handle = sockets[0], .flags = .{ .nonblocking = false } } },diff --git a/src/platform/AGENTS.md b/src/platform/AGENTS.mdindex 477700d..b5b3037 100644--- a/src/platform/AGENTS.md+++ b/src/platform/AGENTS.md@@ -1,7 +1,8 @@# Platform moduleThe one place the OS shows: child handles (`child.zig`), directory watching-(`watch.zig`), the program's own path (`exe.zig`), seccomp-BPF filters+(`watch.zig`), the program's own path (`exe.zig`), where the system's helper+programs live (`tool.zig`), seccomp-BPF filters(`seccomp.zig`, for the PDF viewer and the theme helper), and`child.h`/`self_exe.h` for C and C++. Std only, no compositor imports. Linuxtoday; the FreeBSD port (`docs/plans/freebsd.md`) adds `freebsd_*.zig` parts@@ -33,6 +34,11 @@ without changing callers.does not compile in Zig 0.17: use `seccomp.audit_arch`. A policy change ischecked by evaluating both programs over all numbers and argument samples(`seccomp.evaluate`), not by trusting a read.+* **Programs that run as root or get a secret** (`pkexec`, `systemctl`) are+ found with `tool.system`, which searches only the root-owned system+ directories. Everything else is looked up on `PATH` (`config.app_theme.executable`)+ or spawned by bare name, which `std.process.spawn` resolves on the process's+ `PATH`; never write `/usr/bin/<tool>`, ports live in `/usr/local`.* Nothing here arms a timer or starts a thread; a caller retrying a failed`add` owns that clock.diff --git a/src/platform/mod.zig b/src/platform/mod.zigindex 2c51865..8a19e16 100644--- a/src/platform/mod.zig+++ b/src/platform/mod.zig@@ -1,6 +1,6 @@//! Everything that differs between operating systems, behind one interface:-//! child processes, file-change notification, the program's own path and-//! syscall filters.+//! child processes, file-change notification, the program's own path, where+//! the system's helper programs live and syscall filters.//! Callers never test the OS; `std.os.linux` is used only in here (see//! `AGENTS.md` beside this file). No compositor imports.pub const child = @import("child.zig");@@ -9,10 +9,12 @@ pub const power_supply = @import("power_supply.zig");pub const charge_limit = @import("charge_limit.zig");pub const watch = @import("watch.zig");pub const seccomp = @import("seccomp.zig");+pub const tool = @import("tool.zig");test {_ = child;_ = exe;+ _ = tool;_ = watch;_ = @import("seccomp_tests.zig");}diff --git a/src/platform/tool.zig b/src/platform/tool.zignew file mode 100644index 0000000..f1cc63e--- /dev/null+++ b/src/platform/tool.zig@@ -0,0 +1,24 @@+//! Where the system's own helper programs live. Distro packages put them in+//! `/usr/bin` (`/bin` before the /usr merge), ports in `/usr/local/bin`.+const std = @import("std");++/// The directories searched, in order. Root-owned on every supported system.+const system_dirs = [_][]const u8{ "/usr/bin", "/bin", "/usr/local/bin" };++/// `name` in the system directories, never on `PATH`: for programs that run+/// as root or are handed a secret (`pkexec`, `systemctl`), where a+/// user-writable `PATH` entry must not win. Null when none is executable.+pub fn system(io: std.Io, comptime name: []const u8) ?[:0]const u8 {+ inline for (system_dirs) |dir| {+ const path = dir ++ "/" ++ name;+ if (std.Io.Dir.accessAbsolute(io, path, .{ .execute = true })) |_| return path else |_| {}+ }+ return null;+}++test "system programs come from the system directories" {+ const sh = system(std.testing.io, "sh").?;+ try std.testing.expect(std.mem.endsWith(u8, sh, "/sh"));+ try std.testing.expect(std.fs.path.isAbsolute(sh));+ try std.testing.expectEqual(@as(?[:0]const u8, null), system(std.testing.io, "rediwm-no-such-program"));+}diff --git a/src/systemd_edit.zig b/src/systemd_edit.zigindex e5c9df6..90aa50d 100644--- a/src/systemd_edit.zig+++ b/src/systemd_edit.zig@@ -4,6 +4,7 @@ const std = @import("std");const systemd = @import("systemd.zig");const gpa = @import("main.zig").gpa;const Child = @import("session/child.zig");+const platform = @import("platform");const c = @import("c");pub const Request = struct {@@ -31,8 +32,8 @@ pub fn start(client: *systemd.Client, unit: []const u8, value: []const u8) !voidif (sockets[0] >= 0) _ = c.close(sockets[0]);if (sockets[1] >= 0) _ = c.close(sockets[1]);}- const pkexec = if (c.access("/usr/bin/pkexec", c.X_OK) == 0) "/usr/bin/pkexec" else "/bin/pkexec";- const systemctl = if (c.access("/usr/bin/systemctl", c.X_OK) == 0) "/usr/bin/systemctl" else "/bin/systemctl";+ const pkexec = platform.tool.system(req.io, "pkexec") orelse return error.FileNotFound;+ const systemctl = platform.tool.system(req.io, "systemctl") orelse return error.FileNotFound;var child = try std.process.spawn(req.io, .{.argv = &.{ pkexec, "--disable-internal-agent", systemctl, "--system", "--no-reload", "--stdin", "--drop-in=zz-rediwm-notify.conf", "edit", "--", unit },.stdin = .{ .file = .{ .handle = sockets[0], .flags = .{ .nonblocking = false } } },