sysctlmibinfo2 v2.0.1
Tags: unix, freebsd, sysctl
Published: 2021-03-17 21:42:23

**sysctlmibinfo2 version 2.0.1 is out!**

The [sysctlmibinfo2](https://gitlab.com/alfix/sysctlmibinfo2) library provides an API to explore the [FreeBSD](https://www.freebsd.org) 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](https://gitlab.com/alfix/sysctlview), [nsysctl](https://gitlab.com/alfix/nsysctl), [sysctl-mib-html](https://gitlab.com/alfix/sysctl-mib-html) and [mixertui](https://gitlab.com/alfix/mixertui).

**New version improvements**:

1) New kernel features:

This versione takes advantages of the improvements of  [sysctlinfo 20210222](https://alfonsosiciliano.gitlab.io/posts/2021-03-02-sysctlinfo-20210222.html) and [sysctlbyname 20210223](https://alfonsosiciliano.gitlab.io/posts/2021-03-04-sysctlbyname-improved-20210223.html): 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](https://gitlab.com/alfix/sysctlmibinfo2/-/blob/master/README.md) provides: Introduction, Description, Getting started, API, Examples and Real world use cases.

4) New examples:

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

5) Updated manual:

[sysctlmibinfo2(3)](https://alfonsosiciliano.gitlab.io/posts/2019-12-09-manual-sysctlmibinfo2.html).

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

Example to implement "*[sysctl](https://man.freebsd.org/sysctl/8) -aN*" and "*[nsysctl](https://alfonsosiciliano.gitlab.io/posts/2021-03-08-tutorial-nsysctl-2.html) -aN*" using the new feature *sysctlmif\_nextnodebyname()*:
```c
#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);
}
```

Example to implement "*[sysctl](https://man.freebsd.org/sysctl/8) -d hw.snd*" or "*[nsysctl](https://alfonsosiciliano.gitlab.io/posts/2021-03-08-tutorial-nsysctl-2.html) -d hw.snd*":
```c
#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);
}
```
To compile:
```default
% cc -I/usr/local/include example.c -L/usr/local/lib -lsysctlmibinfo2 -o example
```

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

To install the port [devel/libsysctlmibinfo2](https://www.freshports.org/devel/libsysctlmibinfo2):
```
# cd /usr/ports/devel/libsysctlmibinfo2/ && make install clean
```
To add the package:
```
# pkg install libsysctlmibinfo2
```

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