]> git.armaanb.net Git - slock.git/blobdiff - slock.c
Exit as soon as possible on input grabbing error
[slock.git] / slock.c
diff --git a/slock.c b/slock.c
index 68e813f4e86041ed7be45492771adcdfcb7d9187..dd323740bbdd0206cbc9685031360ba2fe0ea7c4 100644 (file)
--- a/slock.c
+++ b/slock.c
 #include <bsd_auth.h>
 #endif
 
+#include "arg.h"
+#include "util.h"
+
+char *argv0;
+
 enum {
        INIT,
        INPUT,
@@ -67,10 +72,13 @@ dontkillme(void)
        int fd;
 
        fd = open("/proc/self/oom_score_adj", O_WRONLY);
-       if (fd < 0 && errno == ENOENT)
+       if (fd < 0 && errno == ENOENT) {
                return;
-       if (fd < 0 || write(fd, "-1000\n", 6) != 6 || close(fd) != 0)
-               die("cannot disable the out-of-memory killer for this process\n");
+       }
+       if (fd < 0 || write(fd, "-1000\n", (sizeof("-1000\n") - 1)) !=
+           (sizeof("-1000\n") - 1) || close(fd) != 0) {
+               die("can't tame the oom-killer. is suid or sgid set?\n");
+       }
 }
 #endif
 
@@ -83,21 +91,19 @@ getpw(void)
        struct passwd *pw;
 
        errno = 0;
-       pw = getpwuid(getuid());
-       if (!pw) {
+       if (!(pw = getpwuid(getuid()))) {
                if (errno)
-                       die("slock: getpwuid: %s\n", strerror(errno));
+                       die("getpwuid: %s\n", strerror(errno));
                else
-                       die("slock: cannot retrieve password entry\n");
+                       die("cannot retrieve password entry\n");
        }
-       rval =  pw->pw_passwd;
+       rval = pw->pw_passwd;
 
 #if HAVE_SHADOW_H
        if (rval[0] == 'x' && rval[1] == '\0') {
                struct spwd *sp;
-               sp = getspnam(getenv("USER"));
-               if (!sp)
-                       die("slock: cannot retrieve shadow entry (make sure to suid or sgid slock)\n");
+               if (!(sp = getspnam(getenv("USER"))))
+                       die("cannot retrieve shadow entry (make sure to suid or sgid slock)\n");
                rval = sp->sp_pwdp;
        }
 #endif
@@ -105,7 +111,7 @@ getpw(void)
        /* drop privileges */
        if (geteuid() == 0 &&
            ((getegid() != pw->pw_gid && setgid(pw->pw_gid) < 0) || setuid(pw->pw_uid) < 0))
-               die("slock: cannot drop privileges\n");
+               die("cannot drop privileges\n");
        return rval;
 }
 #endif
@@ -119,11 +125,12 @@ readpw(Display *dpy, const char *pws)
 {
        char buf[32], passwd[256];
        int num, screen;
-       unsigned int len, llen;
+       unsigned int len, color;
        KeySym ksym;
        XEvent ev;
+       static int oldc = INIT;
 
-       len = llen = 0;
+       len = 0;
        running = True;
 
        /* As "slock" stands for "Simple X display locker", the DPMS settings
@@ -132,7 +139,7 @@ readpw(Display *dpy, const char *pws)
         * timeout. */
        while (running && !XNextEvent(dpy, &ev)) {
                if (ev.type == KeyPress) {
-                       buf[0] = 0;
+                       explicit_bzero(&buf, sizeof(buf));
                        num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
                        if (IsKeypadKey(ksym)) {
                                if (ksym == XK_KP_Enter)
@@ -158,34 +165,32 @@ readpw(Display *dpy, const char *pws)
                                        XBell(dpy, 100);
                                        failure = True;
                                }
+                               explicit_bzero(&passwd, sizeof(passwd));
                                len = 0;
                                break;
                        case XK_Escape:
+                               explicit_bzero(&passwd, sizeof(passwd));
                                len = 0;
                                break;
                        case XK_BackSpace:
                                if (len)
-                                       --len;
+                                       passwd[len--] = 0;
                                break;
                        default:
-                               if (num && !iscntrl((int) buf[0]) && (len + num < sizeof(passwd))) {
+                               if (num && !iscntrl((int)buf[0]) && (len + num < sizeof(passwd))) {
                                        memcpy(passwd + len, buf, num);
                                        len += num;
                                }
                                break;
                        }
-                       if (llen == 0 && len != 0) {
+                       color = len ? INPUT : (failure || failonclear ? FAILED : INIT);
+                       if (running && oldc != color) {
                                for (screen = 0; screen < nscreens; screen++) {
-                                       XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[INPUT]);
-                                       XClearWindow(dpy, locks[screen]->win);
-                               }
-                       } else if (llen != 0 && len == 0) {
-                               for (screen = 0; screen < nscreens; screen++) {
-                                       XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[failure || failonclear ? FAILED : INIT]);
+                                       XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[color]);
                                        XClearWindow(dpy, locks[screen]->win);
                                }
+                               oldc = color;
                        }
-                       llen = len;
                } else if (rr && ev.type == rrevbase + RRScreenChangeNotify) {
                        XRRScreenChangeNotifyEvent *rre = (XRRScreenChangeNotifyEvent*)&ev;
                        for (screen = 0; screen < nscreens; screen++) {
@@ -217,22 +222,16 @@ static Lock *
 lockscreen(Display *dpy, int screen)
 {
        char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
-       unsigned int len;
        int i;
        Lock *lock;
        XColor color, dummy;
        XSetWindowAttributes wa;
        Cursor invisible;
 
-       if (dpy == NULL || screen < 0)
-               return NULL;
-
-       lock = malloc(sizeof(Lock));
-       if (lock == NULL)
+       if (!running || dpy == NULL || screen < 0 || !(lock = malloc(sizeof(Lock))))
                return NULL;
 
        lock->screen = screen;
-
        lock->root = RootWindow(dpy, lock->screen);
 
        for (i = 0; i < NUMCOLS; i++) {
@@ -249,40 +248,37 @@ lockscreen(Display *dpy, int screen)
        lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
        invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0);
        XDefineCursor(dpy, lock->win, invisible);
-       XMapRaised(dpy, lock->win);
-       if (rr)
-               XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
-       for (len = 1000; len; len--) {
-               if (XGrabPointer(dpy, lock->root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
-                   GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
-                       break;
-               usleep(1000);
-       }
-       if (running && (len > 0)) {
-               for (len = 1000; len; len--) {
-                       if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
-                               break;
-                       usleep(1000);
-               }
-       }
 
-       running &= (len > 0);
-       if (!running) {
+       /* Try to grab mouse pointer *and* keyboard, else fail the lock */
+       if (XGrabPointer(dpy, lock->root, False, ButtonPressMask |
+           ButtonReleaseMask | PointerMotionMask, GrabModeAsync, GrabModeAsync,
+           None, invisible, CurrentTime) != GrabSuccess) {
+               fprintf(stderr, "slock: unable to grab mouse pointer for screen %d\n", screen);
+               running = 0;
                unlockscreen(dpy, lock);
-               lock = NULL;
+               return NULL;
        }
-       else {
-               XSelectInput(dpy, lock->root, SubstructureNotifyMask);
+
+       if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync,
+           CurrentTime) != GrabSuccess) {
+               fprintf(stderr, "slock: unable to grab keyboard for screen %d\n", screen);
+               running = 0;
+               unlockscreen(dpy, lock);
+               return NULL;
        }
 
+       XMapRaised(dpy, lock->win);
+       if (rr)
+               XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
+
+       XSelectInput(dpy, lock->root, SubstructureNotifyMask);
        return lock;
 }
 
 static void
 usage(void)
 {
-       fprintf(stderr, "usage: slock [-v]\n");
-       exit(1);
+       die("usage: slock [-v | cmd [arg ...]]\n");
 }
 
 int
@@ -291,56 +287,86 @@ main(int argc, char **argv) {
        const char *pws;
 #endif
        Display *dpy;
-       int screen;
+       int s, nlocks;
 
-       if ((argc == 2) && !strcmp("-v", argv[1]))
-               die("slock-%s, © 2006-2015 slock engineers\n", VERSION);
-       else if (argc != 1)
+       ARGBEGIN {
+       case 'v':
+               fprintf(stderr, "slock-"VERSION"\n");
+               return 0;
+       default:
                usage();
+       } ARGEND
 
 #ifdef __linux__
        dontkillme();
 #endif
 
-       if (!getpwuid(getuid()))
-               die("slock: no passwd entry for you\n");
+       /* Check if the current user has a password entry */
+       errno = 0;
+       if (!getpwuid(getuid())) {
+               if (errno == 0)
+                       die("slock: no password entry for current user\n");
+               else
+                       die("slock: getpwuid: %s\n", strerror(errno));
+       }
 
 #ifndef HAVE_BSD_AUTH
        pws = getpw();
 #endif
 
-       if (!(dpy = XOpenDisplay(0)))
+       if (!(dpy = XOpenDisplay(NULL)))
                die("slock: cannot open display\n");
+
+       /* check for Xrandr support */
        rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase);
-       /* Get the number of screens in display "dpy" and blank them all. */
+
+       /* get number of screens in display "dpy" and blank them */
        nscreens = ScreenCount(dpy);
-       locks = malloc(sizeof(Lock *) * nscreens);
-       if (locks == NULL)
-               die("slock: malloc: %s\n", strerror(errno));
-       int nlocks = 0;
-       for (screen = 0; screen < nscreens; screen++) {
-               if ( (locks[screen] = lockscreen(dpy, screen)) != NULL)
+       if (!(locks = malloc(sizeof(Lock *) * nscreens))) {
+               XCloseDisplay(dpy);
+               die("slock: out of memory\n");
+       }
+       for (nlocks = 0, s = 0; s < nscreens; s++) {
+               if ((locks[s] = lockscreen(dpy, s)) != NULL)
                        nlocks++;
        }
-       XSync(dpy, False);
+       XSync(dpy, 0);
 
-       /* Did we actually manage to lock something? */
-       if (nlocks == 0) { /* nothing to protect */
+       /* did we actually manage to lock anything? */
+       if (nlocks == 0) {
+               /* nothing to protect */
                free(locks);
                XCloseDisplay(dpy);
                return 1;
        }
 
-       /* Everything is now blank. Now wait for the correct password. */
+       /* run post-lock command */
+       if (argc > 0) {
+               switch (fork()) {
+               case -1:
+                       free(locks);
+                       XCloseDisplay(dpy);
+                       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);
+               }
+       }
+
+       /* everything is now blank. Wait for the correct password */
 #ifdef HAVE_BSD_AUTH
        readpw(dpy);
 #else
        readpw(dpy, pws);
 #endif
 
-       /* Password ok, unlock everything and quit. */
-       for (screen = 0; screen < nscreens; screen++)
-               unlockscreen(dpy, locks[screen]);
+       /* password ok, unlock everything and quit */
+       for (s = 0; s < nscreens; s++)
+               unlockscreen(dpy, locks[s]);
 
        free(locks);
        XCloseDisplay(dpy);