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