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