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