sysctl(3) question

Ian Lepore ian at FreeBSD.org
Wed Aug 13 17:14:14 UTC 2014


On Wed, 2014-08-13 at 12:00 -0500, Sam Fourman Jr. wrote:
> Hello Hackers,
> 
> the following command gives me the disks in a system
> sysctl kern.disks
> kern.disks: cd0 ada0
> 
> 
> my question is, what paramaters and values do I have to pass sysctl(3) to
> give me those disks?
> 
> 
> 
> I have tried and I cant get HW_DISKNAMES from <sys/sysctl.h> to work
> 
> 
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/param.h>
> #include <sys/sysctl.h>
> #include <err.h>
> 
> int main(void) {
> 
> int mib[2];
> size_t len;
> char *p;
> 
> mib[0] = CTL_HW;
> mib[1] = HW_DISKNAMES;
> 
>         /* Determine how much space to allocate. */
>         if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1)
>         err(1, "sysctl");
> 
>         if ((p = (char *)malloc(len)) == NULL)
>         err(1, NULL);
> 
>         /* Populate the allocated area with the string returned
>         *        * by sysctl.
>         *                */
>         if (sysctl(mib, 2, p, &len, NULL, 0) == -1)
>         err(1, "sysctl");
> 
> printf("%s\n", p);
> 
> return 0;
> }
> 
> Sam Fourman Jr.

It looks like the HW_DISKNAMES oid is obsolete and the docs and header
file never got updated to reflect that.

The new disks list uses OID_AUTO rather than a fixed number, so use
sysctlbyname(3) to access it.

-- Ian




More information about the freebsd-hackers mailing list