xref: /netbsd/sys/kern/kern_proc.c (revision bf9ec67e)
1 /*	$NetBSD: kern_proc.c,v 1.47 2002/04/12 17:02:33 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1982, 1986, 1989, 1991, 1993
42  *	The Regents of the University of California.  All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *	This product includes software developed by the University of
55  *	California, Berkeley and its contributors.
56  * 4. Neither the name of the University nor the names of its contributors
57  *    may be used to endorse or promote products derived from this software
58  *    without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  *
72  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
73  */
74 
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.47 2002/04/12 17:02:33 christos Exp $");
77 
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/map.h>
81 #include <sys/kernel.h>
82 #include <sys/proc.h>
83 #include <sys/resourcevar.h>
84 #include <sys/buf.h>
85 #include <sys/acct.h>
86 #include <sys/wait.h>
87 #include <sys/file.h>
88 #include <ufs/ufs/quota.h>
89 #include <sys/uio.h>
90 #include <sys/malloc.h>
91 #include <sys/pool.h>
92 #include <sys/mbuf.h>
93 #include <sys/ioctl.h>
94 #include <sys/tty.h>
95 #include <sys/signalvar.h>
96 
97 /*
98  * Structure associated with user cacheing.
99  */
100 struct uidinfo {
101 	LIST_ENTRY(uidinfo) ui_hash;
102 	uid_t	ui_uid;
103 	long	ui_proccnt;
104 };
105 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
106 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
107 u_long uihash;		/* size of hash table - 1 */
108 
109 /*
110  * Other process lists
111  */
112 struct pidhashhead *pidhashtbl;
113 u_long pidhash;
114 struct pgrphashhead *pgrphashtbl;
115 u_long pgrphash;
116 
117 struct proclist allproc;
118 struct proclist zombproc;	/* resources have been freed */
119 
120 /*
121  * Process list locking:
122  *
123  * We have two types of locks on the proclists: read locks and write
124  * locks.  Read locks can be used in interrupt context, so while we
125  * hold the write lock, we must also block clock interrupts to
126  * lock out any scheduling changes that may happen in interrupt
127  * context.
128  *
129  * The proclist lock locks the following structures:
130  *
131  *	allproc
132  *	zombproc
133  *	pidhashtbl
134  */
135 struct lock proclist_lock;
136 
137 /*
138  * Locking of this proclist is special; it's accessed in a
139  * critical section of process exit, and thus locking it can't
140  * modify interrupt state.  We use a simple spin lock for this
141  * proclist.  Processes on this proclist are also on zombproc;
142  * we use the p_hash member to linkup to deadproc.
143  */
144 struct simplelock deadproc_slock;
145 struct proclist deadproc;	/* dead, but not yet undead */
146 
147 struct pool proc_pool;
148 struct pool pcred_pool;
149 struct pool plimit_pool;
150 struct pool pgrp_pool;
151 struct pool rusage_pool;
152 
153 /*
154  * The process list descriptors, used during pid allocation and
155  * by sysctl.  No locking on this data structure is needed since
156  * it is completely static.
157  */
158 const struct proclist_desc proclists[] = {
159 	{ &allproc	},
160 	{ &zombproc	},
161 	{ NULL		},
162 };
163 
164 static void orphanpg __P((struct pgrp *));
165 #ifdef DEBUG
166 void pgrpdump __P((void));
167 #endif
168 
169 /*
170  * Initialize global process hashing structures.
171  */
172 void
173 procinit()
174 {
175 	const struct proclist_desc *pd;
176 
177 	for (pd = proclists; pd->pd_list != NULL; pd++)
178 		LIST_INIT(pd->pd_list);
179 
180 	spinlockinit(&proclist_lock, "proclk", 0);
181 
182 	LIST_INIT(&deadproc);
183 	simple_lock_init(&deadproc_slock);
184 
185 	pidhashtbl =
186 	    hashinit(maxproc / 4, HASH_LIST, M_PROC, M_WAITOK, &pidhash);
187 	pgrphashtbl =
188 	    hashinit(maxproc / 4, HASH_LIST, M_PROC, M_WAITOK, &pgrphash);
189 	uihashtbl =
190 	    hashinit(maxproc / 16, HASH_LIST, M_PROC, M_WAITOK, &uihash);
191 
192 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
193 	    &pool_allocator_nointr);
194 	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
195 	    &pool_allocator_nointr);
196 	pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
197 	    &pool_allocator_nointr);
198 	pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
199 	    &pool_allocator_nointr);
200 	pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
201 	    &pool_allocator_nointr);
202 }
203 
204 /*
205  * Acquire a read lock on the proclist.
206  */
207 void
208 proclist_lock_read()
209 {
210 	int error;
211 
212 	error = spinlockmgr(&proclist_lock, LK_SHARED, NULL);
213 #ifdef DIAGNOSTIC
214 	if (__predict_false(error != 0))
215 		panic("proclist_lock_read: failed to acquire lock");
216 #endif
217 }
218 
219 /*
220  * Release a read lock on the proclist.
221  */
222 void
223 proclist_unlock_read()
224 {
225 
226 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
227 }
228 
229 /*
230  * Acquire a write lock on the proclist.
231  */
232 int
233 proclist_lock_write()
234 {
235 	int s, error;
236 
237 	s = splclock();
238 	error = spinlockmgr(&proclist_lock, LK_EXCLUSIVE, NULL);
239 #ifdef DIAGNOSTIC
240 	if (__predict_false(error != 0))
241 		panic("proclist_lock: failed to acquire lock");
242 #endif
243 	return (s);
244 }
245 
246 /*
247  * Release a write lock on the proclist.
248  */
249 void
250 proclist_unlock_write(s)
251 	int s;
252 {
253 
254 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
255 	splx(s);
256 }
257 
258 /*
259  * Change the count associated with number of processes
260  * a given user is using.
261  */
262 int
263 chgproccnt(uid, diff)
264 	uid_t	uid;
265 	int	diff;
266 {
267 	struct uidinfo *uip;
268 	struct uihashhead *uipp;
269 
270 	uipp = UIHASH(uid);
271 	for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
272 		if (uip->ui_uid == uid)
273 			break;
274 	if (uip) {
275 		uip->ui_proccnt += diff;
276 		if (uip->ui_proccnt > 0)
277 			return (uip->ui_proccnt);
278 		if (uip->ui_proccnt < 0)
279 			panic("chgproccnt: procs < 0");
280 		LIST_REMOVE(uip, ui_hash);
281 		FREE(uip, M_PROC);
282 		return (0);
283 	}
284 	if (diff <= 0) {
285 		if (diff == 0)
286 			return(0);
287 		panic("chgproccnt: lost user");
288 	}
289 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
290 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
291 	uip->ui_uid = uid;
292 	uip->ui_proccnt = diff;
293 	return (diff);
294 }
295 
296 /*
297  * Is p an inferior of q?
298  */
299 int
300 inferior(p, q)
301 	struct proc *p;
302 	struct proc *q;
303 {
304 
305 	for (; p != q; p = p->p_pptr)
306 		if (p->p_pid == 0)
307 			return (0);
308 	return (1);
309 }
310 
311 /*
312  * Locate a process by number
313  */
314 struct proc *
315 pfind(pid)
316 	pid_t pid;
317 {
318 	struct proc *p;
319 
320 	proclist_lock_read();
321 	for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
322 		if (p->p_pid == pid)
323 			goto out;
324  out:
325 	proclist_unlock_read();
326 	return (p);
327 }
328 
329 /*
330  * Locate a process group by number
331  */
332 struct pgrp *
333 pgfind(pgid)
334 	pid_t pgid;
335 {
336 	struct pgrp *pgrp;
337 
338 	for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
339 		if (pgrp->pg_id == pgid)
340 			return (pgrp);
341 	return (NULL);
342 }
343 
344 /*
345  * Move p to a new or existing process group (and session)
346  */
347 int
348 enterpgrp(p, pgid, mksess)
349 	struct proc *p;
350 	pid_t pgid;
351 	int mksess;
352 {
353 	struct pgrp *pgrp = pgfind(pgid);
354 
355 #ifdef DIAGNOSTIC
356 	if (__predict_false(pgrp != NULL && mksess))	/* firewalls */
357 		panic("enterpgrp: setsid into non-empty pgrp");
358 	if (__predict_false(SESS_LEADER(p)))
359 		panic("enterpgrp: session leader attempted setpgrp");
360 #endif
361 	if (pgrp == NULL) {
362 		pid_t savepid = p->p_pid;
363 		struct proc *np;
364 		/*
365 		 * new process group
366 		 */
367 #ifdef DIAGNOSTIC
368 		if (__predict_false(p->p_pid != pgid))
369 			panic("enterpgrp: new pgrp and pid != pgid");
370 #endif
371 		pgrp = pool_get(&pgrp_pool, PR_WAITOK);
372 		if ((np = pfind(savepid)) == NULL || np != p)
373 			return (ESRCH);
374 		if (mksess) {
375 			struct session *sess;
376 
377 			/*
378 			 * new session
379 			 */
380 			MALLOC(sess, struct session *, sizeof(struct session),
381 			    M_SESSION, M_WAITOK);
382 			sess->s_sid = p->p_pid;
383 			sess->s_leader = p;
384 			sess->s_count = 1;
385 			sess->s_ttyvp = NULL;
386 			sess->s_ttyp = NULL;
387 			memcpy(sess->s_login, p->p_session->s_login,
388 			    sizeof(sess->s_login));
389 			p->p_flag &= ~P_CONTROLT;
390 			pgrp->pg_session = sess;
391 #ifdef DIAGNOSTIC
392 			if (__predict_false(p != curproc))
393 				panic("enterpgrp: mksession and p != curproc");
394 #endif
395 		} else {
396 			SESSHOLD(p->p_session);
397 			pgrp->pg_session = p->p_session;
398 		}
399 		pgrp->pg_id = pgid;
400 		LIST_INIT(&pgrp->pg_members);
401 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
402 		pgrp->pg_jobc = 0;
403 	} else if (pgrp == p->p_pgrp)
404 		return (0);
405 
406 	/*
407 	 * Adjust eligibility of affected pgrps to participate in job control.
408 	 * Increment eligibility counts before decrementing, otherwise we
409 	 * could reach 0 spuriously during the first call.
410 	 */
411 	fixjobc(p, pgrp, 1);
412 	fixjobc(p, p->p_pgrp, 0);
413 
414 	LIST_REMOVE(p, p_pglist);
415 	if (p->p_pgrp->pg_members.lh_first == 0)
416 		pgdelete(p->p_pgrp);
417 	p->p_pgrp = pgrp;
418 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
419 	return (0);
420 }
421 
422 /*
423  * remove process from process group
424  */
425 int
426 leavepgrp(p)
427 	struct proc *p;
428 {
429 
430 	LIST_REMOVE(p, p_pglist);
431 	if (p->p_pgrp->pg_members.lh_first == 0)
432 		pgdelete(p->p_pgrp);
433 	p->p_pgrp = 0;
434 	return (0);
435 }
436 
437 /*
438  * delete a process group
439  */
440 void
441 pgdelete(pgrp)
442 	struct pgrp *pgrp;
443 {
444 
445 	/* Remove reference (if any) from tty to this process group */
446 	if (pgrp->pg_session->s_ttyp != NULL &&
447 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
448 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
449 	LIST_REMOVE(pgrp, pg_hash);
450 	SESSRELE(pgrp->pg_session);
451 	pool_put(&pgrp_pool, pgrp);
452 }
453 
454 /*
455  * Adjust pgrp jobc counters when specified process changes process group.
456  * We count the number of processes in each process group that "qualify"
457  * the group for terminal job control (those with a parent in a different
458  * process group of the same session).  If that count reaches zero, the
459  * process group becomes orphaned.  Check both the specified process'
460  * process group and that of its children.
461  * entering == 0 => p is leaving specified group.
462  * entering == 1 => p is entering specified group.
463  */
464 void
465 fixjobc(p, pgrp, entering)
466 	struct proc *p;
467 	struct pgrp *pgrp;
468 	int entering;
469 {
470 	struct pgrp *hispgrp;
471 	struct session *mysession = pgrp->pg_session;
472 
473 	/*
474 	 * Check p's parent to see whether p qualifies its own process
475 	 * group; if so, adjust count for p's process group.
476 	 */
477 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
478 	    hispgrp->pg_session == mysession) {
479 		if (entering)
480 			pgrp->pg_jobc++;
481 		else if (--pgrp->pg_jobc == 0)
482 			orphanpg(pgrp);
483 	}
484 
485 	/*
486 	 * Check this process' children to see whether they qualify
487 	 * their process groups; if so, adjust counts for children's
488 	 * process groups.
489 	 */
490 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
491 		if ((hispgrp = p->p_pgrp) != pgrp &&
492 		    hispgrp->pg_session == mysession &&
493 		    P_ZOMBIE(p) == 0) {
494 			if (entering)
495 				hispgrp->pg_jobc++;
496 			else if (--hispgrp->pg_jobc == 0)
497 				orphanpg(hispgrp);
498 		}
499 	}
500 }
501 
502 /*
503  * A process group has become orphaned;
504  * if there are any stopped processes in the group,
505  * hang-up all process in that group.
506  */
507 static void
508 orphanpg(pg)
509 	struct pgrp *pg;
510 {
511 	struct proc *p;
512 
513 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
514 		if (p->p_stat == SSTOP) {
515 			for (p = pg->pg_members.lh_first; p != 0;
516 			    p = p->p_pglist.le_next) {
517 				psignal(p, SIGHUP);
518 				psignal(p, SIGCONT);
519 			}
520 			return;
521 		}
522 	}
523 }
524 
525 /* mark process as suid/sgid, reset some values do defaults */
526 void
527 p_sugid(p)
528 	struct proc *p;
529 {
530 	struct plimit *newlim;
531 
532 	p->p_flag |= P_SUGID;
533 	/* reset what needs to be reset in plimit */
534 	if (p->p_limit->pl_corename != defcorename) {
535 		if (p->p_limit->p_refcnt > 1 &&
536 		    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
537 			newlim = limcopy(p->p_limit);
538 			limfree(p->p_limit);
539 			p->p_limit = newlim;
540 		} else {
541 			free(p->p_limit->pl_corename, M_TEMP);
542 		}
543 		p->p_limit->pl_corename = defcorename;
544 	}
545 }
546 
547 
548 #ifdef DEBUG
549 void
550 pgrpdump()
551 {
552 	struct pgrp *pgrp;
553 	struct proc *p;
554 	int i;
555 
556 	for (i = 0; i <= pgrphash; i++) {
557 		if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
558 			printf("\tindx %d\n", i);
559 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
560 				printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
561 				    pgrp, pgrp->pg_id, pgrp->pg_session,
562 				    pgrp->pg_session->s_count,
563 				    pgrp->pg_members.lh_first);
564 				for (p = pgrp->pg_members.lh_first; p != 0;
565 				    p = p->p_pglist.le_next) {
566 					printf("\t\tpid %d addr %p pgrp %p\n",
567 					    p->p_pid, p, p->p_pgrp);
568 				}
569 			}
570 		}
571 	}
572 }
573 #endif /* DEBUG */
574