Improve NetBSD KERN_PROC2 bit, mostly from Kamil Rytarowski.

This commit is contained in:
Nicholas Marriott 2017-02-19 08:31:05 +00:00
parent 203d604bf7
commit 55e76edd3c

View File

@ -80,30 +80,32 @@ osdep_get_name(int fd, __unused char *tty)
return (NULL);
buf = NULL;
len = sizeof(bestp);
len = sizeof bestp;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC2;
mib[2] = KERN_PROC_PGRP;
mib[4] = sizeof (*buf);
mib[5] = 0;
mib[4] = sizeof *buf;
retry:
mib[5] = 0;
if (sysctl(mib, __arraycount(mib), NULL, &len, NULL, 0) == -1)
return (NULL);
if ((newbuf = realloc(buf, len * sizeof (*buf))) == NULL)
if ((newbuf = realloc(buf, len)) == NULL)
goto error;
buf = newbuf;
mib[5] = len / sizeof(*buf);
mib[5] = len / (sizeof *buf);
if (sysctl(mib, __arraycount(mib), buf, &len, NULL, 0) == -1) {
if (errno == ENOMEM)
goto retry; /* possible infinite loop? */
goto retry;
goto error;
}
bestp = NULL;
for (i = 0; i < len / sizeof (*buf); i++) {
for (i = 0; i < len / (sizeof *buf); i++) {
if (buf[i].p_tdev != sb.st_rdev)
continue;
if (bestp == NULL)