Migrate allocation of proc structures from zones to kmalloc(). This gives us
[dragonfly.git] / sys / kern / kern_proc.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
34 * $FreeBSD: src/sys/kern/kern_proc.c,v 1.63.2.9 2003/05/08 07:47:16 kbyanc Exp $
fb2a331e 35 * $DragonFly: src/sys/kern/kern_proc.c,v 1.42 2008/04/01 18:06:34 nth Exp $
984263bc
MD
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/sysctl.h>
42#include <sys/malloc.h>
43#include <sys/proc.h>
56c703bd 44#include <sys/jail.h>
984263bc
MD
45#include <sys/filedesc.h>
46#include <sys/tty.h>
47#include <sys/signalvar.h>
5bf0d9b5 48#include <sys/spinlock.h>
984263bc
MD
49#include <vm/vm.h>
50#include <sys/lock.h>
51#include <vm/pmap.h>
52#include <vm/vm_map.h>
53#include <sys/user.h>
54#include <vm/vm_zone.h>
630ccdeb 55#include <machine/smp.h>
984263bc 56
5bf0d9b5
MD
57#include <sys/spinlock2.h>
58
984263bc
MD
59static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
60MALLOC_DEFINE(M_SESSION, "session", "session header");
fb2a331e 61MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
984263bc
MD
62MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
63
28c57d20 64int ps_showallprocs = 1;
73e4f7b9 65static int ps_showallthreads = 1;
43a0f7ae
MS
66SYSCTL_INT(_security, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
67 &ps_showallprocs, 0,
68 "Unprivileged processes can see proccesses with different UID/GID");
69SYSCTL_INT(_security, OID_AUTO, ps_showallthreads, CTLFLAG_RW,
70 &ps_showallthreads, 0,
71 "Unprivileged processes can see kernel threads");
984263bc 72
51e64ff2
MD
73static void pgdelete(struct pgrp *);
74static void orphanpg(struct pgrp *pg);
75static pid_t proc_getnewpid_locked(int random_offset);
984263bc
MD
76
77/*
78 * Other process lists
79 */
80struct pidhashhead *pidhashtbl;
81u_long pidhash;
82struct pgrphashhead *pgrphashtbl;
83u_long pgrphash;
84struct proclist allproc;
85struct proclist zombproc;
5bf0d9b5 86struct spinlock allproc_spin;
08f2f1bb 87vm_zone_t lwp_zone;
263e4574 88vm_zone_t thread_zone;
984263bc 89
51e64ff2
MD
90/*
91 * Random component to nextpid generation. We mix in a random factor to make
92 * it a little harder to predict. We sanity check the modulus value to avoid
93 * doing it in critical paths. Don't let it be too small or we pointlessly
94 * waste randomness entropy, and don't let it be impossibly large. Using a
95 * modulus that is too big causes a LOT more process table scans and slows
96 * down fork processing as the pidchecked caching is defeated.
97 */
98static int randompid = 0;
99
100static int
101sysctl_kern_randompid(SYSCTL_HANDLER_ARGS)
102{
103 int error, pid;
104
105 pid = randompid;
106 error = sysctl_handle_int(oidp, &pid, 0, req);
107 if (error || !req->newptr)
108 return (error);
109 if (pid < 0 || pid > PID_MAX - 100) /* out of range */
110 pid = PID_MAX - 100;
111 else if (pid < 2) /* NOP */
112 pid = 0;
113 else if (pid < 100) /* Make it reasonable */
114 pid = 100;
115 randompid = pid;
116 return (error);
117}
118
119SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW,
120 0, 0, sysctl_kern_randompid, "I", "Random PID modulus");
121
984263bc
MD
122/*
123 * Initialize global process hashing structures.
124 */
125void
77153250 126procinit(void)
984263bc 127{
984263bc
MD
128 LIST_INIT(&allproc);
129 LIST_INIT(&zombproc);
5bf0d9b5 130 spin_init(&allproc_spin);
984263bc
MD
131 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
132 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
08f2f1bb 133 lwp_zone = zinit("LWP", sizeof (struct lwp), 0, 0, 5);
263e4574 134 thread_zone = zinit("THREAD", sizeof (struct thread), 0, 0, 5);
984263bc
MD
135 uihashinit();
136}
137
138/*
139 * Is p an inferior of the current process?
140 */
141int
77153250 142inferior(struct proc *p)
984263bc 143{
984263bc
MD
144 for (; p != curproc; p = p->p_pptr)
145 if (p->p_pid == 0)
146 return (0);
147 return (1);
148}
149
150/*
151 * Locate a process by number
152 */
153struct proc *
77153250 154pfind(pid_t pid)
984263bc 155{
1fd87d54 156 struct proc *p;
984263bc 157
5bf0d9b5 158 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
984263bc
MD
159 if (p->p_pid == pid)
160 return (p);
5bf0d9b5 161 }
984263bc
MD
162 return (NULL);
163}
164
165/*
166 * Locate a process group by number
167 */
168struct pgrp *
77153250 169pgfind(pid_t pgid)
984263bc 170{
1fd87d54 171 struct pgrp *pgrp;
984263bc 172
5bf0d9b5 173 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
984263bc
MD
174 if (pgrp->pg_id == pgid)
175 return (pgrp);
5bf0d9b5 176 }
984263bc
MD
177 return (NULL);
178}
179
180/*
181 * Move p to a new or existing process group (and session)
182 */
183int
77153250 184enterpgrp(struct proc *p, pid_t pgid, int mksess)
984263bc 185{
1fd87d54 186 struct pgrp *pgrp = pgfind(pgid);
984263bc
MD
187
188 KASSERT(pgrp == NULL || !mksess,
189 ("enterpgrp: setsid into non-empty pgrp"));
190 KASSERT(!SESS_LEADER(p),
191 ("enterpgrp: session leader attempted setpgrp"));
192
193 if (pgrp == NULL) {
194 pid_t savepid = p->p_pid;
195 struct proc *np;
196 /*
197 * new process group
198 */
199 KASSERT(p->p_pid == pgid,
200 ("enterpgrp: new pgrp and pid != pgid"));
201 if ((np = pfind(savepid)) == NULL || np != p)
202 return (ESRCH);
203 MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
204 M_WAITOK);
205 if (mksess) {
1fd87d54 206 struct session *sess;
984263bc
MD
207
208 /*
209 * new session
210 */
211 MALLOC(sess, struct session *, sizeof(struct session),
212 M_SESSION, M_WAITOK);
213 sess->s_leader = p;
214 sess->s_sid = p->p_pid;
215 sess->s_count = 1;
216 sess->s_ttyvp = NULL;
217 sess->s_ttyp = NULL;
218 bcopy(p->p_session->s_login, sess->s_login,
219 sizeof(sess->s_login));
220 p->p_flag &= ~P_CONTROLT;
221 pgrp->pg_session = sess;
222 KASSERT(p == curproc,
223 ("enterpgrp: mksession and p != curproc"));
224 } else {
225 pgrp->pg_session = p->p_session;
8b90699b 226 sess_hold(pgrp->pg_session);
984263bc
MD
227 }
228 pgrp->pg_id = pgid;
229 LIST_INIT(&pgrp->pg_members);
230 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
231 pgrp->pg_jobc = 0;
232 SLIST_INIT(&pgrp->pg_sigiolst);
167e6ecb 233 lockinit(&pgrp->pg_lock, "pgwt", 0, 0);
984263bc
MD
234 } else if (pgrp == p->p_pgrp)
235 return (0);
236
237 /*
238 * Adjust eligibility of affected pgrps to participate in job control.
239 * Increment eligibility counts before decrementing, otherwise we
240 * could reach 0 spuriously during the first call.
241 */
242 fixjobc(p, pgrp, 1);
243 fixjobc(p, p->p_pgrp, 0);
244
245 LIST_REMOVE(p, p_pglist);
246 if (LIST_EMPTY(&p->p_pgrp->pg_members))
247 pgdelete(p->p_pgrp);
248 p->p_pgrp = pgrp;
249 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
250 return (0);
251}
252
253/*
254 * remove process from process group
255 */
256int
77153250 257leavepgrp(struct proc *p)
984263bc
MD
258{
259
260 LIST_REMOVE(p, p_pglist);
261 if (LIST_EMPTY(&p->p_pgrp->pg_members))
262 pgdelete(p->p_pgrp);
263 p->p_pgrp = 0;
264 return (0);
265}
266
267/*
268 * delete a process group
269 */
270static void
77153250 271pgdelete(struct pgrp *pgrp)
984263bc
MD
272{
273
274 /*
275 * Reset any sigio structures pointing to us as a result of
276 * F_SETOWN with our pgid.
277 */
278 funsetownlst(&pgrp->pg_sigiolst);
279
280 if (pgrp->pg_session->s_ttyp != NULL &&
281 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
282 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
283 LIST_REMOVE(pgrp, pg_hash);
8b90699b 284 sess_rele(pgrp->pg_session);
efda3bd0 285 kfree(pgrp, M_PGRP);
8b90699b
MD
286}
287
288/*
289 * Adjust the ref count on a session structure. When the ref count falls to
290 * zero the tty is disassociated from the session and the session structure
291 * is freed. Note that tty assocation is not itself ref-counted.
292 */
293void
294sess_hold(struct session *sp)
295{
296 ++sp->s_count;
297}
298
299void
300sess_rele(struct session *sp)
301{
302 KKASSERT(sp->s_count > 0);
303 if (--sp->s_count == 0) {
304 if (sp->s_ttyp && sp->s_ttyp->t_session) {
305#ifdef TTY_DO_FULL_CLOSE
306 /* FULL CLOSE, see ttyclearsession() */
307 KKASSERT(sp->s_ttyp->t_session == sp);
308 sp->s_ttyp->t_session = NULL;
309#else
310 /* HALF CLOSE, see ttyclearsession() */
311 if (sp->s_ttyp->t_session == sp)
312 sp->s_ttyp->t_session = NULL;
313#endif
314 }
efda3bd0 315 kfree(sp, M_SESSION);
8b90699b 316 }
984263bc
MD
317}
318
319/*
320 * Adjust pgrp jobc counters when specified process changes process group.
321 * We count the number of processes in each process group that "qualify"
322 * the group for terminal job control (those with a parent in a different
323 * process group of the same session). If that count reaches zero, the
324 * process group becomes orphaned. Check both the specified process'
325 * process group and that of its children.
326 * entering == 0 => p is leaving specified group.
327 * entering == 1 => p is entering specified group.
328 */
329void
77153250 330fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
984263bc 331{
1fd87d54
RG
332 struct pgrp *hispgrp;
333 struct session *mysession = pgrp->pg_session;
984263bc
MD
334
335 /*
336 * Check p's parent to see whether p qualifies its own process
337 * group; if so, adjust count for p's process group.
338 */
339 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
340 hispgrp->pg_session == mysession) {
341 if (entering)
342 pgrp->pg_jobc++;
343 else if (--pgrp->pg_jobc == 0)
344 orphanpg(pgrp);
345 }
346
347 /*
348 * Check this process' children to see whether they qualify
349 * their process groups; if so, adjust counts for children's
350 * process groups.
351 */
352 LIST_FOREACH(p, &p->p_children, p_sibling)
353 if ((hispgrp = p->p_pgrp) != pgrp &&
354 hispgrp->pg_session == mysession &&
416d05d7 355 p->p_stat != SZOMB) {
984263bc
MD
356 if (entering)
357 hispgrp->pg_jobc++;
358 else if (--hispgrp->pg_jobc == 0)
359 orphanpg(hispgrp);
360 }
361}
362
363/*
364 * A process group has become orphaned;
365 * if there are any stopped processes in the group,
366 * hang-up all process in that group.
367 */
368static void
77153250 369orphanpg(struct pgrp *pg)
984263bc 370{
1fd87d54 371 struct proc *p;
984263bc
MD
372
373 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
164b8401 374 if (p->p_stat == SSTOP) {
984263bc 375 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
84204577
MD
376 ksignal(p, SIGHUP);
377 ksignal(p, SIGCONT);
984263bc
MD
378 }
379 return;
380 }
381 }
382}
383
51e64ff2
MD
384/*
385 * Add a new process to the allproc list and the PID hash. This
386 * also assigns a pid to the new process.
387 *
0ced1954 388 * MPALMOSTSAFE - acquires mplock for karc4random() call
51e64ff2
MD
389 */
390void
391proc_add_allproc(struct proc *p)
392{
393 int random_offset;
394
395 if ((random_offset = randompid) != 0) {
396 get_mplock();
0ced1954 397 random_offset = karc4random() % random_offset;
51e64ff2
MD
398 rel_mplock();
399 }
400
401 spin_lock_wr(&allproc_spin);
402 p->p_pid = proc_getnewpid_locked(random_offset);
403 LIST_INSERT_HEAD(&allproc, p, p_list);
404 LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash);
405 spin_unlock_wr(&allproc_spin);
406}
407
408/*
409 * Calculate a new process pid. This function is integrated into
410 * proc_add_allproc() to guarentee that the new pid is not reused before
411 * the new process can be added to the allproc list.
412 *
413 * MPSAFE - must be called with allproc_spin held.
414 */
415static
416pid_t
417proc_getnewpid_locked(int random_offset)
418{
419 static pid_t nextpid;
420 static pid_t pidchecked;
421 struct proc *p;
422
423 /*
424 * Find an unused process ID. We remember a range of unused IDs
425 * ready to use (from nextpid+1 through pidchecked-1).
426 */
427 nextpid = nextpid + 1 + random_offset;
428retry:
429 /*
430 * If the process ID prototype has wrapped around,
431 * restart somewhat above 0, as the low-numbered procs
432 * tend to include daemons that don't exit.
433 */
434 if (nextpid >= PID_MAX) {
435 nextpid = nextpid % PID_MAX;
436 if (nextpid < 100)
437 nextpid += 100;
438 pidchecked = 0;
439 }
440 if (nextpid >= pidchecked) {
441 int doingzomb = 0;
442
443 pidchecked = PID_MAX;
444 /*
445 * Scan the active and zombie procs to check whether this pid
446 * is in use. Remember the lowest pid that's greater
447 * than nextpid, so we can avoid checking for a while.
448 */
449 p = LIST_FIRST(&allproc);
450again:
451 for (; p != 0; p = LIST_NEXT(p, p_list)) {
452 while (p->p_pid == nextpid ||
453 p->p_pgrp->pg_id == nextpid ||
454 p->p_session->s_sid == nextpid) {
455 nextpid++;
456 if (nextpid >= pidchecked)
457 goto retry;
458 }
459 if (p->p_pid > nextpid && pidchecked > p->p_pid)
460 pidchecked = p->p_pid;
461 if (p->p_pgrp->pg_id > nextpid &&
462 pidchecked > p->p_pgrp->pg_id)
463 pidchecked = p->p_pgrp->pg_id;
464 if (p->p_session->s_sid > nextpid &&
465 pidchecked > p->p_session->s_sid)
466 pidchecked = p->p_session->s_sid;
467 }
468 if (!doingzomb) {
469 doingzomb = 1;
470 p = LIST_FIRST(&zombproc);
471 goto again;
472 }
473 }
474 return(nextpid);
475}
476
5bf0d9b5
MD
477/*
478 * Called from exit1 to remove a process from the allproc
479 * list and move it to the zombie list.
480 *
481 * MPSAFE
482 */
483void
484proc_move_allproc_zombie(struct proc *p)
485{
486 spin_lock_wr(&allproc_spin);
487 while (p->p_lock) {
488 spin_unlock_wr(&allproc_spin);
489 tsleep(p, 0, "reap1", hz / 10);
490 spin_lock_wr(&allproc_spin);
491 }
492 LIST_REMOVE(p, p_list);
493 LIST_INSERT_HEAD(&zombproc, p, p_list);
494 LIST_REMOVE(p, p_hash);
416d05d7 495 p->p_stat = SZOMB;
5bf0d9b5
MD
496 spin_unlock_wr(&allproc_spin);
497}
498
499/*
500 * This routine is called from kern_wait() and will remove the process
501 * from the zombie list and the sibling list. This routine will block
502 * if someone has a lock on the proces (p_lock).
503 *
504 * MPSAFE
505 */
506void
507proc_remove_zombie(struct proc *p)
508{
509 spin_lock_wr(&allproc_spin);
510 while (p->p_lock) {
511 spin_unlock_wr(&allproc_spin);
512 tsleep(p, 0, "reap1", hz / 10);
513 spin_lock_wr(&allproc_spin);
514 }
515 LIST_REMOVE(p, p_list); /* off zombproc */
516 LIST_REMOVE(p, p_sibling);
517 spin_unlock_wr(&allproc_spin);
518}
519
520/*
521 * Scan all processes on the allproc list. The process is automatically
522 * held for the callback. A return value of -1 terminates the loop.
523 *
524 * MPSAFE
525 */
526void
527allproc_scan(int (*callback)(struct proc *, void *), void *data)
528{
529 struct proc *p;
530 int r;
531
532 spin_lock_rd(&allproc_spin);
533 LIST_FOREACH(p, &allproc, p_list) {
534 PHOLD(p);
535 spin_unlock_rd(&allproc_spin);
536 r = callback(p, data);
537 spin_lock_rd(&allproc_spin);
538 PRELE(p);
539 if (r < 0)
540 break;
541 }
542 spin_unlock_rd(&allproc_spin);
543}
544
c7e98b2f
SS
545/*
546 * Scan all lwps of processes on the allproc list. The lwp is automatically
547 * held for the callback. A return value of -1 terminates the loop.
548 *
549 * possibly not MPSAFE, needs to access foreingn proc structures
550 */
551void
552alllwp_scan(int (*callback)(struct lwp *, void *), void *data)
553{
554 struct proc *p;
555 struct lwp *lp;
556 int r = 0;
557
558 spin_lock_rd(&allproc_spin);
559 LIST_FOREACH(p, &allproc, p_list) {
560 PHOLD(p);
561 spin_unlock_rd(&allproc_spin);
562 FOREACH_LWP_IN_PROC(lp, p) {
563 LWPHOLD(lp);
564 r = callback(lp, data);
565 LWPRELE(lp);
566 }
567 spin_lock_rd(&allproc_spin);
568 PRELE(p);
569 if (r < 0)
570 break;
571 }
572 spin_unlock_rd(&allproc_spin);
573}
574
5bf0d9b5
MD
575/*
576 * Scan all processes on the zombproc list. The process is automatically
577 * held for the callback. A return value of -1 terminates the loop.
578 *
579 * MPSAFE
580 */
581void
582zombproc_scan(int (*callback)(struct proc *, void *), void *data)
583{
584 struct proc *p;
585 int r;
586
587 spin_lock_rd(&allproc_spin);
588 LIST_FOREACH(p, &zombproc, p_list) {
589 PHOLD(p);
590 spin_unlock_rd(&allproc_spin);
591 r = callback(p, data);
592 spin_lock_rd(&allproc_spin);
593 PRELE(p);
594 if (r < 0)
595 break;
596 }
597 spin_unlock_rd(&allproc_spin);
598}
599
984263bc
MD
600#include "opt_ddb.h"
601#ifdef DDB
602#include <ddb/ddb.h>
603
604DB_SHOW_COMMAND(pgrpdump, pgrpdump)
605{
1fd87d54
RG
606 struct pgrp *pgrp;
607 struct proc *p;
608 int i;
984263bc
MD
609
610 for (i = 0; i <= pgrphash; i++) {
611 if (!LIST_EMPTY(&pgrphashtbl[i])) {
6ea70f76 612 kprintf("\tindx %d\n", i);
984263bc 613 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
6ea70f76 614 kprintf(
984263bc
MD
615 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
616 (void *)pgrp, (long)pgrp->pg_id,
617 (void *)pgrp->pg_session,
618 pgrp->pg_session->s_count,
619 (void *)LIST_FIRST(&pgrp->pg_members));
620 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
6ea70f76 621 kprintf("\t\tpid %ld addr %p pgrp %p\n",
984263bc
MD
622 (long)p->p_pid, (void *)p,
623 (void *)p->p_pgrp);
624 }
625 }
626 }
627 }
628}
629#endif /* DDB */
630
5bf0d9b5
MD
631/*
632 * Locate a process on the zombie list. Return a held process or NULL.
633 */
984263bc
MD
634struct proc *
635zpfind(pid_t pid)
636{
637 struct proc *p;
638
639 LIST_FOREACH(p, &zombproc, p_list)
640 if (p->p_pid == pid)
641 return (p);
642 return (NULL);
643}
644
984263bc 645static int
5dfd06ac 646sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
984263bc 647{
5dfd06ac
SS
648 struct kinfo_proc ki;
649 struct lwp *lp;
fe14f34e 650 int skp = 0, had_output = 0;
984263bc 651 int error;
d9d6cb99 652
5dfd06ac
SS
653 fill_kinfo_proc(p, &ki);
654 if ((flags & KERN_PROC_FLAG_LWP) == 0)
655 skp = 1;
656 FOREACH_LWP_IN_PROC(lp, p) {
657 fill_kinfo_lwp(lp, &ki.kp_lwp);
658output:
659 had_output = 1;
660 error = SYSCTL_OUT(req, &ki, sizeof(ki));
661 if (error)
662 return error;
663 if (skp)
664 break;
73e4f7b9 665 }
5dfd06ac
SS
666 /* We need to output at least the proc, even if there is no lwp. */
667 if (!had_output)
668 goto output;
73e4f7b9 669#if 0
984263bc
MD
670 if (!doingzomb && pid && (pfind(pid) != p))
671 return EAGAIN;
672 if (doingzomb && zpfind(pid) != p)
673 return EAGAIN;
73e4f7b9 674#endif
984263bc
MD
675 return (0);
676}
677
ef02d0e1
TS
678static int
679sysctl_out_proc_kthread(struct thread *td, struct sysctl_req *req, int flags)
680{
681 struct kinfo_proc ki;
682 int error;
683
684 fill_kinfo_proc_kthread(td, &ki);
685 error = SYSCTL_OUT(req, &ki, sizeof(ki));
686 if (error)
687 return error;
688 return(0);
689}
690
984263bc
MD
691static int
692sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
693{
694 int *name = (int*) arg1;
5dfd06ac 695 int oid = oidp->oid_number;
984263bc 696 u_int namelen = arg2;
5dfd06ac
SS
697 struct proc *p, *np;
698 struct proclist *plist;
ef02d0e1 699 struct thread *td;
5dfd06ac 700 int doingzomb, flags = 0;
984263bc 701 int error = 0;
ef02d0e1 702 int n;
630ccdeb 703 int origcpu;
41c20dac 704 struct ucred *cr1 = curproc->p_ucred;
984263bc 705
5dfd06ac
SS
706 flags = oid & KERN_PROC_FLAGMASK;
707 oid &= ~KERN_PROC_FLAGMASK;
708
709 if ((oid == KERN_PROC_ALL && namelen != 0) ||
710 (oid != KERN_PROC_ALL && namelen != 1))
711 return (EINVAL);
712
713 if (oid == KERN_PROC_PID) {
984263bc
MD
714 p = pfind((pid_t)name[0]);
715 if (!p)
716 return (0);
41c20dac 717 if (!PRISON_CHECK(cr1, p->p_ucred))
984263bc 718 return (0);
e7093b07 719 PHOLD(p);
5dfd06ac 720 error = sysctl_out_proc(p, req, flags);
e7093b07 721 PRELE(p);
984263bc
MD
722 return (error);
723 }
5dfd06ac 724
984263bc
MD
725 if (!req->oldptr) {
726 /* overestimate by 5 procs */
727 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
728 if (error)
729 return (error);
730 }
5dfd06ac
SS
731 for (doingzomb = 0; doingzomb <= 1; doingzomb++) {
732 if (doingzomb)
733 plist = &zombproc;
984263bc 734 else
5dfd06ac
SS
735 plist = &allproc;
736 LIST_FOREACH_MUTABLE(p, plist, p_list, np) {
984263bc
MD
737 /*
738 * Show a user only their processes.
739 */
41c20dac 740 if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred))
984263bc
MD
741 continue;
742 /*
743 * Skip embryonic processes.
744 */
745 if (p->p_stat == SIDL)
746 continue;
747 /*
748 * TODO - make more efficient (see notes below).
749 * do by session.
750 */
5dfd06ac 751 switch (oid) {
984263bc
MD
752 case KERN_PROC_PGRP:
753 /* could do this by traversing pgrp */
754 if (p->p_pgrp == NULL ||
755 p->p_pgrp->pg_id != (pid_t)name[0])
756 continue;
757 break;
758
759 case KERN_PROC_TTY:
760 if ((p->p_flag & P_CONTROLT) == 0 ||
761 p->p_session == NULL ||
762 p->p_session->s_ttyp == NULL ||
763 dev2udev(p->p_session->s_ttyp->t_dev) !=
764 (udev_t)name[0])
765 continue;
766 break;
767
768 case KERN_PROC_UID:
769 if (p->p_ucred == NULL ||
770 p->p_ucred->cr_uid != (uid_t)name[0])
771 continue;
772 break;
773
774 case KERN_PROC_RUID:
775 if (p->p_ucred == NULL ||
41c20dac 776 p->p_ucred->cr_ruid != (uid_t)name[0])
984263bc
MD
777 continue;
778 break;
779 }
780
41c20dac 781 if (!PRISON_CHECK(cr1, p->p_ucred))
984263bc 782 continue;
c008d3ad 783 PHOLD(p);
5dfd06ac 784 error = sysctl_out_proc(p, req, flags);
c008d3ad 785 PRELE(p);
984263bc
MD
786 if (error)
787 return (error);
788 }
789 }
630ccdeb
MD
790
791 /*
792 * Iterate over all active cpus and scan their thread list. Start
793 * with the next logical cpu and end with our original cpu. We
794 * migrate our own thread to each target cpu in order to safely scan
795 * its thread list. In the last loop we migrate back to our original
796 * cpu.
797 */
798 origcpu = mycpu->gd_cpuid;
56c703bd
JS
799 if (!ps_showallthreads || jailed(cr1))
800 goto post_threads;
801 for (n = 1; n <= ncpus; ++n) {
630ccdeb
MD
802 globaldata_t rgd;
803 int nid;
804
805 nid = (origcpu + n) % ncpus;
806 if ((smp_active_mask & (1 << nid)) == 0)
807 continue;
808 rgd = globaldata_find(nid);
809 lwkt_setcpu_self(rgd);
630ccdeb 810
73e4f7b9
MD
811 TAILQ_FOREACH(td, &mycpu->gd_tdallq, td_allq) {
812 if (td->td_proc)
813 continue;
ef02d0e1 814 switch (oid) {
73e4f7b9
MD
815 case KERN_PROC_PGRP:
816 case KERN_PROC_TTY:
817 case KERN_PROC_UID:
818 case KERN_PROC_RUID:
819 continue;
820 default:
821 break;
822 }
823 lwkt_hold(td);
ef02d0e1 824 error = sysctl_out_proc_kthread(td, req, doingzomb);
73e4f7b9
MD
825 lwkt_rele(td);
826 if (error)
827 return (error);
828 }
829 }
56c703bd 830post_threads:
984263bc
MD
831 return (0);
832}
833
834/*
835 * This sysctl allows a process to retrieve the argument list or process
836 * title for another process without groping around in the address space
837 * of the other process. It also allow a process to set its own "process
838 * title to a string of its own choice.
839 */
840static int
841sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
842{
843 int *name = (int*) arg1;
844 u_int namelen = arg2;
845 struct proc *p;
846 struct pargs *pa;
847 int error = 0;
41c20dac 848 struct ucred *cr1 = curproc->p_ucred;
984263bc
MD
849
850 if (namelen != 1)
851 return (EINVAL);
852
853 p = pfind((pid_t)name[0]);
854 if (!p)
855 return (0);
856
41c20dac 857 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
984263bc
MD
858 return (0);
859
860 if (req->newptr && curproc != p)
861 return (EPERM);
862
863 if (req->oldptr && p->p_args != NULL)
864 error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length);
865 if (req->newptr == NULL)
866 return (error);
867
868 if (p->p_args && --p->p_args->ar_ref == 0)
869 FREE(p->p_args, M_PARGS);
870 p->p_args = NULL;
871
872 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
873 return (error);
874
875 MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen,
876 M_PARGS, M_WAITOK);
877 pa->ar_ref = 1;
878 pa->ar_length = req->newlen;
879 error = SYSCTL_IN(req, pa->ar_args, req->newlen);
880 if (!error)
881 p->p_args = pa;
882 else
883 FREE(pa, M_PARGS);
884 return (error);
885}
886
887SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
888
889SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
890 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
891
892SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
893 sysctl_kern_proc, "Process table");
894
895SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
896 sysctl_kern_proc, "Process table");
897
898SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
899 sysctl_kern_proc, "Process table");
900
901SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
902 sysctl_kern_proc, "Process table");
903
904SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
905 sysctl_kern_proc, "Process table");
906
5dfd06ac
SS
907SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD,
908 sysctl_kern_proc, "Process table");
909
910SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD,
911 sysctl_kern_proc, "Process table");
912
913SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD,
914 sysctl_kern_proc, "Process table");
915
916SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD,
917 sysctl_kern_proc, "Process table");
918
919SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD,
920 sysctl_kern_proc, "Process table");
921
922SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD,
923 sysctl_kern_proc, "Process table");
924
984263bc
MD
925SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
926 sysctl_kern_proc_args, "Process argument list");