xref: /freebsd/sys/kern/kern_proc.c (revision 952d112864d8008aa87278a30a539d888a8493cd)
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.25 1997/02/22 09:39:08 peter 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/buf.h>
43 #include <sys/acct.h>
44 #include <sys/wait.h>
45 #include <sys/file.h>
46 #include <ufs/ufs/quota.h>
47 #include <sys/uio.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/tty.h>
51 #include <sys/signalvar.h>
52 #include <vm/vm.h>
53 #include <vm/vm_param.h>
54 #include <vm/vm_prot.h>
55 #include <sys/lock.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_map.h>
58 #include <sys/user.h>
59 
60 struct prochd qs[NQS];		/* as good a place as any... */
61 struct prochd rtqs[NQS];	/* Space for REALTIME queues too */
62 struct prochd idqs[NQS];	/* Space for IDLE queues too */
63 
64 static void pgdelete	__P((struct pgrp *));
65 
66 /*
67  * Structure associated with user cacheing.
68  */
69 struct uidinfo {
70 	LIST_ENTRY(uidinfo) ui_hash;
71 	uid_t	ui_uid;
72 	long	ui_proccnt;
73 };
74 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
75 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
76 static u_long uihash;		/* size of hash table - 1 */
77 
78 static void	orphanpg __P((struct pgrp *pg));
79 
80 /*
81  * Other process lists
82  */
83 struct pidhashhead *pidhashtbl;
84 u_long pidhash;
85 struct pgrphashhead *pgrphashtbl;
86 u_long pgrphash;
87 struct proclist allproc;
88 struct proclist zombproc;
89 
90 /*
91  * Initialize global process hashing structures.
92  */
93 void
94 procinit()
95 {
96 
97 	LIST_INIT(&allproc);
98 	LIST_INIT(&zombproc);
99 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
100 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
101 	uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash);
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 #ifdef DIAGNOSTIC
199 	if (pgrp != NULL && mksess)	/* firewalls */
200 		panic("enterpgrp: setsid into non-empty pgrp");
201 	if (SESS_LEADER(p))
202 		panic("enterpgrp: session leader attempted setpgrp");
203 #endif
204 	if (pgrp == NULL) {
205 		pid_t savepid = p->p_pid;
206 		struct proc *np;
207 		/*
208 		 * new process group
209 		 */
210 #ifdef DIAGNOSTIC
211 		if (p->p_pid != pgid)
212 			panic("enterpgrp: new pgrp and pid != pgid");
213 #endif
214 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
215 		    M_WAITOK);
216 		if ((np = pfind(savepid)) == NULL || np != p)
217 			return (ESRCH);
218 		if (mksess) {
219 			register struct session *sess;
220 
221 			/*
222 			 * new session
223 			 */
224 			MALLOC(sess, struct session *, sizeof(struct session),
225 			    M_SESSION, M_WAITOK);
226 			sess->s_leader = p;
227 			sess->s_count = 1;
228 			sess->s_ttyvp = NULL;
229 			sess->s_ttyp = NULL;
230 			bcopy(p->p_session->s_login, sess->s_login,
231 			    sizeof(sess->s_login));
232 			p->p_flag &= ~P_CONTROLT;
233 			pgrp->pg_session = sess;
234 #ifdef DIAGNOSTIC
235 			if (p != curproc)
236 				panic("enterpgrp: mksession and p != curproc");
237 #endif
238 		} else {
239 			pgrp->pg_session = p->p_session;
240 			pgrp->pg_session->s_count++;
241 		}
242 		pgrp->pg_id = pgid;
243 		LIST_INIT(&pgrp->pg_members);
244 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
245 		pgrp->pg_jobc = 0;
246 	} else if (pgrp == p->p_pgrp)
247 		return (0);
248 
249 	/*
250 	 * Adjust eligibility of affected pgrps to participate in job control.
251 	 * Increment eligibility counts before decrementing, otherwise we
252 	 * could reach 0 spuriously during the first call.
253 	 */
254 	fixjobc(p, pgrp, 1);
255 	fixjobc(p, p->p_pgrp, 0);
256 
257 	LIST_REMOVE(p, p_pglist);
258 	if (p->p_pgrp->pg_members.lh_first == 0)
259 		pgdelete(p->p_pgrp);
260 	p->p_pgrp = pgrp;
261 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
262 	return (0);
263 }
264 
265 /*
266  * remove process from process group
267  */
268 int
269 leavepgrp(p)
270 	register struct proc *p;
271 {
272 
273 	LIST_REMOVE(p, p_pglist);
274 	if (p->p_pgrp->pg_members.lh_first == 0)
275 		pgdelete(p->p_pgrp);
276 	p->p_pgrp = 0;
277 	return (0);
278 }
279 
280 /*
281  * delete a process group
282  */
283 static void
284 pgdelete(pgrp)
285 	register struct pgrp *pgrp;
286 {
287 
288 	if (pgrp->pg_session->s_ttyp != NULL &&
289 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
290 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
291 	LIST_REMOVE(pgrp, pg_hash);
292 	if (--pgrp->pg_session->s_count == 0)
293 		FREE(pgrp->pg_session, M_SESSION);
294 	FREE(pgrp, M_PGRP);
295 }
296 
297 /*
298  * Adjust pgrp jobc counters when specified process changes process group.
299  * We count the number of processes in each process group that "qualify"
300  * the group for terminal job control (those with a parent in a different
301  * process group of the same session).  If that count reaches zero, the
302  * process group becomes orphaned.  Check both the specified process'
303  * process group and that of its children.
304  * entering == 0 => p is leaving specified group.
305  * entering == 1 => p is entering specified group.
306  */
307 void
308 fixjobc(p, pgrp, entering)
309 	register struct proc *p;
310 	register struct pgrp *pgrp;
311 	int entering;
312 {
313 	register struct pgrp *hispgrp;
314 	register struct session *mysession = pgrp->pg_session;
315 
316 	/*
317 	 * Check p's parent to see whether p qualifies its own process
318 	 * group; if so, adjust count for p's process group.
319 	 */
320 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
321 	    hispgrp->pg_session == mysession)
322 		if (entering)
323 			pgrp->pg_jobc++;
324 		else if (--pgrp->pg_jobc == 0)
325 			orphanpg(pgrp);
326 
327 	/*
328 	 * Check this process' children to see whether they qualify
329 	 * their process groups; if so, adjust counts for children's
330 	 * process groups.
331 	 */
332 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next)
333 		if ((hispgrp = p->p_pgrp) != pgrp &&
334 		    hispgrp->pg_session == mysession &&
335 		    p->p_stat != SZOMB)
336 			if (entering)
337 				hispgrp->pg_jobc++;
338 			else if (--hispgrp->pg_jobc == 0)
339 				orphanpg(hispgrp);
340 }
341 
342 /*
343  * A process group has become orphaned;
344  * if there are any stopped processes in the group,
345  * hang-up all process in that group.
346  */
347 static void
348 orphanpg(pg)
349 	struct pgrp *pg;
350 {
351 	register struct proc *p;
352 
353 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
354 		if (p->p_stat == SSTOP) {
355 			for (p = pg->pg_members.lh_first; p != 0;
356 			    p = p->p_pglist.le_next) {
357 				psignal(p, SIGHUP);
358 				psignal(p, SIGCONT);
359 			}
360 			return;
361 		}
362 	}
363 }
364 
365 #include "opt_ddb.h"
366 #ifdef DDB
367 #include <ddb/ddb.h>
368 
369 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
370 {
371 	register struct pgrp *pgrp;
372 	register struct proc *p;
373 	register i;
374 
375 	for (i = 0; i <= pgrphash; i++) {
376 		if (pgrp = pgrphashtbl[i].lh_first) {
377 			printf("\tindx %d\n", i);
378 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
379 				printf("\tpgrp %x, pgid %d, sess %x, sesscnt %d, mem %x\n",
380 				    pgrp, pgrp->pg_id, pgrp->pg_session,
381 				    pgrp->pg_session->s_count,
382 				    pgrp->pg_members.lh_first);
383 				for (p = pgrp->pg_members.lh_first; p != 0;
384 				    p = p->p_pglist.le_next) {
385 					printf("\t\tpid %d addr %x pgrp %x\n",
386 					    p->p_pid, p, p->p_pgrp);
387 				}
388 			}
389 		}
390 	}
391 }
392 #endif /* DDB */
393 
394 /*
395  * Fill in an eproc structure for the specified process.
396  */
397 void
398 fill_eproc(p, ep)
399 	register struct proc *p;
400 	register struct eproc *ep;
401 {
402 	register struct tty *tp;
403 
404 	bzero(ep, sizeof(*ep));
405 
406 	ep->e_paddr = p;
407 	if (p->p_cred) {
408 		ep->e_pcred = *p->p_cred;
409 		if (p->p_ucred)
410 			ep->e_ucred = *p->p_ucred;
411 	}
412 	if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) {
413 		register struct vmspace *vm = p->p_vmspace;
414 
415 #ifdef pmap_resident_count
416 		ep->e_vm.vm_rssize = pmap_resident_count(&vm->vm_pmap); /*XXX*/
417 #else
418 		ep->e_vm.vm_rssize = vm->vm_rssize;
419 #endif
420 		ep->e_vm.vm_tsize = vm->vm_tsize;
421 		ep->e_vm.vm_dsize = vm->vm_dsize;
422 		ep->e_vm.vm_ssize = vm->vm_ssize;
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