]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
namespace-util: add namespace_open_by_type() helper
authorLennart Poettering <lennart@poettering.net>
Tue, 5 Dec 2023 10:51:26 +0000 (11:51 +0100)
committerLennart Poettering <lennart@poettering.net>
Sat, 6 Apr 2024 14:08:23 +0000 (16:08 +0200)
src/basic/namespace-util.c
src/basic/namespace-util.h

index 53633c7e8f98f97c5ccfb86e987876dec97588f1..0e54a1e2358a2b5dd84198a856736ab08fd84e96 100644 (file)
@@ -426,3 +426,19 @@ int parse_userns_uid_range(const char *s, uid_t *ret_uid_shift, uid_t *ret_uid_r
 
         return 0;
 }
+
+int namespace_open_by_type(NamespaceType type) {
+        const char *p;
+        int fd;
+
+        assert(type >= 0);
+        assert(type < _NAMESPACE_TYPE_MAX);
+
+        p = pid_namespace_path(0, type);
+
+        fd = RET_NERRNO(open(p, O_RDONLY|O_NOCTTY|O_CLOEXEC));
+        if (fd == -ENOENT && proc_mounted() == 0)
+                return -ENOSYS;
+
+        return fd;
+}
index e7cf1a2201254a24f27322496479273c15bf0fbb..972ef05d539d7e2c84e492f3ffd476501eb94d6e 100644 (file)
@@ -60,3 +60,5 @@ int netns_acquire(void);
 int in_same_namespace(pid_t pid1, pid_t pid2, NamespaceType type);
 
 int parse_userns_uid_range(const char *s, uid_t *ret_uid_shift, uid_t *ret_uid_range);
+
+int namespace_open_by_type(NamespaceType type);