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); }
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); }
char * format_next_process(caddr_t handle, char *(*getuserid) ()) { register struct macos_proc *pp; register long cputime; register double pct; struct handle *hp; char *command; /* text outputted to describe the command */ int show_cmd_local = show_fullcmd; /* * 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'; command = comm; } /* * 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 = (double) (RP(pp, cpu_usage)) / TH_USAGE_SCALE; /* get the process's command name in to "cmd" */ if (show_fullcmd) if (get_fullcmd(MPP(pp, p_pid), pp->fullcmd) < 0) show_cmd_local = 0; /* Don't show if full command not found. */ /* * 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)), 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 * pct, (show_cmd_local == 0 ? command : pp->fullcmd)); return (fmt); }
caddr_t get_process_info(struct system_info * si, struct process_select * sel, int x, char *conninfo, int mode) { 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; 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); }
caddr_t get_process_info(struct system_info *si, struct process_select *sel, int x) { 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; register struct kinfo_proc **prefp2; register struct thread_basic_info *thread; /* * these are copied out of sel for speed */ int show_idle; int show_system; int show_uid; int show_command; kproc_list = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc); 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++) { kern_return_t rc; u_int info_count = TASK_BASIC_INFO_COUNT; /* * 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) { rc = task_for_pid(mach_task_self(), PP(pp2, p_pid), &(proc_list[i].the_task)); if(rc != KERN_SUCCESS) { puke("error: get task info for pid %d failed with rc = %d", PP(pp2, p_pid), rc); } /* * load the task information */ rc = task_info(proc_list[i].the_task, TASK_BASIC_INFO, (task_info_t)&(proc_list[i].task_info), &info_count); if(rc != KERN_SUCCESS) { puke("error: couldn't get task info (%s); rc = %d", strerror(errno), rc); } /* * load the thread summary information */ 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_system = sel->system; show_uid = sel->uid != -1; show_command = sel->command != NULL; /* 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); }