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>
71 static const char *conffile;
73 static int aflag, bflag, dflag, eflag, hflag, iflag;
74 static int Nflag, nflag, oflag, qflag, Tflag, Wflag, xflag;
76 static int oidfmt(int *, int, char *, u_int *);
77 static int parsefile(const char *);
78 static int parse(const char *, int);
79 static int show_var(int *, int);
80 static int sysctl_all(int *oid, int len);
81 static int name2oid(const char *, int *);
83 static int set_IK(const char *, int *);
89 (void)fprintf(stderr, "%s\n%s\n",
90 "usage: sysctl [-bdehiNnoqTWx] [-f filename] name[=value] ...",
91 " sysctl [-bdehNnoqTWx] -a");
96 main(int argc, char **argv)
101 setlocale(LC_NUMERIC, "");
105 while ((ch = getopt(argc, argv, "Aabdef:hiNnoqTwWxX")) != -1) {
170 if (aflag && argc == 0)
171 exit(sysctl_all(0, 0));
172 if (argc == 0 && conffile == NULL)
176 if (conffile != NULL)
177 warncount += parsefile(conffile);
180 warncount += parse(*argv++, 0);
186 * Parse a name into a MIB entry.
187 * Lookup and print out the MIB entry if it exists.
188 * Set a new value if requested.
191 parse(const char *string, int lineno)
196 unsigned int uintval;
198 unsigned long ulongval;
202 int mib[CTL_MAXNAME];
203 char *cp, *bufp, buf[BUFSIZ], *endptr, fmt[BUFSIZ], line[BUFSIZ];
207 snprintf(line, sizeof(line), " at line %d", lineno);
212 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ) {
213 warnx("oid too long: '%s'%s", string, line);
216 bufp = strsep(&cp, "=:");
218 /* Tflag just lists tunables, do not allow assignment */
219 if (Tflag || Wflag) {
220 warnx("Can't set variables when using -T or -W");
225 /* Strip a pair of " or ' if any. */
229 if (cp[strlen(cp) - 1] == *cp)
230 cp[strlen(cp) - 1] = '\0';
234 newsize = strlen(cp);
237 cp = bufp + strlen(bufp) - 1;
238 while (cp >= bufp && isspace((int)*cp)) {
242 len = name2oid(bufp, mib);
250 if (errno == ENOENT) {
251 warnx("unknown oid '%s'%s", bufp, line);
253 warn("unknown oid '%s'%s", bufp, line);
259 if (oidfmt(mib, len, fmt, &kind)) {
260 warn("couldn't find format of oid '%s'%s", bufp, line);
267 if (newval == NULL || dflag) {
268 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
270 i = show_var(mib, len);
274 sysctl_all(mib, len);
276 i = show_var(mib, len);
281 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
282 warnx("oid '%s' isn't a leaf node%s", bufp, line);
286 if (!(kind & CTLFLAG_WR)) {
287 if (kind & CTLFLAG_TUN) {
288 warnx("oid '%s' is a read only tunable%s", bufp, line);
289 warnx("Tunable values are set in /boot/loader.conf");
291 warnx("oid '%s' is read only%s", bufp, line);
295 if ((kind & CTLTYPE) == CTLTYPE_INT ||
296 (kind & CTLTYPE) == CTLTYPE_UINT ||
297 (kind & CTLTYPE) == CTLTYPE_LONG ||
298 (kind & CTLTYPE) == CTLTYPE_ULONG ||
299 (kind & CTLTYPE) == CTLTYPE_S64 ||
300 (kind & CTLTYPE) == CTLTYPE_U64) {
301 if (strlen(newval) == 0) {
302 warnx("empty numeric value");
307 switch (kind & CTLTYPE) {
309 if (strcmp(fmt, "IK") == 0) {
310 if (!set_IK(newval, &intval)) {
311 warnx("invalid value '%s'%s",
312 (char *)newval, line);
316 intval = (int)strtol(newval, &endptr,
318 if (endptr == newval || *endptr != '\0') {
319 warnx("invalid integer '%s'%s",
320 (char *)newval, line);
325 newsize = sizeof(intval);
328 uintval = (int) strtoul(newval, &endptr, 0);
329 if (endptr == newval || *endptr != '\0') {
330 warnx("invalid unsigned integer '%s'%s",
331 (char *)newval, line);
335 newsize = sizeof(uintval);
338 longval = strtol(newval, &endptr, 0);
339 if (endptr == newval || *endptr != '\0') {
340 warnx("invalid long integer '%s'%s",
341 (char *)newval, line);
345 newsize = sizeof(longval);
348 ulongval = strtoul(newval, &endptr, 0);
349 if (endptr == newval || *endptr != '\0') {
350 warnx("invalid unsigned long integer"
351 " '%s'%s", (char *)newval, line);
355 newsize = sizeof(ulongval);
360 i64val = strtoimax(newval, &endptr, 0);
361 if (endptr == newval || *endptr != '\0') {
362 warnx("invalid int64_t '%s'%s",
363 (char *)newval, line);
367 newsize = sizeof(i64val);
370 u64val = strtoumax(newval, &endptr, 0);
371 if (endptr == newval || *endptr != '\0') {
372 warnx("invalid uint64_t '%s'%s",
373 (char *)newval, line);
377 newsize = sizeof(u64val);
382 warnx("oid '%s' is type %d,"
383 " cannot set that%s", bufp,
384 kind & CTLTYPE, line);
388 i = show_var(mib, len);
389 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
394 warnx("%s: value is not available%s",
398 warnx("%s: specification is incomplete%s",
402 warnx("%s: type is unknown to this program%s",
406 warn("%s%s", string, line);
414 j = show_var(mib, len);
424 parsefile(const char *filename)
427 char line[BUFSIZ], *p, *pq, *pdq;
428 int warncount = 0, lineno = 0;
430 file = fopen(filename, "r");
432 err(EX_NOINPUT, "%s", filename);
433 while (fgets(line, sizeof(line), file) != NULL) {
436 pq = strchr(line, '\'');
437 pdq = strchr(line, '\"');
438 /* Replace the first # with \0. */
439 while((p = strchr(p, '#')) != NULL) {
440 if (pq != NULL && p > pq) {
441 if ((p = strchr(pq+1, '\'')) != NULL)
444 } else if (pdq != NULL && p > pdq) {
445 if ((p = strchr(pdq+1, '\"')) != NULL)
448 } else if (p == line || *(p-1) != '\\') {
455 p = line + strlen(line) - 1;
456 while (p >= line && isspace((int)*p)) {
461 while (isspace((int)*p))
466 warncount += parse(p, lineno);
473 /* These functions will dump out various interesting structures. */
476 S_clockinfo(size_t l2, void *p)
478 struct clockinfo *ci = (struct clockinfo*)p;
480 if (l2 != sizeof(*ci)) {
481 warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci));
484 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
485 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
486 ci->hz, ci->tick, ci->profhz, ci->stathz);
491 S_loadavg(size_t l2, void *p)
493 struct loadavg *tv = (struct loadavg*)p;
495 if (l2 != sizeof(*tv)) {
496 warnx("S_loadavg %zu != %zu", l2, sizeof(*tv));
499 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
500 (double)tv->ldavg[0]/(double)tv->fscale,
501 (double)tv->ldavg[1]/(double)tv->fscale,
502 (double)tv->ldavg[2]/(double)tv->fscale);
507 S_timeval(size_t l2, void *p)
509 struct timeval *tv = (struct timeval*)p;
513 if (l2 != sizeof(*tv)) {
514 warnx("S_timeval %zu != %zu", l2, sizeof(*tv));
517 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
518 "{ sec = %jd, usec = %ld } ",
519 (intmax_t)tv->tv_sec, tv->tv_usec);
521 p1 = strdup(ctime(&tv_sec));
522 for (p2=p1; *p2 ; p2++)
531 S_vmtotal(size_t l2, void *p)
533 struct vmtotal *v = (struct vmtotal *)p;
534 int pageKilo = getpagesize() / 1024;
536 if (l2 != sizeof(*v)) {
537 warnx("S_vmtotal %zu != %zu", l2, sizeof(*v));
542 "\nSystem wide totals computed every five seconds:"
543 " (values in kilobytes)\n");
544 printf("===============================================\n");
546 "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: "
548 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
550 "Virtual Memory:\t\t(Total: %jdK Active: %jdK)\n",
551 (intmax_t)v->t_vm * pageKilo, (intmax_t)v->t_avm * pageKilo);
552 printf("Real Memory:\t\t(Total: %jdK Active: %jdK)\n",
553 (intmax_t)v->t_rm * pageKilo, (intmax_t)v->t_arm * pageKilo);
554 printf("Shared Virtual Memory:\t(Total: %jdK Active: %jdK)\n",
555 (intmax_t)v->t_vmshr * pageKilo, (intmax_t)v->t_avmshr * pageKilo);
556 printf("Shared Real Memory:\t(Total: %jdK Active: %jdK)\n",
557 (intmax_t)v->t_rmshr * pageKilo, (intmax_t)v->t_armshr * pageKilo);
558 printf("Free Memory:\t%jdK", (intmax_t)v->t_free * pageKilo);
564 #define efi_next_descriptor(ptr, size) \
565 ((struct efi_md *)(((uint8_t *) ptr) + size))
568 S_efi_map(size_t l2, void *p)
570 struct efi_map_header *efihdr;
576 static const char *types[] = {
582 "RuntimeServicesCode",
583 "RuntimeServicesData",
584 "ConventionalMemory",
589 "MemoryMappedIOPortSpace",
594 * Memory map data provided by UEFI via the GetMemoryMap
597 if (l2 < sizeof(*efihdr)) {
598 warnx("S_efi_map length less than header");
602 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
603 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
605 if (efihdr->descriptor_size == 0)
607 if (l2 != efisz + efihdr->memory_size) {
608 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz +
609 efihdr->memory_size);
612 ndesc = efihdr->memory_size / efihdr->descriptor_size;
614 printf("\n%23s %12s %12s %8s %4s",
615 "Type", "Physical", "Virtual", "#Pages", "Attr");
617 for (i = 0; i < ndesc; i++,
618 map = efi_next_descriptor(map, efihdr->descriptor_size)) {
619 if (map->md_type <= EFI_MD_TYPE_PALCODE)
620 type = types[map->md_type];
623 printf("\n%23s %012lx %12p %08lx ", type, map->md_phys,
624 map->md_virt, map->md_pages);
625 if (map->md_attr & EFI_MD_ATTR_UC)
627 if (map->md_attr & EFI_MD_ATTR_WC)
629 if (map->md_attr & EFI_MD_ATTR_WT)
631 if (map->md_attr & EFI_MD_ATTR_WB)
633 if (map->md_attr & EFI_MD_ATTR_UCE)
635 if (map->md_attr & EFI_MD_ATTR_WP)
637 if (map->md_attr & EFI_MD_ATTR_RP)
639 if (map->md_attr & EFI_MD_ATTR_XP)
641 if (map->md_attr & EFI_MD_ATTR_RT)
648 #if defined(__amd64__) || defined(__i386__)
650 S_bios_smap_xattr(size_t l2, void *p)
652 struct bios_smap_xattr *smap, *end;
654 if (l2 % sizeof(*smap) != 0) {
655 warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2,
660 end = (struct bios_smap_xattr *)((char *)p + l2);
661 for (smap = p; smap < end; smap++)
662 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx",
663 smap->type, smap->xattr, (uintmax_t)smap->base,
664 (uintmax_t)smap->length);
670 set_IK(const char *str, int *val)
677 if ((len = strlen(str)) == 0)
680 if (*p == 'C' || *p == 'F') {
681 temp = strtof(str, &endptr);
682 if (endptr == str || endptr != p)
685 temp = (temp - 32) * 5 / 9;
686 kelv = temp * 10 + 2732;
688 kelv = (int)strtol(str, &endptr, 10);
689 if (endptr == str || *endptr != '\0')
697 * These functions uses a presently undocumented interface to the kernel
698 * to walk the tree and get the type so it can print the value.
699 * This interface is under work and consideration, and should probably
700 * be killed with a big axe by the first person who can find the time.
701 * (be aware though, that the proper interface isn't as obvious as it
702 * may seem, there are various conflicting requirements.
706 name2oid(const char *name, int *oidp)
715 j = CTL_MAXNAME * sizeof(int);
716 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
724 oidfmt(int *oid, int len, char *fmt, u_int *kind)
726 int qoid[CTL_MAXNAME+2];
733 memcpy(qoid + 2, oid, len * sizeof(int));
736 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
738 err(1, "sysctl fmt %d %zu %d", i, j, errno);
741 *kind = *(u_int *)buf;
744 strcpy(fmt, (char *)(buf + sizeof(u_int)));
748 static int ctl_sign[CTLTYPE+1] = {
754 static int ctl_size[CTLTYPE+1] = {
755 [CTLTYPE_INT] = sizeof(int),
756 [CTLTYPE_UINT] = sizeof(u_int),
757 [CTLTYPE_LONG] = sizeof(long),
758 [CTLTYPE_ULONG] = sizeof(u_long),
759 [CTLTYPE_S64] = sizeof(int64_t),
760 [CTLTYPE_U64] = sizeof(int64_t),
764 * This formats and outputs the value of one variable
766 * Returns zero if anything was actually output.
767 * Returns one if didn't know what to do with this.
768 * Return minus one if we had errors.
771 show_var(int *oid, int nlen)
773 u_char buf[BUFSIZ], *val, *oval, *p;
774 char name[BUFSIZ], fmt[BUFSIZ];
775 const char *sep, *sep1;
776 int qoid[CTL_MAXNAME+2];
779 int i, hexlen, sign, ctltype;
783 int (*func)(size_t, void *);
786 umv = mv = intlen = 0;
792 memcpy(qoid + 2, oid, nlen * sizeof(int));
796 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
798 err(1, "sysctl name %d %zu %d", i, j, errno);
800 oidfmt(oid, nlen, fmt, &kind);
801 /* if Wflag then only list sysctls that are writeable and not stats. */
802 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0))
805 /* if Tflag then only list sysctls that are tuneables. */
806 if (Tflag && (kind & CTLFLAG_TUN) == 0)
819 if (dflag) { /* just print description */
822 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
824 printf("%s%s", name, sep);
828 /* find an estimate of how much we need for this var */
830 i = sysctl(oid, nlen, 0, &j, 0, 0);
831 j += j; /* we want to be sure :-) */
833 val = oval = malloc(j + 1);
835 warnx("malloc failed");
838 ctltype = (kind & CTLTYPE);
840 i = sysctl(oid, nlen, val, &len, 0, 0);
841 if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) {
847 fwrite(val, 1, len, stdout);
853 sign = ctl_sign[ctltype];
854 intlen = ctl_size[ctltype];
859 printf("%s%s", name, sep);
860 printf("%.*s", (int)len, p);
871 printf("%s%s", name, sep);
872 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
874 while (len >= intlen) {
875 switch (kind & CTLTYPE) {
888 umv = *(uint64_t *)p;
894 printf("%#0*jx", hexlen, umv);
896 printf(hflag ? "%'ju" : "%ju", umv);
897 else if (fmt[1] == 'K') {
901 printf("%.1fC", (mv - 2732.0) / 10);
903 printf(hflag ? "%'jd" : "%jd", mv);
913 if (strcmp(fmt, "S,clockinfo") == 0)
915 else if (strcmp(fmt, "S,timeval") == 0)
917 else if (strcmp(fmt, "S,loadavg") == 0)
919 else if (strcmp(fmt, "S,vmtotal") == 0)
922 else if (strcmp(fmt, "S,efi_map_header") == 0)
925 #if defined(__amd64__) || defined(__i386__)
926 else if (strcmp(fmt, "S,bios_smap_xattr") == 0)
927 func = S_bios_smap_xattr;
933 printf("%s%s", name, sep);
940 if (!oflag && !xflag) {
945 printf("%s%s", name, sep);
946 printf("Format:%s Length:%zu Dump:0x", fmt, len);
947 while (len-- && (xflag || p < val + 16))
948 printf("%02x", *p++);
949 if (!xflag && len > 16)
959 sysctl_all(int *oid, int len)
961 int name1[22], name2[22];
969 memcpy(name1+2, oid, len * sizeof(int));
977 j = sysctl(name1, l1, name2, &l2, 0, 0);
982 err(1, "sysctl(getnext) %d %zu", j, l2);
987 if (len < 0 || l2 < (unsigned int)len)
990 for (i = 0; i < len; i++)
991 if (name2[i] != oid[i])
994 i = show_var(name2, l2);
998 memcpy(name1+2, name2, l2 * sizeof(int));