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