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