]> git.ipfire.org Git - thirdparty/man-pages.git/blame_incremental - man2/clone.2
exec.3: Minor wording fix in subsection title
[thirdparty/man-pages.git] / man2 / clone.2
... / ...
CommitLineData
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 2019-03-06 "Linux" "Linux Programmer's Manual"
43.SH NAME
44clone, __clone2 \- create a child process
45.SH SYNOPSIS
46.nf
47/* Prototype for the glibc wrapper function */
48.PP
49.B #define _GNU_SOURCE
50.B #include <sched.h>
51.PP
52.BI "int clone(int (*" "fn" ")(void *), void *" child_stack ,
53.BI " int " flags ", void *" "arg" ", ... "
54.BI " /* pid_t *" ptid ", void *" newtls \
55", pid_t *" ctid " */ );"
56.PP
57/* For the prototype of the raw system call, see NOTES */
58.fi
59.SH DESCRIPTION
60.BR clone ()
61creates a new process, in a manner similar to
62.BR fork (2).
63.PP
64This page describes both the glibc
65.BR clone ()
66wrapper function and the underlying system call on which it is based.
67The main text describes the wrapper function;
68the differences for the raw system call
69are described toward the end of this page.
70.PP
71Unlike
72.BR fork (2),
73.BR clone ()
74allows the child process to share parts of its execution context with
75the calling process, such as the virtual address space, the table of file
76descriptors, and the table of signal handlers.
77(Note that on this manual
78page, "calling process" normally corresponds to "parent process".
79But see the description of
80.B CLONE_PARENT
81below.)
82.PP
83One use of
84.BR clone ()
85is to implement threads: multiple flows of control in a program that
86run concurrently in a shared address space.
87.PP
88When the child process is created with
89.BR clone (),
90it commences execution by calling the function pointed to by the argument
91.IR fn .
92(This differs from
93.BR fork (2),
94where execution continues in the child from the point
95of the
96.BR fork (2)
97call.)
98The
99.I arg
100argument is passed as the argument of the function
101.IR fn .
102.PP
103When the
104.IR fn ( arg )
105function returns, the child process terminates.
106The integer returned by
107.I fn
108is the exit status for the child process.
109The child process may also terminate explicitly by calling
110.BR exit (2)
111or after receiving a fatal signal.
112.PP
113The
114.I child_stack
115argument specifies the location of the stack used by the child process.
116Since the child and calling process may share memory,
117it is not possible for the child process to execute in the
118same stack as the calling process.
119The calling process must therefore
120set up memory space for the child stack and pass a pointer to this
121space to
122.BR clone ().
123Stacks grow downward on all processors that run Linux
124(except the HP PA processors), so
125.I child_stack
126usually points to the topmost address of the memory space set up for
127the child stack.
128.PP
129The low byte of
130.I flags
131contains the number of the
132.I "termination signal"
133sent to the parent when the child dies.
134If this signal is specified as anything other than
135.BR SIGCHLD ,
136then the parent process must specify the
137.B __WALL
138or
139.B __WCLONE
140options when waiting for the child with
141.BR wait (2).
142If no signal is specified, then the parent process is not signaled
143when the child terminates.
144.PP
145.I flags
146may also be bitwise-ORed with zero or more of the following constants,
147in order to specify what is shared between the calling process
148and the child process:
149.TP
150.BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)"
151Clear (zero) the child thread ID at the location
152.I ctid
153in child memory when the child exits, and do a wakeup on the futex
154at that address.
155The address involved may be changed by the
156.BR set_tid_address (2)
157system call.
158This is used by threading libraries.
159.TP
160.BR CLONE_CHILD_SETTID " (since Linux 2.5.49)"
161Store the child thread ID at the location
162.I ctid
163in the child's memory.
164The store operation completes before
165.BR clone ()
166returns control to user space.
167.TP
168.BR CLONE_FILES " (since Linux 2.0)"
169If
170.B CLONE_FILES
171is set, the calling process and the child process share the same file
172descriptor table.
173Any file descriptor created by the calling process or by the child
174process is also valid in the other process.
175Similarly, if one of the processes closes a file descriptor,
176or changes its associated flags (using the
177.BR fcntl (2)
178.B F_SETFD
179operation), the other process is also affected.
180If a process sharing a file descriptor table calls
181.BR execve (2),
182its file descriptor table is duplicated (unshared).
183.IP
184If
185.B CLONE_FILES
186is not set, the child process inherits a copy of all file descriptors
187opened in the calling process at the time of
188.BR clone ().
189Subsequent operations that open or close file descriptors,
190or change file descriptor flags,
191performed by either the calling
192process or the child process do not affect the other process.
193Note, however,
194that the duplicated file descriptors in the child refer to the same
195open file descriptions as the corresponding file descriptors
196in the calling process,
197and thus share file offsets and file status flags (see
198.BR open (2)).
199.TP
200.BR CLONE_FS " (since Linux 2.0)"
201If
202.B CLONE_FS
203is set, the caller and the child process share the same filesystem
204information.
205This includes the root of the filesystem, the current
206working directory, and the umask.
207Any call to
208.BR chroot (2),
209.BR chdir (2),
210or
211.BR umask (2)
212performed by the calling process or the child process also affects the
213other process.
214.IP
215If
216.B CLONE_FS
217is not set, the child process works on a copy of the filesystem
218information of the calling process at the time of the
219.BR clone ()
220call.
221Calls to
222.BR chroot (2),
223.BR chdir (2),
224or
225.BR umask (2)
226performed later by one of the processes do not affect the other process.
227.TP
228.BR CLONE_IO " (since Linux 2.6.25)"
229If
230.B CLONE_IO
231is set, then the new process shares an I/O context with
232the calling process.
233If this flag is not set, then (as with
234.BR fork (2))
235the new process has its own I/O context.
236.IP
237.\" The following based on text from Jens Axboe
238The I/O context is the I/O scope of the disk scheduler (i.e.,
239what the I/O scheduler uses to model scheduling of a process's I/O).
240If processes share the same I/O context,
241they are treated as one by the I/O scheduler.
242As a consequence, they get to share disk time.
243For some I/O schedulers,
244.\" the anticipatory and CFQ scheduler
245if two processes share an I/O context,
246they will be allowed to interleave their disk access.
247If several threads are doing I/O on behalf of the same process
248.RB ( aio_read (3),
249for instance), they should employ
250.BR CLONE_IO
251to get better I/O performance.
252.\" with CFQ and AS.
253.IP
254If the kernel is not configured with the
255.B CONFIG_BLOCK
256option, this flag is a no-op.
257.TP
258.BR CLONE_NEWCGROUP " (since Linux 4.6)"
259Create the process in a new cgroup namespace.
260If this flag is not set, then (as with
261.BR fork (2))
262the process is created in the same cgroup namespaces as the calling process.
263This flag is intended for the implementation of containers.
264.IP
265For further information on cgroup namespaces, see
266.BR cgroup_namespaces (7).
267.IP
268Only a privileged process
269.RB ( CAP_SYS_ADMIN )
270can employ
271.BR CLONE_NEWCGROUP .
272.\"
273.TP
274.BR CLONE_NEWIPC " (since Linux 2.6.19)"
275If
276.B CLONE_NEWIPC
277is set, then create the process in a new IPC namespace.
278If this flag is not set, then (as with
279.BR fork (2)),
280the process is created in the same IPC namespace as
281the calling process.
282This flag is intended for the implementation of containers.
283.IP
284An IPC namespace provides an isolated view of System\ V IPC objects (see
285.BR svipc (7))
286and (since Linux 2.6.30)
287.\" commit 7eafd7c74c3f2e67c27621b987b28397110d643f
288.\" https://lwn.net/Articles/312232/
289POSIX message queues
290(see
291.BR mq_overview (7)).
292The common characteristic of these IPC mechanisms is that IPC
293objects are identified by mechanisms other than filesystem
294pathnames.
295.IP
296Objects created in an IPC namespace are visible to all other processes
297that are members of that namespace,
298but are not visible to processes in other IPC namespaces.
299.IP
300When an IPC namespace is destroyed
301(i.e., when the last process that is a member of the namespace terminates),
302all IPC objects in the namespace are automatically destroyed.
303.IP
304Only a privileged process
305.RB ( CAP_SYS_ADMIN )
306can employ
307.BR CLONE_NEWIPC .
308This flag can't be specified in conjunction with
309.BR CLONE_SYSVSEM .
310.IP
311For further information on IPC namespaces, see
312.BR namespaces (7).
313.TP
314.BR CLONE_NEWNET " (since Linux 2.6.24)"
315(The implementation of this flag was completed only
316by about kernel version 2.6.29.)
317.IP
318If
319.B CLONE_NEWNET
320is set, then create the process in a new network namespace.
321If this flag is not set, then (as with
322.BR fork (2))
323the process is created in the same network namespace as
324the calling process.
325This flag is intended for the implementation of containers.
326.IP
327A network namespace provides an isolated view of the networking stack
328(network device interfaces, IPv4 and IPv6 protocol stacks,
329IP routing tables, firewall rules, the
330.I /proc/net
331and
332.I /sys/class/net
333directory trees, sockets, etc.).
334A physical network device can live in exactly one
335network namespace.
336A virtual network
337.RB ( veth (4))
338device pair provides a pipe-like abstraction
339that can be used to create tunnels between network namespaces,
340and can be used to create a bridge to a physical network device
341in another namespace.
342.IP
343When a network namespace is freed
344(i.e., when the last process in the namespace terminates),
345its physical network devices are moved back to the
346initial network namespace (not to the parent of the process).
347For further information on network namespaces, see
348.BR namespaces (7).
349.IP
350Only a privileged process
351.RB ( CAP_SYS_ADMIN )
352can employ
353.BR CLONE_NEWNET .
354.TP
355.BR CLONE_NEWNS " (since Linux 2.4.19)"
356If
357.B CLONE_NEWNS
358is set, the cloned child is started in a new mount namespace,
359initialized with a copy of the namespace of the parent.
360If
361.B CLONE_NEWNS
362is not set, the child lives in the same mount
363namespace as the parent.
364.IP
365Only a privileged process
366.RB ( CAP_SYS_ADMIN )
367can employ
368.BR CLONE_NEWNS .
369It is not permitted to specify both
370.B CLONE_NEWNS
371and
372.B CLONE_FS
373.\" See https://lwn.net/Articles/543273/
374in the same
375.BR clone ()
376call.
377.IP
378For further information on mount namespaces, see
379.BR namespaces (7)
380and
381.BR mount_namespaces (7).
382.TP
383.BR CLONE_NEWPID " (since Linux 2.6.24)"
384.\" This explanation draws a lot of details from
385.\" http://lwn.net/Articles/259217/
386.\" Authors: Pavel Emelyanov <xemul@openvz.org>
387.\" and Kir Kolyshkin <kir@openvz.org>
388.\"
389.\" The primary kernel commit is 30e49c263e36341b60b735cbef5ca37912549264
390.\" Author: Pavel Emelyanov <xemul@openvz.org>
391If
392.B CLONE_NEWPID
393is set, then create the process in a new PID namespace.
394If this flag is not set, then (as with
395.BR fork (2))
396the process is created in the same PID namespace as
397the calling process.
398This flag is intended for the implementation of containers.
399.IP
400For further information on PID namespaces, see
401.BR namespaces (7)
402and
403.BR pid_namespaces (7).
404.IP
405Only a privileged process
406.RB ( CAP_SYS_ADMIN )
407can employ
408.BR CLONE_NEWPID .
409This flag can't be specified in conjunction with
410.BR CLONE_THREAD
411or
412.BR CLONE_PARENT .
413.TP
414.BR CLONE_NEWUSER
415(This flag first became meaningful for
416.BR clone ()
417in Linux 2.6.23,
418the current
419.BR clone ()
420semantics were merged in Linux 3.5,
421and the final pieces to make the user namespaces completely usable were
422merged in Linux 3.8.)
423.IP
424If
425.B CLONE_NEWUSER
426is set, then create the process in a new user namespace.
427If this flag is not set, then (as with
428.BR fork (2))
429the process is created in the same user namespace as the calling process.
430.IP
431Before Linux 3.8, use of
432.BR CLONE_NEWUSER
433required that the caller have three capabilities:
434.BR CAP_SYS_ADMIN ,
435.BR CAP_SETUID ,
436and
437.BR CAP_SETGID .
438.\" Before Linux 2.6.29, it appears that only CAP_SYS_ADMIN was needed
439Starting with Linux 3.8,
440no privileges are needed to create a user namespace.
441.IP
442This flag can't be specified in conjunction with
443.BR CLONE_THREAD
444or
445.BR CLONE_PARENT .
446For security reasons,
447.\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
448.\" https://lwn.net/Articles/543273/
449.\" The fix actually went into 3.9 and into 3.8.3. However, user namespaces
450.\" were, for practical purposes, unusable in earlier 3.8.x because of the
451.\" various filesystems that didn't support userns.
452.BR CLONE_NEWUSER
453cannot be specified in conjunction with
454.BR CLONE_FS .
455.IP
456For further information on user namespaces, see
457.BR namespaces (7)
458and
459.BR user_namespaces (7).
460.TP
461.BR CLONE_NEWUTS " (since Linux 2.6.19)"
462If
463.B CLONE_NEWUTS
464is set, then create the process in a new UTS namespace,
465whose identifiers are initialized by duplicating the identifiers
466from the UTS namespace of the calling process.
467If this flag is not set, then (as with
468.BR fork (2))
469the process is created in the same UTS namespace as
470the calling process.
471This flag is intended for the implementation of containers.
472.IP
473A UTS namespace is the set of identifiers returned by
474.BR uname (2);
475among these, the domain name and the hostname can be modified by
476.BR setdomainname (2)
477and
478.BR sethostname (2),
479respectively.
480Changes made to the identifiers in a UTS namespace
481are visible to all other processes in the same namespace,
482but are not visible to processes in other UTS namespaces.
483.IP
484Only a privileged process
485.RB ( CAP_SYS_ADMIN )
486can employ
487.BR CLONE_NEWUTS .
488.IP
489For further information on UTS namespaces, see
490.BR namespaces (7).
491.TP
492.BR CLONE_PARENT " (since Linux 2.3.12)"
493If
494.B CLONE_PARENT
495is set, then the parent of the new child (as returned by
496.BR getppid (2))
497will be the same as that of the calling process.
498.IP
499If
500.B CLONE_PARENT
501is not set, then (as with
502.BR fork (2))
503the child's parent is the calling process.
504.IP
505Note that it is the parent process, as returned by
506.BR getppid (2),
507which is signaled when the child terminates, so that
508if
509.B CLONE_PARENT
510is set, then the parent of the calling process, rather than the
511calling process itself, will be signaled.
512.TP
513.BR CLONE_PARENT_SETTID " (since Linux 2.5.49)"
514Store the child thread ID at the location
515.I ptid
516in the parent's memory.
517(In Linux 2.5.32-2.5.48 there was a flag
518.B CLONE_SETTID
519that did this.)
520The store operation completes before
521.BR clone ()
522returns control to user space.
523.TP
524.BR CLONE_PID " (Linux 2.0 to 2.5.15)"
525If
526.B CLONE_PID
527is set, the child process is created with the same process ID as
528the calling process.
529This is good for hacking the system, but otherwise
530of not much use.
531From Linux 2.3.21 onward, this flag could be
532specified only by the system boot process (PID 0).
533The flag disappeared completely from the kernel sources in Linux 2.5.16.
534Since then, the kernel silently ignores this bit if it is specified in
535.IR flags .
536.TP
537.BR CLONE_PTRACE " (since Linux 2.2)"
538If
539.B CLONE_PTRACE
540is specified, and the calling process is being traced,
541then trace the child also (see
542.BR ptrace (2)).
543.TP
544.BR CLONE_SETTLS " (since Linux 2.5.32)"
545The TLS (Thread Local Storage) descriptor is set to
546.IR newtls .
547.IP
548The interpretation of
549.I newtls
550and the resulting effect is architecture dependent.
551On x86,
552.I newtls
553is interpreted as a
554.IR "struct user_desc\ *"
555(see
556.BR set_thread_area (2)).
557On x86-64 it is the new value to be set for the %fs base register
558(see the
559.B ARCH_SET_FS
560argument to
561.BR arch_prctl (2)).
562On architectures with a dedicated TLS register, it is the new value
563of that register.
564.TP
565.BR CLONE_SIGHAND " (since Linux 2.0)"
566If
567.B CLONE_SIGHAND
568is set, the calling process and the child process share the same table of
569signal handlers.
570If the calling process or child process calls
571.BR sigaction (2)
572to change the behavior associated with a signal, the behavior is
573changed in the other process as well.
574However, the calling process and child
575processes still have distinct signal masks and sets of pending
576signals.
577So, one of them may block or unblock signals using
578.BR sigprocmask (2)
579without affecting the other process.
580.IP
581If
582.B CLONE_SIGHAND
583is not set, the child process inherits a copy of the signal handlers
584of the calling process at the time
585.BR clone ()
586is called.
587Calls to
588.BR sigaction (2)
589performed later by one of the processes have no effect on the other
590process.
591.IP
592Since Linux 2.6.0,
593.\" Precisely: Linux 2.6.0-test6
594.I flags
595must also include
596.B CLONE_VM
597if
598.B CLONE_SIGHAND
599is specified
600.TP
601.BR CLONE_STOPPED " (since Linux 2.6.0)"
602.\" Precisely: Linux 2.6.0-test2
603If
604.B CLONE_STOPPED
605is set, then the child is initially stopped (as though it was sent a
606.B SIGSTOP
607signal), and must be resumed by sending it a
608.B SIGCONT
609signal.
610.IP
611This flag was
612.I deprecated
613from Linux 2.6.25 onward,
614and was
615.I removed
616altogether in Linux 2.6.38.
617Since then, the kernel silently ignores it without error.
618.\" glibc 2.8 removed this defn from bits/sched.h
619Starting with Linux 4.6, the same bit was reused for the
620.BR CLONE_NEWCGROUP
621flag.
622.TP
623.BR CLONE_SYSVSEM " (since Linux 2.5.10)"
624If
625.B CLONE_SYSVSEM
626is set, then the child and the calling process share
627a single list of System V semaphore adjustment
628.RI ( semadj )
629values (see
630.BR semop (2)).
631In this case, the shared list accumulates
632.I semadj
633values across all processes sharing the list,
634and semaphore adjustments are performed only when the last process
635that is sharing the list terminates (or ceases sharing the list using
636.BR unshare (2)).
637If this flag is not set, then the child has a separate
638.I semadj
639list that is initially empty.
640.TP
641.BR CLONE_THREAD " (since Linux 2.4.0)"
642.\" Precisely: Linux 2.6.0-test8
643If
644.B CLONE_THREAD
645is set, the child is placed in the same thread group as the calling process.
646To make the remainder of the discussion of
647.B CLONE_THREAD
648more readable, the term "thread" is used to refer to the
649processes within a thread group.
650.IP
651Thread groups were a feature added in Linux 2.4 to support the
652POSIX threads notion of a set of threads that share a single PID.
653Internally, this shared PID is the so-called
654thread group identifier (TGID) for the thread group.
655Since Linux 2.4, calls to
656.BR getpid (2)
657return the TGID of the caller.
658.IP
659The threads within a group can be distinguished by their (system-wide)
660unique thread IDs (TID).
661A new thread's TID is available as the function result
662returned to the caller of
663.BR clone (),
664and a thread can obtain
665its own TID using
666.BR gettid (2).
667.IP
668When a call is made to
669.BR clone ()
670without specifying
671.BR CLONE_THREAD ,
672then the resulting thread is placed in a new thread group
673whose TGID is the same as the thread's TID.
674This thread is the
675.I leader
676of the new thread group.
677.IP
678A new thread created with
679.B CLONE_THREAD
680has the same parent process as the caller of
681.BR clone ()
682(i.e., like
683.BR CLONE_PARENT ),
684so that calls to
685.BR getppid (2)
686return the same value for all of the threads in a thread group.
687When a
688.B CLONE_THREAD
689thread terminates, the thread that created it using
690.BR clone ()
691is not sent a
692.B SIGCHLD
693(or other termination) signal;
694nor can the status of such a thread be obtained
695using
696.BR wait (2).
697(The thread is said to be
698.IR detached .)
699.IP
700After all of the threads in a thread group terminate
701the parent process of the thread group is sent a
702.B SIGCHLD
703(or other termination) signal.
704.IP
705If any of the threads in a thread group performs an
706.BR execve (2),
707then all threads other than the thread group leader are terminated,
708and the new program is executed in the thread group leader.
709.IP
710If one of the threads in a thread group creates a child using
711.BR fork (2),
712then any thread in the group can
713.BR wait (2)
714for that child.
715.IP
716Since Linux 2.5.35,
717.I flags
718must also include
719.B CLONE_SIGHAND
720if
721.B CLONE_THREAD
722is specified
723(and note that, since Linux 2.6.0,
724.\" Precisely: Linux 2.6.0-test6
725.BR CLONE_SIGHAND
726also requires
727.BR CLONE_VM
728to be included).
729.IP
730Signal dispositions and actions are process-wide:
731if an unhandled signal is delivered to a thread, then
732it will affect (terminate, stop, continue, be ignored in)
733all members of the thread group.
734.IP
735Each thread has its own signal mask, as set by
736.BR sigprocmask (2).
737.IP
738A signal may be process-directed or thread-directed.
739A process-directed signal is targeted at a thread group (i.e., a TGID),
740and is delivered to an arbitrarily selected thread from among those
741that are not blocking the signal.
742A signal may be process directed because it was generated by the kernel
743for reasons other than a hardware exception, or because it was sent using
744.BR kill (2)
745or
746.BR sigqueue (3).
747A thread-directed signal is targeted at (i.e., delivered to)
748a specific thread.
749A signal may be thread directed because it was sent using
750.BR tgkill (2)
751or
752.BR pthread_sigqueue (3),
753or because the thread executed a machine language instruction that triggered
754a hardware exception
755(e.g., invalid memory access triggering
756.BR SIGSEGV
757or a floating-point exception triggering
758.BR SIGFPE ).
759.IP
760A call to
761.BR sigpending (2)
762returns a signal set that is the union of the pending process-directed
763signals and the signals that are pending for the calling thread.
764.IP
765If a process-directed signal is delivered to a thread group,
766and the thread group has installed a handler for the signal, then
767the handler will be invoked in exactly one, arbitrarily selected
768member of the thread group that has not blocked the signal.
769If multiple threads in a group are waiting to accept the same signal using
770.BR sigwaitinfo (2),
771the kernel will arbitrarily select one of these threads
772to receive the signal.
773.TP
774.BR CLONE_UNTRACED " (since Linux 2.5.46)"
775If
776.B CLONE_UNTRACED
777is specified, then a tracing process cannot force
778.B CLONE_PTRACE
779on this child process.
780.TP
781.BR CLONE_VFORK " (since Linux 2.2)"
782If
783.B CLONE_VFORK
784is set, the execution of the calling process is suspended
785until the child releases its virtual memory
786resources via a call to
787.BR execve (2)
788or
789.BR _exit (2)
790(as with
791.BR vfork (2)).
792.IP
793If
794.B CLONE_VFORK
795is not set, then both the calling process and the child are schedulable
796after the call, and an application should not rely on execution occurring
797in any particular order.
798.TP
799.BR CLONE_VM " (since Linux 2.0)"
800If
801.B CLONE_VM
802is set, the calling process and the child process run in the same memory
803space.
804In particular, memory writes performed by the calling process
805or by the child process are also visible in the other process.
806Moreover, any memory mapping or unmapping performed with
807.BR mmap (2)
808or
809.BR munmap (2)
810by the child or calling process also affects the other process.
811.IP
812If
813.B CLONE_VM
814is not set, the child process runs in a separate copy of the memory
815space of the calling process at the time of
816.BR clone ().
817Memory writes or file mappings/unmappings performed by one of the
818processes do not affect the other, as with
819.BR fork (2).
820.SH NOTES
821Note that the glibc
822.BR clone ()
823wrapper function makes some changes
824in the memory pointed to by
825.I child_stack
826(changes required to set the stack up correctly for the child)
827.I before
828invoking the
829.BR clone ()
830system call.
831So, in cases where
832.BR clone ()
833is used to recursively create children,
834do not use the buffer employed for the parent's stack
835as the stack of the child.
836.\"
837.SS C library/kernel differences
838The raw
839.BR clone ()
840system call corresponds more closely to
841.BR fork (2)
842in that execution in the child continues from the point of the
843call.
844As such, the
845.I fn
846and
847.I arg
848arguments of the
849.BR clone ()
850wrapper function are omitted.
851.PP
852Another difference for the raw
853.BR clone ()
854system call is that the
855.I child_stack
856argument may be NULL,
857in which case the child uses a duplicate of the parent's stack.
858(Copy-on-write semantics ensure that the child gets separate copies
859of stack pages when either process modifies the stack.)
860In this case, for correct operation, the
861.B CLONE_VM
862option should not be specified.
863(If the child
864.I shares
865the parent's memory because of the use of the
866.BR CLONE_VM
867flag,
868then no copy-on-write duplication occurs and chaos is likely to result.)
869.PP
870The order of the arguments also differs in the raw system call,
871and there are variations in the arguments across architectures,
872as detailed in the following paragraphs.
873.PP
874The raw system call interface on x86-64 and some other architectures
875(including sh, tile, ia-64, and alpha) is:
876.PP
877.in +4
878.EX
879.BI "long clone(unsigned long " flags ", void *" child_stack ,
880.BI " int *" ptid ", int *" ctid ,
881.BI " unsigned long " newtls );
882.EE
883.in
884.PP
885On x86-32, and several other common architectures
886(including score, ARM, ARM 64, PA-RISC, arc, Power PC, xtensa,
887and MIPS),
888.\" CONFIG_CLONE_BACKWARDS
889the order of the last two arguments is reversed:
890.PP
891.in +4
892.EX
893.BI "long clone(unsigned long " flags ", void *" child_stack ,
894.BI " int *" ptid ", unsigned long " newtls ,
895.BI " int *" ctid );
896.EE
897.in
898.PP
899On the cris and s390 architectures,
900.\" CONFIG_CLONE_BACKWARDS2
901the order of the first two arguments is reversed:
902.PP
903.in +4
904.EX
905.BI "long clone(void *" child_stack ", unsigned long " flags ,
906.BI " int *" ptid ", int *" ctid ,
907.BI " unsigned long " newtls );
908.EE
909.in
910.PP
911On the microblaze architecture,
912.\" CONFIG_CLONE_BACKWARDS3
913an additional argument is supplied:
914.PP
915.in +4
916.EX
917.BI "long clone(unsigned long " flags ", void *" child_stack ,
918.BI " int " stack_size , "\fR /* Size of stack */"
919.BI " int *" ptid ", int *" ctid ,
920.BI " unsigned long " newtls );
921.EE
922.in
923.\"
924.SS blackfin, m68k, and sparc
925.\" Mike Frysinger noted in a 2013 mail:
926.\" these arches don't define __ARCH_WANT_SYS_CLONE:
927.\" blackfin ia64 m68k sparc
928The argument-passing conventions on
929blackfin, m68k, and sparc are different from the descriptions above.
930For details, see the kernel (and glibc) source.
931.SS ia64
932On ia64, a different interface is used:
933.PP
934.in +4
935.EX
936.BI "int __clone2(int (*" "fn" ")(void *), "
937.BI " void *" child_stack_base ", size_t " stack_size ,
938.BI " int " flags ", void *" "arg" ", ... "
939.BI " /* pid_t *" ptid ", struct user_desc *" tls \
940", pid_t *" ctid " */ );"
941.EE
942.in
943.PP
944The prototype shown above is for the glibc wrapper function;
945for the system call itself,
946the prototype can be described as follows (it is identical to the
947.BR clone ()
948prototype on microblaze):
949.PP
950.in +4
951.EX
952.BI "long clone2(unsigned long " flags ", void *" child_stack_base ,
953.BI " int " stack_size , "\fR /* Size of stack */"
954.BI " int *" ptid ", int *" ctid ,
955.BI " unsigned long " tls );
956.EE
957.in
958.PP
959.BR __clone2 ()
960operates in the same way as
961.BR clone (),
962except that
963.I child_stack_base
964points to the lowest address of the child's stack area,
965and
966.I stack_size
967specifies the size of the stack pointed to by
968.IR child_stack_base .
969.SS Linux 2.4 and earlier
970In Linux 2.4 and earlier,
971.BR clone ()
972does not take arguments
973.IR ptid ,
974.IR tls ,
975and
976.IR ctid .
977.SH RETURN VALUE
978.\" gettid(2) returns current->pid;
979.\" getpid(2) returns current->tgid;
980On success, the thread ID of the child process is returned
981in the caller's thread of execution.
982On failure, \-1 is returned
983in the caller's context, no child process will be created, and
984.I errno
985will be set appropriately.
986.SH ERRORS
987.TP
988.B EAGAIN
989Too many processes are already running; see
990.BR fork (2).
991.TP
992.B EINVAL
993.B CLONE_SIGHAND
994was specified, but
995.B CLONE_VM
996was not.
997(Since Linux 2.6.0.)
998.\" Precisely: Linux 2.6.0-test6
999.TP
1000.B EINVAL
1001.B CLONE_THREAD
1002was specified, but
1003.B CLONE_SIGHAND
1004was not.
1005(Since Linux 2.5.35.)
1006.\" .TP
1007.\" .B EINVAL
1008.\" Precisely one of
1009.\" .B CLONE_DETACHED
1010.\" and
1011.\" .B CLONE_THREAD
1012.\" was specified.
1013.\" (Since Linux 2.6.0-test6.)
1014.TP
1015.B EINVAL
1016.B CLONE_THREAD
1017was specified, but the current process previously called
1018.BR unshare (2)
1019with the
1020.B CLONE_NEWPID
1021flag or used
1022.BR setns (2)
1023to reassociate itself with a PID namespace.
1024.TP
1025.B EINVAL
1026.\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
1027Both
1028.B CLONE_FS
1029and
1030.B CLONE_NEWNS
1031were specified in
1032.IR flags .
1033.TP
1034.BR EINVAL " (since Linux 3.9)"
1035Both
1036.B CLONE_NEWUSER
1037and
1038.B CLONE_FS
1039were specified in
1040.IR flags .
1041.TP
1042.B EINVAL
1043Both
1044.B CLONE_NEWIPC
1045and
1046.B CLONE_SYSVSEM
1047were specified in
1048.IR flags .
1049.TP
1050.B EINVAL
1051One (or both) of
1052.BR CLONE_NEWPID
1053or
1054.BR CLONE_NEWUSER
1055and one (or both) of
1056.BR CLONE_THREAD
1057or
1058.BR CLONE_PARENT
1059were specified in
1060.IR flags .
1061.TP
1062.B EINVAL
1063Returned by the glibc
1064.BR clone ()
1065wrapper function when
1066.IR fn
1067or
1068.IR child_stack
1069is specified as NULL.
1070.TP
1071.B EINVAL
1072.BR CLONE_NEWIPC
1073was specified in
1074.IR flags ,
1075but the kernel was not configured with the
1076.B CONFIG_SYSVIPC
1077and
1078.BR CONFIG_IPC_NS
1079options.
1080.TP
1081.B EINVAL
1082.BR CLONE_NEWNET
1083was specified in
1084.IR flags ,
1085but the kernel was not configured with the
1086.B CONFIG_NET_NS
1087option.
1088.TP
1089.B EINVAL
1090.BR CLONE_NEWPID
1091was specified in
1092.IR flags ,
1093but the kernel was not configured with the
1094.B CONFIG_PID_NS
1095option.
1096.TP
1097.B EINVAL
1098.BR CLONE_NEWUSER
1099was specified in
1100.IR flags ,
1101but the kernel was not configured with the
1102.B CONFIG_USER_NS
1103option.
1104.TP
1105.B EINVAL
1106.BR CLONE_NEWUTS
1107was specified in
1108.IR flags ,
1109but the kernel was not configured with the
1110.B CONFIG_UTS_NS
1111option.
1112.TP
1113.B EINVAL
1114.I child_stack
1115is not aligned to a suitable boundary for this architecture.
1116For example, on aarch64,
1117.I child_stack
1118must be a multiple of 16.
1119.TP
1120.B ENOMEM
1121Cannot allocate sufficient memory to allocate a task structure for the
1122child, or to copy those parts of the caller's context that need to be
1123copied.
1124.TP
1125.BR ENOSPC " (since Linux 3.7)"
1126.\" commit f2302505775fd13ba93f034206f1e2a587017929
1127.B CLONE_NEWPID
1128was specified in flags,
1129but the limit on the nesting depth of PID namespaces
1130would have been exceeded; see
1131.BR pid_namespaces (7).
1132.TP
1133.BR ENOSPC " (since Linux 4.9; beforehand " EUSERS )
1134.B CLONE_NEWUSER
1135was specified in
1136.IR flags ,
1137and the call would cause the limit on the number of
1138nested user namespaces to be exceeded.
1139See
1140.BR user_namespaces (7).
1141.IP
1142From Linux 3.11 to Linux 4.8, the error diagnosed in this case was
1143.BR EUSERS .
1144.TP
1145.BR ENOSPC " (since Linux 4.9)"
1146One of the values in
1147.I flags
1148specified the creation of a new user namespace,
1149but doing so would have caused the limit defined by the corresponding file in
1150.IR /proc/sys/user
1151to be exceeded.
1152For further details, see
1153.BR namespaces (7).
1154.TP
1155.B EPERM
1156.BR CLONE_NEWCGROUP ,
1157.BR CLONE_NEWIPC ,
1158.BR CLONE_NEWNET ,
1159.BR CLONE_NEWNS ,
1160.BR CLONE_NEWPID ,
1161or
1162.BR CLONE_NEWUTS
1163was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP).
1164.TP
1165.B EPERM
1166.B CLONE_PID
1167was specified by a process other than process 0.
1168(This error occurs only on Linux 2.5.15 and earlier.)
1169.TP
1170.B EPERM
1171.BR CLONE_NEWUSER
1172was specified in
1173.IR flags ,
1174but either the effective user ID or the effective group ID of the caller
1175does not have a mapping in the parent namespace (see
1176.BR user_namespaces (7)).
1177.TP
1178.BR EPERM " (since Linux 3.9)"
1179.\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d
1180.B CLONE_NEWUSER
1181was specified in
1182.I flags
1183and the caller is in a chroot environment
1184.\" FIXME What is the rationale for this restriction?
1185(i.e., the caller's root directory does not match the root directory
1186of the mount namespace in which it resides).
1187.TP
1188.BR ERESTARTNOINTR " (since Linux 2.6.17)"
1189.\" commit 4a2c7a7837da1b91468e50426066d988050e4d56
1190System call was interrupted by a signal and will be restarted.
1191(This can be seen only during a trace.)
1192.TP
1193.BR EUSERS " (Linux 3.11 to Linux 4.8)"
1194.B CLONE_NEWUSER
1195was specified in
1196.IR flags ,
1197and the limit on the number of nested user namespaces would be exceeded.
1198See the discussion of the
1199.BR ENOSPC
1200error above.
1201.\" .SH VERSIONS
1202.\" There is no entry for
1203.\" .BR clone ()
1204.\" in libc5.
1205.\" glibc2 provides
1206.\" .BR clone ()
1207.\" as described in this manual page.
1208.SH CONFORMING TO
1209.BR clone ()
1210is Linux-specific and should not be used in programs
1211intended to be portable.
1212.SH NOTES
1213The
1214.BR kcmp (2)
1215system call can be used to test whether two processes share various
1216resources such as a file descriptor table,
1217System V semaphore undo operations, or a virtual address space.
1218.PP
1219.PP
1220Handlers registered using
1221.BR pthread_atfork (3)
1222are not executed during a call to
1223.BR clone ().
1224.PP
1225In the Linux 2.4.x series,
1226.B CLONE_THREAD
1227generally does not make the parent of the new thread the same
1228as the parent of the calling process.
1229However, for kernel versions 2.4.7 to 2.4.18 the
1230.B CLONE_THREAD
1231flag implied the
1232.B CLONE_PARENT
1233flag (as in Linux 2.6.0 and later).
1234.PP
1235For a while there was
1236.B CLONE_DETACHED
1237(introduced in 2.5.32):
1238parent wants no child-exit signal.
1239In Linux 2.6.2, the need to give this flag together with
1240.B CLONE_THREAD
1241disappeared.
1242This flag is still defined, but has no effect.
1243.PP
1244On i386,
1245.BR clone ()
1246should not be called through vsyscall, but directly through
1247.IR "int $0x80" .
1248.SH BUGS
1249GNU C library versions 2.3.4 up to and including 2.24
1250contained a wrapper function for
1251.BR getpid (2)
1252that performed caching of PIDs.
1253This caching relied on support in the glibc wrapper for
1254.BR clone (),
1255but limitations in the implementation
1256meant that the cache was not up to date in some circumstances.
1257In particular,
1258if a signal was delivered to the child immediately after the
1259.BR clone ()
1260call, then a call to
1261.BR getpid (2)
1262in a handler for the signal could return the PID
1263of the calling process ("the parent"),
1264if the clone wrapper had not yet had a chance to update the PID
1265cache in the child.
1266(This discussion ignores the case where the child was created using
1267.BR CLONE_THREAD ,
1268when
1269.BR getpid (2)
1270.I should
1271return the same value in the child and in the process that called
1272.BR clone (),
1273since the caller and the child are in the same thread group.
1274The stale-cache problem also does not occur if the
1275.I flags
1276argument includes
1277.BR CLONE_VM .)
1278To get the truth, it was sometimes necessary to use code such as the following:
1279.PP
1280.in +4n
1281.EX
1282#include <syscall.h>
1283
1284pid_t mypid;
1285
1286mypid = syscall(SYS_getpid);
1287.EE
1288.in
1289.\" See also the following bug reports
1290.\" https://bugzilla.redhat.com/show_bug.cgi?id=417521
1291.\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910
1292.PP
1293Because of the stale-cache problem, as well as other problems noted in
1294.BR getpid (2),
1295the PID caching feature was removed in glibc 2.25.
1296.SH EXAMPLE
1297The following program demonstrates the use of
1298.BR clone ()
1299to create a child process that executes in a separate UTS namespace.
1300The child changes the hostname in its UTS namespace.
1301Both parent and child then display the system hostname,
1302making it possible to see that the hostname
1303differs in the UTS namespaces of the parent and child.
1304For an example of the use of this program, see
1305.BR setns (2).
1306.SS Program source
1307.EX
1308#define _GNU_SOURCE
1309#include <sys/wait.h>
1310#include <sys/utsname.h>
1311#include <sched.h>
1312#include <string.h>
1313#include <stdio.h>
1314#include <stdlib.h>
1315#include <unistd.h>
1316
1317#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
1318 } while (0)
1319
1320static int /* Start function for cloned child */
1321childFunc(void *arg)
1322{
1323 struct utsname uts;
1324
1325 /* Change hostname in UTS namespace of child */
1326
1327 if (sethostname(arg, strlen(arg)) == \-1)
1328 errExit("sethostname");
1329
1330 /* Retrieve and display hostname */
1331
1332 if (uname(&uts) == \-1)
1333 errExit("uname");
1334 printf("uts.nodename in child: %s\en", uts.nodename);
1335
1336 /* Keep the namespace open for a while, by sleeping.
1337 This allows some experimentation\-\-for example, another
1338 process might join the namespace. */
1339
1340 sleep(200);
1341
1342 return 0; /* Child terminates now */
1343}
1344
1345#define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */
1346
1347int
1348main(int argc, char *argv[])
1349{
1350 char *stack; /* Start of stack buffer */
1351 char *stackTop; /* End of stack buffer */
1352 pid_t pid;
1353 struct utsname uts;
1354
1355 if (argc < 2) {
1356 fprintf(stderr, "Usage: %s <child\-hostname>\en", argv[0]);
1357 exit(EXIT_SUCCESS);
1358 }
1359
1360 /* Allocate stack for child */
1361
1362 stack = malloc(STACK_SIZE);
1363 if (stack == NULL)
1364 errExit("malloc");
1365 stackTop = stack + STACK_SIZE; /* Assume stack grows downward */
1366
1367 /* Create child that has its own UTS namespace;
1368 child commences execution in childFunc() */
1369
1370 pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]);
1371 if (pid == \-1)
1372 errExit("clone");
1373 printf("clone() returned %ld\en", (long) pid);
1374
1375 /* Parent falls through to here */
1376
1377 sleep(1); /* Give child time to change its hostname */
1378
1379 /* Display hostname in parent\(aqs UTS namespace. This will be
1380 different from hostname in child\(aqs UTS namespace. */
1381
1382 if (uname(&uts) == \-1)
1383 errExit("uname");
1384 printf("uts.nodename in parent: %s\en", uts.nodename);
1385
1386 if (waitpid(pid, NULL, 0) == \-1) /* Wait for child */
1387 errExit("waitpid");
1388 printf("child has terminated\en");
1389
1390 exit(EXIT_SUCCESS);
1391}
1392.EE
1393.SH SEE ALSO
1394.BR fork (2),
1395.BR futex (2),
1396.BR getpid (2),
1397.BR gettid (2),
1398.BR kcmp (2),
1399.BR set_thread_area (2),
1400.BR set_tid_address (2),
1401.BR setns (2),
1402.BR tkill (2),
1403.BR unshare (2),
1404.BR wait (2),
1405.BR capabilities (7),
1406.BR namespaces (7),
1407.BR pthreads (7)