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