| Commit | Line | Data |
|---|---|---|
| 984263bc | 1 | /* |
| 99ad9bc4 MD |
2 | * (MPSAFE) |
| 3 | * | |
| 984263bc MD |
4 | * Copyright (c) 1982, 1986, 1989, 1991, 1993 |
| 5 | * The Regents of the University of California. All rights reserved. | |
| 6 | * | |
| 7 | * Redistribution and use in source and binary forms, with or without | |
| 8 | * modification, are permitted provided that the following conditions | |
| 9 | * are met: | |
| 10 | * 1. Redistributions of source code must retain the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer. | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * 3. All advertising materials mentioning features or use of this software | |
| 16 | * must display the following acknowledgement: | |
| 17 | * This product includes software developed by the University of | |
| 18 | * California, Berkeley and its contributors. | |
| 19 | * 4. Neither the name of the University nor the names of its contributors | |
| 20 | * may be used to endorse or promote products derived from this software | |
| 21 | * without specific prior written permission. | |
| 22 | * | |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 33 | * SUCH DAMAGE. | |
| 34 | * | |
| 35 | * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95 | |
| 36 | * $FreeBSD: src/sys/kern/kern_proc.c,v 1.63.2.9 2003/05/08 07:47:16 kbyanc Exp $ | |
| 37 | */ | |
| 38 | ||
| 39 | #include <sys/param.h> | |
| 40 | #include <sys/systm.h> | |
| 41 | #include <sys/kernel.h> | |
| 42 | #include <sys/sysctl.h> | |
| 43 | #include <sys/malloc.h> | |
| 44 | #include <sys/proc.h> | |
| 56c703bd | 45 | #include <sys/jail.h> |
| 984263bc MD |
46 | #include <sys/filedesc.h> |
| 47 | #include <sys/tty.h> | |
| 8c72e3d5 | 48 | #include <sys/dsched.h> |
| 984263bc | 49 | #include <sys/signalvar.h> |
| 5bf0d9b5 | 50 | #include <sys/spinlock.h> |
| 984263bc MD |
51 | #include <vm/vm.h> |
| 52 | #include <sys/lock.h> | |
| 53 | #include <vm/pmap.h> | |
| 54 | #include <vm/vm_map.h> | |
| 55 | #include <sys/user.h> | |
| 630ccdeb | 56 | #include <machine/smp.h> |
| 984263bc | 57 | |
| 58c2553a | 58 | #include <sys/refcount.h> |
| 5bf0d9b5 | 59 | #include <sys/spinlock2.h> |
| 684a93c4 | 60 | #include <sys/mplock2.h> |
| 5bf0d9b5 | 61 | |
| 984263bc MD |
62 | static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); |
| 63 | MALLOC_DEFINE(M_SESSION, "session", "session header"); | |
| fb2a331e | 64 | MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); |
| f6c36234 | 65 | MALLOC_DEFINE(M_LWP, "lwp", "lwp structures"); |
| 984263bc MD |
66 | MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); |
| 67 | ||
| 28c57d20 | 68 | int ps_showallprocs = 1; |
| 73e4f7b9 | 69 | static int ps_showallthreads = 1; |
| 43a0f7ae MS |
70 | SYSCTL_INT(_security, OID_AUTO, ps_showallprocs, CTLFLAG_RW, |
| 71 | &ps_showallprocs, 0, | |
| 72 | "Unprivileged processes can see proccesses with different UID/GID"); | |
| 73 | SYSCTL_INT(_security, OID_AUTO, ps_showallthreads, CTLFLAG_RW, | |
| 74 | &ps_showallthreads, 0, | |
| 75 | "Unprivileged processes can see kernel threads"); | |
| 984263bc | 76 | |
| 51e64ff2 MD |
77 | static void pgdelete(struct pgrp *); |
| 78 | static void orphanpg(struct pgrp *pg); | |
| 79 | static pid_t proc_getnewpid_locked(int random_offset); | |
| 984263bc MD |
80 | |
| 81 | /* | |
| 82 | * Other process lists | |
| 83 | */ | |
| 84 | struct pidhashhead *pidhashtbl; | |
| 85 | u_long pidhash; | |
| 86 | struct pgrphashhead *pgrphashtbl; | |
| 87 | u_long pgrphash; | |
| 88 | struct proclist allproc; | |
| 89 | struct proclist zombproc; | |
| 984263bc MD |
90 | |
| 91 | /* | |
| 51e64ff2 MD |
92 | * Random component to nextpid generation. We mix in a random factor to make |
| 93 | * it a little harder to predict. We sanity check the modulus value to avoid | |
| 94 | * doing it in critical paths. Don't let it be too small or we pointlessly | |
| 95 | * waste randomness entropy, and don't let it be impossibly large. Using a | |
| 96 | * modulus that is too big causes a LOT more process table scans and slows | |
| 97 | * down fork processing as the pidchecked caching is defeated. | |
| 98 | */ | |
| 99 | static int randompid = 0; | |
| 100 | ||
| 99ad9bc4 MD |
101 | /* |
| 102 | * No requirements. | |
| 103 | */ | |
| 51e64ff2 MD |
104 | static int |
| 105 | sysctl_kern_randompid(SYSCTL_HANDLER_ARGS) | |
| 106 | { | |
| 107 | int error, pid; | |
| 108 | ||
| 109 | pid = randompid; | |
| 110 | error = sysctl_handle_int(oidp, &pid, 0, req); | |
| 111 | if (error || !req->newptr) | |
| 112 | return (error); | |
| 113 | if (pid < 0 || pid > PID_MAX - 100) /* out of range */ | |
| 114 | pid = PID_MAX - 100; | |
| 115 | else if (pid < 2) /* NOP */ | |
| 116 | pid = 0; | |
| 117 | else if (pid < 100) /* Make it reasonable */ | |
| 118 | pid = 100; | |
| 119 | randompid = pid; | |
| 120 | return (error); | |
| 121 | } | |
| 122 | ||
| 123 | SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW, | |
| 124 | 0, 0, sysctl_kern_randompid, "I", "Random PID modulus"); | |
| 125 | ||
| 126 | /* | |
| 984263bc | 127 | * Initialize global process hashing structures. |
| 99ad9bc4 MD |
128 | * |
| 129 | * Called from the low level boot code only. | |
| 984263bc MD |
130 | */ |
| 131 | void | |
| 77153250 | 132 | procinit(void) |
| 984263bc | 133 | { |
| 984263bc MD |
134 | LIST_INIT(&allproc); |
| 135 | LIST_INIT(&zombproc); | |
| 40aaf5fc | 136 | lwkt_init(); |
| 984263bc MD |
137 | pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); |
| 138 | pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); | |
| 984263bc MD |
139 | uihashinit(); |
| 140 | } | |
| 141 | ||
| 142 | /* | |
| 82354ad8 MD |
143 | * Process hold/release support functions. These functions must be MPSAFE. |
| 144 | * Called via the PHOLD(), PRELE(), and PSTALL() macros. | |
| 145 | * | |
| 146 | * p->p_lock is a simple hold count with a waiting interlock. No wakeup() | |
| 147 | * is issued unless someone is actually waiting for the process. | |
| 148 | * | |
| 149 | * Most holds are short-term, allowing a process scan or other similar | |
| 150 | * operation to access a proc structure without it getting ripped out from | |
| 151 | * under us. procfs and process-list sysctl ops also use the hold function | |
| 152 | * interlocked with various p_flags to keep the vmspace intact when reading | |
| 153 | * or writing a user process's address space. | |
| 154 | * | |
| 155 | * There are two situations where a hold count can be longer. Exiting lwps | |
| 156 | * hold the process until the lwp is reaped, and the parent will hold the | |
| 157 | * child during vfork()/exec() sequences while the child is marked P_PPWAIT. | |
| 158 | * | |
| 159 | * The kernel waits for the hold count to drop to 0 (or 1 in some cases) at | |
| 160 | * various critical points in the fork/exec and exit paths before proceeding. | |
| 161 | */ | |
| 162 | #define PLOCK_WAITING 0x40000000 | |
| 163 | #define PLOCK_MASK 0x3FFFFFFF | |
| 164 | ||
| 165 | void | |
| 166 | pstall(struct proc *p, const char *wmesg, int count) | |
| 167 | { | |
| 168 | int o; | |
| 169 | int n; | |
| 170 | ||
| 171 | for (;;) { | |
| 172 | o = p->p_lock; | |
| 173 | cpu_ccfence(); | |
| 174 | if ((o & PLOCK_MASK) <= count) | |
| 175 | break; | |
| 176 | n = o | PLOCK_WAITING; | |
| 177 | tsleep_interlock(&p->p_lock, 0); | |
| b2a6ad87 MD |
178 | |
| 179 | /* | |
| 180 | * If someone is trying to single-step the process they can | |
| 181 | * prevent us from going into zombie-land. | |
| 182 | */ | |
| 183 | if (p->p_step) { | |
| 184 | spin_lock(&p->p_spin); | |
| 185 | p->p_stops = 0; | |
| 186 | p->p_step = 0; | |
| 187 | spin_unlock(&p->p_spin); | |
| 188 | wakeup(&p->p_step); | |
| 189 | } | |
| 190 | ||
| 82354ad8 MD |
191 | if (atomic_cmpset_int(&p->p_lock, o, n)) { |
| 192 | tsleep(&p->p_lock, PINTERLOCKED, wmesg, 0); | |
| 193 | } | |
| 194 | } | |
| 195 | } | |
| 196 | ||
| 197 | void | |
| 198 | phold(struct proc *p) | |
| 199 | { | |
| 200 | int o; | |
| 201 | int n; | |
| 202 | ||
| 203 | for (;;) { | |
| 204 | o = p->p_lock; | |
| 205 | cpu_ccfence(); | |
| 206 | n = o + 1; | |
| 207 | if (atomic_cmpset_int(&p->p_lock, o, n)) | |
| 208 | break; | |
| 209 | } | |
| 210 | } | |
| 211 | ||
| 212 | void | |
| 213 | prele(struct proc *p) | |
| 214 | { | |
| 215 | int o; | |
| 216 | int n; | |
| 217 | ||
| 218 | /* | |
| 219 | * Fast path | |
| 220 | */ | |
| 221 | if (atomic_cmpset_int(&p->p_lock, 1, 0)) | |
| 222 | return; | |
| 223 | ||
| 224 | /* | |
| 225 | * Slow path | |
| 226 | */ | |
| 227 | for (;;) { | |
| 228 | o = p->p_lock; | |
| 229 | KKASSERT((o & PLOCK_MASK) > 0); | |
| 230 | cpu_ccfence(); | |
| 231 | n = (o - 1) & ~PLOCK_WAITING; | |
| 232 | if (atomic_cmpset_int(&p->p_lock, o, n)) { | |
| 233 | if (o & PLOCK_WAITING) | |
| 234 | wakeup(&p->p_lock); | |
| 235 | break; | |
| 236 | } | |
| 237 | } | |
| 238 | } | |
| 239 | ||
| 240 | /* | |
| 984263bc | 241 | * Is p an inferior of the current process? |
| 99ad9bc4 MD |
242 | * |
| 243 | * No requirements. | |
| 244 | * The caller must hold proc_token if the caller wishes a stable result. | |
| 984263bc MD |
245 | */ |
| 246 | int | |
| 77153250 | 247 | inferior(struct proc *p) |
| 984263bc | 248 | { |
| 99ad9bc4 MD |
249 | lwkt_gettoken(&proc_token); |
| 250 | while (p != curproc) { | |
| 251 | if (p->p_pid == 0) { | |
| 252 | lwkt_reltoken(&proc_token); | |
| 984263bc | 253 | return (0); |
| 99ad9bc4 MD |
254 | } |
| 255 | p = p->p_pptr; | |
| 256 | } | |
| 257 | lwkt_reltoken(&proc_token); | |
| 984263bc MD |
258 | return (1); |
| 259 | } | |
| 260 | ||
| 261 | /* | |
| 58c2553a MD |
262 | * Locate a process by number. The returned process will be referenced and |
| 263 | * must be released with PRELE(). | |
| 99ad9bc4 MD |
264 | * |
| 265 | * No requirements. | |
| 984263bc MD |
266 | */ |
| 267 | struct proc * | |
| 77153250 | 268 | pfind(pid_t pid) |
| 984263bc | 269 | { |
| 1fd87d54 | 270 | struct proc *p; |
| 984263bc | 271 | |
| 99ad9bc4 | 272 | lwkt_gettoken(&proc_token); |
| 5bf0d9b5 | 273 | LIST_FOREACH(p, PIDHASH(pid), p_hash) { |
| 99ad9bc4 | 274 | if (p->p_pid == pid) { |
| 58c2553a | 275 | PHOLD(p); |
| 99ad9bc4 | 276 | lwkt_reltoken(&proc_token); |
| 984263bc | 277 | return (p); |
| 99ad9bc4 | 278 | } |
| 5bf0d9b5 | 279 | } |
| 99ad9bc4 | 280 | lwkt_reltoken(&proc_token); |
| 984263bc MD |
281 | return (NULL); |
| 282 | } | |
| 283 | ||
| 284 | /* | |
| 58c2553a MD |
285 | * Locate a process by number. The returned process is NOT referenced. |
| 286 | * The caller should hold proc_token if the caller wishes a stable result. | |
| 287 | * | |
| 288 | * No requirements. | |
| 289 | */ | |
| 290 | struct proc * | |
| 291 | pfindn(pid_t pid) | |
| 292 | { | |
| 293 | struct proc *p; | |
| 294 | ||
| 295 | lwkt_gettoken(&proc_token); | |
| 296 | LIST_FOREACH(p, PIDHASH(pid), p_hash) { | |
| 297 | if (p->p_pid == pid) { | |
| 298 | lwkt_reltoken(&proc_token); | |
| 299 | return (p); | |
| 300 | } | |
| 301 | } | |
| 302 | lwkt_reltoken(&proc_token); | |
| 303 | return (NULL); | |
| 304 | } | |
| 305 | ||
| 306 | void | |
| 307 | pgref(struct pgrp *pgrp) | |
| 308 | { | |
| 309 | refcount_acquire(&pgrp->pg_refs); | |
| 310 | } | |
| 311 | ||
| 312 | void | |
| 313 | pgrel(struct pgrp *pgrp) | |
| 314 | { | |
| 315 | if (refcount_release(&pgrp->pg_refs)) | |
| 316 | pgdelete(pgrp); | |
| 317 | } | |
| 318 | ||
| 319 | /* | |
| 320 | * Locate a process group by number. The returned process group will be | |
| 321 | * referenced w/pgref() and must be released with pgrel() (or assigned | |
| 322 | * somewhere if you wish to keep the reference). | |
| 99ad9bc4 MD |
323 | * |
| 324 | * No requirements. | |
| 984263bc MD |
325 | */ |
| 326 | struct pgrp * | |
| 77153250 | 327 | pgfind(pid_t pgid) |
| 984263bc | 328 | { |
| 1fd87d54 | 329 | struct pgrp *pgrp; |
| 984263bc | 330 | |
| 99ad9bc4 | 331 | lwkt_gettoken(&proc_token); |
| 5bf0d9b5 | 332 | LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { |
| 46270ec6 | 333 | if (pgrp->pg_id == pgid) { |
| 58c2553a | 334 | refcount_acquire(&pgrp->pg_refs); |
| 46270ec6 | 335 | lwkt_reltoken(&proc_token); |
| 984263bc | 336 | return (pgrp); |
| 46270ec6 | 337 | } |
| 5bf0d9b5 | 338 | } |
| 99ad9bc4 | 339 | lwkt_reltoken(&proc_token); |
| 984263bc MD |
340 | return (NULL); |
| 341 | } | |
| 342 | ||
| 343 | /* | |
| 344 | * Move p to a new or existing process group (and session) | |
| 99ad9bc4 MD |
345 | * |
| 346 | * No requirements. | |
| 984263bc MD |
347 | */ |
| 348 | int | |
| 77153250 | 349 | enterpgrp(struct proc *p, pid_t pgid, int mksess) |
| 984263bc | 350 | { |
| 99ad9bc4 | 351 | struct pgrp *pgrp; |
| 58c2553a | 352 | struct pgrp *opgrp; |
| 99ad9bc4 MD |
353 | int error; |
| 354 | ||
| 99ad9bc4 | 355 | pgrp = pgfind(pgid); |
| 984263bc MD |
356 | |
| 357 | KASSERT(pgrp == NULL || !mksess, | |
| 99ad9bc4 | 358 | ("enterpgrp: setsid into non-empty pgrp")); |
| 984263bc | 359 | KASSERT(!SESS_LEADER(p), |
| 99ad9bc4 | 360 | ("enterpgrp: session leader attempted setpgrp")); |
| 984263bc MD |
361 | |
| 362 | if (pgrp == NULL) { | |
| 363 | pid_t savepid = p->p_pid; | |
| 364 | struct proc *np; | |
| 365 | /* | |
| 366 | * new process group | |
| 367 | */ | |
| 368 | KASSERT(p->p_pid == pgid, | |
| 99ad9bc4 | 369 | ("enterpgrp: new pgrp and pid != pgid")); |
| 58c2553a | 370 | if ((np = pfindn(savepid)) == NULL || np != p) { |
| 99ad9bc4 MD |
371 | error = ESRCH; |
| 372 | goto fatal; | |
| 373 | } | |
| 884717e1 | 374 | pgrp = kmalloc(sizeof(struct pgrp), M_PGRP, M_WAITOK); |
| 984263bc | 375 | if (mksess) { |
| 1fd87d54 | 376 | struct session *sess; |
| 984263bc MD |
377 | |
| 378 | /* | |
| 379 | * new session | |
| 380 | */ | |
| 884717e1 SW |
381 | sess = kmalloc(sizeof(struct session), M_SESSION, |
| 382 | M_WAITOK); | |
| 984263bc MD |
383 | sess->s_leader = p; |
| 384 | sess->s_sid = p->p_pid; | |
| 385 | sess->s_count = 1; | |
| 386 | sess->s_ttyvp = NULL; | |
| 387 | sess->s_ttyp = NULL; | |
| 388 | bcopy(p->p_session->s_login, sess->s_login, | |
| 99ad9bc4 | 389 | sizeof(sess->s_login)); |
| 984263bc MD |
390 | pgrp->pg_session = sess; |
| 391 | KASSERT(p == curproc, | |
| 99ad9bc4 | 392 | ("enterpgrp: mksession and p != curproc")); |
| 616516c8 | 393 | lwkt_gettoken(&p->p_token); |
| 4643740a | 394 | p->p_flags &= ~P_CONTROLT; |
| 616516c8 | 395 | lwkt_reltoken(&p->p_token); |
| 984263bc MD |
396 | } else { |
| 397 | pgrp->pg_session = p->p_session; | |
| 8b90699b | 398 | sess_hold(pgrp->pg_session); |
| 984263bc MD |
399 | } |
| 400 | pgrp->pg_id = pgid; | |
| 401 | LIST_INIT(&pgrp->pg_members); | |
| 402 | LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); | |
| 403 | pgrp->pg_jobc = 0; | |
| 404 | SLIST_INIT(&pgrp->pg_sigiolst); | |
| 58c2553a MD |
405 | lwkt_token_init(&pgrp->pg_token, "pgrp_token"); |
| 406 | refcount_init(&pgrp->pg_refs, 1); | |
| 167e6ecb | 407 | lockinit(&pgrp->pg_lock, "pgwt", 0, 0); |
| 99ad9bc4 | 408 | } else if (pgrp == p->p_pgrp) { |
| 58c2553a | 409 | pgrel(pgrp); |
| 99ad9bc4 | 410 | goto done; |
| 58c2553a | 411 | } /* else pgfind() referenced the pgrp */ |
| 984263bc MD |
412 | |
| 413 | /* | |
| 414 | * Adjust eligibility of affected pgrps to participate in job control. | |
| 415 | * Increment eligibility counts before decrementing, otherwise we | |
| 416 | * could reach 0 spuriously during the first call. | |
| 417 | */ | |
| 58c2553a MD |
418 | lwkt_gettoken(&pgrp->pg_token); |
| 419 | lwkt_gettoken(&p->p_token); | |
| 984263bc MD |
420 | fixjobc(p, pgrp, 1); |
| 421 | fixjobc(p, p->p_pgrp, 0); | |
| 58c2553a MD |
422 | while ((opgrp = p->p_pgrp) != NULL) { |
| 423 | opgrp = p->p_pgrp; | |
| 424 | lwkt_gettoken(&opgrp->pg_token); | |
| 425 | LIST_REMOVE(p, p_pglist); | |
| 426 | p->p_pgrp = NULL; | |
| 427 | lwkt_reltoken(&opgrp->pg_token); | |
| 428 | pgrel(opgrp); | |
| 429 | } | |
| 984263bc MD |
430 | p->p_pgrp = pgrp; |
| 431 | LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); | |
| 58c2553a MD |
432 | lwkt_reltoken(&p->p_token); |
| 433 | lwkt_reltoken(&pgrp->pg_token); | |
| 99ad9bc4 MD |
434 | done: |
| 435 | error = 0; | |
| 436 | fatal: | |
| 99ad9bc4 | 437 | return (error); |
| 984263bc MD |
438 | } |
| 439 | ||
| 440 | /* | |
| 99ad9bc4 MD |
441 | * Remove process from process group |
| 442 | * | |
| 443 | * No requirements. | |
| 984263bc MD |
444 | */ |
| 445 | int | |
| 77153250 | 446 | leavepgrp(struct proc *p) |
| 984263bc | 447 | { |
| 58c2553a MD |
448 | struct pgrp *pg = p->p_pgrp; |
| 449 | ||
| 450 | lwkt_gettoken(&p->p_token); | |
| 451 | pg = p->p_pgrp; | |
| 452 | if (pg) { | |
| 453 | pgref(pg); | |
| 454 | lwkt_gettoken(&pg->pg_token); | |
| 455 | if (p->p_pgrp == pg) { | |
| 456 | p->p_pgrp = NULL; | |
| 457 | LIST_REMOVE(p, p_pglist); | |
| 458 | pgrel(pg); | |
| 459 | } | |
| 460 | lwkt_reltoken(&pg->pg_token); | |
| 461 | lwkt_reltoken(&p->p_token); /* avoid chaining on rel */ | |
| 462 | pgrel(pg); | |
| 463 | } else { | |
| 464 | lwkt_reltoken(&p->p_token); | |
| 465 | } | |
| 984263bc MD |
466 | return (0); |
| 467 | } | |
| 468 | ||
| 469 | /* | |
| 58c2553a MD |
470 | * Delete a process group. Must be called only after the last ref has been |
| 471 | * released. | |
| 984263bc MD |
472 | */ |
| 473 | static void | |
| 77153250 | 474 | pgdelete(struct pgrp *pgrp) |
| 984263bc | 475 | { |
| 984263bc MD |
476 | /* |
| 477 | * Reset any sigio structures pointing to us as a result of | |
| 478 | * F_SETOWN with our pgid. | |
| 479 | */ | |
| 480 | funsetownlst(&pgrp->pg_sigiolst); | |
| 481 | ||
| 482 | if (pgrp->pg_session->s_ttyp != NULL && | |
| 483 | pgrp->pg_session->s_ttyp->t_pgrp == pgrp) | |
| 484 | pgrp->pg_session->s_ttyp->t_pgrp = NULL; | |
| 485 | LIST_REMOVE(pgrp, pg_hash); | |
| 8b90699b | 486 | sess_rele(pgrp->pg_session); |
| efda3bd0 | 487 | kfree(pgrp, M_PGRP); |
| 8b90699b MD |
488 | } |
| 489 | ||
| 490 | /* | |
| 491 | * Adjust the ref count on a session structure. When the ref count falls to | |
| 492 | * zero the tty is disassociated from the session and the session structure | |
| 493 | * is freed. Note that tty assocation is not itself ref-counted. | |
| 99ad9bc4 MD |
494 | * |
| 495 | * No requirements. | |
| 8b90699b MD |
496 | */ |
| 497 | void | |
| 498 | sess_hold(struct session *sp) | |
| 499 | { | |
| 99ad9bc4 | 500 | lwkt_gettoken(&tty_token); |
| 8b90699b | 501 | ++sp->s_count; |
| 99ad9bc4 | 502 | lwkt_reltoken(&tty_token); |
| 8b90699b MD |
503 | } |
| 504 | ||
| 99ad9bc4 MD |
505 | /* |
| 506 | * No requirements. | |
| 507 | */ | |
| 8b90699b MD |
508 | void |
| 509 | sess_rele(struct session *sp) | |
| 510 | { | |
| 94a6eea8 MD |
511 | struct tty *tp; |
| 512 | ||
| 8b90699b | 513 | KKASSERT(sp->s_count > 0); |
| 99ad9bc4 | 514 | lwkt_gettoken(&tty_token); |
| 8b90699b MD |
515 | if (--sp->s_count == 0) { |
| 516 | if (sp->s_ttyp && sp->s_ttyp->t_session) { | |
| 517 | #ifdef TTY_DO_FULL_CLOSE | |
| 518 | /* FULL CLOSE, see ttyclearsession() */ | |
| 519 | KKASSERT(sp->s_ttyp->t_session == sp); | |
| 520 | sp->s_ttyp->t_session = NULL; | |
| 521 | #else | |
| 522 | /* HALF CLOSE, see ttyclearsession() */ | |
| 523 | if (sp->s_ttyp->t_session == sp) | |
| 524 | sp->s_ttyp->t_session = NULL; | |
| 525 | #endif | |
| 526 | } | |
| 94a6eea8 MD |
527 | if ((tp = sp->s_ttyp) != NULL) { |
| 528 | sp->s_ttyp = NULL; | |
| 529 | ttyunhold(tp); | |
| 530 | } | |
| efda3bd0 | 531 | kfree(sp, M_SESSION); |
| 8b90699b | 532 | } |
| 99ad9bc4 | 533 | lwkt_reltoken(&tty_token); |
| 984263bc MD |
534 | } |
| 535 | ||
| 536 | /* | |
| 537 | * Adjust pgrp jobc counters when specified process changes process group. | |
| 538 | * We count the number of processes in each process group that "qualify" | |
| 539 | * the group for terminal job control (those with a parent in a different | |
| 540 | * process group of the same session). If that count reaches zero, the | |
| 541 | * process group becomes orphaned. Check both the specified process' | |
| 542 | * process group and that of its children. | |
| 543 | * entering == 0 => p is leaving specified group. | |
| 544 | * entering == 1 => p is entering specified group. | |
| 99ad9bc4 MD |
545 | * |
| 546 | * No requirements. | |
| 984263bc MD |
547 | */ |
| 548 | void | |
| 77153250 | 549 | fixjobc(struct proc *p, struct pgrp *pgrp, int entering) |
| 984263bc | 550 | { |
| 1fd87d54 | 551 | struct pgrp *hispgrp; |
| 99ad9bc4 | 552 | struct session *mysession; |
| b5c4d81f | 553 | struct proc *np; |
| 984263bc MD |
554 | |
| 555 | /* | |
| 556 | * Check p's parent to see whether p qualifies its own process | |
| 557 | * group; if so, adjust count for p's process group. | |
| 558 | */ | |
| b5c4d81f | 559 | lwkt_gettoken(&p->p_token); /* p_children scan */ |
| 58c2553a | 560 | lwkt_gettoken(&pgrp->pg_token); |
| b5c4d81f | 561 | |
| 99ad9bc4 | 562 | mysession = pgrp->pg_session; |
| 984263bc MD |
563 | if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && |
| 564 | hispgrp->pg_session == mysession) { | |
| 565 | if (entering) | |
| 566 | pgrp->pg_jobc++; | |
| 567 | else if (--pgrp->pg_jobc == 0) | |
| 568 | orphanpg(pgrp); | |
| 569 | } | |
| 570 | ||
| 571 | /* | |
| 572 | * Check this process' children to see whether they qualify | |
| 573 | * their process groups; if so, adjust counts for children's | |
| 574 | * process groups. | |
| 575 | */ | |
| b5c4d81f | 576 | LIST_FOREACH(np, &p->p_children, p_sibling) { |
| 58c2553a MD |
577 | PHOLD(np); |
| 578 | lwkt_gettoken(&np->p_token); | |
| b5c4d81f | 579 | if ((hispgrp = np->p_pgrp) != pgrp && |
| 984263bc | 580 | hispgrp->pg_session == mysession && |
| b5c4d81f | 581 | np->p_stat != SZOMB) { |
| 58c2553a MD |
582 | pgref(hispgrp); |
| 583 | lwkt_gettoken(&hispgrp->pg_token); | |
| 984263bc MD |
584 | if (entering) |
| 585 | hispgrp->pg_jobc++; | |
| 586 | else if (--hispgrp->pg_jobc == 0) | |
| 587 | orphanpg(hispgrp); | |
| 58c2553a MD |
588 | lwkt_reltoken(&hispgrp->pg_token); |
| 589 | pgrel(hispgrp); | |
| 984263bc | 590 | } |
| 58c2553a MD |
591 | lwkt_reltoken(&np->p_token); |
| 592 | PRELE(np); | |
| 99ad9bc4 | 593 | } |
| 58c2553a MD |
594 | KKASSERT(pgrp->pg_refs > 0); |
| 595 | lwkt_reltoken(&pgrp->pg_token); | |
| b5c4d81f | 596 | lwkt_reltoken(&p->p_token); |
| 984263bc MD |
597 | } |
| 598 | ||
| 599 | /* | |
| 600 | * A process group has become orphaned; | |
| 601 | * if there are any stopped processes in the group, | |
| 602 | * hang-up all process in that group. | |
| 99ad9bc4 | 603 | * |
| 58c2553a | 604 | * The caller must hold pg_token. |
| 984263bc MD |
605 | */ |
| 606 | static void | |
| 77153250 | 607 | orphanpg(struct pgrp *pg) |
| 984263bc | 608 | { |
| 1fd87d54 | 609 | struct proc *p; |
| 984263bc MD |
610 | |
| 611 | LIST_FOREACH(p, &pg->pg_members, p_pglist) { | |
| 164b8401 | 612 | if (p->p_stat == SSTOP) { |
| 984263bc | 613 | LIST_FOREACH(p, &pg->pg_members, p_pglist) { |
| 84204577 MD |
614 | ksignal(p, SIGHUP); |
| 615 | ksignal(p, SIGCONT); | |
| 984263bc MD |
616 | } |
| 617 | return; | |
| 618 | } | |
| 619 | } | |
| 620 | } | |
| 621 | ||
| 5bf0d9b5 | 622 | /* |
| 51e64ff2 MD |
623 | * Add a new process to the allproc list and the PID hash. This |
| 624 | * also assigns a pid to the new process. | |
| 625 | * | |
| 99ad9bc4 | 626 | * No requirements. |
| 51e64ff2 MD |
627 | */ |
| 628 | void | |
| 629 | proc_add_allproc(struct proc *p) | |
| 630 | { | |
| 631 | int random_offset; | |
| 632 | ||
| 633 | if ((random_offset = randompid) != 0) { | |
| 634 | get_mplock(); | |
| 0ced1954 | 635 | random_offset = karc4random() % random_offset; |
| 51e64ff2 MD |
636 | rel_mplock(); |
| 637 | } | |
| 638 | ||
| 99ad9bc4 | 639 | lwkt_gettoken(&proc_token); |
| 51e64ff2 MD |
640 | p->p_pid = proc_getnewpid_locked(random_offset); |
| 641 | LIST_INSERT_HEAD(&allproc, p, p_list); | |
| 642 | LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash); | |
| 99ad9bc4 | 643 | lwkt_reltoken(&proc_token); |
| 51e64ff2 MD |
644 | } |
| 645 | ||
| 646 | /* | |
| 647 | * Calculate a new process pid. This function is integrated into | |
| 648 | * proc_add_allproc() to guarentee that the new pid is not reused before | |
| 649 | * the new process can be added to the allproc list. | |
| 650 | * | |
| 99ad9bc4 | 651 | * The caller must hold proc_token. |
| 51e64ff2 MD |
652 | */ |
| 653 | static | |
| 654 | pid_t | |
| 655 | proc_getnewpid_locked(int random_offset) | |
| 656 | { | |
| 657 | static pid_t nextpid; | |
| 658 | static pid_t pidchecked; | |
| 659 | struct proc *p; | |
| 660 | ||
| 661 | /* | |
| 662 | * Find an unused process ID. We remember a range of unused IDs | |
| 663 | * ready to use (from nextpid+1 through pidchecked-1). | |
| 664 | */ | |
| 665 | nextpid = nextpid + 1 + random_offset; | |
| 666 | retry: | |
| 667 | /* | |
| 668 | * If the process ID prototype has wrapped around, | |
| 669 | * restart somewhat above 0, as the low-numbered procs | |
| 670 | * tend to include daemons that don't exit. | |
| 671 | */ | |
| 672 | if (nextpid >= PID_MAX) { | |
| 673 | nextpid = nextpid % PID_MAX; | |
| 674 | if (nextpid < 100) | |
| 675 | nextpid += 100; | |
| 676 | pidchecked = 0; | |
| 677 | } | |
| 678 | if (nextpid >= pidchecked) { | |
| 679 | int doingzomb = 0; | |
| 680 | ||
| 681 | pidchecked = PID_MAX; | |
| 0d78b86e | 682 | |
| 51e64ff2 MD |
683 | /* |
| 684 | * Scan the active and zombie procs to check whether this pid | |
| 685 | * is in use. Remember the lowest pid that's greater | |
| 686 | * than nextpid, so we can avoid checking for a while. | |
| 0d78b86e MD |
687 | * |
| 688 | * NOTE: Processes in the midst of being forked may not | |
| 689 | * yet have p_pgrp and p_pgrp->pg_session set up | |
| 690 | * yet, so we have to check for NULL. | |
| 691 | * | |
| 692 | * Processes being torn down should be interlocked | |
| 693 | * with proc_token prior to the clearing of their | |
| 694 | * p_pgrp. | |
| 51e64ff2 MD |
695 | */ |
| 696 | p = LIST_FIRST(&allproc); | |
| 697 | again: | |
| 0d78b86e | 698 | for (; p != NULL; p = LIST_NEXT(p, p_list)) { |
| 51e64ff2 | 699 | while (p->p_pid == nextpid || |
| 0d78b86e MD |
700 | (p->p_pgrp && p->p_pgrp->pg_id == nextpid) || |
| 701 | (p->p_pgrp && p->p_session && | |
| 702 | p->p_session->s_sid == nextpid)) { | |
| 51e64ff2 MD |
703 | nextpid++; |
| 704 | if (nextpid >= pidchecked) | |
| 705 | goto retry; | |
| 706 | } | |
| 707 | if (p->p_pid > nextpid && pidchecked > p->p_pid) | |
| 708 | pidchecked = p->p_pid; | |
| 0d78b86e MD |
709 | if (p->p_pgrp && |
| 710 | p->p_pgrp->pg_id > nextpid && | |
| 711 | pidchecked > p->p_pgrp->pg_id) { | |
| 51e64ff2 | 712 | pidchecked = p->p_pgrp->pg_id; |
| 0d78b86e MD |
713 | } |
| 714 | if (p->p_pgrp && p->p_session && | |
| 715 | p->p_session->s_sid > nextpid && | |
| 716 | pidchecked > p->p_session->s_sid) { | |
| 51e64ff2 | 717 | pidchecked = p->p_session->s_sid; |
| 0d78b86e | 718 | } |
| 51e64ff2 MD |
719 | } |
| 720 | if (!doingzomb) { | |
| 721 | doingzomb = 1; | |
| 722 | p = LIST_FIRST(&zombproc); | |
| 723 | goto again; | |
| 724 | } | |
| 725 | } | |
| 726 | return(nextpid); | |
| 727 | } | |
| 728 | ||
| 729 | /* | |
| 5bf0d9b5 MD |
730 | * Called from exit1 to remove a process from the allproc |
| 731 | * list and move it to the zombie list. | |
| 732 | * | |
| eb2adbf5 MD |
733 | * Caller must hold p->p_token. We are required to wait until p_lock |
| 734 | * becomes zero before we can manipulate the list, allowing allproc | |
| 735 | * scans to guarantee consistency during a list scan. | |
| 5bf0d9b5 MD |
736 | */ |
| 737 | void | |
| 738 | proc_move_allproc_zombie(struct proc *p) | |
| 739 | { | |
| 99ad9bc4 | 740 | lwkt_gettoken(&proc_token); |
| 82354ad8 | 741 | PSTALL(p, "reap1", 0); |
| 5bf0d9b5 MD |
742 | LIST_REMOVE(p, p_list); |
| 743 | LIST_INSERT_HEAD(&zombproc, p, p_list); | |
| 744 | LIST_REMOVE(p, p_hash); | |
| 416d05d7 | 745 | p->p_stat = SZOMB; |
| 99ad9bc4 | 746 | lwkt_reltoken(&proc_token); |
| 8c72e3d5 | 747 | dsched_exit_proc(p); |
| 5bf0d9b5 MD |
748 | } |
| 749 | ||
| 750 | /* | |
| 751 | * This routine is called from kern_wait() and will remove the process | |
| 752 | * from the zombie list and the sibling list. This routine will block | |
| 753 | * if someone has a lock on the proces (p_lock). | |
| 754 | * | |
| eb2adbf5 MD |
755 | * Caller must hold p->p_token. We are required to wait until p_lock |
| 756 | * becomes zero before we can manipulate the list, allowing allproc | |
| 757 | * scans to guarantee consistency during a list scan. | |
| 5bf0d9b5 MD |
758 | */ |
| 759 | void | |
| 760 | proc_remove_zombie(struct proc *p) | |
| 761 | { | |
| 99ad9bc4 | 762 | lwkt_gettoken(&proc_token); |
| 82354ad8 | 763 | PSTALL(p, "reap2", 0); |
| 5bf0d9b5 MD |
764 | LIST_REMOVE(p, p_list); /* off zombproc */ |
| 765 | LIST_REMOVE(p, p_sibling); | |
| 99ad9bc4 | 766 | lwkt_reltoken(&proc_token); |
| 5bf0d9b5 MD |
767 | } |
| 768 | ||
| 769 | /* | |
| 770 | * Scan all processes on the allproc list. The process is automatically | |
| 771 | * held for the callback. A return value of -1 terminates the loop. | |
| 772 | * | |
| 99ad9bc4 | 773 | * The callback is made with the process held and proc_token held. |
| c5d9d575 MD |
774 | * |
| 775 | * We limit the scan to the number of processes as-of the start of | |
| 776 | * the scan so as not to get caught up in an endless loop if new processes | |
| 777 | * are created more quickly than we can scan the old ones. Add a little | |
| 778 | * slop to try to catch edge cases since nprocs can race. | |
| 779 | * | |
| 780 | * No requirements. | |
| 5bf0d9b5 MD |
781 | */ |
| 782 | void | |
| 783 | allproc_scan(int (*callback)(struct proc *, void *), void *data) | |
| 784 | { | |
| 785 | struct proc *p; | |
| 786 | int r; | |
| c5d9d575 | 787 | int limit = nprocs + ncpus; |
| 5bf0d9b5 | 788 | |
| eb2adbf5 MD |
789 | /* |
| 790 | * proc_token protects the allproc list and PHOLD() prevents the | |
| 791 | * process from being removed from the allproc list or the zombproc | |
| 792 | * list. | |
| 793 | */ | |
| 99ad9bc4 | 794 | lwkt_gettoken(&proc_token); |
| 5bf0d9b5 MD |
795 | LIST_FOREACH(p, &allproc, p_list) { |
| 796 | PHOLD(p); | |
| 5bf0d9b5 | 797 | r = callback(p, data); |
| 5bf0d9b5 MD |
798 | PRELE(p); |
| 799 | if (r < 0) | |
| 800 | break; | |
| c5d9d575 MD |
801 | if (--limit < 0) |
| 802 | break; | |
| 5bf0d9b5 | 803 | } |
| 99ad9bc4 | 804 | lwkt_reltoken(&proc_token); |
| 5bf0d9b5 MD |
805 | } |
| 806 | ||
| 807 | /* | |
| c7e98b2f SS |
808 | * Scan all lwps of processes on the allproc list. The lwp is automatically |
| 809 | * held for the callback. A return value of -1 terminates the loop. | |
| 810 | * | |
| 99ad9bc4 | 811 | * The callback is made with the proces and lwp both held, and proc_token held. |
| eb2adbf5 MD |
812 | * |
| 813 | * No requirements. | |
| c7e98b2f SS |
814 | */ |
| 815 | void | |
| 816 | alllwp_scan(int (*callback)(struct lwp *, void *), void *data) | |
| 817 | { | |
| 818 | struct proc *p; | |
| 819 | struct lwp *lp; | |
| 820 | int r = 0; | |
| 821 | ||
| eb2adbf5 MD |
822 | /* |
| 823 | * proc_token protects the allproc list and PHOLD() prevents the | |
| 824 | * process from being removed from the allproc list or the zombproc | |
| 825 | * list. | |
| 826 | */ | |
| 99ad9bc4 | 827 | lwkt_gettoken(&proc_token); |
| c7e98b2f SS |
828 | LIST_FOREACH(p, &allproc, p_list) { |
| 829 | PHOLD(p); | |
| c7e98b2f SS |
830 | FOREACH_LWP_IN_PROC(lp, p) { |
| 831 | LWPHOLD(lp); | |
| 832 | r = callback(lp, data); | |
| 833 | LWPRELE(lp); | |
| 834 | } | |
| c7e98b2f SS |
835 | PRELE(p); |
| 836 | if (r < 0) | |
| 837 | break; | |
| 838 | } | |
| 99ad9bc4 | 839 | lwkt_reltoken(&proc_token); |
| c7e98b2f SS |
840 | } |
| 841 | ||
| 842 | /* | |
| 5bf0d9b5 MD |
843 | * Scan all processes on the zombproc list. The process is automatically |
| 844 | * held for the callback. A return value of -1 terminates the loop. | |
| 845 | * | |
| 99ad9bc4 MD |
846 | * No requirements. |
| 847 | * The callback is made with the proces held and proc_token held. | |
| 5bf0d9b5 MD |
848 | */ |
| 849 | void | |
| 850 | zombproc_scan(int (*callback)(struct proc *, void *), void *data) | |
| 851 | { | |
| 852 | struct proc *p; | |
| 853 | int r; | |
| 854 | ||
| 99ad9bc4 | 855 | lwkt_gettoken(&proc_token); |
| 5bf0d9b5 MD |
856 | LIST_FOREACH(p, &zombproc, p_list) { |
| 857 | PHOLD(p); | |
| 5bf0d9b5 | 858 | r = callback(p, data); |
| 5bf0d9b5 MD |
859 | PRELE(p); |
| 860 | if (r < 0) | |
| 861 | break; | |
| 862 | } | |
| 99ad9bc4 | 863 | lwkt_reltoken(&proc_token); |
| 5bf0d9b5 MD |
864 | } |
| 865 | ||
| 984263bc MD |
866 | #include "opt_ddb.h" |
| 867 | #ifdef DDB | |
| 868 | #include <ddb/ddb.h> | |
| 869 | ||
| 99ad9bc4 MD |
870 | /* |
| 871 | * Debugging only | |
| 872 | */ | |
| 984263bc MD |
873 | DB_SHOW_COMMAND(pgrpdump, pgrpdump) |
| 874 | { | |
| 1fd87d54 RG |
875 | struct pgrp *pgrp; |
| 876 | struct proc *p; | |
| 877 | int i; | |
| 984263bc MD |
878 | |
| 879 | for (i = 0; i <= pgrphash; i++) { | |
| 880 | if (!LIST_EMPTY(&pgrphashtbl[i])) { | |
| 6ea70f76 | 881 | kprintf("\tindx %d\n", i); |
| 984263bc | 882 | LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { |
| 6ea70f76 | 883 | kprintf( |
| 984263bc MD |
884 | "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", |
| 885 | (void *)pgrp, (long)pgrp->pg_id, | |
| 886 | (void *)pgrp->pg_session, | |
| 887 | pgrp->pg_session->s_count, | |
| 888 | (void *)LIST_FIRST(&pgrp->pg_members)); | |
| 889 | LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { | |
| 6ea70f76 | 890 | kprintf("\t\tpid %ld addr %p pgrp %p\n", |
| 984263bc MD |
891 | (long)p->p_pid, (void *)p, |
| 892 | (void *)p->p_pgrp); | |
| 893 | } | |
| 894 | } | |
| 895 | } | |
| 896 | } | |
| 897 | } | |
| 898 | #endif /* DDB */ | |
| 899 | ||
| 5bf0d9b5 | 900 | /* |
| 6f9db615 | 901 | * Locate a process on the zombie list. Return a process or NULL. |
| 58c2553a MD |
902 | * The returned process will be referenced and the caller must release |
| 903 | * it with PRELE(). | |
| 99ad9bc4 | 904 | * |
| 99ad9bc4 | 905 | * No other requirements. |
| 5bf0d9b5 | 906 | */ |
| 984263bc MD |
907 | struct proc * |
| 908 | zpfind(pid_t pid) | |
| 909 | { | |
| 910 | struct proc *p; | |
| 911 | ||
| 99ad9bc4 MD |
912 | lwkt_gettoken(&proc_token); |
| 913 | LIST_FOREACH(p, &zombproc, p_list) { | |
| 46270ec6 | 914 | if (p->p_pid == pid) { |
| 58c2553a | 915 | PHOLD(p); |
| 46270ec6 | 916 | lwkt_reltoken(&proc_token); |
| 984263bc | 917 | return (p); |
| 46270ec6 | 918 | } |
| 99ad9bc4 MD |
919 | } |
| 920 | lwkt_reltoken(&proc_token); | |
| 984263bc MD |
921 | return (NULL); |
| 922 | } | |
| 923 | ||
| 99ad9bc4 MD |
924 | /* |
| 925 | * The caller must hold proc_token. | |
| 926 | */ | |
| 984263bc | 927 | static int |
| 5dfd06ac | 928 | sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) |
| 984263bc | 929 | { |
| 5dfd06ac SS |
930 | struct kinfo_proc ki; |
| 931 | struct lwp *lp; | |
| fe14f34e | 932 | int skp = 0, had_output = 0; |
| 984263bc | 933 | int error; |
| d9d6cb99 | 934 | |
| fa2217dc | 935 | bzero(&ki, sizeof(ki)); |
| b0c15cdf | 936 | lwkt_gettoken(&p->p_token); |
| 5dfd06ac SS |
937 | fill_kinfo_proc(p, &ki); |
| 938 | if ((flags & KERN_PROC_FLAG_LWP) == 0) | |
| 939 | skp = 1; | |
| fa2217dc | 940 | error = 0; |
| 5dfd06ac | 941 | FOREACH_LWP_IN_PROC(lp, p) { |
| fa2217dc | 942 | LWPHOLD(lp); |
| 5dfd06ac | 943 | fill_kinfo_lwp(lp, &ki.kp_lwp); |
| 5dfd06ac SS |
944 | had_output = 1; |
| 945 | error = SYSCTL_OUT(req, &ki, sizeof(ki)); | |
| fa2217dc | 946 | LWPRELE(lp); |
| 5dfd06ac | 947 | if (error) |
| fa2217dc | 948 | break; |
| 5dfd06ac SS |
949 | if (skp) |
| 950 | break; | |
| 73e4f7b9 | 951 | } |
| b0c15cdf | 952 | lwkt_reltoken(&p->p_token); |
| 5dfd06ac | 953 | /* We need to output at least the proc, even if there is no lwp. */ |
| fa2217dc MD |
954 | if (had_output == 0) { |
| 955 | error = SYSCTL_OUT(req, &ki, sizeof(ki)); | |
| 956 | } | |
| 957 | return (error); | |
| 984263bc MD |
958 | } |
| 959 | ||
| 99ad9bc4 MD |
960 | /* |
| 961 | * The caller must hold proc_token. | |
| 962 | */ | |
| 984263bc | 963 | static int |
| ef02d0e1 TS |
964 | sysctl_out_proc_kthread(struct thread *td, struct sysctl_req *req, int flags) |
| 965 | { | |
| 966 | struct kinfo_proc ki; | |
| 967 | int error; | |
| 968 | ||
| 969 | fill_kinfo_proc_kthread(td, &ki); | |
| 970 | error = SYSCTL_OUT(req, &ki, sizeof(ki)); | |
| 971 | if (error) | |
| 972 | return error; | |
| 973 | return(0); | |
| 974 | } | |
| 975 | ||
| 99ad9bc4 MD |
976 | /* |
| 977 | * No requirements. | |
| 978 | */ | |
| ef02d0e1 | 979 | static int |
| 984263bc MD |
980 | sysctl_kern_proc(SYSCTL_HANDLER_ARGS) |
| 981 | { | |
| 982 | int *name = (int*) arg1; | |
| 5dfd06ac | 983 | int oid = oidp->oid_number; |
| 984263bc | 984 | u_int namelen = arg2; |
| 7cd8fd20 | 985 | struct proc *p; |
| 5dfd06ac | 986 | struct proclist *plist; |
| ef02d0e1 | 987 | struct thread *td; |
| eb2adbf5 | 988 | struct thread *marker; |
| 5dfd06ac | 989 | int doingzomb, flags = 0; |
| 984263bc | 990 | int error = 0; |
| ef02d0e1 | 991 | int n; |
| 630ccdeb | 992 | int origcpu; |
| 41c20dac | 993 | struct ucred *cr1 = curproc->p_ucred; |
| 984263bc | 994 | |
| 5dfd06ac SS |
995 | flags = oid & KERN_PROC_FLAGMASK; |
| 996 | oid &= ~KERN_PROC_FLAGMASK; | |
| 997 | ||
| 998 | if ((oid == KERN_PROC_ALL && namelen != 0) || | |
| eb2adbf5 | 999 | (oid != KERN_PROC_ALL && namelen != 1)) { |
| 5dfd06ac | 1000 | return (EINVAL); |
| eb2adbf5 | 1001 | } |
| 5dfd06ac | 1002 | |
| eb2adbf5 MD |
1003 | /* |
| 1004 | * proc_token protects the allproc list and PHOLD() prevents the | |
| 1005 | * process from being removed from the allproc list or the zombproc | |
| 1006 | * list. | |
| 1007 | */ | |
| 99ad9bc4 | 1008 | lwkt_gettoken(&proc_token); |
| 5dfd06ac | 1009 | if (oid == KERN_PROC_PID) { |
| 58c2553a | 1010 | p = pfindn((pid_t)name[0]); |
| 99ad9bc4 MD |
1011 | if (p == NULL) |
| 1012 | goto post_threads; | |
| 41c20dac | 1013 | if (!PRISON_CHECK(cr1, p->p_ucred)) |
| 99ad9bc4 | 1014 | goto post_threads; |
| e7093b07 | 1015 | PHOLD(p); |
| 5dfd06ac | 1016 | error = sysctl_out_proc(p, req, flags); |
| e7093b07 | 1017 | PRELE(p); |
| 99ad9bc4 | 1018 | goto post_threads; |
| 984263bc | 1019 | } |
| 5dfd06ac | 1020 | |
| 984263bc MD |
1021 | if (!req->oldptr) { |
| 1022 | /* overestimate by 5 procs */ | |
| 1023 | error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); | |
| 1024 | if (error) | |
| 99ad9bc4 | 1025 | goto post_threads; |
| 984263bc | 1026 | } |
| 5dfd06ac SS |
1027 | for (doingzomb = 0; doingzomb <= 1; doingzomb++) { |
| 1028 | if (doingzomb) | |
| 1029 | plist = &zombproc; | |
| 984263bc | 1030 | else |
| 5dfd06ac | 1031 | plist = &allproc; |
| 7cd8fd20 | 1032 | LIST_FOREACH(p, plist, p_list) { |
| 984263bc MD |
1033 | /* |
| 1034 | * Show a user only their processes. | |
| 1035 | */ | |
| 41c20dac | 1036 | if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred)) |
| 984263bc MD |
1037 | continue; |
| 1038 | /* | |
| 1039 | * Skip embryonic processes. | |
| 1040 | */ | |
| 1041 | if (p->p_stat == SIDL) | |
| 1042 | continue; | |
| 1043 | /* | |
| 1044 | * TODO - make more efficient (see notes below). | |
| 1045 | * do by session. | |
| 1046 | */ | |
| 5dfd06ac | 1047 | switch (oid) { |
| 984263bc MD |
1048 | case KERN_PROC_PGRP: |
| 1049 | /* could do this by traversing pgrp */ | |
| 1050 | if (p->p_pgrp == NULL || | |
| 1051 | p->p_pgrp->pg_id != (pid_t)name[0]) | |
| 1052 | continue; | |
| 1053 | break; | |
| 1054 | ||
| 1055 | case KERN_PROC_TTY: | |
| 4643740a | 1056 | if ((p->p_flags & P_CONTROLT) == 0 || |
| 984263bc MD |
1057 | p->p_session == NULL || |
| 1058 | p->p_session->s_ttyp == NULL || | |
| 1059 | dev2udev(p->p_session->s_ttyp->t_dev) != | |
| 1060 | (udev_t)name[0]) | |
| 1061 | continue; | |
| 1062 | break; | |
| 1063 | ||
| 1064 | case KERN_PROC_UID: | |
| 1065 | if (p->p_ucred == NULL || | |
| 1066 | p->p_ucred->cr_uid != (uid_t)name[0]) | |
| 1067 | continue; | |
| 1068 | break; | |
| 1069 | ||
| 1070 | case KERN_PROC_RUID: | |
| 1071 | if (p->p_ucred == NULL || | |
| 41c20dac | 1072 | p->p_ucred->cr_ruid != (uid_t)name[0]) |
| 984263bc MD |
1073 | continue; |
| 1074 | break; | |
| 1075 | } | |
| 1076 | ||
| 41c20dac | 1077 | if (!PRISON_CHECK(cr1, p->p_ucred)) |
| 984263bc | 1078 | continue; |
| c008d3ad | 1079 | PHOLD(p); |
| 5dfd06ac | 1080 | error = sysctl_out_proc(p, req, flags); |
| c008d3ad | 1081 | PRELE(p); |
| 984263bc | 1082 | if (error) |
| 99ad9bc4 | 1083 | goto post_threads; |
| 984263bc MD |
1084 | } |
| 1085 | } | |
| 630ccdeb MD |
1086 | |
| 1087 | /* | |
| 1088 | * Iterate over all active cpus and scan their thread list. Start | |
| 1089 | * with the next logical cpu and end with our original cpu. We | |
| 1090 | * migrate our own thread to each target cpu in order to safely scan | |
| 1091 | * its thread list. In the last loop we migrate back to our original | |
| 1092 | * cpu. | |
| 1093 | */ | |
| 1094 | origcpu = mycpu->gd_cpuid; | |
| 56c703bd JS |
1095 | if (!ps_showallthreads || jailed(cr1)) |
| 1096 | goto post_threads; | |
| 99ad9bc4 | 1097 | |
| eb2adbf5 | 1098 | marker = kmalloc(sizeof(struct thread), M_TEMP, M_WAITOK|M_ZERO); |
| 42b33868 | 1099 | marker->td_flags = TDF_MARKER; |
| eb2adbf5 MD |
1100 | error = 0; |
| 1101 | ||
| 56c703bd | 1102 | for (n = 1; n <= ncpus; ++n) { |
| 630ccdeb MD |
1103 | globaldata_t rgd; |
| 1104 | int nid; | |
| 1105 | ||
| 1106 | nid = (origcpu + n) % ncpus; | |
| da23a592 | 1107 | if ((smp_active_mask & CPUMASK(nid)) == 0) |
| 630ccdeb MD |
1108 | continue; |
| 1109 | rgd = globaldata_find(nid); | |
| 1110 | lwkt_setcpu_self(rgd); | |
| 630ccdeb | 1111 | |
| eb2adbf5 MD |
1112 | crit_enter(); |
| 1113 | TAILQ_INSERT_TAIL(&rgd->gd_tdallq, marker, td_allq); | |
| eb2adbf5 MD |
1114 | |
| 1115 | while ((td = TAILQ_PREV(marker, lwkt_queue, td_allq)) != NULL) { | |
| eb2adbf5 MD |
1116 | TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq); |
| 1117 | TAILQ_INSERT_BEFORE(td, marker, td_allq); | |
| 42b33868 | 1118 | if (td->td_flags & TDF_MARKER) |
| eb2adbf5 | 1119 | continue; |
| 42b33868 | 1120 | if (td->td_proc) |
| eb2adbf5 | 1121 | continue; |
| 42b33868 MD |
1122 | |
| 1123 | lwkt_hold(td); | |
| 1124 | crit_exit(); | |
| eb2adbf5 | 1125 | |
| ef02d0e1 | 1126 | switch (oid) { |
| 73e4f7b9 MD |
1127 | case KERN_PROC_PGRP: |
| 1128 | case KERN_PROC_TTY: | |
| 1129 | case KERN_PROC_UID: | |
| 1130 | case KERN_PROC_RUID: | |
| eb2adbf5 | 1131 | break; |
| 73e4f7b9 | 1132 | default: |
| eb2adbf5 MD |
1133 | error = sysctl_out_proc_kthread(td, req, |
| 1134 | doingzomb); | |
| 73e4f7b9 MD |
1135 | break; |
| 1136 | } | |
| 73e4f7b9 | 1137 | lwkt_rele(td); |
| 42b33868 | 1138 | crit_enter(); |
| 73e4f7b9 | 1139 | if (error) |
| eb2adbf5 | 1140 | break; |
| 73e4f7b9 | 1141 | } |
| eb2adbf5 MD |
1142 | TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq); |
| 1143 | crit_exit(); | |
| 1144 | ||
| 1145 | if (error) | |
| 1146 | break; | |
| 73e4f7b9 | 1147 | } |
| eb2adbf5 MD |
1148 | kfree(marker, M_TEMP); |
| 1149 | ||
| 56c703bd | 1150 | post_threads: |
| 99ad9bc4 MD |
1151 | lwkt_reltoken(&proc_token); |
| 1152 | return (error); | |
| 984263bc MD |
1153 | } |
| 1154 | ||
| 1155 | /* | |
| 1156 | * This sysctl allows a process to retrieve the argument list or process | |
| 1157 | * title for another process without groping around in the address space | |
| 1158 | * of the other process. It also allow a process to set its own "process | |
| 1159 | * title to a string of its own choice. | |
| 99ad9bc4 MD |
1160 | * |
| 1161 | * No requirements. | |
| 984263bc MD |
1162 | */ |
| 1163 | static int | |
| 1164 | sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) | |
| 1165 | { | |
| 1166 | int *name = (int*) arg1; | |
| 1167 | u_int namelen = arg2; | |
| 1168 | struct proc *p; | |
| 19bfc8ab | 1169 | struct pargs *opa; |
| 984263bc MD |
1170 | struct pargs *pa; |
| 1171 | int error = 0; | |
| 41c20dac | 1172 | struct ucred *cr1 = curproc->p_ucred; |
| 984263bc MD |
1173 | |
| 1174 | if (namelen != 1) | |
| 1175 | return (EINVAL); | |
| 1176 | ||
| 46fb7ae4 | 1177 | p = pfind((pid_t)name[0]); |
| 99ad9bc4 | 1178 | if (p == NULL) |
| 46fb7ae4 | 1179 | goto done; |
| 19bfc8ab | 1180 | lwkt_gettoken(&p->p_token); |
| 984263bc | 1181 | |
| 41c20dac | 1182 | if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred)) |
| 99ad9bc4 | 1183 | goto done; |
| 984263bc | 1184 | |
| 99ad9bc4 MD |
1185 | if (req->newptr && curproc != p) { |
| 1186 | error = EPERM; | |
| 1187 | goto done; | |
| 1188 | } | |
| 46fb7ae4 MD |
1189 | if (req->oldptr && (pa = p->p_args) != NULL) { |
| 1190 | refcount_acquire(&pa->ar_ref); | |
| 1191 | error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); | |
| 1192 | if (refcount_release(&pa->ar_ref)) | |
| 1193 | kfree(pa, M_PARGS); | |
| 99ad9bc4 | 1194 | } |
| 19bfc8ab | 1195 | if (req->newptr == NULL) |
| 99ad9bc4 | 1196 | goto done; |
| 984263bc | 1197 | |
| 99ad9bc4 | 1198 | if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) { |
| 99ad9bc4 MD |
1199 | goto done; |
| 1200 | } | |
| 984263bc | 1201 | |
| 19bfc8ab MD |
1202 | pa = kmalloc(sizeof(struct pargs) + req->newlen, M_PARGS, M_WAITOK); |
| 1203 | refcount_init(&pa->ar_ref, 1); | |
| 984263bc MD |
1204 | pa->ar_length = req->newlen; |
| 1205 | error = SYSCTL_IN(req, pa->ar_args, req->newlen); | |
| 19bfc8ab MD |
1206 | if (error) { |
| 1207 | kfree(pa, M_PARGS); | |
| 1208 | goto done; | |
| 1209 | } | |
| 1210 | ||
| c7506e9d MD |
1211 | |
| 1212 | /* | |
| 1213 | * Replace p_args with the new pa. p_args may have previously | |
| 1214 | * been NULL. | |
| 1215 | */ | |
| 19bfc8ab MD |
1216 | opa = p->p_args; |
| 1217 | p->p_args = pa; | |
| 1218 | ||
| c7506e9d MD |
1219 | if (opa) { |
| 1220 | KKASSERT(opa->ar_ref > 0); | |
| 1221 | if (refcount_release(&opa->ar_ref)) { | |
| 1222 | kfree(opa, M_PARGS); | |
| 1223 | /* opa = NULL; */ | |
| 1224 | } | |
| 19bfc8ab | 1225 | } |
| 99ad9bc4 | 1226 | done: |
| 46fb7ae4 MD |
1227 | if (p) { |
| 1228 | lwkt_reltoken(&p->p_token); | |
| 1229 | PRELE(p); | |
| 1230 | } | |
| 984263bc MD |
1231 | return (error); |
| 1232 | } | |
| 1233 | ||
| a45611c5 AH |
1234 | static int |
| 1235 | sysctl_kern_proc_cwd(SYSCTL_HANDLER_ARGS) | |
| 1236 | { | |
| 1237 | int *name = (int*) arg1; | |
| 1238 | u_int namelen = arg2; | |
| 1239 | struct proc *p; | |
| 1240 | int error = 0; | |
| 1241 | char *fullpath, *freepath; | |
| 1242 | struct ucred *cr1 = curproc->p_ucred; | |
| 1243 | ||
| 1244 | if (namelen != 1) | |
| 1245 | return (EINVAL); | |
| 1246 | ||
| 46fb7ae4 | 1247 | p = pfind((pid_t)name[0]); |
| a45611c5 AH |
1248 | if (p == NULL) |
| 1249 | goto done; | |
| 46fb7ae4 | 1250 | lwkt_gettoken(&p->p_token); |
| a45611c5 AH |
1251 | |
| 1252 | /* | |
| 1253 | * If we are not allowed to see other args, we certainly shouldn't | |
| 1254 | * get the cwd either. Also check the usual trespassing. | |
| 1255 | */ | |
| 1256 | if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred)) | |
| 1257 | goto done; | |
| 1258 | ||
| 46fb7ae4 MD |
1259 | if (req->oldptr && p->p_fd != NULL && p->p_fd->fd_ncdir.ncp) { |
| 1260 | struct nchandle nch; | |
| 1261 | ||
| 1262 | cache_copy(&p->p_fd->fd_ncdir, &nch); | |
| 1263 | error = cache_fullpath(p, &nch, &fullpath, &freepath, 0); | |
| 1264 | cache_drop(&nch); | |
| a45611c5 AH |
1265 | if (error) |
| 1266 | goto done; | |
| 1267 | error = SYSCTL_OUT(req, fullpath, strlen(fullpath) + 1); | |
| 1268 | kfree(freepath, M_TEMP); | |
| 1269 | } | |
| 1270 | ||
| a45611c5 | 1271 | done: |
| 46fb7ae4 MD |
1272 | if (p) { |
| 1273 | lwkt_reltoken(&p->p_token); | |
| 1274 | PRELE(p); | |
| 1275 | } | |
| a45611c5 AH |
1276 | return (error); |
| 1277 | } | |
| 1278 | ||
| 984263bc MD |
1279 | SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); |
| 1280 | ||
| 1281 | SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, | |
| 1282 | 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); | |
| 1283 | ||
| 1284 | SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, | |
| 1285 | sysctl_kern_proc, "Process table"); | |
| 1286 | ||
| 1287 | SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, | |
| 1288 | sysctl_kern_proc, "Process table"); | |
| 1289 | ||
| 1290 | SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, | |
| 1291 | sysctl_kern_proc, "Process table"); | |
| 1292 | ||
| 1293 | SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, | |
| 1294 | sysctl_kern_proc, "Process table"); | |
| 1295 | ||
| 1296 | SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, | |
| 1297 | sysctl_kern_proc, "Process table"); | |
| 1298 | ||
| 5dfd06ac SS |
1299 | SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD, |
| 1300 | sysctl_kern_proc, "Process table"); | |
| 1301 | ||
| 1302 | SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD, | |
| 1303 | sysctl_kern_proc, "Process table"); | |
| 1304 | ||
| 1305 | SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD, | |
| 1306 | sysctl_kern_proc, "Process table"); | |
| 1307 | ||
| 1308 | SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD, | |
| 1309 | sysctl_kern_proc, "Process table"); | |
| 1310 | ||
| 1311 | SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD, | |
| 1312 | sysctl_kern_proc, "Process table"); | |
| 1313 | ||
| 1314 | SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD, | |
| 1315 | sysctl_kern_proc, "Process table"); | |
| 1316 | ||
| 984263bc MD |
1317 | SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY, |
| 1318 | sysctl_kern_proc_args, "Process argument list"); | |
| a45611c5 AH |
1319 | |
| 1320 | SYSCTL_NODE(_kern_proc, KERN_PROC_CWD, cwd, CTLFLAG_RD | CTLFLAG_ANYBODY, | |
| 1321 | sysctl_kern_proc_cwd, "Process argument list"); |