X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=slock.c;h=9ec2f717ec00865389303c9893408e3e0d2a5ff4;hb=137f0076c2986306109d637599b885bdaf92cd58;hp=847b3285f5a75af8cd62dfa30f90eeadc64b4a59;hpb=b87bfa234378bcfc1b13273c5089f07902de1725;p=slock.git diff --git a/slock.c b/slock.c index 847b328..9ec2f71 100644 --- a/slock.c +++ b/slock.c @@ -65,19 +65,27 @@ die(const char *errstr, ...) #ifdef __linux__ #include +#include 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 @@ -123,7 +131,7 @@ readpw(Display *dpy) readpw(Display *dpy, const char *pws) #endif { - char buf[32], passwd[256]; + char buf[32], passwd[256], *encrypted; int num, screen; unsigned int len, color; KeySym ksym; @@ -159,7 +167,11 @@ readpw(Display *dpy, const char *pws) #ifdef HAVE_BSD_AUTH running = !auth_userokay(getlogin(), NULL, "auth-slock", passwd); #else - running = !!strcmp(crypt(passwd, pws), pws); + errno = 0; + if (!(encrypted = crypt(passwd, pws))) + fprintf(stderr, "slock: crypt: %s\n", strerror(errno)); + else + running = !!strcmp(encrypted, pws); #endif if (running) { XBell(dpy, 100); @@ -278,7 +290,7 @@ lockscreen(Display *dpy, int screen) static void usage(void) { - die("usage: slock [-v | cmd [arg ...]]\n"); + die("usage: slock [-v] [cmd [arg ...]]\n"); } int @@ -312,6 +324,8 @@ main(int argc, char **argv) { #ifndef HAVE_BSD_AUTH pws = getpw(); + if (strlen(pws) < 2) + die("slock: failed to get user password hash.\n"); #endif if (!(dpy = XOpenDisplay(NULL)))