cgroup-util: introduce cg_pidref_get_unit
authorMike Yuan <me@yhndnzj.com>
Thu, 9 Nov 2023 16:18:53 +0000 (00:18 +0800)
committerMike Yuan <me@yhndnzj.com>
Sat, 11 Nov 2023 09:21:10 +0000 (17:21 +0800)
src/basic/cgroup-util.c
src/basic/cgroup-util.h

index 4c82552..18b16ec 100644 (file)
@@ -1262,6 +1262,27 @@ int cg_pid_get_unit(pid_t pid, char **ret_unit) {
         return cg_path_get_unit(cgroup, ret_unit);
 }
 
+int cg_pidref_get_unit(const PidRef *pidref, char **ret) {
+        _cleanup_free_ char *unit = NULL;
+        int r;
+
+        assert(ret);
+
+        if (!pidref_is_set(pidref))
+                return -ESRCH;
+
+        r = cg_pid_get_unit(pidref->pid, &unit);
+        if (r < 0)
+                return r;
+
+        r = pidref_verify(pidref);
+        if (r < 0)
+                return r;
+
+        *ret = TAKE_PTR(unit);
+        return 0;
+}
+
 /**
  * Skip session-*.scope, but require it to be there.
  */
index bfb3830..6ab14c7 100644 (file)
@@ -279,6 +279,7 @@ int cg_pid_get_path_shifted(pid_t pid, const char *cached_root, char **ret_cgrou
 int cg_pid_get_session(pid_t pid, char **ret_session);
 int cg_pid_get_owner_uid(pid_t pid, uid_t *ret_uid);
 int cg_pid_get_unit(pid_t pid, char **ret_unit);
+int cg_pidref_get_unit(const PidRef *pidref, char **ret);
 int cg_pid_get_user_unit(pid_t pid, char **ret_unit);
 int cg_pid_get_machine_name(pid_t pid, char **ret_machine);
 int cg_pid_get_slice(pid_t pid, char **ret_slice);