Skip to content

Commit

Permalink
userspace_kill: fix display of victim process name
Browse files Browse the repository at this point in the history
Without this patch, the while (1) loop may overwrite the buffer 'name'
before the loop is broken. As a result, incorrect victim name is emitted
to stderr. This patch introduces a new buffer 'victim_name' which holds
the victim name similar to victim_pid, victim_badness and victim_vm_rss.
  • Loading branch information
mikkorantalainen authored and rfjakob committed Feb 4, 2018
1 parent 6e99060 commit 7634c5b
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions kill.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ static void userspace_kill(DIR* procdir, int sig, int ignore_oom_score_adj,
int victim_badness = 0;
unsigned long victim_vm_rss = 0;
char name[PATH_MAX];
char victim_name[PATH_MAX] = { 0 };
struct procinfo p;
int badness;

Expand Down Expand Up @@ -176,15 +177,17 @@ static void userspace_kill(DIR* procdir, int sig, int ignore_oom_score_adj,
victim_pid = pid;
victim_badness = badness;
victim_vm_rss = p.vm_rss;
strncpy(victim_name, name, sizeof(victim_name) - 1);
if (enable_debug)
printf(" ^ new victim (higher badness)\n");
} else if (badness == victim_badness && p.vm_rss > victim_vm_rss) {
victim_pid = pid;
victim_vm_rss = p.vm_rss;
strncpy(victim_name, name, sizeof(victim_name) - 1);
if (enable_debug)
printf(" ^ new victim (higher vm_rss)\n");
}
}
} // end of while(1) loop

if (victim_pid == 0) {
fprintf(stderr, "Error: Could not find a process to kill. Sleeping 10 seconds.\n");
Expand All @@ -194,7 +197,7 @@ static void userspace_kill(DIR* procdir, int sig, int ignore_oom_score_adj,
}

if (sig != 0) {
fprintf(stderr, "Killing process %d %s\n", victim_pid, name);
fprintf(stderr, "Killing process %d %s\n", victim_pid, victim_name);

char notif_args[200];
snprintf(notif_args, 200, "-i dialog-warning 'earlyoom' 'Killing process %d %s'", victim_pid, name);
Expand Down

0 comments on commit 7634c5b

Please sign in to comment.