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 */ #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; }
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); }
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) { 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); }
int main(int argc, char **argv) { char *s; int i; int flags; prand = getpid(); while(1) { switch(getopt(argc, argv, "+w:At:n:N:Ce:d")) { case 'w': who = strdup(optarg); break; case 'A': authq = 1; break; case 't': qtype = atoi(optarg); for (i=DNSQTYPEMIN;i<=DNSQTYPEMAX;i++) { s = dnsqtypename(i); if (s && !strcmp(optarg, s)) { qtype = i; break; } } if (!qtype) puke("Bad -t option"); break; case 'n': newns(optarg, 0); break; case 'N': newns(optarg, 1); break; case 'C': syncsev = CRITBIT; break; case 'e': newexp(optarg); break; case 'd': debug = 1; break; case EOF: goto doneopts; default: usage(); } } doneopts: if (!who) usage(); dnsfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (dnsfd == -1) barf("socket"); /* see UNPv2 p58 */ if ((flags = fcntl(dnsfd, F_GETFL, 0)) == -1) barf("fcntl F_GETFL"); flags |= O_NONBLOCK; if (fcntl(dnsfd, F_SETFL, flags) == -1) barf("fcntl F_SETFL"); sloop(); endgame(); }
main() { register struct tab *p; int i, n, ev, nkl; int flagf, flagb; tabp[tabpp++] = table; tabp[tabpp++] = table+1; while(input()); /* * pass1 -- create interrupt vectors */ nkl = 0; flagf = flagb = 1; fout = creat("l.s", 0666); puke(stra); for(p=table; p->name; p++) if(p->count != 0 && p->key & INTR) { if(p->address>240 && flagb) { flagb = 0; puke(strb); } if(p->address >= 300) { if(flagf) { ev = 0; flagf = 0; puke(strc); } if(p->key & EVEN && ev & 07) { printf("\t.=.+4\n"); ev =+ 4; } ev =+ p->address - 300; } else printf("\n. = %d^.\n", p->address); n = p->count; if(n < 0) n = -n; for(i=0; i<n; i++) if(p->key & KL) { printf(p->codea, nkl, nkl); nkl++; } else printf(p->codea, i, i); } if(flagb) puke(strb); puke(strd); for(p=table; p->name; p++) if(p->count != 0 && p->key & INTR) printf("\n%s%s", p->codeb, p->codec); flush(); close(fout); /* * pass 2 -- create configuration table */ fout = creat("c.c", 0666); puke(stre); for(i=0; p=tabp[i]; i++) if(p->key & BLOCK) printf("%s\n", p->coded); puke(strf); for(i=0; p=tabp[i]; i++) if(p->key & CHAR) printf("%s\n", p->codee); puke(strg); flush(); close(fout); }
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); }