Skip to content

Commit d2afdb7

Browse files
committed
nsfs: add current_in_namespace()
Add a helper to easily check whether a given namespace is the caller's current namespace. This is currently open-coded in a lot of places. Simply switch on the type and compare the results. Reviewed-by: Aleksa Sarai <cyphar@cyphar.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent d7afdf8 commit d2afdb7

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

include/linux/nsfs.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#define _LINUX_NSFS_H
66

77
#include <linux/ns_common.h>
8+
#include <linux/cred.h>
9+
#include <linux/pid_namespace.h>
810

911
struct path;
1012
struct task_struct;
@@ -22,5 +24,17 @@ int ns_get_name(char *buf, size_t size, struct task_struct *task,
2224
const struct proc_ns_operations *ns_ops);
2325
void nsfs_init(void);
2426

25-
#endif /* _LINUX_NSFS_H */
27+
#define __current_namespace_from_type(__ns) \
28+
_Generic((__ns), \
29+
struct cgroup_namespace *: current->nsproxy->cgroup_ns, \
30+
struct ipc_namespace *: current->nsproxy->ipc_ns, \
31+
struct net *: current->nsproxy->net_ns, \
32+
struct pid_namespace *: task_active_pid_ns(current), \
33+
struct mnt_namespace *: current->nsproxy->mnt_ns, \
34+
struct time_namespace *: current->nsproxy->time_ns, \
35+
struct user_namespace *: current_user_ns(), \
36+
struct uts_namespace *: current->nsproxy->uts_ns)
37+
38+
#define current_in_namespace(__ns) (__current_namespace_from_type(__ns) == __ns)
2639

40+
#endif /* _LINUX_NSFS_H */

0 commit comments

Comments
 (0)