3 #include <sys/sysctl.h>
11 kinfo_getfile(pid_t pid, int *cntp)
18 struct kinfo_file *kif, *kp, *kf;
24 mib[2] = KERN_PROC_FILEDESC;
27 error = sysctl(mib, nitems(mib), NULL, &len, NULL, 0);
34 error = sysctl(mib, nitems(mib), buf, &len, NULL, 0);
39 /* Pass 1: count items */
44 kf = (struct kinfo_file *)(uintptr_t)bp;
45 if (kf->kf_structsize == 0)
47 bp += kf->kf_structsize;
51 kif = calloc(cnt, sizeof(*kif));
61 kf = (struct kinfo_file *)(uintptr_t)bp;
62 if (kf->kf_structsize == 0)
64 /* Copy/expand into pre-zeroed buffer */
65 memcpy(kp, kf, kf->kf_structsize);
66 /* Advance to next packed record */
67 bp += kf->kf_structsize;
68 /* Set field size to fixed length, advance */
69 kp->kf_structsize = sizeof(*kp);
74 return (kif); /* Caller must free() return value */