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