]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sbin/sysctl/sysctl.c
MFC r294102:
[FreeBSD/stable/8.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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1993\n\
33         The Regents of the University of California.  All rights reserved.\n";
34 #endif /* not lint */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)from: sysctl.c      8.1 (Berkeley) 6/6/93";
39 #endif
40 static const char rcsid[] =
41   "$FreeBSD$";
42 #endif /* not lint */
43
44 #include <sys/param.h>
45 #include <sys/time.h>
46 #include <sys/resource.h>
47 #include <sys/stat.h>
48 #include <sys/sysctl.h>
49 #include <sys/vmmeter.h>
50
51 #include <ctype.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <inttypes.h>
55 #include <locale.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 static int      aflag, bflag, dflag, eflag, hflag, iflag;
62 static int      Nflag, nflag, oflag, qflag, xflag, warncount;
63
64 static int      oidfmt(int *, int, char *, u_int *);
65 static void     parse(const char *);
66 static int      show_var(int *, int);
67 static int      sysctl_all(int *oid, int len);
68 static int      name2oid(char *, int *);
69
70 static void     set_T_dev_t(char *, void **, size_t *);
71 static int      set_IK(char *, int *);
72
73 static void
74 usage(void)
75 {
76
77         (void)fprintf(stderr, "%s\n%s\n",
78             "usage: sysctl [-bdehiNnoqx] name[=value] ...",
79             "       sysctl [-bdehNnoqx] -a");
80         exit(1);
81 }
82
83 int
84 main(int argc, char **argv)
85 {
86         int ch;
87
88         setlocale(LC_NUMERIC, "");
89         setbuf(stdout,0);
90         setbuf(stderr,0);
91
92         while ((ch = getopt(argc, argv, "AabdehiNnoqwxX")) != -1) {
93                 switch (ch) {
94                 case 'A':
95                         /* compatibility */
96                         aflag = oflag = 1;
97                         break;
98                 case 'a':
99                         aflag = 1;
100                         break;
101                 case 'b':
102                         bflag = 1;
103                         break;
104                 case 'd':
105                         dflag = 1;
106                         break;
107                 case 'e':
108                         eflag = 1;
109                         break;
110                 case 'h':
111                         hflag = 1;
112                         break;
113                 case 'i':
114                         iflag = 1;
115                         break;
116                 case 'N':
117                         Nflag = 1;
118                         break;
119                 case 'n':
120                         nflag = 1;
121                         break;
122                 case 'o':
123                         oflag = 1;
124                         break;
125                 case 'q':
126                         qflag = 1;
127                         break;
128                 case 'w':
129                         /* compatibility */
130                         /* ignored */
131                         break;
132                 case 'X':
133                         /* compatibility */
134                         aflag = xflag = 1;
135                         break;
136                 case 'x':
137                         xflag = 1;
138                         break;
139                 default:
140                         usage();
141                 }
142         }
143         argc -= optind;
144         argv += optind;
145
146         if (Nflag && nflag)
147                 usage();
148         if (aflag && argc == 0)
149                 exit(sysctl_all(0, 0));
150         if (argc == 0)
151                 usage();
152
153         warncount = 0;
154         while (argc-- > 0)
155                 parse(*argv++);
156         exit(warncount);
157 }
158
159 /*
160  * Parse a name into a MIB entry.
161  * Lookup and print out the MIB entry if it exists.
162  * Set a new value if requested.
163  */
164 static void
165 parse(const char *string)
166 {
167         int len, i, j;
168         void *newval = 0;
169         int intval;
170         unsigned int uintval;
171         long longval;
172         unsigned long ulongval;
173         size_t newsize = 0;
174         quad_t quadval;
175         int mib[CTL_MAXNAME];
176         char *cp, *bufp, buf[BUFSIZ], *endptr, fmt[BUFSIZ];
177         u_int kind;
178
179         cp = buf;
180         if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ)
181                 errx(1, "oid too long: '%s'", string);
182         bufp = strsep(&cp, "=");
183         if (cp != NULL) {
184                 while (isspace(*cp))
185                         cp++;
186                 newval = cp;
187                 newsize = strlen(cp);
188         }
189         len = name2oid(bufp, mib);
190
191         if (len < 0) {
192                 if (iflag)
193                         return;
194                 if (qflag)
195                         exit(1);
196                 else
197                         errx(1, "unknown oid '%s'", bufp);
198         }
199
200         if (oidfmt(mib, len, fmt, &kind))
201                 err(1, "couldn't find format of oid '%s'", bufp);
202
203         if (newval == NULL || dflag) {
204                 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
205                         if (dflag) {
206                                 i = show_var(mib, len);
207                                 if (!i && !bflag)
208                                         putchar('\n');
209                         }
210                         sysctl_all(mib, len);
211                 } else {
212                         i = show_var(mib, len);
213                         if (!i && !bflag)
214                                 putchar('\n');
215                 }
216         } else {
217                 if ((kind & CTLTYPE) == CTLTYPE_NODE)
218                         errx(1, "oid '%s' isn't a leaf node", bufp);
219
220                 if (!(kind & CTLFLAG_WR)) {
221                         if (kind & CTLFLAG_TUN) {
222                                 warnx("oid '%s' is a read only tunable", bufp);
223                                 errx(1, "Tunable values are set in /boot/loader.conf");
224                         } else {
225                                 errx(1, "oid '%s' is read only", bufp);
226                         }
227                 }
228
229                 if ((kind & CTLTYPE) == CTLTYPE_INT ||
230                     (kind & CTLTYPE) == CTLTYPE_UINT ||
231                     (kind & CTLTYPE) == CTLTYPE_LONG ||
232                     (kind & CTLTYPE) == CTLTYPE_ULONG ||
233                     (kind & CTLTYPE) == CTLTYPE_QUAD) {
234                         if (strlen(newval) == 0)
235                                 errx(1, "empty numeric value");
236                 }
237
238                 switch (kind & CTLTYPE) {
239                         case CTLTYPE_INT:
240                                 if (strcmp(fmt, "IK") == 0) {
241                                         if (!set_IK(newval, &intval))
242                                                 errx(1, "invalid value '%s'",
243                                                     (char *)newval);
244                                 } else {
245                                         intval = (int)strtol(newval, &endptr,
246                                             0);
247                                         if (endptr == newval || *endptr != '\0')
248                                                 errx(1, "invalid integer '%s'",
249                                                     (char *)newval);
250                                 }
251                                 newval = &intval;
252                                 newsize = sizeof(intval);
253                                 break;
254                         case CTLTYPE_UINT:
255                                 uintval = (int) strtoul(newval, &endptr, 0);
256                                 if (endptr == newval || *endptr != '\0')
257                                         errx(1, "invalid unsigned integer '%s'",
258                                             (char *)newval);
259                                 newval = &uintval;
260                                 newsize = sizeof(uintval);
261                                 break;
262                         case CTLTYPE_LONG:
263                                 longval = strtol(newval, &endptr, 0);
264                                 if (endptr == newval || *endptr != '\0')
265                                         errx(1, "invalid long integer '%s'",
266                                             (char *)newval);
267                                 newval = &longval;
268                                 newsize = sizeof(longval);
269                                 break;
270                         case CTLTYPE_ULONG:
271                                 ulongval = strtoul(newval, &endptr, 0);
272                                 if (endptr == newval || *endptr != '\0')
273                                         errx(1, "invalid unsigned long integer"
274                                             " '%s'", (char *)newval);
275                                 newval = &ulongval;
276                                 newsize = sizeof(ulongval);
277                                 break;
278                         case CTLTYPE_STRING:
279                                 break;
280                         case CTLTYPE_QUAD:
281                                 sscanf(newval, "%qd", &quadval);
282                                 newval = &quadval;
283                                 newsize = sizeof(quadval);
284                                 break;
285                         case CTLTYPE_OPAQUE:
286                                 if (strcmp(fmt, "T,dev_t") == 0) {
287                                         set_T_dev_t (newval, &newval, &newsize);
288                                         break;
289                                 }
290                                 /* FALLTHROUGH */
291                         default:
292                                 errx(1, "oid '%s' is type %d,"
293                                         " cannot set that", bufp,
294                                         kind & CTLTYPE);
295                 }
296
297                 i = show_var(mib, len);
298                 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
299                         if (!i && !bflag)
300                                 putchar('\n');
301                         switch (errno) {
302                         case EOPNOTSUPP:
303                                 errx(1, "%s: value is not available",
304                                         string);
305                         case ENOTDIR:
306                                 errx(1, "%s: specification is incomplete",
307                                         string);
308                         case ENOMEM:
309                                 errx(1, "%s: type is unknown to this program",
310                                         string);
311                         default:
312                                 warn("%s", string);
313                                 warncount++;
314                                 return;
315                         }
316                 }
317                 if (!bflag)
318                         printf(" -> ");
319                 i = nflag;
320                 nflag = 1;
321                 j = show_var(mib, len);
322                 if (!j && !bflag)
323                         putchar('\n');
324                 nflag = i;
325         }
326 }
327
328 /* These functions will dump out various interesting structures. */
329
330 static int
331 S_clockinfo(int l2, void *p)
332 {
333         struct clockinfo *ci = (struct clockinfo*)p;
334
335         if (l2 != sizeof(*ci)) {
336                 warnx("S_clockinfo %d != %d", l2, sizeof(*ci));
337                 return (1);
338         }
339         printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
340                 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
341                 ci->hz, ci->tick, ci->profhz, ci->stathz);
342         return (0);
343 }
344
345 static int
346 S_loadavg(int l2, void *p)
347 {
348         struct loadavg *tv = (struct loadavg*)p;
349
350         if (l2 != sizeof(*tv)) {
351                 warnx("S_loadavg %d != %d", l2, sizeof(*tv));
352                 return (1);
353         }
354         printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
355                 (double)tv->ldavg[0]/(double)tv->fscale,
356                 (double)tv->ldavg[1]/(double)tv->fscale,
357                 (double)tv->ldavg[2]/(double)tv->fscale);
358         return (0);
359 }
360
361 static int
362 S_timeval(int l2, void *p)
363 {
364         struct timeval *tv = (struct timeval*)p;
365         time_t tv_sec;
366         char *p1, *p2;
367
368         if (l2 != sizeof(*tv)) {
369                 warnx("S_timeval %d != %d", l2, sizeof(*tv));
370                 return (1);
371         }
372         printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
373                 "{ sec = %jd, usec = %ld } ",
374                 (intmax_t)tv->tv_sec, tv->tv_usec);
375         tv_sec = tv->tv_sec;
376         p1 = strdup(ctime(&tv_sec));
377         for (p2=p1; *p2 ; p2++)
378                 if (*p2 == '\n')
379                         *p2 = '\0';
380         fputs(p1, stdout);
381         free(p1);
382         return (0);
383 }
384
385 static int
386 S_vmtotal(int l2, void *p)
387 {
388         struct vmtotal *v = (struct vmtotal *)p;
389         int pageKilo = getpagesize() / 1024;
390
391         if (l2 != sizeof(*v)) {
392                 warnx("S_vmtotal %d != %d", l2, sizeof(*v));
393                 return (1);
394         }
395
396         printf(
397             "\nSystem wide totals computed every five seconds:"
398             " (values in kilobytes)\n");
399         printf("===============================================\n");
400         printf(
401             "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: "
402             "%hd Sleep: %hd)\n",
403             v->t_rq, v->t_dw, v->t_pw, v->t_sl);
404         printf(
405             "Virtual Memory:\t\t(Total: %dK Active: %dK)\n",
406             v->t_vm * pageKilo, v->t_avm * pageKilo);
407         printf("Real Memory:\t\t(Total: %dK Active: %dK)\n",
408             v->t_rm * pageKilo, v->t_arm * pageKilo);
409         printf("Shared Virtual Memory:\t(Total: %dK Active: %dK)\n",
410             v->t_vmshr * pageKilo, v->t_avmshr * pageKilo);
411         printf("Shared Real Memory:\t(Total: %dK Active: %dK)\n",
412             v->t_rmshr * pageKilo, v->t_armshr * pageKilo);
413         printf("Free Memory:\t%dK\n", v->t_free * pageKilo);
414
415         return (0);
416 }
417
418 static int
419 T_dev_t(int l2, void *p)
420 {
421         dev_t *d = (dev_t *)p;
422
423         if (l2 != sizeof(*d)) {
424                 warnx("T_dev_T %d != %d", l2, sizeof(*d));
425                 return (1);
426         }
427         if ((int)(*d) != -1) {
428                 if (minor(*d) > 255 || minor(*d) < 0)
429                         printf("{ major = %d, minor = 0x%x }",
430                                 major(*d), minor(*d));
431                 else
432                         printf("{ major = %d, minor = %d }",
433                                 major(*d), minor(*d));
434         }
435         return (0);
436 }
437
438 static void
439 set_T_dev_t(char *path, void **val, size_t *size)
440 {
441         static struct stat statb;
442
443         if (strcmp(path, "none") && strcmp(path, "off")) {
444                 int rc = stat (path, &statb);
445                 if (rc) {
446                         err(1, "cannot stat %s", path);
447                 }
448
449                 if (!S_ISCHR(statb.st_mode)) {
450                         errx(1, "must specify a device special file.");
451                 }
452         } else {
453                 statb.st_rdev = NODEV;
454         }
455         *val = (void *) &statb.st_rdev;
456         *size = sizeof(statb.st_rdev);
457 }
458
459 static int
460 set_IK(char *str, int *val)
461 {
462         float temp;
463         int len, kelv;
464         char *p, *endptr;
465
466         if ((len = strlen(str)) == 0)
467                 return (0);
468         p = &str[len - 1];
469         if (*p == 'C' || *p == 'F') {
470                 *p = '\0';
471                 temp = strtof(str, &endptr);
472                 if (endptr == str || *endptr != '\0')
473                         return (0);
474                 if (*p == 'F')
475                         temp = (temp - 32) * 5 / 9;
476                 kelv = temp * 10 + 2732;
477         } else {
478                 kelv = (int)strtol(str, &endptr, 10);
479                 if (endptr == str || *endptr != '\0')
480                         return (0);
481         }
482         *val = kelv;
483         return (1);
484 }
485
486 /*
487  * These functions uses a presently undocumented interface to the kernel
488  * to walk the tree and get the type so it can print the value.
489  * This interface is under work and consideration, and should probably
490  * be killed with a big axe by the first person who can find the time.
491  * (be aware though, that the proper interface isn't as obvious as it
492  * may seem, there are various conflicting requirements.
493  */
494
495 static int
496 name2oid(char *name, int *oidp)
497 {
498         int oid[2];
499         int i;
500         size_t j;
501
502         oid[0] = 0;
503         oid[1] = 3;
504
505         j = CTL_MAXNAME * sizeof(int);
506         i = sysctl(oid, 2, oidp, &j, name, strlen(name));
507         if (i < 0)
508                 return (i);
509         j /= sizeof(int);
510         return (j);
511 }
512
513 static int
514 oidfmt(int *oid, int len, char *fmt, u_int *kind)
515 {
516         int qoid[CTL_MAXNAME+2];
517         u_char buf[BUFSIZ];
518         int i;
519         size_t j;
520
521         qoid[0] = 0;
522         qoid[1] = 4;
523         memcpy(qoid + 2, oid, len * sizeof(int));
524
525         j = sizeof(buf);
526         i = sysctl(qoid, len + 2, buf, &j, 0, 0);
527         if (i)
528                 err(1, "sysctl fmt %d %d %d", i, j, errno);
529
530         if (kind)
531                 *kind = *(u_int *)buf;
532
533         if (fmt)
534                 strcpy(fmt, (char *)(buf + sizeof(u_int)));
535         return (0);
536 }
537
538 /*
539  * This formats and outputs the value of one variable
540  *
541  * Returns zero if anything was actually output.
542  * Returns one if didn't know what to do with this.
543  * Return minus one if we had errors.
544  */
545
546 static int
547 show_var(int *oid, int nlen)
548 {
549         u_char buf[BUFSIZ], *val, *oval, *p;
550         char name[BUFSIZ], *fmt;
551         const char *sep, *sep1;
552         int qoid[CTL_MAXNAME+2];
553         uintmax_t umv;
554         intmax_t mv;
555         int i, hexlen;
556         size_t intlen;
557         size_t j, len;
558         u_int kind;
559         int (*func)(int, void *);
560
561         bzero(buf, BUFSIZ);
562         bzero(name, BUFSIZ);
563         qoid[0] = 0;
564         memcpy(qoid + 2, oid, nlen * sizeof(int));
565
566         qoid[1] = 1;
567         j = sizeof(name);
568         i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
569         if (i || !j)
570                 err(1, "sysctl name %d %d %d", i, j, errno);
571
572         if (Nflag) {
573                 printf("%s", name);
574                 return (0);
575         }
576
577         if (eflag)
578                 sep = "=";
579         else
580                 sep = ": ";
581
582         if (dflag) {    /* just print description */
583                 qoid[1] = 5;
584                 j = sizeof(buf);
585                 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
586                 if (!nflag)
587                         printf("%s%s", name, sep);
588                 printf("%s", buf);
589                 return (0);
590         }
591         /* find an estimate of how much we need for this var */
592         j = 0;
593         i = sysctl(oid, nlen, 0, &j, 0, 0);
594         j += j; /* we want to be sure :-) */
595
596         val = oval = malloc(j + 1);
597         if (val == NULL) {
598                 warnx("malloc failed");
599                 return (1);
600         }
601         len = j;
602         i = sysctl(oid, nlen, val, &len, 0, 0);
603         if (i || !len) {
604                 free(oval);
605                 return (1);
606         }
607
608         if (bflag) {
609                 fwrite(val, 1, len, stdout);
610                 free(oval);
611                 return (0);
612         }
613         val[len] = '\0';
614         fmt = buf;
615         oidfmt(oid, nlen, fmt, &kind);
616         p = val;
617         switch (*fmt) {
618         case 'A':
619                 if (!nflag)
620                         printf("%s%s", name, sep);
621                 printf("%.*s", len, p);
622                 free(oval);
623                 return (0);
624
625         case 'I':
626         case 'L':
627         case 'Q':
628                 if (!nflag)
629                         printf("%s%s", name, sep);
630                 switch (*fmt) {
631                 case 'I': intlen = sizeof(int); break;
632                 case 'L': intlen = sizeof(long); break;
633                 case 'Q': intlen = sizeof(quad_t); break;
634                 }
635                 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
636                 sep1 = "";
637                 while (len >= intlen) {
638                         switch (*fmt) {
639                         case 'I':
640                                 umv = *(u_int *)p;
641                                 mv = *(int *)p;
642                                 break;
643                         case 'L':
644                                 umv = *(u_long *)p;
645                                 mv = *(long *)p;
646                                 break;
647                         case 'Q':
648                                 umv = *(u_quad_t *)p;
649                                 mv = *(quad_t *)p;
650                                 break;
651                         }
652                         fputs(sep1, stdout);
653                         if (fmt[1] == 'U')
654                                 printf(hflag ? "%'ju" : "%ju", umv);
655                         else if (fmt[1] == 'X')
656                                 printf("%#0*jx", hexlen, umv);
657                         else if (fmt[1] == 'K') {
658                                 if (mv < 0)
659                                         printf("%jd", mv);
660                                 else
661                                         printf("%.1fC", (mv - 2732.0) / 10);
662                         } else
663                                 printf(hflag ? "%'jd" : "%jd", mv);
664                         sep1 = " ";
665                         len -= intlen;
666                         p += intlen;
667                 }
668                 free(oval);
669                 return (0);
670
671         case 'P':
672                 if (!nflag)
673                         printf("%s%s", name, sep);
674                 printf("%p", *(void **)p);
675                 free(oval);
676                 return (0);
677
678         case 'T':
679         case 'S':
680                 i = 0;
681                 if (strcmp(fmt, "S,clockinfo") == 0)
682                         func = S_clockinfo;
683                 else if (strcmp(fmt, "S,timeval") == 0)
684                         func = S_timeval;
685                 else if (strcmp(fmt, "S,loadavg") == 0)
686                         func = S_loadavg;
687                 else if (strcmp(fmt, "S,vmtotal") == 0)
688                         func = S_vmtotal;
689                 else if (strcmp(fmt, "T,dev_t") == 0)
690                         func = T_dev_t;
691                 else
692                         func = NULL;
693                 if (func) {
694                         if (!nflag)
695                                 printf("%s%s", name, sep);
696                         i = (*func)(len, p);
697                         free(oval);
698                         return (i);
699                 }
700                 /* FALLTHROUGH */
701         default:
702                 if (!oflag && !xflag) {
703                         free(oval);
704                         return (1);
705                 }
706                 if (!nflag)
707                         printf("%s%s", name, sep);
708                 printf("Format:%s Length:%d Dump:0x", fmt, len);
709                 while (len-- && (xflag || p < val + 16))
710                         printf("%02x", *p++);
711                 if (!xflag && len > 16)
712                         printf("...");
713                 free(oval);
714                 return (0);
715         }
716         free(oval);
717         return (1);
718 }
719
720 static int
721 sysctl_all(int *oid, int len)
722 {
723         int name1[22], name2[22];
724         int i, j;
725         size_t l1, l2;
726
727         name1[0] = 0;
728         name1[1] = 2;
729         l1 = 2;
730         if (len) {
731                 memcpy(name1+2, oid, len * sizeof(int));
732                 l1 += len;
733         } else {
734                 name1[2] = 1;
735                 l1++;
736         }
737         for (;;) {
738                 l2 = sizeof(name2);
739                 j = sysctl(name1, l1, name2, &l2, 0, 0);
740                 if (j < 0) {
741                         if (errno == ENOENT)
742                                 return (0);
743                         else
744                                 err(1, "sysctl(getnext) %d %d", j, l2);
745                 }
746
747                 l2 /= sizeof(int);
748
749                 if (len < 0 || l2 < (unsigned int)len)
750                         return (0);
751
752                 for (i = 0; i < len; i++)
753                         if (name2[i] != oid[i])
754                                 return (0);
755
756                 i = show_var(name2, l2);
757                 if (!i && !bflag)
758                         putchar('\n');
759
760                 memcpy(name1+2, name2, l2 * sizeof(int));
761                 l1 = 2 + l2;
762         }
763 }