]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/clone.2
malloc.3: SEE ALSO: add malloc_usable_size(3)
[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.
4e836144 34.\" 2008-11-18, mtk, order CLONE_* flags alphabetically
82ee147a 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()?)
360ed6b3 42.\"
a60450a9 43.TH CLONE 2 2011-09-08 "Linux" "Linux Programmer's Manual"
fea681da 44.SH NAME
9b0e0996 45clone, __clone2 \- create a child process
fea681da 46.SH SYNOPSIS
c10859eb 47.nf
86b91fdf 48.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
cc4615cc
MK
49.\" Actually _BSD_SOURCE || _SVID_SOURCE
50.\" See http://sources.redhat.com/bugzilla/show_bug.cgi?id=4749
fea681da 51.B #include <sched.h>
c10859eb 52
ff929e3b
MK
53.BI "int clone(int (*" "fn" ")(void *), void *" child_stack ,
54.BI " int " flags ", void *" "arg" ", ... "
d3dbc9b1 55.BI " /* pid_t *" ptid ", struct user_desc *" tls \
ff929e3b 56", pid_t *" ctid " */ );"
c10859eb 57.fi
fea681da 58.SH DESCRIPTION
edcc65ff
MK
59.BR clone ()
60creates a new process, in a manner similar to
fea681da 61.BR fork (2).
735f354f 62It is actually a library function layered on top of the underlying
e511ffb6 63.BR clone ()
fea681da
MK
64system call, hereinafter referred to as
65.BR sys_clone .
66A description of
0daa9e92 67.B sys_clone
5fab2e7c 68is given toward the end of this page.
fea681da
MK
69
70Unlike
71.BR fork (2),
c13182ef 72these calls
fea681da
MK
73allow the child process to share parts of its execution context with
74the calling process, such as the memory space, the table of file
c13182ef
MK
75descriptors, and the table of signal handlers.
76(Note that on this manual
77page, "calling process" normally corresponds to "parent process".
78But see the description of
79.B CLONE_PARENT
fea681da
MK
80below.)
81
82The main use of
edcc65ff 83.BR clone ()
fea681da
MK
84is to implement threads: multiple threads of control in a program that
85run concurrently in a shared memory space.
86
87When the child process is created with
c13182ef 88.BR clone (),
fea681da
MK
89it executes the function
90application
c13182ef 91.IR fn ( arg ).
fea681da 92(This differs from
c13182ef 93.BR fork (2),
fea681da 94where execution continues in the child from the point
c13182ef
MK
95of the
96.BR fork (2)
fea681da
MK
97call.)
98The
99.I fn
100argument is a pointer to a function that is called by the child
101process at the beginning of its execution.
102The
103.I arg
104argument is passed to the
105.I fn
106function.
107
c13182ef 108When the
fea681da 109.IR fn ( arg )
c13182ef
MK
110function application returns, the child process terminates.
111The integer returned by
fea681da 112.I fn
c13182ef
MK
113is the exit code for the child process.
114The child process may also terminate explicitly by calling
fea681da
MK
115.BR exit (2)
116or after receiving a fatal signal.
117
118The
119.I child_stack
c13182ef
MK
120argument specifies the location of the stack used by the child process.
121Since the child and calling process may share memory,
fea681da 122it is not possible for the child process to execute in the
c13182ef
MK
123same stack as the calling process.
124The calling process must therefore
fea681da
MK
125set up memory space for the child stack and pass a pointer to this
126space to
edcc65ff 127.BR clone ().
5fab2e7c 128Stacks grow downward on all processors that run Linux
fea681da
MK
129(except the HP PA processors), so
130.I child_stack
131usually points to the topmost address of the memory space set up for
132the child stack.
133
134The low byte of
135.I flags
fd8a5be4
MK
136contains the number of the
137.I "termination signal"
138sent to the parent when the child dies.
139If this signal is specified as anything other than
fea681da
MK
140.BR SIGCHLD ,
141then the parent process must specify the
c13182ef
MK
142.B __WALL
143or
fea681da 144.B __WCLONE
c13182ef
MK
145options when waiting for the child with
146.BR wait (2).
fea681da
MK
147If no signal is specified, then the parent process is not signaled
148when the child terminates.
149
150.I flags
fd8a5be4
MK
151may also be bitwise-or'ed with zero or more of the following constants,
152in order to specify what is shared between the calling process
fea681da 153and the child process:
fea681da 154.TP
f5dbc7c8
MK
155.BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)"
156Erase child thread ID at location
d3dbc9b1 157.I ctid
f5dbc7c8
MK
158in child memory when the child exits, and do a wakeup on the futex
159at that address.
160The address involved may be changed by the
161.BR set_tid_address (2)
162system call.
163This is used by threading libraries.
164.TP
165.BR CLONE_CHILD_SETTID " (since Linux 2.5.49)"
166Store child thread ID at location
d3dbc9b1 167.I ctid
f5dbc7c8
MK
168in child memory.
169.TP
170.B CLONE_FILES
fea681da 171If
f5dbc7c8
MK
172.B CLONE_FILES
173is set, the calling process and the child process share the same file
174descriptor table.
175Any file descriptor created by the calling process or by the child
176process is also valid in the other process.
177Similarly, if one of the processes closes a file descriptor,
178or changes its associated flags (using the
179.BR fcntl (2)
180.B F_SETFD
181operation), the other process is also affected.
fea681da
MK
182
183If
f5dbc7c8
MK
184.B CLONE_FILES
185is not set, the child process inherits a copy of all file descriptors
186opened in the calling process at the time of
187.BR clone ().
188(The duplicated file descriptors in the child refer to the
189same open file descriptions (see
190.BR open (2))
191as the corresponding file descriptors in the calling process.)
192Subsequent operations that open or close file descriptors,
193or change file descriptor flags,
194performed by either the calling
195process or the child process do not affect the other process.
fea681da
MK
196.TP
197.B CLONE_FS
198If
199.B CLONE_FS
314c8ff4 200is set, the caller and the child process share the same file system
c13182ef
MK
201information.
202This includes the root of the file system, the current
203working directory, and the umask.
204Any call to
fea681da
MK
205.BR chroot (2),
206.BR chdir (2),
207or
208.BR umask (2)
edcc65ff 209performed by the calling process or the child process also affects the
fea681da
MK
210other process.
211
c13182ef 212If
fea681da
MK
213.B CLONE_FS
214is not set, the child process works on a copy of the file system
215information of the calling process at the time of the
edcc65ff 216.BR clone ()
fea681da
MK
217call.
218Calls to
219.BR chroot (2),
220.BR chdir (2),
221.BR umask (2)
222performed later by one of the processes do not affect the other process.
fea681da 223.TP
a4cc375e 224.BR CLONE_IO " (since Linux 2.6.25)"
11f27a1c
JA
225If
226.B CLONE_IO
227is set, then the new process shares an I/O context with
228the calling process.
229If this flag is not set, then (as with
230.BR fork (2))
231the new process has its own I/O context.
232
233.\" The following based on text from Jens Axboe
a113945f 234The I/O context is the I/O scope of the disk scheduler (i.e,
11f27a1c
JA
235what the I/O scheduler uses to model scheduling of a process's I/O).
236If processes share the same I/O context,
237they are treated as one by the I/O scheduler.
238As a consequence, they get to share disk time.
239For some I/O schedulers,
240.\" the anticipatory and CFQ scheduler
241if two processes share an I/O context,
242they will be allowed to interleave their disk access.
243If several threads are doing I/O on behalf of the same process
244.RB ( aio_read (3),
245for instance), they should employ
246.BR CLONE_IO
247to get better I/O performance.
248.\" with CFQ and AS.
249
250If the kernel is not configured with the
251.B CONFIG_BLOCK
252option, this flag is a no-op.
253.TP
8722311b 254.BR CLONE_NEWIPC " (since Linux 2.6.19)"
667417b3
MK
255If
256.B CLONE_NEWIPC
257is set, then create the process in a new IPC namespace.
258If this flag is not set, then (as with
259.BR fork (2)),
260the process is created in the same IPC namespace as
261the calling process.
0236bea9 262This flag is intended for the implementation of containers.
667417b3
MK
263
264An IPC namespace consists of the set of identifiers for
265System V IPC objects.
266(These objects are created using
267.BR msgctl (2),
268.BR semctl (2),
269and
270.BR shmctl (2)).
c440fe01 271Objects created in an IPC namespace are visible to all other processes
667417b3
MK
272that are members of that namespace,
273but are not visible to processes in other IPC namespaces.
274
83c1f4b5
MK
275When an IPC namespace is destroyed
276(i.e, when the last process that is a member of the namespace terminates),
277all IPC objects in the namespace are automatically destroyed.
278
667417b3
MK
279Use of this flag requires: a kernel configured with the
280.B CONFIG_SYSVIPC
281and
282.B CONFIG_IPC_NS
c8e18bd1 283options and that the process be privileged
667417b3
MK
284.RB ( CAP_SYS_ADMIN ).
285This flag can't be specified in conjunction with
286.BR CLONE_SYSVSEM .
287.TP
163bf178 288.BR CLONE_NEWNET " (since Linux 2.6.24)"
b9145b2c 289.\" FIXME Check when the implementation was completed
9108d867
MK
290(The implementation of this flag was only completed
291by about kernel version 2.6.29.)
163bf178
MK
292
293If
294.B CLONE_NEWNET
295is set, then create the process in a new network namespace.
296If this flag is not set, then (as with
297.BR fork (2)),
298the process is created in the same network namespace as
299the calling process.
300This flag is intended for the implementation of containers.
301
302A network namespace provides an isolated view of the networking stack
303(network device interfaces, IPv4 and IPv6 protocol stacks,
304IP routing tables, firewall rules, the
305.I /proc/net
306and
307.I /sys/class/net
308directory trees, sockets, etc.).
309A physical network device can live in exactly one
310network namespace.
311A virtual network device ("veth") pair provides a pipe-like abstraction
312that can be used to create tunnels between network namespaces,
313and can be used to create a bridge to a physical network device
314in another namespace.
315
bf032425
SH
316When a network namespace is freed
317(i.e., when the last process in the namespace terminates),
318its physical network devices are moved back to the
319initial network namespace (not to the parent of the process).
320
163bf178
MK
321Use of this flag requires: a kernel configured with the
322.B CONFIG_NET_NS
323option and that the process be privileged
cae2ec15 324.RB ( CAP_SYS_ADMIN ).
163bf178 325.TP
c10859eb 326.BR CLONE_NEWNS " (since Linux 2.4.19)"
732e54dd 327Start the child in a new mount namespace.
fea681da 328
732e54dd 329Every process lives in a mount namespace.
c13182ef 330The
fea681da
MK
331.I namespace
332of a process is the data (the set of mounts) describing the file hierarchy
c13182ef
MK
333as seen by that process.
334After a
fea681da
MK
335.BR fork (2)
336or
2777b1ca 337.BR clone ()
fea681da
MK
338where the
339.B CLONE_NEWNS
732e54dd 340flag is not set, the child lives in the same mount
4df2eb09 341namespace as the parent.
fea681da
MK
342The system calls
343.BR mount (2)
344and
345.BR umount (2)
732e54dd 346change the mount namespace of the calling process, and hence affect
fea681da 347all processes that live in the same namespace, but do not affect
732e54dd 348processes in a different mount namespace.
fea681da
MK
349
350After a
2777b1ca 351.BR clone ()
fea681da
MK
352where the
353.B CLONE_NEWNS
732e54dd 354flag is set, the cloned child is started in a new mount namespace,
fea681da
MK
355initialized with a copy of the namespace of the parent.
356
0b9bdf82 357Only a privileged process (one having the \fBCAP_SYS_ADMIN\fP capability)
fea681da
MK
358may specify the
359.B CLONE_NEWNS
360flag.
361It is not permitted to specify both
362.B CLONE_NEWNS
363and
364.B CLONE_FS
365in the same
e511ffb6 366.BR clone ()
fea681da 367call.
fea681da 368.TP
82ee147a
MK
369.BR CLONE_NEWPID " (since Linux 2.6.24)"
370.\" This explanation draws a lot of details from
371.\" http://lwn.net/Articles/259217/
372.\" Authors: Pavel Emelyanov <xemul@openvz.org>
373.\" and Kir Kolyshkin <kir@openvz.org>
374.\"
375.\" The primary kernel commit is 30e49c263e36341b60b735cbef5ca37912549264
376.\" Author: Pavel Emelyanov <xemul@openvz.org>
377If
5c95e5e8 378.B CLONE_NEWPID
82ee147a
MK
379is set, then create the process in a new PID namespace.
380If this flag is not set, then (as with
381.BR fork (2)),
382the process is created in the same PID namespace as
383the calling process.
0236bea9 384This flag is intended for the implementation of containers.
82ee147a
MK
385
386A PID namespace provides an isolated environment for PIDs:
387PIDs in a new namespace start at 1,
388somewhat like a standalone system, and calls to
389.BR fork (2),
390.BR vfork (2),
391or
27d47e71 392.BR clone ()
5584229c 393will produce processes with PIDs that are unique within the namespace.
82ee147a
MK
394
395The first process created in a new namespace
396(i.e., the process created using the
397.BR CLONE_NEWPID
398flag) has the PID 1, and is the "init" process for the namespace.
399Children that are orphaned within the namespace will be reparented
400to this process rather than
401.BR init (8).
402Unlike the traditional
403.B init
404process, the "init" process of a PID namespace can terminate,
405and if it does, all of the processes in the namespace are terminated.
406
407PID namespaces form a hierarchy.