Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ pub fn main(init: std.process.Init) !void {
break :blk tracker.allocator();
} else base_gpa;

// SMAppService agents can start without the usual login environment.
// Populate the identity variables that config discovery, daemon logging,
// and PID-file naming rely on from the current user's passwd record.
ensureUserEnvironment();

defer if (comptime track_alloc) {
std.debug.print("\n=== Final Allocation Report ===\n", .{});
var stderr_buf: [4096]u8 = undefined;
Expand Down Expand Up @@ -227,6 +232,20 @@ pub fn main(init: std.process.Init) !void {
skhd.run(!no_hotload) catch {};
}

fn ensureUserEnvironment() void {
const need_home = utils.getenv("HOME") == null;
const need_user = utils.getenv("USER") == null;
if (!need_home and !need_user) return;

const pw = c.getpwuid(c.getuid()) orelse return;
if (need_home) {
if (pw.*.pw_dir) |home| _ = c.setenv("HOME", home, 0);
}
if (need_user) {
if (pw.*.pw_name) |user| _ = c.setenv("USER", user, 0);
}
}

/// Print the error name and exit non-zero. Used by grabber CLI commands
/// so the user sees *what* failed instead of a silent exit-1 — the bare
/// `catch std.process.exit(1)` form leaves users staring at a half-finished
Expand Down