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