mirror of
https://github.com/mintycube/slock.git
synced 2024-10-22 14:05:51 +02:00
Refactor dontkillme()
- Use file pointers instead of raw I/O, inspired by Kernel code. - Use OOM_SCORE_ADJ_MIN from linux/oom.h instead of working with magic values. - Stricter error checking and descriptive error messages. The reasoning for using the constant rather than magic values lies in the fact that this ensures people get the message. With "-1000", a code reviewer would question if that is really the lowest possible number or just an arbitrary value. The kernel ABI probably won't change, but even in the case, we wouldn't have to modify the code. The OOM killer only is guaranteed to not kill you if you have OOM_SCORE_ADJ_MIN.
This commit is contained in:
parent
a9eddbd94f
commit
137f0076c2
20
slock.c
20
slock.c
@ -65,19 +65,27 @@ die(const char *errstr, ...)
|
|||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <linux/oom.h>
|
||||||
|
|
||||||
static void
|
static void
|
||||||
dontkillme(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 (!(f = fopen(oomfile, "w"))) {
|
||||||
if (fd < 0 && errno == ENOENT) {
|
if (errno == ENOENT)
|
||||||
return;
|
return;
|
||||||
|
die("slock: fopen %s: %s\n", oomfile, strerror(errno));
|
||||||
}
|
}
|
||||||
if (fd < 0 || write(fd, "-1000\n", (sizeof("-1000\n") - 1)) !=
|
fprintf(f, "%d", OOM_SCORE_ADJ_MIN);
|
||||||
(sizeof("-1000\n") - 1) || close(fd) != 0) {
|
if (fclose(f)) {
|
||||||
die("can't tame the oom-killer. is suid or sgid set?\n");
|
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
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user