summaryrefslogtreecommitdiff
path: root/share/man/man9/fork1.9
blob: 3cd79b87def999643cf10085940cbc44e362c7e9 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
.\"	$OpenBSD: fork1.9,v 1.16 2011/11/16 10:05:45 jmc Exp $
.\"	$NetBSD: fork1.9,v 1.3 1999/03/16 00:40:47 garbled Exp $
.\"
.\" Copyright (c) 1998 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
.\" NASA Ames Research Center.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd $Mdocdate: November 16 2011 $
.Dt FORK1 9
.Os
.Sh NAME
.Nm fork1
.Nd create a new process
.Sh SYNOPSIS
.Fd #include <sys/types.h>
.Fd #include <sys/proc.h>
.Ft int
.Fo "fork1"
.Fa "struct proc *p1"
.Fa "int exitsig"
.Fa "int flags"
.Fa "void *stack"
.Fa "pid_t *tidptr"
.Fa "void (*func)(void *)"
.Fa "void *arg"
.Fa "register_t *retval"
.Fa "struct proc **rnewprocp"
.Fc
.Sh DESCRIPTION
.Fn fork1
creates a new process out of
.Ar p1 ,
which should be the current process.
This function is used primarily to implement the
.Xr fork 2 ,
.Xr rfork 2 ,
.Xr vfork 2
system calls, as well as the
.Xr kthread_create 9
function.
.Pp
The
.Ar flags
argument is used to control the behavior of the fork and is created by
a bitwise-OR of the following values:
.Bl -tag -width FORK_SHAREFILES
.It Dv FORK_FORK
The call is done by the
.Xr fork 2
system call.
Used only for statistics.
.It Dv FORK_VFORK
The call is done by the
.Xr vfork 2
system call.
Used only for statistics.
.It Dv FORK_RFORK
The call is done by the
.Xr rfork 2
system call.
Used only for statistics.
.It Dv FORK_TFORK
The call is done by the
.Fn __tfork
system call.
Used only for statistics.
.It Dv FORK_PPWAIT
Suspend the parent process until the child is terminated (by calling
.Xr _exit 2
or abnormally), or makes a call to
.Xr execve 2 .
.It Dv FORK_SHAREFILES
Let the child share the file descriptor table with the parent through
fdshare().
The default behavior is to copy the table through
fdcopy().
.It Dv FORK_CLEANFILES
The child starts with a clean file descriptor table created by
fdinit().
.It Dv FORK_NOZOMBIE
The child will be dissociated from the parent and will not leave a status
for the parent to collect.
See
.Xr wait 2 .
.It Dv FORK_SHAREVM
The child will share the parent's address space.
The default behavior is
that the child gets a copy-on-write copy of the address space.
.It Dv FORK_SIGHAND
The child will share the parent's signal actions, including the handler,
mask, and flags, with sigactsshare().
The default behavior is to copy the signal actions from the parent with
sigactsinit().
.Dv FORK_SHAREVM
must also be set.
.It Dv FORK_PTRACE
The child will start with tracing enabled, as if
ptrace(PT_TRACE_ME, 0, 0, 0) had been invoked in the child.
.It Dv FORK_THREAD
The child will instead be a kernel-level thread in the same process
as the parent.
.Dv FORK_NOZOMBIE ,
.Dv FORK_SHAREVM ,
and
.Dv FORK_SIGHAND
must also be set.
.El
.Pp
If
.Fa stack
is not
.Dv NULL ,
it will be used as the initial value of the child's stack pointer,
instead of using the child's copy of the parent's stack.
.Pp
If
.Fa tidptr
is not
.Dv NULL ,
the PID of the child process will be written there in the parent
on success.
This is guaranteed to be done before
the child process is started.
.Pp
If
.Fa retval
is not
.Dv NULL ,
it will hold the following values after successful completion
of the fork operation:
.Bl -tag -width retval[0]
.It Fa retval[0]
This will contain the PID of the child process.
.It Fa retval[1]
In the parent process, this will contain the value 0.
In the child process, this will contain 1.
.El
.Pp
The signal
.Fa exitsig
is sent to the parent
.Fa p1
on exit of the new process.
.Pp
If
.Fa func
is not
.Dv NULL ,
the new process will begin execution by calling this function.
It defaults to child_return, which returns to userland.
.Pp
If
.Fa arg
is not
.Dv NULL ,
it is the argument to the previous function.
It defaults to a pointer to the new process.
.Pp
The newly created process is returned through
.Fa *rnewprocp .
.Sh RETURN VALUES
Upon successful completion of the fork operation,
.Fn fork1
returns 0.
Otherwise, the following error values are returned:
.Bl -tag -width [EAGAIN]
.It Bq Er EAGAIN
The limit on the total number of system processes would be exceeded.
.It Bq Er EAGAIN
The limit
.Dv RLIMIT_NPROC
on the total number of processes under execution by this
user id would be exceeded.
.El
.Sh SEE ALSO
.Xr execve 2 ,
.Xr fork 2 ,
.Xr rfork 2 ,
.Xr vfork 2 ,
.Xr kthread_create 9 ,
.Xr pfind 9 ,
.Xr psignal 9 ,
.Xr uvm_fork 9
.Sh CAVEATS
The
.Nm
function semantics are specific to
.Ox .
Other BSD systems have different semantics.
.Pp
The only use of a non-null
.Fa stack
is for
.Xr compat_linux 8
on i386, so that feature is mostly untested.