Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.163.2.2
      1  1.163.2.2     rmind /*	$NetBSD: kern_proc.c,v 1.163.2.2 2011/03/05 20:55:15 rmind Exp $	*/
      2       1.33   thorpej 
      3       1.33   thorpej /*-
      4      1.131        ad  * Copyright (c) 1999, 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5       1.33   thorpej  * All rights reserved.
      6       1.33   thorpej  *
      7       1.33   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8       1.33   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9      1.100        ad  * NASA Ames Research Center, and by Andrew Doran.
     10       1.33   thorpej  *
     11       1.33   thorpej  * Redistribution and use in source and binary forms, with or without
     12       1.33   thorpej  * modification, are permitted provided that the following conditions
     13       1.33   thorpej  * are met:
     14       1.33   thorpej  * 1. Redistributions of source code must retain the above copyright
     15       1.33   thorpej  *    notice, this list of conditions and the following disclaimer.
     16       1.33   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.33   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18       1.33   thorpej  *    documentation and/or other materials provided with the distribution.
     19       1.33   thorpej  *
     20       1.33   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.33   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.33   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.33   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.33   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.33   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.33   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.33   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.33   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.33   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.33   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31       1.33   thorpej  */
     32        1.9       cgd 
     33        1.1       cgd /*
     34        1.7       cgd  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     35        1.7       cgd  *	The Regents of the University of California.  All rights reserved.
     36        1.1       cgd  *
     37        1.1       cgd  * Redistribution and use in source and binary forms, with or without
     38        1.1       cgd  * modification, are permitted provided that the following conditions
     39        1.1       cgd  * are met:
     40        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     41        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     42        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     43        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     44        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     45       1.65       agc  * 3. Neither the name of the University nor the names of its contributors
     46        1.1       cgd  *    may be used to endorse or promote products derived from this software
     47        1.1       cgd  *    without specific prior written permission.
     48        1.1       cgd  *
     49        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59        1.1       cgd  * SUCH DAMAGE.
     60        1.1       cgd  *
     61       1.23      fvdl  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
     62        1.1       cgd  */
     63       1.45     lukem 
     64       1.45     lukem #include <sys/cdefs.h>
     65  1.163.2.2     rmind __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.163.2.2 2011/03/05 20:55:15 rmind Exp $");
     66       1.48      yamt 
     67  1.163.2.1     rmind #ifdef _KERNEL_OPT
     68       1.48      yamt #include "opt_kstack.h"
     69       1.88      onoe #include "opt_maxuprc.h"
     70      1.161    darran #include "opt_dtrace.h"
     71  1.163.2.2     rmind #include "opt_compat_netbsd32.h"
     72  1.163.2.1     rmind #endif
     73        1.1       cgd 
     74        1.5   mycroft #include <sys/param.h>
     75        1.5   mycroft #include <sys/systm.h>
     76        1.5   mycroft #include <sys/kernel.h>
     77        1.5   mycroft #include <sys/proc.h>
     78       1.28   thorpej #include <sys/resourcevar.h>
     79        1.5   mycroft #include <sys/buf.h>
     80        1.5   mycroft #include <sys/acct.h>
     81        1.5   mycroft #include <sys/wait.h>
     82        1.5   mycroft #include <sys/file.h>
     83        1.8   mycroft #include <ufs/ufs/quota.h>
     84        1.5   mycroft #include <sys/uio.h>
     85       1.24   thorpej #include <sys/pool.h>
     86      1.147     rmind #include <sys/pset.h>
     87        1.5   mycroft #include <sys/mbuf.h>
     88        1.5   mycroft #include <sys/ioctl.h>
     89        1.5   mycroft #include <sys/tty.h>
     90       1.11       cgd #include <sys/signalvar.h>
     91       1.51  gmcgarry #include <sys/ras.h>
     92      1.144  wrstuden #include <sys/sa.h>
     93      1.144  wrstuden #include <sys/savar.h>
     94       1.81  junyoung #include <sys/filedesc.h>
     95      1.103       dsl #include "sys/syscall_stats.h"
     96       1.89      elad #include <sys/kauth.h>
     97      1.100        ad #include <sys/sleepq.h>
     98      1.126        ad #include <sys/atomic.h>
     99      1.131        ad #include <sys/kmem.h>
    100      1.160    darran #include <sys/dtrace_bsd.h>
    101  1.163.2.2     rmind #include <sys/sysctl.h>
    102  1.163.2.2     rmind #include <sys/exec.h>
    103  1.163.2.2     rmind #include <sys/cpu.h>
    104      1.160    darran 
    105       1.79      yamt #include <uvm/uvm_extern.h>
    106  1.163.2.2     rmind #include <uvm/uvm_extern.h>
    107  1.163.2.2     rmind 
    108  1.163.2.2     rmind #ifdef COMPAT_NETBSD32
    109  1.163.2.2     rmind #include <compat/netbsd32/netbsd32.h>
    110  1.163.2.2     rmind #endif
    111        1.5   mycroft 
    112        1.7       cgd /*
    113       1.10   mycroft  * Other process lists
    114        1.7       cgd  */
    115       1.31   thorpej 
    116       1.10   mycroft struct proclist allproc;
    117       1.32   thorpej struct proclist zombproc;	/* resources have been freed */
    118       1.32   thorpej 
    119      1.136        ad kmutex_t	*proc_lock;
    120       1.33   thorpej 
    121       1.33   thorpej /*
    122       1.72  junyoung  * pid to proc lookup is done by indexing the pid_table array.
    123       1.61       dsl  * Since pid numbers are only allocated when an empty slot
    124       1.61       dsl  * has been found, there is no need to search any lists ever.
    125       1.61       dsl  * (an orphaned pgrp will lock the slot, a session will lock
    126       1.61       dsl  * the pgrp with the same number.)
    127       1.61       dsl  * If the table is too small it is reallocated with twice the
    128       1.61       dsl  * previous size and the entries 'unzipped' into the two halves.
    129       1.61       dsl  * A linked list of free entries is passed through the pt_proc
    130       1.61       dsl  * field of 'free' items - set odd to be an invalid ptr.
    131       1.61       dsl  */
    132       1.61       dsl 
    133       1.61       dsl struct pid_table {
    134       1.61       dsl 	struct proc	*pt_proc;
    135       1.61       dsl 	struct pgrp	*pt_pgrp;
    136  1.163.2.2     rmind 	pid_t		pt_pid;
    137       1.72  junyoung };
    138       1.61       dsl #if 1	/* strongly typed cast - should be a noop */
    139       1.84     perry static inline uint p2u(struct proc *p) { return (uint)(uintptr_t)p; }
    140       1.61       dsl #else
    141       1.61       dsl #define p2u(p) ((uint)p)
    142       1.72  junyoung #endif
    143       1.61       dsl #define P_VALID(p) (!(p2u(p) & 1))
    144       1.61       dsl #define P_NEXT(p) (p2u(p) >> 1)
    145       1.61       dsl #define P_FREE(pid) ((struct proc *)(uintptr_t)((pid) << 1 | 1))
    146       1.61       dsl 
    147       1.61       dsl #define INITIAL_PID_TABLE_SIZE	(1 << 5)
    148       1.61       dsl static struct pid_table *pid_table;
    149       1.61       dsl static uint pid_tbl_mask = INITIAL_PID_TABLE_SIZE - 1;
    150       1.61       dsl static uint pid_alloc_lim;	/* max we allocate before growing table */
    151       1.61       dsl static uint pid_alloc_cnt;	/* number of allocated pids */
    152       1.61       dsl 
    153       1.61       dsl /* links through free slots - never empty! */
    154       1.61       dsl static uint next_free_pt, last_free_pt;
    155       1.61       dsl static pid_t pid_max = PID_MAX;		/* largest value we allocate */
    156       1.31   thorpej 
    157       1.81  junyoung /* Components of the first process -- never freed. */
    158      1.123      matt 
    159      1.145        ad extern struct emul emul_netbsd;	/* defined in kern_exec.c */
    160      1.123      matt 
    161      1.123      matt struct session session0 = {
    162      1.123      matt 	.s_count = 1,
    163      1.123      matt 	.s_sid = 0,
    164      1.123      matt };
    165      1.123      matt struct pgrp pgrp0 = {
    166      1.123      matt 	.pg_members = LIST_HEAD_INITIALIZER(&pgrp0.pg_members),
    167      1.123      matt 	.pg_session = &session0,
    168      1.123      matt };
    169      1.132        ad filedesc_t filedesc0;
    170      1.123      matt struct cwdinfo cwdi0 = {
    171      1.123      matt 	.cwdi_cmask = CMASK,		/* see cmask below */
    172      1.123      matt 	.cwdi_refcnt = 1,
    173      1.123      matt };
    174      1.143  gmcgarry struct plimit limit0;
    175       1.81  junyoung struct pstats pstat0;
    176       1.81  junyoung struct vmspace vmspace0;
    177       1.81  junyoung struct sigacts sigacts0;
    178      1.123      matt struct proc proc0 = {
    179      1.123      matt 	.p_lwps = LIST_HEAD_INITIALIZER(&proc0.p_lwps),
    180      1.123      matt 	.p_sigwaiters = LIST_HEAD_INITIALIZER(&proc0.p_sigwaiters),
    181      1.123      matt 	.p_nlwps = 1,
    182      1.123      matt 	.p_nrlwps = 1,
    183      1.123      matt 	.p_nlwpid = 1,		/* must match lwp0.l_lid */
    184      1.123      matt 	.p_pgrp = &pgrp0,
    185      1.123      matt 	.p_comm = "system",
    186      1.123      matt 	/*
    187      1.123      matt 	 * Set P_NOCLDWAIT so that kernel threads are reparented to init(8)
    188      1.123      matt 	 * when they exit.  init(8) can easily wait them out for us.
    189      1.123      matt 	 */
    190      1.123      matt 	.p_flag = PK_SYSTEM | PK_NOCLDWAIT,
    191      1.123      matt 	.p_stat = SACTIVE,
    192      1.123      matt 	.p_nice = NZERO,
    193      1.123      matt 	.p_emul = &emul_netbsd,
    194      1.123      matt 	.p_cwdi = &cwdi0,
    195      1.123      matt 	.p_limit = &limit0,
    196      1.132        ad 	.p_fd = &filedesc0,
    197      1.123      matt 	.p_vmspace = &vmspace0,
    198      1.123      matt 	.p_stats = &pstat0,
    199      1.123      matt 	.p_sigacts = &sigacts0,
    200      1.123      matt };
    201      1.123      matt kauth_cred_t cred0;
    202       1.81  junyoung 
    203       1.81  junyoung int nofile = NOFILE;
    204       1.81  junyoung int maxuprc = MAXUPRC;
    205       1.81  junyoung int cmask = CMASK;
    206       1.81  junyoung 
    207       1.57   thorpej MALLOC_DEFINE(M_EMULDATA, "emuldata", "Per-process emulation data");
    208       1.57   thorpej MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
    209       1.10   mycroft 
    210  1.163.2.2     rmind static int sysctl_doeproc(SYSCTLFN_PROTO);
    211  1.163.2.2     rmind static int sysctl_kern_proc_args(SYSCTLFN_PROTO);
    212  1.163.2.2     rmind static void fill_kproc2(struct proc *, struct kinfo_proc2 *, bool);
    213  1.163.2.2     rmind 
    214       1.31   thorpej /*
    215       1.31   thorpej  * The process list descriptors, used during pid allocation and
    216       1.31   thorpej  * by sysctl.  No locking on this data structure is needed since
    217       1.31   thorpej  * it is completely static.
    218       1.31   thorpej  */
    219       1.31   thorpej const struct proclist_desc proclists[] = {
    220       1.31   thorpej 	{ &allproc	},
    221       1.31   thorpej 	{ &zombproc	},
    222       1.31   thorpej 	{ NULL		},
    223       1.31   thorpej };
    224       1.31   thorpej 
    225      1.151     rmind static struct pgrp *	pg_remove(pid_t);
    226      1.151     rmind static void		pg_delete(pid_t);
    227      1.151     rmind static void		orphanpg(struct pgrp *);
    228       1.13  christos 
    229       1.95   thorpej static specificdata_domain_t proc_specificdata_domain;
    230       1.95   thorpej 
    231      1.128        ad static pool_cache_t proc_cache;
    232      1.128        ad 
    233      1.153      elad static kauth_listener_t proc_listener;
    234      1.153      elad 
    235      1.153      elad static int
    236      1.153      elad proc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    237      1.153      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    238      1.153      elad {
    239      1.153      elad 	struct proc *p;
    240      1.153      elad 	int result;
    241      1.153      elad 
    242      1.153      elad 	result = KAUTH_RESULT_DEFER;
    243      1.153      elad 	p = arg0;
    244      1.153      elad 
    245      1.153      elad 	switch (action) {
    246      1.153      elad 	case KAUTH_PROCESS_CANSEE: {
    247      1.153      elad 		enum kauth_process_req req;
    248      1.153      elad 
    249      1.153      elad 		req = (enum kauth_process_req)arg1;
    250      1.153      elad 
    251      1.153      elad 		switch (req) {
    252      1.153      elad 		case KAUTH_REQ_PROCESS_CANSEE_ARGS:
    253      1.153      elad 		case KAUTH_REQ_PROCESS_CANSEE_ENTRY:
    254      1.153      elad 		case KAUTH_REQ_PROCESS_CANSEE_OPENFILES:
    255      1.153      elad 			result = KAUTH_RESULT_ALLOW;
    256      1.153      elad 
    257      1.153      elad 			break;
    258      1.153      elad 
    259      1.153      elad 		case KAUTH_REQ_PROCESS_CANSEE_ENV:
    260      1.153      elad 			if (kauth_cred_getuid(cred) !=
    261      1.153      elad 			    kauth_cred_getuid(p->p_cred) ||
    262      1.153      elad 			    kauth_cred_getuid(cred) !=
    263      1.153      elad 			    kauth_cred_getsvuid(p->p_cred))
    264      1.153      elad 				break;
    265      1.153      elad 
    266      1.153      elad 			result = KAUTH_RESULT_ALLOW;
    267      1.153      elad 
    268      1.153      elad 			break;
    269      1.153      elad 
    270      1.153      elad 		default:
    271      1.153      elad 			break;
    272      1.153      elad 		}
    273      1.153      elad 
    274      1.153      elad 		break;
    275      1.153      elad 		}
    276      1.153      elad 
    277      1.153      elad 	case KAUTH_PROCESS_FORK: {
    278      1.153      elad 		int lnprocs = (int)(unsigned long)arg2;
    279      1.153      elad 
    280      1.153      elad 		/*
    281      1.153      elad 		 * Don't allow a nonprivileged user to use the last few
    282      1.153      elad 		 * processes. The variable lnprocs is the current number of
    283      1.153      elad 		 * processes, maxproc is the limit.
    284      1.153      elad 		 */
    285      1.153      elad 		if (__predict_false((lnprocs >= maxproc - 5)))
    286      1.153      elad 			break;
    287      1.153      elad 
    288      1.153      elad 		result = KAUTH_RESULT_ALLOW;
    289      1.153      elad 
    290      1.153      elad 		break;
    291      1.153      elad 		}
    292      1.153      elad 
    293      1.153      elad 	case KAUTH_PROCESS_CORENAME:
    294      1.153      elad 	case KAUTH_PROCESS_STOPFLAG:
    295      1.153      elad 		if (proc_uidmatch(cred, p->p_cred) == 0)
    296      1.153      elad 			result = KAUTH_RESULT_ALLOW;
    297      1.153      elad 
    298      1.153      elad 		break;
    299      1.153      elad 
    300      1.153      elad 	default:
    301      1.153      elad 		break;
    302      1.153      elad 	}
    303      1.153      elad 
    304      1.153      elad 	return result;
    305      1.153      elad }
    306      1.153      elad 
    307       1.10   mycroft /*
    308       1.10   mycroft  * Initialize global process hashing structures.
    309       1.10   mycroft  */
    310       1.11       cgd void
    311       1.59       dsl procinit(void)
    312        1.7       cgd {
    313       1.31   thorpej 	const struct proclist_desc *pd;
    314      1.150     rmind 	u_int i;
    315       1.61       dsl #define	LINK_EMPTY ((PID_MAX + INITIAL_PID_TABLE_SIZE) & ~(INITIAL_PID_TABLE_SIZE - 1))
    316       1.31   thorpej 
    317       1.31   thorpej 	for (pd = proclists; pd->pd_list != NULL; pd++)
    318       1.31   thorpej 		LIST_INIT(pd->pd_list);
    319        1.7       cgd 
    320      1.136        ad 	proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    321      1.150     rmind 	pid_table = kmem_alloc(INITIAL_PID_TABLE_SIZE
    322      1.150     rmind 	    * sizeof(struct pid_table), KM_SLEEP);
    323       1.33   thorpej 
    324       1.61       dsl 	/* Set free list running through table...
    325       1.61       dsl 	   Preset 'use count' above PID_MAX so we allocate pid 1 next. */
    326       1.61       dsl 	for (i = 0; i <= pid_tbl_mask; i++) {
    327       1.61       dsl 		pid_table[i].pt_proc = P_FREE(LINK_EMPTY + i + 1);
    328       1.61       dsl 		pid_table[i].pt_pgrp = 0;
    329  1.163.2.2     rmind 		pid_table[i].pt_pid = 0;
    330       1.61       dsl 	}
    331       1.61       dsl 	/* slot 0 is just grabbed */
    332       1.61       dsl 	next_free_pt = 1;
    333       1.61       dsl 	/* Need to fix last entry. */
    334       1.61       dsl 	last_free_pt = pid_tbl_mask;
    335       1.61       dsl 	pid_table[last_free_pt].pt_proc = P_FREE(LINK_EMPTY);
    336       1.61       dsl 	/* point at which we grow table - to avoid reusing pids too often */
    337       1.61       dsl 	pid_alloc_lim = pid_tbl_mask - 1;
    338       1.61       dsl #undef LINK_EMPTY
    339       1.61       dsl 
    340       1.95   thorpej 	proc_specificdata_domain = specificdata_domain_create();
    341       1.95   thorpej 	KASSERT(proc_specificdata_domain != NULL);
    342      1.128        ad 
    343      1.128        ad 	proc_cache = pool_cache_init(sizeof(struct proc), 0, 0, 0,
    344      1.128        ad 	    "procpl", NULL, IPL_NONE, NULL, NULL, NULL);
    345      1.153      elad 
    346      1.153      elad 	proc_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    347      1.153      elad 	    proc_listener_cb, NULL);
    348        1.7       cgd }
    349        1.1       cgd 
    350  1.163.2.2     rmind void
    351  1.163.2.2     rmind procinit_sysctl(void)
    352  1.163.2.2     rmind {
    353  1.163.2.2     rmind 	static struct sysctllog *clog;
    354  1.163.2.2     rmind 
    355  1.163.2.2     rmind 	sysctl_createv(&clog, 0, NULL, NULL,
    356  1.163.2.2     rmind 		       CTLFLAG_PERMANENT,
    357  1.163.2.2     rmind 		       CTLTYPE_NODE, "kern", NULL,
    358  1.163.2.2     rmind 		       NULL, 0, NULL, 0,
    359  1.163.2.2     rmind 		       CTL_KERN, CTL_EOL);
    360  1.163.2.2     rmind 
    361  1.163.2.2     rmind 	sysctl_createv(&clog, 0, NULL, NULL,
    362  1.163.2.2     rmind 		       CTLFLAG_PERMANENT,
    363  1.163.2.2     rmind 		       CTLTYPE_NODE, "proc",
    364  1.163.2.2     rmind 		       SYSCTL_DESCR("System-wide process information"),
    365  1.163.2.2     rmind 		       sysctl_doeproc, 0, NULL, 0,
    366  1.163.2.2     rmind 		       CTL_KERN, KERN_PROC, CTL_EOL);
    367  1.163.2.2     rmind 	sysctl_createv(&clog, 0, NULL, NULL,
    368  1.163.2.2     rmind 		       CTLFLAG_PERMANENT,
    369  1.163.2.2     rmind 		       CTLTYPE_NODE, "proc2",
    370  1.163.2.2     rmind 		       SYSCTL_DESCR("Machine-independent process information"),
    371  1.163.2.2     rmind 		       sysctl_doeproc, 0, NULL, 0,
    372  1.163.2.2     rmind 		       CTL_KERN, KERN_PROC2, CTL_EOL);
    373  1.163.2.2     rmind 	sysctl_createv(&clog, 0, NULL, NULL,
    374  1.163.2.2     rmind 		       CTLFLAG_PERMANENT,
    375  1.163.2.2     rmind 		       CTLTYPE_NODE, "proc_args",
    376  1.163.2.2     rmind 		       SYSCTL_DESCR("Process argument information"),
    377  1.163.2.2     rmind 		       sysctl_kern_proc_args, 0, NULL, 0,
    378  1.163.2.2     rmind 		       CTL_KERN, KERN_PROC_ARGS, CTL_EOL);
    379  1.163.2.2     rmind 
    380  1.163.2.2     rmind 	/*
    381  1.163.2.2     rmind 	  "nodes" under these:
    382  1.163.2.2     rmind 
    383  1.163.2.2     rmind 	  KERN_PROC_ALL
    384  1.163.2.2     rmind 	  KERN_PROC_PID pid
    385  1.163.2.2     rmind 	  KERN_PROC_PGRP pgrp
    386  1.163.2.2     rmind 	  KERN_PROC_SESSION sess
    387  1.163.2.2     rmind 	  KERN_PROC_TTY tty
    388  1.163.2.2     rmind 	  KERN_PROC_UID uid
    389  1.163.2.2     rmind 	  KERN_PROC_RUID uid
    390  1.163.2.2     rmind 	  KERN_PROC_GID gid
    391  1.163.2.2     rmind 	  KERN_PROC_RGID gid
    392  1.163.2.2     rmind 
    393  1.163.2.2     rmind 	  all in all, probably not worth the effort...
    394  1.163.2.2     rmind 	*/
    395  1.163.2.2     rmind }
    396  1.163.2.2     rmind 
    397        1.7       cgd /*
    398       1.81  junyoung  * Initialize process 0.
    399       1.81  junyoung  */
    400       1.81  junyoung void
    401       1.81  junyoung proc0_init(void)
    402       1.81  junyoung {
    403       1.81  junyoung 	struct proc *p;
    404       1.81  junyoung 	struct pgrp *pg;
    405       1.81  junyoung 	rlim_t lim;
    406      1.143  gmcgarry 	int i;
    407       1.81  junyoung 
    408       1.81  junyoung 	p = &proc0;
    409       1.81  junyoung 	pg = &pgrp0;
    410      1.123      matt 
    411      1.127        ad 	mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
    412      1.129        ad 	mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
    413      1.137        ad 	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    414      1.107        ad 
    415      1.122        ad 	rw_init(&p->p_reflock);
    416      1.100        ad 	cv_init(&p->p_waitcv, "wait");
    417      1.100        ad 	cv_init(&p->p_lwpcv, "lwpwait");
    418      1.100        ad 
    419  1.163.2.1     rmind 	LIST_INSERT_HEAD(&p->p_lwps, &lwp0, l_sibling);
    420      1.100        ad 
    421       1.81  junyoung 	pid_table[0].pt_proc = p;
    422       1.81  junyoung 	LIST_INSERT_HEAD(&allproc, p, p_list);
    423       1.81  junyoung 
    424       1.81  junyoung 	pid_table[0].pt_pgrp = pg;
    425       1.81  junyoung 	LIST_INSERT_HEAD(&pg->pg_members, p, p_pglist);
    426       1.81  junyoung 
    427       1.81  junyoung #ifdef __HAVE_SYSCALL_INTERN
    428       1.81  junyoung 	(*p->p_emul->e_syscall_intern)(p);
    429       1.81  junyoung #endif
    430       1.81  junyoung 
    431       1.81  junyoung 	/* Create credentials. */
    432       1.89      elad 	cred0 = kauth_cred_alloc();
    433       1.89      elad 	p->p_cred = cred0;
    434       1.81  junyoung 
    435       1.81  junyoung 	/* Create the CWD info. */
    436      1.113        ad 	rw_init(&cwdi0.cwdi_lock);
    437       1.81  junyoung 
    438       1.81  junyoung 	/* Create the limits structures. */
    439      1.116       dsl 	mutex_init(&limit0.pl_lock, MUTEX_DEFAULT, IPL_NONE);
    440      1.143  gmcgarry 	for (i = 0; i < __arraycount(limit0.pl_rlimit); i++)
    441      1.143  gmcgarry 		limit0.pl_rlimit[i].rlim_cur =
    442      1.143  gmcgarry 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
    443       1.81  junyoung 
    444       1.81  junyoung 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
    445       1.81  junyoung 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
    446       1.81  junyoung 	    maxfiles < nofile ? maxfiles : nofile;
    447       1.81  junyoung 
    448       1.81  junyoung 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
    449       1.81  junyoung 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
    450       1.81  junyoung 	    maxproc < maxuprc ? maxproc : maxuprc;
    451       1.81  junyoung 
    452      1.163       jym 	lim = MIN(VM_MAXUSER_ADDRESS, ctob((rlim_t)uvmexp.free));
    453       1.81  junyoung 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
    454       1.81  junyoung 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
    455       1.81  junyoung 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
    456      1.143  gmcgarry 	limit0.pl_corename = defcorename;
    457      1.143  gmcgarry 	limit0.pl_refcnt = 1;
    458      1.143  gmcgarry 	limit0.pl_sv_limit = NULL;
    459       1.81  junyoung 
    460       1.81  junyoung 	/* Configure virtual memory system, set vm rlimits. */
    461       1.81  junyoung 	uvm_init_limits(p);
    462       1.81  junyoung 
    463       1.81  junyoung 	/* Initialize file descriptor table for proc0. */
    464      1.132        ad 	fd_init(&filedesc0);
    465       1.81  junyoung 
    466       1.81  junyoung 	/*
    467       1.81  junyoung 	 * Initialize proc0's vmspace, which uses the kernel pmap.
    468       1.81  junyoung 	 * All kernel processes (which never have user space mappings)
    469       1.81  junyoung 	 * share proc0's vmspace, and thus, the kernel pmap.
    470       1.81  junyoung 	 */
    471       1.81  junyoung 	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
    472       1.81  junyoung 	    trunc_page(VM_MAX_ADDRESS));
    473       1.81  junyoung 
    474      1.127        ad 	/* Initialize signal state for proc0. XXX IPL_SCHED */
    475      1.127        ad 	mutex_init(&p->p_sigacts->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
    476       1.81  junyoung 	siginit(p);
    477       1.96  christos 
    478       1.96  christos 	proc_initspecific(p);
    479  1.163.2.1     rmind 	kdtrace_proc_ctor(NULL, p);
    480       1.81  junyoung }
    481       1.81  junyoung 
    482       1.81  junyoung /*
    483      1.151     rmind  * Session reference counting.
    484      1.151     rmind  */
    485      1.151     rmind 
    486      1.151     rmind void
    487      1.151     rmind proc_sesshold(struct session *ss)
    488      1.151     rmind {
    489      1.151     rmind 
    490      1.151     rmind 	KASSERT(mutex_owned(proc_lock));
    491      1.151     rmind 	ss->s_count++;
    492      1.151     rmind }
    493      1.151     rmind 
    494      1.151     rmind void
    495      1.151     rmind proc_sessrele(struct session *ss)
    496      1.151     rmind {
    497      1.151     rmind 
    498      1.151     rmind 	KASSERT(mutex_owned(proc_lock));
    499      1.151     rmind 	/*
    500      1.151     rmind 	 * We keep the pgrp with the same id as the session in order to
    501      1.151     rmind 	 * stop a process being given the same pid.  Since the pgrp holds
    502      1.151     rmind 	 * a reference to the session, it must be a 'zombie' pgrp by now.
    503      1.151     rmind 	 */
    504      1.151     rmind 	if (--ss->s_count == 0) {
    505      1.151     rmind 		struct pgrp *pg;
    506      1.151     rmind 
    507      1.151     rmind 		pg = pg_remove(ss->s_sid);
    508      1.151     rmind 		mutex_exit(proc_lock);
    509      1.151     rmind 
    510      1.151     rmind 		kmem_free(pg, sizeof(struct pgrp));
    511      1.151     rmind 		kmem_free(ss, sizeof(struct session));
    512      1.151     rmind 	} else {
    513      1.151     rmind 		mutex_exit(proc_lock);
    514      1.151     rmind 	}
    515      1.151     rmind }
    516      1.151     rmind 
    517      1.151     rmind /*
    518       1.74  junyoung  * Check that the specified process group is in the session of the
    519       1.60       dsl  * specified process.
    520       1.60       dsl  * Treats -ve ids as process ids.
    521       1.60       dsl  * Used to validate TIOCSPGRP requests.
    522       1.60       dsl  */
    523       1.60       dsl int
    524       1.60       dsl pgid_in_session(struct proc *p, pid_t pg_id)
    525       1.60       dsl {
    526       1.60       dsl 	struct pgrp *pgrp;
    527      1.101       dsl 	struct session *session;
    528      1.107        ad 	int error;
    529      1.101       dsl 
    530      1.136        ad 	mutex_enter(proc_lock);
    531       1.60       dsl 	if (pg_id < 0) {
    532  1.163.2.1     rmind 		struct proc *p1 = proc_find(-pg_id);
    533  1.163.2.1     rmind 		if (p1 == NULL) {
    534  1.163.2.1     rmind 			error = EINVAL;
    535  1.163.2.1     rmind 			goto fail;
    536  1.163.2.1     rmind 		}
    537       1.60       dsl 		pgrp = p1->p_pgrp;
    538       1.60       dsl 	} else {
    539  1.163.2.1     rmind 		pgrp = pgrp_find(pg_id);
    540  1.163.2.1     rmind 		if (pgrp == NULL) {
    541  1.163.2.1     rmind 			error = EINVAL;
    542  1.163.2.1     rmind 			goto fail;
    543  1.163.2.1     rmind 		}
    544       1.60       dsl 	}
    545      1.101       dsl 	session = pgrp->pg_session;
    546  1.163.2.1     rmind 	error = (session != p->p_pgrp->pg_session) ? EPERM : 0;
    547  1.163.2.1     rmind fail:
    548      1.136        ad 	mutex_exit(proc_lock);
    549      1.107        ad 	return error;
    550        1.7       cgd }
    551        1.4    andrew 
    552        1.1       cgd /*
    553      1.148     rmind  * p_inferior: is p an inferior of q?
    554        1.1       cgd  */
    555      1.148     rmind static inline bool
    556      1.148     rmind p_inferior(struct proc *p, struct proc *q)
    557        1.1       cgd {
    558        1.1       cgd 
    559      1.148     rmind 	KASSERT(mutex_owned(proc_lock));
    560      1.148     rmind 
    561       1.41  sommerfe 	for (; p != q; p = p->p_pptr)
    562        1.1       cgd 		if (p->p_pid == 0)
    563      1.148     rmind 			return false;
    564      1.148     rmind 	return true;
    565        1.1       cgd }
    566        1.1       cgd 
    567        1.1       cgd /*
    568  1.163.2.1     rmind  * proc_find: locate a process by the ID.
    569  1.163.2.1     rmind  *
    570  1.163.2.1     rmind  * => Must be called with proc_lock held.
    571        1.1       cgd  */
    572  1.163.2.1     rmind proc_t *
    573  1.163.2.1     rmind proc_find_raw(pid_t pid)
    574        1.1       cgd {
    575  1.163.2.2     rmind 	struct pid_table *pt;
    576  1.163.2.2     rmind 	proc_t *p;
    577        1.1       cgd 
    578  1.163.2.2     rmind 	KASSERT(mutex_owned(proc_lock));
    579  1.163.2.2     rmind 	pt = &pid_table[pid & pid_tbl_mask];
    580  1.163.2.2     rmind 	p = pt->pt_proc;
    581  1.163.2.2     rmind 	if (__predict_false(!P_VALID(p) || pt->pt_pid != pid)) {
    582  1.163.2.1     rmind 		return NULL;
    583  1.163.2.1     rmind 	}
    584  1.163.2.1     rmind 	return p;
    585  1.163.2.1     rmind }
    586      1.100        ad 
    587  1.163.2.1     rmind proc_t *
    588  1.163.2.1     rmind proc_find(pid_t pid)
    589  1.163.2.1     rmind {
    590  1.163.2.1     rmind 	proc_t *p;
    591  1.163.2.1     rmind 
    592  1.163.2.1     rmind 	p = proc_find_raw(pid);
    593  1.163.2.1     rmind 	if (__predict_false(p == NULL)) {
    594  1.163.2.1     rmind 		return NULL;
    595  1.163.2.1     rmind 	}
    596  1.163.2.2     rmind 
    597  1.163.2.1     rmind 	/*
    598  1.163.2.1     rmind 	 * Only allow live processes to be found by PID.
    599  1.163.2.1     rmind 	 * XXX: p_stat might change, since unlocked.
    600  1.163.2.1     rmind 	 */
    601  1.163.2.1     rmind 	if (__predict_true(p->p_stat == SACTIVE || p->p_stat == SSTOP)) {
    602       1.68       dsl 		return p;
    603       1.68       dsl 	}
    604       1.68       dsl 	return NULL;
    605        1.1       cgd }
    606        1.1       cgd 
    607        1.1       cgd /*
    608  1.163.2.1     rmind  * pgrp_find: locate a process group by the ID.
    609  1.163.2.1     rmind  *
    610  1.163.2.1     rmind  * => Must be called with proc_lock held.
    611        1.1       cgd  */
    612        1.1       cgd struct pgrp *
    613  1.163.2.1     rmind pgrp_find(pid_t pgid)
    614        1.1       cgd {
    615       1.68       dsl 	struct pgrp *pg;
    616        1.1       cgd 
    617  1.163.2.1     rmind 	KASSERT(mutex_owned(proc_lock));
    618  1.163.2.1     rmind 
    619       1.68       dsl 	pg = pid_table[pgid & pid_tbl_mask].pt_pgrp;
    620  1.163.2.2     rmind 
    621       1.61       dsl 	/*
    622  1.163.2.1     rmind 	 * Cannot look up a process group that only exists because the
    623  1.163.2.1     rmind 	 * session has not died yet (traditional).
    624       1.61       dsl 	 */
    625       1.68       dsl 	if (pg == NULL || pg->pg_id != pgid || LIST_EMPTY(&pg->pg_members)) {
    626       1.68       dsl 		return NULL;
    627       1.68       dsl 	}
    628       1.68       dsl 	return pg;
    629        1.1       cgd }
    630        1.1       cgd 
    631       1.61       dsl static void
    632       1.61       dsl expand_pid_table(void)
    633        1.1       cgd {
    634      1.150     rmind 	size_t pt_size, tsz;
    635       1.61       dsl 	struct pid_table *n_pt, *new_pt;
    636       1.61       dsl 	struct proc *proc;
    637       1.61       dsl 	struct pgrp *pgrp;
    638  1.163.2.2     rmind 	pid_t pid, rpid;
    639      1.150     rmind 	u_int i;
    640  1.163.2.2     rmind 	uint new_pt_mask;
    641        1.1       cgd 
    642      1.150     rmind 	pt_size = pid_tbl_mask + 1;
    643      1.150     rmind 	tsz = pt_size * 2 * sizeof(struct pid_table);
    644      1.150     rmind 	new_pt = kmem_alloc(tsz, KM_SLEEP);
    645  1.163.2.2     rmind 	new_pt_mask = pt_size * 2 - 1;
    646       1.61       dsl 
    647      1.136        ad 	mutex_enter(proc_lock);
    648       1.61       dsl 	if (pt_size != pid_tbl_mask + 1) {
    649       1.61       dsl 		/* Another process beat us to it... */
    650      1.136        ad 		mutex_exit(proc_lock);
    651      1.150     rmind 		kmem_free(new_pt, tsz);
    652       1.61       dsl 		return;
    653       1.61       dsl 	}
    654       1.72  junyoung 
    655       1.61       dsl 	/*
    656       1.61       dsl 	 * Copy entries from old table into new one.
    657       1.61       dsl 	 * If 'pid' is 'odd' we need to place in the upper half,
    658       1.61       dsl 	 * even pid's to the lower half.
    659       1.61       dsl 	 * Free items stay in the low half so we don't have to
    660       1.61       dsl 	 * fixup the reference to them.
    661       1.61       dsl 	 * We stuff free items on the front of the freelist
    662       1.61       dsl 	 * because we can't write to unmodified entries.
    663       1.74  junyoung 	 * Processing the table backwards maintains a semblance
    664  1.163.2.2     rmind 	 * of issuing pid numbers that increase with time.
    665       1.61       dsl 	 */
    666       1.61       dsl 	i = pt_size - 1;
    667       1.61       dsl 	n_pt = new_pt + i;
    668       1.61       dsl 	for (; ; i--, n_pt--) {
    669       1.61       dsl 		proc = pid_table[i].pt_proc;
    670       1.61       dsl 		pgrp = pid_table[i].pt_pgrp;
    671       1.61       dsl 		if (!P_VALID(proc)) {
    672       1.61       dsl 			/* Up 'use count' so that link is valid */
    673       1.61       dsl 			pid = (P_NEXT(proc) + pt_size) & ~pt_size;
    674  1.163.2.2     rmind 			rpid = 0;
    675       1.61       dsl 			proc = P_FREE(pid);
    676       1.61       dsl 			if (pgrp)
    677       1.61       dsl 				pid = pgrp->pg_id;
    678  1.163.2.2     rmind 		} else {
    679  1.163.2.2     rmind 			pid = pid_table[i].pt_pid;
    680  1.163.2.2     rmind 			rpid = pid;
    681  1.163.2.2     rmind 		}
    682       1.72  junyoung 
    683       1.61       dsl 		/* Save entry in appropriate half of table */
    684       1.61       dsl 		n_pt[pid & pt_size].pt_proc = proc;
    685       1.61       dsl 		n_pt[pid & pt_size].pt_pgrp = pgrp;
    686  1.163.2.2     rmind 		n_pt[pid & pt_size].pt_pid = rpid;
    687       1.61       dsl 
    688       1.61       dsl 		/* Put other piece on start of free list */
    689       1.61       dsl 		pid = (pid ^ pt_size) & ~pid_tbl_mask;
    690       1.61       dsl 		n_pt[pid & pt_size].pt_proc =
    691  1.163.2.2     rmind 			P_FREE((pid & ~pt_size) | next_free_pt);
    692       1.61       dsl 		n_pt[pid & pt_size].pt_pgrp = 0;
    693  1.163.2.2     rmind 		n_pt[pid & pt_size].pt_pid = 0;
    694  1.163.2.2     rmind 
    695       1.61       dsl 		next_free_pt = i | (pid & pt_size);
    696       1.61       dsl 		if (i == 0)
    697       1.61       dsl 			break;
    698       1.61       dsl 	}
    699       1.61       dsl 
    700      1.150     rmind 	/* Save old table size and switch tables */
    701      1.150     rmind 	tsz = pt_size * sizeof(struct pid_table);
    702       1.61       dsl 	n_pt = pid_table;
    703       1.61       dsl 	pid_table = new_pt;
    704  1.163.2.2     rmind 	pid_tbl_mask = new_pt_mask;
    705       1.61       dsl 
    706       1.61       dsl 	/*
    707       1.61       dsl 	 * pid_max starts as PID_MAX (= 30000), once we have 16384
    708       1.61       dsl 	 * allocated pids we need it to be larger!
    709       1.61       dsl 	 */
    710       1.61       dsl 	if (pid_tbl_mask > PID_MAX) {
    711       1.61       dsl 		pid_max = pid_tbl_mask * 2 + 1;
    712       1.61       dsl 		pid_alloc_lim |= pid_alloc_lim << 1;
    713       1.61       dsl 	} else
    714       1.61       dsl 		pid_alloc_lim <<= 1;	/* doubles number of free slots... */
    715       1.61       dsl 
    716      1.136        ad 	mutex_exit(proc_lock);
    717      1.150     rmind 	kmem_free(n_pt, tsz);
    718       1.61       dsl }
    719       1.61       dsl 
    720       1.61       dsl struct proc *
    721       1.61       dsl proc_alloc(void)
    722       1.61       dsl {
    723       1.61       dsl 	struct proc *p;
    724       1.61       dsl 
    725      1.128        ad 	p = pool_cache_get(proc_cache, PR_WAITOK);
    726       1.61       dsl 	p->p_stat = SIDL;			/* protect against others */
    727       1.96  christos 	proc_initspecific(p);
    728  1.163.2.1     rmind 	kdtrace_proc_ctor(NULL, p);
    729  1.163.2.2     rmind 	p->p_pid = -1;
    730  1.163.2.2     rmind 	proc_alloc_pid(p);
    731  1.163.2.2     rmind 	return p;
    732  1.163.2.2     rmind }
    733  1.163.2.2     rmind 
    734  1.163.2.2     rmind pid_t
    735  1.163.2.2     rmind proc_alloc_pid(struct proc *p)
    736  1.163.2.2     rmind {
    737  1.163.2.2     rmind 	struct pid_table *pt;
    738  1.163.2.2     rmind 	pid_t pid;
    739  1.163.2.2     rmind 	int nxt;
    740       1.61       dsl 
    741       1.61       dsl 	for (;;expand_pid_table()) {
    742       1.61       dsl 		if (__predict_false(pid_alloc_cnt >= pid_alloc_lim))
    743       1.61       dsl 			/* ensure pids cycle through 2000+ values */
    744       1.61       dsl 			continue;
    745      1.136        ad 		mutex_enter(proc_lock);
    746       1.61       dsl 		pt = &pid_table[next_free_pt];
    747        1.1       cgd #ifdef DIAGNOSTIC
    748       1.63  christos 		if (__predict_false(P_VALID(pt->pt_proc) || pt->pt_pgrp))
    749       1.61       dsl 			panic("proc_alloc: slot busy");
    750        1.1       cgd #endif
    751       1.61       dsl 		nxt = P_NEXT(pt->pt_proc);
    752       1.61       dsl 		if (nxt & pid_tbl_mask)
    753       1.61       dsl 			break;
    754       1.61       dsl 		/* Table full - expand (NB last entry not used....) */
    755      1.136        ad 		mutex_exit(proc_lock);
    756       1.61       dsl 	}
    757       1.61       dsl 
    758       1.61       dsl 	/* pid is 'saved use count' + 'size' + entry */
    759       1.61       dsl 	pid = (nxt & ~pid_tbl_mask) + pid_tbl_mask + 1 + next_free_pt;
    760       1.61       dsl 	if ((uint)pid > (uint)pid_max)
    761       1.61       dsl 		pid &= pid_tbl_mask;
    762       1.61       dsl 	next_free_pt = nxt & pid_tbl_mask;
    763       1.61       dsl 
    764       1.61       dsl 	/* Grab table slot */
    765       1.61       dsl 	pt->pt_proc = p;
    766  1.163.2.2     rmind 
    767  1.163.2.2     rmind 	KASSERT(pt->pt_pid == 0);
    768  1.163.2.2     rmind 	pt->pt_pid = pid;
    769  1.163.2.2     rmind 	if (p->p_pid == -1) {
    770  1.163.2.2     rmind 		p->p_pid = pid;
    771  1.163.2.2     rmind 	}
    772       1.61       dsl 	pid_alloc_cnt++;
    773      1.136        ad 	mutex_exit(proc_lock);
    774       1.61       dsl 
    775  1.163.2.2     rmind 	return pid;
    776       1.61       dsl }
    777       1.61       dsl 
    778       1.61       dsl /*
    779      1.118        ad  * Free a process id - called from proc_free (in kern_exit.c)
    780      1.100        ad  *
    781      1.136        ad  * Called with the proc_lock held.
    782       1.61       dsl  */
    783       1.61       dsl void
    784  1.163.2.2     rmind proc_free_pid(pid_t pid)
    785       1.61       dsl {
    786       1.61       dsl 	struct pid_table *pt;
    787       1.61       dsl 
    788      1.136        ad 	KASSERT(mutex_owned(proc_lock));
    789       1.61       dsl 
    790       1.61       dsl 	pt = &pid_table[pid & pid_tbl_mask];
    791  1.163.2.2     rmind 
    792       1.61       dsl 	/* save pid use count in slot */
    793       1.61       dsl 	pt->pt_proc = P_FREE(pid & ~pid_tbl_mask);
    794  1.163.2.2     rmind 	KASSERT(pt->pt_pid == pid);
    795  1.163.2.2     rmind 	pt->pt_pid = 0;
    796       1.61       dsl 
    797       1.61       dsl 	if (pt->pt_pgrp == NULL) {
    798       1.61       dsl 		/* link last freed entry onto ours */
    799       1.61       dsl 		pid &= pid_tbl_mask;
    800       1.61       dsl 		pt = &pid_table[last_free_pt];
    801       1.61       dsl 		pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pid);
    802  1.163.2.2     rmind 		pt->pt_pid = 0;
    803       1.61       dsl 		last_free_pt = pid;
    804       1.61       dsl 		pid_alloc_cnt--;
    805       1.61       dsl 	}
    806       1.61       dsl 
    807      1.126        ad 	atomic_dec_uint(&nprocs);
    808       1.61       dsl }
    809       1.61       dsl 
    810      1.128        ad void
    811      1.128        ad proc_free_mem(struct proc *p)
    812      1.128        ad {
    813      1.128        ad 
    814      1.160    darran 	kdtrace_proc_dtor(NULL, p);
    815      1.128        ad 	pool_cache_put(proc_cache, p);
    816      1.128        ad }
    817      1.128        ad 
    818       1.61       dsl /*
    819      1.151     rmind  * proc_enterpgrp: move p to a new or existing process group (and session).
    820       1.61       dsl  *
    821       1.61       dsl  * If we are creating a new pgrp, the pgid should equal
    822       1.72  junyoung  * the calling process' pid.
    823       1.61       dsl  * If is only valid to enter a process group that is in the session
    824       1.61       dsl  * of the process.
    825       1.61       dsl  * Also mksess should only be set if we are creating a process group
    826       1.61       dsl  *
    827      1.134      yamt  * Only called from sys_setsid and sys_setpgid.
    828       1.61       dsl  */
    829       1.61       dsl int
    830      1.151     rmind proc_enterpgrp(struct proc *curp, pid_t pid, pid_t pgid, bool mksess)
    831       1.61       dsl {
    832       1.61       dsl 	struct pgrp *new_pgrp, *pgrp;
    833       1.61       dsl 	struct session *sess;
    834      1.100        ad 	struct proc *p;
    835       1.61       dsl 	int rval;
    836       1.61       dsl 	pid_t pg_id = NO_PGID;
    837       1.61       dsl 
    838      1.151     rmind 	sess = mksess ? kmem_alloc(sizeof(*sess), KM_SLEEP) : NULL;
    839       1.61       dsl 
    840      1.107        ad 	/* Allocate data areas we might need before doing any validity checks */
    841      1.136        ad 	mutex_enter(proc_lock);		/* Because pid_table might change */
    842      1.107        ad 	if (pid_table[pgid & pid_tbl_mask].pt_pgrp == 0) {
    843      1.136        ad 		mutex_exit(proc_lock);
    844      1.131        ad 		new_pgrp = kmem_alloc(sizeof(*new_pgrp), KM_SLEEP);
    845      1.136        ad 		mutex_enter(proc_lock);
    846      1.107        ad 	} else
    847      1.107        ad 		new_pgrp = NULL;
    848       1.61       dsl 	rval = EPERM;	/* most common error (to save typing) */
    849       1.61       dsl 
    850       1.61       dsl 	/* Check pgrp exists or can be created */
    851       1.61       dsl 	pgrp = pid_table[pgid & pid_tbl_mask].pt_pgrp;
    852       1.61       dsl 	if (pgrp != NULL && pgrp->pg_id != pgid)
    853       1.61       dsl 		goto done;
    854       1.61       dsl 
    855       1.61       dsl 	/* Can only set another process under restricted circumstances. */
    856      1.100        ad 	if (pid != curp->p_pid) {
    857  1.163.2.1     rmind 		/* Must exist and be one of our children... */
    858  1.163.2.1     rmind 		p = proc_find(pid);
    859  1.163.2.1     rmind 		if (p == NULL || !p_inferior(p, curp)) {
    860       1.61       dsl 			rval = ESRCH;
    861       1.61       dsl 			goto done;
    862       1.61       dsl 		}
    863       1.61       dsl 		/* ... in the same session... */
    864       1.61       dsl 		if (sess != NULL || p->p_session != curp->p_session)
    865       1.61       dsl 			goto done;
    866       1.61       dsl 		/* ... existing pgid must be in same session ... */
    867       1.61       dsl 		if (pgrp != NULL && pgrp->pg_session != p->p_session)
    868       1.61       dsl 			goto done;
    869       1.61       dsl 		/* ... and not done an exec. */
    870      1.102     pavel 		if (p->p_flag & PK_EXEC) {
    871       1.61       dsl 			rval = EACCES;
    872       1.61       dsl 			goto done;
    873       1.49     enami 		}
    874      1.100        ad 	} else {
    875      1.100        ad 		/* ... setsid() cannot re-enter a pgrp */
    876      1.100        ad 		if (mksess && (curp->p_pgid == curp->p_pid ||
    877  1.163.2.1     rmind 		    pgrp_find(curp->p_pid)))
    878      1.100        ad 			goto done;
    879      1.100        ad 		p = curp;
    880       1.61       dsl 	}
    881        1.1       cgd 
    882       1.61       dsl 	/* Changing the process group/session of a session
    883       1.61       dsl 	   leader is definitely off limits. */
    884       1.61       dsl 	if (SESS_LEADER(p)) {
    885       1.61       dsl 		if (sess == NULL && p->p_pgrp == pgrp)
    886       1.61       dsl 			/* unless it's a definite noop */
    887       1.61       dsl 			rval = 0;
    888       1.61       dsl 		goto done;
    889       1.61       dsl 	}
    890       1.61       dsl 
    891       1.61       dsl 	/* Can only create a process group with id of process */
    892       1.61       dsl 	if (pgrp == NULL && pgid != pid)
    893       1.61       dsl 		goto done;
    894       1.61       dsl 
    895       1.61       dsl 	/* Can only create a session if creating pgrp */
    896       1.61       dsl 	if (sess != NULL && pgrp != NULL)
    897       1.61       dsl 		goto done;
    898       1.61       dsl 
    899       1.61       dsl 	/* Check we allocated memory for a pgrp... */
    900       1.61       dsl 	if (pgrp == NULL && new_pgrp == NULL)
    901       1.61       dsl 		goto done;
    902       1.61       dsl 
    903       1.61       dsl 	/* Don't attach to 'zombie' pgrp */
    904       1.61       dsl 	if (pgrp != NULL && LIST_EMPTY(&pgrp->pg_members))
    905       1.61       dsl 		goto done;
    906       1.61       dsl 
    907       1.61       dsl 	/* Expect to succeed now */
    908       1.61       dsl 	rval = 0;
    909       1.61       dsl 
    910       1.61       dsl 	if (pgrp == p->p_pgrp)
    911       1.61       dsl 		/* nothing to do */
    912       1.61       dsl 		goto done;
    913       1.61       dsl 
    914       1.61       dsl 	/* Ok all setup, link up required structures */
    915      1.100        ad 
    916       1.61       dsl 	if (pgrp == NULL) {
    917       1.61       dsl 		pgrp = new_pgrp;
    918      1.141      yamt 		new_pgrp = NULL;
    919       1.61       dsl 		if (sess != NULL) {
    920       1.21   thorpej 			sess->s_sid = p->p_pid;
    921        1.1       cgd 			sess->s_leader = p;
    922        1.1       cgd 			sess->s_count = 1;
    923        1.1       cgd 			sess->s_ttyvp = NULL;
    924        1.1       cgd 			sess->s_ttyp = NULL;
    925       1.58       dsl 			sess->s_flags = p->p_session->s_flags & ~S_LOGIN_SET;
    926       1.25     perry 			memcpy(sess->s_login, p->p_session->s_login,
    927        1.1       cgd 			    sizeof(sess->s_login));
    928      1.100        ad 			p->p_lflag &= ~PL_CONTROLT;
    929        1.1       cgd 		} else {
    930       1.61       dsl 			sess = p->p_pgrp->pg_session;
    931      1.151     rmind 			proc_sesshold(sess);
    932        1.1       cgd 		}
    933       1.61       dsl 		pgrp->pg_session = sess;
    934      1.141      yamt 		sess = NULL;
    935       1.61       dsl 
    936        1.1       cgd 		pgrp->pg_id = pgid;
    937       1.10   mycroft 		LIST_INIT(&pgrp->pg_members);
    938       1.61       dsl #ifdef DIAGNOSTIC
    939       1.63  christos 		if (__predict_false(pid_table[pgid & pid_tbl_mask].pt_pgrp))
    940       1.61       dsl 			panic("enterpgrp: pgrp table slot in use");
    941       1.63  christos 		if (__predict_false(mksess && p != curp))
    942       1.63  christos 			panic("enterpgrp: mksession and p != curproc");
    943       1.61       dsl #endif
    944       1.61       dsl 		pid_table[pgid & pid_tbl_mask].pt_pgrp = pgrp;
    945        1.1       cgd 		pgrp->pg_jobc = 0;
    946      1.136        ad 	}
    947        1.1       cgd 
    948        1.1       cgd 	/*
    949        1.1       cgd 	 * Adjust eligibility of affected pgrps to participate in job control.
    950        1.1       cgd 	 * Increment eligibility counts before decrementing, otherwise we
    951        1.1       cgd 	 * could reach 0 spuriously during the first call.
    952        1.1       cgd 	 */
    953        1.1       cgd 	fixjobc(p, pgrp, 1);
    954        1.1       cgd 	fixjobc(p, p->p_pgrp, 0);
    955        1.1       cgd 
    956      1.139        ad 	/* Interlock with ttread(). */
    957      1.139        ad 	mutex_spin_enter(&tty_lock);
    958      1.139        ad 
    959      1.100        ad 	/* Move process to requested group. */
    960       1.10   mycroft 	LIST_REMOVE(p, p_pglist);
    961       1.52      matt 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
    962       1.61       dsl 		/* defer delete until we've dumped the lock */
    963       1.61       dsl 		pg_id = p->p_pgrp->pg_id;
    964        1.1       cgd 	p->p_pgrp = pgrp;
    965       1.10   mycroft 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    966      1.100        ad 
    967      1.100        ad 	/* Done with the swap; we can release the tty mutex. */
    968      1.128        ad 	mutex_spin_exit(&tty_lock);
    969      1.128        ad 
    970       1.61       dsl     done:
    971      1.151     rmind 	if (pg_id != NO_PGID) {
    972      1.151     rmind 		/* Releases proc_lock. */
    973      1.100        ad 		pg_delete(pg_id);
    974      1.151     rmind 	} else {
    975      1.151     rmind 		mutex_exit(proc_lock);
    976      1.151     rmind 	}
    977       1.61       dsl 	if (sess != NULL)
    978      1.131        ad 		kmem_free(sess, sizeof(*sess));
    979       1.61       dsl 	if (new_pgrp != NULL)
    980      1.131        ad 		kmem_free(new_pgrp, sizeof(*new_pgrp));
    981       1.63  christos #ifdef DEBUG_PGRP
    982       1.63  christos 	if (__predict_false(rval))
    983       1.61       dsl 		printf("enterpgrp(%d,%d,%d), curproc %d, rval %d\n",
    984       1.61       dsl 			pid, pgid, mksess, curp->p_pid, rval);
    985       1.61       dsl #endif
    986       1.61       dsl 	return rval;
    987        1.1       cgd }
    988        1.1       cgd 
    989        1.1       cgd /*
    990      1.151     rmind  * proc_leavepgrp: remove a process from its process group.
    991      1.151     rmind  *  => must be called with the proc_lock held, which will be released;
    992        1.1       cgd  */
    993      1.100        ad void
    994      1.151     rmind proc_leavepgrp(struct proc *p)
    995        1.1       cgd {
    996       1.61       dsl 	struct pgrp *pgrp;
    997        1.1       cgd 
    998      1.136        ad 	KASSERT(mutex_owned(proc_lock));
    999      1.100        ad 
   1000      1.139        ad 	/* Interlock with ttread() */
   1001      1.128        ad 	mutex_spin_enter(&tty_lock);
   1002       1.61       dsl 	pgrp = p->p_pgrp;
   1003       1.10   mycroft 	LIST_REMOVE(p, p_pglist);
   1004       1.94        ad 	p->p_pgrp = NULL;
   1005      1.128        ad 	mutex_spin_exit(&tty_lock);
   1006      1.100        ad 
   1007      1.151     rmind 	if (LIST_EMPTY(&pgrp->pg_members)) {
   1008      1.151     rmind 		/* Releases proc_lock. */
   1009      1.100        ad 		pg_delete(pgrp->pg_id);
   1010      1.151     rmind 	} else {
   1011      1.151     rmind 		mutex_exit(proc_lock);
   1012      1.151     rmind 	}
   1013       1.61       dsl }
   1014       1.61       dsl 
   1015      1.100        ad /*
   1016      1.151     rmind  * pg_remove: remove a process group from the table.
   1017      1.151     rmind  *  => must be called with the proc_lock held;
   1018      1.151     rmind  *  => returns process group to free;
   1019      1.100        ad  */
   1020      1.151     rmind static struct pgrp *
   1021      1.151     rmind pg_remove(pid_t pg_id)
   1022       1.61       dsl {
   1023       1.61       dsl 	struct pgrp *pgrp;
   1024       1.61       dsl 	struct pid_table *pt;
   1025       1.61       dsl 
   1026      1.136        ad 	KASSERT(mutex_owned(proc_lock));
   1027      1.100        ad 
   1028       1.61       dsl 	pt = &pid_table[pg_id & pid_tbl_mask];
   1029       1.61       dsl 	pgrp = pt->pt_pgrp;
   1030      1.151     rmind 
   1031      1.151     rmind 	KASSERT(pgrp != NULL);
   1032      1.151     rmind 	KASSERT(pgrp->pg_id == pg_id);
   1033      1.151     rmind 	KASSERT(LIST_EMPTY(&pgrp->pg_members));
   1034      1.151     rmind 
   1035      1.151     rmind 	pt->pt_pgrp = NULL;
   1036       1.61       dsl 
   1037       1.61       dsl 	if (!P_VALID(pt->pt_proc)) {
   1038      1.151     rmind 		/* Orphaned pgrp, put slot onto free list. */
   1039      1.151     rmind 		KASSERT((P_NEXT(pt->pt_proc) & pid_tbl_mask) == 0);
   1040       1.61       dsl 		pg_id &= pid_tbl_mask;
   1041       1.61       dsl 		pt = &pid_table[last_free_pt];
   1042       1.61       dsl 		pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pg_id);
   1043  1.163.2.2     rmind 		KASSERT(pt->pt_pid == 0);
   1044       1.61       dsl 		last_free_pt = pg_id;
   1045       1.61       dsl 		pid_alloc_cnt--;
   1046       1.61       dsl 	}
   1047      1.151     rmind 	return pgrp;
   1048        1.1       cgd }
   1049        1.1       cgd 
   1050        1.1       cgd /*
   1051      1.151     rmind  * pg_delete: delete and free a process group.
   1052      1.151     rmind  *  => must be called with the proc_lock held, which will be released.
   1053        1.1       cgd  */
   1054       1.61       dsl static void
   1055       1.61       dsl pg_delete(pid_t pg_id)
   1056       1.61       dsl {
   1057      1.151     rmind 	struct pgrp *pg;
   1058       1.61       dsl 	struct tty *ttyp;
   1059       1.61       dsl 	struct session *ss;
   1060      1.100        ad 
   1061      1.136        ad 	KASSERT(mutex_owned(proc_lock));
   1062       1.61       dsl 
   1063      1.151     rmind 	pg = pid_table[pg_id & pid_tbl_mask].pt_pgrp;
   1064      1.151     rmind 	if (pg == NULL || pg->pg_id != pg_id || !LIST_EMPTY(&pg->pg_members)) {
   1065      1.151     rmind 		mutex_exit(proc_lock);
   1066       1.61       dsl 		return;
   1067      1.151     rmind 	}
   1068       1.61       dsl 
   1069      1.151     rmind 	ss = pg->pg_session;
   1070       1.71        pk 
   1071       1.61       dsl 	/* Remove reference (if any) from tty to this process group */
   1072      1.128        ad 	mutex_spin_enter(&tty_lock);
   1073       1.71        pk 	ttyp = ss->s_ttyp;
   1074      1.151     rmind 	if (ttyp != NULL && ttyp->t_pgrp == pg) {
   1075       1.61       dsl 		ttyp->t_pgrp = NULL;
   1076      1.151     rmind 		KASSERT(ttyp->t_session == ss);
   1077       1.71        pk 	}
   1078      1.128        ad 	mutex_spin_exit(&tty_lock);
   1079       1.61       dsl 
   1080       1.71        pk 	/*
   1081      1.151     rmind 	 * The leading process group in a session is freed by proc_sessrele(),
   1082      1.151     rmind 	 * if last reference.  Note: proc_sessrele() releases proc_lock.
   1083       1.71        pk 	 */
   1084      1.151     rmind 	pg = (ss->s_sid != pg->pg_id) ? pg_remove(pg_id) : NULL;
   1085      1.151     rmind 	proc_sessrele(ss);
   1086       1.61       dsl 
   1087      1.151     rmind 	if (pg != NULL) {
   1088      1.151     rmind 		/* Free it, if was not done by proc_sessrele(). */
   1089      1.151     rmind 		kmem_free(pg, sizeof(struct pgrp));
   1090      1.151     rmind 	}
   1091        1.1       cgd }
   1092        1.1       cgd 
   1093        1.1       cgd /*
   1094        1.1       cgd  * Adjust pgrp jobc counters when specified process changes process group.
   1095        1.1       cgd  * We count the number of processes in each process group that "qualify"
   1096        1.1       cgd  * the group for terminal job control (those with a parent in a different
   1097        1.1       cgd  * process group of the same session).  If that count reaches zero, the
   1098        1.1       cgd  * process group becomes orphaned.  Check both the specified process'
   1099        1.1       cgd  * process group and that of its children.
   1100        1.1       cgd  * entering == 0 => p is leaving specified group.
   1101        1.1       cgd  * entering == 1 => p is entering specified group.
   1102       1.68       dsl  *
   1103      1.136        ad  * Call with proc_lock held.
   1104        1.1       cgd  */
   1105        1.4    andrew void
   1106       1.59       dsl fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
   1107        1.1       cgd {
   1108       1.39  augustss 	struct pgrp *hispgrp;
   1109       1.39  augustss 	struct session *mysession = pgrp->pg_session;
   1110       1.68       dsl 	struct proc *child;
   1111        1.1       cgd 
   1112      1.136        ad 	KASSERT(mutex_owned(proc_lock));
   1113      1.100        ad 
   1114        1.1       cgd 	/*
   1115        1.1       cgd 	 * Check p's parent to see whether p qualifies its own process
   1116        1.1       cgd 	 * group; if so, adjust count for p's process group.
   1117        1.1       cgd 	 */
   1118       1.68       dsl 	hispgrp = p->p_pptr->p_pgrp;
   1119       1.68       dsl 	if (hispgrp != pgrp && hispgrp->pg_session == mysession) {
   1120      1.100        ad 		if (entering) {
   1121        1.1       cgd 			pgrp->pg_jobc++;
   1122      1.136        ad 			p->p_lflag &= ~PL_ORPHANPG;
   1123      1.100        ad 		} else if (--pgrp->pg_jobc == 0)
   1124        1.1       cgd 			orphanpg(pgrp);
   1125       1.26   thorpej 	}
   1126        1.1       cgd 
   1127        1.1       cgd 	/*
   1128        1.1       cgd 	 * Check this process' children to see whether they qualify
   1129        1.1       cgd 	 * their process groups; if so, adjust counts for children's
   1130        1.1       cgd 	 * process groups.
   1131        1.1       cgd 	 */
   1132       1.68       dsl 	LIST_FOREACH(child, &p->p_children, p_sibling) {
   1133       1.68       dsl 		hispgrp = child->p_pgrp;
   1134       1.68       dsl 		if (hispgrp != pgrp && hispgrp->pg_session == mysession &&
   1135       1.68       dsl 		    !P_ZOMBIE(child)) {
   1136      1.100        ad 			if (entering) {
   1137      1.136        ad 				child->p_lflag &= ~PL_ORPHANPG;
   1138        1.1       cgd 				hispgrp->pg_jobc++;
   1139      1.100        ad 			} else if (--hispgrp->pg_jobc == 0)
   1140        1.1       cgd 				orphanpg(hispgrp);
   1141       1.26   thorpej 		}
   1142       1.26   thorpej 	}
   1143        1.1       cgd }
   1144        1.1       cgd 
   1145       1.72  junyoung /*
   1146        1.1       cgd  * A process group has become orphaned;
   1147        1.1       cgd  * if there are any stopped processes in the group,
   1148        1.1       cgd  * hang-up all process in that group.
   1149       1.68       dsl  *
   1150      1.136        ad  * Call with proc_lock held.
   1151        1.1       cgd  */
   1152        1.4    andrew static void
   1153       1.59       dsl orphanpg(struct pgrp *pg)
   1154        1.1       cgd {
   1155       1.39  augustss 	struct proc *p;
   1156      1.100        ad 
   1157      1.136        ad 	KASSERT(mutex_owned(proc_lock));
   1158      1.100        ad 
   1159       1.52      matt 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
   1160        1.1       cgd 		if (p->p_stat == SSTOP) {
   1161      1.136        ad 			p->p_lflag |= PL_ORPHANPG;
   1162      1.100        ad 			psignal(p, SIGHUP);
   1163      1.100        ad 			psignal(p, SIGCONT);
   1164       1.35    bouyer 		}
   1165       1.35    bouyer 	}
   1166       1.35    bouyer }
   1167        1.1       cgd 
   1168       1.61       dsl #ifdef DDB
   1169       1.61       dsl #include <ddb/db_output.h>
   1170       1.61       dsl void pidtbl_dump(void);
   1171       1.14  christos void
   1172       1.61       dsl pidtbl_dump(void)
   1173        1.1       cgd {
   1174       1.61       dsl 	struct pid_table *pt;
   1175       1.61       dsl 	struct proc *p;
   1176       1.39  augustss 	struct pgrp *pgrp;
   1177       1.61       dsl 	int id;
   1178        1.1       cgd 
   1179       1.61       dsl 	db_printf("pid table %p size %x, next %x, last %x\n",
   1180       1.61       dsl 		pid_table, pid_tbl_mask+1,
   1181       1.61       dsl 		next_free_pt, last_free_pt);
   1182       1.61       dsl 	for (pt = pid_table, id = 0; id <= pid_tbl_mask; id++, pt++) {
   1183       1.61       dsl 		p = pt->pt_proc;
   1184       1.61       dsl 		if (!P_VALID(p) && !pt->pt_pgrp)
   1185       1.61       dsl 			continue;
   1186       1.61       dsl 		db_printf("  id %x: ", id);
   1187       1.61       dsl 		if (P_VALID(p))
   1188  1.163.2.2     rmind 			db_printf("slotpid %d proc %p id %d (0x%x) %s\n",
   1189  1.163.2.2     rmind 				pt->pt_pid, p, p->p_pid, p->p_pid, p->p_comm);
   1190       1.61       dsl 		else
   1191       1.61       dsl 			db_printf("next %x use %x\n",
   1192       1.61       dsl 				P_NEXT(p) & pid_tbl_mask,
   1193       1.61       dsl 				P_NEXT(p) & ~pid_tbl_mask);
   1194       1.61       dsl 		if ((pgrp = pt->pt_pgrp)) {
   1195       1.61       dsl 			db_printf("\tsession %p, sid %d, count %d, login %s\n",
   1196       1.61       dsl 			    pgrp->pg_session, pgrp->pg_session->s_sid,
   1197       1.61       dsl 			    pgrp->pg_session->s_count,
   1198       1.61       dsl 			    pgrp->pg_session->s_login);
   1199       1.61       dsl 			db_printf("\tpgrp %p, pg_id %d, pg_jobc %d, members %p\n",
   1200       1.61       dsl 			    pgrp, pgrp->pg_id, pgrp->pg_jobc,
   1201      1.135      yamt 			    LIST_FIRST(&pgrp->pg_members));
   1202      1.135      yamt 			LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
   1203       1.72  junyoung 				db_printf("\t\tpid %d addr %p pgrp %p %s\n",
   1204       1.61       dsl 				    p->p_pid, p, p->p_pgrp, p->p_comm);
   1205       1.10   mycroft 			}
   1206        1.1       cgd 		}
   1207        1.1       cgd 	}
   1208        1.1       cgd }
   1209       1.61       dsl #endif /* DDB */
   1210       1.48      yamt 
   1211       1.48      yamt #ifdef KSTACK_CHECK_MAGIC
   1212       1.48      yamt 
   1213       1.48      yamt #define	KSTACK_MAGIC	0xdeadbeaf
   1214       1.48      yamt 
   1215       1.48      yamt /* XXX should be per process basis? */
   1216      1.149     rmind static int	kstackleftmin = KSTACK_SIZE;
   1217      1.149     rmind static int	kstackleftthres = KSTACK_SIZE / 8;
   1218       1.48      yamt 
   1219       1.48      yamt void
   1220       1.56      yamt kstack_setup_magic(const struct lwp *l)
   1221       1.48      yamt {
   1222       1.85     perry 	uint32_t *ip;
   1223       1.85     perry 	uint32_t const *end;
   1224       1.48      yamt 
   1225       1.56      yamt 	KASSERT(l != NULL);
   1226       1.56      yamt 	KASSERT(l != &lwp0);
   1227       1.48      yamt 
   1228       1.48      yamt 	/*
   1229       1.48      yamt 	 * fill all the stack with magic number
   1230       1.48      yamt 	 * so that later modification on it can be detected.
   1231       1.48      yamt 	 */
   1232       1.85     perry 	ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1233      1.114    dyoung 	end = (uint32_t *)((char *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1234       1.48      yamt 	for (; ip < end; ip++) {
   1235       1.48      yamt 		*ip = KSTACK_MAGIC;
   1236       1.48      yamt 	}
   1237       1.48      yamt }
   1238       1.48      yamt 
   1239       1.48      yamt void
   1240       1.56      yamt kstack_check_magic(const struct lwp *l)
   1241       1.48      yamt {
   1242       1.85     perry 	uint32_t const *ip, *end;
   1243       1.48      yamt 	int stackleft;
   1244       1.48      yamt 
   1245       1.56      yamt 	KASSERT(l != NULL);
   1246       1.48      yamt 
   1247       1.48      yamt 	/* don't check proc0 */ /*XXX*/
   1248       1.56      yamt 	if (l == &lwp0)
   1249       1.48      yamt 		return;
   1250       1.48      yamt 
   1251       1.48      yamt #ifdef __MACHINE_STACK_GROWS_UP
   1252       1.48      yamt 	/* stack grows upwards (eg. hppa) */
   1253      1.106  christos 	ip = (uint32_t *)((void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1254       1.85     perry 	end = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1255       1.48      yamt 	for (ip--; ip >= end; ip--)
   1256       1.48      yamt 		if (*ip != KSTACK_MAGIC)
   1257       1.48      yamt 			break;
   1258       1.72  junyoung 
   1259      1.106  christos 	stackleft = (void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (void *)ip;
   1260       1.48      yamt #else /* __MACHINE_STACK_GROWS_UP */
   1261       1.48      yamt 	/* stack grows downwards (eg. i386) */
   1262       1.85     perry 	ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1263      1.114    dyoung 	end = (uint32_t *)((char *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1264       1.48      yamt 	for (; ip < end; ip++)
   1265       1.48      yamt 		if (*ip != KSTACK_MAGIC)
   1266       1.48      yamt 			break;
   1267       1.48      yamt 
   1268       1.93  christos 	stackleft = ((const char *)ip) - (const char *)KSTACK_LOWEST_ADDR(l);
   1269       1.48      yamt #endif /* __MACHINE_STACK_GROWS_UP */
   1270       1.48      yamt 
   1271       1.48      yamt 	if (kstackleftmin > stackleft) {
   1272       1.48      yamt 		kstackleftmin = stackleft;
   1273       1.48      yamt 		if (stackleft < kstackleftthres)
   1274       1.56      yamt 			printf("warning: kernel stack left %d bytes"
   1275       1.56      yamt 			    "(pid %u:lid %u)\n", stackleft,
   1276       1.56      yamt 			    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
   1277       1.48      yamt 	}
   1278       1.48      yamt 
   1279       1.48      yamt 	if (stackleft <= 0) {
   1280       1.56      yamt 		panic("magic on the top of kernel stack changed for "
   1281       1.56      yamt 		    "pid %u, lid %u: maybe kernel stack overflow",
   1282       1.56      yamt 		    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
   1283       1.48      yamt 	}
   1284       1.48      yamt }
   1285       1.50     enami #endif /* KSTACK_CHECK_MAGIC */
   1286       1.79      yamt 
   1287       1.79      yamt int
   1288       1.79      yamt proclist_foreach_call(struct proclist *list,
   1289       1.79      yamt     int (*callback)(struct proc *, void *arg), void *arg)
   1290       1.79      yamt {
   1291       1.79      yamt 	struct proc marker;
   1292       1.79      yamt 	struct proc *p;
   1293       1.79      yamt 	int ret = 0;
   1294       1.79      yamt 
   1295      1.102     pavel 	marker.p_flag = PK_MARKER;
   1296      1.136        ad 	mutex_enter(proc_lock);
   1297       1.79      yamt 	for (p = LIST_FIRST(list); ret == 0 && p != NULL;) {
   1298      1.102     pavel 		if (p->p_flag & PK_MARKER) {
   1299       1.79      yamt 			p = LIST_NEXT(p, p_list);
   1300       1.79      yamt 			continue;
   1301       1.79      yamt 		}
   1302       1.79      yamt 		LIST_INSERT_AFTER(p, &marker, p_list);
   1303       1.79      yamt 		ret = (*callback)(p, arg);
   1304      1.136        ad 		KASSERT(mutex_owned(proc_lock));
   1305       1.79      yamt 		p = LIST_NEXT(&marker, p_list);
   1306       1.79      yamt 		LIST_REMOVE(&marker, p_list);
   1307       1.79      yamt 	}
   1308      1.136        ad 	mutex_exit(proc_lock);
   1309       1.79      yamt 
   1310       1.79      yamt 	return ret;
   1311       1.79      yamt }
   1312       1.86      yamt 
   1313       1.86      yamt int
   1314       1.86      yamt proc_vmspace_getref(struct proc *p, struct vmspace **vm)
   1315       1.86      yamt {
   1316       1.86      yamt 
   1317       1.86      yamt 	/* XXXCDC: how should locking work here? */
   1318       1.86      yamt 
   1319       1.87      yamt 	/* curproc exception is for coredump. */
   1320       1.87      yamt 
   1321      1.100        ad 	if ((p != curproc && (p->p_sflag & PS_WEXIT) != 0) ||
   1322       1.86      yamt 	    (p->p_vmspace->vm_refcnt < 1)) { /* XXX */
   1323       1.86      yamt 		return EFAULT;
   1324       1.86      yamt 	}
   1325       1.86      yamt 
   1326       1.86      yamt 	uvmspace_addref(p->p_vmspace);
   1327       1.86      yamt 	*vm = p->p_vmspace;
   1328       1.86      yamt 
   1329       1.86      yamt 	return 0;
   1330       1.86      yamt }
   1331       1.94        ad 
   1332       1.94        ad /*
   1333       1.94        ad  * Acquire a write lock on the process credential.
   1334       1.94        ad  */
   1335       1.94        ad void
   1336      1.100        ad proc_crmod_enter(void)
   1337       1.94        ad {
   1338      1.100        ad 	struct lwp *l = curlwp;
   1339      1.100        ad 	struct proc *p = l->l_proc;
   1340      1.100        ad 	struct plimit *lim;
   1341      1.100        ad 	kauth_cred_t oc;
   1342      1.100        ad 	char *cn;
   1343       1.94        ad 
   1344      1.117       dsl 	/* Reset what needs to be reset in plimit. */
   1345      1.117       dsl 	if (p->p_limit->pl_corename != defcorename) {
   1346      1.117       dsl 		lim_privatise(p, false);
   1347      1.117       dsl 		lim = p->p_limit;
   1348      1.117       dsl 		mutex_enter(&lim->pl_lock);
   1349      1.117       dsl 		cn = lim->pl_corename;
   1350      1.117       dsl 		lim->pl_corename = defcorename;
   1351      1.117       dsl 		mutex_exit(&lim->pl_lock);
   1352      1.117       dsl 		if (cn != defcorename)
   1353      1.117       dsl 			free(cn, M_TEMP);
   1354      1.117       dsl 	}
   1355      1.117       dsl 
   1356      1.137        ad 	mutex_enter(p->p_lock);
   1357      1.100        ad 
   1358      1.100        ad 	/* Ensure the LWP cached credentials are up to date. */
   1359      1.100        ad 	if ((oc = l->l_cred) != p->p_cred) {
   1360      1.100        ad 		kauth_cred_hold(p->p_cred);
   1361      1.100        ad 		l->l_cred = p->p_cred;
   1362      1.100        ad 		kauth_cred_free(oc);
   1363      1.100        ad 	}
   1364      1.100        ad 
   1365       1.94        ad }
   1366       1.94        ad 
   1367       1.94        ad /*
   1368      1.100        ad  * Set in a new process credential, and drop the write lock.  The credential
   1369      1.100        ad  * must have a reference already.  Optionally, free a no-longer required
   1370      1.100        ad  * credential.  The scheduler also needs to inspect p_cred, so we also
   1371      1.100        ad  * briefly acquire the sched state mutex.
   1372       1.94        ad  */
   1373       1.94        ad void
   1374      1.104   thorpej proc_crmod_leave(kauth_cred_t scred, kauth_cred_t fcred, bool sugid)
   1375       1.94        ad {
   1376      1.133        ad 	struct lwp *l = curlwp, *l2;
   1377      1.100        ad 	struct proc *p = l->l_proc;
   1378      1.100        ad 	kauth_cred_t oc;
   1379      1.100        ad 
   1380      1.137        ad 	KASSERT(mutex_owned(p->p_lock));
   1381      1.137        ad 
   1382      1.100        ad 	/* Is there a new credential to set in? */
   1383      1.100        ad 	if (scred != NULL) {
   1384      1.100        ad 		p->p_cred = scred;
   1385      1.133        ad 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1386      1.133        ad 			if (l2 != l)
   1387      1.133        ad 				l2->l_prflag |= LPR_CRMOD;
   1388      1.133        ad 		}
   1389      1.100        ad 
   1390      1.100        ad 		/* Ensure the LWP cached credentials are up to date. */
   1391      1.100        ad 		if ((oc = l->l_cred) != scred) {
   1392      1.100        ad 			kauth_cred_hold(scred);
   1393      1.100        ad 			l->l_cred = scred;
   1394      1.100        ad 		}
   1395      1.100        ad 	} else
   1396      1.100        ad 		oc = NULL;	/* XXXgcc */
   1397      1.100        ad 
   1398      1.100        ad 	if (sugid) {
   1399      1.100        ad 		/*
   1400      1.100        ad 		 * Mark process as having changed credentials, stops
   1401      1.100        ad 		 * tracing etc.
   1402      1.100        ad 		 */
   1403      1.102     pavel 		p->p_flag |= PK_SUGID;
   1404      1.100        ad 	}
   1405       1.94        ad 
   1406      1.137        ad 	mutex_exit(p->p_lock);
   1407      1.100        ad 
   1408      1.100        ad 	/* If there is a credential to be released, free it now. */
   1409      1.100        ad 	if (fcred != NULL) {
   1410      1.100        ad 		KASSERT(scred != NULL);
   1411       1.94        ad 		kauth_cred_free(fcred);
   1412      1.100        ad 		if (oc != scred)
   1413      1.100        ad 			kauth_cred_free(oc);
   1414      1.100        ad 	}
   1415      1.100        ad }
   1416      1.100        ad 
   1417      1.100        ad /*
   1418       1.95   thorpej  * proc_specific_key_create --
   1419       1.95   thorpej  *	Create a key for subsystem proc-specific data.
   1420       1.95   thorpej  */
   1421       1.95   thorpej int
   1422       1.95   thorpej proc_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
   1423       1.95   thorpej {
   1424       1.95   thorpej 
   1425       1.98   thorpej 	return (specificdata_key_create(proc_specificdata_domain, keyp, dtor));
   1426       1.95   thorpej }
   1427       1.95   thorpej 
   1428       1.95   thorpej /*
   1429       1.95   thorpej  * proc_specific_key_delete --
   1430       1.95   thorpej  *	Delete a key for subsystem proc-specific data.
   1431       1.95   thorpej  */
   1432       1.95   thorpej void
   1433       1.95   thorpej proc_specific_key_delete(specificdata_key_t key)
   1434       1.95   thorpej {
   1435       1.95   thorpej 
   1436       1.95   thorpej 	specificdata_key_delete(proc_specificdata_domain, key);
   1437       1.95   thorpej }
   1438       1.95   thorpej 
   1439       1.98   thorpej /*
   1440       1.98   thorpej  * proc_initspecific --
   1441       1.98   thorpej  *	Initialize a proc's specificdata container.
   1442       1.98   thorpej  */
   1443       1.96  christos void
   1444       1.96  christos proc_initspecific(struct proc *p)
   1445       1.96  christos {
   1446       1.96  christos 	int error;
   1447       1.98   thorpej 
   1448       1.96  christos 	error = specificdata_init(proc_specificdata_domain, &p->p_specdataref);
   1449       1.96  christos 	KASSERT(error == 0);
   1450       1.96  christos }
   1451       1.96  christos 
   1452       1.95   thorpej /*
   1453       1.98   thorpej  * proc_finispecific --
   1454       1.98   thorpej  *	Finalize a proc's specificdata container.
   1455       1.98   thorpej  */
   1456       1.98   thorpej void
   1457       1.98   thorpej proc_finispecific(struct proc *p)
   1458       1.98   thorpej {
   1459       1.98   thorpej 
   1460       1.98   thorpej 	specificdata_fini(proc_specificdata_domain, &p->p_specdataref);
   1461       1.98   thorpej }
   1462       1.98   thorpej 
   1463       1.98   thorpej /*
   1464       1.95   thorpej  * proc_getspecific --
   1465       1.95   thorpej  *	Return proc-specific data corresponding to the specified key.
   1466       1.95   thorpej  */
   1467       1.95   thorpej void *
   1468       1.95   thorpej proc_getspecific(struct proc *p, specificdata_key_t key)
   1469       1.95   thorpej {
   1470       1.95   thorpej 
   1471       1.95   thorpej 	return (specificdata_getspecific(proc_specificdata_domain,
   1472       1.95   thorpej 					 &p->p_specdataref, key));
   1473       1.95   thorpej }
   1474       1.95   thorpej 
   1475       1.95   thorpej /*
   1476       1.95   thorpej  * proc_setspecific --
   1477       1.95   thorpej  *	Set proc-specific data corresponding to the specified key.
   1478       1.95   thorpej  */
   1479       1.95   thorpej void
   1480       1.95   thorpej proc_setspecific(struct proc *p, specificdata_key_t key, void *data)
   1481       1.95   thorpej {
   1482       1.95   thorpej 
   1483       1.95   thorpej 	specificdata_setspecific(proc_specificdata_domain,
   1484       1.95   thorpej 				 &p->p_specdataref, key, data);
   1485       1.95   thorpej }
   1486      1.154      elad 
   1487      1.154      elad int
   1488      1.154      elad proc_uidmatch(kauth_cred_t cred, kauth_cred_t target)
   1489      1.154      elad {
   1490      1.154      elad 	int r = 0;
   1491      1.154      elad 
   1492      1.154      elad 	if (kauth_cred_getuid(cred) != kauth_cred_getuid(target) ||
   1493      1.154      elad 	    kauth_cred_getuid(cred) != kauth_cred_getsvuid(target)) {
   1494      1.154      elad 		/*
   1495      1.154      elad 		 * suid proc of ours or proc not ours
   1496      1.154      elad 		 */
   1497      1.154      elad 		r = EPERM;
   1498      1.154      elad 	} else if (kauth_cred_getgid(target) != kauth_cred_getsvgid(target)) {
   1499      1.154      elad 		/*
   1500      1.154      elad 		 * sgid proc has sgid back to us temporarily
   1501      1.154      elad 		 */
   1502      1.154      elad 		r = EPERM;
   1503      1.154      elad 	} else {
   1504      1.154      elad 		/*
   1505      1.154      elad 		 * our rgid must be in target's group list (ie,
   1506      1.154      elad 		 * sub-processes started by a sgid process)
   1507      1.154      elad 		 */
   1508      1.154      elad 		int ismember = 0;
   1509      1.154      elad 
   1510      1.154      elad 		if (kauth_cred_ismember_gid(cred,
   1511      1.154      elad 		    kauth_cred_getgid(target), &ismember) != 0 ||
   1512      1.154      elad 		    !ismember)
   1513      1.154      elad 			r = EPERM;
   1514      1.154      elad 	}
   1515      1.154      elad 
   1516      1.154      elad 	return (r);
   1517      1.154      elad }
   1518      1.154      elad 
   1519  1.163.2.2     rmind /*
   1520  1.163.2.2     rmind  * sysctl stuff
   1521  1.163.2.2     rmind  */
   1522  1.163.2.2     rmind 
   1523  1.163.2.2     rmind #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
   1524  1.163.2.2     rmind 
   1525  1.163.2.2     rmind static const u_int sysctl_flagmap[] = {
   1526  1.163.2.2     rmind 	PK_ADVLOCK, P_ADVLOCK,
   1527  1.163.2.2     rmind 	PK_EXEC, P_EXEC,
   1528  1.163.2.2     rmind 	PK_NOCLDWAIT, P_NOCLDWAIT,
   1529  1.163.2.2     rmind 	PK_32, P_32,
   1530  1.163.2.2     rmind 	PK_CLDSIGIGN, P_CLDSIGIGN,
   1531  1.163.2.2     rmind 	PK_SUGID, P_SUGID,
   1532  1.163.2.2     rmind 	0
   1533  1.163.2.2     rmind };
   1534  1.163.2.2     rmind 
   1535  1.163.2.2     rmind static const u_int sysctl_sflagmap[] = {
   1536  1.163.2.2     rmind 	PS_NOCLDSTOP, P_NOCLDSTOP,
   1537  1.163.2.2     rmind 	PS_WEXIT, P_WEXIT,
   1538  1.163.2.2     rmind 	PS_STOPFORK, P_STOPFORK,
   1539  1.163.2.2     rmind 	PS_STOPEXEC, P_STOPEXEC,
   1540  1.163.2.2     rmind 	PS_STOPEXIT, P_STOPEXIT,
   1541  1.163.2.2     rmind 	0
   1542  1.163.2.2     rmind };
   1543  1.163.2.2     rmind 
   1544  1.163.2.2     rmind static const u_int sysctl_slflagmap[] = {
   1545  1.163.2.2     rmind 	PSL_TRACED, P_TRACED,
   1546  1.163.2.2     rmind 	PSL_FSTRACE, P_FSTRACE,
   1547  1.163.2.2     rmind 	PSL_CHTRACED, P_CHTRACED,
   1548  1.163.2.2     rmind 	PSL_SYSCALL, P_SYSCALL,
   1549  1.163.2.2     rmind 	0
   1550  1.163.2.2     rmind };
   1551  1.163.2.2     rmind 
   1552  1.163.2.2     rmind static const u_int sysctl_lflagmap[] = {
   1553  1.163.2.2     rmind 	PL_CONTROLT, P_CONTROLT,
   1554  1.163.2.2     rmind 	PL_PPWAIT, P_PPWAIT,
   1555  1.163.2.2     rmind 	0
   1556  1.163.2.2     rmind };
   1557  1.163.2.2     rmind 
   1558  1.163.2.2     rmind static const u_int sysctl_stflagmap[] = {
   1559  1.163.2.2     rmind 	PST_PROFIL, P_PROFIL,
   1560  1.163.2.2     rmind 	0
   1561  1.163.2.2     rmind 
   1562  1.163.2.2     rmind };
   1563  1.163.2.2     rmind 
   1564  1.163.2.2     rmind /* used by kern_lwp also */
   1565  1.163.2.2     rmind const u_int sysctl_lwpflagmap[] = {
   1566  1.163.2.2     rmind 	LW_SINTR, L_SINTR,
   1567  1.163.2.2     rmind 	LW_SYSTEM, L_SYSTEM,
   1568  1.163.2.2     rmind 	LW_SA, L_SA,	/* WRS ??? */
   1569  1.163.2.2     rmind 	0
   1570  1.163.2.2     rmind };
   1571  1.163.2.2     rmind 
   1572  1.163.2.2     rmind /*
   1573  1.163.2.2     rmind  * Find the most ``active'' lwp of a process and return it for ps display
   1574  1.163.2.2     rmind  * purposes
   1575  1.163.2.2     rmind  */
   1576  1.163.2.2     rmind static struct lwp *
   1577  1.163.2.2     rmind proc_active_lwp(struct proc *p)
   1578  1.163.2.2     rmind {
   1579  1.163.2.2     rmind 	static const int ostat[] = {
   1580  1.163.2.2     rmind 		0,
   1581  1.163.2.2     rmind 		2,	/* LSIDL */
   1582  1.163.2.2     rmind 		6,	/* LSRUN */
   1583  1.163.2.2     rmind 		5,	/* LSSLEEP */
   1584  1.163.2.2     rmind 		4,	/* LSSTOP */
   1585  1.163.2.2     rmind 		0,	/* LSZOMB */
   1586  1.163.2.2     rmind 		1,	/* LSDEAD */
   1587  1.163.2.2     rmind 		7,	/* LSONPROC */
   1588  1.163.2.2     rmind 		3	/* LSSUSPENDED */
   1589  1.163.2.2     rmind 	};
   1590  1.163.2.2     rmind 
   1591  1.163.2.2     rmind 	struct lwp *l, *lp = NULL;
   1592  1.163.2.2     rmind 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1593  1.163.2.2     rmind 		KASSERT(l->l_stat >= 0 && l->l_stat < __arraycount(ostat));
   1594  1.163.2.2     rmind 		if (lp == NULL ||
   1595  1.163.2.2     rmind 		    ostat[l->l_stat] > ostat[lp->l_stat] ||
   1596  1.163.2.2     rmind 		    (ostat[l->l_stat] == ostat[lp->l_stat] &&
   1597  1.163.2.2     rmind 		    l->l_cpticks > lp->l_cpticks)) {
   1598  1.163.2.2     rmind 			lp = l;
   1599  1.163.2.2     rmind 			continue;
   1600  1.163.2.2     rmind 		}
   1601  1.163.2.2     rmind 	}
   1602  1.163.2.2     rmind 	return lp;
   1603  1.163.2.2     rmind }
   1604  1.163.2.2     rmind 
   1605  1.163.2.2     rmind static int
   1606  1.163.2.2     rmind sysctl_doeproc(SYSCTLFN_ARGS)
   1607  1.163.2.2     rmind {
   1608  1.163.2.2     rmind 	union {
   1609  1.163.2.2     rmind 		struct kinfo_proc kproc;
   1610  1.163.2.2     rmind 		struct kinfo_proc2 kproc2;
   1611  1.163.2.2     rmind 	} *kbuf;
   1612  1.163.2.2     rmind 	struct proc *p, *next, *marker;
   1613  1.163.2.2     rmind 	char *where, *dp;
   1614  1.163.2.2     rmind 	int type, op, arg, error;
   1615  1.163.2.2     rmind 	u_int elem_size, kelem_size, elem_count;
   1616  1.163.2.2     rmind 	size_t buflen, needed;
   1617  1.163.2.2     rmind 	bool match, zombie, mmmbrains;
   1618  1.163.2.2     rmind 
   1619  1.163.2.2     rmind 	if (namelen == 1 && name[0] == CTL_QUERY)
   1620  1.163.2.2     rmind 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1621  1.163.2.2     rmind 
   1622  1.163.2.2     rmind 	dp = where = oldp;
   1623  1.163.2.2     rmind 	buflen = where != NULL ? *oldlenp : 0;
   1624  1.163.2.2     rmind 	error = 0;
   1625  1.163.2.2     rmind 	needed = 0;
   1626  1.163.2.2     rmind 	type = rnode->sysctl_num;
   1627  1.163.2.2     rmind 
   1628  1.163.2.2     rmind 	if (type == KERN_PROC) {
   1629  1.163.2.2     rmind 		if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
   1630  1.163.2.2     rmind 			return (EINVAL);
   1631  1.163.2.2     rmind 		op = name[0];
   1632  1.163.2.2     rmind 		if (op != KERN_PROC_ALL)
   1633  1.163.2.2     rmind 			arg = name[1];
   1634  1.163.2.2     rmind 		else
   1635  1.163.2.2     rmind 			arg = 0;		/* Quell compiler warning */
   1636  1.163.2.2     rmind 		elem_count = 0;	/* Ditto */
   1637  1.163.2.2     rmind 		kelem_size = elem_size = sizeof(kbuf->kproc);
   1638  1.163.2.2     rmind 	} else {
   1639  1.163.2.2     rmind 		if (namelen != 4)
   1640  1.163.2.2     rmind 			return (EINVAL);
   1641  1.163.2.2     rmind 		op = name[0];
   1642  1.163.2.2     rmind 		arg = name[1];
   1643  1.163.2.2     rmind 		elem_size = name[2];
   1644  1.163.2.2     rmind 		elem_count = name[3];
   1645  1.163.2.2     rmind 		kelem_size = sizeof(kbuf->kproc2);
   1646  1.163.2.2     rmind 	}
   1647  1.163.2.2     rmind 
   1648  1.163.2.2     rmind 	sysctl_unlock();
   1649  1.163.2.2     rmind 
   1650  1.163.2.2     rmind 	kbuf = kmem_alloc(sizeof(*kbuf), KM_SLEEP);
   1651  1.163.2.2     rmind 	marker = kmem_alloc(sizeof(*marker), KM_SLEEP);
   1652  1.163.2.2     rmind 	marker->p_flag = PK_MARKER;
   1653  1.163.2.2     rmind 
   1654  1.163.2.2     rmind 	mutex_enter(proc_lock);
   1655  1.163.2.2     rmind 	mmmbrains = false;
   1656  1.163.2.2     rmind 	for (p = LIST_FIRST(&allproc);; p = next) {
   1657  1.163.2.2     rmind 		if (p == NULL) {
   1658  1.163.2.2     rmind 			if (!mmmbrains) {
   1659  1.163.2.2     rmind 				p = LIST_FIRST(&zombproc);
   1660  1.163.2.2     rmind 				mmmbrains = true;
   1661  1.163.2.2     rmind 			}
   1662  1.163.2.2     rmind 			if (p == NULL)
   1663  1.163.2.2     rmind 				break;
   1664  1.163.2.2     rmind 		}
   1665  1.163.2.2     rmind 		next = LIST_NEXT(p, p_list);
   1666  1.163.2.2     rmind 		if ((p->p_flag & PK_MARKER) != 0)
   1667  1.163.2.2     rmind 			continue;
   1668  1.163.2.2     rmind 
   1669  1.163.2.2     rmind 		/*
   1670  1.163.2.2     rmind 		 * Skip embryonic processes.
   1671  1.163.2.2     rmind 		 */
   1672  1.163.2.2     rmind 		if (p->p_stat == SIDL)
   1673  1.163.2.2     rmind 			continue;
   1674  1.163.2.2     rmind 
   1675  1.163.2.2     rmind 		mutex_enter(p->p_lock);
   1676  1.163.2.2     rmind 		error = kauth_authorize_process(l->l_cred,
   1677  1.163.2.2     rmind 		    KAUTH_PROCESS_CANSEE, p,
   1678  1.163.2.2     rmind 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
   1679  1.163.2.2     rmind 		if (error != 0) {
   1680  1.163.2.2     rmind 			mutex_exit(p->p_lock);
   1681  1.163.2.2     rmind 			continue;
   1682  1.163.2.2     rmind 		}
   1683  1.163.2.2     rmind 
   1684  1.163.2.2     rmind 		/*
   1685  1.163.2.2     rmind 		 * TODO - make more efficient (see notes below).
   1686  1.163.2.2     rmind 		 * do by session.
   1687  1.163.2.2     rmind 		 */
   1688  1.163.2.2     rmind 		switch (op) {
   1689  1.163.2.2     rmind 		case KERN_PROC_PID:
   1690  1.163.2.2     rmind 			/* could do this with just a lookup */
   1691  1.163.2.2     rmind 			match = (p->p_pid == (pid_t)arg);
   1692  1.163.2.2     rmind 			break;
   1693  1.163.2.2     rmind 
   1694  1.163.2.2     rmind 		case KERN_PROC_PGRP:
   1695  1.163.2.2     rmind 			/* could do this by traversing pgrp */
   1696  1.163.2.2     rmind 			match = (p->p_pgrp->pg_id == (pid_t)arg);
   1697  1.163.2.2     rmind 			break;
   1698  1.163.2.2     rmind 
   1699  1.163.2.2     rmind 		case KERN_PROC_SESSION:
   1700  1.163.2.2     rmind 			match = (p->p_session->s_sid == (pid_t)arg);
   1701  1.163.2.2     rmind 			break;
   1702  1.163.2.2     rmind 
   1703  1.163.2.2     rmind 		case KERN_PROC_TTY:
   1704  1.163.2.2     rmind 			match = true;
   1705  1.163.2.2     rmind 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
   1706  1.163.2.2     rmind 				if ((p->p_lflag & PL_CONTROLT) == 0 ||
   1707  1.163.2.2     rmind 				    p->p_session->s_ttyp == NULL ||
   1708  1.163.2.2     rmind 				    p->p_session->s_ttyvp != NULL) {
   1709  1.163.2.2     rmind 				    	match = false;
   1710  1.163.2.2     rmind 				}
   1711  1.163.2.2     rmind 			} else if ((p->p_lflag & PL_CONTROLT) == 0 ||
   1712  1.163.2.2     rmind 			    p->p_session->s_ttyp == NULL) {
   1713  1.163.2.2     rmind 				if ((dev_t)arg != KERN_PROC_TTY_NODEV) {
   1714  1.163.2.2     rmind 					match = false;
   1715  1.163.2.2     rmind 				}
   1716  1.163.2.2     rmind 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg) {
   1717  1.163.2.2     rmind 				match = false;
   1718  1.163.2.2     rmind 			}
   1719  1.163.2.2     rmind 			break;
   1720  1.163.2.2     rmind 
   1721  1.163.2.2     rmind 		case KERN_PROC_UID:
   1722  1.163.2.2     rmind 			match = (kauth_cred_geteuid(p->p_cred) == (uid_t)arg);
   1723  1.163.2.2     rmind 			break;
   1724  1.163.2.2     rmind 
   1725  1.163.2.2     rmind 		case KERN_PROC_RUID:
   1726  1.163.2.2     rmind 			match = (kauth_cred_getuid(p->p_cred) == (uid_t)arg);
   1727  1.163.2.2     rmind 			break;
   1728  1.163.2.2     rmind 
   1729  1.163.2.2     rmind 		case KERN_PROC_GID:
   1730  1.163.2.2     rmind 			match = (kauth_cred_getegid(p->p_cred) == (uid_t)arg);
   1731  1.163.2.2     rmind 			break;
   1732  1.163.2.2     rmind 
   1733  1.163.2.2     rmind 		case KERN_PROC_RGID:
   1734  1.163.2.2     rmind 			match = (kauth_cred_getgid(p->p_cred) == (uid_t)arg);
   1735  1.163.2.2     rmind 			break;
   1736  1.163.2.2     rmind 
   1737  1.163.2.2     rmind 		case KERN_PROC_ALL:
   1738  1.163.2.2     rmind 			match = true;
   1739  1.163.2.2     rmind 			/* allow everything */
   1740  1.163.2.2     rmind 			break;
   1741  1.163.2.2     rmind 
   1742  1.163.2.2     rmind 		default:
   1743  1.163.2.2     rmind 			error = EINVAL;
   1744  1.163.2.2     rmind 			mutex_exit(p->p_lock);
   1745  1.163.2.2     rmind 			goto cleanup;
   1746  1.163.2.2     rmind 		}
   1747  1.163.2.2     rmind 		if (!match) {
   1748  1.163.2.2     rmind 			mutex_exit(p->p_lock);
   1749  1.163.2.2     rmind 			continue;
   1750  1.163.2.2     rmind 		}
   1751  1.163.2.2     rmind 
   1752  1.163.2.2     rmind 		/*
   1753  1.163.2.2     rmind 		 * Grab a hold on the process.
   1754  1.163.2.2     rmind 		 */
   1755  1.163.2.2     rmind 		if (mmmbrains) {
   1756  1.163.2.2     rmind 			zombie = true;
   1757  1.163.2.2     rmind 		} else {
   1758  1.163.2.2     rmind 			zombie = !rw_tryenter(&p->p_reflock, RW_READER);
   1759  1.163.2.2     rmind 		}
   1760  1.163.2.2     rmind 		if (zombie) {
   1761  1.163.2.2     rmind 			LIST_INSERT_AFTER(p, marker, p_list);
   1762  1.163.2.2     rmind 		}
   1763  1.163.2.2     rmind 
   1764  1.163.2.2     rmind 		if (buflen >= elem_size &&
   1765  1.163.2.2     rmind 		    (type == KERN_PROC || elem_count > 0)) {
   1766  1.163.2.2     rmind 			if (type == KERN_PROC) {
   1767  1.163.2.2     rmind 				kbuf->kproc.kp_proc = *p;
   1768  1.163.2.2     rmind 				fill_eproc(p, &kbuf->kproc.kp_eproc, zombie);
   1769  1.163.2.2     rmind 			} else {
   1770  1.163.2.2     rmind 				fill_kproc2(p, &kbuf->kproc2, zombie);
   1771  1.163.2.2     rmind 				elem_count--;
   1772  1.163.2.2     rmind 			}
   1773  1.163.2.2     rmind 			mutex_exit(p->p_lock);
   1774  1.163.2.2     rmind 			mutex_exit(proc_lock);
   1775  1.163.2.2     rmind 			/*
   1776  1.163.2.2     rmind 			 * Copy out elem_size, but not larger than kelem_size
   1777  1.163.2.2     rmind 			 */
   1778  1.163.2.2     rmind 			error = sysctl_copyout(l, kbuf, dp,
   1779  1.163.2.2     rmind 			    min(kelem_size, elem_size));
   1780  1.163.2.2     rmind 			mutex_enter(proc_lock);
   1781  1.163.2.2     rmind 			if (error) {
   1782  1.163.2.2     rmind 				goto bah;
   1783  1.163.2.2     rmind 			}
   1784  1.163.2.2     rmind 			dp += elem_size;
   1785  1.163.2.2     rmind 			buflen -= elem_size;
   1786  1.163.2.2     rmind 		} else {
   1787  1.163.2.2     rmind 			mutex_exit(p->p_lock);
   1788  1.163.2.2     rmind 		}
   1789  1.163.2.2     rmind 		needed += elem_size;
   1790  1.163.2.2     rmind 
   1791  1.163.2.2     rmind 		/*
   1792  1.163.2.2     rmind 		 * Release reference to process.
   1793  1.163.2.2     rmind 		 */
   1794  1.163.2.2     rmind 	 	if (zombie) {
   1795  1.163.2.2     rmind 			next = LIST_NEXT(marker, p_list);
   1796  1.163.2.2     rmind  			LIST_REMOVE(marker, p_list);
   1797  1.163.2.2     rmind 		} else {
   1798  1.163.2.2     rmind 			rw_exit(&p->p_reflock);
   1799  1.163.2.2     rmind 			next = LIST_NEXT(p, p_list);
   1800  1.163.2.2     rmind 		}
   1801  1.163.2.2     rmind 	}
   1802  1.163.2.2     rmind 	mutex_exit(proc_lock);
   1803  1.163.2.2     rmind 
   1804  1.163.2.2     rmind 	if (where != NULL) {
   1805  1.163.2.2     rmind 		*oldlenp = dp - where;
   1806  1.163.2.2     rmind 		if (needed > *oldlenp) {
   1807  1.163.2.2     rmind 			error = ENOMEM;
   1808  1.163.2.2     rmind 			goto out;
   1809  1.163.2.2     rmind 		}
   1810  1.163.2.2     rmind 	} else {
   1811  1.163.2.2     rmind 		needed += KERN_PROCSLOP;
   1812  1.163.2.2     rmind 		*oldlenp = needed;
   1813  1.163.2.2     rmind 	}
   1814  1.163.2.2     rmind 	if (kbuf)
   1815  1.163.2.2     rmind 		kmem_free(kbuf, sizeof(*kbuf));
   1816  1.163.2.2     rmind 	if (marker)
   1817  1.163.2.2     rmind 		kmem_free(marker, sizeof(*marker));
   1818  1.163.2.2     rmind 	sysctl_relock();
   1819  1.163.2.2     rmind 	return 0;
   1820  1.163.2.2     rmind  bah:
   1821  1.163.2.2     rmind  	if (zombie)
   1822  1.163.2.2     rmind  		LIST_REMOVE(marker, p_list);
   1823  1.163.2.2     rmind 	else
   1824  1.163.2.2     rmind 		rw_exit(&p->p_reflock);
   1825  1.163.2.2     rmind  cleanup:
   1826  1.163.2.2     rmind 	mutex_exit(proc_lock);
   1827  1.163.2.2     rmind  out:
   1828  1.163.2.2     rmind 	if (kbuf)
   1829  1.163.2.2     rmind 		kmem_free(kbuf, sizeof(*kbuf));
   1830  1.163.2.2     rmind 	if (marker)
   1831  1.163.2.2     rmind 		kmem_free(marker, sizeof(*marker));
   1832  1.163.2.2     rmind 	sysctl_relock();
   1833  1.163.2.2     rmind 	return error;
   1834  1.163.2.2     rmind }
   1835  1.163.2.2     rmind 
   1836  1.163.2.2     rmind int
   1837  1.163.2.2     rmind copyin_psstrings(struct proc *p, struct ps_strings *arginfo)
   1838  1.163.2.2     rmind {
   1839  1.163.2.2     rmind 
   1840  1.163.2.2     rmind #ifdef COMPAT_NETBSD32
   1841  1.163.2.2     rmind 	if (p->p_flag & PK_32) {
   1842  1.163.2.2     rmind 		struct ps_strings32 arginfo32;
   1843  1.163.2.2     rmind 
   1844  1.163.2.2     rmind 		int error = copyin_proc(p, (void *)p->p_psstrp, &arginfo32,
   1845  1.163.2.2     rmind 		    sizeof(arginfo32));
   1846  1.163.2.2     rmind 		if (error)
   1847  1.163.2.2     rmind 			return error;
   1848  1.163.2.2     rmind 		arginfo->ps_argvstr = (void *)(uintptr_t)arginfo32.ps_argvstr;
   1849  1.163.2.2     rmind 		arginfo->ps_nargvstr = arginfo32.ps_nargvstr;
   1850  1.163.2.2     rmind 		arginfo->ps_envstr = (void *)(uintptr_t)arginfo32.ps_envstr;
   1851  1.163.2.2     rmind 		arginfo->ps_nenvstr = arginfo32.ps_nenvstr;
   1852  1.163.2.2     rmind 		return 0;
   1853  1.163.2.2     rmind 	}
   1854  1.163.2.2     rmind #endif
   1855  1.163.2.2     rmind 	return copyin_proc(p, (void *)p->p_psstrp, arginfo, sizeof(*arginfo));
   1856  1.163.2.2     rmind }
   1857  1.163.2.2     rmind 
   1858  1.163.2.2     rmind static int
   1859  1.163.2.2     rmind copy_procargs_sysctl_cb(void *cookie_, const void *src, size_t off, size_t len)
   1860  1.163.2.2     rmind {
   1861  1.163.2.2     rmind 	void **cookie = cookie_;
   1862  1.163.2.2     rmind 	struct lwp *l = cookie[0];
   1863  1.163.2.2     rmind 	char *dst = cookie[1];
   1864  1.163.2.2     rmind 
   1865  1.163.2.2     rmind 	return sysctl_copyout(l, src, dst + off, len);
   1866  1.163.2.2     rmind }
   1867  1.163.2.2     rmind 
   1868  1.163.2.2     rmind /*
   1869  1.163.2.2     rmind  * sysctl helper routine for kern.proc_args pseudo-subtree.
   1870  1.163.2.2     rmind  */
   1871  1.163.2.2     rmind static int
   1872  1.163.2.2     rmind sysctl_kern_proc_args(SYSCTLFN_ARGS)
   1873  1.163.2.2     rmind {
   1874  1.163.2.2     rmind 	struct ps_strings pss;
   1875  1.163.2.2     rmind 	struct proc *p;
   1876  1.163.2.2     rmind 	pid_t pid;
   1877  1.163.2.2     rmind 	int type, error;
   1878  1.163.2.2     rmind 	void *cookie[2];
   1879  1.163.2.2     rmind 
   1880  1.163.2.2     rmind 	if (namelen == 1 && name[0] == CTL_QUERY)
   1881  1.163.2.2     rmind 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1882  1.163.2.2     rmind 
   1883  1.163.2.2     rmind 	if (newp != NULL || namelen != 2)
   1884  1.163.2.2     rmind 		return (EINVAL);
   1885  1.163.2.2     rmind 	pid = name[0];
   1886  1.163.2.2     rmind 	type = name[1];
   1887  1.163.2.2     rmind 
   1888  1.163.2.2     rmind 	switch (type) {
   1889  1.163.2.2     rmind 	case KERN_PROC_ARGV:
   1890  1.163.2.2     rmind 	case KERN_PROC_NARGV:
   1891  1.163.2.2     rmind 	case KERN_PROC_ENV:
   1892  1.163.2.2     rmind 	case KERN_PROC_NENV:
   1893  1.163.2.2     rmind 		/* ok */
   1894  1.163.2.2     rmind 		break;
   1895  1.163.2.2     rmind 	default:
   1896  1.163.2.2     rmind 		return (EINVAL);
   1897  1.163.2.2     rmind 	}
   1898  1.163.2.2     rmind 
   1899  1.163.2.2     rmind 	sysctl_unlock();
   1900  1.163.2.2     rmind 
   1901  1.163.2.2     rmind 	/* check pid */
   1902  1.163.2.2     rmind 	mutex_enter(proc_lock);
   1903  1.163.2.2     rmind 	if ((p = proc_find(pid)) == NULL) {
   1904  1.163.2.2     rmind 		error = EINVAL;
   1905  1.163.2.2     rmind 		goto out_locked;
   1906  1.163.2.2     rmind 	}
   1907  1.163.2.2     rmind 	mutex_enter(p->p_lock);
   1908  1.163.2.2     rmind 
   1909  1.163.2.2     rmind 	/* Check permission. */
   1910  1.163.2.2     rmind 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
   1911  1.163.2.2     rmind 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
   1912  1.163.2.2     rmind 		    p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ARGS), NULL, NULL);
   1913  1.163.2.2     rmind 	else if (type == KERN_PROC_ENV || type == KERN_PROC_NENV)
   1914  1.163.2.2     rmind 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
   1915  1.163.2.2     rmind 		    p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENV), NULL, NULL);
   1916  1.163.2.2     rmind 	else
   1917  1.163.2.2     rmind 		error = EINVAL; /* XXXGCC */
   1918  1.163.2.2     rmind 	if (error) {
   1919  1.163.2.2     rmind 		mutex_exit(p->p_lock);
   1920  1.163.2.2     rmind 		goto out_locked;
   1921  1.163.2.2     rmind 	}
   1922  1.163.2.2     rmind 
   1923  1.163.2.2     rmind 	if (oldp == NULL) {
   1924  1.163.2.2     rmind 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
   1925  1.163.2.2     rmind 			*oldlenp = sizeof (int);
   1926  1.163.2.2     rmind 		else
   1927  1.163.2.2     rmind 			*oldlenp = ARG_MAX;	/* XXX XXX XXX */
   1928  1.163.2.2     rmind 		error = 0;
   1929  1.163.2.2     rmind 		mutex_exit(p->p_lock);
   1930  1.163.2.2     rmind 		goto out_locked;
   1931  1.163.2.2     rmind 	}
   1932  1.163.2.2     rmind 
   1933  1.163.2.2     rmind 	/*
   1934  1.163.2.2     rmind 	 * Zombies don't have a stack, so we can't read their psstrings.
   1935  1.163.2.2     rmind 	 * System processes also don't have a user stack.
   1936  1.163.2.2     rmind 	 */
   1937  1.163.2.2     rmind 	if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
   1938  1.163.2.2     rmind 		error = EINVAL;
   1939  1.163.2.2     rmind 		mutex_exit(p->p_lock);
   1940  1.163.2.2     rmind 		goto out_locked;
   1941  1.163.2.2     rmind 	}
   1942  1.163.2.2     rmind 
   1943  1.163.2.2     rmind 	rw_enter(&p->p_reflock, RW_READER);
   1944  1.163.2.2     rmind 	mutex_exit(p->p_lock);
   1945  1.163.2.2     rmind 	mutex_exit(proc_lock);
   1946  1.163.2.2     rmind 
   1947  1.163.2.2     rmind 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
   1948  1.163.2.2     rmind 		int value;
   1949  1.163.2.2     rmind 		if ((error = copyin_psstrings(p, &pss)) == 0) {
   1950  1.163.2.2     rmind 			if (type == KERN_PROC_NARGV)
   1951  1.163.2.2     rmind 				value = pss.ps_nargvstr;
   1952  1.163.2.2     rmind 			else
   1953  1.163.2.2     rmind 				value = pss.ps_nenvstr;
   1954  1.163.2.2     rmind 			error = sysctl_copyout(l, &value, oldp, sizeof(value));
   1955  1.163.2.2     rmind 			*oldlenp = sizeof(value);
   1956  1.163.2.2     rmind 		}
   1957  1.163.2.2     rmind 	} else {
   1958  1.163.2.2     rmind 		cookie[0] = l;
   1959  1.163.2.2     rmind 		cookie[1] = oldp;
   1960  1.163.2.2     rmind 		error = copy_procargs(p, type, oldlenp,
   1961  1.163.2.2     rmind 		    copy_procargs_sysctl_cb, cookie);
   1962  1.163.2.2     rmind 	}
   1963  1.163.2.2     rmind 	rw_exit(&p->p_reflock);
   1964  1.163.2.2     rmind 	sysctl_relock();
   1965  1.163.2.2     rmind 	return error;
   1966  1.163.2.2     rmind 
   1967  1.163.2.2     rmind out_locked:
   1968  1.163.2.2     rmind 	mutex_exit(proc_lock);
   1969  1.163.2.2     rmind 	sysctl_relock();
   1970  1.163.2.2     rmind 	return error;
   1971  1.163.2.2     rmind }
   1972  1.163.2.2     rmind 
   1973  1.163.2.2     rmind int
   1974  1.163.2.2     rmind copy_procargs(struct proc *p, int oid, size_t *limit,
   1975  1.163.2.2     rmind     int (*cb)(void *, const void *, size_t, size_t), void *cookie)
   1976  1.163.2.2     rmind {
   1977  1.163.2.2     rmind 	struct ps_strings pss;
   1978  1.163.2.2     rmind 	size_t len, i, loaded, entry_len;
   1979  1.163.2.2     rmind 	struct uio auio;
   1980  1.163.2.2     rmind 	struct iovec aiov;
   1981  1.163.2.2     rmind 	int error, argvlen;
   1982  1.163.2.2     rmind 	char *arg;
   1983  1.163.2.2     rmind 	char **argv;
   1984  1.163.2.2     rmind 	vaddr_t user_argv;
   1985  1.163.2.2     rmind 	struct vmspace *vmspace;
   1986  1.163.2.2     rmind 
   1987  1.163.2.2     rmind 	/*
   1988  1.163.2.2     rmind 	 * Allocate a temporary buffer to hold the argument vector and
   1989  1.163.2.2     rmind 	 * the arguments themselve.
   1990  1.163.2.2     rmind 	 */
   1991  1.163.2.2     rmind 	arg = kmem_alloc(PAGE_SIZE, KM_SLEEP);
   1992  1.163.2.2     rmind 	argv = kmem_alloc(PAGE_SIZE, KM_SLEEP);
   1993  1.163.2.2     rmind 
   1994  1.163.2.2     rmind 	/*
   1995  1.163.2.2     rmind 	 * Lock the process down in memory.
   1996  1.163.2.2     rmind 	 */
   1997  1.163.2.2     rmind 	vmspace = p->p_vmspace;
   1998  1.163.2.2     rmind 	uvmspace_addref(vmspace);
   1999  1.163.2.2     rmind 
   2000  1.163.2.2     rmind 	/*
   2001  1.163.2.2     rmind 	 * Read in the ps_strings structure.
   2002  1.163.2.2     rmind 	 */
   2003  1.163.2.2     rmind 	if ((error = copyin_psstrings(p, &pss)) != 0)
   2004  1.163.2.2     rmind 		goto done;
   2005  1.163.2.2     rmind 
   2006  1.163.2.2     rmind 	/*
   2007  1.163.2.2     rmind 	 * Now read the address of the argument vector.
   2008  1.163.2.2     rmind 	 */
   2009  1.163.2.2     rmind 	switch (oid) {
   2010  1.163.2.2     rmind 	case KERN_PROC_ARGV:
   2011  1.163.2.2     rmind 		user_argv = (uintptr_t)pss.ps_argvstr;
   2012  1.163.2.2     rmind 		argvlen = pss.ps_nargvstr;
   2013  1.163.2.2     rmind 		break;
   2014  1.163.2.2     rmind 	case KERN_PROC_ENV:
   2015  1.163.2.2     rmind 		user_argv = (uintptr_t)pss.ps_envstr;
   2016  1.163.2.2     rmind 		argvlen = pss.ps_nenvstr;
   2017  1.163.2.2     rmind 		break;
   2018  1.163.2.2     rmind 	default:
   2019  1.163.2.2     rmind 		error = EINVAL;
   2020  1.163.2.2     rmind 		goto done;
   2021  1.163.2.2     rmind 	}
   2022  1.163.2.2     rmind 
   2023  1.163.2.2     rmind 	if (argvlen < 0) {
   2024  1.163.2.2     rmind 		error = EIO;
   2025  1.163.2.2     rmind 		goto done;
   2026  1.163.2.2     rmind 	}
   2027  1.163.2.2     rmind 
   2028  1.163.2.2     rmind #ifdef COMPAT_NETBSD32
   2029  1.163.2.2     rmind 	if (p->p_flag & PK_32)
   2030  1.163.2.2     rmind 		entry_len = sizeof(netbsd32_charp);
   2031  1.163.2.2     rmind 	else
   2032  1.163.2.2     rmind #endif
   2033  1.163.2.2     rmind 		entry_len = sizeof(char *);
   2034  1.163.2.2     rmind 
   2035  1.163.2.2     rmind 	/*
   2036  1.163.2.2     rmind 	 * Now copy each string.
   2037  1.163.2.2     rmind 	 */
   2038  1.163.2.2     rmind 	len = 0; /* bytes written to user buffer */
   2039  1.163.2.2     rmind 	loaded = 0; /* bytes from argv already processed */
   2040  1.163.2.2     rmind 	i = 0; /* To make compiler happy */
   2041  1.163.2.2     rmind 
   2042  1.163.2.2     rmind 	for (; argvlen; --argvlen) {
   2043  1.163.2.2     rmind 		int finished = 0;
   2044  1.163.2.2     rmind 		vaddr_t base;
   2045  1.163.2.2     rmind 		size_t xlen;
   2046  1.163.2.2     rmind 		int j;
   2047  1.163.2.2     rmind 
   2048  1.163.2.2     rmind 		if (loaded == 0) {
   2049  1.163.2.2     rmind 			size_t rem = entry_len * argvlen;
   2050  1.163.2.2     rmind 			loaded = MIN(rem, PAGE_SIZE);
   2051  1.163.2.2     rmind 			error = copyin_vmspace(vmspace,
   2052  1.163.2.2     rmind 			    (const void *)user_argv, argv, loaded);
   2053  1.163.2.2     rmind 			if (error)
   2054  1.163.2.2     rmind 				break;
   2055  1.163.2.2     rmind 			user_argv += loaded;
   2056  1.163.2.2     rmind 			i = 0;
   2057  1.163.2.2     rmind 		}
   2058  1.163.2.2     rmind 
   2059  1.163.2.2     rmind #ifdef COMPAT_NETBSD32
   2060  1.163.2.2     rmind 		if (p->p_flag & PK_32) {
   2061  1.163.2.2     rmind 			netbsd32_charp *argv32;
   2062  1.163.2.2     rmind 
   2063  1.163.2.2     rmind 			argv32 = (netbsd32_charp *)argv;
   2064  1.163.2.2     rmind 			base = (vaddr_t)NETBSD32PTR64(argv32[i++]);
   2065  1.163.2.2     rmind 		} else
   2066  1.163.2.2     rmind #endif
   2067  1.163.2.2     rmind 			base = (vaddr_t)argv[i++];
   2068  1.163.2.2     rmind 		loaded -= entry_len;
   2069  1.163.2.2     rmind 
   2070  1.163.2.2     rmind 		/*
   2071  1.163.2.2     rmind 		 * The program has messed around with its arguments,
   2072  1.163.2.2     rmind 		 * possibly deleting some, and replacing them with
   2073  1.163.2.2     rmind 		 * NULL's. Treat this as the last argument and not
   2074  1.163.2.2     rmind 		 * a failure.
   2075  1.163.2.2     rmind 		 */
   2076  1.163.2.2     rmind 		if (base == 0)
   2077  1.163.2.2     rmind 			break;
   2078  1.163.2.2     rmind 
   2079  1.163.2.2     rmind 		while (!finished) {
   2080  1.163.2.2     rmind 			xlen = PAGE_SIZE - (base & PAGE_MASK);
   2081  1.163.2.2     rmind 
   2082  1.163.2.2     rmind 			aiov.iov_base = arg;
   2083  1.163.2.2     rmind 			aiov.iov_len = PAGE_SIZE;
   2084  1.163.2.2     rmind 			auio.uio_iov = &aiov;
   2085  1.163.2.2     rmind 			auio.uio_iovcnt = 1;
   2086  1.163.2.2     rmind 			auio.uio_offset = base;
   2087  1.163.2.2     rmind 			auio.uio_resid = xlen;
   2088  1.163.2.2     rmind 			auio.uio_rw = UIO_READ;
   2089  1.163.2.2     rmind 			UIO_SETUP_SYSSPACE(&auio);
   2090  1.163.2.2     rmind 			error = uvm_io(&vmspace->vm_map, &auio);
   2091  1.163.2.2     rmind 			if (error)
   2092  1.163.2.2     rmind 				goto done;
   2093  1.163.2.2     rmind 
   2094  1.163.2.2     rmind 			/* Look for the end of the string */
   2095  1.163.2.2     rmind 			for (j = 0; j < xlen; j++) {
   2096  1.163.2.2     rmind 				if (arg[j] == '\0') {
   2097  1.163.2.2     rmind 					xlen = j + 1;
   2098  1.163.2.2     rmind 					finished = 1;
   2099  1.163.2.2     rmind 					break;
   2100  1.163.2.2     rmind 				}
   2101  1.163.2.2     rmind 			}
   2102  1.163.2.2     rmind 
   2103  1.163.2.2     rmind 			/* Check for user buffer overflow */
   2104  1.163.2.2     rmind 			if (len + xlen > *limit) {
   2105  1.163.2.2     rmind 				finished = 1;
   2106  1.163.2.2     rmind 				if (len > *limit)
   2107  1.163.2.2     rmind 					xlen = 0;
   2108  1.163.2.2     rmind 				else
   2109  1.163.2.2     rmind 					xlen = *limit - len;
   2110  1.163.2.2     rmind 			}
   2111  1.163.2.2     rmind 
   2112  1.163.2.2     rmind 			/* Copyout the page */
   2113  1.163.2.2     rmind 			error = (*cb)(cookie, arg, len, xlen);
   2114  1.163.2.2     rmind 			if (error)
   2115  1.163.2.2     rmind 				goto done;
   2116  1.163.2.2     rmind 
   2117  1.163.2.2     rmind 			len += xlen;
   2118  1.163.2.2     rmind 			base += xlen;
   2119  1.163.2.2     rmind 		}
   2120  1.163.2.2     rmind 	}
   2121  1.163.2.2     rmind 	*limit = len;
   2122  1.163.2.2     rmind 
   2123  1.163.2.2     rmind done:
   2124  1.163.2.2     rmind 	kmem_free(argv, PAGE_SIZE);
   2125  1.163.2.2     rmind 	kmem_free(arg, PAGE_SIZE);
   2126  1.163.2.2     rmind 	uvmspace_free(vmspace);
   2127  1.163.2.2     rmind 	return error;
   2128  1.163.2.2     rmind }
   2129  1.163.2.2     rmind 
   2130  1.163.2.2     rmind /*
   2131  1.163.2.2     rmind  * Fill in an eproc structure for the specified process.
   2132  1.163.2.2     rmind  */
   2133  1.163.2.2     rmind void
   2134  1.163.2.2     rmind fill_eproc(struct proc *p, struct eproc *ep, bool zombie)
   2135  1.163.2.2     rmind {
   2136  1.163.2.2     rmind 	struct tty *tp;
   2137  1.163.2.2     rmind 	struct lwp *l;
   2138  1.163.2.2     rmind 
   2139  1.163.2.2     rmind 	KASSERT(mutex_owned(proc_lock));
   2140  1.163.2.2     rmind 	KASSERT(mutex_owned(p->p_lock));
   2141  1.163.2.2     rmind 
   2142  1.163.2.2     rmind 	memset(ep, 0, sizeof(*ep));
   2143  1.163.2.2     rmind 
   2144  1.163.2.2     rmind 	ep->e_paddr = p;
   2145  1.163.2.2     rmind 	ep->e_sess = p->p_session;
   2146  1.163.2.2     rmind 	if (p->p_cred) {
   2147  1.163.2.2     rmind 		kauth_cred_topcred(p->p_cred, &ep->e_pcred);
   2148  1.163.2.2     rmind 		kauth_cred_toucred(p->p_cred, &ep->e_ucred);
   2149  1.163.2.2     rmind 	}
   2150  1.163.2.2     rmind 	if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
   2151  1.163.2.2     rmind 		struct vmspace *vm = p->p_vmspace;
   2152  1.163.2.2     rmind 
   2153  1.163.2.2     rmind 		ep->e_vm.vm_rssize = vm_resident_count(vm);
   2154  1.163.2.2     rmind 		ep->e_vm.vm_tsize = vm->vm_tsize;
   2155  1.163.2.2     rmind 		ep->e_vm.vm_dsize = vm->vm_dsize;
   2156  1.163.2.2     rmind 		ep->e_vm.vm_ssize = vm->vm_ssize;
   2157  1.163.2.2     rmind 		ep->e_vm.vm_map.size = vm->vm_map.size;
   2158  1.163.2.2     rmind 
   2159  1.163.2.2     rmind 		/* Pick the primary (first) LWP */
   2160  1.163.2.2     rmind 		l = proc_active_lwp(p);
   2161  1.163.2.2     rmind 		KASSERT(l != NULL);
   2162  1.163.2.2     rmind 		lwp_lock(l);
   2163  1.163.2.2     rmind 		if (l->l_wchan)
   2164  1.163.2.2     rmind 			strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
   2165  1.163.2.2     rmind 		lwp_unlock(l);
   2166  1.163.2.2     rmind 	}
   2167  1.163.2.2     rmind 	if (p->p_pptr)
   2168  1.163.2.2     rmind 		ep->e_ppid = p->p_pptr->p_pid;
   2169  1.163.2.2     rmind 	if (p->p_pgrp && p->p_session) {
   2170  1.163.2.2     rmind 		ep->e_pgid = p->p_pgrp->pg_id;
   2171  1.163.2.2     rmind 		ep->e_jobc = p->p_pgrp->pg_jobc;
   2172  1.163.2.2     rmind 		ep->e_sid = p->p_session->s_sid;
   2173  1.163.2.2     rmind 		if ((p->p_lflag & PL_CONTROLT) &&
   2174  1.163.2.2     rmind 		    (tp = ep->e_sess->s_ttyp)) {
   2175  1.163.2.2     rmind 			ep->e_tdev = tp->t_dev;
   2176  1.163.2.2     rmind 			ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2177  1.163.2.2     rmind 			ep->e_tsess = tp->t_session;
   2178  1.163.2.2     rmind 		} else
   2179  1.163.2.2     rmind 			ep->e_tdev = (uint32_t)NODEV;
   2180  1.163.2.2     rmind 		ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
   2181  1.163.2.2     rmind 		if (SESS_LEADER(p))
   2182  1.163.2.2     rmind 			ep->e_flag |= EPROC_SLEADER;
   2183  1.163.2.2     rmind 		strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
   2184  1.163.2.2     rmind 	}
   2185  1.163.2.2     rmind 	ep->e_xsize = ep->e_xrssize = 0;
   2186  1.163.2.2     rmind 	ep->e_xccount = ep->e_xswrss = 0;
   2187  1.163.2.2     rmind }
   2188  1.163.2.2     rmind 
   2189  1.163.2.2     rmind /*
   2190  1.163.2.2     rmind  * Fill in a kinfo_proc2 structure for the specified process.
   2191  1.163.2.2     rmind  */
   2192  1.163.2.2     rmind static void
   2193  1.163.2.2     rmind fill_kproc2(struct proc *p, struct kinfo_proc2 *ki, bool zombie)
   2194  1.163.2.2     rmind {
   2195  1.163.2.2     rmind 	struct tty *tp;
   2196  1.163.2.2     rmind 	struct lwp *l, *l2;
   2197  1.163.2.2     rmind 	struct timeval ut, st, rt;
   2198  1.163.2.2     rmind 	sigset_t ss1, ss2;
   2199  1.163.2.2     rmind 	struct rusage ru;
   2200  1.163.2.2     rmind 	struct vmspace *vm;
   2201  1.163.2.2     rmind 
   2202  1.163.2.2     rmind 	KASSERT(mutex_owned(proc_lock));
   2203  1.163.2.2     rmind 	KASSERT(mutex_owned(p->p_lock));
   2204  1.163.2.2     rmind 
   2205  1.163.2.2     rmind 	sigemptyset(&ss1);
   2206  1.163.2.2     rmind 	sigemptyset(&ss2);
   2207  1.163.2.2     rmind 	memset(ki, 0, sizeof(*ki));
   2208  1.163.2.2     rmind 
   2209  1.163.2.2     rmind 	ki->p_paddr = PTRTOUINT64(p);
   2210  1.163.2.2     rmind 	ki->p_fd = PTRTOUINT64(p->p_fd);
   2211  1.163.2.2     rmind 	ki->p_cwdi = PTRTOUINT64(p->p_cwdi);
   2212  1.163.2.2     rmind 	ki->p_stats = PTRTOUINT64(p->p_stats);
   2213  1.163.2.2     rmind 	ki->p_limit = PTRTOUINT64(p->p_limit);
   2214  1.163.2.2     rmind 	ki->p_vmspace = PTRTOUINT64(p->p_vmspace);
   2215  1.163.2.2     rmind 	ki->p_sigacts = PTRTOUINT64(p->p_sigacts);
   2216  1.163.2.2     rmind 	ki->p_sess = PTRTOUINT64(p->p_session);
   2217  1.163.2.2     rmind 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
   2218  1.163.2.2     rmind 	ki->p_ru = PTRTOUINT64(&p->p_stats->p_ru);
   2219  1.163.2.2     rmind 	ki->p_eflag = 0;
   2220  1.163.2.2     rmind 	ki->p_exitsig = p->p_exitsig;
   2221  1.163.2.2     rmind 	ki->p_flag = L_INMEM;   /* Process never swapped out */
   2222  1.163.2.2     rmind 	ki->p_flag |= sysctl_map_flags(sysctl_flagmap, p->p_flag);
   2223  1.163.2.2     rmind 	ki->p_flag |= sysctl_map_flags(sysctl_sflagmap, p->p_sflag);
   2224  1.163.2.2     rmind 	ki->p_flag |= sysctl_map_flags(sysctl_slflagmap, p->p_slflag);
   2225  1.163.2.2     rmind 	ki->p_flag |= sysctl_map_flags(sysctl_lflagmap, p->p_lflag);
   2226  1.163.2.2     rmind 	ki->p_flag |= sysctl_map_flags(sysctl_stflagmap, p->p_stflag);
   2227  1.163.2.2     rmind 	ki->p_pid = p->p_pid;
   2228  1.163.2.2     rmind 	if (p->p_pptr)
   2229  1.163.2.2     rmind 		ki->p_ppid = p->p_pptr->p_pid;
   2230  1.163.2.2     rmind 	else
   2231  1.163.2.2     rmind 		ki->p_ppid = 0;
   2232  1.163.2.2     rmind 	ki->p_uid = kauth_cred_geteuid(p->p_cred);
   2233  1.163.2.2     rmind 	ki->p_ruid = kauth_cred_getuid(p->p_cred);
   2234  1.163.2.2     rmind 	ki->p_gid = kauth_cred_getegid(p->p_cred);
   2235  1.163.2.2     rmind 	ki->p_rgid = kauth_cred_getgid(p->p_cred);
   2236  1.163.2.2     rmind 	ki->p_svuid = kauth_cred_getsvuid(p->p_cred);
   2237  1.163.2.2     rmind 	ki->p_svgid = kauth_cred_getsvgid(p->p_cred);
   2238  1.163.2.2     rmind 	ki->p_ngroups = kauth_cred_ngroups(p->p_cred);
   2239  1.163.2.2     rmind 	kauth_cred_getgroups(p->p_cred, ki->p_groups,
   2240  1.163.2.2     rmind 	    min(ki->p_ngroups, sizeof(ki->p_groups) / sizeof(ki->p_groups[0])),
   2241  1.163.2.2     rmind 	    UIO_SYSSPACE);
   2242  1.163.2.2     rmind 
   2243  1.163.2.2     rmind 	ki->p_uticks = p->p_uticks;
   2244  1.163.2.2     rmind 	ki->p_sticks = p->p_sticks;
   2245  1.163.2.2     rmind 	ki->p_iticks = p->p_iticks;
   2246  1.163.2.2     rmind 	ki->p_tpgid = NO_PGID;	/* may be changed if controlling tty below */
   2247  1.163.2.2     rmind 	ki->p_tracep = PTRTOUINT64(p->p_tracep);
   2248  1.163.2.2     rmind 	ki->p_traceflag = p->p_traceflag;
   2249  1.163.2.2     rmind 
   2250  1.163.2.2     rmind 	memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
   2251  1.163.2.2     rmind 	memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
   2252  1.163.2.2     rmind 
   2253  1.163.2.2     rmind 	ki->p_cpticks = 0;
   2254  1.163.2.2     rmind 	ki->p_pctcpu = p->p_pctcpu;
   2255  1.163.2.2     rmind 	ki->p_estcpu = 0;
   2256  1.163.2.2     rmind 	ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
   2257  1.163.2.2     rmind 	ki->p_realstat = p->p_stat;
   2258  1.163.2.2     rmind 	ki->p_nice = p->p_nice;
   2259  1.163.2.2     rmind 	ki->p_xstat = p->p_xstat;
   2260  1.163.2.2     rmind 	ki->p_acflag = p->p_acflag;
   2261  1.163.2.2     rmind 
   2262  1.163.2.2     rmind 	strncpy(ki->p_comm, p->p_comm,
   2263  1.163.2.2     rmind 	    min(sizeof(ki->p_comm), sizeof(p->p_comm)));
   2264  1.163.2.2     rmind 	strncpy(ki->p_ename, p->p_emul->e_name, sizeof(ki->p_ename));
   2265  1.163.2.2     rmind 
   2266  1.163.2.2     rmind 	ki->p_nlwps = p->p_nlwps;
   2267  1.163.2.2     rmind 	ki->p_realflag = ki->p_flag;
   2268  1.163.2.2     rmind 
   2269  1.163.2.2     rmind 	if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
   2270  1.163.2.2     rmind 		vm = p->p_vmspace;
   2271  1.163.2.2     rmind 		ki->p_vm_rssize = vm_resident_count(vm);
   2272  1.163.2.2     rmind 		ki->p_vm_tsize = vm->vm_tsize;
   2273  1.163.2.2     rmind 		ki->p_vm_dsize = vm->vm_dsize;
   2274  1.163.2.2     rmind 		ki->p_vm_ssize = vm->vm_ssize;
   2275  1.163.2.2     rmind 		ki->p_vm_vsize = vm->vm_map.size;
   2276  1.163.2.2     rmind 		/*
   2277  1.163.2.2     rmind 		 * Since the stack is initially mapped mostly with
   2278  1.163.2.2     rmind 		 * PROT_NONE and grown as needed, adjust the "mapped size"
   2279  1.163.2.2     rmind 		 * to skip the unused stack portion.
   2280  1.163.2.2     rmind 		 */
   2281  1.163.2.2     rmind 		ki->p_vm_msize =
   2282  1.163.2.2     rmind 		    atop(vm->vm_map.size) - vm->vm_issize + vm->vm_ssize;
   2283  1.163.2.2     rmind 
   2284  1.163.2.2     rmind 		/* Pick the primary (first) LWP */
   2285  1.163.2.2     rmind 		l = proc_active_lwp(p);
   2286  1.163.2.2     rmind 		KASSERT(l != NULL);
   2287  1.163.2.2     rmind 		lwp_lock(l);
   2288  1.163.2.2     rmind 		ki->p_nrlwps = p->p_nrlwps;
   2289  1.163.2.2     rmind 		ki->p_forw = 0;
   2290  1.163.2.2     rmind 		ki->p_back = 0;
   2291  1.163.2.2     rmind 		ki->p_addr = PTRTOUINT64(l->l_addr);
   2292  1.163.2.2     rmind 		ki->p_stat = l->l_stat;
   2293  1.163.2.2     rmind 		ki->p_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
   2294  1.163.2.2     rmind 		ki->p_swtime = l->l_swtime;
   2295  1.163.2.2     rmind 		ki->p_slptime = l->l_slptime;
   2296  1.163.2.2     rmind 		if (l->l_stat == LSONPROC)
   2297  1.163.2.2     rmind 			ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2298  1.163.2.2     rmind 		else
   2299  1.163.2.2     rmind 			ki->p_schedflags = 0;
   2300  1.163.2.2     rmind 		ki->p_priority = lwp_eprio(l);
   2301  1.163.2.2     rmind 		ki->p_usrpri = l->l_priority;
   2302  1.163.2.2     rmind 		if (l->l_wchan)
   2303  1.163.2.2     rmind 			strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
   2304  1.163.2.2     rmind 		ki->p_wchan = PTRTOUINT64(l->l_wchan);
   2305  1.163.2.2     rmind 		ki->p_cpuid = cpu_index(l->l_cpu);
   2306  1.163.2.2     rmind 		lwp_unlock(l);
   2307  1.163.2.2     rmind 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2308  1.163.2.2     rmind 			/* This is hardly correct, but... */
   2309  1.163.2.2     rmind 			sigplusset(&l->l_sigpend.sp_set, &ss1);
   2310  1.163.2.2     rmind 			sigplusset(&l->l_sigmask, &ss2);
   2311  1.163.2.2     rmind 			ki->p_cpticks += l->l_cpticks;
   2312  1.163.2.2     rmind 			ki->p_pctcpu += l->l_pctcpu;
   2313  1.163.2.2     rmind 			ki->p_estcpu += l->l_estcpu;
   2314  1.163.2.2     rmind 		}
   2315  1.163.2.2     rmind 	}
   2316  1.163.2.2     rmind 	sigplusset(&p->p_sigpend.sp_set, &ss2);
   2317  1.163.2.2     rmind 	memcpy(&ki->p_siglist, &ss1, sizeof(ki_sigset_t));
   2318  1.163.2.2     rmind 	memcpy(&ki->p_sigmask, &ss2, sizeof(ki_sigset_t));
   2319  1.163.2.2     rmind 
   2320  1.163.2.2     rmind 	if (p->p_session != NULL) {
   2321  1.163.2.2     rmind 		ki->p_sid = p->p_session->s_sid;
   2322  1.163.2.2     rmind 		ki->p__pgid = p->p_pgrp->pg_id;
   2323  1.163.2.2     rmind 		if (p->p_session->s_ttyvp)
   2324  1.163.2.2     rmind 			ki->p_eflag |= EPROC_CTTY;
   2325  1.163.2.2     rmind 		if (SESS_LEADER(p))
   2326  1.163.2.2     rmind 			ki->p_eflag |= EPROC_SLEADER;
   2327  1.163.2.2     rmind 		strncpy(ki->p_login, p->p_session->s_login,
   2328  1.163.2.2     rmind 		    min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
   2329  1.163.2.2     rmind 		ki->p_jobc = p->p_pgrp->pg_jobc;
   2330  1.163.2.2     rmind 		if ((p->p_lflag & PL_CONTROLT) && (tp = p->p_session->s_ttyp)) {
   2331  1.163.2.2     rmind 			ki->p_tdev = tp->t_dev;
   2332  1.163.2.2     rmind 			ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2333  1.163.2.2     rmind 			ki->p_tsess = PTRTOUINT64(tp->t_session);
   2334  1.163.2.2     rmind 		} else {
   2335  1.163.2.2     rmind 			ki->p_tdev = (int32_t)NODEV;
   2336  1.163.2.2     rmind 		}
   2337  1.163.2.2     rmind 	}
   2338  1.163.2.2     rmind 
   2339  1.163.2.2     rmind 	if (!P_ZOMBIE(p) && !zombie) {
   2340  1.163.2.2     rmind 		ki->p_uvalid = 1;
   2341  1.163.2.2     rmind 		ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
   2342  1.163.2.2     rmind 		ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
   2343  1.163.2.2     rmind 
   2344  1.163.2.2     rmind 		calcru(p, &ut, &st, NULL, &rt);
   2345  1.163.2.2     rmind 		ki->p_rtime_sec = rt.tv_sec;
   2346  1.163.2.2     rmind 		ki->p_rtime_usec = rt.tv_usec;
   2347  1.163.2.2     rmind 		ki->p_uutime_sec = ut.tv_sec;
   2348  1.163.2.2     rmind 		ki->p_uutime_usec = ut.tv_usec;
   2349  1.163.2.2     rmind 		ki->p_ustime_sec = st.tv_sec;
   2350  1.163.2.2     rmind 		ki->p_ustime_usec = st.tv_usec;
   2351  1.163.2.2     rmind 
   2352  1.163.2.2     rmind 		memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
   2353  1.163.2.2     rmind 		ki->p_uru_nvcsw = 0;
   2354  1.163.2.2     rmind 		ki->p_uru_nivcsw = 0;
   2355  1.163.2.2     rmind 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   2356  1.163.2.2     rmind 			ki->p_uru_nvcsw += (l2->l_ncsw - l2->l_nivcsw);
   2357  1.163.2.2     rmind 			ki->p_uru_nivcsw += l2->l_nivcsw;
   2358  1.163.2.2     rmind 			ruadd(&ru, &l2->l_ru);
   2359  1.163.2.2     rmind 		}
   2360  1.163.2.2     rmind 		ki->p_uru_maxrss = ru.ru_maxrss;
   2361  1.163.2.2     rmind 		ki->p_uru_ixrss = ru.ru_ixrss;
   2362  1.163.2.2     rmind 		ki->p_uru_idrss = ru.ru_idrss;
   2363  1.163.2.2     rmind 		ki->p_uru_isrss = ru.ru_isrss;
   2364  1.163.2.2     rmind 		ki->p_uru_minflt = ru.ru_minflt;
   2365  1.163.2.2     rmind 		ki->p_uru_majflt = ru.ru_majflt;
   2366  1.163.2.2     rmind 		ki->p_uru_nswap = ru.ru_nswap;
   2367  1.163.2.2     rmind 		ki->p_uru_inblock = ru.ru_inblock;
   2368  1.163.2.2     rmind 		ki->p_uru_oublock = ru.ru_oublock;
   2369  1.163.2.2     rmind 		ki->p_uru_msgsnd = ru.ru_msgsnd;
   2370  1.163.2.2     rmind 		ki->p_uru_msgrcv = ru.ru_msgrcv;
   2371  1.163.2.2     rmind 		ki->p_uru_nsignals = ru.ru_nsignals;
   2372  1.163.2.2     rmind 
   2373  1.163.2.2     rmind 		timeradd(&p->p_stats->p_cru.ru_utime,
   2374  1.163.2.2     rmind 			 &p->p_stats->p_cru.ru_stime, &ut);
   2375  1.163.2.2     rmind 		ki->p_uctime_sec = ut.tv_sec;
   2376  1.163.2.2     rmind 		ki->p_uctime_usec = ut.tv_usec;
   2377  1.163.2.2     rmind 	}
   2378  1.163.2.2     rmind }
   2379