]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/setns.2
setns.2, unshare.2: Add reference to pid_namespaces(7)
[thirdparty/man-pages.git] / man2 / setns.2
CommitLineData
dfa22c8b 1.\" Copyright (C) 2011, Eric Biederman <ebiederm@xmission.com>
8d41e607 2.\" and Copyright (C) 2011, 2012, Michael Kerrisk <mtk.manpages@gamil.com>
2297bf0e 3.\"
c4a99c30 4.\" %%%LICENSE_START(GPLv2_ONELINE)
dfa22c8b 5.\" Licensed under the GPLv2
8ff7380d 6.\" %%%LICENSE_END
dfa22c8b 7.\"
8d41e607 8.TH SETNS 2 2013-01-01 "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>
15.sp
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.
dfa22c8b 21
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
13009cfa 27.BR namespaces (5)
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.
34
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
268a93cb 44.BR CLONE_NEWIPC " (since Linux 3.0)"
92dcb220
MK
45.I fd
46must refer to an IPC namespace.
dfa22c8b 47.TP
268a93cb 48.BR CLONE_NEWNET " (since Linux 3.0)"
92dcb220
MK
49.I fd
50must refer to a network namespace.
dfa22c8b 51.TP
268a93cb 52.BR CLONE_NEWNS " (since Linux 3.8)"
99fd2fe3
EB
53.I fd
54must refer to a mount namespace.
55.TP
268a93cb 56.BR CLONE_NEWPID " (since Linux 3.8)"
99fd2fe3
EB
57.I fd
58must refer to a PID namespace.
59.TP
268a93cb 60.BR CLONE_NEWUSER " (since Linux 3.8)"
99fd2fe3
EB
61.I fd
62must refer to a user namespace.
63.TP
268a93cb 64.BR CLONE_NEWUTS " (since Linux 3.0)"
92dcb220
MK
65.I fd
66must refer to a UTS namespace.
dfa22c8b 67.PP
92dcb220
MK
68Specifying
69.I nstype
70as 0 suffices if the caller knows (or does not care)
71what type of namespace is referred to by
72.IR fd .
73Specifying a nonzero value for
74.I nstype
75is useful if the caller does not know what type of namespace is referred to by
76.IR fd
77and wants to ensure that the namespace is of a particular type.
78(The caller might not know the type of the namespace referred to by
79.IR fd
80if the file descriptor was opened by another process and, for example,
81passed to the caller via a UNIX domain socket.)
99fd2fe3 82
f16c7698
MK
83.B CLONE_NEWPID
84behaves somewhat differently from the other
85.I nstype
86values:
87reassociating the calling thread with a PID namespace only changes
88the PID namespace that child processes of the caller will be created in;
89it does not change the PID namespace of the caller itself.
90Reassociating with a PID namespace is only allowed if the
cd7e05aa 91PID namespace specified by
99fd2fe3 92.IR fd
f16c7698 93is a descendant (child, grandchild, etc.)
773f59eb 94of the PID namespace of the caller.
5c8d010b
MK
95For further details on PID namespaces, see
96.BR user_namespaces (7).
99fd2fe3 97
5c67baab 98A multithreaded process may not change user namespace with
cd7e05aa 99.BR setns ().
81714b4f
MK
100It is not permitted to use
101.BR setns ()
102to reenter the caller's current user namespace.
103This prevents a caller that has dropped capabilities from regaining
104those capabilities via a call to
105.BR setns ().
7612b8a7 106
7fc8e5ec 107A process reassociating itself with a user namespace must have the
cd7e05aa 108.B CAP_SYS_ADMIN
49af76fe 109.\" See kernel/user_namespace.c:userns_install() [3.8 source]
7fc8e5ec 110capability in the target user namespace.
7612b8a7
MK
111Upon successfully joining a user namespace,
112a process is granted all capabilities in that namespace,
113regardless of its user and group IDs.
99fd2fe3
EB
114
115A process may not be reassociated with a new mount namespace if it is
5c67baab 116multithreaded.
49af76fe
MK
117.\" Above check is in fs/namespace.c:mntns_install() [3.8 source]
118Changing the mount namespace requires that the caller possess both
cd7e05aa
MK
119.B CAP_SYS_CHROOT
120and
49af76fe 121.BR CAP_SYS_ADMIN
21bfe3e9
MK
122capabilities in its own user namespace and
123.BR CAP_SYS_ADMIN
124in the target mount namespace.
dfa22c8b 125.SH RETURN VALUE
92dcb220
MK
126On success,
127.IR setns ()
128returns 0.
dfa22c8b
EB
129On failure, \-1 is returned and
130.I errno
131is set to indicate the error.
132.SH ERRORS
133.TP
dfa22c8b 134.B EBADF
c07ad242
MK
135.I fd
136is not a valid file descriptor.
dfa22c8b
EB
137.TP
138.B EINVAL
92dcb220
MK
139.I fd
140refers to a namespace whose type does not match that specified in
7aa166c5 141.IR nstype ,
49af76fe 142or there is problem with reassociating
7aa166c5 143the thread with the specified namespace.
dfa22c8b 144.TP
edc3c3b4
MK
145.B EINVAL
146The caller attempted to join the user namespace
147in which it is already a member.
148.TP
dfa22c8b
EB
149.B ENOMEM
150Cannot allocate sufficient memory to change the specified namespace.
dfa22c8b
EB
151.TP
152.B EPERM
7fc8e5ec 153The calling thread did not have the required capability
63b6ef41 154for this operation.
dfa22c8b
EB
155.SH VERSIONS
156The
157.BR setns ()
c95b6ae1 158system call first appeared in Linux in kernel 3.0;
268a93cb 159library support was added to glibc in version 2.14.
dfa22c8b
EB
160.SH CONFORMING TO
161The
162.BR setns ()
163system call is Linux-specific.
164.SH NOTES
7aa166c5
MK
165Not all of the attributes that can be shared when
166a new thread is created using
dfa22c8b
EB
167.BR clone (2)
168can be changed using
169.BR setns ().
8d41e607
MK
170.SH EXAMPLE
171The program below takes two or more arguments.
172The first argument specifies the pathname of a namespace file in an existing
173.I /proc/[pid]/ns/
174directory.
175The remaining arguments specify a command and its arguments.
176The program opens the namespace file, joins that namespace using
177.BR setns (),
178and executes the specified command inside that namespace.
179
180The following shell session demonstrates the use of this program
181(compiled as a binary named
a1ce9159 182.IR ns_exec )
8d41e607
MK
183in conjunction with the
184.BR CLONE_NEWUTS
185example program in the
186.BR clone (2)
187man page (complied as a binary named
188.IR newuts ).
189
190We begin by executing the example program in
191.BR clone (2)
192in the background.
193That program creates a child in a separate UTS namespace.
51ebe080 194The child changes the hostname in its namespace,
8d41e607
MK
195and then both processes display the hostnames in their UTS namespaces,
196so that we can see that they are different.
197
198.nf
199.in +4n
200$ \fBsu\fP # Need privilege for namespace operations
9f1b9726 201Password:
8d41e607
MK
202# \fB./newuts bizarro &\fP
203[1] 3549
204clone() returned 3550
205uts.nodename in child: bizarro
206uts.nodename in parent: antero
5c977011 207# \fBuname \-n\fP # Verify hostname in the shell
8d41e607
MK
208antero
209.in
210.fi
211
212We then run the program shown below,
213using it to execute a shell.
214Inside that shell, we verify that the hostname is the one
215set by the child created by the first program:
216
217.nf
218.in +4n
a1ce9159 219# \fB./ns_exec /proc/3550/ns/uts /bin/bash\fP
5c977011 220# \fBuname \-n\fP # Executed in shell started by ns_exec
8d41e607
MK
221bizarro
222.in
223.fi
8d41e607
MK
224.SS Program source
225.nf
226#define _GNU_SOURCE
227#include <fcntl.h>
228#include <sched.h>
229#include <unistd.h>
230#include <stdlib.h>
231#include <stdio.h>
232
233#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
234 } while (0)
235
236int
237main(int argc, char *argv[])
238{
239 int fd;
240
241 if (argc < 3) {
242 fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\\n", argv[0]);
243 exit(EXIT_FAILURE);
244 }
245
246 fd = open(argv[1], O_RDONLY); /* Get descriptor for namespace */
247 if (fd == \-1)
248 errExit("open");
249
250 if (setns(fd, 0) == \-1) /* Join that namespace */
251 errExit("setns");
252
253 execvp(argv[2], &argv[2]); /* Execute a command in namespace */
254 errExit("execvp");
255}
256.fi
dfa22c8b
EB
257.SH SEE ALSO
258.BR clone (2),
259.BR fork (2),
2a9f74a9 260.BR unshare (2),
b93a34b5 261.BR vfork (2),
41096af1 262.BR namespaces (7),
92dcb220 263.BR unix (7)