1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
|
/*
* dproc.c -- pstat-based HP-UX process access functions for lsof
*/
/*
* Copyright 1999 Purdue Research Foundation, West Lafayette, Indiana
* 47907. All rights reserved.
*
* Written by Victor A. Abell
*
* This software is not subject to any license of the American Telephone
* and Telegraph Company or the Regents of the University of California.
*
* Permission is granted to anyone to use this software for any purpose on
* any computer system, and to alter it and redistribute it freely, subject
* to the following restrictions:
*
* 1. Neither the authors nor Purdue University are responsible for any
* consequences of the use of this software.
*
* 2. The origin of this software must not be misrepresented, either by
* explicit claim or by omission. Credit to the authors and Purdue
* University must appear in documentation and sources.
*
* 3. Altered versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 4. This notice may not be removed or altered.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright 1999 Purdue Research Foundation.\nAll rights reserved.\n";
static char *rcsid = "$Id";
#endif
#include "lsof.h"
/*
* Local definitions
*/
#define FDS_ALLOC_INCR 256 /* fds[] allocation increment */
#define FDS_ALLOC_INIT 64 /* initial fds[] allocation */
#define FINFOINCR 128 /* pst_fileinfo2 table allocation
* increment */
#define INCLMEM(s, m) ((size_t)(offsetof(struct s, m) \
+ sizeof(((struct s *)0)->m)))
/* size of struct s, including
* member m */
#define PSTATINCR 512 /* pst_status table allocation
* increment */
#define TXTVMINCR 64 /* text and vm info table table
* allocation increment */
#define VMREGINCR 64 /* VM region table table allocation
* increment */
/*
* Local structures
*/
struct pstatck {
size_t moff; /* offset of size member in pst_static
* -- from offsetof(...member) */
size_t msz; /* structure's pst_static member
* inclusion size -- from INCLMEM(s, m)
* macro */
size_t ssz; /* structure size -- from
* sizeof(struct) */
char *sn; /* structure name */
} PstatCk[] = {
{ (size_t)offsetof(struct pst_static, pst_status_size),
(size_t)INCLMEM(pst_static, pst_status_size),
sizeof(struct pst_status),
"pst_status" },
{ (size_t)offsetof(struct pst_static, pst_vminfo_size),
(size_t)INCLMEM(pst_static, pst_vminfo_size),
sizeof(struct pst_vminfo),
"pst_vminfo" },
{ (size_t)offsetof(struct pst_static, pst_filedetails_size),
(size_t)INCLMEM(pst_static, pst_filedetails_size),
sizeof(struct pst_filedetails),
"pst_filedetails" },
{ (size_t)offsetof(struct pst_static, pst_socket_size),
(size_t)INCLMEM(pst_static, pst_socket_size),
sizeof(struct pst_socket),
"pst_socket" },
{ (size_t)offsetof(struct pst_static, pst_stream_size),
(size_t)INCLMEM(pst_static, pst_stream_size),
sizeof(struct pst_stream),
"pst_stream" },
{ (size_t)offsetof(struct pst_static, pst_mpathnode_size),
(size_t)INCLMEM(pst_static, pst_mpathnode_size),
sizeof(struct pst_mpathnode),
"pst_mpathnode" },
{ (size_t)offsetof(struct pst_static, pst_fileinfo2_size),
(size_t)INCLMEM(pst_static, pst_fileinfo2_size),
sizeof(struct pst_fileinfo2),
"pst_fileinfo2" },
};
#define NPSTATCK (sizeof(PstatCk) /sizeof(struct pstatck))
/*
* Local static variables
*/
static int HvRtPsfid = -1; /* "/" psfileid status:
* -1: not yet tested;
* 0: tested and unknown;
* 1: tested and known */
static struct psfileid RtPsfid; /* "/" psfileid */
/*
* Local function prototypes
*/
_PROTOTYPE(static void get_kernel_access,(void));
_PROTOTYPE(static void process_text,(struct pst_status *p));
_PROTOTYPE(static struct pst_fileinfo2 *read_files,(struct pst_status *p,
int *n));
_PROTOTYPE(static struct pst_status *read_proc,(int *n));
_PROTOTYPE(static struct pst_vm_status *read_vmreg,(struct pst_status *p,
int *n));
/*
* gather_proc_info() -- gather process information
*/
void
gather_proc_info()
{
short cckreg; /* conditional status of regular file
* checking:
* 0 = unconditionally check
* 1 = conditionally check */
short ckscko; /* socket file only checking status:
* 0 = none
* 1 = check only socket files,
* including TCP and UDP
* streams with eXPORT data,
* where supported */
int cwds, fd, *fds, fdsa, i, j, l, nf, np, rtds;
struct pst_fileinfo2 *f;
long flag;
KA_T ka, na;
MALLOC_S nb;
struct pst_status *p;
struct pst_filedetails pd;
struct pst_socket *s;
short pss, sf;
/*
* Compute current working and root directory statuses and the statuses of
* the first FDS_ALLOC_INIT FDs.
*/
if (Fand && Fdl) {
cwds = (ck_fd_status(CWD, -1) != 2) ? 0 : 1;
rtds = (ck_fd_status(RTD, -1) != 2) ? 0 : 1;
nb = (MALLOC_S)(sizeof(int) * FDS_ALLOC_INIT);
if (!(fds = (int *)malloc(nb))) {
(void) fprintf(stderr,
"%s: can't allocate %d FD status entries\n", Pn,
FDS_ALLOC_INIT);
Exit(1);
}
for (fdsa = 0; fdsa < FDS_ALLOC_INIT; fdsa++) {
if (Fand && Fdl)
fds[fdsa] = (ck_fd_status(NULL, fdsa) == 2) ? 1 : 0;
else
fds[fdsa] = 1;
}
} else {
cwds = rtds = 1;
fdsa = 0;
fds = (int *)NULL;
}
/*
* If only socket files have been selected, or socket files have been selected
* ANDed with other selection options, enable the skipping of regular files.
*
* If socket files and some process options have been selected, enable
* conditional skipping of regular file; i.e., regular files will be skipped
* unless they belong to a process selected by one of the specified options.
*/
if (Selflags & SELNW) {
/*
* Some network files selection options have been specified.
*/
if (Fand || !(Selflags & ~SELNW)) {
/*
* Selection ANDing or only network file options have been
* specified, so set unconditional skipping of regular files
* and socket file only checking.
*/
cckreg = 0;
ckscko = 1;
} else {
/*
* If ORed file selection options have been specified, or no ORed
* process selection options have been specified, enable
* unconditional file checking and clear socket file only checking.
*
* If only ORed process selection options have been specified,
* enable conditional file skipping and socket file only checking.
*/
if ((Selflags & SELFILE) || !(Selflags & SelProc))
cckreg = ckscko = 0;
else
cckreg = ckscko = 1;
}
} else {
/*
* No network file selection options were specified. Enable
* unconditional file checking and clear socket file only checking.
*/
cckreg = ckscko = 0;
}
/*
* Examine proc structures and their associated information.
*/
for (i = 0, p = read_proc(&np); i < np; i++, p++) {
if (!p->pst_stat || p->pst_stat == PS_ZOMBIE)
continue;
if (is_proc_excl((int)p->pst_pid, (int)p->pst_pgrp,
(UID_ARG)p->pst_uid, &pss, &sf))
continue;
/*
* Make sure the command name is NUL-terminated.
*/
p->pst_ucomm[PST_UCOMMLEN - 1] = '\0';
if (is_cmd_excl(p->pst_ucomm, &pss, &sf))
continue;
if (cckreg) {
/*
* If conditional checking of regular files is enabled, enable
* socket file only checking, based on the process' selection
* status.
*/
ckscko = (sf & SelProc) ? 0 : 1;
}
alloc_lproc((int)p->pst_pid, (int)p->pst_pgrp, (int)p->pst_ppid,
(UID_ARG)p->pst_uid, p->pst_ucomm, (int)pss, (int)sf);
Plf = (struct lfile *)NULL;
/*
* Save current working directory information.
*/
if (!ckscko && cwds
&& IS_PSFILEID(&p->pst_cdir) && (p->pst_cdir.psf_fileid > 0)
) {
alloc_lfile(CWD, -1);
if ((na = read_det(&p->pst_fid_cdir, p->pst_hi_fileid_cdir,
p->pst_lo_fileid_cdir,
p->pst_hi_nodeid_cdir,
p->pst_lo_nodeid_cdir, &pd)))
(void) process_finfo(&pd, &p->pst_fid_cdir,
&p->pst_cdir, na);
else {
(void) snpf(Namech, Namechl,
"can't read %s pst_filedetails%s%s", CWD,
errno ? ": " : "", errno ? strerror(errno) : "");
enter_nm(Namech);
}
if (Lf->sf)
link_lfile();
}
/*
* Save root directory information.
*/
if (!ckscko && rtds
&& IS_PSFILEID(&p->pst_rdir) && (p->pst_rdir.psf_fileid > 0)
) {
if (HvRtPsfid < 0)
(void) scanmnttab();
if (!HvRtPsfid
|| memcmp((void *)&RtPsfid, (void *)&p->pst_rdir,
sizeof(RtPsfid)))
{
alloc_lfile(RTD, -1);
if ((na = read_det(&p->pst_fid_rdir,
p->pst_hi_fileid_rdir,
p->pst_lo_fileid_rdir,
p->pst_hi_nodeid_rdir,
p->pst_lo_nodeid_rdir, &pd)))
(void) process_finfo(&pd, &p->pst_fid_rdir,
&p->pst_rdir, na);
else {
(void) snpf(Namech, Namechl,
"can't read %s pst_filedetails%s%s", RTD,
errno ? ": " : "",
errno ? strerror(errno) : "");
enter_nm(Namech);
}
if (Lf->sf)
link_lfile();
}
}
/*
* Print information on the text files.
*/
if (!ckscko)
(void) process_text(p);
/*
* Loop through user's files.
*/
for (j = 0, f = read_files(p, &nf); j < nf; j++, f++) {
fd = (int)f->psf_fd;
/*
* Check FD status and allocate local file space, as required.
*/
if (Fand && Fdl && fds) {
/*
* Check and update the FD status array.
*/
if (fd >= fdsa) {
for (l = fdsa; l <= fd; l += FDS_ALLOC_INCR)
;
nb = (MALLOC_S)(l * sizeof(int));
if (!(fds = (int *)realloc((MALLOC_P *)fds, nb))) {
(void) fprintf(stderr,
"%s: can't reallocate %d FD status entries\n",
Pn, l);
Exit(1);
}
while (fdsa < l) {
fds[fdsa] = (ck_fd_status(NULL, fdsa) == 2) ? 1 : 0;
fdsa++;
}
}
if (!fds[fd])
continue;
}
alloc_lfile(NULL, (int)f->psf_fd);
/*
* Construct access code.
*/
if ((flag = (long)(f->psf_flag & ~PS_FEXCLOS))
== (long)PS_FRDONLY)
Lf->access = 'r';
else if (flag == (long)PS_FWRONLY)
Lf->access = 'w';
else
Lf->access = 'u';
#if defined(HASFSTRUCT)
/*
* Save file structure values.
*/
if (Fsv & FSV_CT) {
Lf->fct = (long)f->psf_count;
Lf->fsv |= FSV_CT;
}
if (Fsv & FSV_FA) {
ka = (((KA_T)(f->psf_hi_fileid & 0xffffffff) << 32)
| (KA_T)(f->psf_lo_fileid & 0xffffffff));
if ((Lf->fsa = ka))
Lf->fsv |= FSV_FA;
}
if (Fsv & FSV_FG) {
Lf->ffg = flag;
Lf->fsv |= FSV_FG;
}
Lf->pof = (long)(f->psf_flag & PS_FEXCLOS);
#endif /* defined(HASFSTRUCT) */
/*
* Save file offset. _PSTAT64 should alwaus be defined, but just
* to be safe, check for it.
*/
#if defined(_PSTAT64)
Lf->off = (SZOFFTYPE)f->_PSF_OFFSET64;
#else /* !defined(_PSTAT64) */
Lf->off = (SZOFFTYPE)f->psf_offset;
#endif /* defined(_PSTAT64) */
/*
* Process the file by its type.
*/
switch (f->psf_ftype) {
case PS_TYPE_VNODE:
if (ckscko || Selinet)
break;
if ((na = read_det(&f->psf_fid, f->psf_hi_fileid,
f->psf_lo_fileid, f->psf_hi_nodeid,
f->psf_lo_nodeid, &pd)))
(void) process_finfo(&pd, &f->psf_fid, &f->psf_id, na);
else {
(void) snpf(Namech, Namechl,
"can't read pst_filedetails%s%s",
errno ? ": " : "",
errno ? strerror(errno) : "");
enter_nm(Namech);
}
break;
case PS_TYPE_SOCKET:
switch (f->psf_subtype) {
case PS_SUBTYPE_SOCK:
(void) process_socket(f, (struct pst_socket *)NULL);
break;
case PS_SUBTYPE_SOCKSTR:
if ((s = read_sock(f)))
(void) process_socket(f, s);
else
(void) process_stream(f, (int)ckscko);
break;
default:
(void) snpf(Namech, Namechl,
"unknown socket sub-type: %d", (int)f->psf_subtype);
enter_nm(Namech);
}
break;
case PS_TYPE_STREAMS:
(void) process_stream(f, (int)ckscko);
break;
case PS_TYPE_UNKNOWN:
(void) snpf(Lf->type, sizeof(Lf->type), "UNKN");
(void) enter_nm("no more information");
break;
case PS_TYPE_UNSP:
(void) snpf(Lf->type, sizeof(Lf->type), "UNSP");
(void) enter_nm("no more information");
break;
case PS_TYPE_LLA:
(void) snpf(Lf->type, sizeof(Lf->type), "LLA");
(void) enter_nm("no more information");
break;
}
if (Lf->sf)
link_lfile();
}
/*
* Examine results.
*/
if (examine_lproc())
return;
}
}
/*
* get_kernel_access() -- access the required information in the kernel
*/
static void
get_kernel_access()
{
int err = 0;
int i;
struct pst_static pst;
_T_LONG_T *szp;
/*
* Check the kernel version.
*/
(void) ckkv("HP-UX", LSOF_VSTR, (char *)NULL, (char *)NULL);
/*
* Check PSTAT support. First make sure we can read pst_static up through
* its pst_static_size member. If not, quit. If we can, read the full
* pst_static structure.
*/
if (pstat_getstatic(&pst, (size_t)INCLMEM(pst_static, pst_static_size),
1, 0) != 1)
{
(void) fprintf(stderr,
"%s: FATAL: can't determine PSTAT static size: %s\n",
Pn, strerror(errno));
Exit(1);
}
if (pstat_getstatic(&pst, (size_t)pst.pst_static_size, 1, 0) != 1) {
(void) fprintf(stderr,
"%s: FATAL: can't read %ld bytes of pst_static\n",
Pn, (long)pst.pst_static_size);
Exit(1);
}
/*
* Check all the pst_static members defined in PstatCk[].
*/
for (i = 0; i < NPSTATCK; i++) {
if (pst.pst_static_size < PstatCk[i].msz) {
(void) fprintf(stderr,
"%s: FATAL: pst_static doesn't contain %s_size\n",
Pn, PstatCk[i].sn);
err = 1;
continue;
}
szp = (_T_LONG_T *)(((char *)&pst) + PstatCk[i].moff);
if (*szp < PstatCk[i].ssz) {
(void) fprintf(stderr,
"%s: FATAL: %s_size should be: %llu; is %llu\n",
Pn, PstatCk[i].sn, (unsigned long long)PstatCk[i].ssz,
(unsigned long long)*szp);
err = 1;
}
}
/*
* Save the clone major device number, if pst_static is big enough to hold it.
*/
if (pst.pst_static_size >= (size_t)INCLMEM(pst_static, clonemajor)) {
CloneMaj = pst.clonemajor;
HaveCloneMaj = 1;
}
if (!err)
return;
Exit(1);
}
/*
* initialize() -- perform all initialization
*/
void
initialize()
{
get_kernel_access();
}
/*
* process_text() -- process text access information
*/
static void
process_text(p)
struct pst_status *p; /* pst_status for process */
{
int i, j, nr, ntvu;
int meme = 0;
static int mems = -1;
KA_T na;
MALLOC_S nb;
static int ntva;
struct pst_vm_status *rp;
static int txts = -1;
struct txtvm {
char *fd;
struct pst_fid opfid;
struct psfileid psfid;
KA_T na;
struct pst_filedetails pd;
};
static struct txtvm *tv = (struct txtvm *)NULL;
/*
* Get and remember "mem" and "txt" FD statuses.
*/
if (mems < 0) {
if (Fand && Fdl)
mems = (ck_fd_status("mem", -1) == 2) ? 1 : 0;
else
mems = 1;
}
if (txts < 0) {
if (Fand && Fdl)
txts = (ck_fd_status("txt", -1) == 2) ? 1 : 0;
else
txts = 1;
}
if (!mems && !txts)
return;
/*
* Pre-allocate sufficient tv[] space for text file.
*/
if (!tv) {
ntva = TXTVMINCR;
nb = (MALLOC_S)(ntva * sizeof(struct txtvm));
if (!(tv = (struct txtvm *)malloc(nb))) {
no_txtvm_space:
(void) fprintf(stderr,
"%s: no memory for text and VM info array; PID: %d\n",
Pn, (int)p->pst_pid);
Exit(1);
}
}
/*
* Enter text file in tv[], if possible.
*/
if (txts && IS_PSFILEID(&p->pst_text) && (p->pst_text.psf_fileid > 0))
{
if ((na = read_det(&p->pst_fid_text, p->pst_hi_fileid_text,
p->pst_lo_fileid_text, p->pst_hi_nodeid_text,
p->pst_lo_nodeid_text, &tv[0].pd)))
{
tv[0].fd = "txt";
tv[0].na = na;
tv[0].opfid = p->pst_fid_text;
tv[0].psfid = p->pst_text;
ntvu = 1;
} else {
alloc_lfile("txt", -1);
(void) snpf(Namech, Namechl,
"can't read txt pst_filedetails%s%s",
errno ? ": " : "", errno ? strerror(errno) : "");
enter_nm(Namech);
if (Lf->sf)
link_lfile();
ntvu = 0;
}
} else
ntvu = 0;
/*
* Get unique VM regions.
*/
if (mems) {
for (i = 0, rp = read_vmreg(p, &nr); (i < nr); i++, rp++) {
/*
* Skip duplicate regions.
*/
for (j = 0; j < ntvu; j++) {
if (memcmp((void *)&rp->pst_id, (void *)&tv[j].psfid,
sizeof(struct psfileid))
== 0)
break;
}
if (j < ntvu)
continue;
/*
* Make sure there's tv[] space for this region.
*/
if (ntvu >= ntva) {
ntva += TXTVMINCR;
nb = (MALLOC_S)(ntva * sizeof(struct txtvm));
if (!(tv = (struct txtvm *)realloc((MALLOC_P *)tv, nb)))
goto no_txtvm_space;
}
/*
* See if we can read the file details for this region.
*/
if ((na = read_det(&rp->pst_fid, rp->pst_hi_fileid,
rp->pst_lo_fileid, rp->pst_hi_nodeid,
rp->pst_lo_nodeid, &tv[ntvu].pd)))
{
tv[ntvu].fd = "mem";
tv[ntvu].na = na;
tv[ntvu].opfid = rp->pst_fid;
tv[ntvu].psfid = rp->pst_id;
ntvu++;
} else if (!meme) {
alloc_lfile("mem", -1);
(void) snpf(Namech, Namechl,
"can't read mem pst_filedetails%s%s",
errno ? ": " : "", errno ? strerror(errno) : "");
enter_nm(Namech);
if (Lf->sf)
link_lfile();
meme = 1;
}
}
}
/*
* Process information for unique regions.
*/
for (i = 0; i < ntvu; i++) {
alloc_lfile(tv[i].fd, -1);
(void) process_finfo(&tv[i].pd, &tv[i].opfid, &tv[i].psfid,
tv[i].na);
if (Lf->sf)
link_lfile();
}
}
/*
* read_det() -- read the pst_filedetails structure
*/
KA_T
read_det(ki, hf, lf, hn, ln, pd)
struct pst_fid *ki; /* kernel file ID */
uint32_t hf; /* high file ID bits */
uint32_t lf; /* low file ID bits */
uint32_t hn; /* high node ID bits */
uint32_t ln; /* low node ID bits */
struct pst_filedetails *pd; /* details receiver */
{
KA_T na;
errno = 0;
na = (KA_T)(((KA_T)(hn & 0xffffffff) << 32) | (KA_T)(ln & 0xffffffff));
if (pstat_getfiledetails(pd, sizeof(struct pst_filedetails), ki) <= 0
|| hf != pd->psfd_hi_fileid || lf != pd->psfd_lo_fileid
|| hn != pd->psfd_hi_nodeid || ln != pd->psfd_lo_nodeid)
return((KA_T)0);
return(na);
}
/*
* read_files() -- read the file descriptor information for a process
*/
static struct pst_fileinfo2 *
read_files(p, n)
struct pst_status *p; /* pst_status for the process */
int *n; /* returned fi[] entry count */
{
size_t ec;
static struct pst_fileinfo2 *fi = (struct pst_fileinfo2 *)NULL;
MALLOC_S nb;
int nf = 0;
static int nfa = 0;
int rc;
static size_t sz = sizeof(struct pst_fileinfo2);
/*
* Read the pst_fileinfo2 information for all files of the process
* into fi[].
*/
do {
if (nf >= nfa) {
/*
* Increase the size of fi[].
*/
nfa += FINFOINCR;
nb = (MALLOC_S)(nfa * sizeof(struct pst_fileinfo2));
if (!fi)
fi = (struct pst_fileinfo2 *)malloc(nb);
else
fi = (struct pst_fileinfo2 *)realloc((MALLOC_P *)fi, nb);
if (!fi) {
(void) fprintf(stderr,
"%s: can't allocate %d bytes for pst_filinfo\n",
Pn, nb);
Exit(1);
}
}
/*
* Read the next block of pst_fileinfo2 structures.
*/
ec = (size_t)(nfa - nf);
if ((rc = pstat_getfile2(fi + nf, sz, ec, nf, p->pst_pid)) > 0) {
nf += rc;
if (rc < (int)ec)
rc = 0;
}
} while (rc > 0);
*n = nf;
return(fi);
}
/*
* read_proc() -- read process table status information
*/
static struct pst_status *
read_proc(n)
int *n; /* returned ps[] entry count */
{
size_t el;
int i = 0;
MALLOC_S nb;
int np = 0;
static int npa = 0;
static struct pst_status *ps = (struct pst_status *)NULL;
int rc;
size_t sz = sizeof(struct pst_status);
/*
* Read the pst_status information for all processes into ps[].
*/
do {
if (np >= npa) {
/*
* Increase the size of ps[].
*/
npa += PSTATINCR;
nb = (MALLOC_S)(npa * sizeof(struct pst_status));
if (!ps)
ps = (struct pst_status *)malloc(nb);
else
ps = (struct pst_status *)realloc((MALLOC_P *)ps, nb);
if (!ps) {
ps_alloc_error:
(void) fprintf(stderr,
"%s: can't allocate %d bytes for pst_status table\n",
Pn, nb);
Exit(1);
}
}
/*
* Read the next block of pst_status structures.
*/
el = (size_t)(npa - np);
if ((rc = pstat_getproc(ps + np, sz, el, i)) > 0) {
np += rc;
i = (ps + np - 1)->pst_idx + 1;
if (rc < el)
rc = 0;
}
} while (rc > 0);
/*
* Reduce ps[] to a minimum, unless repeat mode is in effect.
*/
if (!RptTm && ps && np && (np < npa)) {
nb = (MALLOC_S)(np * sizeof(struct pst_status));
if (!(ps = (struct pst_status *)realloc((MALLOC_P *)ps, nb)))
goto ps_alloc_error;
}
*n = np;
return(ps);
}
/*
* read_vmreg() -- read info about the VM regions of a process
*/
static struct pst_vm_status *
read_vmreg(p, n)
struct pst_status *p; /* pst_status for process */
int *n; /* returned region count */
{
size_t ec = (size_t)p->pst_pid;
MALLOC_S nb;
int nr, rx;
static int nra = 0;
struct pst_vm_status *rp;
static struct pst_vm_status *reg = (struct pst_vm_status *)NULL;
size_t sz = sizeof(struct pst_vm_status);
/*
* Read all VM region information for the process.
*/
for (nr = rx = 0;; rx++) {
if (nr >= nra) {
/*
* Increase the region table size.
*/
nra += VMREGINCR;
nb = (MALLOC_S)(nra * sizeof(struct pst_vm_status));
if (!reg)
reg = (struct pst_vm_status *)malloc(nb);
else
reg = (struct pst_vm_status *)realloc((MALLOC_P *)reg, nb);
if (!reg) {
(void) fprintf(stderr,
"%s: can't allocate %d bytes for pst_vm_status\n",
Pn, nb);
Exit(1);
}
}
/*
* Read the pst_vm_status structure for the next region.
*/
rp = reg + nr;
if (pstat_getprocvm(rp, sz, ec, rx) != 1)
break;
if (IS_PSFILEID(&rp->pst_id) && (rp->pst_id.psf_fileid > 0))
nr++;
}
*n = nr;
return(reg);
}
/*
* scanmnttab() -- scan mount table
*/
extern void
scanmnttab()
{
struct mounts *mp;
/*
* Scan the mount table to identify NFS file systems and form the psfileid
* for "/".
*
* This function allows the mount table scan to be deferred until its
* information is needed.
*/
if ((HvRtPsfid >= 0) && (HasNFS >= 0))
return;
(void) memset((void *)&RtPsfid, 0, sizeof(RtPsfid));
for (HasNFS = HvRtPsfid = 0, mp = readmnt(); mp; mp = mp->next) {
if (mp->MOUNTS_FSTYPE
&& (strcmp(mp->MOUNTS_FSTYPE, MNTTYPE_NFS) == 0
|| strcmp(mp->MOUNTS_FSTYPE, MNTTYPE_NFS3) == 0)) {
HasNFS = 1;
mp->is_nfs = 1;
} else
mp->is_nfs = 0;
if (!HvRtPsfid && !strcmp(mp->dir, "/")) {
HvRtPsfid = 1;
RtPsfid.psf_fsid.psfs_id = mp->dev;
RtPsfid.psf_fsid.psfs_type = mp->MOUNTS_STAT_FSTYPE;
RtPsfid.psf_fileid = mp->inode;
}
}
}
|