Skip to content

Commit

Permalink
oom: restore old OOM score
Browse files Browse the repository at this point in the history
restore the original OOM score before running the OCI runtime so that
the container process will inherit the OOM score from the environment
instead of the default value of 0.

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe authored and haircommander committed Mar 20, 2023
1 parent 9d3a955 commit 813c8d7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/conmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ 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 @@ -54,7 +55,7 @@ int main(int argc, char *argv[])

process_cli();

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

/* ignoring SIGPIPE prevents conmon from being spuriously killed */
signal(SIGPIPE, SIG_IGN);
Expand Down Expand Up @@ -290,7 +291,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("0");
attempt_oom_adjust(old_oom_score, NULL);
execv(g_ptr_array_index(runtime_argv, 0), (char **)runtime_argv->pdata);
exit(127);
}
Expand Down
14 changes: 11 additions & 3 deletions src/oom.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@
#include <string.h>
#include <unistd.h>

void attempt_oom_adjust(const char *const oom_score)
void attempt_oom_adjust(int oom_score, int *old_value)
{
#ifdef __linux__
int oom_score_fd = open("/proc/self/oom_score_adj", O_WRONLY);
char fmt_oom_score[16];
int oom_score_fd = open("/proc/self/oom_score_adj", O_RDWR);
if (oom_score_fd < 0) {
ndebugf("failed to open /proc/self/oom_score_adj: %s\n", strerror(errno));
return;
}
if (write(oom_score_fd, oom_score, strlen(oom_score)) < 0) {
if (old_value) {
if (read(oom_score_fd, fmt_oom_score, sizeof(fmt_oom_score)) < 0) {
ndebugf("failed to read from /proc/self/oom_score_adj: %s\n", strerror(errno));
}
*old_value = atoi(fmt_oom_score);
}
sprintf(fmt_oom_score, "%d", oom_score);
if (write(oom_score_fd, fmt_oom_score, strlen(fmt_oom_score)) < 0) {
ndebugf("failed to write to /proc/self/oom_score_adj: %s\n", strerror(errno));
}
close(oom_score_fd);
Expand Down
2 changes: 1 addition & 1 deletion src/oom.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#if !defined(OOM_H)
#define OOM_H

void attempt_oom_adjust(const char *const oom_score);
void attempt_oom_adjust(int oom_score, int *old_value);

#endif // OOM_H

0 comments on commit 813c8d7

Please sign in to comment.