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