Date: Sat, 19 Apr 2003 15:12:28 +0100 From: "Andy Gilligan" <andy@evo6.org> To: <stable@freebsd.org> Subject: MFC for sysctl descriptions Message-ID: <000801c3067d$b6379340$0a00000a@vx>
next in thread | raw e-mail | index | archive | help
[-- Attachment #1 --] Hello everyone... :) I liked the idea of having descriptions for various sysctl oid's in -CURRENT, so I have created a patch to merge these over to -STABLE. I am unsure as to how much work has already gone into bringing this to -STABLE, but as the descriptions already exist in the source, I am fairly confident that it was planned at some point. All I can really ask is that someone with more knowledge than myself about the sysctl internals have a brief look at my patch to see if it is viable. On my system (4.8-STABLE), everything seems to work, and there have been no adverse effects from it. (That I am aware of at least) The patch is attached to this email, and is also available at: http://evo6.org/sysctl-mfc.patch I hope someone may find this useful. :) Very best regards, Andy Gilligan [-- Attachment #2 --] 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 13:52:00 -0000 @@ -303,6 +303,8 @@ } sysctl_unregister_oid(oidp); if (del) { + if (oidp->descr) + free((void *)(uintptr_t)(const void *)oidp->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->descr = malloc(len, M_SYSCTLOID, M_WAITOK); + if (oidp->descr) + strcpy((char *)(uintptr_t)(const void *)oidp->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->descr) + return (ENOENT); + error = SYSCTL_OUT(req, oid->descr, strlen(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 13:24:38 -0000 @@ -133,6 +133,7 @@ int (*oid_handler)(SYSCTL_HANDLER_ARGS); const char *oid_fmt; int oid_refcnt; + const char *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) \
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?000801c3067d$b6379340$0a00000a>
