kern_proc.c revision 1.51 1 /* $NetBSD: kern_proc.c,v 1.51 2002/08/28 07:16:38 gmcgarry 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.51 2002/08/28 07:16:38 gmcgarry Exp $");
77
78 #include "opt_kstack.h"
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/map.h>
83 #include <sys/kernel.h>
84 #include <sys/proc.h>
85 #include <sys/resourcevar.h>
86 #include <sys/buf.h>
87 #include <sys/acct.h>
88 #include <sys/wait.h>
89 #include <sys/file.h>
90 #include <ufs/ufs/quota.h>
91 #include <sys/uio.h>
92 #include <sys/malloc.h>
93 #include <sys/pool.h>
94 #include <sys/mbuf.h>
95 #include <sys/ioctl.h>
96 #include <sys/tty.h>
97 #include <sys/signalvar.h>
98 #include <sys/ras.h>
99
100 /*
101 * Structure associated with user cacheing.
102 */
103 struct uidinfo {
104 LIST_ENTRY(uidinfo) ui_hash;
105 uid_t ui_uid;
106 long ui_proccnt;
107 };
108 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
109 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
110 u_long uihash; /* size of hash table - 1 */
111
112 /*
113 * Other process lists
114 */
115 struct pidhashhead *pidhashtbl;
116 u_long pidhash;
117 struct pgrphashhead *pgrphashtbl;
118 u_long pgrphash;
119
120 struct proclist allproc;
121 struct proclist zombproc; /* resources have been freed */
122
123 /*
124 * Process list locking:
125 *
126 * We have two types of locks on the proclists: read locks and write
127 * locks. Read locks can be used in interrupt context, so while we
128 * hold the write lock, we must also block clock interrupts to
129 * lock out any scheduling changes that may happen in interrupt
130 * context.
131 *
132 * The proclist lock locks the following structures:
133 *
134 * allproc
135 * zombproc
136 * pidhashtbl
137 */
138 struct lock proclist_lock;
139
140 /*
141 * Locking of this proclist is special; it's accessed in a
142 * critical section of process exit, and thus locking it can't
143 * modify interrupt state. We use a simple spin lock for this
144 * proclist. Processes on this proclist are also on zombproc;
145 * we use the p_hash member to linkup to deadproc.
146 */
147 struct simplelock deadproc_slock;
148 struct proclist deadproc; /* dead, but not yet undead */
149
150 struct pool proc_pool;
151 struct pool pcred_pool;
152 struct pool plimit_pool;
153 struct pool pgrp_pool;
154 struct pool rusage_pool;
155 struct pool ras_pool;
156
157 /*
158 * The process list descriptors, used during pid allocation and
159 * by sysctl. No locking on this data structure is needed since
160 * it is completely static.
161 */
162 const struct proclist_desc proclists[] = {
163 { &allproc },
164 { &zombproc },
165 { NULL },
166 };
167
168 static void orphanpg __P((struct pgrp *));
169 #ifdef DEBUG
170 void pgrpdump __P((void));
171 #endif
172
173 /*
174 * Initialize global process hashing structures.
175 */
176 void
177 procinit()
178 {
179 const struct proclist_desc *pd;
180
181 for (pd = proclists; pd->pd_list != NULL; pd++)
182 LIST_INIT(pd->pd_list);
183
184 spinlockinit(&proclist_lock, "proclk", 0);
185
186 LIST_INIT(&deadproc);
187 simple_lock_init(&deadproc_slock);
188
189 pidhashtbl =
190 hashinit(maxproc / 4, HASH_LIST, M_PROC, M_WAITOK, &pidhash);
191 pgrphashtbl =
192 hashinit(maxproc / 4, HASH_LIST, M_PROC, M_WAITOK, &pgrphash);
193 uihashtbl =
194 hashinit(maxproc / 16, HASH_LIST, M_PROC, M_WAITOK, &uihash);
195
196 pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
197 &pool_allocator_nointr);
198 pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
199 &pool_allocator_nointr);
200 pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
201 &pool_allocator_nointr);
202 pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
203 &pool_allocator_nointr);
204 pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
205 &pool_allocator_nointr);
206 pool_init(&ras_pool, sizeof(struct ras), 0, 0, 0, "raspl",
207 &pool_allocator_nointr);
208 }
209
210 /*
211 * Acquire a read lock on the proclist.
212 */
213 void
214 proclist_lock_read()
215 {
216 int error;
217
218 error = spinlockmgr(&proclist_lock, LK_SHARED, NULL);
219 #ifdef DIAGNOSTIC
220 if (__predict_false(error != 0))
221 panic("proclist_lock_read: failed to acquire lock");
222 #endif
223 }
224
225 /*
226 * Release a read lock on the proclist.
227 */
228 void
229 proclist_unlock_read()
230 {
231
232 (void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
233 }
234
235 /*
236 * Acquire a write lock on the proclist.
237 */
238 int
239 proclist_lock_write()
240 {
241 int s, error;
242
243 s = splclock();
244 error = spinlockmgr(&proclist_lock, LK_EXCLUSIVE, NULL);
245 #ifdef DIAGNOSTIC
246 if (__predict_false(error != 0))
247 panic("proclist_lock: failed to acquire lock");
248 #endif
249 return (s);
250 }
251
252 /*
253 * Release a write lock on the proclist.
254 */
255 void
256 proclist_unlock_write(s)
257 int s;
258 {
259
260 (void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
261 splx(s);
262 }
263
264 /*
265 * Change the count associated with number of processes
266 * a given user is using.
267 */
268 int
269 chgproccnt(uid, diff)
270 uid_t uid;
271 int diff;
272 {
273 struct uidinfo *uip;
274 struct uihashhead *uipp;
275
276 uipp = UIHASH(uid);
277 for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
278 if (uip->ui_uid == uid)
279 break;
280 if (uip) {
281 uip->ui_proccnt += diff;
282 if (uip->ui_proccnt > 0)
283 return (uip->ui_proccnt);
284 if (uip->ui_proccnt < 0)
285 panic("chgproccnt: procs < 0");
286 LIST_REMOVE(uip, ui_hash);
287 FREE(uip, M_PROC);
288 return (0);
289 }
290 if (diff <= 0) {
291 if (diff == 0)
292 return(0);
293 panic("chgproccnt: lost user");
294 }
295 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
296 LIST_INSERT_HEAD(uipp, uip, ui_hash);
297 uip->ui_uid = uid;
298 uip->ui_proccnt = diff;
299 return (diff);
300 }
301
302 /*
303 * Is p an inferior of q?
304 */
305 int
306 inferior(p, q)
307 struct proc *p;
308 struct proc *q;
309 {
310
311 for (; p != q; p = p->p_pptr)
312 if (p->p_pid == 0)
313 return (0);
314 return (1);
315 }
316
317 /*
318 * Locate a process by number
319 */
320 struct proc *
321 pfind(pid)
322 pid_t pid;
323 {
324 struct proc *p;
325
326 proclist_lock_read();
327 for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
328 if (p->p_pid == pid)
329 goto out;
330 out:
331 proclist_unlock_read();
332 return (p);
333 }
334
335 /*
336 * Locate a process group by number
337 */
338 struct pgrp *
339 pgfind(pgid)
340 pid_t pgid;
341 {
342 struct pgrp *pgrp;
343
344 for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != NULL;
345 pgrp = pgrp->pg_hash.le_next)
346 if (pgrp->pg_id == pgid)
347 return (pgrp);
348 return (NULL);
349 }
350
351 /*
352 * Move p to a new or existing process group (and session)
353 */
354 int
355 enterpgrp(p, pgid, mksess)
356 struct proc *p;
357 pid_t pgid;
358 int mksess;
359 {
360 struct pgrp *pgrp = pgfind(pgid);
361
362 #ifdef DIAGNOSTIC
363 if (__predict_false(pgrp != NULL && mksess)) /* firewalls */
364 panic("enterpgrp: setsid into non-empty pgrp");
365 if (__predict_false(SESS_LEADER(p)))
366 panic("enterpgrp: session leader attempted setpgrp");
367 #endif
368 if (pgrp == NULL) {
369 pid_t savepid = p->p_pid;
370 struct proc *np;
371 /*
372 * new process group
373 */
374 #ifdef DIAGNOSTIC
375 if (__predict_false(p->p_pid != pgid))
376 panic("enterpgrp: new pgrp and pid != pgid");
377 #endif
378 pgrp = pool_get(&pgrp_pool, PR_WAITOK);
379 if ((np = pfind(savepid)) == NULL || np != p) {
380 pool_put(&pgrp_pool, pgrp);
381 return (ESRCH);
382 }
383 if (mksess) {
384 struct session *sess;
385
386 /*
387 * new session
388 */
389 MALLOC(sess, struct session *, sizeof(struct session),
390 M_SESSION, M_WAITOK);
391 if ((np = pfind(savepid)) == NULL || np != p) {
392 FREE(sess, M_SESSION);
393 pool_put(&pgrp_pool, pgrp);
394 return (ESRCH);
395 }
396 sess->s_sid = p->p_pid;
397 sess->s_leader = p;
398 sess->s_count = 1;
399 sess->s_ttyvp = NULL;
400 sess->s_ttyp = NULL;
401 memcpy(sess->s_login, p->p_session->s_login,
402 sizeof(sess->s_login));
403 p->p_flag &= ~P_CONTROLT;
404 pgrp->pg_session = sess;
405 #ifdef DIAGNOSTIC
406 if (__predict_false(p != curproc))
407 panic("enterpgrp: mksession and p != curproc");
408 #endif
409 } else {
410 SESSHOLD(p->p_session);
411 pgrp->pg_session = p->p_session;
412 }
413 pgrp->pg_id = pgid;
414 LIST_INIT(&pgrp->pg_members);
415 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
416 pgrp->pg_jobc = 0;
417 } else if (pgrp == p->p_pgrp)
418 return (0);
419
420 /*
421 * Adjust eligibility of affected pgrps to participate in job control.
422 * Increment eligibility counts before decrementing, otherwise we
423 * could reach 0 spuriously during the first call.
424 */
425 fixjobc(p, pgrp, 1);
426 fixjobc(p, p->p_pgrp, 0);
427
428 LIST_REMOVE(p, p_pglist);
429 if (p->p_pgrp->pg_members.lh_first == 0)
430 pgdelete(p->p_pgrp);
431 p->p_pgrp = pgrp;
432 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
433 return (0);
434 }
435
436 /*
437 * remove process from process group
438 */
439 int
440 leavepgrp(p)
441 struct proc *p;
442 {
443
444 LIST_REMOVE(p, p_pglist);
445 if (p->p_pgrp->pg_members.lh_first == 0)
446 pgdelete(p->p_pgrp);
447 p->p_pgrp = 0;
448 return (0);
449 }
450
451 /*
452 * delete a process group
453 */
454 void
455 pgdelete(pgrp)
456 struct pgrp *pgrp;
457 {
458
459 /* Remove reference (if any) from tty to this process group */
460 if (pgrp->pg_session->s_ttyp != NULL &&
461 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
462 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
463 LIST_REMOVE(pgrp, pg_hash);
464 SESSRELE(pgrp->pg_session);
465 pool_put(&pgrp_pool, pgrp);
466 }
467
468 /*
469 * Adjust pgrp jobc counters when specified process changes process group.
470 * We count the number of processes in each process group that "qualify"
471 * the group for terminal job control (those with a parent in a different
472 * process group of the same session). If that count reaches zero, the
473 * process group becomes orphaned. Check both the specified process'
474 * process group and that of its children.
475 * entering == 0 => p is leaving specified group.
476 * entering == 1 => p is entering specified group.
477 */
478 void
479 fixjobc(p, pgrp, entering)
480 struct proc *p;
481 struct pgrp *pgrp;
482 int entering;
483 {
484 struct pgrp *hispgrp;
485 struct session *mysession = pgrp->pg_session;
486
487 /*
488 * Check p's parent to see whether p qualifies its own process
489 * group; if so, adjust count for p's process group.
490 */
491 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
492 hispgrp->pg_session == mysession) {
493 if (entering)
494 pgrp->pg_jobc++;
495 else if (--pgrp->pg_jobc == 0)
496 orphanpg(pgrp);
497 }
498
499 /*
500 * Check this process' children to see whether they qualify
501 * their process groups; if so, adjust counts for children's
502 * process groups.
503 */
504 for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
505 if ((hispgrp = p->p_pgrp) != pgrp &&
506 hispgrp->pg_session == mysession &&
507 P_ZOMBIE(p) == 0) {
508 if (entering)
509 hispgrp->pg_jobc++;
510 else if (--hispgrp->pg_jobc == 0)
511 orphanpg(hispgrp);
512 }
513 }
514 }
515
516 /*
517 * A process group has become orphaned;
518 * if there are any stopped processes in the group,
519 * hang-up all process in that group.
520 */
521 static void
522 orphanpg(pg)
523 struct pgrp *pg;
524 {
525 struct proc *p;
526
527 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
528 if (p->p_stat == SSTOP) {
529 for (p = pg->pg_members.lh_first; p != 0;
530 p = p->p_pglist.le_next) {
531 psignal(p, SIGHUP);
532 psignal(p, SIGCONT);
533 }
534 return;
535 }
536 }
537 }
538
539 /* mark process as suid/sgid, reset some values do defaults */
540 void
541 p_sugid(p)
542 struct proc *p;
543 {
544 struct plimit *newlim;
545
546 p->p_flag |= P_SUGID;
547 /* reset what needs to be reset in plimit */
548 if (p->p_limit->pl_corename != defcorename) {
549 if (p->p_limit->p_refcnt > 1 &&
550 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
551 newlim = limcopy(p->p_limit);
552 limfree(p->p_limit);
553 p->p_limit = newlim;
554 }
555 free(p->p_limit->pl_corename, M_TEMP);
556 p->p_limit->pl_corename = defcorename;
557 }
558 }
559
560 #ifdef DEBUG
561 void
562 pgrpdump()
563 {
564 struct pgrp *pgrp;
565 struct proc *p;
566 int i;
567
568 for (i = 0; i <= pgrphash; i++) {
569 if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
570 printf("\tindx %d\n", i);
571 for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
572 printf("\tpgrp %p, pgid %d, sess %p, "
573 "sesscnt %d, mem %p\n",
574 pgrp, pgrp->pg_id, pgrp->pg_session,
575 pgrp->pg_session->s_count,
576 pgrp->pg_members.lh_first);
577 for (p = pgrp->pg_members.lh_first; p != 0;
578 p = p->p_pglist.le_next) {
579 printf("\t\tpid %d addr %p pgrp %p\n",
580 p->p_pid, p, p->p_pgrp);
581 }
582 }
583 }
584 }
585 }
586 #endif /* DEBUG */
587
588 #ifdef KSTACK_CHECK_MAGIC
589 #include <sys/user.h>
590
591 #define KSTACK_MAGIC 0xdeadbeaf
592
593 /* XXX should be per process basis? */
594 int kstackleftmin = KSTACK_SIZE;
595 int kstackleftthres = KSTACK_SIZE / 8; /* warn if remaining stack is
596 less than this */
597
598 void
599 kstack_setup_magic(const struct proc *p)
600 {
601 u_int32_t *ip;
602 u_int32_t const *end;
603
604 KASSERT(p != 0);
605 KASSERT(p != &proc0);
606
607 /*
608 * fill all the stack with magic number
609 * so that later modification on it can be detected.
610 */
611 ip = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
612 end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
613 for (; ip < end; ip++) {
614 *ip = KSTACK_MAGIC;
615 }
616 }
617
618 void
619 kstack_check_magic(const struct proc *p)
620 {
621 u_int32_t const *ip, *end;
622 int stackleft;
623
624 KASSERT(p != 0);
625
626 /* don't check proc0 */ /*XXX*/
627 if (p == &proc0)
628 return;
629
630 #ifdef __MACHINE_STACK_GROWS_UP
631 /* stack grows upwards (eg. hppa) */
632 ip = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
633 end = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
634 for (ip--; ip >= end; ip--)
635 if (*ip != KSTACK_MAGIC)
636 break;
637
638 stackleft = (caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE - (caddr_t)ip;
639 #else /* __MACHINE_STACK_GROWS_UP */
640 /* stack grows downwards (eg. i386) */
641 ip = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
642 end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
643 for (; ip < end; ip++)
644 if (*ip != KSTACK_MAGIC)
645 break;
646
647 stackleft = (caddr_t)ip - KSTACK_LOWEST_ADDR(p);
648 #endif /* __MACHINE_STACK_GROWS_UP */
649
650 if (kstackleftmin > stackleft) {
651 kstackleftmin = stackleft;
652 if (stackleft < kstackleftthres)
653 printf("warning: kernel stack left %d bytes(pid %u)\n",
654 stackleft, p->p_pid);
655 }
656
657 if (stackleft <= 0) {
658 panic("magic on the top of kernel stack changed for pid %u: "
659 "maybe kernel stack overflow\n", p->p_pid);
660 }
661 }
662 #endif /* KSTACK_CHECK_MAGIC */
663