]> git.armaanb.net Git - slock.git/blobdiff - slock.c
Add cleanup() to do free(locks) + XCloseDisplay()
[slock.git] / slock.c
diff --git a/slock.c b/slock.c
index 3e2ab8f7511fec94ae2e94e3810575ecca8125ca..4980325f9fd9df80528cb256e6cf4aa6459a8a2c 100644 (file)
--- a/slock.c
+++ b/slock.c
@@ -65,19 +65,27 @@ die(const char *errstr, ...)
 
 #ifdef __linux__
 #include <fcntl.h>
+#include <linux/oom.h>
 
 static void
 dontkillme(void)
 {
-       int fd;
+       FILE *f;
+       const char oomfile[] = "/proc/self/oom_score_adj";
 
-       fd = open("/proc/self/oom_score_adj", O_WRONLY);
-       if (fd < 0 && errno == ENOENT) {
-               return;
+       if (!(f = fopen(oomfile, "w"))) {
+               if (errno == ENOENT)
+                       return;
+               die("slock: fopen %s: %s\n", oomfile, strerror(errno));
        }
-       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");
+       fprintf(f, "%d", OOM_SCORE_ADJ_MIN);
+       if (fclose(f)) {
+               if (errno == EACCES)
+                       die("slock: unable to disable OOM killer. "
+                           "suid or sgid set?\n");
+               else
+                       die("slock: fclose %s: %s\n", oomfile,
+                           strerror(errno));
        }
 }
 #endif
@@ -222,6 +230,13 @@ unlockscreen(Display *dpy, Lock *lock)
        free(lock);
 }
 
+static void
+cleanup(Display *dpy)
+{
+       free(locks);
+       XCloseDisplay(dpy);
+}
+
 static Lock *
 lockscreen(Display *dpy, int screen)
 {
@@ -341,8 +356,7 @@ main(int argc, char **argv) {
        /* did we actually manage to lock anything? */
        if (nlocks == 0) {
                /* nothing to protect */
-               free(locks);
-               XCloseDisplay(dpy);
+               cleanup(dpy);
                return 1;
        }
 
@@ -350,8 +364,7 @@ main(int argc, char **argv) {
        if (argc > 0) {
                switch (fork()) {
                case -1:
-                       free(locks);
-                       XCloseDisplay(dpy);
+                       cleanup(dpy);
                        die("slock: fork failed: %s\n", strerror(errno));
                case 0:
                        if (close(ConnectionNumber(dpy)) < 0)
@@ -374,8 +387,7 @@ main(int argc, char **argv) {
        for (s = 0; s < nscreens; s++)
                unlockscreen(dpy, locks[s]);
 
-       free(locks);
-       XCloseDisplay(dpy);
+       cleanup(dpy);
 
        return 0;
 }