DEV Community

Cover image for sysctlinfo 20210222
Alfonso Siciliano
Alfonso Siciliano

Posted on • Originally published at alfonsosiciliano.gitlab.io

sysctlinfo 20210222

sysctlinfo version 20210222 is out!

The FreeBSD kernel maintains a Management Information Base ("MIB") where an object represents a parameter of the system. The sysctl system call explores the MIB to find an object by its OID then calls its handler to get or set the value of the parameter. It is often necessary to find an object not to call its handler but to get its info (description, type, OID by name, next object, and so on).

Some typical example with /sbin/sysctl :

% sysctl -d kern.ostype
kern.ostype: Operating system type
% sysctl -t kern.ostype
kern.ostype: string
% sysctl -aN
Enter fullscreen mode Exit fullscreen mode

sysctlinfo is an interface to explore the MIB, it is used by sysctlview, nsysctl, sysctlmibinfo2, sysctlbyname-improved and mixertui.


New version improvements:

New features:

NEXTOBJNODE_BYNAME, NEXTOBJLEAF_BYNAME, OBJHASHANDLER, OBJHASHANDLER_BYNAME and FAKENEXTOBJLEAFNOSKIP, to get more info about an object, to avoid extra computation in userland and to improve the compatibilty with the current undocumented interface.

Improved efficiency:

After a refactoring the system computation is ~100% more efficient than the undocumented interface to pass all info about an object to userland.

Complete README:

The README provides: Introduction, Description, Getting started, Features, API, Real world use cases, Implementation notes, FAQ and a comparison sysctlinfo - undocumented interface.

New examples:

New examples in the Public Domain to build new projects.

Updated manuals:

sysctlinfo(4) and sysctlinfo(3).


An example to implement "sysctl -aN" using the new NEXTOBJNODE_BYNAME feature:

#include <sys/param.h>
#include <sys/sysctl.h>

#include <stdio.h>
#include <string.h>
#include <sysctlinfo.h>

/* Example to implement `/sbin/sysctl -aN` */
int main()
{
        char name[MAXPATHLEN], next[MAXPATHLEN];
        int prop[2] = {CTL_SYSCTL, NEXTOBJNODE_BYNAME};
        size_t nextlen = MAXPATHLEN;

        strcpy(next, "kern");
    do {
                strncpy(name, next, nextlen);
                printf("%s\n", name);
                nextlen = MAXPATHLEN;
        } while(!(SYSCTLINFO_BYNAME(name, prop, next, &nextlen)));

        return (0);
}
Enter fullscreen mode Exit fullscreen mode

Output:


To install the port sysutils/sysctlinfo-kmod:

# cd /usr/ports/sysutils/sysctlinfo-kmod/ && make install clean
Enter fullscreen mode Exit fullscreen mode

To add the package:

# pkg install sysctlinfo-kmod
Enter fullscreen mode Exit fullscreen mode

To know more: https://gitlab.com/alfix/sysctlinfo.

Top comments (0)