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