kern_proc.c revision 1.47.2.1 1 /* $NetBSD: kern_proc.c,v 1.47.2.1 2002/07/15 10:36:32 gehenna 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.2.1 2002/07/15 10:36:32 gehenna 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 return (ESRCH);
376 if (mksess) {
377 struct session *sess;
378
379 /*
380 * new session
381 */
382 MALLOC(sess, struct session *, sizeof(struct session),
383 M_SESSION, M_WAITOK);
384 sess->s_sid = p->p_pid;
385 sess->s_leader = p;
386 sess->s_count = 1;
387 sess->s_ttyvp = NULL;
388 sess->s_ttyp = NULL;
389 memcpy(sess->s_login, p->p_session->s_login,
390 sizeof(sess->s_login));
391 p->p_flag &= ~P_CONTROLT;
392 pgrp->pg_session = sess;
393 #ifdef DIAGNOSTIC
394 if (__predict_false(p != curproc))
395 panic("enterpgrp: mksession and p != curproc");
396 #endif
397 } else {
398 SESSHOLD(p->p_session);
399 pgrp->pg_session = p->p_session;
400 }
401 pgrp->pg_id = pgid;
402 LIST_INIT(&pgrp->pg_members);
403 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
404 pgrp->pg_jobc = 0;
405 } else if (pgrp == p->p_pgrp)
406 return (0);
407
408 /*
409 * Adjust eligibility of affected pgrps to participate in job control.
410 * Increment eligibility counts before decrementing, otherwise we
411 * could reach 0 spuriously during the first call.
412 */
413 fixjobc(p, pgrp, 1);
414 fixjobc(p, p->p_pgrp, 0);
415
416 LIST_REMOVE(p, p_pglist);
417 if (p->p_pgrp->pg_members.lh_first == 0)
418 pgdelete(p->p_pgrp);
419 p->p_pgrp = pgrp;
420 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
421 return (0);
422 }
423
424 /*
425 * remove process from process group
426 */
427 int
428 leavepgrp(p)
429 struct proc *p;
430 {
431
432 LIST_REMOVE(p, p_pglist);
433 if (p->p_pgrp->pg_members.lh_first == 0)
434 pgdelete(p->p_pgrp);
435 p->p_pgrp = 0;
436 return (0);
437 }
438
439 /*
440 * delete a process group
441 */
442 void
443 pgdelete(pgrp)
444 struct pgrp *pgrp;
445 {
446
447 /* Remove reference (if any) from tty to this process group */
448 if (pgrp->pg_session->s_ttyp != NULL &&
449 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
450 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
451 LIST_REMOVE(pgrp, pg_hash);
452 SESSRELE(pgrp->pg_session);
453 pool_put(&pgrp_pool, pgrp);
454 }
455
456 /*
457 * Adjust pgrp jobc counters when specified process changes process group.
458 * We count the number of processes in each process group that "qualify"
459 * the group for terminal job control (those with a parent in a different
460 * process group of the same session). If that count reaches zero, the
461 * process group becomes orphaned. Check both the specified process'
462 * process group and that of its children.
463 * entering == 0 => p is leaving specified group.
464 * entering == 1 => p is entering specified group.
465 */
466 void
467 fixjobc(p, pgrp, entering)
468 struct proc *p;
469 struct pgrp *pgrp;
470 int entering;
471 {
472 struct pgrp *hispgrp;
473 struct session *mysession = pgrp->pg_session;
474
475 /*
476 * Check p's parent to see whether p qualifies its own process
477 * group; if so, adjust count for p's process group.
478 */
479 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
480 hispgrp->pg_session == mysession) {
481 if (entering)
482 pgrp->pg_jobc++;
483 else if (--pgrp->pg_jobc == 0)
484 orphanpg(pgrp);
485 }
486
487 /*
488 * Check this process' children to see whether they qualify
489 * their process groups; if so, adjust counts for children's
490 * process groups.
491 */
492 for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
493 if ((hispgrp = p->p_pgrp) != pgrp &&
494 hispgrp->pg_session == mysession &&
495 P_ZOMBIE(p) == 0) {
496 if (entering)
497 hispgrp->pg_jobc++;
498 else if (--hispgrp->pg_jobc == 0)
499 orphanpg(hispgrp);
500 }
501 }
502 }
503
504 /*
505 * A process group has become orphaned;
506 * if there are any stopped processes in the group,
507 * hang-up all process in that group.
508 */
509 static void
510 orphanpg(pg)
511 struct pgrp *pg;
512 {
513 struct proc *p;
514
515 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
516 if (p->p_stat == SSTOP) {
517 for (p = pg->pg_members.lh_first; p != 0;
518 p = p->p_pglist.le_next) {
519 psignal(p, SIGHUP);
520 psignal(p, SIGCONT);
521 }
522 return;
523 }
524 }
525 }
526
527 /* mark process as suid/sgid, reset some values do defaults */
528 void
529 p_sugid(p)
530 struct proc *p;
531 {
532 struct plimit *newlim;
533
534 p->p_flag |= P_SUGID;
535 /* reset what needs to be reset in plimit */
536 if (p->p_limit->pl_corename != defcorename) {
537 if (p->p_limit->p_refcnt > 1 &&
538 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
539 newlim = limcopy(p->p_limit);
540 limfree(p->p_limit);
541 p->p_limit = newlim;
542 } else {
543 free(p->p_limit->pl_corename, M_TEMP);
544 }
545 p->p_limit->pl_corename = defcorename;
546 }
547 }
548
549
550 #ifdef DEBUG
551 void
552 pgrpdump()
553 {
554 struct pgrp *pgrp;
555 struct proc *p;
556 int i;
557
558 for (i = 0; i <= pgrphash; i++) {
559 if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
560 printf("\tindx %d\n", i);
561 for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
562 printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
563 pgrp, pgrp->pg_id, pgrp->pg_session,
564 pgrp->pg_session->s_count,
565 pgrp->pg_members.lh_first);
566 for (p = pgrp->pg_members.lh_first; p != 0;
567 p = p->p_pglist.le_next) {
568 printf("\t\tpid %d addr %p pgrp %p\n",
569 p->p_pid, p, p->p_pgrp);
570 }
571 }
572 }
573 }
574 }
575 #endif /* DEBUG */
576
577 #ifdef KSTACK_CHECK_MAGIC
578 #include <sys/user.h>
579
580 #define KSTACK_MAGIC 0xdeadbeaf
581
582 /* XXX should be per process basis? */
583 int kstackleftmin = KSTACK_SIZE;
584 int kstackleftthres = KSTACK_SIZE / 8; /* warn if remaining stack is less than this */
585
586 void
587 kstack_setup_magic(const struct proc *p)
588 {
589 u_int32_t *ip;
590 u_int32_t const *end;
591
592 KASSERT(p != 0);
593 KASSERT(p != &proc0);
594
595 /*
596 * fill all the stack with magic number
597 * so that later modification on it can be detected.
598 */
599 ip = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
600 end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
601 for (; ip < end; ip++) {
602 *ip = KSTACK_MAGIC;
603 }
604 }
605
606 void
607 kstack_check_magic(const struct proc *p)
608 {
609 u_int32_t const *ip, *end;
610 int stackleft;
611
612 KASSERT(p != 0);
613
614 /* don't check proc0 */ /*XXX*/
615 if (p == &proc0)
616 return;
617
618 #ifdef __MACHINE_STACK_GROWS_UP
619 /* stack grows upwards (eg. hppa) */
620 ip = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
621 end = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
622 for (ip--; ip >= end; ip--)
623 if (*ip != KSTACK_MAGIC)
624 break;
625
626 stackleft = (caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE - (caddr_t)ip;
627 #else /* __MACHINE_STACK_GROWS_UP */
628 /* stack grows downwards (eg. i386) */
629 ip = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
630 end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
631 for (; ip < end; ip++)
632 if (*ip != KSTACK_MAGIC)
633 break;
634
635 stackleft = (caddr_t)ip - KSTACK_LOWEST_ADDR(p);
636 #endif /* __MACHINE_STACK_GROWS_UP */
637
638 if (kstackleftmin > stackleft) {
639 kstackleftmin = stackleft;
640 if (stackleft < kstackleftthres)
641 printf("warning: kernel stack left %d bytes(pid %u)\n",
642 stackleft, p->p_pid);
643 }
644
645 if (stackleft <= 0) {
646 panic("magic on the top of kernel stack changed for pid %u: "
647 "maybe kernel stack overflow\n", p->p_pid);
648 }
649 }
650 #endif /*KSTACK_CHECK_MAGIC*/
651