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