1*8269e767SBrooks Davis.\" 2*8269e767SBrooks Davis.\" This manual page is taken directly from Plan9, and modified to 3*8269e767SBrooks Davis.\" describe the actual BSD implementation. Permission for 4*8269e767SBrooks Davis.\" use of this page comes from Rob Pike <rob@plan9.att.com>. 5*8269e767SBrooks Davis.\" 6*8269e767SBrooks Davis.Dd September 25, 2019 7*8269e767SBrooks Davis.Dt RFORK 2 8*8269e767SBrooks Davis.Os 9*8269e767SBrooks Davis.Sh NAME 10*8269e767SBrooks Davis.Nm rfork 11*8269e767SBrooks Davis.Nd manipulate process resources 12*8269e767SBrooks Davis.Sh LIBRARY 13*8269e767SBrooks Davis.Lb libc 14*8269e767SBrooks Davis.Sh SYNOPSIS 15*8269e767SBrooks Davis.In unistd.h 16*8269e767SBrooks Davis.Ft pid_t 17*8269e767SBrooks Davis.Fn rfork "int flags" 18*8269e767SBrooks Davis.Sh DESCRIPTION 19*8269e767SBrooks DavisForking, vforking or rforking are the only ways new processes are created. 20*8269e767SBrooks DavisThe 21*8269e767SBrooks Davis.Fa flags 22*8269e767SBrooks Davisargument to 23*8269e767SBrooks Davis.Fn rfork 24*8269e767SBrooks Davisselects which resources of the 25*8269e767SBrooks Davisinvoking process (parent) are shared 26*8269e767SBrooks Davisby the new process (child) or initialized to 27*8269e767SBrooks Davistheir default values. 28*8269e767SBrooks DavisThe resources include 29*8269e767SBrooks Davisthe open file descriptor table (which, when shared, permits processes 30*8269e767SBrooks Davisto open and close files for other processes), 31*8269e767SBrooks Davisand open files. 32*8269e767SBrooks DavisThe 33*8269e767SBrooks Davis.Fa flags 34*8269e767SBrooks Davisargument 35*8269e767SBrooks Davisis either 36*8269e767SBrooks Davis.Dv RFSPAWN 37*8269e767SBrooks Davisor the logical OR of some subset of: 38*8269e767SBrooks Davis.Bl -tag -width ".Dv RFLINUXTHPN" 39*8269e767SBrooks Davis.It Dv RFPROC 40*8269e767SBrooks DavisIf set a new process is created; otherwise changes affect the 41*8269e767SBrooks Daviscurrent process. 42*8269e767SBrooks Davis.It Dv RFNOWAIT 43*8269e767SBrooks DavisIf set, the child process will be dissociated from the parent. 44*8269e767SBrooks DavisUpon 45*8269e767SBrooks Davisexit the child will not leave a status for the parent to collect. 46*8269e767SBrooks DavisSee 47*8269e767SBrooks Davis.Xr wait 2 . 48*8269e767SBrooks Davis.It Dv RFFDG 49*8269e767SBrooks DavisIf set, the invoker's file descriptor table (see 50*8269e767SBrooks Davis.Xr intro 2 ) 51*8269e767SBrooks Davisis copied; otherwise the two processes share a 52*8269e767SBrooks Davissingle table. 53*8269e767SBrooks Davis.It Dv RFCFDG 54*8269e767SBrooks DavisIf set, the new process starts with a clean file descriptor table. 55*8269e767SBrooks DavisIs mutually exclusive with 56*8269e767SBrooks Davis.Dv RFFDG . 57*8269e767SBrooks Davis.It Dv RFTHREAD 58*8269e767SBrooks DavisIf set, the new process shares file descriptor to process leaders table 59*8269e767SBrooks Daviswith its parent. 60*8269e767SBrooks DavisOnly applies when neither 61*8269e767SBrooks Davis.Dv RFFDG 62*8269e767SBrooks Davisnor 63*8269e767SBrooks Davis.Dv RFCFDG 64*8269e767SBrooks Davisare set. 65*8269e767SBrooks Davis.It Dv RFMEM 66*8269e767SBrooks DavisIf set, the kernel will force sharing of the entire address space, 67*8269e767SBrooks Davistypically by sharing the hardware page table directly. 68*8269e767SBrooks DavisThe child 69*8269e767SBrooks Daviswill thus inherit and share all the segments the parent process owns, 70*8269e767SBrooks Daviswhether they are normally shareable or not. 71*8269e767SBrooks DavisThe stack segment is 72*8269e767SBrooks Davisnot split (both the parent and child return on the same stack) and thus 73*8269e767SBrooks Davis.Fn rfork 74*8269e767SBrooks Daviswith the RFMEM flag may not generally be called directly from high level 75*8269e767SBrooks Davislanguages including C. 76*8269e767SBrooks DavisMay be set only with 77*8269e767SBrooks Davis.Dv RFPROC . 78*8269e767SBrooks DavisA helper function is provided to assist with this problem and will cause 79*8269e767SBrooks Davisthe new process to run on the provided stack. 80*8269e767SBrooks DavisSee 81*8269e767SBrooks Davis.Xr rfork_thread 3 82*8269e767SBrooks Davisfor information. 83*8269e767SBrooks DavisNote that a lot of code will not run correctly in such an environment. 84*8269e767SBrooks Davis.It Dv RFSIGSHARE 85*8269e767SBrooks DavisIf set, the kernel will force sharing the sigacts structure between the 86*8269e767SBrooks Davischild and the parent. 87*8269e767SBrooks Davis.It Dv RFTSIGZMB 88*8269e767SBrooks DavisIf set, the kernel will deliver a specified signal to the parent 89*8269e767SBrooks Davisupon the child exit, instead of default SIGCHLD. 90*8269e767SBrooks DavisThe signal number 91*8269e767SBrooks Davis.Dv signum 92*8269e767SBrooks Davisis specified by oring the 93*8269e767SBrooks Davis.Dv RFTSIGFLAGS(signum) 94*8269e767SBrooks Davisexpression into 95*8269e767SBrooks Davis.Fa flags . 96*8269e767SBrooks DavisSpecifying signal number 0 disables signal delivery upon the child exit. 97*8269e767SBrooks Davis.It Dv RFLINUXTHPN 98*8269e767SBrooks DavisIf set, the kernel will deliver SIGUSR1 instead of SIGCHLD upon thread 99*8269e767SBrooks Davisexit for the child. 100*8269e767SBrooks DavisThis is intended to mimic certain Linux clone behaviour. 101*8269e767SBrooks Davis.El 102*8269e767SBrooks Davis.Pp 103*8269e767SBrooks DavisFile descriptors in a shared file descriptor table are kept 104*8269e767SBrooks Davisopen until either they are explicitly closed 105*8269e767SBrooks Davisor all processes sharing the table exit. 106*8269e767SBrooks Davis.Pp 107*8269e767SBrooks DavisIf 108*8269e767SBrooks Davis.Dv RFSPAWN 109*8269e767SBrooks Davisis passed, 110*8269e767SBrooks Davis.Nm 111*8269e767SBrooks Daviswill use 112*8269e767SBrooks Davis.Xr vfork 2 113*8269e767SBrooks Davissemantics but reset all signal actions in the child to default. 114*8269e767SBrooks DavisThis flag is used by the 115*8269e767SBrooks Davis.Xr posix_spawn 3 116*8269e767SBrooks Davisimplementation in libc. 117*8269e767SBrooks Davis.Pp 118*8269e767SBrooks DavisIf 119*8269e767SBrooks Davis.Dv RFPROC 120*8269e767SBrooks Davisis set, the 121*8269e767SBrooks Davisvalue returned in the parent process 122*8269e767SBrooks Davisis the process id 123*8269e767SBrooks Davisof the child process; the value returned in the child is zero. 124*8269e767SBrooks DavisWithout 125*8269e767SBrooks Davis.Dv RFPROC , 126*8269e767SBrooks Davisthe return value is zero. 127*8269e767SBrooks DavisProcess id's range from 1 to the maximum integer 128*8269e767SBrooks Davis.Ft ( int ) 129*8269e767SBrooks Davisvalue. 130*8269e767SBrooks DavisThe 131*8269e767SBrooks Davis.Fn rfork 132*8269e767SBrooks Davissystem call 133*8269e767SBrooks Daviswill sleep, if necessary, until required process resources are available. 134*8269e767SBrooks Davis.Pp 135*8269e767SBrooks DavisThe 136*8269e767SBrooks Davis.Fn fork 137*8269e767SBrooks Davissystem call 138*8269e767SBrooks Daviscan be implemented as a call to 139*8269e767SBrooks Davis.Fn rfork "RFFDG | RFPROC" 140*8269e767SBrooks Davisbut is not for backwards compatibility. 141*8269e767SBrooks Davis.Sh RETURN VALUES 142*8269e767SBrooks DavisUpon successful completion, 143*8269e767SBrooks Davis.Fn rfork 144*8269e767SBrooks Davisreturns a value 145*8269e767SBrooks Davisof 0 to the child process and returns the process ID of the child 146*8269e767SBrooks Davisprocess to the parent process. 147*8269e767SBrooks DavisOtherwise, a value of -1 is returned 148*8269e767SBrooks Davisto the parent process, no child process is created, and the global 149*8269e767SBrooks Davisvariable 150*8269e767SBrooks Davis.Va errno 151*8269e767SBrooks Davisis set to indicate the error. 152*8269e767SBrooks Davis.Sh ERRORS 153*8269e767SBrooks DavisThe 154*8269e767SBrooks Davis.Fn rfork 155*8269e767SBrooks Davissystem call 156*8269e767SBrooks Daviswill fail and no child process will be created if: 157*8269e767SBrooks Davis.Bl -tag -width Er 158*8269e767SBrooks Davis.It Bq Er EAGAIN 159*8269e767SBrooks DavisThe system-imposed limit on the total 160*8269e767SBrooks Davisnumber of processes under execution would be exceeded. 161*8269e767SBrooks DavisThe limit is given by the 162*8269e767SBrooks Davis.Xr sysctl 3 163*8269e767SBrooks DavisMIB variable 164*8269e767SBrooks Davis.Dv KERN_MAXPROC . 165*8269e767SBrooks Davis(The limit is actually ten less than this 166*8269e767SBrooks Davisexcept for the super user). 167*8269e767SBrooks Davis.It Bq Er EAGAIN 168*8269e767SBrooks DavisThe user is not the super user, and 169*8269e767SBrooks Davisthe system-imposed limit 170*8269e767SBrooks Davison the total number of 171*8269e767SBrooks Davisprocesses under execution by a single user would be exceeded. 172*8269e767SBrooks DavisThe limit is given by the 173*8269e767SBrooks Davis.Xr sysctl 3 174*8269e767SBrooks DavisMIB variable 175*8269e767SBrooks Davis.Dv KERN_MAXPROCPERUID . 176*8269e767SBrooks Davis.It Bq Er EAGAIN 177*8269e767SBrooks DavisThe user is not the super user, and 178*8269e767SBrooks Davisthe soft resource limit corresponding to the 179*8269e767SBrooks Davis.Fa resource 180*8269e767SBrooks Davisargument 181*8269e767SBrooks Davis.Dv RLIMIT_NOFILE 182*8269e767SBrooks Daviswould be exceeded (see 183*8269e767SBrooks Davis.Xr getrlimit 2 ) . 184*8269e767SBrooks Davis.It Bq Er EINVAL 185*8269e767SBrooks DavisBoth the RFFDG and the RFCFDG flags were specified. 186*8269e767SBrooks Davis.It Bq Er EINVAL 187*8269e767SBrooks DavisAny flags not listed above were specified. 188*8269e767SBrooks Davis.It Bq Er EINVAL 189*8269e767SBrooks DavisAn invalid signal number was specified. 190*8269e767SBrooks Davis.It Bq Er ENOMEM 191*8269e767SBrooks DavisThere is insufficient swap space for the new process. 192*8269e767SBrooks Davis.El 193*8269e767SBrooks Davis.Sh SEE ALSO 194*8269e767SBrooks Davis.Xr fork 2 , 195*8269e767SBrooks Davis.Xr intro 2 , 196*8269e767SBrooks Davis.Xr minherit 2 , 197*8269e767SBrooks Davis.Xr vfork 2 , 198*8269e767SBrooks Davis.Xr pthread_create 3 , 199*8269e767SBrooks Davis.Xr rfork_thread 3 200*8269e767SBrooks Davis.Sh HISTORY 201*8269e767SBrooks DavisThe 202*8269e767SBrooks Davis.Fn rfork 203*8269e767SBrooks Davisfunction first appeared in Plan9. 204