Add login & logout script support

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion 2024-08-06 11:06:20 +02:00
parent 2901b408dc
commit b5b3317dd8
No known key found for this signature in database
GPG Key ID: 3E85EB44F610AD7F
4 changed files with 28 additions and 9 deletions

View File

@ -1,11 +1,11 @@
# The active animation
# none -> Nothing (default)
# none -> Nothing
# doom -> PSX DOOM fire
# matrix -> CMatrix
animation = none
# Stop the animation after some time
# 0 -> Run forever (default)
# 0 -> Run forever
# 1..2e12 -> Stop the animation after this many seconds
animation_timeout_sec = 0
@ -112,6 +112,18 @@ lang = en
# Load the saved desktop and username
load = true
# Command executed when logging in
# If null, no command will be executed
# Important: the code itself must end with `exec "$@"` in order to launch the session!
# You can also set environment variables in there, they'll persist until logout
login_cmd = null
# Command executed when logging out
# If null, no command will be executed
# Important: the session will already be terminated when this command is executed, so
# no need to add `exec "$@"` at the end
logout_cmd = null
# Main box horizontal margin
margin_box_h = 2

View File

@ -156,8 +156,8 @@ fn startSession(
std.posix.chdirZ(pwd.pw_dir.?) catch return error.ChangeDirectoryFailed;
switch (current_environment.display_server) {
.wayland => try executeWaylandCmd(pwd.pw_shell.?, config.setup_cmd, current_environment.cmd),
.shell => try executeShellCmd(pwd.pw_shell.?, config.setup_cmd),
.wayland => try executeWaylandCmd(pwd.pw_shell.?, config.setup_cmd, config.login_cmd orelse "", current_environment.cmd),
.shell => try executeShellCmd(pwd.pw_shell.?, config.setup_cmd, config.login_cmd orelse ""),
.xinitrc, .x11 => if (build_options.enable_x11_support) {
var vt_buf: [5]u8 = undefined;
const vt = try std.fmt.bufPrint(&vt_buf, "vt{d}", .{config.tty});
@ -372,16 +372,16 @@ fn xauth(display_name: [:0]u8, shell: [*:0]const u8, pw_dir: [*:0]const u8, xaut
if (status.status != 0) return error.XauthFailed;
}
fn executeShellCmd(shell: [*:0]const u8, setup_cmd: []const u8) !void {
fn executeShellCmd(shell: [*:0]const u8, setup_cmd: []const u8, login_cmd: []const u8) !void {
var cmd_buffer: [1024]u8 = undefined;
const cmd_str = try std.fmt.bufPrintZ(&cmd_buffer, "{s} {s}", .{ setup_cmd, shell });
const cmd_str = try std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ setup_cmd, login_cmd, shell });
const args = [_:null]?[*:0]const u8{ shell, "-c", cmd_str };
return std.posix.execveZ(shell, &args, std.c.environ);
}
fn executeWaylandCmd(shell: [*:0]const u8, setup_cmd: []const u8, desktop_cmd: []const u8) !void {
fn executeWaylandCmd(shell: [*:0]const u8, setup_cmd: []const u8, login_cmd: []const u8, desktop_cmd: []const u8) !void {
var cmd_buffer: [1024]u8 = undefined;
const cmd_str = try std.fmt.bufPrintZ(&cmd_buffer, "{s} {s}", .{ setup_cmd, desktop_cmd });
const cmd_str = try std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ setup_cmd, login_cmd, desktop_cmd });
const args = [_:null]?[*:0]const u8{ shell, "-c", cmd_str };
return std.posix.execveZ(shell, &args, std.c.environ);
}
@ -418,7 +418,7 @@ fn executeX11Cmd(shell: [*:0]const u8, pw_dir: [*:0]const u8, config: Config, de
xorg_pid = try std.posix.fork();
if (xorg_pid == 0) {
var cmd_buffer: [1024]u8 = undefined;
const cmd_str = std.fmt.bufPrintZ(&cmd_buffer, "{s} {s}", .{ config.setup_cmd, desktop_cmd }) catch std.process.exit(1);
const cmd_str = std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ config.setup_cmd, config.login_cmd orelse "", desktop_cmd }) catch std.process.exit(1);
const args = [_:null]?[*:0]const u8{ shell, "-c", cmd_str };
std.posix.execveZ(shell, &args, std.c.environ) catch {};
std.process.exit(1);

View File

@ -32,6 +32,8 @@ initial_info_text: ?[]const u8 = null,
input_len: u8 = 34,
lang: []const u8 = "en",
load: bool = true,
login_cmd: ?[]const u8 = null,
logout_cmd: ?[]const u8 = null,
margin_box_h: u8 = 2,
margin_box_v: u8 = 1,
min_refresh_delta: u16 = 5,

View File

@ -698,6 +698,11 @@ pub fn main() !void {
try info_line.addMessage(getAuthErrorMsg(err, lang), config.error_bg, config.error_fg);
if (config.clear_password or err != error.PamAuthError) password.clear();
} else {
if (config.logout_cmd) |logout_cmd| {
var logout_process = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", logout_cmd }, allocator);
_ = logout_process.spawnAndWait() catch .{};
}
password.clear();
try info_line.addMessage(lang.logout, config.bg, config.fg);
}