kern_proc.c revision 1.94.4.2 1 /* $NetBSD: kern_proc.c,v 1.94.4.2 2006/09/11 18:43:43 ad 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. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
69 */
70
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.94.4.2 2006/09/11 18:43:43 ad Exp $");
73
74 #include "opt_kstack.h"
75 #include "opt_maxuprc.h"
76 #include "opt_multiprocessor.h"
77 #include "opt_lockdebug.h"
78
79 #include <sys/param.h>
80 #include <sys/systm.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 #include <sys/ras.h>
97 #include <sys/sa.h>
98 #include <sys/savar.h>
99 #include <sys/filedesc.h>
100 #include <sys/kauth.h>
101 #include <sys/turnstile.h>
102
103 #include <uvm/uvm.h>
104 #include <uvm/uvm_extern.h>
105
106 /*
107 * Other process lists
108 */
109
110 struct proclist allproc;
111 struct proclist zombproc; /* resources have been freed */
112
113 /*
114 * There are three locks on global process state.
115 *
116 * 1. proclist_lock is a reader/writer lock and is used when modifying or
117 * examining process state from a process context. It protects our internal
118 * tables, all of the process lists, and a number of members of struct lwp
119 * and struct proc.
120
121 * 2. proclist_mutex is used when allproc must be traversed from an
122 * interrupt context, or when we must signal processes from an interrupt
123 * context. The proclist_lock should always be used in preference.
124 *
125 * 3. alllwp_mutex protects the "alllwp" list.
126 *
127 * proclist_lock proclist_mutex alllwp_mutex structure
128 * --------------- --------------- --------------- -----------------
129 * x zombproc
130 * x x pid_table
131 * x proc::p_pptr
132 * x proc::p_sibling
133 * x proc::p_children
134 * x x allproc
135 * x x proc::p_pgrp
136 * x x proc::p_pglist
137 * x x proc::p_session
138 * x x proc::p_list
139 * x alllwp
140 * x lwp::l_list
141 */
142 krwlock_t proclist_lock;
143 kmutex_t proclist_mutex;
144
145 /*
146 * pid to proc lookup is done by indexing the pid_table array.
147 * Since pid numbers are only allocated when an empty slot
148 * has been found, there is no need to search any lists ever.
149 * (an orphaned pgrp will lock the slot, a session will lock
150 * the pgrp with the same number.)
151 * If the table is too small it is reallocated with twice the
152 * previous size and the entries 'unzipped' into the two halves.
153 * A linked list of free entries is passed through the pt_proc
154 * field of 'free' items - set odd to be an invalid ptr.
155 */
156
157 struct pid_table {
158 struct proc *pt_proc;
159 struct pgrp *pt_pgrp;
160 };
161 #if 1 /* strongly typed cast - should be a noop */
162 static inline uint p2u(struct proc *p) { return (uint)(uintptr_t)p; }
163 #else
164 #define p2u(p) ((uint)p)
165 #endif
166 #define P_VALID(p) (!(p2u(p) & 1))
167 #define P_NEXT(p) (p2u(p) >> 1)
168 #define P_FREE(pid) ((struct proc *)(uintptr_t)((pid) << 1 | 1))
169
170 #define INITIAL_PID_TABLE_SIZE (1 << 5)
171 static struct pid_table *pid_table;
172 static uint pid_tbl_mask = INITIAL_PID_TABLE_SIZE - 1;
173 static uint pid_alloc_lim; /* max we allocate before growing table */
174 static uint pid_alloc_cnt; /* number of allocated pids */
175
176 /* links through free slots - never empty! */
177 static uint next_free_pt, last_free_pt;
178 static pid_t pid_max = PID_MAX; /* largest value we allocate */
179
180 /* Components of the first process -- never freed. */
181 struct session session0;
182 struct pgrp pgrp0;
183 struct proc proc0;
184 struct lwp lwp0;
185 kauth_cred_t cred0;
186 struct filedesc0 filedesc0;
187 struct cwdinfo cwdi0;
188 struct plimit limit0;
189 struct pstats pstat0;
190 struct vmspace vmspace0;
191 struct sigacts sigacts0;
192 struct turnstile turnstile0;
193
194 extern struct user *proc0paddr;
195
196 extern const struct emul emul_netbsd; /* defined in kern_exec.c */
197
198 int nofile = NOFILE;
199 int maxuprc = MAXUPRC;
200 int cmask = CMASK;
201
202 POOL_INIT(proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
203 &pool_allocator_nointr);
204 POOL_INIT(lwp_pool, sizeof(struct lwp), 0, 0, 0, "lwppl",
205 &pool_allocator_nointr);
206 POOL_INIT(lwp_uc_pool, sizeof(ucontext_t), 0, 0, 0, "lwpucpl",
207 &pool_allocator_nointr);
208 POOL_INIT(pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
209 &pool_allocator_nointr);
210 POOL_INIT(plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
211 &pool_allocator_nointr);
212 POOL_INIT(pstats_pool, sizeof(struct pstats), 0, 0, 0, "pstatspl",
213 &pool_allocator_nointr);
214 POOL_INIT(rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
215 &pool_allocator_nointr);
216 POOL_INIT(ras_pool, sizeof(struct ras), 0, 0, 0, "raspl",
217 &pool_allocator_nointr);
218 POOL_INIT(session_pool, sizeof(struct session), 0, 0, 0, "sessionpl",
219 &pool_allocator_nointr);
220
221 MALLOC_DEFINE(M_EMULDATA, "emuldata", "Per-process emulation data");
222 MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
223 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
224
225 /*
226 * The process list descriptors, used during pid allocation and
227 * by sysctl. No locking on this data structure is needed since
228 * it is completely static.
229 */
230 const struct proclist_desc proclists[] = {
231 { &allproc },
232 { &zombproc },
233 { NULL },
234 };
235
236 static void orphanpg(struct pgrp *);
237 static void pg_delete(pid_t);
238
239 /*
240 * Initialize global process hashing structures.
241 */
242 void
243 procinit(void)
244 {
245 const struct proclist_desc *pd;
246 int i;
247 #define LINK_EMPTY ((PID_MAX + INITIAL_PID_TABLE_SIZE) & ~(INITIAL_PID_TABLE_SIZE - 1))
248
249 for (pd = proclists; pd->pd_list != NULL; pd++)
250 LIST_INIT(pd->pd_list);
251
252 rw_init(&proclist_lock);
253 mutex_init(&proclist_mutex, MUTEX_SPIN, IPL_SCHED);
254 mutex_init(&alllwp_mutex, MUTEX_SPIN, IPL_SCHED);
255
256 pid_table = malloc(INITIAL_PID_TABLE_SIZE * sizeof *pid_table,
257 M_PROC, M_WAITOK);
258 /* Set free list running through table...
259 Preset 'use count' above PID_MAX so we allocate pid 1 next. */
260 for (i = 0; i <= pid_tbl_mask; i++) {
261 pid_table[i].pt_proc = P_FREE(LINK_EMPTY + i + 1);
262 pid_table[i].pt_pgrp = 0;
263 }
264 /* slot 0 is just grabbed */
265 next_free_pt = 1;
266 /* Need to fix last entry. */
267 last_free_pt = pid_tbl_mask;
268 pid_table[last_free_pt].pt_proc = P_FREE(LINK_EMPTY);
269 /* point at which we grow table - to avoid reusing pids too often */
270 pid_alloc_lim = pid_tbl_mask - 1;
271 #undef LINK_EMPTY
272
273 LIST_INIT(&alllwp);
274
275 uihashtbl =
276 hashinit(maxproc / 16, HASH_LIST, M_PROC, M_WAITOK, &uihash);
277 }
278
279 /*
280 * Initialize process 0.
281 */
282 void
283 proc0_init(void)
284 {
285 struct proc *p;
286 struct pgrp *pg;
287 struct session *sess;
288 struct lwp *l;
289 u_int i;
290 rlim_t lim;
291
292 p = &proc0;
293 pg = &pgrp0;
294 sess = &session0;
295 l = &lwp0;
296
297 simple_lock_init(&p->p_lock);
298 mutex_init(&p->p_crmutex, MUTEX_DEFAULT, IPL_NONE);
299 LIST_INIT(&p->p_lwps);
300 LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
301 p->p_nlwps = 1;
302 simple_lock_init(&p->p_sigctx.ps_silock);
303 CIRCLEQ_INIT(&p->p_sigctx.ps_siginfo);
304
305 pid_table[0].pt_proc = p;
306 LIST_INSERT_HEAD(&allproc, p, p_list);
307 LIST_INSERT_HEAD(&alllwp, l, l_list);
308
309 p->p_pgrp = pg;
310 pid_table[0].pt_pgrp = pg;
311 LIST_INIT(&pg->pg_members);
312 LIST_INSERT_HEAD(&pg->pg_members, p, p_pglist);
313
314 pg->pg_session = sess;
315 sess->s_count = 1;
316 sess->s_sid = 0;
317 sess->s_leader = p;
318
319 /*
320 * Set P_NOCLDWAIT so that kernel threads are reparented to
321 * init(8) when they exit. init(8) can easily wait them out
322 * for us.
323 */
324 p->p_flag = P_SYSTEM | P_NOCLDWAIT;
325 p->p_stat = SACTIVE;
326 p->p_nice = NZERO;
327 p->p_emul = &emul_netbsd;
328 #ifdef __HAVE_SYSCALL_INTERN
329 (*p->p_emul->e_syscall_intern)(p);
330 #endif
331 strncpy(p->p_comm, "swapper", MAXCOMLEN);
332
333 l->l_flag = L_INMEM;
334 l->l_stat = LSONPROC;
335 l->l_ts = &turnstile0;
336 p->p_nrlwps = 1;
337
338 callout_init(&l->l_tsleep_ch);
339
340 /* Create credentials. */
341 cred0 = kauth_cred_alloc();
342 p->p_cred = cred0;
343 kauth_cred_hold(cred0);
344 l->l_cred = cred0;
345
346 /* Create the CWD info. */
347 p->p_cwdi = &cwdi0;
348 cwdi0.cwdi_cmask = cmask;
349 cwdi0.cwdi_refcnt = 1;
350 simple_lock_init(&cwdi0.cwdi_slock);
351
352 /* Create the limits structures. */
353 p->p_limit = &limit0;
354 simple_lock_init(&limit0.p_slock);
355 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
356 limit0.pl_rlimit[i].rlim_cur =
357 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
358
359 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
360 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
361 maxfiles < nofile ? maxfiles : nofile;
362
363 limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
364 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
365 maxproc < maxuprc ? maxproc : maxuprc;
366
367 lim = ptoa(uvmexp.free);
368 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
369 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
370 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
371 limit0.pl_corename = defcorename;
372 limit0.p_refcnt = 1;
373
374 /* Configure virtual memory system, set vm rlimits. */
375 uvm_init_limits(p);
376
377 /* Initialize file descriptor table for proc0. */
378 p->p_fd = &filedesc0.fd_fd;
379 fdinit1(&filedesc0);
380
381 /*
382 * Initialize proc0's vmspace, which uses the kernel pmap.
383 * All kernel processes (which never have user space mappings)
384 * share proc0's vmspace, and thus, the kernel pmap.
385 */
386 uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
387 trunc_page(VM_MAX_ADDRESS));
388 p->p_vmspace = &vmspace0;
389
390 l->l_addr = proc0paddr; /* XXX */
391
392 p->p_stats = &pstat0;
393
394 /* Initialize signal state for proc0. */
395 p->p_sigacts = &sigacts0;
396 siginit(p);
397 }
398
399 /*
400 * Check that the specified process group is in the session of the
401 * specified process.
402 * Treats -ve ids as process ids.
403 * Used to validate TIOCSPGRP requests.
404 */
405 int
406 pgid_in_session(struct proc *p, pid_t pg_id)
407 {
408 struct pgrp *pgrp;
409
410 if (pg_id < 0) {
411 struct proc *p1 = pfind(-pg_id);
412 if (p1 == NULL)
413 return EINVAL;
414 pgrp = p1->p_pgrp;
415 } else {
416 pgrp = pgfind(pg_id);
417 if (pgrp == NULL)
418 return EINVAL;
419 }
420 if (pgrp->pg_session != p->p_pgrp->pg_session)
421 return EPERM;
422 return 0;
423 }
424
425 /*
426 * Is p an inferior of q?
427 *
428 * Call with the proclist_lock held.
429 */
430 int
431 inferior(struct proc *p, struct proc *q)
432 {
433
434 for (; p != q; p = p->p_pptr)
435 if (p->p_pid == 0)
436 return 0;
437 return 1;
438 }
439
440 /*
441 * Locate a process by number
442 */
443 struct proc *
444 p_find(pid_t pid, uint flags)
445 {
446 struct proc *p;
447 char stat;
448
449 if (!(flags & PFIND_LOCKED))
450 rw_enter(&proclist_lock, RW_READER);
451
452 p = pid_table[pid & pid_tbl_mask].pt_proc;
453 /* Only allow live processes to be found by pid. */
454 if (P_VALID(p) && p->p_pid == pid &&
455 ((stat = p->p_stat) == SACTIVE || stat == SSTOP
456 || (stat == SZOMB && (flags & PFIND_ZOMBIE)))) {
457 if (flags & PFIND_UNLOCK_OK)
458 rw_exit(&proclist_lock);
459 return p;
460 }
461 if (flags & PFIND_UNLOCK_FAIL)
462 rw_exit(&proclist_lock);
463 return NULL;
464 }
465
466
467 /*
468 * Locate a process group by number
469 */
470 struct pgrp *
471 pg_find(pid_t pgid, uint flags)
472 {
473 struct pgrp *pg;
474
475 if (!(flags & PFIND_LOCKED))
476 rw_enter(&proclist_lock, RW_READER);
477 pg = pid_table[pgid & pid_tbl_mask].pt_pgrp;
478 /*
479 * Can't look up a pgrp that only exists because the session
480 * hasn't died yet (traditional)
481 */
482 if (pg == NULL || pg->pg_id != pgid || LIST_EMPTY(&pg->pg_members)) {
483 if (flags & PFIND_UNLOCK_FAIL)
484 rw_exit(&proclist_lock);
485 return NULL;
486 }
487
488 if (flags & PFIND_UNLOCK_OK)
489 rw_exit(&proclist_lock);
490 return pg;
491 }
492
493 static void
494 expand_pid_table(void)
495 {
496 uint pt_size = pid_tbl_mask + 1;
497 struct pid_table *n_pt, *new_pt;
498 struct proc *proc;
499 struct pgrp *pgrp;
500 int i;
501 pid_t pid;
502
503 new_pt = malloc(pt_size * 2 * sizeof *new_pt, M_PROC, M_WAITOK);
504
505 rw_enter(&proclist_lock, RW_WRITER);
506 if (pt_size != pid_tbl_mask + 1) {
507 /* Another process beat us to it... */
508 rw_exit(&proclist_lock);
509 FREE(new_pt, M_PROC);
510 return;
511 }
512
513 /*
514 * Copy entries from old table into new one.
515 * If 'pid' is 'odd' we need to place in the upper half,
516 * even pid's to the lower half.
517 * Free items stay in the low half so we don't have to
518 * fixup the reference to them.
519 * We stuff free items on the front of the freelist
520 * because we can't write to unmodified entries.
521 * Processing the table backwards maintains a semblance
522 * of issueing pid numbers that increase with time.
523 */
524 i = pt_size - 1;
525 n_pt = new_pt + i;
526 for (; ; i--, n_pt--) {
527 proc = pid_table[i].pt_proc;
528 pgrp = pid_table[i].pt_pgrp;
529 if (!P_VALID(proc)) {
530 /* Up 'use count' so that link is valid */
531 pid = (P_NEXT(proc) + pt_size) & ~pt_size;
532 proc = P_FREE(pid);
533 if (pgrp)
534 pid = pgrp->pg_id;
535 } else
536 pid = proc->p_pid;
537
538 /* Save entry in appropriate half of table */
539 n_pt[pid & pt_size].pt_proc = proc;
540 n_pt[pid & pt_size].pt_pgrp = pgrp;
541
542 /* Put other piece on start of free list */
543 pid = (pid ^ pt_size) & ~pid_tbl_mask;
544 n_pt[pid & pt_size].pt_proc =
545 P_FREE((pid & ~pt_size) | next_free_pt);
546 n_pt[pid & pt_size].pt_pgrp = 0;
547 next_free_pt = i | (pid & pt_size);
548 if (i == 0)
549 break;
550 }
551
552 /* Switch tables */
553 mutex_enter(&proclist_mutex);
554 n_pt = pid_table;
555 pid_table = new_pt;
556 mutex_exit(&proclist_mutex);
557 pid_tbl_mask = pt_size * 2 - 1;
558
559 /*
560 * pid_max starts as PID_MAX (= 30000), once we have 16384
561 * allocated pids we need it to be larger!
562 */
563 if (pid_tbl_mask > PID_MAX) {
564 pid_max = pid_tbl_mask * 2 + 1;
565 pid_alloc_lim |= pid_alloc_lim << 1;
566 } else
567 pid_alloc_lim <<= 1; /* doubles number of free slots... */
568
569 rw_exit(&proclist_lock);
570 FREE(n_pt, M_PROC);
571 }
572
573 struct proc *
574 proc_alloc(void)
575 {
576 struct proc *p;
577 int nxt;
578 pid_t pid;
579 struct pid_table *pt;
580
581 p = pool_get(&proc_pool, PR_WAITOK);
582 p->p_stat = SIDL; /* protect against others */
583
584 /* allocate next free pid */
585
586 for (;;expand_pid_table()) {
587 if (__predict_false(pid_alloc_cnt >= pid_alloc_lim))
588 /* ensure pids cycle through 2000+ values */
589 continue;
590 rw_enter(&proclist_lock, RW_WRITER);
591 pt = &pid_table[next_free_pt];
592 #ifdef DIAGNOSTIC
593 if (__predict_false(P_VALID(pt->pt_proc) || pt->pt_pgrp))
594 panic("proc_alloc: slot busy");
595 #endif
596 nxt = P_NEXT(pt->pt_proc);
597 if (nxt & pid_tbl_mask)
598 break;
599 /* Table full - expand (NB last entry not used....) */
600 rw_exit(&proclist_lock);
601 }
602
603 /* pid is 'saved use count' + 'size' + entry */
604 pid = (nxt & ~pid_tbl_mask) + pid_tbl_mask + 1 + next_free_pt;
605 if ((uint)pid > (uint)pid_max)
606 pid &= pid_tbl_mask;
607 p->p_pid = pid;
608 next_free_pt = nxt & pid_tbl_mask;
609
610 /* Grab table slot */
611 mutex_enter(&proclist_mutex);
612 pt->pt_proc = p;
613 mutex_exit(&proclist_mutex);
614 pid_alloc_cnt++;
615
616 rw_exit(&proclist_lock);
617
618 return p;
619 }
620
621 /*
622 * Free last resources of a process - called from proc_free (in kern_exit.c)
623 *
624 * Called with the proclist_lock write held, and releases upon exit.
625 */
626 void
627 proc_free_mem(struct proc *p)
628 {
629 pid_t pid = p->p_pid;
630 struct pid_table *pt;
631
632 LOCK_ASSERT(rw_write_held(&proclist_lock));
633
634 pt = &pid_table[pid & pid_tbl_mask];
635 #ifdef DIAGNOSTIC
636 if (__predict_false(pt->pt_proc != p))
637 panic("proc_free: pid_table mismatch, pid %x, proc %p",
638 pid, p);
639 #endif
640 mutex_enter(&proclist_mutex);
641 /* save pid use count in slot */
642 pt->pt_proc = P_FREE(pid & ~pid_tbl_mask);
643
644 if (pt->pt_pgrp == NULL) {
645 /* link last freed entry onto ours */
646 pid &= pid_tbl_mask;
647 pt = &pid_table[last_free_pt];
648 pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pid);
649 last_free_pt = pid;
650 pid_alloc_cnt--;
651 }
652 mutex_exit(&proclist_mutex);
653
654 nprocs--;
655 rw_exit(&proclist_lock);
656
657 pool_put(&proc_pool, p);
658 }
659
660 /*
661 * Move p to a new or existing process group (and session)
662 *
663 * If we are creating a new pgrp, the pgid should equal
664 * the calling process' pid.
665 * If is only valid to enter a process group that is in the session
666 * of the process.
667 * Also mksess should only be set if we are creating a process group
668 *
669 * Only called from sys_setsid, sys_setpgid/sys_setpgrp and the
670 * SYSV setpgrp support for hpux.
671 */
672 int
673 enterpgrp(struct proc *curp, pid_t pid, pid_t pgid, int mksess)
674 {
675 struct pgrp *new_pgrp, *pgrp;
676 struct session *sess;
677 struct proc *p;
678 int rval;
679 pid_t pg_id = NO_PGID;
680
681 /* Allocate data areas we might need before doing any validity checks */
682 rw_enter(&proclist_lock, RW_READER); /* Because pid_table might change */
683 if (pid_table[pgid & pid_tbl_mask].pt_pgrp == 0) {
684 rw_exit(&proclist_lock);
685 new_pgrp = pool_get(&pgrp_pool, PR_WAITOK);
686 } else {
687 rw_exit(&proclist_lock);
688 new_pgrp = NULL;
689 }
690 if (mksess)
691 sess = pool_get(&session_pool, M_WAITOK);
692 else
693 sess = NULL;
694
695 rw_enter(&proclist_lock, RW_WRITER);
696 rval = EPERM; /* most common error (to save typing) */
697
698 /* Check pgrp exists or can be created */
699 pgrp = pid_table[pgid & pid_tbl_mask].pt_pgrp;
700 if (pgrp != NULL && pgrp->pg_id != pgid)
701 goto done;
702
703 /* Can only set another process under restricted circumstances. */
704 if (pid != curp->p_pid) {
705 /* must exist and be one of our children... */
706 if ((p = p_find(pid, PFIND_LOCKED)) == NULL ||
707 !inferior(p, curp)) {
708 rval = ESRCH;
709 goto done;
710 }
711 /* ... in the same session... */
712 if (sess != NULL || p->p_session != curp->p_session)
713 goto done;
714 /* ... existing pgid must be in same session ... */
715 if (pgrp != NULL && pgrp->pg_session != p->p_session)
716 goto done;
717 /* ... and not done an exec. */
718 if (p->p_flag & P_EXEC) {
719 rval = EACCES;
720 goto done;
721 }
722 } else {
723 /* ... setsid() cannot re-enter a pgrp */
724 if (mksess && (curp->p_pgid == curp->p_pid ||
725 pg_find(curp->p_pid, PFIND_LOCKED)))
726 goto done;
727 p = curp;
728 }
729
730 /* Changing the process group/session of a session
731 leader is definitely off limits. */
732 if (SESS_LEADER(p)) {
733 if (sess == NULL && p->p_pgrp == pgrp)
734 /* unless it's a definite noop */
735 rval = 0;
736 goto done;
737 }
738
739 /* Can only create a process group with id of process */
740 if (pgrp == NULL && pgid != pid)
741 goto done;
742
743 /* Can only create a session if creating pgrp */
744 if (sess != NULL && pgrp != NULL)
745 goto done;
746
747 /* Check we allocated memory for a pgrp... */
748 if (pgrp == NULL && new_pgrp == NULL)
749 goto done;
750
751 /* Don't attach to 'zombie' pgrp */
752 if (pgrp != NULL && LIST_EMPTY(&pgrp->pg_members))
753 goto done;
754
755 /* Expect to succeed now */
756 rval = 0;
757
758 if (pgrp == p->p_pgrp)
759 /* nothing to do */
760 goto done;
761
762 /* Ok all setup, link up required structures */
763
764 if (pgrp == NULL) {
765 pgrp = new_pgrp;
766 new_pgrp = 0;
767 if (sess != NULL) {
768 sess->s_sid = p->p_pid;
769 sess->s_leader = p;
770 sess->s_count = 1;
771 sess->s_ttyvp = NULL;
772 sess->s_ttyp = NULL;
773 sess->s_flags = p->p_session->s_flags & ~S_LOGIN_SET;
774 memcpy(sess->s_login, p->p_session->s_login,
775 sizeof(sess->s_login));
776 p->p_flag &= ~P_CONTROLT;
777 } else {
778 sess = p->p_pgrp->pg_session;
779 SESSHOLD(sess);
780 }
781 pgrp->pg_session = sess;
782 sess = 0;
783
784 pgrp->pg_id = pgid;
785 LIST_INIT(&pgrp->pg_members);
786 #ifdef DIAGNOSTIC
787 if (__predict_false(pid_table[pgid & pid_tbl_mask].pt_pgrp))
788 panic("enterpgrp: pgrp table slot in use");
789 if (__predict_false(mksess && p != curp))
790 panic("enterpgrp: mksession and p != curproc");
791 #endif
792 mutex_enter(&proclist_mutex);
793 pid_table[pgid & pid_tbl_mask].pt_pgrp = pgrp;
794 pgrp->pg_jobc = 0;
795 } else
796 mutex_enter(&proclist_mutex);
797
798 #ifdef notyet
799 /*
800 * If there's a controlling terminal for the current session, we
801 * have to interlock with it. See ttread().
802 */
803 if (p->p_session->s_ttyvp != NULL) {
804 tp = p->p_session->s_ttyp;
805 mutex_enter(&tp->t_mutex);
806 } else
807 tp = NULL;
808 #endif
809
810 /*
811 * Adjust eligibility of affected pgrps to participate in job control.
812 * Increment eligibility counts before decrementing, otherwise we
813 * could reach 0 spuriously during the first call.
814 */
815 fixjobc(p, pgrp, 1);
816 fixjobc(p, p->p_pgrp, 0);
817
818 /* Move process to requested group. */
819 LIST_REMOVE(p, p_pglist);
820 if (LIST_EMPTY(&p->p_pgrp->pg_members))
821 /* defer delete until we've dumped the lock */
822 pg_id = p->p_pgrp->pg_id;
823 p->p_pgrp = pgrp;
824 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
825 mutex_exit(&proclist_mutex);
826
827 #ifdef notyet
828 /* Done with the swap; we can release the tty mutex. */
829 if (tp != NULL)
830 mutex_exit(&tp->t_mutex);
831 #endif
832
833 done:
834 rw_exit(&proclist_lock);
835 if (sess != NULL)
836 pool_put(&session_pool, sess);
837 if (new_pgrp != NULL)
838 pool_put(&pgrp_pool, new_pgrp);
839 if (pg_id != NO_PGID)
840 pg_delete(pg_id);
841 #ifdef DEBUG_PGRP
842 if (__predict_false(rval))
843 printf("enterpgrp(%d,%d,%d), curproc %d, rval %d\n",
844 pid, pgid, mksess, curp->p_pid, rval);
845 #endif
846 return rval;
847 }
848
849 /*
850 * Remove a process from its process group.
851 */
852 void
853 leavepgrp(struct proc *p)
854 {
855 struct pgrp *pgrp;
856
857 /*
858 * If there's a controlling terminal for the session, we have to
859 * interlock with it. See ttread().
860 */
861 rw_enter(&proclist_lock, RW_WRITER);
862 mutex_enter(&proclist_mutex);
863
864 #ifdef notyet
865 if (p_>p_session->s_ttyvp != NULL) {
866 tp = p->p_session->s_ttyp;
867 mutex_enter(&tp->t_mutex);
868 } else
869 tp = NULL;
870 #endif
871
872 pgrp = p->p_pgrp;
873 LIST_REMOVE(p, p_pglist);
874 p->p_pgrp = NULL;
875
876 #ifdef notyet
877 if (tp != NULL)
878 mutex_exit(&tp->t_mutex);
879 #endif
880 mutex_exit(&proclist_mutex);
881 rw_exit(&proclist_lock);
882
883 if (LIST_EMPTY(&pgrp->pg_members))
884 pg_delete(pgrp->pg_id);
885 }
886
887 static void
888 pg_free(pid_t pg_id)
889 {
890 struct pgrp *pgrp;
891 struct pid_table *pt;
892
893 rw_enter(&proclist_lock, RW_WRITER);
894 pt = &pid_table[pg_id & pid_tbl_mask];
895 pgrp = pt->pt_pgrp;
896 #ifdef DIAGNOSTIC
897 if (__predict_false(!pgrp || pgrp->pg_id != pg_id
898 || !LIST_EMPTY(&pgrp->pg_members)))
899 panic("pg_free: process group absent or has members");
900 #endif
901 pt->pt_pgrp = 0;
902
903 if (!P_VALID(pt->pt_proc)) {
904 /* orphaned pgrp, put slot onto free list */
905 #ifdef DIAGNOSTIC
906 if (__predict_false(P_NEXT(pt->pt_proc) & pid_tbl_mask))
907 panic("pg_free: process slot on free list");
908 #endif
909 mutex_enter(&proclist_mutex);
910 pg_id &= pid_tbl_mask;
911 pt = &pid_table[last_free_pt];
912 pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pg_id);
913 mutex_exit(&proclist_mutex);
914 last_free_pt = pg_id;
915 pid_alloc_cnt--;
916 }
917 rw_exit(&proclist_lock);
918
919 pool_put(&pgrp_pool, pgrp);
920 }
921
922 /*
923 * delete a process group
924 */
925 static void
926 pg_delete(pid_t pg_id)
927 {
928 struct pgrp *pgrp;
929 struct tty *ttyp;
930 struct session *ss;
931 int is_pgrp_leader;
932
933 rw_enter(&proclist_lock, RW_WRITER);
934 pgrp = pid_table[pg_id & pid_tbl_mask].pt_pgrp;
935 if (pgrp == NULL || pgrp->pg_id != pg_id ||
936 !LIST_EMPTY(&pgrp->pg_members)) {
937 rw_exit(&proclist_lock);
938 return;
939 }
940
941 ss = pgrp->pg_session;
942
943 /* Remove reference (if any) from tty to this process group */
944 ttyp = ss->s_ttyp;
945 if (ttyp != NULL && ttyp->t_pgrp == pgrp) {
946 ttyp->t_pgrp = NULL;
947 #ifdef DIAGNOSTIC
948 if (ttyp->t_session != ss)
949 panic("pg_delete: wrong session on terminal");
950 #endif
951 }
952
953 /*
954 * The leading process group in a session is freed
955 * by sessdelete() if last reference.
956 */
957 is_pgrp_leader = (ss->s_sid == pgrp->pg_id);
958 rw_exit(&proclist_lock);
959 SESSRELE(ss);
960
961 if (is_pgrp_leader)
962 return;
963
964 pg_free(pg_id);
965 }
966
967 /*
968 * Delete session - called from SESSRELE when s_count becomes zero.
969 */
970 void
971 sessdelete(struct session *ss)
972 {
973 /*
974 * We keep the pgrp with the same id as the session in
975 * order to stop a process being given the same pid.
976 * Since the pgrp holds a reference to the session, it
977 * must be a 'zombie' pgrp by now.
978 */
979
980 pg_free(ss->s_sid);
981
982 pool_put(&session_pool, ss);
983 }
984
985 /*
986 * Adjust pgrp jobc counters when specified process changes process group.
987 * We count the number of processes in each process group that "qualify"
988 * the group for terminal job control (those with a parent in a different
989 * process group of the same session). If that count reaches zero, the
990 * process group becomes orphaned. Check both the specified process'
991 * process group and that of its children.
992 * entering == 0 => p is leaving specified group.
993 * entering == 1 => p is entering specified group.
994 *
995 * Call with proclist_lock held.
996 */
997 void
998 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
999 {
1000 struct pgrp *hispgrp;
1001 struct session *mysession = pgrp->pg_session;
1002 struct proc *child;
1003
1004 /*
1005 * Check p's parent to see whether p qualifies its own process
1006 * group; if so, adjust count for p's process group.
1007 */
1008 hispgrp = p->p_pptr->p_pgrp;
1009 if (hispgrp != pgrp && hispgrp->pg_session == mysession) {
1010 if (entering)
1011 pgrp->pg_jobc++;
1012 else if (--pgrp->pg_jobc == 0)
1013 orphanpg(pgrp);
1014 }
1015
1016 /*
1017 * Check this process' children to see whether they qualify
1018 * their process groups; if so, adjust counts for children's
1019 * process groups.
1020 */
1021 LIST_FOREACH(child, &p->p_children, p_sibling) {
1022 hispgrp = child->p_pgrp;
1023 if (hispgrp != pgrp && hispgrp->pg_session == mysession &&
1024 !P_ZOMBIE(child)) {
1025 if (entering)
1026 hispgrp->pg_jobc++;
1027 else if (--hispgrp->pg_jobc == 0)
1028 orphanpg(hispgrp);
1029 }
1030 }
1031 }
1032
1033 /*
1034 * A process group has become orphaned;
1035 * if there are any stopped processes in the group,
1036 * hang-up all process in that group.
1037 *
1038 * Call with proclist_lock held.
1039 */
1040 static void
1041 orphanpg(struct pgrp *pg)
1042 {
1043 struct proc *p;
1044
1045 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1046 if (p->p_stat == SSTOP) {
1047 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1048 psignal(p, SIGHUP);
1049 psignal(p, SIGCONT);
1050 }
1051 return;
1052 }
1053 }
1054 }
1055
1056 /* mark process as suid/sgid, reset some values to defaults */
1057 void
1058 p_sugid(struct proc *p)
1059 {
1060 struct plimit *lim;
1061 char *cn;
1062
1063 p->p_flag |= P_SUGID;
1064 /* reset what needs to be reset in plimit */
1065 lim = p->p_limit;
1066 if (lim->pl_corename != defcorename) {
1067 if (lim->p_refcnt > 1 &&
1068 (lim->p_lflags & PL_SHAREMOD) == 0) {
1069 p->p_limit = limcopy(lim);
1070 limfree(lim);
1071 lim = p->p_limit;
1072 }
1073 simple_lock(&lim->p_slock);
1074 cn = lim->pl_corename;
1075 lim->pl_corename = defcorename;
1076 simple_unlock(&lim->p_slock);
1077 if (cn != defcorename)
1078 free(cn, M_TEMP);
1079 }
1080 }
1081
1082 #ifdef DDB
1083 #include <ddb/db_output.h>
1084 void pidtbl_dump(void);
1085 void
1086 pidtbl_dump(void)
1087 {
1088 struct pid_table *pt;
1089 struct proc *p;
1090 struct pgrp *pgrp;
1091 int id;
1092
1093 db_printf("pid table %p size %x, next %x, last %x\n",
1094 pid_table, pid_tbl_mask+1,
1095 next_free_pt, last_free_pt);
1096 for (pt = pid_table, id = 0; id <= pid_tbl_mask; id++, pt++) {
1097 p = pt->pt_proc;
1098 if (!P_VALID(p) && !pt->pt_pgrp)
1099 continue;
1100 db_printf(" id %x: ", id);
1101 if (P_VALID(p))
1102 db_printf("proc %p id %d (0x%x) %s\n",
1103 p, p->p_pid, p->p_pid, p->p_comm);
1104 else
1105 db_printf("next %x use %x\n",
1106 P_NEXT(p) & pid_tbl_mask,
1107 P_NEXT(p) & ~pid_tbl_mask);
1108 if ((pgrp = pt->pt_pgrp)) {
1109 db_printf("\tsession %p, sid %d, count %d, login %s\n",
1110 pgrp->pg_session, pgrp->pg_session->s_sid,
1111 pgrp->pg_session->s_count,
1112 pgrp->pg_session->s_login);
1113 db_printf("\tpgrp %p, pg_id %d, pg_jobc %d, members %p\n",
1114 pgrp, pgrp->pg_id, pgrp->pg_jobc,
1115 pgrp->pg_members.lh_first);
1116 for (p = pgrp->pg_members.lh_first; p != 0;
1117 p = p->p_pglist.le_next) {
1118 db_printf("\t\tpid %d addr %p pgrp %p %s\n",
1119 p->p_pid, p, p->p_pgrp, p->p_comm);
1120 }
1121 }
1122 }
1123 }
1124 #endif /* DDB */
1125
1126 #ifdef KSTACK_CHECK_MAGIC
1127 #include <sys/user.h>
1128
1129 #define KSTACK_MAGIC 0xdeadbeaf
1130
1131 /* XXX should be per process basis? */
1132 int kstackleftmin = KSTACK_SIZE;
1133 int kstackleftthres = KSTACK_SIZE / 8; /* warn if remaining stack is
1134 less than this */
1135
1136 void
1137 kstack_setup_magic(const struct lwp *l)
1138 {
1139 uint32_t *ip;
1140 uint32_t const *end;
1141
1142 KASSERT(l != NULL);
1143 KASSERT(l != &lwp0);
1144
1145 /*
1146 * fill all the stack with magic number
1147 * so that later modification on it can be detected.
1148 */
1149 ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
1150 end = (uint32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1151 for (; ip < end; ip++) {
1152 *ip = KSTACK_MAGIC;
1153 }
1154 }
1155
1156 void
1157 kstack_check_magic(const struct lwp *l)
1158 {
1159 uint32_t const *ip, *end;
1160 int stackleft;
1161
1162 KASSERT(l != NULL);
1163
1164 /* don't check proc0 */ /*XXX*/
1165 if (l == &lwp0)
1166 return;
1167
1168 #ifdef __MACHINE_STACK_GROWS_UP
1169 /* stack grows upwards (eg. hppa) */
1170 ip = (uint32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1171 end = (uint32_t *)KSTACK_LOWEST_ADDR(l);
1172 for (ip--; ip >= end; ip--)
1173 if (*ip != KSTACK_MAGIC)
1174 break;
1175
1176 stackleft = (caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (caddr_t)ip;
1177 #else /* __MACHINE_STACK_GROWS_UP */
1178 /* stack grows downwards (eg. i386) */
1179 ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
1180 end = (uint32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1181 for (; ip < end; ip++)
1182 if (*ip != KSTACK_MAGIC)
1183 break;
1184
1185 stackleft = ((const char *)ip) - (const char *)KSTACK_LOWEST_ADDR(l);
1186 #endif /* __MACHINE_STACK_GROWS_UP */
1187
1188 if (kstackleftmin > stackleft) {
1189 kstackleftmin = stackleft;
1190 if (stackleft < kstackleftthres)
1191 printf("warning: kernel stack left %d bytes"
1192 "(pid %u:lid %u)\n", stackleft,
1193 (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
1194 }
1195
1196 if (stackleft <= 0) {
1197 panic("magic on the top of kernel stack changed for "
1198 "pid %u, lid %u: maybe kernel stack overflow",
1199 (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
1200 }
1201 }
1202 #endif /* KSTACK_CHECK_MAGIC */
1203
1204 /*
1205 * XXXSMP this is bust, it grabs a read lock and then messes about
1206 * with allproc.
1207 */
1208 int
1209 proclist_foreach_call(struct proclist *list,
1210 int (*callback)(struct proc *, void *arg), void *arg)
1211 {
1212 struct proc marker;
1213 struct proc *p;
1214 struct lwp * const l = curlwp;
1215 int ret = 0;
1216
1217 marker.p_flag = P_MARKER;
1218 PHOLD(l);
1219 rw_enter(&proclist_lock, RW_READER);
1220 for (p = LIST_FIRST(list); ret == 0 && p != NULL;) {
1221 if (p->p_flag & P_MARKER) {
1222 p = LIST_NEXT(p, p_list);
1223 continue;
1224 }
1225 LIST_INSERT_AFTER(p, &marker, p_list);
1226 ret = (*callback)(p, arg);
1227 KASSERT(rw_read_held(&proclist_lock));
1228 p = LIST_NEXT(&marker, p_list);
1229 LIST_REMOVE(&marker, p_list);
1230 }
1231 rw_exit(&proclist_lock);
1232 PRELE(l);
1233
1234 return ret;
1235 }
1236
1237 int
1238 proc_vmspace_getref(struct proc *p, struct vmspace **vm)
1239 {
1240
1241 /* XXXCDC: how should locking work here? */
1242
1243 /* curproc exception is for coredump. */
1244
1245 if ((p != curproc && (p->p_flag & P_WEXIT) != 0) ||
1246 (p->p_vmspace->vm_refcnt < 1)) { /* XXX */
1247 return EFAULT;
1248 }
1249
1250 uvmspace_addref(p->p_vmspace);
1251 *vm = p->p_vmspace;
1252
1253 return 0;
1254 }
1255
1256 /*
1257 * Acquire a write lock on the process credential.
1258 */
1259 void
1260 proc_crmod_enter(struct proc *p)
1261 {
1262
1263 mutex_enter(&p->p_crmutex);
1264 }
1265
1266 /*
1267 * Block out readers, set in a new process credential, and drop the write
1268 * lock. The credential must have a reference already. Optionally, free a
1269 * no-longer required credential.
1270 */
1271 void
1272 proc_crmod_leave(struct proc *p, kauth_cred_t scred, kauth_cred_t fcred)
1273 {
1274
1275 p->p_cred = scred;
1276 mutex_exit(&p->p_crmutex);
1277 if (fcred != NULL)
1278 kauth_cred_free(fcred);
1279 }
1280