kern.file weirdness

YONETANI Tomokazu qhwt at myrealbox.com
Mon Nov 24 00:09:54 PST 2003


On Sun, Nov 23, 2003 at 11:56:30PM -0800, Matthew Dillon wrote:
> 
> :FreeBSD-current's version of sockstat has a block of code
> :which looks like this:
> :
> :static void
> :getfiles(void)
> :{
> :	size_t len;
> :
> :	if ((xfiles = malloc(len = sizeof *xfiles)) == NULL)
> :		err(1, "malloc()");
> :	while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
> :		if (errno != ENOMEM)
> :			err(1, "sysctlbyname()");
> :		len *= 2;
> :		if ((xfiles = realloc(xfiles, len)) == NULL)
> :			err(1, "realloc()");
> :	}
> :	if (len > 0 && xfiles->xf_size != sizeof *xfiles)
> :		errx(1, "struct xfile size mismatch");
> :	nxfiles = len / sizeof *xfiles;
> :}
> :
> :i.e., you must resize the buffer until sysctlbyname() fails with
> :ENOMEM.
> :
> :Cheers.
> 
>     I think you meant 'resize the buffer until sysctlbyname()
>     stops failing with ENOMEM'.

*Blush* Yes, that was what I meant say. Thanks for the correction.





More information about the Submit mailing list