From 74f7f08801bca3e22200deb9fcbd3e9cd7b5569d Mon Sep 17 00:00:00 2001 From: Kinzie Date: Thu, 9 May 2024 06:04:05 +0100 Subject: [PATCH] fix cascade, set info_line before auth, make code clearer and a bug fix --- res/lang/en.ini | 1 + src/auth.zig | 33 +++++++++++++++++++++------------ src/config/Lang.zig | 1 + src/main.zig | 23 +++++++++++++---------- src/tui/components/InfoLine.zig | 22 ++++++++++++++++++++++ 5 files changed, 58 insertions(+), 22 deletions(-) diff --git a/res/lang/en.ini b/res/lang/en.ini index 766dc86..e9bb9e4 100644 --- a/res/lang/en.ini +++ b/res/lang/en.ini @@ -1,3 +1,4 @@ +authenticating = authenticating... capslock = capslock err_alloc = failed memory allocation err_bounds = out-of-bounds index diff --git a/src/auth.zig b/src/auth.zig index 0d2d015..8aab524 100644 --- a/src/auth.zig +++ b/src/auth.zig @@ -83,21 +83,30 @@ pub fn authenticate(config: Config, current_environment: Desktop.Environment, lo std.process.exit(0); } - var entry: Utmp = std.mem.zeroes(Utmp); - addUtmpEntry(&entry, pwd.pw_name, child_pid) catch {}; - defer removeUtmpEntry(&entry); + var entry = std.mem.zeroes(Utmp); - // If we receive SIGTERM, forward it to child_pid - const act = std.posix.Sigaction{ - .handler = .{ .handler = &sessionSignalHandler }, - .mask = std.posix.empty_sigset, - .flags = 0, - }; - try std.posix.sigaction(std.posix.SIG.TERM, &act, null); + { + // If an error occurs here, we can send SIGTERM to the session + errdefer cleanup: { + _ = std.posix.kill(child_pid, std.posix.SIG.TERM) catch break :cleanup; + _ = std.posix.waitpid(child_pid, 0); + } + // If we receive SIGTERM, forward it to child_pid + const act = std.posix.Sigaction{ + .handler = .{ .handler = &sessionSignalHandler }, + .mask = std.posix.empty_sigset, + .flags = 0, + }; + try std.posix.sigaction(std.posix.SIG.TERM, &act, null); + + try addUtmpEntry(&entry, pwd.pw_name, child_pid); + } // Wait for the session to stop _ = std.posix.waitpid(child_pid, 0); + removeUtmpEntry(&entry); + try resetTerminal(pwd.pw_shell, config.term_reset_cmd); if (shared_err.readError()) |err| return err; @@ -190,7 +199,7 @@ fn loginConv( // Initialise allocated memory to 0 // This ensures memory can be freed by pam on success - for (response) |*r| r.* = std.mem.zeroes(interop.pam.pam_response); + @memset(response, std.mem.zeroes(interop.pam.pam_response)); var username: ?[:0]u8 = null; var password: ?[:0]u8 = null; @@ -412,7 +421,7 @@ fn addUtmpEntry(entry: *Utmp, username: [*:0]const u8, pid: c_int) !void { entry.ut_pid = pid; var buf: [4096]u8 = undefined; - const ttyname = try std.os.getFdPath(0, &buf); + const ttyname = try std.os.getFdPath(std.posix.STDIN_FILENO, &buf); var ttyname_buf: [32]u8 = undefined; _ = try std.fmt.bufPrintZ(&ttyname_buf, "{s}", .{ttyname["/dev/".len..]}); diff --git a/src/config/Lang.zig b/src/config/Lang.zig index 0333e0b..23590f7 100644 --- a/src/config/Lang.zig +++ b/src/config/Lang.zig @@ -1,3 +1,4 @@ +authenticating: []const u8 = "authenticating...", capslock: []const u8 = "capslock", err_alloc: []const u8 = "failed memory allocation", err_bounds: []const u8 = "out-of-bounds index", diff --git a/src/main.zig b/src/main.zig index 36a9164..0abdf01 100644 --- a/src/main.zig +++ b/src/main.zig @@ -267,7 +267,7 @@ pub fn main() !void { while (run) { // If there's no input or there's an animation, a resolution change needs to be checked if (!update or config.animation != .none) { - if (!update) std.time.sleep(100_000_000); + if (!update) std.time.sleep(std.time.ns_per_ms * 100); termbox.tb_present(); // Required to update tb_width(), tb_height() and tb_cell_buffer() @@ -367,10 +367,7 @@ pub fn main() !void { buffer.drawLabel(lang.login, label_x, label_y + 4); buffer.drawLabel(lang.password, label_x, label_y + 6); - if (info_line.width > 0 and buffer.box_width > info_line.width) { - const x = buffer.box_x + ((buffer.box_width - info_line.width) / 2); - buffer.drawLabel(info_line.text, x, label_y); - } + info_line.draw(buffer); if (!config.hide_key_hints) { var length: u64 = 0; @@ -423,11 +420,11 @@ pub fn main() !void { update = animate; } else { - std.time.sleep(10_000_000); + std.time.sleep(std.time.ns_per_ms * 10); update = buffer.cascade(); if (!update) { - std.time.sleep(7_000_000_000); + std.time.sleep(std.time.ns_per_s * 7); auth_fails = 0; } } @@ -531,6 +528,11 @@ pub fn main() !void { const password_text = try allocator.dupeZ(u8, password.text.items); defer allocator.free(password_text); + try info_line.setText(lang.authenticating); + InfoLine.clearRendered(allocator, buffer) catch {}; + info_line.draw(buffer); + _ = termbox.tb_present(); + session_pid = try std.posix.fork(); if (session_pid == 0) { const current_environment = desktop.environments.items[desktop.current]; @@ -557,14 +559,15 @@ pub fn main() !void { } try std.posix.tcsetattr(std.posix.STDIN_FILENO, .FLUSH, tb_termios); - termbox.tb_clear(); + if (auth_fails < 10) { + termbox.tb_clear(); + termbox.tb_present(); + } update = true; var restore_cursor = std.ChildProcess.init(&[_][]const u8{ "/bin/sh", "-c", config.term_restore_cursor_cmd }, allocator); _ = restore_cursor.spawnAndWait() catch .{}; - - termbox.tb_present(); }, else => { if (!insert_mode) { diff --git a/src/tui/components/InfoLine.zig b/src/tui/components/InfoLine.zig index 0a216a9..82d33ee 100644 --- a/src/tui/components/InfoLine.zig +++ b/src/tui/components/InfoLine.zig @@ -1,4 +1,6 @@ +const std = @import("std"); const utils = @import("../utils.zig"); +const TerminalBuffer = @import("../TerminalBuffer.zig"); const InfoLine = @This(); @@ -9,3 +11,23 @@ pub fn setText(self: *InfoLine, text: []const u8) !void { self.width = if (text.len > 0) try utils.strWidth(text) else 0; self.text = text; } + +pub fn draw(self: InfoLine, buffer: TerminalBuffer) void { + if (self.width > 0 and buffer.box_width > self.width) { + const label_y = buffer.box_y + buffer.margin_box_v; + const x = buffer.box_x + ((buffer.box_width - self.width) / 2); + + buffer.drawLabel(self.text, x, label_y); + } +} + +pub fn clearRendered(allocator: std.mem.Allocator, buffer: TerminalBuffer) !void { + // draw over the area + const y = buffer.box_y + buffer.margin_box_v; + const spaces = try allocator.alloc(u8, buffer.box_width); + defer allocator.free(spaces); + + @memset(spaces, ' '); + + buffer.drawLabel(spaces, buffer.box_x, y); +}