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