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