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