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
195 open file descriptions as the corresponding file descriptors
196 in the calling process,
197 and thus share file offsets and file status flags (see
200 .BR CLONE_FS " (since Linux 2.0)"
203 is set, the caller and the child process share the same filesystem
205 This includes the root of the filesystem, the current
206 working directory, and the umask.
212 performed by the calling process or the child process also affects the
217 is not set, the child process works on a copy of the filesystem
218 information of the calling process at the time of the
226 performed later by one of the processes do not affect the other process.
228 .BR CLONE_IO " (since Linux 2.6.25)"
231 is set, then the new process shares an I/O context with
233 If this flag is not set, then (as with
235 the new process has its own I/O context.
237 .\" The following based on text from Jens Axboe
238 The I/O context is the I/O scope of the disk scheduler (i.e.,
239 what the I/O scheduler uses to model scheduling of a process's I/O).
240 If processes share the same I/O context,
241 they are treated as one by the I/O scheduler.
242 As a consequence, they get to share disk time.
243 For some I/O schedulers,
244 .\" the anticipatory and CFQ scheduler
245 if two processes share an I/O context,
246 they will be allowed to interleave their disk access.
247 If several threads are doing I/O on behalf of the same process
249 for instance), they should employ
251 to get better I/O performance.
254 If the kernel is not configured with the
256 option, this flag is a no-op.
258 .BR CLONE_NEWCGROUP " (since Linux 4.6)"
259 Create the process in a new cgroup namespace.
260 If this flag is not set, then (as with
262 the process is created in the same cgroup namespaces as the calling process.
263 This flag is intended for the implementation of containers.
265 For further information on cgroup namespaces, see
266 .BR cgroup_namespaces (7).
268 Only a privileged process
269 .RB ( CAP_SYS_ADMIN )
271 .BR CLONE_NEWCGROUP .
274 .BR CLONE_NEWIPC " (since Linux 2.6.19)"
277 is set, then create the process in a new IPC namespace.
278 If this flag is not set, then (as with
280 the process is created in the same IPC namespace as
282 This flag is intended for the implementation of containers.
284 An IPC namespace provides an isolated view of System\ V IPC objects (see
286 and (since Linux 2.6.30)
287 .\" commit 7eafd7c74c3f2e67c27621b987b28397110d643f
288 .\" https://lwn.net/Articles/312232/
291 .BR mq_overview (7)).
292 The common characteristic of these IPC mechanisms is that IPC
293 objects are identified by mechanisms other than filesystem
296 Objects created in an IPC namespace are visible to all other processes
297 that are members of that namespace,
298 but are not visible to processes in other IPC namespaces.
300 When an IPC namespace is destroyed
301 (i.e., when the last process that is a member of the namespace terminates),
302 all IPC objects in the namespace are automatically destroyed.
304 Only a privileged process
305 .RB ( CAP_SYS_ADMIN )
308 This flag can't be specified in conjunction with
311 For further information on IPC namespaces, see
314 .BR CLONE_NEWNET " (since Linux 2.6.24)"
315 (The implementation of this flag was completed only
316 by about kernel version 2.6.29.)
320 is set, then create the process in a new network namespace.
321 If this flag is not set, then (as with
323 the process is created in the same network namespace as
325 This flag is intended for the implementation of containers.
327 A network namespace provides an isolated view of the networking stack
328 (network device interfaces, IPv4 and IPv6 protocol stacks,
329 IP routing tables, firewall rules, the
333 directory trees, sockets, etc.).
334 A physical network device can live in exactly one
338 device pair provides a pipe-like abstraction
339 that can be used to create tunnels between network namespaces,
340 and can be used to create a bridge to a physical network device
341 in another namespace.
343 When a network namespace is freed
344 (i.e., when the last process in the namespace terminates),
345 its physical network devices are moved back to the
346 initial network namespace (not to the parent of the process).
347 For further information on network namespaces, see
350 Only a privileged process
351 .RB ( CAP_SYS_ADMIN )
355 .BR CLONE_NEWNS " (since Linux 2.4.19)"
358 is set, the cloned child is started in a new mount namespace,
359 initialized with a copy of the namespace of the parent.
362 is not set, the child lives in the same mount
363 namespace as the parent.
365 Only a privileged process
366 .RB ( CAP_SYS_ADMIN )
369 It is not permitted to specify both
373 .\" See https://lwn.net/Articles/543273/
378 For further information on mount namespaces, see
381 .BR mount_namespaces (7).
383 .BR CLONE_NEWPID " (since Linux 2.6.24)"
384 .\" This explanation draws a lot of details from
385 .\" http://lwn.net/Articles/259217/
386 .\" Authors: Pavel Emelyanov <xemul@openvz.org>
387 .\" and Kir Kolyshkin <kir@openvz.org>
389 .\" The primary kernel commit is 30e49c263e36341b60b735cbef5ca37912549264
390 .\" Author: Pavel Emelyanov <xemul@openvz.org>
393 is set, then create the process in a new PID namespace.
394 If this flag is not set, then (as with
396 the process is created in the same PID namespace as
398 This flag is intended for the implementation of containers.
400 For further information on PID namespaces, see
403 .BR pid_namespaces (7).
405 Only a privileged process
406 .RB ( CAP_SYS_ADMIN )
409 This flag can't be specified in conjunction with
415 (This flag first became meaningful for
420 semantics were merged in Linux 3.5,
421 and the final pieces to make the user namespaces completely usable were
422 merged in Linux 3.8.)
426 is set, then create the process in a new user namespace.
427 If this flag is not set, then (as with
429 the process is created in the same user namespace as the calling process.
431 Before Linux 3.8, use of
433 required that the caller have three capabilities:
438 .\" Before Linux 2.6.29, it appears that only CAP_SYS_ADMIN was needed
439 Starting with Linux 3.8,
440 no privileges are needed to create a user namespace.
442 This flag can't be specified in conjunction with
446 For security reasons,
447 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
448 .\" https://lwn.net/Articles/543273/
449 .\" The fix actually went into 3.9 and into 3.8.3. However, user namespaces
450 .\" were, for practical purposes, unusable in earlier 3.8.x because of the
451 .\" various filesystems that didn't support userns.
453 cannot be specified in conjunction with
456 For further information on user namespaces, see
459 .BR user_namespaces (7).
461 .BR CLONE_NEWUTS " (since Linux 2.6.19)"
464 is set, then create the process in a new UTS namespace,
465 whose identifiers are initialized by duplicating the identifiers
466 from the UTS namespace of the calling process.
467 If this flag is not set, then (as with
469 the process is created in the same UTS namespace as
471 This flag is intended for the implementation of containers.
473 A UTS namespace is the set of identifiers returned by
475 among these, the domain name and the hostname can be modified by
476 .BR setdomainname (2)
480 Changes made to the identifiers in a UTS namespace
481 are visible to all other processes in the same namespace,
482 but are not visible to processes in other UTS namespaces.
484 Only a privileged process
485 .RB ( CAP_SYS_ADMIN )
489 For further information on UTS namespaces, see
492 .BR CLONE_PARENT " (since Linux 2.3.12)"
495 is set, then the parent of the new child (as returned by
497 will be the same as that of the calling process.
501 is not set, then (as with
503 the child's parent is the calling process.
505 Note that it is the parent process, as returned by
507 which is signaled when the child terminates, so that
510 is set, then the parent of the calling process, rather than the
511 calling process itself, will be signaled.
513 .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)"
514 Store the child thread ID at the location
516 in the parent's memory.
517 (In Linux 2.5.32-2.5.48 there was a flag
520 The store operation completes before
522 returns control to user space.
524 .BR CLONE_PID " (Linux 2.0 to 2.5.15)"
527 is set, the child process is created with the same process ID as
529 This is good for hacking the system, but otherwise
531 From Linux 2.3.21 onward, this flag could be
532 specified only by the system boot process (PID 0).
533 The flag disappeared completely from the kernel sources in Linux 2.5.16.
534 Since then, the kernel silently ignores this bit if it is specified in
537 .BR CLONE_PTRACE " (since Linux 2.2)"
540 is specified, and the calling process is being traced,
541 then trace the child also (see
544 .BR CLONE_SETTLS " (since Linux 2.5.32)"
545 The TLS (Thread Local Storage) descriptor is set to
548 The interpretation of
550 and the resulting effect is architecture dependent.
554 .IR "struct user_desc\ *"
556 .BR set_thread_area (2)).
557 On x86-64 it is the new value to be set for the %fs base register
562 On architectures with a dedicated TLS register, it is the new value
565 .BR CLONE_SIGHAND " (since Linux 2.0)"
568 is set, the calling process and the child process share the same table of
570 If the calling process or child process calls
572 to change the behavior associated with a signal, the behavior is
573 changed in the other process as well.
574 However, the calling process and child
575 processes still have distinct signal masks and sets of pending
577 So, one of them may block or unblock signals using
579 without affecting the other process.
583 is not set, the child process inherits a copy of the signal handlers
584 of the calling process at the time
589 performed later by one of the processes have no effect on the other
593 .\" Precisely: Linux 2.6.0-test6
601 .BR CLONE_STOPPED " (since Linux 2.6.0)"
602 .\" Precisely: Linux 2.6.0-test2
605 is set, then the child is initially stopped (as though it was sent a
607 signal), and must be resumed by sending it a
613 from Linux 2.6.25 onward,
616 altogether in Linux 2.6.38.
617 Since then, the kernel silently ignores it without error.
618 .\" glibc 2.8 removed this defn from bits/sched.h
619 Starting with Linux 4.6, the same bit was reused for the
623 .BR CLONE_SYSVSEM " (since Linux 2.5.10)"
626 is set, then the child and the calling process share
627 a single list of System V semaphore adjustment
631 In this case, the shared list accumulates
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
637 If this flag is not set, then the child has a separate
639 list that is initially empty.
641 .BR CLONE_THREAD " (since Linux 2.4.0)"
642 .\" Precisely: Linux 2.6.0-test8
645 is set, the child is placed in the same thread group as the calling process.
646 To make the remainder of the discussion of
648 more readable, the term "thread" is used to refer to the
649 processes within a thread group.
651 Thread groups were a feature added in Linux 2.4 to support the
652 POSIX threads notion of a set of threads that share a single PID.
653 Internally, this shared PID is the so-called
654 thread group identifier (TGID) for the thread group.
655 Since Linux 2.4, calls to
657 return the TGID of the caller.
659 The threads within a group can be distinguished by their (system-wide)
660 unique thread IDs (TID).
661 A new thread's TID is available as the function result
662 returned to the caller of
664 and a thread can obtain
668 When a call is made to
672 then the resulting thread is placed in a new thread group
673 whose TGID is the same as the thread's TID.
676 of the new thread group.
678 A new thread created with
680 has the same parent process as the caller of
686 return the same value for all of the threads in a thread group.
689 thread terminates, the thread that created it using
693 (or other termination) signal;
694 nor can the status of such a thread be obtained
697 (The thread is said to be
700 After all of the threads in a thread group terminate
701 the parent process of the thread group is sent a
703 (or other termination) signal.
705 If any of the threads in a thread group performs an
707 then all threads other than the thread group leader are terminated,
708 and the new program is executed in the thread group leader.
710 If one of the threads in a thread group creates a child using
712 then any thread in the group can
723 (and note that, since Linux 2.6.0,
724 .\" Precisely: Linux 2.6.0-test6
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
738 A signal may be process-directed or thread-directed.
739 A process-directed signal is targeted at a thread group (i.e., a TGID),
740 and is delivered to an arbitrarily selected thread from among those
741 that are not blocking the signal.
742 A signal may be process directed because it was generated by the kernel
743 for reasons other than a hardware exception, or because it was sent using
747 A thread-directed signal is targeted at (i.e., delivered to)
749 A signal may be thread directed because it was sent using
752 .BR pthread_sigqueue (3),
753 or because the thread executed a machine language instruction that triggered
755 (e.g., invalid memory access triggering
757 or a floating-point exception triggering
762 returns a signal set that is the union of the pending process-directed
763 signals and the signals that are pending for the calling thread.
765 If a process-directed signal is delivered to a thread group,
766 and the thread group has installed a handler for the signal, then
767 the handler will be invoked in exactly one, arbitrarily selected
768 member of the thread group that has not blocked the signal.
769 If multiple threads in a group are waiting to accept the same signal using
771 the kernel will arbitrarily select one of these threads
772 to receive the signal.
774 .BR CLONE_UNTRACED " (since Linux 2.5.46)"
777 is specified, then a tracing process cannot force
779 on this child process.
781 .BR CLONE_VFORK " (since Linux 2.2)"
784 is set, the execution of the calling process is suspended
785 until the child releases its virtual memory
786 resources via a call to
795 is not set, then both the calling process and the child are schedulable
796 after the call, and an application should not rely on execution occurring
797 in any particular order.
799 .BR CLONE_VM " (since Linux 2.0)"
802 is set, the calling process and the child process run in the same memory
804 In particular, memory writes performed by the calling process
805 or by the child process are also visible in the other process.
806 Moreover, any memory mapping or unmapping performed with
810 by the child or calling process also affects the other process.
814 is not set, the child process runs in a separate copy of the memory
815 space of the calling process at the time of
817 Memory writes or file mappings/unmappings performed by one of the
818 processes do not affect the other, as with
823 wrapper function makes some changes
824 in the memory pointed to by
826 (changes required to set the stack up correctly for the child)
833 is used to recursively create children,
834 do not use the buffer employed for the parent's stack
835 as the stack of the child.
837 .SS C library/kernel differences
840 system call corresponds more closely to
842 in that execution in the child continues from the point of the
850 wrapper function are omitted.
852 Another difference for the raw
854 system call is that the
856 argument may be NULL,
857 in which case the child uses a duplicate of the parent's stack.
858 (Copy-on-write semantics ensure that the child gets separate copies
859 of stack pages when either process modifies the stack.)
860 In this case, for correct operation, the
862 option should not be specified.
865 the parent's memory because of the use of the
868 then no copy-on-write duplication occurs and chaos is likely to result.)
870 The order of the arguments also differs in the raw system call,
871 and there are variations in the arguments across architectures,
872 as detailed in the following paragraphs.
874 The raw system call interface on x86-64 and some other architectures
875 (including sh, tile, ia-64, and alpha) is:
879 .BI "long clone(unsigned long " flags ", void *" child_stack ,
880 .BI " int *" ptid ", int *" ctid ,
881 .BI " unsigned long " newtls );
885 On x86-32, and several other common architectures
886 (including score, ARM, ARM 64, PA-RISC, arc, Power PC, xtensa,
888 .\" CONFIG_CLONE_BACKWARDS
889 the order of the last two arguments is reversed:
893 .BI "long clone(unsigned long " flags ", void *" child_stack ,
894 .BI " int *" ptid ", unsigned long " newtls ,
899 On the cris and s390 architectures,
900 .\" CONFIG_CLONE_BACKWARDS2
901 the order of the first two arguments is reversed:
905 .BI "long clone(void *" child_stack ", unsigned long " flags ,
906 .BI " int *" ptid ", int *" ctid ,
907 .BI " unsigned long " newtls );
911 On the microblaze architecture,
912 .\" CONFIG_CLONE_BACKWARDS3
913 an additional argument is supplied:
917 .BI "long clone(unsigned long " flags ", void *" child_stack ,
918 .BI " int " stack_size , "\fR /* Size of stack */"
919 .BI " int *" ptid ", int *" ctid ,
920 .BI " unsigned long " newtls );
924 .SS blackfin, m68k, and sparc
925 .\" Mike Frysinger noted in a 2013 mail:
926 .\" these arches don't define __ARCH_WANT_SYS_CLONE:
927 .\" blackfin ia64 m68k sparc
928 The argument-passing conventions on
929 blackfin, m68k, and sparc are different from the descriptions above.
930 For details, see the kernel (and glibc) source.
932 On ia64, a different interface is used:
936 .BI "int __clone2(int (*" "fn" ")(void *), "
937 .BI " void *" child_stack_base ", size_t " stack_size ,
938 .BI " int " flags ", void *" "arg" ", ... "
939 .BI " /* pid_t *" ptid ", struct user_desc *" tls \
940 ", pid_t *" ctid " */ );"
944 The prototype shown above is for the glibc wrapper function;
945 for the system call itself,
946 the prototype can be described as follows (it is identical to the
948 prototype on microblaze):
952 .BI "long clone2(unsigned long " flags ", void *" child_stack_base ,
953 .BI " int " stack_size , "\fR /* Size of stack */"
954 .BI " int *" ptid ", int *" ctid ,
955 .BI " unsigned long " tls );
960 operates in the same way as
964 points to the lowest address of the child's stack area,
967 specifies the size of the stack pointed to by
968 .IR child_stack_base .
969 .SS Linux 2.4 and earlier
970 In Linux 2.4 and earlier,
972 does not take arguments
978 .\" gettid(2) returns current->pid;
979 .\" getpid(2) returns current->tgid;
980 On success, the thread ID of the child process is returned
981 in the caller's thread of execution.
982 On failure, \-1 is returned
983 in the caller's context, no child process will be created, and
985 will be set appropriately.
989 Too many processes are already running; see
998 .\" Precisely: Linux 2.6.0-test6
1005 (Since Linux 2.5.35.)
1008 .\" Precisely one of
1009 .\" .B CLONE_DETACHED
1013 .\" (Since Linux 2.6.0-test6.)
1017 was specified, but the current process previously called
1023 to reassociate itself with a PID namespace.
1026 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
1034 .BR EINVAL " (since Linux 3.9)"
1055 and one (or both) of
1063 Returned by the glibc
1065 wrapper function when
1069 is specified as NULL.
1075 but the kernel was not configured with the
1085 but the kernel was not configured with the
1093 but the kernel was not configured with the
1101 but the kernel was not configured with the
1109 but the kernel was not configured with the
1115 is not aligned to a suitable boundary for this architecture.
1116 For example, on aarch64,
1118 must be a multiple of 16.
1121 Cannot allocate sufficient memory to allocate a task structure for the
1122 child, or to copy those parts of the caller's context that need to be
1125 .BR ENOSPC " (since Linux 3.7)"
1126 .\" commit f2302505775fd13ba93f034206f1e2a587017929
1128 was specified in flags,
1129 but the limit on the nesting depth of PID namespaces
1130 would have been exceeded; see
1131 .BR pid_namespaces (7).
1133 .BR ENOSPC " (since Linux 4.9; beforehand " EUSERS )
1137 and the call would cause the limit on the number of
1138 nested user namespaces to be exceeded.
1140 .BR user_namespaces (7).
1142 From Linux 3.11 to Linux 4.8, the error diagnosed in this case was
1145 .BR ENOSPC " (since Linux 4.9)"
1146 One of the values in
1148 specified the creation of a new user namespace,
1149 but doing so would have caused the limit defined by the corresponding file in
1152 For further details, see
1156 .BR CLONE_NEWCGROUP ,
1163 was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP).
1167 was specified by a process other than process 0.
1168 (This error occurs only on Linux 2.5.15 and earlier.)
1174 but either the effective user ID or the effective group ID of the caller
1175 does not have a mapping in the parent namespace (see
1176 .BR user_namespaces (7)).
1178 .BR EPERM " (since Linux 3.9)"
1179 .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d
1183 and the caller is in a chroot environment
1184 .\" FIXME What is the rationale for this restriction?
1185 (i.e., the caller's root directory does not match the root directory
1186 of the mount namespace in which it resides).
1188 .BR ERESTARTNOINTR " (since Linux 2.6.17)"
1189 .\" commit 4a2c7a7837da1b91468e50426066d988050e4d56
1190 System call was interrupted by a signal and will be restarted.
1191 (This can be seen only during a trace.)
1193 .BR EUSERS " (Linux 3.11 to Linux 4.8)"
1197 and the limit on the number of nested user namespaces would be exceeded.
1198 See the discussion of the
1202 .\" There is no entry for
1207 .\" as described in this manual page.
1210 is Linux-specific and should not be used in programs
1211 intended to be portable.
1215 system call can be used to test whether two processes share various
1216 resources such as a file descriptor table,
1217 System V semaphore undo operations, or a virtual address space.
1220 Handlers registered using
1221 .BR pthread_atfork (3)
1222 are not executed during a call to
1225 In the Linux 2.4.x series,
1227 generally does not make the parent of the new thread the same
1228 as the parent of the calling process.
1229 However, for kernel versions 2.4.7 to 2.4.18 the
1233 flag (as in Linux 2.6.0 and later).
1235 For a while there was
1237 (introduced in 2.5.32):
1238 parent wants no child-exit signal.
1239 In Linux 2.6.2, the need to give this flag together with
1242 This flag is still defined, but has no effect.
1246 should not be called through vsyscall, but directly through
1249 GNU C library versions 2.3.4 up to and including 2.24
1250 contained a wrapper function for
1252 that performed caching of PIDs.
1253 This caching relied on support in the glibc wrapper for
1255 but limitations in the implementation
1256 meant that the cache was not up to date in some circumstances.
1258 if a signal was delivered to the child immediately after the
1260 call, then a call to
1262 in a handler for the signal could return the PID
1263 of the calling process ("the parent"),
1264 if the clone wrapper had not yet had a chance to update the PID
1266 (This discussion ignores the case where the child was created using
1271 return the same value in the child and in the process that called
1273 since the caller and the child are in the same thread group.
1274 The stale-cache problem also does not occur if the
1278 To get the truth, it was sometimes necessary to use code such as the following:
1282 #include <syscall.h>
1286 mypid = syscall(SYS_getpid);
1289 .\" See also the following bug reports
1290 .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521
1291 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910
1293 Because of the stale-cache problem, as well as other problems noted in
1295 the PID caching feature was removed in glibc 2.25.
1297 The following program demonstrates the use of
1299 to create a child process that executes in a separate UTS namespace.
1300 The child changes the hostname in its UTS namespace.
1301 Both parent and child then display the system hostname,
1302 making it possible to see that the hostname
1303 differs in the UTS namespaces of the parent and child.
1304 For an example of the use of this program, see
1309 #include <sys/wait.h>
1310 #include <sys/utsname.h>
1317 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
1320 static int /* Start function for cloned child */
1321 childFunc(void *arg)
1325 /* Change hostname in UTS namespace of child */
1327 if (sethostname(arg, strlen(arg)) == \-1)
1328 errExit("sethostname");
1330 /* Retrieve and display hostname */
1332 if (uname(&uts) == \-1)
1334 printf("uts.nodename in child: %s\\n", uts.nodename);
1336 /* Keep the namespace open for a while, by sleeping.
1337 This allows some experimentation\-\-for example, another
1338 process might join the namespace. */
1342 return 0; /* Child terminates now */
1345 #define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */
1348 main(int argc, char *argv[])
1350 char *stack; /* Start of stack buffer */
1351 char *stackTop; /* End of stack buffer */
1356 fprintf(stderr, "Usage: %s <child\-hostname>\\n", argv[0]);
1360 /* Allocate stack for child */
1362 stack = malloc(STACK_SIZE);
1365 stackTop = stack + STACK_SIZE; /* Assume stack grows downward */
1367 /* Create child that has its own UTS namespace;
1368 child commences execution in childFunc() */
1370 pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]);
1373 printf("clone() returned %ld\\n", (long) pid);
1375 /* Parent falls through to here */
1377 sleep(1); /* Give child time to change its hostname */
1379 /* Display hostname in parent\(aqs UTS namespace. This will be
1380 different from hostname in child\(aqs UTS namespace. */
1382 if (uname(&uts) == \-1)
1384 printf("uts.nodename in parent: %s\\n", uts.nodename);
1386 if (waitpid(pid, NULL, 0) == \-1) /* Wait for child */
1388 printf("child has terminated\\n");
1399 .BR set_thread_area (2),
1400 .BR set_tid_address (2),
1405 .BR capabilities (7),