]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/clone.2
Added CAP_AUDIT_CONTROL and CAP_AUDIT_WRITE
[thirdparty/man-pages.git] / man2 / clone.2
CommitLineData
fea681da
MK
1.\" Hey Emacs! This file is -*- nroff -*- source.
2.\"
3.\" Copyright (c) 1992 Drew Eckhardt <drew@cs.colorado.edu>, March 28, 1992
de7639e5 4.\" and Michael Kerrisk, 2001, 2002, 2005
fea681da
MK
5.\" May be distributed under the GNU General Public License.
6.\" Modified by Michael Haardt <michael@moria.de>
7.\" Modified 24 Jul 1993 by Rik Faith <faith@cs.unc.edu>
8.\" Modified 21 Aug 1994 by Michael Chastain <mec@shell.portal.com>:
9.\" New man page (copied from 'fork.2').
10.\" Modified 10 June 1995 by Andries Brouwer <aeb@cwi.nl>
11.\" Modified 25 April 1998 by Xavier Leroy <Xavier.Leroy@inria.fr>
12.\" Modified 26 Jun 2001 by Michael Kerrisk
13.\" Mostly upgraded to 2.4.x
14.\" Added prototype for sys_clone() plus description
15.\" Added CLONE_THREAD with a brief description of thread groups
16.\" Added CLONE_PARENT and revised entire page remove ambiguity
17.\" between "calling process" and "parent process"
18.\" Added CLONE_PTRACE and CLONE_VFORK
19.\" Added EPERM and EINVAL error codes
fd8a5be4 20.\" Renamed "__clone" to "clone" (which is the prototype in <sched.h>)
fea681da 21.\" various other minor tidy ups and clarifications.
305a0578 22.\" Modified 26 Jun 2001 by Michael Kerrisk <mtk-manpages@gmx.net>
fea681da 23.\" Updated notes for 2.4.7+ behaviour of CLONE_THREAD
305a0578 24.\" Modified 15 Oct 2002 by Michael Kerrisk <mtk-manpages@gmx.net>
fea681da
MK
25.\" Added description for CLONE_NEWNS, which was added in 2.4.19
26.\" Slightly rephrased, aeb.
27.\" Modified 1 Feb 2003 - added CLONE_SIGHAND restriction, aeb.
28.\" Modified 1 Jan 2004 - various updates, aeb
29.\" Modified 2004-09-10 - added CLONE_PARENT_SETTID etc - aeb.
31830ef0
MK
30.\" 2005-04-12, mtk, noted the PID caching behaviour of NPTL's getpid()
31.\" wrapper under BUGS.
fd8a5be4
MK
32.\" 2005-05-10, mtk, added CLONE_SYSVSEM, CLONE_UNTRACED, CLONE_STOPPED.
33.\" 2005-05-17, mtk, Substantially enhanced discussion of CLONE_THREAD.
fea681da 34.\"
fd8a5be4 35.TH CLONE 2 2005-05-17 "Linux 2.6" "Linux Programmer's Manual"
fea681da
MK
36.SH NAME
37clone \- create a child process
38.SH SYNOPSIS
39.B #include <sched.h>
40.sp
41.BI "int clone(int (*" "fn" ")(void *), void *" "child_stack" ", int " "flags" ", void *" "arg" );
42.sp
43.BI "_syscall2(int, " clone ", int, " flags ", void *, " child_stack )
44.sp
45.BI "_syscall5(int, " clone ", int, " flags ", void *, " child_stack ,
46.br
47.BI " int *, " parent_tidptr ", struct user_desc *, " newtls ,
48.br
49.BI " int *, " child_tidptr )
50.SH DESCRIPTION
edcc65ff
MK
51.BR clone ()
52creates a new process, in a manner similar to
fea681da 53.BR fork (2).
edcc65ff 54.BR clone ()
fea681da
MK
55is a library function layered on top
56of the underlying
57.BR clone
58system call, hereinafter referred to as
59.BR sys_clone .
60A description of
61.BR sys_clone
62is given towards the end of this page.
63
64Unlike
65.BR fork (2),
66these calls
67allow the child process to share parts of its execution context with
68the calling process, such as the memory space, the table of file
69descriptors, and the table of signal handlers. (Note that on this manual
70page, "calling process" normally corresponds to "parent process". But see
71the description of
72.B CLONE_PARENT
73below.)
74
75The main use of
edcc65ff 76.BR clone ()
fea681da
MK
77is to implement threads: multiple threads of control in a program that
78run concurrently in a shared memory space.
79
80When the child process is created with
edcc65ff 81.BR clone (),
fea681da
MK
82it executes the function
83application
84.IR fn ( arg ).
85(This differs from
86.BR fork (2),
87where execution continues in the child from the point
88of the
89.BR fork (2)
90call.)
91The
92.I fn
93argument is a pointer to a function that is called by the child
94process at the beginning of its execution.
95The
96.I arg
97argument is passed to the
98.I fn
99function.
100
101When the
102.IR fn ( arg )
103function application returns, the child process terminates. The
104integer returned by
105.I fn
106is the exit code for the child process. The child process may also
107terminate explicitly by calling
108.BR exit (2)
109or after receiving a fatal signal.
110
111The
112.I child_stack
113argument specifies the location of the stack used by the child
114process. Since the child and calling process may share memory,
115it is not possible for the child process to execute in the
116same stack as the calling process. The calling process must therefore
117set up memory space for the child stack and pass a pointer to this
118space to
edcc65ff 119.BR clone ().
fea681da
MK
120Stacks grow downwards on all processors that run Linux
121(except the HP PA processors), so
122.I child_stack
123usually points to the topmost address of the memory space set up for
124the child stack.
125
126The low byte of
127.I flags
fd8a5be4
MK
128contains the number of the
129.I "termination signal"
130sent to the parent when the child dies.
131If this signal is specified as anything other than
fea681da
MK
132.BR SIGCHLD ,
133then the parent process must specify the
134.B __WALL
135or
136.B __WCLONE
137options when waiting for the child with
138.BR wait (2).
139If no signal is specified, then the parent process is not signaled
140when the child terminates.
141
142.I flags
fd8a5be4
MK
143may also be bitwise-or'ed with zero or more of the following constants,
144in order to specify what is shared between the calling process
fea681da 145and the child process:
fea681da
MK
146.TP
147.BR CLONE_PARENT " (since Linux 2.3.12)"
148If
149.B CLONE_PARENT
150is set, then the parent of the new child (as returned by
151.BR getppid (2))
152will be the same as that of the calling process.
153
154If
155.B CLONE_PARENT
156is not set, then (as with
157.BR fork (2))
158the child's parent is the calling process.
159
160Note that it is the parent process, as returned by
161.BR getppid (2),
162which is signaled when the child terminates, so that
163if
164.B CLONE_PARENT
165is set, then the parent of the calling process, rather than the
166calling process itself, will be signaled.
fea681da
MK
167.TP
168.B CLONE_FS
169If
170.B CLONE_FS
171is set, the caller and the child processes share the same file system
172information. This includes the root of the file system, the current
173working directory, and the umask. Any call to
174.BR chroot (2),
175.BR chdir (2),
176or
177.BR umask (2)
edcc65ff 178performed by the calling process or the child process also affects the
fea681da
MK
179other process.
180
181If
182.B CLONE_FS
183is not set, the child process works on a copy of the file system
184information of the calling process at the time of the
edcc65ff 185.BR clone ()
fea681da
MK
186call.
187Calls to
188.BR chroot (2),
189.BR chdir (2),
190.BR umask (2)
191performed later by one of the processes do not affect the other process.
fea681da
MK
192.TP
193.B CLONE_FILES
194If
195.B CLONE_FILES
196is set, the calling process and the child processes share the same file
edcc65ff
MK
197descriptor table.
198Any file descriptor created by the calling process or by the child
199process is also valid in the other process.
200Similarly, if one of the processes closes a file descriptor,
201or changes its associated flags (using the
202.BR fcntl (2)
203.B F_SETFD
204operation), the other process is also affected.
fea681da
MK
205
206If
207.B CLONE_FILES
208is not set, the child process inherits a copy of all file descriptors
209opened in the calling process at the time of
edcc65ff
MK
210.BR clone ().
211(The duplicated file descriptors in the child refer to the
212same open file descriptions (see
213.BR open (2))
214as the corresponding file descriptors in the calling process.)
215Subsequent operations that open or close file descriptors,
216or change file descriptor flags,
217performed by either the calling
218process or the child process do not affect the other process.
fea681da
MK
219.TP
220.BR CLONE_NEWNS " (since Linux 2.4.19)
221Start the child in a new namespace.
222
223Every process lives in a namespace. The
224.I namespace
225of a process is the data (the set of mounts) describing the file hierarchy
226as seen by that process. After a
227.BR fork (2)
228or
229.BR clone (2)
230where the
231.B CLONE_NEWNS
232flag is not set, the child lives in the same namespace as the parent.
233The system calls
234.BR mount (2)
235and
236.BR umount (2)
237change the namespace of the calling process, and hence affect
238all processes that live in the same namespace, but do not affect
239processes in a different namespace.
240
241After a
242.BR clone (2)
243where the
244.B CLONE_NEWNS
245flag is set, the cloned child is started in a new namespace,
246initialized with a copy of the namespace of the parent.
247
248Only a privileged process (one having the CAP_SYS_ADMIN capability)
249may specify the
250.B CLONE_NEWNS
251flag.
252It is not permitted to specify both
253.B CLONE_NEWNS
254and
255.B CLONE_FS
256in the same
257.BR clone
258call.
fea681da
MK
259.TP
260.B CLONE_SIGHAND
261If
262.B CLONE_SIGHAND
263is set, the calling process and the child processes share the same table of
264signal handlers. If the calling process or child process calls
265.BR sigaction (2)
266to change the behavior associated with a signal, the behavior is
267changed in the other process as well. However, the calling process and child
268processes still have distinct signal masks and sets of pending
269signals. So, one of them may block or unblock some signals using
270.BR sigprocmask (2)
271without affecting the other process.
272
273If
274.B CLONE_SIGHAND
275is not set, the child process inherits a copy of the signal handlers
276of the calling process at the time
edcc65ff 277.BR clone ()
fea681da
MK
278is called. Calls to
279.BR sigaction (2)
280performed later by one of the processes have no effect on the other
281process.
fea681da
MK
282.TP
283.B CLONE_PTRACE
284If
285.B CLONE_PTRACE
a69b6bda
MK
286is specified, and the calling process is being traced,
287then trace the child also (see
fea681da 288.BR ptrace (2)).
a69b6bda
MK
289.TP
290.BR CLONE_UNTRACED " (since Linux 2.5.46)"
291If
292.B CLONE_UNTRACED
293is specified, then a tracing process cannot force
294.B CLONE_PTRACE
295on this child process.
296.TP
297.BR CLONE_STOPPED " (since Linux 2.6.0-test2)"
298If
299.B CLONE_STOPPED
300is set, then the child is initially stopped (as though it was sent a
301.B SIGSTOP
302signal), and must be resumed by sending it a
303.B SIGCONT
304signal.
305.TP
a69b6bda
MK
306.BR CLONE_STOPPED " (since Linux 2.6.0-test2)"
307If
308.B CLONE_STOPPED
309is set, then the child is initially stopped (as though it was sent a
310.B SIGSTOP
311signal), and must be resumed by sending it a
312.B SIGCONT
313signal.
fea681da
MK
314.TP
315.B CLONE_VFORK
316If
317.B CLONE_VFORK
318is set, the execution of the calling process is suspended
319until the child releases its virtual memory
320resources via a call to
321.BR execve (2)
322or
323.BR _exit (2)
324(as with
325.BR vfork (2)).
326
327If
328.B CLONE_VFORK
329is not set then both the calling process and the child are schedulable
330after the call, and an application should not rely on execution occurring
331in any particular order.
fea681da
MK
332.TP
333.B CLONE_VM
334If
335.B CLONE_VM
336is set, the calling process and the child processes run in the same memory
337space. In particular, memory writes performed by the calling process
338or by the child process are also visible in the other process.
339Moreover, any memory mapping or unmapping performed with
340.BR mmap (2)
341or
342.BR munmap (2)
343by the child or calling process also affects the other process.
344
345If
346.B CLONE_VM
347is not set, the child process runs in a separate copy of the memory
348space of the calling process at the time of
edcc65ff 349.BR clone ().
fea681da
MK
350Memory writes or file mappings/unmappings performed by one of the
351processes do not affect the other, as with
352.BR fork (2).
fea681da
MK
353.TP
354.BR CLONE_PID " (obsolete)"
355If
356.B CLONE_PID
357is set, the child process is created with the same process ID as
358the calling process. This is good for hacking the system, but otherwise
359of not much use. Since 2.3.21 this flag can be
360specified only by the system boot process (PID 0).
361It disappeared in Linux 2.5.16.
fea681da
MK
362.TP
363.BR CLONE_THREAD " (since Linux 2.4.0-test8)"
364If
365.B CLONE_THREAD
366is set, the child is placed in the same thread group as the calling process.
fd8a5be4
MK
367To make the remainder of the discussion of
368.B CLONE_THREAD
369more readable, the term "thread" is used to refer to the
370processes within a thread group.
fea681da
MK
371.\" For a while there was CLONE_DETACHED (introduced in 2.5.32):
372.\" parent wants no child-exit signal. In 2.6.2 the need to give this
373.\" together with CLONE_THREAD disappeared.
374
fd8a5be4
MK
375Thread groups were a feature added in Linux 2.4 to support the
376POSIX threads notion of a set of threads that share a single PID.
377Internally, this shared PID is the so-called
378thread group identifier (TGID) for the thread group.
379Since Linux 2.4, calls to
fea681da 380.BR getpid (2)
fd8a5be4
MK
381return the TGID of the caller.
382
383The threads within a group can be distinguished by their (system-wide)
384unique thread IDs (TID).
385A new thread's TID is available as the function result
386returned to the caller of
387.BR clone (),
388and a thread can obtain
389its own TID using
390.BR gettid (2).
391
392When a call is made to
393.BR clone ()
394without specifying
395.BR CLONE_THREAD ,
396then the resulting thread is placed in a new thread group
397whose TGID is the same as the thread's TID.
398This thread is the
399.I leader
400of the new thread group.
401
402A new thread created with
403.B CLONE_THREAD
404has the same parent process as the caller of
405.BR clone ()
406(i.e., like
407.BR CLONE_PARENT ),
408so that calls to
409.BR getppid (2)
410return the same value for all of the threads in a thread group.
411When a
412.B CLONE_THREAD
413thread terminates, the thread that created it using
414.BR clone ()
415is not sent a
416.B SIGCHLD
417(or other termination) signal;
418nor can the status of such a thread be obtained
419using
420.BR wait (2).
421(The thread is said to be
422.IR detached .)
423
e2fbf61d
MK
424After all of the threads in a thread group terminate
425the parent process of the thread group is sent a
fd8a5be4
MK
426.B SIGCHLD
427(or other termination) signal.
428
429If any of the threads in a thread group performs an
430.BR execve (2),
431then all threads other than the thread group leader are terminated,
432and the new program is executed in the thread group leader.
433
f7110f60
MK
434If one of the threads in a thread group creates a child using
435.BR fork (2),
436then any thread in the group can
437.BR wait (2)
438for that child.
439
edcc65ff 440Since Linux 2.5.35,
fd8a5be4
MK
441.I flags
442must also include
443.B CLONE_SIGHAND
444if
445.B CLONE_THREAD
446is specified.
e2fbf61d
MK
447
448Signals may be sent to a thread group as a whole (i.e., a TGID) using
449.BR kill (2),
450or to a specific thread (i.e., TID) using
451.BR tgkill (2).
452
453Signal dispositions and actions are process-wide:
454if an unhandled signal is delivered to a thread, then
455it will affect (terminate, stop, continue, be ignored in)
456all members of the thread group.
457
99408a60 458Each thread has its own signal mask, as set by
e2fbf61d 459.BR sigprocmask (2),
99408a60 460but signals can be pending
e2fbf61d
MK
461either for the whole process
462(i.e., deliverable to any member of the thread group),
463when sent with
464.BR kill (2)
465or for an individual thread, when sent with
466.BR tgkill (2).
99408a60
MK
467A call to
468.BR sigpending (2)
469returns a signal set that is the union of the signals pending for the
470whole process and the signals that are pending for the calling thread.
e2fbf61d
MK
471
472If
473.BR kill (2)
474is used to send a signal to a thread group,
475and the thread group has installed a handler for the signal, then
476the handler will be invoked in exactly one, arbitrarily selected
477member of the thread group that has not blocked the signal.
478If multiple threads in a group are waiting to accept the same signal using
479.BR sigwaitinfo (2),
480the kernel will arbitrarily select one of these threads
481to receive a signal sent using
482.BR kill (2).
a69b6bda
MK
483.TP
484.BR CLONE_SYSVSEM " (since Linux 2.5.10)"
485If
486.B CLONE_SYSVSEM
487is set, then the child and the calling process share
488a single list of System V semaphore undo values (see
489.BR semop (2)).
490If this flag is not set, then the child has a separate undo list,
491which is initially empty.
fea681da
MK
492.TP
493.BR CLONE_SETTLS " (since Linux 2.5.32)"
494The
495.I newtls
496parameter is the new TLS (Thread Local Storage) descriptor.
497(See
498.BR set_thread_area (2).)
fea681da
MK
499.TP
500.BR CLONE_PARENT_SETTID " (since Linux 2.5.49)"
501Store child thread ID at location
502.I parent_tidptr
503in parent and child memory.
504(In Linux 2.5.32-2.5.48 there was a flag CLONE_SETTID that did this.)
fea681da
MK
505.TP
506.BR CLONE_CHILD_SETTID " (since Linux 2.5.49)"
507Store child thread ID at location
508.I child_tidptr
509in child memory.
fea681da
MK
510.TP
511.BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)"
512Erase child thread ID at location
513.I child_tidptr
514in child memory when the child exits, and do a wakeup on the futex
515at that address.
516The address involved may be changed by the
517.BR set_tid_address (2)
518system call. This is used by threading libraries.
519
fea681da
MK
520.SS "sys_clone"
521The
522.B sys_clone
523system call corresponds more closely to
524.BR fork (2)
525in that execution in the child continues from the point of the
526call. Thus,
527.B sys_clone
528only requires the
529.I flags
530and
531.I child_stack
532arguments, which have the same meaning as for
edcc65ff 533.BR clone ().
fea681da 534(Note that the order of these arguments differs from
edcc65ff 535.BR clone ().)
fea681da
MK
536
537Another difference for
538.B sys_clone
539is that the
540.I child_stack
541argument may be zero, in which case copy-on-write semantics ensure that the
542child gets separate copies of stack pages when either process modifies
543the stack. In this case, for correct operation, the
544.B CLONE_VM
545option should not be specified.
546
547Since Linux 2.5.49 the system call has five parameters.
548The two new parameters are
549.I parent_tidptr
550which points to the location (in parent and child memory) where
551the parent thread ID will be written in case CLONE_PARENT_SETTID
552was specified, and
553.I child_tidptr
554which points to the location (in child memory) where the child thread ID
555will be written in case CLONE_CHILD_SETTID was specified.
556
557.SH "RETURN VALUE"
558.\" gettid() returns current->pid;
559.\" getpid() returns current->tgid;
560On success, the thread ID of the child process is returned
561in the caller's thread of execution. On failure, a \-1 will be returned
562in the caller's context, no child process will be created, and
563.I errno
564will be set appropriately.
565
566.SH ERRORS
567.TP
568.B EAGAIN
569Too many processes are already running.
570.TP
571.B EINVAL
572.B CLONE_SIGHAND
573was specified, but
574.B CLONE_VM
575was not. (Since Linux 2.6.0-test6.)
576.TP
577.B EINVAL
578.B CLONE_THREAD
579was specified, but
580.B CLONE_SIGHAND
581was not. (Since Linux 2.5.35.)
582.TP
583.B EINVAL
584Precisely one of
585.B CLONE_DETACHED
586and
587.B CLONE_THREAD
588was specified. (Since Linux 2.6.0-test6.)
589.TP
590.B EINVAL
591Both
592.B CLONE_FS
593and
594.B CLONE_NEWNS
595were specified in
596.IR flags .
597.TP
598.B EINVAL
599Returned by
edcc65ff 600.BR clone ()
fea681da
MK
601when a zero value is specified for
602.IR child_stack .
603.TP
604.B ENOMEM
605Cannot allocate sufficient memory to allocate a task structure for the
606child, or to copy those parts of the caller's context that need to be
607copied.
608.TP
609.B EPERM
610.B CLONE_NEWNS
611was specified by a non-root process (process without CAP_SYS_ADMIN).
612.TP
613.B EPERM
614.B CLONE_PID
615was specified by a process other than process 0.
616
617.SH AVAILABILITY
618There is no entry for
edcc65ff 619.BR clone ()
fea681da 620in libc5. glibc2 provides
edcc65ff 621.BR clone ()
fea681da
MK
622as described in this manual page.
623
624.SH NOTES
fd8a5be4
MK
625In the kernel 2.4.x series,
626.B CLONE_THREAD
627generally does not make the parent of the new thread the same
628as the parent of the calling process.
629However, for kernel versions 2.4.7 to 2.4.18 the
630.B CLONE_THREAD
631flag implied the
632.B CLONE_PARENT
633flag (as in kernel 2.6).
fea681da
MK
634
635.SH "CONFORMING TO"
636The
edcc65ff 637.BR clone ()
fea681da
MK
638and
639.B sys_clone
640calls are Linux-specific and should not be used in programs
641intended to be portable.
642
31830ef0
MK
643.SH BUGS
644Versions of the GNU C library that include the NPTL threading library
645contain a wrapper function for
646.BR getpid ()
647that performs caching of PIDs.
648In programs linked against such libraries, calls to
649.BR getpid ()
650may return the same value, even when the threads were not created using
651.B CLONE_THREAD
652(and thus are not in the same thread group).
653To get the truth, it may be necessary to use code such as the following
654.nf
655
656 #include <syscall.h>
657
658 pid_t mypid;
659
660 mypid = syscall(SYS_getpid);
661.fi
662
fea681da
MK
663.SH "SEE ALSO"
664.BR fork (2),
2b44301c 665.BR futex (2),
fea681da
MK
666.BR getpid (2),
667.BR gettid (2),
f2d0bbf1 668.BR set_thread_area (2),
2b44301c 669.BR set_tid_address (2),
f2d0bbf1 670.BR tkill (2),
fea681da 671.BR wait (2),
3616b7c0
MK
672.BR capabilities (7),
673.BR pthreads (7)