From: Lennart Poettering Date: Thu, 30 Sep 2021 09:19:11 +0000 (+0200) Subject: process-util: add helper for querying oom score adjustment value X-Git-Tag: v250-rc1~569^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2c37c613a78637059a6b219912946b50c8109aee;p=thirdparty%2Fsystemd.git process-util: add helper for querying oom score adjustment value --- diff --git a/src/basic/process-util.c b/src/basic/process-util.c index c424f62ef38..5e7ed06ea55 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -1502,6 +1502,24 @@ int set_oom_score_adjust(int value) { WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_DISABLE_BUFFER); } +int get_oom_score_adjust(int *ret) { + _cleanup_free_ char *t; + int r, a; + + r = read_virtual_file("/proc/self/oom_score_adj", SIZE_MAX, &t, NULL); + if (r < 0) + return r; + + delete_trailing_chars(t, WHITESPACE); + + assert_se(safe_atoi(t, &a) >= 0); + assert_se(oom_score_adjust_is_valid(a)); + + if (ret) + *ret = a; + return 0; +} + int pidfd_get_pid(int fd, pid_t *ret) { char path[STRLEN("/proc/self/fdinfo/") + DECIMAL_STR_MAX(int)]; _cleanup_free_ char *fdinfo = NULL; diff --git a/src/basic/process-util.h b/src/basic/process-util.h index 551b236c011..7e87f5a17c7 100644 --- a/src/basic/process-util.h +++ b/src/basic/process-util.h @@ -175,6 +175,7 @@ static inline int safe_fork(const char *name, ForkFlags flags, pid_t *ret_pid) { int namespace_fork(const char *outer_name, const char *inner_name, int except_fds[], size_t n_except_fds, ForkFlags flags, int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd, pid_t *ret_pid); int set_oom_score_adjust(int value); +int get_oom_score_adjust(int *ret); /* The highest possibly (theoretic) pid_t value on this architecture. */ #define PID_T_MAX ((pid_t) INT32_MAX) diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c index 8f6042708cd..488de1242a4 100644 --- a/src/core/dbus-execute.c +++ b/src/core/dbus-execute.c @@ -104,8 +104,7 @@ static int property_get_oom_score_adjust( sd_bus_error *error) { ExecContext *c = userdata; - int32_t n; - int r; + int r, n; assert(bus); assert(reply); @@ -114,17 +113,10 @@ static int property_get_oom_score_adjust( if (c->oom_score_adjust_set) n = c->oom_score_adjust; else { - _cleanup_free_ char *t = NULL; - n = 0; - r = read_one_line_file("/proc/self/oom_score_adj", &t); + r = get_oom_score_adjust(&n); if (r < 0) log_debug_errno(r, "Failed to read /proc/self/oom_score_adj, ignoring: %m"); - else { - r = safe_atoi32(t, &n); - if (r < 0) - log_debug_errno(r, "Failed to parse \"%s\" from /proc/self/oom_score_adj, ignoring: %m", t); - } } return sd_bus_message_append(reply, "i", n);