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