DEV Community


sysctlbyname-improved v20210223

Alfonso Siciliano
Software developer, UNIX (*BSD) enthusiast, embedded systems player, hashtags: #coding #kernel #opensource.
Originally published at ・2 min read

sysctlbyname-improved version 20210223 is out!

The FreeBSD Operating System 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 Object Identifier ("OID") to get or set the value of the parameter.

An OID is a series of numbers, it is possible to replace a number with a string to obtain an object name, example [1.1] -> "kern.ostype", the sysctlbyname system call finds an object by its name.

The purpose of sysctlbyname-improved is to allow sysctlbyname to handle:

  • a name with an empty string level, example "security.jail.param.allow.mount."
  • an extended name for a CTLTYPE_NODE with a defined handler, example "<pid>"

Example with "", the implementation of sysctlbyname_improved() is in the project repo:

#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/user.h>

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

int main()
    size_t valuelen;
    struct kinfo_proc kp;

    printf("  ## sysctlbyname ##\n");
    valuelen = sizeof(kp);
    if (sysctlbyname("", &kp, &valuelen, NULL, 0) == 0)
        printf("effective user id: %d\n", kp.ki_uid);
        printf(" error\n");

    printf("  ## sysctlbyname_improved ##\n");
    valuelen = sizeof(kp);
    if (sysctlbyname_improved("", &kp, &valuelen, NULL, 0) == 0)
        printf(" (effective user id) %d\n", kp.ki_uid);
        printf(" error\n");

    return 0;
Enter fullscreen mode Exit fullscreen mode


  ## sysctlbyname ## error
  ## sysctlbyname_improved ## (effective user id) 0
Enter fullscreen mode Exit fullscreen mode

Furthermore this project can be useful to convert an extended name in the corresponding OID, example "" -> [], this feature is used by sysctlmif_oidextendedbybame() function of the sysctlmibinfo2 library.

Properly, sysctlbyname-improved is an extension of sysctlinfo, so this new version takes advantages of the improvements (mainly efficiency) of sysctlinfo 20210222.

To install the port sysutils/sysctlbyname-improved-kmod:

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

To add the package:

# pkg install sysctlbyname-improved-kmod
Enter fullscreen mode Exit fullscreen mode

To know more:

Discussion (0)