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