Index: head/sys/kern/sys_procdesc.c =================================================================== --- head/sys/kern/sys_procdesc.c (revision 339389) +++ head/sys/kern/sys_procdesc.c (revision 339390) @@ -1,570 +1,571 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2009, 2016 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. * * Portions of this software were developed by BAE Systems, the University of * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent * Computing (TC) research program. * * 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. */ /*- * FreeBSD process descriptor facility. * * Some processes are represented by a file descriptor, which will be used in * preference to signaling and pids for the purposes of process management, * and is, in effect, a form of capability. When a process descriptor is * used with a process, it ceases to be visible to certain traditional UNIX * process facilities, such as waitpid(2). * * Some semantics: * * - At most one process descriptor will exist for any process, although * references to that descriptor may be held from many processes (or even * be in flight between processes over a local domain socket). * - Last close on the process descriptor will terminate the process using * SIGKILL and reparent it to init so that there's a process to reap it * when it's done exiting. * - If the process exits before the descriptor is closed, it will not * generate SIGCHLD on termination, or be picked up by waitpid(). * - The pdkill(2) system call may be used to deliver a signal to the process * using its process descriptor. * - The pdwait4(2) system call may be used to block (or not) on a process * descriptor to collect termination information. * * Open questions: * * - How to handle ptrace(2)? * - Will we want to add a pidtoprocdesc(2) system call to allow process * descriptors to be created for processes without pdfork(2)? */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include FEATURE(process_descriptors, "Process Descriptors"); static uma_zone_t procdesc_zone; static fo_poll_t procdesc_poll; static fo_kqfilter_t procdesc_kqfilter; static fo_stat_t procdesc_stat; static fo_close_t procdesc_close; static fo_fill_kinfo_t procdesc_fill_kinfo; static struct fileops procdesc_ops = { .fo_read = invfo_rdwr, .fo_write = invfo_rdwr, .fo_truncate = invfo_truncate, .fo_ioctl = invfo_ioctl, .fo_poll = procdesc_poll, .fo_kqfilter = procdesc_kqfilter, .fo_stat = procdesc_stat, .fo_close = procdesc_close, .fo_chmod = invfo_chmod, .fo_chown = invfo_chown, .fo_sendfile = invfo_sendfile, .fo_fill_kinfo = procdesc_fill_kinfo, .fo_flags = DFLAG_PASSABLE, }; /* * Initialize with VFS so that process descriptors are available along with * other file descriptor types. As long as it runs before init(8) starts, * there shouldn't be a problem. */ static void procdesc_init(void *dummy __unused) { procdesc_zone = uma_zcreate("procdesc", sizeof(struct procdesc), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); if (procdesc_zone == NULL) panic("procdesc_init: procdesc_zone not initialized"); } SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_ANY, procdesc_init, NULL); /* * Return a locked process given a process descriptor, or ESRCH if it has * died. */ int procdesc_find(struct thread *td, int fd, cap_rights_t *rightsp, struct proc **p) { struct procdesc *pd; struct file *fp; int error; error = fget(td, fd, rightsp, &fp); if (error) return (error); if (fp->f_type != DTYPE_PROCDESC) { error = EBADF; goto out; } pd = fp->f_data; sx_slock(&proctree_lock); if (pd->pd_proc != NULL) { *p = pd->pd_proc; PROC_LOCK(*p); } else error = ESRCH; sx_sunlock(&proctree_lock); out: fdrop(fp, td); return (error); } /* * Function to be used by procstat(1) sysctls when returning procdesc * information. */ pid_t procdesc_pid(struct file *fp_procdesc) { struct procdesc *pd; KASSERT(fp_procdesc->f_type == DTYPE_PROCDESC, ("procdesc_pid: !procdesc")); pd = fp_procdesc->f_data; return (pd->pd_pid); } /* * Retrieve the PID associated with a process descriptor. */ int kern_pdgetpid(struct thread *td, int fd, cap_rights_t *rightsp, pid_t *pidp) { struct file *fp; int error; error = fget(td, fd, rightsp, &fp); if (error) return (error); if (fp->f_type != DTYPE_PROCDESC) { error = EBADF; goto out; } *pidp = procdesc_pid(fp); out: fdrop(fp, td); return (error); } /* * System call to return the pid of a process given its process descriptor. */ int sys_pdgetpid(struct thread *td, struct pdgetpid_args *uap) { pid_t pid; int error; AUDIT_ARG_FD(uap->fd); error = kern_pdgetpid(td, uap->fd, &cap_pdgetpid_rights, &pid); if (error == 0) error = copyout(&pid, uap->pidp, sizeof(pid)); return (error); } /* * When a new process is forked by pdfork(), a file descriptor is allocated * by the fork code first, then the process is forked, and then we get a * chance to set up the process descriptor. Failure is not permitted at this * point, so procdesc_new() must succeed. */ void procdesc_new(struct proc *p, int flags) { struct procdesc *pd; pd = uma_zalloc(procdesc_zone, M_WAITOK | M_ZERO); pd->pd_proc = p; pd->pd_pid = p->p_pid; p->p_procdesc = pd; pd->pd_flags = 0; if (flags & PD_DAEMON) pd->pd_flags |= PDF_DAEMON; PROCDESC_LOCK_INIT(pd); knlist_init_mtx(&pd->pd_selinfo.si_note, &pd->pd_lock); /* * Process descriptors start out with two references: one from their * struct file, and the other from their struct proc. */ refcount_init(&pd->pd_refcount, 2); } /* * Create a new process decriptor for the process that refers to it. */ int procdesc_falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags, struct filecaps *fcaps) { int fflags; fflags = 0; if (flags & PD_CLOEXEC) fflags = O_CLOEXEC; return (falloc_caps(td, resultfp, resultfd, fflags, fcaps)); } /* * Initialize a file with a process descriptor. */ void procdesc_finit(struct procdesc *pdp, struct file *fp) { finit(fp, FREAD | FWRITE, DTYPE_PROCDESC, pdp, &procdesc_ops); } static void procdesc_free(struct procdesc *pd) { /* * When the last reference is released, we assert that the descriptor * has been closed, but not that the process has exited, as we will * detach the descriptor before the process dies if the descript is * closed, as we can't wait synchronously. */ if (refcount_release(&pd->pd_refcount)) { KASSERT(pd->pd_proc == NULL, ("procdesc_free: pd_proc != NULL")); KASSERT((pd->pd_flags & PDF_CLOSED), ("procdesc_free: !PDF_CLOSED")); knlist_destroy(&pd->pd_selinfo.si_note); PROCDESC_LOCK_DESTROY(pd); uma_zfree(procdesc_zone, pd); } } /* * procdesc_exit() - notify a process descriptor that its process is exiting. * We use the proctree_lock to ensure that process exit either happens * strictly before or strictly after a concurrent call to procdesc_close(). */ int procdesc_exit(struct proc *p) { struct procdesc *pd; sx_assert(&proctree_lock, SA_XLOCKED); PROC_LOCK_ASSERT(p, MA_OWNED); KASSERT(p->p_procdesc != NULL, ("procdesc_exit: p_procdesc NULL")); pd = p->p_procdesc; PROCDESC_LOCK(pd); - KASSERT((pd->pd_flags & PDF_CLOSED) == 0 || p->p_pptr == initproc, - ("procdesc_exit: closed && parent not init")); + KASSERT((pd->pd_flags & PDF_CLOSED) == 0 || p->p_pptr == p->p_reaper, + ("procdesc_exit: closed && parent not reaper")); pd->pd_flags |= PDF_EXITED; pd->pd_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig); /* * If the process descriptor has been closed, then we have nothing * to do; return 1 so that init will get SIGCHLD and do the reaping. * Clean up the procdesc now rather than letting it happen during * that reap. */ if (pd->pd_flags & PDF_CLOSED) { PROCDESC_UNLOCK(pd); pd->pd_proc = NULL; p->p_procdesc = NULL; procdesc_free(pd); return (1); } if (pd->pd_flags & PDF_SELECTED) { pd->pd_flags &= ~PDF_SELECTED; selwakeup(&pd->pd_selinfo); } KNOTE_LOCKED(&pd->pd_selinfo.si_note, NOTE_EXIT); PROCDESC_UNLOCK(pd); return (0); } /* * When a process descriptor is reaped, perhaps as a result of close() or * pdwait4(), release the process's reference on the process descriptor. */ void procdesc_reap(struct proc *p) { struct procdesc *pd; sx_assert(&proctree_lock, SA_XLOCKED); KASSERT(p->p_procdesc != NULL, ("procdesc_reap: p_procdesc == NULL")); pd = p->p_procdesc; pd->pd_proc = NULL; p->p_procdesc = NULL; procdesc_free(pd); } /* * procdesc_close() - last close on a process descriptor. If the process is * still running, terminate with SIGKILL (unless PDF_DAEMON is set) and let - * init(8) clean up the mess; if not, we have to clean up the zombie ourselves. + * its reaper clean up the mess; if not, we have to clean up the zombie + * ourselves. */ static int procdesc_close(struct file *fp, struct thread *td) { struct procdesc *pd; struct proc *p; KASSERT(fp->f_type == DTYPE_PROCDESC, ("procdesc_close: !procdesc")); pd = fp->f_data; fp->f_ops = &badfileops; fp->f_data = NULL; sx_xlock(&proctree_lock); PROCDESC_LOCK(pd); pd->pd_flags |= PDF_CLOSED; PROCDESC_UNLOCK(pd); p = pd->pd_proc; if (p == NULL) { /* * This is the case where process' exit status was already * collected and procdesc_reap() was already called. */ sx_xunlock(&proctree_lock); } else { PROC_LOCK(p); AUDIT_ARG_PROCESS(p); if (p->p_state == PRS_ZOMBIE) { /* * If the process is already dead and just awaiting * reaping, do that now. This will release the * process's reference to the process descriptor when it * calls back into procdesc_reap(). */ proc_reap(curthread, p, NULL, 0); } else { /* * If the process is not yet dead, we need to kill it, * but we can't wait around synchronously for it to go * away, as that path leads to madness (and deadlocks). * First, detach the process from its descriptor so that * its exit status will be reported normally. */ pd->pd_proc = NULL; p->p_procdesc = NULL; procdesc_free(pd); /* - * Next, reparent it to init(8) so that there's someone - * to pick up the pieces; finally, terminate with - * prejudice. + * Next, reparent it to its reaper (usually init(8)) so + * that there's someone to pick up the pieces; finally, + * terminate with prejudice. */ p->p_sigparent = SIGCHLD; - proc_reparent(p, initproc); + proc_reparent(p, p->p_reaper); if ((pd->pd_flags & PDF_DAEMON) == 0) kern_psignal(p, SIGKILL); PROC_UNLOCK(p); sx_xunlock(&proctree_lock); } } /* * Release the file descriptor's reference on the process descriptor. */ procdesc_free(pd); return (0); } static int procdesc_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td) { struct procdesc *pd; int revents; revents = 0; pd = fp->f_data; PROCDESC_LOCK(pd); if (pd->pd_flags & PDF_EXITED) revents |= POLLHUP; if (revents == 0) { selrecord(td, &pd->pd_selinfo); pd->pd_flags |= PDF_SELECTED; } PROCDESC_UNLOCK(pd); return (revents); } static void procdesc_kqops_detach(struct knote *kn) { struct procdesc *pd; pd = kn->kn_fp->f_data; knlist_remove(&pd->pd_selinfo.si_note, kn, 0); } static int procdesc_kqops_event(struct knote *kn, long hint) { struct procdesc *pd; u_int event; pd = kn->kn_fp->f_data; if (hint == 0) { /* * Initial test after registration. Generate a NOTE_EXIT in * case the process already terminated before registration. */ event = pd->pd_flags & PDF_EXITED ? NOTE_EXIT : 0; } else { /* Mask off extra data. */ event = (u_int)hint & NOTE_PCTRLMASK; } /* If the user is interested in this event, record it. */ if (kn->kn_sfflags & event) kn->kn_fflags |= event; /* Process is gone, so flag the event as finished. */ if (event == NOTE_EXIT) { kn->kn_flags |= EV_EOF | EV_ONESHOT; if (kn->kn_fflags & NOTE_EXIT) kn->kn_data = pd->pd_xstat; if (kn->kn_fflags == 0) kn->kn_flags |= EV_DROP; return (1); } return (kn->kn_fflags != 0); } static struct filterops procdesc_kqops = { .f_isfd = 1, .f_detach = procdesc_kqops_detach, .f_event = procdesc_kqops_event, }; static int procdesc_kqfilter(struct file *fp, struct knote *kn) { struct procdesc *pd; pd = fp->f_data; switch (kn->kn_filter) { case EVFILT_PROCDESC: kn->kn_fop = &procdesc_kqops; kn->kn_flags |= EV_CLEAR; knlist_add(&pd->pd_selinfo.si_note, kn, 0); return (0); default: return (EINVAL); } } static int procdesc_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, struct thread *td) { struct procdesc *pd; struct timeval pstart, boottime; /* * XXXRW: Perhaps we should cache some more information from the * process so that we can return it reliably here even after it has * died. For example, caching its credential data. */ bzero(sb, sizeof(*sb)); pd = fp->f_data; sx_slock(&proctree_lock); if (pd->pd_proc != NULL) { PROC_LOCK(pd->pd_proc); AUDIT_ARG_PROCESS(pd->pd_proc); /* Set birth and [acm] times to process start time. */ pstart = pd->pd_proc->p_stats->p_start; getboottime(&boottime); timevaladd(&pstart, &boottime); TIMEVAL_TO_TIMESPEC(&pstart, &sb->st_birthtim); sb->st_atim = sb->st_birthtim; sb->st_ctim = sb->st_birthtim; sb->st_mtim = sb->st_birthtim; if (pd->pd_proc->p_state != PRS_ZOMBIE) sb->st_mode = S_IFREG | S_IRWXU; else sb->st_mode = S_IFREG; sb->st_uid = pd->pd_proc->p_ucred->cr_ruid; sb->st_gid = pd->pd_proc->p_ucred->cr_rgid; PROC_UNLOCK(pd->pd_proc); } else sb->st_mode = S_IFREG; sx_sunlock(&proctree_lock); return (0); } static int procdesc_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp) { struct procdesc *pdp; kif->kf_type = KF_TYPE_PROCDESC; pdp = fp->f_data; kif->kf_un.kf_proc.kf_pid = pdp->pd_pid; return (0); } Index: head/tests/sys/kern/reaper.c =================================================================== --- head/tests/sys/kern/reaper.c (revision 339389) +++ head/tests/sys/kern/reaper.c (revision 339390) @@ -1,758 +1,794 @@ /*- * Copyright (c) 2016 Jilles Tjoelker * All rights reserved. * * 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. */ #include __FBSDID("$FreeBSD$"); #include +#include #include #include #include #include #include static void dummy_sighandler(int sig __unused, siginfo_t *info __unused, void *ctx __unused) { } ATF_TC_WITHOUT_HEAD(reaper_wait_child_first); ATF_TC_BODY(reaper_wait_child_first, tc) { pid_t parent, child, grandchild, pid; int status, r; int pip[2]; /* Be paranoid. */ pid = waitpid(-1, NULL, WNOHANG); ATF_REQUIRE(pid == -1 && errno == ECHILD); parent = getpid(); r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL); ATF_REQUIRE_EQ(0, r); r = pipe(pip); ATF_REQUIRE_EQ(0, r); child = fork(); ATF_REQUIRE(child != -1); if (child == 0) { if (close(pip[1]) != 0) _exit(100); grandchild = fork(); if (grandchild == -1) _exit(101); else if (grandchild == 0) { if (read(pip[0], &(uint8_t){ 0 }, 1) != 0) _exit(102); if (getppid() != parent) _exit(103); _exit(2); } else _exit(3); } pid = waitpid(child, &status, 0); ATF_REQUIRE_EQ(child, pid); r = WIFEXITED(status) ? WEXITSTATUS(status) : -1; ATF_CHECK_EQ(3, r); r = close(pip[1]); ATF_REQUIRE_EQ(0, r); pid = waitpid(-1, &status, 0); ATF_REQUIRE(pid > 0 && pid != child); r = WIFEXITED(status) ? WEXITSTATUS(status) : -1; ATF_CHECK_EQ(2, r); r = close(pip[0]); ATF_REQUIRE_EQ(0, r); } ATF_TC_WITHOUT_HEAD(reaper_wait_grandchild_first); ATF_TC_BODY(reaper_wait_grandchild_first, tc) { pid_t parent, child, grandchild, pid; int status, r; /* Be paranoid. */ pid = waitpid(-1, NULL, WNOHANG); ATF_REQUIRE(pid == -1 && errno == ECHILD); parent = getpid(); r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL); ATF_REQUIRE_EQ(0, r); child = fork(); ATF_REQUIRE(child != -1); if (child == 0) { grandchild = fork(); if (grandchild == -1) _exit(101); else if (grandchild == 0) _exit(2); else { if (waitid(P_PID, grandchild, NULL, WNOWAIT | WEXITED) != 0) _exit(102); _exit(3); } } pid = waitpid(child, &status, 0); ATF_REQUIRE_EQ(child, pid); r = WIFEXITED(status) ? WEXITSTATUS(status) : -1; ATF_CHECK_EQ(3, r); pid = waitpid(-1, &status, 0); ATF_REQUIRE(pid > 0 && pid != child); r = WIFEXITED(status) ? WEXITSTATUS(status) : -1; ATF_CHECK_EQ(2, r); } ATF_TC(reaper_sigchld_child_first); ATF_TC_HEAD(reaper_sigchld_child_first, tc) { atf_tc_set_md_var(tc, "timeout", "2"); } ATF_TC_BODY(reaper_sigchld_child_first, tc) { struct sigaction act; sigset_t mask; siginfo_t info; pid_t parent, child, grandchild, pid; int r; int pip[2]; /* Be paranoid. */ pid = waitpid(-1, NULL, WNOHANG); ATF_REQUIRE(pid == -1 && errno == ECHILD); act.sa_sigaction = dummy_sighandler; act.sa_flags = SA_SIGINFO | SA_RESTART; r = sigemptyset(&act.sa_mask); ATF_REQUIRE_EQ(0, r); r = sigaction(SIGCHLD, &act, NULL); ATF_REQUIRE_EQ(0, r); r = sigemptyset(&mask); ATF_REQUIRE_EQ(0, r); r = sigaddset(&mask, SIGCHLD); ATF_REQUIRE_EQ(0, r); r = sigprocmask(SIG_BLOCK, &mask, NULL); ATF_REQUIRE_EQ(0, r); parent = getpid(); r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL); ATF_REQUIRE_EQ(0, r); r = pipe(pip); ATF_REQUIRE_EQ(0, r); child = fork(); ATF_REQUIRE(child != -1); if (child == 0) { if (close(pip[1]) != 0) _exit(100); grandchild = fork(); if (grandchild == -1) _exit(101); else if (grandchild == 0) { if (read(pip[0], &(uint8_t){ 0 }, 1) != 0) _exit(102); if (getppid() != parent) _exit(103); _exit(2); } else _exit(3); } r = sigwaitinfo(&mask, &info); ATF_REQUIRE_EQ(SIGCHLD, r); ATF_CHECK_EQ(SIGCHLD, info.si_signo); ATF_CHECK_EQ(CLD_EXITED, info.si_code); ATF_CHECK_EQ(3, info.si_status); ATF_CHECK_EQ(child, info.si_pid); pid = waitpid(child, NULL, 0); ATF_REQUIRE_EQ(child, pid); r = close(pip[1]); ATF_REQUIRE_EQ(0, r); r = sigwaitinfo(&mask, &info); ATF_REQUIRE_EQ(SIGCHLD, r); ATF_CHECK_EQ(SIGCHLD, info.si_signo); ATF_CHECK_EQ(CLD_EXITED, info.si_code); ATF_CHECK_EQ(2, info.si_status); grandchild = info.si_pid; ATF_REQUIRE(grandchild > 0); ATF_REQUIRE(grandchild != parent); ATF_REQUIRE(grandchild != child); pid = waitpid(-1, NULL, 0); ATF_REQUIRE_EQ(grandchild, pid); r = close(pip[0]); ATF_REQUIRE_EQ(0, r); } ATF_TC(reaper_sigchld_grandchild_first); ATF_TC_HEAD(reaper_sigchld_grandchild_first, tc) { atf_tc_set_md_var(tc, "timeout", "2"); } ATF_TC_BODY(reaper_sigchld_grandchild_first, tc) { struct sigaction act; sigset_t mask; siginfo_t info; pid_t parent, child, grandchild, pid; int r; /* Be paranoid. */ pid = waitpid(-1, NULL, WNOHANG); ATF_REQUIRE(pid == -1 && errno == ECHILD); act.sa_sigaction = dummy_sighandler; act.sa_flags = SA_SIGINFO | SA_RESTART; r = sigemptyset(&act.sa_mask); ATF_REQUIRE_EQ(0, r); r = sigaction(SIGCHLD, &act, NULL); ATF_REQUIRE_EQ(0, r); r = sigemptyset(&mask); ATF_REQUIRE_EQ(0, r); r = sigaddset(&mask, SIGCHLD); ATF_REQUIRE_EQ(0, r); r = sigprocmask(SIG_BLOCK, &mask, NULL); ATF_REQUIRE_EQ(0, r); parent = getpid(); r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL); ATF_REQUIRE_EQ(0, r); child = fork(); ATF_REQUIRE(child != -1); if (child == 0) { grandchild = fork(); if (grandchild == -1) _exit(101); else if (grandchild == 0) _exit(2); else { if (waitid(P_PID, grandchild, NULL, WNOWAIT | WEXITED) != 0) _exit(102); _exit(3); } } pid = waitpid(child, NULL, 0); ATF_REQUIRE_EQ(child, pid); r = sigwaitinfo(&mask, &info); ATF_REQUIRE_EQ(SIGCHLD, r); ATF_CHECK_EQ(SIGCHLD, info.si_signo); ATF_CHECK_EQ(CLD_EXITED, info.si_code); ATF_CHECK_EQ(2, info.si_status); grandchild = info.si_pid; ATF_REQUIRE(grandchild > 0); ATF_REQUIRE(grandchild != parent); ATF_REQUIRE(grandchild != child); pid = waitpid(-1, NULL, 0); ATF_REQUIRE_EQ(grandchild, pid); } ATF_TC_WITHOUT_HEAD(reaper_status); ATF_TC_BODY(reaper_status, tc) { struct procctl_reaper_status st; ssize_t sr; pid_t parent, child, pid; int r, status; int pip[2]; parent = getpid(); r = procctl(P_PID, parent, PROC_REAP_STATUS, &st); ATF_REQUIRE_EQ(0, r); ATF_CHECK_EQ(0, st.rs_flags & REAPER_STATUS_OWNED); ATF_CHECK(st.rs_children > 0); ATF_CHECK(st.rs_descendants > 0); ATF_CHECK(st.rs_descendants >= st.rs_children); ATF_CHECK(st.rs_reaper != parent); ATF_CHECK(st.rs_reaper > 0); r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL); ATF_REQUIRE_EQ(0, r); r = procctl(P_PID, parent, PROC_REAP_STATUS, &st); ATF_REQUIRE_EQ(0, r); ATF_CHECK_EQ(REAPER_STATUS_OWNED, st.rs_flags & (REAPER_STATUS_OWNED | REAPER_STATUS_REALINIT)); ATF_CHECK_EQ(0, st.rs_children); ATF_CHECK_EQ(0, st.rs_descendants); ATF_CHECK(st.rs_reaper == parent); ATF_CHECK_EQ(-1, st.rs_pid); r = pipe(pip); ATF_REQUIRE_EQ(0, r); child = fork(); ATF_REQUIRE(child != -1); if (child == 0) { if (close(pip[0]) != 0) _exit(100); if (procctl(P_PID, parent, PROC_REAP_STATUS, &st) != 0) _exit(101); if (write(pip[1], &st, sizeof(st)) != (ssize_t)sizeof(st)) _exit(102); if (procctl(P_PID, getpid(), PROC_REAP_STATUS, &st) != 0) _exit(103); if (write(pip[1], &st, sizeof(st)) != (ssize_t)sizeof(st)) _exit(104); _exit(0); } r = close(pip[1]); ATF_REQUIRE_EQ(0, r); sr = read(pip[0], &st, sizeof(st)); ATF_REQUIRE_EQ((ssize_t)sizeof(st), sr); ATF_CHECK_EQ(REAPER_STATUS_OWNED, st.rs_flags & (REAPER_STATUS_OWNED | REAPER_STATUS_REALINIT)); ATF_CHECK_EQ(1, st.rs_children); ATF_CHECK_EQ(1, st.rs_descendants); ATF_CHECK(st.rs_reaper == parent); ATF_CHECK_EQ(child, st.rs_pid); sr = read(pip[0], &st, sizeof(st)); ATF_REQUIRE_EQ((ssize_t)sizeof(st), sr); ATF_CHECK_EQ(0, st.rs_flags & (REAPER_STATUS_OWNED | REAPER_STATUS_REALINIT)); ATF_CHECK_EQ(1, st.rs_children); ATF_CHECK_EQ(1, st.rs_descendants); ATF_CHECK(st.rs_reaper == parent); ATF_CHECK_EQ(child, st.rs_pid); r = close(pip[0]); ATF_REQUIRE_EQ(0, r); pid = waitpid(child, &status, 0); ATF_REQUIRE_EQ(child, pid); ATF_CHECK_EQ(0, status); r = procctl(P_PID, parent, PROC_REAP_STATUS, &st); ATF_REQUIRE_EQ(0, r); ATF_CHECK_EQ(REAPER_STATUS_OWNED, st.rs_flags & (REAPER_STATUS_OWNED | REAPER_STATUS_REALINIT)); ATF_CHECK_EQ(0, st.rs_children); ATF_CHECK_EQ(0, st.rs_descendants); ATF_CHECK(st.rs_reaper == parent); ATF_CHECK_EQ(-1, st.rs_pid); } ATF_TC_WITHOUT_HEAD(reaper_getpids); ATF_TC_BODY(reaper_getpids, tc) { struct procctl_reaper_pidinfo info[10]; ssize_t sr; pid_t parent, child, grandchild, pid; int r, status, childidx; int pipa[2], pipb[2]; parent = getpid(); r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL); ATF_REQUIRE_EQ(0, r); memset(info, '\0', sizeof(info)); r = procctl(P_PID, parent, PROC_REAP_GETPIDS, &(struct procctl_reaper_pids){ .rp_count = sizeof(info) / sizeof(info[0]), .rp_pids = info }); ATF_CHECK_EQ(0, r); ATF_CHECK_EQ(0, info[0].pi_flags & REAPER_PIDINFO_VALID); r = pipe(pipa); ATF_REQUIRE_EQ(0, r); r = pipe(pipb); ATF_REQUIRE_EQ(0, r); child = fork(); ATF_REQUIRE(child != -1); if (child == 0) { if (close(pipa[1]) != 0) _exit(100); if (close(pipb[0]) != 0) _exit(100); if (read(pipa[0], &(uint8_t){ 0 }, 1) != 1) _exit(101); grandchild = fork(); if (grandchild == -1) _exit(102); if (grandchild == 0) { if (write(pipb[1], &(uint8_t){ 0 }, 1) != 1) _exit(103); if (read(pipa[0], &(uint8_t){ 0 }, 1) != 1) _exit(104); _exit(0); } for (;;) pause(); } r = close(pipa[0]); ATF_REQUIRE_EQ(0, r); r = close(pipb[1]); ATF_REQUIRE_EQ(0, r); memset(info, '\0', sizeof(info)); r = procctl(P_PID, parent, PROC_REAP_GETPIDS, &(struct procctl_reaper_pids){ .rp_count = sizeof(info) / sizeof(info[0]), .rp_pids = info }); ATF_CHECK_EQ(0, r); ATF_CHECK_EQ(REAPER_PIDINFO_VALID | REAPER_PIDINFO_CHILD, info[0].pi_flags & (REAPER_PIDINFO_VALID | REAPER_PIDINFO_CHILD)); ATF_CHECK_EQ(child, info[0].pi_pid); ATF_CHECK_EQ(child, info[0].pi_subtree); ATF_CHECK_EQ(0, info[1].pi_flags & REAPER_PIDINFO_VALID); sr = write(pipa[1], &(uint8_t){ 0 }, 1); ATF_REQUIRE_EQ(1, sr); sr = read(pipb[0], &(uint8_t){ 0 }, 1); ATF_REQUIRE_EQ(1, sr); memset(info, '\0', sizeof(info)); r = procctl(P_PID, parent, PROC_REAP_GETPIDS, &(struct procctl_reaper_pids){ .rp_count = sizeof(info) / sizeof(info[0]), .rp_pids = info }); ATF_CHECK_EQ(0, r); ATF_CHECK_EQ(REAPER_PIDINFO_VALID, info[0].pi_flags & REAPER_PIDINFO_VALID); ATF_CHECK_EQ(REAPER_PIDINFO_VALID, info[1].pi_flags & REAPER_PIDINFO_VALID); ATF_CHECK_EQ(0, info[2].pi_flags & REAPER_PIDINFO_VALID); ATF_CHECK_EQ(child, info[0].pi_subtree); ATF_CHECK_EQ(child, info[1].pi_subtree); childidx = info[1].pi_pid == child ? 1 : 0; ATF_CHECK_EQ(REAPER_PIDINFO_CHILD, info[childidx].pi_flags & REAPER_PIDINFO_CHILD); ATF_CHECK_EQ(0, info[childidx ^ 1].pi_flags & REAPER_PIDINFO_CHILD); ATF_CHECK(info[childidx].pi_pid == child); grandchild = info[childidx ^ 1].pi_pid; ATF_CHECK(grandchild > 0); ATF_CHECK(grandchild != child); ATF_CHECK(grandchild != parent); r = kill(child, SIGTERM); ATF_REQUIRE_EQ(0, r); pid = waitpid(child, &status, 0); ATF_REQUIRE_EQ(child, pid); ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM); memset(info, '\0', sizeof(info)); r = procctl(P_PID, parent, PROC_REAP_GETPIDS, &(struct procctl_reaper_pids){ .rp_count = sizeof(info) / sizeof(info[0]), .rp_pids = info }); ATF_CHECK_EQ(0, r); ATF_CHECK_EQ(REAPER_PIDINFO_VALID, info[0].pi_flags & REAPER_PIDINFO_VALID); ATF_CHECK_EQ(0, info[1].pi_flags & REAPER_PIDINFO_VALID); ATF_CHECK_EQ(child, info[0].pi_subtree); ATF_CHECK_EQ(REAPER_PIDINFO_CHILD, info[0].pi_flags & REAPER_PIDINFO_CHILD); ATF_CHECK_EQ(grandchild, info[0].pi_pid); sr = write(pipa[1], &(uint8_t){ 0 }, 1); ATF_REQUIRE_EQ(1, sr); memset(info, '\0', sizeof(info)); r = procctl(P_PID, parent, PROC_REAP_GETPIDS, &(struct procctl_reaper_pids){ .rp_count = sizeof(info) / sizeof(info[0]), .rp_pids = info }); ATF_CHECK_EQ(0, r); ATF_CHECK_EQ(REAPER_PIDINFO_VALID, info[0].pi_flags & REAPER_PIDINFO_VALID); ATF_CHECK_EQ(0, info[1].pi_flags & REAPER_PIDINFO_VALID); ATF_CHECK_EQ(child, info[0].pi_subtree); ATF_CHECK_EQ(REAPER_PIDINFO_CHILD, info[0].pi_flags & REAPER_PIDINFO_CHILD); ATF_CHECK_EQ(grandchild, info[0].pi_pid); pid = waitpid(grandchild, &status, 0); ATF_REQUIRE_EQ(grandchild, pid); ATF_CHECK_EQ(0, status); memset(info, '\0', sizeof(info)); r = procctl(P_PID, parent, PROC_REAP_GETPIDS, &(struct procctl_reaper_pids){ .rp_count = sizeof(info) / sizeof(info[0]), .rp_pids = info }); ATF_CHECK_EQ(0, r); ATF_CHECK_EQ(0, info[0].pi_flags & REAPER_PIDINFO_VALID); r = close(pipa[1]); ATF_REQUIRE_EQ(0, r); r = close(pipb[0]); ATF_REQUIRE_EQ(0, r); } ATF_TC_WITHOUT_HEAD(reaper_kill_badsig); ATF_TC_BODY(reaper_kill_badsig, tc) { struct procctl_reaper_kill params; pid_t parent; int r; parent = getpid(); r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL); ATF_REQUIRE_EQ(0, r); params.rk_sig = -1; params.rk_flags = 0; r = procctl(P_PID, parent, PROC_REAP_KILL, ¶ms); ATF_CHECK(r == -1 && errno == EINVAL); } ATF_TC_WITHOUT_HEAD(reaper_kill_sigzero); ATF_TC_BODY(reaper_kill_sigzero, tc) { struct procctl_reaper_kill params; pid_t parent; int r; parent = getpid(); r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL); ATF_REQUIRE_EQ(0, r); params.rk_sig = 0; params.rk_flags = 0; r = procctl(P_PID, parent, PROC_REAP_KILL, ¶ms); ATF_CHECK(r == -1 && errno == EINVAL); } ATF_TC_WITHOUT_HEAD(reaper_kill_empty); ATF_TC_BODY(reaper_kill_empty, tc) { struct procctl_reaper_kill params; pid_t parent; int r; parent = getpid(); r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL); ATF_REQUIRE_EQ(0, r); params.rk_sig = SIGTERM; params.rk_flags = 0; params.rk_killed = 77; r = procctl(P_PID, parent, PROC_REAP_KILL, ¶ms); ATF_CHECK(r == -1 && errno == ESRCH); ATF_CHECK_EQ(0, params.rk_killed); } ATF_TC_WITHOUT_HEAD(reaper_kill_normal); ATF_TC_BODY(reaper_kill_normal, tc) { struct procctl_reaper_kill params; ssize_t sr; pid_t parent, child, grandchild, pid; int r, status; int pip[2]; parent = getpid(); r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL); ATF_REQUIRE_EQ(0, r); r = pipe(pip); ATF_REQUIRE_EQ(0, r); child = fork(); ATF_REQUIRE(child != -1); if (child == 0) { if (close(pip[0]) != 0) _exit(100); grandchild = fork(); if (grandchild == -1) _exit(101); if (grandchild == 0) { if (write(pip[1], &(uint8_t){ 0 }, 1) != 1) _exit(102); for (;;) pause(); } for (;;) pause(); } r = close(pip[1]); ATF_REQUIRE_EQ(0, r); sr = read(pip[0], &(uint8_t){ 0 }, 1); ATF_REQUIRE_EQ(1, sr); params.rk_sig = SIGTERM; params.rk_flags = 0; params.rk_killed = 77; r = procctl(P_PID, parent, PROC_REAP_KILL, ¶ms); ATF_CHECK_EQ(0, r); ATF_CHECK_EQ(2, params.rk_killed); pid = waitpid(child, &status, 0); ATF_REQUIRE_EQ(child, pid); ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM); pid = waitpid(-1, &status, 0); ATF_REQUIRE(pid > 0); ATF_CHECK(pid != parent); ATF_CHECK(pid != child); ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM); r = close(pip[0]); ATF_REQUIRE_EQ(0, r); } ATF_TC_WITHOUT_HEAD(reaper_kill_subtree); ATF_TC_BODY(reaper_kill_subtree, tc) { struct procctl_reaper_kill params; ssize_t sr; pid_t parent, child1, child2, grandchild1, grandchild2, pid; int r, status; int pip[2]; parent = getpid(); r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL); ATF_REQUIRE_EQ(0, r); r = pipe(pip); ATF_REQUIRE_EQ(0, r); child1 = fork(); ATF_REQUIRE(child1 != -1); if (child1 == 0) { if (close(pip[0]) != 0) _exit(100); grandchild1 = fork(); if (grandchild1 == -1) _exit(101); if (grandchild1 == 0) { if (write(pip[1], &(uint8_t){ 0 }, 1) != 1) _exit(102); for (;;) pause(); } for (;;) pause(); } child2 = fork(); ATF_REQUIRE(child2 != -1); if (child2 == 0) { if (close(pip[0]) != 0) _exit(100); grandchild2 = fork(); if (grandchild2 == -1) _exit(101); if (grandchild2 == 0) { if (write(pip[1], &(uint8_t){ 0 }, 1) != 1) _exit(102); for (;;) pause(); } for (;;) pause(); } r = close(pip[1]); ATF_REQUIRE_EQ(0, r); sr = read(pip[0], &(uint8_t){ 0 }, 1); ATF_REQUIRE_EQ(1, sr); sr = read(pip[0], &(uint8_t){ 0 }, 1); ATF_REQUIRE_EQ(1, sr); params.rk_sig = SIGUSR1; params.rk_flags = REAPER_KILL_SUBTREE; params.rk_subtree = child1; params.rk_killed = 77; r = procctl(P_PID, parent, PROC_REAP_KILL, ¶ms); ATF_REQUIRE_EQ(0, r); ATF_REQUIRE_EQ(2, params.rk_killed); ATF_CHECK_EQ(-1, params.rk_fpid); pid = waitpid(child1, &status, 0); ATF_REQUIRE_EQ(child1, pid); ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGUSR1); pid = waitpid(-1, &status, 0); ATF_REQUIRE(pid > 0); ATF_CHECK(pid != parent); ATF_CHECK(pid != child1); ATF_CHECK(pid != child2); ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGUSR1); params.rk_sig = SIGUSR2; params.rk_flags = REAPER_KILL_SUBTREE; params.rk_subtree = child2; params.rk_killed = 77; r = procctl(P_PID, parent, PROC_REAP_KILL, ¶ms); ATF_REQUIRE_EQ(0, r); ATF_REQUIRE_EQ(2, params.rk_killed); ATF_CHECK_EQ(-1, params.rk_fpid); pid = waitpid(child2, &status, 0); ATF_REQUIRE_EQ(child2, pid); ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGUSR2); pid = waitpid(-1, &status, 0); ATF_REQUIRE(pid > 0); ATF_CHECK(pid != parent); ATF_CHECK(pid != child1); ATF_CHECK(pid != child2); ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGUSR2); r = close(pip[0]); ATF_REQUIRE_EQ(0, r); } +ATF_TC_WITHOUT_HEAD(reaper_pdfork); +ATF_TC_BODY(reaper_pdfork, tc) +{ + struct procctl_reaper_status st; + pid_t child, grandchild, parent, pid; + int pd, r, status; + + parent = getpid(); + r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL); + ATF_REQUIRE_EQ(r, 0); + + child = pdfork(&pd, 0); + ATF_REQUIRE(child != -1); + if (child == 0) { + grandchild = pdfork(&pd, 0); + if (grandchild == -1) + _exit(1); + if (grandchild == 0) + pause(); + _exit(0); + } + pid = waitpid(child, &status, 0); + ATF_REQUIRE_EQ(pid, child); + r = WIFEXITED(status) ? WEXITSTATUS(status) : -1; + ATF_REQUIRE_EQ(r, 0); + + r = procctl(P_PID, parent, PROC_REAP_STATUS, &st); + ATF_REQUIRE_EQ(r, 0); + ATF_CHECK((st.rs_flags & REAPER_STATUS_OWNED) != 0); + ATF_CHECK(st.rs_reaper == parent); + ATF_CHECK(st.rs_children == 1); + ATF_CHECK(st.rs_descendants == 1); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, reaper_wait_child_first); ATF_TP_ADD_TC(tp, reaper_wait_grandchild_first); ATF_TP_ADD_TC(tp, reaper_sigchld_child_first); ATF_TP_ADD_TC(tp, reaper_sigchld_grandchild_first); ATF_TP_ADD_TC(tp, reaper_status); ATF_TP_ADD_TC(tp, reaper_getpids); ATF_TP_ADD_TC(tp, reaper_kill_badsig); ATF_TP_ADD_TC(tp, reaper_kill_sigzero); ATF_TP_ADD_TC(tp, reaper_kill_empty); ATF_TP_ADD_TC(tp, reaper_kill_normal); ATF_TP_ADD_TC(tp, reaper_kill_subtree); + ATF_TP_ADD_TC(tp, reaper_pdfork); return (atf_no_error()); }