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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
40 static const char rcsid[] =
44 #include <sys/param.h>
46 #include <sys/resource.h>
48 #include <sys/sysctl.h>
49 #include <sys/vmmeter.h>
53 #include <machine/metadata.h>
56 #if defined(__amd64__) || defined(__i386__)
57 #include <machine/pc/bios.h>
72 static const char *conffile;
74 static int aflag, bflag, Bflag, dflag, eflag, hflag, iflag;
75 static int Nflag, nflag, oflag, qflag, Tflag, Wflag, xflag;
77 static int oidfmt(int *, int, char *, u_int *);
78 static int parsefile(const char *);
79 static int parse(const char *, int);
80 static int show_var(int *, int);
81 static int sysctl_all(int *oid, int len);
82 static int name2oid(const char *, int *);
84 static int strIKtoi(const char *, char **, const char *);
86 static int ctl_sign[CTLTYPE+1] = {
92 static int ctl_size[CTLTYPE+1] = {
93 [CTLTYPE_INT] = sizeof(int),
94 [CTLTYPE_UINT] = sizeof(u_int),
95 [CTLTYPE_LONG] = sizeof(long),
96 [CTLTYPE_ULONG] = sizeof(u_long),
97 [CTLTYPE_S64] = sizeof(int64_t),
98 [CTLTYPE_U64] = sizeof(uint64_t),
101 static const char *ctl_typename[CTLTYPE+1] = {
102 [CTLTYPE_INT] = "integer",
103 [CTLTYPE_UINT] = "unsigned integer",
104 [CTLTYPE_LONG] = "long integer",
105 [CTLTYPE_ULONG] = "unsigned long",
106 [CTLTYPE_S64] = "int64_t",
107 [CTLTYPE_U64] = "uint64_t",
114 (void)fprintf(stderr, "%s\n%s\n",
115 "usage: sysctl [-bdehiNnoqTWx] [ -B <bufsize> ] [-f filename] name[=value] ...",
116 " sysctl [-bdehNnoqTWx] [ -B <bufsize> ] -a");
121 main(int argc, char **argv)
126 setlocale(LC_NUMERIC, "");
130 while ((ch = getopt(argc, argv, "AabB:def:hiNnoqTwWxX")) != -1) {
143 Bflag = strtol(optarg, NULL, 0);
198 if (aflag && argc == 0)
199 exit(sysctl_all(0, 0));
200 if (argc == 0 && conffile == NULL)
204 if (conffile != NULL)
205 warncount += parsefile(conffile);
208 warncount += parse(*argv++, 0);
214 * Parse a name into a MIB entry.
215 * Lookup and print out the MIB entry if it exists.
216 * Set a new value if requested.
219 parse(const char *string, int lineno)
223 const char *newvalstr = NULL;
225 unsigned int uintval;
227 unsigned long ulongval;
228 size_t newsize = Bflag;
231 int mib[CTL_MAXNAME];
232 char *cp, *bufp, buf[BUFSIZ], *endptr = NULL, fmt[BUFSIZ], line[BUFSIZ];
236 snprintf(line, sizeof(line), " at line %d", lineno);
241 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ) {
242 warnx("oid too long: '%s'%s", string, line);
245 bufp = strsep(&cp, "=:");
247 /* Tflag just lists tunables, do not allow assignment */
248 if (Tflag || Wflag) {
249 warnx("Can't set variables when using -T or -W");
254 /* Strip a pair of " or ' if any. */
258 if (cp[strlen(cp) - 1] == *cp)
259 cp[strlen(cp) - 1] = '\0';
263 newsize = strlen(cp);
265 len = name2oid(bufp, mib);
273 warn("unknown oid '%s'%s", bufp, line);
278 if (oidfmt(mib, len, fmt, &kind)) {
279 warn("couldn't find format of oid '%s'%s", bufp, line);
286 if (newvalstr == NULL || dflag) {
287 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
289 i = show_var(mib, len);
293 sysctl_all(mib, len);
295 i = show_var(mib, len);
300 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
301 warnx("oid '%s' isn't a leaf node%s", bufp, line);
305 if (!(kind & CTLFLAG_WR)) {
306 if (kind & CTLFLAG_TUN) {
307 warnx("oid '%s' is a read only tunable%s", bufp, line);
308 warnx("Tunable values are set in /boot/loader.conf");
310 warnx("oid '%s' is read only%s", bufp, line);
314 switch (kind & CTLTYPE) {
321 if (strlen(newvalstr) == 0) {
322 warnx("empty numeric value");
329 warnx("oid '%s' is type %d,"
330 " cannot set that%s", bufp,
331 kind & CTLTYPE, line);
337 switch (kind & CTLTYPE) {
339 if (strncmp(fmt, "IK", 2) == 0)
340 intval = strIKtoi(newvalstr, &endptr, fmt);
342 intval = (int)strtol(newvalstr, &endptr,
345 newsize = sizeof(intval);
348 uintval = (int) strtoul(newvalstr, &endptr, 0);
350 newsize = sizeof(uintval);
353 longval = strtol(newvalstr, &endptr, 0);
355 newsize = sizeof(longval);
358 ulongval = strtoul(newvalstr, &endptr, 0);
360 newsize = sizeof(ulongval);
366 i64val = strtoimax(newvalstr, &endptr, 0);
368 newsize = sizeof(i64val);
371 u64val = strtoumax(newvalstr, &endptr, 0);
373 newsize = sizeof(u64val);
380 if (errno != 0 || endptr == newvalstr ||
381 (endptr != NULL && *endptr != '\0')) {
382 warnx("invalid %s '%s'%s", ctl_typename[kind & CTLTYPE],
387 i = show_var(mib, len);
388 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
393 warnx("%s: value is not available%s",
397 warnx("%s: specification is incomplete%s",
401 warnx("%s: type is unknown to this program%s",
405 warn("%s%s", string, line);
413 j = show_var(mib, len);
423 parsefile(const char *filename)
426 char line[BUFSIZ], *p, *pq, *pdq;
427 int warncount = 0, lineno = 0;
429 file = fopen(filename, "r");
431 err(EX_NOINPUT, "%s", filename);
432 while (fgets(line, sizeof(line), file) != NULL) {
435 pq = strchr(line, '\'');
436 pdq = strchr(line, '\"');
437 /* Replace the first # with \0. */
438 while((p = strchr(p, '#')) != NULL) {
439 if (pq != NULL && p > pq) {
440 if ((p = strchr(pq+1, '\'')) != NULL)
443 } else if (pdq != NULL && p > pdq) {
444 if ((p = strchr(pdq+1, '\"')) != NULL)
447 } else if (p == line || *(p-1) != '\\') {
454 p = line + strlen(line) - 1;
455 while (p >= line && isspace((int)*p)) {
460 while (isspace((int)*p))
465 warncount += parse(p, lineno);
472 /* These functions will dump out various interesting structures. */
475 S_clockinfo(size_t l2, void *p)
477 struct clockinfo *ci = (struct clockinfo*)p;
479 if (l2 != sizeof(*ci)) {
480 warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci));
483 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
484 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
485 ci->hz, ci->tick, ci->profhz, ci->stathz);
490 S_loadavg(size_t l2, void *p)
492 struct loadavg *tv = (struct loadavg*)p;
494 if (l2 != sizeof(*tv)) {
495 warnx("S_loadavg %zu != %zu", l2, sizeof(*tv));
498 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
499 (double)tv->ldavg[0]/(double)tv->fscale,
500 (double)tv->ldavg[1]/(double)tv->fscale,
501 (double)tv->ldavg[2]/(double)tv->fscale);
506 S_timeval(size_t l2, void *p)
508 struct timeval *tv = (struct timeval*)p;
512 if (l2 != sizeof(*tv)) {
513 warnx("S_timeval %zu != %zu", l2, sizeof(*tv));
516 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
517 "{ sec = %jd, usec = %ld } ",
518 (intmax_t)tv->tv_sec, tv->tv_usec);
520 p1 = strdup(ctime(&tv_sec));
521 for (p2=p1; *p2 ; p2++)
530 S_vmtotal(size_t l2, void *p)
532 struct vmtotal *v = (struct vmtotal *)p;
533 int pageKilo = getpagesize() / 1024;
535 if (l2 != sizeof(*v)) {
536 warnx("S_vmtotal %zu != %zu", l2, sizeof(*v));
541 "\nSystem wide totals computed every five seconds:"
542 " (values in kilobytes)\n");
543 printf("===============================================\n");
545 "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: "
547 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
549 "Virtual Memory:\t\t(Total: %dK Active: %dK)\n",
550 v->t_vm * pageKilo, v->t_avm * pageKilo);
551 printf("Real Memory:\t\t(Total: %dK Active: %dK)\n",
552 v->t_rm * pageKilo, v->t_arm * pageKilo);
553 printf("Shared Virtual Memory:\t(Total: %dK Active: %dK)\n",
554 v->t_vmshr * pageKilo, v->t_avmshr * pageKilo);
555 printf("Shared Real Memory:\t(Total: %dK Active: %dK)\n",
556 v->t_rmshr * pageKilo, v->t_armshr * pageKilo);
557 printf("Free Memory:\t%dK", v->t_free * pageKilo);
563 #define efi_next_descriptor(ptr, size) \
564 ((struct efi_md *)(((uint8_t *) ptr) + size))
567 S_efi_map(size_t l2, void *p)
569 struct efi_map_header *efihdr;
575 static const char *types[] = {
581 "RuntimeServicesCode",
582 "RuntimeServicesData",
583 "ConventionalMemory",
588 "MemoryMappedIOPortSpace",
593 * Memory map data provided by UEFI via the GetMemoryMap
596 if (l2 < sizeof(*efihdr)) {
597 warnx("S_efi_map length less than header");
601 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
602 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
604 if (efihdr->descriptor_size == 0)
606 if (l2 != efisz + efihdr->memory_size) {
607 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz +
608 efihdr->memory_size);
611 ndesc = efihdr->memory_size / efihdr->descriptor_size;
613 printf("\n%23s %12s %12s %8s %4s",
614 "Type", "Physical", "Virtual", "#Pages", "Attr");
616 for (i = 0; i < ndesc; i++,
617 map = efi_next_descriptor(map, efihdr->descriptor_size)) {
618 if (map->md_type <= EFI_MD_TYPE_PALCODE)
619 type = types[map->md_type];
622 printf("\n%23s %012lx %12p %08lx ", type, map->md_phys,
623 map->md_virt, map->md_pages);
624 if (map->md_attr & EFI_MD_ATTR_UC)
626 if (map->md_attr & EFI_MD_ATTR_WC)
628 if (map->md_attr & EFI_MD_ATTR_WT)
630 if (map->md_attr & EFI_MD_ATTR_WB)
632 if (map->md_attr & EFI_MD_ATTR_UCE)
634 if (map->md_attr & EFI_MD_ATTR_WP)
636 if (map->md_attr & EFI_MD_ATTR_RP)
638 if (map->md_attr & EFI_MD_ATTR_XP)
640 if (map->md_attr & EFI_MD_ATTR_RT)
647 #if defined(__amd64__) || defined(__i386__)
649 S_bios_smap_xattr(size_t l2, void *p)
651 struct bios_smap_xattr *smap, *end;
653 if (l2 % sizeof(*smap) != 0) {
654 warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2,
659 end = (struct bios_smap_xattr *)((char *)p + l2);
660 for (smap = p; smap < end; smap++)
661 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx",
662 smap->type, smap->xattr, (uintmax_t)smap->base,
663 (uintmax_t)smap->length);
669 strIKtoi(const char *str, char **endptrp, const char *fmt)
680 /* caller already checked this */
684 * A format of "IK" is in deciKelvin. A format of "IK3" is in
685 * milliKelvin. The single digit following IK is log10 of the
686 * multiplying factor to convert Kelvin into the untis of this sysctl,
687 * or the dividing factor to convert the sysctl value to Kelvin. Numbers
688 * larger than 6 will run into precision issues with 32-bit integers.
689 * Characters that aren't ASCII digits after the 'K' are ignored. No
690 * localization is present because this is an interface from the kernel
691 * to this program (eg not an end-user interface), so isdigit() isn't
694 if (fmt[2] != '\0' && fmt[2] >= '0' && fmt[2] <= '9')
699 if (*p == 'C' || *p == 'F' || *p == 'K') {
700 temp = strtof(str, endptrp);
701 if (*endptrp != str && *endptrp == p && errno == 0) {
703 temp = (temp - 32) * 5 / 9;
707 for (i = 0; i < prec; i++)
709 return ((int)(temp + 0.5));
712 /* No unit specified -> treat it as a raw number */
713 kelv = (int)strtol(str, endptrp, 10);
714 if (*endptrp != str && *endptrp == p && errno == 0) {
725 * These functions uses a presently undocumented interface to the kernel
726 * to walk the tree and get the type so it can print the value.
727 * This interface is under work and consideration, and should probably
728 * be killed with a big axe by the first person who can find the time.
729 * (be aware though, that the proper interface isn't as obvious as it
730 * may seem, there are various conflicting requirements.
734 name2oid(const char *name, int *oidp)
743 j = CTL_MAXNAME * sizeof(int);
744 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
752 oidfmt(int *oid, int len, char *fmt, u_int *kind)
754 int qoid[CTL_MAXNAME+2];
761 memcpy(qoid + 2, oid, len * sizeof(int));
764 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
766 err(1, "sysctl fmt %d %zu %d", i, j, errno);
769 *kind = *(u_int *)buf;
772 strcpy(fmt, (char *)(buf + sizeof(u_int)));
777 * This formats and outputs the value of one variable
779 * Returns zero if anything was actually output.
780 * Returns one if didn't know what to do with this.
781 * Return minus one if we had errors.
784 show_var(int *oid, int nlen)
786 u_char buf[BUFSIZ], *val, *oval, *p;
787 char name[BUFSIZ], fmt[BUFSIZ];
788 const char *sep, *sep1;
789 int qoid[CTL_MAXNAME+2];
792 int i, hexlen, sign, ctltype;
797 int (*func)(size_t, void *);
801 umv = mv = intlen = 0;
807 memcpy(qoid + 2, oid, nlen * sizeof(int));
811 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
813 err(1, "sysctl name %d %zu %d", i, j, errno);
815 oidfmt(oid, nlen, fmt, &kind);
816 /* if Wflag then only list sysctls that are writeable and not stats. */
817 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0))
820 /* if Tflag then only list sysctls that are tuneables. */
821 if (Tflag && (kind & CTLFLAG_TUN) == 0)
834 if (dflag) { /* just print description */
837 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
839 printf("%s%s", name, sep);
843 /* find an estimate of how much we need for this var */
848 i = sysctl(oid, nlen, 0, &j, 0, 0);
849 j += j; /* we want to be sure :-) */
852 val = oval = malloc(j + 1);
854 warnx("malloc failed");
857 ctltype = (kind & CTLTYPE);
859 i = sysctl(oid, nlen, val, &len, 0, 0);
860 if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) {
866 fwrite(val, 1, len, stdout);
872 sign = ctl_sign[ctltype];
873 intlen = ctl_size[ctltype];
878 printf("%s%s", name, sep);
879 printf("%.*s", (int)len, p);
890 printf("%s%s", name, sep);
891 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
893 while (len >= intlen) {
894 switch (kind & CTLTYPE) {
907 umv = *(uint64_t *)p;
913 printf("%#0*jx", hexlen, umv);
915 printf(hflag ? "%'ju" : "%ju", umv);
916 else if (fmt[1] == 'K') {
921 * See strIKtoi for details on fmt.
927 for (int i = 0; i < prec; i++)
929 printf("%.*fC", prec,
930 (float)mv / base - 273.15);
933 printf(hflag ? "%'jd" : "%jd", mv);
943 if (strcmp(fmt, "S,clockinfo") == 0)
945 else if (strcmp(fmt, "S,timeval") == 0)
947 else if (strcmp(fmt, "S,loadavg") == 0)
949 else if (strcmp(fmt, "S,vmtotal") == 0)
952 else if (strcmp(fmt, "S,efi_map_header") == 0)
955 #if defined(__amd64__) || defined(__i386__)
956 else if (strcmp(fmt, "S,bios_smap_xattr") == 0)
957 func = S_bios_smap_xattr;
963 printf("%s%s", name, sep);
970 if (!oflag && !xflag) {
975 printf("%s%s", name, sep);
976 printf("Format:%s Length:%zu Dump:0x", fmt, len);
977 while (len-- && (xflag || p < val + 16))
978 printf("%02x", *p++);
979 if (!xflag && len > 16)
989 sysctl_all(int *oid, int len)
991 int name1[22], name2[22];
999 memcpy(name1+2, oid, len * sizeof(int));
1007 j = sysctl(name1, l1, name2, &l2, 0, 0);
1009 if (errno == ENOENT)
1012 err(1, "sysctl(getnext) %d %zu", j, l2);
1017 if (len < 0 || l2 < (unsigned int)len)
1020 for (i = 0; i < len; i++)
1021 if (name2[i] != oid[i])
1024 i = show_var(name2, l2);
1028 memcpy(name1+2, name2, l2 * sizeof(int));