slock-xss-lock-1.7.diff (1980B)
1 diff -up a/slock.c b/slock.c 2 --- a/slock.c 2026-07-28 03:28:03.120285888 +0530 3 +++ b/slock.c 2026-08-03 11:20:35.223992495 +0530 4 @@ -6,7 +6,9 @@ 5 6 #include <ctype.h> 7 #include <errno.h> 8 +#include <fcntl.h> 9 #include <grp.h> 10 +#include <limits.h> 11 #include <pwd.h> 12 #include <stdarg.h> 13 #include <stdlib.h> 14 @@ -59,7 +61,6 @@ die(const char *errstr, ...) 15 } 16 17 #ifdef __linux__ 18 -#include <fcntl.h> 19 #include <linux/oom.h> 20 21 static void 22 @@ -301,6 +302,28 @@ lockscreen(Display *dpy, struct xrandr * 23 return NULL; 24 } 25 26 +static int 27 +get_xss_sleep_lock_fd(void) 28 +{ 29 + char *env_str, *end; 30 + long env_val; 31 + 32 + env_str = getenv("XSS_SLEEP_LOCK_FD"); 33 + if (!env_str || !*env_str) 34 + return -1; 35 + 36 + errno = 0; 37 + env_val = strtol(env_str, &end, 10); 38 + if (errno || 39 + *end || 40 + (env_val < 0 || env_val > INT_MAX)) { 41 + fprintf(stderr, "slock: strtol $XSS_SLEEP_LOCK_FD: %s\n", 42 + errno ? strerror(errno) : "invalid value"); 43 + return -1; 44 + } 45 + return (int)env_val; 46 +} 47 + 48 static void 49 usage(void) 50 { 51 @@ -318,6 +341,7 @@ main(int argc, char **argv) { 52 const char *hash; 53 Display *dpy; 54 int s, nlocks, nscreens; 55 + int lockfd; 56 57 ARGBEGIN { 58 case 'v': 59 @@ -327,6 +351,18 @@ main(int argc, char **argv) { 60 usage(); 61 } ARGEND 62 63 + lockfd = get_xss_sleep_lock_fd(); 64 + if (lockfd >= 0) { 65 + int flags = fcntl(lockfd, F_GETFD); 66 + if (flags < 0) { 67 + fprintf(stderr, "slock: fcntl $XSS_SLEEP_LOCK_FD F_GETFD: %s\n", 68 + strerror(errno)); 69 + } else if (fcntl(lockfd, F_SETFD, flags | FD_CLOEXEC) == -1) { 70 + fprintf(stderr, "slock: fcntl $XSS_SLEEP_LOCK_FD F_SETFD: %s\n", 71 + strerror(errno)); 72 + } 73 + } 74 + 75 /* validate drop-user and -group */ 76 errno = 0; 77 if (!(pwd = getpwnam(user))) 78 @@ -378,6 +414,9 @@ main(int argc, char **argv) { 79 if (nlocks != nscreens) 80 return 1; 81 82 + if (lockfd >= 0 && close(lockfd) < 0) 83 + fprintf(stderr, "slock: close $XSS_SLEEP_LOCK_FD: %s\n", strerror(errno)); 84 + 85 /* run post-lock command */ 86 if (argc > 0) { 87 pid_t pid;