]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/clone.2
clone.2: IPC objects are automatically destroyed when IPC namespace is destroyed
[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
1130df60 4.\" and Copyright (c) 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
c13182ef 16.\" Added CLONE_PARENT and revised entire page remove ambiguity
fea681da
MK
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.
c11b1abf 22.\" Modified 26 Jun 2001 by Michael Kerrisk <mtk.manpages@gmail.com>
d9bfdb9c 23.\" Updated notes for 2.4.7+ behavior of CLONE_THREAD
c11b1abf 24.\" Modified 15 Oct 2002 by Michael Kerrisk <mtk.manpages@gmail.com>
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
0967c11f 29.\" Modified 2004-09-10 - added CLONE_PARENT_SETTID etc. - aeb.
d9bfdb9c 30.\" 2005-04-12, mtk, noted the PID caching behavior of NPTL's getpid()
31830ef0 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.
82ee147a
MK
34.\" 2008-11-18, mtk, order CLONE_* flags alphabetically
35.\" 2008-11-18, mtk, document CLONE_NEWPID
43ce9dda 36.\" 2008-11-19, mtk, document CLONE_NEWUTS
667417b3 37.\" 2008-11-19, mtk, document CLONE_NEWIPC
cfdc761b 38.\" 2008-11-19, Jens Axboe, mtk, document CLONE_IO
fea681da 39.\"
185341d4
MK
40.\" FIXME Document CLONE_NEWUSER, which is new in 2.6.23
41.\" (also supported for unshare()?)
cf1c6c80 42.\" FIXME Document CLONE_NEWNET, new in Linux 2.6.24
6807c79a 43.\" FIXME . 2.6.25 marks the unused CLONE_STOPPED as obsolete, and it will
21a0b03d 44.\" probably be removed in the future.
360ed6b3 45.\"
732e54dd 46.TH CLONE 2 2008-11-20 "Linux" "Linux Programmer's Manual"
fea681da 47.SH NAME
9b0e0996 48clone, __clone2 \- create a child process
fea681da 49.SH SYNOPSIS
c10859eb 50.nf
cc4615cc
MK
51.B #define _GNU_SOURCE
52.\" Actually _BSD_SOURCE || _SVID_SOURCE
53.\" See http://sources.redhat.com/bugzilla/show_bug.cgi?id=4749
fea681da 54.B #include <sched.h>
c10859eb 55
ff929e3b
MK
56.BI "int clone(int (*" "fn" ")(void *), void *" child_stack ,
57.BI " int " flags ", void *" "arg" ", ... "
58.BI " /* pid_t *" pid ", struct user_desc *" tls \
59", pid_t *" ctid " */ );"
c10859eb 60.fi
fea681da 61.SH DESCRIPTION
edcc65ff
MK
62.BR clone ()
63creates a new process, in a manner similar to
fea681da 64.BR fork (2).
735f354f 65It is actually a library function layered on top of the underlying
e511ffb6 66.BR clone ()
fea681da
MK
67system call, hereinafter referred to as
68.BR sys_clone .
69A description of
0daa9e92 70.B sys_clone
fea681da
MK
71is given towards the end of this page.
72
73Unlike
74.BR fork (2),
c13182ef 75these calls
fea681da
MK
76allow the child process to share parts of its execution context with
77the calling process, such as the memory space, the table of file
c13182ef
MK
78descriptors, and the table of signal handlers.
79(Note that on this manual
80page, "calling process" normally corresponds to "parent process".
81But see the description of
82.B CLONE_PARENT
fea681da
MK
83below.)
84
85The main use of
edcc65ff 86.BR clone ()
fea681da
MK
87is to implement threads: multiple threads of control in a program that
88run concurrently in a shared memory space.
89
90When the child process is created with
c13182ef 91.BR clone (),
fea681da
MK
92it executes the function
93application
c13182ef 94.IR fn ( arg ).
fea681da 95(This differs from
c13182ef 96.BR fork (2),
fea681da 97where execution continues in the child from the point
c13182ef
MK
98of the
99.BR fork (2)
fea681da
MK
100call.)
101The
102.I fn
103argument is a pointer to a function that is called by the child
104process at the beginning of its execution.
105The
106.I arg
107argument is passed to the
108.I fn
109function.
110
c13182ef 111When the
fea681da 112.IR fn ( arg )
c13182ef
MK
113function application returns, the child process terminates.
114The integer returned by
fea681da 115.I fn
c13182ef
MK
116is the exit code for the child process.
117The child process may also terminate explicitly by calling
fea681da
MK
118.BR exit (2)
119or after receiving a fatal signal.
120
121The
122.I child_stack
c13182ef
MK
123argument specifies the location of the stack used by the child process.
124Since the child and calling process may share memory,
fea681da 125it is not possible for the child process to execute in the
c13182ef
MK
126same stack as the calling process.
127The calling process must therefore
fea681da
MK
128set up memory space for the child stack and pass a pointer to this
129space to
edcc65ff 130.BR clone ().
fea681da
MK
131Stacks grow downwards on all processors that run Linux
132(except the HP PA processors), so
133.I child_stack
134usually points to the topmost address of the memory space set up for
135the child stack.
136
137The low byte of
138.I flags
fd8a5be4
MK
139contains the number of the
140.I "termination signal"
141sent to the parent when the child dies.
142If this signal is specified as anything other than
fea681da
MK
143.BR SIGCHLD ,
144then the parent process must specify the
c13182ef
MK
145.B __WALL
146or
fea681da 147.B __WCLONE
c13182ef
MK
148options when waiting for the child with
149.BR wait (2).
fea681da
MK
150If no signal is specified, then the parent process is not signaled
151when the child terminates.
152
153.I flags
fd8a5be4
MK
154may also be bitwise-or'ed with zero or more of the following constants,
155in order to specify what is shared between the calling process
fea681da 156and the child process:
fea681da 157.TP
f5dbc7c8
MK
158.BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)"
159Erase child thread ID at location
160.I child_tidptr
161in child memory when the child exits, and do a wakeup on the futex
162at that address.
163The address involved may be changed by the
164.BR set_tid_address (2)
165system call.
166This is used by threading libraries.
167.TP
168.BR CLONE_CHILD_SETTID " (since Linux 2.5.49)"
169Store child thread ID at location
170.I child_tidptr
171in child memory.
172.TP
173.B CLONE_FILES
fea681da 174If
f5dbc7c8
MK
175.B CLONE_FILES
176is set, the calling process and the child process share the same file
177descriptor table.
178Any file descriptor created by the calling process or by the child
179process is also valid in the other process.
180Similarly, if one of the processes closes a file descriptor,
181or changes its associated flags (using the
182.BR fcntl (2)
183.B F_SETFD
184operation), the other process is also affected.
fea681da
MK
185
186If
f5dbc7c8
MK
187.B CLONE_FILES
188is not set, the child process inherits a copy of all file descriptors
189opened in the calling process at the time of
190.BR clone ().
191(The duplicated file descriptors in the child refer to the
192same open file descriptions (see
193.BR open (2))
194as the corresponding file descriptors in the calling process.)
195Subsequent operations that open or close file descriptors,
196or change file descriptor flags,
197performed by either the calling
198process or the child process do not affect the other process.
fea681da
MK
199.TP
200.B CLONE_FS
201If
202.B CLONE_FS
314c8ff4 203is set, the caller and the child process share the same file system
c13182ef
MK
204information.
205This includes the root of the file system, the current
206working directory, and the umask.
207Any call to
fea681da
MK
208.BR chroot (2),
209.BR chdir (2),
210or
211.BR umask (2)
edcc65ff 212performed by the calling process or the child process also affects the
fea681da
MK
213other process.
214
c13182ef 215If
fea681da
MK
216.B CLONE_FS
217is not set, the child process works on a copy of the file system
218information of the calling process at the time of the
edcc65ff 219.BR clone ()
fea681da
MK
220call.
221Calls to
222.BR chroot (2),
223.BR chdir (2),
224.BR umask (2)
225performed later by one of the processes do not affect the other process.
fea681da 226.TP
a4cc375e 227.BR CLONE_IO " (since Linux 2.6.25)"
11f27a1c
JA
228If
229.B CLONE_IO
230is set, then the new process shares an I/O context with
231the calling process.
232If this flag is not set, then (as with
233.BR fork (2))
234the new process has its own I/O context.
235
236.\" The following based on text from Jens Axboe
237The I/O context is the I/O scope of the disk scheduler (i.e,
238what the I/O scheduler uses to model scheduling of a process's I/O).
239If processes share the same I/O context,
240they are treated as one by the I/O scheduler.
241As a consequence, they get to share disk time.
242For some I/O schedulers,
243.\" the anticipatory and CFQ scheduler
244if two processes share an I/O context,
245they will be allowed to interleave their disk access.
246If several threads are doing I/O on behalf of the same process
247.RB ( aio_read (3),
248for instance), they should employ
249.BR CLONE_IO
250to get better I/O performance.
251.\" with CFQ and AS.
252
253If the kernel is not configured with the
254.B CONFIG_BLOCK
255option, this flag is a no-op.
256.TP
667417b3
MK
257.BR CLONE_NEWIPC " (since Linux 2.4.19)"
258If
259.B CLONE_NEWIPC
260is set, then create the process in a new IPC namespace.
261If this flag is not set, then (as with
262.BR fork (2)),
263the process is created in the same IPC namespace as
264the calling process.
0236bea9 265This flag is intended for the implementation of containers.
667417b3
MK
266
267An IPC namespace consists of the set of identifiers for
268System V IPC objects.
269(These objects are created using
270.BR msgctl (2),
271.BR semctl (2),
272and
273.BR shmctl (2)).
274Objects created in an IPC namespace are visible to other processes
275that are members of that namespace,
276but are not visible to processes in other IPC namespaces.
277
83c1f4b5
MK
278When an IPC namespace is destroyed
279(i.e, when the last process that is a member of the namespace terminates),
280all IPC objects in the namespace are automatically destroyed.
281
667417b3
MK
282Use of this flag requires: a kernel configured with the
283.B CONFIG_SYSVIPC
284and
285.B CONFIG_IPC_NS
286configuration options and that the process be privileged
287.RB ( CAP_SYS_ADMIN ).
288This flag can't be specified in conjunction with
289.BR CLONE_SYSVSEM .
290.TP
c10859eb 291.BR CLONE_NEWNS " (since Linux 2.4.19)"
732e54dd 292Start the child in a new mount namespace.
fea681da 293
732e54dd 294Every process lives in a mount namespace.
c13182ef 295The
fea681da
MK
296.I namespace
297of a process is the data (the set of mounts) describing the file hierarchy
c13182ef
MK
298as seen by that process.
299After a
fea681da
MK
300.BR fork (2)
301or
2777b1ca 302.BR clone ()
fea681da
MK
303where the
304.B CLONE_NEWNS
732e54dd 305flag is not set, the child lives in the same mount
4df2eb09 306namespace as the parent.
fea681da
MK
307The system calls
308.BR mount (2)
309and
310.BR umount (2)
732e54dd 311change the mount namespace of the calling process, and hence affect
fea681da 312all processes that live in the same namespace, but do not affect
732e54dd 313processes in a different mount namespace.
fea681da
MK
314
315After a
2777b1ca 316.BR clone ()
fea681da
MK
317where the
318.B CLONE_NEWNS
732e54dd 319flag is set, the cloned child is started in a new mount namespace,
fea681da
MK
320initialized with a copy of the namespace of the parent.
321
0b9bdf82 322Only a privileged process (one having the \fBCAP_SYS_ADMIN\fP capability)
fea681da
MK
323may specify the
324.B CLONE_NEWNS
325flag.
326It is not permitted to specify both
327.B CLONE_NEWNS
328and
329.B CLONE_FS
330in the same
e511ffb6 331.BR clone ()
fea681da 332call.
fea681da 333.TP
82ee147a
MK
334.BR CLONE_NEWPID " (since Linux 2.6.24)"
335.\" This explanation draws a lot of details from
336.\" http://lwn.net/Articles/259217/
337.\" Authors: Pavel Emelyanov <xemul@openvz.org>
338.\" and Kir Kolyshkin <kir@openvz.org>
339.\"
340.\" The primary kernel commit is 30e49c263e36341b60b735cbef5ca37912549264
341.\" Author: Pavel Emelyanov <xemul@openvz.org>
342If
5c95e5e8 343.B CLONE_NEWPID
82ee147a
MK
344is set, then create the process in a new PID namespace.
345If this flag is not set, then (as with
346.BR fork (2)),
347the process is created in the same PID namespace as
348the calling process.
0236bea9 349This flag is intended for the implementation of containers.
82ee147a
MK
350
351A PID namespace provides an isolated environment for PIDs:
352PIDs in a new namespace start at 1,
353somewhat like a standalone system, and calls to
354.BR fork (2),
355.BR vfork (2),
356or
357.BR clone (2)
358will produce processes whose PIDs within the namespace
359are only guaranteed to be unique within that namespace.
360
361The first process created in a new namespace
362(i.e., the process created using the
363.BR CLONE_NEWPID
364flag) has the PID 1, and is the "init" process for the namespace.
365Children that are orphaned within the namespace will be reparented
366to this process rather than
367.BR init (8).
368Unlike the traditional
369.B init
370process, the "init" process of a PID namespace can terminate,
371and if it does, all of the processes in the namespace are terminated.
372
373PID namespaces form a hierarchy.
374When a PID new namespace is created,
375the PIDs of the processes in that namespace are visible
376in the PID namespace of the process that created the new namespace;
377analogously, if the parent PID namespace is itself
378the child of another PID namespace,
379then PIDs of the child and parent PID namespaces will both be
380visible in the grandparent PID namespace.
381Conversely, the processes in the "child" PID namespace do not see
382the PIDs of the processes in the parent namespace.
383The existence of a namespace hierarchy means that each process
384may now have multiple PIDs:
385one for each namespace in which it is visible.
386(A call to
387.BR getpid (2)
388always returns the PID associated with the namespace in which
389the process was created.)
390
391After creating the new namespace,
392it is useful for the child to change its root directory
393and mount a new procfs instance at
394.I /proc
395so that tools such as
396.BR ps (1)
397work correctly.
398.\" mount -t proc proc /proc
399
400Use of this flag requires: a kernel configured with the
401.B CONFIG_PID_NS
402configuration option and that the process be privileged
af3c728e 403.RB ( CAP_SYS_ADMIN ).
82ee147a
MK
404This flag can't be specified in conjunction with
405.BR CLONE_THREAD .
406.TP
43ce9dda
MK
407.BR CLONE_NEWUTS " (since Linux 2.6.19)"
408If
409.B CLONE_NEWUTS
410is set, then create the process in a new UTS namespace.
411If this flag is not set, then (as with
412.BR fork (2)),
413the process is created in the same UTS namespace as
414the calling process.
0236bea9 415This flag is intended for the implementation of containers.
43ce9dda
MK
416
417A UTS namespace is the set of identifiers returned by
418.BR uname (2);
419among these, the domain name and the host name can be modified by
420.BR setdomainname (2)
421and
422.BR
423.BR sethostname (2),
424respectively.
425Changes made to these identifiers in one UTS namespace
426are visible to other processes in the same namespace,
427but are not visible to processes in other UTS namespaces.
428
429Use of this flag requires: a kernel configured with the
430.B CONFIG_UTS_NS
431configuration option and that the process be privileged
432.RB ( CAP_SYS_ADMIN ).
433.TP
f5dbc7c8
MK
434.BR CLONE_PARENT " (since Linux 2.3.12)"
435If
436.B CLONE_PARENT
437is set, then the parent of the new child (as returned by
438.BR getppid (2))
439will be the same as that of the calling process.
440
441If
442.B CLONE_PARENT
443is not set, then (as with
444.BR fork (2))
445the child's parent is the calling process.
446
447Note that it is the parent process, as returned by
448.BR getppid (2),
449which is signaled when the child terminates, so that
450if
451.B CLONE_PARENT
452is set, then the parent of the calling process, rather than the
453calling process itself, will be signaled.
454.TP
455.BR CLONE_PARENT_SETTID " (since Linux 2.5.49)"
456Store child thread ID at location
457.I parent_tidptr
458in parent and child memory.
459(In Linux 2.5.32-2.5.48 there was a flag
460.B CLONE_SETTID
461that did this.)
462.TP
463.BR CLONE_PID " (obsolete)"
464If
465.B CLONE_PID
466is set, the child process is created with the same process ID as
467the calling process.
468This is good for hacking the system, but otherwise
469of not much use.
470Since 2.3.21 this flag can be
471specified only by the system boot process (PID 0).
472It disappeared in Linux 2.5.16.
473.TP
474.B CLONE_PTRACE
475If
476.B CLONE_PTRACE
477is specified, and the calling process is being traced,
478then trace the child also (see
479.BR ptrace (2)).
480.TP
481.BR CLONE_SETTLS " (since Linux 2.5.32)"
482The
483.I newtls
484argument is the new TLS (Thread Local Storage) descriptor.
485(See
486.BR set_thread_area (2).)
487.TP
fea681da
MK
488.B CLONE_SIGHAND
489If
490.B CLONE_SIGHAND
314c8ff4 491is set, the calling process and the child process share the same table of
c13182ef
MK
492signal handlers.
493If the calling process or child process calls
fea681da 494.BR sigaction (2)
c13182ef
MK
495to change the behavior associated with a signal, the behavior is
496changed in the other process as well.
497However, the calling process and child
fea681da 498processes still have distinct signal masks and sets of pending
c13182ef
MK
499signals.
500So, one of them may block or unblock some signals using
fea681da
MK
501.BR sigprocmask (2)
502without affecting the other process.
503
504If
505.B CLONE_SIGHAND
506is not set, the child process inherits a copy of the signal handlers
507of the calling process at the time
edcc65ff 508.BR clone ()
c13182ef
MK
509is called.
510Calls to
fea681da
MK
511.BR sigaction (2)
512performed later by one of the processes have no effect on the other
513process.
29546c24
MK
514
515Since Linux 2.6.0-test6,
516.I flags
517must also include
518.B CLONE_VM
519if
520.B CLONE_SIGHAND
521is specified
fea681da 522.TP
a69b6bda
MK
523.BR CLONE_STOPPED " (since Linux 2.6.0-test2)"
524If
525.B CLONE_STOPPED
526is set, then the child is initially stopped (as though it was sent a
527.B SIGSTOP
528signal), and must be resumed by sending it a
529.B SIGCONT
530signal.
ef37eaf2
MK
531
532.I "From Linux 2.6.25 this flag is deprecated."
533You probably never wanted to use it,
53c94269 534you certainly shouldn't be using it, and soon it will go away.
a5a061ee 535.\" glibc 2.8 removed this defn from bits/sched.h
a69b6bda 536.TP
f5dbc7c8 537.BR CLONE_SYSVSEM " (since Linux 2.5.10)"
fea681da 538If
f5dbc7c8
MK
539.B CLONE_SYSVSEM
540is set, then the child and the calling process share
541a single list of System V semaphore undo values (see
542.BR semop (2)).
543If this flag is not set, then the child has a separate undo list,
544which is initially empty.
fea681da
MK
545.TP
546.BR CLONE_THREAD " (since Linux 2.4.0-test8)"
547If
548.B CLONE_THREAD
549is set, the child is placed in the same thread group as the calling process.
fd8a5be4
MK
550To make the remainder of the discussion of
551.B CLONE_THREAD
552more readable, the term "thread" is used to refer to the
553processes within a thread group.
fea681da 554
fd8a5be4
MK
555Thread groups were a feature added in Linux 2.4 to support the
556POSIX threads notion of a set of threads that share a single PID.
557Internally, this shared PID is the so-called
558thread group identifier (TGID) for the thread group.
c13182ef 559Since Linux 2.4, calls to
fea681da 560.BR getpid (2)
fd8a5be4
MK
561return the TGID of the caller.
562
563The threads within a group can be distinguished by their (system-wide)
564unique thread IDs (TID).
565A new thread's TID is available as the function result
566returned to the caller of
567.BR clone (),
568and a thread can obtain
569its own TID using
570.BR gettid (2).
571
c13182ef 572When a call is made to
fd8a5be4
MK
573.BR clone ()
574without specifying
575.BR CLONE_THREAD ,
576then the resulting thread is placed in a new thread group
577whose TGID is the same as the thread's TID.
578This thread is the
579.I leader
580of the new thread group.
581
582A new thread created with
583.B CLONE_THREAD
584has the same parent process as the caller of
585.BR clone ()
c13182ef 586(i.e., like
fd8a5be4
MK
587.BR CLONE_PARENT ),
588so that calls to
589.BR getppid (2)
590return the same value for all of the threads in a thread group.
591When a
c13182ef 592.B CLONE_THREAD
fd8a5be4
MK
593thread terminates, the thread that created it using
594.BR clone ()
595is not sent a
596.B SIGCHLD
597(or other termination) signal;
598nor can the status of such a thread be obtained
599using
600.BR wait (2).
601(The thread is said to be
602.IR detached .)
603
e2fbf61d
MK
604After all of the threads in a thread group terminate
605the parent process of the thread group is sent a
fd8a5be4
MK
606.B SIGCHLD
607(or other termination) signal.
608
609If any of the threads in a thread group performs an
610.BR execve (2),
611then all threads other than the thread group leader are terminated,
612and the new program is executed in the thread group leader.
613
f7110f60
MK
614If one of the threads in a thread group creates a child using
615.BR fork (2),
616then any thread in the group can
617.BR wait (2)
618for that child.
619
edcc65ff 620Since Linux 2.5.35,
fd8a5be4
MK
621.I flags
622must also include
623.B CLONE_SIGHAND
624if
625.B CLONE_THREAD
626is specified.
e2fbf61d
MK
627
628Signals may be sent to a thread group as a whole (i.e., a TGID) using
629.BR kill (2),
630or to a specific thread (i.e., TID) using
631.BR tgkill (2).
632
633Signal dispositions and actions are process-wide:
634if an unhandled signal is delivered to a thread, then
635it will affect (terminate, stop, continue, be ignored in)
636all members of the thread group.
637
99408a60 638Each thread has its own signal mask, as set by
e2fbf61d 639.BR sigprocmask (2),
82a06020 640but signals can be pending either: for the whole process
e2fbf61d
MK
641(i.e., deliverable to any member of the thread group),
642when sent with
82a06020 643.BR kill (2);
e2fbf61d
MK
644or for an individual thread, when sent with
645.BR tgkill (2).
99408a60
MK
646A call to
647.BR sigpending (2)
648returns a signal set that is the union of the signals pending for the
649whole process and the signals that are pending for the calling thread.
e2fbf61d 650
c13182ef 651If
e2fbf61d
MK
652.BR kill (2)
653is used to send a signal to a thread group,
654and the thread group has installed a handler for the signal, then
655the handler will be invoked in exactly one, arbitrarily selected
656member of the thread group that has not blocked the signal.
c13182ef 657If multiple threads in a group are waiting to accept the same signal using
e2fbf61d
MK
658.BR sigwaitinfo (2),
659the kernel will arbitrarily select one of these threads
c13182ef 660to receive a signal sent using
e2fbf61d 661.BR kill (2).
a69b6bda 662.TP
f5dbc7c8 663.BR CLONE_UNTRACED " (since Linux 2.5.46)"
a69b6bda 664If
f5dbc7c8
MK
665.B CLONE_UNTRACED
666is specified, then a tracing process cannot force
667.B CLONE_PTRACE
668on this child process.
fea681da 669.TP
f5dbc7c8
MK
670.B CLONE_VFORK
671If
672.B CLONE_VFORK
673is set, the execution of the calling process is suspended
674until the child releases its virtual memory
675resources via a call to
676.BR execve (2)
677or
678.BR _exit (2)
679(as with
680.BR vfork (2)).
681
682If
683.B CLONE_VFORK
684is not set then both the calling process and the child are schedulable
685after the call, and an application should not rely on execution occurring
686in any particular order.
fea681da 687.TP
f5dbc7c8
MK
688.B CLONE_VM
689If
690.B CLONE_VM
691is set, the calling process and the child process run in the same memory
692space.
693In particular, memory writes performed by the calling process
694or by the child process are also visible in the other process.
695Moreover, any memory mapping or unmapping performed with
696.BR mmap (2)
697or
698.BR munmap (2)
699by the child or calling process also affects the other process.
700
701If
702.B CLONE_VM
703is not set, the child process runs in a separate copy of the memory
704space of the calling process at the time of
705.BR clone ().
706Memory writes or file mappings/unmappings performed by one of the
707processes do not affect the other, as with
708.BR fork (2).
fea681da
MK
709.SS "sys_clone"
710The
711.B sys_clone
712system call corresponds more closely to
713.BR fork (2)
714in that execution in the child continues from the point of the
c13182ef
MK
715call.
716Thus,
fea681da
MK
717.B sys_clone
718only requires the
719.I flags
c13182ef 720and
fea681da 721.I child_stack
c13182ef
MK
722arguments, which have the same meaning as for
723.BR clone ().
fea681da 724(Note that the order of these arguments differs from
c13182ef 725.BR clone ().)
fea681da 726
c13182ef 727Another difference for
fea681da
MK
728.B sys_clone
729is that the
730.I child_stack
c13182ef 731argument may be zero, in which case copy-on-write semantics ensure that the
fea681da 732child gets separate copies of stack pages when either process modifies
c13182ef
MK
733the stack.
734In this case, for correct operation, the
fea681da
MK
735.B CLONE_VM
736option should not be specified.
737
c4bb193f
MK
738Since Linux 2.5.49 the system call has five arguments.
739The two new arguments are
fea681da
MK
740.I parent_tidptr
741which points to the location (in parent and child memory) where
682edefb
MK
742the child thread ID will be written in case
743.B CLONE_PARENT_SETTID
fea681da
MK
744was specified, and
745.I child_tidptr
746which points to the location (in child memory) where the child thread ID
682edefb
MK
747will be written in case
748.B CLONE_CHILD_SETTID
749was specified.
fea681da 750.SH "RETURN VALUE"
0bfa087b
MK
751.\" gettid(2) returns current->pid;
752.\" getpid(2) returns current->tgid;
fea681da 753On success, the thread ID of the child process is returned
c13182ef 754in the caller's thread of execution.
84811e86 755On failure, \-1 is returned
fea681da
MK
756in the caller's context, no child process will be created, and
757.I errno
758will be set appropriately.
fea681da
MK
759.SH ERRORS
760.TP
761.B EAGAIN
762Too many processes are already running.
763.TP
764.B EINVAL
765.B CLONE_SIGHAND
766was specified, but
767.B CLONE_VM
2e8a7fb3
MK
768was not.
769(Since Linux 2.6.0-test6.)
fea681da
MK
770.TP
771.B EINVAL
772.B CLONE_THREAD
773was specified, but
774.B CLONE_SIGHAND
6387216b
MK
775was not.
776(Since Linux 2.5.35.)
29546c24
MK
777.\" .TP
778.\" .B EINVAL
779.\" Precisely one of
780.\" .B CLONE_DETACHED
781.\" and
782.\" .B CLONE_THREAD
6387216b
MK
783.\" was specified.
784.\" (Since Linux 2.6.0-test6.)
fea681da
MK
785.TP
786.B EINVAL
787Both
788.B CLONE_FS
789and
790.B CLONE_NEWNS
791were specified in
792.IR flags .
793.TP
794.B EINVAL
82ee147a 795Both
667417b3
MK
796.B CLONE_NEWIPC
797and
798.B CLONE_SYSVSEM
799were specified in
800.IR flags .
801.TP
802.B EINVAL
803Both
82ee147a
MK
804.BR CLONE_NEWPID
805and
806.BR CLONE_THREAD
807were specified in
808.IR flags .
809.TP
810.B EINVAL
c13182ef 811Returned by
edcc65ff 812.BR clone ()
c13182ef 813when a zero value is specified for
fea681da
MK
814.IR child_stack .
815.TP
28cad2c1 816.B EINVAL
667417b3
MK
817.BR CLONE_NEWIPC
818was specified in
819.IR flags ,
820but the kernel was not configured with the
821.B CONFIG_SYSVIPC
822and
823.BR CONFIG_IPC_NS
824options.
825.TP
826.B EINVAL
28cad2c1
MK
827.BR CLONE_NEWPID
828was specified in
829.IR flags ,
830but the kernel was not configured with the
831.B CONFIG_PID_NS
832option.
833.TP
43ce9dda
MK
834.B EINVAL
835.BR CLONE_NEWUTS
836was specified in
837.IR flags ,
838but the kernel was not configured with the
839.B CONFIG_UTS
840option.
841.TP
fea681da
MK
842.B ENOMEM
843Cannot allocate sufficient memory to allocate a task structure for the
844child, or to copy those parts of the caller's context that need to be
845copied.
846.TP
847.B EPERM
667417b3 848.BR CLONE_NEWIPC ,
43ce9dda
MK
849.BR CLONE_NEWNS ,
850.BR CLONE_NEWPID ,
82ee147a 851or
43ce9dda 852.BR CLONE_NEWUTS
0b9bdf82 853was specified by a non-root process (process without \fBCAP_SYS_ADMIN\fP).
fea681da
MK
854.TP
855.B EPERM
856.B CLONE_PID
857was specified by a process other than process 0.
a759cc87 858.SH VERSIONS
fea681da 859There is no entry for
edcc65ff 860.BR clone ()
a759cc87
MK
861in libc5.
862glibc2 provides
edcc65ff 863.BR clone ()
fea681da 864as described in this manual page.
a1d5f77c
MK
865.SH "CONFORMING TO"
866The
867.BR clone ()
868and
869.B sys_clone
8382f16d 870calls are Linux-specific and should not be used in programs
a1d5f77c 871intended to be portable.
fea681da 872.SH NOTES
fd8a5be4
MK
873In the kernel 2.4.x series,
874.B CLONE_THREAD
875generally does not make the parent of the new thread the same
876as the parent of the calling process.
877However, for kernel versions 2.4.7 to 2.4.18 the
878.B CLONE_THREAD
879flag implied the
c13182ef 880.B CLONE_PARENT
fd8a5be4 881flag (as in kernel 2.6).
fea681da 882
c13182ef
MK
883For a while there was
884.B CLONE_DETACHED
a5053dcb 885(introduced in 2.5.32):
c13182ef 886parent wants no child-exit signal.
a5053dcb 887In 2.6.2 the need to give this
c13182ef
MK
888together with
889.B CLONE_THREAD
a5053dcb
MK
890disappeared.
891This flag is still defined, but has no effect.
892
34ccb744 893On i386,
a5a997ca
MK
894.BR clone ()
895should not be called through vsyscall, but directly through
896.IR "int $0x80" .
ff929e3b 897
22399250 898On ia64, a different system call is used:
ff929e3b
MK
899.nf
900
9b0e0996
MK
901.BI "int __clone2(int (*" "fn" ")(void *), "
902.BI " void *" child_stack_base ", size_t " stack_size ,
903.BI " int " flags ", void *" "arg" ", ... "
904.BI " /* pid_t *" pid ", struct user_desc *" tls \
ff929e3b
MK
905", pid_t *" ctid " */ );"
906.fi
907.PP
908The
9b0e0996 909.BR __clone2 ()
c13182ef 910system call operates in the same way as
ff929e3b
MK
911.BR clone (),
912except that
913.I child_stack_base
914points to the lowest address of the child's stack area,
915and
916.I stack_size
917specifies the size of the stack pointed to by
918.IR child_stack_base .
31830ef0
MK
919.SH BUGS
920Versions of the GNU C library that include the NPTL threading library
c13182ef 921contain a wrapper function for
0bfa087b 922.BR getpid (2)
31830ef0 923that performs caching of PIDs.
c60237c9
MK
924This caching relies on support in the glibc wrapper for
925.BR clone (),
926but as currently implemented,
927the cache may not be up to date in some circumstances.
928In particular,
929if a signal is delivered to the child immediately after the
930.BR clone ()
931call, then a call to
932.BR getpid ()
933in a handler for the signal may return the PID
934of the calling process ("the parent"),
88619baf 935if the clone wrapper has not yet had a chance to update the PID
c60237c9
MK
936cache in the child.
937(This discussion ignores the case where the child was created using
9291ce36 938.BR CLONE_THREAD ,
c60237c9
MK
939when
940.BR getpid ()
941.I should
942return the same value in the child and in the process that called
943.BR clone (),
a1d48abb 944since the caller and the child are in the same thread group.
e7d807b7 945The stale-cache problem also does not occur if the
a1d48abb
JR
946.I flags
947argument includes
948.BR CLONE_VM .)
c60237c9 949To get the truth, it may be necessary to use code such as the following:
31830ef0
MK
950.nf
951
952 #include <syscall.h>
953
954 pid_t mypid;
955
956 mypid = syscall(SYS_getpid);
957.fi
c60237c9
MK
958.\" See also the following bug reports
959.\" https://bugzilla.redhat.com/show_bug.cgi?id=417521
960.\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910
fea681da
MK
961.SH "SEE ALSO"
962.BR fork (2),
2b44301c 963.BR futex (2),
fea681da
MK
964.BR getpid (2),
965.BR gettid (2),
f2d0bbf1 966.BR set_thread_area (2),
2b44301c 967.BR set_tid_address (2),
f2d0bbf1 968.BR tkill (2),
5cc01e9c 969.BR unshare (2),
fea681da 970.BR wait (2),
3616b7c0
MK
971.BR capabilities (7),
972.BR pthreads (7)