]>
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 | .\" |
97986708 | 42 | .TH CLONE 2 2016-03-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 MK |
47 | /* Prototype for the glibc wrapper function */ |
48 | ||
4f71ba5d | 49 | .B #define _GNU_SOURCE |
fea681da | 50 | .B #include <sched.h> |
c10859eb | 51 | |
ff929e3b MK |
52 | .BI "int clone(int (*" "fn" ")(void *), void *" child_stack , |
53 | .BI " int " flags ", void *" "arg" ", ... " | |
d3dbc9b1 | 54 | .BI " /* pid_t *" ptid ", struct user_desc *" tls \ |
ff929e3b | 55 | ", pid_t *" ctid " */ );" |
81f10dad | 56 | |
e585064b | 57 | /* Prototype for the raw system call */ |
81f10dad MK |
58 | |
59 | .BI "long clone(unsigned long " flags ", void *" child_stack , | |
60 | .BI " void *" ptid ", void *" ctid , | |
61 | .BI " struct pt_regs *" regs ); | |
c10859eb | 62 | .fi |
fea681da | 63 | .SH DESCRIPTION |
edcc65ff MK |
64 | .BR clone () |
65 | creates a new process, in a manner similar to | |
fea681da | 66 | .BR fork (2). |
81f10dad MK |
67 | |
68 | This page describes both the glibc | |
e511ffb6 | 69 | .BR clone () |
e585064b | 70 | wrapper function and the underlying system call on which it is based. |
81f10dad | 71 | The main text describes the wrapper function; |
e585064b | 72 | the differences for the raw system call |
81f10dad | 73 | are described toward the end of this page. |
fea681da MK |
74 | |
75 | Unlike | |
76 | .BR fork (2), | |
81f10dad MK |
77 | .BR clone () |
78 | allows the child process to share parts of its execution context with | |
fea681da | 79 | the calling process, such as the memory space, the table of file |
c13182ef MK |
80 | descriptors, and the table of signal handlers. |
81 | (Note that on this manual | |
82 | page, "calling process" normally corresponds to "parent process". | |
83 | But see the description of | |
84 | .B CLONE_PARENT | |
fea681da MK |
85 | below.) |
86 | ||
1533d242 | 87 | One use of |
edcc65ff | 88 | .BR clone () |
fea681da MK |
89 | is to implement threads: multiple threads of control in a program that |
90 | run concurrently in a shared memory space. | |
91 | ||
92 | When the child process is created with | |
c13182ef | 93 | .BR clone (), |
fea681da | 94 | it executes the function |
c13182ef | 95 | .IR fn ( arg ). |
fea681da | 96 | (This differs from |
c13182ef | 97 | .BR fork (2), |
fea681da | 98 | where execution continues in the child from the point |
c13182ef MK |
99 | of the |
100 | .BR fork (2) | |
fea681da MK |
101 | call.) |
102 | The | |
103 | .I fn | |
104 | argument is a pointer to a function that is called by the child | |
105 | process at the beginning of its execution. | |
106 | The | |
107 | .I arg | |
108 | argument is passed to the | |
109 | .I fn | |
110 | function. | |
111 | ||
c13182ef | 112 | When the |
fea681da | 113 | .IR fn ( arg ) |
c13182ef MK |
114 | function application returns, the child process terminates. |
115 | The integer returned by | |
fea681da | 116 | .I fn |
c13182ef MK |
117 | is the exit code for the child process. |
118 | The child process may also terminate explicitly by calling | |
fea681da MK |
119 | .BR exit (2) |
120 | or after receiving a fatal signal. | |
121 | ||
122 | The | |
123 | .I child_stack | |
c13182ef MK |
124 | argument specifies the location of the stack used by the child process. |
125 | Since the child and calling process may share memory, | |
fea681da | 126 | it is not possible for the child process to execute in the |
c13182ef MK |
127 | same stack as the calling process. |
128 | The calling process must therefore | |
fea681da MK |
129 | set up memory space for the child stack and pass a pointer to this |
130 | space to | |
edcc65ff | 131 | .BR clone (). |
5fab2e7c | 132 | Stacks grow downward on all processors that run Linux |
fea681da MK |
133 | (except the HP PA processors), so |
134 | .I child_stack | |
135 | usually points to the topmost address of the memory space set up for | |
136 | the child stack. | |
137 | ||
138 | The low byte of | |
139 | .I flags | |
fd8a5be4 MK |
140 | contains the number of the |
141 | .I "termination signal" | |
142 | sent to the parent when the child dies. | |
143 | If this signal is specified as anything other than | |
fea681da MK |
144 | .BR SIGCHLD , |
145 | then the parent process must specify the | |
c13182ef MK |
146 | .B __WALL |
147 | or | |
fea681da | 148 | .B __WCLONE |
c13182ef MK |
149 | options when waiting for the child with |
150 | .BR wait (2). | |
fea681da MK |
151 | If no signal is specified, then the parent process is not signaled |
152 | when the child terminates. | |
153 | ||
154 | .I flags | |
fd8a5be4 MK |
155 | may also be bitwise-or'ed with zero or more of the following constants, |
156 | in order to specify what is shared between the calling process | |
fea681da | 157 | and the child process: |
fea681da | 158 | .TP |
f5dbc7c8 | 159 | .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)" |
8ef021ea | 160 | Erase the child thread ID at the location |
d3dbc9b1 | 161 | .I ctid |
f5dbc7c8 MK |
162 | in child memory when the child exits, and do a wakeup on the futex |
163 | at that address. | |
164 | The address involved may be changed by the | |
165 | .BR set_tid_address (2) | |
166 | system call. | |
167 | This is used by threading libraries. | |
168 | .TP | |
169 | .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)" | |
8ef021ea | 170 | Store the child thread ID at the location |
d3dbc9b1 | 171 | .I ctid |
8ef021ea | 172 | in the child's memory. |
f5dbc7c8 | 173 | .TP |
1603d6a1 | 174 | .BR CLONE_FILES " (since Linux 2.0)" |
fea681da | 175 | If |
f5dbc7c8 MK |
176 | .B CLONE_FILES |
177 | is set, the calling process and the child process share the same file | |
178 | descriptor table. | |
179 | Any file descriptor created by the calling process or by the child | |
180 | process is also valid in the other process. | |
181 | Similarly, if one of the processes closes a file descriptor, | |
182 | or changes its associated flags (using the | |
183 | .BR fcntl (2) | |
184 | .B F_SETFD | |
185 | operation), the other process is also affected. | |
8a76b19e KE |
186 | If a process sharing a file descriptor table calls |
187 | .BR execve (2), | |
188 | its file descriptor table is duplicated (unshared). | |
fea681da MK |
189 | |
190 | If | |
f5dbc7c8 MK |
191 | .B CLONE_FILES |
192 | is not set, the child process inherits a copy of all file descriptors | |
193 | opened in the calling process at the time of | |
194 | .BR clone (). | |
195 | (The duplicated file descriptors in the child refer to the | |
196 | same open file descriptions (see | |
197 | .BR open (2)) | |
198 | as the corresponding file descriptors in the calling process.) | |
199 | Subsequent operations that open or close file descriptors, | |
200 | or change file descriptor flags, | |
201 | performed by either the calling | |
202 | process or the child process do not affect the other process. | |
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 MK |
217 | other process. |
218 | ||
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. | |
239 | ||
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. | |
256 | ||
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. | |
267 | ||
268 | For further information on cgroup namespaces, see | |
b9fe4bc3 | 269 | .BR cgroup_namespaces (7). |
c5af0674 MK |
270 | |
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. |
667417b3 | 286 | |
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. | |
009a049e | 298 | |
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. | |
302 | ||
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 MK |
305 | all IPC objects in the namespace are automatically destroyed. |
306 | ||
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 . | |
9343f8e7 MK |
313 | |
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.) |
163bf178 MK |
320 | |
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. | |
329 | ||
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. | |
344 | ||
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). | |
bf032425 | 351 | |
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. |
fea681da | 366 | |
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. |
c212248c MK |
379 | |
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. | |
401 | ||
402 | For further information on PID namespaces, see | |
7e0e902b MK |
403 | .BR namespaces (7) |
404 | and | |
39b3f005 | 405 | .BR pid_namespaces (7). |
9d005472 | 406 | |
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.) | |
425 | ||
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 EB |
431 | the process is created in the same user namespace as the calling process. |
432 | ||
9d005472 | 433 | For further information on user namespaces, see |
f58fb24f MK |
434 | .BR namespaces (7) |
435 | and | |
436 | .BR user_namespaces (7) | |
06b30458 | 437 | |
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. |
f0007192 | 448 | |
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 MK |
461 | .BR CLONE_FS . |
462 | ||
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. |
43ce9dda MK |
477 | |
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 MK |
487 | but are not visible to processes in other UTS namespaces. |
488 | ||
ab5dd83f MK |
489 | Only a privileged process |
490 | .RB ( CAP_SYS_ADMIN ) | |
491 | can employ | |
492 | .BR CLONE_NEWUTS . | |
9cc7ad66 | 493 | |
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. | |
503 | ||
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. | |
509 | ||
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.) | |
525 | .TP | |
526 | .BR CLONE_PID " (obsolete)" | |
527 | If | |
528 | .B CLONE_PID | |
529 | is set, the child process is created with the same process ID as | |
530 | the calling process. | |
531 | This is good for hacking the system, but otherwise | |
532 | of not much use. | |
533 | Since 2.3.21 this flag can be | |
534 | specified only by the system boot process (PID 0). | |
28b44abc MK |
535 | It disappeared in Linux 2.5.16. |
536 | Since then, the kernel silently ignores it without error. | |
f5dbc7c8 | 537 | .TP |
1603d6a1 | 538 | .BR CLONE_PTRACE " (since Linux 2.2)" |
f5dbc7c8 MK |
539 | If |
540 | .B CLONE_PTRACE | |
541 | is specified, and the calling process is being traced, | |
542 | then trace the child also (see | |
543 | .BR ptrace (2)). | |
544 | .TP | |
545 | .BR CLONE_SETTLS " (since Linux 2.5.32)" | |
546 | The | |
547 | .I newtls | |
548 | argument is the new TLS (Thread Local Storage) descriptor. | |
549 | (See | |
550 | .BR set_thread_area (2).) | |
551 | .TP | |
1603d6a1 | 552 | .BR CLONE_SIGHAND " (since Linux 2.0)" |
fea681da MK |
553 | If |
554 | .B CLONE_SIGHAND | |
314c8ff4 | 555 | is set, the calling process and the child process share the same table of |
c13182ef MK |
556 | signal handlers. |
557 | If the calling process or child process calls | |
fea681da | 558 | .BR sigaction (2) |
c13182ef MK |
559 | to change the behavior associated with a signal, the behavior is |
560 | changed in the other process as well. | |
561 | However, the calling process and child | |
fea681da | 562 | processes still have distinct signal masks and sets of pending |
c13182ef MK |
563 | signals. |
564 | So, one of them may block or unblock some signals using | |
fea681da MK |
565 | .BR sigprocmask (2) |
566 | without affecting the other process. | |
567 | ||
568 | If | |
569 | .B CLONE_SIGHAND | |
570 | is not set, the child process inherits a copy of the signal handlers | |
571 | of the calling process at the time | |
edcc65ff | 572 | .BR clone () |
c13182ef MK |
573 | is called. |
574 | Calls to | |
fea681da MK |
575 | .BR sigaction (2) |
576 | performed later by one of the processes have no effect on the other | |
577 | process. | |
29546c24 MK |
578 | |
579 | Since Linux 2.6.0-test6, | |
580 | .I flags | |
581 | must also include | |
582 | .B CLONE_VM | |
583 | if | |
584 | .B CLONE_SIGHAND | |
585 | is specified | |
fea681da | 586 | .TP |
a69b6bda MK |
587 | .BR CLONE_STOPPED " (since Linux 2.6.0-test2)" |
588 | If | |
589 | .B CLONE_STOPPED | |
590 | is set, then the child is initially stopped (as though it was sent a | |
591 | .B SIGSTOP | |
592 | signal), and must be resumed by sending it a | |
593 | .B SIGCONT | |
594 | signal. | |
ef37eaf2 | 595 | |
a60450a9 MK |
596 | This flag was |
597 | .I deprecated | |
598 | from Linux 2.6.25 onward, | |
599 | and was | |
600 | .I removed | |
28b44abc MK |
601 | altogether in Linux 2.6.38. |
602 | Since then, the kernel silently ignores it without error. | |
a5a061ee | 603 | .\" glibc 2.8 removed this defn from bits/sched.h |
c5af0674 MK |
604 | Starting with Linux 4.6, the same bit was reused for the |
605 | .BR CLONE_NEWCGROUP | |
606 | flag. | |
a69b6bda | 607 | .TP |
f5dbc7c8 | 608 | .BR CLONE_SYSVSEM " (since Linux 2.5.10)" |
fea681da | 609 | If |
f5dbc7c8 MK |
610 | .B CLONE_SYSVSEM |
611 | is set, then the child and the calling process share | |
5ada4b94 MK |
612 | a single list of System V semaphore adjustment |
613 | .RI ( semadj ) | |
614 | values (see | |
f5dbc7c8 | 615 | .BR semop (2)). |
5ada4b94 MK |
616 | In this case, the shared list accumulates |
617 | .I semadj | |
618 | values across all processes sharing the list, | |
619 | and semaphore adjustments are performed only when the last process | |
620 | that is sharing the list terminates (or ceases sharing the list using | |
621 | .BR unshare (2)). | |
f5d401dd | 622 | If this flag is not set, then the child has a separate |
5ada4b94 MK |
623 | .I semadj |
624 | list that is initially empty. | |
fea681da MK |
625 | .TP |
626 | .BR CLONE_THREAD " (since Linux 2.4.0-test8)" | |
627 | If | |
628 | .B CLONE_THREAD | |
629 | is set, the child is placed in the same thread group as the calling process. | |
fd8a5be4 MK |
630 | To make the remainder of the discussion of |
631 | .B CLONE_THREAD | |
632 | more readable, the term "thread" is used to refer to the | |
633 | processes within a thread group. | |
fea681da | 634 | |
fd8a5be4 MK |
635 | Thread groups were a feature added in Linux 2.4 to support the |
636 | POSIX threads notion of a set of threads that share a single PID. | |
637 | Internally, this shared PID is the so-called | |
638 | thread group identifier (TGID) for the thread group. | |
c13182ef | 639 | Since Linux 2.4, calls to |
fea681da | 640 | .BR getpid (2) |
fd8a5be4 MK |
641 | return the TGID of the caller. |
642 | ||
643 | The threads within a group can be distinguished by their (system-wide) | |
644 | unique thread IDs (TID). | |
645 | A new thread's TID is available as the function result | |
646 | returned to the caller of | |
647 | .BR clone (), | |
648 | and a thread can obtain | |
649 | its own TID using | |
650 | .BR gettid (2). | |
651 | ||
c13182ef | 652 | When a call is made to |
fd8a5be4 MK |
653 | .BR clone () |
654 | without specifying | |
655 | .BR CLONE_THREAD , | |
656 | then the resulting thread is placed in a new thread group | |
657 | whose TGID is the same as the thread's TID. | |
658 | This thread is the | |
659 | .I leader | |
660 | of the new thread group. | |
661 | ||
662 | A new thread created with | |
663 | .B CLONE_THREAD | |
664 | has the same parent process as the caller of | |
665 | .BR clone () | |
c13182ef | 666 | (i.e., like |
fd8a5be4 MK |
667 | .BR CLONE_PARENT ), |
668 | so that calls to | |
669 | .BR getppid (2) | |
670 | return the same value for all of the threads in a thread group. | |
671 | When a | |
c13182ef | 672 | .B CLONE_THREAD |
fd8a5be4 MK |
673 | thread terminates, the thread that created it using |
674 | .BR clone () | |
675 | is not sent a | |
676 | .B SIGCHLD | |
677 | (or other termination) signal; | |
678 | nor can the status of such a thread be obtained | |
679 | using | |
680 | .BR wait (2). | |
681 | (The thread is said to be | |
682 | .IR detached .) | |
683 | ||
e2fbf61d MK |
684 | After all of the threads in a thread group terminate |
685 | the parent process of the thread group is sent a | |
fd8a5be4 MK |
686 | .B SIGCHLD |
687 | (or other termination) signal. | |
688 | ||
689 | If any of the threads in a thread group performs an | |
690 | .BR execve (2), | |
691 | then all threads other than the thread group leader are terminated, | |
692 | and the new program is executed in the thread group leader. | |
693 | ||
f7110f60 MK |
694 | If one of the threads in a thread group creates a child using |
695 | .BR fork (2), | |
696 | then any thread in the group can | |
697 | .BR wait (2) | |
698 | for that child. | |
699 | ||
edcc65ff | 700 | Since Linux 2.5.35, |
fd8a5be4 MK |
701 | .I flags |
702 | must also include | |
703 | .B CLONE_SIGHAND | |
704 | if | |
705 | .B CLONE_THREAD | |
6fd69f33 MK |
706 | is specified |
707 | (and note that, since Linux 2.6.0-test6, | |
708 | .BR CLONE_SIGHAND | |
709 | also requires | |
710 | .BR CLONE_VM | |
711 | to be included). | |
e2fbf61d MK |
712 | |
713 | Signals may be sent to a thread group as a whole (i.e., a TGID) using | |
714 | .BR kill (2), | |
715 | or to a specific thread (i.e., TID) using | |
716 | .BR tgkill (2). | |
717 | ||
718 | Signal dispositions and actions are process-wide: | |
719 | if an unhandled signal is delivered to a thread, then | |
720 | it will affect (terminate, stop, continue, be ignored in) | |
721 | all members of the thread group. | |
722 | ||
99408a60 | 723 | Each thread has its own signal mask, as set by |
e2fbf61d | 724 | .BR sigprocmask (2), |
82a06020 | 725 | but signals can be pending either: for the whole process |
e2fbf61d MK |
726 | (i.e., deliverable to any member of the thread group), |
727 | when sent with | |
82a06020 | 728 | .BR kill (2); |
e2fbf61d MK |
729 | or for an individual thread, when sent with |
730 | .BR tgkill (2). | |
99408a60 MK |
731 | A call to |
732 | .BR sigpending (2) | |
733 | returns a signal set that is the union of the signals pending for the | |
734 | whole process and the signals that are pending for the calling thread. | |
e2fbf61d | 735 | |
c13182ef | 736 | If |
e2fbf61d MK |
737 | .BR kill (2) |
738 | is used to send a signal to a thread group, | |
739 | and the thread group has installed a handler for the signal, then | |
740 | the handler will be invoked in exactly one, arbitrarily selected | |
741 | member of the thread group that has not blocked the signal. | |
c13182ef | 742 | If multiple threads in a group are waiting to accept the same signal using |
e2fbf61d MK |
743 | .BR sigwaitinfo (2), |
744 | the kernel will arbitrarily select one of these threads | |
c13182ef | 745 | to receive a signal sent using |
e2fbf61d | 746 | .BR kill (2). |
a69b6bda | 747 | .TP |
f5dbc7c8 | 748 | .BR CLONE_UNTRACED " (since Linux 2.5.46)" |
a69b6bda | 749 | If |
f5dbc7c8 MK |
750 | .B CLONE_UNTRACED |
751 | is specified, then a tracing process cannot force | |
752 | .B CLONE_PTRACE | |
753 | on this child process. | |
fea681da | 754 | .TP |
1603d6a1 | 755 | .BR CLONE_VFORK " (since Linux 2.2)" |
f5dbc7c8 MK |
756 | If |
757 | .B CLONE_VFORK | |
758 | is set, the execution of the calling process is suspended | |
759 | until the child releases its virtual memory | |
760 | resources via a call to | |
761 | .BR execve (2) | |
762 | or | |
763 | .BR _exit (2) | |
764 | (as with | |
765 | .BR vfork (2)). | |
766 | ||
767 | If | |
768 | .B CLONE_VFORK | |
4b4a853a | 769 | is not set, then both the calling process and the child are schedulable |
f5dbc7c8 MK |
770 | after the call, and an application should not rely on execution occurring |
771 | in any particular order. | |
fea681da | 772 | .TP |
1603d6a1 | 773 | .BR CLONE_VM " (since Linux 2.0)" |
f5dbc7c8 MK |
774 | If |
775 | .B CLONE_VM | |
776 | is set, the calling process and the child process run in the same memory | |
777 | space. | |
778 | In particular, memory writes performed by the calling process | |
779 | or by the child process are also visible in the other process. | |
780 | Moreover, any memory mapping or unmapping performed with | |
781 | .BR mmap (2) | |
782 | or | |
783 | .BR munmap (2) | |
784 | by the child or calling process also affects the other process. | |
785 | ||
786 | If | |
787 | .B CLONE_VM | |
788 | is not set, the child process runs in a separate copy of the memory | |
789 | space of the calling process at the time of | |
790 | .BR clone (). | |
791 | Memory writes or file mappings/unmappings performed by one of the | |
792 | processes do not affect the other, as with | |
793 | .BR fork (2). | |
0722a578 | 794 | .SS C library/kernel differences |
e585064b MK |
795 | The raw |
796 | .BR clone () | |
fea681da MK |
797 | system call corresponds more closely to |
798 | .BR fork (2) | |
799 | in that execution in the child continues from the point of the | |
c13182ef | 800 | call. |
5add3af3 MK |
801 | As such, the |
802 | .I fn | |
c13182ef | 803 | and |
5add3af3 MK |
804 | .I arg |
805 | arguments of the | |
806 | .BR clone () | |
807 | wrapper function are omitted. | |
808 | Furthermore, the argument order changes. | |
c787510f | 809 | The raw system call interface on x86 and many other architectures is roughly: |
5add3af3 MK |
810 | .in +4 |
811 | .nf | |
812 | ||
813 | .BI "long clone(unsigned long " flags ", void *" child_stack , | |
814 | .BI " void *" ptid ", void *" ctid , | |
815 | .BI " struct pt_regs *" regs ); | |
fea681da | 816 | |
5add3af3 MK |
817 | .fi |
818 | .in | |
e585064b | 819 | Another difference for the raw system call is that the |
fea681da | 820 | .I child_stack |
c13182ef | 821 | argument may be zero, in which case copy-on-write semantics ensure that the |
fea681da | 822 | child gets separate copies of stack pages when either process modifies |
c13182ef MK |
823 | the stack. |
824 | In this case, for correct operation, the | |
fea681da MK |
825 | .B CLONE_VM |
826 | option should not be specified. | |
c787510f | 827 | |
e585064b | 828 | For some architectures, the order of the arguments for the system call |
c787510f | 829 | differs from that shown above. |
7d2e6d74 | 830 | On the score, microblaze, ARM, ARM 64, PA-RISC, arc, Power PC, xtensa, |
c787510f MK |
831 | and MIPS architectures, |
832 | the order of the fourth and fifth arguments is reversed. | |
833 | On the cris and s390 architectures, | |
834 | the order of the first and second arguments is reversed. | |
251113d0 MK |
835 | .SS blackfin, m68k, and sparc |
836 | The argument-passing conventions on | |
04346be5 | 837 | blackfin, m68k, and sparc are different from the descriptions above. |
251113d0 | 838 | For details, see the kernel (and glibc) source. |
574c92b6 | 839 | .SS ia64 |
097a1f3b MK |
840 | On ia64, a different interface is used: |
841 | .nf | |
842 | ||
843 | .BI "int __clone2(int (*" "fn" ")(void *), " | |
844 | .BI " void *" child_stack_base ", size_t " stack_size , | |
845 | .BI " int " flags ", void *" "arg" ", ... " | |
846 | .BI " /* pid_t *" ptid ", struct user_desc *" tls \ | |
847 | ", pid_t *" ctid " */ );" | |
848 | .fi | |
849 | .PP | |
850 | The prototype shown above is for the glibc wrapper function; | |
851 | the raw system call interface has no | |
852 | .I fn | |
853 | or | |
854 | .I arg | |
855 | argument, and changes the order of the arguments so that | |
856 | .I flags | |
857 | is the first argument, and | |
858 | .I tls | |
859 | is the last argument. | |
860 | .PP | |
861 | .BR __clone2 () | |
862 | operates in the same way as | |
863 | .BR clone (), | |
864 | except that | |
865 | .I child_stack_base | |
866 | points to the lowest address of the child's stack area, | |
867 | and | |
868 | .I stack_size | |
869 | specifies the size of the stack pointed to by | |
870 | .IR child_stack_base . | |
5add3af3 | 871 | .SS Linux 2.4 and earlier |
577f9b62 MK |
872 | In Linux 2.4 and earlier, |
873 | .BR clone () | |
874 | does not take arguments | |
875 | .IR ptid , | |
876 | .IR tls , | |
877 | and | |
130b2e49 | 878 | .IR ctid . |
47297adb | 879 | .SH RETURN VALUE |
0bfa087b MK |
880 | .\" gettid(2) returns current->pid; |
881 | .\" getpid(2) returns current->tgid; | |
fea681da | 882 | On success, the thread ID of the child process is returned |
c13182ef | 883 | in the caller's thread of execution. |
84811e86 | 884 | On failure, \-1 is returned |
fea681da MK |
885 | in the caller's context, no child process will be created, and |
886 | .I errno | |
887 | will be set appropriately. | |
fea681da MK |
888 | .SH ERRORS |
889 | .TP | |
890 | .B EAGAIN | |
e1b6e186 MK |
891 | Too many processes are already running; see |
892 | .BR fork (2). | |
fea681da MK |
893 | .TP |
894 | .B EINVAL | |
895 | .B CLONE_SIGHAND | |
896 | was specified, but | |
897 | .B CLONE_VM | |
2e8a7fb3 MK |
898 | was not. |
899 | (Since Linux 2.6.0-test6.) | |
fea681da MK |
900 | .TP |
901 | .B EINVAL | |
902 | .B CLONE_THREAD | |
903 | was specified, but | |
904 | .B CLONE_SIGHAND | |
6387216b MK |
905 | was not. |
906 | (Since Linux 2.5.35.) | |
29546c24 MK |
907 | .\" .TP |
908 | .\" .B EINVAL | |
909 | .\" Precisely one of | |
910 | .\" .B CLONE_DETACHED | |
911 | .\" and | |
912 | .\" .B CLONE_THREAD | |
6387216b MK |
913 | .\" was specified. |
914 | .\" (Since Linux 2.6.0-test6.) | |
fea681da MK |
915 | .TP |
916 | .B EINVAL | |
d34e5645 | 917 | .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71 |
fea681da MK |
918 | Both |
919 | .B CLONE_FS | |
920 | and | |
921 | .B CLONE_NEWNS | |
922 | were specified in | |
923 | .IR flags . | |
924 | .TP | |
d34e5645 MK |
925 | .BR EINVAL " (since Linux 3.9)" |
926 | Both | |
927 | .B CLONE_NEWUSER | |
928 | and | |
929 | .B CLONE_FS | |
930 | were specified in | |
931 | .IR flags . | |
932 | .TP | |
fea681da | 933 | .B EINVAL |
82ee147a | 934 | Both |
667417b3 MK |
935 | .B CLONE_NEWIPC |
936 | and | |
937 | .B CLONE_SYSVSEM | |
938 | were specified in | |
939 | .IR flags . | |
940 | .TP | |
941 | .B EINVAL | |
f0007192 | 942 | One (or both) of |
82ee147a | 943 | .BR CLONE_NEWPID |
f0007192 MK |
944 | or |
945 | .BR CLONE_NEWUSER | |
946 | and one (or both) of | |
82ee147a | 947 | .BR CLONE_THREAD |
f0007192 MK |
948 | or |
949 | .BR CLONE_PARENT | |
82ee147a MK |
950 | were specified in |
951 | .IR flags . | |
952 | .TP | |
953 | .B EINVAL | |
c13182ef | 954 | Returned by |
edcc65ff | 955 | .BR clone () |
c13182ef | 956 | when a zero value is specified for |
fea681da MK |
957 | .IR child_stack . |
958 | .TP | |
28cad2c1 | 959 | .B EINVAL |
667417b3 MK |
960 | .BR CLONE_NEWIPC |
961 | was specified in | |
962 | .IR flags , | |
963 | but the kernel was not configured with the | |
964 | .B CONFIG_SYSVIPC | |
965 | and | |
966 | .BR CONFIG_IPC_NS | |
967 | options. | |
968 | .TP | |
969 | .B EINVAL | |
163bf178 MK |
970 | .BR CLONE_NEWNET |
971 | was specified in | |
972 | .IR flags , | |
973 | but the kernel was not configured with the | |
974 | .B CONFIG_NET_NS | |
975 | option. | |
976 | .TP | |
977 | .B EINVAL | |
28cad2c1 MK |
978 | .BR CLONE_NEWPID |
979 | was specified in | |
980 | .IR flags , | |
981 | but the kernel was not configured with the | |
982 | .B CONFIG_PID_NS | |
983 | option. | |
984 | .TP | |
43ce9dda MK |
985 | .B EINVAL |
986 | .BR CLONE_NEWUTS | |
987 | was specified in | |
988 | .IR flags , | |
989 | but the kernel was not configured with the | |
990 | .B CONFIG_UTS | |
991 | option. | |
992 | .TP | |
c550a897 MK |
993 | .B EINVAL |
994 | .I child_stack | |
995 | is not aligned to a suitable boundary for this architecture. | |
996 | For example, on aarch64, | |
997 | .I child_stack | |
998 | must be a multiple of 16. | |
999 | .TP | |
fea681da MK |
1000 | .B ENOMEM |
1001 | Cannot allocate sufficient memory to allocate a task structure for the | |
1002 | child, or to copy those parts of the caller's context that need to be | |
1003 | copied. | |
1004 | .TP | |
1005 | .B EPERM | |
667417b3 | 1006 | .BR CLONE_NEWIPC , |
163bf178 | 1007 | .BR CLONE_NEWNET , |
43ce9dda MK |
1008 | .BR CLONE_NEWNS , |
1009 | .BR CLONE_NEWPID , | |
82ee147a | 1010 | or |
43ce9dda | 1011 | .BR CLONE_NEWUTS |
00b08db3 | 1012 | was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP). |
fea681da MK |
1013 | .TP |
1014 | .B EPERM | |
1015 | .B CLONE_PID | |
1016 | was specified by a process other than process 0. | |
365d292a MK |
1017 | .TP |
1018 | .B EPERM | |
1019 | .BR CLONE_NEWUSER | |
1020 | was specified in | |
1021 | .IR flags , | |
1022 | but either the effective user ID or the effective group ID of the caller | |
1023 | does not have a mapping in the parent namespace (see | |
f58fb24f | 1024 | .BR user_namespaces (7)). |
6fd119e7 | 1025 | .TP |
ac007938 MK |
1026 | .BR EPERM " (since Linux 3.9)" |
1027 | .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d | |
11a38815 AM |
1028 | .B CLONE_NEWUSER |
1029 | was specified in | |
ac007938 MK |
1030 | .I flags |
1031 | and the caller is in a chroot environment | |
1032 | .\" FIXME What is the rationale for this restriction? | |
1033 | (i.e., the caller's root directory does not match the root directory | |
1034 | of the mount namespace in which it resides). | |
1035 | .TP | |
1036 | .BR EUSERS " (since Linux 3.11)" | |
6fd119e7 MK |
1037 | .B CLONE_NEWUSER |
1038 | was specified in | |
1039 | .IR flags , | |
1040 | and the call would cause the limit on the number of | |
1041 | nested user namespaces to be exceeded. | |
1042 | See | |
1043 | .BR user_namespaces (7). | |
10e46057 NF |
1044 | .TP |
1045 | .BR ERESTARTNOINTR " (since Linux 2.6.17)" | |
11a6d050 | 1046 | .\" commit 4a2c7a7837da1b91468e50426066d988050e4d56 |
10e46057 NF |
1047 | System call was interrupted by a signal and will be restarted. |
1048 | (This can be seen only during a trace.) | |
365d292a MK |
1049 | .SH VERSIONS |
1050 | There is no entry for | |
1051 | .BR clone () | |
1052 | in libc5. | |
1053 | glibc2 provides | |
1054 | .BR clone () | |
1055 | as described in this manual page. | |
47297adb | 1056 | .SH CONFORMING TO |
a1d5f77c | 1057 | .BR clone () |
e585064b | 1058 | is Linux-specific and should not be used in programs |
a1d5f77c | 1059 | intended to be portable. |
fea681da | 1060 | .SH NOTES |
fd8a5be4 MK |
1061 | In the kernel 2.4.x series, |
1062 | .B CLONE_THREAD | |
1063 | generally does not make the parent of the new thread the same | |
1064 | as the parent of the calling process. | |
1065 | However, for kernel versions 2.4.7 to 2.4.18 the | |
1066 | .B CLONE_THREAD | |
1067 | flag implied the | |
c13182ef | 1068 | .B CLONE_PARENT |
fd8a5be4 | 1069 | flag (as in kernel 2.6). |
fea681da | 1070 | |
c13182ef MK |
1071 | For a while there was |
1072 | .B CLONE_DETACHED | |
a5053dcb | 1073 | (introduced in 2.5.32): |
c13182ef | 1074 | parent wants no child-exit signal. |
a5053dcb | 1075 | In 2.6.2 the need to give this |
c13182ef MK |
1076 | together with |
1077 | .B CLONE_THREAD | |
a5053dcb MK |
1078 | disappeared. |
1079 | This flag is still defined, but has no effect. | |
1080 | ||
34ccb744 | 1081 | On i386, |
a5a997ca MK |
1082 | .BR clone () |
1083 | should not be called through vsyscall, but directly through | |
1084 | .IR "int $0x80" . | |
31830ef0 MK |
1085 | .SH BUGS |
1086 | Versions of the GNU C library that include the NPTL threading library | |
c13182ef | 1087 | contain a wrapper function for |
0bfa087b | 1088 | .BR getpid (2) |
31830ef0 | 1089 | that performs caching of PIDs. |
c60237c9 MK |
1090 | This caching relies on support in the glibc wrapper for |
1091 | .BR clone (), | |
1092 | but as currently implemented, | |
1093 | the cache may not be up to date in some circumstances. | |
1094 | In particular, | |
1095 | if a signal is delivered to the child immediately after the | |
1096 | .BR clone () | |
1097 | call, then a call to | |
0b80cf56 | 1098 | .BR getpid (2) |
c60237c9 MK |
1099 | in a handler for the signal may return the PID |
1100 | of the calling process ("the parent"), | |
88619baf | 1101 | if the clone wrapper has not yet had a chance to update the PID |
c60237c9 MK |
1102 | cache in the child. |
1103 | (This discussion ignores the case where the child was created using | |
9291ce36 | 1104 | .BR CLONE_THREAD , |
c60237c9 | 1105 | when |
0b80cf56 | 1106 | .BR getpid (2) |
c60237c9 MK |
1107 | .I should |
1108 | return the same value in the child and in the process that called | |
1109 | .BR clone (), | |
a1d48abb | 1110 | since the caller and the child are in the same thread group. |
e7d807b7 | 1111 | The stale-cache problem also does not occur if the |
a1d48abb JR |
1112 | .I flags |
1113 | argument includes | |
1114 | .BR CLONE_VM .) | |
c60237c9 | 1115 | To get the truth, it may be necessary to use code such as the following: |
31830ef0 MK |
1116 | .nf |
1117 | ||
1118 | #include <syscall.h> | |
1119 | ||
1120 | pid_t mypid; | |
1121 | ||
1122 | mypid = syscall(SYS_getpid); | |
1123 | .fi | |
c60237c9 MK |
1124 | .\" See also the following bug reports |
1125 | .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521 | |
1126 | .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910 | |
8c7b566c | 1127 | .SH EXAMPLE |
8c7b566c | 1128 | The following program demonstrates the use of |
9c13072a | 1129 | .BR clone () |
8c7b566c MK |
1130 | to create a child process that executes in a separate UTS namespace. |
1131 | The child changes the hostname in its UTS namespace. | |
1132 | Both parent and child then display the system hostname, | |
1133 | making it possible to see that the hostname | |
1134 | differs in the UTS namespaces of the parent and child. | |
1135 | For an example of the use of this program, see | |
1136 | .BR setns (2). | |
f30b7415 | 1137 | .SS Program source |
8c7b566c MK |
1138 | .nf |
1139 | #define _GNU_SOURCE | |
1140 | #include <sys/wait.h> | |
1141 | #include <sys/utsname.h> | |
1142 | #include <sched.h> | |
1143 | #include <string.h> | |
1144 | #include <stdio.h> | |
1145 | #include <stdlib.h> | |
1146 | #include <unistd.h> | |
1147 | ||
1148 | #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\ | |
1149 | } while (0) | |
1150 | ||
1151 | static int /* Start function for cloned child */ | |
1152 | childFunc(void *arg) | |
1153 | { | |
1154 | struct utsname uts; | |
1155 | ||
1156 | /* Change hostname in UTS namespace of child */ | |
1157 | ||
1158 | if (sethostname(arg, strlen(arg)) == \-1) | |
1159 | errExit("sethostname"); | |
1160 | ||
07d4e6ea | 1161 | /* Retrieve and display hostname */ |
8c7b566c MK |
1162 | |
1163 | if (uname(&uts) == \-1) | |
1164 | errExit("uname"); | |
1165 | printf("uts.nodename in child: %s\\n", uts.nodename); | |
1166 | ||
1167 | /* Keep the namespace open for a while, by sleeping. | |
1168 | This allows some experimentation\-\-for example, another | |
1169 | process might join the namespace. */ | |
9f1b9726 | 1170 | |
8c7b566c MK |
1171 | sleep(200); |
1172 | ||
1173 | return 0; /* Child terminates now */ | |
1174 | } | |
1175 | ||
1176 | #define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */ | |
1177 | ||
1178 | int | |
1179 | main(int argc, char *argv[]) | |
1180 | { | |
1181 | char *stack; /* Start of stack buffer */ | |
1182 | char *stackTop; /* End of stack buffer */ | |
1183 | pid_t pid; | |
1184 | struct utsname uts; | |
1185 | ||
1186 | if (argc < 2) { | |
1187 | fprintf(stderr, "Usage: %s <child\-hostname>\\n", argv[0]); | |
1188 | exit(EXIT_SUCCESS); | |
1189 | } | |
1190 | ||
1191 | /* Allocate stack for child */ | |
1192 | ||
1193 | stack = malloc(STACK_SIZE); | |
1194 | if (stack == NULL) | |
1195 | errExit("malloc"); | |
1196 | stackTop = stack + STACK_SIZE; /* Assume stack grows downward */ | |
1197 | ||
1198 | /* Create child that has its own UTS namespace; | |
1199 | child commences execution in childFunc() */ | |
1200 | ||
1201 | pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]); | |
1202 | if (pid == \-1) | |
1203 | errExit("clone"); | |
1204 | printf("clone() returned %ld\\n", (long) pid); | |
1205 | ||
1206 | /* Parent falls through to here */ | |
1207 | ||
1208 | sleep(1); /* Give child time to change its hostname */ | |
1209 | ||
9f1b9726 | 1210 | /* Display hostname in parent\(aqs UTS namespace. This will be |
8c7b566c MK |
1211 | different from hostname in child\(aqs UTS namespace. */ |
1212 | ||
1213 | if (uname(&uts) == \-1) | |
1214 | errExit("uname"); | |
1215 | printf("uts.nodename in parent: %s\\n", uts.nodename); | |
1216 | ||
1217 | if (waitpid(pid, NULL, 0) == \-1) /* Wait for child */ | |
1218 | errExit("waitpid"); | |
1219 | printf("child has terminated\\n"); | |
1220 | ||
1221 | exit(EXIT_SUCCESS); | |
1222 | } | |
1223 | .fi | |
47297adb | 1224 | .SH SEE ALSO |
fea681da | 1225 | .BR fork (2), |
2b44301c | 1226 | .BR futex (2), |
fea681da MK |
1227 | .BR getpid (2), |
1228 | .BR gettid (2), | |
6f8746e4 | 1229 | .BR kcmp (2), |
f2d0bbf1 | 1230 | .BR set_thread_area (2), |
2b44301c | 1231 | .BR set_tid_address (2), |
8403481f | 1232 | .BR setns (2), |
f2d0bbf1 | 1233 | .BR tkill (2), |
5cc01e9c | 1234 | .BR unshare (2), |
fea681da | 1235 | .BR wait (2), |
3616b7c0 | 1236 | .BR capabilities (7), |
41096af1 | 1237 | .BR namespaces (7), |
3616b7c0 | 1238 | .BR pthreads (7) |