]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
process-util: move namespace_get_leader() to namespace-util
authorMike Yuan <me@yhndnzj.com>
Thu, 28 Nov 2024 00:21:03 +0000 (01:21 +0100)
committerMike Yuan <me@yhndnzj.com>
Sat, 4 Jan 2025 16:08:00 +0000 (17:08 +0100)
This allows us to drop the hack for recursive includes.

src/basic/namespace-util.c
src/basic/namespace-util.h
src/basic/process-util.c
src/basic/process-util.h

index 10bbf36ca2f0d2ec235b33ffda0bcd5131b2a52f..4a1625ac0c1f0a2341e3f37c8aa1b483a8952790 100644 (file)
@@ -310,6 +310,34 @@ int namespace_is_init(NamespaceType type) {
         return st.st_ino == namespace_info[type].root_inode;
 }
 
+int namespace_get_leader(pid_t pid, NamespaceType type, pid_t *ret) {
+        int r;
+
+        assert(pid >= 0);
+        assert(type >= 0 && type < _NAMESPACE_TYPE_MAX);
+        assert(ret);
+
+        for (;;) {
+                pid_t ppid;
+
+                r = get_process_ppid(pid, &ppid);
+                if (r < 0)
+                        return r;
+
+                r = in_same_namespace(pid, ppid, type);
+                if (r < 0)
+                        return r;
+                if (r == 0) {
+                        /* If the parent and the child are not in the same namespace, then the child is
+                         * the leader we are looking for. */
+                        *ret = pid;
+                        return 0;
+                }
+
+                pid = ppid;
+        }
+}
+
 int detach_mount_namespace(void) {
         /* Detaches the mount namespace, disabling propagation from our namespace to the host. Sets
          * propagation first to MS_SLAVE for all mounts (disabling propagation), and then back to MS_SHARED
index ccc42bc40c0a8fc2c0cc9b79519274827759d7d9..1b466ea2194656be22ba93a83cb64316c7799eea 100644 (file)
@@ -3,11 +3,9 @@
 
 #include <sys/types.h>
 
-typedef enum NamespaceType NamespaceType;
-
 #include "pidref.h"
 
-enum NamespaceType {
+typedef enum NamespaceType {
         NAMESPACE_CGROUP,
         NAMESPACE_IPC,
         NAMESPACE_NET,
@@ -18,7 +16,7 @@ enum NamespaceType {
         NAMESPACE_TIME,
         _NAMESPACE_TYPE_MAX,
         _NAMESPACE_TYPE_INVALID = -EINVAL,
-};
+} NamespaceType;
 
 extern const struct namespace_info {
         const char *proc_name;
@@ -55,6 +53,8 @@ int is_our_namespace(int fd, NamespaceType type);
 
 int namespace_is_init(NamespaceType type);
 
+int namespace_get_leader(pid_t pid, NamespaceType type, pid_t *ret);
+
 int detach_mount_namespace(void);
 int detach_mount_namespace_harder(uid_t target_uid, gid_t target_gid);
 int detach_mount_namespace_userns(int userns_fd);
index ec4ea98c036bbe6c6cd7650841b6ed0e4683b3ef..e0ba9126339343f6836fee72b12b1781e83c323a 100644 (file)
@@ -395,33 +395,6 @@ int container_get_leader(const char *machine, pid_t *pid) {
         return 0;
 }
 
-int namespace_get_leader(pid_t pid, NamespaceType type, pid_t *ret) {
-        int r;
-
-        assert(ret);
-
-        for (;;) {
-                pid_t ppid;
-
-                r = get_process_ppid(pid, &ppid);
-                if (r < 0)
-                        return r;
-
-                r = in_same_namespace(pid, ppid, type);
-                if (r < 0)
-                        return r;
-                if (r == 0) {
-                        /* If the parent and the child are not in the same
-                         * namespace, then the child is the leader we are
-                         * looking for. */
-                        *ret = pid;
-                        return 0;
-                }
-
-                pid = ppid;
-        }
-}
-
 int pid_is_kernel_thread(pid_t pid) {
         _cleanup_free_ char *line = NULL;
         unsigned long long flags;
index a59cdf22acbef8b5061fdc755d70a77a32153e48..78cf90d370692ab919920efb6843ef39c72f4ecf 100644 (file)
@@ -14,7 +14,6 @@
 #include "alloc-util.h"
 #include "format-util.h"
 #include "macro.h"
-#include "namespace-util.h"
 #include "pidref.h"
 #include "time-util.h"
 
@@ -60,8 +59,6 @@ int get_process_umask(pid_t pid, mode_t *ret);
 
 int container_get_leader(const char *machine, pid_t *pid);
 
-int namespace_get_leader(pid_t pid, NamespaceType type, pid_t *ret);
-
 int wait_for_terminate(pid_t pid, siginfo_t *status);
 
 typedef enum WaitFlags {