summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/libc/gen/Makefile.inc4
-rw-r--r--lib/libc/gen/sysctl.355
2 files changed, 56 insertions, 3 deletions
diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc
index 9e42d7a1e44d..cde0adc246fb 100644
--- a/lib/libc/gen/Makefile.inc
+++ b/lib/libc/gen/Makefile.inc
@@ -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
diff --git a/lib/libc/gen/sysctl.3 b/lib/libc/gen/sysctl.3
index 197aefe354b8..d719b06a88a0 100644
--- a/lib/libc/gen/sysctl.3
+++ b/lib/libc/gen/sysctl.3
@@ -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.