]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/setns.2
setns.2: Document the PID, user, and mount namespace support
[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
27.BR proc (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
EB
43.TP
44.BR CLONE_NEWIPC
92dcb220
MK
45.I fd
46must refer to an IPC namespace.
dfa22c8b
EB
47.TP
48.BR CLONE_NEWNET
92dcb220
MK
49.I fd
50must refer to a network namespace.
dfa22c8b 51.TP
99fd2fe3
EB
52.BR CLONE_NEWNS
53.I fd
54must refer to a mount namespace.
55.TP
56.BR CLONE_NEWPID
57.I fd
58must refer to a PID namespace.
59.TP
60.BR CLONE_NEWUSER
61.I fd
62must refer to a user namespace.
63.TP
dfa22c8b 64.BR CLONE_NEWUTS
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
EB
82
83The pid namespace is a little different. Reassociating the calling
84thread with a pid namespace only changes the pid namespace that the
85child processes will be created in.
86
87Changing the pid namespace for child processes is only allowed if the
88pid namespace specified by
89.IR fd
90is a child pid namespace of the pid namespace of the current thread.
91
92A multi-threaded process may not change user namespace with setns. A
93process may not reassociate the thread with the current user
94namespace. The process reassociating itself with a user namespace
95must have CAP_SYS_ADMIN privileges in the target user namespace.
96
97A process may not be reassociated with a new mount namespace if it is
98multi-threaded or it does not possess both CAP_SYS_CHROOT privileges
99and CAP_SYS_ADMIN rights over the target mount namespace.
100
dfa22c8b 101.SH RETURN VALUE
92dcb220
MK
102On success,
103.IR setns ()
104returns 0.
dfa22c8b
EB
105On failure, \-1 is returned and
106.I errno
107is set to indicate the error.
108.SH ERRORS
109.TP
dfa22c8b 110.B EBADF
c07ad242
MK
111.I fd
112is not a valid file descriptor.
dfa22c8b
EB
113.TP
114.B EINVAL
92dcb220
MK
115.I fd
116refers to a namespace whose type does not match that specified in
7aa166c5
MK
117.IR nstype ,
118or there is problem with reassociating the
119the thread with the specified namespace.
dfa22c8b
EB
120.TP
121.B ENOMEM
122Cannot allocate sufficient memory to change the specified namespace.
dfa22c8b
EB
123.TP
124.B EPERM
c409c4ff 125The calling thread did not have the required privilege
63b6ef41
MK
126.RB ( CAP_SYS_ADMIN )
127for this operation.
dfa22c8b
EB
128.SH VERSIONS
129The
130.BR setns ()
c95b6ae1 131system call first appeared in Linux in kernel 3.0;
99fd2fe3
EB
132library support was added to glibc in version 2.14;
133Support for PID, user and mount namespaces first appeard in Linux in kernel 3.8.
dfa22c8b
EB
134.SH CONFORMING TO
135The
136.BR setns ()
137system call is Linux-specific.
138.SH NOTES
7aa166c5
MK
139Not all of the attributes that can be shared when
140a new thread is created using
dfa22c8b
EB
141.BR clone (2)
142can be changed using
143.BR setns ().
8d41e607
MK
144.SH EXAMPLE
145The program below takes two or more arguments.
146The first argument specifies the pathname of a namespace file in an existing
147.I /proc/[pid]/ns/
148directory.
149The remaining arguments specify a command and its arguments.
150The program opens the namespace file, joins that namespace using
151.BR setns (),
152and executes the specified command inside that namespace.
153
154The following shell session demonstrates the use of this program
155(compiled as a binary named
a1ce9159 156.IR ns_exec )
8d41e607
MK
157in conjunction with the
158.BR CLONE_NEWUTS
159example program in the
160.BR clone (2)
161man page (complied as a binary named
162.IR newuts ).
163
164We begin by executing the example program in
165.BR clone (2)
166in the background.
167That program creates a child in a separate UTS namespace.
51ebe080 168The child changes the hostname in its namespace,
8d41e607
MK
169and then both processes display the hostnames in their UTS namespaces,
170so that we can see that they are different.
171
172.nf
173.in +4n
174$ \fBsu\fP # Need privilege for namespace operations
9f1b9726 175Password:
8d41e607
MK
176# \fB./newuts bizarro &\fP
177[1] 3549
178clone() returned 3550
179uts.nodename in child: bizarro
180uts.nodename in parent: antero
5c977011 181# \fBuname \-n\fP # Verify hostname in the shell
8d41e607
MK
182antero
183.in
184.fi
185
186We then run the program shown below,
187using it to execute a shell.
188Inside that shell, we verify that the hostname is the one
189set by the child created by the first program:
190
191.nf
192.in +4n
a1ce9159 193# \fB./ns_exec /proc/3550/ns/uts /bin/bash\fP
5c977011 194# \fBuname \-n\fP # Executed in shell started by ns_exec
8d41e607
MK
195bizarro
196.in
197.fi
8d41e607
MK
198.SS Program source
199.nf
200#define _GNU_SOURCE
201#include <fcntl.h>
202#include <sched.h>
203#include <unistd.h>
204#include <stdlib.h>
205#include <stdio.h>
206
207#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
208 } while (0)
209
210int
211main(int argc, char *argv[])
212{
213 int fd;
214
215 if (argc < 3) {
216 fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\\n", argv[0]);
217 exit(EXIT_FAILURE);
218 }
219
220 fd = open(argv[1], O_RDONLY); /* Get descriptor for namespace */
221 if (fd == \-1)
222 errExit("open");
223
224 if (setns(fd, 0) == \-1) /* Join that namespace */
225 errExit("setns");
226
227 execvp(argv[2], &argv[2]); /* Execute a command in namespace */
228 errExit("execvp");
229}
230.fi
dfa22c8b
EB
231.SH SEE ALSO
232.BR clone (2),
233.BR fork (2),
b93a34b5 234.BR vfork (2),
92dcb220
MK
235.BR proc (5),
236.BR unix (7)