Skip to content

Commit

Permalink
introduce --oom-score-adj
Browse files Browse the repository at this point in the history
  • Loading branch information
vzxv committed Apr 5, 2023
1 parent ddc9036 commit c23f130
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
27 changes: 27 additions & 0 deletions enter.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ static void apply_rlimit(int resource, struct rlimit const *value)
}
}

ssize_t write_oom_score_adj(long value)
{
int fd = open("/proc/self/oom_score_adj", O_WRONLY);
if (fd == -1) {
return -1;
}

char data[BUFSIZ];
if ((size_t) snprintf(data, sizeof (data), "%ld\n", value) >= sizeof (data)) {
errx(1, "'%ld\n' takes more than %zu bytes.", value, sizeof (data));
}

ssize_t written = write(fd, data, sizeof(data));
if (written == -1) {
return -1;
}

if (close(fd) == -1) {
return -1;
}
return written;
}

static int sig_handler(int epollfd, const struct epoll_event *ev, int fd, pid_t pid)
{
siginfo_t info;
Expand Down Expand Up @@ -315,6 +338,10 @@ int enter(struct entry_settings *opts)
err(1, "close timens_offsets");
}

if (write_oom_score_adj(opts->oom_score_adj) == -1) {
err(1, "writing oom_score_adj");
}

/* Setup a socket pair for file-descriptor passing. Used by pty allocation. */
enum {
SOCKET_PARENT,
Expand Down
2 changes: 2 additions & 0 deletions enter.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ struct entry_settings {
struct climit climits[MAX_CGROUPS];
size_t nactiveclimits;

long oom_score_adj;

const char *arch;

struct bst_rlimit rlimits[BST_NLIMIT];
Expand Down
9 changes: 9 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ enum {
OPTION_CLIMIT,
OPTION_CLIMIT_TRY,
OPTION_PIDFILE,
OPTION_OOM_SCORE_ADJ,
OPTION_IP,
OPTION_ROUTE,
OPTION_NO_FAKE_DEVTMPFS,
Expand Down Expand Up @@ -304,6 +305,7 @@ int main(int argc, char *argv[], char *envp[])
{ "cgroup", required_argument, NULL, OPTION_CGROUP },
{ "limit", required_argument, NULL, OPTION_CLIMIT },
{ "try-limit", required_argument, NULL, OPTION_CLIMIT_TRY },
{ "oom-score-adj", required_argument, NULL, OPTION_OOM_SCORE_ADJ },
{ "pidfile", required_argument, NULL, OPTION_PIDFILE },
{ "ip", required_argument, NULL, OPTION_IP },
{ "route", required_argument, NULL, OPTION_ROUTE },
Expand Down Expand Up @@ -424,6 +426,13 @@ int main(int argc, char *argv[], char *envp[])
handle_climit_arg(&opts, optarg, false);
break;

case OPTION_OOM_SCORE_ADJ:
opts.oom_score_adj = strtol(optarg, NULL, 10);
if (errno != 0) {
errx(1, "oom score adjustment must be an integer");
}
break;

case OPTION_SHARE:
process_share(opts.shares, optarg);
break;
Expand Down
1 change: 1 addition & 0 deletions outer.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ void outer_helper_spawn(struct outer_helper *helper)
}

unshare:

if (helper->unshare_user) {
burn_uidmap_gidmap(child_pid, helper->uid_desired, helper->gid_desired);
}
Expand Down
1 change: 1 addition & 0 deletions usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Options:
--rlimit <res>=<hard>:<soft>,
--rlimit <res>=<value> Set the specified resource to the provided
value(s).
--oom-score-adj <value> Adjust the OOM score for the process.
--pidfile <path> Write PID in file and exclusive-lock it.
--tty[=<options>] Allocate a pty for the process.
--close-fd[=<fd>|<from>-[to]] Close specified file descriptor or fd range
Expand Down

0 comments on commit c23f130

Please sign in to comment.