2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 2017 Dell EMC
5 * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
6 * Copyright (c) 1988, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
44 #include <sys/resourcevar.h>
46 #include <sys/ucred.h>
51 #include <sys/vnode.h>
52 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/domain.h>
56 #include <sys/protosw.h>
59 #include <sys/unpcb.h>
60 #include <sys/sysctl.h>
62 #include <sys/filedesc.h>
63 #include <sys/queue.h>
69 #include <sys/capsicum.h>
70 #include <sys/ptrace.h>
72 #include <sys/mount.h>
74 #include <ufs/ufs/quota.h>
75 #include <ufs/ufs/inode.h>
76 #include <fs/devfs/devfs.h>
77 #include <fs/devfs/devfs_int.h>
79 #include <nfs/nfsproto.h>
80 #include <nfsclient/nfs.h>
81 #include <nfsclient/nfsnode.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_object.h>
87 #include <net/route.h>
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
92 #include <netinet/in_pcb.h>
110 #include <libprocstat.h>
111 #include "libprocstat_internal.h"
112 #include "common_kvm.h"
115 int statfs(const char *, struct statfs *); /* XXX */
117 #define PROCSTAT_KVM 1
118 #define PROCSTAT_SYSCTL 2
119 #define PROCSTAT_CORE 3
121 static char **getargv(struct procstat *procstat, struct kinfo_proc *kp,
122 size_t nchr, int env);
123 static char *getmnton(kvm_t *kd, struct mount *m);
124 static struct kinfo_vmentry * kinfo_getvmmap_core(struct procstat_core *core,
126 static Elf_Auxinfo *procstat_getauxv_core(struct procstat_core *core,
128 static Elf_Auxinfo *procstat_getauxv_sysctl(pid_t pid, unsigned int *cntp);
129 static struct filestat_list *procstat_getfiles_kvm(
130 struct procstat *procstat, struct kinfo_proc *kp, int mmapped);
131 static struct filestat_list *procstat_getfiles_sysctl(
132 struct procstat *procstat, struct kinfo_proc *kp, int mmapped);
133 static int procstat_get_pipe_info_sysctl(struct filestat *fst,
134 struct pipestat *pipe, char *errbuf);
135 static int procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst,
136 struct pipestat *pipe, char *errbuf);
137 static int procstat_get_pts_info_sysctl(struct filestat *fst,
138 struct ptsstat *pts, char *errbuf);
139 static int procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst,
140 struct ptsstat *pts, char *errbuf);
141 static int procstat_get_sem_info_sysctl(struct filestat *fst,
142 struct semstat *sem, char *errbuf);
143 static int procstat_get_sem_info_kvm(kvm_t *kd, struct filestat *fst,
144 struct semstat *sem, char *errbuf);
145 static int procstat_get_shm_info_sysctl(struct filestat *fst,
146 struct shmstat *shm, char *errbuf);
147 static int procstat_get_shm_info_kvm(kvm_t *kd, struct filestat *fst,
148 struct shmstat *shm, char *errbuf);
149 static int procstat_get_socket_info_sysctl(struct filestat *fst,
150 struct sockstat *sock, char *errbuf);
151 static int procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst,
152 struct sockstat *sock, char *errbuf);
153 static int to_filestat_flags(int flags);
154 static int procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
155 struct vnstat *vn, char *errbuf);
156 static int procstat_get_vnode_info_sysctl(struct filestat *fst,
157 struct vnstat *vn, char *errbuf);
158 static gid_t *procstat_getgroups_core(struct procstat_core *core,
159 unsigned int *count);
160 static gid_t * procstat_getgroups_kvm(kvm_t *kd, struct kinfo_proc *kp,
161 unsigned int *count);
162 static gid_t *procstat_getgroups_sysctl(pid_t pid, unsigned int *count);
163 static struct kinfo_kstack *procstat_getkstack_sysctl(pid_t pid,
165 static int procstat_getosrel_core(struct procstat_core *core,
167 static int procstat_getosrel_kvm(kvm_t *kd, struct kinfo_proc *kp,
169 static int procstat_getosrel_sysctl(pid_t pid, int *osrelp);
170 static int procstat_getpathname_core(struct procstat_core *core,
171 char *pathname, size_t maxlen);
172 static int procstat_getpathname_sysctl(pid_t pid, char *pathname,
174 static int procstat_getrlimit_core(struct procstat_core *core, int which,
175 struct rlimit* rlimit);
176 static int procstat_getrlimit_kvm(kvm_t *kd, struct kinfo_proc *kp,
177 int which, struct rlimit* rlimit);
178 static int procstat_getrlimit_sysctl(pid_t pid, int which,
179 struct rlimit* rlimit);
180 static int procstat_getumask_core(struct procstat_core *core,
181 unsigned short *maskp);
182 static int procstat_getumask_kvm(kvm_t *kd, struct kinfo_proc *kp,
183 unsigned short *maskp);
184 static int procstat_getumask_sysctl(pid_t pid, unsigned short *maskp);
185 static int vntype2psfsttype(int type);
188 procstat_close(struct procstat *procstat)
192 if (procstat->type == PROCSTAT_KVM)
193 kvm_close(procstat->kd);
194 else if (procstat->type == PROCSTAT_CORE)
195 procstat_core_close(procstat->core);
196 procstat_freeargv(procstat);
197 procstat_freeenvv(procstat);
202 procstat_open_sysctl(void)
204 struct procstat *procstat;
206 procstat = calloc(1, sizeof(*procstat));
207 if (procstat == NULL) {
211 procstat->type = PROCSTAT_SYSCTL;
216 procstat_open_kvm(const char *nlistf, const char *memf)
218 struct procstat *procstat;
220 char buf[_POSIX2_LINE_MAX];
222 procstat = calloc(1, sizeof(*procstat));
223 if (procstat == NULL) {
227 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
229 warnx("kvm_openfiles(): %s", buf);
233 procstat->type = PROCSTAT_KVM;
239 procstat_open_core(const char *filename)
241 struct procstat *procstat;
242 struct procstat_core *core;
244 procstat = calloc(1, sizeof(*procstat));
245 if (procstat == NULL) {
249 core = procstat_core_open(filename);
254 procstat->type = PROCSTAT_CORE;
255 procstat->core = core;
260 procstat_getprocs(struct procstat *procstat, int what, int arg,
263 struct kinfo_proc *p0, *p;
272 if (procstat->type == PROCSTAT_KVM) {
274 p0 = kvm_getprocs(procstat->kd, what, arg, &cnt);
275 if (p0 == NULL || cnt <= 0)
278 len = *count * sizeof(*p);
281 warnx("malloc(%zu)", len);
286 } else if (procstat->type == PROCSTAT_SYSCTL) {
292 error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
293 if (error < 0 && errno != EPERM) {
294 warn("sysctl(kern.proc)");
298 warnx("no processes?");
303 p = reallocf(p, len);
305 warnx("reallocf(%zu)", len);
309 error = sysctl(name, nitems(name), p, &len, NULL, 0);
310 } while (error < 0 && errno == ENOMEM && olen == len);
311 if (error < 0 && errno != EPERM) {
312 warn("sysctl(kern.proc)");
315 /* Perform simple consistency checks. */
316 if ((len % sizeof(*p)) != 0 || p->ki_structsize != sizeof(*p)) {
317 warnx("kinfo_proc structure size mismatch (len = %zu)", len);
320 *count = len / sizeof(*p);
322 } else if (procstat->type == PROCSTAT_CORE) {
323 p = procstat_core_get(procstat->core, PSC_TYPE_PROC, NULL,
325 if ((len % sizeof(*p)) != 0 || p->ki_structsize != sizeof(*p)) {
326 warnx("kinfo_proc structure size mismatch");
329 *count = len / sizeof(*p);
332 warnx("unknown access method: %d", procstat->type);
342 procstat_freeprocs(struct procstat *procstat __unused, struct kinfo_proc *p)
350 struct filestat_list *
351 procstat_getfiles(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
354 switch(procstat->type) {
356 return (procstat_getfiles_kvm(procstat, kp, mmapped));
357 case PROCSTAT_SYSCTL:
359 return (procstat_getfiles_sysctl(procstat, kp, mmapped));
361 warnx("unknown access method: %d", procstat->type);
367 procstat_freefiles(struct procstat *procstat, struct filestat_list *head)
369 struct filestat *fst, *tmp;
371 STAILQ_FOREACH_SAFE(fst, head, next, tmp) {
372 if (fst->fs_path != NULL)
377 if (procstat->vmentries != NULL) {
378 free(procstat->vmentries);
379 procstat->vmentries = NULL;
381 if (procstat->files != NULL) {
382 free(procstat->files);
383 procstat->files = NULL;
387 static struct filestat *
388 filestat_new_entry(void *typedep, int type, int fd, int fflags, int uflags,
389 int refcount, off_t offset, char *path, cap_rights_t *cap_rightsp)
391 struct filestat *entry;
393 entry = calloc(1, sizeof(*entry));
398 entry->fs_typedep = typedep;
399 entry->fs_fflags = fflags;
400 entry->fs_uflags = uflags;
402 entry->fs_type = type;
403 entry->fs_ref_count = refcount;
404 entry->fs_offset = offset;
405 entry->fs_path = path;
406 if (cap_rightsp != NULL)
407 entry->fs_cap_rights = *cap_rightsp;
409 cap_rights_init(&entry->fs_cap_rights);
413 static struct vnode *
414 getctty(kvm_t *kd, struct kinfo_proc *kp)
422 error = kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
425 warnx("can't read proc struct at %p for pid %d",
426 kp->ki_paddr, kp->ki_pid);
429 if (proc.p_pgrp == NULL)
431 error = kvm_read_all(kd, (unsigned long)proc.p_pgrp, &pgrp,
434 warnx("can't read pgrp struct at %p for pid %d",
435 proc.p_pgrp, kp->ki_pid);
438 error = kvm_read_all(kd, (unsigned long)pgrp.pg_session, &sess,
441 warnx("can't read session struct at %p for pid %d",
442 pgrp.pg_session, kp->ki_pid);
445 return (sess.s_ttyvp);
449 procstat_vm_map_reader(void *token, vm_map_entry_t addr, vm_map_entry_t dest)
454 return (kvm_read_all(kd, (unsigned long)addr, dest, sizeof(*dest)));
457 static struct filestat_list *
458 procstat_getfiles_kvm(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
461 struct filedesc filed;
463 unsigned long pwd_addr;
464 struct vm_map_entry vmentry;
465 struct vm_object object;
466 struct vmspace vmspace;
467 vm_map_entry_t entryp;
470 struct filedescent *ofiles;
471 struct filestat *entry;
472 struct filestat_list *head;
484 if (kp->ki_fd == NULL)
486 if (!kvm_read_all(kd, (unsigned long)kp->ki_fd, &filed,
488 warnx("can't read filedesc at %p", (void *)kp->ki_fd);
492 pwd_addr = (unsigned long)(FILEDESC_KVM_LOAD_PWD(&filed));
494 if (!kvm_read_all(kd, pwd_addr, &pwd, sizeof(pwd))) {
495 warnx("can't read fd_pwd at %p", (void *)pwd_addr);
502 * Allocate list head.
504 head = malloc(sizeof(*head));
509 /* root directory vnode, if one. */
512 entry = filestat_new_entry(pwd.pwd_rdir, PS_FST_TYPE_VNODE, -1,
513 PS_FST_FFLAG_READ, PS_FST_UFLAG_RDIR, 0, 0, NULL, NULL);
515 STAILQ_INSERT_TAIL(head, entry, next);
517 /* current working directory vnode. */
519 entry = filestat_new_entry(pwd.pwd_cdir, PS_FST_TYPE_VNODE, -1,
520 PS_FST_FFLAG_READ, PS_FST_UFLAG_CDIR, 0, 0, NULL, NULL);
522 STAILQ_INSERT_TAIL(head, entry, next);
524 /* jail root, if any. */
526 entry = filestat_new_entry(pwd.pwd_jdir, PS_FST_TYPE_VNODE, -1,
527 PS_FST_FFLAG_READ, PS_FST_UFLAG_JAIL, 0, 0, NULL, NULL);
529 STAILQ_INSERT_TAIL(head, entry, next);
532 /* ktrace vnode, if one */
534 entry = filestat_new_entry(kp->ki_tracep, PS_FST_TYPE_VNODE, -1,
535 PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE,
536 PS_FST_UFLAG_TRACE, 0, 0, NULL, NULL);
538 STAILQ_INSERT_TAIL(head, entry, next);
540 /* text vnode, if one */
542 entry = filestat_new_entry(kp->ki_textvp, PS_FST_TYPE_VNODE, -1,
543 PS_FST_FFLAG_READ, PS_FST_UFLAG_TEXT, 0, 0, NULL, NULL);
545 STAILQ_INSERT_TAIL(head, entry, next);
547 /* Controlling terminal. */
548 if ((vp = getctty(kd, kp)) != NULL) {
549 entry = filestat_new_entry(vp, PS_FST_TYPE_VNODE, -1,
550 PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE,
551 PS_FST_UFLAG_CTTY, 0, 0, NULL, NULL);
553 STAILQ_INSERT_TAIL(head, entry, next);
556 nfiles = filed.fd_lastfile + 1;
557 ofiles = malloc(nfiles * sizeof(struct filedescent));
558 if (ofiles == NULL) {
559 warn("malloc(%zu)", nfiles * sizeof(struct filedescent));
562 if (!kvm_read_all(kd, (unsigned long)filed.fd_ofiles, ofiles,
563 nfiles * sizeof(struct filedescent))) {
564 warnx("cannot read file structures at %p",
565 (void *)filed.fd_ofiles);
569 for (i = 0; i <= filed.fd_lastfile; i++) {
570 if (ofiles[i].fde_file == NULL)
572 if (!kvm_read_all(kd, (unsigned long)ofiles[i].fde_file, &file,
573 sizeof(struct file))) {
574 warnx("can't read file %d at %p", i,
575 (void *)ofiles[i].fde_file);
578 switch (file.f_type) {
580 type = PS_FST_TYPE_VNODE;
584 type = PS_FST_TYPE_SOCKET;
588 type = PS_FST_TYPE_PIPE;
592 type = PS_FST_TYPE_FIFO;
597 type = PS_FST_TYPE_PTS;
602 type = PS_FST_TYPE_SEM;
606 type = PS_FST_TYPE_SHM;
610 type = PS_FST_TYPE_PROCDESC;
614 type = PS_FST_TYPE_DEV;
620 /* XXXRW: No capability rights support for kvm yet. */
621 entry = filestat_new_entry(data, type, i,
622 to_filestat_flags(file.f_flag), 0, 0, 0, NULL, NULL);
624 STAILQ_INSERT_TAIL(head, entry, next);
631 * Process mmapped files if requested.
634 if (!kvm_read_all(kd, (unsigned long)kp->ki_vmspace, &vmspace,
636 warnx("can't read vmspace at %p",
637 (void *)kp->ki_vmspace);
641 vmentry = vmspace.vm_map.header;
642 for (entryp = vm_map_entry_read_succ(kd, &vmentry, procstat_vm_map_reader);
643 entryp != NULL && entryp != &kp->ki_vmspace->vm_map.header;
644 entryp = vm_map_entry_read_succ(kd, &vmentry, procstat_vm_map_reader)) {
645 if (vmentry.eflags & MAP_ENTRY_IS_SUB_MAP)
647 if ((objp = vmentry.object.vm_object) == NULL)
649 for (; objp; objp = object.backing_object) {
650 if (!kvm_read_all(kd, (unsigned long)objp,
651 &object, sizeof(object))) {
652 warnx("can't read vm_object at %p",
658 /* We want only vnode objects. */
659 if (object.type != OBJT_VNODE)
662 prot = vmentry.protection;
664 if (prot & VM_PROT_READ)
665 fflags = PS_FST_FFLAG_READ;
666 if ((vmentry.eflags & MAP_ENTRY_COW) == 0 &&
667 prot & VM_PROT_WRITE)
668 fflags |= PS_FST_FFLAG_WRITE;
671 * Create filestat entry.
673 entry = filestat_new_entry(object.handle,
674 PS_FST_TYPE_VNODE, -1, fflags,
675 PS_FST_UFLAG_MMAP, 0, 0, NULL, NULL);
677 STAILQ_INSERT_TAIL(head, entry, next);
680 warnx("can't read vm_map_entry");
687 * kinfo types to filestat translation.
690 kinfo_type2fst(int kftype)
696 { KF_TYPE_PROCDESC, PS_FST_TYPE_PROCDESC },
697 { KF_TYPE_CRYPTO, PS_FST_TYPE_CRYPTO },
698 { KF_TYPE_DEV, PS_FST_TYPE_DEV },
699 { KF_TYPE_FIFO, PS_FST_TYPE_FIFO },
700 { KF_TYPE_KQUEUE, PS_FST_TYPE_KQUEUE },
701 { KF_TYPE_MQUEUE, PS_FST_TYPE_MQUEUE },
702 { KF_TYPE_NONE, PS_FST_TYPE_NONE },
703 { KF_TYPE_PIPE, PS_FST_TYPE_PIPE },
704 { KF_TYPE_PTS, PS_FST_TYPE_PTS },
705 { KF_TYPE_SEM, PS_FST_TYPE_SEM },
706 { KF_TYPE_SHM, PS_FST_TYPE_SHM },
707 { KF_TYPE_SOCKET, PS_FST_TYPE_SOCKET },
708 { KF_TYPE_VNODE, PS_FST_TYPE_VNODE },
709 { KF_TYPE_UNKNOWN, PS_FST_TYPE_UNKNOWN }
711 #define NKFTYPES (sizeof(kftypes2fst) / sizeof(*kftypes2fst))
714 for (i = 0; i < NKFTYPES; i++)
715 if (kftypes2fst[i].kf_type == kftype)
718 return (PS_FST_TYPE_UNKNOWN);
719 return (kftypes2fst[i].fst_type);
723 * kinfo flags to filestat translation.
726 kinfo_fflags2fst(int kfflags)
732 { KF_FLAG_APPEND, PS_FST_FFLAG_APPEND },
733 { KF_FLAG_ASYNC, PS_FST_FFLAG_ASYNC },
734 { KF_FLAG_CREAT, PS_FST_FFLAG_CREAT },
735 { KF_FLAG_DIRECT, PS_FST_FFLAG_DIRECT },
736 { KF_FLAG_EXCL, PS_FST_FFLAG_EXCL },
737 { KF_FLAG_EXEC, PS_FST_FFLAG_EXEC },
738 { KF_FLAG_EXLOCK, PS_FST_FFLAG_EXLOCK },
739 { KF_FLAG_FSYNC, PS_FST_FFLAG_SYNC },
740 { KF_FLAG_HASLOCK, PS_FST_FFLAG_HASLOCK },
741 { KF_FLAG_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW },
742 { KF_FLAG_NONBLOCK, PS_FST_FFLAG_NONBLOCK },
743 { KF_FLAG_READ, PS_FST_FFLAG_READ },
744 { KF_FLAG_SHLOCK, PS_FST_FFLAG_SHLOCK },
745 { KF_FLAG_TRUNC, PS_FST_FFLAG_TRUNC },
746 { KF_FLAG_WRITE, PS_FST_FFLAG_WRITE }
748 #define NKFFLAGS (sizeof(kfflags2fst) / sizeof(*kfflags2fst))
753 for (i = 0; i < NKFFLAGS; i++)
754 if ((kfflags & kfflags2fst[i].kf_flag) != 0)
755 flags |= kfflags2fst[i].fst_flag;
760 kinfo_uflags2fst(int fd)
764 case KF_FD_TYPE_CTTY:
765 return (PS_FST_UFLAG_CTTY);
767 return (PS_FST_UFLAG_CDIR);
768 case KF_FD_TYPE_JAIL:
769 return (PS_FST_UFLAG_JAIL);
770 case KF_FD_TYPE_TEXT:
771 return (PS_FST_UFLAG_TEXT);
772 case KF_FD_TYPE_TRACE:
773 return (PS_FST_UFLAG_TRACE);
774 case KF_FD_TYPE_ROOT:
775 return (PS_FST_UFLAG_RDIR);
780 static struct kinfo_file *
781 kinfo_getfile_core(struct procstat_core *core, int *cntp)
786 struct kinfo_file *kif, *kp, *kf;
788 buf = procstat_core_get(core, PSC_TYPE_FILES, NULL, &len);
792 * XXXMG: The code below is just copy&past from libutil.
793 * The code duplication can be avoided if libutil
794 * is extended to provide something like:
795 * struct kinfo_file *kinfo_getfile_from_buf(const char *buf,
796 * size_t len, int *cntp);
799 /* Pass 1: count items */
804 kf = (struct kinfo_file *)(uintptr_t)bp;
805 if (kf->kf_structsize == 0)
807 bp += kf->kf_structsize;
811 kif = calloc(cnt, sizeof(*kif));
821 kf = (struct kinfo_file *)(uintptr_t)bp;
822 if (kf->kf_structsize == 0)
824 /* Copy/expand into pre-zeroed buffer */
825 memcpy(kp, kf, kf->kf_structsize);
826 /* Advance to next packed record */
827 bp += kf->kf_structsize;
828 /* Set field size to fixed length, advance */
829 kp->kf_structsize = sizeof(*kp);
834 return (kif); /* Caller must free() return value */
837 static struct filestat_list *
838 procstat_getfiles_sysctl(struct procstat *procstat, struct kinfo_proc *kp,
841 struct kinfo_file *kif, *files;
842 struct kinfo_vmentry *kve, *vmentries;
843 struct filestat_list *head;
844 struct filestat *entry;
850 cap_rights_t cap_rights;
853 if (kp->ki_fd == NULL)
855 switch(procstat->type) {
856 case PROCSTAT_SYSCTL:
857 files = kinfo_getfile(kp->ki_pid, &cnt);
860 files = kinfo_getfile_core(procstat->core, &cnt);
863 assert(!"invalid type");
865 if (files == NULL && errno != EPERM) {
866 warn("kinfo_getfile()");
869 procstat->files = files;
872 * Allocate list head.
874 head = malloc(sizeof(*head));
878 for (i = 0; i < cnt; i++) {
881 type = kinfo_type2fst(kif->kf_type);
882 fd = kif->kf_fd >= 0 ? kif->kf_fd : -1;
883 fflags = kinfo_fflags2fst(kif->kf_flags);
884 uflags = kinfo_uflags2fst(kif->kf_fd);
885 refcount = kif->kf_ref_count;
886 offset = kif->kf_offset;
887 if (*kif->kf_path != '\0')
888 path = strdup(kif->kf_path);
891 cap_rights = kif->kf_cap_rights;
894 * Create filestat entry.
896 entry = filestat_new_entry(kif, type, fd, fflags, uflags,
897 refcount, offset, path, &cap_rights);
899 STAILQ_INSERT_TAIL(head, entry, next);
902 vmentries = procstat_getvmmap(procstat, kp, &cnt);
903 procstat->vmentries = vmentries;
904 if (vmentries == NULL || cnt == 0)
906 for (i = 0; i < cnt; i++) {
908 if (kve->kve_type != KVME_TYPE_VNODE)
911 if (kve->kve_protection & KVME_PROT_READ)
912 fflags = PS_FST_FFLAG_READ;
913 if ((kve->kve_flags & KVME_FLAG_COW) == 0 &&
914 kve->kve_protection & KVME_PROT_WRITE)
915 fflags |= PS_FST_FFLAG_WRITE;
916 offset = kve->kve_offset;
917 refcount = kve->kve_ref_count;
918 if (*kve->kve_path != '\0')
919 path = strdup(kve->kve_path);
922 entry = filestat_new_entry(kve, PS_FST_TYPE_VNODE, -1,
923 fflags, PS_FST_UFLAG_MMAP, refcount, offset, path,
926 STAILQ_INSERT_TAIL(head, entry, next);
934 procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst,
935 struct pipestat *ps, char *errbuf)
939 if (procstat->type == PROCSTAT_KVM) {
940 return (procstat_get_pipe_info_kvm(procstat->kd, fst, ps,
942 } else if (procstat->type == PROCSTAT_SYSCTL ||
943 procstat->type == PROCSTAT_CORE) {
944 return (procstat_get_pipe_info_sysctl(fst, ps, errbuf));
946 warnx("unknown access method: %d", procstat->type);
948 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
954 procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst,
955 struct pipestat *ps, char *errbuf)
963 bzero(ps, sizeof(*ps));
964 pipep = fst->fs_typedep;
967 if (!kvm_read_all(kd, (unsigned long)pipep, &pi, sizeof(struct pipe))) {
968 warnx("can't read pipe at %p", (void *)pipep);
971 ps->addr = (uintptr_t)pipep;
972 ps->peer = (uintptr_t)pi.pipe_peer;
973 ps->buffer_cnt = pi.pipe_buffer.cnt;
978 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
983 procstat_get_pipe_info_sysctl(struct filestat *fst, struct pipestat *ps,
984 char *errbuf __unused)
986 struct kinfo_file *kif;
990 bzero(ps, sizeof(*ps));
991 kif = fst->fs_typedep;
994 ps->addr = kif->kf_un.kf_pipe.kf_pipe_addr;
995 ps->peer = kif->kf_un.kf_pipe.kf_pipe_peer;
996 ps->buffer_cnt = kif->kf_un.kf_pipe.kf_pipe_buffer_cnt;
1001 procstat_get_pts_info(struct procstat *procstat, struct filestat *fst,
1002 struct ptsstat *pts, char *errbuf)
1006 if (procstat->type == PROCSTAT_KVM) {
1007 return (procstat_get_pts_info_kvm(procstat->kd, fst, pts,
1009 } else if (procstat->type == PROCSTAT_SYSCTL ||
1010 procstat->type == PROCSTAT_CORE) {
1011 return (procstat_get_pts_info_sysctl(fst, pts, errbuf));
1013 warnx("unknown access method: %d", procstat->type);
1015 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1021 procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst,
1022 struct ptsstat *pts, char *errbuf)
1030 bzero(pts, sizeof(*pts));
1031 ttyp = fst->fs_typedep;
1034 if (!kvm_read_all(kd, (unsigned long)ttyp, &tty, sizeof(struct tty))) {
1035 warnx("can't read tty at %p", (void *)ttyp);
1038 pts->dev = dev2udev(kd, tty.t_dev);
1039 (void)kdevtoname(kd, tty.t_dev, pts->devname);
1044 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1049 procstat_get_pts_info_sysctl(struct filestat *fst, struct ptsstat *pts,
1050 char *errbuf __unused)
1052 struct kinfo_file *kif;
1056 bzero(pts, sizeof(*pts));
1057 kif = fst->fs_typedep;
1060 pts->dev = kif->kf_un.kf_pts.kf_pts_dev;
1061 strlcpy(pts->devname, kif->kf_path, sizeof(pts->devname));
1066 procstat_get_sem_info(struct procstat *procstat, struct filestat *fst,
1067 struct semstat *sem, char *errbuf)
1071 if (procstat->type == PROCSTAT_KVM) {
1072 return (procstat_get_sem_info_kvm(procstat->kd, fst, sem,
1074 } else if (procstat->type == PROCSTAT_SYSCTL ||
1075 procstat->type == PROCSTAT_CORE) {
1076 return (procstat_get_sem_info_sysctl(fst, sem, errbuf));
1078 warnx("unknown access method: %d", procstat->type);
1080 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1086 procstat_get_sem_info_kvm(kvm_t *kd, struct filestat *fst,
1087 struct semstat *sem, char *errbuf)
1097 bzero(sem, sizeof(*sem));
1098 ksemp = fst->fs_typedep;
1101 if (!kvm_read_all(kd, (unsigned long)ksemp, &ksem,
1102 sizeof(struct ksem))) {
1103 warnx("can't read ksem at %p", (void *)ksemp);
1106 sem->mode = S_IFREG | ksem.ks_mode;
1107 sem->value = ksem.ks_value;
1108 if (fst->fs_path == NULL && ksem.ks_path != NULL) {
1109 path = malloc(MAXPATHLEN);
1110 for (i = 0; i < MAXPATHLEN - 1; i++) {
1111 if (!kvm_read_all(kd, (unsigned long)ksem.ks_path + i,
1114 if (path[i] == '\0')
1121 fst->fs_path = path;
1127 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1132 procstat_get_sem_info_sysctl(struct filestat *fst, struct semstat *sem,
1133 char *errbuf __unused)
1135 struct kinfo_file *kif;
1139 bzero(sem, sizeof(*sem));
1140 kif = fst->fs_typedep;
1143 sem->value = kif->kf_un.kf_sem.kf_sem_value;
1144 sem->mode = kif->kf_un.kf_sem.kf_sem_mode;
1149 procstat_get_shm_info(struct procstat *procstat, struct filestat *fst,
1150 struct shmstat *shm, char *errbuf)
1154 if (procstat->type == PROCSTAT_KVM) {
1155 return (procstat_get_shm_info_kvm(procstat->kd, fst, shm,
1157 } else if (procstat->type == PROCSTAT_SYSCTL ||
1158 procstat->type == PROCSTAT_CORE) {
1159 return (procstat_get_shm_info_sysctl(fst, shm, errbuf));
1161 warnx("unknown access method: %d", procstat->type);
1163 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1169 procstat_get_shm_info_kvm(kvm_t *kd, struct filestat *fst,
1170 struct shmstat *shm, char *errbuf)
1180 bzero(shm, sizeof(*shm));
1181 shmfdp = fst->fs_typedep;
1184 if (!kvm_read_all(kd, (unsigned long)shmfdp, &shmfd,
1185 sizeof(struct shmfd))) {
1186 warnx("can't read shmfd at %p", (void *)shmfdp);
1189 shm->mode = S_IFREG | shmfd.shm_mode;
1190 shm->size = shmfd.shm_size;
1191 if (fst->fs_path == NULL && shmfd.shm_path != NULL) {
1192 path = malloc(MAXPATHLEN);
1193 for (i = 0; i < MAXPATHLEN - 1; i++) {
1194 if (!kvm_read_all(kd, (unsigned long)shmfd.shm_path + i,
1197 if (path[i] == '\0')
1204 fst->fs_path = path;
1210 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1215 procstat_get_shm_info_sysctl(struct filestat *fst, struct shmstat *shm,
1216 char *errbuf __unused)
1218 struct kinfo_file *kif;
1222 bzero(shm, sizeof(*shm));
1223 kif = fst->fs_typedep;
1226 shm->size = kif->kf_un.kf_file.kf_file_size;
1227 shm->mode = kif->kf_un.kf_file.kf_file_mode;
1232 procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
1233 struct vnstat *vn, char *errbuf)
1237 if (procstat->type == PROCSTAT_KVM) {
1238 return (procstat_get_vnode_info_kvm(procstat->kd, fst, vn,
1240 } else if (procstat->type == PROCSTAT_SYSCTL ||
1241 procstat->type == PROCSTAT_CORE) {
1242 return (procstat_get_vnode_info_sysctl(fst, vn, errbuf));
1244 warnx("unknown access method: %d", procstat->type);
1246 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1252 procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
1253 struct vnstat *vn, char *errbuf)
1255 /* Filesystem specific handlers. */
1256 #define FSTYPE(fst) {#fst, fst##_filestat}
1259 int (*handler)(kvm_t *kd, struct vnode *vp,
1269 #ifdef LIBPROCSTAT_ZFS
1273 #define NTYPES (sizeof(fstypes) / sizeof(*fstypes))
1283 vp = fst->fs_typedep;
1286 error = kvm_read_all(kd, (unsigned long)vp, &vnode, sizeof(vnode));
1288 warnx("can't read vnode at %p", (void *)vp);
1291 bzero(vn, sizeof(*vn));
1292 vn->vn_type = vntype2psfsttype(vnode.v_type);
1293 if (vnode.v_type == VNON || vnode.v_type == VBAD)
1295 error = kvm_read_all(kd, (unsigned long)vnode.v_lock.lock_object.lo_name,
1296 tagstr, sizeof(tagstr));
1298 warnx("can't read lo_name at %p", (void *)vp);
1301 tagstr[sizeof(tagstr) - 1] = '\0';
1304 * Find appropriate handler.
1306 for (i = 0; i < NTYPES; i++)
1307 if (!strcmp(fstypes[i].tag, tagstr)) {
1308 if (fstypes[i].handler(kd, &vnode, vn) != 0) {
1315 snprintf(errbuf, _POSIX2_LINE_MAX, "?(%s)", tagstr);
1318 vn->vn_mntdir = getmnton(kd, vnode.v_mount);
1319 if ((vnode.v_type == VBLK || vnode.v_type == VCHR) &&
1320 vnode.v_rdev != NULL){
1321 vn->vn_dev = dev2udev(kd, vnode.v_rdev);
1322 (void)kdevtoname(kd, vnode.v_rdev, vn->vn_devname);
1330 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1335 * kinfo vnode type to filestat translation.
1338 kinfo_vtype2fst(int kfvtype)
1343 } kfvtypes2fst[] = {
1344 { KF_VTYPE_VBAD, PS_FST_VTYPE_VBAD },
1345 { KF_VTYPE_VBLK, PS_FST_VTYPE_VBLK },
1346 { KF_VTYPE_VCHR, PS_FST_VTYPE_VCHR },
1347 { KF_VTYPE_VDIR, PS_FST_VTYPE_VDIR },
1348 { KF_VTYPE_VFIFO, PS_FST_VTYPE_VFIFO },
1349 { KF_VTYPE_VLNK, PS_FST_VTYPE_VLNK },
1350 { KF_VTYPE_VNON, PS_FST_VTYPE_VNON },
1351 { KF_VTYPE_VREG, PS_FST_VTYPE_VREG },
1352 { KF_VTYPE_VSOCK, PS_FST_VTYPE_VSOCK }
1354 #define NKFVTYPES (sizeof(kfvtypes2fst) / sizeof(*kfvtypes2fst))
1357 for (i = 0; i < NKFVTYPES; i++)
1358 if (kfvtypes2fst[i].kf_vtype == kfvtype)
1361 return (PS_FST_VTYPE_UNKNOWN);
1362 return (kfvtypes2fst[i].fst_vtype);
1366 procstat_get_vnode_info_sysctl(struct filestat *fst, struct vnstat *vn,
1369 struct statfs stbuf;
1370 struct kinfo_file *kif;
1371 struct kinfo_vmentry *kve;
1383 bzero(vn, sizeof(*vn));
1384 if (fst->fs_typedep == NULL)
1386 if (fst->fs_uflags & PS_FST_UFLAG_MMAP) {
1387 kve = fst->fs_typedep;
1388 fileid = kve->kve_vn_fileid;
1389 fsid = kve->kve_vn_fsid;
1390 mode = kve->kve_vn_mode;
1391 path = kve->kve_path;
1392 rdev = kve->kve_vn_rdev;
1393 size = kve->kve_vn_size;
1394 vntype = kinfo_vtype2fst(kve->kve_vn_type);
1395 status = kve->kve_status;
1397 kif = fst->fs_typedep;
1398 fileid = kif->kf_un.kf_file.kf_file_fileid;
1399 fsid = kif->kf_un.kf_file.kf_file_fsid;
1400 mode = kif->kf_un.kf_file.kf_file_mode;
1401 path = kif->kf_path;
1402 rdev = kif->kf_un.kf_file.kf_file_rdev;
1403 size = kif->kf_un.kf_file.kf_file_size;
1404 vntype = kinfo_vtype2fst(kif->kf_vnode_type);
1405 status = kif->kf_status;
1407 vn->vn_type = vntype;
1408 if (vntype == PS_FST_VTYPE_VNON || vntype == PS_FST_VTYPE_VBAD)
1410 if ((status & KF_ATTR_VALID) == 0) {
1411 if (errbuf != NULL) {
1412 snprintf(errbuf, _POSIX2_LINE_MAX,
1413 "? (no info available)");
1417 if (path && *path) {
1418 statfs(path, &stbuf);
1419 vn->vn_mntdir = strdup(stbuf.f_mntonname);
1421 vn->vn_mntdir = strdup("-");
1423 if (vntype == PS_FST_VTYPE_VBLK) {
1424 name = devname(rdev, S_IFBLK);
1426 strlcpy(vn->vn_devname, name,
1427 sizeof(vn->vn_devname));
1428 } else if (vntype == PS_FST_VTYPE_VCHR) {
1429 name = devname(vn->vn_dev, S_IFCHR);
1431 strlcpy(vn->vn_devname, name,
1432 sizeof(vn->vn_devname));
1435 vn->vn_fileid = fileid;
1442 procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
1443 struct sockstat *sock, char *errbuf)
1447 if (procstat->type == PROCSTAT_KVM) {
1448 return (procstat_get_socket_info_kvm(procstat->kd, fst, sock,
1450 } else if (procstat->type == PROCSTAT_SYSCTL ||
1451 procstat->type == PROCSTAT_CORE) {
1452 return (procstat_get_socket_info_sysctl(fst, sock, errbuf));
1454 warnx("unknown access method: %d", procstat->type);
1456 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1462 procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst,
1463 struct sockstat *sock, char *errbuf)
1467 struct protosw proto;
1476 bzero(sock, sizeof(*sock));
1477 so = fst->fs_typedep;
1480 sock->so_addr = (uintptr_t)so;
1481 /* fill in socket */
1482 if (!kvm_read_all(kd, (unsigned long)so, &s,
1483 sizeof(struct socket))) {
1484 warnx("can't read sock at %p", (void *)so);
1487 /* fill in protosw entry */
1488 if (!kvm_read_all(kd, (unsigned long)s.so_proto, &proto,
1489 sizeof(struct protosw))) {
1490 warnx("can't read protosw at %p", (void *)s.so_proto);
1493 /* fill in domain */
1494 if (!kvm_read_all(kd, (unsigned long)proto.pr_domain, &dom,
1495 sizeof(struct domain))) {
1496 warnx("can't read domain at %p",
1497 (void *)proto.pr_domain);
1500 if ((len = kvm_read(kd, (unsigned long)dom.dom_name, sock->dname,
1501 sizeof(sock->dname) - 1)) < 0) {
1502 warnx("can't read domain name at %p", (void *)dom.dom_name);
1503 sock->dname[0] = '\0';
1506 sock->dname[len] = '\0';
1509 * Fill in known data.
1511 sock->type = s.so_type;
1512 sock->proto = proto.pr_protocol;
1513 sock->dom_family = dom.dom_family;
1514 sock->so_pcb = (uintptr_t)s.so_pcb;
1517 * Protocol specific data.
1519 switch(dom.dom_family) {
1522 if (proto.pr_protocol == IPPROTO_TCP) {
1524 if (kvm_read(kd, (u_long)s.so_pcb,
1525 (char *)&inpcb, sizeof(struct inpcb))
1526 != sizeof(struct inpcb)) {
1527 warnx("can't read inpcb at %p",
1531 (uintptr_t)inpcb.inp_ppcb;
1532 sock->sendq = s.so_snd.sb_ccc;
1533 sock->recvq = s.so_rcv.sb_ccc;
1539 if (kvm_read(kd, (u_long)s.so_pcb, (char *)&unpcb,
1540 sizeof(struct unpcb)) != sizeof(struct unpcb)){
1541 warnx("can't read unpcb at %p",
1543 } else if (unpcb.unp_conn) {
1544 sock->so_rcv_sb_state = s.so_rcv.sb_state;
1545 sock->so_snd_sb_state = s.so_snd.sb_state;
1546 sock->unp_conn = (uintptr_t)unpcb.unp_conn;
1547 sock->sendq = s.so_snd.sb_ccc;
1548 sock->recvq = s.so_rcv.sb_ccc;
1559 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1564 procstat_get_socket_info_sysctl(struct filestat *fst, struct sockstat *sock,
1565 char *errbuf __unused)
1567 struct kinfo_file *kif;
1571 bzero(sock, sizeof(*sock));
1572 kif = fst->fs_typedep;
1577 * Fill in known data.
1579 sock->type = kif->kf_sock_type;
1580 sock->proto = kif->kf_sock_protocol;
1581 sock->dom_family = kif->kf_sock_domain;
1582 sock->so_pcb = kif->kf_un.kf_sock.kf_sock_pcb;
1583 strlcpy(sock->dname, kif->kf_path, sizeof(sock->dname));
1584 bcopy(&kif->kf_un.kf_sock.kf_sa_local, &sock->sa_local,
1585 kif->kf_un.kf_sock.kf_sa_local.ss_len);
1586 bcopy(&kif->kf_un.kf_sock.kf_sa_peer, &sock->sa_peer,
1587 kif->kf_un.kf_sock.kf_sa_peer.ss_len);
1590 * Protocol specific data.
1592 switch(sock->dom_family) {
1595 if (sock->proto == IPPROTO_TCP) {
1596 sock->inp_ppcb = kif->kf_un.kf_sock.kf_sock_inpcb;
1597 sock->sendq = kif->kf_un.kf_sock.kf_sock_sendq;
1598 sock->recvq = kif->kf_un.kf_sock.kf_sock_recvq;
1602 if (kif->kf_un.kf_sock.kf_sock_unpconn != 0) {
1603 sock->so_rcv_sb_state =
1604 kif->kf_un.kf_sock.kf_sock_rcv_sb_state;
1605 sock->so_snd_sb_state =
1606 kif->kf_un.kf_sock.kf_sock_snd_sb_state;
1608 kif->kf_un.kf_sock.kf_sock_unpconn;
1609 sock->sendq = kif->kf_un.kf_sock.kf_sock_sendq;
1610 sock->recvq = kif->kf_un.kf_sock.kf_sock_recvq;
1620 * Descriptor flags to filestat translation.
1623 to_filestat_flags(int flags)
1629 { FREAD, PS_FST_FFLAG_READ },
1630 { FWRITE, PS_FST_FFLAG_WRITE },
1631 { O_APPEND, PS_FST_FFLAG_APPEND },
1632 { O_ASYNC, PS_FST_FFLAG_ASYNC },
1633 { O_CREAT, PS_FST_FFLAG_CREAT },
1634 { O_DIRECT, PS_FST_FFLAG_DIRECT },
1635 { O_EXCL, PS_FST_FFLAG_EXCL },
1636 { O_EXEC, PS_FST_FFLAG_EXEC },
1637 { O_EXLOCK, PS_FST_FFLAG_EXLOCK },
1638 { O_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW },
1639 { O_NONBLOCK, PS_FST_FFLAG_NONBLOCK },
1640 { O_SHLOCK, PS_FST_FFLAG_SHLOCK },
1641 { O_SYNC, PS_FST_FFLAG_SYNC },
1642 { O_TRUNC, PS_FST_FFLAG_TRUNC }
1644 #define NFSTFLAGS (sizeof(fstflags) / sizeof(*fstflags))
1649 for (i = 0; i < NFSTFLAGS; i++)
1650 if (flags & fstflags[i].flag)
1651 fst_flags |= fstflags[i].fst_flag;
1656 * Vnode type to filestate translation.
1659 vntype2psfsttype(int type)
1665 { VBAD, PS_FST_VTYPE_VBAD },
1666 { VBLK, PS_FST_VTYPE_VBLK },
1667 { VCHR, PS_FST_VTYPE_VCHR },
1668 { VDIR, PS_FST_VTYPE_VDIR },
1669 { VFIFO, PS_FST_VTYPE_VFIFO },
1670 { VLNK, PS_FST_VTYPE_VLNK },
1671 { VNON, PS_FST_VTYPE_VNON },
1672 { VREG, PS_FST_VTYPE_VREG },
1673 { VSOCK, PS_FST_VTYPE_VSOCK }
1675 #define NVFTYPES (sizeof(vt2fst) / sizeof(*vt2fst))
1676 unsigned int i, fst_type;
1678 fst_type = PS_FST_VTYPE_UNKNOWN;
1679 for (i = 0; i < NVFTYPES; i++) {
1680 if (type == vt2fst[i].vtype) {
1681 fst_type = vt2fst[i].fst_vtype;
1689 getmnton(kvm_t *kd, struct mount *m)
1692 static struct mtab {
1695 char mntonname[MNAMELEN + 1];
1699 for (mt = mhead; mt != NULL; mt = mt->next)
1701 return (mt->mntonname);
1702 if (!kvm_read_all(kd, (unsigned long)m, &mnt, sizeof(struct mount))) {
1703 warnx("can't read mount table at %p", (void *)m);
1706 if ((mt = malloc(sizeof (struct mtab))) == NULL)
1709 bcopy(&mnt.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
1710 mt->mntonname[MNAMELEN] = '\0';
1713 return (mt->mntonname);
1717 * Auxiliary structures and functions to get process environment or
1718 * command line arguments.
1727 static struct argvec *
1728 argvec_alloc(size_t bufsize)
1732 av = malloc(sizeof(*av));
1735 av->bufsize = bufsize;
1736 av->buf = malloc(av->bufsize);
1737 if (av->buf == NULL) {
1742 av->argv = malloc(sizeof(char *) * av->argc);
1743 if (av->argv == NULL) {
1752 argvec_free(struct argvec * av)
1761 getargv(struct procstat *procstat, struct kinfo_proc *kp, size_t nchr, int env)
1763 int error, name[4], argc, i;
1764 struct argvec *av, **avp;
1771 if (procstat->type == PROCSTAT_KVM) {
1772 warnx("can't use kvm access method");
1775 if (procstat->type != PROCSTAT_SYSCTL &&
1776 procstat->type != PROCSTAT_CORE) {
1777 warnx("unknown access method: %d", procstat->type);
1781 if (nchr == 0 || nchr > ARG_MAX)
1784 avp = (struct argvec **)(env ? &procstat->argv : &procstat->envv);
1789 av = argvec_alloc(nchr);
1792 warn("malloc(%zu)", nchr);
1796 } else if (av->bufsize < nchr) {
1797 av->buf = reallocf(av->buf, nchr);
1798 if (av->buf == NULL) {
1799 warn("malloc(%zu)", nchr);
1803 if (procstat->type == PROCSTAT_SYSCTL) {
1805 name[1] = KERN_PROC;
1806 name[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS;
1807 name[3] = kp->ki_pid;
1809 error = sysctl(name, nitems(name), av->buf, &len, NULL, 0);
1810 if (error != 0 && errno != ESRCH && errno != EPERM)
1811 warn("sysctl(kern.proc.%s)", env ? "env" : "args");
1812 if (error != 0 || len == 0)
1814 } else /* procstat->type == PROCSTAT_CORE */ {
1815 type = env ? PSC_TYPE_ENVV : PSC_TYPE_ARGV;
1817 if (procstat_core_get(procstat->core, type, av->buf, &len)
1826 for (p = av->buf; p < av->buf + len; p += strlen(p) + 1) {
1832 argv = realloc(argv, sizeof(char *) * argc);
1834 warn("malloc(%zu)", sizeof(char *) * argc);
1846 * Return process command line arguments.
1849 procstat_getargv(struct procstat *procstat, struct kinfo_proc *p, size_t nchr)
1852 return (getargv(procstat, p, nchr, 0));
1856 * Free the buffer allocated by procstat_getargv().
1859 procstat_freeargv(struct procstat *procstat)
1862 if (procstat->argv != NULL) {
1863 argvec_free(procstat->argv);
1864 procstat->argv = NULL;
1869 * Return process environment.
1872 procstat_getenvv(struct procstat *procstat, struct kinfo_proc *p, size_t nchr)
1875 return (getargv(procstat, p, nchr, 1));
1879 * Free the buffer allocated by procstat_getenvv().
1882 procstat_freeenvv(struct procstat *procstat)
1884 if (procstat->envv != NULL) {
1885 argvec_free(procstat->envv);
1886 procstat->envv = NULL;
1890 static struct kinfo_vmentry *
1891 kinfo_getvmmap_core(struct procstat_core *core, int *cntp)
1895 char *buf, *bp, *eb;
1896 struct kinfo_vmentry *kiv, *kp, *kv;
1898 buf = procstat_core_get(core, PSC_TYPE_VMMAP, NULL, &len);
1903 * XXXMG: The code below is just copy&past from libutil.
1904 * The code duplication can be avoided if libutil
1905 * is extended to provide something like:
1906 * struct kinfo_vmentry *kinfo_getvmmap_from_buf(const char *buf,
1907 * size_t len, int *cntp);
1910 /* Pass 1: count items */
1915 kv = (struct kinfo_vmentry *)(uintptr_t)bp;
1916 if (kv->kve_structsize == 0)
1918 bp += kv->kve_structsize;
1922 kiv = calloc(cnt, sizeof(*kiv));
1930 /* Pass 2: unpack */
1932 kv = (struct kinfo_vmentry *)(uintptr_t)bp;
1933 if (kv->kve_structsize == 0)
1935 /* Copy/expand into pre-zeroed buffer */
1936 memcpy(kp, kv, kv->kve_structsize);
1937 /* Advance to next packed record */
1938 bp += kv->kve_structsize;
1939 /* Set field size to fixed length, advance */
1940 kp->kve_structsize = sizeof(*kp);
1945 return (kiv); /* Caller must free() return value */
1948 struct kinfo_vmentry *
1949 procstat_getvmmap(struct procstat *procstat, struct kinfo_proc *kp,
1953 switch(procstat->type) {
1955 warnx("kvm method is not supported");
1957 case PROCSTAT_SYSCTL:
1958 return (kinfo_getvmmap(kp->ki_pid, cntp));
1960 return (kinfo_getvmmap_core(procstat->core, cntp));
1962 warnx("unknown access method: %d", procstat->type);
1968 procstat_freevmmap(struct procstat *procstat __unused,
1969 struct kinfo_vmentry *vmmap)
1976 procstat_getgroups_kvm(kvm_t *kd, struct kinfo_proc *kp, unsigned int *cntp)
1985 if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
1987 warnx("can't read proc struct at %p for pid %d",
1988 kp->ki_paddr, kp->ki_pid);
1991 if (proc.p_ucred == NOCRED)
1993 if (!kvm_read_all(kd, (unsigned long)proc.p_ucred, &ucred,
1995 warnx("can't read ucred struct at %p for pid %d",
1996 proc.p_ucred, kp->ki_pid);
1999 len = ucred.cr_ngroups * sizeof(gid_t);
2000 groups = malloc(len);
2001 if (groups == NULL) {
2002 warn("malloc(%zu)", len);
2005 if (!kvm_read_all(kd, (unsigned long)ucred.cr_groups, groups, len)) {
2006 warnx("can't read groups at %p for pid %d",
2007 ucred.cr_groups, kp->ki_pid);
2011 *cntp = ucred.cr_ngroups;
2016 procstat_getgroups_sysctl(pid_t pid, unsigned int *cntp)
2024 mib[2] = KERN_PROC_GROUPS;
2026 len = (sysconf(_SC_NGROUPS_MAX) + 1) * sizeof(gid_t);
2027 groups = malloc(len);
2028 if (groups == NULL) {
2029 warn("malloc(%zu)", len);
2032 if (sysctl(mib, nitems(mib), groups, &len, NULL, 0) == -1) {
2033 warn("sysctl: kern.proc.groups: %d", pid);
2037 *cntp = len / sizeof(gid_t);
2042 procstat_getgroups_core(struct procstat_core *core, unsigned int *cntp)
2047 groups = procstat_core_get(core, PSC_TYPE_GROUPS, NULL, &len);
2050 *cntp = len / sizeof(gid_t);
2055 procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
2058 switch(procstat->type) {
2060 return (procstat_getgroups_kvm(procstat->kd, kp, cntp));
2061 case PROCSTAT_SYSCTL:
2062 return (procstat_getgroups_sysctl(kp->ki_pid, cntp));
2064 return (procstat_getgroups_core(procstat->core, cntp));
2066 warnx("unknown access method: %d", procstat->type);
2072 procstat_freegroups(struct procstat *procstat __unused, gid_t *groups)
2079 procstat_getumask_kvm(kvm_t *kd, struct kinfo_proc *kp, unsigned short *maskp)
2085 if (kp->ki_fd == NULL)
2087 if (!kvm_read_all(kd, (unsigned long)kp->ki_fd, &fd, sizeof(fd))) {
2088 warnx("can't read filedesc at %p for pid %d", kp->ki_fd,
2092 *maskp = fd.fd_cmask;
2097 procstat_getumask_sysctl(pid_t pid, unsigned short *maskp)
2105 mib[2] = KERN_PROC_UMASK;
2107 len = sizeof(*maskp);
2108 error = sysctl(mib, nitems(mib), maskp, &len, NULL, 0);
2109 if (error != 0 && errno != ESRCH && errno != EPERM)
2110 warn("sysctl: kern.proc.umask: %d", pid);
2115 procstat_getumask_core(struct procstat_core *core, unsigned short *maskp)
2118 unsigned short *buf;
2120 buf = procstat_core_get(core, PSC_TYPE_UMASK, NULL, &len);
2123 if (len < sizeof(*maskp)) {
2133 procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
2134 unsigned short *maskp)
2136 switch(procstat->type) {
2138 return (procstat_getumask_kvm(procstat->kd, kp, maskp));
2139 case PROCSTAT_SYSCTL:
2140 return (procstat_getumask_sysctl(kp->ki_pid, maskp));
2142 return (procstat_getumask_core(procstat->core, maskp));
2144 warnx("unknown access method: %d", procstat->type);
2150 procstat_getrlimit_kvm(kvm_t *kd, struct kinfo_proc *kp, int which,
2151 struct rlimit* rlimit)
2154 unsigned long offset;
2158 assert(which >= 0 && which < RLIM_NLIMITS);
2159 if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
2161 warnx("can't read proc struct at %p for pid %d",
2162 kp->ki_paddr, kp->ki_pid);
2165 if (proc.p_limit == NULL)
2167 offset = (unsigned long)proc.p_limit + sizeof(struct rlimit) * which;
2168 if (!kvm_read_all(kd, offset, rlimit, sizeof(*rlimit))) {
2169 warnx("can't read rlimit struct at %p for pid %d",
2170 (void *)offset, kp->ki_pid);
2177 procstat_getrlimit_sysctl(pid_t pid, int which, struct rlimit* rlimit)
2183 name[1] = KERN_PROC;
2184 name[2] = KERN_PROC_RLIMIT;
2187 len = sizeof(struct rlimit);
2188 error = sysctl(name, nitems(name), rlimit, &len, NULL, 0);
2189 if (error < 0 && errno != ESRCH) {
2190 warn("sysctl: kern.proc.rlimit: %d", pid);
2193 if (error < 0 || len != sizeof(struct rlimit))
2199 procstat_getrlimit_core(struct procstat_core *core, int which,
2200 struct rlimit* rlimit)
2203 struct rlimit* rlimits;
2205 if (which < 0 || which >= RLIM_NLIMITS) {
2207 warn("getrlimit: which");
2210 rlimits = procstat_core_get(core, PSC_TYPE_RLIMIT, NULL, &len);
2211 if (rlimits == NULL)
2213 if (len < sizeof(struct rlimit) * RLIM_NLIMITS) {
2217 *rlimit = rlimits[which];
2223 procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp, int which,
2224 struct rlimit* rlimit)
2226 switch(procstat->type) {
2228 return (procstat_getrlimit_kvm(procstat->kd, kp, which,
2230 case PROCSTAT_SYSCTL:
2231 return (procstat_getrlimit_sysctl(kp->ki_pid, which, rlimit));
2233 return (procstat_getrlimit_core(procstat->core, which, rlimit));
2235 warnx("unknown access method: %d", procstat->type);
2241 procstat_getpathname_sysctl(pid_t pid, char *pathname, size_t maxlen)
2247 name[1] = KERN_PROC;
2248 name[2] = KERN_PROC_PATHNAME;
2251 error = sysctl(name, nitems(name), pathname, &len, NULL, 0);
2252 if (error != 0 && errno != ESRCH)
2253 warn("sysctl: kern.proc.pathname: %d", pid);
2260 procstat_getpathname_core(struct procstat_core *core, char *pathname,
2263 struct kinfo_file *files;
2266 files = kinfo_getfile_core(core, &cnt);
2270 for (i = 0; i < cnt; i++) {
2271 if (files[i].kf_fd != KF_FD_TYPE_TEXT)
2273 strncpy(pathname, files[i].kf_path, maxlen);
2282 procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
2283 char *pathname, size_t maxlen)
2285 switch(procstat->type) {
2287 /* XXX: Return empty string. */
2291 case PROCSTAT_SYSCTL:
2292 return (procstat_getpathname_sysctl(kp->ki_pid, pathname,
2295 return (procstat_getpathname_core(procstat->core, pathname,
2298 warnx("unknown access method: %d", procstat->type);
2304 procstat_getosrel_kvm(kvm_t *kd, struct kinfo_proc *kp, int *osrelp)
2310 if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
2312 warnx("can't read proc struct at %p for pid %d",
2313 kp->ki_paddr, kp->ki_pid);
2316 *osrelp = proc.p_osrel;
2321 procstat_getosrel_sysctl(pid_t pid, int *osrelp)
2327 name[1] = KERN_PROC;
2328 name[2] = KERN_PROC_OSREL;
2330 len = sizeof(*osrelp);
2331 error = sysctl(name, nitems(name), osrelp, &len, NULL, 0);
2332 if (error != 0 && errno != ESRCH)
2333 warn("sysctl: kern.proc.osrel: %d", pid);
2338 procstat_getosrel_core(struct procstat_core *core, int *osrelp)
2343 buf = procstat_core_get(core, PSC_TYPE_OSREL, NULL, &len);
2346 if (len < sizeof(*osrelp)) {
2356 procstat_getosrel(struct procstat *procstat, struct kinfo_proc *kp, int *osrelp)
2358 switch(procstat->type) {
2360 return (procstat_getosrel_kvm(procstat->kd, kp, osrelp));
2361 case PROCSTAT_SYSCTL:
2362 return (procstat_getosrel_sysctl(kp->ki_pid, osrelp));
2364 return (procstat_getosrel_core(procstat->core, osrelp));
2366 warnx("unknown access method: %d", procstat->type);
2371 #define PROC_AUXV_MAX 256
2373 #if __ELF_WORD_SIZE == 64
2374 static const char *elf32_sv_names[] = {
2380 is_elf32_sysctl(pid_t pid)
2384 static char sv_name[256];
2387 name[1] = KERN_PROC;
2388 name[2] = KERN_PROC_SV_NAME;
2390 len = sizeof(sv_name);
2391 error = sysctl(name, nitems(name), sv_name, &len, NULL, 0);
2392 if (error != 0 || len == 0)
2394 for (i = 0; i < sizeof(elf32_sv_names) / sizeof(*elf32_sv_names); i++) {
2395 if (strncmp(sv_name, elf32_sv_names[i], sizeof(sv_name)) == 0)
2401 static Elf_Auxinfo *
2402 procstat_getauxv32_sysctl(pid_t pid, unsigned int *cntp)
2405 Elf32_Auxinfo *auxv32;
2408 unsigned int i, count;
2412 name[1] = KERN_PROC;
2413 name[2] = KERN_PROC_AUXV;
2415 len = PROC_AUXV_MAX * sizeof(Elf32_Auxinfo);
2417 auxv32 = malloc(len);
2418 if (auxv32 == NULL) {
2419 warn("malloc(%zu)", len);
2422 if (sysctl(name, nitems(name), auxv32, &len, NULL, 0) == -1) {
2423 if (errno != ESRCH && errno != EPERM)
2424 warn("sysctl: kern.proc.auxv: %d: %d", pid, errno);
2427 count = len / sizeof(Elf_Auxinfo);
2428 auxv = malloc(count * sizeof(Elf_Auxinfo));
2430 warn("malloc(%zu)", count * sizeof(Elf_Auxinfo));
2433 for (i = 0; i < count; i++) {
2435 * XXX: We expect that values for a_type on a 32-bit platform
2436 * are directly mapped to values on 64-bit one, which is not
2439 auxv[i].a_type = auxv32[i].a_type;
2440 ptr = &auxv32[i].a_un;
2441 auxv[i].a_un.a_val = *((uint32_t *)ptr);
2448 #endif /* __ELF_WORD_SIZE == 64 */
2450 static Elf_Auxinfo *
2451 procstat_getauxv_sysctl(pid_t pid, unsigned int *cntp)
2457 #if __ELF_WORD_SIZE == 64
2458 if (is_elf32_sysctl(pid))
2459 return (procstat_getauxv32_sysctl(pid, cntp));
2462 name[1] = KERN_PROC;
2463 name[2] = KERN_PROC_AUXV;
2465 len = PROC_AUXV_MAX * sizeof(Elf_Auxinfo);
2468 warn("malloc(%zu)", len);
2471 if (sysctl(name, nitems(name), auxv, &len, NULL, 0) == -1) {
2472 if (errno != ESRCH && errno != EPERM)
2473 warn("sysctl: kern.proc.auxv: %d: %d", pid, errno);
2477 *cntp = len / sizeof(Elf_Auxinfo);
2481 static Elf_Auxinfo *
2482 procstat_getauxv_core(struct procstat_core *core, unsigned int *cntp)
2487 auxv = procstat_core_get(core, PSC_TYPE_AUXV, NULL, &len);
2490 *cntp = len / sizeof(Elf_Auxinfo);
2495 procstat_getauxv(struct procstat *procstat, struct kinfo_proc *kp,
2498 switch(procstat->type) {
2500 warnx("kvm method is not supported");
2502 case PROCSTAT_SYSCTL:
2503 return (procstat_getauxv_sysctl(kp->ki_pid, cntp));
2505 return (procstat_getauxv_core(procstat->core, cntp));
2507 warnx("unknown access method: %d", procstat->type);
2513 procstat_freeauxv(struct procstat *procstat __unused, Elf_Auxinfo *auxv)
2519 static struct ptrace_lwpinfo *
2520 procstat_getptlwpinfo_core(struct procstat_core *core, unsigned int *cntp)
2523 struct ptrace_lwpinfo *pl;
2527 cnt = procstat_core_note_count(core, PSC_TYPE_PTLWPINFO);
2531 len = cnt * sizeof(*pl);
2532 buf = calloc(1, len);
2533 pl = procstat_core_get(core, PSC_TYPE_PTLWPINFO, buf, &len);
2538 *cntp = len / sizeof(*pl);
2542 struct ptrace_lwpinfo *
2543 procstat_getptlwpinfo(struct procstat *procstat, unsigned int *cntp)
2545 switch (procstat->type) {
2547 warnx("kvm method is not supported");
2549 case PROCSTAT_SYSCTL:
2550 warnx("sysctl method is not supported");
2553 return (procstat_getptlwpinfo_core(procstat->core, cntp));
2555 warnx("unknown access method: %d", procstat->type);
2561 procstat_freeptlwpinfo(struct procstat *procstat __unused,
2562 struct ptrace_lwpinfo *pl)
2567 static struct kinfo_kstack *
2568 procstat_getkstack_sysctl(pid_t pid, int *cntp)
2570 struct kinfo_kstack *kkstp;
2575 name[1] = KERN_PROC;
2576 name[2] = KERN_PROC_KSTACK;
2580 error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
2581 if (error < 0 && errno != ESRCH && errno != EPERM && errno != ENOENT) {
2582 warn("sysctl: kern.proc.kstack: %d", pid);
2585 if (error == -1 && errno == ENOENT) {
2586 warnx("sysctl: kern.proc.kstack unavailable"
2587 " (options DDB or options STACK required in kernel)");
2592 kkstp = malloc(len);
2593 if (kkstp == NULL) {
2594 warn("malloc(%zu)", len);
2597 if (sysctl(name, nitems(name), kkstp, &len, NULL, 0) == -1) {
2598 warn("sysctl: kern.proc.pid: %d", pid);
2602 *cntp = len / sizeof(*kkstp);
2607 struct kinfo_kstack *
2608 procstat_getkstack(struct procstat *procstat, struct kinfo_proc *kp,
2611 switch(procstat->type) {
2613 warnx("kvm method is not supported");
2615 case PROCSTAT_SYSCTL:
2616 return (procstat_getkstack_sysctl(kp->ki_pid, cntp));
2618 warnx("core method is not supported");
2621 warnx("unknown access method: %d", procstat->type);
2627 procstat_freekstack(struct procstat *procstat __unused,
2628 struct kinfo_kstack *kkstp)