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