Add the function sysctlnametomib to libc. Details on the semantics

and use of this function have been added to the sysctl.3 manual page.
This commit is contained in:
mckusick 2001-01-23 03:40:32 +00:00
parent 209674cbd7
commit aa0f6c1808
3 changed files with 111 additions and 3 deletions

View File

@ -25,7 +25,7 @@ SRCS+= _rand48.c _spinlock_stub.c alarm.c arc4random.c assert.c \
setdomainname.c sethostname.c setjmperr.c setmode.c setproctitle.c \
shmat.c shmctl.c shmdt.c shmget.c siginterrupt.c siglist.c signal.c \
sigsetops.c sleep.c srand48.c stringlist.c strtofflags.c \
sysconf.c sysctl.c sysctlbyname.c \
sysconf.c sysctl.c sysctlbyname.c sysctlnametomib.c \
syslog.c telldir.c termios.c time.c times.c timezone.c ttyname.c \
ttyslot.c ualarm.c uname.c unvis.c usleep.c utime.c valloc.c vis.c \
wait.c wait3.c waitpid.c
@ -115,7 +115,7 @@ MLINKS+=sigsetops.3 sigaddset.3 sigsetops.3 sigdelset.3 \
sigsetops.3 sigismember.3
MLINKS+=stringlist.3 sl_add.3 stringlist.3 sl_find.3 \
stringlist.3 sl_free.3 stringlist.3 sl_init.3
MLINKS+=sysctl.3 sysctlbyname.3
MLINKS+=sysctl.3 sysctlbyname.3 sysctl.3 sysctlnametomib.3
MLINKS+=syslog.3 closelog.3 syslog.3 openlog.3 syslog.3 setlogmask.3 \
syslog.3 vsyslog.3
MLINKS+=tcsendbreak.3 tcdrain.3 tcsendbreak.3 tcflow.3 tcsendbreak.3 tcflush.3

View File

@ -37,7 +37,8 @@
.Os
.Sh NAME
.Nm sysctl ,
.Nm sysctlbyname
.Nm sysctlbyname ,
.Nm sysctlnametomib
.Nd get or set system information
.Sh LIBRARY
.Lb libc
@ -48,6 +49,8 @@
.Fn sysctl "int *name" "u_int namelen" "void *oldp" "size_t *oldlenp" "void *newp" "size_t newlen"
.Ft int
.Fn sysctlbyname "const char *name" "void *oldp" "size_t *oldlenp" "void *newp" "size_t newlen"
.Ft int
.Fn sysctlnametomib "const char *name" "int *mibp" "size_t *sizep"
.Sh DESCRIPTION
The
.Fn sysctl
@ -126,6 +129,56 @@ should be set to NULL and
.Fa newlen
set to 0.
.Pp
The
.Fn sysctlnametomib
function accepts an ASCII representation of the name,
looks up the integer name vector,
and returns the numeric representation in the mib array pointed to by
.Fa mibp .
The number of elements in the mib array is given by the location specified by
.Fa sizep
before the call,
and that location gives the number of entries copied after a successful call.
The resulting
.Fa mib
and
.Fa size
may be used in subsequent
.Fn sysctl
calls to get the data associated with the requested ASCII name.
This interface is intended for use by applications that want to
repeatedly request the same variable (the
.Fn sysctl
function runs in about a third the time as the same request made via the
.Fn sysctlbyname
function).
The
.Fn sysctlbyname
function is also useful for fetching mib prefixes and then adding
a final component.
For example, to fetch process information
for processes with pid's less than 100:
.Bd -literal -offset indent -compact
int i, mib[4];
size_t len;
struct kinfo_proc kp;
/* Fill out the first three components of the mib */
len = 4;
sysctlnametomib("kern.proc.pid", mib, &len);
/* Fetch and print entries for pid's < 100 */
for (i = 0; i < 100; i++) {
mib[3] = i;
len = sizeof(kp);
if (sysctl(mib, 4, &kp, &len, NULL, 0) == -1)
perror("sysctl");
else if (len > 0)
printkproc(&kp);
}
.Ed
.Pp
The top level names are defined with a CTL_ prefix in
.Aq Pa sys/sysctl.h ,
and are as follows.

View File

@ -0,0 +1,55 @@
/*
* Copyright 2001 The FreeBSD Project. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <sys/types.h>
#include <sys/sysctl.h>
/*
* This function uses a presently undocumented interface to the kernel
* to walk the tree and get the type so it can print the value.
* This interface is under work and consideration, and should probably
* be killed with a big axe by the first person who can find the time.
* (be aware though, that the proper interface isn't as obvious as it
* may seem, there are various conflicting requirements.
*/
int
sysctlnametomib(const char *name, int *mibp, size_t *sizep)
{
int oid[2];
int error;
oid[0] = 0;
oid[1] = 3;
*sizep *= sizeof (int);
error = sysctl(oid, 2, mibp, sizep, name, strlen(name));
*sizep /= sizeof (int);
if (error < 0)
return (error);
return (0);
}