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