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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
.\"
.\" Copyright (c) 2009-2010, 2012-2013 Robert N. M. Watson
.\" All rights reserved.
.\"
.\" This software was developed at the University of Cambridge Computer
.\" Laboratory with support from a grant from Google, Inc.
.\"
.\" This software was developed by SRI International and the University of
.\" Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
.\" ("CTSRD"), as part of the DARPA CRASH research programme.
.\"
.\" 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 AUTHOR 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 AUTHOR 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 April 19, 2026
.Dt PDFORK 2
.Os
.Sh NAME
.Nm pdfork ,
.Nm pdrfork ,
.Nm pdgetpid ,
.Nm pdkill ,
.Nm pdwait
.Nd System calls to manage process descriptors
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In sys/procdesc.h
.Ft pid_t
.Fn pdfork "int *fdp" "int pdflags"
.Ft pid_t
.Fn pdrfork "int *fdp" "int pdflags" "int rfflags"
.Ft int
.Fn pdgetpid "int fd" "pid_t *pidp"
.Ft int
.Fn pdkill "int fd" "int signum"
.Ft int
.Fo pdwait
.Fa "int fd"
.Fa "int *status"
.Fa "int options"
.Fa "struct __wrusage *wrusage"
.Fa "struct __siginfo *info"
.Fc
.Sh DESCRIPTION
Process descriptors are special file descriptors that represent processes,
and are created using
.Fn pdfork ,
a variant of
.Xr fork 2 ,
which, if successful, returns a process descriptor in the integer pointed to
by
.Fa fdp .
Processes created via
.Fn pdfork
will not cause
.Dv SIGCHLD
on termination.
.Fn pdfork
can accept the
.Fa pdflags:
.Bl -tag -width PD_CLOEXEC
.It Dv PD_DAEMON
Instead of the default terminate-on-close behaviour, allow the process to
live until it is explicitly killed with
.Xr kill 2 .
.Pp
This option is not permitted in
.Xr capsicum 4
capability mode (see
.Xr cap_enter 2 ) .
.El
.Bl -tag -width ".Dv PD_DAEMON"
.It Dv PD_CLOEXEC
Set close-on-exec on process descriptor.
.El
.Pp
The
.Fn pdrfork
system call is a variant of
.Fn pdfork
that also takes the
.Fa rfflags
argument to control sharing of process resources between the caller
and the new process.
Like
.Fn pdfork ,
the function writes the process descriptor referencing the created
process into the location pointed to by the
.Fa fdp
argument.
See
.Xr rfork 2
for a description of the possible
.Fa rfflag
flags.
The
.Fn pdrfork
system call requires that both the
.Va RFPROC
and
.Va RFPROCDESC
flags, or
.Va RFSPAWN
flag are specified.
.Pp
.Fn pdgetpid
queries the process ID (PID) in the process descriptor
.Fa fd .
.Pp
.Fn pdkill
is functionally identical to
.Xr kill 2 ,
except that it accepts a process descriptor,
.Fa fd ,
rather than a PID.
.Pp
The
.Fn pdwait
system call allows the calling thread to wait and retrieve
the status information on the process referenced by the
.Fa fd
process descriptor.
See the description of the
.Xr wait6
system call for the behavior specification.
.Pp
The following system calls also have effects specific to process descriptors:
.Pp
.Xr fstat 2
queries status of a process descriptor; currently only the
.Fa st_mode ,
.Fa st_birthtime ,
.Fa st_atime ,
.Fa st_ctime
and
.Fa st_mtime
fields are defined.
If the owner read, write, and execute bits are set then the
process represented by the process descriptor is still alive.
.Pp
.Xr poll 2
and
.Xr select 2
allow waiting for process state transitions; currently only
.Dv POLLHUP
is defined, and will be raised when the process dies.
Process state transitions can also be monitored using
.Xr kqueue 2
filter
.Dv EVFILT_PROCDESC ;
currently only
.Dv NOTE_EXIT
is implemented.
.Pp
.Xr close 2
will close the process descriptor unless
.Dv PD_DAEMON
is set; if the process is still alive and this is
the last reference to the process descriptor, the process will be terminated
with the signal
.Dv SIGKILL .
The PID of the referenced process is not reused until the process
descriptor is closed,
whether or not the zombie process is reaped by
.Fn pdwait ,
.Xr wait6 ,
or similar system calls.
.Sh RETURN VALUES
.Fn pdfork
and
.Fn pdrfork
return a PID, 0 or -1, as
.Xr fork 2
does.
.Pp
.Fn pdgetpid ,
.Fn pdkill ,
and
.Fn pdwait
return 0 on success and -1 on failure.
.Sh ERRORS
These functions may return the same error numbers as their PID-based equivalents
(e.g.
.Fn pdfork
may return the same error numbers as
.Xr fork 2 ) ,
with the following additions:
.Bl -tag -width Er
.It Bq Er EFAULT
The copyout of the resulting file descriptor value to the memory pointed
to by
.Fa fdp
failed.
.Pp
Note that the child process was already created when this condition
is detected,
and the child continues execution, same as the parent.
If this error must be handled, it is advisable to memoize the
.Fn getpid
result before the call to
.Fn pdfork
or
.Fn pdrfork ,
and compare it to the value returned by
.Fn getpid
after, to see if code is executing in parent or child.
.It Bq Er EINVAL
The signal number given to
.Fn pdkill
is invalid.
.It Bq Er ENOTCAPABLE
The process descriptor being operated on has insufficient rights (e.g.
.Dv CAP_PDKILL
for
.Fn pdkill ) .
.El
.Sh SEE ALSO
.Xr close 2 ,
.Xr fork 2 ,
.Xr fstat 2 ,
.Xr kill 2 ,
.Xr kqueue 2 ,
.Xr poll 2 ,
.Xr wait4 2 ,
.Xr capsicum 4 ,
.Xr procdesc 4
.Sh HISTORY
The
.Fn pdfork ,
.Fn pdgetpid ,
and
.Fn pdkill
system calls first appeared in
.Fx 9.0 .
The
.Fn pdrfork
and
.Fn pdwait
system calls first appeared in
.Fx 15.1 .
.Pp
Support for process descriptors mode was developed as part of the
.Tn TrustedBSD
Project.
.Sh AUTHORS
.An -nosplit
These functions and the capability facility were created by
.An Robert N. M. Watson Aq Mt rwatson@FreeBSD.org
and
.An Jonathan Anderson Aq Mt jonathan@FreeBSD.org
at the University of Cambridge Computer Laboratory with support from a grant
from Google, Inc.
The
.Fn pdrfork
and
.Fn pdwait
functions were developed by
.An Konstantin Belousov Aq Mt kib@FreeBSD.org
with input from
.An Alan Somers Aq Mt asomers@FreeBSD.org .
|