1 /*	$OpenBSD: kern_proc.c,v 1.18 2004/01/29 17:19:42 millert Exp $	*/
2 /* + 1.40.4.1 */
3 /*	$NetBSD: kern_proc.c,v 1.14 1996/02/09 18:59:41 christos Exp $	*/
4 
5 /*
6  * Copyright (c) 1982, 1986, 1989, 1991, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. 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.4 (Berkeley) 1/4/94
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/buf.h>
41 #include <sys/acct.h>
42 #include <sys/wait.h>
43 #include <sys/file.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 proclist zombproc;
66 
67 struct pool proc_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 /*
75  * Locking of this proclist is special; it's accessed in a
76  * critical section of process exit, and thus locking it can't
77  * modify interrupt state.  We use a simple spin lock for this
78  * proclist.  Processes on this proclist are also on zombproc;
79  * we use the p_hash member to linkup to deadproc.
80  */
81 struct simplelock deadproc_slock;
82 struct proclist deadproc;		/* dead, but not yet undead */
83 
84 static void orphanpg(struct pgrp *);
85 #ifdef DEBUG
86 void pgrpdump(void);
87 #endif
88 
89 /*
90  * Initialize global process hashing structures.
91  */
92 void
procinit()93 procinit()
94 {
95 
96 	LIST_INIT(&allproc);
97 	LIST_INIT(&zombproc);
98 
99 	LIST_INIT(&deadproc);
100 	simple_lock_init(&deadproc_slock);
101 
102 	pidhashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pidhash);
103 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pgrphash);
104 	uihashtbl = hashinit(maxproc / 16, M_PROC, M_WAITOK, &uihash);
105 
106 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
107 	    &pool_allocator_nointr);
108 	pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "zombiepl",
109 	    &pool_allocator_nointr);
110 	pool_init(&ucred_pool, sizeof(struct ucred), 0, 0, 0, "ucredpl",
111 	    &pool_allocator_nointr);
112 	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
113 	    &pool_allocator_nointr);
114 	pool_init(&session_pool, sizeof(struct session), 0, 0, 0, "sessionpl",
115 	    &pool_allocator_nointr);
116 	pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
117 	    &pool_allocator_nointr);
118 }
119 
120 /*
121  * Change the count associated with number of processes
122  * a given user is using.
123  */
124 struct uidinfo *
uid_find(uid_t uid)125 uid_find(uid_t uid)
126 {
127 	struct uidinfo *uip;
128 	struct uihashhead *uipp;
129 
130 	uipp = UIHASH(uid);
131 	LIST_FOREACH(uip, uipp, ui_hash)
132 		if (uip->ui_uid == uid)
133 			break;
134 	if (uip)
135 		return (uip);
136 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
137 	bzero(uip, sizeof(*uip));
138 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
139 	uip->ui_uid = uid;
140 
141 	return (uip);
142 }
143 
144 int
chgproccnt(uid_t uid,int diff)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
inferior(struct proc * p,struct proc * parent)160 inferior(struct proc *p, struct proc *parent)
161 {
162 	for (; p != parent; p = p->p_pptr)
163 		if (p->p_pid == 0 || p->p_pid == 1)
164 			return (0);
165 	return (1);
166 }
167 
168 /*
169  * Locate a process by number
170  */
171 struct proc *
pfind(pid)172 pfind(pid)
173 	register pid_t pid;
174 {
175 	register struct proc *p;
176 
177 	for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
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 *
pgfind(pgid)187 pgfind(pgid)
188 	register pid_t pgid;
189 {
190 	register struct pgrp *pgrp;
191 
192 	for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
193 		if (pgrp->pg_id == pgid)
194 			return (pgrp);
195 	return (NULL);
196 }
197 
198 /*
199  * Move p to a new or existing process group (and session)
200  */
201 int
enterpgrp(p,pgid,mksess)202 enterpgrp(p, pgid, mksess)
203 	register struct proc *p;
204 	pid_t pgid;
205 	int mksess;
206 {
207 	register struct pgrp *pgrp = pgfind(pgid);
208 
209 #ifdef DIAGNOSTIC
210 	if (pgrp != NULL && mksess)	/* firewalls */
211 		panic("enterpgrp: setsid into non-empty pgrp");
212 	if (SESS_LEADER(p))
213 		panic("enterpgrp: session leader attempted setpgrp");
214 #endif
215 	if (pgrp == NULL) {
216 		pid_t savepid = p->p_pid;
217 		struct proc *np;
218 		/*
219 		 * new process group
220 		 */
221 #ifdef DIAGNOSTIC
222 		if (p->p_pid != pgid)
223 			panic("enterpgrp: new pgrp and pid != pgid");
224 #endif
225 		if ((np = pfind(savepid)) == NULL || np != p)
226 			return (ESRCH);
227 		pgrp = pool_get(&pgrp_pool, PR_WAITOK);
228 		if (mksess) {
229 			register struct session *sess;
230 
231 			/*
232 			 * new session
233 			 */
234 			sess = pool_get(&session_pool, PR_WAITOK);
235 			sess->s_leader = p;
236 			sess->s_count = 1;
237 			sess->s_ttyvp = NULL;
238 			sess->s_ttyp = NULL;
239 			bcopy(p->p_session->s_login, sess->s_login,
240 			    sizeof(sess->s_login));
241 			p->p_flag &= ~P_CONTROLT;
242 			pgrp->pg_session = sess;
243 #ifdef DIAGNOSTIC
244 			if (p != curproc)
245 				panic("enterpgrp: mksession and p != curproc");
246 #endif
247 		} else {
248 			pgrp->pg_session = p->p_session;
249 			pgrp->pg_session->s_count++;
250 		}
251 		pgrp->pg_id = pgid;
252 		LIST_INIT(&pgrp->pg_members);
253 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
254 		pgrp->pg_jobc = 0;
255 	} else if (pgrp == p->p_pgrp)
256 		return (0);
257 
258 	/*
259 	 * Adjust eligibility of affected pgrps to participate in job control.
260 	 * Increment eligibility counts before decrementing, otherwise we
261 	 * could reach 0 spuriously during the first call.
262 	 */
263 	fixjobc(p, pgrp, 1);
264 	fixjobc(p, p->p_pgrp, 0);
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 = pgrp;
270 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
271 	return (0);
272 }
273 
274 /*
275  * remove process from process group
276  */
277 int
leavepgrp(p)278 leavepgrp(p)
279 	register struct proc *p;
280 {
281 
282 	LIST_REMOVE(p, p_pglist);
283 	if (p->p_pgrp->pg_members.lh_first == 0)
284 		pgdelete(p->p_pgrp);
285 	p->p_pgrp = 0;
286 	return (0);
287 }
288 
289 /*
290  * delete a process group
291  */
292 void
pgdelete(pgrp)293 pgdelete(pgrp)
294 	register struct pgrp *pgrp;
295 {
296 
297 	if (pgrp->pg_session->s_ttyp != NULL &&
298 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
299 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
300 	LIST_REMOVE(pgrp, pg_hash);
301 	SESSRELE(pgrp->pg_session);
302 	pool_put(&pgrp_pool, pgrp);
303 }
304 
305 /*
306  * Adjust pgrp jobc counters when specified process changes process group.
307  * We count the number of processes in each process group that "qualify"
308  * the group for terminal job control (those with a parent in a different
309  * process group of the same session).  If that count reaches zero, the
310  * process group becomes orphaned.  Check both the specified process'
311  * process group and that of its children.
312  * entering == 0 => p is leaving specified group.
313  * entering == 1 => p is entering specified group.
314  */
315 void
fixjobc(p,pgrp,entering)316 fixjobc(p, pgrp, entering)
317 	register struct proc *p;
318 	register struct pgrp *pgrp;
319 	int entering;
320 {
321 	register struct pgrp *hispgrp;
322 	register struct session *mysession = pgrp->pg_session;
323 
324 	/*
325 	 * Check p's parent to see whether p qualifies its own process
326 	 * group; if so, adjust count for p's process group.
327 	 */
328 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
329 	    hispgrp->pg_session == mysession) {
330 		if (entering)
331 			pgrp->pg_jobc++;
332 		else if (--pgrp->pg_jobc == 0)
333 			orphanpg(pgrp);
334 	}
335 
336 	/*
337 	 * Check this process' children to see whether they qualify
338 	 * their process groups; if so, adjust counts for children's
339 	 * process groups.
340 	 */
341 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next)
342 		if ((hispgrp = p->p_pgrp) != pgrp &&
343 		    hispgrp->pg_session == mysession &&
344 		    P_ZOMBIE(p) == 0) {
345 			if (entering)
346 				hispgrp->pg_jobc++;
347 			else if (--hispgrp->pg_jobc == 0)
348 				orphanpg(hispgrp);
349 		}
350 }
351 
352 /*
353  * A process group has become orphaned;
354  * if there are any stopped processes in the group,
355  * hang-up all process in that group.
356  */
357 static void
orphanpg(pg)358 orphanpg(pg)
359 	struct pgrp *pg;
360 {
361 	register struct proc *p;
362 
363 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
364 		if (p->p_stat == SSTOP) {
365 			for (p = pg->pg_members.lh_first; p != 0;
366 			    p = p->p_pglist.le_next) {
367 				psignal(p, SIGHUP);
368 				psignal(p, SIGCONT);
369 			}
370 			return;
371 		}
372 	}
373 }
374 
375 void
proc_printit(struct proc * p,const char * modif,int (* pr)(const char *,...))376 proc_printit(struct proc *p, const char *modif, int (*pr)(const char *, ...))
377 {
378 	static const char *const pstat[] = {
379 		"idle", "run", "sleep", "stop", "zombie", "dead"
380 	};
381 	char pstbuf[5];
382 	const char *pst = pstbuf;
383 
384 	if (p->p_stat < 1 || p->p_stat > sizeof(pstat) / sizeof(pstat[0]))
385 		snprintf(pstbuf, sizeof(pstbuf), "%d", p->p_stat);
386 	else
387 		pst = pstat[(int)p->p_stat - 1];
388 
389 	(*pr)("PROC (%s) pid=%d stat=%s flags=%b\n",
390 	    p->p_comm, p->p_pid, pst, p->p_flag, P_BITS);
391 	(*pr)("    pri=%u, usrpri=%u, nice=%d\n",
392 	    p->p_priority, p->p_usrpri, p->p_nice);
393 	(*pr)("    forw=%p, back=%p, list=%p,%p\n",
394 	    p->p_forw, p->p_back, p->p_list.le_next, p->p_list.le_prev);
395 	(*pr)("    user=%p, vmspace=%p\n",
396 	    p->p_addr, p->p_vmspace);
397 	(*pr)("    estcpu=%u, cpticks=%d, pctcpu=%u.%u%, swtime=%u\n",
398 	    p->p_estcpu, p->p_cpticks, p->p_pctcpu / 100, p->p_pctcpu % 100,
399 	    p->p_swtime);
400 	(*pr)("    user=%llu, sys=%llu, intr=%llu\n",
401 	    p->p_uticks, p->p_sticks, p->p_iticks);
402 }
403 
404 #ifdef DEBUG
405 void
pgrpdump()406 pgrpdump()
407 {
408 	register struct pgrp *pgrp;
409 	register struct proc *p;
410 	register int i;
411 
412 	for (i = 0; i <= pgrphash; i++) {
413 		if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
414 			printf("\tindx %d\n", i);
415 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
416 				printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
417 				    pgrp, pgrp->pg_id, pgrp->pg_session,
418 				    pgrp->pg_session->s_count,
419 				    pgrp->pg_members.lh_first);
420 				for (p = pgrp->pg_members.lh_first; p != 0;
421 				    p = p->p_pglist.le_next) {
422 					printf("\t\tpid %d addr %p pgrp %p\n",
423 					    p->p_pid, p, p->p_pgrp);
424 				}
425 			}
426 		}
427 	}
428 }
429 #endif /* DEBUG */
430