1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
.\" $OpenBSD: rfork.2,v 1.21 2004/04/03 10:07:08 jmc Exp $
.\"
.\" Copyright (c) 2003 Jason McIntyre <jmc@openbsd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd June 17, 2003
.Dt RFORK 2
.Os
.Sh NAME
.Nm rfork
.Nd control new processes
.Sh SYNOPSIS
.Fd #include <sys/param.h>
.Fd #include <unistd.h>
.Ft int
.Fn rfork "int flags"
.Sh DESCRIPTION
The fork functions
.Po
.Xr fork 2 ,
.Xr vfork 2 ,
and
.Fn rfork
.Pc
create new processes.
The new process
.Pq child process
is an exact copy of the calling process
.Pq parent process ,
except as outlined in the
.Xr fork 2
manual page.
.Fn rfork
can be used to manipulate the resources of the parent process and the
child process.
Operations currently supported include whether to copy or share the file
descriptor table between the two processes, whether to share the address
space, and whether the parent should
.Xr wait 2
for the child process to
.Xr _exit 2 .
.Fn rfork
takes a single argument,
.Fa flags ,
which controls which of these resources should be manipulated.
They are defined in the header file
.Aq Pa sys/param.h
and are the logical OR of one or more of the following:
.Bl -tag -width "RFNOWAIT"
.It Dv RFNAMEG
New Plan 9
.Sq name space .
This is a Plan 9 specific flag, and not implemented.
.It Dv RFENVG
Copy Plan 9
.Sq env space .
This is a Plan 9 specific flag, and not implemented.
.It Dv RFFDG
Copy the parent's file descriptor table.
If this flag is unset, the parent and child will share the parent's
file descriptor table.
May not be used in conjunction with
.Dv RFCFDG .
.It Dv RFNOTEG
Create new Plan 9
.Sq note group .
This is a Plan 9 specific flag, and not implemented.
.It Dv RFPROC
Create a new process.
The current implementation requires this flag to always be set.
.It Dv RFMEM
The kernel forces sharing of the entire address space.
The child will then inherit all the shared segments the parent process owns.
Subsequent forks by the parent will then propagate the shared
data and bss between children.
.It Dv RFNOWAIT
Parent need not
.Xr wait 2
on child.
.It Dv RFCNAMEG
Zero Plan 9
.Sq name space .
This is a Plan 9 specific flag, and not implemented.
.It Dv RFCENVG
Zero Plan 9
.Sq env space .
This is a Plan 9 specific flag, and not implemented.
.It Dv RFCFDG
Zero the child's file descriptor table
.Pq i.e. start with a blank file descriptor table .
May not be used in conjunction with
.Dv RFFDG .
.El
.Sh RETURN VALUES
The parent process returns the process ID
.Pq PID
of the child process.
The child process returns 0.
The range of the process ID is defined in
.Aq Pa sys/proc.h
and is currently between 1 and 32766, inclusive.
.Pp
.Fn fork
can be implemented as a call to
.Fn rfork
using "RFFDG|RFPROC", but isn't for backwards compatibility.
If a process has file descriptor table sharing active, setuid or setgid
programs will not
.Xr execve 2
with extra privileges.
.Sh ERRORS
.Fn rfork
will fail and no child process will be created if:
.Bl -tag -width Er
.It Bq Er ENOMEM
Cannot allocate memory.
The new process image required more memory than was allowed by the hardware or
by system-imposed memory management constraints.
A lack of swap space is normally temporary; however, a lack of core is not.
Soft limits may be increased to their corresponding hard limits.
.It Bq Er EINVAL
Invalid argument.
Some invalid argument was supplied.
.It Bq Er EAGAIN
Resource temporarily unavailable.
The system-imposed limit on the total
number of processes under execution would be exceeded.
This limit is configuration-dependent.
.It Bq Er EAGAIN
Resource temporarily unavailable.
The system-imposed limit
.Dv MAXUPRC
on the total number of processes under execution by a single user would be
exceeded.
.Dv MAXUPRC
is currently defined in
.Aq Pa sys/param.h
as
.Dv CHILD_MAX ,
which is currently defined as 80 in
.Aq Pa sys/limits.h .
.El
.Sh SEE ALSO
.Xr _exit 2 ,
.Xr execve 2 ,
.Xr fork 2 ,
.Xr intro 2 ,
.Xr vfork 2
.Sh HISTORY
The
.Fn rfork
function first appeared in Plan 9.
|