]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/setns.2
setns.2: Minor text reorganization
[thirdparty/man-pages.git] / man2 / setns.2
CommitLineData
dfa22c8b 1.\" Copyright (C) 2011, Eric Biederman <ebiederm@xmission.com>
83825f79 2.\" and Copyright (C) 2011, 2012, Michael Kerrisk <mtk.manpages@gmail.com>
2297bf0e 3.\"
c4a99c30 4.\" %%%LICENSE_START(GPLv2_ONELINE)
dfa22c8b 5.\" Licensed under the GPLv2
8ff7380d 6.\" %%%LICENSE_END
dfa22c8b 7.\"
4b8c67d9 8.TH SETNS 2 2017-09-15 "Linux" "Linux Programmer's Manual"
dfa22c8b 9.SH NAME
7aa166c5 10setns \- reassociate thread with a namespace
dfa22c8b
EB
11.SH SYNOPSIS
12.nf
13.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
14.B #include <sched.h>
68e4db0a 15.PP
dfa22c8b
EB
16.BI "int setns(int " fd ", int " nstype );
17.fi
18.SH DESCRIPTION
c07ad242 19Given a file descriptor referring to a namespace,
7aa166c5 20reassociate the calling thread with that namespace.
efeece04 21.PP
92dcb220
MK
22The
23.I fd
24argument is a file descriptor referring to one of the namespace entries in a
25.I /proc/[pid]/ns/
26directory; see
6edfe909 27.BR namespaces (7)
c409c4ff 28for further information on
92dcb220 29.IR /proc/[pid]/ns/ .
7aa166c5 30The calling thread will be reassociated with the corresponding namespace,
92dcb220
MK
31subject to any constraints imposed by the
32.I nstype
33argument.
efeece04 34.PP
dfa22c8b
EB
35The
36.I nstype
c07ad242 37argument specifies which type of namespace
7aa166c5 38the calling thread may be reassociated with.
4e1e208d 39This argument can have one of the following values:
dfa22c8b
EB
40.TP
41.BR 0
92dcb220 42Allow any type of namespace to be joined.
dfa22c8b 43.TP
46dd3176
MK
44.BR CLONE_NEWCGROUP " (since Linux 4.6)"
45.I fd
46must refer to a cgroup namespace.
47.TP
268a93cb 48.BR CLONE_NEWIPC " (since Linux 3.0)"
92dcb220
MK
49.I fd
50must refer to an IPC namespace.
dfa22c8b 51.TP
268a93cb 52.BR CLONE_NEWNET " (since Linux 3.0)"
92dcb220
MK
53.I fd
54must refer to a network namespace.
dfa22c8b 55.TP
268a93cb 56.BR CLONE_NEWNS " (since Linux 3.8)"
99fd2fe3
EB
57.I fd
58must refer to a mount namespace.
59.TP
268a93cb 60.BR CLONE_NEWPID " (since Linux 3.8)"
99fd2fe3 61.I fd
ba7d7ed9 62must refer to a descendant PID namespace.
99fd2fe3 63.TP
268a93cb 64.BR CLONE_NEWUSER " (since Linux 3.8)"
99fd2fe3
EB
65.I fd
66must refer to a user namespace.
67.TP
268a93cb 68.BR CLONE_NEWUTS " (since Linux 3.0)"
92dcb220
MK
69.I fd
70must refer to a UTS namespace.
dfa22c8b 71.PP
92dcb220
MK
72Specifying
73.I nstype
74as 0 suffices if the caller knows (or does not care)
75what type of namespace is referred to by
76.IR fd .
77Specifying a nonzero value for
78.I nstype
79is useful if the caller does not know what type of namespace is referred to by
80.IR fd
81and wants to ensure that the namespace is of a particular type.
82(The caller might not know the type of the namespace referred to by
83.IR fd
84if the file descriptor was opened by another process and, for example,
85passed to the caller via a UNIX domain socket.)
8e23f12b
MK
86.\"
87.SS Details for specific namespace types
88Note the following details and restrictions when reassociating with
89specific namespace types:
90.TP
91User namespaces
ec66fbff
MK
92A process reassociating itself with a user namespace must have the
93.B CAP_SYS_ADMIN
94.\" See kernel/user_namespace.c:userns_install() [3.8 source]
95capability in the target user namespace.
96Upon successfully joining a user namespace,
97a process is granted all capabilities in that namespace,
98regardless of its user and group IDs.
2e4a22de 99.IP
5c67baab 100A multithreaded process may not change user namespace with
cd7e05aa 101.BR setns ().
2e4a22de 102.IP
81714b4f
MK
103It is not permitted to use
104.BR setns ()
105to reenter the caller's current user namespace.
106This prevents a caller that has dropped capabilities from regaining
107those capabilities via a call to
108.BR setns ().
2e4a22de 109.IP
85e34225
MK
110For security reasons,
111.\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
112.\" https://lwn.net/Articles/543273/
113a process can't join a new user namespace if it is sharing
8db37760 114filesystem-related attributes
85e34225
MK
115(the attributes whose sharing is controlled by the
116.BR clone (2)
117.B CLONE_FS
118flag) with another process.
2e4a22de 119.IP
e57c3979 120For further details on user namespaces, see
ec66fbff 121.BR user_namespaces (7).
8e23f12b
MK
122.TP
123Mount namespaces
49af76fe 124Changing the mount namespace requires that the caller possess both
cd7e05aa
MK
125.B CAP_SYS_CHROOT
126and
f5d401dd 127.BR CAP_SYS_ADMIN
21bfe3e9 128capabilities in its own user namespace and
f5d401dd 129.BR CAP_SYS_ADMIN
6f91075e 130in the the user namespace that owns the target mount namespace.
2e4a22de 131.IP
c6b1b95b
MK
132A process may not be reassociated with a new mount namespace if it is
133multithreaded.
134.\" Above check is in fs/namespace.c:mntns_install() [3.8 source]
135.IP
53d084e8
MK
136See
137.BR user_namespaces (7)
138for details on the interaction of user namespaces and mount namespaces.
8e23f12b 139.TP
0ecd5804
MK
140PID namespaces
141If
142.I fd
143refers to a PID namespaces, the semantics are somewhat different
144from other namespace types:
145reassociating the calling thread with a PID namespace changes only
146the PID namespace that subsequently created child processes of
147the caller will be placed in;
148it does not change the PID namespace of the caller itself.
149.IP
150Reassociating with a PID namespace is allowed only if the
151PID namespace specified by
152.IR fd
153is a descendant (child, grandchild, etc.)
154of the PID namespace of the caller.
155.IP
156For further details on PID namespaces, see
157.BR pid_namespaces (7).
158.TP
8e23f12b 159Cgroup namespaces
46dd3176
MK
160Using
161.BR setns ()
162to change the caller's cgroup namespace does not change
163the caller's cgroup memberships.
dfa22c8b 164.SH RETURN VALUE
92dcb220 165On success,
577b1ed5 166.BR setns ()
92dcb220 167returns 0.
dfa22c8b
EB
168On failure, \-1 is returned and
169.I errno
170is set to indicate the error.
171.SH ERRORS
172.TP
dfa22c8b 173.B EBADF
c07ad242
MK
174.I fd
175is not a valid file descriptor.
dfa22c8b
EB
176.TP
177.B EINVAL
92dcb220
MK
178.I fd
179refers to a namespace whose type does not match that specified in
16fe718f
MK
180.IR nstype .
181.TP
182.B EINVAL
183There is problem with reassociating
7aa166c5 184the thread with the specified namespace.
dfa22c8b 185.TP
ba7d7ed9
MF
186.\" See kernel/pid_namespace.c::pidns_install() [kernel 3.18 sources]
187.B EINVAL
c9b0afde
MK
188The caller tried to join an ancestor (parent, grandparent, and so on)
189PID namespace.
ba7d7ed9 190.TP
edc3c3b4
MK
191.B EINVAL
192The caller attempted to join the user namespace
193in which it is already a member.
194.TP
e63259f2
MK
195.B EINVAL
196.\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
197The caller shares filesystem
198.RB ( CLONE_FS )
199state (in particular, the root directory)
200with other processes and tried to join a new user namespace.
201.TP
202.B EINVAL
203.\" See kernel/user_namespace.c::userns_install() [kernel 3.15 sources]
204The caller is multithreaded and tried to join a new user namespace.
205.TP
dfa22c8b
EB
206.B ENOMEM
207Cannot allocate sufficient memory to change the specified namespace.
dfa22c8b
EB
208.TP
209.B EPERM
7fc8e5ec 210The calling thread did not have the required capability
63b6ef41 211for this operation.
dfa22c8b
EB
212.SH VERSIONS
213The
214.BR setns ()
c95b6ae1 215system call first appeared in Linux in kernel 3.0;
268a93cb 216library support was added to glibc in version 2.14.
dfa22c8b
EB
217.SH CONFORMING TO
218The
219.BR setns ()
220system call is Linux-specific.
221.SH NOTES
7aa166c5
MK
222Not all of the attributes that can be shared when
223a new thread is created using
dfa22c8b
EB
224.BR clone (2)
225can be changed using
226.BR setns ().
8d41e607
MK
227.SH EXAMPLE
228The program below takes two or more arguments.
229The first argument specifies the pathname of a namespace file in an existing
230.I /proc/[pid]/ns/
231directory.
232The remaining arguments specify a command and its arguments.
233The program opens the namespace file, joins that namespace using
234.BR setns (),
235and executes the specified command inside that namespace.
efeece04 236.PP
8d41e607
MK
237The following shell session demonstrates the use of this program
238(compiled as a binary named
a1ce9159 239.IR ns_exec )
8d41e607
MK
240in conjunction with the
241.BR CLONE_NEWUTS
242example program in the
243.BR clone (2)
244man page (complied as a binary named
245.IR newuts ).
efeece04 246.PP
8d41e607
MK
247We begin by executing the example program in
248.BR clone (2)
249in the background.
250That program creates a child in a separate UTS namespace.
51ebe080 251The child changes the hostname in its namespace,
8d41e607
MK
252and then both processes display the hostnames in their UTS namespaces,
253so that we can see that they are different.
efeece04 254.PP
8d41e607 255.in +4n
b8302363 256.EX
8d41e607 257$ \fBsu\fP # Need privilege for namespace operations
9f1b9726 258Password:
8d41e607
MK
259# \fB./newuts bizarro &\fP
260[1] 3549
261clone() returned 3550
262uts.nodename in child: bizarro
263uts.nodename in parent: antero
5c977011 264# \fBuname \-n\fP # Verify hostname in the shell
8d41e607 265antero
b8302363 266.EE
e646a1ba 267.in
efeece04 268.PP
8d41e607
MK
269We then run the program shown below,
270using it to execute a shell.
271Inside that shell, we verify that the hostname is the one
272set by the child created by the first program:
efeece04 273.PP
8d41e607 274.in +4n
b8302363 275.EX
a1ce9159 276# \fB./ns_exec /proc/3550/ns/uts /bin/bash\fP
5c977011 277# \fBuname \-n\fP # Executed in shell started by ns_exec
8d41e607 278bizarro
b8302363 279.EE
e646a1ba 280.in
8d41e607 281.SS Program source
e7d0bb47 282.EX
8d41e607
MK
283#define _GNU_SOURCE
284#include <fcntl.h>
285#include <sched.h>
286#include <unistd.h>
287#include <stdlib.h>
288#include <stdio.h>
289
290#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
291 } while (0)
292
293int
294main(int argc, char *argv[])
295{
296 int fd;
297
298 if (argc < 3) {
299 fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\\n", argv[0]);
300 exit(EXIT_FAILURE);
301 }
302
e2b63503 303 fd = open(argv[1], O_RDONLY); /* Get file descriptor for namespace */
8d41e607
MK
304 if (fd == \-1)
305 errExit("open");
306
e2b63503 307 if (setns(fd, 0) == \-1) /* Join that namespace */
8d41e607
MK
308 errExit("setns");
309
e2b63503 310 execvp(argv[2], &argv[2]); /* Execute a command in namespace */
8d41e607
MK
311 errExit("execvp");
312}
e7d0bb47 313.EE
dfa22c8b 314.SH SEE ALSO
7680cb3e 315.BR nsenter (1),
dfa22c8b
EB
316.BR clone (2),
317.BR fork (2),
2a9f74a9 318.BR unshare (2),
b93a34b5 319.BR vfork (2),
41096af1 320.BR namespaces (7),
92dcb220 321.BR unix (7)