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