CLONE(2) | Linux Programmer's Manual | CLONE(2) |
NAME
clone, __clone2 - create a child processSYNOPSIS
/* Prototype for the glibc wrapper function */
#include
<sched.h>
int clone(int (*fn)(void *), void *child_stack,
int flags, void *arg, ...
/* pid_t *ptid, struct user_desc *tls, pid_t *ctid */ );
/* Prototype for the raw system call */
long clone(unsigned long flags, void *child_stack,
void *ptid, void *ctid,
struct pt_regs *regs);
Feature Test Macro Requirements for glibc wrapper function (see feature_test_macros(7)):
clone():
- Since glibc 2.14:
- _GNU_SOURCE
- Before glibc 2.14:
-
_BSD_SOURCE || _SVID_SOURCE
/* _GNU_SOURCE also suffices */
DESCRIPTION
clone() creates a new process, in a manner similar to fork(2).- CLONE_CHILD_CLEARTID (since Linux 2.5.49)
- Erase child thread ID at location ctid in child memory when the child exits, and do a wakeup on the futex at that address. The address involved may be changed by the set_tid_address(2) system call. This is used by threading libraries.
- CLONE_CHILD_SETTID (since Linux 2.5.49)
- Store child thread ID at location ctid in child memory.
- CLONE_FILES (since Linux 2.0)
-
If
CLONE_FILES is set, the calling process and the child process share the same file descriptor table. Any file descriptor created by the calling process or by the child process is also valid in the other process. Similarly, if one of the processes closes a file descriptor, or changes its associated flags (using the
fcntl(2)
F_SETFD operation), the other process is also affected.
- CLONE_FS (since Linux 2.0)
-
If
CLONE_FS is set, the caller and the child process share the same file system information. This includes the root of the file system, the current working directory, and the umask. Any call to
chroot(2),
chdir(2), or
umask(2) performed by the calling process or the child process also affects the other process.
- CLONE_IO (since Linux 2.6.25)
-
If
CLONE_IO is set, then the new process shares an I/O context with the calling process. If this flag is not set, then (as with
fork(2)) the new process has its own I/O context.
- CLONE_NEWIPC (since Linux 2.6.19)
-
If
CLONE_NEWIPC is set, then create the process in a new IPC namespace. If this flag is not set, then (as with
fork(2)), the process is created in the same IPC namespace as the calling process. This flag is intended for the implementation of containers.
- CLONE_NEWNET (since Linux 2.6.24)
-
(The implementation of this flag was completed only by about kernel version 2.6.29.)
- CLONE_NEWNS (since Linux 2.4.19)
-
Start the child in a new mount namespace.
- CLONE_NEWPID (since Linux 2.6.24)
-
If
CLONE_NEWPID is set, then create the process in a new PID namespace. If this flag is not set, then (as with
fork(2)), the process is created in the same PID namespace as the calling process. This flag is intended for the implementation of containers.
- CLONE_NEWUTS (since Linux 2.6.19)
-
If
CLONE_NEWUTS 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
fork(2)), the process is created in the same UTS namespace as the calling process. This flag is intended for the implementation of containers.
- CLONE_PARENT (since Linux 2.3.12)
-
If
CLONE_PARENT is set, then the parent of the new child (as returned by
getppid(2)) will be the same as that of the calling process.
- CLONE_PARENT_SETTID (since Linux 2.5.49)
- Store child thread ID at location ptid in parent and child memory. (In Linux 2.5.32-2.5.48 there was a flag CLONE_SETTID that did this.)
- CLONE_PID (obsolete)
- If CLONE_PID is set, the child process is created with the same process ID as the calling process. This is good for hacking the system, but otherwise of not much use. Since 2.3.21 this flag can be specified only by the system boot process (PID 0). It disappeared in Linux 2.5.16.
- CLONE_PTRACE (since Linux 2.2)
- If CLONE_PTRACE is specified, and the calling process is being traced, then trace the child also (see ptrace(2)).
- CLONE_SETTLS (since Linux 2.5.32)
- The newtls argument is the new TLS (Thread Local Storage) descriptor. (See set_thread_area(2).)
- CLONE_SIGHAND (since Linux 2.0)
-
If
CLONE_SIGHAND is set, the calling process and the child process share the same table of signal handlers. If the calling process or child process calls
sigaction(2) to change the behavior associated with a signal, the behavior is changed in the other process as well. However, the calling process and child processes still have distinct signal masks and sets of pending signals. So, one of them may block or unblock some signals using
sigprocmask(2) without affecting the other process.
- CLONE_STOPPED (since Linux 2.6.0-test2)
-
If
CLONE_STOPPED is set, then the child is initially stopped (as though it was sent a
SIGSTOP signal), and must be resumed by sending it a
SIGCONT signal.
- CLONE_SYSVSEM (since Linux 2.5.10)
- If CLONE_SYSVSEM is set, then the child and the calling process share a single list of System V semaphore undo values (see semop(2)). If this flag is not set, then the child has a separate undo list, which is initially empty.
- CLONE_THREAD (since Linux 2.4.0-test8)
-
If
CLONE_THREAD is set, the child is placed in the same thread group as the calling process. To make the remainder of the discussion of
CLONE_THREAD more readable, the term "thread" is used to refer to the processes within a thread group.
- CLONE_UNTRACED (since Linux 2.5.46)
- If CLONE_UNTRACED is specified, then a tracing process cannot force CLONE_PTRACE on this child process.
- CLONE_VFORK (since Linux 2.2)
-
If
CLONE_VFORK is set, the execution of the calling process is suspended until the child releases its virtual memory resources via a call to
execve(2) or
_exit(2) (as with
vfork(2)).
- CLONE_VM (since Linux 2.0)
-
If
CLONE_VM is set, the calling process and the child process run in the same memory space. In particular, memory writes performed by the calling process or by the child process are also visible in the other process. Moreover, any memory mapping or unmapping performed with
mmap(2) or
munmap(2) by the child or calling process also affects the other process.
The raw system call interface
The raw clone() system call corresponds more closely to fork(2) in that execution in the child continues from the point of the call. As such, the fn and arg arguments of the clone() wrapper function are omitted. Furthermore, the argument order changes. The raw system call interface on x86 and many other architectures is roughly:
long clone(unsigned long flags, void *child_stack,
void *ptid, void *ctid,
struct pt_regs *regs);
Another difference for the raw system call is that the 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 the stack. In this case, for correct operation, the 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.
blackfin, m68k, and sparc
The argument-passing conventions on blackfin, m68k, and sparc are different from descriptions above. For details, see the kernel (and glibc) source.ia64
On ia64, a different interface is used:
int __clone2(int (*fn)(void *),
void *child_stack_base, size_t stack_size,
int flags, void *arg, ...
/* pid_t *ptid, struct user_desc *tls, pid_t *ctid */ );
The prototype shown above is for the glibc wrapper function; the raw system call interface has no fn or arg argument, and changes the order of the arguments so that flags is the first argument, and tls is the last argument.
__clone2() operates in the same way as clone(), except that child_stack_base points to the lowest address of the child's stack area, and stack_size specifies the size of the stack pointed to by child_stack_base.
Linux 2.4 and earlier
In Linux 2.4 and earlier, clone() does not take arguments ptid, tls, and ctid.RETURN VALUE
On success, the thread ID of the child process is returned in the caller's thread of execution. On failure, -1 is returned in the caller's context, no child process will be created, and errno will be set appropriately.ERRORS
- EAGAIN
- Too many processes are already running.
- EINVAL
- CLONE_SIGHAND was specified, but CLONE_VM was not. (Since Linux 2.6.0-test6.)
- EINVAL
- CLONE_THREAD was specified, but CLONE_SIGHAND was not. (Since Linux 2.5.35.)
- EINVAL
- Both CLONE_FS and CLONE_NEWNS were specified in flags.
- EINVAL
- Both CLONE_NEWIPC and CLONE_SYSVSEM were specified in flags.
- EINVAL
- Both CLONE_NEWPID and CLONE_THREAD were specified in flags.
- EINVAL
- Returned by clone() when a zero value is specified for child_stack.
- EINVAL
- CLONE_NEWIPC was specified in flags, but the kernel was not configured with the CONFIG_SYSVIPC and CONFIG_IPC_NS options.
- EINVAL
- CLONE_NEWNET was specified in flags, but the kernel was not configured with the CONFIG_NET_NS option.
- EINVAL
- CLONE_NEWPID was specified in flags, but the kernel was not configured with the CONFIG_PID_NS option.
- EINVAL
- CLONE_NEWUTS was specified in flags, but the kernel was not configured with the CONFIG_UTS option.
- ENOMEM
- Cannot allocate sufficient memory to allocate a task structure for the child, or to copy those parts of the caller's context that need to be copied.
- EPERM
- CLONE_NEWIPC, CLONE_NEWNET, CLONE_NEWNS, CLONE_NEWPID, or CLONE_NEWUTS was specified by an unprivileged process (process without CAP_SYS_ADMIN).
- EPERM
- CLONE_PID was specified by a process other than process 0.
VERSIONS
There is no entry for clone() in libc5. glibc2 provides clone() as described in this manual page.CONFORMING TO
clone() is Linux-specific and should not be used in programs intended to be portable.NOTES
In the kernel 2.4.x series, CLONE_THREAD generally does not make the parent of the new thread the same as the parent of the calling process. However, for kernel versions 2.4.7 to 2.4.18 the CLONE_THREAD flag implied the CLONE_PARENT flag (as in kernel 2.6).BUGS
Versions of the GNU C library that include the NPTL threading library contain a wrapper function for getpid(2) that performs caching of PIDs. This caching relies on support in the glibc wrapper for clone(), but as currently implemented, the cache may not be up to date in some circumstances. In particular, if a signal is delivered to the child immediately after the clone() call, then a call to getpid(2) in a handler for the signal may return the PID of the calling process ("the parent"), if the clone wrapper has not yet had a chance to update the PID cache in the child. (This discussion ignores the case where the child was created using CLONE_THREAD, when getpid(2) should return the same value in the child and in the process that called clone(), since the caller and the child are in the same thread group. The stale-cache problem also does not occur if the flags argument includes CLONE_VM.) To get the truth, it may be necessary to use code such as the following:
#include <syscall.h>
pid_t mypid;
mypid = syscall(SYS_getpid);
EXAMPLE
Create a child that executes in a separate UTS namespace
The following program demonstrates the use of 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 setns(2).#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's UTS namespace. This will be
different from hostname in child's 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);
}
SEE ALSO
fork(2), futex(2), getpid(2), gettid(2), kcmp(2), set_thread_area(2), set_tid_address(2), setns(2), tkill(2), unshare(2), wait(2), capabilities(7), pthreads(7)COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.2013-04-16 | Linux |