Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.274
      1  1.274        ad /*	$NetBSD: kern_proc.c,v 1.274 2023/10/05 19:41:07 ad Exp $	*/
      2   1.33   thorpej 
      3   1.33   thorpej /*-
      4  1.272        ad  * Copyright (c) 1999, 2006, 2007, 2008, 2020, 2023
      5  1.272        ad  *     The NetBSD Foundation, Inc.
      6   1.33   thorpej  * All rights reserved.
      7   1.33   thorpej  *
      8   1.33   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      9   1.33   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     10  1.100        ad  * NASA Ames Research Center, and by Andrew Doran.
     11   1.33   thorpej  *
     12   1.33   thorpej  * Redistribution and use in source and binary forms, with or without
     13   1.33   thorpej  * modification, are permitted provided that the following conditions
     14   1.33   thorpej  * are met:
     15   1.33   thorpej  * 1. Redistributions of source code must retain the above copyright
     16   1.33   thorpej  *    notice, this list of conditions and the following disclaimer.
     17   1.33   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.33   thorpej  *    notice, this list of conditions and the following disclaimer in the
     19   1.33   thorpej  *    documentation and/or other materials provided with the distribution.
     20   1.33   thorpej  *
     21   1.33   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22   1.33   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23   1.33   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24   1.33   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25   1.33   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26   1.33   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27   1.33   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28   1.33   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29   1.33   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30   1.33   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31   1.33   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     32   1.33   thorpej  */
     33    1.9       cgd 
     34    1.1       cgd /*
     35    1.7       cgd  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     36    1.7       cgd  *	The Regents of the University of California.  All rights reserved.
     37    1.1       cgd  *
     38    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     39    1.1       cgd  * modification, are permitted provided that the following conditions
     40    1.1       cgd  * are met:
     41    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     42    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     43    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     44    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     45    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     46   1.65       agc  * 3. Neither the name of the University nor the names of its contributors
     47    1.1       cgd  *    may be used to endorse or promote products derived from this software
     48    1.1       cgd  *    without specific prior written permission.
     49    1.1       cgd  *
     50    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     51    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60    1.1       cgd  * SUCH DAMAGE.
     61    1.1       cgd  *
     62   1.23      fvdl  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
     63    1.1       cgd  */
     64   1.45     lukem 
     65   1.45     lukem #include <sys/cdefs.h>
     66  1.274        ad __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.274 2023/10/05 19:41:07 ad Exp $");
     67   1.48      yamt 
     68  1.165     pooka #ifdef _KERNEL_OPT
     69   1.48      yamt #include "opt_kstack.h"
     70   1.88      onoe #include "opt_maxuprc.h"
     71  1.161    darran #include "opt_dtrace.h"
     72  1.171     pooka #include "opt_compat_netbsd32.h"
     73  1.222  christos #include "opt_kaslr.h"
     74  1.165     pooka #endif
     75    1.1       cgd 
     76  1.205  christos #if defined(__HAVE_COMPAT_NETBSD32) && !defined(COMPAT_NETBSD32) \
     77  1.205  christos     && !defined(_RUMPKERNEL)
     78  1.205  christos #define COMPAT_NETBSD32
     79  1.205  christos #endif
     80  1.205  christos 
     81    1.5   mycroft #include <sys/param.h>
     82    1.5   mycroft #include <sys/systm.h>
     83    1.5   mycroft #include <sys/kernel.h>
     84    1.5   mycroft #include <sys/proc.h>
     85   1.28   thorpej #include <sys/resourcevar.h>
     86    1.5   mycroft #include <sys/buf.h>
     87    1.5   mycroft #include <sys/acct.h>
     88    1.5   mycroft #include <sys/wait.h>
     89    1.5   mycroft #include <sys/file.h>
     90    1.8   mycroft #include <ufs/ufs/quota.h>
     91    1.5   mycroft #include <sys/uio.h>
     92   1.24   thorpej #include <sys/pool.h>
     93  1.147     rmind #include <sys/pset.h>
     94    1.5   mycroft #include <sys/ioctl.h>
     95    1.5   mycroft #include <sys/tty.h>
     96   1.11       cgd #include <sys/signalvar.h>
     97   1.51  gmcgarry #include <sys/ras.h>
     98   1.81  junyoung #include <sys/filedesc.h>
     99  1.185    martin #include <sys/syscall_stats.h>
    100   1.89      elad #include <sys/kauth.h>
    101  1.100        ad #include <sys/sleepq.h>
    102  1.126        ad #include <sys/atomic.h>
    103  1.131        ad #include <sys/kmem.h>
    104  1.194  christos #include <sys/namei.h>
    105  1.160    darran #include <sys/dtrace_bsd.h>
    106  1.170     pooka #include <sys/sysctl.h>
    107  1.170     pooka #include <sys/exec.h>
    108  1.170     pooka #include <sys/cpu.h>
    109  1.225  pgoyette #include <sys/compat_stub.h>
    110  1.250   thorpej #include <sys/futex.h>
    111  1.252        ad #include <sys/pserialize.h>
    112  1.160    darran 
    113  1.169  uebayasi #include <uvm/uvm_extern.h>
    114    1.5   mycroft 
    115    1.7       cgd /*
    116  1.180     rmind  * Process lists.
    117    1.7       cgd  */
    118   1.31   thorpej 
    119  1.180     rmind struct proclist		allproc		__cacheline_aligned;
    120  1.180     rmind struct proclist		zombproc	__cacheline_aligned;
    121   1.32   thorpej 
    122  1.271    simonb kmutex_t		proc_lock	__cacheline_aligned;
    123  1.252        ad static pserialize_t	proc_psz;
    124   1.33   thorpej 
    125   1.33   thorpej /*
    126  1.247   thorpej  * pid to lwp/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.247   thorpej  * A linked list of free entries is passed through the pt_lwp
    134  1.247   thorpej  * field of 'free' items - set odd to be an invalid ptr.  Two
    135  1.247   thorpej  * additional bits are also used to indicate if the slot is
    136  1.247   thorpej  * currently occupied by a proc or lwp, and if the PID is
    137  1.247   thorpej  * hidden from certain kinds of lookups.  We thus require a
    138  1.247   thorpej  * minimum alignment for proc and lwp structures (LWPs are
    139  1.247   thorpej  * at least 32-byte aligned).
    140   1.61       dsl  */
    141   1.61       dsl 
    142   1.61       dsl struct pid_table {
    143  1.247   thorpej 	uintptr_t	pt_slot;
    144   1.61       dsl 	struct pgrp	*pt_pgrp;
    145  1.168       chs 	pid_t		pt_pid;
    146   1.72  junyoung };
    147  1.247   thorpej 
    148  1.248   thorpej #define	PT_F_FREE		((uintptr_t)__BIT(0))
    149  1.247   thorpej #define	PT_F_LWP		0	/* pseudo-flag */
    150  1.248   thorpej #define	PT_F_PROC		((uintptr_t)__BIT(1))
    151  1.247   thorpej 
    152  1.247   thorpej #define	PT_F_TYPEBITS		(PT_F_FREE|PT_F_PROC)
    153  1.251   thorpej #define	PT_F_ALLBITS		(PT_F_FREE|PT_F_PROC)
    154  1.247   thorpej 
    155  1.247   thorpej #define	PT_VALID(s)		(((s) & PT_F_FREE) == 0)
    156  1.247   thorpej #define	PT_RESERVED(s)		((s) == 0)
    157  1.247   thorpej #define	PT_NEXT(s)		((u_int)(s) >> 1)
    158  1.247   thorpej #define	PT_SET_FREE(pid)	(((pid) << 1) | PT_F_FREE)
    159  1.247   thorpej #define	PT_SET_LWP(l)		((uintptr_t)(l))
    160  1.247   thorpej #define	PT_SET_PROC(p)		(((uintptr_t)(p)) | PT_F_PROC)
    161  1.247   thorpej #define	PT_SET_RESERVED		0
    162  1.247   thorpej #define	PT_GET_LWP(s)		((struct lwp *)((s) & ~PT_F_ALLBITS))
    163  1.247   thorpej #define	PT_GET_PROC(s)		((struct proc *)((s) & ~PT_F_ALLBITS))
    164  1.247   thorpej #define	PT_GET_TYPE(s)		((s) & PT_F_TYPEBITS)
    165  1.247   thorpej #define	PT_IS_LWP(s)		(PT_GET_TYPE(s) == PT_F_LWP && (s) != 0)
    166  1.247   thorpej #define	PT_IS_PROC(s)		(PT_GET_TYPE(s) == PT_F_PROC)
    167  1.247   thorpej 
    168  1.247   thorpej #define	MIN_PROC_ALIGNMENT	(PT_F_ALLBITS + 1)
    169   1.61       dsl 
    170  1.180     rmind /*
    171  1.180     rmind  * Table of process IDs (PIDs).
    172  1.180     rmind  */
    173  1.180     rmind static struct pid_table *pid_table	__read_mostly;
    174  1.180     rmind 
    175  1.180     rmind #define	INITIAL_PID_TABLE_SIZE		(1 << 5)
    176  1.180     rmind 
    177  1.180     rmind /* Table mask, threshold for growing and number of allocated PIDs. */
    178  1.180     rmind static u_int		pid_tbl_mask	__read_mostly;
    179  1.180     rmind static u_int		pid_alloc_lim	__read_mostly;
    180  1.180     rmind static u_int		pid_alloc_cnt	__cacheline_aligned;
    181  1.180     rmind 
    182  1.180     rmind /* Next free, last free and maximum PIDs. */
    183  1.180     rmind static u_int		next_free_pt	__cacheline_aligned;
    184  1.180     rmind static u_int		last_free_pt	__cacheline_aligned;
    185  1.180     rmind static pid_t		pid_max		__read_mostly;
    186   1.31   thorpej 
    187   1.81  junyoung /* Components of the first process -- never freed. */
    188  1.123      matt 
    189  1.123      matt struct session session0 = {
    190  1.123      matt 	.s_count = 1,
    191  1.123      matt 	.s_sid = 0,
    192  1.123      matt };
    193  1.123      matt struct pgrp pgrp0 = {
    194  1.123      matt 	.pg_members = LIST_HEAD_INITIALIZER(&pgrp0.pg_members),
    195  1.123      matt 	.pg_session = &session0,
    196  1.123      matt };
    197  1.132        ad filedesc_t filedesc0;
    198  1.123      matt struct cwdinfo cwdi0 = {
    199  1.187     pooka 	.cwdi_cmask = CMASK,
    200  1.123      matt 	.cwdi_refcnt = 1,
    201  1.123      matt };
    202  1.143  gmcgarry struct plimit limit0;
    203   1.81  junyoung struct pstats pstat0;
    204   1.81  junyoung struct vmspace vmspace0;
    205   1.81  junyoung struct sigacts sigacts0;
    206  1.123      matt struct proc proc0 = {
    207  1.123      matt 	.p_lwps = LIST_HEAD_INITIALIZER(&proc0.p_lwps),
    208  1.123      matt 	.p_sigwaiters = LIST_HEAD_INITIALIZER(&proc0.p_sigwaiters),
    209  1.123      matt 	.p_nlwps = 1,
    210  1.123      matt 	.p_nrlwps = 1,
    211  1.123      matt 	.p_pgrp = &pgrp0,
    212  1.123      matt 	.p_comm = "system",
    213  1.123      matt 	/*
    214  1.123      matt 	 * Set P_NOCLDWAIT so that kernel threads are reparented to init(8)
    215  1.123      matt 	 * when they exit.  init(8) can easily wait them out for us.
    216  1.123      matt 	 */
    217  1.123      matt 	.p_flag = PK_SYSTEM | PK_NOCLDWAIT,
    218  1.123      matt 	.p_stat = SACTIVE,
    219  1.123      matt 	.p_nice = NZERO,
    220  1.123      matt 	.p_emul = &emul_netbsd,
    221  1.123      matt 	.p_cwdi = &cwdi0,
    222  1.123      matt 	.p_limit = &limit0,
    223  1.132        ad 	.p_fd = &filedesc0,
    224  1.123      matt 	.p_vmspace = &vmspace0,
    225  1.123      matt 	.p_stats = &pstat0,
    226  1.123      matt 	.p_sigacts = &sigacts0,
    227  1.188      matt #ifdef PROC0_MD_INITIALIZERS
    228  1.188      matt 	PROC0_MD_INITIALIZERS
    229  1.188      matt #endif
    230  1.123      matt };
    231  1.123      matt kauth_cred_t cred0;
    232   1.81  junyoung 
    233  1.180     rmind static const int	nofile	= NOFILE;
    234  1.180     rmind static const int	maxuprc	= MAXUPRC;
    235   1.81  junyoung 
    236  1.170     pooka static int sysctl_doeproc(SYSCTLFN_PROTO);
    237  1.170     pooka static int sysctl_kern_proc_args(SYSCTLFN_PROTO);
    238  1.222  christos static int sysctl_security_expose_address(SYSCTLFN_PROTO);
    239  1.170     pooka 
    240  1.222  christos #ifdef KASLR
    241  1.223  christos static int kern_expose_address = 0;
    242  1.222  christos #else
    243  1.222  christos static int kern_expose_address = 1;
    244  1.222  christos #endif
    245   1.31   thorpej /*
    246   1.31   thorpej  * The process list descriptors, used during pid allocation and
    247   1.31   thorpej  * by sysctl.  No locking on this data structure is needed since
    248   1.31   thorpej  * it is completely static.
    249   1.31   thorpej  */
    250   1.31   thorpej const struct proclist_desc proclists[] = {
    251   1.31   thorpej 	{ &allproc	},
    252   1.31   thorpej 	{ &zombproc	},
    253   1.31   thorpej 	{ NULL		},
    254   1.31   thorpej };
    255   1.31   thorpej 
    256  1.151     rmind static struct pgrp *	pg_remove(pid_t);
    257  1.151     rmind static void		pg_delete(pid_t);
    258  1.151     rmind static void		orphanpg(struct pgrp *);
    259   1.13  christos 
    260   1.95   thorpej static specificdata_domain_t proc_specificdata_domain;
    261   1.95   thorpej 
    262  1.128        ad static pool_cache_t proc_cache;
    263  1.128        ad 
    264  1.153      elad static kauth_listener_t proc_listener;
    265  1.153      elad 
    266  1.222  christos static void fill_proc(const struct proc *, struct proc *, bool);
    267  1.194  christos static int fill_pathname(struct lwp *, pid_t, void *, size_t *);
    268  1.229     kamil static int fill_cwd(struct lwp *, pid_t, void *, size_t *);
    269  1.194  christos 
    270  1.153      elad static int
    271  1.153      elad proc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    272  1.153      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    273  1.153      elad {
    274  1.153      elad 	struct proc *p;
    275  1.153      elad 	int result;
    276  1.153      elad 
    277  1.153      elad 	result = KAUTH_RESULT_DEFER;
    278  1.153      elad 	p = arg0;
    279  1.153      elad 
    280  1.153      elad 	switch (action) {
    281  1.153      elad 	case KAUTH_PROCESS_CANSEE: {
    282  1.153      elad 		enum kauth_process_req req;
    283  1.153      elad 
    284  1.241     joerg 		req = (enum kauth_process_req)(uintptr_t)arg1;
    285  1.153      elad 
    286  1.153      elad 		switch (req) {
    287  1.153      elad 		case KAUTH_REQ_PROCESS_CANSEE_ARGS:
    288  1.153      elad 		case KAUTH_REQ_PROCESS_CANSEE_ENTRY:
    289  1.153      elad 		case KAUTH_REQ_PROCESS_CANSEE_OPENFILES:
    290  1.213      maxv 		case KAUTH_REQ_PROCESS_CANSEE_EPROC:
    291  1.153      elad 			result = KAUTH_RESULT_ALLOW;
    292  1.153      elad 			break;
    293  1.153      elad 
    294  1.153      elad 		case KAUTH_REQ_PROCESS_CANSEE_ENV:
    295  1.153      elad 			if (kauth_cred_getuid(cred) !=
    296  1.153      elad 			    kauth_cred_getuid(p->p_cred) ||
    297  1.153      elad 			    kauth_cred_getuid(cred) !=
    298  1.153      elad 			    kauth_cred_getsvuid(p->p_cred))
    299  1.153      elad 				break;
    300  1.153      elad 
    301  1.153      elad 			result = KAUTH_RESULT_ALLOW;
    302  1.153      elad 
    303  1.153      elad 			break;
    304  1.153      elad 
    305  1.215      maxv 		case KAUTH_REQ_PROCESS_CANSEE_KPTR:
    306  1.222  christos 			if (!kern_expose_address)
    307  1.222  christos 				break;
    308  1.222  christos 
    309  1.222  christos 			if (kern_expose_address == 1 && !(p->p_flag & PK_KMEM))
    310  1.222  christos 				break;
    311  1.222  christos 
    312  1.222  christos 			result = KAUTH_RESULT_ALLOW;
    313  1.222  christos 
    314  1.222  christos 			break;
    315  1.222  christos 
    316  1.153      elad 		default:
    317  1.153      elad 			break;
    318  1.153      elad 		}
    319  1.153      elad 
    320  1.153      elad 		break;
    321  1.153      elad 		}
    322  1.153      elad 
    323  1.153      elad 	case KAUTH_PROCESS_FORK: {
    324  1.153      elad 		int lnprocs = (int)(unsigned long)arg2;
    325  1.153      elad 
    326  1.153      elad 		/*
    327  1.153      elad 		 * Don't allow a nonprivileged user to use the last few
    328  1.153      elad 		 * processes. The variable lnprocs is the current number of
    329  1.153      elad 		 * processes, maxproc is the limit.
    330  1.153      elad 		 */
    331  1.153      elad 		if (__predict_false((lnprocs >= maxproc - 5)))
    332  1.153      elad 			break;
    333  1.153      elad 
    334  1.153      elad 		result = KAUTH_RESULT_ALLOW;
    335  1.153      elad 
    336  1.153      elad 		break;
    337  1.153      elad 		}
    338  1.153      elad 
    339  1.153      elad 	case KAUTH_PROCESS_CORENAME:
    340  1.153      elad 	case KAUTH_PROCESS_STOPFLAG:
    341  1.153      elad 		if (proc_uidmatch(cred, p->p_cred) == 0)
    342  1.153      elad 			result = KAUTH_RESULT_ALLOW;
    343  1.153      elad 
    344  1.153      elad 		break;
    345  1.153      elad 
    346  1.153      elad 	default:
    347  1.153      elad 		break;
    348  1.153      elad 	}
    349  1.153      elad 
    350  1.153      elad 	return result;
    351  1.153      elad }
    352  1.153      elad 
    353  1.221  christos static int
    354  1.221  christos proc_ctor(void *arg __unused, void *obj, int flags __unused)
    355  1.221  christos {
    356  1.263   thorpej 	struct proc *p = obj;
    357  1.263   thorpej 
    358  1.263   thorpej 	memset(p, 0, sizeof(*p));
    359  1.263   thorpej 	klist_init(&p->p_klist);
    360  1.263   thorpej 
    361  1.263   thorpej 	/*
    362  1.263   thorpej 	 * There is no need for a proc_dtor() to do a klist_fini(),
    363  1.263   thorpej 	 * since knote_proc_exit() ensures that p->p_klist is empty
    364  1.263   thorpej 	 * when a process exits.
    365  1.263   thorpej 	 */
    366  1.263   thorpej 
    367  1.221  christos 	return 0;
    368  1.221  christos }
    369  1.221  christos 
    370  1.247   thorpej static pid_t proc_alloc_pid_slot(struct proc *, uintptr_t);
    371  1.247   thorpej 
    372   1.10   mycroft /*
    373   1.10   mycroft  * Initialize global process hashing structures.
    374   1.10   mycroft  */
    375   1.11       cgd void
    376   1.59       dsl procinit(void)
    377    1.7       cgd {
    378   1.31   thorpej 	const struct proclist_desc *pd;
    379  1.150     rmind 	u_int i;
    380   1.61       dsl #define	LINK_EMPTY ((PID_MAX + INITIAL_PID_TABLE_SIZE) & ~(INITIAL_PID_TABLE_SIZE - 1))
    381   1.31   thorpej 
    382   1.31   thorpej 	for (pd = proclists; pd->pd_list != NULL; pd++)
    383   1.31   thorpej 		LIST_INIT(pd->pd_list);
    384    1.7       cgd 
    385  1.253        ad 	mutex_init(&proc_lock, MUTEX_DEFAULT, IPL_NONE);
    386  1.247   thorpej 
    387  1.252        ad 	proc_psz = pserialize_create();
    388  1.247   thorpej 
    389  1.150     rmind 	pid_table = kmem_alloc(INITIAL_PID_TABLE_SIZE
    390  1.150     rmind 	    * sizeof(struct pid_table), KM_SLEEP);
    391  1.180     rmind 	pid_tbl_mask = INITIAL_PID_TABLE_SIZE - 1;
    392  1.180     rmind 	pid_max = PID_MAX;
    393   1.33   thorpej 
    394   1.61       dsl 	/* Set free list running through table...
    395   1.61       dsl 	   Preset 'use count' above PID_MAX so we allocate pid 1 next. */
    396   1.61       dsl 	for (i = 0; i <= pid_tbl_mask; i++) {
    397  1.247   thorpej 		pid_table[i].pt_slot = PT_SET_FREE(LINK_EMPTY + i + 1);
    398   1.61       dsl 		pid_table[i].pt_pgrp = 0;
    399  1.168       chs 		pid_table[i].pt_pid = 0;
    400   1.61       dsl 	}
    401   1.61       dsl 	/* slot 0 is just grabbed */
    402   1.61       dsl 	next_free_pt = 1;
    403   1.61       dsl 	/* Need to fix last entry. */
    404   1.61       dsl 	last_free_pt = pid_tbl_mask;
    405  1.247   thorpej 	pid_table[last_free_pt].pt_slot = PT_SET_FREE(LINK_EMPTY);
    406   1.61       dsl 	/* point at which we grow table - to avoid reusing pids too often */
    407   1.61       dsl 	pid_alloc_lim = pid_tbl_mask - 1;
    408   1.61       dsl #undef LINK_EMPTY
    409   1.61       dsl 
    410  1.247   thorpej 	/* Reserve PID 1 for init(8). */	/* XXX slightly gross */
    411  1.253        ad 	mutex_enter(&proc_lock);
    412  1.247   thorpej 	if (proc_alloc_pid_slot(&proc0, PT_SET_RESERVED) != 1)
    413  1.247   thorpej 		panic("failed to reserve PID 1 for init(8)");
    414  1.253        ad 	mutex_exit(&proc_lock);
    415  1.247   thorpej 
    416   1.95   thorpej 	proc_specificdata_domain = specificdata_domain_create();
    417   1.95   thorpej 	KASSERT(proc_specificdata_domain != NULL);
    418  1.128        ad 
    419  1.247   thorpej 	size_t proc_alignment = coherency_unit;
    420  1.247   thorpej 	if (proc_alignment < MIN_PROC_ALIGNMENT)
    421  1.247   thorpej 		proc_alignment = MIN_PROC_ALIGNMENT;
    422  1.247   thorpej 
    423  1.247   thorpej 	proc_cache = pool_cache_init(sizeof(struct proc), proc_alignment, 0, 0,
    424  1.221  christos 	    "procpl", NULL, IPL_NONE, proc_ctor, NULL, NULL);
    425  1.153      elad 
    426  1.153      elad 	proc_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    427  1.153      elad 	    proc_listener_cb, NULL);
    428    1.7       cgd }
    429    1.1       cgd 
    430  1.170     pooka void
    431  1.170     pooka procinit_sysctl(void)
    432  1.170     pooka {
    433  1.170     pooka 	static struct sysctllog *clog;
    434  1.170     pooka 
    435  1.170     pooka 	sysctl_createv(&clog, 0, NULL, NULL,
    436  1.222  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    437  1.222  christos 		       CTLTYPE_INT, "expose_address",
    438  1.222  christos 		       SYSCTL_DESCR("Enable exposing kernel addresses"),
    439  1.222  christos 		       sysctl_security_expose_address, 0,
    440  1.222  christos 		       &kern_expose_address, 0, CTL_KERN, CTL_CREATE, CTL_EOL);
    441  1.222  christos 	sysctl_createv(&clog, 0, NULL, NULL,
    442  1.170     pooka 		       CTLFLAG_PERMANENT,
    443  1.170     pooka 		       CTLTYPE_NODE, "proc",
    444  1.170     pooka 		       SYSCTL_DESCR("System-wide process information"),
    445  1.170     pooka 		       sysctl_doeproc, 0, NULL, 0,
    446  1.170     pooka 		       CTL_KERN, KERN_PROC, CTL_EOL);
    447  1.170     pooka 	sysctl_createv(&clog, 0, NULL, NULL,
    448  1.170     pooka 		       CTLFLAG_PERMANENT,
    449  1.170     pooka 		       CTLTYPE_NODE, "proc2",
    450  1.170     pooka 		       SYSCTL_DESCR("Machine-independent process information"),
    451  1.170     pooka 		       sysctl_doeproc, 0, NULL, 0,
    452  1.170     pooka 		       CTL_KERN, KERN_PROC2, CTL_EOL);
    453  1.170     pooka 	sysctl_createv(&clog, 0, NULL, NULL,
    454  1.170     pooka 		       CTLFLAG_PERMANENT,
    455  1.170     pooka 		       CTLTYPE_NODE, "proc_args",
    456  1.170     pooka 		       SYSCTL_DESCR("Process argument information"),
    457  1.170     pooka 		       sysctl_kern_proc_args, 0, NULL, 0,
    458  1.170     pooka 		       CTL_KERN, KERN_PROC_ARGS, CTL_EOL);
    459  1.170     pooka 
    460  1.170     pooka 	/*
    461  1.170     pooka 	  "nodes" under these:
    462  1.170     pooka 
    463  1.170     pooka 	  KERN_PROC_ALL
    464  1.170     pooka 	  KERN_PROC_PID pid
    465  1.170     pooka 	  KERN_PROC_PGRP pgrp
    466  1.170     pooka 	  KERN_PROC_SESSION sess
    467  1.170     pooka 	  KERN_PROC_TTY tty
    468  1.170     pooka 	  KERN_PROC_UID uid
    469  1.170     pooka 	  KERN_PROC_RUID uid
    470  1.170     pooka 	  KERN_PROC_GID gid
    471  1.170     pooka 	  KERN_PROC_RGID gid
    472  1.170     pooka 
    473  1.170     pooka 	  all in all, probably not worth the effort...
    474  1.170     pooka 	*/
    475  1.170     pooka }
    476  1.170     pooka 
    477    1.7       cgd /*
    478   1.81  junyoung  * Initialize process 0.
    479   1.81  junyoung  */
    480   1.81  junyoung void
    481   1.81  junyoung proc0_init(void)
    482   1.81  junyoung {
    483   1.81  junyoung 	struct proc *p;
    484   1.81  junyoung 	struct pgrp *pg;
    485  1.177     rmind 	struct rlimit *rlim;
    486   1.81  junyoung 	rlim_t lim;
    487  1.143  gmcgarry 	int i;
    488   1.81  junyoung 
    489   1.81  junyoung 	p = &proc0;
    490   1.81  junyoung 	pg = &pgrp0;
    491  1.123      matt 
    492  1.127        ad 	mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
    493  1.129        ad 	mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
    494  1.137        ad 	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    495  1.107        ad 
    496  1.122        ad 	rw_init(&p->p_reflock);
    497  1.100        ad 	cv_init(&p->p_waitcv, "wait");
    498  1.100        ad 	cv_init(&p->p_lwpcv, "lwpwait");
    499  1.100        ad 
    500  1.166     pooka 	LIST_INSERT_HEAD(&p->p_lwps, &lwp0, l_sibling);
    501  1.100        ad 
    502  1.247   thorpej 	KASSERT(lwp0.l_lid == 0);
    503  1.247   thorpej 	pid_table[lwp0.l_lid].pt_slot = PT_SET_LWP(&lwp0);
    504   1.81  junyoung 	LIST_INSERT_HEAD(&allproc, p, p_list);
    505   1.81  junyoung 
    506  1.247   thorpej 	pid_table[lwp0.l_lid].pt_pgrp = pg;
    507   1.81  junyoung 	LIST_INSERT_HEAD(&pg->pg_members, p, p_pglist);
    508   1.81  junyoung 
    509   1.81  junyoung #ifdef __HAVE_SYSCALL_INTERN
    510   1.81  junyoung 	(*p->p_emul->e_syscall_intern)(p);
    511   1.81  junyoung #endif
    512   1.81  junyoung 
    513   1.81  junyoung 	/* Create credentials. */
    514   1.89      elad 	cred0 = kauth_cred_alloc();
    515   1.89      elad 	p->p_cred = cred0;
    516   1.81  junyoung 
    517   1.81  junyoung 	/* Create the CWD info. */
    518  1.246        ad 	rw_init(&cwdi0.cwdi_lock);
    519   1.81  junyoung 
    520   1.81  junyoung 	/* Create the limits structures. */
    521  1.116       dsl 	mutex_init(&limit0.pl_lock, MUTEX_DEFAULT, IPL_NONE);
    522  1.177     rmind 
    523  1.177     rmind 	rlim = limit0.pl_rlimit;
    524  1.177     rmind 	for (i = 0; i < __arraycount(limit0.pl_rlimit); i++) {
    525  1.177     rmind 		rlim[i].rlim_cur = RLIM_INFINITY;
    526  1.177     rmind 		rlim[i].rlim_max = RLIM_INFINITY;
    527  1.177     rmind 	}
    528  1.177     rmind 
    529  1.177     rmind 	rlim[RLIMIT_NOFILE].rlim_max = maxfiles;
    530  1.177     rmind 	rlim[RLIMIT_NOFILE].rlim_cur = maxfiles < nofile ? maxfiles : nofile;
    531  1.177     rmind 
    532  1.177     rmind 	rlim[RLIMIT_NPROC].rlim_max = maxproc;
    533  1.177     rmind 	rlim[RLIMIT_NPROC].rlim_cur = maxproc < maxuprc ? maxproc : maxuprc;
    534   1.81  junyoung 
    535  1.255        ad 	lim = MIN(VM_MAXUSER_ADDRESS, ctob((rlim_t)uvm_availmem(false)));
    536  1.177     rmind 	rlim[RLIMIT_RSS].rlim_max = lim;
    537  1.177     rmind 	rlim[RLIMIT_MEMLOCK].rlim_max = lim;
    538  1.177     rmind 	rlim[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
    539  1.177     rmind 
    540  1.186  christos 	rlim[RLIMIT_NTHR].rlim_max = maxlwp;
    541  1.267       mrg 	rlim[RLIMIT_NTHR].rlim_cur = maxlwp / 2;
    542  1.186  christos 
    543  1.179     rmind 	/* Note that default core name has zero length. */
    544  1.177     rmind 	limit0.pl_corename = defcorename;
    545  1.179     rmind 	limit0.pl_cnlen = 0;
    546  1.177     rmind 	limit0.pl_refcnt = 1;
    547  1.179     rmind 	limit0.pl_writeable = false;
    548  1.143  gmcgarry 	limit0.pl_sv_limit = NULL;
    549   1.81  junyoung 
    550   1.81  junyoung 	/* Configure virtual memory system, set vm rlimits. */
    551   1.81  junyoung 	uvm_init_limits(p);
    552   1.81  junyoung 
    553   1.81  junyoung 	/* Initialize file descriptor table for proc0. */
    554  1.132        ad 	fd_init(&filedesc0);
    555   1.81  junyoung 
    556   1.81  junyoung 	/*
    557   1.81  junyoung 	 * Initialize proc0's vmspace, which uses the kernel pmap.
    558   1.81  junyoung 	 * All kernel processes (which never have user space mappings)
    559   1.81  junyoung 	 * share proc0's vmspace, and thus, the kernel pmap.
    560   1.81  junyoung 	 */
    561   1.81  junyoung 	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
    562  1.197      maxv 	    trunc_page(VM_MAXUSER_ADDRESS),
    563  1.191  christos #ifdef __USE_TOPDOWN_VM
    564  1.190    martin 	    true
    565  1.190    martin #else
    566  1.190    martin 	    false
    567  1.190    martin #endif
    568  1.190    martin 	    );
    569   1.81  junyoung 
    570  1.127        ad 	/* Initialize signal state for proc0. XXX IPL_SCHED */
    571  1.127        ad 	mutex_init(&p->p_sigacts->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
    572   1.81  junyoung 	siginit(p);
    573   1.96  christos 
    574  1.164     rmind 	proc_initspecific(p);
    575  1.160    darran 	kdtrace_proc_ctor(NULL, p);
    576   1.81  junyoung }
    577   1.81  junyoung 
    578   1.81  junyoung /*
    579  1.151     rmind  * Session reference counting.
    580  1.151     rmind  */
    581  1.151     rmind 
    582  1.151     rmind void
    583  1.151     rmind proc_sesshold(struct session *ss)
    584  1.151     rmind {
    585  1.151     rmind 
    586  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
    587  1.151     rmind 	ss->s_count++;
    588  1.151     rmind }
    589  1.151     rmind 
    590  1.252        ad void
    591  1.252        ad proc_sessrele(struct session *ss)
    592  1.151     rmind {
    593  1.247   thorpej 	struct pgrp *pg;
    594  1.151     rmind 
    595  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
    596  1.245      maxv 	KASSERT(ss->s_count > 0);
    597  1.247   thorpej 
    598  1.151     rmind 	/*
    599  1.151     rmind 	 * We keep the pgrp with the same id as the session in order to
    600  1.151     rmind 	 * stop a process being given the same pid.  Since the pgrp holds
    601  1.151     rmind 	 * a reference to the session, it must be a 'zombie' pgrp by now.
    602  1.151     rmind 	 */
    603  1.151     rmind 	if (--ss->s_count == 0) {
    604  1.247   thorpej 		pg = pg_remove(ss->s_sid);
    605  1.247   thorpej 	} else {
    606  1.247   thorpej 		pg = NULL;
    607  1.247   thorpej 		ss = NULL;
    608  1.247   thorpej 	}
    609  1.151     rmind 
    610  1.253        ad 	mutex_exit(&proc_lock);
    611  1.151     rmind 
    612  1.247   thorpej 	if (pg)
    613  1.151     rmind 		kmem_free(pg, sizeof(struct pgrp));
    614  1.247   thorpej 	if (ss)
    615  1.151     rmind 		kmem_free(ss, sizeof(struct session));
    616  1.247   thorpej }
    617  1.247   thorpej 
    618  1.151     rmind /*
    619   1.74  junyoung  * Check that the specified process group is in the session of the
    620   1.60       dsl  * specified process.
    621   1.60       dsl  * Treats -ve ids as process ids.
    622   1.60       dsl  * Used to validate TIOCSPGRP requests.
    623   1.60       dsl  */
    624   1.60       dsl int
    625   1.60       dsl pgid_in_session(struct proc *p, pid_t pg_id)
    626   1.60       dsl {
    627   1.60       dsl 	struct pgrp *pgrp;
    628  1.101       dsl 	struct session *session;
    629  1.107        ad 	int error;
    630  1.101       dsl 
    631  1.262       nia 	if (pg_id == INT_MIN)
    632  1.262       nia 		return EINVAL;
    633  1.262       nia 
    634  1.253        ad 	mutex_enter(&proc_lock);
    635   1.60       dsl 	if (pg_id < 0) {
    636  1.167     rmind 		struct proc *p1 = proc_find(-pg_id);
    637  1.167     rmind 		if (p1 == NULL) {
    638  1.167     rmind 			error = EINVAL;
    639  1.167     rmind 			goto fail;
    640  1.167     rmind 		}
    641   1.60       dsl 		pgrp = p1->p_pgrp;
    642   1.60       dsl 	} else {
    643  1.167     rmind 		pgrp = pgrp_find(pg_id);
    644  1.167     rmind 		if (pgrp == NULL) {
    645  1.167     rmind 			error = EINVAL;
    646  1.167     rmind 			goto fail;
    647  1.167     rmind 		}
    648   1.60       dsl 	}
    649  1.101       dsl 	session = pgrp->pg_session;
    650  1.167     rmind 	error = (session != p->p_pgrp->pg_session) ? EPERM : 0;
    651  1.167     rmind fail:
    652  1.253        ad 	mutex_exit(&proc_lock);
    653  1.107        ad 	return error;
    654    1.7       cgd }
    655    1.4    andrew 
    656    1.1       cgd /*
    657  1.148     rmind  * p_inferior: is p an inferior of q?
    658    1.1       cgd  */
    659  1.148     rmind static inline bool
    660  1.148     rmind p_inferior(struct proc *p, struct proc *q)
    661    1.1       cgd {
    662    1.1       cgd 
    663  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
    664  1.148     rmind 
    665   1.41  sommerfe 	for (; p != q; p = p->p_pptr)
    666    1.1       cgd 		if (p->p_pid == 0)
    667  1.148     rmind 			return false;
    668  1.148     rmind 	return true;
    669    1.1       cgd }
    670    1.1       cgd 
    671    1.1       cgd /*
    672  1.247   thorpej  * proc_find_lwp: locate an lwp in said proc by the ID.
    673  1.247   thorpej  *
    674  1.247   thorpej  * => Must be called with p::p_lock held.
    675  1.252        ad  * => LSIDL lwps are not returned because they are only partially
    676  1.247   thorpej  *    constructed while occupying the slot.
    677  1.247   thorpej  * => Callers need to be careful about lwp::l_stat of the returned
    678  1.247   thorpej  *    lwp.
    679  1.247   thorpej  */
    680  1.247   thorpej struct lwp *
    681  1.247   thorpej proc_find_lwp(proc_t *p, pid_t pid)
    682  1.247   thorpej {
    683  1.247   thorpej 	struct pid_table *pt;
    684  1.265  riastrad 	unsigned pt_mask;
    685  1.247   thorpej 	struct lwp *l = NULL;
    686  1.247   thorpej 	uintptr_t slot;
    687  1.252        ad 	int s;
    688  1.247   thorpej 
    689  1.247   thorpej 	KASSERT(mutex_owned(p->p_lock));
    690  1.252        ad 
    691  1.252        ad 	/*
    692  1.265  riastrad 	 * Look in the pid_table.  This is done unlocked inside a
    693  1.265  riastrad 	 * pserialize read section covering pid_table's memory
    694  1.265  riastrad 	 * allocation only, so take care to read things in the correct
    695  1.265  riastrad 	 * order:
    696  1.265  riastrad 	 *
    697  1.265  riastrad 	 * 1. First read the table mask -- this only ever increases, in
    698  1.265  riastrad 	 *    expand_pid_table, so a stale value is safely
    699  1.265  riastrad 	 *    conservative.
    700  1.265  riastrad 	 *
    701  1.265  riastrad 	 * 2. Next read the pid table -- this is always set _before_
    702  1.265  riastrad 	 *    the mask increases, so if we see a new table and stale
    703  1.265  riastrad 	 *    mask, the mask is still valid for the table.
    704  1.252        ad 	 */
    705  1.252        ad 	s = pserialize_read_enter();
    706  1.265  riastrad 	pt_mask = atomic_load_acquire(&pid_tbl_mask);
    707  1.265  riastrad 	pt = &atomic_load_consume(&pid_table)[pid & pt_mask];
    708  1.252        ad 	slot = atomic_load_consume(&pt->pt_slot);
    709  1.252        ad 	if (__predict_false(!PT_IS_LWP(slot))) {
    710  1.252        ad 		pserialize_read_exit(s);
    711  1.252        ad 		return NULL;
    712  1.252        ad 	}
    713  1.252        ad 
    714  1.252        ad 	/*
    715  1.252        ad 	 * Check to see if the LWP is from the correct process.  We won't
    716  1.252        ad 	 * see entries in pid_table from a prior process that also used "p",
    717  1.252        ad 	 * by virtue of the fact that allocating "p" means all prior updates
    718  1.252        ad 	 * to dependant data structures are visible to this thread.
    719  1.252        ad 	 */
    720  1.252        ad 	l = PT_GET_LWP(slot);
    721  1.252        ad 	if (__predict_false(atomic_load_relaxed(&l->l_proc) != p)) {
    722  1.252        ad 		pserialize_read_exit(s);
    723  1.252        ad 		return NULL;
    724  1.252        ad 	}
    725  1.252        ad 
    726  1.252        ad 	/*
    727  1.252        ad 	 * We now know that p->p_lock holds this LWP stable.
    728  1.252        ad 	 *
    729  1.252        ad 	 * If the status is not LSIDL, it means the LWP is intended to be
    730  1.252        ad 	 * findable by LID and l_lid cannot change behind us.
    731  1.252        ad 	 *
    732  1.252        ad 	 * No need to acquire the LWP's lock to check for LSIDL, as
    733  1.252        ad 	 * p->p_lock must be held to transition in and out of LSIDL.
    734  1.252        ad 	 * Any other observed state of is no particular interest.
    735  1.252        ad 	 */
    736  1.252        ad 	pserialize_read_exit(s);
    737  1.252        ad 	return l->l_stat != LSIDL && l->l_lid == pid ? l : NULL;
    738  1.252        ad }
    739  1.252        ad 
    740  1.252        ad /*
    741  1.252        ad  * proc_find_lwp_unlocked: locate an lwp in said proc by the ID.
    742  1.252        ad  *
    743  1.252        ad  * => Called in a pserialize read section with no locks held.
    744  1.252        ad  * => LSIDL lwps are not returned because they are only partially
    745  1.252        ad  *    constructed while occupying the slot.
    746  1.252        ad  * => Callers need to be careful about lwp::l_stat of the returned
    747  1.252        ad  *    lwp.
    748  1.252        ad  * => If an LWP is found, it's returned locked.
    749  1.252        ad  */
    750  1.252        ad struct lwp *
    751  1.252        ad proc_find_lwp_unlocked(proc_t *p, pid_t pid)
    752  1.252        ad {
    753  1.252        ad 	struct pid_table *pt;
    754  1.265  riastrad 	unsigned pt_mask;
    755  1.252        ad 	struct lwp *l = NULL;
    756  1.252        ad 	uintptr_t slot;
    757  1.252        ad 
    758  1.252        ad 	KASSERT(pserialize_in_read_section());
    759  1.252        ad 
    760  1.252        ad 	/*
    761  1.265  riastrad 	 * Look in the pid_table.  This is done unlocked inside a
    762  1.265  riastrad 	 * pserialize read section covering pid_table's memory
    763  1.265  riastrad 	 * allocation only, so take care to read things in the correct
    764  1.265  riastrad 	 * order:
    765  1.265  riastrad 	 *
    766  1.265  riastrad 	 * 1. First read the table mask -- this only ever increases, in
    767  1.265  riastrad 	 *    expand_pid_table, so a stale value is safely
    768  1.265  riastrad 	 *    conservative.
    769  1.265  riastrad 	 *
    770  1.265  riastrad 	 * 2. Next read the pid table -- this is always set _before_
    771  1.265  riastrad 	 *    the mask increases, so if we see a new table and stale
    772  1.265  riastrad 	 *    mask, the mask is still valid for the table.
    773  1.252        ad 	 */
    774  1.265  riastrad 	pt_mask = atomic_load_acquire(&pid_tbl_mask);
    775  1.265  riastrad 	pt = &atomic_load_consume(&pid_table)[pid & pt_mask];
    776  1.252        ad 	slot = atomic_load_consume(&pt->pt_slot);
    777  1.252        ad 	if (__predict_false(!PT_IS_LWP(slot))) {
    778  1.252        ad 		return NULL;
    779  1.252        ad 	}
    780  1.252        ad 
    781  1.252        ad 	/*
    782  1.252        ad 	 * Lock the LWP we found to get it stable.  If it's embryonic or
    783  1.252        ad 	 * reaped (LSIDL) then none of the other fields can safely be
    784  1.252        ad 	 * checked.
    785  1.252        ad 	 */
    786  1.252        ad 	l = PT_GET_LWP(slot);
    787  1.252        ad 	lwp_lock(l);
    788  1.252        ad 	if (__predict_false(l->l_stat == LSIDL)) {
    789  1.252        ad 		lwp_unlock(l);
    790  1.252        ad 		return NULL;
    791  1.252        ad 	}
    792  1.247   thorpej 
    793  1.252        ad 	/*
    794  1.252        ad 	 * l_proc and l_lid are now known stable because the LWP is not
    795  1.252        ad 	 * LSIDL, so check those fields too to make sure we found the
    796  1.252        ad 	 * right thing.
    797  1.252        ad 	 */
    798  1.252        ad 	if (__predict_false(l->l_proc != p || l->l_lid != pid)) {
    799  1.252        ad 		lwp_unlock(l);
    800  1.252        ad 		return NULL;
    801  1.247   thorpej 	}
    802  1.247   thorpej 
    803  1.252        ad 	/* Everything checks out, return it locked. */
    804  1.247   thorpej 	return l;
    805  1.247   thorpej }
    806  1.247   thorpej 
    807  1.247   thorpej /*
    808  1.251   thorpej  * proc_find_lwp_acquire_proc: locate an lwp and acquire a lock
    809  1.251   thorpej  * on its containing proc.
    810  1.247   thorpej  *
    811  1.251   thorpej  * => Similar to proc_find_lwp(), but does not require you to have
    812  1.251   thorpej  *    the proc a priori.
    813  1.251   thorpej  * => Also returns proc * to caller, with p::p_lock held.
    814  1.251   thorpej  * => Same caveats apply.
    815  1.247   thorpej  */
    816  1.247   thorpej struct lwp *
    817  1.251   thorpej proc_find_lwp_acquire_proc(pid_t pid, struct proc **pp)
    818  1.247   thorpej {
    819  1.247   thorpej 	struct pid_table *pt;
    820  1.251   thorpej 	struct proc *p = NULL;
    821  1.247   thorpej 	struct lwp *l = NULL;
    822  1.247   thorpej 	uintptr_t slot;
    823  1.247   thorpej 
    824  1.251   thorpej 	KASSERT(pp != NULL);
    825  1.253        ad 	mutex_enter(&proc_lock);
    826  1.247   thorpej 	pt = &pid_table[pid & pid_tbl_mask];
    827  1.247   thorpej 
    828  1.247   thorpej 	slot = pt->pt_slot;
    829  1.251   thorpej 	if (__predict_true(PT_IS_LWP(slot) && pt->pt_pid == pid)) {
    830  1.247   thorpej 		l = PT_GET_LWP(slot);
    831  1.251   thorpej 		p = l->l_proc;
    832  1.251   thorpej 		mutex_enter(p->p_lock);
    833  1.252        ad 		if (__predict_false(l->l_stat == LSIDL)) {
    834  1.251   thorpej 			mutex_exit(p->p_lock);
    835  1.251   thorpej 			l = NULL;
    836  1.251   thorpej 			p = NULL;
    837  1.251   thorpej 		}
    838  1.247   thorpej 	}
    839  1.253        ad 	mutex_exit(&proc_lock);
    840  1.247   thorpej 
    841  1.251   thorpej 	KASSERT(p == NULL || mutex_owned(p->p_lock));
    842  1.251   thorpej 	*pp = p;
    843  1.247   thorpej 	return l;
    844  1.247   thorpej }
    845  1.247   thorpej 
    846  1.247   thorpej /*
    847  1.247   thorpej  * proc_find_raw_pid_table_locked: locate a process by the ID.
    848  1.167     rmind  *
    849  1.252        ad  * => Must be called with proc_lock held.
    850    1.1       cgd  */
    851  1.247   thorpej static proc_t *
    852  1.251   thorpej proc_find_raw_pid_table_locked(pid_t pid, bool any_lwpid)
    853    1.1       cgd {
    854  1.168       chs 	struct pid_table *pt;
    855  1.247   thorpej 	proc_t *p = NULL;
    856  1.247   thorpej 	uintptr_t slot;
    857  1.167     rmind 
    858  1.253        ad 	/* No - used by DDB.  KASSERT(mutex_owned(&proc_lock)); */
    859  1.168       chs 	pt = &pid_table[pid & pid_tbl_mask];
    860  1.247   thorpej 
    861  1.247   thorpej 	slot = pt->pt_slot;
    862  1.247   thorpej 	if (__predict_true(PT_IS_LWP(slot) && pt->pt_pid == pid)) {
    863  1.247   thorpej 		/*
    864  1.247   thorpej 		 * When looking up processes, require a direct match
    865  1.247   thorpej 		 * on the PID assigned to the proc, not just one of
    866  1.247   thorpej 		 * its LWPs.
    867  1.247   thorpej 		 *
    868  1.252        ad 		 * N.B. We require lwp::l_proc of LSIDL LWPs to be
    869  1.247   thorpej 		 * valid here.
    870  1.247   thorpej 		 */
    871  1.247   thorpej 		p = PT_GET_LWP(slot)->l_proc;
    872  1.251   thorpej 		if (__predict_false(p->p_pid != pid && !any_lwpid))
    873  1.247   thorpej 			p = NULL;
    874  1.247   thorpej 	} else if (PT_IS_PROC(slot) && pt->pt_pid == pid) {
    875  1.247   thorpej 		p = PT_GET_PROC(slot);
    876  1.167     rmind 	}
    877  1.167     rmind 	return p;
    878  1.167     rmind }
    879    1.1       cgd 
    880  1.167     rmind proc_t *
    881  1.247   thorpej proc_find_raw(pid_t pid)
    882  1.247   thorpej {
    883  1.252        ad 
    884  1.252        ad 	return proc_find_raw_pid_table_locked(pid, false);
    885  1.247   thorpej }
    886  1.247   thorpej 
    887  1.247   thorpej static proc_t *
    888  1.252        ad proc_find_internal(pid_t pid, bool any_lwpid)
    889  1.167     rmind {
    890  1.167     rmind 	proc_t *p;
    891  1.100        ad 
    892  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
    893  1.247   thorpej 
    894  1.251   thorpej 	p = proc_find_raw_pid_table_locked(pid, any_lwpid);
    895  1.167     rmind 	if (__predict_false(p == NULL)) {
    896  1.167     rmind 		return NULL;
    897  1.167     rmind 	}
    898  1.168       chs 
    899  1.167     rmind 	/*
    900  1.167     rmind 	 * Only allow live processes to be found by PID.
    901  1.247   thorpej 	 * XXX: p_stat might change, since proc unlocked.
    902  1.167     rmind 	 */
    903  1.167     rmind 	if (__predict_true(p->p_stat == SACTIVE || p->p_stat == SSTOP)) {
    904   1.68       dsl 		return p;
    905   1.68       dsl 	}
    906   1.68       dsl 	return NULL;
    907    1.1       cgd }
    908    1.1       cgd 
    909  1.251   thorpej proc_t *
    910  1.251   thorpej proc_find(pid_t pid)
    911  1.251   thorpej {
    912  1.251   thorpej 	return proc_find_internal(pid, false);
    913  1.251   thorpej }
    914  1.251   thorpej 
    915  1.251   thorpej proc_t *
    916  1.251   thorpej proc_find_lwpid(pid_t pid)
    917  1.251   thorpej {
    918  1.251   thorpej 	return proc_find_internal(pid, true);
    919  1.251   thorpej }
    920  1.251   thorpej 
    921    1.1       cgd /*
    922  1.252        ad  * pgrp_find: locate a process group by the ID.
    923  1.167     rmind  *
    924  1.252        ad  * => Must be called with proc_lock held.
    925    1.1       cgd  */
    926  1.252        ad struct pgrp *
    927  1.252        ad pgrp_find(pid_t pgid)
    928    1.1       cgd {
    929   1.68       dsl 	struct pgrp *pg;
    930    1.1       cgd 
    931  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
    932  1.167     rmind 
    933   1.68       dsl 	pg = pid_table[pgid & pid_tbl_mask].pt_pgrp;
    934  1.168       chs 
    935   1.61       dsl 	/*
    936  1.167     rmind 	 * Cannot look up a process group that only exists because the
    937  1.167     rmind 	 * session has not died yet (traditional).
    938   1.61       dsl 	 */
    939   1.68       dsl 	if (pg == NULL || pg->pg_id != pgid || LIST_EMPTY(&pg->pg_members)) {
    940   1.68       dsl 		return NULL;
    941   1.68       dsl 	}
    942   1.68       dsl 	return pg;
    943    1.1       cgd }
    944    1.1       cgd 
    945   1.61       dsl static void
    946   1.61       dsl expand_pid_table(void)
    947    1.1       cgd {
    948  1.150     rmind 	size_t pt_size, tsz;
    949   1.61       dsl 	struct pid_table *n_pt, *new_pt;
    950  1.247   thorpej 	uintptr_t slot;
    951   1.61       dsl 	struct pgrp *pgrp;
    952  1.168       chs 	pid_t pid, rpid;
    953  1.150     rmind 	u_int i;
    954  1.168       chs 	uint new_pt_mask;
    955    1.1       cgd 
    956  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
    957  1.247   thorpej 
    958  1.247   thorpej 	/* Unlock the pid_table briefly to allocate memory. */
    959  1.249   thorpej 	pt_size = pid_tbl_mask + 1;
    960  1.253        ad 	mutex_exit(&proc_lock);
    961  1.247   thorpej 
    962  1.150     rmind 	tsz = pt_size * 2 * sizeof(struct pid_table);
    963  1.150     rmind 	new_pt = kmem_alloc(tsz, KM_SLEEP);
    964  1.168       chs 	new_pt_mask = pt_size * 2 - 1;
    965   1.61       dsl 
    966  1.250   thorpej 	/* XXX For now.  The pratical limit is much lower anyway. */
    967  1.250   thorpej 	KASSERT(new_pt_mask <= FUTEX_TID_MASK);
    968  1.250   thorpej 
    969  1.253        ad 	mutex_enter(&proc_lock);
    970   1.61       dsl 	if (pt_size != pid_tbl_mask + 1) {
    971   1.61       dsl 		/* Another process beat us to it... */
    972  1.253        ad 		mutex_exit(&proc_lock);
    973  1.150     rmind 		kmem_free(new_pt, tsz);
    974  1.247   thorpej 		goto out;
    975   1.61       dsl 	}
    976   1.72  junyoung 
    977   1.61       dsl 	/*
    978   1.61       dsl 	 * Copy entries from old table into new one.
    979   1.61       dsl 	 * If 'pid' is 'odd' we need to place in the upper half,
    980   1.61       dsl 	 * even pid's to the lower half.
    981   1.61       dsl 	 * Free items stay in the low half so we don't have to
    982   1.61       dsl 	 * fixup the reference to them.
    983   1.61       dsl 	 * We stuff free items on the front of the freelist
    984   1.61       dsl 	 * because we can't write to unmodified entries.
    985   1.74  junyoung 	 * Processing the table backwards maintains a semblance
    986  1.168       chs 	 * of issuing pid numbers that increase with time.
    987   1.61       dsl 	 */
    988   1.61       dsl 	i = pt_size - 1;
    989   1.61       dsl 	n_pt = new_pt + i;
    990   1.61       dsl 	for (; ; i--, n_pt--) {
    991  1.247   thorpej 		slot = pid_table[i].pt_slot;
    992   1.61       dsl 		pgrp = pid_table[i].pt_pgrp;
    993  1.247   thorpej 		if (!PT_VALID(slot)) {
    994   1.61       dsl 			/* Up 'use count' so that link is valid */
    995  1.247   thorpej 			pid = (PT_NEXT(slot) + pt_size) & ~pt_size;
    996  1.168       chs 			rpid = 0;
    997  1.247   thorpej 			slot = PT_SET_FREE(pid);
    998   1.61       dsl 			if (pgrp)
    999   1.61       dsl 				pid = pgrp->pg_id;
   1000  1.168       chs 		} else {
   1001  1.168       chs 			pid = pid_table[i].pt_pid;
   1002  1.168       chs 			rpid = pid;
   1003  1.168       chs 		}
   1004   1.72  junyoung 
   1005   1.61       dsl 		/* Save entry in appropriate half of table */
   1006  1.247   thorpej 		n_pt[pid & pt_size].pt_slot = slot;
   1007   1.61       dsl 		n_pt[pid & pt_size].pt_pgrp = pgrp;
   1008  1.168       chs 		n_pt[pid & pt_size].pt_pid = rpid;
   1009   1.61       dsl 
   1010   1.61       dsl 		/* Put other piece on start of free list */
   1011   1.61       dsl 		pid = (pid ^ pt_size) & ~pid_tbl_mask;
   1012  1.247   thorpej 		n_pt[pid & pt_size].pt_slot =
   1013  1.247   thorpej 			PT_SET_FREE((pid & ~pt_size) | next_free_pt);
   1014   1.61       dsl 		n_pt[pid & pt_size].pt_pgrp = 0;
   1015  1.168       chs 		n_pt[pid & pt_size].pt_pid = 0;
   1016  1.168       chs 
   1017   1.61       dsl 		next_free_pt = i | (pid & pt_size);
   1018   1.61       dsl 		if (i == 0)
   1019   1.61       dsl 			break;
   1020   1.61       dsl 	}
   1021   1.61       dsl 
   1022  1.150     rmind 	/* Save old table size and switch tables */
   1023  1.150     rmind 	tsz = pt_size * sizeof(struct pid_table);
   1024   1.61       dsl 	n_pt = pid_table;
   1025  1.264  riastrad 	atomic_store_release(&pid_table, new_pt);
   1026  1.265  riastrad 	KASSERT(new_pt_mask >= pid_tbl_mask);
   1027  1.265  riastrad 	atomic_store_release(&pid_tbl_mask, new_pt_mask);
   1028   1.61       dsl 
   1029   1.61       dsl 	/*
   1030   1.61       dsl 	 * pid_max starts as PID_MAX (= 30000), once we have 16384
   1031   1.61       dsl 	 * allocated pids we need it to be larger!
   1032   1.61       dsl 	 */
   1033   1.61       dsl 	if (pid_tbl_mask > PID_MAX) {
   1034   1.61       dsl 		pid_max = pid_tbl_mask * 2 + 1;
   1035   1.61       dsl 		pid_alloc_lim |= pid_alloc_lim << 1;
   1036   1.61       dsl 	} else
   1037   1.61       dsl 		pid_alloc_lim <<= 1;	/* doubles number of free slots... */
   1038   1.61       dsl 
   1039  1.253        ad 	mutex_exit(&proc_lock);
   1040  1.252        ad 
   1041  1.252        ad 	/*
   1042  1.252        ad 	 * Make sure that unlocked access to the old pid_table is complete
   1043  1.252        ad 	 * and then free it.
   1044  1.252        ad 	 */
   1045  1.252        ad 	pserialize_perform(proc_psz);
   1046  1.150     rmind 	kmem_free(n_pt, tsz);
   1047  1.247   thorpej 
   1048  1.252        ad  out:	/* Return with proc_lock held again. */
   1049  1.253        ad 	mutex_enter(&proc_lock);
   1050   1.61       dsl }
   1051   1.61       dsl 
   1052   1.61       dsl struct proc *
   1053   1.61       dsl proc_alloc(void)
   1054   1.61       dsl {
   1055   1.61       dsl 	struct proc *p;
   1056   1.61       dsl 
   1057  1.128        ad 	p = pool_cache_get(proc_cache, PR_WAITOK);
   1058   1.61       dsl 	p->p_stat = SIDL;			/* protect against others */
   1059   1.96  christos 	proc_initspecific(p);
   1060  1.164     rmind 	kdtrace_proc_ctor(NULL, p);
   1061  1.247   thorpej 
   1062  1.247   thorpej 	/*
   1063  1.247   thorpej 	 * Allocate a placeholder in the pid_table.  When we create the
   1064  1.247   thorpej 	 * first LWP for this process, it will take ownership of the
   1065  1.247   thorpej 	 * slot.
   1066  1.247   thorpej 	 */
   1067  1.247   thorpej 	if (__predict_false(proc_alloc_pid(p) == -1)) {
   1068  1.247   thorpej 		/* Allocating the PID failed; unwind. */
   1069  1.247   thorpej 		proc_finispecific(p);
   1070  1.247   thorpej 		proc_free_mem(p);
   1071  1.247   thorpej 		p = NULL;
   1072  1.247   thorpej 	}
   1073  1.168       chs 	return p;
   1074  1.168       chs }
   1075  1.168       chs 
   1076  1.183      yamt /*
   1077  1.247   thorpej  * proc_alloc_pid_slot: allocate PID and record the occcupant so that
   1078  1.183      yamt  * proc_find_raw() can find it by the PID.
   1079  1.183      yamt  */
   1080  1.247   thorpej static pid_t __noinline
   1081  1.247   thorpej proc_alloc_pid_slot(struct proc *p, uintptr_t slot)
   1082  1.168       chs {
   1083  1.168       chs 	struct pid_table *pt;
   1084  1.168       chs 	pid_t pid;
   1085  1.168       chs 	int nxt;
   1086   1.61       dsl 
   1087  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
   1088  1.247   thorpej 
   1089   1.61       dsl 	for (;;expand_pid_table()) {
   1090  1.247   thorpej 		if (__predict_false(pid_alloc_cnt >= pid_alloc_lim)) {
   1091   1.61       dsl 			/* ensure pids cycle through 2000+ values */
   1092   1.61       dsl 			continue;
   1093  1.247   thorpej 		}
   1094  1.247   thorpej 		/*
   1095  1.247   thorpej 		 * The first user process *must* be given PID 1.
   1096  1.247   thorpej 		 * it has already been reserved for us.  This
   1097  1.247   thorpej 		 * will be coming in from the proc_alloc() call
   1098  1.247   thorpej 		 * above, and the entry will be usurped later when
   1099  1.247   thorpej 		 * the first user LWP is created.
   1100  1.247   thorpej 		 * XXX this is slightly gross.
   1101  1.247   thorpej 		 */
   1102  1.247   thorpej 		if (__predict_false(PT_RESERVED(pid_table[1].pt_slot) &&
   1103  1.247   thorpej 				    p != &proc0)) {
   1104  1.247   thorpej 			KASSERT(PT_IS_PROC(slot));
   1105  1.247   thorpej 			pt = &pid_table[1];
   1106  1.247   thorpej 			pt->pt_slot = slot;
   1107  1.247   thorpej 			return 1;
   1108  1.247   thorpej 		}
   1109   1.61       dsl 		pt = &pid_table[next_free_pt];
   1110    1.1       cgd #ifdef DIAGNOSTIC
   1111  1.247   thorpej 		if (__predict_false(PT_VALID(pt->pt_slot) || pt->pt_pgrp))
   1112   1.61       dsl 			panic("proc_alloc: slot busy");
   1113    1.1       cgd #endif
   1114  1.247   thorpej 		nxt = PT_NEXT(pt->pt_slot);
   1115   1.61       dsl 		if (nxt & pid_tbl_mask)
   1116   1.61       dsl 			break;
   1117   1.61       dsl 		/* Table full - expand (NB last entry not used....) */
   1118   1.61       dsl 	}
   1119   1.61       dsl 
   1120   1.61       dsl 	/* pid is 'saved use count' + 'size' + entry */
   1121   1.61       dsl 	pid = (nxt & ~pid_tbl_mask) + pid_tbl_mask + 1 + next_free_pt;
   1122   1.61       dsl 	if ((uint)pid > (uint)pid_max)
   1123   1.61       dsl 		pid &= pid_tbl_mask;
   1124   1.61       dsl 	next_free_pt = nxt & pid_tbl_mask;
   1125   1.61       dsl 
   1126  1.250   thorpej 	/* XXX For now.  The pratical limit is much lower anyway. */
   1127  1.250   thorpej 	KASSERT(pid <= FUTEX_TID_MASK);
   1128  1.250   thorpej 
   1129   1.61       dsl 	/* Grab table slot */
   1130  1.247   thorpej 	pt->pt_slot = slot;
   1131  1.168       chs 
   1132  1.168       chs 	KASSERT(pt->pt_pid == 0);
   1133  1.168       chs 	pt->pt_pid = pid;
   1134  1.247   thorpej 	pid_alloc_cnt++;
   1135  1.247   thorpej 
   1136  1.247   thorpej 	return pid;
   1137  1.247   thorpej }
   1138  1.247   thorpej 
   1139  1.247   thorpej pid_t
   1140  1.247   thorpej proc_alloc_pid(struct proc *p)
   1141  1.247   thorpej {
   1142  1.247   thorpej 	pid_t pid;
   1143  1.247   thorpej 
   1144  1.247   thorpej 	KASSERT((((uintptr_t)p) & PT_F_ALLBITS) == 0);
   1145  1.252        ad 	KASSERT(p->p_stat == SIDL);
   1146  1.247   thorpej 
   1147  1.253        ad 	mutex_enter(&proc_lock);
   1148  1.247   thorpej 	pid = proc_alloc_pid_slot(p, PT_SET_PROC(p));
   1149  1.247   thorpej 	if (pid != -1)
   1150  1.168       chs 		p->p_pid = pid;
   1151  1.253        ad 	mutex_exit(&proc_lock);
   1152  1.247   thorpej 
   1153  1.247   thorpej 	return pid;
   1154  1.247   thorpej }
   1155  1.247   thorpej 
   1156  1.247   thorpej pid_t
   1157  1.247   thorpej proc_alloc_lwpid(struct proc *p, struct lwp *l)
   1158  1.247   thorpej {
   1159  1.247   thorpej 	struct pid_table *pt;
   1160  1.247   thorpej 	pid_t pid;
   1161  1.247   thorpej 
   1162  1.247   thorpej 	KASSERT((((uintptr_t)l) & PT_F_ALLBITS) == 0);
   1163  1.252        ad 	KASSERT(l->l_proc == p);
   1164  1.252        ad 	KASSERT(l->l_stat == LSIDL);
   1165  1.252        ad 
   1166  1.252        ad 	/*
   1167  1.252        ad 	 * For unlocked lookup in proc_find_lwp(), make sure l->l_proc
   1168  1.252        ad 	 * is globally visible before the LWP becomes visible via the
   1169  1.252        ad 	 * pid_table.
   1170  1.252        ad 	 */
   1171  1.252        ad #ifndef __HAVE_ATOMIC_AS_MEMBAR
   1172  1.252        ad 	membar_producer();
   1173  1.252        ad #endif
   1174  1.247   thorpej 
   1175  1.247   thorpej 	/*
   1176  1.247   thorpej 	 * If the slot for p->p_pid currently points to the proc,
   1177  1.247   thorpej 	 * then we should usurp this ID for the LWP.  This happens
   1178  1.247   thorpej 	 * at least once per process (for the first LWP), and can
   1179  1.247   thorpej 	 * happen again if the first LWP for a process exits and
   1180  1.247   thorpej 	 * before the process creates another.
   1181  1.247   thorpej 	 */
   1182  1.253        ad 	mutex_enter(&proc_lock);
   1183  1.247   thorpej 	pid = p->p_pid;
   1184  1.247   thorpej 	pt = &pid_table[pid & pid_tbl_mask];
   1185  1.247   thorpej 	KASSERT(pt->pt_pid == pid);
   1186  1.247   thorpej 	if (PT_IS_PROC(pt->pt_slot)) {
   1187  1.247   thorpej 		KASSERT(PT_GET_PROC(pt->pt_slot) == p);
   1188  1.247   thorpej 		l->l_lid = pid;
   1189  1.247   thorpej 		pt->pt_slot = PT_SET_LWP(l);
   1190  1.247   thorpej 	} else {
   1191  1.247   thorpej 		/* Need to allocate a new slot. */
   1192  1.247   thorpej 		pid = proc_alloc_pid_slot(p, PT_SET_LWP(l));
   1193  1.247   thorpej 		if (pid != -1)
   1194  1.247   thorpej 			l->l_lid = pid;
   1195  1.168       chs 	}
   1196  1.253        ad 	mutex_exit(&proc_lock);
   1197   1.61       dsl 
   1198  1.168       chs 	return pid;
   1199   1.61       dsl }
   1200   1.61       dsl 
   1201  1.247   thorpej static void __noinline
   1202  1.247   thorpej proc_free_pid_internal(pid_t pid, uintptr_t type __diagused)
   1203   1.61       dsl {
   1204   1.61       dsl 	struct pid_table *pt;
   1205   1.61       dsl 
   1206  1.265  riastrad 	KASSERT(mutex_owned(&proc_lock));
   1207  1.265  riastrad 
   1208  1.247   thorpej 	pt = &pid_table[pid & pid_tbl_mask];
   1209   1.61       dsl 
   1210  1.247   thorpej 	KASSERT(PT_GET_TYPE(pt->pt_slot) == type);
   1211  1.247   thorpej 	KASSERT(pt->pt_pid == pid);
   1212  1.168       chs 
   1213   1.61       dsl 	/* save pid use count in slot */
   1214  1.247   thorpej 	pt->pt_slot = PT_SET_FREE(pid & ~pid_tbl_mask);
   1215  1.168       chs 	pt->pt_pid = 0;
   1216   1.61       dsl 
   1217   1.61       dsl 	if (pt->pt_pgrp == NULL) {
   1218   1.61       dsl 		/* link last freed entry onto ours */
   1219   1.61       dsl 		pid &= pid_tbl_mask;
   1220   1.61       dsl 		pt = &pid_table[last_free_pt];
   1221  1.247   thorpej 		pt->pt_slot = PT_SET_FREE(PT_NEXT(pt->pt_slot) | pid);
   1222  1.168       chs 		pt->pt_pid = 0;
   1223   1.61       dsl 		last_free_pt = pid;
   1224   1.61       dsl 		pid_alloc_cnt--;
   1225   1.61       dsl 	}
   1226  1.247   thorpej }
   1227  1.247   thorpej 
   1228  1.247   thorpej /*
   1229  1.247   thorpej  * Free a process id - called from proc_free (in kern_exit.c)
   1230  1.247   thorpej  *
   1231  1.247   thorpej  * Called with the proc_lock held.
   1232  1.247   thorpej  */
   1233  1.247   thorpej void
   1234  1.247   thorpej proc_free_pid(pid_t pid)
   1235  1.247   thorpej {
   1236  1.253        ad 
   1237  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
   1238  1.247   thorpej 	proc_free_pid_internal(pid, PT_F_PROC);
   1239  1.247   thorpej }
   1240  1.247   thorpej 
   1241  1.247   thorpej /*
   1242  1.247   thorpej  * Free a process id used by an LWP.  If this was the process's
   1243  1.247   thorpej  * first LWP, we convert the slot to point to the process; the
   1244  1.247   thorpej  * entry will get cleaned up later when the process finishes exiting.
   1245  1.247   thorpej  *
   1246  1.247   thorpej  * If not, then it's the same as proc_free_pid().
   1247  1.247   thorpej  */
   1248  1.247   thorpej void
   1249  1.247   thorpej proc_free_lwpid(struct proc *p, pid_t pid)
   1250  1.247   thorpej {
   1251  1.247   thorpej 
   1252  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
   1253  1.247   thorpej 
   1254  1.247   thorpej 	if (__predict_true(p->p_pid == pid)) {
   1255  1.247   thorpej 		struct pid_table *pt;
   1256  1.247   thorpej 
   1257  1.247   thorpej 		pt = &pid_table[pid & pid_tbl_mask];
   1258  1.247   thorpej 
   1259  1.247   thorpej 		KASSERT(pt->pt_pid == pid);
   1260  1.247   thorpej 		KASSERT(PT_IS_LWP(pt->pt_slot));
   1261  1.247   thorpej 		KASSERT(PT_GET_LWP(pt->pt_slot)->l_proc == p);
   1262  1.247   thorpej 
   1263  1.247   thorpej 		pt->pt_slot = PT_SET_PROC(p);
   1264  1.247   thorpej 		return;
   1265  1.247   thorpej 	}
   1266  1.247   thorpej 	proc_free_pid_internal(pid, PT_F_LWP);
   1267   1.61       dsl }
   1268   1.61       dsl 
   1269  1.128        ad void
   1270  1.128        ad proc_free_mem(struct proc *p)
   1271  1.128        ad {
   1272  1.128        ad 
   1273  1.160    darran 	kdtrace_proc_dtor(NULL, p);
   1274  1.128        ad 	pool_cache_put(proc_cache, p);
   1275  1.128        ad }
   1276  1.128        ad 
   1277   1.61       dsl /*
   1278  1.151     rmind  * proc_enterpgrp: move p to a new or existing process group (and session).
   1279   1.61       dsl  *
   1280   1.61       dsl  * If we are creating a new pgrp, the pgid should equal
   1281   1.72  junyoung  * the calling process' pid.
   1282   1.61       dsl  * If is only valid to enter a process group that is in the session
   1283   1.61       dsl  * of the process.
   1284   1.61       dsl  * Also mksess should only be set if we are creating a process group
   1285   1.61       dsl  *
   1286  1.181    martin  * Only called from sys_setsid, sys_setpgid and posix_spawn/spawn_return.
   1287   1.61       dsl  */
   1288   1.61       dsl int
   1289  1.151     rmind proc_enterpgrp(struct proc *curp, pid_t pid, pid_t pgid, bool mksess)
   1290   1.61       dsl {
   1291   1.61       dsl 	struct pgrp *new_pgrp, *pgrp;
   1292   1.61       dsl 	struct session *sess;
   1293  1.100        ad 	struct proc *p;
   1294   1.61       dsl 	int rval;
   1295   1.61       dsl 	pid_t pg_id = NO_PGID;
   1296   1.61       dsl 
   1297  1.252        ad 	/* Allocate data areas we might need before doing any validity checks */
   1298  1.151     rmind 	sess = mksess ? kmem_alloc(sizeof(*sess), KM_SLEEP) : NULL;
   1299  1.252        ad 	new_pgrp = kmem_alloc(sizeof(*new_pgrp), KM_SLEEP);
   1300   1.61       dsl 
   1301  1.253        ad 	mutex_enter(&proc_lock);
   1302   1.61       dsl 	rval = EPERM;	/* most common error (to save typing) */
   1303   1.61       dsl 
   1304   1.61       dsl 	/* Check pgrp exists or can be created */
   1305   1.61       dsl 	pgrp = pid_table[pgid & pid_tbl_mask].pt_pgrp;
   1306   1.61       dsl 	if (pgrp != NULL && pgrp->pg_id != pgid)
   1307   1.61       dsl 		goto done;
   1308   1.61       dsl 
   1309   1.61       dsl 	/* Can only set another process under restricted circumstances. */
   1310  1.100        ad 	if (pid != curp->p_pid) {
   1311  1.167     rmind 		/* Must exist and be one of our children... */
   1312  1.252        ad 		p = proc_find_internal(pid, false);
   1313  1.167     rmind 		if (p == NULL || !p_inferior(p, curp)) {
   1314   1.61       dsl 			rval = ESRCH;
   1315   1.61       dsl 			goto done;
   1316   1.61       dsl 		}
   1317   1.61       dsl 		/* ... in the same session... */
   1318   1.61       dsl 		if (sess != NULL || p->p_session != curp->p_session)
   1319   1.61       dsl 			goto done;
   1320   1.61       dsl 		/* ... existing pgid must be in same session ... */
   1321   1.61       dsl 		if (pgrp != NULL && pgrp->pg_session != p->p_session)
   1322   1.61       dsl 			goto done;
   1323   1.61       dsl 		/* ... and not done an exec. */
   1324  1.102     pavel 		if (p->p_flag & PK_EXEC) {
   1325   1.61       dsl 			rval = EACCES;
   1326   1.61       dsl 			goto done;
   1327   1.49     enami 		}
   1328  1.100        ad 	} else {
   1329  1.100        ad 		/* ... setsid() cannot re-enter a pgrp */
   1330  1.100        ad 		if (mksess && (curp->p_pgid == curp->p_pid ||
   1331  1.252        ad 		    pgrp_find(curp->p_pid)))
   1332  1.100        ad 			goto done;
   1333  1.100        ad 		p = curp;
   1334   1.61       dsl 	}
   1335    1.1       cgd 
   1336   1.61       dsl 	/* Changing the process group/session of a session
   1337   1.61       dsl 	   leader is definitely off limits. */
   1338   1.61       dsl 	if (SESS_LEADER(p)) {
   1339   1.61       dsl 		if (sess == NULL && p->p_pgrp == pgrp)
   1340   1.61       dsl 			/* unless it's a definite noop */
   1341   1.61       dsl 			rval = 0;
   1342   1.61       dsl 		goto done;
   1343   1.61       dsl 	}
   1344   1.61       dsl 
   1345   1.61       dsl 	/* Can only create a process group with id of process */
   1346   1.61       dsl 	if (pgrp == NULL && pgid != pid)
   1347   1.61       dsl 		goto done;
   1348   1.61       dsl 
   1349   1.61       dsl 	/* Can only create a session if creating pgrp */
   1350   1.61       dsl 	if (sess != NULL && pgrp != NULL)
   1351   1.61       dsl 		goto done;
   1352   1.61       dsl 
   1353   1.61       dsl 	/* Check we allocated memory for a pgrp... */
   1354   1.61       dsl 	if (pgrp == NULL && new_pgrp == NULL)
   1355   1.61       dsl 		goto done;
   1356   1.61       dsl 
   1357   1.61       dsl 	/* Don't attach to 'zombie' pgrp */
   1358   1.61       dsl 	if (pgrp != NULL && LIST_EMPTY(&pgrp->pg_members))
   1359   1.61       dsl 		goto done;
   1360   1.61       dsl 
   1361   1.61       dsl 	/* Expect to succeed now */
   1362   1.61       dsl 	rval = 0;
   1363   1.61       dsl 
   1364   1.61       dsl 	if (pgrp == p->p_pgrp)
   1365   1.61       dsl 		/* nothing to do */
   1366   1.61       dsl 		goto done;
   1367   1.61       dsl 
   1368   1.61       dsl 	/* Ok all setup, link up required structures */
   1369  1.100        ad 
   1370   1.61       dsl 	if (pgrp == NULL) {
   1371   1.61       dsl 		pgrp = new_pgrp;
   1372  1.141      yamt 		new_pgrp = NULL;
   1373   1.61       dsl 		if (sess != NULL) {
   1374   1.21   thorpej 			sess->s_sid = p->p_pid;
   1375    1.1       cgd 			sess->s_leader = p;
   1376    1.1       cgd 			sess->s_count = 1;
   1377    1.1       cgd 			sess->s_ttyvp = NULL;
   1378    1.1       cgd 			sess->s_ttyp = NULL;
   1379   1.58       dsl 			sess->s_flags = p->p_session->s_flags & ~S_LOGIN_SET;
   1380   1.25     perry 			memcpy(sess->s_login, p->p_session->s_login,
   1381    1.1       cgd 			    sizeof(sess->s_login));
   1382  1.100        ad 			p->p_lflag &= ~PL_CONTROLT;
   1383    1.1       cgd 		} else {
   1384   1.61       dsl 			sess = p->p_pgrp->pg_session;
   1385  1.151     rmind 			proc_sesshold(sess);
   1386    1.1       cgd 		}
   1387   1.61       dsl 		pgrp->pg_session = sess;
   1388  1.141      yamt 		sess = NULL;
   1389   1.61       dsl 
   1390    1.1       cgd 		pgrp->pg_id = pgid;
   1391   1.10   mycroft 		LIST_INIT(&pgrp->pg_members);
   1392   1.61       dsl #ifdef DIAGNOSTIC
   1393   1.63  christos 		if (__predict_false(pid_table[pgid & pid_tbl_mask].pt_pgrp))
   1394   1.61       dsl 			panic("enterpgrp: pgrp table slot in use");
   1395   1.63  christos 		if (__predict_false(mksess && p != curp))
   1396   1.63  christos 			panic("enterpgrp: mksession and p != curproc");
   1397   1.61       dsl #endif
   1398   1.61       dsl 		pid_table[pgid & pid_tbl_mask].pt_pgrp = pgrp;
   1399    1.1       cgd 		pgrp->pg_jobc = 0;
   1400  1.136        ad 	}
   1401    1.1       cgd 
   1402    1.1       cgd 	/*
   1403    1.1       cgd 	 * Adjust eligibility of affected pgrps to participate in job control.
   1404    1.1       cgd 	 * Increment eligibility counts before decrementing, otherwise we
   1405    1.1       cgd 	 * could reach 0 spuriously during the first call.
   1406    1.1       cgd 	 */
   1407    1.1       cgd 	fixjobc(p, pgrp, 1);
   1408    1.1       cgd 	fixjobc(p, p->p_pgrp, 0);
   1409    1.1       cgd 
   1410  1.139        ad 	/* Interlock with ttread(). */
   1411  1.139        ad 	mutex_spin_enter(&tty_lock);
   1412  1.139        ad 
   1413  1.100        ad 	/* Move process to requested group. */
   1414   1.10   mycroft 	LIST_REMOVE(p, p_pglist);
   1415   1.52      matt 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
   1416   1.61       dsl 		/* defer delete until we've dumped the lock */
   1417   1.61       dsl 		pg_id = p->p_pgrp->pg_id;
   1418    1.1       cgd 	p->p_pgrp = pgrp;
   1419   1.10   mycroft 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
   1420  1.100        ad 
   1421  1.100        ad 	/* Done with the swap; we can release the tty mutex. */
   1422  1.128        ad 	mutex_spin_exit(&tty_lock);
   1423  1.128        ad 
   1424   1.61       dsl     done:
   1425  1.151     rmind 	if (pg_id != NO_PGID) {
   1426  1.151     rmind 		/* Releases proc_lock. */
   1427  1.100        ad 		pg_delete(pg_id);
   1428  1.151     rmind 	} else {
   1429  1.253        ad 		mutex_exit(&proc_lock);
   1430  1.151     rmind 	}
   1431   1.61       dsl 	if (sess != NULL)
   1432  1.131        ad 		kmem_free(sess, sizeof(*sess));
   1433   1.61       dsl 	if (new_pgrp != NULL)
   1434  1.131        ad 		kmem_free(new_pgrp, sizeof(*new_pgrp));
   1435   1.63  christos #ifdef DEBUG_PGRP
   1436   1.63  christos 	if (__predict_false(rval))
   1437   1.61       dsl 		printf("enterpgrp(%d,%d,%d), curproc %d, rval %d\n",
   1438   1.61       dsl 			pid, pgid, mksess, curp->p_pid, rval);
   1439   1.61       dsl #endif
   1440   1.61       dsl 	return rval;
   1441    1.1       cgd }
   1442    1.1       cgd 
   1443    1.1       cgd /*
   1444  1.151     rmind  * proc_leavepgrp: remove a process from its process group.
   1445  1.151     rmind  *  => must be called with the proc_lock held, which will be released;
   1446    1.1       cgd  */
   1447  1.100        ad void
   1448  1.151     rmind proc_leavepgrp(struct proc *p)
   1449    1.1       cgd {
   1450   1.61       dsl 	struct pgrp *pgrp;
   1451    1.1       cgd 
   1452  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
   1453  1.100        ad 
   1454  1.139        ad 	/* Interlock with ttread() */
   1455  1.128        ad 	mutex_spin_enter(&tty_lock);
   1456   1.61       dsl 	pgrp = p->p_pgrp;
   1457   1.10   mycroft 	LIST_REMOVE(p, p_pglist);
   1458   1.94        ad 	p->p_pgrp = NULL;
   1459  1.128        ad 	mutex_spin_exit(&tty_lock);
   1460  1.100        ad 
   1461  1.151     rmind 	if (LIST_EMPTY(&pgrp->pg_members)) {
   1462  1.151     rmind 		/* Releases proc_lock. */
   1463  1.100        ad 		pg_delete(pgrp->pg_id);
   1464  1.151     rmind 	} else {
   1465  1.253        ad 		mutex_exit(&proc_lock);
   1466  1.151     rmind 	}
   1467   1.61       dsl }
   1468   1.61       dsl 
   1469  1.100        ad /*
   1470  1.151     rmind  * pg_remove: remove a process group from the table.
   1471  1.151     rmind  *  => must be called with the proc_lock held;
   1472  1.151     rmind  *  => returns process group to free;
   1473  1.100        ad  */
   1474  1.151     rmind static struct pgrp *
   1475  1.151     rmind pg_remove(pid_t pg_id)
   1476   1.61       dsl {
   1477   1.61       dsl 	struct pgrp *pgrp;
   1478   1.61       dsl 	struct pid_table *pt;
   1479   1.61       dsl 
   1480  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
   1481  1.100        ad 
   1482   1.61       dsl 	pt = &pid_table[pg_id & pid_tbl_mask];
   1483   1.61       dsl 	pgrp = pt->pt_pgrp;
   1484  1.151     rmind 
   1485  1.151     rmind 	KASSERT(pgrp != NULL);
   1486  1.151     rmind 	KASSERT(pgrp->pg_id == pg_id);
   1487  1.151     rmind 	KASSERT(LIST_EMPTY(&pgrp->pg_members));
   1488  1.151     rmind 
   1489  1.151     rmind 	pt->pt_pgrp = NULL;
   1490   1.61       dsl 
   1491  1.247   thorpej 	if (!PT_VALID(pt->pt_slot)) {
   1492  1.151     rmind 		/* Orphaned pgrp, put slot onto free list. */
   1493  1.247   thorpej 		KASSERT((PT_NEXT(pt->pt_slot) & pid_tbl_mask) == 0);
   1494   1.61       dsl 		pg_id &= pid_tbl_mask;
   1495   1.61       dsl 		pt = &pid_table[last_free_pt];
   1496  1.247   thorpej 		pt->pt_slot = PT_SET_FREE(PT_NEXT(pt->pt_slot) | pg_id);
   1497  1.168       chs 		KASSERT(pt->pt_pid == 0);
   1498   1.61       dsl 		last_free_pt = pg_id;
   1499   1.61       dsl 		pid_alloc_cnt--;
   1500   1.61       dsl 	}
   1501  1.151     rmind 	return pgrp;
   1502    1.1       cgd }
   1503    1.1       cgd 
   1504    1.1       cgd /*
   1505  1.151     rmind  * pg_delete: delete and free a process group.
   1506  1.151     rmind  *  => must be called with the proc_lock held, which will be released.
   1507    1.1       cgd  */
   1508   1.61       dsl static void
   1509   1.61       dsl pg_delete(pid_t pg_id)
   1510   1.61       dsl {
   1511  1.151     rmind 	struct pgrp *pg;
   1512   1.61       dsl 	struct tty *ttyp;
   1513   1.61       dsl 	struct session *ss;
   1514  1.100        ad 
   1515  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
   1516   1.61       dsl 
   1517  1.151     rmind 	pg = pid_table[pg_id & pid_tbl_mask].pt_pgrp;
   1518  1.151     rmind 	if (pg == NULL || pg->pg_id != pg_id || !LIST_EMPTY(&pg->pg_members)) {
   1519  1.253        ad 		mutex_exit(&proc_lock);
   1520   1.61       dsl 		return;
   1521  1.151     rmind 	}
   1522   1.61       dsl 
   1523  1.151     rmind 	ss = pg->pg_session;
   1524   1.71        pk 
   1525   1.61       dsl 	/* Remove reference (if any) from tty to this process group */
   1526  1.128        ad 	mutex_spin_enter(&tty_lock);
   1527   1.71        pk 	ttyp = ss->s_ttyp;
   1528  1.151     rmind 	if (ttyp != NULL && ttyp->t_pgrp == pg) {
   1529   1.61       dsl 		ttyp->t_pgrp = NULL;
   1530  1.151     rmind 		KASSERT(ttyp->t_session == ss);
   1531   1.71        pk 	}
   1532  1.128        ad 	mutex_spin_exit(&tty_lock);
   1533   1.61       dsl 
   1534   1.71        pk 	/*
   1535  1.252        ad 	 * The leading process group in a session is freed by proc_sessrele(),
   1536  1.252        ad 	 * if last reference.  It will also release the locks.
   1537   1.71        pk 	 */
   1538  1.151     rmind 	pg = (ss->s_sid != pg->pg_id) ? pg_remove(pg_id) : NULL;
   1539  1.252        ad 	proc_sessrele(ss);
   1540   1.61       dsl 
   1541  1.151     rmind 	if (pg != NULL) {
   1542  1.247   thorpej 		/* Free it, if was not done above. */
   1543  1.151     rmind 		kmem_free(pg, sizeof(struct pgrp));
   1544  1.151     rmind 	}
   1545    1.1       cgd }
   1546    1.1       cgd 
   1547    1.1       cgd /*
   1548    1.1       cgd  * Adjust pgrp jobc counters when specified process changes process group.
   1549    1.1       cgd  * We count the number of processes in each process group that "qualify"
   1550    1.1       cgd  * the group for terminal job control (those with a parent in a different
   1551    1.1       cgd  * process group of the same session).  If that count reaches zero, the
   1552    1.1       cgd  * process group becomes orphaned.  Check both the specified process'
   1553    1.1       cgd  * process group and that of its children.
   1554    1.1       cgd  * entering == 0 => p is leaving specified group.
   1555    1.1       cgd  * entering == 1 => p is entering specified group.
   1556   1.68       dsl  *
   1557  1.136        ad  * Call with proc_lock held.
   1558    1.1       cgd  */
   1559    1.4    andrew void
   1560   1.59       dsl fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
   1561    1.1       cgd {
   1562   1.39  augustss 	struct pgrp *hispgrp;
   1563   1.39  augustss 	struct session *mysession = pgrp->pg_session;
   1564   1.68       dsl 	struct proc *child;
   1565    1.1       cgd 
   1566  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
   1567  1.100        ad 
   1568    1.1       cgd 	/*
   1569    1.1       cgd 	 * Check p's parent to see whether p qualifies its own process
   1570    1.1       cgd 	 * group; if so, adjust count for p's process group.
   1571    1.1       cgd 	 */
   1572   1.68       dsl 	hispgrp = p->p_pptr->p_pgrp;
   1573   1.68       dsl 	if (hispgrp != pgrp && hispgrp->pg_session == mysession) {
   1574  1.100        ad 		if (entering) {
   1575    1.1       cgd 			pgrp->pg_jobc++;
   1576  1.136        ad 			p->p_lflag &= ~PL_ORPHANPG;
   1577  1.245      maxv 		} else {
   1578  1.261    martin 			/* KASSERT(pgrp->pg_jobc > 0); */
   1579  1.245      maxv 			if (--pgrp->pg_jobc == 0)
   1580  1.245      maxv 				orphanpg(pgrp);
   1581  1.245      maxv 		}
   1582   1.26   thorpej 	}
   1583    1.1       cgd 
   1584    1.1       cgd 	/*
   1585    1.1       cgd 	 * Check this process' children to see whether they qualify
   1586    1.1       cgd 	 * their process groups; if so, adjust counts for children's
   1587    1.1       cgd 	 * process groups.
   1588    1.1       cgd 	 */
   1589   1.68       dsl 	LIST_FOREACH(child, &p->p_children, p_sibling) {
   1590   1.68       dsl 		hispgrp = child->p_pgrp;
   1591   1.68       dsl 		if (hispgrp != pgrp && hispgrp->pg_session == mysession &&
   1592   1.68       dsl 		    !P_ZOMBIE(child)) {
   1593  1.100        ad 			if (entering) {
   1594  1.136        ad 				child->p_lflag &= ~PL_ORPHANPG;
   1595    1.1       cgd 				hispgrp->pg_jobc++;
   1596  1.245      maxv 			} else {
   1597  1.245      maxv 				KASSERT(hispgrp->pg_jobc > 0);
   1598  1.245      maxv 				if (--hispgrp->pg_jobc == 0)
   1599  1.245      maxv 					orphanpg(hispgrp);
   1600  1.245      maxv 			}
   1601   1.26   thorpej 		}
   1602   1.26   thorpej 	}
   1603    1.1       cgd }
   1604    1.1       cgd 
   1605   1.72  junyoung /*
   1606    1.1       cgd  * A process group has become orphaned;
   1607    1.1       cgd  * if there are any stopped processes in the group,
   1608    1.1       cgd  * hang-up all process in that group.
   1609   1.68       dsl  *
   1610  1.136        ad  * Call with proc_lock held.
   1611    1.1       cgd  */
   1612    1.4    andrew static void
   1613   1.59       dsl orphanpg(struct pgrp *pg)
   1614    1.1       cgd {
   1615   1.39  augustss 	struct proc *p;
   1616  1.100        ad 
   1617  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
   1618  1.100        ad 
   1619   1.52      matt 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
   1620    1.1       cgd 		if (p->p_stat == SSTOP) {
   1621  1.136        ad 			p->p_lflag |= PL_ORPHANPG;
   1622  1.100        ad 			psignal(p, SIGHUP);
   1623  1.100        ad 			psignal(p, SIGCONT);
   1624   1.35    bouyer 		}
   1625   1.35    bouyer 	}
   1626   1.35    bouyer }
   1627    1.1       cgd 
   1628   1.61       dsl #ifdef DDB
   1629   1.61       dsl #include <ddb/db_output.h>
   1630   1.61       dsl void pidtbl_dump(void);
   1631   1.14  christos void
   1632   1.61       dsl pidtbl_dump(void)
   1633    1.1       cgd {
   1634   1.61       dsl 	struct pid_table *pt;
   1635   1.61       dsl 	struct proc *p;
   1636   1.39  augustss 	struct pgrp *pgrp;
   1637  1.247   thorpej 	uintptr_t slot;
   1638   1.61       dsl 	int id;
   1639    1.1       cgd 
   1640   1.61       dsl 	db_printf("pid table %p size %x, next %x, last %x\n",
   1641   1.61       dsl 		pid_table, pid_tbl_mask+1,
   1642   1.61       dsl 		next_free_pt, last_free_pt);
   1643   1.61       dsl 	for (pt = pid_table, id = 0; id <= pid_tbl_mask; id++, pt++) {
   1644  1.247   thorpej 		slot = pt->pt_slot;
   1645  1.247   thorpej 		if (!PT_VALID(slot) && !pt->pt_pgrp)
   1646   1.61       dsl 			continue;
   1647  1.247   thorpej 		if (PT_IS_LWP(slot)) {
   1648  1.247   thorpej 			p = PT_GET_LWP(slot)->l_proc;
   1649  1.247   thorpej 		} else if (PT_IS_PROC(slot)) {
   1650  1.247   thorpej 			p = PT_GET_PROC(slot);
   1651  1.247   thorpej 		} else {
   1652  1.247   thorpej 			p = NULL;
   1653  1.247   thorpej 		}
   1654   1.61       dsl 		db_printf("  id %x: ", id);
   1655  1.247   thorpej 		if (p != NULL)
   1656  1.168       chs 			db_printf("slotpid %d proc %p id %d (0x%x) %s\n",
   1657  1.168       chs 				pt->pt_pid, p, p->p_pid, p->p_pid, p->p_comm);
   1658   1.61       dsl 		else
   1659   1.61       dsl 			db_printf("next %x use %x\n",
   1660  1.247   thorpej 				PT_NEXT(slot) & pid_tbl_mask,
   1661  1.247   thorpej 				PT_NEXT(slot) & ~pid_tbl_mask);
   1662   1.61       dsl 		if ((pgrp = pt->pt_pgrp)) {
   1663   1.61       dsl 			db_printf("\tsession %p, sid %d, count %d, login %s\n",
   1664   1.61       dsl 			    pgrp->pg_session, pgrp->pg_session->s_sid,
   1665   1.61       dsl 			    pgrp->pg_session->s_count,
   1666   1.61       dsl 			    pgrp->pg_session->s_login);
   1667   1.61       dsl 			db_printf("\tpgrp %p, pg_id %d, pg_jobc %d, members %p\n",
   1668   1.61       dsl 			    pgrp, pgrp->pg_id, pgrp->pg_jobc,
   1669  1.135      yamt 			    LIST_FIRST(&pgrp->pg_members));
   1670  1.135      yamt 			LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
   1671   1.72  junyoung 				db_printf("\t\tpid %d addr %p pgrp %p %s\n",
   1672   1.61       dsl 				    p->p_pid, p, p->p_pgrp, p->p_comm);
   1673   1.10   mycroft 			}
   1674    1.1       cgd 		}
   1675    1.1       cgd 	}
   1676    1.1       cgd }
   1677   1.61       dsl #endif /* DDB */
   1678   1.48      yamt 
   1679   1.48      yamt #ifdef KSTACK_CHECK_MAGIC
   1680   1.48      yamt 
   1681   1.48      yamt #define	KSTACK_MAGIC	0xdeadbeaf
   1682   1.48      yamt 
   1683   1.48      yamt /* XXX should be per process basis? */
   1684  1.149     rmind static int	kstackleftmin = KSTACK_SIZE;
   1685  1.149     rmind static int	kstackleftthres = KSTACK_SIZE / 8;
   1686   1.48      yamt 
   1687   1.48      yamt void
   1688   1.56      yamt kstack_setup_magic(const struct lwp *l)
   1689   1.48      yamt {
   1690   1.85     perry 	uint32_t *ip;
   1691   1.85     perry 	uint32_t const *end;
   1692   1.48      yamt 
   1693   1.56      yamt 	KASSERT(l != NULL);
   1694   1.56      yamt 	KASSERT(l != &lwp0);
   1695   1.48      yamt 
   1696   1.48      yamt 	/*
   1697   1.48      yamt 	 * fill all the stack with magic number
   1698   1.48      yamt 	 * so that later modification on it can be detected.
   1699   1.48      yamt 	 */
   1700   1.85     perry 	ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1701  1.114    dyoung 	end = (uint32_t *)((char *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1702   1.48      yamt 	for (; ip < end; ip++) {
   1703   1.48      yamt 		*ip = KSTACK_MAGIC;
   1704   1.48      yamt 	}
   1705   1.48      yamt }
   1706   1.48      yamt 
   1707   1.48      yamt void
   1708   1.56      yamt kstack_check_magic(const struct lwp *l)
   1709   1.48      yamt {
   1710   1.85     perry 	uint32_t const *ip, *end;
   1711   1.48      yamt 	int stackleft;
   1712   1.48      yamt 
   1713   1.56      yamt 	KASSERT(l != NULL);
   1714   1.48      yamt 
   1715   1.48      yamt 	/* don't check proc0 */ /*XXX*/
   1716   1.56      yamt 	if (l == &lwp0)
   1717   1.48      yamt 		return;
   1718   1.48      yamt 
   1719   1.48      yamt #ifdef __MACHINE_STACK_GROWS_UP
   1720   1.48      yamt 	/* stack grows upwards (eg. hppa) */
   1721  1.106  christos 	ip = (uint32_t *)((void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1722   1.85     perry 	end = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1723   1.48      yamt 	for (ip--; ip >= end; ip--)
   1724   1.48      yamt 		if (*ip != KSTACK_MAGIC)
   1725   1.48      yamt 			break;
   1726   1.72  junyoung 
   1727  1.106  christos 	stackleft = (void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (void *)ip;
   1728   1.48      yamt #else /* __MACHINE_STACK_GROWS_UP */
   1729   1.48      yamt 	/* stack grows downwards (eg. i386) */
   1730   1.85     perry 	ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1731  1.114    dyoung 	end = (uint32_t *)((char *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1732   1.48      yamt 	for (; ip < end; ip++)
   1733   1.48      yamt 		if (*ip != KSTACK_MAGIC)
   1734   1.48      yamt 			break;
   1735   1.48      yamt 
   1736   1.93  christos 	stackleft = ((const char *)ip) - (const char *)KSTACK_LOWEST_ADDR(l);
   1737   1.48      yamt #endif /* __MACHINE_STACK_GROWS_UP */
   1738   1.48      yamt 
   1739   1.48      yamt 	if (kstackleftmin > stackleft) {
   1740   1.48      yamt 		kstackleftmin = stackleft;
   1741   1.48      yamt 		if (stackleft < kstackleftthres)
   1742   1.56      yamt 			printf("warning: kernel stack left %d bytes"
   1743   1.56      yamt 			    "(pid %u:lid %u)\n", stackleft,
   1744   1.56      yamt 			    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
   1745   1.48      yamt 	}
   1746   1.48      yamt 
   1747   1.48      yamt 	if (stackleft <= 0) {
   1748   1.56      yamt 		panic("magic on the top of kernel stack changed for "
   1749   1.56      yamt 		    "pid %u, lid %u: maybe kernel stack overflow",
   1750   1.56      yamt 		    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
   1751   1.48      yamt 	}
   1752   1.48      yamt }
   1753   1.50     enami #endif /* KSTACK_CHECK_MAGIC */
   1754   1.79      yamt 
   1755   1.79      yamt int
   1756   1.79      yamt proclist_foreach_call(struct proclist *list,
   1757   1.79      yamt     int (*callback)(struct proc *, void *arg), void *arg)
   1758   1.79      yamt {
   1759   1.79      yamt 	struct proc marker;
   1760   1.79      yamt 	struct proc *p;
   1761   1.79      yamt 	int ret = 0;
   1762   1.79      yamt 
   1763  1.102     pavel 	marker.p_flag = PK_MARKER;
   1764  1.253        ad 	mutex_enter(&proc_lock);
   1765   1.79      yamt 	for (p = LIST_FIRST(list); ret == 0 && p != NULL;) {
   1766  1.102     pavel 		if (p->p_flag & PK_MARKER) {
   1767   1.79      yamt 			p = LIST_NEXT(p, p_list);
   1768   1.79      yamt 			continue;
   1769   1.79      yamt 		}
   1770   1.79      yamt 		LIST_INSERT_AFTER(p, &marker, p_list);
   1771   1.79      yamt 		ret = (*callback)(p, arg);
   1772  1.253        ad 		KASSERT(mutex_owned(&proc_lock));
   1773   1.79      yamt 		p = LIST_NEXT(&marker, p_list);
   1774   1.79      yamt 		LIST_REMOVE(&marker, p_list);
   1775   1.79      yamt 	}
   1776  1.253        ad 	mutex_exit(&proc_lock);
   1777   1.79      yamt 
   1778   1.79      yamt 	return ret;
   1779   1.79      yamt }
   1780   1.86      yamt 
   1781   1.86      yamt int
   1782   1.86      yamt proc_vmspace_getref(struct proc *p, struct vmspace **vm)
   1783   1.86      yamt {
   1784   1.86      yamt 
   1785   1.86      yamt 	/* XXXCDC: how should locking work here? */
   1786   1.86      yamt 
   1787   1.87      yamt 	/* curproc exception is for coredump. */
   1788   1.87      yamt 
   1789  1.100        ad 	if ((p != curproc && (p->p_sflag & PS_WEXIT) != 0) ||
   1790  1.254     kamil 	    (p->p_vmspace->vm_refcnt < 1)) {
   1791   1.86      yamt 		return EFAULT;
   1792   1.86      yamt 	}
   1793   1.86      yamt 
   1794   1.86      yamt 	uvmspace_addref(p->p_vmspace);
   1795   1.86      yamt 	*vm = p->p_vmspace;
   1796   1.86      yamt 
   1797   1.86      yamt 	return 0;
   1798   1.86      yamt }
   1799   1.94        ad 
   1800   1.94        ad /*
   1801   1.94        ad  * Acquire a write lock on the process credential.
   1802   1.94        ad  */
   1803  1.258  riastrad void
   1804  1.100        ad proc_crmod_enter(void)
   1805   1.94        ad {
   1806  1.100        ad 	struct lwp *l = curlwp;
   1807  1.100        ad 	struct proc *p = l->l_proc;
   1808  1.100        ad 	kauth_cred_t oc;
   1809   1.94        ad 
   1810  1.117       dsl 	/* Reset what needs to be reset in plimit. */
   1811  1.117       dsl 	if (p->p_limit->pl_corename != defcorename) {
   1812  1.178     rmind 		lim_setcorename(p, defcorename, 0);
   1813  1.117       dsl 	}
   1814  1.117       dsl 
   1815  1.137        ad 	mutex_enter(p->p_lock);
   1816  1.100        ad 
   1817  1.100        ad 	/* Ensure the LWP cached credentials are up to date. */
   1818  1.100        ad 	if ((oc = l->l_cred) != p->p_cred) {
   1819  1.273        ad 		l->l_cred = kauth_cred_hold(p->p_cred);
   1820  1.100        ad 		kauth_cred_free(oc);
   1821  1.100        ad 	}
   1822   1.94        ad }
   1823   1.94        ad 
   1824   1.94        ad /*
   1825  1.100        ad  * Set in a new process credential, and drop the write lock.  The credential
   1826  1.100        ad  * must have a reference already.  Optionally, free a no-longer required
   1827  1.268  riastrad  * credential.
   1828   1.94        ad  */
   1829   1.94        ad void
   1830  1.104   thorpej proc_crmod_leave(kauth_cred_t scred, kauth_cred_t fcred, bool sugid)
   1831   1.94        ad {
   1832  1.133        ad 	struct lwp *l = curlwp, *l2;
   1833  1.100        ad 	struct proc *p = l->l_proc;
   1834  1.100        ad 	kauth_cred_t oc;
   1835  1.100        ad 
   1836  1.137        ad 	KASSERT(mutex_owned(p->p_lock));
   1837  1.137        ad 
   1838  1.100        ad 	/* Is there a new credential to set in? */
   1839  1.100        ad 	if (scred != NULL) {
   1840  1.100        ad 		p->p_cred = scred;
   1841  1.133        ad 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1842  1.274        ad 			if (l2 != l) {
   1843  1.274        ad 				lwp_lock(l2);
   1844  1.274        ad 				l2->l_flag |= LW_CACHECRED;
   1845  1.274        ad 				lwp_need_userret(l2);
   1846  1.274        ad 				lwp_unlock(l2);
   1847  1.274        ad 			}
   1848  1.133        ad 		}
   1849  1.100        ad 
   1850  1.100        ad 		/* Ensure the LWP cached credentials are up to date. */
   1851  1.100        ad 		if ((oc = l->l_cred) != scred) {
   1852  1.274        ad 			l->l_cred = kauth_cred_hold(scred);
   1853  1.100        ad 		}
   1854  1.100        ad 	} else
   1855  1.100        ad 		oc = NULL;	/* XXXgcc */
   1856  1.100        ad 
   1857  1.100        ad 	if (sugid) {
   1858  1.100        ad 		/*
   1859  1.100        ad 		 * Mark process as having changed credentials, stops
   1860  1.100        ad 		 * tracing etc.
   1861  1.100        ad 		 */
   1862  1.102     pavel 		p->p_flag |= PK_SUGID;
   1863  1.100        ad 	}
   1864   1.94        ad 
   1865  1.137        ad 	mutex_exit(p->p_lock);
   1866  1.100        ad 
   1867  1.100        ad 	/* If there is a credential to be released, free it now. */
   1868  1.100        ad 	if (fcred != NULL) {
   1869  1.100        ad 		KASSERT(scred != NULL);
   1870   1.94        ad 		kauth_cred_free(fcred);
   1871  1.100        ad 		if (oc != scred)
   1872  1.100        ad 			kauth_cred_free(oc);
   1873  1.100        ad 	}
   1874  1.100        ad }
   1875  1.100        ad 
   1876  1.100        ad /*
   1877   1.95   thorpej  * proc_specific_key_create --
   1878   1.95   thorpej  *	Create a key for subsystem proc-specific data.
   1879   1.95   thorpej  */
   1880   1.95   thorpej int
   1881   1.95   thorpej proc_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
   1882   1.95   thorpej {
   1883   1.95   thorpej 
   1884   1.98   thorpej 	return (specificdata_key_create(proc_specificdata_domain, keyp, dtor));
   1885   1.95   thorpej }
   1886   1.95   thorpej 
   1887   1.95   thorpej /*
   1888   1.95   thorpej  * proc_specific_key_delete --
   1889   1.95   thorpej  *	Delete a key for subsystem proc-specific data.
   1890   1.95   thorpej  */
   1891   1.95   thorpej void
   1892   1.95   thorpej proc_specific_key_delete(specificdata_key_t key)
   1893   1.95   thorpej {
   1894   1.95   thorpej 
   1895   1.95   thorpej 	specificdata_key_delete(proc_specificdata_domain, key);
   1896   1.95   thorpej }
   1897   1.95   thorpej 
   1898   1.98   thorpej /*
   1899   1.98   thorpej  * proc_initspecific --
   1900   1.98   thorpej  *	Initialize a proc's specificdata container.
   1901   1.98   thorpej  */
   1902   1.96  christos void
   1903   1.96  christos proc_initspecific(struct proc *p)
   1904   1.96  christos {
   1905  1.189    martin 	int error __diagused;
   1906   1.98   thorpej 
   1907   1.96  christos 	error = specificdata_init(proc_specificdata_domain, &p->p_specdataref);
   1908   1.96  christos 	KASSERT(error == 0);
   1909   1.96  christos }
   1910   1.96  christos 
   1911   1.95   thorpej /*
   1912   1.98   thorpej  * proc_finispecific --
   1913   1.98   thorpej  *	Finalize a proc's specificdata container.
   1914   1.98   thorpej  */
   1915   1.98   thorpej void
   1916   1.98   thorpej proc_finispecific(struct proc *p)
   1917   1.98   thorpej {
   1918   1.98   thorpej 
   1919   1.98   thorpej 	specificdata_fini(proc_specificdata_domain, &p->p_specdataref);
   1920   1.98   thorpej }
   1921   1.98   thorpej 
   1922   1.98   thorpej /*
   1923   1.95   thorpej  * proc_getspecific --
   1924   1.95   thorpej  *	Return proc-specific data corresponding to the specified key.
   1925   1.95   thorpej  */
   1926   1.95   thorpej void *
   1927   1.95   thorpej proc_getspecific(struct proc *p, specificdata_key_t key)
   1928   1.95   thorpej {
   1929   1.95   thorpej 
   1930   1.95   thorpej 	return (specificdata_getspecific(proc_specificdata_domain,
   1931   1.95   thorpej 					 &p->p_specdataref, key));
   1932   1.95   thorpej }
   1933   1.95   thorpej 
   1934   1.95   thorpej /*
   1935   1.95   thorpej  * proc_setspecific --
   1936   1.95   thorpej  *	Set proc-specific data corresponding to the specified key.
   1937   1.95   thorpej  */
   1938   1.95   thorpej void
   1939   1.95   thorpej proc_setspecific(struct proc *p, specificdata_key_t key, void *data)
   1940   1.95   thorpej {
   1941   1.95   thorpej 
   1942   1.95   thorpej 	specificdata_setspecific(proc_specificdata_domain,
   1943   1.95   thorpej 				 &p->p_specdataref, key, data);
   1944   1.95   thorpej }
   1945  1.154      elad 
   1946  1.154      elad int
   1947  1.154      elad proc_uidmatch(kauth_cred_t cred, kauth_cred_t target)
   1948  1.154      elad {
   1949  1.154      elad 	int r = 0;
   1950  1.154      elad 
   1951  1.154      elad 	if (kauth_cred_getuid(cred) != kauth_cred_getuid(target) ||
   1952  1.154      elad 	    kauth_cred_getuid(cred) != kauth_cred_getsvuid(target)) {
   1953  1.154      elad 		/*
   1954  1.154      elad 		 * suid proc of ours or proc not ours
   1955  1.154      elad 		 */
   1956  1.154      elad 		r = EPERM;
   1957  1.154      elad 	} else if (kauth_cred_getgid(target) != kauth_cred_getsvgid(target)) {
   1958  1.154      elad 		/*
   1959  1.154      elad 		 * sgid proc has sgid back to us temporarily
   1960  1.154      elad 		 */
   1961  1.154      elad 		r = EPERM;
   1962  1.154      elad 	} else {
   1963  1.154      elad 		/*
   1964  1.154      elad 		 * our rgid must be in target's group list (ie,
   1965  1.154      elad 		 * sub-processes started by a sgid process)
   1966  1.154      elad 		 */
   1967  1.154      elad 		int ismember = 0;
   1968  1.154      elad 
   1969  1.154      elad 		if (kauth_cred_ismember_gid(cred,
   1970  1.154      elad 		    kauth_cred_getgid(target), &ismember) != 0 ||
   1971  1.154      elad 		    !ismember)
   1972  1.154      elad 			r = EPERM;
   1973  1.154      elad 	}
   1974  1.154      elad 
   1975  1.154      elad 	return (r);
   1976  1.154      elad }
   1977  1.170     pooka 
   1978  1.170     pooka /*
   1979  1.170     pooka  * sysctl stuff
   1980  1.170     pooka  */
   1981  1.170     pooka 
   1982  1.170     pooka #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
   1983  1.170     pooka 
   1984  1.170     pooka static const u_int sysctl_flagmap[] = {
   1985  1.170     pooka 	PK_ADVLOCK, P_ADVLOCK,
   1986  1.170     pooka 	PK_EXEC, P_EXEC,
   1987  1.170     pooka 	PK_NOCLDWAIT, P_NOCLDWAIT,
   1988  1.170     pooka 	PK_32, P_32,
   1989  1.170     pooka 	PK_CLDSIGIGN, P_CLDSIGIGN,
   1990  1.170     pooka 	PK_SUGID, P_SUGID,
   1991  1.170     pooka 	0
   1992  1.170     pooka };
   1993  1.170     pooka 
   1994  1.170     pooka static const u_int sysctl_sflagmap[] = {
   1995  1.170     pooka 	PS_NOCLDSTOP, P_NOCLDSTOP,
   1996  1.170     pooka 	PS_WEXIT, P_WEXIT,
   1997  1.170     pooka 	PS_STOPFORK, P_STOPFORK,
   1998  1.170     pooka 	PS_STOPEXEC, P_STOPEXEC,
   1999  1.170     pooka 	PS_STOPEXIT, P_STOPEXIT,
   2000  1.170     pooka 	0
   2001  1.170     pooka };
   2002  1.170     pooka 
   2003  1.170     pooka static const u_int sysctl_slflagmap[] = {
   2004  1.170     pooka 	PSL_TRACED, P_TRACED,
   2005  1.170     pooka 	PSL_CHTRACED, P_CHTRACED,
   2006  1.170     pooka 	PSL_SYSCALL, P_SYSCALL,
   2007  1.170     pooka 	0
   2008  1.170     pooka };
   2009  1.170     pooka 
   2010  1.170     pooka static const u_int sysctl_lflagmap[] = {
   2011  1.170     pooka 	PL_CONTROLT, P_CONTROLT,
   2012  1.170     pooka 	PL_PPWAIT, P_PPWAIT,
   2013  1.170     pooka 	0
   2014  1.170     pooka };
   2015  1.170     pooka 
   2016  1.170     pooka static const u_int sysctl_stflagmap[] = {
   2017  1.170     pooka 	PST_PROFIL, P_PROFIL,
   2018  1.170     pooka 	0
   2019  1.170     pooka 
   2020  1.170     pooka };
   2021  1.170     pooka 
   2022  1.170     pooka /* used by kern_lwp also */
   2023  1.170     pooka const u_int sysctl_lwpflagmap[] = {
   2024  1.170     pooka 	LW_SINTR, L_SINTR,
   2025  1.170     pooka 	LW_SYSTEM, L_SYSTEM,
   2026  1.170     pooka 	0
   2027  1.170     pooka };
   2028  1.170     pooka 
   2029  1.170     pooka /*
   2030  1.170     pooka  * Find the most ``active'' lwp of a process and return it for ps display
   2031  1.170     pooka  * purposes
   2032  1.170     pooka  */
   2033  1.170     pooka static struct lwp *
   2034  1.170     pooka proc_active_lwp(struct proc *p)
   2035  1.170     pooka {
   2036  1.170     pooka 	static const int ostat[] = {
   2037  1.258  riastrad 		0,
   2038  1.170     pooka 		2,	/* LSIDL */
   2039  1.170     pooka 		6,	/* LSRUN */
   2040  1.170     pooka 		5,	/* LSSLEEP */
   2041  1.170     pooka 		4,	/* LSSTOP */
   2042  1.170     pooka 		0,	/* LSZOMB */
   2043  1.170     pooka 		1,	/* LSDEAD */
   2044  1.170     pooka 		7,	/* LSONPROC */
   2045  1.170     pooka 		3	/* LSSUSPENDED */
   2046  1.170     pooka 	};
   2047  1.170     pooka 
   2048  1.170     pooka 	struct lwp *l, *lp = NULL;
   2049  1.170     pooka 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2050  1.270  riastrad 		KASSERT(l->l_stat >= 0);
   2051  1.270  riastrad 		KASSERT(l->l_stat < __arraycount(ostat));
   2052  1.170     pooka 		if (lp == NULL ||
   2053  1.170     pooka 		    ostat[l->l_stat] > ostat[lp->l_stat] ||
   2054  1.170     pooka 		    (ostat[l->l_stat] == ostat[lp->l_stat] &&
   2055  1.170     pooka 		    l->l_cpticks > lp->l_cpticks)) {
   2056  1.170     pooka 			lp = l;
   2057  1.170     pooka 			continue;
   2058  1.170     pooka 		}
   2059  1.170     pooka 	}
   2060  1.170     pooka 	return lp;
   2061  1.170     pooka }
   2062  1.170     pooka 
   2063  1.170     pooka static int
   2064  1.170     pooka sysctl_doeproc(SYSCTLFN_ARGS)
   2065  1.170     pooka {
   2066  1.170     pooka 	union {
   2067  1.170     pooka 		struct kinfo_proc kproc;
   2068  1.170     pooka 		struct kinfo_proc2 kproc2;
   2069  1.170     pooka 	} *kbuf;
   2070  1.170     pooka 	struct proc *p, *next, *marker;
   2071  1.170     pooka 	char *where, *dp;
   2072  1.170     pooka 	int type, op, arg, error;
   2073  1.170     pooka 	u_int elem_size, kelem_size, elem_count;
   2074  1.170     pooka 	size_t buflen, needed;
   2075  1.170     pooka 	bool match, zombie, mmmbrains;
   2076  1.222  christos 	const bool allowaddr = get_expose_address(curproc);
   2077  1.170     pooka 
   2078  1.170     pooka 	if (namelen == 1 && name[0] == CTL_QUERY)
   2079  1.170     pooka 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2080  1.170     pooka 
   2081  1.170     pooka 	dp = where = oldp;
   2082  1.170     pooka 	buflen = where != NULL ? *oldlenp : 0;
   2083  1.170     pooka 	error = 0;
   2084  1.170     pooka 	needed = 0;
   2085  1.170     pooka 	type = rnode->sysctl_num;
   2086  1.170     pooka 
   2087  1.170     pooka 	if (type == KERN_PROC) {
   2088  1.194  christos 		if (namelen == 0)
   2089  1.194  christos 			return EINVAL;
   2090  1.194  christos 		switch (op = name[0]) {
   2091  1.194  christos 		case KERN_PROC_ALL:
   2092  1.194  christos 			if (namelen != 1)
   2093  1.194  christos 				return EINVAL;
   2094  1.194  christos 			arg = 0;
   2095  1.194  christos 			break;
   2096  1.194  christos 		default:
   2097  1.194  christos 			if (namelen != 2)
   2098  1.194  christos 				return EINVAL;
   2099  1.170     pooka 			arg = name[1];
   2100  1.194  christos 			break;
   2101  1.194  christos 		}
   2102  1.210       kre 		elem_count = 0;	/* Hush little compiler, don't you cry */
   2103  1.170     pooka 		kelem_size = elem_size = sizeof(kbuf->kproc);
   2104  1.170     pooka 	} else {
   2105  1.170     pooka 		if (namelen != 4)
   2106  1.194  christos 			return EINVAL;
   2107  1.170     pooka 		op = name[0];
   2108  1.170     pooka 		arg = name[1];
   2109  1.170     pooka 		elem_size = name[2];
   2110  1.170     pooka 		elem_count = name[3];
   2111  1.170     pooka 		kelem_size = sizeof(kbuf->kproc2);
   2112  1.170     pooka 	}
   2113  1.170     pooka 
   2114  1.170     pooka 	sysctl_unlock();
   2115  1.170     pooka 
   2116  1.221  christos 	kbuf = kmem_zalloc(sizeof(*kbuf), KM_SLEEP);
   2117  1.170     pooka 	marker = kmem_alloc(sizeof(*marker), KM_SLEEP);
   2118  1.170     pooka 	marker->p_flag = PK_MARKER;
   2119  1.170     pooka 
   2120  1.253        ad 	mutex_enter(&proc_lock);
   2121  1.211     kamil 	/*
   2122  1.211     kamil 	 * Start with zombies to prevent reporting processes twice, in case they
   2123  1.211     kamil 	 * are dying and being moved from the list of alive processes to zombies.
   2124  1.211     kamil 	 */
   2125  1.211     kamil 	mmmbrains = true;
   2126  1.211     kamil 	for (p = LIST_FIRST(&zombproc);; p = next) {
   2127  1.170     pooka 		if (p == NULL) {
   2128  1.211     kamil 			if (mmmbrains) {
   2129  1.211     kamil 				p = LIST_FIRST(&allproc);
   2130  1.211     kamil 				mmmbrains = false;
   2131  1.170     pooka 			}
   2132  1.170     pooka 			if (p == NULL)
   2133  1.170     pooka 				break;
   2134  1.170     pooka 		}
   2135  1.170     pooka 		next = LIST_NEXT(p, p_list);
   2136  1.170     pooka 		if ((p->p_flag & PK_MARKER) != 0)
   2137  1.170     pooka 			continue;
   2138  1.170     pooka 
   2139  1.170     pooka 		/*
   2140  1.170     pooka 		 * Skip embryonic processes.
   2141  1.170     pooka 		 */
   2142  1.170     pooka 		if (p->p_stat == SIDL)
   2143  1.170     pooka 			continue;
   2144  1.170     pooka 
   2145  1.170     pooka 		mutex_enter(p->p_lock);
   2146  1.170     pooka 		error = kauth_authorize_process(l->l_cred,
   2147  1.170     pooka 		    KAUTH_PROCESS_CANSEE, p,
   2148  1.213      maxv 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_EPROC), NULL, NULL);
   2149  1.170     pooka 		if (error != 0) {
   2150  1.170     pooka 			mutex_exit(p->p_lock);
   2151  1.170     pooka 			continue;
   2152  1.170     pooka 		}
   2153  1.170     pooka 
   2154  1.170     pooka 		/*
   2155  1.211     kamil 		 * Hande all the operations in one switch on the cost of
   2156  1.211     kamil 		 * algorithm complexity is on purpose. The win splitting this
   2157  1.266    andvar 		 * function into several similar copies makes maintenance
   2158  1.266    andvar 		 * burden, code grow and boost is negligible in practical
   2159  1.266    andvar 		 * systems.
   2160  1.170     pooka 		 */
   2161  1.170     pooka 		switch (op) {
   2162  1.170     pooka 		case KERN_PROC_PID:
   2163  1.170     pooka 			match = (p->p_pid == (pid_t)arg);
   2164  1.170     pooka 			break;
   2165  1.170     pooka 
   2166  1.170     pooka 		case KERN_PROC_PGRP:
   2167  1.170     pooka 			match = (p->p_pgrp->pg_id == (pid_t)arg);
   2168  1.170     pooka 			break;
   2169  1.170     pooka 
   2170  1.170     pooka 		case KERN_PROC_SESSION:
   2171  1.170     pooka 			match = (p->p_session->s_sid == (pid_t)arg);
   2172  1.170     pooka 			break;
   2173  1.170     pooka 
   2174  1.170     pooka 		case KERN_PROC_TTY:
   2175  1.170     pooka 			match = true;
   2176  1.170     pooka 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
   2177  1.170     pooka 				if ((p->p_lflag & PL_CONTROLT) == 0 ||
   2178  1.170     pooka 				    p->p_session->s_ttyp == NULL ||
   2179  1.170     pooka 				    p->p_session->s_ttyvp != NULL) {
   2180  1.170     pooka 				    	match = false;
   2181  1.170     pooka 				}
   2182  1.170     pooka 			} else if ((p->p_lflag & PL_CONTROLT) == 0 ||
   2183  1.170     pooka 			    p->p_session->s_ttyp == NULL) {
   2184  1.170     pooka 				if ((dev_t)arg != KERN_PROC_TTY_NODEV) {
   2185  1.170     pooka 					match = false;
   2186  1.170     pooka 				}
   2187  1.170     pooka 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg) {
   2188  1.170     pooka 				match = false;
   2189  1.170     pooka 			}
   2190  1.170     pooka 			break;
   2191  1.170     pooka 
   2192  1.170     pooka 		case KERN_PROC_UID:
   2193  1.170     pooka 			match = (kauth_cred_geteuid(p->p_cred) == (uid_t)arg);
   2194  1.170     pooka 			break;
   2195  1.170     pooka 
   2196  1.170     pooka 		case KERN_PROC_RUID:
   2197  1.170     pooka 			match = (kauth_cred_getuid(p->p_cred) == (uid_t)arg);
   2198  1.170     pooka 			break;
   2199  1.170     pooka 
   2200  1.170     pooka 		case KERN_PROC_GID:
   2201  1.170     pooka 			match = (kauth_cred_getegid(p->p_cred) == (uid_t)arg);
   2202  1.170     pooka 			break;
   2203  1.170     pooka 
   2204  1.170     pooka 		case KERN_PROC_RGID:
   2205  1.170     pooka 			match = (kauth_cred_getgid(p->p_cred) == (uid_t)arg);
   2206  1.170     pooka 			break;
   2207  1.170     pooka 
   2208  1.170     pooka 		case KERN_PROC_ALL:
   2209  1.170     pooka 			match = true;
   2210  1.170     pooka 			/* allow everything */
   2211  1.170     pooka 			break;
   2212  1.170     pooka 
   2213  1.170     pooka 		default:
   2214  1.170     pooka 			error = EINVAL;
   2215  1.170     pooka 			mutex_exit(p->p_lock);
   2216  1.170     pooka 			goto cleanup;
   2217  1.170     pooka 		}
   2218  1.170     pooka 		if (!match) {
   2219  1.170     pooka 			mutex_exit(p->p_lock);
   2220  1.170     pooka 			continue;
   2221  1.170     pooka 		}
   2222  1.170     pooka 
   2223  1.170     pooka 		/*
   2224  1.170     pooka 		 * Grab a hold on the process.
   2225  1.170     pooka 		 */
   2226  1.258  riastrad 		if (mmmbrains) {
   2227  1.170     pooka 			zombie = true;
   2228  1.170     pooka 		} else {
   2229  1.170     pooka 			zombie = !rw_tryenter(&p->p_reflock, RW_READER);
   2230  1.170     pooka 		}
   2231  1.170     pooka 		if (zombie) {
   2232  1.170     pooka 			LIST_INSERT_AFTER(p, marker, p_list);
   2233  1.170     pooka 		}
   2234  1.170     pooka 
   2235  1.170     pooka 		if (buflen >= elem_size &&
   2236  1.170     pooka 		    (type == KERN_PROC || elem_count > 0)) {
   2237  1.234     kamil 			ruspace(p);	/* Update process vm resource use */
   2238  1.234     kamil 
   2239  1.170     pooka 			if (type == KERN_PROC) {
   2240  1.222  christos 				fill_proc(p, &kbuf->kproc.kp_proc, allowaddr);
   2241  1.222  christos 				fill_eproc(p, &kbuf->kproc.kp_eproc, zombie,
   2242  1.222  christos 				    allowaddr);
   2243  1.170     pooka 			} else {
   2244  1.222  christos 				fill_kproc2(p, &kbuf->kproc2, zombie,
   2245  1.222  christos 				    allowaddr);
   2246  1.170     pooka 				elem_count--;
   2247  1.170     pooka 			}
   2248  1.170     pooka 			mutex_exit(p->p_lock);
   2249  1.253        ad 			mutex_exit(&proc_lock);
   2250  1.170     pooka 			/*
   2251  1.170     pooka 			 * Copy out elem_size, but not larger than kelem_size
   2252  1.170     pooka 			 */
   2253  1.170     pooka 			error = sysctl_copyout(l, kbuf, dp,
   2254  1.214  riastrad 			    uimin(kelem_size, elem_size));
   2255  1.253        ad 			mutex_enter(&proc_lock);
   2256  1.170     pooka 			if (error) {
   2257  1.170     pooka 				goto bah;
   2258  1.170     pooka 			}
   2259  1.170     pooka 			dp += elem_size;
   2260  1.170     pooka 			buflen -= elem_size;
   2261  1.170     pooka 		} else {
   2262  1.170     pooka 			mutex_exit(p->p_lock);
   2263  1.170     pooka 		}
   2264  1.170     pooka 		needed += elem_size;
   2265  1.170     pooka 
   2266  1.170     pooka 		/*
   2267  1.170     pooka 		 * Release reference to process.
   2268  1.170     pooka 		 */
   2269  1.170     pooka 	 	if (zombie) {
   2270  1.170     pooka 			next = LIST_NEXT(marker, p_list);
   2271  1.170     pooka  			LIST_REMOVE(marker, p_list);
   2272  1.170     pooka 		} else {
   2273  1.170     pooka 			rw_exit(&p->p_reflock);
   2274  1.170     pooka 			next = LIST_NEXT(p, p_list);
   2275  1.170     pooka 		}
   2276  1.211     kamil 
   2277  1.211     kamil 		/*
   2278  1.211     kamil 		 * Short-circuit break quickly!
   2279  1.211     kamil 		 */
   2280  1.211     kamil 		if (op == KERN_PROC_PID)
   2281  1.211     kamil                 	break;
   2282  1.170     pooka 	}
   2283  1.253        ad 	mutex_exit(&proc_lock);
   2284  1.170     pooka 
   2285  1.170     pooka 	if (where != NULL) {
   2286  1.170     pooka 		*oldlenp = dp - where;
   2287  1.170     pooka 		if (needed > *oldlenp) {
   2288  1.170     pooka 			error = ENOMEM;
   2289  1.170     pooka 			goto out;
   2290  1.170     pooka 		}
   2291  1.170     pooka 	} else {
   2292  1.170     pooka 		needed += KERN_PROCSLOP;
   2293  1.170     pooka 		*oldlenp = needed;
   2294  1.170     pooka 	}
   2295  1.211     kamil 	kmem_free(kbuf, sizeof(*kbuf));
   2296  1.211     kamil 	kmem_free(marker, sizeof(*marker));
   2297  1.170     pooka 	sysctl_relock();
   2298  1.170     pooka 	return 0;
   2299  1.170     pooka  bah:
   2300  1.170     pooka  	if (zombie)
   2301  1.170     pooka  		LIST_REMOVE(marker, p_list);
   2302  1.170     pooka 	else
   2303  1.170     pooka 		rw_exit(&p->p_reflock);
   2304  1.170     pooka  cleanup:
   2305  1.253        ad 	mutex_exit(&proc_lock);
   2306  1.170     pooka  out:
   2307  1.211     kamil 	kmem_free(kbuf, sizeof(*kbuf));
   2308  1.211     kamil 	kmem_free(marker, sizeof(*marker));
   2309  1.170     pooka 	sysctl_relock();
   2310  1.170     pooka 	return error;
   2311  1.170     pooka }
   2312  1.170     pooka 
   2313  1.172     joerg int
   2314  1.172     joerg copyin_psstrings(struct proc *p, struct ps_strings *arginfo)
   2315  1.172     joerg {
   2316  1.225  pgoyette #if !defined(_RUMPKERNEL)
   2317  1.225  pgoyette 	int retval;
   2318  1.172     joerg 
   2319  1.172     joerg 	if (p->p_flag & PK_32) {
   2320  1.228  pgoyette 		MODULE_HOOK_CALL(kern_proc32_copyin_hook, (p, arginfo),
   2321  1.225  pgoyette 		    enosys(), retval);
   2322  1.225  pgoyette 		return retval;
   2323  1.225  pgoyette 	}
   2324  1.225  pgoyette #endif /* !defined(_RUMPKERNEL) */
   2325  1.172     joerg 
   2326  1.173      matt 	return copyin_proc(p, (void *)p->p_psstrp, arginfo, sizeof(*arginfo));
   2327  1.172     joerg }
   2328  1.172     joerg 
   2329  1.172     joerg static int
   2330  1.172     joerg copy_procargs_sysctl_cb(void *cookie_, const void *src, size_t off, size_t len)
   2331  1.172     joerg {
   2332  1.172     joerg 	void **cookie = cookie_;
   2333  1.172     joerg 	struct lwp *l = cookie[0];
   2334  1.172     joerg 	char *dst = cookie[1];
   2335  1.172     joerg 
   2336  1.172     joerg 	return sysctl_copyout(l, src, dst + off, len);
   2337  1.172     joerg }
   2338  1.172     joerg 
   2339  1.170     pooka /*
   2340  1.170     pooka  * sysctl helper routine for kern.proc_args pseudo-subtree.
   2341  1.170     pooka  */
   2342  1.170     pooka static int
   2343  1.170     pooka sysctl_kern_proc_args(SYSCTLFN_ARGS)
   2344  1.170     pooka {
   2345  1.170     pooka 	struct ps_strings pss;
   2346  1.170     pooka 	struct proc *p;
   2347  1.170     pooka 	pid_t pid;
   2348  1.172     joerg 	int type, error;
   2349  1.172     joerg 	void *cookie[2];
   2350  1.170     pooka 
   2351  1.170     pooka 	if (namelen == 1 && name[0] == CTL_QUERY)
   2352  1.170     pooka 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2353  1.170     pooka 
   2354  1.170     pooka 	if (newp != NULL || namelen != 2)
   2355  1.170     pooka 		return (EINVAL);
   2356  1.170     pooka 	pid = name[0];
   2357  1.170     pooka 	type = name[1];
   2358  1.170     pooka 
   2359  1.170     pooka 	switch (type) {
   2360  1.194  christos 	case KERN_PROC_PATHNAME:
   2361  1.194  christos 		sysctl_unlock();
   2362  1.194  christos 		error = fill_pathname(l, pid, oldp, oldlenp);
   2363  1.194  christos 		sysctl_relock();
   2364  1.194  christos 		return error;
   2365  1.194  christos 
   2366  1.229     kamil 	case KERN_PROC_CWD:
   2367  1.229     kamil 		sysctl_unlock();
   2368  1.229     kamil 		error = fill_cwd(l, pid, oldp, oldlenp);
   2369  1.229     kamil 		sysctl_relock();
   2370  1.229     kamil 		return error;
   2371  1.229     kamil 
   2372  1.170     pooka 	case KERN_PROC_ARGV:
   2373  1.170     pooka 	case KERN_PROC_NARGV:
   2374  1.170     pooka 	case KERN_PROC_ENV:
   2375  1.170     pooka 	case KERN_PROC_NENV:
   2376  1.170     pooka 		/* ok */
   2377  1.170     pooka 		break;
   2378  1.170     pooka 	default:
   2379  1.170     pooka 		return (EINVAL);
   2380  1.170     pooka 	}
   2381  1.170     pooka 
   2382  1.170     pooka 	sysctl_unlock();
   2383  1.170     pooka 
   2384  1.170     pooka 	/* check pid */
   2385  1.253        ad 	mutex_enter(&proc_lock);
   2386  1.170     pooka 	if ((p = proc_find(pid)) == NULL) {
   2387  1.170     pooka 		error = EINVAL;
   2388  1.170     pooka 		goto out_locked;
   2389  1.170     pooka 	}
   2390  1.170     pooka 	mutex_enter(p->p_lock);
   2391  1.170     pooka 
   2392  1.170     pooka 	/* Check permission. */
   2393  1.170     pooka 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
   2394  1.170     pooka 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
   2395  1.170     pooka 		    p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ARGS), NULL, NULL);
   2396  1.170     pooka 	else if (type == KERN_PROC_ENV || type == KERN_PROC_NENV)
   2397  1.170     pooka 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
   2398  1.170     pooka 		    p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENV), NULL, NULL);
   2399  1.170     pooka 	else
   2400  1.170     pooka 		error = EINVAL; /* XXXGCC */
   2401  1.170     pooka 	if (error) {
   2402  1.170     pooka 		mutex_exit(p->p_lock);
   2403  1.170     pooka 		goto out_locked;
   2404  1.170     pooka 	}
   2405  1.170     pooka 
   2406  1.170     pooka 	if (oldp == NULL) {
   2407  1.170     pooka 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
   2408  1.170     pooka 			*oldlenp = sizeof (int);
   2409  1.170     pooka 		else
   2410  1.170     pooka 			*oldlenp = ARG_MAX;	/* XXX XXX XXX */
   2411  1.170     pooka 		error = 0;
   2412  1.170     pooka 		mutex_exit(p->p_lock);
   2413  1.170     pooka 		goto out_locked;
   2414  1.170     pooka 	}
   2415  1.170     pooka 
   2416  1.170     pooka 	/*
   2417  1.170     pooka 	 * Zombies don't have a stack, so we can't read their psstrings.
   2418  1.170     pooka 	 * System processes also don't have a user stack.
   2419  1.170     pooka 	 */
   2420  1.170     pooka 	if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
   2421  1.170     pooka 		error = EINVAL;
   2422  1.170     pooka 		mutex_exit(p->p_lock);
   2423  1.170     pooka 		goto out_locked;
   2424  1.170     pooka 	}
   2425  1.170     pooka 
   2426  1.174     rmind 	error = rw_tryenter(&p->p_reflock, RW_READER) ? 0 : EBUSY;
   2427  1.172     joerg 	mutex_exit(p->p_lock);
   2428  1.174     rmind 	if (error) {
   2429  1.174     rmind 		goto out_locked;
   2430  1.174     rmind 	}
   2431  1.253        ad 	mutex_exit(&proc_lock);
   2432  1.172     joerg 
   2433  1.172     joerg 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
   2434  1.172     joerg 		int value;
   2435  1.172     joerg 		if ((error = copyin_psstrings(p, &pss)) == 0) {
   2436  1.172     joerg 			if (type == KERN_PROC_NARGV)
   2437  1.172     joerg 				value = pss.ps_nargvstr;
   2438  1.172     joerg 			else
   2439  1.172     joerg 				value = pss.ps_nenvstr;
   2440  1.172     joerg 			error = sysctl_copyout(l, &value, oldp, sizeof(value));
   2441  1.172     joerg 			*oldlenp = sizeof(value);
   2442  1.172     joerg 		}
   2443  1.170     pooka 	} else {
   2444  1.172     joerg 		cookie[0] = l;
   2445  1.172     joerg 		cookie[1] = oldp;
   2446  1.172     joerg 		error = copy_procargs(p, type, oldlenp,
   2447  1.172     joerg 		    copy_procargs_sysctl_cb, cookie);
   2448  1.170     pooka 	}
   2449  1.172     joerg 	rw_exit(&p->p_reflock);
   2450  1.172     joerg 	sysctl_relock();
   2451  1.172     joerg 	return error;
   2452  1.172     joerg 
   2453  1.172     joerg out_locked:
   2454  1.253        ad 	mutex_exit(&proc_lock);
   2455  1.172     joerg 	sysctl_relock();
   2456  1.172     joerg 	return error;
   2457  1.172     joerg }
   2458  1.172     joerg 
   2459  1.172     joerg int
   2460  1.172     joerg copy_procargs(struct proc *p, int oid, size_t *limit,
   2461  1.172     joerg     int (*cb)(void *, const void *, size_t, size_t), void *cookie)
   2462  1.172     joerg {
   2463  1.172     joerg 	struct ps_strings pss;
   2464  1.172     joerg 	size_t len, i, loaded, entry_len;
   2465  1.172     joerg 	struct uio auio;
   2466  1.172     joerg 	struct iovec aiov;
   2467  1.172     joerg 	int error, argvlen;
   2468  1.172     joerg 	char *arg;
   2469  1.172     joerg 	char **argv;
   2470  1.172     joerg 	vaddr_t user_argv;
   2471  1.172     joerg 	struct vmspace *vmspace;
   2472  1.170     pooka 
   2473  1.170     pooka 	/*
   2474  1.172     joerg 	 * Allocate a temporary buffer to hold the argument vector and
   2475  1.172     joerg 	 * the arguments themselve.
   2476  1.170     pooka 	 */
   2477  1.170     pooka 	arg = kmem_alloc(PAGE_SIZE, KM_SLEEP);
   2478  1.172     joerg 	argv = kmem_alloc(PAGE_SIZE, KM_SLEEP);
   2479  1.172     joerg 
   2480  1.172     joerg 	/*
   2481  1.172     joerg 	 * Lock the process down in memory.
   2482  1.172     joerg 	 */
   2483  1.172     joerg 	vmspace = p->p_vmspace;
   2484  1.172     joerg 	uvmspace_addref(vmspace);
   2485  1.170     pooka 
   2486  1.170     pooka 	/*
   2487  1.170     pooka 	 * Read in the ps_strings structure.
   2488  1.170     pooka 	 */
   2489  1.172     joerg 	if ((error = copyin_psstrings(p, &pss)) != 0)
   2490  1.170     pooka 		goto done;
   2491  1.170     pooka 
   2492  1.170     pooka 	/*
   2493  1.170     pooka 	 * Now read the address of the argument vector.
   2494  1.170     pooka 	 */
   2495  1.172     joerg 	switch (oid) {
   2496  1.170     pooka 	case KERN_PROC_ARGV:
   2497  1.172     joerg 		user_argv = (uintptr_t)pss.ps_argvstr;
   2498  1.172     joerg 		argvlen = pss.ps_nargvstr;
   2499  1.172     joerg 		break;
   2500  1.170     pooka 	case KERN_PROC_ENV:
   2501  1.172     joerg 		user_argv = (uintptr_t)pss.ps_envstr;
   2502  1.172     joerg 		argvlen = pss.ps_nenvstr;
   2503  1.170     pooka 		break;
   2504  1.170     pooka 	default:
   2505  1.170     pooka 		error = EINVAL;
   2506  1.170     pooka 		goto done;
   2507  1.170     pooka 	}
   2508  1.170     pooka 
   2509  1.172     joerg 	if (argvlen < 0) {
   2510  1.172     joerg 		error = EIO;
   2511  1.172     joerg 		goto done;
   2512  1.172     joerg 	}
   2513  1.172     joerg 
   2514  1.170     pooka 
   2515  1.170     pooka 	/*
   2516  1.170     pooka 	 * Now copy each string.
   2517  1.170     pooka 	 */
   2518  1.170     pooka 	len = 0; /* bytes written to user buffer */
   2519  1.172     joerg 	loaded = 0; /* bytes from argv already processed */
   2520  1.172     joerg 	i = 0; /* To make compiler happy */
   2521  1.198  christos 	entry_len = PROC_PTRSZ(p);
   2522  1.172     joerg 
   2523  1.172     joerg 	for (; argvlen; --argvlen) {
   2524  1.170     pooka 		int finished = 0;
   2525  1.170     pooka 		vaddr_t base;
   2526  1.170     pooka 		size_t xlen;
   2527  1.170     pooka 		int j;
   2528  1.170     pooka 
   2529  1.172     joerg 		if (loaded == 0) {
   2530  1.172     joerg 			size_t rem = entry_len * argvlen;
   2531  1.172     joerg 			loaded = MIN(rem, PAGE_SIZE);
   2532  1.172     joerg 			error = copyin_vmspace(vmspace,
   2533  1.172     joerg 			    (const void *)user_argv, argv, loaded);
   2534  1.172     joerg 			if (error)
   2535  1.172     joerg 				break;
   2536  1.172     joerg 			user_argv += loaded;
   2537  1.172     joerg 			i = 0;
   2538  1.172     joerg 		}
   2539  1.172     joerg 
   2540  1.225  pgoyette #if !defined(_RUMPKERNEL)
   2541  1.225  pgoyette 		if (p->p_flag & PK_32)
   2542  1.228  pgoyette 			MODULE_HOOK_CALL(kern_proc32_base_hook,
   2543  1.225  pgoyette 			    (argv, i++), 0, base);
   2544  1.225  pgoyette 		else
   2545  1.225  pgoyette #endif /* !defined(_RUMPKERNEL) */
   2546  1.172     joerg 			base = (vaddr_t)argv[i++];
   2547  1.172     joerg 		loaded -= entry_len;
   2548  1.170     pooka 
   2549  1.170     pooka 		/*
   2550  1.170     pooka 		 * The program has messed around with its arguments,
   2551  1.170     pooka 		 * possibly deleting some, and replacing them with
   2552  1.170     pooka 		 * NULL's. Treat this as the last argument and not
   2553  1.170     pooka 		 * a failure.
   2554  1.170     pooka 		 */
   2555  1.170     pooka 		if (base == 0)
   2556  1.170     pooka 			break;
   2557  1.170     pooka 
   2558  1.170     pooka 		while (!finished) {
   2559  1.170     pooka 			xlen = PAGE_SIZE - (base & PAGE_MASK);
   2560  1.170     pooka 
   2561  1.170     pooka 			aiov.iov_base = arg;
   2562  1.170     pooka 			aiov.iov_len = PAGE_SIZE;
   2563  1.170     pooka 			auio.uio_iov = &aiov;
   2564  1.170     pooka 			auio.uio_iovcnt = 1;
   2565  1.170     pooka 			auio.uio_offset = base;
   2566  1.170     pooka 			auio.uio_resid = xlen;
   2567  1.170     pooka 			auio.uio_rw = UIO_READ;
   2568  1.170     pooka 			UIO_SETUP_SYSSPACE(&auio);
   2569  1.196  christos 			error = uvm_io(&vmspace->vm_map, &auio, 0);
   2570  1.170     pooka 			if (error)
   2571  1.170     pooka 				goto done;
   2572  1.170     pooka 
   2573  1.170     pooka 			/* Look for the end of the string */
   2574  1.170     pooka 			for (j = 0; j < xlen; j++) {
   2575  1.170     pooka 				if (arg[j] == '\0') {
   2576  1.170     pooka 					xlen = j + 1;
   2577  1.170     pooka 					finished = 1;
   2578  1.170     pooka 					break;
   2579  1.170     pooka 				}
   2580  1.170     pooka 			}
   2581  1.170     pooka 
   2582  1.170     pooka 			/* Check for user buffer overflow */
   2583  1.172     joerg 			if (len + xlen > *limit) {
   2584  1.170     pooka 				finished = 1;
   2585  1.172     joerg 				if (len > *limit)
   2586  1.170     pooka 					xlen = 0;
   2587  1.170     pooka 				else
   2588  1.172     joerg 					xlen = *limit - len;
   2589  1.170     pooka 			}
   2590  1.170     pooka 
   2591  1.170     pooka 			/* Copyout the page */
   2592  1.172     joerg 			error = (*cb)(cookie, arg, len, xlen);
   2593  1.170     pooka 			if (error)
   2594  1.170     pooka 				goto done;
   2595  1.170     pooka 
   2596  1.170     pooka 			len += xlen;
   2597  1.170     pooka 			base += xlen;
   2598  1.170     pooka 		}
   2599  1.170     pooka 	}
   2600  1.172     joerg 	*limit = len;
   2601  1.170     pooka 
   2602  1.170     pooka done:
   2603  1.172     joerg 	kmem_free(argv, PAGE_SIZE);
   2604  1.172     joerg 	kmem_free(arg, PAGE_SIZE);
   2605  1.170     pooka 	uvmspace_free(vmspace);
   2606  1.170     pooka 	return error;
   2607  1.170     pooka }
   2608  1.170     pooka 
   2609  1.170     pooka /*
   2610  1.220      maxv  * Fill in a proc structure for the specified process.
   2611  1.220      maxv  */
   2612  1.220      maxv static void
   2613  1.222  christos fill_proc(const struct proc *psrc, struct proc *p, bool allowaddr)
   2614  1.220      maxv {
   2615  1.256  christos 	COND_SET_STRUCT(p->p_list, psrc->p_list, allowaddr);
   2616  1.259  riastrad 	memset(&p->p_auxlock, 0, sizeof(p->p_auxlock));
   2617  1.256  christos 	COND_SET_STRUCT(p->p_lock, psrc->p_lock, allowaddr);
   2618  1.259  riastrad 	memset(&p->p_stmutex, 0, sizeof(p->p_stmutex));
   2619  1.259  riastrad 	memset(&p->p_reflock, 0, sizeof(p->p_reflock));
   2620  1.256  christos 	COND_SET_STRUCT(p->p_waitcv, psrc->p_waitcv, allowaddr);
   2621  1.256  christos 	COND_SET_STRUCT(p->p_lwpcv, psrc->p_lwpcv, allowaddr);
   2622  1.256  christos 	COND_SET_PTR(p->p_cred, psrc->p_cred, allowaddr);
   2623  1.256  christos 	COND_SET_PTR(p->p_fd, psrc->p_fd, allowaddr);
   2624  1.256  christos 	COND_SET_PTR(p->p_cwdi, psrc->p_cwdi, allowaddr);
   2625  1.256  christos 	COND_SET_PTR(p->p_stats, psrc->p_stats, allowaddr);
   2626  1.256  christos 	COND_SET_PTR(p->p_limit, psrc->p_limit, allowaddr);
   2627  1.256  christos 	COND_SET_PTR(p->p_vmspace, psrc->p_vmspace, allowaddr);
   2628  1.256  christos 	COND_SET_PTR(p->p_sigacts, psrc->p_sigacts, allowaddr);
   2629  1.256  christos 	COND_SET_PTR(p->p_aio, psrc->p_aio, allowaddr);
   2630  1.220      maxv 	p->p_mqueue_cnt = psrc->p_mqueue_cnt;
   2631  1.259  riastrad 	memset(&p->p_specdataref, 0, sizeof(p->p_specdataref));
   2632  1.220      maxv 	p->p_exitsig = psrc->p_exitsig;
   2633  1.220      maxv 	p->p_flag = psrc->p_flag;
   2634  1.220      maxv 	p->p_sflag = psrc->p_sflag;
   2635  1.220      maxv 	p->p_slflag = psrc->p_slflag;
   2636  1.220      maxv 	p->p_lflag = psrc->p_lflag;
   2637  1.220      maxv 	p->p_stflag = psrc->p_stflag;
   2638  1.220      maxv 	p->p_stat = psrc->p_stat;
   2639  1.220      maxv 	p->p_trace_enabled = psrc->p_trace_enabled;
   2640  1.220      maxv 	p->p_pid = psrc->p_pid;
   2641  1.256  christos 	COND_SET_STRUCT(p->p_pglist, psrc->p_pglist, allowaddr);
   2642  1.256  christos 	COND_SET_PTR(p->p_pptr, psrc->p_pptr, allowaddr);
   2643  1.256  christos 	COND_SET_STRUCT(p->p_sibling, psrc->p_sibling, allowaddr);
   2644  1.256  christos 	COND_SET_STRUCT(p->p_children, psrc->p_children, allowaddr);
   2645  1.256  christos 	COND_SET_STRUCT(p->p_lwps, psrc->p_lwps, allowaddr);
   2646  1.256  christos 	COND_SET_PTR(p->p_raslist, psrc->p_raslist, allowaddr);
   2647  1.220      maxv 	p->p_nlwps = psrc->p_nlwps;
   2648  1.220      maxv 	p->p_nzlwps = psrc->p_nzlwps;
   2649  1.220      maxv 	p->p_nrlwps = psrc->p_nrlwps;
   2650  1.220      maxv 	p->p_nlwpwait = psrc->p_nlwpwait;
   2651  1.220      maxv 	p->p_ndlwps = psrc->p_ndlwps;
   2652  1.220      maxv 	p->p_nstopchild = psrc->p_nstopchild;
   2653  1.220      maxv 	p->p_waited = psrc->p_waited;
   2654  1.256  christos 	COND_SET_PTR(p->p_zomblwp, psrc->p_zomblwp, allowaddr);
   2655  1.256  christos 	COND_SET_PTR(p->p_vforklwp, psrc->p_vforklwp, allowaddr);
   2656  1.256  christos 	COND_SET_PTR(p->p_sched_info, psrc->p_sched_info, allowaddr);
   2657  1.220      maxv 	p->p_estcpu = psrc->p_estcpu;
   2658  1.220      maxv 	p->p_estcpu_inherited = psrc->p_estcpu_inherited;
   2659  1.220      maxv 	p->p_forktime = psrc->p_forktime;
   2660  1.220      maxv 	p->p_pctcpu = psrc->p_pctcpu;
   2661  1.256  christos 	COND_SET_PTR(p->p_opptr, psrc->p_opptr, allowaddr);
   2662  1.256  christos 	COND_SET_PTR(p->p_timers, psrc->p_timers, allowaddr);
   2663  1.220      maxv 	p->p_rtime = psrc->p_rtime;
   2664  1.220      maxv 	p->p_uticks = psrc->p_uticks;
   2665  1.220      maxv 	p->p_sticks = psrc->p_sticks;
   2666  1.220      maxv 	p->p_iticks = psrc->p_iticks;
   2667  1.220      maxv 	p->p_xutime = psrc->p_xutime;
   2668  1.220      maxv 	p->p_xstime = psrc->p_xstime;
   2669  1.220      maxv 	p->p_traceflag = psrc->p_traceflag;
   2670  1.256  christos 	COND_SET_PTR(p->p_tracep, psrc->p_tracep, allowaddr);
   2671  1.256  christos 	COND_SET_PTR(p->p_textvp, psrc->p_textvp, allowaddr);
   2672  1.256  christos 	COND_SET_PTR(p->p_emul, psrc->p_emul, allowaddr);
   2673  1.256  christos 	COND_SET_PTR(p->p_emuldata, psrc->p_emuldata, allowaddr);
   2674  1.256  christos 	COND_SET_CPTR(p->p_execsw, psrc->p_execsw, allowaddr);
   2675  1.256  christos 	COND_SET_STRUCT(p->p_klist, psrc->p_klist, allowaddr);
   2676  1.256  christos 	COND_SET_STRUCT(p->p_sigwaiters, psrc->p_sigwaiters, allowaddr);
   2677  1.256  christos 	COND_SET_STRUCT(p->p_sigpend.sp_info, psrc->p_sigpend.sp_info,
   2678  1.256  christos 	    allowaddr);
   2679  1.256  christos 	p->p_sigpend.sp_set = psrc->p_sigpend.sp_set;
   2680  1.256  christos 	COND_SET_PTR(p->p_lwpctl, psrc->p_lwpctl, allowaddr);
   2681  1.220      maxv 	p->p_ppid = psrc->p_ppid;
   2682  1.243     kamil 	p->p_oppid = psrc->p_oppid;
   2683  1.256  christos 	COND_SET_PTR(p->p_path, psrc->p_path, allowaddr);
   2684  1.256  christos 	p->p_sigctx = psrc->p_sigctx;
   2685  1.220      maxv 	p->p_nice = psrc->p_nice;
   2686  1.220      maxv 	memcpy(p->p_comm, psrc->p_comm, sizeof(p->p_comm));
   2687  1.256  christos 	COND_SET_PTR(p->p_pgrp, psrc->p_pgrp, allowaddr);
   2688  1.220      maxv 	COND_SET_VALUE(p->p_psstrp, psrc->p_psstrp, allowaddr);
   2689  1.220      maxv 	p->p_pax = psrc->p_pax;
   2690  1.220      maxv 	p->p_xexit = psrc->p_xexit;
   2691  1.220      maxv 	p->p_xsig = psrc->p_xsig;
   2692  1.220      maxv 	p->p_acflag = psrc->p_acflag;
   2693  1.256  christos 	COND_SET_STRUCT(p->p_md, psrc->p_md, allowaddr);
   2694  1.220      maxv 	p->p_stackbase = psrc->p_stackbase;
   2695  1.256  christos 	COND_SET_PTR(p->p_dtrace, psrc->p_dtrace, allowaddr);
   2696  1.220      maxv }
   2697  1.220      maxv 
   2698  1.220      maxv /*
   2699  1.170     pooka  * Fill in an eproc structure for the specified process.
   2700  1.170     pooka  */
   2701  1.170     pooka void
   2702  1.222  christos fill_eproc(struct proc *p, struct eproc *ep, bool zombie, bool allowaddr)
   2703  1.170     pooka {
   2704  1.170     pooka 	struct tty *tp;
   2705  1.170     pooka 	struct lwp *l;
   2706  1.170     pooka 
   2707  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
   2708  1.170     pooka 	KASSERT(mutex_owned(p->p_lock));
   2709  1.170     pooka 
   2710  1.256  christos 	COND_SET_PTR(ep->e_paddr, p, allowaddr);
   2711  1.256  christos 	COND_SET_PTR(ep->e_sess, p->p_session, allowaddr);
   2712  1.170     pooka 	if (p->p_cred) {
   2713  1.170     pooka 		kauth_cred_topcred(p->p_cred, &ep->e_pcred);
   2714  1.170     pooka 		kauth_cred_toucred(p->p_cred, &ep->e_ucred);
   2715  1.170     pooka 	}
   2716  1.170     pooka 	if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
   2717  1.170     pooka 		struct vmspace *vm = p->p_vmspace;
   2718  1.170     pooka 
   2719  1.170     pooka 		ep->e_vm.vm_rssize = vm_resident_count(vm);
   2720  1.170     pooka 		ep->e_vm.vm_tsize = vm->vm_tsize;
   2721  1.170     pooka 		ep->e_vm.vm_dsize = vm->vm_dsize;
   2722  1.170     pooka 		ep->e_vm.vm_ssize = vm->vm_ssize;
   2723  1.170     pooka 		ep->e_vm.vm_map.size = vm->vm_map.size;
   2724  1.170     pooka 
   2725  1.170     pooka 		/* Pick the primary (first) LWP */
   2726  1.170     pooka 		l = proc_active_lwp(p);
   2727  1.170     pooka 		KASSERT(l != NULL);
   2728  1.170     pooka 		lwp_lock(l);
   2729  1.170     pooka 		if (l->l_wchan)
   2730  1.170     pooka 			strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
   2731  1.170     pooka 		lwp_unlock(l);
   2732  1.170     pooka 	}
   2733  1.199       kre 	ep->e_ppid = p->p_ppid;
   2734  1.170     pooka 	if (p->p_pgrp && p->p_session) {
   2735  1.170     pooka 		ep->e_pgid = p->p_pgrp->pg_id;
   2736  1.170     pooka 		ep->e_jobc = p->p_pgrp->pg_jobc;
   2737  1.170     pooka 		ep->e_sid = p->p_session->s_sid;
   2738  1.170     pooka 		if ((p->p_lflag & PL_CONTROLT) &&
   2739  1.216      maxv 		    (tp = p->p_session->s_ttyp)) {
   2740  1.170     pooka 			ep->e_tdev = tp->t_dev;
   2741  1.170     pooka 			ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2742  1.256  christos 			COND_SET_PTR(ep->e_tsess, tp->t_session, allowaddr);
   2743  1.170     pooka 		} else
   2744  1.170     pooka 			ep->e_tdev = (uint32_t)NODEV;
   2745  1.216      maxv 		ep->e_flag = p->p_session->s_ttyvp ? EPROC_CTTY : 0;
   2746  1.170     pooka 		if (SESS_LEADER(p))
   2747  1.170     pooka 			ep->e_flag |= EPROC_SLEADER;
   2748  1.216      maxv 		strncpy(ep->e_login, p->p_session->s_login, MAXLOGNAME);
   2749  1.170     pooka 	}
   2750  1.170     pooka 	ep->e_xsize = ep->e_xrssize = 0;
   2751  1.170     pooka 	ep->e_xccount = ep->e_xswrss = 0;
   2752  1.170     pooka }
   2753  1.170     pooka 
   2754  1.170     pooka /*
   2755  1.170     pooka  * Fill in a kinfo_proc2 structure for the specified process.
   2756  1.170     pooka  */
   2757  1.193     njoly void
   2758  1.222  christos fill_kproc2(struct proc *p, struct kinfo_proc2 *ki, bool zombie, bool allowaddr)
   2759  1.170     pooka {
   2760  1.170     pooka 	struct tty *tp;
   2761  1.272        ad 	struct lwp *l;
   2762  1.170     pooka 	struct timeval ut, st, rt;
   2763  1.170     pooka 	sigset_t ss1, ss2;
   2764  1.170     pooka 	struct rusage ru;
   2765  1.170     pooka 	struct vmspace *vm;
   2766  1.170     pooka 
   2767  1.253        ad 	KASSERT(mutex_owned(&proc_lock));
   2768  1.170     pooka 	KASSERT(mutex_owned(p->p_lock));
   2769  1.170     pooka 
   2770  1.170     pooka 	sigemptyset(&ss1);
   2771  1.170     pooka 	sigemptyset(&ss2);
   2772  1.170     pooka 
   2773  1.218  christos 	COND_SET_VALUE(ki->p_paddr, PTRTOUINT64(p), allowaddr);
   2774  1.218  christos 	COND_SET_VALUE(ki->p_fd, PTRTOUINT64(p->p_fd), allowaddr);
   2775  1.218  christos 	COND_SET_VALUE(ki->p_cwdi, PTRTOUINT64(p->p_cwdi), allowaddr);
   2776  1.218  christos 	COND_SET_VALUE(ki->p_stats, PTRTOUINT64(p->p_stats), allowaddr);
   2777  1.218  christos 	COND_SET_VALUE(ki->p_limit, PTRTOUINT64(p->p_limit), allowaddr);
   2778  1.218  christos 	COND_SET_VALUE(ki->p_vmspace, PTRTOUINT64(p->p_vmspace), allowaddr);
   2779  1.218  christos 	COND_SET_VALUE(ki->p_sigacts, PTRTOUINT64(p->p_sigacts), allowaddr);
   2780  1.218  christos 	COND_SET_VALUE(ki->p_sess, PTRTOUINT64(p->p_session), allowaddr);
   2781  1.170     pooka 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
   2782  1.218  christos 	COND_SET_VALUE(ki->p_ru, PTRTOUINT64(&p->p_stats->p_ru), allowaddr);
   2783  1.170     pooka 	ki->p_eflag = 0;
   2784  1.170     pooka 	ki->p_exitsig = p->p_exitsig;
   2785  1.170     pooka 	ki->p_flag = L_INMEM;   /* Process never swapped out */
   2786  1.170     pooka 	ki->p_flag |= sysctl_map_flags(sysctl_flagmap, p->p_flag);
   2787  1.170     pooka 	ki->p_flag |= sysctl_map_flags(sysctl_sflagmap, p->p_sflag);
   2788  1.170     pooka 	ki->p_flag |= sysctl_map_flags(sysctl_slflagmap, p->p_slflag);
   2789  1.170     pooka 	ki->p_flag |= sysctl_map_flags(sysctl_lflagmap, p->p_lflag);
   2790  1.170     pooka 	ki->p_flag |= sysctl_map_flags(sysctl_stflagmap, p->p_stflag);
   2791  1.170     pooka 	ki->p_pid = p->p_pid;
   2792  1.199       kre 	ki->p_ppid = p->p_ppid;
   2793  1.170     pooka 	ki->p_uid = kauth_cred_geteuid(p->p_cred);
   2794  1.170     pooka 	ki->p_ruid = kauth_cred_getuid(p->p_cred);
   2795  1.170     pooka 	ki->p_gid = kauth_cred_getegid(p->p_cred);
   2796  1.170     pooka 	ki->p_rgid = kauth_cred_getgid(p->p_cred);
   2797  1.170     pooka 	ki->p_svuid = kauth_cred_getsvuid(p->p_cred);
   2798  1.170     pooka 	ki->p_svgid = kauth_cred_getsvgid(p->p_cred);
   2799  1.170     pooka 	ki->p_ngroups = kauth_cred_ngroups(p->p_cred);
   2800  1.170     pooka 	kauth_cred_getgroups(p->p_cred, ki->p_groups,
   2801  1.214  riastrad 	    uimin(ki->p_ngroups, sizeof(ki->p_groups) / sizeof(ki->p_groups[0])),
   2802  1.170     pooka 	    UIO_SYSSPACE);
   2803  1.170     pooka 
   2804  1.170     pooka 	ki->p_uticks = p->p_uticks;
   2805  1.170     pooka 	ki->p_sticks = p->p_sticks;
   2806  1.170     pooka 	ki->p_iticks = p->p_iticks;
   2807  1.170     pooka 	ki->p_tpgid = NO_PGID;	/* may be changed if controlling tty below */
   2808  1.218  christos 	COND_SET_VALUE(ki->p_tracep, PTRTOUINT64(p->p_tracep), allowaddr);
   2809  1.170     pooka 	ki->p_traceflag = p->p_traceflag;
   2810  1.170     pooka 
   2811  1.170     pooka 	memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
   2812  1.170     pooka 	memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
   2813  1.170     pooka 
   2814  1.170     pooka 	ki->p_cpticks = 0;
   2815  1.170     pooka 	ki->p_pctcpu = p->p_pctcpu;
   2816  1.170     pooka 	ki->p_estcpu = 0;
   2817  1.170     pooka 	ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
   2818  1.170     pooka 	ki->p_realstat = p->p_stat;
   2819  1.170     pooka 	ki->p_nice = p->p_nice;
   2820  1.195  christos 	ki->p_xstat = P_WAITSTATUS(p);
   2821  1.170     pooka 	ki->p_acflag = p->p_acflag;
   2822  1.170     pooka 
   2823  1.170     pooka 	strncpy(ki->p_comm, p->p_comm,
   2824  1.214  riastrad 	    uimin(sizeof(ki->p_comm), sizeof(p->p_comm)));
   2825  1.170     pooka 	strncpy(ki->p_ename, p->p_emul->e_name, sizeof(ki->p_ename));
   2826  1.170     pooka 
   2827  1.170     pooka 	ki->p_nlwps = p->p_nlwps;
   2828  1.170     pooka 	ki->p_realflag = ki->p_flag;
   2829  1.170     pooka 
   2830  1.170     pooka 	if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
   2831  1.170     pooka 		vm = p->p_vmspace;
   2832  1.170     pooka 		ki->p_vm_rssize = vm_resident_count(vm);
   2833  1.170     pooka 		ki->p_vm_tsize = vm->vm_tsize;
   2834  1.170     pooka 		ki->p_vm_dsize = vm->vm_dsize;
   2835  1.170     pooka 		ki->p_vm_ssize = vm->vm_ssize;
   2836  1.184    martin 		ki->p_vm_vsize = atop(vm->vm_map.size);
   2837  1.170     pooka 		/*
   2838  1.170     pooka 		 * Since the stack is initially mapped mostly with
   2839  1.170     pooka 		 * PROT_NONE and grown as needed, adjust the "mapped size"
   2840  1.170     pooka 		 * to skip the unused stack portion.
   2841  1.170     pooka 		 */
   2842  1.170     pooka 		ki->p_vm_msize =
   2843  1.170     pooka 		    atop(vm->vm_map.size) - vm->vm_issize + vm->vm_ssize;
   2844  1.170     pooka 
   2845  1.170     pooka 		/* Pick the primary (first) LWP */
   2846  1.170     pooka 		l = proc_active_lwp(p);
   2847  1.170     pooka 		KASSERT(l != NULL);
   2848  1.170     pooka 		lwp_lock(l);
   2849  1.170     pooka 		ki->p_nrlwps = p->p_nrlwps;
   2850  1.170     pooka 		ki->p_forw = 0;
   2851  1.170     pooka 		ki->p_back = 0;
   2852  1.218  christos 		COND_SET_VALUE(ki->p_addr, PTRTOUINT64(l->l_addr), allowaddr);
   2853  1.170     pooka 		ki->p_stat = l->l_stat;
   2854  1.170     pooka 		ki->p_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
   2855  1.170     pooka 		ki->p_swtime = l->l_swtime;
   2856  1.170     pooka 		ki->p_slptime = l->l_slptime;
   2857  1.170     pooka 		if (l->l_stat == LSONPROC)
   2858  1.170     pooka 			ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2859  1.170     pooka 		else
   2860  1.170     pooka 			ki->p_schedflags = 0;
   2861  1.170     pooka 		ki->p_priority = lwp_eprio(l);
   2862  1.170     pooka 		ki->p_usrpri = l->l_priority;
   2863  1.170     pooka 		if (l->l_wchan)
   2864  1.170     pooka 			strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
   2865  1.218  christos 		COND_SET_VALUE(ki->p_wchan, PTRTOUINT64(l->l_wchan), allowaddr);
   2866  1.170     pooka 		ki->p_cpuid = cpu_index(l->l_cpu);
   2867  1.170     pooka 		lwp_unlock(l);
   2868  1.170     pooka 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2869  1.170     pooka 			/* This is hardly correct, but... */
   2870  1.170     pooka 			sigplusset(&l->l_sigpend.sp_set, &ss1);
   2871  1.170     pooka 			sigplusset(&l->l_sigmask, &ss2);
   2872  1.170     pooka 			ki->p_cpticks += l->l_cpticks;
   2873  1.170     pooka 			ki->p_pctcpu += l->l_pctcpu;
   2874  1.170     pooka 			ki->p_estcpu += l->l_estcpu;
   2875  1.170     pooka 		}
   2876  1.170     pooka 	}
   2877  1.237     kamil 	sigplusset(&p->p_sigpend.sp_set, &ss1);
   2878  1.170     pooka 	memcpy(&ki->p_siglist, &ss1, sizeof(ki_sigset_t));
   2879  1.170     pooka 	memcpy(&ki->p_sigmask, &ss2, sizeof(ki_sigset_t));
   2880  1.170     pooka 
   2881  1.170     pooka 	if (p->p_session != NULL) {
   2882  1.170     pooka 		ki->p_sid = p->p_session->s_sid;
   2883  1.170     pooka 		ki->p__pgid = p->p_pgrp->pg_id;
   2884  1.170     pooka 		if (p->p_session->s_ttyvp)
   2885  1.170     pooka 			ki->p_eflag |= EPROC_CTTY;
   2886  1.170     pooka 		if (SESS_LEADER(p))
   2887  1.170     pooka 			ki->p_eflag |= EPROC_SLEADER;
   2888  1.170     pooka 		strncpy(ki->p_login, p->p_session->s_login,
   2889  1.214  riastrad 		    uimin(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
   2890  1.170     pooka 		ki->p_jobc = p->p_pgrp->pg_jobc;
   2891  1.170     pooka 		if ((p->p_lflag & PL_CONTROLT) && (tp = p->p_session->s_ttyp)) {
   2892  1.170     pooka 			ki->p_tdev = tp->t_dev;
   2893  1.170     pooka 			ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2894  1.218  christos 			COND_SET_VALUE(ki->p_tsess, PTRTOUINT64(tp->t_session),
   2895  1.217      maxv 			    allowaddr);
   2896  1.170     pooka 		} else {
   2897  1.170     pooka 			ki->p_tdev = (int32_t)NODEV;
   2898  1.170     pooka 		}
   2899  1.170     pooka 	}
   2900  1.170     pooka 
   2901  1.170     pooka 	if (!P_ZOMBIE(p) && !zombie) {
   2902  1.170     pooka 		ki->p_uvalid = 1;
   2903  1.170     pooka 		ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
   2904  1.170     pooka 		ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
   2905  1.170     pooka 
   2906  1.170     pooka 		calcru(p, &ut, &st, NULL, &rt);
   2907  1.170     pooka 		ki->p_rtime_sec = rt.tv_sec;
   2908  1.170     pooka 		ki->p_rtime_usec = rt.tv_usec;
   2909  1.170     pooka 		ki->p_uutime_sec = ut.tv_sec;
   2910  1.170     pooka 		ki->p_uutime_usec = ut.tv_usec;
   2911  1.170     pooka 		ki->p_ustime_sec = st.tv_sec;
   2912  1.170     pooka 		ki->p_ustime_usec = st.tv_usec;
   2913  1.170     pooka 
   2914  1.170     pooka 		memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
   2915  1.272        ad 		rulwps(p, &ru);
   2916  1.272        ad 		ki->p_uru_nvcsw = ru.ru_nvcsw;
   2917  1.272        ad 		ki->p_uru_nivcsw = ru.ru_nivcsw;
   2918  1.170     pooka 		ki->p_uru_maxrss = ru.ru_maxrss;
   2919  1.170     pooka 		ki->p_uru_ixrss = ru.ru_ixrss;
   2920  1.170     pooka 		ki->p_uru_idrss = ru.ru_idrss;
   2921  1.170     pooka 		ki->p_uru_isrss = ru.ru_isrss;
   2922  1.170     pooka 		ki->p_uru_minflt = ru.ru_minflt;
   2923  1.170     pooka 		ki->p_uru_majflt = ru.ru_majflt;
   2924  1.170     pooka 		ki->p_uru_nswap = ru.ru_nswap;
   2925  1.170     pooka 		ki->p_uru_inblock = ru.ru_inblock;
   2926  1.170     pooka 		ki->p_uru_oublock = ru.ru_oublock;
   2927  1.170     pooka 		ki->p_uru_msgsnd = ru.ru_msgsnd;
   2928  1.170     pooka 		ki->p_uru_msgrcv = ru.ru_msgrcv;
   2929  1.170     pooka 		ki->p_uru_nsignals = ru.ru_nsignals;
   2930  1.170     pooka 
   2931  1.170     pooka 		timeradd(&p->p_stats->p_cru.ru_utime,
   2932  1.170     pooka 			 &p->p_stats->p_cru.ru_stime, &ut);
   2933  1.170     pooka 		ki->p_uctime_sec = ut.tv_sec;
   2934  1.170     pooka 		ki->p_uctime_usec = ut.tv_usec;
   2935  1.170     pooka 	}
   2936  1.170     pooka }
   2937  1.194  christos 
   2938  1.194  christos 
   2939  1.194  christos int
   2940  1.194  christos proc_find_locked(struct lwp *l, struct proc **p, pid_t pid)
   2941  1.194  christos {
   2942  1.194  christos 	int error;
   2943  1.194  christos 
   2944  1.253        ad 	mutex_enter(&proc_lock);
   2945  1.194  christos 	if (pid == -1)
   2946  1.194  christos 		*p = l->l_proc;
   2947  1.194  christos 	else
   2948  1.194  christos 		*p = proc_find(pid);
   2949  1.194  christos 
   2950  1.194  christos 	if (*p == NULL) {
   2951  1.194  christos 		if (pid != -1)
   2952  1.253        ad 			mutex_exit(&proc_lock);
   2953  1.194  christos 		return ESRCH;
   2954  1.194  christos 	}
   2955  1.194  christos 	if (pid != -1)
   2956  1.194  christos 		mutex_enter((*p)->p_lock);
   2957  1.253        ad 	mutex_exit(&proc_lock);
   2958  1.194  christos 
   2959  1.194  christos 	error = kauth_authorize_process(l->l_cred,
   2960  1.194  christos 	    KAUTH_PROCESS_CANSEE, *p,
   2961  1.194  christos 	    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
   2962  1.194  christos 	if (error) {
   2963  1.194  christos 		if (pid != -1)
   2964  1.194  christos 			mutex_exit((*p)->p_lock);
   2965  1.194  christos 	}
   2966  1.194  christos 	return error;
   2967  1.194  christos }
   2968  1.194  christos 
   2969  1.194  christos static int
   2970  1.194  christos fill_pathname(struct lwp *l, pid_t pid, void *oldp, size_t *oldlenp)
   2971  1.194  christos {
   2972  1.194  christos 	int error;
   2973  1.194  christos 	struct proc *p;
   2974  1.194  christos 
   2975  1.194  christos 	if ((error = proc_find_locked(l, &p, pid)) != 0)
   2976  1.194  christos 		return error;
   2977  1.194  christos 
   2978  1.208  christos 	if (p->p_path == NULL) {
   2979  1.194  christos 		if (pid != -1)
   2980  1.194  christos 			mutex_exit(p->p_lock);
   2981  1.194  christos 		return ENOENT;
   2982  1.194  christos 	}
   2983  1.194  christos 
   2984  1.208  christos 	size_t len = strlen(p->p_path) + 1;
   2985  1.194  christos 	if (oldp != NULL) {
   2986  1.219      maxv 		size_t copylen = uimin(len, *oldlenp);
   2987  1.219      maxv 		error = sysctl_copyout(l, p->p_path, oldp, copylen);
   2988  1.194  christos 		if (error == 0 && *oldlenp < len)
   2989  1.194  christos 			error = ENOSPC;
   2990  1.194  christos 	}
   2991  1.194  christos 	*oldlenp = len;
   2992  1.194  christos 	if (pid != -1)
   2993  1.194  christos 		mutex_exit(p->p_lock);
   2994  1.194  christos 	return error;
   2995  1.194  christos }
   2996  1.206  christos 
   2997  1.229     kamil static int
   2998  1.229     kamil fill_cwd(struct lwp *l, pid_t pid, void *oldp, size_t *oldlenp)
   2999  1.229     kamil {
   3000  1.229     kamil 	int error;
   3001  1.229     kamil 	struct proc *p;
   3002  1.229     kamil 	char *path;
   3003  1.229     kamil 	char *bp, *bend;
   3004  1.246        ad 	struct cwdinfo *cwdi;
   3005  1.229     kamil 	struct vnode *vp;
   3006  1.229     kamil 	size_t len, lenused;
   3007  1.229     kamil 
   3008  1.229     kamil 	if ((error = proc_find_locked(l, &p, pid)) != 0)
   3009  1.229     kamil 		return error;
   3010  1.229     kamil 
   3011  1.229     kamil 	len = MAXPATHLEN * 4;
   3012  1.229     kamil 
   3013  1.229     kamil 	path = kmem_alloc(len, KM_SLEEP);
   3014  1.229     kamil 
   3015  1.229     kamil 	bp = &path[len];
   3016  1.229     kamil 	bend = bp;
   3017  1.229     kamil 	*(--bp) = '\0';
   3018  1.229     kamil 
   3019  1.246        ad 	cwdi = p->p_cwdi;
   3020  1.246        ad 	rw_enter(&cwdi->cwdi_lock, RW_READER);
   3021  1.229     kamil 	vp = cwdi->cwdi_cdir;
   3022  1.231     kamil 	error = getcwd_common(vp, NULL, &bp, path, len/2, 0, l);
   3023  1.246        ad 	rw_exit(&cwdi->cwdi_lock);
   3024  1.229     kamil 
   3025  1.229     kamil 	if (error)
   3026  1.229     kamil 		goto out;
   3027  1.229     kamil 
   3028  1.229     kamil 	lenused = bend - bp;
   3029  1.229     kamil 
   3030  1.229     kamil 	if (oldp != NULL) {
   3031  1.230     kamil 		size_t copylen = uimin(lenused, *oldlenp);
   3032  1.230     kamil 		error = sysctl_copyout(l, bp, oldp, copylen);
   3033  1.229     kamil 		if (error == 0 && *oldlenp < lenused)
   3034  1.229     kamil 			error = ENOSPC;
   3035  1.229     kamil 	}
   3036  1.229     kamil 	*oldlenp = lenused;
   3037  1.229     kamil out:
   3038  1.229     kamil 	if (pid != -1)
   3039  1.229     kamil 		mutex_exit(p->p_lock);
   3040  1.229     kamil 	kmem_free(path, len);
   3041  1.229     kamil 	return error;
   3042  1.229     kamil }
   3043  1.229     kamil 
   3044  1.206  christos int
   3045  1.206  christos proc_getauxv(struct proc *p, void **buf, size_t *len)
   3046  1.206  christos {
   3047  1.206  christos 	struct ps_strings pss;
   3048  1.206  christos 	int error;
   3049  1.206  christos 	void *uauxv, *kauxv;
   3050  1.209      maxv 	size_t size;
   3051  1.206  christos 
   3052  1.206  christos 	if ((error = copyin_psstrings(p, &pss)) != 0)
   3053  1.206  christos 		return error;
   3054  1.209      maxv 	if (pss.ps_envstr == NULL)
   3055  1.209      maxv 		return EIO;
   3056  1.206  christos 
   3057  1.209      maxv 	size = p->p_execsw->es_arglen;
   3058  1.209      maxv 	if (size == 0)
   3059  1.206  christos 		return EIO;
   3060  1.206  christos 
   3061  1.206  christos 	size_t ptrsz = PROC_PTRSZ(p);
   3062  1.206  christos 	uauxv = (void *)((char *)pss.ps_envstr + (pss.ps_nenvstr + 1) * ptrsz);
   3063  1.206  christos 
   3064  1.206  christos 	kauxv = kmem_alloc(size, KM_SLEEP);
   3065  1.206  christos 
   3066  1.206  christos 	error = copyin_proc(p, uauxv, kauxv, size);
   3067  1.206  christos 	if (error) {
   3068  1.206  christos 		kmem_free(kauxv, size);
   3069  1.206  christos 		return error;
   3070  1.206  christos 	}
   3071  1.206  christos 
   3072  1.206  christos 	*buf = kauxv;
   3073  1.206  christos 	*len = size;
   3074  1.206  christos 
   3075  1.206  christos 	return 0;
   3076  1.206  christos }
   3077  1.222  christos 
   3078  1.222  christos 
   3079  1.222  christos static int
   3080  1.222  christos sysctl_security_expose_address(SYSCTLFN_ARGS)
   3081  1.222  christos {
   3082  1.222  christos 	int expose_address, error;
   3083  1.222  christos 	struct sysctlnode node;
   3084  1.222  christos 
   3085  1.222  christos 	node = *rnode;
   3086  1.222  christos 	node.sysctl_data = &expose_address;
   3087  1.222  christos 	expose_address = *(int *)rnode->sysctl_data;
   3088  1.222  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   3089  1.222  christos 	if (error || newp == NULL)
   3090  1.222  christos 		return error;
   3091  1.222  christos 
   3092  1.222  christos 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_KERNADDR,
   3093  1.222  christos 	    0, NULL, NULL, NULL))
   3094  1.222  christos 		return EPERM;
   3095  1.222  christos 
   3096  1.222  christos 	switch (expose_address) {
   3097  1.222  christos 	case 0:
   3098  1.222  christos 	case 1:
   3099  1.222  christos 	case 2:
   3100  1.222  christos 		break;
   3101  1.222  christos 	default:
   3102  1.222  christos 		return EINVAL;
   3103  1.222  christos 	}
   3104  1.222  christos 
   3105  1.222  christos 	*(int *)rnode->sysctl_data = expose_address;
   3106  1.222  christos 
   3107  1.222  christos 	return 0;
   3108  1.222  christos }
   3109  1.222  christos 
   3110  1.222  christos bool
   3111  1.222  christos get_expose_address(struct proc *p)
   3112  1.222  christos {
   3113  1.222  christos 	/* allow only if sysctl variable is set or privileged */
   3114  1.222  christos 	return kauth_authorize_process(kauth_cred_get(), KAUTH_PROCESS_CANSEE,
   3115  1.222  christos 	    p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_KPTR), NULL, NULL) == 0;
   3116  1.222  christos }
   3117