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