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