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