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 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
|
/*
* Configurable ps-like program.
* Linux-specific process information collection routines.
*
* Copyright (c) 2014 David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*/
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include "ips.h"
#define PROCDIR "/proc" /* path for proc filesystem */
#define BEGNAMECHAR '(' /* begin char for program name */
#define ENDNAMECHAR ')' /* end char for program name */
/*
* A generous size of a buffer which will hold the /proc file names
* that we are interested in. These file names only contain small fixed
* components along with one or two possible integer values.
*/
#define PROC_FILE_LEN 80
/*
* Static variables.
*/
static DIR * procDir; /* opendir for /proc */
/*
* Local procedures.
*/
static void ExamineProcessId(pid_t pid, pthread_t tid);
static void GetProcessCommandLine(PROC * proc);
static void GetProcessEnvironment(PROC * proc);
static void GetProcessOpenFileCount(PROC * proc);
static void GetProcessCurrentDirectory(PROC * proc);
static void GetProcessRootDirectory(PROC * proc);
static void GetProcessExecInode(PROC * proc);
static void GetProcessStdioDescriptors(PROC * proc);
static void GetProcessWaitSymbol(PROC * proc);
static void ScanThreads(PROC * proc);
static BOOL IsCopyAllowed(const PROC * proc);
static BOOL ReadLinkPath(const char * name, char ** retpath,
int * retpathlength);
/*
* Initialize things that we need up front for process status collection.
* Returns TRUE if successful.
*/
BOOL
InitializeProcessData(void)
{
CollectStaticSystemInfo();
if (useUserNames)
CollectUserNames();
if (useGroupNames)
CollectGroupNames();
if (useDeviceNames)
CollectDeviceNames();
/*
* Open the /proc directory so that the names in it can be found.
*/
procDir = opendir(PROCDIR);
if (procDir == NULL)
{
fprintf(stderr, "Cannot open %s\n", PROCDIR);
return FALSE;
}
ancientFlag = TRUE;
return TRUE;
}
/*
* Collect system information that we need.
* This information collected here is static.
*/
void
CollectStaticSystemInfo(void)
{
const char * cp;
int fd;
int cc;
char buf[256];
/*
* Get the number of ticks per second.
*/
ticksPerSecond = sysconf(_SC_CLK_TCK);
if (ticksPerSecond <= 0)
ticksPerSecond = 100;
/*
* Get the page size.
*/
pageSize = sysconf(_SC_PAGESIZE);
if (pageSize <= 0)
pageSize = 4096;
/*
* Collect the amount of memory on the system.
*/
fd = open(PROCDIR "/meminfo", O_RDONLY);
if (fd < 0)
return;
cc = read(fd, buf, sizeof(buf) - 1);
(void) close(fd);
if (cc <= 0)
return;
buf[cc] = '\0';
cp = strstr(buf, "MemTotal:");
if (cp == NULL)
return;
cp += 9;
totalMemoryClicks = GetDecimalNumber(&cp) / (pageSize / 1024);
/*
* Get the starting uptime and time of day.
* This will be used to determine the age of processes.
*/
startUptime = GetUptime();
startTime = time(NULL);
}
/*
* Collect system information that we need.
* This information collected here is dynamic.
* For now all we need is the process and thread counts.
*/
void
CollectDynamicSystemInfo(void)
{
const struct dirent * dp;
const char * cp;
int fd;
int cc;
char buf[256];
/*
* Read the load average information.
* This has a format like: 0.03 0.06 0.07 1/214 12496
*/
fd = open(PROCDIR "/loadavg", O_RDONLY);
if (fd < 0)
return;
cc = read(fd, buf, sizeof(buf) - 1);
(void) close(fd);
if (cc <= 0)
return;
buf[cc] = '\0';
/*
* The number of threads on the system follows a slash.
*/
cp = strchr(buf, '/');
if (cp == 0)
return;
cp++;
threadTotalCount = GetDecimalNumber(&cp);
/*
* Get the number of processes by counting the number of
* filenames in the /proc directory which are numeric.
*/
procTotalCount = 0;
seekdir(procDir, 0);
while ((dp = readdir(procDir)) != NULL)
{
cp = dp->d_name;
while (isDigit(*cp))
cp++;
if (*cp == '\0')
procTotalCount++;
}
}
/*
* Get the uptime of the system in jiffies. Since the /proc data format
* is floating point seconds, we have to convert it.
* Returns 0 if something is wrong.
*/
ULONG
GetUptime(void)
{
const char * cp;
int fd;
int cc;
ULONG intVal;
ULONG fracVal;
ULONG fracScale;
char buf[128];
fd = open(PROCDIR "/uptime", O_RDONLY);
if (fd < 0)
return 0;
cc = read(fd, buf, sizeof(buf) - 1);
(void) close(fd);
if (cc <= 0)
return 0;
buf[cc] = '\0';
cp = buf;
while (isBlank(*cp))
cp++;
intVal = 0;
fracVal = 0;
fracScale = 1;
while (isDigit(*cp))
intVal = intVal * 10 + *cp++ - '0';
if (*cp == '.')
cp++;
while (isDigit(*cp))
{
fracVal = fracVal * 10 + *cp++ - '0';
fracScale = fracScale * 10;
}
if ((*cp != ' ') && (*cp != '\n'))
return 0;
return (intVal * ticksPerSecond) + ((fracVal * ticksPerSecond) / fracScale);
}
/*
* Scan all processes and set their new state.
*/
void
ScanProcesses(void)
{
const struct dirent * dp;
const char * name;
pid_t pid;
int i;
UpdateTimes();
CollectDynamicSystemInfo();
/*
* If we require our own process information, then get that now.
*/
if (useSelf)
ExamineProcessId(myPid, NO_THREAD_ID);
/*
* If there were no specific pids given, then scan them all by
* reading the numeric entries from the "/proc" directory.
* Otherwise, examine just the specified processes.
*/
if (pidCount == 0)
{
seekdir(procDir, 0);
while ((dp = readdir(procDir)) != NULL)
{
name = dp->d_name;
pid = 0;
while (isDigit(*name))
pid = pid * 10 + *name++ - '0';
if (*name)
continue;
if ((pid != myPid) || !useSelf)
ExamineProcessId(pid, NO_THREAD_ID);
}
}
else
{
for (i = 0; i < pidCount; i++)
{
if ((pidList[i] != myPid) || !useSelf)
ExamineProcessId(pidList[i], NO_THREAD_ID);
}
}
RemoveDeadProcesses();
SortProcesses();
UpdateProcessCounts();
ancientFlag = FALSE;
}
/*
* Collect data about the specified process and thread id.
* A thread id of NO_THREAD_ID collects the main process data,
* possibly along with the process data of all of its threads.
* This allocates a new PROC structure if necessary.
* If the process is successfully examined, the valid flag is set.
*/
static void
ExamineProcessId(pid_t pid, pthread_t tid)
{
PROC * proc;
int fd;
int cc;
int i;
char * begName;
char * endName;
const char * cp;
long ticksFromStart;
long secondsFromStart;
BOOL okSkip;
struct stat statBuf;
char buf[512];
char name[PROC_FILE_LEN];
proc = FindProcess(pid, tid);
proc->isValid = FALSE;
proc->isShown = FALSE;
okSkip = ((pid != myPid) || !useSelf);
if (okSkip && noSelf && (pid == myPid))
return;
if (proc->isThread)
sprintf(name, "%s/%ld/task/%ld/stat", PROCDIR, (long) pid, (long) tid);
else
sprintf(name, "%s/%ld/stat", PROCDIR, (long) pid);
fd = open(name, O_RDONLY);
if (fd < 0)
{
if (errno != ENOENT)
perror(name);
return;
}
if (fstat(fd, &statBuf) < 0)
{
(void) close(fd);
return;
}
proc->deathTime = 0;
if (okSkip)
{
if (myProcs && (statBuf.st_uid != myUid))
{
(void) close(fd);
return;
}
if (noRoot && (statBuf.st_uid == 0))
{
(void) close(fd);
return;
}
if ((userCount > 0))
{
for (i = 0; i < userCount; i++)
{
if (statBuf.st_uid == userList[i])
break;
}
if (i == userCount)
{
(void) close(fd);
return;
}
}
if ((groupCount > 0))
{
for (i = 0; i < groupCount; i++)
{
if (statBuf.st_gid == groupList[i])
break;
}
if (i == groupCount)
{
(void) close(fd);
return;
}
}
}
proc->uid = statBuf.st_uid;
proc->gid = statBuf.st_gid;
/*
* Read the process status into a buffer.
*/
cc = read(fd, buf, sizeof(buf));
if (cc < 0)
{
(void) close(fd);
return;
}
(void) close(fd);
if (cc == sizeof(buf))
{
fprintf(stderr, "status buffer overflow");
return;
}
buf[cc] = '\0';
/*
* The program name begins after a left parenthesis.
* Break the status string into two at that point.
*/
begName = strchr(buf, BEGNAMECHAR);
if (begName == NULL)
{
fprintf(stderr, "Cannot find start of program name\n");
return;
}
*begName++ = '\0';
/*
* The program name ends after a right parenthesis.
* But, look for the rightmost one in case the program name
* itself contains a parenthesis!
*/
endName = strchr(begName, ENDNAMECHAR);
if (endName == NULL)
{
fprintf(stderr, "Cannot find end of program name\n");
return;
}
while ((cp = strchr(endName + 1, ENDNAMECHAR)) != NULL)
endName = begName + (cp - begName);
MakePrintable(begName, endName - begName);
*endName++ = '\0';
strncpy(proc->program, begName, MAX_PROGRAM_LEN);
proc->program[MAX_PROGRAM_LEN] = '\0';
/*
* Find the process state character, and then parse the numbers on
* the rest of the line to collect the remaining state information.
*/
cp = endName;
while (isBlank(*cp))
cp++;
if ((*cp == '\0') || (*cp == '\n') || isDigit(*cp))
{
fprintf(stderr, "Bad proc state character\n");
return;
}
proc->state = *cp++;
proc->states[0] = proc->state;
proc->states[1] = '\0';
proc->parentPid = GetDecimalNumber(&cp);
proc->processGroup = GetDecimalNumber(&cp);
proc->sessionId = GetDecimalNumber(&cp);
proc->ttyDevice = GetDecimalNumber(&cp);
proc->ttyProcessGroup = GetDecimalNumber(&cp);
proc->flags = GetDecimalNumber(&cp);
proc->minorFaults = GetDecimalNumber(&cp);
proc->childMinorFaults = GetDecimalNumber(&cp);
proc->majorFaults = GetDecimalNumber(&cp);
proc->childMajorFaults = GetDecimalNumber(&cp);
proc->userRunTime = GetDecimalNumber(&cp);
proc->systemRunTime = GetDecimalNumber(&cp);
proc->childUserRunTime = GetDecimalNumber(&cp);
proc->childSystemRunTime = GetDecimalNumber(&cp);
proc->priority = GetDecimalNumber(&cp);
proc->nice = GetDecimalNumber(&cp);
proc->threadCount = GetDecimalNumber(&cp);
proc->itRealValue = GetDecimalNumber(&cp);
proc->startTimeTicks = GetDecimalNumber(&cp);
proc->virtualSize = GetDecimalNumber(&cp);
proc->rss = GetDecimalNumber(&cp);
proc->rssLimit = GetDecimalNumber(&cp);
proc->startCode = GetDecimalNumber(&cp);
proc->endCode = GetDecimalNumber(&cp);
proc->startStack = GetDecimalNumber(&cp);
proc->esp = GetDecimalNumber(&cp);
proc->eip = GetDecimalNumber(&cp);
proc->signal = GetDecimalNumber(&cp);
proc->sigBlock = GetDecimalNumber(&cp);
proc->sigIgnore = GetDecimalNumber(&cp);
proc->sigCatch = GetDecimalNumber(&cp);
proc->waitChan = GetDecimalNumber(&cp);
proc->pagesSwapped = GetDecimalNumber(&cp);
proc->childPagesSwapped = GetDecimalNumber(&cp);
proc->exitSignal = GetDecimalNumber(&cp);
proc->processor = GetDecimalNumber(&cp);
proc->realTimePriority = GetDecimalNumber(&cp);
proc->policy = GetDecimalNumber(&cp);
/*
* Convert the processes start time into clock time and age.
* Get the number of ticks after we started when the specified
* process started and convert that to elapsed seconds.
* This can be positive or negative according to whether the
* process started before or after us.
*/
ticksFromStart = proc->startTimeTicks - startUptime;
if (ticksFromStart >= 0)
secondsFromStart = ticksFromStart / ticksPerSecond;
else
secondsFromStart = -((-ticksFromStart) / ticksPerSecond);
/*
* Add the elapsed seconds to the clock time when we started
* to get the clock time the process started.
*/
proc->startTimeClock = startTime + secondsFromStart;
/*
* If the process is newly seen then save its starting runtime.
* This is used for the cpu percentage calculation.
*/
if (proc->isNew)
proc->firstCpuTime = proc->userRunTime + proc->systemRunTime;
/*
* See if the process has changed state, and is therefore active.
*/
CheckActiveProcess(proc);
/*
* Get several pieces of extra data, but only if the process
* has changed state, and only if these data are required.
* However, get the extra data always if it is older than
* the specified sync time.
*/
if (proc->isChanged ||
((proc->lastSyncTime + syncTime) <= currentTime))
{
proc->lastSyncTime = currentTime;
GetProcessOpenFileCount(proc);
GetProcessStdioDescriptors(proc);
GetProcessCurrentDirectory(proc);
GetProcessRootDirectory(proc);
GetProcessExecInode(proc);
GetProcessCommandLine(proc);
GetProcessEnvironment(proc);
GetProcessWaitSymbol(proc);
}
proc->liveCounter = liveCounter;
proc->isValid = TRUE;
if (IsShownProcess(proc))
proc->isShown = TRUE;
/*
* If we are showing or using threads and this process has more than
* one thread then collect information on them.
*/
if (showThreads || useThreads)
ScanThreads(proc);
/*
* Store the states string for threads if any.
*/
BuildStates(proc);
}
/*
* Scan the list of threads for the indicated process.
*/
static void
ScanThreads(PROC * proc)
{
DIR * dir;
const struct dirent * dp;
const char * cp;
pthread_t tid;
char name[PROC_FILE_LEN];
/*
* If this is a thread or there aren't multiple threads then
* do nothing.
*/
if (proc->isThread || (proc->threadCount <= 1))
return;
/*
* Read the list of thread ids from the task directory of the
* main process and examine each one.
*/
sprintf(name, "%s/%ld/task", PROCDIR, (long) proc->pid);
dir = opendir(name);
if (dir == NULL)
return;
while ((dp = readdir(dir)) != NULL)
{
cp = dp->d_name;
tid = 0;
while (isDigit(*cp))
tid = tid * 10 + (*cp++ - '0');
if (*cp == '\0')
ExamineProcessId(proc->pid, tid);
}
closedir(dir);
}
/*
* Get the wait channel symbol name for the process.
*/
static void
GetProcessWaitSymbol(PROC * proc)
{
int fd;
int len;
char name[PROC_FILE_LEN];
/*
* If the wait channel is 0 or is not used then
* set an empty symbol name.
*/
if (!useWaitChan || (proc->waitChan == 0))
{
proc->waitChanSymbol[0] = '\0';
return;
}
/*
* Open the wait channel file and read it into a buffer
* including trying to read one extra character.
*/
sprintf(name, "%s/%ld/wchan", PROCDIR, (long) proc->pid);
fd = open(name, O_RDONLY);
len = -1;
if (fd >= 0)
{
len = read(fd, proc->waitChanSymbol, MAX_WCHAN_LEN + 1);
(void) close(fd);
}
/*
* If the symbol wasn't found then store a dash for the symbol.
*/
if (len < 0)
{
proc->waitChanSymbol[0] = '-';
proc->waitChanSymbol[1] = '\0';
return;
}
/*
* If we read one more character than our limit, then we missed
* some of the symbol name line, so flag that by replacing the
* last character of the allowed length with a vertical bar.
*/
if (len > MAX_WCHAN_LEN)
{
len = MAX_WCHAN_LEN;
proc->waitChanSymbol[MAX_WCHAN_LEN - 1] = '|';
}
/*
* Null terminate the wait symbol name and make it printable.
*/
proc->waitChanSymbol[len] = '\0';
MakePrintable(proc->waitChanSymbol, len);
}
/*
* Get the command line for the specified process.
* If there isn't one, then use the program name as the command line,
* surrounded by parenthesis. If the command line is small, then it
* fits within the proc structure, otherwise we have to malloc it.
* Threads copy the command line string from the main process.
*/
static void
GetProcessCommandLine(PROC * proc)
{
int fd;
int len;
char name[PROC_FILE_LEN];
char buffer[MAX_COMMAND_LEN + 2];
len = 0;
if (!useCommand)
{
proc->hasCommand = FALSE;
proc->commandLength = 0;
proc->command[0] = '\0';
return;
}
proc->hasCommand = TRUE;
/*
* If we are a thread process and have an owner then copy the
* command from the owner structure.
*/
if (IsCopyAllowed(proc))
{
SetCommandLine(proc, proc->owner->command, proc->owner->commandLength);
return;
}
/*
* Open the command line file and read it into a large buffer,
* including trying to read one extra character.
*/
sprintf(name, "%s/%ld/cmdline", PROCDIR, (long) proc->pid);
fd = open(name, O_RDONLY);
if (fd >= 0)
{
len = read(fd, buffer, MAX_COMMAND_LEN + 1);
(void) close(fd);
}
/*
* If we could not get the command line, or if there was none
* there, then use the program name surrounded by parenthesis.
* Remember that there is no real command line for user tests.
*/
if ((fd < 0) || (len <= 0))
{
proc->hasCommand = FALSE;
len = strlen(proc->program);
buffer[0] = '(';
memcpy(&buffer[1], proc->program, len);
buffer[len + 1] = ')';
len += 2;
}
/*
* If we read one more character than our limit, then we missed
* some of the command line, so flag that by replacing the last
* character of the allowed length with a vertical bar.
*/
if (len > MAX_COMMAND_LEN)
{
len = MAX_COMMAND_LEN;
buffer[MAX_COMMAND_LEN - 1] = '|';
}
/*
* Null terminate the command line and make it printable.
*/
buffer[len] = '\0';
MakePrintable(buffer, len);
/*
* Store the command line into the structure.
*/
SetCommandLine(proc, buffer, len);
}
/*
* Get the environment for the specified process.
* This could be very large, so it is allocated dynamically and we
* attempt to share strings among processes. Threads copy the
* environment string value from the main process.
*/
static void
GetProcessEnvironment(PROC * proc)
{
int fd;
int len;
char name[PROC_FILE_LEN];
char buffer[MAX_ENVIRON_LEN + 2];
if (!useEnvironment)
return;
/*
* If we are a thread process and have an owner then copy the
* environment from the owner structure.
*/
if (IsCopyAllowed(proc))
{
SetSharedString(&proc->environment, &proc->environmentLength,
proc->owner->environment, proc->owner->environmentLength);
return;
}
/*
* Open the environment file and read it into a large buffer,
* including trying to read one extra character.
*/
sprintf(name, "%s/%ld/environ", PROCDIR, (long) proc->pid);
fd = open(name, O_RDONLY);
len = 0;
if (fd >= 0)
{
len = read(fd, buffer, MAX_ENVIRON_LEN + 1);
(void) close(fd);
}
/*
* If we could not open the file, or if there was nothing there,
* then free any old environment string and point to a null string.
*/
if ((fd < 0) || (len <= 0))
{
FreeSharedString(proc->environment);
proc->environment = emptyString;
proc->environmentLength = 0;
return;
}
/*
* If we read one more character than our limit, then we missed
* some of the environment, so flag that by replacing the last
* character of the allowed length with a vertical bar.
*/
if (len > MAX_ENVIRON_LEN)
{
len = MAX_ENVIRON_LEN;
buffer[MAX_ENVIRON_LEN - 1] = '|';
}
/*
* Null terminate the environment string and make it printable.
*/
buffer[len] = '\0';
MakePrintable(buffer, len);
/*
* Store the environment line into the structure.
*/
SetSharedString(&proc->environment, &proc->environmentLength,
buffer, len);
}
/*
* Get the number of open files for the process.
* This is expensive and so is only gotten if the column is actually in use.
* The permissions only allow this information to be gotten for the same user
* id or if you are running as root.
*/
static void
GetProcessOpenFileCount(PROC * proc)
{
DIR * dir;
const struct dirent * dp;
const char * cp;
int count;
char name[PROC_FILE_LEN];
proc->openFiles = -1;
if (!useOpenFiles)
return;
/*
* If we are a thread process and have an owner then copy the
* open file count from the main process.
*/
if (IsCopyAllowed(proc))
{
proc->openFiles = proc->owner->openFiles;
return;
}
/*
* Open the fd directory in the process status and count the
* number of numeric file names.
*/
sprintf(name, "%s/%ld/fd", PROCDIR, (long) proc->pid);
dir = opendir(name);
if (dir == NULL)
return;
count = 0;
while ((dp = readdir(dir)) != NULL)
{
cp = dp->d_name;
while (isDigit(*cp))
cp++;
if (*cp == '\0')
count++;
}
closedir(dir);
proc->openFiles = count;
}
/*
* Get the current working directory of the specified process.
* This is expensive and so is only gotten if the column is actually in use.
* The permissions only allow this information to be gotten for the same user
* id or if you are running as root.
*/
static void
GetProcessCurrentDirectory(PROC * proc)
{
char name[PROC_FILE_LEN];
if (!useCurrentDirectory)
return;
/*
* If we are a thread process and have an owner then copy the
* current directory path from the main process.
*/
if (IsCopyAllowed(proc))
{
SetSharedString(&proc->cwdPath, &proc->cwdPathLength,
proc->owner->cwdPath, proc->owner->cwdPathLength);
return;
}
sprintf(name, "%s/%ld/cwd", PROCDIR, (long) proc->pid);
ReadLinkPath(name, &proc->cwdPath, &proc->cwdPathLength);
}
/*
* Get the current root directory of the specified process.
* This is expensive and so is only gotten if the column is actually in use.
* The permissions only allow this information to be gotten for the same user
* id or if you are running as root.
*/
static void
GetProcessRootDirectory(PROC * proc)
{
char name[PROC_FILE_LEN];
if (!useRootDirectory)
return;
/*
* If we are a thread process and have an owner then copy the
* root directory path from the main process.
*/
if (IsCopyAllowed(proc))
{
SetSharedString(&proc->rootPath, &proc->rootPathLength,
proc->owner->rootPath, proc->owner->rootPathLength);
return;
}
sprintf(name, "%s/%ld/root", PROCDIR, (long) proc->pid);
ReadLinkPath(name, &proc->rootPath, &proc->rootPathLength);
}
/*
* Get the device and inode of the executable file for specified process.
* This is expensive and so is only gotten if the column is actually in use.
* The permissions only allow this information to be gotten for the same user
* id or if you are running as root.
*/
static void
GetProcessExecInode(PROC * proc)
{
char name[PROC_FILE_LEN];
if (!useExecInode)
return;
/*
* If we are a thread process and have an owner then copy the
* executable path from the main process.
*/
if (IsCopyAllowed(proc))
{
SetSharedString(&proc->execPath, &proc->execPathLength,
proc->owner->execPath, proc->owner->execPathLength);
return;
}
sprintf(name, "%s/%ld/exe", PROCDIR, (long) proc->pid);
ReadLinkPath(name, &proc->execPath, &proc->execPathLength);
}
/*
* Get information about processes three standard file descriptors.
* This is expensive and so is only gotten if the columns are actually in use.
* The permissions only allow this information to be gotten for the same user
* id or if you are running as root.
*/
static void
GetProcessStdioDescriptors(PROC * proc)
{
int fd;
int dummy;
char name[PROC_FILE_LEN];
for (fd = 0; fd <= 2; fd++)
{
if (!useStdioTable[fd])
continue;
/*
* If we are a thread process and have an owner then copy the
* stdio path from the main process.
*/
if (IsCopyAllowed(proc))
{
SetSharedString(&proc->stdioPaths[fd], &dummy,
proc->owner->stdioPaths[fd],
strlen(proc->owner->stdioPaths[fd]));
continue;
}
sprintf(name, "%s/%ld/fd/%d", PROCDIR, (long) proc->pid, fd);
ReadLinkPath(name, &proc->stdioPaths[fd], &dummy);
}
}
/*
* Get the destination path and length for the specified symbolic link.
* The path is returned into the indicated variables, which are updated.
* The returned string is allocated within a shared pool of strings and
* so can only be freed by calling the appropriate routine. Returns TRUE
* if the information was able to be obtained.
*/
static BOOL
ReadLinkPath(const char * name, char ** retpath, int * retpathlength)
{
char * newPath;
char * oldPath;
int len;
int oldLength;
char buffer[MAX_PATH_LEN];
/*
* Save the values for the old path.
*/
oldPath = *retpath;
oldLength = *retpathlength;
/*
* Read the value of the symbolic link.
*/
len = readlink(name, buffer, sizeof(buffer));
if ((len <= 0) || (len == sizeof(buffer)))
{
FreeSharedString(oldPath);
*retpath = emptyString;
*retpathlength = 0;
return FALSE;
}
buffer[len] = '\0';
/*
* If the path is the same as the existing one then do nothing.
*/
if ((len == oldLength) && (strcmp(oldPath, buffer) == 0))
return TRUE;
/*
* The value has changed. Delete the old string.
*/
FreeSharedString(oldPath);
*retpath = emptyString;
*retpathlength = 0;
/*
* Allocate a new shared string to store the new value.
*/
newPath = AllocateSharedString(buffer, len);
if (newPath == NULL)
return FALSE;
/*
* Return the new string value.
*/
*retpath = newPath;
*retpathlength = len;
return TRUE;
}
/*
* Return whether or now we are a thread process that is allowed to
* copy data from the owning main process.
*/
static BOOL
IsCopyAllowed(const PROC * proc)
{
if (noCopy)
return FALSE;
return (proc->isThread && (proc->owner != 0));
}
/*
* Pick the best state character of the two states which most indicates
* what the multiple threads of a process are doing.
*/
int
PickBestState(int state1, int state2)
{
if ((state1 == 'R') || (state2 == 'R'))
return 'R';
if ((state1 == 'D') || (state2 == 'D'))
return 'D';
if ((state1 == 'S') || (state2 == 'S'))
return 'S';
return state1;
}
/* END CODE */
|