xref: /netbsd-src/sys/kern/kern_proc.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: kern_proc.c,v 1.31 1998/09/08 23:47:49 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/map.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/resourcevar.h>
44 #include <sys/buf.h>
45 #include <sys/acct.h>
46 #include <sys/wait.h>
47 #include <sys/file.h>
48 #include <ufs/ufs/quota.h>
49 #include <sys/uio.h>
50 #include <sys/malloc.h>
51 #include <sys/pool.h>
52 #include <sys/mbuf.h>
53 #include <sys/ioctl.h>
54 #include <sys/tty.h>
55 #include <sys/signalvar.h>
56 
57 /*
58  * Structure associated with user cacheing.
59  */
60 struct uidinfo {
61 	LIST_ENTRY(uidinfo) ui_hash;
62 	uid_t	ui_uid;
63 	long	ui_proccnt;
64 };
65 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
66 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
67 u_long uihash;		/* size of hash table - 1 */
68 
69 /*
70  * Other process lists
71  */
72 struct pidhashhead *pidhashtbl;
73 u_long pidhash;
74 struct pgrphashhead *pgrphashtbl;
75 u_long pgrphash;
76 
77 struct proclist allproc;
78 struct proclist deadproc;	/* dead, but not yet undead */
79 struct proclist zombproc;	/* resources have been freed */
80 
81 struct pool proc_pool;
82 struct pool pcred_pool;
83 struct pool plimit_pool;
84 struct pool pgrp_pool;
85 struct pool rusage_pool;
86 
87 /*
88  * The process list descriptors, used during pid allocation and
89  * by sysctl.  No locking on this data structure is needed since
90  * it is completely static.
91  */
92 const struct proclist_desc proclists[] = {
93 	{ &allproc	},
94 	{ &deadproc	},
95 	{ &zombproc	},
96 	{ NULL		},
97 };
98 
99 static void orphanpg __P((struct pgrp *));
100 #ifdef DEBUG
101 void pgrpdump __P((void));
102 #endif
103 
104 /*
105  * Initialize global process hashing structures.
106  */
107 void
108 procinit()
109 {
110 	const struct proclist_desc *pd;
111 
112 	for (pd = proclists; pd->pd_list != NULL; pd++)
113 		LIST_INIT(pd->pd_list);
114 
115 	pidhashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pidhash);
116 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pgrphash);
117 	uihashtbl = hashinit(maxproc / 16, M_PROC, M_WAITOK, &uihash);
118 
119 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
120 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_PROC);
121 	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
122 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_PGRP);
123 	pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
124 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
125 	pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
126 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
127 	pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
128 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_ZOMBIE);
129 }
130 
131 /*
132  * Change the count associated with number of processes
133  * a given user is using.
134  */
135 int
136 chgproccnt(uid, diff)
137 	uid_t	uid;
138 	int	diff;
139 {
140 	register struct uidinfo *uip;
141 	register struct uihashhead *uipp;
142 
143 	uipp = UIHASH(uid);
144 	for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
145 		if (uip->ui_uid == uid)
146 			break;
147 	if (uip) {
148 		uip->ui_proccnt += diff;
149 		if (uip->ui_proccnt > 0)
150 			return (uip->ui_proccnt);
151 		if (uip->ui_proccnt < 0)
152 			panic("chgproccnt: procs < 0");
153 		LIST_REMOVE(uip, ui_hash);
154 		FREE(uip, M_PROC);
155 		return (0);
156 	}
157 	if (diff <= 0) {
158 		if (diff == 0)
159 			return(0);
160 		panic("chgproccnt: lost user");
161 	}
162 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
163 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
164 	uip->ui_uid = uid;
165 	uip->ui_proccnt = diff;
166 	return (diff);
167 }
168 
169 /*
170  * Is p an inferior of the current process?
171  */
172 int
173 inferior(p)
174 	register struct proc *p;
175 {
176 
177 	for (; p != curproc; p = p->p_pptr)
178 		if (p->p_pid == 0)
179 			return (0);
180 	return (1);
181 }
182 
183 /*
184  * Locate a process by number
185  */
186 struct proc *
187 pfind(pid)
188 	register pid_t pid;
189 {
190 	register struct proc *p;
191 
192 	for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
193 		if (p->p_pid == pid)
194 			return (p);
195 	return (NULL);
196 }
197 
198 /*
199  * Locate a process group by number
200  */
201 struct pgrp *
202 pgfind(pgid)
203 	register pid_t pgid;
204 {
205 	register struct pgrp *pgrp;
206 
207 	for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
208 		if (pgrp->pg_id == pgid)
209 			return (pgrp);
210 	return (NULL);
211 }
212 
213 /*
214  * Move p to a new or existing process group (and session)
215  */
216 int
217 enterpgrp(p, pgid, mksess)
218 	register struct proc *p;
219 	pid_t pgid;
220 	int mksess;
221 {
222 	register struct pgrp *pgrp = pgfind(pgid);
223 
224 #ifdef DIAGNOSTIC
225 	if (pgrp != NULL && mksess)	/* firewalls */
226 		panic("enterpgrp: setsid into non-empty pgrp");
227 	if (SESS_LEADER(p))
228 		panic("enterpgrp: session leader attempted setpgrp");
229 #endif
230 	if (pgrp == NULL) {
231 		pid_t savepid = p->p_pid;
232 		struct proc *np;
233 		/*
234 		 * new process group
235 		 */
236 #ifdef DIAGNOSTIC
237 		if (p->p_pid != pgid)
238 			panic("enterpgrp: new pgrp and pid != pgid");
239 #endif
240 		pgrp = pool_get(&pgrp_pool, PR_WAITOK);
241 		if ((np = pfind(savepid)) == NULL || np != p)
242 			return (ESRCH);
243 		if (mksess) {
244 			register struct session *sess;
245 
246 			/*
247 			 * new session
248 			 */
249 			MALLOC(sess, struct session *, sizeof(struct session),
250 			    M_SESSION, M_WAITOK);
251 			sess->s_sid = p->p_pid;
252 			sess->s_leader = p;
253 			sess->s_count = 1;
254 			sess->s_ttyvp = NULL;
255 			sess->s_ttyp = NULL;
256 			memcpy(sess->s_login, p->p_session->s_login,
257 			    sizeof(sess->s_login));
258 			p->p_flag &= ~P_CONTROLT;
259 			pgrp->pg_session = sess;
260 #ifdef DIAGNOSTIC
261 			if (p != curproc)
262 				panic("enterpgrp: mksession and p != curproc");
263 #endif
264 		} else {
265 			pgrp->pg_session = p->p_session;
266 			pgrp->pg_session->s_count++;
267 		}
268 		pgrp->pg_id = pgid;
269 		LIST_INIT(&pgrp->pg_members);
270 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
271 		pgrp->pg_jobc = 0;
272 	} else if (pgrp == p->p_pgrp)
273 		return (0);
274 
275 	/*
276 	 * Adjust eligibility of affected pgrps to participate in job control.
277 	 * Increment eligibility counts before decrementing, otherwise we
278 	 * could reach 0 spuriously during the first call.
279 	 */
280 	fixjobc(p, pgrp, 1);
281 	fixjobc(p, p->p_pgrp, 0);
282 
283 	LIST_REMOVE(p, p_pglist);
284 	if (p->p_pgrp->pg_members.lh_first == 0)
285 		pgdelete(p->p_pgrp);
286 	p->p_pgrp = pgrp;
287 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
288 	return (0);
289 }
290 
291 /*
292  * remove process from process group
293  */
294 int
295 leavepgrp(p)
296 	register struct proc *p;
297 {
298 
299 	LIST_REMOVE(p, p_pglist);
300 	if (p->p_pgrp->pg_members.lh_first == 0)
301 		pgdelete(p->p_pgrp);
302 	p->p_pgrp = 0;
303 	return (0);
304 }
305 
306 /*
307  * delete a process group
308  */
309 void
310 pgdelete(pgrp)
311 	register struct pgrp *pgrp;
312 {
313 
314 	if (pgrp->pg_session->s_ttyp != NULL &&
315 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
316 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
317 	LIST_REMOVE(pgrp, pg_hash);
318 	if (--pgrp->pg_session->s_count == 0)
319 		FREE(pgrp->pg_session, M_SESSION);
320 	pool_put(&pgrp_pool, pgrp);
321 }
322 
323 /*
324  * Adjust pgrp jobc counters when specified process changes process group.
325  * We count the number of processes in each process group that "qualify"
326  * the group for terminal job control (those with a parent in a different
327  * process group of the same session).  If that count reaches zero, the
328  * process group becomes orphaned.  Check both the specified process'
329  * process group and that of its children.
330  * entering == 0 => p is leaving specified group.
331  * entering == 1 => p is entering specified group.
332  */
333 void
334 fixjobc(p, pgrp, entering)
335 	register struct proc *p;
336 	register struct pgrp *pgrp;
337 	int entering;
338 {
339 	register struct pgrp *hispgrp;
340 	register struct session *mysession = pgrp->pg_session;
341 
342 	/*
343 	 * Check p's parent to see whether p qualifies its own process
344 	 * group; if so, adjust count for p's process group.
345 	 */
346 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
347 	    hispgrp->pg_session == mysession) {
348 		if (entering)
349 			pgrp->pg_jobc++;
350 		else if (--pgrp->pg_jobc == 0)
351 			orphanpg(pgrp);
352 	}
353 
354 	/*
355 	 * Check this process' children to see whether they qualify
356 	 * their process groups; if so, adjust counts for children's
357 	 * process groups.
358 	 */
359 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
360 		if ((hispgrp = p->p_pgrp) != pgrp &&
361 		    hispgrp->pg_session == mysession &&
362 		    p->p_stat != SZOMB) {
363 			if (entering)
364 				hispgrp->pg_jobc++;
365 			else if (--hispgrp->pg_jobc == 0)
366 				orphanpg(hispgrp);
367 		}
368 	}
369 }
370 
371 /*
372  * A process group has become orphaned;
373  * if there are any stopped processes in the group,
374  * hang-up all process in that group.
375  */
376 static void
377 orphanpg(pg)
378 	struct pgrp *pg;
379 {
380 	register struct proc *p;
381 
382 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
383 		if (p->p_stat == SSTOP) {
384 			for (p = pg->pg_members.lh_first; p != 0;
385 			    p = p->p_pglist.le_next) {
386 				psignal(p, SIGHUP);
387 				psignal(p, SIGCONT);
388 			}
389 			return;
390 		}
391 	}
392 }
393 
394 #ifdef DEBUG
395 void
396 pgrpdump()
397 {
398 	register struct pgrp *pgrp;
399 	register struct proc *p;
400 	register int i;
401 
402 	for (i = 0; i <= pgrphash; i++) {
403 		if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
404 			printf("\tindx %d\n", i);
405 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
406 				printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
407 				    pgrp, pgrp->pg_id, pgrp->pg_session,
408 				    pgrp->pg_session->s_count,
409 				    pgrp->pg_members.lh_first);
410 				for (p = pgrp->pg_members.lh_first; p != 0;
411 				    p = p->p_pglist.le_next) {
412 					printf("\t\tpid %d addr %p pgrp %p\n",
413 					    p->p_pid, p, p->p_pgrp);
414 				}
415 			}
416 		}
417 	}
418 }
419 #endif /* DEBUG */
420