1.\" $OpenBSD: fork1.9,v 1.19 2012/06/13 06:15:23 guenther Exp $ 2.\" $NetBSD: fork1.9,v 1.3 1999/03/16 00:40:47 garbled Exp $ 3.\" 4.\" Copyright (c) 1998 The NetBSD Foundation, Inc. 5.\" All rights reserved. 6.\" 7.\" This code is derived from software contributed to The NetBSD Foundation 8.\" by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9.\" NASA Ames Research Center. 10.\" 11.\" Redistribution and use in source and binary forms, with or without 12.\" modification, are permitted provided that the following conditions 13.\" are met: 14.\" 1. Redistributions of source code must retain the above copyright 15.\" notice, this list of conditions and the following disclaimer. 16.\" 2. Redistributions in binary form must reproduce the above copyright 17.\" notice, this list of conditions and the following disclaimer in the 18.\" documentation and/or other materials provided with the distribution. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30.\" POSSIBILITY OF SUCH DAMAGE. 31.\" 32.Dd $Mdocdate: June 13 2012 $ 33.Dt FORK1 9 34.Os 35.Sh NAME 36.Nm fork1 37.Nd create a new process 38.Sh SYNOPSIS 39.Fd #include <sys/types.h> 40.Fd #include <sys/proc.h> 41.Ft int 42.Fo "fork1" 43.Fa "struct proc *p1" 44.Fa "int exitsig" 45.Fa "int flags" 46.Fa "void *stack" 47.Fa "pid_t *tidptr" 48.Fa "void (*func)(void *)" 49.Fa "void *arg" 50.Fa "register_t *retval" 51.Fa "struct proc **rnewprocp" 52.Fc 53.Sh DESCRIPTION 54.Fn fork1 55creates a new process out of 56.Ar p1 , 57which should be the current process. 58This function is used primarily to implement the 59.Xr fork 2 , 60.Xr __tfork 2 , 61.Xr vfork 2 62system calls, as well as the 63.Xr kthread_create 9 64function. 65.Pp 66The 67.Ar flags 68argument is used to control the behavior of the fork and is created by 69a bitwise-OR of the following values: 70.Bl -tag -width FORK_SHAREFILES 71.It Dv FORK_FORK 72The call is done by the 73.Xr fork 2 74system call. 75Used only for statistics. 76.It Dv FORK_VFORK 77The call is done by the 78.Xr vfork 2 79system call. 80Used only for statistics. 81.It Dv FORK_TFORK 82The call is done by the 83.Xr __tfork 2 84system call. 85Used only for statistics. 86.It Dv FORK_PPWAIT 87Suspend the parent process until the child is terminated (by calling 88.Xr _exit 2 89or abnormally), or makes a call to 90.Xr execve 2 . 91.It Dv FORK_SHAREFILES 92Let the child share the file descriptor table with the parent through 93fdshare(). 94The default behavior is to copy the table through 95fdcopy(). 96.It Dv FORK_CLEANFILES 97The child starts with a clean file descriptor table created by 98fdinit(). 99.It Dv FORK_NOZOMBIE 100The child will be dissociated from the parent and will not leave a status 101for the parent to collect. 102See 103.Xr wait 2 . 104.It Dv FORK_SHAREVM 105The child will share the parent's address space. 106The default behavior is 107that the child gets a copy-on-write copy of the address space. 108.It Dv FORK_SIGHAND 109The child will share the parent's signal actions, including the handler, 110mask, and flags, with sigactsshare(). 111The default behavior is to copy the signal actions from the parent with 112sigactsinit(). 113.Dv FORK_SHAREVM 114must also be set. 115.It Dv FORK_PTRACE 116The child will start with tracing enabled, as if 117ptrace(PT_TRACE_ME, 0, 0, 0) had been invoked in the child. 118.It Dv FORK_THREAD 119The child will instead be a kernel-level thread in the same process 120as the parent. 121.Dv FORK_NOZOMBIE , 122.Dv FORK_SHAREVM , 123and 124.Dv FORK_SIGHAND 125must also be set. 126.El 127.Pp 128If 129.Fa stack 130is not 131.Dv NULL , 132it will be used as the initial value of the child's stack pointer, 133instead of using the child's copy of the parent's stack. 134.Pp 135If 136.Fa tidptr 137is not 138.Dv NULL , 139the PID of the child process will be written there in the parent 140on success. 141This is guaranteed to be done before 142the child process is started. 143.Pp 144If 145.Fa retval 146is not 147.Dv NULL , 148it will hold the following values after successful completion 149of the fork operation: 150.Bl -tag -width retval[0] 151.It Fa retval[0] 152This will contain the PID of the child process. 153.It Fa retval[1] 154In the parent process, this will contain the value 0. 155In the child process, this will contain 1. 156.El 157.Pp 158The signal 159.Fa exitsig 160is sent to the parent 161.Fa p1 162on exit of the new process. 163.Pp 164If 165.Fa func 166is not 167.Dv NULL , 168the new process will begin execution by calling this function. 169It defaults to child_return, which returns to userland. 170.Pp 171If 172.Fa arg 173is not 174.Dv NULL , 175it is the argument to the previous function. 176It defaults to a pointer to the new process. 177.Pp 178The newly created process is returned through 179.Fa *rnewprocp . 180.Sh RETURN VALUES 181Upon successful completion of the fork operation, 182.Fn fork1 183returns 0. 184Otherwise, the following error values are returned: 185.Bl -tag -width [EAGAIN] 186.It Bq Er EAGAIN 187The limit on the total number of system processes would be exceeded. 188.It Bq Er EAGAIN 189The limit 190.Dv RLIMIT_NPROC 191on the total number of processes under execution by this 192user id would be exceeded. 193.El 194.Sh SEE ALSO 195.Xr __tfork 2 , 196.Xr execve 2 , 197.Xr fork 2 , 198.Xr vfork 2 , 199.Xr kthread_create 9 , 200.Xr pfind 9 , 201.Xr psignal 9 , 202.Xr uvm_fork 9 203.Sh CAVEATS 204The 205.Nm 206function semantics are specific to 207.Ox . 208Other BSD systems have different semantics. 209.Pp 210The only use of a non-null 211.Fa stack 212is for 213.Xr compat_linux 8 214on i386, so that feature is mostly untested. 215