aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2024-02-26 20:23:19 -0800
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2024-02-26 20:23:19 -0800
commit4c565d2020230ad3fdf603fde32b9892e56c16e1 (patch)
tree392fcb3d30532851f445a1ef68806869a5afa381
parenta34d8fb4327bbb1afd92e7527c53fcaad547a495 (diff)
downloadslock-4c565d2020230ad3fdf603fde32b9892e56c16e1.tar.gz
slock-4c565d2020230ad3fdf603fde32b9892e56c16e1.tar.xz
add ability to fork and print PID after locking
-rw-r--r--slock.15
-rw-r--r--slock.c31
2 files changed, 35 insertions, 1 deletions
diff --git a/slock.1 b/slock.1
index 40c15e1..08b62b9 100644
--- a/slock.1
+++ b/slock.1
@@ -6,6 +6,7 @@
.Nd simple X screen locker
.Sh SYNOPSIS
.Nm
+.Op Fl f Op Fl p
.Op Fl v
.Op Ar cmd Op Ar arg ...
.Sh DESCRIPTION
@@ -17,6 +18,10 @@ is executed after the screen has been locked.
.Pp
The options are as follows:
.Bl -tag -width Ds
+.It Fl f
+Fork and exit parent process after locking screen.
+.It Fl p
+Print child PID to stdout after forking.
.It Fl v
Print version information to stdout and exit.
.El
diff --git a/slock.c b/slock.c
index b2f14e3..0036c10 100644
--- a/slock.c
+++ b/slock.c
@@ -10,6 +10,7 @@
#include <pwd.h>
#include <stdarg.h>
#include <stdlib.h>
+#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
@@ -300,7 +301,7 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
static void
usage(void)
{
- die("usage: slock [-v] [cmd [arg ...]]\n");
+ die("usage: slock [-f [-p]] [-v] [cmd [arg ...]]\n");
}
int
@@ -314,8 +315,19 @@ main(int argc, char **argv) {
const char *hash;
Display *dpy;
int s, nlocks, nscreens;
+ int opt_fork, opt_pid;
+ pid_t pid;
+
+ opt_fork = 0;
+ opt_pid = 0;
ARGBEGIN {
+ case 'f':
+ opt_fork = 1;
+ break;
+ case 'p':
+ opt_pid = 1;
+ break;
case 'v':
puts("slock-"VERSION);
return 0;
@@ -323,6 +335,9 @@ main(int argc, char **argv) {
usage();
} ARGEND
+ if (opt_pid && !opt_fork)
+ usage();
+
/* validate drop-user and -group */
errno = 0;
if (!(pwd = getpwnam(user)))
@@ -374,6 +389,20 @@ main(int argc, char **argv) {
if (nlocks != nscreens)
return 1;
+ if (opt_fork) {
+ switch (pid = fork()) {
+ case -1:
+ die("slock: fork failed: %s\n", strerror(errno));
+ case 0:
+ freopen("/dev/null", "w", stdout);
+ break;
+ default:
+ if (opt_pid)
+ printf("%ji\n", (intmax_t)pid);
+ exit(0);
+ }
+ }
+
/* run post-lock command */
if (argc > 0) {
switch (fork()) {