Skip to content

Commit

Permalink
Lock: add -ready-fd option
Browse files Browse the repository at this point in the history
My original idea was sending SIGUSR1, but ifreund suggested this, also
present in swaylock. The advantage of this approach is that it's simpler
and works well with at least two service managers (s6-rc, dinit).

Figuring out how to use this in a shell script took me longer than i'd
like to admit. This is what i ended up with:

    ( waylock-wrapper -ready-fd 3 3>&1 1>&2 & ) | read && suspend
  • Loading branch information
tiosgz committed Feb 18, 2024
1 parent 66b74c3 commit d859e5b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions doc/waylock.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ unlocked.
session before suspend. With this option waylock will exit once the
session has been locked and it is safe to suspend.

*-ready-fd* _fd_
Write a newline to file descriptor _fd_ (a number) after locking and
close the _fd_. This is useful if a fork-less lock notification is needed;
for example, for integration with a service manager.

*-ignore-empty-password*
Do not submit empty passwords to PAM for validation when the Enter
key is pressed. Instead, do nothing. This may be useful if, for
Expand Down
12 changes: 12 additions & 0 deletions src/Lock.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub const Color = enum {

pub const Options = struct {
fork_on_lock: bool,
ready_fd: os.fd_t = -1,
ignore_empty_password: bool,
init_color: u24 = 0x002b36,
input_color: u24 = 0x6c71c4,
Expand Down Expand Up @@ -60,6 +61,7 @@ state: enum {
color: Color = .init,

fork_on_lock: bool,
ready_fd: os.fd_t,
ignore_empty_password: bool,

pollfds: [2]os.pollfd,
Expand All @@ -82,6 +84,7 @@ auth_connection: auth.Connection,
pub fn run(options: Options) void {
var lock: Lock = .{
.fork_on_lock = options.fork_on_lock,
.ready_fd = options.ready_fd,
.ignore_empty_password = options.ignore_empty_password,
.pollfds = undefined,
.display = wl.Display.connect(null) catch |err| {
Expand Down Expand Up @@ -358,6 +361,15 @@ fn session_lock_listener(_: *ext.SessionLockV1, event: ext.SessionLockV1.Event,
.locked => {
assert(lock.state == .locking);
lock.state = .locked;
if (lock.ready_fd >= 0) {
const file = std.fs.File{ .handle = lock.ready_fd };
file.writeAll("\n") catch |err| {
log.err("failed to send readiness notification: {s}", .{@errorName(err)});
os.exit(1);
};
file.close();
lock.ready_fd = -1;
}
if (lock.fork_on_lock) {
fork_to_background();
lock.password.protect_after_fork();
Expand Down
8 changes: 8 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const usage =
\\ -log-level <level> Set the log level to error, warning, info, or debug.
\\
\\ -fork-on-lock Fork to the background after locking.
\\ -ready-fd <fd> Write a newline to fd after locking.
\\ -ignore-empty-password Do not validate an empty password.
\\
\\ -init-color 0xRRGGBB Set the initial color.
Expand All @@ -33,6 +34,7 @@ pub fn main() void {
.{ .name = "version", .kind = .boolean },
.{ .name = "log-level", .kind = .arg },
.{ .name = "fork-on-lock", .kind = .boolean },
.{ .name = "ready-fd", .kind = .arg },
.{ .name = "ignore-empty-password", .kind = .boolean },
.{ .name = "init-color", .kind = .arg },
.{ .name = "input-color", .kind = .arg },
Expand Down Expand Up @@ -74,6 +76,12 @@ pub fn main() void {
.fork_on_lock = result.flags.@"fork-on-lock",
.ignore_empty_password = result.flags.@"ignore-empty-password",
};
if (result.flags.@"ready-fd") |raw| {
options.ready_fd = std.fmt.parseInt(os.fd_t, raw, 10) catch {
log.err("invalid file descriptor '{s}'", .{raw});
os.exit(1);
};
}
if (result.flags.@"init-color") |raw| options.init_color = parse_color(raw);
if (result.flags.@"input-color") |raw| options.input_color = parse_color(raw);
if (result.flags.@"fail-color") |raw| options.fail_color = parse_color(raw);
Expand Down

0 comments on commit d859e5b

Please sign in to comment.