The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/init_sysctl.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: init_sysctl.c,v 1.36.2.8 2009/03/20 15:08:55 msaitoh Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Andrew Brown.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 3. All advertising materials mentioning features or use of this software
   19  *    must display the following acknowledgement:
   20  *      This product includes software developed by the NetBSD
   21  *      Foundation, Inc. and its contributors.
   22  * 4. Neither the name of The NetBSD Foundation nor the names of its
   23  *    contributors may be used to endorse or promote products derived
   24  *    from this software without specific prior written permission.
   25  *
   26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   36  * POSSIBILITY OF SUCH DAMAGE.
   37  */
   38 
   39 #include <sys/cdefs.h>
   40 __KERNEL_RCSID(0, "$NetBSD: init_sysctl.c,v 1.36.2.8 2009/03/20 15:08:55 msaitoh Exp $");
   41 
   42 #include "opt_sysv.h"
   43 #include "opt_multiprocessor.h"
   44 #include "opt_posix.h"
   45 #include "opt_verified_exec.h"
   46 #include "pty.h"
   47 #include "rnd.h"
   48 
   49 #include <sys/types.h>
   50 #include <sys/param.h>
   51 #include <sys/sysctl.h>
   52 #include <sys/errno.h>
   53 #include <sys/systm.h>
   54 #include <sys/kernel.h>
   55 #include <sys/unistd.h>
   56 #include <sys/disklabel.h>
   57 #include <sys/rnd.h>
   58 #include <sys/vnode.h>
   59 #include <sys/mount.h>
   60 #include <sys/namei.h>
   61 #include <sys/msgbuf.h>
   62 #include <dev/cons.h>
   63 #include <sys/socketvar.h>
   64 #include <sys/file.h>
   65 #include <sys/filedesc.h>
   66 #include <sys/tty.h>
   67 #include <sys/malloc.h>
   68 #include <sys/resource.h>
   69 #include <sys/resourcevar.h>
   70 #include <sys/exec.h>
   71 #include <sys/conf.h>
   72 #include <sys/device.h>
   73 #define VERIEXEC_NEED_NODE
   74 #include <sys/verified_exec.h>
   75 
   76 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
   77 #include <sys/ipc.h>
   78 #endif
   79 #ifdef SYSVMSG
   80 #include <sys/msg.h>
   81 #endif
   82 #ifdef SYSVSEM
   83 #include <sys/sem.h>
   84 #endif
   85 #ifdef SYSVSHM
   86 #include <sys/shm.h>
   87 #endif
   88 
   89 #include <machine/cpu.h>
   90 
   91 /*
   92  * try over estimating by 5 procs/lwps
   93  */
   94 #define KERN_PROCSLOP   (5 * sizeof(struct kinfo_proc))
   95 #define KERN_LWPSLOP    (5 * sizeof(struct kinfo_lwp))
   96 
   97 #ifndef MULTIPROCESSOR
   98 #define sysctl_ncpus()  (1)
   99 #else /* MULTIPROCESSOR */
  100 #ifndef CPU_INFO_FOREACH
  101 #define CPU_INFO_ITERATOR int
  102 #define CPU_INFO_FOREACH(cii, ci) cii = 0, ci = curcpu(); ci != NULL; ci = NULL
  103 #endif
  104 static int
  105 sysctl_ncpus(void)
  106 {
  107         struct cpu_info *ci;
  108         CPU_INFO_ITERATOR cii;
  109 
  110         int ncpus = 0;
  111         for (CPU_INFO_FOREACH(cii, ci))
  112                 ncpus++;
  113         return (ncpus);
  114 }
  115 #endif /* MULTIPROCESSOR */
  116 
  117 static int sysctl_kern_maxvnodes(SYSCTLFN_PROTO);
  118 static int sysctl_kern_rtc_offset(SYSCTLFN_PROTO);
  119 static int sysctl_kern_maxproc(SYSCTLFN_PROTO);
  120 static int sysctl_kern_securelevel(SYSCTLFN_PROTO);
  121 static int sysctl_kern_hostid(SYSCTLFN_PROTO);
  122 static int sysctl_setlen(SYSCTLFN_PROTO);
  123 static int sysctl_kern_clockrate(SYSCTLFN_PROTO);
  124 static int sysctl_kern_file(SYSCTLFN_PROTO);
  125 static int sysctl_kern_autonice(SYSCTLFN_PROTO);
  126 static int sysctl_msgbuf(SYSCTLFN_PROTO);
  127 static int sysctl_kern_defcorename(SYSCTLFN_PROTO);
  128 static int sysctl_kern_cptime(SYSCTLFN_PROTO);
  129 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
  130 static int sysctl_kern_sysvipc(SYSCTLFN_PROTO);
  131 #endif /* defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM) */
  132 #if NPTY > 0
  133 static int sysctl_kern_maxptys(SYSCTLFN_PROTO);
  134 #endif /* NPTY > 0 */
  135 static int sysctl_kern_sbmax(SYSCTLFN_PROTO);
  136 static int sysctl_kern_urnd(SYSCTLFN_PROTO);
  137 static int sysctl_kern_lwp(SYSCTLFN_PROTO);
  138 static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
  139 static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
  140 static int sysctl_kern_drivers(SYSCTLFN_PROTO);
  141 static int sysctl_kern_file2(SYSCTLFN_PROTO);
  142 #ifdef VERIFIED_EXEC
  143 static int sysctl_kern_veriexec(SYSCTLFN_PROTO);
  144 #endif
  145 static int sysctl_doeproc(SYSCTLFN_PROTO);
  146 static int sysctl_kern_proc_args(SYSCTLFN_PROTO);
  147 static int sysctl_hw_usermem(SYSCTLFN_PROTO);
  148 static int sysctl_hw_cnmagic(SYSCTLFN_PROTO);
  149 static int sysctl_hw_ncpu(SYSCTLFN_PROTO);
  150 
  151 static void fill_kproc2(struct proc *, struct kinfo_proc2 *);
  152 static void fill_lwp(struct lwp *l, struct kinfo_lwp *kl);
  153 static void fill_file(struct kinfo_file *, const struct file *, struct proc *,
  154                       int);
  155 
  156 /*
  157  * ********************************************************************
  158  * section 1: setup routines
  159  * ********************************************************************
  160  * these functions are stuffed into a link set for sysctl setup
  161  * functions.  they're never called or referenced from anywhere else.
  162  * ********************************************************************
  163  */
  164 
  165 /*
  166  * sets up the base nodes...
  167  */
  168 SYSCTL_SETUP(sysctl_root_setup, "sysctl base setup")
  169 {
  170 
  171         sysctl_createv(clog, 0, NULL, NULL,
  172                        CTLFLAG_PERMANENT,
  173                        CTLTYPE_NODE, "kern",
  174                        SYSCTL_DESCR("High kernel"),
  175                        NULL, 0, NULL, 0,
  176                        CTL_KERN, CTL_EOL);
  177         sysctl_createv(clog, 0, NULL, NULL,
  178                        CTLFLAG_PERMANENT,
  179                        CTLTYPE_NODE, "vm",
  180                        SYSCTL_DESCR("Virtual memory"),
  181                        NULL, 0, NULL, 0,
  182                        CTL_VM, CTL_EOL);
  183         sysctl_createv(clog, 0, NULL, NULL,
  184                        CTLFLAG_PERMANENT,
  185                        CTLTYPE_NODE, "vfs",
  186                        SYSCTL_DESCR("Filesystem"),
  187                        NULL, 0, NULL, 0,
  188                        CTL_VFS, CTL_EOL);
  189         sysctl_createv(clog, 0, NULL, NULL,
  190                        CTLFLAG_PERMANENT,
  191                        CTLTYPE_NODE, "net",
  192                        SYSCTL_DESCR("Networking"),
  193                        NULL, 0, NULL, 0,
  194                        CTL_NET, CTL_EOL);
  195         sysctl_createv(clog, 0, NULL, NULL,
  196                        CTLFLAG_PERMANENT,
  197                        CTLTYPE_NODE, "debug",
  198                        SYSCTL_DESCR("Debugging"),
  199                        NULL, 0, NULL, 0,
  200                        CTL_DEBUG, CTL_EOL);
  201         sysctl_createv(clog, 0, NULL, NULL,
  202                        CTLFLAG_PERMANENT,
  203                        CTLTYPE_NODE, "hw",
  204                        SYSCTL_DESCR("Generic CPU, I/O"),
  205                        NULL, 0, NULL, 0,
  206                        CTL_HW, CTL_EOL);
  207         sysctl_createv(clog, 0, NULL, NULL,
  208                        CTLFLAG_PERMANENT,
  209                        CTLTYPE_NODE, "machdep",
  210                        SYSCTL_DESCR("Machine dependent"),
  211                        NULL, 0, NULL, 0,
  212                        CTL_MACHDEP, CTL_EOL);
  213         /*
  214          * this node is inserted so that the sysctl nodes in libc can
  215          * operate.
  216          */
  217         sysctl_createv(clog, 0, NULL, NULL,
  218                        CTLFLAG_PERMANENT,
  219                        CTLTYPE_NODE, "user",
  220                        SYSCTL_DESCR("User-level"),
  221                        NULL, 0, NULL, 0,
  222                        CTL_USER, CTL_EOL);
  223         sysctl_createv(clog, 0, NULL, NULL,
  224                        CTLFLAG_PERMANENT,
  225                        CTLTYPE_NODE, "ddb",
  226                        SYSCTL_DESCR("In-kernel debugger"),
  227                        NULL, 0, NULL, 0,
  228                        CTL_DDB, CTL_EOL);
  229         sysctl_createv(clog, 0, NULL, NULL,
  230                        CTLFLAG_PERMANENT,
  231                        CTLTYPE_NODE, "proc",
  232                        SYSCTL_DESCR("Per-process"),
  233                        NULL, 0, NULL, 0,
  234                        CTL_PROC, CTL_EOL);
  235         sysctl_createv(clog, 0, NULL, NULL,
  236                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  237                        CTLTYPE_NODE, "vendor",
  238                        SYSCTL_DESCR("Vendor specific"),
  239                        NULL, 0, NULL, 0,
  240                        CTL_VENDOR, CTL_EOL);
  241         sysctl_createv(clog, 0, NULL, NULL,
  242                        CTLFLAG_PERMANENT,
  243                        CTLTYPE_NODE, "emul",
  244                        SYSCTL_DESCR("Emulation settings"),
  245                        NULL, 0, NULL, 0,
  246                        CTL_EMUL, CTL_EOL);
  247 }
  248 
  249 /*
  250  * this setup routine is a replacement for kern_sysctl()
  251  */
  252 SYSCTL_SETUP(sysctl_kern_setup, "sysctl kern subtree setup")
  253 {
  254         extern int kern_logsigexit;     /* defined in kern/kern_sig.c */
  255         extern fixpt_t ccpu;            /* defined in kern/kern_synch.c */
  256         extern int dumponpanic;         /* defined in kern/subr_prf.c */
  257 
  258         sysctl_createv(clog, 0, NULL, NULL,
  259                        CTLFLAG_PERMANENT,
  260                        CTLTYPE_NODE, "kern", NULL,
  261                        NULL, 0, NULL, 0,
  262                        CTL_KERN, CTL_EOL);
  263 
  264         sysctl_createv(clog, 0, NULL, NULL,
  265                        CTLFLAG_PERMANENT,
  266                        CTLTYPE_STRING, "ostype",
  267                        SYSCTL_DESCR("Operating system type"),
  268                        NULL, 0, &ostype, 0,
  269                        CTL_KERN, KERN_OSTYPE, CTL_EOL);
  270         sysctl_createv(clog, 0, NULL, NULL,
  271                        CTLFLAG_PERMANENT,
  272                        CTLTYPE_STRING, "osrelease",
  273                        SYSCTL_DESCR("Operating system release"),
  274                        NULL, 0, &osrelease, 0,
  275                        CTL_KERN, KERN_OSRELEASE, CTL_EOL);
  276         sysctl_createv(clog, 0, NULL, NULL,
  277                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  278                        CTLTYPE_INT, "osrevision",
  279                        SYSCTL_DESCR("Operating system revision"),
  280                        NULL, __NetBSD_Version__, NULL, 0,
  281                        CTL_KERN, KERN_OSREV, CTL_EOL);
  282         sysctl_createv(clog, 0, NULL, NULL,
  283                        CTLFLAG_PERMANENT,
  284                        CTLTYPE_STRING, "version",
  285                        SYSCTL_DESCR("Kernel version"),
  286                        NULL, 0, &version, 0,
  287                        CTL_KERN, KERN_VERSION, CTL_EOL);
  288         sysctl_createv(clog, 0, NULL, NULL,
  289                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  290                        CTLTYPE_INT, "maxvnodes",
  291                        SYSCTL_DESCR("Maximum number of vnodes"),
  292                        sysctl_kern_maxvnodes, 0, NULL, 0,
  293                        CTL_KERN, KERN_MAXVNODES, CTL_EOL);
  294         sysctl_createv(clog, 0, NULL, NULL,
  295                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  296                        CTLTYPE_INT, "maxproc",
  297                        SYSCTL_DESCR("Maximum number of simultaneous processes"),
  298                        sysctl_kern_maxproc, 0, NULL, 0,
  299                        CTL_KERN, KERN_MAXPROC, CTL_EOL);
  300         sysctl_createv(clog, 0, NULL, NULL,
  301                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  302                        CTLTYPE_INT, "maxfiles",
  303                        SYSCTL_DESCR("Maximum number of open files"),
  304                        NULL, 0, &maxfiles, 0,
  305                        CTL_KERN, KERN_MAXFILES, CTL_EOL);
  306         sysctl_createv(clog, 0, NULL, NULL,
  307                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  308                        CTLTYPE_INT, "argmax",
  309                        SYSCTL_DESCR("Maximum number of bytes of arguments to "
  310                                     "execve(2)"),
  311                        NULL, ARG_MAX, NULL, 0,
  312                        CTL_KERN, KERN_ARGMAX, CTL_EOL);
  313         sysctl_createv(clog, 0, NULL, NULL,
  314                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  315                        CTLTYPE_INT, "securelevel",
  316                        SYSCTL_DESCR("System security level"),
  317                        sysctl_kern_securelevel, 0, &securelevel, 0,
  318                        CTL_KERN, KERN_SECURELVL, CTL_EOL);
  319         sysctl_createv(clog, 0, NULL, NULL,
  320                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  321                        CTLTYPE_STRING, "hostname",
  322                        SYSCTL_DESCR("System hostname"),
  323                        sysctl_setlen, 0, &hostname, MAXHOSTNAMELEN,
  324                        CTL_KERN, KERN_HOSTNAME, CTL_EOL);
  325         sysctl_createv(clog, 0, NULL, NULL,
  326                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
  327                        CTLTYPE_INT, "hostid",
  328                        SYSCTL_DESCR("System host ID number"),
  329                        sysctl_kern_hostid, 0, NULL, 0,
  330                        CTL_KERN, KERN_HOSTID, CTL_EOL);
  331         sysctl_createv(clog, 0, NULL, NULL,
  332                        CTLFLAG_PERMANENT,
  333                        CTLTYPE_STRUCT, "clockrate",
  334                        SYSCTL_DESCR("Kernel clock rates"),
  335                        sysctl_kern_clockrate, 0, NULL,
  336                        sizeof(struct clockinfo),
  337                        CTL_KERN, KERN_CLOCKRATE, CTL_EOL);
  338         sysctl_createv(clog, 0, NULL, NULL,
  339                        CTLFLAG_PERMANENT,
  340                        CTLTYPE_STRUCT, "vnode",
  341                        SYSCTL_DESCR("System vnode table"),
  342                        sysctl_kern_vnode, 0, NULL, 0,
  343                        CTL_KERN, KERN_VNODE, CTL_EOL);
  344         sysctl_createv(clog, 0, NULL, NULL,
  345                        CTLFLAG_PERMANENT,
  346                        CTLTYPE_STRUCT, "file",
  347                        SYSCTL_DESCR("System open file table"),
  348                        sysctl_kern_file, 0, NULL, 0,
  349                        CTL_KERN, KERN_FILE, CTL_EOL);
  350 #ifndef GPROF
  351         sysctl_createv(clog, 0, NULL, NULL,
  352                        CTLFLAG_PERMANENT,
  353                        CTLTYPE_NODE, "profiling",
  354                        SYSCTL_DESCR("Profiling information (not available)"),
  355                        sysctl_notavail, 0, NULL, 0,
  356                        CTL_KERN, KERN_PROF, CTL_EOL);
  357 #endif
  358         sysctl_createv(clog, 0, NULL, NULL,
  359                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  360                        CTLTYPE_INT, "posix1version",
  361                        SYSCTL_DESCR("Version of ISO/IEC 9945 (POSIX 1003.1) "
  362                                     "with which the operating system attempts "
  363                                     "to comply"),
  364                        NULL, _POSIX_VERSION, NULL, 0,
  365                        CTL_KERN, KERN_POSIX1, CTL_EOL);
  366         sysctl_createv(clog, 0, NULL, NULL,
  367                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  368                        CTLTYPE_INT, "ngroups",
  369                        SYSCTL_DESCR("Maximum number of supplemental groups"),
  370                        NULL, NGROUPS_MAX, NULL, 0,
  371                        CTL_KERN, KERN_NGROUPS, CTL_EOL);
  372         sysctl_createv(clog, 0, NULL, NULL,
  373                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  374                        CTLTYPE_INT, "job_control",
  375                        SYSCTL_DESCR("Whether job control is available"),
  376                        NULL, 1, NULL, 0,
  377                        CTL_KERN, KERN_JOB_CONTROL, CTL_EOL);
  378         sysctl_createv(clog, 0, NULL, NULL,
  379                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  380                        CTLTYPE_INT, "saved_ids",
  381                        SYSCTL_DESCR("Whether saved set-group/user ID is "
  382                                     "available"), NULL,
  383 #ifdef _POSIX_SAVED_IDS
  384                        1,
  385 #else /* _POSIX_SAVED_IDS */
  386                        0,
  387 #endif /* _POSIX_SAVED_IDS */
  388                        NULL, 0, CTL_KERN, KERN_SAVED_IDS, CTL_EOL);
  389         sysctl_createv(clog, 0, NULL, NULL,
  390                        CTLFLAG_PERMANENT,
  391                        CTLTYPE_STRUCT, "boottime",
  392                        SYSCTL_DESCR("System boot time"),
  393                        NULL, 0, &boottime, sizeof(boottime),
  394                        CTL_KERN, KERN_BOOTTIME, CTL_EOL);
  395         sysctl_createv(clog, 0, NULL, NULL,
  396                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  397                        CTLTYPE_STRING, "domainname",
  398                        SYSCTL_DESCR("YP domain name"),
  399                        sysctl_setlen, 0, &domainname, MAXHOSTNAMELEN,
  400                        CTL_KERN, KERN_DOMAINNAME, CTL_EOL);
  401         sysctl_createv(clog, 0, NULL, NULL,
  402                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  403                        CTLTYPE_INT, "maxpartitions",
  404                        SYSCTL_DESCR("Maximum number of partitions allowed per "
  405                                     "disk"),
  406                        NULL, MAXPARTITIONS, NULL, 0,
  407                        CTL_KERN, KERN_MAXPARTITIONS, CTL_EOL);
  408         sysctl_createv(clog, 0, NULL, NULL,
  409                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  410                        CTLTYPE_INT, "rawpartition",
  411                        SYSCTL_DESCR("Raw partition of a disk"),
  412                        NULL, RAW_PART, NULL, 0,
  413                        CTL_KERN, KERN_RAWPARTITION, CTL_EOL);
  414         sysctl_createv(clog, 0, NULL, NULL,
  415                        CTLFLAG_PERMANENT,
  416                        CTLTYPE_STRUCT, "timex", NULL,
  417                        sysctl_notavail, 0, NULL, 0,
  418                        CTL_KERN, KERN_TIMEX, CTL_EOL);
  419         sysctl_createv(clog, 0, NULL, NULL,
  420                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  421                        CTLTYPE_INT, "autonicetime",
  422                        SYSCTL_DESCR("CPU clock seconds before non-root "
  423                                     "process priority is lowered"),
  424                        sysctl_kern_autonice, 0, &autonicetime, 0,
  425                        CTL_KERN, KERN_AUTONICETIME, CTL_EOL);
  426         sysctl_createv(clog, 0, NULL, NULL,
  427                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  428                        CTLTYPE_INT, "autoniceval",
  429                        SYSCTL_DESCR("Automatic reniced non-root process "
  430                                     "priority"),
  431                        sysctl_kern_autonice, 0, &autoniceval, 0,
  432                        CTL_KERN, KERN_AUTONICEVAL, CTL_EOL);
  433         sysctl_createv(clog, 0, NULL, NULL,
  434                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  435                        CTLTYPE_INT, "rtc_offset",
  436                        SYSCTL_DESCR("Offset of real time clock from UTC in "
  437                                     "minutes"),
  438                        sysctl_kern_rtc_offset, 0, &rtc_offset, 0,
  439                        CTL_KERN, KERN_RTC_OFFSET, CTL_EOL);
  440         sysctl_createv(clog, 0, NULL, NULL,
  441                        CTLFLAG_PERMANENT,
  442                        CTLTYPE_STRING, "root_device",
  443                        SYSCTL_DESCR("Name of the root device"),
  444                        sysctl_root_device, 0, NULL, 0,
  445                        CTL_KERN, KERN_ROOT_DEVICE, CTL_EOL);
  446         sysctl_createv(clog, 0, NULL, NULL,
  447                        CTLFLAG_PERMANENT,
  448                        CTLTYPE_INT, "msgbufsize",
  449                        SYSCTL_DESCR("Size of the kernel message buffer"),
  450                        sysctl_msgbuf, 0, NULL, 0,
  451                        CTL_KERN, KERN_MSGBUFSIZE, CTL_EOL);
  452         sysctl_createv(clog, 0, NULL, NULL,
  453                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  454                        CTLTYPE_INT, "fsync",
  455                        SYSCTL_DESCR("Whether the POSIX 1003.1b File "
  456                                     "Synchronization Option is available on "
  457                                     "this system"),
  458                        NULL, 1, NULL, 0,
  459                        CTL_KERN, KERN_FSYNC, CTL_EOL);
  460         sysctl_createv(clog, 0, NULL, NULL,
  461                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  462                        CTLTYPE_INT, "sysvmsg",
  463                        SYSCTL_DESCR("System V style message support available"),
  464                        NULL,
  465 #ifdef SYSVMSG
  466                        1,
  467 #else /* SYSVMSG */
  468                        0,
  469 #endif /* SYSVMSG */
  470                        NULL, 0, CTL_KERN, KERN_SYSVMSG, CTL_EOL);
  471         sysctl_createv(clog, 0, NULL, NULL,
  472                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  473                        CTLTYPE_INT, "sysvsem",
  474                        SYSCTL_DESCR("System V style semaphore support "
  475                                     "available"), NULL,
  476 #ifdef SYSVSEM
  477                        1,
  478 #else /* SYSVSEM */
  479                        0,
  480 #endif /* SYSVSEM */
  481                        NULL, 0, CTL_KERN, KERN_SYSVSEM, CTL_EOL);
  482         sysctl_createv(clog, 0, NULL, NULL,
  483                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  484                        CTLTYPE_INT, "sysvshm",
  485                        SYSCTL_DESCR("System V style shared memory support "
  486                                     "available"), NULL,
  487 #ifdef SYSVSHM
  488                        1,
  489 #else /* SYSVSHM */
  490                        0,
  491 #endif /* SYSVSHM */
  492                        NULL, 0, CTL_KERN, KERN_SYSVSHM, CTL_EOL);
  493         sysctl_createv(clog, 0, NULL, NULL,
  494                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  495                        CTLTYPE_INT, "synchronized_io",
  496                        SYSCTL_DESCR("Whether the POSIX 1003.1b Synchronized "
  497                                     "I/O Option is available on this system"),
  498                        NULL, 1, NULL, 0,
  499                        CTL_KERN, KERN_SYNCHRONIZED_IO, CTL_EOL);
  500         sysctl_createv(clog, 0, NULL, NULL,
  501                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  502                        CTLTYPE_INT, "iov_max",
  503                        SYSCTL_DESCR("Maximum number of iovec structures per "
  504                                     "process"),
  505                        NULL, IOV_MAX, NULL, 0,
  506                        CTL_KERN, KERN_IOV_MAX, CTL_EOL);
  507         sysctl_createv(clog, 0, NULL, NULL,
  508                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  509                        CTLTYPE_INT, "mapped_files",
  510                        SYSCTL_DESCR("Whether the POSIX 1003.1b Memory Mapped "
  511                                     "Files Option is available on this system"),
  512                        NULL, 1, NULL, 0,
  513                        CTL_KERN, KERN_MAPPED_FILES, CTL_EOL);
  514         sysctl_createv(clog, 0, NULL, NULL,
  515                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  516                        CTLTYPE_INT, "memlock",
  517                        SYSCTL_DESCR("Whether the POSIX 1003.1b Process Memory "
  518                                     "Locking Option is available on this "
  519                                     "system"),
  520                        NULL, 1, NULL, 0,
  521                        CTL_KERN, KERN_MEMLOCK, CTL_EOL);
  522         sysctl_createv(clog, 0, NULL, NULL,
  523                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  524                        CTLTYPE_INT, "memlock_range",
  525                        SYSCTL_DESCR("Whether the POSIX 1003.1b Range Memory "
  526                                     "Locking Option is available on this "
  527                                     "system"),
  528                        NULL, 1, NULL, 0,
  529                        CTL_KERN, KERN_MEMLOCK_RANGE, CTL_EOL);
  530         sysctl_createv(clog, 0, NULL, NULL,
  531                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  532                        CTLTYPE_INT, "memory_protection",
  533                        SYSCTL_DESCR("Whether the POSIX 1003.1b Memory "
  534                                     "Protection Option is available on this "
  535                                     "system"),
  536                        NULL, 1, NULL, 0,
  537                        CTL_KERN, KERN_MEMORY_PROTECTION, CTL_EOL);
  538         sysctl_createv(clog, 0, NULL, NULL,
  539                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  540                        CTLTYPE_INT, "login_name_max",
  541                        SYSCTL_DESCR("Maximum login name length"),
  542                        NULL, LOGIN_NAME_MAX, NULL, 0,
  543                        CTL_KERN, KERN_LOGIN_NAME_MAX, CTL_EOL);
  544         sysctl_createv(clog, 0, NULL, NULL,
  545                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  546                        CTLTYPE_STRING, "defcorename",
  547                        SYSCTL_DESCR("Default core file name"),
  548                        sysctl_kern_defcorename, 0, defcorename, MAXPATHLEN,
  549                        CTL_KERN, KERN_DEFCORENAME, CTL_EOL);
  550         sysctl_createv(clog, 0, NULL, NULL,
  551                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  552                        CTLTYPE_INT, "logsigexit",
  553                        SYSCTL_DESCR("Log process exit when caused by signals"),
  554                        NULL, 0, &kern_logsigexit, 0,
  555                        CTL_KERN, KERN_LOGSIGEXIT, CTL_EOL);
  556         sysctl_createv(clog, 0, NULL, NULL,
  557                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  558                        CTLTYPE_INT, "fscale",
  559                        SYSCTL_DESCR("Kernel fixed-point scale factor"),
  560                        NULL, FSCALE, NULL, 0,
  561                        CTL_KERN, KERN_FSCALE, CTL_EOL);
  562         sysctl_createv(clog, 0, NULL, NULL,
  563                        CTLFLAG_PERMANENT,
  564                        CTLTYPE_INT, "ccpu",
  565                        SYSCTL_DESCR("Scheduler exponential decay value"),
  566                        NULL, 0, &ccpu, 0,
  567                        CTL_KERN, KERN_CCPU, CTL_EOL);
  568         sysctl_createv(clog, 0, NULL, NULL,
  569                        CTLFLAG_PERMANENT,
  570                        CTLTYPE_STRUCT, "cp_time",
  571                        SYSCTL_DESCR("Clock ticks spent in different CPU states"),
  572                        sysctl_kern_cptime, 0, NULL, 0,
  573                        CTL_KERN, KERN_CP_TIME, CTL_EOL);
  574 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
  575         sysctl_createv(clog, 0, NULL, NULL,
  576                        CTLFLAG_PERMANENT,
  577                        CTLTYPE_STRUCT, "sysvipc_info",
  578                        SYSCTL_DESCR("System V style IPC information"),
  579                        sysctl_kern_sysvipc, 0, NULL, 0,
  580                        CTL_KERN, KERN_SYSVIPC_INFO, CTL_EOL);
  581 #endif /* SYSVMSG || SYSVSEM || SYSVSHM */
  582         sysctl_createv(clog, 0, NULL, NULL,
  583                        CTLFLAG_PERMANENT,
  584                        CTLTYPE_INT, "msgbuf",
  585                        SYSCTL_DESCR("Kernel message buffer"),
  586                        sysctl_msgbuf, 0, NULL, 0,
  587                        CTL_KERN, KERN_MSGBUF, CTL_EOL);
  588         sysctl_createv(clog, 0, NULL, NULL,
  589                        CTLFLAG_PERMANENT,
  590                        CTLTYPE_STRUCT, "consdev",
  591                        SYSCTL_DESCR("Console device"),
  592                        sysctl_consdev, 0, NULL, sizeof(dev_t),
  593                        CTL_KERN, KERN_CONSDEV, CTL_EOL);
  594 #if NPTY > 0
  595         sysctl_createv(clog, 0, NULL, NULL,
  596                        CTLFLAG_PERMANENT,
  597                        CTLTYPE_INT, "maxptys",
  598                        SYSCTL_DESCR("Maximum number of pseudo-ttys"),
  599                        sysctl_kern_maxptys, 0, NULL, 0,
  600                        CTL_KERN, KERN_MAXPTYS, CTL_EOL);
  601 #endif /* NPTY > 0 */
  602         sysctl_createv(clog, 0, NULL, NULL,
  603                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  604                        CTLTYPE_INT, "maxphys",
  605                        SYSCTL_DESCR("Maximum raw I/O transfer size"),
  606                        NULL, MAXPHYS, NULL, 0,
  607                        CTL_KERN, KERN_MAXPHYS, CTL_EOL);
  608         sysctl_createv(clog, 0, NULL, NULL,
  609                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  610                        CTLTYPE_INT, "sbmax",
  611                        SYSCTL_DESCR("Maximum socket buffer size"),
  612                        sysctl_kern_sbmax, 0, NULL, 0,
  613                        CTL_KERN, KERN_SBMAX, CTL_EOL);
  614         sysctl_createv(clog, 0, NULL, NULL,
  615                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  616                        CTLTYPE_INT, "monotonic_clock",
  617                        SYSCTL_DESCR("Implementation version of the POSIX "
  618                                     "1003.1b Monotonic Clock Option"),
  619                        /* XXX _POSIX_VERSION */
  620                        NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0,
  621                        CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL);
  622         sysctl_createv(clog, 0, NULL, NULL,
  623                        CTLFLAG_PERMANENT,
  624                        CTLTYPE_INT, "urandom",
  625                        SYSCTL_DESCR("Random integer value"),
  626                        sysctl_kern_urnd, 0, NULL, 0,
  627                        CTL_KERN, KERN_URND, CTL_EOL);
  628         sysctl_createv(clog, 0, NULL, NULL,
  629                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  630                        CTLTYPE_INT, "labelsector",
  631                        SYSCTL_DESCR("Sector number containing the disklabel"),
  632                        NULL, LABELSECTOR, NULL, 0,
  633                        CTL_KERN, KERN_LABELSECTOR, CTL_EOL);
  634         sysctl_createv(clog, 0, NULL, NULL,
  635                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  636                        CTLTYPE_INT, "labeloffset",
  637                        SYSCTL_DESCR("Offset of the disklabel within the "
  638                                     "sector"),
  639                        NULL, LABELOFFSET, NULL, 0,
  640                        CTL_KERN, KERN_LABELOFFSET, CTL_EOL);
  641         sysctl_createv(clog, 0, NULL, NULL,
  642                        CTLFLAG_PERMANENT,
  643                        CTLTYPE_NODE, "lwp",
  644                        SYSCTL_DESCR("System-wide LWP information"),
  645                        sysctl_kern_lwp, 0, NULL, 0,
  646                        CTL_KERN, KERN_LWP, CTL_EOL);
  647         sysctl_createv(clog, 0, NULL, NULL,
  648                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  649                        CTLTYPE_INT, "forkfsleep",
  650                        SYSCTL_DESCR("Milliseconds to sleep on fork failure due "
  651                                     "to process limits"),
  652                        sysctl_kern_forkfsleep, 0, NULL, 0,
  653                        CTL_KERN, KERN_FORKFSLEEP, CTL_EOL);
  654         sysctl_createv(clog, 0, NULL, NULL,
  655                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  656                        CTLTYPE_INT, "posix_threads",
  657                        SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
  658                                     "Threads option to which the system "
  659                                     "attempts to conform"),
  660                        /* XXX _POSIX_VERSION */
  661                        NULL, _POSIX_THREADS, NULL, 0,
  662                        CTL_KERN, KERN_POSIX_THREADS, CTL_EOL);
  663         sysctl_createv(clog, 0, NULL, NULL,
  664                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  665                        CTLTYPE_INT, "posix_semaphores",
  666                        SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
  667                                     "Semaphores option to which the system "
  668                                     "attempts to conform"), NULL,
  669 #ifdef P1003_1B_SEMAPHORE
  670                        200112,
  671 #else /* P1003_1B_SEMAPHORE */
  672                        0,
  673 #endif /* P1003_1B_SEMAPHORE */
  674                        NULL, 0, CTL_KERN, KERN_POSIX_SEMAPHORES, CTL_EOL);
  675         sysctl_createv(clog, 0, NULL, NULL,
  676                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  677                        CTLTYPE_INT, "posix_barriers",
  678                        SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
  679                                     "Barriers option to which the system "
  680                                     "attempts to conform"),
  681                        /* XXX _POSIX_VERSION */
  682                        NULL, _POSIX_BARRIERS, NULL, 0,
  683                        CTL_KERN, KERN_POSIX_BARRIERS, CTL_EOL);
  684         sysctl_createv(clog, 0, NULL, NULL,
  685                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  686                        CTLTYPE_INT, "posix_timers",
  687                        SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
  688                                     "Timers option to which the system "
  689                                     "attempts to conform"),
  690                        /* XXX _POSIX_VERSION */
  691                        NULL, _POSIX_TIMERS, NULL, 0,
  692                        CTL_KERN, KERN_POSIX_TIMERS, CTL_EOL);
  693         sysctl_createv(clog, 0, NULL, NULL,
  694                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  695                        CTLTYPE_INT, "posix_spin_locks",
  696                        SYSCTL_DESCR("Version of IEEE Std 1003.1 and its Spin "
  697                                     "Locks option to which the system attempts "
  698                                     "to conform"),
  699                        /* XXX _POSIX_VERSION */
  700                        NULL, _POSIX_SPIN_LOCKS, NULL, 0,
  701                        CTL_KERN, KERN_POSIX_SPIN_LOCKS, CTL_EOL);
  702         sysctl_createv(clog, 0, NULL, NULL,
  703                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  704                        CTLTYPE_INT, "posix_reader_writer_locks",
  705                        SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
  706                                     "Read-Write Locks option to which the "
  707                                     "system attempts to conform"),
  708                        /* XXX _POSIX_VERSION */
  709                        NULL, _POSIX_READER_WRITER_LOCKS, NULL, 0,
  710                        CTL_KERN, KERN_POSIX_READER_WRITER_LOCKS, CTL_EOL);
  711         sysctl_createv(clog, 0, NULL, NULL,
  712                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  713                        CTLTYPE_INT, "dump_on_panic",
  714                        SYSCTL_DESCR("Perform a crash dump on system panic"),
  715                        NULL, 0, &dumponpanic, 0,
  716                        CTL_KERN, KERN_DUMP_ON_PANIC, CTL_EOL);
  717         sysctl_createv(clog, 0, NULL, NULL,
  718                        CTLFLAG_PERMANENT,
  719                        CTLTYPE_INT, "root_partition",
  720                        SYSCTL_DESCR("Root partition on the root device"),
  721                        sysctl_kern_root_partition, 0, NULL, 0,
  722                        CTL_KERN, KERN_ROOT_PARTITION, CTL_EOL);
  723         sysctl_createv(clog, 0, NULL, NULL,
  724                        CTLFLAG_PERMANENT,
  725                        CTLTYPE_STRUCT, "drivers",
  726                        SYSCTL_DESCR("List of all drivers with block and "
  727                                     "character device numbers"),
  728                        sysctl_kern_drivers, 0, NULL, 0,
  729                        CTL_KERN, KERN_DRIVERS, CTL_EOL);
  730         sysctl_createv(clog, 0, NULL, NULL,
  731                        CTLFLAG_PERMANENT,
  732                        CTLTYPE_STRUCT, "file2",
  733                        SYSCTL_DESCR("System open file table"),
  734                        sysctl_kern_file2, 0, NULL, 0,
  735                        CTL_KERN, KERN_FILE2, CTL_EOL);
  736 #ifdef VERIFIED_EXEC
  737         sysctl_createv(clog, 0, NULL, NULL,
  738                        CTLFLAG_PERMANENT,
  739                        CTLTYPE_NODE, "veriexec",
  740                        SYSCTL_DESCR("Verified Exec"),
  741                        NULL, 0, NULL, 0,
  742                        CTL_KERN, KERN_VERIEXEC, CTL_EOL);
  743         sysctl_createv(clog, 0, NULL, NULL,
  744                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  745                        CTLTYPE_INT, "verbose",
  746                        SYSCTL_DESCR("Verified Exec verbose level"),
  747                        NULL, 0, &veriexec_verbose, 0,
  748                        CTL_KERN, KERN_VERIEXEC, VERIEXEC_VERBOSE,
  749                        CTL_EOL);
  750         sysctl_createv(clog, 0, NULL, NULL,
  751                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  752                        CTLTYPE_INT, "strict",
  753                        SYSCTL_DESCR("Verified Exec strict level"),
  754                        sysctl_kern_veriexec, 0, NULL, 0,
  755                        CTL_KERN, KERN_VERIEXEC, VERIEXEC_STRICT, CTL_EOL);
  756         sysctl_createv(clog, 0, NULL, NULL,
  757                        CTLFLAG_PERMANENT,
  758                        CTLTYPE_STRING, "algorithms",
  759                        SYSCTL_DESCR("Verified Exec supported hashing "
  760                                     "algorithms"),
  761                        sysctl_kern_veriexec, 0, NULL, 0,
  762                        CTL_KERN, KERN_VERIEXEC, VERIEXEC_ALGORITHMS, CTL_EOL);
  763         sysctl_createv(clog, 0, NULL, &veriexec_count_node,
  764                        CTLFLAG_PERMANENT,
  765                        CTLTYPE_NODE, "count",
  766                        SYSCTL_DESCR("Number of fingerprints on device(s)"),
  767                        NULL, 0, NULL, 0,
  768                        CTL_KERN, KERN_VERIEXEC, VERIEXEC_COUNT, CTL_EOL);
  769 #endif /* VERIFIED_EXEC */
  770 }
  771 
  772 SYSCTL_SETUP(sysctl_kern_proc_setup,
  773              "sysctl kern.proc/proc2/proc_args subtree setup")
  774 {
  775 
  776         sysctl_createv(clog, 0, NULL, NULL,
  777                        CTLFLAG_PERMANENT,
  778                        CTLTYPE_NODE, "kern", NULL,
  779                        NULL, 0, NULL, 0,
  780                        CTL_KERN, CTL_EOL);
  781 
  782         sysctl_createv(clog, 0, NULL, NULL,
  783                        CTLFLAG_PERMANENT,
  784                        CTLTYPE_NODE, "proc",
  785                        SYSCTL_DESCR("System-wide process information"),
  786                        sysctl_doeproc, 0, NULL, 0,
  787                        CTL_KERN, KERN_PROC, CTL_EOL);
  788         sysctl_createv(clog, 0, NULL, NULL,
  789                        CTLFLAG_PERMANENT,
  790                        CTLTYPE_NODE, "proc2",
  791                        SYSCTL_DESCR("Machine-independent process information"),
  792                        sysctl_doeproc, 0, NULL, 0,
  793                        CTL_KERN, KERN_PROC2, CTL_EOL);
  794         sysctl_createv(clog, 0, NULL, NULL,
  795                        CTLFLAG_PERMANENT,
  796                        CTLTYPE_NODE, "proc_args",
  797                        SYSCTL_DESCR("Process argument information"),
  798                        sysctl_kern_proc_args, 0, NULL, 0,
  799                        CTL_KERN, KERN_PROC_ARGS, CTL_EOL);
  800 
  801         /*
  802           "nodes" under these:
  803 
  804           KERN_PROC_ALL
  805           KERN_PROC_PID pid
  806           KERN_PROC_PGRP pgrp
  807           KERN_PROC_SESSION sess
  808           KERN_PROC_TTY tty
  809           KERN_PROC_UID uid
  810           KERN_PROC_RUID uid
  811           KERN_PROC_GID gid
  812           KERN_PROC_RGID gid
  813 
  814           all in all, probably not worth the effort...
  815         */
  816 }
  817 
  818 SYSCTL_SETUP(sysctl_hw_setup, "sysctl hw subtree setup")
  819 {
  820         u_int u;
  821         u_quad_t q;
  822 
  823         sysctl_createv(clog, 0, NULL, NULL,
  824                        CTLFLAG_PERMANENT,
  825                        CTLTYPE_NODE, "hw", NULL,
  826                        NULL, 0, NULL, 0,
  827                        CTL_HW, CTL_EOL);
  828 
  829         sysctl_createv(clog, 0, NULL, NULL,
  830                        CTLFLAG_PERMANENT,
  831                        CTLTYPE_STRING, "machine",
  832                        SYSCTL_DESCR("Machine class"),
  833                        NULL, 0, machine, 0,
  834                        CTL_HW, HW_MACHINE, CTL_EOL);
  835         sysctl_createv(clog, 0, NULL, NULL,
  836                        CTLFLAG_PERMANENT,
  837                        CTLTYPE_STRING, "model",
  838                        SYSCTL_DESCR("Machine model"),
  839                        NULL, 0, cpu_model, 0,
  840                        CTL_HW, HW_MODEL, CTL_EOL);
  841         sysctl_createv(clog, 0, NULL, NULL,
  842                        CTLFLAG_PERMANENT,
  843                        CTLTYPE_INT, "ncpu",
  844                        SYSCTL_DESCR("Number of active CPUs"),
  845                        sysctl_hw_ncpu, 0, NULL, 0,
  846                        CTL_HW, HW_NCPU, CTL_EOL);
  847         sysctl_createv(clog, 0, NULL, NULL,
  848                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  849                        CTLTYPE_INT, "byteorder",
  850                        SYSCTL_DESCR("System byte order"),
  851                        NULL, BYTE_ORDER, NULL, 0,
  852                        CTL_HW, HW_BYTEORDER, CTL_EOL);
  853         u = ((u_int)physmem > (UINT_MAX / PAGE_SIZE)) ?
  854                 UINT_MAX : physmem * PAGE_SIZE;
  855         sysctl_createv(clog, 0, NULL, NULL,
  856                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  857                        CTLTYPE_INT, "physmem",
  858                        SYSCTL_DESCR("Bytes of physical memory"),
  859                        NULL, u, NULL, 0,
  860                        CTL_HW, HW_PHYSMEM, CTL_EOL);
  861         sysctl_createv(clog, 0, NULL, NULL,
  862                        CTLFLAG_PERMANENT,
  863                        CTLTYPE_INT, "usermem",
  864                        SYSCTL_DESCR("Bytes of non-kernel memory"),
  865                        sysctl_hw_usermem, 0, NULL, 0,
  866                        CTL_HW, HW_USERMEM, CTL_EOL);
  867         sysctl_createv(clog, 0, NULL, NULL,
  868                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  869                        CTLTYPE_INT, "pagesize",
  870                        SYSCTL_DESCR("Software page size"),
  871                        NULL, PAGE_SIZE, NULL, 0,
  872                        CTL_HW, HW_PAGESIZE, CTL_EOL);
  873         sysctl_createv(clog, 0, NULL, NULL,
  874                        CTLFLAG_PERMANENT,
  875                        CTLTYPE_STRING, "disknames",
  876                        SYSCTL_DESCR("List of disk devices present"),
  877                        sysctl_hw_disknames, 0, NULL, 0,
  878                        CTL_HW, HW_DISKNAMES, CTL_EOL);
  879         sysctl_createv(clog, 0, NULL, NULL,
  880                        CTLFLAG_PERMANENT,
  881                        CTLTYPE_STRUCT, "diskstats",
  882                        SYSCTL_DESCR("Statistics on disk operation"),
  883                        sysctl_hw_diskstats, 0, NULL, 0,
  884                        CTL_HW, HW_DISKSTATS, CTL_EOL);
  885         sysctl_createv(clog, 0, NULL, NULL,
  886                        CTLFLAG_PERMANENT,
  887                        CTLTYPE_STRING, "machine_arch",
  888                        SYSCTL_DESCR("Machine CPU class"),
  889                        NULL, 0, machine_arch, 0,
  890                        CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
  891         sysctl_createv(clog, 0, NULL, NULL,
  892                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  893                        CTLTYPE_INT, "alignbytes",
  894                        SYSCTL_DESCR("Alignment constraint for all possible "
  895                                     "data types"),
  896                        NULL, ALIGNBYTES, NULL, 0,
  897                        CTL_HW, HW_ALIGNBYTES, CTL_EOL);
  898         sysctl_createv(clog, 0, NULL, NULL,
  899                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
  900                        CTLTYPE_STRING, "cnmagic",
  901                        SYSCTL_DESCR("Console magic key sequence"),
  902                        sysctl_hw_cnmagic, 0, NULL, CNS_LEN,
  903                        CTL_HW, HW_CNMAGIC, CTL_EOL);
  904         q = (u_quad_t)physmem * PAGE_SIZE;
  905         sysctl_createv(clog, 0, NULL, NULL,
  906                        CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
  907                        CTLTYPE_QUAD, "physmem64",
  908                        SYSCTL_DESCR("Bytes of physical memory"),
  909                        NULL, q, NULL, 0,
  910                        CTL_HW, HW_PHYSMEM64, CTL_EOL);
  911         sysctl_createv(clog, 0, NULL, NULL,
  912                        CTLFLAG_PERMANENT,
  913                        CTLTYPE_QUAD, "usermem64",
  914                        SYSCTL_DESCR("Bytes of non-kernel memory"),
  915                        sysctl_hw_usermem, 0, NULL, 0,
  916                        CTL_HW, HW_USERMEM64, CTL_EOL);
  917 }
  918 
  919 #ifdef DEBUG
  920 /*
  921  * Debugging related system variables.
  922  */
  923 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
  924 struct ctldebug debug5, debug6, debug7, debug8, debug9;
  925 struct ctldebug debug10, debug11, debug12, debug13, debug14;
  926 struct ctldebug debug15, debug16, debug17, debug18, debug19;
  927 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
  928         &debug0, &debug1, &debug2, &debug3, &debug4,
  929         &debug5, &debug6, &debug7, &debug8, &debug9,
  930         &debug10, &debug11, &debug12, &debug13, &debug14,
  931         &debug15, &debug16, &debug17, &debug18, &debug19,
  932 };
  933 
  934 /*
  935  * this setup routine is a replacement for debug_sysctl()
  936  *
  937  * note that it creates several nodes per defined debug variable
  938  */
  939 SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup")
  940 {
  941         struct ctldebug *cdp;
  942         char nodename[20];
  943         int i;
  944 
  945         /*
  946          * two ways here:
  947          *
  948          * the "old" way (debug.name -> value) which was emulated by
  949          * the sysctl(8) binary
  950          *
  951          * the new way, which the sysctl(8) binary was actually using
  952 
  953          node   debug
  954          node   debug.0
  955          string debug.0.name
  956          int    debug.0.value
  957          int    debug.name
  958 
  959          */
  960 
  961         sysctl_createv(clog, 0, NULL, NULL,
  962                        CTLFLAG_PERMANENT,
  963                        CTLTYPE_NODE, "debug", NULL,
  964                        NULL, 0, NULL, 0,
  965                        CTL_DEBUG, CTL_EOL);
  966 
  967         for (i = 0; i < CTL_DEBUG_MAXID; i++) {
  968                 cdp = debugvars[i];
  969                 if (cdp->debugname == NULL || cdp->debugvar == NULL)
  970                         continue;
  971 
  972                 snprintf(nodename, sizeof(nodename), "debug%d", i);
  973                 sysctl_createv(clog, 0, NULL, NULL,
  974                                CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
  975                                CTLTYPE_NODE, nodename, NULL,
  976                                NULL, 0, NULL, 0,
  977                                CTL_DEBUG, i, CTL_EOL);
  978                 sysctl_createv(clog, 0, NULL, NULL,
  979                                CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
  980                                CTLTYPE_STRING, "name", NULL,
  981                                NULL, 0, cdp->debugname, 0,
  982                                CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL);
  983                 sysctl_createv(clog, 0, NULL, NULL,
  984                                CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
  985                                CTLTYPE_INT, "value", NULL,
  986                                NULL, 0, cdp->debugvar, 0,
  987                                CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL);
  988                 sysctl_createv(clog, 0, NULL, NULL,
  989                                CTLFLAG_PERMANENT,
  990                                CTLTYPE_INT, cdp->debugname, NULL,
  991                                NULL, 0, cdp->debugvar, 0,
  992                                CTL_DEBUG, CTL_CREATE, CTL_EOL);
  993         }
  994 }
  995 #endif /* DEBUG */
  996 
  997 /*
  998  * ********************************************************************
  999  * section 2: private node-specific helper routines.
 1000  * ********************************************************************
 1001  */
 1002 
 1003 /*
 1004  * sysctl helper routine for kern.maxvnodes.  drain vnodes if
 1005  * new value is lower than desiredvnodes and then calls reinit
 1006  * routines that needs to adjust to the new value.
 1007  */
 1008 static int
 1009 sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
 1010 {
 1011         int error, new_vnodes, old_vnodes;
 1012         struct sysctlnode node;
 1013 
 1014         new_vnodes = desiredvnodes;
 1015         node = *rnode;
 1016         node.sysctl_data = &new_vnodes;
 1017         error = sysctl_lookup(SYSCTLFN_CALL(&node));
 1018         if (error || newp == NULL)
 1019                 return (error);
 1020 
 1021         old_vnodes = desiredvnodes;
 1022         desiredvnodes = new_vnodes;
 1023         if (new_vnodes < old_vnodes) {
 1024                 error = vfs_drainvnodes(new_vnodes, l->l_proc);
 1025                 if (error) {
 1026                         desiredvnodes = old_vnodes;
 1027                         return (error);
 1028                 }
 1029         }
 1030         vfs_reinit();
 1031         nchreinit();
 1032 
 1033         return (0);
 1034 }
 1035 
 1036 /*
 1037  * sysctl helper routine for rtc_offset - set time after changes
 1038  */
 1039 static int
 1040 sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
 1041 {
 1042         struct timeval tv, delta;
 1043         int s, error, new_rtc_offset;
 1044         struct sysctlnode node;
 1045 
 1046         new_rtc_offset = rtc_offset;
 1047         node = *rnode;
 1048         node.sysctl_data = &new_rtc_offset;
 1049         error = sysctl_lookup(SYSCTLFN_CALL(&node));
 1050         if (error || newp == NULL)
 1051                 return (error);
 1052 
 1053         if (securelevel > 0)
 1054                 return (EPERM);
 1055         if (rtc_offset == new_rtc_offset)
 1056                 return (0);
 1057 
 1058         /* if we change the offset, adjust the time */
 1059         s = splclock();
 1060         tv = time;
 1061         splx(s);
 1062         delta.tv_sec = 60*(new_rtc_offset - rtc_offset);
 1063         delta.tv_usec = 0;
 1064         timeradd(&tv, &delta, &tv);
 1065         rtc_offset = new_rtc_offset;
 1066         settime(&tv);
 1067 
 1068         return (0);
 1069 }
 1070 
 1071 /*
 1072  * sysctl helper routine for kern.maxvnodes.  ensures that the new
 1073  * values are not too low or too high.
 1074  */
 1075 static int
 1076 sysctl_kern_maxproc(SYSCTLFN_ARGS)
 1077 {
 1078         int error, nmaxproc;
 1079         struct sysctlnode node;
 1080 
 1081         nmaxproc = maxproc;
 1082         node = *rnode;
 1083         node.sysctl_data = &nmaxproc;
 1084         error = sysctl_lookup(SYSCTLFN_CALL(&node));
 1085         if (error || newp == NULL)
 1086                 return (error);
 1087 
 1088         if (nmaxproc < 0 || nmaxproc >= PID_MAX)
 1089                 return (EINVAL);
 1090 #ifdef __HAVE_CPU_MAXPROC
 1091         if (nmaxproc > cpu_maxproc())
 1092                 return (EINVAL);
 1093 #endif
 1094         maxproc = nmaxproc;
 1095 
 1096         return (0);
 1097 }
 1098 
 1099 /*
 1100  * sysctl helper routine for kern.securelevel.  ensures that the value
 1101  * only rises unless the caller has pid 1 (assumed to be init).
 1102  */
 1103 static int
 1104 sysctl_kern_securelevel(SYSCTLFN_ARGS)
 1105 {
 1106         int newsecurelevel, error;
 1107         struct sysctlnode node;
 1108 
 1109         newsecurelevel = securelevel;
 1110         node = *rnode;
 1111         node.sysctl_data = &newsecurelevel;
 1112         error = sysctl_lookup(SYSCTLFN_CALL(&node));
 1113         if (error || newp == NULL)
 1114                 return (error);
 1115 
 1116         if (newsecurelevel < securelevel && l && l->l_proc->p_pid != 1)
 1117                 return (EPERM);
 1118         securelevel = newsecurelevel;
 1119 
 1120         return (error);
 1121 }
 1122 
 1123 /*
 1124  * sysctl helper function for kern.hostid.  the hostid is a long, but
 1125  * we export it as an int, so we need to give it a little help.
 1126  */
 1127 static int
 1128 sysctl_kern_hostid(SYSCTLFN_ARGS)
 1129 {
 1130         int error, inthostid;
 1131         struct sysctlnode node;
 1132 
 1133         inthostid = hostid;  /* XXX assumes sizeof int <= sizeof long */
 1134         node = *rnode;
 1135         node.sysctl_data = &inthostid;
 1136         error = sysctl_lookup(SYSCTLFN_CALL(&node));
 1137         if (error || newp == NULL)
 1138                 return (error);
 1139 
 1140         hostid = (unsigned)inthostid;
 1141 
 1142         return (0);
 1143 }
 1144 
 1145 /*
 1146  * sysctl helper function for kern.hostname and kern.domainnname.
 1147  * resets the relevant recorded length when the underlying name is
 1148  * changed.
 1149  */
 1150 static int
 1151 sysctl_setlen(SYSCTLFN_ARGS)
 1152 {
 1153         int error;
 1154 
 1155         error = sysctl_lookup(SYSCTLFN_CALL(rnode));
 1156         if (error || newp == NULL)
 1157                 return (error);
 1158 
 1159         switch (rnode->sysctl_num) {
 1160         case KERN_HOSTNAME:
 1161                 hostnamelen = strlen((const char*)rnode->sysctl_data);
 1162                 break;
 1163         case KERN_DOMAINNAME:
 1164                 domainnamelen = strlen((const char*)rnode->sysctl_data);
 1165                 break;
 1166         }
 1167 
 1168         return (0);
 1169 }
 1170 
 1171 /*
 1172  * sysctl helper routine for kern.clockrate.  assembles a struct on
 1173  * the fly to be returned to the caller.
 1174  */
 1175 static int
 1176 sysctl_kern_clockrate(SYSCTLFN_ARGS)
 1177 {
 1178         struct clockinfo clkinfo;
 1179         struct sysctlnode node;
 1180 
 1181         clkinfo.tick = tick;
 1182         clkinfo.tickadj = tickadj;
 1183         clkinfo.hz = hz;
 1184         clkinfo.profhz = profhz;
 1185         clkinfo.stathz = stathz ? stathz : hz;
 1186 
 1187         node = *rnode;
 1188         node.sysctl_data = &clkinfo;
 1189         return (sysctl_lookup(SYSCTLFN_CALL(&node)));
 1190 }
 1191 
 1192 
 1193 /*
 1194  * sysctl helper routine for kern.file pseudo-subtree.
 1195  */
 1196 static int
 1197 sysctl_kern_file(SYSCTLFN_ARGS)
 1198 {
 1199         int error;
 1200         size_t buflen;
 1201         struct file *fp;
 1202         char *start, *where;
 1203 
 1204         start = where = oldp;
 1205         buflen = *oldlenp;
 1206         if (where == NULL) {
 1207                 /*
 1208                  * overestimate by 10 files
 1209                  */
 1210                 *oldlenp = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
 1211                 return (0);
 1212         }
 1213 
 1214         /*
 1215          * first copyout filehead
 1216          */
 1217         if (buflen < sizeof(filehead)) {
 1218                 *oldlenp = 0;
 1219                 return (0);
 1220         }
 1221         error = copyout(&filehead, where, sizeof(filehead));
 1222         if (error)
 1223                 return (error);
 1224         buflen -= sizeof(filehead);
 1225         where += sizeof(filehead);
 1226 
 1227         /*
 1228          * followed by an array of file structures
 1229          */
 1230         LIST_FOREACH(fp, &filehead, f_list) {
 1231                 if (buflen < sizeof(struct file)) {
 1232                         *oldlenp = where - start;
 1233                         return (ENOMEM);
 1234                 }
 1235                 error = copyout(fp, where, sizeof(struct file));
 1236                 if (error)
 1237                         return (error);
 1238                 buflen -= sizeof(struct file);
 1239                 where += sizeof(struct file);
 1240         }
 1241         *oldlenp = where - start;
 1242         return (0);
 1243 }
 1244 
 1245 /*
 1246  * sysctl helper routine for kern.autonicetime and kern.autoniceval.
 1247  * asserts that the assigned value is in the correct range.
 1248  */
 1249 static int
 1250 sysctl_kern_autonice(SYSCTLFN_ARGS)
 1251 {
 1252         int error, t = 0;
 1253         struct sysctlnode node;
 1254 
 1255         node = *rnode;
 1256         t = *(int*)node.sysctl_data;
 1257         node.sysctl_data = &t;
 1258         error = sysctl_lookup(SYSCTLFN_CALL(&node));
 1259         if (error || newp == NULL)
 1260                 return (error);
 1261 
 1262         switch (node.sysctl_num) {
 1263         case KERN_AUTONICETIME:
 1264                 if (t >= 0)
 1265                         autonicetime = t;
 1266                 break;
 1267         case KERN_AUTONICEVAL:
 1268                 if (t < PRIO_MIN)
 1269                         t = PRIO_MIN;
 1270                 else if (t > PRIO_MAX)
 1271                         t = PRIO_MAX;
 1272                 autoniceval = t;
 1273                 break;
 1274         }
 1275 
 1276         return (0);
 1277 }
 1278 
 1279 /*
 1280  * sysctl helper routine for kern.msgbufsize and kern.msgbuf.  for the
 1281  * former it merely checks the message buffer is set up.  for the latter,
 1282  * it also copies out the data if necessary.
 1283  */
 1284 static int
 1285 sysctl_msgbuf(SYSCTLFN_ARGS)
 1286 {
 1287         char *where = oldp;
 1288         size_t len, maxlen;
 1289         long beg, end;
 1290         int error;
 1291 
 1292         if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
 1293                 msgbufenabled = 0;
 1294                 return (ENXIO);
 1295         }
 1296 
 1297         switch (rnode->sysctl_num) {
 1298         case KERN_MSGBUFSIZE: {
 1299                 struct sysctlnode node = *rnode;
 1300                 int msg_bufs = (int)msgbufp->msg_bufs;
 1301                 node.sysctl_data = &msg_bufs;
 1302                 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
 1303         }
 1304         case KERN_MSGBUF:
 1305                 break;
 1306         default:
 1307                 return (EOPNOTSUPP);
 1308         }
 1309 
 1310         if (newp != NULL)
 1311                 return (EPERM);
 1312 
 1313         if (oldp == NULL) {
 1314                 /* always return full buffer size */
 1315                 *oldlenp = msgbufp->msg_bufs;
 1316                 return (0);
 1317         }
 1318 
 1319         error = 0;
 1320         maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
 1321 
 1322         /*
 1323          * First, copy from the write pointer to the end of
 1324          * message buffer.
 1325          */
 1326         beg = msgbufp->msg_bufx;
 1327         end = msgbufp->msg_bufs;
 1328         while (maxlen > 0) {
 1329                 len = MIN(end - beg, maxlen);
 1330                 if (len == 0)
 1331                         break;
 1332                 error = copyout(&msgbufp->msg_bufc[beg], where, len);
 1333                 if (error)
 1334                         break;
 1335                 where += len;
 1336                 maxlen -= len;
 1337 
 1338                 /*
 1339                  * ... then, copy from the beginning of message buffer to
 1340                  * the write pointer.
 1341                  */
 1342                 beg = 0;
 1343                 end = msgbufp->msg_bufx;
 1344         }
 1345 
 1346         return (error);
 1347 }
 1348 
 1349 /*
 1350  * sysctl helper routine for kern.defcorename.  in the case of a new
 1351  * string being assigned, check that it's not a zero-length string.
 1352  * (XXX the check in -current doesn't work, but do we really care?)
 1353  */
 1354 static int
 1355 sysctl_kern_defcorename(SYSCTLFN_ARGS)
 1356 {
 1357         int error;
 1358         char newcorename[MAXPATHLEN];
 1359         struct sysctlnode node;
 1360 
 1361         node = *rnode;
 1362         node.sysctl_data = &newcorename[0];
 1363         memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
 1364         error = sysctl_lookup(SYSCTLFN_CALL(&node));
 1365         if (error || newp == NULL)
 1366                 return (error);
 1367 
 1368         /*
 1369          * when sysctl_lookup() deals with a string, it's guaranteed
 1370          * to come back nul terminated.  so there.  :)
 1371          */
 1372         if (strlen(newcorename) == 0)
 1373                 return (EINVAL);
 1374 
 1375         memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
 1376 
 1377         return (0);
 1378 }
 1379 
 1380 /*
 1381  * sysctl helper routine for kern.cp_time node.  adds up cpu time
 1382  * across all cpus.
 1383  */
 1384 static int
 1385 sysctl_kern_cptime(SYSCTLFN_ARGS)
 1386 {
 1387         struct sysctlnode node = *rnode;
 1388 
 1389 #ifndef MULTIPROCESSOR
 1390 
 1391         if (namelen == 1) {
 1392                 if (name[0] != 0)
 1393                         return (ENOENT);
 1394                 /*
 1395                  * you're allowed to ask for the zero'th processor
 1396                  */
 1397                 name++;
 1398                 namelen--;
 1399         }
 1400         node.sysctl_data = curcpu()->ci_schedstate.spc_cp_time;
 1401         node.sysctl_size = sizeof(curcpu()->ci_schedstate.spc_cp_time);
 1402         return (sysctl_lookup(SYSCTLFN_CALL(&node)));
 1403 
 1404 #else /* MULTIPROCESSOR */
 1405 
 1406         u_int64_t *cp_time = NULL;
 1407         int error, n = sysctl_ncpus(), i;
 1408         struct cpu_info *ci;
 1409         CPU_INFO_ITERATOR cii;
 1410 
 1411         /*
 1412          * if you specifically pass a buffer that is the size of the
 1413          * sum, or if you are probing for the size, you get the "sum"
 1414          * of cp_time (and the size thereof) across all processors.
 1415          *
 1416          * alternately, you can pass an additional mib number and get
 1417          * cp_time for that particular processor.
 1418          */
 1419         switch (namelen) {
 1420         case 0:
 1421                 if (*oldlenp == sizeof(u_int64_t) * CPUSTATES || oldp == NULL) {
 1422                         node.sysctl_size = sizeof(u_int64_t) * CPUSTATES;
 1423                         n = -1; /* SUM */
 1424                 }
 1425                 else {
 1426                         node.sysctl_size = n * sizeof(u_int64_t) * CPUSTATES;
 1427                         n = -2; /* ALL */
 1428                 }
 1429                 break;
 1430         case 1:
 1431                 if (name[0] < 0 || name[0] >= n)
 1432                         return (ENOENT); /* ENOSUCHPROCESSOR */
 1433                 node.sysctl_size = sizeof(u_int64_t) * CPUSTATES;
 1434                 n = name[0];
 1435                 /*
 1436                  * adjust these so that sysctl_lookup() will be happy
 1437                  */
 1438                 name++;
 1439                 namelen--;
 1440                 break;
 1441         default:
 1442                 return (EINVAL);
 1443         }
 1444 
 1445         cp_time = malloc(node.sysctl_size, M_TEMP, M_WAITOK|M_CANFAIL);
 1446         if (cp_time == NULL)
 1447                 return (ENOMEM);
 1448         node.sysctl_data = cp_time;
 1449         memset(cp_time, 0, node.sysctl_size);
 1450 
 1451         for (CPU_INFO_FOREACH(cii, ci)) {
 1452                 if (n <= 0)
 1453                         for (i = 0; i < CPUSTATES; i++)
 1454                                 cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
 1455                 /*
 1456                  * if a specific processor was requested and we just
 1457                  * did it, we're done here
 1458                  */
 1459                 if (n == 0)
 1460                         break;
 1461                 /*
 1462                  * if doing "all", skip to next cp_time set for next processor
 1463                  */
 1464                 if (n == -2)
 1465                         cp_time += CPUSTATES;
 1466                 /*
 1467                  * if we're doing a specific processor, we're one
 1468                  * processor closer
 1469                  */
 1470                 if (n > 0)
 1471                         n--;
 1472         }
 1473 
 1474         error = sysctl_lookup(SYSCTLFN_CALL(&node));
 1475         free(node.sysctl_data, M_TEMP);
 1476         return (error);
 1477 
 1478 #endif /* MULTIPROCESSOR */
 1479 }
 1480 
 1481 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
 1482 /*
 1483  * sysctl helper routine for kern.sysvipc_info subtree.
 1484  */
 1485 
 1486 #define FILL_PERM(src, dst) do { \
 1487         (dst)._key = (src)._key; \
 1488         (dst).uid = (src).uid; \
 1489         (dst).gid = (src).gid; \
 1490         (dst).cuid = (src).cuid; \
 1491         (dst).cgid = (src).cgid; \
 1492         (dst).mode = (src).mode; \
 1493         (dst)._seq = (src)._seq; \
 1494 } while (/*CONSTCOND*/ 0);
 1495 #define FILL_MSG(src, dst) do { \
 1496         FILL_PERM((src).msg_perm, (dst).msg_perm); \
 1497         (dst).msg_qnum = (src).msg_qnum; \
 1498         (dst).msg_qbytes = (src).msg_qbytes; \
 1499         (dst)._msg_cbytes = (src)._msg_cbytes; \
 1500         (dst).msg_lspid = (src).msg_lspid; \
 1501         (dst).msg_lrpid = (src).msg_lrpid; \
 1502         (dst).msg_stime = (src).msg_stime; \
 1503         (dst).msg_rtime = (src).msg_rtime; \
 1504         (dst).msg_ctime = (src).msg_ctime; \
 1505 } while (/*CONSTCOND*/ 0)
 1506 #define FILL_SEM(src, dst) do { \
 1507         FILL_PERM((src).sem_perm, (dst).sem_perm); \
 1508         (dst).sem_nsems = (src).sem_nsems; \
 1509         (dst).sem_otime = (src).sem_otime; \
 1510         (dst).sem_ctime = (src).sem_ctime; \
 1511 } while (/*CONSTCOND*/ 0)
 1512 #define FILL_SHM(src, dst) do { \
 1513         FILL_PERM((src).shm_perm, (dst).shm_perm); \
 1514         (dst).shm_segsz = (src).shm_segsz; \
 1515         (dst).shm_lpid = (src).shm_lpid; \
 1516         (dst).shm_cpid = (src).shm_cpid; \
 1517         (dst).shm_atime = (src).shm_atime; \
 1518         (dst).shm_dtime = (src).shm_dtime; \
 1519         (dst).shm_ctime = (src).shm_ctime; \
 1520         (dst).shm_nattch = (src).shm_nattch; \
 1521 } while (/*CONSTCOND*/ 0)
 1522 
 1523 static int
 1524 sysctl_kern_sysvipc(SYSCTLFN_ARGS)
 1525 {
 1526         void *where = oldp;
 1527         size_t *sizep = oldlenp;
 1528 #ifdef SYSVMSG
 1529         struct msg_sysctl_info *msgsi = NULL;
 1530 #endif
 1531 #ifdef SYSVSEM
 1532         struct sem_sysctl_info *semsi = NULL;
 1533 #endif
 1534 #ifdef SYSVSHM
 1535         struct shm_sysctl_info *shmsi = NULL;
 1536 #endif
 1537         size_t infosize, dssize, tsize, buflen;
 1538         void *buf = NULL;
 1539         char *start;
 1540         int32_t nds;
 1541         int i, error, ret;
 1542 
 1543         if (namelen != 1)
 1544                 return (EINVAL);
 1545 
 1546         start = where;
 1547         buflen = *sizep;
 1548 
 1549         switch (*name) {
 1550         case KERN_SYSVIPC_MSG_INFO:
 1551 #ifdef SYSVMSG
 1552                 infosize = sizeof(msgsi->msginfo);
 1553                 nds = msginfo.msgmni;
 1554                 dssize = sizeof(msgsi->msgids[0]);
 1555                 break;
 1556 #else
 1557                 return (EINVAL);
 1558 #endif
 1559         case KERN_SYSVIPC_SEM_INFO:
 1560 #ifdef SYSVSEM
 1561                 infosize = sizeof(semsi->seminfo);
 1562                 nds = seminfo.semmni;
 1563                 dssize = sizeof(semsi->semids[0]);
 1564                 break;
 1565 #else
 1566                 return (EINVAL);
 1567 #endif
 1568         case KERN_SYSVIPC_SHM_INFO:
 1569 #ifdef SYSVSHM
 1570                 infosize = sizeof(shmsi->shminfo);
 1571                 nds = shminfo.shmmni;
 1572                 dssize = sizeof(shmsi->shmids[0]);
 1573                 break;
 1574 #else
 1575                 return (EINVAL);
 1576 #endif
 1577         default:
 1578                 return (EINVAL);
 1579         }
 1580         /*
 1581          * Round infosize to 64 bit boundary if requesting more than just
 1582          * the info structure or getting the total data size.
 1583          */
 1584         if (where == NULL || *sizep > infosize)
 1585                 infosize = ((infosize + 7) / 8) * 8;
 1586         tsize = infosize + nds * dssize;
 1587 
 1588         /* Return just the total size required. */
 1589         if (where == NULL) {
 1590                 *sizep = tsize;
 1591                 return (0);
 1592         }
 1593 
 1594         /* Not enough room for even the info struct. */
 1595         if (buflen < infosize) {
 1596                 *sizep = 0;
 1597                 return (ENOMEM);
 1598         }
 1599         buf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK);
 1600         memset(buf, 0, min(tsize, buflen));
 1601 
 1602         switch (*name) {
 1603 #ifdef SYSVMSG
 1604         case KERN_SYSVIPC_MSG_INFO:
 1605                 msgsi = (struct msg_sysctl_info *)buf;
 1606                 msgsi->msginfo = msginfo;
 1607                 break;
 1608 #endif
 1609 #ifdef SYSVSEM
 1610         case KERN_SYSVIPC_SEM_INFO:
 1611                 semsi = (struct sem_sysctl_info *)buf;
 1612                 semsi->seminfo = seminfo;
 1613                 break;
 1614 #endif
 1615 #ifdef SYSVSHM
 1616         case KERN_SYSVIPC_SHM_INFO:
 1617                 shmsi = (struct shm_sysctl_info *)buf;
 1618                 shmsi->shminfo = shminfo;
 1619                 break;
 1620 #endif
 1621         }
 1622         buflen -= infosize;
 1623 
 1624         ret = 0;
 1625         if (buflen > 0) {
 1626                 /* Fill in the IPC data structures.  */
 1627                 for (i = 0; i < nds; i++) {
 1628                         if (buflen < dssize) {
 1629                                 ret = ENOMEM;
 1630                                 break;
 1631                         }
 1632                         switch (*name) {
 1633 #ifdef SYSVMSG
 1634                         case KERN_SYSVIPC_MSG_INFO:
 1635                                 FILL_MSG(msqids[i], msgsi->msgids[i]);
 1636                                 break;
 1637 #endif
 1638 #ifdef SYSVSEM
 1639                         case KERN_SYSVIPC_SEM_INFO:
 1640                                 FILL_SEM(sema[i], semsi->semids[i]);
 1641                                 break;
 1642 #endif
 1643 #ifdef SYSVSHM
 1644                         case KERN_SYSVIPC_SHM_INFO:
 1645                                 FILL_SHM(shmsegs[i], shmsi->shmids[i]);
 1646                                 break;
 1647 #endif
 1648                         }
 1649                         buflen -= dssize;
 1650                 }
 1651         }
 1652         *sizep -= buflen;
 1653         error = copyout(buf, start, *sizep);
 1654         /* If copyout succeeded, use return code set earlier. */
 1655         if (error == 0)
 1656                 error = ret;
 1657         if (buf)
 1658                 free(buf, M_TEMP);
 1659         return (error);
 1660 }
 1661 
 1662 #undef FILL_PERM
 1663 #undef FILL_MSG
 1664 #undef FILL_SEM
 1665 #undef FILL_SHM
 1666 
 1667 #endif /* defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM) */
 1668 
 1669 #if NPTY > 0
 1670 /*
 1671  * sysctl helper routine for kern.maxptys.  ensures that any new value
 1672  * is acceptable to the pty subsystem.
 1673  */
 1674 static int
 1675 sysctl_kern_maxptys(SYSCTLFN_ARGS)
 1676 {
 1677         int pty_maxptys(int, int);              /* defined in kern/tty_pty.c */
 1678         int error, max;
 1679         struct sysctlnode node;
 1680 
 1681         /* get current value of maxptys */
 1682         max = pty_maxptys(0, 0);
 1683 
 1684         node = *rnode;
 1685         node.sysctl_data = &max;
 1686         error = sysctl_lookup(SYSCTLFN_CALL(&node));
 1687         if (error || newp == NULL)
 1688                 return (error);
 1689 
 1690         if (max != pty_maxptys(max, 1))
 1691                 return (EINVAL);
 1692 
 1693         return (0);
 1694 }
 1695 #endif /* NPTY > 0 */
 1696 
 1697 /*
 1698  * sysctl helper routine for kern.sbmax.  basically just ensures that
 1699  * any new value is not too small.
 1700  */
 1701 static int
 1702 sysctl_kern_sbmax(SYSCTLFN_ARGS)
 1703 {
 1704         int error, new_sbmax;
 1705         struct sysctlnode node;
 1706 
 1707         new_sbmax = sb_max;
 1708         node = *rnode;
 1709         node.sysctl_data = &new_sbmax;
 1710         error = sysctl_lookup(SYSCTLFN_CALL(&node));
 1711         if (error || newp == NULL)
 1712                 return (error);
 1713 
 1714         error = sb_max_set(new_sbmax);
 1715 
 1716         return (error);
 1717 }
 1718 
 1719 /*
 1720  * sysctl helper routine for kern.urandom node.  picks a random number
 1721  * for you.
 1722  */
 1723 static int
 1724 sysctl_kern_urnd(SYSCTLFN_ARGS)
 1725 {
 1726 #if NRND > 0
 1727         int v;
 1728 
 1729         if (rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY) == sizeof(v)) {
 1730                 struct sysctlnode node = *rnode;
 1731                 node.sysctl_data = &v;
 1732                 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
 1733         }
 1734         else
 1735                 return (EIO);   /*XXX*/
 1736 #else
 1737         return (EOPNOTSUPP);
 1738 #endif
 1739 }
 1740 
 1741 /*
 1742  * sysctl helper routine to do kern.lwp.* work.
 1743  */
 1744 static int
 1745 sysctl_kern_lwp(SYSCTLFN_ARGS)
 1746 {
 1747         struct kinfo_lwp klwp;
 1748         struct proc *p;
 1749         struct lwp *l2;
 1750         char *where, *dp;
 1751         int pid, elem_size, elem_count;
 1752         int buflen, needed, error;
 1753 
 1754         if (namelen == 1 && name[0] == CTL_QUERY)
 1755                 return (sysctl_query(SYSCTLFN_CALL(rnode)));
 1756 
 1757         dp = where = oldp;
 1758         buflen = where != NULL ? *oldlenp : 0;
 1759         error = needed = 0;
 1760 
 1761         if (newp != NULL || namelen != 3)
 1762                 return (EINVAL);
 1763         pid = name[0];
 1764         elem_size = name[1];
 1765         elem_count = name[2];
 1766 
 1767         p = pfind(pid);
 1768         if (p == NULL)
 1769                 return (ESRCH);
 1770         LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
 1771                 if (buflen >= elem_size && elem_count > 0) {
 1772                         fill_lwp(l2, &klwp);
 1773                         /*
 1774                          * Copy out elem_size, but not larger than
 1775                          * the size of a struct kinfo_proc2.
 1776                          */
 1777                         error = copyout(&klwp, dp,
 1778                             min(sizeof(klwp), elem_size));
 1779                         if (error)
 1780                                 goto cleanup;
 1781                         dp += elem_size;
 1782                         buflen -= elem_size;
 1783                         elem_count--;
 1784                 }
 1785                 needed += elem_size;
 1786         }
 1787 
 1788         if (where != NULL) {
 1789                 *oldlenp = dp - where;
 1790                 if (needed > *oldlenp)
 1791                         return (ENOMEM);
 1792         } else {
 1793                 needed += KERN_LWPSLOP;
 1794                 *oldlenp = needed;
 1795         }
 1796         return (0);
 1797  cleanup:
 1798         return (error);
 1799 }
 1800 
 1801 /*
 1802  * sysctl helper routine for kern.forkfsleep node.  ensures that the
 1803  * given value is not too large or two small, and is at least one
 1804  * timer tick if not zero.
 1805  */
 1806 static int
 1807 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
 1808 {
 1809         /* userland sees value in ms, internally is in ticks */
 1810         extern int forkfsleep;          /* defined in kern/kern_fork.c */
 1811         int error, timo, lsleep;
 1812         struct sysctlnode node;
 1813 
 1814         lsleep = forkfsleep * 1000 / hz;
 1815         node = *rnode;
 1816         node.sysctl_data = &lsleep;
 1817         error = sysctl_lookup(SYSCTLFN_CALL(&node));
 1818         if (error || newp == NULL)
 1819                 return (error);
 1820 
 1821         /* refuse negative values, and overly 'long time' */
 1822         if (lsleep < 0 || lsleep > MAXSLP * 1000)
 1823                 return (EINVAL);
 1824 
 1825         timo = mstohz(lsleep);
 1826 
 1827         /* if the interval is >0 ms && <1 tick, use 1 tick */
 1828         if (lsleep != 0 && timo == 0)
 1829                 forkfsleep = 1;
 1830         else
 1831                 forkfsleep = timo;
 1832 
 1833         return (0);
 1834 }
 1835 
 1836 /*
 1837  * sysctl helper routine for kern.root_partition
 1838  */
 1839 static int
 1840 sysctl_kern_root_partition(SYSCTLFN_ARGS)
 1841 {
 1842         int rootpart = DISKPART(rootdev);
 1843         struct sysctlnode node = *rnode;
 1844 
 1845         node.sysctl_data = &rootpart;
 1846         return (sysctl_lookup(SYSCTLFN_CALL(&node)));
 1847 }
 1848 
 1849 /*
 1850  * sysctl helper function for kern.drivers
 1851  */
 1852 static int
 1853 sysctl_kern_drivers(SYSCTLFN_ARGS)
 1854 {
 1855         int error;
 1856         size_t buflen;
 1857         struct kinfo_drivers kd;
 1858         char *start, *where;
 1859         const char *dname;
 1860         int i;
 1861         extern struct devsw_conv *devsw_conv;
 1862         extern int max_devsw_convs;
 1863 
 1864         if (newp != NULL || namelen != 0)
 1865                 return (EINVAL);
 1866 
 1867         start = where = oldp;
 1868         buflen = *oldlenp;
 1869         if (where == NULL) {
 1870                 *oldlenp = max_devsw_convs * sizeof kd;
 1871                 return 0;
 1872         }
 1873 
 1874         /*
 1875          * An array of kinfo_drivers structures
 1876          */
 1877         error = 0;
 1878         for (i = 0; i < max_devsw_convs; i++) {
 1879                 dname = devsw_conv[i].d_name;
 1880                 if (dname == NULL)
 1881                         continue;
 1882                 if (buflen < sizeof kd) {
 1883                         error = ENOMEM;
 1884                         break;
 1885                 }
 1886                 memset(&kd, 0, sizeof(kd));
 1887                 kd.d_bmajor = devsw_conv[i].d_bmajor;
 1888                 kd.d_cmajor = devsw_conv[i].d_cmajor;
 1889                 strlcpy(kd.d_name, dname, sizeof kd.d_name);
 1890                 error = copyout(&kd, where, sizeof kd);
 1891                 if (error != 0)
 1892                         break;
 1893                 buflen -= sizeof kd;
 1894                 where += sizeof kd;
 1895         }
 1896         *oldlenp = where - start;
 1897         return error;
 1898 }
 1899 
 1900 /*
 1901  * sysctl helper function for kern.file2
 1902  */
 1903 static int
 1904 sysctl_kern_file2(SYSCTLFN_ARGS)
 1905 {
 1906         struct proc *p;
 1907         struct file *fp;
 1908         struct filedesc *fd;
 1909         struct kinfo_file kf;
 1910         char *dp;
 1911         u_int i, op;
 1912         size_t len, needed, elem_size, out_size;
 1913         int error, arg, elem_count;
 1914 
 1915         if (namelen == 1 && name[0] == CTL_QUERY)
 1916                 return (sysctl_query(SYSCTLFN_CALL(rnode)));
 1917 
 1918         if (namelen != 4)
 1919                 return (EINVAL);
 1920 
 1921         error = 0;
 1922         dp = oldp;
 1923         len = (oldp != NULL) ? *oldlenp : 0;
 1924         op = name[0];
 1925         arg = name[1];
 1926         elem_size = name[2];
 1927         elem_count = name[3];
 1928         out_size = MIN(sizeof(kf), elem_size);
 1929         needed = 0;
 1930 
 1931         if (elem_size < 1 || elem_count < 0)
 1932                 return (EINVAL);
 1933 
 1934         switch (op) {
 1935         case KERN_FILE_BYFILE:
 1936                 /*
 1937                  * doesn't use arg so it must be zero
 1938                  */
 1939                 if (arg != 0)
 1940                         return (EINVAL);
 1941                 LIST_FOREACH(fp, &filehead, f_list) {
 1942                         if (len >= elem_size && elem_count > 0) {
 1943                                 fill_file(&kf, fp, NULL, 0);
 1944                                 error = copyout(&kf, dp, out_size);
 1945                                 if (error)
 1946                                         break;
 1947                                 dp += elem_size;
 1948                                 len -= elem_size;
 1949                         }
 1950                         needed += elem_size;
 1951                         if (elem_count > 0) {
 1952                                 if (elem_count != INT_MAX)
 1953                                         elem_count--;
 1954                         }
 1955                 }
 1956                 break;
 1957             case KERN_FILE_BYPID:
 1958                 if (arg < -1)
 1959                         /* -1 means all processes */
 1960                         return (EINVAL);
 1961                 proclist_lock_read();
 1962                 PROCLIST_FOREACH(p, &allproc) {
 1963                         if (p->p_stat == SIDL)
 1964                                 /* skip embryonic processes */
 1965                                 continue;
 1966                         if (arg > 0 && p->p_pid != arg)
 1967                                 /* pick only the one we want */
 1968                                 /* XXX want 0 to mean "kernel files" */
 1969                                 continue;
 1970                         fd = p->p_fd;
 1971                         for (i = 0; i < fd->fd_nfiles; i++) {
 1972                                 fp = fd->fd_ofiles[i];
 1973                                 if (fp == NULL || !FILE_IS_USABLE(fp))
 1974                                         continue;
 1975                                 if (len >= elem_size && elem_count > 0) {
 1976                                         fill_file(&kf, fd->fd_ofiles[i],
 1977                                                   p, i);
 1978                                         error = copyout(&kf, dp, out_size);
 1979                                         if (error)
 1980                                                 break;
 1981                                         dp += elem_size;
 1982                                         len -= elem_size;
 1983                                 }
 1984                                 needed += elem_size;
 1985                                 if (elem_count > 0) {
 1986                                         if (elem_count != INT_MAX)
 1987                                                 elem_count--;
 1988                                 }
 1989                         }
 1990                 }
 1991                 proclist_unlock_read();
 1992                 break;
 1993         default:
 1994                 return (EINVAL);
 1995         }
 1996 
 1997         if (oldp == NULL)
 1998                 needed += KERN_FILESLOP * elem_size;
 1999         *oldlenp = needed;
 2000 
 2001         return (error);
 2002 }
 2003 
 2004 static void
 2005 fill_file(struct kinfo_file *kp, const struct file *fp, struct proc *p, int i)
 2006 {
 2007 
 2008         memset(kp, 0, sizeof(*kp));
 2009 
 2010         kp->ki_fileaddr =       PTRTOUINT64(fp);
 2011         kp->ki_flag =           fp->f_flag;
 2012         kp->ki_iflags =         fp->f_iflags;
 2013         kp->ki_ftype =          fp->f_type;
 2014         kp->ki_count =          fp->f_count;
 2015         kp->ki_msgcount =       fp->f_msgcount;
 2016         kp->ki_usecount =       fp->f_usecount;
 2017         kp->ki_fucred =         PTRTOUINT64(fp->f_cred);
 2018         kp->ki_fuid =           fp->f_cred->cr_uid;
 2019         kp->ki_fgid =           fp->f_cred->cr_gid;
 2020         kp->ki_fops =           PTRTOUINT64(fp->f_ops);
 2021         kp->ki_foffset =        fp->f_offset;
 2022         kp->ki_fdata =          PTRTOUINT64(fp->f_data);
 2023 
 2024         /* vnode information to glue this file to something */
 2025         if (fp->f_type == DTYPE_VNODE) {
 2026                 struct vnode *vp = (struct vnode *)fp->f_data;
 2027 
 2028                 kp->ki_vun =    PTRTOUINT64(vp->v_un.vu_socket);
 2029                 kp->ki_vsize =  vp->v_size;
 2030                 kp->ki_vtype =  vp->v_type;
 2031                 kp->ki_vtag =   vp->v_tag;
 2032                 kp->ki_vdata =  PTRTOUINT64(vp->v_data);
 2033         }
 2034 
 2035         /* process information when retrieved via KERN_FILE_BYPID */
 2036         if (p) {
 2037                 kp->ki_pid =            p->p_pid;
 2038                 kp->ki_fd =             i;
 2039                 kp->ki_ofileflags =     p->p_fd->fd_ofileflags[i];
 2040         }
 2041 }
 2042 
 2043 static int
 2044 sysctl_doeproc(SYSCTLFN_ARGS)
 2045 {
 2046         struct eproc eproc;
 2047         struct kinfo_proc2 kproc2;
 2048         struct kinfo_proc *dp;
 2049         struct proc *p;
 2050         const struct proclist_desc *pd;
 2051         char *where, *dp2;
 2052         int type, op, arg;
 2053         u_int elem_size, elem_count;
 2054         size_t buflen, needed;
 2055         int error;
 2056 
 2057         if (namelen == 1 && name[0] == CTL_QUERY)
 2058                 return (sysctl_query(SYSCTLFN_CALL(rnode)));
 2059 
 2060         dp = oldp;
 2061         dp2 = where = oldp;
 2062         buflen = where != NULL ? *oldlenp : 0;
 2063         error = 0;
 2064         needed = 0;
 2065         type = rnode->sysctl_num;
 2066 
 2067         if (type == KERN_PROC) {
 2068                 if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
 2069                         return (EINVAL);
 2070                 op = name[0];
 2071                 if (op != KERN_PROC_ALL)
 2072                         arg = name[1];
 2073                 else
 2074                         arg = 0;                /* Quell compiler warning */
 2075                 elem_size = elem_count = 0;     /* Ditto */
 2076         } else {
 2077                 if (namelen != 4)
 2078                         return (EINVAL);
 2079                 op = name[0];
 2080                 arg = name[1];
 2081                 elem_size = name[2];
 2082                 elem_count = name[3];
 2083         }
 2084 
 2085         proclist_lock_read();
 2086 
 2087         pd = proclists;
 2088 again:
 2089         PROCLIST_FOREACH(p, pd->pd_list) {
 2090                 /*
 2091                  * Skip embryonic processes.
 2092                  */
 2093                 if (p->p_stat == SIDL)
 2094                         continue;
 2095                 /*
 2096                  * TODO - make more efficient (see notes below).
 2097                  * do by session.
 2098                  */
 2099                 switch (op) {
 2100 
 2101                 case KERN_PROC_PID:
 2102                         /* could do this with just a lookup */
 2103                         if (p->p_pid != (pid_t)arg)
 2104                                 continue;
 2105                         break;
 2106 
 2107                 case KERN_PROC_PGRP:
 2108                         /* could do this by traversing pgrp */
 2109                         if (p->p_pgrp->pg_id != (pid_t)arg)
 2110                                 continue;
 2111                         break;
 2112 
 2113                 case KERN_PROC_SESSION:
 2114                         if (p->p_session->s_sid != (pid_t)arg)
 2115                                 continue;
 2116                         break;
 2117 
 2118                 case KERN_PROC_TTY:
 2119                         if (arg == (int) KERN_PROC_TTY_REVOKE) {
 2120                                 if ((p->p_flag & P_CONTROLT) == 0 ||
 2121                                     p->p_session->s_ttyp == NULL ||
 2122                                     p->p_session->s_ttyvp != NULL)
 2123                                         continue;
 2124                         } else if ((p->p_flag & P_CONTROLT) == 0 ||
 2125                             p->p_session->s_ttyp == NULL) {
 2126                                 if ((dev_t)arg != KERN_PROC_TTY_NODEV)
 2127                                         continue;
 2128                         } else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
 2129                                 continue;
 2130                         break;
 2131 
 2132                 case KERN_PROC_UID:
 2133                         if (p->p_ucred->cr_uid != (uid_t)arg)
 2134                                 continue;
 2135                         break;
 2136 
 2137                 case KERN_PROC_RUID:
 2138                         if (p->p_cred->p_ruid != (uid_t)arg)
 2139                                 continue;
 2140                         break;
 2141 
 2142                 case KERN_PROC_GID:
 2143                         if (p->p_ucred->cr_gid != (uid_t)arg)
 2144                                 continue;
 2145                         break;
 2146 
 2147                 case KERN_PROC_RGID:
 2148                         if (p->p_cred->p_rgid != (uid_t)arg)
 2149                                 continue;
 2150                         break;
 2151 
 2152                 case KERN_PROC_ALL:
 2153                         /* allow everything */
 2154                         break;
 2155 
 2156                 default:
 2157                         error = EINVAL;
 2158                         goto cleanup;
 2159                 }
 2160                 if (type == KERN_PROC) {
 2161                         if (buflen >= sizeof(struct kinfo_proc)) {
 2162                                 fill_eproc(p, &eproc);
 2163                                 error = copyout(p, &dp->kp_proc,
 2164                                     sizeof(struct proc));
 2165                                 if (error)
 2166                                         goto cleanup;
 2167                                 error = copyout(&eproc, &dp->kp_eproc,
 2168                                     sizeof(eproc));
 2169                                 if (error)
 2170                                         goto cleanup;
 2171                                 dp++;
 2172                                 buflen -= sizeof(struct kinfo_proc);
 2173                         }
 2174                         needed += sizeof(struct kinfo_proc);
 2175                 } else { /* KERN_PROC2 */
 2176                         if (buflen >= elem_size && elem_count > 0) {
 2177                                 fill_kproc2(p, &kproc2);
 2178                                 /*
 2179                                  * Copy out elem_size, but not larger than
 2180                                  * the size of a struct kinfo_proc2.
 2181                                  */
 2182                                 error = copyout(&kproc2, dp2,
 2183                                     min(sizeof(kproc2), elem_size));
 2184                                 if (error)
 2185                                         goto cleanup;
 2186                                 dp2 += elem_size;
 2187                                 buflen -= elem_size;
 2188                                 elem_count--;
 2189                         }
 2190                         needed += elem_size;
 2191                 }
 2192         }
 2193         pd++;
 2194         if (pd->pd_list != NULL)
 2195                 goto again;
 2196         proclist_unlock_read();
 2197 
 2198         if (where != NULL) {
 2199                 if (type == KERN_PROC)
 2200                         *oldlenp = (char *)dp - where;
 2201                 else
 2202                         *oldlenp = dp2 - where;
 2203                 if (needed > *oldlenp)
 2204                         return (ENOMEM);
 2205         } else {
 2206                 needed += KERN_PROCSLOP;
 2207                 *oldlenp = needed;
 2208         }
 2209         return (0);
 2210  cleanup:
 2211         proclist_unlock_read();
 2212         return (error);
 2213 }
 2214 
 2215 /*
 2216  * sysctl helper routine for kern.proc_args pseudo-subtree.
 2217  */
 2218 static int
 2219 sysctl_kern_proc_args(SYSCTLFN_ARGS)
 2220 {
 2221         struct ps_strings pss;
 2222         struct proc *p, *up = l->l_proc;
 2223         size_t len, upper_bound, xlen, i;
 2224         struct uio auio;
 2225         struct iovec aiov;
 2226         vaddr_t argv;
 2227         pid_t pid;
 2228         int nargv, type, error;
 2229         char *arg;
 2230         char *tmp;
 2231 
 2232         if (namelen == 1 && name[0] == CTL_QUERY)
 2233                 return (sysctl_query(SYSCTLFN_CALL(rnode)));
 2234 
 2235         if (newp != NULL || namelen != 2)
 2236                 return (EINVAL);
 2237         pid = name[0];
 2238         type = name[1];
 2239 
 2240         switch (type) {
 2241         case KERN_PROC_ARGV:
 2242         case KERN_PROC_NARGV:
 2243         case KERN_PROC_ENV:
 2244         case KERN_PROC_NENV:
 2245                 /* ok */
 2246                 break;
 2247         default:
 2248                 return (EINVAL);
 2249         }
 2250 
 2251         /* check pid */
 2252         if ((p = pfind(pid)) == NULL)
 2253                 return (EINVAL);
 2254 
 2255         /* only root or same user change look at the environment */
 2256         if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
 2257                 if (up->p_ucred->cr_uid != 0) {
 2258                         if (up->p_cred->p_ruid != p->p_cred->p_ruid ||
 2259                             up->p_cred->p_ruid != p->p_cred->p_svuid)
 2260                                 return (EPERM);
 2261                 }
 2262         }
 2263 
 2264         if (oldp == NULL) {
 2265                 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
 2266                         *oldlenp = sizeof (int);
 2267                 else
 2268                         *oldlenp = ARG_MAX;     /* XXX XXX XXX */
 2269                 return (0);
 2270         }
 2271 
 2272         /*
 2273          * Zombies don't have a stack, so we can't read their psstrings.
 2274          * System processes also don't have a user stack.
 2275          */
 2276         if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0)
 2277                 return (EINVAL);
 2278 
 2279         /*
 2280          * Lock the process down in memory.
 2281          */
 2282         /* XXXCDC: how should locking work here? */
 2283         if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
 2284                 return (EFAULT);
 2285 
 2286         p->p_vmspace->vm_refcnt++;      /* XXX */
 2287 
 2288         /*
 2289          * Allocate a temporary buffer to hold the arguments.
 2290          */
 2291         arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
 2292 
 2293         /*
 2294          * Read in the ps_strings structure.
 2295          */
 2296         aiov.iov_base = &pss;
 2297         aiov.iov_len = sizeof(pss);
 2298         auio.uio_iov = &aiov;
 2299         auio.uio_iovcnt = 1;
 2300         auio.uio_offset = (vaddr_t)p->p_psstr;
 2301         auio.uio_resid = sizeof(pss);
 2302         auio.uio_segflg = UIO_SYSSPACE;
 2303         auio.uio_rw = UIO_READ;
 2304         auio.uio_procp = NULL;
 2305         error = uvm_io(&p->p_vmspace->vm_map, &auio);
 2306         if (error)
 2307                 goto done;
 2308 
 2309         if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
 2310                 memcpy(&nargv, (char *)&pss + p->p_psnargv, sizeof(nargv));
 2311         else
 2312                 memcpy(&nargv, (char *)&pss + p->p_psnenv, sizeof(nargv));
 2313         if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
 2314                 error = copyout(&nargv, oldp, sizeof(nargv));
 2315                 *oldlenp = sizeof(nargv);
 2316                 goto done;
 2317         }
 2318         /*
 2319          * Now read the address of the argument vector.
 2320          */
 2321         switch (type) {
 2322         case KERN_PROC_ARGV:
 2323                 /* XXX compat32 stuff here */
 2324                 memcpy(&tmp, (char *)&pss + p->p_psargv, sizeof(tmp));
 2325                 break;
 2326         case KERN_PROC_ENV:
 2327                 memcpy(&tmp, (char *)&pss + p->p_psenv, sizeof(tmp));
 2328                 break;
 2329         default:
 2330                 return (EINVAL);
 2331         }
 2332         auio.uio_offset = (off_t)(unsigned long)tmp;
 2333         aiov.iov_base = &argv;
 2334         aiov.iov_len = sizeof(argv);
 2335         auio.uio_iov = &aiov;
 2336         auio.uio_iovcnt = 1;
 2337         auio.uio_resid = sizeof(argv);
 2338         auio.uio_segflg = UIO_SYSSPACE;
 2339         auio.uio_rw = UIO_READ;
 2340         auio.uio_procp = NULL;
 2341         error = uvm_io(&p->p_vmspace->vm_map, &auio);
 2342         if (error)
 2343                 goto done;
 2344 
 2345         /*
 2346          * Now copy in the actual argument vector, one page at a time,
 2347          * since we don't know how long the vector is (though, we do
 2348          * know how many NUL-terminated strings are in the vector).
 2349          */
 2350         len = 0;
 2351         upper_bound = *oldlenp;
 2352         for (; nargv != 0 && len < upper_bound; len += xlen) {
 2353                 aiov.iov_base = arg;
 2354                 aiov.iov_len = PAGE_SIZE;
 2355                 auio.uio_iov = &aiov;
 2356                 auio.uio_iovcnt = 1;
 2357                 auio.uio_offset = argv + len;
 2358                 xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
 2359                 auio.uio_resid = xlen;
 2360                 auio.uio_segflg = UIO_SYSSPACE;
 2361                 auio.uio_rw = UIO_READ;
 2362                 auio.uio_procp = NULL;
 2363                 error = uvm_io(&p->p_vmspace->vm_map, &auio);
 2364                 if (error)
 2365                         goto done;
 2366 
 2367                 for (i = 0; i < xlen && nargv != 0; i++) {
 2368                         if (arg[i] == '\0')
 2369                                 nargv--;        /* one full string */
 2370                 }
 2371 
 2372                 /*
 2373                  * Make sure we don't copyout past the end of the user's
 2374                  * buffer.
 2375                  */
 2376                 if (len + i > upper_bound)
 2377                         i = upper_bound - len;
 2378 
 2379                 error = copyout(arg, (char *)oldp + len, i);
 2380                 if (error)
 2381                         break;
 2382 
 2383                 if (nargv == 0) {
 2384                         len += i;
 2385                         break;
 2386                 }
 2387         }
 2388         *oldlenp = len;
 2389 
 2390 done:
 2391         uvmspace_free(p->p_vmspace);
 2392 
 2393         free(arg, M_TEMP);
 2394         return (error);
 2395 }
 2396 
 2397 /*
 2398  * Sysctl helper routine for Verified Exec.
 2399  */
 2400 #ifdef VERIFIED_EXEC
 2401 static int
 2402 sysctl_kern_veriexec(SYSCTLFN_ARGS)
 2403 {
 2404         int newval, error;
 2405         int *var = NULL, raise_only = 0;
 2406         struct sysctlnode node;
 2407 
 2408         node = *rnode;
 2409 
 2410         switch (rnode->sysctl_num) {
 2411         case VERIEXEC_STRICT:
 2412                 raise_only = 1;
 2413                 var = &veriexec_strict;
 2414                 break;
 2415         case VERIEXEC_ALGORITHMS:
 2416                 node.sysctl_data = veriexec_fp_names;
 2417                 node.sysctl_size = strlen(veriexec_fp_names) + 1;
 2418                 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
 2419         default:
 2420                 return (EINVAL);
 2421         }
 2422 
 2423         newval = *var;
 2424 
 2425         node.sysctl_data = &newval;
 2426         error = sysctl_lookup(SYSCTLFN_CALL(&node));
 2427         if (error || newp == NULL) {
 2428                 return (error);
 2429         }
 2430 
 2431         if (raise_only && (newval < *var))
 2432                 return (EPERM);
 2433 
 2434         *var = newval;
 2435 
 2436         return (error);
 2437 }
 2438 #endif /* VERIFIED_EXEC */
 2439 
 2440 /*
 2441  * sysctl helper routine for hw.usermem and hw.usermem64.  values are
 2442  * calculate on the fly taking into account integer overflow and the
 2443  * current wired count.
 2444  */
 2445 static int
 2446 sysctl_hw_usermem(SYSCTLFN_ARGS)
 2447 {
 2448         u_int ui;
 2449         u_quad_t uq;
 2450         struct sysctlnode node;
 2451 
 2452         node = *rnode;
 2453         switch (rnode->sysctl_num) {
 2454             case HW_USERMEM:
 2455                 if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
 2456                         ui = UINT_MAX;
 2457                 else
 2458                         ui *= PAGE_SIZE;
 2459                 node.sysctl_data = &ui;
 2460                 break;
 2461         case HW_USERMEM64:
 2462                 uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
 2463                 node.sysctl_data = &uq;
 2464                 break;
 2465         default:
 2466                 return (EINVAL);
 2467         }
 2468 
 2469         return (sysctl_lookup(SYSCTLFN_CALL(&node)));
 2470 }
 2471 
 2472 /*
 2473  * sysctl helper routine for kern.cnmagic node.  pulls the old value
 2474  * out, encoded, and stuffs the new value in for decoding.
 2475  */
 2476 static int
 2477 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
 2478 {
 2479         char magic[CNS_LEN];
 2480         int error;
 2481         struct sysctlnode node;
 2482 
 2483         if (oldp)
 2484                 cn_get_magic(magic, CNS_LEN);
 2485         node = *rnode;
 2486         node.sysctl_data = &magic[0];
 2487         error = sysctl_lookup(SYSCTLFN_CALL(&node));
 2488         if (error || newp == NULL)
 2489                 return (error);
 2490 
 2491         return (cn_set_magic(magic));
 2492 }
 2493 
 2494 static int
 2495 sysctl_hw_ncpu(SYSCTLFN_ARGS)
 2496 {
 2497         int ncpu;
 2498         struct sysctlnode node;
 2499 
 2500         ncpu = sysctl_ncpus();
 2501         node = *rnode;
 2502         node.sysctl_data = &ncpu;
 2503 
 2504         return (sysctl_lookup(SYSCTLFN_CALL(&node)));
 2505 }
 2506 
 2507 
 2508 /*
 2509  * ********************************************************************
 2510  * section 3: public helper routines that are used for more than one
 2511  * node
 2512  * ********************************************************************
 2513  */
 2514 
 2515 /*
 2516  * sysctl helper routine for the kern.root_device node and some ports'
 2517  * machdep.root_device nodes.
 2518  */
 2519 int
 2520 sysctl_root_device(SYSCTLFN_ARGS)
 2521 {
 2522         struct sysctlnode node;
 2523 
 2524         node = *rnode;
 2525         node.sysctl_data = root_device->dv_xname;
 2526         node.sysctl_size = strlen(root_device->dv_xname) + 1;
 2527         return (sysctl_lookup(SYSCTLFN_CALL(&node)));
 2528 }
 2529 
 2530 /*
 2531  * sysctl helper routine for kern.consdev, dependent on the current
 2532  * state of the console.  also used for machdep.console_device on some
 2533  * ports.
 2534  */
 2535 int
 2536 sysctl_consdev(SYSCTLFN_ARGS)
 2537 {
 2538         dev_t consdev;
 2539         struct sysctlnode node;
 2540 
 2541         if (cn_tab != NULL)
 2542                 consdev = cn_tab->cn_dev;
 2543         else
 2544                 consdev = NODEV;
 2545         node = *rnode;
 2546         node.sysctl_data = &consdev;
 2547         node.sysctl_size = sizeof(consdev);
 2548         return (sysctl_lookup(SYSCTLFN_CALL(&node)));
 2549 }
 2550 
 2551 /*
 2552  * ********************************************************************
 2553  * section 4: support for some helpers
 2554  * ********************************************************************
 2555  */
 2556 
 2557 /*
 2558  * Fill in a kinfo_proc2 structure for the specified process.
 2559  */
 2560 static void
 2561 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
 2562 {
 2563         struct tty *tp;
 2564         struct lwp *l;
 2565         struct timeval ut, st;
 2566 
 2567         memset(ki, 0, sizeof(*ki));
 2568 
 2569         ki->p_paddr = PTRTOUINT64(p);
 2570         ki->p_fd = PTRTOUINT64(p->p_fd);
 2571         ki->p_cwdi = PTRTOUINT64(p->p_cwdi);
 2572         ki->p_stats = PTRTOUINT64(p->p_stats);
 2573         ki->p_limit = PTRTOUINT64(p->p_limit);
 2574         ki->p_vmspace = PTRTOUINT64(p->p_vmspace);
 2575         ki->p_sigacts = PTRTOUINT64(p->p_sigacts);
 2576         ki->p_sess = PTRTOUINT64(p->p_session);
 2577         ki->p_tsess = 0;        /* may be changed if controlling tty below */
 2578         ki->p_ru = PTRTOUINT64(p->p_ru);
 2579 
 2580         ki->p_eflag = 0;
 2581         ki->p_exitsig = p->p_exitsig;
 2582         ki->p_flag = p->p_flag;
 2583 
 2584         ki->p_pid = p->p_pid;
 2585         if (p->p_pptr)
 2586                 ki->p_ppid = p->p_pptr->p_pid;
 2587         else
 2588                 ki->p_ppid = 0;
 2589         ki->p_sid = p->p_session->s_sid;
 2590         ki->p__pgid = p->p_pgrp->pg_id;
 2591 
 2592         ki->p_tpgid = NO_PGID;  /* may be changed if controlling tty below */
 2593 
 2594         ki->p_uid = p->p_ucred->cr_uid;
 2595         ki->p_ruid = p->p_cred->p_ruid;
 2596         ki->p_gid = p->p_ucred->cr_gid;
 2597         ki->p_rgid = p->p_cred->p_rgid;
 2598         ki->p_svuid = p->p_cred->p_svuid;
 2599         ki->p_svgid = p->p_cred->p_svgid;
 2600 
 2601         memcpy(ki->p_groups, p->p_cred->pc_ucred->cr_groups,
 2602             min(sizeof(ki->p_groups), sizeof(p->p_cred->pc_ucred->cr_groups)));
 2603         ki->p_ngroups = p->p_cred->pc_ucred->cr_ngroups;
 2604 
 2605         ki->p_jobc = p->p_pgrp->pg_jobc;
 2606         if ((p->p_flag & P_CONTROLT) && (tp = p->p_session->s_ttyp)) {
 2607                 ki->p_tdev = tp->t_dev;
 2608                 ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
 2609                 ki->p_tsess = PTRTOUINT64(tp->t_session);
 2610         } else {
 2611                 ki->p_tdev = NODEV;
 2612         }
 2613 
 2614         ki->p_estcpu = p->p_estcpu;
 2615         ki->p_rtime_sec = p->p_rtime.tv_sec;
 2616         ki->p_rtime_usec = p->p_rtime.tv_usec;
 2617         ki->p_cpticks = p->p_cpticks;
 2618         ki->p_pctcpu = p->p_pctcpu;
 2619 
 2620         ki->p_uticks = p->p_uticks;
 2621         ki->p_sticks = p->p_sticks;
 2622         ki->p_iticks = p->p_iticks;
 2623 
 2624         ki->p_tracep = PTRTOUINT64(p->p_tracep);
 2625         ki->p_traceflag = p->p_traceflag;
 2626 
 2627 
 2628         memcpy(&ki->p_siglist, &p->p_sigctx.ps_siglist, sizeof(ki_sigset_t));
 2629         memcpy(&ki->p_sigmask, &p->p_sigctx.ps_sigmask, sizeof(ki_sigset_t));
 2630         memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
 2631         memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
 2632 
 2633         ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
 2634         ki->p_realstat = p->p_stat;
 2635         ki->p_nice = p->p_nice;
 2636 
 2637         ki->p_xstat = p->p_xstat;
 2638         ki->p_acflag = p->p_acflag;
 2639 
 2640         strncpy(ki->p_comm, p->p_comm,
 2641             min(sizeof(ki->p_comm), sizeof(p->p_comm)));
 2642 
 2643         strncpy(ki->p_login, p->p_session->s_login,
 2644             min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
 2645 
 2646         ki->p_nlwps = p->p_nlwps;
 2647         ki->p_nrlwps = p->p_nrlwps;
 2648         ki->p_realflag = p->p_flag;
 2649 
 2650         if (p->p_stat == SIDL || P_ZOMBIE(p)) {
 2651                 ki->p_vm_rssize = 0;
 2652                 ki->p_vm_tsize = 0;
 2653                 ki->p_vm_dsize = 0;
 2654                 ki->p_vm_ssize = 0;
 2655                 l = NULL;
 2656         } else {
 2657                 struct vmspace *vm = p->p_vmspace;
 2658 
 2659                 ki->p_vm_rssize = vm_resident_count(vm);
 2660                 ki->p_vm_tsize = vm->vm_tsize;
 2661                 ki->p_vm_dsize = vm->vm_dsize;
 2662                 ki->p_vm_ssize = vm->vm_ssize;
 2663 
 2664                 /* Pick a "representative" LWP */
 2665                 l = proc_representative_lwp(p);
 2666                 ki->p_forw = PTRTOUINT64(l->l_forw);
 2667                 ki->p_back = PTRTOUINT64(l->l_back);
 2668                 ki->p_addr = PTRTOUINT64(l->l_addr);
 2669                 ki->p_stat = l->l_stat;
 2670                 ki->p_flag |= l->l_flag & P_SHARED;
 2671                 ki->p_swtime = l->l_swtime;
 2672                 ki->p_slptime = l->l_slptime;
 2673                 if (l->l_stat == LSONPROC) {
 2674                         KDASSERT(l->l_cpu != NULL);
 2675                         ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
 2676                 } else
 2677                         ki->p_schedflags = 0;
 2678                 ki->p_holdcnt = l->l_holdcnt;
 2679                 ki->p_priority = l->l_priority;
 2680                 ki->p_usrpri = l->l_usrpri;
 2681                 if (l->l_wmesg)
 2682                         strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
 2683                 ki->p_wchan = PTRTOUINT64(l->l_wchan);
 2684 
 2685         }
 2686 
 2687         if (p->p_session->s_ttyvp)
 2688                 ki->p_eflag |= EPROC_CTTY;
 2689         if (SESS_LEADER(p))
 2690                 ki->p_eflag |= EPROC_SLEADER;
 2691 
 2692         /* XXX Is this double check necessary? */
 2693         if (P_ZOMBIE(p)) {
 2694                 ki->p_uvalid = 0;
 2695         } else {
 2696                 ki->p_uvalid = 1;
 2697 
 2698                 ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
 2699                 ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
 2700 
 2701                 calcru(p, &ut, &st, 0);
 2702                 ki->p_uutime_sec = ut.tv_sec;
 2703                 ki->p_uutime_usec = ut.tv_usec;
 2704                 ki->p_ustime_sec = st.tv_sec;
 2705                 ki->p_ustime_usec = st.tv_usec;
 2706 
 2707                 ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
 2708                 ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
 2709                 ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
 2710                 ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
 2711                 ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
 2712                 ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
 2713                 ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
 2714                 ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
 2715                 ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
 2716                 ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
 2717                 ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
 2718                 ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
 2719                 ki->p_uru_nvcsw = p->p_stats->p_ru.ru_nvcsw;
 2720                 ki->p_uru_nivcsw = p->p_stats->p_ru.ru_nivcsw;
 2721 
 2722                 timeradd(&p->p_stats->p_cru.ru_utime,
 2723                          &p->p_stats->p_cru.ru_stime, &ut);
 2724                 ki->p_uctime_sec = ut.tv_sec;
 2725                 ki->p_uctime_usec = ut.tv_usec;
 2726         }
 2727 #ifdef MULTIPROCESSOR
 2728         if (l && l->l_cpu != NULL)
 2729                 ki->p_cpuid = l->l_cpu->ci_cpuid;
 2730         else
 2731 #endif
 2732                 ki->p_cpuid = KI_NOCPU;
 2733 }
 2734 
 2735 /*
 2736  * Fill in a kinfo_lwp structure for the specified lwp.
 2737  */
 2738 static void
 2739 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
 2740 {
 2741 
 2742         kl->l_forw = PTRTOUINT64(l->l_forw);
 2743         kl->l_back = PTRTOUINT64(l->l_back);
 2744         kl->l_laddr = PTRTOUINT64(l);
 2745         kl->l_addr = PTRTOUINT64(l->l_addr);
 2746         kl->l_stat = l->l_stat;
 2747         kl->l_lid = l->l_lid;
 2748         kl->l_flag = l->l_flag;
 2749 
 2750         kl->l_swtime = l->l_swtime;
 2751         kl->l_slptime = l->l_slptime;
 2752         if (l->l_stat == LSONPROC) {
 2753                 KDASSERT(l->l_cpu != NULL);
 2754                 kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
 2755         } else
 2756                 kl->l_schedflags = 0;
 2757         kl->l_holdcnt = l->l_holdcnt;
 2758         kl->l_priority = l->l_priority;
 2759         kl->l_usrpri = l->l_usrpri;
 2760         if (l->l_wmesg)
 2761                 strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
 2762         kl->l_wchan = PTRTOUINT64(l->l_wchan);
 2763 #ifdef MULTIPROCESSOR
 2764         if (l->l_cpu != NULL)
 2765                 kl->l_cpuid = l->l_cpu->ci_cpuid;
 2766         else
 2767 #endif
 2768                 kl->l_cpuid = KI_NOCPU;
 2769 }
 2770 
 2771 /*
 2772  * Fill in an eproc structure for the specified process.
 2773  */
 2774 void
 2775 fill_eproc(struct proc *p, struct eproc *ep)
 2776 {
 2777         struct tty *tp;
 2778         struct lwp *l;
 2779 
 2780         ep->e_paddr = p;
 2781         ep->e_sess = p->p_session;
 2782         ep->e_pcred = *p->p_cred;
 2783         ep->e_ucred = *p->p_ucred;
 2784         if (p->p_stat == SIDL || P_ZOMBIE(p)) {
 2785                 ep->e_vm.vm_rssize = 0;
 2786                 ep->e_vm.vm_tsize = 0;
 2787                 ep->e_vm.vm_dsize = 0;
 2788                 ep->e_vm.vm_ssize = 0;
 2789                 /* ep->e_vm.vm_pmap = XXX; */
 2790         } else {
 2791                 struct vmspace *vm = p->p_vmspace;
 2792 
 2793                 ep->e_vm.vm_rssize = vm_resident_count(vm);
 2794                 ep->e_vm.vm_tsize = vm->vm_tsize;
 2795                 ep->e_vm.vm_dsize = vm->vm_dsize;
 2796                 ep->e_vm.vm_ssize = vm->vm_ssize;
 2797 
 2798                 /* Pick a "representative" LWP */
 2799                 l = proc_representative_lwp(p);
 2800 
 2801                 if (l->l_wmesg)
 2802                         strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
 2803         }
 2804         if (p->p_pptr)
 2805                 ep->e_ppid = p->p_pptr->p_pid;
 2806         else
 2807                 ep->e_ppid = 0;
 2808         ep->e_pgid = p->p_pgrp->pg_id;
 2809         ep->e_sid = ep->e_sess->s_sid;
 2810         ep->e_jobc = p->p_pgrp->pg_jobc;
 2811         if ((p->p_flag & P_CONTROLT) &&
 2812             (tp = ep->e_sess->s_ttyp)) {
 2813                 ep->e_tdev = tp->t_dev;
 2814                 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
 2815                 ep->e_tsess = tp->t_session;
 2816         } else
 2817                 ep->e_tdev = NODEV;
 2818 
 2819         ep->e_xsize = ep->e_xrssize = 0;
 2820         ep->e_xccount = ep->e_xswrss = 0;
 2821         ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
 2822         if (SESS_LEADER(p))
 2823                 ep->e_flag |= EPROC_SLEADER;
 2824         strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
 2825 }

Cache object: ff8da081180632d4960d3f3dda9c784e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.