diff options
| -rw-r--r-- | slock.c | 20 |
1 files changed, 6 insertions, 14 deletions
| @@ -60,28 +60,20 @@ die(const char *errstr, ...) | |||
| 60 | 60 | ||
| 61 | #ifdef __linux__ | 61 | #ifdef __linux__ |
| 62 | #include <fcntl.h> | 62 | #include <fcntl.h> |
| 63 | #include <linux/oom.h> | ||
| 64 | 63 | ||
| 65 | static void | 64 | static void |
| 66 | dontkillme(void) | 65 | dontkillme(void) |
| 67 | { | 66 | { |
| 68 | int fd; | 67 | int fd; |
| 69 | int length; | ||
| 70 | char value[64]; | ||
| 71 | 68 | ||
| 72 | fd = open("/proc/self/oom_score_adj", O_WRONLY); | 69 | fd = open("/proc/self/oom_score_adj", O_WRONLY); |
| 73 | if (fd < 0 && errno == ENOENT) | 70 | if (fd < 0 && errno == ENOENT) { |
| 74 | return; | 71 | return; |
| 75 | 72 | } | |
| 76 | /* convert OOM_SCORE_ADJ_MIN to string for writing */ | 73 | if (fd < 0 || write(fd, "-1000\n", (sizeof("-1000\n") - 1)) != |
| 77 | length = snprintf(value, sizeof(value), "%d\n", OOM_SCORE_ADJ_MIN); | 74 | (sizeof("-1000\n") - 1) || close(fd) != 0) { |
| 78 | 75 | die("can't tame the oom-killer. is suid or sgid set?\n"); | |
| 79 | /* bail on truncation */ | 76 | } |
| 80 | if (length >= sizeof(value)) | ||
| 81 | die("buffer too small\n"); | ||
| 82 | |||
| 83 | if (fd < 0 || write(fd, value, length) != length || close(fd) != 0) | ||
| 84 | die("cannot disable the out-of-memory killer for this process (make sure to suid or sgid slock)\n"); | ||
| 85 | } | 77 | } |
| 86 | #endif | 78 | #endif |
| 87 | 79 | ||
