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