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