Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 31 Oct 2001 13:22:47 +0200
From:      Peter Pentchev <roam@ringlet.net>
To:        arch@FreeBSD.org
Cc:        audit@FreeBSD.org
Subject:   Re: sysctl(8) enhancement: display sysctl MIB's
Message-ID:  <20011031132247.B13366@straylight.oblivion.bg>
In-Reply-To: <20010723140620.A52175@ringworld.oblivion.bg>; from roam@orbitel.bg on Mon, Jul 23, 2001 at 02:06:20PM %2B0300
References:  <20010723004034.I882@ringworld.oblivion.bg> <20010723104201.855893E2F@bazooka.unixfreak.org> <20010723140620.A52175@ringworld.oblivion.bg>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi,

Here's the patch to the updated sysctl sources after Anton Berezin
added the '-e' option.

For those who have missed the previous discussion, this patch adds
a new '-m' option which displays the MIB's before the sysctl names.
E.g.

[roam@straylight:v4 ~/fbsd/r/src/sbin/sysctl]$ ./sysctl net.local
net.local.stream.sendspace: 8192
net.local.stream.recvspace: 8192
net.local.dgram.maxdgram: 2048
net.local.dgram.recvspace: 4096
net.local.inflight: 0
[roam@straylight:v4 ~/fbsd/r/src/sbin/sysctl]$ ./sysctl -m net.local
4.1.1.155 net.local.stream.sendspace: 8192
4.1.1.156 net.local.stream.recvspace: 8192
4.1.2.157 net.local.dgram.maxdgram: 2048
4.1.2.158 net.local.dgram.recvspace: 4096
4.1.159 net.local.inflight: 0
[roam@straylight:v4 ~/fbsd/r/src/sbin/sysctl]$

G'luck,
Peter

-- 
What would this sentence be like if pi were 3?

Index: src/sbin/sysctl/sysctl.8
===================================================================
RCS file: /home/ncvs/src/sbin/sysctl/sysctl.8,v
retrieving revision 1.39
diff -u -r1.39 sysctl.8
--- src/sbin/sysctl/sysctl.8	30 Oct 2001 20:15:32 -0000	1.39
+++ src/sbin/sysctl/sysctl.8	31 Oct 2001 09:40:46 -0000
@@ -40,11 +40,11 @@
 .Nd get or set kernel state
 .Sh SYNOPSIS
 .Nm
-.Op Fl beNnox
+.Op Fl bemNnox
 .Ar name Ns Op = Ns Ar value
 .Ar ...
 .Nm
-.Op Fl beNnox
+.Op Fl bemNnox
 .Fl a
 .Sh DESCRIPTION
 The
@@ -81,6 +81,11 @@
 or
 .Fl n
 is specified, or a variable is being set.
+.It Fl m
+Specify that the MIB numerical value should be printed before the name.
+This option is ignored if
+.Fl n
+is 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.38
diff -u -r1.38 sysctl.c
--- src/sbin/sysctl/sysctl.c	30 Oct 2001 20:15:32 -0000	1.38
+++ src/sbin/sysctl/sysctl.c	31 Oct 2001 09:51:47 -0000
@@ -58,10 +58,11 @@
 #include <string.h>
 #include <unistd.h>
 
-static int	aflag, bflag, eflag, Nflag, nflag, oflag, xflag;
+static int	aflag, bflag, eflag, mflag, Nflag, nflag, oflag, xflag;
 
 static int	oidfmt(int *, int, char *, u_int *);
 static void	parse(char *);
+static int	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 [-beNnox] variable[=value] ...",
-	    "       sysctl [-beNnox] -a");
+	    "usage: sysctl [-bemNnox] variable[=value] ...",
+	    "       sysctl [-bemNnox] -a");
 	exit(1);
 }
 
@@ -83,7 +84,7 @@
 	setbuf(stdout,0);
 	setbuf(stderr,0);
 
-	while ((ch = getopt(argc, argv, "AabeNnowxX")) != -1) {
+	while ((ch = getopt(argc, argv, "AabemNnowxX")) != -1) {
 		switch (ch) {
 		case 'A':
 			/* compatibility */
@@ -98,6 +99,9 @@
 		case 'e':
 			eflag = 1;
 			break;
+		case 'm':
+			mflag = 1;
+			break;
 		case 'N':
 			Nflag = 1;
 			break;
@@ -372,6 +376,29 @@
 }
 
 /*
+ * This formats and outputs the name of one variable.
+ * If the -m command-line flag was specified, the MIB value
+ * is also output.
+ */
+
+static int
+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%s", name, eflag? "=": ": ");
+}
+
+/*
  * This formats and outputs the value of one variable
  *
  * Returns zero if anything was actually output.
@@ -383,7 +410,7 @@
 show_var(int *oid, int nlen)
 {
 	u_char buf[BUFSIZ], *val, *p;
-	char name[BUFSIZ], *fmt, *sep;
+	char name[BUFSIZ], *fmt;
 	int qoid[CTL_MAXNAME+2];
 	int i;
 	size_t j, len;
@@ -400,15 +427,10 @@
 		err(1, "sysctl name %d %d %d", i, j, errno);
 
 	if (Nflag) {
-		printf("%s", name);
+		show_varname(name, oid, nlen);
 		return (0);
 	}
 
-	if (eflag)
-		sep = "=";
-	else
-		sep = ": ";
-
 	/* find an estimate of how much we need for this var */
 	j = 0;
 	i = sysctl(oid, nlen, 0, &j, 0, 0);
@@ -438,14 +460,12 @@
 	p = val;
 	switch (*fmt) {
 	case 'A':
-		if (!nflag)
-			printf("%s%s", name, sep);
+		show_varname(name, oid, nlen);
 		printf("%s", p);
 		return (0);
 		
 	case 'I':
-		if (!nflag)
-			printf("%s%s", name, sep);
+		show_varname(name, oid, nlen);
 		fmt++;
 		val = "";
 		while (len >= sizeof(int)) {
@@ -460,8 +480,7 @@
 		return (0);
 
 	case 'L':
-		if (!nflag)
-			printf("%s%s", name, sep);
+		show_varname(name, oid, nlen);
 		fmt++;
 		val = "";
 		while (len >= sizeof(long)) {
@@ -476,8 +495,7 @@
 		return (0);
 
 	case 'P':
-		if (!nflag)
-			printf("%s%s", name, sep);
+		show_varname(name, oid, nlen);
 		printf("%p", *(void **)p);
 		return (0);
 
@@ -495,16 +513,14 @@
 		else
 			func = NULL;
 		if (func) {
-			if (!nflag)
-				printf("%s%s", name, sep);
+			show_varname(name, oid, nlen);
 			return ((*func)(len, p));
 		}
 		/* FALL THROUGH */
 	default:
 		if (!oflag && !xflag)
 			return (1);
-		if (!nflag)
-			printf("%s%s", name, sep);
+		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?20011031132247.B13366>