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