DEV Community

Cover image for sysctlmibinfo2 v2.0.1
Alfonso Siciliano
Alfonso Siciliano

Posted on • Edited on • Originally published at alfonsosiciliano.gitlab.io

sysctlmibinfo2 v2.0.1

sysctlmibinfo2 version 2.0.1 is out!

The sysctlmibinfo2 library provides an API to explore the FreeBSD sysctl MIB and to get the properties of an object, so it is useful to handle an object correctly or/and to build a sysctl-like utility; it is used by sysctlview, nsysctl, sysctl-mib-html and mixertui.

New version improvements:

1) New kernel features:

This versione takes advantages of the improvements of sysctlinfo 20210222 and sysctlbyname 20210223: improved efficiency to build a sysctlmif_object and new features to get info about an object: handler and nextbyname.

2) New functions:

sysctlmif_hashandler(), sysctlmif_hashandlerbyname(), sysctlmif_nextnodebyname(), sysctlmif_nextleafbyname(), sysctlmif_leaves() and sysctlmif_leavesbyname().

3) Complete README:

The README provides: Introduction, Description, Getting started, API, Examples and Real world use cases.

4) New examples:

Examples in the Public Domain to build new projects.

5) Updated manual:

sysctlmibinfo2(3).


Example to implement "sysctl -aN" and "nsysctl -aN" using the new feature sysctlmif_nextnodebyname():

#include <sys/param.h>

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

/* Example to implement "/sbin/sysctl -aN" */
int main()
{
        char name[MAXPATHLEN], next[MAXPATHLEN];
        size_t nextlen = MAXPATHLEN;

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

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

Example to implement "sysctl -d hw.snd" or "nsysctl -d hw.snd":

#include <stdio.h>
#include <sysctlmibinfo2.h>

/* Example to implement "sysctl -d hw.snd" */
int main()
{
    struct sysctlmif_list *list;
    struct sysctlmif_object *obj;

    if ((list = sysctlmif_grouplistbyname("hw.snd")) == NULL)
        return (1);

    SLIST_FOREACH(obj, list, object_link)
        printf("%s: %s\n", obj->name, obj->desc);

    sysctlmif_freelist(list);

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

To compile:

% cc -I/usr/local/include example.c -L/usr/local/lib -lsysctlmibinfo2 -o example
Enter fullscreen mode Exit fullscreen mode

To install the port devel/libsysctlmibinfo2:

# cd /usr/ports/devel/libsysctlmibinfo2/ && make install clean
Enter fullscreen mode Exit fullscreen mode

To add the package:

# pkg install libsysctlmibinfo2
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)