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