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