Home | History | Annotate | Line # | Download | only in kern
init_sysctl.c revision 1.170
      1  1.170       dsl /*	$NetBSD: init_sysctl.c,v 1.170 2009/12/12 17:29:34 dsl Exp $ */
      2    1.1    atatat 
      3    1.1    atatat /*-
      4  1.164        ad  * Copyright (c) 2003, 2007, 2008, 2009 The NetBSD Foundation, Inc.
      5    1.1    atatat  * All rights reserved.
      6    1.1    atatat  *
      7    1.1    atatat  * This code is derived from software contributed to The NetBSD Foundation
      8  1.118        ad  * by Andrew Brown, and by Andrew Doran.
      9    1.1    atatat  *
     10    1.1    atatat  * Redistribution and use in source and binary forms, with or without
     11    1.1    atatat  * modification, are permitted provided that the following conditions
     12    1.1    atatat  * are met:
     13    1.1    atatat  * 1. Redistributions of source code must retain the above copyright
     14    1.1    atatat  *    notice, this list of conditions and the following disclaimer.
     15    1.1    atatat  * 2. Redistributions in binary form must reproduce the above copyright
     16    1.1    atatat  *    notice, this list of conditions and the following disclaimer in the
     17    1.1    atatat  *    documentation and/or other materials provided with the distribution.
     18    1.1    atatat  *
     19    1.1    atatat  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20    1.1    atatat  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21    1.1    atatat  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22    1.1    atatat  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23    1.1    atatat  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24    1.1    atatat  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25    1.1    atatat  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26    1.1    atatat  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27    1.1    atatat  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28    1.1    atatat  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29    1.1    atatat  * POSSIBILITY OF SUCH DAMAGE.
     30    1.1    atatat  */
     31    1.1    atatat 
     32   1.16    atatat #include <sys/cdefs.h>
     33  1.170       dsl __KERNEL_RCSID(0, "$NetBSD: init_sysctl.c,v 1.170 2009/12/12 17:29:34 dsl Exp $");
     34   1.16    atatat 
     35    1.1    atatat #include "opt_sysv.h"
     36   1.82      manu #include "opt_compat_netbsd32.h"
     37  1.153  christos #include "opt_compat_netbsd.h"
     38  1.156       apb #include "opt_modular.h"
     39  1.145  wrstuden #include "opt_sa.h"
     40  1.150        ad #include "opt_posix.h"
     41    1.1    atatat #include "pty.h"
     42    1.1    atatat #include "rnd.h"
     43    1.1    atatat 
     44    1.1    atatat #include <sys/types.h>
     45    1.1    atatat #include <sys/param.h>
     46    1.1    atatat #include <sys/sysctl.h>
     47  1.104      yamt #include <sys/cpu.h>
     48    1.1    atatat #include <sys/errno.h>
     49    1.1    atatat #include <sys/systm.h>
     50    1.1    atatat #include <sys/kernel.h>
     51    1.1    atatat #include <sys/unistd.h>
     52    1.1    atatat #include <sys/disklabel.h>
     53    1.1    atatat #include <sys/rnd.h>
     54    1.1    atatat #include <sys/vnode.h>
     55    1.1    atatat #include <sys/mount.h>
     56    1.1    atatat #include <sys/namei.h>
     57    1.1    atatat #include <sys/msgbuf.h>
     58    1.1    atatat #include <dev/cons.h>
     59    1.1    atatat #include <sys/socketvar.h>
     60    1.1    atatat #include <sys/file.h>
     61   1.34    atatat #include <sys/filedesc.h>
     62    1.1    atatat #include <sys/tty.h>
     63  1.154      yamt #include <sys/kmem.h>
     64    1.1    atatat #include <sys/resource.h>
     65    1.1    atatat #include <sys/resourcevar.h>
     66    1.1    atatat #include <sys/exec.h>
     67    1.1    atatat #include <sys/conf.h>
     68    1.1    atatat #include <sys/device.h>
     69   1.61      elad #include <sys/stat.h>
     70   1.68      elad #include <sys/kauth.h>
     71   1.86      manu #include <sys/ktrace.h>
     72  1.149        ad #include <sys/ksem.h>
     73    1.1    atatat 
     74   1.82      manu #ifdef COMPAT_NETBSD32
     75   1.82      manu #include <compat/netbsd32/netbsd32.h>
     76   1.82      manu #endif
     77  1.153  christos #ifdef COMPAT_50
     78  1.153  christos #include <compat/sys/time.h>
     79  1.153  christos #endif
     80   1.82      manu 
     81  1.145  wrstuden #ifdef KERN_SA
     82  1.145  wrstuden #include <sys/sa.h>
     83  1.145  wrstuden #endif
     84  1.145  wrstuden 
     85  1.112        ad #include <sys/cpu.h>
     86    1.1    atatat 
     87  1.150        ad #if defined(MODULAR) || defined(P1003_1B_SEMAPHORE)
     88  1.150        ad int posix_semaphores = 200112;
     89  1.150        ad #else
     90  1.148        ad int posix_semaphores;
     91  1.150        ad #endif
     92  1.150        ad 
     93   1.61      elad int security_setidcore_dump;
     94   1.61      elad char security_setidcore_path[MAXPATHLEN] = "/var/crash/%n.core";
     95   1.61      elad uid_t security_setidcore_owner = 0;
     96   1.61      elad gid_t security_setidcore_group = 0;
     97   1.61      elad mode_t security_setidcore_mode = (S_IRUSR|S_IWUSR);
     98   1.54      elad 
     99   1.95        ad static const u_int sysctl_flagmap[] = {
    100   1.97     pavel 	PK_ADVLOCK, P_ADVLOCK,
    101   1.97     pavel 	PK_EXEC, P_EXEC,
    102   1.97     pavel 	PK_NOCLDWAIT, P_NOCLDWAIT,
    103   1.97     pavel 	PK_32, P_32,
    104   1.97     pavel 	PK_CLDSIGIGN, P_CLDSIGIGN,
    105   1.97     pavel 	PK_SUGID, P_SUGID,
    106   1.95        ad 	0
    107   1.95        ad };
    108   1.95        ad 
    109   1.95        ad static const u_int sysctl_sflagmap[] = {
    110   1.97     pavel 	PS_NOCLDSTOP, P_NOCLDSTOP,
    111   1.97     pavel 	PS_WEXIT, P_WEXIT,
    112   1.97     pavel 	PS_STOPFORK, P_STOPFORK,
    113   1.97     pavel 	PS_STOPEXEC, P_STOPEXEC,
    114   1.97     pavel 	PS_STOPEXIT, P_STOPEXIT,
    115   1.95        ad 	0
    116   1.95        ad };
    117   1.95        ad 
    118   1.95        ad static const u_int sysctl_slflagmap[] = {
    119   1.97     pavel 	PSL_TRACED, P_TRACED,
    120   1.97     pavel 	PSL_FSTRACE, P_FSTRACE,
    121   1.97     pavel 	PSL_CHTRACED, P_CHTRACED,
    122   1.97     pavel 	PSL_SYSCALL, P_SYSCALL,
    123   1.95        ad 	0
    124   1.95        ad };
    125   1.95        ad 
    126   1.95        ad static const u_int sysctl_lflagmap[] = {
    127   1.97     pavel 	PL_CONTROLT, P_CONTROLT,
    128  1.141        ad 	PL_PPWAIT, P_PPWAIT,
    129   1.95        ad 	0
    130   1.95        ad };
    131   1.95        ad 
    132   1.95        ad static const u_int sysctl_stflagmap[] = {
    133   1.97     pavel 	PST_PROFIL, P_PROFIL,
    134   1.95        ad 	0
    135   1.95        ad 
    136   1.95        ad };
    137   1.95        ad 
    138   1.95        ad static const u_int sysctl_lwpflagmap[] = {
    139   1.97     pavel 	LW_SINTR, P_SINTR,
    140   1.97     pavel 	LW_SYSTEM, P_SYSTEM,
    141  1.145  wrstuden 	LW_SA, P_SA,	/* WRS ??? */
    142   1.95        ad 	0
    143   1.95        ad };
    144   1.95        ad 
    145   1.95        ad static const u_int sysctl_lwpprflagmap[] = {
    146   1.97     pavel 	LPR_DETACHED, L_DETACHED,
    147   1.95        ad 	0
    148   1.95        ad };
    149   1.95        ad 
    150    1.1    atatat /*
    151    1.1    atatat  * try over estimating by 5 procs/lwps
    152    1.1    atatat  */
    153    1.1    atatat #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
    154    1.1    atatat #define KERN_LWPSLOP	(5 * sizeof(struct kinfo_lwp))
    155    1.1    atatat 
    156  1.105        ad static int dcopyout(struct lwp *, const void *, void *, size_t);
    157   1.86      manu 
    158  1.105        ad static int
    159  1.123      matt dcopyout(struct lwp *l, const void *kaddr, void *uaddr, size_t len)
    160   1.86      manu {
    161   1.86      manu 	int error;
    162   1.86      manu 
    163   1.86      manu 	error = copyout(kaddr, uaddr, len);
    164  1.105        ad 	ktrmibio(-1, UIO_READ, uaddr, len, error);
    165   1.86      manu 
    166   1.86      manu 	return error;
    167   1.86      manu }
    168   1.96        ad 
    169   1.64       erh #ifdef DIAGNOSTIC
    170   1.64       erh static int sysctl_kern_trigger_panic(SYSCTLFN_PROTO);
    171   1.64       erh #endif
    172    1.1    atatat static int sysctl_kern_maxvnodes(SYSCTLFN_PROTO);
    173   1.14    martin static int sysctl_kern_rtc_offset(SYSCTLFN_PROTO);
    174    1.1    atatat static int sysctl_kern_maxproc(SYSCTLFN_PROTO);
    175    1.1    atatat static int sysctl_kern_hostid(SYSCTLFN_PROTO);
    176   1.17    atatat static int sysctl_setlen(SYSCTLFN_PROTO);
    177    1.1    atatat static int sysctl_kern_clockrate(SYSCTLFN_PROTO);
    178    1.1    atatat static int sysctl_kern_file(SYSCTLFN_PROTO);
    179    1.1    atatat static int sysctl_msgbuf(SYSCTLFN_PROTO);
    180    1.1    atatat static int sysctl_kern_defcorename(SYSCTLFN_PROTO);
    181    1.1    atatat static int sysctl_kern_cptime(SYSCTLFN_PROTO);
    182    1.6        he #if NPTY > 0
    183    1.1    atatat static int sysctl_kern_maxptys(SYSCTLFN_PROTO);
    184    1.6        he #endif /* NPTY > 0 */
    185    1.1    atatat static int sysctl_kern_sbmax(SYSCTLFN_PROTO);
    186    1.1    atatat static int sysctl_kern_urnd(SYSCTLFN_PROTO);
    187   1.91  christos static int sysctl_kern_arnd(SYSCTLFN_PROTO);
    188    1.1    atatat static int sysctl_kern_lwp(SYSCTLFN_PROTO);
    189    1.1    atatat static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
    190    1.1    atatat static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
    191    1.1    atatat static int sysctl_kern_drivers(SYSCTLFN_PROTO);
    192   1.34    atatat static int sysctl_kern_file2(SYSCTLFN_PROTO);
    193   1.61      elad static int sysctl_security_setidcore(SYSCTLFN_PROTO);
    194   1.61      elad static int sysctl_security_setidcorename(SYSCTLFN_PROTO);
    195   1.45  christos static int sysctl_kern_cpid(SYSCTLFN_PROTO);
    196    1.1    atatat static int sysctl_doeproc(SYSCTLFN_PROTO);
    197    1.1    atatat static int sysctl_kern_proc_args(SYSCTLFN_PROTO);
    198    1.1    atatat static int sysctl_hw_usermem(SYSCTLFN_PROTO);
    199    1.1    atatat static int sysctl_hw_cnmagic(SYSCTLFN_PROTO);
    200    1.1    atatat 
    201   1.95        ad static u_int sysctl_map_flags(const u_int *, u_int);
    202  1.135        ad static void fill_kproc2(struct proc *, struct kinfo_proc2 *, bool);
    203    1.1    atatat static void fill_lwp(struct lwp *l, struct kinfo_lwp *kl);
    204  1.124        ad static void fill_file(struct kinfo_file *, const file_t *, const fdfile_t *,
    205  1.124        ad 		      int, pid_t);
    206    1.1    atatat 
    207    1.1    atatat /*
    208    1.1    atatat  * ********************************************************************
    209    1.1    atatat  * section 1: setup routines
    210    1.1    atatat  * ********************************************************************
    211  1.110  christos  * These functions are stuffed into a link set for sysctl setup
    212  1.110  christos  * functions. They're never called or referenced from anywhere else.
    213    1.1    atatat  * ********************************************************************
    214    1.1    atatat  */
    215    1.1    atatat 
    216    1.1    atatat /*
    217    1.1    atatat  * this setup routine is a replacement for kern_sysctl()
    218    1.1    atatat  */
    219    1.1    atatat SYSCTL_SETUP(sysctl_kern_setup, "sysctl kern subtree setup")
    220    1.1    atatat {
    221    1.1    atatat 	extern int kern_logsigexit;	/* defined in kern/kern_sig.c */
    222    1.1    atatat 	extern fixpt_t ccpu;		/* defined in kern/kern_synch.c */
    223    1.1    atatat 	extern int dumponpanic;		/* defined in kern/subr_prf.c */
    224   1.75      elad 	const struct sysctlnode *rnode;
    225    1.1    atatat 
    226   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    227   1.24    atatat 		       CTLFLAG_PERMANENT,
    228    1.1    atatat 		       CTLTYPE_NODE, "kern", NULL,
    229    1.1    atatat 		       NULL, 0, NULL, 0,
    230    1.1    atatat 		       CTL_KERN, CTL_EOL);
    231    1.1    atatat 
    232   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    233   1.24    atatat 		       CTLFLAG_PERMANENT,
    234   1.26    atatat 		       CTLTYPE_STRING, "ostype",
    235   1.26    atatat 		       SYSCTL_DESCR("Operating system type"),
    236    1.1    atatat 		       NULL, 0, &ostype, 0,
    237    1.1    atatat 		       CTL_KERN, KERN_OSTYPE, CTL_EOL);
    238   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    239   1.24    atatat 		       CTLFLAG_PERMANENT,
    240   1.26    atatat 		       CTLTYPE_STRING, "osrelease",
    241   1.26    atatat 		       SYSCTL_DESCR("Operating system release"),
    242    1.1    atatat 		       NULL, 0, &osrelease, 0,
    243    1.1    atatat 		       CTL_KERN, KERN_OSRELEASE, CTL_EOL);
    244   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    245   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    246   1.26    atatat 		       CTLTYPE_INT, "osrevision",
    247   1.26    atatat 		       SYSCTL_DESCR("Operating system revision"),
    248    1.1    atatat 		       NULL, __NetBSD_Version__, NULL, 0,
    249    1.1    atatat 		       CTL_KERN, KERN_OSREV, CTL_EOL);
    250   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    251   1.24    atatat 		       CTLFLAG_PERMANENT,
    252   1.26    atatat 		       CTLTYPE_STRING, "version",
    253   1.26    atatat 		       SYSCTL_DESCR("Kernel version"),
    254    1.1    atatat 		       NULL, 0, &version, 0,
    255    1.1    atatat 		       CTL_KERN, KERN_VERSION, CTL_EOL);
    256   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    257   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    258   1.26    atatat 		       CTLTYPE_INT, "maxvnodes",
    259   1.26    atatat 		       SYSCTL_DESCR("Maximum number of vnodes"),
    260    1.1    atatat 		       sysctl_kern_maxvnodes, 0, NULL, 0,
    261    1.1    atatat 		       CTL_KERN, KERN_MAXVNODES, CTL_EOL);
    262   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    263   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    264   1.26    atatat 		       CTLTYPE_INT, "maxproc",
    265   1.26    atatat 		       SYSCTL_DESCR("Maximum number of simultaneous processes"),
    266    1.1    atatat 		       sysctl_kern_maxproc, 0, NULL, 0,
    267    1.1    atatat 		       CTL_KERN, KERN_MAXPROC, CTL_EOL);
    268   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    269   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    270   1.26    atatat 		       CTLTYPE_INT, "maxfiles",
    271   1.26    atatat 		       SYSCTL_DESCR("Maximum number of open files"),
    272    1.1    atatat 		       NULL, 0, &maxfiles, 0,
    273    1.1    atatat 		       CTL_KERN, KERN_MAXFILES, CTL_EOL);
    274   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    275   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    276   1.26    atatat 		       CTLTYPE_INT, "argmax",
    277   1.26    atatat 		       SYSCTL_DESCR("Maximum number of bytes of arguments to "
    278   1.26    atatat 				    "execve(2)"),
    279    1.1    atatat 		       NULL, ARG_MAX, NULL, 0,
    280    1.1    atatat 		       CTL_KERN, KERN_ARGMAX, CTL_EOL);
    281   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    282   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    283   1.26    atatat 		       CTLTYPE_STRING, "hostname",
    284   1.26    atatat 		       SYSCTL_DESCR("System hostname"),
    285   1.17    atatat 		       sysctl_setlen, 0, &hostname, MAXHOSTNAMELEN,
    286    1.1    atatat 		       CTL_KERN, KERN_HOSTNAME, CTL_EOL);
    287   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    288   1.27    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
    289   1.26    atatat 		       CTLTYPE_INT, "hostid",
    290   1.26    atatat 		       SYSCTL_DESCR("System host ID number"),
    291    1.1    atatat 		       sysctl_kern_hostid, 0, NULL, 0,
    292    1.1    atatat 		       CTL_KERN, KERN_HOSTID, CTL_EOL);
    293   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    294   1.24    atatat 		       CTLFLAG_PERMANENT,
    295   1.26    atatat 		       CTLTYPE_STRUCT, "clockrate",
    296   1.26    atatat 		       SYSCTL_DESCR("Kernel clock rates"),
    297    1.1    atatat 		       sysctl_kern_clockrate, 0, NULL,
    298    1.1    atatat 		       sizeof(struct clockinfo),
    299    1.1    atatat 		       CTL_KERN, KERN_CLOCKRATE, CTL_EOL);
    300   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    301   1.24    atatat 		       CTLFLAG_PERMANENT,
    302   1.53    rpaulo 		       CTLTYPE_INT, "hardclock_ticks",
    303   1.53    rpaulo 		       SYSCTL_DESCR("Number of hardclock ticks"),
    304   1.53    rpaulo 		       NULL, 0, &hardclock_ticks, sizeof(hardclock_ticks),
    305   1.53    rpaulo 		       CTL_KERN, KERN_HARDCLOCK_TICKS, CTL_EOL);
    306   1.53    rpaulo 	sysctl_createv(clog, 0, NULL, NULL,
    307   1.53    rpaulo 		       CTLFLAG_PERMANENT,
    308   1.26    atatat 		       CTLTYPE_STRUCT, "vnode",
    309   1.26    atatat 		       SYSCTL_DESCR("System vnode table"),
    310    1.1    atatat 		       sysctl_kern_vnode, 0, NULL, 0,
    311    1.1    atatat 		       CTL_KERN, KERN_VNODE, CTL_EOL);
    312   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    313   1.24    atatat 		       CTLFLAG_PERMANENT,
    314   1.26    atatat 		       CTLTYPE_STRUCT, "file",
    315   1.26    atatat 		       SYSCTL_DESCR("System open file table"),
    316    1.1    atatat 		       sysctl_kern_file, 0, NULL, 0,
    317    1.1    atatat 		       CTL_KERN, KERN_FILE, CTL_EOL);
    318    1.1    atatat #ifndef GPROF
    319   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    320   1.24    atatat 		       CTLFLAG_PERMANENT,
    321   1.26    atatat 		       CTLTYPE_NODE, "profiling",
    322   1.26    atatat 		       SYSCTL_DESCR("Profiling information (not available)"),
    323    1.1    atatat 		       sysctl_notavail, 0, NULL, 0,
    324    1.1    atatat 		       CTL_KERN, KERN_PROF, CTL_EOL);
    325    1.1    atatat #endif
    326   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    327   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    328   1.26    atatat 		       CTLTYPE_INT, "posix1version",
    329   1.26    atatat 		       SYSCTL_DESCR("Version of ISO/IEC 9945 (POSIX 1003.1) "
    330   1.26    atatat 				    "with which the operating system attempts "
    331   1.26    atatat 				    "to comply"),
    332    1.1    atatat 		       NULL, _POSIX_VERSION, NULL, 0,
    333    1.1    atatat 		       CTL_KERN, KERN_POSIX1, CTL_EOL);
    334   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    335   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    336   1.26    atatat 		       CTLTYPE_INT, "ngroups",
    337   1.26    atatat 		       SYSCTL_DESCR("Maximum number of supplemental groups"),
    338    1.1    atatat 		       NULL, NGROUPS_MAX, NULL, 0,
    339    1.1    atatat 		       CTL_KERN, KERN_NGROUPS, CTL_EOL);
    340   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    341   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    342   1.26    atatat 		       CTLTYPE_INT, "job_control",
    343   1.26    atatat 		       SYSCTL_DESCR("Whether job control is available"),
    344    1.1    atatat 		       NULL, 1, NULL, 0,
    345    1.1    atatat 		       CTL_KERN, KERN_JOB_CONTROL, CTL_EOL);
    346   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    347   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    348   1.26    atatat 		       CTLTYPE_INT, "saved_ids",
    349   1.37       mrg 		       SYSCTL_DESCR("Whether POSIX saved set-group/user ID is "
    350   1.26    atatat 				    "available"), NULL,
    351    1.1    atatat #ifdef _POSIX_SAVED_IDS
    352    1.1    atatat 		       1,
    353    1.1    atatat #else /* _POSIX_SAVED_IDS */
    354    1.1    atatat 		       0,
    355    1.1    atatat #endif /* _POSIX_SAVED_IDS */
    356    1.1    atatat 		       NULL, 0, CTL_KERN, KERN_SAVED_IDS, CTL_EOL);
    357   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    358  1.166       apb 		       CTLFLAG_PERMANENT|CTLFLAG_HEX,
    359  1.166       apb 		       CTLTYPE_INT, "boothowto",
    360  1.166       apb 		       SYSCTL_DESCR("Flags from boot loader"),
    361  1.166       apb 		       NULL, 0, &boothowto, sizeof(boothowto),
    362  1.166       apb 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    363  1.166       apb 	sysctl_createv(clog, 0, NULL, NULL,
    364   1.24    atatat 		       CTLFLAG_PERMANENT,
    365   1.26    atatat 		       CTLTYPE_STRUCT, "boottime",
    366   1.26    atatat 		       SYSCTL_DESCR("System boot time"),
    367    1.1    atatat 		       NULL, 0, &boottime, sizeof(boottime),
    368    1.1    atatat 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
    369  1.153  christos #ifdef COMPAT_50
    370  1.153  christos 	{
    371  1.153  christos 		extern struct timeval50 boottime50;
    372  1.153  christos 		sysctl_createv(clog, 0, NULL, NULL,
    373  1.153  christos 			       CTLFLAG_PERMANENT,
    374  1.153  christos 			       CTLTYPE_STRUCT, "oboottime",
    375  1.153  christos 			       SYSCTL_DESCR("System boot time"),
    376  1.153  christos 			       NULL, 0, &boottime50, sizeof(boottime50),
    377  1.153  christos 			       CTL_KERN, KERN_OBOOTTIME, CTL_EOL);
    378  1.153  christos 	}
    379  1.153  christos #endif
    380   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    381   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    382   1.26    atatat 		       CTLTYPE_STRING, "domainname",
    383   1.26    atatat 		       SYSCTL_DESCR("YP domain name"),
    384   1.17    atatat 		       sysctl_setlen, 0, &domainname, MAXHOSTNAMELEN,
    385    1.1    atatat 		       CTL_KERN, KERN_DOMAINNAME, CTL_EOL);
    386   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    387   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    388   1.26    atatat 		       CTLTYPE_INT, "maxpartitions",
    389   1.26    atatat 		       SYSCTL_DESCR("Maximum number of partitions allowed per "
    390   1.26    atatat 				    "disk"),
    391    1.1    atatat 		       NULL, MAXPARTITIONS, NULL, 0,
    392    1.1    atatat 		       CTL_KERN, KERN_MAXPARTITIONS, CTL_EOL);
    393   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    394   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    395   1.26    atatat 		       CTLTYPE_INT, "rawpartition",
    396   1.26    atatat 		       SYSCTL_DESCR("Raw partition of a disk"),
    397    1.1    atatat 		       NULL, RAW_PART, NULL, 0,
    398    1.1    atatat 		       CTL_KERN, KERN_RAWPARTITION, CTL_EOL);
    399   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    400   1.24    atatat 		       CTLFLAG_PERMANENT,
    401    1.1    atatat 		       CTLTYPE_STRUCT, "timex", NULL,
    402    1.1    atatat 		       sysctl_notavail, 0, NULL, 0,
    403    1.1    atatat 		       CTL_KERN, KERN_TIMEX, CTL_EOL);
    404   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    405   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    406   1.26    atatat 		       CTLTYPE_INT, "rtc_offset",
    407   1.26    atatat 		       SYSCTL_DESCR("Offset of real time clock from UTC in "
    408   1.26    atatat 				    "minutes"),
    409   1.14    martin 		       sysctl_kern_rtc_offset, 0, &rtc_offset, 0,
    410    1.1    atatat 		       CTL_KERN, KERN_RTC_OFFSET, CTL_EOL);
    411   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    412   1.24    atatat 		       CTLFLAG_PERMANENT,
    413   1.26    atatat 		       CTLTYPE_STRING, "root_device",
    414   1.26    atatat 		       SYSCTL_DESCR("Name of the root device"),
    415    1.1    atatat 		       sysctl_root_device, 0, NULL, 0,
    416    1.1    atatat 		       CTL_KERN, KERN_ROOT_DEVICE, CTL_EOL);
    417   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    418   1.24    atatat 		       CTLFLAG_PERMANENT,
    419   1.26    atatat 		       CTLTYPE_INT, "msgbufsize",
    420   1.26    atatat 		       SYSCTL_DESCR("Size of the kernel message buffer"),
    421   1.31    atatat 		       sysctl_msgbuf, 0, NULL, 0,
    422    1.1    atatat 		       CTL_KERN, KERN_MSGBUFSIZE, CTL_EOL);
    423   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    424   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    425   1.26    atatat 		       CTLTYPE_INT, "fsync",
    426   1.26    atatat 		       SYSCTL_DESCR("Whether the POSIX 1003.1b File "
    427   1.26    atatat 				    "Synchronization Option is available on "
    428   1.26    atatat 				    "this system"),
    429    1.1    atatat 		       NULL, 1, NULL, 0,
    430    1.1    atatat 		       CTL_KERN, KERN_FSYNC, CTL_EOL);
    431   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    432   1.92  christos 		       CTLFLAG_PERMANENT,
    433   1.92  christos 		       CTLTYPE_NODE, "ipc",
    434   1.92  christos 		       SYSCTL_DESCR("SysV IPC options"),
    435   1.92  christos 		       NULL, 0, NULL, 0,
    436   1.92  christos 		       CTL_KERN, KERN_SYSVIPC, CTL_EOL);
    437   1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    438   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    439   1.26    atatat 		       CTLTYPE_INT, "sysvmsg",
    440   1.26    atatat 		       SYSCTL_DESCR("System V style message support available"),
    441   1.26    atatat 		       NULL,
    442    1.1    atatat #ifdef SYSVMSG
    443    1.1    atatat 		       1,
    444    1.1    atatat #else /* SYSVMSG */
    445    1.1    atatat 		       0,
    446    1.1    atatat #endif /* SYSVMSG */
    447   1.92  christos 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_MSG, CTL_EOL);
    448   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    449   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    450   1.26    atatat 		       CTLTYPE_INT, "sysvsem",
    451   1.26    atatat 		       SYSCTL_DESCR("System V style semaphore support "
    452   1.26    atatat 				    "available"), NULL,
    453    1.1    atatat #ifdef SYSVSEM
    454    1.1    atatat 		       1,
    455    1.1    atatat #else /* SYSVSEM */
    456    1.1    atatat 		       0,
    457    1.1    atatat #endif /* SYSVSEM */
    458   1.92  christos 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SEM, CTL_EOL);
    459   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    460   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    461   1.26    atatat 		       CTLTYPE_INT, "sysvshm",
    462   1.26    atatat 		       SYSCTL_DESCR("System V style shared memory support "
    463   1.26    atatat 				    "available"), NULL,
    464    1.1    atatat #ifdef SYSVSHM
    465    1.1    atatat 		       1,
    466    1.1    atatat #else /* SYSVSHM */
    467    1.1    atatat 		       0,
    468    1.1    atatat #endif /* SYSVSHM */
    469   1.92  christos 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHM, CTL_EOL);
    470   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    471   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    472   1.26    atatat 		       CTLTYPE_INT, "synchronized_io",
    473   1.26    atatat 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Synchronized "
    474   1.26    atatat 				    "I/O Option is available on this system"),
    475    1.1    atatat 		       NULL, 1, NULL, 0,
    476    1.1    atatat 		       CTL_KERN, KERN_SYNCHRONIZED_IO, CTL_EOL);
    477   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    478   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    479   1.26    atatat 		       CTLTYPE_INT, "iov_max",
    480   1.26    atatat 		       SYSCTL_DESCR("Maximum number of iovec structures per "
    481   1.26    atatat 				    "process"),
    482    1.1    atatat 		       NULL, IOV_MAX, NULL, 0,
    483    1.1    atatat 		       CTL_KERN, KERN_IOV_MAX, CTL_EOL);
    484   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    485   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    486   1.26    atatat 		       CTLTYPE_INT, "mapped_files",
    487   1.26    atatat 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory Mapped "
    488   1.26    atatat 				    "Files Option is available on this system"),
    489    1.1    atatat 		       NULL, 1, NULL, 0,
    490    1.1    atatat 		       CTL_KERN, KERN_MAPPED_FILES, CTL_EOL);
    491   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    492   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    493   1.26    atatat 		       CTLTYPE_INT, "memlock",
    494   1.26    atatat 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Process Memory "
    495   1.26    atatat 				    "Locking Option is available on this "
    496   1.26    atatat 				    "system"),
    497    1.1    atatat 		       NULL, 1, NULL, 0,
    498    1.1    atatat 		       CTL_KERN, KERN_MEMLOCK, CTL_EOL);
    499   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    500   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    501   1.26    atatat 		       CTLTYPE_INT, "memlock_range",
    502   1.26    atatat 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Range Memory "
    503   1.26    atatat 				    "Locking Option is available on this "
    504   1.26    atatat 				    "system"),
    505    1.1    atatat 		       NULL, 1, NULL, 0,
    506    1.1    atatat 		       CTL_KERN, KERN_MEMLOCK_RANGE, CTL_EOL);
    507   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    508   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    509   1.26    atatat 		       CTLTYPE_INT, "memory_protection",
    510   1.26    atatat 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory "
    511   1.26    atatat 				    "Protection Option is available on this "
    512   1.26    atatat 				    "system"),
    513    1.1    atatat 		       NULL, 1, NULL, 0,
    514    1.1    atatat 		       CTL_KERN, KERN_MEMORY_PROTECTION, CTL_EOL);
    515   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    516   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    517   1.26    atatat 		       CTLTYPE_INT, "login_name_max",
    518   1.26    atatat 		       SYSCTL_DESCR("Maximum login name length"),
    519    1.1    atatat 		       NULL, LOGIN_NAME_MAX, NULL, 0,
    520    1.1    atatat 		       CTL_KERN, KERN_LOGIN_NAME_MAX, CTL_EOL);
    521   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    522   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    523   1.26    atatat 		       CTLTYPE_STRING, "defcorename",
    524   1.26    atatat 		       SYSCTL_DESCR("Default core file name"),
    525    1.1    atatat 		       sysctl_kern_defcorename, 0, defcorename, MAXPATHLEN,
    526    1.1    atatat 		       CTL_KERN, KERN_DEFCORENAME, CTL_EOL);
    527   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    528   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    529   1.26    atatat 		       CTLTYPE_INT, "logsigexit",
    530   1.26    atatat 		       SYSCTL_DESCR("Log process exit when caused by signals"),
    531    1.1    atatat 		       NULL, 0, &kern_logsigexit, 0,
    532    1.1    atatat 		       CTL_KERN, KERN_LOGSIGEXIT, CTL_EOL);
    533   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    534   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    535   1.26    atatat 		       CTLTYPE_INT, "fscale",
    536   1.26    atatat 		       SYSCTL_DESCR("Kernel fixed-point scale factor"),
    537    1.1    atatat 		       NULL, FSCALE, NULL, 0,
    538    1.1    atatat 		       CTL_KERN, KERN_FSCALE, CTL_EOL);
    539   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    540   1.24    atatat 		       CTLFLAG_PERMANENT,
    541   1.26    atatat 		       CTLTYPE_INT, "ccpu",
    542   1.26    atatat 		       SYSCTL_DESCR("Scheduler exponential decay value"),
    543    1.1    atatat 		       NULL, 0, &ccpu, 0,
    544    1.1    atatat 		       CTL_KERN, KERN_CCPU, CTL_EOL);
    545   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    546   1.24    atatat 		       CTLFLAG_PERMANENT,
    547   1.26    atatat 		       CTLTYPE_STRUCT, "cp_time",
    548   1.26    atatat 		       SYSCTL_DESCR("Clock ticks spent in different CPU states"),
    549    1.1    atatat 		       sysctl_kern_cptime, 0, NULL, 0,
    550    1.1    atatat 		       CTL_KERN, KERN_CP_TIME, CTL_EOL);
    551   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    552   1.24    atatat 		       CTLFLAG_PERMANENT,
    553   1.26    atatat 		       CTLTYPE_INT, "msgbuf",
    554   1.26    atatat 		       SYSCTL_DESCR("Kernel message buffer"),
    555    1.1    atatat 		       sysctl_msgbuf, 0, NULL, 0,
    556    1.1    atatat 		       CTL_KERN, KERN_MSGBUF, CTL_EOL);
    557   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    558   1.24    atatat 		       CTLFLAG_PERMANENT,
    559   1.26    atatat 		       CTLTYPE_STRUCT, "consdev",
    560   1.26    atatat 		       SYSCTL_DESCR("Console device"),
    561    1.1    atatat 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
    562    1.1    atatat 		       CTL_KERN, KERN_CONSDEV, CTL_EOL);
    563    1.1    atatat #if NPTY > 0
    564   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    565   1.24    atatat 		       CTLFLAG_PERMANENT,
    566   1.26    atatat 		       CTLTYPE_INT, "maxptys",
    567   1.26    atatat 		       SYSCTL_DESCR("Maximum number of pseudo-ttys"),
    568    1.1    atatat 		       sysctl_kern_maxptys, 0, NULL, 0,
    569    1.1    atatat 		       CTL_KERN, KERN_MAXPTYS, CTL_EOL);
    570    1.1    atatat #endif /* NPTY > 0 */
    571   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    572   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    573   1.26    atatat 		       CTLTYPE_INT, "maxphys",
    574   1.26    atatat 		       SYSCTL_DESCR("Maximum raw I/O transfer size"),
    575    1.1    atatat 		       NULL, MAXPHYS, NULL, 0,
    576    1.1    atatat 		       CTL_KERN, KERN_MAXPHYS, CTL_EOL);
    577   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    578   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    579   1.26    atatat 		       CTLTYPE_INT, "sbmax",
    580   1.26    atatat 		       SYSCTL_DESCR("Maximum socket buffer size"),
    581    1.1    atatat 		       sysctl_kern_sbmax, 0, NULL, 0,
    582    1.1    atatat 		       CTL_KERN, KERN_SBMAX, CTL_EOL);
    583   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    584   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    585   1.26    atatat 		       CTLTYPE_INT, "monotonic_clock",
    586   1.26    atatat 		       SYSCTL_DESCR("Implementation version of the POSIX "
    587   1.26    atatat 				    "1003.1b Monotonic Clock Option"),
    588    1.1    atatat 		       /* XXX _POSIX_VERSION */
    589    1.1    atatat 		       NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0,
    590    1.1    atatat 		       CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL);
    591   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    592   1.24    atatat 		       CTLFLAG_PERMANENT,
    593   1.26    atatat 		       CTLTYPE_INT, "urandom",
    594   1.26    atatat 		       SYSCTL_DESCR("Random integer value"),
    595    1.1    atatat 		       sysctl_kern_urnd, 0, NULL, 0,
    596    1.1    atatat 		       CTL_KERN, KERN_URND, CTL_EOL);
    597   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    598   1.91  christos 		       CTLFLAG_PERMANENT,
    599   1.91  christos 		       CTLTYPE_INT, "arandom",
    600   1.91  christos 		       SYSCTL_DESCR("n bytes of random data"),
    601   1.91  christos 		       sysctl_kern_arnd, 0, NULL, 0,
    602   1.91  christos 		       CTL_KERN, KERN_ARND, CTL_EOL);
    603   1.91  christos 	sysctl_createv(clog, 0, NULL, NULL,
    604   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    605   1.26    atatat 		       CTLTYPE_INT, "labelsector",
    606   1.26    atatat 		       SYSCTL_DESCR("Sector number containing the disklabel"),
    607    1.1    atatat 		       NULL, LABELSECTOR, NULL, 0,
    608    1.1    atatat 		       CTL_KERN, KERN_LABELSECTOR, CTL_EOL);
    609   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    610   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    611   1.26    atatat 		       CTLTYPE_INT, "labeloffset",
    612   1.26    atatat 		       SYSCTL_DESCR("Offset of the disklabel within the "
    613   1.26    atatat 				    "sector"),
    614    1.1    atatat 		       NULL, LABELOFFSET, NULL, 0,
    615    1.1    atatat 		       CTL_KERN, KERN_LABELOFFSET, CTL_EOL);
    616   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    617   1.24    atatat 		       CTLFLAG_PERMANENT,
    618   1.26    atatat 		       CTLTYPE_NODE, "lwp",
    619   1.26    atatat 		       SYSCTL_DESCR("System-wide LWP information"),
    620    1.1    atatat 		       sysctl_kern_lwp, 0, NULL, 0,
    621    1.1    atatat 		       CTL_KERN, KERN_LWP, CTL_EOL);
    622   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    623   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    624   1.26    atatat 		       CTLTYPE_INT, "forkfsleep",
    625   1.26    atatat 		       SYSCTL_DESCR("Milliseconds to sleep on fork failure due "
    626   1.26    atatat 				    "to process limits"),
    627    1.1    atatat 		       sysctl_kern_forkfsleep, 0, NULL, 0,
    628    1.1    atatat 		       CTL_KERN, KERN_FORKFSLEEP, CTL_EOL);
    629   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    630   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    631   1.26    atatat 		       CTLTYPE_INT, "posix_threads",
    632   1.26    atatat 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    633   1.26    atatat 				    "Threads option to which the system "
    634   1.26    atatat 				    "attempts to conform"),
    635    1.1    atatat 		       /* XXX _POSIX_VERSION */
    636    1.1    atatat 		       NULL, _POSIX_THREADS, NULL, 0,
    637    1.1    atatat 		       CTL_KERN, KERN_POSIX_THREADS, CTL_EOL);
    638   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    639  1.148        ad 		       CTLFLAG_PERMANENT,
    640   1.26    atatat 		       CTLTYPE_INT, "posix_semaphores",
    641   1.26    atatat 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    642   1.26    atatat 				    "Semaphores option to which the system "
    643   1.26    atatat 				    "attempts to conform"), NULL,
    644  1.148        ad 		       0, &posix_semaphores,
    645  1.148        ad 		       0, CTL_KERN, KERN_POSIX_SEMAPHORES, CTL_EOL);
    646   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    647   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    648   1.26    atatat 		       CTLTYPE_INT, "posix_barriers",
    649   1.26    atatat 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    650   1.26    atatat 				    "Barriers option to which the system "
    651   1.26    atatat 				    "attempts to conform"),
    652    1.1    atatat 		       /* XXX _POSIX_VERSION */
    653    1.1    atatat 		       NULL, _POSIX_BARRIERS, NULL, 0,
    654    1.1    atatat 		       CTL_KERN, KERN_POSIX_BARRIERS, CTL_EOL);
    655   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    656   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    657   1.26    atatat 		       CTLTYPE_INT, "posix_timers",
    658   1.26    atatat 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    659   1.26    atatat 				    "Timers option to which the system "
    660   1.26    atatat 				    "attempts to conform"),
    661    1.1    atatat 		       /* XXX _POSIX_VERSION */
    662    1.1    atatat 		       NULL, _POSIX_TIMERS, NULL, 0,
    663    1.1    atatat 		       CTL_KERN, KERN_POSIX_TIMERS, CTL_EOL);
    664   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    665   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    666   1.26    atatat 		       CTLTYPE_INT, "posix_spin_locks",
    667   1.26    atatat 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its Spin "
    668   1.26    atatat 				    "Locks option to which the system attempts "
    669   1.26    atatat 				    "to conform"),
    670    1.1    atatat 		       /* XXX _POSIX_VERSION */
    671    1.1    atatat 		       NULL, _POSIX_SPIN_LOCKS, NULL, 0,
    672    1.1    atatat 		       CTL_KERN, KERN_POSIX_SPIN_LOCKS, CTL_EOL);
    673   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    674   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    675   1.26    atatat 		       CTLTYPE_INT, "posix_reader_writer_locks",
    676   1.26    atatat 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    677   1.26    atatat 				    "Read-Write Locks option to which the "
    678   1.26    atatat 				    "system attempts to conform"),
    679    1.1    atatat 		       /* XXX _POSIX_VERSION */
    680    1.1    atatat 		       NULL, _POSIX_READER_WRITER_LOCKS, NULL, 0,
    681    1.1    atatat 		       CTL_KERN, KERN_POSIX_READER_WRITER_LOCKS, CTL_EOL);
    682   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    683   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    684   1.26    atatat 		       CTLTYPE_INT, "dump_on_panic",
    685   1.26    atatat 		       SYSCTL_DESCR("Perform a crash dump on system panic"),
    686    1.1    atatat 		       NULL, 0, &dumponpanic, 0,
    687    1.1    atatat 		       CTL_KERN, KERN_DUMP_ON_PANIC, CTL_EOL);
    688   1.64       erh #ifdef DIAGNOSTIC
    689   1.64       erh 	sysctl_createv(clog, 0, NULL, NULL,
    690   1.64       erh 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    691   1.64       erh 		       CTLTYPE_INT, "panic_now",
    692   1.64       erh 		       SYSCTL_DESCR("Trigger a panic"),
    693   1.64       erh 		       sysctl_kern_trigger_panic, 0, NULL, 0,
    694   1.64       erh 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    695   1.64       erh #endif
    696   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    697   1.24    atatat 		       CTLFLAG_PERMANENT,
    698   1.26    atatat 		       CTLTYPE_INT, "root_partition",
    699   1.26    atatat 		       SYSCTL_DESCR("Root partition on the root device"),
    700    1.1    atatat 		       sysctl_kern_root_partition, 0, NULL, 0,
    701    1.1    atatat 		       CTL_KERN, KERN_ROOT_PARTITION, CTL_EOL);
    702   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    703   1.24    atatat 		       CTLFLAG_PERMANENT,
    704   1.26    atatat 		       CTLTYPE_STRUCT, "drivers",
    705   1.26    atatat 		       SYSCTL_DESCR("List of all drivers with block and "
    706   1.26    atatat 				    "character device numbers"),
    707    1.1    atatat 		       sysctl_kern_drivers, 0, NULL, 0,
    708    1.1    atatat 		       CTL_KERN, KERN_DRIVERS, CTL_EOL);
    709   1.34    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    710   1.34    atatat 		       CTLFLAG_PERMANENT,
    711   1.36    atatat 		       CTLTYPE_STRUCT, "file2",
    712   1.34    atatat 		       SYSCTL_DESCR("System open file table"),
    713   1.34    atatat 		       sysctl_kern_file2, 0, NULL, 0,
    714   1.34    atatat 		       CTL_KERN, KERN_FILE2, CTL_EOL);
    715   1.45  christos 	sysctl_createv(clog, 0, NULL, NULL,
    716   1.45  christos 		       CTLFLAG_PERMANENT,
    717   1.45  christos 		       CTLTYPE_STRUCT, "cp_id",
    718   1.45  christos 		       SYSCTL_DESCR("Mapping of CPU number to CPU id"),
    719   1.45  christos 		       sysctl_kern_cpid, 0, NULL, 0,
    720   1.45  christos 		       CTL_KERN, KERN_CP_ID, CTL_EOL);
    721   1.75      elad 	sysctl_createv(clog, 0, NULL, &rnode,
    722   1.75      elad 		       CTLFLAG_PERMANENT,
    723   1.75      elad 		       CTLTYPE_NODE, "coredump",
    724   1.75      elad 		       SYSCTL_DESCR("Coredump settings."),
    725   1.75      elad 		       NULL, 0, NULL, 0,
    726   1.75      elad 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    727   1.75      elad 	sysctl_createv(clog, 0, &rnode, &rnode,
    728   1.75      elad 		       CTLFLAG_PERMANENT,
    729   1.75      elad 		       CTLTYPE_NODE, "setid",
    730   1.75      elad 		       SYSCTL_DESCR("Set-id processes' coredump settings."),
    731   1.75      elad 		       NULL, 0, NULL, 0,
    732   1.75      elad 		       CTL_CREATE, CTL_EOL);
    733   1.75      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    734   1.75      elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    735   1.75      elad 		       CTLTYPE_INT, "dump",
    736   1.75      elad 		       SYSCTL_DESCR("Allow set-id processes to dump core."),
    737   1.75      elad 		       sysctl_security_setidcore, 0, &security_setidcore_dump,
    738   1.75      elad 		       sizeof(security_setidcore_dump),
    739   1.75      elad 		       CTL_CREATE, CTL_EOL);
    740   1.75      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    741   1.75      elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    742   1.75      elad 		       CTLTYPE_STRING, "path",
    743   1.75      elad 		       SYSCTL_DESCR("Path pattern for set-id coredumps."),
    744   1.75      elad 		       sysctl_security_setidcorename, 0,
    745   1.75      elad 		       &security_setidcore_path,
    746   1.75      elad 		       sizeof(security_setidcore_path),
    747   1.75      elad 		       CTL_CREATE, CTL_EOL);
    748   1.75      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    749   1.75      elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    750   1.75      elad 		       CTLTYPE_INT, "owner",
    751   1.75      elad 		       SYSCTL_DESCR("Owner id for set-id processes' cores."),
    752   1.75      elad 		       sysctl_security_setidcore, 0, &security_setidcore_owner,
    753   1.75      elad 		       0,
    754   1.75      elad 		       CTL_CREATE, CTL_EOL);
    755   1.75      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    756   1.75      elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    757   1.75      elad 		       CTLTYPE_INT, "group",
    758   1.75      elad 		       SYSCTL_DESCR("Group id for set-id processes' cores."),
    759   1.75      elad 		       sysctl_security_setidcore, 0, &security_setidcore_group,
    760   1.75      elad 		       0,
    761   1.75      elad 		       CTL_CREATE, CTL_EOL);
    762   1.75      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    763   1.75      elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    764   1.75      elad 		       CTLTYPE_INT, "mode",
    765   1.75      elad 		       SYSCTL_DESCR("Mode for set-id processes' cores."),
    766   1.75      elad 		       sysctl_security_setidcore, 0, &security_setidcore_mode,
    767   1.75      elad 		       0,
    768   1.75      elad 		       CTL_CREATE, CTL_EOL);
    769  1.145  wrstuden #ifdef KERN_SA
    770  1.145  wrstuden 	sysctl_createv(clog, 0, NULL, NULL,
    771  1.145  wrstuden 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    772  1.145  wrstuden 		       CTLTYPE_INT, "no_sa_support",
    773  1.145  wrstuden 		       SYSCTL_DESCR("0 if the kernel supports SA, otherwise it doesn't"),
    774  1.145  wrstuden 		       NULL, 0, &sa_system_disabled, 0,
    775  1.145  wrstuden 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    776  1.145  wrstuden #else
    777  1.106     joerg 	sysctl_createv(clog, 0, NULL, NULL,
    778  1.106     joerg 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    779  1.106     joerg 		       CTLTYPE_INT, "no_sa_support",
    780  1.106     joerg 		       SYSCTL_DESCR("0 if the kernel supports SA, otherwise it doesn't"),
    781  1.106     joerg 		       NULL, 1, NULL, 0,
    782  1.106     joerg 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    783  1.145  wrstuden #endif
    784  1.150        ad 
    785  1.150        ad 	/* kern.posix. */
    786  1.150        ad 	sysctl_createv(clog, 0, NULL, &rnode,
    787  1.150        ad 			CTLFLAG_PERMANENT,
    788  1.150        ad 			CTLTYPE_NODE, "posix",
    789  1.150        ad 			SYSCTL_DESCR("POSIX options"),
    790  1.150        ad 			NULL, 0, NULL, 0,
    791  1.150        ad 			CTL_KERN, CTL_CREATE, CTL_EOL);
    792  1.150        ad 	sysctl_createv(clog, 0, &rnode, NULL,
    793  1.150        ad 			CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    794  1.150        ad 			CTLTYPE_INT, "semmax",
    795  1.150        ad 			SYSCTL_DESCR("Maximal number of semaphores"),
    796  1.150        ad 			NULL, 0, &ksem_max, 0,
    797  1.150        ad 			CTL_CREATE, CTL_EOL);
    798    1.1    atatat }
    799    1.1    atatat 
    800    1.1    atatat SYSCTL_SETUP(sysctl_kern_proc_setup,
    801    1.1    atatat 	     "sysctl kern.proc/proc2/proc_args subtree setup")
    802    1.1    atatat {
    803    1.1    atatat 
    804   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    805   1.24    atatat 		       CTLFLAG_PERMANENT,
    806    1.1    atatat 		       CTLTYPE_NODE, "kern", NULL,
    807    1.1    atatat 		       NULL, 0, NULL, 0,
    808    1.1    atatat 		       CTL_KERN, CTL_EOL);
    809    1.1    atatat 
    810   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    811   1.24    atatat 		       CTLFLAG_PERMANENT,
    812   1.26    atatat 		       CTLTYPE_NODE, "proc",
    813   1.26    atatat 		       SYSCTL_DESCR("System-wide process information"),
    814    1.1    atatat 		       sysctl_doeproc, 0, NULL, 0,
    815    1.1    atatat 		       CTL_KERN, KERN_PROC, CTL_EOL);
    816   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    817   1.24    atatat 		       CTLFLAG_PERMANENT,
    818   1.26    atatat 		       CTLTYPE_NODE, "proc2",
    819   1.26    atatat 		       SYSCTL_DESCR("Machine-independent process information"),
    820    1.1    atatat 		       sysctl_doeproc, 0, NULL, 0,
    821    1.1    atatat 		       CTL_KERN, KERN_PROC2, CTL_EOL);
    822   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    823   1.24    atatat 		       CTLFLAG_PERMANENT,
    824   1.26    atatat 		       CTLTYPE_NODE, "proc_args",
    825   1.26    atatat 		       SYSCTL_DESCR("Process argument information"),
    826    1.1    atatat 		       sysctl_kern_proc_args, 0, NULL, 0,
    827    1.1    atatat 		       CTL_KERN, KERN_PROC_ARGS, CTL_EOL);
    828    1.1    atatat 
    829    1.1    atatat 	/*
    830    1.1    atatat 	  "nodes" under these:
    831    1.1    atatat 
    832    1.1    atatat 	  KERN_PROC_ALL
    833    1.1    atatat 	  KERN_PROC_PID pid
    834    1.1    atatat 	  KERN_PROC_PGRP pgrp
    835    1.1    atatat 	  KERN_PROC_SESSION sess
    836    1.1    atatat 	  KERN_PROC_TTY tty
    837    1.1    atatat 	  KERN_PROC_UID uid
    838    1.1    atatat 	  KERN_PROC_RUID uid
    839    1.1    atatat 	  KERN_PROC_GID gid
    840    1.1    atatat 	  KERN_PROC_RGID gid
    841    1.1    atatat 
    842    1.1    atatat 	  all in all, probably not worth the effort...
    843    1.1    atatat 	*/
    844    1.1    atatat }
    845    1.1    atatat 
    846    1.1    atatat SYSCTL_SETUP(sysctl_hw_setup, "sysctl hw subtree setup")
    847    1.1    atatat {
    848    1.1    atatat 	u_int u;
    849    1.1    atatat 	u_quad_t q;
    850    1.1    atatat 
    851   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    852   1.24    atatat 		       CTLFLAG_PERMANENT,
    853    1.1    atatat 		       CTLTYPE_NODE, "hw", NULL,
    854    1.1    atatat 		       NULL, 0, NULL, 0,
    855    1.1    atatat 		       CTL_HW, CTL_EOL);
    856    1.1    atatat 
    857   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    858   1.24    atatat 		       CTLFLAG_PERMANENT,
    859   1.26    atatat 		       CTLTYPE_STRING, "machine",
    860   1.26    atatat 		       SYSCTL_DESCR("Machine class"),
    861    1.1    atatat 		       NULL, 0, machine, 0,
    862    1.1    atatat 		       CTL_HW, HW_MACHINE, CTL_EOL);
    863   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    864   1.24    atatat 		       CTLFLAG_PERMANENT,
    865   1.26    atatat 		       CTLTYPE_STRING, "model",
    866   1.26    atatat 		       SYSCTL_DESCR("Machine model"),
    867    1.1    atatat 		       NULL, 0, cpu_model, 0,
    868    1.1    atatat 		       CTL_HW, HW_MODEL, CTL_EOL);
    869   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    870   1.24    atatat 		       CTLFLAG_PERMANENT,
    871   1.26    atatat 		       CTLTYPE_INT, "ncpu",
    872  1.109        ad 		       SYSCTL_DESCR("Number of CPUs configured"),
    873  1.109        ad 		       NULL, 0, &ncpu, 0,
    874    1.1    atatat 		       CTL_HW, HW_NCPU, CTL_EOL);
    875   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    876   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    877   1.26    atatat 		       CTLTYPE_INT, "byteorder",
    878   1.26    atatat 		       SYSCTL_DESCR("System byte order"),
    879    1.1    atatat 		       NULL, BYTE_ORDER, NULL, 0,
    880    1.1    atatat 		       CTL_HW, HW_BYTEORDER, CTL_EOL);
    881    1.1    atatat 	u = ((u_int)physmem > (UINT_MAX / PAGE_SIZE)) ?
    882    1.1    atatat 		UINT_MAX : physmem * PAGE_SIZE;
    883   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    884   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    885   1.26    atatat 		       CTLTYPE_INT, "physmem",
    886   1.26    atatat 		       SYSCTL_DESCR("Bytes of physical memory"),
    887    1.1    atatat 		       NULL, u, NULL, 0,
    888    1.1    atatat 		       CTL_HW, HW_PHYSMEM, CTL_EOL);
    889   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    890   1.24    atatat 		       CTLFLAG_PERMANENT,
    891   1.26    atatat 		       CTLTYPE_INT, "usermem",
    892   1.26    atatat 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
    893    1.1    atatat 		       sysctl_hw_usermem, 0, NULL, 0,
    894    1.1    atatat 		       CTL_HW, HW_USERMEM, CTL_EOL);
    895   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    896   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    897   1.26    atatat 		       CTLTYPE_INT, "pagesize",
    898   1.26    atatat 		       SYSCTL_DESCR("Software page size"),
    899    1.1    atatat 		       NULL, PAGE_SIZE, NULL, 0,
    900    1.1    atatat 		       CTL_HW, HW_PAGESIZE, CTL_EOL);
    901   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    902   1.24    atatat 		       CTLFLAG_PERMANENT,
    903   1.26    atatat 		       CTLTYPE_STRING, "machine_arch",
    904   1.26    atatat 		       SYSCTL_DESCR("Machine CPU class"),
    905    1.1    atatat 		       NULL, 0, machine_arch, 0,
    906    1.1    atatat 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
    907   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    908   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    909   1.26    atatat 		       CTLTYPE_INT, "alignbytes",
    910   1.26    atatat 		       SYSCTL_DESCR("Alignment constraint for all possible "
    911   1.26    atatat 				    "data types"),
    912    1.1    atatat 		       NULL, ALIGNBYTES, NULL, 0,
    913    1.1    atatat 		       CTL_HW, HW_ALIGNBYTES, CTL_EOL);
    914   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    915   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
    916   1.26    atatat 		       CTLTYPE_STRING, "cnmagic",
    917   1.26    atatat 		       SYSCTL_DESCR("Console magic key sequence"),
    918    1.1    atatat 		       sysctl_hw_cnmagic, 0, NULL, CNS_LEN,
    919    1.1    atatat 		       CTL_HW, HW_CNMAGIC, CTL_EOL);
    920    1.1    atatat 	q = (u_quad_t)physmem * PAGE_SIZE;
    921   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    922   1.24    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    923   1.26    atatat 		       CTLTYPE_QUAD, "physmem64",
    924   1.26    atatat 		       SYSCTL_DESCR("Bytes of physical memory"),
    925    1.1    atatat 		       NULL, q, NULL, 0,
    926    1.1    atatat 		       CTL_HW, HW_PHYSMEM64, CTL_EOL);
    927   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    928   1.24    atatat 		       CTLFLAG_PERMANENT,
    929   1.26    atatat 		       CTLTYPE_QUAD, "usermem64",
    930   1.26    atatat 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
    931    1.1    atatat 		       sysctl_hw_usermem, 0, NULL, 0,
    932    1.1    atatat 		       CTL_HW, HW_USERMEM64, CTL_EOL);
    933  1.109        ad 	sysctl_createv(clog, 0, NULL, NULL,
    934  1.109        ad 		       CTLFLAG_PERMANENT,
    935  1.109        ad 		       CTLTYPE_INT, "ncpuonline",
    936  1.109        ad 		       SYSCTL_DESCR("Number of CPUs online"),
    937  1.109        ad 		       NULL, 0, &ncpuonline, 0,
    938  1.109        ad 		       CTL_HW, HW_NCPUONLINE, CTL_EOL);
    939    1.1    atatat }
    940    1.1    atatat 
    941    1.1    atatat #ifdef DEBUG
    942    1.1    atatat /*
    943    1.1    atatat  * Debugging related system variables.
    944    1.1    atatat  */
    945    1.1    atatat struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
    946    1.1    atatat struct ctldebug debug5, debug6, debug7, debug8, debug9;
    947    1.1    atatat struct ctldebug debug10, debug11, debug12, debug13, debug14;
    948    1.1    atatat struct ctldebug debug15, debug16, debug17, debug18, debug19;
    949    1.1    atatat static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
    950    1.1    atatat 	&debug0, &debug1, &debug2, &debug3, &debug4,
    951    1.1    atatat 	&debug5, &debug6, &debug7, &debug8, &debug9,
    952    1.1    atatat 	&debug10, &debug11, &debug12, &debug13, &debug14,
    953    1.1    atatat 	&debug15, &debug16, &debug17, &debug18, &debug19,
    954    1.1    atatat };
    955    1.1    atatat 
    956    1.1    atatat /*
    957    1.1    atatat  * this setup routine is a replacement for debug_sysctl()
    958    1.1    atatat  *
    959    1.1    atatat  * note that it creates several nodes per defined debug variable
    960    1.1    atatat  */
    961    1.1    atatat SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup")
    962    1.1    atatat {
    963    1.1    atatat 	struct ctldebug *cdp;
    964    1.1    atatat 	char nodename[20];
    965    1.1    atatat 	int i;
    966    1.1    atatat 
    967    1.1    atatat 	/*
    968    1.1    atatat 	 * two ways here:
    969    1.1    atatat 	 *
    970    1.1    atatat 	 * the "old" way (debug.name -> value) which was emulated by
    971    1.1    atatat 	 * the sysctl(8) binary
    972    1.1    atatat 	 *
    973    1.1    atatat 	 * the new way, which the sysctl(8) binary was actually using
    974    1.1    atatat 
    975    1.1    atatat 	 node	debug
    976    1.1    atatat 	 node	debug.0
    977  1.110  christos 	 string debug.0.name
    978    1.1    atatat 	 int	debug.0.value
    979    1.1    atatat 	 int	debug.name
    980    1.1    atatat 
    981    1.1    atatat 	 */
    982    1.1    atatat 
    983   1.24    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    984   1.24    atatat 		       CTLFLAG_PERMANENT,
    985    1.1    atatat 		       CTLTYPE_NODE, "debug", NULL,
    986    1.1    atatat 		       NULL, 0, NULL, 0,
    987    1.1    atatat 		       CTL_DEBUG, CTL_EOL);
    988    1.1    atatat 
    989    1.1    atatat 	for (i = 0; i < CTL_DEBUG_MAXID; i++) {
    990    1.1    atatat 		cdp = debugvars[i];
    991    1.1    atatat 		if (cdp->debugname == NULL || cdp->debugvar == NULL)
    992    1.1    atatat 			continue;
    993    1.1    atatat 
    994    1.1    atatat 		snprintf(nodename, sizeof(nodename), "debug%d", i);
    995   1.24    atatat 		sysctl_createv(clog, 0, NULL, NULL,
    996   1.24    atatat 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
    997    1.1    atatat 			       CTLTYPE_NODE, nodename, NULL,
    998    1.1    atatat 			       NULL, 0, NULL, 0,
    999    1.1    atatat 			       CTL_DEBUG, i, CTL_EOL);
   1000   1.24    atatat 		sysctl_createv(clog, 0, NULL, NULL,
   1001   1.24    atatat 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
   1002    1.1    atatat 			       CTLTYPE_STRING, "name", NULL,
   1003   1.40  christos 			       /*XXXUNCONST*/
   1004   1.40  christos 			       NULL, 0, __UNCONST(cdp->debugname), 0,
   1005    1.1    atatat 			       CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL);
   1006   1.24    atatat 		sysctl_createv(clog, 0, NULL, NULL,
   1007   1.24    atatat 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
   1008    1.1    atatat 			       CTLTYPE_INT, "value", NULL,
   1009    1.1    atatat 			       NULL, 0, cdp->debugvar, 0,
   1010    1.1    atatat 			       CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL);
   1011   1.24    atatat 		sysctl_createv(clog, 0, NULL, NULL,
   1012   1.24    atatat 			       CTLFLAG_PERMANENT,
   1013    1.1    atatat 			       CTLTYPE_INT, cdp->debugname, NULL,
   1014    1.1    atatat 			       NULL, 0, cdp->debugvar, 0,
   1015    1.1    atatat 			       CTL_DEBUG, CTL_CREATE, CTL_EOL);
   1016    1.1    atatat 	}
   1017    1.1    atatat }
   1018    1.1    atatat #endif /* DEBUG */
   1019    1.1    atatat 
   1020    1.1    atatat /*
   1021    1.1    atatat  * ********************************************************************
   1022    1.1    atatat  * section 2: private node-specific helper routines.
   1023    1.1    atatat  * ********************************************************************
   1024    1.1    atatat  */
   1025    1.1    atatat 
   1026   1.64       erh #ifdef DIAGNOSTIC
   1027   1.64       erh static int
   1028   1.64       erh sysctl_kern_trigger_panic(SYSCTLFN_ARGS)
   1029   1.64       erh {
   1030   1.64       erh 	int newtrig, error;
   1031   1.64       erh 	struct sysctlnode node;
   1032   1.64       erh 
   1033   1.64       erh 	newtrig = 0;
   1034   1.64       erh 	node = *rnode;
   1035   1.64       erh 	node.sysctl_data = &newtrig;
   1036   1.64       erh 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1037   1.64       erh 	if (error || newp == NULL)
   1038   1.64       erh 		return (error);
   1039   1.64       erh 
   1040   1.64       erh 	if (newtrig != 0)
   1041   1.64       erh 		panic("Panic triggered");
   1042   1.64       erh 
   1043   1.64       erh 	return (error);
   1044   1.64       erh }
   1045   1.64       erh #endif
   1046   1.64       erh 
   1047    1.1    atatat /*
   1048  1.110  christos  * sysctl helper routine for kern.maxvnodes.  Drain vnodes if
   1049   1.13      yamt  * new value is lower than desiredvnodes and then calls reinit
   1050    1.1    atatat  * routines that needs to adjust to the new value.
   1051    1.1    atatat  */
   1052    1.1    atatat static int
   1053    1.1    atatat sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
   1054    1.1    atatat {
   1055  1.142        ad 	int error, new_vnodes, old_vnodes, new_max;
   1056    1.1    atatat 	struct sysctlnode node;
   1057    1.1    atatat 
   1058    1.1    atatat 	new_vnodes = desiredvnodes;
   1059    1.1    atatat 	node = *rnode;
   1060    1.1    atatat 	node.sysctl_data = &new_vnodes;
   1061    1.1    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1062    1.1    atatat 	if (error || newp == NULL)
   1063    1.1    atatat 		return (error);
   1064    1.1    atatat 
   1065  1.142        ad 	/* Limits: 75% of KVA and physical memory. */
   1066  1.142        ad 	new_max = calc_cache_size(kernel_map, 75, 75) / VNODE_COST;
   1067  1.142        ad 	if (new_vnodes > new_max)
   1068  1.142        ad 		new_vnodes = new_max;
   1069  1.142        ad 
   1070   1.12      yamt 	old_vnodes = desiredvnodes;
   1071    1.1    atatat 	desiredvnodes = new_vnodes;
   1072   1.12      yamt 	if (new_vnodes < old_vnodes) {
   1073   1.58  christos 		error = vfs_drainvnodes(new_vnodes, l);
   1074   1.12      yamt 		if (error) {
   1075   1.12      yamt 			desiredvnodes = old_vnodes;
   1076   1.12      yamt 			return (error);
   1077   1.12      yamt 		}
   1078   1.12      yamt 	}
   1079    1.1    atatat 	vfs_reinit();
   1080    1.1    atatat 	nchreinit();
   1081    1.1    atatat 
   1082    1.1    atatat 	return (0);
   1083    1.1    atatat }
   1084    1.1    atatat 
   1085    1.1    atatat /*
   1086   1.14    martin  * sysctl helper routine for rtc_offset - set time after changes
   1087   1.14    martin  */
   1088   1.14    martin static int
   1089   1.14    martin sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
   1090   1.14    martin {
   1091   1.57  christos 	struct timespec ts, delta;
   1092   1.57  christos 	int error, new_rtc_offset;
   1093   1.14    martin 	struct sysctlnode node;
   1094   1.14    martin 
   1095   1.14    martin 	new_rtc_offset = rtc_offset;
   1096   1.14    martin 	node = *rnode;
   1097   1.14    martin 	node.sysctl_data = &new_rtc_offset;
   1098   1.14    martin 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1099   1.14    martin 	if (error || newp == NULL)
   1100   1.14    martin 		return (error);
   1101   1.14    martin 
   1102   1.83      elad 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
   1103   1.83      elad 	    KAUTH_REQ_SYSTEM_TIME_RTCOFFSET,
   1104  1.114      elad 	    KAUTH_ARG(new_rtc_offset), NULL, NULL))
   1105   1.14    martin 		return (EPERM);
   1106   1.14    martin 	if (rtc_offset == new_rtc_offset)
   1107   1.14    martin 		return (0);
   1108   1.14    martin 
   1109   1.14    martin 	/* if we change the offset, adjust the time */
   1110   1.57  christos 	nanotime(&ts);
   1111   1.57  christos 	delta.tv_sec = 60 * (new_rtc_offset - rtc_offset);
   1112   1.57  christos 	delta.tv_nsec = 0;
   1113   1.57  christos 	timespecadd(&ts, &delta, &ts);
   1114   1.14    martin 	rtc_offset = new_rtc_offset;
   1115  1.114      elad 	return (settime(l->l_proc, &ts));
   1116   1.14    martin }
   1117   1.14    martin 
   1118   1.14    martin /*
   1119  1.110  christos  * sysctl helper routine for kern.maxproc. Ensures that the new
   1120    1.1    atatat  * values are not too low or too high.
   1121    1.1    atatat  */
   1122    1.1    atatat static int
   1123    1.1    atatat sysctl_kern_maxproc(SYSCTLFN_ARGS)
   1124    1.1    atatat {
   1125    1.1    atatat 	int error, nmaxproc;
   1126    1.1    atatat 	struct sysctlnode node;
   1127    1.1    atatat 
   1128    1.1    atatat 	nmaxproc = maxproc;
   1129    1.1    atatat 	node = *rnode;
   1130    1.1    atatat 	node.sysctl_data = &nmaxproc;
   1131    1.1    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1132    1.1    atatat 	if (error || newp == NULL)
   1133    1.1    atatat 		return (error);
   1134    1.1    atatat 
   1135    1.1    atatat 	if (nmaxproc < 0 || nmaxproc >= PID_MAX)
   1136    1.1    atatat 		return (EINVAL);
   1137    1.1    atatat #ifdef __HAVE_CPU_MAXPROC
   1138    1.1    atatat 	if (nmaxproc > cpu_maxproc())
   1139    1.1    atatat 		return (EINVAL);
   1140    1.1    atatat #endif
   1141    1.1    atatat 	maxproc = nmaxproc;
   1142    1.1    atatat 
   1143    1.1    atatat 	return (0);
   1144    1.1    atatat }
   1145    1.1    atatat 
   1146    1.1    atatat /*
   1147  1.110  christos  * sysctl helper function for kern.hostid. The hostid is a long, but
   1148    1.1    atatat  * we export it as an int, so we need to give it a little help.
   1149    1.1    atatat  */
   1150    1.1    atatat static int
   1151    1.1    atatat sysctl_kern_hostid(SYSCTLFN_ARGS)
   1152    1.1    atatat {
   1153    1.1    atatat 	int error, inthostid;
   1154    1.1    atatat 	struct sysctlnode node;
   1155    1.1    atatat 
   1156   1.29    martin 	inthostid = hostid;  /* XXX assumes sizeof int <= sizeof long */
   1157    1.1    atatat 	node = *rnode;
   1158    1.1    atatat 	node.sysctl_data = &inthostid;
   1159    1.1    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1160    1.1    atatat 	if (error || newp == NULL)
   1161    1.1    atatat 		return (error);
   1162    1.1    atatat 
   1163   1.27    atatat 	hostid = (unsigned)inthostid;
   1164    1.1    atatat 
   1165    1.1    atatat 	return (0);
   1166    1.1    atatat }
   1167    1.1    atatat 
   1168    1.1    atatat /*
   1169   1.17    atatat  * sysctl helper function for kern.hostname and kern.domainnname.
   1170   1.17    atatat  * resets the relevant recorded length when the underlying name is
   1171   1.17    atatat  * changed.
   1172   1.15    martin  */
   1173   1.15    martin static int
   1174   1.17    atatat sysctl_setlen(SYSCTLFN_ARGS)
   1175   1.15    martin {
   1176   1.15    martin 	int error;
   1177   1.15    martin 
   1178   1.43    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
   1179   1.15    martin 	if (error || newp == NULL)
   1180   1.15    martin 		return (error);
   1181   1.15    martin 
   1182   1.17    atatat 	switch (rnode->sysctl_num) {
   1183   1.17    atatat 	case KERN_HOSTNAME:
   1184   1.17    atatat 		hostnamelen = strlen((const char*)rnode->sysctl_data);
   1185   1.17    atatat 		break;
   1186   1.17    atatat 	case KERN_DOMAINNAME:
   1187   1.17    atatat 		domainnamelen = strlen((const char*)rnode->sysctl_data);
   1188   1.17    atatat 		break;
   1189   1.17    atatat 	}
   1190   1.17    atatat 
   1191   1.15    martin 	return (0);
   1192   1.15    martin }
   1193   1.15    martin 
   1194   1.15    martin /*
   1195  1.110  christos  * sysctl helper routine for kern.clockrate. Assembles a struct on
   1196    1.1    atatat  * the fly to be returned to the caller.
   1197    1.1    atatat  */
   1198    1.1    atatat static int
   1199    1.1    atatat sysctl_kern_clockrate(SYSCTLFN_ARGS)
   1200    1.1    atatat {
   1201    1.1    atatat 	struct clockinfo clkinfo;
   1202    1.1    atatat 	struct sysctlnode node;
   1203    1.1    atatat 
   1204    1.1    atatat 	clkinfo.tick = tick;
   1205    1.1    atatat 	clkinfo.tickadj = tickadj;
   1206    1.1    atatat 	clkinfo.hz = hz;
   1207    1.1    atatat 	clkinfo.profhz = profhz;
   1208    1.1    atatat 	clkinfo.stathz = stathz ? stathz : hz;
   1209    1.1    atatat 
   1210    1.1    atatat 	node = *rnode;
   1211    1.1    atatat 	node.sysctl_data = &clkinfo;
   1212    1.1    atatat 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1213    1.1    atatat }
   1214    1.1    atatat 
   1215    1.1    atatat 
   1216    1.1    atatat /*
   1217    1.1    atatat  * sysctl helper routine for kern.file pseudo-subtree.
   1218    1.1    atatat  */
   1219    1.1    atatat static int
   1220    1.1    atatat sysctl_kern_file(SYSCTLFN_ARGS)
   1221    1.1    atatat {
   1222    1.1    atatat 	int error;
   1223    1.1    atatat 	size_t buflen;
   1224  1.118        ad 	struct file *fp, *dp, *np, fbuf;
   1225    1.1    atatat 	char *start, *where;
   1226    1.1    atatat 
   1227    1.1    atatat 	start = where = oldp;
   1228    1.1    atatat 	buflen = *oldlenp;
   1229  1.118        ad 	dp = NULL;
   1230  1.118        ad 
   1231    1.1    atatat 	if (where == NULL) {
   1232    1.1    atatat 		/*
   1233    1.1    atatat 		 * overestimate by 10 files
   1234    1.1    atatat 		 */
   1235  1.118        ad 		*oldlenp = sizeof(filehead) + (nfiles + 10) *
   1236  1.118        ad 		    sizeof(struct file);
   1237    1.1    atatat 		return (0);
   1238    1.1    atatat 	}
   1239    1.1    atatat 
   1240    1.1    atatat 	/*
   1241   1.86      manu 	 * first dcopyout filehead
   1242    1.1    atatat 	 */
   1243    1.1    atatat 	if (buflen < sizeof(filehead)) {
   1244    1.1    atatat 		*oldlenp = 0;
   1245    1.1    atatat 		return (0);
   1246    1.1    atatat 	}
   1247  1.118        ad 	sysctl_unlock();
   1248   1.86      manu 	error = dcopyout(l, &filehead, where, sizeof(filehead));
   1249  1.118        ad 	if (error) {
   1250  1.118        ad 	 	sysctl_relock();
   1251  1.118        ad 		return error;
   1252  1.118        ad 	}
   1253    1.1    atatat 	buflen -= sizeof(filehead);
   1254    1.1    atatat 	where += sizeof(filehead);
   1255    1.1    atatat 
   1256    1.1    atatat 	/*
   1257  1.118        ad 	 * allocate dummy file descriptor to make position in list
   1258  1.118        ad 	 */
   1259  1.118        ad 	if ((dp = fgetdummy()) == NULL) {
   1260  1.118        ad 	 	sysctl_relock();
   1261  1.118        ad 		return ENOMEM;
   1262  1.118        ad 	}
   1263  1.118        ad 
   1264  1.118        ad 	/*
   1265    1.1    atatat 	 * followed by an array of file structures
   1266    1.1    atatat 	 */
   1267  1.118        ad 	mutex_enter(&filelist_lock);
   1268  1.118        ad 	for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) {
   1269  1.121        ad 	    	np = LIST_NEXT(fp, f_list);
   1270  1.121        ad 	    	mutex_enter(&fp->f_lock);
   1271  1.121        ad 	    	if (fp->f_count == 0) {
   1272  1.121        ad 		    	mutex_exit(&fp->f_lock);
   1273  1.121        ad 	    		continue;
   1274  1.121        ad 		}
   1275  1.120      elad 		/*
   1276  1.120      elad 		 * XXX Need to prevent that from being an alternative way
   1277  1.120      elad 		 * XXX to getting process information.
   1278  1.120      elad 		 */
   1279   1.78        ad 		if (kauth_authorize_generic(l->l_cred,
   1280  1.118        ad 		    KAUTH_GENERIC_CANSEE, fp->f_cred) != 0) {
   1281  1.121        ad 		    	mutex_exit(&fp->f_lock);
   1282   1.55      elad 			continue;
   1283  1.118        ad 		}
   1284    1.1    atatat 		if (buflen < sizeof(struct file)) {
   1285    1.1    atatat 			*oldlenp = where - start;
   1286  1.122        ad 		    	mutex_exit(&fp->f_lock);
   1287  1.118        ad 			error = ENOMEM;
   1288  1.118        ad 			break;
   1289  1.118        ad 		}
   1290  1.118        ad 		memcpy(&fbuf, fp, sizeof(fbuf));
   1291  1.118        ad 		LIST_INSERT_AFTER(fp, dp, f_list);
   1292  1.121        ad 	    	mutex_exit(&fp->f_lock);
   1293  1.118        ad 		mutex_exit(&filelist_lock);
   1294  1.118        ad 		error = dcopyout(l, &fbuf, where, sizeof(fbuf));
   1295  1.118        ad 		if (error) {
   1296  1.118        ad 			mutex_enter(&filelist_lock);
   1297  1.118        ad 			LIST_REMOVE(dp, f_list);
   1298  1.118        ad 			break;
   1299    1.1    atatat 		}
   1300    1.1    atatat 		buflen -= sizeof(struct file);
   1301    1.1    atatat 		where += sizeof(struct file);
   1302  1.118        ad 		mutex_enter(&filelist_lock);
   1303  1.118        ad 		np = LIST_NEXT(dp, f_list);
   1304  1.118        ad 		LIST_REMOVE(dp, f_list);
   1305    1.1    atatat 	}
   1306  1.118        ad 	mutex_exit(&filelist_lock);
   1307    1.1    atatat 	*oldlenp = where - start;
   1308  1.118        ad  	if (dp != NULL)
   1309  1.118        ad 		fputdummy(dp);
   1310  1.118        ad  	sysctl_relock();
   1311  1.118        ad 	return (error);
   1312    1.1    atatat }
   1313    1.1    atatat 
   1314    1.1    atatat /*
   1315  1.110  christos  * sysctl helper routine for kern.msgbufsize and kern.msgbuf. For the
   1316  1.110  christos  * former it merely checks the message buffer is set up. For the latter,
   1317   1.28    simonb  * it also copies out the data if necessary.
   1318    1.1    atatat  */
   1319    1.1    atatat static int
   1320    1.1    atatat sysctl_msgbuf(SYSCTLFN_ARGS)
   1321    1.1    atatat {
   1322    1.1    atatat 	char *where = oldp;
   1323    1.1    atatat 	size_t len, maxlen;
   1324    1.1    atatat 	long beg, end;
   1325  1.118        ad 	extern kmutex_t log_lock;
   1326    1.1    atatat 	int error;
   1327    1.1    atatat 
   1328    1.1    atatat 	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
   1329    1.1    atatat 		msgbufenabled = 0;
   1330    1.1    atatat 		return (ENXIO);
   1331    1.1    atatat 	}
   1332    1.1    atatat 
   1333    1.1    atatat 	switch (rnode->sysctl_num) {
   1334   1.31    atatat 	case KERN_MSGBUFSIZE: {
   1335   1.31    atatat 		struct sysctlnode node = *rnode;
   1336   1.31    atatat 		int msg_bufs = (int)msgbufp->msg_bufs;
   1337   1.31    atatat 		node.sysctl_data = &msg_bufs;
   1338   1.31    atatat 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1339   1.31    atatat 	}
   1340    1.1    atatat 	case KERN_MSGBUF:
   1341    1.1    atatat 		break;
   1342    1.1    atatat 	default:
   1343    1.1    atatat 		return (EOPNOTSUPP);
   1344    1.1    atatat 	}
   1345    1.1    atatat 
   1346    1.1    atatat 	if (newp != NULL)
   1347    1.1    atatat 		return (EPERM);
   1348    1.1    atatat 
   1349  1.110  christos 	if (oldp == NULL) {
   1350    1.1    atatat 		/* always return full buffer size */
   1351    1.1    atatat 		*oldlenp = msgbufp->msg_bufs;
   1352    1.1    atatat 		return (0);
   1353  1.110  christos 	}
   1354    1.1    atatat 
   1355  1.118        ad 	sysctl_unlock();
   1356    1.1    atatat 
   1357    1.1    atatat 	/*
   1358    1.1    atatat 	 * First, copy from the write pointer to the end of
   1359    1.1    atatat 	 * message buffer.
   1360    1.1    atatat 	 */
   1361  1.118        ad 	error = 0;
   1362  1.118        ad 	mutex_spin_enter(&log_lock);
   1363  1.118        ad 	maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
   1364    1.1    atatat 	beg = msgbufp->msg_bufx;
   1365    1.1    atatat 	end = msgbufp->msg_bufs;
   1366  1.118        ad 	mutex_spin_exit(&log_lock);
   1367  1.118        ad 
   1368    1.1    atatat 	while (maxlen > 0) {
   1369    1.1    atatat 		len = MIN(end - beg, maxlen);
   1370    1.1    atatat 		if (len == 0)
   1371    1.1    atatat 			break;
   1372  1.118        ad 		/* XXX unlocked, but hardly matters. */
   1373   1.86      manu 		error = dcopyout(l, &msgbufp->msg_bufc[beg], where, len);
   1374    1.1    atatat 		if (error)
   1375    1.1    atatat 			break;
   1376    1.1    atatat 		where += len;
   1377    1.1    atatat 		maxlen -= len;
   1378    1.1    atatat 
   1379    1.1    atatat 		/*
   1380    1.1    atatat 		 * ... then, copy from the beginning of message buffer to
   1381    1.1    atatat 		 * the write pointer.
   1382    1.1    atatat 		 */
   1383    1.1    atatat 		beg = 0;
   1384    1.1    atatat 		end = msgbufp->msg_bufx;
   1385    1.1    atatat 	}
   1386    1.1    atatat 
   1387  1.118        ad 	sysctl_relock();
   1388    1.1    atatat 	return (error);
   1389    1.1    atatat }
   1390    1.1    atatat 
   1391    1.1    atatat /*
   1392  1.110  christos  * sysctl helper routine for kern.defcorename. In the case of a new
   1393    1.1    atatat  * string being assigned, check that it's not a zero-length string.
   1394    1.1    atatat  * (XXX the check in -current doesn't work, but do we really care?)
   1395    1.1    atatat  */
   1396    1.1    atatat static int
   1397    1.1    atatat sysctl_kern_defcorename(SYSCTLFN_ARGS)
   1398    1.1    atatat {
   1399    1.1    atatat 	int error;
   1400   1.62      yamt 	char *newcorename;
   1401    1.1    atatat 	struct sysctlnode node;
   1402    1.1    atatat 
   1403   1.62      yamt 	newcorename = PNBUF_GET();
   1404    1.1    atatat 	node = *rnode;
   1405    1.1    atatat 	node.sysctl_data = &newcorename[0];
   1406    1.1    atatat 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
   1407    1.1    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1408   1.62      yamt 	if (error || newp == NULL) {
   1409   1.62      yamt 		goto done;
   1410   1.62      yamt 	}
   1411    1.1    atatat 
   1412    1.1    atatat 	/*
   1413    1.1    atatat 	 * when sysctl_lookup() deals with a string, it's guaranteed
   1414  1.110  christos 	 * to come back nul terminated. So there.  :)
   1415    1.1    atatat 	 */
   1416   1.62      yamt 	if (strlen(newcorename) == 0) {
   1417   1.62      yamt 		error = EINVAL;
   1418   1.62      yamt 	} else {
   1419   1.62      yamt 		memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
   1420   1.62      yamt 		error = 0;
   1421   1.62      yamt 	}
   1422   1.62      yamt done:
   1423   1.62      yamt 	PNBUF_PUT(newcorename);
   1424   1.62      yamt 	return error;
   1425    1.1    atatat }
   1426    1.1    atatat 
   1427    1.1    atatat /*
   1428  1.110  christos  * sysctl helper routine for kern.cp_time node. Adds up cpu time
   1429    1.1    atatat  * across all cpus.
   1430    1.1    atatat  */
   1431    1.1    atatat static int
   1432    1.1    atatat sysctl_kern_cptime(SYSCTLFN_ARGS)
   1433    1.1    atatat {
   1434    1.1    atatat 	struct sysctlnode node = *rnode;
   1435   1.59     perry 	uint64_t *cp_time = NULL;
   1436   1.96        ad 	int error, n = ncpu, i;
   1437    1.1    atatat 	struct cpu_info *ci;
   1438    1.1    atatat 	CPU_INFO_ITERATOR cii;
   1439    1.1    atatat 
   1440    1.1    atatat 	/*
   1441    1.1    atatat 	 * if you specifically pass a buffer that is the size of the
   1442    1.1    atatat 	 * sum, or if you are probing for the size, you get the "sum"
   1443    1.1    atatat 	 * of cp_time (and the size thereof) across all processors.
   1444    1.1    atatat 	 *
   1445    1.1    atatat 	 * alternately, you can pass an additional mib number and get
   1446    1.1    atatat 	 * cp_time for that particular processor.
   1447    1.1    atatat 	 */
   1448    1.1    atatat 	switch (namelen) {
   1449    1.1    atatat 	case 0:
   1450  1.110  christos 		if (*oldlenp == sizeof(uint64_t) * CPUSTATES || oldp == NULL) {
   1451   1.59     perry 			node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
   1452    1.1    atatat 			n = -1; /* SUM */
   1453    1.1    atatat 		}
   1454    1.1    atatat 		else {
   1455   1.59     perry 			node.sysctl_size = n * sizeof(uint64_t) * CPUSTATES;
   1456    1.1    atatat 			n = -2; /* ALL */
   1457    1.1    atatat 		}
   1458    1.1    atatat 		break;
   1459    1.1    atatat 	case 1:
   1460    1.1    atatat 		if (name[0] < 0 || name[0] >= n)
   1461   1.18    atatat 			return (ENOENT); /* ENOSUCHPROCESSOR */
   1462   1.59     perry 		node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
   1463    1.1    atatat 		n = name[0];
   1464    1.1    atatat 		/*
   1465    1.1    atatat 		 * adjust these so that sysctl_lookup() will be happy
   1466    1.1    atatat 		 */
   1467    1.1    atatat 		name++;
   1468    1.1    atatat 		namelen--;
   1469    1.7    martin 		break;
   1470    1.1    atatat 	default:
   1471    1.1    atatat 		return (EINVAL);
   1472    1.1    atatat 	}
   1473    1.1    atatat 
   1474  1.118        ad 	cp_time = kmem_alloc(node.sysctl_size, KM_SLEEP);
   1475    1.1    atatat 	if (cp_time == NULL)
   1476    1.1    atatat 		return (ENOMEM);
   1477    1.1    atatat 	node.sysctl_data = cp_time;
   1478    1.1    atatat 	memset(cp_time, 0, node.sysctl_size);
   1479    1.1    atatat 
   1480    1.1    atatat 	for (CPU_INFO_FOREACH(cii, ci)) {
   1481  1.118        ad 		if (n <= 0) {
   1482  1.118        ad 			for (i = 0; i < CPUSTATES; i++) {
   1483    1.1    atatat 				cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
   1484  1.118        ad 			}
   1485  1.118        ad 		}
   1486    1.1    atatat 		/*
   1487    1.1    atatat 		 * if a specific processor was requested and we just
   1488    1.1    atatat 		 * did it, we're done here
   1489    1.1    atatat 		 */
   1490    1.1    atatat 		if (n == 0)
   1491    1.1    atatat 			break;
   1492    1.1    atatat 		/*
   1493    1.1    atatat 		 * if doing "all", skip to next cp_time set for next processor
   1494    1.1    atatat 		 */
   1495    1.1    atatat 		if (n == -2)
   1496    1.1    atatat 			cp_time += CPUSTATES;
   1497    1.1    atatat 		/*
   1498    1.1    atatat 		 * if we're doing a specific processor, we're one
   1499    1.1    atatat 		 * processor closer
   1500    1.1    atatat 		 */
   1501    1.1    atatat 		if (n > 0)
   1502    1.1    atatat 			n--;
   1503    1.1    atatat 	}
   1504    1.1    atatat 
   1505    1.1    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1506  1.118        ad 	kmem_free(node.sysctl_data, node.sysctl_size);
   1507    1.5    martin 	return (error);
   1508    1.1    atatat }
   1509    1.1    atatat 
   1510    1.1    atatat #if NPTY > 0
   1511    1.1    atatat /*
   1512  1.110  christos  * sysctl helper routine for kern.maxptys. Ensures that any new value
   1513    1.1    atatat  * is acceptable to the pty subsystem.
   1514    1.1    atatat  */
   1515    1.1    atatat static int
   1516    1.1    atatat sysctl_kern_maxptys(SYSCTLFN_ARGS)
   1517    1.1    atatat {
   1518    1.1    atatat 	int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
   1519   1.40  christos 	int error, xmax;
   1520    1.1    atatat 	struct sysctlnode node;
   1521    1.1    atatat 
   1522    1.1    atatat 	/* get current value of maxptys */
   1523   1.40  christos 	xmax = pty_maxptys(0, 0);
   1524    1.1    atatat 
   1525    1.1    atatat 	node = *rnode;
   1526   1.40  christos 	node.sysctl_data = &xmax;
   1527    1.1    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1528    1.1    atatat 	if (error || newp == NULL)
   1529    1.1    atatat 		return (error);
   1530   1.33     perry 
   1531   1.40  christos 	if (xmax != pty_maxptys(xmax, 1))
   1532    1.1    atatat 		return (EINVAL);
   1533    1.1    atatat 
   1534    1.1    atatat 	return (0);
   1535    1.1    atatat }
   1536    1.1    atatat #endif /* NPTY > 0 */
   1537    1.1    atatat 
   1538    1.1    atatat /*
   1539  1.110  christos  * sysctl helper routine for kern.sbmax. Basically just ensures that
   1540    1.1    atatat  * any new value is not too small.
   1541    1.1    atatat  */
   1542    1.1    atatat static int
   1543    1.1    atatat sysctl_kern_sbmax(SYSCTLFN_ARGS)
   1544    1.1    atatat {
   1545    1.1    atatat 	int error, new_sbmax;
   1546    1.1    atatat 	struct sysctlnode node;
   1547    1.1    atatat 
   1548    1.1    atatat 	new_sbmax = sb_max;
   1549    1.1    atatat 	node = *rnode;
   1550    1.1    atatat 	node.sysctl_data = &new_sbmax;
   1551    1.1    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1552    1.1    atatat 	if (error || newp == NULL)
   1553    1.1    atatat 		return (error);
   1554    1.1    atatat 
   1555  1.118        ad 	KERNEL_LOCK(1, NULL);
   1556    1.1    atatat 	error = sb_max_set(new_sbmax);
   1557  1.118        ad 	KERNEL_UNLOCK_ONE(NULL);
   1558    1.1    atatat 
   1559    1.1    atatat 	return (error);
   1560    1.1    atatat }
   1561    1.1    atatat 
   1562    1.1    atatat /*
   1563  1.110  christos  * sysctl helper routine for kern.urandom node. Picks a random number
   1564    1.1    atatat  * for you.
   1565    1.1    atatat  */
   1566    1.1    atatat static int
   1567    1.1    atatat sysctl_kern_urnd(SYSCTLFN_ARGS)
   1568    1.1    atatat {
   1569    1.1    atatat #if NRND > 0
   1570  1.118        ad 	int v, rv;
   1571    1.1    atatat 
   1572  1.118        ad 	KERNEL_LOCK(1, NULL);
   1573  1.118        ad 	rv = rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY);
   1574  1.118        ad 	KERNEL_UNLOCK_ONE(NULL);
   1575  1.118        ad 	if (rv == sizeof(v)) {
   1576    1.1    atatat 		struct sysctlnode node = *rnode;
   1577    1.1    atatat 		node.sysctl_data = &v;
   1578    1.1    atatat 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1579    1.1    atatat 	}
   1580    1.1    atatat 	else
   1581    1.1    atatat 		return (EIO);	/*XXX*/
   1582    1.1    atatat #else
   1583    1.1    atatat 	return (EOPNOTSUPP);
   1584    1.1    atatat #endif
   1585    1.1    atatat }
   1586    1.1    atatat 
   1587    1.1    atatat /*
   1588  1.110  christos  * sysctl helper routine for kern.arandom node. Picks a random number
   1589   1.91  christos  * for you.
   1590   1.91  christos  */
   1591   1.91  christos static int
   1592   1.91  christos sysctl_kern_arnd(SYSCTLFN_ARGS)
   1593   1.91  christos {
   1594   1.91  christos #if NRND > 0
   1595   1.91  christos 	int error;
   1596   1.91  christos 	void *v;
   1597   1.91  christos 	struct sysctlnode node = *rnode;
   1598   1.91  christos 
   1599   1.91  christos 	if (*oldlenp == 0)
   1600   1.91  christos 		return 0;
   1601   1.91  christos 	if (*oldlenp > 8192)
   1602   1.91  christos 		return E2BIG;
   1603   1.91  christos 
   1604  1.118        ad 	v = kmem_alloc(*oldlenp, KM_SLEEP);
   1605   1.91  christos 	arc4randbytes(v, *oldlenp);
   1606   1.91  christos 	node.sysctl_data = v;
   1607   1.91  christos 	node.sysctl_size = *oldlenp;
   1608   1.91  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1609  1.118        ad 	kmem_free(v, *oldlenp);
   1610   1.91  christos 	return error;
   1611   1.91  christos #else
   1612   1.91  christos 	return (EOPNOTSUPP);
   1613   1.91  christos #endif
   1614   1.91  christos }
   1615   1.91  christos /*
   1616    1.1    atatat  * sysctl helper routine to do kern.lwp.* work.
   1617    1.1    atatat  */
   1618    1.1    atatat static int
   1619    1.1    atatat sysctl_kern_lwp(SYSCTLFN_ARGS)
   1620    1.1    atatat {
   1621    1.1    atatat 	struct kinfo_lwp klwp;
   1622    1.1    atatat 	struct proc *p;
   1623  1.118        ad 	struct lwp *l2, *l3;
   1624    1.1    atatat 	char *where, *dp;
   1625    1.1    atatat 	int pid, elem_size, elem_count;
   1626    1.1    atatat 	int buflen, needed, error;
   1627  1.118        ad 	bool gotit;
   1628    1.1    atatat 
   1629   1.19    atatat 	if (namelen == 1 && name[0] == CTL_QUERY)
   1630   1.43    atatat 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1631   1.19    atatat 
   1632    1.1    atatat 	dp = where = oldp;
   1633    1.1    atatat 	buflen = where != NULL ? *oldlenp : 0;
   1634    1.1    atatat 	error = needed = 0;
   1635    1.1    atatat 
   1636   1.11    simonb 	if (newp != NULL || namelen != 3)
   1637    1.1    atatat 		return (EINVAL);
   1638   1.11    simonb 	pid = name[0];
   1639   1.11    simonb 	elem_size = name[1];
   1640   1.11    simonb 	elem_count = name[2];
   1641    1.1    atatat 
   1642  1.118        ad 	sysctl_unlock();
   1643  1.101      yamt 	if (pid == -1) {
   1644  1.132        ad 		mutex_enter(proc_lock);
   1645  1.162      yamt 		PROCLIST_FOREACH(p, &allproc) {
   1646  1.118        ad 			/* Grab a hold on the process. */
   1647  1.118        ad 			if (!rw_tryenter(&p->p_reflock, RW_READER)) {
   1648  1.118        ad 				continue;
   1649  1.118        ad 			}
   1650  1.132        ad 			mutex_exit(proc_lock);
   1651  1.118        ad 
   1652  1.133        ad 			mutex_enter(p->p_lock);
   1653  1.118        ad 			LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1654  1.118        ad 				if (buflen >= elem_size && elem_count > 0) {
   1655  1.118        ad 					lwp_lock(l2);
   1656  1.118        ad 					fill_lwp(l2, &klwp);
   1657  1.118        ad 					lwp_unlock(l2);
   1658  1.133        ad 					mutex_exit(p->p_lock);
   1659  1.101      yamt 
   1660  1.118        ad 					/*
   1661  1.118        ad 					 * Copy out elem_size, but not
   1662  1.118        ad 					 * larger than the size of a
   1663  1.118        ad 					 * struct kinfo_proc2.
   1664  1.118        ad 					 */
   1665  1.118        ad 					error = dcopyout(l, &klwp, dp,
   1666  1.118        ad 					    min(sizeof(klwp), elem_size));
   1667  1.118        ad 					if (error) {
   1668  1.118        ad 						rw_exit(&p->p_reflock);
   1669  1.118        ad 						goto cleanup;
   1670  1.118        ad 					}
   1671  1.133        ad 					mutex_enter(p->p_lock);
   1672  1.118        ad 					LIST_FOREACH(l3, &p->p_lwps,
   1673  1.118        ad 					    l_sibling) {
   1674  1.118        ad 						if (l2 == l3)
   1675  1.118        ad 							break;
   1676  1.118        ad 					}
   1677  1.118        ad 					if (l3 == NULL) {
   1678  1.133        ad 						mutex_exit(p->p_lock);
   1679  1.118        ad 						rw_exit(&p->p_reflock);
   1680  1.118        ad 						error = EAGAIN;
   1681  1.118        ad 						goto cleanup;
   1682  1.118        ad 					}
   1683  1.118        ad 					dp += elem_size;
   1684  1.118        ad 					buflen -= elem_size;
   1685  1.118        ad 					elem_count--;
   1686  1.118        ad 				}
   1687  1.118        ad 				needed += elem_size;
   1688  1.101      yamt 			}
   1689  1.133        ad 			mutex_exit(p->p_lock);
   1690  1.118        ad 
   1691  1.118        ad 			/* Drop reference to process. */
   1692  1.132        ad 			mutex_enter(proc_lock);
   1693  1.118        ad 			rw_exit(&p->p_reflock);
   1694  1.101      yamt 		}
   1695  1.132        ad 		mutex_exit(proc_lock);
   1696  1.101      yamt 	} else {
   1697  1.132        ad 		mutex_enter(proc_lock);
   1698  1.101      yamt 		p = p_find(pid, PFIND_LOCKED);
   1699  1.101      yamt 		if (p == NULL) {
   1700  1.118        ad 			error = ESRCH;
   1701  1.132        ad 			mutex_exit(proc_lock);
   1702  1.118        ad 			goto cleanup;
   1703  1.118        ad 		}
   1704  1.118        ad 		/* Grab a hold on the process. */
   1705  1.118        ad 		gotit = rw_tryenter(&p->p_reflock, RW_READER);
   1706  1.132        ad 		mutex_exit(proc_lock);
   1707  1.118        ad 		if (!gotit) {
   1708  1.118        ad 			error = ESRCH;
   1709  1.118        ad 			goto cleanup;
   1710  1.101      yamt 		}
   1711  1.118        ad 
   1712  1.133        ad 		mutex_enter(p->p_lock);
   1713  1.101      yamt 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1714  1.101      yamt 			if (buflen >= elem_size && elem_count > 0) {
   1715  1.101      yamt 				lwp_lock(l2);
   1716  1.101      yamt 				fill_lwp(l2, &klwp);
   1717  1.101      yamt 				lwp_unlock(l2);
   1718  1.133        ad 				mutex_exit(p->p_lock);
   1719  1.101      yamt 				/*
   1720  1.101      yamt 				 * Copy out elem_size, but not larger than
   1721  1.101      yamt 				 * the size of a struct kinfo_proc2.
   1722  1.101      yamt 				 */
   1723  1.101      yamt 				error = dcopyout(l, &klwp, dp,
   1724  1.101      yamt 				    min(sizeof(klwp), elem_size));
   1725  1.107        ad 				if (error) {
   1726  1.118        ad 					rw_exit(&p->p_reflock);
   1727  1.107        ad 					goto cleanup;
   1728  1.107        ad 				}
   1729  1.133        ad 				mutex_enter(p->p_lock);
   1730  1.107        ad 				LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
   1731  1.107        ad 					if (l2 == l3)
   1732  1.107        ad 						break;
   1733  1.107        ad 				}
   1734  1.108     rmind 				if (l3 == NULL) {
   1735  1.133        ad 					mutex_exit(p->p_lock);
   1736  1.118        ad 					rw_exit(&p->p_reflock);
   1737  1.107        ad 					error = EAGAIN;
   1738  1.101      yamt 					goto cleanup;
   1739  1.107        ad 				}
   1740  1.101      yamt 				dp += elem_size;
   1741  1.101      yamt 				buflen -= elem_size;
   1742  1.101      yamt 				elem_count--;
   1743  1.101      yamt 			}
   1744  1.101      yamt 			needed += elem_size;
   1745    1.1    atatat 		}
   1746  1.133        ad 		mutex_exit(p->p_lock);
   1747  1.118        ad 
   1748  1.118        ad 		/* Drop reference to process. */
   1749  1.118        ad 		rw_exit(&p->p_reflock);
   1750    1.1    atatat 	}
   1751    1.1    atatat 
   1752    1.1    atatat 	if (where != NULL) {
   1753    1.1    atatat 		*oldlenp = dp - where;
   1754  1.118        ad 		if (needed > *oldlenp) {
   1755  1.118        ad 			sysctl_relock();
   1756    1.1    atatat 			return (ENOMEM);
   1757  1.118        ad 		}
   1758    1.1    atatat 	} else {
   1759   1.22    atatat 		needed += KERN_LWPSLOP;
   1760    1.1    atatat 		*oldlenp = needed;
   1761    1.1    atatat 	}
   1762  1.118        ad 	error = 0;
   1763    1.1    atatat  cleanup:
   1764  1.118        ad 	sysctl_relock();
   1765    1.1    atatat 	return (error);
   1766    1.1    atatat }
   1767    1.1    atatat 
   1768    1.1    atatat /*
   1769  1.110  christos  * sysctl helper routine for kern.forkfsleep node. Ensures that the
   1770    1.1    atatat  * given value is not too large or two small, and is at least one
   1771    1.1    atatat  * timer tick if not zero.
   1772    1.1    atatat  */
   1773    1.1    atatat static int
   1774    1.1    atatat sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
   1775    1.1    atatat {
   1776    1.1    atatat 	/* userland sees value in ms, internally is in ticks */
   1777    1.1    atatat 	extern int forkfsleep;		/* defined in kern/kern_fork.c */
   1778    1.1    atatat 	int error, timo, lsleep;
   1779    1.1    atatat 	struct sysctlnode node;
   1780    1.1    atatat 
   1781    1.1    atatat 	lsleep = forkfsleep * 1000 / hz;
   1782    1.1    atatat 	node = *rnode;
   1783    1.1    atatat 	node.sysctl_data = &lsleep;
   1784    1.1    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1785    1.1    atatat 	if (error || newp == NULL)
   1786    1.1    atatat 		return (error);
   1787    1.1    atatat 
   1788    1.1    atatat 	/* refuse negative values, and overly 'long time' */
   1789    1.1    atatat 	if (lsleep < 0 || lsleep > MAXSLP * 1000)
   1790    1.1    atatat 		return (EINVAL);
   1791    1.1    atatat 
   1792    1.1    atatat 	timo = mstohz(lsleep);
   1793    1.1    atatat 
   1794    1.1    atatat 	/* if the interval is >0 ms && <1 tick, use 1 tick */
   1795    1.1    atatat 	if (lsleep != 0 && timo == 0)
   1796    1.1    atatat 		forkfsleep = 1;
   1797    1.1    atatat 	else
   1798    1.1    atatat 		forkfsleep = timo;
   1799    1.1    atatat 
   1800    1.1    atatat 	return (0);
   1801    1.1    atatat }
   1802    1.1    atatat 
   1803    1.1    atatat /*
   1804    1.1    atatat  * sysctl helper routine for kern.root_partition
   1805    1.1    atatat  */
   1806    1.1    atatat static int
   1807    1.1    atatat sysctl_kern_root_partition(SYSCTLFN_ARGS)
   1808    1.1    atatat {
   1809    1.1    atatat 	int rootpart = DISKPART(rootdev);
   1810    1.1    atatat 	struct sysctlnode node = *rnode;
   1811    1.1    atatat 
   1812    1.1    atatat 	node.sysctl_data = &rootpart;
   1813    1.1    atatat 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1814    1.1    atatat }
   1815    1.1    atatat 
   1816    1.1    atatat /*
   1817    1.1    atatat  * sysctl helper function for kern.drivers
   1818    1.1    atatat  */
   1819    1.1    atatat static int
   1820    1.1    atatat sysctl_kern_drivers(SYSCTLFN_ARGS)
   1821    1.1    atatat {
   1822    1.1    atatat 	int error;
   1823    1.1    atatat 	size_t buflen;
   1824    1.1    atatat 	struct kinfo_drivers kd;
   1825    1.1    atatat 	char *start, *where;
   1826    1.1    atatat 	const char *dname;
   1827    1.1    atatat 	int i;
   1828    1.1    atatat 	extern struct devsw_conv *devsw_conv;
   1829    1.1    atatat 	extern int max_devsw_convs;
   1830    1.1    atatat 
   1831    1.1    atatat 	if (newp != NULL || namelen != 0)
   1832    1.1    atatat 		return (EINVAL);
   1833    1.1    atatat 
   1834    1.1    atatat 	start = where = oldp;
   1835    1.1    atatat 	buflen = *oldlenp;
   1836    1.1    atatat 	if (where == NULL) {
   1837    1.1    atatat 		*oldlenp = max_devsw_convs * sizeof kd;
   1838    1.1    atatat 		return 0;
   1839    1.1    atatat 	}
   1840    1.1    atatat 
   1841    1.1    atatat 	/*
   1842    1.1    atatat 	 * An array of kinfo_drivers structures
   1843    1.1    atatat 	 */
   1844    1.1    atatat 	error = 0;
   1845  1.118        ad 	sysctl_unlock();
   1846  1.152     pooka 	mutex_enter(&device_lock);
   1847    1.1    atatat 	for (i = 0; i < max_devsw_convs; i++) {
   1848    1.1    atatat 		dname = devsw_conv[i].d_name;
   1849    1.1    atatat 		if (dname == NULL)
   1850    1.1    atatat 			continue;
   1851    1.1    atatat 		if (buflen < sizeof kd) {
   1852    1.1    atatat 			error = ENOMEM;
   1853    1.1    atatat 			break;
   1854    1.1    atatat 		}
   1855   1.25    atatat 		memset(&kd, 0, sizeof(kd));
   1856    1.1    atatat 		kd.d_bmajor = devsw_conv[i].d_bmajor;
   1857    1.1    atatat 		kd.d_cmajor = devsw_conv[i].d_cmajor;
   1858    1.1    atatat 		strlcpy(kd.d_name, dname, sizeof kd.d_name);
   1859  1.152     pooka 		mutex_exit(&device_lock);
   1860   1.86      manu 		error = dcopyout(l, &kd, where, sizeof kd);
   1861  1.152     pooka 		mutex_enter(&device_lock);
   1862    1.1    atatat 		if (error != 0)
   1863    1.1    atatat 			break;
   1864    1.1    atatat 		buflen -= sizeof kd;
   1865    1.1    atatat 		where += sizeof kd;
   1866    1.1    atatat 	}
   1867  1.152     pooka 	mutex_exit(&device_lock);
   1868  1.118        ad 	sysctl_relock();
   1869    1.1    atatat 	*oldlenp = where - start;
   1870    1.1    atatat 	return error;
   1871    1.1    atatat }
   1872    1.1    atatat 
   1873   1.34    atatat /*
   1874   1.34    atatat  * sysctl helper function for kern.file2
   1875   1.34    atatat  */
   1876   1.34    atatat static int
   1877   1.34    atatat sysctl_kern_file2(SYSCTLFN_ARGS)
   1878   1.34    atatat {
   1879   1.34    atatat 	struct proc *p;
   1880  1.121        ad 	struct file *fp, *tp, *np;
   1881   1.34    atatat 	struct filedesc *fd;
   1882   1.34    atatat 	struct kinfo_file kf;
   1883   1.34    atatat 	char *dp;
   1884   1.34    atatat 	u_int i, op;
   1885   1.34    atatat 	size_t len, needed, elem_size, out_size;
   1886   1.34    atatat 	int error, arg, elem_count;
   1887  1.124        ad 	fdfile_t *ff;
   1888  1.164        ad 	fdtab_t *dt;
   1889   1.34    atatat 
   1890   1.34    atatat 	if (namelen == 1 && name[0] == CTL_QUERY)
   1891   1.43    atatat 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1892   1.34    atatat 
   1893   1.34    atatat 	if (namelen != 4)
   1894   1.34    atatat 		return (EINVAL);
   1895   1.34    atatat 
   1896   1.34    atatat 	error = 0;
   1897   1.34    atatat 	dp = oldp;
   1898   1.34    atatat 	len = (oldp != NULL) ? *oldlenp : 0;
   1899   1.34    atatat 	op = name[0];
   1900   1.34    atatat 	arg = name[1];
   1901   1.34    atatat 	elem_size = name[2];
   1902   1.34    atatat 	elem_count = name[3];
   1903   1.34    atatat 	out_size = MIN(sizeof(kf), elem_size);
   1904   1.34    atatat 	needed = 0;
   1905   1.34    atatat 
   1906   1.34    atatat 	if (elem_size < 1 || elem_count < 0)
   1907   1.34    atatat 		return (EINVAL);
   1908   1.34    atatat 
   1909   1.34    atatat 	switch (op) {
   1910   1.34    atatat 	case KERN_FILE_BYFILE:
   1911   1.34    atatat 		/*
   1912   1.34    atatat 		 * doesn't use arg so it must be zero
   1913   1.34    atatat 		 */
   1914   1.34    atatat 		if (arg != 0)
   1915   1.34    atatat 			return (EINVAL);
   1916  1.118        ad 		sysctl_unlock();
   1917  1.121        ad 		/*
   1918  1.121        ad 		 * allocate dummy file descriptor to make position in list
   1919  1.121        ad 		 */
   1920  1.121        ad 		if ((tp = fgetdummy()) == NULL) {
   1921  1.121        ad 		 	sysctl_relock();
   1922  1.121        ad 			return ENOMEM;
   1923  1.121        ad 		}
   1924  1.118        ad 		mutex_enter(&filelist_lock);
   1925  1.121        ad 		for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) {
   1926  1.121        ad 			np = LIST_NEXT(fp, f_list);
   1927  1.121        ad 			mutex_enter(&fp->f_lock);
   1928  1.121        ad 			if (fp->f_count == 0) {
   1929  1.121        ad 				mutex_exit(&fp->f_lock);
   1930  1.121        ad 				continue;
   1931  1.121        ad 			}
   1932  1.120      elad 			/*
   1933  1.120      elad 			 * XXX Need to prevent that from being an alternative
   1934  1.120      elad 			 * XXX way for getting process information.
   1935  1.120      elad 			 */
   1936   1.78        ad 			if (kauth_authorize_generic(l->l_cred,
   1937  1.121        ad 			    KAUTH_GENERIC_CANSEE, fp->f_cred) != 0) {
   1938  1.118        ad 				mutex_exit(&fp->f_lock);
   1939  1.118        ad 				continue;
   1940  1.118        ad 			}
   1941   1.34    atatat 			if (len >= elem_size && elem_count > 0) {
   1942  1.124        ad 				fill_file(&kf, fp, NULL, 0, 0);
   1943  1.121        ad 				LIST_INSERT_AFTER(fp, tp, f_list);
   1944  1.121        ad 				mutex_exit(&fp->f_lock);
   1945  1.121        ad 				mutex_exit(&filelist_lock);
   1946   1.86      manu 				error = dcopyout(l, &kf, dp, out_size);
   1947  1.121        ad 				mutex_enter(&filelist_lock);
   1948  1.121        ad 				np = LIST_NEXT(tp, f_list);
   1949  1.121        ad 				LIST_REMOVE(tp, f_list);
   1950  1.118        ad 				if (error) {
   1951   1.34    atatat 					break;
   1952  1.118        ad 				}
   1953   1.34    atatat 				dp += elem_size;
   1954   1.34    atatat 				len -= elem_size;
   1955  1.121        ad 			} else {
   1956  1.121        ad 				mutex_exit(&fp->f_lock);
   1957   1.34    atatat 			}
   1958  1.158       mrg 			needed += elem_size;
   1959  1.159       mrg 			if (elem_count > 0 && elem_count != INT_MAX)
   1960  1.159       mrg 				elem_count--;
   1961   1.34    atatat 		}
   1962  1.118        ad 		mutex_exit(&filelist_lock);
   1963  1.121        ad 		fputdummy(tp);
   1964  1.118        ad 		sysctl_relock();
   1965   1.34    atatat 		break;
   1966   1.69      yamt 	case KERN_FILE_BYPID:
   1967   1.34    atatat 		if (arg < -1)
   1968   1.34    atatat 			/* -1 means all processes */
   1969   1.34    atatat 			return (EINVAL);
   1970  1.118        ad 		sysctl_unlock();
   1971  1.132        ad 		mutex_enter(proc_lock);
   1972  1.162      yamt 		PROCLIST_FOREACH(p, &allproc) {
   1973  1.118        ad 			if (p->p_stat == SIDL) {
   1974   1.34    atatat 				/* skip embryonic processes */
   1975   1.34    atatat 				continue;
   1976  1.118        ad 			}
   1977  1.118        ad 			if (arg > 0 && p->p_pid != arg) {
   1978   1.34    atatat 				/* pick only the one we want */
   1979   1.34    atatat 				/* XXX want 0 to mean "kernel files" */
   1980   1.34    atatat 				continue;
   1981  1.118        ad 			}
   1982  1.133        ad 			mutex_enter(p->p_lock);
   1983  1.118        ad 			error = kauth_authorize_process(l->l_cred,
   1984  1.120      elad 			    KAUTH_PROCESS_CANSEE, p,
   1985  1.120      elad 			    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES),
   1986  1.120      elad 			    NULL, NULL);
   1987  1.133        ad 			mutex_exit(p->p_lock);
   1988  1.118        ad 			if (error != 0) {
   1989  1.151      elad 				/*
   1990  1.151      elad 				 * Don't leak kauth retval if we're silently
   1991  1.151      elad 				 * skipping this entry.
   1992  1.151      elad 				 */
   1993  1.151      elad 				error = 0;
   1994  1.118        ad 				continue;
   1995  1.118        ad 			}
   1996  1.118        ad 
   1997  1.118        ad 			/*
   1998  1.118        ad 			 * Grab a hold on the process.
   1999  1.118        ad 			 */
   2000  1.118        ad 			if (!rw_tryenter(&p->p_reflock, RW_READER)) {
   2001  1.118        ad 				continue;
   2002  1.118        ad 			}
   2003  1.132        ad 			mutex_exit(proc_lock);
   2004  1.118        ad 
   2005  1.120      elad 			/* XXX Do we need to check permission per file? */
   2006   1.34    atatat 			fd = p->p_fd;
   2007  1.124        ad 			mutex_enter(&fd->fd_lock);
   2008  1.164        ad 			dt = fd->fd_dt;
   2009  1.164        ad 			for (i = 0; i < dt->dt_nfiles; i++) {
   2010  1.164        ad 				if ((ff = dt->dt_ff[i]) == NULL) {
   2011  1.118        ad 					continue;
   2012  1.118        ad 				}
   2013  1.124        ad 				if ((fp = ff->ff_file) == NULL) {
   2014   1.34    atatat 					continue;
   2015  1.118        ad 				}
   2016   1.34    atatat 				if (len >= elem_size && elem_count > 0) {
   2017  1.137        ad 					mutex_enter(&fp->f_lock);
   2018  1.124        ad 					fill_file(&kf, fp, ff, i, p->p_pid);
   2019  1.118        ad 					mutex_exit(&fp->f_lock);
   2020  1.124        ad 					mutex_exit(&fd->fd_lock);
   2021   1.86      manu 					error = dcopyout(l, &kf, dp, out_size);
   2022  1.124        ad 					mutex_enter(&fd->fd_lock);
   2023   1.34    atatat 					if (error)
   2024   1.34    atatat 						break;
   2025   1.34    atatat 					dp += elem_size;
   2026   1.34    atatat 					len -= elem_size;
   2027   1.34    atatat 				}
   2028  1.158       mrg 				needed += elem_size;
   2029  1.159       mrg 				if (elem_count > 0 && elem_count != INT_MAX)
   2030  1.159       mrg 					elem_count--;
   2031   1.34    atatat 			}
   2032  1.124        ad 			mutex_exit(&fd->fd_lock);
   2033  1.118        ad 
   2034  1.118        ad 			/*
   2035  1.118        ad 			 * Release reference to process.
   2036  1.118        ad 			 */
   2037  1.132        ad 			mutex_enter(proc_lock);
   2038  1.118        ad 			rw_exit(&p->p_reflock);
   2039   1.34    atatat 		}
   2040  1.132        ad 		mutex_exit(proc_lock);
   2041  1.118        ad 		sysctl_relock();
   2042   1.34    atatat 		break;
   2043   1.34    atatat 	default:
   2044   1.34    atatat 		return (EINVAL);
   2045   1.34    atatat 	}
   2046   1.34    atatat 
   2047   1.34    atatat 	if (oldp == NULL)
   2048   1.34    atatat 		needed += KERN_FILESLOP * elem_size;
   2049   1.34    atatat 	*oldlenp = needed;
   2050   1.34    atatat 
   2051   1.34    atatat 	return (error);
   2052   1.34    atatat }
   2053   1.34    atatat 
   2054   1.34    atatat static void
   2055  1.124        ad fill_file(struct kinfo_file *kp, const file_t *fp, const fdfile_t *ff,
   2056  1.124        ad 	  int i, pid_t pid)
   2057   1.34    atatat {
   2058   1.34    atatat 
   2059   1.34    atatat 	memset(kp, 0, sizeof(*kp));
   2060   1.34    atatat 
   2061   1.34    atatat 	kp->ki_fileaddr =	PTRTOUINT64(fp);
   2062   1.34    atatat 	kp->ki_flag =		fp->f_flag;
   2063  1.157        ad 	kp->ki_iflags =		0;
   2064   1.34    atatat 	kp->ki_ftype =		fp->f_type;
   2065   1.34    atatat 	kp->ki_count =		fp->f_count;
   2066   1.34    atatat 	kp->ki_msgcount =	fp->f_msgcount;
   2067   1.34    atatat 	kp->ki_fucred =		PTRTOUINT64(fp->f_cred);
   2068   1.68      elad 	kp->ki_fuid =		kauth_cred_geteuid(fp->f_cred);
   2069   1.68      elad 	kp->ki_fgid =		kauth_cred_getegid(fp->f_cred);
   2070   1.34    atatat 	kp->ki_fops =		PTRTOUINT64(fp->f_ops);
   2071   1.34    atatat 	kp->ki_foffset =	fp->f_offset;
   2072   1.34    atatat 	kp->ki_fdata =		PTRTOUINT64(fp->f_data);
   2073   1.34    atatat 
   2074   1.34    atatat 	/* vnode information to glue this file to something */
   2075   1.34    atatat 	if (fp->f_type == DTYPE_VNODE) {
   2076   1.34    atatat 		struct vnode *vp = (struct vnode *)fp->f_data;
   2077   1.34    atatat 
   2078   1.34    atatat 		kp->ki_vun =	PTRTOUINT64(vp->v_un.vu_socket);
   2079   1.34    atatat 		kp->ki_vsize =	vp->v_size;
   2080   1.34    atatat 		kp->ki_vtype =	vp->v_type;
   2081   1.34    atatat 		kp->ki_vtag =	vp->v_tag;
   2082   1.34    atatat 		kp->ki_vdata =	PTRTOUINT64(vp->v_data);
   2083   1.34    atatat 	}
   2084   1.34    atatat 
   2085  1.110  christos 	/* process information when retrieved via KERN_FILE_BYPID */
   2086  1.124        ad 	if (ff != NULL) {
   2087  1.124        ad 		kp->ki_pid =		pid;
   2088   1.34    atatat 		kp->ki_fd =		i;
   2089  1.124        ad 		kp->ki_ofileflags =	ff->ff_exclose;
   2090  1.124        ad 		kp->ki_usecount =	ff->ff_refcnt;
   2091   1.34    atatat 	}
   2092   1.34    atatat }
   2093   1.34    atatat 
   2094    1.1    atatat static int
   2095    1.1    atatat sysctl_doeproc(SYSCTLFN_ARGS)
   2096    1.1    atatat {
   2097  1.163      yamt 	union {
   2098  1.163      yamt 		struct kinfo_proc kproc;
   2099  1.163      yamt 		struct kinfo_proc2 kproc2;
   2100  1.163      yamt 	} *kbuf;
   2101  1.135        ad 	struct proc *p, *next, *marker;
   2102  1.163      yamt 	char *where, *dp;
   2103  1.135        ad 	int type, op, arg, error;
   2104  1.163      yamt 	u_int elem_size, kelem_size, elem_count;
   2105    1.1    atatat 	size_t buflen, needed;
   2106  1.135        ad 	bool match, zombie, mmmbrains;
   2107    1.1    atatat 
   2108   1.19    atatat 	if (namelen == 1 && name[0] == CTL_QUERY)
   2109   1.43    atatat 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2110   1.19    atatat 
   2111  1.163      yamt 	dp = where = oldp;
   2112    1.1    atatat 	buflen = where != NULL ? *oldlenp : 0;
   2113    1.1    atatat 	error = 0;
   2114    1.1    atatat 	needed = 0;
   2115    1.1    atatat 	type = rnode->sysctl_num;
   2116    1.1    atatat 
   2117    1.1    atatat 	if (type == KERN_PROC) {
   2118    1.1    atatat 		if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
   2119    1.1    atatat 			return (EINVAL);
   2120    1.1    atatat 		op = name[0];
   2121    1.1    atatat 		if (op != KERN_PROC_ALL)
   2122    1.1    atatat 			arg = name[1];
   2123    1.1    atatat 		else
   2124    1.1    atatat 			arg = 0;		/* Quell compiler warning */
   2125  1.163      yamt 		elem_count = 0;	/* Ditto */
   2126  1.163      yamt 		kelem_size = elem_size = sizeof(kbuf->kproc);
   2127    1.1    atatat 	} else {
   2128    1.1    atatat 		if (namelen != 4)
   2129    1.1    atatat 			return (EINVAL);
   2130    1.1    atatat 		op = name[0];
   2131    1.1    atatat 		arg = name[1];
   2132    1.1    atatat 		elem_size = name[2];
   2133    1.1    atatat 		elem_count = name[3];
   2134  1.163      yamt 		kelem_size = sizeof(kbuf->kproc2);
   2135    1.1    atatat 	}
   2136    1.1    atatat 
   2137  1.118        ad 	sysctl_unlock();
   2138  1.118        ad 
   2139  1.163      yamt 	kbuf = kmem_alloc(sizeof(*kbuf), KM_SLEEP);
   2140  1.135        ad 	marker = kmem_alloc(sizeof(*marker), KM_SLEEP);
   2141  1.161      yamt 	marker->p_flag = PK_MARKER;
   2142  1.118        ad 
   2143  1.132        ad 	mutex_enter(proc_lock);
   2144  1.136        ad 	mmmbrains = false;
   2145  1.135        ad 	for (p = LIST_FIRST(&allproc);; p = next) {
   2146  1.135        ad 		if (p == NULL) {
   2147  1.136        ad 			if (!mmmbrains) {
   2148  1.135        ad 				p = LIST_FIRST(&zombproc);
   2149  1.136        ad 				mmmbrains = true;
   2150  1.135        ad 			}
   2151  1.135        ad 			if (p == NULL)
   2152  1.135        ad 				break;
   2153  1.135        ad 		}
   2154  1.135        ad 		next = LIST_NEXT(p, p_list);
   2155  1.162      yamt 		if ((p->p_flag & PK_MARKER) != 0)
   2156  1.162      yamt 			continue;
   2157  1.135        ad 
   2158    1.1    atatat 		/*
   2159    1.1    atatat 		 * Skip embryonic processes.
   2160    1.1    atatat 		 */
   2161    1.1    atatat 		if (p->p_stat == SIDL)
   2162    1.1    atatat 			continue;
   2163   1.55      elad 
   2164  1.133        ad 		mutex_enter(p->p_lock);
   2165  1.118        ad 		error = kauth_authorize_process(l->l_cred,
   2166  1.120      elad 		    KAUTH_PROCESS_CANSEE, p,
   2167  1.120      elad 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
   2168  1.118        ad 		if (error != 0) {
   2169  1.133        ad 			mutex_exit(p->p_lock);
   2170   1.55      elad 			continue;
   2171  1.118        ad 		}
   2172   1.55      elad 
   2173    1.1    atatat 		/*
   2174    1.1    atatat 		 * TODO - make more efficient (see notes below).
   2175    1.1    atatat 		 * do by session.
   2176    1.1    atatat 		 */
   2177    1.1    atatat 		switch (op) {
   2178    1.1    atatat 		case KERN_PROC_PID:
   2179    1.1    atatat 			/* could do this with just a lookup */
   2180  1.118        ad 			match = (p->p_pid == (pid_t)arg);
   2181    1.1    atatat 			break;
   2182    1.1    atatat 
   2183    1.1    atatat 		case KERN_PROC_PGRP:
   2184    1.1    atatat 			/* could do this by traversing pgrp */
   2185  1.118        ad 			match = (p->p_pgrp->pg_id == (pid_t)arg);
   2186    1.1    atatat 			break;
   2187    1.1    atatat 
   2188    1.1    atatat 		case KERN_PROC_SESSION:
   2189  1.118        ad 			match = (p->p_session->s_sid == (pid_t)arg);
   2190    1.1    atatat 			break;
   2191    1.1    atatat 
   2192    1.1    atatat 		case KERN_PROC_TTY:
   2193  1.118        ad 			match = true;
   2194    1.1    atatat 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
   2195   1.95        ad 				if ((p->p_lflag & PL_CONTROLT) == 0 ||
   2196    1.1    atatat 				    p->p_session->s_ttyp == NULL ||
   2197  1.118        ad 				    p->p_session->s_ttyvp != NULL) {
   2198  1.118        ad 				    	match = false;
   2199  1.118        ad 				}
   2200   1.95        ad 			} else if ((p->p_lflag & PL_CONTROLT) == 0 ||
   2201    1.1    atatat 			    p->p_session->s_ttyp == NULL) {
   2202  1.118        ad 				if ((dev_t)arg != KERN_PROC_TTY_NODEV) {
   2203  1.118        ad 					match = false;
   2204  1.118        ad 				}
   2205  1.118        ad 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg) {
   2206  1.118        ad 				match = false;
   2207  1.118        ad 			}
   2208    1.1    atatat 			break;
   2209    1.1    atatat 
   2210    1.1    atatat 		case KERN_PROC_UID:
   2211  1.118        ad 			match = (kauth_cred_geteuid(p->p_cred) == (uid_t)arg);
   2212    1.1    atatat 			break;
   2213    1.1    atatat 
   2214    1.1    atatat 		case KERN_PROC_RUID:
   2215  1.118        ad 			match = (kauth_cred_getuid(p->p_cred) == (uid_t)arg);
   2216    1.1    atatat 			break;
   2217    1.1    atatat 
   2218    1.1    atatat 		case KERN_PROC_GID:
   2219  1.118        ad 			match = (kauth_cred_getegid(p->p_cred) == (uid_t)arg);
   2220    1.1    atatat 			break;
   2221    1.1    atatat 
   2222    1.1    atatat 		case KERN_PROC_RGID:
   2223  1.118        ad 			match = (kauth_cred_getgid(p->p_cred) == (uid_t)arg);
   2224    1.1    atatat 			break;
   2225    1.1    atatat 
   2226    1.1    atatat 		case KERN_PROC_ALL:
   2227  1.118        ad 			match = true;
   2228    1.1    atatat 			/* allow everything */
   2229    1.1    atatat 			break;
   2230    1.1    atatat 
   2231    1.1    atatat 		default:
   2232    1.1    atatat 			error = EINVAL;
   2233  1.133        ad 			mutex_exit(p->p_lock);
   2234    1.1    atatat 			goto cleanup;
   2235    1.1    atatat 		}
   2236  1.118        ad 		if (!match) {
   2237  1.133        ad 			mutex_exit(p->p_lock);
   2238  1.118        ad 			continue;
   2239  1.118        ad 		}
   2240  1.118        ad 
   2241  1.118        ad 		/*
   2242  1.118        ad 		 * Grab a hold on the process.
   2243  1.118        ad 		 */
   2244  1.136        ad 		if (mmmbrains) {
   2245  1.136        ad 			zombie = true;
   2246  1.136        ad 		} else {
   2247  1.136        ad 			zombie = !rw_tryenter(&p->p_reflock, RW_READER);
   2248  1.136        ad 		}
   2249  1.135        ad 		if (zombie) {
   2250  1.135        ad 			LIST_INSERT_AFTER(p, marker, p_list);
   2251  1.118        ad 		}
   2252  1.135        ad 
   2253  1.163      yamt 		if (buflen >= elem_size &&
   2254  1.163      yamt 		    (type == KERN_PROC || elem_count > 0)) {
   2255  1.163      yamt 			if (type == KERN_PROC) {
   2256  1.163      yamt 				kbuf->kproc.kp_proc = *p;
   2257  1.163      yamt 				fill_eproc(p, &kbuf->kproc.kp_eproc, zombie);
   2258  1.118        ad 			} else {
   2259  1.163      yamt 				fill_kproc2(p, &kbuf->kproc2, zombie);
   2260  1.163      yamt 				elem_count--;
   2261    1.1    atatat 			}
   2262  1.163      yamt 			mutex_exit(p->p_lock);
   2263  1.163      yamt 			mutex_exit(proc_lock);
   2264  1.163      yamt 			/*
   2265  1.163      yamt 			 * Copy out elem_size, but not larger than kelem_size
   2266  1.163      yamt 			 */
   2267  1.163      yamt 			error = dcopyout(l, kbuf, dp,
   2268  1.163      yamt 			    min(kelem_size, elem_size));
   2269  1.163      yamt 			mutex_enter(proc_lock);
   2270  1.163      yamt 			if (error) {
   2271  1.163      yamt 				goto bah;
   2272    1.1    atatat 			}
   2273  1.163      yamt 			dp += elem_size;
   2274  1.163      yamt 			buflen -= elem_size;
   2275  1.163      yamt 		} else {
   2276  1.163      yamt 			mutex_exit(p->p_lock);
   2277    1.1    atatat 		}
   2278  1.163      yamt 		needed += elem_size;
   2279  1.118        ad 
   2280  1.118        ad 		/*
   2281  1.118        ad 		 * Release reference to process.
   2282  1.118        ad 		 */
   2283  1.135        ad 	 	if (zombie) {
   2284  1.136        ad 			next = LIST_NEXT(marker, p_list);
   2285  1.135        ad  			LIST_REMOVE(marker, p_list);
   2286  1.135        ad 		} else {
   2287  1.135        ad 			rw_exit(&p->p_reflock);
   2288  1.135        ad 		}
   2289    1.1    atatat 	}
   2290  1.132        ad 	mutex_exit(proc_lock);
   2291    1.1    atatat 
   2292    1.1    atatat 	if (where != NULL) {
   2293  1.163      yamt 		*oldlenp = dp - where;
   2294   1.73  christos 		if (needed > *oldlenp) {
   2295   1.73  christos 			error = ENOMEM;
   2296   1.73  christos 			goto out;
   2297   1.73  christos 		}
   2298    1.1    atatat 	} else {
   2299   1.22    atatat 		needed += KERN_PROCSLOP;
   2300    1.1    atatat 		*oldlenp = needed;
   2301    1.1    atatat 	}
   2302  1.163      yamt 	if (kbuf)
   2303  1.163      yamt 		kmem_free(kbuf, sizeof(*kbuf));
   2304  1.135        ad 	if (marker)
   2305  1.135        ad 		kmem_free(marker, sizeof(*marker));
   2306  1.118        ad 	sysctl_relock();
   2307   1.74  christos 	return 0;
   2308  1.135        ad  bah:
   2309  1.135        ad  	if (zombie)
   2310  1.135        ad  		LIST_REMOVE(marker, p_list);
   2311  1.135        ad 	else
   2312  1.135        ad 		rw_exit(&p->p_reflock);
   2313    1.1    atatat  cleanup:
   2314  1.132        ad 	mutex_exit(proc_lock);
   2315   1.73  christos  out:
   2316  1.163      yamt 	if (kbuf)
   2317  1.163      yamt 		kmem_free(kbuf, sizeof(*kbuf));
   2318  1.135        ad 	if (marker)
   2319  1.135        ad 		kmem_free(marker, sizeof(*marker));
   2320  1.118        ad 	sysctl_relock();
   2321   1.74  christos 	return error;
   2322    1.1    atatat }
   2323    1.1    atatat 
   2324    1.1    atatat /*
   2325    1.1    atatat  * sysctl helper routine for kern.proc_args pseudo-subtree.
   2326    1.1    atatat  */
   2327    1.1    atatat static int
   2328    1.1    atatat sysctl_kern_proc_args(SYSCTLFN_ARGS)
   2329    1.1    atatat {
   2330    1.1    atatat 	struct ps_strings pss;
   2331   1.78        ad 	struct proc *p;
   2332   1.84      manu 	size_t len, i;
   2333    1.1    atatat 	struct uio auio;
   2334    1.1    atatat 	struct iovec aiov;
   2335    1.1    atatat 	pid_t pid;
   2336  1.118        ad 	int nargv, type, error, argvlen;
   2337    1.1    atatat 	char *arg;
   2338   1.84      manu 	char **argv = NULL;
   2339    1.1    atatat 	char *tmp;
   2340   1.56      yamt 	struct vmspace *vmspace;
   2341   1.56      yamt 	vaddr_t psstr_addr;
   2342   1.56      yamt 	vaddr_t offsetn;
   2343   1.56      yamt 	vaddr_t offsetv;
   2344    1.1    atatat 
   2345   1.19    atatat 	if (namelen == 1 && name[0] == CTL_QUERY)
   2346   1.43    atatat 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2347   1.19    atatat 
   2348    1.1    atatat 	if (newp != NULL || namelen != 2)
   2349    1.1    atatat 		return (EINVAL);
   2350    1.1    atatat 	pid = name[0];
   2351    1.1    atatat 	type = name[1];
   2352  1.118        ad 	argv = NULL;
   2353  1.118        ad 	argvlen = 0;
   2354    1.1    atatat 
   2355    1.1    atatat 	switch (type) {
   2356    1.1    atatat 	case KERN_PROC_ARGV:
   2357    1.1    atatat 	case KERN_PROC_NARGV:
   2358    1.1    atatat 	case KERN_PROC_ENV:
   2359    1.1    atatat 	case KERN_PROC_NENV:
   2360    1.1    atatat 		/* ok */
   2361    1.1    atatat 		break;
   2362    1.1    atatat 	default:
   2363    1.1    atatat 		return (EINVAL);
   2364    1.1    atatat 	}
   2365    1.1    atatat 
   2366  1.118        ad 	sysctl_unlock();
   2367   1.56      yamt 
   2368    1.1    atatat 	/* check pid */
   2369  1.132        ad 	mutex_enter(proc_lock);
   2370   1.56      yamt 	if ((p = p_find(pid, PFIND_LOCKED)) == NULL) {
   2371   1.56      yamt 		error = EINVAL;
   2372   1.56      yamt 		goto out_locked;
   2373   1.56      yamt 	}
   2374  1.133        ad 	mutex_enter(p->p_lock);
   2375  1.120      elad 
   2376  1.120      elad 	/* Check permission. */
   2377  1.120      elad 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
   2378  1.120      elad 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
   2379  1.120      elad 		    p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ARGS), NULL, NULL);
   2380  1.120      elad 	else if (type == KERN_PROC_ENV || type == KERN_PROC_NENV)
   2381  1.120      elad 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
   2382  1.120      elad 		    p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENV), NULL, NULL);
   2383  1.120      elad 	else
   2384  1.120      elad 		error = EINVAL; /* XXXGCC */
   2385   1.71      yamt 	if (error) {
   2386  1.133        ad 		mutex_exit(p->p_lock);
   2387   1.56      yamt 		goto out_locked;
   2388   1.56      yamt 	}
   2389   1.55      elad 
   2390    1.1    atatat 	if (oldp == NULL) {
   2391    1.1    atatat 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
   2392    1.1    atatat 			*oldlenp = sizeof (int);
   2393    1.1    atatat 		else
   2394    1.1    atatat 			*oldlenp = ARG_MAX;	/* XXX XXX XXX */
   2395   1.56      yamt 		error = 0;
   2396  1.133        ad 		mutex_exit(p->p_lock);
   2397   1.56      yamt 		goto out_locked;
   2398    1.1    atatat 	}
   2399    1.1    atatat 
   2400    1.1    atatat 	/*
   2401    1.1    atatat 	 * Zombies don't have a stack, so we can't read their psstrings.
   2402    1.1    atatat 	 * System processes also don't have a user stack.
   2403    1.1    atatat 	 */
   2404   1.97     pavel 	if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
   2405   1.56      yamt 		error = EINVAL;
   2406  1.133        ad 		mutex_exit(p->p_lock);
   2407   1.56      yamt 		goto out_locked;
   2408   1.56      yamt 	}
   2409    1.1    atatat 
   2410    1.1    atatat 	/*
   2411    1.1    atatat 	 * Lock the process down in memory.
   2412    1.1    atatat 	 */
   2413   1.56      yamt 	psstr_addr = (vaddr_t)p->p_psstr;
   2414   1.56      yamt 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV) {
   2415   1.56      yamt 		offsetn = p->p_psnargv;
   2416   1.56      yamt 		offsetv = p->p_psargv;
   2417   1.56      yamt 	} else {
   2418   1.56      yamt 		offsetn = p->p_psnenv;
   2419   1.56      yamt 		offsetv = p->p_psenv;
   2420   1.56      yamt 	}
   2421   1.56      yamt 	vmspace = p->p_vmspace;
   2422  1.118        ad 	uvmspace_addref(vmspace);
   2423  1.133        ad 	mutex_exit(p->p_lock);
   2424  1.132        ad 	mutex_exit(proc_lock);
   2425    1.1    atatat 
   2426    1.1    atatat 	/*
   2427    1.1    atatat 	 * Allocate a temporary buffer to hold the arguments.
   2428    1.1    atatat 	 */
   2429  1.118        ad 	arg = kmem_alloc(PAGE_SIZE, KM_SLEEP);
   2430    1.1    atatat 
   2431    1.1    atatat 	/*
   2432    1.1    atatat 	 * Read in the ps_strings structure.
   2433    1.1    atatat 	 */
   2434    1.1    atatat 	aiov.iov_base = &pss;
   2435    1.1    atatat 	aiov.iov_len = sizeof(pss);
   2436    1.1    atatat 	auio.uio_iov = &aiov;
   2437    1.1    atatat 	auio.uio_iovcnt = 1;
   2438   1.56      yamt 	auio.uio_offset = psstr_addr;
   2439    1.1    atatat 	auio.uio_resid = sizeof(pss);
   2440    1.1    atatat 	auio.uio_rw = UIO_READ;
   2441   1.63      yamt 	UIO_SETUP_SYSSPACE(&auio);
   2442   1.56      yamt 	error = uvm_io(&vmspace->vm_map, &auio);
   2443    1.1    atatat 	if (error)
   2444    1.1    atatat 		goto done;
   2445    1.1    atatat 
   2446   1.56      yamt 	memcpy(&nargv, (char *)&pss + offsetn, sizeof(nargv));
   2447    1.1    atatat 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
   2448   1.86      manu 		error = dcopyout(l, &nargv, oldp, sizeof(nargv));
   2449    1.1    atatat 		*oldlenp = sizeof(nargv);
   2450    1.1    atatat 		goto done;
   2451    1.1    atatat 	}
   2452    1.1    atatat 	/*
   2453    1.1    atatat 	 * Now read the address of the argument vector.
   2454    1.1    atatat 	 */
   2455    1.1    atatat 	switch (type) {
   2456    1.1    atatat 	case KERN_PROC_ARGV:
   2457   1.56      yamt 		/* FALLTHROUGH */
   2458    1.1    atatat 	case KERN_PROC_ENV:
   2459   1.56      yamt 		memcpy(&tmp, (char *)&pss + offsetv, sizeof(tmp));
   2460    1.1    atatat 		break;
   2461    1.1    atatat 	default:
   2462  1.118        ad 		error = EINVAL;
   2463  1.118        ad 		goto done;
   2464    1.1    atatat 	}
   2465   1.82      manu 
   2466   1.84      manu #ifdef COMPAT_NETBSD32
   2467   1.97     pavel 	if (p->p_flag & PK_32)
   2468   1.84      manu 		len = sizeof(netbsd32_charp) * nargv;
   2469   1.84      manu 	else
   2470   1.84      manu #endif
   2471   1.84      manu 		len = sizeof(char *) * nargv;
   2472   1.84      manu 
   2473  1.119        ad 	if ((argvlen = len) != 0)
   2474  1.119        ad 		argv = kmem_alloc(len, KM_SLEEP);
   2475   1.84      manu 
   2476   1.84      manu 	aiov.iov_base = argv;
   2477   1.84      manu 	aiov.iov_len = len;
   2478    1.1    atatat 	auio.uio_iov = &aiov;
   2479    1.1    atatat 	auio.uio_iovcnt = 1;
   2480   1.84      manu 	auio.uio_offset = (off_t)(unsigned long)tmp;
   2481   1.84      manu 	auio.uio_resid = len;
   2482    1.1    atatat 	auio.uio_rw = UIO_READ;
   2483   1.63      yamt 	UIO_SETUP_SYSSPACE(&auio);
   2484   1.56      yamt 	error = uvm_io(&vmspace->vm_map, &auio);
   2485    1.1    atatat 	if (error)
   2486    1.1    atatat 		goto done;
   2487    1.1    atatat 
   2488  1.110  christos 	/*
   2489   1.84      manu 	 * Now copy each string.
   2490   1.84      manu 	 */
   2491   1.84      manu 	len = 0; /* bytes written to user buffer */
   2492   1.84      manu 	for (i = 0; i < nargv; i++) {
   2493   1.84      manu 		int finished = 0;
   2494   1.84      manu 		vaddr_t base;
   2495   1.84      manu 		size_t xlen;
   2496   1.84      manu 		int j;
   2497   1.84      manu 
   2498   1.82      manu #ifdef COMPAT_NETBSD32
   2499   1.97     pavel 		if (p->p_flag & PK_32) {
   2500   1.84      manu 			netbsd32_charp *argv32;
   2501   1.84      manu 
   2502   1.84      manu 			argv32 = (netbsd32_charp *)argv;
   2503   1.84      manu 			base = (vaddr_t)NETBSD32PTR64(argv32[i]);
   2504   1.84      manu 		} else
   2505   1.82      manu #endif
   2506   1.84      manu 			base = (vaddr_t)argv[i];
   2507   1.82      manu 
   2508  1.111  christos 		/*
   2509  1.111  christos 		 * The program has messed around with its arguments,
   2510  1.111  christos 		 * possibly deleting some, and replacing them with
   2511  1.111  christos 		 * NULL's. Treat this as the last argument and not
   2512  1.111  christos 		 * a failure.
   2513  1.111  christos 		 */
   2514  1.111  christos 		if (base == 0)
   2515  1.111  christos 			break;
   2516  1.111  christos 
   2517   1.84      manu 		while (!finished) {
   2518   1.84      manu 			xlen = PAGE_SIZE - (base & PAGE_MASK);
   2519    1.1    atatat 
   2520   1.84      manu 			aiov.iov_base = arg;
   2521   1.84      manu 			aiov.iov_len = PAGE_SIZE;
   2522   1.84      manu 			auio.uio_iov = &aiov;
   2523   1.84      manu 			auio.uio_iovcnt = 1;
   2524   1.84      manu 			auio.uio_offset = base;
   2525   1.84      manu 			auio.uio_resid = xlen;
   2526   1.84      manu 			auio.uio_rw = UIO_READ;
   2527   1.84      manu 			UIO_SETUP_SYSSPACE(&auio);
   2528   1.84      manu 			error = uvm_io(&vmspace->vm_map, &auio);
   2529   1.84      manu 			if (error)
   2530   1.84      manu 				goto done;
   2531    1.1    atatat 
   2532   1.84      manu 			/* Look for the end of the string */
   2533   1.84      manu 			for (j = 0; j < xlen; j++) {
   2534   1.84      manu 				if (arg[j] == '\0') {
   2535   1.84      manu 					xlen = j + 1;
   2536   1.84      manu 					finished = 1;
   2537   1.84      manu 					break;
   2538   1.84      manu 				}
   2539   1.84      manu 			}
   2540    1.1    atatat 
   2541   1.84      manu 			/* Check for user buffer overflow */
   2542   1.84      manu 			if (len + xlen > *oldlenp) {
   2543   1.84      manu 				finished = 1;
   2544  1.110  christos 				if (len > *oldlenp)
   2545   1.84      manu 					xlen = 0;
   2546   1.84      manu 				else
   2547   1.84      manu 					xlen = *oldlenp - len;
   2548   1.84      manu 			}
   2549  1.110  christos 
   2550   1.84      manu 			/* Copyout the page */
   2551   1.86      manu 			error = dcopyout(l, arg, (char *)oldp + len, xlen);
   2552   1.84      manu 			if (error)
   2553   1.84      manu 				goto done;
   2554    1.1    atatat 
   2555   1.84      manu 			len += xlen;
   2556   1.84      manu 			base += xlen;
   2557    1.1    atatat 		}
   2558    1.1    atatat 	}
   2559    1.1    atatat 	*oldlenp = len;
   2560    1.1    atatat 
   2561    1.1    atatat done:
   2562  1.119        ad 	if (argvlen != 0)
   2563  1.118        ad 		kmem_free(argv, argvlen);
   2564   1.56      yamt 	uvmspace_free(vmspace);
   2565  1.118        ad 	kmem_free(arg, PAGE_SIZE);
   2566  1.118        ad 	sysctl_relock();
   2567   1.56      yamt 	return error;
   2568   1.56      yamt 
   2569   1.56      yamt out_locked:
   2570  1.132        ad 	mutex_exit(proc_lock);
   2571  1.118        ad 	sysctl_relock();
   2572   1.56      yamt 	return error;
   2573    1.1    atatat }
   2574    1.1    atatat 
   2575   1.61      elad static int
   2576   1.61      elad sysctl_security_setidcore(SYSCTLFN_ARGS)
   2577   1.61      elad {
   2578   1.61      elad 	int newsize, error;
   2579   1.61      elad 	struct sysctlnode node;
   2580   1.61      elad 
   2581   1.61      elad 	node = *rnode;
   2582   1.61      elad 	node.sysctl_data = &newsize;
   2583   1.61      elad 	newsize = *(int *)rnode->sysctl_data;
   2584   1.61      elad 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2585   1.61      elad 	if (error || newp == NULL)
   2586   1.61      elad 		return error;
   2587   1.61      elad 
   2588   1.83      elad 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
   2589   1.85      elad 	    0, NULL, NULL, NULL))
   2590   1.61      elad 		return (EPERM);
   2591   1.61      elad 
   2592   1.61      elad 	*(int *)rnode->sysctl_data = newsize;
   2593   1.61      elad 
   2594   1.61      elad 	return 0;
   2595   1.61      elad }
   2596   1.61      elad 
   2597   1.61      elad static int
   2598   1.61      elad sysctl_security_setidcorename(SYSCTLFN_ARGS)
   2599   1.61      elad {
   2600   1.61      elad 	int error;
   2601   1.72      yamt 	char *newsetidcorename;
   2602   1.61      elad 	struct sysctlnode node;
   2603   1.61      elad 
   2604   1.72      yamt 	newsetidcorename = PNBUF_GET();
   2605   1.61      elad 	node = *rnode;
   2606   1.72      yamt 	node.sysctl_data = newsetidcorename;
   2607   1.61      elad 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
   2608   1.61      elad 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2609   1.72      yamt 	if (error || newp == NULL) {
   2610   1.72      yamt 		goto out;
   2611   1.72      yamt 	}
   2612   1.83      elad 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
   2613   1.85      elad 	    0, NULL, NULL, NULL)) {
   2614   1.72      yamt 		error = EPERM;
   2615   1.72      yamt 		goto out;
   2616   1.72      yamt 	}
   2617   1.72      yamt 	if (strlen(newsetidcorename) == 0) {
   2618   1.72      yamt 		error = EINVAL;
   2619   1.72      yamt 		goto out;
   2620   1.72      yamt 	}
   2621   1.61      elad 	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
   2622   1.72      yamt out:
   2623   1.72      yamt 	PNBUF_PUT(newsetidcorename);
   2624   1.72      yamt 	return error;
   2625   1.61      elad }
   2626   1.61      elad 
   2627   1.38      elad /*
   2628  1.110  christos  * sysctl helper routine for kern.cp_id node. Maps cpus to their
   2629   1.45  christos  * cpuids.
   2630   1.45  christos  */
   2631   1.45  christos static int
   2632   1.45  christos sysctl_kern_cpid(SYSCTLFN_ARGS)
   2633   1.45  christos {
   2634   1.45  christos 	struct sysctlnode node = *rnode;
   2635   1.59     perry 	uint64_t *cp_id = NULL;
   2636   1.96        ad 	int error, n = ncpu;
   2637   1.45  christos 	struct cpu_info *ci;
   2638   1.45  christos 	CPU_INFO_ITERATOR cii;
   2639   1.45  christos 
   2640   1.45  christos 	/*
   2641  1.110  christos 	 * Here you may either retrieve a single cpu id or the whole
   2642  1.110  christos 	 * set. The size you get back when probing depends on what
   2643   1.46    atatat 	 * you ask for.
   2644   1.45  christos 	 */
   2645   1.45  christos 	switch (namelen) {
   2646   1.45  christos 	case 0:
   2647   1.59     perry 		node.sysctl_size = n * sizeof(uint64_t);
   2648   1.45  christos 		n = -2; /* ALL */
   2649   1.45  christos 		break;
   2650   1.45  christos 	case 1:
   2651   1.45  christos 		if (name[0] < 0 || name[0] >= n)
   2652   1.45  christos 			return (ENOENT); /* ENOSUCHPROCESSOR */
   2653   1.59     perry 		node.sysctl_size = sizeof(uint64_t);
   2654   1.45  christos 		n = name[0];
   2655   1.45  christos 		/*
   2656   1.45  christos 		 * adjust these so that sysctl_lookup() will be happy
   2657   1.45  christos 		 */
   2658   1.45  christos 		name++;
   2659   1.45  christos 		namelen--;
   2660   1.45  christos 		break;
   2661   1.45  christos 	default:
   2662   1.45  christos 		return (EINVAL);
   2663   1.45  christos 	}
   2664   1.45  christos 
   2665  1.118        ad 	cp_id = kmem_alloc(node.sysctl_size, KM_SLEEP);
   2666   1.45  christos 	if (cp_id == NULL)
   2667   1.45  christos 		return (ENOMEM);
   2668   1.45  christos 	node.sysctl_data = cp_id;
   2669   1.45  christos 	memset(cp_id, 0, node.sysctl_size);
   2670   1.45  christos 
   2671   1.45  christos 	for (CPU_INFO_FOREACH(cii, ci)) {
   2672   1.45  christos 		if (n <= 0)
   2673  1.138        ad 			cp_id[0] = cpu_index(ci);
   2674   1.45  christos 		/*
   2675   1.45  christos 		 * if a specific processor was requested and we just
   2676   1.45  christos 		 * did it, we're done here
   2677   1.45  christos 		 */
   2678   1.45  christos 		if (n == 0)
   2679   1.45  christos 			break;
   2680   1.45  christos 		/*
   2681   1.45  christos 		 * if doing "all", skip to next cp_id slot for next processor
   2682   1.45  christos 		 */
   2683   1.45  christos 		if (n == -2)
   2684   1.45  christos 			cp_id++;
   2685   1.45  christos 		/*
   2686   1.45  christos 		 * if we're doing a specific processor, we're one
   2687   1.45  christos 		 * processor closer
   2688   1.45  christos 		 */
   2689   1.45  christos 		if (n > 0)
   2690   1.45  christos 			n--;
   2691   1.45  christos 	}
   2692   1.45  christos 
   2693   1.45  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2694  1.118        ad 	kmem_free(node.sysctl_data, node.sysctl_size);
   2695   1.45  christos 	return (error);
   2696   1.45  christos }
   2697   1.45  christos 
   2698   1.45  christos /*
   2699  1.110  christos  * sysctl helper routine for hw.usermem and hw.usermem64. Values are
   2700    1.1    atatat  * calculate on the fly taking into account integer overflow and the
   2701    1.1    atatat  * current wired count.
   2702    1.1    atatat  */
   2703    1.1    atatat static int
   2704    1.1    atatat sysctl_hw_usermem(SYSCTLFN_ARGS)
   2705    1.1    atatat {
   2706    1.1    atatat 	u_int ui;
   2707    1.1    atatat 	u_quad_t uq;
   2708    1.1    atatat 	struct sysctlnode node;
   2709    1.1    atatat 
   2710    1.1    atatat 	node = *rnode;
   2711    1.1    atatat 	switch (rnode->sysctl_num) {
   2712  1.155    cegger 	case HW_USERMEM:
   2713    1.1    atatat 		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
   2714    1.1    atatat 			ui = UINT_MAX;
   2715    1.1    atatat 		else
   2716    1.1    atatat 			ui *= PAGE_SIZE;
   2717    1.1    atatat 		node.sysctl_data = &ui;
   2718    1.1    atatat 		break;
   2719    1.1    atatat 	case HW_USERMEM64:
   2720    1.1    atatat 		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
   2721    1.1    atatat 		node.sysctl_data = &uq;
   2722    1.1    atatat 		break;
   2723    1.1    atatat 	default:
   2724    1.1    atatat 		return (EINVAL);
   2725    1.1    atatat 	}
   2726    1.1    atatat 
   2727    1.1    atatat 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2728    1.1    atatat }
   2729    1.1    atatat 
   2730    1.1    atatat /*
   2731  1.110  christos  * sysctl helper routine for kern.cnmagic node. Pulls the old value
   2732    1.1    atatat  * out, encoded, and stuffs the new value in for decoding.
   2733    1.1    atatat  */
   2734    1.1    atatat static int
   2735    1.1    atatat sysctl_hw_cnmagic(SYSCTLFN_ARGS)
   2736    1.1    atatat {
   2737    1.1    atatat 	char magic[CNS_LEN];
   2738    1.1    atatat 	int error;
   2739    1.1    atatat 	struct sysctlnode node;
   2740    1.1    atatat 
   2741    1.1    atatat 	if (oldp)
   2742    1.1    atatat 		cn_get_magic(magic, CNS_LEN);
   2743    1.1    atatat 	node = *rnode;
   2744    1.1    atatat 	node.sysctl_data = &magic[0];
   2745    1.1    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2746    1.1    atatat 	if (error || newp == NULL)
   2747    1.1    atatat 		return (error);
   2748   1.33     perry 
   2749    1.1    atatat 	return (cn_set_magic(magic));
   2750    1.1    atatat }
   2751    1.3    martin 
   2752    1.1    atatat /*
   2753    1.1    atatat  * ********************************************************************
   2754    1.1    atatat  * section 3: public helper routines that are used for more than one
   2755    1.1    atatat  * node
   2756    1.1    atatat  * ********************************************************************
   2757    1.1    atatat  */
   2758    1.1    atatat 
   2759    1.1    atatat /*
   2760    1.1    atatat  * sysctl helper routine for the kern.root_device node and some ports'
   2761    1.1    atatat  * machdep.root_device nodes.
   2762    1.1    atatat  */
   2763    1.1    atatat int
   2764    1.1    atatat sysctl_root_device(SYSCTLFN_ARGS)
   2765    1.1    atatat {
   2766    1.1    atatat 	struct sysctlnode node;
   2767    1.1    atatat 
   2768    1.1    atatat 	node = *rnode;
   2769    1.1    atatat 	node.sysctl_data = root_device->dv_xname;
   2770  1.130    cegger 	node.sysctl_size = strlen(device_xname(root_device)) + 1;
   2771    1.1    atatat 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2772    1.1    atatat }
   2773    1.1    atatat 
   2774    1.1    atatat /*
   2775    1.1    atatat  * sysctl helper routine for kern.consdev, dependent on the current
   2776  1.110  christos  * state of the console. Also used for machdep.console_device on some
   2777    1.1    atatat  * ports.
   2778    1.1    atatat  */
   2779    1.1    atatat int
   2780    1.1    atatat sysctl_consdev(SYSCTLFN_ARGS)
   2781    1.1    atatat {
   2782    1.1    atatat 	dev_t consdev;
   2783  1.165  christos 	uint32_t oconsdev;
   2784    1.1    atatat 	struct sysctlnode node;
   2785    1.1    atatat 
   2786    1.1    atatat 	if (cn_tab != NULL)
   2787    1.1    atatat 		consdev = cn_tab->cn_dev;
   2788    1.1    atatat 	else
   2789    1.1    atatat 		consdev = NODEV;
   2790    1.1    atatat 	node = *rnode;
   2791  1.165  christos 	switch (*oldlenp) {
   2792  1.165  christos 	case sizeof(consdev):
   2793  1.165  christos 		node.sysctl_data = &consdev;
   2794  1.165  christos 		node.sysctl_size = sizeof(consdev);
   2795  1.165  christos 		break;
   2796  1.165  christos 	case sizeof(oconsdev):
   2797  1.165  christos 		oconsdev = (uint32_t)consdev;
   2798  1.165  christos 		node.sysctl_data = &oconsdev;
   2799  1.165  christos 		node.sysctl_size = sizeof(oconsdev);
   2800  1.165  christos 		break;
   2801  1.165  christos 	default:
   2802  1.165  christos 		return EINVAL;
   2803  1.165  christos 	}
   2804    1.1    atatat 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2805    1.1    atatat }
   2806    1.1    atatat 
   2807    1.1    atatat /*
   2808    1.1    atatat  * ********************************************************************
   2809    1.1    atatat  * section 4: support for some helpers
   2810    1.1    atatat  * ********************************************************************
   2811    1.1    atatat  */
   2812  1.147  christos /*
   2813  1.147  christos  * Find the most ``active'' lwp of a process and return it for ps display
   2814  1.147  christos  * purposes
   2815  1.147  christos  */
   2816  1.146  christos static struct lwp *
   2817  1.147  christos proc_active_lwp(struct proc *p)
   2818  1.146  christos {
   2819  1.146  christos 	static const int ostat[] = {
   2820  1.146  christos 		0,
   2821  1.146  christos 		2,	/* LSIDL */
   2822  1.146  christos 		6,	/* LSRUN */
   2823  1.146  christos 		5,	/* LSSLEEP */
   2824  1.146  christos 		4,	/* LSSTOP */
   2825  1.146  christos 		0,	/* LSZOMB */
   2826  1.146  christos 		1,	/* LSDEAD */
   2827  1.146  christos 		7,	/* LSONPROC */
   2828  1.146  christos 		3	/* LSSUSPENDED */
   2829  1.146  christos 	};
   2830  1.146  christos 
   2831  1.146  christos 	struct lwp *l, *lp = NULL;
   2832  1.146  christos 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2833  1.146  christos 		KASSERT(l->l_stat >= 0 && l->l_stat < __arraycount(ostat));
   2834  1.146  christos 		if (lp == NULL ||
   2835  1.146  christos 		    ostat[l->l_stat] > ostat[lp->l_stat] ||
   2836  1.146  christos 		    (ostat[l->l_stat] == ostat[lp->l_stat] &&
   2837  1.146  christos 		    l->l_cpticks > lp->l_cpticks)) {
   2838  1.146  christos 			lp = l;
   2839  1.146  christos 			continue;
   2840  1.146  christos 		}
   2841  1.146  christos 	}
   2842  1.146  christos 	return lp;
   2843  1.146  christos }
   2844  1.146  christos 
   2845    1.1    atatat 
   2846    1.1    atatat /*
   2847    1.1    atatat  * Fill in a kinfo_proc2 structure for the specified process.
   2848    1.1    atatat  */
   2849    1.1    atatat static void
   2850  1.135        ad fill_kproc2(struct proc *p, struct kinfo_proc2 *ki, bool zombie)
   2851    1.1    atatat {
   2852    1.1    atatat 	struct tty *tp;
   2853   1.95        ad 	struct lwp *l, *l2;
   2854   1.95        ad 	struct timeval ut, st, rt;
   2855   1.95        ad 	sigset_t ss1, ss2;
   2856  1.125        ad 	struct rusage ru;
   2857  1.135        ad 	struct vmspace *vm;
   2858    1.1    atatat 
   2859  1.132        ad 	KASSERT(mutex_owned(proc_lock));
   2860  1.133        ad 	KASSERT(mutex_owned(p->p_lock));
   2861  1.118        ad 
   2862  1.135        ad 	sigemptyset(&ss1);
   2863  1.135        ad 	sigemptyset(&ss2);
   2864    1.1    atatat 	memset(ki, 0, sizeof(*ki));
   2865    1.1    atatat 
   2866   1.21    atatat 	ki->p_paddr = PTRTOUINT64(p);
   2867   1.21    atatat 	ki->p_fd = PTRTOUINT64(p->p_fd);
   2868   1.21    atatat 	ki->p_cwdi = PTRTOUINT64(p->p_cwdi);
   2869   1.21    atatat 	ki->p_stats = PTRTOUINT64(p->p_stats);
   2870   1.21    atatat 	ki->p_limit = PTRTOUINT64(p->p_limit);
   2871   1.21    atatat 	ki->p_vmspace = PTRTOUINT64(p->p_vmspace);
   2872   1.21    atatat 	ki->p_sigacts = PTRTOUINT64(p->p_sigacts);
   2873   1.21    atatat 	ki->p_sess = PTRTOUINT64(p->p_session);
   2874    1.1    atatat 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
   2875  1.100       dsl 	ki->p_ru = PTRTOUINT64(&p->p_stats->p_ru);
   2876    1.1    atatat 	ki->p_eflag = 0;
   2877    1.1    atatat 	ki->p_exitsig = p->p_exitsig;
   2878  1.169       dsl 	ki->p_flag = L_INMEM;   /* Process never swapped out */
   2879  1.169       dsl 	ki->p_flag |= sysctl_map_flags(sysctl_flagmap, p->p_flag);
   2880   1.95        ad 	ki->p_flag |= sysctl_map_flags(sysctl_sflagmap, p->p_sflag);
   2881   1.95        ad 	ki->p_flag |= sysctl_map_flags(sysctl_slflagmap, p->p_slflag);
   2882   1.95        ad 	ki->p_flag |= sysctl_map_flags(sysctl_lflagmap, p->p_lflag);
   2883   1.95        ad 	ki->p_flag |= sysctl_map_flags(sysctl_stflagmap, p->p_stflag);
   2884    1.1    atatat 	ki->p_pid = p->p_pid;
   2885    1.1    atatat 	if (p->p_pptr)
   2886    1.1    atatat 		ki->p_ppid = p->p_pptr->p_pid;
   2887    1.1    atatat 	else
   2888    1.1    atatat 		ki->p_ppid = 0;
   2889   1.68      elad 	ki->p_uid = kauth_cred_geteuid(p->p_cred);
   2890   1.68      elad 	ki->p_ruid = kauth_cred_getuid(p->p_cred);
   2891   1.68      elad 	ki->p_gid = kauth_cred_getegid(p->p_cred);
   2892   1.68      elad 	ki->p_rgid = kauth_cred_getgid(p->p_cred);
   2893   1.68      elad 	ki->p_svuid = kauth_cred_getsvuid(p->p_cred);
   2894   1.68      elad 	ki->p_svgid = kauth_cred_getsvgid(p->p_cred);
   2895   1.68      elad 	ki->p_ngroups = kauth_cred_ngroups(p->p_cred);
   2896   1.68      elad 	kauth_cred_getgroups(p->p_cred, ki->p_groups,
   2897  1.102       dsl 	    min(ki->p_ngroups, sizeof(ki->p_groups) / sizeof(ki->p_groups[0])),
   2898  1.102       dsl 	    UIO_SYSSPACE);
   2899    1.1    atatat 
   2900    1.1    atatat 	ki->p_uticks = p->p_uticks;
   2901    1.1    atatat 	ki->p_sticks = p->p_sticks;
   2902    1.1    atatat 	ki->p_iticks = p->p_iticks;
   2903  1.135        ad 	ki->p_tpgid = NO_PGID;	/* may be changed if controlling tty below */
   2904   1.21    atatat 	ki->p_tracep = PTRTOUINT64(p->p_tracep);
   2905    1.1    atatat 	ki->p_traceflag = p->p_traceflag;
   2906    1.1    atatat 
   2907    1.1    atatat 	memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
   2908    1.1    atatat 	memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
   2909    1.1    atatat 
   2910  1.101      yamt 	ki->p_cpticks = 0;
   2911  1.101      yamt 	ki->p_pctcpu = p->p_pctcpu;
   2912  1.113        ad 	ki->p_estcpu = 0;
   2913    1.1    atatat 	ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
   2914    1.1    atatat 	ki->p_realstat = p->p_stat;
   2915    1.1    atatat 	ki->p_nice = p->p_nice;
   2916    1.1    atatat 	ki->p_xstat = p->p_xstat;
   2917    1.1    atatat 	ki->p_acflag = p->p_acflag;
   2918    1.1    atatat 
   2919    1.1    atatat 	strncpy(ki->p_comm, p->p_comm,
   2920    1.1    atatat 	    min(sizeof(ki->p_comm), sizeof(p->p_comm)));
   2921  1.139  christos 	strncpy(ki->p_ename, p->p_emul->e_name, sizeof(ki->p_ename));
   2922    1.1    atatat 
   2923    1.1    atatat 	ki->p_nlwps = p->p_nlwps;
   2924   1.95        ad 	ki->p_realflag = ki->p_flag;
   2925    1.1    atatat 
   2926  1.135        ad 	if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
   2927  1.135        ad 		vm = p->p_vmspace;
   2928    1.1    atatat 		ki->p_vm_rssize = vm_resident_count(vm);
   2929    1.1    atatat 		ki->p_vm_tsize = vm->vm_tsize;
   2930    1.1    atatat 		ki->p_vm_dsize = vm->vm_dsize;
   2931    1.1    atatat 		ki->p_vm_ssize = vm->vm_ssize;
   2932  1.160       mrg 		ki->p_vm_vsize = vm->vm_map.size;
   2933  1.160       mrg 		/*
   2934  1.160       mrg 		 * Since the stack is initially mapped mostly with
   2935  1.160       mrg 		 * PROT_NONE and grown as needed, adjust the "mapped size"
   2936  1.160       mrg 		 * to skip the unused stack portion.
   2937  1.160       mrg 		 */
   2938  1.160       mrg 		ki->p_vm_msize =
   2939  1.160       mrg 		    atop(vm->vm_map.size) - vm->vm_issize + vm->vm_ssize;
   2940    1.1    atatat 
   2941  1.143     rmind 		/* Pick the primary (first) LWP */
   2942  1.147  christos 		l = proc_active_lwp(p);
   2943  1.143     rmind 		KASSERT(l != NULL);
   2944   1.95        ad 		lwp_lock(l);
   2945  1.143     rmind 		ki->p_nrlwps = p->p_nrlwps;
   2946  1.101      yamt 		ki->p_forw = 0;
   2947  1.101      yamt 		ki->p_back = 0;
   2948   1.21    atatat 		ki->p_addr = PTRTOUINT64(l->l_addr);
   2949    1.1    atatat 		ki->p_stat = l->l_stat;
   2950   1.95        ad 		ki->p_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
   2951    1.1    atatat 		ki->p_swtime = l->l_swtime;
   2952    1.1    atatat 		ki->p_slptime = l->l_slptime;
   2953   1.95        ad 		if (l->l_stat == LSONPROC)
   2954    1.1    atatat 			ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2955   1.95        ad 		else
   2956    1.1    atatat 			ki->p_schedflags = 0;
   2957  1.113        ad 		ki->p_priority = lwp_eprio(l);
   2958  1.113        ad 		ki->p_usrpri = l->l_priority;
   2959  1.131      yamt 		if (l->l_wchan)
   2960  1.129   xtraeme 			strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
   2961   1.21    atatat 		ki->p_wchan = PTRTOUINT64(l->l_wchan);
   2962  1.138        ad 		ki->p_cpuid = cpu_index(l->l_cpu);
   2963   1.95        ad 		lwp_unlock(l);
   2964  1.135        ad 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2965  1.135        ad 			/* This is hardly correct, but... */
   2966  1.135        ad 			sigplusset(&l->l_sigpend.sp_set, &ss1);
   2967  1.135        ad 			sigplusset(&l->l_sigmask, &ss2);
   2968  1.135        ad 			ki->p_cpticks += l->l_cpticks;
   2969  1.135        ad 			ki->p_pctcpu += l->l_pctcpu;
   2970  1.135        ad 			ki->p_estcpu += l->l_estcpu;
   2971  1.135        ad 		}
   2972    1.1    atatat 	}
   2973  1.135        ad 	sigplusset(&p->p_sigpend.sp_set, &ss2);
   2974  1.135        ad 	memcpy(&ki->p_siglist, &ss1, sizeof(ki_sigset_t));
   2975  1.135        ad 	memcpy(&ki->p_sigmask, &ss2, sizeof(ki_sigset_t));
   2976  1.135        ad 
   2977  1.135        ad 	if (p->p_session != NULL) {
   2978  1.135        ad 		ki->p_sid = p->p_session->s_sid;
   2979  1.135        ad 		ki->p__pgid = p->p_pgrp->pg_id;
   2980  1.135        ad 		if (p->p_session->s_ttyvp)
   2981  1.135        ad 			ki->p_eflag |= EPROC_CTTY;
   2982  1.135        ad 		if (SESS_LEADER(p))
   2983  1.135        ad 			ki->p_eflag |= EPROC_SLEADER;
   2984  1.135        ad 		strncpy(ki->p_login, p->p_session->s_login,
   2985  1.135        ad 		    min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
   2986  1.135        ad 		ki->p_jobc = p->p_pgrp->pg_jobc;
   2987  1.135        ad 		if ((p->p_lflag & PL_CONTROLT) && (tp = p->p_session->s_ttyp)) {
   2988  1.135        ad 			ki->p_tdev = tp->t_dev;
   2989  1.135        ad 			ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2990  1.135        ad 			ki->p_tsess = PTRTOUINT64(tp->t_session);
   2991  1.135        ad 		} else {
   2992  1.153  christos 			ki->p_tdev = (int32_t)NODEV;
   2993  1.135        ad 		}
   2994  1.135        ad 	}
   2995  1.135        ad 
   2996  1.135        ad 	if (!P_ZOMBIE(p) && !zombie) {
   2997    1.1    atatat 		ki->p_uvalid = 1;
   2998    1.1    atatat 		ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
   2999    1.1    atatat 		ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
   3000    1.1    atatat 
   3001   1.95        ad 		calcru(p, &ut, &st, NULL, &rt);
   3002   1.95        ad 		ki->p_rtime_sec = rt.tv_sec;
   3003   1.95        ad 		ki->p_rtime_usec = rt.tv_usec;
   3004    1.1    atatat 		ki->p_uutime_sec = ut.tv_sec;
   3005    1.1    atatat 		ki->p_uutime_usec = ut.tv_usec;
   3006    1.1    atatat 		ki->p_ustime_sec = st.tv_sec;
   3007    1.1    atatat 		ki->p_ustime_usec = st.tv_usec;
   3008    1.1    atatat 
   3009  1.125        ad 		memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
   3010   1.95        ad 		ki->p_uru_nvcsw = 0;
   3011   1.95        ad 		ki->p_uru_nivcsw = 0;
   3012   1.95        ad 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   3013  1.135        ad 			ki->p_uru_nvcsw += (l2->l_ncsw - l2->l_nivcsw);
   3014  1.135        ad 			ki->p_uru_nivcsw += l2->l_nivcsw;
   3015  1.125        ad 			ruadd(&ru, &l2->l_ru);
   3016   1.95        ad 		}
   3017  1.125        ad 		ki->p_uru_maxrss = ru.ru_maxrss;
   3018  1.125        ad 		ki->p_uru_ixrss = ru.ru_ixrss;
   3019  1.125        ad 		ki->p_uru_idrss = ru.ru_idrss;
   3020  1.125        ad 		ki->p_uru_isrss = ru.ru_isrss;
   3021  1.125        ad 		ki->p_uru_minflt = ru.ru_minflt;
   3022  1.125        ad 		ki->p_uru_majflt = ru.ru_majflt;
   3023  1.125        ad 		ki->p_uru_nswap = ru.ru_nswap;
   3024  1.125        ad 		ki->p_uru_inblock = ru.ru_inblock;
   3025  1.125        ad 		ki->p_uru_oublock = ru.ru_oublock;
   3026  1.125        ad 		ki->p_uru_msgsnd = ru.ru_msgsnd;
   3027  1.125        ad 		ki->p_uru_msgrcv = ru.ru_msgrcv;
   3028  1.125        ad 		ki->p_uru_nsignals = ru.ru_nsignals;
   3029    1.1    atatat 
   3030    1.1    atatat 		timeradd(&p->p_stats->p_cru.ru_utime,
   3031    1.1    atatat 			 &p->p_stats->p_cru.ru_stime, &ut);
   3032    1.1    atatat 		ki->p_uctime_sec = ut.tv_sec;
   3033    1.1    atatat 		ki->p_uctime_usec = ut.tv_usec;
   3034    1.1    atatat 	}
   3035    1.1    atatat }
   3036    1.1    atatat 
   3037    1.1    atatat /*
   3038    1.1    atatat  * Fill in a kinfo_lwp structure for the specified lwp.
   3039    1.1    atatat  */
   3040    1.1    atatat static void
   3041    1.1    atatat fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
   3042    1.1    atatat {
   3043  1.101      yamt 	struct proc *p = l->l_proc;
   3044  1.115      yamt 	struct timeval tv;
   3045    1.1    atatat 
   3046  1.131      yamt 	KASSERT(lwp_locked(l, NULL));
   3047  1.131      yamt 
   3048  1.101      yamt 	kl->l_forw = 0;
   3049  1.101      yamt 	kl->l_back = 0;
   3050   1.21    atatat 	kl->l_laddr = PTRTOUINT64(l);
   3051   1.21    atatat 	kl->l_addr = PTRTOUINT64(l->l_addr);
   3052    1.1    atatat 	kl->l_stat = l->l_stat;
   3053    1.1    atatat 	kl->l_lid = l->l_lid;
   3054  1.170       dsl 	kl->l_flag = L_INMEM;
   3055  1.170       dsl 	kl->l_flag |= sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag);
   3056  1.144  christos 	kl->l_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
   3057    1.1    atatat 
   3058    1.1    atatat 	kl->l_swtime = l->l_swtime;
   3059    1.1    atatat 	kl->l_slptime = l->l_slptime;
   3060   1.95        ad 	if (l->l_stat == LSONPROC)
   3061    1.1    atatat 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   3062   1.95        ad 	else
   3063    1.1    atatat 		kl->l_schedflags = 0;
   3064  1.113        ad 	kl->l_priority = lwp_eprio(l);
   3065  1.113        ad 	kl->l_usrpri = l->l_priority;
   3066  1.131      yamt 	if (l->l_wchan)
   3067  1.129   xtraeme 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
   3068   1.21    atatat 	kl->l_wchan = PTRTOUINT64(l->l_wchan);
   3069  1.138        ad 	kl->l_cpuid = cpu_index(l->l_cpu);
   3070  1.115      yamt 	bintime2timeval(&l->l_rtime, &tv);
   3071  1.115      yamt 	kl->l_rtime_sec = tv.tv_sec;
   3072  1.115      yamt 	kl->l_rtime_usec = tv.tv_usec;
   3073  1.101      yamt 	kl->l_cpticks = l->l_cpticks;
   3074  1.101      yamt 	kl->l_pctcpu = l->l_pctcpu;
   3075  1.101      yamt 	kl->l_pid = p->p_pid;
   3076  1.101      yamt 	if (l->l_name == NULL)
   3077  1.101      yamt 		kl->l_name[0] = '\0';
   3078  1.128   xtraeme 	else
   3079  1.129   xtraeme 		strlcpy(kl->l_name, l->l_name, sizeof(kl->l_name));
   3080    1.1    atatat }
   3081    1.1    atatat 
   3082    1.1    atatat /*
   3083    1.1    atatat  * Fill in an eproc structure for the specified process.
   3084    1.1    atatat  */
   3085    1.1    atatat void
   3086  1.135        ad fill_eproc(struct proc *p, struct eproc *ep, bool zombie)
   3087    1.1    atatat {
   3088    1.1    atatat 	struct tty *tp;
   3089    1.1    atatat 	struct lwp *l;
   3090    1.1    atatat 
   3091  1.132        ad 	KASSERT(mutex_owned(proc_lock));
   3092  1.133        ad 	KASSERT(mutex_owned(p->p_lock));
   3093  1.118        ad 
   3094  1.135        ad 	memset(ep, 0, sizeof(*ep));
   3095  1.135        ad 
   3096    1.1    atatat 	ep->e_paddr = p;
   3097    1.1    atatat 	ep->e_sess = p->p_session;
   3098  1.135        ad 	if (p->p_cred) {
   3099  1.135        ad 		kauth_cred_topcred(p->p_cred, &ep->e_pcred);
   3100  1.135        ad 		kauth_cred_toucred(p->p_cred, &ep->e_ucred);
   3101  1.135        ad 	}
   3102  1.135        ad 	if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
   3103    1.1    atatat 		struct vmspace *vm = p->p_vmspace;
   3104    1.1    atatat 
   3105    1.1    atatat 		ep->e_vm.vm_rssize = vm_resident_count(vm);
   3106    1.1    atatat 		ep->e_vm.vm_tsize = vm->vm_tsize;
   3107    1.1    atatat 		ep->e_vm.vm_dsize = vm->vm_dsize;
   3108    1.1    atatat 		ep->e_vm.vm_ssize = vm->vm_ssize;
   3109  1.160       mrg 		ep->e_vm.vm_map.size = vm->vm_map.size;
   3110    1.1    atatat 
   3111  1.143     rmind 		/* Pick the primary (first) LWP */
   3112  1.147  christos 		l = proc_active_lwp(p);
   3113  1.143     rmind 		KASSERT(l != NULL);
   3114   1.95        ad 		lwp_lock(l);
   3115  1.131      yamt 		if (l->l_wchan)
   3116    1.1    atatat 			strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
   3117   1.95        ad 		lwp_unlock(l);
   3118    1.1    atatat 	}
   3119    1.1    atatat 	if (p->p_pptr)
   3120    1.1    atatat 		ep->e_ppid = p->p_pptr->p_pid;
   3121  1.135        ad 	if (p->p_pgrp && p->p_session) {
   3122  1.135        ad 		ep->e_pgid = p->p_pgrp->pg_id;
   3123  1.135        ad 		ep->e_jobc = p->p_pgrp->pg_jobc;
   3124  1.135        ad 		ep->e_sid = p->p_session->s_sid;
   3125  1.135        ad 		if ((p->p_lflag & PL_CONTROLT) &&
   3126  1.135        ad 		    (tp = ep->e_sess->s_ttyp)) {
   3127  1.135        ad 			ep->e_tdev = tp->t_dev;
   3128  1.135        ad 			ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   3129  1.135        ad 			ep->e_tsess = tp->t_session;
   3130  1.135        ad 		} else
   3131  1.153  christos 			ep->e_tdev = (uint32_t)NODEV;
   3132  1.135        ad 		ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
   3133  1.135        ad 		if (SESS_LEADER(p))
   3134  1.135        ad 			ep->e_flag |= EPROC_SLEADER;
   3135  1.135        ad 		strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
   3136  1.135        ad 	}
   3137    1.1    atatat 	ep->e_xsize = ep->e_xrssize = 0;
   3138    1.1    atatat 	ep->e_xccount = ep->e_xswrss = 0;
   3139    1.1    atatat }
   3140   1.95        ad 
   3141   1.95        ad u_int
   3142   1.95        ad sysctl_map_flags(const u_int *map, u_int word)
   3143   1.95        ad {
   3144   1.95        ad 	u_int rv;
   3145   1.95        ad 
   3146   1.95        ad 	for (rv = 0; *map != 0; map += 2)
   3147   1.95        ad 		if ((word & map[0]) != 0)
   3148   1.95        ad 			rv |= map[1];
   3149   1.95        ad 
   3150   1.95        ad 	return rv;
   3151   1.95        ad }
   3152