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