libprocstat.c revision 249672
1/*- 2 * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org> 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by the University of 17 * California, Berkeley and its contributors. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD: head/lib/libprocstat/libprocstat.c 249672 2013-04-20 07:57:08Z trociny $"); 37 38#include <sys/param.h> 39#include <sys/time.h> 40#include <sys/proc.h> 41#include <sys/user.h> 42#include <sys/stat.h> 43#include <sys/vnode.h> 44#include <sys/socket.h> 45#include <sys/socketvar.h> 46#include <sys/domain.h> 47#include <sys/protosw.h> 48#include <sys/un.h> 49#include <sys/unpcb.h> 50#include <sys/sysctl.h> 51#include <sys/tty.h> 52#include <sys/filedesc.h> 53#include <sys/queue.h> 54#define _WANT_FILE 55#include <sys/file.h> 56#include <sys/conf.h> 57#include <sys/mman.h> 58#define _KERNEL 59#include <sys/mount.h> 60#include <sys/pipe.h> 61#include <ufs/ufs/quota.h> 62#include <ufs/ufs/inode.h> 63#include <fs/devfs/devfs.h> 64#include <fs/devfs/devfs_int.h> 65#undef _KERNEL 66#include <nfs/nfsproto.h> 67#include <nfsclient/nfs.h> 68#include <nfsclient/nfsnode.h> 69 70#include <vm/vm.h> 71#include <vm/vm_map.h> 72#include <vm/vm_object.h> 73 74#include <net/route.h> 75#include <netinet/in.h> 76#include <netinet/in_systm.h> 77#include <netinet/ip.h> 78#include <netinet/in_pcb.h> 79 80#include <assert.h> 81#include <ctype.h> 82#include <err.h> 83#include <fcntl.h> 84#include <kvm.h> 85#include <libutil.h> 86#include <limits.h> 87#include <paths.h> 88#include <pwd.h> 89#include <stdio.h> 90#include <stdlib.h> 91#include <stddef.h> 92#include <string.h> 93#include <unistd.h> 94#include <netdb.h> 95 96#include <libprocstat.h> 97#include "libprocstat_internal.h" 98#include "common_kvm.h" 99#include "core.h" 100 101int statfs(const char *, struct statfs *); /* XXX */ 102 103#define PROCSTAT_KVM 1 104#define PROCSTAT_SYSCTL 2 105#define PROCSTAT_CORE 3 106 107static char *getmnton(kvm_t *kd, struct mount *m); 108static struct kinfo_vmentry * kinfo_getvmmap_core(struct procstat_core *core, 109 int *cntp); 110static struct filestat_list *procstat_getfiles_kvm( 111 struct procstat *procstat, struct kinfo_proc *kp, int mmapped); 112static struct filestat_list *procstat_getfiles_sysctl( 113 struct procstat *procstat, struct kinfo_proc *kp, int mmapped); 114static int procstat_get_pipe_info_sysctl(struct filestat *fst, 115 struct pipestat *pipe, char *errbuf); 116static int procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst, 117 struct pipestat *pipe, char *errbuf); 118static int procstat_get_pts_info_sysctl(struct filestat *fst, 119 struct ptsstat *pts, char *errbuf); 120static int procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst, 121 struct ptsstat *pts, char *errbuf); 122static int procstat_get_shm_info_sysctl(struct filestat *fst, 123 struct shmstat *shm, char *errbuf); 124static int procstat_get_shm_info_kvm(kvm_t *kd, struct filestat *fst, 125 struct shmstat *shm, char *errbuf); 126static int procstat_get_socket_info_sysctl(struct filestat *fst, 127 struct sockstat *sock, char *errbuf); 128static int procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst, 129 struct sockstat *sock, char *errbuf); 130static int to_filestat_flags(int flags); 131static int procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst, 132 struct vnstat *vn, char *errbuf); 133static int procstat_get_vnode_info_sysctl(struct filestat *fst, 134 struct vnstat *vn, char *errbuf); 135static gid_t *procstat_getgroups_core(struct procstat_core *core, 136 unsigned int *count); 137static gid_t *procstat_getgroups_sysctl(pid_t pid, unsigned int *count); 138static int procstat_getumask_core(struct procstat_core *core, 139 unsigned short *maskp); 140static int procstat_getumask_sysctl(pid_t pid, unsigned short *maskp); 141static int vntype2psfsttype(int type); 142 143void 144procstat_close(struct procstat *procstat) 145{ 146 147 assert(procstat); 148 if (procstat->type == PROCSTAT_KVM) 149 kvm_close(procstat->kd); 150 else if (procstat->type == PROCSTAT_CORE) 151 procstat_core_close(procstat->core); 152 free(procstat); 153} 154 155struct procstat * 156procstat_open_sysctl(void) 157{ 158 struct procstat *procstat; 159 160 procstat = calloc(1, sizeof(*procstat)); 161 if (procstat == NULL) { 162 warn("malloc()"); 163 return (NULL); 164 } 165 procstat->type = PROCSTAT_SYSCTL; 166 return (procstat); 167} 168 169struct procstat * 170procstat_open_kvm(const char *nlistf, const char *memf) 171{ 172 struct procstat *procstat; 173 kvm_t *kd; 174 char buf[_POSIX2_LINE_MAX]; 175 176 procstat = calloc(1, sizeof(*procstat)); 177 if (procstat == NULL) { 178 warn("malloc()"); 179 return (NULL); 180 } 181 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf); 182 if (kd == NULL) { 183 warnx("kvm_openfiles(): %s", buf); 184 free(procstat); 185 return (NULL); 186 } 187 procstat->type = PROCSTAT_KVM; 188 procstat->kd = kd; 189 return (procstat); 190} 191 192struct procstat * 193procstat_open_core(const char *filename) 194{ 195 struct procstat *procstat; 196 struct procstat_core *core; 197 198 procstat = calloc(1, sizeof(*procstat)); 199 if (procstat == NULL) { 200 warn("malloc()"); 201 return (NULL); 202 } 203 core = procstat_core_open(filename); 204 if (core == NULL) { 205 free(procstat); 206 return (NULL); 207 } 208 procstat->type = PROCSTAT_CORE; 209 procstat->core = core; 210 return (procstat); 211} 212 213struct kinfo_proc * 214procstat_getprocs(struct procstat *procstat, int what, int arg, 215 unsigned int *count) 216{ 217 struct kinfo_proc *p0, *p; 218 size_t len; 219 int name[4]; 220 int cnt; 221 int error; 222 223 assert(procstat); 224 assert(count); 225 p = NULL; 226 if (procstat->type == PROCSTAT_KVM) { 227 *count = 0; 228 p0 = kvm_getprocs(procstat->kd, what, arg, &cnt); 229 if (p0 == NULL || cnt <= 0) 230 return (NULL); 231 *count = cnt; 232 len = *count * sizeof(*p); 233 p = malloc(len); 234 if (p == NULL) { 235 warnx("malloc(%zu)", len); 236 goto fail; 237 } 238 bcopy(p0, p, len); 239 return (p); 240 } else if (procstat->type == PROCSTAT_SYSCTL) { 241 len = 0; 242 name[0] = CTL_KERN; 243 name[1] = KERN_PROC; 244 name[2] = what; 245 name[3] = arg; 246 error = sysctl(name, 4, NULL, &len, NULL, 0); 247 if (error < 0 && errno != EPERM) { 248 warn("sysctl(kern.proc)"); 249 goto fail; 250 } 251 if (len == 0) { 252 warnx("no processes?"); 253 goto fail; 254 } 255 p = malloc(len); 256 if (p == NULL) { 257 warnx("malloc(%zu)", len); 258 goto fail; 259 } 260 error = sysctl(name, 4, p, &len, NULL, 0); 261 if (error < 0 && errno != EPERM) { 262 warn("sysctl(kern.proc)"); 263 goto fail; 264 } 265 /* Perform simple consistency checks. */ 266 if ((len % sizeof(*p)) != 0 || p->ki_structsize != sizeof(*p)) { 267 warnx("kinfo_proc structure size mismatch (len = %zu)", len); 268 goto fail; 269 } 270 *count = len / sizeof(*p); 271 return (p); 272 } else if (procstat->type == PROCSTAT_CORE) { 273 p = procstat_core_get(procstat->core, PSC_TYPE_PROC, NULL, 274 &len); 275 if ((len % sizeof(*p)) != 0 || p->ki_structsize != sizeof(*p)) { 276 warnx("kinfo_proc structure size mismatch"); 277 goto fail; 278 } 279 *count = len / sizeof(*p); 280 return (p); 281 } else { 282 warnx("unknown access method: %d", procstat->type); 283 return (NULL); 284 } 285fail: 286 if (p) 287 free(p); 288 return (NULL); 289} 290 291void 292procstat_freeprocs(struct procstat *procstat __unused, struct kinfo_proc *p) 293{ 294 295 if (p != NULL) 296 free(p); 297 p = NULL; 298} 299 300struct filestat_list * 301procstat_getfiles(struct procstat *procstat, struct kinfo_proc *kp, int mmapped) 302{ 303 304 switch(procstat->type) { 305 case PROCSTAT_KVM: 306 return (procstat_getfiles_kvm(procstat, kp, mmapped)); 307 case PROCSTAT_SYSCTL: 308 case PROCSTAT_CORE: 309 return (procstat_getfiles_sysctl(procstat, kp, mmapped)); 310 default: 311 warnx("unknown access method: %d", procstat->type); 312 return (NULL); 313 } 314} 315 316void 317procstat_freefiles(struct procstat *procstat, struct filestat_list *head) 318{ 319 struct filestat *fst, *tmp; 320 321 STAILQ_FOREACH_SAFE(fst, head, next, tmp) { 322 if (fst->fs_path != NULL) 323 free(fst->fs_path); 324 free(fst); 325 } 326 free(head); 327 if (procstat->vmentries != NULL) { 328 free(procstat->vmentries); 329 procstat->vmentries = NULL; 330 } 331 if (procstat->files != NULL) { 332 free(procstat->files); 333 procstat->files = NULL; 334 } 335} 336 337static struct filestat * 338filestat_new_entry(void *typedep, int type, int fd, int fflags, int uflags, 339 int refcount, off_t offset, char *path, cap_rights_t cap_rights) 340{ 341 struct filestat *entry; 342 343 entry = calloc(1, sizeof(*entry)); 344 if (entry == NULL) { 345 warn("malloc()"); 346 return (NULL); 347 } 348 entry->fs_typedep = typedep; 349 entry->fs_fflags = fflags; 350 entry->fs_uflags = uflags; 351 entry->fs_fd = fd; 352 entry->fs_type = type; 353 entry->fs_ref_count = refcount; 354 entry->fs_offset = offset; 355 entry->fs_path = path; 356 entry->fs_cap_rights = cap_rights; 357 return (entry); 358} 359 360static struct vnode * 361getctty(kvm_t *kd, struct kinfo_proc *kp) 362{ 363 struct pgrp pgrp; 364 struct proc proc; 365 struct session sess; 366 int error; 367 368 assert(kp); 369 error = kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc, 370 sizeof(proc)); 371 if (error == 0) { 372 warnx("can't read proc struct at %p for pid %d", 373 kp->ki_paddr, kp->ki_pid); 374 return (NULL); 375 } 376 if (proc.p_pgrp == NULL) 377 return (NULL); 378 error = kvm_read_all(kd, (unsigned long)proc.p_pgrp, &pgrp, 379 sizeof(pgrp)); 380 if (error == 0) { 381 warnx("can't read pgrp struct at %p for pid %d", 382 proc.p_pgrp, kp->ki_pid); 383 return (NULL); 384 } 385 error = kvm_read_all(kd, (unsigned long)pgrp.pg_session, &sess, 386 sizeof(sess)); 387 if (error == 0) { 388 warnx("can't read session struct at %p for pid %d", 389 pgrp.pg_session, kp->ki_pid); 390 return (NULL); 391 } 392 return (sess.s_ttyvp); 393} 394 395static struct filestat_list * 396procstat_getfiles_kvm(struct procstat *procstat, struct kinfo_proc *kp, int mmapped) 397{ 398 struct file file; 399 struct filedesc filed; 400 struct vm_map_entry vmentry; 401 struct vm_object object; 402 struct vmspace vmspace; 403 vm_map_entry_t entryp; 404 vm_map_t map; 405 vm_object_t objp; 406 struct vnode *vp; 407 struct file **ofiles; 408 struct filestat *entry; 409 struct filestat_list *head; 410 kvm_t *kd; 411 void *data; 412 int i, fflags; 413 int prot, type; 414 unsigned int nfiles; 415 416 assert(procstat); 417 kd = procstat->kd; 418 if (kd == NULL) 419 return (NULL); 420 if (kp->ki_fd == NULL) 421 return (NULL); 422 if (!kvm_read_all(kd, (unsigned long)kp->ki_fd, &filed, 423 sizeof(filed))) { 424 warnx("can't read filedesc at %p", (void *)kp->ki_fd); 425 return (NULL); 426 } 427 428 /* 429 * Allocate list head. 430 */ 431 head = malloc(sizeof(*head)); 432 if (head == NULL) 433 return (NULL); 434 STAILQ_INIT(head); 435 436 /* root directory vnode, if one. */ 437 if (filed.fd_rdir) { 438 entry = filestat_new_entry(filed.fd_rdir, PS_FST_TYPE_VNODE, -1, 439 PS_FST_FFLAG_READ, PS_FST_UFLAG_RDIR, 0, 0, NULL, 0); 440 if (entry != NULL) 441 STAILQ_INSERT_TAIL(head, entry, next); 442 } 443 /* current working directory vnode. */ 444 if (filed.fd_cdir) { 445 entry = filestat_new_entry(filed.fd_cdir, PS_FST_TYPE_VNODE, -1, 446 PS_FST_FFLAG_READ, PS_FST_UFLAG_CDIR, 0, 0, NULL, 0); 447 if (entry != NULL) 448 STAILQ_INSERT_TAIL(head, entry, next); 449 } 450 /* jail root, if any. */ 451 if (filed.fd_jdir) { 452 entry = filestat_new_entry(filed.fd_jdir, PS_FST_TYPE_VNODE, -1, 453 PS_FST_FFLAG_READ, PS_FST_UFLAG_JAIL, 0, 0, NULL, 0); 454 if (entry != NULL) 455 STAILQ_INSERT_TAIL(head, entry, next); 456 } 457 /* ktrace vnode, if one */ 458 if (kp->ki_tracep) { 459 entry = filestat_new_entry(kp->ki_tracep, PS_FST_TYPE_VNODE, -1, 460 PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE, 461 PS_FST_UFLAG_TRACE, 0, 0, NULL, 0); 462 if (entry != NULL) 463 STAILQ_INSERT_TAIL(head, entry, next); 464 } 465 /* text vnode, if one */ 466 if (kp->ki_textvp) { 467 entry = filestat_new_entry(kp->ki_textvp, PS_FST_TYPE_VNODE, -1, 468 PS_FST_FFLAG_READ, PS_FST_UFLAG_TEXT, 0, 0, NULL, 0); 469 if (entry != NULL) 470 STAILQ_INSERT_TAIL(head, entry, next); 471 } 472 /* Controlling terminal. */ 473 if ((vp = getctty(kd, kp)) != NULL) { 474 entry = filestat_new_entry(vp, PS_FST_TYPE_VNODE, -1, 475 PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE, 476 PS_FST_UFLAG_CTTY, 0, 0, NULL, 0); 477 if (entry != NULL) 478 STAILQ_INSERT_TAIL(head, entry, next); 479 } 480 481 nfiles = filed.fd_lastfile + 1; 482 ofiles = malloc(nfiles * sizeof(struct file *)); 483 if (ofiles == NULL) { 484 warn("malloc(%zu)", nfiles * sizeof(struct file *)); 485 goto do_mmapped; 486 } 487 if (!kvm_read_all(kd, (unsigned long)filed.fd_ofiles, ofiles, 488 nfiles * sizeof(struct file *))) { 489 warnx("cannot read file structures at %p", 490 (void *)filed.fd_ofiles); 491 free(ofiles); 492 goto do_mmapped; 493 } 494 for (i = 0; i <= filed.fd_lastfile; i++) { 495 if (ofiles[i] == NULL) 496 continue; 497 if (!kvm_read_all(kd, (unsigned long)ofiles[i], &file, 498 sizeof(struct file))) { 499 warnx("can't read file %d at %p", i, 500 (void *)ofiles[i]); 501 continue; 502 } 503 switch (file.f_type) { 504 case DTYPE_VNODE: 505 type = PS_FST_TYPE_VNODE; 506 data = file.f_vnode; 507 break; 508 case DTYPE_SOCKET: 509 type = PS_FST_TYPE_SOCKET; 510 data = file.f_data; 511 break; 512 case DTYPE_PIPE: 513 type = PS_FST_TYPE_PIPE; 514 data = file.f_data; 515 break; 516 case DTYPE_FIFO: 517 type = PS_FST_TYPE_FIFO; 518 data = file.f_vnode; 519 break; 520#ifdef DTYPE_PTS 521 case DTYPE_PTS: 522 type = PS_FST_TYPE_PTS; 523 data = file.f_data; 524 break; 525#endif 526 case DTYPE_SHM: 527 type = PS_FST_TYPE_SHM; 528 data = file.f_data; 529 break; 530 default: 531 continue; 532 } 533 /* XXXRW: No capability rights support for kvm yet. */ 534 entry = filestat_new_entry(data, type, i, 535 to_filestat_flags(file.f_flag), 0, 0, 0, NULL, 0); 536 if (entry != NULL) 537 STAILQ_INSERT_TAIL(head, entry, next); 538 } 539 free(ofiles); 540 541do_mmapped: 542 543 /* 544 * Process mmapped files if requested. 545 */ 546 if (mmapped) { 547 if (!kvm_read_all(kd, (unsigned long)kp->ki_vmspace, &vmspace, 548 sizeof(vmspace))) { 549 warnx("can't read vmspace at %p", 550 (void *)kp->ki_vmspace); 551 goto exit; 552 } 553 map = &vmspace.vm_map; 554 555 for (entryp = map->header.next; 556 entryp != &kp->ki_vmspace->vm_map.header; 557 entryp = vmentry.next) { 558 if (!kvm_read_all(kd, (unsigned long)entryp, &vmentry, 559 sizeof(vmentry))) { 560 warnx("can't read vm_map_entry at %p", 561 (void *)entryp); 562 continue; 563 } 564 if (vmentry.eflags & MAP_ENTRY_IS_SUB_MAP) 565 continue; 566 if ((objp = vmentry.object.vm_object) == NULL) 567 continue; 568 for (; objp; objp = object.backing_object) { 569 if (!kvm_read_all(kd, (unsigned long)objp, 570 &object, sizeof(object))) { 571 warnx("can't read vm_object at %p", 572 (void *)objp); 573 break; 574 } 575 } 576 577 /* We want only vnode objects. */ 578 if (object.type != OBJT_VNODE) 579 continue; 580 581 prot = vmentry.protection; 582 fflags = 0; 583 if (prot & VM_PROT_READ) 584 fflags = PS_FST_FFLAG_READ; 585 if ((vmentry.eflags & MAP_ENTRY_COW) == 0 && 586 prot & VM_PROT_WRITE) 587 fflags |= PS_FST_FFLAG_WRITE; 588 589 /* 590 * Create filestat entry. 591 */ 592 entry = filestat_new_entry(object.handle, 593 PS_FST_TYPE_VNODE, -1, fflags, 594 PS_FST_UFLAG_MMAP, 0, 0, NULL, 0); 595 if (entry != NULL) 596 STAILQ_INSERT_TAIL(head, entry, next); 597 } 598 } 599exit: 600 return (head); 601} 602 603/* 604 * kinfo types to filestat translation. 605 */ 606static int 607kinfo_type2fst(int kftype) 608{ 609 static struct { 610 int kf_type; 611 int fst_type; 612 } kftypes2fst[] = { 613 { KF_TYPE_CRYPTO, PS_FST_TYPE_CRYPTO }, 614 { KF_TYPE_FIFO, PS_FST_TYPE_FIFO }, 615 { KF_TYPE_KQUEUE, PS_FST_TYPE_KQUEUE }, 616 { KF_TYPE_MQUEUE, PS_FST_TYPE_MQUEUE }, 617 { KF_TYPE_NONE, PS_FST_TYPE_NONE }, 618 { KF_TYPE_PIPE, PS_FST_TYPE_PIPE }, 619 { KF_TYPE_PTS, PS_FST_TYPE_PTS }, 620 { KF_TYPE_SEM, PS_FST_TYPE_SEM }, 621 { KF_TYPE_SHM, PS_FST_TYPE_SHM }, 622 { KF_TYPE_SOCKET, PS_FST_TYPE_SOCKET }, 623 { KF_TYPE_VNODE, PS_FST_TYPE_VNODE }, 624 { KF_TYPE_UNKNOWN, PS_FST_TYPE_UNKNOWN } 625 }; 626#define NKFTYPES (sizeof(kftypes2fst) / sizeof(*kftypes2fst)) 627 unsigned int i; 628 629 for (i = 0; i < NKFTYPES; i++) 630 if (kftypes2fst[i].kf_type == kftype) 631 break; 632 if (i == NKFTYPES) 633 return (PS_FST_TYPE_UNKNOWN); 634 return (kftypes2fst[i].fst_type); 635} 636 637/* 638 * kinfo flags to filestat translation. 639 */ 640static int 641kinfo_fflags2fst(int kfflags) 642{ 643 static struct { 644 int kf_flag; 645 int fst_flag; 646 } kfflags2fst[] = { 647 { KF_FLAG_APPEND, PS_FST_FFLAG_APPEND }, 648 { KF_FLAG_ASYNC, PS_FST_FFLAG_ASYNC }, 649 { KF_FLAG_CREAT, PS_FST_FFLAG_CREAT }, 650 { KF_FLAG_DIRECT, PS_FST_FFLAG_DIRECT }, 651 { KF_FLAG_EXCL, PS_FST_FFLAG_EXCL }, 652 { KF_FLAG_EXEC, PS_FST_FFLAG_EXEC }, 653 { KF_FLAG_EXLOCK, PS_FST_FFLAG_EXLOCK }, 654 { KF_FLAG_FSYNC, PS_FST_FFLAG_SYNC }, 655 { KF_FLAG_HASLOCK, PS_FST_FFLAG_HASLOCK }, 656 { KF_FLAG_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW }, 657 { KF_FLAG_NONBLOCK, PS_FST_FFLAG_NONBLOCK }, 658 { KF_FLAG_READ, PS_FST_FFLAG_READ }, 659 { KF_FLAG_SHLOCK, PS_FST_FFLAG_SHLOCK }, 660 { KF_FLAG_TRUNC, PS_FST_FFLAG_TRUNC }, 661 { KF_FLAG_WRITE, PS_FST_FFLAG_WRITE } 662 }; 663#define NKFFLAGS (sizeof(kfflags2fst) / sizeof(*kfflags2fst)) 664 unsigned int i; 665 int flags; 666 667 flags = 0; 668 for (i = 0; i < NKFFLAGS; i++) 669 if ((kfflags & kfflags2fst[i].kf_flag) != 0) 670 flags |= kfflags2fst[i].fst_flag; 671 return (flags); 672} 673 674static int 675kinfo_uflags2fst(int fd) 676{ 677 678 switch (fd) { 679 case KF_FD_TYPE_CTTY: 680 return (PS_FST_UFLAG_CTTY); 681 case KF_FD_TYPE_CWD: 682 return (PS_FST_UFLAG_CDIR); 683 case KF_FD_TYPE_JAIL: 684 return (PS_FST_UFLAG_JAIL); 685 case KF_FD_TYPE_TEXT: 686 return (PS_FST_UFLAG_TEXT); 687 case KF_FD_TYPE_TRACE: 688 return (PS_FST_UFLAG_TRACE); 689 case KF_FD_TYPE_ROOT: 690 return (PS_FST_UFLAG_RDIR); 691 } 692 return (0); 693} 694 695static struct kinfo_file * 696kinfo_getfile_core(struct procstat_core *core, int *cntp) 697{ 698 int cnt; 699 size_t len; 700 char *buf, *bp, *eb; 701 struct kinfo_file *kif, *kp, *kf; 702 703 buf = procstat_core_get(core, PSC_TYPE_FILES, NULL, &len); 704 if (buf == NULL) 705 return (NULL); 706 /* 707 * XXXMG: The code below is just copy&past from libutil. 708 * The code duplication can be avoided if libutil 709 * is extended to provide something like: 710 * struct kinfo_file *kinfo_getfile_from_buf(const char *buf, 711 * size_t len, int *cntp); 712 */ 713 714 /* Pass 1: count items */ 715 cnt = 0; 716 bp = buf; 717 eb = buf + len; 718 while (bp < eb) { 719 kf = (struct kinfo_file *)(uintptr_t)bp; 720 bp += kf->kf_structsize; 721 cnt++; 722 } 723 724 kif = calloc(cnt, sizeof(*kif)); 725 if (kif == NULL) { 726 free(buf); 727 return (NULL); 728 } 729 bp = buf; 730 eb = buf + len; 731 kp = kif; 732 /* Pass 2: unpack */ 733 while (bp < eb) { 734 kf = (struct kinfo_file *)(uintptr_t)bp; 735 /* Copy/expand into pre-zeroed buffer */ 736 memcpy(kp, kf, kf->kf_structsize); 737 /* Advance to next packed record */ 738 bp += kf->kf_structsize; 739 /* Set field size to fixed length, advance */ 740 kp->kf_structsize = sizeof(*kp); 741 kp++; 742 } 743 free(buf); 744 *cntp = cnt; 745 return (kif); /* Caller must free() return value */ 746} 747 748static struct filestat_list * 749procstat_getfiles_sysctl(struct procstat *procstat, struct kinfo_proc *kp, 750 int mmapped) 751{ 752 struct kinfo_file *kif, *files; 753 struct kinfo_vmentry *kve, *vmentries; 754 struct filestat_list *head; 755 struct filestat *entry; 756 char *path; 757 off_t offset; 758 int cnt, fd, fflags; 759 int i, type, uflags; 760 int refcount; 761 cap_rights_t cap_rights; 762 763 assert(kp); 764 if (kp->ki_fd == NULL) 765 return (NULL); 766 switch(procstat->type) { 767 case PROCSTAT_SYSCTL: 768 files = kinfo_getfile(kp->ki_pid, &cnt); 769 break; 770 case PROCSTAT_CORE: 771 files = kinfo_getfile_core(procstat->core, &cnt); 772 break; 773 default: 774 assert(!"invalid type"); 775 } 776 if (files == NULL && errno != EPERM) { 777 warn("kinfo_getfile()"); 778 return (NULL); 779 } 780 procstat->files = files; 781 782 /* 783 * Allocate list head. 784 */ 785 head = malloc(sizeof(*head)); 786 if (head == NULL) 787 return (NULL); 788 STAILQ_INIT(head); 789 for (i = 0; i < cnt; i++) { 790 kif = &files[i]; 791 792 type = kinfo_type2fst(kif->kf_type); 793 fd = kif->kf_fd >= 0 ? kif->kf_fd : -1; 794 fflags = kinfo_fflags2fst(kif->kf_flags); 795 uflags = kinfo_uflags2fst(kif->kf_fd); 796 refcount = kif->kf_ref_count; 797 offset = kif->kf_offset; 798 if (*kif->kf_path != '\0') 799 path = strdup(kif->kf_path); 800 else 801 path = NULL; 802 cap_rights = kif->kf_cap_rights; 803 804 /* 805 * Create filestat entry. 806 */ 807 entry = filestat_new_entry(kif, type, fd, fflags, uflags, 808 refcount, offset, path, cap_rights); 809 if (entry != NULL) 810 STAILQ_INSERT_TAIL(head, entry, next); 811 } 812 if (mmapped != 0) { 813 vmentries = procstat_getvmmap(procstat, kp, &cnt); 814 procstat->vmentries = vmentries; 815 if (vmentries == NULL || cnt == 0) 816 goto fail; 817 for (i = 0; i < cnt; i++) { 818 kve = &vmentries[i]; 819 if (kve->kve_type != KVME_TYPE_VNODE) 820 continue; 821 fflags = 0; 822 if (kve->kve_protection & KVME_PROT_READ) 823 fflags = PS_FST_FFLAG_READ; 824 if ((kve->kve_flags & KVME_FLAG_COW) == 0 && 825 kve->kve_protection & KVME_PROT_WRITE) 826 fflags |= PS_FST_FFLAG_WRITE; 827 offset = kve->kve_offset; 828 refcount = kve->kve_ref_count; 829 if (*kve->kve_path != '\0') 830 path = strdup(kve->kve_path); 831 else 832 path = NULL; 833 entry = filestat_new_entry(kve, PS_FST_TYPE_VNODE, -1, 834 fflags, PS_FST_UFLAG_MMAP, refcount, offset, path, 835 0); 836 if (entry != NULL) 837 STAILQ_INSERT_TAIL(head, entry, next); 838 } 839 } 840fail: 841 return (head); 842} 843 844int 845procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst, 846 struct pipestat *ps, char *errbuf) 847{ 848 849 assert(ps); 850 if (procstat->type == PROCSTAT_KVM) { 851 return (procstat_get_pipe_info_kvm(procstat->kd, fst, ps, 852 errbuf)); 853 } else if (procstat->type == PROCSTAT_SYSCTL || 854 procstat->type == PROCSTAT_CORE) { 855 return (procstat_get_pipe_info_sysctl(fst, ps, errbuf)); 856 } else { 857 warnx("unknown access method: %d", procstat->type); 858 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 859 return (1); 860 } 861} 862 863static int 864procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst, 865 struct pipestat *ps, char *errbuf) 866{ 867 struct pipe pi; 868 void *pipep; 869 870 assert(kd); 871 assert(ps); 872 assert(fst); 873 bzero(ps, sizeof(*ps)); 874 pipep = fst->fs_typedep; 875 if (pipep == NULL) 876 goto fail; 877 if (!kvm_read_all(kd, (unsigned long)pipep, &pi, sizeof(struct pipe))) { 878 warnx("can't read pipe at %p", (void *)pipep); 879 goto fail; 880 } 881 ps->addr = (uintptr_t)pipep; 882 ps->peer = (uintptr_t)pi.pipe_peer; 883 ps->buffer_cnt = pi.pipe_buffer.cnt; 884 return (0); 885 886fail: 887 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 888 return (1); 889} 890 891static int 892procstat_get_pipe_info_sysctl(struct filestat *fst, struct pipestat *ps, 893 char *errbuf __unused) 894{ 895 struct kinfo_file *kif; 896 897 assert(ps); 898 assert(fst); 899 bzero(ps, sizeof(*ps)); 900 kif = fst->fs_typedep; 901 if (kif == NULL) 902 return (1); 903 ps->addr = kif->kf_un.kf_pipe.kf_pipe_addr; 904 ps->peer = kif->kf_un.kf_pipe.kf_pipe_peer; 905 ps->buffer_cnt = kif->kf_un.kf_pipe.kf_pipe_buffer_cnt; 906 return (0); 907} 908 909int 910procstat_get_pts_info(struct procstat *procstat, struct filestat *fst, 911 struct ptsstat *pts, char *errbuf) 912{ 913 914 assert(pts); 915 if (procstat->type == PROCSTAT_KVM) { 916 return (procstat_get_pts_info_kvm(procstat->kd, fst, pts, 917 errbuf)); 918 } else if (procstat->type == PROCSTAT_SYSCTL || 919 procstat->type == PROCSTAT_CORE) { 920 return (procstat_get_pts_info_sysctl(fst, pts, errbuf)); 921 } else { 922 warnx("unknown access method: %d", procstat->type); 923 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 924 return (1); 925 } 926} 927 928static int 929procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst, 930 struct ptsstat *pts, char *errbuf) 931{ 932 struct tty tty; 933 void *ttyp; 934 935 assert(kd); 936 assert(pts); 937 assert(fst); 938 bzero(pts, sizeof(*pts)); 939 ttyp = fst->fs_typedep; 940 if (ttyp == NULL) 941 goto fail; 942 if (!kvm_read_all(kd, (unsigned long)ttyp, &tty, sizeof(struct tty))) { 943 warnx("can't read tty at %p", (void *)ttyp); 944 goto fail; 945 } 946 pts->dev = dev2udev(kd, tty.t_dev); 947 (void)kdevtoname(kd, tty.t_dev, pts->devname); 948 return (0); 949 950fail: 951 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 952 return (1); 953} 954 955static int 956procstat_get_pts_info_sysctl(struct filestat *fst, struct ptsstat *pts, 957 char *errbuf __unused) 958{ 959 struct kinfo_file *kif; 960 961 assert(pts); 962 assert(fst); 963 bzero(pts, sizeof(*pts)); 964 kif = fst->fs_typedep; 965 if (kif == NULL) 966 return (0); 967 pts->dev = kif->kf_un.kf_pts.kf_pts_dev; 968 strlcpy(pts->devname, kif->kf_path, sizeof(pts->devname)); 969 return (0); 970} 971 972int 973procstat_get_shm_info(struct procstat *procstat, struct filestat *fst, 974 struct shmstat *shm, char *errbuf) 975{ 976 977 assert(shm); 978 if (procstat->type == PROCSTAT_KVM) { 979 return (procstat_get_shm_info_kvm(procstat->kd, fst, shm, 980 errbuf)); 981 } else if (procstat->type == PROCSTAT_SYSCTL || 982 procstat->type == PROCSTAT_CORE) { 983 return (procstat_get_shm_info_sysctl(fst, shm, errbuf)); 984 } else { 985 warnx("unknown access method: %d", procstat->type); 986 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 987 return (1); 988 } 989} 990 991static int 992procstat_get_shm_info_kvm(kvm_t *kd, struct filestat *fst, 993 struct shmstat *shm, char *errbuf) 994{ 995 struct shmfd shmfd; 996 void *shmfdp; 997 char *path; 998 int i; 999 1000 assert(kd); 1001 assert(shm); 1002 assert(fst); 1003 bzero(shm, sizeof(*shm)); 1004 shmfdp = fst->fs_typedep; 1005 if (shmfdp == NULL) 1006 goto fail; 1007 if (!kvm_read_all(kd, (unsigned long)shmfdp, &shmfd, 1008 sizeof(struct shmfd))) { 1009 warnx("can't read shmfd at %p", (void *)shmfdp); 1010 goto fail; 1011 } 1012 shm->mode = S_IFREG | shmfd.shm_mode; 1013 shm->size = shmfd.shm_size; 1014 if (fst->fs_path == NULL && shmfd.shm_path != NULL) { 1015 path = malloc(MAXPATHLEN); 1016 for (i = 0; i < MAXPATHLEN - 1; i++) { 1017 if (!kvm_read_all(kd, (unsigned long)shmfd.shm_path + i, 1018 path + i, 1)) 1019 break; 1020 if (path[i] == '\0') 1021 break; 1022 } 1023 path[i] = '\0'; 1024 if (i == 0) 1025 free(path); 1026 else 1027 fst->fs_path = path; 1028 } 1029 return (0); 1030 1031fail: 1032 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 1033 return (1); 1034} 1035 1036static int 1037procstat_get_shm_info_sysctl(struct filestat *fst, struct shmstat *shm, 1038 char *errbuf __unused) 1039{ 1040 struct kinfo_file *kif; 1041 1042 assert(shm); 1043 assert(fst); 1044 bzero(shm, sizeof(*shm)); 1045 kif = fst->fs_typedep; 1046 if (kif == NULL) 1047 return (0); 1048 shm->size = kif->kf_un.kf_file.kf_file_size; 1049 shm->mode = kif->kf_un.kf_file.kf_file_mode; 1050 return (0); 1051} 1052 1053int 1054procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst, 1055 struct vnstat *vn, char *errbuf) 1056{ 1057 1058 assert(vn); 1059 if (procstat->type == PROCSTAT_KVM) { 1060 return (procstat_get_vnode_info_kvm(procstat->kd, fst, vn, 1061 errbuf)); 1062 } else if (procstat->type == PROCSTAT_SYSCTL || 1063 procstat->type == PROCSTAT_CORE) { 1064 return (procstat_get_vnode_info_sysctl(fst, vn, errbuf)); 1065 } else { 1066 warnx("unknown access method: %d", procstat->type); 1067 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 1068 return (1); 1069 } 1070} 1071 1072static int 1073procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst, 1074 struct vnstat *vn, char *errbuf) 1075{ 1076 /* Filesystem specific handlers. */ 1077 #define FSTYPE(fst) {#fst, fst##_filestat} 1078 struct { 1079 const char *tag; 1080 int (*handler)(kvm_t *kd, struct vnode *vp, 1081 struct vnstat *vn); 1082 } fstypes[] = { 1083 FSTYPE(devfs), 1084 FSTYPE(isofs), 1085 FSTYPE(msdosfs), 1086 FSTYPE(nfs), 1087 FSTYPE(udf), 1088 FSTYPE(ufs), 1089#ifdef LIBPROCSTAT_ZFS 1090 FSTYPE(zfs), 1091#endif 1092 }; 1093#define NTYPES (sizeof(fstypes) / sizeof(*fstypes)) 1094 struct vnode vnode; 1095 char tagstr[12]; 1096 void *vp; 1097 int error, found; 1098 unsigned int i; 1099 1100 assert(kd); 1101 assert(vn); 1102 assert(fst); 1103 vp = fst->fs_typedep; 1104 if (vp == NULL) 1105 goto fail; 1106 error = kvm_read_all(kd, (unsigned long)vp, &vnode, sizeof(vnode)); 1107 if (error == 0) { 1108 warnx("can't read vnode at %p", (void *)vp); 1109 goto fail; 1110 } 1111 bzero(vn, sizeof(*vn)); 1112 vn->vn_type = vntype2psfsttype(vnode.v_type); 1113 if (vnode.v_type == VNON || vnode.v_type == VBAD) 1114 return (0); 1115 error = kvm_read_all(kd, (unsigned long)vnode.v_tag, tagstr, 1116 sizeof(tagstr)); 1117 if (error == 0) { 1118 warnx("can't read v_tag at %p", (void *)vp); 1119 goto fail; 1120 } 1121 tagstr[sizeof(tagstr) - 1] = '\0'; 1122 1123 /* 1124 * Find appropriate handler. 1125 */ 1126 for (i = 0, found = 0; i < NTYPES; i++) 1127 if (!strcmp(fstypes[i].tag, tagstr)) { 1128 if (fstypes[i].handler(kd, &vnode, vn) != 0) { 1129 goto fail; 1130 } 1131 break; 1132 } 1133 if (i == NTYPES) { 1134 snprintf(errbuf, _POSIX2_LINE_MAX, "?(%s)", tagstr); 1135 return (1); 1136 } 1137 vn->vn_mntdir = getmnton(kd, vnode.v_mount); 1138 if ((vnode.v_type == VBLK || vnode.v_type == VCHR) && 1139 vnode.v_rdev != NULL){ 1140 vn->vn_dev = dev2udev(kd, vnode.v_rdev); 1141 (void)kdevtoname(kd, vnode.v_rdev, vn->vn_devname); 1142 } else { 1143 vn->vn_dev = -1; 1144 } 1145 return (0); 1146 1147fail: 1148 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 1149 return (1); 1150} 1151 1152/* 1153 * kinfo vnode type to filestat translation. 1154 */ 1155static int 1156kinfo_vtype2fst(int kfvtype) 1157{ 1158 static struct { 1159 int kf_vtype; 1160 int fst_vtype; 1161 } kfvtypes2fst[] = { 1162 { KF_VTYPE_VBAD, PS_FST_VTYPE_VBAD }, 1163 { KF_VTYPE_VBLK, PS_FST_VTYPE_VBLK }, 1164 { KF_VTYPE_VCHR, PS_FST_VTYPE_VCHR }, 1165 { KF_VTYPE_VDIR, PS_FST_VTYPE_VDIR }, 1166 { KF_VTYPE_VFIFO, PS_FST_VTYPE_VFIFO }, 1167 { KF_VTYPE_VLNK, PS_FST_VTYPE_VLNK }, 1168 { KF_VTYPE_VNON, PS_FST_VTYPE_VNON }, 1169 { KF_VTYPE_VREG, PS_FST_VTYPE_VREG }, 1170 { KF_VTYPE_VSOCK, PS_FST_VTYPE_VSOCK } 1171 }; 1172#define NKFVTYPES (sizeof(kfvtypes2fst) / sizeof(*kfvtypes2fst)) 1173 unsigned int i; 1174 1175 for (i = 0; i < NKFVTYPES; i++) 1176 if (kfvtypes2fst[i].kf_vtype == kfvtype) 1177 break; 1178 if (i == NKFVTYPES) 1179 return (PS_FST_VTYPE_UNKNOWN); 1180 return (kfvtypes2fst[i].fst_vtype); 1181} 1182 1183static int 1184procstat_get_vnode_info_sysctl(struct filestat *fst, struct vnstat *vn, 1185 char *errbuf) 1186{ 1187 struct statfs stbuf; 1188 struct kinfo_file *kif; 1189 struct kinfo_vmentry *kve; 1190 uint64_t fileid; 1191 uint64_t size; 1192 char *name, *path; 1193 uint32_t fsid; 1194 uint16_t mode; 1195 uint32_t rdev; 1196 int vntype; 1197 int status; 1198 1199 assert(fst); 1200 assert(vn); 1201 bzero(vn, sizeof(*vn)); 1202 if (fst->fs_typedep == NULL) 1203 return (1); 1204 if (fst->fs_uflags & PS_FST_UFLAG_MMAP) { 1205 kve = fst->fs_typedep; 1206 fileid = kve->kve_vn_fileid; 1207 fsid = kve->kve_vn_fsid; 1208 mode = kve->kve_vn_mode; 1209 path = kve->kve_path; 1210 rdev = kve->kve_vn_rdev; 1211 size = kve->kve_vn_size; 1212 vntype = kinfo_vtype2fst(kve->kve_vn_type); 1213 status = kve->kve_status; 1214 } else { 1215 kif = fst->fs_typedep; 1216 fileid = kif->kf_un.kf_file.kf_file_fileid; 1217 fsid = kif->kf_un.kf_file.kf_file_fsid; 1218 mode = kif->kf_un.kf_file.kf_file_mode; 1219 path = kif->kf_path; 1220 rdev = kif->kf_un.kf_file.kf_file_rdev; 1221 size = kif->kf_un.kf_file.kf_file_size; 1222 vntype = kinfo_vtype2fst(kif->kf_vnode_type); 1223 status = kif->kf_status; 1224 } 1225 vn->vn_type = vntype; 1226 if (vntype == PS_FST_VTYPE_VNON || vntype == PS_FST_VTYPE_VBAD) 1227 return (0); 1228 if ((status & KF_ATTR_VALID) == 0) { 1229 snprintf(errbuf, _POSIX2_LINE_MAX, "? (no info available)"); 1230 return (1); 1231 } 1232 if (path && *path) { 1233 statfs(path, &stbuf); 1234 vn->vn_mntdir = strdup(stbuf.f_mntonname); 1235 } else 1236 vn->vn_mntdir = strdup("-"); 1237 vn->vn_dev = rdev; 1238 if (vntype == PS_FST_VTYPE_VBLK) { 1239 name = devname(rdev, S_IFBLK); 1240 if (name != NULL) 1241 strlcpy(vn->vn_devname, name, 1242 sizeof(vn->vn_devname)); 1243 } else if (vntype == PS_FST_VTYPE_VCHR) { 1244 name = devname(vn->vn_dev, S_IFCHR); 1245 if (name != NULL) 1246 strlcpy(vn->vn_devname, name, 1247 sizeof(vn->vn_devname)); 1248 } 1249 vn->vn_fsid = fsid; 1250 vn->vn_fileid = fileid; 1251 vn->vn_size = size; 1252 vn->vn_mode = mode; 1253 return (0); 1254} 1255 1256int 1257procstat_get_socket_info(struct procstat *procstat, struct filestat *fst, 1258 struct sockstat *sock, char *errbuf) 1259{ 1260 1261 assert(sock); 1262 if (procstat->type == PROCSTAT_KVM) { 1263 return (procstat_get_socket_info_kvm(procstat->kd, fst, sock, 1264 errbuf)); 1265 } else if (procstat->type == PROCSTAT_SYSCTL || 1266 procstat->type == PROCSTAT_CORE) { 1267 return (procstat_get_socket_info_sysctl(fst, sock, errbuf)); 1268 } else { 1269 warnx("unknown access method: %d", procstat->type); 1270 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 1271 return (1); 1272 } 1273} 1274 1275static int 1276procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst, 1277 struct sockstat *sock, char *errbuf) 1278{ 1279 struct domain dom; 1280 struct inpcb inpcb; 1281 struct protosw proto; 1282 struct socket s; 1283 struct unpcb unpcb; 1284 ssize_t len; 1285 void *so; 1286 1287 assert(kd); 1288 assert(sock); 1289 assert(fst); 1290 bzero(sock, sizeof(*sock)); 1291 so = fst->fs_typedep; 1292 if (so == NULL) 1293 goto fail; 1294 sock->so_addr = (uintptr_t)so; 1295 /* fill in socket */ 1296 if (!kvm_read_all(kd, (unsigned long)so, &s, 1297 sizeof(struct socket))) { 1298 warnx("can't read sock at %p", (void *)so); 1299 goto fail; 1300 } 1301 /* fill in protosw entry */ 1302 if (!kvm_read_all(kd, (unsigned long)s.so_proto, &proto, 1303 sizeof(struct protosw))) { 1304 warnx("can't read protosw at %p", (void *)s.so_proto); 1305 goto fail; 1306 } 1307 /* fill in domain */ 1308 if (!kvm_read_all(kd, (unsigned long)proto.pr_domain, &dom, 1309 sizeof(struct domain))) { 1310 warnx("can't read domain at %p", 1311 (void *)proto.pr_domain); 1312 goto fail; 1313 } 1314 if ((len = kvm_read(kd, (unsigned long)dom.dom_name, sock->dname, 1315 sizeof(sock->dname) - 1)) < 0) { 1316 warnx("can't read domain name at %p", (void *)dom.dom_name); 1317 sock->dname[0] = '\0'; 1318 } 1319 else 1320 sock->dname[len] = '\0'; 1321 1322 /* 1323 * Fill in known data. 1324 */ 1325 sock->type = s.so_type; 1326 sock->proto = proto.pr_protocol; 1327 sock->dom_family = dom.dom_family; 1328 sock->so_pcb = (uintptr_t)s.so_pcb; 1329 1330 /* 1331 * Protocol specific data. 1332 */ 1333 switch(dom.dom_family) { 1334 case AF_INET: 1335 case AF_INET6: 1336 if (proto.pr_protocol == IPPROTO_TCP) { 1337 if (s.so_pcb) { 1338 if (kvm_read(kd, (u_long)s.so_pcb, 1339 (char *)&inpcb, sizeof(struct inpcb)) 1340 != sizeof(struct inpcb)) { 1341 warnx("can't read inpcb at %p", 1342 (void *)s.so_pcb); 1343 } else 1344 sock->inp_ppcb = 1345 (uintptr_t)inpcb.inp_ppcb; 1346 } 1347 } 1348 break; 1349 case AF_UNIX: 1350 if (s.so_pcb) { 1351 if (kvm_read(kd, (u_long)s.so_pcb, (char *)&unpcb, 1352 sizeof(struct unpcb)) != sizeof(struct unpcb)){ 1353 warnx("can't read unpcb at %p", 1354 (void *)s.so_pcb); 1355 } else if (unpcb.unp_conn) { 1356 sock->so_rcv_sb_state = s.so_rcv.sb_state; 1357 sock->so_snd_sb_state = s.so_snd.sb_state; 1358 sock->unp_conn = (uintptr_t)unpcb.unp_conn; 1359 } 1360 } 1361 break; 1362 default: 1363 break; 1364 } 1365 return (0); 1366 1367fail: 1368 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 1369 return (1); 1370} 1371 1372static int 1373procstat_get_socket_info_sysctl(struct filestat *fst, struct sockstat *sock, 1374 char *errbuf __unused) 1375{ 1376 struct kinfo_file *kif; 1377 1378 assert(sock); 1379 assert(fst); 1380 bzero(sock, sizeof(*sock)); 1381 kif = fst->fs_typedep; 1382 if (kif == NULL) 1383 return (0); 1384 1385 /* 1386 * Fill in known data. 1387 */ 1388 sock->type = kif->kf_sock_type; 1389 sock->proto = kif->kf_sock_protocol; 1390 sock->dom_family = kif->kf_sock_domain; 1391 sock->so_pcb = kif->kf_un.kf_sock.kf_sock_pcb; 1392 strlcpy(sock->dname, kif->kf_path, sizeof(sock->dname)); 1393 bcopy(&kif->kf_sa_local, &sock->sa_local, kif->kf_sa_local.ss_len); 1394 bcopy(&kif->kf_sa_peer, &sock->sa_peer, kif->kf_sa_peer.ss_len); 1395 1396 /* 1397 * Protocol specific data. 1398 */ 1399 switch(sock->dom_family) { 1400 case AF_INET: 1401 case AF_INET6: 1402 if (sock->proto == IPPROTO_TCP) 1403 sock->inp_ppcb = kif->kf_un.kf_sock.kf_sock_inpcb; 1404 break; 1405 case AF_UNIX: 1406 if (kif->kf_un.kf_sock.kf_sock_unpconn != 0) { 1407 sock->so_rcv_sb_state = 1408 kif->kf_un.kf_sock.kf_sock_rcv_sb_state; 1409 sock->so_snd_sb_state = 1410 kif->kf_un.kf_sock.kf_sock_snd_sb_state; 1411 sock->unp_conn = 1412 kif->kf_un.kf_sock.kf_sock_unpconn; 1413 } 1414 break; 1415 default: 1416 break; 1417 } 1418 return (0); 1419} 1420 1421/* 1422 * Descriptor flags to filestat translation. 1423 */ 1424static int 1425to_filestat_flags(int flags) 1426{ 1427 static struct { 1428 int flag; 1429 int fst_flag; 1430 } fstflags[] = { 1431 { FREAD, PS_FST_FFLAG_READ }, 1432 { FWRITE, PS_FST_FFLAG_WRITE }, 1433 { O_APPEND, PS_FST_FFLAG_APPEND }, 1434 { O_ASYNC, PS_FST_FFLAG_ASYNC }, 1435 { O_CREAT, PS_FST_FFLAG_CREAT }, 1436 { O_DIRECT, PS_FST_FFLAG_DIRECT }, 1437 { O_EXCL, PS_FST_FFLAG_EXCL }, 1438 { O_EXEC, PS_FST_FFLAG_EXEC }, 1439 { O_EXLOCK, PS_FST_FFLAG_EXLOCK }, 1440 { O_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW }, 1441 { O_NONBLOCK, PS_FST_FFLAG_NONBLOCK }, 1442 { O_SHLOCK, PS_FST_FFLAG_SHLOCK }, 1443 { O_SYNC, PS_FST_FFLAG_SYNC }, 1444 { O_TRUNC, PS_FST_FFLAG_TRUNC } 1445 }; 1446#define NFSTFLAGS (sizeof(fstflags) / sizeof(*fstflags)) 1447 int fst_flags; 1448 unsigned int i; 1449 1450 fst_flags = 0; 1451 for (i = 0; i < NFSTFLAGS; i++) 1452 if (flags & fstflags[i].flag) 1453 fst_flags |= fstflags[i].fst_flag; 1454 return (fst_flags); 1455} 1456 1457/* 1458 * Vnode type to filestate translation. 1459 */ 1460static int 1461vntype2psfsttype(int type) 1462{ 1463 static struct { 1464 int vtype; 1465 int fst_vtype; 1466 } vt2fst[] = { 1467 { VBAD, PS_FST_VTYPE_VBAD }, 1468 { VBLK, PS_FST_VTYPE_VBLK }, 1469 { VCHR, PS_FST_VTYPE_VCHR }, 1470 { VDIR, PS_FST_VTYPE_VDIR }, 1471 { VFIFO, PS_FST_VTYPE_VFIFO }, 1472 { VLNK, PS_FST_VTYPE_VLNK }, 1473 { VNON, PS_FST_VTYPE_VNON }, 1474 { VREG, PS_FST_VTYPE_VREG }, 1475 { VSOCK, PS_FST_VTYPE_VSOCK } 1476 }; 1477#define NVFTYPES (sizeof(vt2fst) / sizeof(*vt2fst)) 1478 unsigned int i, fst_type; 1479 1480 fst_type = PS_FST_VTYPE_UNKNOWN; 1481 for (i = 0; i < NVFTYPES; i++) { 1482 if (type == vt2fst[i].vtype) { 1483 fst_type = vt2fst[i].fst_vtype; 1484 break; 1485 } 1486 } 1487 return (fst_type); 1488} 1489 1490static char * 1491getmnton(kvm_t *kd, struct mount *m) 1492{ 1493 struct mount mnt; 1494 static struct mtab { 1495 struct mtab *next; 1496 struct mount *m; 1497 char mntonname[MNAMELEN + 1]; 1498 } *mhead = NULL; 1499 struct mtab *mt; 1500 1501 for (mt = mhead; mt != NULL; mt = mt->next) 1502 if (m == mt->m) 1503 return (mt->mntonname); 1504 if (!kvm_read_all(kd, (unsigned long)m, &mnt, sizeof(struct mount))) { 1505 warnx("can't read mount table at %p", (void *)m); 1506 return (NULL); 1507 } 1508 if ((mt = malloc(sizeof (struct mtab))) == NULL) 1509 err(1, NULL); 1510 mt->m = m; 1511 bcopy(&mnt.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); 1512 mt->mntonname[MNAMELEN] = '\0'; 1513 mt->next = mhead; 1514 mhead = mt; 1515 return (mt->mntonname); 1516} 1517 1518static struct kinfo_vmentry * 1519kinfo_getvmmap_core(struct procstat_core *core, int *cntp) 1520{ 1521 int cnt; 1522 size_t len; 1523 char *buf, *bp, *eb; 1524 struct kinfo_vmentry *kiv, *kp, *kv; 1525 1526 buf = procstat_core_get(core, PSC_TYPE_VMMAP, NULL, &len); 1527 if (buf == NULL) 1528 return (NULL); 1529 1530 /* 1531 * XXXMG: The code below is just copy&past from libutil. 1532 * The code duplication can be avoided if libutil 1533 * is extended to provide something like: 1534 * struct kinfo_vmentry *kinfo_getvmmap_from_buf(const char *buf, 1535 * size_t len, int *cntp); 1536 */ 1537 1538 /* Pass 1: count items */ 1539 cnt = 0; 1540 bp = buf; 1541 eb = buf + len; 1542 while (bp < eb) { 1543 kv = (struct kinfo_vmentry *)(uintptr_t)bp; 1544 bp += kv->kve_structsize; 1545 cnt++; 1546 } 1547 1548 kiv = calloc(cnt, sizeof(*kiv)); 1549 if (kiv == NULL) { 1550 free(buf); 1551 return (NULL); 1552 } 1553 bp = buf; 1554 eb = buf + len; 1555 kp = kiv; 1556 /* Pass 2: unpack */ 1557 while (bp < eb) { 1558 kv = (struct kinfo_vmentry *)(uintptr_t)bp; 1559 /* Copy/expand into pre-zeroed buffer */ 1560 memcpy(kp, kv, kv->kve_structsize); 1561 /* Advance to next packed record */ 1562 bp += kv->kve_structsize; 1563 /* Set field size to fixed length, advance */ 1564 kp->kve_structsize = sizeof(*kp); 1565 kp++; 1566 } 1567 free(buf); 1568 *cntp = cnt; 1569 return (kiv); /* Caller must free() return value */ 1570} 1571 1572struct kinfo_vmentry * 1573procstat_getvmmap(struct procstat *procstat, struct kinfo_proc *kp, 1574 unsigned int *cntp) 1575{ 1576 switch(procstat->type) { 1577 case PROCSTAT_KVM: 1578 warnx("kvm method is not supported"); 1579 return (NULL); 1580 case PROCSTAT_SYSCTL: 1581 return (kinfo_getvmmap(kp->ki_pid, cntp)); 1582 case PROCSTAT_CORE: 1583 return (kinfo_getvmmap_core(procstat->core, cntp)); 1584 default: 1585 warnx("unknown access method: %d", procstat->type); 1586 return (NULL); 1587 } 1588} 1589 1590void 1591procstat_freevmmap(struct procstat *procstat __unused, 1592 struct kinfo_vmentry *vmmap) 1593{ 1594 1595 free(vmmap); 1596} 1597 1598static gid_t * 1599procstat_getgroups_sysctl(pid_t pid, unsigned int *cntp) 1600{ 1601 int mib[4]; 1602 size_t len; 1603 gid_t *groups; 1604 1605 mib[0] = CTL_KERN; 1606 mib[1] = KERN_PROC; 1607 mib[2] = KERN_PROC_GROUPS; 1608 mib[3] = pid; 1609 len = (sysconf(_SC_NGROUPS_MAX) + 1) * sizeof(gid_t); 1610 groups = malloc(len); 1611 if (groups == NULL) { 1612 warn("malloc(%zu)", len); 1613 return (NULL); 1614 } 1615 if (sysctl(mib, 4, groups, &len, NULL, 0) == -1) { 1616 warn("sysctl: kern.proc.groups: %d", pid); 1617 free(groups); 1618 return (NULL); 1619 } 1620 *cntp = len / sizeof(gid_t); 1621 return (groups); 1622} 1623 1624static gid_t * 1625procstat_getgroups_core(struct procstat_core *core, unsigned int *cntp) 1626{ 1627 size_t len; 1628 gid_t *groups; 1629 1630 groups = procstat_core_get(core, PSC_TYPE_GROUPS, NULL, &len); 1631 if (groups == NULL) 1632 return (NULL); 1633 *cntp = len / sizeof(gid_t); 1634 return (groups); 1635} 1636 1637gid_t * 1638procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp, 1639 unsigned int *cntp) 1640{ 1641 switch(procstat->type) { 1642 case PROCSTAT_KVM: 1643 warnx("kvm method is not supported"); 1644 return (NULL); 1645 case PROCSTAT_SYSCTL: 1646 return (procstat_getgroups_sysctl(kp->ki_pid, cntp)); 1647 case PROCSTAT_CORE: 1648 return (procstat_getgroups_core(procstat->core, cntp)); 1649 default: 1650 warnx("unknown access method: %d", procstat->type); 1651 return (NULL); 1652 } 1653} 1654 1655void 1656procstat_freegroups(struct procstat *procstat __unused, gid_t *groups) 1657{ 1658 1659 free(groups); 1660} 1661 1662static int 1663procstat_getumask_sysctl(pid_t pid, unsigned short *maskp) 1664{ 1665 int error; 1666 int mib[4]; 1667 size_t len; 1668 1669 mib[0] = CTL_KERN; 1670 mib[1] = KERN_PROC; 1671 mib[2] = KERN_PROC_UMASK; 1672 mib[3] = pid; 1673 len = sizeof(*maskp); 1674 error = sysctl(mib, 4, maskp, &len, NULL, 0); 1675 if (error != 0 && errno != ESRCH) 1676 warn("sysctl: kern.proc.umask: %d", pid); 1677 return (error); 1678} 1679 1680static int 1681procstat_getumask_core(struct procstat_core *core, unsigned short *maskp) 1682{ 1683 size_t len; 1684 unsigned short *buf; 1685 1686 buf = procstat_core_get(core, PSC_TYPE_UMASK, NULL, &len); 1687 if (buf == NULL) 1688 return (-1); 1689 if (len < sizeof(*maskp)) { 1690 free(buf); 1691 return (-1); 1692 } 1693 *maskp = *buf; 1694 free(buf); 1695 return (0); 1696} 1697 1698int 1699procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp, 1700 unsigned short *maskp) 1701{ 1702 switch(procstat->type) { 1703 case PROCSTAT_KVM: 1704 warnx("kvm method is not supported"); 1705 return (-1); 1706 case PROCSTAT_SYSCTL: 1707 return (procstat_getumask_sysctl(kp->ki_pid, maskp)); 1708 case PROCSTAT_CORE: 1709 return (procstat_getumask_core(procstat->core, maskp)); 1710 default: 1711 warnx("unknown access method: %d", procstat->type); 1712 return (-1); 1713 } 1714} 1715