Changes.old: tfix
[man-pages.git] / man7 / pid_namespaces.7
blobcbb03233b25e56460105aca0d2468a26f4f122bb
1 .\" Copyright (c) 2013 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (c) 2012 by Eric W. Biederman <ebiederm@xmission.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\"
27 .TH PID_NAMESPACES 7 2017-05-03 "Linux" "Linux Programmer's Manual"
28 .SH NAME
29 pid_namespaces \- overview of Linux PID namespaces
30 .SH DESCRIPTION
31 For an overview of namespaces, see
32 .BR namespaces (7).
34 PID namespaces isolate the process ID number space,
35 meaning that processes in different PID namespaces can have the same PID.
36 PID namespaces allow containers to provide functionality
37 such as suspending/resuming the set of processes in the container and
38 migrating the container to a new host
39 while the processes inside the container maintain the same PIDs.
41 PIDs in a new PID namespace start at 1,
42 somewhat like a standalone system, and calls to
43 .BR fork (2),
44 .BR vfork (2),
46 .BR clone (2)
47 will produce processes with PIDs that are unique within the namespace.
49 Use of PID namespaces requires a kernel that is configured with the
50 .B CONFIG_PID_NS
51 option.
52 .\"
53 .\" ============================================================
54 .\"
55 .SS The namespace "init" process
56 The first process created in a new namespace
57 (i.e., the process created using
58 .BR clone (2)
59 with the
60 .BR CLONE_NEWPID
61 flag, or the first child created by a process after a call to
62 .BR unshare (2)
63 using the
64 .BR CLONE_NEWPID
65 flag) has the PID 1, and is the "init" process for the namespace (see
66 .BR init (1)).
67 A child process that is orphaned within the namespace will be reparented
68 to this process rather than
69 .BR init (1)
70 (unless one of the ancestors of the child
71 in the same PID namespace employed the
72 .BR prctl (2)
73 .B PR_SET_CHILD_SUBREAPER
74 command to mark itself as the reaper of orphaned descendant processes).
76 If the "init" process of a PID namespace terminates,
77 the kernel terminates all of the processes in the namespace via a
78 .BR SIGKILL
79 signal.
80 This behavior reflects the fact that the "init" process
81 is essential for the correct operation of a PID namespace.
82 In this case, a subsequent
83 .BR fork (2)
84 into this PID namespace will fail with the error
85 .BR ENOMEM ;
86 it is not possible to create a new processes in a PID namespace whose "init"
87 process has terminated.
88 Such scenarios can occur when, for example,
89 a process uses an open file descriptor for a
90 .I /proc/[pid]/ns/pid
91 file corresponding to a process that was in a namespace to
92 .BR setns (2)
93 into that namespace after the "init" process has terminated.
94 Another possible scenario can occur after a call to
95 .BR unshare (2):
96 if the first child subsequently created by a
97 .BR fork (2)
98 terminates, then subsequent calls to
99 .BR fork (2)
100 will fail with
101 .BR ENOMEM .
103 Only signals for which the "init" process has established a signal handler
104 can be sent to the "init" process by other members of the PID namespace.
105 This restriction applies even to privileged processes,
106 and prevents other members of the PID namespace from
107 accidentally killing the "init" process.
109 Likewise, a process in an ancestor namespace
110 can\(emsubject to the usual permission checks described in
111 .BR kill (2)\(emsend
112 signals to the "init" process of a child PID namespace only
113 if the "init" process has established a handler for that signal.
114 (Within the handler, the
115 .I siginfo_t
116 .I si_pid
117 field described in
118 .BR sigaction (2)
119 will be zero.)
120 .B SIGKILL
122 .B SIGSTOP
123 are treated exceptionally:
124 these signals are forcibly delivered when sent from an ancestor PID namespace.
125 Neither of these signals can be caught by the "init" process,
126 and so will result in the usual actions associated with those signals
127 (respectively, terminating and stopping the process).
129 Starting with Linux 3.4, the
130 .BR reboot (2)
131 system call causes a signal to be sent to the namespace "init" process.
133 .BR reboot (2)
134 for more details.
136 .\" ============================================================
138 .SS Nesting PID namespaces
139 PID namespaces can be nested:
140 each PID namespace has a parent,
141 except for the initial ("root") PID namespace.
142 The parent of a PID namespace is the PID namespace of the process that
143 created the namespace using
144 .BR clone (2)
146 .BR unshare (2).
147 PID namespaces thus form a tree,
148 with all namespaces ultimately tracing their ancestry to the root namespace.
149 Since Linux 3.7,
150 .\" commit f2302505775fd13ba93f034206f1e2a587017929
151 .\" The kernel constant MAX_PID_NS_LEVEL
152 the kernel limits the maximum nesting depth for PID namespaces to 32.
154 A process is visible to other processes in its PID namespace,
155 and to the processes in each direct ancestor PID namespace
156 going back to the root PID namespace.
157 In this context, "visible" means that one process
158 can be the target of operations by another process using
159 system calls that specify a process ID.
160 Conversely, the processes in a child PID namespace can't see
161 processes in the parent and further removed ancestor namespaces.
162 More succinctly: a process can see (e.g., send signals with
163 .BR kill (2),
164 set nice values with
165 .BR setpriority (2),
166 etc.) only processes contained in its own PID namespace
167 and in descendants of that namespace.
169 A process has one process ID in each of the layers of the PID
170 namespace hierarchy in which is visible,
171 and walking back though each direct ancestor namespace
172 through to the root PID namespace.
173 System calls that operate on process IDs always
174 operate using the process ID that is visible in the
175 PID namespace of the caller.
176 A call to
177 .BR getpid (2)
178 always returns the PID associated with the namespace in which
179 the process was created.
181 Some processes in a PID namespace may have parents
182 that are outside of the namespace.
183 For example, the parent of the initial process in the namespace
184 (i.e., the
185 .BR init (1)
186 process with PID 1) is necessarily in another namespace.
187 Likewise, the direct children of a process that uses
188 .BR setns (2)
189 to cause its children to join a PID namespace are in a different
190 PID namespace from the caller of
191 .BR setns (2).
192 Calls to
193 .BR getppid (2)
194 for such processes return 0.
196 While processes may freely descend into child PID namespaces
197 (e.g., using
198 .BR setns (2)
199 with a PID namespace file descriptor),
200 they may not move in the other direction.
201 That is to say, processes may not enter any ancestor namespaces
202 (parent, grandparent, etc.).
203 Changing PID namespaces is a one-way operation.
206 .BR NS_GET_PARENT
207 .BR ioctl (2)
208 operation can be used to discover the parental relationship
209 between PID namespaces; see
210 .BR ioctl_ns (2).
212 .\" ============================================================
214 .SS setns(2) and unshare(2) semantics
215 Calls to
216 .BR setns (2)
217 that specify a PID namespace file descriptor
218 and calls to
219 .BR unshare (2)
220 with the
221 .BR CLONE_NEWPID
222 flag cause children subsequently created
223 by the caller to be placed in a different PID namespace from the caller.
224 (Since Linux 4.12, that PID namespace is shown via the
225 .IR /proc/[pid]/ns/pid_for_children
226 file, as described in
227 .BR namespaces (7).)
228 These calls do not, however,
229 change the PID namespace of the calling process,
230 because doing so would change the caller's idea of its own PID
231 (as reported by
232 .BR getpid ()),
233 which would break many applications and libraries.
235 To put things another way:
236 a process's PID namespace membership is determined when the process is created
237 and cannot be changed thereafter.
238 Among other things, this means that the parental relationship
239 between processes mirrors the parental relationship between PID namespaces:
240 the parent of a process is either in the same namespace
241 or resides in the immediate parent PID namespace.
242 .SS Compatibility of CLONE_NEWPID with other CLONE_* flags
243 In current versions of Linux,
244 .BR CLONE_NEWPID
245 can't be combined with
246 .BR CLONE_THREAD .
247 Threads are required to be in the same PID namespace such that
248 the threads in a process can send signals to each other.
249 Similarly, it must be possible to see all of the threads
250 of a processes in the
251 .BR proc (5)
252 filesystem.
253 Additionally, if two threads were in different PID
254 namespaces, the process ID of the process sending a signal
255 could not be meaningfully encoded when a signal is sent
256 (see the description of the
257 .I siginfo_t
258 type in
259 .BR sigaction (2)).
260 Since this is computed when a signal is enqueued,
261 a signal queue shared by processes in multiple PID namespaces
262 would defeat that.
264 .\" Note these restrictions were all introduced in
265 .\" 8382fcac1b813ad0a4e68a838fc7ae93fa39eda0
266 .\" when CLONE_NEWPID|CLONE_VM was disallowed
267 In earlier versions of Linux,
268 .BR CLONE_NEWPID
269 was additionally disallowed (failing with the error
270 .BR EINVAL )
271 in combination with
272 .BR CLONE_SIGHAND
273 .\" (restriction lifted in faf00da544045fdc1454f3b9e6d7f65c841de302)
274 (before Linux 4.3) as well as
275 .\" (restriction lifted in e79f525e99b04390ca4d2366309545a836c03bf1)
276 .BR CLONE_VM
277 (before Linux 3.12).
278 The changes that lifted these restrictions have also been ported to
279 earlier stable kernels.
281 .\" ============================================================
283 .SS /proc and PID namespaces
285 .I /proc
286 filesystem shows (in the
287 .I /proc/[pid]
288 directories) only processes visible in the PID namespace
289 of the process that performed the mount, even if the
290 .I /proc
291 filesystem is viewed from processes in other namespaces.
293 After creating a new PID namespace,
294 it is useful for the child to change its root directory
295 and mount a new procfs instance at
296 .I /proc
297 so that tools such as
298 .BR ps (1)
299 work correctly.
300 If a new mount namespace is simultaneously created by including
301 .BR CLONE_NEWNS
302 in the
303 .IR flags
304 argument of
305 .BR clone (2)
307 .BR unshare (2),
308 then it isn't necessary to change the root directory:
309 a new procfs instance can be mounted directly over
310 .IR /proc .
312 From a shell, the command to mount
313 .I /proc
316     $ mount -t proc proc /proc
318 Calling
319 .BR readlink (2)
320 on the path
321 .I /proc/self
322 yields the process ID of the caller in the PID namespace of the procfs mount
323 (i.e., the PID namespace of the process that mounted the procfs).
324 This can be useful for introspection purposes,
325 when a process wants to discover its PID in other namespaces.
327 .\" ============================================================
329 .SS Miscellaneous
330 When a process ID is passed over a UNIX domain socket to a
331 process in a different PID namespace (see the description of
332 .B SCM_CREDENTIALS
334 .BR unix (7)),
335 it is translated into the corresponding PID value in
336 the receiving process's PID namespace.
337 .SH CONFORMING TO
338 Namespaces are a Linux-specific feature.
339 .SH EXAMPLE
341 .BR user_namespaces (7).
342 .SH SEE ALSO
343 .BR clone (2),
344 .BR setns (2),
345 .BR unshare (2),
346 .BR proc (5),
347 .BR capabilities (7),
348 .BR credentials (7),
349 .BR namespaces (7),
350 .BR user_namespaces (7),
351 .BR switch_root (8)