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