kernel - MPSAFE work - fix bugs in recent MPSAFE work.
[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
5bf0d9b5 59#include <sys/spinlock2.h>
684a93c4 60#include <sys/mplock2.h>
5bf0d9b5 61
984263bc
MD
62static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
63MALLOC_DEFINE(M_SESSION, "session", "session header");
fb2a331e 64MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
f6c36234 65MALLOC_DEFINE(M_LWP, "lwp", "lwp structures");
984263bc
MD
66MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
67
28c57d20 68int ps_showallprocs = 1;
73e4f7b9 69static int ps_showallthreads = 1;
43a0f7ae
MS
70SYSCTL_INT(_security, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
71 &ps_showallprocs, 0,
72 "Unprivileged processes can see proccesses with different UID/GID");
73SYSCTL_INT(_security, OID_AUTO, ps_showallthreads, CTLFLAG_RW,
74 &ps_showallthreads, 0,
75 "Unprivileged processes can see kernel threads");
984263bc 76
51e64ff2
MD
77static void pgdelete(struct pgrp *);
78static void orphanpg(struct pgrp *pg);
79static pid_t proc_getnewpid_locked(int random_offset);
984263bc
MD
80
81/*
82 * Other process lists
83 */
84struct pidhashhead *pidhashtbl;
85u_long pidhash;
86struct pgrphashhead *pgrphashtbl;
87u_long pgrphash;
88struct proclist allproc;
89struct proclist zombproc;
984263bc 90
51e64ff2
MD
91/*
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 */
99static int randompid = 0;
100
99ad9bc4
MD
101/*
102 * No requirements.
103 */
51e64ff2
MD
104static int
105sysctl_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
123SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW,
124 0, 0, sysctl_kern_randompid, "I", "Random PID modulus");
125
984263bc
MD
126/*
127 * Initialize global process hashing structures.
99ad9bc4
MD
128 *
129 * Called from the low level boot code only.
984263bc
MD
130 */
131void
77153250 132procinit(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/*
143 * Is p an inferior of the current process?
99ad9bc4
MD
144 *
145 * No requirements.
146 * The caller must hold proc_token if the caller wishes a stable result.
984263bc
MD
147 */
148int
77153250 149inferior(struct proc *p)
984263bc 150{
99ad9bc4
MD
151 lwkt_gettoken(&proc_token);
152 while (p != curproc) {
153 if (p->p_pid == 0) {
154 lwkt_reltoken(&proc_token);
984263bc 155 return (0);
99ad9bc4
MD
156 }
157 p = p->p_pptr;
158 }
159 lwkt_reltoken(&proc_token);
984263bc
MD
160 return (1);
161}
162
163/*
164 * Locate a process by number
99ad9bc4
MD
165 *
166 * XXX TODO - change API to PHOLD() the returned process ?
167 *
168 * No requirements.
169 * The caller must hold proc_token if the caller wishes a stable result.
984263bc
MD
170 */
171struct proc *
77153250 172pfind(pid_t pid)
984263bc 173{
1fd87d54 174 struct proc *p;
984263bc 175
99ad9bc4 176 lwkt_gettoken(&proc_token);
5bf0d9b5 177 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
99ad9bc4
MD
178 if (p->p_pid == pid) {
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/*
188 * Locate a process group by number
99ad9bc4
MD
189 *
190 * No requirements.
191 * The caller must hold proc_token if the caller wishes a stable result.
984263bc
MD
192 */
193struct pgrp *
77153250 194pgfind(pid_t pgid)
984263bc 195{
1fd87d54 196 struct pgrp *pgrp;
984263bc 197
99ad9bc4 198 lwkt_gettoken(&proc_token);
5bf0d9b5 199 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
46270ec6
MD
200 if (pgrp->pg_id == pgid) {
201 lwkt_reltoken(&proc_token);
984263bc 202 return (pgrp);
46270ec6 203 }
5bf0d9b5 204 }
99ad9bc4 205 lwkt_reltoken(&proc_token);
984263bc
MD
206 return (NULL);
207}
208
209/*
210 * Move p to a new or existing process group (and session)
99ad9bc4
MD
211 *
212 * No requirements.
984263bc
MD
213 */
214int
77153250 215enterpgrp(struct proc *p, pid_t pgid, int mksess)
984263bc 216{
99ad9bc4
MD
217 struct pgrp *pgrp;
218 int error;
219
220 lwkt_gettoken(&proc_token);
221 pgrp = pgfind(pgid);
984263bc
MD
222
223 KASSERT(pgrp == NULL || !mksess,
99ad9bc4 224 ("enterpgrp: setsid into non-empty pgrp"));
984263bc 225 KASSERT(!SESS_LEADER(p),
99ad9bc4 226 ("enterpgrp: session leader attempted setpgrp"));
984263bc
MD
227
228 if (pgrp == NULL) {
229 pid_t savepid = p->p_pid;
230 struct proc *np;
231 /*
232 * new process group
233 */
234 KASSERT(p->p_pid == pgid,
99ad9bc4
MD
235 ("enterpgrp: new pgrp and pid != pgid"));
236 if ((np = pfind(savepid)) == NULL || np != p) {
237 error = ESRCH;
238 goto fatal;
239 }
240 MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp),
241 M_PGRP, M_WAITOK);
984263bc 242 if (mksess) {
1fd87d54 243 struct session *sess;
984263bc
MD
244
245 /*
246 * new session
247 */
248 MALLOC(sess, struct session *, sizeof(struct session),
99ad9bc4 249 M_SESSION, M_WAITOK);
984263bc
MD
250 sess->s_leader = p;
251 sess->s_sid = p->p_pid;
252 sess->s_count = 1;
253 sess->s_ttyvp = NULL;
254 sess->s_ttyp = NULL;
255 bcopy(p->p_session->s_login, sess->s_login,
99ad9bc4 256 sizeof(sess->s_login));
984263bc
MD
257 p->p_flag &= ~P_CONTROLT;
258 pgrp->pg_session = sess;
259 KASSERT(p == curproc,
99ad9bc4 260 ("enterpgrp: mksession and p != curproc"));
984263bc
MD
261 } else {
262 pgrp->pg_session = p->p_session;
8b90699b 263 sess_hold(pgrp->pg_session);
984263bc
MD
264 }
265 pgrp->pg_id = pgid;
266 LIST_INIT(&pgrp->pg_members);
267 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
268 pgrp->pg_jobc = 0;
269 SLIST_INIT(&pgrp->pg_sigiolst);
167e6ecb 270 lockinit(&pgrp->pg_lock, "pgwt", 0, 0);
99ad9bc4
MD
271 } else if (pgrp == p->p_pgrp) {
272 goto done;
273 }
984263bc
MD
274
275 /*
276 * Adjust eligibility of affected pgrps to participate in job control.
277 * Increment eligibility counts before decrementing, otherwise we
278 * could reach 0 spuriously during the first call.
279 */
280 fixjobc(p, pgrp, 1);
281 fixjobc(p, p->p_pgrp, 0);
282
283 LIST_REMOVE(p, p_pglist);
284 if (LIST_EMPTY(&p->p_pgrp->pg_members))
285 pgdelete(p->p_pgrp);
286 p->p_pgrp = pgrp;
287 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
99ad9bc4
MD
288done:
289 error = 0;
290fatal:
291 lwkt_reltoken(&proc_token);
292 return (error);
984263bc
MD
293}
294
295/*
99ad9bc4
MD
296 * Remove process from process group
297 *
298 * No requirements.
984263bc
MD
299 */
300int
77153250 301leavepgrp(struct proc *p)
984263bc 302{
99ad9bc4 303 lwkt_gettoken(&proc_token);
984263bc
MD
304 LIST_REMOVE(p, p_pglist);
305 if (LIST_EMPTY(&p->p_pgrp->pg_members))
306 pgdelete(p->p_pgrp);
99ad9bc4
MD
307 p->p_pgrp = NULL;
308 lwkt_reltoken(&proc_token);
984263bc
MD
309 return (0);
310}
311
312/*
99ad9bc4
MD
313 * Delete a process group
314 *
315 * The caller must hold proc_token.
984263bc
MD
316 */
317static void
77153250 318pgdelete(struct pgrp *pgrp)
984263bc 319{
984263bc
MD
320 /*
321 * Reset any sigio structures pointing to us as a result of
322 * F_SETOWN with our pgid.
323 */
324 funsetownlst(&pgrp->pg_sigiolst);
325
326 if (pgrp->pg_session->s_ttyp != NULL &&
327 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
328 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
329 LIST_REMOVE(pgrp, pg_hash);
8b90699b 330 sess_rele(pgrp->pg_session);
efda3bd0 331 kfree(pgrp, M_PGRP);
8b90699b
MD
332}
333
334/*
335 * Adjust the ref count on a session structure. When the ref count falls to
336 * zero the tty is disassociated from the session and the session structure
337 * is freed. Note that tty assocation is not itself ref-counted.
99ad9bc4
MD
338 *
339 * No requirements.
8b90699b
MD
340 */
341void
342sess_hold(struct session *sp)
343{
99ad9bc4 344 lwkt_gettoken(&tty_token);
8b90699b 345 ++sp->s_count;
99ad9bc4 346 lwkt_reltoken(&tty_token);
8b90699b
MD
347}
348
99ad9bc4
MD
349/*
350 * No requirements.
351 */
8b90699b
MD
352void
353sess_rele(struct session *sp)
354{
355 KKASSERT(sp->s_count > 0);
99ad9bc4 356 lwkt_gettoken(&tty_token);
8b90699b
MD
357 if (--sp->s_count == 0) {
358 if (sp->s_ttyp && sp->s_ttyp->t_session) {
359#ifdef TTY_DO_FULL_CLOSE
360 /* FULL CLOSE, see ttyclearsession() */
361 KKASSERT(sp->s_ttyp->t_session == sp);
362 sp->s_ttyp->t_session = NULL;
363#else
364 /* HALF CLOSE, see ttyclearsession() */
365 if (sp->s_ttyp->t_session == sp)
366 sp->s_ttyp->t_session = NULL;
367#endif
368 }
efda3bd0 369 kfree(sp, M_SESSION);
8b90699b 370 }
99ad9bc4 371 lwkt_reltoken(&tty_token);
984263bc
MD
372}
373
374/*
375 * Adjust pgrp jobc counters when specified process changes process group.
376 * We count the number of processes in each process group that "qualify"
377 * the group for terminal job control (those with a parent in a different
378 * process group of the same session). If that count reaches zero, the
379 * process group becomes orphaned. Check both the specified process'
380 * process group and that of its children.
381 * entering == 0 => p is leaving specified group.
382 * entering == 1 => p is entering specified group.
99ad9bc4
MD
383 *
384 * No requirements.
984263bc
MD
385 */
386void
77153250 387fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
984263bc 388{
1fd87d54 389 struct pgrp *hispgrp;
99ad9bc4 390 struct session *mysession;
984263bc
MD
391
392 /*
393 * Check p's parent to see whether p qualifies its own process
394 * group; if so, adjust count for p's process group.
395 */
99ad9bc4
MD
396 lwkt_gettoken(&proc_token);
397 mysession = pgrp->pg_session;
984263bc
MD
398 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
399 hispgrp->pg_session == mysession) {
400 if (entering)
401 pgrp->pg_jobc++;
402 else if (--pgrp->pg_jobc == 0)
403 orphanpg(pgrp);
404 }
405
406 /*
407 * Check this process' children to see whether they qualify
408 * their process groups; if so, adjust counts for children's
409 * process groups.
410 */
99ad9bc4 411 LIST_FOREACH(p, &p->p_children, p_sibling) {
984263bc
MD
412 if ((hispgrp = p->p_pgrp) != pgrp &&
413 hispgrp->pg_session == mysession &&
416d05d7 414 p->p_stat != SZOMB) {
984263bc
MD
415 if (entering)
416 hispgrp->pg_jobc++;
417 else if (--hispgrp->pg_jobc == 0)
418 orphanpg(hispgrp);
419 }
99ad9bc4
MD
420 }
421 lwkt_reltoken(&proc_token);
984263bc
MD
422}
423
424/*
425 * A process group has become orphaned;
426 * if there are any stopped processes in the group,
427 * hang-up all process in that group.
99ad9bc4
MD
428 *
429 * The caller must hold proc_token.
984263bc
MD
430 */
431static void
77153250 432orphanpg(struct pgrp *pg)
984263bc 433{
1fd87d54 434 struct proc *p;
984263bc
MD
435
436 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
164b8401 437 if (p->p_stat == SSTOP) {
984263bc 438 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
84204577
MD
439 ksignal(p, SIGHUP);
440 ksignal(p, SIGCONT);
984263bc
MD
441 }
442 return;
443 }
444 }
445}
446
51e64ff2
MD
447/*
448 * Add a new process to the allproc list and the PID hash. This
449 * also assigns a pid to the new process.
450 *
99ad9bc4 451 * No requirements.
51e64ff2
MD
452 */
453void
454proc_add_allproc(struct proc *p)
455{
456 int random_offset;
457
458 if ((random_offset = randompid) != 0) {
459 get_mplock();
0ced1954 460 random_offset = karc4random() % random_offset;
51e64ff2
MD
461 rel_mplock();
462 }
463
99ad9bc4 464 lwkt_gettoken(&proc_token);
51e64ff2
MD
465 p->p_pid = proc_getnewpid_locked(random_offset);
466 LIST_INSERT_HEAD(&allproc, p, p_list);
467 LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash);
99ad9bc4 468 lwkt_reltoken(&proc_token);
51e64ff2
MD
469}
470
471/*
472 * Calculate a new process pid. This function is integrated into
473 * proc_add_allproc() to guarentee that the new pid is not reused before
474 * the new process can be added to the allproc list.
475 *
99ad9bc4 476 * The caller must hold proc_token.
51e64ff2
MD
477 */
478static
479pid_t
480proc_getnewpid_locked(int random_offset)
481{
482 static pid_t nextpid;
483 static pid_t pidchecked;
484 struct proc *p;
485
486 /*
487 * Find an unused process ID. We remember a range of unused IDs
488 * ready to use (from nextpid+1 through pidchecked-1).
489 */
490 nextpid = nextpid + 1 + random_offset;
491retry:
492 /*
493 * If the process ID prototype has wrapped around,
494 * restart somewhat above 0, as the low-numbered procs
495 * tend to include daemons that don't exit.
496 */
497 if (nextpid >= PID_MAX) {
498 nextpid = nextpid % PID_MAX;
499 if (nextpid < 100)
500 nextpid += 100;
501 pidchecked = 0;
502 }
503 if (nextpid >= pidchecked) {
504 int doingzomb = 0;
505
506 pidchecked = PID_MAX;
507 /*
508 * Scan the active and zombie procs to check whether this pid
509 * is in use. Remember the lowest pid that's greater
510 * than nextpid, so we can avoid checking for a while.
511 */
512 p = LIST_FIRST(&allproc);
513again:
514 for (; p != 0; p = LIST_NEXT(p, p_list)) {
515 while (p->p_pid == nextpid ||
516 p->p_pgrp->pg_id == nextpid ||
517 p->p_session->s_sid == nextpid) {
518 nextpid++;
519 if (nextpid >= pidchecked)
520 goto retry;
521 }
522 if (p->p_pid > nextpid && pidchecked > p->p_pid)
523 pidchecked = p->p_pid;
524 if (p->p_pgrp->pg_id > nextpid &&
525 pidchecked > p->p_pgrp->pg_id)
526 pidchecked = p->p_pgrp->pg_id;
527 if (p->p_session->s_sid > nextpid &&
528 pidchecked > p->p_session->s_sid)
529 pidchecked = p->p_session->s_sid;
530 }
531 if (!doingzomb) {
532 doingzomb = 1;
533 p = LIST_FIRST(&zombproc);
534 goto again;
535 }
536 }
537 return(nextpid);
538}
539
5bf0d9b5
MD
540/*
541 * Called from exit1 to remove a process from the allproc
542 * list and move it to the zombie list.
543 *
99ad9bc4 544 * No requirements.
5bf0d9b5
MD
545 */
546void
547proc_move_allproc_zombie(struct proc *p)
548{
99ad9bc4 549 lwkt_gettoken(&proc_token);
5bf0d9b5 550 while (p->p_lock) {
5bf0d9b5 551 tsleep(p, 0, "reap1", hz / 10);
5bf0d9b5
MD
552 }
553 LIST_REMOVE(p, p_list);
554 LIST_INSERT_HEAD(&zombproc, p, p_list);
555 LIST_REMOVE(p, p_hash);
416d05d7 556 p->p_stat = SZOMB;
99ad9bc4 557 lwkt_reltoken(&proc_token);
8c72e3d5 558 dsched_exit_proc(p);
5bf0d9b5
MD
559}
560
561/*
562 * This routine is called from kern_wait() and will remove the process
563 * from the zombie list and the sibling list. This routine will block
564 * if someone has a lock on the proces (p_lock).
565 *
99ad9bc4 566 * No requirements.
5bf0d9b5
MD
567 */
568void
569proc_remove_zombie(struct proc *p)
570{
99ad9bc4 571 lwkt_gettoken(&proc_token);
5bf0d9b5 572 while (p->p_lock) {
5bf0d9b5 573 tsleep(p, 0, "reap1", hz / 10);
5bf0d9b5
MD
574 }
575 LIST_REMOVE(p, p_list); /* off zombproc */
576 LIST_REMOVE(p, p_sibling);
99ad9bc4 577 lwkt_reltoken(&proc_token);
5bf0d9b5
MD
578}
579
580/*
581 * Scan all processes on the allproc list. The process is automatically
582 * held for the callback. A return value of -1 terminates the loop.
583 *
99ad9bc4
MD
584 * No requirements.
585 * The callback is made with the process held and proc_token held.
5bf0d9b5
MD
586 */
587void
588allproc_scan(int (*callback)(struct proc *, void *), void *data)
589{
590 struct proc *p;
591 int r;
592
99ad9bc4 593 lwkt_gettoken(&proc_token);
5bf0d9b5
MD
594 LIST_FOREACH(p, &allproc, p_list) {
595 PHOLD(p);
5bf0d9b5 596 r = callback(p, data);
5bf0d9b5
MD
597 PRELE(p);
598 if (r < 0)
599 break;
600 }
99ad9bc4 601 lwkt_reltoken(&proc_token);
5bf0d9b5
MD
602}
603
c7e98b2f
SS
604/*
605 * Scan all lwps of processes on the allproc list. The lwp is automatically
606 * held for the callback. A return value of -1 terminates the loop.
607 *
99ad9bc4
MD
608 * No requirements.
609 * The callback is made with the proces and lwp both held, and proc_token held.
c7e98b2f
SS
610 */
611void
612alllwp_scan(int (*callback)(struct lwp *, void *), void *data)
613{
614 struct proc *p;
615 struct lwp *lp;
616 int r = 0;
617
99ad9bc4 618 lwkt_gettoken(&proc_token);
c7e98b2f
SS
619 LIST_FOREACH(p, &allproc, p_list) {
620 PHOLD(p);
c7e98b2f
SS
621 FOREACH_LWP_IN_PROC(lp, p) {
622 LWPHOLD(lp);
623 r = callback(lp, data);
624 LWPRELE(lp);
625 }
c7e98b2f
SS
626 PRELE(p);
627 if (r < 0)
628 break;
629 }
99ad9bc4 630 lwkt_reltoken(&proc_token);
c7e98b2f
SS
631}
632
5bf0d9b5
MD
633/*
634 * Scan all processes on the zombproc list. The process is automatically
635 * held for the callback. A return value of -1 terminates the loop.
636 *
99ad9bc4
MD
637 * No requirements.
638 * The callback is made with the proces held and proc_token held.
5bf0d9b5
MD
639 */
640void
641zombproc_scan(int (*callback)(struct proc *, void *), void *data)
642{
643 struct proc *p;
644 int r;
645
99ad9bc4 646 lwkt_gettoken(&proc_token);
5bf0d9b5
MD
647 LIST_FOREACH(p, &zombproc, p_list) {
648 PHOLD(p);
5bf0d9b5 649 r = callback(p, data);
5bf0d9b5
MD
650 PRELE(p);
651 if (r < 0)
652 break;
653 }
99ad9bc4 654 lwkt_reltoken(&proc_token);
5bf0d9b5
MD
655}
656
984263bc
MD
657#include "opt_ddb.h"
658#ifdef DDB
659#include <ddb/ddb.h>
660
99ad9bc4
MD
661/*
662 * Debugging only
663 */
984263bc
MD
664DB_SHOW_COMMAND(pgrpdump, pgrpdump)
665{
1fd87d54
RG
666 struct pgrp *pgrp;
667 struct proc *p;
668 int i;
984263bc
MD
669
670 for (i = 0; i <= pgrphash; i++) {
671 if (!LIST_EMPTY(&pgrphashtbl[i])) {
6ea70f76 672 kprintf("\tindx %d\n", i);
984263bc 673 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
6ea70f76 674 kprintf(
984263bc
MD
675 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
676 (void *)pgrp, (long)pgrp->pg_id,
677 (void *)pgrp->pg_session,
678 pgrp->pg_session->s_count,
679 (void *)LIST_FIRST(&pgrp->pg_members));
680 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
6ea70f76 681 kprintf("\t\tpid %ld addr %p pgrp %p\n",
984263bc
MD
682 (long)p->p_pid, (void *)p,
683 (void *)p->p_pgrp);
684 }
685 }
686 }
687 }
688}
689#endif /* DDB */
690
5bf0d9b5 691/*
6f9db615 692 * Locate a process on the zombie list. Return a process or NULL.
99ad9bc4
MD
693 *
694 * The caller must hold proc_token if a stable result is desired.
695 * No other requirements.
5bf0d9b5 696 */
984263bc
MD
697struct proc *
698zpfind(pid_t pid)
699{
700 struct proc *p;
701
99ad9bc4
MD
702 lwkt_gettoken(&proc_token);
703 LIST_FOREACH(p, &zombproc, p_list) {
46270ec6
MD
704 if (p->p_pid == pid) {
705 lwkt_reltoken(&proc_token);
984263bc 706 return (p);
46270ec6 707 }
99ad9bc4
MD
708 }
709 lwkt_reltoken(&proc_token);
984263bc
MD
710 return (NULL);
711}
712
99ad9bc4
MD
713/*
714 * The caller must hold proc_token.
715 */
984263bc 716static int
5dfd06ac 717sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
984263bc 718{
5dfd06ac
SS
719 struct kinfo_proc ki;
720 struct lwp *lp;
fe14f34e 721 int skp = 0, had_output = 0;
984263bc 722 int error;
d9d6cb99 723
fa2217dc 724 bzero(&ki, sizeof(ki));
5dfd06ac
SS
725 fill_kinfo_proc(p, &ki);
726 if ((flags & KERN_PROC_FLAG_LWP) == 0)
727 skp = 1;
fa2217dc 728 error = 0;
5dfd06ac 729 FOREACH_LWP_IN_PROC(lp, p) {
fa2217dc 730 LWPHOLD(lp);
5dfd06ac 731 fill_kinfo_lwp(lp, &ki.kp_lwp);
5dfd06ac
SS
732 had_output = 1;
733 error = SYSCTL_OUT(req, &ki, sizeof(ki));
fa2217dc 734 LWPRELE(lp);
5dfd06ac 735 if (error)
fa2217dc 736 break;
5dfd06ac
SS
737 if (skp)
738 break;
73e4f7b9 739 }
5dfd06ac 740 /* We need to output at least the proc, even if there is no lwp. */
fa2217dc
MD
741 if (had_output == 0) {
742 error = SYSCTL_OUT(req, &ki, sizeof(ki));
743 }
744 return (error);
984263bc
MD
745}
746
99ad9bc4
MD
747/*
748 * The caller must hold proc_token.
749 */
ef02d0e1
TS
750static int
751sysctl_out_proc_kthread(struct thread *td, struct sysctl_req *req, int flags)
752{
753 struct kinfo_proc ki;
754 int error;
755
756 fill_kinfo_proc_kthread(td, &ki);
757 error = SYSCTL_OUT(req, &ki, sizeof(ki));
758 if (error)
759 return error;
760 return(0);
761}
762
99ad9bc4
MD
763/*
764 * No requirements.
765 */
984263bc
MD
766static int
767sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
768{
769 int *name = (int*) arg1;
5dfd06ac 770 int oid = oidp->oid_number;
984263bc 771 u_int namelen = arg2;
7cd8fd20 772 struct proc *p;
5dfd06ac 773 struct proclist *plist;
ef02d0e1 774 struct thread *td;
5dfd06ac 775 int doingzomb, flags = 0;
984263bc 776 int error = 0;
ef02d0e1 777 int n;
630ccdeb 778 int origcpu;
41c20dac 779 struct ucred *cr1 = curproc->p_ucred;
984263bc 780
5dfd06ac
SS
781 flags = oid & KERN_PROC_FLAGMASK;
782 oid &= ~KERN_PROC_FLAGMASK;
783
784 if ((oid == KERN_PROC_ALL && namelen != 0) ||
785 (oid != KERN_PROC_ALL && namelen != 1))
786 return (EINVAL);
787
99ad9bc4 788 lwkt_gettoken(&proc_token);
5dfd06ac 789 if (oid == KERN_PROC_PID) {
984263bc 790 p = pfind((pid_t)name[0]);
99ad9bc4
MD
791 if (p == NULL)
792 goto post_threads;
41c20dac 793 if (!PRISON_CHECK(cr1, p->p_ucred))
99ad9bc4 794 goto post_threads;
e7093b07 795 PHOLD(p);
5dfd06ac 796 error = sysctl_out_proc(p, req, flags);
e7093b07 797 PRELE(p);
99ad9bc4 798 goto post_threads;
984263bc 799 }
5dfd06ac 800
984263bc
MD
801 if (!req->oldptr) {
802 /* overestimate by 5 procs */
803 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
804 if (error)
99ad9bc4 805 goto post_threads;
984263bc 806 }
5dfd06ac
SS
807 for (doingzomb = 0; doingzomb <= 1; doingzomb++) {
808 if (doingzomb)
809 plist = &zombproc;
984263bc 810 else
5dfd06ac 811 plist = &allproc;
7cd8fd20 812 LIST_FOREACH(p, plist, p_list) {
984263bc
MD
813 /*
814 * Show a user only their processes.
815 */
41c20dac 816 if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred))
984263bc
MD
817 continue;
818 /*
819 * Skip embryonic processes.
820 */
821 if (p->p_stat == SIDL)
822 continue;
823 /*
824 * TODO - make more efficient (see notes below).
825 * do by session.
826 */
5dfd06ac 827 switch (oid) {
984263bc
MD
828 case KERN_PROC_PGRP:
829 /* could do this by traversing pgrp */
830 if (p->p_pgrp == NULL ||
831 p->p_pgrp->pg_id != (pid_t)name[0])
832 continue;
833 break;
834
835 case KERN_PROC_TTY:
836 if ((p->p_flag & P_CONTROLT) == 0 ||
837 p->p_session == NULL ||
838 p->p_session->s_ttyp == NULL ||
839 dev2udev(p->p_session->s_ttyp->t_dev) !=
840 (udev_t)name[0])
841 continue;
842 break;
843
844 case KERN_PROC_UID:
845 if (p->p_ucred == NULL ||
846 p->p_ucred->cr_uid != (uid_t)name[0])
847 continue;
848 break;
849
850 case KERN_PROC_RUID:
851 if (p->p_ucred == NULL ||
41c20dac 852 p->p_ucred->cr_ruid != (uid_t)name[0])
984263bc
MD
853 continue;
854 break;
855 }
856
41c20dac 857 if (!PRISON_CHECK(cr1, p->p_ucred))
984263bc 858 continue;
c008d3ad 859 PHOLD(p);
5dfd06ac 860 error = sysctl_out_proc(p, req, flags);
c008d3ad 861 PRELE(p);
984263bc 862 if (error)
99ad9bc4 863 goto post_threads;
984263bc
MD
864 }
865 }
630ccdeb
MD
866
867 /*
868 * Iterate over all active cpus and scan their thread list. Start
869 * with the next logical cpu and end with our original cpu. We
870 * migrate our own thread to each target cpu in order to safely scan
871 * its thread list. In the last loop we migrate back to our original
872 * cpu.
873 */
874 origcpu = mycpu->gd_cpuid;
56c703bd
JS
875 if (!ps_showallthreads || jailed(cr1))
876 goto post_threads;
99ad9bc4 877
56c703bd 878 for (n = 1; n <= ncpus; ++n) {
630ccdeb
MD
879 globaldata_t rgd;
880 int nid;
881
882 nid = (origcpu + n) % ncpus;
883 if ((smp_active_mask & (1 << nid)) == 0)
884 continue;
885 rgd = globaldata_find(nid);
886 lwkt_setcpu_self(rgd);
630ccdeb 887
73e4f7b9
MD
888 TAILQ_FOREACH(td, &mycpu->gd_tdallq, td_allq) {
889 if (td->td_proc)
890 continue;
ef02d0e1 891 switch (oid) {
73e4f7b9
MD
892 case KERN_PROC_PGRP:
893 case KERN_PROC_TTY:
894 case KERN_PROC_UID:
895 case KERN_PROC_RUID:
896 continue;
897 default:
898 break;
899 }
900 lwkt_hold(td);
ef02d0e1 901 error = sysctl_out_proc_kthread(td, req, doingzomb);
73e4f7b9
MD
902 lwkt_rele(td);
903 if (error)
99ad9bc4 904 goto post_threads;
73e4f7b9
MD
905 }
906 }
56c703bd 907post_threads:
99ad9bc4
MD
908 lwkt_reltoken(&proc_token);
909 return (error);
984263bc
MD
910}
911
912/*
913 * This sysctl allows a process to retrieve the argument list or process
914 * title for another process without groping around in the address space
915 * of the other process. It also allow a process to set its own "process
916 * title to a string of its own choice.
99ad9bc4
MD
917 *
918 * No requirements.
984263bc
MD
919 */
920static int
921sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
922{
923 int *name = (int*) arg1;
924 u_int namelen = arg2;
925 struct proc *p;
926 struct pargs *pa;
927 int error = 0;
41c20dac 928 struct ucred *cr1 = curproc->p_ucred;
984263bc
MD
929
930 if (namelen != 1)
931 return (EINVAL);
932
99ad9bc4 933 lwkt_gettoken(&proc_token);
984263bc 934 p = pfind((pid_t)name[0]);
99ad9bc4
MD
935 if (p == NULL)
936 goto done;
984263bc 937
41c20dac 938 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
99ad9bc4 939 goto done;
984263bc 940
99ad9bc4
MD
941 if (req->newptr && curproc != p) {
942 error = EPERM;
943 goto done;
944 }
984263bc 945
99ad9bc4
MD
946 PHOLD(p);
947 if (req->oldptr && p->p_args != NULL) {
948 error = SYSCTL_OUT(req, p->p_args->ar_args,
949 p->p_args->ar_length);
950 }
951 if (req->newptr == NULL) {
952 PRELE(p);
953 goto done;
954 }
984263bc
MD
955
956 if (p->p_args && --p->p_args->ar_ref == 0)
957 FREE(p->p_args, M_PARGS);
958 p->p_args = NULL;
959
99ad9bc4
MD
960 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) {
961 PRELE(p);
962 goto done;
963 }
984263bc
MD
964
965 MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen,
99ad9bc4 966 M_PARGS, M_WAITOK);
984263bc
MD
967 pa->ar_ref = 1;
968 pa->ar_length = req->newlen;
969 error = SYSCTL_IN(req, pa->ar_args, req->newlen);
970 if (!error)
971 p->p_args = pa;
972 else
973 FREE(pa, M_PARGS);
99ad9bc4
MD
974 PRELE(p);
975done:
976 lwkt_reltoken(&proc_token);
984263bc
MD
977 return (error);
978}
979
980SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
981
982SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
983 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
984
985SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
986 sysctl_kern_proc, "Process table");
987
988SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
989 sysctl_kern_proc, "Process table");
990
991SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
992 sysctl_kern_proc, "Process table");
993
994SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
995 sysctl_kern_proc, "Process table");
996
997SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
998 sysctl_kern_proc, "Process table");
999
5dfd06ac
SS
1000SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD,
1001 sysctl_kern_proc, "Process table");
1002
1003SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD,
1004 sysctl_kern_proc, "Process table");
1005
1006SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD,
1007 sysctl_kern_proc, "Process table");
1008
1009SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD,
1010 sysctl_kern_proc, "Process table");
1011
1012SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD,
1013 sysctl_kern_proc, "Process table");
1014
1015SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD,
1016 sysctl_kern_proc, "Process table");
1017
984263bc
MD
1018SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
1019 sysctl_kern_proc_args, "Process argument list");