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