diff options
author | Lennart Poettering <lennart@poettering.net> | 2023-12-05 11:51:26 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2024-04-06 16:08:23 +0200 |
commit | 574a07c79d0518040fd44492fa3ee0e21e65d495 (patch) | |
tree | 8a5ae45e4db1e8ed8d135c3b4479e2be19cebbb4 | |
parent | namespace-util: add detach_mount_namespace_userns() (diff) | |
download | systemd-574a07c79d0518040fd44492fa3ee0e21e65d495.tar.xz systemd-574a07c79d0518040fd44492fa3ee0e21e65d495.zip |
namespace-util: add namespace_open_by_type() helper
-rw-r--r-- | src/basic/namespace-util.c | 16 | ||||
-rw-r--r-- | src/basic/namespace-util.h | 2 |
2 files changed, 18 insertions, 0 deletions
diff --git a/src/basic/namespace-util.c b/src/basic/namespace-util.c index 53633c7e8f..0e54a1e235 100644 --- a/src/basic/namespace-util.c +++ b/src/basic/namespace-util.c @@ -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; +} diff --git a/src/basic/namespace-util.h b/src/basic/namespace-util.h index e7cf1a2201..972ef05d53 100644 --- a/src/basic/namespace-util.h +++ b/src/basic/namespace-util.h @@ -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); |