1 .\" Copyright (c) 1992 Drew Eckhardt <drew@cs.colorado.edu>, March 28, 1992
2 .\" and Copyright (c) Michael Kerrisk, 2001, 2002, 2005, 2013
4 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
5 .\" May be distributed under the GNU General Public License.
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
18 .\" Added CLONE_PARENT and revised entire page remove ambiguity
19 .\" between "calling process" and "parent process"
20 .\" Added CLONE_PTRACE and CLONE_VFORK
21 .\" Added EPERM and EINVAL error codes
22 .\" Renamed "__clone" to "clone" (which is the prototype in <sched.h>)
23 .\" various other minor tidy ups and clarifications.
24 .\" Modified 26 Jun 2001 by Michael Kerrisk <mtk.manpages@gmail.com>
25 .\" Updated notes for 2.4.7+ behavior of CLONE_THREAD
26 .\" Modified 15 Oct 2002 by Michael Kerrisk <mtk.manpages@gmail.com>
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
31 .\" Modified 2004-09-10 - added CLONE_PARENT_SETTID etc. - aeb.
32 .\" 2005-04-12, mtk, noted the PID caching behavior of NPTL's getpid()
33 .\" wrapper under BUGS.
34 .\" 2005-05-10, mtk, added CLONE_SYSVSEM, CLONE_UNTRACED, CLONE_STOPPED.
35 .\" 2005-05-17, mtk, Substantially enhanced discussion of CLONE_THREAD.
36 .\" 2008-11-18, mtk, order CLONE_* flags alphabetically
37 .\" 2008-11-18, mtk, document CLONE_NEWPID
38 .\" 2008-11-19, mtk, document CLONE_NEWUTS
39 .\" 2008-11-19, mtk, document CLONE_NEWIPC
40 .\" 2008-11-19, Jens Axboe, mtk, document CLONE_IO
42 .TH CLONE 2 2017-09-15 "Linux" "Linux Programmer's Manual"
44 clone, __clone2 \- create a child process
47 /* Prototype for the glibc wrapper function */
49 .B #define _GNU_SOURCE
52 .BI "int clone(int (*" "fn" ")(void *), void *" child_stack ,
53 .BI " int " flags ", void *" "arg" ", ... "
54 .BI " /* pid_t *" ptid ", void *" newtls \
55 ", pid_t *" ctid " */ );"
57 /* For the prototype of the raw system call, see NOTES */
61 creates a new process, in a manner similar to
64 This page describes both the glibc
66 wrapper function and the underlying system call on which it is based.
67 The main text describes the wrapper function;
68 the differences for the raw system call
69 are described toward the end of this page.
74 allows the child process to share parts of its execution context with
75 the calling process, such as the virtual address space, the table of file
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
85 is to implement threads: multiple flows of control in a program that
86 run concurrently in a shared address space.
88 When the child process is created with
90 it commences execution by calling the function pointed to by the argument
94 where execution continues in the child from the point
100 argument is passed as the argument of the function
105 function returns, the child process terminates.
106 The integer returned by
108 is the exit status for the child process.
109 The child process may also terminate explicitly by calling
111 or after receiving a fatal signal.
115 argument specifies the location of the stack used by the child process.
116 Since the child and calling process may share memory,
117 it is not possible for the child process to execute in the
118 same stack as the calling process.
119 The calling process must therefore
120 set up memory space for the child stack and pass a pointer to this
123 Stacks grow downward on all processors that run Linux
124 (except the HP PA processors), so
126 usually points to the topmost address of the memory space set up for
131 contains the number of the
132 .I "termination signal"
133 sent to the parent when the child dies.
134 If this signal is specified as anything other than
136 then the parent process must specify the
140 options when waiting for the child with
142 If no signal is specified, then the parent process is not signaled
143 when the child terminates.
146 may also be bitwise-ORed with zero or more of the following constants,
147 in order to specify what is shared between the calling process
148 and the child process:
150 .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)"
151 Clear (zero) the child thread ID at the location
153 in child memory when the child exits, and do a wakeup on the futex
155 The address involved may be changed by the
156 .BR set_tid_address (2)
158 This is used by threading libraries.
160 .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)"
161 Store the child thread ID at the location
163 in the child's memory.
164 The store operation completes before
166 returns control to user space.
168 .BR CLONE_FILES " (since Linux 2.0)"
171 is set, the calling process and the child process share the same file
173 Any file descriptor created by the calling process or by the child
174 process is also valid in the other process.
175 Similarly, if one of the processes closes a file descriptor,
176 or changes its associated flags (using the
179 operation), the other process is also affected.
180 If a process sharing a file descriptor table calls
182 its file descriptor table is duplicated (unshared).
186 is not set, the child process inherits a copy of all file descriptors
187 opened in the calling process at the time of
189 Subsequent operations that open or close file descriptors,
190 or change file descriptor flags,
191 performed by either the calling
192 process or the child process do not affect the other process.
194 that the duplicated file descriptors in the child refer to the same open file
195 descriptions as the corresponding file descriptors in the calling process,
196 and thus share file offsets and file status flags (see
199 .BR CLONE_FS " (since Linux 2.0)"
202 is set, the caller and the child process share the same filesystem
204 This includes the root of the filesystem, the current
205 working directory, and the umask.
211 performed by the calling process or the child process also affects the
216 is not set, the child process works on a copy of the filesystem
217 information of the calling process at the time of the
225 performed later by one of the processes do not affect the other process.
227 .BR CLONE_IO " (since Linux 2.6.25)"
230 is set, then the new process shares an I/O context with
232 If this flag is not set, then (as with
234 the new process has its own I/O context.
236 .\" The following based on text from Jens Axboe
237 The I/O context is the I/O scope of the disk scheduler (i.e.,
238 what the I/O scheduler uses to model scheduling of a process's I/O).
239 If processes share the same I/O context,
240 they are treated as one by the I/O scheduler.
241 As a consequence, they get to share disk time.
242 For some I/O schedulers,
243 .\" the anticipatory and CFQ scheduler
244 if two processes share an I/O context,
245 they will be allowed to interleave their disk access.
246 If several threads are doing I/O on behalf of the same process
248 for instance), they should employ
250 to get better I/O performance.
253 If the kernel is not configured with the
255 option, this flag is a no-op.
257 .BR CLONE_NEWCGROUP " (since Linux 4.6)"
258 Create the process in a new cgroup namespace.
259 If this flag is not set, then (as with
261 the process is created in the same cgroup namespaces as the calling process.
262 This flag is intended for the implementation of containers.
264 For further information on cgroup namespaces, see
265 .BR cgroup_namespaces (7).
267 Only a privileged process
268 .RB ( CAP_SYS_ADMIN )
270 .BR CLONE_NEWCGROUP .
273 .BR CLONE_NEWIPC " (since Linux 2.6.19)"
276 is set, then create the process in a new IPC namespace.
277 If this flag is not set, then (as with
279 the process is created in the same IPC namespace as
281 This flag is intended for the implementation of containers.
283 An IPC namespace provides an isolated view of System\ V IPC objects (see
285 and (since Linux 2.6.30)
286 .\" commit 7eafd7c74c3f2e67c27621b987b28397110d643f
287 .\" https://lwn.net/Articles/312232/
290 .BR mq_overview (7)).
291 The common characteristic of these IPC mechanisms is that IPC
292 objects are identified by mechanisms other than filesystem
295 Objects created in an IPC namespace are visible to all other processes
296 that are members of that namespace,
297 but are not visible to processes in other IPC namespaces.
299 When an IPC namespace is destroyed
300 (i.e., when the last process that is a member of the namespace terminates),
301 all IPC objects in the namespace are automatically destroyed.
303 Only a privileged process
304 .RB ( CAP_SYS_ADMIN )
307 This flag can't be specified in conjunction with
310 For further information on IPC namespaces, see
313 .BR CLONE_NEWNET " (since Linux 2.6.24)"
314 (The implementation of this flag was completed only
315 by about kernel version 2.6.29.)
319 is set, then create the process in a new network namespace.
320 If this flag is not set, then (as with
322 the process is created in the same network namespace as
324 This flag is intended for the implementation of containers.
326 A network namespace provides an isolated view of the networking stack
327 (network device interfaces, IPv4 and IPv6 protocol stacks,
328 IP routing tables, firewall rules, the
332 directory trees, sockets, etc.).
333 A physical network device can live in exactly one
337 device pair provides a pipe-like abstraction
338 that can be used to create tunnels between network namespaces,
339 and can be used to create a bridge to a physical network device
340 in another namespace.
342 When a network namespace is freed
343 (i.e., when the last process in the namespace terminates),
344 its physical network devices are moved back to the
345 initial network namespace (not to the parent of the process).
346 For further information on network namespaces, see
349 Only a privileged process
350 .RB ( CAP_SYS_ADMIN )
354 .BR CLONE_NEWNS " (since Linux 2.4.19)"
357 is set, the cloned child is started in a new mount namespace,
358 initialized with a copy of the namespace of the parent.
361 is not set, the child lives in the same mount
362 namespace as the parent.
364 Only a privileged process
365 .RB ( CAP_SYS_ADMIN )
368 It is not permitted to specify both
372 .\" See https://lwn.net/Articles/543273/
377 For further information on mount namespaces, see
380 .BR mount_namespaces (7).
382 .BR CLONE_NEWPID " (since Linux 2.6.24)"
383 .\" This explanation draws a lot of details from
384 .\" http://lwn.net/Articles/259217/
385 .\" Authors: Pavel Emelyanov <xemul@openvz.org>
386 .\" and Kir Kolyshkin <kir@openvz.org>
388 .\" The primary kernel commit is 30e49c263e36341b60b735cbef5ca37912549264
389 .\" Author: Pavel Emelyanov <xemul@openvz.org>
392 is set, then create the process in a new PID namespace.
393 If this flag is not set, then (as with
395 the process is created in the same PID namespace as
397 This flag is intended for the implementation of containers.
399 For further information on PID namespaces, see
402 .BR pid_namespaces (7).
404 Only a privileged process
405 .RB ( CAP_SYS_ADMIN )
408 This flag can't be specified in conjunction with
414 (This flag first became meaningful for
419 semantics were merged in Linux 3.5,
420 and the final pieces to make the user namespaces completely usable were
421 merged in Linux 3.8.)
425 is set, then create the process in a new user namespace.
426 If this flag is not set, then (as with
428 the process is created in the same user namespace as the calling process.
430 Before Linux 3.8, use of
432 required that the caller have three capabilities:
437 .\" Before Linux 2.6.29, it appears that only CAP_SYS_ADMIN was needed
438 Starting with Linux 3.8,
439 no privileges are needed to create a user namespace.
441 This flag can't be specified in conjunction with
445 For security reasons,
446 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
447 .\" https://lwn.net/Articles/543273/
448 .\" The fix actually went into 3.9 and into 3.8.3. However, user namespaces
449 .\" were, for practical purposes, unusable in earlier 3.8.x because of the
450 .\" various filesystems that didn't support userns.
452 cannot be specified in conjunction with
455 For further information on user namespaces, see
458 .BR user_namespaces (7).
460 .BR CLONE_NEWUTS " (since Linux 2.6.19)"
463 is set, then create the process in a new UTS namespace,
464 whose identifiers are initialized by duplicating the identifiers
465 from the UTS namespace of the calling process.
466 If this flag is not set, then (as with
468 the process is created in the same UTS namespace as
470 This flag is intended for the implementation of containers.
472 A UTS namespace is the set of identifiers returned by
474 among these, the domain name and the hostname can be modified by
475 .BR setdomainname (2)
479 Changes made to the identifiers in a UTS namespace
480 are visible to all other processes in the same namespace,
481 but are not visible to processes in other UTS namespaces.
483 Only a privileged process
484 .RB ( CAP_SYS_ADMIN )
488 For further information on UTS namespaces, see
491 .BR CLONE_PARENT " (since Linux 2.3.12)"
494 is set, then the parent of the new child (as returned by
496 will be the same as that of the calling process.
500 is not set, then (as with
502 the child's parent is the calling process.
504 Note that it is the parent process, as returned by
506 which is signaled when the child terminates, so that
509 is set, then the parent of the calling process, rather than the
510 calling process itself, will be signaled.
512 .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)"
513 Store the child thread ID at the location
515 in the parent's memory.
516 (In Linux 2.5.32-2.5.48 there was a flag
519 The store operation completes before
521 returns control to user space.
523 .BR CLONE_PID " (Linux 2.0 to 2.5.15)"
526 is set, the child process is created with the same process ID as
528 This is good for hacking the system, but otherwise
530 From Linux 2.3.21 onward, this flag could be
531 specified only by the system boot process (PID 0).
532 The flag disappeared completely from the kernel sources in Linux 2.5.16.
533 Since then, the kernel silently ignores this bit if it is specified in
536 .BR CLONE_PTRACE " (since Linux 2.2)"
539 is specified, and the calling process is being traced,
540 then trace the child also (see
543 .BR CLONE_SETTLS " (since Linux 2.5.32)"
544 The TLS (Thread Local Storage) descriptor is set to
547 The interpretation of
549 and the resulting effect is architecture dependent.
553 .IR "struct user_desc\ *"
555 .BR set_thread_area (2)).
556 On x86-64 it is the new value to be set for the %fs base register
561 On architectures with a dedicated TLS register, it is the new value
564 .BR CLONE_SIGHAND " (since Linux 2.0)"
567 is set, the calling process and the child process share the same table of
569 If the calling process or child process calls
571 to change the behavior associated with a signal, the behavior is
572 changed in the other process as well.
573 However, the calling process and child
574 processes still have distinct signal masks and sets of pending
576 So, one of them may block or unblock signals using
578 without affecting the other process.
582 is not set, the child process inherits a copy of the signal handlers
583 of the calling process at the time
588 performed later by one of the processes have no effect on the other
591 Since Linux 2.6.0-test6,
599 .BR CLONE_STOPPED " (since Linux 2.6.0-test2)"
602 is set, then the child is initially stopped (as though it was sent a
604 signal), and must be resumed by sending it a
610 from Linux 2.6.25 onward,
613 altogether in Linux 2.6.38.
614 Since then, the kernel silently ignores it without error.
615 .\" glibc 2.8 removed this defn from bits/sched.h
616 Starting with Linux 4.6, the same bit was reused for the
620 .BR CLONE_SYSVSEM " (since Linux 2.5.10)"
623 is set, then the child and the calling process share
624 a single list of System V semaphore adjustment
628 In this case, the shared list accumulates
630 values across all processes sharing the list,
631 and semaphore adjustments are performed only when the last process
632 that is sharing the list terminates (or ceases sharing the list using
634 If this flag is not set, then the child has a separate
636 list that is initially empty.
638 .BR CLONE_THREAD " (since Linux 2.4.0-test8)"
641 is set, the child is placed in the same thread group as the calling process.
642 To make the remainder of the discussion of
644 more readable, the term "thread" is used to refer to the
645 processes within a thread group.
647 Thread groups were a feature added in Linux 2.4 to support the
648 POSIX threads notion of a set of threads that share a single PID.
649 Internally, this shared PID is the so-called
650 thread group identifier (TGID) for the thread group.
651 Since Linux 2.4, calls to
653 return the TGID of the caller.
655 The threads within a group can be distinguished by their (system-wide)
656 unique thread IDs (TID).
657 A new thread's TID is available as the function result
658 returned to the caller of
660 and a thread can obtain
664 When a call is made to
668 then the resulting thread is placed in a new thread group
669 whose TGID is the same as the thread's TID.
672 of the new thread group.
674 A new thread created with
676 has the same parent process as the caller of
682 return the same value for all of the threads in a thread group.
685 thread terminates, the thread that created it using
689 (or other termination) signal;
690 nor can the status of such a thread be obtained
693 (The thread is said to be
696 After all of the threads in a thread group terminate
697 the parent process of the thread group is sent a
699 (or other termination) signal.
701 If any of the threads in a thread group performs an
703 then all threads other than the thread group leader are terminated,
704 and the new program is executed in the thread group leader.
706 If one of the threads in a thread group creates a child using
708 then any thread in the group can
719 (and note that, since Linux 2.6.0-test6,
725 Signals may be sent to a thread group as a whole (i.e., a TGID) using
727 or to a specific thread (i.e., TID) using
730 Signal dispositions and actions are process-wide:
731 if an unhandled signal is delivered to a thread, then
732 it will affect (terminate, stop, continue, be ignored in)
733 all members of the thread group.
735 Each thread has its own signal mask, as set by
737 but signals can be pending either: for the whole process
738 (i.e., deliverable to any member of the thread group),
741 or for an individual thread, when sent with
745 returns a signal set that is the union of the signals pending for the
746 whole process and the signals that are pending for the calling thread.
748 If a process-directed signal is delivered to a thread group,
749 and the thread group has installed a handler for the signal, then
750 the handler will be invoked in exactly one, arbitrarily selected
751 member of the thread group that has not blocked the signal.
752 If multiple threads in a group are waiting to accept the same signal using
754 the kernel will arbitrarily select one of these threads
755 to receive the signal.
757 .BR CLONE_UNTRACED " (since Linux 2.5.46)"
760 is specified, then a tracing process cannot force
762 on this child process.
764 .BR CLONE_VFORK " (since Linux 2.2)"
767 is set, the execution of the calling process is suspended
768 until the child releases its virtual memory
769 resources via a call to
778 is not set, then both the calling process and the child are schedulable
779 after the call, and an application should not rely on execution occurring
780 in any particular order.
782 .BR CLONE_VM " (since Linux 2.0)"
785 is set, the calling process and the child process run in the same memory
787 In particular, memory writes performed by the calling process
788 or by the child process are also visible in the other process.
789 Moreover, any memory mapping or unmapping performed with
793 by the child or calling process also affects the other process.
797 is not set, the child process runs in a separate copy of the memory
798 space of the calling process at the time of
800 Memory writes or file mappings/unmappings performed by one of the
801 processes do not affect the other, as with
806 wrapper function makes some changes
807 in the memory pointed to by
809 (changes required to set the stack up correctly for the child)
816 is used to recursively create children,
817 do not use the buffer employed for the parent's stack
818 as the stack of the child.
820 .SS C library/kernel differences
823 system call corresponds more closely to
825 in that execution in the child continues from the point of the
833 wrapper function are omitted.
835 Another difference for the raw
837 system call is that the
839 argument may be NULL,
840 in which case the child uses a duplicate of the parent's stack.
841 (Copy-on-write semantics ensure that the child gets separate copies
842 of stack pages when either process modifies the stack.)
843 In this case, for correct operation, the
845 option should not be specified.
848 the parent's memory because of the use of the
851 then no copy-on-write duplication occurs and chaos is likely to result.)
853 The order of the arguments also differs in the raw system call,
854 and there are variations in the arguments across architectures,
855 as detailed in the following paragraphs.
857 The raw system call interface on x86-64 and some other architectures
858 (including sh, tile, and alpha) is:
862 .BI "long clone(unsigned long " flags ", void *" child_stack ,
863 .BI " int *" ptid ", int *" ctid ,
864 .BI " unsigned long " newtls );
868 On x86-32, and several other common architectures
869 (including score, ARM, ARM 64, PA-RISC, arc, Power PC, xtensa,
871 .\" CONFIG_CLONE_BACKWARDS
872 the order of the last two arguments is reversed:
876 .BI "long clone(unsigned long " flags ", void *" child_stack ,
877 .BI " int *" ptid ", unsigned long " newtls ,
882 On the cris and s390 architectures,
883 .\" CONFIG_CLONE_BACKWARDS2
884 the order of the first two arguments is reversed:
888 .BI "long clone(void *" child_stack ", unsigned long " flags ,
889 .BI " int *" ptid ", int *" ctid ,
890 .BI " unsigned long " newtls );
894 On the microblaze architecture,
895 .\" CONFIG_CLONE_BACKWARDS3
896 an additional argument is supplied:
900 .BI "long clone(unsigned long " flags ", void *" child_stack ,
901 .BI " int " stack_size , "\fR /* Size of stack */"
902 .BI " int *" ptid ", int *" ctid ,
903 .BI " unsigned long " newtls );
907 .SS blackfin, m68k, and sparc
908 .\" Mike Frysinger noted in a 2013 mail:
909 .\" these arches don't define __ARCH_WANT_SYS_CLONE:
910 .\" blackfin ia64 m68k sparc
911 The argument-passing conventions on
912 blackfin, m68k, and sparc are different from the descriptions above.
913 For details, see the kernel (and glibc) source.
915 On ia64, a different interface is used:
918 .BI "int __clone2(int (*" "fn" ")(void *), "
919 .BI " void *" child_stack_base ", size_t " stack_size ,
920 .BI " int " flags ", void *" "arg" ", ... "
921 .BI " /* pid_t *" ptid ", struct user_desc *" tls \
922 ", pid_t *" ctid " */ );"
925 The prototype shown above is for the glibc wrapper function;
926 the raw system call interface has no
930 argument, and changes the order of the arguments so that
932 is the first argument, and
934 is the last argument.
937 operates in the same way as
941 points to the lowest address of the child's stack area,
944 specifies the size of the stack pointed to by
945 .IR child_stack_base .
946 .SS Linux 2.4 and earlier
947 In Linux 2.4 and earlier,
949 does not take arguments
955 .\" gettid(2) returns current->pid;
956 .\" getpid(2) returns current->tgid;
957 On success, the thread ID of the child process is returned
958 in the caller's thread of execution.
959 On failure, \-1 is returned
960 in the caller's context, no child process will be created, and
962 will be set appropriately.
966 Too many processes are already running; see
974 (Since Linux 2.6.0-test6.)
981 (Since Linux 2.5.35.)
985 .\" .B CLONE_DETACHED
989 .\" (Since Linux 2.6.0-test6.)
992 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
1000 .BR EINVAL " (since Linux 3.9)"
1021 and one (or both) of
1029 Returned by the glibc
1031 wrapper function when
1035 is specified as NULL.
1041 but the kernel was not configured with the
1051 but the kernel was not configured with the
1059 but the kernel was not configured with the
1067 but the kernel was not configured with the
1075 but the kernel was not configured with the
1081 is not aligned to a suitable boundary for this architecture.
1082 For example, on aarch64,
1084 must be a multiple of 16.
1087 Cannot allocate sufficient memory to allocate a task structure for the
1088 child, or to copy those parts of the caller's context that need to be
1091 .BR ENOSPC " (since Linux 3.7)"
1092 .\" commit f2302505775fd13ba93f034206f1e2a587017929
1094 was specified in flags,
1095 but the limit on the nesting depth of PID namespaces
1096 would have been exceeded; see
1097 .BR pid_namespaces (7).
1099 .BR ENOSPC " (since Linux 4.9; beforehand " EUSERS )
1103 and the call would cause the limit on the number of
1104 nested user namespaces to be exceeded.
1106 .BR user_namespaces (7).
1108 From Linux 3.11 to Linux 4.8, the error diagnosed in this case was
1111 .BR ENOSPC " (since Linux 4.9)"
1112 One of the values in
1114 specified the creation of a new user namespace,
1115 but doing so would have caused the limit defined by the corresponding file in
1118 For further details, see
1122 .BR CLONE_NEWCGROUP ,
1129 was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP).
1133 was specified by a process other than process 0.
1134 (This error occurs only on Linux 2.5.15 and earlier.)
1140 but either the effective user ID or the effective group ID of the caller
1141 does not have a mapping in the parent namespace (see
1142 .BR user_namespaces (7)).
1144 .BR EPERM " (since Linux 3.9)"
1145 .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d
1149 and the caller is in a chroot environment
1150 .\" FIXME What is the rationale for this restriction?
1151 (i.e., the caller's root directory does not match the root directory
1152 of the mount namespace in which it resides).
1154 .BR ERESTARTNOINTR " (since Linux 2.6.17)"
1155 .\" commit 4a2c7a7837da1b91468e50426066d988050e4d56
1156 System call was interrupted by a signal and will be restarted.
1157 (This can be seen only during a trace.)
1159 .BR EUSERS " (Linux 3.11 to Linux 4.8)"
1163 and the limit on the number of nested user namespaces would be exceeded.
1164 See the discussion of the
1168 .\" There is no entry for
1173 .\" as described in this manual page.
1176 is Linux-specific and should not be used in programs
1177 intended to be portable.
1181 system call can be used to test whether two processes share various
1182 resources such as a file descriptor table,
1183 System V semaphore undo operations, or a virtual address space.
1186 Handlers registered using
1187 .BR pthread_atfork (3)
1188 are not executed during a call to
1191 In the Linux 2.4.x series,
1193 generally does not make the parent of the new thread the same
1194 as the parent of the calling process.
1195 However, for kernel versions 2.4.7 to 2.4.18 the
1199 flag (as in Linux 2.6.0 and later).
1201 For a while there was
1203 (introduced in 2.5.32):
1204 parent wants no child-exit signal.
1205 In Linux 2.6.2, the need to give this flag together with
1208 This flag is still defined, but has no effect.
1212 should not be called through vsyscall, but directly through
1215 GNU C library versions 2.3.4 up to and including 2.24
1216 contained a wrapper function for
1218 that performed caching of PIDs.
1219 This caching relied on support in the glibc wrapper for
1221 but limitations in the implementation
1222 meant that the cache was not up to date in some circumstances.
1224 if a signal was delivered to the child immediately after the
1226 call, then a call to
1228 in a handler for the signal could return the PID
1229 of the calling process ("the parent"),
1230 if the clone wrapper had not yet had a chance to update the PID
1232 (This discussion ignores the case where the child was created using
1237 return the same value in the child and in the process that called
1239 since the caller and the child are in the same thread group.
1240 The stale-cache problem also does not occur if the
1244 To get the truth, it was sometimes necessary to use code such as the following:
1248 #include <syscall.h>
1252 mypid = syscall(SYS_getpid);
1255 .\" See also the following bug reports
1256 .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521
1257 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910
1259 Because of the stale-cache problem, as well as other problems noted in
1261 the PID caching feature was removed in glibc 2.25.
1263 The following program demonstrates the use of
1265 to create a child process that executes in a separate UTS namespace.
1266 The child changes the hostname in its UTS namespace.
1267 Both parent and child then display the system hostname,
1268 making it possible to see that the hostname
1269 differs in the UTS namespaces of the parent and child.
1270 For an example of the use of this program, see
1275 #include <sys/wait.h>
1276 #include <sys/utsname.h>
1283 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
1286 static int /* Start function for cloned child */
1287 childFunc(void *arg)
1291 /* Change hostname in UTS namespace of child */
1293 if (sethostname(arg, strlen(arg)) == \-1)
1294 errExit("sethostname");
1296 /* Retrieve and display hostname */
1298 if (uname(&uts) == \-1)
1300 printf("uts.nodename in child: %s\\n", uts.nodename);
1302 /* Keep the namespace open for a while, by sleeping.
1303 This allows some experimentation\-\-for example, another
1304 process might join the namespace. */
1308 return 0; /* Child terminates now */
1311 #define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */
1314 main(int argc, char *argv[])
1316 char *stack; /* Start of stack buffer */
1317 char *stackTop; /* End of stack buffer */
1322 fprintf(stderr, "Usage: %s <child\-hostname>\\n", argv[0]);
1326 /* Allocate stack for child */
1328 stack = malloc(STACK_SIZE);
1331 stackTop = stack + STACK_SIZE; /* Assume stack grows downward */
1333 /* Create child that has its own UTS namespace;
1334 child commences execution in childFunc() */
1336 pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]);
1339 printf("clone() returned %ld\\n", (long) pid);
1341 /* Parent falls through to here */
1343 sleep(1); /* Give child time to change its hostname */
1345 /* Display hostname in parent\(aqs UTS namespace. This will be
1346 different from hostname in child\(aqs UTS namespace. */
1348 if (uname(&uts) == \-1)
1350 printf("uts.nodename in parent: %s\\n", uts.nodename);
1352 if (waitpid(pid, NULL, 0) == \-1) /* Wait for child */
1354 printf("child has terminated\\n");
1365 .BR set_thread_area (2),
1366 .BR set_tid_address (2),
1371 .BR capabilities (7),