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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
41 #include <sys/resourcevar.h>
43 #include <sys/ucred.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>
54 #include <sys/unpcb.h>
55 #include <sys/sysctl.h>
57 #include <sys/filedesc.h>
58 #include <sys/queue.h>
64 #include <sys/capsicum.h>
66 #include <sys/mount.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>
73 #include <nfs/nfsproto.h>
74 #include <nfsclient/nfs.h>
75 #include <nfsclient/nfsnode.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_object.h>
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>
103 #include <libprocstat.h>
104 #include "libprocstat_internal.h"
105 #include "common_kvm.h"
108 int statfs(const char *, struct statfs *); /* XXX */
110 #define PROCSTAT_KVM 1
111 #define PROCSTAT_SYSCTL 2
112 #define PROCSTAT_CORE 3
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,
119 static Elf_Auxinfo *procstat_getauxv_core(struct procstat_core *core,
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,
158 static int procstat_getosrel_core(struct procstat_core *core,
160 static int procstat_getosrel_kvm(kvm_t *kd, struct kinfo_proc *kp,
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,
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);
181 procstat_close(struct procstat *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);
195 procstat_open_sysctl(void)
197 struct procstat *procstat;
199 procstat = calloc(1, sizeof(*procstat));
200 if (procstat == NULL) {
204 procstat->type = PROCSTAT_SYSCTL;
209 procstat_open_kvm(const char *nlistf, const char *memf)
211 struct procstat *procstat;
213 char buf[_POSIX2_LINE_MAX];
215 procstat = calloc(1, sizeof(*procstat));
216 if (procstat == NULL) {
220 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
222 warnx("kvm_openfiles(): %s", buf);
226 procstat->type = PROCSTAT_KVM;
232 procstat_open_core(const char *filename)
234 struct procstat *procstat;
235 struct procstat_core *core;
237 procstat = calloc(1, sizeof(*procstat));
238 if (procstat == NULL) {
242 core = procstat_core_open(filename);
247 procstat->type = PROCSTAT_CORE;
248 procstat->core = core;
253 procstat_getprocs(struct procstat *procstat, int what, int arg,
256 struct kinfo_proc *p0, *p;
265 if (procstat->type == PROCSTAT_KVM) {
267 p0 = kvm_getprocs(procstat->kd, what, arg, &cnt);
268 if (p0 == NULL || cnt <= 0)
271 len = *count * sizeof(*p);
274 warnx("malloc(%zu)", len);
279 } else if (procstat->type == PROCSTAT_SYSCTL) {
285 error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
286 if (error < 0 && errno != EPERM) {
287 warn("sysctl(kern.proc)");
291 warnx("no processes?");
296 p = reallocf(p, len);
298 warnx("reallocf(%zu)", 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)");
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);
313 *count = len / sizeof(*p);
315 } else if (procstat->type == PROCSTAT_CORE) {
316 p = procstat_core_get(procstat->core, PSC_TYPE_PROC, NULL,
318 if ((len % sizeof(*p)) != 0 || p->ki_structsize != sizeof(*p)) {
319 warnx("kinfo_proc structure size mismatch");
322 *count = len / sizeof(*p);
325 warnx("unknown access method: %d", procstat->type);
335 procstat_freeprocs(struct procstat *procstat __unused, struct kinfo_proc *p)
343 struct filestat_list *
344 procstat_getfiles(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
347 switch(procstat->type) {
349 return (procstat_getfiles_kvm(procstat, kp, mmapped));
350 case PROCSTAT_SYSCTL:
352 return (procstat_getfiles_sysctl(procstat, kp, mmapped));
354 warnx("unknown access method: %d", procstat->type);
360 procstat_freefiles(struct procstat *procstat, struct filestat_list *head)
362 struct filestat *fst, *tmp;
364 STAILQ_FOREACH_SAFE(fst, head, next, tmp) {
365 if (fst->fs_path != NULL)
370 if (procstat->vmentries != NULL) {
371 free(procstat->vmentries);
372 procstat->vmentries = NULL;
374 if (procstat->files != NULL) {
375 free(procstat->files);
376 procstat->files = NULL;
380 static struct filestat *
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)
384 struct filestat *entry;
386 entry = calloc(1, sizeof(*entry));
391 entry->fs_typedep = typedep;
392 entry->fs_fflags = fflags;
393 entry->fs_uflags = uflags;
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;
402 cap_rights_init(&entry->fs_cap_rights);
406 static struct vnode *
407 getctty(kvm_t *kd, struct kinfo_proc *kp)
415 error = kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
418 warnx("can't read proc struct at %p for pid %d",
419 kp->ki_paddr, kp->ki_pid);
422 if (proc.p_pgrp == NULL)
424 error = kvm_read_all(kd, (unsigned long)proc.p_pgrp, &pgrp,
427 warnx("can't read pgrp struct at %p for pid %d",
428 proc.p_pgrp, kp->ki_pid);
431 error = kvm_read_all(kd, (unsigned long)pgrp.pg_session, &sess,
434 warnx("can't read session struct at %p for pid %d",
435 pgrp.pg_session, kp->ki_pid);
438 return (sess.s_ttyvp);
441 static struct filestat_list *
442 procstat_getfiles_kvm(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
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;
453 struct file **ofiles;
454 struct filestat *entry;
455 struct filestat_list *head;
466 if (kp->ki_fd == NULL)
468 if (!kvm_read_all(kd, (unsigned long)kp->ki_fd, &filed,
470 warnx("can't read filedesc at %p", (void *)kp->ki_fd);
475 * Allocate list head.
477 head = malloc(sizeof(*head));
482 /* root directory vnode, if one. */
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);
487 STAILQ_INSERT_TAIL(head, entry, next);
489 /* current working directory vnode. */
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);
494 STAILQ_INSERT_TAIL(head, entry, next);
496 /* jail root, if any. */
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);
501 STAILQ_INSERT_TAIL(head, entry, next);
503 /* ktrace vnode, if one */
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);
509 STAILQ_INSERT_TAIL(head, entry, next);
511 /* text vnode, if one */
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);
516 STAILQ_INSERT_TAIL(head, entry, next);
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);
524 STAILQ_INSERT_TAIL(head, entry, next);
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 *));
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);
540 for (i = 0; i <= filed.fd_lastfile; i++) {
541 if (ofiles[i] == NULL)
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,
549 switch (file.f_type) {
551 type = PS_FST_TYPE_VNODE;
555 type = PS_FST_TYPE_SOCKET;
559 type = PS_FST_TYPE_PIPE;
563 type = PS_FST_TYPE_FIFO;
568 type = PS_FST_TYPE_PTS;
573 type = PS_FST_TYPE_SEM;
577 type = PS_FST_TYPE_SHM;
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);
587 STAILQ_INSERT_TAIL(head, entry, next);
594 * Process mmapped files if requested.
597 if (!kvm_read_all(kd, (unsigned long)kp->ki_vmspace, &vmspace,
599 warnx("can't read vmspace at %p",
600 (void *)kp->ki_vmspace);
603 map = &vmspace.vm_map;
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,
610 warnx("can't read vm_map_entry at %p",
614 if (vmentry.eflags & MAP_ENTRY_IS_SUB_MAP)
616 if ((objp = vmentry.object.vm_object) == NULL)
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",
627 /* We want only vnode objects. */
628 if (object.type != OBJT_VNODE)
631 prot = vmentry.protection;
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;
640 * Create filestat entry.
642 entry = filestat_new_entry(object.handle,
643 PS_FST_TYPE_VNODE, -1, fflags,
644 PS_FST_UFLAG_MMAP, 0, 0, NULL, NULL);
646 STAILQ_INSERT_TAIL(head, entry, next);
654 * kinfo types to filestat translation.
657 kinfo_type2fst(int kftype)
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 }
676 #define NKFTYPES (sizeof(kftypes2fst) / sizeof(*kftypes2fst))
679 for (i = 0; i < NKFTYPES; i++)
680 if (kftypes2fst[i].kf_type == kftype)
683 return (PS_FST_TYPE_UNKNOWN);
684 return (kftypes2fst[i].fst_type);
688 * kinfo flags to filestat translation.
691 kinfo_fflags2fst(int kfflags)
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 }
713 #define NKFFLAGS (sizeof(kfflags2fst) / sizeof(*kfflags2fst))
718 for (i = 0; i < NKFFLAGS; i++)
719 if ((kfflags & kfflags2fst[i].kf_flag) != 0)
720 flags |= kfflags2fst[i].fst_flag;
725 kinfo_uflags2fst(int fd)
729 case KF_FD_TYPE_CTTY:
730 return (PS_FST_UFLAG_CTTY);
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);
745 static struct kinfo_file *
746 kinfo_getfile_core(struct procstat_core *core, int *cntp)
751 struct kinfo_file *kif, *kp, *kf;
753 buf = procstat_core_get(core, PSC_TYPE_FILES, NULL, &len);
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);
764 /* Pass 1: count items */
769 kf = (struct kinfo_file *)(uintptr_t)bp;
770 if (kf->kf_structsize == 0)
772 bp += kf->kf_structsize;
776 kif = calloc(cnt, sizeof(*kif));
786 kf = (struct kinfo_file *)(uintptr_t)bp;
787 if (kf->kf_structsize == 0)
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);
799 return (kif); /* Caller must free() return value */
802 static struct filestat_list *
803 procstat_getfiles_sysctl(struct procstat *procstat, struct kinfo_proc *kp,
806 struct kinfo_file *kif, *files;
807 struct kinfo_vmentry *kve, *vmentries;
808 struct filestat_list *head;
809 struct filestat *entry;
815 cap_rights_t cap_rights;
818 if (kp->ki_fd == NULL)
820 switch(procstat->type) {
821 case PROCSTAT_SYSCTL:
822 files = kinfo_getfile(kp->ki_pid, &cnt);
825 files = kinfo_getfile_core(procstat->core, &cnt);
828 assert(!"invalid type");
830 if (files == NULL && errno != EPERM) {
831 warn("kinfo_getfile()");
834 procstat->files = files;
837 * Allocate list head.
839 head = malloc(sizeof(*head));
843 for (i = 0; i < cnt; i++) {
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);
856 cap_rights = kif->kf_cap_rights;
859 * Create filestat entry.
861 entry = filestat_new_entry(kif, type, fd, fflags, uflags,
862 refcount, offset, path, &cap_rights);
864 STAILQ_INSERT_TAIL(head, entry, next);
867 vmentries = procstat_getvmmap(procstat, kp, &cnt);
868 procstat->vmentries = vmentries;
869 if (vmentries == NULL || cnt == 0)
871 for (i = 0; i < cnt; i++) {
873 if (kve->kve_type != KVME_TYPE_VNODE)
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);
887 entry = filestat_new_entry(kve, PS_FST_TYPE_VNODE, -1,
888 fflags, PS_FST_UFLAG_MMAP, refcount, offset, path,
891 STAILQ_INSERT_TAIL(head, entry, next);
899 procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst,
900 struct pipestat *ps, char *errbuf)
904 if (procstat->type == PROCSTAT_KVM) {
905 return (procstat_get_pipe_info_kvm(procstat->kd, fst, ps,
907 } else if (procstat->type == PROCSTAT_SYSCTL ||
908 procstat->type == PROCSTAT_CORE) {
909 return (procstat_get_pipe_info_sysctl(fst, ps, errbuf));
911 warnx("unknown access method: %d", procstat->type);
913 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
919 procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst,
920 struct pipestat *ps, char *errbuf)
928 bzero(ps, sizeof(*ps));
929 pipep = fst->fs_typedep;
932 if (!kvm_read_all(kd, (unsigned long)pipep, &pi, sizeof(struct pipe))) {
933 warnx("can't read pipe at %p", (void *)pipep);
936 ps->addr = (uintptr_t)pipep;
937 ps->peer = (uintptr_t)pi.pipe_peer;
938 ps->buffer_cnt = pi.pipe_buffer.cnt;
943 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
948 procstat_get_pipe_info_sysctl(struct filestat *fst, struct pipestat *ps,
949 char *errbuf __unused)
951 struct kinfo_file *kif;
955 bzero(ps, sizeof(*ps));
956 kif = fst->fs_typedep;
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;
966 procstat_get_pts_info(struct procstat *procstat, struct filestat *fst,
967 struct ptsstat *pts, char *errbuf)
971 if (procstat->type == PROCSTAT_KVM) {
972 return (procstat_get_pts_info_kvm(procstat->kd, fst, pts,
974 } else if (procstat->type == PROCSTAT_SYSCTL ||
975 procstat->type == PROCSTAT_CORE) {
976 return (procstat_get_pts_info_sysctl(fst, pts, errbuf));
978 warnx("unknown access method: %d", procstat->type);
980 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
986 procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst,
987 struct ptsstat *pts, char *errbuf)
995 bzero(pts, sizeof(*pts));
996 ttyp = fst->fs_typedep;
999 if (!kvm_read_all(kd, (unsigned long)ttyp, &tty, sizeof(struct tty))) {
1000 warnx("can't read tty at %p", (void *)ttyp);
1003 pts->dev = dev2udev(kd, tty.t_dev);
1004 (void)kdevtoname(kd, tty.t_dev, pts->devname);
1009 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1014 procstat_get_pts_info_sysctl(struct filestat *fst, struct ptsstat *pts,
1015 char *errbuf __unused)
1017 struct kinfo_file *kif;
1021 bzero(pts, sizeof(*pts));
1022 kif = fst->fs_typedep;
1025 pts->dev = kif->kf_un.kf_pts.kf_pts_dev;
1026 strlcpy(pts->devname, kif->kf_path, sizeof(pts->devname));
1031 procstat_get_sem_info(struct procstat *procstat, struct filestat *fst,
1032 struct semstat *sem, char *errbuf)
1036 if (procstat->type == PROCSTAT_KVM) {
1037 return (procstat_get_sem_info_kvm(procstat->kd, fst, sem,
1039 } else if (procstat->type == PROCSTAT_SYSCTL ||
1040 procstat->type == PROCSTAT_CORE) {
1041 return (procstat_get_sem_info_sysctl(fst, sem, errbuf));
1043 warnx("unknown access method: %d", procstat->type);
1045 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1051 procstat_get_sem_info_kvm(kvm_t *kd, struct filestat *fst,
1052 struct semstat *sem, char *errbuf)
1062 bzero(sem, sizeof(*sem));
1063 ksemp = fst->fs_typedep;
1066 if (!kvm_read_all(kd, (unsigned long)ksemp, &ksem,
1067 sizeof(struct ksem))) {
1068 warnx("can't read ksem at %p", (void *)ksemp);
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,
1079 if (path[i] == '\0')
1086 fst->fs_path = path;
1092 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1097 procstat_get_sem_info_sysctl(struct filestat *fst, struct semstat *sem,
1098 char *errbuf __unused)
1100 struct kinfo_file *kif;
1104 bzero(sem, sizeof(*sem));
1105 kif = fst->fs_typedep;
1108 sem->value = kif->kf_un.kf_sem.kf_sem_value;
1109 sem->mode = kif->kf_un.kf_sem.kf_sem_mode;
1114 procstat_get_shm_info(struct procstat *procstat, struct filestat *fst,
1115 struct shmstat *shm, char *errbuf)
1119 if (procstat->type == PROCSTAT_KVM) {
1120 return (procstat_get_shm_info_kvm(procstat->kd, fst, shm,
1122 } else if (procstat->type == PROCSTAT_SYSCTL ||
1123 procstat->type == PROCSTAT_CORE) {
1124 return (procstat_get_shm_info_sysctl(fst, shm, errbuf));
1126 warnx("unknown access method: %d", procstat->type);
1128 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1134 procstat_get_shm_info_kvm(kvm_t *kd, struct filestat *fst,
1135 struct shmstat *shm, char *errbuf)
1145 bzero(shm, sizeof(*shm));
1146 shmfdp = fst->fs_typedep;
1149 if (!kvm_read_all(kd, (unsigned long)shmfdp, &shmfd,
1150 sizeof(struct shmfd))) {
1151 warnx("can't read shmfd at %p", (void *)shmfdp);
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,
1162 if (path[i] == '\0')
1169 fst->fs_path = path;
1175 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1180 procstat_get_shm_info_sysctl(struct filestat *fst, struct shmstat *shm,
1181 char *errbuf __unused)
1183 struct kinfo_file *kif;
1187 bzero(shm, sizeof(*shm));
1188 kif = fst->fs_typedep;
1191 shm->size = kif->kf_un.kf_file.kf_file_size;
1192 shm->mode = kif->kf_un.kf_file.kf_file_mode;
1197 procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
1198 struct vnstat *vn, char *errbuf)
1202 if (procstat->type == PROCSTAT_KVM) {
1203 return (procstat_get_vnode_info_kvm(procstat->kd, fst, vn,
1205 } else if (procstat->type == PROCSTAT_SYSCTL ||
1206 procstat->type == PROCSTAT_CORE) {
1207 return (procstat_get_vnode_info_sysctl(fst, vn, errbuf));
1209 warnx("unknown access method: %d", procstat->type);
1211 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1217 procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
1218 struct vnstat *vn, char *errbuf)
1220 /* Filesystem specific handlers. */
1221 #define FSTYPE(fst) {#fst, fst##_filestat}
1224 int (*handler)(kvm_t *kd, struct vnode *vp,
1234 #ifdef LIBPROCSTAT_ZFS
1238 #define NTYPES (sizeof(fstypes) / sizeof(*fstypes))
1248 vp = fst->fs_typedep;
1251 error = kvm_read_all(kd, (unsigned long)vp, &vnode, sizeof(vnode));
1253 warnx("can't read vnode at %p", (void *)vp);
1256 bzero(vn, sizeof(*vn));
1257 vn->vn_type = vntype2psfsttype(vnode.v_type);
1258 if (vnode.v_type == VNON || vnode.v_type == VBAD)
1260 error = kvm_read_all(kd, (unsigned long)vnode.v_tag, tagstr,
1263 warnx("can't read v_tag at %p", (void *)vp);
1266 tagstr[sizeof(tagstr) - 1] = '\0';
1269 * Find appropriate handler.
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) {
1280 snprintf(errbuf, _POSIX2_LINE_MAX, "?(%s)", tagstr);
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);
1295 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1300 * kinfo vnode type to filestat translation.
1303 kinfo_vtype2fst(int kfvtype)
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 }
1319 #define NKFVTYPES (sizeof(kfvtypes2fst) / sizeof(*kfvtypes2fst))
1322 for (i = 0; i < NKFVTYPES; i++)
1323 if (kfvtypes2fst[i].kf_vtype == kfvtype)
1326 return (PS_FST_VTYPE_UNKNOWN);
1327 return (kfvtypes2fst[i].fst_vtype);
1331 procstat_get_vnode_info_sysctl(struct filestat *fst, struct vnstat *vn,
1334 struct statfs stbuf;
1335 struct kinfo_file *kif;
1336 struct kinfo_vmentry *kve;
1348 bzero(vn, sizeof(*vn));
1349 if (fst->fs_typedep == NULL)
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;
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;
1372 vn->vn_type = vntype;
1373 if (vntype == PS_FST_VTYPE_VNON || vntype == PS_FST_VTYPE_VBAD)
1375 if ((status & KF_ATTR_VALID) == 0) {
1376 if (errbuf != NULL) {
1377 snprintf(errbuf, _POSIX2_LINE_MAX,
1378 "? (no info available)");
1382 if (path && *path) {
1383 statfs(path, &stbuf);
1384 vn->vn_mntdir = strdup(stbuf.f_mntonname);
1386 vn->vn_mntdir = strdup("-");
1388 if (vntype == PS_FST_VTYPE_VBLK) {
1389 name = devname(rdev, S_IFBLK);
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);
1396 strlcpy(vn->vn_devname, name,
1397 sizeof(vn->vn_devname));
1400 vn->vn_fileid = fileid;
1407 procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
1408 struct sockstat *sock, char *errbuf)
1412 if (procstat->type == PROCSTAT_KVM) {
1413 return (procstat_get_socket_info_kvm(procstat->kd, fst, sock,
1415 } else if (procstat->type == PROCSTAT_SYSCTL ||
1416 procstat->type == PROCSTAT_CORE) {
1417 return (procstat_get_socket_info_sysctl(fst, sock, errbuf));
1419 warnx("unknown access method: %d", procstat->type);
1421 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1427 procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst,
1428 struct sockstat *sock, char *errbuf)
1432 struct protosw proto;
1441 bzero(sock, sizeof(*sock));
1442 so = fst->fs_typedep;
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);
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);
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);
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';
1471 sock->dname[len] = '\0';
1474 * Fill in known data.
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;
1482 * Protocol specific data.
1484 switch(dom.dom_family) {
1487 if (proto.pr_protocol == IPPROTO_TCP) {
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",
1496 (uintptr_t)inpcb.inp_ppcb;
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",
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;
1520 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1525 procstat_get_socket_info_sysctl(struct filestat *fst, struct sockstat *sock,
1526 char *errbuf __unused)
1528 struct kinfo_file *kif;
1532 bzero(sock, sizeof(*sock));
1533 kif = fst->fs_typedep;
1538 * Fill in known data.
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);
1549 * Protocol specific data.
1551 switch(sock->dom_family) {
1554 if (sock->proto == IPPROTO_TCP)
1555 sock->inp_ppcb = kif->kf_un.kf_sock.kf_sock_inpcb;
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;
1564 kif->kf_un.kf_sock.kf_sock_unpconn;
1574 * Descriptor flags to filestat translation.
1577 to_filestat_flags(int flags)
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 }
1598 #define NFSTFLAGS (sizeof(fstflags) / sizeof(*fstflags))
1603 for (i = 0; i < NFSTFLAGS; i++)
1604 if (flags & fstflags[i].flag)
1605 fst_flags |= fstflags[i].fst_flag;
1610 * Vnode type to filestate translation.
1613 vntype2psfsttype(int type)
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 }
1629 #define NVFTYPES (sizeof(vt2fst) / sizeof(*vt2fst))
1630 unsigned int i, fst_type;
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;
1643 getmnton(kvm_t *kd, struct mount *m)
1646 static struct mtab {
1649 char mntonname[MNAMELEN + 1];
1653 for (mt = mhead; mt != NULL; mt = mt->next)
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);
1660 if ((mt = malloc(sizeof (struct mtab))) == NULL)
1663 bcopy(&mnt.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
1664 mt->mntonname[MNAMELEN] = '\0';
1667 return (mt->mntonname);
1671 * Auxiliary structures and functions to get process environment or
1672 * command line arguments.
1681 static struct argvec *
1682 argvec_alloc(size_t bufsize)
1686 av = malloc(sizeof(*av));
1689 av->bufsize = bufsize;
1690 av->buf = malloc(av->bufsize);
1691 if (av->buf == NULL) {
1696 av->argv = malloc(sizeof(char *) * av->argc);
1697 if (av->argv == NULL) {
1706 argvec_free(struct argvec * av)
1715 getargv(struct procstat *procstat, struct kinfo_proc *kp, size_t nchr, int env)
1717 int error, name[4], argc, i;
1718 struct argvec *av, **avp;
1725 if (procstat->type == PROCSTAT_KVM) {
1726 warnx("can't use kvm access method");
1729 if (procstat->type != PROCSTAT_SYSCTL &&
1730 procstat->type != PROCSTAT_CORE) {
1731 warnx("unknown access method: %d", procstat->type);
1735 if (nchr == 0 || nchr > ARG_MAX)
1738 avp = (struct argvec **)(env ? &procstat->argv : &procstat->envv);
1743 av = argvec_alloc(nchr);
1746 warn("malloc(%zu)", nchr);
1750 } else if (av->bufsize < nchr) {
1751 av->buf = reallocf(av->buf, nchr);
1752 if (av->buf == NULL) {
1753 warn("malloc(%zu)", nchr);
1757 if (procstat->type == PROCSTAT_SYSCTL) {
1759 name[1] = KERN_PROC;
1760 name[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS;
1761 name[3] = kp->ki_pid;
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)
1768 } else /* procstat->type == PROCSTAT_CORE */ {
1769 type = env ? PSC_TYPE_ENVV : PSC_TYPE_ARGV;
1771 if (procstat_core_get(procstat->core, type, av->buf, &len)
1780 for (p = av->buf; p < av->buf + len; p += strlen(p) + 1) {
1786 argv = realloc(argv, sizeof(char *) * argc);
1788 warn("malloc(%zu)", sizeof(char *) * argc);
1800 * Return process command line arguments.
1803 procstat_getargv(struct procstat *procstat, struct kinfo_proc *p, size_t nchr)
1806 return (getargv(procstat, p, nchr, 0));
1810 * Free the buffer allocated by procstat_getargv().
1813 procstat_freeargv(struct procstat *procstat)
1816 if (procstat->argv != NULL) {
1817 argvec_free(procstat->argv);
1818 procstat->argv = NULL;
1823 * Return process environment.
1826 procstat_getenvv(struct procstat *procstat, struct kinfo_proc *p, size_t nchr)
1829 return (getargv(procstat, p, nchr, 1));
1833 * Free the buffer allocated by procstat_getenvv().
1836 procstat_freeenvv(struct procstat *procstat)
1838 if (procstat->envv != NULL) {
1839 argvec_free(procstat->envv);
1840 procstat->envv = NULL;
1844 static struct kinfo_vmentry *
1845 kinfo_getvmmap_core(struct procstat_core *core, int *cntp)
1849 char *buf, *bp, *eb;
1850 struct kinfo_vmentry *kiv, *kp, *kv;
1852 buf = procstat_core_get(core, PSC_TYPE_VMMAP, NULL, &len);
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);
1864 /* Pass 1: count items */
1869 kv = (struct kinfo_vmentry *)(uintptr_t)bp;
1870 if (kv->kve_structsize == 0)
1872 bp += kv->kve_structsize;
1876 kiv = calloc(cnt, sizeof(*kiv));
1884 /* Pass 2: unpack */
1886 kv = (struct kinfo_vmentry *)(uintptr_t)bp;
1887 if (kv->kve_structsize == 0)
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);
1899 return (kiv); /* Caller must free() return value */
1902 struct kinfo_vmentry *
1903 procstat_getvmmap(struct procstat *procstat, struct kinfo_proc *kp,
1907 switch(procstat->type) {
1909 warnx("kvm method is not supported");
1911 case PROCSTAT_SYSCTL:
1912 return (kinfo_getvmmap(kp->ki_pid, cntp));
1914 return (kinfo_getvmmap_core(procstat->core, cntp));
1916 warnx("unknown access method: %d", procstat->type);
1922 procstat_freevmmap(struct procstat *procstat __unused,
1923 struct kinfo_vmentry *vmmap)
1930 procstat_getgroups_kvm(kvm_t *kd, struct kinfo_proc *kp, unsigned int *cntp)
1939 if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
1941 warnx("can't read proc struct at %p for pid %d",
1942 kp->ki_paddr, kp->ki_pid);
1945 if (proc.p_ucred == NOCRED)
1947 if (!kvm_read_all(kd, (unsigned long)proc.p_ucred, &ucred,
1949 warnx("can't read ucred struct at %p for pid %d",
1950 proc.p_ucred, kp->ki_pid);
1953 len = ucred.cr_ngroups * sizeof(gid_t);
1954 groups = malloc(len);
1955 if (groups == NULL) {
1956 warn("malloc(%zu)", len);
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);
1965 *cntp = ucred.cr_ngroups;
1970 procstat_getgroups_sysctl(pid_t pid, unsigned int *cntp)
1978 mib[2] = KERN_PROC_GROUPS;
1980 len = (sysconf(_SC_NGROUPS_MAX) + 1) * sizeof(gid_t);
1981 groups = malloc(len);
1982 if (groups == NULL) {
1983 warn("malloc(%zu)", len);
1986 if (sysctl(mib, nitems(mib), groups, &len, NULL, 0) == -1) {
1987 warn("sysctl: kern.proc.groups: %d", pid);
1991 *cntp = len / sizeof(gid_t);
1996 procstat_getgroups_core(struct procstat_core *core, unsigned int *cntp)
2001 groups = procstat_core_get(core, PSC_TYPE_GROUPS, NULL, &len);
2004 *cntp = len / sizeof(gid_t);
2009 procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
2012 switch(procstat->type) {
2014 return (procstat_getgroups_kvm(procstat->kd, kp, cntp));
2015 case PROCSTAT_SYSCTL:
2016 return (procstat_getgroups_sysctl(kp->ki_pid, cntp));
2018 return (procstat_getgroups_core(procstat->core, cntp));
2020 warnx("unknown access method: %d", procstat->type);
2026 procstat_freegroups(struct procstat *procstat __unused, gid_t *groups)
2033 procstat_getumask_kvm(kvm_t *kd, struct kinfo_proc *kp, unsigned short *maskp)
2039 if (kp->ki_fd == NULL)
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,
2046 *maskp = fd.fd_cmask;
2051 procstat_getumask_sysctl(pid_t pid, unsigned short *maskp)
2059 mib[2] = KERN_PROC_UMASK;
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);
2069 procstat_getumask_core(struct procstat_core *core, unsigned short *maskp)
2072 unsigned short *buf;
2074 buf = procstat_core_get(core, PSC_TYPE_UMASK, NULL, &len);
2077 if (len < sizeof(*maskp)) {
2087 procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
2088 unsigned short *maskp)
2090 switch(procstat->type) {
2092 return (procstat_getumask_kvm(procstat->kd, kp, maskp));
2093 case PROCSTAT_SYSCTL:
2094 return (procstat_getumask_sysctl(kp->ki_pid, maskp));
2096 return (procstat_getumask_core(procstat->core, maskp));
2098 warnx("unknown access method: %d", procstat->type);
2104 procstat_getrlimit_kvm(kvm_t *kd, struct kinfo_proc *kp, int which,
2105 struct rlimit* rlimit)
2108 unsigned long offset;
2112 assert(which >= 0 && which < RLIM_NLIMITS);
2113 if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
2115 warnx("can't read proc struct at %p for pid %d",
2116 kp->ki_paddr, kp->ki_pid);
2119 if (proc.p_limit == NULL)
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);
2131 procstat_getrlimit_sysctl(pid_t pid, int which, struct rlimit* rlimit)
2137 name[1] = KERN_PROC;
2138 name[2] = KERN_PROC_RLIMIT;
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);
2147 if (error < 0 || len != sizeof(struct rlimit))
2153 procstat_getrlimit_core(struct procstat_core *core, int which,
2154 struct rlimit* rlimit)
2157 struct rlimit* rlimits;
2159 if (which < 0 || which >= RLIM_NLIMITS) {
2161 warn("getrlimit: which");
2164 rlimits = procstat_core_get(core, PSC_TYPE_RLIMIT, NULL, &len);
2165 if (rlimits == NULL)
2167 if (len < sizeof(struct rlimit) * RLIM_NLIMITS) {
2171 *rlimit = rlimits[which];
2176 procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp, int which,
2177 struct rlimit* rlimit)
2179 switch(procstat->type) {
2181 return (procstat_getrlimit_kvm(procstat->kd, kp, which,
2183 case PROCSTAT_SYSCTL:
2184 return (procstat_getrlimit_sysctl(kp->ki_pid, which, rlimit));
2186 return (procstat_getrlimit_core(procstat->core, which, rlimit));
2188 warnx("unknown access method: %d", procstat->type);
2194 procstat_getpathname_sysctl(pid_t pid, char *pathname, size_t maxlen)
2200 name[1] = KERN_PROC;
2201 name[2] = KERN_PROC_PATHNAME;
2204 error = sysctl(name, nitems(name), pathname, &len, NULL, 0);
2205 if (error != 0 && errno != ESRCH)
2206 warn("sysctl: kern.proc.pathname: %d", pid);
2213 procstat_getpathname_core(struct procstat_core *core, char *pathname,
2216 struct kinfo_file *files;
2219 files = kinfo_getfile_core(core, &cnt);
2223 for (i = 0; i < cnt; i++) {
2224 if (files[i].kf_fd != KF_FD_TYPE_TEXT)
2226 strncpy(pathname, files[i].kf_path, maxlen);
2235 procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
2236 char *pathname, size_t maxlen)
2238 switch(procstat->type) {
2240 /* XXX: Return empty string. */
2244 case PROCSTAT_SYSCTL:
2245 return (procstat_getpathname_sysctl(kp->ki_pid, pathname,
2248 return (procstat_getpathname_core(procstat->core, pathname,
2251 warnx("unknown access method: %d", procstat->type);
2257 procstat_getosrel_kvm(kvm_t *kd, struct kinfo_proc *kp, int *osrelp)
2263 if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
2265 warnx("can't read proc struct at %p for pid %d",
2266 kp->ki_paddr, kp->ki_pid);
2269 *osrelp = proc.p_osrel;
2274 procstat_getosrel_sysctl(pid_t pid, int *osrelp)
2280 name[1] = KERN_PROC;
2281 name[2] = KERN_PROC_OSREL;
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);
2291 procstat_getosrel_core(struct procstat_core *core, int *osrelp)
2296 buf = procstat_core_get(core, PSC_TYPE_OSREL, NULL, &len);
2299 if (len < sizeof(*osrelp)) {
2309 procstat_getosrel(struct procstat *procstat, struct kinfo_proc *kp, int *osrelp)
2311 switch(procstat->type) {
2313 return (procstat_getosrel_kvm(procstat->kd, kp, osrelp));
2314 case PROCSTAT_SYSCTL:
2315 return (procstat_getosrel_sysctl(kp->ki_pid, osrelp));
2317 return (procstat_getosrel_core(procstat->core, osrelp));
2319 warnx("unknown access method: %d", procstat->type);
2324 #define PROC_AUXV_MAX 256
2326 #if __ELF_WORD_SIZE == 64
2327 static const char *elf32_sv_names[] = {
2333 is_elf32_sysctl(pid_t pid)
2337 static char sv_name[256];
2340 name[1] = KERN_PROC;
2341 name[2] = KERN_PROC_SV_NAME;
2343 len = sizeof(sv_name);
2344 error = sysctl(name, nitems(name), sv_name, &len, NULL, 0);
2345 if (error != 0 || len == 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)
2354 static Elf_Auxinfo *
2355 procstat_getauxv32_sysctl(pid_t pid, unsigned int *cntp)
2358 Elf32_Auxinfo *auxv32;
2361 unsigned int i, count;
2365 name[1] = KERN_PROC;
2366 name[2] = KERN_PROC_AUXV;
2368 len = PROC_AUXV_MAX * sizeof(Elf32_Auxinfo);
2370 auxv32 = malloc(len);
2371 if (auxv32 == NULL) {
2372 warn("malloc(%zu)", len);
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);
2380 count = len / sizeof(Elf_Auxinfo);
2381 auxv = malloc(count * sizeof(Elf_Auxinfo));
2383 warn("malloc(%zu)", count * sizeof(Elf_Auxinfo));
2386 for (i = 0; i < count; i++) {
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
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);
2401 #endif /* __ELF_WORD_SIZE == 64 */
2403 static Elf_Auxinfo *
2404 procstat_getauxv_sysctl(pid_t pid, unsigned int *cntp)
2410 #if __ELF_WORD_SIZE == 64
2411 if (is_elf32_sysctl(pid))
2412 return (procstat_getauxv32_sysctl(pid, cntp));
2415 name[1] = KERN_PROC;
2416 name[2] = KERN_PROC_AUXV;
2418 len = PROC_AUXV_MAX * sizeof(Elf_Auxinfo);
2421 warn("malloc(%zu)", len);
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);
2430 *cntp = len / sizeof(Elf_Auxinfo);
2434 static Elf_Auxinfo *
2435 procstat_getauxv_core(struct procstat_core *core, unsigned int *cntp)
2440 auxv = procstat_core_get(core, PSC_TYPE_AUXV, NULL, &len);
2443 *cntp = len / sizeof(Elf_Auxinfo);
2448 procstat_getauxv(struct procstat *procstat, struct kinfo_proc *kp,
2451 switch(procstat->type) {
2453 warnx("kvm method is not supported");
2455 case PROCSTAT_SYSCTL:
2456 return (procstat_getauxv_sysctl(kp->ki_pid, cntp));
2458 return (procstat_getauxv_core(procstat->core, cntp));
2460 warnx("unknown access method: %d", procstat->type);
2466 procstat_freeauxv(struct procstat *procstat __unused, Elf_Auxinfo *auxv)
2472 static struct kinfo_kstack *
2473 procstat_getkstack_sysctl(pid_t pid, int *cntp)
2475 struct kinfo_kstack *kkstp;
2480 name[1] = KERN_PROC;
2481 name[2] = KERN_PROC_KSTACK;
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);
2490 if (error == -1 && errno == ENOENT) {
2491 warnx("sysctl: kern.proc.kstack unavailable"
2492 " (options DDB or options STACK required in kernel)");
2497 kkstp = malloc(len);
2498 if (kkstp == NULL) {
2499 warn("malloc(%zu)", len);
2502 if (sysctl(name, nitems(name), kkstp, &len, NULL, 0) == -1) {
2503 warn("sysctl: kern.proc.pid: %d", pid);
2507 *cntp = len / sizeof(*kkstp);
2512 struct kinfo_kstack *
2513 procstat_getkstack(struct procstat *procstat, struct kinfo_proc *kp,
2516 switch(procstat->type) {
2518 warnx("kvm method is not supported");
2520 case PROCSTAT_SYSCTL:
2521 return (procstat_getkstack_sysctl(kp->ki_pid, cntp));
2523 warnx("core method is not supported");
2526 warnx("unknown access method: %d", procstat->type);
2532 procstat_freekstack(struct procstat *procstat __unused,
2533 struct kinfo_kstack *kkstp)