]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
namespace-util: add in_same_namespace()
authorChristian Brauner <brauner@kernel.org>
Fri, 30 Sep 2022 13:02:52 +0000 (15:02 +0200)
committerChristian Brauner (Microsoft) <brauner@kernel.org>
Tue, 4 Oct 2022 16:51:28 +0000 (18:51 +0200)
Add a helper for the canonical way to determine whether two namespaces
are identical.

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

index b330e2a11d1e8303e6ca7365a48c27522de3ceb8..a87a875943c7dfcd8dba681dd7cea48e2d7f0d4a 100644 (file)
@@ -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);
+}
index 5c1912985d898aee387ebe7ae1d29eb48dc5346f..be5b2281d3c4c1bc556ffd1898c2b7b857ddbf1c 100644 (file)
@@ -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);