sysctl(3) question

Lars Engels lme at freebsd.org
Wed Aug 13 18:29:25 UTC 2014


On Wed, Aug 13, 2014 at 12:00:32PM -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;
> }

Try sysctlbyname():

#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <err.h>

int main(void) {

size_t len;
char *p;

/* Determine how much space to allocate. */
if (sysctlbyname("kern.disks", NULL, &len, NULL, 0) == -1)
err(1, "sysctl");

if ((p = (char *)malloc(len)) == NULL)
err(1, NULL);

if (sysctlbyname("kern.disks", p, &len, NULL, 0) == -1)
	err(1, "sysctl");

printf("%s\n", p);

return 0;
}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 618 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20140813/2b0b0e72/attachment.sig>


More information about the freebsd-hackers mailing list