Merge from vendor branch CVS:
[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.17 2004/09/13 16:22:36 dillon 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/filedesc.h>
45 #include <sys/tty.h>
46 #include <sys/signalvar.h>
47 #include <vm/vm.h>
48 #include <sys/lock.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_map.h>
51 #include <sys/user.h>
52 #include <vm/vm_zone.h>
53 #include <machine/smp.h>
54
55 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
56 MALLOC_DEFINE(M_SESSION, "session", "session header");
57 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
58 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
59
60 int ps_showallprocs = 1;
61 static int ps_showallthreads = 1;
62 SYSCTL_INT(_kern, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
63     &ps_showallprocs, 0, "");
64 SYSCTL_INT(_kern, OID_AUTO, ps_showallthreads, CTLFLAG_RW,
65     &ps_showallthreads, 0, "");
66
67 static void pgdelete    (struct pgrp *);
68
69 static void     orphanpg (struct pgrp *pg);
70
71 /*
72  * Other process lists
73  */
74 struct pidhashhead *pidhashtbl;
75 u_long pidhash;
76 struct pgrphashhead *pgrphashtbl;
77 u_long pgrphash;
78 struct proclist allproc;
79 struct proclist zombproc;
80 vm_zone_t proc_zone;
81 vm_zone_t thread_zone;
82
83 /*
84  * Initialize global process hashing structures.
85  */
86 void
87 procinit()
88 {
89
90         LIST_INIT(&allproc);
91         LIST_INIT(&zombproc);
92         pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
93         pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
94         proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5);
95         thread_zone = zinit("THREAD", sizeof (struct thread), 0, 0, 5);
96         uihashinit();
97 }
98
99 /*
100  * Is p an inferior of the current process?
101  */
102 int
103 inferior(p)
104         struct proc *p;
105 {
106
107         for (; p != curproc; p = p->p_pptr)
108                 if (p->p_pid == 0)
109                         return (0);
110         return (1);
111 }
112
113 /*
114  * Locate a process by number
115  */
116 struct proc *
117 pfind(pid)
118         pid_t pid;
119 {
120         struct proc *p;
121
122         LIST_FOREACH(p, PIDHASH(pid), p_hash)
123                 if (p->p_pid == pid)
124                         return (p);
125         return (NULL);
126 }
127
128 /*
129  * Locate a process group by number
130  */
131 struct pgrp *
132 pgfind(pgid)
133         pid_t pgid;
134 {
135         struct pgrp *pgrp;
136
137         LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
138                 if (pgrp->pg_id == pgid)
139                         return (pgrp);
140         return (NULL);
141 }
142
143 /*
144  * Move p to a new or existing process group (and session)
145  */
146 int
147 enterpgrp(p, pgid, mksess)
148         struct proc *p;
149         pid_t pgid;
150         int mksess;
151 {
152         struct pgrp *pgrp = pgfind(pgid);
153
154         KASSERT(pgrp == NULL || !mksess,
155             ("enterpgrp: setsid into non-empty pgrp"));
156         KASSERT(!SESS_LEADER(p),
157             ("enterpgrp: session leader attempted setpgrp"));
158
159         if (pgrp == NULL) {
160                 pid_t savepid = p->p_pid;
161                 struct proc *np;
162                 /*
163                  * new process group
164                  */
165                 KASSERT(p->p_pid == pgid,
166                     ("enterpgrp: new pgrp and pid != pgid"));
167                 if ((np = pfind(savepid)) == NULL || np != p)
168                         return (ESRCH);
169                 MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
170                     M_WAITOK);
171                 if (mksess) {
172                         struct session *sess;
173
174                         /*
175                          * new session
176                          */
177                         MALLOC(sess, struct session *, sizeof(struct session),
178                             M_SESSION, M_WAITOK);
179                         sess->s_leader = p;
180                         sess->s_sid = p->p_pid;
181                         sess->s_count = 1;
182                         sess->s_ttyvp = NULL;
183                         sess->s_ttyp = NULL;
184                         bcopy(p->p_session->s_login, sess->s_login,
185                             sizeof(sess->s_login));
186                         p->p_flag &= ~P_CONTROLT;
187                         pgrp->pg_session = sess;
188                         KASSERT(p == curproc,
189                             ("enterpgrp: mksession and p != curproc"));
190                 } else {
191                         pgrp->pg_session = p->p_session;
192                         sess_hold(pgrp->pg_session);
193                 }
194                 pgrp->pg_id = pgid;
195                 LIST_INIT(&pgrp->pg_members);
196                 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
197                 pgrp->pg_jobc = 0;
198                 SLIST_INIT(&pgrp->pg_sigiolst);
199         } else if (pgrp == p->p_pgrp)
200                 return (0);
201
202         /*
203          * Adjust eligibility of affected pgrps to participate in job control.
204          * Increment eligibility counts before decrementing, otherwise we
205          * could reach 0 spuriously during the first call.
206          */
207         fixjobc(p, pgrp, 1);
208         fixjobc(p, p->p_pgrp, 0);
209
210         LIST_REMOVE(p, p_pglist);
211         if (LIST_EMPTY(&p->p_pgrp->pg_members))
212                 pgdelete(p->p_pgrp);
213         p->p_pgrp = pgrp;
214         LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
215         return (0);
216 }
217
218 /*
219  * remove process from process group
220  */
221 int
222 leavepgrp(p)
223         struct proc *p;
224 {
225
226         LIST_REMOVE(p, p_pglist);
227         if (LIST_EMPTY(&p->p_pgrp->pg_members))
228                 pgdelete(p->p_pgrp);
229         p->p_pgrp = 0;
230         return (0);
231 }
232
233 /*
234  * delete a process group
235  */
236 static void
237 pgdelete(pgrp)
238         struct pgrp *pgrp;
239 {
240
241         /*
242          * Reset any sigio structures pointing to us as a result of
243          * F_SETOWN with our pgid.
244          */
245         funsetownlst(&pgrp->pg_sigiolst);
246
247         if (pgrp->pg_session->s_ttyp != NULL &&
248             pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
249                 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
250         LIST_REMOVE(pgrp, pg_hash);
251         sess_rele(pgrp->pg_session);
252         free(pgrp, M_PGRP);
253 }
254
255 /*
256  * Adjust the ref count on a session structure.  When the ref count falls to
257  * zero the tty is disassociated from the session and the session structure
258  * is freed.  Note that tty assocation is not itself ref-counted.
259  */
260 void
261 sess_hold(struct session *sp)
262 {
263         ++sp->s_count;
264 }
265
266 void
267 sess_rele(struct session *sp)
268 {
269         KKASSERT(sp->s_count > 0);
270         if (--sp->s_count == 0) {
271                 if (sp->s_ttyp && sp->s_ttyp->t_session) {
272 #ifdef TTY_DO_FULL_CLOSE
273                         /* FULL CLOSE, see ttyclearsession() */
274                         KKASSERT(sp->s_ttyp->t_session == sp);
275                         sp->s_ttyp->t_session = NULL;
276 #else
277                         /* HALF CLOSE, see ttyclearsession() */
278                         if (sp->s_ttyp->t_session == sp)
279                                 sp->s_ttyp->t_session = NULL;
280 #endif
281                 }
282                 free(sp, M_SESSION);
283         }
284 }
285
286 /*
287  * Adjust pgrp jobc counters when specified process changes process group.
288  * We count the number of processes in each process group that "qualify"
289  * the group for terminal job control (those with a parent in a different
290  * process group of the same session).  If that count reaches zero, the
291  * process group becomes orphaned.  Check both the specified process'
292  * process group and that of its children.
293  * entering == 0 => p is leaving specified group.
294  * entering == 1 => p is entering specified group.
295  */
296 void
297 fixjobc(p, pgrp, entering)
298         struct proc *p;
299         struct pgrp *pgrp;
300         int entering;
301 {
302         struct pgrp *hispgrp;
303         struct session *mysession = pgrp->pg_session;
304
305         /*
306          * Check p's parent to see whether p qualifies its own process
307          * group; if so, adjust count for p's process group.
308          */
309         if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
310             hispgrp->pg_session == mysession) {
311                 if (entering)
312                         pgrp->pg_jobc++;
313                 else if (--pgrp->pg_jobc == 0)
314                         orphanpg(pgrp);
315         }
316
317         /*
318          * Check this process' children to see whether they qualify
319          * their process groups; if so, adjust counts for children's
320          * process groups.
321          */
322         LIST_FOREACH(p, &p->p_children, p_sibling)
323                 if ((hispgrp = p->p_pgrp) != pgrp &&
324                     hispgrp->pg_session == mysession &&
325                     p->p_stat != SZOMB) {
326                         if (entering)
327                                 hispgrp->pg_jobc++;
328                         else if (--hispgrp->pg_jobc == 0)
329                                 orphanpg(hispgrp);
330                 }
331 }
332
333 /*
334  * A process group has become orphaned;
335  * if there are any stopped processes in the group,
336  * hang-up all process in that group.
337  */
338 static void
339 orphanpg(pg)
340         struct pgrp *pg;
341 {
342         struct proc *p;
343
344         LIST_FOREACH(p, &pg->pg_members, p_pglist) {
345                 if (p->p_stat == SSTOP) {
346                         LIST_FOREACH(p, &pg->pg_members, p_pglist) {
347                                 psignal(p, SIGHUP);
348                                 psignal(p, SIGCONT);
349                         }
350                         return;
351                 }
352         }
353 }
354
355 #include "opt_ddb.h"
356 #ifdef DDB
357 #include <ddb/ddb.h>
358
359 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
360 {
361         struct pgrp *pgrp;
362         struct proc *p;
363         int i;
364
365         for (i = 0; i <= pgrphash; i++) {
366                 if (!LIST_EMPTY(&pgrphashtbl[i])) {
367                         printf("\tindx %d\n", i);
368                         LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
369                                 printf(
370                         "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
371                                     (void *)pgrp, (long)pgrp->pg_id,
372                                     (void *)pgrp->pg_session,
373                                     pgrp->pg_session->s_count,
374                                     (void *)LIST_FIRST(&pgrp->pg_members));
375                                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
376                                         printf("\t\tpid %ld addr %p pgrp %p\n", 
377                                             (long)p->p_pid, (void *)p,
378                                             (void *)p->p_pgrp);
379                                 }
380                         }
381                 }
382         }
383 }
384 #endif /* DDB */
385
386 /*
387  * Fill in an eproc structure for the specified thread.
388  */
389 void
390 fill_eproc_td(thread_t td, struct eproc *ep, struct proc *xp)
391 {
392         bzero(ep, sizeof(*ep));
393
394         ep->e_uticks = td->td_uticks;
395         ep->e_sticks = td->td_sticks;
396         ep->e_iticks = td->td_iticks;
397         ep->e_tdev = NOUDEV;
398         ep->e_cpuid = td->td_gd->gd_cpuid;
399         if (td->td_wmesg) {
400                 strncpy(ep->e_wmesg, td->td_wmesg, WMESGLEN);
401                 ep->e_wmesg[WMESGLEN] = 0;
402         }
403
404         /*
405          * Fake up portions of the proc structure copied out by the sysctl
406          * to return useful information.  Note that using td_pri directly
407          * is messy because it includes critial section data so we fake
408          * up an rtprio.prio for threads.
409          */
410         if (xp) {
411                 *xp = *initproc;
412                 xp->p_rtprio.type = RTP_PRIO_THREAD;
413                 xp->p_rtprio.prio = td->td_pri & TDPRI_MASK;
414                 xp->p_pid = -1;
415         }
416 }
417
418 /*
419  * Fill in an eproc structure for the specified process.
420  */
421 void
422 fill_eproc(struct proc *p, struct eproc *ep)
423 {
424         struct tty *tp;
425
426         fill_eproc_td(p->p_thread, ep, NULL);
427
428         ep->e_paddr = p;
429         if (p->p_ucred) {
430                 ep->e_ucred = *p->p_ucred;
431         }
432         if (p->p_procsig) {
433                 ep->e_procsig = *p->p_procsig;
434         }
435         if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) {
436                 struct vmspace *vm = p->p_vmspace;
437                 ep->e_vm = *vm;
438                 ep->e_vm.vm_rssize = vmspace_resident_count(vm); /*XXX*/
439         }
440         if ((p->p_flag & P_INMEM) && p->p_stats)
441                 ep->e_stats = *p->p_stats;
442         if (p->p_pptr)
443                 ep->e_ppid = p->p_pptr->p_pid;
444         if (p->p_pgrp) {
445                 ep->e_pgid = p->p_pgrp->pg_id;
446                 ep->e_jobc = p->p_pgrp->pg_jobc;
447                 ep->e_sess = p->p_pgrp->pg_session;
448
449                 if (ep->e_sess) {
450                         bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login));
451                         if (ep->e_sess->s_ttyvp)
452                                 ep->e_flag = EPROC_CTTY;
453                         if (p->p_session && SESS_LEADER(p))
454                                 ep->e_flag |= EPROC_SLEADER;
455                 }
456         }
457         if ((p->p_flag & P_CONTROLT) &&
458             (ep->e_sess != NULL) &&
459             ((tp = ep->e_sess->s_ttyp) != NULL)) {
460                 ep->e_tdev = dev2udev(tp->t_dev);
461                 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
462                 ep->e_tsess = tp->t_session;
463         } else {
464                 ep->e_tdev = NOUDEV;
465         }
466 }
467
468 struct proc *
469 zpfind(pid_t pid)
470 {
471         struct proc *p;
472
473         LIST_FOREACH(p, &zombproc, p_list)
474                 if (p->p_pid == pid)
475                         return (p);
476         return (NULL);
477 }
478
479 static int
480 sysctl_out_proc(struct proc *p, struct thread *td, struct sysctl_req *req, int doingzomb)
481 {
482         struct eproc eproc;
483         struct proc xproc;
484         int error;
485 #if 0
486         pid_t pid = p->p_pid;
487 #endif
488
489         if (p) {
490                 td = p->p_thread;
491                 fill_eproc(p, &eproc);
492                 xproc = *p;
493
494                 /*
495                  * Fixup p_stat from SRUN to SSLEEP if the LWKT thread is
496                  * in a thread-blocked state.
497                  *
498                  * XXX temporary fix which might become permanent (I'd rather
499                  * not pollute the thread scheduler with knowlege about 
500                  * processes).
501                  */
502                 if (p->p_stat == SRUN && td && (td->td_flags & TDF_BLOCKED)) {
503                         xproc.p_stat = SSLEEP;
504                 }
505         } else if (td) {
506                 fill_eproc_td(td, &eproc, &xproc);
507         }
508         error = SYSCTL_OUT(req,(caddr_t)&xproc, sizeof(struct proc));
509         if (error)
510                 return (error);
511         error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc));
512         if (error)
513                 return (error);
514         error = SYSCTL_OUT(req,(caddr_t)td, sizeof(struct thread));
515         if (error)
516                 return (error);
517 #if 0
518         if (!doingzomb && pid && (pfind(pid) != p))
519                 return EAGAIN;
520         if (doingzomb && zpfind(pid) != p)
521                 return EAGAIN;
522 #endif
523         return (0);
524 }
525
526 static int
527 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
528 {
529         int *name = (int*) arg1;
530         u_int namelen = arg2;
531         struct proc *p;
532         struct thread *td;
533         int doingzomb;
534         int error = 0;
535         int n;
536         int origcpu;
537         struct ucred *cr1 = curproc->p_ucred;
538
539         if (oidp->oid_number == KERN_PROC_PID) {
540                 if (namelen != 1) 
541                         return (EINVAL);
542                 p = pfind((pid_t)name[0]);
543                 if (!p)
544                         return (0);
545                 if (!PRISON_CHECK(cr1, p->p_ucred))
546                         return (0);
547                 error = sysctl_out_proc(p, NULL, req, 0);
548                 return (error);
549         }
550         if (oidp->oid_number == KERN_PROC_ALL && !namelen)
551                 ;
552         else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
553                 ;
554         else
555                 return (EINVAL);
556         
557         if (!req->oldptr) {
558                 /* overestimate by 5 procs */
559                 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
560                 if (error)
561                         return (error);
562         }
563         for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
564                 if (!doingzomb)
565                         p = LIST_FIRST(&allproc);
566                 else
567                         p = LIST_FIRST(&zombproc);
568                 for (; p != 0; p = LIST_NEXT(p, p_list)) {
569                         /*
570                          * Show a user only their processes.
571                          */
572                         if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred))
573                                 continue;
574                         /*
575                          * Skip embryonic processes.
576                          */
577                         if (p->p_stat == SIDL)
578                                 continue;
579                         /*
580                          * TODO - make more efficient (see notes below).
581                          * do by session.
582                          */
583                         switch (oidp->oid_number) {
584                         case KERN_PROC_PGRP:
585                                 /* could do this by traversing pgrp */
586                                 if (p->p_pgrp == NULL || 
587                                     p->p_pgrp->pg_id != (pid_t)name[0])
588                                         continue;
589                                 break;
590
591                         case KERN_PROC_TTY:
592                                 if ((p->p_flag & P_CONTROLT) == 0 ||
593                                     p->p_session == NULL ||
594                                     p->p_session->s_ttyp == NULL ||
595                                     dev2udev(p->p_session->s_ttyp->t_dev) != 
596                                         (udev_t)name[0])
597                                         continue;
598                                 break;
599
600                         case KERN_PROC_UID:
601                                 if (p->p_ucred == NULL || 
602                                     p->p_ucred->cr_uid != (uid_t)name[0])
603                                         continue;
604                                 break;
605
606                         case KERN_PROC_RUID:
607                                 if (p->p_ucred == NULL || 
608                                     p->p_ucred->cr_ruid != (uid_t)name[0])
609                                         continue;
610                                 break;
611                         }
612
613                         if (!PRISON_CHECK(cr1, p->p_ucred))
614                                 continue;
615                         PHOLD(p);
616                         error = sysctl_out_proc(p, NULL, req, doingzomb);
617                         PRELE(p);
618                         if (error)
619                                 return (error);
620                 }
621         }
622
623         /*
624          * Iterate over all active cpus and scan their thread list.  Start
625          * with the next logical cpu and end with our original cpu.  We
626          * migrate our own thread to each target cpu in order to safely scan
627          * its thread list.  In the last loop we migrate back to our original
628          * cpu.
629          */
630         origcpu = mycpu->gd_cpuid;
631         for (n = 1; ps_showallthreads && n <= ncpus; ++n) {
632                 globaldata_t rgd;
633                 int nid;
634
635                 nid = (origcpu + n) % ncpus;
636                 if ((smp_active_mask & (1 << nid)) == 0)
637                         continue;
638                 rgd = globaldata_find(nid);
639                 lwkt_setcpu_self(rgd);
640                 cpu_mb1();      /* CURRENT CPU HAS CHANGED */
641
642                 TAILQ_FOREACH(td, &mycpu->gd_tdallq, td_allq) {
643                         if (td->td_proc)
644                                 continue;
645                         switch (oidp->oid_number) {
646                         case KERN_PROC_PGRP:
647                         case KERN_PROC_TTY:
648                         case KERN_PROC_UID:
649                         case KERN_PROC_RUID:
650                                 continue;
651                         default:
652                                 break;
653                         }
654                         lwkt_hold(td);
655                         error = sysctl_out_proc(NULL, td, req, doingzomb);
656                         lwkt_rele(td);
657                         if (error)
658                                 return (error);
659                 }
660         }
661         return (0);
662 }
663
664 /*
665  * This sysctl allows a process to retrieve the argument list or process
666  * title for another process without groping around in the address space
667  * of the other process.  It also allow a process to set its own "process 
668  * title to a string of its own choice.
669  */
670 static int
671 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
672 {
673         int *name = (int*) arg1;
674         u_int namelen = arg2;
675         struct proc *p;
676         struct pargs *pa;
677         int error = 0;
678         struct ucred *cr1 = curproc->p_ucred;
679
680         if (namelen != 1) 
681                 return (EINVAL);
682
683         p = pfind((pid_t)name[0]);
684         if (!p)
685                 return (0);
686
687         if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
688                 return (0);
689
690         if (req->newptr && curproc != p)
691                 return (EPERM);
692
693         if (req->oldptr && p->p_args != NULL)
694                 error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length);
695         if (req->newptr == NULL)
696                 return (error);
697
698         if (p->p_args && --p->p_args->ar_ref == 0) 
699                 FREE(p->p_args, M_PARGS);
700         p->p_args = NULL;
701
702         if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
703                 return (error);
704
705         MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen, 
706             M_PARGS, M_WAITOK);
707         pa->ar_ref = 1;
708         pa->ar_length = req->newlen;
709         error = SYSCTL_IN(req, pa->ar_args, req->newlen);
710         if (!error)
711                 p->p_args = pa;
712         else
713                 FREE(pa, M_PARGS);
714         return (error);
715 }
716
717 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
718
719 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
720         0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
721
722 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 
723         sysctl_kern_proc, "Process table");
724
725 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 
726         sysctl_kern_proc, "Process table");
727
728 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 
729         sysctl_kern_proc, "Process table");
730
731 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 
732         sysctl_kern_proc, "Process table");
733
734 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 
735         sysctl_kern_proc, "Process table");
736
737 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
738         sysctl_kern_proc_args, "Process argument list");