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);
236 len = name2oid(bufp, mib);
244 warn("unknown oid '%s'%s", bufp, line);
249 if (oidfmt(mib, len, fmt, &kind)) {
250 warn("couldn't find format of oid '%s'%s", bufp, line);
257 if (newval == NULL || dflag) {
258 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
260 i = show_var(mib, len);
264 sysctl_all(mib, len);
266 i = show_var(mib, len);
271 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
272 warnx("oid '%s' isn't a leaf node%s", bufp, line);
276 if (!(kind & CTLFLAG_WR)) {
277 if (kind & CTLFLAG_TUN) {
278 warnx("oid '%s' is a read only tunable%s", bufp, line);
279 warnx("Tunable values are set in /boot/loader.conf");
281 warnx("oid '%s' is read only%s", bufp, line);
285 if ((kind & CTLTYPE) == CTLTYPE_INT ||
286 (kind & CTLTYPE) == CTLTYPE_UINT ||
287 (kind & CTLTYPE) == CTLTYPE_LONG ||
288 (kind & CTLTYPE) == CTLTYPE_ULONG ||
289 (kind & CTLTYPE) == CTLTYPE_S64 ||
290 (kind & CTLTYPE) == CTLTYPE_U64) {
291 if (strlen(newval) == 0) {
292 warnx("empty numeric value");
297 switch (kind & CTLTYPE) {
299 if (strcmp(fmt, "IK") == 0) {
300 if (!set_IK(newval, &intval)) {
301 warnx("invalid value '%s'%s",
302 (char *)newval, line);
306 intval = (int)strtol(newval, &endptr,
308 if (endptr == newval || *endptr != '\0') {
309 warnx("invalid integer '%s'%s",
310 (char *)newval, line);
315 newsize = sizeof(intval);
318 uintval = (int) strtoul(newval, &endptr, 0);
319 if (endptr == newval || *endptr != '\0') {
320 warnx("invalid unsigned integer '%s'%s",
321 (char *)newval, line);
325 newsize = sizeof(uintval);
328 longval = strtol(newval, &endptr, 0);
329 if (endptr == newval || *endptr != '\0') {
330 warnx("invalid long integer '%s'%s",
331 (char *)newval, line);
335 newsize = sizeof(longval);
338 ulongval = strtoul(newval, &endptr, 0);
339 if (endptr == newval || *endptr != '\0') {
340 warnx("invalid unsigned long integer"
341 " '%s'%s", (char *)newval, line);
345 newsize = sizeof(ulongval);
350 i64val = strtoimax(newval, &endptr, 0);
351 if (endptr == newval || *endptr != '\0') {
352 warnx("invalid int64_t '%s'%s",
353 (char *)newval, line);
357 newsize = sizeof(i64val);
360 u64val = strtoumax(newval, &endptr, 0);
361 if (endptr == newval || *endptr != '\0') {
362 warnx("invalid uint64_t '%s'%s",
363 (char *)newval, line);
367 newsize = sizeof(u64val);
372 warnx("oid '%s' is type %d,"
373 " cannot set that%s", bufp,
374 kind & CTLTYPE, line);
378 i = show_var(mib, len);
379 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
384 warnx("%s: value is not available%s",
388 warnx("%s: specification is incomplete%s",
392 warnx("%s: type is unknown to this program%s",
396 warn("%s%s", string, line);
404 j = show_var(mib, len);
414 parsefile(const char *filename)
417 char line[BUFSIZ], *p, *pq, *pdq;
418 int warncount = 0, lineno = 0;
420 file = fopen(filename, "r");
422 err(EX_NOINPUT, "%s", filename);
423 while (fgets(line, sizeof(line), file) != NULL) {
426 pq = strchr(line, '\'');
427 pdq = strchr(line, '\"');
428 /* Replace the first # with \0. */
429 while((p = strchr(p, '#')) != NULL) {
430 if (pq != NULL && p > pq) {
431 if ((p = strchr(pq+1, '\'')) != NULL)
434 } else if (pdq != NULL && p > pdq) {
435 if ((p = strchr(pdq+1, '\"')) != NULL)
438 } else if (p == line || *(p-1) != '\\') {
445 p = line + strlen(line) - 1;
446 while (p >= line && isspace((int)*p)) {
451 while (isspace((int)*p))
456 warncount += parse(p, lineno);
463 /* These functions will dump out various interesting structures. */
466 S_clockinfo(size_t l2, void *p)
468 struct clockinfo *ci = (struct clockinfo*)p;
470 if (l2 != sizeof(*ci)) {
471 warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci));
474 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
475 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
476 ci->hz, ci->tick, ci->profhz, ci->stathz);
481 S_loadavg(size_t l2, void *p)
483 struct loadavg *tv = (struct loadavg*)p;
485 if (l2 != sizeof(*tv)) {
486 warnx("S_loadavg %zu != %zu", l2, sizeof(*tv));
489 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
490 (double)tv->ldavg[0]/(double)tv->fscale,
491 (double)tv->ldavg[1]/(double)tv->fscale,
492 (double)tv->ldavg[2]/(double)tv->fscale);
497 S_timeval(size_t l2, void *p)
499 struct timeval *tv = (struct timeval*)p;
503 if (l2 != sizeof(*tv)) {
504 warnx("S_timeval %zu != %zu", l2, sizeof(*tv));
507 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
508 "{ sec = %jd, usec = %ld } ",
509 (intmax_t)tv->tv_sec, tv->tv_usec);
511 p1 = strdup(ctime(&tv_sec));
512 for (p2=p1; *p2 ; p2++)
521 S_vmtotal(size_t l2, void *p)
523 struct vmtotal *v = (struct vmtotal *)p;
524 int pageKilo = getpagesize() / 1024;
526 if (l2 != sizeof(*v)) {
527 warnx("S_vmtotal %zu != %zu", l2, sizeof(*v));
532 "\nSystem wide totals computed every five seconds:"
533 " (values in kilobytes)\n");
534 printf("===============================================\n");
536 "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: "
538 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
540 "Virtual Memory:\t\t(Total: %dK Active: %dK)\n",
541 v->t_vm * pageKilo, v->t_avm * pageKilo);
542 printf("Real Memory:\t\t(Total: %dK Active: %dK)\n",
543 v->t_rm * pageKilo, v->t_arm * pageKilo);
544 printf("Shared Virtual Memory:\t(Total: %dK Active: %dK)\n",
545 v->t_vmshr * pageKilo, v->t_avmshr * pageKilo);
546 printf("Shared Real Memory:\t(Total: %dK Active: %dK)\n",
547 v->t_rmshr * pageKilo, v->t_armshr * pageKilo);
548 printf("Free Memory:\t%dK", v->t_free * pageKilo);
554 #define efi_next_descriptor(ptr, size) \
555 ((struct efi_md *)(((uint8_t *) ptr) + size))
558 S_efi_map(size_t l2, void *p)
560 struct efi_map_header *efihdr;
566 static const char *types[] = {
572 "RuntimeServicesCode",
573 "RuntimeServicesData",
574 "ConventionalMemory",
579 "MemoryMappedIOPortSpace",
584 * Memory map data provided by UEFI via the GetMemoryMap
587 if (l2 < sizeof(*efihdr)) {
588 warnx("S_efi_map length less than header");
592 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
593 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
595 if (efihdr->descriptor_size == 0)
597 if (l2 != efisz + efihdr->memory_size) {
598 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz +
599 efihdr->memory_size);
602 ndesc = efihdr->memory_size / efihdr->descriptor_size;
604 printf("\n%23s %12s %12s %8s %4s",
605 "Type", "Physical", "Virtual", "#Pages", "Attr");
607 for (i = 0; i < ndesc; i++,
608 map = efi_next_descriptor(map, efihdr->descriptor_size)) {
609 if (map->md_type <= EFI_MD_TYPE_PALCODE)
610 type = types[map->md_type];
613 printf("\n%23s %012lx %12p %08lx ", type, map->md_phys,
614 map->md_virt, map->md_pages);
615 if (map->md_attr & EFI_MD_ATTR_UC)
617 if (map->md_attr & EFI_MD_ATTR_WC)
619 if (map->md_attr & EFI_MD_ATTR_WT)
621 if (map->md_attr & EFI_MD_ATTR_WB)
623 if (map->md_attr & EFI_MD_ATTR_UCE)
625 if (map->md_attr & EFI_MD_ATTR_WP)
627 if (map->md_attr & EFI_MD_ATTR_RP)
629 if (map->md_attr & EFI_MD_ATTR_XP)
631 if (map->md_attr & EFI_MD_ATTR_RT)
638 #if defined(__amd64__) || defined(__i386__)
640 S_bios_smap_xattr(size_t l2, void *p)
642 struct bios_smap_xattr *smap, *end;
644 if (l2 % sizeof(*smap) != 0) {
645 warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2,
650 end = (struct bios_smap_xattr *)((char *)p + l2);
651 for (smap = p; smap < end; smap++)
652 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx",
653 smap->type, smap->xattr, (uintmax_t)smap->base,
654 (uintmax_t)smap->length);
660 set_IK(const char *str, int *val)
667 if ((len = strlen(str)) == 0)
670 if (*p == 'C' || *p == 'F') {
671 temp = strtof(str, &endptr);
672 if (endptr == str || endptr != p)
675 temp = (temp - 32) * 5 / 9;
676 kelv = temp * 10 + 2732;
678 kelv = (int)strtol(str, &endptr, 10);
679 if (endptr == str || *endptr != '\0')
687 * These functions uses a presently undocumented interface to the kernel
688 * to walk the tree and get the type so it can print the value.
689 * This interface is under work and consideration, and should probably
690 * be killed with a big axe by the first person who can find the time.
691 * (be aware though, that the proper interface isn't as obvious as it
692 * may seem, there are various conflicting requirements.
696 name2oid(const char *name, int *oidp)
705 j = CTL_MAXNAME * sizeof(int);
706 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
714 oidfmt(int *oid, int len, char *fmt, u_int *kind)
716 int qoid[CTL_MAXNAME+2];
723 memcpy(qoid + 2, oid, len * sizeof(int));
726 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
728 err(1, "sysctl fmt %d %zu %d", i, j, errno);
731 *kind = *(u_int *)buf;
734 strcpy(fmt, (char *)(buf + sizeof(u_int)));
738 static int ctl_sign[CTLTYPE+1] = {
744 static int ctl_size[CTLTYPE+1] = {
745 [CTLTYPE_INT] = sizeof(int),
746 [CTLTYPE_UINT] = sizeof(u_int),
747 [CTLTYPE_LONG] = sizeof(long),
748 [CTLTYPE_ULONG] = sizeof(u_long),
749 [CTLTYPE_S64] = sizeof(int64_t),
750 [CTLTYPE_U64] = sizeof(int64_t),
754 * This formats and outputs the value of one variable
756 * Returns zero if anything was actually output.
757 * Returns one if didn't know what to do with this.
758 * Return minus one if we had errors.
761 show_var(int *oid, int nlen)
763 u_char buf[BUFSIZ], *val, *oval, *p;
764 char name[BUFSIZ], fmt[BUFSIZ];
765 const char *sep, *sep1;
766 int qoid[CTL_MAXNAME+2];
769 int i, hexlen, sign, ctltype;
773 int (*func)(size_t, void *);
776 umv = mv = intlen = 0;
782 memcpy(qoid + 2, oid, nlen * sizeof(int));
786 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
788 err(1, "sysctl name %d %zu %d", i, j, errno);
790 oidfmt(oid, nlen, fmt, &kind);
791 /* if Wflag then only list sysctls that are writeable and not stats. */
792 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0))
795 /* if Tflag then only list sysctls that are tuneables. */
796 if (Tflag && (kind & CTLFLAG_TUN) == 0)
809 if (dflag) { /* just print description */
812 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
814 printf("%s%s", name, sep);
818 /* find an estimate of how much we need for this var */
820 i = sysctl(oid, nlen, 0, &j, 0, 0);
821 j += j; /* we want to be sure :-) */
823 val = oval = malloc(j + 1);
825 warnx("malloc failed");
828 ctltype = (kind & CTLTYPE);
830 i = sysctl(oid, nlen, val, &len, 0, 0);
831 if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) {
837 fwrite(val, 1, len, stdout);
843 sign = ctl_sign[ctltype];
844 intlen = ctl_size[ctltype];
849 printf("%s%s", name, sep);
850 printf("%.*s", (int)len, p);
861 printf("%s%s", name, sep);
862 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
864 while (len >= intlen) {
865 switch (kind & CTLTYPE) {
878 umv = *(uint64_t *)p;
884 printf("%#0*jx", hexlen, umv);
886 printf(hflag ? "%'ju" : "%ju", umv);
887 else if (fmt[1] == 'K') {
891 printf("%.1fC", (mv - 2732.0) / 10);
893 printf(hflag ? "%'jd" : "%jd", mv);
903 if (strcmp(fmt, "S,clockinfo") == 0)
905 else if (strcmp(fmt, "S,timeval") == 0)
907 else if (strcmp(fmt, "S,loadavg") == 0)
909 else if (strcmp(fmt, "S,vmtotal") == 0)
912 else if (strcmp(fmt, "S,efi_map_header") == 0)
915 #if defined(__amd64__) || defined(__i386__)
916 else if (strcmp(fmt, "S,bios_smap_xattr") == 0)
917 func = S_bios_smap_xattr;
923 printf("%s%s", name, sep);
930 if (!oflag && !xflag) {
935 printf("%s%s", name, sep);
936 printf("Format:%s Length:%zu Dump:0x", fmt, len);
937 while (len-- && (xflag || p < val + 16))
938 printf("%02x", *p++);
939 if (!xflag && len > 16)
949 sysctl_all(int *oid, int len)
951 int name1[22], name2[22];
959 memcpy(name1+2, oid, len * sizeof(int));
967 j = sysctl(name1, l1, name2, &l2, 0, 0);
972 err(1, "sysctl(getnext) %d %zu", j, l2);
977 if (len < 0 || l2 < (unsigned int)len)
980 for (i = 0; i < len; i++)
981 if (name2[i] != oid[i])
984 i = show_var(name2, l2);
988 memcpy(name1+2, name2, l2 * sizeof(int));