xref: /openbsd-src/sys/kern/kern_proc.c (revision cd1eb269cafb12c415be1749cd4a4b5422710415)
1 /*	$OpenBSD: kern_proc.c,v 1.42 2010/03/24 23:18:17 tedu 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 <sys/rwlock.h>
44 #include <ufs/ufs/quota.h>
45 #include <sys/uio.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/ioctl.h>
49 #include <sys/tty.h>
50 #include <sys/signalvar.h>
51 #include <sys/pool.h>
52 
53 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
54 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
55 u_long uihash;		/* size of hash table - 1 */
56 
57 /*
58  * Other process lists
59  */
60 struct pidhashhead *pidhashtbl;
61 u_long pidhash;
62 struct pgrphashhead *pgrphashtbl;
63 u_long pgrphash;
64 struct proclist allproc;
65 struct rwlock allproclk;
66 struct proclist zombproc;
67 
68 struct pool proc_pool;
69 struct pool process_pool;
70 struct pool rusage_pool;
71 struct pool ucred_pool;
72 struct pool pgrp_pool;
73 struct pool session_pool;
74 struct pool pcred_pool;
75 
76 static void orphanpg(struct pgrp *);
77 #ifdef DEBUG
78 void pgrpdump(void);
79 #endif
80 
81 /*
82  * Initialize global process hashing structures.
83  */
84 void
85 procinit(void)
86 {
87 	LIST_INIT(&allproc);
88 	rw_init(&allproclk, "allproc");
89 	LIST_INIT(&zombproc);
90 
91 
92 	pidhashtbl = hashinit(maxproc / 4, M_PROC, M_NOWAIT, &pidhash);
93 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_NOWAIT, &pgrphash);
94 	uihashtbl = hashinit(maxproc / 16, M_PROC, M_NOWAIT, &uihash);
95 	if (!pidhashtbl || !pgrphashtbl || !uihashtbl)
96 		panic("procinit: malloc");
97 
98 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
99 	    &pool_allocator_nointr);
100 	pool_init(&process_pool, sizeof(struct process), 0, 0, 0, "processpl",
101 	    &pool_allocator_nointr);
102 	pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "zombiepl",
103 	    &pool_allocator_nointr);
104 	pool_init(&ucred_pool, sizeof(struct ucred), 0, 0, 0, "ucredpl",
105 	    &pool_allocator_nointr);
106 	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
107 	    &pool_allocator_nointr);
108 	pool_init(&session_pool, sizeof(struct session), 0, 0, 0, "sessionpl",
109 	    &pool_allocator_nointr);
110 	pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
111 	    &pool_allocator_nointr);
112 }
113 
114 struct uidinfo *
115 uid_find(uid_t uid)
116 {
117 	struct uidinfo *uip, *nuip;
118 	struct uihashhead *uipp;
119 
120 	uipp = UIHASH(uid);
121 	LIST_FOREACH(uip, uipp, ui_hash)
122 		if (uip->ui_uid == uid)
123 			break;
124 	if (uip)
125 		return (uip);
126 	nuip = malloc(sizeof(*nuip), M_PROC, M_WAITOK|M_ZERO);
127 	LIST_FOREACH(uip, uipp, ui_hash)
128 		if (uip->ui_uid == uid)
129 			break;
130 	if (uip) {
131 		free(nuip, M_PROC);
132 		return (uip);
133 	}
134 	nuip->ui_uid = uid;
135 	LIST_INSERT_HEAD(uipp, nuip, ui_hash);
136 
137 	return (nuip);
138 }
139 
140 /*
141  * Change the count associated with number of processes
142  * a given user is using.
143  */
144 int
145 chgproccnt(uid_t uid, int diff)
146 {
147 	struct uidinfo *uip;
148 
149 	uip = uid_find(uid);
150 	uip->ui_proccnt += diff;
151 	if (uip->ui_proccnt < 0)
152 		panic("chgproccnt: procs < 0");
153 	return (uip->ui_proccnt);
154 }
155 
156 /*
157  * Is p an inferior of parent?
158  */
159 int
160 inferior(struct proc *p, struct proc *parent)
161 {
162 
163 	for (; p != parent; p = p->p_pptr)
164 		if (p->p_pid == 0 || p->p_pid == 1)
165 			return (0);
166 	return (1);
167 }
168 
169 /*
170  * Locate a process by number
171  */
172 struct proc *
173 pfind(pid_t pid)
174 {
175 	struct proc *p;
176 
177 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
178 		if (p->p_pid == pid)
179 			return (p);
180 	return (NULL);
181 }
182 
183 /*
184  * Locate a process group by number
185  */
186 struct pgrp *
187 pgfind(pid_t pgid)
188 {
189 	struct pgrp *pgrp;
190 
191 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
192 		if (pgrp->pg_id == pgid)
193 			return (pgrp);
194 	return (NULL);
195 }
196 
197 /*
198  * Move p to a new or existing process group (and session)
199  * Caller provides a pre-allocated pgrp and session that should
200  * be freed if they are not used.
201  */
202 int
203 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *newpgrp,
204     struct session *newsess)
205 {
206 	struct pgrp *pgrp = pgfind(pgid);
207 
208 #ifdef DIAGNOSTIC
209 	if (pgrp != NULL && newsess)	/* firewalls */
210 		panic("enterpgrp: setsid into non-empty pgrp");
211 	if (SESS_LEADER(p))
212 		panic("enterpgrp: session leader attempted setpgrp");
213 #endif
214 	if (pgrp == NULL) {
215 		struct proc *np;
216 		/*
217 		 * new process group
218 		 */
219 #ifdef DIAGNOSTIC
220 		if (p->p_pid != pgid)
221 			panic("enterpgrp: new pgrp and pid != pgid");
222 #endif
223 
224 		if ((np = pfind(p->p_pid)) == NULL || np != p) {
225 			pool_put(&pgrp_pool, newpgrp);
226 			if (newsess)
227 				pool_put(&session_pool, newsess);
228 			return (ESRCH);
229 		}
230 
231 		pgrp = newpgrp;
232 		if (newsess) {
233 			/*
234 			 * new session
235 			 */
236 			newsess->s_leader = p;
237 			newsess->s_count = 1;
238 			newsess->s_ttyvp = NULL;
239 			newsess->s_ttyp = NULL;
240 			bcopy(p->p_session->s_login, newsess->s_login,
241 			    sizeof(newsess->s_login));
242 			atomic_clearbits_int(&p->p_flag, P_CONTROLT);
243 			pgrp->pg_session = newsess;
244 #ifdef DIAGNOSTIC
245 			if (p != curproc)
246 				panic("enterpgrp: mksession and p != curproc");
247 #endif
248 		} else {
249 			pgrp->pg_session = p->p_session;
250 			pgrp->pg_session->s_count++;
251 		}
252 		pgrp->pg_id = pgid;
253 		LIST_INIT(&pgrp->pg_members);
254 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
255 		pgrp->pg_jobc = 0;
256 	} else if (pgrp == p->p_pgrp) {
257 		if (newsess)
258 			pool_put(&session_pool, newsess);
259 		pool_put(&pgrp_pool, newpgrp);
260 		return (0);
261 	} else {
262 		if (newsess)
263 			pool_put(&session_pool, newsess);
264 		pool_put(&pgrp_pool, newpgrp);
265 	}
266 
267 	/*
268 	 * Adjust eligibility of affected pgrps to participate in job control.
269 	 * Increment eligibility counts before decrementing, otherwise we
270 	 * could reach 0 spuriously during the first call.
271 	 */
272 	fixjobc(p, pgrp, 1);
273 	fixjobc(p, p->p_pgrp, 0);
274 
275 	LIST_REMOVE(p, p_pglist);
276 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
277 		pgdelete(p->p_pgrp);
278 	p->p_pgrp = pgrp;
279 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
280 	return (0);
281 }
282 
283 /*
284  * remove process from process group
285  */
286 int
287 leavepgrp(struct proc *p)
288 {
289 
290 	LIST_REMOVE(p, p_pglist);
291 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
292 		pgdelete(p->p_pgrp);
293 	p->p_pgrp = 0;
294 	return (0);
295 }
296 
297 /*
298  * delete a process group
299  */
300 void
301 pgdelete(struct pgrp *pgrp)
302 {
303 
304 	if (pgrp->pg_session->s_ttyp != NULL &&
305 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
306 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
307 	LIST_REMOVE(pgrp, pg_hash);
308 	SESSRELE(pgrp->pg_session);
309 	pool_put(&pgrp_pool, pgrp);
310 }
311 
312 /*
313  * Adjust pgrp jobc counters when specified process changes process group.
314  * We count the number of processes in each process group that "qualify"
315  * the group for terminal job control (those with a parent in a different
316  * process group of the same session).  If that count reaches zero, the
317  * process group becomes orphaned.  Check both the specified process'
318  * process group and that of its children.
319  * entering == 0 => p is leaving specified group.
320  * entering == 1 => p is entering specified group.
321  */
322 void
323 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
324 {
325 	struct pgrp *hispgrp;
326 	struct session *mysession = pgrp->pg_session;
327 
328 	/*
329 	 * Check p's parent to see whether p qualifies its own process
330 	 * group; if so, adjust count for p's process group.
331 	 */
332 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
333 	    hispgrp->pg_session == mysession) {
334 		if (entering)
335 			pgrp->pg_jobc++;
336 		else if (--pgrp->pg_jobc == 0)
337 			orphanpg(pgrp);
338 	}
339 
340 	/*
341 	 * Check this process' children to see whether they qualify
342 	 * their process groups; if so, adjust counts for children's
343 	 * process groups.
344 	 */
345 	LIST_FOREACH(p, &p->p_children, p_sibling)
346 		if ((hispgrp = p->p_pgrp) != pgrp &&
347 		    hispgrp->pg_session == mysession &&
348 		    P_ZOMBIE(p) == 0) {
349 			if (entering)
350 				hispgrp->pg_jobc++;
351 			else if (--hispgrp->pg_jobc == 0)
352 				orphanpg(hispgrp);
353 		}
354 }
355 
356 /*
357  * A process group has become orphaned;
358  * if there are any stopped processes in the group,
359  * hang-up all process in that group.
360  */
361 static void
362 orphanpg(struct pgrp *pg)
363 {
364 	struct proc *p;
365 
366 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
367 		if (p->p_stat == SSTOP) {
368 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
369 				psignal(p, SIGHUP);
370 				psignal(p, SIGCONT);
371 			}
372 			return;
373 		}
374 	}
375 }
376 
377 #ifdef DDB
378 void
379 proc_printit(struct proc *p, const char *modif, int (*pr)(const char *, ...))
380 {
381 	static const char *const pstat[] = {
382 		"idle", "run", "sleep", "stop", "zombie", "dead", "onproc"
383 	};
384 	char pstbuf[5];
385 	const char *pst = pstbuf;
386 
387 	if (p->p_stat < 1 || p->p_stat > sizeof(pstat) / sizeof(pstat[0]))
388 		snprintf(pstbuf, sizeof(pstbuf), "%d", p->p_stat);
389 	else
390 		pst = pstat[(int)p->p_stat - 1];
391 
392 	(*pr)("PROC (%s) pid=%d stat=%s flags=%b\n",
393 	    p->p_comm, p->p_pid, pst, p->p_flag, P_BITS);
394 	(*pr)("    pri=%u, usrpri=%u, nice=%d\n",
395 	    p->p_priority, p->p_usrpri, p->p_nice);
396 	(*pr)("    forw=%p, list=%p,%p\n",
397 	    TAILQ_NEXT(p, p_runq), p->p_list.le_next, p->p_list.le_prev);
398 	(*pr)("    user=%p, vmspace=%p\n",
399 	    p->p_addr, p->p_vmspace);
400 	(*pr)("    estcpu=%u, cpticks=%d, pctcpu=%u.%u%, swtime=%u\n",
401 	    p->p_estcpu, p->p_cpticks, p->p_pctcpu / 100, p->p_pctcpu % 100,
402 	    p->p_swtime);
403 	(*pr)("    user=%llu, sys=%llu, intr=%llu\n",
404 	    p->p_uticks, p->p_sticks, p->p_iticks);
405 }
406 #include <machine/db_machdep.h>
407 
408 #include <ddb/db_interface.h>
409 #include <ddb/db_output.h>
410 
411 void
412 db_show_all_procs(db_expr_t addr, int haddr, db_expr_t count, char *modif)
413 {
414 	char *mode;
415 	int doingzomb = 0;
416 	struct proc *p, *pp;
417 
418 	if (modif[0] == 0)
419 		modif[0] = 'n';			/* default == normal mode */
420 
421 	mode = "mawn";
422 	while (*mode && *mode != modif[0])
423 		mode++;
424 	if (*mode == 0 || *mode == 'm') {
425 		db_printf("usage: show all procs [/a] [/n] [/w]\n");
426 		db_printf("\t/a == show process address info\n");
427 		db_printf("\t/n == show normal process info [default]\n");
428 		db_printf("\t/w == show process wait/emul info\n");
429 		return;
430 	}
431 
432 	p = LIST_FIRST(&allproc);
433 
434 	switch (*mode) {
435 
436 	case 'a':
437 		db_printf("   PID  %-10s  %18s  %18s  %18s\n",
438 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
439 		break;
440 	case 'n':
441 		db_printf("   PID  %5s  %5s  %5s  S  %10s  %-12s  %-16s\n",
442 		    "PPID", "PGRP", "UID", "FLAGS", "WAIT", "COMMAND");
443 		break;
444 	case 'w':
445 		db_printf("   PID  %-16s  %-8s  %18s  %s\n",
446 		    "COMMAND", "EMUL", "WAIT-CHANNEL", "WAIT-MSG");
447 		break;
448 	}
449 
450 	while (p != 0) {
451 		pp = p->p_pptr;
452 		if (p->p_stat) {
453 
454 			db_printf("%c%5d  ", p == curproc ? '*' : ' ',
455 				p->p_pid);
456 
457 			switch (*mode) {
458 
459 			case 'a':
460 				db_printf("%-10.10s  %18p  %18p  %18p\n",
461 				    p->p_comm, p, p->p_addr, p->p_vmspace);
462 				break;
463 
464 			case 'n':
465 				db_printf("%5d  %5d  %5d  %d  %#10x  "
466 				    "%-12.12s  %-16s\n",
467 				    pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
468 				    p->p_cred->p_ruid, p->p_stat, p->p_flag,
469 				    (p->p_wchan && p->p_wmesg) ?
470 					p->p_wmesg : "", p->p_comm);
471 				break;
472 
473 			case 'w':
474 				db_printf("%-16s  %-8s  %18p  %s\n", p->p_comm,
475 				    p->p_emul->e_name, p->p_wchan,
476 				    (p->p_wchan && p->p_wmesg) ?
477 					p->p_wmesg : "");
478 				break;
479 
480 			}
481 		}
482 		p = LIST_NEXT(p, p_list);
483 		if (p == 0 && doingzomb == 0) {
484 			doingzomb = 1;
485 			p = LIST_FIRST(&zombproc);
486 		}
487 	}
488 }
489 #endif
490 
491 #ifdef DEBUG
492 void
493 pgrpdump(void)
494 {
495 	struct pgrp *pgrp;
496 	struct proc *p;
497 	int i;
498 
499 	for (i = 0; i <= pgrphash; i++) {
500 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
501 			printf("\tindx %d\n", i);
502 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
503 				printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
504 				    pgrp, pgrp->pg_id, pgrp->pg_session,
505 				    pgrp->pg_session->s_count,
506 				    LIST_FIRST(&pgrp->pg_members));
507 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
508 					printf("\t\tpid %d addr %p pgrp %p\n",
509 					    p->p_pid, p, p->p_pgrp);
510 				}
511 			}
512 		}
513 	}
514 }
515 #endif /* DEBUG */
516