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