]>
Commit | Line | Data |
---|---|---|
fea681da | 1 | .\" Copyright (c) 1992 Drew Eckhardt <drew@cs.colorado.edu>, March 28, 1992 |
8c7b566c | 2 | .\" and Copyright (c) Michael Kerrisk, 2001, 2002, 2005, 2013 |
2297bf0e | 3 | .\" |
fd0fc519 | 4 | .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE) |
fea681da | 5 | .\" May be distributed under the GNU General Public License. |
fd0fc519 | 6 | .\" %%%LICENSE_END |
dccaff1e | 7 | .\" |
fea681da MK |
8 | .\" Modified by Michael Haardt <michael@moria.de> |
9 | .\" Modified 24 Jul 1993 by Rik Faith <faith@cs.unc.edu> | |
10 | .\" Modified 21 Aug 1994 by Michael Chastain <mec@shell.portal.com>: | |
11 | .\" New man page (copied from 'fork.2'). | |
12 | .\" Modified 10 June 1995 by Andries Brouwer <aeb@cwi.nl> | |
13 | .\" Modified 25 April 1998 by Xavier Leroy <Xavier.Leroy@inria.fr> | |
14 | .\" Modified 26 Jun 2001 by Michael Kerrisk | |
15 | .\" Mostly upgraded to 2.4.x | |
16 | .\" Added prototype for sys_clone() plus description | |
17 | .\" Added CLONE_THREAD with a brief description of thread groups | |
c13182ef | 18 | .\" Added CLONE_PARENT and revised entire page remove ambiguity |
fea681da MK |
19 | .\" between "calling process" and "parent process" |
20 | .\" Added CLONE_PTRACE and CLONE_VFORK | |
21 | .\" Added EPERM and EINVAL error codes | |
fd8a5be4 | 22 | .\" Renamed "__clone" to "clone" (which is the prototype in <sched.h>) |
fea681da | 23 | .\" various other minor tidy ups and clarifications. |
c11b1abf | 24 | .\" Modified 26 Jun 2001 by Michael Kerrisk <mtk.manpages@gmail.com> |
d9bfdb9c | 25 | .\" Updated notes for 2.4.7+ behavior of CLONE_THREAD |
c11b1abf | 26 | .\" Modified 15 Oct 2002 by Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da MK |
27 | .\" Added description for CLONE_NEWNS, which was added in 2.4.19 |
28 | .\" Slightly rephrased, aeb. | |
29 | .\" Modified 1 Feb 2003 - added CLONE_SIGHAND restriction, aeb. | |
30 | .\" Modified 1 Jan 2004 - various updates, aeb | |
0967c11f | 31 | .\" Modified 2004-09-10 - added CLONE_PARENT_SETTID etc. - aeb. |
d9bfdb9c | 32 | .\" 2005-04-12, mtk, noted the PID caching behavior of NPTL's getpid() |
31830ef0 | 33 | .\" wrapper under BUGS. |
fd8a5be4 MK |
34 | .\" 2005-05-10, mtk, added CLONE_SYSVSEM, CLONE_UNTRACED, CLONE_STOPPED. |
35 | .\" 2005-05-17, mtk, Substantially enhanced discussion of CLONE_THREAD. | |
4e836144 | 36 | .\" 2008-11-18, mtk, order CLONE_* flags alphabetically |
82ee147a | 37 | .\" 2008-11-18, mtk, document CLONE_NEWPID |
43ce9dda | 38 | .\" 2008-11-19, mtk, document CLONE_NEWUTS |
667417b3 | 39 | .\" 2008-11-19, mtk, document CLONE_NEWIPC |
cfdc761b | 40 | .\" 2008-11-19, Jens Axboe, mtk, document CLONE_IO |
fea681da | 41 | .\" |
4b8c67d9 | 42 | .TH CLONE 2 2017-09-15 "Linux" "Linux Programmer's Manual" |
fea681da | 43 | .SH NAME |
9b0e0996 | 44 | clone, __clone2 \- create a child process |
fea681da | 45 | .SH SYNOPSIS |
c10859eb | 46 | .nf |
81f10dad | 47 | /* Prototype for the glibc wrapper function */ |
dbfe9c70 | 48 | .PP |
4f71ba5d | 49 | .B #define _GNU_SOURCE |
fea681da | 50 | .B #include <sched.h> |
dbfe9c70 | 51 | .PP |
ff929e3b MK |
52 | .BI "int clone(int (*" "fn" ")(void *), void *" child_stack , |
53 | .BI " int " flags ", void *" "arg" ", ... " | |
dd6d3d2e | 54 | .BI " /* pid_t *" ptid ", void *" newtls \ |
ff929e3b | 55 | ", pid_t *" ctid " */ );" |
dbfe9c70 | 56 | .PP |
2a15a76b | 57 | /* For the prototype of the raw system call, see NOTES */ |
c10859eb | 58 | .fi |
fea681da | 59 | .SH DESCRIPTION |
edcc65ff MK |
60 | .BR clone () |
61 | creates a new process, in a manner similar to | |
fea681da | 62 | .BR fork (2). |
efeece04 | 63 | .PP |
81f10dad | 64 | This page describes both the glibc |
e511ffb6 | 65 | .BR clone () |
e585064b | 66 | wrapper function and the underlying system call on which it is based. |
81f10dad | 67 | The main text describes the wrapper function; |
e585064b | 68 | the differences for the raw system call |
81f10dad | 69 | are described toward the end of this page. |
efeece04 | 70 | .PP |
fea681da MK |
71 | Unlike |
72 | .BR fork (2), | |
81f10dad MK |
73 | .BR clone () |
74 | allows the child process to share parts of its execution context with | |
4ba17a6d | 75 | the calling process, such as the virtual address space, the table of file |
c13182ef MK |
76 | descriptors, and the table of signal handlers. |
77 | (Note that on this manual | |
78 | page, "calling process" normally corresponds to "parent process". | |
79 | But see the description of | |
80 | .B CLONE_PARENT | |
fea681da | 81 | below.) |
efeece04 | 82 | .PP |
1533d242 | 83 | One use of |
edcc65ff | 84 | .BR clone () |
4ba17a6d MK |
85 | is to implement threads: multiple flows of control in a program that |
86 | run concurrently in a shared address space. | |
efeece04 | 87 | .PP |
fea681da | 88 | When the child process is created with |
c13182ef | 89 | .BR clone (), |
7495cbc7 MK |
90 | it commences execution by calling the function pointed to by the argument |
91 | .IR fn . | |
fea681da | 92 | (This differs from |
c13182ef | 93 | .BR fork (2), |
fea681da | 94 | where execution continues in the child from the point |
c13182ef MK |
95 | of the |
96 | .BR fork (2) | |
fea681da MK |
97 | call.) |
98 | The | |
fea681da | 99 | .I arg |
7495cbc7 MK |
100 | argument is passed as the argument of the function |
101 | .IR fn . | |
efeece04 | 102 | .PP |
c13182ef | 103 | When the |
fea681da | 104 | .IR fn ( arg ) |
4ba17a6d | 105 | function returns, the child process terminates. |
c13182ef | 106 | The integer returned by |
fea681da | 107 | .I fn |
4ba17a6d | 108 | is the exit status for the child process. |
c13182ef | 109 | The child process may also terminate explicitly by calling |
fea681da MK |
110 | .BR exit (2) |
111 | or after receiving a fatal signal. | |
efeece04 | 112 | .PP |
fea681da MK |
113 | The |
114 | .I child_stack | |
c13182ef MK |
115 | argument specifies the location of the stack used by the child process. |
116 | Since the child and calling process may share memory, | |
fea681da | 117 | it is not possible for the child process to execute in the |
c13182ef MK |
118 | same stack as the calling process. |
119 | The calling process must therefore | |
fea681da MK |
120 | set up memory space for the child stack and pass a pointer to this |
121 | space to | |
edcc65ff | 122 | .BR clone (). |
5fab2e7c | 123 | Stacks grow downward on all processors that run Linux |
fea681da MK |
124 | (except the HP PA processors), so |
125 | .I child_stack | |
126 | usually points to the topmost address of the memory space set up for | |
127 | the child stack. | |
efeece04 | 128 | .PP |
fea681da MK |
129 | The low byte of |
130 | .I flags | |
fd8a5be4 MK |
131 | contains the number of the |
132 | .I "termination signal" | |
133 | sent to the parent when the child dies. | |
134 | If this signal is specified as anything other than | |
fea681da MK |
135 | .BR SIGCHLD , |
136 | then the parent process must specify the | |
c13182ef MK |
137 | .B __WALL |
138 | or | |
fea681da | 139 | .B __WCLONE |
c13182ef MK |
140 | options when waiting for the child with |
141 | .BR wait (2). | |
fea681da MK |
142 | If no signal is specified, then the parent process is not signaled |
143 | when the child terminates. | |
efeece04 | 144 | .PP |
fea681da | 145 | .I flags |
4ba17a6d | 146 | may also be bitwise-ORed with zero or more of the following constants, |
fd8a5be4 | 147 | in order to specify what is shared between the calling process |
fea681da | 148 | and the child process: |
fea681da | 149 | .TP |
f5dbc7c8 | 150 | .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)" |
4ba5392d | 151 | Clear (zero) the child thread ID at the location |
d3dbc9b1 | 152 | .I ctid |
f5dbc7c8 MK |
153 | in child memory when the child exits, and do a wakeup on the futex |
154 | at that address. | |
155 | The address involved may be changed by the | |
156 | .BR set_tid_address (2) | |
157 | system call. | |
158 | This is used by threading libraries. | |
159 | .TP | |
160 | .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)" | |
8ef021ea | 161 | Store the child thread ID at the location |
d3dbc9b1 | 162 | .I ctid |
8ef021ea | 163 | in the child's memory. |
b5da2f91 MK |
164 | The store operation completes before |
165 | .BR clone () | |
166 | returns control to user space. | |
f5dbc7c8 | 167 | .TP |
1603d6a1 | 168 | .BR CLONE_FILES " (since Linux 2.0)" |
fea681da | 169 | If |
f5dbc7c8 MK |
170 | .B CLONE_FILES |
171 | is set, the calling process and the child process share the same file | |
172 | descriptor table. | |
173 | Any file descriptor created by the calling process or by the child | |
174 | process is also valid in the other process. | |
175 | Similarly, if one of the processes closes a file descriptor, | |
176 | or changes its associated flags (using the | |
177 | .BR fcntl (2) | |
178 | .B F_SETFD | |
179 | operation), the other process is also affected. | |
8a76b19e KE |
180 | If a process sharing a file descriptor table calls |
181 | .BR execve (2), | |
182 | its file descriptor table is duplicated (unshared). | |
efeece04 | 183 | .IP |
fea681da | 184 | If |
f5dbc7c8 MK |
185 | .B CLONE_FILES |
186 | is not set, the child process inherits a copy of all file descriptors | |
187 | opened in the calling process at the time of | |
188 | .BR clone (). | |
f5dbc7c8 MK |
189 | Subsequent operations that open or close file descriptors, |
190 | or change file descriptor flags, | |
191 | performed by either the calling | |
192 | process or the child process do not affect the other process. | |
db8ba2b4 | 193 | Note, however, |
839d161f MK |
194 | that the duplicated file descriptors in the child refer to the same |
195 | open file descriptions as the corresponding file descriptors | |
196 | in the calling process, | |
2433365b | 197 | and thus share file offsets and file status flags (see |
db8ba2b4 | 198 | .BR open (2)). |
fea681da | 199 | .TP |
1603d6a1 | 200 | .BR CLONE_FS " (since Linux 2.0)" |
fea681da MK |
201 | If |
202 | .B CLONE_FS | |
9ee4a2b6 | 203 | is set, the caller and the child process share the same filesystem |
c13182ef | 204 | information. |
9ee4a2b6 | 205 | This includes the root of the filesystem, the current |
c13182ef MK |
206 | working directory, and the umask. |
207 | Any call to | |
fea681da MK |
208 | .BR chroot (2), |
209 | .BR chdir (2), | |
210 | or | |
211 | .BR umask (2) | |
edcc65ff | 212 | performed by the calling process or the child process also affects the |
fea681da | 213 | other process. |
efeece04 | 214 | .IP |
c13182ef | 215 | If |
fea681da | 216 | .B CLONE_FS |
9ee4a2b6 | 217 | is not set, the child process works on a copy of the filesystem |
fea681da | 218 | information of the calling process at the time of the |
edcc65ff | 219 | .BR clone () |
fea681da MK |
220 | call. |
221 | Calls to | |
222 | .BR chroot (2), | |
223 | .BR chdir (2), | |
4ba17a6d | 224 | or |
fea681da MK |
225 | .BR umask (2) |
226 | performed later by one of the processes do not affect the other process. | |
fea681da | 227 | .TP |
a4cc375e | 228 | .BR CLONE_IO " (since Linux 2.6.25)" |
11f27a1c JA |
229 | If |
230 | .B CLONE_IO | |
231 | is set, then the new process shares an I/O context with | |
232 | the calling process. | |
233 | If this flag is not set, then (as with | |
234 | .BR fork (2)) | |
235 | the new process has its own I/O context. | |
efeece04 | 236 | .IP |
11f27a1c | 237 | .\" The following based on text from Jens Axboe |
d1f84ed7 | 238 | The I/O context is the I/O scope of the disk scheduler (i.e., |
11f27a1c JA |
239 | what the I/O scheduler uses to model scheduling of a process's I/O). |
240 | If processes share the same I/O context, | |
241 | they are treated as one by the I/O scheduler. | |
242 | As a consequence, they get to share disk time. | |
243 | For some I/O schedulers, | |
244 | .\" the anticipatory and CFQ scheduler | |
245 | if two processes share an I/O context, | |
246 | they will be allowed to interleave their disk access. | |
247 | If several threads are doing I/O on behalf of the same process | |
248 | .RB ( aio_read (3), | |
249 | for instance), they should employ | |
250 | .BR CLONE_IO | |
251 | to get better I/O performance. | |
252 | .\" with CFQ and AS. | |
efeece04 | 253 | .IP |
11f27a1c JA |
254 | If the kernel is not configured with the |
255 | .B CONFIG_BLOCK | |
256 | option, this flag is a no-op. | |
257 | .TP | |
c5af0674 MK |
258 | .BR CLONE_NEWCGROUP " (since Linux 4.6)" |
259 | Create the process in a new cgroup namespace. | |
260 | If this flag is not set, then (as with | |
261 | .BR fork (2)) | |
262 | the process is created in the same cgroup namespaces as the calling process. | |
263 | This flag is intended for the implementation of containers. | |
efeece04 | 264 | .IP |
c5af0674 | 265 | For further information on cgroup namespaces, see |
b9fe4bc3 | 266 | .BR cgroup_namespaces (7). |
efeece04 | 267 | .IP |
c5af0674 MK |
268 | Only a privileged process |
269 | .RB ( CAP_SYS_ADMIN ) | |
270 | can employ | |
271 | .BR CLONE_NEWCGROUP . | |
272 | .\" | |
273 | .TP | |
8722311b | 274 | .BR CLONE_NEWIPC " (since Linux 2.6.19)" |
667417b3 MK |
275 | If |
276 | .B CLONE_NEWIPC | |
277 | is set, then create the process in a new IPC namespace. | |
278 | If this flag is not set, then (as with | |
06b30458 | 279 | .BR fork (2)), |
667417b3 MK |
280 | the process is created in the same IPC namespace as |
281 | the calling process. | |
0236bea9 | 282 | This flag is intended for the implementation of containers. |
efeece04 | 283 | .IP |
efbfd7ec | 284 | An IPC namespace provides an isolated view of System\ V IPC objects (see |
009a049e MK |
285 | .BR svipc (7)) |
286 | and (since Linux 2.6.30) | |
287 | .\" commit 7eafd7c74c3f2e67c27621b987b28397110d643f | |
288 | .\" https://lwn.net/Articles/312232/ | |
289 | POSIX message queues | |
290 | (see | |
291 | .BR mq_overview (7)). | |
19911fa5 MK |
292 | The common characteristic of these IPC mechanisms is that IPC |
293 | objects are identified by mechanisms other than filesystem | |
294 | pathnames. | |
efeece04 | 295 | .IP |
c440fe01 | 296 | Objects created in an IPC namespace are visible to all other processes |
667417b3 MK |
297 | that are members of that namespace, |
298 | but are not visible to processes in other IPC namespaces. | |
efeece04 | 299 | .IP |
83c1f4b5 | 300 | When an IPC namespace is destroyed |
009a049e | 301 | (i.e., when the last process that is a member of the namespace terminates), |
83c1f4b5 | 302 | all IPC objects in the namespace are automatically destroyed. |
efeece04 | 303 | .IP |
ab5dd83f MK |
304 | Only a privileged process |
305 | .RB ( CAP_SYS_ADMIN ) | |
306 | can employ | |
307 | .BR CLONE_NEWIPC . | |
667417b3 MK |
308 | This flag can't be specified in conjunction with |
309 | .BR CLONE_SYSVSEM . | |
efeece04 | 310 | .IP |
9343f8e7 MK |
311 | For further information on IPC namespaces, see |
312 | .BR namespaces (7). | |
667417b3 | 313 | .TP |
163bf178 | 314 | .BR CLONE_NEWNET " (since Linux 2.6.24)" |
33a0ccb2 | 315 | (The implementation of this flag was completed only |
9108d867 | 316 | by about kernel version 2.6.29.) |
efeece04 | 317 | .IP |
163bf178 MK |
318 | If |
319 | .B CLONE_NEWNET | |
320 | is set, then create the process in a new network namespace. | |
321 | If this flag is not set, then (as with | |
57ef8c39 | 322 | .BR fork (2)) |
163bf178 MK |
323 | the process is created in the same network namespace as |
324 | the calling process. | |
325 | This flag is intended for the implementation of containers. | |
efeece04 | 326 | .IP |
163bf178 MK |
327 | A network namespace provides an isolated view of the networking stack |
328 | (network device interfaces, IPv4 and IPv6 protocol stacks, | |
329 | IP routing tables, firewall rules, the | |
330 | .I /proc/net | |
331 | and | |
332 | .I /sys/class/net | |
333 | directory trees, sockets, etc.). | |
334 | A physical network device can live in exactly one | |
335 | network namespace. | |
6f34a82c MK |
336 | A virtual network |
337 | .RB ( veth (4)) | |
338 | device pair provides a pipe-like abstraction | |
163bf178 MK |
339 | that can be used to create tunnels between network namespaces, |
340 | and can be used to create a bridge to a physical network device | |
341 | in another namespace. | |
efeece04 | 342 | .IP |
bf032425 SH |
343 | When a network namespace is freed |
344 | (i.e., when the last process in the namespace terminates), | |
345 | its physical network devices are moved back to the | |
346 | initial network namespace (not to the parent of the process). | |
73680728 MK |
347 | For further information on network namespaces, see |
348 | .BR namespaces (7). | |
efeece04 | 349 | .IP |
ab5dd83f MK |
350 | Only a privileged process |
351 | .RB ( CAP_SYS_ADMIN ) | |
352 | can employ | |
353 | .BR CLONE_NEWNET . | |
163bf178 | 354 | .TP |
c10859eb | 355 | .BR CLONE_NEWNS " (since Linux 2.4.19)" |
3dd2331c MK |
356 | If |
357 | .B CLONE_NEWNS | |
358 | is set, the cloned child is started in a new mount namespace, | |
359 | initialized with a copy of the namespace of the parent. | |
360 | If | |
fea681da | 361 | .B CLONE_NEWNS |
3dd2331c | 362 | is not set, the child lives in the same mount |
4df2eb09 | 363 | namespace as the parent. |
efeece04 | 364 | .IP |
ab5dd83f MK |
365 | Only a privileged process |
366 | .RB ( CAP_SYS_ADMIN ) | |
367 | can employ | |
368 | .BR CLONE_NEWNS . | |
fea681da MK |
369 | It is not permitted to specify both |
370 | .B CLONE_NEWNS | |
371 | and | |
372 | .B CLONE_FS | |
9219d208 | 373 | .\" See https://lwn.net/Articles/543273/ |
fea681da | 374 | in the same |
e511ffb6 | 375 | .BR clone () |
fea681da | 376 | call. |
efeece04 | 377 | .IP |
c212248c MK |
378 | For further information on mount namespaces, see |
379 | .BR namespaces (7) | |
380 | and | |
381 | .BR mount_namespaces (7). | |
9d005472 MK |
382 | .TP |
383 | .BR CLONE_NEWPID " (since Linux 2.6.24)" | |
384 | .\" This explanation draws a lot of details from | |
385 | .\" http://lwn.net/Articles/259217/ | |
386 | .\" Authors: Pavel Emelyanov <xemul@openvz.org> | |
387 | .\" and Kir Kolyshkin <kir@openvz.org> | |
388 | .\" | |
389 | .\" The primary kernel commit is 30e49c263e36341b60b735cbef5ca37912549264 | |
390 | .\" Author: Pavel Emelyanov <xemul@openvz.org> | |
391 | If | |
392 | .B CLONE_NEWPID | |
393 | is set, then create the process in a new PID namespace. | |
394 | If this flag is not set, then (as with | |
395 | .BR fork (2)) | |
396 | the process is created in the same PID namespace as | |
397 | the calling process. | |
398 | This flag is intended for the implementation of containers. | |
efeece04 | 399 | .IP |
9d005472 | 400 | For further information on PID namespaces, see |
7e0e902b MK |
401 | .BR namespaces (7) |
402 | and | |
39b3f005 | 403 | .BR pid_namespaces (7). |
efeece04 | 404 | .IP |
ab5dd83f MK |
405 | Only a privileged process |
406 | .RB ( CAP_SYS_ADMIN ) | |
407 | can employ | |
408 | .BR CLONE_NEWPID . | |
9d005472 | 409 | This flag can't be specified in conjunction with |
f0007192 MK |
410 | .BR CLONE_THREAD |
411 | or | |
412 | .BR CLONE_PARENT . | |
70d21f17 | 413 | .TP |
06b30458 MK |
414 | .BR CLONE_NEWUSER |
415 | (This flag first became meaningful for | |
416 | .BR clone () | |
4d2b3ed7 MK |
417 | in Linux 2.6.23, |
418 | the current | |
11a38815 | 419 | .BR clone () |
4d2b3ed7 MK |
420 | semantics were merged in Linux 3.5, |
421 | and the final pieces to make the user namespaces completely usable were | |
422 | merged in Linux 3.8.) | |
efeece04 | 423 | .IP |
70d21f17 EB |
424 | If |
425 | .B CLONE_NEWUSER | |
06b30458 MK |
426 | is set, then create the process in a new user namespace. |
427 | If this flag is not set, then (as with | |
57ef8c39 | 428 | .BR fork (2)) |
70d21f17 | 429 | the process is created in the same user namespace as the calling process. |
efeece04 | 430 | .IP |
fefbcba8 MK |
431 | Before Linux 3.8, use of |
432 | .BR CLONE_NEWUSER | |
433 | required that the caller have three capabilities: | |
434 | .BR CAP_SYS_ADMIN , | |
435 | .BR CAP_SETUID , | |
436 | and | |
437 | .BR CAP_SETGID . | |
438 | .\" Before Linux 2.6.29, it appears that only CAP_SYS_ADMIN was needed | |
06b30458 | 439 | Starting with Linux 3.8, |
9d005472 | 440 | no privileges are needed to create a user namespace. |
efeece04 | 441 | .IP |
5e72cf7d MK |
442 | This flag can't be specified in conjunction with |
443 | .BR CLONE_THREAD | |
444 | or | |
445 | .BR CLONE_PARENT . | |
446 | For security reasons, | |
447 | .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71 | |
448 | .\" https://lwn.net/Articles/543273/ | |
449 | .\" The fix actually went into 3.9 and into 3.8.3. However, user namespaces | |
450 | .\" were, for practical purposes, unusable in earlier 3.8.x because of the | |
ab3311aa | 451 | .\" various filesystems that didn't support userns. |
f0007192 MK |
452 | .BR CLONE_NEWUSER |
453 | cannot be specified in conjunction with | |
5e72cf7d | 454 | .BR CLONE_FS . |
efeece04 | 455 | .IP |
5e72cf7d | 456 | For further information on user namespaces, see |
e5f3df48 MK |
457 | .BR namespaces (7) |
458 | and | |
5e72cf7d | 459 | .BR user_namespaces (7). |
82ee147a | 460 | .TP |
43ce9dda MK |
461 | .BR CLONE_NEWUTS " (since Linux 2.6.19)" |
462 | If | |
463 | .B CLONE_NEWUTS | |
e1b11906 MK |
464 | is set, then create the process in a new UTS namespace, |
465 | whose identifiers are initialized by duplicating the identifiers | |
466 | from the UTS namespace of the calling process. | |
43ce9dda | 467 | If this flag is not set, then (as with |
57ef8c39 | 468 | .BR fork (2)) |
43ce9dda MK |
469 | the process is created in the same UTS namespace as |
470 | the calling process. | |
0236bea9 | 471 | This flag is intended for the implementation of containers. |
efeece04 | 472 | .IP |
43ce9dda MK |
473 | A UTS namespace is the set of identifiers returned by |
474 | .BR uname (2); | |
850905cf | 475 | among these, the domain name and the hostname can be modified by |
43ce9dda MK |
476 | .BR setdomainname (2) |
477 | and | |
43ce9dda MK |
478 | .BR sethostname (2), |
479 | respectively. | |
c440fe01 MK |
480 | Changes made to the identifiers in a UTS namespace |
481 | are visible to all other processes in the same namespace, | |
43ce9dda | 482 | but are not visible to processes in other UTS namespaces. |
efeece04 | 483 | .IP |
ab5dd83f MK |
484 | Only a privileged process |
485 | .RB ( CAP_SYS_ADMIN ) | |
486 | can employ | |
487 | .BR CLONE_NEWUTS . | |
efeece04 | 488 | .IP |
83d9e9b2 | 489 | For further information on UTS namespaces, see |
9cc7ad66 | 490 | .BR namespaces (7). |
43ce9dda | 491 | .TP |
f5dbc7c8 MK |
492 | .BR CLONE_PARENT " (since Linux 2.3.12)" |
493 | If | |
494 | .B CLONE_PARENT | |
495 | is set, then the parent of the new child (as returned by | |
496 | .BR getppid (2)) | |
497 | will be the same as that of the calling process. | |
efeece04 | 498 | .IP |
f5dbc7c8 MK |
499 | If |
500 | .B CLONE_PARENT | |
501 | is not set, then (as with | |
502 | .BR fork (2)) | |
503 | the child's parent is the calling process. | |
efeece04 | 504 | .IP |
f5dbc7c8 MK |
505 | Note that it is the parent process, as returned by |
506 | .BR getppid (2), | |
507 | which is signaled when the child terminates, so that | |
508 | if | |
509 | .B CLONE_PARENT | |
510 | is set, then the parent of the calling process, rather than the | |
511 | calling process itself, will be signaled. | |
512 | .TP | |
513 | .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)" | |
8ef021ea | 514 | Store the child thread ID at the location |
d3dbc9b1 | 515 | .I ptid |
8ef021ea | 516 | in the parent's memory. |
f5dbc7c8 MK |
517 | (In Linux 2.5.32-2.5.48 there was a flag |
518 | .B CLONE_SETTID | |
519 | that did this.) | |
b5da2f91 MK |
520 | The store operation completes before |
521 | .BR clone () | |
522 | returns control to user space. | |
f5dbc7c8 | 523 | .TP |
1c173eb3 | 524 | .BR CLONE_PID " (Linux 2.0 to 2.5.15)" |
f5dbc7c8 MK |
525 | If |
526 | .B CLONE_PID | |
527 | is set, the child process is created with the same process ID as | |
528 | the calling process. | |
529 | This is good for hacking the system, but otherwise | |
530 | of not much use. | |
1c173eb3 | 531 | From Linux 2.3.21 onward, this flag could be |
f5dbc7c8 | 532 | specified only by the system boot process (PID 0). |
1c173eb3 MK |
533 | The flag disappeared completely from the kernel sources in Linux 2.5.16. |
534 | Since then, the kernel silently ignores this bit if it is specified in | |
535 | .IR flags . | |
f5dbc7c8 | 536 | .TP |
1603d6a1 | 537 | .BR CLONE_PTRACE " (since Linux 2.2)" |
f5dbc7c8 MK |
538 | If |
539 | .B CLONE_PTRACE | |
540 | is specified, and the calling process is being traced, | |
541 | then trace the child also (see | |
542 | .BR ptrace (2)). | |
543 | .TP | |
544 | .BR CLONE_SETTLS " (since Linux 2.5.32)" | |
dd6d3d2e | 545 | The TLS (Thread Local Storage) descriptor is set to |
2551f801 | 546 | .IR newtls . |
efeece04 | 547 | .IP |
dd6d3d2e KF |
548 | The interpretation of |
549 | .I newtls | |
550 | and the resulting effect is architecture dependent. | |
551 | On x86, | |
f5dbc7c8 | 552 | .I newtls |
dd6d3d2e | 553 | is interpreted as a |
2551f801 | 554 | .IR "struct user_desc\ *" |
35bf8cb4 | 555 | (see |
dd6d3d2e | 556 | .BR set_thread_area (2)). |
9ea5bc66 | 557 | On x86-64 it is the new value to be set for the %fs base register |
35bf8cb4 | 558 | (see the |
2551f801 | 559 | .B ARCH_SET_FS |
dd6d3d2e KF |
560 | argument to |
561 | .BR arch_prctl (2)). | |
562 | On architectures with a dedicated TLS register, it is the new value | |
563 | of that register. | |
f5dbc7c8 | 564 | .TP |
1603d6a1 | 565 | .BR CLONE_SIGHAND " (since Linux 2.0)" |
fea681da MK |
566 | If |
567 | .B CLONE_SIGHAND | |
314c8ff4 | 568 | is set, the calling process and the child process share the same table of |
c13182ef MK |
569 | signal handlers. |
570 | If the calling process or child process calls | |
fea681da | 571 | .BR sigaction (2) |
c13182ef MK |
572 | to change the behavior associated with a signal, the behavior is |
573 | changed in the other process as well. | |
574 | However, the calling process and child | |
fea681da | 575 | processes still have distinct signal masks and sets of pending |
c13182ef | 576 | signals. |
4ba17a6d | 577 | So, one of them may block or unblock signals using |
fea681da MK |
578 | .BR sigprocmask (2) |
579 | without affecting the other process. | |
efeece04 | 580 | .IP |
fea681da MK |
581 | If |
582 | .B CLONE_SIGHAND | |
583 | is not set, the child process inherits a copy of the signal handlers | |
584 | of the calling process at the time | |
edcc65ff | 585 | .BR clone () |
c13182ef MK |
586 | is called. |
587 | Calls to | |
fea681da MK |
588 | .BR sigaction (2) |
589 | performed later by one of the processes have no effect on the other | |
590 | process. | |
efeece04 | 591 | .IP |
d6bec36e MK |
592 | Since Linux 2.6.0, |
593 | .\" Precisely: Linux 2.6.0-test6 | |
29546c24 MK |
594 | .I flags |
595 | must also include | |
596 | .B CLONE_VM | |
597 | if | |
598 | .B CLONE_SIGHAND | |
599 | is specified | |
fea681da | 600 | .TP |
d6bec36e MK |
601 | .BR CLONE_STOPPED " (since Linux 2.6.0)" |
602 | .\" Precisely: Linux 2.6.0-test2 | |
a69b6bda MK |
603 | If |
604 | .B CLONE_STOPPED | |
605 | is set, then the child is initially stopped (as though it was sent a | |
606 | .B SIGSTOP | |
607 | signal), and must be resumed by sending it a | |
608 | .B SIGCONT | |
609 | signal. | |
efeece04 | 610 | .IP |
a60450a9 MK |
611 | This flag was |
612 | .I deprecated | |
613 | from Linux 2.6.25 onward, | |
614 | and was | |
615 | .I removed | |
28b44abc MK |
616 | altogether in Linux 2.6.38. |
617 | Since then, the kernel silently ignores it without error. | |
a5a061ee | 618 | .\" glibc 2.8 removed this defn from bits/sched.h |
c5af0674 MK |
619 | Starting with Linux 4.6, the same bit was reused for the |
620 | .BR CLONE_NEWCGROUP | |
621 | flag. | |
a69b6bda | 622 | .TP |
f5dbc7c8 | 623 | .BR CLONE_SYSVSEM " (since Linux 2.5.10)" |
fea681da | 624 | If |
f5dbc7c8 MK |
625 | .B CLONE_SYSVSEM |
626 | is set, then the child and the calling process share | |
5ada4b94 MK |
627 | a single list of System V semaphore adjustment |
628 | .RI ( semadj ) | |
629 | values (see | |
f5dbc7c8 | 630 | .BR semop (2)). |
5ada4b94 MK |
631 | In this case, the shared list accumulates |
632 | .I semadj | |
633 | values across all processes sharing the list, | |
634 | and semaphore adjustments are performed only when the last process | |
635 | that is sharing the list terminates (or ceases sharing the list using | |
636 | .BR unshare (2)). | |
f5d401dd | 637 | If this flag is not set, then the child has a separate |
5ada4b94 MK |
638 | .I semadj |
639 | list that is initially empty. | |
fea681da | 640 | .TP |
d6bec36e MK |
641 | .BR CLONE_THREAD " (since Linux 2.4.0)" |
642 | .\" Precisely: Linux 2.6.0-test8 | |
fea681da MK |
643 | If |
644 | .B CLONE_THREAD | |
645 | is set, the child is placed in the same thread group as the calling process. | |
fd8a5be4 MK |
646 | To make the remainder of the discussion of |
647 | .B CLONE_THREAD | |
648 | more readable, the term "thread" is used to refer to the | |
649 | processes within a thread group. | |
efeece04 | 650 | .IP |
fd8a5be4 MK |
651 | Thread groups were a feature added in Linux 2.4 to support the |
652 | POSIX threads notion of a set of threads that share a single PID. | |
653 | Internally, this shared PID is the so-called | |
654 | thread group identifier (TGID) for the thread group. | |
c13182ef | 655 | Since Linux 2.4, calls to |
fea681da | 656 | .BR getpid (2) |
fd8a5be4 | 657 | return the TGID of the caller. |
efeece04 | 658 | .IP |
fd8a5be4 MK |
659 | The threads within a group can be distinguished by their (system-wide) |
660 | unique thread IDs (TID). | |
661 | A new thread's TID is available as the function result | |
662 | returned to the caller of | |
663 | .BR clone (), | |
664 | and a thread can obtain | |
665 | its own TID using | |
666 | .BR gettid (2). | |
efeece04 | 667 | .IP |
c13182ef | 668 | When a call is made to |
fd8a5be4 MK |
669 | .BR clone () |
670 | without specifying | |
671 | .BR CLONE_THREAD , | |
672 | then the resulting thread is placed in a new thread group | |
673 | whose TGID is the same as the thread's TID. | |
674 | This thread is the | |
675 | .I leader | |
676 | of the new thread group. | |
efeece04 | 677 | .IP |
fd8a5be4 MK |
678 | A new thread created with |
679 | .B CLONE_THREAD | |
680 | has the same parent process as the caller of | |
681 | .BR clone () | |
c13182ef | 682 | (i.e., like |
fd8a5be4 MK |
683 | .BR CLONE_PARENT ), |
684 | so that calls to | |
685 | .BR getppid (2) | |
686 | return the same value for all of the threads in a thread group. | |
687 | When a | |
c13182ef | 688 | .B CLONE_THREAD |
fd8a5be4 MK |
689 | thread terminates, the thread that created it using |
690 | .BR clone () | |
691 | is not sent a | |
692 | .B SIGCHLD | |
693 | (or other termination) signal; | |
694 | nor can the status of such a thread be obtained | |
695 | using | |
696 | .BR wait (2). | |
697 | (The thread is said to be | |
698 | .IR detached .) | |
efeece04 | 699 | .IP |
e2fbf61d MK |
700 | After all of the threads in a thread group terminate |
701 | the parent process of the thread group is sent a | |
fd8a5be4 MK |
702 | .B SIGCHLD |
703 | (or other termination) signal. | |
efeece04 | 704 | .IP |
fd8a5be4 MK |
705 | If any of the threads in a thread group performs an |
706 | .BR execve (2), | |
707 | then all threads other than the thread group leader are terminated, | |
708 | and the new program is executed in the thread group leader. | |
efeece04 | 709 | .IP |
f7110f60 MK |
710 | If one of the threads in a thread group creates a child using |
711 | .BR fork (2), | |
712 | then any thread in the group can | |
713 | .BR wait (2) | |
714 | for that child. | |
efeece04 | 715 | .IP |
edcc65ff | 716 | Since Linux 2.5.35, |
fd8a5be4 MK |
717 | .I flags |
718 | must also include | |
719 | .B CLONE_SIGHAND | |
720 | if | |
721 | .B CLONE_THREAD | |
6fd69f33 | 722 | is specified |
d6bec36e MK |
723 | (and note that, since Linux 2.6.0, |
724 | .\" Precisely: Linux 2.6.0-test6 | |
6fd69f33 MK |
725 | .BR CLONE_SIGHAND |
726 | also requires | |
727 | .BR CLONE_VM | |
728 | to be included). | |
efeece04 | 729 | .IP |
e2fbf61d MK |
730 | Signal dispositions and actions are process-wide: |
731 | if an unhandled signal is delivered to a thread, then | |
732 | it will affect (terminate, stop, continue, be ignored in) | |
733 | all members of the thread group. | |
efeece04 | 734 | .IP |
99408a60 | 735 | Each thread has its own signal mask, as set by |
f957eebd MK |
736 | .BR sigprocmask (2). |
737 | .IP | |
738 | A signal may be process-directed or thread-directed. | |
739 | A process-directed signal is targeted at a thread group (i.e., a TGID), | |
740 | and is delivered to an arbitrarily selected thread from among those | |
741 | that are not blocking the signal. | |
9d32816b | 742 | A signal may be process directed because it was generated by the kernel |
f957eebd MK |
743 | for reasons other than a hardware exception, or because it was sent using |
744 | .BR kill (2) | |
745 | or | |
746 | .BR sigqueue (3). | |
747 | A thread-directed signal is targeted at (i.e., delivered to) | |
748 | a specific thread. | |
749 | A signal may be thread directed because it was sent using | |
750 | .BR tgkill (2) | |
751 | or | |
752 | .BR pthread_sigqueue (3), | |
753 | or because the thread executed a machine language instruction that triggered | |
754 | a hardware exception | |
755 | (e.g., invalid memory access triggering | |
756 | .BR SIGSEGV | |
757 | or a floating-point exception triggering | |
758 | .BR SIGFPE ). | |
759 | .IP | |
99408a60 MK |
760 | A call to |
761 | .BR sigpending (2) | |
f957eebd MK |
762 | returns a signal set that is the union of the pending process-directed |
763 | signals and the signals that are pending for the calling thread. | |
efeece04 | 764 | .IP |
475c2753 | 765 | If a process-directed signal is delivered to a thread group, |
e2fbf61d MK |
766 | and the thread group has installed a handler for the signal, then |
767 | the handler will be invoked in exactly one, arbitrarily selected | |
768 | member of the thread group that has not blocked the signal. | |
c13182ef | 769 | If multiple threads in a group are waiting to accept the same signal using |
e2fbf61d MK |
770 | .BR sigwaitinfo (2), |
771 | the kernel will arbitrarily select one of these threads | |
475c2753 | 772 | to receive the signal. |
a69b6bda | 773 | .TP |
f5dbc7c8 | 774 | .BR CLONE_UNTRACED " (since Linux 2.5.46)" |
a69b6bda | 775 | If |
f5dbc7c8 MK |
776 | .B CLONE_UNTRACED |
777 | is specified, then a tracing process cannot force | |
778 | .B CLONE_PTRACE | |
779 | on this child process. | |
fea681da | 780 | .TP |
1603d6a1 | 781 | .BR CLONE_VFORK " (since Linux 2.2)" |
f5dbc7c8 MK |
782 | If |
783 | .B CLONE_VFORK | |
784 | is set, the execution of the calling process is suspended | |
785 | until the child releases its virtual memory | |
786 | resources via a call to | |
787 | .BR execve (2) | |
788 | or | |
789 | .BR _exit (2) | |
790 | (as with | |
791 | .BR vfork (2)). | |
efeece04 | 792 | .IP |
f5dbc7c8 MK |
793 | If |
794 | .B CLONE_VFORK | |
4b4a853a | 795 | is not set, then both the calling process and the child are schedulable |
f5dbc7c8 MK |
796 | after the call, and an application should not rely on execution occurring |
797 | in any particular order. | |
fea681da | 798 | .TP |
1603d6a1 | 799 | .BR CLONE_VM " (since Linux 2.0)" |
f5dbc7c8 MK |
800 | If |
801 | .B CLONE_VM | |
802 | is set, the calling process and the child process run in the same memory | |
803 | space. | |
804 | In particular, memory writes performed by the calling process | |
805 | or by the child process are also visible in the other process. | |
806 | Moreover, any memory mapping or unmapping performed with | |
807 | .BR mmap (2) | |
808 | or | |
809 | .BR munmap (2) | |
810 | by the child or calling process also affects the other process. | |
efeece04 | 811 | .IP |
f5dbc7c8 MK |
812 | If |
813 | .B CLONE_VM | |
814 | is not set, the child process runs in a separate copy of the memory | |
815 | space of the calling process at the time of | |
816 | .BR clone (). | |
817 | Memory writes or file mappings/unmappings performed by one of the | |
818 | processes do not affect the other, as with | |
819 | .BR fork (2). | |
1874193e | 820 | .SH NOTES |
1c6ebc4b MK |
821 | Note that the glibc |
822 | .BR clone () | |
823 | wrapper function makes some changes | |
824 | in the memory pointed to by | |
825 | .I child_stack | |
826 | (changes required to set the stack up correctly for the child) | |
827 | .I before | |
828 | invoking the | |
829 | .BR clone () | |
830 | system call. | |
831 | So, in cases where | |
832 | .BR clone () | |
833 | is used to recursively create children, | |
834 | do not use the buffer employed for the parent's stack | |
835 | as the stack of the child. | |
836 | .\" | |
0722a578 | 837 | .SS C library/kernel differences |
e585064b MK |
838 | The raw |
839 | .BR clone () | |
fea681da MK |
840 | system call corresponds more closely to |
841 | .BR fork (2) | |
842 | in that execution in the child continues from the point of the | |
c13182ef | 843 | call. |
5add3af3 MK |
844 | As such, the |
845 | .I fn | |
c13182ef | 846 | and |
5add3af3 MK |
847 | .I arg |
848 | arguments of the | |
849 | .BR clone () | |
850 | wrapper function are omitted. | |
161fce30 | 851 | .PP |
b219e68c | 852 | Another difference for the raw |
d35f5c34 | 853 | .BR clone () |
b219e68c MK |
854 | system call is that the |
855 | .I child_stack | |
82f9cb98 | 856 | argument may be NULL, |
b219e68c MK |
857 | in which case the child uses a duplicate of the parent's stack. |
858 | (Copy-on-write semantics ensure that the child gets separate copies | |
859 | of stack pages when either process modifies the stack.) | |
860 | In this case, for correct operation, the | |
861 | .B CLONE_VM | |
862 | option should not be specified. | |
d35f5c34 MK |
863 | (If the child |
864 | .I shares | |
865 | the parent's memory because of the use of the | |
866 | .BR CLONE_VM | |
b219e68c MK |
867 | flag, |
868 | then no copy-on-write duplication occurs and chaos is likely to result.) | |
d35f5c34 | 869 | .PP |
161fce30 MK |
870 | The order of the arguments also differs in the raw system call, |
871 | and there are variations in the arguments across architectures, | |
872 | as detailed in the following paragraphs. | |
efeece04 | 873 | .PP |
2a15a76b | 874 | The raw system call interface on x86-64 and some other architectures |
8016cc90 | 875 | (including sh, tile, ia-64, and alpha) is: |
efeece04 | 876 | .PP |
5add3af3 | 877 | .in +4 |
b76974c1 | 878 | .EX |
2a15a76b MK |
879 | .BI "long clone(unsigned long " flags ", void *" child_stack , |
880 | .BI " int *" ptid ", int *" ctid , | |
881 | .BI " unsigned long " newtls ); | |
b76974c1 | 882 | .EE |
2a15a76b | 883 | .in |
efeece04 | 884 | .PP |
2a15a76b MK |
885 | On x86-32, and several other common architectures |
886 | (including score, ARM, ARM 64, PA-RISC, arc, Power PC, xtensa, | |
887 | and MIPS), | |
888 | .\" CONFIG_CLONE_BACKWARDS | |
889 | the order of the last two arguments is reversed: | |
efeece04 | 890 | .PP |
2a15a76b | 891 | .in +4 |
b76974c1 | 892 | .EX |
5add3af3 | 893 | .BI "long clone(unsigned long " flags ", void *" child_stack , |
2a15a76b MK |
894 | .BI " int *" ptid ", unsigned long " newtls , |
895 | .BI " int *" ctid ); | |
b76974c1 | 896 | .EE |
2a15a76b | 897 | .in |
efeece04 | 898 | .PP |
2a15a76b MK |
899 | On the cris and s390 architectures, |
900 | .\" CONFIG_CLONE_BACKWARDS2 | |
901 | the order of the first two arguments is reversed: | |
efeece04 | 902 | .PP |
2a15a76b | 903 | .in +4 |
b76974c1 | 904 | .EX |
2a15a76b | 905 | .BI "long clone(void *" child_stack ", unsigned long " flags , |
fda55470 | 906 | .BI " int *" ptid ", int *" ctid , |
dd6d3d2e | 907 | .BI " unsigned long " newtls ); |
b76974c1 | 908 | .EE |
2a15a76b | 909 | .in |
efeece04 | 910 | .PP |
2a15a76b MK |
911 | On the microblaze architecture, |
912 | .\" CONFIG_CLONE_BACKWARDS3 | |
913 | an additional argument is supplied: | |
efeece04 | 914 | .PP |
2a15a76b | 915 | .in +4 |
b76974c1 | 916 | .EX |
2a15a76b MK |
917 | .BI "long clone(unsigned long " flags ", void *" child_stack , |
918 | .BI " int " stack_size , "\fR /* Size of stack */" | |
919 | .BI " int *" ptid ", int *" ctid , | |
920 | .BI " unsigned long " newtls ); | |
b76974c1 | 921 | .EE |
5add3af3 | 922 | .in |
2a15a76b | 923 | .\" |
251113d0 | 924 | .SS blackfin, m68k, and sparc |
2a15a76b MK |
925 | .\" Mike Frysinger noted in a 2013 mail: |
926 | .\" these arches don't define __ARCH_WANT_SYS_CLONE: | |
927 | .\" blackfin ia64 m68k sparc | |
251113d0 | 928 | The argument-passing conventions on |
04346be5 | 929 | blackfin, m68k, and sparc are different from the descriptions above. |
251113d0 | 930 | For details, see the kernel (and glibc) source. |
574c92b6 | 931 | .SS ia64 |
097a1f3b | 932 | On ia64, a different interface is used: |
7a346077 | 933 | .PP |
8016cc90 ES |
934 | .in +4 |
935 | .EX | |
097a1f3b MK |
936 | .BI "int __clone2(int (*" "fn" ")(void *), " |
937 | .BI " void *" child_stack_base ", size_t " stack_size , | |
938 | .BI " int " flags ", void *" "arg" ", ... " | |
939 | .BI " /* pid_t *" ptid ", struct user_desc *" tls \ | |
940 | ", pid_t *" ctid " */ );" | |
8016cc90 ES |
941 | .EE |
942 | .in | |
097a1f3b | 943 | .PP |
9b9c895c MK |
944 | The prototype shown above is for the glibc wrapper function; |
945 | for the system call itself, | |
946 | the prototype can be described as follows (it is identical to the | |
8016cc90 ES |
947 | .BR clone () |
948 | prototype on microblaze): | |
949 | .PP | |
950 | .in +4 | |
951 | .EX | |
952 | .BI "long clone2(unsigned long " flags ", void *" child_stack_base , | |
953 | .BI " int " stack_size , "\fR /* Size of stack */" | |
954 | .BI " int *" ptid ", int *" ctid , | |
955 | .BI " unsigned long " tls ); | |
956 | .EE | |
957 | .in | |
097a1f3b MK |
958 | .PP |
959 | .BR __clone2 () | |
960 | operates in the same way as | |
961 | .BR clone (), | |
962 | except that | |
963 | .I child_stack_base | |
964 | points to the lowest address of the child's stack area, | |
965 | and | |
966 | .I stack_size | |
967 | specifies the size of the stack pointed to by | |
968 | .IR child_stack_base . | |
5add3af3 | 969 | .SS Linux 2.4 and earlier |
577f9b62 MK |
970 | In Linux 2.4 and earlier, |
971 | .BR clone () | |
972 | does not take arguments | |
973 | .IR ptid , | |
974 | .IR tls , | |
975 | and | |
130b2e49 | 976 | .IR ctid . |
47297adb | 977 | .SH RETURN VALUE |
0bfa087b MK |
978 | .\" gettid(2) returns current->pid; |
979 | .\" getpid(2) returns current->tgid; | |
fea681da | 980 | On success, the thread ID of the child process is returned |
c13182ef | 981 | in the caller's thread of execution. |
84811e86 | 982 | On failure, \-1 is returned |
fea681da MK |
983 | in the caller's context, no child process will be created, and |
984 | .I errno | |
985 | will be set appropriately. | |
fea681da MK |
986 | .SH ERRORS |
987 | .TP | |
988 | .B EAGAIN | |
e1b6e186 MK |
989 | Too many processes are already running; see |
990 | .BR fork (2). | |
fea681da MK |
991 | .TP |
992 | .B EINVAL | |
993 | .B CLONE_SIGHAND | |
994 | was specified, but | |
995 | .B CLONE_VM | |
2e8a7fb3 | 996 | was not. |
d6bec36e MK |
997 | (Since Linux 2.6.0.) |
998 | .\" Precisely: Linux 2.6.0-test6 | |
fea681da MK |
999 | .TP |
1000 | .B EINVAL | |
1001 | .B CLONE_THREAD | |
1002 | was specified, but | |
1003 | .B CLONE_SIGHAND | |
6387216b MK |
1004 | was not. |
1005 | (Since Linux 2.5.35.) | |
29546c24 MK |
1006 | .\" .TP |
1007 | .\" .B EINVAL | |
1008 | .\" Precisely one of | |
1009 | .\" .B CLONE_DETACHED | |
1010 | .\" and | |
1011 | .\" .B CLONE_THREAD | |
6387216b MK |
1012 | .\" was specified. |
1013 | .\" (Since Linux 2.6.0-test6.) | |
fea681da MK |
1014 | .TP |
1015 | .B EINVAL | |
d6868c69 JH |
1016 | .B CLONE_THREAD |
1017 | was specified, but the current process previously called | |
1018 | .BR unshare (2) | |
1019 | with the | |
1020 | .B CLONE_NEWPID | |
1021 | flag or used | |
1022 | .BR setns (2) | |
1023 | to reassociate itself with a PID namespace. | |
1024 | .TP | |
1025 | .B EINVAL | |
d34e5645 | 1026 | .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71 |
fea681da MK |
1027 | Both |
1028 | .B CLONE_FS | |
1029 | and | |
1030 | .B CLONE_NEWNS | |
1031 | were specified in | |
1032 | .IR flags . | |
1033 | .TP | |
d34e5645 MK |
1034 | .BR EINVAL " (since Linux 3.9)" |
1035 | Both | |
1036 | .B CLONE_NEWUSER | |
1037 | and | |
1038 | .B CLONE_FS | |
1039 | were specified in | |
1040 | .IR flags . | |
1041 | .TP | |
fea681da | 1042 | .B EINVAL |
82ee147a | 1043 | Both |
667417b3 MK |
1044 | .B CLONE_NEWIPC |
1045 | and | |
1046 | .B CLONE_SYSVSEM | |
1047 | were specified in | |
1048 | .IR flags . | |
1049 | .TP | |
1050 | .B EINVAL | |
f0007192 | 1051 | One (or both) of |
82ee147a | 1052 | .BR CLONE_NEWPID |
f0007192 MK |
1053 | or |
1054 | .BR CLONE_NEWUSER | |
1055 | and one (or both) of | |
82ee147a | 1056 | .BR CLONE_THREAD |
f0007192 MK |
1057 | or |
1058 | .BR CLONE_PARENT | |
82ee147a MK |
1059 | were specified in |
1060 | .IR flags . | |
1061 | .TP | |
1062 | .B EINVAL | |
d4748fad | 1063 | Returned by the glibc |
edcc65ff | 1064 | .BR clone () |
d4748fad MK |
1065 | wrapper function when |
1066 | .IR fn | |
1067 | or | |
1068 | .IR child_stack | |
1069 | is specified as NULL. | |
fea681da | 1070 | .TP |
28cad2c1 | 1071 | .B EINVAL |
667417b3 MK |
1072 | .BR CLONE_NEWIPC |
1073 | was specified in | |
1074 | .IR flags , | |
1075 | but the kernel was not configured with the | |
1076 | .B CONFIG_SYSVIPC | |
1077 | and | |
1078 | .BR CONFIG_IPC_NS | |
1079 | options. | |
1080 | .TP | |
1081 | .B EINVAL | |
163bf178 MK |
1082 | .BR CLONE_NEWNET |
1083 | was specified in | |
1084 | .IR flags , | |
1085 | but the kernel was not configured with the | |
1086 | .B CONFIG_NET_NS | |
1087 | option. | |
1088 | .TP | |
1089 | .B EINVAL | |
28cad2c1 MK |
1090 | .BR CLONE_NEWPID |
1091 | was specified in | |
1092 | .IR flags , | |
1093 | but the kernel was not configured with the | |
1094 | .B CONFIG_PID_NS | |
1095 | option. | |
1096 | .TP | |
43ce9dda | 1097 | .B EINVAL |
231d0bbe MK |
1098 | .BR CLONE_NEWUSER |
1099 | was specified in | |
1100 | .IR flags , | |
1101 | but the kernel was not configured with the | |
1102 | .B CONFIG_USER_NS | |
1103 | option. | |
1104 | .TP | |
1105 | .B EINVAL | |
43ce9dda MK |
1106 | .BR CLONE_NEWUTS |
1107 | was specified in | |
1108 | .IR flags , | |
1109 | but the kernel was not configured with the | |
832fe8ea | 1110 | .B CONFIG_UTS_NS |
43ce9dda MK |
1111 | option. |
1112 | .TP | |
c550a897 MK |
1113 | .B EINVAL |
1114 | .I child_stack | |
1115 | is not aligned to a suitable boundary for this architecture. | |
1116 | For example, on aarch64, | |
1117 | .I child_stack | |
1118 | must be a multiple of 16. | |
1119 | .TP | |
fea681da MK |
1120 | .B ENOMEM |
1121 | Cannot allocate sufficient memory to allocate a task structure for the | |
1122 | child, or to copy those parts of the caller's context that need to be | |
1123 | copied. | |
1124 | .TP | |
b20e22ae MK |
1125 | .BR ENOSPC " (since Linux 3.7)" |
1126 | .\" commit f2302505775fd13ba93f034206f1e2a587017929 | |
1127 | .B CLONE_NEWPID | |
1128 | was specified in flags, | |
1129 | but the limit on the nesting depth of PID namespaces | |
1130 | would have been exceeded; see | |
1131 | .BR pid_namespaces (7). | |
1132 | .TP | |
b5742ecc MK |
1133 | .BR ENOSPC " (since Linux 4.9; beforehand " EUSERS ) |
1134 | .B CLONE_NEWUSER | |
1135 | was specified in | |
1136 | .IR flags , | |
1137 | and the call would cause the limit on the number of | |
1138 | nested user namespaces to be exceeded. | |
1139 | See | |
1140 | .BR user_namespaces (7). | |
efeece04 | 1141 | .IP |
b5742ecc MK |
1142 | From Linux 3.11 to Linux 4.8, the error diagnosed in this case was |
1143 | .BR EUSERS . | |
1144 | .TP | |
2f7a331e MK |
1145 | .BR ENOSPC " (since Linux 4.9)" |
1146 | One of the values in | |
1147 | .I flags | |
1148 | specified the creation of a new user namespace, | |
1149 | but doing so would have caused the limit defined by the corresponding file in | |
1150 | .IR /proc/sys/user | |
1151 | to be exceeded. | |
1152 | For further details, see | |
1153 | .BR namespaces (7). | |
1154 | .TP | |
fea681da | 1155 | .B EPERM |
aa825b59 | 1156 | .BR CLONE_NEWCGROUP , |
667417b3 | 1157 | .BR CLONE_NEWIPC , |
163bf178 | 1158 | .BR CLONE_NEWNET , |
43ce9dda MK |
1159 | .BR CLONE_NEWNS , |
1160 | .BR CLONE_NEWPID , | |
82ee147a | 1161 | or |
43ce9dda | 1162 | .BR CLONE_NEWUTS |
00b08db3 | 1163 | was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP). |
fea681da MK |
1164 | .TP |
1165 | .B EPERM | |
1166 | .B CLONE_PID | |
1167 | was specified by a process other than process 0. | |
1c173eb3 | 1168 | (This error occurs only on Linux 2.5.15 and earlier.) |
365d292a MK |
1169 | .TP |
1170 | .B EPERM | |
1171 | .BR CLONE_NEWUSER | |
1172 | was specified in | |
1173 | .IR flags , | |
1174 | but either the effective user ID or the effective group ID of the caller | |
1175 | does not have a mapping in the parent namespace (see | |
f58fb24f | 1176 | .BR user_namespaces (7)). |
6fd119e7 | 1177 | .TP |
ac007938 MK |
1178 | .BR EPERM " (since Linux 3.9)" |
1179 | .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d | |
11a38815 AM |
1180 | .B CLONE_NEWUSER |
1181 | was specified in | |
ac007938 MK |
1182 | .I flags |
1183 | and the caller is in a chroot environment | |
1184 | .\" FIXME What is the rationale for this restriction? | |
1185 | (i.e., the caller's root directory does not match the root directory | |
1186 | of the mount namespace in which it resides). | |
1187 | .TP | |
6717ee86 MK |
1188 | .BR ERESTARTNOINTR " (since Linux 2.6.17)" |
1189 | .\" commit 4a2c7a7837da1b91468e50426066d988050e4d56 | |
1190 | System call was interrupted by a signal and will be restarted. | |
1191 | (This can be seen only during a trace.) | |
1192 | .TP | |
b5742ecc | 1193 | .BR EUSERS " (Linux 3.11 to Linux 4.8)" |
6fd119e7 MK |
1194 | .B CLONE_NEWUSER |
1195 | was specified in | |
1196 | .IR flags , | |
b5742ecc MK |
1197 | and the limit on the number of nested user namespaces would be exceeded. |
1198 | See the discussion of the | |
1199 | .BR ENOSPC | |
1200 | error above. | |
92b72224 MK |
1201 | .\" .SH VERSIONS |
1202 | .\" There is no entry for | |
1203 | .\" .BR clone () | |
1204 | .\" in libc5. | |
1205 | .\" glibc2 provides | |
1206 | .\" .BR clone () | |
1207 | .\" as described in this manual page. | |
47297adb | 1208 | .SH CONFORMING TO |
a1d5f77c | 1209 | .BR clone () |
e585064b | 1210 | is Linux-specific and should not be used in programs |
a1d5f77c | 1211 | intended to be portable. |
fea681da | 1212 | .SH NOTES |
79bdcc4a MK |
1213 | The |
1214 | .BR kcmp (2) | |
1215 | system call can be used to test whether two processes share various | |
49dba87f | 1216 | resources such as a file descriptor table, |
79bdcc4a | 1217 | System V semaphore undo operations, or a virtual address space. |
efeece04 MK |
1218 | .PP |
1219 | .PP | |
c471c363 MK |
1220 | Handlers registered using |
1221 | .BR pthread_atfork (3) | |
1222 | are not executed during a call to | |
1223 | .BR clone (). | |
efeece04 | 1224 | .PP |
ca8b1e32 | 1225 | In the Linux 2.4.x series, |
fd8a5be4 MK |
1226 | .B CLONE_THREAD |
1227 | generally does not make the parent of the new thread the same | |
1228 | as the parent of the calling process. | |
1229 | However, for kernel versions 2.4.7 to 2.4.18 the | |
1230 | .B CLONE_THREAD | |
1231 | flag implied the | |
c13182ef | 1232 | .B CLONE_PARENT |
ca8b1e32 | 1233 | flag (as in Linux 2.6.0 and later). |
efeece04 | 1234 | .PP |
c13182ef MK |
1235 | For a while there was |
1236 | .B CLONE_DETACHED | |
a5053dcb | 1237 | (introduced in 2.5.32): |
c13182ef | 1238 | parent wants no child-exit signal. |
4d543007 | 1239 | In Linux 2.6.2, the need to give this flag together with |
c13182ef | 1240 | .B CLONE_THREAD |
a5053dcb MK |
1241 | disappeared. |
1242 | This flag is still defined, but has no effect. | |
efeece04 | 1243 | .PP |
34ccb744 | 1244 | On i386, |
a5a997ca MK |
1245 | .BR clone () |
1246 | should not be called through vsyscall, but directly through | |
1247 | .IR "int $0x80" . | |
31830ef0 | 1248 | .SH BUGS |
abcf3b1d MK |
1249 | GNU C library versions 2.3.4 up to and including 2.24 |
1250 | contained a wrapper function for | |
0bfa087b | 1251 | .BR getpid (2) |
abcf3b1d MK |
1252 | that performed caching of PIDs. |
1253 | This caching relied on support in the glibc wrapper for | |
c60237c9 | 1254 | .BR clone (), |
abcf3b1d MK |
1255 | but limitations in the implementation |
1256 | meant that the cache was not up to date in some circumstances. | |
c60237c9 | 1257 | In particular, |
abcf3b1d | 1258 | if a signal was delivered to the child immediately after the |
c60237c9 MK |
1259 | .BR clone () |
1260 | call, then a call to | |
0b80cf56 | 1261 | .BR getpid (2) |
abcf3b1d | 1262 | in a handler for the signal could return the PID |
c60237c9 | 1263 | of the calling process ("the parent"), |
abcf3b1d | 1264 | if the clone wrapper had not yet had a chance to update the PID |
c60237c9 MK |
1265 | cache in the child. |
1266 | (This discussion ignores the case where the child was created using | |
9291ce36 | 1267 | .BR CLONE_THREAD , |
c60237c9 | 1268 | when |
0b80cf56 | 1269 | .BR getpid (2) |
c60237c9 MK |
1270 | .I should |
1271 | return the same value in the child and in the process that called | |
1272 | .BR clone (), | |
a1d48abb | 1273 | since the caller and the child are in the same thread group. |
e7d807b7 | 1274 | The stale-cache problem also does not occur if the |
a1d48abb JR |
1275 | .I flags |
1276 | argument includes | |
1277 | .BR CLONE_VM .) | |
abcf3b1d MK |
1278 | To get the truth, it was sometimes necessary to use code such as the following: |
1279 | .PP | |
47f743f1 MK |
1280 | .in +4n |
1281 | .EX | |
1282 | #include <syscall.h> | |
31830ef0 | 1283 | |
47f743f1 | 1284 | pid_t mypid; |
31830ef0 | 1285 | |
47f743f1 MK |
1286 | mypid = syscall(SYS_getpid); |
1287 | .EE | |
1288 | .in | |
c60237c9 MK |
1289 | .\" See also the following bug reports |
1290 | .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521 | |
1291 | .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910 | |
abcf3b1d MK |
1292 | .PP |
1293 | Because of the stale-cache problem, as well as other problems noted in | |
1294 | .BR getpid (2), | |
1295 | the PID caching feature was removed in glibc 2.25. | |
8c7b566c | 1296 | .SH EXAMPLE |
8c7b566c | 1297 | The following program demonstrates the use of |
9c13072a | 1298 | .BR clone () |
8c7b566c MK |
1299 | to create a child process that executes in a separate UTS namespace. |
1300 | The child changes the hostname in its UTS namespace. | |
1301 | Both parent and child then display the system hostname, | |
1302 | making it possible to see that the hostname | |
1303 | differs in the UTS namespaces of the parent and child. | |
1304 | For an example of the use of this program, see | |
1305 | .BR setns (2). | |
f30b7415 | 1306 | .SS Program source |
e7d0bb47 | 1307 | .EX |
8c7b566c MK |
1308 | #define _GNU_SOURCE |
1309 | #include <sys/wait.h> | |
1310 | #include <sys/utsname.h> | |
1311 | #include <sched.h> | |
1312 | #include <string.h> | |
1313 | #include <stdio.h> | |
1314 | #include <stdlib.h> | |
1315 | #include <unistd.h> | |
1316 | ||
d1a71985 | 1317 | #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e |
8c7b566c MK |
1318 | } while (0) |
1319 | ||
1320 | static int /* Start function for cloned child */ | |
1321 | childFunc(void *arg) | |
1322 | { | |
1323 | struct utsname uts; | |
1324 | ||
1325 | /* Change hostname in UTS namespace of child */ | |
1326 | ||
1327 | if (sethostname(arg, strlen(arg)) == \-1) | |
1328 | errExit("sethostname"); | |
1329 | ||
07d4e6ea | 1330 | /* Retrieve and display hostname */ |
8c7b566c MK |
1331 | |
1332 | if (uname(&uts) == \-1) | |
1333 | errExit("uname"); | |
d1a71985 | 1334 | printf("uts.nodename in child: %s\en", uts.nodename); |
8c7b566c MK |
1335 | |
1336 | /* Keep the namespace open for a while, by sleeping. | |
1337 | This allows some experimentation\-\-for example, another | |
1338 | process might join the namespace. */ | |
9f1b9726 | 1339 | |
8c7b566c MK |
1340 | sleep(200); |
1341 | ||
1342 | return 0; /* Child terminates now */ | |
1343 | } | |
1344 | ||
1345 | #define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */ | |
1346 | ||
1347 | int | |
1348 | main(int argc, char *argv[]) | |
1349 | { | |
1350 | char *stack; /* Start of stack buffer */ | |
1351 | char *stackTop; /* End of stack buffer */ | |
1352 | pid_t pid; | |
1353 | struct utsname uts; | |
1354 | ||
1355 | if (argc < 2) { | |
d1a71985 | 1356 | fprintf(stderr, "Usage: %s <child\-hostname>\en", argv[0]); |
8c7b566c MK |
1357 | exit(EXIT_SUCCESS); |
1358 | } | |
1359 | ||
1360 | /* Allocate stack for child */ | |
1361 | ||
1362 | stack = malloc(STACK_SIZE); | |
1363 | if (stack == NULL) | |
1364 | errExit("malloc"); | |
1365 | stackTop = stack + STACK_SIZE; /* Assume stack grows downward */ | |
1366 | ||
1367 | /* Create child that has its own UTS namespace; | |
1368 | child commences execution in childFunc() */ | |
1369 | ||
1370 | pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]); | |
1371 | if (pid == \-1) | |
1372 | errExit("clone"); | |
d1a71985 | 1373 | printf("clone() returned %ld\en", (long) pid); |
8c7b566c MK |
1374 | |
1375 | /* Parent falls through to here */ | |
1376 | ||
1377 | sleep(1); /* Give child time to change its hostname */ | |
1378 | ||
9f1b9726 | 1379 | /* Display hostname in parent\(aqs UTS namespace. This will be |
8c7b566c MK |
1380 | different from hostname in child\(aqs UTS namespace. */ |
1381 | ||
1382 | if (uname(&uts) == \-1) | |
1383 | errExit("uname"); | |
d1a71985 | 1384 | printf("uts.nodename in parent: %s\en", uts.nodename); |
8c7b566c MK |
1385 | |
1386 | if (waitpid(pid, NULL, 0) == \-1) /* Wait for child */ | |
1387 | errExit("waitpid"); | |
d1a71985 | 1388 | printf("child has terminated\en"); |
8c7b566c MK |
1389 | |
1390 | exit(EXIT_SUCCESS); | |
1391 | } | |
e7d0bb47 | 1392 | .EE |
47297adb | 1393 | .SH SEE ALSO |
fea681da | 1394 | .BR fork (2), |
2b44301c | 1395 | .BR futex (2), |
fea681da MK |
1396 | .BR getpid (2), |
1397 | .BR gettid (2), | |
6f8746e4 | 1398 | .BR kcmp (2), |
f2d0bbf1 | 1399 | .BR set_thread_area (2), |
2b44301c | 1400 | .BR set_tid_address (2), |
8403481f | 1401 | .BR setns (2), |
f2d0bbf1 | 1402 | .BR tkill (2), |
5cc01e9c | 1403 | .BR unshare (2), |
fea681da | 1404 | .BR wait (2), |
3616b7c0 | 1405 | .BR capabilities (7), |
41096af1 | 1406 | .BR namespaces (7), |
3616b7c0 | 1407 | .BR pthreads (7) |