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