Home | History | Annotate | Line # | Download | only in sysctl
sysctl.c revision 1.162
      1  1.162     kamil /*	$NetBSD: sysctl.c,v 1.162 2019/08/18 04:10:22 kamil Exp $ */
      2   1.75    atatat 
      3   1.75    atatat /*-
      4   1.75    atatat  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5   1.75    atatat  *	All rights reserved.
      6   1.75    atatat  *
      7   1.75    atatat  * This code is derived from software contributed to The NetBSD Foundation
      8   1.75    atatat  * by Andrew Brown.
      9   1.75    atatat  *
     10   1.75    atatat  * Redistribution and use in source and binary forms, with or without
     11   1.75    atatat  * modification, are permitted provided that the following conditions
     12   1.75    atatat  * are met:
     13   1.75    atatat  * 1. Redistributions of source code must retain the above copyright
     14   1.75    atatat  *    notice, this list of conditions and the following disclaimer.
     15   1.75    atatat  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.75    atatat  *    notice, this list of conditions and the following disclaimer in the
     17   1.75    atatat  *    documentation and/or other materials provided with the distribution.
     18   1.75    atatat  *
     19   1.75    atatat  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.75    atatat  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.75    atatat  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.75    atatat  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.75    atatat  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.75    atatat  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.75    atatat  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.75    atatat  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.75    atatat  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.75    atatat  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.75    atatat  * POSSIBILITY OF SUCH DAMAGE.
     30   1.75    atatat  */
     31    1.9   thorpej 
     32    1.1       cgd /*
     33    1.1       cgd  * Copyright (c) 1993
     34    1.1       cgd  *	The Regents of the University of California.  All rights reserved.
     35    1.1       cgd  *
     36    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     37    1.1       cgd  * modification, are permitted provided that the following conditions
     38    1.1       cgd  * are met:
     39    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     40    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     41    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     42    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     43    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     44   1.71       agc  * 3. Neither the name of the University nor the names of its contributors
     45    1.1       cgd  *    may be used to endorse or promote products derived from this software
     46    1.1       cgd  *    without specific prior written permission.
     47    1.1       cgd  *
     48    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58    1.1       cgd  * SUCH DAMAGE.
     59    1.1       cgd  */
     60    1.1       cgd 
     61   1.14  christos #include <sys/cdefs.h>
     62    1.1       cgd #ifndef lint
     63  1.124     lukem __COPYRIGHT("@(#) Copyright (c) 1993\
     64  1.124     lukem  The Regents of the University of California.  All rights reserved.");
     65    1.1       cgd #endif /* not lint */
     66    1.1       cgd 
     67    1.1       cgd #ifndef lint
     68    1.9   thorpej #if 0
     69    1.9   thorpej static char sccsid[] = "@(#)sysctl.c	8.1 (Berkeley) 6/6/93";
     70    1.9   thorpej #else
     71  1.162     kamil __RCSID("$NetBSD: sysctl.c,v 1.162 2019/08/18 04:10:22 kamil Exp $");
     72    1.9   thorpej #endif
     73    1.1       cgd #endif /* not lint */
     74    1.1       cgd 
     75   1.75    atatat #include <sys/types.h>
     76    1.1       cgd #include <sys/param.h>
     77    1.1       cgd #include <sys/sysctl.h>
     78   1.17   thorpej #include <sys/mount.h>
     79   1.22    bouyer #include <sys/resource.h>
     80   1.75    atatat #include <sys/stat.h>
     81   1.75    atatat #include <sys/sched.h>
     82   1.75    atatat #include <sys/socket.h>
     83  1.146  christos #include <sys/bitops.h>
     84    1.1       cgd #include <netinet/in.h>
     85    1.1       cgd #include <netinet/ip_var.h>
     86    1.8   thorpej #include <netinet/tcp.h>
     87    1.8   thorpej #include <netinet/tcp_timer.h>
     88    1.8   thorpej #include <netinet/tcp_var.h>
     89   1.24    itojun #include <netinet/icmp6.h>
     90   1.75    atatat #include <nfs/rpcv2.h>
     91   1.75    atatat #include <nfs/nfsproto.h>
     92   1.75    atatat #include <nfs/nfs.h>
     93   1.75    atatat #include <machine/cpu.h>
     94   1.20    itojun 
     95   1.75    atatat #include <assert.h>
     96   1.75    atatat #include <ctype.h>
     97   1.14  christos #include <err.h>
     98    1.1       cgd #include <errno.h>
     99   1.75    atatat #include <inttypes.h>
    100   1.98    atatat #include <regex.h>
    101   1.75    atatat #include <stdarg.h>
    102    1.1       cgd #include <stdio.h>
    103    1.1       cgd #include <stdlib.h>
    104    1.1       cgd #include <string.h>
    105   1.75    atatat #include <time.h>
    106   1.15      tron #include <unistd.h>
    107    1.1       cgd 
    108  1.133     pooka #include "prog_ops.h"
    109  1.132     pooka 
    110   1.75    atatat /*
    111   1.75    atatat  * this needs to be able to do the printing and the setting
    112   1.75    atatat  */
    113   1.75    atatat #define HANDLER_PROTO const char *, const char *, char *, \
    114   1.75    atatat 	int *, u_int, const struct sysctlnode *, \
    115   1.75    atatat 	u_int, void *
    116   1.75    atatat #define HANDLER_ARGS const char *sname, const char *dname, char *value, \
    117   1.75    atatat 	int *name, u_int namelen, const struct sysctlnode *pnode, \
    118   1.75    atatat 	u_int type, void *v
    119   1.75    atatat #define DISPLAY_VALUE	0
    120   1.75    atatat #define DISPLAY_OLD	1
    121   1.75    atatat #define DISPLAY_NEW	2
    122   1.75    atatat 
    123   1.75    atatat /*
    124   1.75    atatat  * generic routines
    125   1.75    atatat  */
    126  1.141  christos static const struct handlespec *findhandler(const char *, regex_t *, size_t *);
    127   1.98    atatat static void canonicalize(const char *, char *);
    128  1.100    atatat static void purge_tree(struct sysctlnode *);
    129  1.134  christos static void print_tree(int *, u_int, struct sysctlnode *, u_int, int, regex_t *,
    130  1.134  christos     size_t *);
    131  1.158  dholland static void write_number(int *, u_int, struct sysctlnode *, char *, bool);
    132  1.158  dholland static void write_string(int *, u_int, struct sysctlnode *, char *, bool);
    133   1.75    atatat static void display_number(const struct sysctlnode *, const char *,
    134   1.75    atatat 			   const void *, size_t, int);
    135   1.75    atatat static void display_string(const struct sysctlnode *, const char *,
    136   1.75    atatat 			   const void *, size_t, int);
    137   1.75    atatat static void display_struct(const struct sysctlnode *, const char *,
    138   1.75    atatat 			   const void *, size_t, int);
    139   1.75    atatat static void hex_dump(const unsigned char *, size_t);
    140  1.138     joerg __dead static void usage(void);
    141  1.134  christos static void parse(char *, regex_t *, size_t *);
    142   1.82    atatat static void parse_create(char *);
    143   1.82    atatat static void parse_destroy(char *);
    144   1.84    atatat static void parse_describe(char *);
    145   1.84    atatat static void getdesc1(int *, u_int, struct sysctlnode *);
    146   1.84    atatat static void getdesc(int *, u_int, struct sysctlnode *);
    147   1.92    atatat static void trim_whitespace(char *, int);
    148   1.75    atatat static void sysctlerror(int);
    149   1.92    atatat static void sysctlparseerror(u_int, const char *);
    150  1.142     joerg static void sysctlperror(const char *, ...) __printflike(1, 2);
    151   1.92    atatat #define EXIT(n) do { \
    152   1.92    atatat 	if (fn == NULL) exit(n); else return; } while (/*CONSTCOND*/0)
    153   1.56  christos 
    154   1.75    atatat /*
    155   1.85    atatat  * "borrowed" from libc:sysctlgetmibinfo.c
    156   1.75    atatat  */
    157   1.85    atatat int __learn_tree(int *, u_int, struct sysctlnode *);
    158   1.22    bouyer 
    159   1.75    atatat /*
    160   1.75    atatat  * "handlers"
    161   1.75    atatat  */
    162   1.75    atatat static void printother(HANDLER_PROTO);
    163   1.75    atatat static void kern_clockrate(HANDLER_PROTO);
    164   1.75    atatat static void kern_boottime(HANDLER_PROTO);
    165   1.75    atatat static void kern_consdev(HANDLER_PROTO);
    166   1.75    atatat static void kern_cp_time(HANDLER_PROTO);
    167  1.103  christos static void kern_cp_id(HANDLER_PROTO);
    168  1.118  christos static void kern_drivers(HANDLER_PROTO);
    169   1.75    atatat static void vm_loadavg(HANDLER_PROTO);
    170   1.75    atatat static void proc_limit(HANDLER_PROTO);
    171   1.75    atatat #ifdef CPU_DISKINFO
    172   1.75    atatat static void machdep_diskinfo(HANDLER_PROTO);
    173   1.75    atatat #endif /* CPU_DISKINFO */
    174  1.108      elad static void mode_bits(HANDLER_PROTO);
    175  1.144  christos static void reserve(HANDLER_PROTO);
    176    1.1       cgd 
    177   1.95  jdolecek static const struct handlespec {
    178   1.98    atatat 	const char *ps_re;
    179   1.75    atatat 	void (*ps_p)(HANDLER_PROTO);
    180   1.75    atatat 	void (*ps_w)(HANDLER_PROTO);
    181  1.104  christos 	const void *ps_d;
    182   1.75    atatat } handlers[] = {
    183  1.118  christos 	{ "/kern/clockrate",			kern_clockrate, NULL, NULL },
    184  1.154  christos 	{ "/kern/evcnt",			printother, NULL, "vmstat -e" },
    185   1.98    atatat 	{ "/kern/vnode",			printother, NULL, "pstat" },
    186   1.98    atatat 	{ "/kern/proc(2|_args)?",		printother, NULL, "ps" },
    187   1.98    atatat 	{ "/kern/file2?",			printother, NULL, "pstat" },
    188   1.98    atatat 	{ "/kern/ntptime",			printother, NULL,
    189   1.98    atatat 						"ntpdc -c kerninfo" },
    190   1.98    atatat 	{ "/kern/msgbuf",			printother, NULL, "dmesg" },
    191  1.118  christos 	{ "/kern/boottime",			kern_boottime, NULL, NULL },
    192  1.118  christos 	{ "/kern/consdev",			kern_consdev, NULL, NULL },
    193  1.118  christos 	{ "/kern/cp_time(/[0-9]+)?",		kern_cp_time, NULL, NULL },
    194   1.98    atatat 	{ "/kern/sysvipc_info",			printother, NULL, "ipcs" },
    195  1.118  christos 	{ "/kern/cp_id(/[0-9]+)?",		kern_cp_id, NULL, NULL },
    196   1.98    atatat 
    197  1.118  christos 	{ "/kern/coredump/setid/mode",		mode_bits, mode_bits, NULL },
    198  1.118  christos 	{ "/kern/drivers",			kern_drivers, NULL, NULL },
    199  1.116      elad 
    200  1.156  knakahar 	{ "/kern/intr/list",			printother, NULL, "intrctl" },
    201  1.156  knakahar 	{ "/kern/intr/affinity",		printother, NULL, "intrctl" },
    202  1.156  knakahar 	{ "/kern/intr/intr",			printother, NULL, "intrctl" },
    203  1.156  knakahar 	{ "/kern/intr/nointr",			printother, NULL, "intrctl" },
    204  1.156  knakahar 
    205   1.98    atatat 	{ "/vm/vmmeter",			printother, NULL,
    206   1.98    atatat 						"vmstat' or 'systat" },
    207  1.118  christos 	{ "/vm/loadavg",			vm_loadavg, NULL, NULL },
    208   1.98    atatat 	{ "/vm/uvmexp2?",			printother, NULL,
    209   1.98    atatat 						"vmstat' or 'systat" },
    210   1.98    atatat 
    211   1.98    atatat 	{ "/vfs/nfs/nfsstats",			printother, NULL, "nfsstat" },
    212   1.98    atatat 
    213   1.98    atatat 	{ "/net/inet6?/tcp6?/ident",		printother, NULL, "identd" },
    214   1.98    atatat 	{ "/net/inet6/icmp6/nd6_[dp]rlist",	printother, NULL, "ndp" },
    215  1.157  christos 	{ "/net/inet6/ip6/addctlpolicy",	printother, NULL,
    216  1.157  christos 						"ip6addrctl" },
    217   1.98    atatat 	{ "/net/key/dumps[ap]",			printother, NULL, "setkey" },
    218   1.98    atatat 	{ "/net/[^/]+/[^/]+/pcblist",		printother, NULL,
    219   1.98    atatat 						"netstat' or 'sockstat" },
    220  1.106    rpaulo 	{ "/net/(inet|inet6)/[^/]+/stats",	printother, NULL, "netstat"},
    221  1.159  christos 	{ "/net/inet/(ipip|esp|ah|ipcomp)/.*_stats",
    222  1.159  christos 						printother, NULL, "netstat"},
    223  1.159  christos 	{ "/net/inet/ipsec/ipsecstats",		printother, NULL, "netstat"},
    224  1.105    rpaulo 	{ "/net/bpf/(stats|peers)",		printother, NULL, "netstat"},
    225   1.98    atatat 
    226  1.145  christos 	{ "/net/inet.*/tcp.*/deb.*",		printother, NULL, "trpt" },
    227  1.145  christos 
    228  1.144  christos 	{ "/net/inet.*/ip.*/anonportalgo/reserve", reserve, reserve, NULL },
    229  1.107    rpaulo 
    230  1.107    rpaulo 	{ "/net/ns/spp/deb.*",			printother, NULL, "trsp" },
    231  1.107    rpaulo 
    232   1.98    atatat 	{ "/hw/diskstats",			printother, NULL, "iostat" },
    233   1.98    atatat 
    234   1.75    atatat #ifdef CPU_CONSDEV
    235  1.118  christos 	{ "/machdep/consdev",			kern_consdev, NULL, NULL },
    236   1.75    atatat #endif /* CPU_CONSDEV */
    237   1.75    atatat #ifdef CPU_DISKINFO
    238  1.118  christos 	{ "/machdep/diskinfo",			machdep_diskinfo, NULL, NULL },
    239   1.75    atatat #endif /* CPU_CONSDEV */
    240   1.98    atatat 
    241  1.118  christos 	{ "/proc/[^/]+/rlimit/[^/]+/[^/]+",	proc_limit, proc_limit, NULL },
    242   1.98    atatat 
    243   1.98    atatat 	/*
    244   1.98    atatat 	 * these will only be called when the given node has no children
    245   1.98    atatat 	 */
    246   1.98    atatat 	{ "/net/[^/]+",				printother, NULL, NULL },
    247   1.98    atatat 	{ "/debug",				printother, NULL, NULL },
    248   1.98    atatat 	{ "/ddb",				printother, NULL, NULL },
    249   1.98    atatat 	{ "/vendor",				printother, NULL, NULL },
    250   1.98    atatat 
    251  1.118  christos 	{ NULL,					NULL, NULL, NULL },
    252    1.1       cgd };
    253   1.56  christos 
    254   1.75    atatat struct sysctlnode my_root = {
    255   1.83    atatat 	.sysctl_flags = SYSCTL_VERSION|CTLFLAG_ROOT|CTLTYPE_NODE,
    256  1.153    martin 	.sysctl_size = sizeof(struct sysctlnode),
    257   1.75    atatat 	.sysctl_num = 0,
    258   1.75    atatat 	.sysctl_name = "(prog_root)",
    259    1.1       cgd };
    260    1.1       cgd 
    261   1.84    atatat int	Aflag, aflag, dflag, Mflag, nflag, qflag, rflag, wflag, xflag;
    262   1.92    atatat size_t	nr;
    263   1.92    atatat char	*fn;
    264  1.121  christos int	req, stale, errs;
    265   1.65     lukem FILE	*warnfp = stderr;
    266    1.1       cgd 
    267  1.146  christos #define MAXPORTS	0x10000
    268  1.146  christos 
    269    1.1       cgd /*
    270   1.75    atatat  * vah-riables n stuff
    271    1.1       cgd  */
    272   1.75    atatat char gsname[SYSCTL_NAMELEN * CTL_MAXNAME + CTL_MAXNAME],
    273   1.98    atatat 	canonname[SYSCTL_NAMELEN * CTL_MAXNAME + CTL_MAXNAME],
    274   1.75    atatat 	gdname[10 * CTL_MAXNAME + CTL_MAXNAME];
    275  1.104  christos char sep[] = ".";
    276  1.104  christos const char *eq = " = ";
    277   1.75    atatat const char *lname[] = {
    278   1.75    atatat 	"top", "second", "third", "fourth", "fifth", "sixth",
    279   1.75    atatat 	"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"
    280   1.75    atatat };
    281    1.1       cgd 
    282   1.22    bouyer /*
    283   1.75    atatat  * you've heard of main, haven't you?
    284   1.22    bouyer  */
    285    1.1       cgd int
    286   1.53    simonb main(int argc, char *argv[])
    287    1.1       cgd {
    288   1.75    atatat 	int name[CTL_MAXNAME];
    289   1.75    atatat 	int ch;
    290  1.134  christos 	size_t lastcompiled = 0;
    291  1.134  christos 	regex_t *re;
    292    1.1       cgd 
    293  1.134  christos 	setprogname(argv[0]);
    294   1.84    atatat 	while ((ch = getopt(argc, argv, "Aabdef:Mnqrwx")) != -1) {
    295    1.1       cgd 		switch (ch) {
    296    1.1       cgd 		case 'A':
    297   1.75    atatat 			Aflag++;
    298    1.1       cgd 			break;
    299    1.1       cgd 		case 'a':
    300   1.75    atatat 			aflag++;
    301    1.1       cgd 			break;
    302   1.84    atatat 		case 'd':
    303   1.84    atatat 			dflag++;
    304   1.84    atatat 			break;
    305   1.72     grant 		case 'e':
    306   1.75    atatat 			eq = "=";
    307   1.72     grant 			break;
    308   1.27    tsarna 		case 'f':
    309   1.27    tsarna 			fn = optarg;
    310   1.75    atatat 			wflag++;
    311   1.75    atatat 			break;
    312   1.75    atatat 		case 'M':
    313   1.75    atatat 			Mflag++;
    314   1.27    tsarna 			break;
    315    1.1       cgd 		case 'n':
    316   1.75    atatat 			nflag++;
    317    1.1       cgd 			break;
    318   1.58  sommerfe 		case 'q':
    319   1.75    atatat 			qflag++;
    320   1.75    atatat 			break;
    321   1.82    atatat 		case 'b':	/* FreeBSD compat */
    322   1.75    atatat 		case 'r':
    323   1.75    atatat 			rflag++;
    324   1.58  sommerfe 			break;
    325    1.1       cgd 		case 'w':
    326   1.75    atatat 			wflag++;
    327   1.75    atatat 			break;
    328   1.75    atatat 		case 'x':
    329   1.75    atatat 			xflag++;
    330    1.1       cgd 			break;
    331    1.1       cgd 		default:
    332    1.1       cgd 			usage();
    333    1.1       cgd 		}
    334    1.1       cgd 	}
    335   1.58  sommerfe 
    336   1.75    atatat 	argc -= optind;
    337   1.75    atatat 	argv += optind;
    338   1.75    atatat 
    339   1.75    atatat 	if (xflag && rflag)
    340   1.75    atatat 		usage();
    341   1.75    atatat 	/* if ((xflag || rflag) && wflag)
    342   1.75    atatat 		usage(); */
    343  1.136  christos 	/* if (aflag && (Mflag || qflag))
    344   1.75    atatat 		usage(); */
    345  1.136  christos 	if ((aflag || Aflag) && qflag)
    346  1.136  christos 		usage();
    347   1.84    atatat 	if ((Aflag || Mflag || dflag) && argc == 0 && fn == NULL)
    348   1.75    atatat 		aflag = 1;
    349   1.58  sommerfe 
    350  1.133     pooka 	if (prog_init && prog_init() == -1)
    351  1.151  christos 		err(EXIT_FAILURE, "prog init failed");
    352  1.133     pooka 
    353   1.75    atatat 	if (Aflag)
    354   1.75    atatat 		warnfp = stdout;
    355  1.100    atatat 	stale = req = 0;
    356    1.1       cgd 
    357  1.134  christos 	if ((re = malloc(sizeof(*re) * __arraycount(handlers))) == NULL)
    358  1.151  christos 		err(EXIT_FAILURE, "malloc regex");
    359  1.134  christos 
    360   1.75    atatat 	if (aflag) {
    361  1.134  christos 		print_tree(&name[0], 0, NULL, CTLTYPE_NODE, 1,
    362  1.134  christos 		    re, &lastcompiled);
    363   1.75    atatat 		/* if (argc == 0) */
    364   1.75    atatat 		return (0);
    365    1.1       cgd 	}
    366   1.27    tsarna 
    367   1.27    tsarna 	if (fn) {
    368   1.27    tsarna 		FILE *fp;
    369   1.27    tsarna 		char *l;
    370   1.58  sommerfe 
    371   1.27    tsarna 		fp = fopen(fn, "r");
    372   1.27    tsarna 		if (fp == NULL) {
    373  1.151  christos 			err(EXIT_FAILURE, "%s", fn);
    374   1.27    tsarna 		} else {
    375   1.92    atatat 			nr = 0;
    376   1.92    atatat 			while ((l = fparseln(fp, NULL, &nr, NULL, 0)) != NULL)
    377   1.75    atatat 			{
    378   1.75    atatat 				if (*l) {
    379  1.134  christos 					parse(l, re, &lastcompiled);
    380   1.75    atatat 					free(l);
    381   1.75    atatat 				}
    382   1.27    tsarna 			}
    383   1.27    tsarna 			fclose(fp);
    384   1.27    tsarna 		}
    385  1.121  christos 		return errs ? 1 : 0;
    386   1.27    tsarna 	}
    387   1.75    atatat 
    388   1.75    atatat 	if (argc == 0)
    389   1.75    atatat 		usage();
    390   1.75    atatat 
    391   1.75    atatat 	while (argc-- > 0)
    392  1.134  christos 		parse(*argv++, re, &lastcompiled);
    393   1.75    atatat 
    394  1.151  christos 	return errs ? EXIT_FAILURE : EXIT_SUCCESS;
    395    1.1       cgd }
    396    1.1       cgd 
    397    1.1       cgd /*
    398   1.75    atatat  * ********************************************************************
    399   1.75    atatat  * how to find someone special to handle the reading (or maybe even
    400   1.75    atatat  * writing) of a particular node
    401   1.75    atatat  * ********************************************************************
    402    1.1       cgd  */
    403   1.95  jdolecek static const struct handlespec *
    404  1.141  christos findhandler(const char *s, regex_t *re, size_t *lastcompiled)
    405   1.75    atatat {
    406   1.95  jdolecek 	const struct handlespec *p;
    407  1.134  christos 	size_t i, l;
    408  1.134  christos 	int j;
    409   1.98    atatat 	char eb[64];
    410  1.134  christos 	regmatch_t match;
    411   1.75    atatat 
    412   1.75    atatat 	p = &handlers[0];
    413   1.98    atatat 	l = strlen(s);
    414   1.98    atatat 	for (i = 0; p[i].ps_re != NULL; i++) {
    415  1.134  christos 		if (i >= *lastcompiled) {
    416  1.134  christos 			j = regcomp(&re[i], p[i].ps_re, REG_EXTENDED);
    417  1.134  christos 			if (j != 0) {
    418  1.134  christos 				regerror(j, &re[i], eb, sizeof(eb));
    419  1.151  christos 				errx(EXIT_FAILURE, "regcomp: %s: %s", p[i].ps_re, eb);
    420  1.134  christos 			}
    421  1.134  christos 			*lastcompiled = i + 1;
    422   1.98    atatat 		}
    423  1.134  christos 		j = regexec(&re[i], s, 1, &match, 0);
    424   1.98    atatat 		if (j == 0) {
    425  1.141  christos 			if (match.rm_so == 0 && match.rm_eo == (int)l)
    426  1.134  christos 				return &p[i];
    427   1.98    atatat 		}
    428   1.98    atatat 		else if (j != REG_NOMATCH) {
    429  1.134  christos 			regerror(j, &re[i], eb, sizeof(eb));
    430  1.151  christos 			errx(EXIT_FAILURE, "regexec: %s: %s", p[i].ps_re, eb);
    431   1.98    atatat 		}
    432   1.75    atatat 	}
    433   1.75    atatat 
    434  1.134  christos 	return NULL;
    435   1.75    atatat }
    436   1.75    atatat 
    437   1.98    atatat /*
    438   1.98    atatat  * after sysctlgetmibinfo is done with the name, we convert all
    439   1.98    atatat  * separators to / and stuff one at the front if it was missing
    440   1.98    atatat  */
    441   1.98    atatat static void
    442   1.98    atatat canonicalize(const char *i, char *o)
    443    1.1       cgd {
    444   1.98    atatat 	const char *t;
    445   1.98    atatat 	char p[SYSCTL_NAMELEN + 1];
    446   1.98    atatat 	int l;
    447   1.98    atatat 
    448   1.98    atatat 	if (i[0] != *sep) {
    449   1.98    atatat 		o[0] = '/';
    450   1.98    atatat 		o[1] = '\0';
    451    1.1       cgd 	}
    452   1.98    atatat 	else
    453   1.98    atatat 		o[0] = '\0';
    454   1.75    atatat 
    455   1.98    atatat 	t = i;
    456   1.98    atatat 	do {
    457   1.98    atatat 		i = t;
    458   1.98    atatat 		t = strchr(i, sep[0]);
    459   1.98    atatat 		if (t == NULL)
    460   1.98    atatat 			strcat(o, i);
    461   1.98    atatat 		else {
    462   1.98    atatat 			l = t - i;
    463   1.98    atatat 			t++;
    464   1.98    atatat 			memcpy(p, i, l);
    465   1.98    atatat 			p[l] = '\0';
    466   1.98    atatat 			strcat(o, p);
    467   1.98    atatat 			strcat(o, "/");
    468   1.98    atatat 		}
    469   1.98    atatat 	} while (t != NULL);
    470    1.1       cgd }
    471    1.1       cgd 
    472    1.1       cgd /*
    473   1.75    atatat  * ********************************************************************
    474   1.75    atatat  * convert this special number to a special string so we can print the
    475   1.75    atatat  * mib
    476   1.75    atatat  * ********************************************************************
    477    1.1       cgd  */
    478   1.75    atatat static const char *
    479   1.75    atatat sf(u_int f)
    480    1.1       cgd {
    481   1.75    atatat 	static char s[256];
    482  1.104  christos 	const char *c;
    483   1.75    atatat 
    484   1.75    atatat 	s[0] = '\0';
    485   1.75    atatat 	c = "";
    486   1.75    atatat 
    487   1.75    atatat #define print_flag(_f, _s, _c, _q, _x) \
    488   1.83    atatat 	if (((_f) & (__CONCAT(CTLFLAG_,_x))) == (__CONCAT(CTLFLAG_,_q))) { \
    489   1.75    atatat 		strlcat((_s), (_c), sizeof(_s)); \
    490   1.75    atatat 		strlcat((_s), __STRING(_q), sizeof(_s)); \
    491   1.75    atatat 		(_c) = ","; \
    492   1.83    atatat 		(_f) &= ~(__CONCAT(CTLFLAG_,_x)); \
    493   1.75    atatat 	}
    494   1.83    atatat 	print_flag(f, s, c, READONLY,  READWRITE);
    495   1.83    atatat 	print_flag(f, s, c, READWRITE, READWRITE);
    496   1.83    atatat 	print_flag(f, s, c, ANYWRITE,  ANYWRITE);
    497   1.83    atatat 	print_flag(f, s, c, PRIVATE,   PRIVATE);
    498   1.83    atatat 	print_flag(f, s, c, PERMANENT, PERMANENT);
    499   1.83    atatat 	print_flag(f, s, c, OWNDATA,   OWNDATA);
    500   1.83    atatat 	print_flag(f, s, c, IMMEDIATE, IMMEDIATE);
    501   1.83    atatat 	print_flag(f, s, c, HEX,       HEX);
    502   1.83    atatat 	print_flag(f, s, c, ROOT,      ROOT);
    503   1.83    atatat 	print_flag(f, s, c, ANYNUMBER, ANYNUMBER);
    504   1.83    atatat 	print_flag(f, s, c, HIDDEN,    HIDDEN);
    505   1.83    atatat 	print_flag(f, s, c, ALIAS,     ALIAS);
    506   1.75    atatat #undef print_flag
    507   1.75    atatat 
    508   1.75    atatat 	if (f) {
    509   1.75    atatat 		char foo[9];
    510   1.75    atatat 		snprintf(foo, sizeof(foo), "%x", f);
    511   1.75    atatat 		strlcat(s, c, sizeof(s));
    512   1.75    atatat 		strlcat(s, foo, sizeof(s));
    513   1.75    atatat 	}
    514    1.1       cgd 
    515   1.75    atatat 	return (s);
    516   1.75    atatat }
    517   1.72     grant 
    518   1.75    atatat static const char *
    519   1.75    atatat st(u_int t)
    520   1.75    atatat {
    521    1.1       cgd 
    522   1.75    atatat 	switch (t) {
    523   1.75    atatat 	case CTLTYPE_NODE:
    524   1.75    atatat 		return "NODE";
    525   1.75    atatat 	case CTLTYPE_INT:
    526   1.75    atatat 		return "INT";
    527   1.75    atatat 	case CTLTYPE_STRING:
    528   1.75    atatat 		return "STRING";
    529   1.75    atatat 	case CTLTYPE_QUAD:
    530   1.75    atatat                 return "QUAD";
    531   1.75    atatat 	case CTLTYPE_STRUCT:
    532   1.75    atatat 		return "STRUCT";
    533  1.131       mrg 	case CTLTYPE_BOOL:
    534  1.131       mrg 		return "BOOL";
    535   1.75    atatat 	}
    536    1.1       cgd 
    537   1.75    atatat 	return "???";
    538   1.75    atatat }
    539   1.46       chs 
    540   1.75    atatat /*
    541   1.75    atatat  * ********************************************************************
    542  1.100    atatat  * recursively eliminate all data belonging to the given node
    543  1.100    atatat  * ********************************************************************
    544  1.100    atatat  */
    545  1.100    atatat static void
    546  1.100    atatat purge_tree(struct sysctlnode *rnode)
    547  1.100    atatat {
    548  1.100    atatat 	struct sysctlnode *node;
    549  1.100    atatat 
    550  1.100    atatat 	if (rnode == NULL ||
    551  1.100    atatat 	    SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE ||
    552  1.100    atatat 	    rnode->sysctl_child == NULL)
    553  1.100    atatat 		return;
    554  1.100    atatat 
    555  1.100    atatat 	for (node = rnode->sysctl_child;
    556  1.100    atatat 	     node < &rnode->sysctl_child[rnode->sysctl_clen];
    557  1.100    atatat 	     node++)
    558  1.100    atatat 		purge_tree(node);
    559  1.100    atatat 	free(rnode->sysctl_child);
    560  1.100    atatat 	rnode->sysctl_csize = 0;
    561  1.100    atatat 	rnode->sysctl_clen = 0;
    562  1.100    atatat 	rnode->sysctl_child = NULL;
    563  1.100    atatat 
    564  1.100    atatat 	if (rnode->sysctl_desc == (const char*)-1)
    565  1.100    atatat 		rnode->sysctl_desc = NULL;
    566  1.100    atatat 	if (rnode->sysctl_desc != NULL)
    567  1.104  christos 		free(__UNCONST(rnode->sysctl_desc));
    568  1.100    atatat 	rnode->sysctl_desc = NULL;
    569  1.100    atatat }
    570  1.100    atatat 
    571  1.137  christos static void __attribute__((__format__(__printf__, 3, 4)))
    572  1.137  christos appendprintf(char **bp, size_t *lbp, const char *fmt, ...)
    573  1.137  christos {
    574  1.137  christos 	int r;
    575  1.137  christos 	va_list ap;
    576  1.137  christos 
    577  1.137  christos 	va_start(ap, fmt);
    578  1.137  christos 	r = vsnprintf(*bp, *lbp, fmt, ap);
    579  1.137  christos 	va_end(ap);
    580  1.137  christos 	if (r < 0 || (size_t)r > *lbp)
    581  1.137  christos 		r = *lbp;
    582  1.137  christos 	*bp += r;
    583  1.137  christos 	*lbp -= r;
    584  1.137  christos }
    585  1.137  christos 
    586  1.100    atatat /*
    587  1.100    atatat  * ********************************************************************
    588   1.75    atatat  * print this node and any others underneath it
    589   1.75    atatat  * ********************************************************************
    590   1.75    atatat  */
    591   1.75    atatat static void
    592   1.75    atatat print_tree(int *name, u_int namelen, struct sysctlnode *pnode, u_int type,
    593  1.134  christos    int add, regex_t *re, size_t *lastcompiled)
    594   1.75    atatat {
    595   1.75    atatat 	struct sysctlnode *node;
    596  1.128     lukem 	int rc;
    597  1.137  christos 	size_t ni, sz, ldp, lsp;
    598  1.137  christos 	char *sp, *dp, *tsp, *tdp;
    599   1.95  jdolecek 	const struct handlespec *p;
    600   1.75    atatat 
    601  1.137  christos 	sp = tsp = &gsname[strlen(gsname)];
    602  1.137  christos 	dp = tdp = &gdname[strlen(gdname)];
    603  1.137  christos 	ldp = sizeof(gdname) - (dp - gdname);
    604  1.137  christos 	lsp = sizeof(gsname) - (sp - gsname);
    605   1.75    atatat 
    606   1.75    atatat 	if (sp != &gsname[0] && dp == &gdname[0]) {
    607   1.75    atatat 		/*
    608   1.75    atatat 		 * aw...shucks.  now we must play catch up
    609   1.75    atatat 		 */
    610  1.137  christos 		for (ni = 0; ni < namelen; ni++)
    611  1.137  christos 			appendprintf(&tdp, &ldp, "%s%d", ni > 0 ? "." : "",
    612  1.137  christos 			    name[ni]);
    613   1.75    atatat 	}
    614    1.1       cgd 
    615   1.75    atatat 	if (pnode == NULL)
    616   1.75    atatat 		pnode = &my_root;
    617   1.75    atatat 	else if (add) {
    618  1.137  christos 		appendprintf(&tsp, &lsp, "%s%s", namelen > 1 ? sep : "",
    619  1.137  christos 			pnode->sysctl_name);
    620  1.137  christos 		appendprintf(&tdp, &ldp, "%s%d", namelen > 1 ? "." : "",
    621  1.137  christos 			pnode->sysctl_num);
    622   1.75    atatat 	}
    623   1.75    atatat 
    624   1.75    atatat 	if (Mflag && pnode != &my_root) {
    625   1.75    atatat 		if (nflag)
    626   1.75    atatat 			printf("%s: ", gdname);
    627   1.75    atatat 		else
    628   1.75    atatat 			printf("%s (%s): ", gsname, gdname);
    629   1.75    atatat 		printf("CTLTYPE_%s", st(type));
    630   1.75    atatat 		if (type == CTLTYPE_NODE) {
    631   1.83    atatat 			if (SYSCTL_FLAGS(pnode->sysctl_flags) & CTLFLAG_ALIAS)
    632   1.75    atatat 				printf(", alias %d",
    633   1.75    atatat 				       pnode->sysctl_alias);
    634   1.75    atatat 			else
    635   1.75    atatat 				printf(", children %d/%d",
    636   1.75    atatat 				       pnode->sysctl_clen,
    637   1.75    atatat 				       pnode->sysctl_csize);
    638   1.75    atatat 		}
    639   1.75    atatat 		printf(", size %zu", pnode->sysctl_size);
    640   1.75    atatat 		printf(", flags 0x%x<%s>",
    641   1.75    atatat 		       SYSCTL_FLAGS(pnode->sysctl_flags),
    642   1.75    atatat 		       sf(SYSCTL_FLAGS(pnode->sysctl_flags)));
    643   1.75    atatat 		if (pnode->sysctl_func)
    644   1.75    atatat 			printf(", func=%p", pnode->sysctl_func);
    645   1.75    atatat 		printf(", ver=%d", pnode->sysctl_ver);
    646   1.75    atatat 		printf("\n");
    647   1.75    atatat 		if (type != CTLTYPE_NODE) {
    648   1.75    atatat 			*sp = *dp = '\0';
    649    1.1       cgd 			return;
    650    1.1       cgd 		}
    651   1.75    atatat 	}
    652   1.75    atatat 
    653   1.84    atatat 	if (dflag && pnode != &my_root) {
    654   1.84    atatat 		if (Aflag || type != CTLTYPE_NODE) {
    655   1.84    atatat 			if (pnode->sysctl_desc == NULL)
    656   1.84    atatat 				getdesc1(name, namelen, pnode);
    657   1.91    atatat 			if (Aflag || !add ||
    658   1.84    atatat 			    (pnode->sysctl_desc != NULL &&
    659   1.84    atatat 			     pnode->sysctl_desc != (const char*)-1)) {
    660   1.84    atatat 				if (!nflag)
    661   1.84    atatat 					printf("%s: ", gsname);
    662   1.84    atatat 				if (pnode->sysctl_desc == NULL ||
    663   1.84    atatat 				    pnode->sysctl_desc == (const char*)-1)
    664   1.84    atatat 					printf("(no description)\n");
    665   1.84    atatat 				else
    666   1.84    atatat 					printf("%s\n", pnode->sysctl_desc);
    667   1.84    atatat 			}
    668   1.84    atatat 		}
    669   1.84    atatat 
    670   1.84    atatat 		if (type != CTLTYPE_NODE) {
    671   1.84    atatat 			*sp = *dp = '\0';
    672   1.84    atatat 			return;
    673   1.84    atatat 		}
    674   1.84    atatat 	}
    675   1.84    atatat 
    676   1.75    atatat 	/*
    677   1.75    atatat 	 * if this is an alias and we added our name, that means we
    678   1.75    atatat 	 * got here by recursing down into the tree, so skip it.  The
    679   1.75    atatat 	 * only way to print an aliased node is with either -M or by
    680   1.75    atatat 	 * name specifically.
    681   1.75    atatat 	 */
    682   1.83    atatat 	if (SYSCTL_FLAGS(pnode->sysctl_flags) & CTLFLAG_ALIAS && add) {
    683   1.75    atatat 		*sp = *dp = '\0';
    684    1.1       cgd 		return;
    685   1.75    atatat 	}
    686    1.1       cgd 
    687   1.98    atatat 	canonicalize(gsname, canonname);
    688  1.141  christos 	p = findhandler(canonname, re, lastcompiled);
    689   1.75    atatat 	if (type != CTLTYPE_NODE && p != NULL) {
    690  1.141  christos 		if (p->ps_p == NULL) {
    691  1.141  christos 			sysctlperror("Cannot print `%s': %s\n", gsname,
    692  1.141  christos 			    strerror(EOPNOTSUPP));
    693  1.151  christos 			exit(EXIT_FAILURE);
    694  1.141  christos 		}
    695   1.75    atatat 		(*p->ps_p)(gsname, gdname, NULL, name, namelen, pnode, type,
    696  1.104  christos 			   __UNCONST(p->ps_d));
    697   1.75    atatat 		*sp = *dp = '\0';
    698   1.75    atatat 		return;
    699   1.75    atatat 	}
    700    1.1       cgd 
    701   1.75    atatat 	if (type != CTLTYPE_NODE && pnode->sysctl_size == 0) {
    702  1.133     pooka 		rc = prog_sysctl(&name[0], namelen, NULL, &sz, NULL, 0);
    703   1.75    atatat 		if (rc == -1) {
    704   1.75    atatat 			sysctlerror(1);
    705   1.75    atatat 			*sp = *dp = '\0';
    706   1.18   thorpej 			return;
    707   1.75    atatat 		}
    708   1.75    atatat 		if (sz == 0) {
    709   1.75    atatat 			if ((Aflag || req) && !Mflag)
    710   1.75    atatat 				printf("%s: node contains no data\n", gsname);
    711   1.75    atatat 			*sp = *dp = '\0';
    712   1.18   thorpej 			return;
    713   1.18   thorpej 		}
    714   1.75    atatat 	}
    715   1.75    atatat 	else
    716   1.75    atatat 		sz = pnode->sysctl_size;
    717   1.17   thorpej 
    718   1.75    atatat 	switch (type) {
    719   1.75    atatat 	case CTLTYPE_NODE: {
    720   1.85    atatat 		__learn_tree(name, namelen, pnode);
    721   1.75    atatat 		node = pnode->sysctl_child;
    722   1.75    atatat 		if (node == NULL) {
    723   1.88    atatat 			if (dflag)
    724   1.88    atatat 				/* do nothing */;
    725   1.88    atatat 			else if (p != NULL)
    726   1.75    atatat 				(*p->ps_p)(gsname, gdname, NULL, name, namelen,
    727  1.104  christos 					   pnode, type, __UNCONST(p->ps_d));
    728   1.88    atatat 			else if ((Aflag || req) && !Mflag)
    729   1.75    atatat 				printf("%s: no children\n", gsname);
    730   1.75    atatat 		}
    731   1.75    atatat 		else {
    732   1.84    atatat 			if (dflag)
    733   1.84    atatat 				/*
    734   1.84    atatat 				 * get all descriptions for next level
    735   1.84    atatat 				 * in one chunk
    736   1.84    atatat 				 */
    737   1.84    atatat 				getdesc(name, namelen, pnode);
    738   1.75    atatat 			req = 0;
    739   1.75    atatat 			for (ni = 0; ni < pnode->sysctl_clen; ni++) {
    740   1.75    atatat 				name[namelen] = node[ni].sysctl_num;
    741   1.83    atatat 				if ((node[ni].sysctl_flags & CTLFLAG_HIDDEN) &&
    742   1.75    atatat 				    !(Aflag || req))
    743   1.75    atatat 					continue;
    744   1.75    atatat 				print_tree(name, namelen + 1, &node[ni],
    745   1.75    atatat 					   SYSCTL_TYPE(node[ni].sysctl_flags),
    746  1.134  christos 					   1, re, lastcompiled);
    747   1.75    atatat 			}
    748   1.75    atatat 		}
    749    1.1       cgd 		break;
    750   1.75    atatat 	}
    751   1.75    atatat 	case CTLTYPE_INT: {
    752   1.75    atatat 		int i;
    753  1.133     pooka 		rc = prog_sysctl(name, namelen, &i, &sz, NULL, 0);
    754   1.75    atatat 		if (rc == -1) {
    755   1.75    atatat 			sysctlerror(1);
    756   1.75    atatat 			break;
    757   1.75    atatat 		}
    758   1.75    atatat 		display_number(pnode, gsname, &i, sizeof(i), DISPLAY_VALUE);
    759   1.22    bouyer 		break;
    760   1.75    atatat 	}
    761  1.131       mrg 	case CTLTYPE_BOOL: {
    762  1.131       mrg 		bool b;
    763  1.133     pooka 		rc = prog_sysctl(name, namelen, &b, &sz, NULL, 0);
    764  1.131       mrg 		if (rc == -1) {
    765  1.131       mrg 			sysctlerror(1);
    766  1.131       mrg 			break;
    767  1.131       mrg 		}
    768  1.131       mrg 		display_number(pnode, gsname, &b, sizeof(b), DISPLAY_VALUE);
    769  1.131       mrg 		break;
    770  1.131       mrg 	}
    771   1.75    atatat 	case CTLTYPE_STRING: {
    772   1.75    atatat 		unsigned char buf[1024], *tbuf;
    773   1.75    atatat 		tbuf = buf;
    774   1.75    atatat 		sz = sizeof(buf);
    775  1.133     pooka 		rc = prog_sysctl(&name[0], namelen, tbuf, &sz, NULL, 0);
    776   1.75    atatat 		if (rc == -1 && errno == ENOMEM) {
    777   1.75    atatat 			tbuf = malloc(sz);
    778   1.75    atatat 			if (tbuf == NULL) {
    779   1.75    atatat 				sysctlerror(1);
    780   1.75    atatat 				break;
    781   1.75    atatat 			}
    782  1.133     pooka 			rc = prog_sysctl(&name[0], namelen, tbuf, &sz, NULL, 0);
    783   1.56  christos 		}
    784   1.75    atatat 		if (rc == -1)
    785   1.75    atatat 			sysctlerror(1);
    786   1.75    atatat 		else
    787   1.96    atatat 			display_string(pnode, gsname, tbuf, sz, DISPLAY_VALUE);
    788   1.75    atatat 		if (tbuf != buf)
    789   1.75    atatat 			free(tbuf);
    790   1.56  christos 		break;
    791    1.1       cgd 	}
    792   1.75    atatat 	case CTLTYPE_QUAD: {
    793   1.75    atatat 		u_quad_t q;
    794   1.75    atatat 		sz = sizeof(q);
    795  1.133     pooka 		rc = prog_sysctl(&name[0], namelen, &q, &sz, NULL, 0);
    796   1.75    atatat 		if (rc == -1) {
    797   1.75    atatat 			sysctlerror(1);
    798   1.75    atatat 			break;
    799   1.75    atatat 		}
    800   1.75    atatat 		display_number(pnode, gsname, &q, sizeof(q), DISPLAY_VALUE);
    801   1.75    atatat 		break;
    802    1.1       cgd 	}
    803   1.75    atatat 	case CTLTYPE_STRUCT: {
    804   1.75    atatat 		/*
    805   1.75    atatat 		 * we shouldn't actually get here, but if we
    806   1.75    atatat 		 * do, would it be nice to have *something* to
    807   1.75    atatat 		 * do other than completely ignore the
    808   1.75    atatat 		 * request.
    809   1.75    atatat 		 */
    810   1.75    atatat 		unsigned char *d;
    811   1.75    atatat 		if ((d = malloc(sz)) == NULL) {
    812   1.75    atatat 			fprintf(warnfp, "%s: !malloc failed!\n", gsname);
    813    1.1       cgd 			break;
    814   1.75    atatat 		}
    815  1.133     pooka 		rc = prog_sysctl(&name[0], namelen, d, &sz, NULL, 0);
    816   1.75    atatat 		if (rc == -1) {
    817   1.75    atatat 			sysctlerror(1);
    818    1.1       cgd 			break;
    819    1.1       cgd 		}
    820   1.75    atatat 		display_struct(pnode, gsname, d, sz, DISPLAY_VALUE);
    821   1.75    atatat 		free(d);
    822   1.75    atatat 		break;
    823    1.1       cgd 	}
    824   1.75    atatat 	default:
    825   1.75    atatat 		/* should i print an error here? */
    826   1.75    atatat 		break;
    827   1.75    atatat 	}
    828   1.75    atatat 
    829   1.75    atatat 	*sp = *dp = '\0';
    830   1.75    atatat }
    831   1.75    atatat 
    832   1.75    atatat /*
    833   1.75    atatat  * ********************************************************************
    834   1.75    atatat  * parse a request, possibly determining that it's a create or destroy
    835   1.75    atatat  * request
    836   1.75    atatat  * ********************************************************************
    837   1.75    atatat  */
    838   1.75    atatat static void
    839  1.134  christos parse(char *l, regex_t *re, size_t *lastcompiled)
    840   1.75    atatat {
    841   1.75    atatat 	struct sysctlnode *node;
    842   1.95  jdolecek 	const struct handlespec *w;
    843   1.84    atatat 	int name[CTL_MAXNAME], dodesc = 0;
    844   1.75    atatat 	u_int namelen, type;
    845   1.75    atatat 	char *key, *value, *dot;
    846   1.75    atatat 	size_t sz;
    847  1.129  christos 	bool optional = false;
    848   1.75    atatat 
    849   1.75    atatat 	req = 1;
    850   1.75    atatat 	key = l;
    851  1.129  christos 
    852  1.129  christos 	if ((value = strchr(l, '=')) != NULL) {
    853  1.129  christos 		if (value > l && value[-1] == '?') {
    854  1.129  christos 			value[-1] = '\0';
    855  1.129  christos 			optional = true;
    856  1.129  christos 		}
    857   1.75    atatat 		*value++ = '\0';
    858  1.129  christos 	}
    859    1.1       cgd 
    860   1.75    atatat 	if ((dot = strpbrk(key, "./")) == NULL)
    861   1.75    atatat 		sep[0] = '.';
    862   1.75    atatat 	else
    863   1.75    atatat 		sep[0] = dot[0];
    864   1.75    atatat 	sep[1] = '\0';
    865    1.1       cgd 
    866   1.82    atatat 	while (key[0] == sep[0] && key[1] == sep[0]) {
    867   1.75    atatat 		if (value != NULL)
    868   1.75    atatat 			value[-1] = '=';
    869   1.82    atatat 		if (strncmp(key + 2, "create", 6) == 0 &&
    870   1.82    atatat 		    (key[8] == '=' || key[8] == sep[0]))
    871   1.92    atatat 			parse_create(key + 8 + (key[8] == '=' ? 1 : 0));
    872   1.82    atatat 		else if (strncmp(key + 2, "destroy", 7) == 0 &&
    873   1.82    atatat 			 (key[9] == '=' || key[9] == sep[0]))
    874   1.92    atatat 			parse_destroy(key + 9 + (key[9] == '=' ? 1 : 0));
    875   1.84    atatat 		else if (strncmp(key + 2, "describe", 8) == 0 &&
    876   1.84    atatat 			 (key[10] == '=' || key[10] == sep[0])) {
    877   1.84    atatat 			key += 10 + (key[10] == '=');
    878   1.84    atatat 			if ((value = strchr(key, '=')) != NULL)
    879   1.84    atatat 				parse_describe(key);
    880   1.84    atatat 			else {
    881   1.84    atatat 				if (!dflag)
    882   1.84    atatat 					dodesc = 1;
    883   1.84    atatat 				break;
    884   1.84    atatat 			}
    885   1.84    atatat 		}
    886    1.1       cgd 		else
    887   1.92    atatat 			sysctlperror("unable to parse '%s'\n", key);
    888   1.32    simonb 		return;
    889   1.32    simonb 	}
    890   1.32    simonb 
    891  1.100    atatat 	if (stale) {
    892  1.100    atatat 		purge_tree(&my_root);
    893  1.100    atatat 		stale = 0;
    894  1.100    atatat 	}
    895   1.75    atatat 	node = &my_root;
    896   1.75    atatat 	namelen = CTL_MAXNAME;
    897   1.75    atatat 	sz = sizeof(gsname);
    898   1.75    atatat 
    899  1.162     kamil 	if (prog_sysctlgetmibinfo(key, &name[0], &namelen, gsname, &sz, &node,
    900   1.83    atatat 			     SYSCTL_VERSION) == -1) {
    901  1.129  christos 		if (optional)
    902  1.129  christos 			return;
    903   1.92    atatat 		sysctlparseerror(namelen, l);
    904  1.151  christos 		EXIT(EXIT_FAILURE);
    905   1.75    atatat 	}
    906   1.75    atatat 
    907   1.75    atatat 	type = SYSCTL_TYPE(node->sysctl_flags);
    908   1.75    atatat 
    909   1.75    atatat 	if (value == NULL) {
    910   1.84    atatat 		if (dodesc)
    911   1.84    atatat 			dflag = 1;
    912  1.134  christos 		print_tree(&name[0], namelen, node, type, 0, re, lastcompiled);
    913   1.84    atatat 		if (dodesc)
    914   1.84    atatat 			dflag = 0;
    915   1.75    atatat 		gsname[0] = '\0';
    916   1.69       dsl 		return;
    917   1.69       dsl 	}
    918   1.69       dsl 
    919   1.92    atatat 	if (fn)
    920   1.92    atatat 		trim_whitespace(value, 1);
    921   1.92    atatat 
    922   1.82    atatat 	if (!wflag) {
    923   1.92    atatat 		sysctlperror("Must specify -w to set variables\n");
    924  1.151  christos 		exit(EXIT_FAILURE);
    925   1.82    atatat 	}
    926   1.82    atatat 
    927   1.98    atatat 	canonicalize(gsname, canonname);
    928  1.141  christos 	if (type != CTLTYPE_NODE && (w = findhandler(canonname, re,
    929  1.134  christos 	    lastcompiled)) != NULL) {
    930  1.141  christos 		if (w->ps_w == NULL) {
    931  1.141  christos 			sysctlperror("Cannot write `%s': %s\n", gsname,
    932  1.141  christos 			    strerror(EOPNOTSUPP));
    933  1.151  christos 			exit(EXIT_FAILURE);
    934  1.141  christos 		}
    935   1.75    atatat 		(*w->ps_w)(gsname, gdname, value, name, namelen, node, type,
    936  1.104  christos 			   __UNCONST(w->ps_d));
    937   1.75    atatat 		gsname[0] = '\0';
    938   1.26      fvdl 		return;
    939   1.26      fvdl 	}
    940   1.58  sommerfe 
    941    1.1       cgd 	switch (type) {
    942   1.75    atatat 	case CTLTYPE_NODE:
    943   1.75    atatat 		/*
    944   1.75    atatat 		 * XXX old behavior is to print.  should we error instead?
    945   1.75    atatat 		 */
    946  1.134  christos 		print_tree(&name[0], namelen, node, CTLTYPE_NODE, 1, re,
    947  1.134  christos 		    lastcompiled);
    948   1.75    atatat 		break;
    949    1.1       cgd 	case CTLTYPE_INT:
    950  1.131       mrg 	case CTLTYPE_BOOL:
    951  1.131       mrg 	case CTLTYPE_QUAD:
    952  1.158  dholland 		write_number(&name[0], namelen, node, value, optional);
    953   1.75    atatat 		break;
    954    1.1       cgd 	case CTLTYPE_STRING:
    955  1.158  dholland 		write_string(&name[0], namelen, node, value, optional);
    956   1.75    atatat 		break;
    957   1.75    atatat 	case CTLTYPE_STRUCT:
    958   1.75    atatat 		/*
    959   1.75    atatat 		 * XXX old behavior is to print.  should we error instead?
    960   1.75    atatat 		 */
    961   1.75    atatat 		/* fprintf(warnfp, "you can't write to %s\n", gsname); */
    962  1.134  christos 		print_tree(&name[0], namelen, node, type, 0, re, lastcompiled);
    963   1.75    atatat 		break;
    964   1.75    atatat 	}
    965   1.75    atatat }
    966   1.75    atatat 
    967   1.75    atatat /*
    968   1.75    atatat 
    969   1.75    atatat   //create=foo.bar.whatever...,
    970   1.75    atatat   [type=(int|quad|string|struct|node),]
    971   1.75    atatat   [size=###,]
    972   1.75    atatat   [n=###,]
    973   1.82    atatat   [flags=(iohxparw12),]
    974   1.75    atatat   [addr=0x####,|symbol=...|value=...]
    975   1.75    atatat 
    976   1.75    atatat   size is optional for some types.  type must be set before anything
    977  1.130      elad   else.  nodes can have [rwhp], but nothing else applies.  if no
    978   1.75    atatat   size or type is given, node is asserted.  writeable is the default,
    979  1.130      elad   with [rw] being read-only and unconditionally writeable
    980   1.75    atatat   respectively.  if you specify addr, it is assumed to be the name of
    981   1.83    atatat   a kernel symbol, if value, CTLFLAG_OWNDATA will be asserted for
    982   1.83    atatat   strings, CTLFLAG_IMMEDIATE for ints and u_quad_ts.  you cannot
    983   1.75    atatat   specify both value and addr.
    984   1.75    atatat 
    985   1.75    atatat */
    986   1.75    atatat 
    987   1.75    atatat static void
    988   1.82    atatat parse_create(char *l)
    989   1.75    atatat {
    990   1.75    atatat 	struct sysctlnode node;
    991   1.75    atatat 	size_t sz;
    992   1.75    atatat 	char *nname, *key, *value, *data, *addr, *c, *t;
    993   1.75    atatat 	int name[CTL_MAXNAME], i, rc, method, flags, rw;
    994   1.75    atatat 	u_int namelen, type;
    995   1.92    atatat 	u_quad_t uq;
    996   1.92    atatat 	quad_t q;
    997  1.131       mrg 	bool b;
    998   1.75    atatat 
    999   1.82    atatat 	if (!wflag) {
   1000   1.92    atatat 		sysctlperror("Must specify -w to create nodes\n");
   1001  1.151  christos 		exit(EXIT_FAILURE);
   1002   1.82    atatat 	}
   1003   1.82    atatat 
   1004   1.75    atatat 	/*
   1005   1.75    atatat 	 * these are the pieces that make up the description of a new
   1006   1.75    atatat 	 * node
   1007   1.75    atatat 	 */
   1008   1.75    atatat 	memset(&node, 0, sizeof(node));
   1009   1.75    atatat 	node.sysctl_num = CTL_CREATE;  /* any number is fine */
   1010   1.75    atatat 	flags = 0;
   1011   1.75    atatat 	rw = -1;
   1012   1.75    atatat 	type = 0;
   1013   1.75    atatat 	sz = 0;
   1014   1.75    atatat 	data = addr = NULL;
   1015   1.75    atatat 	memset(name, 0, sizeof(name));
   1016   1.75    atatat 	namelen = 0;
   1017   1.75    atatat 	method = 0;
   1018   1.75    atatat 
   1019   1.75    atatat 	/*
   1020   1.75    atatat 	 * misc stuff used when constructing
   1021   1.75    atatat 	 */
   1022   1.75    atatat 	i = 0;
   1023  1.131       mrg 	b = false;
   1024   1.92    atatat 	uq = 0;
   1025   1.75    atatat 	key = NULL;
   1026   1.75    atatat 	value = NULL;
   1027   1.75    atatat 
   1028   1.75    atatat 	/*
   1029   1.75    atatat 	 * the name of the thing we're trying to create is first, so
   1030   1.75    atatat 	 * pick it off.
   1031   1.75    atatat 	 */
   1032   1.75    atatat 	nname = l;
   1033   1.75    atatat 	if ((c = strchr(nname, ',')) != NULL)
   1034   1.75    atatat 		*c++ = '\0';
   1035   1.75    atatat 
   1036   1.75    atatat 	while (c != NULL) {
   1037   1.75    atatat 
   1038   1.75    atatat 		/*
   1039   1.75    atatat 		 * pull off the next "key=value" pair
   1040   1.75    atatat 		 */
   1041   1.75    atatat 		key = c;
   1042   1.75    atatat 		if ((t = strchr(key, '=')) != NULL) {
   1043   1.75    atatat 			*t++ = '\0';
   1044   1.75    atatat 			value = t;
   1045   1.75    atatat 		}
   1046   1.75    atatat 		else
   1047   1.75    atatat 			value = NULL;
   1048   1.75    atatat 
   1049   1.75    atatat 		/*
   1050   1.75    atatat 		 * if the "key" is "value", then that eats the rest of
   1051   1.75    atatat 		 * the string, so we're done, otherwise bite it off at
   1052   1.75    atatat 		 * the next comma.
   1053   1.75    atatat 		 */
   1054   1.75    atatat 		if (strcmp(key, "value") == 0) {
   1055   1.75    atatat 			c = NULL;
   1056   1.75    atatat 			data = value;
   1057   1.75    atatat 			break;
   1058   1.75    atatat 		}
   1059  1.112  christos 		else if (value) {
   1060   1.75    atatat 			if ((c = strchr(value, ',')) != NULL)
   1061   1.75    atatat 				*c++ = '\0';
   1062   1.75    atatat 		}
   1063   1.75    atatat 
   1064   1.75    atatat 		/*
   1065  1.133     pooka 		 * note that we (mostly) let the invoker of prog_sysctl(8)
   1066   1.75    atatat 		 * play rampant here and depend on the kernel to tell
   1067   1.75    atatat 		 * them that they were wrong.  well...within reason.
   1068   1.75    atatat 		 * we later check the various parameters against each
   1069   1.75    atatat 		 * other to make sure it makes some sort of sense.
   1070   1.75    atatat 		 */
   1071   1.75    atatat 		if (strcmp(key, "addr") == 0) {
   1072   1.75    atatat 			/*
   1073   1.75    atatat 			 * we can't check these two.  only the kernel
   1074   1.75    atatat 			 * can tell us when it fails to find the name
   1075   1.75    atatat 			 * (or if the address is invalid).
   1076   1.75    atatat 			 */
   1077   1.75    atatat 			if (method != 0) {
   1078   1.92    atatat 				sysctlperror(
   1079   1.92    atatat 				    "%s: already have %s for new node\n",
   1080   1.92    atatat 				    nname,
   1081   1.75    atatat 				    method == CTL_CREATE ? "addr" : "symbol");
   1082  1.151  christos 				EXIT(EXIT_FAILURE);
   1083   1.75    atatat 			}
   1084  1.113  christos 			if (value == NULL) {
   1085  1.113  christos 				sysctlperror("%s: missing value\n", nname);
   1086  1.151  christos 				EXIT(EXIT_FAILURE);
   1087  1.113  christos 			}
   1088  1.114  christos 			errno = 0;
   1089   1.75    atatat 			addr = (void*)strtoul(value, &t, 0);
   1090   1.94    atatat 			if (t == value || *t != '\0' || errno != 0) {
   1091   1.92    atatat 				sysctlperror(
   1092   1.92    atatat 				    "%s: '%s' is not a valid address\n",
   1093   1.92    atatat 				    nname, value);
   1094  1.151  christos 				EXIT(EXIT_FAILURE);
   1095   1.75    atatat 			}
   1096   1.75    atatat 			method = CTL_CREATE;
   1097   1.75    atatat 		}
   1098   1.75    atatat 		else if (strcmp(key, "symbol") == 0) {
   1099   1.75    atatat 			if (method != 0) {
   1100   1.92    atatat 				sysctlperror(
   1101   1.92    atatat 				    "%s: already have %s for new node\n",
   1102   1.92    atatat 				    nname,
   1103   1.75    atatat 				    method == CTL_CREATE ? "addr" : "symbol");
   1104  1.151  christos 				EXIT(EXIT_FAILURE);
   1105   1.75    atatat 			}
   1106   1.75    atatat 			addr = value;
   1107   1.75    atatat 			method = CTL_CREATESYM;
   1108   1.75    atatat 		}
   1109   1.75    atatat 		else if (strcmp(key, "type") == 0) {
   1110  1.114  christos 			if (value == NULL) {
   1111  1.114  christos 				sysctlperror("%s: missing value\n", nname);
   1112  1.151  christos 				EXIT(EXIT_FAILURE);
   1113  1.114  christos 			}
   1114   1.75    atatat 			if (strcmp(value, "node") == 0)
   1115   1.75    atatat 				type = CTLTYPE_NODE;
   1116   1.75    atatat 			else if (strcmp(value, "int") == 0) {
   1117   1.75    atatat 				sz = sizeof(int);
   1118   1.75    atatat 				type = CTLTYPE_INT;
   1119   1.75    atatat 			}
   1120  1.131       mrg 			else if (strcmp(value, "bool") == 0) {
   1121  1.131       mrg 				sz = sizeof(bool);
   1122  1.131       mrg 				type = CTLTYPE_BOOL;
   1123  1.131       mrg 			}
   1124   1.75    atatat 			else if (strcmp(value, "string") == 0)
   1125   1.75    atatat 				type = CTLTYPE_STRING;
   1126   1.75    atatat 			else if (strcmp(value, "quad") == 0) {
   1127   1.75    atatat 				sz = sizeof(u_quad_t);
   1128   1.75    atatat 				type = CTLTYPE_QUAD;
   1129   1.75    atatat 			}
   1130   1.75    atatat 			else if (strcmp(value, "struct") == 0)
   1131   1.75    atatat 				type = CTLTYPE_STRUCT;
   1132   1.75    atatat 			else {
   1133   1.92    atatat 				sysctlperror(
   1134   1.92    atatat 					"%s: '%s' is not a valid type\n",
   1135   1.92    atatat 					nname, value);
   1136  1.151  christos 				EXIT(EXIT_FAILURE);
   1137   1.75    atatat 			}
   1138   1.75    atatat 		}
   1139   1.75    atatat 		else if (strcmp(key, "size") == 0) {
   1140  1.115   jnemeth 			if (value == NULL) {
   1141  1.115   jnemeth 				sysctlperror("%s: missing value\n", nname);
   1142  1.151  christos 				EXIT(EXIT_FAILURE);
   1143  1.115   jnemeth 			}
   1144   1.75    atatat 			errno = 0;
   1145   1.75    atatat 			/*
   1146   1.75    atatat 			 * yes, i know size_t is not an unsigned long,
   1147   1.75    atatat 			 * but we can all agree that it ought to be,
   1148   1.75    atatat 			 * right?
   1149   1.75    atatat 			 */
   1150   1.75    atatat 			sz = strtoul(value, &t, 0);
   1151   1.94    atatat 			if (t == value || *t != '\0' || errno != 0) {
   1152   1.92    atatat 				sysctlperror(
   1153   1.92    atatat 					"%s: '%s' is not a valid size\n",
   1154   1.92    atatat 					nname, value);
   1155  1.151  christos 				EXIT(EXIT_FAILURE);
   1156   1.75    atatat 			}
   1157   1.75    atatat 		}
   1158   1.75    atatat 		else if (strcmp(key, "n") == 0) {
   1159  1.114  christos 			if (value == NULL) {
   1160  1.114  christos 				sysctlperror("%s: missing value\n", nname);
   1161  1.151  christos 				EXIT(EXIT_FAILURE);
   1162  1.114  christos 			}
   1163   1.75    atatat 			errno = 0;
   1164  1.110  christos 			q = strtoll(value, &t, 0);
   1165   1.94    atatat 			if (t == value || *t != '\0' || errno != 0 ||
   1166   1.92    atatat 			    q < INT_MIN || q > UINT_MAX) {
   1167   1.92    atatat 				sysctlperror(
   1168   1.92    atatat 				    "%s: '%s' is not a valid mib number\n",
   1169   1.92    atatat 				    nname, value);
   1170  1.151  christos 				EXIT(EXIT_FAILURE);
   1171   1.75    atatat 			}
   1172   1.92    atatat 			node.sysctl_num = (int)q;
   1173   1.75    atatat 		}
   1174   1.75    atatat 		else if (strcmp(key, "flags") == 0) {
   1175  1.114  christos 			if (value == NULL) {
   1176  1.114  christos 				sysctlperror("%s: missing value\n", nname);
   1177  1.151  christos 				EXIT(EXIT_FAILURE);
   1178  1.114  christos 			}
   1179   1.75    atatat 			t = value;
   1180   1.75    atatat 			while (*t != '\0') {
   1181   1.75    atatat 				switch (*t) {
   1182   1.75    atatat 				case 'a':
   1183   1.83    atatat 					flags |= CTLFLAG_ANYWRITE;
   1184   1.75    atatat 					break;
   1185   1.75    atatat 				case 'h':
   1186   1.83    atatat 					flags |= CTLFLAG_HIDDEN;
   1187   1.75    atatat 					break;
   1188   1.75    atatat 				case 'i':
   1189   1.83    atatat 					flags |= CTLFLAG_IMMEDIATE;
   1190   1.75    atatat 					break;
   1191   1.75    atatat 				case 'o':
   1192   1.83    atatat 					flags |= CTLFLAG_OWNDATA;
   1193   1.75    atatat 					break;
   1194   1.75    atatat 				case 'p':
   1195   1.83    atatat 					flags |= CTLFLAG_PRIVATE;
   1196   1.75    atatat 					break;
   1197  1.143       dsl 				case 'u':
   1198  1.143       dsl 					flags |= CTLFLAG_UNSIGNED;
   1199  1.143       dsl 					break;
   1200   1.75    atatat 				case 'x':
   1201   1.83    atatat 					flags |= CTLFLAG_HEX;
   1202   1.75    atatat 					break;
   1203   1.75    atatat 
   1204   1.75    atatat 				case 'r':
   1205   1.83    atatat 					rw = CTLFLAG_READONLY;
   1206   1.75    atatat 					break;
   1207   1.75    atatat 				case 'w':
   1208   1.83    atatat 					rw = CTLFLAG_READWRITE;
   1209   1.75    atatat 					break;
   1210   1.75    atatat 				default:
   1211   1.92    atatat 					sysctlperror(
   1212   1.92    atatat 					   "%s: '%c' is not a valid flag\n",
   1213   1.92    atatat 					    nname, *t);
   1214  1.151  christos 					EXIT(EXIT_FAILURE);
   1215   1.75    atatat 				}
   1216   1.75    atatat 				t++;
   1217   1.75    atatat 			}
   1218   1.75    atatat 		}
   1219   1.75    atatat 		else {
   1220   1.92    atatat 			sysctlperror("%s: unrecognized keyword '%s'\n",
   1221   1.92    atatat 				     nname, key);
   1222  1.151  christos 			EXIT(EXIT_FAILURE);
   1223   1.75    atatat 		}
   1224   1.75    atatat 	}
   1225   1.75    atatat 
   1226   1.75    atatat 	/*
   1227   1.75    atatat 	 * now that we've finished parsing the given string, fill in
   1228   1.75    atatat 	 * anything they didn't specify
   1229   1.75    atatat 	 */
   1230   1.75    atatat 	if (type == 0)
   1231   1.75    atatat 		type = CTLTYPE_NODE;
   1232   1.75    atatat 
   1233   1.75    atatat 	/*
   1234   1.75    atatat 	 * the "data" can be interpreted various ways depending on the
   1235   1.75    atatat 	 * type of node we're creating, as can the size
   1236   1.75    atatat 	 */
   1237   1.75    atatat 	if (data != NULL) {
   1238   1.75    atatat 		if (addr != NULL) {
   1239   1.92    atatat 			sysctlperror(
   1240   1.92    atatat 				"%s: cannot specify both value and "
   1241   1.92    atatat 				"address\n", nname);
   1242  1.151  christos 			EXIT(EXIT_FAILURE);
   1243   1.75    atatat 		}
   1244   1.75    atatat 
   1245   1.75    atatat 		switch (type) {
   1246   1.75    atatat 		case CTLTYPE_INT:
   1247   1.75    atatat 			errno = 0;
   1248  1.110  christos 			q = strtoll(data, &t, 0);
   1249   1.94    atatat 			if (t == data || *t != '\0' || errno != 0 ||
   1250   1.92    atatat 				q < INT_MIN || q > UINT_MAX) {
   1251   1.92    atatat 				sysctlperror(
   1252   1.92    atatat 				    "%s: '%s' is not a valid integer\n",
   1253   1.92    atatat 				    nname, value);
   1254  1.151  christos 				EXIT(EXIT_FAILURE);
   1255   1.75    atatat 			}
   1256   1.92    atatat 			i = (int)q;
   1257   1.83    atatat 			if (!(flags & CTLFLAG_OWNDATA)) {
   1258   1.83    atatat 				flags |= CTLFLAG_IMMEDIATE;
   1259   1.75    atatat 				node.sysctl_idata = i;
   1260   1.75    atatat 			}
   1261   1.75    atatat 			else
   1262   1.75    atatat 				node.sysctl_data = &i;
   1263   1.75    atatat 			if (sz == 0)
   1264   1.75    atatat 				sz = sizeof(int);
   1265   1.75    atatat 			break;
   1266  1.131       mrg 		case CTLTYPE_BOOL:
   1267  1.131       mrg 			errno = 0;
   1268  1.131       mrg 			q = strtoll(data, &t, 0);
   1269  1.131       mrg 			if (t == data || *t != '\0' || errno != 0 ||
   1270  1.131       mrg 				(q != 0 && q != 1)) {
   1271  1.131       mrg 				sysctlperror(
   1272  1.131       mrg 				    "%s: '%s' is not a valid bool\n",
   1273  1.131       mrg 				    nname, value);
   1274  1.151  christos 				EXIT(EXIT_FAILURE);
   1275  1.131       mrg 			}
   1276  1.131       mrg 			b = q == 1;
   1277  1.131       mrg 			if (!(flags & CTLFLAG_OWNDATA)) {
   1278  1.131       mrg 				flags |= CTLFLAG_IMMEDIATE;
   1279  1.131       mrg 				node.sysctl_idata = b;
   1280  1.131       mrg 			}
   1281  1.131       mrg 			else
   1282  1.131       mrg 				node.sysctl_data = &b;
   1283  1.131       mrg 			if (sz == 0)
   1284  1.131       mrg 				sz = sizeof(bool);
   1285  1.131       mrg 			break;
   1286   1.75    atatat 		case CTLTYPE_STRING:
   1287   1.83    atatat 			flags |= CTLFLAG_OWNDATA;
   1288   1.75    atatat 			node.sysctl_data = data;
   1289   1.75    atatat 			if (sz == 0)
   1290   1.75    atatat 				sz = strlen(data) + 1;
   1291   1.75    atatat 			else if (sz < strlen(data) + 1) {
   1292   1.92    atatat 				sysctlperror("%s: ignoring size=%zu for "
   1293   1.75    atatat 					"string node, too small for given "
   1294   1.92    atatat 					"value\n", nname, sz);
   1295   1.75    atatat 				sz = strlen(data) + 1;
   1296   1.75    atatat 			}
   1297   1.75    atatat 			break;
   1298   1.75    atatat 		case CTLTYPE_QUAD:
   1299   1.75    atatat 			errno = 0;
   1300   1.92    atatat 			uq = strtouq(data, &t, 0);
   1301   1.94    atatat 			if (t == data || *t != '\0' || errno != 0) {
   1302   1.92    atatat 				sysctlperror(
   1303   1.92    atatat 					"%s: '%s' is not a valid quad\n",
   1304   1.92    atatat 					nname, value);
   1305  1.151  christos 				EXIT(EXIT_FAILURE);
   1306   1.75    atatat 			}
   1307   1.83    atatat 			if (!(flags & CTLFLAG_OWNDATA)) {
   1308   1.83    atatat 				flags |= CTLFLAG_IMMEDIATE;
   1309   1.92    atatat 				node.sysctl_qdata = uq;
   1310   1.75    atatat 			}
   1311   1.75    atatat 			else
   1312   1.92    atatat 				node.sysctl_data = &uq;
   1313   1.75    atatat 			if (sz == 0)
   1314   1.75    atatat 				sz = sizeof(u_quad_t);
   1315   1.75    atatat 			break;
   1316   1.75    atatat 		case CTLTYPE_STRUCT:
   1317   1.92    atatat 			sysctlperror("%s: struct not initializable\n",
   1318   1.92    atatat 				     nname);
   1319  1.151  christos 			EXIT(EXIT_FAILURE);
   1320   1.75    atatat 		}
   1321   1.75    atatat 
   1322   1.75    atatat 		/*
   1323   1.75    atatat 		 * these methods have all provided local starting
   1324   1.75    atatat 		 * values that the kernel must copy in
   1325   1.75    atatat 		 */
   1326   1.75    atatat 	}
   1327   1.75    atatat 
   1328   1.75    atatat 	/*
   1329   1.75    atatat 	 * hmm...no data, but we have an address of data.  that's
   1330   1.75    atatat 	 * fine.
   1331   1.75    atatat 	 */
   1332   1.75    atatat 	else if (addr != 0)
   1333   1.75    atatat 		node.sysctl_data = (void*)addr;
   1334   1.75    atatat 
   1335   1.75    atatat 	/*
   1336   1.75    atatat 	 * no data and no address?  well...okay.  we might be able to
   1337   1.75    atatat 	 * manage that.
   1338   1.75    atatat 	 */
   1339   1.75    atatat 	else if (type != CTLTYPE_NODE) {
   1340   1.75    atatat 		if (sz == 0) {
   1341   1.92    atatat 			sysctlperror(
   1342   1.92    atatat 			    "%s: need a size or a starting value\n",
   1343   1.92    atatat 			    nname);
   1344  1.151  christos                         EXIT(EXIT_FAILURE);
   1345   1.75    atatat                 }
   1346   1.83    atatat 		if (!(flags & CTLFLAG_IMMEDIATE))
   1347   1.83    atatat 			flags |= CTLFLAG_OWNDATA;
   1348   1.75    atatat 	}
   1349   1.75    atatat 
   1350   1.75    atatat 	/*
   1351   1.75    atatat 	 * now we do a few sanity checks on the description we've
   1352   1.75    atatat 	 * assembled
   1353   1.75    atatat 	 */
   1354   1.83    atatat 	if ((flags & CTLFLAG_IMMEDIATE) &&
   1355   1.75    atatat 	    (type == CTLTYPE_STRING || type == CTLTYPE_STRUCT)) {
   1356   1.92    atatat 		sysctlperror("%s: cannot make an immediate %s\n",
   1357   1.92    atatat 			     nname,
   1358   1.92    atatat 			     (type == CTLTYPE_STRING) ? "string" : "struct");
   1359  1.151  christos 		EXIT(EXIT_FAILURE);
   1360   1.75    atatat 	}
   1361   1.75    atatat 	if (type == CTLTYPE_NODE && node.sysctl_data != NULL) {
   1362   1.92    atatat 		sysctlperror("%s: nodes do not have data\n", nname);
   1363  1.151  christos 		EXIT(EXIT_FAILURE);
   1364   1.75    atatat 	}
   1365   1.75    atatat 
   1366   1.75    atatat 	/*
   1367   1.75    atatat 	 * some types must have a particular size
   1368   1.75    atatat 	 */
   1369   1.75    atatat 	if (sz != 0) {
   1370   1.75    atatat 		if ((type == CTLTYPE_INT && sz != sizeof(int)) ||
   1371  1.131       mrg 		    (type == CTLTYPE_BOOL && sz != sizeof(bool)) ||
   1372   1.75    atatat 		    (type == CTLTYPE_QUAD && sz != sizeof(u_quad_t)) ||
   1373   1.75    atatat 		    (type == CTLTYPE_NODE && sz != 0)) {
   1374   1.92    atatat 			sysctlperror("%s: wrong size for type\n", nname);
   1375  1.151  christos 			EXIT(EXIT_FAILURE);
   1376   1.75    atatat 		}
   1377   1.75    atatat 	}
   1378   1.75    atatat 	else if (type == CTLTYPE_STRUCT) {
   1379   1.92    atatat 		sysctlperror("%s: struct must have size\n", nname);
   1380  1.151  christos 		EXIT(EXIT_FAILURE);
   1381   1.75    atatat 	}
   1382   1.75    atatat 
   1383   1.75    atatat 	/*
   1384   1.75    atatat 	 * now...if no one said anything yet, we default nodes or
   1385   1.75    atatat 	 * any type that owns data being writeable, and everything
   1386   1.75    atatat 	 * else being readonly.
   1387   1.75    atatat 	 */
   1388   1.75    atatat 	if (rw == -1) {
   1389   1.75    atatat 		if (type == CTLTYPE_NODE ||
   1390   1.83    atatat 		    (flags & (CTLFLAG_OWNDATA|CTLFLAG_IMMEDIATE)))
   1391   1.83    atatat 			rw = CTLFLAG_READWRITE;
   1392   1.75    atatat 		else
   1393   1.83    atatat 			rw = CTLFLAG_READONLY;
   1394   1.75    atatat 	}
   1395   1.75    atatat 
   1396   1.75    atatat 	/*
   1397   1.75    atatat 	 * if a kernel address was specified, that can't be made
   1398   1.75    atatat 	 * writeable by us.
   1399   1.83    atatat 	if (rw != CTLFLAG_READONLY && addr) {
   1400   1.92    atatat 		sysctlperror("%s: kernel data can only be readable\n", nname);
   1401  1.151  christos 		EXIT(EXIT_FAILURE);
   1402   1.75    atatat 	}
   1403   1.75    atatat 	 */
   1404   1.75    atatat 
   1405   1.75    atatat 	/*
   1406   1.75    atatat 	 * what separator were they using in the full name of the new
   1407   1.75    atatat 	 * node?
   1408   1.75    atatat 	 */
   1409   1.75    atatat 	if ((t = strpbrk(nname, "./")) == NULL)
   1410   1.75    atatat 		sep[0] = '.';
   1411   1.75    atatat 	else
   1412   1.75    atatat 		sep[0] = t[0];
   1413   1.75    atatat 	sep[1] = '\0';
   1414   1.75    atatat 
   1415   1.75    atatat 	/*
   1416   1.75    atatat 	 * put it all together, now.  t'ain't much, is it?
   1417   1.75    atatat 	 */
   1418   1.83    atatat 	node.sysctl_flags = SYSCTL_VERSION|flags|rw|type;
   1419   1.75    atatat 	node.sysctl_size = sz;
   1420   1.75    atatat 	t = strrchr(nname, sep[0]);
   1421   1.75    atatat 	if (t != NULL)
   1422   1.75    atatat 		strlcpy(node.sysctl_name, t + 1, sizeof(node.sysctl_name));
   1423   1.75    atatat 	else
   1424   1.75    atatat 		strlcpy(node.sysctl_name, nname, sizeof(node.sysctl_name));
   1425   1.82    atatat 	if (t == nname)
   1426   1.82    atatat 		t = NULL;
   1427   1.75    atatat 
   1428   1.75    atatat 	/*
   1429   1.75    atatat 	 * if this is a new top-level node, then we don't need to find
   1430   1.75    atatat 	 * the mib for its parent
   1431   1.75    atatat 	 */
   1432   1.75    atatat 	if (t == NULL) {
   1433   1.75    atatat 		namelen = 0;
   1434   1.75    atatat 		gsname[0] = '\0';
   1435   1.75    atatat 	}
   1436   1.75    atatat 
   1437   1.75    atatat 	/*
   1438   1.75    atatat 	 * on the other hand, if it's not a top-level node...
   1439   1.75    atatat 	 */
   1440   1.75    atatat 	else {
   1441   1.75    atatat 		namelen = sizeof(name) / sizeof(name[0]);
   1442   1.75    atatat 		sz = sizeof(gsname);
   1443   1.75    atatat 		*t = '\0';
   1444  1.162     kamil 		rc = prog_sysctlgetmibinfo(nname, &name[0], &namelen,
   1445   1.83    atatat 				      gsname, &sz, NULL, SYSCTL_VERSION);
   1446   1.75    atatat 		*t = sep[0];
   1447   1.75    atatat 		if (rc == -1) {
   1448   1.92    atatat 			sysctlparseerror(namelen, nname);
   1449  1.151  christos 			EXIT(EXIT_FAILURE);
   1450    1.1       cgd 		}
   1451   1.75    atatat 	}
   1452   1.75    atatat 
   1453   1.75    atatat 	/*
   1454   1.75    atatat 	 * yes, a new node is being created
   1455   1.75    atatat 	 */
   1456   1.75    atatat 	if (method != 0)
   1457   1.75    atatat 		name[namelen++] = method;
   1458   1.75    atatat 	else
   1459   1.75    atatat 		name[namelen++] = CTL_CREATE;
   1460   1.75    atatat 
   1461   1.75    atatat 	sz = sizeof(node);
   1462  1.133     pooka 	rc = prog_sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node));
   1463   1.75    atatat 
   1464   1.75    atatat 	if (rc == -1) {
   1465   1.92    atatat 		sysctlperror("%s: CTL_CREATE failed: %s\n",
   1466   1.92    atatat 			     nname, strerror(errno));
   1467  1.151  christos 		EXIT(EXIT_FAILURE);
   1468   1.75    atatat 	}
   1469  1.100    atatat 	else {
   1470  1.100    atatat 		if (!qflag && !nflag)
   1471  1.100    atatat 			printf("%s(%s): (created)\n", nname, st(type));
   1472  1.100    atatat 		stale = 1;
   1473  1.100    atatat 	}
   1474   1.75    atatat }
   1475   1.75    atatat 
   1476   1.75    atatat static void
   1477   1.82    atatat parse_destroy(char *l)
   1478   1.75    atatat {
   1479   1.75    atatat 	struct sysctlnode node;
   1480   1.75    atatat 	size_t sz;
   1481   1.75    atatat 	int name[CTL_MAXNAME], rc;
   1482   1.75    atatat 	u_int namelen;
   1483   1.75    atatat 
   1484   1.82    atatat 	if (!wflag) {
   1485   1.92    atatat 		sysctlperror("Must specify -w to destroy nodes\n");
   1486  1.151  christos 		exit(EXIT_FAILURE);
   1487   1.82    atatat 	}
   1488   1.82    atatat 
   1489   1.75    atatat 	memset(name, 0, sizeof(name));
   1490   1.75    atatat 	namelen = sizeof(name) / sizeof(name[0]);
   1491   1.75    atatat 	sz = sizeof(gsname);
   1492  1.162     kamil 	rc = prog_sysctlgetmibinfo(l, &name[0], &namelen, gsname, &sz, NULL,
   1493   1.83    atatat 			      SYSCTL_VERSION);
   1494   1.75    atatat 	if (rc == -1) {
   1495   1.92    atatat 		sysctlparseerror(namelen, l);
   1496  1.151  christos 		EXIT(EXIT_FAILURE);
   1497   1.75    atatat 	}
   1498   1.75    atatat 
   1499   1.75    atatat 	memset(&node, 0, sizeof(node));
   1500   1.83    atatat 	node.sysctl_flags = SYSCTL_VERSION;
   1501   1.75    atatat 	node.sysctl_num = name[namelen - 1];
   1502   1.75    atatat 	name[namelen - 1] = CTL_DESTROY;
   1503   1.75    atatat 
   1504   1.75    atatat 	sz = sizeof(node);
   1505  1.133     pooka 	rc = prog_sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node));
   1506   1.75    atatat 
   1507   1.75    atatat 	if (rc == -1) {
   1508   1.92    atatat 		sysctlperror("%s: CTL_DESTROY failed: %s\n",
   1509   1.92    atatat 			     l, strerror(errno));
   1510  1.151  christos 		EXIT(EXIT_FAILURE);
   1511   1.75    atatat 	}
   1512  1.100    atatat 	else {
   1513  1.100    atatat 		if (!qflag && !nflag)
   1514  1.100    atatat 			printf("%s(%s): (destroyed)\n", gsname,
   1515  1.100    atatat 			       st(SYSCTL_TYPE(node.sysctl_flags)));
   1516  1.100    atatat 		stale = 1;
   1517  1.100    atatat 	}
   1518   1.75    atatat }
   1519   1.75    atatat 
   1520   1.84    atatat static void
   1521   1.84    atatat parse_describe(char *l)
   1522   1.84    atatat {
   1523   1.84    atatat 	struct sysctlnode newdesc;
   1524   1.84    atatat 	char buf[1024], *value;
   1525   1.84    atatat 	struct sysctldesc *d = (void*)&buf[0];
   1526   1.84    atatat 	int name[CTL_MAXNAME], rc;
   1527   1.84    atatat 	u_int namelen;
   1528   1.84    atatat 	size_t sz;
   1529   1.84    atatat 
   1530   1.84    atatat 	if (!wflag) {
   1531   1.92    atatat 		sysctlperror("Must specify -w to set descriptions\n");
   1532  1.151  christos 		exit(EXIT_FAILURE);
   1533   1.84    atatat 	}
   1534   1.84    atatat 
   1535   1.84    atatat 	value = strchr(l, '=');
   1536   1.84    atatat 	*value++ = '\0';
   1537   1.84    atatat 
   1538   1.84    atatat 	memset(name, 0, sizeof(name));
   1539   1.84    atatat 	namelen = sizeof(name) / sizeof(name[0]);
   1540   1.84    atatat 	sz = sizeof(gsname);
   1541  1.162     kamil 	rc = prog_sysctlgetmibinfo(l, &name[0], &namelen, gsname, &sz, NULL,
   1542   1.84    atatat 			      SYSCTL_VERSION);
   1543   1.84    atatat 	if (rc == -1) {
   1544   1.92    atatat 		sysctlparseerror(namelen, l);
   1545  1.151  christos 		EXIT(EXIT_FAILURE);
   1546   1.84    atatat 	}
   1547   1.84    atatat 
   1548   1.84    atatat 	sz = sizeof(buf);
   1549   1.84    atatat 	memset(&newdesc, 0, sizeof(newdesc));
   1550   1.84    atatat 	newdesc.sysctl_flags = SYSCTL_VERSION|CTLFLAG_OWNDESC;
   1551   1.84    atatat 	newdesc.sysctl_num = name[namelen - 1];
   1552   1.84    atatat 	newdesc.sysctl_desc = value;
   1553   1.84    atatat 	name[namelen - 1] = CTL_DESCRIBE;
   1554  1.133     pooka 	rc = prog_sysctl(name, namelen, d, &sz, &newdesc, sizeof(newdesc));
   1555   1.84    atatat 	if (rc == -1)
   1556   1.92    atatat 		sysctlperror("%s: CTL_DESCRIBE failed: %s\n",
   1557   1.92    atatat 			     gsname, strerror(errno));
   1558   1.84    atatat 	else if (d->descr_len == 1)
   1559   1.92    atatat 		sysctlperror("%s: description not set\n", gsname);
   1560   1.84    atatat 	else if (!qflag && !nflag)
   1561   1.84    atatat 		printf("%s: %s\n", gsname, d->descr_str);
   1562   1.84    atatat }
   1563   1.84    atatat 
   1564   1.75    atatat /*
   1565   1.75    atatat  * ********************************************************************
   1566   1.75    atatat  * when things go wrong...
   1567   1.75    atatat  * ********************************************************************
   1568   1.75    atatat  */
   1569   1.75    atatat static void
   1570   1.75    atatat usage(void)
   1571   1.75    atatat {
   1572   1.75    atatat 	const char *progname = getprogname();
   1573    1.1       cgd 
   1574   1.75    atatat 	(void)fprintf(stderr,
   1575   1.77      jmmv 		      "usage:\t%s %s\n"
   1576   1.75    atatat 		      "\t%s %s\n"
   1577   1.75    atatat 		      "\t%s %s\n"
   1578   1.75    atatat 		      "\t%s %s\n"
   1579   1.75    atatat 		      "\t%s %s\n"
   1580   1.75    atatat 		      "\t%s %s\n",
   1581  1.135  christos 		      progname, "[-dneq] [-x[x]|-r] variable ...",
   1582   1.75    atatat 		      progname, "[-ne] [-q] -w variable=value ...",
   1583   1.87    atatat 		      progname, "[-dne] -a",
   1584   1.87    atatat 		      progname, "[-dne] -A",
   1585   1.75    atatat 		      progname, "[-ne] -M",
   1586   1.87    atatat 		      progname, "[-dne] [-q] -f file");
   1587  1.151  christos 	exit(EXIT_FAILURE);
   1588   1.22    bouyer }
   1589   1.22    bouyer 
   1590   1.84    atatat static void
   1591   1.84    atatat getdesc1(int *name, u_int namelen, struct sysctlnode *pnode)
   1592   1.84    atatat {
   1593   1.84    atatat 	struct sysctlnode node;
   1594   1.84    atatat 	char buf[1024], *desc;
   1595   1.84    atatat 	struct sysctldesc *d = (void*)buf;
   1596   1.84    atatat 	size_t sz = sizeof(buf);
   1597   1.84    atatat 	int rc;
   1598   1.84    atatat 
   1599   1.84    atatat 	memset(&node, 0, sizeof(node));
   1600   1.84    atatat 	node.sysctl_flags = SYSCTL_VERSION;
   1601   1.84    atatat 	node.sysctl_num = name[namelen - 1];
   1602   1.84    atatat 	name[namelen - 1] = CTL_DESCRIBE;
   1603  1.133     pooka 	rc = prog_sysctl(name, namelen, d, &sz, &node, sizeof(node));
   1604   1.84    atatat 
   1605   1.84    atatat 	if (rc == -1 ||
   1606   1.84    atatat 	    d->descr_len == 1 ||
   1607   1.84    atatat 	    d->descr_num != pnode->sysctl_num ||
   1608   1.84    atatat 	    d->descr_ver != pnode->sysctl_ver)
   1609   1.84    atatat 		desc = (char *)-1;
   1610   1.84    atatat 	else
   1611   1.84    atatat 		desc = malloc(d->descr_len);
   1612   1.84    atatat 
   1613   1.84    atatat 	if (desc == NULL)
   1614   1.84    atatat 		desc = (char *)-1;
   1615   1.84    atatat 	if (desc != (char *)-1)
   1616   1.89    atatat 		memcpy(desc, &d->descr_str[0], d->descr_len);
   1617   1.84    atatat 	name[namelen - 1] = node.sysctl_num;
   1618   1.84    atatat 	if (pnode->sysctl_desc != NULL &&
   1619   1.84    atatat 	    pnode->sysctl_desc != (const char *)-1)
   1620  1.104  christos 		free(__UNCONST(pnode->sysctl_desc));
   1621   1.84    atatat 	pnode->sysctl_desc = desc;
   1622   1.84    atatat }
   1623   1.84    atatat 
   1624   1.84    atatat static void
   1625   1.84    atatat getdesc(int *name, u_int namelen, struct sysctlnode *pnode)
   1626   1.84    atatat {
   1627   1.84    atatat 	struct sysctlnode *node = pnode->sysctl_child;
   1628   1.92    atatat 	struct sysctldesc *d, *p, *plim;
   1629   1.84    atatat 	char *desc;
   1630  1.152    martin 	size_t i, sz, child_cnt;
   1631  1.128     lukem 	int rc;
   1632   1.84    atatat 
   1633   1.84    atatat 	sz = 128 * pnode->sysctl_clen;
   1634   1.84    atatat 	name[namelen] = CTL_DESCRIBE;
   1635   1.84    atatat 
   1636   1.84    atatat 	/*
   1637   1.84    atatat 	 * attempt *twice* to get the description chunk.  if two tries
   1638   1.84    atatat 	 * doesn't work, give up.
   1639   1.84    atatat 	 */
   1640   1.84    atatat 	i = 0;
   1641   1.84    atatat 	do {
   1642   1.93    atatat 		d = malloc(sz);
   1643   1.84    atatat 		if (d == NULL)
   1644   1.84    atatat 			return;
   1645  1.133     pooka 		rc = prog_sysctl(name, namelen + 1, d, &sz, NULL, 0);
   1646   1.84    atatat 		if (rc == -1) {
   1647   1.84    atatat 			free(d);
   1648   1.84    atatat 			d = NULL;
   1649   1.84    atatat 			if (i == 0 && errno == ENOMEM)
   1650   1.84    atatat 				i = 1;
   1651   1.84    atatat 			else
   1652   1.84    atatat 				return;
   1653   1.84    atatat 		}
   1654   1.84    atatat 	} while (d == NULL);
   1655   1.84    atatat 
   1656   1.84    atatat 	/*
   1657   1.84    atatat 	 * hokey nested loop here, giving O(n**2) behavior, but should
   1658   1.84    atatat 	 * suffice for now
   1659   1.84    atatat 	 */
   1660   1.92    atatat 	plim = /*LINTED ptr cast*/(struct sysctldesc *)((char*)d + sz);
   1661  1.152    martin 	child_cnt = (pnode->sysctl_flags & CTLTYPE_NODE) ? pnode->sysctl_clen
   1662  1.152    martin 	    : 0;
   1663  1.152    martin 	for (i = 0; i < child_cnt; i++) {
   1664   1.84    atatat 		node = &pnode->sysctl_child[i];
   1665   1.92    atatat 		for (p = d; p < plim; p = NEXT_DESCR(p))
   1666   1.84    atatat 			if (node->sysctl_num == p->descr_num)
   1667   1.84    atatat 				break;
   1668   1.99    atatat 		if (p < plim && node->sysctl_ver == p->descr_ver) {
   1669   1.84    atatat 			/*
   1670   1.84    atatat 			 * match found, attempt to attach description
   1671   1.84    atatat 			 */
   1672   1.84    atatat 			if (p->descr_len == 1)
   1673   1.84    atatat 				desc = NULL;
   1674   1.84    atatat 			else
   1675   1.84    atatat 				desc = malloc(p->descr_len);
   1676   1.84    atatat 			if (desc == NULL)
   1677   1.84    atatat 				desc = (char *)-1;
   1678   1.84    atatat 			else
   1679   1.90    atatat 				memcpy(desc, &p->descr_str[0], p->descr_len);
   1680   1.84    atatat 			node->sysctl_desc = desc;
   1681   1.84    atatat 		}
   1682   1.84    atatat 	}
   1683   1.84    atatat 
   1684   1.84    atatat 	free(d);
   1685   1.84    atatat }
   1686   1.84    atatat 
   1687   1.92    atatat static void
   1688   1.92    atatat trim_whitespace(char *s, int dir)
   1689   1.92    atatat {
   1690   1.92    atatat 	char *i, *o;
   1691   1.92    atatat 
   1692   1.92    atatat 	i = o = s;
   1693   1.92    atatat 	if (dir & 1)
   1694   1.92    atatat 		while (isspace((unsigned char)*i))
   1695   1.92    atatat 			i++;
   1696   1.92    atatat 	while ((*o++ = *i++) != '\0');
   1697   1.92    atatat 	o -= 2; /* already past nul, skip back to before it */
   1698   1.92    atatat 	if (dir & 2)
   1699   1.92    atatat 		while (o > s && isspace((unsigned char)*o))
   1700   1.92    atatat 			*o-- = '\0';
   1701   1.92    atatat }
   1702   1.92    atatat 
   1703   1.75    atatat void
   1704   1.75    atatat sysctlerror(int soft)
   1705   1.75    atatat {
   1706   1.75    atatat 	if (soft) {
   1707   1.75    atatat 		switch (errno) {
   1708   1.75    atatat 		case ENOENT:
   1709   1.75    atatat 		case ENOPROTOOPT:
   1710   1.75    atatat 		case ENOTDIR:
   1711   1.97    atatat 		case EINVAL:
   1712   1.75    atatat 		case EOPNOTSUPP:
   1713   1.75    atatat 		case EPROTONOSUPPORT:
   1714   1.75    atatat 			if (Aflag || req)
   1715  1.154  christos 				sysctlperror("%s: the value is not available "
   1716  1.154  christos 				    "(%s)\n", gsname, strerror(errno));
   1717   1.75    atatat 			return;
   1718   1.22    bouyer 		}
   1719   1.75    atatat 	}
   1720   1.75    atatat 
   1721  1.140  christos 	if (Aflag || req)
   1722  1.140  christos 		sysctlperror("%s: %s\n", gsname, strerror(errno));
   1723   1.75    atatat 	if (!soft)
   1724  1.151  christos 		EXIT(EXIT_FAILURE);
   1725   1.92    atatat }
   1726   1.92    atatat 
   1727   1.92    atatat void
   1728   1.92    atatat sysctlparseerror(u_int namelen, const char *pname)
   1729   1.92    atatat {
   1730   1.92    atatat 
   1731  1.135  christos 	if (qflag) {
   1732  1.135  christos 		errs++;
   1733  1.135  christos 		return;
   1734  1.135  christos 	}
   1735   1.92    atatat 	sysctlperror("%s level name '%s' in '%s' is invalid\n",
   1736   1.92    atatat 		     lname[namelen], gsname, pname);
   1737   1.92    atatat }
   1738   1.92    atatat 
   1739   1.92    atatat static void
   1740   1.92    atatat sysctlperror(const char *fmt, ...)
   1741   1.92    atatat {
   1742   1.92    atatat 	va_list ap;
   1743   1.92    atatat 
   1744   1.92    atatat 	(void)fprintf(warnfp, "%s: ", getprogname());
   1745   1.92    atatat 	if (fn)
   1746   1.92    atatat 		(void)fprintf(warnfp, "%s#%zu: ", fn, nr);
   1747   1.92    atatat 	va_start(ap, fmt);
   1748   1.92    atatat 	(void)vfprintf(warnfp, fmt, ap);
   1749   1.92    atatat 	va_end(ap);
   1750  1.121  christos 	errs++;
   1751   1.75    atatat }
   1752   1.75    atatat 
   1753   1.92    atatat 
   1754   1.75    atatat /*
   1755   1.75    atatat  * ********************************************************************
   1756   1.75    atatat  * how to write to a "simple" node
   1757   1.75    atatat  * ********************************************************************
   1758   1.75    atatat  */
   1759   1.75    atatat static void
   1760  1.158  dholland write_number(int *name, u_int namelen, struct sysctlnode *node, char *value,
   1761  1.158  dholland 	bool optional)
   1762   1.75    atatat {
   1763  1.110  christos 	u_int ii, io;
   1764   1.75    atatat 	u_quad_t qi, qo;
   1765   1.75    atatat 	size_t si, so;
   1766  1.131       mrg 	bool bi, bo;
   1767   1.75    atatat 	int rc;
   1768   1.75    atatat 	void *i, *o;
   1769   1.75    atatat 	char *t;
   1770   1.75    atatat 
   1771   1.92    atatat 	if (fn)
   1772   1.92    atatat 		trim_whitespace(value, 3);
   1773   1.92    atatat 
   1774   1.75    atatat 	si = so = 0;
   1775   1.75    atatat 	i = o = NULL;
   1776  1.131       mrg 	bi = bo = false;
   1777   1.75    atatat 	errno = 0;
   1778   1.75    atatat 	qi = strtouq(value, &t, 0);
   1779  1.110  christos 	if (qi == UQUAD_MAX && errno == ERANGE) {
   1780  1.110  christos 		sysctlperror("%s: %s\n", value, strerror(errno));
   1781  1.151  christos 		EXIT(EXIT_FAILURE);
   1782   1.75    atatat 	}
   1783   1.94    atatat 	if (t == value || *t != '\0') {
   1784   1.92    atatat 		sysctlperror("%s: not a number\n", value);
   1785  1.151  christos 		EXIT(EXIT_FAILURE);
   1786   1.75    atatat 	}
   1787   1.75    atatat 
   1788   1.75    atatat 	switch (SYSCTL_TYPE(node->sysctl_flags)) {
   1789  1.110  christos 	case CTLTYPE_INT:
   1790  1.110  christos 		ii = (u_int)qi;
   1791  1.111  christos 		io = (u_int)(qi >> 32);
   1792  1.111  christos 		if (io != (u_int)-1 && io != 0) {
   1793  1.110  christos 			sysctlperror("%s: %s\n", value, strerror(ERANGE));
   1794  1.151  christos 			EXIT(EXIT_FAILURE);
   1795   1.75    atatat 		}
   1796   1.75    atatat 		o = &io;
   1797   1.75    atatat 		so = sizeof(io);
   1798   1.75    atatat 		i = &ii;
   1799   1.75    atatat 		si = sizeof(ii);
   1800   1.75    atatat 		break;
   1801  1.131       mrg 	case CTLTYPE_BOOL:
   1802  1.131       mrg 		bi = (bool)qi;
   1803  1.131       mrg 		o = &bo;
   1804  1.131       mrg 		so = sizeof(bo);
   1805  1.131       mrg 		i = &bi;
   1806  1.131       mrg 		si = sizeof(bi);
   1807  1.131       mrg 		break;
   1808   1.75    atatat 	case CTLTYPE_QUAD:
   1809   1.75    atatat 		o = &qo;
   1810   1.75    atatat 		so = sizeof(qo);
   1811   1.75    atatat 		i = &qi;
   1812   1.75    atatat 		si = sizeof(qi);
   1813   1.75    atatat 		break;
   1814   1.75    atatat 	}
   1815   1.75    atatat 
   1816  1.133     pooka 	rc = prog_sysctl(name, namelen, o, &so, i, si);
   1817   1.92    atatat 	if (rc == -1) {
   1818  1.158  dholland 		if (!optional || errno != EPERM) {
   1819  1.158  dholland 			sysctlerror(0);
   1820  1.158  dholland 		}
   1821   1.92    atatat 		return;
   1822   1.92    atatat 	}
   1823   1.75    atatat 
   1824   1.75    atatat 	switch (SYSCTL_TYPE(node->sysctl_flags)) {
   1825   1.75    atatat 	case CTLTYPE_INT:
   1826   1.75    atatat 		display_number(node, gsname, &io, sizeof(io), DISPLAY_OLD);
   1827   1.75    atatat 		display_number(node, gsname, &ii, sizeof(ii), DISPLAY_NEW);
   1828   1.75    atatat 		break;
   1829  1.131       mrg 	case CTLTYPE_BOOL:
   1830  1.131       mrg 		display_number(node, gsname, &bo, sizeof(bo), DISPLAY_OLD);
   1831  1.131       mrg 		display_number(node, gsname, &bi, sizeof(bi), DISPLAY_NEW);
   1832  1.131       mrg 		break;
   1833   1.75    atatat 	case CTLTYPE_QUAD:
   1834   1.75    atatat 		display_number(node, gsname, &qo, sizeof(qo), DISPLAY_OLD);
   1835   1.75    atatat 		display_number(node, gsname, &qi, sizeof(qi), DISPLAY_NEW);
   1836   1.75    atatat 		break;
   1837   1.75    atatat 	}
   1838   1.75    atatat }
   1839   1.75    atatat 
   1840   1.75    atatat static void
   1841  1.158  dholland write_string(int *name, u_int namelen, struct sysctlnode *node, char *value,
   1842  1.158  dholland 	bool optional)
   1843   1.75    atatat {
   1844   1.75    atatat 	char *i, *o;
   1845   1.75    atatat 	size_t si, so;
   1846   1.75    atatat 	int rc;
   1847   1.75    atatat 
   1848   1.75    atatat 	i = value;
   1849   1.75    atatat 	si = strlen(i) + 1;
   1850   1.75    atatat 	so = node->sysctl_size;
   1851   1.75    atatat 	if (si > so && so != 0) {
   1852   1.92    atatat 		sysctlperror("%s: string too long\n", value);
   1853  1.151  christos 		EXIT(EXIT_FAILURE);
   1854   1.75    atatat 	}
   1855   1.75    atatat 	o = malloc(so);
   1856   1.75    atatat 	if (o == NULL) {
   1857   1.92    atatat 		sysctlperror("%s: !malloc failed!\n", gsname);
   1858  1.151  christos 		exit(EXIT_FAILURE);
   1859   1.75    atatat 	}
   1860   1.75    atatat 
   1861  1.133     pooka 	rc = prog_sysctl(name, namelen, o, &so, i, si);
   1862   1.92    atatat 	if (rc == -1) {
   1863  1.158  dholland 		if (!optional || errno != EPERM) {
   1864  1.158  dholland 			sysctlerror(0);
   1865  1.158  dholland 		}
   1866  1.158  dholland 		free(o);
   1867   1.92    atatat 		return;
   1868   1.92    atatat 	}
   1869   1.75    atatat 
   1870   1.75    atatat 	display_string(node, gsname, o, so, DISPLAY_OLD);
   1871   1.75    atatat 	display_string(node, gsname, i, si, DISPLAY_NEW);
   1872   1.75    atatat 	free(o);
   1873   1.75    atatat }
   1874   1.75    atatat 
   1875   1.75    atatat /*
   1876   1.75    atatat  * ********************************************************************
   1877   1.75    atatat  * simple ways to print stuff consistently
   1878   1.75    atatat  * ********************************************************************
   1879   1.75    atatat  */
   1880   1.75    atatat static void
   1881   1.75    atatat display_number(const struct sysctlnode *node, const char *name,
   1882   1.75    atatat 	       const void *data, size_t sz, int n)
   1883   1.75    atatat {
   1884   1.75    atatat 	u_quad_t q;
   1885  1.131       mrg 	bool b;
   1886   1.75    atatat 	int i;
   1887   1.75    atatat 
   1888   1.75    atatat 	if (qflag)
   1889   1.75    atatat 		return;
   1890   1.75    atatat 	if ((nflag || rflag) && (n == DISPLAY_OLD))
   1891   1.75    atatat 		return;
   1892   1.75    atatat 
   1893   1.75    atatat 	if (rflag && n != DISPLAY_OLD) {
   1894   1.75    atatat 		fwrite(data, sz, 1, stdout);
   1895   1.75    atatat 		return;
   1896   1.75    atatat 	}
   1897   1.75    atatat 
   1898   1.75    atatat 	if (!nflag) {
   1899   1.75    atatat 		if (n == DISPLAY_VALUE)
   1900   1.75    atatat 			printf("%s%s", name, eq);
   1901   1.75    atatat 		else if (n == DISPLAY_OLD)
   1902   1.75    atatat 			printf("%s: ", name);
   1903   1.75    atatat 	}
   1904   1.75    atatat 
   1905   1.75    atatat 	if (xflag > 1) {
   1906   1.79    atatat 		if (n != DISPLAY_NEW)
   1907   1.79    atatat 			printf("\n");
   1908   1.75    atatat 		hex_dump(data, sz);
   1909   1.22    bouyer 		return;
   1910   1.75    atatat 	}
   1911   1.22    bouyer 
   1912   1.75    atatat 	switch (SYSCTL_TYPE(node->sysctl_flags)) {
   1913   1.75    atatat 	case CTLTYPE_INT:
   1914   1.75    atatat 		memcpy(&i, data, sz);
   1915   1.75    atatat 		if (xflag)
   1916   1.75    atatat 			printf("0x%0*x", (int)sz * 2, i);
   1917   1.83    atatat 		else if (node->sysctl_flags & CTLFLAG_HEX)
   1918   1.75    atatat 			printf("%#x", i);
   1919  1.143       dsl 		else if (node->sysctl_flags & CTLFLAG_UNSIGNED)
   1920  1.143       dsl 			printf("%u", i);
   1921   1.75    atatat 		else
   1922   1.75    atatat 			printf("%d", i);
   1923   1.75    atatat 		break;
   1924  1.131       mrg 	case CTLTYPE_BOOL:
   1925  1.131       mrg 		memcpy(&b, data, sz);
   1926  1.131       mrg 		if (xflag)
   1927  1.131       mrg 			printf("0x%0*x", (int)sz * 2, b);
   1928  1.131       mrg 		else if (node->sysctl_flags & CTLFLAG_HEX)
   1929  1.131       mrg 			printf("%#x", b);
   1930  1.131       mrg 		else
   1931  1.131       mrg 			printf("%d", b);
   1932  1.131       mrg 		break;
   1933    1.1       cgd 	case CTLTYPE_QUAD:
   1934   1.75    atatat 		memcpy(&q, data, sz);
   1935   1.75    atatat 		if (xflag)
   1936   1.75    atatat 			printf("0x%0*" PRIx64, (int)sz * 2, q);
   1937   1.83    atatat 		else if (node->sysctl_flags & CTLFLAG_HEX)
   1938   1.75    atatat 			printf("%#" PRIx64, q);
   1939  1.143       dsl 		else if (node->sysctl_flags & CTLFLAG_UNSIGNED)
   1940  1.143       dsl 			printf("%" PRIu64, q);
   1941   1.75    atatat 		else
   1942   1.75    atatat 			printf("%" PRIu64, q);
   1943   1.75    atatat 		break;
   1944   1.75    atatat 	}
   1945   1.75    atatat 
   1946   1.75    atatat 	if (n == DISPLAY_OLD)
   1947   1.75    atatat 		printf(" -> ");
   1948   1.75    atatat 	else
   1949   1.75    atatat 		printf("\n");
   1950   1.75    atatat }
   1951   1.75    atatat 
   1952   1.75    atatat static void
   1953   1.75    atatat display_string(const struct sysctlnode *node, const char *name,
   1954   1.75    atatat 	       const void *data, size_t sz, int n)
   1955   1.75    atatat {
   1956   1.75    atatat 	const unsigned char *buf = data;
   1957   1.75    atatat 	int ni;
   1958   1.75    atatat 
   1959   1.75    atatat 	if (qflag)
   1960   1.75    atatat 		return;
   1961   1.75    atatat 	if ((nflag || rflag) && (n == DISPLAY_OLD))
   1962   1.75    atatat 		return;
   1963   1.75    atatat 
   1964   1.75    atatat 	if (rflag && n != DISPLAY_OLD) {
   1965   1.75    atatat 		fwrite(data, sz, 1, stdout);
   1966   1.75    atatat 		return;
   1967   1.75    atatat 	}
   1968   1.75    atatat 
   1969   1.75    atatat 	if (!nflag) {
   1970   1.75    atatat 		if (n == DISPLAY_VALUE)
   1971   1.75    atatat 			printf("%s%s", name, eq);
   1972   1.75    atatat 		else if (n == DISPLAY_OLD)
   1973   1.75    atatat 			printf("%s: ", name);
   1974   1.75    atatat 	}
   1975   1.75    atatat 
   1976   1.75    atatat 	if (xflag > 1) {
   1977   1.79    atatat 		if (n != DISPLAY_NEW)
   1978   1.79    atatat 			printf("\n");
   1979   1.75    atatat 		hex_dump(data, sz);
   1980   1.75    atatat 		return;
   1981   1.75    atatat 	}
   1982   1.75    atatat 
   1983   1.83    atatat 	if (xflag || node->sysctl_flags & CTLFLAG_HEX) {
   1984   1.75    atatat 		for (ni = 0; ni < (int)sz; ni++) {
   1985   1.75    atatat 			if (xflag)
   1986   1.75    atatat 				printf("%02x", buf[ni]);
   1987   1.75    atatat 			if (buf[ni] == '\0')
   1988   1.75    atatat 				break;
   1989   1.75    atatat 			if (!xflag)
   1990   1.75    atatat 				printf("\\x%2.2x", buf[ni]);
   1991    1.1       cgd 		}
   1992   1.75    atatat 	}
   1993   1.75    atatat 	else
   1994   1.75    atatat 		printf("%.*s", (int)sz, buf);
   1995   1.75    atatat 
   1996   1.75    atatat 	if (n == DISPLAY_OLD)
   1997   1.75    atatat 		printf(" -> ");
   1998   1.75    atatat 	else
   1999   1.75    atatat 		printf("\n");
   2000   1.75    atatat }
   2001   1.75    atatat 
   2002   1.75    atatat /*ARGSUSED*/
   2003   1.75    atatat static void
   2004   1.75    atatat display_struct(const struct sysctlnode *node, const char *name,
   2005   1.75    atatat 	       const void *data, size_t sz, int n)
   2006   1.75    atatat {
   2007   1.75    atatat 	const unsigned char *buf = data;
   2008   1.75    atatat 	int ni;
   2009   1.75    atatat 	size_t more;
   2010   1.75    atatat 
   2011   1.75    atatat 	if (qflag)
   2012   1.75    atatat 		return;
   2013   1.75    atatat 	if (!(xflag || rflag)) {
   2014   1.75    atatat 		if (Aflag || req)
   2015   1.92    atatat 			sysctlperror(
   2016   1.92    atatat 			    "%s: this type is unknown to this program\n",
   2017   1.92    atatat 			    gsname);
   2018   1.75    atatat 		return;
   2019   1.75    atatat 	}
   2020   1.75    atatat 	if ((nflag || rflag) && (n == DISPLAY_OLD))
   2021    1.1       cgd 		return;
   2022    1.1       cgd 
   2023   1.75    atatat 	if (rflag && n != DISPLAY_OLD) {
   2024   1.75    atatat 		fwrite(data, sz, 1, stdout);
   2025    1.1       cgd 		return;
   2026   1.75    atatat 	}
   2027    1.1       cgd 
   2028   1.75    atatat         if (!nflag) {
   2029   1.75    atatat                 if (n == DISPLAY_VALUE)
   2030   1.75    atatat                         printf("%s%s", name, eq);
   2031   1.75    atatat                 else if (n == DISPLAY_OLD)
   2032   1.75    atatat                         printf("%s: ", name);
   2033   1.75    atatat         }
   2034   1.75    atatat 
   2035   1.75    atatat 	if (xflag > 1) {
   2036   1.79    atatat 		if (n != DISPLAY_NEW)
   2037   1.79    atatat 			printf("\n");
   2038   1.75    atatat 		hex_dump(data, sz);
   2039    1.1       cgd 		return;
   2040    1.1       cgd 	}
   2041   1.75    atatat 
   2042   1.75    atatat 	if (sz > 16) {
   2043   1.75    atatat 		more = sz - 16;
   2044   1.75    atatat 		sz = 16;
   2045   1.75    atatat 	}
   2046   1.75    atatat 	else
   2047   1.75    atatat 		more = 0;
   2048   1.75    atatat 	for (ni = 0; ni < (int)sz; ni++)
   2049   1.75    atatat 		printf("%02x", buf[ni]);
   2050   1.75    atatat 	if (more)
   2051   1.75    atatat 		printf("...(%zu more bytes)", more);
   2052   1.75    atatat 	printf("\n");
   2053   1.75    atatat }
   2054   1.75    atatat 
   2055   1.75    atatat static void
   2056   1.75    atatat hex_dump(const unsigned char *buf, size_t len)
   2057   1.75    atatat {
   2058  1.128     lukem 	unsigned int i;
   2059  1.128     lukem 	int j;
   2060   1.75    atatat 	char line[80], tmp[12];
   2061   1.75    atatat 
   2062   1.75    atatat 	memset(line, ' ', sizeof(line));
   2063   1.75    atatat 	for (i = 0, j = 15; i < len; i++) {
   2064   1.75    atatat 		j = i % 16;
   2065   1.75    atatat 		/* reset line */
   2066   1.75    atatat 		if (j == 0) {
   2067   1.75    atatat 			line[58] = '|';
   2068   1.75    atatat 			line[77] = '|';
   2069   1.75    atatat 			line[78] = 0;
   2070  1.127  pgoyette 			snprintf(tmp, sizeof(tmp), "%07x", i);
   2071   1.75    atatat 			memcpy(&line[0], tmp, 7);
   2072   1.75    atatat 		}
   2073   1.75    atatat 		/* copy out hex version of byte */
   2074   1.75    atatat 		snprintf(tmp, sizeof(tmp), "%02x", buf[i]);
   2075   1.75    atatat 		memcpy(&line[9 + j * 3], tmp, 2);
   2076   1.75    atatat 		/* copy out plain version of byte */
   2077   1.75    atatat 		line[60 + j] = (isprint(buf[i])) ? buf[i] : '.';
   2078   1.75    atatat 		/* print a full line and erase it */
   2079   1.75    atatat 		if (j == 15) {
   2080   1.75    atatat 			printf("%s\n", line);
   2081   1.75    atatat 			memset(line, ' ', sizeof(line));
   2082   1.75    atatat 		}
   2083   1.75    atatat 	}
   2084   1.75    atatat 	if (line[0] != ' ')
   2085   1.75    atatat 		printf("%s\n", line);
   2086   1.75    atatat 	printf("%07zu bytes\n", len);
   2087    1.1       cgd }
   2088    1.1       cgd 
   2089    1.1       cgd /*
   2090   1.75    atatat  * ********************************************************************
   2091   1.75    atatat  * functions that handle particular nodes
   2092   1.75    atatat  * ********************************************************************
   2093    1.1       cgd  */
   2094   1.75    atatat /*ARGSUSED*/
   2095   1.14  christos static void
   2096   1.75    atatat printother(HANDLER_ARGS)
   2097    1.1       cgd {
   2098   1.75    atatat 	int rc;
   2099   1.75    atatat 	void *p;
   2100   1.75    atatat 	size_t sz1, sz2;
   2101    1.1       cgd 
   2102   1.75    atatat 	if (!(Aflag || req) || Mflag)
   2103    1.1       cgd 		return;
   2104   1.75    atatat 
   2105   1.75    atatat 	/*
   2106   1.75    atatat 	 * okay...you asked for it, so let's give it a go
   2107   1.75    atatat 	 */
   2108   1.75    atatat 	while (type != CTLTYPE_NODE && (xflag || rflag)) {
   2109  1.133     pooka 		rc = prog_sysctl(name, namelen, NULL, &sz1, NULL, 0);
   2110   1.75    atatat 		if (rc == -1 || sz1 == 0)
   2111   1.75    atatat 			break;
   2112   1.75    atatat 		p = malloc(sz1);
   2113   1.75    atatat 		if (p == NULL)
   2114   1.75    atatat 			break;
   2115   1.75    atatat 		sz2 = sz1;
   2116  1.133     pooka 		rc = prog_sysctl(name, namelen, p, &sz2, NULL, 0);
   2117   1.75    atatat 		if (rc == -1 || sz1 != sz2) {
   2118   1.75    atatat 			free(p);
   2119   1.75    atatat 			break;
   2120   1.75    atatat 		}
   2121   1.75    atatat 		display_struct(pnode, gsname, p, sz1, DISPLAY_VALUE);
   2122   1.75    atatat 		free(p);
   2123   1.75    atatat 		return;
   2124   1.75    atatat 	}
   2125   1.75    atatat 
   2126   1.75    atatat 	/*
   2127   1.75    atatat 	 * that didn't work...do we have a specific message for this
   2128   1.75    atatat 	 * thing?
   2129   1.75    atatat 	 */
   2130   1.75    atatat 	if (v != NULL) {
   2131   1.92    atatat 		sysctlperror("%s: use '%s' to view this information\n",
   2132   1.92    atatat 			     gsname, (const char *)v);
   2133   1.75    atatat 		return;
   2134   1.75    atatat 	}
   2135   1.75    atatat 
   2136   1.75    atatat 	/*
   2137   1.75    atatat 	 * hmm...i wonder if we have any generic hints?
   2138   1.75    atatat 	 */
   2139   1.75    atatat 	switch (name[0]) {
   2140   1.75    atatat 	case CTL_NET:
   2141   1.92    atatat 		sysctlperror("%s: use 'netstat' to view this information\n",
   2142   1.92    atatat 			     sname);
   2143   1.75    atatat 		break;
   2144   1.75    atatat 	case CTL_DEBUG:
   2145   1.92    atatat 		sysctlperror("%s: missing 'options DEBUG' from kernel?\n",
   2146   1.92    atatat 			     sname);
   2147   1.75    atatat 		break;
   2148   1.75    atatat 	case CTL_DDB:
   2149   1.92    atatat 		sysctlperror("%s: missing 'options DDB' from kernel?\n",
   2150   1.92    atatat 			     sname);
   2151   1.75    atatat 		break;
   2152   1.98    atatat 	case CTL_VENDOR:
   2153   1.98    atatat 		sysctlperror("%s: no vendor extensions installed\n",
   2154   1.98    atatat 			     sname);
   2155   1.98    atatat 		break;
   2156    1.1       cgd 	}
   2157    1.1       cgd }
   2158    1.1       cgd 
   2159   1.75    atatat /*ARGSUSED*/
   2160   1.75    atatat static void
   2161   1.75    atatat kern_clockrate(HANDLER_ARGS)
   2162   1.75    atatat {
   2163   1.75    atatat 	struct clockinfo clkinfo;
   2164   1.75    atatat 	size_t sz;
   2165   1.75    atatat 	int rc;
   2166   1.75    atatat 
   2167   1.75    atatat 	sz = sizeof(clkinfo);
   2168  1.133     pooka 	rc = prog_sysctl(name, namelen, &clkinfo, &sz, NULL, 0);
   2169   1.75    atatat 	if (rc == -1) {
   2170   1.75    atatat 		sysctlerror(1);
   2171   1.75    atatat 		return;
   2172   1.75    atatat 	}
   2173   1.75    atatat 	if (sz != sizeof(clkinfo))
   2174  1.151  christos 		errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
   2175   1.74  jonathan 
   2176   1.79    atatat 	if (xflag || rflag) {
   2177   1.75    atatat 		display_struct(pnode, sname, &clkinfo, sz,
   2178   1.75    atatat 			       DISPLAY_VALUE);
   2179   1.79    atatat 		return;
   2180   1.79    atatat 	}
   2181   1.75    atatat 	else if (!nflag)
   2182   1.75    atatat 		printf("%s: ", sname);
   2183   1.75    atatat 	printf("tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n",
   2184   1.75    atatat 	       clkinfo.tick, clkinfo.tickadj,
   2185   1.75    atatat 	       clkinfo.hz, clkinfo.profhz, clkinfo.stathz);
   2186   1.75    atatat }
   2187    1.1       cgd 
   2188   1.75    atatat /*ARGSUSED*/
   2189   1.75    atatat static void
   2190   1.75    atatat kern_boottime(HANDLER_ARGS)
   2191    1.1       cgd {
   2192  1.161       kre 	struct timespec timespec;
   2193   1.75    atatat 	time_t boottime;
   2194   1.75    atatat 	size_t sz;
   2195   1.75    atatat 	int rc;
   2196   1.75    atatat 
   2197  1.161       kre 	sz = sizeof(timespec);
   2198  1.161       kre 	rc = prog_sysctl(name, namelen, &timespec, &sz, NULL, 0);
   2199   1.75    atatat 	if (rc == -1) {
   2200   1.75    atatat 		sysctlerror(1);
   2201   1.75    atatat 		return;
   2202   1.75    atatat 	}
   2203  1.161       kre 	if (sz != sizeof(timespec))
   2204  1.151  christos 		errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
   2205    1.1       cgd 
   2206  1.161       kre 	boottime = timespec.tv_sec;
   2207   1.75    atatat 	if (xflag || rflag)
   2208  1.161       kre 		display_struct(pnode, sname, &timespec, sz,
   2209   1.75    atatat 			       DISPLAY_VALUE);
   2210   1.75    atatat 	else if (!nflag)
   2211   1.75    atatat 		/* ctime() provides the \n */
   2212   1.75    atatat 		printf("%s%s%s", sname, eq, ctime(&boottime));
   2213   1.75    atatat 	else if (nflag == 1)
   2214  1.161       kre 		printf("%lld\n", (long long)boottime);
   2215   1.75    atatat 	else
   2216  1.161       kre 		printf("%lld.%9.9ld\n", (long long)timespec.tv_sec,
   2217  1.161       kre 		       timespec.tv_nsec);
   2218   1.75    atatat }
   2219   1.20    itojun 
   2220   1.75    atatat /*ARGSUSED*/
   2221   1.75    atatat static void
   2222   1.75    atatat kern_consdev(HANDLER_ARGS)
   2223   1.20    itojun {
   2224   1.75    atatat 	dev_t cons;
   2225   1.75    atatat 	size_t sz;
   2226   1.75    atatat 	int rc;
   2227   1.75    atatat 
   2228   1.75    atatat 	sz = sizeof(cons);
   2229  1.133     pooka 	rc = prog_sysctl(name, namelen, &cons, &sz, NULL, 0);
   2230   1.75    atatat 	if (rc == -1) {
   2231   1.75    atatat 		sysctlerror(1);
   2232   1.75    atatat 		return;
   2233   1.75    atatat 	}
   2234   1.75    atatat 	if (sz != sizeof(cons))
   2235  1.151  christos 		errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
   2236   1.20    itojun 
   2237   1.75    atatat 	if (xflag || rflag)
   2238   1.75    atatat 		display_struct(pnode, sname, &cons, sz,
   2239   1.75    atatat 			       DISPLAY_VALUE);
   2240  1.101  christos 	else {
   2241  1.101  christos 		if (!nflag)
   2242  1.101  christos 			printf("%s%s", sname, eq);
   2243  1.102  christos 		if (nflag < 2 && (sname = devname(cons, S_IFCHR)) != NULL)
   2244  1.102  christos 			printf("%s\n", sname);
   2245  1.101  christos 		else
   2246  1.125  christos 			printf("0x%llx\n", (unsigned long long)cons);
   2247  1.101  christos 	}
   2248   1.75    atatat }
   2249   1.17   thorpej 
   2250   1.75    atatat /*ARGSUSED*/
   2251   1.75    atatat static void
   2252   1.75    atatat kern_cp_time(HANDLER_ARGS)
   2253   1.17   thorpej {
   2254   1.80    atatat 	u_int64_t *cp_time;
   2255   1.80    atatat 	size_t sz, osz;
   2256   1.80    atatat 	int rc, i, n;
   2257   1.80    atatat 	char s[sizeof("kern.cp_time.nnnnnn")];
   2258   1.80    atatat 	const char *tname;
   2259   1.80    atatat 
   2260   1.80    atatat 	/*
   2261   1.80    atatat 	 * three things to do here.
   2262   1.80    atatat 	 * case 1: get sum (no Aflag and namelen == 2)
   2263   1.80    atatat 	 * case 2: get specific processor (namelen == 3)
   2264   1.80    atatat 	 * case 3: get all processors (Aflag and namelen == 2)
   2265   1.80    atatat 	 */
   2266   1.80    atatat 
   2267   1.80    atatat 	if (namelen == 2 && Aflag) {
   2268   1.80    atatat 		sz = sizeof(n);
   2269  1.162     kamil 		rc = prog_sysctlbyname("hw.ncpu", &n, &sz, NULL, 0);
   2270   1.80    atatat 		if (rc != 0)
   2271   1.80    atatat 			return; /* XXX print an error, eh? */
   2272   1.81    atatat 		n++; /* Add on space for the sum. */
   2273   1.80    atatat 		sz = n * sizeof(u_int64_t) * CPUSTATES;
   2274   1.80    atatat 	}
   2275   1.80    atatat 	else {
   2276   1.81    atatat 		n = -1; /* Just print one data set. */
   2277   1.80    atatat 		sz = sizeof(u_int64_t) * CPUSTATES;
   2278   1.80    atatat 	}
   2279   1.80    atatat 
   2280   1.80    atatat 	cp_time = malloc(sz);
   2281   1.80    atatat 	if (cp_time == NULL) {
   2282   1.80    atatat 		sysctlerror(1);
   2283   1.80    atatat 		return;
   2284   1.80    atatat 	}
   2285   1.81    atatat 
   2286   1.80    atatat 	osz = sz;
   2287  1.133     pooka 	rc = prog_sysctl(name, namelen, cp_time + (n != -1) * CPUSTATES, &osz,
   2288   1.81    atatat 		    NULL, 0);
   2289   1.75    atatat 
   2290   1.75    atatat 	if (rc == -1) {
   2291   1.75    atatat 		sysctlerror(1);
   2292   1.80    atatat 		free(cp_time);
   2293   1.75    atatat 		return;
   2294   1.75    atatat 	}
   2295   1.81    atatat 
   2296   1.81    atatat 	/*
   2297   1.81    atatat 	 * Check, but account for space we'll occupy with the sum.
   2298   1.81    atatat 	 */
   2299   1.81    atatat 	if (osz != sz - (n != -1) * CPUSTATES * sizeof(u_int64_t))
   2300  1.151  christos 		errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
   2301   1.17   thorpej 
   2302   1.81    atatat 	/*
   2303   1.81    atatat 	 * Compute the actual sum.  Two calls would be easier (we
   2304   1.81    atatat 	 * could just call ourselves recursively above), but the
   2305   1.81    atatat 	 * numbers wouldn't add up.
   2306   1.81    atatat 	 */
   2307   1.81    atatat 	if (n != -1) {
   2308   1.81    atatat 		memset(cp_time, 0, sizeof(u_int64_t) * CPUSTATES);
   2309   1.81    atatat 		for (i = 1; i < n; i++) {
   2310   1.81    atatat 			cp_time[CP_USER] += cp_time[i * CPUSTATES + CP_USER];
   2311   1.81    atatat                         cp_time[CP_NICE] += cp_time[i * CPUSTATES + CP_NICE];
   2312   1.81    atatat                         cp_time[CP_SYS] += cp_time[i * CPUSTATES + CP_SYS];
   2313   1.81    atatat                         cp_time[CP_INTR] += cp_time[i * CPUSTATES + CP_INTR];
   2314   1.81    atatat                         cp_time[CP_IDLE] += cp_time[i * CPUSTATES + CP_IDLE];
   2315   1.81    atatat 		}
   2316   1.81    atatat 	}
   2317   1.81    atatat 
   2318   1.80    atatat 	tname = sname;
   2319   1.80    atatat 	for (i = 0; n == -1 || i < n; i++) {
   2320   1.81    atatat 		if (i > 0) {
   2321   1.81    atatat 			(void)snprintf(s, sizeof(s), "%s%s%d", sname, sep,
   2322   1.81    atatat 				       i - 1);
   2323   1.80    atatat 			tname = s;
   2324   1.80    atatat 		}
   2325   1.80    atatat 		if (xflag || rflag)
   2326   1.80    atatat 			display_struct(pnode, tname, cp_time + (i * CPUSTATES),
   2327   1.80    atatat 				       sizeof(u_int64_t) * CPUSTATES,
   2328   1.80    atatat 				       DISPLAY_VALUE);
   2329   1.80    atatat 		else {
   2330   1.80    atatat 			if (!nflag)
   2331   1.80    atatat 				printf("%s: ", tname);
   2332   1.80    atatat 			printf("user = %" PRIu64
   2333   1.80    atatat 			       ", nice = %" PRIu64
   2334   1.80    atatat 			       ", sys = %" PRIu64
   2335   1.80    atatat 			       ", intr = %" PRIu64
   2336   1.80    atatat 			       ", idle = %" PRIu64
   2337   1.80    atatat 			       "\n",
   2338   1.80    atatat 			       cp_time[i * CPUSTATES + CP_USER],
   2339   1.80    atatat 			       cp_time[i * CPUSTATES + CP_NICE],
   2340   1.80    atatat 			       cp_time[i * CPUSTATES + CP_SYS],
   2341   1.80    atatat 			       cp_time[i * CPUSTATES + CP_INTR],
   2342   1.80    atatat 			       cp_time[i * CPUSTATES + CP_IDLE]);
   2343   1.80    atatat 		}
   2344   1.81    atatat 		/*
   2345   1.81    atatat 		 * Just printing the one node.
   2346   1.81    atatat 		 */
   2347   1.80    atatat 		if (n == -1)
   2348   1.80    atatat 			break;
   2349   1.79    atatat 	}
   2350   1.80    atatat 
   2351   1.80    atatat 	free(cp_time);
   2352   1.17   thorpej }
   2353   1.17   thorpej 
   2354   1.75    atatat /*ARGSUSED*/
   2355   1.75    atatat static void
   2356  1.118  christos kern_drivers(HANDLER_ARGS)
   2357  1.118  christos {
   2358  1.118  christos 	struct kinfo_drivers *kd;
   2359  1.118  christos 	size_t sz, i;
   2360  1.118  christos 	int rc;
   2361  1.118  christos 	const char *comma;
   2362  1.118  christos 
   2363  1.133     pooka 	rc = prog_sysctl(name, namelen, NULL, &sz, NULL, 0);
   2364  1.118  christos 	if (rc == -1) {
   2365  1.118  christos 		sysctlerror(1);
   2366  1.118  christos 		return;
   2367  1.118  christos 	}
   2368  1.118  christos 
   2369  1.118  christos 	if (sz % sizeof(*kd))
   2370  1.151  christos 		err(EXIT_FAILURE, "bad size %zu for kern.drivers", sz);
   2371  1.118  christos 
   2372  1.118  christos 	kd = malloc(sz);
   2373  1.118  christos 	if (kd == NULL) {
   2374  1.118  christos 		sysctlerror(1);
   2375  1.118  christos 		return;
   2376  1.118  christos 	}
   2377  1.118  christos 
   2378  1.133     pooka 	rc = prog_sysctl(name, namelen, kd, &sz, NULL, 0);
   2379  1.118  christos 	if (rc == -1) {
   2380  1.118  christos 		sysctlerror(1);
   2381  1.149   msaitoh 		free(kd);
   2382  1.118  christos 		return;
   2383  1.118  christos 	}
   2384  1.118  christos 
   2385  1.118  christos 	comma = "";
   2386  1.119  christos 	if (!nflag)
   2387  1.119  christos 		printf("%s%s", sname, eq);
   2388  1.118  christos 	for (i = 0, sz /= sizeof(*kd); i < sz; i++) {
   2389  1.118  christos 		(void)printf("%s[%d %d %s]", comma, kd[i].d_cmajor,
   2390  1.118  christos 		    kd[i].d_bmajor, kd[i].d_name);
   2391  1.118  christos 		comma = ", ";
   2392  1.118  christos 	}
   2393  1.118  christos 	(void)printf("\n");
   2394  1.118  christos 	free(kd);
   2395  1.118  christos }
   2396  1.118  christos 
   2397  1.118  christos /*ARGSUSED*/
   2398  1.118  christos static void
   2399  1.103  christos kern_cp_id(HANDLER_ARGS)
   2400  1.103  christos {
   2401  1.103  christos 	u_int64_t *cp_id;
   2402  1.103  christos 	size_t sz, osz;
   2403  1.103  christos 	int rc, i, n;
   2404  1.103  christos 	char s[sizeof("kern.cp_id.nnnnnn")];
   2405  1.103  christos 	const char *tname;
   2406  1.103  christos 	struct sysctlnode node = *pnode;
   2407  1.103  christos 
   2408  1.103  christos 	/*
   2409  1.103  christos 	 * three things to do here.
   2410  1.103  christos 	 * case 1: print a specific cpu id (namelen == 3)
   2411  1.103  christos 	 * case 2: print all cpu ids separately (Aflag set)
   2412  1.103  christos 	 * case 3: print all cpu ids on one line
   2413  1.103  christos 	 */
   2414  1.103  christos 
   2415  1.103  christos 	if (namelen == 2) {
   2416  1.103  christos 		sz = sizeof(n);
   2417  1.162     kamil 		rc = prog_sysctlbyname("hw.ncpu", &n, &sz, NULL, 0);
   2418  1.103  christos 		if (rc != 0)
   2419  1.103  christos 			return; /* XXX print an error, eh? */
   2420  1.103  christos 		sz = n * sizeof(u_int64_t);
   2421  1.103  christos 	}
   2422  1.103  christos 	else {
   2423  1.103  christos 		n = -1; /* Just print one cpu id. */
   2424  1.103  christos 		sz = sizeof(u_int64_t);
   2425  1.103  christos 	}
   2426  1.103  christos 
   2427  1.103  christos 	cp_id = malloc(sz);
   2428  1.103  christos 	if (cp_id == NULL) {
   2429  1.103  christos 		sysctlerror(1);
   2430  1.103  christos 		return;
   2431  1.103  christos 	}
   2432  1.103  christos 
   2433  1.103  christos 	osz = sz;
   2434  1.133     pooka 	rc = prog_sysctl(name, namelen, cp_id, &osz, NULL, 0);
   2435  1.103  christos 	if (rc == -1) {
   2436  1.103  christos 		sysctlerror(1);
   2437  1.103  christos 		free(cp_id);
   2438  1.103  christos 		return;
   2439  1.103  christos 	}
   2440  1.103  christos 
   2441  1.103  christos 	/*
   2442  1.103  christos 	 * Check that we got back what we asked for.
   2443  1.103  christos 	 */
   2444  1.103  christos 	if (osz != sz)
   2445  1.151  christos 		errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
   2446  1.103  christos 
   2447  1.103  christos 	/* pretend for output purposes */
   2448  1.103  christos 	node.sysctl_flags = SYSCTL_FLAGS(pnode->sysctl_flags) |
   2449  1.103  christos 		SYSCTL_TYPE(CTLTYPE_QUAD);
   2450  1.103  christos 
   2451  1.103  christos 	tname = sname;
   2452  1.103  christos 	if (namelen == 3)
   2453  1.103  christos 		display_number(&node, tname, cp_id,
   2454  1.103  christos 			       sizeof(u_int64_t),
   2455  1.103  christos 			       DISPLAY_VALUE);
   2456  1.103  christos 	else if (Aflag) {
   2457  1.160       mrg 		for (i = 0; i < n; i++) {
   2458  1.103  christos 			(void)snprintf(s, sizeof(s), "%s%s%d", sname, sep, i);
   2459  1.103  christos 			tname = s;
   2460  1.103  christos 			display_number(&node, tname, &cp_id[i],
   2461  1.103  christos 				       sizeof(u_int64_t),
   2462  1.103  christos 				       DISPLAY_VALUE);
   2463  1.160       mrg 		}
   2464  1.103  christos 	}
   2465  1.103  christos 	else {
   2466  1.103  christos 		if (xflag || rflag)
   2467  1.103  christos 			display_struct(pnode, tname, cp_id, sz, DISPLAY_VALUE);
   2468  1.103  christos 		else {
   2469  1.103  christos 			if (!nflag)
   2470  1.103  christos 				printf("%s: ", tname);
   2471  1.103  christos 			for (i = 0; i < n; i++) {
   2472  1.103  christos 				if (i)
   2473  1.103  christos 					printf(", ");
   2474  1.103  christos 				printf("%d = %" PRIu64, i, cp_id[i]);
   2475  1.103  christos 			}
   2476  1.103  christos 			printf("\n");
   2477  1.103  christos 		}
   2478  1.103  christos 	}
   2479  1.103  christos 
   2480  1.103  christos 	free(cp_id);
   2481  1.103  christos }
   2482  1.103  christos 
   2483  1.103  christos /*ARGSUSED*/
   2484  1.103  christos static void
   2485   1.75    atatat vm_loadavg(HANDLER_ARGS)
   2486   1.22    bouyer {
   2487   1.75    atatat 	struct loadavg loadavg;
   2488   1.75    atatat 	size_t sz;
   2489   1.75    atatat 	int rc;
   2490   1.75    atatat 
   2491   1.75    atatat 	sz = sizeof(loadavg);
   2492  1.133     pooka 	rc = prog_sysctl(name, namelen, &loadavg, &sz, NULL, 0);
   2493   1.75    atatat 	if (rc == -1) {
   2494   1.75    atatat 		sysctlerror(1);
   2495   1.75    atatat 		return;
   2496   1.75    atatat 	}
   2497   1.75    atatat 	if (sz != sizeof(loadavg))
   2498  1.151  christos 		errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
   2499   1.75    atatat 
   2500   1.79    atatat 	if (xflag || rflag) {
   2501   1.75    atatat 		display_struct(pnode, sname, &loadavg, sz,
   2502   1.75    atatat 			       DISPLAY_VALUE);
   2503   1.79    atatat 		return;
   2504   1.79    atatat 	}
   2505   1.79    atatat 	if (!nflag)
   2506   1.75    atatat 		printf("%s: ", sname);
   2507   1.75    atatat 	printf("%.2f %.2f %.2f\n",
   2508   1.75    atatat 	       (double) loadavg.ldavg[0] / loadavg.fscale,
   2509   1.75    atatat 	       (double) loadavg.ldavg[1] / loadavg.fscale,
   2510   1.75    atatat 	       (double) loadavg.ldavg[2] / loadavg.fscale);
   2511    1.1       cgd }
   2512    1.1       cgd 
   2513   1.75    atatat /*ARGSUSED*/
   2514   1.75    atatat static void
   2515   1.75    atatat proc_limit(HANDLER_ARGS)
   2516    1.1       cgd {
   2517   1.75    atatat 	u_quad_t olim, *newp, nlim;
   2518   1.75    atatat 	size_t osz, nsz;
   2519   1.75    atatat 	char *t;
   2520   1.75    atatat 	int rc;
   2521   1.75    atatat 
   2522   1.92    atatat 	if (fn)
   2523   1.92    atatat 		trim_whitespace(value, 3);
   2524   1.92    atatat 
   2525   1.75    atatat 	osz = sizeof(olim);
   2526   1.75    atatat 	if (value != NULL) {
   2527   1.75    atatat 		nsz = sizeof(nlim);
   2528   1.75    atatat 		newp = &nlim;
   2529   1.75    atatat 		if (strcmp(value, "unlimited") == 0)
   2530   1.75    atatat 			nlim = RLIM_INFINITY;
   2531   1.75    atatat 		else {
   2532   1.75    atatat 			errno = 0;
   2533   1.75    atatat 			nlim = strtouq(value, &t, 0);
   2534   1.94    atatat 			if (t == value || *t != '\0' || errno != 0) {
   2535   1.92    atatat 				sysctlperror("%s: '%s' is not a valid limit\n",
   2536   1.92    atatat 					     sname, value);
   2537  1.151  christos 				EXIT(EXIT_FAILURE);
   2538   1.75    atatat 			}
   2539   1.75    atatat 		}
   2540   1.75    atatat 	}
   2541   1.75    atatat 	else {
   2542   1.75    atatat 		nsz = 0;
   2543   1.75    atatat 		newp = NULL;
   2544   1.75    atatat 	}
   2545   1.75    atatat 
   2546  1.133     pooka 	rc = prog_sysctl(name, namelen, &olim, &osz, newp, nsz);
   2547   1.75    atatat 	if (rc == -1) {
   2548   1.75    atatat 		sysctlerror(newp == NULL);
   2549   1.75    atatat 		return;
   2550   1.75    atatat 	}
   2551   1.75    atatat 
   2552   1.75    atatat 	if (newp && qflag)
   2553   1.75    atatat 		return;
   2554   1.75    atatat 
   2555   1.75    atatat 	if (rflag || xflag || olim != RLIM_INFINITY)
   2556   1.75    atatat 		display_number(pnode, sname, &olim, sizeof(olim),
   2557   1.75    atatat 			       newp ? DISPLAY_OLD : DISPLAY_VALUE);
   2558   1.75    atatat 	else
   2559   1.75    atatat 		display_string(pnode, sname, "unlimited", 10,
   2560   1.75    atatat 			       newp ? DISPLAY_OLD : DISPLAY_VALUE);
   2561    1.1       cgd 
   2562   1.75    atatat 	if (newp) {
   2563   1.75    atatat 		if (rflag || xflag || nlim != RLIM_INFINITY)
   2564   1.75    atatat 			display_number(pnode, sname, &nlim, sizeof(nlim),
   2565   1.75    atatat 				       DISPLAY_NEW);
   2566   1.75    atatat 		else
   2567   1.75    atatat 			display_string(pnode, sname, "unlimited", 10,
   2568   1.75    atatat 				       DISPLAY_NEW);
   2569    1.1       cgd 	}
   2570    1.1       cgd }
   2571    1.1       cgd 
   2572   1.75    atatat #ifdef CPU_DISKINFO
   2573   1.75    atatat /*ARGSUSED*/
   2574   1.14  christos static void
   2575   1.75    atatat machdep_diskinfo(HANDLER_ARGS)
   2576    1.1       cgd {
   2577   1.75    atatat 	struct disklist *dl;
   2578   1.75    atatat 	struct biosdisk_info *bi;
   2579   1.75    atatat 	struct nativedisk_info *ni;
   2580   1.75    atatat 	int rc;
   2581   1.75    atatat 	size_t sz;
   2582   1.75    atatat 	uint i, b, lim;
   2583   1.75    atatat 
   2584  1.133     pooka 	rc = prog_sysctl(name, namelen, NULL, &sz, NULL, 0);
   2585   1.75    atatat 	if (rc == -1) {
   2586   1.75    atatat 		sysctlerror(1);
   2587   1.75    atatat 		return;
   2588   1.75    atatat 	}
   2589   1.75    atatat 	dl = malloc(sz);
   2590   1.75    atatat 	if (dl == NULL) {
   2591   1.75    atatat 		sysctlerror(1);
   2592   1.75    atatat 		return;
   2593   1.75    atatat 	}
   2594  1.133     pooka 	rc = prog_sysctl(name, namelen, dl, &sz, NULL, 0);
   2595   1.75    atatat 	if (rc == -1) {
   2596   1.75    atatat 		sysctlerror(1);
   2597   1.75    atatat 		return;
   2598   1.75    atatat 	}
   2599   1.45       cgd 
   2600   1.75    atatat 	if (!nflag)
   2601   1.75    atatat 		printf("%s: ", sname);
   2602   1.75    atatat 	lim = dl->dl_nbiosdisks;
   2603   1.75    atatat 	if (lim > MAX_BIOSDISKS)
   2604   1.75    atatat 		lim = MAX_BIOSDISKS;
   2605   1.75    atatat 	for (bi = dl->dl_biosdisks, i = 0; i < lim; bi++, i++)
   2606   1.75    atatat 		printf("%x:%" PRIu64 "(%d/%d/%d),%x ",
   2607   1.75    atatat 		       bi->bi_dev, bi->bi_lbasecs,
   2608   1.75    atatat 		       bi->bi_cyl, bi->bi_head, bi->bi_sec,
   2609   1.75    atatat 		       bi->bi_flags);
   2610   1.75    atatat 	lim = dl->dl_nnativedisks;
   2611   1.75    atatat 	ni = dl->dl_nativedisks;
   2612   1.75    atatat 	bi = dl->dl_biosdisks;
   2613   1.75    atatat 	/* LINTED -- pointer casts are tedious */
   2614   1.75    atatat 	if ((char *)&ni[lim] != (char *)dl + sz) {
   2615   1.92    atatat 		sysctlperror("%s: size mismatch\n", gsname);
   2616   1.75    atatat 		return;
   2617   1.75    atatat 	}
   2618   1.75    atatat 	for (i = 0; i < lim; ni++, i++) {
   2619   1.75    atatat 		char t = ':';
   2620   1.75    atatat 		printf(" %.*s", (int)sizeof ni->ni_devname,
   2621   1.75    atatat 		       ni->ni_devname);
   2622  1.128     lukem 		for (b = 0; b < (unsigned int)ni->ni_nmatches; t = ',', b++)
   2623   1.75    atatat 			printf("%c%x", t,
   2624   1.75    atatat 			       bi[ni->ni_biosmatches[b]].bi_dev);
   2625   1.75    atatat 	}
   2626   1.75    atatat 	printf("\n");
   2627  1.126     njoly 	free(dl);
   2628    1.1       cgd }
   2629   1.75    atatat #endif /* CPU_DISKINFO */
   2630  1.108      elad 
   2631  1.108      elad /*ARGSUSED*/
   2632  1.108      elad static void
   2633  1.108      elad mode_bits(HANDLER_ARGS)
   2634  1.108      elad {
   2635  1.122      elad 	char buf[12], outbuf[100];
   2636  1.108      elad 	int o, m, *newp, rc;
   2637  1.108      elad 	size_t osz, nsz;
   2638  1.108      elad 	mode_t om, mm;
   2639  1.108      elad 
   2640  1.108      elad 	if (fn)
   2641  1.108      elad 		trim_whitespace(value, 3);
   2642  1.108      elad 
   2643  1.108      elad 	newp = NULL;
   2644  1.108      elad 	osz = sizeof(o);
   2645  1.108      elad 	if (value != NULL) {
   2646  1.108      elad 		void *foo;
   2647  1.108      elad 		int tt;
   2648  1.108      elad 		size_t ttsz = sizeof(tt);
   2649  1.108      elad 		mode_t old_umask;
   2650  1.108      elad 
   2651  1.108      elad 		nsz = sizeof(m);
   2652  1.108      elad 		newp = &m;
   2653  1.108      elad 		errno = 0;
   2654  1.133     pooka 		rc = prog_sysctl(name, namelen, &tt, &ttsz, NULL, 0);
   2655  1.108      elad 		if (rc == -1) {
   2656  1.108      elad 			sysctlperror("%s: failed query\n", sname);
   2657  1.108      elad 			return;
   2658  1.108      elad 		}
   2659  1.108      elad 
   2660  1.108      elad 		old_umask = umask(0);
   2661  1.108      elad 		foo = setmode(value);
   2662  1.108      elad 		umask(old_umask);
   2663  1.108      elad 		if (foo == NULL) {
   2664  1.108      elad 			sysctlperror("%s: '%s' is an invalid mode\n", sname,
   2665  1.108      elad 				     value);
   2666  1.151  christos 			EXIT(EXIT_FAILURE);
   2667  1.108      elad 		}
   2668  1.108      elad 		old_umask = umask(0);
   2669  1.108      elad 		m = getmode(foo, (mode_t)tt);
   2670  1.108      elad 		umask(old_umask);
   2671  1.108      elad 		if (errno) {
   2672  1.108      elad 			sysctlperror("%s: '%s' is an invalid mode\n", sname,
   2673  1.108      elad 				     value);
   2674  1.151  christos 			EXIT(EXIT_FAILURE);
   2675  1.108      elad 		}
   2676  1.108      elad 	}
   2677  1.108      elad 	else {
   2678  1.108      elad 		nsz = 0;
   2679  1.108      elad 		newp = NULL;
   2680  1.108      elad 	}
   2681  1.108      elad 
   2682  1.133     pooka 	rc = prog_sysctl(name, namelen, &o, &osz, newp, nsz);
   2683  1.108      elad 	if (rc == -1) {
   2684  1.108      elad 		sysctlerror(newp == NULL);
   2685  1.108      elad 		return;
   2686  1.108      elad 	}
   2687  1.108      elad 
   2688  1.108      elad 	if (newp && qflag)
   2689  1.108      elad 		return;
   2690  1.108      elad 
   2691  1.108      elad 	om = (mode_t)o;
   2692  1.108      elad 	mm = (mode_t)m;
   2693  1.108      elad 
   2694  1.108      elad 	if (rflag || xflag)
   2695  1.108      elad 		display_number(pnode, sname, &o, sizeof(o),
   2696  1.108      elad 			       newp ? DISPLAY_OLD : DISPLAY_VALUE);
   2697  1.108      elad 	else {
   2698  1.108      elad 		memset(buf, 0, sizeof(buf));
   2699  1.108      elad 		strmode(om, buf);
   2700  1.108      elad 		rc = snprintf(outbuf, sizeof(outbuf), "%04o (%s)", om, buf + 1);
   2701  1.108      elad 		display_string(pnode, sname, outbuf, rc, newp ? DISPLAY_OLD : DISPLAY_VALUE);
   2702  1.108      elad 	}
   2703  1.108      elad 
   2704  1.108      elad 	if (newp) {
   2705  1.108      elad 		if (rflag || xflag)
   2706  1.108      elad 			display_number(pnode, sname, &m, sizeof(m),
   2707  1.108      elad 				       DISPLAY_NEW);
   2708  1.108      elad 		else {
   2709  1.108      elad 			memset(buf, 0, sizeof(buf));
   2710  1.108      elad 			strmode(mm, buf);
   2711  1.108      elad 			rc = snprintf(outbuf, sizeof(outbuf), "%04o (%s)", mm, buf + 1);
   2712  1.108      elad 			display_string(pnode, sname, outbuf, rc, DISPLAY_NEW);
   2713  1.108      elad 		}
   2714  1.108      elad 	}
   2715  1.108      elad }
   2716  1.144  christos 
   2717  1.148  christos typedef __BITMAP_TYPE(, uint32_t, 0x10000) bitmap;
   2718  1.148  christos 
   2719  1.144  christos static char *
   2720  1.148  christos bitmask_print(const bitmap *o)
   2721  1.144  christos {
   2722  1.144  christos 	char *s, *os;
   2723  1.144  christos 
   2724  1.144  christos 	s = os = NULL;
   2725  1.146  christos 	for (size_t i = 0; i < MAXPORTS; i++)
   2726  1.146  christos 		if (__BITMAP_ISSET(i, o)) {
   2727  1.144  christos 			int rv;
   2728  1.144  christos 
   2729  1.144  christos 			if (os)
   2730  1.144  christos 			    	rv = asprintf(&s, "%s,%zu", os, i);
   2731  1.144  christos 			else
   2732  1.144  christos 			    	rv = asprintf(&s, "%zu", i);
   2733  1.144  christos 			if (rv == -1)
   2734  1.151  christos 				err(EXIT_FAILURE, "%s 1", __func__);
   2735  1.144  christos 			free(os);
   2736  1.144  christos 			os = s;
   2737  1.144  christos 		}
   2738  1.144  christos 	if (s == NULL && (s = strdup("")) == NULL)
   2739  1.151  christos 		err(EXIT_FAILURE, "%s 2", __func__);
   2740  1.144  christos 	return s;
   2741  1.144  christos }
   2742  1.144  christos 
   2743  1.144  christos static void
   2744  1.148  christos bitmask_scan(const void *v, bitmap *o)
   2745  1.144  christos {
   2746  1.144  christos 	char *s = strdup(v);
   2747  1.144  christos 	if (s == NULL)
   2748  1.151  christos 		err(EXIT_FAILURE, "%s", __func__);
   2749  1.147  christos 
   2750  1.148  christos 	__BITMAP_ZERO(o);
   2751  1.144  christos 	for (s = strtok(s, ","); s; s = strtok(NULL, ",")) {
   2752  1.144  christos 		char *e;
   2753  1.144  christos 		errno = 0;
   2754  1.144  christos 		unsigned long l = strtoul(s, &e, 0);
   2755  1.144  christos 		if ((l == ULONG_MAX && errno == ERANGE) || s == e || *e)
   2756  1.151  christos 			errx(EXIT_FAILURE, "Invalid port: %s", s);
   2757  1.146  christos 		if (l >= MAXPORTS)
   2758  1.151  christos 			errx(EXIT_FAILURE, "Port out of range: %s", s);
   2759  1.146  christos 		__BITMAP_SET(l, o);
   2760  1.144  christos 	}
   2761  1.144  christos }
   2762  1.144  christos 
   2763  1.144  christos 
   2764  1.144  christos static void
   2765  1.144  christos reserve(HANDLER_ARGS)
   2766  1.144  christos {
   2767  1.144  christos 	int rc;
   2768  1.144  christos 	size_t osz, nsz;
   2769  1.148  christos 	bitmap o, n;
   2770  1.144  christos 
   2771  1.144  christos 	if (fn)
   2772  1.144  christos 		trim_whitespace(value, 3);
   2773  1.144  christos 
   2774  1.144  christos 	osz = sizeof(o);
   2775  1.144  christos 	if (value) {
   2776  1.148  christos 		bitmask_scan(value, &n);
   2777  1.148  christos 		value = (char *)&n;
   2778  1.144  christos 		nsz = sizeof(n);
   2779  1.144  christos 	} else
   2780  1.144  christos 		nsz = 0;
   2781  1.144  christos 
   2782  1.144  christos 	rc = prog_sysctl(name, namelen, &o, &osz, value, nsz);
   2783  1.144  christos 	if (rc == -1) {
   2784  1.144  christos 		sysctlerror(value == NULL);
   2785  1.144  christos 		return;
   2786  1.144  christos 	}
   2787  1.144  christos 
   2788  1.144  christos 	if (value && qflag)
   2789  1.144  christos 		return;
   2790  1.144  christos 
   2791  1.144  christos 	if (rflag || xflag)
   2792  1.148  christos 		display_struct(pnode, sname, &o, sizeof(o),
   2793  1.144  christos 		    value ? DISPLAY_OLD : DISPLAY_VALUE);
   2794  1.144  christos 	else {
   2795  1.148  christos 		char *s = bitmask_print(&o);
   2796  1.144  christos 		display_string(pnode, sname, s, strlen(s),
   2797  1.144  christos 		    value ? DISPLAY_OLD : DISPLAY_VALUE);
   2798  1.144  christos 		free(s);
   2799  1.144  christos 	}
   2800  1.144  christos 
   2801  1.144  christos 	if (value) {
   2802  1.144  christos 		if (rflag || xflag)
   2803  1.148  christos 			display_struct(pnode, sname, &n, sizeof(n),
   2804  1.144  christos 			    DISPLAY_NEW);
   2805  1.144  christos 		else {
   2806  1.148  christos 			char *s = bitmask_print(&n);
   2807  1.144  christos 			display_string(pnode, sname, s, strlen(s), DISPLAY_NEW);
   2808  1.144  christos 			free(s);
   2809  1.144  christos 		}
   2810  1.144  christos 	}
   2811  1.144  christos }
   2812