OSDN Git Service

efc9fb53d245cdb0fbcad4f6a3bad1b2b1c5f722
[linuxjm/LDP_man-pages.git] / original / 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 2014-02-27 "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 .\" 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 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 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 hostname can be modified by
497 .BR setdomainname (2)
498 and
499 .BR sethostname (2),
500 respectively.
501 Changes made to the identifiers in a UTS namespace
502 are visible to all other processes in the same namespace,
503 but are not visible to processes in other UTS namespaces.
504
505 Use of this flag requires: a kernel configured with the
506 .B CONFIG_UTS_NS
507 option and that the process be privileged
508 .RB ( CAP_SYS_ADMIN ).
509 .TP
510 .BR CLONE_PARENT " (since Linux 2.3.12)"
511 If
512 .B CLONE_PARENT
513 is set, then the parent of the new child (as returned by
514 .BR getppid (2))
515 will be the same as that of the calling process.
516
517 If
518 .B CLONE_PARENT
519 is not set, then (as with
520 .BR fork (2))
521 the child's parent is the calling process.
522
523 Note that it is the parent process, as returned by
524 .BR getppid (2),
525 which is signaled when the child terminates, so that
526 if
527 .B CLONE_PARENT
528 is set, then the parent of the calling process, rather than the
529 calling process itself, will be signaled.
530 .TP
531 .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)"
532 Store child thread ID at location
533 .I ptid
534 in parent and child memory.
535 (In Linux 2.5.32-2.5.48 there was a flag
536 .B CLONE_SETTID
537 that did this.)
538 .TP
539 .BR CLONE_PID " (obsolete)"
540 If
541 .B CLONE_PID
542 is set, the child process is created with the same process ID as
543 the calling process.
544 This is good for hacking the system, but otherwise
545 of not much use.
546 Since 2.3.21 this flag can be
547 specified only by the system boot process (PID 0).
548 It disappeared in Linux 2.5.16.
549 .TP
550 .BR CLONE_PTRACE " (since Linux 2.2)"
551 If
552 .B CLONE_PTRACE
553 is specified, and the calling process is being traced,
554 then trace the child also (see
555 .BR ptrace (2)).
556 .TP
557 .BR CLONE_SETTLS " (since Linux 2.5.32)"
558 The
559 .I newtls
560 argument is the new TLS (Thread Local Storage) descriptor.
561 (See
562 .BR set_thread_area (2).)
563 .TP
564 .BR CLONE_SIGHAND " (since Linux 2.0)"
565 If
566 .B CLONE_SIGHAND
567 is set, the calling process and the child process share the same table of
568 signal handlers.
569 If the calling process or child process calls
570 .BR sigaction (2)
571 to change the behavior associated with a signal, the behavior is
572 changed in the other process as well.
573 However, the calling process and child
574 processes still have distinct signal masks and sets of pending
575 signals.
576 So, one of them may block or unblock some signals using
577 .BR sigprocmask (2)
578 without affecting the other process.
579
580 If
581 .B CLONE_SIGHAND
582 is not set, the child process inherits a copy of the signal handlers
583 of the calling process at the time
584 .BR clone ()
585 is called.
586 Calls to
587 .BR sigaction (2)
588 performed later by one of the processes have no effect on the other
589 process.
590
591 Since Linux 2.6.0-test6,
592 .I flags
593 must also include
594 .B CLONE_VM
595 if
596 .B CLONE_SIGHAND
597 is specified
598 .TP
599 .BR CLONE_STOPPED " (since Linux 2.6.0-test2)"
600 If
601 .B CLONE_STOPPED
602 is set, then the child is initially stopped (as though it was sent a
603 .B SIGSTOP
604 signal), and must be resumed by sending it a
605 .B SIGCONT
606 signal.
607
608 This flag was
609 .I deprecated
610 from Linux 2.6.25 onward,
611 and was
612 .I removed
613 altogether in Linux 2.6.38.
614 .\" glibc 2.8 removed this defn from bits/sched.h
615 .TP
616 .BR CLONE_SYSVSEM " (since Linux 2.5.10)"
617 If
618 .B CLONE_SYSVSEM
619 is set, then the child and the calling process share
620 a single list of System\ V semaphore undo values (see
621 .BR semop (2)).
622 If this flag is not set, then the child has a separate undo list,
623 which is initially empty.
624 .TP
625 .BR CLONE_THREAD " (since Linux 2.4.0-test8)"
626 If
627 .B CLONE_THREAD
628 is set, the child is placed in the same thread group as the calling process.
629 To make the remainder of the discussion of
630 .B CLONE_THREAD
631 more readable, the term "thread" is used to refer to the
632 processes within a thread group.
633
634 Thread groups were a feature added in Linux 2.4 to support the
635 POSIX threads notion of a set of threads that share a single PID.
636 Internally, this shared PID is the so-called
637 thread group identifier (TGID) for the thread group.
638 Since Linux 2.4, calls to
639 .BR getpid (2)
640 return the TGID of the caller.
641
642 The threads within a group can be distinguished by their (system-wide)
643 unique thread IDs (TID).
644 A new thread's TID is available as the function result
645 returned to the caller of
646 .BR clone (),
647 and a thread can obtain
648 its own TID using
649 .BR gettid (2).
650
651 When a call is made to
652 .BR clone ()
653 without specifying
654 .BR CLONE_THREAD ,
655 then the resulting thread is placed in a new thread group
656 whose TGID is the same as the thread's TID.
657 This thread is the
658 .I leader
659 of the new thread group.
660
661 A new thread created with
662 .B CLONE_THREAD
663 has the same parent process as the caller of
664 .BR clone ()
665 (i.e., like
666 .BR CLONE_PARENT ),
667 so that calls to
668 .BR getppid (2)
669 return the same value for all of the threads in a thread group.
670 When a
671 .B CLONE_THREAD
672 thread terminates, the thread that created it using
673 .BR clone ()
674 is not sent a
675 .B SIGCHLD
676 (or other termination) signal;
677 nor can the status of such a thread be obtained
678 using
679 .BR wait (2).
680 (The thread is said to be
681 .IR detached .)
682
683 After all of the threads in a thread group terminate
684 the parent process of the thread group is sent a
685 .B SIGCHLD
686 (or other termination) signal.
687
688 If any of the threads in a thread group performs an
689 .BR execve (2),
690 then all threads other than the thread group leader are terminated,
691 and the new program is executed in the thread group leader.
692
693 If one of the threads in a thread group creates a child using
694 .BR fork (2),
695 then any thread in the group can
696 .BR wait (2)
697 for that child.
698
699 Since Linux 2.5.35,
700 .I flags
701 must also include
702 .B CLONE_SIGHAND
703 if
704 .B CLONE_THREAD
705 is specified
706 (and note that, since Linux 2.6.0-test6,
707 .BR CLONE_SIGHAND
708 also requires
709 .BR CLONE_VM
710 to be included).
711
712 Signals may be sent to a thread group as a whole (i.e., a TGID) using
713 .BR kill (2),
714 or to a specific thread (i.e., TID) using
715 .BR tgkill (2).
716
717 Signal dispositions and actions are process-wide:
718 if an unhandled signal is delivered to a thread, then
719 it will affect (terminate, stop, continue, be ignored in)
720 all members of the thread group.
721
722 Each thread has its own signal mask, as set by
723 .BR sigprocmask (2),
724 but signals can be pending either: for the whole process
725 (i.e., deliverable to any member of the thread group),
726 when sent with
727 .BR kill (2);
728 or for an individual thread, when sent with
729 .BR tgkill (2).
730 A call to
731 .BR sigpending (2)
732 returns a signal set that is the union of the signals pending for the
733 whole process and the signals that are pending for the calling thread.
734
735 If
736 .BR kill (2)
737 is used to send a signal to a thread group,
738 and the thread group has installed a handler for the signal, then
739 the handler will be invoked in exactly one, arbitrarily selected
740 member of the thread group that has not blocked the signal.
741 If multiple threads in a group are waiting to accept the same signal using
742 .BR sigwaitinfo (2),
743 the kernel will arbitrarily select one of these threads
744 to receive a signal sent using
745 .BR kill (2).
746 .TP
747 .BR CLONE_UNTRACED " (since Linux 2.5.46)"
748 If
749 .B CLONE_UNTRACED
750 is specified, then a tracing process cannot force
751 .B CLONE_PTRACE
752 on this child process.
753 .TP
754 .BR CLONE_VFORK " (since Linux 2.2)"
755 If
756 .B CLONE_VFORK
757 is set, the execution of the calling process is suspended
758 until the child releases its virtual memory
759 resources via a call to
760 .BR execve (2)
761 or
762 .BR _exit (2)
763 (as with
764 .BR vfork (2)).
765
766 If
767 .B CLONE_VFORK
768 is not set, then both the calling process and the child are schedulable
769 after the call, and an application should not rely on execution occurring
770 in any particular order.
771 .TP
772 .BR CLONE_VM " (since Linux 2.0)"
773 If
774 .B CLONE_VM
775 is set, the calling process and the child process run in the same memory
776 space.
777 In particular, memory writes performed by the calling process
778 or by the child process are also visible in the other process.
779 Moreover, any memory mapping or unmapping performed with
780 .BR mmap (2)
781 or
782 .BR munmap (2)
783 by the child or calling process also affects the other process.
784
785 If
786 .B CLONE_VM
787 is not set, the child process runs in a separate copy of the memory
788 space of the calling process at the time of
789 .BR clone ().
790 Memory writes or file mappings/unmappings performed by one of the
791 processes do not affect the other, as with
792 .BR fork (2).
793 .SS The raw system call interface
794 The raw
795 .BR clone ()
796 system call corresponds more closely to
797 .BR fork (2)
798 in that execution in the child continues from the point of the
799 call.
800 As such, the
801 .I fn
802 and
803 .I arg
804 arguments of the
805 .BR clone ()
806 wrapper function are omitted.
807 Furthermore, the argument order changes.
808 The raw system call interface on x86 and many other architectures is roughly:
809 .in +4
810 .nf
811
812 .BI "long clone(unsigned long " flags ", void *" child_stack ,
813 .BI "           void *" ptid ", void *" ctid ,
814 .BI "           struct pt_regs *" regs );
815
816 .fi
817 .in
818 Another difference for the raw system call is that the
819 .I child_stack
820 argument may be zero, in which case copy-on-write semantics ensure that the
821 child gets separate copies of stack pages when either process modifies
822 the stack.
823 In this case, for correct operation, the
824 .B CLONE_VM
825 option should not be specified.
826
827 For some architectures, the order of the arguments for the system call
828 differs from that shown above.
829 On the score, microblaze, ARM, ARM 64, PA-RISC, arc, Power PC, xtensa,
830 and MIPS architectures,
831 the order of the fourth and fifth arguments is reversed.
832 On the cris and s390 architectures,
833 the order of the first and second arguments is reversed.
834 .SS blackfin, m68k, and sparc
835 The argument-passing conventions on
836 blackfin, m68k, and sparc are different from the descriptions above.
837 For details, see the kernel (and glibc) source.
838 .SS ia64
839 On ia64, a different interface is used:
840 .nf
841
842 .BI "int __clone2(int (*" "fn" ")(void *), "
843 .BI "             void *" child_stack_base ", size_t " stack_size ,
844 .BI "             int " flags ", void *" "arg" ", ... "
845 .BI "          /* pid_t *" ptid ", struct user_desc *" tls \
846 ", pid_t *" ctid " */ );"
847 .fi
848 .PP
849 The prototype shown above is for the glibc wrapper function;
850 the raw system call interface has no
851 .I fn
852 or
853 .I arg
854 argument, and changes the order of the arguments so that
855 .I flags
856 is the first argument, and
857 .I tls
858 is the last argument.
859 .PP
860 .BR __clone2 ()
861 operates in the same way as
862 .BR clone (),
863 except that
864 .I child_stack_base
865 points to the lowest address of the child's stack area,
866 and
867 .I stack_size
868 specifies the size of the stack pointed to by
869 .IR child_stack_base .
870 .SS Linux 2.4 and earlier
871 In Linux 2.4 and earlier,
872 .BR clone ()
873 does not take arguments
874 .IR ptid ,
875 .IR tls ,
876 and
877 .IR ctid .
878 .SH RETURN VALUE
879 .\" gettid(2) returns current->pid;
880 .\" getpid(2) returns current->tgid;
881 On success, the thread ID of the child process is returned
882 in the caller's thread of execution.
883 On failure, \-1 is returned
884 in the caller's context, no child process will be created, and
885 .I errno
886 will be set appropriately.
887 .SH ERRORS
888 .TP
889 .B EAGAIN
890 Too many processes are already running.
891 .TP
892 .B EINVAL
893 .B CLONE_SIGHAND
894 was specified, but
895 .B CLONE_VM
896 was not.
897 (Since Linux 2.6.0-test6.)
898 .TP
899 .B EINVAL
900 .B CLONE_THREAD
901 was specified, but
902 .B CLONE_SIGHAND
903 was not.
904 (Since Linux 2.5.35.)
905 .\" .TP
906 .\" .B EINVAL
907 .\" Precisely one of
908 .\" .B CLONE_DETACHED
909 .\" and
910 .\" .B CLONE_THREAD
911 .\" was specified.
912 .\" (Since Linux 2.6.0-test6.)
913 .TP
914 .B EINVAL
915 Both
916 .B CLONE_FS
917 and
918 .B CLONE_NEWNS
919 were specified in
920 .IR flags .
921 .TP
922 .B EINVAL
923 Both
924 .B CLONE_NEWIPC
925 and
926 .B CLONE_SYSVSEM
927 were specified in
928 .IR flags .
929 .TP
930 .B EINVAL
931 Both
932 .BR CLONE_NEWPID
933 and
934 .BR CLONE_THREAD
935 were specified in
936 .IR flags .
937 .TP
938 .B EINVAL
939 Returned by
940 .BR clone ()
941 when a zero value is specified for
942 .IR child_stack .
943 .TP
944 .B EINVAL
945 .BR CLONE_NEWIPC
946 was specified in
947 .IR flags ,
948 but the kernel was not configured with the
949 .B CONFIG_SYSVIPC
950 and
951 .BR CONFIG_IPC_NS
952 options.
953 .TP
954 .B EINVAL
955 .BR CLONE_NEWNET
956 was specified in
957 .IR flags ,
958 but the kernel was not configured with the
959 .B CONFIG_NET_NS
960 option.
961 .TP
962 .B EINVAL
963 .BR CLONE_NEWPID
964 was specified in
965 .IR flags ,
966 but the kernel was not configured with the
967 .B CONFIG_PID_NS
968 option.
969 .TP
970 .B EINVAL
971 .BR CLONE_NEWUTS
972 was specified in
973 .IR flags ,
974 but the kernel was not configured with the
975 .B CONFIG_UTS
976 option.
977 .TP
978 .B ENOMEM
979 Cannot allocate sufficient memory to allocate a task structure for the
980 child, or to copy those parts of the caller's context that need to be
981 copied.
982 .TP
983 .B EPERM
984 .BR CLONE_NEWIPC ,
985 .BR CLONE_NEWNET ,
986 .BR CLONE_NEWNS ,
987 .BR CLONE_NEWPID ,
988 or
989 .BR CLONE_NEWUTS
990 was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP).
991 .TP
992 .B EPERM
993 .B CLONE_PID
994 was specified by a process other than process 0.
995 .SH VERSIONS
996 There is no entry for
997 .BR clone ()
998 in libc5.
999 glibc2 provides
1000 .BR clone ()
1001 as described in this manual page.
1002 .SH CONFORMING TO
1003 .BR clone ()
1004 is Linux-specific and should not be used in programs
1005 intended to be portable.
1006 .SH NOTES
1007 In the kernel 2.4.x series,
1008 .B CLONE_THREAD
1009 generally does not make the parent of the new thread the same
1010 as the parent of the calling process.
1011 However, for kernel versions 2.4.7 to 2.4.18 the
1012 .B CLONE_THREAD
1013 flag implied the
1014 .B CLONE_PARENT
1015 flag (as in kernel 2.6).
1016
1017 For a while there was
1018 .B CLONE_DETACHED
1019 (introduced in 2.5.32):
1020 parent wants no child-exit signal.
1021 In 2.6.2 the need to give this
1022 together with
1023 .B CLONE_THREAD
1024 disappeared.
1025 This flag is still defined, but has no effect.
1026
1027 On i386,
1028 .BR clone ()
1029 should not be called through vsyscall, but directly through
1030 .IR "int $0x80" .
1031 .SH BUGS
1032 Versions of the GNU C library that include the NPTL threading library
1033 contain a wrapper function for
1034 .BR getpid (2)
1035 that performs caching of PIDs.
1036 This caching relies on support in the glibc wrapper for
1037 .BR clone (),
1038 but as currently implemented,
1039 the cache may not be up to date in some circumstances.
1040 In particular,
1041 if a signal is delivered to the child immediately after the
1042 .BR clone ()
1043 call, then a call to
1044 .BR getpid (2)
1045 in a handler for the signal may return the PID
1046 of the calling process ("the parent"),
1047 if the clone wrapper has not yet had a chance to update the PID
1048 cache in the child.
1049 (This discussion ignores the case where the child was created using
1050 .BR CLONE_THREAD ,
1051 when
1052 .BR getpid (2)
1053 .I should
1054 return the same value in the child and in the process that called
1055 .BR clone (),
1056 since the caller and the child are in the same thread group.
1057 The stale-cache problem also does not occur if the
1058 .I flags
1059 argument includes
1060 .BR CLONE_VM .)
1061 To get the truth, it may be necessary to use code such as the following:
1062 .nf
1063
1064     #include <syscall.h>
1065
1066     pid_t mypid;
1067
1068     mypid = syscall(SYS_getpid);
1069 .fi
1070 .\" See also the following bug reports
1071 .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521
1072 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910
1073 .SH EXAMPLE
1074 The following program demonstrates the use of
1075 .BR clone ()
1076 to create a child process that executes in a separate UTS namespace.
1077 The child changes the hostname in its UTS namespace.
1078 Both parent and child then display the system hostname,
1079 making it possible to see that the hostname
1080 differs in the UTS namespaces of the parent and child.
1081 For an example of the use of this program, see
1082 .BR setns (2).
1083 .SS Program source
1084 .nf
1085 #define _GNU_SOURCE
1086 #include <sys/wait.h>
1087 #include <sys/utsname.h>
1088 #include <sched.h>
1089 #include <string.h>
1090 #include <stdio.h>
1091 #include <stdlib.h>
1092 #include <unistd.h>
1093
1094 #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \\
1095                         } while (0)
1096
1097 static int              /* Start function for cloned child */
1098 childFunc(void *arg)
1099 {
1100     struct utsname uts;
1101
1102     /* Change hostname in UTS namespace of child */
1103
1104     if (sethostname(arg, strlen(arg)) == \-1)
1105         errExit("sethostname");
1106
1107     /* Retrieve and display hostname */
1108
1109     if (uname(&uts) == \-1)
1110         errExit("uname");
1111     printf("uts.nodename in child:  %s\\n", uts.nodename);
1112
1113     /* Keep the namespace open for a while, by sleeping.
1114        This allows some experimentation\-\-for example, another
1115        process might join the namespace. */
1116
1117     sleep(200);
1118
1119     return 0;           /* Child terminates now */
1120 }
1121
1122 #define STACK_SIZE (1024 * 1024)    /* Stack size for cloned child */
1123
1124 int
1125 main(int argc, char *argv[])
1126 {
1127     char *stack;                    /* Start of stack buffer */
1128     char *stackTop;                 /* End of stack buffer */
1129     pid_t pid;
1130     struct utsname uts;
1131
1132     if (argc < 2) {
1133         fprintf(stderr, "Usage: %s <child\-hostname>\\n", argv[0]);
1134         exit(EXIT_SUCCESS);
1135     }
1136
1137     /* Allocate stack for child */
1138
1139     stack = malloc(STACK_SIZE);
1140     if (stack == NULL)
1141         errExit("malloc");
1142     stackTop = stack + STACK_SIZE;  /* Assume stack grows downward */
1143
1144     /* Create child that has its own UTS namespace;
1145        child commences execution in childFunc() */
1146
1147     pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]);
1148     if (pid == \-1)
1149         errExit("clone");
1150     printf("clone() returned %ld\\n", (long) pid);
1151
1152     /* Parent falls through to here */
1153
1154     sleep(1);           /* Give child time to change its hostname */
1155
1156     /* Display hostname in parent\(aqs UTS namespace. This will be
1157        different from hostname in child\(aqs UTS namespace. */
1158
1159     if (uname(&uts) == \-1)
1160         errExit("uname");
1161     printf("uts.nodename in parent: %s\\n", uts.nodename);
1162
1163     if (waitpid(pid, NULL, 0) == \-1)    /* Wait for child */
1164         errExit("waitpid");
1165     printf("child has terminated\\n");
1166
1167     exit(EXIT_SUCCESS);
1168 }
1169 .fi
1170 .SH SEE ALSO
1171 .BR fork (2),
1172 .BR futex (2),
1173 .BR getpid (2),
1174 .BR gettid (2),
1175 .BR kcmp (2),
1176 .BR set_thread_area (2),
1177 .BR set_tid_address (2),
1178 .BR setns (2),
1179 .BR tkill (2),
1180 .BR unshare (2),
1181 .BR wait (2),
1182 .BR capabilities (7),
1183 .BR pthreads (7)
1184 .SH COLOPHON
1185 This page is part of release 3.67 of the Linux
1186 .I man-pages
1187 project.
1188 A description of the project,
1189 information about reporting bugs,
1190 and the latest version of this page,
1191 can be found at
1192 \%http://www.kernel.org/doc/man\-pages/.