]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Hey Emacs! This file is -*- nroff -*- source. |
2 | .\" | |
3 | .\" Copyright (c) 1992 Drew Eckhardt <drew@cs.colorado.edu>, March 28, 1992 | |
1130df60 | 4 | .\" and Copyright (c) Michael Kerrisk, 2001, 2002, 2005 |
fea681da MK |
5 | .\" May be distributed under the GNU General Public License. |
6 | .\" Modified by Michael Haardt <michael@moria.de> | |
7 | .\" Modified 24 Jul 1993 by Rik Faith <faith@cs.unc.edu> | |
8 | .\" Modified 21 Aug 1994 by Michael Chastain <mec@shell.portal.com>: | |
9 | .\" New man page (copied from 'fork.2'). | |
10 | .\" Modified 10 June 1995 by Andries Brouwer <aeb@cwi.nl> | |
11 | .\" Modified 25 April 1998 by Xavier Leroy <Xavier.Leroy@inria.fr> | |
12 | .\" Modified 26 Jun 2001 by Michael Kerrisk | |
13 | .\" Mostly upgraded to 2.4.x | |
14 | .\" Added prototype for sys_clone() plus description | |
15 | .\" Added CLONE_THREAD with a brief description of thread groups | |
c13182ef | 16 | .\" Added CLONE_PARENT and revised entire page remove ambiguity |
fea681da MK |
17 | .\" between "calling process" and "parent process" |
18 | .\" Added CLONE_PTRACE and CLONE_VFORK | |
19 | .\" Added EPERM and EINVAL error codes | |
fd8a5be4 | 20 | .\" Renamed "__clone" to "clone" (which is the prototype in <sched.h>) |
fea681da | 21 | .\" various other minor tidy ups and clarifications. |
c11b1abf | 22 | .\" Modified 26 Jun 2001 by Michael Kerrisk <mtk.manpages@gmail.com> |
d9bfdb9c | 23 | .\" Updated notes for 2.4.7+ behavior of CLONE_THREAD |
c11b1abf | 24 | .\" Modified 15 Oct 2002 by Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da MK |
25 | .\" Added description for CLONE_NEWNS, which was added in 2.4.19 |
26 | .\" Slightly rephrased, aeb. | |
27 | .\" Modified 1 Feb 2003 - added CLONE_SIGHAND restriction, aeb. | |
28 | .\" Modified 1 Jan 2004 - various updates, aeb | |
0967c11f | 29 | .\" Modified 2004-09-10 - added CLONE_PARENT_SETTID etc. - aeb. |
d9bfdb9c | 30 | .\" 2005-04-12, mtk, noted the PID caching behavior of NPTL's getpid() |
31830ef0 | 31 | .\" wrapper under BUGS. |
fd8a5be4 MK |
32 | .\" 2005-05-10, mtk, added CLONE_SYSVSEM, CLONE_UNTRACED, CLONE_STOPPED. |
33 | .\" 2005-05-17, mtk, Substantially enhanced discussion of CLONE_THREAD. | |
82ee147a MK |
34 | .\" 2008-11-18, mtk, order CLONE_* flags alphabetically |
35 | .\" 2008-11-18, mtk, document CLONE_NEWPID | |
43ce9dda | 36 | .\" 2008-11-19, mtk, document CLONE_NEWUTS |
667417b3 | 37 | .\" 2008-11-19, mtk, document CLONE_NEWIPC |
cfdc761b | 38 | .\" 2008-11-19, Jens Axboe, mtk, document CLONE_IO |
fea681da | 39 | .\" |
185341d4 MK |
40 | .\" FIXME Document CLONE_NEWUSER, which is new in 2.6.23 |
41 | .\" (also supported for unshare()?) | |
6807c79a | 42 | .\" FIXME . 2.6.25 marks the unused CLONE_STOPPED as obsolete, and it will |
21a0b03d | 43 | .\" probably be removed in the future. |
360ed6b3 | 44 | .\" |
bf032425 | 45 | .TH CLONE 2 2008-11-25 "Linux" "Linux Programmer's Manual" |
fea681da | 46 | .SH NAME |
9b0e0996 | 47 | clone, __clone2 \- create a child process |
fea681da | 48 | .SH SYNOPSIS |
c10859eb | 49 | .nf |
cc4615cc MK |
50 | .B #define _GNU_SOURCE |
51 | .\" Actually _BSD_SOURCE || _SVID_SOURCE | |
52 | .\" See http://sources.redhat.com/bugzilla/show_bug.cgi?id=4749 | |
fea681da | 53 | .B #include <sched.h> |
c10859eb | 54 | |
ff929e3b MK |
55 | .BI "int clone(int (*" "fn" ")(void *), void *" child_stack , |
56 | .BI " int " flags ", void *" "arg" ", ... " | |
57 | .BI " /* pid_t *" pid ", struct user_desc *" tls \ | |
58 | ", pid_t *" ctid " */ );" | |
c10859eb | 59 | .fi |
fea681da | 60 | .SH DESCRIPTION |
edcc65ff MK |
61 | .BR clone () |
62 | creates a new process, in a manner similar to | |
fea681da | 63 | .BR fork (2). |
735f354f | 64 | It is actually a library function layered on top of the underlying |
e511ffb6 | 65 | .BR clone () |
fea681da MK |
66 | system call, hereinafter referred to as |
67 | .BR sys_clone . | |
68 | A description of | |
0daa9e92 | 69 | .B sys_clone |
fea681da MK |
70 | is given towards the end of this page. |
71 | ||
72 | Unlike | |
73 | .BR fork (2), | |
c13182ef | 74 | these calls |
fea681da MK |
75 | allow the child process to share parts of its execution context with |
76 | the calling process, such as the memory space, the table of file | |
c13182ef MK |
77 | descriptors, and the table of signal handlers. |
78 | (Note that on this manual | |
79 | page, "calling process" normally corresponds to "parent process". | |
80 | But see the description of | |
81 | .B CLONE_PARENT | |
fea681da MK |
82 | below.) |
83 | ||
84 | The main use of | |
edcc65ff | 85 | .BR clone () |
fea681da MK |
86 | is to implement threads: multiple threads of control in a program that |
87 | run concurrently in a shared memory space. | |
88 | ||
89 | When the child process is created with | |
c13182ef | 90 | .BR clone (), |
fea681da MK |
91 | it executes the function |
92 | application | |
c13182ef | 93 | .IR fn ( arg ). |
fea681da | 94 | (This differs from |
c13182ef | 95 | .BR fork (2), |
fea681da | 96 | where execution continues in the child from the point |
c13182ef MK |
97 | of the |
98 | .BR fork (2) | |
fea681da MK |
99 | call.) |
100 | The | |
101 | .I fn | |
102 | argument is a pointer to a function that is called by the child | |
103 | process at the beginning of its execution. | |
104 | The | |
105 | .I arg | |
106 | argument is passed to the | |
107 | .I fn | |
108 | function. | |
109 | ||
c13182ef | 110 | When the |
fea681da | 111 | .IR fn ( arg ) |
c13182ef MK |
112 | function application returns, the child process terminates. |
113 | The integer returned by | |
fea681da | 114 | .I fn |
c13182ef MK |
115 | is the exit code for the child process. |
116 | The child process may also terminate explicitly by calling | |
fea681da MK |
117 | .BR exit (2) |
118 | or after receiving a fatal signal. | |
119 | ||
120 | The | |
121 | .I child_stack | |
c13182ef MK |
122 | argument specifies the location of the stack used by the child process. |
123 | Since the child and calling process may share memory, | |
fea681da | 124 | it is not possible for the child process to execute in the |
c13182ef MK |
125 | same stack as the calling process. |
126 | The calling process must therefore | |
fea681da MK |
127 | set up memory space for the child stack and pass a pointer to this |
128 | space to | |
edcc65ff | 129 | .BR clone (). |
fea681da MK |
130 | Stacks grow downwards on all processors that run Linux |
131 | (except the HP PA processors), so | |
132 | .I child_stack | |
133 | usually points to the topmost address of the memory space set up for | |
134 | the child stack. | |
135 | ||
136 | The low byte of | |
137 | .I flags | |
fd8a5be4 MK |
138 | contains the number of the |
139 | .I "termination signal" | |
140 | sent to the parent when the child dies. | |
141 | If this signal is specified as anything other than | |
fea681da MK |
142 | .BR SIGCHLD , |
143 | then the parent process must specify the | |
c13182ef MK |
144 | .B __WALL |
145 | or | |
fea681da | 146 | .B __WCLONE |
c13182ef MK |
147 | options when waiting for the child with |
148 | .BR wait (2). | |
fea681da MK |
149 | If no signal is specified, then the parent process is not signaled |
150 | when the child terminates. | |
151 | ||
152 | .I flags | |
fd8a5be4 MK |
153 | may also be bitwise-or'ed with zero or more of the following constants, |
154 | in order to specify what is shared between the calling process | |
fea681da | 155 | and the child process: |
fea681da | 156 | .TP |
f5dbc7c8 MK |
157 | .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)" |
158 | Erase child thread ID at location | |
159 | .I child_tidptr | |
160 | in child memory when the child exits, and do a wakeup on the futex | |
161 | at that address. | |
162 | The address involved may be changed by the | |
163 | .BR set_tid_address (2) | |
164 | system call. | |
165 | This is used by threading libraries. | |
166 | .TP | |
167 | .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)" | |
168 | Store child thread ID at location | |
169 | .I child_tidptr | |
170 | in child memory. | |
171 | .TP | |
172 | .B CLONE_FILES | |
fea681da | 173 | If |
f5dbc7c8 MK |
174 | .B CLONE_FILES |
175 | is set, the calling process and the child process share the same file | |
176 | descriptor table. | |
177 | Any file descriptor created by the calling process or by the child | |
178 | process is also valid in the other process. | |
179 | Similarly, if one of the processes closes a file descriptor, | |
180 | or changes its associated flags (using the | |
181 | .BR fcntl (2) | |
182 | .B F_SETFD | |
183 | operation), the other process is also affected. | |
fea681da MK |
184 | |
185 | If | |
f5dbc7c8 MK |
186 | .B CLONE_FILES |
187 | is not set, the child process inherits a copy of all file descriptors | |
188 | opened in the calling process at the time of | |
189 | .BR clone (). | |
190 | (The duplicated file descriptors in the child refer to the | |
191 | same open file descriptions (see | |
192 | .BR open (2)) | |
193 | as the corresponding file descriptors in the calling process.) | |
194 | Subsequent operations that open or close file descriptors, | |
195 | or change file descriptor flags, | |
196 | performed by either the calling | |
197 | process or the child process do not affect the other process. | |
fea681da MK |
198 | .TP |
199 | .B CLONE_FS | |
200 | If | |
201 | .B CLONE_FS | |
314c8ff4 | 202 | is set, the caller and the child process share the same file system |
c13182ef MK |
203 | information. |
204 | This includes the root of the file system, the current | |
205 | working directory, and the umask. | |
206 | Any call to | |
fea681da MK |
207 | .BR chroot (2), |
208 | .BR chdir (2), | |
209 | or | |
210 | .BR umask (2) | |
edcc65ff | 211 | performed by the calling process or the child process also affects the |
fea681da MK |
212 | other process. |
213 | ||
c13182ef | 214 | If |
fea681da MK |
215 | .B CLONE_FS |
216 | is not set, the child process works on a copy of the file system | |
217 | information of the calling process at the time of the | |
edcc65ff | 218 | .BR clone () |
fea681da MK |
219 | call. |
220 | Calls to | |
221 | .BR chroot (2), | |
222 | .BR chdir (2), | |
223 | .BR umask (2) | |
224 | performed later by one of the processes do not affect the other process. | |
fea681da | 225 | .TP |
a4cc375e | 226 | .BR CLONE_IO " (since Linux 2.6.25)" |
11f27a1c JA |
227 | If |
228 | .B CLONE_IO | |
229 | is set, then the new process shares an I/O context with | |
230 | the calling process. | |
231 | If this flag is not set, then (as with | |
232 | .BR fork (2)) | |
233 | the new process has its own I/O context. | |
234 | ||
235 | .\" The following based on text from Jens Axboe | |
236 | The I/O context is the I/O scope of the disk scheduler (i.e, | |
237 | what the I/O scheduler uses to model scheduling of a process's I/O). | |
238 | If processes share the same I/O context, | |
239 | they are treated as one by the I/O scheduler. | |
240 | As a consequence, they get to share disk time. | |
241 | For some I/O schedulers, | |
242 | .\" the anticipatory and CFQ scheduler | |
243 | if two processes share an I/O context, | |
244 | they will be allowed to interleave their disk access. | |
245 | If several threads are doing I/O on behalf of the same process | |
246 | .RB ( aio_read (3), | |
247 | for instance), they should employ | |
248 | .BR CLONE_IO | |
249 | to get better I/O performance. | |
250 | .\" with CFQ and AS. | |
251 | ||
252 | If the kernel is not configured with the | |
253 | .B CONFIG_BLOCK | |
254 | option, this flag is a no-op. | |
255 | .TP | |
667417b3 MK |
256 | .BR CLONE_NEWIPC " (since Linux 2.4.19)" |
257 | If | |
258 | .B CLONE_NEWIPC | |
259 | is set, then create the process in a new IPC namespace. | |
260 | If this flag is not set, then (as with | |
261 | .BR fork (2)), | |
262 | the process is created in the same IPC namespace as | |
263 | the calling process. | |
0236bea9 | 264 | This flag is intended for the implementation of containers. |
667417b3 MK |
265 | |
266 | An IPC namespace consists of the set of identifiers for | |
267 | System V IPC objects. | |
268 | (These objects are created using | |
269 | .BR msgctl (2), | |
270 | .BR semctl (2), | |
271 | and | |
272 | .BR shmctl (2)). | |
273 | Objects created in an IPC namespace are visible to other processes | |
274 | that are members of that namespace, | |
275 | but are not visible to processes in other IPC namespaces. | |
276 | ||
83c1f4b5 MK |
277 | When an IPC namespace is destroyed |
278 | (i.e, when the last process that is a member of the namespace terminates), | |
279 | all IPC objects in the namespace are automatically destroyed. | |
280 | ||
667417b3 MK |
281 | Use of this flag requires: a kernel configured with the |
282 | .B CONFIG_SYSVIPC | |
283 | and | |
284 | .B CONFIG_IPC_NS | |
c8e18bd1 | 285 | options and that the process be privileged |
667417b3 MK |
286 | .RB ( CAP_SYS_ADMIN ). |
287 | This flag can't be specified in conjunction with | |
288 | .BR CLONE_SYSVSEM . | |
289 | .TP | |
163bf178 MK |
290 | .BR CLONE_NEWNET " (since Linux 2.6.24)" |
291 | (The implementation of this flag is not yet complete, | |
292 | but probably will be mostly complete by about Linux 2.6.28.) | |
293 | ||
294 | If | |
295 | .B CLONE_NEWNET | |
296 | is set, then create the process in a new network namespace. | |
297 | If this flag is not set, then (as with | |
298 | .BR fork (2)), | |
299 | the process is created in the same network namespace as | |
300 | the calling process. | |
301 | This flag is intended for the implementation of containers. | |
302 | ||
303 | A network namespace provides an isolated view of the networking stack | |
304 | (network device interfaces, IPv4 and IPv6 protocol stacks, | |
305 | IP routing tables, firewall rules, the | |
306 | .I /proc/net | |
307 | and | |
308 | .I /sys/class/net | |
309 | directory trees, sockets, etc.). | |
310 | A physical network device can live in exactly one | |
311 | network namespace. | |
312 | A virtual network device ("veth") pair provides a pipe-like abstraction | |
313 | that can be used to create tunnels between network namespaces, | |
314 | and can be used to create a bridge to a physical network device | |
315 | in another namespace. | |
316 | ||
bf032425 SH |
317 | When a network namespace is freed |
318 | (i.e., when the last process in the namespace terminates), | |
319 | its physical network devices are moved back to the | |
320 | initial network namespace (not to the parent of the process). | |
321 | ||
163bf178 MK |
322 | Use of this flag requires: a kernel configured with the |
323 | .B CONFIG_NET_NS | |
324 | option and that the process be privileged | |
cae2ec15 | 325 | .RB ( CAP_SYS_ADMIN ). |
163bf178 | 326 | .TP |
c10859eb | 327 | .BR CLONE_NEWNS " (since Linux 2.4.19)" |
732e54dd | 328 | Start the child in a new mount namespace. |
fea681da | 329 | |
732e54dd | 330 | Every process lives in a mount namespace. |
c13182ef | 331 | The |
fea681da MK |
332 | .I namespace |
333 | of a process is the data (the set of mounts) describing the file hierarchy | |
c13182ef MK |
334 | as seen by that process. |
335 | After a | |
fea681da MK |
336 | .BR fork (2) |
337 | or | |
2777b1ca | 338 | .BR clone () |
fea681da MK |
339 | where the |
340 | .B CLONE_NEWNS | |
732e54dd | 341 | flag is not set, the child lives in the same mount |
4df2eb09 | 342 | namespace as the parent. |
fea681da MK |
343 | The system calls |
344 | .BR mount (2) | |
345 | and | |
346 | .BR umount (2) | |
732e54dd | 347 | change the mount namespace of the calling process, and hence affect |
fea681da | 348 | all processes that live in the same namespace, but do not affect |
732e54dd | 349 | processes in a different mount namespace. |
fea681da MK |
350 | |
351 | After a | |
2777b1ca | 352 | .BR clone () |
fea681da MK |
353 | where the |
354 | .B CLONE_NEWNS | |
732e54dd | 355 | flag is set, the cloned child is started in a new mount namespace, |
fea681da MK |
356 | initialized with a copy of the namespace of the parent. |
357 | ||
0b9bdf82 | 358 | Only a privileged process (one having the \fBCAP_SYS_ADMIN\fP capability) |
fea681da MK |
359 | may specify the |
360 | .B CLONE_NEWNS | |
361 | flag. | |
362 | It is not permitted to specify both | |
363 | .B CLONE_NEWNS | |
364 | and | |
365 | .B CLONE_FS | |
366 | in the same | |
e511ffb6 | 367 | .BR clone () |
fea681da | 368 | call. |
fea681da | 369 | .TP |
82ee147a MK |
370 | .BR CLONE_NEWPID " (since Linux 2.6.24)" |
371 | .\" This explanation draws a lot of details from | |
372 | .\" http://lwn.net/Articles/259217/ | |
373 | .\" Authors: Pavel Emelyanov <xemul@openvz.org> | |
374 | .\" and Kir Kolyshkin <kir@openvz.org> | |
375 | .\" | |
376 | .\" The primary kernel commit is 30e49c263e36341b60b735cbef5ca37912549264 | |
377 | .\" Author: Pavel Emelyanov <xemul@openvz.org> | |
378 | If | |
5c95e5e8 | 379 | .B CLONE_NEWPID |
82ee147a MK |
380 | is set, then create the process in a new PID namespace. |
381 | If this flag is not set, then (as with | |
382 | .BR fork (2)), | |
383 | the process is created in the same PID namespace as | |
384 | the calling process. | |
0236bea9 | 385 | This flag is intended for the implementation of containers. |
82ee147a MK |
386 | |
387 | A PID namespace provides an isolated environment for PIDs: | |
388 | PIDs in a new namespace start at 1, | |
389 | somewhat like a standalone system, and calls to | |
390 | .BR fork (2), | |
391 | .BR vfork (2), | |
392 | or | |
393 | .BR clone (2) | |
5584229c | 394 | will produce processes with PIDs that are unique within the namespace. |
82ee147a MK |
395 | |
396 | The first process created in a new namespace | |
397 | (i.e., the process created using the | |
398 | .BR CLONE_NEWPID | |
399 | flag) has the PID 1, and is the "init" process for the namespace. | |
400 | Children that are orphaned within the namespace will be reparented | |
401 | to this process rather than | |
402 | .BR init (8). | |
403 | Unlike the traditional | |
404 | .B init | |
405 | process, the "init" process of a PID namespace can terminate, | |
406 | and if it does, all of the processes in the namespace are terminated. | |
407 | ||
408 | PID namespaces form a hierarchy. | |
409 | When a PID new namespace is created, | |
9cb9dcc9 | 410 | the processes in that namespace are visible |
82ee147a MK |
411 | in the PID namespace of the process that created the new namespace; |
412 | analogously, if the parent PID namespace is itself | |
413 | the child of another PID namespace, | |
9cb9dcc9 | 414 | then processes in the child and parent PID namespaces will both be |
82ee147a MK |
415 | visible in the grandparent PID namespace. |
416 | Conversely, the processes in the "child" PID namespace do not see | |
5584229c | 417 | the processes in the parent namespace. |
82ee147a MK |
418 | The existence of a namespace hierarchy means that each process |
419 | may now have multiple PIDs: | |
5584229c MK |
420 | one for each namespace in which it is visible; |
421 | each of these PIDs is unique within the corresponding namespace. | |
82ee147a MK |
422 | (A call to |
423 | .BR getpid (2) | |
424 | always returns the PID associated with the namespace in which | |
489f4278 | 425 | the process lives.) |
82ee147a MK |
426 | |
427 | After creating the new namespace, | |
428 | it is useful for the child to change its root directory | |
429 | and mount a new procfs instance at | |
430 | .I /proc | |
431 | so that tools such as | |
432 | .BR ps (1) | |
433 | work correctly. | |
434 | .\" mount -t proc proc /proc | |
9cb9dcc9 MK |
435 | (If |
436 | .BR CLONE_NEWNS | |
437 | is also included in | |
438 | .IR flags , | |
439 | then it isn't necessary to change the root directory: | |
868d9bf5 | 440 | a new procfs instance can be mounted directly over |
9cb9dcc9 | 441 | .IR /proc .) |
82ee147a MK |
442 | |
443 | Use of this flag requires: a kernel configured with the | |
444 | .B CONFIG_PID_NS | |
c8e18bd1 | 445 | option and that the process be privileged |
af3c728e | 446 | .RB ( CAP_SYS_ADMIN ). |
82ee147a MK |
447 | This flag can't be specified in conjunction with |
448 | .BR CLONE_THREAD . | |
449 | .TP | |
43ce9dda MK |
450 | .BR CLONE_NEWUTS " (since Linux 2.6.19)" |
451 | If | |
452 | .B CLONE_NEWUTS | |
e1b11906 MK |
453 | is set, then create the process in a new UTS namespace, |
454 | whose identifiers are initialized by duplicating the identifiers | |
455 | from the UTS namespace of the calling process. | |
43ce9dda MK |
456 | If this flag is not set, then (as with |
457 | .BR fork (2)), | |
458 | the process is created in the same UTS namespace as | |
459 | the calling process. | |
0236bea9 | 460 | This flag is intended for the implementation of containers. |
43ce9dda MK |
461 | |
462 | A UTS namespace is the set of identifiers returned by | |
463 | .BR uname (2); | |
464 | among these, the domain name and the host name can be modified by | |
465 | .BR setdomainname (2) | |
466 | and | |
467 | .BR | |
468 | .BR sethostname (2), | |
469 | respectively. | |
470 | Changes made to these identifiers in one UTS namespace | |
471 | are visible to other processes in the same namespace, | |
472 | but are not visible to processes in other UTS namespaces. | |
473 | ||
474 | Use of this flag requires: a kernel configured with the | |
475 | .B CONFIG_UTS_NS | |
c8e18bd1 | 476 | option and that the process be privileged |
43ce9dda MK |
477 | .RB ( CAP_SYS_ADMIN ). |
478 | .TP | |
f5dbc7c8 MK |
479 | .BR CLONE_PARENT " (since Linux 2.3.12)" |
480 | If | |
481 | .B CLONE_PARENT | |
482 | is set, then the parent of the new child (as returned by | |
483 | .BR getppid (2)) | |
484 | will be the same as that of the calling process. | |
485 | ||
486 | If | |
487 | .B CLONE_PARENT | |
488 | is not set, then (as with | |
489 | .BR fork (2)) | |
490 | the child's parent is the calling process. | |
491 | ||
492 | Note that it is the parent process, as returned by | |
493 | .BR getppid (2), | |
494 | which is signaled when the child terminates, so that | |
495 | if | |
496 | .B CLONE_PARENT | |
497 | is set, then the parent of the calling process, rather than the | |
498 | calling process itself, will be signaled. | |
499 | .TP | |
500 | .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)" | |
501 | Store child thread ID at location | |
502 | .I parent_tidptr | |
503 | in parent and child memory. | |
504 | (In Linux 2.5.32-2.5.48 there was a flag | |
505 | .B CLONE_SETTID | |
506 | that did this.) | |
507 | .TP | |
508 | .BR CLONE_PID " (obsolete)" | |
509 | If | |
510 | .B CLONE_PID | |
511 | is set, the child process is created with the same process ID as | |
512 | the calling process. | |
513 | This is good for hacking the system, but otherwise | |
514 | of not much use. | |
515 | Since 2.3.21 this flag can be | |
516 | specified only by the system boot process (PID 0). | |
517 | It disappeared in Linux 2.5.16. | |
518 | .TP | |
519 | .B CLONE_PTRACE | |
520 | If | |
521 | .B CLONE_PTRACE | |
522 | is specified, and the calling process is being traced, | |
523 | then trace the child also (see | |
524 | .BR ptrace (2)). | |
525 | .TP | |
526 | .BR CLONE_SETTLS " (since Linux 2.5.32)" | |
527 | The | |
528 | .I newtls | |
529 | argument is the new TLS (Thread Local Storage) descriptor. | |
530 | (See | |
531 | .BR set_thread_area (2).) | |
532 | .TP | |
fea681da MK |
533 | .B CLONE_SIGHAND |
534 | If | |
535 | .B CLONE_SIGHAND | |
314c8ff4 | 536 | is set, the calling process and the child process share the same table of |
c13182ef MK |
537 | signal handlers. |
538 | If the calling process or child process calls | |
fea681da | 539 | .BR sigaction (2) |
c13182ef MK |
540 | to change the behavior associated with a signal, the behavior is |
541 | changed in the other process as well. | |
542 | However, the calling process and child | |
fea681da | 543 | processes still have distinct signal masks and sets of pending |
c13182ef MK |
544 | signals. |
545 | So, one of them may block or unblock some signals using | |
fea681da MK |
546 | .BR sigprocmask (2) |
547 | without affecting the other process. | |
548 | ||
549 | If | |
550 | .B CLONE_SIGHAND | |
551 | is not set, the child process inherits a copy of the signal handlers | |
552 | of the calling process at the time | |
edcc65ff | 553 | .BR clone () |
c13182ef MK |
554 | is called. |
555 | Calls to | |
fea681da MK |
556 | .BR sigaction (2) |
557 | performed later by one of the processes have no effect on the other | |
558 | process. | |
29546c24 MK |
559 | |
560 | Since Linux 2.6.0-test6, | |
561 | .I flags | |
562 | must also include | |
563 | .B CLONE_VM | |
564 | if | |
565 | .B CLONE_SIGHAND | |
566 | is specified | |
fea681da | 567 | .TP |
a69b6bda MK |
568 | .BR CLONE_STOPPED " (since Linux 2.6.0-test2)" |
569 | If | |
570 | .B CLONE_STOPPED | |
571 | is set, then the child is initially stopped (as though it was sent a | |
572 | .B SIGSTOP | |
573 | signal), and must be resumed by sending it a | |
574 | .B SIGCONT | |
575 | signal. | |
ef37eaf2 MK |
576 | |
577 | .I "From Linux 2.6.25 this flag is deprecated." | |
578 | You probably never wanted to use it, | |
53c94269 | 579 | you certainly shouldn't be using it, and soon it will go away. |
a5a061ee | 580 | .\" glibc 2.8 removed this defn from bits/sched.h |
a69b6bda | 581 | .TP |
f5dbc7c8 | 582 | .BR CLONE_SYSVSEM " (since Linux 2.5.10)" |
fea681da | 583 | If |
f5dbc7c8 MK |
584 | .B CLONE_SYSVSEM |
585 | is set, then the child and the calling process share | |
586 | a single list of System V semaphore undo values (see | |
587 | .BR semop (2)). | |
588 | If this flag is not set, then the child has a separate undo list, | |
589 | which is initially empty. | |
fea681da MK |
590 | .TP |
591 | .BR CLONE_THREAD " (since Linux 2.4.0-test8)" | |
592 | If | |
593 | .B CLONE_THREAD | |
594 | is set, the child is placed in the same thread group as the calling process. | |
fd8a5be4 MK |
595 | To make the remainder of the discussion of |
596 | .B CLONE_THREAD | |
597 | more readable, the term "thread" is used to refer to the | |
598 | processes within a thread group. | |
fea681da | 599 | |
fd8a5be4 MK |
600 | Thread groups were a feature added in Linux 2.4 to support the |
601 | POSIX threads notion of a set of threads that share a single PID. | |
602 | Internally, this shared PID is the so-called | |
603 | thread group identifier (TGID) for the thread group. | |
c13182ef | 604 | Since Linux 2.4, calls to |
fea681da | 605 | .BR getpid (2) |
fd8a5be4 MK |
606 | return the TGID of the caller. |
607 | ||
608 | The threads within a group can be distinguished by their (system-wide) | |
609 | unique thread IDs (TID). | |
610 | A new thread's TID is available as the function result | |
611 | returned to the caller of | |
612 | .BR clone (), | |
613 | and a thread can obtain | |
614 | its own TID using | |
615 | .BR gettid (2). | |
616 | ||
c13182ef | 617 | When a call is made to |
fd8a5be4 MK |
618 | .BR clone () |
619 | without specifying | |
620 | .BR CLONE_THREAD , | |
621 | then the resulting thread is placed in a new thread group | |
622 | whose TGID is the same as the thread's TID. | |
623 | This thread is the | |
624 | .I leader | |
625 | of the new thread group. | |
626 | ||
627 | A new thread created with | |
628 | .B CLONE_THREAD | |
629 | has the same parent process as the caller of | |
630 | .BR clone () | |
c13182ef | 631 | (i.e., like |
fd8a5be4 MK |
632 | .BR CLONE_PARENT ), |
633 | so that calls to | |
634 | .BR getppid (2) | |
635 | return the same value for all of the threads in a thread group. | |
636 | When a | |
c13182ef | 637 | .B CLONE_THREAD |
fd8a5be4 MK |
638 | thread terminates, the thread that created it using |
639 | .BR clone () | |
640 | is not sent a | |
641 | .B SIGCHLD | |
642 | (or other termination) signal; | |
643 | nor can the status of such a thread be obtained | |
644 | using | |
645 | .BR wait (2). | |
646 | (The thread is said to be | |
647 | .IR detached .) | |
648 | ||
e2fbf61d MK |
649 | After all of the threads in a thread group terminate |
650 | the parent process of the thread group is sent a | |
fd8a5be4 MK |
651 | .B SIGCHLD |
652 | (or other termination) signal. | |
653 | ||
654 | If any of the threads in a thread group performs an | |
655 | .BR execve (2), | |
656 | then all threads other than the thread group leader are terminated, | |
657 | and the new program is executed in the thread group leader. | |
658 | ||
f7110f60 MK |
659 | If one of the threads in a thread group creates a child using |
660 | .BR fork (2), | |
661 | then any thread in the group can | |
662 | .BR wait (2) | |
663 | for that child. | |
664 | ||
edcc65ff | 665 | Since Linux 2.5.35, |
fd8a5be4 MK |
666 | .I flags |
667 | must also include | |
668 | .B CLONE_SIGHAND | |
669 | if | |
670 | .B CLONE_THREAD | |
671 | is specified. | |
e2fbf61d MK |
672 | |
673 | Signals may be sent to a thread group as a whole (i.e., a TGID) using | |
674 | .BR kill (2), | |
675 | or to a specific thread (i.e., TID) using | |
676 | .BR tgkill (2). | |
677 | ||
678 | Signal dispositions and actions are process-wide: | |
679 | if an unhandled signal is delivered to a thread, then | |
680 | it will affect (terminate, stop, continue, be ignored in) | |
681 | all members of the thread group. | |
682 | ||
99408a60 | 683 | Each thread has its own signal mask, as set by |
e2fbf61d | 684 | .BR sigprocmask (2), |
82a06020 | 685 | but signals can be pending either: for the whole process |
e2fbf61d MK |
686 | (i.e., deliverable to any member of the thread group), |
687 | when sent with | |
82a06020 | 688 | .BR kill (2); |
e2fbf61d MK |
689 | or for an individual thread, when sent with |
690 | .BR tgkill (2). | |
99408a60 MK |
691 | A call to |
692 | .BR sigpending (2) | |
693 | returns a signal set that is the union of the signals pending for the | |
694 | whole process and the signals that are pending for the calling thread. | |
e2fbf61d | 695 | |
c13182ef | 696 | If |
e2fbf61d MK |
697 | .BR kill (2) |
698 | is used to send a signal to a thread group, | |
699 | and the thread group has installed a handler for the signal, then | |
700 | the handler will be invoked in exactly one, arbitrarily selected | |
701 | member of the thread group that has not blocked the signal. | |
c13182ef | 702 | If multiple threads in a group are waiting to accept the same signal using |
e2fbf61d MK |
703 | .BR sigwaitinfo (2), |
704 | the kernel will arbitrarily select one of these threads | |
c13182ef | 705 | to receive a signal sent using |
e2fbf61d | 706 | .BR kill (2). |
a69b6bda | 707 | .TP |
f5dbc7c8 | 708 | .BR CLONE_UNTRACED " (since Linux 2.5.46)" |
a69b6bda | 709 | If |
f5dbc7c8 MK |
710 | .B CLONE_UNTRACED |
711 | is specified, then a tracing process cannot force | |
712 | .B CLONE_PTRACE | |
713 | on this child process. | |
fea681da | 714 | .TP |
f5dbc7c8 MK |
715 | .B CLONE_VFORK |
716 | If | |
717 | .B CLONE_VFORK | |
718 | is set, the execution of the calling process is suspended | |
719 | until the child releases its virtual memory | |
720 | resources via a call to | |
721 | .BR execve (2) | |
722 | or | |
723 | .BR _exit (2) | |
724 | (as with | |
725 | .BR vfork (2)). | |
726 | ||
727 | If | |
728 | .B CLONE_VFORK | |
729 | is not set then both the calling process and the child are schedulable | |
730 | after the call, and an application should not rely on execution occurring | |
731 | in any particular order. | |
fea681da | 732 | .TP |
f5dbc7c8 MK |
733 | .B CLONE_VM |
734 | If | |
735 | .B CLONE_VM | |
736 | is set, the calling process and the child process run in the same memory | |
737 | space. | |
738 | In particular, memory writes performed by the calling process | |
739 | or by the child process are also visible in the other process. | |
740 | Moreover, any memory mapping or unmapping performed with | |
741 | .BR mmap (2) | |
742 | or | |
743 | .BR munmap (2) | |
744 | by the child or calling process also affects the other process. | |
745 | ||
746 | If | |
747 | .B CLONE_VM | |
748 | is not set, the child process runs in a separate copy of the memory | |
749 | space of the calling process at the time of | |
750 | .BR clone (). | |
751 | Memory writes or file mappings/unmappings performed by one of the | |
752 | processes do not affect the other, as with | |
753 | .BR fork (2). | |
fea681da MK |
754 | .SS "sys_clone" |
755 | The | |
756 | .B sys_clone | |
757 | system call corresponds more closely to | |
758 | .BR fork (2) | |
759 | in that execution in the child continues from the point of the | |
c13182ef MK |
760 | call. |
761 | Thus, | |
fea681da MK |
762 | .B sys_clone |
763 | only requires the | |
764 | .I flags | |
c13182ef | 765 | and |
fea681da | 766 | .I child_stack |
c13182ef MK |
767 | arguments, which have the same meaning as for |
768 | .BR clone (). | |
fea681da | 769 | (Note that the order of these arguments differs from |
c13182ef | 770 | .BR clone ().) |
fea681da | 771 | |
c13182ef | 772 | Another difference for |
fea681da MK |
773 | .B sys_clone |
774 | is that the | |
775 | .I child_stack | |
c13182ef | 776 | argument may be zero, in which case copy-on-write semantics ensure that the |
fea681da | 777 | child gets separate copies of stack pages when either process modifies |
c13182ef MK |
778 | the stack. |
779 | In this case, for correct operation, the | |
fea681da MK |
780 | .B CLONE_VM |
781 | option should not be specified. | |
782 | ||
c4bb193f MK |
783 | Since Linux 2.5.49 the system call has five arguments. |
784 | The two new arguments are | |
fea681da MK |
785 | .I parent_tidptr |
786 | which points to the location (in parent and child memory) where | |
682edefb MK |
787 | the child thread ID will be written in case |
788 | .B CLONE_PARENT_SETTID | |
fea681da MK |
789 | was specified, and |
790 | .I child_tidptr | |
791 | which points to the location (in child memory) where the child thread ID | |
682edefb MK |
792 | will be written in case |
793 | .B CLONE_CHILD_SETTID | |
794 | was specified. | |
fea681da | 795 | .SH "RETURN VALUE" |
0bfa087b MK |
796 | .\" gettid(2) returns current->pid; |
797 | .\" getpid(2) returns current->tgid; | |
fea681da | 798 | On success, the thread ID of the child process is returned |
c13182ef | 799 | in the caller's thread of execution. |
84811e86 | 800 | On failure, \-1 is returned |
fea681da MK |
801 | in the caller's context, no child process will be created, and |
802 | .I errno | |
803 | will be set appropriately. | |
fea681da MK |
804 | .SH ERRORS |
805 | .TP | |
806 | .B EAGAIN | |
807 | Too many processes are already running. | |
808 | .TP | |
809 | .B EINVAL | |
810 | .B CLONE_SIGHAND | |
811 | was specified, but | |
812 | .B CLONE_VM | |
2e8a7fb3 MK |
813 | was not. |
814 | (Since Linux 2.6.0-test6.) | |
fea681da MK |
815 | .TP |
816 | .B EINVAL | |
817 | .B CLONE_THREAD | |
818 | was specified, but | |
819 | .B CLONE_SIGHAND | |
6387216b MK |
820 | was not. |
821 | (Since Linux 2.5.35.) | |
29546c24 MK |
822 | .\" .TP |
823 | .\" .B EINVAL | |
824 | .\" Precisely one of | |
825 | .\" .B CLONE_DETACHED | |
826 | .\" and | |
827 | .\" .B CLONE_THREAD | |
6387216b MK |
828 | .\" was specified. |
829 | .\" (Since Linux 2.6.0-test6.) | |
fea681da MK |
830 | .TP |
831 | .B EINVAL | |
832 | Both | |
833 | .B CLONE_FS | |
834 | and | |
835 | .B CLONE_NEWNS | |
836 | were specified in | |
837 | .IR flags . | |
838 | .TP | |
839 | .B EINVAL | |
82ee147a | 840 | Both |
667417b3 MK |
841 | .B CLONE_NEWIPC |
842 | and | |
843 | .B CLONE_SYSVSEM | |
844 | were specified in | |
845 | .IR flags . | |
846 | .TP | |
847 | .B EINVAL | |
848 | Both | |
82ee147a MK |
849 | .BR CLONE_NEWPID |
850 | and | |
851 | .BR CLONE_THREAD | |
852 | were specified in | |
853 | .IR flags . | |
854 | .TP | |
855 | .B EINVAL | |
c13182ef | 856 | Returned by |
edcc65ff | 857 | .BR clone () |
c13182ef | 858 | when a zero value is specified for |
fea681da MK |
859 | .IR child_stack . |
860 | .TP | |
28cad2c1 | 861 | .B EINVAL |
667417b3 MK |
862 | .BR CLONE_NEWIPC |
863 | was specified in | |
864 | .IR flags , | |
865 | but the kernel was not configured with the | |
866 | .B CONFIG_SYSVIPC | |
867 | and | |
868 | .BR CONFIG_IPC_NS | |
869 | options. | |
870 | .TP | |
871 | .B EINVAL | |
163bf178 MK |
872 | .BR CLONE_NEWNET |
873 | was specified in | |
874 | .IR flags , | |
875 | but the kernel was not configured with the | |
876 | .B CONFIG_NET_NS | |
877 | option. | |
878 | .TP | |
879 | .B EINVAL | |
28cad2c1 MK |
880 | .BR CLONE_NEWPID |
881 | was specified in | |
882 | .IR flags , | |
883 | but the kernel was not configured with the | |
884 | .B CONFIG_PID_NS | |
885 | option. | |
886 | .TP | |
43ce9dda MK |
887 | .B EINVAL |
888 | .BR CLONE_NEWUTS | |
889 | was specified in | |
890 | .IR flags , | |
891 | but the kernel was not configured with the | |
892 | .B CONFIG_UTS | |
893 | option. | |
894 | .TP | |
fea681da MK |
895 | .B ENOMEM |
896 | Cannot allocate sufficient memory to allocate a task structure for the | |
897 | child, or to copy those parts of the caller's context that need to be | |
898 | copied. | |
899 | .TP | |
900 | .B EPERM | |
667417b3 | 901 | .BR CLONE_NEWIPC , |
163bf178 | 902 | .BR CLONE_NEWNET , |
43ce9dda MK |
903 | .BR CLONE_NEWNS , |
904 | .BR CLONE_NEWPID , | |
82ee147a | 905 | or |
43ce9dda | 906 | .BR CLONE_NEWUTS |
0b9bdf82 | 907 | was specified by a non-root process (process without \fBCAP_SYS_ADMIN\fP). |
fea681da MK |
908 | .TP |
909 | .B EPERM | |
910 | .B CLONE_PID | |
911 | was specified by a process other than process 0. | |
a759cc87 | 912 | .SH VERSIONS |
fea681da | 913 | There is no entry for |
edcc65ff | 914 | .BR clone () |
a759cc87 MK |
915 | in libc5. |
916 | glibc2 provides | |
edcc65ff | 917 | .BR clone () |
fea681da | 918 | as described in this manual page. |
a1d5f77c MK |
919 | .SH "CONFORMING TO" |
920 | The | |
921 | .BR clone () | |
922 | and | |
923 | .B sys_clone | |
8382f16d | 924 | calls are Linux-specific and should not be used in programs |
a1d5f77c | 925 | intended to be portable. |
fea681da | 926 | .SH NOTES |
fd8a5be4 MK |
927 | In the kernel 2.4.x series, |
928 | .B CLONE_THREAD | |
929 | generally does not make the parent of the new thread the same | |
930 | as the parent of the calling process. | |
931 | However, for kernel versions 2.4.7 to 2.4.18 the | |
932 | .B CLONE_THREAD | |
933 | flag implied the | |
c13182ef | 934 | .B CLONE_PARENT |
fd8a5be4 | 935 | flag (as in kernel 2.6). |
fea681da | 936 | |
c13182ef MK |
937 | For a while there was |
938 | .B CLONE_DETACHED | |
a5053dcb | 939 | (introduced in 2.5.32): |
c13182ef | 940 | parent wants no child-exit signal. |
a5053dcb | 941 | In 2.6.2 the need to give this |
c13182ef MK |
942 | together with |
943 | .B CLONE_THREAD | |
a5053dcb MK |
944 | disappeared. |
945 | This flag is still defined, but has no effect. | |
946 | ||
34ccb744 | 947 | On i386, |
a5a997ca MK |
948 | .BR clone () |
949 | should not be called through vsyscall, but directly through | |
950 | .IR "int $0x80" . | |
ff929e3b | 951 | |
22399250 | 952 | On ia64, a different system call is used: |
ff929e3b MK |
953 | .nf |
954 | ||
9b0e0996 MK |
955 | .BI "int __clone2(int (*" "fn" ")(void *), " |
956 | .BI " void *" child_stack_base ", size_t " stack_size , | |
957 | .BI " int " flags ", void *" "arg" ", ... " | |
958 | .BI " /* pid_t *" pid ", struct user_desc *" tls \ | |
ff929e3b MK |
959 | ", pid_t *" ctid " */ );" |
960 | .fi | |
961 | .PP | |
962 | The | |
9b0e0996 | 963 | .BR __clone2 () |
c13182ef | 964 | system call operates in the same way as |
ff929e3b MK |
965 | .BR clone (), |
966 | except that | |
967 | .I child_stack_base | |
968 | points to the lowest address of the child's stack area, | |
969 | and | |
970 | .I stack_size | |
971 | specifies the size of the stack pointed to by | |
972 | .IR child_stack_base . | |
31830ef0 MK |
973 | .SH BUGS |
974 | Versions of the GNU C library that include the NPTL threading library | |
c13182ef | 975 | contain a wrapper function for |
0bfa087b | 976 | .BR getpid (2) |
31830ef0 | 977 | that performs caching of PIDs. |
c60237c9 MK |
978 | This caching relies on support in the glibc wrapper for |
979 | .BR clone (), | |
980 | but as currently implemented, | |
981 | the cache may not be up to date in some circumstances. | |
982 | In particular, | |
983 | if a signal is delivered to the child immediately after the | |
984 | .BR clone () | |
985 | call, then a call to | |
986 | .BR getpid () | |
987 | in a handler for the signal may return the PID | |
988 | of the calling process ("the parent"), | |
88619baf | 989 | if the clone wrapper has not yet had a chance to update the PID |
c60237c9 MK |
990 | cache in the child. |
991 | (This discussion ignores the case where the child was created using | |
9291ce36 | 992 | .BR CLONE_THREAD , |
c60237c9 MK |
993 | when |
994 | .BR getpid () | |
995 | .I should | |
996 | return the same value in the child and in the process that called | |
997 | .BR clone (), | |
a1d48abb | 998 | since the caller and the child are in the same thread group. |
e7d807b7 | 999 | The stale-cache problem also does not occur if the |
a1d48abb JR |
1000 | .I flags |
1001 | argument includes | |
1002 | .BR CLONE_VM .) | |
c60237c9 | 1003 | To get the truth, it may be necessary to use code such as the following: |
31830ef0 MK |
1004 | .nf |
1005 | ||
1006 | #include <syscall.h> | |
1007 | ||
1008 | pid_t mypid; | |
1009 | ||
1010 | mypid = syscall(SYS_getpid); | |
1011 | .fi | |
c60237c9 MK |
1012 | .\" See also the following bug reports |
1013 | .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521 | |
1014 | .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910 | |
fea681da MK |
1015 | .SH "SEE ALSO" |
1016 | .BR fork (2), | |
2b44301c | 1017 | .BR futex (2), |
fea681da MK |
1018 | .BR getpid (2), |
1019 | .BR gettid (2), | |
f2d0bbf1 | 1020 | .BR set_thread_area (2), |
2b44301c | 1021 | .BR set_tid_address (2), |
f2d0bbf1 | 1022 | .BR tkill (2), |
5cc01e9c | 1023 | .BR unshare (2), |
fea681da | 1024 | .BR wait (2), |
3616b7c0 MK |
1025 | .BR capabilities (7), |
1026 | .BR pthreads (7) |