Clean up redundant authentication & session code

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion 2025-09-25 08:32:30 +02:00
parent d8b0ae34f3
commit 145ad5142c
No known key found for this signature in database
2 changed files with 14 additions and 46 deletions

View File

@ -166,14 +166,12 @@ fn startSession(
// Execute what the user requested
switch (current_environment.display_server) {
.wayland => try executeWaylandCmd(log_writer, allocator, user_entry.shell.?, options, current_environment.cmd),
.shell => try executeShellCmd(allocator, user_entry.shell.?, options),
.wayland, .shell, .custom => try executeCmd(log_writer, allocator, user_entry.shell.?, options, current_environment.is_terminal, current_environment.cmd),
.xinitrc, .x11 => if (build_options.enable_x11_support) {
var vt_buf: [5]u8 = undefined;
const vt = try std.fmt.bufPrint(&vt_buf, "vt{d}", .{options.tty});
try executeX11Cmd(log_writer, allocator, user_entry.shell.?, user_entry.home.?, options, current_environment.cmd, vt);
},
.custom => try executeCustomCmd(log_writer, allocator, user_entry.shell.?, options, current_environment.is_terminal, current_environment.cmd),
}
}
@ -197,7 +195,7 @@ fn setXdgEnv(allocator: std.mem.Allocator, tty_str: []u8, environment: Environme
.wayland => "wayland",
.shell => "tty",
.xinitrc, .x11 => "x11",
.custom => "unspecified",
.custom => if (environment.is_terminal) "tty" else "unspecified",
}, false);
// The "/run/user/%d" directory is not available on FreeBSD. It is much
@ -389,36 +387,6 @@ fn xauth(log_writer: *std.Io.Writer, allocator: std.mem.Allocator, display_name:
}
}
fn executeShellCmd(allocator: std.mem.Allocator, shell: []const u8, options: AuthOptions) !void {
// We don't want to redirect stdout and stderr in a shell session
const shell_z = try allocator.dupeZ(u8, shell);
defer allocator.free(shell_z);
var cmd_buffer: [1024]u8 = undefined;
const cmd_str = try std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ options.setup_cmd, options.login_cmd orelse "", shell });
const args = [_:null]?[*:0]const u8{ shell_z, "-c", cmd_str };
return std.posix.execveZ(shell_z, &args, std.c.environ);
}
fn executeWaylandCmd(log_writer: *std.Io.Writer, allocator: std.mem.Allocator, shell: []const u8, options: AuthOptions, desktop_cmd: []const u8) !void {
var maybe_log_file: ?std.fs.File = null;
if (options.session_log) |log_path| {
maybe_log_file = try redirectStandardStreams(log_writer, log_path, true);
}
defer if (maybe_log_file) |log_file| log_file.close();
const shell_z = try allocator.dupeZ(u8, shell);
defer allocator.free(shell_z);
var cmd_buffer: [1024]u8 = undefined;
const cmd_str = try std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ options.setup_cmd, options.login_cmd orelse "", desktop_cmd });
const args = [_:null]?[*:0]const u8{ shell_z, "-c", cmd_str };
return std.posix.execveZ(shell_z, &args, std.c.environ);
}
fn executeX11Cmd(log_writer: *std.Io.Writer, allocator: std.mem.Allocator, shell: []const u8, home: []const u8, options: AuthOptions, desktop_cmd: []const u8, vt: []const u8) !void {
const display_num = try getFreeDisplay();
var buf: [4]u8 = undefined;
@ -482,7 +450,7 @@ fn executeX11Cmd(log_writer: *std.Io.Writer, allocator: std.mem.Allocator, shell
_ = std.posix.waitpid(x_pid, 0);
}
fn executeCustomCmd(log_writer: *std.Io.Writer, allocator: std.mem.Allocator, shell: []const u8, options: AuthOptions, is_terminal: bool, exec_cmd: []const u8) !void {
fn executeCmd(log_writer: *std.Io.Writer, allocator: std.mem.Allocator, shell: []const u8, options: AuthOptions, is_terminal: bool, exec_cmd: []const u8) !void {
var maybe_log_file: ?std.fs.File = null;
if (!is_terminal) {
// For custom desktop entries, the "Terminal" value here determines if

View File

@ -990,6 +990,7 @@ fn addOtherEnvironment(session: *Session, lang: Lang, display_server: DisplaySer
.cmd = exec orelse "",
.specifier = lang.other,
.display_server = display_server,
.is_terminal = display_server == .shell,
});
}
@ -1010,24 +1011,23 @@ fn crawl(session: *Session, lang: Lang, path: []const u8, display_server: Displa
});
errdefer entry_ini.deinit();
var maybe_xdg_session_desktop: ?[]const u8 = null;
const maybe_desktop_names = entry_ini.data.@"Desktop Entry".DesktopNames;
if (maybe_desktop_names) |desktop_names| {
maybe_xdg_session_desktop = std.mem.sliceTo(desktop_names, ';');
} else if (display_server != .custom) {
// If DesktopNames is empty, and this isn't a custom session entry,
// we'll take the name of the session file
maybe_xdg_session_desktop = std.fs.path.stem(item.name);
}
// Prepare the XDG_CURRENT_DESKTOP environment variable here
const entry = entry_ini.data.@"Desktop Entry";
var maybe_xdg_session_desktop: ?[]const u8 = null;
var maybe_xdg_desktop_names: ?[]const u8 = null;
// Prepare the XDG_SESSION_DESKTOP and XDG_CURRENT_DESKTOP environment
// variables here
if (entry.DesktopNames) |desktop_names| {
maybe_xdg_session_desktop = std.mem.sliceTo(desktop_names, ';');
for (desktop_names) |*c| {
if (c.* == ';') c.* = ':';
}
maybe_xdg_desktop_names = desktop_names;
} else if (display_server != .custom) {
// If DesktopNames is empty, and this isn't a custom session entry,
// we'll take the name of the session file
maybe_xdg_session_desktop = std.fs.path.stem(item.name);
}
try session.addEnvironment(.{