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