xref: /openbsd-src/sys/kern/kern_proc.c (revision 48950c12d106c85f315112191a0228d7b83b9510)
1 /*	$OpenBSD: kern_proc.c,v 1.50 2013/02/17 17:39:29 miod Exp $	*/
2 /*	$NetBSD: kern_proc.c,v 1.14 1996/02/09 18:59:41 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)kern_proc.c	8.4 (Berkeley) 1/4/94
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/proc.h>
39 #include <sys/buf.h>
40 #include <sys/acct.h>
41 #include <sys/wait.h>
42 #include <sys/file.h>
43 #include <ufs/ufs/quota.h>
44 #include <sys/uio.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/ioctl.h>
48 #include <sys/tty.h>
49 #include <sys/signalvar.h>
50 #include <sys/pool.h>
51 
52 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
53 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
54 u_long uihash;		/* size of hash table - 1 */
55 
56 /*
57  * Other process lists
58  */
59 struct pidhashhead *pidhashtbl;
60 u_long pidhash;
61 struct pgrphashhead *pgrphashtbl;
62 u_long pgrphash;
63 struct proclist allproc;
64 struct proclist zombproc;
65 
66 struct pool proc_pool;
67 struct pool process_pool;
68 struct pool rusage_pool;
69 struct pool ucred_pool;
70 struct pool pgrp_pool;
71 struct pool session_pool;
72 struct pool pcred_pool;
73 
74 static void orphanpg(struct pgrp *);
75 #ifdef DEBUG
76 void pgrpdump(void);
77 #endif
78 
79 /*
80  * Initialize global process hashing structures.
81  */
82 void
83 procinit(void)
84 {
85 	LIST_INIT(&allproc);
86 	LIST_INIT(&zombproc);
87 
88 
89 	pidhashtbl = hashinit(maxthread / 4, M_PROC, M_NOWAIT, &pidhash);
90 	pgrphashtbl = hashinit(maxprocess / 4, M_PROC, M_NOWAIT, &pgrphash);
91 	uihashtbl = hashinit(maxprocess / 16, M_PROC, M_NOWAIT, &uihash);
92 	if (!pidhashtbl || !pgrphashtbl || !uihashtbl)
93 		panic("procinit: malloc");
94 
95 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
96 	    &pool_allocator_nointr);
97 	pool_init(&process_pool, sizeof(struct process), 0, 0, 0, "processpl",
98 	    &pool_allocator_nointr);
99 	pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "zombiepl",
100 	    &pool_allocator_nointr);
101 	pool_init(&ucred_pool, sizeof(struct ucred), 0, 0, 0, "ucredpl",
102 	    &pool_allocator_nointr);
103 	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
104 	    &pool_allocator_nointr);
105 	pool_init(&session_pool, sizeof(struct session), 0, 0, 0, "sessionpl",
106 	    &pool_allocator_nointr);
107 	pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
108 	    &pool_allocator_nointr);
109 }
110 
111 struct uidinfo *
112 uid_find(uid_t uid)
113 {
114 	struct uidinfo *uip, *nuip;
115 	struct uihashhead *uipp;
116 
117 	uipp = UIHASH(uid);
118 	LIST_FOREACH(uip, uipp, ui_hash)
119 		if (uip->ui_uid == uid)
120 			break;
121 	if (uip)
122 		return (uip);
123 	nuip = malloc(sizeof(*nuip), M_PROC, M_WAITOK|M_ZERO);
124 	LIST_FOREACH(uip, uipp, ui_hash)
125 		if (uip->ui_uid == uid)
126 			break;
127 	if (uip) {
128 		free(nuip, M_PROC);
129 		return (uip);
130 	}
131 	nuip->ui_uid = uid;
132 	LIST_INSERT_HEAD(uipp, nuip, ui_hash);
133 
134 	return (nuip);
135 }
136 
137 /*
138  * Change the count associated with number of threads
139  * a given user is using.
140  */
141 int
142 chgproccnt(uid_t uid, int diff)
143 {
144 	struct uidinfo *uip;
145 
146 	uip = uid_find(uid);
147 	uip->ui_proccnt += diff;
148 	if (uip->ui_proccnt < 0)
149 		panic("chgproccnt: procs < 0");
150 	return (uip->ui_proccnt);
151 }
152 
153 /*
154  * Is p an inferior of parent?
155  */
156 int
157 inferior(struct process *pr, struct process *parent)
158 {
159 
160 	for (; pr != parent; pr = pr->ps_pptr)
161 		if (pr->ps_pid == 0 || pr->ps_pid == 1)
162 			return (0);
163 	return (1);
164 }
165 
166 /*
167  * Locate a proc (thread) by number
168  */
169 struct proc *
170 pfind(pid_t pid)
171 {
172 	struct proc *p;
173 
174 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
175 		if (p->p_pid == pid)
176 			return (p);
177 	return (NULL);
178 }
179 
180 /*
181  * Locate a process by number
182  */
183 struct process *
184 prfind(pid_t pid)
185 {
186 	struct proc *p;
187 
188 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
189 		if (p->p_pid == pid)
190 			return (p->p_flag & P_THREAD ? NULL : p->p_p);
191 	return (NULL);
192 }
193 
194 /*
195  * Locate a process group by number
196  */
197 struct pgrp *
198 pgfind(pid_t pgid)
199 {
200 	struct pgrp *pgrp;
201 
202 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
203 		if (pgrp->pg_id == pgid)
204 			return (pgrp);
205 	return (NULL);
206 }
207 
208 /*
209  * Move p to a new or existing process group (and session)
210  * Caller provides a pre-allocated pgrp and session that should
211  * be freed if they are not used.
212  * XXX need proctree lock
213  */
214 int
215 enterpgrp(struct process *pr, pid_t pgid, struct pgrp *newpgrp,
216     struct session *newsess)
217 {
218 	struct pgrp *pgrp = pgfind(pgid);
219 
220 #ifdef DIAGNOSTIC
221 	if (pgrp != NULL && newsess)	/* firewalls */
222 		panic("enterpgrp: setsid into non-empty pgrp");
223 	if (SESS_LEADER(pr))
224 		panic("enterpgrp: session leader attempted setpgrp");
225 #endif
226 	if (pgrp == NULL) {
227 		/*
228 		 * new process group
229 		 */
230 #ifdef DIAGNOSTIC
231 		if (pr->ps_pid != pgid)
232 			panic("enterpgrp: new pgrp and pid != pgid");
233 #endif
234 
235 		pgrp = newpgrp;
236 		if (newsess) {
237 			/*
238 			 * new session
239 			 */
240 			newsess->s_leader = pr;
241 			newsess->s_count = 1;
242 			newsess->s_ttyvp = NULL;
243 			newsess->s_ttyp = NULL;
244 			bcopy(pr->ps_session->s_login, newsess->s_login,
245 			    sizeof(newsess->s_login));
246 			atomic_clearbits_int(&pr->ps_flags, PS_CONTROLT);
247 			pgrp->pg_session = newsess;
248 #ifdef DIAGNOSTIC
249 			if (pr != curproc->p_p)
250 				panic("enterpgrp: mksession but not curproc");
251 #endif
252 		} else {
253 			pgrp->pg_session = pr->ps_session;
254 			pgrp->pg_session->s_count++;
255 		}
256 		pgrp->pg_id = pgid;
257 		LIST_INIT(&pgrp->pg_members);
258 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
259 		pgrp->pg_jobc = 0;
260 	} else if (pgrp == pr->ps_pgrp) {
261 		if (newsess)
262 			pool_put(&session_pool, newsess);
263 		pool_put(&pgrp_pool, newpgrp);
264 		return (0);
265 	} else {
266 		if (newsess)
267 			pool_put(&session_pool, newsess);
268 		pool_put(&pgrp_pool, newpgrp);
269 	}
270 
271 	/*
272 	 * Adjust eligibility of affected pgrps to participate in job control.
273 	 * Increment eligibility counts before decrementing, otherwise we
274 	 * could reach 0 spuriously during the first call.
275 	 */
276 	fixjobc(pr, pgrp, 1);
277 	fixjobc(pr, pr->ps_pgrp, 0);
278 
279 	LIST_REMOVE(pr, ps_pglist);
280 	if (LIST_EMPTY(&pr->ps_pgrp->pg_members))
281 		pgdelete(pr->ps_pgrp);
282 	pr->ps_pgrp = pgrp;
283 	LIST_INSERT_HEAD(&pgrp->pg_members, pr, ps_pglist);
284 	return (0);
285 }
286 
287 /*
288  * remove process from process group
289  */
290 void
291 leavepgrp(struct process *pr)
292 {
293 
294 	LIST_REMOVE(pr, ps_pglist);
295 	if (LIST_EMPTY(&pr->ps_pgrp->pg_members))
296 		pgdelete(pr->ps_pgrp);
297 	pr->ps_pgrp = 0;
298 }
299 
300 /*
301  * delete a process group
302  */
303 void
304 pgdelete(struct pgrp *pgrp)
305 {
306 
307 	if (pgrp->pg_session->s_ttyp != NULL &&
308 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
309 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
310 	LIST_REMOVE(pgrp, pg_hash);
311 	SESSRELE(pgrp->pg_session);
312 	pool_put(&pgrp_pool, pgrp);
313 }
314 
315 /*
316  * Adjust pgrp jobc counters when specified process changes process group.
317  * We count the number of processes in each process group that "qualify"
318  * the group for terminal job control (those with a parent in a different
319  * process group of the same session).  If that count reaches zero, the
320  * process group becomes orphaned.  Check both the specified process'
321  * process group and that of its children.
322  * entering == 0 => pr is leaving specified group.
323  * entering == 1 => pr is entering specified group.
324  * XXX need proctree lock
325  */
326 void
327 fixjobc(struct process *pr, struct pgrp *pgrp, int entering)
328 {
329 	struct pgrp *hispgrp;
330 	struct session *mysession = pgrp->pg_session;
331 
332 	/*
333 	 * Check pr's parent to see whether pr qualifies its own process
334 	 * group; if so, adjust count for pr's process group.
335 	 */
336 	if ((hispgrp = pr->ps_pptr->ps_pgrp) != pgrp &&
337 	    hispgrp->pg_session == mysession) {
338 		if (entering)
339 			pgrp->pg_jobc++;
340 		else if (--pgrp->pg_jobc == 0)
341 			orphanpg(pgrp);
342 	}
343 
344 	/*
345 	 * Check this process' children to see whether they qualify
346 	 * their process groups; if so, adjust counts for children's
347 	 * process groups.
348 	 */
349 	LIST_FOREACH(pr, &pr->ps_children, ps_sibling)
350 		if ((hispgrp = pr->ps_pgrp) != pgrp &&
351 		    hispgrp->pg_session == mysession &&
352 		    P_ZOMBIE(pr->ps_mainproc) == 0) {
353 			if (entering)
354 				hispgrp->pg_jobc++;
355 			else if (--hispgrp->pg_jobc == 0)
356 				orphanpg(hispgrp);
357 		}
358 }
359 
360 /*
361  * A process group has become orphaned;
362  * if there are any stopped processes in the group,
363  * hang-up all process in that group.
364  */
365 static void
366 orphanpg(struct pgrp *pg)
367 {
368 	struct process *pr;
369 
370 	LIST_FOREACH(pr, &pg->pg_members, ps_pglist) {
371 		if (pr->ps_mainproc->p_stat == SSTOP) {
372 			LIST_FOREACH(pr, &pg->pg_members, ps_pglist) {
373 				prsignal(pr, SIGHUP);
374 				prsignal(pr, SIGCONT);
375 			}
376 			return;
377 		}
378 	}
379 }
380 
381 #ifdef DDB
382 void
383 proc_printit(struct proc *p, const char *modif,
384     int (*pr)(const char *, ...) /* __attribute__((__format__(__kprintf__,1,2))) */)
385 {
386 	static const char *const pstat[] = {
387 		"idle", "run", "sleep", "stop", "zombie", "dead", "onproc"
388 	};
389 	char pstbuf[5];
390 	const char *pst = pstbuf;
391 
392 
393 	if (p->p_stat < 1 || p->p_stat > sizeof(pstat) / sizeof(pstat[0]))
394 		snprintf(pstbuf, sizeof(pstbuf), "%d", p->p_stat);
395 	else
396 		pst = pstat[(int)p->p_stat - 1];
397 
398 	(*pr)("PROC (%s) pid=%d stat=%s flags=%b\n",
399 	    p->p_comm, p->p_pid, pst, p->p_flag, P_BITS);
400 	(*pr)("    pri=%u, usrpri=%u, nice=%d\n",
401 	    p->p_priority, p->p_usrpri, p->p_p->ps_nice);
402 	(*pr)("    forw=%p, list=%p,%p\n",
403 	    TAILQ_NEXT(p, p_runq), p->p_list.le_next, p->p_list.le_prev);
404 	(*pr)("    process=%p user=%p, vmspace=%p\n",
405 	    p->p_p, p->p_addr, p->p_vmspace);
406 	(*pr)("    estcpu=%u, cpticks=%d, pctcpu=%u.%u, swtime=%u\n",
407 	    p->p_estcpu, p->p_cpticks, p->p_pctcpu / 100, p->p_pctcpu % 100,
408 	    p->p_swtime);
409 	(*pr)("    user=%llu, sys=%llu, intr=%llu\n",
410 	    p->p_uticks, p->p_sticks, p->p_iticks);
411 }
412 #include <machine/db_machdep.h>
413 
414 #include <ddb/db_interface.h>
415 #include <ddb/db_output.h>
416 
417 void
418 db_show_all_procs(db_expr_t addr, int haddr, db_expr_t count, char *modif)
419 {
420 	char *mode;
421 	int doingzomb = 0;
422 	struct proc *p;
423 	struct process *pr, *ppr;
424 
425 	if (modif[0] == 0)
426 		modif[0] = 'n';			/* default == normal mode */
427 
428 	mode = "mawn";
429 	while (*mode && *mode != modif[0])
430 		mode++;
431 	if (*mode == 0 || *mode == 'm') {
432 		db_printf("usage: show all procs [/a] [/n] [/w]\n");
433 		db_printf("\t/a == show process address info\n");
434 		db_printf("\t/n == show normal process info [default]\n");
435 		db_printf("\t/w == show process wait/emul info\n");
436 		return;
437 	}
438 
439 	p = LIST_FIRST(&allproc);
440 
441 	switch (*mode) {
442 
443 	case 'a':
444 		db_printf("   PID  %-10s  %18s  %18s  %18s\n",
445 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
446 		break;
447 	case 'n':
448 		db_printf("   PID  %5s  %5s  %5s  S  %10s  %-12s  %-16s\n",
449 		    "PPID", "PGRP", "UID", "FLAGS", "WAIT", "COMMAND");
450 		break;
451 	case 'w':
452 		db_printf("   PID  %-16s  %-8s  %18s  %s\n",
453 		    "COMMAND", "EMUL", "WAIT-CHANNEL", "WAIT-MSG");
454 		break;
455 	}
456 
457 	while (p != 0) {
458 		pr = p->p_p;
459 		ppr = pr->ps_pptr;
460 		if (p->p_stat) {
461 
462 			db_printf("%c%5d  ", p == curproc ? '*' : ' ',
463 				p->p_pid);
464 
465 			switch (*mode) {
466 
467 			case 'a':
468 				db_printf("%-10.10s  %18p  %18p  %18p\n",
469 				    p->p_comm, p, p->p_addr, p->p_vmspace);
470 				break;
471 
472 			case 'n':
473 				db_printf("%5d  %5d  %5d  %d  %#10x  "
474 				    "%-12.12s  %-16s\n",
475 				    ppr ? ppr->ps_pid : -1,
476 				    pr->ps_pgrp ? pr->ps_pgrp->pg_id : -1,
477 				    pr->ps_cred->p_ruid, p->p_stat, p->p_flag,
478 				    (p->p_wchan && p->p_wmesg) ?
479 					p->p_wmesg : "", p->p_comm);
480 				break;
481 
482 			case 'w':
483 				db_printf("%-16s  %-8s  %18p  %s\n", p->p_comm,
484 				    p->p_emul->e_name, p->p_wchan,
485 				    (p->p_wchan && p->p_wmesg) ?
486 					p->p_wmesg : "");
487 				break;
488 
489 			}
490 		}
491 		p = LIST_NEXT(p, p_list);
492 		if (p == 0 && doingzomb == 0) {
493 			doingzomb = 1;
494 			p = LIST_FIRST(&zombproc);
495 		}
496 	}
497 }
498 #endif
499 
500 #ifdef DEBUG
501 void
502 pgrpdump(void)
503 {
504 	struct pgrp *pgrp;
505 	struct process *pr;
506 	int i;
507 
508 	for (i = 0; i <= pgrphash; i++) {
509 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
510 			printf("\tindx %d\n", i);
511 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
512 				printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
513 				    pgrp, pgrp->pg_id, pgrp->pg_session,
514 				    pgrp->pg_session->s_count,
515 				    LIST_FIRST(&pgrp->pg_members));
516 				LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) {
517 					printf("\t\tpid %d addr %p pgrp %p\n",
518 					    pr->ps_pid, pr, pr->ps_pgrp);
519 				}
520 			}
521 		}
522 	}
523 }
524 #endif /* DEBUG */
525