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