libutil: eliminate one syscall from kinfo_getproc

Previously we invoked the sysctl with a NULL buffer to query the size,
allocated a buffer, then invoked it again to fetch the data.

As we only handle the case where the sysctl provides data of the
expected size we can just allocate a correctly-sized buffer to begin
with.

Reported by:	Thomas Hurst via Twitter
Reviewed by:	kevans
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D35140
This commit is contained in:
Ed Maste 2022-05-06 12:41:04 -04:00
parent 1d2421ad8b
commit 904c148f1c

View File

@ -46,17 +46,15 @@ kinfo_getproc(pid_t pid)
int mib[4];
size_t len;
len = 0;
len = sizeof(*kipp);
kipp = malloc(len);
if (kipp == NULL)
return (NULL);
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = pid;
if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0)
return (NULL);
kipp = malloc(len);
if (kipp == NULL)
return (NULL);
if (sysctl(mib, nitems(mib), kipp, &len, NULL, 0) < 0)
goto bad;