Merge from vendor branch CVS:
[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.5 2004/05/31 14:59:34 hmp 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/param.h>
48
49 #include <ctype.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 static int      aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag;
58
59 static int      oidfmt(int *, int, char *, u_int *);
60 static void     parse(char *);
61 static int      show_var(int *, int);
62 static int      sysctl_all (int *oid, int len);
63 static int      name2oid(char *, int *);
64
65 static void     set_T_dev_t (char *, void **, int *);
66
67 static void
68 usage(void)
69 {
70
71         (void)fprintf(stderr, "%s\n%s\n",
72             "usage: sysctl [-bdeNnox] variable[=value] ...",
73             "       sysctl [-bdeNnox] -a");
74         exit(1);
75 }
76
77 int
78 main(int argc, char **argv)
79 {
80         int ch;
81         setbuf(stdout,0);
82         setbuf(stderr,0);
83
84         while ((ch = getopt(argc, argv, "AabdeNnowxX")) != -1) {
85                 switch (ch) {
86                 case 'A':
87                         /* compatibility */
88                         aflag = oflag = 1;
89                         break;
90                 case 'a':
91                         aflag = 1;
92                         break;
93                 case 'b':
94                         bflag = 1;
95                         break;
96                 case 'd':
97                         dflag = 1;
98                         break;
99                 case 'e':
100                         eflag = 1;
101                         break;
102                 case 'N':
103                         Nflag = 1;
104                         break;
105                 case 'n':
106                         nflag = 1;
107                         break;
108                 case 'o':
109                         oflag = 1;
110                         break;
111                 case 'w':
112                         /* compatibility */
113                         /* ignored */
114                         break;
115                 case 'X':
116                         /* compatibility */
117                         aflag = xflag = 1;
118                         break;
119                 case 'x':
120                         xflag = 1;
121                         break;
122                 default:
123                         usage();
124                 }
125         }
126         argc -= optind;
127         argv += optind;
128
129         if (Nflag && nflag)
130                 usage();
131         if (aflag && argc == 0)
132                 exit(sysctl_all(0, 0));
133         if (argc == 0)
134                 usage();
135         while (argc-- > 0)
136                 parse(*argv++);
137         exit(0);
138 }
139
140 /*
141  * Parse a name into a MIB entry.
142  * Lookup and print out the MIB entry if it exists.
143  * Set a new value if requested.
144  */
145 static void
146 parse(char *string)
147 {
148         int len, i, j;
149         void *newval = 0;
150         int intval;
151         unsigned int uintval;
152         long longval;
153         unsigned long ulongval;
154         size_t newsize = 0;
155         quad_t quadval;
156         int mib[CTL_MAXNAME];
157         char *cp, *bufp, buf[BUFSIZ], fmt[BUFSIZ];
158         u_int kind;
159
160         bufp = buf;
161         snprintf(buf, BUFSIZ, "%s", string);
162         if ((cp = strchr(string, '=')) != NULL) {
163                 *strchr(buf, '=') = '\0';
164                 *cp++ = '\0';
165                 while (isspace(*cp))
166                         cp++;
167                 newval = cp;
168                 newsize = strlen(cp);
169         }
170         len = name2oid(bufp, mib);
171
172         if (len < 0) 
173                 errx(1, "unknown oid '%s'", bufp);
174
175         if (oidfmt(mib, len, fmt, &kind))
176                 err(1, "couldn't find format of oid '%s'", bufp);
177
178         if (newval == NULL) {
179                 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
180                         sysctl_all(mib, len);
181                 } else {
182                         i = show_var(mib, len);
183                         if (!i && !bflag)
184                                 putchar('\n');
185                 }
186         } else {
187                 if ((kind & CTLTYPE) == CTLTYPE_NODE)
188                         errx(1, "oid '%s' isn't a leaf node", bufp);
189
190                 if (!(kind&CTLFLAG_WR))
191                         errx(1, "oid '%s' is read only", bufp);
192         
193                 switch (kind & CTLTYPE) {
194                         case CTLTYPE_INT:
195                                 intval = (int) strtol(newval, NULL, 0);
196                                 newval = &intval;
197                                 newsize = sizeof(intval);
198                                 break;
199                         case CTLTYPE_UINT:
200                                 uintval = (int) strtoul(newval, NULL, 0);
201                                 newval = &uintval;
202                                 newsize = sizeof uintval;
203                                 break;
204                         case CTLTYPE_LONG:
205                                 longval = strtol(newval, NULL, 0);
206                                 newval = &longval;
207                                 newsize = sizeof longval;
208                                 break;
209                         case CTLTYPE_ULONG:
210                                 ulongval = strtoul(newval, NULL, 0);
211                                 newval = &ulongval;
212                                 newsize = sizeof ulongval;
213                                 break;
214                         case CTLTYPE_STRING:
215                                 break;
216                         case CTLTYPE_QUAD:
217                                 sscanf(newval, "%qd", &quadval);
218                                 newval = &quadval;
219                                 newsize = sizeof(quadval);
220                                 break;
221                         case CTLTYPE_OPAQUE:
222                                 if (strcmp(fmt, "T,dev_t") == 0) {
223                                         set_T_dev_t ((char*)newval, &newval, &newsize);
224                                         break;
225                                 }
226                                 /* FALLTHROUGH */
227                         default:
228                                 errx(1, "oid '%s' is type %d,"
229                                         " cannot set that", bufp,
230                                         kind & CTLTYPE);
231                 }
232
233                 i = show_var(mib, len);
234                 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
235                         if (!i && !bflag)
236                                 putchar('\n');
237                         switch (errno) {
238                         case EOPNOTSUPP:
239                                 errx(1, "%s: value is not available", 
240                                         string);
241                         case ENOTDIR:
242                                 errx(1, "%s: specification is incomplete", 
243                                         string);
244                         case ENOMEM:
245                                 errx(1, "%s: type is unknown to this program", 
246                                         string);
247                         default:
248                                 warn("%s", string);
249                                 return;
250                         }
251                 }
252                 if (!bflag)
253                         printf(" -> ");
254                 i = nflag;
255                 nflag = 1;
256                 j = show_var(mib, len);
257                 if (!j && !bflag)
258                         putchar('\n');
259                 nflag = i;
260         }
261 }
262
263 /* These functions will dump out various interesting structures. */
264
265 static int
266 S_clockinfo(int l2, void *p)
267 {
268         struct clockinfo *ci = (struct clockinfo*)p;
269         if (l2 != sizeof(*ci))
270                 err(1, "S_clockinfo %d != %d", l2, sizeof(*ci));
271         printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
272                 ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz);
273         return (0);
274 }
275
276 static int
277 S_loadavg(int l2, void *p)
278 {
279         struct loadavg *tv = (struct loadavg*)p;
280
281         if (l2 != sizeof(*tv))
282                 err(1, "S_loadavg %d != %d", l2, sizeof(*tv));
283
284         printf("{ %.2f %.2f %.2f }",
285                 (double)tv->ldavg[0]/(double)tv->fscale,
286                 (double)tv->ldavg[1]/(double)tv->fscale,
287                 (double)tv->ldavg[2]/(double)tv->fscale);
288         return (0);
289 }
290
291 static int
292 S_timeval(int l2, void *p)
293 {
294         struct timeval *tv = (struct timeval*)p;
295         time_t tv_sec;
296         char *p1, *p2;
297
298         if (l2 != sizeof(*tv))
299                 err(1, "S_timeval %d != %d", l2, sizeof(*tv));
300         printf("{ sec = %ld, usec = %ld } ",
301                 tv->tv_sec, tv->tv_usec);
302         tv_sec = tv->tv_sec;
303         p1 = strdup(ctime(&tv_sec));
304         for (p2=p1; *p2 ; p2++)
305                 if (*p2 == '\n')
306                         *p2 = '\0';
307         fputs(p1, stdout);
308         return (0);
309 }
310
311 static int
312 T_dev_t(int l2, void *p)
313 {
314         dev_t *d = (dev_t *)p;
315         if (l2 != sizeof(*d))
316                 err(1, "T_dev_T %d != %d", l2, sizeof(*d));
317         if ((int)(*d) != -1) {
318                 if (minor(*d) > 255 || minor(*d) < 0)
319                         printf("{ major = %d, minor = 0x%x }",
320                                 major(*d), minor(*d));
321                 else
322                         printf("{ major = %d, minor = %d }",
323                                 major(*d), minor(*d));
324         }
325         return (0);
326 }
327
328 static void
329 set_T_dev_t (char *path, void **val, int *size)
330 {
331         static struct stat statb;
332
333         if (strcmp(path, "none") && strcmp(path, "off")) {
334                 int rc = stat (path, &statb);
335                 if (rc) {
336                         err(1, "cannot stat %s", path);
337                 }
338
339                 if (!S_ISCHR(statb.st_mode)) {
340                         errx(1, "must specify a device special file.");
341                 }
342         } else {
343                 statb.st_rdev = NODEV;
344         }
345         *val = (char*) &statb.st_rdev;
346         *size = sizeof statb.st_rdev;
347 }
348
349 /*
350  * These functions uses a presently undocumented interface to the kernel
351  * to walk the tree and get the type so it can print the value.
352  * This interface is under work and consideration, and should probably
353  * be killed with a big axe by the first person who can find the time.
354  * (be aware though, that the proper interface isn't as obvious as it
355  * may seem, there are various conflicting requirements.
356  */
357
358 static int
359 name2oid(char *name, int *oidp)
360 {
361         int oid[2];
362         int i;
363         size_t j;
364
365         oid[0] = 0;
366         oid[1] = 3;
367
368         j = CTL_MAXNAME * sizeof(int);
369         i = sysctl(oid, 2, oidp, &j, name, strlen(name));
370         if (i < 0) 
371                 return i;
372         j /= sizeof(int);
373         return (j);
374 }
375
376 static int
377 oidfmt(int *oid, int len, char *fmt, u_int *kind)
378 {
379         int qoid[CTL_MAXNAME+2];
380         u_char buf[BUFSIZ];
381         int i;
382         size_t j;
383
384         qoid[0] = 0;
385         qoid[1] = 4;
386         memcpy(qoid + 2, oid, len * sizeof(int));
387
388         j = sizeof(buf);
389         i = sysctl(qoid, len + 2, buf, &j, 0, 0);
390         if (i)
391                 err(1, "sysctl fmt %d %d %d", i, j, errno);
392
393         if (kind)
394                 *kind = *(u_int *)buf;
395
396         if (fmt)
397                 strcpy(fmt, (char *)(buf + sizeof(u_int)));
398         return 0;
399 }
400
401 #ifdef __i386__
402 /*
403  * Code to map a bootdev major number into a suitable device name.
404  * Major numbers are mapped into names as in boot2.c
405  */
406 struct _foo {
407         int majdev;
408         char *name;
409 } maj2name[] = {
410         { 30,   "ad" },
411         { 0,    "wd" },
412         { 1,    "wfd" },
413         { 2,    "fd" },
414         { 4,    "da" },
415         { -1,   NULL }  /* terminator */
416 };
417
418 static int
419 machdep_bootdev(u_long value)
420 {
421         int majdev, unit, slice, part;
422         struct _foo *p;
423
424         if ((value & B_MAGICMASK) != B_DEVMAGIC) {
425                 printf("invalid (0x%08lx)", value);
426                 return 0;
427         }
428         majdev = B_TYPE(value);
429         unit = B_UNIT(value);
430         slice = B_SLICE(value);
431         part = B_PARTITION(value);
432         if (majdev == 2) {      /* floppy, as known to the boot block... */
433                 printf("/dev/fd%d", unit);
434                 return 0;
435         }
436         for (p = maj2name; p->name != NULL && p->majdev != majdev ; p++) ;
437         if (p->name != NULL) {  /* found */
438                 if (slice == WHOLE_DISK_SLICE)
439                         printf("/dev/%s%d%c", p->name, unit, part);
440                 else
441                         printf("/dev/%s%ds%d%c",
442                             p->name, unit, slice - BASE_SLICE + 1, part + 'a');
443         } else
444                 printf("unknown (major %d unit %d slice %d part %d)",
445                         majdev, unit, slice, part);
446         return 0;
447 }
448 #endif
449
450 /*
451  * This formats and outputs the value of one variable
452  *
453  * Returns zero if anything was actually output.
454  * Returns one if didn't know what to do with this.
455  * Return minus one if we had errors.
456  */
457
458 static int
459 show_var(int *oid, int nlen)
460 {
461         u_char buf[BUFSIZ], *val, *p;
462         char name[BUFSIZ], *fmt, *sep;
463         int qoid[CTL_MAXNAME+2];
464         int i;
465         size_t j, len;
466         u_int kind;
467         int (*func)(int, void *);
468
469         qoid[0] = 0;
470         memcpy(qoid + 2, oid, nlen * sizeof(int));
471
472         qoid[1] = 1;
473         j = sizeof(name);
474         i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
475         if (i || !j)
476                 err(1, "sysctl name %d %d %d", i, j, errno);
477
478         if (Nflag) {
479                 printf("%s", name);
480                 return (0);
481         }
482
483         if (eflag)
484                 sep = "=";
485         else
486                 sep = ": ";
487
488         if (dflag) {    /* just print description */
489                 qoid[1] = 5;
490                 j = sizeof(buf);
491                 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
492                 if (!nflag)
493                         printf("%s%s", name, sep);
494                 printf("%s", buf);
495                 return(0);
496         }
497         /* find an estimate of how much we need for this var */
498         j = 0;
499         i = sysctl(oid, nlen, 0, &j, 0, 0);
500         j += j; /* we want to be sure :-) */
501
502         val = alloca(j + 1);
503         len = j;
504         i = sysctl(oid, nlen, val, &len, 0, 0);
505         if (i || !len)
506                 return (1);
507
508         if (bflag) {
509                 fwrite(val, 1, len, stdout);
510                 return (0);
511         }
512
513         val[len] = '\0';
514         fmt = buf;
515         oidfmt(oid, nlen, fmt, &kind);
516         p = val;
517         switch (*fmt) {
518         case 'A':
519                 if (!nflag)
520                         printf("%s%s", name, sep);
521                 printf("%.*s", len, p);
522                 return (0);
523                 
524         case 'I':
525                 if (!nflag)
526                         printf("%s%s", name, sep);
527                 fmt++;
528                 val = "";
529                 while (len >= sizeof(int)) {
530                         if(*fmt == 'U')
531                                 printf("%s%u", val, *(unsigned int *)p);
532                         else
533                                 printf("%s%d", val, *(int *)p);
534                         val = " ";
535                         len -= sizeof(int);
536                         p += sizeof(int);
537                 }
538                 return (0);
539
540         case 'L':
541                 if (!nflag)
542                         printf("%s%s", name, sep);
543                 fmt++;
544 #ifdef __i386__
545                 if (!strcmp(name, "machdep.guessed_bootdev"))
546                         return machdep_bootdev(*(unsigned long *)p);
547 #endif
548                 val = "";
549                 while (len >= sizeof(long)) {
550                         if(*fmt == 'U')
551                                 printf("%s%lu", val, *(unsigned long *)p);
552                         else
553                                 printf("%s%ld", val, *(long *)p);
554                         val = " ";
555                         len -= sizeof(long);
556                         p += sizeof(long);
557                 }
558                 return (0);
559
560         case 'P':
561                 if (!nflag)
562                         printf("%s%s", name, sep);
563                 printf("%p", *(void **)p);
564                 return (0);
565
566         case 'Q':
567                 if (!nflag)
568                         printf("%s%s", name, sep);
569                 fmt++;
570                 val = "";
571                 while (len >= sizeof(int64_t)) {
572                         if(*fmt == 'U')
573                                 printf("%s%llu", val, (long long)*(u_int64_t *)p);
574                         else
575                                 printf("%s%lld", val, (long long)*(int64_t *)p);
576                         val = " ";
577                         len -= sizeof(int64_t);
578                         p += sizeof(int64_t);
579                 }
580                 return (0);
581
582         case 'T':
583         case 'S':
584                 if (!oflag && !xflag) {
585                         i = 0;
586                         if (strcmp(fmt, "S,clockinfo") == 0)
587                                 func = S_clockinfo;
588                         else if (strcmp(fmt, "S,timeval") == 0)
589                                 func = S_timeval;
590                         else if (strcmp(fmt, "S,loadavg") == 0)
591                                 func = S_loadavg;
592                         else if (strcmp(fmt, "T,dev_t") == 0)
593                                 func = T_dev_t;
594                         else
595                                 func = NULL;
596                         if (func) {
597                                 if (!nflag)
598                                         printf("%s%s", name, sep);
599                                 return ((*func)(len, p));
600                         }
601                 }
602                 /* FALL THROUGH */
603         default:
604                 if (!oflag && !xflag)
605                         return (1);
606                 if (!nflag)
607                         printf("%s%s", name, sep);
608                 printf("Format:%s Length:%d Dump:0x", fmt, len);
609                 while (len-- && (xflag || p < val + 16))
610                         printf("%02x", *p++);
611                 if (!xflag && len > 16)
612                         printf("...");
613                 return (0);
614         }
615         return (1);
616 }
617
618 static int
619 sysctl_all (int *oid, int len)
620 {
621         int name1[22], name2[22];
622         int i, j;
623         size_t l1, l2;
624
625         name1[0] = 0;
626         name1[1] = 2;
627         l1 = 2;
628         if (len) {
629                 memcpy(name1+2, oid, len * sizeof(int));
630                 l1 += len;
631         } else {
632                 name1[2] = 1;
633                 l1++;
634         }
635         for (;;) {
636                 l2 = sizeof(name2);
637                 j = sysctl(name1, l1, name2, &l2, 0, 0);
638                 if (j < 0) {
639                         if (errno == ENOENT)
640                                 return 0;
641                         else
642                                 err(1, "sysctl(getnext) %d %d", j, l2);
643                 }
644
645                 l2 /= sizeof(int);
646
647                 if (l2 < len)
648                         return 0;
649
650                 for (i = 0; i < len; i++)
651                         if (name2[i] != oid[i])
652                                 return 0;
653
654                 i = show_var(name2, l2);
655                 if (!i && !bflag)
656                         putchar('\n');
657
658                 memcpy(name1+2, name2, l2 * sizeof(int));
659                 l1 = 2 + l2;
660         }
661 }