1 /*-
2 * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
3 * Copyright (c) 1988, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: stable/10/lib/libprocstat/libprocstat.c 312036 2017-01-13 08:42:11Z ngie $");
37
38 #include <sys/param.h>
39 #include <sys/elf.h>
40 #include <sys/time.h>
41 #include <sys/resourcevar.h>
42 #define _WANT_UCRED
43 #include <sys/ucred.h>
44 #undef _WANT_UCRED
45 #include <sys/proc.h>
46 #include <sys/user.h>
47 #include <sys/stat.h>
48 #include <sys/vnode.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/domain.h>
52 #include <sys/protosw.h>
53 #include <sys/un.h>
54 #include <sys/unpcb.h>
55 #include <sys/sysctl.h>
56 #include <sys/tty.h>
57 #include <sys/filedesc.h>
58 #include <sys/queue.h>
59 #define _WANT_FILE
60 #include <sys/file.h>
61 #include <sys/conf.h>
62 #include <sys/ksem.h>
63 #include <sys/mman.h>
64 #include <sys/capsicum.h>
65 #define _KERNEL
66 #include <sys/mount.h>
67 #include <sys/pipe.h>
68 #include <ufs/ufs/quota.h>
69 #include <ufs/ufs/inode.h>
70 #include <fs/devfs/devfs.h>
71 #include <fs/devfs/devfs_int.h>
72 #undef _KERNEL
73 #include <nfs/nfsproto.h>
74 #include <nfsclient/nfs.h>
75 #include <nfsclient/nfsnode.h>
76
77 #include <vm/vm.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_object.h>
80
81 #include <net/route.h>
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
85 #include <netinet/in_pcb.h>
86
87 #include <assert.h>
88 #include <ctype.h>
89 #include <err.h>
90 #include <fcntl.h>
91 #include <kvm.h>
92 #include <libutil.h>
93 #include <limits.h>
94 #include <paths.h>
95 #include <pwd.h>
96 #include <stdio.h>
97 #include <stdlib.h>
98 #include <stddef.h>
99 #include <string.h>
100 #include <unistd.h>
101 #include <netdb.h>
102
103 #include <libprocstat.h>
104 #include "libprocstat_internal.h"
105 #include "common_kvm.h"
106 #include "core.h"
107
108 int statfs(const char *, struct statfs *); /* XXX */
109
110 #define PROCSTAT_KVM 1
111 #define PROCSTAT_SYSCTL 2
112 #define PROCSTAT_CORE 3
113
114 static char **getargv(struct procstat *procstat, struct kinfo_proc *kp,
115 size_t nchr, int env);
116 static char *getmnton(kvm_t *kd, struct mount *m);
117 static struct kinfo_vmentry * kinfo_getvmmap_core(struct procstat_core *core,
118 int *cntp);
119 static Elf_Auxinfo *procstat_getauxv_core(struct procstat_core *core,
120 unsigned int *cntp);
121 static Elf_Auxinfo *procstat_getauxv_sysctl(pid_t pid, unsigned int *cntp);
122 static struct filestat_list *procstat_getfiles_kvm(
123 struct procstat *procstat, struct kinfo_proc *kp, int mmapped);
124 static struct filestat_list *procstat_getfiles_sysctl(
125 struct procstat *procstat, struct kinfo_proc *kp, int mmapped);
126 static int procstat_get_pipe_info_sysctl(struct filestat *fst,
127 struct pipestat *pipe, char *errbuf);
128 static int procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst,
129 struct pipestat *pipe, char *errbuf);
130 static int procstat_get_pts_info_sysctl(struct filestat *fst,
131 struct ptsstat *pts, char *errbuf);
132 static int procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst,
133 struct ptsstat *pts, char *errbuf);
134 static int procstat_get_sem_info_sysctl(struct filestat *fst,
135 struct semstat *sem, char *errbuf);
136 static int procstat_get_sem_info_kvm(kvm_t *kd, struct filestat *fst,
137 struct semstat *sem, char *errbuf);
138 static int procstat_get_shm_info_sysctl(struct filestat *fst,
139 struct shmstat *shm, char *errbuf);
140 static int procstat_get_shm_info_kvm(kvm_t *kd, struct filestat *fst,
141 struct shmstat *shm, char *errbuf);
142 static int procstat_get_socket_info_sysctl(struct filestat *fst,
143 struct sockstat *sock, char *errbuf);
144 static int procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst,
145 struct sockstat *sock, char *errbuf);
146 static int to_filestat_flags(int flags);
147 static int procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
148 struct vnstat *vn, char *errbuf);
149 static int procstat_get_vnode_info_sysctl(struct filestat *fst,
150 struct vnstat *vn, char *errbuf);
151 static gid_t *procstat_getgroups_core(struct procstat_core *core,
152 unsigned int *count);
153 static gid_t * procstat_getgroups_kvm(kvm_t *kd, struct kinfo_proc *kp,
154 unsigned int *count);
155 static gid_t *procstat_getgroups_sysctl(pid_t pid, unsigned int *count);
156 static struct kinfo_kstack *procstat_getkstack_sysctl(pid_t pid,
157 int *cntp);
158 static int procstat_getosrel_core(struct procstat_core *core,
159 int *osrelp);
160 static int procstat_getosrel_kvm(kvm_t *kd, struct kinfo_proc *kp,
161 int *osrelp);
162 static int procstat_getosrel_sysctl(pid_t pid, int *osrelp);
163 static int procstat_getpathname_core(struct procstat_core *core,
164 char *pathname, size_t maxlen);
165 static int procstat_getpathname_sysctl(pid_t pid, char *pathname,
166 size_t maxlen);
167 static int procstat_getrlimit_core(struct procstat_core *core, int which,
168 struct rlimit* rlimit);
169 static int procstat_getrlimit_kvm(kvm_t *kd, struct kinfo_proc *kp,
170 int which, struct rlimit* rlimit);
171 static int procstat_getrlimit_sysctl(pid_t pid, int which,
172 struct rlimit* rlimit);
173 static int procstat_getumask_core(struct procstat_core *core,
174 unsigned short *maskp);
175 static int procstat_getumask_kvm(kvm_t *kd, struct kinfo_proc *kp,
176 unsigned short *maskp);
177 static int procstat_getumask_sysctl(pid_t pid, unsigned short *maskp);
178 static int vntype2psfsttype(int type);
179
180 void
procstat_close(struct procstat * procstat)181 procstat_close(struct procstat *procstat)
182 {
183
184 assert(procstat);
185 if (procstat->type == PROCSTAT_KVM)
186 kvm_close(procstat->kd);
187 else if (procstat->type == PROCSTAT_CORE)
188 procstat_core_close(procstat->core);
189 procstat_freeargv(procstat);
190 procstat_freeenvv(procstat);
191 free(procstat);
192 }
193
194 struct procstat *
procstat_open_sysctl(void)195 procstat_open_sysctl(void)
196 {
197 struct procstat *procstat;
198
199 procstat = calloc(1, sizeof(*procstat));
200 if (procstat == NULL) {
201 warn("malloc()");
202 return (NULL);
203 }
204 procstat->type = PROCSTAT_SYSCTL;
205 return (procstat);
206 }
207
208 struct procstat *
procstat_open_kvm(const char * nlistf,const char * memf)209 procstat_open_kvm(const char *nlistf, const char *memf)
210 {
211 struct procstat *procstat;
212 kvm_t *kd;
213 char buf[_POSIX2_LINE_MAX];
214
215 procstat = calloc(1, sizeof(*procstat));
216 if (procstat == NULL) {
217 warn("malloc()");
218 return (NULL);
219 }
220 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
221 if (kd == NULL) {
222 warnx("kvm_openfiles(): %s", buf);
223 free(procstat);
224 return (NULL);
225 }
226 procstat->type = PROCSTAT_KVM;
227 procstat->kd = kd;
228 return (procstat);
229 }
230
231 struct procstat *
procstat_open_core(const char * filename)232 procstat_open_core(const char *filename)
233 {
234 struct procstat *procstat;
235 struct procstat_core *core;
236
237 procstat = calloc(1, sizeof(*procstat));
238 if (procstat == NULL) {
239 warn("malloc()");
240 return (NULL);
241 }
242 core = procstat_core_open(filename);
243 if (core == NULL) {
244 free(procstat);
245 return (NULL);
246 }
247 procstat->type = PROCSTAT_CORE;
248 procstat->core = core;
249 return (procstat);
250 }
251
252 struct kinfo_proc *
procstat_getprocs(struct procstat * procstat,int what,int arg,unsigned int * count)253 procstat_getprocs(struct procstat *procstat, int what, int arg,
254 unsigned int *count)
255 {
256 struct kinfo_proc *p0, *p;
257 size_t len, olen;
258 int name[4];
259 int cnt;
260 int error;
261
262 assert(procstat);
263 assert(count);
264 p = NULL;
265 if (procstat->type == PROCSTAT_KVM) {
266 *count = 0;
267 p0 = kvm_getprocs(procstat->kd, what, arg, &cnt);
268 if (p0 == NULL || cnt <= 0)
269 return (NULL);
270 *count = cnt;
271 len = *count * sizeof(*p);
272 p = malloc(len);
273 if (p == NULL) {
274 warnx("malloc(%zu)", len);
275 goto fail;
276 }
277 bcopy(p0, p, len);
278 return (p);
279 } else if (procstat->type == PROCSTAT_SYSCTL) {
280 len = 0;
281 name[0] = CTL_KERN;
282 name[1] = KERN_PROC;
283 name[2] = what;
284 name[3] = arg;
285 error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
286 if (error < 0 && errno != EPERM) {
287 warn("sysctl(kern.proc)");
288 goto fail;
289 }
290 if (len == 0) {
291 warnx("no processes?");
292 goto fail;
293 }
294 do {
295 len += len / 10;
296 p = reallocf(p, len);
297 if (p == NULL) {
298 warnx("reallocf(%zu)", len);
299 goto fail;
300 }
301 olen = len;
302 error = sysctl(name, nitems(name), p, &len, NULL, 0);
303 } while (error < 0 && errno == ENOMEM && olen == len);
304 if (error < 0 && errno != EPERM) {
305 warn("sysctl(kern.proc)");
306 goto fail;
307 }
308 /* Perform simple consistency checks. */
309 if ((len % sizeof(*p)) != 0 || p->ki_structsize != sizeof(*p)) {
310 warnx("kinfo_proc structure size mismatch (len = %zu)", len);
311 goto fail;
312 }
313 *count = len / sizeof(*p);
314 return (p);
315 } else if (procstat->type == PROCSTAT_CORE) {
316 p = procstat_core_get(procstat->core, PSC_TYPE_PROC, NULL,
317 &len);
318 if ((len % sizeof(*p)) != 0 || p->ki_structsize != sizeof(*p)) {
319 warnx("kinfo_proc structure size mismatch");
320 goto fail;
321 }
322 *count = len / sizeof(*p);
323 return (p);
324 } else {
325 warnx("unknown access method: %d", procstat->type);
326 return (NULL);
327 }
328 fail:
329 if (p)
330 free(p);
331 return (NULL);
332 }
333
334 void
procstat_freeprocs(struct procstat * procstat __unused,struct kinfo_proc * p)335 procstat_freeprocs(struct procstat *procstat __unused, struct kinfo_proc *p)
336 {
337
338 if (p != NULL)
339 free(p);
340 p = NULL;
341 }
342
343 struct filestat_list *
procstat_getfiles(struct procstat * procstat,struct kinfo_proc * kp,int mmapped)344 procstat_getfiles(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
345 {
346
347 switch(procstat->type) {
348 case PROCSTAT_KVM:
349 return (procstat_getfiles_kvm(procstat, kp, mmapped));
350 case PROCSTAT_SYSCTL:
351 case PROCSTAT_CORE:
352 return (procstat_getfiles_sysctl(procstat, kp, mmapped));
353 default:
354 warnx("unknown access method: %d", procstat->type);
355 return (NULL);
356 }
357 }
358
359 void
procstat_freefiles(struct procstat * procstat,struct filestat_list * head)360 procstat_freefiles(struct procstat *procstat, struct filestat_list *head)
361 {
362 struct filestat *fst, *tmp;
363
364 STAILQ_FOREACH_SAFE(fst, head, next, tmp) {
365 if (fst->fs_path != NULL)
366 free(fst->fs_path);
367 free(fst);
368 }
369 free(head);
370 if (procstat->vmentries != NULL) {
371 free(procstat->vmentries);
372 procstat->vmentries = NULL;
373 }
374 if (procstat->files != NULL) {
375 free(procstat->files);
376 procstat->files = NULL;
377 }
378 }
379
380 static struct filestat *
filestat_new_entry(void * typedep,int type,int fd,int fflags,int uflags,int refcount,off_t offset,char * path,cap_rights_t * cap_rightsp)381 filestat_new_entry(void *typedep, int type, int fd, int fflags, int uflags,
382 int refcount, off_t offset, char *path, cap_rights_t *cap_rightsp)
383 {
384 struct filestat *entry;
385
386 entry = calloc(1, sizeof(*entry));
387 if (entry == NULL) {
388 warn("malloc()");
389 return (NULL);
390 }
391 entry->fs_typedep = typedep;
392 entry->fs_fflags = fflags;
393 entry->fs_uflags = uflags;
394 entry->fs_fd = fd;
395 entry->fs_type = type;
396 entry->fs_ref_count = refcount;
397 entry->fs_offset = offset;
398 entry->fs_path = path;
399 if (cap_rightsp != NULL)
400 entry->fs_cap_rights = *cap_rightsp;
401 else
402 cap_rights_init(&entry->fs_cap_rights);
403 return (entry);
404 }
405
406 static struct vnode *
getctty(kvm_t * kd,struct kinfo_proc * kp)407 getctty(kvm_t *kd, struct kinfo_proc *kp)
408 {
409 struct pgrp pgrp;
410 struct proc proc;
411 struct session sess;
412 int error;
413
414 assert(kp);
415 error = kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
416 sizeof(proc));
417 if (error == 0) {
418 warnx("can't read proc struct at %p for pid %d",
419 kp->ki_paddr, kp->ki_pid);
420 return (NULL);
421 }
422 if (proc.p_pgrp == NULL)
423 return (NULL);
424 error = kvm_read_all(kd, (unsigned long)proc.p_pgrp, &pgrp,
425 sizeof(pgrp));
426 if (error == 0) {
427 warnx("can't read pgrp struct at %p for pid %d",
428 proc.p_pgrp, kp->ki_pid);
429 return (NULL);
430 }
431 error = kvm_read_all(kd, (unsigned long)pgrp.pg_session, &sess,
432 sizeof(sess));
433 if (error == 0) {
434 warnx("can't read session struct at %p for pid %d",
435 pgrp.pg_session, kp->ki_pid);
436 return (NULL);
437 }
438 return (sess.s_ttyvp);
439 }
440
441 static struct filestat_list *
procstat_getfiles_kvm(struct procstat * procstat,struct kinfo_proc * kp,int mmapped)442 procstat_getfiles_kvm(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
443 {
444 struct file file;
445 struct filedesc filed;
446 struct vm_map_entry vmentry;
447 struct vm_object object;
448 struct vmspace vmspace;
449 vm_map_entry_t entryp;
450 vm_map_t map;
451 vm_object_t objp;
452 struct vnode *vp;
453 struct file **ofiles;
454 struct filestat *entry;
455 struct filestat_list *head;
456 kvm_t *kd;
457 void *data;
458 int i, fflags;
459 int prot, type;
460 unsigned int nfiles;
461
462 assert(procstat);
463 kd = procstat->kd;
464 if (kd == NULL)
465 return (NULL);
466 if (kp->ki_fd == NULL)
467 return (NULL);
468 if (!kvm_read_all(kd, (unsigned long)kp->ki_fd, &filed,
469 sizeof(filed))) {
470 warnx("can't read filedesc at %p", (void *)kp->ki_fd);
471 return (NULL);
472 }
473
474 /*
475 * Allocate list head.
476 */
477 head = malloc(sizeof(*head));
478 if (head == NULL)
479 return (NULL);
480 STAILQ_INIT(head);
481
482 /* root directory vnode, if one. */
483 if (filed.fd_rdir) {
484 entry = filestat_new_entry(filed.fd_rdir, PS_FST_TYPE_VNODE, -1,
485 PS_FST_FFLAG_READ, PS_FST_UFLAG_RDIR, 0, 0, NULL, NULL);
486 if (entry != NULL)
487 STAILQ_INSERT_TAIL(head, entry, next);
488 }
489 /* current working directory vnode. */
490 if (filed.fd_cdir) {
491 entry = filestat_new_entry(filed.fd_cdir, PS_FST_TYPE_VNODE, -1,
492 PS_FST_FFLAG_READ, PS_FST_UFLAG_CDIR, 0, 0, NULL, NULL);
493 if (entry != NULL)
494 STAILQ_INSERT_TAIL(head, entry, next);
495 }
496 /* jail root, if any. */
497 if (filed.fd_jdir) {
498 entry = filestat_new_entry(filed.fd_jdir, PS_FST_TYPE_VNODE, -1,
499 PS_FST_FFLAG_READ, PS_FST_UFLAG_JAIL, 0, 0, NULL, NULL);
500 if (entry != NULL)
501 STAILQ_INSERT_TAIL(head, entry, next);
502 }
503 /* ktrace vnode, if one */
504 if (kp->ki_tracep) {
505 entry = filestat_new_entry(kp->ki_tracep, PS_FST_TYPE_VNODE, -1,
506 PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE,
507 PS_FST_UFLAG_TRACE, 0, 0, NULL, NULL);
508 if (entry != NULL)
509 STAILQ_INSERT_TAIL(head, entry, next);
510 }
511 /* text vnode, if one */
512 if (kp->ki_textvp) {
513 entry = filestat_new_entry(kp->ki_textvp, PS_FST_TYPE_VNODE, -1,
514 PS_FST_FFLAG_READ, PS_FST_UFLAG_TEXT, 0, 0, NULL, NULL);
515 if (entry != NULL)
516 STAILQ_INSERT_TAIL(head, entry, next);
517 }
518 /* Controlling terminal. */
519 if ((vp = getctty(kd, kp)) != NULL) {
520 entry = filestat_new_entry(vp, PS_FST_TYPE_VNODE, -1,
521 PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE,
522 PS_FST_UFLAG_CTTY, 0, 0, NULL, NULL);
523 if (entry != NULL)
524 STAILQ_INSERT_TAIL(head, entry, next);
525 }
526
527 nfiles = filed.fd_lastfile + 1;
528 ofiles = malloc(nfiles * sizeof(struct file *));
529 if (ofiles == NULL) {
530 warn("malloc(%zu)", nfiles * sizeof(struct file *));
531 goto do_mmapped;
532 }
533 if (!kvm_read_all(kd, (unsigned long)filed.fd_ofiles, ofiles,
534 nfiles * sizeof(struct file *))) {
535 warnx("cannot read file structures at %p",
536 (void *)filed.fd_ofiles);
537 free(ofiles);
538 goto do_mmapped;
539 }
540 for (i = 0; i <= filed.fd_lastfile; i++) {
541 if (ofiles[i] == NULL)
542 continue;
543 if (!kvm_read_all(kd, (unsigned long)ofiles[i], &file,
544 sizeof(struct file))) {
545 warnx("can't read file %d at %p", i,
546 (void *)ofiles[i]);
547 continue;
548 }
549 switch (file.f_type) {
550 case DTYPE_VNODE:
551 type = PS_FST_TYPE_VNODE;
552 data = file.f_vnode;
553 break;
554 case DTYPE_SOCKET:
555 type = PS_FST_TYPE_SOCKET;
556 data = file.f_data;
557 break;
558 case DTYPE_PIPE:
559 type = PS_FST_TYPE_PIPE;
560 data = file.f_data;
561 break;
562 case DTYPE_FIFO:
563 type = PS_FST_TYPE_FIFO;
564 data = file.f_vnode;
565 break;
566 #ifdef DTYPE_PTS
567 case DTYPE_PTS:
568 type = PS_FST_TYPE_PTS;
569 data = file.f_data;
570 break;
571 #endif
572 case DTYPE_SEM:
573 type = PS_FST_TYPE_SEM;
574 data = file.f_data;
575 break;
576 case DTYPE_SHM:
577 type = PS_FST_TYPE_SHM;
578 data = file.f_data;
579 break;
580 default:
581 continue;
582 }
583 /* XXXRW: No capability rights support for kvm yet. */
584 entry = filestat_new_entry(data, type, i,
585 to_filestat_flags(file.f_flag), 0, 0, 0, NULL, NULL);
586 if (entry != NULL)
587 STAILQ_INSERT_TAIL(head, entry, next);
588 }
589 free(ofiles);
590
591 do_mmapped:
592
593 /*
594 * Process mmapped files if requested.
595 */
596 if (mmapped) {
597 if (!kvm_read_all(kd, (unsigned long)kp->ki_vmspace, &vmspace,
598 sizeof(vmspace))) {
599 warnx("can't read vmspace at %p",
600 (void *)kp->ki_vmspace);
601 goto exit;
602 }
603 map = &vmspace.vm_map;
604
605 for (entryp = map->header.next;
606 entryp != &kp->ki_vmspace->vm_map.header;
607 entryp = vmentry.next) {
608 if (!kvm_read_all(kd, (unsigned long)entryp, &vmentry,
609 sizeof(vmentry))) {
610 warnx("can't read vm_map_entry at %p",
611 (void *)entryp);
612 continue;
613 }
614 if (vmentry.eflags & MAP_ENTRY_IS_SUB_MAP)
615 continue;
616 if ((objp = vmentry.object.vm_object) == NULL)
617 continue;
618 for (; objp; objp = object.backing_object) {
619 if (!kvm_read_all(kd, (unsigned long)objp,
620 &object, sizeof(object))) {
621 warnx("can't read vm_object at %p",
622 (void *)objp);
623 break;
624 }
625 }
626
627 /* We want only vnode objects. */
628 if (object.type != OBJT_VNODE)
629 continue;
630
631 prot = vmentry.protection;
632 fflags = 0;
633 if (prot & VM_PROT_READ)
634 fflags = PS_FST_FFLAG_READ;
635 if ((vmentry.eflags & MAP_ENTRY_COW) == 0 &&
636 prot & VM_PROT_WRITE)
637 fflags |= PS_FST_FFLAG_WRITE;
638
639 /*
640 * Create filestat entry.
641 */
642 entry = filestat_new_entry(object.handle,
643 PS_FST_TYPE_VNODE, -1, fflags,
644 PS_FST_UFLAG_MMAP, 0, 0, NULL, NULL);
645 if (entry != NULL)
646 STAILQ_INSERT_TAIL(head, entry, next);
647 }
648 }
649 exit:
650 return (head);
651 }
652
653 /*
654 * kinfo types to filestat translation.
655 */
656 static int
kinfo_type2fst(int kftype)657 kinfo_type2fst(int kftype)
658 {
659 static struct {
660 int kf_type;
661 int fst_type;
662 } kftypes2fst[] = {
663 { KF_TYPE_CRYPTO, PS_FST_TYPE_CRYPTO },
664 { KF_TYPE_FIFO, PS_FST_TYPE_FIFO },
665 { KF_TYPE_KQUEUE, PS_FST_TYPE_KQUEUE },
666 { KF_TYPE_MQUEUE, PS_FST_TYPE_MQUEUE },
667 { KF_TYPE_NONE, PS_FST_TYPE_NONE },
668 { KF_TYPE_PIPE, PS_FST_TYPE_PIPE },
669 { KF_TYPE_PTS, PS_FST_TYPE_PTS },
670 { KF_TYPE_SEM, PS_FST_TYPE_SEM },
671 { KF_TYPE_SHM, PS_FST_TYPE_SHM },
672 { KF_TYPE_SOCKET, PS_FST_TYPE_SOCKET },
673 { KF_TYPE_VNODE, PS_FST_TYPE_VNODE },
674 { KF_TYPE_UNKNOWN, PS_FST_TYPE_UNKNOWN }
675 };
676 #define NKFTYPES (sizeof(kftypes2fst) / sizeof(*kftypes2fst))
677 unsigned int i;
678
679 for (i = 0; i < NKFTYPES; i++)
680 if (kftypes2fst[i].kf_type == kftype)
681 break;
682 if (i == NKFTYPES)
683 return (PS_FST_TYPE_UNKNOWN);
684 return (kftypes2fst[i].fst_type);
685 }
686
687 /*
688 * kinfo flags to filestat translation.
689 */
690 static int
kinfo_fflags2fst(int kfflags)691 kinfo_fflags2fst(int kfflags)
692 {
693 static struct {
694 int kf_flag;
695 int fst_flag;
696 } kfflags2fst[] = {
697 { KF_FLAG_APPEND, PS_FST_FFLAG_APPEND },
698 { KF_FLAG_ASYNC, PS_FST_FFLAG_ASYNC },
699 { KF_FLAG_CREAT, PS_FST_FFLAG_CREAT },
700 { KF_FLAG_DIRECT, PS_FST_FFLAG_DIRECT },
701 { KF_FLAG_EXCL, PS_FST_FFLAG_EXCL },
702 { KF_FLAG_EXEC, PS_FST_FFLAG_EXEC },
703 { KF_FLAG_EXLOCK, PS_FST_FFLAG_EXLOCK },
704 { KF_FLAG_FSYNC, PS_FST_FFLAG_SYNC },
705 { KF_FLAG_HASLOCK, PS_FST_FFLAG_HASLOCK },
706 { KF_FLAG_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW },
707 { KF_FLAG_NONBLOCK, PS_FST_FFLAG_NONBLOCK },
708 { KF_FLAG_READ, PS_FST_FFLAG_READ },
709 { KF_FLAG_SHLOCK, PS_FST_FFLAG_SHLOCK },
710 { KF_FLAG_TRUNC, PS_FST_FFLAG_TRUNC },
711 { KF_FLAG_WRITE, PS_FST_FFLAG_WRITE }
712 };
713 #define NKFFLAGS (sizeof(kfflags2fst) / sizeof(*kfflags2fst))
714 unsigned int i;
715 int flags;
716
717 flags = 0;
718 for (i = 0; i < NKFFLAGS; i++)
719 if ((kfflags & kfflags2fst[i].kf_flag) != 0)
720 flags |= kfflags2fst[i].fst_flag;
721 return (flags);
722 }
723
724 static int
kinfo_uflags2fst(int fd)725 kinfo_uflags2fst(int fd)
726 {
727
728 switch (fd) {
729 case KF_FD_TYPE_CTTY:
730 return (PS_FST_UFLAG_CTTY);
731 case KF_FD_TYPE_CWD:
732 return (PS_FST_UFLAG_CDIR);
733 case KF_FD_TYPE_JAIL:
734 return (PS_FST_UFLAG_JAIL);
735 case KF_FD_TYPE_TEXT:
736 return (PS_FST_UFLAG_TEXT);
737 case KF_FD_TYPE_TRACE:
738 return (PS_FST_UFLAG_TRACE);
739 case KF_FD_TYPE_ROOT:
740 return (PS_FST_UFLAG_RDIR);
741 }
742 return (0);
743 }
744
745 static struct kinfo_file *
kinfo_getfile_core(struct procstat_core * core,int * cntp)746 kinfo_getfile_core(struct procstat_core *core, int *cntp)
747 {
748 int cnt;
749 size_t len;
750 char *buf, *bp, *eb;
751 struct kinfo_file *kif, *kp, *kf;
752
753 buf = procstat_core_get(core, PSC_TYPE_FILES, NULL, &len);
754 if (buf == NULL)
755 return (NULL);
756 /*
757 * XXXMG: The code below is just copy&past from libutil.
758 * The code duplication can be avoided if libutil
759 * is extended to provide something like:
760 * struct kinfo_file *kinfo_getfile_from_buf(const char *buf,
761 * size_t len, int *cntp);
762 */
763
764 /* Pass 1: count items */
765 cnt = 0;
766 bp = buf;
767 eb = buf + len;
768 while (bp < eb) {
769 kf = (struct kinfo_file *)(uintptr_t)bp;
770 if (kf->kf_structsize == 0)
771 break;
772 bp += kf->kf_structsize;
773 cnt++;
774 }
775
776 kif = calloc(cnt, sizeof(*kif));
777 if (kif == NULL) {
778 free(buf);
779 return (NULL);
780 }
781 bp = buf;
782 eb = buf + len;
783 kp = kif;
784 /* Pass 2: unpack */
785 while (bp < eb) {
786 kf = (struct kinfo_file *)(uintptr_t)bp;
787 if (kf->kf_structsize == 0)
788 break;
789 /* Copy/expand into pre-zeroed buffer */
790 memcpy(kp, kf, kf->kf_structsize);
791 /* Advance to next packed record */
792 bp += kf->kf_structsize;
793 /* Set field size to fixed length, advance */
794 kp->kf_structsize = sizeof(*kp);
795 kp++;
796 }
797 free(buf);
798 *cntp = cnt;
799 return (kif); /* Caller must free() return value */
800 }
801
802 static struct filestat_list *
procstat_getfiles_sysctl(struct procstat * procstat,struct kinfo_proc * kp,int mmapped)803 procstat_getfiles_sysctl(struct procstat *procstat, struct kinfo_proc *kp,
804 int mmapped)
805 {
806 struct kinfo_file *kif, *files;
807 struct kinfo_vmentry *kve, *vmentries;
808 struct filestat_list *head;
809 struct filestat *entry;
810 char *path;
811 off_t offset;
812 int cnt, fd, fflags;
813 int i, type, uflags;
814 int refcount;
815 cap_rights_t cap_rights;
816
817 assert(kp);
818 if (kp->ki_fd == NULL)
819 return (NULL);
820 switch(procstat->type) {
821 case PROCSTAT_SYSCTL:
822 files = kinfo_getfile(kp->ki_pid, &cnt);
823 break;
824 case PROCSTAT_CORE:
825 files = kinfo_getfile_core(procstat->core, &cnt);
826 break;
827 default:
828 assert(!"invalid type");
829 }
830 if (files == NULL && errno != EPERM) {
831 warn("kinfo_getfile()");
832 return (NULL);
833 }
834 procstat->files = files;
835
836 /*
837 * Allocate list head.
838 */
839 head = malloc(sizeof(*head));
840 if (head == NULL)
841 return (NULL);
842 STAILQ_INIT(head);
843 for (i = 0; i < cnt; i++) {
844 kif = &files[i];
845
846 type = kinfo_type2fst(kif->kf_type);
847 fd = kif->kf_fd >= 0 ? kif->kf_fd : -1;
848 fflags = kinfo_fflags2fst(kif->kf_flags);
849 uflags = kinfo_uflags2fst(kif->kf_fd);
850 refcount = kif->kf_ref_count;
851 offset = kif->kf_offset;
852 if (*kif->kf_path != '\0')
853 path = strdup(kif->kf_path);
854 else
855 path = NULL;
856 cap_rights = kif->kf_cap_rights;
857
858 /*
859 * Create filestat entry.
860 */
861 entry = filestat_new_entry(kif, type, fd, fflags, uflags,
862 refcount, offset, path, &cap_rights);
863 if (entry != NULL)
864 STAILQ_INSERT_TAIL(head, entry, next);
865 }
866 if (mmapped != 0) {
867 vmentries = procstat_getvmmap(procstat, kp, &cnt);
868 procstat->vmentries = vmentries;
869 if (vmentries == NULL || cnt == 0)
870 goto fail;
871 for (i = 0; i < cnt; i++) {
872 kve = &vmentries[i];
873 if (kve->kve_type != KVME_TYPE_VNODE)
874 continue;
875 fflags = 0;
876 if (kve->kve_protection & KVME_PROT_READ)
877 fflags = PS_FST_FFLAG_READ;
878 if ((kve->kve_flags & KVME_FLAG_COW) == 0 &&
879 kve->kve_protection & KVME_PROT_WRITE)
880 fflags |= PS_FST_FFLAG_WRITE;
881 offset = kve->kve_offset;
882 refcount = kve->kve_ref_count;
883 if (*kve->kve_path != '\0')
884 path = strdup(kve->kve_path);
885 else
886 path = NULL;
887 entry = filestat_new_entry(kve, PS_FST_TYPE_VNODE, -1,
888 fflags, PS_FST_UFLAG_MMAP, refcount, offset, path,
889 NULL);
890 if (entry != NULL)
891 STAILQ_INSERT_TAIL(head, entry, next);
892 }
893 }
894 fail:
895 return (head);
896 }
897
898 int
procstat_get_pipe_info(struct procstat * procstat,struct filestat * fst,struct pipestat * ps,char * errbuf)899 procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst,
900 struct pipestat *ps, char *errbuf)
901 {
902
903 assert(ps);
904 if (procstat->type == PROCSTAT_KVM) {
905 return (procstat_get_pipe_info_kvm(procstat->kd, fst, ps,
906 errbuf));
907 } else if (procstat->type == PROCSTAT_SYSCTL ||
908 procstat->type == PROCSTAT_CORE) {
909 return (procstat_get_pipe_info_sysctl(fst, ps, errbuf));
910 } else {
911 warnx("unknown access method: %d", procstat->type);
912 if (errbuf != NULL)
913 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
914 return (1);
915 }
916 }
917
918 static int
procstat_get_pipe_info_kvm(kvm_t * kd,struct filestat * fst,struct pipestat * ps,char * errbuf)919 procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst,
920 struct pipestat *ps, char *errbuf)
921 {
922 struct pipe pi;
923 void *pipep;
924
925 assert(kd);
926 assert(ps);
927 assert(fst);
928 bzero(ps, sizeof(*ps));
929 pipep = fst->fs_typedep;
930 if (pipep == NULL)
931 goto fail;
932 if (!kvm_read_all(kd, (unsigned long)pipep, &pi, sizeof(struct pipe))) {
933 warnx("can't read pipe at %p", (void *)pipep);
934 goto fail;
935 }
936 ps->addr = (uintptr_t)pipep;
937 ps->peer = (uintptr_t)pi.pipe_peer;
938 ps->buffer_cnt = pi.pipe_buffer.cnt;
939 return (0);
940
941 fail:
942 if (errbuf != NULL)
943 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
944 return (1);
945 }
946
947 static int
procstat_get_pipe_info_sysctl(struct filestat * fst,struct pipestat * ps,char * errbuf __unused)948 procstat_get_pipe_info_sysctl(struct filestat *fst, struct pipestat *ps,
949 char *errbuf __unused)
950 {
951 struct kinfo_file *kif;
952
953 assert(ps);
954 assert(fst);
955 bzero(ps, sizeof(*ps));
956 kif = fst->fs_typedep;
957 if (kif == NULL)
958 return (1);
959 ps->addr = kif->kf_un.kf_pipe.kf_pipe_addr;
960 ps->peer = kif->kf_un.kf_pipe.kf_pipe_peer;
961 ps->buffer_cnt = kif->kf_un.kf_pipe.kf_pipe_buffer_cnt;
962 return (0);
963 }
964
965 int
procstat_get_pts_info(struct procstat * procstat,struct filestat * fst,struct ptsstat * pts,char * errbuf)966 procstat_get_pts_info(struct procstat *procstat, struct filestat *fst,
967 struct ptsstat *pts, char *errbuf)
968 {
969
970 assert(pts);
971 if (procstat->type == PROCSTAT_KVM) {
972 return (procstat_get_pts_info_kvm(procstat->kd, fst, pts,
973 errbuf));
974 } else if (procstat->type == PROCSTAT_SYSCTL ||
975 procstat->type == PROCSTAT_CORE) {
976 return (procstat_get_pts_info_sysctl(fst, pts, errbuf));
977 } else {
978 warnx("unknown access method: %d", procstat->type);
979 if (errbuf != NULL)
980 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
981 return (1);
982 }
983 }
984
985 static int
procstat_get_pts_info_kvm(kvm_t * kd,struct filestat * fst,struct ptsstat * pts,char * errbuf)986 procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst,
987 struct ptsstat *pts, char *errbuf)
988 {
989 struct tty tty;
990 void *ttyp;
991
992 assert(kd);
993 assert(pts);
994 assert(fst);
995 bzero(pts, sizeof(*pts));
996 ttyp = fst->fs_typedep;
997 if (ttyp == NULL)
998 goto fail;
999 if (!kvm_read_all(kd, (unsigned long)ttyp, &tty, sizeof(struct tty))) {
1000 warnx("can't read tty at %p", (void *)ttyp);
1001 goto fail;
1002 }
1003 pts->dev = dev2udev(kd, tty.t_dev);
1004 (void)kdevtoname(kd, tty.t_dev, pts->devname);
1005 return (0);
1006
1007 fail:
1008 if (errbuf != NULL)
1009 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1010 return (1);
1011 }
1012
1013 static int
procstat_get_pts_info_sysctl(struct filestat * fst,struct ptsstat * pts,char * errbuf __unused)1014 procstat_get_pts_info_sysctl(struct filestat *fst, struct ptsstat *pts,
1015 char *errbuf __unused)
1016 {
1017 struct kinfo_file *kif;
1018
1019 assert(pts);
1020 assert(fst);
1021 bzero(pts, sizeof(*pts));
1022 kif = fst->fs_typedep;
1023 if (kif == NULL)
1024 return (0);
1025 pts->dev = kif->kf_un.kf_pts.kf_pts_dev;
1026 strlcpy(pts->devname, kif->kf_path, sizeof(pts->devname));
1027 return (0);
1028 }
1029
1030 int
procstat_get_sem_info(struct procstat * procstat,struct filestat * fst,struct semstat * sem,char * errbuf)1031 procstat_get_sem_info(struct procstat *procstat, struct filestat *fst,
1032 struct semstat *sem, char *errbuf)
1033 {
1034
1035 assert(sem);
1036 if (procstat->type == PROCSTAT_KVM) {
1037 return (procstat_get_sem_info_kvm(procstat->kd, fst, sem,
1038 errbuf));
1039 } else if (procstat->type == PROCSTAT_SYSCTL ||
1040 procstat->type == PROCSTAT_CORE) {
1041 return (procstat_get_sem_info_sysctl(fst, sem, errbuf));
1042 } else {
1043 warnx("unknown access method: %d", procstat->type);
1044 if (errbuf != NULL)
1045 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1046 return (1);
1047 }
1048 }
1049
1050 static int
procstat_get_sem_info_kvm(kvm_t * kd,struct filestat * fst,struct semstat * sem,char * errbuf)1051 procstat_get_sem_info_kvm(kvm_t *kd, struct filestat *fst,
1052 struct semstat *sem, char *errbuf)
1053 {
1054 struct ksem ksem;
1055 void *ksemp;
1056 char *path;
1057 int i;
1058
1059 assert(kd);
1060 assert(sem);
1061 assert(fst);
1062 bzero(sem, sizeof(*sem));
1063 ksemp = fst->fs_typedep;
1064 if (ksemp == NULL)
1065 goto fail;
1066 if (!kvm_read_all(kd, (unsigned long)ksemp, &ksem,
1067 sizeof(struct ksem))) {
1068 warnx("can't read ksem at %p", (void *)ksemp);
1069 goto fail;
1070 }
1071 sem->mode = S_IFREG | ksem.ks_mode;
1072 sem->value = ksem.ks_value;
1073 if (fst->fs_path == NULL && ksem.ks_path != NULL) {
1074 path = malloc(MAXPATHLEN);
1075 for (i = 0; i < MAXPATHLEN - 1; i++) {
1076 if (!kvm_read_all(kd, (unsigned long)ksem.ks_path + i,
1077 path + i, 1))
1078 break;
1079 if (path[i] == '\0')
1080 break;
1081 }
1082 path[i] = '\0';
1083 if (i == 0)
1084 free(path);
1085 else
1086 fst->fs_path = path;
1087 }
1088 return (0);
1089
1090 fail:
1091 if (errbuf != NULL)
1092 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1093 return (1);
1094 }
1095
1096 static int
procstat_get_sem_info_sysctl(struct filestat * fst,struct semstat * sem,char * errbuf __unused)1097 procstat_get_sem_info_sysctl(struct filestat *fst, struct semstat *sem,
1098 char *errbuf __unused)
1099 {
1100 struct kinfo_file *kif;
1101
1102 assert(sem);
1103 assert(fst);
1104 bzero(sem, sizeof(*sem));
1105 kif = fst->fs_typedep;
1106 if (kif == NULL)
1107 return (0);
1108 sem->value = kif->kf_un.kf_sem.kf_sem_value;
1109 sem->mode = kif->kf_un.kf_sem.kf_sem_mode;
1110 return (0);
1111 }
1112
1113 int
procstat_get_shm_info(struct procstat * procstat,struct filestat * fst,struct shmstat * shm,char * errbuf)1114 procstat_get_shm_info(struct procstat *procstat, struct filestat *fst,
1115 struct shmstat *shm, char *errbuf)
1116 {
1117
1118 assert(shm);
1119 if (procstat->type == PROCSTAT_KVM) {
1120 return (procstat_get_shm_info_kvm(procstat->kd, fst, shm,
1121 errbuf));
1122 } else if (procstat->type == PROCSTAT_SYSCTL ||
1123 procstat->type == PROCSTAT_CORE) {
1124 return (procstat_get_shm_info_sysctl(fst, shm, errbuf));
1125 } else {
1126 warnx("unknown access method: %d", procstat->type);
1127 if (errbuf != NULL)
1128 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1129 return (1);
1130 }
1131 }
1132
1133 static int
procstat_get_shm_info_kvm(kvm_t * kd,struct filestat * fst,struct shmstat * shm,char * errbuf)1134 procstat_get_shm_info_kvm(kvm_t *kd, struct filestat *fst,
1135 struct shmstat *shm, char *errbuf)
1136 {
1137 struct shmfd shmfd;
1138 void *shmfdp;
1139 char *path;
1140 int i;
1141
1142 assert(kd);
1143 assert(shm);
1144 assert(fst);
1145 bzero(shm, sizeof(*shm));
1146 shmfdp = fst->fs_typedep;
1147 if (shmfdp == NULL)
1148 goto fail;
1149 if (!kvm_read_all(kd, (unsigned long)shmfdp, &shmfd,
1150 sizeof(struct shmfd))) {
1151 warnx("can't read shmfd at %p", (void *)shmfdp);
1152 goto fail;
1153 }
1154 shm->mode = S_IFREG | shmfd.shm_mode;
1155 shm->size = shmfd.shm_size;
1156 if (fst->fs_path == NULL && shmfd.shm_path != NULL) {
1157 path = malloc(MAXPATHLEN);
1158 for (i = 0; i < MAXPATHLEN - 1; i++) {
1159 if (!kvm_read_all(kd, (unsigned long)shmfd.shm_path + i,
1160 path + i, 1))
1161 break;
1162 if (path[i] == '\0')
1163 break;
1164 }
1165 path[i] = '\0';
1166 if (i == 0)
1167 free(path);
1168 else
1169 fst->fs_path = path;
1170 }
1171 return (0);
1172
1173 fail:
1174 if (errbuf != NULL)
1175 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1176 return (1);
1177 }
1178
1179 static int
procstat_get_shm_info_sysctl(struct filestat * fst,struct shmstat * shm,char * errbuf __unused)1180 procstat_get_shm_info_sysctl(struct filestat *fst, struct shmstat *shm,
1181 char *errbuf __unused)
1182 {
1183 struct kinfo_file *kif;
1184
1185 assert(shm);
1186 assert(fst);
1187 bzero(shm, sizeof(*shm));
1188 kif = fst->fs_typedep;
1189 if (kif == NULL)
1190 return (0);
1191 shm->size = kif->kf_un.kf_file.kf_file_size;
1192 shm->mode = kif->kf_un.kf_file.kf_file_mode;
1193 return (0);
1194 }
1195
1196 int
procstat_get_vnode_info(struct procstat * procstat,struct filestat * fst,struct vnstat * vn,char * errbuf)1197 procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
1198 struct vnstat *vn, char *errbuf)
1199 {
1200
1201 assert(vn);
1202 if (procstat->type == PROCSTAT_KVM) {
1203 return (procstat_get_vnode_info_kvm(procstat->kd, fst, vn,
1204 errbuf));
1205 } else if (procstat->type == PROCSTAT_SYSCTL ||
1206 procstat->type == PROCSTAT_CORE) {
1207 return (procstat_get_vnode_info_sysctl(fst, vn, errbuf));
1208 } else {
1209 warnx("unknown access method: %d", procstat->type);
1210 if (errbuf != NULL)
1211 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1212 return (1);
1213 }
1214 }
1215
1216 static int
procstat_get_vnode_info_kvm(kvm_t * kd,struct filestat * fst,struct vnstat * vn,char * errbuf)1217 procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
1218 struct vnstat *vn, char *errbuf)
1219 {
1220 /* Filesystem specific handlers. */
1221 #define FSTYPE(fst) {#fst, fst##_filestat}
1222 struct {
1223 const char *tag;
1224 int (*handler)(kvm_t *kd, struct vnode *vp,
1225 struct vnstat *vn);
1226 } fstypes[] = {
1227 FSTYPE(devfs),
1228 FSTYPE(isofs),
1229 FSTYPE(msdosfs),
1230 FSTYPE(nfs),
1231 FSTYPE(smbfs),
1232 FSTYPE(udf),
1233 FSTYPE(ufs),
1234 #ifdef LIBPROCSTAT_ZFS
1235 FSTYPE(zfs),
1236 #endif
1237 };
1238 #define NTYPES (sizeof(fstypes) / sizeof(*fstypes))
1239 struct vnode vnode;
1240 char tagstr[12];
1241 void *vp;
1242 int error, found;
1243 unsigned int i;
1244
1245 assert(kd);
1246 assert(vn);
1247 assert(fst);
1248 vp = fst->fs_typedep;
1249 if (vp == NULL)
1250 goto fail;
1251 error = kvm_read_all(kd, (unsigned long)vp, &vnode, sizeof(vnode));
1252 if (error == 0) {
1253 warnx("can't read vnode at %p", (void *)vp);
1254 goto fail;
1255 }
1256 bzero(vn, sizeof(*vn));
1257 vn->vn_type = vntype2psfsttype(vnode.v_type);
1258 if (vnode.v_type == VNON || vnode.v_type == VBAD)
1259 return (0);
1260 error = kvm_read_all(kd, (unsigned long)vnode.v_tag, tagstr,
1261 sizeof(tagstr));
1262 if (error == 0) {
1263 warnx("can't read v_tag at %p", (void *)vp);
1264 goto fail;
1265 }
1266 tagstr[sizeof(tagstr) - 1] = '\0';
1267
1268 /*
1269 * Find appropriate handler.
1270 */
1271 for (i = 0, found = 0; i < NTYPES; i++)
1272 if (!strcmp(fstypes[i].tag, tagstr)) {
1273 if (fstypes[i].handler(kd, &vnode, vn) != 0) {
1274 goto fail;
1275 }
1276 break;
1277 }
1278 if (i == NTYPES) {
1279 if (errbuf != NULL)
1280 snprintf(errbuf, _POSIX2_LINE_MAX, "?(%s)", tagstr);
1281 return (1);
1282 }
1283 vn->vn_mntdir = getmnton(kd, vnode.v_mount);
1284 if ((vnode.v_type == VBLK || vnode.v_type == VCHR) &&
1285 vnode.v_rdev != NULL){
1286 vn->vn_dev = dev2udev(kd, vnode.v_rdev);
1287 (void)kdevtoname(kd, vnode.v_rdev, vn->vn_devname);
1288 } else {
1289 vn->vn_dev = -1;
1290 }
1291 return (0);
1292
1293 fail:
1294 if (errbuf != NULL)
1295 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1296 return (1);
1297 }
1298
1299 /*
1300 * kinfo vnode type to filestat translation.
1301 */
1302 static int
kinfo_vtype2fst(int kfvtype)1303 kinfo_vtype2fst(int kfvtype)
1304 {
1305 static struct {
1306 int kf_vtype;
1307 int fst_vtype;
1308 } kfvtypes2fst[] = {
1309 { KF_VTYPE_VBAD, PS_FST_VTYPE_VBAD },
1310 { KF_VTYPE_VBLK, PS_FST_VTYPE_VBLK },
1311 { KF_VTYPE_VCHR, PS_FST_VTYPE_VCHR },
1312 { KF_VTYPE_VDIR, PS_FST_VTYPE_VDIR },
1313 { KF_VTYPE_VFIFO, PS_FST_VTYPE_VFIFO },
1314 { KF_VTYPE_VLNK, PS_FST_VTYPE_VLNK },
1315 { KF_VTYPE_VNON, PS_FST_VTYPE_VNON },
1316 { KF_VTYPE_VREG, PS_FST_VTYPE_VREG },
1317 { KF_VTYPE_VSOCK, PS_FST_VTYPE_VSOCK }
1318 };
1319 #define NKFVTYPES (sizeof(kfvtypes2fst) / sizeof(*kfvtypes2fst))
1320 unsigned int i;
1321
1322 for (i = 0; i < NKFVTYPES; i++)
1323 if (kfvtypes2fst[i].kf_vtype == kfvtype)
1324 break;
1325 if (i == NKFVTYPES)
1326 return (PS_FST_VTYPE_UNKNOWN);
1327 return (kfvtypes2fst[i].fst_vtype);
1328 }
1329
1330 static int
procstat_get_vnode_info_sysctl(struct filestat * fst,struct vnstat * vn,char * errbuf)1331 procstat_get_vnode_info_sysctl(struct filestat *fst, struct vnstat *vn,
1332 char *errbuf)
1333 {
1334 struct statfs stbuf;
1335 struct kinfo_file *kif;
1336 struct kinfo_vmentry *kve;
1337 uint64_t fileid;
1338 uint64_t size;
1339 char *name, *path;
1340 uint32_t fsid;
1341 uint16_t mode;
1342 uint32_t rdev;
1343 int vntype;
1344 int status;
1345
1346 assert(fst);
1347 assert(vn);
1348 bzero(vn, sizeof(*vn));
1349 if (fst->fs_typedep == NULL)
1350 return (1);
1351 if (fst->fs_uflags & PS_FST_UFLAG_MMAP) {
1352 kve = fst->fs_typedep;
1353 fileid = kve->kve_vn_fileid;
1354 fsid = kve->kve_vn_fsid;
1355 mode = kve->kve_vn_mode;
1356 path = kve->kve_path;
1357 rdev = kve->kve_vn_rdev;
1358 size = kve->kve_vn_size;
1359 vntype = kinfo_vtype2fst(kve->kve_vn_type);
1360 status = kve->kve_status;
1361 } else {
1362 kif = fst->fs_typedep;
1363 fileid = kif->kf_un.kf_file.kf_file_fileid;
1364 fsid = kif->kf_un.kf_file.kf_file_fsid;
1365 mode = kif->kf_un.kf_file.kf_file_mode;
1366 path = kif->kf_path;
1367 rdev = kif->kf_un.kf_file.kf_file_rdev;
1368 size = kif->kf_un.kf_file.kf_file_size;
1369 vntype = kinfo_vtype2fst(kif->kf_vnode_type);
1370 status = kif->kf_status;
1371 }
1372 vn->vn_type = vntype;
1373 if (vntype == PS_FST_VTYPE_VNON || vntype == PS_FST_VTYPE_VBAD)
1374 return (0);
1375 if ((status & KF_ATTR_VALID) == 0) {
1376 if (errbuf != NULL) {
1377 snprintf(errbuf, _POSIX2_LINE_MAX,
1378 "? (no info available)");
1379 }
1380 return (1);
1381 }
1382 if (path && *path) {
1383 statfs(path, &stbuf);
1384 vn->vn_mntdir = strdup(stbuf.f_mntonname);
1385 } else
1386 vn->vn_mntdir = strdup("-");
1387 vn->vn_dev = rdev;
1388 if (vntype == PS_FST_VTYPE_VBLK) {
1389 name = devname(rdev, S_IFBLK);
1390 if (name != NULL)
1391 strlcpy(vn->vn_devname, name,
1392 sizeof(vn->vn_devname));
1393 } else if (vntype == PS_FST_VTYPE_VCHR) {
1394 name = devname(vn->vn_dev, S_IFCHR);
1395 if (name != NULL)
1396 strlcpy(vn->vn_devname, name,
1397 sizeof(vn->vn_devname));
1398 }
1399 vn->vn_fsid = fsid;
1400 vn->vn_fileid = fileid;
1401 vn->vn_size = size;
1402 vn->vn_mode = mode;
1403 return (0);
1404 }
1405
1406 int
procstat_get_socket_info(struct procstat * procstat,struct filestat * fst,struct sockstat * sock,char * errbuf)1407 procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
1408 struct sockstat *sock, char *errbuf)
1409 {
1410
1411 assert(sock);
1412 if (procstat->type == PROCSTAT_KVM) {
1413 return (procstat_get_socket_info_kvm(procstat->kd, fst, sock,
1414 errbuf));
1415 } else if (procstat->type == PROCSTAT_SYSCTL ||
1416 procstat->type == PROCSTAT_CORE) {
1417 return (procstat_get_socket_info_sysctl(fst, sock, errbuf));
1418 } else {
1419 warnx("unknown access method: %d", procstat->type);
1420 if (errbuf != NULL)
1421 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1422 return (1);
1423 }
1424 }
1425
1426 static int
procstat_get_socket_info_kvm(kvm_t * kd,struct filestat * fst,struct sockstat * sock,char * errbuf)1427 procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst,
1428 struct sockstat *sock, char *errbuf)
1429 {
1430 struct domain dom;
1431 struct inpcb inpcb;
1432 struct protosw proto;
1433 struct socket s;
1434 struct unpcb unpcb;
1435 ssize_t len;
1436 void *so;
1437
1438 assert(kd);
1439 assert(sock);
1440 assert(fst);
1441 bzero(sock, sizeof(*sock));
1442 so = fst->fs_typedep;
1443 if (so == NULL)
1444 goto fail;
1445 sock->so_addr = (uintptr_t)so;
1446 /* fill in socket */
1447 if (!kvm_read_all(kd, (unsigned long)so, &s,
1448 sizeof(struct socket))) {
1449 warnx("can't read sock at %p", (void *)so);
1450 goto fail;
1451 }
1452 /* fill in protosw entry */
1453 if (!kvm_read_all(kd, (unsigned long)s.so_proto, &proto,
1454 sizeof(struct protosw))) {
1455 warnx("can't read protosw at %p", (void *)s.so_proto);
1456 goto fail;
1457 }
1458 /* fill in domain */
1459 if (!kvm_read_all(kd, (unsigned long)proto.pr_domain, &dom,
1460 sizeof(struct domain))) {
1461 warnx("can't read domain at %p",
1462 (void *)proto.pr_domain);
1463 goto fail;
1464 }
1465 if ((len = kvm_read(kd, (unsigned long)dom.dom_name, sock->dname,
1466 sizeof(sock->dname) - 1)) < 0) {
1467 warnx("can't read domain name at %p", (void *)dom.dom_name);
1468 sock->dname[0] = '\0';
1469 }
1470 else
1471 sock->dname[len] = '\0';
1472
1473 /*
1474 * Fill in known data.
1475 */
1476 sock->type = s.so_type;
1477 sock->proto = proto.pr_protocol;
1478 sock->dom_family = dom.dom_family;
1479 sock->so_pcb = (uintptr_t)s.so_pcb;
1480
1481 /*
1482 * Protocol specific data.
1483 */
1484 switch(dom.dom_family) {
1485 case AF_INET:
1486 case AF_INET6:
1487 if (proto.pr_protocol == IPPROTO_TCP) {
1488 if (s.so_pcb) {
1489 if (kvm_read(kd, (u_long)s.so_pcb,
1490 (char *)&inpcb, sizeof(struct inpcb))
1491 != sizeof(struct inpcb)) {
1492 warnx("can't read inpcb at %p",
1493 (void *)s.so_pcb);
1494 } else
1495 sock->inp_ppcb =
1496 (uintptr_t)inpcb.inp_ppcb;
1497 }
1498 }
1499 break;
1500 case AF_UNIX:
1501 if (s.so_pcb) {
1502 if (kvm_read(kd, (u_long)s.so_pcb, (char *)&unpcb,
1503 sizeof(struct unpcb)) != sizeof(struct unpcb)){
1504 warnx("can't read unpcb at %p",
1505 (void *)s.so_pcb);
1506 } else if (unpcb.unp_conn) {
1507 sock->so_rcv_sb_state = s.so_rcv.sb_state;
1508 sock->so_snd_sb_state = s.so_snd.sb_state;
1509 sock->unp_conn = (uintptr_t)unpcb.unp_conn;
1510 }
1511 }
1512 break;
1513 default:
1514 break;
1515 }
1516 return (0);
1517
1518 fail:
1519 if (errbuf != NULL)
1520 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1521 return (1);
1522 }
1523
1524 static int
procstat_get_socket_info_sysctl(struct filestat * fst,struct sockstat * sock,char * errbuf __unused)1525 procstat_get_socket_info_sysctl(struct filestat *fst, struct sockstat *sock,
1526 char *errbuf __unused)
1527 {
1528 struct kinfo_file *kif;
1529
1530 assert(sock);
1531 assert(fst);
1532 bzero(sock, sizeof(*sock));
1533 kif = fst->fs_typedep;
1534 if (kif == NULL)
1535 return (0);
1536
1537 /*
1538 * Fill in known data.
1539 */
1540 sock->type = kif->kf_sock_type;
1541 sock->proto = kif->kf_sock_protocol;
1542 sock->dom_family = kif->kf_sock_domain;
1543 sock->so_pcb = kif->kf_un.kf_sock.kf_sock_pcb;
1544 strlcpy(sock->dname, kif->kf_path, sizeof(sock->dname));
1545 bcopy(&kif->kf_sa_local, &sock->sa_local, kif->kf_sa_local.ss_len);
1546 bcopy(&kif->kf_sa_peer, &sock->sa_peer, kif->kf_sa_peer.ss_len);
1547
1548 /*
1549 * Protocol specific data.
1550 */
1551 switch(sock->dom_family) {
1552 case AF_INET:
1553 case AF_INET6:
1554 if (sock->proto == IPPROTO_TCP)
1555 sock->inp_ppcb = kif->kf_un.kf_sock.kf_sock_inpcb;
1556 break;
1557 case AF_UNIX:
1558 if (kif->kf_un.kf_sock.kf_sock_unpconn != 0) {
1559 sock->so_rcv_sb_state =
1560 kif->kf_un.kf_sock.kf_sock_rcv_sb_state;
1561 sock->so_snd_sb_state =
1562 kif->kf_un.kf_sock.kf_sock_snd_sb_state;
1563 sock->unp_conn =
1564 kif->kf_un.kf_sock.kf_sock_unpconn;
1565 }
1566 break;
1567 default:
1568 break;
1569 }
1570 return (0);
1571 }
1572
1573 /*
1574 * Descriptor flags to filestat translation.
1575 */
1576 static int
to_filestat_flags(int flags)1577 to_filestat_flags(int flags)
1578 {
1579 static struct {
1580 int flag;
1581 int fst_flag;
1582 } fstflags[] = {
1583 { FREAD, PS_FST_FFLAG_READ },
1584 { FWRITE, PS_FST_FFLAG_WRITE },
1585 { O_APPEND, PS_FST_FFLAG_APPEND },
1586 { O_ASYNC, PS_FST_FFLAG_ASYNC },
1587 { O_CREAT, PS_FST_FFLAG_CREAT },
1588 { O_DIRECT, PS_FST_FFLAG_DIRECT },
1589 { O_EXCL, PS_FST_FFLAG_EXCL },
1590 { O_EXEC, PS_FST_FFLAG_EXEC },
1591 { O_EXLOCK, PS_FST_FFLAG_EXLOCK },
1592 { O_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW },
1593 { O_NONBLOCK, PS_FST_FFLAG_NONBLOCK },
1594 { O_SHLOCK, PS_FST_FFLAG_SHLOCK },
1595 { O_SYNC, PS_FST_FFLAG_SYNC },
1596 { O_TRUNC, PS_FST_FFLAG_TRUNC }
1597 };
1598 #define NFSTFLAGS (sizeof(fstflags) / sizeof(*fstflags))
1599 int fst_flags;
1600 unsigned int i;
1601
1602 fst_flags = 0;
1603 for (i = 0; i < NFSTFLAGS; i++)
1604 if (flags & fstflags[i].flag)
1605 fst_flags |= fstflags[i].fst_flag;
1606 return (fst_flags);
1607 }
1608
1609 /*
1610 * Vnode type to filestate translation.
1611 */
1612 static int
vntype2psfsttype(int type)1613 vntype2psfsttype(int type)
1614 {
1615 static struct {
1616 int vtype;
1617 int fst_vtype;
1618 } vt2fst[] = {
1619 { VBAD, PS_FST_VTYPE_VBAD },
1620 { VBLK, PS_FST_VTYPE_VBLK },
1621 { VCHR, PS_FST_VTYPE_VCHR },
1622 { VDIR, PS_FST_VTYPE_VDIR },
1623 { VFIFO, PS_FST_VTYPE_VFIFO },
1624 { VLNK, PS_FST_VTYPE_VLNK },
1625 { VNON, PS_FST_VTYPE_VNON },
1626 { VREG, PS_FST_VTYPE_VREG },
1627 { VSOCK, PS_FST_VTYPE_VSOCK }
1628 };
1629 #define NVFTYPES (sizeof(vt2fst) / sizeof(*vt2fst))
1630 unsigned int i, fst_type;
1631
1632 fst_type = PS_FST_VTYPE_UNKNOWN;
1633 for (i = 0; i < NVFTYPES; i++) {
1634 if (type == vt2fst[i].vtype) {
1635 fst_type = vt2fst[i].fst_vtype;
1636 break;
1637 }
1638 }
1639 return (fst_type);
1640 }
1641
1642 static char *
getmnton(kvm_t * kd,struct mount * m)1643 getmnton(kvm_t *kd, struct mount *m)
1644 {
1645 struct mount mnt;
1646 static struct mtab {
1647 struct mtab *next;
1648 struct mount *m;
1649 char mntonname[MNAMELEN + 1];
1650 } *mhead = NULL;
1651 struct mtab *mt;
1652
1653 for (mt = mhead; mt != NULL; mt = mt->next)
1654 if (m == mt->m)
1655 return (mt->mntonname);
1656 if (!kvm_read_all(kd, (unsigned long)m, &mnt, sizeof(struct mount))) {
1657 warnx("can't read mount table at %p", (void *)m);
1658 return (NULL);
1659 }
1660 if ((mt = malloc(sizeof (struct mtab))) == NULL)
1661 err(1, NULL);
1662 mt->m = m;
1663 bcopy(&mnt.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
1664 mt->mntonname[MNAMELEN] = '\0';
1665 mt->next = mhead;
1666 mhead = mt;
1667 return (mt->mntonname);
1668 }
1669
1670 /*
1671 * Auxiliary structures and functions to get process environment or
1672 * command line arguments.
1673 */
1674 struct argvec {
1675 char *buf;
1676 size_t bufsize;
1677 char **argv;
1678 size_t argc;
1679 };
1680
1681 static struct argvec *
argvec_alloc(size_t bufsize)1682 argvec_alloc(size_t bufsize)
1683 {
1684 struct argvec *av;
1685
1686 av = malloc(sizeof(*av));
1687 if (av == NULL)
1688 return (NULL);
1689 av->bufsize = bufsize;
1690 av->buf = malloc(av->bufsize);
1691 if (av->buf == NULL) {
1692 free(av);
1693 return (NULL);
1694 }
1695 av->argc = 32;
1696 av->argv = malloc(sizeof(char *) * av->argc);
1697 if (av->argv == NULL) {
1698 free(av->buf);
1699 free(av);
1700 return (NULL);
1701 }
1702 return av;
1703 }
1704
1705 static void
argvec_free(struct argvec * av)1706 argvec_free(struct argvec * av)
1707 {
1708
1709 free(av->argv);
1710 free(av->buf);
1711 free(av);
1712 }
1713
1714 static char **
getargv(struct procstat * procstat,struct kinfo_proc * kp,size_t nchr,int env)1715 getargv(struct procstat *procstat, struct kinfo_proc *kp, size_t nchr, int env)
1716 {
1717 int error, name[4], argc, i;
1718 struct argvec *av, **avp;
1719 enum psc_type type;
1720 size_t len;
1721 char *p, **argv;
1722
1723 assert(procstat);
1724 assert(kp);
1725 if (procstat->type == PROCSTAT_KVM) {
1726 warnx("can't use kvm access method");
1727 return (NULL);
1728 }
1729 if (procstat->type != PROCSTAT_SYSCTL &&
1730 procstat->type != PROCSTAT_CORE) {
1731 warnx("unknown access method: %d", procstat->type);
1732 return (NULL);
1733 }
1734
1735 if (nchr == 0 || nchr > ARG_MAX)
1736 nchr = ARG_MAX;
1737
1738 avp = (struct argvec **)(env ? &procstat->argv : &procstat->envv);
1739 av = *avp;
1740
1741 if (av == NULL)
1742 {
1743 av = argvec_alloc(nchr);
1744 if (av == NULL)
1745 {
1746 warn("malloc(%zu)", nchr);
1747 return (NULL);
1748 }
1749 *avp = av;
1750 } else if (av->bufsize < nchr) {
1751 av->buf = reallocf(av->buf, nchr);
1752 if (av->buf == NULL) {
1753 warn("malloc(%zu)", nchr);
1754 return (NULL);
1755 }
1756 }
1757 if (procstat->type == PROCSTAT_SYSCTL) {
1758 name[0] = CTL_KERN;
1759 name[1] = KERN_PROC;
1760 name[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS;
1761 name[3] = kp->ki_pid;
1762 len = nchr;
1763 error = sysctl(name, nitems(name), av->buf, &len, NULL, 0);
1764 if (error != 0 && errno != ESRCH && errno != EPERM)
1765 warn("sysctl(kern.proc.%s)", env ? "env" : "args");
1766 if (error != 0 || len == 0)
1767 return (NULL);
1768 } else /* procstat->type == PROCSTAT_CORE */ {
1769 type = env ? PSC_TYPE_ENVV : PSC_TYPE_ARGV;
1770 len = nchr;
1771 if (procstat_core_get(procstat->core, type, av->buf, &len)
1772 == NULL) {
1773 return (NULL);
1774 }
1775 }
1776
1777 argv = av->argv;
1778 argc = av->argc;
1779 i = 0;
1780 for (p = av->buf; p < av->buf + len; p += strlen(p) + 1) {
1781 argv[i++] = p;
1782 if (i < argc)
1783 continue;
1784 /* Grow argv. */
1785 argc += argc;
1786 argv = realloc(argv, sizeof(char *) * argc);
1787 if (argv == NULL) {
1788 warn("malloc(%zu)", sizeof(char *) * argc);
1789 return (NULL);
1790 }
1791 av->argv = argv;
1792 av->argc = argc;
1793 }
1794 argv[i] = NULL;
1795
1796 return (argv);
1797 }
1798
1799 /*
1800 * Return process command line arguments.
1801 */
1802 char **
procstat_getargv(struct procstat * procstat,struct kinfo_proc * p,size_t nchr)1803 procstat_getargv(struct procstat *procstat, struct kinfo_proc *p, size_t nchr)
1804 {
1805
1806 return (getargv(procstat, p, nchr, 0));
1807 }
1808
1809 /*
1810 * Free the buffer allocated by procstat_getargv().
1811 */
1812 void
procstat_freeargv(struct procstat * procstat)1813 procstat_freeargv(struct procstat *procstat)
1814 {
1815
1816 if (procstat->argv != NULL) {
1817 argvec_free(procstat->argv);
1818 procstat->argv = NULL;
1819 }
1820 }
1821
1822 /*
1823 * Return process environment.
1824 */
1825 char **
procstat_getenvv(struct procstat * procstat,struct kinfo_proc * p,size_t nchr)1826 procstat_getenvv(struct procstat *procstat, struct kinfo_proc *p, size_t nchr)
1827 {
1828
1829 return (getargv(procstat, p, nchr, 1));
1830 }
1831
1832 /*
1833 * Free the buffer allocated by procstat_getenvv().
1834 */
1835 void
procstat_freeenvv(struct procstat * procstat)1836 procstat_freeenvv(struct procstat *procstat)
1837 {
1838 if (procstat->envv != NULL) {
1839 argvec_free(procstat->envv);
1840 procstat->envv = NULL;
1841 }
1842 }
1843
1844 static struct kinfo_vmentry *
kinfo_getvmmap_core(struct procstat_core * core,int * cntp)1845 kinfo_getvmmap_core(struct procstat_core *core, int *cntp)
1846 {
1847 int cnt;
1848 size_t len;
1849 char *buf, *bp, *eb;
1850 struct kinfo_vmentry *kiv, *kp, *kv;
1851
1852 buf = procstat_core_get(core, PSC_TYPE_VMMAP, NULL, &len);
1853 if (buf == NULL)
1854 return (NULL);
1855
1856 /*
1857 * XXXMG: The code below is just copy&past from libutil.
1858 * The code duplication can be avoided if libutil
1859 * is extended to provide something like:
1860 * struct kinfo_vmentry *kinfo_getvmmap_from_buf(const char *buf,
1861 * size_t len, int *cntp);
1862 */
1863
1864 /* Pass 1: count items */
1865 cnt = 0;
1866 bp = buf;
1867 eb = buf + len;
1868 while (bp < eb) {
1869 kv = (struct kinfo_vmentry *)(uintptr_t)bp;
1870 if (kv->kve_structsize == 0)
1871 break;
1872 bp += kv->kve_structsize;
1873 cnt++;
1874 }
1875
1876 kiv = calloc(cnt, sizeof(*kiv));
1877 if (kiv == NULL) {
1878 free(buf);
1879 return (NULL);
1880 }
1881 bp = buf;
1882 eb = buf + len;
1883 kp = kiv;
1884 /* Pass 2: unpack */
1885 while (bp < eb) {
1886 kv = (struct kinfo_vmentry *)(uintptr_t)bp;
1887 if (kv->kve_structsize == 0)
1888 break;
1889 /* Copy/expand into pre-zeroed buffer */
1890 memcpy(kp, kv, kv->kve_structsize);
1891 /* Advance to next packed record */
1892 bp += kv->kve_structsize;
1893 /* Set field size to fixed length, advance */
1894 kp->kve_structsize = sizeof(*kp);
1895 kp++;
1896 }
1897 free(buf);
1898 *cntp = cnt;
1899 return (kiv); /* Caller must free() return value */
1900 }
1901
1902 struct kinfo_vmentry *
procstat_getvmmap(struct procstat * procstat,struct kinfo_proc * kp,unsigned int * cntp)1903 procstat_getvmmap(struct procstat *procstat, struct kinfo_proc *kp,
1904 unsigned int *cntp)
1905 {
1906
1907 switch(procstat->type) {
1908 case PROCSTAT_KVM:
1909 warnx("kvm method is not supported");
1910 return (NULL);
1911 case PROCSTAT_SYSCTL:
1912 return (kinfo_getvmmap(kp->ki_pid, cntp));
1913 case PROCSTAT_CORE:
1914 return (kinfo_getvmmap_core(procstat->core, cntp));
1915 default:
1916 warnx("unknown access method: %d", procstat->type);
1917 return (NULL);
1918 }
1919 }
1920
1921 void
procstat_freevmmap(struct procstat * procstat __unused,struct kinfo_vmentry * vmmap)1922 procstat_freevmmap(struct procstat *procstat __unused,
1923 struct kinfo_vmentry *vmmap)
1924 {
1925
1926 free(vmmap);
1927 }
1928
1929 static gid_t *
procstat_getgroups_kvm(kvm_t * kd,struct kinfo_proc * kp,unsigned int * cntp)1930 procstat_getgroups_kvm(kvm_t *kd, struct kinfo_proc *kp, unsigned int *cntp)
1931 {
1932 struct proc proc;
1933 struct ucred ucred;
1934 gid_t *groups;
1935 size_t len;
1936
1937 assert(kd != NULL);
1938 assert(kp != NULL);
1939 if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
1940 sizeof(proc))) {
1941 warnx("can't read proc struct at %p for pid %d",
1942 kp->ki_paddr, kp->ki_pid);
1943 return (NULL);
1944 }
1945 if (proc.p_ucred == NOCRED)
1946 return (NULL);
1947 if (!kvm_read_all(kd, (unsigned long)proc.p_ucred, &ucred,
1948 sizeof(ucred))) {
1949 warnx("can't read ucred struct at %p for pid %d",
1950 proc.p_ucred, kp->ki_pid);
1951 return (NULL);
1952 }
1953 len = ucred.cr_ngroups * sizeof(gid_t);
1954 groups = malloc(len);
1955 if (groups == NULL) {
1956 warn("malloc(%zu)", len);
1957 return (NULL);
1958 }
1959 if (!kvm_read_all(kd, (unsigned long)ucred.cr_groups, groups, len)) {
1960 warnx("can't read groups at %p for pid %d",
1961 ucred.cr_groups, kp->ki_pid);
1962 free(groups);
1963 return (NULL);
1964 }
1965 *cntp = ucred.cr_ngroups;
1966 return (groups);
1967 }
1968
1969 static gid_t *
procstat_getgroups_sysctl(pid_t pid,unsigned int * cntp)1970 procstat_getgroups_sysctl(pid_t pid, unsigned int *cntp)
1971 {
1972 int mib[4];
1973 size_t len;
1974 gid_t *groups;
1975
1976 mib[0] = CTL_KERN;
1977 mib[1] = KERN_PROC;
1978 mib[2] = KERN_PROC_GROUPS;
1979 mib[3] = pid;
1980 len = (sysconf(_SC_NGROUPS_MAX) + 1) * sizeof(gid_t);
1981 groups = malloc(len);
1982 if (groups == NULL) {
1983 warn("malloc(%zu)", len);
1984 return (NULL);
1985 }
1986 if (sysctl(mib, nitems(mib), groups, &len, NULL, 0) == -1) {
1987 warn("sysctl: kern.proc.groups: %d", pid);
1988 free(groups);
1989 return (NULL);
1990 }
1991 *cntp = len / sizeof(gid_t);
1992 return (groups);
1993 }
1994
1995 static gid_t *
procstat_getgroups_core(struct procstat_core * core,unsigned int * cntp)1996 procstat_getgroups_core(struct procstat_core *core, unsigned int *cntp)
1997 {
1998 size_t len;
1999 gid_t *groups;
2000
2001 groups = procstat_core_get(core, PSC_TYPE_GROUPS, NULL, &len);
2002 if (groups == NULL)
2003 return (NULL);
2004 *cntp = len / sizeof(gid_t);
2005 return (groups);
2006 }
2007
2008 gid_t *
procstat_getgroups(struct procstat * procstat,struct kinfo_proc * kp,unsigned int * cntp)2009 procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
2010 unsigned int *cntp)
2011 {
2012 switch(procstat->type) {
2013 case PROCSTAT_KVM:
2014 return (procstat_getgroups_kvm(procstat->kd, kp, cntp));
2015 case PROCSTAT_SYSCTL:
2016 return (procstat_getgroups_sysctl(kp->ki_pid, cntp));
2017 case PROCSTAT_CORE:
2018 return (procstat_getgroups_core(procstat->core, cntp));
2019 default:
2020 warnx("unknown access method: %d", procstat->type);
2021 return (NULL);
2022 }
2023 }
2024
2025 void
procstat_freegroups(struct procstat * procstat __unused,gid_t * groups)2026 procstat_freegroups(struct procstat *procstat __unused, gid_t *groups)
2027 {
2028
2029 free(groups);
2030 }
2031
2032 static int
procstat_getumask_kvm(kvm_t * kd,struct kinfo_proc * kp,unsigned short * maskp)2033 procstat_getumask_kvm(kvm_t *kd, struct kinfo_proc *kp, unsigned short *maskp)
2034 {
2035 struct filedesc fd;
2036
2037 assert(kd != NULL);
2038 assert(kp != NULL);
2039 if (kp->ki_fd == NULL)
2040 return (-1);
2041 if (!kvm_read_all(kd, (unsigned long)kp->ki_fd, &fd, sizeof(fd))) {
2042 warnx("can't read filedesc at %p for pid %d", kp->ki_fd,
2043 kp->ki_pid);
2044 return (-1);
2045 }
2046 *maskp = fd.fd_cmask;
2047 return (0);
2048 }
2049
2050 static int
procstat_getumask_sysctl(pid_t pid,unsigned short * maskp)2051 procstat_getumask_sysctl(pid_t pid, unsigned short *maskp)
2052 {
2053 int error;
2054 int mib[4];
2055 size_t len;
2056
2057 mib[0] = CTL_KERN;
2058 mib[1] = KERN_PROC;
2059 mib[2] = KERN_PROC_UMASK;
2060 mib[3] = pid;
2061 len = sizeof(*maskp);
2062 error = sysctl(mib, nitems(mib), maskp, &len, NULL, 0);
2063 if (error != 0 && errno != ESRCH && errno != EPERM)
2064 warn("sysctl: kern.proc.umask: %d", pid);
2065 return (error);
2066 }
2067
2068 static int
procstat_getumask_core(struct procstat_core * core,unsigned short * maskp)2069 procstat_getumask_core(struct procstat_core *core, unsigned short *maskp)
2070 {
2071 size_t len;
2072 unsigned short *buf;
2073
2074 buf = procstat_core_get(core, PSC_TYPE_UMASK, NULL, &len);
2075 if (buf == NULL)
2076 return (-1);
2077 if (len < sizeof(*maskp)) {
2078 free(buf);
2079 return (-1);
2080 }
2081 *maskp = *buf;
2082 free(buf);
2083 return (0);
2084 }
2085
2086 int
procstat_getumask(struct procstat * procstat,struct kinfo_proc * kp,unsigned short * maskp)2087 procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
2088 unsigned short *maskp)
2089 {
2090 switch(procstat->type) {
2091 case PROCSTAT_KVM:
2092 return (procstat_getumask_kvm(procstat->kd, kp, maskp));
2093 case PROCSTAT_SYSCTL:
2094 return (procstat_getumask_sysctl(kp->ki_pid, maskp));
2095 case PROCSTAT_CORE:
2096 return (procstat_getumask_core(procstat->core, maskp));
2097 default:
2098 warnx("unknown access method: %d", procstat->type);
2099 return (-1);
2100 }
2101 }
2102
2103 static int
procstat_getrlimit_kvm(kvm_t * kd,struct kinfo_proc * kp,int which,struct rlimit * rlimit)2104 procstat_getrlimit_kvm(kvm_t *kd, struct kinfo_proc *kp, int which,
2105 struct rlimit* rlimit)
2106 {
2107 struct proc proc;
2108 unsigned long offset;
2109
2110 assert(kd != NULL);
2111 assert(kp != NULL);
2112 assert(which >= 0 && which < RLIM_NLIMITS);
2113 if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
2114 sizeof(proc))) {
2115 warnx("can't read proc struct at %p for pid %d",
2116 kp->ki_paddr, kp->ki_pid);
2117 return (-1);
2118 }
2119 if (proc.p_limit == NULL)
2120 return (-1);
2121 offset = (unsigned long)proc.p_limit + sizeof(struct rlimit) * which;
2122 if (!kvm_read_all(kd, offset, rlimit, sizeof(*rlimit))) {
2123 warnx("can't read rlimit struct at %p for pid %d",
2124 (void *)offset, kp->ki_pid);
2125 return (-1);
2126 }
2127 return (0);
2128 }
2129
2130 static int
procstat_getrlimit_sysctl(pid_t pid,int which,struct rlimit * rlimit)2131 procstat_getrlimit_sysctl(pid_t pid, int which, struct rlimit* rlimit)
2132 {
2133 int error, name[5];
2134 size_t len;
2135
2136 name[0] = CTL_KERN;
2137 name[1] = KERN_PROC;
2138 name[2] = KERN_PROC_RLIMIT;
2139 name[3] = pid;
2140 name[4] = which;
2141 len = sizeof(struct rlimit);
2142 error = sysctl(name, nitems(name), rlimit, &len, NULL, 0);
2143 if (error < 0 && errno != ESRCH) {
2144 warn("sysctl: kern.proc.rlimit: %d", pid);
2145 return (-1);
2146 }
2147 if (error < 0 || len != sizeof(struct rlimit))
2148 return (-1);
2149 return (0);
2150 }
2151
2152 static int
procstat_getrlimit_core(struct procstat_core * core,int which,struct rlimit * rlimit)2153 procstat_getrlimit_core(struct procstat_core *core, int which,
2154 struct rlimit* rlimit)
2155 {
2156 size_t len;
2157 struct rlimit* rlimits;
2158
2159 if (which < 0 || which >= RLIM_NLIMITS) {
2160 errno = EINVAL;
2161 warn("getrlimit: which");
2162 return (-1);
2163 }
2164 rlimits = procstat_core_get(core, PSC_TYPE_RLIMIT, NULL, &len);
2165 if (rlimits == NULL)
2166 return (-1);
2167 if (len < sizeof(struct rlimit) * RLIM_NLIMITS) {
2168 free(rlimits);
2169 return (-1);
2170 }
2171 *rlimit = rlimits[which];
2172 return (0);
2173 }
2174
2175 int
procstat_getrlimit(struct procstat * procstat,struct kinfo_proc * kp,int which,struct rlimit * rlimit)2176 procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp, int which,
2177 struct rlimit* rlimit)
2178 {
2179 switch(procstat->type) {
2180 case PROCSTAT_KVM:
2181 return (procstat_getrlimit_kvm(procstat->kd, kp, which,
2182 rlimit));
2183 case PROCSTAT_SYSCTL:
2184 return (procstat_getrlimit_sysctl(kp->ki_pid, which, rlimit));
2185 case PROCSTAT_CORE:
2186 return (procstat_getrlimit_core(procstat->core, which, rlimit));
2187 default:
2188 warnx("unknown access method: %d", procstat->type);
2189 return (-1);
2190 }
2191 }
2192
2193 static int
procstat_getpathname_sysctl(pid_t pid,char * pathname,size_t maxlen)2194 procstat_getpathname_sysctl(pid_t pid, char *pathname, size_t maxlen)
2195 {
2196 int error, name[4];
2197 size_t len;
2198
2199 name[0] = CTL_KERN;
2200 name[1] = KERN_PROC;
2201 name[2] = KERN_PROC_PATHNAME;
2202 name[3] = pid;
2203 len = maxlen;
2204 error = sysctl(name, nitems(name), pathname, &len, NULL, 0);
2205 if (error != 0 && errno != ESRCH)
2206 warn("sysctl: kern.proc.pathname: %d", pid);
2207 if (len == 0)
2208 pathname[0] = '\0';
2209 return (error);
2210 }
2211
2212 static int
procstat_getpathname_core(struct procstat_core * core,char * pathname,size_t maxlen)2213 procstat_getpathname_core(struct procstat_core *core, char *pathname,
2214 size_t maxlen)
2215 {
2216 struct kinfo_file *files;
2217 int cnt, i, result;
2218
2219 files = kinfo_getfile_core(core, &cnt);
2220 if (files == NULL)
2221 return (-1);
2222 result = -1;
2223 for (i = 0; i < cnt; i++) {
2224 if (files[i].kf_fd != KF_FD_TYPE_TEXT)
2225 continue;
2226 strncpy(pathname, files[i].kf_path, maxlen);
2227 result = 0;
2228 break;
2229 }
2230 free(files);
2231 return (result);
2232 }
2233
2234 int
procstat_getpathname(struct procstat * procstat,struct kinfo_proc * kp,char * pathname,size_t maxlen)2235 procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
2236 char *pathname, size_t maxlen)
2237 {
2238 switch(procstat->type) {
2239 case PROCSTAT_KVM:
2240 /* XXX: Return empty string. */
2241 if (maxlen > 0)
2242 pathname[0] = '\0';
2243 return (0);
2244 case PROCSTAT_SYSCTL:
2245 return (procstat_getpathname_sysctl(kp->ki_pid, pathname,
2246 maxlen));
2247 case PROCSTAT_CORE:
2248 return (procstat_getpathname_core(procstat->core, pathname,
2249 maxlen));
2250 default:
2251 warnx("unknown access method: %d", procstat->type);
2252 return (-1);
2253 }
2254 }
2255
2256 static int
procstat_getosrel_kvm(kvm_t * kd,struct kinfo_proc * kp,int * osrelp)2257 procstat_getosrel_kvm(kvm_t *kd, struct kinfo_proc *kp, int *osrelp)
2258 {
2259 struct proc proc;
2260
2261 assert(kd != NULL);
2262 assert(kp != NULL);
2263 if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
2264 sizeof(proc))) {
2265 warnx("can't read proc struct at %p for pid %d",
2266 kp->ki_paddr, kp->ki_pid);
2267 return (-1);
2268 }
2269 *osrelp = proc.p_osrel;
2270 return (0);
2271 }
2272
2273 static int
procstat_getosrel_sysctl(pid_t pid,int * osrelp)2274 procstat_getosrel_sysctl(pid_t pid, int *osrelp)
2275 {
2276 int error, name[4];
2277 size_t len;
2278
2279 name[0] = CTL_KERN;
2280 name[1] = KERN_PROC;
2281 name[2] = KERN_PROC_OSREL;
2282 name[3] = pid;
2283 len = sizeof(*osrelp);
2284 error = sysctl(name, nitems(name), osrelp, &len, NULL, 0);
2285 if (error != 0 && errno != ESRCH)
2286 warn("sysctl: kern.proc.osrel: %d", pid);
2287 return (error);
2288 }
2289
2290 static int
procstat_getosrel_core(struct procstat_core * core,int * osrelp)2291 procstat_getosrel_core(struct procstat_core *core, int *osrelp)
2292 {
2293 size_t len;
2294 int *buf;
2295
2296 buf = procstat_core_get(core, PSC_TYPE_OSREL, NULL, &len);
2297 if (buf == NULL)
2298 return (-1);
2299 if (len < sizeof(*osrelp)) {
2300 free(buf);
2301 return (-1);
2302 }
2303 *osrelp = *buf;
2304 free(buf);
2305 return (0);
2306 }
2307
2308 int
procstat_getosrel(struct procstat * procstat,struct kinfo_proc * kp,int * osrelp)2309 procstat_getosrel(struct procstat *procstat, struct kinfo_proc *kp, int *osrelp)
2310 {
2311 switch(procstat->type) {
2312 case PROCSTAT_KVM:
2313 return (procstat_getosrel_kvm(procstat->kd, kp, osrelp));
2314 case PROCSTAT_SYSCTL:
2315 return (procstat_getosrel_sysctl(kp->ki_pid, osrelp));
2316 case PROCSTAT_CORE:
2317 return (procstat_getosrel_core(procstat->core, osrelp));
2318 default:
2319 warnx("unknown access method: %d", procstat->type);
2320 return (-1);
2321 }
2322 }
2323
2324 #define PROC_AUXV_MAX 256
2325
2326 #if __ELF_WORD_SIZE == 64
2327 static const char *elf32_sv_names[] = {
2328 "Linux ELF32",
2329 "FreeBSD ELF32",
2330 };
2331
2332 static int
is_elf32_sysctl(pid_t pid)2333 is_elf32_sysctl(pid_t pid)
2334 {
2335 int error, name[4];
2336 size_t len, i;
2337 static char sv_name[256];
2338
2339 name[0] = CTL_KERN;
2340 name[1] = KERN_PROC;
2341 name[2] = KERN_PROC_SV_NAME;
2342 name[3] = pid;
2343 len = sizeof(sv_name);
2344 error = sysctl(name, nitems(name), sv_name, &len, NULL, 0);
2345 if (error != 0 || len == 0)
2346 return (0);
2347 for (i = 0; i < sizeof(elf32_sv_names) / sizeof(*elf32_sv_names); i++) {
2348 if (strncmp(sv_name, elf32_sv_names[i], sizeof(sv_name)) == 0)
2349 return (1);
2350 }
2351 return (0);
2352 }
2353
2354 static Elf_Auxinfo *
procstat_getauxv32_sysctl(pid_t pid,unsigned int * cntp)2355 procstat_getauxv32_sysctl(pid_t pid, unsigned int *cntp)
2356 {
2357 Elf_Auxinfo *auxv;
2358 Elf32_Auxinfo *auxv32;
2359 void *ptr;
2360 size_t len;
2361 unsigned int i, count;
2362 int name[4];
2363
2364 name[0] = CTL_KERN;
2365 name[1] = KERN_PROC;
2366 name[2] = KERN_PROC_AUXV;
2367 name[3] = pid;
2368 len = PROC_AUXV_MAX * sizeof(Elf32_Auxinfo);
2369 auxv = NULL;
2370 auxv32 = malloc(len);
2371 if (auxv32 == NULL) {
2372 warn("malloc(%zu)", len);
2373 goto out;
2374 }
2375 if (sysctl(name, nitems(name), auxv32, &len, NULL, 0) == -1) {
2376 if (errno != ESRCH && errno != EPERM)
2377 warn("sysctl: kern.proc.auxv: %d: %d", pid, errno);
2378 goto out;
2379 }
2380 count = len / sizeof(Elf_Auxinfo);
2381 auxv = malloc(count * sizeof(Elf_Auxinfo));
2382 if (auxv == NULL) {
2383 warn("malloc(%zu)", count * sizeof(Elf_Auxinfo));
2384 goto out;
2385 }
2386 for (i = 0; i < count; i++) {
2387 /*
2388 * XXX: We expect that values for a_type on a 32-bit platform
2389 * are directly mapped to values on 64-bit one, which is not
2390 * necessarily true.
2391 */
2392 auxv[i].a_type = auxv32[i].a_type;
2393 ptr = &auxv32[i].a_un;
2394 auxv[i].a_un.a_val = *((uint32_t *)ptr);
2395 }
2396 *cntp = count;
2397 out:
2398 free(auxv32);
2399 return (auxv);
2400 }
2401 #endif /* __ELF_WORD_SIZE == 64 */
2402
2403 static Elf_Auxinfo *
procstat_getauxv_sysctl(pid_t pid,unsigned int * cntp)2404 procstat_getauxv_sysctl(pid_t pid, unsigned int *cntp)
2405 {
2406 Elf_Auxinfo *auxv;
2407 int name[4];
2408 size_t len;
2409
2410 #if __ELF_WORD_SIZE == 64
2411 if (is_elf32_sysctl(pid))
2412 return (procstat_getauxv32_sysctl(pid, cntp));
2413 #endif
2414 name[0] = CTL_KERN;
2415 name[1] = KERN_PROC;
2416 name[2] = KERN_PROC_AUXV;
2417 name[3] = pid;
2418 len = PROC_AUXV_MAX * sizeof(Elf_Auxinfo);
2419 auxv = malloc(len);
2420 if (auxv == NULL) {
2421 warn("malloc(%zu)", len);
2422 return (NULL);
2423 }
2424 if (sysctl(name, nitems(name), auxv, &len, NULL, 0) == -1) {
2425 if (errno != ESRCH && errno != EPERM)
2426 warn("sysctl: kern.proc.auxv: %d: %d", pid, errno);
2427 free(auxv);
2428 return (NULL);
2429 }
2430 *cntp = len / sizeof(Elf_Auxinfo);
2431 return (auxv);
2432 }
2433
2434 static Elf_Auxinfo *
procstat_getauxv_core(struct procstat_core * core,unsigned int * cntp)2435 procstat_getauxv_core(struct procstat_core *core, unsigned int *cntp)
2436 {
2437 Elf_Auxinfo *auxv;
2438 size_t len;
2439
2440 auxv = procstat_core_get(core, PSC_TYPE_AUXV, NULL, &len);
2441 if (auxv == NULL)
2442 return (NULL);
2443 *cntp = len / sizeof(Elf_Auxinfo);
2444 return (auxv);
2445 }
2446
2447 Elf_Auxinfo *
procstat_getauxv(struct procstat * procstat,struct kinfo_proc * kp,unsigned int * cntp)2448 procstat_getauxv(struct procstat *procstat, struct kinfo_proc *kp,
2449 unsigned int *cntp)
2450 {
2451 switch(procstat->type) {
2452 case PROCSTAT_KVM:
2453 warnx("kvm method is not supported");
2454 return (NULL);
2455 case PROCSTAT_SYSCTL:
2456 return (procstat_getauxv_sysctl(kp->ki_pid, cntp));
2457 case PROCSTAT_CORE:
2458 return (procstat_getauxv_core(procstat->core, cntp));
2459 default:
2460 warnx("unknown access method: %d", procstat->type);
2461 return (NULL);
2462 }
2463 }
2464
2465 void
procstat_freeauxv(struct procstat * procstat __unused,Elf_Auxinfo * auxv)2466 procstat_freeauxv(struct procstat *procstat __unused, Elf_Auxinfo *auxv)
2467 {
2468
2469 free(auxv);
2470 }
2471
2472 static struct kinfo_kstack *
procstat_getkstack_sysctl(pid_t pid,int * cntp)2473 procstat_getkstack_sysctl(pid_t pid, int *cntp)
2474 {
2475 struct kinfo_kstack *kkstp;
2476 int error, name[4];
2477 size_t len;
2478
2479 name[0] = CTL_KERN;
2480 name[1] = KERN_PROC;
2481 name[2] = KERN_PROC_KSTACK;
2482 name[3] = pid;
2483
2484 len = 0;
2485 error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
2486 if (error < 0 && errno != ESRCH && errno != EPERM && errno != ENOENT) {
2487 warn("sysctl: kern.proc.kstack: %d", pid);
2488 return (NULL);
2489 }
2490 if (error == -1 && errno == ENOENT) {
2491 warnx("sysctl: kern.proc.kstack unavailable"
2492 " (options DDB or options STACK required in kernel)");
2493 return (NULL);
2494 }
2495 if (error == -1)
2496 return (NULL);
2497 kkstp = malloc(len);
2498 if (kkstp == NULL) {
2499 warn("malloc(%zu)", len);
2500 return (NULL);
2501 }
2502 if (sysctl(name, nitems(name), kkstp, &len, NULL, 0) == -1) {
2503 warn("sysctl: kern.proc.pid: %d", pid);
2504 free(kkstp);
2505 return (NULL);
2506 }
2507 *cntp = len / sizeof(*kkstp);
2508
2509 return (kkstp);
2510 }
2511
2512 struct kinfo_kstack *
procstat_getkstack(struct procstat * procstat,struct kinfo_proc * kp,unsigned int * cntp)2513 procstat_getkstack(struct procstat *procstat, struct kinfo_proc *kp,
2514 unsigned int *cntp)
2515 {
2516 switch(procstat->type) {
2517 case PROCSTAT_KVM:
2518 warnx("kvm method is not supported");
2519 return (NULL);
2520 case PROCSTAT_SYSCTL:
2521 return (procstat_getkstack_sysctl(kp->ki_pid, cntp));
2522 case PROCSTAT_CORE:
2523 warnx("core method is not supported");
2524 return (NULL);
2525 default:
2526 warnx("unknown access method: %d", procstat->type);
2527 return (NULL);
2528 }
2529 }
2530
2531 void
procstat_freekstack(struct procstat * procstat __unused,struct kinfo_kstack * kkstp)2532 procstat_freekstack(struct procstat *procstat __unused,
2533 struct kinfo_kstack *kkstp)
2534 {
2535
2536 free(kkstp);
2537 }
2538