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