sysctlinfo 20210222
Tags: freebsd, unix, kernel, sysctl
Published: 2021-03-18 21:38:41

**sysctlinfo version 20210222 is out!**

The [FreeBSD](https://www.freebsd.org) 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 :
```default
% sysctl -d kern.ostype
kern.ostype: Operating system type
% sysctl -t kern.ostype
kern.ostype: string
% sysctl -aN
```

[sysctlinfo](https://gitlab.com/alfix/sysctlinfo) is an interface to explore the MIB, it is used by [sysctlview](https://gitlab.com/alfix/sysctlview), [nsysctl](https://gitlab.com/alfix/nsysctl), [sysctlmibinfo2](https://gitlab.com/alfix/sysctlmibinfo2), [sysctlbyname-improved](https://gitlab.com/alfix/sysctlbyname-improved) and [mixertui](https://gitlab.com/alfix/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.
[![](https://pbs.twimg.com/media/Et-Qz4TXMAIE0_M?format=png&name=small)][urllink1]

[urllink1]: https://pbs.twimg.com/media/Et-Qz4TXMAIE0_M?format=png&name=small

Complete README:

The [README](https://gitlab.com/alfix/sysctlinfo/-/blob/master/README.md) provides: Introduction, Description, Getting started, Features, API, Real world use cases, Implementation notes, FAQ and a comparison sysctlinfo - undocumented interface.

New examples:

New [examples](https://gitlab.com/alfix/sysctlinfo/-/tree/master/examples) in the Public Domain to build new projects.

Updated manuals:

[sysctlinfo(4) and sysctlinfo(3)](2019-10-02-manuals-sysctlinfo.html).

-------------------------------------------------------------

An example to implement *"sysctl -aN"* using the new NEXTOBJNODE\_BYNAME feature:
```c
#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);
}
```

Output:

[![](https://alfonsosiciliano.gitlab.io/images/sysctlinfo-20210222.png)][urllink2]

[urllink2]: https://alfonsosiciliano.gitlab.io/images/sysctlinfo-20210222.png

-------------------------------------------------------------

To install the port [sysutils/sysctlinfo-kmod](https://www.freshports.org/sysutils/sysctlinfo-kmod):
```default
# cd /usr/ports/sysutils/sysctlinfo-kmod/ && make install clean
```
To add the package:
```default
# pkg install sysctlinfo-kmod
```

**To know more**: <https://gitlab.com/alfix/sysctlinfo>.