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