Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 23 Jul 2001 14:06:20 +0300
From:      Peter Pentchev <roam@orbitel.bg>
To:        arch@FreeBSD.org
Cc:        audit@FreeBSD.org
Subject:   Re: sysctl(8) enhancement: display sysctl MIB's
Message-ID:  <20010723140620.A52175@ringworld.oblivion.bg>
In-Reply-To: <20010723104201.855893E2F@bazooka.unixfreak.org>; from dima@unixfreak.org on Mon, Jul 23, 2001 at 03:42:01AM -0700
References:  <20010723004034.I882@ringworld.oblivion.bg> <20010723104201.855893E2F@bazooka.unixfreak.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Jul 23, 2001 at 03:42:01AM -0700, Dima Dorfman wrote:
[snip]
> 
> I don't think checking printf()'s return value does anything other
> than add more lines to the program.  Most (none?) of the other
> programs don't do it.  Besides, what can you do when it fails?
> Remember, err(3) calls it too... (well, it calls fprintf to write to
> stderr, but you get the idea)
[snip]
> 
> If you're going to make show_varname() return something to indicate an
> error (your code doesn't do that right now, but the return type is
> 'int', and the comment says that it might fail in the future), you
> should check the return value when you call it.  Actually, I think
> making the return type 'void' makes more sense: this is a support
> routine (as opposed to an interface to something), and if it detects
> an error, calling one of the err(3) functions would be cleaner than
> doing error checking all over the place.
> 
> 
> Other than that, I think it's a good idea.

OK, a revised patch follows..

G'luck,
Peter

-- 
If the meanings of 'true' and 'false' were switched, then this sentence wouldn't be false.

Index: src/sbin/sysctl/sysctl.8
===================================================================
RCS file: /home/ncvs/src/sbin/sysctl/sysctl.8,v
retrieving revision 1.36
diff -u -r1.36 sysctl.8
--- src/sbin/sysctl/sysctl.8	2001/07/13 09:09:48	1.36
+++ src/sbin/sysctl/sysctl.8	2001/07/23 11:05:00
@@ -40,7 +40,7 @@
 .Nd get or set kernel state
 .Sh SYNOPSIS
 .Nm
-.Op Fl bNnox
+.Op Fl bmNnox
 .Ar name Ns Op = Ns Ar value
 .Ar ...
 .Nm
@@ -71,6 +71,11 @@
 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 m
+Specify that the MIB numerical value should be printed before the name.
+This flag only takes effect if
+.Fl n
+is not specified.
 .It Fl N
 Show only variable names, not their values.
 This is particularly useful with shells that offer programmable
Index: src/sbin/sysctl/sysctl.c
===================================================================
RCS file: /home/ncvs/src/sbin/sysctl/sysctl.c,v
retrieving revision 1.37
diff -u -r1.37 sysctl.c
--- src/sbin/sysctl/sysctl.c	2001/06/18 21:06:24	1.37
+++ src/sbin/sysctl/sysctl.c	2001/07/23 11:05:00
@@ -58,10 +58,11 @@
 #include <string.h>
 #include <unistd.h>
 
-static int	aflag, bflag, Nflag, nflag, oflag, xflag;
+static int	aflag, bflag, mflag, Nflag, nflag, oflag, xflag;
 
 static int	oidfmt(int *, int, char *, u_int *);
 static void	parse(char *);
+static void	show_varname(const char *, const int *, int);
 static int	show_var(int *, int);
 static int	sysctl_all (int *oid, int len);
 static int	name2oid(char *, int *);
@@ -71,8 +72,8 @@
 {
 
 	(void)fprintf(stderr, "%s\n%s\n",
-	    "usage: sysctl [-bNnox] variable[=value] ...",
-	    "       sysctl [-bNnox] -a");
+	    "usage: sysctl [-bmNnox] variable[=value] ...",
+	    "       sysctl [-bmNnox] -a");
 	exit(1);
 }
 
@@ -83,7 +84,7 @@
 	setbuf(stdout,0);
 	setbuf(stderr,0);
 
-	while ((ch = getopt(argc, argv, "AabNnowxX")) != -1) {
+	while ((ch = getopt(argc, argv, "AabmNnowxX")) != -1) {
 		switch (ch) {
 		case 'A':
 			/* compatibility */
@@ -95,6 +96,9 @@
 		case 'b':
 			bflag = 1;
 			break;
+		case 'm':
+			mflag = 1;
+			break;
 		case 'N':
 			Nflag = 1;
 			break;
@@ -369,6 +373,30 @@
 }
 
 /*
+ * This formats and outputs the name of one variable.
+ * If the -m command-line flag was specified, the MIB value
+ * is also printed out.
+ */
+
+static void
+show_varname(const char *name, const int *oid, int len)
+{
+	int i;
+
+	if (nflag)
+		return;
+
+	if (mflag) {
+		for (i = 0; i < len-1; i++)
+			printf("%X.", oid[i]);
+		printf("%X ", oid[i]);
+	}
+
+	printf("%s: ", name);
+	return;
+}
+
+/*
  * This formats and outputs the value of one variable
  *
  * Returns zero if anything was actually output.
@@ -397,7 +425,7 @@
 		err(1, "sysctl name %d %d %d", i, j, errno);
 
 	if (Nflag) {
-		printf("%s", name);
+		show_varname(name, oid, nlen);
 		return (0);
 	}
 
@@ -430,14 +458,12 @@
 	p = val;
 	switch (*fmt) {
 	case 'A':
-		if (!nflag)
-			printf("%s: ", name);
+		show_varname(name, oid, nlen);
 		printf("%s", p);
 		return (0);
 		
 	case 'I':
-		if (!nflag)
-			printf("%s: ", name);
+		show_varname(name, oid, nlen);
 		fmt++;
 		val = "";
 		while (len >= sizeof(int)) {
@@ -452,8 +478,7 @@
 		return (0);
 
 	case 'L':
-		if (!nflag)
-			printf("%s: ", name);
+		show_varname(name, oid, nlen);
 		fmt++;
 		val = "";
 		while (len >= sizeof(long)) {
@@ -468,8 +493,7 @@
 		return (0);
 
 	case 'P':
-		if (!nflag)
-			printf("%s: ", name);
+		show_varname(name, oid, nlen);
 		printf("%p", *(void **)p);
 		return (0);
 
@@ -487,16 +511,14 @@
 		else
 			func = NULL;
 		if (func) {
-			if (!nflag)
-				printf("%s: ", name);
+			show_varname(name, oid, nlen);
 			return ((*func)(len, p));
 		}
 		/* FALL THROUGH */
 	default:
 		if (!oflag && !xflag)
 			return (1);
-		if (!nflag)
-			printf("%s: ", name);
+		show_varname(name, oid, nlen);
 		printf("Format:%s Length:%d Dump:0x", fmt, len);
 		while (len-- && (xflag || p < val + 16))
 			printf("%02x", *p++);

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-audit" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010723140620.A52175>