Skip to content

Commit

Permalink
oom-score: restore oom score before running exit command
Browse files Browse the repository at this point in the history
When the exit command is executed, the oom score is restored to its
original value otherwise the command runs with -1000.

Closes: containers/podman#20765

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Nov 23, 2023
1 parent f0eb924 commit 2deff03
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/conmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ int main(int argc, char *argv[])
_cleanup_gerror_ GError *err = NULL;
char buf[BUF_SIZE];
int num_read;
int old_oom_score = 0;
_cleanup_close_ int dev_null_r_cleanup = -1;
_cleanup_close_ int dev_null_w_cleanup = -1;
_cleanup_close_ int dummyfd = -1;
Expand All @@ -55,7 +54,7 @@ int main(int argc, char *argv[])

process_cli();

attempt_oom_adjust(-1000, &old_oom_score);
attempt_oom_adjust(-1000);

/* ignoring SIGPIPE prevents conmon from being spuriously killed */
signal(SIGPIPE, SIG_IGN);
Expand Down Expand Up @@ -295,7 +294,7 @@ int main(int argc, char *argv[])
}

// We don't want runc to be unkillable so we reset the oom_score_adj back to 0
attempt_oom_adjust(old_oom_score, NULL);
reset_oom_adjust();
execv(g_ptr_array_index(runtime_argv, 0), (char **)runtime_argv->pdata);
exit(127);
}
Expand Down
3 changes: 3 additions & 0 deletions src/ctr_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "globals.h"
#include "ctr_logging.h"
#include "close_fds.h"
#include "oom.h"

#include <errno.h>
#include <glib.h>
Expand Down Expand Up @@ -201,6 +202,8 @@ void do_exit_command()
sleep(opt_exit_delay);
}

reset_oom_adjust();

execv(opt_exit_command, args);

/* Should not happen, but better be safe. */
Expand Down
6 changes: 6 additions & 0 deletions src/ctr_logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,8 @@ static void reopen_k8s_file(void)
return;

_cleanup_free_ char *k8s_log_path_tmp = g_strdup_printf("%s.tmp", k8s_log_path);
_cleanup_free_ char *k8s_log_path_tmp_old = g_strdup_printf("%s.tmp.old", k8s_log_path);
_cleanup_free_ char *k8s_log_path_old = g_strdup_printf("%s.1", k8s_log_path);

/* Sync the logs to disk */
if (!opt_no_sync_log && fsync(k8s_log_fd) < 0) {
Expand All @@ -614,6 +616,10 @@ static void reopen_k8s_file(void)
if (k8s_log_fd < 0)
pexitf("Failed to open log file %s", k8s_log_path);

unlink(k8s_log_path_tmp_old);
link(k8s_log_path, k8s_log_path_tmp_old);
rename(k8s_log_path_tmp_old, k8s_log_path_old);

/* Replace the previous file */
if (rename(k8s_log_path_tmp, k8s_log_path) < 0) {
pexit("Failed to rename log file");
Expand Down
14 changes: 13 additions & 1 deletion src/oom.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <string.h>
#include <unistd.h>

void attempt_oom_adjust(int oom_score, int *old_value)
int old_oom_score = 0;

static void write_oom_adjust(int oom_score, int *old_value)
{
#ifdef __linux__
char fmt_oom_score[16];
Expand All @@ -30,3 +32,13 @@ void attempt_oom_adjust(int oom_score, int *old_value)
(void)old_value;
#endif
}

void attempt_oom_adjust(int oom_score)
{
write_oom_adjust(oom_score, &old_oom_score);
}

void reset_oom_adjust()
{
write_oom_adjust(old_oom_score, NULL);
}
3 changes: 2 additions & 1 deletion src/oom.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if !defined(OOM_H)
#define OOM_H

void attempt_oom_adjust(int oom_score, int *old_value);
void attempt_oom_adjust(int oom_score);
void reset_oom_adjust();

#endif // OOM_H

0 comments on commit 2deff03

Please sign in to comment.