1df8bae1dSRodney W. Grimes /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 6df8bae1dSRodney W. Grimes * Mike Karels at Berkeley Software Design, Inc. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 9df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 10df8bae1dSRodney W. Grimes * are met: 11df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 12df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 13df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 15df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 16df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 17df8bae1dSRodney W. Grimes * must display the following acknowledgement: 18df8bae1dSRodney W. Grimes * This product includes software developed by the University of 19df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 20df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 21df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 22df8bae1dSRodney W. Grimes * without specific prior written permission. 23df8bae1dSRodney W. Grimes * 24df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34df8bae1dSRodney W. Grimes * SUCH DAMAGE. 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94 37d2d3e875SBruce Evans * $Id: kern_sysctl.c,v 1.36 1995/11/11 00:09:21 bde Exp $ 38df8bae1dSRodney W. Grimes */ 39df8bae1dSRodney W. Grimes 40df8bae1dSRodney W. Grimes /* 41df8bae1dSRodney W. Grimes * sysctl system call. 42df8bae1dSRodney W. Grimes */ 43df8bae1dSRodney W. Grimes 44df8bae1dSRodney W. Grimes #include <sys/param.h> 45df8bae1dSRodney W. Grimes #include <sys/systm.h> 46d2d3e875SBruce Evans #include <sys/sysproto.h> 47df8bae1dSRodney W. Grimes #include <sys/kernel.h> 48df8bae1dSRodney W. Grimes #include <sys/malloc.h> 49df8bae1dSRodney W. Grimes #include <sys/proc.h> 50df8bae1dSRodney W. Grimes #include <sys/file.h> 51df8bae1dSRodney W. Grimes #include <sys/vnode.h> 52df8bae1dSRodney W. Grimes #include <sys/unistd.h> 53df8bae1dSRodney W. Grimes #include <sys/buf.h> 54df8bae1dSRodney W. Grimes #include <sys/ioctl.h> 55df8bae1dSRodney W. Grimes #include <sys/tty.h> 565bb4f738SGarrett Wollman #include <sys/conf.h> 57df8bae1dSRodney W. Grimes #include <vm/vm.h> 58df8bae1dSRodney W. Grimes #include <sys/sysctl.h> 593a34a5c3SPoul-Henning Kamp #include <sys/user.h> 60df8bae1dSRodney W. Grimes 61787d58f2SPoul-Henning Kamp extern struct linker_set sysctl_; 62787d58f2SPoul-Henning Kamp 63b396cd83SPoul-Henning Kamp /* BEGIN_MIB */ 64787d58f2SPoul-Henning Kamp SYSCTL_NODE(, 0, sysctl, CTLFLAG_RW, 0, 65787d58f2SPoul-Henning Kamp "Sysctl internal magic"); 662e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_KERN, kern, CTLFLAG_RW, 0, 672e210993SPoul-Henning Kamp "High kernel, proc, limits &c"); 682e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_VM, vm, CTLFLAG_RW, 0, 692e210993SPoul-Henning Kamp "Virtual memory"); 702e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_FS, fs, CTLFLAG_RW, 0, 712e210993SPoul-Henning Kamp "File system"); 722e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_NET, net, CTLFLAG_RW, 0, 732e210993SPoul-Henning Kamp "Network, (see socket.h)"); 742e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_DEBUG, debug, CTLFLAG_RW, 0, 752e210993SPoul-Henning Kamp "Debugging"); 762e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_HW, hw, CTLFLAG_RW, 0, 772e210993SPoul-Henning Kamp "hardware"); 782e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_MACHDEP, machdep, CTLFLAG_RW, 0, 792e210993SPoul-Henning Kamp "machine dependent"); 802e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_USER, user, CTLFLAG_RW, 0, 812e210993SPoul-Henning Kamp "user-level"); 82b396cd83SPoul-Henning Kamp 832e210993SPoul-Henning Kamp SYSCTL_STRING(_kern, KERN_OSRELEASE, osrelease, CTLFLAG_RD, osrelease, 0, ""); 84b396cd83SPoul-Henning Kamp 852e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_OSREV, osrevision, CTLFLAG_RD, 0, BSD, ""); 86b396cd83SPoul-Henning Kamp 872e210993SPoul-Henning Kamp SYSCTL_STRING(_kern, KERN_VERSION, version, CTLFLAG_RD, version, 0, ""); 88b396cd83SPoul-Henning Kamp 892e210993SPoul-Henning Kamp SYSCTL_STRING(_kern, KERN_OSTYPE, ostype, CTLFLAG_RD, ostype, 0, ""); 90b396cd83SPoul-Henning Kamp 91b396cd83SPoul-Henning Kamp extern int osreldate; 922e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_OSRELDATE, osreldate, CTLFLAG_RD, &osreldate, 0, ""); 93b396cd83SPoul-Henning Kamp 942e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RD, &desiredvnodes, 0, ""); 95b396cd83SPoul-Henning Kamp 962e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_MAXPROC, maxproc, CTLFLAG_RD, &maxproc, 0, ""); 97b396cd83SPoul-Henning Kamp 98b396cd83SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_MAXPROCPERUID, maxprocperuid, 993a34a5c3SPoul-Henning Kamp CTLFLAG_RD, &maxprocperuid, 0, ""); 100b396cd83SPoul-Henning Kamp 101b396cd83SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, 1023a34a5c3SPoul-Henning Kamp CTLFLAG_RD, &maxfilesperproc, 0, ""); 103b396cd83SPoul-Henning Kamp 1042e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_ARGMAX, argmax, CTLFLAG_RD, 0, ARG_MAX, ""); 105b396cd83SPoul-Henning Kamp 1062e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_POSIX1, posix1version, CTLFLAG_RD, 0, _POSIX_VERSION, ""); 107b396cd83SPoul-Henning Kamp 1082e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_NGROUPS, ngroups, CTLFLAG_RD, 0, NGROUPS_MAX, ""); 109b396cd83SPoul-Henning Kamp 1102e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_JOB_CONTROL, job_control, CTLFLAG_RD, 0, 1, ""); 111b396cd83SPoul-Henning Kamp 1122e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW, &maxfiles, 0, ""); 113b396cd83SPoul-Henning Kamp 114b396cd83SPoul-Henning Kamp #ifdef _POSIX_SAVED_IDS 1152e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids, CTLFLAG_RD, 0, 1, ""); 116b396cd83SPoul-Henning Kamp #else 1172e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids, CTLFLAG_RD, 0, 0, ""); 118b396cd83SPoul-Henning Kamp #endif 119b396cd83SPoul-Henning Kamp 120b396cd83SPoul-Henning Kamp char kernelname[MAXPATHLEN] = "/kernel"; /* XXX bloat */ 121b396cd83SPoul-Henning Kamp 122b396cd83SPoul-Henning Kamp SYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile, 1233a34a5c3SPoul-Henning Kamp CTLFLAG_RW, kernelname, sizeof kernelname, ""); 124b396cd83SPoul-Henning Kamp 125b396cd83SPoul-Henning Kamp SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, 1263a34a5c3SPoul-Henning Kamp CTLFLAG_RW, &boottime, timeval, ""); 127b396cd83SPoul-Henning Kamp 1282e210993SPoul-Henning Kamp SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, ""); 129b396cd83SPoul-Henning Kamp 130787d58f2SPoul-Henning Kamp SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD, cpu_model, 0, ""); 131b396cd83SPoul-Henning Kamp 1322e210993SPoul-Henning Kamp SYSCTL_INT(_hw, HW_NCPU, ncpu, CTLFLAG_RD, 0, 1, ""); 133b396cd83SPoul-Henning Kamp 1342e210993SPoul-Henning Kamp SYSCTL_INT(_hw, HW_BYTEORDER, byteorder, CTLFLAG_RD, 0, BYTE_ORDER, ""); 135b396cd83SPoul-Henning Kamp 1362e210993SPoul-Henning Kamp SYSCTL_INT(_hw, HW_PAGESIZE, pagesize, CTLFLAG_RD, 0, PAGE_SIZE, ""); 137b396cd83SPoul-Henning Kamp 138b396cd83SPoul-Henning Kamp /* END_MIB */ 139b396cd83SPoul-Henning Kamp 140b396cd83SPoul-Henning Kamp extern int vfs_update_wakeup; 141b396cd83SPoul-Henning Kamp extern int vfs_update_interval; 142b396cd83SPoul-Henning Kamp static int 1433a34a5c3SPoul-Henning Kamp sysctl_kern_updateinterval SYSCTL_HANDLER_ARGS 144b396cd83SPoul-Henning Kamp { 1453a34a5c3SPoul-Henning Kamp int error = sysctl_handle_int(oidp, 1463a34a5c3SPoul-Henning Kamp oidp->oid_arg1, oidp->oid_arg2, 1473a34a5c3SPoul-Henning Kamp oldp, oldlenp, newp, newlen); 1483a34a5c3SPoul-Henning Kamp if (!error) 149b396cd83SPoul-Henning Kamp wakeup(&vfs_update_wakeup); 1503a34a5c3SPoul-Henning Kamp return error; 151b396cd83SPoul-Henning Kamp } 152b396cd83SPoul-Henning Kamp 1532e210993SPoul-Henning Kamp SYSCTL_PROC(_kern, KERN_UPDATEINTERVAL, update, CTLTYPE_INT|CTLFLAG_RW, 1543a34a5c3SPoul-Henning Kamp &vfs_update_interval, 0, sysctl_kern_updateinterval, ""); 155b396cd83SPoul-Henning Kamp 1562e210993SPoul-Henning Kamp 1572e210993SPoul-Henning Kamp char hostname[MAXHOSTNAMELEN]; 1582e210993SPoul-Henning Kamp int hostnamelen; 1592e210993SPoul-Henning Kamp static int 1602e210993SPoul-Henning Kamp sysctl_kern_hostname SYSCTL_HANDLER_ARGS 1612e210993SPoul-Henning Kamp { 1622e210993SPoul-Henning Kamp int error = sysctl_handle_string(oidp, 1632e210993SPoul-Henning Kamp oidp->oid_arg1, oidp->oid_arg2, 1642e210993SPoul-Henning Kamp oldp, oldlenp, newp, newlen); 1652e210993SPoul-Henning Kamp if (newp && (error == 0 || error == ENOMEM)) 1662e210993SPoul-Henning Kamp hostnamelen = newlen; 1672e210993SPoul-Henning Kamp return error; 1682e210993SPoul-Henning Kamp } 1692e210993SPoul-Henning Kamp 1702e210993SPoul-Henning Kamp SYSCTL_PROC(_kern, KERN_HOSTNAME, hostname, CTLTYPE_STRING|CTLFLAG_RW, 1712e210993SPoul-Henning Kamp &hostname, sizeof(hostname), sysctl_kern_hostname, ""); 1722e210993SPoul-Henning Kamp 173787d58f2SPoul-Henning Kamp static int 1743c8e79ddSBruce Evans sysctl_order_cmp(const void *a, const void *b) 175787d58f2SPoul-Henning Kamp { 1763c8e79ddSBruce Evans const struct sysctl_oid **pa, **pb; 1773c8e79ddSBruce Evans 1783c8e79ddSBruce Evans pa = (const struct sysctl_oid **)a; 1793c8e79ddSBruce Evans pb = (const struct sysctl_oid **)b; 1803c8e79ddSBruce Evans if (*pa == NULL) 1813c8e79ddSBruce Evans return (1); 1823c8e79ddSBruce Evans if (*pb == NULL) 1833c8e79ddSBruce Evans return (-1); 184787d58f2SPoul-Henning Kamp return ((*pa)->oid_number - (*pb)->oid_number); 185787d58f2SPoul-Henning Kamp } 186787d58f2SPoul-Henning Kamp 187787d58f2SPoul-Henning Kamp static void 188787d58f2SPoul-Henning Kamp sysctl_order(void *arg) 189787d58f2SPoul-Henning Kamp { 190b8da2396SPoul-Henning Kamp int j; 191787d58f2SPoul-Henning Kamp struct linker_set *l = (struct linker_set *) arg; 192787d58f2SPoul-Henning Kamp struct sysctl_oid **oidpp; 193787d58f2SPoul-Henning Kamp 194787d58f2SPoul-Henning Kamp j = l->ls_length; 195787d58f2SPoul-Henning Kamp oidpp = (struct sysctl_oid **) l->ls_items; 196787d58f2SPoul-Henning Kamp for (; j--; oidpp++) { 197787d58f2SPoul-Henning Kamp if (!*oidpp) 198787d58f2SPoul-Henning Kamp continue; 199787d58f2SPoul-Henning Kamp if ((*oidpp)->oid_arg1 == arg) { 200787d58f2SPoul-Henning Kamp *oidpp = 0; 201787d58f2SPoul-Henning Kamp continue; 202787d58f2SPoul-Henning Kamp } 203787d58f2SPoul-Henning Kamp if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) 204787d58f2SPoul-Henning Kamp if (!(*oidpp)->oid_handler) 205787d58f2SPoul-Henning Kamp sysctl_order((*oidpp)->oid_arg1); 206787d58f2SPoul-Henning Kamp } 207787d58f2SPoul-Henning Kamp qsort(l->ls_items, l->ls_length, sizeof l->ls_items[0], 208787d58f2SPoul-Henning Kamp sysctl_order_cmp); 209787d58f2SPoul-Henning Kamp } 210787d58f2SPoul-Henning Kamp 211787d58f2SPoul-Henning Kamp SYSINIT(sysctl,SI_SUB_KMEM,SI_ORDER_ANY,sysctl_order,&sysctl_); 212787d58f2SPoul-Henning Kamp 213787d58f2SPoul-Henning Kamp static void 214787d58f2SPoul-Henning Kamp sysctl_sysctl_debug_dump_node(struct linker_set *l,int i) 215787d58f2SPoul-Henning Kamp { 216787d58f2SPoul-Henning Kamp int j,k; 217787d58f2SPoul-Henning Kamp struct sysctl_oid **oidpp; 218787d58f2SPoul-Henning Kamp 219787d58f2SPoul-Henning Kamp j = l->ls_length; 220787d58f2SPoul-Henning Kamp oidpp = (struct sysctl_oid **) l->ls_items; 221787d58f2SPoul-Henning Kamp for (; j--; oidpp++) { 222787d58f2SPoul-Henning Kamp 223787d58f2SPoul-Henning Kamp if (!*oidpp) 224787d58f2SPoul-Henning Kamp continue; 225787d58f2SPoul-Henning Kamp 226787d58f2SPoul-Henning Kamp for (k=0; k<i; k++) 227787d58f2SPoul-Henning Kamp printf(" "); 228787d58f2SPoul-Henning Kamp 229787d58f2SPoul-Henning Kamp if ((*oidpp)->oid_number > 100) { 230b8da2396SPoul-Henning Kamp printf("Junk! %p # %d %s k %x a1 %p a2 %x h %p\n", 231787d58f2SPoul-Henning Kamp *oidpp, 232787d58f2SPoul-Henning Kamp (*oidpp)->oid_number, (*oidpp)->oid_name, 233787d58f2SPoul-Henning Kamp (*oidpp)->oid_kind, (*oidpp)->oid_arg1, 234787d58f2SPoul-Henning Kamp (*oidpp)->oid_arg2, (*oidpp)->oid_handler); 235787d58f2SPoul-Henning Kamp continue; 236787d58f2SPoul-Henning Kamp } 237787d58f2SPoul-Henning Kamp printf("%d %s ", (*oidpp)->oid_number, (*oidpp)->oid_name); 238787d58f2SPoul-Henning Kamp 239787d58f2SPoul-Henning Kamp printf("%c%c", 240787d58f2SPoul-Henning Kamp (*oidpp)->oid_kind & CTLFLAG_RD ? 'R':' ', 241787d58f2SPoul-Henning Kamp (*oidpp)->oid_kind & CTLFLAG_WR ? 'W':' '); 242787d58f2SPoul-Henning Kamp 243787d58f2SPoul-Henning Kamp switch ((*oidpp)->oid_kind & CTLTYPE) { 244787d58f2SPoul-Henning Kamp case CTLTYPE_NODE: 245787d58f2SPoul-Henning Kamp if ((*oidpp)->oid_handler) { 246787d58f2SPoul-Henning Kamp printf(" Node(proc)\n"); 247787d58f2SPoul-Henning Kamp } else { 248787d58f2SPoul-Henning Kamp printf(" Node\n"); 249787d58f2SPoul-Henning Kamp sysctl_sysctl_debug_dump_node( 250787d58f2SPoul-Henning Kamp (*oidpp)->oid_arg1,i+2); 251787d58f2SPoul-Henning Kamp } 252787d58f2SPoul-Henning Kamp break; 253787d58f2SPoul-Henning Kamp case CTLTYPE_INT: printf(" Int\n"); break; 254787d58f2SPoul-Henning Kamp case CTLTYPE_STRING: printf(" String\n"); break; 255787d58f2SPoul-Henning Kamp case CTLTYPE_QUAD: printf(" Quad\n"); break; 256787d58f2SPoul-Henning Kamp case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 257787d58f2SPoul-Henning Kamp default: printf("\n"); 258787d58f2SPoul-Henning Kamp } 259787d58f2SPoul-Henning Kamp 260787d58f2SPoul-Henning Kamp } 261787d58f2SPoul-Henning Kamp } 262787d58f2SPoul-Henning Kamp 263787d58f2SPoul-Henning Kamp 264787d58f2SPoul-Henning Kamp static int 265787d58f2SPoul-Henning Kamp sysctl_sysctl_debug SYSCTL_HANDLER_ARGS 266787d58f2SPoul-Henning Kamp { 267787d58f2SPoul-Henning Kamp sysctl_sysctl_debug_dump_node(&sysctl_,0); 268787d58f2SPoul-Henning Kamp return ENOENT; 269787d58f2SPoul-Henning Kamp } 270787d58f2SPoul-Henning Kamp 271787d58f2SPoul-Henning Kamp SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 272787d58f2SPoul-Henning Kamp 0, 0, sysctl_sysctl_debug, ""); 2732e210993SPoul-Henning Kamp 2742e210993SPoul-Henning Kamp char domainname[MAXHOSTNAMELEN]; 2752e210993SPoul-Henning Kamp int domainnamelen; 2762e210993SPoul-Henning Kamp static int 2772e210993SPoul-Henning Kamp sysctl_kern_domainname SYSCTL_HANDLER_ARGS 2782e210993SPoul-Henning Kamp { 2792e210993SPoul-Henning Kamp int error = sysctl_handle_string(oidp, 2802e210993SPoul-Henning Kamp oidp->oid_arg1, oidp->oid_arg2, 2812e210993SPoul-Henning Kamp oldp, oldlenp, newp, newlen); 2822e210993SPoul-Henning Kamp if (newp && (error == 0 || error == ENOMEM)) 2832e210993SPoul-Henning Kamp domainnamelen = newlen; 2842e210993SPoul-Henning Kamp return error; 2852e210993SPoul-Henning Kamp } 2862e210993SPoul-Henning Kamp 2872e210993SPoul-Henning Kamp SYSCTL_PROC(_kern, KERN_DOMAINNAME, domainname, CTLTYPE_STRING|CTLFLAG_RW, 2882e210993SPoul-Henning Kamp &domainname, sizeof(domainname), sysctl_kern_domainname, ""); 2892e210993SPoul-Henning Kamp 2902e210993SPoul-Henning Kamp long hostid; 2912e210993SPoul-Henning Kamp /* Some trouble here, if sizeof (int) != sizeof (long) */ 2922e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_HOSTID, hostid, CTLFLAG_RW, &hostid, 0, ""); 2932e210993SPoul-Henning Kamp 2943a34a5c3SPoul-Henning Kamp int 2953a34a5c3SPoul-Henning Kamp sysctl_handle_int SYSCTL_HANDLER_ARGS 296b396cd83SPoul-Henning Kamp { 297b396cd83SPoul-Henning Kamp /* If there isn't sufficient space to return */ 298b396cd83SPoul-Henning Kamp if (oldp && *oldlenp < sizeof(int)) 299b396cd83SPoul-Henning Kamp return (ENOMEM); 300b396cd83SPoul-Henning Kamp 301b396cd83SPoul-Henning Kamp /* If it is a constant, don't write */ 3023a34a5c3SPoul-Henning Kamp if (newp && !arg1) 303b396cd83SPoul-Henning Kamp return (EPERM); 304b396cd83SPoul-Henning Kamp 305b396cd83SPoul-Henning Kamp /* If we get more than an int */ 306b396cd83SPoul-Henning Kamp if (newp && newlen != sizeof(int)) 307b396cd83SPoul-Henning Kamp return (EINVAL); 308b396cd83SPoul-Henning Kamp 309b396cd83SPoul-Henning Kamp *oldlenp = sizeof(int); 3103a34a5c3SPoul-Henning Kamp if (oldp && arg1 ) 3112e210993SPoul-Henning Kamp bcopy(arg1, oldp, sizeof(int)); 312b396cd83SPoul-Henning Kamp else if (oldp) 3132e210993SPoul-Henning Kamp bcopy(&arg2, oldp, sizeof(int)); 3142e210993SPoul-Henning Kamp if (newp) 3152e210993SPoul-Henning Kamp bcopy(newp, arg1, sizeof(int)); 3162e210993SPoul-Henning Kamp return (0); 317b396cd83SPoul-Henning Kamp } 318b396cd83SPoul-Henning Kamp 3193a34a5c3SPoul-Henning Kamp int 3203a34a5c3SPoul-Henning Kamp sysctl_handle_string SYSCTL_HANDLER_ARGS 321b396cd83SPoul-Henning Kamp { 3222e210993SPoul-Henning Kamp int len, error=0; 3233a34a5c3SPoul-Henning Kamp char *str = (char *)arg1; 324b396cd83SPoul-Henning Kamp 325b396cd83SPoul-Henning Kamp len = strlen(str) + 1; 326b396cd83SPoul-Henning Kamp 327b396cd83SPoul-Henning Kamp if (oldp && *oldlenp < len) { 328b396cd83SPoul-Henning Kamp len = *oldlenp; 3292e210993SPoul-Henning Kamp error=ENOMEM; 330b396cd83SPoul-Henning Kamp } 331b396cd83SPoul-Henning Kamp 3323a34a5c3SPoul-Henning Kamp if (newp && newlen >= arg2) 333b396cd83SPoul-Henning Kamp return (EINVAL); 334b396cd83SPoul-Henning Kamp 335b396cd83SPoul-Henning Kamp if (oldp) { 336b396cd83SPoul-Henning Kamp *oldlenp = len; 3372e210993SPoul-Henning Kamp bcopy(str, oldp, len); 338b396cd83SPoul-Henning Kamp } 3392e210993SPoul-Henning Kamp 3402e210993SPoul-Henning Kamp if (newp) { 3412e210993SPoul-Henning Kamp bcopy(newp, str, newlen); 342b396cd83SPoul-Henning Kamp str[newlen] = 0; 343b396cd83SPoul-Henning Kamp } 3442e210993SPoul-Henning Kamp return (error); 345b396cd83SPoul-Henning Kamp } 346b396cd83SPoul-Henning Kamp 3473a34a5c3SPoul-Henning Kamp int 3483a34a5c3SPoul-Henning Kamp sysctl_handle_opaque SYSCTL_HANDLER_ARGS 349b396cd83SPoul-Henning Kamp { 3502e210993SPoul-Henning Kamp if (oldp && *oldlenp < arg2) 351b396cd83SPoul-Henning Kamp return (ENOMEM); 352b396cd83SPoul-Henning Kamp 3533a34a5c3SPoul-Henning Kamp if (newp && newlen != arg2) 354b396cd83SPoul-Henning Kamp return (EINVAL); 355b396cd83SPoul-Henning Kamp 356b396cd83SPoul-Henning Kamp if (oldp) { 3573a34a5c3SPoul-Henning Kamp *oldlenp = arg2; 3582e210993SPoul-Henning Kamp bcopy(arg1, oldp, arg2); 359b396cd83SPoul-Henning Kamp } 3602e210993SPoul-Henning Kamp if (newp) 3612e210993SPoul-Henning Kamp bcopy(newp, arg1, arg2); 3622e210993SPoul-Henning Kamp return (0); 363b396cd83SPoul-Henning Kamp } 364b396cd83SPoul-Henning Kamp 3653a34a5c3SPoul-Henning Kamp #ifdef DEBUG 3663a34a5c3SPoul-Henning Kamp static sysctlfn debug_sysctl; 3673a34a5c3SPoul-Henning Kamp #endif 368b396cd83SPoul-Henning Kamp 369df8bae1dSRodney W. Grimes /* 370df8bae1dSRodney W. Grimes * Locking and stats 371df8bae1dSRodney W. Grimes */ 372df8bae1dSRodney W. Grimes static struct sysctl_lock { 373df8bae1dSRodney W. Grimes int sl_lock; 374df8bae1dSRodney W. Grimes int sl_want; 375df8bae1dSRodney W. Grimes int sl_locked; 376df8bae1dSRodney W. Grimes } memlock; 377df8bae1dSRodney W. Grimes 3782e210993SPoul-Henning Kamp 3792e210993SPoul-Henning Kamp 3802e210993SPoul-Henning Kamp /* 3812e210993SPoul-Henning Kamp * Traverse our tree, and find the right node, execute whatever it points 3822e210993SPoul-Henning Kamp * at, and return the resulting error code. 3832e210993SPoul-Henning Kamp * We work entirely in kernel-space at this time. 3842e210993SPoul-Henning Kamp */ 3852e210993SPoul-Henning Kamp 3862e210993SPoul-Henning Kamp 3872e210993SPoul-Henning Kamp int 3882e210993SPoul-Henning Kamp sysctl_root SYSCTL_HANDLER_ARGS 3892e210993SPoul-Henning Kamp { 3902e210993SPoul-Henning Kamp int *name = (int *) arg1; 3912e210993SPoul-Henning Kamp int namelen = arg2; 3922e210993SPoul-Henning Kamp int indx, i, j; 3932e210993SPoul-Henning Kamp struct sysctl_oid **oidpp; 3942e210993SPoul-Henning Kamp struct linker_set *lsp = &sysctl_; 3952e210993SPoul-Henning Kamp 3962e210993SPoul-Henning Kamp j = lsp->ls_length; 3972e210993SPoul-Henning Kamp oidpp = (struct sysctl_oid **) lsp->ls_items; 3982e210993SPoul-Henning Kamp 3992e210993SPoul-Henning Kamp indx = 0; 4002e210993SPoul-Henning Kamp while (j-- && indx < CTL_MAXNAME) { 401787d58f2SPoul-Henning Kamp if (*oidpp && ((*oidpp)->oid_number == name[indx])) { 4022e210993SPoul-Henning Kamp indx++; 4032e210993SPoul-Henning Kamp if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 4042e210993SPoul-Henning Kamp if ((*oidpp)->oid_handler) 4052e210993SPoul-Henning Kamp goto found; 4062e210993SPoul-Henning Kamp if (indx == namelen) 4072e210993SPoul-Henning Kamp return ENOENT; 4082e210993SPoul-Henning Kamp lsp = (struct linker_set*)(*oidpp)->oid_arg1; 4092e210993SPoul-Henning Kamp j = lsp->ls_length; 4102e210993SPoul-Henning Kamp oidpp = (struct sysctl_oid **)lsp->ls_items; 4112e210993SPoul-Henning Kamp } else { 4122e210993SPoul-Henning Kamp if (indx != namelen) 4132e210993SPoul-Henning Kamp return EISDIR; 4142e210993SPoul-Henning Kamp goto found; 4152e210993SPoul-Henning Kamp } 4162e210993SPoul-Henning Kamp } else { 4172e210993SPoul-Henning Kamp oidpp++; 4182e210993SPoul-Henning Kamp } 4192e210993SPoul-Henning Kamp } 4202e210993SPoul-Henning Kamp return EJUSTRETURN; 4212e210993SPoul-Henning Kamp found: 4222e210993SPoul-Henning Kamp 4232e210993SPoul-Henning Kamp /* If writing isn't allowed */ 4242e210993SPoul-Henning Kamp if (newp && !((*oidpp)->oid_kind & CTLFLAG_WR)) 4252e210993SPoul-Henning Kamp return (EPERM); 4262e210993SPoul-Henning Kamp 4272e210993SPoul-Henning Kamp if (!(*oidpp)->oid_handler) 4282e210993SPoul-Henning Kamp return EINVAL; 4292e210993SPoul-Henning Kamp 4302e210993SPoul-Henning Kamp if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 4312e210993SPoul-Henning Kamp i = ((*oidpp)->oid_handler) (*oidpp, 4322e210993SPoul-Henning Kamp name + indx, namelen - indx, 4332e210993SPoul-Henning Kamp oldp, oldlenp, newp, newlen); 4342e210993SPoul-Henning Kamp } else { 4352e210993SPoul-Henning Kamp i = ((*oidpp)->oid_handler) (*oidpp, 4362e210993SPoul-Henning Kamp (*oidpp)->oid_arg1, (*oidpp)->oid_arg2, 4372e210993SPoul-Henning Kamp oldp, oldlenp, newp, newlen); 4382e210993SPoul-Henning Kamp } 4392e210993SPoul-Henning Kamp return (i); 4402e210993SPoul-Henning Kamp } 4412e210993SPoul-Henning Kamp 442d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 443b8da2396SPoul-Henning Kamp struct sysctl_args { 444b8da2396SPoul-Henning Kamp int *name; 445b8da2396SPoul-Henning Kamp u_int namelen; 446b8da2396SPoul-Henning Kamp void *old; 447b8da2396SPoul-Henning Kamp size_t *oldlenp; 448b8da2396SPoul-Henning Kamp void *new; 449b8da2396SPoul-Henning Kamp size_t newlen; 450b8da2396SPoul-Henning Kamp }; 451d2d3e875SBruce Evans #endif 452b8da2396SPoul-Henning Kamp 453df8bae1dSRodney W. Grimes int 454df8bae1dSRodney W. Grimes __sysctl(p, uap, retval) 455df8bae1dSRodney W. Grimes struct proc *p; 456df8bae1dSRodney W. Grimes register struct sysctl_args *uap; 457df8bae1dSRodney W. Grimes int *retval; 458df8bae1dSRodney W. Grimes { 459b8da2396SPoul-Henning Kamp int error, name[CTL_MAXNAME]; 460b396cd83SPoul-Henning Kamp 461df8bae1dSRodney W. Grimes if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 462df8bae1dSRodney W. Grimes return (EINVAL); 463b396cd83SPoul-Henning Kamp 464797f2d22SPoul-Henning Kamp error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 465797f2d22SPoul-Henning Kamp if (error) 466df8bae1dSRodney W. Grimes return (error); 467df8bae1dSRodney W. Grimes 468b8da2396SPoul-Henning Kamp return (userland_sysctl(p, name, uap->namelen, 469b8da2396SPoul-Henning Kamp uap->old, uap->oldlenp, 0, 470b8da2396SPoul-Henning Kamp uap->new, uap->newlen, retval)); 471b8da2396SPoul-Henning Kamp } 472b8da2396SPoul-Henning Kamp 4736ff1bbebSPoul-Henning Kamp static sysctlfn kern_sysctl; 4746ff1bbebSPoul-Henning Kamp 475b8da2396SPoul-Henning Kamp /* 476b8da2396SPoul-Henning Kamp * This is used from various compatibility syscalls too. That's why name 477b8da2396SPoul-Henning Kamp * must be in kernel space. 478b8da2396SPoul-Henning Kamp */ 479b8da2396SPoul-Henning Kamp int 480b8da2396SPoul-Henning Kamp userland_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, int *retval) 481b8da2396SPoul-Henning Kamp { 482b8da2396SPoul-Henning Kamp int error = 0, dolock = 1, i; 483b8da2396SPoul-Henning Kamp u_int savelen = 0, oldlen = 0; 484b8da2396SPoul-Henning Kamp sysctlfn *fn; 485b8da2396SPoul-Henning Kamp void *oldp = 0; 486b8da2396SPoul-Henning Kamp void *newp = 0; 487b8da2396SPoul-Henning Kamp 488b8da2396SPoul-Henning Kamp if (new != NULL && (error = suser(p->p_ucred, &p->p_acflag))) 489b396cd83SPoul-Henning Kamp return (error); 490b396cd83SPoul-Henning Kamp 491b8da2396SPoul-Henning Kamp if (oldlenp) { 492b8da2396SPoul-Henning Kamp if (inkernel) { 493b8da2396SPoul-Henning Kamp oldlen = *oldlenp; 494b8da2396SPoul-Henning Kamp } else { 495b8da2396SPoul-Henning Kamp error = copyin(oldlenp, &oldlen, sizeof(oldlen)); 496b8da2396SPoul-Henning Kamp if (error) 497b8da2396SPoul-Henning Kamp return (error); 498b8da2396SPoul-Henning Kamp } 499b8da2396SPoul-Henning Kamp } 500b8da2396SPoul-Henning Kamp 501b8da2396SPoul-Henning Kamp if (old) 5022e210993SPoul-Henning Kamp oldp = malloc(oldlen, M_TEMP, M_WAITOK); 5032e210993SPoul-Henning Kamp 504b8da2396SPoul-Henning Kamp if (newlen) { 505b8da2396SPoul-Henning Kamp newp = malloc(newlen, M_TEMP, M_WAITOK); 506b8da2396SPoul-Henning Kamp error = copyin(new, newp, newlen); 507b396cd83SPoul-Henning Kamp } 5082e210993SPoul-Henning Kamp if (error) { 5092e210993SPoul-Henning Kamp if (oldp) 5102e210993SPoul-Henning Kamp free(oldp, M_TEMP); 5112e210993SPoul-Henning Kamp if (newp) 5122e210993SPoul-Henning Kamp free(newp, M_TEMP); 5132e210993SPoul-Henning Kamp return error; 514b396cd83SPoul-Henning Kamp } 515b396cd83SPoul-Henning Kamp 51669feb388SPoul-Henning Kamp error = sysctl_root(0, name, namelen, oldp, &oldlen, newp, newlen); 517b396cd83SPoul-Henning Kamp 5182e210993SPoul-Henning Kamp if (!error || error == ENOMEM) { 51969feb388SPoul-Henning Kamp if (retval) 52069feb388SPoul-Henning Kamp *retval = oldlen; 521b8da2396SPoul-Henning Kamp if (oldlenp) { 522b8da2396SPoul-Henning Kamp if (inkernel) { 523b8da2396SPoul-Henning Kamp *oldlenp = oldlen; 524b8da2396SPoul-Henning Kamp } else { 525b8da2396SPoul-Henning Kamp i = copyout(&oldlen, oldlenp, sizeof(oldlen)); 526b396cd83SPoul-Henning Kamp if (i) 527b396cd83SPoul-Henning Kamp error = i; 5282e210993SPoul-Henning Kamp } 529b8da2396SPoul-Henning Kamp } 5302e210993SPoul-Henning Kamp if ((error == ENOMEM || !error ) && oldp) { 531b8da2396SPoul-Henning Kamp i = copyout(oldp, old, oldlen); 5322e210993SPoul-Henning Kamp if (i) 5332e210993SPoul-Henning Kamp error = i; 5342e210993SPoul-Henning Kamp free(oldp, M_TEMP); 5352e210993SPoul-Henning Kamp } 5362e210993SPoul-Henning Kamp if (newp) 5372e210993SPoul-Henning Kamp free(newp, M_TEMP); 5382e210993SPoul-Henning Kamp return (error); 5392e210993SPoul-Henning Kamp } 540b396cd83SPoul-Henning Kamp 5412e210993SPoul-Henning Kamp if (oldp) 5422e210993SPoul-Henning Kamp free(oldp, M_TEMP); 5432e210993SPoul-Henning Kamp if (newp) 5442e210993SPoul-Henning Kamp free(newp, M_TEMP); 5452e210993SPoul-Henning Kamp 546df8bae1dSRodney W. Grimes switch (name[0]) { 547df8bae1dSRodney W. Grimes case CTL_KERN: 548df8bae1dSRodney W. Grimes fn = kern_sysctl; 549e9e2a852SAndrey A. Chernov if (name[1] != KERN_VNODE) /* XXX */ 550df8bae1dSRodney W. Grimes dolock = 0; 551df8bae1dSRodney W. Grimes break; 552df8bae1dSRodney W. Grimes case CTL_HW: 553df8bae1dSRodney W. Grimes fn = hw_sysctl; 554df8bae1dSRodney W. Grimes break; 555df8bae1dSRodney W. Grimes case CTL_VM: 556df8bae1dSRodney W. Grimes fn = vm_sysctl; 557df8bae1dSRodney W. Grimes break; 558df8bae1dSRodney W. Grimes case CTL_NET: 559df8bae1dSRodney W. Grimes fn = net_sysctl; 560df8bae1dSRodney W. Grimes break; 561df8bae1dSRodney W. Grimes case CTL_FS: 562df8bae1dSRodney W. Grimes fn = fs_sysctl; 563df8bae1dSRodney W. Grimes break; 564df8bae1dSRodney W. Grimes #ifdef DEBUG 565df8bae1dSRodney W. Grimes case CTL_DEBUG: 566df8bae1dSRodney W. Grimes fn = debug_sysctl; 567df8bae1dSRodney W. Grimes break; 568df8bae1dSRodney W. Grimes #endif 569df8bae1dSRodney W. Grimes default: 570df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 571df8bae1dSRodney W. Grimes } 572b8da2396SPoul-Henning Kamp if (old != NULL) { 573b8da2396SPoul-Henning Kamp if (!useracc(old, oldlen, B_WRITE)) 574df8bae1dSRodney W. Grimes return (EFAULT); 575df8bae1dSRodney W. Grimes while (memlock.sl_lock) { 576df8bae1dSRodney W. Grimes memlock.sl_want = 1; 57782478919SDavid Greenman (void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0); 578df8bae1dSRodney W. Grimes memlock.sl_locked++; 579df8bae1dSRodney W. Grimes } 580df8bae1dSRodney W. Grimes memlock.sl_lock = 1; 581df8bae1dSRodney W. Grimes if (dolock) 582b8da2396SPoul-Henning Kamp vslock(old, oldlen); 583df8bae1dSRodney W. Grimes savelen = oldlen; 584df8bae1dSRodney W. Grimes } 585b396cd83SPoul-Henning Kamp 586b396cd83SPoul-Henning Kamp 587b8da2396SPoul-Henning Kamp error = (*fn)(name + 1, namelen - 1, old, &oldlen, 588b8da2396SPoul-Henning Kamp new, newlen, p); 5892e210993SPoul-Henning Kamp 5902e210993SPoul-Henning Kamp 591b8da2396SPoul-Henning Kamp if (old != NULL) { 592df8bae1dSRodney W. Grimes if (dolock) 593b8da2396SPoul-Henning Kamp vsunlock(old, savelen, B_WRITE); 594df8bae1dSRodney W. Grimes memlock.sl_lock = 0; 595df8bae1dSRodney W. Grimes if (memlock.sl_want) { 596df8bae1dSRodney W. Grimes memlock.sl_want = 0; 597df8bae1dSRodney W. Grimes wakeup((caddr_t)&memlock); 598df8bae1dSRodney W. Grimes } 599df8bae1dSRodney W. Grimes } 600787d58f2SPoul-Henning Kamp #if 0 601787d58f2SPoul-Henning Kamp if (error) { 602787d58f2SPoul-Henning Kamp printf("SYSCTL_ERROR: "); 603b8da2396SPoul-Henning Kamp for(i=0;i<namelen;i++) 604787d58f2SPoul-Henning Kamp printf("%d ", name[i]); 605787d58f2SPoul-Henning Kamp printf("= %d\n", error); 606787d58f2SPoul-Henning Kamp } 607787d58f2SPoul-Henning Kamp #endif 608df8bae1dSRodney W. Grimes if (error) 609df8bae1dSRodney W. Grimes return (error); 610b8da2396SPoul-Henning Kamp if (retval) 611df8bae1dSRodney W. Grimes *retval = oldlen; 612b8da2396SPoul-Henning Kamp if (oldlenp) { 613b8da2396SPoul-Henning Kamp if (inkernel) { 614b8da2396SPoul-Henning Kamp *oldlenp = oldlen; 615b8da2396SPoul-Henning Kamp } else { 616b8da2396SPoul-Henning Kamp error = copyout(&oldlen, oldlenp, sizeof(oldlen)); 617b8da2396SPoul-Henning Kamp } 618b8da2396SPoul-Henning Kamp } 619b8da2396SPoul-Henning Kamp return (error); 620df8bae1dSRodney W. Grimes } 621df8bae1dSRodney W. Grimes 622df8bae1dSRodney W. Grimes /* 623df8bae1dSRodney W. Grimes * Attributes stored in the kernel. 624df8bae1dSRodney W. Grimes */ 6256ae6a09bSGarrett Wollman int securelevel = -1; 626df8bae1dSRodney W. Grimes 627df8bae1dSRodney W. Grimes /* 628df8bae1dSRodney W. Grimes * kernel related system variables. 629df8bae1dSRodney W. Grimes */ 6303c8e79ddSBruce Evans static int 631df8bae1dSRodney W. Grimes kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 632df8bae1dSRodney W. Grimes int *name; 633df8bae1dSRodney W. Grimes u_int namelen; 634df8bae1dSRodney W. Grimes void *oldp; 635df8bae1dSRodney W. Grimes size_t *oldlenp; 636df8bae1dSRodney W. Grimes void *newp; 637df8bae1dSRodney W. Grimes size_t newlen; 638df8bae1dSRodney W. Grimes struct proc *p; 639df8bae1dSRodney W. Grimes { 6402e210993SPoul-Henning Kamp int error, level; 6415bb4f738SGarrett Wollman dev_t ndumpdev; 642df8bae1dSRodney W. Grimes 643df8bae1dSRodney W. Grimes /* all sysctl names at this level are terminal */ 6443f31c649SGarrett Wollman if (namelen != 1 && !(name[0] == KERN_PROC || name[0] == KERN_PROF 6453f31c649SGarrett Wollman || name[0] == KERN_NTP_PLL)) 646df8bae1dSRodney W. Grimes return (ENOTDIR); /* overloaded */ 647df8bae1dSRodney W. Grimes 648df8bae1dSRodney W. Grimes switch (name[0]) { 649b396cd83SPoul-Henning Kamp 650df8bae1dSRodney W. Grimes case KERN_SECURELVL: 651df8bae1dSRodney W. Grimes level = securelevel; 652df8bae1dSRodney W. Grimes if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) || 653df8bae1dSRodney W. Grimes newp == NULL) 654df8bae1dSRodney W. Grimes return (error); 655df8bae1dSRodney W. Grimes if (level < securelevel && p->p_pid != 1) 656df8bae1dSRodney W. Grimes return (EPERM); 657df8bae1dSRodney W. Grimes securelevel = level; 658df8bae1dSRodney W. Grimes return (0); 659df8bae1dSRodney W. Grimes case KERN_VNODE: 660df8bae1dSRodney W. Grimes return (sysctl_vnode(oldp, oldlenp)); 661df8bae1dSRodney W. Grimes case KERN_PROC: 662df8bae1dSRodney W. Grimes return (sysctl_doproc(name + 1, namelen - 1, oldp, oldlenp)); 663df8bae1dSRodney W. Grimes case KERN_FILE: 664df8bae1dSRodney W. Grimes return (sysctl_file(oldp, oldlenp)); 665df8bae1dSRodney W. Grimes #ifdef GPROF 666df8bae1dSRodney W. Grimes case KERN_PROF: 667df8bae1dSRodney W. Grimes return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp, 668df8bae1dSRodney W. Grimes newp, newlen)); 669df8bae1dSRodney W. Grimes #endif 6703f31c649SGarrett Wollman case KERN_NTP_PLL: 6713f31c649SGarrett Wollman return (ntp_sysctl(name + 1, namelen - 1, oldp, oldlenp, 6723f31c649SGarrett Wollman newp, newlen, p)); 6735bb4f738SGarrett Wollman case KERN_DUMPDEV: 6745bb4f738SGarrett Wollman ndumpdev = dumpdev; 6755bb4f738SGarrett Wollman error = sysctl_struct(oldp, oldlenp, newp, newlen, &ndumpdev, 6765bb4f738SGarrett Wollman sizeof ndumpdev); 6775bb4f738SGarrett Wollman if (!error && ndumpdev != dumpdev) { 6785bb4f738SGarrett Wollman error = setdumpdev(ndumpdev); 6795bb4f738SGarrett Wollman } 6805bb4f738SGarrett Wollman return error; 681df8bae1dSRodney W. Grimes default: 682df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 683df8bae1dSRodney W. Grimes } 684df8bae1dSRodney W. Grimes /* NOTREACHED */ 685df8bae1dSRodney W. Grimes } 686df8bae1dSRodney W. Grimes 687df8bae1dSRodney W. Grimes /* 688df8bae1dSRodney W. Grimes * hardware related system variables. 689df8bae1dSRodney W. Grimes */ 69026f9a767SRodney W. Grimes int 691df8bae1dSRodney W. Grimes hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 692df8bae1dSRodney W. Grimes int *name; 693df8bae1dSRodney W. Grimes u_int namelen; 694df8bae1dSRodney W. Grimes void *oldp; 695df8bae1dSRodney W. Grimes size_t *oldlenp; 696df8bae1dSRodney W. Grimes void *newp; 697df8bae1dSRodney W. Grimes size_t newlen; 698df8bae1dSRodney W. Grimes struct proc *p; 699df8bae1dSRodney W. Grimes { 7008478cabaSGarrett Wollman /* almost all sysctl names at this level are terminal */ 7018478cabaSGarrett Wollman if (namelen != 1 && name[0] != HW_DEVCONF) 702df8bae1dSRodney W. Grimes return (ENOTDIR); /* overloaded */ 703df8bae1dSRodney W. Grimes 704df8bae1dSRodney W. Grimes switch (name[0]) { 705df8bae1dSRodney W. Grimes case HW_PHYSMEM: 706df8bae1dSRodney W. Grimes return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem))); 707df8bae1dSRodney W. Grimes case HW_USERMEM: 708df8bae1dSRodney W. Grimes return (sysctl_rdint(oldp, oldlenp, newp, 709df8bae1dSRodney W. Grimes ctob(physmem - cnt.v_wire_count))); 7108478cabaSGarrett Wollman case HW_DEVCONF: 7118478cabaSGarrett Wollman return (dev_sysctl(name + 1, namelen - 1, oldp, oldlenp, 7128478cabaSGarrett Wollman newp, newlen, p)); 713df8bae1dSRodney W. Grimes default: 714df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 715df8bae1dSRodney W. Grimes } 716df8bae1dSRodney W. Grimes /* NOTREACHED */ 717df8bae1dSRodney W. Grimes } 718df8bae1dSRodney W. Grimes 719df8bae1dSRodney W. Grimes #ifdef DEBUG 720df8bae1dSRodney W. Grimes /* 721df8bae1dSRodney W. Grimes * Debugging related system variables. 722df8bae1dSRodney W. Grimes */ 723df8bae1dSRodney W. Grimes struct ctldebug debug0, debug1, debug2, debug3, debug4; 724df8bae1dSRodney W. Grimes struct ctldebug debug5, debug6, debug7, debug8, debug9; 725df8bae1dSRodney W. Grimes struct ctldebug debug10, debug11, debug12, debug13, debug14; 726df8bae1dSRodney W. Grimes struct ctldebug debug15, debug16, debug17, debug18, debug19; 727df8bae1dSRodney W. Grimes static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = { 728df8bae1dSRodney W. Grimes &debug0, &debug1, &debug2, &debug3, &debug4, 729df8bae1dSRodney W. Grimes &debug5, &debug6, &debug7, &debug8, &debug9, 730df8bae1dSRodney W. Grimes &debug10, &debug11, &debug12, &debug13, &debug14, 731df8bae1dSRodney W. Grimes &debug15, &debug16, &debug17, &debug18, &debug19, 732df8bae1dSRodney W. Grimes }; 7336124ac44SBruce Evans static int 734df8bae1dSRodney W. Grimes debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 735df8bae1dSRodney W. Grimes int *name; 736df8bae1dSRodney W. Grimes u_int namelen; 737df8bae1dSRodney W. Grimes void *oldp; 738df8bae1dSRodney W. Grimes size_t *oldlenp; 739df8bae1dSRodney W. Grimes void *newp; 740df8bae1dSRodney W. Grimes size_t newlen; 741df8bae1dSRodney W. Grimes struct proc *p; 742df8bae1dSRodney W. Grimes { 743df8bae1dSRodney W. Grimes struct ctldebug *cdp; 744df8bae1dSRodney W. Grimes 745df8bae1dSRodney W. Grimes /* all sysctl names at this level are name and field */ 746df8bae1dSRodney W. Grimes if (namelen != 2) 747df8bae1dSRodney W. Grimes return (ENOTDIR); /* overloaded */ 748df8bae1dSRodney W. Grimes cdp = debugvars[name[0]]; 749df8bae1dSRodney W. Grimes if (cdp->debugname == 0) 750df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 751df8bae1dSRodney W. Grimes switch (name[1]) { 752df8bae1dSRodney W. Grimes case CTL_DEBUG_NAME: 753df8bae1dSRodney W. Grimes return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname)); 754df8bae1dSRodney W. Grimes case CTL_DEBUG_VALUE: 755df8bae1dSRodney W. Grimes return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar)); 756df8bae1dSRodney W. Grimes default: 757df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 758df8bae1dSRodney W. Grimes } 759df8bae1dSRodney W. Grimes /* NOTREACHED */ 760df8bae1dSRodney W. Grimes } 761df8bae1dSRodney W. Grimes #endif /* DEBUG */ 762df8bae1dSRodney W. Grimes 763df8bae1dSRodney W. Grimes /* 764df8bae1dSRodney W. Grimes * Validate parameters and get old / set new parameters 765df8bae1dSRodney W. Grimes * for an integer-valued sysctl function. 766df8bae1dSRodney W. Grimes */ 76726f9a767SRodney W. Grimes int 768df8bae1dSRodney W. Grimes sysctl_int(oldp, oldlenp, newp, newlen, valp) 769df8bae1dSRodney W. Grimes void *oldp; 770df8bae1dSRodney W. Grimes size_t *oldlenp; 771df8bae1dSRodney W. Grimes void *newp; 772df8bae1dSRodney W. Grimes size_t newlen; 773df8bae1dSRodney W. Grimes int *valp; 774df8bae1dSRodney W. Grimes { 775df8bae1dSRodney W. Grimes int error = 0; 776df8bae1dSRodney W. Grimes 777df8bae1dSRodney W. Grimes if (oldp && *oldlenp < sizeof(int)) 778df8bae1dSRodney W. Grimes return (ENOMEM); 779df8bae1dSRodney W. Grimes if (newp && newlen != sizeof(int)) 780df8bae1dSRodney W. Grimes return (EINVAL); 781df8bae1dSRodney W. Grimes *oldlenp = sizeof(int); 782df8bae1dSRodney W. Grimes if (oldp) 783df8bae1dSRodney W. Grimes error = copyout(valp, oldp, sizeof(int)); 784df8bae1dSRodney W. Grimes if (error == 0 && newp) 785df8bae1dSRodney W. Grimes error = copyin(newp, valp, sizeof(int)); 786df8bae1dSRodney W. Grimes return (error); 787df8bae1dSRodney W. Grimes } 788df8bae1dSRodney W. Grimes 789df8bae1dSRodney W. Grimes /* 790df8bae1dSRodney W. Grimes * As above, but read-only. 791df8bae1dSRodney W. Grimes */ 79226f9a767SRodney W. Grimes int 793df8bae1dSRodney W. Grimes sysctl_rdint(oldp, oldlenp, newp, val) 794df8bae1dSRodney W. Grimes void *oldp; 795df8bae1dSRodney W. Grimes size_t *oldlenp; 796df8bae1dSRodney W. Grimes void *newp; 797df8bae1dSRodney W. Grimes int val; 798df8bae1dSRodney W. Grimes { 799df8bae1dSRodney W. Grimes int error = 0; 800df8bae1dSRodney W. Grimes 801df8bae1dSRodney W. Grimes if (oldp && *oldlenp < sizeof(int)) 802df8bae1dSRodney W. Grimes return (ENOMEM); 803df8bae1dSRodney W. Grimes if (newp) 804df8bae1dSRodney W. Grimes return (EPERM); 805df8bae1dSRodney W. Grimes *oldlenp = sizeof(int); 806df8bae1dSRodney W. Grimes if (oldp) 807df8bae1dSRodney W. Grimes error = copyout((caddr_t)&val, oldp, sizeof(int)); 808df8bae1dSRodney W. Grimes return (error); 809df8bae1dSRodney W. Grimes } 810df8bae1dSRodney W. Grimes 811df8bae1dSRodney W. Grimes /* 812df8bae1dSRodney W. Grimes * Validate parameters and get old / set new parameters 813df8bae1dSRodney W. Grimes * for a string-valued sysctl function. 814df8bae1dSRodney W. Grimes */ 81526f9a767SRodney W. Grimes int 816df8bae1dSRodney W. Grimes sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen) 817df8bae1dSRodney W. Grimes void *oldp; 818df8bae1dSRodney W. Grimes size_t *oldlenp; 819df8bae1dSRodney W. Grimes void *newp; 820df8bae1dSRodney W. Grimes size_t newlen; 821df8bae1dSRodney W. Grimes char *str; 822df8bae1dSRodney W. Grimes int maxlen; 823df8bae1dSRodney W. Grimes { 824e1453736SMike Pritchard int len, error = 0, rval = 0; 825df8bae1dSRodney W. Grimes 826df8bae1dSRodney W. Grimes len = strlen(str) + 1; 827e1453736SMike Pritchard if (oldp && *oldlenp < len) { 828f81fcd41SGuido van Rooij len = *oldlenp; 829e1453736SMike Pritchard rval = ENOMEM; 830e1453736SMike Pritchard } 831df8bae1dSRodney W. Grimes if (newp && newlen >= maxlen) 832df8bae1dSRodney W. Grimes return (EINVAL); 833df8bae1dSRodney W. Grimes if (oldp) { 834df8bae1dSRodney W. Grimes *oldlenp = len; 835df8bae1dSRodney W. Grimes error = copyout(str, oldp, len); 836e1453736SMike Pritchard if (error) 837e1453736SMike Pritchard rval = error; 838df8bae1dSRodney W. Grimes } 839e1453736SMike Pritchard if ((error == 0 || error == ENOMEM) && newp) { 840df8bae1dSRodney W. Grimes error = copyin(newp, str, newlen); 841e1453736SMike Pritchard if (error) 842e1453736SMike Pritchard rval = error; 843df8bae1dSRodney W. Grimes str[newlen] = 0; 844df8bae1dSRodney W. Grimes } 845e1453736SMike Pritchard return (rval); 846df8bae1dSRodney W. Grimes } 847df8bae1dSRodney W. Grimes 848df8bae1dSRodney W. Grimes /* 849df8bae1dSRodney W. Grimes * As above, but read-only. 850df8bae1dSRodney W. Grimes */ 85126f9a767SRodney W. Grimes int 852df8bae1dSRodney W. Grimes sysctl_rdstring(oldp, oldlenp, newp, str) 853df8bae1dSRodney W. Grimes void *oldp; 854df8bae1dSRodney W. Grimes size_t *oldlenp; 855df8bae1dSRodney W. Grimes void *newp; 856df8bae1dSRodney W. Grimes char *str; 857df8bae1dSRodney W. Grimes { 858e1453736SMike Pritchard int len, error = 0, rval = 0; 859df8bae1dSRodney W. Grimes 860df8bae1dSRodney W. Grimes len = strlen(str) + 1; 861e1453736SMike Pritchard if (oldp && *oldlenp < len) { 862e1453736SMike Pritchard len = *oldlenp; 863e1453736SMike Pritchard rval = ENOMEM; 864e1453736SMike Pritchard } 865df8bae1dSRodney W. Grimes if (newp) 866df8bae1dSRodney W. Grimes return (EPERM); 867df8bae1dSRodney W. Grimes *oldlenp = len; 868df8bae1dSRodney W. Grimes if (oldp) 869df8bae1dSRodney W. Grimes error = copyout(str, oldp, len); 870e1453736SMike Pritchard if (error) 871e1453736SMike Pritchard rval = error; 872e1453736SMike Pritchard return (rval); 873df8bae1dSRodney W. Grimes } 874df8bae1dSRodney W. Grimes 875df8bae1dSRodney W. Grimes /* 876df8bae1dSRodney W. Grimes * Validate parameters and get old / set new parameters 877df8bae1dSRodney W. Grimes * for a structure oriented sysctl function. 878df8bae1dSRodney W. Grimes */ 87926f9a767SRodney W. Grimes int 880df8bae1dSRodney W. Grimes sysctl_struct(oldp, oldlenp, newp, newlen, sp, len) 881df8bae1dSRodney W. Grimes void *oldp; 882df8bae1dSRodney W. Grimes size_t *oldlenp; 883df8bae1dSRodney W. Grimes void *newp; 884df8bae1dSRodney W. Grimes size_t newlen; 885df8bae1dSRodney W. Grimes void *sp; 886df8bae1dSRodney W. Grimes int len; 887df8bae1dSRodney W. Grimes { 888df8bae1dSRodney W. Grimes int error = 0; 889df8bae1dSRodney W. Grimes 890df8bae1dSRodney W. Grimes if (oldp && *oldlenp < len) 891df8bae1dSRodney W. Grimes return (ENOMEM); 892df8bae1dSRodney W. Grimes if (newp && newlen > len) 893df8bae1dSRodney W. Grimes return (EINVAL); 894df8bae1dSRodney W. Grimes if (oldp) { 895df8bae1dSRodney W. Grimes *oldlenp = len; 896df8bae1dSRodney W. Grimes error = copyout(sp, oldp, len); 897df8bae1dSRodney W. Grimes } 898df8bae1dSRodney W. Grimes if (error == 0 && newp) 899df8bae1dSRodney W. Grimes error = copyin(newp, sp, len); 900df8bae1dSRodney W. Grimes return (error); 901df8bae1dSRodney W. Grimes } 902df8bae1dSRodney W. Grimes 903df8bae1dSRodney W. Grimes /* 904df8bae1dSRodney W. Grimes * Validate parameters and get old parameters 905df8bae1dSRodney W. Grimes * for a structure oriented sysctl function. 906df8bae1dSRodney W. Grimes */ 90726f9a767SRodney W. Grimes int 908df8bae1dSRodney W. Grimes sysctl_rdstruct(oldp, oldlenp, newp, sp, len) 909df8bae1dSRodney W. Grimes void *oldp; 910df8bae1dSRodney W. Grimes size_t *oldlenp; 911df8bae1dSRodney W. Grimes void *newp, *sp; 912df8bae1dSRodney W. Grimes int len; 913df8bae1dSRodney W. Grimes { 914df8bae1dSRodney W. Grimes int error = 0; 915df8bae1dSRodney W. Grimes 916df8bae1dSRodney W. Grimes if (oldp && *oldlenp < len) 917df8bae1dSRodney W. Grimes return (ENOMEM); 918df8bae1dSRodney W. Grimes if (newp) 919df8bae1dSRodney W. Grimes return (EPERM); 920df8bae1dSRodney W. Grimes *oldlenp = len; 921df8bae1dSRodney W. Grimes if (oldp) 922df8bae1dSRodney W. Grimes error = copyout(sp, oldp, len); 923df8bae1dSRodney W. Grimes return (error); 924df8bae1dSRodney W. Grimes } 925df8bae1dSRodney W. Grimes 926df8bae1dSRodney W. Grimes /* 927df8bae1dSRodney W. Grimes * Get file structures. 928df8bae1dSRodney W. Grimes */ 92926f9a767SRodney W. Grimes int 930df8bae1dSRodney W. Grimes sysctl_file(where, sizep) 931df8bae1dSRodney W. Grimes char *where; 932df8bae1dSRodney W. Grimes size_t *sizep; 933df8bae1dSRodney W. Grimes { 934df8bae1dSRodney W. Grimes int buflen, error; 935df8bae1dSRodney W. Grimes struct file *fp; 936df8bae1dSRodney W. Grimes char *start = where; 937df8bae1dSRodney W. Grimes 938df8bae1dSRodney W. Grimes buflen = *sizep; 939df8bae1dSRodney W. Grimes if (where == NULL) { 940df8bae1dSRodney W. Grimes /* 941df8bae1dSRodney W. Grimes * overestimate by 10 files 942df8bae1dSRodney W. Grimes */ 943df8bae1dSRodney W. Grimes *sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file); 944df8bae1dSRodney W. Grimes return (0); 945df8bae1dSRodney W. Grimes } 946df8bae1dSRodney W. Grimes 947df8bae1dSRodney W. Grimes /* 948df8bae1dSRodney W. Grimes * first copyout filehead 949df8bae1dSRodney W. Grimes */ 950df8bae1dSRodney W. Grimes if (buflen < sizeof(filehead)) { 951df8bae1dSRodney W. Grimes *sizep = 0; 952df8bae1dSRodney W. Grimes return (0); 953df8bae1dSRodney W. Grimes } 954797f2d22SPoul-Henning Kamp error = copyout((caddr_t)&filehead, where, sizeof(filehead)); 955797f2d22SPoul-Henning Kamp if (error) 956df8bae1dSRodney W. Grimes return (error); 957df8bae1dSRodney W. Grimes buflen -= sizeof(filehead); 958df8bae1dSRodney W. Grimes where += sizeof(filehead); 959df8bae1dSRodney W. Grimes 960df8bae1dSRodney W. Grimes /* 961df8bae1dSRodney W. Grimes * followed by an array of file structures 962df8bae1dSRodney W. Grimes */ 963df8bae1dSRodney W. Grimes for (fp = filehead; fp != NULL; fp = fp->f_filef) { 964df8bae1dSRodney W. Grimes if (buflen < sizeof(struct file)) { 965df8bae1dSRodney W. Grimes *sizep = where - start; 966df8bae1dSRodney W. Grimes return (ENOMEM); 967df8bae1dSRodney W. Grimes } 968797f2d22SPoul-Henning Kamp error = copyout((caddr_t)fp, where, sizeof (struct file)); 969797f2d22SPoul-Henning Kamp if (error) 970df8bae1dSRodney W. Grimes return (error); 971df8bae1dSRodney W. Grimes buflen -= sizeof(struct file); 972df8bae1dSRodney W. Grimes where += sizeof(struct file); 973df8bae1dSRodney W. Grimes } 974df8bae1dSRodney W. Grimes *sizep = where - start; 975df8bae1dSRodney W. Grimes return (0); 976df8bae1dSRodney W. Grimes } 977df8bae1dSRodney W. Grimes 978df8bae1dSRodney W. Grimes /* 979df8bae1dSRodney W. Grimes * try over estimating by 5 procs 980df8bae1dSRodney W. Grimes */ 981df8bae1dSRodney W. Grimes #define KERN_PROCSLOP (5 * sizeof (struct kinfo_proc)) 982df8bae1dSRodney W. Grimes 98326f9a767SRodney W. Grimes int 984df8bae1dSRodney W. Grimes sysctl_doproc(name, namelen, where, sizep) 985df8bae1dSRodney W. Grimes int *name; 986df8bae1dSRodney W. Grimes u_int namelen; 987df8bae1dSRodney W. Grimes char *where; 988df8bae1dSRodney W. Grimes size_t *sizep; 989df8bae1dSRodney W. Grimes { 990df8bae1dSRodney W. Grimes register struct proc *p; 991df8bae1dSRodney W. Grimes register struct kinfo_proc *dp = (struct kinfo_proc *)where; 992df8bae1dSRodney W. Grimes register int needed = 0; 993df8bae1dSRodney W. Grimes int buflen = where != NULL ? *sizep : 0; 994df8bae1dSRodney W. Grimes int doingzomb; 995df8bae1dSRodney W. Grimes struct eproc eproc; 996df8bae1dSRodney W. Grimes int error = 0; 997df8bae1dSRodney W. Grimes 998df8bae1dSRodney W. Grimes if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL)) 999df8bae1dSRodney W. Grimes return (EINVAL); 1000df8bae1dSRodney W. Grimes p = (struct proc *)allproc; 1001df8bae1dSRodney W. Grimes doingzomb = 0; 1002df8bae1dSRodney W. Grimes again: 1003df8bae1dSRodney W. Grimes for (; p != NULL; p = p->p_next) { 1004df8bae1dSRodney W. Grimes /* 1005df8bae1dSRodney W. Grimes * Skip embryonic processes. 1006df8bae1dSRodney W. Grimes */ 1007df8bae1dSRodney W. Grimes if (p->p_stat == SIDL) 1008df8bae1dSRodney W. Grimes continue; 1009df8bae1dSRodney W. Grimes /* 1010df8bae1dSRodney W. Grimes * TODO - make more efficient (see notes below). 1011df8bae1dSRodney W. Grimes * do by session. 1012df8bae1dSRodney W. Grimes */ 1013df8bae1dSRodney W. Grimes switch (name[0]) { 1014df8bae1dSRodney W. Grimes 1015df8bae1dSRodney W. Grimes case KERN_PROC_PID: 1016df8bae1dSRodney W. Grimes /* could do this with just a lookup */ 1017df8bae1dSRodney W. Grimes if (p->p_pid != (pid_t)name[1]) 1018df8bae1dSRodney W. Grimes continue; 1019df8bae1dSRodney W. Grimes break; 1020df8bae1dSRodney W. Grimes 1021df8bae1dSRodney W. Grimes case KERN_PROC_PGRP: 1022df8bae1dSRodney W. Grimes /* could do this by traversing pgrp */ 1023be6a1d14SDavid Greenman if (p->p_pgrp == NULL || p->p_pgrp->pg_id != (pid_t)name[1]) 1024df8bae1dSRodney W. Grimes continue; 1025df8bae1dSRodney W. Grimes break; 1026df8bae1dSRodney W. Grimes 1027df8bae1dSRodney W. Grimes case KERN_PROC_TTY: 1028df8bae1dSRodney W. Grimes if ((p->p_flag & P_CONTROLT) == 0 || 1029be6a1d14SDavid Greenman p->p_session == NULL || 1030df8bae1dSRodney W. Grimes p->p_session->s_ttyp == NULL || 1031df8bae1dSRodney W. Grimes p->p_session->s_ttyp->t_dev != (dev_t)name[1]) 1032df8bae1dSRodney W. Grimes continue; 1033df8bae1dSRodney W. Grimes break; 1034df8bae1dSRodney W. Grimes 1035df8bae1dSRodney W. Grimes case KERN_PROC_UID: 1036be6a1d14SDavid Greenman if (p->p_ucred == NULL || p->p_ucred->cr_uid != (uid_t)name[1]) 1037df8bae1dSRodney W. Grimes continue; 1038df8bae1dSRodney W. Grimes break; 1039df8bae1dSRodney W. Grimes 1040df8bae1dSRodney W. Grimes case KERN_PROC_RUID: 1041be6a1d14SDavid Greenman if (p->p_ucred == NULL || p->p_cred->p_ruid != (uid_t)name[1]) 1042df8bae1dSRodney W. Grimes continue; 1043df8bae1dSRodney W. Grimes break; 1044df8bae1dSRodney W. Grimes } 1045df8bae1dSRodney W. Grimes if (buflen >= sizeof(struct kinfo_proc)) { 1046df8bae1dSRodney W. Grimes fill_eproc(p, &eproc); 1047797f2d22SPoul-Henning Kamp error = copyout((caddr_t)p, &dp->kp_proc, 1048797f2d22SPoul-Henning Kamp sizeof(struct proc)); 1049797f2d22SPoul-Henning Kamp if (error) 1050df8bae1dSRodney W. Grimes return (error); 1051797f2d22SPoul-Henning Kamp error = copyout((caddr_t)&eproc, &dp->kp_eproc, 1052797f2d22SPoul-Henning Kamp sizeof(eproc)); 1053797f2d22SPoul-Henning Kamp if (error) 1054df8bae1dSRodney W. Grimes return (error); 1055df8bae1dSRodney W. Grimes dp++; 1056df8bae1dSRodney W. Grimes buflen -= sizeof(struct kinfo_proc); 1057df8bae1dSRodney W. Grimes } 1058df8bae1dSRodney W. Grimes needed += sizeof(struct kinfo_proc); 1059df8bae1dSRodney W. Grimes } 1060df8bae1dSRodney W. Grimes if (doingzomb == 0) { 1061df8bae1dSRodney W. Grimes p = zombproc; 1062df8bae1dSRodney W. Grimes doingzomb++; 1063df8bae1dSRodney W. Grimes goto again; 1064df8bae1dSRodney W. Grimes } 1065df8bae1dSRodney W. Grimes if (where != NULL) { 1066df8bae1dSRodney W. Grimes *sizep = (caddr_t)dp - where; 1067df8bae1dSRodney W. Grimes if (needed > *sizep) 1068df8bae1dSRodney W. Grimes return (ENOMEM); 1069df8bae1dSRodney W. Grimes } else { 1070df8bae1dSRodney W. Grimes needed += KERN_PROCSLOP; 1071df8bae1dSRodney W. Grimes *sizep = needed; 1072df8bae1dSRodney W. Grimes } 1073df8bae1dSRodney W. Grimes return (0); 1074df8bae1dSRodney W. Grimes } 1075df8bae1dSRodney W. Grimes 1076df8bae1dSRodney W. Grimes /* 1077df8bae1dSRodney W. Grimes * Fill in an eproc structure for the specified process. 1078df8bae1dSRodney W. Grimes */ 1079df8bae1dSRodney W. Grimes void 1080df8bae1dSRodney W. Grimes fill_eproc(p, ep) 1081df8bae1dSRodney W. Grimes register struct proc *p; 1082df8bae1dSRodney W. Grimes register struct eproc *ep; 1083df8bae1dSRodney W. Grimes { 1084df8bae1dSRodney W. Grimes register struct tty *tp; 1085df8bae1dSRodney W. Grimes 1086be6a1d14SDavid Greenman bzero(ep, sizeof(*ep)); 1087be6a1d14SDavid Greenman 1088df8bae1dSRodney W. Grimes ep->e_paddr = p; 10891c8fc26cSDavid Greenman if (p->p_cred) { 1090df8bae1dSRodney W. Grimes ep->e_pcred = *p->p_cred; 1091be6a1d14SDavid Greenman if (p->p_ucred) 1092df8bae1dSRodney W. Grimes ep->e_ucred = *p->p_ucred; 10931c8fc26cSDavid Greenman } 10941c8fc26cSDavid Greenman if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) { 1095df8bae1dSRodney W. Grimes register struct vmspace *vm = p->p_vmspace; 1096df8bae1dSRodney W. Grimes 1097df8bae1dSRodney W. Grimes #ifdef pmap_resident_count 1098df8bae1dSRodney W. Grimes ep->e_vm.vm_rssize = pmap_resident_count(&vm->vm_pmap); /*XXX*/ 1099df8bae1dSRodney W. Grimes #else 1100df8bae1dSRodney W. Grimes ep->e_vm.vm_rssize = vm->vm_rssize; 1101df8bae1dSRodney W. Grimes #endif 1102df8bae1dSRodney W. Grimes ep->e_vm.vm_tsize = vm->vm_tsize; 1103df8bae1dSRodney W. Grimes ep->e_vm.vm_dsize = vm->vm_dsize; 1104df8bae1dSRodney W. Grimes ep->e_vm.vm_ssize = vm->vm_ssize; 1105df8bae1dSRodney W. Grimes #ifndef sparc 1106df8bae1dSRodney W. Grimes ep->e_vm.vm_pmap = vm->vm_pmap; 1107df8bae1dSRodney W. Grimes #endif 1108df8bae1dSRodney W. Grimes } 1109df8bae1dSRodney W. Grimes if (p->p_pptr) 1110df8bae1dSRodney W. Grimes ep->e_ppid = p->p_pptr->p_pid; 1111be6a1d14SDavid Greenman if (p->p_pgrp) { 1112be6a1d14SDavid Greenman ep->e_sess = p->p_pgrp->pg_session; 1113df8bae1dSRodney W. Grimes ep->e_pgid = p->p_pgrp->pg_id; 1114df8bae1dSRodney W. Grimes ep->e_jobc = p->p_pgrp->pg_jobc; 1115be6a1d14SDavid Greenman } 1116df8bae1dSRodney W. Grimes if ((p->p_flag & P_CONTROLT) && 1117be6a1d14SDavid Greenman (ep->e_sess != NULL) && 1118be6a1d14SDavid Greenman ((tp = ep->e_sess->s_ttyp) != NULL)) { 1119df8bae1dSRodney W. Grimes ep->e_tdev = tp->t_dev; 1120df8bae1dSRodney W. Grimes ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 1121df8bae1dSRodney W. Grimes ep->e_tsess = tp->t_session; 1122df8bae1dSRodney W. Grimes } else 1123df8bae1dSRodney W. Grimes ep->e_tdev = NODEV; 1124be6a1d14SDavid Greenman if (ep->e_sess && ep->e_sess->s_ttyvp) 1125be6a1d14SDavid Greenman ep->e_flag = EPROC_CTTY; 1126df8bae1dSRodney W. Grimes if (SESS_LEADER(p)) 1127df8bae1dSRodney W. Grimes ep->e_flag |= EPROC_SLEADER; 1128be6a1d14SDavid Greenman if (p->p_wmesg) { 1129df8bae1dSRodney W. Grimes strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN); 1130be6a1d14SDavid Greenman ep->e_wmesg[WMESGLEN] = 0; 1131be6a1d14SDavid Greenman } 1132df8bae1dSRodney W. Grimes } 1133df8bae1dSRodney W. Grimes 1134df8bae1dSRodney W. Grimes #ifdef COMPAT_43 1135df8bae1dSRodney W. Grimes #include <sys/socket.h> 1136df8bae1dSRodney W. Grimes #define KINFO_PROC (0<<8) 1137df8bae1dSRodney W. Grimes #define KINFO_RT (1<<8) 1138df8bae1dSRodney W. Grimes #define KINFO_VNODE (2<<8) 1139df8bae1dSRodney W. Grimes #define KINFO_FILE (3<<8) 1140df8bae1dSRodney W. Grimes #define KINFO_METER (4<<8) 1141df8bae1dSRodney W. Grimes #define KINFO_LOADAVG (5<<8) 1142df8bae1dSRodney W. Grimes #define KINFO_CLOCKRATE (6<<8) 1143df8bae1dSRodney W. Grimes 11446ece4a51SPeter Wemm /* Non-standard BSDI extension - only present on their 4.3 net-2 releases */ 11456ece4a51SPeter Wemm #define KINFO_BSDI_SYSINFO (101<<8) 11466ece4a51SPeter Wemm 11476ece4a51SPeter Wemm /* 11486ece4a51SPeter Wemm * XXX this is bloat, but I hope it's better here than on the potentially 11496ece4a51SPeter Wemm * limited kernel stack... -Peter 11506ece4a51SPeter Wemm */ 11516ece4a51SPeter Wemm 11526ece4a51SPeter Wemm struct { 11536ece4a51SPeter Wemm int bsdi_machine; /* "i386" on BSD/386 */ 11546ece4a51SPeter Wemm /* ^^^ this is an offset to the string, relative to the struct start */ 11556ece4a51SPeter Wemm char *pad0; 11566ece4a51SPeter Wemm long pad1; 11576ece4a51SPeter Wemm long pad2; 11586ece4a51SPeter Wemm long pad3; 11596ece4a51SPeter Wemm u_long pad4; 11606ece4a51SPeter Wemm u_long pad5; 11616ece4a51SPeter Wemm u_long pad6; 11626ece4a51SPeter Wemm 11636ece4a51SPeter Wemm int bsdi_ostype; /* "BSD/386" on BSD/386 */ 11646ece4a51SPeter Wemm int bsdi_osrelease; /* "1.1" on BSD/386 */ 11656ece4a51SPeter Wemm long pad7; 11666ece4a51SPeter Wemm long pad8; 11676ece4a51SPeter Wemm char *pad9; 11686ece4a51SPeter Wemm 11696ece4a51SPeter Wemm long pad10; 11706ece4a51SPeter Wemm long pad11; 11716ece4a51SPeter Wemm int pad12; 11726ece4a51SPeter Wemm long pad13; 11736ece4a51SPeter Wemm quad_t pad14; 11746ece4a51SPeter Wemm long pad15; 11756ece4a51SPeter Wemm 11766ece4a51SPeter Wemm struct timeval pad16; 11776ece4a51SPeter Wemm /* we dont set this, because BSDI's uname used gethostname() instead */ 11786ece4a51SPeter Wemm int bsdi_hostname; /* hostname on BSD/386 */ 11796ece4a51SPeter Wemm 11806ece4a51SPeter Wemm /* the actual string data is appended here */ 11816ece4a51SPeter Wemm 11826ece4a51SPeter Wemm } bsdi_si; 11836ece4a51SPeter Wemm /* 11846ece4a51SPeter Wemm * this data is appended to the end of the bsdi_si structure during copyout. 11856ece4a51SPeter Wemm * The "char *" offsets are relative to the base of the bsdi_si struct. 11866ece4a51SPeter Wemm * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings 11876ece4a51SPeter Wemm * should not exceed the length of the buffer here... (or else!! :-) 11886ece4a51SPeter Wemm */ 11896ece4a51SPeter Wemm char bsdi_strings[80]; /* It had better be less than this! */ 11906ece4a51SPeter Wemm 1191d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1192df8bae1dSRodney W. Grimes struct getkerninfo_args { 1193df8bae1dSRodney W. Grimes int op; 1194df8bae1dSRodney W. Grimes char *where; 1195df8bae1dSRodney W. Grimes int *size; 1196df8bae1dSRodney W. Grimes int arg; 1197df8bae1dSRodney W. Grimes }; 1198d2d3e875SBruce Evans #endif 1199df8bae1dSRodney W. Grimes 120026f9a767SRodney W. Grimes int 1201df8bae1dSRodney W. Grimes ogetkerninfo(p, uap, retval) 1202df8bae1dSRodney W. Grimes struct proc *p; 1203df8bae1dSRodney W. Grimes register struct getkerninfo_args *uap; 1204df8bae1dSRodney W. Grimes int *retval; 1205df8bae1dSRodney W. Grimes { 1206b8da2396SPoul-Henning Kamp int error, name[6]; 1207df8bae1dSRodney W. Grimes u_int size; 1208df8bae1dSRodney W. Grimes 1209df8bae1dSRodney W. Grimes switch (uap->op & 0xff00) { 1210df8bae1dSRodney W. Grimes 1211df8bae1dSRodney W. Grimes case KINFO_RT: 1212b8da2396SPoul-Henning Kamp name[0] = CTL_NET; 1213b8da2396SPoul-Henning Kamp name[1] = PF_ROUTE; 1214b8da2396SPoul-Henning Kamp name[2] = 0; 1215b8da2396SPoul-Henning Kamp name[3] = (uap->op & 0xff0000) >> 16; 1216b8da2396SPoul-Henning Kamp name[4] = uap->op & 0xff; 1217b8da2396SPoul-Henning Kamp name[5] = uap->arg; 1218b8da2396SPoul-Henning Kamp error = userland_sysctl(p, name, 6, uap->where, uap->size, 1219b8da2396SPoul-Henning Kamp 0, 0, 0, 0); 1220df8bae1dSRodney W. Grimes break; 1221df8bae1dSRodney W. Grimes 1222df8bae1dSRodney W. Grimes case KINFO_VNODE: 1223b8da2396SPoul-Henning Kamp name[0] = CTL_KERN; 1224b8da2396SPoul-Henning Kamp name[1] = KERN_VNODE; 1225b8da2396SPoul-Henning Kamp error = userland_sysctl(p, name, 2, uap->where, uap->size, 1226b8da2396SPoul-Henning Kamp 0, 0, 0, 0); 1227df8bae1dSRodney W. Grimes break; 1228df8bae1dSRodney W. Grimes 1229df8bae1dSRodney W. Grimes case KINFO_PROC: 1230b8da2396SPoul-Henning Kamp name[0] = CTL_KERN; 1231b8da2396SPoul-Henning Kamp name[1] = KERN_PROC; 1232b8da2396SPoul-Henning Kamp name[2] = uap->op & 0xff; 1233b8da2396SPoul-Henning Kamp name[3] = uap->arg; 1234b8da2396SPoul-Henning Kamp error = userland_sysctl(p, name, 4, uap->where, uap->size, 1235b8da2396SPoul-Henning Kamp 0, 0, 0, 0); 1236df8bae1dSRodney W. Grimes break; 1237df8bae1dSRodney W. Grimes 1238df8bae1dSRodney W. Grimes case KINFO_FILE: 1239b8da2396SPoul-Henning Kamp name[0] = CTL_KERN; 1240b8da2396SPoul-Henning Kamp name[1] = KERN_FILE; 1241b8da2396SPoul-Henning Kamp error = userland_sysctl(p, name, 2, uap->where, uap->size, 1242b8da2396SPoul-Henning Kamp 0, 0, 0, 0); 1243df8bae1dSRodney W. Grimes break; 1244df8bae1dSRodney W. Grimes 1245df8bae1dSRodney W. Grimes case KINFO_METER: 1246b8da2396SPoul-Henning Kamp name[0] = CTL_VM; 1247b8da2396SPoul-Henning Kamp name[1] = VM_METER; 1248b8da2396SPoul-Henning Kamp error = userland_sysctl(p, name, 2, uap->where, uap->size, 1249b8da2396SPoul-Henning Kamp 0, 0, 0, 0); 1250df8bae1dSRodney W. Grimes break; 1251df8bae1dSRodney W. Grimes 1252df8bae1dSRodney W. Grimes case KINFO_LOADAVG: 1253b8da2396SPoul-Henning Kamp name[0] = CTL_VM; 1254b8da2396SPoul-Henning Kamp name[1] = VM_LOADAVG; 1255b8da2396SPoul-Henning Kamp error = userland_sysctl(p, name, 2, uap->where, uap->size, 1256b8da2396SPoul-Henning Kamp 0, 0, 0, 0); 1257df8bae1dSRodney W. Grimes break; 1258df8bae1dSRodney W. Grimes 1259df8bae1dSRodney W. Grimes case KINFO_CLOCKRATE: 1260b8da2396SPoul-Henning Kamp name[0] = CTL_KERN; 1261b8da2396SPoul-Henning Kamp name[1] = KERN_CLOCKRATE; 1262b8da2396SPoul-Henning Kamp error = userland_sysctl(p, name, 2, uap->where, uap->size, 1263b8da2396SPoul-Henning Kamp 0, 0, 0, 0); 1264df8bae1dSRodney W. Grimes break; 1265df8bae1dSRodney W. Grimes 12666ece4a51SPeter Wemm case KINFO_BSDI_SYSINFO: { 12676ece4a51SPeter Wemm /* 12686ece4a51SPeter Wemm * this is pretty crude, but it's just enough for uname() 12696ece4a51SPeter Wemm * from BSDI's 1.x libc to work. 12706ece4a51SPeter Wemm * 12716ece4a51SPeter Wemm * In particular, it doesn't return the same results when 12726ece4a51SPeter Wemm * the supplied buffer is too small. BSDI's version apparently 12736ece4a51SPeter Wemm * will return the amount copied, and set the *size to how 12746ece4a51SPeter Wemm * much was needed. The emulation framework here isn't capable 12756ece4a51SPeter Wemm * of that, so we just set both to the amount copied. 12766ece4a51SPeter Wemm * BSDI's 2.x product apparently fails with ENOMEM in this 12776ece4a51SPeter Wemm * scenario. 12786ece4a51SPeter Wemm */ 12796ece4a51SPeter Wemm 12806ece4a51SPeter Wemm u_int needed; 12816ece4a51SPeter Wemm u_int left; 12826ece4a51SPeter Wemm char *s; 12836ece4a51SPeter Wemm 12846ece4a51SPeter Wemm bzero((char *)&bsdi_si, sizeof(bsdi_si)); 12856ece4a51SPeter Wemm bzero(bsdi_strings, sizeof(bsdi_strings)); 12866ece4a51SPeter Wemm 12876ece4a51SPeter Wemm s = bsdi_strings; 12886ece4a51SPeter Wemm 12896ece4a51SPeter Wemm bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si); 12906ece4a51SPeter Wemm strcpy(s, ostype); 12916ece4a51SPeter Wemm s += strlen(s) + 1; 12926ece4a51SPeter Wemm 12936ece4a51SPeter Wemm bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si); 12946ece4a51SPeter Wemm strcpy(s, osrelease); 12956ece4a51SPeter Wemm s += strlen(s) + 1; 12966ece4a51SPeter Wemm 12976ece4a51SPeter Wemm bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si); 12986ece4a51SPeter Wemm strcpy(s, machine); 12996ece4a51SPeter Wemm s += strlen(s) + 1; 13006ece4a51SPeter Wemm 13016ece4a51SPeter Wemm needed = sizeof(bsdi_si) + (s - bsdi_strings); 13026ece4a51SPeter Wemm 13036ece4a51SPeter Wemm if (uap->where == NULL) { 13046ece4a51SPeter Wemm /* process is asking how much buffer to supply.. */ 13056ece4a51SPeter Wemm size = needed; 13066ece4a51SPeter Wemm error = 0; 13076ece4a51SPeter Wemm break; 13086ece4a51SPeter Wemm } 13096ece4a51SPeter Wemm 13106ece4a51SPeter Wemm 13116ece4a51SPeter Wemm /* if too much buffer supplied, trim it down */ 13126ece4a51SPeter Wemm if (size > needed) 13136ece4a51SPeter Wemm size = needed; 13146ece4a51SPeter Wemm 13156ece4a51SPeter Wemm /* how much of the buffer is remaining */ 13166ece4a51SPeter Wemm left = size; 13176ece4a51SPeter Wemm 13186ece4a51SPeter Wemm if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0) 13196ece4a51SPeter Wemm break; 13206ece4a51SPeter Wemm 13216ece4a51SPeter Wemm /* is there any point in continuing? */ 13226ece4a51SPeter Wemm if (left > sizeof(bsdi_si)) { 13236ece4a51SPeter Wemm left -= sizeof(bsdi_si); 13246ece4a51SPeter Wemm error = copyout(&bsdi_strings, 13256ece4a51SPeter Wemm uap->where + sizeof(bsdi_si), left); 13266ece4a51SPeter Wemm } 13276ece4a51SPeter Wemm break; 13286ece4a51SPeter Wemm } 13296ece4a51SPeter Wemm 1330df8bae1dSRodney W. Grimes default: 1331df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 1332df8bae1dSRodney W. Grimes } 1333df8bae1dSRodney W. Grimes if (error) 1334df8bae1dSRodney W. Grimes return (error); 1335df8bae1dSRodney W. Grimes *retval = size; 1336df8bae1dSRodney W. Grimes if (uap->size) 1337df8bae1dSRodney W. Grimes error = copyout((caddr_t)&size, (caddr_t)uap->size, 1338df8bae1dSRodney W. Grimes sizeof(size)); 1339df8bae1dSRodney W. Grimes return (error); 1340df8bae1dSRodney W. Grimes } 1341df8bae1dSRodney W. Grimes #endif /* COMPAT_43 */ 1342