]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
process-util: add helper for querying oom score adjustment value
authorLennart Poettering <lennart@poettering.net>
Thu, 30 Sep 2021 09:19:11 +0000 (11:19 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 4 Oct 2021 14:27:10 +0000 (16:27 +0200)
src/basic/process-util.c
src/basic/process-util.h
src/core/dbus-execute.c

index c424f62ef3822c1fcfb9de34108a9899a72a905d..5e7ed06ea554073f441a81d8524e1d488ad956d3 100644 (file)
@@ -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;
index 551b236c01156590d72c71931b2abd6b3a85f0c2..7e87f5a17c7eadf3b65f463699ac13824011418e 100644 (file)
@@ -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)
index 8f6042708cdcf51f6e274d976053042b1dbd1586..488de1242a44be170ad8cf0772d1adce8c3d8345 100644 (file)
@@ -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);