freebsd-dev/lib/libutil/kinfo_getfile.c
Peter Wemm de94a63bd1 Attempt a quick bandaid for arm build breakage. I went to the trouble of
maintaining alignment, but I'm not sure how to tell gcc this.
2008-12-02 10:10:50 +00:00

73 lines
1.3 KiB
C

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/user.h>
#include <sys/sysctl.h>
#include <stdlib.h>
#include <string.h>
#include "libutil.h"
struct kinfo_file *
kinfo_getfile(pid_t pid, int *cntp)
{
int mib[4];
int error;
int cnt;
size_t len;
char *buf, *bp, *eb;
struct kinfo_file *kif, *kp, *kf;
len = 0;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_FILEDESC;
mib[3] = pid;
error = sysctl(mib, 4, NULL, &len, NULL, 0);
if (error)
return (0);
len = len * 4 / 3;
buf = malloc(len);
if (buf == NULL)
return (0);
error = sysctl(mib, 4, buf, &len, NULL, 0);
if (error) {
free(buf);
return (0);
}
/* Pass 1: count items */
cnt = 0;
bp = buf;
eb = buf + len;
while (bp < eb) {
kf = (struct kinfo_file *)(uintptr_t)bp;
bp += kf->kf_structsize;
cnt++;
}
kif = calloc(cnt, sizeof(*kif));
if (kif == NULL) {
free(buf);
return (0);
}
bp = buf;
eb = buf + len;
kp = kif;
/* Pass 2: unpack */
while (bp < eb) {
kf = (struct kinfo_file *)(uintptr_t)bp;
/* Copy/expand into pre-zeroed buffer */
memcpy(kp, kf, kf->kf_structsize);
/* Advance to next packed record */
bp += kf->kf_structsize;
/* Set field size to fixed length, advance */
kp->kf_structsize = sizeof(*kp);
kp++;
}
free(buf);
*cntp = cnt;
return (kif); /* Caller must free() return value */
}