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