sysctl.c revision 1.79 1 /* $NetBSD: sysctl.c,v 1.79 2004/02/19 06:44:18 atatat Exp $ */
2
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Brown.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The NetBSD Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * Copyright (c) 1993
37 * The Regents of the University of California. All rights reserved.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64 #include <sys/cdefs.h>
65 #ifndef lint
66 __COPYRIGHT(
67 "@(#) Copyright (c) 1993\n\
68 The Regents of the University of California. All rights reserved.\n");
69 #endif /* not lint */
70
71 #ifndef lint
72 #if 0
73 static char sccsid[] = "@(#)sysctl.c 8.1 (Berkeley) 6/6/93";
74 #else
75 __RCSID("$NetBSD: sysctl.c,v 1.79 2004/02/19 06:44:18 atatat Exp $");
76 #endif
77 #endif /* not lint */
78
79 #include <sys/types.h>
80 #include <sys/param.h>
81 #include <sys/sysctl.h>
82 #include <sys/mount.h>
83 #include <sys/resource.h>
84 #include <sys/stat.h>
85 #include <sys/sched.h>
86 #include <sys/socket.h>
87 #include <netinet/in.h>
88 #include <netinet/ip_var.h>
89 #include <netinet/tcp.h>
90 #include <netinet/tcp_timer.h>
91 #include <netinet/tcp_var.h>
92 #include <netinet/icmp6.h>
93 #include <nfs/rpcv2.h>
94 #include <nfs/nfsproto.h>
95 #include <nfs/nfs.h>
96 #include <machine/cpu.h>
97 #include <netkey/key_var.h>
98
99 #include <assert.h>
100 #include <ctype.h>
101 #include <err.h>
102 #include <errno.h>
103 #include <inttypes.h>
104 #include <stdarg.h>
105 #include <stdio.h>
106 #include <stdlib.h>
107 #include <string.h>
108 #include <time.h>
109 #include <unistd.h>
110
111 /*
112 * this needs to be able to do the printing and the setting
113 */
114 #define HANDLER_PROTO const char *, const char *, char *, \
115 int *, u_int, const struct sysctlnode *, \
116 u_int, void *
117 #define HANDLER_ARGS const char *sname, const char *dname, char *value, \
118 int *name, u_int namelen, const struct sysctlnode *pnode, \
119 u_int type, void *v
120 #define DISPLAY_VALUE 0
121 #define DISPLAY_OLD 1
122 #define DISPLAY_NEW 2
123
124 /*
125 * generic routines
126 */
127 static struct handlespec *findprinter(const int *, u_int);
128 static struct handlespec *findwriter(const int *, u_int);
129 static void print_tree(int *, u_int, struct sysctlnode *, u_int, int);
130 static void write_number(int *, u_int, struct sysctlnode *, char *);
131 static void write_string(int *, u_int, struct sysctlnode *, char *);
132 static void display_number(const struct sysctlnode *, const char *,
133 const void *, size_t, int);
134 static void display_string(const struct sysctlnode *, const char *,
135 const void *, size_t, int);
136 static void display_struct(const struct sysctlnode *, const char *,
137 const void *, size_t, int);
138 static void hex_dump(const unsigned char *, size_t);
139 static void usage(void);
140 static void parse(char *);
141 static void cparse(char *);
142 static void dparse(char *);
143 static void sysctlerror(int);
144
145 /*
146 * unexported from some place else (XXX tbd)
147 */
148 int learn_tree(int *, u_int, struct sysctlnode *);
149 int sysctlbyname(const char *, void *, size_t *, void *, size_t);
150 int sysctlgetmibinfo(const char *, int *, u_int *,
151 char *, size_t *, struct sysctlnode **);
152
153 /*
154 * "handlers"
155 */
156 static void printother(HANDLER_PROTO);
157 static void kern_clockrate(HANDLER_PROTO);
158 static void kern_boottime(HANDLER_PROTO);
159 static void kern_consdev(HANDLER_PROTO);
160 static void kern_cp_time(HANDLER_PROTO);
161 static void vm_loadavg(HANDLER_PROTO);
162 static void proc_limit(HANDLER_PROTO);
163 #ifdef CPU_DISKINFO
164 static void machdep_diskinfo(HANDLER_PROTO);
165 #endif /* CPU_DISKINFO */
166
167 struct handlespec {
168 int ps_name[CTL_MAXNAME];
169 void (*ps_p)(HANDLER_PROTO);
170 void (*ps_w)(HANDLER_PROTO);
171 void *ps_d;
172 } handlers[] = {
173 { { CTL_KERN, KERN_CLOCKRATE }, kern_clockrate },
174 { { CTL_KERN, KERN_VNODE }, printother, NULL, "pstat" },
175 { { CTL_KERN, KERN_PROC }, printother, NULL, "ps" },
176 { { CTL_KERN, KERN_PROC2 }, printother, NULL, "ps" },
177 { { CTL_KERN, KERN_PROC_ARGS }, printother, NULL, "ps" },
178 { { CTL_KERN, KERN_FILE }, printother, NULL, "pstat" },
179 { { CTL_KERN, KERN_NTPTIME }, printother, NULL,
180 "ntpdc -c kerninfo" },
181 { { CTL_KERN, KERN_MSGBUF }, printother, NULL, "dmesg" },
182 { { CTL_KERN, KERN_BOOTTIME }, kern_boottime },
183 { { CTL_KERN, KERN_CONSDEV }, kern_consdev },
184 { { CTL_KERN, KERN_CP_TIME }, kern_cp_time },
185 { { CTL_KERN, KERN_SYSVIPC_INFO }, printother, NULL, "ipcs" },
186 { { CTL_VM, VM_METER }, printother, NULL,
187 "vmstat' or 'systat" },
188 { { CTL_VM, VM_LOADAVG }, vm_loadavg },
189 { { CTL_VM, VM_UVMEXP }, printother, NULL,
190 "vmstat' or 'systat" },
191 { { CTL_VM, VM_UVMEXP2 }, printother, NULL,
192 "vmstat' or 'systat" },
193 { { CTL_VFS, 2 /* NFS */, NFS_NFSSTATS },
194 printother, NULL, "nfsstat" },
195 { { CTL_NET }, printother, NULL, NULL },
196 { { CTL_NET, PF_LOCAL }, printother, NULL, NULL },
197 { { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_IDENT },
198 printother, NULL, "identd" },
199 { { CTL_NET, PF_INET6, IPPROTO_TCP, TCPCTL_IDENT },
200 printother, NULL, "identd" },
201
202 { { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST },
203 printother, NULL, "ndp" },
204 { { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST },
205 printother, NULL, "ndp" },
206
207
208 { { CTL_NET, PF_KEY, KEYCTL_DUMPSA },
209 printother, NULL, "setkey" },
210 { { CTL_NET, PF_KEY, KEYCTL_DUMPSP },
211 printother, NULL, "setkey" },
212 /* { { CTL_DEBUG }, printother, NULL, NULL }, */
213 { { CTL_HW, HW_DISKSTATS }, printother, NULL, "iostat" },
214 #ifdef CPU_CONSDEV
215 { { CTL_MACHDEP, CPU_CONSDEV }, kern_consdev },
216 #endif /* CPU_CONSDEV */
217 #ifdef CPU_DISKINFO
218 { { CTL_MACHDEP, CPU_DISKINFO },machdep_diskinfo },
219 #endif /* CPU_CONSDEV */
220 { { CTL_DDB }, printother, NULL, NULL },
221 { { CTL_PROC, -1, PROC_PID_LIMIT, -1, -1 }, proc_limit, proc_limit },
222 { { CTL_UNSPEC }, },
223 };
224
225 struct sysctlnode my_root = {
226 #if defined(lint)
227 0
228 #else /* defined(lint) */
229 .sysctl_flags = SYSCTL_ROOT|
230 SYSCTL_TYPE(CTLTYPE_NODE),
231 .sysctl_size = sizeof(struct sysctlnode),
232 .sysctl_num = 0,
233 .sysctl_name = "(prog_root)",
234 #endif /* defined(lint) */
235 };
236
237 int Aflag, aflag, Mflag, nflag, qflag, rflag, wflag, xflag;
238 int req;
239 FILE *warnfp = stderr;
240
241 /*
242 * vah-riables n stuff
243 */
244 char gsname[SYSCTL_NAMELEN * CTL_MAXNAME + CTL_MAXNAME],
245 gdname[10 * CTL_MAXNAME + CTL_MAXNAME];
246 char sep[2] = ".", *eq = " = ";
247 const char *lname[] = {
248 "top", "second", "third", "fourth", "fifth", "sixth",
249 "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"
250 };
251
252 /*
253 * you've heard of main, haven't you?
254 */
255 int
256 main(int argc, char *argv[])
257 {
258 char *fn = NULL;
259 int name[CTL_MAXNAME];
260 int ch;
261
262 while ((ch = getopt(argc, argv, "Aaef:Mnqrwx")) != -1) {
263 switch (ch) {
264 case 'A':
265 Aflag++;
266 break;
267 case 'a':
268 aflag++;
269 break;
270 case 'e':
271 eq = "=";
272 break;
273 case 'f':
274 fn = optarg;
275 wflag++;
276 break;
277 case 'M':
278 Mflag++;
279 break;
280 case 'n':
281 nflag++;
282 break;
283 case 'q':
284 qflag++;
285 break;
286 case 'r':
287 rflag++;
288 break;
289 case 'w':
290 wflag++;
291 break;
292 case 'x':
293 xflag++;
294 break;
295 default:
296 usage();
297 }
298 }
299
300 argc -= optind;
301 argv += optind;
302
303 if (qflag && !wflag)
304 usage();
305 if (xflag && rflag)
306 usage();
307 /* if ((xflag || rflag) && wflag)
308 usage(); */
309 /* if (aflag && Mflag)
310 usage(); */
311 if ((Aflag || Mflag) && argc == 0 && fn == NULL)
312 aflag = 1;
313
314 if (Aflag)
315 warnfp = stdout;
316 req = 0;
317
318 if (aflag) {
319 print_tree(&name[0], 0, NULL, CTLTYPE_NODE, 1);
320 /* if (argc == 0) */
321 return (0);
322 }
323
324 if (fn) {
325 FILE *fp;
326 char *l;
327
328 fp = fopen(fn, "r");
329 if (fp == NULL) {
330 err(1, "%s", fn);
331 } else {
332 while ((l = fparseln(fp, NULL, NULL, NULL, 0)) != NULL)
333 {
334 if (*l) {
335 parse(l);
336 free(l);
337 }
338 }
339 fclose(fp);
340 }
341 return (0);
342 }
343
344 if (argc == 0)
345 usage();
346
347 while (argc-- > 0)
348 parse(*argv++);
349
350 return (0);
351 }
352
353 /*
354 * ********************************************************************
355 * how to find someone special to handle the reading (or maybe even
356 * writing) of a particular node
357 * ********************************************************************
358 */
359 static struct handlespec *
360 findprinter(const int *name, u_int namelen)
361 {
362 struct handlespec *p;
363 int i, j;
364
365 if (namelen < 1)
366 return (NULL);
367
368 p = &handlers[0];
369 for (i = 0; p[i].ps_name[0] != CTL_UNSPEC; i++) {
370 for (j = 0; j < namelen; j++)
371 if (p[i].ps_name[j] != name[j] &&
372 p[i].ps_name[j] != -1)
373 break;
374 if (j == namelen && p[i].ps_p != NULL)
375 return (&p[i]);
376 }
377
378 return (NULL);
379 }
380
381 static struct handlespec *
382 findwriter(const int *name, u_int namelen)
383 {
384 struct handlespec *p;
385 int i, j;
386
387 if (namelen < 1)
388 return (NULL);
389
390 p = &handlers[0];
391 for (i = 0; p[i].ps_name[0] != CTL_UNSPEC; i++) {
392 for (j = 0; j < namelen; j++)
393 if (p[i].ps_name[j] != name[j] &&
394 p[i].ps_name[j] != -1)
395 break;
396 if (j == namelen && p[i].ps_w != NULL)
397 return (&p[i]);
398 }
399
400 return (NULL);
401 }
402
403 /*
404 * ********************************************************************
405 * convert this special number to a special string so we can print the
406 * mib
407 * ********************************************************************
408 */
409 static const char *
410 sf(u_int f)
411 {
412 static char s[256];
413 char *c;
414
415 s[0] = '\0';
416 c = "";
417
418 #define print_flag(_f, _s, _c, _q, _x) \
419 if ((/*CONSTCOND*/_x) ? \
420 (((_f) & (_x)) == (__CONCAT(SYSCTL_,_q))) : \
421 ((_f) & (__CONCAT(SYSCTL_,_q)))) { \
422 strlcat((_s), (_c), sizeof(_s)); \
423 strlcat((_s), __STRING(_q), sizeof(_s)); \
424 (_c) = ","; \
425 (_f) &= ~(__CONCAT(SYSCTL_,_q)|(_x)); \
426 }
427 print_flag(f, s, c, READONLY, SYSCTL_READWRITE);
428 print_flag(f, s, c, READONLY1, SYSCTL_READWRITE);
429 print_flag(f, s, c, READONLY2, SYSCTL_READWRITE);
430 print_flag(f, s, c, READWRITE, SYSCTL_READWRITE);
431 print_flag(f, s, c, ANYWRITE, 0);
432 print_flag(f, s, c, PRIVATE, 0);
433 print_flag(f, s, c, PERMANENT, 0);
434 print_flag(f, s, c, OWNDATA, 0);
435 print_flag(f, s, c, IMMEDIATE, 0);
436 print_flag(f, s, c, HEX, 0);
437 print_flag(f, s, c, ROOT, 0);
438 print_flag(f, s, c, ANYNUMBER, 0);
439 print_flag(f, s, c, HIDDEN, 0);
440 print_flag(f, s, c, ALIAS, 0);
441 #undef print_flag
442
443 if (f) {
444 char foo[9];
445 snprintf(foo, sizeof(foo), "%x", f);
446 strlcat(s, c, sizeof(s));
447 strlcat(s, foo, sizeof(s));
448 }
449
450 return (s);
451 }
452
453 static const char *
454 st(u_int t)
455 {
456
457 switch (t) {
458 case CTLTYPE_NODE:
459 return "NODE";
460 case CTLTYPE_INT:
461 return "INT";
462 case CTLTYPE_STRING:
463 return "STRING";
464 case CTLTYPE_QUAD:
465 return "QUAD";
466 case CTLTYPE_STRUCT:
467 return "STRUCT";
468 }
469
470 return "???";
471 }
472
473 /*
474 * ********************************************************************
475 * print this node and any others underneath it
476 * ********************************************************************
477 */
478 static void
479 print_tree(int *name, u_int namelen, struct sysctlnode *pnode, u_int type,
480 int add)
481 {
482 struct sysctlnode *node;
483 int rc, ni;
484 size_t sz;
485 char *sp, *dp, n[20];
486 struct handlespec *p;
487
488 sp = &gsname[strlen(gsname)];
489 dp = &gdname[strlen(gdname)];
490
491 if (sp != &gsname[0] && dp == &gdname[0]) {
492 /*
493 * aw...shucks. now we must play catch up
494 */
495 for (ni = 0; ni < namelen; ni++) {
496 (void)snprintf(n, sizeof(n), "%d", name[ni]);
497 if (ni > 0)
498 strncat(gdname, ".", sizeof(gdname));
499 strncat(gdname, n, sizeof(gdname));
500 }
501 }
502
503 if (pnode == NULL)
504 pnode = &my_root;
505 else if (add) {
506 snprintf(n, sizeof(n), "%d", pnode->sysctl_num);
507 if (namelen > 1) {
508 strncat(gsname, sep, sizeof(gsname));
509 strncat(gdname, ".", sizeof(gdname));
510 }
511 strncat(gsname, pnode->sysctl_name, sizeof(gsname));
512 strncat(gdname, n, sizeof(gdname));
513 }
514
515 if (Mflag && pnode != &my_root) {
516 if (nflag)
517 printf("%s: ", gdname);
518 else
519 printf("%s (%s): ", gsname, gdname);
520 printf("CTLTYPE_%s", st(type));
521 if (type == CTLTYPE_NODE) {
522 if (SYSCTL_FLAGS(pnode->sysctl_flags) & SYSCTL_ALIAS)
523 printf(", alias %d",
524 pnode->sysctl_alias);
525 else
526 printf(", children %d/%d",
527 pnode->sysctl_clen,
528 pnode->sysctl_csize);
529 }
530 printf(", size %zu", pnode->sysctl_size);
531 printf(", flags 0x%x<%s>",
532 SYSCTL_FLAGS(pnode->sysctl_flags),
533 sf(SYSCTL_FLAGS(pnode->sysctl_flags)));
534 if (pnode->sysctl_func)
535 printf(", func=%p", pnode->sysctl_func);
536 printf(", ver=%d", pnode->sysctl_ver);
537 printf("\n");
538 if (type != CTLTYPE_NODE) {
539 *sp = *dp = '\0';
540 return;
541 }
542 }
543
544 /*
545 * if this is an alias and we added our name, that means we
546 * got here by recursing down into the tree, so skip it. The
547 * only way to print an aliased node is with either -M or by
548 * name specifically.
549 */
550 if (SYSCTL_FLAGS(pnode->sysctl_flags) & SYSCTL_ALIAS && add) {
551 *sp = *dp = '\0';
552 return;
553 }
554
555 p = findprinter(name, namelen);
556 if (type != CTLTYPE_NODE && p != NULL) {
557 (*p->ps_p)(gsname, gdname, NULL, name, namelen, pnode, type,
558 p->ps_d);
559 *sp = *dp = '\0';
560 return;
561 }
562
563 if (type != CTLTYPE_NODE && pnode->sysctl_size == 0) {
564 rc = sysctl(&name[0], namelen, NULL, &sz, NULL, 0);
565 if (rc == -1) {
566 sysctlerror(1);
567 *sp = *dp = '\0';
568 return;
569 }
570 if (sz == 0) {
571 if ((Aflag || req) && !Mflag)
572 printf("%s: node contains no data\n", gsname);
573 *sp = *dp = '\0';
574 return;
575 }
576 }
577 else
578 sz = pnode->sysctl_size;
579
580 switch (type) {
581 case CTLTYPE_NODE: {
582 learn_tree(name, namelen, pnode);
583 node = pnode->sysctl_child;
584 if (node == NULL) {
585 if (p != NULL)
586 (*p->ps_p)(gsname, gdname, NULL, name, namelen,
587 pnode, type, p->ps_d);
588 else if ((Aflag || req) && !Mflag)
589 printf("%s: no children\n", gsname);
590 }
591 else {
592 req = 0;
593 for (ni = 0; ni < pnode->sysctl_clen; ni++) {
594 name[namelen] = node[ni].sysctl_num;
595 if ((node[ni].sysctl_flags & SYSCTL_HIDDEN) &&
596 !(Aflag || req))
597 continue;
598 print_tree(name, namelen + 1, &node[ni],
599 SYSCTL_TYPE(node[ni].sysctl_flags),
600 1);
601 }
602 }
603 break;
604 }
605 case CTLTYPE_INT: {
606 int i;
607 rc = sysctl(name, namelen, &i, &sz, NULL, 0);
608 if (rc == -1) {
609 sysctlerror(1);
610 break;
611 }
612 display_number(pnode, gsname, &i, sizeof(i), DISPLAY_VALUE);
613 break;
614 }
615 case CTLTYPE_STRING: {
616 unsigned char buf[1024], *tbuf;
617 tbuf = buf;
618 sz = sizeof(buf);
619 rc = sysctl(&name[0], namelen, tbuf, &sz, NULL, 0);
620 if (rc == -1 && errno == ENOMEM) {
621 tbuf = malloc(sz);
622 if (tbuf == NULL) {
623 sysctlerror(1);
624 break;
625 }
626 rc = sysctl(&name[0], namelen, tbuf, &sz, NULL, 0);
627 }
628 if (rc == -1)
629 sysctlerror(1);
630 else
631 display_string(pnode, gsname, buf, sz, DISPLAY_VALUE);
632 if (tbuf != buf)
633 free(tbuf);
634 break;
635 }
636 case CTLTYPE_QUAD: {
637 u_quad_t q;
638 sz = sizeof(q);
639 rc = sysctl(&name[0], namelen, &q, &sz, NULL, 0);
640 if (rc == -1) {
641 sysctlerror(1);
642 break;
643 }
644 display_number(pnode, gsname, &q, sizeof(q), DISPLAY_VALUE);
645 break;
646 }
647 case CTLTYPE_STRUCT: {
648 /*
649 * we shouldn't actually get here, but if we
650 * do, would it be nice to have *something* to
651 * do other than completely ignore the
652 * request.
653 */
654 unsigned char *d;
655 if ((d = malloc(sz)) == NULL) {
656 fprintf(warnfp, "%s: !malloc failed!\n", gsname);
657 break;
658 }
659 rc = sysctl(&name[0], namelen, d, &sz, NULL, 0);
660 if (rc == -1) {
661 sysctlerror(1);
662 break;
663 }
664 display_struct(pnode, gsname, d, sz, DISPLAY_VALUE);
665 free(d);
666 break;
667 }
668 default:
669 /* should i print an error here? */
670 break;
671 }
672
673 *sp = *dp = '\0';
674 }
675
676 /*
677 * ********************************************************************
678 * parse a request, possibly determining that it's a create or destroy
679 * request
680 * ********************************************************************
681 */
682 static void
683 parse(char *l)
684 {
685 struct sysctlnode *node;
686 struct handlespec *w;
687 int name[CTL_MAXNAME];
688 u_int namelen, type;
689 char *key, *value, *dot;
690 size_t sz;
691
692 req = 1;
693 key = l;
694 value = strchr(l, '=');
695 if (value != NULL) {
696 if (!wflag) {
697 fprintf(warnfp,
698 "%s: Must specify -w to set variables\n",
699 getprogname());
700 exit(1);
701 }
702 *value++ = '\0';
703 }
704
705 if ((dot = strpbrk(key, "./")) == NULL)
706 sep[0] = '.';
707 else
708 sep[0] = dot[0];
709 sep[1] = '\0';
710
711 if (key[0] == sep[0] && key[1] == sep[0]) {
712 if (value != NULL)
713 value[-1] = '=';
714 if (strncmp(key + 2, "create=", 7) == 0)
715 cparse(key + 9);
716 else if (strncmp(key + 2, "destroy=", 8) == 0)
717 dparse(key + 10);
718 else
719 fprintf(warnfp, "%s: unable to parse '%s'\n",
720 getprogname(), key);
721 return;
722 }
723
724 node = &my_root;
725 namelen = CTL_MAXNAME;
726 sz = sizeof(gsname);
727
728 if (sysctlgetmibinfo(key, &name[0], &namelen, gsname, &sz, &node) == -1)
729 {
730 fprintf(warnfp, "%s: %s level name '%s' in '%s' is invalid\n",
731 getprogname(), lname[namelen], gsname, l);
732 exit(1);
733 }
734
735 type = SYSCTL_TYPE(node->sysctl_flags);
736
737 if (value == NULL) {
738 print_tree(&name[0], namelen, node, type, 0);
739 gsname[0] = '\0';
740 return;
741 }
742
743 if (type != CTLTYPE_NODE && (w = findwriter(name, namelen)) != NULL) {
744 (*w->ps_w)(gsname, gdname, value, name, namelen, node, type,
745 w->ps_d);
746 gsname[0] = '\0';
747 return;
748 }
749
750 switch (type) {
751 case CTLTYPE_NODE:
752 /*
753 * XXX old behavior is to print. should we error instead?
754 */
755 print_tree(&name[0], namelen, node, CTLTYPE_NODE, 1);
756 break;
757 case CTLTYPE_INT:
758 write_number(&name[0], namelen, node, value);
759 break;
760 case CTLTYPE_STRING:
761 write_string(&name[0], namelen, node, value);
762 break;
763 case CTLTYPE_QUAD:
764 write_number(&name[0], namelen, node, value);
765 break;
766 case CTLTYPE_STRUCT:
767 /*
768 * XXX old behavior is to print. should we error instead?
769 */
770 /* fprintf(warnfp, "you can't write to %s\n", gsname); */
771 print_tree(&name[0], namelen, node, type, 0);
772 break;
773 }
774 }
775
776 /*
777
778 //create=foo.bar.whatever...,
779 [type=(int|quad|string|struct|node),]
780 [size=###,]
781 [n=###,]
782 [flags=(tiohxparw12),]
783 [addr=0x####,|symbol=...|value=...]
784
785 size is optional for some types. type must be set before anything
786 else. nodes can have [r12whp], but nothing else applies. if no
787 size or type is given, node is asserted. writeable is the default,
788 with [r12w] being read-only, writeable below securelevel 1,
789 writeable below securelevel 2, and unconditionally writeable
790 respectively. if you specify addr, it is assumed to be the name of
791 a kernel symbol, if value, SYSCTL_OWNDATA will be asserted for
792 strings, SYSCTL_IMMEDIATE for ints and u_quad_ts. you cannot
793 specify both value and addr.
794
795 */
796
797 static void
798 cparse(char *l)
799 {
800 struct sysctlnode node;
801 size_t sz;
802 char *nname, *key, *value, *data, *addr, *c, *t;
803 int name[CTL_MAXNAME], i, rc, method, flags, rw;
804 u_int namelen, type;
805 u_quad_t q;
806 long li, lo;
807
808 /*
809 * these are the pieces that make up the description of a new
810 * node
811 */
812 memset(&node, 0, sizeof(node));
813 node.sysctl_num = CTL_CREATE; /* any number is fine */
814 flags = 0;
815 rw = -1;
816 type = 0;
817 sz = 0;
818 data = addr = NULL;
819 memset(name, 0, sizeof(name));
820 namelen = 0;
821 method = 0;
822
823 /*
824 * misc stuff used when constructing
825 */
826 i = 0;
827 q = 0;
828 key = NULL;
829 value = NULL;
830
831 /*
832 * the name of the thing we're trying to create is first, so
833 * pick it off.
834 */
835 nname = l;
836 if ((c = strchr(nname, ',')) != NULL)
837 *c++ = '\0';
838
839 while (c != NULL) {
840
841 /*
842 * pull off the next "key=value" pair
843 */
844 key = c;
845 if ((t = strchr(key, '=')) != NULL) {
846 *t++ = '\0';
847 value = t;
848 }
849 else
850 value = NULL;
851
852 /*
853 * if the "key" is "value", then that eats the rest of
854 * the string, so we're done, otherwise bite it off at
855 * the next comma.
856 */
857 if (strcmp(key, "value") == 0) {
858 c = NULL;
859 data = value;
860 break;
861 }
862 else {
863 if ((c = strchr(value, ',')) != NULL)
864 *c++ = '\0';
865 }
866
867 /*
868 * note that we (mostly) let the invoker of sysctl(8)
869 * play rampant here and depend on the kernel to tell
870 * them that they were wrong. well...within reason.
871 * we later check the various parameters against each
872 * other to make sure it makes some sort of sense.
873 */
874 if (strcmp(key, "addr") == 0) {
875 /*
876 * we can't check these two. only the kernel
877 * can tell us when it fails to find the name
878 * (or if the address is invalid).
879 */
880 if (method != 0) {
881 fprintf(warnfp,
882 "%s: %s: already have %s for new node\n",
883 getprogname(), nname,
884 method == CTL_CREATE ? "addr" : "symbol");
885 exit(1);
886 }
887 errno = 0;
888 addr = (void*)strtoul(value, &t, 0);
889 if (*t != '\0' || errno != 0) {
890 fprintf(warnfp,
891 "%s: %s: '%s' is not a valid address\n",
892 getprogname(), nname, value);
893 exit(1);
894 }
895 method = CTL_CREATE;
896 }
897 else if (strcmp(key, "symbol") == 0) {
898 if (method != 0) {
899 fprintf(warnfp,
900 "%s: %s: already have %s for new node\n",
901 getprogname(), nname,
902 method == CTL_CREATE ? "addr" : "symbol");
903 exit(1);
904 }
905 addr = value;
906 method = CTL_CREATESYM;
907 }
908 else if (strcmp(key, "type") == 0) {
909 if (strcmp(value, "node") == 0)
910 type = CTLTYPE_NODE;
911 else if (strcmp(value, "int") == 0) {
912 sz = sizeof(int);
913 type = CTLTYPE_INT;
914 }
915 else if (strcmp(value, "string") == 0)
916 type = CTLTYPE_STRING;
917 else if (strcmp(value, "quad") == 0) {
918 sz = sizeof(u_quad_t);
919 type = CTLTYPE_QUAD;
920 }
921 else if (strcmp(value, "struct") == 0)
922 type = CTLTYPE_STRUCT;
923 else {
924 fprintf(warnfp,
925 "%s: %s: '%s' is not a valid type\n",
926 getprogname(), nname, value);
927 exit(1);
928 }
929 }
930 else if (strcmp(key, "size") == 0) {
931 errno = 0;
932 /*
933 * yes, i know size_t is not an unsigned long,
934 * but we can all agree that it ought to be,
935 * right?
936 */
937 sz = strtoul(value, &t, 0);
938 if (*t != '\0' || errno != 0) {
939 fprintf(warnfp,
940 "%s: %s: '%s' is not a valid size\n",
941 getprogname(), nname, value);
942 exit(1);
943 }
944 }
945 else if (strcmp(key, "n") == 0) {
946 errno = 0;
947 li = strtol(value, &t, 0);
948 node.sysctl_num = li;
949 lo = node.sysctl_num;
950 if (*t != '\0' || errno != 0 || li != lo || lo < 0) {
951 fprintf(warnfp,
952 "%s: %s: '%s' is not a valid mib number\n",
953 getprogname(), nname, value);
954 exit(1);
955 }
956 }
957 else if (strcmp(key, "flags") == 0) {
958 t = value;
959 while (*t != '\0') {
960 switch (*t) {
961 case 'a':
962 flags |= SYSCTL_ANYWRITE;
963 break;
964 case 'h':
965 flags |= SYSCTL_HIDDEN;
966 break;
967 case 'i':
968 flags |= SYSCTL_IMMEDIATE;
969 break;
970 case 'o':
971 flags |= SYSCTL_OWNDATA;
972 break;
973 case 'p':
974 flags |= SYSCTL_PRIVATE;
975 break;
976 case 'x':
977 flags |= SYSCTL_HEX;
978 break;
979
980 case 'r':
981 rw = SYSCTL_READONLY;
982 break;
983 case '1':
984 rw = SYSCTL_READONLY1;
985 break;
986 case '2':
987 rw = SYSCTL_READONLY2;
988 break;
989 case 'w':
990 rw = SYSCTL_READWRITE;
991 break;
992 default:
993 fprintf(warnfp,
994 "%s: %s: '%c' is not a valid flag\n",
995 getprogname(), nname, *t);
996 exit(1);
997 }
998 t++;
999 }
1000 }
1001 else {
1002 fprintf(warnfp, "%s: %s: unrecognized keyword '%s'\n",
1003 getprogname(), nname, key);
1004 exit(1);
1005 }
1006 }
1007
1008 /*
1009 * now that we've finished parsing the given string, fill in
1010 * anything they didn't specify
1011 */
1012 if (type == 0)
1013 type = CTLTYPE_NODE;
1014
1015 /*
1016 * the "data" can be interpreted various ways depending on the
1017 * type of node we're creating, as can the size
1018 */
1019 if (data != NULL) {
1020 if (addr != NULL) {
1021 fprintf(warnfp,
1022 "%s: %s: cannot specify both value and "
1023 "address\n", getprogname(), nname);
1024 exit(1);
1025 }
1026
1027 switch (type) {
1028 case CTLTYPE_INT:
1029 errno = 0;
1030 li = strtol(data, &t, 0);
1031 i = li;
1032 lo = i;
1033 if (*t != '\0' || errno != 0 || li != lo || lo < 0) {
1034 fprintf(warnfp,
1035 "%s: %s: '%s' is not a valid integer\n",
1036 getprogname(), nname, value);
1037 exit(1);
1038 }
1039 if (!(flags & SYSCTL_OWNDATA)) {
1040 flags |= SYSCTL_IMMEDIATE;
1041 node.sysctl_idata = i;
1042 }
1043 else
1044 node.sysctl_data = &i;
1045 if (sz == 0)
1046 sz = sizeof(int);
1047 break;
1048 case CTLTYPE_STRING:
1049 flags |= SYSCTL_OWNDATA;
1050 node.sysctl_data = data;
1051 if (sz == 0)
1052 sz = strlen(data) + 1;
1053 else if (sz < strlen(data) + 1) {
1054 fprintf(warnfp, "%s: %s: ignoring size=%zu for "
1055 "string node, too small for given "
1056 "value\n", getprogname(), nname, sz);
1057 sz = strlen(data) + 1;
1058 }
1059 break;
1060 case CTLTYPE_QUAD:
1061 errno = 0;
1062 q = strtouq(data, &t, 0);
1063 if (*t != '\0' || errno != 0) {
1064 fprintf(warnfp,
1065 "%s: %s: '%s' is not a valid quad\n",
1066 getprogname(), nname, value);
1067 exit(1);
1068 }
1069 if (!(flags & SYSCTL_OWNDATA)) {
1070 flags |= SYSCTL_IMMEDIATE;
1071 node.sysctl_qdata = q;
1072 }
1073 else
1074 node.sysctl_data = &q;
1075 if (sz == 0)
1076 sz = sizeof(u_quad_t);
1077 break;
1078 case CTLTYPE_STRUCT:
1079 fprintf(warnfp,
1080 "%s: %s: struct not initializable\n",
1081 getprogname(), nname);
1082 exit(1);
1083 }
1084
1085 /*
1086 * these methods have all provided local starting
1087 * values that the kernel must copy in
1088 */
1089 }
1090
1091 /*
1092 * hmm...no data, but we have an address of data. that's
1093 * fine.
1094 */
1095 else if (addr != 0)
1096 node.sysctl_data = (void*)addr;
1097
1098 /*
1099 * no data and no address? well...okay. we might be able to
1100 * manage that.
1101 */
1102 else if (type != CTLTYPE_NODE) {
1103 if (sz == 0) {
1104 fprintf(warnfp,
1105 "%s: %s: need a size or a starting value\n",
1106 getprogname(), nname);
1107 exit(1);
1108 }
1109 if (!(flags & SYSCTL_IMMEDIATE))
1110 flags |= SYSCTL_OWNDATA;
1111 }
1112
1113 /*
1114 * now we do a few sanity checks on the description we've
1115 * assembled
1116 */
1117 if ((flags & SYSCTL_IMMEDIATE) &&
1118 (type == CTLTYPE_STRING || type == CTLTYPE_STRUCT)) {
1119 fprintf(warnfp,
1120 "%s: %s: cannot make an immediate %s\n",
1121 getprogname(), nname,
1122 (type == CTLTYPE_STRING) ? "string" : "struct");
1123 exit(1);
1124 }
1125 if (type == CTLTYPE_NODE && node.sysctl_data != NULL) {
1126 fprintf(warnfp, "%s: %s: nodes do not have data\n",
1127 getprogname(), nname);
1128 exit(1);
1129 }
1130
1131 /*
1132 * some types must have a particular size
1133 */
1134 if (sz != 0) {
1135 if ((type == CTLTYPE_INT && sz != sizeof(int)) ||
1136 (type == CTLTYPE_QUAD && sz != sizeof(u_quad_t)) ||
1137 (type == CTLTYPE_NODE && sz != 0)) {
1138 fprintf(warnfp, "%s: %s: wrong size for type\n",
1139 getprogname(), nname);
1140 exit(1);
1141 }
1142 }
1143 else if (type == CTLTYPE_STRUCT) {
1144 fprintf(warnfp, "%s: %s: struct must have size\n",
1145 getprogname(), nname);
1146 exit(1);
1147 }
1148
1149 /*
1150 * now...if no one said anything yet, we default nodes or
1151 * any type that owns data being writeable, and everything
1152 * else being readonly.
1153 */
1154 if (rw == -1) {
1155 if (type == CTLTYPE_NODE ||
1156 (flags & (SYSCTL_OWNDATA|SYSCTL_IMMEDIATE)))
1157 rw = SYSCTL_READWRITE;
1158 else
1159 rw = SYSCTL_READONLY;
1160 }
1161
1162 /*
1163 * if a kernel address was specified, that can't be made
1164 * writeable by us.
1165 if (rw != SYSCTL_READONLY && addr) {
1166 fprintf(warnfp, "%s: %s: kernel data can only be readable\n",
1167 getprogname(), nname);
1168 exit(1);
1169 }
1170 */
1171
1172 /*
1173 * what separator were they using in the full name of the new
1174 * node?
1175 */
1176 if ((t = strpbrk(nname, "./")) == NULL)
1177 sep[0] = '.';
1178 else
1179 sep[0] = t[0];
1180 sep[1] = '\0';
1181
1182 /*
1183 * put it all together, now. t'ain't much, is it?
1184 */
1185 node.sysctl_flags = flags|rw|type;
1186 node.sysctl_size = sz;
1187 t = strrchr(nname, sep[0]);
1188 if (t != NULL)
1189 strlcpy(node.sysctl_name, t + 1, sizeof(node.sysctl_name));
1190 else
1191 strlcpy(node.sysctl_name, nname, sizeof(node.sysctl_name));
1192
1193 /*
1194 * if this is a new top-level node, then we don't need to find
1195 * the mib for its parent
1196 */
1197 if (t == NULL) {
1198 namelen = 0;
1199 gsname[0] = '\0';
1200 }
1201
1202 /*
1203 * on the other hand, if it's not a top-level node...
1204 */
1205 else {
1206 namelen = sizeof(name) / sizeof(name[0]);
1207 sz = sizeof(gsname);
1208 *t = '\0';
1209 rc = sysctlgetmibinfo(nname, &name[0], &namelen,
1210 gsname, &sz, NULL);
1211 *t = sep[0];
1212 if (rc == -1) {
1213 fprintf(warnfp,
1214 "%s: %s level name '%s' in '%s' is invalid\n",
1215 getprogname(), lname[namelen], gsname, nname);
1216 exit(1);
1217 }
1218 }
1219
1220 /*
1221 * yes, a new node is being created
1222 */
1223 if (method != 0)
1224 name[namelen++] = method;
1225 else
1226 name[namelen++] = CTL_CREATE;
1227
1228 sz = sizeof(node);
1229 rc = sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node));
1230
1231 if (rc == -1) {
1232 fprintf(warnfp,
1233 "%s: %s: CTL_CREATE failed: %s\n",
1234 getprogname(), nname, strerror(errno));
1235 exit(1);
1236 }
1237 else if (!qflag && !nflag)
1238 printf("%s(%s): (created)\n", nname, st(type));
1239 }
1240
1241 static void
1242 dparse(char *l)
1243 {
1244 struct sysctlnode node;
1245 size_t sz;
1246 int name[CTL_MAXNAME], rc;
1247 u_int namelen;
1248
1249 memset(name, 0, sizeof(name));
1250 namelen = sizeof(name) / sizeof(name[0]);
1251 sz = sizeof(gsname);
1252 rc = sysctlgetmibinfo(l, &name[0], &namelen, gsname, &sz, NULL);
1253 if (rc == -1) {
1254 fprintf(warnfp,
1255 "%s: %s level name '%s' in '%s' is invalid\n",
1256 getprogname(), lname[namelen], gsname, l);
1257 exit(1);
1258 }
1259
1260 memset(&node, 0, sizeof(node));
1261 node.sysctl_num = name[namelen - 1];
1262 name[namelen - 1] = CTL_DESTROY;
1263
1264 sz = sizeof(node);
1265 rc = sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node));
1266
1267 if (rc == -1) {
1268 fprintf(warnfp,
1269 "%s: %s: CTL_DESTROY failed: %s\n",
1270 getprogname(), l, strerror(errno));
1271 exit(1);
1272 }
1273 else if (!qflag && !nflag)
1274 printf("%s(%s): (destroyed)\n", gsname,
1275 st(SYSCTL_TYPE(node.sysctl_flags)));
1276 }
1277
1278 /*
1279 * ********************************************************************
1280 * when things go wrong...
1281 * ********************************************************************
1282 */
1283 static void
1284 usage(void)
1285 {
1286 const char *progname = getprogname();
1287
1288 (void)fprintf(stderr,
1289 "usage:\t%s %s\n"
1290 "\t%s %s\n"
1291 "\t%s %s\n"
1292 "\t%s %s\n"
1293 "\t%s %s\n"
1294 "\t%s %s\n",
1295 progname, "[-ne] [-x[x]|-r] variable ...",
1296 progname, "[-ne] [-q] -w variable=value ...",
1297 progname, "[-ne] -a",
1298 progname, "[-ne] -A",
1299 progname, "[-ne] -M",
1300 progname, "[-ne] [-q] -f file");
1301 exit(1);
1302 }
1303
1304 void
1305 sysctlerror(int soft)
1306 {
1307 if (soft) {
1308 switch (errno) {
1309 case ENOENT:
1310 case ENOPROTOOPT:
1311 case ENOTDIR:
1312 case EOPNOTSUPP:
1313 case EPROTONOSUPPORT:
1314 if (Aflag || req)
1315 fprintf(warnfp,
1316 "%s: the value is not available\n",
1317 gsname);
1318 return;
1319 }
1320 }
1321
1322 fprintf(warnfp, "%s: sysctl() failed with %s\n",
1323 gsname, strerror(errno));
1324 if (!soft)
1325 exit(1);
1326 }
1327
1328 /*
1329 * ********************************************************************
1330 * how to write to a "simple" node
1331 * ********************************************************************
1332 */
1333 static void
1334 write_number(int *name, u_int namelen, struct sysctlnode *node, char *value)
1335 {
1336 int ii, io;
1337 u_quad_t qi, qo;
1338 size_t si, so;
1339 int rc;
1340 void *i, *o;
1341 char *t;
1342
1343 si = so = 0;
1344 i = o = NULL;
1345 errno = 0;
1346 qi = strtouq(value, &t, 0);
1347 if (errno != 0) {
1348 fprintf(warnfp, "%s: value too large\n", value);
1349 exit(1);
1350 }
1351 if (*t != '\0') {
1352 fprintf(warnfp, "%s: not a number\n", value);
1353 exit(1);
1354 }
1355
1356 switch (SYSCTL_TYPE(node->sysctl_flags)) {
1357 case CTLTYPE_INT:
1358 ii = (int)qi;
1359 qo = ii;
1360 if (qo != qi) {
1361 fprintf(warnfp, "%s: value too large\n", value);
1362 exit(1);
1363 }
1364 o = &io;
1365 so = sizeof(io);
1366 i = ⅈ
1367 si = sizeof(ii);
1368 break;
1369 case CTLTYPE_QUAD:
1370 o = &qo;
1371 so = sizeof(qo);
1372 i = &qi;
1373 si = sizeof(qi);
1374 break;
1375 }
1376
1377 rc = sysctl(name, namelen, o, &so, i, si);
1378 if (rc == -1)
1379 sysctlerror(0);
1380
1381 switch (SYSCTL_TYPE(node->sysctl_flags)) {
1382 case CTLTYPE_INT:
1383 display_number(node, gsname, &io, sizeof(io), DISPLAY_OLD);
1384 display_number(node, gsname, &ii, sizeof(ii), DISPLAY_NEW);
1385 break;
1386 case CTLTYPE_QUAD:
1387 display_number(node, gsname, &qo, sizeof(qo), DISPLAY_OLD);
1388 display_number(node, gsname, &qi, sizeof(qi), DISPLAY_NEW);
1389 break;
1390 }
1391 }
1392
1393 static void
1394 write_string(int *name, u_int namelen, struct sysctlnode *node, char *value)
1395 {
1396 char *i, *o;
1397 size_t si, so;
1398 int rc;
1399
1400 i = value;
1401 si = strlen(i) + 1;
1402 so = node->sysctl_size;
1403 if (si > so && so != 0) {
1404 fprintf(warnfp, "%s: string too long\n", value);
1405 exit(1);
1406 }
1407 o = malloc(so);
1408 if (o == NULL) {
1409 fprintf(warnfp, "%s: !malloc failed!\n", gsname);
1410 exit(1);
1411 }
1412
1413 rc = sysctl(name, namelen, o, &so, i, si);
1414 if (rc == -1)
1415 sysctlerror(0);
1416
1417 display_string(node, gsname, o, so, DISPLAY_OLD);
1418 display_string(node, gsname, i, si, DISPLAY_NEW);
1419 free(o);
1420 }
1421
1422 /*
1423 * ********************************************************************
1424 * simple ways to print stuff consistently
1425 * ********************************************************************
1426 */
1427 static void
1428 display_number(const struct sysctlnode *node, const char *name,
1429 const void *data, size_t sz, int n)
1430 {
1431 u_quad_t q;
1432 int i;
1433
1434 if (qflag)
1435 return;
1436 if ((nflag || rflag) && (n == DISPLAY_OLD))
1437 return;
1438
1439 if (rflag && n != DISPLAY_OLD) {
1440 fwrite(data, sz, 1, stdout);
1441 return;
1442 }
1443
1444 if (!nflag) {
1445 if (n == DISPLAY_VALUE)
1446 printf("%s%s", name, eq);
1447 else if (n == DISPLAY_OLD)
1448 printf("%s: ", name);
1449 }
1450
1451 if (xflag > 1) {
1452 if (n != DISPLAY_NEW)
1453 printf("\n");
1454 hex_dump(data, sz);
1455 return;
1456 }
1457
1458 switch (SYSCTL_TYPE(node->sysctl_flags)) {
1459 case CTLTYPE_INT:
1460 memcpy(&i, data, sz);
1461 if (xflag)
1462 printf("0x%0*x", (int)sz * 2, i);
1463 else if (node->sysctl_flags & SYSCTL_HEX)
1464 printf("%#x", i);
1465 else
1466 printf("%d", i);
1467 break;
1468 case CTLTYPE_QUAD:
1469 memcpy(&q, data, sz);
1470 if (xflag)
1471 printf("0x%0*" PRIx64, (int)sz * 2, q);
1472 else if (node->sysctl_flags & SYSCTL_HEX)
1473 printf("%#" PRIx64, q);
1474 else
1475 printf("%" PRIu64, q);
1476 break;
1477 }
1478
1479 if (n == DISPLAY_OLD)
1480 printf(" -> ");
1481 else
1482 printf("\n");
1483 }
1484
1485 static void
1486 display_string(const struct sysctlnode *node, const char *name,
1487 const void *data, size_t sz, int n)
1488 {
1489 const unsigned char *buf = data;
1490 int ni;
1491
1492 if (qflag)
1493 return;
1494 if ((nflag || rflag) && (n == DISPLAY_OLD))
1495 return;
1496
1497 if (rflag && n != DISPLAY_OLD) {
1498 fwrite(data, sz, 1, stdout);
1499 return;
1500 }
1501
1502 if (!nflag) {
1503 if (n == DISPLAY_VALUE)
1504 printf("%s%s", name, eq);
1505 else if (n == DISPLAY_OLD)
1506 printf("%s: ", name);
1507 }
1508
1509 if (xflag > 1) {
1510 if (n != DISPLAY_NEW)
1511 printf("\n");
1512 hex_dump(data, sz);
1513 return;
1514 }
1515
1516 if (xflag || node->sysctl_flags & SYSCTL_HEX) {
1517 for (ni = 0; ni < (int)sz; ni++) {
1518 if (xflag)
1519 printf("%02x", buf[ni]);
1520 if (buf[ni] == '\0')
1521 break;
1522 if (!xflag)
1523 printf("\\x%2.2x", buf[ni]);
1524 }
1525 }
1526 else
1527 printf("%.*s", (int)sz, buf);
1528
1529 if (n == DISPLAY_OLD)
1530 printf(" -> ");
1531 else
1532 printf("\n");
1533 }
1534
1535 /*ARGSUSED*/
1536 static void
1537 display_struct(const struct sysctlnode *node, const char *name,
1538 const void *data, size_t sz, int n)
1539 {
1540 const unsigned char *buf = data;
1541 int ni;
1542 size_t more;
1543
1544 if (qflag)
1545 return;
1546 if (!(xflag || rflag)) {
1547 if (Aflag || req)
1548 fprintf(warnfp,
1549 "%s: this type is unknown to this program\n",
1550 gsname);
1551 return;
1552 }
1553 if ((nflag || rflag) && (n == DISPLAY_OLD))
1554 return;
1555
1556 if (rflag && n != DISPLAY_OLD) {
1557 fwrite(data, sz, 1, stdout);
1558 return;
1559 }
1560
1561 if (!nflag) {
1562 if (n == DISPLAY_VALUE)
1563 printf("%s%s", name, eq);
1564 else if (n == DISPLAY_OLD)
1565 printf("%s: ", name);
1566 }
1567
1568 if (xflag > 1) {
1569 if (n != DISPLAY_NEW)
1570 printf("\n");
1571 hex_dump(data, sz);
1572 return;
1573 }
1574
1575 if (sz > 16) {
1576 more = sz - 16;
1577 sz = 16;
1578 }
1579 else
1580 more = 0;
1581 for (ni = 0; ni < (int)sz; ni++)
1582 printf("%02x", buf[ni]);
1583 if (more)
1584 printf("...(%zu more bytes)", more);
1585 printf("\n");
1586 }
1587
1588 static void
1589 hex_dump(const unsigned char *buf, size_t len)
1590 {
1591 int i, j;
1592 char line[80], tmp[12];
1593
1594 memset(line, ' ', sizeof(line));
1595 for (i = 0, j = 15; i < len; i++) {
1596 j = i % 16;
1597 /* reset line */
1598 if (j == 0) {
1599 line[58] = '|';
1600 line[77] = '|';
1601 line[78] = 0;
1602 snprintf(tmp, sizeof(tmp), "%07d", i);
1603 memcpy(&line[0], tmp, 7);
1604 }
1605 /* copy out hex version of byte */
1606 snprintf(tmp, sizeof(tmp), "%02x", buf[i]);
1607 memcpy(&line[9 + j * 3], tmp, 2);
1608 /* copy out plain version of byte */
1609 line[60 + j] = (isprint(buf[i])) ? buf[i] : '.';
1610 /* print a full line and erase it */
1611 if (j == 15) {
1612 printf("%s\n", line);
1613 memset(line, ' ', sizeof(line));
1614 }
1615 }
1616 if (line[0] != ' ')
1617 printf("%s\n", line);
1618 printf("%07zu bytes\n", len);
1619 }
1620
1621 /*
1622 * ********************************************************************
1623 * functions that handle particular nodes
1624 * ********************************************************************
1625 */
1626 /*ARGSUSED*/
1627 static void
1628 printother(HANDLER_ARGS)
1629 {
1630 int rc;
1631 void *p;
1632 size_t sz1, sz2;
1633
1634 if (!(Aflag || req) || Mflag)
1635 return;
1636
1637 /*
1638 * okay...you asked for it, so let's give it a go
1639 */
1640 while (type != CTLTYPE_NODE && (xflag || rflag)) {
1641 rc = sysctl(name, namelen, NULL, &sz1, NULL, 0);
1642 if (rc == -1 || sz1 == 0)
1643 break;
1644 p = malloc(sz1);
1645 if (p == NULL)
1646 break;
1647 sz2 = sz1;
1648 rc = sysctl(name, namelen, p, &sz2, NULL, 0);
1649 if (rc == -1 || sz1 != sz2) {
1650 free(p);
1651 break;
1652 }
1653 display_struct(pnode, gsname, p, sz1, DISPLAY_VALUE);
1654 free(p);
1655 return;
1656 }
1657
1658 /*
1659 * that didn't work...do we have a specific message for this
1660 * thing?
1661 */
1662 if (v != NULL) {
1663 fprintf(warnfp, "%s: use '%s' to view this information\n",
1664 gsname, (const char *)v);
1665 return;
1666 }
1667
1668 /*
1669 * hmm...i wonder if we have any generic hints?
1670 */
1671 switch (name[0]) {
1672 case CTL_NET:
1673 fprintf(warnfp, "%s: use 'netstat' to view this information\n",
1674 sname);
1675 break;
1676 case CTL_DEBUG:
1677 fprintf(warnfp, "%s: missing 'options DEBUG' from kernel?\n",
1678 sname);
1679 break;
1680 case CTL_DDB:
1681 fprintf(warnfp, "%s: missing 'options DDB' from kernel?\n",
1682 sname);
1683 break;
1684 }
1685 }
1686
1687 /*ARGSUSED*/
1688 static void
1689 kern_clockrate(HANDLER_ARGS)
1690 {
1691 struct clockinfo clkinfo;
1692 size_t sz;
1693 int rc;
1694
1695 sz = sizeof(clkinfo);
1696 rc = sysctl(name, namelen, &clkinfo, &sz, NULL, 0);
1697 if (rc == -1) {
1698 sysctlerror(1);
1699 return;
1700 }
1701 if (sz != sizeof(clkinfo))
1702 errx(1, "%s: !returned size wrong!", sname);
1703
1704 if (xflag || rflag) {
1705 display_struct(pnode, sname, &clkinfo, sz,
1706 DISPLAY_VALUE);
1707 return;
1708 }
1709 else if (!nflag)
1710 printf("%s: ", sname);
1711 printf("tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n",
1712 clkinfo.tick, clkinfo.tickadj,
1713 clkinfo.hz, clkinfo.profhz, clkinfo.stathz);
1714 }
1715
1716 /*ARGSUSED*/
1717 static void
1718 kern_boottime(HANDLER_ARGS)
1719 {
1720 struct timeval timeval;
1721 time_t boottime;
1722 size_t sz;
1723 int rc;
1724
1725 sz = sizeof(timeval);
1726 rc = sysctl(name, namelen, &timeval, &sz, NULL, 0);
1727 if (rc == -1) {
1728 sysctlerror(1);
1729 return;
1730 }
1731 if (sz != sizeof(timeval))
1732 errx(1, "%s: !returned size wrong!", sname);
1733
1734 boottime = timeval.tv_sec;
1735 if (xflag || rflag)
1736 display_struct(pnode, sname, &timeval, sz,
1737 DISPLAY_VALUE);
1738 else if (!nflag)
1739 /* ctime() provides the \n */
1740 printf("%s%s%s", sname, eq, ctime(&boottime));
1741 else if (nflag == 1)
1742 printf("%ld\n", (long)boottime);
1743 else
1744 printf("%ld.%06ld\n", (long)timeval.tv_sec,
1745 (long)timeval.tv_usec);
1746 }
1747
1748 /*ARGSUSED*/
1749 static void
1750 kern_consdev(HANDLER_ARGS)
1751 {
1752 dev_t cons;
1753 size_t sz;
1754 int rc;
1755
1756 sz = sizeof(cons);
1757 rc = sysctl(name, namelen, &cons, &sz, NULL, 0);
1758 if (rc == -1) {
1759 sysctlerror(1);
1760 return;
1761 }
1762 if (sz != sizeof(cons))
1763 errx(1, "%s: !returned size wrong!", sname);
1764
1765 if (xflag || rflag)
1766 display_struct(pnode, sname, &cons, sz,
1767 DISPLAY_VALUE);
1768 else if (!nflag)
1769 printf("%s%s%s\n", sname, eq, devname(cons, S_IFCHR));
1770 else
1771 printf("0x%x\n", cons);
1772 }
1773
1774 /*ARGSUSED*/
1775 static void
1776 kern_cp_time(HANDLER_ARGS)
1777 {
1778 u_int64_t cp_time[CPUSTATES];
1779 size_t sz;
1780 int rc;
1781
1782 sz = sizeof(cp_time);
1783 rc = sysctl(name, namelen, &cp_time, &sz, NULL, 0);
1784 if (rc == -1) {
1785 sysctlerror(1);
1786 return;
1787 }
1788 if (sz != sizeof(cp_time))
1789 errx(1, "%s: !returned size wrong!", sname);
1790
1791 if (xflag || rflag) {
1792 display_struct(pnode, sname, &cp_time, sz,
1793 DISPLAY_VALUE);
1794 return;
1795 }
1796 else if (!nflag)
1797 printf("%s: ", sname);
1798 printf("user = %" PRIu64
1799 ", nice = %" PRIu64
1800 ", sys = %" PRIu64
1801 ", intr = %" PRIu64
1802 ", idle = %" PRIu64
1803 "\n",
1804 cp_time[CP_USER],
1805 cp_time[CP_NICE],
1806 cp_time[CP_SYS],
1807 cp_time[CP_INTR],
1808 cp_time[CP_IDLE]);
1809 }
1810
1811 /*ARGSUSED*/
1812 static void
1813 vm_loadavg(HANDLER_ARGS)
1814 {
1815 struct loadavg loadavg;
1816 size_t sz;
1817 int rc;
1818
1819 sz = sizeof(loadavg);
1820 rc = sysctl(name, namelen, &loadavg, &sz, NULL, 0);
1821 if (rc == -1) {
1822 sysctlerror(1);
1823 return;
1824 }
1825 if (sz != sizeof(loadavg))
1826 errx(1, "%s: !returned size wrong!", sname);
1827
1828 if (xflag || rflag) {
1829 display_struct(pnode, sname, &loadavg, sz,
1830 DISPLAY_VALUE);
1831 return;
1832 }
1833 if (!nflag)
1834 printf("%s: ", sname);
1835 printf("%.2f %.2f %.2f\n",
1836 (double) loadavg.ldavg[0] / loadavg.fscale,
1837 (double) loadavg.ldavg[1] / loadavg.fscale,
1838 (double) loadavg.ldavg[2] / loadavg.fscale);
1839 }
1840
1841 /*ARGSUSED*/
1842 static void
1843 proc_limit(HANDLER_ARGS)
1844 {
1845 u_quad_t olim, *newp, nlim;
1846 size_t osz, nsz;
1847 char *t;
1848 int rc;
1849
1850 osz = sizeof(olim);
1851 if (value != NULL) {
1852 nsz = sizeof(nlim);
1853 newp = &nlim;
1854 if (strcmp(value, "unlimited") == 0)
1855 nlim = RLIM_INFINITY;
1856 else {
1857 errno = 0;
1858 nlim = strtouq(value, &t, 0);
1859 if (*t != '\0' || errno != 0) {
1860 fprintf(warnfp,
1861 "%s: %s: '%s' is not a valid limit\n",
1862 getprogname(), sname, value);
1863 exit(1);
1864 }
1865 }
1866 }
1867 else {
1868 nsz = 0;
1869 newp = NULL;
1870 }
1871
1872 rc = sysctl(name, namelen, &olim, &osz, newp, nsz);
1873 if (rc == -1) {
1874 sysctlerror(newp == NULL);
1875 return;
1876 }
1877
1878 if (newp && qflag)
1879 return;
1880
1881 if (rflag || xflag || olim != RLIM_INFINITY)
1882 display_number(pnode, sname, &olim, sizeof(olim),
1883 newp ? DISPLAY_OLD : DISPLAY_VALUE);
1884 else
1885 display_string(pnode, sname, "unlimited", 10,
1886 newp ? DISPLAY_OLD : DISPLAY_VALUE);
1887
1888 if (newp) {
1889 if (rflag || xflag || nlim != RLIM_INFINITY)
1890 display_number(pnode, sname, &nlim, sizeof(nlim),
1891 DISPLAY_NEW);
1892 else
1893 display_string(pnode, sname, "unlimited", 10,
1894 DISPLAY_NEW);
1895 }
1896 }
1897
1898 #ifdef CPU_DISKINFO
1899 /*ARGSUSED*/
1900 static void
1901 machdep_diskinfo(HANDLER_ARGS)
1902 {
1903 struct disklist *dl;
1904 struct biosdisk_info *bi;
1905 struct nativedisk_info *ni;
1906 int rc;
1907 size_t sz;
1908 uint i, b, lim;
1909
1910 rc = sysctl(name, namelen, NULL, &sz, NULL, 0);
1911 if (rc == -1) {
1912 sysctlerror(1);
1913 return;
1914 }
1915 dl = malloc(sz);
1916 if (dl == NULL) {
1917 sysctlerror(1);
1918 return;
1919 }
1920 rc = sysctl(name, namelen, dl, &sz, NULL, 0);
1921 if (rc == -1) {
1922 sysctlerror(1);
1923 return;
1924 }
1925
1926 if (!nflag)
1927 printf("%s: ", sname);
1928 lim = dl->dl_nbiosdisks;
1929 if (lim > MAX_BIOSDISKS)
1930 lim = MAX_BIOSDISKS;
1931 for (bi = dl->dl_biosdisks, i = 0; i < lim; bi++, i++)
1932 printf("%x:%" PRIu64 "(%d/%d/%d),%x ",
1933 bi->bi_dev, bi->bi_lbasecs,
1934 bi->bi_cyl, bi->bi_head, bi->bi_sec,
1935 bi->bi_flags);
1936 lim = dl->dl_nnativedisks;
1937 ni = dl->dl_nativedisks;
1938 bi = dl->dl_biosdisks;
1939 /* LINTED -- pointer casts are tedious */
1940 if ((char *)&ni[lim] != (char *)dl + sz) {
1941 fprintf(warnfp, "size mismatch\n");
1942 return;
1943 }
1944 for (i = 0; i < lim; ni++, i++) {
1945 char t = ':';
1946 printf(" %.*s", (int)sizeof ni->ni_devname,
1947 ni->ni_devname);
1948 for (b = 0; b < ni->ni_nmatches; t = ',', b++)
1949 printf("%c%x", t,
1950 bi[ni->ni_biosmatches[b]].bi_dev);
1951 }
1952 printf("\n");
1953 }
1954 #endif /* CPU_DISKINFO */
1955