Skip to content

Commit 8f4b654

Browse files
TaWeiTurfjakob
authored andcommitted
Add option -g: Kill processes in a process group
#247
1 parent 17cd327 commit 8f4b654

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

MANPAGE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,27 @@ When earlyoom is run through its default systemd service, the `-p` switch doesn'
104104
#### -n
105105
Enable notifications via d-bus.
106106

107+
#### -g
108+
Kill all processes that have same process group id (PGID) as the process
109+
with excessive memory usage.
110+
111+
For example, with this flag turned on, the whole application will be killed when
112+
one of its subprocess consumes too much memory (as long as they all have the
113+
same PGID, of course).
114+
115+
Enable this flag when completely cleaning up the "entire application" is more desirable,
116+
and you are sure that the application puts all its processes in the same PGID.
117+
118+
Note that some desktop environments (GNOME, for example) put all desktop
119+
application in the same process group as `gnome-shell`. earlyoom might kill
120+
all such processes including `gnome-shell` when this flag is turned on.
121+
122+
Be sure to check how your environment behaves beforehand. Use
123+
124+
pstree -gT
125+
126+
to show all processes with the PGID in brackets.
127+
107128
#### \-\-prefer REGEX
108129
prefer killing processes matching REGEX (adds 300 to oom_score)
109130

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ Usage: ./earlyoom [OPTION]...
245245
-i user-space oom killer should ignore positive
246246
oom_score_adj values
247247
-n enable d-bus notifications
248+
-g kill all processes within a process group
248249
-d enable debugging messages
249250
-v print version information and exit
250251
-r INTERVAL memory report interval in seconds (default 1), set

kill.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ int kill_wait(const poll_loop_args_t* args, pid_t pid, int sig)
7373
}
7474
meminfo_t m = { 0 };
7575
const unsigned poll_ms = 100;
76+
if (args->kill_process_group) {
77+
int res = getpgid(pid);
78+
if (res < 0) {
79+
return res;
80+
}
81+
pid = -res;
82+
warn("killing whole process group %d (-g flag is active)\n", pid);
83+
}
7684
int res = kill(pid, sig);
7785
if (res != 0) {
7886
return res;

kill.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ typedef struct {
1818
bool ignore_oom_score_adj;
1919
/* send d-bus notifications? */
2020
bool notify;
21+
/* kill all processes within a process group */
22+
bool kill_process_group;
2123
/* prefer/avoid killing these processes. NULL = no-op. */
2224
regex_t* prefer_regex;
2325
regex_t* avoid_regex;

main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ int main(int argc, char* argv[])
101101
meminfo_t m = parse_meminfo();
102102

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

0 commit comments

Comments
 (0)