6 #include <sys/sysctl.h>
13 kinfo_getfile(pid_t pid, int *cntp)
20 struct kinfo_file *kif, *kp, *kf;
26 mib[2] = KERN_PROC_FILEDESC;
29 error = sysctl(mib, 4, NULL, &len, NULL, 0);
36 error = sysctl(mib, 4, buf, &len, NULL, 0);
41 /* Pass 1: count items */
46 kf = (struct kinfo_file *)(uintptr_t)bp;
47 bp += kf->kf_structsize;
51 kif = calloc(cnt, sizeof(*kif));
61 kf = (struct kinfo_file *)(uintptr_t)bp;
62 /* Copy/expand into pre-zeroed buffer */
63 memcpy(kp, kf, kf->kf_structsize);
64 /* Advance to next packed record */
65 bp += kf->kf_structsize;
66 /* Set field size to fixed length, advance */
67 kp->kf_structsize = sizeof(*kp);
72 return (kif); /* Caller must free() return value */