]>
Commit | Line | Data |
---|---|---|
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 | 10 | setns \- 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 | 19 | Given a file descriptor referring to a namespace, |
7aa166c5 | 20 | reassociate the calling thread with that namespace. |
dfa22c8b | 21 | |
92dcb220 MK |
22 | The |
23 | .I fd | |
24 | argument is a file descriptor referring to one of the namespace entries in a | |
25 | .I /proc/[pid]/ns/ | |
26 | directory; see | |
27 | .BR proc (5) | |
c409c4ff | 28 | for further information on |
92dcb220 | 29 | .IR /proc/[pid]/ns/ . |
7aa166c5 | 30 | The calling thread will be reassociated with the corresponding namespace, |
92dcb220 MK |
31 | subject to any constraints imposed by the |
32 | .I nstype | |
33 | argument. | |
34 | ||
dfa22c8b EB |
35 | The |
36 | .I nstype | |
c07ad242 | 37 | argument specifies which type of namespace |
7aa166c5 | 38 | the calling thread may be reassociated with. |
4e1e208d | 39 | This argument can have one of the following values: |
dfa22c8b EB |
40 | .TP |
41 | .BR 0 | |
92dcb220 | 42 | Allow any type of namespace to be joined. |
dfa22c8b EB |
43 | .TP |
44 | .BR CLONE_NEWIPC | |
92dcb220 MK |
45 | .I fd |
46 | must refer to an IPC namespace. | |
dfa22c8b EB |
47 | .TP |
48 | .BR CLONE_NEWNET | |
92dcb220 MK |
49 | .I fd |
50 | must refer to a network namespace. | |
dfa22c8b | 51 | .TP |
99fd2fe3 EB |
52 | .BR CLONE_NEWNS |
53 | .I fd | |
54 | must refer to a mount namespace. | |
55 | .TP | |
56 | .BR CLONE_NEWPID | |
57 | .I fd | |
58 | must refer to a PID namespace. | |
59 | .TP | |
60 | .BR CLONE_NEWUSER | |
61 | .I fd | |
62 | must refer to a user namespace. | |
63 | .TP | |
dfa22c8b | 64 | .BR CLONE_NEWUTS |
92dcb220 MK |
65 | .I fd |
66 | must refer to a UTS namespace. | |
dfa22c8b | 67 | .PP |
92dcb220 MK |
68 | Specifying |
69 | .I nstype | |
70 | as 0 suffices if the caller knows (or does not care) | |
71 | what type of namespace is referred to by | |
72 | .IR fd . | |
73 | Specifying a nonzero value for | |
74 | .I nstype | |
75 | is useful if the caller does not know what type of namespace is referred to by | |
76 | .IR fd | |
77 | and 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 | |
80 | if the file descriptor was opened by another process and, for example, | |
81 | passed to the caller via a UNIX domain socket.) | |
99fd2fe3 | 82 | |
f16c7698 MK |
83 | .B CLONE_NEWPID |
84 | behaves somewhat differently from the other | |
85 | .I nstype | |
86 | values: | |
87 | reassociating the calling thread with a PID namespace only changes | |
88 | the PID namespace that child processes of the caller will be created in; | |
89 | it does not change the PID namespace of the caller itself. | |
90 | Reassociating with a PID namespace is only allowed if the | |
cd7e05aa | 91 | PID namespace specified by |
99fd2fe3 | 92 | .IR fd |
f16c7698 MK |
93 | is a descendant (child, grandchild, etc.) |
94 | PID namespace of the PID namespace of the caller. | |
99fd2fe3 | 95 | |
cd7e05aa MK |
96 | A multi-threaded process may not change user namespace with |
97 | .BR setns (). | |
49af76fe | 98 | A process may not reassociate the thread with the caller's user |
cd7e05aa | 99 | namespace. |
49af76fe | 100 | A process reassociating itself with a user namespace must have |
cd7e05aa | 101 | .B CAP_SYS_ADMIN |
49af76fe | 102 | .\" See kernel/user_namespace.c:userns_install() [3.8 source] |
cd7e05aa | 103 | privileges in the target user namespace. |
99fd2fe3 EB |
104 | |
105 | A process may not be reassociated with a new mount namespace if it is | |
49af76fe MK |
106 | multi-threaded. |
107 | .\" Above check is in fs/namespace.c:mntns_install() [3.8 source] | |
108 | Changing the mount namespace requires that the caller possess both | |
cd7e05aa MK |
109 | .B CAP_SYS_CHROOT |
110 | and | |
49af76fe MK |
111 | .BR CAP_SYS_ADMIN |
112 | capabilities. | |
99fd2fe3 | 113 | |
dfa22c8b | 114 | .SH RETURN VALUE |
92dcb220 MK |
115 | On success, |
116 | .IR setns () | |
117 | returns 0. | |
dfa22c8b EB |
118 | On failure, \-1 is returned and |
119 | .I errno | |
120 | is set to indicate the error. | |
121 | .SH ERRORS | |
122 | .TP | |
dfa22c8b | 123 | .B EBADF |
c07ad242 MK |
124 | .I fd |
125 | is not a valid file descriptor. | |
dfa22c8b EB |
126 | .TP |
127 | .B EINVAL | |
92dcb220 MK |
128 | .I fd |
129 | refers to a namespace whose type does not match that specified in | |
7aa166c5 | 130 | .IR nstype , |
49af76fe | 131 | or there is problem with reassociating |
7aa166c5 | 132 | the thread with the specified namespace. |
dfa22c8b EB |
133 | .TP |
134 | .B ENOMEM | |
135 | Cannot allocate sufficient memory to change the specified namespace. | |
dfa22c8b EB |
136 | .TP |
137 | .B EPERM | |
c409c4ff | 138 | The calling thread did not have the required privilege |
63b6ef41 MK |
139 | .RB ( CAP_SYS_ADMIN ) |
140 | for this operation. | |
dfa22c8b EB |
141 | .SH VERSIONS |
142 | The | |
143 | .BR setns () | |
c95b6ae1 | 144 | system call first appeared in Linux in kernel 3.0; |
99fd2fe3 | 145 | library support was added to glibc in version 2.14; |
cd7e05aa | 146 | Support for PID, user, and mount namespaces first appeared in Linux 3.8. |
dfa22c8b EB |
147 | .SH CONFORMING TO |
148 | The | |
149 | .BR setns () | |
150 | system call is Linux-specific. | |
151 | .SH NOTES | |
7aa166c5 MK |
152 | Not all of the attributes that can be shared when |
153 | a new thread is created using | |
dfa22c8b EB |
154 | .BR clone (2) |
155 | can be changed using | |
156 | .BR setns (). | |
8d41e607 MK |
157 | .SH EXAMPLE |
158 | The program below takes two or more arguments. | |
159 | The first argument specifies the pathname of a namespace file in an existing | |
160 | .I /proc/[pid]/ns/ | |
161 | directory. | |
162 | The remaining arguments specify a command and its arguments. | |
163 | The program opens the namespace file, joins that namespace using | |
164 | .BR setns (), | |
165 | and executes the specified command inside that namespace. | |
166 | ||
167 | The following shell session demonstrates the use of this program | |
168 | (compiled as a binary named | |
a1ce9159 | 169 | .IR ns_exec ) |
8d41e607 MK |
170 | in conjunction with the |
171 | .BR CLONE_NEWUTS | |
172 | example program in the | |
173 | .BR clone (2) | |
174 | man page (complied as a binary named | |
175 | .IR newuts ). | |
176 | ||
177 | We begin by executing the example program in | |
178 | .BR clone (2) | |
179 | in the background. | |
180 | That program creates a child in a separate UTS namespace. | |
51ebe080 | 181 | The child changes the hostname in its namespace, |
8d41e607 MK |
182 | and then both processes display the hostnames in their UTS namespaces, |
183 | so that we can see that they are different. | |
184 | ||
185 | .nf | |
186 | .in +4n | |
187 | $ \fBsu\fP # Need privilege for namespace operations | |
9f1b9726 | 188 | Password: |
8d41e607 MK |
189 | # \fB./newuts bizarro &\fP |
190 | [1] 3549 | |
191 | clone() returned 3550 | |
192 | uts.nodename in child: bizarro | |
193 | uts.nodename in parent: antero | |
5c977011 | 194 | # \fBuname \-n\fP # Verify hostname in the shell |
8d41e607 MK |
195 | antero |
196 | .in | |
197 | .fi | |
198 | ||
199 | We then run the program shown below, | |
200 | using it to execute a shell. | |
201 | Inside that shell, we verify that the hostname is the one | |
202 | set by the child created by the first program: | |
203 | ||
204 | .nf | |
205 | .in +4n | |
a1ce9159 | 206 | # \fB./ns_exec /proc/3550/ns/uts /bin/bash\fP |
5c977011 | 207 | # \fBuname \-n\fP # Executed in shell started by ns_exec |
8d41e607 MK |
208 | bizarro |
209 | .in | |
210 | .fi | |
8d41e607 MK |
211 | .SS Program source |
212 | .nf | |
213 | #define _GNU_SOURCE | |
214 | #include <fcntl.h> | |
215 | #include <sched.h> | |
216 | #include <unistd.h> | |
217 | #include <stdlib.h> | |
218 | #include <stdio.h> | |
219 | ||
220 | #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\ | |
221 | } while (0) | |
222 | ||
223 | int | |
224 | main(int argc, char *argv[]) | |
225 | { | |
226 | int fd; | |
227 | ||
228 | if (argc < 3) { | |
229 | fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\\n", argv[0]); | |
230 | exit(EXIT_FAILURE); | |
231 | } | |
232 | ||
233 | fd = open(argv[1], O_RDONLY); /* Get descriptor for namespace */ | |
234 | if (fd == \-1) | |
235 | errExit("open"); | |
236 | ||
237 | if (setns(fd, 0) == \-1) /* Join that namespace */ | |
238 | errExit("setns"); | |
239 | ||
240 | execvp(argv[2], &argv[2]); /* Execute a command in namespace */ | |
241 | errExit("execvp"); | |
242 | } | |
243 | .fi | |
dfa22c8b EB |
244 | .SH SEE ALSO |
245 | .BR clone (2), | |
246 | .BR fork (2), | |
2a9f74a9 | 247 | .BR unshare (2), |
b93a34b5 | 248 | .BR vfork (2), |
92dcb220 MK |
249 | .BR proc (5), |
250 | .BR unix (7) |