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