1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009 Robert N. M. Watson 5 * All rights reserved. 6 * 7 * This software was developed at the University of Cambridge Computer 8 * Laboratory with support from a grant from Google, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef _SYS_PROCDESC_H_ 33 #define _SYS_PROCDESC_H_ 34 35 #ifdef _KERNEL 36 37 #include <sys/selinfo.h> /* struct selinfo */ 38 #include <sys/_lock.h> 39 #include <sys/_mutex.h> 40 41 /*- 42 * struct procdesc describes a process descriptor, and essentially consists 43 * of two pointers -- one to the file descriptor, and one to the process. 44 * When both become NULL, the process descriptor will be freed. An important 45 * invariant is that there is only ever one process descriptor for a process, 46 * so a single file pointer will suffice. 47 * 48 * Locking key: 49 * (c) - Constant after initial setup. 50 * (p) - Protected by the process descriptor mutex. 51 * (r) - Atomic reference count. 52 * (s) - Protected by selinfo. 53 * (t) - Protected by the proctree_lock 54 * (p|t) - Both procree_lock and process descriptor must be locked 55 * for pd_fpcount 56 */ 57 struct proc; 58 struct sigio; 59 struct procdesc { 60 /* 61 * Basic process descriptor state: the process, a cache of its pid to 62 * satisfy queries after the process exits, and process descriptor 63 * refcount. 64 */ 65 struct proc *pd_proc; /* (t) Process. */ 66 pid_t pd_pid; /* (c) Cached pid. */ 67 pid_t pd_last_child; /* (p) Pid of the last child. */ 68 u_int pd_refcount; /* (r) Reference count. */ 69 u_int pd_fpcount; /* (p|t) files referencing me */ 70 71 /* 72 * In-flight data and notification of events. 73 */ 74 int pd_flags; /* (t) PD_ flags. */ 75 struct selinfo pd_selinfo; /* (p) Event notification. */ 76 struct mtx pd_lock; /* Protect data + events. */ 77 78 /* Exit status. */ 79 u_int pd_xexit; 80 u_int pd_xsig; 81 struct __wrusage pd_wrusage; 82 siginfo_t pd_siginfo; 83 }; 84 85 /* 86 * Locking macros for the procdesc itself. 87 */ 88 #define PROCDESC_LOCK_DESTROY(pd) mtx_destroy(&(pd)->pd_lock) 89 #define PROCDESC_LOCK_INIT(pd) mtx_init(&(pd)->pd_lock, "procdesc", NULL, \ 90 MTX_DEF) 91 #define PROCDESC_LOCK(pd) mtx_lock(&(pd)->pd_lock) 92 #define PROCDESC_UNLOCK(pd) mtx_unlock(&(pd)->pd_lock) 93 94 /* 95 * Flags for the pd_flags field. 96 */ 97 #define PDF_EXIT_INFO 0x00000001 /* Exit info calculated. */ 98 #define PDF_EXITED 0x00000004 /* Process exited. */ 99 100 /* 101 * Flags for file f_pdflags. 102 */ 103 #define F_PD_NOKILL 0x00000001 /* Opened with PD_DAEMON. Don't send 104 SIGKILL when file closes. */ 105 #define F_PD_NOFINSTALL 0x00000002 /* Procdesc file is closing because 106 finstall() failed */ 107 108 /* 109 * In-kernel interfaces to process descriptors. 110 */ 111 bool procdesc_exit(struct proc *); 112 void procdesc_fork(struct proc *p, pid_t child_pid); 113 void procdesc_jobstate(struct proc *p); 114 int kern_pdgetpid(struct thread *, int fd, const cap_rights_t *, 115 pid_t *pidp); 116 void procdesc_new(struct proc *, int); 117 void procdesc_finit(struct procdesc *, struct file *); 118 pid_t procdesc_pid(struct file *); 119 void procdesc_reap(struct proc *); 120 void procdesc_fill_winfo(struct procdesc *pd, bool proc_locked); 121 122 int procdesc_falloc(struct thread *, struct file **, int *, int, 123 struct filecaps *); 124 int fget_procdesc(struct thread *td, int pfd, 125 const cap_rights_t *cap_rights, int wrong_type_error, 126 struct file **pfp, struct procdesc **pdp, struct proc **pp); 127 #else /* !_KERNEL */ 128 129 #include <sys/cdefs.h> 130 #include <sys/_types.h> 131 132 #ifndef _PID_T_DECLARED 133 typedef __pid_t pid_t; 134 #define _PID_T_DECLARED 135 #endif 136 137 struct rusage; 138 139 /* 140 * Process descriptor system calls. 141 */ 142 __BEGIN_DECLS 143 struct __wrusage; 144 struct __siginfo; 145 146 pid_t pdfork(int *, int); 147 pid_t pdrfork(int *, int, int); 148 int pdkill(int, int); 149 int pdgetpid(int, pid_t *); 150 int pdopenpid(pid_t, int); 151 int pdwait(int, int *, int, struct __wrusage *, struct __siginfo *); 152 int pddupfd(int, int, int); 153 pid_t pdrfork_thread(int *, int, int, void *, int (*)(void *), void *); 154 int pdptrace(int, int, int, void *, int); 155 __END_DECLS 156 157 #endif /* _KERNEL */ 158 159 /* 160 * Flags which can be passed to pdfork(2). 161 */ 162 #define PD_DAEMON 0x00000001 /* Don't exit when procdesc closes. */ 163 #define PD_CLOEXEC 0x00000002 /* Close file descriptor on exec. */ 164 #define PD_NOWAITPID 0x00000004 /* Reap without waitpid(). */ 165 #define PD_PTRACE_CAP 0x00000008 /* Allow PT_PROCDESC in cap mode. */ 166 167 #define PD_ALLOWED_AT_FORK \ 168 (PD_DAEMON | PD_CLOEXEC | PD_NOWAITPID | PD_PTRACE_CAP) 169 #define PD_ALLOWED_AT_OPENPID (PD_DAEMON | PD_CLOEXEC | PD_PTRACE_CAP) 170 171 #endif /* !_SYS_PROCDESC_H_ */ 172