Skip to content

Commit

Permalink
Add --ready-fd
Browse files Browse the repository at this point in the history
This implements a readiness notification mechanism which works on
both systemd and s6.

References: swaywm#42
References: swaywm#275
  • Loading branch information
emersion authored and mstoeckl committed Jan 28, 2023
1 parent 50c9e4d commit 75d6a7e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/swaylock.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct swaylock_args {
bool hide_keyboard_layout;
bool show_failed_attempts;
bool daemonize;
int ready_fd;
bool indicator_idle_visible;
char *plugin_command;
};
Expand Down
22 changes: 21 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state,
{"debug", no_argument, NULL, 'd'},
{"ignore-empty-password", no_argument, NULL, 'e'},
{"daemonize", no_argument, NULL, 'f'},
{"ready-fd", required_argument, NULL, 'R'},
{"help", no_argument, NULL, 'h'},
{"image", required_argument, NULL, 'i'},
{"disable-caps-lock-text", no_argument, NULL, 'L'},
Expand Down Expand Up @@ -887,6 +888,8 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state,
"Show current count of failed authentication attempts.\n"
" -f, --daemonize "
"Detach from the controlling terminal after locking.\n"
" -R, --ready-fd <fd> "
"File descriptor to send readiness notifications to.\n"
" -h, --help "
"Show help message and quit.\n"
" -i, --image [[<output>]:]<path> "
Expand Down Expand Up @@ -998,7 +1001,7 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state,
optind = 1;
while (1) {
int opt_idx = 0;
c = getopt_long(argc, argv, "c:deFfhi:kKLlnrs:tuvC:", long_options,
c = getopt_long(argc, argv, "c:deFfhi:kKLlnrs:tuvC:R:", long_options,
&opt_idx);
if (c == -1) {
break;
Expand Down Expand Up @@ -1032,6 +1035,11 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state,
state->args.daemonize = true;
}
break;
case 'R':
if (state) {
state->args.ready_fd = strtol(optarg, NULL, 10);
}
break;
case 'i':
if (state) {
load_image(optarg, state);
Expand Down Expand Up @@ -1738,6 +1746,7 @@ int main(int argc, char **argv) {
.hide_keyboard_layout = false,
.show_failed_attempts = false,
.indicator_idle_visible = false,
.ready_fd = -1,
.plugin_command = NULL,
};
wl_list_init(&state.images);
Expand Down Expand Up @@ -1870,6 +1879,17 @@ int main(int argc, char **argv) {
state.locked = true;
}

if (state.args.ready_fd >= 0) {
// s6 wants a newline and ignores any text before that, systemd wants
// READY=1, so use the least common denominator
const char ready_str[] = "READY=1\n";
if (write(state.args.ready_fd, ready_str, strlen(ready_str)) != strlen(ready_str)) {
swaylock_log(LOG_ERROR, "Failed to send readiness notification");
return 2;
}
close(state.args.ready_fd);
state.args.ready_fd = -1;
}
if (state.args.daemonize) {
daemonize();
}
Expand Down

0 comments on commit 75d6a7e

Please sign in to comment.