OSDN Git Service

Complete 4 sched pages
[linuxjm/LDP_man-pages.git] / original / man2 / clone.2
index 86fe954..ec6b1f9 100644 (file)
@@ -1,8 +1,10 @@
-.\" Hey Emacs! This file is -*- nroff -*- source.
-.\"
 .\" Copyright (c) 1992 Drew Eckhardt <drew@cs.colorado.edu>, March 28, 1992
-.\" and Copyright (c) Michael Kerrisk, 2001, 2002, 2005
+.\" and Copyright (c) Michael Kerrisk, 2001, 2002, 2005, 2013
+.\"
+.\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
 .\" May be distributed under the GNU General Public License.
+.\" %%%LICENSE_END
+.\"
 .\" Modified by Michael Haardt <michael@moria.de>
 .\" Modified 24 Jul 1993 by Rik Faith <faith@cs.unc.edu>
 .\" Modified 21 Aug 1994 by Michael Chastain <mec@shell.portal.com>:
 .\" 2008-11-19, mtk, document CLONE_NEWIPC
 .\" 2008-11-19, Jens Axboe, mtk, document CLONE_IO
 .\"
-.\" FIXME Document CLONE_NEWUSER, which is new in 2.6.23
-.\"       (also supported for unshare()?)
-.\"
-.TH CLONE 2 2011-09-08 "Linux" "Linux Programmer's Manual"
+.TH CLONE 2 2014-09-21 "Linux" "Linux Programmer's Manual"
 .SH NAME
 clone, __clone2 \- create a child process
 .SH SYNOPSIS
 .nf
-.BR "#define _GNU_SOURCE" "             /* See feature_test_macros(7) */"
-.\" Actually _BSD_SOURCE || _SVID_SOURCE
-.\" See http://sources.redhat.com/bugzilla/show_bug.cgi?id=4749
+/* Prototype for the glibc wrapper function */
+
 .B #include <sched.h>
 
 .BI "int clone(int (*" "fn" ")(void *), void *" child_stack ,
 .BI "          int " flags ", void *" "arg" ", ... "
 .BI "          /* pid_t *" ptid ", struct user_desc *" tls \
 ", pid_t *" ctid " */ );"
+
+/* Prototype for the raw system call */
+
+.BI "long clone(unsigned long " flags ", void *" child_stack ,
+.BI "          void *" ptid ", void *" ctid ,
+.BI "          struct pt_regs *" regs );
 .fi
+.sp
+.in -4n
+Feature Test Macro Requirements for glibc wrapper function (see
+.BR feature_test_macros (7)):
+.in
+.sp
+.BR clone ():
+.ad l
+.RS 4
+.PD 0
+.TP 4
+Since glibc 2.14:
+_GNU_SOURCE
+.TP 4
+.\" See http://sources.redhat.com/bugzilla/show_bug.cgi?id=4749
+Before glibc 2.14:
+_BSD_SOURCE || _SVID_SOURCE
+    /* _GNU_SOURCE also suffices */
+.PD
+.RE
+.ad b
 .SH DESCRIPTION
 .BR clone ()
 creates a new process, in a manner similar to
 .BR fork (2).
-It is actually a library function layered on top of the underlying
+
+This page describes both the glibc
 .BR clone ()
-system call, hereinafter referred to as
-.BR sys_clone .
-A description of
-.B sys_clone
-is given toward the end of this page.
+wrapper function and the underlying system call on which it is based.
+The main text describes the wrapper function;
+the differences for the raw system call
+are described toward the end of this page.
 
 Unlike
 .BR fork (2),
-these calls
-allow the child process to share parts of its execution context with
+.BR clone ()
+allows the child process to share parts of its execution context with
 the calling process, such as the memory space, the table of file
 descriptors, and the table of signal handlers.
 (Note that on this manual
@@ -87,7 +112,6 @@ run concurrently in a shared memory space.
 When the child process is created with
 .BR clone (),
 it executes the function
-application
 .IR fn ( arg ).
 (This differs from
 .BR fork (2),
@@ -167,7 +191,7 @@ Store child thread ID at location
 .I ctid
 in child memory.
 .TP
-.B CLONE_FILES
+.BR CLONE_FILES " (since Linux 2.0)"
 If
 .B CLONE_FILES
 is set, the calling process and the child process share the same file
@@ -194,12 +218,12 @@ or change file descriptor flags,
 performed by either the calling
 process or the child process do not affect the other process.
 .TP
-.B CLONE_FS
+.BR CLONE_FS " (since Linux 2.0)"
 If
 .B CLONE_FS
-is set, the caller and the child process share the same file system
+is set, the caller and the child process share the same filesystem
 information.
-This includes the root of the file system, the current
+This includes the root of the filesystem, the current
 working directory, and the umask.
 Any call to
 .BR chroot (2),
@@ -211,7 +235,7 @@ other process.
 
 If
 .B CLONE_FS
-is not set, the child process works on a copy of the file system
+is not set, the child process works on a copy of the filesystem
 information of the calling process at the time of the
 .BR clone ()
 call.
@@ -261,39 +285,45 @@ the process is created in the same IPC namespace as
 the calling process.
 This flag is intended for the implementation of containers.
 
-An IPC namespace consists of the set of identifiers for
-System V IPC objects.
-(These objects are created using
-.BR msgctl (2),
-.BR semctl (2),
-and
-.BR shmctl (2)).
+An IPC namespace provides an isolated view of System\ V IPC objects (see
+.BR svipc (7))
+and (since Linux 2.6.30)
+.\" commit 7eafd7c74c3f2e67c27621b987b28397110d643f
+.\" https://lwn.net/Articles/312232/
+POSIX message queues
+(see
+.BR mq_overview (7)).
+The common characteristic of these IPC mechanisms is that IPC
+objects are identified by mechanisms other than filesystem
+pathnames.
+
 Objects created in an IPC namespace are visible to all other processes
 that are members of that namespace,
 but are not visible to processes in other IPC namespaces.
 
 When an IPC namespace is destroyed
-(i.e, when the last process that is a member of the namespace terminates),
+(i.e., when the last process that is a member of the namespace terminates),
 all IPC objects in the namespace are automatically destroyed.
 
-Use of this flag requires: a kernel configured with the
-.B CONFIG_SYSVIPC
-and
-.B CONFIG_IPC_NS
-options and that the process be privileged
-.RB ( CAP_SYS_ADMIN ).
+Only a privileged process
+.RB ( CAP_SYS_ADMIN )
+can employ
+.BR CLONE_NEWIPC .
 This flag can't be specified in conjunction with
 .BR CLONE_SYSVSEM .
+
+For further information on IPC namespaces, see
+.BR namespaces (7).
 .TP
 .BR CLONE_NEWNET " (since Linux 2.6.24)"
-(The implementation of this flag was only completed
+(The implementation of this flag was completed only
 by about kernel version 2.6.29.)
 
 If
 .B CLONE_NEWNET
 is set, then create the process in a new network namespace.
 If this flag is not set, then (as with
-.BR fork (2)),
+.BR fork (2))
 the process is created in the same network namespace as
 the calling process.
 This flag is intended for the implementation of containers.
@@ -308,6 +338,7 @@ directory trees, sockets, etc.).
 A physical network device can live in exactly one
 network namespace.
 A virtual network device ("veth") pair provides a pipe-like abstraction
+.\" FIXME . Add pointer to veth(4) page when it is eventually completed
 that can be used to create tunnels between network namespaces,
 and can be used to create a bridge to a physical network device
 in another namespace.
@@ -316,51 +347,36 @@ When a network namespace is freed
 (i.e., when the last process in the namespace terminates),
 its physical network devices are moved back to the
 initial network namespace (not to the parent of the process).
+For further information on network namespaces, see
+.BR namespaces (7).
 
-Use of this flag requires: a kernel configured with the
-.B CONFIG_NET_NS
-option and that the process be privileged
-.RB ( CAP_SYS_ADMIN ).
+Only a privileged process
+.RB ( CAP_SYS_ADMIN )
+can employ
+.BR CLONE_NEWNET .
 .TP
 .BR CLONE_NEWNS " (since Linux 2.4.19)"
-Start the child in a new mount namespace.
-
-Every process lives in a mount namespace.
-The
-.I namespace
-of a process is the data (the set of mounts) describing the file hierarchy
-as seen by that process.
-After a
-.BR fork (2)
-or
-.BR clone ()
-where the
+If
 .B CLONE_NEWNS
-flag is not set, the child lives in the same mount
+is set, the cloned child is started in a new mount namespace,
+initialized with a copy of the namespace of the parent.
+If
+.B CLONE_NEWNS
+is not set, the child lives in the same mount
 namespace as the parent.
-The system calls
-.BR mount (2)
-and
-.BR umount (2)
-change the mount namespace of the calling process, and hence affect
-all processes that live in the same namespace, but do not affect
-processes in a different mount namespace.
 
-After a
-.BR clone ()
-where the
-.B CLONE_NEWNS
-flag is set, the cloned child is started in a new mount namespace,
-initialized with a copy of the namespace of the parent.
+For further information on mount namespaces, see
+.BR namespaces (7).
 
-Only a privileged process (one having the \fBCAP_SYS_ADMIN\fP capability)
-may specify the
-.B CLONE_NEWNS
-flag.
+Only a privileged process
+.RB ( CAP_SYS_ADMIN )
+can employ
+.BR CLONE_NEWNS .
 It is not permitted to specify both
 .B CLONE_NEWNS
 and
 .B CLONE_FS
+.\" See https://lwn.net/Articles/543273/
 in the same
 .BR clone ()
 call.
@@ -377,73 +393,74 @@ If
 .B CLONE_NEWPID
 is set, then create the process in a new PID namespace.
 If this flag is not set, then (as with
-.BR fork (2)),
+.BR fork (2))
 the process is created in the same PID namespace as
 the calling process.
 This flag is intended for the implementation of containers.
 
-A PID namespace provides an isolated environment for PIDs:
-PIDs in a new namespace start at 1,
-somewhat like a standalone system, and calls to
-.BR fork (2),
-.BR vfork (2),
+For further information on PID namespaces, see
+.BR namespaces (7)
+and
+.BR pid_namespaces (7)
+
+Only a privileged process
+.RB ( CAP_SYS_ADMIN )
+can employ
+.BR CLONE_NEWPID .
+This flag can't be specified in conjunction with
+.BR CLONE_THREAD
 or
+.BR CLONE_PARENT .
+.TP
+.BR CLONE_NEWUSER
+(This flag first became meaningful for
+.BR clone ()
+in Linux 2.6.23,
+the current
 .BR clone ()
-will produce processes with PIDs that are unique within the namespace.
+semantics were merged in Linux 3.5,
+and the final pieces to make the user namespaces completely usable were
+merged in Linux 3.8.)
 
-The first process created in a new namespace
-(i.e., the process created using the
-.BR CLONE_NEWPID
-flag) has the PID 1, and is the "init" process for the namespace.
-Children that are orphaned within the namespace will be reparented
-to this process rather than
-.BR init (8).
-Unlike the traditional
-.B init
-process, the "init" process of a PID namespace can terminate,
-and if it does, all of the processes in the namespace are terminated.
-
-PID namespaces form a hierarchy.
-When a new PID namespace is created,
-the processes in that namespace are visible
-in the PID namespace of the process that created the new namespace;
-analogously, if the parent PID namespace is itself
-the child of another PID namespace,
-then processes in the child and parent PID namespaces will both be
-visible in the grandparent PID namespace.
-Conversely, the processes in the "child" PID namespace do not see
-the processes in the parent namespace.
-The existence of a namespace hierarchy means that each process
-may now have multiple PIDs:
-one for each namespace in which it is visible;
-each of these PIDs is unique within the corresponding namespace.
-(A call to
-.BR getpid (2)
-always returns the PID associated with the namespace in which
-the process lives.)
-
-After creating the new namespace,
-it is useful for the child to change its root directory
-and mount a new procfs instance at
-.I /proc
-so that tools such as
-.BR ps (1)
-work correctly.
-.\" mount -t proc proc /proc
-(If
-.BR CLONE_NEWNS
-is also included in
-.IR flags ,
-then it isn't necessary to change the root directory:
-a new procfs instance can be mounted directly over
-.IR /proc .)
+If
+.B CLONE_NEWUSER
+is set, then create the process in a new user namespace.
+If this flag is not set, then (as with
+.BR fork (2))
+the process is created in the same user namespace as the calling process.
+
+For further information on user namespaces, see
+.BR namespaces (7)
+and
+.BR user_namespaces (7)
+
+Before Linux 3.8, use of
+.BR CLONE_NEWUSER
+required that the caller have three capabilities:
+.BR CAP_SYS_ADMIN ,
+.BR CAP_SETUID ,
+and
+.BR CAP_SETGID .
+.\" Before Linux 2.6.29, it appears that only CAP_SYS_ADMIN was needed
+Starting with Linux 3.8,
+no privileges are needed to create a user namespace.
 
-Use of this flag requires: a kernel configured with the
-.B CONFIG_PID_NS
-option and that the process be privileged
-.RB ( CAP_SYS_ADMIN ).
 This flag can't be specified in conjunction with
-.BR CLONE_THREAD .
+.BR CLONE_THREAD
+or
+.BR CLONE_PARENT .
+For security reasons,
+.\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
+.\" https://lwn.net/Articles/543273/
+.\" The fix actually went into 3.9 and into 3.8.3. However, user namespaces
+.\" were, for practical purposes, unusable in earlier 3.8.x because of the
+.\" various filesystems that didn't support userns.
+.BR CLONE_NEWUSER
+cannot be specified in conjunction with
+.BR CLONE_FS .
+
+For further information on user namespaces, see
+.BR user_namespaces (7).
 .TP
 .BR CLONE_NEWUTS " (since Linux 2.6.19)"
 If
@@ -452,27 +469,29 @@ is set, then create the process in a new UTS namespace,
 whose identifiers are initialized by duplicating the identifiers
 from the UTS namespace of the calling process.
 If this flag is not set, then (as with
-.BR fork (2)),
+.BR fork (2))
 the process is created in the same UTS namespace as
 the calling process.
 This flag is intended for the implementation of containers.
 
 A UTS namespace is the set of identifiers returned by
 .BR uname (2);
-among these, the domain name and the host name can be modified by
+among these, the domain name and the hostname can be modified by
 .BR setdomainname (2)
 and
-.BR
 .BR sethostname (2),
 respectively.
 Changes made to the identifiers in a UTS namespace
 are visible to all other processes in the same namespace,
 but are not visible to processes in other UTS namespaces.
 
-Use of this flag requires: a kernel configured with the
-.B CONFIG_UTS_NS
-option and that the process be privileged
-.RB ( CAP_SYS_ADMIN ).
+Only a privileged process
+.RB ( CAP_SYS_ADMIN )
+can employ
+.BR CLONE_NEWUTS .
+
+For further information on UTS namespaces, see
+.BR namespaces (7).
 .TP
 .BR CLONE_PARENT " (since Linux 2.3.12)"
 If
@@ -514,7 +533,7 @@ Since 2.3.21 this flag can be
 specified only by the system boot process (PID 0).
 It disappeared in Linux 2.5.16.
 .TP
-.B CLONE_PTRACE
+.BR CLONE_PTRACE " (since Linux 2.2)"
 If
 .B CLONE_PTRACE
 is specified, and the calling process is being traced,
@@ -528,7 +547,7 @@ argument is the new TLS (Thread Local Storage) descriptor.
 (See
 .BR set_thread_area (2).)
 .TP
-.B CLONE_SIGHAND
+.BR CLONE_SIGHAND " (since Linux 2.0)"
 If
 .B CLONE_SIGHAND
 is set, the calling process and the child process share the same table of
@@ -584,10 +603,19 @@ altogether in Linux 2.6.38.
 If
 .B CLONE_SYSVSEM
 is set, then the child and the calling process share
-a single list of System V semaphore undo values (see
+a single list of System V semaphore adjustment
+.RI ( semadj )
+values (see
 .BR semop (2)).
-If this flag is not set, then the child has a separate undo list,
-which is initially empty.
+In this case, the shared list accumulates
+.I semadj
+values across all processes sharing the list,
+and semaphore adjustments are performed only when the last process
+that is sharing the list terminates (or ceases sharing the list using
+.BR unshare (2)).
+If this flag is not set, then the child has a separate
+.I semadj
+list that is initially empty.
 .TP
 .BR CLONE_THREAD " (since Linux 2.4.0-test8)"
 If
@@ -669,7 +697,12 @@ must also include
 .B CLONE_SIGHAND
 if
 .B CLONE_THREAD
-is specified.
+is specified
+(and note that, since Linux 2.6.0-test6,
+.BR CLONE_SIGHAND
+also requires
+.BR CLONE_VM
+to be included).
 
 Signals may be sent to a thread group as a whole (i.e., a TGID) using
 .BR kill (2),
@@ -713,7 +746,7 @@ is specified, then a tracing process cannot force
 .B CLONE_PTRACE
 on this child process.
 .TP
-.B CLONE_VFORK
+.BR CLONE_VFORK " (since Linux 2.2)"
 If
 .B CLONE_VFORK
 is set, the execution of the calling process is suspended
@@ -727,11 +760,11 @@ or
 
 If
 .B CLONE_VFORK
-is not set then both the calling process and the child are schedulable
+is not set, then both the calling process and the child are schedulable
 after the call, and an application should not rely on execution occurring
 in any particular order.
 .TP
-.B CLONE_VM
+.BR CLONE_VM " (since Linux 2.0)"
 If
 .B CLONE_VM
 is set, the calling process and the child process run in the same memory
@@ -752,27 +785,32 @@ space of the calling process at the time of
 Memory writes or file mappings/unmappings performed by one of the
 processes do not affect the other, as with
 .BR fork (2).
-.SS "sys_clone"
-The
-.B sys_clone
+.SS C library/kernel ABI differences
+The raw
+.BR clone ()
 system call corresponds more closely to
 .BR fork (2)
 in that execution in the child continues from the point of the
 call.
-Thus,
-.B sys_clone
-only requires the
-.I flags
+As such, the
+.I fn
 and
-.I child_stack
-arguments, which have the same meaning as for
-.BR clone ().
-(Note that the order of these arguments differs from
-.BR clone ().)
+.I arg
+arguments of the
+.BR clone ()
+wrapper function are omitted.
+Furthermore, the argument order changes.
+The raw system call interface on x86 and many other architectures is roughly:
+.in +4
+.nf
+
+.BI "long clone(unsigned long " flags ", void *" child_stack ,
+.BI "           void *" ptid ", void *" ctid ,
+.BI "           struct pt_regs *" regs );
 
-Another difference for
-.B sys_clone
-is that the
+.fi
+.in
+Another difference for the raw system call is that the
 .I child_stack
 argument may be zero, in which case copy-on-write semantics ensure that the
 child gets separate copies of stack pages when either process modifies
@@ -781,6 +819,50 @@ In this case, for correct operation, the
 .B CLONE_VM
 option should not be specified.
 
+For some architectures, the order of the arguments for the system call
+differs from that shown above.
+On the score, microblaze, ARM, ARM 64, PA-RISC, arc, Power PC, xtensa,
+and MIPS architectures,
+the order of the fourth and fifth arguments is reversed.
+On the cris and s390 architectures,
+the order of the first and second arguments is reversed.
+.SS blackfin, m68k, and sparc
+The argument-passing conventions on
+blackfin, m68k, and sparc are different from the descriptions above.
+For details, see the kernel (and glibc) source.
+.SS ia64
+On ia64, a different interface is used:
+.nf
+
+.BI "int __clone2(int (*" "fn" ")(void *), "
+.BI "             void *" child_stack_base ", size_t " stack_size ,
+.BI "             int " flags ", void *" "arg" ", ... "
+.BI "          /* pid_t *" ptid ", struct user_desc *" tls \
+", pid_t *" ctid " */ );"
+.fi
+.PP
+The prototype shown above is for the glibc wrapper function;
+the raw system call interface has no
+.I fn
+or
+.I arg
+argument, and changes the order of the arguments so that
+.I flags
+is the first argument, and
+.I tls
+is the last argument.
+.PP
+.BR __clone2 ()
+operates in the same way as
+.BR clone (),
+except that
+.I child_stack_base
+points to the lowest address of the child's stack area,
+and
+.I stack_size
+specifies the size of the stack pointed to by
+.IR child_stack_base .
+.SS Linux 2.4 and earlier
 In Linux 2.4 and earlier,
 .BR clone ()
 does not take arguments
@@ -788,7 +870,7 @@ does not take arguments
 .IR tls ,
 and
 .IR ctid .
-.SH "RETURN VALUE"
+.SH RETURN VALUE
 .\" gettid(2) returns current->pid;
 .\" getpid(2) returns current->tgid;
 On success, the thread ID of the child process is returned
@@ -800,7 +882,8 @@ will be set appropriately.
 .SH ERRORS
 .TP
 .B EAGAIN
-Too many processes are already running.
+Too many processes are already running; see
+.BR fork (2).
 .TP
 .B EINVAL
 .B CLONE_SIGHAND
@@ -825,6 +908,7 @@ was not.
 .\" (Since Linux 2.6.0-test6.)
 .TP
 .B EINVAL
+.\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
 Both
 .B CLONE_FS
 and
@@ -832,6 +916,14 @@ and
 were specified in
 .IR flags .
 .TP
+.BR EINVAL " (since Linux 3.9)"
+Both
+.B CLONE_NEWUSER
+and
+.B CLONE_FS
+were specified in
+.IR flags .
+.TP
 .B EINVAL
 Both
 .B CLONE_NEWIPC
@@ -841,10 +933,14 @@ were specified in
 .IR flags .
 .TP
 .B EINVAL
-Both
+One (or both) of
 .BR CLONE_NEWPID
-and
+or
+.BR CLONE_NEWUSER
+and one (or both) of
 .BR CLONE_THREAD
+or
+.BR CLONE_PARENT
 were specified in
 .IR flags .
 .TP
@@ -905,6 +1001,33 @@ was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP).
 .B EPERM
 .B CLONE_PID
 was specified by a process other than process 0.
+.TP
+.B EPERM
+.BR CLONE_NEWUSER
+was specified in
+.IR flags ,
+but either the effective user ID or the effective group ID of the caller
+does not have a mapping in the parent namespace (see
+.BR user_namespaces (7)).
+.TP
+.BR EPERM " (since Linux 3.9)"
+.\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d
+.B CLONE_NEWUSER
+was specified in
+.I flags
+and the caller is in a chroot environment
+.\" FIXME What is the rationale for this restriction?
+(i.e., the caller's root directory does not match the root directory
+of the mount namespace in which it resides).
+.TP
+.BR EUSERS " (since Linux 3.11)"
+.B CLONE_NEWUSER
+was specified in
+.IR flags ,
+and the call would cause the limit on the number of
+nested user namespaces to be exceeded.
+See
+.BR user_namespaces (7).
 .SH VERSIONS
 There is no entry for
 .BR clone ()
@@ -912,12 +1035,9 @@ in libc5.
 glibc2 provides
 .BR clone ()
 as described in this manual page.
-.SH "CONFORMING TO"
-The
+.SH CONFORMING TO
 .BR clone ()
-and
-.B sys_clone
-calls are Linux-specific and should not be used in programs
+is Linux-specific and should not be used in programs
 intended to be portable.
 .SH NOTES
 In the kernel 2.4.x series,
@@ -944,28 +1064,6 @@ On i386,
 .BR clone ()
 should not be called through vsyscall, but directly through
 .IR "int $0x80" .
-
-On ia64, a different system call is used:
-.nf
-
-.BI "int __clone2(int (*" "fn" ")(void *), "
-.BI "             void *" child_stack_base ", size_t " stack_size ,
-.BI "             int " flags ", void *" "arg" ", ... "
-.BI "          /* pid_t *" ptid ", struct user_desc *" tls \
-", pid_t *" ctid " */ );"
-.fi
-.PP
-The
-.BR __clone2 ()
-system call operates in the same way as
-.BR clone (),
-except that
-.I child_stack_base
-points to the lowest address of the child's stack area,
-and
-.I stack_size
-specifies the size of the stack pointed to by
-.IR child_stack_base .
 .SH BUGS
 Versions of the GNU C library that include the NPTL threading library
 contain a wrapper function for
@@ -1008,15 +1106,124 @@ To get the truth, it may be necessary to use code such as the following:
 .\" See also the following bug reports
 .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521
 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910
-.SH "SEE ALSO"
+.SH EXAMPLE
+The following program demonstrates the use of
+.BR clone ()
+to create a child process that executes in a separate UTS namespace.
+The child changes the hostname in its UTS namespace.
+Both parent and child then display the system hostname,
+making it possible to see that the hostname
+differs in the UTS namespaces of the parent and child.
+For an example of the use of this program, see
+.BR setns (2).
+.SS Program source
+.nf
+#define _GNU_SOURCE
+#include <sys/wait.h>
+#include <sys/utsname.h>
+#include <sched.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \\
+                        } while (0)
+
+static int              /* Start function for cloned child */
+childFunc(void *arg)
+{
+    struct utsname uts;
+
+    /* Change hostname in UTS namespace of child */
+
+    if (sethostname(arg, strlen(arg)) == \-1)
+        errExit("sethostname");
+
+    /* Retrieve and display hostname */
+
+    if (uname(&uts) == \-1)
+        errExit("uname");
+    printf("uts.nodename in child:  %s\\n", uts.nodename);
+
+    /* Keep the namespace open for a while, by sleeping.
+       This allows some experimentation\-\-for example, another
+       process might join the namespace. */
+
+    sleep(200);
+
+    return 0;           /* Child terminates now */
+}
+
+#define STACK_SIZE (1024 * 1024)    /* Stack size for cloned child */
+
+int
+main(int argc, char *argv[])
+{
+    char *stack;                    /* Start of stack buffer */
+    char *stackTop;                 /* End of stack buffer */
+    pid_t pid;
+    struct utsname uts;
+
+    if (argc < 2) {
+        fprintf(stderr, "Usage: %s <child\-hostname>\\n", argv[0]);
+        exit(EXIT_SUCCESS);
+    }
+
+    /* Allocate stack for child */
+
+    stack = malloc(STACK_SIZE);
+    if (stack == NULL)
+        errExit("malloc");
+    stackTop = stack + STACK_SIZE;  /* Assume stack grows downward */
+
+    /* Create child that has its own UTS namespace;
+       child commences execution in childFunc() */
+
+    pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]);
+    if (pid == \-1)
+        errExit("clone");
+    printf("clone() returned %ld\\n", (long) pid);
+
+    /* Parent falls through to here */
+
+    sleep(1);           /* Give child time to change its hostname */
+
+    /* Display hostname in parent\(aqs UTS namespace. This will be
+       different from hostname in child\(aqs UTS namespace. */
+
+    if (uname(&uts) == \-1)
+        errExit("uname");
+    printf("uts.nodename in parent: %s\\n", uts.nodename);
+
+    if (waitpid(pid, NULL, 0) == \-1)    /* Wait for child */
+        errExit("waitpid");
+    printf("child has terminated\\n");
+
+    exit(EXIT_SUCCESS);
+}
+.fi
+.SH SEE ALSO
 .BR fork (2),
 .BR futex (2),
 .BR getpid (2),
 .BR gettid (2),
+.BR kcmp (2),
 .BR set_thread_area (2),
 .BR set_tid_address (2),
+.BR setns (2),
 .BR tkill (2),
 .BR unshare (2),
 .BR wait (2),
 .BR capabilities (7),
+.BR namespaces (7),
 .BR pthreads (7)
+.SH COLOPHON
+This page is part of release 3.76 of the Linux
+.I man-pages
+project.
+A description of the project,
+information about reporting bugs,
+and the latest version of this page,
+can be found at
+\%http://www.kernel.org/doc/man\-pages/.