xref: /freebsd/sys/kern/kern_proc.c (revision a1a4f1a0d87b594d3f17a97dc0127eec1417e6f6)
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 };
68 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
69 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
70 static u_long uihash;		/* size of hash table - 1 */
71 
72 static void	orphanpg __P((struct pgrp *pg));
73 
74 /*
75  * Other process lists
76  */
77 struct pidhashhead *pidhashtbl;
78 u_long pidhash;
79 struct pgrphashhead *pgrphashtbl;
80 u_long pgrphash;
81 struct proclist allproc;
82 struct proclist zombproc;
83 vm_zone_t proc_zone;
84 
85 /*
86  * Initialize global process hashing structures.
87  */
88 void
89 procinit()
90 {
91 
92 	LIST_INIT(&allproc);
93 	LIST_INIT(&zombproc);
94 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
95 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
96 	uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash);
97 	proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5);
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 	KASSERT(pgrp == NULL || !mksess,
195 	    ("enterpgrp: setsid into non-empty pgrp"));
196 	KASSERT(!SESS_LEADER(p),
197 	    ("enterpgrp: session leader attempted setpgrp"));
198 
199 	if (pgrp == NULL) {
200 		pid_t savepid = p->p_pid;
201 		struct proc *np;
202 		/*
203 		 * new process group
204 		 */
205 		KASSERT(p->p_pid == pgid,
206 		    ("enterpgrp: new pgrp and pid != pgid"));
207 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
208 		    M_WAITOK);
209 		if ((np = pfind(savepid)) == NULL || np != p)
210 			return (ESRCH);
211 		if (mksess) {
212 			register struct session *sess;
213 
214 			/*
215 			 * new session
216 			 */
217 			MALLOC(sess, struct session *, sizeof(struct session),
218 			    M_SESSION, M_WAITOK);
219 			sess->s_leader = p;
220 			sess->s_sid = p->p_pid;
221 			sess->s_count = 1;
222 			sess->s_ttyvp = NULL;
223 			sess->s_ttyp = NULL;
224 			bcopy(p->p_session->s_login, sess->s_login,
225 			    sizeof(sess->s_login));
226 			p->p_flag &= ~P_CONTROLT;
227 			pgrp->pg_session = sess;
228 			KASSERT(p == curproc,
229 			    ("enterpgrp: mksession and p != curproc"));
230 		} else {
231 			pgrp->pg_session = p->p_session;
232 			pgrp->pg_session->s_count++;
233 		}
234 		pgrp->pg_id = pgid;
235 		LIST_INIT(&pgrp->pg_members);
236 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
237 		pgrp->pg_jobc = 0;
238 		SLIST_INIT(&pgrp->pg_sigiolst);
239 	} else if (pgrp == p->p_pgrp)
240 		return (0);
241 
242 	/*
243 	 * Adjust eligibility of affected pgrps to participate in job control.
244 	 * Increment eligibility counts before decrementing, otherwise we
245 	 * could reach 0 spuriously during the first call.
246 	 */
247 	fixjobc(p, pgrp, 1);
248 	fixjobc(p, p->p_pgrp, 0);
249 
250 	LIST_REMOVE(p, p_pglist);
251 	if (p->p_pgrp->pg_members.lh_first == 0)
252 		pgdelete(p->p_pgrp);
253 	p->p_pgrp = pgrp;
254 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
255 	return (0);
256 }
257 
258 /*
259  * remove process from process group
260  */
261 int
262 leavepgrp(p)
263 	register struct proc *p;
264 {
265 
266 	LIST_REMOVE(p, p_pglist);
267 	if (p->p_pgrp->pg_members.lh_first == 0)
268 		pgdelete(p->p_pgrp);
269 	p->p_pgrp = 0;
270 	return (0);
271 }
272 
273 /*
274  * delete a process group
275  */
276 static void
277 pgdelete(pgrp)
278 	register struct pgrp *pgrp;
279 {
280 
281 	/*
282 	 * Reset any sigio structures pointing to us as a result of
283 	 * F_SETOWN with our pgid.
284 	 */
285 	funsetownlst(&pgrp->pg_sigiolst);
286 
287 	if (pgrp->pg_session->s_ttyp != NULL &&
288 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
289 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
290 	LIST_REMOVE(pgrp, pg_hash);
291 	if (--pgrp->pg_session->s_count == 0)
292 		FREE(pgrp->pg_session, M_SESSION);
293 	FREE(pgrp, M_PGRP);
294 }
295 
296 /*
297  * Adjust pgrp jobc counters when specified process changes process group.
298  * We count the number of processes in each process group that "qualify"
299  * the group for terminal job control (those with a parent in a different
300  * process group of the same session).  If that count reaches zero, the
301  * process group becomes orphaned.  Check both the specified process'
302  * process group and that of its children.
303  * entering == 0 => p is leaving specified group.
304  * entering == 1 => p is entering specified group.
305  */
306 void
307 fixjobc(p, pgrp, entering)
308 	register struct proc *p;
309 	register struct pgrp *pgrp;
310 	int entering;
311 {
312 	register struct pgrp *hispgrp;
313 	register struct session *mysession = pgrp->pg_session;
314 
315 	/*
316 	 * Check p's parent to see whether p qualifies its own process
317 	 * group; if so, adjust count for p's process group.
318 	 */
319 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
320 	    hispgrp->pg_session == mysession) {
321 		if (entering)
322 			pgrp->pg_jobc++;
323 		else if (--pgrp->pg_jobc == 0)
324 			orphanpg(pgrp);
325 	}
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 /*
344  * A process group has become orphaned;
345  * if there are any stopped processes in the group,
346  * hang-up all process in that group.
347  */
348 static void
349 orphanpg(pg)
350 	struct pgrp *pg;
351 {
352 	register struct proc *p;
353 
354 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
355 		if (p->p_stat == SSTOP) {
356 			for (p = pg->pg_members.lh_first; p != 0;
357 			    p = p->p_pglist.le_next) {
358 				psignal(p, SIGHUP);
359 				psignal(p, SIGCONT);
360 			}
361 			return;
362 		}
363 	}
364 }
365 
366 #include "opt_ddb.h"
367 #ifdef DDB
368 #include <ddb/ddb.h>
369 
370 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
371 {
372 	register struct pgrp *pgrp;
373 	register struct proc *p;
374 	register int i;
375 
376 	for (i = 0; i <= pgrphash; i++) {
377 		if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
378 			printf("\tindx %d\n", i);
379 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
380 				printf(
381 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
382 				    (void *)pgrp, (long)pgrp->pg_id,
383 				    (void *)pgrp->pg_session,
384 				    pgrp->pg_session->s_count,
385 				    (void *)pgrp->pg_members.lh_first);
386 				for (p = pgrp->pg_members.lh_first; p != 0;
387 				    p = p->p_pglist.le_next) {
388 					printf("\t\tpid %ld addr %p pgrp %p\n",
389 					    (long)p->p_pid, (void *)p,
390 					    (void *)p->p_pgrp);
391 				}
392 			}
393 		}
394 	}
395 }
396 #endif /* DDB */
397 
398 /*
399  * Fill in an eproc structure for the specified process.
400  */
401 void
402 fill_eproc(p, ep)
403 	register struct proc *p;
404 	register struct eproc *ep;
405 {
406 	register struct tty *tp;
407 
408 	bzero(ep, sizeof(*ep));
409 
410 	ep->e_paddr = p;
411 	if (p->p_cred) {
412 		ep->e_pcred = *p->p_cred;
413 		if (p->p_ucred)
414 			ep->e_ucred = *p->p_ucred;
415 	}
416 	if (p->p_procsig){
417 		ep->e_procsig = *p->p_procsig;
418 	}
419 	if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) {
420 		register struct vmspace *vm = p->p_vmspace;
421 		ep->e_vm = *vm;
422 		ep->e_vm.vm_rssize = vmspace_resident_count(vm); /*XXX*/
423 	}
424 	if (p->p_pptr)
425 		ep->e_ppid = p->p_pptr->p_pid;
426 	if (p->p_pgrp) {
427 		ep->e_pgid = p->p_pgrp->pg_id;
428 		ep->e_jobc = p->p_pgrp->pg_jobc;
429 		ep->e_sess = p->p_pgrp->pg_session;
430 
431 		if (ep->e_sess) {
432 			bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login));
433 			if (ep->e_sess->s_ttyvp)
434 				ep->e_flag = EPROC_CTTY;
435 			if (p->p_session && SESS_LEADER(p))
436 				ep->e_flag |= EPROC_SLEADER;
437 		}
438 	}
439 	if ((p->p_flag & P_CONTROLT) &&
440 	    (ep->e_sess != NULL) &&
441 	    ((tp = ep->e_sess->s_ttyp) != NULL)) {
442 		ep->e_tdev = dev2udev(tp->t_dev);
443 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
444 		ep->e_tsess = tp->t_session;
445 	} else
446 		ep->e_tdev = NOUDEV;
447 	if (p->p_wmesg) {
448 		strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
449 		ep->e_wmesg[WMESGLEN] = 0;
450 	}
451 }
452 
453 static struct proc *
454 zpfind(pid_t pid)
455 {
456 	struct proc *p;
457 
458 	for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
459 		if (p->p_pid == pid)
460 			return (p);
461 	return (NULL);
462 }
463 
464 
465 static int
466 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb)
467 {
468 	struct eproc eproc;
469 	int error;
470 	pid_t pid = p->p_pid;
471 
472 	fill_eproc(p, &eproc);
473 	error = SYSCTL_OUT(req,(caddr_t)p, sizeof(struct proc));
474 	if (error)
475 		return (error);
476 	error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc));
477 	if (error)
478 		return (error);
479 	if (!doingzomb && pid && (pfind(pid) != p))
480 		return EAGAIN;
481 	if (doingzomb && zpfind(pid) != p)
482 		return EAGAIN;
483 	return (0);
484 }
485 
486 static int
487 sysctl_kern_proc SYSCTL_HANDLER_ARGS
488 {
489 	int *name = (int*) arg1;
490 	u_int namelen = arg2;
491 	struct proc *p;
492 	int doingzomb;
493 	int error = 0;
494 
495 	if (oidp->oid_number == KERN_PROC_PID) {
496 		if (namelen != 1)
497 			return (EINVAL);
498 		p = pfind((pid_t)name[0]);
499 		if (!p)
500 			return (0);
501 		if (!PRISON_CHECK(curproc, p))
502 			return (0);
503 		error = sysctl_out_proc(p, req, 0);
504 		return (error);
505 	}
506 	if (oidp->oid_number == KERN_PROC_ALL && !namelen)
507 		;
508 	else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
509 		;
510 	else
511 		return (EINVAL);
512 
513 	if (!req->oldptr) {
514 		/* overestimate by 5 procs */
515 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
516 		if (error)
517 			return (error);
518 	}
519 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
520 		if (!doingzomb)
521 			p = allproc.lh_first;
522 		else
523 			p = zombproc.lh_first;
524 		for (; p != 0; p = p->p_list.le_next) {
525 			/*
526 			 * Skip embryonic processes.
527 			 */
528 			if (p->p_stat == SIDL)
529 				continue;
530 			/*
531 			 * TODO - make more efficient (see notes below).
532 			 * do by session.
533 			 */
534 			switch (oidp->oid_number) {
535 
536 			case KERN_PROC_PGRP:
537 				/* could do this by traversing pgrp */
538 				if (p->p_pgrp == NULL ||
539 				    p->p_pgrp->pg_id != (pid_t)name[0])
540 					continue;
541 				break;
542 
543 			case KERN_PROC_TTY:
544 				if ((p->p_flag & P_CONTROLT) == 0 ||
545 				    p->p_session == NULL ||
546 				    p->p_session->s_ttyp == NULL ||
547 				    dev2udev(p->p_session->s_ttyp->t_dev) !=
548 					(udev_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 			if (!PRISON_CHECK(curproc, p))
566 				continue;
567 
568 			error = sysctl_out_proc(p, req, doingzomb);
569 			if (error)
570 				return (error);
571 		}
572 	}
573 	return (0);
574 }
575 
576 
577 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
578 
579 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
580 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
581 
582 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
583 	sysctl_kern_proc, "Process table");
584 
585 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
586 	sysctl_kern_proc, "Process table");
587 
588 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
589 	sysctl_kern_proc, "Process table");
590 
591 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
592 	sysctl_kern_proc, "Process table");
593 
594 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
595 	sysctl_kern_proc, "Process table");
596