From: Mike Yuan Date: Thu, 28 Nov 2024 00:21:03 +0000 (+0100) Subject: process-util: move namespace_get_leader() to namespace-util X-Git-Tag: v258-rc1~1704^2~7 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a33f691374b7c696a8e038c64af64ce3eec74dd9;p=thirdparty%2Fsystemd.git process-util: move namespace_get_leader() to namespace-util This allows us to drop the hack for recursive includes. --- diff --git a/src/basic/namespace-util.c b/src/basic/namespace-util.c index 10bbf36ca2f..4a1625ac0c1 100644 --- a/src/basic/namespace-util.c +++ b/src/basic/namespace-util.c @@ -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 diff --git a/src/basic/namespace-util.h b/src/basic/namespace-util.h index ccc42bc40c0..1b466ea2194 100644 --- a/src/basic/namespace-util.h +++ b/src/basic/namespace-util.h @@ -3,11 +3,9 @@ #include -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); diff --git a/src/basic/process-util.c b/src/basic/process-util.c index ec4ea98c036..e0ba9126339 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -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; diff --git a/src/basic/process-util.h b/src/basic/process-util.h index a59cdf22acb..78cf90d3706 100644 --- a/src/basic/process-util.h +++ b/src/basic/process-util.h @@ -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 {