xref: /freebsd/lib/libsys/pdfork.2 (revision 9c151e1fbf1a71a4912afa9693a39d55a00db4eb)
1.\"
2.\" Copyright (c) 2009-2010, 2012-2013 Robert N. M. Watson
3.\" All rights reserved.
4.\"
5.\" This software was developed at the University of Cambridge Computer
6.\" Laboratory with support from a grant from Google, Inc.
7.\"
8.\" This software was developed by SRI International and the University of
9.\" Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
10.\" ("CTSRD"), as part of the DARPA CRASH research programme.
11.\"
12.\" Redistribution and use in source and binary forms, with or without
13.\" modification, are permitted provided that the following conditions
14.\" are met:
15.\" 1. Redistributions of source code must retain the above copyright
16.\"    notice, this list of conditions and the following disclaimer.
17.\" 2. Redistributions in binary form must reproduce the above copyright
18.\"    notice, this list of conditions and the following disclaimer in the
19.\"    documentation and/or other materials provided with the distribution.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
32.\"
33.Dd January 20, 2026
34.Dt PDFORK 2
35.Os
36.Sh NAME
37.Nm pdfork ,
38.Nm pdrfork ,
39.Nm pdgetpid ,
40.Nm pdkill ,
41.Nm pdwait
42.Nd System calls to manage process descriptors
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In sys/procdesc.h
47.Ft pid_t
48.Fn pdfork "int *fdp" "int pdflags"
49.Ft pid_t
50.Fn pdrfork "int *fdp" "int pdflags" "int rfflags"
51.Ft int
52.Fn pdgetpid "int fd" "pid_t *pidp"
53.Ft int
54.Fn pdkill "int fd" "int signum"
55.Ft int
56.Fo pdwait
57.Fa "int fd"
58.Fa "int *status"
59.Fa "int options"
60.Fa "struct __wrusage *wrusage"
61.Fa "struct __siginfo *info"
62.Fc
63.Sh DESCRIPTION
64Process descriptors are special file descriptors that represent processes,
65and are created using
66.Fn pdfork ,
67a variant of
68.Xr fork 2 ,
69which, if successful, returns a process descriptor in the integer pointed to
70by
71.Fa fdp .
72Processes created via
73.Fn pdfork
74will not cause
75.Dv SIGCHLD
76on termination.
77.Fn pdfork
78can accept the
79.Fa pdflags:
80.Bl -tag -width PD_CLOEXEC
81.It Dv PD_DAEMON
82Instead of the default terminate-on-close behaviour, allow the process to
83live until it is explicitly killed with
84.Xr kill 2 .
85.Pp
86This option is not permitted in
87.Xr capsicum 4
88capability mode (see
89.Xr cap_enter 2 ) .
90.El
91.Bl -tag -width ".Dv PD_DAEMON"
92.It Dv PD_CLOEXEC
93Set close-on-exec on process descriptor.
94.El
95.Pp
96The
97.Fn pdrfork
98system call is a variant of
99.Fn pdfork
100that also takes the
101.Fa rfflags
102argument to control sharing of process resources between the caller
103and the new process.
104Like
105.Fn pdfork ,
106the function writes the process descriptor referencing the created
107process into the location pointed to by the
108.Fa fdp
109argument.
110See
111.Xr rfork 2
112for a description of the possible
113.Fa rfflag
114flags.
115The
116.Fn pdrfork
117system call requires that the
118.Va RFPROC
119or
120.Va RFSPAWN
121flag is specified.
122.Pp
123.Fn pdgetpid
124queries the process ID (PID) in the process descriptor
125.Fa fd .
126.Pp
127.Fn pdkill
128is functionally identical to
129.Xr kill 2 ,
130except that it accepts a process descriptor,
131.Fa fd ,
132rather than a PID.
133.Pp
134The
135.Fn pdwait
136system call allows the calling thread to wait and retrieve
137the status information on the process referenced by the
138.Fa fd
139process descriptor.
140See the description of the
141.Xr wait6
142system call for the behavior specification.
143.Pp
144The following system calls also have effects specific to process descriptors:
145.Pp
146.Xr fstat 2
147queries status of a process descriptor; currently only the
148.Fa st_mode ,
149.Fa st_birthtime ,
150.Fa st_atime ,
151.Fa st_ctime
152and
153.Fa st_mtime
154fields are defined.
155If the owner read, write, and execute bits are set then the
156process represented by the process descriptor is still alive.
157.Pp
158.Xr poll 2
159and
160.Xr select 2
161allow waiting for process state transitions; currently only
162.Dv POLLHUP
163is defined, and will be raised when the process dies.
164Process state transitions can also be monitored using
165.Xr kqueue 2
166filter
167.Dv EVFILT_PROCDESC ;
168currently only
169.Dv NOTE_EXIT
170is implemented.
171.Pp
172.Xr close 2
173will close the process descriptor unless
174.Dv PD_DAEMON
175is set; if the process is still alive and this is
176the last reference to the process descriptor, the process will be terminated
177with the signal
178.Dv SIGKILL .
179The PID of the referenced process is not reused until the process
180descriptor is closed,
181whether or not the zombie process is reaped by
182.Fn pdwait ,
183.Xr wait6 ,
184or similar system calls.
185.Sh RETURN VALUES
186.Fn pdfork
187and
188.Fn pdrfork
189return a PID, 0 or -1, as
190.Xr fork 2
191does.
192.Pp
193.Fn pdgetpid ,
194.Fn pdkill ,
195and
196.Fn pdwait
197return 0 on success and -1 on failure.
198.Sh ERRORS
199These functions may return the same error numbers as their PID-based equivalents
200(e.g.
201.Fn pdfork
202may return the same error numbers as
203.Xr fork 2 ) ,
204with the following additions:
205.Bl -tag -width Er
206.It Bq Er EINVAL
207The signal number given to
208.Fn pdkill
209is invalid.
210.It Bq Er ENOTCAPABLE
211The process descriptor being operated on has insufficient rights (e.g.
212.Dv CAP_PDKILL
213for
214.Fn pdkill ) .
215.El
216.Sh SEE ALSO
217.Xr close 2 ,
218.Xr fork 2 ,
219.Xr fstat 2 ,
220.Xr kill 2 ,
221.Xr kqueue 2 ,
222.Xr poll 2 ,
223.Xr wait4 2 ,
224.Xr capsicum 4 ,
225.Xr procdesc 4
226.Sh HISTORY
227The
228.Fn pdfork ,
229.Fn pdgetpid ,
230and
231.Fn pdkill
232system calls first appeared in
233.Fx 9.0 .
234The
235.Fn pdrfork
236and
237.Fn pdwait
238system calls first appeared in
239.Fx 16.0 .
240.Pp
241Support for process descriptors mode was developed as part of the
242.Tn TrustedBSD
243Project.
244.Sh AUTHORS
245.An -nosplit
246These functions and the capability facility were created by
247.An Robert N. M. Watson Aq Mt rwatson@FreeBSD.org
248and
249.An Jonathan Anderson Aq Mt jonathan@FreeBSD.org
250at the University of Cambridge Computer Laboratory with support from a grant
251from Google, Inc.
252The
253.Fn pdrfork
254and
255.Fn pdwait
256functions were developed by
257.An Konstantin Belousov Aq Mt kib@FreeBSD.org
258with input from
259.An Alan Somers Aq Mt asomers@FreeBSD.org .
260