kern/51201: [PATCH] MFC for sysctl descriptions.
Andy Gilligan
andy at evo6.org
Sun Apr 20 17:10:18 PDT 2003
>Number: 51201
>Category: kern
>Synopsis: [PATCH] MFC for sysctl descriptions.
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: freebsd-bugs
>State: open
>Quarter:
>Keywords:
>Date-Required:
>Class: change-request
>Submitter-Id: current-users
>Arrival-Date: Sun Apr 20 17:10:16 PDT 2003
>Closed-Date:
>Last-Modified:
>Originator: Andy Gilligan
>Release: FreeBSD 4.8-STABLE i386
>Organization:
>Environment:
>Description:
FreeBSD 5.0 and later introduce descriptions for various sysctl oids.
This patch brings this functionality to the -STABLE branch.
For example:
root at host# sysctl -d net.inet.tcp.rfc1323
net.inet.tcp.rfc1323: Enable rfc1323 (high performance TCP) extensions
>How-To-Repeat:
sysctl -d <oid>
>Fix:
This patch modifies userland /sbin/sysctl and its man page,
along with small kernel changes to allow descriptions.
--- sysctl-mfc.patch begins here ---
Index: sbin/sysctl/sysctl.8
===================================================================
RCS file: /data/ncvs/src/sbin/sysctl/sysctl.8,v
retrieving revision 1.23.2.15
diff -u -r1.23.2.15 sysctl.8
--- sbin/sysctl/sysctl.8 23 Feb 2003 22:20:40 -0000 1.23.2.15
+++ sbin/sysctl/sysctl.8 19 Apr 2003 14:59:15 -0000
@@ -40,11 +40,11 @@
.Nd get or set kernel state
.Sh SYNOPSIS
.Nm
-.Op Fl beNnox
+.Op Fl bdeNnox
.Ar name Ns Op = Ns Ar value
.Ar ...
.Nm
-.Op Fl beNnox
+.Op Fl bdeNnox
.Fl a
.Sh DESCRIPTION
The
@@ -71,6 +71,8 @@
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 d
+Print the description of the variable instead of its value.
.It Fl e
Separate the name and the value of the variable(s) with
.Ql = .
Index: sbin/sysctl/sysctl.c
===================================================================
RCS file: /data/ncvs/src/sbin/sysctl/sysctl.c,v
retrieving revision 1.25.2.10
diff -u -r1.25.2.10 sysctl.c
--- sbin/sysctl/sysctl.c 22 Jan 2003 00:36:23 -0000 1.25.2.10
+++ sbin/sysctl/sysctl.c 19 Apr 2003 13:29:15 -0000
@@ -63,7 +63,7 @@
#include <string.h>
#include <unistd.h>
-static int aflag, bflag, eflag, Nflag, nflag, oflag, xflag;
+static int aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag;
static int oidfmt(int *, int, char *, u_int *);
static void parse(char *);
@@ -78,8 +78,8 @@
{
(void)fprintf(stderr, "%s\n%s\n",
- "usage: sysctl [-beNnox] variable[=value] ...",
- " sysctl [-beNnox] -a");
+ "usage: sysctl [-bdeNnox] variable[=value] ...",
+ " sysctl [-bdeNnox] -a");
exit(1);
}
@@ -90,7 +90,7 @@
setbuf(stdout,0);
setbuf(stderr,0);
- while ((ch = getopt(argc, argv, "AabeNnowxX")) != -1) {
+ while ((ch = getopt(argc, argv, "AabdeNnowxX")) != -1) {
switch (ch) {
case 'A':
/* compatibility */
@@ -102,6 +102,9 @@
case 'b':
bflag = 1;
break;
+ case 'd':
+ dflag = 1;
+ break;
case 'e':
eflag = 1;
break;
@@ -491,6 +494,15 @@
else
sep = ": ";
+ if (dflag) { /* just print description */
+ qoid[1] = 5;
+ j = sizeof(buf);
+ i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
+ if (!nflag)
+ printf("%s%s", name, sep);
+ printf("%s", buf);
+ return(0);
+ }
/* find an estimate of how much we need for this var */
j = 0;
i = sysctl(oid, nlen, 0, &j, 0, 0);
Index: sys/kern/kern_sysctl.c
===================================================================
RCS file: /data/ncvs/src/sys/kern/kern_sysctl.c,v
retrieving revision 1.92.2.8
diff -u -r1.92.2.8 kern_sysctl.c
--- sys/kern/kern_sysctl.c 9 Sep 2002 19:27:58 -0000 1.92.2.8
+++ sys/kern/kern_sysctl.c 19 Apr 2003 16:37:54 -0000
@@ -303,6 +303,8 @@
}
sysctl_unregister_oid(oidp);
if (del) {
+ if (oidp->oid_descr)
+ free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
free((void *)(uintptr_t)(const void *)oidp->oid_name,
M_SYSCTLOID);
free(oidp, M_SYSCTLOID);
@@ -364,6 +366,12 @@
oidp->oid_arg2 = arg2;
}
oidp->oid_fmt = fmt;
+ if (descr) {
+ int len = strlen(descr) + 1;
+ oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
+ if (oidp->oid_descr)
+ strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
+ };
/* Update the context, if used */
if (clist != NULL)
sysctl_ctx_entry_add(clist, oidp);
@@ -718,6 +726,24 @@
SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
+
+static int
+sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
+{
+ struct sysctl_oid *oid;
+ int error;
+
+ error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
+ if (error)
+ return (error);
+
+ if (!oid->oid_descr)
+ return (ENOENT);
+ error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
+ return (error);
+}
+
+SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
/*
* Default "handler" functions.
Index: sys/sys/sysctl.h
===================================================================
RCS file: /data/ncvs/src/sys/sys/sysctl.h,v
retrieving revision 1.81.2.9
diff -u -r1.81.2.9 sysctl.h
--- sys/sys/sysctl.h 9 Sep 2002 19:27:54 -0000 1.81.2.9
+++ sys/sys/sysctl.h 19 Apr 2003 16:32:13 -0000
@@ -133,6 +133,7 @@
int (*oid_handler)(SYSCTL_HANDLER_ARGS);
const char *oid_fmt;
int oid_refcnt;
+ const char *oid_descr;
};
#define SYSCTL_IN(r, p, l) (r->newfunc)(r, p, l)
@@ -174,7 +175,7 @@
#define SYSCTL_OID(parent, nbr, name, kind, a1, a2, handler, fmt, descr) \
static struct sysctl_oid sysctl__##parent##_##name = { \
&sysctl_##parent##_children, { 0 }, \
- nbr, kind, a1, a2, #name, handler, fmt, 0 }; \
+ nbr, kind, a1, a2, #name, handler, fmt, 0, descr }; \
DATA_SET(sysctl_set, sysctl__##parent##_##name);
#define SYSCTL_ADD_OID(ctx, parent, nbr, name, kind, a1, a2, handler, fmt, descr) \
--- sysctl-mfc.patch ends here ---
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the freebsd-bugs
mailing list