1SETNS(2) Linux Programmer's Manual SETNS(2)
2
3
4
6 setns - reassociate thread with a namespace
7
9 #define _GNU_SOURCE /* See feature_test_macros(7) */
10 #include <sched.h>
11
12 int setns(int fd, int nstype);
13
15 The setns() system call allows the calling thread to move into differ‐
16 ent namespaces. The fd argument is one of the following:
17
18 • a file descriptor referring to one of the magic links in a
19 /proc/[pid]/ns/ directory (or a bind mount to such a link);
20
21 • a PID file descriptor (see pidfd_open(2)).
22
23 The nstype argument is interpreted differently in each case.
24
25 fd refers to a /proc/[pid]/ns/ link
26 If fd refers to a /proc/[pid]/ns/ link, then setns() reassociates the
27 calling thread with the namespace associated with that link, subject to
28 any constraints imposed by the nstype argument. In this usage, each
29 call to setns() changes just one of the caller's namespace memberships.
30
31 The nstype argument specifies which type of namespace the calling
32 thread may be reassociated with. This argument can have one of the
33 following values:
34
35 0 Allow any type of namespace to be joined.
36
37 CLONE_NEWCGROUP (since Linux 4.6)
38 fd must refer to a cgroup namespace.
39
40 CLONE_NEWIPC (since Linux 3.0)
41 fd must refer to an IPC namespace.
42
43 CLONE_NEWNET (since Linux 3.0)
44 fd must refer to a network namespace.
45
46 CLONE_NEWNS (since Linux 3.8)
47 fd must refer to a mount namespace.
48
49 CLONE_NEWPID (since Linux 3.8)
50 fd must refer to a descendant PID namespace.
51
52 CLONE_NEWTIME (since Linux 5.8)
53 fd must refer to a time namespace.
54
55 CLONE_NEWUSER (since Linux 3.8)
56 fd must refer to a user namespace.
57
58 CLONE_NEWUTS (since Linux 3.0)
59 fd must refer to a UTS namespace.
60
61 Specifying nstype as 0 suffices if the caller knows (or does not care)
62 what type of namespace is referred to by fd. Specifying a nonzero
63 value for nstype is useful if the caller does not know what type of
64 namespace is referred to by fd and wants to ensure that the namespace
65 is of a particular type. (The caller might not know the type of the
66 namespace referred to by fd if the file descriptor was opened by an‐
67 other process and, for example, passed to the caller via a UNIX domain
68 socket.)
69
70 fd is a PID file descriptor
71 Since Linux 5.8, fd may refer to a PID file descriptor obtained from
72 pidfd_open(2) or clone(2). In this usage, setns() atomically moves the
73 calling thread into one or more of the same namespaces as the thread
74 referred to by fd.
75
76 The nstype argument is a bit mask specified by ORing together one or
77 more of the CLONE_NEW* namespace constants listed above. The caller is
78 moved into each of the target thread's namespaces that is specified in
79 nstype; the caller's memberships in the remaining namespaces are left
80 unchanged.
81
82 F