]>
Commit | Line | Data |
---|---|---|
1 | .\" Copyright (c) 1992 Drew Eckhardt <drew@cs.colorado.edu>, March 28, 1992 | |
2 | .\" and Copyright (c) Michael Kerrisk, 2001, 2002, 2005, 2013 | |
3 | .\" | |
4 | .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE) | |
5 | .\" May be distributed under the GNU General Public License. | |
6 | .\" %%%LICENSE_END | |
7 | .\" | |
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 | |
18 | .\" Added CLONE_PARENT and revised entire page remove ambiguity | |
19 | .\" between "calling process" and "parent process" | |
20 | .\" Added CLONE_PTRACE and CLONE_VFORK | |
21 | .\" Added EPERM and EINVAL error codes | |
22 | .\" Renamed "__clone" to "clone" (which is the prototype in <sched.h>) | |
23 | .\" various other minor tidy ups and clarifications. | |
24 | .\" Modified 26 Jun 2001 by Michael Kerrisk <mtk.manpages@gmail.com> | |
25 | .\" Updated notes for 2.4.7+ behavior of CLONE_THREAD | |
26 | .\" Modified 15 Oct 2002 by Michael Kerrisk <mtk.manpages@gmail.com> | |
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 | |
31 | .\" Modified 2004-09-10 - added CLONE_PARENT_SETTID etc. - aeb. | |
32 | .\" 2005-04-12, mtk, noted the PID caching behavior of NPTL's getpid() | |
33 | .\" wrapper under BUGS. | |
34 | .\" 2005-05-10, mtk, added CLONE_SYSVSEM, CLONE_UNTRACED, CLONE_STOPPED. | |
35 | .\" 2005-05-17, mtk, Substantially enhanced discussion of CLONE_THREAD. | |
36 | .\" 2008-11-18, mtk, order CLONE_* flags alphabetically | |
37 | .\" 2008-11-18, mtk, document CLONE_NEWPID | |
38 | .\" 2008-11-19, mtk, document CLONE_NEWUTS | |
39 | .\" 2008-11-19, mtk, document CLONE_NEWIPC | |
40 | .\" 2008-11-19, Jens Axboe, mtk, document CLONE_IO | |
41 | .\" | |
42 | .TH CLONE 2 2014-08-19 "Linux" "Linux Programmer's Manual" | |
43 | .SH NAME | |
44 | clone, __clone2 \- create a child process | |
45 | .SH SYNOPSIS | |
46 | .nf | |
47 | /* Prototype for the glibc wrapper function */ | |
48 | ||
49 | .B #include <sched.h> | |
50 | ||
51 | .BI "int clone(int (*" "fn" ")(void *), void *" child_stack , | |
52 | .BI " int " flags ", void *" "arg" ", ... " | |
53 | .BI " /* pid_t *" ptid ", struct user_desc *" tls \ | |
54 | ", pid_t *" ctid " */ );" | |
55 | ||
56 | /* Prototype for the raw system call */ | |
57 | ||
58 | .BI "long clone(unsigned long " flags ", void *" child_stack , | |
59 | .BI " void *" ptid ", void *" ctid , | |
60 | .BI " struct pt_regs *" regs ); | |
61 | .fi | |
62 | .sp | |
63 | .in -4n | |
64 | Feature Test Macro Requirements for glibc wrapper function (see | |
65 | .BR feature_test_macros (7)): | |
66 | .in | |
67 | .sp | |
68 | .BR clone (): | |
69 | .ad l | |
70 | .RS 4 | |
71 | .PD 0 | |
72 | .TP 4 | |
73 | Since glibc 2.14: | |
74 | _GNU_SOURCE | |
75 | .TP 4 | |
76 | .\" See http://sources.redhat.com/bugzilla/show_bug.cgi?id=4749 | |
77 | Before glibc 2.14: | |
78 | _BSD_SOURCE || _SVID_SOURCE | |
79 | /* _GNU_SOURCE also suffices */ | |
80 | .PD | |
81 | .RE | |
82 | .ad b | |
83 | .SH DESCRIPTION | |
84 | .BR clone () | |
85 | creates a new process, in a manner similar to | |
86 | .BR fork (2). | |
87 | ||
88 | This page describes both the glibc | |
89 | .BR clone () | |
90 | wrapper function and the underlying system call on which it is based. | |
91 | The main text describes the wrapper function; | |
92 | the differences for the raw system call | |
93 | are described toward the end of this page. | |
94 | ||
95 | Unlike | |
96 | .BR fork (2), | |
97 | .BR clone () | |
98 | allows the child process to share parts of its execution context with | |
99 | the calling process, such as the memory space, the table of file | |
100 | descriptors, and the table of signal handlers. | |
101 | (Note that on this manual | |
102 | page, "calling process" normally corresponds to "parent process". | |
103 | But see the description of | |
104 | .B CLONE_PARENT | |
105 | below.) | |
106 | ||
107 | The main use of | |
108 | .BR clone () | |
109 | is to implement threads: multiple threads of control in a program that | |
110 | run concurrently in a shared memory space. | |
111 | ||
112 | When the child process is created with | |
113 | .BR clone (), | |
114 | it executes the function | |
115 | .IR fn ( arg ). | |
116 | (This differs from | |
117 | .BR fork (2), | |
118 | where execution continues in the child from the point | |
119 | of the | |
120 | .BR fork (2) | |
121 | call.) | |
122 | The | |
123 | .I fn | |
124 | argument is a pointer to a function that is called by the child | |
125 | process at the beginning of its execution. | |
126 | The | |
127 | .I arg | |
128 | argument is passed to the | |
129 | .I fn | |
130 | function. | |
131 | ||
132 | When the | |
133 | .IR fn ( arg ) | |
134 | function application returns, the child process terminates. | |
135 | The integer returned by | |
136 | .I fn | |
137 | is the exit code for the child process. | |
138 | The child process may also terminate explicitly by calling | |
139 | .BR exit (2) | |
140 | or after receiving a fatal signal. | |
141 | ||
142 | The | |
143 | .I child_stack | |
144 | argument specifies the location of the stack used by the child process. | |
145 | Since the child and calling process may share memory, | |
146 | it is not possible for the child process to execute in the | |
147 | same stack as the calling process. | |
148 | The calling process must therefore | |
149 | set up memory space for the child stack and pass a pointer to this | |
150 | space to | |
151 | .BR clone (). | |
152 | Stacks grow downward on all processors that run Linux | |
153 | (except the HP PA processors), so | |
154 | .I child_stack | |
155 | usually points to the topmost address of the memory space set up for | |
156 | the child stack. | |
157 | ||
158 | The low byte of | |
159 | .I flags | |
160 | contains the number of the | |
161 | .I "termination signal" | |
162 | sent to the parent when the child dies. | |
163 | If this signal is specified as anything other than | |
164 | .BR SIGCHLD , | |
165 | then the parent process must specify the | |
166 | .B __WALL | |
167 | or | |
168 | .B __WCLONE | |
169 | options when waiting for the child with | |
170 | .BR wait (2). | |
171 | If no signal is specified, then the parent process is not signaled | |
172 | when the child terminates. | |
173 | ||
174 | .I flags | |
175 | may also be bitwise-or'ed with zero or more of the following constants, | |
176 | in order to specify what is shared between the calling process | |
177 | and the child process: | |
178 | .TP | |
179 | .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)" | |
180 | Erase child thread ID at location | |
181 | .I ctid | |
182 | in child memory when the child exits, and do a wakeup on the futex | |
183 | at that address. | |
184 | The address involved may be changed by the | |
185 | .BR set_tid_address (2) | |
186 | system call. | |
187 | This is used by threading libraries. | |
188 | .TP | |
189 | .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)" | |
190 | Store child thread ID at location | |
191 | .I ctid | |
192 | in child memory. | |
193 | .TP | |
194 | .BR CLONE_FILES " (since Linux 2.0)" | |
195 | If | |
196 | .B CLONE_FILES | |
197 | is set, the calling process and the child process share the same file | |
198 | descriptor table. | |
199 | Any file descriptor created by the calling process or by the child | |
200 | process is also valid in the other process. | |
201 | Similarly, if one of the processes closes a file descriptor, | |
202 | or changes its associated flags (using the | |
203 | .BR fcntl (2) | |
204 | .B F_SETFD | |
205 | operation), the other process is also affected. | |
206 | ||
207 | If | |
208 | .B CLONE_FILES | |
209 | is not set, the child process inherits a copy of all file descriptors | |
210 | opened in the calling process at the time of | |
211 | .BR clone (). | |
212 | (The duplicated file descriptors in the child refer to the | |
213 | same open file descriptions (see | |
214 | .BR open (2)) | |
215 | as the corresponding file descriptors in the calling process.) | |
216 | Subsequent operations that open or close file descriptors, | |
217 | or change file descriptor flags, | |
218 | performed by either the calling | |
219 | process or the child process do not affect the other process. | |
220 | .TP | |
221 | .BR CLONE_FS " (since Linux 2.0)" | |
222 | If | |
223 | .B CLONE_FS | |
224 | is set, the caller and the child process share the same filesystem | |
225 | information. | |
226 | This includes the root of the filesystem, the current | |
227 | working directory, and the umask. | |
228 | Any call to | |
229 | .BR chroot (2), | |
230 | .BR chdir (2), | |
231 | or | |
232 | .BR umask (2) | |
233 | performed by the calling process or the child process also affects the | |
234 | other process. | |
235 | ||
236 | If | |
237 | .B CLONE_FS | |
238 | is not set, the child process works on a copy of the filesystem | |
239 | information of the calling process at the time of the | |
240 | .BR clone () | |
241 | call. | |
242 | Calls to | |
243 | .BR chroot (2), | |
244 | .BR chdir (2), | |
245 | .BR umask (2) | |
246 | performed later by one of the processes do not affect the other process. | |
247 | .TP | |
248 | .BR CLONE_IO " (since Linux 2.6.25)" | |
249 | If | |
250 | .B CLONE_IO | |
251 | is set, then the new process shares an I/O context with | |
252 | the calling process. | |
253 | If this flag is not set, then (as with | |
254 | .BR fork (2)) | |
255 | the new process has its own I/O context. | |
256 | ||
257 | .\" The following based on text from Jens Axboe | |
258 | The I/O context is the I/O scope of the disk scheduler (i.e, | |
259 | what the I/O scheduler uses to model scheduling of a process's I/O). | |
260 | If processes share the same I/O context, | |
261 | they are treated as one by the I/O scheduler. | |
262 | As a consequence, they get to share disk time. | |
263 | For some I/O schedulers, | |
264 | .\" the anticipatory and CFQ scheduler | |
265 | if two processes share an I/O context, | |
266 | they will be allowed to interleave their disk access. | |
267 | If several threads are doing I/O on behalf of the same process | |
268 | .RB ( aio_read (3), | |
269 | for instance), they should employ | |
270 | .BR CLONE_IO | |
271 | to get better I/O performance. | |
272 | .\" with CFQ and AS. | |
273 | ||
274 | If the kernel is not configured with the | |
275 | .B CONFIG_BLOCK | |
276 | option, this flag is a no-op. | |
277 | .TP | |
278 | .BR CLONE_NEWIPC " (since Linux 2.6.19)" | |
279 | If | |
280 | .B CLONE_NEWIPC | |
281 | is set, then create the process in a new IPC namespace. | |
282 | If this flag is not set, then (as with | |
283 | .BR fork (2)), | |
284 | the process is created in the same IPC namespace as | |
285 | the calling process. | |
286 | This flag is intended for the implementation of containers. | |
287 | ||
288 | An IPC namespace provides an isolated view of System\ V IPC objects (see | |
289 | .BR svipc (7)) | |
290 | and (since Linux 2.6.30) | |
291 | .\" commit 7eafd7c74c3f2e67c27621b987b28397110d643f | |
292 | .\" https://lwn.net/Articles/312232/ | |
293 | POSIX message queues | |
294 | (see | |
295 | .BR mq_overview (7)). | |
296 | The common characteristic of these IPC mechanisms is that IPC | |
297 | objects are identified by mechanisms other than filesystem | |
298 | pathnames. | |
299 | ||
300 | Objects created in an IPC namespace are visible to all other processes | |
301 | that are members of that namespace, | |
302 | but are not visible to processes in other IPC namespaces. | |
303 | ||
304 | When an IPC namespace is destroyed | |
305 | (i.e., when the last process that is a member of the namespace terminates), | |
306 | all IPC objects in the namespace are automatically destroyed. | |
307 | ||
308 | Only a privileged process | |
309 | .RB ( CAP_SYS_ADMIN ) | |
310 | can employ | |
311 | .BR CLONE_NEWIPC . | |
312 | This flag can't be specified in conjunction with | |
313 | .BR CLONE_SYSVSEM . | |
314 | ||
315 | For further information on IPC namespaces, see | |
316 | .BR namespaces (7). | |
317 | .TP | |
318 | .BR CLONE_NEWNET " (since Linux 2.6.24)" | |
319 | (The implementation of this flag was completed only | |
320 | by about kernel version 2.6.29.) | |
321 | ||
322 | If | |
323 | .B CLONE_NEWNET | |
324 | is set, then create the process in a new network namespace. | |
325 | If this flag is not set, then (as with | |
326 | .BR fork (2)) | |
327 | the process is created in the same network namespace as | |
328 | the calling process. | |
329 | This flag is intended for the implementation of containers. | |
330 | ||
331 | A network namespace provides an isolated view of the networking stack | |
332 | (network device interfaces, IPv4 and IPv6 protocol stacks, | |
333 | IP routing tables, firewall rules, the | |
334 | .I /proc/net | |
335 | and | |
336 | .I /sys/class/net | |
337 | directory trees, sockets, etc.). | |
338 | A physical network device can live in exactly one | |
339 | network namespace. | |
340 | A virtual network device ("veth") pair provides a pipe-like abstraction | |
341 | .\" FIXME . Add pointer to veth(4) page when it is eventually completed | |
342 | that can be used to create tunnels between network namespaces, | |
343 | and can be used to create a bridge to a physical network device | |
344 | in another namespace. | |
345 | ||
346 | When a network namespace is freed | |
347 | (i.e., when the last process in the namespace terminates), | |
348 | its physical network devices are moved back to the | |
349 | initial network namespace (not to the parent of the process). | |
350 | For further information on network namespaces, see | |
351 | .BR namespaces (7). | |
352 | ||
353 | Only a privileged process | |
354 | .RB ( CAP_SYS_ADMIN ) | |
355 | can employ | |
356 | .BR CLONE_NEWNET . | |
357 | .TP | |
358 | .BR CLONE_NEWNS " (since Linux 2.4.19)" | |
359 | If | |
360 | .B CLONE_NEWNS | |
361 | is set, the cloned child is started in a new mount namespace, | |
362 | initialized with a copy of the namespace of the parent. | |
363 | If | |
364 | .B CLONE_NEWNS | |
365 | is not set, the child lives in the same mount | |
366 | namespace as the parent. | |
367 | ||
368 | For further information on mount namespaces, see | |
369 | .BR namespaces (7). | |
370 | ||
371 | Only a privileged process | |
372 | .RB ( CAP_SYS_ADMIN ) | |
373 | can employ | |
374 | .BR CLONE_NEWNS . | |
375 | It is not permitted to specify both | |
376 | .B CLONE_NEWNS | |
377 | and | |
378 | .B CLONE_FS | |
379 | in the same | |
380 | .BR clone () | |
381 | call. | |
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. | |
399 | ||
400 | For further information on PID namespaces, see | |
401 | .BR namespaces (7) | |
402 | and | |
403 | .BR pid_namespaces (7) | |
404 | ||
405 | Only a privileged process | |
406 | .RB ( CAP_SYS_ADMIN ) | |
407 | can employ | |
408 | .BR CLONE_NEWPID . | |
409 | This flag can't be specified in conjunction with | |
410 | .BR CLONE_THREAD | |
411 | or | |
412 | .BR CLONE_PARENT . | |
413 | .TP | |
414 | .BR CLONE_NEWUSER | |
415 | (This flag first became meaningful for | |
416 | .BR clone () | |
417 | in Linux 2.6.23, | |
418 | the current | |
419 | .BR clone() | |
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.) | |
423 | ||
424 | If | |
425 | .B CLONE_NEWUSER | |
426 | is set, then create the process in a new user namespace. | |
427 | If this flag is not set, then (as with | |
428 | .BR fork (2)) | |
429 | the process is created in the same user namespace as the calling process. | |
430 | ||
431 | For further information on user namespaces, see | |
432 | .BR namespaces (7) | |
433 | and | |
434 | .BR user_namespaces (7) | |
435 | ||
436 | Before Linux 3.8, use of | |
437 | .BR CLONE_NEWUSER | |
438 | required that the caller have three capabilities: | |
439 | .BR CAP_SYS_ADMIN , | |
440 | .BR CAP_SETUID , | |
441 | and | |
442 | .BR CAP_SETGID . | |
443 | .\" Before Linux 2.6.29, it appears that only CAP_SYS_ADMIN was needed | |
444 | Starting with Linux 3.8, | |
445 | no privileges are needed to create a user namespace. | |
446 | ||
447 | This flag can't be specified in conjunction with | |
448 | .BR CLONE_THREAD | |
449 | or | |
450 | .BR CLONE_PARENT . | |
451 | For security reasons, | |
452 | .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71 | |
453 | .\" https://lwn.net/Articles/543273/ | |
454 | .\" The fix actually went into 3.9 and into 3.8.3. However, user namespaces | |
455 | .\" were, for practical purposes, unusable in earlier 3.8.x because of the | |
456 | .\" various filesystems that didn't support userns. | |
457 | .BR CLONE_NEWUSER | |
458 | cannot be specified in conjunction with | |
459 | .BR CLONE_FS . | |
460 | ||
461 | For further information on user namespaces, see | |
462 | .BR user_namespaces (7). | |
463 | .TP | |
464 | .BR CLONE_NEWUTS " (since Linux 2.6.19)" | |
465 | If | |
466 | .B CLONE_NEWUTS | |
467 | is set, then create the process in a new UTS namespace, | |
468 | whose identifiers are initialized by duplicating the identifiers | |
469 | from the UTS namespace of the calling process. | |
470 | If this flag is not set, then (as with | |
471 | .BR fork (2)) | |
472 | the process is created in the same UTS namespace as | |
473 | the calling process. | |
474 | This flag is intended for the implementation of containers. | |
475 | ||
476 | A UTS namespace is the set of identifiers returned by | |
477 | .BR uname (2); | |
478 | among these, the domain name and the hostname can be modified by | |
479 | .BR setdomainname (2) | |
480 | and | |
481 | .BR sethostname (2), | |
482 | respectively. | |
483 | Changes made to the identifiers in a UTS namespace | |
484 | are visible to all other processes in the same namespace, | |
485 | but are not visible to processes in other UTS namespaces. | |
486 | ||
487 | Only a privileged process | |
488 | .RB ( CAP_SYS_ADMIN ) | |
489 | can employ | |
490 | .BR CLONE_NEWUTS . | |
491 | ||
492 | For further information on UTS namespaces, see | |
493 | .BR namespaces (7). | |
494 | .TP | |
495 | .BR CLONE_PARENT " (since Linux 2.3.12)" | |
496 | If | |
497 | .B CLONE_PARENT | |
498 | is set, then the parent of the new child (as returned by | |
499 | .BR getppid (2)) | |
500 | will be the same as that of the calling process. | |
501 | ||
502 | If | |
503 | .B CLONE_PARENT | |
504 | is not set, then (as with | |
505 | .BR fork (2)) | |
506 | the child's parent is the calling process. | |
507 | ||
508 | Note that it is the parent process, as returned by | |
509 | .BR getppid (2), | |
510 | which is signaled when the child terminates, so that | |
511 | if | |
512 | .B CLONE_PARENT | |
513 | is set, then the parent of the calling process, rather than the | |
514 | calling process itself, will be signaled. | |
515 | .TP | |
516 | .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)" | |
517 | Store child thread ID at location | |
518 | .I ptid | |
519 | in parent and child memory. | |
520 | (In Linux 2.5.32-2.5.48 there was a flag | |
521 | .B CLONE_SETTID | |
522 | that did this.) | |
523 | .TP | |
524 | .BR CLONE_PID " (obsolete)" | |
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. | |
531 | Since 2.3.21 this flag can be | |
532 | specified only by the system boot process (PID 0). | |
533 | It disappeared in Linux 2.5.16. | |
534 | .TP | |
535 | .BR CLONE_PTRACE " (since Linux 2.2)" | |
536 | If | |
537 | .B CLONE_PTRACE | |
538 | is specified, and the calling process is being traced, | |
539 | then trace the child also (see | |
540 | .BR ptrace (2)). | |
541 | .TP | |
542 | .BR CLONE_SETTLS " (since Linux 2.5.32)" | |
543 | The | |
544 | .I newtls | |
545 | argument is the new TLS (Thread Local Storage) descriptor. | |
546 | (See | |
547 | .BR set_thread_area (2).) | |
548 | .TP | |
549 | .BR CLONE_SIGHAND " (since Linux 2.0)" | |
550 | If | |
551 | .B CLONE_SIGHAND | |
552 | is set, the calling process and the child process share the same table of | |
553 | signal handlers. | |
554 | If the calling process or child process calls | |
555 | .BR sigaction (2) | |
556 | to change the behavior associated with a signal, the behavior is | |
557 | changed in the other process as well. | |
558 | However, the calling process and child | |
559 | processes still have distinct signal masks and sets of pending | |
560 | signals. | |
561 | So, one of them may block or unblock some signals using | |
562 | .BR sigprocmask (2) | |
563 | without affecting the other process. | |
564 | ||
565 | If | |
566 | .B CLONE_SIGHAND | |
567 | is not set, the child process inherits a copy of the signal handlers | |
568 | of the calling process at the time | |
569 | .BR clone () | |
570 | is called. | |
571 | Calls to | |
572 | .BR sigaction (2) | |
573 | performed later by one of the processes have no effect on the other | |
574 | process. | |
575 | ||
576 | Since Linux 2.6.0-test6, | |
577 | .I flags | |
578 | must also include | |
579 | .B CLONE_VM | |
580 | if | |
581 | .B CLONE_SIGHAND | |
582 | is specified | |
583 | .TP | |
584 | .BR CLONE_STOPPED " (since Linux 2.6.0-test2)" | |
585 | If | |
586 | .B CLONE_STOPPED | |
587 | is set, then the child is initially stopped (as though it was sent a | |
588 | .B SIGSTOP | |
589 | signal), and must be resumed by sending it a | |
590 | .B SIGCONT | |
591 | signal. | |
592 | ||
593 | This flag was | |
594 | .I deprecated | |
595 | from Linux 2.6.25 onward, | |
596 | and was | |
597 | .I removed | |
598 | altogether in Linux 2.6.38. | |
599 | .\" glibc 2.8 removed this defn from bits/sched.h | |
600 | .TP | |
601 | .BR CLONE_SYSVSEM " (since Linux 2.5.10)" | |
602 | If | |
603 | .B CLONE_SYSVSEM | |
604 | is set, then the child and the calling process share | |
605 | a single list of System V semaphore adjustment | |
606 | .RI ( semadj ) | |
607 | values (see | |
608 | .BR semop (2)). | |
609 | In this case, the shared list accumulates | |
610 | .I semadj | |
611 | values across all processes sharing the list, | |
612 | and semaphore adjustments are performed only when the last process | |
613 | that is sharing the list terminates (or ceases sharing the list using | |
614 | .BR unshare (2)). | |
615 | If this flag is not set, then the child has a separate | |
616 | .I semadj | |
617 | list that is initially empty. | |
618 | .TP | |
619 | .BR CLONE_THREAD " (since Linux 2.4.0-test8)" | |
620 | If | |
621 | .B CLONE_THREAD | |
622 | is set, the child is placed in the same thread group as the calling process. | |
623 | To make the remainder of the discussion of | |
624 | .B CLONE_THREAD | |
625 | more readable, the term "thread" is used to refer to the | |
626 | processes within a thread group. | |
627 | ||
628 | Thread groups were a feature added in Linux 2.4 to support the | |
629 | POSIX threads notion of a set of threads that share a single PID. | |
630 | Internally, this shared PID is the so-called | |
631 | thread group identifier (TGID) for the thread group. | |
632 | Since Linux 2.4, calls to | |
633 | .BR getpid (2) | |
634 | return the TGID of the caller. | |
635 | ||
636 | The threads within a group can be distinguished by their (system-wide) | |
637 | unique thread IDs (TID). | |
638 | A new thread's TID is available as the function result | |
639 | returned to the caller of | |
640 | .BR clone (), | |
641 | and a thread can obtain | |
642 | its own TID using | |
643 | .BR gettid (2). | |
644 | ||
645 | When a call is made to | |
646 | .BR clone () | |
647 | without specifying | |
648 | .BR CLONE_THREAD , | |
649 | then the resulting thread is placed in a new thread group | |
650 | whose TGID is the same as the thread's TID. | |
651 | This thread is the | |
652 | .I leader | |
653 | of the new thread group. | |
654 | ||
655 | A new thread created with | |
656 | .B CLONE_THREAD | |
657 | has the same parent process as the caller of | |
658 | .BR clone () | |
659 | (i.e., like | |
660 | .BR CLONE_PARENT ), | |
661 | so that calls to | |
662 | .BR getppid (2) | |
663 | return the same value for all of the threads in a thread group. | |
664 | When a | |
665 | .B CLONE_THREAD | |
666 | thread terminates, the thread that created it using | |
667 | .BR clone () | |
668 | is not sent a | |
669 | .B SIGCHLD | |
670 | (or other termination) signal; | |
671 | nor can the status of such a thread be obtained | |
672 | using | |
673 | .BR wait (2). | |
674 | (The thread is said to be | |
675 | .IR detached .) | |
676 | ||
677 | After all of the threads in a thread group terminate | |
678 | the parent process of the thread group is sent a | |
679 | .B SIGCHLD | |
680 | (or other termination) signal. | |
681 | ||
682 | If any of the threads in a thread group performs an | |
683 | .BR execve (2), | |
684 | then all threads other than the thread group leader are terminated, | |
685 | and the new program is executed in the thread group leader. | |
686 | ||
687 | If one of the threads in a thread group creates a child using | |
688 | .BR fork (2), | |
689 | then any thread in the group can | |
690 | .BR wait (2) | |
691 | for that child. | |
692 | ||
693 | Since Linux 2.5.35, | |
694 | .I flags | |
695 | must also include | |
696 | .B CLONE_SIGHAND | |
697 | if | |
698 | .B CLONE_THREAD | |
699 | is specified | |
700 | (and note that, since Linux 2.6.0-test6, | |
701 | .BR CLONE_SIGHAND | |
702 | also requires | |
703 | .BR CLONE_VM | |
704 | to be included). | |
705 | ||
706 | Signals may be sent to a thread group as a whole (i.e., a TGID) using | |
707 | .BR kill (2), | |
708 | or to a specific thread (i.e., TID) using | |
709 | .BR tgkill (2). | |
710 | ||
711 | Signal dispositions and actions are process-wide: | |
712 | if an unhandled signal is delivered to a thread, then | |
713 | it will affect (terminate, stop, continue, be ignored in) | |
714 | all members of the thread group. | |
715 | ||
716 | Each thread has its own signal mask, as set by | |
717 | .BR sigprocmask (2), | |
718 | but signals can be pending either: for the whole process | |
719 | (i.e., deliverable to any member of the thread group), | |
720 | when sent with | |
721 | .BR kill (2); | |
722 | or for an individual thread, when sent with | |
723 | .BR tgkill (2). | |
724 | A call to | |
725 | .BR sigpending (2) | |
726 | returns a signal set that is the union of the signals pending for the | |
727 | whole process and the signals that are pending for the calling thread. | |
728 | ||
729 | If | |
730 | .BR kill (2) | |
731 | is used to send a signal to a thread group, | |
732 | and the thread group has installed a handler for the signal, then | |
733 | the handler will be invoked in exactly one, arbitrarily selected | |
734 | member of the thread group that has not blocked the signal. | |
735 | If multiple threads in a group are waiting to accept the same signal using | |
736 | .BR sigwaitinfo (2), | |
737 | the kernel will arbitrarily select one of these threads | |
738 | to receive a signal sent using | |
739 | .BR kill (2). | |
740 | .TP | |
741 | .BR CLONE_UNTRACED " (since Linux 2.5.46)" | |
742 | If | |
743 | .B CLONE_UNTRACED | |
744 | is specified, then a tracing process cannot force | |
745 | .B CLONE_PTRACE | |
746 | on this child process. | |
747 | .TP | |
748 | .BR CLONE_VFORK " (since Linux 2.2)" | |
749 | If | |
750 | .B CLONE_VFORK | |
751 | is set, the execution of the calling process is suspended | |
752 | until the child releases its virtual memory | |
753 | resources via a call to | |
754 | .BR execve (2) | |
755 | or | |
756 | .BR _exit (2) | |
757 | (as with | |
758 | .BR vfork (2)). | |
759 | ||
760 | If | |
761 | .B CLONE_VFORK | |
762 | is not set, then both the calling process and the child are schedulable | |
763 | after the call, and an application should not rely on execution occurring | |
764 | in any particular order. | |
765 | .TP | |
766 | .BR CLONE_VM " (since Linux 2.0)" | |
767 | If | |
768 | .B CLONE_VM | |
769 | is set, the calling process and the child process run in the same memory | |
770 | space. | |
771 | In particular, memory writes performed by the calling process | |
772 | or by the child process are also visible in the other process. | |
773 | Moreover, any memory mapping or unmapping performed with | |
774 | .BR mmap (2) | |
775 | or | |
776 | .BR munmap (2) | |
777 | by the child or calling process also affects the other process. | |
778 | ||
779 | If | |
780 | .B CLONE_VM | |
781 | is not set, the child process runs in a separate copy of the memory | |
782 | space of the calling process at the time of | |
783 | .BR clone (). | |
784 | Memory writes or file mappings/unmappings performed by one of the | |
785 | processes do not affect the other, as with | |
786 | .BR fork (2). | |
787 | .SS C library/kernel ABI differences | |
788 | The raw | |
789 | .BR clone () | |
790 | system call corresponds more closely to | |
791 | .BR fork (2) | |
792 | in that execution in the child continues from the point of the | |
793 | call. | |
794 | As such, the | |
795 | .I fn | |
796 | and | |
797 | .I arg | |
798 | arguments of the | |
799 | .BR clone () | |
800 | wrapper function are omitted. | |
801 | Furthermore, the argument order changes. | |
802 | The raw system call interface on x86 and many other architectures is roughly: | |
803 | .in +4 | |
804 | .nf | |
805 | ||
806 | .BI "long clone(unsigned long " flags ", void *" child_stack , | |
807 | .BI " void *" ptid ", void *" ctid , | |
808 | .BI " struct pt_regs *" regs ); | |
809 | ||
810 | .fi | |
811 | .in | |
812 | Another difference for the raw system call is that the | |
813 | .I child_stack | |
814 | argument may be zero, in which case copy-on-write semantics ensure that the | |
815 | child gets separate copies of stack pages when either process modifies | |
816 | the stack. | |
817 | In this case, for correct operation, the | |
818 | .B CLONE_VM | |
819 | option should not be specified. | |
820 | ||
821 | For some architectures, the order of the arguments for the system call | |
822 | differs from that shown above. | |
823 | On the score, microblaze, ARM, ARM 64, PA-RISC, arc, Power PC, xtensa, | |
824 | and MIPS architectures, | |
825 | the order of the fourth and fifth arguments is reversed. | |
826 | On the cris and s390 architectures, | |
827 | the order of the first and second arguments is reversed. | |
828 | .SS blackfin, m68k, and sparc | |
829 | The argument-passing conventions on | |
830 | blackfin, m68k, and sparc are different from the descriptions above. | |
831 | For details, see the kernel (and glibc) source. | |
832 | .SS ia64 | |
833 | On ia64, a different interface is used: | |
834 | .nf | |
835 | ||
836 | .BI "int __clone2(int (*" "fn" ")(void *), " | |
837 | .BI " void *" child_stack_base ", size_t " stack_size , | |
838 | .BI " int " flags ", void *" "arg" ", ... " | |
839 | .BI " /* pid_t *" ptid ", struct user_desc *" tls \ | |
840 | ", pid_t *" ctid " */ );" | |
841 | .fi | |
842 | .PP | |
843 | The prototype shown above is for the glibc wrapper function; | |
844 | the raw system call interface has no | |
845 | .I fn | |
846 | or | |
847 | .I arg | |
848 | argument, and changes the order of the arguments so that | |
849 | .I flags | |
850 | is the first argument, and | |
851 | .I tls | |
852 | is the last argument. | |
853 | .PP | |
854 | .BR __clone2 () | |
855 | operates in the same way as | |
856 | .BR clone (), | |
857 | except that | |
858 | .I child_stack_base | |
859 | points to the lowest address of the child's stack area, | |
860 | and | |
861 | .I stack_size | |
862 | specifies the size of the stack pointed to by | |
863 | .IR child_stack_base . | |
864 | .SS Linux 2.4 and earlier | |
865 | In Linux 2.4 and earlier, | |
866 | .BR clone () | |
867 | does not take arguments | |
868 | .IR ptid , | |
869 | .IR tls , | |
870 | and | |
871 | .IR ctid . | |
872 | .SH RETURN VALUE | |
873 | .\" gettid(2) returns current->pid; | |
874 | .\" getpid(2) returns current->tgid; | |
875 | On success, the thread ID of the child process is returned | |
876 | in the caller's thread of execution. | |
877 | On failure, \-1 is returned | |
878 | in the caller's context, no child process will be created, and | |
879 | .I errno | |
880 | will be set appropriately. | |
881 | .SH ERRORS | |
882 | .TP | |
883 | .B EAGAIN | |
884 | Too many processes are already running; see | |
885 | .BR fork (2). | |
886 | .TP | |
887 | .B EINVAL | |
888 | .B CLONE_SIGHAND | |
889 | was specified, but | |
890 | .B CLONE_VM | |
891 | was not. | |
892 | (Since Linux 2.6.0-test6.) | |
893 | .TP | |
894 | .B EINVAL | |
895 | .B CLONE_THREAD | |
896 | was specified, but | |
897 | .B CLONE_SIGHAND | |
898 | was not. | |
899 | (Since Linux 2.5.35.) | |
900 | .\" .TP | |
901 | .\" .B EINVAL | |
902 | .\" Precisely one of | |
903 | .\" .B CLONE_DETACHED | |
904 | .\" and | |
905 | .\" .B CLONE_THREAD | |
906 | .\" was specified. | |
907 | .\" (Since Linux 2.6.0-test6.) | |
908 | .TP | |
909 | .B EINVAL | |
910 | .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71 | |
911 | Both | |
912 | .B CLONE_FS | |
913 | and | |
914 | .B CLONE_NEWNS | |
915 | were specified in | |
916 | .IR flags . | |
917 | .TP | |
918 | .BR EINVAL " (since Linux 3.9)" | |
919 | Both | |
920 | .B CLONE_NEWUSER | |
921 | and | |
922 | .B CLONE_FS | |
923 | were specified in | |
924 | .IR flags . | |
925 | .TP | |
926 | .B EINVAL | |
927 | Both | |
928 | .B CLONE_NEWIPC | |
929 | and | |
930 | .B CLONE_SYSVSEM | |
931 | were specified in | |
932 | .IR flags . | |
933 | .TP | |
934 | .B EINVAL | |
935 | One (or both) of | |
936 | .BR CLONE_NEWPID | |
937 | or | |
938 | .BR CLONE_NEWUSER | |
939 | and one (or both) of | |
940 | .BR CLONE_THREAD | |
941 | or | |
942 | .BR CLONE_PARENT | |
943 | were specified in | |
944 | .IR flags . | |
945 | .TP | |
946 | .B EINVAL | |
947 | Returned by | |
948 | .BR clone () | |
949 | when a zero value is specified for | |
950 | .IR child_stack . | |
951 | .TP | |
952 | .B EINVAL | |
953 | .BR CLONE_NEWIPC | |
954 | was specified in | |
955 | .IR flags , | |
956 | but the kernel was not configured with the | |
957 | .B CONFIG_SYSVIPC | |
958 | and | |
959 | .BR CONFIG_IPC_NS | |
960 | options. | |
961 | .TP | |
962 | .B EINVAL | |
963 | .BR CLONE_NEWNET | |
964 | was specified in | |
965 | .IR flags , | |
966 | but the kernel was not configured with the | |
967 | .B CONFIG_NET_NS | |
968 | option. | |
969 | .TP | |
970 | .B EINVAL | |
971 | .BR CLONE_NEWPID | |
972 | was specified in | |
973 | .IR flags , | |
974 | but the kernel was not configured with the | |
975 | .B CONFIG_PID_NS | |
976 | option. | |
977 | .TP | |
978 | .B EINVAL | |
979 | .BR CLONE_NEWUTS | |
980 | was specified in | |
981 | .IR flags , | |
982 | but the kernel was not configured with the | |
983 | .B CONFIG_UTS | |
984 | option. | |
985 | .TP | |
986 | .B ENOMEM | |
987 | Cannot allocate sufficient memory to allocate a task structure for the | |
988 | child, or to copy those parts of the caller's context that need to be | |
989 | copied. | |
990 | .TP | |
991 | .B EPERM | |
992 | .BR CLONE_NEWIPC , | |
993 | .BR CLONE_NEWNET , | |
994 | .BR CLONE_NEWNS , | |
995 | .BR CLONE_NEWPID , | |
996 | or | |
997 | .BR CLONE_NEWUTS | |
998 | was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP). | |
999 | .TP | |
1000 | .B EPERM | |
1001 | .B CLONE_PID | |
1002 | was specified by a process other than process 0. | |
1003 | .TP | |
1004 | .B EPERM | |
1005 | .BR CLONE_NEWUSER | |
1006 | was specified in | |
1007 | .IR flags , | |
1008 | but either the effective user ID or the effective group ID of the caller | |
1009 | does not have a mapping in the parent namespace (see | |
1010 | .BR user_namespaces (7)). | |
1011 | .SH VERSIONS | |
1012 | There is no entry for | |
1013 | .BR clone () | |
1014 | in libc5. | |
1015 | glibc2 provides | |
1016 | .BR clone () | |
1017 | as described in this manual page. | |
1018 | .SH CONFORMING TO | |
1019 | .BR clone () | |
1020 | is Linux-specific and should not be used in programs | |
1021 | intended to be portable. | |
1022 | .SH NOTES | |
1023 | In the kernel 2.4.x series, | |
1024 | .B CLONE_THREAD | |
1025 | generally does not make the parent of the new thread the same | |
1026 | as the parent of the calling process. | |
1027 | However, for kernel versions 2.4.7 to 2.4.18 the | |
1028 | .B CLONE_THREAD | |
1029 | flag implied the | |
1030 | .B CLONE_PARENT | |
1031 | flag (as in kernel 2.6). | |
1032 | ||
1033 | For a while there was | |
1034 | .B CLONE_DETACHED | |
1035 | (introduced in 2.5.32): | |
1036 | parent wants no child-exit signal. | |
1037 | In 2.6.2 the need to give this | |
1038 | together with | |
1039 | .B CLONE_THREAD | |
1040 | disappeared. | |
1041 | This flag is still defined, but has no effect. | |
1042 | ||
1043 | On i386, | |
1044 | .BR clone () | |
1045 | should not be called through vsyscall, but directly through | |
1046 | .IR "int $0x80" . | |
1047 | .SH BUGS | |
1048 | Versions of the GNU C library that include the NPTL threading library | |
1049 | contain a wrapper function for | |
1050 | .BR getpid (2) | |
1051 | that performs caching of PIDs. | |
1052 | This caching relies on support in the glibc wrapper for | |
1053 | .BR clone (), | |
1054 | but as currently implemented, | |
1055 | the cache may not be up to date in some circumstances. | |
1056 | In particular, | |
1057 | if a signal is delivered to the child immediately after the | |
1058 | .BR clone () | |
1059 | call, then a call to | |
1060 | .BR getpid (2) | |
1061 | in a handler for the signal may return the PID | |
1062 | of the calling process ("the parent"), | |
1063 | if the clone wrapper has not yet had a chance to update the PID | |
1064 | cache in the child. | |
1065 | (This discussion ignores the case where the child was created using | |
1066 | .BR CLONE_THREAD , | |
1067 | when | |
1068 | .BR getpid (2) | |
1069 | .I should | |
1070 | return the same value in the child and in the process that called | |
1071 | .BR clone (), | |
1072 | since the caller and the child are in the same thread group. | |
1073 | The stale-cache problem also does not occur if the | |
1074 | .I flags | |
1075 | argument includes | |
1076 | .BR CLONE_VM .) | |
1077 | To get the truth, it may be necessary to use code such as the following: | |
1078 | .nf | |
1079 | ||
1080 | #include <syscall.h> | |
1081 | ||
1082 | pid_t mypid; | |
1083 | ||
1084 | mypid = syscall(SYS_getpid); | |
1085 | .fi | |
1086 | .\" See also the following bug reports | |
1087 | .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521 | |
1088 | .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910 | |
1089 | .SH EXAMPLE | |
1090 | The following program demonstrates the use of | |
1091 | .BR clone () | |
1092 | to create a child process that executes in a separate UTS namespace. | |
1093 | The child changes the hostname in its UTS namespace. | |
1094 | Both parent and child then display the system hostname, | |
1095 | making it possible to see that the hostname | |
1096 | differs in the UTS namespaces of the parent and child. | |
1097 | For an example of the use of this program, see | |
1098 | .BR setns (2). | |
1099 | .SS Program source | |
1100 | .nf | |
1101 | #define _GNU_SOURCE | |
1102 | #include <sys/wait.h> | |
1103 | #include <sys/utsname.h> | |
1104 | #include <sched.h> | |
1105 | #include <string.h> | |
1106 | #include <stdio.h> | |
1107 | #include <stdlib.h> | |
1108 | #include <unistd.h> | |
1109 | ||
1110 | #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\ | |
1111 | } while (0) | |
1112 | ||
1113 | static int /* Start function for cloned child */ | |
1114 | childFunc(void *arg) | |
1115 | { | |
1116 | struct utsname uts; | |
1117 | ||
1118 | /* Change hostname in UTS namespace of child */ | |
1119 | ||
1120 | if (sethostname(arg, strlen(arg)) == \-1) | |
1121 | errExit("sethostname"); | |
1122 | ||
1123 | /* Retrieve and display hostname */ | |
1124 | ||
1125 | if (uname(&uts) == \-1) | |
1126 | errExit("uname"); | |
1127 | printf("uts.nodename in child: %s\\n", uts.nodename); | |
1128 | ||
1129 | /* Keep the namespace open for a while, by sleeping. | |
1130 | This allows some experimentation\-\-for example, another | |
1131 | process might join the namespace. */ | |
1132 | ||
1133 | sleep(200); | |
1134 | ||
1135 | return 0; /* Child terminates now */ | |
1136 | } | |
1137 | ||
1138 | #define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */ | |
1139 | ||
1140 | int | |
1141 | main(int argc, char *argv[]) | |
1142 | { | |
1143 | char *stack; /* Start of stack buffer */ | |
1144 | char *stackTop; /* End of stack buffer */ | |
1145 | pid_t pid; | |
1146 | struct utsname uts; | |
1147 | ||
1148 | if (argc < 2) { | |
1149 | fprintf(stderr, "Usage: %s <child\-hostname>\\n", argv[0]); | |
1150 | exit(EXIT_SUCCESS); | |
1151 | } | |
1152 | ||
1153 | /* Allocate stack for child */ | |
1154 | ||
1155 | stack = malloc(STACK_SIZE); | |
1156 | if (stack == NULL) | |
1157 | errExit("malloc"); | |
1158 | stackTop = stack + STACK_SIZE; /* Assume stack grows downward */ | |
1159 | ||
1160 | /* Create child that has its own UTS namespace; | |
1161 | child commences execution in childFunc() */ | |
1162 | ||
1163 | pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]); | |
1164 | if (pid == \-1) | |
1165 | errExit("clone"); | |
1166 | printf("clone() returned %ld\\n", (long) pid); | |
1167 | ||
1168 | /* Parent falls through to here */ | |
1169 | ||
1170 | sleep(1); /* Give child time to change its hostname */ | |
1171 | ||
1172 | /* Display hostname in parent\(aqs UTS namespace. This will be | |
1173 | different from hostname in child\(aqs UTS namespace. */ | |
1174 | ||
1175 | if (uname(&uts) == \-1) | |
1176 | errExit("uname"); | |
1177 | printf("uts.nodename in parent: %s\\n", uts.nodename); | |
1178 | ||
1179 | if (waitpid(pid, NULL, 0) == \-1) /* Wait for child */ | |
1180 | errExit("waitpid"); | |
1181 | printf("child has terminated\\n"); | |
1182 | ||
1183 | exit(EXIT_SUCCESS); | |
1184 | } | |
1185 | .fi | |
1186 | .SH SEE ALSO | |
1187 | .BR fork (2), | |
1188 | .BR futex (2), | |
1189 | .BR getpid (2), | |
1190 | .BR gettid (2), | |
1191 | .BR kcmp (2), | |
1192 | .BR set_thread_area (2), | |
1193 | .BR set_tid_address (2), | |
1194 | .BR setns (2), | |
1195 | .BR tkill (2), | |
1196 | .BR unshare (2), | |
1197 | .BR wait (2), | |
1198 | .BR capabilities (7), | |
1199 | .BR namespaces (7), | |
1200 | .BR pthreads (7) |