Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:28:41 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
54 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
55 MALLOC_DEFINE(M_SESSION, "session", "session header");
56 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
57 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
58
59 static int ps_showallprocs = 1;
60 SYSCTL_INT(_kern, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
61     &ps_showallprocs, 0, "");
62
63 static void pgdelete    __P((struct pgrp *));
64
65 static void     orphanpg __P((struct pgrp *pg));
66
67 /*
68  * Other process lists
69  */
70 struct pidhashhead *pidhashtbl;
71 u_long pidhash;
72 struct pgrphashhead *pgrphashtbl;
73 u_long pgrphash;
74 struct proclist allproc;
75 struct proclist zombproc;
76 vm_zone_t proc_zone;
77
78 /*
79  * Initialize global process hashing structures.
80  */
81 void
82 procinit()
83 {
84
85         LIST_INIT(&allproc);
86         LIST_INIT(&zombproc);
87         pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
88         pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
89         proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5);
90         uihashinit();
91 }
92
93 /*
94  * Is p an inferior of the current process?
95  */
96 int
97 inferior(p)
98         register struct proc *p;
99 {
100
101         for (; p != curproc; p = p->p_pptr)
102                 if (p->p_pid == 0)
103                         return (0);
104         return (1);
105 }
106
107 /*
108  * Locate a process by number
109  */
110 struct proc *
111 pfind(pid)
112         register pid_t pid;
113 {
114         register struct proc *p;
115
116         LIST_FOREACH(p, PIDHASH(pid), p_hash)
117                 if (p->p_pid == pid)
118                         return (p);
119         return (NULL);
120 }
121
122 /*
123  * Locate a process group by number
124  */
125 struct pgrp *
126 pgfind(pgid)
127         register pid_t pgid;
128 {
129         register struct pgrp *pgrp;
130
131         LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
132                 if (pgrp->pg_id == pgid)
133                         return (pgrp);
134         return (NULL);
135 }
136
137 /*
138  * Move p to a new or existing process group (and session)
139  */
140 int
141 enterpgrp(p, pgid, mksess)
142         register struct proc *p;
143         pid_t pgid;
144         int mksess;
145 {
146         register struct pgrp *pgrp = pgfind(pgid);
147
148         KASSERT(pgrp == NULL || !mksess,
149             ("enterpgrp: setsid into non-empty pgrp"));
150         KASSERT(!SESS_LEADER(p),
151             ("enterpgrp: session leader attempted setpgrp"));
152
153         if (pgrp == NULL) {
154                 pid_t savepid = p->p_pid;
155                 struct proc *np;
156                 /*
157                  * new process group
158                  */
159                 KASSERT(p->p_pid == pgid,
160                     ("enterpgrp: new pgrp and pid != pgid"));
161                 if ((np = pfind(savepid)) == NULL || np != p)
162                         return (ESRCH);
163                 MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
164                     M_WAITOK);
165                 if (mksess) {
166                         register struct session *sess;
167
168                         /*
169                          * new session
170                          */
171                         MALLOC(sess, struct session *, sizeof(struct session),
172                             M_SESSION, M_WAITOK);
173                         sess->s_leader = p;
174                         sess->s_sid = p->p_pid;
175                         sess->s_count = 1;
176                         sess->s_ttyvp = NULL;
177                         sess->s_ttyp = NULL;
178                         bcopy(p->p_session->s_login, sess->s_login,
179                             sizeof(sess->s_login));
180                         p->p_flag &= ~P_CONTROLT;
181                         pgrp->pg_session = sess;
182                         KASSERT(p == curproc,
183                             ("enterpgrp: mksession and p != curproc"));
184                 } else {
185                         pgrp->pg_session = p->p_session;
186                         pgrp->pg_session->s_count++;
187                 }
188                 pgrp->pg_id = pgid;
189                 LIST_INIT(&pgrp->pg_members);
190                 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
191                 pgrp->pg_jobc = 0;
192                 SLIST_INIT(&pgrp->pg_sigiolst);
193         } else if (pgrp == p->p_pgrp)
194                 return (0);
195
196         /*
197          * Adjust eligibility of affected pgrps to participate in job control.
198          * Increment eligibility counts before decrementing, otherwise we
199          * could reach 0 spuriously during the first call.
200          */
201         fixjobc(p, pgrp, 1);
202         fixjobc(p, p->p_pgrp, 0);
203
204         LIST_REMOVE(p, p_pglist);
205         if (LIST_EMPTY(&p->p_pgrp->pg_members))
206                 pgdelete(p->p_pgrp);
207         p->p_pgrp = pgrp;
208         LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
209         return (0);
210 }
211
212 /*
213  * remove process from process group
214  */
215 int
216 leavepgrp(p)
217         register struct proc *p;
218 {
219
220         LIST_REMOVE(p, p_pglist);
221         if (LIST_EMPTY(&p->p_pgrp->pg_members))
222                 pgdelete(p->p_pgrp);
223         p->p_pgrp = 0;
224         return (0);
225 }
226
227 /*
228  * delete a process group
229  */
230 static void
231 pgdelete(pgrp)
232         register struct pgrp *pgrp;
233 {
234
235         /*
236          * Reset any sigio structures pointing to us as a result of
237          * F_SETOWN with our pgid.
238          */
239         funsetownlst(&pgrp->pg_sigiolst);
240
241         if (pgrp->pg_session->s_ttyp != NULL &&
242             pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
243                 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
244         LIST_REMOVE(pgrp, pg_hash);
245         if (--pgrp->pg_session->s_count == 0)
246                 FREE(pgrp->pg_session, M_SESSION);
247         FREE(pgrp, M_PGRP);
248 }
249
250 /*
251  * Adjust pgrp jobc counters when specified process changes process group.
252  * We count the number of processes in each process group that "qualify"
253  * the group for terminal job control (those with a parent in a different
254  * process group of the same session).  If that count reaches zero, the
255  * process group becomes orphaned.  Check both the specified process'
256  * process group and that of its children.
257  * entering == 0 => p is leaving specified group.
258  * entering == 1 => p is entering specified group.
259  */
260 void
261 fixjobc(p, pgrp, entering)
262         register struct proc *p;
263         register struct pgrp *pgrp;
264         int entering;
265 {
266         register struct pgrp *hispgrp;
267         register struct session *mysession = pgrp->pg_session;
268
269         /*
270          * Check p's parent to see whether p qualifies its own process
271          * group; if so, adjust count for p's process group.
272          */
273         if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
274             hispgrp->pg_session == mysession) {
275                 if (entering)
276                         pgrp->pg_jobc++;
277                 else if (--pgrp->pg_jobc == 0)
278                         orphanpg(pgrp);
279         }
280
281         /*
282          * Check this process' children to see whether they qualify
283          * their process groups; if so, adjust counts for children's
284          * process groups.
285          */
286         LIST_FOREACH(p, &p->p_children, p_sibling)
287                 if ((hispgrp = p->p_pgrp) != pgrp &&
288                     hispgrp->pg_session == mysession &&
289                     p->p_stat != SZOMB) {
290                         if (entering)
291                                 hispgrp->pg_jobc++;
292                         else if (--hispgrp->pg_jobc == 0)
293                                 orphanpg(hispgrp);
294                 }
295 }
296
297 /*
298  * A process group has become orphaned;
299  * if there are any stopped processes in the group,
300  * hang-up all process in that group.
301  */
302 static void
303 orphanpg(pg)
304         struct pgrp *pg;
305 {
306         register struct proc *p;
307
308         LIST_FOREACH(p, &pg->pg_members, p_pglist) {
309                 if (p->p_stat == SSTOP) {
310                         LIST_FOREACH(p, &pg->pg_members, p_pglist) {
311                                 psignal(p, SIGHUP);
312                                 psignal(p, SIGCONT);
313                         }
314                         return;
315                 }
316         }
317 }
318
319 #include "opt_ddb.h"
320 #ifdef DDB
321 #include <ddb/ddb.h>
322
323 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
324 {
325         register struct pgrp *pgrp;
326         register struct proc *p;
327         register int i;
328
329         for (i = 0; i <= pgrphash; i++) {
330                 if (!LIST_EMPTY(&pgrphashtbl[i])) {
331                         printf("\tindx %d\n", i);
332                         LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
333                                 printf(
334                         "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
335                                     (void *)pgrp, (long)pgrp->pg_id,
336                                     (void *)pgrp->pg_session,
337                                     pgrp->pg_session->s_count,
338                                     (void *)LIST_FIRST(&pgrp->pg_members));
339                                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
340                                         printf("\t\tpid %ld addr %p pgrp %p\n", 
341                                             (long)p->p_pid, (void *)p,
342                                             (void *)p->p_pgrp);
343                                 }
344                         }
345                 }
346         }
347 }
348 #endif /* DDB */
349
350 /*
351  * Fill in an eproc structure for the specified process.
352  */
353 void
354 fill_eproc(p, ep)
355         register struct proc *p;
356         register struct eproc *ep;
357 {
358         register struct tty *tp;
359
360         bzero(ep, sizeof(*ep));
361
362         ep->e_paddr = p;
363         if (p->p_cred) {
364                 ep->e_pcred = *p->p_cred;
365                 if (p->p_ucred)
366                         ep->e_ucred = *p->p_ucred;
367         }
368         if (p->p_procsig) {
369                 ep->e_procsig = *p->p_procsig;
370         }
371         if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) {
372                 register struct vmspace *vm = p->p_vmspace;
373                 ep->e_vm = *vm;
374                 ep->e_vm.vm_rssize = vmspace_resident_count(vm); /*XXX*/
375         }
376         if ((p->p_flag & P_INMEM) && p->p_stats)
377                 ep->e_stats = *p->p_stats;
378         if (p->p_pptr)
379                 ep->e_ppid = p->p_pptr->p_pid;
380         if (p->p_pgrp) {
381                 ep->e_pgid = p->p_pgrp->pg_id;
382                 ep->e_jobc = p->p_pgrp->pg_jobc;
383                 ep->e_sess = p->p_pgrp->pg_session;
384
385                 if (ep->e_sess) {
386                         bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login));
387                         if (ep->e_sess->s_ttyvp)
388                                 ep->e_flag = EPROC_CTTY;
389                         if (p->p_session && SESS_LEADER(p))
390                                 ep->e_flag |= EPROC_SLEADER;
391                 }
392         }
393         if ((p->p_flag & P_CONTROLT) &&
394             (ep->e_sess != NULL) &&
395             ((tp = ep->e_sess->s_ttyp) != NULL)) {
396                 ep->e_tdev = dev2udev(tp->t_dev);
397                 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
398                 ep->e_tsess = tp->t_session;
399         } else
400                 ep->e_tdev = NOUDEV;
401         if (p->p_wmesg) {
402                 strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
403                 ep->e_wmesg[WMESGLEN] = 0;
404         }
405 }
406
407 struct proc *
408 zpfind(pid_t pid)
409 {
410         struct proc *p;
411
412         LIST_FOREACH(p, &zombproc, p_list)
413                 if (p->p_pid == pid)
414                         return (p);
415         return (NULL);
416 }
417
418
419 static int
420 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb)
421 {
422         struct eproc eproc;
423         int error;
424         pid_t pid = p->p_pid;
425
426         fill_eproc(p, &eproc);
427         error = SYSCTL_OUT(req,(caddr_t)p, sizeof(struct proc));
428         if (error)
429                 return (error);
430         error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc));
431         if (error)
432                 return (error);
433         if (!doingzomb && pid && (pfind(pid) != p))
434                 return EAGAIN;
435         if (doingzomb && zpfind(pid) != p)
436                 return EAGAIN;
437         return (0);
438 }
439
440 static int
441 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
442 {
443         int *name = (int*) arg1;
444         u_int namelen = arg2;
445         struct proc *p;
446         int doingzomb;
447         int error = 0;
448
449         if (oidp->oid_number == KERN_PROC_PID) {
450                 if (namelen != 1) 
451                         return (EINVAL);
452                 p = pfind((pid_t)name[0]);
453                 if (!p)
454                         return (0);
455                 if (!PRISON_CHECK(curproc, p))
456                         return (0);
457                 error = sysctl_out_proc(p, req, 0);
458                 return (error);
459         }
460         if (oidp->oid_number == KERN_PROC_ALL && !namelen)
461                 ;
462         else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
463                 ;
464         else
465                 return (EINVAL);
466         
467         if (!req->oldptr) {
468                 /* overestimate by 5 procs */
469                 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
470                 if (error)
471                         return (error);
472         }
473         for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
474                 if (!doingzomb)
475                         p = LIST_FIRST(&allproc);
476                 else
477                         p = LIST_FIRST(&zombproc);
478                 for (; p != 0; p = LIST_NEXT(p, p_list)) {
479                         /*
480                          * Show a user only their processes.
481                          */
482                         if ((!ps_showallprocs) && p_trespass(curproc, p))
483                                 continue;
484                         /*
485                          * Skip embryonic processes.
486                          */
487                         if (p->p_stat == SIDL)
488                                 continue;
489                         /*
490                          * TODO - make more efficient (see notes below).
491                          * do by session.
492                          */
493                         switch (oidp->oid_number) {
494
495                         case KERN_PROC_PGRP:
496                                 /* could do this by traversing pgrp */
497                                 if (p->p_pgrp == NULL || 
498                                     p->p_pgrp->pg_id != (pid_t)name[0])
499                                         continue;
500                                 break;
501
502                         case KERN_PROC_TTY:
503                                 if ((p->p_flag & P_CONTROLT) == 0 ||
504                                     p->p_session == NULL ||
505                                     p->p_session->s_ttyp == NULL ||
506                                     dev2udev(p->p_session->s_ttyp->t_dev) != 
507                                         (udev_t)name[0])
508                                         continue;
509                                 break;
510
511                         case KERN_PROC_UID:
512                                 if (p->p_ucred == NULL || 
513                                     p->p_ucred->cr_uid != (uid_t)name[0])
514                                         continue;
515                                 break;
516
517                         case KERN_PROC_RUID:
518                                 if (p->p_ucred == NULL || 
519                                     p->p_cred->p_ruid != (uid_t)name[0])
520                                         continue;
521                                 break;
522                         }
523
524                         if (!PRISON_CHECK(curproc, p))
525                                 continue;
526
527                         error = sysctl_out_proc(p, req, doingzomb);
528                         if (error)
529                                 return (error);
530                 }
531         }
532         return (0);
533 }
534
535 /*
536  * This sysctl allows a process to retrieve the argument list or process
537  * title for another process without groping around in the address space
538  * of the other process.  It also allow a process to set its own "process 
539  * title to a string of its own choice.
540  */
541 static int
542 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
543 {
544         int *name = (int*) arg1;
545         u_int namelen = arg2;
546         struct proc *p;
547         struct pargs *pa;
548         int error = 0;
549
550         if (namelen != 1) 
551                 return (EINVAL);
552
553         p = pfind((pid_t)name[0]);
554         if (!p)
555                 return (0);
556
557         if ((!ps_argsopen) && p_trespass(curproc, p))
558                 return (0);
559
560         if (req->newptr && curproc != p)
561                 return (EPERM);
562
563         if (req->oldptr && p->p_args != NULL)
564                 error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length);
565         if (req->newptr == NULL)
566                 return (error);
567
568         if (p->p_args && --p->p_args->ar_ref == 0) 
569                 FREE(p->p_args, M_PARGS);
570         p->p_args = NULL;
571
572         if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
573                 return (error);
574
575         MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen, 
576             M_PARGS, M_WAITOK);
577         pa->ar_ref = 1;
578         pa->ar_length = req->newlen;
579         error = SYSCTL_IN(req, pa->ar_args, req->newlen);
580         if (!error)
581                 p->p_args = pa;
582         else
583                 FREE(pa, M_PARGS);
584         return (error);
585 }
586
587 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
588
589 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
590         0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
591
592 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 
593         sysctl_kern_proc, "Process table");
594
595 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 
596         sysctl_kern_proc, "Process table");
597
598 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 
599         sysctl_kern_proc, "Process table");
600
601 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 
602         sysctl_kern_proc, "Process table");
603
604 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 
605         sysctl_kern_proc, "Process table");
606
607 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
608         sysctl_kern_proc_args, "Process argument list");