xref: /openbsd-src/sys/kern/kern_proc.c (revision 9dc70864756380a8a893d1670edabaa78fb35384)
1 /*	$OpenBSD: kern_proc.c,v 1.8 2001/03/23 18:42:06 art 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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)kern_proc.c	8.4 (Berkeley) 1/4/94
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/map.h>
42 #include <sys/kernel.h>
43 #include <sys/proc.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/mbuf.h>
52 #include <sys/ioctl.h>
53 #include <sys/tty.h>
54 #include <sys/signalvar.h>
55 #include <sys/pool.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 struct proclist allproc;
77 struct proclist zombproc;
78 
79 struct pool proc_pool;
80 
81 /*
82  * Locking of this proclist is special; it's accessed in a
83  * critical section of process exit, and thus locking it can't
84  * modify interrupt state.  We use a simple spin lock for this
85  * proclist.  Processes on this proclist are also on zombproc;
86  * we use the p_hash member to linkup to deadproc.
87  */
88 struct simplelock deadproc_slock;
89 struct proclist deadproc;		/* dead, but not yet undead */
90 
91 static void orphanpg __P((struct pgrp *));
92 #ifdef DEBUG
93 void pgrpdump __P((void));
94 #endif
95 
96 /*
97  * Initialize global process hashing structures.
98  */
99 void
100 procinit()
101 {
102 
103 	LIST_INIT(&allproc);
104 	LIST_INIT(&zombproc);
105 
106 	LIST_INIT(&deadproc);
107 	simple_lock_init(&deadproc_slock);
108 
109 	pidhashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pidhash);
110 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pgrphash);
111 	uihashtbl = hashinit(maxproc / 16, M_PROC, M_WAITOK, &uihash);
112 
113 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
114 		0, pool_page_alloc_nointr, pool_page_free_nointr, M_PROC);
115 }
116 
117 /*
118  * Change the count associated with number of processes
119  * a given user is using.
120  */
121 int
122 chgproccnt(uid, diff)
123 	uid_t	uid;
124 	int	diff;
125 {
126 	register struct uidinfo *uip;
127 	register struct uihashhead *uipp;
128 
129 	uipp = UIHASH(uid);
130 	for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
131 		if (uip->ui_uid == uid)
132 			break;
133 	if (uip) {
134 		uip->ui_proccnt += diff;
135 		if (uip->ui_proccnt > 0)
136 			return (uip->ui_proccnt);
137 		if (uip->ui_proccnt < 0)
138 			panic("chgproccnt: procs < 0");
139 		LIST_REMOVE(uip, ui_hash);
140 		FREE(uip, M_PROC);
141 		return (0);
142 	}
143 	if (diff <= 0) {
144 		if (diff == 0)
145 			return(0);
146 		panic("chgproccnt: lost user");
147 	}
148 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
149 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
150 	uip->ui_uid = uid;
151 	uip->ui_proccnt = diff;
152 	return (diff);
153 }
154 
155 /*
156  * Is p an inferior of the current process?
157  */
158 int
159 inferior(p)
160 	register struct proc *p;
161 {
162 
163 	for (; p != curproc; p = p->p_pptr)
164 		if (p->p_pid == 0)
165 			return (0);
166 	return (1);
167 }
168 
169 /*
170  * Locate a process by number
171  */
172 struct proc *
173 pfind(pid)
174 	register pid_t pid;
175 {
176 	register struct proc *p;
177 
178 	for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
179 		if (p->p_pid == pid)
180 			return (p);
181 	return (NULL);
182 }
183 
184 /*
185  * Locate a process group by number
186  */
187 struct pgrp *
188 pgfind(pgid)
189 	register pid_t pgid;
190 {
191 	register struct pgrp *pgrp;
192 
193 	for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
194 		if (pgrp->pg_id == pgid)
195 			return (pgrp);
196 	return (NULL);
197 }
198 
199 /*
200  * Move p to a new or existing process group (and session)
201  */
202 int
203 enterpgrp(p, pgid, mksess)
204 	register struct proc *p;
205 	pid_t pgid;
206 	int mksess;
207 {
208 	register struct pgrp *pgrp = pgfind(pgid);
209 
210 #ifdef DIAGNOSTIC
211 	if (pgrp != NULL && mksess)	/* firewalls */
212 		panic("enterpgrp: setsid into non-empty pgrp");
213 	if (SESS_LEADER(p))
214 		panic("enterpgrp: session leader attempted setpgrp");
215 #endif
216 	if (pgrp == NULL) {
217 		pid_t savepid = p->p_pid;
218 		struct proc *np;
219 		/*
220 		 * new process group
221 		 */
222 #ifdef DIAGNOSTIC
223 		if (p->p_pid != pgid)
224 			panic("enterpgrp: new pgrp and pid != pgid");
225 #endif
226 		if ((np = pfind(savepid)) == NULL || np != p)
227 			return (ESRCH);
228 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
229 		    M_WAITOK);
230 		if (mksess) {
231 			register struct session *sess;
232 
233 			/*
234 			 * new session
235 			 */
236 			MALLOC(sess, struct session *, sizeof(struct session),
237 			    M_SESSION, M_WAITOK);
238 			sess->s_leader = p;
239 			sess->s_count = 1;
240 			sess->s_ttyvp = NULL;
241 			sess->s_ttyp = NULL;
242 			bcopy(p->p_session->s_login, sess->s_login,
243 			    sizeof(sess->s_login));
244 			p->p_flag &= ~P_CONTROLT;
245 			pgrp->pg_session = sess;
246 #ifdef DIAGNOSTIC
247 			if (p != curproc)
248 				panic("enterpgrp: mksession and p != curproc");
249 #endif
250 		} else {
251 			pgrp->pg_session = p->p_session;
252 			pgrp->pg_session->s_count++;
253 		}
254 		pgrp->pg_id = pgid;
255 		LIST_INIT(&pgrp->pg_members);
256 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
257 		pgrp->pg_jobc = 0;
258 	} else if (pgrp == p->p_pgrp)
259 		return (0);
260 
261 	/*
262 	 * Adjust eligibility of affected pgrps to participate in job control.
263 	 * Increment eligibility counts before decrementing, otherwise we
264 	 * could reach 0 spuriously during the first call.
265 	 */
266 	fixjobc(p, pgrp, 1);
267 	fixjobc(p, p->p_pgrp, 0);
268 
269 	LIST_REMOVE(p, p_pglist);
270 	if (p->p_pgrp->pg_members.lh_first == 0)
271 		pgdelete(p->p_pgrp);
272 	p->p_pgrp = pgrp;
273 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
274 	return (0);
275 }
276 
277 /*
278  * remove process from process group
279  */
280 int
281 leavepgrp(p)
282 	register struct proc *p;
283 {
284 
285 	LIST_REMOVE(p, p_pglist);
286 	if (p->p_pgrp->pg_members.lh_first == 0)
287 		pgdelete(p->p_pgrp);
288 	p->p_pgrp = 0;
289 	return (0);
290 }
291 
292 /*
293  * delete a process group
294  */
295 void
296 pgdelete(pgrp)
297 	register struct pgrp *pgrp;
298 {
299 
300 	if (pgrp->pg_session->s_ttyp != NULL &&
301 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
302 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
303 	LIST_REMOVE(pgrp, pg_hash);
304 	if (--pgrp->pg_session->s_count == 0)
305 		FREE(pgrp->pg_session, M_SESSION);
306 	FREE(pgrp, M_PGRP);
307 }
308 
309 /*
310  * Adjust pgrp jobc counters when specified process changes process group.
311  * We count the number of processes in each process group that "qualify"
312  * the group for terminal job control (those with a parent in a different
313  * process group of the same session).  If that count reaches zero, the
314  * process group becomes orphaned.  Check both the specified process'
315  * process group and that of its children.
316  * entering == 0 => p is leaving specified group.
317  * entering == 1 => p is entering specified group.
318  */
319 void
320 fixjobc(p, pgrp, entering)
321 	register struct proc *p;
322 	register struct pgrp *pgrp;
323 	int entering;
324 {
325 	register struct pgrp *hispgrp;
326 	register 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 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next)
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(pg)
363 	struct pgrp *pg;
364 {
365 	register struct proc *p;
366 
367 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
368 		if (p->p_stat == SSTOP) {
369 			for (p = pg->pg_members.lh_first; p != 0;
370 			    p = p->p_pglist.le_next) {
371 				psignal(p, SIGHUP);
372 				psignal(p, SIGCONT);
373 			}
374 			return;
375 		}
376 	}
377 }
378 
379 #ifdef DEBUG
380 void
381 pgrpdump()
382 {
383 	register struct pgrp *pgrp;
384 	register struct proc *p;
385 	register int i;
386 
387 	for (i = 0; i <= pgrphash; i++) {
388 		if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
389 			printf("\tindx %d\n", i);
390 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
391 				printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
392 				    pgrp, pgrp->pg_id, pgrp->pg_session,
393 				    pgrp->pg_session->s_count,
394 				    pgrp->pg_members.lh_first);
395 				for (p = pgrp->pg_members.lh_first; p != 0;
396 				    p = p->p_pglist.le_next) {
397 					printf("\t\tpid %d addr %p pgrp %p\n",
398 					    p->p_pid, p, p->p_pgrp);
399 				}
400 			}
401 		}
402 	}
403 }
404 #endif /* DEBUG */
405