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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
44 static const char rcsid[] =
45 "$FreeBSD: src/sbin/sysctl/sysctl.c,v 1.25.2.11 2003/05/01 22:48:08 trhodes Exp $";
49 #include <sys/diskslice.h> /* used for bootdev parsing */
50 #include <sys/reboot.h> /* used for bootdev parsing */
52 #include <sys/types.h>
54 #include <sys/sysctl.h>
55 #include <sys/resource.h>
56 #include <sys/param.h>
66 static int aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag;
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 *);
74 static void set_T_dev_t (char *, void **, int *);
80 (void)fprintf(stderr, "%s\n%s\n",
81 "usage: sysctl [-bdeNnox] variable[=value] ...",
82 " sysctl [-bdeNnox] -a");
87 main(int argc, char **argv)
93 while ((ch = getopt(argc, argv, "AabdeNnowxX")) != -1) {
140 if (aflag && argc == 0)
141 exit(sysctl_all(0, 0));
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.
160 unsigned int uintval;
162 unsigned long ulongval;
165 int mib[CTL_MAXNAME];
166 char *cp, *bufp, buf[BUFSIZ], fmt[BUFSIZ];
170 snprintf(buf, BUFSIZ, "%s", string);
171 if ((cp = strchr(string, '=')) != NULL) {
172 *strchr(buf, '=') = '\0';
177 newsize = strlen(cp);
179 len = name2oid(bufp, mib);
182 errx(1, "unknown oid '%s'", bufp);
184 if (oidfmt(mib, len, fmt, &kind))
185 err(1, "couldn't find format of oid '%s'", bufp);
187 if (newval == NULL) {
188 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
189 sysctl_all(mib, len);
191 i = show_var(mib, len);
196 if ((kind & CTLTYPE) == CTLTYPE_NODE)
197 errx(1, "oid '%s' isn't a leaf node", bufp);
199 if (!(kind&CTLFLAG_WR))
200 errx(1, "oid '%s' is read only", bufp);
202 switch (kind & CTLTYPE) {
204 intval = (int) strtol(newval, NULL, 0);
206 newsize = sizeof(intval);
209 uintval = (int) strtoul(newval, NULL, 0);
211 newsize = sizeof uintval;
214 longval = strtol(newval, NULL, 0);
216 newsize = sizeof longval;
219 ulongval = strtoul(newval, NULL, 0);
221 newsize = sizeof ulongval;
226 sscanf(newval, "%qd", &quadval);
228 newsize = sizeof(quadval);
231 if (strcmp(fmt, "T,dev_t") == 0) {
232 set_T_dev_t ((char*)newval, &newval, &newsize);
237 errx(1, "oid '%s' is type %d,"
238 " cannot set that", bufp,
242 i = show_var(mib, len);
243 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
248 errx(1, "%s: value is not available",
251 errx(1, "%s: specification is incomplete",
254 errx(1, "%s: type is unknown to this program",
265 j = show_var(mib, len);
272 /* These functions will dump out various interesting structures. */
275 S_clockinfo(int l2, void *p)
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);
286 S_loadavg(int l2, void *p)
288 struct loadavg *tv = (struct loadavg*)p;
290 if (l2 != sizeof(*tv))
291 err(1, "S_loadavg %d != %d", l2, sizeof(*tv));
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);
301 S_timeval(int l2, void *p)
303 struct timeval *tv = (struct timeval*)p;
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);
312 p1 = strdup(ctime(&tv_sec));
313 for (p2=p1; *p2 ; p2++)
321 T_dev_t(int l2, void *p)
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));
331 printf("{ major = %d, minor = %d }",
332 major(*d), minor(*d));
338 set_T_dev_t (char *path, void **val, int *size)
340 static struct stat statb;
342 if (strcmp(path, "none") && strcmp(path, "off")) {
343 int rc = stat (path, &statb);
345 err(1, "cannot stat %s", path);
348 if (!S_ISCHR(statb.st_mode)) {
349 errx(1, "must specify a device special file.");
352 statb.st_rdev = NODEV;
354 *val = (char*) &statb.st_rdev;
355 *size = sizeof statb.st_rdev;
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.
368 name2oid(char *name, int *oidp)
377 j = CTL_MAXNAME * sizeof(int);
378 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
386 oidfmt(int *oid, int len, char *fmt, u_int *kind)
388 int qoid[CTL_MAXNAME+2];
395 memcpy(qoid + 2, oid, len * sizeof(int));
398 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
400 err(1, "sysctl fmt %d %d %d", i, j, errno);
403 *kind = *(u_int *)buf;
406 strcpy(fmt, (char *)(buf + sizeof(u_int)));
412 * Code to map a bootdev major number into a suitable device name.
413 * Major numbers are mapped into names as in boot2.c
424 -1, NULL /* terminator */
428 machdep_bootdev(u_long value)
430 int majdev, unit, slice, part;
433 if (value & B_MAGICMASK != B_DEVMAGIC) {
434 printf("invalid (0x%08x)", value);
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);
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);
450 printf("/dev/%s%ds%d%c",
451 p->name, unit, slice - BASE_SLICE + 1, part + 'a');
453 printf("unknown (major %d unit %d slice %d part %d)",
454 majdev, unit, slice, part);
460 * This formats and outputs the value of one variable
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.
468 show_var(int *oid, int nlen)
470 u_char buf[BUFSIZ], *val, *p;
471 char name[BUFSIZ], *fmt, *sep;
472 int qoid[CTL_MAXNAME+2];
476 int (*func)(int, void *);
479 memcpy(qoid + 2, oid, nlen * sizeof(int));
483 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
485 err(1, "sysctl name %d %d %d", i, j, errno);
497 if (dflag) { /* just print description */
500 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
502 printf("%s%s", name, sep);
506 /* find an estimate of how much we need for this var */
508 i = sysctl(oid, nlen, 0, &j, 0, 0);
509 j += j; /* we want to be sure :-) */
513 i = sysctl(oid, nlen, val, &len, 0, 0);
518 fwrite(val, 1, len, stdout);
524 oidfmt(oid, nlen, fmt, &kind);
529 printf("%s%s", name, sep);
530 printf("%.*s", len, p);
535 printf("%s%s", name, sep);
538 while (len >= sizeof(int)) {
540 printf("%s%u", val, *(unsigned int *)p);
542 printf("%s%d", val, *(int *)p);
551 printf("%s%s", name, sep);
554 if (!strcmp(name, "machdep.guessed_bootdev"))
555 return machdep_bootdev(*(unsigned long *)p);
558 while (len >= sizeof(long)) {
560 printf("%s%lu", val, *(unsigned long *)p);
562 printf("%s%ld", val, *(long *)p);
571 printf("%s%s", name, sep);
572 printf("%p", *(void **)p);
578 if (strcmp(fmt, "S,clockinfo") == 0)
580 else if (strcmp(fmt, "S,timeval") == 0)
582 else if (strcmp(fmt, "S,loadavg") == 0)
584 else if (strcmp(fmt, "T,dev_t") == 0)
590 printf("%s%s", name, sep);
591 return ((*func)(len, p));
595 if (!oflag && !xflag)
598 printf("%s%s", name, sep);
599 printf("Format:%s Length:%d Dump:0x", fmt, len);
600 while (len-- && (xflag || p < val + 16))
601 printf("%02x", *p++);
602 if (!xflag && len > 16)
610 sysctl_all (int *oid, int len)
612 int name1[22], name2[22];
620 memcpy(name1+2, oid, len * sizeof(int));
628 j = sysctl(name1, l1, name2, &l2, 0, 0);
633 err(1, "sysctl(getnext) %d %d", j, l2);
641 for (i = 0; i < len; i++)
642 if (name2[i] != oid[i])
645 i = show_var(name2, l2);
649 memcpy(name1+2, name2, l2 * sizeof(int));