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