Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option -g: kill all processes within a process group #247

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions MANPAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ When earlyoom is run through its default systemd service, the `-p` switch doesn'
#### -n
Enable notifications via d-bus.

#### -g
Kill all processes that are in the same process group as the one with excessive
memory usage.

For example, with this flag turned on, the whole application will be killed when
one of its subprocess consumes too much memory (as long as they all have the
same PGID, of course).

Enable this flag when completely cleaning up the "entire process" is more desirable.

Notice that some desktop environments (GNOME, for example) put every desktop
application in the same process group as `gnome-shell` does. EarlyOOM might kill
all such processes when this flag is turned on. Be sure to check how your environment
behaves beforehand.

#### \-\-prefer REGEX
prefer killing processes matching REGEX (adds 300 to oom_score)

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ Usage: ./earlyoom [OPTION]...
-i user-space oom killer should ignore positive
oom_score_adj values
-n enable d-bus notifications
-g kill all processes within a process group
-d enable debugging messages
-v print version information and exit
-r INTERVAL memory report interval in seconds (default 1), set
Expand Down
6 changes: 6 additions & 0 deletions kill.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ int kill_wait(const poll_loop_args_t* args, pid_t pid, int sig)
}
meminfo_t m = { 0 };
const unsigned poll_ms = 100;
if (args->kill_process_group) {
if ((pid = getpgid(pid)) < 0) {
return pid;
}
pid = -pid;
}
int res = kill(pid, sig);
if (res != 0) {
return res;
Expand Down
2 changes: 2 additions & 0 deletions kill.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ typedef struct {
bool ignore_oom_score_adj;
/* send d-bus notifications? */
bool notify;
/* kill all processes within a process group */
bool kill_process_group;
/* prefer/avoid killing these processes. NULL = no-op. */
regex_t* prefer_regex;
regex_t* avoid_regex;
Expand Down
6 changes: 5 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ int main(int argc, char* argv[])
meminfo_t m = parse_meminfo();

int c;
const char* short_opt = "m:s:M:S:kinN:dvr:ph";
const char* short_opt = "m:s:M:S:kingN:dvr:ph";
struct option long_opt[] = {
{ "prefer", required_argument, NULL, LONG_OPT_PREFER },
{ "avoid", required_argument, NULL, LONG_OPT_AVOID },
Expand Down Expand Up @@ -173,6 +173,9 @@ int main(int argc, char* argv[])
args.notify = true;
fprintf(stderr, "Notifying through D-Bus\n");
break;
case 'g':
args.kill_process_group = true;
break;
case 'N':
args.notify = true;
fprintf(stderr, "Notifying through D-Bus, argument '%s' ignored for compatability\n", optarg);
Expand Down Expand Up @@ -220,6 +223,7 @@ int main(int argc, char* argv[])
" -i user-space oom killer should ignore positive\n"
" oom_score_adj values\n"
" -n enable d-bus notifications\n"
" -g kill all processes within a process group\n"
" -d enable debugging messages\n"
" -v print version information and exit\n"
" -r INTERVAL memory report interval in seconds (default 1), set\n"
Expand Down