[Midnightbsd-cvs] src [10477] trunk/sbin/sysctl/sysctl.c: sync sysctl(8)

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Wed Jun 6 20:31:59 EDT 2018


Revision: 10477
          http://svnweb.midnightbsd.org/src/?rev=10477
Author:   laffer1
Date:     2018-06-06 20:31:58 -0400 (Wed, 06 Jun 2018)
Log Message:
-----------
sync sysctl(8)

Modified Paths:
--------------
    trunk/sbin/sysctl/Makefile
    trunk/sbin/sysctl/sysctl.8
    trunk/sbin/sysctl/sysctl.c

Property Changed:
----------------
    trunk/sbin/sysctl/sysctl.8

Modified: trunk/sbin/sysctl/Makefile
===================================================================
--- trunk/sbin/sysctl/Makefile	2018-06-07 00:29:21 UTC (rev 10476)
+++ trunk/sbin/sysctl/Makefile	2018-06-07 00:31:58 UTC (rev 10477)
@@ -1,4 +1,5 @@
 #	@(#)Makefile	8.1 (Berkeley) 6/6/93
+# $FreeBSD: stable/10/sbin/sysctl/Makefile 203917 2010-02-15 14:08:06Z uqs $
 # $MidnightBSD$
 
 PROG=	sysctl

Modified: trunk/sbin/sysctl/sysctl.8
===================================================================
--- trunk/sbin/sysctl/sysctl.8	2018-06-07 00:29:21 UTC (rev 10476)
+++ trunk/sbin/sysctl/sysctl.8	2018-06-07 00:31:58 UTC (rev 10477)
@@ -26,9 +26,10 @@
 .\" SUCH DAMAGE.
 .\"
 .\"	From: @(#)sysctl.8	8.1 (Berkeley) 6/6/93
+.\" $FreeBSD: stable/10/sbin/sysctl/sysctl.8 244198 2012-12-13 23:32:47Z delphij $
 .\" $MidnightBSD$
 .\"
-.Dd January 17, 2011
+.Dd December 13, 2012
 .Dt SYSCTL 8
 .Os
 .Sh NAME
@@ -36,11 +37,12 @@
 .Nd get or set kernel state
 .Sh SYNOPSIS
 .Nm
-.Op Fl bdehiNnoqx
+.Op Fl bdehiNnoRTqx
+.Op Fl f Ar filename
 .Ar name Ns Op = Ns Ar value
 .Ar ...
 .Nm
-.Op Fl bdehNnoqx
+.Op Fl bdehNnoRTqx
 .Fl a
 .Sh DESCRIPTION
 The
@@ -80,6 +82,11 @@
 or
 .Fl n
 is specified, or a variable is being set.
+.It Fl f Ar filename
+Specify a file which contains a pair of name and value in each line.
+.Nm
+reads and processes the specified file first and then processes the name
+and value pairs in the command line argument.
 .It Fl h
 Format output for human, rather than machine, readability.
 .It Fl i
@@ -121,6 +128,11 @@
 Suppress some warnings generated by
 .Nm
 to standard error.
+.It Fl T
+Display only variables that are setable via loader (CTLFLAG_TUN).
+.It Fl W
+Display only wriable variables that are not statistical.
+Useful for determining the set of runtime tunable sysctls.
 .It Fl X
 Equivalent to
 .Fl x a
@@ -166,72 +178,72 @@
 String and integer values can be set using
 .Nm .
 .Bl -column security.bsd.unprivileged_read_msgbuf integerxxx
-.It Sy "Name	Type	Changeable
-.It "kern.ostype	string	no
-.It "kern.osrelease	string	no
-.It "kern.osrevision	integer	no
-.It "kern.version	string	no
-.It "kern.maxvnodes	integer	yes
-.It "kern.maxproc	integer	no
-.It "kern.maxprocperuid	integer	yes
-.It "kern.maxfiles	integer	yes
-.It "kern.maxfilesperproc	integer	yes
-.It "kern.argmax	integer	no
-.It "kern.securelevel	integer	raise only
-.It "kern.hostname	string	yes
-.It "kern.hostid	integer	yes
-.It "kern.clockrate	struct	no
-.It "kern.posix1version	integer	no
-.It "kern.ngroups	integer	no
-.It "kern.job_control	integer	no
-.It "kern.saved_ids	integer	no
-.It "kern.boottime	struct	no
-.It "kern.domainname	string	yes
-.It "kern.filedelay	integer	yes
-.It "kern.dirdelay	integer	yes
-.It "kern.metadelay	integer	yes
-.It "kern.osreldate	string	no
-.It "kern.bootfile	string	yes
-.It "kern.corefile	string	yes
-.It "kern.logsigexit	integer	yes
-.It "security.bsd.suser_enabled	integer	yes
-.It "security.bsd.see_other_uids	integer	yes
-.It "security.bsd.unprivileged_proc_debug	integer	yes
-.It "security.bsd.unprivileged_read_msgbuf	integer	yes
-.It "vm.loadavg	struct	no
-.It "hw.machine	string	no
-.It "hw.model	string	no
-.It "hw.ncpu	integer	no
-.It "hw.byteorder	integer	no
-.It "hw.physmem	integer	no
-.It "hw.usermem	integer	no
-.It "hw.pagesize	integer	no
-.It "hw.floatingpoint	integer	no
-.It "hw.machine_arch	string	no
-.It "hw.realmem	integer	no
-.It "machdep.adjkerntz	integer	yes
-.It "machdep.disable_rtc_set	integer	yes
-.It "machdep.guessed_bootdev	string	no
-.It "user.cs_path	string	no
-.It "user.bc_base_max	integer	no
-.It "user.bc_dim_max	integer	no
-.It "user.bc_scale_max	integer	no
-.It "user.bc_string_max	integer	no
-.It "user.coll_weights_max	integer	no
-.It "user.expr_nest_max	integer	no
-.It "user.line_max	integer	no
-.It "user.re_dup_max	integer	no
-.It "user.posix2_version	integer	no
-.It "user.posix2_c_bind	integer	no
-.It "user.posix2_c_dev	integer	no
-.It "user.posix2_char_term	integer	no
-.It "user.posix2_fort_dev	integer	no
-.It "user.posix2_fort_run	integer	no
-.It "user.posix2_localedef	integer	no
-.It "user.posix2_sw_dev	integer	no
-.It "user.posix2_upe	integer	no
-.It "user.stream_max	integer	no
-.It "user.tzname_max	integer	no
+.It Sy "Name	Type	Changeable"
+.It "kern.ostype	string	no"
+.It "kern.osrelease	string	no"
+.It "kern.osrevision	integer	no"
+.It "kern.version	string	no"
+.It "kern.maxvnodes	integer	yes"
+.It "kern.maxproc	integer	no"
+.It "kern.maxprocperuid	integer	yes"
+.It "kern.maxfiles	integer	yes"
+.It "kern.maxfilesperproc	integer	yes"
+.It "kern.argmax	integer	no"
+.It "kern.securelevel	integer	raise only"
+.It "kern.hostname	string	yes"
+.It "kern.hostid	integer	yes"
+.It "kern.clockrate	struct	no"
+.It "kern.posix1version	integer	no"
+.It "kern.ngroups	integer	no"
+.It "kern.job_control	integer	no"
+.It "kern.saved_ids	integer	no"
+.It "kern.boottime	struct	no"
+.It "kern.domainname	string	yes"
+.It "kern.filedelay	integer	yes"
+.It "kern.dirdelay	integer	yes"
+.It "kern.metadelay	integer	yes"
+.It "kern.osreldate	string	no"
+.It "kern.bootfile	string	yes"
+.It "kern.corefile	string	yes"
+.It "kern.logsigexit	integer	yes"
+.It "security.bsd.suser_enabled	integer	yes"
+.It "security.bsd.see_other_uids	integer	yes"
+.It "security.bsd.unprivileged_proc_debug	integer	yes"
+.It "security.bsd.unprivileged_read_msgbuf	integer	yes"
+.It "vm.loadavg	struct	no"
+.It "hw.machine	string	no"
+.It "hw.model	string	no"
+.It "hw.ncpu	integer	no"
+.It "hw.byteorder	integer	no"
+.It "hw.physmem	integer	no"
+.It "hw.usermem	integer	no"
+.It "hw.pagesize	integer	no"
+.It "hw.floatingpoint	integer	no"
+.It "hw.machine_arch	string	no"
+.It "hw.realmem	integer	no"
+.It "machdep.adjkerntz	integer	yes"
+.It "machdep.disable_rtc_set	integer	yes"
+.It "machdep.guessed_bootdev	string	no"
+.It "user.cs_path	string	no"
+.It "user.bc_base_max	integer	no"
+.It "user.bc_dim_max	integer	no"
+.It "user.bc_scale_max	integer	no"
+.It "user.bc_string_max	integer	no"
+.It "user.coll_weights_max	integer	no"
+.It "user.expr_nest_max	integer	no"
+.It "user.line_max	integer	no"
+.It "user.re_dup_max	integer	no"
+.It "user.posix2_version	integer	no"
+.It "user.posix2_c_bind	integer	no"
+.It "user.posix2_c_dev	integer	no"
+.It "user.posix2_char_term	integer	no"
+.It "user.posix2_fort_dev	integer	no"
+.It "user.posix2_fort_run	integer	no"
+.It "user.posix2_localedef	integer	no"
+.It "user.posix2_sw_dev	integer	no"
+.It "user.posix2_upe	integer	no"
+.It "user.stream_max	integer	no"
+.It "user.tzname_max	integer	no"
 .El
 .Sh FILES
 .Bl -tag -width ".In netinet/icmp_var.h" -compact


Property changes on: trunk/sbin/sysctl/sysctl.8
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Modified: trunk/sbin/sysctl/sysctl.c
===================================================================
--- trunk/sbin/sysctl/sysctl.c	2018-06-07 00:29:21 UTC (rev 10476)
+++ trunk/sbin/sysctl/sysctl.c	2018-06-07 00:31:58 UTC (rev 10477)
@@ -48,6 +48,15 @@
 #include <sys/sysctl.h>
 #include <sys/vmmeter.h>
 
+#ifdef __amd64__
+#include <sys/efi.h>
+#include <machine/metadata.h>
+#endif
+
+#if defined(__amd64__) || defined(__i386__)
+#include <machine/pc/bios.h>
+#endif
+
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
@@ -56,16 +65,20 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sysexits.h>
 #include <unistd.h>
 
+static const char *conffile;
+
 static int	aflag, bflag, dflag, eflag, hflag, iflag;
-static int	Nflag, nflag, oflag, qflag, xflag, warncount;
+static int	Nflag, nflag, oflag, qflag, Tflag, Wflag, xflag;
 
 static int	oidfmt(int *, int, char *, u_int *);
-static void	parse(const char *);
+static int	parsefile(const char *);
+static int	parse(const char *, int);
 static int	show_var(int *, int);
 static int	sysctl_all(int *oid, int len);
-static int	name2oid(char *, int *);
+static int	name2oid(const char *, int *);
 
 static int	set_IK(const char *, int *);
 
@@ -74,8 +87,8 @@
 {
 
 	(void)fprintf(stderr, "%s\n%s\n",
-	    "usage: sysctl [-bdehiNnoqx] name[=value] ...",
-	    "       sysctl [-bdehNnoqx] -a");
+	    "usage: sysctl [-bdehiNnoqTWx] [-f filename] name[=value] ...",
+	    "       sysctl [-bdehNnoqTWx] -a");
 	exit(1);
 }
 
@@ -83,12 +96,13 @@
 main(int argc, char **argv)
 {
 	int ch;
+	int warncount = 0;
 
 	setlocale(LC_NUMERIC, "");
 	setbuf(stdout,0);
 	setbuf(stderr,0);
 
-	while ((ch = getopt(argc, argv, "AabdehiNnoqwxX")) != -1) {
+	while ((ch = getopt(argc, argv, "Aabdef:hiNnoqTwWxX")) != -1) {
 		switch (ch) {
 		case 'A':
 			/* compatibility */
@@ -106,6 +120,9 @@
 		case 'e':
 			eflag = 1;
 			break;
+		case 'f':
+			conffile = optarg;
+			break;
 		case 'h':
 			hflag = 1;
 			break;
@@ -124,10 +141,16 @@
 		case 'q':
 			qflag = 1;
 			break;
+		case 'T':
+			Tflag = 1;
+			break;
 		case 'w':
 			/* compatibility */
 			/* ignored */
 			break;
+		case 'W':
+			Wflag = 1;
+			break;
 		case 'X':
 			/* compatibility */
 			aflag = xflag = 1;
@@ -146,13 +169,17 @@
 		usage();
 	if (aflag && argc == 0)
 		exit(sysctl_all(0, 0));
-	if (argc == 0)
+	if (argc == 0 && conffile == NULL)
 		usage();
 
 	warncount = 0;
+	if (conffile != NULL)
+		warncount += parsefile(conffile);
+
 	while (argc-- > 0)
-		parse(*argv++);
-	exit(warncount);
+		warncount += parse(*argv++, 0);
+
+	return (warncount);
 }
 
 /*
@@ -160,8 +187,8 @@
  * Lookup and print out the MIB entry if it exists.
  * Set a new value if requested.
  */
-static void
-parse(const char *string)
+static int
+parse(const char *string, int lineno)
 {
 	int len, i, j;
 	void *newval = 0;
@@ -173,33 +200,70 @@
 	int64_t i64val;
 	uint64_t u64val;
 	int mib[CTL_MAXNAME];
-	char *cp, *bufp, buf[BUFSIZ], *endptr, fmt[BUFSIZ];
+	char *cp, *bufp, buf[BUFSIZ], *endptr, fmt[BUFSIZ], line[BUFSIZ];
 	u_int kind;
 
+	if (lineno)
+		snprintf(line, sizeof(line), " at line %d", lineno);
+	else
+		line[0] = '\0';
+
 	cp = buf;
-	if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ)
-		errx(1, "oid too long: '%s'", string);
-	bufp = strsep(&cp, "=");
+	if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ) {
+		warnx("oid too long: '%s'%s", string, line);
+		return (1);
+	}
+	bufp = strsep(&cp, "=:");
 	if (cp != NULL) {
+		/* Tflag just lists tunables, do not allow assignment */
+		if (Tflag || Wflag) {
+			warnx("Can't set variables when using -T or -W");
+			usage();
+		}
 		while (isspace(*cp))
 			cp++;
+		/* Strip a pair of " or ' if any. */
+		switch (*cp) {
+		case '\"':
+		case '\'':
+			if (cp[strlen(cp) - 1] == *cp)
+				cp[strlen(cp) - 1] = '\0';
+			cp++;
+		}
 		newval = cp;
 		newsize = strlen(cp);
 	}
+	/* Trim spaces */
+	cp = bufp + strlen(bufp) - 1;
+	while (cp >= bufp && isspace((int)*cp)) {
+		*cp = '\0';
+		cp--;
+	}
 	len = name2oid(bufp, mib);
 
 	if (len < 0) {
 		if (iflag)
-			return;
+			return (0);
 		if (qflag)
+			return (1);
+		else {
+			if (errno == ENOENT) {
+				warnx("unknown oid '%s'%s", bufp, line);
+			} else {
+				warn("unknown oid '%s'%s", bufp, line);
+			}
+			return (1);
+		}
+	}
+
+	if (oidfmt(mib, len, fmt, &kind)) {
+		warn("couldn't find format of oid '%s'%s", bufp, line);
+		if (iflag)
+			return (1);
+		else
 			exit(1);
-		else
-			errx(1, "unknown oid '%s'", bufp);
 	}
 
-	if (oidfmt(mib, len, fmt, &kind))
-		err(1, "couldn't find format of oid '%s'", bufp);
-
 	if (newval == NULL || dflag) {
 		if ((kind & CTLTYPE) == CTLTYPE_NODE) {
 			if (dflag) {
@@ -214,16 +278,18 @@
 				putchar('\n');
 		}
 	} else {
-		if ((kind & CTLTYPE) == CTLTYPE_NODE)
-			errx(1, "oid '%s' isn't a leaf node", bufp);
+		if ((kind & CTLTYPE) == CTLTYPE_NODE) {
+			warnx("oid '%s' isn't a leaf node%s", bufp, line);
+			return (1);
+		}
 
 		if (!(kind & CTLFLAG_WR)) {
 			if (kind & CTLFLAG_TUN) {
-				warnx("oid '%s' is a read only tunable", bufp);
-				errx(1, "Tunable values are set in /boot/loader.conf");
-			} else {
-				errx(1, "oid '%s' is read only", bufp);
-			}
+				warnx("oid '%s' is a read only tunable%s", bufp, line);
+				warnx("Tunable values are set in /boot/loader.conf");
+			} else
+				warnx("oid '%s' is read only%s", bufp, line);
+			return (1);
 		}
 
 		if ((kind & CTLTYPE) == CTLTYPE_INT ||
@@ -232,22 +298,28 @@
 		    (kind & CTLTYPE) == CTLTYPE_ULONG ||
 		    (kind & CTLTYPE) == CTLTYPE_S64 ||
 		    (kind & CTLTYPE) == CTLTYPE_U64) {
-			if (strlen(newval) == 0)
-				errx(1, "empty numeric value");
+			if (strlen(newval) == 0) {
+				warnx("empty numeric value");
+				return (1);
+			}
 		}
 
 		switch (kind & CTLTYPE) {
 			case CTLTYPE_INT:
 				if (strcmp(fmt, "IK") == 0) {
-					if (!set_IK(newval, &intval))
-						errx(1, "invalid value '%s'",
-						    (char *)newval);
+					if (!set_IK(newval, &intval)) {
+						warnx("invalid value '%s'%s",
+						    (char *)newval, line);
+						return (1);
+					}
  				} else {
 					intval = (int)strtol(newval, &endptr,
 					    0);
-					if (endptr == newval || *endptr != '\0')
-						errx(1, "invalid integer '%s'",
-						    (char *)newval);
+					if (endptr == newval || *endptr != '\0') {
+						warnx("invalid integer '%s'%s",
+						    (char *)newval, line);
+						return (1);
+					}
 				}
 				newval = &intval;
 				newsize = sizeof(intval);
@@ -254,25 +326,31 @@
 				break;
 			case CTLTYPE_UINT:
 				uintval = (int) strtoul(newval, &endptr, 0);
-				if (endptr == newval || *endptr != '\0')
-					errx(1, "invalid unsigned integer '%s'",
-					    (char *)newval);
+				if (endptr == newval || *endptr != '\0') {
+					warnx("invalid unsigned integer '%s'%s",
+					    (char *)newval, line);
+					return (1);
+				}
 				newval = &uintval;
 				newsize = sizeof(uintval);
 				break;
 			case CTLTYPE_LONG:
 				longval = strtol(newval, &endptr, 0);
-				if (endptr == newval || *endptr != '\0')
-					errx(1, "invalid long integer '%s'",
-					    (char *)newval);
+				if (endptr == newval || *endptr != '\0') {
+					warnx("invalid long integer '%s'%s",
+					    (char *)newval, line);
+					return (1);
+				}
 				newval = &longval;
 				newsize = sizeof(longval);
 				break;
 			case CTLTYPE_ULONG:
 				ulongval = strtoul(newval, &endptr, 0);
-				if (endptr == newval || *endptr != '\0')
-					errx(1, "invalid unsigned long integer"
-					    " '%s'", (char *)newval);
+				if (endptr == newval || *endptr != '\0') {
+					warnx("invalid unsigned long integer"
+					    " '%s'%s", (char *)newval, line);
+					return (1);
+				}
 				newval = &ulongval;
 				newsize = sizeof(ulongval);
 				break;
@@ -280,17 +358,21 @@
 				break;
 			case CTLTYPE_S64:
 				i64val = strtoimax(newval, &endptr, 0);
-				if (endptr == newval || *endptr != '\0')
-					errx(1, "invalid int64_t '%s'",
-					    (char *)newval);
+				if (endptr == newval || *endptr != '\0') {
+					warnx("invalid int64_t '%s'%s",
+					    (char *)newval, line);
+					return (1);
+				}
 				newval = &i64val;
 				newsize = sizeof(i64val);
 				break;
 			case CTLTYPE_U64:
 				u64val = strtoumax(newval, &endptr, 0);
-				if (endptr == newval || *endptr != '\0')
-					errx(1, "invalid uint64_t '%s'",
-					    (char *)newval);
+				if (endptr == newval || *endptr != '\0') {
+					warnx("invalid uint64_t '%s'%s",
+					    (char *)newval, line);
+					return (1);
+				}
 				newval = &u64val;
 				newsize = sizeof(u64val);
 				break;
@@ -297,9 +379,10 @@
 			case CTLTYPE_OPAQUE:
 				/* FALLTHROUGH */
 			default:
-				errx(1, "oid '%s' is type %d,"
-					" cannot set that", bufp,
-					kind & CTLTYPE);
+				warnx("oid '%s' is type %d,"
+					" cannot set that%s", bufp,
+					kind & CTLTYPE, line);
+				return (1);
 		}
 
 		i = show_var(mib, len);
@@ -308,18 +391,20 @@
 				putchar('\n');
 			switch (errno) {
 			case EOPNOTSUPP:
-				errx(1, "%s: value is not available",
-					string);
+				warnx("%s: value is not available%s",
+					string, line);
+				return (1);
 			case ENOTDIR:
-				errx(1, "%s: specification is incomplete",
-					string);
+				warnx("%s: specification is incomplete%s",
+					string, line);
+				return (1);
 			case ENOMEM:
-				errx(1, "%s: type is unknown to this program",
-					string);
+				warnx("%s: type is unknown to this program%s",
+					string, line);
+				return (1);
 			default:
-				warn("%s", string);
-				warncount++;
-				return;
+				warn("%s%s", string, line);
+				return (1);
 			}
 		}
 		if (!bflag)
@@ -331,17 +416,69 @@
 			putchar('\n');
 		nflag = i;
 	}
+
+	return (0);
 }
 
+static int
+parsefile(const char *filename)
+{
+	FILE *file;
+	char line[BUFSIZ], *p, *pq, *pdq;
+	int warncount = 0, lineno = 0;
+
+	file = fopen(filename, "r");
+	if (file == NULL)
+		err(EX_NOINPUT, "%s", filename);
+	while (fgets(line, sizeof(line), file) != NULL) {
+		lineno++;
+		p = line;
+		pq = strchr(line, '\'');
+		pdq = strchr(line, '\"');
+		/* Replace the first # with \0. */
+		while((p = strchr(p, '#')) != NULL) {
+			if (pq != NULL && p > pq) {
+				if ((p = strchr(pq+1, '\'')) != NULL)
+					*(++p) = '\0';
+				break;
+			} else if (pdq != NULL && p > pdq) {
+				if ((p = strchr(pdq+1, '\"')) != NULL)
+					*(++p) = '\0';
+				break;
+			} else if (p == line || *(p-1) != '\\') {
+				*p = '\0';
+				break;
+			}
+			p++;
+		}
+		/* Trim spaces */
+		p = line + strlen(line) - 1;
+		while (p >= line && isspace((int)*p)) {
+			*p = '\0';
+			p--;
+		}
+		p = line;
+		while (isspace((int)*p))
+			p++;
+		if (*p == '\0')
+			continue;
+		else
+			warncount += parse(p, lineno);
+	}
+	fclose(file);
+
+	return (warncount);
+}
+
 /* These functions will dump out various interesting structures. */
 
 static int
-S_clockinfo(int l2, void *p)
+S_clockinfo(size_t l2, void *p)
 {
 	struct clockinfo *ci = (struct clockinfo*)p;
 
 	if (l2 != sizeof(*ci)) {
-		warnx("S_clockinfo %d != %zu", l2, sizeof(*ci));
+		warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci));
 		return (1);
 	}
 	printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
@@ -351,12 +488,12 @@
 }
 
 static int
-S_loadavg(int l2, void *p)
+S_loadavg(size_t l2, void *p)
 {
 	struct loadavg *tv = (struct loadavg*)p;
 
 	if (l2 != sizeof(*tv)) {
-		warnx("S_loadavg %d != %zu", l2, sizeof(*tv));
+		warnx("S_loadavg %zu != %zu", l2, sizeof(*tv));
 		return (1);
 	}
 	printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
@@ -367,7 +504,7 @@
 }
 
 static int
-S_timeval(int l2, void *p)
+S_timeval(size_t l2, void *p)
 {
 	struct timeval *tv = (struct timeval*)p;
 	time_t tv_sec;
@@ -374,7 +511,7 @@
 	char *p1, *p2;
 
 	if (l2 != sizeof(*tv)) {
-		warnx("S_timeval %d != %zu", l2, sizeof(*tv));
+		warnx("S_timeval %zu != %zu", l2, sizeof(*tv));
 		return (1);
 	}
 	printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
@@ -391,13 +528,13 @@
 }
 
 static int
-S_vmtotal(int l2, void *p)
+S_vmtotal(size_t l2, void *p)
 {
 	struct vmtotal *v = (struct vmtotal *)p;
 	int pageKilo = getpagesize() / 1024;
 
 	if (l2 != sizeof(*v)) {
-		warnx("S_vmtotal %d != %zu", l2, sizeof(*v));
+		warnx("S_vmtotal %zu != %zu", l2, sizeof(*v));
 		return (1);
 	}
 
@@ -410,20 +547,126 @@
 	    "%hd Sleep: %hd)\n",
 	    v->t_rq, v->t_dw, v->t_pw, v->t_sl);
 	printf(
-	    "Virtual Memory:\t\t(Total: %dK Active: %dK)\n",
-	    v->t_vm * pageKilo, v->t_avm * pageKilo);
-	printf("Real Memory:\t\t(Total: %dK Active: %dK)\n",
-	    v->t_rm * pageKilo, v->t_arm * pageKilo);
-	printf("Shared Virtual Memory:\t(Total: %dK Active: %dK)\n",
-	    v->t_vmshr * pageKilo, v->t_avmshr * pageKilo);
-	printf("Shared Real Memory:\t(Total: %dK Active: %dK)\n",
-	    v->t_rmshr * pageKilo, v->t_armshr * pageKilo);
-	printf("Free Memory:\t%dK\n", v->t_free * pageKilo);
+	    "Virtual Memory:\t\t(Total: %jdK Active: %jdK)\n",
+	    (intmax_t)v->t_vm * pageKilo, (intmax_t)v->t_avm * pageKilo);
+	printf("Real Memory:\t\t(Total: %jdK Active: %jdK)\n",
+	    (intmax_t)v->t_rm * pageKilo, (intmax_t)v->t_arm * pageKilo);
+	printf("Shared Virtual Memory:\t(Total: %jdK Active: %jdK)\n",
+	    (intmax_t)v->t_vmshr * pageKilo, (intmax_t)v->t_avmshr * pageKilo);
+	printf("Shared Real Memory:\t(Total: %jdK Active: %jdK)\n",
+	    (intmax_t)v->t_rmshr * pageKilo, (intmax_t)v->t_armshr * pageKilo);
+	printf("Free Memory:\t%jdK", (intmax_t)v->t_free * pageKilo);
 
 	return (0);
 }
 
+#ifdef __amd64__
+#define efi_next_descriptor(ptr, size) \
+	((struct efi_md *)(((uint8_t *) ptr) + size))
+
 static int
+S_efi_map(size_t l2, void *p)
+{
+	struct efi_map_header *efihdr;
+	struct efi_md *map;
+	const char *type;
+	size_t efisz;
+	int ndesc, i;
+
+	static const char *types[] = {
+		"Reserved",
+		"LoaderCode",
+		"LoaderData",
+		"BootServicesCode",
+		"BootServicesData",
+		"RuntimeServicesCode",
+		"RuntimeServicesData",
+		"ConventionalMemory",
+		"UnusableMemory",
+		"ACPIReclaimMemory",
+		"ACPIMemoryNVS",
+		"MemoryMappedIO",
+		"MemoryMappedIOPortSpace",
+		"PalCode"
+	};
+
+	/*
+	 * Memory map data provided by UEFI via the GetMemoryMap
+	 * Boot Services API.
+	 */
+	if (l2 < sizeof(*efihdr)) {
+		warnx("S_efi_map length less than header");
+		return (1);
+	}
+	efihdr = p;
+	efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
+	map = (struct efi_md *)((uint8_t *)efihdr + efisz); 
+
+	if (efihdr->descriptor_size == 0)
+		return (0);
+	if (l2 != efisz + efihdr->memory_size) {
+		warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz +
+		    efihdr->memory_size);
+		return (1);
+	}		
+	ndesc = efihdr->memory_size / efihdr->descriptor_size;
+
+	printf("\n%23s %12s %12s %8s %4s",
+	    "Type", "Physical", "Virtual", "#Pages", "Attr");
+
+	for (i = 0; i < ndesc; i++,
+	    map = efi_next_descriptor(map, efihdr->descriptor_size)) {
+		if (map->md_type <= EFI_MD_TYPE_PALCODE)
+			type = types[map->md_type];
+		else
+			type = "<INVALID>";
+		printf("\n%23s %012lx %12p %08lx ", type, map->md_phys,
+		    map->md_virt, map->md_pages);
+		if (map->md_attr & EFI_MD_ATTR_UC)
+			printf("UC ");
+		if (map->md_attr & EFI_MD_ATTR_WC)
+			printf("WC ");
+		if (map->md_attr & EFI_MD_ATTR_WT)
+			printf("WT ");
+		if (map->md_attr & EFI_MD_ATTR_WB)
+			printf("WB ");
+		if (map->md_attr & EFI_MD_ATTR_UCE)
+			printf("UCE ");
+		if (map->md_attr & EFI_MD_ATTR_WP)
+			printf("WP ");
+		if (map->md_attr & EFI_MD_ATTR_RP)
+			printf("RP ");
+		if (map->md_attr & EFI_MD_ATTR_XP)
+			printf("XP ");
+		if (map->md_attr & EFI_MD_ATTR_RT)
+			printf("RUNTIME");
+	}
+	return (0);
+}
+#endif
+
+#if defined(__amd64__) || defined(__i386__)
+static int
+S_bios_smap_xattr(size_t l2, void *p)
+{
+	struct bios_smap_xattr *smap, *end;
+
+	if (l2 % sizeof(*smap) != 0) {
+		warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2,
+		    sizeof(*smap));
+		return (1);
+	}
+
+	end = (struct bios_smap_xattr *)((char *)p + l2);
+	for (smap = p; smap < end; smap++)
+		printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx",
+		    smap->type, smap->xattr, (uintmax_t)smap->base,
+		    (uintmax_t)smap->length);
+	return (0);
+}
+#endif
+
+static int
 set_IK(const char *str, int *val)
 {
 	float temp;
@@ -460,7 +703,7 @@
  */
 
 static int
-name2oid(char *name, int *oidp)
+name2oid(const char *name, int *oidp)
 {
 	int oid[2];
 	int i;
@@ -528,7 +771,7 @@
 show_var(int *oid, int nlen)
 {
 	u_char buf[BUFSIZ], *val, *oval, *p;
-	char name[BUFSIZ], *fmt;
+	char name[BUFSIZ], fmt[BUFSIZ];
 	const char *sep, *sep1;
 	int qoid[CTL_MAXNAME+2];
 	uintmax_t umv;
@@ -537,12 +780,13 @@
 	size_t intlen;
 	size_t j, len;
 	u_int kind;
-	int (*func)(int, void *);
+	int (*func)(size_t, void *);
 
 	/* Silence GCC. */
 	umv = mv = intlen = 0;
 
 	bzero(buf, BUFSIZ);
+	bzero(fmt, BUFSIZ);
 	bzero(name, BUFSIZ);
 	qoid[0] = 0;
 	memcpy(qoid + 2, oid, nlen * sizeof(int));
@@ -553,6 +797,15 @@
 	if (i || !j)
 		err(1, "sysctl name %d %zu %d", i, j, errno);
 
+	oidfmt(oid, nlen, fmt, &kind);
+	/* if Wflag then only list sysctls that are writeable and not stats. */
+	if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0))
+		return 1;
+
+	/* if Tflag then only list sysctls that are tuneables. */
+	if (Tflag && (kind & CTLFLAG_TUN) == 0)
+		return 1;
+
 	if (Nflag) {
 		printf("%s", name);
 		return (0);
@@ -582,9 +835,10 @@
 		warnx("malloc failed");
 		return (1);
 	}
+	ctltype = (kind & CTLTYPE);
 	len = j;
 	i = sysctl(oid, nlen, val, &len, 0, 0);
-	if (i || !len) {
+	if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) {
 		free(oval);
 		return (1);
 	}
@@ -595,10 +849,7 @@
 		return (0);
 	}
 	val[len] = '\0';
-	fmt = buf;
-	oidfmt(oid, nlen, fmt, &kind);
 	p = val;
-	ctltype = (kind & CTLTYPE);
 	sign = ctl_sign[ctltype];
 	intlen = ctl_size[ctltype];
 
@@ -667,6 +918,14 @@
 			func = S_loadavg;
 		else if (strcmp(fmt, "S,vmtotal") == 0)
 			func = S_vmtotal;
+#ifdef __amd64__
+		else if (strcmp(fmt, "S,efi_map_header") == 0)
+			func = S_efi_map;
+#endif
+#if defined(__amd64__) || defined(__i386__)
+		else if (strcmp(fmt, "S,bios_smap_xattr") == 0)
+			func = S_bios_smap_xattr;
+#endif
 		else
 			func = NULL;
 		if (func) {



More information about the Midnightbsd-cvs mailing list