]> git.armaanb.net Git - slock.git/blobdiff - slock.c
add hint for suid to oom error message
[slock.git] / slock.c
diff --git a/slock.c b/slock.c
index d6053afc912908680af1bcac9c9595134abe3070..fa7ec8428afb91be37198d7c2b035539a6719534 100644 (file)
--- a/slock.c
+++ b/slock.c
@@ -60,17 +60,28 @@ die(const char *errstr, ...)
 
 #ifdef __linux__
 #include <fcntl.h>
+#include <linux/oom.h>
 
 static void
 dontkillme(void)
 {
        int fd;
+       int length;
+       char value[64];
 
        fd = open("/proc/self/oom_score_adj", O_WRONLY);
        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");
+
+       /* convert OOM_SCORE_ADJ_MIN to string for writing */
+       length = snprintf(value, sizeof(value), "%d\n", OOM_SCORE_ADJ_MIN);
+
+       /* bail on truncation */
+       if (length >= sizeof(value))
+               die("buffer too small\n");
+
+       if (fd < 0 || write(fd, value, length) != length || close(fd) != 0)
+               die("cannot disable the out-of-memory killer for this process (make sure to suid or sgid slock)\n");
 }
 #endif
 
@@ -83,20 +94,18 @@ 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));
                else
                        die("slock: 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)
+               if (!(sp = getspnam(getenv("USER"))))
                        die("slock: cannot retrieve shadow entry (make sure to suid or sgid slock)\n");
                rval = sp->sp_pwdp;
        }
@@ -169,14 +178,14 @@ readpw(Display *dpy, const char *pws)
                                        --len;
                                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;
                        }
                        color = len ? INPUT : (failure || failonclear ? FAILED : INIT);
-                       if (oldc != color) {
+                       if (running && oldc != color) {
                                for (screen = 0; screen < nscreens; screen++) {
                                        XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[color]);
                                        XClearWindow(dpy, locks[screen]->win);
@@ -221,15 +230,10 @@ lockscreen(Display *dpy, int screen)
        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,36 +253,37 @@ lockscreen(Display *dpy, int screen)
        XMapRaised(dpy, lock->win);
        if (rr)
                XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
+
+       /* Try to grab mouse pointer *and* keyboard, else fail the lock */
        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)) {
+       if (!len) {
+               fprintf(stderr, "slock: unable to grab mouse pointer for screen %d\n", screen);
+       } else {
                for (len = 1000; len; len--) {
-                       if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
-                               break;
+                       if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) {
+                               /* everything fine, we grabbed both inputs */
+                               XSelectInput(dpy, lock->root, SubstructureNotifyMask);
+                               return lock;
+                       }
                        usleep(1000);
                }
+               fprintf(stderr, "slock: unable to grab keyboard for screen %d\n", screen);
        }
-
-       running &= (len > 0);
-       if (!running) {
-               unlockscreen(dpy, lock);
-               lock = NULL;
-       }
-       else {
-               XSelectInput(dpy, lock->root, SubstructureNotifyMask);
-       }
-
-       return lock;
+       /* grabbing one of the inputs failed */
+       running = 0;
+       unlockscreen(dpy, lock);
+       return NULL;
 }
 
 static void
 usage(void)
 {
-       fprintf(stderr, "usage: slock [-v]\n");
+       fprintf(stderr, "usage: slock [-v|POST_LOCK_CMD]\n");
        exit(1);
 }
 
@@ -292,7 +297,8 @@ main(int argc, char **argv) {
 
        if ((argc == 2) && !strcmp("-v", argv[1]))
                die("slock-%s, © 2006-2015 slock engineers\n", VERSION);
-       else if (argc != 1)
+
+       if ((argc == 2) && !strcmp("-h", argv[1]))
                usage();
 
 #ifdef __linux__
@@ -311,12 +317,11 @@ main(int argc, char **argv) {
        rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase);
        /* Get the number of screens in display "dpy" and blank them all. */
        nscreens = ScreenCount(dpy);
-       locks = malloc(sizeof(Lock *) * nscreens);
-       if (locks == NULL)
+       if (!(locks = malloc(sizeof(Lock*) * nscreens)))
                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[screen] = lockscreen(dpy, screen)) != NULL)
                        nlocks++;
        }
        XSync(dpy, False);
@@ -328,6 +333,13 @@ main(int argc, char **argv) {
                return 1;
        }
 
+       if (argc >= 2 && fork() == 0) {
+               if (dpy)
+                       close(ConnectionNumber(dpy));
+               execvp(argv[1], argv+1);
+               die("slock: execvp %s failed: %s\n", argv[1], strerror(errno));
+       }
+
        /* Everything is now blank. Now wait for the correct password. */
 #ifdef HAVE_BSD_AUTH
        readpw(dpy);