libprocstat.c (223279) | libprocstat.c (224859) |
---|---|
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: --- 19 unchanged lines hidden (view full) --- 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> | 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: --- 19 unchanged lines hidden (view full) --- 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35#include <sys/cdefs.h> |
36__FBSDID("$FreeBSD: head/lib/libprocstat/libprocstat.c 223279 2011-06-18 23:01:26Z jilles $"); | 36__FBSDID("$FreeBSD: head/lib/libprocstat/libprocstat.c 224859 2011-08-14 00:42:09Z rwatson $"); |
37 38#include <sys/param.h> 39#include <sys/time.h> 40#include <sys/proc.h> 41#include <sys/user.h> 42#include <sys/stat.h> 43#include <sys/vnode.h> 44#include <sys/socket.h> --- 232 unchanged lines hidden (view full) --- 277 if (procstat->files != NULL) { 278 free(procstat->files); 279 procstat->files = NULL; 280 } 281} 282 283static struct filestat * 284filestat_new_entry(void *typedep, int type, int fd, int fflags, int uflags, | 37 38#include <sys/param.h> 39#include <sys/time.h> 40#include <sys/proc.h> 41#include <sys/user.h> 42#include <sys/stat.h> 43#include <sys/vnode.h> 44#include <sys/socket.h> --- 232 unchanged lines hidden (view full) --- 277 if (procstat->files != NULL) { 278 free(procstat->files); 279 procstat->files = NULL; 280 } 281} 282 283static struct filestat * 284filestat_new_entry(void *typedep, int type, int fd, int fflags, int uflags, |
285 int refcount, off_t offset, char *path) | 285 int refcount, off_t offset, char *path, cap_rights_t cap_rights) |
286{ 287 struct filestat *entry; 288 289 entry = calloc(1, sizeof(*entry)); 290 if (entry == NULL) { 291 warn("malloc()"); 292 return (NULL); 293 } 294 entry->fs_typedep = typedep; 295 entry->fs_fflags = fflags; 296 entry->fs_uflags = uflags; 297 entry->fs_fd = fd; 298 entry->fs_type = type; 299 entry->fs_ref_count = refcount; 300 entry->fs_offset = offset; 301 entry->fs_path = path; | 286{ 287 struct filestat *entry; 288 289 entry = calloc(1, sizeof(*entry)); 290 if (entry == NULL) { 291 warn("malloc()"); 292 return (NULL); 293 } 294 entry->fs_typedep = typedep; 295 entry->fs_fflags = fflags; 296 entry->fs_uflags = uflags; 297 entry->fs_fd = fd; 298 entry->fs_type = type; 299 entry->fs_ref_count = refcount; 300 entry->fs_offset = offset; 301 entry->fs_path = path; |
302 entry->fs_cap_rights = cap_rights; |
|
302 return (entry); 303} 304 305static struct vnode * 306getctty(kvm_t *kd, struct kinfo_proc *kp) 307{ 308 struct pgrp pgrp; 309 struct proc proc; --- 66 unchanged lines hidden (view full) --- 376 head = malloc(sizeof(*head)); 377 if (head == NULL) 378 return (NULL); 379 STAILQ_INIT(head); 380 381 /* root directory vnode, if one. */ 382 if (filed.fd_rdir) { 383 entry = filestat_new_entry(filed.fd_rdir, PS_FST_TYPE_VNODE, -1, | 303 return (entry); 304} 305 306static struct vnode * 307getctty(kvm_t *kd, struct kinfo_proc *kp) 308{ 309 struct pgrp pgrp; 310 struct proc proc; --- 66 unchanged lines hidden (view full) --- 377 head = malloc(sizeof(*head)); 378 if (head == NULL) 379 return (NULL); 380 STAILQ_INIT(head); 381 382 /* root directory vnode, if one. */ 383 if (filed.fd_rdir) { 384 entry = filestat_new_entry(filed.fd_rdir, PS_FST_TYPE_VNODE, -1, |
384 PS_FST_FFLAG_READ, PS_FST_UFLAG_RDIR, 0, 0, NULL); | 385 PS_FST_FFLAG_READ, PS_FST_UFLAG_RDIR, 0, 0, NULL, 0); |
385 if (entry != NULL) 386 STAILQ_INSERT_TAIL(head, entry, next); 387 } 388 /* current working directory vnode. */ 389 if (filed.fd_cdir) { 390 entry = filestat_new_entry(filed.fd_cdir, PS_FST_TYPE_VNODE, -1, | 386 if (entry != NULL) 387 STAILQ_INSERT_TAIL(head, entry, next); 388 } 389 /* current working directory vnode. */ 390 if (filed.fd_cdir) { 391 entry = filestat_new_entry(filed.fd_cdir, PS_FST_TYPE_VNODE, -1, |
391 PS_FST_FFLAG_READ, PS_FST_UFLAG_CDIR, 0, 0, NULL); | 392 PS_FST_FFLAG_READ, PS_FST_UFLAG_CDIR, 0, 0, NULL, 0); |
392 if (entry != NULL) 393 STAILQ_INSERT_TAIL(head, entry, next); 394 } 395 /* jail root, if any. */ 396 if (filed.fd_jdir) { 397 entry = filestat_new_entry(filed.fd_jdir, PS_FST_TYPE_VNODE, -1, | 393 if (entry != NULL) 394 STAILQ_INSERT_TAIL(head, entry, next); 395 } 396 /* jail root, if any. */ 397 if (filed.fd_jdir) { 398 entry = filestat_new_entry(filed.fd_jdir, PS_FST_TYPE_VNODE, -1, |
398 PS_FST_FFLAG_READ, PS_FST_UFLAG_JAIL, 0, 0, NULL); | 399 PS_FST_FFLAG_READ, PS_FST_UFLAG_JAIL, 0, 0, NULL, 0); |
399 if (entry != NULL) 400 STAILQ_INSERT_TAIL(head, entry, next); 401 } 402 /* ktrace vnode, if one */ 403 if (kp->ki_tracep) { 404 entry = filestat_new_entry(kp->ki_tracep, PS_FST_TYPE_VNODE, -1, 405 PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE, | 400 if (entry != NULL) 401 STAILQ_INSERT_TAIL(head, entry, next); 402 } 403 /* ktrace vnode, if one */ 404 if (kp->ki_tracep) { 405 entry = filestat_new_entry(kp->ki_tracep, PS_FST_TYPE_VNODE, -1, 406 PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE, |
406 PS_FST_UFLAG_TRACE, 0, 0, NULL); | 407 PS_FST_UFLAG_TRACE, 0, 0, NULL, 0); |
407 if (entry != NULL) 408 STAILQ_INSERT_TAIL(head, entry, next); 409 } 410 /* text vnode, if one */ 411 if (kp->ki_textvp) { 412 entry = filestat_new_entry(kp->ki_textvp, PS_FST_TYPE_VNODE, -1, | 408 if (entry != NULL) 409 STAILQ_INSERT_TAIL(head, entry, next); 410 } 411 /* text vnode, if one */ 412 if (kp->ki_textvp) { 413 entry = filestat_new_entry(kp->ki_textvp, PS_FST_TYPE_VNODE, -1, |
413 PS_FST_FFLAG_READ, PS_FST_UFLAG_TEXT, 0, 0, NULL); | 414 PS_FST_FFLAG_READ, PS_FST_UFLAG_TEXT, 0, 0, NULL, 0); |
414 if (entry != NULL) 415 STAILQ_INSERT_TAIL(head, entry, next); 416 } 417 /* Controlling terminal. */ 418 if ((vp = getctty(kd, kp)) != NULL) { 419 entry = filestat_new_entry(vp, PS_FST_TYPE_VNODE, -1, 420 PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE, | 415 if (entry != NULL) 416 STAILQ_INSERT_TAIL(head, entry, next); 417 } 418 /* Controlling terminal. */ 419 if ((vp = getctty(kd, kp)) != NULL) { 420 entry = filestat_new_entry(vp, PS_FST_TYPE_VNODE, -1, 421 PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE, |
421 PS_FST_UFLAG_CTTY, 0, 0, NULL); | 422 PS_FST_UFLAG_CTTY, 0, 0, NULL, 0); |
422 if (entry != NULL) 423 STAILQ_INSERT_TAIL(head, entry, next); 424 } 425 426 nfiles = filed.fd_lastfile + 1; 427 ofiles = malloc(nfiles * sizeof(struct file *)); 428 if (ofiles == NULL) { 429 warn("malloc(%zu)", nfiles * sizeof(struct file *)); --- 36 unchanged lines hidden (view full) --- 466 case DTYPE_PTS: 467 type = PS_FST_TYPE_PTS; 468 data = file.f_data; 469 break; 470#endif 471 default: 472 continue; 473 } | 423 if (entry != NULL) 424 STAILQ_INSERT_TAIL(head, entry, next); 425 } 426 427 nfiles = filed.fd_lastfile + 1; 428 ofiles = malloc(nfiles * sizeof(struct file *)); 429 if (ofiles == NULL) { 430 warn("malloc(%zu)", nfiles * sizeof(struct file *)); --- 36 unchanged lines hidden (view full) --- 467 case DTYPE_PTS: 468 type = PS_FST_TYPE_PTS; 469 data = file.f_data; 470 break; 471#endif 472 default: 473 continue; 474 } |
475 /* XXXRW: No capability rights support for kvm yet. */ |
|
474 entry = filestat_new_entry(data, type, i, | 476 entry = filestat_new_entry(data, type, i, |
475 to_filestat_flags(file.f_flag), 0, 0, 0, NULL); | 477 to_filestat_flags(file.f_flag), 0, 0, 0, NULL, 0); |
476 if (entry != NULL) 477 STAILQ_INSERT_TAIL(head, entry, next); 478 } 479 free(ofiles); 480 481do_mmapped: 482 483 /* --- 42 unchanged lines hidden (view full) --- 526 prot & VM_PROT_WRITE) 527 fflags |= PS_FST_FFLAG_WRITE; 528 529 /* 530 * Create filestat entry. 531 */ 532 entry = filestat_new_entry(object.handle, 533 PS_FST_TYPE_VNODE, -1, fflags, | 478 if (entry != NULL) 479 STAILQ_INSERT_TAIL(head, entry, next); 480 } 481 free(ofiles); 482 483do_mmapped: 484 485 /* --- 42 unchanged lines hidden (view full) --- 528 prot & VM_PROT_WRITE) 529 fflags |= PS_FST_FFLAG_WRITE; 530 531 /* 532 * Create filestat entry. 533 */ 534 entry = filestat_new_entry(object.handle, 535 PS_FST_TYPE_VNODE, -1, fflags, |
534 PS_FST_UFLAG_MMAP, 0, 0, NULL); | 536 PS_FST_UFLAG_MMAP, 0, 0, NULL, 0); |
535 if (entry != NULL) 536 STAILQ_INSERT_TAIL(head, entry, next); 537 } 538 } 539exit: 540 return (head); 541} 542 --- 38 unchanged lines hidden (view full) --- 581kinfo_fflags2fst(int kfflags) 582{ 583 static struct { 584 int kf_flag; 585 int fst_flag; 586 } kfflags2fst[] = { 587 { KF_FLAG_APPEND, PS_FST_FFLAG_APPEND }, 588 { KF_FLAG_ASYNC, PS_FST_FFLAG_ASYNC }, | 537 if (entry != NULL) 538 STAILQ_INSERT_TAIL(head, entry, next); 539 } 540 } 541exit: 542 return (head); 543} 544 --- 38 unchanged lines hidden (view full) --- 583kinfo_fflags2fst(int kfflags) 584{ 585 static struct { 586 int kf_flag; 587 int fst_flag; 588 } kfflags2fst[] = { 589 { KF_FLAG_APPEND, PS_FST_FFLAG_APPEND }, 590 { KF_FLAG_ASYNC, PS_FST_FFLAG_ASYNC }, |
591 { KF_FLAG_CAPABILITY, PS_FST_FFLAG_CAPABILITY }, |
|
589 { KF_FLAG_CREAT, PS_FST_FFLAG_CREAT }, 590 { KF_FLAG_DIRECT, PS_FST_FFLAG_DIRECT }, 591 { KF_FLAG_EXCL, PS_FST_FFLAG_EXCL }, 592 { KF_FLAG_EXEC, PS_FST_FFLAG_EXEC }, 593 { KF_FLAG_EXLOCK, PS_FST_FFLAG_EXLOCK }, 594 { KF_FLAG_FSYNC, PS_FST_FFLAG_SYNC }, 595 { KF_FLAG_HASLOCK, PS_FST_FFLAG_HASLOCK }, 596 { KF_FLAG_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW }, --- 42 unchanged lines hidden (view full) --- 639 struct kinfo_vmentry *kve, *vmentries; 640 struct filestat_list *head; 641 struct filestat *entry; 642 char *path; 643 off_t offset; 644 int cnt, fd, fflags; 645 int i, type, uflags; 646 int refcount; | 592 { KF_FLAG_CREAT, PS_FST_FFLAG_CREAT }, 593 { KF_FLAG_DIRECT, PS_FST_FFLAG_DIRECT }, 594 { KF_FLAG_EXCL, PS_FST_FFLAG_EXCL }, 595 { KF_FLAG_EXEC, PS_FST_FFLAG_EXEC }, 596 { KF_FLAG_EXLOCK, PS_FST_FFLAG_EXLOCK }, 597 { KF_FLAG_FSYNC, PS_FST_FFLAG_SYNC }, 598 { KF_FLAG_HASLOCK, PS_FST_FFLAG_HASLOCK }, 599 { KF_FLAG_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW }, --- 42 unchanged lines hidden (view full) --- 642 struct kinfo_vmentry *kve, *vmentries; 643 struct filestat_list *head; 644 struct filestat *entry; 645 char *path; 646 off_t offset; 647 int cnt, fd, fflags; 648 int i, type, uflags; 649 int refcount; |
650 cap_rights_t cap_rights; |
|
647 648 assert(kp); 649 if (kp->ki_fd == NULL) 650 return (NULL); 651 652 files = kinfo_getfile(kp->ki_pid, &cnt); 653 if (files == NULL && errno != EPERM) { 654 warn("kinfo_getfile()"); --- 16 unchanged lines hidden (view full) --- 671 fflags = kinfo_fflags2fst(kif->kf_flags); 672 uflags = kinfo_uflags2fst(kif->kf_fd); 673 refcount = kif->kf_ref_count; 674 offset = kif->kf_offset; 675 if (*kif->kf_path != '\0') 676 path = strdup(kif->kf_path); 677 else 678 path = NULL; | 651 652 assert(kp); 653 if (kp->ki_fd == NULL) 654 return (NULL); 655 656 files = kinfo_getfile(kp->ki_pid, &cnt); 657 if (files == NULL && errno != EPERM) { 658 warn("kinfo_getfile()"); --- 16 unchanged lines hidden (view full) --- 675 fflags = kinfo_fflags2fst(kif->kf_flags); 676 uflags = kinfo_uflags2fst(kif->kf_fd); 677 refcount = kif->kf_ref_count; 678 offset = kif->kf_offset; 679 if (*kif->kf_path != '\0') 680 path = strdup(kif->kf_path); 681 else 682 path = NULL; |
683 cap_rights = kif->kf_cap_rights; |
|
679 680 /* 681 * Create filestat entry. 682 */ 683 entry = filestat_new_entry(kif, type, fd, fflags, uflags, | 684 685 /* 686 * Create filestat entry. 687 */ 688 entry = filestat_new_entry(kif, type, fd, fflags, uflags, |
684 refcount, offset, path); | 689 refcount, offset, path, cap_rights); |
685 if (entry != NULL) 686 STAILQ_INSERT_TAIL(head, entry, next); 687 } 688 if (mmapped != 0) { 689 vmentries = kinfo_getvmmap(kp->ki_pid, &cnt); 690 procstat->vmentries = vmentries; 691 if (vmentries == NULL || cnt == 0) 692 goto fail; --- 9 unchanged lines hidden (view full) --- 702 fflags |= PS_FST_FFLAG_WRITE; 703 offset = kve->kve_offset; 704 refcount = kve->kve_ref_count; 705 if (*kve->kve_path != '\0') 706 path = strdup(kve->kve_path); 707 else 708 path = NULL; 709 entry = filestat_new_entry(kve, PS_FST_TYPE_VNODE, -1, | 690 if (entry != NULL) 691 STAILQ_INSERT_TAIL(head, entry, next); 692 } 693 if (mmapped != 0) { 694 vmentries = kinfo_getvmmap(kp->ki_pid, &cnt); 695 procstat->vmentries = vmentries; 696 if (vmentries == NULL || cnt == 0) 697 goto fail; --- 9 unchanged lines hidden (view full) --- 707 fflags |= PS_FST_FFLAG_WRITE; 708 offset = kve->kve_offset; 709 refcount = kve->kve_ref_count; 710 if (*kve->kve_path != '\0') 711 path = strdup(kve->kve_path); 712 else 713 path = NULL; 714 entry = filestat_new_entry(kve, PS_FST_TYPE_VNODE, -1, |
710 fflags, PS_FST_UFLAG_MMAP, refcount, offset, path); | 715 fflags, PS_FST_UFLAG_MMAP, refcount, offset, path, 716 0); |
711 if (entry != NULL) 712 STAILQ_INSERT_TAIL(head, entry, next); 713 } 714 } 715fail: 716 return (head); 717} 718 --- 593 unchanged lines hidden --- | 717 if (entry != NULL) 718 STAILQ_INSERT_TAIL(head, entry, next); 719 } 720 } 721fail: 722 return (head); 723} 724 --- 593 unchanged lines hidden --- |