diff options
author | Christian Brauner <brauner@kernel.org> | 2022-09-30 15:02:52 +0200 |
---|---|---|
committer | Christian Brauner (Microsoft) <brauner@kernel.org> | 2022-10-04 18:51:28 +0200 |
commit | 2fe299a320757bf5a3e0362a00e570d3bf713eab (patch) | |
tree | 676a450273931285f4db8af5b19198f6da0c70d3 /src/basic | |
parent | nsflags: replace namespace_flag_map with general namespace_info introduced ea... (diff) | |
download | systemd-2fe299a320757bf5a3e0362a00e570d3bf713eab.tar.xz systemd-2fe299a320757bf5a3e0362a00e570d3bf713eab.zip |
namespace-util: add in_same_namespace()
Add a helper for the canonical way to determine whether two namespaces
are identical.
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/namespace-util.c | 24 | ||||
-rw-r--r-- | src/basic/namespace-util.h | 1 |
2 files changed, 25 insertions, 0 deletions
diff --git a/src/basic/namespace-util.c b/src/basic/namespace-util.c index b330e2a11d..a87a875943 100644 --- a/src/basic/namespace-util.c +++ b/src/basic/namespace-util.c @@ -236,3 +236,27 @@ int userns_acquire(const char *uid_map, const char *gid_map) { return TAKE_FD(userns_fd); } + +int in_same_namespace(pid_t pid1, pid_t pid2, NamespaceType type) { + const char *ns_path; + struct stat ns_st1, ns_st2; + + if (pid1 == 0) + pid1 = getpid_cached(); + + if (pid2 == 0) + pid2 = getpid_cached(); + + if (pid1 == pid2) + return 1; + + ns_path = pid_namespace_path(pid1, type); + if (stat(ns_path, &ns_st1) < 0) + return -errno; + + ns_path = pid_namespace_path(pid2, type); + if (stat(ns_path, &ns_st2) < 0) + return -errno; + + return stat_inode_same(&ns_st1, &ns_st2); +} diff --git a/src/basic/namespace-util.h b/src/basic/namespace-util.h index 5c1912985d..be5b2281d3 100644 --- a/src/basic/namespace-util.h +++ b/src/basic/namespace-util.h @@ -45,3 +45,4 @@ static inline bool userns_shift_range_valid(uid_t shift, uid_t range) { } int userns_acquire(const char *uid_map, const char *gid_map); +int in_same_namespace(pid_t pid1, pid_t pid2, NamespaceType type); |