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