Skip to content

Commit

Permalink
Don't kill ourselves when hidepid is set
Browse files Browse the repository at this point in the history
If you use hidepid=1 or hidepid=2 for your /proc filesystem,
earlyoom running as a normal user can no longer see (hidepid=2)
or look at the memory usage (hidepid=1) of running processes.

Detect this sitatuation by counting candidate processes.
Iff there is only one candidate and it is ourselves,
don't kill ourselves.

#184
  • Loading branch information
rfjakob committed Apr 11, 2020
1 parent 4179ebc commit 571433b
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions kill.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ void kill_largest_process(const poll_loop_args_t* args, int sig)
fatal(5, "Could not open /proc: %s", strerror(errno));
}

int candidates = 0;
while (1) {
errno = 0;
struct dirent* d = readdir(procdir);
Expand Down Expand Up @@ -190,6 +191,7 @@ void kill_largest_process(const poll_loop_args_t* args, int sig)
}

debug(" badness %3d", cur.badness);
candidates++;

if (cur.badness < victim.badness) {
// skip "type 1", encoded as 1 space
Expand Down Expand Up @@ -242,6 +244,12 @@ void kill_largest_process(const poll_loop_args_t* args, int sig)
} // end of while(1) loop
closedir(procdir);

if (candidates <= 1 && victim.pid == getpid()) {
warn("Only found myself (pid %d) in /proc. Do you use hidpid? See https://github.com/rfjakob/earlyoom/wiki/proc-hidepid\n",
victim.pid);
victim.pid = 0;
}

if (victim.pid <= 0) {
warn("Could not find a process to kill. Sleeping 1 second.\n");
if (args->notify) {
Expand Down

0 comments on commit 571433b

Please sign in to comment.