From 72023758232e708018d3c7b0b324e61f55cb5c9b Mon Sep 17 00:00:00 2001 From: bakkeby Date: Sun, 9 Mar 2025 12:52:16 +0100 Subject: [PATCH] simplify post-lock cmd and die if it fails this patch does two things: 0. simplify the code by using posix_spawn() 1. unify the behavior of what happens if the post-lock cmd fails. currently, if `fork()` fails, slock will die without locking the screen. HOWEVER if `execvp()` fails it prints a message to stderr (which the user cannot see since the screen has been locked already) and only exits the child while the parent locks the screen. to reproduce: # slock some_bin_that_doesnt_exist this behavior is inconsistent, if the idea is that post-lock cmd is _not_ important then we shouldn't `die()` on `fork()` failure either. and if we assume that the post-lock cmd _is_ important, then we should die on exec failure as well. this patch assumes the latter and calls `die()` if `posix_spawn()` fails. ref. https://git.suckless.org/slock/commit/a70d5d2429abf8dcb70a8817990975dc9a621d27.html --- README.md | 4 ++-- slock.c | 16 +++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 4de370bc4e620406091bf04fd40eb54ea4826131..182951e45dd48b4f99ee4cbb7b2c8c49aee3fde9 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -Similar to [dwm-flexipatch](https://github.com/bakkeby/dwm-flexipatch) this slock 1.5 (a34d8fb, -2023-10-06) project has a different take on patching. It uses preprocessor directives to decide +Similar to [dwm-flexipatch](https://github.com/bakkeby/dwm-flexipatch) this slock 1.5 (a70d5d2, +2025-03-09) project has a different take on patching. It uses preprocessor directives to decide whether or not to include a patch during build time. Essentially this means that this build, for better or worse, contains both the patched _and_ the original code. The aim being that you can select which patches to include and the build will contain that code and nothing more. diff --git a/slock.c b/slock.c index 50cadaf185e509553ce2b141eedd4e3c9bd1f6bb..fad8afc67393ad4dceebae90409cbbf1c20543a9 100644 --- a/slock.c +++ b/slock.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -763,15 +764,12 @@ main(int argc, char **argv) { /* run post-lock command */ if (argc > 0) { - switch (fork()) { - case -1: - die("slock: fork failed: %s\n", strerror(errno)); - case 0: - if (close(ConnectionNumber(dpy)) < 0) - die("slock: close: %s\n", strerror(errno)); - execvp(argv[0], argv); - fprintf(stderr, "slock: execvp %s: %s\n", argv[0], strerror(errno)); - _exit(1); + pid_t pid; + extern char **environ; + int err = posix_spawnp(&pid, argv[0], NULL, NULL, argv, environ); + if (err) { + die("slock: failed to execute post-lock command: %s: %s\n", + argv[0], strerror(err)); } }