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