Subject: Re: sysctlfs [was: Re: core file name format: diffs]
To: Manuel Bouyer <bouyer@antioche.lip6.fr>
From: Brian C. Grayson <bgrayson@marvin.ece.utexas.edu>
List: tech-kern
Date: 09/22/1999 02:37:37
On Mon, Sep 20, 1999 at 06:07:21PM +0200, Manuel Bouyer wrote:
> On Mon, Sep 20, 1999 at 05:23:58PM +0200, Jaromir Dolecek wrote:
> > > On Mon, Sep 20, 1999 at 05:07:00PM +0200, Hubert Feyrer wrote:
> > > > 
> > > > I'd really love to see a filesystem-frontend to sysctl.
> > > > (Just mumbling to myself, mostly. I know that I got the source, etc. :-)
> > 
> > Ah, that means that the info will be on three places then ? I.E.
> > the code in kernel, symbolic names in sysctl and the same symbolic names
> > in kernfs ?
> 
> Actually these names are defined in sys/sysctl.h and other includes files.
> Maybe the solution would be to handle sysctlfs in userland then (don't we have
> some mechanisms to handle userland-filesystems already ?).

  This could probably be done with a rfilter/wfilter portal
mount (shameless plug!).  At least the readable stuff.  This
probably isn't everything you want, but it might tide you over
for the moment.  And it avoids a lot of duplicated stuff, because
it just calls sysctl (the program) to do all its dirty work --
code reuse!  And it only took me around 10 minutes to write this
up and debug it.

  A writeable variant is left as an exercise to the reader. 

  Here's an example:

/etc/portal.conf:
# Use a simple sysctl readable interface.
sysctl/ rfilter sysctl/ /tmp/sysctl.sh %s

/tmp/sysctl.sh:  (Make sure you make this chmod a+x, otherwise it
		  will silently fail.)
#!/bin/sh
##  Fast hack at a sysctl filesystem.  The path can be either in 
##  dot-style (kern.mbuf.msize) or in slash-style (kern/mbuf/msize).
##  Partial paths (such as kern.mbuf) are passed as-is to sysctl,
##  which will output all subitems that have that prefix.
##  This is just a proof-of-concept, by Brian Grayson
##  (bgrayson@netbsd.org), Sep 1999.
for path in $*; do
  ##  First, change any slashes into dots.
  path=`echo $path | tr '/' '.'`
  ##  Now invoke sysctl, and post-process the output to make it 
  ##  friendlier.  In particular:
  ##  1.  Remove the common leading prefix.
  ##  2.  Remove a now-leading ".", if any.
  ##  3.  If we are left with " = <val>", strip out the " = " also.
  sysctl $path | sed -e "s/$path//;s/^\.//;s/^ = //"
done

Example usage:
% mount_portal /etc/portal.conf /p

% cat /p/sysctl/kern/mbuf/msize
128

% cat /p/sysctl/kern.mbuf.msize
128

% cat /p/sysctl/kern/mbuf
msize = 128
mclbytes = 2048
nmbclusters = 512
mblowat = 16
mcllowat = 8

% cat /p/sysctl/kern.mbuf
msize = 128
mclbytes = 2048
nmbclusters = 512
mblowat = 16
mcllowat = 8

% sysctl kern.mbuf
kern.mbuf.msize = 128
kern.mbuf.mclbytes = 2048
kern.mbuf.nmbclusters = 512
kern.mbuf.mblowat = 16
kern.mbuf.mcllowat = 8

##  Notice that the script automatically removes the leading
##  portion that matches the path.  This "smart" handling may not
##  be desired.  But I like it.

  Enjoy!

  Brian