diff --git a/kill.c b/kill.c index 99478e4..ab29acb 100644 --- a/kill.c +++ b/kill.c @@ -55,9 +55,9 @@ static void maybe_notify(char* notif_command, char* notif_args) * Send the selected signal to "pid" and wait for the process to exit * (max 10 seconds) */ -int kill_wait(const poll_loop_args_t args, pid_t pid, int sig) +int kill_wait(const poll_loop_args_t *args, pid_t pid, int sig) { - if (args.dryrun && sig != 0) { + if (args->dryrun && sig != 0) { warn("dryrun, not actually sending any signal\n"); return 0; } @@ -78,7 +78,7 @@ int kill_wait(const poll_loop_args_t args, pid_t pid, int sig) if (sig != SIGKILL) { m = parse_meminfo(); print_mem_stats(debug, m); - if (m.MemAvailablePercent <= args.mem_kill_percent && m.SwapFreePercent <= args.swap_kill_percent) { + if (m.MemAvailablePercent <= args->mem_kill_percent && m.SwapFreePercent <= args->swap_kill_percent) { sig = SIGKILL; res = kill(pid, sig); // kill first, print after @@ -104,7 +104,7 @@ int kill_wait(const poll_loop_args_t args, pid_t pid, int sig) /* * Find the process with the largest oom_score and kill it. */ -void kill_largest_process(const poll_loop_args_t args, int sig) +void kill_largest_process(const poll_loop_args_t *args, int sig) { struct procinfo victim = { 0 }; struct timespec t0 = { 0 }, t1 = { 0 }; @@ -153,7 +153,7 @@ void kill_largest_process(const poll_loop_args_t args, int sig) } cur.badness = res; } - if (args.ignore_oom_score_adj) { + if (args->ignore_oom_score_adj) { int oom_score_adj = 0; int res = get_oom_score_adj(cur.pid, &oom_score_adj); if (res < 0) { @@ -165,16 +165,16 @@ void kill_largest_process(const poll_loop_args_t args, int sig) } } - if ((args.prefer_regex || args.avoid_regex)) { + if ((args->prefer_regex || args->avoid_regex)) { int res = get_comm(cur.pid, cur.name, sizeof(cur.name)); if (res < 0) { debug(" error reading process name: %s\n", strerror(-res)); continue; } - if (args.prefer_regex && regexec(args.prefer_regex, cur.name, (size_t)0, NULL, 0) == 0) { + if (args->prefer_regex && regexec(args->prefer_regex, cur.name, (size_t)0, NULL, 0) == 0) { cur.badness += BADNESS_PREFER; } - if (args.avoid_regex && regexec(args.avoid_regex, cur.name, (size_t)0, NULL, 0) == 0) { + if (args->avoid_regex && regexec(args->avoid_regex, cur.name, (size_t)0, NULL, 0) == 0) { cur.badness += BADNESS_AVOID; } } @@ -234,7 +234,7 @@ void kill_largest_process(const poll_loop_args_t args, int sig) if (victim.pid <= 0) { warn("Could not find a process to kill. Sleeping 1 second.\n"); - maybe_notify(args.notif_command, + maybe_notify(args->notif_command, "-i dialog-error 'earlyoom' 'Error: Could not find a process to kill. Sleeping 1 second.'"); sleep(1); return; @@ -271,7 +271,7 @@ void kill_largest_process(const poll_loop_args_t args, int sig) sanitize(victim.name); snprintf(notif_args, sizeof(notif_args), "-i dialog-warning 'earlyoom' 'Low memory! Killing process %d %s'", victim.pid, victim.name); - maybe_notify(args.notif_command, notif_args); + maybe_notify(args->notif_command, notif_args); } if (sig == 0) { @@ -280,7 +280,7 @@ void kill_largest_process(const poll_loop_args_t args, int sig) if (res != 0) { warn("kill failed: %s\n", strerror(saved_errno)); - maybe_notify(args.notif_command, + maybe_notify(args->notif_command, "-i dialog-error 'earlyoom' 'Error: Failed to kill process'"); // Killing the process may have failed because we are not running as root. // In that case, trying again in 100ms will just yield the same error. diff --git a/kill.h b/kill.h index ac0b623..baa29b1 100644 --- a/kill.h +++ b/kill.h @@ -25,6 +25,6 @@ typedef struct { bool dryrun; } poll_loop_args_t; -void kill_largest_process(poll_loop_args_t args, int sig); +void kill_largest_process(const poll_loop_args_t *args, int sig); #endif diff --git a/main.c b/main.c index b2b8cfc..0cefd23 100644 --- a/main.c +++ b/main.c @@ -39,7 +39,7 @@ enum { }; static int set_oom_score_adj(int); -static void poll_loop(const poll_loop_args_t args); +static void poll_loop(const poll_loop_args_t *args); // Prevent Golang / Cgo name collision when the test suite runs - // Cgo generates it's own main function. @@ -294,7 +294,7 @@ int main(int argc, char* argv[]) * calling mlockall() */ debug("dry-running kill_largest_process()...\n"); - kill_largest_process(args, 0); + kill_largest_process(&args, 0); int err = mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT); // kernels older than 4.4 don't support MCL_ONFAULT. Retry without it. @@ -306,7 +306,7 @@ int main(int argc, char* argv[]) } // Jump into main poll loop - poll_loop(args); + poll_loop(&args); return 0; } @@ -368,7 +368,7 @@ static unsigned sleep_time_ms(const poll_loop_args_t* args, const meminfo_t* m) return (unsigned)ms; } -static void poll_loop(const poll_loop_args_t args) +static void poll_loop(const poll_loop_args_t *args) { // Print a a memory report when this reaches zero. We start at zero so // we print the first report immediately. @@ -377,24 +377,24 @@ static void poll_loop(const poll_loop_args_t args) while (1) { int sig = 0; meminfo_t m = parse_meminfo(); - if (m.MemAvailablePercent <= args.mem_kill_percent && m.SwapFreePercent <= args.swap_kill_percent) { + if (m.MemAvailablePercent <= args->mem_kill_percent && m.SwapFreePercent <= args->swap_kill_percent) { print_mem_stats(warn, m); warn("low memory! at or below SIGKILL limits: mem " PRIPCT ", swap " PRIPCT "\n", - args.mem_kill_percent, args.swap_kill_percent); + args->mem_kill_percent, args->swap_kill_percent); sig = SIGKILL; - } else if (m.MemAvailablePercent <= args.mem_term_percent && m.SwapFreePercent <= args.swap_term_percent) { + } else if (m.MemAvailablePercent <= args->mem_term_percent && m.SwapFreePercent <= args->swap_term_percent) { print_mem_stats(warn, m); warn("low memory! at or below SIGTERM limits: mem " PRIPCT ", swap " PRIPCT "\n", - args.mem_term_percent, args.swap_term_percent); + args->mem_term_percent, args->swap_term_percent); sig = SIGTERM; } if (sig) { kill_largest_process(args, sig); - } else if (args.report_interval_ms && report_countdown_ms <= 0) { + } else if (args->report_interval_ms && report_countdown_ms <= 0) { print_mem_stats(printf, m); - report_countdown_ms = args.report_interval_ms; + report_countdown_ms = args->report_interval_ms; } - unsigned sleep_ms = sleep_time_ms(&args, &m); + unsigned sleep_ms = sleep_time_ms(args, &m); debug("adaptive sleep time: %d ms\n", sleep_ms); usleep(sleep_ms * 1000); report_countdown_ms -= (int)sleep_ms; diff --git a/testsuite_c_wrappers.go b/testsuite_c_wrappers.go index 61ba09a..23ea9fb 100644 --- a/testsuite_c_wrappers.go +++ b/testsuite_c_wrappers.go @@ -44,7 +44,7 @@ func parse_meminfo() C.meminfo_t { func kill_largest_process() { var args C.poll_loop_args_t - C.kill_largest_process(args, 0) + C.kill_largest_process(&args, 0) } func get_oom_score(pid int) int {