1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2026 ConnectWise
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/types.h>
29 #include <sys/user.h>
30 #include <sys/proc.h>
31 #include <sys/procdesc.h>
32 #include <sys/sysctl.h>
33 #include <sys/wait.h>
34
35 #include <atf-c.h>
36 #include <stdio.h>
37
38 /* Tests for procdesc(4) that aren't specific to any one syscall */
39
40 /*
41 * Even after waiting on a process descriptor with waitpid(2), the kernel will
42 * not recycle the pid until after the process descriptor is closed. That is
43 * important to prevent users from trying to wait() twice, the second time
44 * using a dangling pid.
45 *
46 * Whether this same anti-recycling behavior is used with pdwait() is
47 * unimportant, because pdwait _always_ uses a process descriptor.
48 */
49 ATF_TC_WITHOUT_HEAD(pid_recycle);
ATF_TC_BODY(pid_recycle,tc)50 ATF_TC_BODY(pid_recycle, tc)
51 {
52 size_t len;
53 int i, pd, pid_max;
54 pid_t dangle_pid;
55
56 len = sizeof(pid_max);
57 ATF_REQUIRE_EQ_MSG(0,
58 sysctlbyname("kern.pid_max", &pid_max, &len, NULL, 0),
59 "sysctlbyname: %s", strerror(errno));
60
61 /* Create a process descriptor */
62 dangle_pid = pdfork(&pd, PD_CLOEXEC | PD_DAEMON);
63 ATF_REQUIRE_MSG(dangle_pid >= 0, "pdfork: %s", strerror(errno));
64 if (dangle_pid == 0) {
65 // In child
66 _exit(0);
67 }
68 /*
69 * Reap the child, but don't close the pd, creating a dangling pid.
70 * Notably, it isn't a Zombie, because the process is reaped.
71 */
72 ATF_REQUIRE_EQ(dangle_pid, waitpid(dangle_pid, NULL, WEXITED));
73
74 /*
75 * Now create and kill pid_max additional children. Test to see if pid
76 * gets reused. If not, that means the kernel is correctly reserving
77 * the dangling pid from reuse.
78 */
79 for (i = 0; i < pid_max; i++) {
80 pid_t pid;
81
82 pid = vfork();
83 ATF_REQUIRE_MSG(pid >= 0, "vfork: %s", strerror(errno));
84 if (pid == 0)
85 _exit(0);
86 ATF_REQUIRE_MSG(pid != dangle_pid,
87 "pid got recycled after %d forks", i);
88 ATF_REQUIRE_EQ(pid, waitpid(pid, NULL, WEXITED));
89 }
90 close(pd);
91 }
92
ATF_TP_ADD_TCS(tp)93 ATF_TP_ADD_TCS(tp)
94 {
95 ATF_TP_ADD_TC(tp, pid_recycle);
96
97 return (atf_no_error());
98 }
99