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.10 2005/01/18 03:13:52 cpressey 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>
49 #include <machine/inttypes.h>
59 static int aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag;
61 static int oidfmt(int *, size_t, char *, u_int *);
62 static void parse(const char *);
63 static int show_var(int *, size_t);
64 static int sysctl_all(int *, size_t);
65 static void set_T_dev_t(const char *, void **, int *);
71 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.
146 parse(const char *string)
152 unsigned int uintval;
154 unsigned long ulongval;
157 int mib[CTL_MAXNAME];
158 char *cp, fmt[BUFSIZ];
160 char *name_allocated = NULL;
163 if ((cp = strchr(string, '=')) != NULL) {
164 if ((name_allocated = malloc(cp - string + 1)) == NULL)
165 err(1, "malloc failed");
166 strlcpy(name_allocated, string, cp - string + 1);
167 name = name_allocated;
169 while (isspace(*++cp))
173 newsize = strlen(cp);
179 if (sysctlnametomib(name, mib, &len) < 0) {
180 if (errno == ENOENT) {
181 errx(1, "unknown oid '%s'", name);
183 err(1, "sysctlnametomib(\"%s\")", name);
187 if (oidfmt(mib, len, fmt, &kind))
188 err(1, "couldn't find format of oid '%s'", name);
190 if (newval == NULL) {
191 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
192 sysctl_all(mib, len);
194 i = show_var(mib, len);
199 if ((kind & CTLTYPE) == CTLTYPE_NODE)
200 errx(1, "oid '%s' isn't a leaf node", name);
202 if (!(kind&CTLFLAG_WR))
203 errx(1, "oid '%s' is read only", name);
205 switch (kind & CTLTYPE) {
207 intval = (int) strtol(newval, NULL, 0);
209 newsize = sizeof(intval);
212 uintval = (int) strtoul(newval, NULL, 0);
214 newsize = sizeof uintval;
217 longval = strtol(newval, NULL, 0);
219 newsize = sizeof longval;
222 ulongval = strtoul(newval, NULL, 0);
224 newsize = sizeof ulongval;
229 sscanf(newval, "%qd", &quadval);
231 newsize = sizeof(quadval);
234 if (strcmp(fmt, "T,dev_t") == 0) {
235 set_T_dev_t ((char*)newval, &newval, &newsize);
240 errx(1, "oid '%s' is type %d,"
241 " cannot set that", name,
245 i = show_var(mib, len);
246 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
251 errx(1, "%s: value is not available",
254 errx(1, "%s: specification is incomplete",
257 errx(1, "%s: type is unknown to this program",
268 j = show_var(mib, len);
274 if (name_allocated != NULL)
275 free(name_allocated);
278 /* These functions will dump out various interesting structures. */
281 S_clockinfo(int l2, void *p)
283 struct clockinfo *ci = (struct clockinfo*)p;
284 if (l2 != sizeof(*ci))
285 err(1, "S_clockinfo %d != %d", l2, sizeof(*ci));
286 printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
287 ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz);
292 S_loadavg(int l2, void *p)
294 struct loadavg *tv = (struct loadavg*)p;
296 if (l2 != sizeof(*tv))
297 err(1, "S_loadavg %d != %d", l2, sizeof(*tv));
299 printf("{ %.2f %.2f %.2f }",
300 (double)tv->ldavg[0]/(double)tv->fscale,
301 (double)tv->ldavg[1]/(double)tv->fscale,
302 (double)tv->ldavg[2]/(double)tv->fscale);
307 S_timeval(int l2, void *p)
309 struct timeval *tv = (struct timeval*)p;
313 if (l2 != sizeof(*tv))
314 err(1, "S_timeval %d != %d", l2, sizeof(*tv));
315 printf("{ sec = %ld, usec = %ld } ",
316 tv->tv_sec, tv->tv_usec);
318 p1 = strdup(ctime(&tv_sec));
319 for (p2=p1; *p2 ; p2++)
327 T_dev_t(int l2, void *p)
329 dev_t *d = (dev_t *)p;
330 if (l2 != sizeof(*d))
331 err(1, "T_dev_T %d != %d", l2, sizeof(*d));
332 if ((int)(*d) != -1) {
333 if (minor(*d) > 255 || minor(*d) < 0)
334 printf("{ major = %d, minor = 0x%x }",
335 major(*d), minor(*d));
337 printf("{ major = %d, minor = %d }",
338 major(*d), minor(*d));
344 set_T_dev_t(const char *path, void **val, int *size)
346 static struct stat statb;
348 if (strcmp(path, "none") && strcmp(path, "off")) {
349 int rc = stat (path, &statb);
351 err(1, "cannot stat %s", path);
354 if (!S_ISCHR(statb.st_mode)) {
355 errx(1, "must specify a device special file.");
358 statb.st_rdev = NODEV;
360 *val = (char*) &statb.st_rdev;
361 *size = sizeof statb.st_rdev;
365 * These functions uses a presently undocumented interface to the kernel
366 * to walk the tree and get the type so it can print the value.
367 * This interface is under work and consideration, and should probably
368 * be killed with a big axe by the first person who can find the time.
369 * (be aware though, that the proper interface isn't as obvious as it
370 * may seem, there are various conflicting requirements.
374 oidfmt(int *oid, size_t len, char *fmt, u_int *kind)
376 int qoid[CTL_MAXNAME+2];
383 memcpy(qoid + 2, oid, len * sizeof(int));
386 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
388 err(1, "sysctl fmt %d %d %d", i, j, errno);
391 *kind = *(u_int *)buf;
394 strcpy(fmt, (char *)(buf + sizeof(u_int)));
400 * Code to map a bootdev major number into a suitable device name.
401 * Major numbers are mapped into names as in boot2.c
412 { -1, NULL } /* terminator */
416 machdep_bootdev(u_long value)
418 int majdev, unit, slice, part;
421 if ((value & B_MAGICMASK) != B_DEVMAGIC) {
422 printf("invalid (0x%08lx)", value);
425 majdev = B_TYPE(value);
426 unit = B_UNIT(value);
427 slice = B_SLICE(value);
428 part = B_PARTITION(value);
429 if (majdev == 2) { /* floppy, as known to the boot block... */
430 printf("/dev/fd%d", unit);
433 for (p = maj2name; p->name != NULL && p->majdev != majdev ; p++) ;
434 if (p->name != NULL) { /* found */
435 if (slice == WHOLE_DISK_SLICE)
436 printf("/dev/%s%d%c", p->name, unit, part);
438 printf("/dev/%s%ds%d%c",
439 p->name, unit, slice - BASE_SLICE + 1, part + 'a');
441 printf("unknown (major %d unit %d slice %d part %d)",
442 majdev, unit, slice, part);
448 * This formats and outputs the value of one variable
450 * Returns zero if anything was actually output.
451 * Returns one if didn't know what to do with this.
452 * Return minus one if we had errors.
456 show_var(int *oid, size_t nlen)
458 u_char buf[BUFSIZ], *val, *p;
459 char name[BUFSIZ], *fmt;
460 const char *sep, *spacer;
461 int qoid[CTL_MAXNAME+2];
465 int (*func)(int, void *);
468 memcpy(qoid + 2, oid, nlen * sizeof(int));
472 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
474 err(1, "sysctl name %d %d %d", i, j, errno);
486 if (dflag) { /* just print description */
489 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
491 printf("%s%s", name, sep);
495 /* find an estimate of how much we need for this var */
497 i = sysctl(oid, nlen, 0, &j, 0, 0);
498 j += j; /* we want to be sure :-) */
502 i = sysctl(oid, nlen, val, &len, 0, 0);
507 fwrite(val, 1, len, stdout);
513 oidfmt(oid, nlen, fmt, &kind);
518 printf("%s%s", name, sep);
519 for (j = 0; j < len; j++) {
523 fwrite(p, j, 1, stdout);
528 printf("%s%s", name, sep);
531 while (len >= sizeof(int)) {
533 printf("%s%u", spacer, *(unsigned int *)p);
535 printf("%s%d", spacer, *(int *)p);
544 printf("%s%s", name, sep);
547 if (!strcmp(name, "machdep.guessed_bootdev"))
548 return machdep_bootdev(*(unsigned long *)p);
551 while (len >= sizeof(long)) {
553 printf("%s%lu", spacer, *(unsigned long *)p);
555 printf("%s%ld", spacer, *(long *)p);
564 printf("%s%s", name, sep);
565 printf("%p", *(void **)p);
570 printf("%s%s", name, sep);
573 while (len >= sizeof(int64_t)) {
575 printf("%s%"PRIu64, spacer, *(uint64_t *)p);
577 printf("%s%"PRId64, spacer, *(int64_t *)p);
579 len -= sizeof(int64_t);
580 p += sizeof(int64_t);
586 if (!oflag && !xflag) {
588 if (strcmp(fmt, "S,clockinfo") == 0)
590 else if (strcmp(fmt, "S,timeval") == 0)
592 else if (strcmp(fmt, "S,loadavg") == 0)
594 else if (strcmp(fmt, "T,dev_t") == 0)
600 printf("%s%s", name, sep);
601 return ((*func)(len, p));
606 if (!oflag && !xflag)
609 printf("%s%s", name, sep);
610 printf("Format:%s Length:%d Dump:0x", fmt, len);
611 while (len-- && (xflag || p < val + 16))
612 printf("%02x", *p++);
613 if (!xflag && len > 16)
621 sysctl_all(int *oid, size_t len)
623 int name1[22], name2[22];
631 memcpy(name1+2, oid, len * sizeof(int));
639 retval = sysctl(name1, l1, name2, &l2, 0, 0);
644 err(1, "sysctl(getnext) %d %d", retval, l2);
652 for (i = 0; i < len; i++)
653 if (name2[i] != oid[i])
656 retval = show_var(name2, l2);
657 if (retval == 0 && !bflag)
660 memcpy(name1+2, name2, l2 * sizeof(int));