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 2016-03-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 ", struct user_desc *" tls \
55 ", pid_t *" ctid " */ );"
57 /* Prototype for the raw system call */
59 .BI "long clone(unsigned long " flags ", void *" child_stack ,
60 .BI " void *" ptid ", void *" ctid ,
61 .BI " struct pt_regs *" regs );
65 creates a new process, in a manner similar to
68 This page describes both the glibc
70 wrapper function and the underlying system call on which it is based.
71 The main text describes the wrapper function;
72 the differences for the raw system call
73 are described toward the end of this page.
78 allows the child process to share parts of its execution context with
79 the calling process, such as the memory space, the table of file
80 descriptors, and the table of signal handlers.
81 (Note that on this manual
82 page, "calling process" normally corresponds to "parent process".
83 But see the description of
89 is to implement threads: multiple threads of control in a program that
90 run concurrently in a shared memory space.
92 When the child process is created with
94 it executes the function
98 where execution continues in the child from the point
104 argument is a pointer to a function that is called by the child
105 process at the beginning of its execution.
108 argument is passed to the
114 function application returns, the child process terminates.
115 The integer returned by
117 is the exit code for the child process.
118 The child process may also terminate explicitly by calling
120 or after receiving a fatal signal.
124 argument specifies the location of the stack used by the child process.
125 Since the child and calling process may share memory,
126 it is not possible for the child process to execute in the
127 same stack as the calling process.
128 The calling process must therefore
129 set up memory space for the child stack and pass a pointer to this
132 Stacks grow downward on all processors that run Linux
133 (except the HP PA processors), so
135 usually points to the topmost address of the memory space set up for
140 contains the number of the
141 .I "termination signal"
142 sent to the parent when the child dies.
143 If this signal is specified as anything other than
145 then the parent process must specify the
149 options when waiting for the child with
151 If no signal is specified, then the parent process is not signaled
152 when the child terminates.
155 may also be bitwise-or'ed with zero or more of the following constants,
156 in order to specify what is shared between the calling process
157 and the child process:
159 .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)"
160 Erase the child thread ID at the location
162 in child memory when the child exits, and do a wakeup on the futex
164 The address involved may be changed by the
165 .BR set_tid_address (2)
167 This is used by threading libraries.
169 .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)"
170 Store the child thread ID at the location
172 in the child's memory.
174 .BR CLONE_FILES " (since Linux 2.0)"
177 is set, the calling process and the child process share the same file
179 Any file descriptor created by the calling process or by the child
180 process is also valid in the other process.
181 Similarly, if one of the processes closes a file descriptor,
182 or changes its associated flags (using the
185 operation), the other process is also affected.
186 If a process sharing a file descriptor table calls
188 its file descriptor table is duplicated (unshared).
192 is not set, the child process inherits a copy of all file descriptors
193 opened in the calling process at the time of
195 (The duplicated file descriptors in the child refer to the
196 same open file descriptions (see
198 as the corresponding file descriptors in the calling process.)
199 Subsequent operations that open or close file descriptors,
200 or change file descriptor flags,
201 performed by either the calling
202 process or the child process do not affect the other process.
204 .BR CLONE_FS " (since Linux 2.0)"
207 is set, the caller and the child process share the same filesystem
209 This includes the root of the filesystem, the current
210 working directory, and the umask.
216 performed by the calling process or the child process also affects the
221 is not set, the child process works on a copy of the filesystem
222 information of the calling process at the time of the
229 performed later by one of the processes do not affect the other process.
231 .BR CLONE_IO " (since Linux 2.6.25)"
234 is set, then the new process shares an I/O context with
236 If this flag is not set, then (as with
238 the new process has its own I/O context.
240 .\" The following based on text from Jens Axboe
241 The I/O context is the I/O scope of the disk scheduler (i.e.,
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
252 for instance), they should employ
254 to get better I/O performance.
257 If the kernel is not configured with the
259 option, this flag is a no-op.
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
265 the process is created in the same cgroup namespaces as the calling process.
266 This flag is intended for the implementation of containers.
268 For further information on cgroup namespaces, see
269 .BR cgroup_namespaces (7).
271 Only a privileged process
272 .RB ( CAP_SYS_ADMIN )
274 .BR CLONE_NEWCGROUP .
277 .BR CLONE_NEWIPC " (since Linux 2.6.19)"
280 is set, then create the process in a new IPC namespace.
281 If this flag is not set, then (as with
283 the process is created in the same IPC namespace as
285 This flag is intended for the implementation of containers.
287 An IPC namespace provides an isolated view of System\ V IPC objects (see
289 and (since Linux 2.6.30)
290 .\" commit 7eafd7c74c3f2e67c27621b987b28397110d643f
291 .\" https://lwn.net/Articles/312232/
294 .BR mq_overview (7)).
295 The common characteristic of these IPC mechanisms is that IPC
296 objects are identified by mechanisms other than filesystem
299 Objects created in an IPC namespace are visible to all other processes
300 that are members of that namespace,
301 but are not visible to processes in other IPC namespaces.
303 When an IPC namespace is destroyed
304 (i.e., when the last process that is a member of the namespace terminates),
305 all IPC objects in the namespace are automatically destroyed.
307 Only a privileged process
308 .RB ( CAP_SYS_ADMIN )
311 This flag can't be specified in conjunction with
314 For further information on IPC namespaces, see
317 .BR CLONE_NEWNET " (since Linux 2.6.24)"
318 (The implementation of this flag was completed only
319 by about kernel version 2.6.29.)
323 is set, then create the process in a new network namespace.
324 If this flag is not set, then (as with
326 the process is created in the same network namespace as
328 This flag is intended for the implementation of containers.
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
336 directory trees, sockets, etc.).
337 A physical network device can live in exactly one
339 A virtual network device ("veth") pair provides a pipe-like abstraction
340 .\" FIXME . Add pointer to veth(4) page when it is eventually completed
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.
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).
349 For further information on network namespaces, see
352 Only a privileged process
353 .RB ( CAP_SYS_ADMIN )
357 .BR CLONE_NEWNS " (since Linux 2.4.19)"
360 is set, the cloned child is started in a new mount namespace,
361 initialized with a copy of the namespace of the parent.
364 is not set, the child lives in the same mount
365 namespace as the parent.
367 For further information on mount namespaces, see
370 Only a privileged process
371 .RB ( CAP_SYS_ADMIN )
374 It is not permitted to specify both
378 .\" See https://lwn.net/Articles/543273/
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 For further information on user namespaces, see
434 .BR user_namespaces (7)
436 Before Linux 3.8, use of
438 required that the caller have three capabilities:
443 .\" Before Linux 2.6.29, it appears that only CAP_SYS_ADMIN was needed
444 Starting with Linux 3.8,
445 no privileges are needed to create a user namespace.
447 This flag can't be specified in conjunction with
451 For security reasons,
452 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
453 .\" https://lwn.net/Articles/543273/
454 .\" The fix actually went into 3.9 and into 3.8.3. However, user namespaces
455 .\" were, for practical purposes, unusable in earlier 3.8.x because of the
456 .\" various filesystems that didn't support userns.
458 cannot be specified in conjunction with
461 For further information on user namespaces, see
462 .BR user_namespaces (7).
464 .BR CLONE_NEWUTS " (since Linux 2.6.19)"
467 is set, then create the process in a new UTS namespace,
468 whose identifiers are initialized by duplicating the identifiers
469 from the UTS namespace of the calling process.
470 If this flag is not set, then (as with
472 the process is created in the same UTS namespace as
474 This flag is intended for the implementation of containers.
476 A UTS namespace is the set of identifiers returned by
478 among these, the domain name and the hostname can be modified by
479 .BR setdomainname (2)
483 Changes made to the identifiers in a UTS namespace
484 are visible to all other processes in the same namespace,
485 but are not visible to processes in other UTS namespaces.
487 Only a privileged process
488 .RB ( CAP_SYS_ADMIN )
492 For further information on UTS namespaces, see
495 .BR CLONE_PARENT " (since Linux 2.3.12)"
498 is set, then the parent of the new child (as returned by
500 will be the same as that of the calling process.
504 is not set, then (as with
506 the child's parent is the calling process.
508 Note that it is the parent process, as returned by
510 which is signaled when the child terminates, so that
513 is set, then the parent of the calling process, rather than the
514 calling process itself, will be signaled.
516 .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)"
517 Store the child thread ID at the location
519 in the parent's memory.
520 (In Linux 2.5.32-2.5.48 there was a flag
524 .BR CLONE_PID " (obsolete)"
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 Since 2.3.21 this flag can be
532 specified only by the system boot process (PID 0).
533 It disappeared in Linux 2.5.16.
534 Since then, the kernel silently ignores it without error.
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)"
546 argument is the new TLS (Thread Local Storage) descriptor.
548 .BR set_thread_area (2).)
550 .BR CLONE_SIGHAND " (since Linux 2.0)"
553 is set, the calling process and the child process share the same table of
555 If the calling process or child process calls
557 to change the behavior associated with a signal, the behavior is
558 changed in the other process as well.
559 However, the calling process and child
560 processes still have distinct signal masks and sets of pending
562 So, one of them may block or unblock some signals using
564 without affecting the other process.
568 is not set, the child process inherits a copy of the signal handlers
569 of the calling process at the time
574 performed later by one of the processes have no effect on the other
577 Since Linux 2.6.0-test6,
585 .BR CLONE_STOPPED " (since Linux 2.6.0-test2)"
588 is set, then the child is initially stopped (as though it was sent a
590 signal), and must be resumed by sending it a
596 from Linux 2.6.25 onward,
599 altogether in Linux 2.6.38.
600 Since then, the kernel silently ignores it without error.
601 .\" glibc 2.8 removed this defn from bits/sched.h
602 Starting with Linux 4.6, the same bit was reused for the
606 .BR CLONE_SYSVSEM " (since Linux 2.5.10)"
609 is set, then the child and the calling process share
610 a single list of System V semaphore adjustment
614 In this case, the shared list accumulates
616 values across all processes sharing the list,
617 and semaphore adjustments are performed only when the last process
618 that is sharing the list terminates (or ceases sharing the list using
620 If this flag is not set, then the child has a separate
622 list that is initially empty.
624 .BR CLONE_THREAD " (since Linux 2.4.0-test8)"
627 is set, the child is placed in the same thread group as the calling process.
628 To make the remainder of the discussion of
630 more readable, the term "thread" is used to refer to the
631 processes within a thread group.
633 Thread groups were a feature added in Linux 2.4 to support the
634 POSIX threads notion of a set of threads that share a single PID.
635 Internally, this shared PID is the so-called
636 thread group identifier (TGID) for the thread group.
637 Since Linux 2.4, calls to
639 return the TGID of the caller.
641 The threads within a group can be distinguished by their (system-wide)
642 unique thread IDs (TID).
643 A new thread's TID is available as the function result
644 returned to the caller of
646 and a thread can obtain
650 When a call is made to
654 then the resulting thread is placed in a new thread group
655 whose TGID is the same as the thread's TID.
658 of the new thread group.
660 A new thread created with
662 has the same parent process as the caller of
668 return the same value for all of the threads in a thread group.
671 thread terminates, the thread that created it using
675 (or other termination) signal;
676 nor can the status of such a thread be obtained
679 (The thread is said to be
682 After all of the threads in a thread group terminate
683 the parent process of the thread group is sent a
685 (or other termination) signal.
687 If any of the threads in a thread group performs an
689 then all threads other than the thread group leader are terminated,
690 and the new program is executed in the thread group leader.
692 If one of the threads in a thread group creates a child using
694 then any thread in the group can
705 (and note that, since Linux 2.6.0-test6,
711 Signals may be sent to a thread group as a whole (i.e., a TGID) using
713 or to a specific thread (i.e., TID) using
716 Signal dispositions and actions are process-wide:
717 if an unhandled signal is delivered to a thread, then
718 it will affect (terminate, stop, continue, be ignored in)
719 all members of the thread group.
721 Each thread has its own signal mask, as set by
723 but signals can be pending either: for the whole process
724 (i.e., deliverable to any member of the thread group),
727 or for an individual thread, when sent with
731 returns a signal set that is the union of the signals pending for the
732 whole process and the signals that are pending for the calling thread.
736 is used to send a signal to a thread group,
737 and the thread group has installed a handler for the signal, then
738 the handler will be invoked in exactly one, arbitrarily selected
739 member of the thread group that has not blocked the signal.
740 If multiple threads in a group are waiting to accept the same signal using
742 the kernel will arbitrarily select one of these threads
743 to receive a signal sent using
746 .BR CLONE_UNTRACED " (since Linux 2.5.46)"
749 is specified, then a tracing process cannot force
751 on this child process.
753 .BR CLONE_VFORK " (since Linux 2.2)"
756 is set, the execution of the calling process is suspended
757 until the child releases its virtual memory
758 resources via a call to
767 is not set, then both the calling process and the child are schedulable
768 after the call, and an application should not rely on execution occurring
769 in any particular order.
771 .BR CLONE_VM " (since Linux 2.0)"
774 is set, the calling process and the child process run in the same memory
776 In particular, memory writes performed by the calling process
777 or by the child process are also visible in the other process.
778 Moreover, any memory mapping or unmapping performed with
782 by the child or calling process also affects the other process.
786 is not set, the child process runs in a separate copy of the memory
787 space of the calling process at the time of
789 Memory writes or file mappings/unmappings performed by one of the
790 processes do not affect the other, as with
792 .SS C library/kernel differences
795 system call corresponds more closely to
797 in that execution in the child continues from the point of the
805 wrapper function are omitted.
806 Furthermore, the argument order changes.
807 The raw system call interface on x86 and many other architectures is roughly:
811 .BI "long clone(unsigned long " flags ", void *" child_stack ,
812 .BI " void *" ptid ", void *" ctid ,
813 .BI " struct pt_regs *" regs );
817 Another difference for the raw system call is that the
819 argument may be zero, in which case copy-on-write semantics ensure that the
820 child gets separate copies of stack pages when either process modifies
822 In this case, for correct operation, the
824 option should not be specified.
826 For some architectures, the order of the arguments for the system call
827 differs from that shown above.
828 On the score, microblaze, ARM, ARM 64, PA-RISC, arc, Power PC, xtensa,
829 and MIPS architectures,
830 the order of the fourth and fifth arguments is reversed.
831 On the cris and s390 architectures,
832 the order of the first and second arguments is reversed.
833 .SS blackfin, m68k, and sparc
834 The argument-passing conventions on
835 blackfin, m68k, and sparc are different from the descriptions above.
836 For details, see the kernel (and glibc) source.
838 On ia64, a different interface is used:
841 .BI "int __clone2(int (*" "fn" ")(void *), "
842 .BI " void *" child_stack_base ", size_t " stack_size ,
843 .BI " int " flags ", void *" "arg" ", ... "
844 .BI " /* pid_t *" ptid ", struct user_desc *" tls \
845 ", pid_t *" ctid " */ );"
848 The prototype shown above is for the glibc wrapper function;
849 the raw system call interface has no
853 argument, and changes the order of the arguments so that
855 is the first argument, and
857 is the last argument.
860 operates in the same way as
864 points to the lowest address of the child's stack area,
867 specifies the size of the stack pointed to by
868 .IR child_stack_base .
869 .SS Linux 2.4 and earlier
870 In Linux 2.4 and earlier,
872 does not take arguments
878 .\" gettid(2) returns current->pid;
879 .\" getpid(2) returns current->tgid;
880 On success, the thread ID of the child process is returned
881 in the caller's thread of execution.
882 On failure, \-1 is returned
883 in the caller's context, no child process will be created, and
885 will be set appropriately.
889 Too many processes are already running; see
897 (Since Linux 2.6.0-test6.)
904 (Since Linux 2.5.35.)
908 .\" .B CLONE_DETACHED
912 .\" (Since Linux 2.6.0-test6.)
915 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
923 .BR EINVAL " (since Linux 3.9)"
954 when a zero value is specified for
961 but the kernel was not configured with the
971 but the kernel was not configured with the
979 but the kernel was not configured with the
987 but the kernel was not configured with the
993 is not aligned to a suitable boundary for this architecture.
994 For example, on aarch64,
996 must be a multiple of 16.
999 Cannot allocate sufficient memory to allocate a task structure for the
1000 child, or to copy those parts of the caller's context that need to be
1010 was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP).
1014 was specified by a process other than process 0.
1020 but either the effective user ID or the effective group ID of the caller
1021 does not have a mapping in the parent namespace (see
1022 .BR user_namespaces (7)).
1024 .BR EPERM " (since Linux 3.9)"
1025 .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d
1029 and the caller is in a chroot environment
1030 .\" FIXME What is the rationale for this restriction?
1031 (i.e., the caller's root directory does not match the root directory
1032 of the mount namespace in which it resides).
1034 .BR EUSERS " (since Linux 3.11)"
1038 and the call would cause the limit on the number of
1039 nested user namespaces to be exceeded.
1041 .BR user_namespaces (7).
1043 .BR ERESTARTNOINTR " (since Linux 2.6.17)"
1044 .\" commit 4a2c7a7837da1b91468e50426066d988050e4d56
1045 System call was interrupted by a signal and will be restarted.
1046 (This can be seen only during a trace.)
1048 There is no entry for
1053 as described in this manual page.
1056 is Linux-specific and should not be used in programs
1057 intended to be portable.
1059 In the kernel 2.4.x series,
1061 generally does not make the parent of the new thread the same
1062 as the parent of the calling process.
1063 However, for kernel versions 2.4.7 to 2.4.18 the
1067 flag (as in kernel 2.6).
1069 For a while there was
1071 (introduced in 2.5.32):
1072 parent wants no child-exit signal.
1073 In 2.6.2 the need to give this
1077 This flag is still defined, but has no effect.
1081 should not be called through vsyscall, but directly through
1084 Versions of the GNU C library that include the NPTL threading library
1085 contain a wrapper function for
1087 that performs caching of PIDs.
1088 This caching relies on support in the glibc wrapper for
1090 but as currently implemented,
1091 the cache may not be up to date in some circumstances.
1093 if a signal is delivered to the child immediately after the
1095 call, then a call to
1097 in a handler for the signal may return the PID
1098 of the calling process ("the parent"),
1099 if the clone wrapper has not yet had a chance to update the PID
1101 (This discussion ignores the case where the child was created using
1106 return the same value in the child and in the process that called
1108 since the caller and the child are in the same thread group.
1109 The stale-cache problem also does not occur if the
1113 To get the truth, it may be necessary to use code such as the following:
1116 #include <syscall.h>
1120 mypid = syscall(SYS_getpid);
1122 .\" See also the following bug reports
1123 .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521
1124 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910
1126 The following program demonstrates the use of
1128 to create a child process that executes in a separate UTS namespace.
1129 The child changes the hostname in its UTS namespace.
1130 Both parent and child then display the system hostname,
1131 making it possible to see that the hostname
1132 differs in the UTS namespaces of the parent and child.
1133 For an example of the use of this program, see
1138 #include <sys/wait.h>
1139 #include <sys/utsname.h>
1146 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
1149 static int /* Start function for cloned child */
1150 childFunc(void *arg)
1154 /* Change hostname in UTS namespace of child */
1156 if (sethostname(arg, strlen(arg)) == \-1)
1157 errExit("sethostname");
1159 /* Retrieve and display hostname */
1161 if (uname(&uts) == \-1)
1163 printf("uts.nodename in child: %s\\n", uts.nodename);
1165 /* Keep the namespace open for a while, by sleeping.
1166 This allows some experimentation\-\-for example, another
1167 process might join the namespace. */
1171 return 0; /* Child terminates now */
1174 #define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */
1177 main(int argc, char *argv[])
1179 char *stack; /* Start of stack buffer */
1180 char *stackTop; /* End of stack buffer */
1185 fprintf(stderr, "Usage: %s <child\-hostname>\\n", argv[0]);
1189 /* Allocate stack for child */
1191 stack = malloc(STACK_SIZE);
1194 stackTop = stack + STACK_SIZE; /* Assume stack grows downward */
1196 /* Create child that has its own UTS namespace;
1197 child commences execution in childFunc() */
1199 pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]);
1202 printf("clone() returned %ld\\n", (long) pid);
1204 /* Parent falls through to here */
1206 sleep(1); /* Give child time to change its hostname */
1208 /* Display hostname in parent\(aqs UTS namespace. This will be
1209 different from hostname in child\(aqs UTS namespace. */
1211 if (uname(&uts) == \-1)
1213 printf("uts.nodename in parent: %s\\n", uts.nodename);
1215 if (waitpid(pid, NULL, 0) == \-1) /* Wait for child */
1217 printf("child has terminated\\n");
1228 .BR set_thread_area (2),
1229 .BR set_tid_address (2),
1234 .BR capabilities (7),