DEV Community

Alfonso Siciliano
Alfonso Siciliano

Posted on • Originally published at alfonsosiciliano.gitlab.io

sysctlbyname-improved v20210223

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 "kern.proc.pid.<pid>"

Example with "kern.proc.pid.0", 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("kern.proc.pid.1", &kp, &valuelen, NULL, 0) == 0)
        printf("effective user id: %d\n", kp.ki_uid);
    else
        printf("kern.proc.pid.1: error\n");


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

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

  ## sysctlbyname ##
kern.proc.pid.1: error
  ## sysctlbyname_improved ##
kern.proc.pid.1: (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 "kern.proc.pid.2021" -> [1.14.1.2021], 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: https://gitlab.com/alfix/sysctlbyname-improved.

Top comments (0)