3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.3 2003/06/29 06:48:32 dillon Exp $
40 #include <sys/diskslice.h> /* used for bootdev parsing */
41 #include <sys/reboot.h> /* used for bootdev parsing */
43 #include <sys/types.h>
45 #include <sys/sysctl.h>
46 #include <sys/resource.h>
47 #include <sys/param.h>
57 static int aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag;
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 *);
65 static void set_T_dev_t (char *, void **, int *);
71 (void)fprintf(stderr, "%s\n%s\n",
72 "usage: sysctl [-bdeNnox] variable[=value] ...",
73 " sysctl [-bdeNnox] -a");
78 main(int argc, char **argv)
84 while ((ch = getopt(argc, argv, "AabdeNnowxX")) != -1) {
131 if (aflag && argc == 0)
132 exit(sysctl_all(0, 0));
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.
151 unsigned int uintval;
153 unsigned long ulongval;
156 int mib[CTL_MAXNAME];
157 char *cp, *bufp, buf[BUFSIZ], fmt[BUFSIZ];
161 snprintf(buf, BUFSIZ, "%s", string);
162 if ((cp = strchr(string, '=')) != NULL) {
163 *strchr(buf, '=') = '\0';
168 newsize = strlen(cp);
170 len = name2oid(bufp, mib);
173 errx(1, "unknown oid '%s'", bufp);
175 if (oidfmt(mib, len, fmt, &kind))
176 err(1, "couldn't find format of oid '%s'", bufp);
178 if (newval == NULL) {
179 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
180 sysctl_all(mib, len);
182 i = show_var(mib, len);
187 if ((kind & CTLTYPE) == CTLTYPE_NODE)
188 errx(1, "oid '%s' isn't a leaf node", bufp);
190 if (!(kind&CTLFLAG_WR))
191 errx(1, "oid '%s' is read only", bufp);
193 switch (kind & CTLTYPE) {
195 intval = (int) strtol(newval, NULL, 0);
197 newsize = sizeof(intval);
200 uintval = (int) strtoul(newval, NULL, 0);
202 newsize = sizeof uintval;
205 longval = strtol(newval, NULL, 0);
207 newsize = sizeof longval;
210 ulongval = strtoul(newval, NULL, 0);
212 newsize = sizeof ulongval;
217 sscanf(newval, "%qd", &quadval);
219 newsize = sizeof(quadval);
222 if (strcmp(fmt, "T,dev_t") == 0) {
223 set_T_dev_t ((char*)newval, &newval, &newsize);
228 errx(1, "oid '%s' is type %d,"
229 " cannot set that", bufp,
233 i = show_var(mib, len);
234 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
239 errx(1, "%s: value is not available",
242 errx(1, "%s: specification is incomplete",
245 errx(1, "%s: type is unknown to this program",
256 j = show_var(mib, len);
263 /* These functions will dump out various interesting structures. */
266 S_clockinfo(int l2, void *p)
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);
277 S_loadavg(int l2, void *p)
279 struct loadavg *tv = (struct loadavg*)p;
281 if (l2 != sizeof(*tv))
282 err(1, "S_loadavg %d != %d", l2, sizeof(*tv));
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);
292 S_timeval(int l2, void *p)
294 struct timeval *tv = (struct timeval*)p;
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);
303 p1 = strdup(ctime(&tv_sec));
304 for (p2=p1; *p2 ; p2++)
312 T_dev_t(int l2, void *p)
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));
322 printf("{ major = %d, minor = %d }",
323 major(*d), minor(*d));
329 set_T_dev_t (char *path, void **val, int *size)
331 static struct stat statb;
333 if (strcmp(path, "none") && strcmp(path, "off")) {
334 int rc = stat (path, &statb);
336 err(1, "cannot stat %s", path);
339 if (!S_ISCHR(statb.st_mode)) {
340 errx(1, "must specify a device special file.");
343 statb.st_rdev = NODEV;
345 *val = (char*) &statb.st_rdev;
346 *size = sizeof statb.st_rdev;
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.
359 name2oid(char *name, int *oidp)
368 j = CTL_MAXNAME * sizeof(int);
369 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
377 oidfmt(int *oid, int len, char *fmt, u_int *kind)
379 int qoid[CTL_MAXNAME+2];
386 memcpy(qoid + 2, oid, len * sizeof(int));
389 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
391 err(1, "sysctl fmt %d %d %d", i, j, errno);
394 *kind = *(u_int *)buf;
397 strcpy(fmt, (char *)(buf + sizeof(u_int)));
403 * Code to map a bootdev major number into a suitable device name.
404 * Major numbers are mapped into names as in boot2.c
415 -1, NULL /* terminator */
419 machdep_bootdev(u_long value)
421 int majdev, unit, slice, part;
424 if (value & B_MAGICMASK != B_DEVMAGIC) {
425 printf("invalid (0x%08x)", value);
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);
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);
441 printf("/dev/%s%ds%d%c",
442 p->name, unit, slice - BASE_SLICE + 1, part + 'a');
444 printf("unknown (major %d unit %d slice %d part %d)",
445 majdev, unit, slice, part);
451 * This formats and outputs the value of one variable
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.
459 show_var(int *oid, int nlen)
461 u_char buf[BUFSIZ], *val, *p;
462 char name[BUFSIZ], *fmt, *sep;
463 int qoid[CTL_MAXNAME+2];
467 int (*func)(int, void *);
470 memcpy(qoid + 2, oid, nlen * sizeof(int));
474 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
476 err(1, "sysctl name %d %d %d", i, j, errno);
488 if (dflag) { /* just print description */
491 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
493 printf("%s%s", name, sep);
497 /* find an estimate of how much we need for this var */
499 i = sysctl(oid, nlen, 0, &j, 0, 0);
500 j += j; /* we want to be sure :-) */
504 i = sysctl(oid, nlen, val, &len, 0, 0);
509 fwrite(val, 1, len, stdout);
515 oidfmt(oid, nlen, fmt, &kind);
520 printf("%s%s", name, sep);
521 printf("%.*s", len, p);
526 printf("%s%s", name, sep);
529 while (len >= sizeof(int)) {
531 printf("%s%u", val, *(unsigned int *)p);
533 printf("%s%d", val, *(int *)p);
542 printf("%s%s", name, sep);
545 if (!strcmp(name, "machdep.guessed_bootdev"))
546 return machdep_bootdev(*(unsigned long *)p);
549 while (len >= sizeof(long)) {
551 printf("%s%lu", val, *(unsigned long *)p);
553 printf("%s%ld", val, *(long *)p);
562 printf("%s%s", name, sep);
563 printf("%p", *(void **)p);
568 printf("%s%s", name, sep);
571 while (len >= sizeof(int64_t)) {
573 printf("%s%llu", val, (long long)*(u_int64_t *)p);
575 printf("%s%lld", val, (long long)*(int64_t *)p);
577 len -= sizeof(int64_t);
578 p += sizeof(int64_t);
585 if (strcmp(fmt, "S,clockinfo") == 0)
587 else if (strcmp(fmt, "S,timeval") == 0)
589 else if (strcmp(fmt, "S,loadavg") == 0)
591 else if (strcmp(fmt, "T,dev_t") == 0)
597 printf("%s%s", name, sep);
598 return ((*func)(len, p));
602 if (!oflag && !xflag)
605 printf("%s%s", name, sep);
606 printf("Format:%s Length:%d Dump:0x", fmt, len);
607 while (len-- && (xflag || p < val + 16))
608 printf("%02x", *p++);
609 if (!xflag && len > 16)
617 sysctl_all (int *oid, int len)
619 int name1[22], name2[22];
627 memcpy(name1+2, oid, len * sizeof(int));
635 j = sysctl(name1, l1, name2, &l2, 0, 0);
640 err(1, "sysctl(getnext) %d %d", j, l2);
648 for (i = 0; i < len; i++)
649 if (name2[i] != oid[i])
652 i = show_var(name2, l2);
656 memcpy(name1+2, name2, l2 * sizeof(int));