X-Git-Url: https://git.danieliu.xyz/?p=slock.git;a=blobdiff_plain;f=slock.c;h=9ec2f717ec00865389303c9893408e3e0d2a5ff4;hp=3e2ab8f7511fec94ae2e94e3810575ecca8125ca;hb=137f0076c2986306109d637599b885bdaf92cd58;hpb=a9eddbd94fb03a36186ef2b1e784468dfcddbc19 diff --git a/slock.c b/slock.c index 3e2ab8f..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