kern.file weirdness

Matthew Dillon dillon at apollo.backplane.com
Sun Nov 23 23:56:52 PST 2003


: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'.

					-Matt
					Matthew Dillon 
					<dillon at xxxxxxxxxxxxx>





More information about the Submit mailing list