/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2026 ConnectWise4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/types.h>28#include <sys/user.h>29#include <sys/proc.h>30#include <sys/procdesc.h>31#include <sys/sysctl.h>32#include <sys/wait.h>3334#include <atf-c.h>35#include <stdio.h>3637/* Tests for procdesc(4) that aren't specific to any one syscall */3839/*40* Even after waiting on a process descriptor with waitpid(2), the kernel will41* not recycle the pid until after the process descriptor is closed. That is42* important to prevent users from trying to wait() twice, the second time43* using a dangling pid.44*45* Whether this same anti-recycling behavior is used with pdwait() is46* unimportant, because pdwait _always_ uses a process descriptor.47*/48ATF_TC_WITHOUT_HEAD(pid_recycle);49ATF_TC_BODY(pid_recycle, tc)50{51size_t len;52int i, pd, pid_max;53pid_t dangle_pid;5455len = sizeof(pid_max);56ATF_REQUIRE_EQ_MSG(0,57sysctlbyname("kern.pid_max", &pid_max, &len, NULL, 0),58"sysctlbyname: %s", strerror(errno));5960/* Create a process descriptor */61dangle_pid = pdfork(&pd, PD_CLOEXEC | PD_DAEMON);62ATF_REQUIRE_MSG(dangle_pid >= 0, "pdfork: %s", strerror(errno));63if (dangle_pid == 0) {64// In child65_exit(0);66}67/*68* Reap the child, but don't close the pd, creating a dangling pid.69* Notably, it isn't a Zombie, because the process is reaped.70*/71ATF_REQUIRE_EQ(dangle_pid, waitpid(dangle_pid, NULL, WEXITED));7273/*74* Now create and kill pid_max additional children. Test to see if pid75* gets reused. If not, that means the kernel is correctly reserving76* the dangling pid from reuse.77*/78for (i = 0; i < pid_max; i++) {79pid_t pid;8081pid = vfork();82ATF_REQUIRE_MSG(pid >= 0, "vfork: %s", strerror(errno));83if (pid == 0)84_exit(0);85ATF_REQUIRE_MSG(pid != dangle_pid,86"pid got recycled after %d forks", i);87ATF_REQUIRE_EQ(pid, waitpid(pid, NULL, WEXITED));88}89close(pd);90}9192ATF_TP_ADD_TCS(tp)93{94ATF_TP_ADD_TC(tp, pid_recycle);9596return (atf_no_error());97}9899100