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