AMD64 - AUDIT RUN - Fix format strings, size_t, and other issues
[dragonfly.git] / sbin / sysctl / sysctl.c
1 /*
2  * Copyright (c) 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)from: sysctl.c   8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/sbin/sysctl/sysctl.c,v 1.25.2.11 2003/05/01 22:48:08 trhodes Exp $
36  * $DragonFly: src/sbin/sysctl/sysctl.c,v 1.16 2007/10/02 12:57:00 hasso Exp $
37  */
38
39 #ifdef __i386__
40 #include <sys/diskslice.h>      /* used for bootdev parsing */
41 #include <sys/reboot.h>         /* used for bootdev parsing */
42 #endif
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/sysctl.h>
46 #include <sys/resource.h>
47 #include <sys/sensors.h>
48 #include <sys/param.h>
49
50 #include <machine/inttypes.h>
51
52 #include <ctype.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 static int      aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag;
61
62 static int      oidfmt(int *, size_t, char *, u_int *);
63 static void     parse(const char *);
64 static int      show_var(int *, size_t);
65 static int      sysctl_all(int *, size_t);
66 static void     set_T_dev_t(const char *, void **, size_t *);
67
68 static void
69 usage(void)
70 {
71
72         fprintf(stderr, "%s\n%s\n",
73             "usage: sysctl [-bdeNnox] variable[=value] ...",
74             "       sysctl [-bdeNnox] -a");
75         exit(1);
76 }
77
78 int
79 main(int argc, char **argv)
80 {
81         int ch;
82         setbuf(stdout,0);
83         setbuf(stderr,0);
84
85         while ((ch = getopt(argc, argv, "AabdeNnowxX")) != -1) {
86                 switch (ch) {
87                 case 'A':
88                         /* compatibility */
89                         aflag = oflag = 1;
90                         break;
91                 case 'a':
92                         aflag = 1;
93                         break;
94                 case 'b':
95                         bflag = 1;
96                         break;
97                 case 'd':
98                         dflag = 1;
99                         break;
100                 case 'e':
101                         eflag = 1;
102                         break;
103                 case 'N':
104                         Nflag = 1;
105                         break;
106                 case 'n':
107                         nflag = 1;
108                         break;
109                 case 'o':
110                         oflag = 1;
111                         break;
112                 case 'w':
113                         /* compatibility */
114                         /* ignored */
115                         break;
116                 case 'X':
117                         /* compatibility */
118                         aflag = xflag = 1;
119                         break;
120                 case 'x':
121                         xflag = 1;
122                         break;
123                 default:
124                         usage();
125                 }
126         }
127         argc -= optind;
128         argv += optind;
129
130         if (Nflag && nflag)
131                 usage();
132         if (aflag && argc == 0)
133                 exit(sysctl_all(0, 0));
134         if (argc == 0)
135                 usage();
136         while (argc-- > 0)
137                 parse(*argv++);
138         exit(0);
139 }
140
141 /*
142  * Parse a name into a MIB entry.
143  * Lookup and print out the MIB entry if it exists.
144  * Set a new value if requested.
145  */
146 static void
147 parse(const char *string)
148 {
149         size_t len;
150         int i, j;
151         void *newval = NULL;
152         int intval;
153         unsigned int uintval;
154         long longval;
155         unsigned long ulongval;
156         size_t newsize = 0;
157         quad_t quadval;
158         u_quad_t uquadval;
159         int mib[CTL_MAXNAME];
160         char *cp, fmt[BUFSIZ];
161         const char *name;
162         char *name_allocated = NULL;
163         u_int kind;
164
165         if ((cp = strchr(string, '=')) != NULL) {
166                 if ((name_allocated = malloc(cp - string + 1)) == NULL)
167                         err(1, "malloc failed");
168                 strlcpy(name_allocated, string, cp - string + 1);
169                 name = name_allocated;
170                 
171                 while (isspace(*++cp))
172                         ;
173
174                 newval = cp;
175                 newsize = strlen(cp);
176         } else {
177                 name = string;
178         }
179
180         len = CTL_MAXNAME;
181         if (sysctlnametomib(name, mib, &len) < 0) {
182                 if (errno == ENOENT) {
183                         errx(1, "unknown oid '%s'", name);
184                 } else {
185                         err(1, "sysctlnametomib(\"%s\")", name);
186                 }
187         }
188
189         if (oidfmt(mib, len, fmt, &kind))
190                 err(1, "couldn't find format of oid '%s'", name);
191
192         if (newval == NULL) {
193                 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
194                         sysctl_all(mib, len);
195                 } else {
196                         i = show_var(mib, len);
197                         if (!i && !bflag)
198                                 putchar('\n');
199                 }
200         } else {
201                 if ((kind & CTLTYPE) == CTLTYPE_NODE)
202                         errx(1, "oid '%s' isn't a leaf node", name);
203
204                 if (!(kind&CTLFLAG_WR))
205                         errx(1, "oid '%s' is read only", name);
206         
207                 switch (kind & CTLTYPE) {
208                         case CTLTYPE_INT:
209                                 intval = (int) strtol(newval, NULL, 0);
210                                 newval = &intval;
211                                 newsize = sizeof(intval);
212                                 break;
213                         case CTLTYPE_UINT:
214                                 uintval = (int) strtoul(newval, NULL, 0);
215                                 newval = &uintval;
216                                 newsize = sizeof uintval;
217                                 break;
218                         case CTLTYPE_LONG:
219                                 longval = strtol(newval, NULL, 0);
220                                 newval = &longval;
221                                 newsize = sizeof longval;
222                                 break;
223                         case CTLTYPE_ULONG:
224                                 ulongval = strtoul(newval, NULL, 0);
225                                 newval = &ulongval;
226                                 newsize = sizeof ulongval;
227                                 break;
228                         case CTLTYPE_STRING:
229                                 break;
230                         case CTLTYPE_QUAD:
231                                 quadval = strtoq(newval, NULL, 0);
232                                 newval = &quadval;
233                                 newsize = sizeof(quadval);
234                                 break;
235                         case CTLTYPE_UQUAD:
236                                 uquadval = strtouq(newval, NULL, 0);
237                                 newval = &quadval;
238                                 newsize = sizeof(quadval);
239                                 break;
240                         case CTLTYPE_OPAQUE:
241                                 if (strcmp(fmt, "T,dev_t") == 0 ||
242                                     strcmp(fmt, "T,udev_t") == 0
243                                 ) {
244                                         set_T_dev_t((char*)newval, &newval,
245                                                     &newsize);
246                                         break;
247                                 }
248                                 /* FALLTHROUGH */
249                         default:
250                                 errx(1, "oid '%s' is type %d,"
251                                         " cannot set that", name,
252                                         kind & CTLTYPE);
253                 }
254
255                 i = show_var(mib, len);
256                 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
257                         if (!i && !bflag)
258                                 putchar('\n');
259                         switch (errno) {
260                         case EOPNOTSUPP:
261                                 errx(1, "%s: value is not available", 
262                                         string);
263                         case ENOTDIR:
264                                 errx(1, "%s: specification is incomplete", 
265                                         string);
266                         case ENOMEM:
267                                 errx(1, "%s: type is unknown to this program", 
268                                         string);
269                         default:
270                                 warn("%s", string);
271                                 return;
272                         }
273                 }
274                 if (!bflag)
275                         printf(" -> ");
276                 i = nflag;
277                 nflag = 1;
278                 j = show_var(mib, len);
279                 if (!j && !bflag)
280                         putchar('\n');
281                 nflag = i;
282         }
283
284         if (name_allocated != NULL)
285                 free(name_allocated);
286 }
287
288 /* These functions will dump out various interesting structures. */
289
290 static int
291 S_clockinfo(int l2, void *p)
292 {
293         struct clockinfo *ci = (struct clockinfo*)p;
294         if (l2 != sizeof(*ci))
295                 err(1, "S_clockinfo %d != %zu", l2, sizeof(*ci));
296         printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
297                 ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz);
298         return (0);
299 }
300
301 static int
302 S_loadavg(int l2, void *p)
303 {
304         struct loadavg *tv = (struct loadavg*)p;
305
306         if (l2 != sizeof(*tv))
307                 err(1, "S_loadavg %d != %zu", l2, sizeof(*tv));
308
309         printf("{ %.2f %.2f %.2f }",
310                 (double)tv->ldavg[0]/(double)tv->fscale,
311                 (double)tv->ldavg[1]/(double)tv->fscale,
312                 (double)tv->ldavg[2]/(double)tv->fscale);
313         return (0);
314 }
315
316 static int
317 S_timespec(int l2, void *p)
318 {
319         struct timespec *ts = (struct timespec*)p;
320         time_t tv_sec;
321         char *p1, *p2;
322
323         if (l2 != sizeof(*ts))
324                 err(1, "S_timespec %d != %zu", l2, sizeof(*ts));
325         printf("{ sec = %ld, nsec = %ld } ",
326                 ts->tv_sec, ts->tv_nsec);
327         tv_sec = ts->tv_sec;
328         p1 = strdup(ctime(&tv_sec));
329         for (p2=p1; *p2 ; p2++)
330                 if (*p2 == '\n')
331                         *p2 = '\0';
332         fputs(p1, stdout);
333         return (0);
334 }
335
336 static int
337 S_timeval(int l2, void *p)
338 {
339         struct timeval *tv = (struct timeval*)p;
340         time_t tv_sec;
341         char *p1, *p2;
342
343         if (l2 != sizeof(*tv))
344                 err(1, "S_timeval %d != %zu", l2, sizeof(*tv));
345         printf("{ sec = %ld, usec = %ld } ",
346                 tv->tv_sec, tv->tv_usec);
347         tv_sec = tv->tv_sec;
348         p1 = strdup(ctime(&tv_sec));
349         for (p2=p1; *p2 ; p2++)
350                 if (*p2 == '\n')
351                         *p2 = '\0';
352         fputs(p1, stdout);
353         return (0);
354 }
355
356 static int
357 S_sensor(int l2, void *p)
358 {
359         struct sensor *s = (struct sensor *)p;
360
361         if (l2 != sizeof(*s)) {
362                 warnx("S_sensor %d != %zu", l2, sizeof(*s));
363                 return (1);
364         }
365
366         if (s->flags & SENSOR_FINVALID) {
367                 /*
368                  * XXX: with this flag, the node should be entirely ignored,
369                  * but as the magic-based sysctl(8) is not too flexible, we
370                  * simply have to print out that the sensor is invalid.
371                  */
372                 printf("invalid");
373                 return (0);
374         }
375
376         if (s->flags & SENSOR_FUNKNOWN)
377                 printf("unknown");
378         else {
379                 switch (s->type) {
380                 case SENSOR_TEMP:
381                         printf("%.2f degC",
382                             (s->value - 273150000) / 1000000.0);
383                         break;
384                 case SENSOR_FANRPM:
385                         printf("%jd RPM", (intmax_t)s->value);
386                         break;
387                 case SENSOR_VOLTS_DC:
388                         printf("%.2f VDC", s->value / 1000000.0);
389                         break;
390                 case SENSOR_AMPS:
391                         printf("%.2f A", s->value / 1000000.0);
392                         break;
393                 case SENSOR_WATTHOUR:
394                         printf("%.2f Wh", s->value / 1000000.0);
395                         break;
396                 case SENSOR_AMPHOUR:
397                         printf("%.2f Ah", s->value / 1000000.0);
398                         break;
399                 case SENSOR_INDICATOR:
400                         printf("%s", s->value ? "On" : "Off");
401                         break;
402                 case SENSOR_INTEGER:
403                         printf("%jd", (intmax_t)s->value);
404                         break;
405                 case SENSOR_PERCENT:
406                         printf("%.2f%%", s->value / 1000.0);
407                         break;
408                 case SENSOR_LUX:
409                         printf("%.2f lx", s->value / 1000000.0);
410                         break;
411                 case SENSOR_DRIVE:
412                 {
413                         const char *name;
414
415                         switch (s->value) {
416                         case SENSOR_DRIVE_EMPTY:
417                                 name = "empty";
418                                 break;
419                         case SENSOR_DRIVE_READY:
420                                 name = "ready";
421                                 break;
422                         case SENSOR_DRIVE_POWERUP:
423                                 name = "powering up";
424                                 break;
425                         case SENSOR_DRIVE_ONLINE:
426                                 name = "online";
427                                 break;
428                         case SENSOR_DRIVE_IDLE:
429                                 name = "idle";
430                                 break;
431                         case SENSOR_DRIVE_ACTIVE:
432                                 name = "active";
433                                 break;
434                         case SENSOR_DRIVE_REBUILD:
435                                 name = "rebuilding";
436                                 break;
437                         case SENSOR_DRIVE_POWERDOWN:
438                                 name = "powering down";
439                                 break;
440                         case SENSOR_DRIVE_FAIL:
441                                 name = "failed";
442                                 break;
443                         case SENSOR_DRIVE_PFAIL:
444                                 name = "degraded";
445                                 break;
446                         default:
447                                 name = "unknown";
448                                 break;
449                         }
450                         printf(name);
451                         break;
452                 }
453                 case SENSOR_TIMEDELTA:
454                         printf("%.6f secs", s->value / 1000000000.0);
455                         break;
456                 default:
457                         printf("unknown");
458                 }
459         }
460
461         if (s->desc[0] != '\0')
462                 printf(" (%s)", s->desc);
463
464         switch (s->status) {
465         case SENSOR_S_UNSPEC:
466                 break;
467         case SENSOR_S_OK:
468                 printf(", OK");
469                 break;
470         case SENSOR_S_WARN:
471                 printf(", WARNING");
472                 break;
473         case SENSOR_S_CRIT:
474                 printf(", CRITICAL");
475                 break;
476         case SENSOR_S_UNKNOWN:
477                 printf(", UNKNOWN");
478                 break;
479         }
480
481         if (s->tv.tv_sec) {
482                 time_t t = s->tv.tv_sec;
483                 char ct[26];
484
485                 ctime_r(&t, ct);
486                 ct[19] = '\0';
487                 printf(", %s.%03ld", ct, s->tv.tv_usec / 1000);
488         }
489
490         return (0);
491 }
492
493 static int
494 T_dev_t(int l2, void *p)
495 {
496         dev_t *d = (dev_t *)p;
497         if (l2 != sizeof(*d))
498                 err(1, "T_dev_T %d != %zu", l2, sizeof(*d));
499         if ((int)(*d) != -1) {
500                 if (minor(*d) > 255 || minor(*d) < 0)
501                         printf("{ major = %d, minor = 0x%x }",
502                                 major(*d), minor(*d));
503                 else
504                         printf("{ major = %d, minor = %d }",
505                                 major(*d), minor(*d));
506         }
507         return (0);
508 }
509
510 static void
511 set_T_dev_t(const char *path, void **val, size_t *size)
512 {
513         static struct stat statb;
514
515         if (strcmp(path, "none") && strcmp(path, "off")) {
516                 int rc = stat (path, &statb);
517                 if (rc) {
518                         err(1, "cannot stat %s", path);
519                 }
520
521                 if (!S_ISCHR(statb.st_mode)) {
522                         errx(1, "must specify a device special file.");
523                 }
524         } else {
525                 statb.st_rdev = NODEV;
526         }
527         *val = (char*) &statb.st_rdev;
528         *size = sizeof statb.st_rdev;
529 }
530
531 /*
532  * These functions uses a presently undocumented interface to the kernel
533  * to walk the tree and get the type so it can print the value.
534  * This interface is under work and consideration, and should probably
535  * be killed with a big axe by the first person who can find the time.
536  * (be aware though, that the proper interface isn't as obvious as it
537  * may seem, there are various conflicting requirements.
538  */
539
540 static int
541 oidfmt(int *oid, size_t len, char *fmt, u_int *kind)
542 {
543         int qoid[CTL_MAXNAME+2];
544         u_char buf[BUFSIZ];
545         int i;
546         size_t j;
547
548         qoid[0] = 0;
549         qoid[1] = 4;
550         memcpy(qoid + 2, oid, len * sizeof(int));
551
552         j = sizeof(buf);
553         i = sysctl(qoid, len + 2, buf, &j, 0, 0);
554         if (i)
555                 err(1, "sysctl fmt %d %zu %d", i, j, errno);
556
557         if (kind)
558                 *kind = *(u_int *)buf;
559
560         if (fmt)
561                 strcpy(fmt, (char *)(buf + sizeof(u_int)));
562         return 0;
563 }
564
565 #ifdef __i386__
566 /*
567  * Code to map a bootdev major number into a suitable device name.
568  * Major numbers are mapped into names as in boot2.c
569  */
570 struct _foo {
571         int majdev;
572         const char *name;
573 } maj2name[] = {
574         { 30,   "ad" },
575         { 0,    "wd" },
576         { 1,    "wfd" },
577         { 2,    "fd" },
578         { 4,    "da" },
579         { -1,   NULL }  /* terminator */
580 };
581
582 static int
583 machdep_bootdev(u_long value)
584 {
585         int majdev, unit, slice, part;
586         struct _foo *p;
587
588         if ((value & B_MAGICMASK) != B_DEVMAGIC) {
589                 printf("invalid (0x%08lx)", value);
590                 return 0;
591         }
592         majdev = B_TYPE(value);
593         unit = B_UNIT(value);
594         slice = B_SLICE(value);
595         part = B_PARTITION(value);
596         if (majdev == 2) {      /* floppy, as known to the boot block... */
597                 printf("/dev/fd%d", unit);
598                 return 0;
599         }
600         for (p = maj2name; p->name != NULL && p->majdev != majdev ; p++) ;
601         if (p->name != NULL) {  /* found */
602                 if (slice == WHOLE_DISK_SLICE)
603                         printf("/dev/%s%d%c", p->name, unit, part);
604                 else
605                         printf("/dev/%s%ds%d%c",
606                             p->name, unit, slice - BASE_SLICE + 1, part + 'a');
607         } else
608                 printf("unknown (major %d unit %d slice %d part %d)",
609                         majdev, unit, slice, part);
610         return 0;
611 }
612 #endif
613
614 /*
615  * This formats and outputs the value of one variable
616  *
617  * Returns zero if anything was actually output.
618  * Returns one if didn't know what to do with this.
619  * Return minus one if we had errors.
620  */
621
622 static int
623 show_var(int *oid, size_t nlen)
624 {
625         u_char buf[BUFSIZ], *val, *p, *nul;
626         char name[BUFSIZ], *fmt;
627         const char *sep, *spacer;
628         int qoid[CTL_MAXNAME+2];
629         int i;
630         size_t j, len;
631         u_int kind;
632         int (*func)(int, void *);
633
634         qoid[0] = 0;
635         memcpy(qoid + 2, oid, nlen * sizeof(int));
636
637         qoid[1] = 1;
638         j = sizeof(name);
639         i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
640         if (i || !j)
641                 err(1, "sysctl name %d %zu %d", i, j, errno);
642
643         if (Nflag) {
644                 printf("%s", name);
645                 return (0);
646         }
647
648         if (eflag)
649                 sep = "=";
650         else
651                 sep = ": ";
652
653         if (dflag) {    /* just print description */
654                 qoid[1] = 5;
655                 j = sizeof(buf);
656                 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
657                 if (!nflag)
658                         printf("%s%s", name, sep);
659                 printf("%s", buf);
660                 return(0);
661         }
662         /* find an estimate of how much we need for this var */
663         j = 0;
664         i = sysctl(oid, nlen, 0, &j, 0, 0);
665         j += j; /* we want to be sure :-) */
666
667         val = alloca(j + 1);
668         len = j;
669         i = sysctl(oid, nlen, val, &len, 0, 0);
670         if (i || !len)
671                 return (1);
672
673         if (bflag) {
674                 fwrite(val, 1, len, stdout);
675                 return (0);
676         }
677
678         val[len] = '\0';
679         fmt = buf;
680         oidfmt(oid, nlen, fmt, &kind);
681         p = val;
682         switch (*fmt) {
683         case 'A':
684                 if (!nflag)
685                         printf("%s%s", name, sep);
686                 nul = memchr(p, '\0', len);
687                 fwrite(p, nul == NULL ? len : nul - p, 1, stdout);
688                 return (0);
689                 
690         case 'I':
691                 if (!nflag)
692                         printf("%s%s", name, sep);
693                 fmt++;
694                 spacer = "";
695                 while (len >= sizeof(int)) {
696                         if(*fmt == 'U')
697                                 printf("%s%u", spacer, *(unsigned int *)p);
698                         else
699                                 printf("%s%d", spacer, *(int *)p);
700                         spacer = " ";
701                         len -= sizeof(int);
702                         p += sizeof(int);
703                 }
704                 return (0);
705
706         case 'L':
707                 if (!nflag)
708                         printf("%s%s", name, sep);
709                 fmt++;
710 #ifdef __i386__
711                 if (!strcmp(name, "machdep.guessed_bootdev"))
712                         return machdep_bootdev(*(unsigned long *)p);
713 #endif
714                 spacer = "";
715                 while (len >= sizeof(long)) {
716                         if(*fmt == 'U')
717                                 printf("%s%lu", spacer, *(unsigned long *)p);
718                         else
719                                 printf("%s%ld", spacer, *(long *)p);
720                         spacer = " ";
721                         len -= sizeof(long);
722                         p += sizeof(long);
723                 }
724                 return (0);
725
726         case 'P':
727                 if (!nflag)
728                         printf("%s%s", name, sep);
729                 printf("%p", *(void **)p);
730                 return (0);
731
732         case 'Q':
733                 if (!nflag)
734                         printf("%s%s", name, sep);
735                 fmt++;
736                 spacer = "";
737                 while (len >= sizeof(quad_t)) {
738                         if(*fmt == 'U') {
739                                 printf("%s%ju",
740                                        spacer, (uintmax_t)*(u_quad_t *)p);
741                         } else {
742                                 printf("%s%jd",
743                                        spacer, (intmax_t)*(quad_t *)p);
744                         }
745                         spacer = " ";
746                         len -= sizeof(int64_t);
747                         p += sizeof(int64_t);
748                 }
749                 return (0);
750
751         case 'T':
752         case 'S':
753                 if (!oflag && !xflag) {
754                         i = 0;
755                         if (strcmp(fmt, "S,clockinfo") == 0)
756                                 func = S_clockinfo;
757                         else if (strcmp(fmt, "S,timespec") == 0)
758                                 func = S_timespec;
759                         else if (strcmp(fmt, "S,timeval") == 0)
760                                 func = S_timeval;
761                         else if (strcmp(fmt, "S,loadavg") == 0)
762                                 func = S_loadavg;
763                         else if (strcmp(fmt, "S,sensor") == 0)
764                                 func = S_sensor;
765                         else if (strcmp(fmt, "T,dev_t") == 0)
766                                 func = T_dev_t;
767                         else if (strcmp(fmt, "T,udev_t") == 0)
768                                 func = T_dev_t;
769                         else
770                                 func = NULL;
771                         if (func) {
772                                 if (!nflag)
773                                         printf("%s%s", name, sep);
774                                 return ((*func)(len, p));
775                         }
776                 }
777                 /* FALL THROUGH */
778         default:
779                 if (!oflag && !xflag)
780                         return (1);
781                 if (!nflag)
782                         printf("%s%s", name, sep);
783                 printf("Format:%s Length:%zu Dump:0x", fmt, len);
784                 while (len-- && (xflag || p < val + 16))
785                         printf("%02x", *p++);
786                 if (!xflag && len > 16)
787                         printf("...");
788                 return (0);
789         }
790         return (1);
791 }
792
793 static int
794 sysctl_all(int *oid, size_t len)
795 {
796         int name1[22], name2[22];
797         int retval;
798         size_t i, l1, l2;
799
800         name1[0] = 0;
801         name1[1] = 2;
802         l1 = 2;
803         if (len) {
804                 memcpy(name1+2, oid, len * sizeof(int));
805                 l1 += len;
806         } else {
807                 name1[2] = 1;
808                 l1++;
809         }
810         for (;;) {
811                 l2 = sizeof(name2);
812                 retval = sysctl(name1, l1, name2, &l2, 0, 0);
813                 if (retval < 0) {
814                         if (errno == ENOENT)
815                                 return 0;
816                         else
817                                 err(1, "sysctl(getnext) %d %zu", retval, l2);
818                 }
819
820                 l2 /= sizeof(int);
821
822                 if (l2 < len)
823                         return 0;
824
825                 for (i = 0; i < len; i++)
826                         if (name2[i] != oid[i])
827                                 return 0;
828
829                 retval = show_var(name2, l2);
830                 if (retval == 0 && !bflag)
831                         putchar('\n');
832
833                 memcpy(name1+2, name2, l2 * sizeof(int));
834                 l1 = 2 + l2;
835         }
836 }