From: Lennart Poettering Date: Mon, 7 May 2018 18:44:41 +0000 (+0200) Subject: process-util: add new helper call for adjusting the OOM score X-Git-Tag: v239~243^2~7 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9f8168eb233d00b10e7d8ae25d9fb4c9688b4bc6;p=platform%2Fupstream%2Fsystemd.git process-util: add new helper call for adjusting the OOM score And let's make use of it in execute.c --- diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 4180769..ce56ce0 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -1445,6 +1445,15 @@ int fork_agent(const char *name, const int except[], size_t n_except, pid_t *ret _exit(EXIT_FAILURE); } +int set_oom_score_adjust(int value) { + char t[DECIMAL_STR_MAX(int)]; + + sprintf(t, "%i", value); + + return write_string_file("/proc/self/oom_score_adj", t, + WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_DISABLE_BUFFER); +} + static const char *const ioprio_class_table[] = { [IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_RT] = "realtime", diff --git a/src/basic/process-util.h b/src/basic/process-util.h index f8d1b5e..0cec7c4 100644 --- a/src/basic/process-util.h +++ b/src/basic/process-util.h @@ -173,6 +173,8 @@ static inline int safe_fork(const char *name, ForkFlags flags, pid_t *ret_pid) { int fork_agent(const char *name, const int except[], size_t n_except, pid_t *pid, const char *path, ...); +int set_oom_score_adjust(int value); + #if SIZEOF_PID_T == 4 /* The highest possibly (theoretic) pid_t value on this architecture. */ #define PID_T_MAX ((pid_t) INT32_MAX) diff --git a/src/core/execute.c b/src/core/execute.c index 9781b7d..9634ef9 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -2916,15 +2916,9 @@ static int exec_child( } if (context->oom_score_adjust_set) { - char t[DECIMAL_STR_MAX(context->oom_score_adjust)]; - - /* When we can't make this change due to EPERM, then - * let's silently skip over it. User namespaces - * prohibit write access to this file, and we - * shouldn't trip up over that. */ - - sprintf(t, "%i", context->oom_score_adjust); - r = write_string_file("/proc/self/oom_score_adj", t, 0); + /* When we can't make this change due to EPERM, then let's silently skip over it. User namespaces + * prohibit write access to this file, and we shouldn't trip up over that. */ + r = set_oom_score_adjust(context->oom_score_adjust); if (IN_SET(r, -EPERM, -EACCES)) log_unit_debug_errno(unit, r, "Failed to adjust OOM setting, assuming containerized execution, ignoring: %m"); else if (r < 0) {