svn commit: r278654 - head/sbin/sysctl

John-Mark Gurney jmg at FreeBSD.org
Fri Feb 13 01:20:39 UTC 2015


Author: jmg
Date: Fri Feb 13 01:20:37 2015
New Revision: 278654
URL: https://svnweb.freebsd.org/changeset/base/278654

Log:
  add support for specifying an initial buffer size when fetching a
  sysctl... This is useful for kern.arandom which (without -B) will
  happily return 0 bytes, which isn't too useful or random...
  
  fix spelling (thanks igor!) of settable while I'm here...

Modified:
  head/sbin/sysctl/sysctl.8
  head/sbin/sysctl/sysctl.c

Modified: head/sbin/sysctl/sysctl.8
==============================================================================
--- head/sbin/sysctl/sysctl.8	Fri Feb 13 01:07:12 2015	(r278653)
+++ head/sbin/sysctl/sysctl.8	Fri Feb 13 01:20:37 2015	(r278654)
@@ -28,7 +28,7 @@
 .\"	From: @(#)sysctl.8	8.1 (Berkeley) 6/6/93
 .\" $FreeBSD$
 .\"
-.Dd December 13, 2012
+.Dd February 12, 2015
 .Dt SYSCTL 8
 .Os
 .Sh NAME
@@ -37,11 +37,13 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl bdehiNnoRTqx
+.Op Fl B Ar bufsize
 .Op Fl f Ar filename
 .Ar name Ns Op = Ns Ar value
 .Ar ...
 .Nm
 .Op Fl bdehNnoRTqx
+.Op Fl B Ar bufsize
 .Fl a
 .Sh DESCRIPTION
 The
@@ -68,6 +70,15 @@ the command line.
 Force the value of the variable(s) to be output in raw, binary format.
 No names are printed and no terminating newlines are output.
 This is mostly useful with a single variable.
+.It Fl B Ar bufsize
+Set the buffer size to read from the
+.Nm
+to
+.Ar bufsize .
+This is necessary for a
+.Nm
+that has variable length, and the probe value of 0 is a valid length, such as
+.Va kern.arandom .
 .It Fl d
 Print the description of the variable instead of its value.
 .It Fl e
@@ -128,7 +139,7 @@ Suppress some warnings generated by
 .Nm
 to standard error.
 .It Fl T
-Display only variables that are setable via loader (CTLFLAG_TUN).
+Display only variables that are settable via loader (CTLFLAG_TUN).
 .It Fl W
 Display only writable variables that are not statistical.
 Useful for determining the set of runtime tunable sysctls.

Modified: head/sbin/sysctl/sysctl.c
==============================================================================
--- head/sbin/sysctl/sysctl.c	Fri Feb 13 01:07:12 2015	(r278653)
+++ head/sbin/sysctl/sysctl.c	Fri Feb 13 01:20:37 2015	(r278654)
@@ -71,7 +71,7 @@ static const char rcsid[] =
 
 static const char *conffile;
 
-static int	aflag, bflag, dflag, eflag, hflag, iflag;
+static int	aflag, bflag, Bflag, dflag, eflag, hflag, iflag;
 static int	Nflag, nflag, oflag, qflag, Tflag, Wflag, xflag;
 
 static int	oidfmt(int *, int, char *, u_int *);
@@ -112,8 +112,8 @@ usage(void)
 {
 
 	(void)fprintf(stderr, "%s\n%s\n",
-	    "usage: sysctl [-bdehiNnoqTWx] [-f filename] name[=value] ...",
-	    "       sysctl [-bdehNnoqTWx] -a");
+	    "usage: sysctl [-bdehiNnoqTWx] [ -B <bufsize> ] [-f filename] name[=value] ...",
+	    "       sysctl [-bdehNnoqTWx] [ -B <bufsize> ] -a");
 	exit(1);
 }
 
@@ -127,7 +127,7 @@ main(int argc, char **argv)
 	setbuf(stdout,0);
 	setbuf(stderr,0);
 
-	while ((ch = getopt(argc, argv, "Aabdef:hiNnoqTwWxX")) != -1) {
+	while ((ch = getopt(argc, argv, "AabB:def:hiNnoqTwWxX")) != -1) {
 		switch (ch) {
 		case 'A':
 			/* compatibility */
@@ -139,6 +139,9 @@ main(int argc, char **argv)
 		case 'b':
 			bflag = 1;
 			break;
+		case 'B':
+			Bflag = strtol(optarg, NULL, 0);
+			break;
 		case 'd':
 			dflag = 1;
 			break;
@@ -222,7 +225,7 @@ parse(const char *string, int lineno)
 	unsigned int uintval;
 	long longval;
 	unsigned long ulongval;
-	size_t newsize = 0;
+	size_t newsize = Bflag;
 	int64_t i64val;
 	uint64_t u64val;
 	int mib[CTL_MAXNAME];
@@ -815,9 +818,13 @@ show_var(int *oid, int nlen)
 		return (0);
 	}
 	/* find an estimate of how much we need for this var */
-	j = 0;
-	i = sysctl(oid, nlen, 0, &j, 0, 0);
-	j += j; /* we want to be sure :-) */
+	if (Bflag)
+		j = Bflag;
+	else {
+		j = 0;
+		i = sysctl(oid, nlen, 0, &j, 0, 0);
+		j += j; /* we want to be sure :-) */
+	}
 
 	val = oval = malloc(j + 1);
 	if (val == NULL) {


More information about the svn-src-head mailing list