]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/clone.2
clone.2: Minor wording fixes
[thirdparty/man-pages.git] / man2 / clone.2
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 2017-09-15 "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 .PP
49 .B #define _GNU_SOURCE
50 .B #include <sched.h>
51 .PP
52 .BI "int clone(int (*" "fn" ")(void *), void *" child_stack ,
53 .BI " int " flags ", void *" "arg" ", ... "
54 .BI " /* pid_t *" ptid ", void *" newtls \
55 ", pid_t *" ctid " */ );"
56 .PP
57 /* For the prototype of the raw system call, see NOTES */
58 .fi
59 .SH DESCRIPTION
60 .BR clone ()
61 creates a new process, in a manner similar to
62 .BR fork (2).
63 .PP
64 This page describes both the glibc
65 .BR clone ()
66 wrapper function and the underlying system call on which it is based.
67 The main text describes the wrapper function;
68 the differences for the raw system call
69 are described toward the end of this page.
70 .PP
71 Unlike
72 .BR fork (2),
73 .BR clone ()
74 allows the child process to share parts of its execution context with
75 the calling process, such as the virtual address space, the table of file
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
81 below.)
82 .PP
83 One use of
84 .BR clone ()
85 is to implement threads: multiple flows of control in a program that
86 run concurrently in a shared address space.
87 .PP
88 When the child process is created with
89 .BR clone (),
90 it executes the function
91 .IR fn ( arg ).
92 (This differs from
93 .BR fork (2),
94 where execution continues in the child from the point
95 of the
96 .BR fork (2)
97 call.)
98 The
99 .I fn
100 argument is a pointer to a function that is called by the child
101 process at the beginning of its execution.
102 The
103 .I arg
104 argument is passed to the
105 .I fn
106 function.
107 .PP
108 When the
109 .IR fn ( arg )
110 function returns, the child process terminates.
111 The integer returned by
112 .I fn
113 is the exit status for the child process.
114 The child process may also terminate explicitly by calling
115 .BR exit (2)
116 or after receiving a fatal signal.
117 .PP
118 The
119 .I child_stack
120 argument specifies the location of the stack used by the child process.
121 Since the child and calling process may share memory,
122 it is not possible for the child process to execute in the
123 same stack as the calling process.
124 The calling process must therefore
125 set up memory space for the child stack and pass a pointer to this
126 space to
127 .BR clone ().
128 Stacks grow downward on all processors that run Linux
129 (except the HP PA processors), so
130 .I child_stack
131 usually points to the topmost address of the memory space set up for
132 the child stack.
133 .PP
134 The low byte of
135 .I flags
136 contains the number of the
137 .I "termination signal"
138 sent to the parent when the child dies.
139 If this signal is specified as anything other than
140 .BR SIGCHLD ,
141 then the parent process must specify the
142 .B __WALL
143 or
144 .B __WCLONE
145 options when waiting for the child with
146 .BR wait (2).
147 If no signal is specified, then the parent process is not signaled
148 when the child terminates.
149 .PP
150 .I flags
151 may also be bitwise-ORed with zero or more of the following constants,
152 in order to specify what is shared between the calling process
153 and the child process:
154 .TP
155 .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)"
156 Clear (zero) the child thread ID at the location
157 .I ctid
158 in child memory when the child exits, and do a wakeup on the futex
159 at that address.
160 The address involved may be changed by the
161 .BR set_tid_address (2)
162 system call.
163 This is used by threading libraries.
164 .TP
165 .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)"
166 Store the child thread ID at the location
167 .I ctid
168 in the child's memory.
169 The store operation completes before
170 .BR clone ()
171 returns control to user space.
172 .TP
173 .BR CLONE_FILES " (since Linux 2.0)"
174 If
175 .B CLONE_FILES
176 is set, the calling process and the child process share the same file
177 descriptor table.
178 Any file descriptor created by the calling process or by the child
179 process is also valid in the other process.
180 Similarly, if one of the processes closes a file descriptor,
181 or changes its associated flags (using the
182 .BR fcntl (2)
183 .B F_SETFD
184 operation), the other process is also affected.
185 If a process sharing a file descriptor table calls
186 .BR execve (2),
187 its file descriptor table is duplicated (unshared).
188 .IP
189 If
190 .B CLONE_FILES
191 is not set, the child process inherits a copy of all file descriptors
192 opened in the calling process at the time of
193 .BR clone ().
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.
198 Note, however,
199 that the duplicated file descriptors in the child refer to the same open file
200 descriptions as the corresponding file descriptors in the calling process,
201 and thus share file offsets and file status flags (see
202 .BR open (2)).
203 .TP
204 .BR CLONE_FS " (since Linux 2.0)"
205 If
206 .B CLONE_FS
207 is set, the caller and the child process share the same filesystem
208 information.
209 This includes the root of the filesystem, the current
210 working directory, and the umask.
211 Any call to
212 .BR chroot (2),
213 .BR chdir (2),
214 or
215 .BR umask (2)
216 performed by the calling process or the child process also affects the
217 other process.
218 .IP
219 If
220 .B CLONE_FS
221 is not set, the child process works on a copy of the filesystem
222 information of the calling process at the time of the
223 .BR clone ()
224 call.
225 Calls to
226 .BR chroot (2),
227 .BR chdir (2),
228 or
229 .BR umask (2)
230 performed later by one of the processes do not affect the other process.
231 .TP
232 .BR CLONE_IO " (since Linux 2.6.25)"
233 If
234 .B CLONE_IO
235 is set, then the new process shares an I/O context with
236 the calling process.
237 If this flag is not set, then (as with
238 .BR fork (2))
239 the new process has its own I/O context.
240 .IP
241 .\" The following based on text from Jens Axboe
242 The I/O context is the I/O scope of the disk scheduler (i.e.,
243 what the I/O scheduler uses to model scheduling of a process's I/O).
244 If processes share the same I/O context,
245 they are treated as one by the I/O scheduler.
246 As a consequence, they get to share disk time.
247 For some I/O schedulers,
248 .\" the anticipatory and CFQ scheduler
249 if two processes share an I/O context,
250 they will be allowed to interleave their disk access.
251 If several threads are doing I/O on behalf of the same process
252 .RB ( aio_read (3),
253 for instance), they should employ
254 .BR CLONE_IO
255 to get better I/O performance.
256 .\" with CFQ and AS.
257 .IP
258 If the kernel is not configured with the
259 .B CONFIG_BLOCK
260 option, this flag is a no-op.
261 .TP
262 .BR CLONE_NEWCGROUP " (since Linux 4.6)"
263 Create the process in a new cgroup namespace.
264 If this flag is not set, then (as with
265 .BR fork (2))
266 the process is created in the same cgroup namespaces as the calling process.
267 This flag is intended for the implementation of containers.
268 .IP
269 For further information on cgroup namespaces, see
270 .BR cgroup_namespaces (7).
271 .IP
272 Only a privileged process
273 .RB ( CAP_SYS_ADMIN )
274 can employ
275 .BR CLONE_NEWCGROUP .
276 .\"
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 .IP
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 .IP
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 .IP
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 .IP
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 .IP
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 .IP
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 .IP
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 .IP
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 .IP
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 .IP
368 Only a privileged process
369 .RB ( CAP_SYS_ADMIN )
370 can employ
371 .BR CLONE_NEWNS .
372 It is not permitted to specify both
373 .B CLONE_NEWNS
374 and
375 .B CLONE_FS
376 .\" See https://lwn.net/Articles/543273/
377 in the same
378 .BR clone ()
379 call.
380 .IP
381 For further information on mount namespaces, see
382 .BR namespaces (7)
383 and
384 .BR mount_namespaces (7).
385 .TP
386 .BR CLONE_NEWPID " (since Linux 2.6.24)"
387 .\" This explanation draws a lot of details from
388 .\" http://lwn.net/Articles/259217/
389 .\" Authors: Pavel Emelyanov <xemul@openvz.org>
390 .\" and Kir Kolyshkin <kir@openvz.org>
391 .\"
392 .\" The primary kernel commit is 30e49c263e36341b60b735cbef5ca37912549264
393 .\" Author: Pavel Emelyanov <xemul@openvz.org>
394 If
395 .B CLONE_NEWPID
396 is set, then create the process in a new PID namespace.
397 If this flag is not set, then (as with
398 .BR fork (2))
399 the process is created in the same PID namespace as
400 the calling process.
401 This flag is intended for the implementation of containers.
402 .IP
403 For further information on PID namespaces, see
404 .BR namespaces (7)
405 and
406 .BR pid_namespaces (7).
407 .IP
408 Only a privileged process
409 .RB ( CAP_SYS_ADMIN )
410 can employ
411 .BR CLONE_NEWPID .
412 This flag can't be specified in conjunction with
413 .BR CLONE_THREAD
414 or
415 .BR CLONE_PARENT .
416 .TP
417 .BR CLONE_NEWUSER
418 (This flag first became meaningful for
419 .BR clone ()
420 in Linux 2.6.23,
421 the current
422 .BR clone ()
423 semantics were merged in Linux 3.5,
424 and the final pieces to make the user namespaces completely usable were
425 merged in Linux 3.8.)
426 .IP
427 If
428 .B CLONE_NEWUSER
429 is set, then create the process in a new user namespace.
430 If this flag is not set, then (as with
431 .BR fork (2))
432 the process is created in the same user namespace as the calling process.
433 .IP
434 For further information on user namespaces, see
435 .BR namespaces (7)
436 and
437 .BR user_namespaces (7)
438 .IP
439 Before Linux 3.8, use of
440 .BR CLONE_NEWUSER
441 required that the caller have three capabilities:
442 .BR CAP_SYS_ADMIN ,
443 .BR CAP_SETUID ,
444 and
445 .BR CAP_SETGID .
446 .\" Before Linux 2.6.29, it appears that only CAP_SYS_ADMIN was needed
447 Starting with Linux 3.8,
448 no privileges are needed to create a user namespace.
449 .IP
450 This flag can't be specified in conjunction with
451 .BR CLONE_THREAD
452 or
453 .BR CLONE_PARENT .
454 For security reasons,
455 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
456 .\" https://lwn.net/Articles/543273/
457 .\" The fix actually went into 3.9 and into 3.8.3. However, user namespaces
458 .\" were, for practical purposes, unusable in earlier 3.8.x because of the
459 .\" various filesystems that didn't support userns.
460 .BR CLONE_NEWUSER
461 cannot be specified in conjunction with
462 .BR CLONE_FS .
463 .IP
464 For further information on user namespaces, see
465 .BR user_namespaces (7).
466 .TP
467 .BR CLONE_NEWUTS " (since Linux 2.6.19)"
468 If
469 .B CLONE_NEWUTS
470 is set, then create the process in a new UTS namespace,
471 whose identifiers are initialized by duplicating the identifiers
472 from the UTS namespace of the calling process.
473 If this flag is not set, then (as with
474 .BR fork (2))
475 the process is created in the same UTS namespace as
476 the calling process.
477 This flag is intended for the implementation of containers.
478 .IP
479 A UTS namespace is the set of identifiers returned by
480 .BR uname (2);
481 among these, the domain name and the hostname can be modified by
482 .BR setdomainname (2)
483 and
484 .BR sethostname (2),
485 respectively.
486 Changes made to the identifiers in a UTS namespace
487 are visible to all other processes in the same namespace,
488 but are not visible to processes in other UTS namespaces.
489 .IP
490 Only a privileged process
491 .RB ( CAP_SYS_ADMIN )
492 can employ
493 .BR CLONE_NEWUTS .
494 .IP
495 For further information on UTS namespaces, see
496 .BR namespaces (7).
497 .TP
498 .BR CLONE_PARENT " (since Linux 2.3.12)"
499 If
500 .B CLONE_PARENT
501 is set, then the parent of the new child (as returned by
502 .BR getppid (2))
503 will be the same as that of the calling process.
504 .IP
505 If
506 .B CLONE_PARENT
507 is not set, then (as with
508 .BR fork (2))
509 the child's parent is the calling process.
510 .IP
511 Note that it is the parent process, as returned by
512 .BR getppid (2),
513 which is signaled when the child terminates, so that
514 if
515 .B CLONE_PARENT
516 is set, then the parent of the calling process, rather than the
517 calling process itself, will be signaled.
518 .TP
519 .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)"
520 Store the child thread ID at the location
521 .I ptid
522 in the parent's memory.
523 (In Linux 2.5.32-2.5.48 there was a flag
524 .B CLONE_SETTID
525 that did this.)
526 The store operation completes before
527 .BR clone ()
528 returns control to user space.
529 .TP
530 .BR CLONE_PID " (obsolete)"
531 If
532 .B CLONE_PID
533 is set, the child process is created with the same process ID as
534 the calling process.
535 This is good for hacking the system, but otherwise
536 of not much use.
537 Since 2.3.21 this flag can be
538 specified only by the system boot process (PID 0).
539 It disappeared in Linux 2.5.16.
540 Since then, the kernel silently ignores it without error.
541 .TP
542 .BR CLONE_PTRACE " (since Linux 2.2)"
543 If
544 .B CLONE_PTRACE
545 is specified, and the calling process is being traced,
546 then trace the child also (see
547 .BR ptrace (2)).
548 .TP
549 .BR CLONE_SETTLS " (since Linux 2.5.32)"
550 The TLS (Thread Local Storage) descriptor is set to
551 .I newtls.
552 .IP
553 The interpretation of
554 .I newtls
555 and the resulting effect is architecture dependent.
556 On x86,
557 .I newtls
558 is interpreted as a
559 .IR "struct user_desc *"
560 (see
561 .BR set_thread_area (2)).
562 On x86_64 it is the new value to be set for the %fs base register
563 (see the
564 .I ARCH_SET_FS
565 argument to
566 .BR arch_prctl (2)).
567 On architectures with a dedicated TLS register, it is the new value
568 of that register.
569 .TP
570 .BR CLONE_SIGHAND " (since Linux 2.0)"
571 If
572 .B CLONE_SIGHAND
573 is set, the calling process and the child process share the same table of
574 signal handlers.
575 If the calling process or child process calls
576 .BR sigaction (2)
577 to change the behavior associated with a signal, the behavior is
578 changed in the other process as well.
579 However, the calling process and child
580 processes still have distinct signal masks and sets of pending
581 signals.
582 So, one of them may block or unblock signals using
583 .BR sigprocmask (2)
584 without affecting the other process.
585 .IP
586 If
587 .B CLONE_SIGHAND
588 is not set, the child process inherits a copy of the signal handlers
589 of the calling process at the time
590 .BR clone ()
591 is called.
592 Calls to
593 .BR sigaction (2)
594 performed later by one of the processes have no effect on the other
595 process.
596 .IP
597 Since Linux 2.6.0-test6,
598 .I flags
599 must also include
600 .B CLONE_VM
601 if
602 .B CLONE_SIGHAND
603 is specified
604 .TP
605 .BR CLONE_STOPPED " (since Linux 2.6.0-test2)"
606 If
607 .B CLONE_STOPPED
608 is set, then the child is initially stopped (as though it was sent a
609 .B SIGSTOP
610 signal), and must be resumed by sending it a
611 .B SIGCONT
612 signal.
613 .IP
614 This flag was
615 .I deprecated
616 from Linux 2.6.25 onward,
617 and was
618 .I removed
619 altogether in Linux 2.6.38.
620 Since then, the kernel silently ignores it without error.
621 .\" glibc 2.8 removed this defn from bits/sched.h
622 Starting with Linux 4.6, the same bit was reused for the
623 .BR CLONE_NEWCGROUP
624 flag.
625 .TP
626 .BR CLONE_SYSVSEM " (since Linux 2.5.10)"
627 If
628 .B CLONE_SYSVSEM
629 is set, then the child and the calling process share
630 a single list of System V semaphore adjustment
631 .RI ( semadj )
632 values (see
633 .BR semop (2)).
634 In this case, the shared list accumulates
635 .I semadj
636 values across all processes sharing the list,
637 and semaphore adjustments are performed only when the last process
638 that is sharing the list terminates (or ceases sharing the list using
639 .BR unshare (2)).
640 If this flag is not set, then the child has a separate
641 .I semadj
642 list that is initially empty.
643 .TP
644 .BR CLONE_THREAD " (since Linux 2.4.0-test8)"
645 If
646 .B CLONE_THREAD
647 is set, the child is placed in the same thread group as the calling process.
648 To make the remainder of the discussion of
649 .B CLONE_THREAD
650 more readable, the term "thread" is used to refer to the
651 processes within a thread group.
652 .IP
653 Thread groups were a feature added in Linux 2.4 to support the
654 POSIX threads notion of a set of threads that share a single PID.
655 Internally, this shared PID is the so-called
656 thread group identifier (TGID) for the thread group.
657 Since Linux 2.4, calls to
658 .BR getpid (2)
659 return the TGID of the caller.
660 .IP
661 The threads within a group can be distinguished by their (system-wide)
662 unique thread IDs (TID).
663 A new thread's TID is available as the function result
664 returned to the caller of
665 .BR clone (),
666 and a thread can obtain
667 its own TID using
668 .BR gettid (2).
669 .IP
670 When a call is made to
671 .BR clone ()
672 without specifying
673 .BR CLONE_THREAD ,
674 then the resulting thread is placed in a new thread group
675 whose TGID is the same as the thread's TID.
676 This thread is the
677 .I leader
678 of the new thread group.
679 .IP
680 A new thread created with
681 .B CLONE_THREAD
682 has the same parent process as the caller of
683 .BR clone ()
684 (i.e., like
685 .BR CLONE_PARENT ),
686 so that calls to
687 .BR getppid (2)
688 return the same value for all of the threads in a thread group.
689 When a
690 .B CLONE_THREAD
691 thread terminates, the thread that created it using
692 .BR clone ()
693 is not sent a
694 .B SIGCHLD
695 (or other termination) signal;
696 nor can the status of such a thread be obtained
697 using
698 .BR wait (2).
699 (The thread is said to be
700 .IR detached .)
701 .IP
702 After all of the threads in a thread group terminate
703 the parent process of the thread group is sent a
704 .B SIGCHLD
705 (or other termination) signal.
706 .IP
707 If any of the threads in a thread group performs an
708 .BR execve (2),
709 then all threads other than the thread group leader are terminated,
710 and the new program is executed in the thread group leader.
711 .IP
712 If one of the threads in a thread group creates a child using
713 .BR fork (2),
714 then any thread in the group can
715 .BR wait (2)
716 for that child.
717 .IP
718 Since Linux 2.5.35,
719 .I flags
720 must also include
721 .B CLONE_SIGHAND
722 if
723 .B CLONE_THREAD
724 is specified
725 (and note that, since Linux 2.6.0-test6,
726 .BR CLONE_SIGHAND
727 also requires
728 .BR CLONE_VM
729 to be included).
730 .IP
731 Signals may be sent to a thread group as a whole (i.e., a TGID) using
732 .BR kill (2),
733 or to a specific thread (i.e., TID) using
734 .BR tgkill (2).
735 .IP
736 Signal dispositions and actions are process-wide:
737 if an unhandled signal is delivered to a thread, then
738 it will affect (terminate, stop, continue, be ignored in)
739 all members of the thread group.
740 .IP
741 Each thread has its own signal mask, as set by
742 .BR sigprocmask (2),
743 but signals can be pending either: for the whole process
744 (i.e., deliverable to any member of the thread group),
745 when sent with
746 .BR kill (2);
747 or for an individual thread, when sent with
748 .BR tgkill (2).
749 A call to
750 .BR sigpending (2)
751 returns a signal set that is the union of the signals pending for the
752 whole process and the signals that are pending for the calling thread.
753 .IP
754 If
755 .BR kill (2)
756 is used to send a signal to a thread group,
757 and the thread group has installed a handler for the signal, then
758 the handler will be invoked in exactly one, arbitrarily selected
759 member of the thread group that has not blocked the signal.
760 If multiple threads in a group are waiting to accept the same signal using
761 .BR sigwaitinfo (2),
762 the kernel will arbitrarily select one of these threads
763 to receive a signal sent using
764 .BR kill (2).
765 .TP
766 .BR CLONE_UNTRACED " (since Linux 2.5.46)"
767 If
768 .B CLONE_UNTRACED
769 is specified, then a tracing process cannot force
770 .B CLONE_PTRACE
771 on this child process.
772 .TP
773 .BR CLONE_VFORK " (since Linux 2.2)"
774 If
775 .B CLONE_VFORK
776 is set, the execution of the calling process is suspended
777 until the child releases its virtual memory
778 resources via a call to
779 .BR execve (2)
780 or
781 .BR _exit (2)
782 (as with
783 .BR vfork (2)).
784 .IP
785 If
786 .B CLONE_VFORK
787 is not set, then both the calling process and the child are schedulable
788 after the call, and an application should not rely on execution occurring
789 in any particular order.
790 .TP
791 .BR CLONE_VM " (since Linux 2.0)"
792 If
793 .B CLONE_VM
794 is set, the calling process and the child process run in the same memory
795 space.
796 In particular, memory writes performed by the calling process
797 or by the child process are also visible in the other process.
798 Moreover, any memory mapping or unmapping performed with
799 .BR mmap (2)
800 or
801 .BR munmap (2)
802 by the child or calling process also affects the other process.
803 .IP
804 If
805 .B CLONE_VM
806 is not set, the child process runs in a separate copy of the memory
807 space of the calling process at the time of
808 .BR clone ().
809 Memory writes or file mappings/unmappings performed by one of the
810 processes do not affect the other, as with
811 .BR fork (2).
812 .SH NOTES
813 Note that the glibc
814 .BR clone ()
815 wrapper function makes some changes
816 in the memory pointed to by
817 .I child_stack
818 (changes required to set the stack up correctly for the child)
819 .I before
820 invoking the
821 .BR clone ()
822 system call.
823 So, in cases where
824 .BR clone ()
825 is used to recursively create children,
826 do not use the buffer employed for the parent's stack
827 as the stack of the child.
828 .\"
829 .SS C library/kernel differences
830 The raw
831 .BR clone ()
832 system call corresponds more closely to
833 .BR fork (2)
834 in that execution in the child continues from the point of the
835 call.
836 As such, the
837 .I fn
838 and
839 .I arg
840 arguments of the
841 .BR clone ()
842 wrapper function are omitted.
843 .PP
844 Unlike the glibc wrapper function, the raw
845 .BR clone ()
846 system call permits
847 .IR child_stack
848 to be specified as NULL,
849 with the meaning that the child uses the stack that was
850 duplicated from the parent.
851 (If the child
852 .I shares
853 the parent's memory because of the use of the
854 .BR CLONE_VM
855 flag, then chaos is likely to result if
856 .I child_stack
857 is specified as NULL.)
858 .PP
859 The order of the arguments also differs in the raw system call,
860 and there are variations in the arguments across architectures,
861 as detailed in the following paragraphs.
862 .PP
863 The raw system call interface on x86-64 and some other architectures
864 (including sh, tile, and alpha) is roughly:
865 .PP
866 .in +4
867 .EX
868 .BI "long clone(unsigned long " flags ", void *" child_stack ,
869 .BI " int *" ptid ", int *" ctid ,
870 .BI " unsigned long " newtls );
871 .EE
872 .in
873 .PP
874 On x86-32, and several other common architectures
875 (including score, ARM, ARM 64, PA-RISC, arc, Power PC, xtensa,
876 and MIPS),
877 .\" CONFIG_CLONE_BACKWARDS
878 the order of the last two arguments is reversed:
879 .PP
880 .in +4
881 .EX
882 .BI "long clone(unsigned long " flags ", void *" child_stack ,
883 .BI " int *" ptid ", unsigned long " newtls ,
884 .BI " int *" ctid );
885 .EE
886 .in
887 .PP
888 On the cris and s390 architectures,
889 .\" CONFIG_CLONE_BACKWARDS2
890 the order of the first two arguments is reversed:
891 .PP
892 .in +4
893 .EX
894 .BI "long clone(void *" child_stack ", unsigned long " flags ,
895 .BI " int *" ptid ", int *" ctid ,
896 .BI " unsigned long " newtls );
897 .EE
898 .in
899 .PP
900 On the microblaze architecture,
901 .\" CONFIG_CLONE_BACKWARDS3
902 an additional argument is supplied:
903 .PP
904 .in +4
905 .EX
906 .BI "long clone(unsigned long " flags ", void *" child_stack ,
907 .BI " int " stack_size , "\fR /* Size of stack */"
908 .BI " int *" ptid ", int *" ctid ,
909 .BI " unsigned long " newtls );
910 .EE
911 .in
912 .PP
913 Another difference for the raw system call is that the
914 .I child_stack
915 argument may be zero, in which case copy-on-write semantics ensure that the
916 child gets separate copies of stack pages when either process modifies
917 the stack.
918 In this case, for correct operation, the
919 .B CLONE_VM
920 option should not be specified.
921 .\"
922 .SS blackfin, m68k, and sparc
923 .\" Mike Frysinger noted in a 2013 mail:
924 .\" these arches don't define __ARCH_WANT_SYS_CLONE:
925 .\" blackfin ia64 m68k sparc
926 The argument-passing conventions on
927 blackfin, m68k, and sparc are different from the descriptions above.
928 For details, see the kernel (and glibc) source.
929 .SS ia64
930 On ia64, a different interface is used:
931 .PP
932 .nf
933 .BI "int __clone2(int (*" "fn" ")(void *), "
934 .BI " void *" child_stack_base ", size_t " stack_size ,
935 .BI " int " flags ", void *" "arg" ", ... "
936 .BI " /* pid_t *" ptid ", struct user_desc *" tls \
937 ", pid_t *" ctid " */ );"
938 .fi
939 .PP
940 The prototype shown above is for the glibc wrapper function;
941 the raw system call interface has no
942 .I fn
943 or
944 .I arg
945 argument, and changes the order of the arguments so that
946 .I flags
947 is the first argument, and
948 .I tls
949 is the last argument.
950 .PP
951 .BR __clone2 ()
952 operates in the same way as
953 .BR clone (),
954 except that
955 .I child_stack_base
956 points to the lowest address of the child's stack area,
957 and
958 .I stack_size
959 specifies the size of the stack pointed to by
960 .IR child_stack_base .
961 .SS Linux 2.4 and earlier
962 In Linux 2.4 and earlier,
963 .BR clone ()
964 does not take arguments
965 .IR ptid ,
966 .IR tls ,
967 and
968 .IR ctid .
969 .SH RETURN VALUE
970 .\" gettid(2) returns current->pid;
971 .\" getpid(2) returns current->tgid;
972 On success, the thread ID of the child process is returned
973 in the caller's thread of execution.
974 On failure, \-1 is returned
975 in the caller's context, no child process will be created, and
976 .I errno
977 will be set appropriately.
978 .SH ERRORS
979 .TP
980 .B EAGAIN
981 Too many processes are already running; see
982 .BR fork (2).
983 .TP
984 .B EINVAL
985 .B CLONE_SIGHAND
986 was specified, but
987 .B CLONE_VM
988 was not.
989 (Since Linux 2.6.0-test6.)
990 .TP
991 .B EINVAL
992 .B CLONE_THREAD
993 was specified, but
994 .B CLONE_SIGHAND
995 was not.
996 (Since Linux 2.5.35.)
997 .\" .TP
998 .\" .B EINVAL
999 .\" Precisely one of
1000 .\" .B CLONE_DETACHED
1001 .\" and
1002 .\" .B CLONE_THREAD
1003 .\" was specified.
1004 .\" (Since Linux 2.6.0-test6.)
1005 .TP
1006 .B EINVAL
1007 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
1008 Both
1009 .B CLONE_FS
1010 and
1011 .B CLONE_NEWNS
1012 were specified in
1013 .IR flags .
1014 .TP
1015 .BR EINVAL " (since Linux 3.9)"
1016 Both
1017 .B CLONE_NEWUSER
1018 and
1019 .B CLONE_FS
1020 were specified in
1021 .IR flags .
1022 .TP
1023 .B EINVAL
1024 Both
1025 .B CLONE_NEWIPC
1026 and
1027 .B CLONE_SYSVSEM
1028 were specified in
1029 .IR flags .
1030 .TP
1031 .B EINVAL
1032 One (or both) of
1033 .BR CLONE_NEWPID
1034 or
1035 .BR CLONE_NEWUSER
1036 and one (or both) of
1037 .BR CLONE_THREAD
1038 or
1039 .BR CLONE_PARENT
1040 were specified in
1041 .IR flags .
1042 .TP
1043 .B EINVAL
1044 Returned by the glibc
1045 .BR clone ()
1046 wrapper function when
1047 .IR fn
1048 or
1049 .IR child_stack
1050 is specified as NULL.
1051 .TP
1052 .B EINVAL
1053 .BR CLONE_NEWIPC
1054 was specified in
1055 .IR flags ,
1056 but the kernel was not configured with the
1057 .B CONFIG_SYSVIPC
1058 and
1059 .BR CONFIG_IPC_NS
1060 options.
1061 .TP
1062 .B EINVAL
1063 .BR CLONE_NEWNET
1064 was specified in
1065 .IR flags ,
1066 but the kernel was not configured with the
1067 .B CONFIG_NET_NS
1068 option.
1069 .TP
1070 .B EINVAL
1071 .BR CLONE_NEWPID
1072 was specified in
1073 .IR flags ,
1074 but the kernel was not configured with the
1075 .B CONFIG_PID_NS
1076 option.
1077 .TP
1078 .B EINVAL
1079 .BR CLONE_NEWUTS
1080 was specified in
1081 .IR flags ,
1082 but the kernel was not configured with the
1083 .B CONFIG_UTS
1084 option.
1085 .TP
1086 .B EINVAL
1087 .I child_stack
1088 is not aligned to a suitable boundary for this architecture.
1089 For example, on aarch64,
1090 .I child_stack
1091 must be a multiple of 16.
1092 .TP
1093 .B ENOMEM
1094 Cannot allocate sufficient memory to allocate a task structure for the
1095 child, or to copy those parts of the caller's context that need to be
1096 copied.
1097 .TP
1098 .BR ENOSPC " (since Linux 3.7)"
1099 .\" commit f2302505775fd13ba93f034206f1e2a587017929
1100 .B CLONE_NEWPID
1101 was specified in flags,
1102 but the limit on the nesting depth of PID namespaces
1103 would have been exceeded; see
1104 .BR pid_namespaces (7).
1105 .TP
1106 .BR ENOSPC " (since Linux 4.9; beforehand " EUSERS )
1107 .B CLONE_NEWUSER
1108 was specified in
1109 .IR flags ,
1110 and the call would cause the limit on the number of
1111 nested user namespaces to be exceeded.
1112 See
1113 .BR user_namespaces (7).
1114 .IP
1115 From Linux 3.11 to Linux 4.8, the error diagnosed in this case was
1116 .BR EUSERS .
1117 .TP
1118 .BR ENOSPC " (since Linux 4.9)"
1119 One of the values in
1120 .I flags
1121 specified the creation of a new user namespace,
1122 but doing so would have caused the limit defined by the corresponding file in
1123 .IR /proc/sys/user
1124 to be exceeded.
1125 For further details, see
1126 .BR namespaces (7).
1127 .TP
1128 .B EPERM
1129 .BR CLONE_NEWCGROUP ,
1130 .BR CLONE_NEWIPC ,
1131 .BR CLONE_NEWNET ,
1132 .BR CLONE_NEWNS ,
1133 .BR CLONE_NEWPID ,
1134 or
1135 .BR CLONE_NEWUTS
1136 was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP).
1137 .TP
1138 .B EPERM
1139 .B CLONE_PID
1140 was specified by a process other than process 0.
1141 .TP
1142 .B EPERM
1143 .BR CLONE_NEWUSER
1144 was specified in
1145 .IR flags ,
1146 but either the effective user ID or the effective group ID of the caller
1147 does not have a mapping in the parent namespace (see
1148 .BR user_namespaces (7)).
1149 .TP
1150 .BR EPERM " (since Linux 3.9)"
1151 .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d
1152 .B CLONE_NEWUSER
1153 was specified in
1154 .I flags
1155 and the caller is in a chroot environment
1156 .\" FIXME What is the rationale for this restriction?
1157 (i.e., the caller's root directory does not match the root directory
1158 of the mount namespace in which it resides).
1159 .TP
1160 .BR ERESTARTNOINTR " (since Linux 2.6.17)"
1161 .\" commit 4a2c7a7837da1b91468e50426066d988050e4d56
1162 System call was interrupted by a signal and will be restarted.
1163 (This can be seen only during a trace.)
1164 .TP
1165 .BR EUSERS " (Linux 3.11 to Linux 4.8)"
1166 .B CLONE_NEWUSER
1167 was specified in
1168 .IR flags ,
1169 and the limit on the number of nested user namespaces would be exceeded.
1170 See the discussion of the
1171 .BR ENOSPC
1172 error above.
1173 .\" .SH VERSIONS
1174 .\" There is no entry for
1175 .\" .BR clone ()
1176 .\" in libc5.
1177 .\" glibc2 provides
1178 .\" .BR clone ()
1179 .\" as described in this manual page.
1180 .SH CONFORMING TO
1181 .BR clone ()
1182 is Linux-specific and should not be used in programs
1183 intended to be portable.
1184 .SH NOTES
1185 The
1186 .BR kcmp (2)
1187 system call can be used to test whether two processes share various
1188 resources such as a file descriptor table,
1189 System V semaphore undo operations, or a virtual address space.
1190 .PP
1191 .PP
1192 Handlers registered using
1193 .BR pthread_atfork (3)
1194 are not executed during a call to
1195 .BR clone ().
1196 .PP
1197 In the Linux 2.4.x series,
1198 .B CLONE_THREAD
1199 generally does not make the parent of the new thread the same
1200 as the parent of the calling process.
1201 However, for kernel versions 2.4.7 to 2.4.18 the
1202 .B CLONE_THREAD
1203 flag implied the
1204 .B CLONE_PARENT
1205 flag (as in Linux 2.6.0 and later).
1206 .PP
1207 For a while there was
1208 .B CLONE_DETACHED
1209 (introduced in 2.5.32):
1210 parent wants no child-exit signal.
1211 In Linux 2.6.2, the need to give this flag together with
1212 .B CLONE_THREAD
1213 disappeared.
1214 This flag is still defined, but has no effect.
1215 .PP
1216 On i386,
1217 .BR clone ()
1218 should not be called through vsyscall, but directly through
1219 .IR "int $0x80" .
1220 .SH BUGS
1221 GNU C library versions 2.3.4 up to and including 2.24
1222 contained a wrapper function for
1223 .BR getpid (2)
1224 that performed caching of PIDs.
1225 This caching relied on support in the glibc wrapper for
1226 .BR clone (),
1227 but limitations in the implementation
1228 meant that the cache was not up to date in some circumstances.
1229 In particular,
1230 if a signal was delivered to the child immediately after the
1231 .BR clone ()
1232 call, then a call to
1233 .BR getpid (2)
1234 in a handler for the signal could return the PID
1235 of the calling process ("the parent"),
1236 if the clone wrapper had not yet had a chance to update the PID
1237 cache in the child.
1238 (This discussion ignores the case where the child was created using
1239 .BR CLONE_THREAD ,
1240 when
1241 .BR getpid (2)
1242 .I should
1243 return the same value in the child and in the process that called
1244 .BR clone (),
1245 since the caller and the child are in the same thread group.
1246 The stale-cache problem also does not occur if the
1247 .I flags
1248 argument includes
1249 .BR CLONE_VM .)
1250 To get the truth, it was sometimes necessary to use code such as the following:
1251 .PP
1252 .in +4n
1253 .EX
1254 #include <syscall.h>
1255
1256 pid_t mypid;
1257
1258 mypid = syscall(SYS_getpid);
1259 .EE
1260 .in
1261 .\" See also the following bug reports
1262 .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521
1263 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910
1264 .PP
1265 Because of the stale-cache problem, as well as other problems noted in
1266 .BR getpid (2),
1267 the PID caching feature was removed in glibc 2.25.
1268 .SH EXAMPLE
1269 The following program demonstrates the use of
1270 .BR clone ()
1271 to create a child process that executes in a separate UTS namespace.
1272 The child changes the hostname in its UTS namespace.
1273 Both parent and child then display the system hostname,
1274 making it possible to see that the hostname
1275 differs in the UTS namespaces of the parent and child.
1276 For an example of the use of this program, see
1277 .BR setns (2).
1278 .SS Program source
1279 .EX
1280 #define _GNU_SOURCE
1281 #include <sys/wait.h>
1282 #include <sys/utsname.h>
1283 #include <sched.h>
1284 #include <string.h>
1285 #include <stdio.h>
1286 #include <stdlib.h>
1287 #include <unistd.h>
1288
1289 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
1290 } while (0)
1291
1292 static int /* Start function for cloned child */
1293 childFunc(void *arg)
1294 {
1295 struct utsname uts;
1296
1297 /* Change hostname in UTS namespace of child */
1298
1299 if (sethostname(arg, strlen(arg)) == \-1)
1300 errExit("sethostname");
1301
1302 /* Retrieve and display hostname */
1303
1304 if (uname(&uts) == \-1)
1305 errExit("uname");
1306 printf("uts.nodename in child: %s\\n", uts.nodename);
1307
1308 /* Keep the namespace open for a while, by sleeping.
1309 This allows some experimentation\-\-for example, another
1310 process might join the namespace. */
1311
1312 sleep(200);
1313
1314 return 0; /* Child terminates now */
1315 }
1316
1317 #define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */
1318
1319 int
1320 main(int argc, char *argv[])
1321 {
1322 char *stack; /* Start of stack buffer */
1323 char *stackTop; /* End of stack buffer */
1324 pid_t pid;
1325 struct utsname uts;
1326
1327 if (argc < 2) {
1328 fprintf(stderr, "Usage: %s <child\-hostname>\\n", argv[0]);
1329 exit(EXIT_SUCCESS);
1330 }
1331
1332 /* Allocate stack for child */
1333
1334 stack = malloc(STACK_SIZE);
1335 if (stack == NULL)
1336 errExit("malloc");
1337 stackTop = stack + STACK_SIZE; /* Assume stack grows downward */
1338
1339 /* Create child that has its own UTS namespace;
1340 child commences execution in childFunc() */
1341
1342 pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]);
1343 if (pid == \-1)
1344 errExit("clone");
1345 printf("clone() returned %ld\\n", (long) pid);
1346
1347 /* Parent falls through to here */
1348
1349 sleep(1); /* Give child time to change its hostname */
1350
1351 /* Display hostname in parent\(aqs UTS namespace. This will be
1352 different from hostname in child\(aqs UTS namespace. */
1353
1354 if (uname(&uts) == \-1)
1355 errExit("uname");
1356 printf("uts.nodename in parent: %s\\n", uts.nodename);
1357
1358 if (waitpid(pid, NULL, 0) == \-1) /* Wait for child */
1359 errExit("waitpid");
1360 printf("child has terminated\\n");
1361
1362 exit(EXIT_SUCCESS);
1363 }
1364 .EE
1365 .SH SEE ALSO
1366 .BR fork (2),
1367 .BR futex (2),
1368 .BR getpid (2),
1369 .BR gettid (2),
1370 .BR kcmp (2),
1371 .BR set_thread_area (2),
1372 .BR set_tid_address (2),
1373 .BR setns (2),
1374 .BR tkill (2),
1375 .BR unshare (2),
1376 .BR wait (2),
1377 .BR capabilities (7),
1378 .BR namespaces (7),
1379 .BR pthreads (7)