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