]>
Commit | Line | Data |
---|---|---|
fea681da | 1 | .\" Copyright (c) 1992 Drew Eckhardt <drew@cs.colorado.edu>, March 28, 1992 |
fb1fa92b | 2 | .\" and Copyright (c) Michael Kerrisk, 2001, 2002, 2005, 2013, 2019 |
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 | .\" |
a5409de9 | 42 | .TH CLONE 2 2019-11-19 "Linux" "Linux Programmer's Manual" |
fea681da | 43 | .SH NAME |
a9e52b43 | 44 | clone, __clone2, clone3 \- 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 |
81c2368f MK |
52 | .BI "int clone(int (*" "fn" ")(void *), void *" stack \ |
53 | ", int " flags ", void *" "arg" ", ... " | |
54 | .BI " /* pid_t *" parent_tid ", void *" tls \ | |
55 | ", pid_t *" child_tid " */ );" | |
dbfe9c70 | 56 | .PP |
faa0e55a MK |
57 | /* For the prototype of the raw clone() system call, see NOTES */ |
58 | .PP | |
75e28eba | 59 | .BI "long clone3(struct clone_args *" cl_args ", size_t " size ); |
c10859eb | 60 | .fi |
faa0e55a MK |
61 | .PP |
62 | .IR Note : | |
63 | There is not yet a glibc wrapper for | |
64 | .BR clone3 (); | |
65 | see NOTES. | |
fea681da | 66 | .SH DESCRIPTION |
faa0e55a | 67 | These system calls |
8dd6b0bc | 68 | create a new ("child") process, in a manner similar to |
fea681da | 69 | .BR fork (2). |
efeece04 | 70 | .PP |
8dd6b0bc | 71 | By contrast with |
fea681da | 72 | .BR fork (2), |
225f5da8 | 73 | these system calls provide more precise control over what pieces of execution |
10337567 MK |
74 | context are shared between the calling process and the child process. |
75 | For example, using these system calls, the caller can control whether | |
76 | or not the two processes share the virtual address space, | |
77 | the table of file descriptors, and the table of signal handlers. | |
225f5da8 | 78 | These system calls also allow the new child process to be placed |
10337567 MK |
79 | in separate |
80 | .BR namespaces (7). | |
81 | .PP | |
82 | Note that in this manual | |
c13182ef | 83 | page, "calling process" normally corresponds to "parent process". |
a10c5a33 | 84 | But see the descriptions of |
c13182ef | 85 | .B CLONE_PARENT |
a10c5a33 MK |
86 | and |
87 | .B CLONE_THREAD | |
10337567 | 88 | below. |
efeece04 | 89 | .PP |
faa0e55a MK |
90 | This page describes the following interfaces: |
91 | .IP * 3 | |
92 | The glibc | |
93 | .BR clone () | |
94 | wrapper function and the underlying system call on which it is based. | |
95 | The main text describes the wrapper function; | |
96 | the differences for the raw system call | |
97 | are described toward the end of this page. | |
98 | .IP * | |
99 | The newer | |
100 | .BR clone3 () | |
101 | system call. | |
5261b0fe MK |
102 | .PP |
103 | In the remainder of this page, the terminology "the clone call" is used | |
324f6154 | 104 | when noting details that apply to all of these interfaces, |
faa0e55a MK |
105 | .\" |
106 | .SS The clone() wrapper function | |
107 | .PP | |
108 | When the child process is created with the | |
109 | .BR clone () | |
110 | wrapper function, | |
7495cbc7 MK |
111 | it commences execution by calling the function pointed to by the argument |
112 | .IR fn . | |
fea681da | 113 | (This differs from |
c13182ef | 114 | .BR fork (2), |
fea681da | 115 | where execution continues in the child from the point |
c13182ef MK |
116 | of the |
117 | .BR fork (2) | |
fea681da MK |
118 | call.) |
119 | The | |
fea681da | 120 | .I arg |
7495cbc7 MK |
121 | argument is passed as the argument of the function |
122 | .IR fn . | |
efeece04 | 123 | .PP |
c13182ef | 124 | When the |
fea681da | 125 | .IR fn ( arg ) |
4ba17a6d | 126 | function returns, the child process terminates. |
c13182ef | 127 | The integer returned by |
fea681da | 128 | .I fn |
4ba17a6d | 129 | is the exit status for the child process. |
c13182ef | 130 | The child process may also terminate explicitly by calling |
fea681da MK |
131 | .BR exit (2) |
132 | or after receiving a fatal signal. | |
efeece04 | 133 | .PP |
fea681da | 134 | The |
81c2368f | 135 | .I stack |
c13182ef MK |
136 | argument specifies the location of the stack used by the child process. |
137 | Since the child and calling process may share memory, | |
fea681da | 138 | it is not possible for the child process to execute in the |
c13182ef MK |
139 | same stack as the calling process. |
140 | The calling process must therefore | |
fea681da MK |
141 | set up memory space for the child stack and pass a pointer to this |
142 | space to | |
edcc65ff | 143 | .BR clone (). |
5fab2e7c | 144 | Stacks grow downward on all processors that run Linux |
fea681da | 145 | (except the HP PA processors), so |
81c2368f | 146 | .I stack |
fea681da MK |
147 | usually points to the topmost address of the memory space set up for |
148 | the child stack. | |
faa0e55a MK |
149 | Note that |
150 | .BR clone () | |
151 | does not provide a means whereby the caller can inform the kernel of the | |
152 | size of the stack area. | |
153 | .PP | |
154 | The remaining arguments to | |
155 | .BR clone () | |
156 | are discussed below. | |
157 | .\" | |
158 | .SS clone3() | |
159 | .PP | |
160 | The | |
161 | .BR clone3 () | |
162 | system call provides a superset of the functionality of the older | |
163 | .BR clone () | |
164 | interface. | |
165 | It also provides a number of API improvements, including: | |
166 | space for additional flags bits; | |
167 | cleaner separation in the use of various arguments; | |
168 | and the ability to specify the size of the child's stack area. | |
169 | .PP | |
170 | As with | |
171 | .BR fork (2), | |
172 | .BR clone3 () | |
173 | returns in both the parent and the child. | |
174 | It returns 0 in the child process and returns the PID of the child | |
175 | in the parent. | |
176 | .PP | |
177 | The | |
178 | .I cl_args | |
179 | argument of | |
180 | .BR clone3 () | |
181 | is a structure of the following form: | |
182 | .PP | |
183 | .in +4n | |
184 | .EX | |
185 | struct clone_args { | |
186 | u64 flags; /* Flags bit mask */ | |
187 | u64 pidfd; /* Where to store PID file descriptor | |
be66dbc7 | 188 | (\fIpid_t *\fP) */ |
faa0e55a | 189 | u64 child_tid; /* Where to store child TID, |
be66dbc7 | 190 | in child's memory (\fIpid_t *\fP) */ |
faa0e55a MK |
191 | u64 parent_tid; /* Where to store child TID, |
192 | in parent's memory (\fIint *\fP) */ | |
193 | u64 exit_signal; /* Signal to deliver to parent on | |
194 | child termination */ | |
195 | u64 stack; /* Pointer to lowest byte of stack */ | |
196 | u64 stack_size; /* Size of stack */ | |
197 | u64 tls; /* Location of new TLS */ | |
f7d5e082 MK |
198 | u64 set_tid; /* Pointer to a \fIpid_t\fP array |
199 | (since Linux 5.5) */ | |
200 | u64 set_tid_size; /* Number of elements in \fIset_tid\fP | |
201 | (since Linux 5.5) */ | |
202 | u64 cgroup; /* Target cgroup file descriptor for the | |
203 | child process (since Linux 5.7) */ | |
faa0e55a MK |
204 | }; |
205 | .EE | |
206 | .in | |
207 | .PP | |
208 | The | |
209 | .I size | |
210 | argument that is supplied to | |
211 | .BR clone3 () | |
212 | should be initialized to the size of this structure. | |
213 | (The existence of the | |
214 | .I size | |
215 | argument permits future extensions to the | |
216 | .IR clone_args | |
217 | structure.) | |
218 | .PP | |
219 | The stack for the child process is specified via | |
220 | .IR cl_args.stack , | |
221 | which points to the lowest byte of the stack area, | |
222 | and | |
223 | .IR cl_args.stack_size , | |
224 | which specifies the size of the stack in bytes. | |
225 | In the case where the | |
226 | .BR CLONE_VM | |
227 | flag (see below) is specified, a stack must be explicitly allocated | |
228 | and specified. | |
229 | Otherwise, these two fields can be specified as NULL and 0, | |
230 | which causes the child to use the same stack area as the parent | |
231 | (in the child's own virtual address space). | |
232 | .PP | |
233 | The remaining fields in the | |
234 | .I cl_args | |
235 | argument are discussed below. | |
236 | .\" | |
237 | .SS Equivalence between clone() and clone3() arguments | |
238 | .PP | |
239 | Unlike the older | |
240 | .BR clone () | |
241 | interface, where arguments are passed individually, in the newer | |
242 | .BR clone3 () | |
243 | interface the arguments are packaged into the | |
244 | .I clone_args | |
245 | structure shown above. | |
246 | This structure allows for a superset of the information passed via the | |
247 | .BR clone () | |
248 | arguments. | |
249 | .PP | |
250 | The following table shows the equivalence between the arguments of | |
251 | .BR clone () | |
252 | and the fields in the | |
253 | .I clone_args | |
254 | argument supplied to | |
255 | .BR clone3 (): | |
256 | .RS | |
257 | .TS | |
258 | lb lb lb | |
259 | l l l | |
260 | li li l. | |
97883fae | 261 | clone() clone3() Notes |
faa0e55a | 262 | \fIcl_args\fP field |
f5d5180f | 263 | flags & ~0xff flags For most flags; details below |
faa0e55a MK |
264 | parent_tid pidfd See CLONE_PIDFD |
265 | child_tid child_tid See CLONE_CHILD_SETTID | |
266 | parent_tid parent_tid See CLONE_PARENT_SETTID | |
267 | flags & 0xff exit_signal | |
268 | stack stack | |
269 | \fP---\fP stack_size | |
270 | tls tls See CLONE_SETTLS | |
bf031aaa AR |
271 | \fP---\fP set_tid See below for details |
272 | \fP---\fP set_tid_size | |
faa0e55a MK |
273 | .TE |
274 | .RE | |
5fbce8f2 MK |
275 | .\" |
276 | .SS The child termination signal | |
efeece04 | 277 | .PP |
faa0e55a MK |
278 | When the child process terminates, a signal may be sent to the parent. |
279 | The termination signal is specified in the low byte of | |
fea681da | 280 | .I flags |
faa0e55a MK |
281 | .RB ( clone ()) |
282 | or in | |
283 | .I cl_args.exit_signal | |
284 | .RB ( clone3 ()). | |
fd8a5be4 | 285 | If this signal is specified as anything other than |
fea681da MK |
286 | .BR SIGCHLD , |
287 | then the parent process must specify the | |
c13182ef MK |
288 | .B __WALL |
289 | or | |
fea681da | 290 | .B __WCLONE |
c13182ef MK |
291 | options when waiting for the child with |
292 | .BR wait (2). | |
faa0e55a | 293 | If no signal (i.e., zero) is specified, then the parent process is not signaled |
fea681da | 294 | when the child terminates. |
5fbce8f2 | 295 | .\" |
bf031aaa AR |
296 | .SS The set_tid array |
297 | .PP | |
298 | By default, the kernel chooses the next sequential PID for the new | |
299 | process in each of the PID namespaces where it is present. | |
300 | When creating a process with | |
301 | .BR clone3 (), | |
302 | the | |
303 | .I set_tid | |
b386cee3 MK |
304 | array (available since Linux 5.5) |
305 | can be used to select specific PIDs for the process in some | |
bf031aaa | 306 | or all of the PID namespaces where it is present. |
ee8bb310 | 307 | If the PID of the newly created process should be set only for the current |
bf031aaa AR |
308 | PID namespace or in the newly created PID namespace (if |
309 | .I flags | |
310 | contains | |
311 | .BR CLONE_NEWPID ) | |
312 | then the first element in the | |
313 | .I set_tid | |
314 | array has to be the desired PID and | |
315 | .I set_tid_size | |
316 | needs to be 1. | |
317 | .PP | |
318 | If the PID of the newly created process should have a certain value in | |
ee8bb310 | 319 | multiple PID namespaces, then the |
bf031aaa | 320 | .I set_tid |
09007c4b MK |
321 | array can have multiple entries. |
322 | The first entry defines the PID in the most | |
ee8bb310 MK |
323 | deeply nested PID namespace and each of the following entries contains |
324 | the PID in the | |
325 | corresponding ancestor PID namespace. | |
09007c4b | 326 | The number of PID namespaces in which a PID |
bf031aaa AR |
327 | should be set is defined by |
328 | .I set_tid_size | |
329 | which cannot be larger than the number of currently nested PID namespaces. | |
330 | .PP | |
331 | To create a process with the following PIDs in a PID namespace hierarchy: | |
332 | .RS | |
333 | .TS | |
ee8bb310 MK |
334 | lb lb lb |
335 | l l l. | |
336 | PID NS level Requested PID Notes | |
337 | 0 31496 Outermost PID namespace | |
bf031aaa | 338 | 1 42 |
ee8bb310 | 339 | 2 7 Innermost PID namespace |
bf031aaa AR |
340 | .TE |
341 | .RE | |
342 | .PP | |
343 | Set the array to: | |
344 | .PP | |
ee8bb310 | 345 | .in +4n |
bf031aaa | 346 | .EX |
ee8bb310 MK |
347 | set_tid[0] = 7; |
348 | set_tid[1] = 42; | |
349 | set_tid[2] = 31496; | |
350 | set_tid_size = 3; | |
bf031aaa | 351 | .EE |
ee8bb310 | 352 | .in |
bf031aaa AR |
353 | .PP |
354 | If only the PIDs in the two innermost PID namespaces | |
355 | need to be specified, set the array to: | |
356 | .PP | |
ee8bb310 | 357 | .in +4n |
bf031aaa | 358 | .EX |
ee8bb310 MK |
359 | set_tid[0] = 7; |
360 | set_tid[1] = 42; | |
361 | set_tid_size = 2; | |
bf031aaa | 362 | .EE |
ee8bb310 | 363 | .in |
bf031aaa AR |
364 | .PP |
365 | The PID in the PID namespaces outside the two innermost PID namespaces | |
366 | will be selected the same way as any other PID is selected. | |
367 | .PP | |
368 | The | |
369 | .I set_tid | |
370 | feature requires | |
ee8bb310 | 371 | .BR CAP_SYS_ADMIN |
bf031aaa AR |
372 | in all owning user namespaces of the target PID namespaces. |
373 | .PP | |
ee8bb310 MK |
374 | Callers may only choose a PID greater than 1 in a given PID namespace |
375 | if an | |
376 | .BR init | |
377 | process (i.e., a process with PID 1) already exists in that namespace. | |
09007c4b | 378 | Otherwise the PID |
bf031aaa AR |
379 | entry for this PID namespace must be 1. |
380 | .\" | |
16853a31 | 381 | .SS The flags mask |
efeece04 | 382 | .PP |
faa0e55a MK |
383 | Both |
384 | .BR clone () | |
385 | and | |
386 | .BR clone3 () | |
387 | allow a flags bit mask that modifies their behavior | |
388 | and allows the caller to specify what is shared between the calling process | |
389 | and the child process. | |
5261b0fe MK |
390 | This bit mask\(emthe |
391 | .I flags | |
392 | argument of | |
393 | .BR clone () | |
394 | or the | |
395 | .I cl_args.flags | |
396 | field passed to | |
397 | .BR clone3 ()\(emis | |
16853a31 MK |
398 | referred to as the |
399 | .I flags | |
400 | mask in the remainder of this page. | |
401 | .PP | |
402 | The | |
403 | .I flags | |
404 | mask is specified as a bitwise-OR of zero or more of | |
405 | the constants listed below. | |
5261b0fe | 406 | Except as noted below, these flags are available |
faa0e55a MK |
407 | (and have the same effect) in both |
408 | .BR clone () | |
409 | and | |
410 | .BR clone3 (). | |
fea681da | 411 | .TP |
f5dbc7c8 | 412 | .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)" |
e2bf1234 | 413 | Clear (zero) the child thread ID at the location pointed to by |
81c2368f | 414 | .I child_tid |
faa0e55a MK |
415 | .RB ( clone ()) |
416 | or | |
417 | .I cl_args.child_tid | |
418 | .RB ( clone3 ()) | |
f5dbc7c8 MK |
419 | in child memory when the child exits, and do a wakeup on the futex |
420 | at that address. | |
421 | The address involved may be changed by the | |
422 | .BR set_tid_address (2) | |
423 | system call. | |
424 | This is used by threading libraries. | |
425 | .TP | |
426 | .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)" | |
e2bf1234 | 427 | Store the child thread ID at the location pointed to by |
81c2368f | 428 | .I child_tid |
faa0e55a MK |
429 | .RB ( clone ()) |
430 | or | |
431 | .I cl_args.child_tid | |
432 | .RB ( clone3 ()) | |
8ef021ea | 433 | in the child's memory. |
5261b0fe | 434 | The store operation completes before the clone call |
6ab62ed8 | 435 | returns control to user space in the child process. |
5261b0fe | 436 | (Note that the store operation may not have completed before the clone call |
6ab62ed8 MK |
437 | returns in the parent process, which will be relevant if the |
438 | .BR CLONE_VM | |
439 | flag is also employed.) | |
f5dbc7c8 | 440 | .TP |
27f14b44 MK |
441 | .BR CLONE_CLEAR_SIGHAND " (since Linux 5.5)" |
442 | .\" commit b612e5df4587c934bd056bf05f4a1deca4de4f75 | |
443 | By default, signal dispositions in the child thread are the same as | |
444 | in the parent. | |
445 | If this flag is specified, | |
446 | then all signals that are handled in the parent | |
447 | are reset to their default dispositions | |
448 | .RB ( SIG_DFL ) | |
449 | in the child. | |
450 | .IP | |
451 | Specifying this flag together with | |
452 | .B CLONE_SIGHAND | |
453 | is nonsensical and disallowed. | |
454 | .TP | |
aed3cc90 CB |
455 | .BR CLONE_INTO_CGROUP " (since Linux 5.7)" |
456 | .\" commit ef2c41cf38a7559bbf91af42d5b6a4429db8fc68 | |
5c216c0f | 457 | By default, a child process is placed in the same version 2 |
aed3cc90 | 458 | cgroup as its parent. |
5c216c0f MK |
459 | If this flag is specified, the child process will be created in a |
460 | different version 2 cgroup than its parent. | |
461 | (Note that | |
aed3cc90 | 462 | .BR CLONE_INTO_CGROUP |
5c216c0f MK |
463 | has effect only for version 2 cgroups.) |
464 | .IP | |
465 | In order to place the child process in a different version 2 cgroup, | |
466 | the caller specifies | |
aed3cc90 CB |
467 | .BR CLONE_INTO_CGROUP |
468 | in | |
469 | .I cl_args.flags | |
5c216c0f | 470 | and passes a directory file descriptor (see the |
aed3cc90 CB |
471 | .BR O_DIRECTORY |
472 | flag for the | |
473 | .BR open (2) | |
474 | syscall) in the | |
5c216c0f MK |
475 | .I cl_args.cgroup |
476 | field. | |
aed3cc90 CB |
477 | The caller may also pass an |
478 | .BR O_PATH | |
479 | (see | |
480 | .BR open (2)) | |
481 | file descriptor for the target cgroup. | |
482 | Note, that all usual version 2 cgroup migration restrictions (see | |
483 | .BR cgroups (7) | |
484 | for details) apply. | |
5c216c0f | 485 | .IP |
aed3cc90 CB |
486 | Spawning a process into a cgroup different from the parent's cgroup |
487 | makes it possible for a service manager to directly spawn new | |
b19679f7 | 488 | services into dedicated cgroups. |
5c216c0f MK |
489 | This eliminates the accounting |
490 | jitter that would be caused by the new process living in the | |
491 | parent's cgroup for a short time before being | |
b19679f7 | 492 | moved into the target cgroup. |
5c216c0f MK |
493 | The |
494 | .BR CLONE_INTO_CGROUP | |
495 | flag also allows the creation of | |
496 | frozen child processes by spawning them into a frozen cgroup. | |
497 | (See | |
aed3cc90 | 498 | .BR cgroups (7) |
5c216c0f MK |
499 | for a description of the freezer controller.) |
500 | For threaded applications (or even thread implementations which | |
501 | make use of cgroups to limit individual threads), it is possible to | |
aed3cc90 CB |
502 | establish a fixed cgroup layout before spawning each thread |
503 | directly into its target cgroup. | |
504 | .TP | |
baa435c6 MK |
505 | .BR CLONE_DETACHED " (historical)" |
506 | For a while (during the Linux 2.5 development series) | |
507 | .\" added in 2.5.32; removed in 2.6.0-test4 | |
508 | there was a | |
509 | .B CLONE_DETACHED | |
510 | flag, | |
511 | which caused the parent not to receive a signal when the child terminated. | |
512 | Ultimately, the effect of this flag was subsumed under the | |
513 | .BR CLONE_THREAD | |
514 | flag and by the time Linux 2.6.0 was released, this flag had no effect. | |
515 | Starting in Linux 2.6.2, the need to give this flag together with | |
516 | .B CLONE_THREAD | |
517 | disappeared. | |
518 | .IP | |
519 | This flag is still defined, but it is usually ignored when calling | |
520 | .BR clone (). | |
521 | However, see the description of | |
522 | .BR CLONE_PIDFD | |
523 | for some exceptions. | |
524 | .TP | |
1603d6a1 | 525 | .BR CLONE_FILES " (since Linux 2.0)" |
fea681da | 526 | If |
f5dbc7c8 MK |
527 | .B CLONE_FILES |
528 | is set, the calling process and the child process share the same file | |
529 | descriptor table. | |
530 | Any file descriptor created by the calling process or by the child | |
531 | process is also valid in the other process. | |
532 | Similarly, if one of the processes closes a file descriptor, | |
533 | or changes its associated flags (using the | |
534 | .BR fcntl (2) | |
535 | .B F_SETFD | |
536 | operation), the other process is also affected. | |
8a76b19e KE |
537 | If a process sharing a file descriptor table calls |
538 | .BR execve (2), | |
539 | its file descriptor table is duplicated (unshared). | |
efeece04 | 540 | .IP |
fea681da | 541 | If |
f5dbc7c8 MK |
542 | .B CLONE_FILES |
543 | is not set, the child process inherits a copy of all file descriptors | |
5261b0fe | 544 | opened in the calling process at the time of the clone call. |
f5dbc7c8 MK |
545 | Subsequent operations that open or close file descriptors, |
546 | or change file descriptor flags, | |
547 | performed by either the calling | |
548 | process or the child process do not affect the other process. | |
db8ba2b4 | 549 | Note, however, |
839d161f MK |
550 | that the duplicated file descriptors in the child refer to the same |
551 | open file descriptions as the corresponding file descriptors | |
552 | in the calling process, | |
2433365b | 553 | and thus share file offsets and file status flags (see |
db8ba2b4 | 554 | .BR open (2)). |
fea681da | 555 | .TP |
1603d6a1 | 556 | .BR CLONE_FS " (since Linux 2.0)" |
fea681da MK |
557 | If |
558 | .B CLONE_FS | |
9ee4a2b6 | 559 | is set, the caller and the child process share the same filesystem |
c13182ef | 560 | information. |
9ee4a2b6 | 561 | This includes the root of the filesystem, the current |
c13182ef MK |
562 | working directory, and the umask. |
563 | Any call to | |
fea681da MK |
564 | .BR chroot (2), |
565 | .BR chdir (2), | |
566 | or | |
567 | .BR umask (2) | |
edcc65ff | 568 | performed by the calling process or the child process also affects the |
fea681da | 569 | other process. |
efeece04 | 570 | .IP |
c13182ef | 571 | If |
fea681da | 572 | .B CLONE_FS |
9ee4a2b6 | 573 | is not set, the child process works on a copy of the filesystem |
5261b0fe | 574 | information of the calling process at the time of the clone call. |
fea681da MK |
575 | Calls to |
576 | .BR chroot (2), | |
577 | .BR chdir (2), | |
4ba17a6d | 578 | or |
fea681da MK |
579 | .BR umask (2) |
580 | performed later by one of the processes do not affect the other process. | |
fea681da | 581 | .TP |
a4cc375e | 582 | .BR CLONE_IO " (since Linux 2.6.25)" |
11f27a1c JA |
583 | If |
584 | .B CLONE_IO | |
585 | is set, then the new process shares an I/O context with | |
586 | the calling process. | |
587 | If this flag is not set, then (as with | |
588 | .BR fork (2)) | |
589 | the new process has its own I/O context. | |
efeece04 | 590 | .IP |
11f27a1c | 591 | .\" The following based on text from Jens Axboe |
d1f84ed7 | 592 | The I/O context is the I/O scope of the disk scheduler (i.e., |
11f27a1c JA |
593 | what the I/O scheduler uses to model scheduling of a process's I/O). |
594 | If processes share the same I/O context, | |
595 | they are treated as one by the I/O scheduler. | |
596 | As a consequence, they get to share disk time. | |
597 | For some I/O schedulers, | |
598 | .\" the anticipatory and CFQ scheduler | |
599 | if two processes share an I/O context, | |
600 | they will be allowed to interleave their disk access. | |
601 | If several threads are doing I/O on behalf of the same process | |
602 | .RB ( aio_read (3), | |
603 | for instance), they should employ | |
604 | .BR CLONE_IO | |
605 | to get better I/O performance. | |
606 | .\" with CFQ and AS. | |
efeece04 | 607 | .IP |
11f27a1c JA |
608 | If the kernel is not configured with the |
609 | .B CONFIG_BLOCK | |
610 | option, this flag is a no-op. | |
611 | .TP | |
c5af0674 MK |
612 | .BR CLONE_NEWCGROUP " (since Linux 4.6)" |
613 | Create the process in a new cgroup namespace. | |
614 | If this flag is not set, then (as with | |
615 | .BR fork (2)) | |
616 | the process is created in the same cgroup namespaces as the calling process. | |
efeece04 | 617 | .IP |
c5af0674 | 618 | For further information on cgroup namespaces, see |
b9fe4bc3 | 619 | .BR cgroup_namespaces (7). |
efeece04 | 620 | .IP |
c5af0674 MK |
621 | Only a privileged process |
622 | .RB ( CAP_SYS_ADMIN ) | |
623 | can employ | |
624 | .BR CLONE_NEWCGROUP . | |
625 | .\" | |
626 | .TP | |
8722311b | 627 | .BR CLONE_NEWIPC " (since Linux 2.6.19)" |
667417b3 MK |
628 | If |
629 | .B CLONE_NEWIPC | |
630 | is set, then create the process in a new IPC namespace. | |
631 | If this flag is not set, then (as with | |
06b30458 | 632 | .BR fork (2)), |
667417b3 MK |
633 | the process is created in the same IPC namespace as |
634 | the calling process. | |
efeece04 | 635 | .IP |
981eda4a MK |
636 | For further information on IPC namespaces, see |
637 | .BR ipc_namespaces (7). | |
638 | .IP | |
ab5dd83f MK |
639 | Only a privileged process |
640 | .RB ( CAP_SYS_ADMIN ) | |
641 | can employ | |
642 | .BR CLONE_NEWIPC . | |
667417b3 MK |
643 | This flag can't be specified in conjunction with |
644 | .BR CLONE_SYSVSEM . | |
645 | .TP | |
163bf178 | 646 | .BR CLONE_NEWNET " (since Linux 2.6.24)" |
33a0ccb2 | 647 | (The implementation of this flag was completed only |
9108d867 | 648 | by about kernel version 2.6.29.) |
efeece04 | 649 | .IP |
163bf178 MK |
650 | If |
651 | .B CLONE_NEWNET | |
652 | is set, then create the process in a new network namespace. | |
653 | If this flag is not set, then (as with | |
57ef8c39 | 654 | .BR fork (2)) |
163bf178 MK |
655 | the process is created in the same network namespace as |
656 | the calling process. | |
efeece04 | 657 | .IP |
73680728 | 658 | For further information on network namespaces, see |
40002795 | 659 | .BR network_namespaces (7). |
efeece04 | 660 | .IP |
ab5dd83f MK |
661 | Only a privileged process |
662 | .RB ( CAP_SYS_ADMIN ) | |
663 | can employ | |
664 | .BR CLONE_NEWNET . | |
163bf178 | 665 | .TP |
c10859eb | 666 | .BR CLONE_NEWNS " (since Linux 2.4.19)" |
3dd2331c MK |
667 | If |
668 | .B CLONE_NEWNS | |
669 | is set, the cloned child is started in a new mount namespace, | |
670 | initialized with a copy of the namespace of the parent. | |
671 | If | |
fea681da | 672 | .B CLONE_NEWNS |
3dd2331c | 673 | is not set, the child lives in the same mount |
4df2eb09 | 674 | namespace as the parent. |
efeece04 | 675 | .IP |
981eda4a MK |
676 | For further information on mount namespaces, see |
677 | .BR namespaces (7) | |
678 | and | |
679 | .BR mount_namespaces (7). | |
680 | .IP | |
ab5dd83f MK |
681 | Only a privileged process |
682 | .RB ( CAP_SYS_ADMIN ) | |
683 | can employ | |
684 | .BR CLONE_NEWNS . | |
fea681da MK |
685 | It is not permitted to specify both |
686 | .B CLONE_NEWNS | |
687 | and | |
688 | .B CLONE_FS | |
9219d208 | 689 | .\" See https://lwn.net/Articles/543273/ |
5261b0fe | 690 | in the same clone call. |
9d005472 MK |
691 | .TP |
692 | .BR CLONE_NEWPID " (since Linux 2.6.24)" | |
693 | .\" This explanation draws a lot of details from | |
694 | .\" http://lwn.net/Articles/259217/ | |
695 | .\" Authors: Pavel Emelyanov <xemul@openvz.org> | |
696 | .\" and Kir Kolyshkin <kir@openvz.org> | |
697 | .\" | |
698 | .\" The primary kernel commit is 30e49c263e36341b60b735cbef5ca37912549264 | |
699 | .\" Author: Pavel Emelyanov <xemul@openvz.org> | |
700 | If | |
701 | .B CLONE_NEWPID | |
702 | is set, then create the process in a new PID namespace. | |
703 | If this flag is not set, then (as with | |
704 | .BR fork (2)) | |
705 | the process is created in the same PID namespace as | |
706 | the calling process. | |
efeece04 | 707 | .IP |
9d005472 | 708 | For further information on PID namespaces, see |
7e0e902b MK |
709 | .BR namespaces (7) |
710 | and | |
39b3f005 | 711 | .BR pid_namespaces (7). |
efeece04 | 712 | .IP |
ab5dd83f MK |
713 | Only a privileged process |
714 | .RB ( CAP_SYS_ADMIN ) | |
715 | can employ | |
716 | .BR CLONE_NEWPID . | |
9d005472 | 717 | This flag can't be specified in conjunction with |
f0007192 MK |
718 | .BR CLONE_THREAD |
719 | or | |
720 | .BR CLONE_PARENT . | |
70d21f17 | 721 | .TP |
06b30458 MK |
722 | .BR CLONE_NEWUSER |
723 | (This flag first became meaningful for | |
724 | .BR clone () | |
4d2b3ed7 MK |
725 | in Linux 2.6.23, |
726 | the current | |
11a38815 | 727 | .BR clone () |
4d2b3ed7 MK |
728 | semantics were merged in Linux 3.5, |
729 | and the final pieces to make the user namespaces completely usable were | |
730 | merged in Linux 3.8.) | |
efeece04 | 731 | .IP |
70d21f17 EB |
732 | If |
733 | .B CLONE_NEWUSER | |
06b30458 MK |
734 | is set, then create the process in a new user namespace. |
735 | If this flag is not set, then (as with | |
57ef8c39 | 736 | .BR fork (2)) |
70d21f17 | 737 | the process is created in the same user namespace as the calling process. |
efeece04 | 738 | .IP |
981eda4a MK |
739 | For further information on user namespaces, see |
740 | .BR namespaces (7) | |
741 | and | |
742 | .BR user_namespaces (7). | |
743 | .IP | |
fefbcba8 MK |
744 | Before Linux 3.8, use of |
745 | .BR CLONE_NEWUSER | |
746 | required that the caller have three capabilities: | |
747 | .BR CAP_SYS_ADMIN , | |
748 | .BR CAP_SETUID , | |
749 | and | |
750 | .BR CAP_SETGID . | |
751 | .\" Before Linux 2.6.29, it appears that only CAP_SYS_ADMIN was needed | |
06b30458 | 752 | Starting with Linux 3.8, |
9d005472 | 753 | no privileges are needed to create a user namespace. |
efeece04 | 754 | .IP |
5e72cf7d MK |
755 | This flag can't be specified in conjunction with |
756 | .BR CLONE_THREAD | |
757 | or | |
758 | .BR CLONE_PARENT . | |
759 | For security reasons, | |
760 | .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71 | |
761 | .\" https://lwn.net/Articles/543273/ | |
762 | .\" The fix actually went into 3.9 and into 3.8.3. However, user namespaces | |
763 | .\" were, for practical purposes, unusable in earlier 3.8.x because of the | |
ab3311aa | 764 | .\" various filesystems that didn't support userns. |
f0007192 MK |
765 | .BR CLONE_NEWUSER |
766 | cannot be specified in conjunction with | |
5e72cf7d | 767 | .BR CLONE_FS . |
82ee147a | 768 | .TP |
43ce9dda MK |
769 | .BR CLONE_NEWUTS " (since Linux 2.6.19)" |
770 | If | |
771 | .B CLONE_NEWUTS | |
e1b11906 MK |
772 | is set, then create the process in a new UTS namespace, |
773 | whose identifiers are initialized by duplicating the identifiers | |
774 | from the UTS namespace of the calling process. | |
43ce9dda | 775 | If this flag is not set, then (as with |
57ef8c39 | 776 | .BR fork (2)) |
43ce9dda MK |
777 | the process is created in the same UTS namespace as |
778 | the calling process. | |
efeece04 | 779 | .IP |
981eda4a MK |
780 | For further information on UTS namespaces, see |
781 | .BR uts_namespaces (7). | |
782 | .IP | |
ab5dd83f MK |
783 | Only a privileged process |
784 | .RB ( CAP_SYS_ADMIN ) | |
785 | can employ | |
786 | .BR CLONE_NEWUTS . | |
43ce9dda | 787 | .TP |
f5dbc7c8 MK |
788 | .BR CLONE_PARENT " (since Linux 2.3.12)" |
789 | If | |
790 | .B CLONE_PARENT | |
791 | is set, then the parent of the new child (as returned by | |
792 | .BR getppid (2)) | |
793 | will be the same as that of the calling process. | |
efeece04 | 794 | .IP |
f5dbc7c8 MK |
795 | If |
796 | .B CLONE_PARENT | |
797 | is not set, then (as with | |
798 | .BR fork (2)) | |
799 | the child's parent is the calling process. | |
efeece04 | 800 | .IP |
f5dbc7c8 MK |
801 | Note that it is the parent process, as returned by |
802 | .BR getppid (2), | |
803 | which is signaled when the child terminates, so that | |
804 | if | |
805 | .B CLONE_PARENT | |
806 | is set, then the parent of the calling process, rather than the | |
807 | calling process itself, will be signaled. | |
a17b9d28 | 808 | .IP |
4269a6ab | 809 | The |
a17b9d28 | 810 | .B CLONE_PARENT |
4269a6ab MK |
811 | flag can't be used in clone calls by the |
812 | global init process (PID 1 in the initial PID namespace) | |
813 | and init processes in other PID namespaces. | |
814 | This restriction prevents the creation of multi-rooted process trees | |
815 | as well as the creation of unreapable zombies in the initial PID namespace. | |
f5dbc7c8 MK |
816 | .TP |
817 | .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)" | |
e2bf1234 | 818 | Store the child thread ID at the location pointed to by |
81c2368f | 819 | .I parent_tid |
faa0e55a MK |
820 | .RB ( clone ()) |
821 | or | |
d5d482ec | 822 | .I cl_args.parent_tid |
faa0e55a | 823 | .RB ( clone3 ()) |
8ef021ea | 824 | in the parent's memory. |
f5dbc7c8 MK |
825 | (In Linux 2.5.32-2.5.48 there was a flag |
826 | .B CLONE_SETTID | |
827 | that did this.) | |
5261b0fe | 828 | The store operation completes before the clone call |
b5da2f91 | 829 | returns control to user space. |
f5dbc7c8 | 830 | .TP |
1c173eb3 | 831 | .BR CLONE_PID " (Linux 2.0 to 2.5.15)" |
f5dbc7c8 MK |
832 | If |
833 | .B CLONE_PID | |
834 | is set, the child process is created with the same process ID as | |
835 | the calling process. | |
836 | This is good for hacking the system, but otherwise | |
837 | of not much use. | |
1c173eb3 | 838 | From Linux 2.3.21 onward, this flag could be |
f5dbc7c8 | 839 | specified only by the system boot process (PID 0). |
1c173eb3 | 840 | The flag disappeared completely from the kernel sources in Linux 2.5.16. |
f5d5180f | 841 | Subsequently, the kernel silently ignored this bit if it was specified in the |
16853a31 MK |
842 | .IR flags |
843 | mask. | |
f5d5180f MK |
844 | Much later, the same bit was recycled for use as the |
845 | .B CLONE_PIDFD | |
846 | flag. | |
f5dbc7c8 | 847 | .TP |
9f938981 | 848 | .BR CLONE_PIDFD " (since Linux 5.2)" |
4e98b074 | 849 | .\" commit b3e5838252665ee4cfa76b82bdf1198dca81e5be |
faa0e55a MK |
850 | If this flag is specified, |
851 | a PID file descriptor referring to the child process is allocated | |
852 | and placed at a specified location in the parent's memory. | |
b4ebffb2 | 853 | The close-on-exec flag is set on this new file descriptor. |
34a975f8 MK |
854 | PID file descriptors can be used for the purposes described in |
855 | .BR pidfd_open (2). | |
faa0e55a MK |
856 | .RS |
857 | .IP * 3 | |
858 | When using | |
859 | .BR clone3 (), | |
860 | the PID file descriptor is placed at the location pointed to by | |
861 | .IR cl_args.pidfd . | |
862 | .IP * | |
863 | When using | |
864 | .BR clone (), | |
865 | the PID file descriptor is placed at the location pointed to by | |
866 | .IR parent_tid . | |
9f938981 | 867 | Since the |
81c2368f | 868 | .I parent_tid |
b97cc7ae | 869 | argument is used to return the PID file descriptor, |
9f938981 CB |
870 | .B CLONE_PIDFD |
871 | cannot be used with | |
faa0e55a MK |
872 | .B CLONE_PARENT_SETTID |
873 | when calling | |
874 | .BR clone (). | |
875 | .RE | |
9f938981 CB |
876 | .IP |
877 | It is currently not possible to use this flag together with | |
878 | .B CLONE_THREAD. | |
b97cc7ae | 879 | This means that the process identified by the PID file descriptor |
f6183e5b | 880 | will always be a thread group leader. |
9f938981 | 881 | .IP |
baa435c6 | 882 | If the obsolete |
9f938981 | 883 | .B CLONE_DETACHED |
baa435c6 MK |
884 | flag is specified alongside |
885 | .BR CLONE_PIDFD | |
886 | when calling | |
887 | .BR clone (), | |
4e98b074 | 888 | an error is returned. |
baa435c6 MK |
889 | An error also results if |
890 | .B CLONE_DETACHED | |
891 | is specified when calling | |
892 | .BR clone3 (). | |
893 | This error behavior ensures that the bit corresponding to | |
894 | .BR CLONE_DETACHED | |
895 | can be reused for further PID file descriptor features in the future. | |
9f938981 | 896 | .TP |
1603d6a1 | 897 | .BR CLONE_PTRACE " (since Linux 2.2)" |
f5dbc7c8 MK |
898 | If |
899 | .B CLONE_PTRACE | |
900 | is specified, and the calling process is being traced, | |
901 | then trace the child also (see | |
902 | .BR ptrace (2)). | |
903 | .TP | |
904 | .BR CLONE_SETTLS " (since Linux 2.5.32)" | |
dd6d3d2e | 905 | The TLS (Thread Local Storage) descriptor is set to |
81c2368f | 906 | .IR tls . |
efeece04 | 907 | .IP |
dd6d3d2e | 908 | The interpretation of |
81c2368f | 909 | .I tls |
dd6d3d2e KF |
910 | and the resulting effect is architecture dependent. |
911 | On x86, | |
81c2368f | 912 | .I tls |
dd6d3d2e | 913 | is interpreted as a |
2551f801 | 914 | .IR "struct user_desc\ *" |
35bf8cb4 | 915 | (see |
dd6d3d2e | 916 | .BR set_thread_area (2)). |
9ea5bc66 | 917 | On x86-64 it is the new value to be set for the %fs base register |
35bf8cb4 | 918 | (see the |
2551f801 | 919 | .B ARCH_SET_FS |
dd6d3d2e KF |
920 | argument to |
921 | .BR arch_prctl (2)). | |
922 | On architectures with a dedicated TLS register, it is the new value | |
923 | of that register. | |
f5d5180f MK |
924 | .IP |
925 | Use of this flag requires detailed knowledge and generally it | |
926 | should not be used except in libraries implementing threading. | |
f5dbc7c8 | 927 | .TP |
1603d6a1 | 928 | .BR CLONE_SIGHAND " (since Linux 2.0)" |
fea681da MK |
929 | If |
930 | .B CLONE_SIGHAND | |
314c8ff4 | 931 | is set, the calling process and the child process share the same table of |
c13182ef MK |
932 | signal handlers. |
933 | If the calling process or child process calls | |
fea681da | 934 | .BR sigaction (2) |
c13182ef MK |
935 | to change the behavior associated with a signal, the behavior is |
936 | changed in the other process as well. | |
937 | However, the calling process and child | |
fea681da | 938 | processes still have distinct signal masks and sets of pending |
c13182ef | 939 | signals. |
4ba17a6d | 940 | So, one of them may block or unblock signals using |
fea681da MK |
941 | .BR sigprocmask (2) |
942 | without affecting the other process. | |
efeece04 | 943 | .IP |
fea681da MK |
944 | If |
945 | .B CLONE_SIGHAND | |
946 | is not set, the child process inherits a copy of the signal handlers | |
5261b0fe | 947 | of the calling process at the time of the clone call. |
c13182ef | 948 | Calls to |
fea681da MK |
949 | .BR sigaction (2) |
950 | performed later by one of the processes have no effect on the other | |
951 | process. | |
efeece04 | 952 | .IP |
d6bec36e MK |
953 | Since Linux 2.6.0, |
954 | .\" Precisely: Linux 2.6.0-test6 | |
16853a31 | 955 | the |
29546c24 | 956 | .I flags |
16853a31 | 957 | mask must also include |
29546c24 MK |
958 | .B CLONE_VM |
959 | if | |
960 | .B CLONE_SIGHAND | |
961 | is specified | |
fea681da | 962 | .TP |
d6bec36e MK |
963 | .BR CLONE_STOPPED " (since Linux 2.6.0)" |
964 | .\" Precisely: Linux 2.6.0-test2 | |
a69b6bda MK |
965 | If |
966 | .B CLONE_STOPPED | |
967 | is set, then the child is initially stopped (as though it was sent a | |
968 | .B SIGSTOP | |
969 | signal), and must be resumed by sending it a | |
970 | .B SIGCONT | |
971 | signal. | |
efeece04 | 972 | .IP |
a60450a9 MK |
973 | This flag was |
974 | .I deprecated | |
975 | from Linux 2.6.25 onward, | |
976 | and was | |
977 | .I removed | |
28b44abc MK |
978 | altogether in Linux 2.6.38. |
979 | Since then, the kernel silently ignores it without error. | |
a5a061ee | 980 | .\" glibc 2.8 removed this defn from bits/sched.h |
c5af0674 MK |
981 | Starting with Linux 4.6, the same bit was reused for the |
982 | .BR CLONE_NEWCGROUP | |
983 | flag. | |
a69b6bda | 984 | .TP |
f5dbc7c8 | 985 | .BR CLONE_SYSVSEM " (since Linux 2.5.10)" |
fea681da | 986 | If |
f5dbc7c8 MK |
987 | .B CLONE_SYSVSEM |
988 | is set, then the child and the calling process share | |
5ada4b94 MK |
989 | a single list of System V semaphore adjustment |
990 | .RI ( semadj ) | |
991 | values (see | |
f5dbc7c8 | 992 | .BR semop (2)). |
5ada4b94 MK |
993 | In this case, the shared list accumulates |
994 | .I semadj | |
995 | values across all processes sharing the list, | |
996 | and semaphore adjustments are performed only when the last process | |
997 | that is sharing the list terminates (or ceases sharing the list using | |
998 | .BR unshare (2)). | |
f5d401dd | 999 | If this flag is not set, then the child has a separate |
5ada4b94 MK |
1000 | .I semadj |
1001 | list that is initially empty. | |
fea681da | 1002 | .TP |
d6bec36e MK |
1003 | .BR CLONE_THREAD " (since Linux 2.4.0)" |
1004 | .\" Precisely: Linux 2.6.0-test8 | |
fea681da MK |
1005 | If |
1006 | .B CLONE_THREAD | |
1007 | is set, the child is placed in the same thread group as the calling process. | |
fd8a5be4 MK |
1008 | To make the remainder of the discussion of |
1009 | .B CLONE_THREAD | |
1010 | more readable, the term "thread" is used to refer to the | |
1011 | processes within a thread group. | |
efeece04 | 1012 | .IP |
fd8a5be4 MK |
1013 | Thread groups were a feature added in Linux 2.4 to support the |
1014 | POSIX threads notion of a set of threads that share a single PID. | |
1015 | Internally, this shared PID is the so-called | |
1016 | thread group identifier (TGID) for the thread group. | |
c13182ef | 1017 | Since Linux 2.4, calls to |
fea681da | 1018 | .BR getpid (2) |
fd8a5be4 | 1019 | return the TGID of the caller. |
efeece04 | 1020 | .IP |
fd8a5be4 MK |
1021 | The threads within a group can be distinguished by their (system-wide) |
1022 | unique thread IDs (TID). | |
1023 | A new thread's TID is available as the function result | |
5261b0fe | 1024 | returned to the caller, |
fd8a5be4 MK |
1025 | and a thread can obtain |
1026 | its own TID using | |
1027 | .BR gettid (2). | |
efeece04 | 1028 | .IP |
5261b0fe | 1029 | When a clone call is made without specifying |
fd8a5be4 MK |
1030 | .BR CLONE_THREAD , |
1031 | then the resulting thread is placed in a new thread group | |
1032 | whose TGID is the same as the thread's TID. | |
1033 | This thread is the | |
1034 | .I leader | |
1035 | of the new thread group. | |
efeece04 | 1036 | .IP |
fd8a5be4 MK |
1037 | A new thread created with |
1038 | .B CLONE_THREAD | |
5261b0fe | 1039 | has the same parent process as the process that made the clone call |
c13182ef | 1040 | (i.e., like |
fd8a5be4 MK |
1041 | .BR CLONE_PARENT ), |
1042 | so that calls to | |
1043 | .BR getppid (2) | |
1044 | return the same value for all of the threads in a thread group. | |
1045 | When a | |
c13182ef | 1046 | .B CLONE_THREAD |
5261b0fe | 1047 | thread terminates, the thread that created it is not sent a |
fd8a5be4 MK |
1048 | .B SIGCHLD |
1049 | (or other termination) signal; | |
1050 | nor can the status of such a thread be obtained | |
1051 | using | |
1052 | .BR wait (2). | |
1053 | (The thread is said to be | |
1054 | .IR detached .) | |
efeece04 | 1055 | .IP |
e2fbf61d MK |
1056 | After all of the threads in a thread group terminate |
1057 | the parent process of the thread group is sent a | |
fd8a5be4 MK |
1058 | .B SIGCHLD |
1059 | (or other termination) signal. | |
efeece04 | 1060 | .IP |
fd8a5be4 MK |
1061 | If any of the threads in a thread group performs an |
1062 | .BR execve (2), | |
1063 | then all threads other than the thread group leader are terminated, | |
1064 | and the new program is executed in the thread group leader. | |
efeece04 | 1065 | .IP |
f7110f60 MK |
1066 | If one of the threads in a thread group creates a child using |
1067 | .BR fork (2), | |
1068 | then any thread in the group can | |
1069 | .BR wait (2) | |
1070 | for that child. | |
efeece04 | 1071 | .IP |
16853a31 | 1072 | Since Linux 2.5.35, the |
fd8a5be4 | 1073 | .I flags |
16853a31 | 1074 | mask must also include |
fd8a5be4 MK |
1075 | .B CLONE_SIGHAND |
1076 | if | |
1077 | .B CLONE_THREAD | |
6fd69f33 | 1078 | is specified |
d6bec36e MK |
1079 | (and note that, since Linux 2.6.0, |
1080 | .\" Precisely: Linux 2.6.0-test6 | |
6fd69f33 MK |
1081 | .BR CLONE_SIGHAND |
1082 | also requires | |
1083 | .BR CLONE_VM | |
1084 | to be included). | |
efeece04 | 1085 | .IP |
e2fbf61d MK |
1086 | Signal dispositions and actions are process-wide: |
1087 | if an unhandled signal is delivered to a thread, then | |
1088 | it will affect (terminate, stop, continue, be ignored in) | |
1089 | all members of the thread group. | |
efeece04 | 1090 | .IP |
99408a60 | 1091 | Each thread has its own signal mask, as set by |
f957eebd MK |
1092 | .BR sigprocmask (2). |
1093 | .IP | |
1094 | A signal may be process-directed or thread-directed. | |
1095 | A process-directed signal is targeted at a thread group (i.e., a TGID), | |
1096 | and is delivered to an arbitrarily selected thread from among those | |
1097 | that are not blocking the signal. | |
ed4f87f0 | 1098 | A signal may be process-directed because it was generated by the kernel |
f957eebd MK |
1099 | for reasons other than a hardware exception, or because it was sent using |
1100 | .BR kill (2) | |
1101 | or | |
1102 | .BR sigqueue (3). | |
1103 | A thread-directed signal is targeted at (i.e., delivered to) | |
1104 | a specific thread. | |
1105 | A signal may be thread directed because it was sent using | |
1106 | .BR tgkill (2) | |
1107 | or | |
1108 | .BR pthread_sigqueue (3), | |
1109 | or because the thread executed a machine language instruction that triggered | |
1110 | a hardware exception | |
1111 | (e.g., invalid memory access triggering | |
1112 | .BR SIGSEGV | |
1113 | or a floating-point exception triggering | |
1114 | .BR SIGFPE ). | |
1115 | .IP | |
99408a60 MK |
1116 | A call to |
1117 | .BR sigpending (2) | |
f957eebd MK |
1118 | returns a signal set that is the union of the pending process-directed |
1119 | signals and the signals that are pending for the calling thread. | |
efeece04 | 1120 | .IP |
475c2753 | 1121 | If a process-directed signal is delivered to a thread group, |
e2fbf61d MK |
1122 | and the thread group has installed a handler for the signal, then |
1123 | the handler will be invoked in exactly one, arbitrarily selected | |
1124 | member of the thread group that has not blocked the signal. | |
c13182ef | 1125 | If multiple threads in a group are waiting to accept the same signal using |
e2fbf61d MK |
1126 | .BR sigwaitinfo (2), |
1127 | the kernel will arbitrarily select one of these threads | |
475c2753 | 1128 | to receive the signal. |
a69b6bda | 1129 | .TP |
f5dbc7c8 | 1130 | .BR CLONE_UNTRACED " (since Linux 2.5.46)" |
a69b6bda | 1131 | If |
f5dbc7c8 MK |
1132 | .B CLONE_UNTRACED |
1133 | is specified, then a tracing process cannot force | |
1134 | .B CLONE_PTRACE | |
1135 | on this child process. | |
fea681da | 1136 | .TP |
1603d6a1 | 1137 | .BR CLONE_VFORK " (since Linux 2.2)" |
f5dbc7c8 MK |
1138 | If |
1139 | .B CLONE_VFORK | |
1140 | is set, the execution of the calling process is suspended | |
1141 | until the child releases its virtual memory | |
1142 | resources via a call to | |
1143 | .BR execve (2) | |
1144 | or | |
1145 | .BR _exit (2) | |
1146 | (as with | |
1147 | .BR vfork (2)). | |
efeece04 | 1148 | .IP |
f5dbc7c8 MK |
1149 | If |
1150 | .B CLONE_VFORK | |
4b4a853a | 1151 | is not set, then both the calling process and the child are schedulable |
f5dbc7c8 MK |
1152 | after the call, and an application should not rely on execution occurring |
1153 | in any particular order. | |
fea681da | 1154 | .TP |
1603d6a1 | 1155 | .BR CLONE_VM " (since Linux 2.0)" |
f5dbc7c8 MK |
1156 | If |
1157 | .B CLONE_VM | |
1158 | is set, the calling process and the child process run in the same memory | |
1159 | space. | |
1160 | In particular, memory writes performed by the calling process | |
1161 | or by the child process are also visible in the other process. | |
1162 | Moreover, any memory mapping or unmapping performed with | |
1163 | .BR mmap (2) | |
1164 | or | |
1165 | .BR munmap (2) | |
1166 | by the child or calling process also affects the other process. | |
efeece04 | 1167 | .IP |
f5dbc7c8 MK |
1168 | If |
1169 | .B CLONE_VM | |
1170 | is not set, the child process runs in a separate copy of the memory | |
5261b0fe | 1171 | space of the calling process at the time of the clone call. |
f5dbc7c8 MK |
1172 | Memory writes or file mappings/unmappings performed by one of the |
1173 | processes do not affect the other, as with | |
1174 | .BR fork (2). | |
1874193e | 1175 | .SH NOTES |
b7cf324f | 1176 | .PP |
faa0e55a | 1177 | One use of these systems calls |
b7cf324f MK |
1178 | is to implement threads: multiple flows of control in a program that |
1179 | run concurrently in a shared address space. | |
1180 | .PP | |
faa0e55a | 1181 | Glibc does not provide a wrapper for |
5261b0fe | 1182 | .BR clone3 (); |
faa0e55a MK |
1183 | call it using |
1184 | .BR syscall (2). | |
1185 | .PP | |
1c6ebc4b MK |
1186 | Note that the glibc |
1187 | .BR clone () | |
1188 | wrapper function makes some changes | |
1189 | in the memory pointed to by | |
81c2368f | 1190 | .I stack |
1c6ebc4b MK |
1191 | (changes required to set the stack up correctly for the child) |
1192 | .I before | |
1193 | invoking the | |
1194 | .BR clone () | |
1195 | system call. | |
1196 | So, in cases where | |
1197 | .BR clone () | |
1198 | is used to recursively create children, | |
1199 | do not use the buffer employed for the parent's stack | |
1200 | as the stack of the child. | |
1201 | .\" | |
0722a578 | 1202 | .SS C library/kernel differences |
e585064b MK |
1203 | The raw |
1204 | .BR clone () | |
fea681da MK |
1205 | system call corresponds more closely to |
1206 | .BR fork (2) | |
1207 | in that execution in the child continues from the point of the | |
c13182ef | 1208 | call. |
5add3af3 MK |
1209 | As such, the |
1210 | .I fn | |
c13182ef | 1211 | and |
5add3af3 MK |
1212 | .I arg |
1213 | arguments of the | |
1214 | .BR clone () | |
1215 | wrapper function are omitted. | |
161fce30 | 1216 | .PP |
f5d5180f | 1217 | In contrast to the glibc wrapper, the raw |
d35f5c34 | 1218 | .BR clone () |
f5d5180f | 1219 | system call accepts NULL as a |
81c2368f | 1220 | .I stack |
f5d5180f MK |
1221 | argument (and |
1222 | .BR clone3 () | |
1223 | likewise allows | |
1224 | .I cl_args.stack | |
1225 | to be NULL). | |
1226 | In this case, the child uses a duplicate of the parent's stack. | |
b219e68c MK |
1227 | (Copy-on-write semantics ensure that the child gets separate copies |
1228 | of stack pages when either process modifies the stack.) | |
1229 | In this case, for correct operation, the | |
1230 | .B CLONE_VM | |
1231 | option should not be specified. | |
d35f5c34 MK |
1232 | (If the child |
1233 | .I shares | |
1234 | the parent's memory because of the use of the | |
1235 | .BR CLONE_VM | |
b219e68c MK |
1236 | flag, |
1237 | then no copy-on-write duplication occurs and chaos is likely to result.) | |
d35f5c34 | 1238 | .PP |
161fce30 MK |
1239 | The order of the arguments also differs in the raw system call, |
1240 | and there are variations in the arguments across architectures, | |
1241 | as detailed in the following paragraphs. | |
efeece04 | 1242 | .PP |
2a15a76b | 1243 | The raw system call interface on x86-64 and some other architectures |
f5d5180f | 1244 | (including sh, tile, and alpha) is: |
efeece04 | 1245 | .PP |
5add3af3 | 1246 | .in +4 |
b76974c1 | 1247 | .EX |
81c2368f MK |
1248 | .BI "long clone(unsigned long " flags ", void *" stack , |
1249 | .BI " int *" parent_tid ", int *" child_tid , | |
1250 | .BI " unsigned long " tls ); | |
b76974c1 | 1251 | .EE |
2a15a76b | 1252 | .in |
efeece04 | 1253 | .PP |
2a15a76b MK |
1254 | On x86-32, and several other common architectures |
1255 | (including score, ARM, ARM 64, PA-RISC, arc, Power PC, xtensa, | |
1256 | and MIPS), | |
1257 | .\" CONFIG_CLONE_BACKWARDS | |
1258 | the order of the last two arguments is reversed: | |
efeece04 | 1259 | .PP |
2a15a76b | 1260 | .in +4 |
b76974c1 | 1261 | .EX |
81c2368f MK |
1262 | .BI "long clone(unsigned long " flags ", void *" stack , |
1263 | .BI " int *" parent_tid ", unsigned long " tls , | |
1264 | .BI " int *" child_tid ); | |
b76974c1 | 1265 | .EE |
2a15a76b | 1266 | .in |
efeece04 | 1267 | .PP |
2a15a76b MK |
1268 | On the cris and s390 architectures, |
1269 | .\" CONFIG_CLONE_BACKWARDS2 | |
1270 | the order of the first two arguments is reversed: | |
efeece04 | 1271 | .PP |
2a15a76b | 1272 | .in +4 |
b76974c1 | 1273 | .EX |
81c2368f MK |
1274 | .BI "long clone(void *" stack ", unsigned long " flags , |
1275 | .BI " int *" parent_tid ", int *" child_tid , | |
1276 | .BI " unsigned long " tls ); | |
b76974c1 | 1277 | .EE |
2a15a76b | 1278 | .in |
efeece04 | 1279 | .PP |
2a15a76b MK |
1280 | On the microblaze architecture, |
1281 | .\" CONFIG_CLONE_BACKWARDS3 | |
1282 | an additional argument is supplied: | |
efeece04 | 1283 | .PP |
2a15a76b | 1284 | .in +4 |
b76974c1 | 1285 | .EX |
81c2368f | 1286 | .BI "long clone(unsigned long " flags ", void *" stack , |
2a15a76b | 1287 | .BI " int " stack_size , "\fR /* Size of stack */" |
81c2368f MK |
1288 | .BI " int *" parent_tid ", int *" child_tid , |
1289 | .BI " unsigned long " tls ); | |
b76974c1 | 1290 | .EE |
5add3af3 | 1291 | .in |
2a15a76b | 1292 | .\" |
251113d0 | 1293 | .SS blackfin, m68k, and sparc |
2a15a76b MK |
1294 | .\" Mike Frysinger noted in a 2013 mail: |
1295 | .\" these arches don't define __ARCH_WANT_SYS_CLONE: | |
1296 | .\" blackfin ia64 m68k sparc | |
251113d0 | 1297 | The argument-passing conventions on |
04346be5 | 1298 | blackfin, m68k, and sparc are different from the descriptions above. |
251113d0 | 1299 | For details, see the kernel (and glibc) source. |
574c92b6 | 1300 | .SS ia64 |
097a1f3b | 1301 | On ia64, a different interface is used: |
7a346077 | 1302 | .PP |
8016cc90 ES |
1303 | .in +4 |
1304 | .EX | |
097a1f3b | 1305 | .BI "int __clone2(int (*" "fn" ")(void *), " |
81c2368f | 1306 | .BI " void *" stack_base ", size_t " stack_size , |
097a1f3b | 1307 | .BI " int " flags ", void *" "arg" ", ... " |
81c2368f MK |
1308 | .BI " /* pid_t *" parent_tid ", struct user_desc *" tls , |
1309 | .BI " pid_t *" child_tid " */ );" | |
8016cc90 ES |
1310 | .EE |
1311 | .in | |
097a1f3b | 1312 | .PP |
9b9c895c MK |
1313 | The prototype shown above is for the glibc wrapper function; |
1314 | for the system call itself, | |
1315 | the prototype can be described as follows (it is identical to the | |
8016cc90 ES |
1316 | .BR clone () |
1317 | prototype on microblaze): | |
1318 | .PP | |
1319 | .in +4 | |
1320 | .EX | |
81c2368f | 1321 | .BI "long clone2(unsigned long " flags ", void *" stack_base , |
8016cc90 | 1322 | .BI " int " stack_size , "\fR /* Size of stack */" |
81c2368f | 1323 | .BI " int *" parent_tid ", int *" child_tid , |
8016cc90 ES |
1324 | .BI " unsigned long " tls ); |
1325 | .EE | |
1326 | .in | |
097a1f3b MK |
1327 | .PP |
1328 | .BR __clone2 () | |
1329 | operates in the same way as | |
1330 | .BR clone (), | |
1331 | except that | |
81c2368f | 1332 | .I stack_base |
097a1f3b MK |
1333 | points to the lowest address of the child's stack area, |
1334 | and | |
1335 | .I stack_size | |
1336 | specifies the size of the stack pointed to by | |
81c2368f | 1337 | .IR stack_base . |
5add3af3 | 1338 | .SS Linux 2.4 and earlier |
577f9b62 MK |
1339 | In Linux 2.4 and earlier, |
1340 | .BR clone () | |
1341 | does not take arguments | |
81c2368f | 1342 | .IR parent_tid , |
577f9b62 MK |
1343 | .IR tls , |
1344 | and | |
81c2368f | 1345 | .IR child_tid . |
47297adb | 1346 | .SH RETURN VALUE |
0bfa087b MK |
1347 | .\" gettid(2) returns current->pid; |
1348 | .\" getpid(2) returns current->tgid; | |
fea681da | 1349 | On success, the thread ID of the child process is returned |
c13182ef | 1350 | in the caller's thread of execution. |
84811e86 | 1351 | On failure, \-1 is returned |
fea681da MK |
1352 | in the caller's context, no child process will be created, and |
1353 | .I errno | |
1354 | will be set appropriately. | |
fea681da MK |
1355 | .SH ERRORS |
1356 | .TP | |
1357 | .B EAGAIN | |
e1b6e186 MK |
1358 | Too many processes are already running; see |
1359 | .BR fork (2). | |
fea681da | 1360 | .TP |
bf031aaa | 1361 | .BR EEXIST " (" clone3 "() only)" |
ee8bb310 | 1362 | One (or more) of the PIDs specified in |
bf031aaa AR |
1363 | .I set_tid |
1364 | already exists in the corresponding PID namespace. | |
1365 | .TP | |
fea681da | 1366 | .B EINVAL |
27f14b44 MK |
1367 | Both |
1368 | .B CLONE_SIGHAND | |
1369 | and | |
1370 | .B CLONE_CLEAR_SIGHAND | |
1371 | were specified in the | |
1372 | .I flags | |
1373 | mask. | |
1374 | .TP | |
1375 | .B EINVAL | |
fea681da | 1376 | .B CLONE_SIGHAND |
16853a31 MK |
1377 | was specified in the |
1378 | .I flags | |
1379 | mask, but | |
fea681da | 1380 | .B CLONE_VM |
2e8a7fb3 | 1381 | was not. |
d6bec36e MK |
1382 | (Since Linux 2.6.0.) |
1383 | .\" Precisely: Linux 2.6.0-test6 | |
fea681da MK |
1384 | .TP |
1385 | .B EINVAL | |
1386 | .B CLONE_THREAD | |
16853a31 MK |
1387 | was specified in the |
1388 | .I flags | |
1389 | mask, but | |
fea681da | 1390 | .B CLONE_SIGHAND |
6387216b MK |
1391 | was not. |
1392 | (Since Linux 2.5.35.) | |
29546c24 MK |
1393 | .\" .TP |
1394 | .\" .B EINVAL | |
1395 | .\" Precisely one of | |
1396 | .\" .B CLONE_DETACHED | |
1397 | .\" and | |
1398 | .\" .B CLONE_THREAD | |
6387216b MK |
1399 | .\" was specified. |
1400 | .\" (Since Linux 2.6.0-test6.) | |
fea681da MK |
1401 | .TP |
1402 | .B EINVAL | |
d6868c69 | 1403 | .B CLONE_THREAD |
16853a31 MK |
1404 | was specified in the |
1405 | .I flags | |
1406 | mask, but the current process previously called | |
d6868c69 JH |
1407 | .BR unshare (2) |
1408 | with the | |
1409 | .B CLONE_NEWPID | |
1410 | flag or used | |
1411 | .BR setns (2) | |
1412 | to reassociate itself with a PID namespace. | |
1413 | .TP | |
1414 | .B EINVAL | |
d34e5645 | 1415 | .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71 |
fea681da MK |
1416 | Both |
1417 | .B CLONE_FS | |
1418 | and | |
1419 | .B CLONE_NEWNS | |
16853a31 MK |
1420 | were specified in the |
1421 | .IR flags | |
1422 | mask. | |
fea681da | 1423 | .TP |
d34e5645 MK |
1424 | .BR EINVAL " (since Linux 3.9)" |
1425 | Both | |
1426 | .B CLONE_NEWUSER | |
1427 | and | |
1428 | .B CLONE_FS | |
16853a31 MK |
1429 | were specified in the |
1430 | .IR flags | |
1431 | mask. | |
d34e5645 | 1432 | .TP |
fea681da | 1433 | .B EINVAL |
82ee147a | 1434 | Both |
667417b3 MK |
1435 | .B CLONE_NEWIPC |
1436 | and | |
1437 | .B CLONE_SYSVSEM | |
16853a31 MK |
1438 | were specified in the |
1439 | .IR flags | |
1440 | mask. | |
667417b3 MK |
1441 | .TP |
1442 | .B EINVAL | |
f0007192 | 1443 | One (or both) of |
82ee147a | 1444 | .BR CLONE_NEWPID |
f0007192 MK |
1445 | or |
1446 | .BR CLONE_NEWUSER | |
1447 | and one (or both) of | |
82ee147a | 1448 | .BR CLONE_THREAD |
f0007192 MK |
1449 | or |
1450 | .BR CLONE_PARENT | |
16853a31 MK |
1451 | were specified in the |
1452 | .IR flags | |
1453 | mask. | |
82ee147a | 1454 | .TP |
be479fdf MK |
1455 | .BR EINVAL " (since Linux 2.6.32)" |
1456 | .\" commit 123be07b0b399670a7cc3d82fef0cb4f93ef885c | |
1457 | .BR CLONE_PARENT | |
1458 | was specified, and the caller is an init process. | |
1459 | .TP | |
82ee147a | 1460 | .B EINVAL |
d4748fad | 1461 | Returned by the glibc |
edcc65ff | 1462 | .BR clone () |
d4748fad MK |
1463 | wrapper function when |
1464 | .IR fn | |
1465 | or | |
81c2368f | 1466 | .IR stack |
d4748fad | 1467 | is specified as NULL. |
fea681da | 1468 | .TP |
28cad2c1 | 1469 | .B EINVAL |
667417b3 | 1470 | .BR CLONE_NEWIPC |
16853a31 MK |
1471 | was specified in the |
1472 | .IR flags | |
1473 | mask, | |
667417b3 MK |
1474 | but the kernel was not configured with the |
1475 | .B CONFIG_SYSVIPC | |
1476 | and | |
1477 | .BR CONFIG_IPC_NS | |
1478 | options. | |
1479 | .TP | |
1480 | .B EINVAL | |
163bf178 | 1481 | .BR CLONE_NEWNET |
16853a31 MK |
1482 | was specified in the |
1483 | .IR flags | |
1484 | mask, | |
163bf178 MK |
1485 | but the kernel was not configured with the |
1486 | .B CONFIG_NET_NS | |
1487 | option. | |
1488 | .TP | |
1489 | .B EINVAL | |
28cad2c1 | 1490 | .BR CLONE_NEWPID |
16853a31 MK |
1491 | was specified in the |
1492 | .IR flags | |
1493 | mask, | |
28cad2c1 MK |
1494 | but the kernel was not configured with the |
1495 | .B CONFIG_PID_NS | |
1496 | option. | |
1497 | .TP | |
43ce9dda | 1498 | .B EINVAL |
231d0bbe | 1499 | .BR CLONE_NEWUSER |
16853a31 MK |
1500 | was specified in the |
1501 | .IR flags | |
1502 | mask, | |
231d0bbe MK |
1503 | but the kernel was not configured with the |
1504 | .B CONFIG_USER_NS | |
1505 | option. | |
1506 | .TP | |
1507 | .B EINVAL | |
43ce9dda | 1508 | .BR CLONE_NEWUTS |
16853a31 MK |
1509 | was specified in the |
1510 | .IR flags | |
1511 | mask, | |
43ce9dda | 1512 | but the kernel was not configured with the |
832fe8ea | 1513 | .B CONFIG_UTS_NS |
43ce9dda MK |
1514 | option. |
1515 | .TP | |
c550a897 | 1516 | .B EINVAL |
81c2368f | 1517 | .I stack |
c550a897 MK |
1518 | is not aligned to a suitable boundary for this architecture. |
1519 | For example, on aarch64, | |
81c2368f | 1520 | .I stack |
c550a897 MK |
1521 | must be a multiple of 16. |
1522 | .TP | |
bc03b116 | 1523 | .BR EINVAL " (" clone3 "() only)" |
baa435c6 MK |
1524 | .B CLONE_DETACHED |
1525 | was specified in the | |
1526 | .I flags | |
1527 | mask. | |
1528 | .TP | |
bc03b116 | 1529 | .BR EINVAL " (" clone "() only)" |
9f938981 CB |
1530 | .B CLONE_PIDFD |
1531 | was specified together with | |
16853a31 MK |
1532 | .B CLONE_DETACHED |
1533 | in the | |
1534 | .I flags | |
1535 | mask. | |
9f938981 CB |
1536 | .TP |
1537 | .B EINVAL | |
1538 | .B CLONE_PIDFD | |
1539 | was specified together with | |
16853a31 MK |
1540 | .B CLONE_THREAD |
1541 | in the | |
1542 | .I flags | |
1543 | mask. | |
9f938981 | 1544 | .TP |
faa0e55a | 1545 | .BR "EINVAL " "(" clone "() only)" |
9f938981 CB |
1546 | .B CLONE_PIDFD |
1547 | was specified together with | |
16853a31 MK |
1548 | .B CLONE_PARENT_SETTID |
1549 | in the | |
1550 | .I flags | |
1551 | mask. | |
9f938981 | 1552 | .TP |
bf031aaa AR |
1553 | .BR EINVAL " (" clone3 "() only)" |
1554 | .I set_tid_size | |
ee8bb310 | 1555 | is greater than the number of nested PID namespaces. |
bf031aaa AR |
1556 | .TP |
1557 | .BR EINVAL " (" clone3 "() only)" | |
2a2b2a5d | 1558 | One of the PIDs specified in |
bf031aaa | 1559 | .I set_tid |
2a2b2a5d | 1560 | was an invalid. |
bf031aaa | 1561 | .TP |
ba9ae75d MK |
1562 | .BR EINVAL " (AArch64 only, Linux 4.6 and earlier)" |
1563 | .I stack | |
1564 | was not aligned to a 126-bit boundary. | |
1565 | .TP | |
fea681da MK |
1566 | .B ENOMEM |
1567 | Cannot allocate sufficient memory to allocate a task structure for the | |
1568 | child, or to copy those parts of the caller's context that need to be | |
1569 | copied. | |
1570 | .TP | |
b20e22ae MK |
1571 | .BR ENOSPC " (since Linux 3.7)" |
1572 | .\" commit f2302505775fd13ba93f034206f1e2a587017929 | |
1573 | .B CLONE_NEWPID | |
16853a31 MK |
1574 | was specified in the |
1575 | .I flags | |
1576 | mask, | |
b20e22ae MK |
1577 | but the limit on the nesting depth of PID namespaces |
1578 | would have been exceeded; see | |
1579 | .BR pid_namespaces (7). | |
1580 | .TP | |
b5742ecc MK |
1581 | .BR ENOSPC " (since Linux 4.9; beforehand " EUSERS ) |
1582 | .B CLONE_NEWUSER | |
16853a31 MK |
1583 | was specified in the |
1584 | .IR flags | |
1585 | mask, and the call would cause the limit on the number of | |
b5742ecc MK |
1586 | nested user namespaces to be exceeded. |
1587 | See | |
1588 | .BR user_namespaces (7). | |
efeece04 | 1589 | .IP |
b5742ecc MK |
1590 | From Linux 3.11 to Linux 4.8, the error diagnosed in this case was |
1591 | .BR EUSERS . | |
1592 | .TP | |
2f7a331e | 1593 | .BR ENOSPC " (since Linux 4.9)" |
16853a31 | 1594 | One of the values in the |
2f7a331e | 1595 | .I flags |
16853a31 | 1596 | mask specified the creation of a new user namespace, |
2f7a331e MK |
1597 | but doing so would have caused the limit defined by the corresponding file in |
1598 | .IR /proc/sys/user | |
1599 | to be exceeded. | |
1600 | For further details, see | |
1601 | .BR namespaces (7). | |
1602 | .TP | |
fea681da | 1603 | .B EPERM |
aa825b59 | 1604 | .BR CLONE_NEWCGROUP , |
667417b3 | 1605 | .BR CLONE_NEWIPC , |
163bf178 | 1606 | .BR CLONE_NEWNET , |
43ce9dda MK |
1607 | .BR CLONE_NEWNS , |
1608 | .BR CLONE_NEWPID , | |
82ee147a | 1609 | or |
43ce9dda | 1610 | .BR CLONE_NEWUTS |
00b08db3 | 1611 | was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP). |
fea681da MK |
1612 | .TP |
1613 | .B EPERM | |
1614 | .B CLONE_PID | |
1615 | was specified by a process other than process 0. | |
1c173eb3 | 1616 | (This error occurs only on Linux 2.5.15 and earlier.) |
365d292a MK |
1617 | .TP |
1618 | .B EPERM | |
1619 | .BR CLONE_NEWUSER | |
16853a31 MK |
1620 | was specified in the |
1621 | .IR flags | |
1622 | mask, | |
365d292a MK |
1623 | but either the effective user ID or the effective group ID of the caller |
1624 | does not have a mapping in the parent namespace (see | |
f58fb24f | 1625 | .BR user_namespaces (7)). |
6fd119e7 | 1626 | .TP |
ac007938 MK |
1627 | .BR EPERM " (since Linux 3.9)" |
1628 | .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d | |
11a38815 | 1629 | .B CLONE_NEWUSER |
16853a31 | 1630 | was specified in the |
ac007938 | 1631 | .I flags |
16853a31 | 1632 | mask and the caller is in a chroot environment |
ac007938 MK |
1633 | .\" FIXME What is the rationale for this restriction? |
1634 | (i.e., the caller's root directory does not match the root directory | |
1635 | of the mount namespace in which it resides). | |
1636 | .TP | |
bf031aaa AR |
1637 | .BR EPERM " (" clone3 "() only)" |
1638 | .I set_tid_size | |
1639 | was greater than zero, and the caller lacks the | |
1640 | .B CAP_SYS_ADMIN | |
1641 | capability in one or more of the user namespaces that own the | |
1642 | corresponding PID namespaces. | |
1643 | .TP | |
6717ee86 MK |
1644 | .BR ERESTARTNOINTR " (since Linux 2.6.17)" |
1645 | .\" commit 4a2c7a7837da1b91468e50426066d988050e4d56 | |
1646 | System call was interrupted by a signal and will be restarted. | |
1647 | (This can be seen only during a trace.) | |
1648 | .TP | |
b5742ecc | 1649 | .BR EUSERS " (Linux 3.11 to Linux 4.8)" |
6fd119e7 | 1650 | .B CLONE_NEWUSER |
16853a31 MK |
1651 | was specified in the |
1652 | .IR flags | |
1653 | mask, | |
b5742ecc MK |
1654 | and the limit on the number of nested user namespaces would be exceeded. |
1655 | See the discussion of the | |
1656 | .BR ENOSPC | |
1657 | error above. | |
faa0e55a MK |
1658 | .SH VERSIONS |
1659 | The | |
1660 | .BR clone3 () | |
1661 | system call first appeared in Linux 5.3. | |
92b72224 MK |
1662 | .\" There is no entry for |
1663 | .\" .BR clone () | |
1664 | .\" in libc5. | |
1665 | .\" glibc2 provides | |
1666 | .\" .BR clone () | |
1667 | .\" as described in this manual page. | |
47297adb | 1668 | .SH CONFORMING TO |
faa0e55a MK |
1669 | These system calls |
1670 | are Linux-specific and should not be used in programs | |
a1d5f77c | 1671 | intended to be portable. |
fea681da | 1672 | .SH NOTES |
79bdcc4a MK |
1673 | The |
1674 | .BR kcmp (2) | |
1675 | system call can be used to test whether two processes share various | |
49dba87f | 1676 | resources such as a file descriptor table, |
79bdcc4a | 1677 | System V semaphore undo operations, or a virtual address space. |
efeece04 MK |
1678 | .PP |
1679 | .PP | |
c471c363 MK |
1680 | Handlers registered using |
1681 | .BR pthread_atfork (3) | |
5261b0fe | 1682 | are not executed during a clone call. |
efeece04 | 1683 | .PP |
ca8b1e32 | 1684 | In the Linux 2.4.x series, |
fd8a5be4 MK |
1685 | .B CLONE_THREAD |
1686 | generally does not make the parent of the new thread the same | |
1687 | as the parent of the calling process. | |
1688 | However, for kernel versions 2.4.7 to 2.4.18 the | |
1689 | .B CLONE_THREAD | |
1690 | flag implied the | |
c13182ef | 1691 | .B CLONE_PARENT |
ca8b1e32 | 1692 | flag (as in Linux 2.6.0 and later). |
efeece04 | 1693 | .PP |
34ccb744 | 1694 | On i386, |
a5a997ca MK |
1695 | .BR clone () |
1696 | should not be called through vsyscall, but directly through | |
1697 | .IR "int $0x80" . | |
31830ef0 | 1698 | .SH BUGS |
abcf3b1d MK |
1699 | GNU C library versions 2.3.4 up to and including 2.24 |
1700 | contained a wrapper function for | |
0bfa087b | 1701 | .BR getpid (2) |
abcf3b1d MK |
1702 | that performed caching of PIDs. |
1703 | This caching relied on support in the glibc wrapper for | |
c60237c9 | 1704 | .BR clone (), |
abcf3b1d MK |
1705 | but limitations in the implementation |
1706 | meant that the cache was not up to date in some circumstances. | |
c60237c9 | 1707 | In particular, |
abcf3b1d | 1708 | if a signal was delivered to the child immediately after the |
c60237c9 MK |
1709 | .BR clone () |
1710 | call, then a call to | |
0b80cf56 | 1711 | .BR getpid (2) |
abcf3b1d | 1712 | in a handler for the signal could return the PID |
c60237c9 | 1713 | of the calling process ("the parent"), |
abcf3b1d | 1714 | if the clone wrapper had not yet had a chance to update the PID |
c60237c9 MK |
1715 | cache in the child. |
1716 | (This discussion ignores the case where the child was created using | |
9291ce36 | 1717 | .BR CLONE_THREAD , |
c60237c9 | 1718 | when |
0b80cf56 | 1719 | .BR getpid (2) |
c60237c9 MK |
1720 | .I should |
1721 | return the same value in the child and in the process that called | |
1722 | .BR clone (), | |
a1d48abb | 1723 | since the caller and the child are in the same thread group. |
e7d807b7 | 1724 | The stale-cache problem also does not occur if the |
a1d48abb JR |
1725 | .I flags |
1726 | argument includes | |
1727 | .BR CLONE_VM .) | |
abcf3b1d MK |
1728 | To get the truth, it was sometimes necessary to use code such as the following: |
1729 | .PP | |
47f743f1 MK |
1730 | .in +4n |
1731 | .EX | |
1732 | #include <syscall.h> | |
31830ef0 | 1733 | |
47f743f1 | 1734 | pid_t mypid; |
31830ef0 | 1735 | |
47f743f1 MK |
1736 | mypid = syscall(SYS_getpid); |
1737 | .EE | |
1738 | .in | |
c60237c9 MK |
1739 | .\" See also the following bug reports |
1740 | .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521 | |
1741 | .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910 | |
abcf3b1d MK |
1742 | .PP |
1743 | Because of the stale-cache problem, as well as other problems noted in | |
1744 | .BR getpid (2), | |
1745 | the PID caching feature was removed in glibc 2.25. | |
8c7b566c | 1746 | .SH EXAMPLE |
8c7b566c | 1747 | The following program demonstrates the use of |
9c13072a | 1748 | .BR clone () |
8c7b566c MK |
1749 | to create a child process that executes in a separate UTS namespace. |
1750 | The child changes the hostname in its UTS namespace. | |
1751 | Both parent and child then display the system hostname, | |
1752 | making it possible to see that the hostname | |
1753 | differs in the UTS namespaces of the parent and child. | |
1754 | For an example of the use of this program, see | |
1755 | .BR setns (2). | |
99c3a000 MK |
1756 | .PP |
1757 | Within the sample program, we allocate the memory that is to | |
1758 | be used for the child's stack using | |
1759 | .BR mmap (2) | |
1760 | rather than | |
1761 | .BR malloc (3) | |
1762 | for the following reasons: | |
1763 | .IP * 3 | |
1764 | .BR mmap (2) | |
1765 | allocates a block of memory that starts on a page | |
1766 | boundary and is a multiple of the page size. | |
1767 | This is useful if we want to establish a guard page (a page with protection | |
1768 | .BR PROT_NONE ) | |
1769 | at the end of the stack using | |
1770 | .BR mprotect (2). | |
1771 | .IP * | |
1772 | We can specify the | |
1773 | .BR MAP_STACK | |
1774 | flag to request a mapping that is suitable for a stack. | |
1775 | For the moment, this flag is a no-op on Linux, | |
1776 | but it exists and has effect on some other systems, | |
1777 | so we should include it for portability. | |
f30b7415 | 1778 | .SS Program source |
e7d0bb47 | 1779 | .EX |
8c7b566c MK |
1780 | #define _GNU_SOURCE |
1781 | #include <sys/wait.h> | |
1782 | #include <sys/utsname.h> | |
1783 | #include <sched.h> | |
1784 | #include <string.h> | |
1785 | #include <stdio.h> | |
1786 | #include <stdlib.h> | |
1787 | #include <unistd.h> | |
99c3a000 | 1788 | #include <sys/mman.h> |
8c7b566c | 1789 | |
d1a71985 | 1790 | #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e |
8c7b566c MK |
1791 | } while (0) |
1792 | ||
1793 | static int /* Start function for cloned child */ | |
1794 | childFunc(void *arg) | |
1795 | { | |
1796 | struct utsname uts; | |
1797 | ||
1798 | /* Change hostname in UTS namespace of child */ | |
1799 | ||
1800 | if (sethostname(arg, strlen(arg)) == \-1) | |
1801 | errExit("sethostname"); | |
1802 | ||
07d4e6ea | 1803 | /* Retrieve and display hostname */ |
8c7b566c MK |
1804 | |
1805 | if (uname(&uts) == \-1) | |
1806 | errExit("uname"); | |
d1a71985 | 1807 | printf("uts.nodename in child: %s\en", uts.nodename); |
8c7b566c MK |
1808 | |
1809 | /* Keep the namespace open for a while, by sleeping. | |
1810 | This allows some experimentation\-\-for example, another | |
1811 | process might join the namespace. */ | |
9f1b9726 | 1812 | |
8c7b566c MK |
1813 | sleep(200); |
1814 | ||
1815 | return 0; /* Child terminates now */ | |
1816 | } | |
1817 | ||
1818 | #define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */ | |
1819 | ||
1820 | int | |
1821 | main(int argc, char *argv[]) | |
1822 | { | |
1823 | char *stack; /* Start of stack buffer */ | |
1824 | char *stackTop; /* End of stack buffer */ | |
1825 | pid_t pid; | |
1826 | struct utsname uts; | |
1827 | ||
1828 | if (argc < 2) { | |
d1a71985 | 1829 | fprintf(stderr, "Usage: %s <child\-hostname>\en", argv[0]); |
8c7b566c MK |
1830 | exit(EXIT_SUCCESS); |
1831 | } | |
1832 | ||
99c3a000 | 1833 | /* Allocate memory to be used for the stack of the child */ |
8c7b566c | 1834 | |
99c3a000 MK |
1835 | stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, |
1836 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, \-1, 0); | |
8eea66b8 | 1837 | if (stack == MAP_FAILED) |
99c3a000 MK |
1838 | errExit("mmap"); |
1839 | ||
8c7b566c MK |
1840 | stackTop = stack + STACK_SIZE; /* Assume stack grows downward */ |
1841 | ||
1842 | /* Create child that has its own UTS namespace; | |
1843 | child commences execution in childFunc() */ | |
1844 | ||
1845 | pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]); | |
1846 | if (pid == \-1) | |
1847 | errExit("clone"); | |
d1a71985 | 1848 | printf("clone() returned %ld\en", (long) pid); |
8c7b566c MK |
1849 | |
1850 | /* Parent falls through to here */ | |
1851 | ||
1852 | sleep(1); /* Give child time to change its hostname */ | |
1853 | ||
9f1b9726 | 1854 | /* Display hostname in parent\(aqs UTS namespace. This will be |
8c7b566c MK |
1855 | different from hostname in child\(aqs UTS namespace. */ |
1856 | ||
1857 | if (uname(&uts) == \-1) | |
1858 | errExit("uname"); | |
d1a71985 | 1859 | printf("uts.nodename in parent: %s\en", uts.nodename); |
8c7b566c MK |
1860 | |
1861 | if (waitpid(pid, NULL, 0) == \-1) /* Wait for child */ | |
1862 | errExit("waitpid"); | |
d1a71985 | 1863 | printf("child has terminated\en"); |
8c7b566c MK |
1864 | |
1865 | exit(EXIT_SUCCESS); | |
1866 | } | |
e7d0bb47 | 1867 | .EE |
47297adb | 1868 | .SH SEE ALSO |
fea681da | 1869 | .BR fork (2), |
2b44301c | 1870 | .BR futex (2), |
fea681da MK |
1871 | .BR getpid (2), |
1872 | .BR gettid (2), | |
6f8746e4 | 1873 | .BR kcmp (2), |
99c3a000 | 1874 | .BR mmap (2), |
d8837668 | 1875 | .BR pidfd_open (2), |
f2d0bbf1 | 1876 | .BR set_thread_area (2), |
2b44301c | 1877 | .BR set_tid_address (2), |
8403481f | 1878 | .BR setns (2), |
f2d0bbf1 | 1879 | .BR tkill (2), |
5cc01e9c | 1880 | .BR unshare (2), |
fea681da | 1881 | .BR wait (2), |
3616b7c0 | 1882 | .BR capabilities (7), |
41096af1 | 1883 | .BR namespaces (7), |
3616b7c0 | 1884 | .BR pthreads (7) |