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$"); 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 100 int statfs(const char *, struct statfs *); /* XXX */ 101 102 #define PROCSTAT_KVM 1 103 #define PROCSTAT_SYSCTL 2 104 105 static char *getmnton(kvm_t *kd, struct mount *m); 106 static struct filestat_list *procstat_getfiles_kvm( 107 struct procstat *procstat, struct kinfo_proc *kp, int mmapped); 108 static struct filestat_list *procstat_getfiles_sysctl( 109 struct procstat *procstat, struct kinfo_proc *kp, int mmapped); 110 static int procstat_get_pipe_info_sysctl(struct filestat *fst, 111 struct pipestat *pipe, char *errbuf); 112 static int procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst, 113 struct pipestat *pipe, char *errbuf); 114 static int procstat_get_pts_info_sysctl(struct filestat *fst, 115 struct ptsstat *pts, char *errbuf); 116 static int procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst, 117 struct ptsstat *pts, char *errbuf); 118 static int procstat_get_shm_info_sysctl(struct filestat *fst, 119 struct shmstat *shm, char *errbuf); 120 static int procstat_get_shm_info_kvm(kvm_t *kd, struct filestat *fst, 121 struct shmstat *shm, char *errbuf); 122 static int procstat_get_socket_info_sysctl(struct filestat *fst, 123 struct sockstat *sock, char *errbuf); 124 static int procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst, 125 struct sockstat *sock, char *errbuf); 126 static int to_filestat_flags(int flags); 127 static int procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst, 128 struct vnstat *vn, char *errbuf); 129 static int procstat_get_vnode_info_sysctl(struct filestat *fst, 130 struct vnstat *vn, char *errbuf); 131 static int vntype2psfsttype(int type); 132 133 void 134 procstat_close(struct procstat *procstat) 135 { 136 137 assert(procstat); 138 if (procstat->type == PROCSTAT_KVM) 139 kvm_close(procstat->kd); 140 free(procstat); 141 } 142 143 struct procstat * 144 procstat_open_sysctl(void) 145 { 146 struct procstat *procstat; 147 148 procstat = calloc(1, sizeof(*procstat)); 149 if (procstat == NULL) { 150 warn("malloc()"); 151 return (NULL); 152 } 153 procstat->type = PROCSTAT_SYSCTL; 154 return (procstat); 155 } 156 157 struct procstat * 158 procstat_open_kvm(const char *nlistf, const char *memf) 159 { 160 struct procstat *procstat; 161 kvm_t *kd; 162 char buf[_POSIX2_LINE_MAX]; 163 164 procstat = calloc(1, sizeof(*procstat)); 165 if (procstat == NULL) { 166 warn("malloc()"); 167 return (NULL); 168 } 169 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf); 170 if (kd == NULL) { 171 warnx("kvm_openfiles(): %s", buf); 172 free(procstat); 173 return (NULL); 174 } 175 procstat->type = PROCSTAT_KVM; 176 procstat->kd = kd; 177 return (procstat); 178 } 179 180 struct kinfo_proc * 181 procstat_getprocs(struct procstat *procstat, int what, int arg, 182 unsigned int *count) 183 { 184 struct kinfo_proc *p0, *p; 185 size_t len; 186 int name[4]; 187 int error; 188 189 assert(procstat); 190 assert(count); 191 p = NULL; 192 if (procstat->type == PROCSTAT_KVM) { 193 p0 = kvm_getprocs(procstat->kd, what, arg, count); 194 if (p0 == NULL || count == 0) 195 return (NULL); 196 len = *count * sizeof(*p); 197 p = malloc(len); 198 if (p == NULL) { 199 warnx("malloc(%zu)", len); 200 goto fail; 201 } 202 bcopy(p0, p, len); 203 return (p); 204 } else if (procstat->type == PROCSTAT_SYSCTL) { 205 len = 0; 206 name[0] = CTL_KERN; 207 name[1] = KERN_PROC; 208 name[2] = what; 209 name[3] = arg; 210 error = sysctl(name, 4, NULL, &len, NULL, 0); 211 if (error < 0 && errno != EPERM) { 212 warn("sysctl(kern.proc)"); 213 goto fail; 214 } 215 if (len == 0) { 216 warnx("no processes?"); 217 goto fail; 218 } 219 p = malloc(len); 220 if (p == NULL) { 221 warnx("malloc(%zu)", len); 222 goto fail; 223 } 224 error = sysctl(name, 4, p, &len, NULL, 0); 225 if (error < 0 && errno != EPERM) { 226 warn("sysctl(kern.proc)"); 227 goto fail; 228 } 229 /* Perform simple consistency checks. */ 230 if ((len % sizeof(*p)) != 0 || p->ki_structsize != sizeof(*p)) { 231 warnx("kinfo_proc structure size mismatch"); 232 goto fail; 233 } 234 *count = len / sizeof(*p); 235 return (p); 236 } else { 237 warnx("unknown access method: %d", procstat->type); 238 return (NULL); 239 } 240 fail: 241 if (p) 242 free(p); 243 return (NULL); 244 } 245 246 void 247 procstat_freeprocs(struct procstat *procstat __unused, struct kinfo_proc *p) 248 { 249 250 if (p != NULL) 251 free(p); 252 p = NULL; 253 } 254 255 struct filestat_list * 256 procstat_getfiles(struct procstat *procstat, struct kinfo_proc *kp, int mmapped) 257 { 258 259 if (procstat->type == PROCSTAT_SYSCTL) 260 return (procstat_getfiles_sysctl(procstat, kp, mmapped)); 261 else if (procstat->type == PROCSTAT_KVM) 262 return (procstat_getfiles_kvm(procstat, kp, mmapped)); 263 else 264 return (NULL); 265 } 266 267 void 268 procstat_freefiles(struct procstat *procstat, struct filestat_list *head) 269 { 270 struct filestat *fst, *tmp; 271 272 STAILQ_FOREACH_SAFE(fst, head, next, tmp) { 273 if (fst->fs_path != NULL) 274 free(fst->fs_path); 275 free(fst); 276 } 277 free(head); 278 if (procstat->vmentries != NULL) { 279 free(procstat->vmentries); 280 procstat->vmentries = NULL; 281 } 282 if (procstat->files != NULL) { 283 free(procstat->files); 284 procstat->files = NULL; 285 } 286 } 287 288 static struct filestat * 289 filestat_new_entry(void *typedep, int type, int fd, int fflags, int uflags, 290 int refcount, off_t offset, char *path, cap_rights_t cap_rights) 291 { 292 struct filestat *entry; 293 294 entry = calloc(1, sizeof(*entry)); 295 if (entry == NULL) { 296 warn("malloc()"); 297 return (NULL); 298 } 299 entry->fs_typedep = typedep; 300 entry->fs_fflags = fflags; 301 entry->fs_uflags = uflags; 302 entry->fs_fd = fd; 303 entry->fs_type = type; 304 entry->fs_ref_count = refcount; 305 entry->fs_offset = offset; 306 entry->fs_path = path; 307 entry->fs_cap_rights = cap_rights; 308 return (entry); 309 } 310 311 static struct vnode * 312 getctty(kvm_t *kd, struct kinfo_proc *kp) 313 { 314 struct pgrp pgrp; 315 struct proc proc; 316 struct session sess; 317 int error; 318 319 assert(kp); 320 error = kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc, 321 sizeof(proc)); 322 if (error == 0) { 323 warnx("can't read proc struct at %p for pid %d", 324 kp->ki_paddr, kp->ki_pid); 325 return (NULL); 326 } 327 if (proc.p_pgrp == NULL) 328 return (NULL); 329 error = kvm_read_all(kd, (unsigned long)proc.p_pgrp, &pgrp, 330 sizeof(pgrp)); 331 if (error == 0) { 332 warnx("can't read pgrp struct at %p for pid %d", 333 proc.p_pgrp, kp->ki_pid); 334 return (NULL); 335 } 336 error = kvm_read_all(kd, (unsigned long)pgrp.pg_session, &sess, 337 sizeof(sess)); 338 if (error == 0) { 339 warnx("can't read session struct at %p for pid %d", 340 pgrp.pg_session, kp->ki_pid); 341 return (NULL); 342 } 343 return (sess.s_ttyvp); 344 } 345 346 static struct filestat_list * 347 procstat_getfiles_kvm(struct procstat *procstat, struct kinfo_proc *kp, int mmapped) 348 { 349 struct file file; 350 struct filedesc filed; 351 struct vm_map_entry vmentry; 352 struct vm_object object; 353 struct vmspace vmspace; 354 vm_map_entry_t entryp; 355 vm_map_t map; 356 vm_object_t objp; 357 struct vnode *vp; 358 struct file **ofiles; 359 struct filestat *entry; 360 struct filestat_list *head; 361 kvm_t *kd; 362 void *data; 363 int i, fflags; 364 int prot, type; 365 unsigned int nfiles; 366 367 assert(procstat); 368 kd = procstat->kd; 369 if (kd == NULL) 370 return (NULL); 371 if (kp->ki_fd == NULL) 372 return (NULL); 373 if (!kvm_read_all(kd, (unsigned long)kp->ki_fd, &filed, 374 sizeof(filed))) { 375 warnx("can't read filedesc at %p", (void *)kp->ki_fd); 376 return (NULL); 377 } 378 379 /* 380 * Allocate list head. 381 */ 382 head = malloc(sizeof(*head)); 383 if (head == NULL) 384 return (NULL); 385 STAILQ_INIT(head); 386 387 /* root directory vnode, if one. */ 388 if (filed.fd_rdir) { 389 entry = filestat_new_entry(filed.fd_rdir, PS_FST_TYPE_VNODE, -1, 390 PS_FST_FFLAG_READ, PS_FST_UFLAG_RDIR, 0, 0, NULL, 0); 391 if (entry != NULL) 392 STAILQ_INSERT_TAIL(head, entry, next); 393 } 394 /* current working directory vnode. */ 395 if (filed.fd_cdir) { 396 entry = filestat_new_entry(filed.fd_cdir, PS_FST_TYPE_VNODE, -1, 397 PS_FST_FFLAG_READ, PS_FST_UFLAG_CDIR, 0, 0, NULL, 0); 398 if (entry != NULL) 399 STAILQ_INSERT_TAIL(head, entry, next); 400 } 401 /* jail root, if any. */ 402 if (filed.fd_jdir) { 403 entry = filestat_new_entry(filed.fd_jdir, PS_FST_TYPE_VNODE, -1, 404 PS_FST_FFLAG_READ, PS_FST_UFLAG_JAIL, 0, 0, NULL, 0); 405 if (entry != NULL) 406 STAILQ_INSERT_TAIL(head, entry, next); 407 } 408 /* ktrace vnode, if one */ 409 if (kp->ki_tracep) { 410 entry = filestat_new_entry(kp->ki_tracep, PS_FST_TYPE_VNODE, -1, 411 PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE, 412 PS_FST_UFLAG_TRACE, 0, 0, NULL, 0); 413 if (entry != NULL) 414 STAILQ_INSERT_TAIL(head, entry, next); 415 } 416 /* text vnode, if one */ 417 if (kp->ki_textvp) { 418 entry = filestat_new_entry(kp->ki_textvp, PS_FST_TYPE_VNODE, -1, 419 PS_FST_FFLAG_READ, PS_FST_UFLAG_TEXT, 0, 0, NULL, 0); 420 if (entry != NULL) 421 STAILQ_INSERT_TAIL(head, entry, next); 422 } 423 /* Controlling terminal. */ 424 if ((vp = getctty(kd, kp)) != NULL) { 425 entry = filestat_new_entry(vp, PS_FST_TYPE_VNODE, -1, 426 PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE, 427 PS_FST_UFLAG_CTTY, 0, 0, NULL, 0); 428 if (entry != NULL) 429 STAILQ_INSERT_TAIL(head, entry, next); 430 } 431 432 nfiles = filed.fd_lastfile + 1; 433 ofiles = malloc(nfiles * sizeof(struct file *)); 434 if (ofiles == NULL) { 435 warn("malloc(%zu)", nfiles * sizeof(struct file *)); 436 goto do_mmapped; 437 } 438 if (!kvm_read_all(kd, (unsigned long)filed.fd_ofiles, ofiles, 439 nfiles * sizeof(struct file *))) { 440 warnx("cannot read file structures at %p", 441 (void *)filed.fd_ofiles); 442 free(ofiles); 443 goto do_mmapped; 444 } 445 for (i = 0; i <= filed.fd_lastfile; i++) { 446 if (ofiles[i] == NULL) 447 continue; 448 if (!kvm_read_all(kd, (unsigned long)ofiles[i], &file, 449 sizeof(struct file))) { 450 warnx("can't read file %d at %p", i, 451 (void *)ofiles[i]); 452 continue; 453 } 454 switch (file.f_type) { 455 case DTYPE_VNODE: 456 type = PS_FST_TYPE_VNODE; 457 data = file.f_vnode; 458 break; 459 case DTYPE_SOCKET: 460 type = PS_FST_TYPE_SOCKET; 461 data = file.f_data; 462 break; 463 case DTYPE_PIPE: 464 type = PS_FST_TYPE_PIPE; 465 data = file.f_data; 466 break; 467 case DTYPE_FIFO: 468 type = PS_FST_TYPE_FIFO; 469 data = file.f_vnode; 470 break; 471 #ifdef DTYPE_PTS 472 case DTYPE_PTS: 473 type = PS_FST_TYPE_PTS; 474 data = file.f_data; 475 break; 476 #endif 477 case DTYPE_SHM: 478 type = PS_FST_TYPE_SHM; 479 data = file.f_data; 480 break; 481 default: 482 continue; 483 } 484 /* XXXRW: No capability rights support for kvm yet. */ 485 entry = filestat_new_entry(data, type, i, 486 to_filestat_flags(file.f_flag), 0, 0, 0, NULL, 0); 487 if (entry != NULL) 488 STAILQ_INSERT_TAIL(head, entry, next); 489 } 490 free(ofiles); 491 492 do_mmapped: 493 494 /* 495 * Process mmapped files if requested. 496 */ 497 if (mmapped) { 498 if (!kvm_read_all(kd, (unsigned long)kp->ki_vmspace, &vmspace, 499 sizeof(vmspace))) { 500 warnx("can't read vmspace at %p", 501 (void *)kp->ki_vmspace); 502 goto exit; 503 } 504 map = &vmspace.vm_map; 505 506 for (entryp = map->header.next; 507 entryp != &kp->ki_vmspace->vm_map.header; 508 entryp = vmentry.next) { 509 if (!kvm_read_all(kd, (unsigned long)entryp, &vmentry, 510 sizeof(vmentry))) { 511 warnx("can't read vm_map_entry at %p", 512 (void *)entryp); 513 continue; 514 } 515 if (vmentry.eflags & MAP_ENTRY_IS_SUB_MAP) 516 continue; 517 if ((objp = vmentry.object.vm_object) == NULL) 518 continue; 519 for (; objp; objp = object.backing_object) { 520 if (!kvm_read_all(kd, (unsigned long)objp, 521 &object, sizeof(object))) { 522 warnx("can't read vm_object at %p", 523 (void *)objp); 524 break; 525 } 526 } 527 528 /* We want only vnode objects. */ 529 if (object.type != OBJT_VNODE) 530 continue; 531 532 prot = vmentry.protection; 533 fflags = 0; 534 if (prot & VM_PROT_READ) 535 fflags = PS_FST_FFLAG_READ; 536 if ((vmentry.eflags & MAP_ENTRY_COW) == 0 && 537 prot & VM_PROT_WRITE) 538 fflags |= PS_FST_FFLAG_WRITE; 539 540 /* 541 * Create filestat entry. 542 */ 543 entry = filestat_new_entry(object.handle, 544 PS_FST_TYPE_VNODE, -1, fflags, 545 PS_FST_UFLAG_MMAP, 0, 0, NULL, 0); 546 if (entry != NULL) 547 STAILQ_INSERT_TAIL(head, entry, next); 548 } 549 } 550 exit: 551 return (head); 552 } 553 554 /* 555 * kinfo types to filestat translation. 556 */ 557 static int 558 kinfo_type2fst(int kftype) 559 { 560 static struct { 561 int kf_type; 562 int fst_type; 563 } kftypes2fst[] = { 564 { KF_TYPE_CRYPTO, PS_FST_TYPE_CRYPTO }, 565 { KF_TYPE_FIFO, PS_FST_TYPE_FIFO }, 566 { KF_TYPE_KQUEUE, PS_FST_TYPE_KQUEUE }, 567 { KF_TYPE_MQUEUE, PS_FST_TYPE_MQUEUE }, 568 { KF_TYPE_NONE, PS_FST_TYPE_NONE }, 569 { KF_TYPE_PIPE, PS_FST_TYPE_PIPE }, 570 { KF_TYPE_PTS, PS_FST_TYPE_PTS }, 571 { KF_TYPE_SEM, PS_FST_TYPE_SEM }, 572 { KF_TYPE_SHM, PS_FST_TYPE_SHM }, 573 { KF_TYPE_SOCKET, PS_FST_TYPE_SOCKET }, 574 { KF_TYPE_VNODE, PS_FST_TYPE_VNODE }, 575 { KF_TYPE_UNKNOWN, PS_FST_TYPE_UNKNOWN } 576 }; 577 #define NKFTYPES (sizeof(kftypes2fst) / sizeof(*kftypes2fst)) 578 unsigned int i; 579 580 for (i = 0; i < NKFTYPES; i++) 581 if (kftypes2fst[i].kf_type == kftype) 582 break; 583 if (i == NKFTYPES) 584 return (PS_FST_TYPE_UNKNOWN); 585 return (kftypes2fst[i].fst_type); 586 } 587 588 /* 589 * kinfo flags to filestat translation. 590 */ 591 static int 592 kinfo_fflags2fst(int kfflags) 593 { 594 static struct { 595 int kf_flag; 596 int fst_flag; 597 } kfflags2fst[] = { 598 { KF_FLAG_APPEND, PS_FST_FFLAG_APPEND }, 599 { KF_FLAG_ASYNC, PS_FST_FFLAG_ASYNC }, 600 { KF_FLAG_CAPABILITY, PS_FST_FFLAG_CAPABILITY }, 601 { KF_FLAG_CREAT, PS_FST_FFLAG_CREAT }, 602 { KF_FLAG_DIRECT, PS_FST_FFLAG_DIRECT }, 603 { KF_FLAG_EXCL, PS_FST_FFLAG_EXCL }, 604 { KF_FLAG_EXEC, PS_FST_FFLAG_EXEC }, 605 { KF_FLAG_EXLOCK, PS_FST_FFLAG_EXLOCK }, 606 { KF_FLAG_FSYNC, PS_FST_FFLAG_SYNC }, 607 { KF_FLAG_HASLOCK, PS_FST_FFLAG_HASLOCK }, 608 { KF_FLAG_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW }, 609 { KF_FLAG_NONBLOCK, PS_FST_FFLAG_NONBLOCK }, 610 { KF_FLAG_READ, PS_FST_FFLAG_READ }, 611 { KF_FLAG_SHLOCK, PS_FST_FFLAG_SHLOCK }, 612 { KF_FLAG_TRUNC, PS_FST_FFLAG_TRUNC }, 613 { KF_FLAG_WRITE, PS_FST_FFLAG_WRITE } 614 }; 615 #define NKFFLAGS (sizeof(kfflags2fst) / sizeof(*kfflags2fst)) 616 unsigned int i; 617 int flags; 618 619 flags = 0; 620 for (i = 0; i < NKFFLAGS; i++) 621 if ((kfflags & kfflags2fst[i].kf_flag) != 0) 622 flags |= kfflags2fst[i].fst_flag; 623 return (flags); 624 } 625 626 static int 627 kinfo_uflags2fst(int fd) 628 { 629 630 switch (fd) { 631 case KF_FD_TYPE_CTTY: 632 return (PS_FST_UFLAG_CTTY); 633 case KF_FD_TYPE_CWD: 634 return (PS_FST_UFLAG_CDIR); 635 case KF_FD_TYPE_JAIL: 636 return (PS_FST_UFLAG_JAIL); 637 case KF_FD_TYPE_TEXT: 638 return (PS_FST_UFLAG_TEXT); 639 case KF_FD_TYPE_TRACE: 640 return (PS_FST_UFLAG_TRACE); 641 case KF_FD_TYPE_ROOT: 642 return (PS_FST_UFLAG_RDIR); 643 } 644 return (0); 645 } 646 647 static struct filestat_list * 648 procstat_getfiles_sysctl(struct procstat *procstat, struct kinfo_proc *kp, int mmapped) 649 { 650 struct kinfo_file *kif, *files; 651 struct kinfo_vmentry *kve, *vmentries; 652 struct filestat_list *head; 653 struct filestat *entry; 654 char *path; 655 off_t offset; 656 int cnt, fd, fflags; 657 int i, type, uflags; 658 int refcount; 659 cap_rights_t cap_rights; 660 661 assert(kp); 662 if (kp->ki_fd == NULL) 663 return (NULL); 664 665 files = kinfo_getfile(kp->ki_pid, &cnt); 666 if (files == NULL && errno != EPERM) { 667 warn("kinfo_getfile()"); 668 return (NULL); 669 } 670 procstat->files = files; 671 672 /* 673 * Allocate list head. 674 */ 675 head = malloc(sizeof(*head)); 676 if (head == NULL) 677 return (NULL); 678 STAILQ_INIT(head); 679 for (i = 0; i < cnt; i++) { 680 kif = &files[i]; 681 682 type = kinfo_type2fst(kif->kf_type); 683 fd = kif->kf_fd >= 0 ? kif->kf_fd : -1; 684 fflags = kinfo_fflags2fst(kif->kf_flags); 685 uflags = kinfo_uflags2fst(kif->kf_fd); 686 refcount = kif->kf_ref_count; 687 offset = kif->kf_offset; 688 if (*kif->kf_path != '\0') 689 path = strdup(kif->kf_path); 690 else 691 path = NULL; 692 cap_rights = kif->kf_cap_rights; 693 694 /* 695 * Create filestat entry. 696 */ 697 entry = filestat_new_entry(kif, type, fd, fflags, uflags, 698 refcount, offset, path, cap_rights); 699 if (entry != NULL) 700 STAILQ_INSERT_TAIL(head, entry, next); 701 } 702 if (mmapped != 0) { 703 vmentries = kinfo_getvmmap(kp->ki_pid, &cnt); 704 procstat->vmentries = vmentries; 705 if (vmentries == NULL || cnt == 0) 706 goto fail; 707 for (i = 0; i < cnt; i++) { 708 kve = &vmentries[i]; 709 if (kve->kve_type != KVME_TYPE_VNODE) 710 continue; 711 fflags = 0; 712 if (kve->kve_protection & KVME_PROT_READ) 713 fflags = PS_FST_FFLAG_READ; 714 if ((kve->kve_flags & KVME_FLAG_COW) == 0 && 715 kve->kve_protection & KVME_PROT_WRITE) 716 fflags |= PS_FST_FFLAG_WRITE; 717 offset = kve->kve_offset; 718 refcount = kve->kve_ref_count; 719 if (*kve->kve_path != '\0') 720 path = strdup(kve->kve_path); 721 else 722 path = NULL; 723 entry = filestat_new_entry(kve, PS_FST_TYPE_VNODE, -1, 724 fflags, PS_FST_UFLAG_MMAP, refcount, offset, path, 725 0); 726 if (entry != NULL) 727 STAILQ_INSERT_TAIL(head, entry, next); 728 } 729 } 730 fail: 731 return (head); 732 } 733 734 int 735 procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst, 736 struct pipestat *ps, char *errbuf) 737 { 738 739 assert(ps); 740 if (procstat->type == PROCSTAT_KVM) { 741 return (procstat_get_pipe_info_kvm(procstat->kd, fst, ps, 742 errbuf)); 743 } else if (procstat->type == PROCSTAT_SYSCTL) { 744 return (procstat_get_pipe_info_sysctl(fst, ps, errbuf)); 745 } else { 746 warnx("unknown access method: %d", procstat->type); 747 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 748 return (1); 749 } 750 } 751 752 static int 753 procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst, 754 struct pipestat *ps, char *errbuf) 755 { 756 struct pipe pi; 757 void *pipep; 758 759 assert(kd); 760 assert(ps); 761 assert(fst); 762 bzero(ps, sizeof(*ps)); 763 pipep = fst->fs_typedep; 764 if (pipep == NULL) 765 goto fail; 766 if (!kvm_read_all(kd, (unsigned long)pipep, &pi, sizeof(struct pipe))) { 767 warnx("can't read pipe at %p", (void *)pipep); 768 goto fail; 769 } 770 ps->addr = (uintptr_t)pipep; 771 ps->peer = (uintptr_t)pi.pipe_peer; 772 ps->buffer_cnt = pi.pipe_buffer.cnt; 773 return (0); 774 775 fail: 776 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 777 return (1); 778 } 779 780 static int 781 procstat_get_pipe_info_sysctl(struct filestat *fst, struct pipestat *ps, 782 char *errbuf __unused) 783 { 784 struct kinfo_file *kif; 785 786 assert(ps); 787 assert(fst); 788 bzero(ps, sizeof(*ps)); 789 kif = fst->fs_typedep; 790 if (kif == NULL) 791 return (1); 792 ps->addr = kif->kf_un.kf_pipe.kf_pipe_addr; 793 ps->peer = kif->kf_un.kf_pipe.kf_pipe_peer; 794 ps->buffer_cnt = kif->kf_un.kf_pipe.kf_pipe_buffer_cnt; 795 return (0); 796 } 797 798 int 799 procstat_get_pts_info(struct procstat *procstat, struct filestat *fst, 800 struct ptsstat *pts, char *errbuf) 801 { 802 803 assert(pts); 804 if (procstat->type == PROCSTAT_KVM) { 805 return (procstat_get_pts_info_kvm(procstat->kd, fst, pts, 806 errbuf)); 807 } else if (procstat->type == PROCSTAT_SYSCTL) { 808 return (procstat_get_pts_info_sysctl(fst, pts, errbuf)); 809 } else { 810 warnx("unknown access method: %d", procstat->type); 811 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 812 return (1); 813 } 814 } 815 816 static int 817 procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst, 818 struct ptsstat *pts, char *errbuf) 819 { 820 struct tty tty; 821 void *ttyp; 822 823 assert(kd); 824 assert(pts); 825 assert(fst); 826 bzero(pts, sizeof(*pts)); 827 ttyp = fst->fs_typedep; 828 if (ttyp == NULL) 829 goto fail; 830 if (!kvm_read_all(kd, (unsigned long)ttyp, &tty, sizeof(struct tty))) { 831 warnx("can't read tty at %p", (void *)ttyp); 832 goto fail; 833 } 834 pts->dev = dev2udev(kd, tty.t_dev); 835 (void)kdevtoname(kd, tty.t_dev, pts->devname); 836 return (0); 837 838 fail: 839 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 840 return (1); 841 } 842 843 static int 844 procstat_get_pts_info_sysctl(struct filestat *fst, struct ptsstat *pts, 845 char *errbuf __unused) 846 { 847 struct kinfo_file *kif; 848 849 assert(pts); 850 assert(fst); 851 bzero(pts, sizeof(*pts)); 852 kif = fst->fs_typedep; 853 if (kif == NULL) 854 return (0); 855 pts->dev = kif->kf_un.kf_pts.kf_pts_dev; 856 strlcpy(pts->devname, kif->kf_path, sizeof(pts->devname)); 857 return (0); 858 } 859 860 int 861 procstat_get_shm_info(struct procstat *procstat, struct filestat *fst, 862 struct shmstat *shm, char *errbuf) 863 { 864 865 assert(shm); 866 if (procstat->type == PROCSTAT_KVM) { 867 return (procstat_get_shm_info_kvm(procstat->kd, fst, shm, 868 errbuf)); 869 } else if (procstat->type == PROCSTAT_SYSCTL) { 870 return (procstat_get_shm_info_sysctl(fst, shm, errbuf)); 871 } else { 872 warnx("unknown access method: %d", procstat->type); 873 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 874 return (1); 875 } 876 } 877 878 static int 879 procstat_get_shm_info_kvm(kvm_t *kd, struct filestat *fst, 880 struct shmstat *shm, char *errbuf) 881 { 882 struct shmfd shmfd; 883 void *shmfdp; 884 char *path; 885 int i; 886 887 assert(kd); 888 assert(shm); 889 assert(fst); 890 bzero(shm, sizeof(*shm)); 891 shmfdp = fst->fs_typedep; 892 if (shmfdp == NULL) 893 goto fail; 894 if (!kvm_read_all(kd, (unsigned long)shmfdp, &shmfd, 895 sizeof(struct shmfd))) { 896 warnx("can't read shmfd at %p", (void *)shmfdp); 897 goto fail; 898 } 899 shm->mode = S_IFREG | shmfd.shm_mode; 900 shm->size = shmfd.shm_size; 901 if (fst->fs_path == NULL && shmfd.shm_path != NULL) { 902 path = malloc(MAXPATHLEN); 903 for (i = 0; i < MAXPATHLEN - 1; i++) { 904 if (!kvm_read_all(kd, (unsigned long)shmfd.shm_path + i, 905 path + i, 1)) 906 break; 907 if (path[i] == '\0') 908 break; 909 } 910 path[i] = '\0'; 911 if (i == 0) 912 free(path); 913 else 914 fst->fs_path = path; 915 } 916 return (0); 917 918 fail: 919 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 920 return (1); 921 } 922 923 static int 924 procstat_get_shm_info_sysctl(struct filestat *fst, struct shmstat *shm, 925 char *errbuf __unused) 926 { 927 struct kinfo_file *kif; 928 929 assert(shm); 930 assert(fst); 931 bzero(shm, sizeof(*shm)); 932 kif = fst->fs_typedep; 933 if (kif == NULL) 934 return (0); 935 shm->size = kif->kf_un.kf_file.kf_file_size; 936 shm->mode = kif->kf_un.kf_file.kf_file_mode; 937 return (0); 938 } 939 940 int 941 procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst, 942 struct vnstat *vn, char *errbuf) 943 { 944 945 assert(vn); 946 if (procstat->type == PROCSTAT_KVM) { 947 return (procstat_get_vnode_info_kvm(procstat->kd, fst, vn, 948 errbuf)); 949 } else if (procstat->type == PROCSTAT_SYSCTL) { 950 return (procstat_get_vnode_info_sysctl(fst, vn, errbuf)); 951 } else { 952 warnx("unknown access method: %d", procstat->type); 953 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 954 return (1); 955 } 956 } 957 958 static int 959 procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst, 960 struct vnstat *vn, char *errbuf) 961 { 962 /* Filesystem specific handlers. */ 963 #define FSTYPE(fst) {#fst, fst##_filestat} 964 struct { 965 const char *tag; 966 int (*handler)(kvm_t *kd, struct vnode *vp, 967 struct vnstat *vn); 968 } fstypes[] = { 969 FSTYPE(devfs), 970 FSTYPE(isofs), 971 FSTYPE(msdosfs), 972 FSTYPE(nfs), 973 FSTYPE(ntfs), 974 #ifdef LIBPROCSTAT_NWFS 975 FSTYPE(nwfs), 976 #endif 977 FSTYPE(smbfs), 978 FSTYPE(udf), 979 FSTYPE(ufs), 980 #ifdef LIBPROCSTAT_ZFS 981 FSTYPE(zfs), 982 #endif 983 }; 984 #define NTYPES (sizeof(fstypes) / sizeof(*fstypes)) 985 struct vnode vnode; 986 char tagstr[12]; 987 void *vp; 988 int error, found; 989 unsigned int i; 990 991 assert(kd); 992 assert(vn); 993 assert(fst); 994 vp = fst->fs_typedep; 995 if (vp == NULL) 996 goto fail; 997 error = kvm_read_all(kd, (unsigned long)vp, &vnode, sizeof(vnode)); 998 if (error == 0) { 999 warnx("can't read vnode at %p", (void *)vp); 1000 goto fail; 1001 } 1002 bzero(vn, sizeof(*vn)); 1003 vn->vn_type = vntype2psfsttype(vnode.v_type); 1004 if (vnode.v_type == VNON || vnode.v_type == VBAD) 1005 return (0); 1006 error = kvm_read_all(kd, (unsigned long)vnode.v_tag, tagstr, 1007 sizeof(tagstr)); 1008 if (error == 0) { 1009 warnx("can't read v_tag at %p", (void *)vp); 1010 goto fail; 1011 } 1012 tagstr[sizeof(tagstr) - 1] = '\0'; 1013 1014 /* 1015 * Find appropriate handler. 1016 */ 1017 for (i = 0, found = 0; i < NTYPES; i++) 1018 if (!strcmp(fstypes[i].tag, tagstr)) { 1019 if (fstypes[i].handler(kd, &vnode, vn) != 0) { 1020 goto fail; 1021 } 1022 break; 1023 } 1024 if (i == NTYPES) { 1025 snprintf(errbuf, _POSIX2_LINE_MAX, "?(%s)", tagstr); 1026 return (1); 1027 } 1028 vn->vn_mntdir = getmnton(kd, vnode.v_mount); 1029 if ((vnode.v_type == VBLK || vnode.v_type == VCHR) && 1030 vnode.v_rdev != NULL){ 1031 vn->vn_dev = dev2udev(kd, vnode.v_rdev); 1032 (void)kdevtoname(kd, vnode.v_rdev, vn->vn_devname); 1033 } else { 1034 vn->vn_dev = -1; 1035 } 1036 return (0); 1037 1038 fail: 1039 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 1040 return (1); 1041 } 1042 1043 /* 1044 * kinfo vnode type to filestat translation. 1045 */ 1046 static int 1047 kinfo_vtype2fst(int kfvtype) 1048 { 1049 static struct { 1050 int kf_vtype; 1051 int fst_vtype; 1052 } kfvtypes2fst[] = { 1053 { KF_VTYPE_VBAD, PS_FST_VTYPE_VBAD }, 1054 { KF_VTYPE_VBLK, PS_FST_VTYPE_VBLK }, 1055 { KF_VTYPE_VCHR, PS_FST_VTYPE_VCHR }, 1056 { KF_VTYPE_VDIR, PS_FST_VTYPE_VDIR }, 1057 { KF_VTYPE_VFIFO, PS_FST_VTYPE_VFIFO }, 1058 { KF_VTYPE_VLNK, PS_FST_VTYPE_VLNK }, 1059 { KF_VTYPE_VNON, PS_FST_VTYPE_VNON }, 1060 { KF_VTYPE_VREG, PS_FST_VTYPE_VREG }, 1061 { KF_VTYPE_VSOCK, PS_FST_VTYPE_VSOCK } 1062 }; 1063 #define NKFVTYPES (sizeof(kfvtypes2fst) / sizeof(*kfvtypes2fst)) 1064 unsigned int i; 1065 1066 for (i = 0; i < NKFVTYPES; i++) 1067 if (kfvtypes2fst[i].kf_vtype == kfvtype) 1068 break; 1069 if (i == NKFVTYPES) 1070 return (PS_FST_VTYPE_UNKNOWN); 1071 return (kfvtypes2fst[i].fst_vtype); 1072 } 1073 1074 static int 1075 procstat_get_vnode_info_sysctl(struct filestat *fst, struct vnstat *vn, 1076 char *errbuf) 1077 { 1078 struct statfs stbuf; 1079 struct kinfo_file *kif; 1080 struct kinfo_vmentry *kve; 1081 uint64_t fileid; 1082 uint64_t size; 1083 char *name, *path; 1084 uint32_t fsid; 1085 uint16_t mode; 1086 uint32_t rdev; 1087 int vntype; 1088 int status; 1089 1090 assert(fst); 1091 assert(vn); 1092 bzero(vn, sizeof(*vn)); 1093 if (fst->fs_typedep == NULL) 1094 return (1); 1095 if (fst->fs_uflags & PS_FST_UFLAG_MMAP) { 1096 kve = fst->fs_typedep; 1097 fileid = kve->kve_vn_fileid; 1098 fsid = kve->kve_vn_fsid; 1099 mode = kve->kve_vn_mode; 1100 path = kve->kve_path; 1101 rdev = kve->kve_vn_rdev; 1102 size = kve->kve_vn_size; 1103 vntype = kinfo_vtype2fst(kve->kve_vn_type); 1104 status = kve->kve_status; 1105 } else { 1106 kif = fst->fs_typedep; 1107 fileid = kif->kf_un.kf_file.kf_file_fileid; 1108 fsid = kif->kf_un.kf_file.kf_file_fsid; 1109 mode = kif->kf_un.kf_file.kf_file_mode; 1110 path = kif->kf_path; 1111 rdev = kif->kf_un.kf_file.kf_file_rdev; 1112 size = kif->kf_un.kf_file.kf_file_size; 1113 vntype = kinfo_vtype2fst(kif->kf_vnode_type); 1114 status = kif->kf_status; 1115 } 1116 vn->vn_type = vntype; 1117 if (vntype == PS_FST_VTYPE_VNON || vntype == PS_FST_VTYPE_VBAD) 1118 return (0); 1119 if ((status & KF_ATTR_VALID) == 0) { 1120 snprintf(errbuf, _POSIX2_LINE_MAX, "? (no info available)"); 1121 return (1); 1122 } 1123 if (path && *path) { 1124 statfs(path, &stbuf); 1125 vn->vn_mntdir = strdup(stbuf.f_mntonname); 1126 } else 1127 vn->vn_mntdir = strdup("-"); 1128 vn->vn_dev = rdev; 1129 if (vntype == PS_FST_VTYPE_VBLK) { 1130 name = devname(rdev, S_IFBLK); 1131 if (name != NULL) 1132 strlcpy(vn->vn_devname, name, 1133 sizeof(vn->vn_devname)); 1134 } else if (vntype == PS_FST_VTYPE_VCHR) { 1135 name = devname(vn->vn_dev, S_IFCHR); 1136 if (name != NULL) 1137 strlcpy(vn->vn_devname, name, 1138 sizeof(vn->vn_devname)); 1139 } 1140 vn->vn_fsid = fsid; 1141 vn->vn_fileid = fileid; 1142 vn->vn_size = size; 1143 vn->vn_mode = mode; 1144 return (0); 1145 } 1146 1147 int 1148 procstat_get_socket_info(struct procstat *procstat, struct filestat *fst, 1149 struct sockstat *sock, char *errbuf) 1150 { 1151 1152 assert(sock); 1153 if (procstat->type == PROCSTAT_KVM) { 1154 return (procstat_get_socket_info_kvm(procstat->kd, fst, sock, 1155 errbuf)); 1156 } else if (procstat->type == PROCSTAT_SYSCTL) { 1157 return (procstat_get_socket_info_sysctl(fst, sock, errbuf)); 1158 } else { 1159 warnx("unknown access method: %d", procstat->type); 1160 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 1161 return (1); 1162 } 1163 } 1164 1165 static int 1166 procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst, 1167 struct sockstat *sock, char *errbuf) 1168 { 1169 struct domain dom; 1170 struct inpcb inpcb; 1171 struct protosw proto; 1172 struct socket s; 1173 struct unpcb unpcb; 1174 ssize_t len; 1175 void *so; 1176 1177 assert(kd); 1178 assert(sock); 1179 assert(fst); 1180 bzero(sock, sizeof(*sock)); 1181 so = fst->fs_typedep; 1182 if (so == NULL) 1183 goto fail; 1184 sock->so_addr = (uintptr_t)so; 1185 /* fill in socket */ 1186 if (!kvm_read_all(kd, (unsigned long)so, &s, 1187 sizeof(struct socket))) { 1188 warnx("can't read sock at %p", (void *)so); 1189 goto fail; 1190 } 1191 /* fill in protosw entry */ 1192 if (!kvm_read_all(kd, (unsigned long)s.so_proto, &proto, 1193 sizeof(struct protosw))) { 1194 warnx("can't read protosw at %p", (void *)s.so_proto); 1195 goto fail; 1196 } 1197 /* fill in domain */ 1198 if (!kvm_read_all(kd, (unsigned long)proto.pr_domain, &dom, 1199 sizeof(struct domain))) { 1200 warnx("can't read domain at %p", 1201 (void *)proto.pr_domain); 1202 goto fail; 1203 } 1204 if ((len = kvm_read(kd, (unsigned long)dom.dom_name, sock->dname, 1205 sizeof(sock->dname) - 1)) < 0) { 1206 warnx("can't read domain name at %p", (void *)dom.dom_name); 1207 sock->dname[0] = '\0'; 1208 } 1209 else 1210 sock->dname[len] = '\0'; 1211 1212 /* 1213 * Fill in known data. 1214 */ 1215 sock->type = s.so_type; 1216 sock->proto = proto.pr_protocol; 1217 sock->dom_family = dom.dom_family; 1218 sock->so_pcb = (uintptr_t)s.so_pcb; 1219 1220 /* 1221 * Protocol specific data. 1222 */ 1223 switch(dom.dom_family) { 1224 case AF_INET: 1225 case AF_INET6: 1226 if (proto.pr_protocol == IPPROTO_TCP) { 1227 if (s.so_pcb) { 1228 if (kvm_read(kd, (u_long)s.so_pcb, 1229 (char *)&inpcb, sizeof(struct inpcb)) 1230 != sizeof(struct inpcb)) { 1231 warnx("can't read inpcb at %p", 1232 (void *)s.so_pcb); 1233 } else 1234 sock->inp_ppcb = 1235 (uintptr_t)inpcb.inp_ppcb; 1236 } 1237 } 1238 break; 1239 case AF_UNIX: 1240 if (s.so_pcb) { 1241 if (kvm_read(kd, (u_long)s.so_pcb, (char *)&unpcb, 1242 sizeof(struct unpcb)) != sizeof(struct unpcb)){ 1243 warnx("can't read unpcb at %p", 1244 (void *)s.so_pcb); 1245 } else if (unpcb.unp_conn) { 1246 sock->so_rcv_sb_state = s.so_rcv.sb_state; 1247 sock->so_snd_sb_state = s.so_snd.sb_state; 1248 sock->unp_conn = (uintptr_t)unpcb.unp_conn; 1249 } 1250 } 1251 break; 1252 default: 1253 break; 1254 } 1255 return (0); 1256 1257 fail: 1258 snprintf(errbuf, _POSIX2_LINE_MAX, "error"); 1259 return (1); 1260 } 1261 1262 static int 1263 procstat_get_socket_info_sysctl(struct filestat *fst, struct sockstat *sock, 1264 char *errbuf __unused) 1265 { 1266 struct kinfo_file *kif; 1267 1268 assert(sock); 1269 assert(fst); 1270 bzero(sock, sizeof(*sock)); 1271 kif = fst->fs_typedep; 1272 if (kif == NULL) 1273 return (0); 1274 1275 /* 1276 * Fill in known data. 1277 */ 1278 sock->type = kif->kf_sock_type; 1279 sock->proto = kif->kf_sock_protocol; 1280 sock->dom_family = kif->kf_sock_domain; 1281 sock->so_pcb = kif->kf_un.kf_sock.kf_sock_pcb; 1282 strlcpy(sock->dname, kif->kf_path, sizeof(sock->dname)); 1283 bcopy(&kif->kf_sa_local, &sock->sa_local, kif->kf_sa_local.ss_len); 1284 bcopy(&kif->kf_sa_peer, &sock->sa_peer, kif->kf_sa_peer.ss_len); 1285 1286 /* 1287 * Protocol specific data. 1288 */ 1289 switch(sock->dom_family) { 1290 case AF_INET: 1291 case AF_INET6: 1292 if (sock->proto == IPPROTO_TCP) 1293 sock->inp_ppcb = kif->kf_un.kf_sock.kf_sock_inpcb; 1294 break; 1295 case AF_UNIX: 1296 if (kif->kf_un.kf_sock.kf_sock_unpconn != 0) { 1297 sock->so_rcv_sb_state = 1298 kif->kf_un.kf_sock.kf_sock_rcv_sb_state; 1299 sock->so_snd_sb_state = 1300 kif->kf_un.kf_sock.kf_sock_snd_sb_state; 1301 sock->unp_conn = 1302 kif->kf_un.kf_sock.kf_sock_unpconn; 1303 } 1304 break; 1305 default: 1306 break; 1307 } 1308 return (0); 1309 } 1310 1311 /* 1312 * Descriptor flags to filestat translation. 1313 */ 1314 static int 1315 to_filestat_flags(int flags) 1316 { 1317 static struct { 1318 int flag; 1319 int fst_flag; 1320 } fstflags[] = { 1321 { FREAD, PS_FST_FFLAG_READ }, 1322 { FWRITE, PS_FST_FFLAG_WRITE }, 1323 { O_APPEND, PS_FST_FFLAG_APPEND }, 1324 { O_ASYNC, PS_FST_FFLAG_ASYNC }, 1325 { O_CREAT, PS_FST_FFLAG_CREAT }, 1326 { O_DIRECT, PS_FST_FFLAG_DIRECT }, 1327 { O_EXCL, PS_FST_FFLAG_EXCL }, 1328 { O_EXEC, PS_FST_FFLAG_EXEC }, 1329 { O_EXLOCK, PS_FST_FFLAG_EXLOCK }, 1330 { O_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW }, 1331 { O_NONBLOCK, PS_FST_FFLAG_NONBLOCK }, 1332 { O_SHLOCK, PS_FST_FFLAG_SHLOCK }, 1333 { O_SYNC, PS_FST_FFLAG_SYNC }, 1334 { O_TRUNC, PS_FST_FFLAG_TRUNC } 1335 }; 1336 #define NFSTFLAGS (sizeof(fstflags) / sizeof(*fstflags)) 1337 int fst_flags; 1338 unsigned int i; 1339 1340 fst_flags = 0; 1341 for (i = 0; i < NFSTFLAGS; i++) 1342 if (flags & fstflags[i].flag) 1343 fst_flags |= fstflags[i].fst_flag; 1344 return (fst_flags); 1345 } 1346 1347 /* 1348 * Vnode type to filestate translation. 1349 */ 1350 static int 1351 vntype2psfsttype(int type) 1352 { 1353 static struct { 1354 int vtype; 1355 int fst_vtype; 1356 } vt2fst[] = { 1357 { VBAD, PS_FST_VTYPE_VBAD }, 1358 { VBLK, PS_FST_VTYPE_VBLK }, 1359 { VCHR, PS_FST_VTYPE_VCHR }, 1360 { VDIR, PS_FST_VTYPE_VDIR }, 1361 { VFIFO, PS_FST_VTYPE_VFIFO }, 1362 { VLNK, PS_FST_VTYPE_VLNK }, 1363 { VNON, PS_FST_VTYPE_VNON }, 1364 { VREG, PS_FST_VTYPE_VREG }, 1365 { VSOCK, PS_FST_VTYPE_VSOCK } 1366 }; 1367 #define NVFTYPES (sizeof(vt2fst) / sizeof(*vt2fst)) 1368 unsigned int i, fst_type; 1369 1370 fst_type = PS_FST_VTYPE_UNKNOWN; 1371 for (i = 0; i < NVFTYPES; i++) { 1372 if (type == vt2fst[i].vtype) { 1373 fst_type = vt2fst[i].fst_vtype; 1374 break; 1375 } 1376 } 1377 return (fst_type); 1378 } 1379 1380 static char * 1381 getmnton(kvm_t *kd, struct mount *m) 1382 { 1383 struct mount mnt; 1384 static struct mtab { 1385 struct mtab *next; 1386 struct mount *m; 1387 char mntonname[MNAMELEN + 1]; 1388 } *mhead = NULL; 1389 struct mtab *mt; 1390 1391 for (mt = mhead; mt != NULL; mt = mt->next) 1392 if (m == mt->m) 1393 return (mt->mntonname); 1394 if (!kvm_read_all(kd, (unsigned long)m, &mnt, sizeof(struct mount))) { 1395 warnx("can't read mount table at %p", (void *)m); 1396 return (NULL); 1397 } 1398 if ((mt = malloc(sizeof (struct mtab))) == NULL) 1399 err(1, NULL); 1400 mt->m = m; 1401 bcopy(&mnt.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); 1402 mt->mntonname[MNAMELEN] = '\0'; 1403 mt->next = mhead; 1404 mhead = mt; 1405 return (mt->mntonname); 1406 } 1407