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
|
/*
* m_macosx.c
*
* AUTHOR: Andrew S. Townley
* based on m_bsd44.c and m_next32.c
* by Christos Zoulas and Tim Pugh
* CREATED: Tue Aug 11 01:51:35 CDT 1998
* SYNOPSIS: MacOS X Server (Rhapsody Developer Release 2)
* DESCRIPTION:
* MacOS X Server (Rhapsody Developer Release 2)
*
* CFLAGS: -DHAVE_STRERROR
* TERMCAP: none
* MATH: none
*/
/*
* normal stuff
*/
#include "config.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include "os.h"
#include "pg_top.h"
#include "machine.h"
#include "utils.h"
/*
* MacOS kernel stuff
*/
#include <fcntl.h>
#include <sys/dkstat.h>
#include <sys/sysctl.h>
#include <mach/message.h>
#include <mach/vm_statistics.h>
#include <mach/mach.h>
#include <mach/host_info.h>
/* for new sysctl calls */
#include <sys/types.h>
#include <sys/stat.h>
#define VMUNIX "/mach_kernel"
/* #define MEM "/dev/mem" */
#define SWAP NULL
#define NUM_AVERAGES 3
#define LOG1024 10
#define PP(pp, field) ((pp)->kp_proc . field)
#define EP(pp, field) ((pp)->kp_eproc . field)
#define VP(pp, field) ((pp)->kp_eproc.e_vm . field)
#define MPP(mp, field) (PP((mp)->kproc, field))
#define MEP(mp, field) (EP((mp)->kproc, field))
#define MVP(mp, field) (VP((mp)->kproc, field))
#define TP(mp, field) ((mp)->task_info . field)
#define RP(mp, field) ((mp)->thread_summary . field)
/* define what weighted cpu is */
#define weighted_cpu(pct, s) (s == 0 ? 0.0 : \
((pct) / (1.0 - exp(s * logcpu))))
/* what we consider to be process size: */
#ifdef notdef
#define PROCSIZE(pp) (VP((pp), vm_tsize) + VP((pp), vm_dsize) + VP((pp), vm_ssize))
#endif
#define PROCSIZE(pp) (EP(pp, e_xsize))
#define TASKSIZE(t) (TP(t, virtual_size) + TP(t, resident_size))
/* what we consider to be resident set size: */
#ifdef notdef
#define RSSIZE(pp) (MVP((pp), vm_rssize))
#endif
#define RSSIZE(pp) (MEP((pp), e_xrssize))
#define pctdouble(p) ((double)(p) / FSCALE)
/*
* globals
*/
/* static kvm_t *kd = NULL; */
static int nproc;
static int onproc = -1;
static int pref_len;
static int maxmem;
static char fmt[MAX_COLS];
/* static double logcpu = 1.0; */
/* process array stuff */
static struct kinfo_proc *kproc_list = NULL;
static struct macos_proc *proc_list = NULL;
static struct macos_proc **proc_ref = NULL;
static int process_states[7];
static struct handle handle;
static struct kinfo_proc *pbase;
/*
* The mach information hopefully will not be necessary
* when the kvm_* interfaces are supported completely.
*
* Since we're only concerned with task and thread info
* for 'interesting' processes, we're going to only allocate
* as many task and thread structures as needed.
*/
/* static struct task_basic_info *task_list = NULL; */
/* memory statistics */
static int pageshift = 0;
static int pagesize = 0;
#define pagetok(size) ((size) << pageshift)
static int swappgsin = -1;
static int swappgsout = -1;
static vm_statistics_data_t vm_stats;
static long memory_stats[7];
/* CPU state percentages */
host_cpu_load_info_data_t cpuload;
static int64_t cp_time[CPU_STATE_MAX];
static int64_t cp_old[CPU_STATE_MAX];
static int64_t cp_diff[CPU_STATE_MAX];
static int64_t cpu_states[CPU_STATE_MAX];
/*
* types
*/
typedef long pctcpu;
/*
* We need to declare a hybrid structure which will store all
* of the stuff we care about for each process.
*/
struct macos_proc
{
struct kinfo_proc *kproc;
task_t the_task;
struct task_basic_info task_info;
unsigned int thread_count;
struct thread_basic_info thread_summary;
};
static int show_fullcmd;
struct handle
{
struct macos_proc **next_proc;
int remaining;
};
static char header[] =
" PID X PRI THRD SIZE RES STATE TIME MEM CPU COMMAND";
/* 0123456 -- field to fill in starts at header+6 */
#define UNAME_START 6
#define Proc_format \
"%5d %-8.8s %3d %4d %5s %5s %-5s %6s %5.2f%% %5.2f%% %.180s"
int proc_compare(const void *, const void *);
/*
* puke()
*
* This function is used to report errors to stderr.
*/
static void
puke(const char *fmt,...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fputc('\n', stderr);
fflush(stderr);
}
/*
* xfrm_cmdline - fix \0 at string ends
*
*
*/
static void
xfrm_cmdline(char *p, int len)
{
while (--len > 0)
{
if (*p == '\0')
{
*p = ' ';
}
p++;
}
}
/*
* load_thread_info()
*
* This function will attempt to load the thread summary info
* for a Mach task. The task is located as part of the macos_proc
* structure.
*
* returns the kern_return_t value of any failed call or KERN_SUCCESS
* if everything works.
*/
int
load_thread_info(struct macos_proc * mp)
{
register kern_return_t rc = 0;
register int i = 0;
register int t_utime = 0;
register int t_stime = 0;
register int t_cpu = 0;
register task_t the_task = mp->the_task;
thread_array_t thread_list = NULL;
/*
* We need to load all of the threads for the given task so we can get the
* performance data from them.
*/
mp->thread_count = 0;
rc = task_threads(the_task, &thread_list, &(mp->thread_count));
if (rc != KERN_SUCCESS)
{
/* puke("error: unable to load threads for task (%s); rc = %d", strerror(errno), rc); */
return (rc);
}
/*
* now, for each of the threads, we need to sum the stats so we can
* present the whole thing to the caller.
*/
for (i = 0; i < mp->thread_count; i++)
{
struct thread_basic_info t_info;
unsigned int icount = THREAD_BASIC_INFO_COUNT;
kern_return_t rc = 0;
rc = thread_info(thread_list[i], THREAD_BASIC_INFO,
(thread_info_t) & t_info, &icount);
if (rc != KERN_SUCCESS)
{
puke("error: unable to load thread info for task (%s); rc = %d", strerror(errno), rc);
return (rc);
}
t_utime += t_info.user_time.seconds;
t_stime += t_info.system_time.seconds;
t_cpu += t_info.cpu_usage;
}
vm_deallocate(mach_task_self(), (vm_address_t) thread_list, sizeof(thread_array_t) * (mp->thread_count));
/*
* Now, we load the values in the structure above.
*/
RP(mp, user_time).seconds = t_utime;
RP(mp, system_time).seconds = t_stime;
RP(mp, cpu_usage) = t_cpu;
return (KERN_SUCCESS);
}
/*
* prototypes for functions which pg_top needs
*/
char *printable();
/*
* definitions for offsets
*/
#define X_NPROC 0
#define X_HZ 1
#define X_MAXMEM 2
#define NLIST_LAST 3
static char *procstates[] =
{
"",
" starting, ",
" running, ",
" sleeping, ",
" stopped, ",
" zombie, ",
" swapped ",
NULL
};
static char *cpustates[] =
{
"user",
"system",
"idle",
"nice",
NULL
};
static char *state_abbrev[] =
{
"",
"start",
"run\0\0\0",
"sleep",
"stop",
"zomb"
};
/*
static char *mach_state[] =
{
"",
"R",
"T",
"S",
"U",
"H"
};
*/
/*
static char *thread_state[] =
{
"",
"run\0\0\0",
"stop",
"wait",
"uwait",
"halted",
};
*/
/*
static char *flags_state[] =
{
"",
"W",
"I"
};
*/
static char *memnames[] =
{
"K Tot, ",
"K Free, ",
"K Act, ",
"K Inact, ",
"K Wired, ",
"K in, ",
"K out ",
NULL
};
/*
* format_header()
*
* This function is used to add the username into the
* header information.
*/
char *
format_header(register char *uname_field)
{
register char *ptr;
ptr = header + UNAME_START;
while (*uname_field != '\0')
*ptr++ = *uname_field++;
return (header);
}
/*
* format_next_process()
*
* This function actually is responsible for the formatting of
* each row which is displayed.
*/
char cmd[MAX_COLS];
char *
format_next_process(caddr_t handle, char *(*getuserid) ())
{
register struct macos_proc *pp;
register long cputime;
register double pct;
struct handle *hp;
/*
* we need to keep track of the next proc structure.
*/
hp = (struct handle *) handle;
pp = *(hp->next_proc++);
hp->remaining--;
/*
* get the process structure and take care of the cputime
*/
if ((MPP(pp, p_flag) & P_INMEM) == 0)
{
/* we want to print swapped processes as <pname> */
char *comm = MPP(pp, p_comm);
#define COMSIZ sizeof(MPP(pp, p_comm))
char buf[COMSIZ];
strncpy(buf, comm, COMSIZ);
comm[0] = '<';
strncpy(&comm[1], buf, COMSIZ - 2);
comm[COMSIZ - 2] = '\0';
strncat(comm, ">", COMSIZ - 1);
comm[COMSIZ - 1] = '\0';
}
/*
* count the cpu time, but ignore the interrupts
*
* At the present time (DR2 8/1998), MacOS X doesn't correctly report this
* information through the kinfo_proc structure. We need to get it from
* the task threads.
*
* cputime = PP(pp, p_rtime).tv_sec;
*/
cputime = RP(pp, user_time).seconds + RP(pp, system_time).seconds;
/*
* calculate the base cpu percentages
*
* Again, at the present time, MacOS X doesn't report this information
* through the kinfo_proc. We need to talk to the threads.
*/
/* pct = pctdouble(PP(pp, p_pctcpu)); */
pct = (double) (RP(pp, cpu_usage)) / TH_USAGE_SCALE;
char *args = NULL, *namePtr = NULL, *stringPtr = '\0';
/* get the process's command name in to "cmd" */
if (show_fullcmd)
{
size_t size = 0;
int mib[4], maxarg, numArgs;
mib[0] = CTL_KERN;
mib[1] = KERN_ARGMAX;
size = sizeof(maxarg);
if ( sysctl(mib, 2, &maxarg, &size, NULL, 0) == -1 ) {
perror("maxarg");
return "1";
}
/* fix for kernel bug? */
maxarg = 8192;
args = (char *) malloc( maxarg );
if ( args == NULL ) {
perror("args");
return "1";
}
mib[0] = CTL_KERN;
mib[1] = KERN_PROCARGS;
mib[2] = MPP(pp, p_pid);
if (mib[2] > 0) {
size = (size_t) maxarg;
if ( sysctl(mib, 3, args, &size, NULL, 0) == -1 ) {
perror("sysctl args"); /* don't freak out because might just be a process that ended */
}
xfrm_cmdline(args, maxarg);
memcpy ( &numArgs, args, sizeof(numArgs));
stringPtr = args + sizeof(numArgs);
if ( (namePtr = strchr(stringPtr, '/')) != NULL ) {
*namePtr++;
}
stringPtr = namePtr;
}
}
/*
* format the entry
*/
/*
* In the final version, I would expect this to work correctly, but it
* seems that not all of the fields in the proc structure are being used.
*
* For now, we'll attempt to get some of the things we need from the mach
* task info.
*/
sprintf(fmt,
Proc_format,
MPP(pp, p_pid),
(*getuserid) (MEP(pp, e_pcred.p_ruid)),
/* TP(pp, base_priority), */
0,
pp->thread_count,
format_k(TASKSIZE(pp) / 1024),
format_k(pagetok(RSSIZE(pp))),
state_abbrev[(u_char) MPP(pp, p_stat)],
format_time(cputime),
100.0 * TP(pp, resident_size) / maxmem,
/* 100.0 * weighted_cpu(pct, (RP(pp, user_time).seconds + RP(pp, system_time).seconds)), */
100.0 * pct,
stringPtr);
return (fmt);
}
/*
* get_process_info()
*
* This function returns information about the processes
* on the system.
*/
caddr_t
get_process_info(struct system_info * si,
struct process_select * sel, int x, char *conninfo)
{
register int i;
register int total_procs;
register int active_procs;
register struct macos_proc **prefp;
register struct macos_proc *pp;
register struct kinfo_proc *pp2;
/*
* these are copied out of sel for speed
*/
int show_idle;
int show_system;
int show_uid;
int show_command;
/* begin mucking */
/* kproc_list = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc); */
PGconn *pgconn;
PGresult *pgresult = NULL;
nproc = 0;
pgconn = connect_to_db(conninfo);
if (pgconn != NULL)
{
pgresult = PQexec(pgconn, QUERY_PROCESSES);
nproc = PQntuples(pgresult);
pbase = (struct kinfo_proc *) malloc(sizeof(struct kinfo_proc *));
}
PQfinish(pgconn);
int mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
size_t len = nproc;
/* if (sysctl(mib, sizeof(mib)/ sizeof(int), NULL, &len, NULL, 0) == -1) {
perror("sysctl test");
return 1;
} */
struct kinfo_proc *buffer;
buffer = (struct kinfo_proc *) malloc( len * sizeof(struct kinfo_proc) );
for (i = 0; i < nproc ; i++) {
size_t size = sizeof(struct kinfo_proc);
mib[3] = atoi(PQgetvalue(pgresult, i, 0));
if (sysctl(mib, sizeof(mib)/sizeof(int), &buffer[i], &size, NULL, 0) == -1) {
perror("sysctl atoi loop");
return "1";
}
}
kproc_list = buffer;
len = nproc;
/* end selena's messing about */
if (nproc > onproc)
{
proc_list = (struct macos_proc *) realloc(proc_list, sizeof(struct macos_proc) * nproc);
proc_ref = (struct macos_proc **) realloc(proc_ref, sizeof(struct macos_proc *) * (onproc = nproc));
}
if (proc_ref == NULL || proc_list == NULL || kproc_list == NULL)
{
puke("error: out of memory (%s)", strerror(errno));
return (NULL);
}
/*
* now, our task is to build the array of information we need to function
* correctly. This involves setting a pointer to each real kinfo_proc
* structure returned by kvm_getprocs() in addition to getting the mach
* information for each of those processes.
*/
for (pp2 = kproc_list, i = 0; i < nproc; pp2++, i++)
{
/*
* first, we set the pointer to the reference in the kproc list.
*/
proc_list[i].kproc = pp2;
/*
* then, we load all of the task info for the process
*/
if (PP(pp2, p_stat) != SZOMB)
{
load_thread_info(&proc_list[i]);
}
}
/* get a pointer to the states summary array */
si->procstates = process_states;
/* set up flags which define what we are going to select */
show_idle = sel->idle;
show_uid = sel->uid != -1;
show_command = sel->command != NULL;
show_fullcmd = sel->fullcmd;
/* count up process states and get pointers to interesting procs */
total_procs = 0;
active_procs = 0;
memset((char *) process_states, 0, sizeof(process_states));
prefp = proc_ref;
for (pp = proc_list, i = 0; i < nproc; pp++, i++)
{
/*
* Place pointers to each valid proc structure in proc_ref[]. Process
* slots that are actually in use have a non-zero status field.
* Processes with P_SYSTEM set are system processes---these get
* ignored unless show_sysprocs is set.
*/
if (MPP(pp, p_stat) != 0 &&
(show_system || ((MPP(pp, p_flag) & P_SYSTEM) == 0)))
{
total_procs++;
process_states[(unsigned char) MPP(pp, p_stat)]++;
if ((MPP(pp, p_stat) != SZOMB) &&
(show_idle || (MPP(pp, p_pctcpu) != 0) ||
(MPP(pp, p_stat) == SRUN)) &&
(!show_uid || MEP(pp, e_pcred.p_ruid) == (uid_t) sel->uid))
{
*prefp++ = pp;
active_procs++;
}
}
}
/*
* if requested, sort the "interesting" processes
*/
qsort((char *) proc_ref, active_procs, sizeof(struct macos_proc *), proc_compare);
/* remember active and total counts */
si->p_total = total_procs;
si->p_active = pref_len = active_procs;
/* pass back a handle */
handle.next_proc = proc_ref;
handle.remaining = active_procs;
return ((caddr_t) & handle);
}
/*
* get_system_info()
*
* This function is responsible for geting the periodic
* system information snapshot.
*/
void
get_system_info(struct system_info * si)
{
register long total;
register int i;
unsigned int count = HOST_CPU_LOAD_INFO_COUNT;
if (host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO,
(host_info_t) & cpuload, &count) == KERN_SUCCESS)
{
for (i = 0; i < CPU_STATE_MAX; i++)
{
cp_time[i] = cpuload.cpu_ticks[i];
}
}
#ifdef MAX_VERBOSE
/*
* print out the entries
*/
for (i = 0; i < CPU_STATE_MAX; i++)
printf("cp_time[%d] = %d\n", i, cp_time[i]);
fflush(stdout);
#endif /* MAX_VERBOSE */
/*
* get the load averages
*/
/*
if (kvm_getloadavg(kd, si->load_avg, NUM_AVERAGES) == -1)
{
puke("error: kvm_getloadavg() failed (%s)", strerror(errno));
return;
}
*/
#ifdef MAX_VERBOSE
printf("%-30s%03.2f, %03.2f, %03.2f\n",
"load averages:",
si->load_avg[0],
si->load_avg[1],
si->load_avg[2]);
#endif /* MAX_VERBOSE */
total = percentages(CPU_STATE_MAX, cpu_states, cp_time, cp_old, cp_diff);
/*
* get the memory statistics
*/
{
kern_return_t status;
count = HOST_VM_INFO_COUNT;
status = host_statistics(mach_host_self(), HOST_VM_INFO,
(host_info_t) & vm_stats, &count);
if (status != KERN_SUCCESS)
{
puke("error: vm_statistics() failed (%s)", strerror(errno));
return;
}
/*
* we already have the total memory, we just need to get it in the
* right format.
*/
pagesize = 1; /* temporary fix to div by 0 errors */
memory_stats[0] = pagetok(maxmem / pagesize);
memory_stats[1] = pagetok(vm_stats.free_count);
memory_stats[2] = pagetok(vm_stats.active_count);
memory_stats[3] = pagetok(vm_stats.inactive_count);
memory_stats[4] = pagetok(vm_stats.wire_count);
if (swappgsin < 0)
{
memory_stats[5] = 1;
memory_stats[6] = 1;
}
else
{
memory_stats[5] = pagetok(((vm_stats.pageins - swappgsin)));
memory_stats[6] = pagetok(((vm_stats.pageouts - swappgsout)));
}
swappgsin = vm_stats.pageins;
swappgsout = vm_stats.pageouts;
}
si->cpustates = cpu_states;
si->memory = memory_stats;
si->last_pid = -1;
return;
}
/*
* machine_init()
*
* This function is responsible for filling in the values of the
* statics structure.
*/
int
machine_init(struct statics * stat)
{
size_t size;
size = sizeof(maxmem);
sysctlbyname("hw.physmem", &maxmem, &size, NULL, 0);
size = sizeof(nproc);
sysctlbyname("kern.maxproc", &nproc, &size, NULL, 0);
#ifdef MAX_VERBOSE
printf("%-30s%10d\n", "total system memory:", maxmem);
#endif /* MAX_VERBOSE */
/*
* calculate the pageshift from the system page size
*/
pagesize = getpagesize();
pageshift = 0;
while ((pagesize >>= 1) > 0)
pageshift++;
pageshift -= LOG1024;
/*
* fill in the statics information
*/
stat->procstate_names = procstates;
stat->cpustate_names = cpustates;
stat->memory_names = memnames;
stat->flags.fullcmds = 1;
/* if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL)
return -1;
*/
return (0);
}
/* comparison routine for qsort */
/*
* proc_compare - comparison function for "qsort"
* Compares the resource consumption of two processes using five
* distinct keys. The keys (in descending order of importance) are:
* percent cpu, cpu ticks, state, resident set size, total virtual
* memory usage. The process states are ordered as follows (from least
* to most important): WAIT, zombie, sleep, stop, start, run. The
* array declaration below maps a process state index into a number
* that reflects this ordering.
*/
static unsigned char sorted_state[] =
{
0, /* not used */
3, /* sleep */
1, /* ABANDONED (WAIT) */
6, /* run */
5, /* start */
2, /* zombie */
4 /* stop */
};
int
proc_compare(const void *pp1, const void *pp2)
{
register struct macos_proc *p1;
register struct macos_proc *p2;
register int result;
register pctcpu lresult;
/* remove one level of indirection */
p1 = *(struct macos_proc **) pp1;
p2 = *(struct macos_proc **) pp2;
/* compare percent cpu (pctcpu) */
if ((lresult = RP(p2, cpu_usage) - RP(p1, cpu_usage)) == 0)
{
/* use cpticks to break the tie */
if ((result = MPP(p2, p_cpticks) - MPP(p1, p_cpticks)) == 0)
{
/* use process state to break the tie */
if ((result = sorted_state[(unsigned char) MPP(p2, p_stat)] -
sorted_state[(unsigned char) MPP(p1, p_stat)]) == 0)
{
/* use priority to break the tie */
if ((result = MPP(p2, p_priority) - MPP(p1, p_priority)) == 0)
{
/* use resident set size (rssize) to break the tie */
if ((result = RSSIZE(p2) - RSSIZE(p1)) == 0)
{
/* use total memory to break the tie */
result = PROCSIZE(p2->kproc) - PROCSIZE(p1->kproc);
}
}
}
}
}
else
{
result = lresult < 0 ? -1 : 1;
}
return (result);
}
/*
* proc_owner(pid) - returns the uid that owns process "pid", or -1 if
* the process does not exist.
* It is EXTREMLY IMPORTANT that this function work correctly.
* If pg_top runs setuid root (as in SVR4), then this function
* is the only thing that stands in the way of a serious
* security problem. It validates requests for the "kill"
* and "renice" commands.
*/
uid_t
proc_owner(pid_t pid)
{
register int cnt;
register struct macos_proc **prefp;
register struct macos_proc *pp;
prefp = proc_ref;
cnt = pref_len;
while (--cnt >= 0)
{
pp = *prefp++;
if (MPP(pp, p_pid) == (pid_t) pid)
{
return ((int) MEP(pp, e_pcred.p_ruid));
}
}
return (-1);
}
|