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