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