xref: /freebsd/lib/libprocstat/libprocstat.c (revision 79102e108099237b1d702e960706a739c7b071bd)
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 ((vnode.v_type == VBLK || vnode.v_type == VCHR) &&
1334 	    vnode.v_rdev != NULL){
1335 		vn->vn_dev = dev2udev(kd, vnode.v_rdev);
1336 		(void)kdevtoname(kd, vnode.v_rdev, vn->vn_devname);
1337 	} else {
1338 		vn->vn_dev = -1;
1339 	}
1340 	return (0);
1341 
1342 fail:
1343 	if (errbuf != NULL)
1344 		snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1345 	return (1);
1346 }
1347 
1348 /*
1349  * kinfo vnode type to filestat translation.
1350  */
1351 static int
kinfo_vtype2fst(int kfvtype)1352 kinfo_vtype2fst(int kfvtype)
1353 {
1354 	static struct {
1355 		int	kf_vtype;
1356 		int	fst_vtype;
1357 	} kfvtypes2fst[] = {
1358 		{ KF_VTYPE_VBAD, PS_FST_VTYPE_VBAD },
1359 		{ KF_VTYPE_VBLK, PS_FST_VTYPE_VBLK },
1360 		{ KF_VTYPE_VCHR, PS_FST_VTYPE_VCHR },
1361 		{ KF_VTYPE_VDIR, PS_FST_VTYPE_VDIR },
1362 		{ KF_VTYPE_VFIFO, PS_FST_VTYPE_VFIFO },
1363 		{ KF_VTYPE_VLNK, PS_FST_VTYPE_VLNK },
1364 		{ KF_VTYPE_VNON, PS_FST_VTYPE_VNON },
1365 		{ KF_VTYPE_VREG, PS_FST_VTYPE_VREG },
1366 		{ KF_VTYPE_VSOCK, PS_FST_VTYPE_VSOCK }
1367 	};
1368 #define	NKFVTYPES	(sizeof(kfvtypes2fst) / sizeof(*kfvtypes2fst))
1369 	unsigned int i;
1370 
1371 	for (i = 0; i < NKFVTYPES; i++)
1372 		if (kfvtypes2fst[i].kf_vtype == kfvtype)
1373 			break;
1374 	if (i == NKFVTYPES)
1375 		return (PS_FST_VTYPE_UNKNOWN);
1376 	return (kfvtypes2fst[i].fst_vtype);
1377 }
1378 
1379 static int
procstat_get_vnode_info_sysctl(struct filestat * fst,struct vnstat * vn,char * errbuf)1380 procstat_get_vnode_info_sysctl(struct filestat *fst, struct vnstat *vn,
1381     char *errbuf)
1382 {
1383 	struct statfs stbuf;
1384 	struct kinfo_file *kif;
1385 	struct kinfo_vmentry *kve;
1386 	char *name, *path;
1387 	uint64_t fileid;
1388 	uint64_t size;
1389 	uint64_t fsid;
1390 	uint64_t rdev;
1391 	uint16_t mode;
1392 	int vntype;
1393 	int status;
1394 
1395 	assert(fst);
1396 	assert(vn);
1397 	bzero(vn, sizeof(*vn));
1398 	if (fst->fs_typedep == NULL)
1399 		return (1);
1400 	if (fst->fs_uflags & PS_FST_UFLAG_MMAP) {
1401 		kve = fst->fs_typedep;
1402 		fileid = kve->kve_vn_fileid;
1403 		fsid = kve->kve_vn_fsid;
1404 		mode = kve->kve_vn_mode;
1405 		path = kve->kve_path;
1406 		rdev = kve->kve_vn_rdev;
1407 		size = kve->kve_vn_size;
1408 		vntype = kinfo_vtype2fst(kve->kve_vn_type);
1409 		status = kve->kve_status;
1410 	} else {
1411 		kif = fst->fs_typedep;
1412 		fileid = kif->kf_un.kf_file.kf_file_fileid;
1413 		fsid = kif->kf_un.kf_file.kf_file_fsid;
1414 		mode = kif->kf_un.kf_file.kf_file_mode;
1415 		path = kif->kf_path;
1416 		rdev = kif->kf_un.kf_file.kf_file_rdev;
1417 		size = kif->kf_un.kf_file.kf_file_size;
1418 		vntype = kinfo_vtype2fst(kif->kf_vnode_type);
1419 		status = kif->kf_status;
1420 	}
1421 	vn->vn_type = vntype;
1422 	if (vntype == PS_FST_VTYPE_VNON || vntype == PS_FST_VTYPE_VBAD)
1423 		return (0);
1424 	if ((status & KF_ATTR_VALID) == 0) {
1425 		if (errbuf != NULL) {
1426 			snprintf(errbuf, _POSIX2_LINE_MAX,
1427 			    "? (no info available)");
1428 		}
1429 		return (1);
1430 	}
1431 	if (path && *path) {
1432 		statfs(path, &stbuf);
1433 		vn->vn_mntdir = strdup(stbuf.f_mntonname);
1434 	} else
1435 		vn->vn_mntdir = strdup("-");
1436 	vn->vn_dev = rdev;
1437 	if (vntype == PS_FST_VTYPE_VBLK) {
1438 		name = devname(rdev, S_IFBLK);
1439 		if (name != NULL)
1440 			strlcpy(vn->vn_devname, name,
1441 			    sizeof(vn->vn_devname));
1442 	} else if (vntype == PS_FST_VTYPE_VCHR) {
1443 		name = devname(vn->vn_dev, S_IFCHR);
1444 		if (name != NULL)
1445 			strlcpy(vn->vn_devname, name,
1446 			    sizeof(vn->vn_devname));
1447 	}
1448 	vn->vn_fsid = fsid;
1449 	vn->vn_fileid = fileid;
1450 	vn->vn_size = size;
1451 	vn->vn_mode = mode;
1452 	return (0);
1453 }
1454 
1455 int
procstat_get_socket_info(struct procstat * procstat,struct filestat * fst,struct sockstat * sock,char * errbuf)1456 procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
1457     struct sockstat *sock, char *errbuf)
1458 {
1459 
1460 	assert(sock);
1461 	if (procstat->type == PROCSTAT_KVM) {
1462 		return (procstat_get_socket_info_kvm(procstat->kd, fst, sock,
1463 		    errbuf));
1464 	} else if (procstat->type == PROCSTAT_SYSCTL ||
1465 		procstat->type == PROCSTAT_CORE) {
1466 		return (procstat_get_socket_info_sysctl(fst, sock, errbuf));
1467 	} else {
1468 		warnx("unknown access method: %d", procstat->type);
1469 		if (errbuf != NULL)
1470 			snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1471 		return (1);
1472 	}
1473 }
1474 
1475 static int
procstat_get_socket_info_kvm(kvm_t * kd,struct filestat * fst,struct sockstat * sock,char * errbuf)1476 procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst,
1477     struct sockstat *sock, char *errbuf)
1478 {
1479 	struct domain dom;
1480 	struct protosw proto;
1481 	struct socket s;
1482 	struct unpcb unpcb;
1483 	ssize_t len;
1484 	void *so;
1485 
1486 	assert(kd);
1487 	assert(sock);
1488 	assert(fst);
1489 	bzero(sock, sizeof(*sock));
1490 	so = fst->fs_typedep;
1491 	if (so == NULL)
1492 		goto fail;
1493 	sock->so_addr = (uintptr_t)so;
1494 	/* fill in socket */
1495 	if (!kvm_read_all(kd, (unsigned long)so, &s,
1496 	    sizeof(struct socket))) {
1497 		warnx("can't read sock at %p", (void *)so);
1498 		goto fail;
1499 	}
1500 	/* fill in protosw entry */
1501 	if (!kvm_read_all(kd, (unsigned long)s.so_proto, &proto,
1502 	    sizeof(struct protosw))) {
1503 		warnx("can't read protosw at %p", (void *)s.so_proto);
1504 		goto fail;
1505 	}
1506 	/* fill in domain */
1507 	if (!kvm_read_all(kd, (unsigned long)proto.pr_domain, &dom,
1508 	    sizeof(struct domain))) {
1509 		warnx("can't read domain at %p",
1510 		    (void *)proto.pr_domain);
1511 		goto fail;
1512 	}
1513 	if ((len = kvm_read(kd, (unsigned long)dom.dom_name, sock->dname,
1514 	    sizeof(sock->dname) - 1)) < 0) {
1515 		warnx("can't read domain name at %p", (void *)dom.dom_name);
1516 		sock->dname[0] = '\0';
1517 	}
1518 	else
1519 		sock->dname[len] = '\0';
1520 
1521 	/*
1522 	 * Fill in known data.
1523 	 */
1524 	sock->type = s.so_type;
1525 	sock->proto = proto.pr_protocol;
1526 	sock->dom_family = dom.dom_family;
1527 	sock->so_pcb = (uintptr_t)s.so_pcb;
1528 	sock->sendq = s.so_snd.sb_ccc;
1529 	sock->recvq = s.so_rcv.sb_ccc;
1530 	sock->so_rcv_sb_state = s.so_rcv.sb_state;
1531 	sock->so_snd_sb_state = s.so_snd.sb_state;
1532 
1533 	/*
1534 	 * Protocol specific data.
1535 	 */
1536 	switch (dom.dom_family) {
1537 	case AF_UNIX:
1538 		if (s.so_pcb) {
1539 			if (kvm_read(kd, (u_long)s.so_pcb, (char *)&unpcb,
1540 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
1541 				warnx("can't read unpcb at %p",
1542 				    (void *)s.so_pcb);
1543 			} else if (unpcb.unp_conn) {
1544 				sock->unp_conn = (uintptr_t)unpcb.unp_conn;
1545 			}
1546 		}
1547 		break;
1548 	default:
1549 		break;
1550 	}
1551 	return (0);
1552 
1553 fail:
1554 	if (errbuf != NULL)
1555 		snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1556 	return (1);
1557 }
1558 
1559 static int
procstat_get_socket_info_sysctl(struct filestat * fst,struct sockstat * sock,char * errbuf __unused)1560 procstat_get_socket_info_sysctl(struct filestat *fst, struct sockstat *sock,
1561     char *errbuf __unused)
1562 {
1563 	struct kinfo_file *kif;
1564 
1565 	assert(sock);
1566 	assert(fst);
1567 	bzero(sock, sizeof(*sock));
1568 	kif = fst->fs_typedep;
1569 	if (kif == NULL)
1570 		return (0);
1571 
1572 	/*
1573 	 * Fill in known data.
1574 	 */
1575 	sock->type = kif->kf_sock_type;
1576 	sock->proto = kif->kf_sock_protocol;
1577 	sock->dom_family = kif->kf_sock_domain;
1578 	sock->so_pcb = kif->kf_un.kf_sock.kf_sock_pcb;
1579 	strlcpy(sock->dname, kif->kf_path, sizeof(sock->dname));
1580 	bcopy(&kif->kf_un.kf_sock.kf_sa_local, &sock->sa_local,
1581 	    kif->kf_un.kf_sock.kf_sa_local.ss_len);
1582 	bcopy(&kif->kf_un.kf_sock.kf_sa_peer, &sock->sa_peer,
1583 	    kif->kf_un.kf_sock.kf_sa_peer.ss_len);
1584 
1585 	/*
1586 	 * Protocol specific data.
1587 	 */
1588 	switch (sock->dom_family) {
1589 	case AF_INET:
1590 	case AF_INET6:
1591 		if (sock->proto == IPPROTO_TCP) {
1592 			sock->sendq = kif->kf_un.kf_sock.kf_sock_sendq;
1593 			sock->recvq = kif->kf_un.kf_sock.kf_sock_recvq;
1594 		}
1595 		break;
1596 	case AF_UNIX:
1597 		if (kif->kf_un.kf_sock.kf_sock_unpconn != 0) {
1598 			sock->so_rcv_sb_state =
1599 			    kif->kf_un.kf_sock.kf_sock_rcv_sb_state;
1600 			sock->so_snd_sb_state =
1601 			    kif->kf_un.kf_sock.kf_sock_snd_sb_state;
1602 			sock->unp_conn =
1603 			    kif->kf_un.kf_sock.kf_sock_unpconn;
1604 			sock->sendq = kif->kf_un.kf_sock.kf_sock_sendq;
1605 			sock->recvq = kif->kf_un.kf_sock.kf_sock_recvq;
1606 		}
1607 		break;
1608 	default:
1609 		break;
1610 	}
1611 	return (0);
1612 }
1613 
1614 /*
1615  * Descriptor flags to filestat translation.
1616  */
1617 static int
to_filestat_flags(int flags)1618 to_filestat_flags(int flags)
1619 {
1620 	static struct {
1621 		int flag;
1622 		int fst_flag;
1623 	} fstflags[] = {
1624 		{ FREAD, PS_FST_FFLAG_READ },
1625 		{ FWRITE, PS_FST_FFLAG_WRITE },
1626 		{ O_APPEND, PS_FST_FFLAG_APPEND },
1627 		{ O_ASYNC, PS_FST_FFLAG_ASYNC },
1628 		{ O_CREAT, PS_FST_FFLAG_CREAT },
1629 		{ O_DIRECT, PS_FST_FFLAG_DIRECT },
1630 		{ O_EXCL, PS_FST_FFLAG_EXCL },
1631 		{ O_EXEC, PS_FST_FFLAG_EXEC },
1632 		{ O_EXLOCK, PS_FST_FFLAG_EXLOCK },
1633 		{ O_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW },
1634 		{ O_NONBLOCK, PS_FST_FFLAG_NONBLOCK },
1635 		{ O_SHLOCK, PS_FST_FFLAG_SHLOCK },
1636 		{ O_SYNC, PS_FST_FFLAG_SYNC },
1637 		{ O_TRUNC, PS_FST_FFLAG_TRUNC }
1638 	};
1639 #define NFSTFLAGS	(sizeof(fstflags) / sizeof(*fstflags))
1640 	int fst_flags;
1641 	unsigned int i;
1642 
1643 	fst_flags = 0;
1644 	for (i = 0; i < NFSTFLAGS; i++)
1645 		if (flags & fstflags[i].flag)
1646 			fst_flags |= fstflags[i].fst_flag;
1647 	return (fst_flags);
1648 }
1649 
1650 /*
1651  * Vnode type to filestate translation.
1652  */
1653 static int
vntype2psfsttype(int type)1654 vntype2psfsttype(int type)
1655 {
1656 	static struct {
1657 		int	vtype;
1658 		int	fst_vtype;
1659 	} vt2fst[] = {
1660 		{ VBAD, PS_FST_VTYPE_VBAD },
1661 		{ VBLK, PS_FST_VTYPE_VBLK },
1662 		{ VCHR, PS_FST_VTYPE_VCHR },
1663 		{ VDIR, PS_FST_VTYPE_VDIR },
1664 		{ VFIFO, PS_FST_VTYPE_VFIFO },
1665 		{ VLNK, PS_FST_VTYPE_VLNK },
1666 		{ VNON, PS_FST_VTYPE_VNON },
1667 		{ VREG, PS_FST_VTYPE_VREG },
1668 		{ VSOCK, PS_FST_VTYPE_VSOCK }
1669 	};
1670 #define	NVFTYPES	(sizeof(vt2fst) / sizeof(*vt2fst))
1671 	unsigned int i, fst_type;
1672 
1673 	fst_type = PS_FST_VTYPE_UNKNOWN;
1674 	for (i = 0; i < NVFTYPES; i++) {
1675 		if (type == vt2fst[i].vtype) {
1676 			fst_type = vt2fst[i].fst_vtype;
1677 			break;
1678 		}
1679 	}
1680 	return (fst_type);
1681 }
1682 
1683 static char *
getmnton(kvm_t * kd,struct mount * m)1684 getmnton(kvm_t *kd, struct mount *m)
1685 {
1686 	struct mount mnt;
1687 	static struct mtab {
1688 		struct mtab *next;
1689 		struct mount *m;
1690 		char mntonname[MNAMELEN + 1];
1691 	} *mhead = NULL;
1692 	struct mtab *mt;
1693 
1694 	for (mt = mhead; mt != NULL; mt = mt->next)
1695 		if (m == mt->m)
1696 			return (mt->mntonname);
1697 	if (!kvm_read_all(kd, (unsigned long)m, &mnt, sizeof(struct mount))) {
1698 		warnx("can't read mount table at %p", (void *)m);
1699 		return (NULL);
1700 	}
1701 	if ((mt = malloc(sizeof (struct mtab))) == NULL)
1702 		err(1, NULL);
1703 	mt->m = m;
1704 	bcopy(&mnt.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
1705 	mt->mntonname[MNAMELEN] = '\0';
1706 	mt->next = mhead;
1707 	mhead = mt;
1708 	return (mt->mntonname);
1709 }
1710 
1711 /*
1712  * Auxiliary structures and functions to get process environment or
1713  * command line arguments.
1714  */
1715 struct argvec {
1716 	char	*buf;
1717 	size_t	bufsize;
1718 	char	**argv;
1719 	size_t	argc;
1720 };
1721 
1722 static struct argvec *
argvec_alloc(size_t bufsize)1723 argvec_alloc(size_t bufsize)
1724 {
1725 	struct argvec *av;
1726 
1727 	av = malloc(sizeof(*av));
1728 	if (av == NULL)
1729 		return (NULL);
1730 	av->bufsize = bufsize;
1731 	av->buf = malloc(av->bufsize);
1732 	if (av->buf == NULL) {
1733 		free(av);
1734 		return (NULL);
1735 	}
1736 	av->argc = 32;
1737 	av->argv = malloc(sizeof(char *) * av->argc);
1738 	if (av->argv == NULL) {
1739 		free(av->buf);
1740 		free(av);
1741 		return (NULL);
1742 	}
1743 	return av;
1744 }
1745 
1746 static void
argvec_free(struct argvec * av)1747 argvec_free(struct argvec * av)
1748 {
1749 
1750 	free(av->argv);
1751 	free(av->buf);
1752 	free(av);
1753 }
1754 
1755 static char **
getargv(struct procstat * procstat,struct kinfo_proc * kp,size_t nchr,int env)1756 getargv(struct procstat *procstat, struct kinfo_proc *kp, size_t nchr, int env)
1757 {
1758 	int error, name[4], argc, i;
1759 	struct argvec *av, **avp;
1760 	enum psc_type type;
1761 	size_t len;
1762 	char *p, **argv;
1763 
1764 	assert(procstat);
1765 	assert(kp);
1766 	if (procstat->type == PROCSTAT_KVM) {
1767 		warnx("can't use kvm access method");
1768 		return (NULL);
1769 	}
1770 	if (procstat->type != PROCSTAT_SYSCTL &&
1771 	    procstat->type != PROCSTAT_CORE) {
1772 		warnx("unknown access method: %d", procstat->type);
1773 		return (NULL);
1774 	}
1775 
1776 	if (nchr == 0 || nchr > ARG_MAX)
1777 		nchr = ARG_MAX;
1778 
1779 	avp = (struct argvec **)(env ? &procstat->argv : &procstat->envv);
1780 	av = *avp;
1781 
1782 	if (av == NULL)
1783 	{
1784 		av = argvec_alloc(nchr);
1785 		if (av == NULL)
1786 		{
1787 			warn("malloc(%zu)", nchr);
1788 			return (NULL);
1789 		}
1790 		*avp = av;
1791 	} else if (av->bufsize < nchr) {
1792 		av->buf = reallocf(av->buf, nchr);
1793 		if (av->buf == NULL) {
1794 			warn("malloc(%zu)", nchr);
1795 			return (NULL);
1796 		}
1797 	}
1798 	if (procstat->type == PROCSTAT_SYSCTL) {
1799 		name[0] = CTL_KERN;
1800 		name[1] = KERN_PROC;
1801 		name[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS;
1802 		name[3] = kp->ki_pid;
1803 		len = nchr;
1804 		error = sysctl(name, nitems(name), av->buf, &len, NULL, 0);
1805 		if (error != 0 && errno != ESRCH && errno != EPERM)
1806 			warn("sysctl(kern.proc.%s)", env ? "env" : "args");
1807 		if (error != 0 || len == 0)
1808 			return (NULL);
1809 	} else /* procstat->type == PROCSTAT_CORE */ {
1810 		type = env ? PSC_TYPE_ENVV : PSC_TYPE_ARGV;
1811 		len = nchr;
1812 		if (procstat_core_get(procstat->core, type, av->buf, &len)
1813 		    == NULL) {
1814 			return (NULL);
1815 		}
1816 	}
1817 
1818 	argv = av->argv;
1819 	argc = av->argc;
1820 	i = 0;
1821 	for (p = av->buf; p < av->buf + len; p += strlen(p) + 1) {
1822 		argv[i++] = p;
1823 		if (i < argc)
1824 			continue;
1825 		/* Grow argv. */
1826 		argc += argc;
1827 		argv = realloc(argv, sizeof(char *) * argc);
1828 		if (argv == NULL) {
1829 			warn("malloc(%zu)", sizeof(char *) * argc);
1830 			return (NULL);
1831 		}
1832 		av->argv = argv;
1833 		av->argc = argc;
1834 	}
1835 	argv[i] = NULL;
1836 
1837 	return (argv);
1838 }
1839 
1840 /*
1841  * Return process command line arguments.
1842  */
1843 char **
procstat_getargv(struct procstat * procstat,struct kinfo_proc * p,size_t nchr)1844 procstat_getargv(struct procstat *procstat, struct kinfo_proc *p, size_t nchr)
1845 {
1846 
1847 	return (getargv(procstat, p, nchr, 0));
1848 }
1849 
1850 /*
1851  * Free the buffer allocated by procstat_getargv().
1852  */
1853 void
procstat_freeargv(struct procstat * procstat)1854 procstat_freeargv(struct procstat *procstat)
1855 {
1856 
1857 	if (procstat->argv != NULL) {
1858 		argvec_free(procstat->argv);
1859 		procstat->argv = NULL;
1860 	}
1861 }
1862 
1863 /*
1864  * Return process environment.
1865  */
1866 char **
procstat_getenvv(struct procstat * procstat,struct kinfo_proc * p,size_t nchr)1867 procstat_getenvv(struct procstat *procstat, struct kinfo_proc *p, size_t nchr)
1868 {
1869 
1870 	return (getargv(procstat, p, nchr, 1));
1871 }
1872 
1873 /*
1874  * Free the buffer allocated by procstat_getenvv().
1875  */
1876 void
procstat_freeenvv(struct procstat * procstat)1877 procstat_freeenvv(struct procstat *procstat)
1878 {
1879 	if (procstat->envv != NULL) {
1880 		argvec_free(procstat->envv);
1881 		procstat->envv = NULL;
1882 	}
1883 }
1884 
1885 static struct kinfo_vmentry *
kinfo_getvmmap_core(struct procstat_core * core,int * cntp)1886 kinfo_getvmmap_core(struct procstat_core *core, int *cntp)
1887 {
1888 	int cnt;
1889 	size_t len;
1890 	char *buf, *bp, *eb;
1891 	struct kinfo_vmentry *kiv, *kp, *kv;
1892 
1893 	buf = procstat_core_get(core, PSC_TYPE_VMMAP, NULL, &len);
1894 	if (buf == NULL)
1895 		return (NULL);
1896 
1897 	/*
1898 	 * XXXMG: The code below is just copy&past from libutil.
1899 	 * The code duplication can be avoided if libutil
1900 	 * is extended to provide something like:
1901 	 *   struct kinfo_vmentry *kinfo_getvmmap_from_buf(const char *buf,
1902 	 *       size_t len, int *cntp);
1903 	 */
1904 
1905 	/* Pass 1: count items */
1906 	cnt = 0;
1907 	bp = buf;
1908 	eb = buf + len;
1909 	while (bp < eb) {
1910 		kv = (struct kinfo_vmentry *)(uintptr_t)bp;
1911 		if (kv->kve_structsize == 0)
1912 			break;
1913 		bp += kv->kve_structsize;
1914 		cnt++;
1915 	}
1916 
1917 	kiv = calloc(cnt, sizeof(*kiv));
1918 	if (kiv == NULL) {
1919 		free(buf);
1920 		return (NULL);
1921 	}
1922 	bp = buf;
1923 	eb = buf + len;
1924 	kp = kiv;
1925 	/* Pass 2: unpack */
1926 	while (bp < eb) {
1927 		kv = (struct kinfo_vmentry *)(uintptr_t)bp;
1928 		if (kv->kve_structsize == 0)
1929 			break;
1930 		/* Copy/expand into pre-zeroed buffer */
1931 		memcpy(kp, kv, kv->kve_structsize);
1932 		/* Advance to next packed record */
1933 		bp += kv->kve_structsize;
1934 		/* Set field size to fixed length, advance */
1935 		kp->kve_structsize = sizeof(*kp);
1936 		kp++;
1937 	}
1938 	free(buf);
1939 	*cntp = cnt;
1940 	return (kiv);	/* Caller must free() return value */
1941 }
1942 
1943 struct kinfo_vmentry *
procstat_getvmmap(struct procstat * procstat,struct kinfo_proc * kp,unsigned int * cntp)1944 procstat_getvmmap(struct procstat *procstat, struct kinfo_proc *kp,
1945     unsigned int *cntp)
1946 {
1947 
1948 	switch (procstat->type) {
1949 	case PROCSTAT_KVM:
1950 		warnx("kvm method is not supported");
1951 		return (NULL);
1952 	case PROCSTAT_SYSCTL:
1953 		return (kinfo_getvmmap(kp->ki_pid, cntp));
1954 	case PROCSTAT_CORE:
1955 		return (kinfo_getvmmap_core(procstat->core, cntp));
1956 	default:
1957 		warnx("unknown access method: %d", procstat->type);
1958 		return (NULL);
1959 	}
1960 }
1961 
1962 void
procstat_freevmmap(struct procstat * procstat __unused,struct kinfo_vmentry * vmmap)1963 procstat_freevmmap(struct procstat *procstat __unused,
1964     struct kinfo_vmentry *vmmap)
1965 {
1966 
1967 	free(vmmap);
1968 }
1969 
1970 static gid_t *
procstat_getgroups_kvm(kvm_t * kd,struct kinfo_proc * kp,unsigned int * cntp)1971 procstat_getgroups_kvm(kvm_t *kd, struct kinfo_proc *kp, unsigned int *cntp)
1972 {
1973 	struct proc proc;
1974 	struct ucred ucred;
1975 	gid_t *groups;
1976 	size_t len;
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 	len = ucred.cr_ngroups * sizeof(gid_t);
1995 	groups = malloc(len);
1996 	if (groups == NULL) {
1997 		warn("malloc(%zu)", len);
1998 		return (NULL);
1999 	}
2000 	if (!kvm_read_all(kd, (unsigned long)ucred.cr_groups, groups, len)) {
2001 		warnx("can't read groups at %p for pid %d",
2002 		    ucred.cr_groups, kp->ki_pid);
2003 		free(groups);
2004 		return (NULL);
2005 	}
2006 	*cntp = ucred.cr_ngroups;
2007 	return (groups);
2008 }
2009 
2010 static gid_t *
procstat_getgroups_sysctl(pid_t pid,unsigned int * cntp)2011 procstat_getgroups_sysctl(pid_t pid, unsigned int *cntp)
2012 {
2013 	int mib[4];
2014 	size_t len;
2015 	gid_t *groups;
2016 
2017 	mib[0] = CTL_KERN;
2018 	mib[1] = KERN_PROC;
2019 	mib[2] = KERN_PROC_GROUPS;
2020 	mib[3] = pid;
2021 	len = (sysconf(_SC_NGROUPS_MAX) + 1) * sizeof(gid_t);
2022 	groups = malloc(len);
2023 	if (groups == NULL) {
2024 		warn("malloc(%zu)", len);
2025 		return (NULL);
2026 	}
2027 	if (sysctl(mib, nitems(mib), groups, &len, NULL, 0) == -1) {
2028 		warn("sysctl: kern.proc.groups: %d", pid);
2029 		free(groups);
2030 		return (NULL);
2031 	}
2032 	*cntp = len / sizeof(gid_t);
2033 	return (groups);
2034 }
2035 
2036 static gid_t *
procstat_getgroups_core(struct procstat_core * core,unsigned int * cntp)2037 procstat_getgroups_core(struct procstat_core *core, unsigned int *cntp)
2038 {
2039 	size_t len;
2040 	gid_t *groups;
2041 
2042 	groups = procstat_core_get(core, PSC_TYPE_GROUPS, NULL, &len);
2043 	if (groups == NULL)
2044 		return (NULL);
2045 	*cntp = len / sizeof(gid_t);
2046 	return (groups);
2047 }
2048 
2049 gid_t *
procstat_getgroups(struct procstat * procstat,struct kinfo_proc * kp,unsigned int * cntp)2050 procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
2051     unsigned int *cntp)
2052 {
2053 	switch (procstat->type) {
2054 	case PROCSTAT_KVM:
2055 		return (procstat_getgroups_kvm(procstat->kd, kp, cntp));
2056 	case PROCSTAT_SYSCTL:
2057 		return (procstat_getgroups_sysctl(kp->ki_pid, cntp));
2058 	case PROCSTAT_CORE:
2059 		return (procstat_getgroups_core(procstat->core, cntp));
2060 	default:
2061 		warnx("unknown access method: %d", procstat->type);
2062 		return (NULL);
2063 	}
2064 }
2065 
2066 void
procstat_freegroups(struct procstat * procstat __unused,gid_t * groups)2067 procstat_freegroups(struct procstat *procstat __unused, gid_t *groups)
2068 {
2069 
2070 	free(groups);
2071 }
2072 
2073 static int
procstat_getumask_kvm(kvm_t * kd,struct kinfo_proc * kp,unsigned short * maskp)2074 procstat_getumask_kvm(kvm_t *kd, struct kinfo_proc *kp, unsigned short *maskp)
2075 {
2076 	struct pwddesc pd;
2077 
2078 	assert(kd != NULL);
2079 	assert(kp != NULL);
2080 	if (kp->ki_pd == NULL)
2081 		return (-1);
2082 	if (!kvm_read_all(kd, (unsigned long)kp->ki_pd, &pd, sizeof(pd))) {
2083 		warnx("can't read pwddesc at %p for pid %d", kp->ki_pd,
2084 		    kp->ki_pid);
2085 		return (-1);
2086 	}
2087 	*maskp = pd.pd_cmask;
2088 	return (0);
2089 }
2090 
2091 static int
procstat_getumask_sysctl(pid_t pid,unsigned short * maskp)2092 procstat_getumask_sysctl(pid_t pid, unsigned short *maskp)
2093 {
2094 	int error;
2095 	int mib[4];
2096 	size_t len;
2097 
2098 	mib[0] = CTL_KERN;
2099 	mib[1] = KERN_PROC;
2100 	mib[2] = KERN_PROC_UMASK;
2101 	mib[3] = pid;
2102 	len = sizeof(*maskp);
2103 	error = sysctl(mib, nitems(mib), maskp, &len, NULL, 0);
2104 	if (error != 0 && errno != ESRCH && errno != EPERM)
2105 		warn("sysctl: kern.proc.umask: %d", pid);
2106 	return (error);
2107 }
2108 
2109 static int
procstat_getumask_core(struct procstat_core * core,unsigned short * maskp)2110 procstat_getumask_core(struct procstat_core *core, unsigned short *maskp)
2111 {
2112 	size_t len;
2113 	unsigned short *buf;
2114 
2115 	buf = procstat_core_get(core, PSC_TYPE_UMASK, NULL, &len);
2116 	if (buf == NULL)
2117 		return (-1);
2118 	if (len < sizeof(*maskp)) {
2119 		free(buf);
2120 		return (-1);
2121 	}
2122 	*maskp = *buf;
2123 	free(buf);
2124 	return (0);
2125 }
2126 
2127 int
procstat_getumask(struct procstat * procstat,struct kinfo_proc * kp,unsigned short * maskp)2128 procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
2129     unsigned short *maskp)
2130 {
2131 	switch (procstat->type) {
2132 	case PROCSTAT_KVM:
2133 		return (procstat_getumask_kvm(procstat->kd, kp, maskp));
2134 	case PROCSTAT_SYSCTL:
2135 		return (procstat_getumask_sysctl(kp->ki_pid, maskp));
2136 	case PROCSTAT_CORE:
2137 		return (procstat_getumask_core(procstat->core, maskp));
2138 	default:
2139 		warnx("unknown access method: %d", procstat->type);
2140 		return (-1);
2141 	}
2142 }
2143 
2144 static int
procstat_getrlimit_kvm(kvm_t * kd,struct kinfo_proc * kp,int which,struct rlimit * rlimit)2145 procstat_getrlimit_kvm(kvm_t *kd, struct kinfo_proc *kp, int which,
2146     struct rlimit* rlimit)
2147 {
2148 	struct proc proc;
2149 	unsigned long offset;
2150 
2151 	assert(kd != NULL);
2152 	assert(kp != NULL);
2153 	assert(which >= 0 && which < RLIM_NLIMITS);
2154 	if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
2155 	    sizeof(proc))) {
2156 		warnx("can't read proc struct at %p for pid %d",
2157 		    kp->ki_paddr, kp->ki_pid);
2158 		return (-1);
2159 	}
2160 	if (proc.p_limit == NULL)
2161 		return (-1);
2162 	offset = (unsigned long)proc.p_limit + sizeof(struct rlimit) * which;
2163 	if (!kvm_read_all(kd, offset, rlimit, sizeof(*rlimit))) {
2164 		warnx("can't read rlimit struct at %p for pid %d",
2165 		    (void *)offset, kp->ki_pid);
2166 		return (-1);
2167 	}
2168 	return (0);
2169 }
2170 
2171 static int
procstat_getrlimit_sysctl(pid_t pid,int which,struct rlimit * rlimit)2172 procstat_getrlimit_sysctl(pid_t pid, int which, struct rlimit* rlimit)
2173 {
2174 	int error, name[5];
2175 	size_t len;
2176 
2177 	name[0] = CTL_KERN;
2178 	name[1] = KERN_PROC;
2179 	name[2] = KERN_PROC_RLIMIT;
2180 	name[3] = pid;
2181 	name[4] = which;
2182 	len = sizeof(struct rlimit);
2183 	error = sysctl(name, nitems(name), rlimit, &len, NULL, 0);
2184 	if (error < 0 && errno != ESRCH) {
2185 		warn("sysctl: kern.proc.rlimit: %d", pid);
2186 		return (-1);
2187 	}
2188 	if (error < 0 || len != sizeof(struct rlimit))
2189 		return (-1);
2190 	return (0);
2191 }
2192 
2193 static int
procstat_getrlimit_core(struct procstat_core * core,int which,struct rlimit * rlimit)2194 procstat_getrlimit_core(struct procstat_core *core, int which,
2195     struct rlimit* rlimit)
2196 {
2197 	size_t len;
2198 	struct rlimit* rlimits;
2199 
2200 	if (which < 0 || which >= RLIM_NLIMITS) {
2201 		errno = EINVAL;
2202 		warn("getrlimit: which");
2203 		return (-1);
2204 	}
2205 	rlimits = procstat_core_get(core, PSC_TYPE_RLIMIT, NULL, &len);
2206 	if (rlimits == NULL)
2207 		return (-1);
2208 	if (len < sizeof(struct rlimit) * RLIM_NLIMITS) {
2209 		free(rlimits);
2210 		return (-1);
2211 	}
2212 	*rlimit = rlimits[which];
2213 	free(rlimits);
2214 	return (0);
2215 }
2216 
2217 int
procstat_getrlimit(struct procstat * procstat,struct kinfo_proc * kp,int which,struct rlimit * rlimit)2218 procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp, int which,
2219     struct rlimit* rlimit)
2220 {
2221 	switch (procstat->type) {
2222 	case PROCSTAT_KVM:
2223 		return (procstat_getrlimit_kvm(procstat->kd, kp, which,
2224 		    rlimit));
2225 	case PROCSTAT_SYSCTL:
2226 		return (procstat_getrlimit_sysctl(kp->ki_pid, which, rlimit));
2227 	case PROCSTAT_CORE:
2228 		return (procstat_getrlimit_core(procstat->core, which, rlimit));
2229 	default:
2230 		warnx("unknown access method: %d", procstat->type);
2231 		return (-1);
2232 	}
2233 }
2234 
2235 static int
procstat_getpathname_sysctl(pid_t pid,char * pathname,size_t maxlen)2236 procstat_getpathname_sysctl(pid_t pid, char *pathname, size_t maxlen)
2237 {
2238 	int error, name[4];
2239 	size_t len;
2240 
2241 	name[0] = CTL_KERN;
2242 	name[1] = KERN_PROC;
2243 	name[2] = KERN_PROC_PATHNAME;
2244 	name[3] = pid;
2245 	len = maxlen;
2246 	error = sysctl(name, nitems(name), pathname, &len, NULL, 0);
2247 	if (error != 0 && errno != ESRCH)
2248 		warn("sysctl: kern.proc.pathname: %d", pid);
2249 	if (len == 0)
2250 		pathname[0] = '\0';
2251 	return (error);
2252 }
2253 
2254 static int
procstat_getpathname_core(struct procstat_core * core,char * pathname,size_t maxlen)2255 procstat_getpathname_core(struct procstat_core *core, char *pathname,
2256     size_t maxlen)
2257 {
2258 	struct kinfo_file *files;
2259 	int cnt, i, result;
2260 
2261 	files = kinfo_getfile_core(core, &cnt);
2262 	if (files == NULL)
2263 		return (-1);
2264 	result = -1;
2265 	for (i = 0; i < cnt; i++) {
2266 		if (files[i].kf_fd != KF_FD_TYPE_TEXT)
2267 			continue;
2268 		strncpy(pathname, files[i].kf_path, maxlen);
2269 		result = 0;
2270 		break;
2271 	}
2272 	free(files);
2273 	return (result);
2274 }
2275 
2276 int
procstat_getpathname(struct procstat * procstat,struct kinfo_proc * kp,char * pathname,size_t maxlen)2277 procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
2278     char *pathname, size_t maxlen)
2279 {
2280 	switch (procstat->type) {
2281 	case PROCSTAT_KVM:
2282 		/* XXX: Return empty string. */
2283 		if (maxlen > 0)
2284 			pathname[0] = '\0';
2285 		return (0);
2286 	case PROCSTAT_SYSCTL:
2287 		return (procstat_getpathname_sysctl(kp->ki_pid, pathname,
2288 		    maxlen));
2289 	case PROCSTAT_CORE:
2290 		return (procstat_getpathname_core(procstat->core, pathname,
2291 		    maxlen));
2292 	default:
2293 		warnx("unknown access method: %d", procstat->type);
2294 		return (-1);
2295 	}
2296 }
2297 
2298 static int
procstat_getosrel_kvm(kvm_t * kd,struct kinfo_proc * kp,int * osrelp)2299 procstat_getosrel_kvm(kvm_t *kd, struct kinfo_proc *kp, int *osrelp)
2300 {
2301 	struct proc proc;
2302 
2303 	assert(kd != NULL);
2304 	assert(kp != NULL);
2305 	if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
2306 	    sizeof(proc))) {
2307 		warnx("can't read proc struct at %p for pid %d",
2308 		    kp->ki_paddr, kp->ki_pid);
2309 		return (-1);
2310 	}
2311 	*osrelp = proc.p_osrel;
2312 	return (0);
2313 }
2314 
2315 static int
procstat_getosrel_sysctl(pid_t pid,int * osrelp)2316 procstat_getosrel_sysctl(pid_t pid, int *osrelp)
2317 {
2318 	int error, name[4];
2319 	size_t len;
2320 
2321 	name[0] = CTL_KERN;
2322 	name[1] = KERN_PROC;
2323 	name[2] = KERN_PROC_OSREL;
2324 	name[3] = pid;
2325 	len = sizeof(*osrelp);
2326 	error = sysctl(name, nitems(name), osrelp, &len, NULL, 0);
2327 	if (error != 0 && errno != ESRCH)
2328 		warn("sysctl: kern.proc.osrel: %d", pid);
2329 	return (error);
2330 }
2331 
2332 static int
procstat_getosrel_core(struct procstat_core * core,int * osrelp)2333 procstat_getosrel_core(struct procstat_core *core, int *osrelp)
2334 {
2335 	size_t len;
2336 	int *buf;
2337 
2338 	buf = procstat_core_get(core, PSC_TYPE_OSREL, NULL, &len);
2339 	if (buf == NULL)
2340 		return (-1);
2341 	if (len < sizeof(*osrelp)) {
2342 		free(buf);
2343 		return (-1);
2344 	}
2345 	*osrelp = *buf;
2346 	free(buf);
2347 	return (0);
2348 }
2349 
2350 int
procstat_getosrel(struct procstat * procstat,struct kinfo_proc * kp,int * osrelp)2351 procstat_getosrel(struct procstat *procstat, struct kinfo_proc *kp, int *osrelp)
2352 {
2353 	switch (procstat->type) {
2354 	case PROCSTAT_KVM:
2355 		return (procstat_getosrel_kvm(procstat->kd, kp, osrelp));
2356 	case PROCSTAT_SYSCTL:
2357 		return (procstat_getosrel_sysctl(kp->ki_pid, osrelp));
2358 	case PROCSTAT_CORE:
2359 		return (procstat_getosrel_core(procstat->core, osrelp));
2360 	default:
2361 		warnx("unknown access method: %d", procstat->type);
2362 		return (-1);
2363 	}
2364 }
2365 
2366 #define PROC_AUXV_MAX	256
2367 
2368 #ifdef PS_ARCH_HAS_FREEBSD32
2369 static const char *elf32_sv_names[] = {
2370 	"Linux ELF32",
2371 	"FreeBSD ELF32",
2372 };
2373 
2374 static int
is_elf32_sysctl(pid_t pid)2375 is_elf32_sysctl(pid_t pid)
2376 {
2377 	int error, name[4];
2378 	size_t len, i;
2379 	char sv_name[32];
2380 
2381 	name[0] = CTL_KERN;
2382 	name[1] = KERN_PROC;
2383 	name[2] = KERN_PROC_SV_NAME;
2384 	name[3] = pid;
2385 	len = sizeof(sv_name);
2386 	error = sysctl(name, nitems(name), sv_name, &len, NULL, 0);
2387 	if (error != 0 || len == 0)
2388 		return (0);
2389 	for (i = 0; i < sizeof(elf32_sv_names) / sizeof(*elf32_sv_names); i++) {
2390 		if (strncmp(sv_name, elf32_sv_names[i], sizeof(sv_name)) == 0)
2391 			return (1);
2392 	}
2393 	return (0);
2394 }
2395 
2396 static Elf_Auxinfo *
procstat_getauxv32_sysctl(pid_t pid,unsigned int * cntp)2397 procstat_getauxv32_sysctl(pid_t pid, unsigned int *cntp)
2398 {
2399 	Elf_Auxinfo *auxv;
2400 	Elf32_Auxinfo *auxv32;
2401 	size_t len;
2402 	unsigned int i, count;
2403 	int name[4];
2404 
2405 	name[0] = CTL_KERN;
2406 	name[1] = KERN_PROC;
2407 	name[2] = KERN_PROC_AUXV;
2408 	name[3] = pid;
2409 	len = PROC_AUXV_MAX * sizeof(Elf32_Auxinfo);
2410 	auxv = NULL;
2411 	auxv32 = malloc(len);
2412 	if (auxv32 == NULL) {
2413 		warn("malloc(%zu)", len);
2414 		goto out;
2415 	}
2416 	if (sysctl(name, nitems(name), auxv32, &len, NULL, 0) == -1) {
2417 		if (errno != ESRCH && errno != EPERM)
2418 			warn("sysctl: kern.proc.auxv: %d: %d", pid, errno);
2419 		goto out;
2420 	}
2421 	count = len / sizeof(Elf32_Auxinfo);
2422 	auxv = malloc(count * sizeof(Elf_Auxinfo));
2423 	if (auxv == NULL) {
2424 		warn("malloc(%zu)", count * sizeof(Elf_Auxinfo));
2425 		goto out;
2426 	}
2427 	for (i = 0; i < count; i++) {
2428 		/*
2429 		 * XXX: We expect that values for a_type on a 32-bit platform
2430 		 * are directly mapped to values on 64-bit one, which is not
2431 		 * necessarily true.
2432 		 */
2433 		auxv[i].a_type = auxv32[i].a_type;
2434 		/*
2435 		 * Don't sign extend values.  Existing entries are positive
2436 		 * integers or pointers.  Under freebsd32, programs typically
2437 		 * have a full [0, 2^32) address space (perhaps minus the last
2438 		 * page) and treating this as a signed integer would be
2439 		 * confusing since these are not kernel pointers.
2440 		 *
2441 		 * XXX: A more complete translation would be ABI and
2442 		 * type-aware.
2443 		 */
2444 		auxv[i].a_un.a_val = (uint32_t)auxv32[i].a_un.a_val;
2445 	}
2446 	*cntp = count;
2447 out:
2448 	free(auxv32);
2449 	return (auxv);
2450 }
2451 #endif /* PS_ARCH_HAS_FREEBSD32 */
2452 
2453 static Elf_Auxinfo *
procstat_getauxv_sysctl(pid_t pid,unsigned int * cntp)2454 procstat_getauxv_sysctl(pid_t pid, unsigned int *cntp)
2455 {
2456 	Elf_Auxinfo *auxv;
2457 	int name[4];
2458 	size_t len;
2459 
2460 #ifdef PS_ARCH_HAS_FREEBSD32
2461 	if (is_elf32_sysctl(pid))
2462 		return (procstat_getauxv32_sysctl(pid, cntp));
2463 #endif
2464 	name[0] = CTL_KERN;
2465 	name[1] = KERN_PROC;
2466 	name[2] = KERN_PROC_AUXV;
2467 	name[3] = pid;
2468 	len = PROC_AUXV_MAX * sizeof(Elf_Auxinfo);
2469 	auxv = malloc(len);
2470 	if (auxv == NULL) {
2471 		warn("malloc(%zu)", len);
2472 		return (NULL);
2473 	}
2474 	if (sysctl(name, nitems(name), auxv, &len, NULL, 0) == -1) {
2475 		if (errno != ESRCH && errno != EPERM)
2476 			warn("sysctl: kern.proc.auxv: %d: %d", pid, errno);
2477 		free(auxv);
2478 		return (NULL);
2479 	}
2480 	*cntp = len / sizeof(Elf_Auxinfo);
2481 	return (auxv);
2482 }
2483 
2484 static Elf_Auxinfo *
procstat_getauxv_core(struct procstat_core * core,unsigned int * cntp)2485 procstat_getauxv_core(struct procstat_core *core, unsigned int *cntp)
2486 {
2487 	Elf_Auxinfo *auxv;
2488 	size_t len;
2489 
2490 	auxv = procstat_core_get(core, PSC_TYPE_AUXV, NULL, &len);
2491 	if (auxv == NULL)
2492 		return (NULL);
2493 	*cntp = len / sizeof(Elf_Auxinfo);
2494 	return (auxv);
2495 }
2496 
2497 Elf_Auxinfo *
procstat_getauxv(struct procstat * procstat,struct kinfo_proc * kp,unsigned int * cntp)2498 procstat_getauxv(struct procstat *procstat, struct kinfo_proc *kp,
2499     unsigned int *cntp)
2500 {
2501 	switch (procstat->type) {
2502 	case PROCSTAT_KVM:
2503 		warnx("kvm method is not supported");
2504 		return (NULL);
2505 	case PROCSTAT_SYSCTL:
2506 		return (procstat_getauxv_sysctl(kp->ki_pid, cntp));
2507 	case PROCSTAT_CORE:
2508 		return (procstat_getauxv_core(procstat->core, cntp));
2509 	default:
2510 		warnx("unknown access method: %d", procstat->type);
2511 		return (NULL);
2512 	}
2513 }
2514 
2515 void
procstat_freeauxv(struct procstat * procstat __unused,Elf_Auxinfo * auxv)2516 procstat_freeauxv(struct procstat *procstat __unused, Elf_Auxinfo *auxv)
2517 {
2518 
2519 	free(auxv);
2520 }
2521 
2522 static struct ptrace_lwpinfo *
procstat_getptlwpinfo_core(struct procstat_core * core,unsigned int * cntp)2523 procstat_getptlwpinfo_core(struct procstat_core *core, unsigned int *cntp)
2524 {
2525 	void *buf;
2526 	struct ptrace_lwpinfo *pl;
2527 	unsigned int cnt;
2528 	size_t len;
2529 
2530 	cnt = procstat_core_note_count(core, PSC_TYPE_PTLWPINFO);
2531 	if (cnt == 0)
2532 		return (NULL);
2533 
2534 	len = cnt * sizeof(*pl);
2535 	buf = calloc(1, len);
2536 	pl = procstat_core_get(core, PSC_TYPE_PTLWPINFO, buf, &len);
2537 	if (pl == NULL) {
2538 		free(buf);
2539 		return (NULL);
2540 	}
2541 	*cntp = len / sizeof(*pl);
2542 	return (pl);
2543 }
2544 
2545 struct ptrace_lwpinfo *
procstat_getptlwpinfo(struct procstat * procstat,unsigned int * cntp)2546 procstat_getptlwpinfo(struct procstat *procstat, unsigned int *cntp)
2547 {
2548 	switch (procstat->type) {
2549 	case PROCSTAT_KVM:
2550 		warnx("kvm method is not supported");
2551 		return (NULL);
2552 	case PROCSTAT_SYSCTL:
2553 		warnx("sysctl method is not supported");
2554 		return (NULL);
2555 	case PROCSTAT_CORE:
2556 	 	return (procstat_getptlwpinfo_core(procstat->core, cntp));
2557 	default:
2558 		warnx("unknown access method: %d", procstat->type);
2559 		return (NULL);
2560 	}
2561 }
2562 
2563 void
procstat_freeptlwpinfo(struct procstat * procstat __unused,struct ptrace_lwpinfo * pl)2564 procstat_freeptlwpinfo(struct procstat *procstat __unused,
2565     struct ptrace_lwpinfo *pl)
2566 {
2567 	free(pl);
2568 }
2569 
2570 static struct kinfo_kstack *
procstat_getkstack_sysctl(pid_t pid,int * cntp)2571 procstat_getkstack_sysctl(pid_t pid, int *cntp)
2572 {
2573 	struct kinfo_kstack *kkstp;
2574 	int error, name[4];
2575 	size_t len;
2576 
2577 	name[0] = CTL_KERN;
2578 	name[1] = KERN_PROC;
2579 	name[2] = KERN_PROC_KSTACK;
2580 	name[3] = pid;
2581 
2582 	len = 0;
2583 	error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
2584 	if (error < 0 && errno != ESRCH && errno != EPERM && errno != ENOENT) {
2585 		warn("sysctl: kern.proc.kstack: %d", pid);
2586 		return (NULL);
2587 	}
2588 	if (error == -1 && errno == ENOENT) {
2589 		warnx("sysctl: kern.proc.kstack unavailable"
2590 		    " (options DDB or options STACK required in kernel)");
2591 		return (NULL);
2592 	}
2593 	if (error == -1)
2594 		return (NULL);
2595 	kkstp = malloc(len);
2596 	if (kkstp == NULL) {
2597 		warn("malloc(%zu)", len);
2598 		return (NULL);
2599 	}
2600 	if (sysctl(name, nitems(name), kkstp, &len, NULL, 0) == -1 &&
2601 	    errno != ENOMEM) {
2602 		warn("sysctl: kern.proc.pid: %d", pid);
2603 		free(kkstp);
2604 		return (NULL);
2605 	}
2606 	*cntp = len / sizeof(*kkstp);
2607 
2608 	return (kkstp);
2609 }
2610 
2611 struct kinfo_kstack *
procstat_getkstack(struct procstat * procstat,struct kinfo_proc * kp,unsigned int * cntp)2612 procstat_getkstack(struct procstat *procstat, struct kinfo_proc *kp,
2613     unsigned int *cntp)
2614 {
2615 	switch (procstat->type) {
2616 	case PROCSTAT_KVM:
2617 		warnx("kvm method is not supported");
2618 		return (NULL);
2619 	case PROCSTAT_SYSCTL:
2620 		return (procstat_getkstack_sysctl(kp->ki_pid, cntp));
2621 	case PROCSTAT_CORE:
2622 		warnx("core method is not supported");
2623 		return (NULL);
2624 	default:
2625 		warnx("unknown access method: %d", procstat->type);
2626 		return (NULL);
2627 	}
2628 }
2629 
2630 void
procstat_freekstack(struct procstat * procstat __unused,struct kinfo_kstack * kkstp)2631 procstat_freekstack(struct procstat *procstat __unused,
2632     struct kinfo_kstack *kkstp)
2633 {
2634 
2635 	free(kkstp);
2636 }
2637 
2638 static struct advlock_list *
procstat_getadvlock_sysctl(struct procstat * procstat __unused)2639 procstat_getadvlock_sysctl(struct procstat *procstat __unused)
2640 {
2641 	struct advlock_list *res;
2642 	struct advlock *a;
2643 	void *buf;
2644 	char *c;
2645 	struct kinfo_lockf *kl;
2646 	size_t buf_len;
2647 	int error;
2648 	static const int kl_name[] = { CTL_KERN, KERN_LOCKF };
2649 
2650 	res = malloc(sizeof(*res));
2651 	if (res == NULL)
2652 		return (NULL);
2653 	STAILQ_INIT(res);
2654 	buf = NULL;
2655 
2656 	buf_len = 0;
2657 	error = sysctl(kl_name, nitems(kl_name), NULL, &buf_len, NULL, 0);
2658 	if (error != 0) {
2659 		warn("sysctl KERN_LOCKF size");
2660 		goto fail;
2661 	}
2662 	buf_len *= 2;
2663 	buf = malloc(buf_len);
2664 	if (buf == NULL) {
2665 		warn("malloc");
2666 		goto fail;
2667 	}
2668 	error = sysctl(kl_name, nitems(kl_name), buf, &buf_len, NULL, 0);
2669 	if (error != 0) {
2670 		warn("sysctl KERN_LOCKF data");
2671 		goto fail;
2672 	}
2673 
2674 	for (c = buf; (char *)c < (char *)buf + buf_len;
2675 	    c += kl->kl_structsize) {
2676 		kl = (struct kinfo_lockf *)(void *)c;
2677 		if (sizeof(*kl) < (size_t)kl->kl_structsize) {
2678 			warn("ABI broken");
2679 			goto fail;
2680 		}
2681 		a = malloc(sizeof(*a));
2682 		if (a == NULL) {
2683 			warn("malloc advlock");
2684 			goto fail;
2685 		}
2686 		switch (kl->kl_rw) {
2687 		case KLOCKF_RW_READ:
2688 			a->rw = PS_ADVLOCK_RO;
2689 			break;
2690 		case KLOCKF_RW_WRITE:
2691 			a->rw = PS_ADVLOCK_RW;
2692 			break;
2693 		default:
2694 			warn("ABI broken");
2695 			free(a);
2696 			goto fail;
2697 		}
2698 		switch (kl->kl_type) {
2699 		case KLOCKF_TYPE_FLOCK:
2700 			a->type = PS_ADVLOCK_TYPE_FLOCK;
2701 			break;
2702 		case KLOCKF_TYPE_PID:
2703 			a->type = PS_ADVLOCK_TYPE_PID;
2704 			break;
2705 		case KLOCKF_TYPE_REMOTE:
2706 			a->type = PS_ADVLOCK_TYPE_REMOTE;
2707 			break;
2708 		default:
2709 			warn("ABI broken");
2710 			free(a);
2711 			goto fail;
2712 		}
2713 		a->pid = kl->kl_pid;
2714 		a->sysid = kl->kl_sysid;
2715 		a->file_fsid = kl->kl_file_fsid;
2716 		a->file_rdev = kl->kl_file_rdev;
2717 		a->file_fileid = kl->kl_file_fileid;
2718 		a->start = kl->kl_start;
2719 		a->len = kl->kl_len;
2720 		if (kl->kl_path[0] != '\0') {
2721 			a->path = strdup(kl->kl_path);
2722 			if (a->path == NULL) {
2723 				warn("malloc");
2724 				free(a);
2725 				goto fail;
2726 			}
2727 		} else
2728 			a->path = NULL;
2729 		STAILQ_INSERT_TAIL(res, a, next);
2730 	}
2731 
2732 	free(buf);
2733 	return (res);
2734 
2735 fail:
2736 	free(buf);
2737 	procstat_freeadvlock(procstat, res);
2738 	return (NULL);
2739 }
2740 
2741 struct advlock_list *
procstat_getadvlock(struct procstat * procstat)2742 procstat_getadvlock(struct procstat *procstat)
2743 {
2744 	switch (procstat->type) {
2745 	case PROCSTAT_KVM:
2746 		warnx("kvm method is not supported");
2747 		return (NULL);
2748 	case PROCSTAT_SYSCTL:
2749 		return (procstat_getadvlock_sysctl(procstat));
2750 	case PROCSTAT_CORE:
2751 		warnx("core method is not supported");
2752 		return (NULL);
2753 	default:
2754 		warnx("unknown access method: %d", procstat->type);
2755 		return (NULL);
2756 	}
2757 }
2758 
2759 void
procstat_freeadvlock(struct procstat * procstat __unused,struct advlock_list * lst)2760 procstat_freeadvlock(struct procstat *procstat __unused,
2761     struct advlock_list *lst)
2762 {
2763 	struct advlock *a, *a1;
2764 
2765 	STAILQ_FOREACH_SAFE(a, lst, next, a1) {
2766 		free(__DECONST(char *, a->path));
2767 		free(a);
2768 	}
2769 	free(lst);
2770 }
2771 
2772 static rlim_t *
procstat_getrlimitusage_sysctl(pid_t pid,unsigned * cntp)2773 procstat_getrlimitusage_sysctl(pid_t pid, unsigned *cntp)
2774 {
2775 	int error, name[4];
2776 	rlim_t *val;
2777 	size_t len;
2778 
2779 	name[0] = CTL_KERN;
2780 	name[1] = KERN_PROC;
2781 	name[2] = KERN_PROC_RLIMIT_USAGE;
2782 	name[3] = pid;
2783 
2784 	len = 0;
2785 	error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
2786 	if (error == -1)
2787 		return (NULL);
2788 	val = malloc(len);
2789 	if (val == NULL)
2790 		return (NULL);
2791 
2792 	error = sysctl(name, nitems(name), val, &len, NULL, 0);
2793 	if (error == -1) {
2794 		free(val);
2795 		return (NULL);
2796 	}
2797 	*cntp = len / sizeof(rlim_t);
2798 	return (val);
2799 }
2800 
2801 rlim_t *
procstat_getrlimitusage(struct procstat * procstat,struct kinfo_proc * kp,unsigned int * cntp)2802 procstat_getrlimitusage(struct procstat *procstat, struct kinfo_proc *kp,
2803     unsigned int *cntp)
2804 {
2805 	switch (procstat->type) {
2806 	case PROCSTAT_KVM:
2807 		warnx("kvm method is not supported");
2808 		return (NULL);
2809 	case PROCSTAT_SYSCTL:
2810 		return (procstat_getrlimitusage_sysctl(kp->ki_pid, cntp));
2811 	case PROCSTAT_CORE:
2812 		warnx("core method is not supported");
2813 		return (NULL);
2814 	default:
2815 		warnx("unknown access method: %d", procstat->type);
2816 		return (NULL);
2817 	}
2818 }
2819 
2820 void
procstat_freerlimitusage(struct procstat * procstat __unused,rlim_t * resusage)2821 procstat_freerlimitusage(struct procstat *procstat __unused, rlim_t *resusage)
2822 {
2823 	free(resusage);
2824 }
2825 
2826 static struct kinfo_knote *
procstat_get_kqueue_info_sysctl(pid_t pid,int kqfd,unsigned int * cntp,char * errbuf)2827 procstat_get_kqueue_info_sysctl(pid_t pid, int kqfd, unsigned int *cntp,
2828     char *errbuf)
2829 {
2830 	int error, name[5];
2831 	struct kinfo_knote *val;
2832 	size_t len;
2833 
2834 	name[0] = CTL_KERN;
2835 	name[1] = KERN_PROC;
2836 	name[2] = KERN_PROC_KQUEUE;
2837 	name[3] = pid;
2838 	name[4] = kqfd;
2839 
2840 	len = 0;
2841 	error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
2842 	if (error == -1) {
2843 		snprintf(errbuf, _POSIX2_LINE_MAX,
2844 		    "KERN_PROC_KQUEUE.pid<%d>.kq<%d> (size q) failed: %s",
2845 		    pid, kqfd, strerror(errno));
2846 		return (NULL);
2847 	}
2848 	val = malloc(len);
2849 	if (val == NULL) {
2850 		snprintf(errbuf, _POSIX2_LINE_MAX, "no memory");
2851 		return (NULL);
2852 	}
2853 
2854 	error = sysctl(name, nitems(name), val, &len, NULL, 0);
2855 	if (error == -1) {
2856 		snprintf(errbuf, _POSIX2_LINE_MAX,
2857 		    "KERN_PROC_KQUEUE.pid<%d>.kq<%d> failed: %s",
2858 		    pid, kqfd, strerror(errno));
2859 		free(val);
2860 		return (NULL);
2861 	}
2862 	*cntp = len / sizeof(*val);
2863 	return (val);
2864 }
2865 
2866 struct kinfo_knote *
procstat_get_kqueue_info(struct procstat * procstat,struct kinfo_proc * kp,int kqfd,unsigned int * count,char * errbuf)2867 procstat_get_kqueue_info(struct procstat *procstat,
2868     struct kinfo_proc *kp, int kqfd, unsigned int *count, char *errbuf)
2869 {
2870 	struct kinfo_knote *kn, *k, *res, *rn;
2871 	size_t len, kqn;
2872 
2873 	switch (procstat->type) {
2874 	case PROCSTAT_KVM:
2875 		warnx("kvm method is not supported");
2876 		return (NULL);
2877 	case PROCSTAT_SYSCTL:
2878 		return (procstat_get_kqueue_info_sysctl(kp->ki_pid, kqfd,
2879 		    count, errbuf));
2880 	case PROCSTAT_CORE:
2881 		k = procstat_core_get(procstat->core, PSC_TYPE_KQUEUES,
2882 		    NULL, &len);
2883 		if (k == NULL) {
2884 			snprintf(errbuf, _POSIX2_LINE_MAX,
2885 			    "getting NT_PROCSTAT_KQUEUES note failed");
2886 			*count = 0;
2887 			return (NULL);
2888 		}
2889 		for (kqn = 0, kn = k; kn < k + len / sizeof(*kn); kn++) {
2890 			if (kn->knt_kq_fd == kqfd)
2891 				kqn++;
2892 		}
2893 		res = calloc(kqn, sizeof(*res));
2894 		if (res == NULL) {
2895 			free(k);
2896 			snprintf(errbuf, _POSIX2_LINE_MAX,
2897 			    "no memory");
2898 			return (NULL);
2899 		}
2900 		for (kn = k, rn = res; kn < k + len / sizeof(*kn); kn++) {
2901 			if (kn->knt_kq_fd != kqfd)
2902 				continue;
2903 			*rn = *kn;
2904 			rn++;
2905 		}
2906 		*count = kqn;
2907 		free(k);
2908 		return (res);
2909 	default:
2910 		warnx("unknown access method: %d", procstat->type);
2911 		return (NULL);
2912 	}
2913 }
2914 
2915 void
procstat_freekqinfo(struct procstat * procstat __unused,struct kinfo_knote * v)2916 procstat_freekqinfo(struct procstat *procstat __unused, struct kinfo_knote *v)
2917 {
2918 	free(v);
2919 }
2920