xref: /freebsd/lib/libprocstat/libprocstat.c (revision 567e6250c003eeb251b4bc8dbe60d2adabab2988)
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 
1977 	assert(kd != NULL);
1978 	assert(kp != NULL);
1979 	if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
1980 	    sizeof(proc))) {
1981 		warnx("can't read proc struct at %p for pid %d",
1982 		    kp->ki_paddr, kp->ki_pid);
1983 		return (NULL);
1984 	}
1985 	if (proc.p_ucred == NOCRED)
1986 		return (NULL);
1987 	if (!kvm_read_all(kd, (unsigned long)proc.p_ucred, &ucred,
1988 	    sizeof(ucred))) {
1989 		warnx("can't read ucred struct at %p for pid %d",
1990 		    proc.p_ucred, kp->ki_pid);
1991 		return (NULL);
1992 	}
1993 	len = ucred.cr_ngroups * sizeof(gid_t);
1994 	groups = malloc(len);
1995 	if (groups == NULL) {
1996 		warn("malloc(%zu)", len);
1997 		return (NULL);
1998 	}
1999 	if (!kvm_read_all(kd, (unsigned long)ucred.cr_groups, groups, len)) {
2000 		warnx("can't read groups at %p for pid %d",
2001 		    ucred.cr_groups, kp->ki_pid);
2002 		free(groups);
2003 		return (NULL);
2004 	}
2005 	*cntp = ucred.cr_ngroups;
2006 	return (groups);
2007 }
2008 
2009 static gid_t *
procstat_getgroups_sysctl(pid_t pid,unsigned int * cntp)2010 procstat_getgroups_sysctl(pid_t pid, unsigned int *cntp)
2011 {
2012 	int mib[4];
2013 	size_t len;
2014 	gid_t *groups;
2015 
2016 	mib[0] = CTL_KERN;
2017 	mib[1] = KERN_PROC;
2018 	mib[2] = KERN_PROC_GROUPS;
2019 	mib[3] = pid;
2020 	len = (sysconf(_SC_NGROUPS_MAX) + 1) * sizeof(gid_t);
2021 	groups = malloc(len);
2022 	if (groups == NULL) {
2023 		warn("malloc(%zu)", len);
2024 		return (NULL);
2025 	}
2026 	if (sysctl(mib, nitems(mib), groups, &len, NULL, 0) == -1) {
2027 		warn("sysctl: kern.proc.groups: %d", pid);
2028 		free(groups);
2029 		return (NULL);
2030 	}
2031 	*cntp = len / sizeof(gid_t);
2032 	return (groups);
2033 }
2034 
2035 static gid_t *
procstat_getgroups_core(struct procstat_core * core,unsigned int * cntp)2036 procstat_getgroups_core(struct procstat_core *core, unsigned int *cntp)
2037 {
2038 	size_t len;
2039 	gid_t *groups;
2040 
2041 	groups = procstat_core_get(core, PSC_TYPE_GROUPS, NULL, &len);
2042 	if (groups == NULL)
2043 		return (NULL);
2044 	*cntp = len / sizeof(gid_t);
2045 	return (groups);
2046 }
2047 
2048 gid_t *
procstat_getgroups(struct procstat * procstat,struct kinfo_proc * kp,unsigned int * cntp)2049 procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
2050     unsigned int *cntp)
2051 {
2052 	switch (procstat->type) {
2053 	case PROCSTAT_KVM:
2054 		return (procstat_getgroups_kvm(procstat->kd, kp, cntp));
2055 	case PROCSTAT_SYSCTL:
2056 		return (procstat_getgroups_sysctl(kp->ki_pid, cntp));
2057 	case PROCSTAT_CORE:
2058 		return (procstat_getgroups_core(procstat->core, cntp));
2059 	default:
2060 		warnx("unknown access method: %d", procstat->type);
2061 		return (NULL);
2062 	}
2063 }
2064 
2065 void
procstat_freegroups(struct procstat * procstat __unused,gid_t * groups)2066 procstat_freegroups(struct procstat *procstat __unused, gid_t *groups)
2067 {
2068 
2069 	free(groups);
2070 }
2071 
2072 static int
procstat_getumask_kvm(kvm_t * kd,struct kinfo_proc * kp,unsigned short * maskp)2073 procstat_getumask_kvm(kvm_t *kd, struct kinfo_proc *kp, unsigned short *maskp)
2074 {
2075 	struct pwddesc pd;
2076 
2077 	assert(kd != NULL);
2078 	assert(kp != NULL);
2079 	if (kp->ki_pd == NULL)
2080 		return (-1);
2081 	if (!kvm_read_all(kd, (unsigned long)kp->ki_pd, &pd, sizeof(pd))) {
2082 		warnx("can't read pwddesc at %p for pid %d", kp->ki_pd,
2083 		    kp->ki_pid);
2084 		return (-1);
2085 	}
2086 	*maskp = pd.pd_cmask;
2087 	return (0);
2088 }
2089 
2090 static int
procstat_getumask_sysctl(pid_t pid,unsigned short * maskp)2091 procstat_getumask_sysctl(pid_t pid, unsigned short *maskp)
2092 {
2093 	int error;
2094 	int mib[4];
2095 	size_t len;
2096 
2097 	mib[0] = CTL_KERN;
2098 	mib[1] = KERN_PROC;
2099 	mib[2] = KERN_PROC_UMASK;
2100 	mib[3] = pid;
2101 	len = sizeof(*maskp);
2102 	error = sysctl(mib, nitems(mib), maskp, &len, NULL, 0);
2103 	if (error != 0 && errno != ESRCH && errno != EPERM)
2104 		warn("sysctl: kern.proc.umask: %d", pid);
2105 	return (error);
2106 }
2107 
2108 static int
procstat_getumask_core(struct procstat_core * core,unsigned short * maskp)2109 procstat_getumask_core(struct procstat_core *core, unsigned short *maskp)
2110 {
2111 	size_t len;
2112 	unsigned short *buf;
2113 
2114 	buf = procstat_core_get(core, PSC_TYPE_UMASK, NULL, &len);
2115 	if (buf == NULL)
2116 		return (-1);
2117 	if (len < sizeof(*maskp)) {
2118 		free(buf);
2119 		return (-1);
2120 	}
2121 	*maskp = *buf;
2122 	free(buf);
2123 	return (0);
2124 }
2125 
2126 int
procstat_getumask(struct procstat * procstat,struct kinfo_proc * kp,unsigned short * maskp)2127 procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
2128     unsigned short *maskp)
2129 {
2130 	switch (procstat->type) {
2131 	case PROCSTAT_KVM:
2132 		return (procstat_getumask_kvm(procstat->kd, kp, maskp));
2133 	case PROCSTAT_SYSCTL:
2134 		return (procstat_getumask_sysctl(kp->ki_pid, maskp));
2135 	case PROCSTAT_CORE:
2136 		return (procstat_getumask_core(procstat->core, maskp));
2137 	default:
2138 		warnx("unknown access method: %d", procstat->type);
2139 		return (-1);
2140 	}
2141 }
2142 
2143 static int
procstat_getrlimit_kvm(kvm_t * kd,struct kinfo_proc * kp,int which,struct rlimit * rlimit)2144 procstat_getrlimit_kvm(kvm_t *kd, struct kinfo_proc *kp, int which,
2145     struct rlimit* rlimit)
2146 {
2147 	struct proc proc;
2148 	unsigned long offset;
2149 
2150 	assert(kd != NULL);
2151 	assert(kp != NULL);
2152 	assert(which >= 0 && which < RLIM_NLIMITS);
2153 	if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
2154 	    sizeof(proc))) {
2155 		warnx("can't read proc struct at %p for pid %d",
2156 		    kp->ki_paddr, kp->ki_pid);
2157 		return (-1);
2158 	}
2159 	if (proc.p_limit == NULL)
2160 		return (-1);
2161 	offset = (unsigned long)proc.p_limit + sizeof(struct rlimit) * which;
2162 	if (!kvm_read_all(kd, offset, rlimit, sizeof(*rlimit))) {
2163 		warnx("can't read rlimit struct at %p for pid %d",
2164 		    (void *)offset, kp->ki_pid);
2165 		return (-1);
2166 	}
2167 	return (0);
2168 }
2169 
2170 static int
procstat_getrlimit_sysctl(pid_t pid,int which,struct rlimit * rlimit)2171 procstat_getrlimit_sysctl(pid_t pid, int which, struct rlimit* rlimit)
2172 {
2173 	int error, name[5];
2174 	size_t len;
2175 
2176 	name[0] = CTL_KERN;
2177 	name[1] = KERN_PROC;
2178 	name[2] = KERN_PROC_RLIMIT;
2179 	name[3] = pid;
2180 	name[4] = which;
2181 	len = sizeof(struct rlimit);
2182 	error = sysctl(name, nitems(name), rlimit, &len, NULL, 0);
2183 	if (error < 0 && errno != ESRCH) {
2184 		warn("sysctl: kern.proc.rlimit: %d", pid);
2185 		return (-1);
2186 	}
2187 	if (error < 0 || len != sizeof(struct rlimit))
2188 		return (-1);
2189 	return (0);
2190 }
2191 
2192 static int
procstat_getrlimit_core(struct procstat_core * core,int which,struct rlimit * rlimit)2193 procstat_getrlimit_core(struct procstat_core *core, int which,
2194     struct rlimit* rlimit)
2195 {
2196 	size_t len;
2197 	struct rlimit* rlimits;
2198 
2199 	if (which < 0 || which >= RLIM_NLIMITS) {
2200 		errno = EINVAL;
2201 		warn("getrlimit: which");
2202 		return (-1);
2203 	}
2204 	rlimits = procstat_core_get(core, PSC_TYPE_RLIMIT, NULL, &len);
2205 	if (rlimits == NULL)
2206 		return (-1);
2207 	if (len < sizeof(struct rlimit) * RLIM_NLIMITS) {
2208 		free(rlimits);
2209 		return (-1);
2210 	}
2211 	*rlimit = rlimits[which];
2212 	free(rlimits);
2213 	return (0);
2214 }
2215 
2216 int
procstat_getrlimit(struct procstat * procstat,struct kinfo_proc * kp,int which,struct rlimit * rlimit)2217 procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp, int which,
2218     struct rlimit* rlimit)
2219 {
2220 	switch (procstat->type) {
2221 	case PROCSTAT_KVM:
2222 		return (procstat_getrlimit_kvm(procstat->kd, kp, which,
2223 		    rlimit));
2224 	case PROCSTAT_SYSCTL:
2225 		return (procstat_getrlimit_sysctl(kp->ki_pid, which, rlimit));
2226 	case PROCSTAT_CORE:
2227 		return (procstat_getrlimit_core(procstat->core, which, rlimit));
2228 	default:
2229 		warnx("unknown access method: %d", procstat->type);
2230 		return (-1);
2231 	}
2232 }
2233 
2234 static int
procstat_getpathname_sysctl(pid_t pid,char * pathname,size_t maxlen)2235 procstat_getpathname_sysctl(pid_t pid, char *pathname, size_t maxlen)
2236 {
2237 	int error, name[4];
2238 	size_t len;
2239 
2240 	name[0] = CTL_KERN;
2241 	name[1] = KERN_PROC;
2242 	name[2] = KERN_PROC_PATHNAME;
2243 	name[3] = pid;
2244 	len = maxlen;
2245 	error = sysctl(name, nitems(name), pathname, &len, NULL, 0);
2246 	if (error != 0 && errno != ESRCH)
2247 		warn("sysctl: kern.proc.pathname: %d", pid);
2248 	if (len == 0)
2249 		pathname[0] = '\0';
2250 	return (error);
2251 }
2252 
2253 static int
procstat_getpathname_core(struct procstat_core * core,char * pathname,size_t maxlen)2254 procstat_getpathname_core(struct procstat_core *core, char *pathname,
2255     size_t maxlen)
2256 {
2257 	struct kinfo_file *files;
2258 	int cnt, i, result;
2259 
2260 	files = kinfo_getfile_core(core, &cnt);
2261 	if (files == NULL)
2262 		return (-1);
2263 	result = -1;
2264 	for (i = 0; i < cnt; i++) {
2265 		if (files[i].kf_fd != KF_FD_TYPE_TEXT)
2266 			continue;
2267 		strncpy(pathname, files[i].kf_path, maxlen);
2268 		result = 0;
2269 		break;
2270 	}
2271 	free(files);
2272 	return (result);
2273 }
2274 
2275 int
procstat_getpathname(struct procstat * procstat,struct kinfo_proc * kp,char * pathname,size_t maxlen)2276 procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
2277     char *pathname, size_t maxlen)
2278 {
2279 	switch (procstat->type) {
2280 	case PROCSTAT_KVM:
2281 		/* XXX: Return empty string. */
2282 		if (maxlen > 0)
2283 			pathname[0] = '\0';
2284 		return (0);
2285 	case PROCSTAT_SYSCTL:
2286 		return (procstat_getpathname_sysctl(kp->ki_pid, pathname,
2287 		    maxlen));
2288 	case PROCSTAT_CORE:
2289 		return (procstat_getpathname_core(procstat->core, pathname,
2290 		    maxlen));
2291 	default:
2292 		warnx("unknown access method: %d", procstat->type);
2293 		return (-1);
2294 	}
2295 }
2296 
2297 static int
procstat_getosrel_kvm(kvm_t * kd,struct kinfo_proc * kp,int * osrelp)2298 procstat_getosrel_kvm(kvm_t *kd, struct kinfo_proc *kp, int *osrelp)
2299 {
2300 	struct proc proc;
2301 
2302 	assert(kd != NULL);
2303 	assert(kp != NULL);
2304 	if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
2305 	    sizeof(proc))) {
2306 		warnx("can't read proc struct at %p for pid %d",
2307 		    kp->ki_paddr, kp->ki_pid);
2308 		return (-1);
2309 	}
2310 	*osrelp = proc.p_osrel;
2311 	return (0);
2312 }
2313 
2314 static int
procstat_getosrel_sysctl(pid_t pid,int * osrelp)2315 procstat_getosrel_sysctl(pid_t pid, int *osrelp)
2316 {
2317 	int error, name[4];
2318 	size_t len;
2319 
2320 	name[0] = CTL_KERN;
2321 	name[1] = KERN_PROC;
2322 	name[2] = KERN_PROC_OSREL;
2323 	name[3] = pid;
2324 	len = sizeof(*osrelp);
2325 	error = sysctl(name, nitems(name), osrelp, &len, NULL, 0);
2326 	if (error != 0 && errno != ESRCH)
2327 		warn("sysctl: kern.proc.osrel: %d", pid);
2328 	return (error);
2329 }
2330 
2331 static int
procstat_getosrel_core(struct procstat_core * core,int * osrelp)2332 procstat_getosrel_core(struct procstat_core *core, int *osrelp)
2333 {
2334 	size_t len;
2335 	int *buf;
2336 
2337 	buf = procstat_core_get(core, PSC_TYPE_OSREL, NULL, &len);
2338 	if (buf == NULL)
2339 		return (-1);
2340 	if (len < sizeof(*osrelp)) {
2341 		free(buf);
2342 		return (-1);
2343 	}
2344 	*osrelp = *buf;
2345 	free(buf);
2346 	return (0);
2347 }
2348 
2349 int
procstat_getosrel(struct procstat * procstat,struct kinfo_proc * kp,int * osrelp)2350 procstat_getosrel(struct procstat *procstat, struct kinfo_proc *kp, int *osrelp)
2351 {
2352 	switch (procstat->type) {
2353 	case PROCSTAT_KVM:
2354 		return (procstat_getosrel_kvm(procstat->kd, kp, osrelp));
2355 	case PROCSTAT_SYSCTL:
2356 		return (procstat_getosrel_sysctl(kp->ki_pid, osrelp));
2357 	case PROCSTAT_CORE:
2358 		return (procstat_getosrel_core(procstat->core, osrelp));
2359 	default:
2360 		warnx("unknown access method: %d", procstat->type);
2361 		return (-1);
2362 	}
2363 }
2364 
2365 #define PROC_AUXV_MAX	256
2366 
2367 #ifdef PS_ARCH_HAS_FREEBSD32
2368 static const char *elf32_sv_names[] = {
2369 	"Linux ELF32",
2370 	"FreeBSD ELF32",
2371 };
2372 
2373 static int
is_elf32_sysctl(pid_t pid)2374 is_elf32_sysctl(pid_t pid)
2375 {
2376 	int error, name[4];
2377 	size_t len, i;
2378 	char sv_name[32];
2379 
2380 	name[0] = CTL_KERN;
2381 	name[1] = KERN_PROC;
2382 	name[2] = KERN_PROC_SV_NAME;
2383 	name[3] = pid;
2384 	len = sizeof(sv_name);
2385 	error = sysctl(name, nitems(name), sv_name, &len, NULL, 0);
2386 	if (error != 0 || len == 0)
2387 		return (0);
2388 	for (i = 0; i < sizeof(elf32_sv_names) / sizeof(*elf32_sv_names); i++) {
2389 		if (strncmp(sv_name, elf32_sv_names[i], sizeof(sv_name)) == 0)
2390 			return (1);
2391 	}
2392 	return (0);
2393 }
2394 
2395 static Elf_Auxinfo *
procstat_getauxv32_sysctl(pid_t pid,unsigned int * cntp)2396 procstat_getauxv32_sysctl(pid_t pid, unsigned int *cntp)
2397 {
2398 	Elf_Auxinfo *auxv;
2399 	Elf32_Auxinfo *auxv32;
2400 	size_t len;
2401 	unsigned int i, count;
2402 	int name[4];
2403 
2404 	name[0] = CTL_KERN;
2405 	name[1] = KERN_PROC;
2406 	name[2] = KERN_PROC_AUXV;
2407 	name[3] = pid;
2408 	len = PROC_AUXV_MAX * sizeof(Elf32_Auxinfo);
2409 	auxv = NULL;
2410 	auxv32 = malloc(len);
2411 	if (auxv32 == NULL) {
2412 		warn("malloc(%zu)", len);
2413 		goto out;
2414 	}
2415 	if (sysctl(name, nitems(name), auxv32, &len, NULL, 0) == -1) {
2416 		if (errno != ESRCH && errno != EPERM)
2417 			warn("sysctl: kern.proc.auxv: %d: %d", pid, errno);
2418 		goto out;
2419 	}
2420 	count = len / sizeof(Elf32_Auxinfo);
2421 	auxv = malloc(count * sizeof(Elf_Auxinfo));
2422 	if (auxv == NULL) {
2423 		warn("malloc(%zu)", count * sizeof(Elf_Auxinfo));
2424 		goto out;
2425 	}
2426 	for (i = 0; i < count; i++) {
2427 		/*
2428 		 * XXX: We expect that values for a_type on a 32-bit platform
2429 		 * are directly mapped to values on 64-bit one, which is not
2430 		 * necessarily true.
2431 		 */
2432 		auxv[i].a_type = auxv32[i].a_type;
2433 		/*
2434 		 * Don't sign extend values.  Existing entries are positive
2435 		 * integers or pointers.  Under freebsd32, programs typically
2436 		 * have a full [0, 2^32) address space (perhaps minus the last
2437 		 * page) and treating this as a signed integer would be
2438 		 * confusing since these are not kernel pointers.
2439 		 *
2440 		 * XXX: A more complete translation would be ABI and
2441 		 * type-aware.
2442 		 */
2443 		auxv[i].a_un.a_val = (uint32_t)auxv32[i].a_un.a_val;
2444 	}
2445 	*cntp = count;
2446 out:
2447 	free(auxv32);
2448 	return (auxv);
2449 }
2450 #endif /* PS_ARCH_HAS_FREEBSD32 */
2451 
2452 static Elf_Auxinfo *
procstat_getauxv_sysctl(pid_t pid,unsigned int * cntp)2453 procstat_getauxv_sysctl(pid_t pid, unsigned int *cntp)
2454 {
2455 	Elf_Auxinfo *auxv;
2456 	int name[4];
2457 	size_t len;
2458 
2459 #ifdef PS_ARCH_HAS_FREEBSD32
2460 	if (is_elf32_sysctl(pid))
2461 		return (procstat_getauxv32_sysctl(pid, cntp));
2462 #endif
2463 	name[0] = CTL_KERN;
2464 	name[1] = KERN_PROC;
2465 	name[2] = KERN_PROC_AUXV;
2466 	name[3] = pid;
2467 	len = PROC_AUXV_MAX * sizeof(Elf_Auxinfo);
2468 	auxv = malloc(len);
2469 	if (auxv == NULL) {
2470 		warn("malloc(%zu)", len);
2471 		return (NULL);
2472 	}
2473 	if (sysctl(name, nitems(name), auxv, &len, NULL, 0) == -1) {
2474 		if (errno != ESRCH && errno != EPERM)
2475 			warn("sysctl: kern.proc.auxv: %d: %d", pid, errno);
2476 		free(auxv);
2477 		return (NULL);
2478 	}
2479 	*cntp = len / sizeof(Elf_Auxinfo);
2480 	return (auxv);
2481 }
2482 
2483 static Elf_Auxinfo *
procstat_getauxv_core(struct procstat_core * core,unsigned int * cntp)2484 procstat_getauxv_core(struct procstat_core *core, unsigned int *cntp)
2485 {
2486 	Elf_Auxinfo *auxv;
2487 	size_t len;
2488 
2489 	auxv = procstat_core_get(core, PSC_TYPE_AUXV, NULL, &len);
2490 	if (auxv == NULL)
2491 		return (NULL);
2492 	*cntp = len / sizeof(Elf_Auxinfo);
2493 	return (auxv);
2494 }
2495 
2496 Elf_Auxinfo *
procstat_getauxv(struct procstat * procstat,struct kinfo_proc * kp,unsigned int * cntp)2497 procstat_getauxv(struct procstat *procstat, struct kinfo_proc *kp,
2498     unsigned int *cntp)
2499 {
2500 	switch (procstat->type) {
2501 	case PROCSTAT_KVM:
2502 		warnx("kvm method is not supported");
2503 		return (NULL);
2504 	case PROCSTAT_SYSCTL:
2505 		return (procstat_getauxv_sysctl(kp->ki_pid, cntp));
2506 	case PROCSTAT_CORE:
2507 		return (procstat_getauxv_core(procstat->core, cntp));
2508 	default:
2509 		warnx("unknown access method: %d", procstat->type);
2510 		return (NULL);
2511 	}
2512 }
2513 
2514 void
procstat_freeauxv(struct procstat * procstat __unused,Elf_Auxinfo * auxv)2515 procstat_freeauxv(struct procstat *procstat __unused, Elf_Auxinfo *auxv)
2516 {
2517 
2518 	free(auxv);
2519 }
2520 
2521 static struct ptrace_lwpinfo *
procstat_getptlwpinfo_core(struct procstat_core * core,unsigned int * cntp)2522 procstat_getptlwpinfo_core(struct procstat_core *core, unsigned int *cntp)
2523 {
2524 	void *buf;
2525 	struct ptrace_lwpinfo *pl;
2526 	unsigned int cnt;
2527 	size_t len;
2528 
2529 	cnt = procstat_core_note_count(core, PSC_TYPE_PTLWPINFO);
2530 	if (cnt == 0)
2531 		return (NULL);
2532 
2533 	len = cnt * sizeof(*pl);
2534 	buf = calloc(1, len);
2535 	pl = procstat_core_get(core, PSC_TYPE_PTLWPINFO, buf, &len);
2536 	if (pl == NULL) {
2537 		free(buf);
2538 		return (NULL);
2539 	}
2540 	*cntp = len / sizeof(*pl);
2541 	return (pl);
2542 }
2543 
2544 struct ptrace_lwpinfo *
procstat_getptlwpinfo(struct procstat * procstat,unsigned int * cntp)2545 procstat_getptlwpinfo(struct procstat *procstat, unsigned int *cntp)
2546 {
2547 	switch (procstat->type) {
2548 	case PROCSTAT_KVM:
2549 		warnx("kvm method is not supported");
2550 		return (NULL);
2551 	case PROCSTAT_SYSCTL:
2552 		warnx("sysctl method is not supported");
2553 		return (NULL);
2554 	case PROCSTAT_CORE:
2555 	 	return (procstat_getptlwpinfo_core(procstat->core, cntp));
2556 	default:
2557 		warnx("unknown access method: %d", procstat->type);
2558 		return (NULL);
2559 	}
2560 }
2561 
2562 void
procstat_freeptlwpinfo(struct procstat * procstat __unused,struct ptrace_lwpinfo * pl)2563 procstat_freeptlwpinfo(struct procstat *procstat __unused,
2564     struct ptrace_lwpinfo *pl)
2565 {
2566 	free(pl);
2567 }
2568 
2569 static struct kinfo_kstack *
procstat_getkstack_sysctl(pid_t pid,int * cntp)2570 procstat_getkstack_sysctl(pid_t pid, int *cntp)
2571 {
2572 	struct kinfo_kstack *kkstp;
2573 	int error, name[4];
2574 	size_t len;
2575 
2576 	name[0] = CTL_KERN;
2577 	name[1] = KERN_PROC;
2578 	name[2] = KERN_PROC_KSTACK;
2579 	name[3] = pid;
2580 
2581 	len = 0;
2582 	error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
2583 	if (error < 0 && errno != ESRCH && errno != EPERM && errno != ENOENT) {
2584 		warn("sysctl: kern.proc.kstack: %d", pid);
2585 		return (NULL);
2586 	}
2587 	if (error == -1 && errno == ENOENT) {
2588 		warnx("sysctl: kern.proc.kstack unavailable"
2589 		    " (options DDB or options STACK required in kernel)");
2590 		return (NULL);
2591 	}
2592 	if (error == -1)
2593 		return (NULL);
2594 	kkstp = malloc(len);
2595 	if (kkstp == NULL) {
2596 		warn("malloc(%zu)", len);
2597 		return (NULL);
2598 	}
2599 	if (sysctl(name, nitems(name), kkstp, &len, NULL, 0) == -1 &&
2600 	    errno != ENOMEM) {
2601 		warn("sysctl: kern.proc.pid: %d", pid);
2602 		free(kkstp);
2603 		return (NULL);
2604 	}
2605 	*cntp = len / sizeof(*kkstp);
2606 
2607 	return (kkstp);
2608 }
2609 
2610 struct kinfo_kstack *
procstat_getkstack(struct procstat * procstat,struct kinfo_proc * kp,unsigned int * cntp)2611 procstat_getkstack(struct procstat *procstat, struct kinfo_proc *kp,
2612     unsigned int *cntp)
2613 {
2614 	switch (procstat->type) {
2615 	case PROCSTAT_KVM:
2616 		warnx("kvm method is not supported");
2617 		return (NULL);
2618 	case PROCSTAT_SYSCTL:
2619 		return (procstat_getkstack_sysctl(kp->ki_pid, cntp));
2620 	case PROCSTAT_CORE:
2621 		warnx("core method is not supported");
2622 		return (NULL);
2623 	default:
2624 		warnx("unknown access method: %d", procstat->type);
2625 		return (NULL);
2626 	}
2627 }
2628 
2629 void
procstat_freekstack(struct procstat * procstat __unused,struct kinfo_kstack * kkstp)2630 procstat_freekstack(struct procstat *procstat __unused,
2631     struct kinfo_kstack *kkstp)
2632 {
2633 
2634 	free(kkstp);
2635 }
2636 
2637 static struct advlock_list *
procstat_getadvlock_sysctl(struct procstat * procstat __unused)2638 procstat_getadvlock_sysctl(struct procstat *procstat __unused)
2639 {
2640 	struct advlock_list *res;
2641 	struct advlock *a;
2642 	void *buf;
2643 	char *c;
2644 	struct kinfo_lockf *kl;
2645 	size_t buf_len;
2646 	int error;
2647 	static const int kl_name[] = { CTL_KERN, KERN_LOCKF };
2648 
2649 	res = malloc(sizeof(*res));
2650 	if (res == NULL)
2651 		return (NULL);
2652 	STAILQ_INIT(res);
2653 	buf = NULL;
2654 
2655 	buf_len = 0;
2656 	error = sysctl(kl_name, nitems(kl_name), NULL, &buf_len, NULL, 0);
2657 	if (error != 0) {
2658 		warn("sysctl KERN_LOCKF size");
2659 		goto fail;
2660 	}
2661 	buf_len *= 2;
2662 	buf = malloc(buf_len);
2663 	if (buf == NULL) {
2664 		warn("malloc");
2665 		goto fail;
2666 	}
2667 	error = sysctl(kl_name, nitems(kl_name), buf, &buf_len, NULL, 0);
2668 	if (error != 0) {
2669 		warn("sysctl KERN_LOCKF data");
2670 		goto fail;
2671 	}
2672 
2673 	for (c = buf; (char *)c < (char *)buf + buf_len;
2674 	    c += kl->kl_structsize) {
2675 		kl = (struct kinfo_lockf *)(void *)c;
2676 		if (sizeof(*kl) < (size_t)kl->kl_structsize) {
2677 			warn("ABI broken");
2678 			goto fail;
2679 		}
2680 		a = malloc(sizeof(*a));
2681 		if (a == NULL) {
2682 			warn("malloc advlock");
2683 			goto fail;
2684 		}
2685 		switch (kl->kl_rw) {
2686 		case KLOCKF_RW_READ:
2687 			a->rw = PS_ADVLOCK_RO;
2688 			break;
2689 		case KLOCKF_RW_WRITE:
2690 			a->rw = PS_ADVLOCK_RW;
2691 			break;
2692 		default:
2693 			warn("ABI broken");
2694 			free(a);
2695 			goto fail;
2696 		}
2697 		switch (kl->kl_type) {
2698 		case KLOCKF_TYPE_FLOCK:
2699 			a->type = PS_ADVLOCK_TYPE_FLOCK;
2700 			break;
2701 		case KLOCKF_TYPE_PID:
2702 			a->type = PS_ADVLOCK_TYPE_PID;
2703 			break;
2704 		case KLOCKF_TYPE_REMOTE:
2705 			a->type = PS_ADVLOCK_TYPE_REMOTE;
2706 			break;
2707 		default:
2708 			warn("ABI broken");
2709 			free(a);
2710 			goto fail;
2711 		}
2712 		a->pid = kl->kl_pid;
2713 		a->sysid = kl->kl_sysid;
2714 		a->file_fsid = kl->kl_file_fsid;
2715 		a->file_rdev = kl->kl_file_rdev;
2716 		a->file_fileid = kl->kl_file_fileid;
2717 		a->start = kl->kl_start;
2718 		a->len = kl->kl_len;
2719 		if (kl->kl_path[0] != '\0') {
2720 			a->path = strdup(kl->kl_path);
2721 			if (a->path == NULL) {
2722 				warn("malloc");
2723 				free(a);
2724 				goto fail;
2725 			}
2726 		} else
2727 			a->path = NULL;
2728 		STAILQ_INSERT_TAIL(res, a, next);
2729 	}
2730 
2731 	free(buf);
2732 	return (res);
2733 
2734 fail:
2735 	free(buf);
2736 	procstat_freeadvlock(procstat, res);
2737 	return (NULL);
2738 }
2739 
2740 struct advlock_list *
procstat_getadvlock(struct procstat * procstat)2741 procstat_getadvlock(struct procstat *procstat)
2742 {
2743 	switch (procstat->type) {
2744 	case PROCSTAT_KVM:
2745 		warnx("kvm method is not supported");
2746 		return (NULL);
2747 	case PROCSTAT_SYSCTL:
2748 		return (procstat_getadvlock_sysctl(procstat));
2749 	case PROCSTAT_CORE:
2750 		warnx("core method is not supported");
2751 		return (NULL);
2752 	default:
2753 		warnx("unknown access method: %d", procstat->type);
2754 		return (NULL);
2755 	}
2756 }
2757 
2758 void
procstat_freeadvlock(struct procstat * procstat __unused,struct advlock_list * lst)2759 procstat_freeadvlock(struct procstat *procstat __unused,
2760     struct advlock_list *lst)
2761 {
2762 	struct advlock *a, *a1;
2763 
2764 	STAILQ_FOREACH_SAFE(a, lst, next, a1) {
2765 		free(__DECONST(char *, a->path));
2766 		free(a);
2767 	}
2768 	free(lst);
2769 }
2770 
2771 static rlim_t *
procstat_getrlimitusage_sysctl(pid_t pid,unsigned * cntp)2772 procstat_getrlimitusage_sysctl(pid_t pid, unsigned *cntp)
2773 {
2774 	int error, name[4];
2775 	rlim_t *val;
2776 	size_t len;
2777 
2778 	name[0] = CTL_KERN;
2779 	name[1] = KERN_PROC;
2780 	name[2] = KERN_PROC_RLIMIT_USAGE;
2781 	name[3] = pid;
2782 
2783 	len = 0;
2784 	error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
2785 	if (error == -1)
2786 		return (NULL);
2787 	val = malloc(len);
2788 	if (val == NULL)
2789 		return (NULL);
2790 
2791 	error = sysctl(name, nitems(name), val, &len, NULL, 0);
2792 	if (error == -1) {
2793 		free(val);
2794 		return (NULL);
2795 	}
2796 	*cntp = len / sizeof(rlim_t);
2797 	return (val);
2798 }
2799 
2800 rlim_t *
procstat_getrlimitusage(struct procstat * procstat,struct kinfo_proc * kp,unsigned int * cntp)2801 procstat_getrlimitusage(struct procstat *procstat, struct kinfo_proc *kp,
2802     unsigned int *cntp)
2803 {
2804 	switch (procstat->type) {
2805 	case PROCSTAT_KVM:
2806 		warnx("kvm method is not supported");
2807 		return (NULL);
2808 	case PROCSTAT_SYSCTL:
2809 		return (procstat_getrlimitusage_sysctl(kp->ki_pid, cntp));
2810 	case PROCSTAT_CORE:
2811 		warnx("core method is not supported");
2812 		return (NULL);
2813 	default:
2814 		warnx("unknown access method: %d", procstat->type);
2815 		return (NULL);
2816 	}
2817 }
2818 
2819 void
procstat_freerlimitusage(struct procstat * procstat __unused,rlim_t * resusage)2820 procstat_freerlimitusage(struct procstat *procstat __unused, rlim_t *resusage)
2821 {
2822 	free(resusage);
2823 }
2824 
2825 static struct kinfo_knote *
procstat_get_kqueue_info_sysctl(pid_t pid,int kqfd,unsigned int * cntp,char * errbuf)2826 procstat_get_kqueue_info_sysctl(pid_t pid, int kqfd, unsigned int *cntp,
2827     char *errbuf)
2828 {
2829 	int error, name[5];
2830 	struct kinfo_knote *val;
2831 	size_t len;
2832 
2833 	name[0] = CTL_KERN;
2834 	name[1] = KERN_PROC;
2835 	name[2] = KERN_PROC_KQUEUE;
2836 	name[3] = pid;
2837 	name[4] = kqfd;
2838 
2839 	len = 0;
2840 	error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
2841 	if (error == -1) {
2842 		snprintf(errbuf, _POSIX2_LINE_MAX,
2843 		    "KERN_PROC_KQUEUE.pid<%d>.kq<%d> (size q) failed: %s",
2844 		    pid, kqfd, strerror(errno));
2845 		return (NULL);
2846 	}
2847 	val = malloc(len);
2848 	if (val == NULL) {
2849 		snprintf(errbuf, _POSIX2_LINE_MAX, "no memory");
2850 		return (NULL);
2851 	}
2852 
2853 	error = sysctl(name, nitems(name), val, &len, NULL, 0);
2854 	if (error == -1) {
2855 		snprintf(errbuf, _POSIX2_LINE_MAX,
2856 		    "KERN_PROC_KQUEUE.pid<%d>.kq<%d> failed: %s",
2857 		    pid, kqfd, strerror(errno));
2858 		free(val);
2859 		return (NULL);
2860 	}
2861 	*cntp = len / sizeof(*val);
2862 	return (val);
2863 }
2864 
2865 struct kinfo_knote *
procstat_get_kqueue_info(struct procstat * procstat,struct kinfo_proc * kp,int kqfd,unsigned int * count,char * errbuf)2866 procstat_get_kqueue_info(struct procstat *procstat,
2867     struct kinfo_proc *kp, int kqfd, unsigned int *count, char *errbuf)
2868 {
2869 	struct kinfo_knote *kn, *k, *res, *rn;
2870 	size_t len, kqn;
2871 
2872 	switch (procstat->type) {
2873 	case PROCSTAT_KVM:
2874 		warnx("kvm method is not supported");
2875 		return (NULL);
2876 	case PROCSTAT_SYSCTL:
2877 		return (procstat_get_kqueue_info_sysctl(kp->ki_pid, kqfd,
2878 		    count, errbuf));
2879 	case PROCSTAT_CORE:
2880 		k = procstat_core_get(procstat->core, PSC_TYPE_KQUEUES,
2881 		    NULL, &len);
2882 		if (k == NULL) {
2883 			snprintf(errbuf, _POSIX2_LINE_MAX,
2884 			    "getting NT_PROCSTAT_KQUEUES note failed");
2885 			*count = 0;
2886 			return (NULL);
2887 		}
2888 		for (kqn = 0, kn = k; kn < k + len / sizeof(*kn); kn++) {
2889 			if (kn->knt_kq_fd == kqfd)
2890 				kqn++;
2891 		}
2892 		res = calloc(kqn, sizeof(*res));
2893 		if (res == NULL) {
2894 			free(k);
2895 			snprintf(errbuf, _POSIX2_LINE_MAX,
2896 			    "no memory");
2897 			return (NULL);
2898 		}
2899 		for (kn = k, rn = res; kn < k + len / sizeof(*kn); kn++) {
2900 			if (kn->knt_kq_fd != kqfd)
2901 				continue;
2902 			*rn = *kn;
2903 			rn++;
2904 		}
2905 		*count = kqn;
2906 		free(k);
2907 		return (res);
2908 	default:
2909 		warnx("unknown access method: %d", procstat->type);
2910 		return (NULL);
2911 	}
2912 }
2913 
2914 void
procstat_freekqinfo(struct procstat * procstat __unused,struct kinfo_knote * v)2915 procstat_freekqinfo(struct procstat *procstat __unused, struct kinfo_knote *v)
2916 {
2917 	free(v);
2918 }
2919