]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/clone.2
3977689d0a6f318221c2cf2965c691e64d119d28
[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-07-23 "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 One 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 the child thread ID at the 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 the child thread ID at the location
191 .I ctid
192 in the child's 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 the child thread ID at the location
522 .I ptid
523 in the parent's 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 Since then, the kernel silently ignores it without error.
539 .TP
540 .BR CLONE_PTRACE " (since Linux 2.2)"
541 If
542 .B CLONE_PTRACE
543 is specified, and the calling process is being traced,
544 then trace the child also (see
545 .BR ptrace (2)).
546 .TP
547 .BR CLONE_SETTLS " (since Linux 2.5.32)"
548 The
549 .I newtls
550 argument is the new TLS (Thread Local Storage) descriptor.
551 (See
552 .BR set_thread_area (2).)
553 .TP
554 .BR CLONE_SIGHAND " (since Linux 2.0)"
555 If
556 .B CLONE_SIGHAND
557 is set, the calling process and the child process share the same table of
558 signal handlers.
559 If the calling process or child process calls
560 .BR sigaction (2)
561 to change the behavior associated with a signal, the behavior is
562 changed in the other process as well.
563 However, the calling process and child
564 processes still have distinct signal masks and sets of pending
565 signals.
566 So, one of them may block or unblock some signals using
567 .BR sigprocmask (2)
568 without affecting the other process.
569
570 If
571 .B CLONE_SIGHAND
572 is not set, the child process inherits a copy of the signal handlers
573 of the calling process at the time
574 .BR clone ()
575 is called.
576 Calls to
577 .BR sigaction (2)
578 performed later by one of the processes have no effect on the other
579 process.
580
581 Since Linux 2.6.0-test6,
582 .I flags
583 must also include
584 .B CLONE_VM
585 if
586 .B CLONE_SIGHAND
587 is specified
588 .TP
589 .BR CLONE_STOPPED " (since Linux 2.6.0-test2)"
590 If
591 .B CLONE_STOPPED
592 is set, then the child is initially stopped (as though it was sent a
593 .B SIGSTOP
594 signal), and must be resumed by sending it a
595 .B SIGCONT
596 signal.
597
598 This flag was
599 .I deprecated
600 from Linux 2.6.25 onward,
601 and was
602 .I removed
603 altogether in Linux 2.6.38.
604 Since then, the kernel silently ignores it without error.
605 .\" glibc 2.8 removed this defn from bits/sched.h
606 .TP
607 .BR CLONE_SYSVSEM " (since Linux 2.5.10)"
608 If
609 .B CLONE_SYSVSEM
610 is set, then the child and the calling process share
611 a single list of System V semaphore adjustment
612 .RI ( semadj )
613 values (see
614 .BR semop (2)).
615 In this case, the shared list accumulates
616 .I semadj
617 values across all processes sharing the list,
618 and semaphore adjustments are performed only when the last process
619 that is sharing the list terminates (or ceases sharing the list using
620 .BR unshare (2)).
621 If this flag is not set, then the child has a separate
622 .I semadj
623 list that 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 C library/kernel differences
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; see
891 .BR fork (2).
892 .TP
893 .B EINVAL
894 .B CLONE_SIGHAND
895 was specified, but
896 .B CLONE_VM
897 was not.
898 (Since Linux 2.6.0-test6.)
899 .TP
900 .B EINVAL
901 .B CLONE_THREAD
902 was specified, but
903 .B CLONE_SIGHAND
904 was not.
905 (Since Linux 2.5.35.)
906 .\" .TP
907 .\" .B EINVAL
908 .\" Precisely one of
909 .\" .B CLONE_DETACHED
910 .\" and
911 .\" .B CLONE_THREAD
912 .\" was specified.
913 .\" (Since Linux 2.6.0-test6.)
914 .TP
915 .B EINVAL
916 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
917 Both
918 .B CLONE_FS
919 and
920 .B CLONE_NEWNS
921 were specified in
922 .IR flags .
923 .TP
924 .BR EINVAL " (since Linux 3.9)"
925 Both
926 .B CLONE_NEWUSER
927 and
928 .B CLONE_FS
929 were specified in
930 .IR flags .
931 .TP
932 .B EINVAL
933 Both
934 .B CLONE_NEWIPC
935 and
936 .B CLONE_SYSVSEM
937 were specified in
938 .IR flags .
939 .TP
940 .B EINVAL
941 One (or both) of
942 .BR CLONE_NEWPID
943 or
944 .BR CLONE_NEWUSER
945 and one (or both) of
946 .BR CLONE_THREAD
947 or
948 .BR CLONE_PARENT
949 were specified in
950 .IR flags .
951 .TP
952 .B EINVAL
953 Returned by
954 .BR clone ()
955 when a zero value is specified for
956 .IR child_stack .
957 .TP
958 .B EINVAL
959 .BR CLONE_NEWIPC
960 was specified in
961 .IR flags ,
962 but the kernel was not configured with the
963 .B CONFIG_SYSVIPC
964 and
965 .BR CONFIG_IPC_NS
966 options.
967 .TP
968 .B EINVAL
969 .BR CLONE_NEWNET
970 was specified in
971 .IR flags ,
972 but the kernel was not configured with the
973 .B CONFIG_NET_NS
974 option.
975 .TP
976 .B EINVAL
977 .BR CLONE_NEWPID
978 was specified in
979 .IR flags ,
980 but the kernel was not configured with the
981 .B CONFIG_PID_NS
982 option.
983 .TP
984 .B EINVAL
985 .BR CLONE_NEWUTS
986 was specified in
987 .IR flags ,
988 but the kernel was not configured with the
989 .B CONFIG_UTS
990 option.
991 .TP
992 .B ENOMEM
993 Cannot allocate sufficient memory to allocate a task structure for the
994 child, or to copy those parts of the caller's context that need to be
995 copied.
996 .TP
997 .B EPERM
998 .BR CLONE_NEWIPC ,
999 .BR CLONE_NEWNET ,
1000 .BR CLONE_NEWNS ,
1001 .BR CLONE_NEWPID ,
1002 or
1003 .BR CLONE_NEWUTS
1004 was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP).
1005 .TP
1006 .B EPERM
1007 .B CLONE_PID
1008 was specified by a process other than process 0.
1009 .TP
1010 .B EPERM
1011 .BR CLONE_NEWUSER
1012 was specified in
1013 .IR flags ,
1014 but either the effective user ID or the effective group ID of the caller
1015 does not have a mapping in the parent namespace (see
1016 .BR user_namespaces (7)).
1017 .TP
1018 .BR EPERM " (since Linux 3.9)"
1019 .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d
1020 .B CLONE_NEWUSER
1021 was specified in
1022 .I flags
1023 and the caller is in a chroot environment
1024 .\" FIXME What is the rationale for this restriction?
1025 (i.e., the caller's root directory does not match the root directory
1026 of the mount namespace in which it resides).
1027 .TP
1028 .BR EUSERS " (since Linux 3.11)"
1029 .B CLONE_NEWUSER
1030 was specified in
1031 .IR flags ,
1032 and the call would cause the limit on the number of
1033 nested user namespaces to be exceeded.
1034 See
1035 .BR user_namespaces (7).
1036 .SH VERSIONS
1037 There is no entry for
1038 .BR clone ()
1039 in libc5.
1040 glibc2 provides
1041 .BR clone ()
1042 as described in this manual page.
1043 .SH CONFORMING TO
1044 .BR clone ()
1045 is Linux-specific and should not be used in programs
1046 intended to be portable.
1047 .SH NOTES
1048 In the kernel 2.4.x series,
1049 .B CLONE_THREAD
1050 generally does not make the parent of the new thread the same
1051 as the parent of the calling process.
1052 However, for kernel versions 2.4.7 to 2.4.18 the
1053 .B CLONE_THREAD
1054 flag implied the
1055 .B CLONE_PARENT
1056 flag (as in kernel 2.6).
1057
1058 For a while there was
1059 .B CLONE_DETACHED
1060 (introduced in 2.5.32):
1061 parent wants no child-exit signal.
1062 In 2.6.2 the need to give this
1063 together with
1064 .B CLONE_THREAD
1065 disappeared.
1066 This flag is still defined, but has no effect.
1067
1068 On i386,
1069 .BR clone ()
1070 should not be called through vsyscall, but directly through
1071 .IR "int $0x80" .
1072 .SH BUGS
1073 Versions of the GNU C library that include the NPTL threading library
1074 contain a wrapper function for
1075 .BR getpid (2)
1076 that performs caching of PIDs.
1077 This caching relies on support in the glibc wrapper for
1078 .BR clone (),
1079 but as currently implemented,
1080 the cache may not be up to date in some circumstances.
1081 In particular,
1082 if a signal is delivered to the child immediately after the
1083 .BR clone ()
1084 call, then a call to
1085 .BR getpid (2)
1086 in a handler for the signal may return the PID
1087 of the calling process ("the parent"),
1088 if the clone wrapper has not yet had a chance to update the PID
1089 cache in the child.
1090 (This discussion ignores the case where the child was created using
1091 .BR CLONE_THREAD ,
1092 when
1093 .BR getpid (2)
1094 .I should
1095 return the same value in the child and in the process that called
1096 .BR clone (),
1097 since the caller and the child are in the same thread group.
1098 The stale-cache problem also does not occur if the
1099 .I flags
1100 argument includes
1101 .BR CLONE_VM .)
1102 To get the truth, it may be necessary to use code such as the following:
1103 .nf
1104
1105 #include <syscall.h>
1106
1107 pid_t mypid;
1108
1109 mypid = syscall(SYS_getpid);
1110 .fi
1111 .\" See also the following bug reports
1112 .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521
1113 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910
1114 .SH EXAMPLE
1115 The following program demonstrates the use of
1116 .BR clone ()
1117 to create a child process that executes in a separate UTS namespace.
1118 The child changes the hostname in its UTS namespace.
1119 Both parent and child then display the system hostname,
1120 making it possible to see that the hostname
1121 differs in the UTS namespaces of the parent and child.
1122 For an example of the use of this program, see
1123 .BR setns (2).
1124 .SS Program source
1125 .nf
1126 #define _GNU_SOURCE
1127 #include <sys/wait.h>
1128 #include <sys/utsname.h>
1129 #include <sched.h>
1130 #include <string.h>
1131 #include <stdio.h>
1132 #include <stdlib.h>
1133 #include <unistd.h>
1134
1135 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
1136 } while (0)
1137
1138 static int /* Start function for cloned child */
1139 childFunc(void *arg)
1140 {
1141 struct utsname uts;
1142
1143 /* Change hostname in UTS namespace of child */
1144
1145 if (sethostname(arg, strlen(arg)) == \-1)
1146 errExit("sethostname");
1147
1148 /* Retrieve and display hostname */
1149
1150 if (uname(&uts) == \-1)
1151 errExit("uname");
1152 printf("uts.nodename in child: %s\\n", uts.nodename);
1153
1154 /* Keep the namespace open for a while, by sleeping.
1155 This allows some experimentation\-\-for example, another
1156 process might join the namespace. */
1157
1158 sleep(200);
1159
1160 return 0; /* Child terminates now */
1161 }
1162
1163 #define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */
1164
1165 int
1166 main(int argc, char *argv[])
1167 {
1168 char *stack; /* Start of stack buffer */
1169 char *stackTop; /* End of stack buffer */
1170 pid_t pid;
1171 struct utsname uts;
1172
1173 if (argc < 2) {
1174 fprintf(stderr, "Usage: %s <child\-hostname>\\n", argv[0]);
1175 exit(EXIT_SUCCESS);
1176 }
1177
1178 /* Allocate stack for child */
1179
1180 stack = malloc(STACK_SIZE);
1181 if (stack == NULL)
1182 errExit("malloc");
1183 stackTop = stack + STACK_SIZE; /* Assume stack grows downward */
1184
1185 /* Create child that has its own UTS namespace;
1186 child commences execution in childFunc() */
1187
1188 pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]);
1189 if (pid == \-1)
1190 errExit("clone");
1191 printf("clone() returned %ld\\n", (long) pid);
1192
1193 /* Parent falls through to here */
1194
1195 sleep(1); /* Give child time to change its hostname */
1196
1197 /* Display hostname in parent\(aqs UTS namespace. This will be
1198 different from hostname in child\(aqs UTS namespace. */
1199
1200 if (uname(&uts) == \-1)
1201 errExit("uname");
1202 printf("uts.nodename in parent: %s\\n", uts.nodename);
1203
1204 if (waitpid(pid, NULL, 0) == \-1) /* Wait for child */
1205 errExit("waitpid");
1206 printf("child has terminated\\n");
1207
1208 exit(EXIT_SUCCESS);
1209 }
1210 .fi
1211 .SH SEE ALSO
1212 .BR fork (2),
1213 .BR futex (2),
1214 .BR getpid (2),
1215 .BR gettid (2),
1216 .BR kcmp (2),
1217 .BR set_thread_area (2),
1218 .BR set_tid_address (2),
1219 .BR setns (2),
1220 .BR tkill (2),
1221 .BR unshare (2),
1222 .BR wait (2),
1223 .BR capabilities (7),
1224 .BR namespaces (7),
1225 .BR pthreads (7)