/* * This function is called only once by the gmond. Use to * initialize data structures, etc or just return SYNAPSE_SUCCESS; */ g_val_t metric_init(void) { g_val_t val; /* * Try to use the vm.swap_info sysctl to gather swap data. If it * isn't implemented, fall back to trying to old kvm based interface. */ mibswap_size = MIB_SWAPINFO_SIZE; if (sysctlnametomib("vm.swap_info", mibswap, &mibswap_size) == -1) { kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "metric_init()"); } else { /* * RELEASE versions of DragonFlyBSD with the swap mib have a version * of libkvm that doesn't need root for simple proc access so we * just open /dev/null to give us a working handle here. */ kd = kvm_open(_PATH_DEVNULL, NULL, NULL, O_RDONLY, "metric_init()"); use_vm_swap_info = 1; } pagesize = getpagesize(); /* Initalize some counters */ get_netbw(NULL, NULL, NULL, NULL); cpu_state(-1); val.int32 = SYNAPSE_SUCCESS; return val; }
int _kvm_swap_used_pages(uint64_t *out) { const int total_only = 1; // from kvm_getswapinfo(3) kvm_t *kd; struct kvm_swap current; kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL); if (kd == NULL) { return -1; } if (kvm_getswapinfo(kd, ¤t, total_only, 0) == -1) { goto error1; } if (kvm_close(kd) != 0) { return -1; } kd = NULL; *out = current.ksw_used; return 0; error1: if (kd != NULL) { kvm_close(kd); } return -1; }
int uv_resident_set_memory(size_t* rss) { kvm_t *kd = NULL; struct kinfo_proc2 *kinfo = NULL; pid_t pid; int nprocs; int max_size = sizeof(struct kinfo_proc2); int page_size; page_size = getpagesize(); pid = getpid(); kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open"); if (kd == NULL) goto error; kinfo = kvm_getproc2(kd, KERN_PROC_PID, pid, max_size, &nprocs); if (kinfo == NULL) goto error; *rss = kinfo->p_vm_rssize * page_size; kvm_close(kd); return 0; error: if (kd) kvm_close(kd); return -EPERM; }
uv_err_t uv_resident_set_memory(size_t* rss) { kvm_t *kd = NULL; struct kinfo_proc *kinfo = NULL; pid_t pid; int nprocs; size_t page_size = getpagesize(); pid = getpid(); kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open"); if (kd == NULL) goto error; kinfo = kvm_getprocs(kd, KERN_PROC_PID, pid, &nprocs); if (kinfo == NULL) goto error; #ifdef __DragonFly__ *rss = kinfo->kp_vm_rssize * page_size; #else *rss = kinfo->ki_rssize * page_size; #endif kvm_close(kd); return uv_ok_; error: if (kd) kvm_close(kd); return uv__new_sys_error(errno); }
MemSensor::MemSensor(int msec) : Sensor(msec) { #if defined Q_OS_FREEBSD || defined(Q_OS_NETBSD) /* get the page size with "getpagesize" and calculate pageshift from it */ int pagesize = getpagesize(); pageshift = 0; while (pagesize > 1) { pageshift++; pagesize >>= 1; } /* we only need the amount of log(2)1024 for our conversion */ pageshift -= 10; # if defined(Q_OS_FREEBSD) && defined(__FreeBSD_version) && __FreeBSD_version >= 500018 kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, "kvm_open"); # elif defined Q_OS_FREEBSD connect(&ksp, SIGNAL(receivedStdout(KProcess *, char *, int )), this,SLOT(receivedStdout(KProcess *, char *, int ))); connect(&ksp, SIGNAL(processExited(KProcess *)), this,SLOT(processExited( KProcess * ))); swapTotal = swapUsed = 0; MaxSet = false; readValues(); # endif #else readValues(); #endif }
void mem_init(void) { int pagesize; if (!(kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open"))) { perror("kvm_open"); exit(1); } kvm_nlist(kd, nlst); if (!nlst[0].n_type) { perror("kvm_nlist"); exit(1); } seteuid(getuid()); setegid(getgid()); if (geteuid() != getuid() || getegid() != getgid()) { perror("sete?id"); exit(1); } }
int uv_resident_set_memory(size_t* rss) { kvm_t *kd = NULL; struct kinfo_proc *kinfo = NULL; pid_t pid; int nprocs, max_size = sizeof(struct kinfo_proc); size_t page_size = getpagesize(); pid = getpid(); kd = kvm_open(NULL, _PATH_MEM, NULL, O_RDONLY, "kvm_open"); if (kd == NULL) goto error; kinfo = kvm_getprocs(kd, KERN_PROC_PID, pid, max_size, &nprocs); if (kinfo == NULL) goto error; *rss = kinfo->p_vm_rssize * page_size; kvm_close(kd); return 0; error: if (kd) kvm_close(kd); return -EPERM; }
static int swapmode(int *retavail, int *retfree) { int n; int pagesize = getpagesize(); struct kvm_swap swapary[1]; static int kd_init = TRUE; if(kd_init) { kd_init = FALSE; if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, "kvm_open")) == NULL) { g_warning("Cannot read kvm."); return -1; } } if(kd == NULL) { return -1; } *retavail = 0; *retfree = 0; #define CONVERT(v) ((quad_t)(v) * pagesize / 1024) n = kvm_getswapinfo(kd, swapary, 1, 0); if (n < 0 || swapary[0].ksw_total == 0) return(0); *retavail = CONVERT(swapary[0].ksw_total); *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used); n = (int)((double)swapary[0].ksw_used * 100.0 / (double)swapary[0].ksw_total); return(n); }
int os_isrunning(int pid) { static kvm_t *kd; struct kinfo_proc *p; int i, n_processes; if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, "kvm_open")) == NULL) { return TRUE; } else { p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes); for (i = 0; i < n_processes; i++) { if (pid == p[i].ki_pid) { return TRUE; } } kvm_close(kd); } return FALSE; }
static void dumpfile_init() { kvm_p = kvm_open(namelist, dumpfile, NULL, O_RDONLY, program_name); if (kvm_p == NULL) { /* kvm_open prints an error message */ exit(1); } if (kvm_nlist(kvm_p, kvm_syms) != 0) { (void) fprintf(stderr, gettext( "Symbol lookup error in %s\n"), namelist); exit(1); } if (kvm_read(kvm_p, kvm_syms[0].n_value, (char *) &dump_bufaddr, sizeof (dump_bufaddr)) != sizeof (dump_bufaddr) || kvm_read(kvm_p, kvm_syms[1].n_value, (char *) &tnf_bufsize, sizeof (tnf_bufsize)) != sizeof (tnf_bufsize)) { (void) fprintf(stderr, gettext( "kvm_read error in %s\n"), dumpfile); exit(1); } if (dump_bufaddr == NULL || tnf_bufsize == 0) { (void) fprintf(stderr, gettext( "No trace data available in the kernel.\n")); exit(1); } }
void cpu_init(void) { kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open"); if (kd == NULL) { fprintf(stderr, "can't open kernel virtual memory"); exit(1); } kvm_nlist(kd, nlst); if (nlst[0].n_type == 0) { fprintf(stderr, "error extracting symbols"); exit(1); } /* drop setgid & setuid (the latter should not be there really) */ seteuid(getuid()); setegid(getgid()); if (geteuid() != getuid() || getegid() != getgid()) { fprintf(stderr, "unable to drop privileges"); exit(1); } }
int machine_init(void) { /* * get the page size with "getpagesize" and calculate pageshift from * it */ int pagesize = getpagesize(); pageshift = 0; while (pagesize > 1) { pageshift++; pagesize >>= 1; } /* we only need the amount of log(2)1024 for our conversion */ pageshift -= 10; /* open kernel virtual memory */ if ((kvmd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL) { perror("kvm_open failed"); return (FALSE); } return (TRUE); }
int k_open() { /* ** Open the kernel memory device */ if (!(kd = kvm_open(path_unix, path_kmem, NULL, O_RDONLY, NULL))) ERROR("main: kvm_open"); /* Kludge - have to do it here or A/UX 3 will barf */ #define N_FILE 0 #define N_TCB 1 #ifdef n_name strcpy(nl[N_FILE].n_name,"file"); strcpy(nl[N_TCB].n_name,"tcb"); strcpy(nl[2].n_name,""); #else nl[N_FILE].n_name = "file"; nl[N_TCB].n_name = "tcb"; nl[2].n_name = ""; #endif /* ** Extract offsets to the needed variables in the kernel */ if (kvm_nlist(kd, nl) != 0) ERROR("main: kvm_nlist"); return 0; }
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { FreeBSDProcessList* fpl = calloc(1, sizeof(FreeBSDProcessList)); ProcessList* pl = (ProcessList*) fpl; ProcessList_init(pl, Class(FreeBSDProcess), usersTable, pidWhiteList, userId); int cpus = 1; size_t sizeof_cpus = sizeof(cpus); int err = sysctlbyname("hw.ncpu", &cpus, &sizeof_cpus, NULL, 0); if (err) cpus = 1; pl->cpuCount = MAX(cpus, 1); CPUData* tmp_cpus = (CPUData*) realloc(fpl->cpus, cpus * sizeof(CPUData)); assert(tmp_cpus != NULL); fpl->cpus = tmp_cpus; for (int i = 0; i < cpus; i++) { fpl->cpus[i].totalTime = 1; fpl->cpus[i].totalPeriod = 1; } size_t len; len = 4; sysctlnametomib("vm.stats.vm.v_wire_count", MIB_vm_stats_vm_v_wire_count, &len); len = 4; sysctlnametomib("vm.stats.vm.v_cache_count", MIB_vm_stats_vm_v_cache_count, &len); len = 2; sysctlnametomib("hw.physmem", MIB_hw_physmem, &len); pageSizeKb = PAGE_SIZE_KB; fpl->kd = kvm_open(NULL, "/dev/null", NULL, 0, NULL); assert(fpl->kd); return pl; }
int machine_init(pf_meminfo_t *ret) { size_t t = sizeof(ret->mem_used); int pagesize; struct kvm_swap swapinfo; kvm_t *kd; pagesize = getpagesize(); sysctlbyname("vm.stats.vm.v_active_count", &ret->mem_used, &t, NULL, 0); ret->mem_used *= pagesize; ret->mem_used *= K2B; sysctlbyname("vm.stats.vm.v_free_count", &ret->mem_free, &t, NULL, 0); ret->mem_free *= pagesize; ret->mem_free *= K2B; ret->mem_total = ret->mem_free + ret->mem_used; sysctlbyname("vfs.bufspace", &ret->buffers, &t, NULL, 0); sysctlbyname("vm.stats.vm.v_cache_count", &ret->cached, &t, NULL, 0); ret->cached *= pagesize; ret->cached *= K2B; kd = kvm_open(NULL, DEVNULL_PATH, NULL, O_RDONLY, "kvm_open"); kvm_getswapinfo(kd, &swapinfo, 1, 0); ret->swap_total *= pagesize; ret->swap_total *= K2B; ret->swap_used *= pagesize; ret->swap_used *= K2B; ret->swap_free = swapinfo.ksw_total - swapinfo.ksw_used; return 0; }
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { int mib[] = { CTL_HW, HW_NCPU }; int fmib[] = { CTL_KERN, KERN_FSCALE }; int i, e; OpenBSDProcessList* opl = calloc(1, sizeof(OpenBSDProcessList)); ProcessList* pl = (ProcessList*) opl; size_t size = sizeof(pl->cpuCount); ProcessList_init(pl, Class(OpenBSDProcess), usersTable, pidWhiteList, userId); e = sysctl(mib, 2, &pl->cpuCount, &size, NULL, 0); if (e == -1 || pl->cpuCount < 1) { pl->cpuCount = 1; } opl->cpus = realloc(opl->cpus, pl->cpuCount * sizeof(CPUData)); size = sizeof(fscale); if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) err(1, "fscale sysctl call failed"); for (i = 0; i < pl->cpuCount; i++) { opl->cpus[i].totalTime = 1; opl->cpus[i].totalPeriod = 1; } pageSizeKb = PAGE_SIZE_KB; // XXX: last arg should eventually be an errbuf opl->kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL); assert(opl->kd); return pl; }
int k_open(void) { kinfo = xmalloc(sizeof(struct kainfo)); kinfo->kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL); if (kinfo->kd == NULL) { free(kinfo); debug("kvm_open: %s", strerror(errno)); return (-1); } kinfo->nl[N_TCB].n_name = "_tcb"; #ifdef MASQ_SUPPORT if (opt_enabled(MASQ)) kinfo->nl[N_NATLIST].n_name = "_nat_instances"; else kinfo->nl[N_NATLIST].n_name = NULL; #endif kinfo->nl[N_TOTAL - 1].n_name = NULL; if (kvm_nlist(kinfo->kd, kinfo->nl) != 0) { kvm_close(kinfo->kd); free(kinfo); debug("kvm_nlist: %s", strerror(errno)); return (-1); } #ifdef MASQ_SUPPORT if (kinfo->nl[N_NATLIST].n_value == 0) disable_opt(MASQ); #endif return (0); }
static int sge_get_kernel_fd(kernel_fd_type *fd) { #if !(defined(IRIX) || defined(HP10) || defined(ALPHA4) || defined(ALPHA5) || defined(AIX51)) char prefix[256] = "my_error:"; #endif DENTER(TOP_LAYER, "sge_get_kernel_fd"); if (!kernel_initialized) { #if defined(IRIX) || defined(HP10) || defined(ALPHA4) || defined(ALPHA5) || defined(AIX51) kernel_fd = open("/dev/kmem", 0); if (kernel_fd != -1) #else kernel_fd = kvm_open(NULL, NULL, NULL, O_RDONLY, prefix); if (kernel_fd != NULL) #endif { kernel_initialized = 1; } else { DPRINTF(("kvm_open() failed: %s\n", strerror(errno))); kernel_initialized = 0; } } *fd = kernel_fd; DEXIT; return kernel_initialized; }
int main(int ac, char **av) { const char *corefile = NULL; const char *sysfile = NULL; struct kinfo_proc *kp; kvm_t *kd; int ch; int i; int nprocs; while ((ch = getopt(ac, av, "M:N:v")) != -1) { switch(ch) { case 'v': ++verboseopt; break; case 'M': corefile = optarg; break; case 'N': sysfile = optarg; break; default: fprintf(stderr, "%s [-M core] [-N system]\n", av[0]); exit(1); } } ac -= optind; av += optind; if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) { perror("kvm_open"); exit(1); } if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nprocs)) == NULL) errx(1, "%s", kvm_geterr(kd)); fprintf(stdout, "%-6s %-6s %-20s %-10s %-5s\n", "PID", "PPID", "COMMAND", "LOGIN", "NICE"); for (i = 0; i < nprocs; i++) { fprintf(stdout, "%-6d %-6d %-20s %-10s %-5d\n", kp[i].kp_pid, kp[i].kp_ppid, kp[i].kp_comm, kp[i].kp_login, kp[i].kp_nice); } kvm_close(kd); return 0; }
int ka_open(void **misc) { int rcode; struct kainfo *kp; kp = s_malloc(sizeof(*kp)); /* ** Open the kernel memory device */ if ((kp->kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL) { if (debug) perror("kvm_open"); syslog(LOG_ERR, "kvm_open: %m"); s_free(kp); return -1; } kp->nl[0].n_name = "_file"; kp->nl[1].n_name = "_nfile"; #ifdef HPUX7 kp->nl[2].n_name = "_tcb_cb"; #else kp->nl[2].n_name = "_tcb"; #endif kp->nl[3].n_name = NULL; /* ** Extract offsets to the needed variables in the kernel */ if ((rcode = kvm_nlist(kp->kd, kp->nl)) != 0) { kp->nl[0].n_name = "file"; kp->nl[1].n_name = "nfile"; #ifdef HPUX7 kp->nl[2].n_name = "tcb_cb"; #else kp->nl[2].n_name = "tcb"; #endif if ((rcode = kvm_nlist(kp->kd, kp->nl)) != 0) { if (debug) fprintf(stderr, "kvm_nlist: returned %d\n", rcode); syslog(LOG_ERR, "kvm_nlist, rcode = %d: %m", rcode); kvm_close(kp->kd); s_free(kp); return -1; } } *misc = (void *) kp; return 0; }
pid_t vfork_and_run(void (*fn)(void*) /*NORETURN*/, void *arg) { /* GNO's fork2 call will return immediately and allow the parent and * child processes to execute concurrently using the same memory * space. To prevent them stomping on each other, we want to get * behavior like a traditional vfork() implementation, where the * parent blocks until the child terminates or execs. * * Our approach is to check the process tables to make sure the * child has actually finished or exec'd. If not, we loop and try again. * We can't just rely on the fact that the child signaled us, because * it may still be running in libc's implementation of exec*. */ long oldmask; pid_t pid; kvmt *kvm_context; struct pentry *proc_entry; int done = 0; /* Isolate child process's environment from parent */ if (environPush() != 0) return -1; /* Block all signals for now */ oldmask = sigblock(-1); pid = fork2(fork_thunk, CHILD_STACKSIZE, 0, forked_child_name, (sizeof(fn) + sizeof(arg) + sizeof(oldmask) + 1) / 2, fn, arg, oldmask); if (pid < 0) goto ret; while (!done) { /* Wait for ~100 ms. If procsend worked, the child could send a * message with it to end the waiting earlier, but this isn't * possible in GNO 2.0.6 because procsend is broken. This isn't * too big an issue, since 100ms isn't very long to wait anyhow. */ procrecvtim(1); /* Check if the child is really dead or forked by inspecting * the kernel's process entry for it. */ kvm_context = kvm_open(); if (kvm_context == NULL) break; proc_entry = kvmgetproc(kvm_context, pid); if (proc_entry == NULL || (proc_entry->args != NULL && strcmp(forked_child_name, proc_entry->args + 8) != 0)) done = 1; kvm_close(kvm_context); } ret: sigsetmask(oldmask); environPop(); return pid; }
int ProcessInfo::getVirtualMemorySize() { kvm_t *kd = NULL; int cnt = 0; char err[_POSIX2_LINE_MAX] = {0}; if ((kd = kvm_open(NULL, "/dev/null", "/dev/null", O_RDONLY, err)) == NULL) return -1; kinfo_proc * task = kvm_getprocs(kd, KERN_PROC_PID, _pid.toNative(), &cnt); kvm_close(kd); return task->ki_size / 1024 / 1024; // convert from bytes to MB }
int ProcessInfo::getResidentSize() { kvm_t *kd = NULL; int cnt = 0; char err[_POSIX2_LINE_MAX] = {0}; if ((kd = kvm_open(NULL, "/dev/null", "/dev/null", O_RDONLY, err)) == NULL) return -1; kinfo_proc * task = kvm_getprocs(kd, KERN_PROC_PID, _pid.toNative(), &cnt); kvm_close(kd); return task->ki_rssize * sysconf( _SC_PAGESIZE ) / 1024 / 1024; // convert from pages to MB }
/** * Read all processes to initialize the information tree. * @param reference reference of ProcessTree * @return treesize>0 if succeeded otherwise =0. */ int initprocesstree_sysdep(ProcessTree_T **reference) { int treesize; static kvm_t *kvm_handle; ProcessTree_T *pt; struct kinfo_proc *pinfo; if (! (kvm_handle = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, prog))) { LogError("system statistic error -- cannot initialize kvm interface\n"); return 0; } pinfo = kvm_getprocs(kvm_handle, KERN_PROC_PROC, 0, &treesize); if (! pinfo || (treesize < 1)) { LogError("system statistic error -- cannot get process tree\n"); kvm_close(kvm_handle); return 0; } pt = CALLOC(sizeof(ProcessTree_T), treesize); for (int i = 0; i < treesize; i++) { StringBuffer_T cmdline = StringBuffer_create(64); pt[i].pid = pinfo[i].ki_pid; pt[i].ppid = pinfo[i].ki_ppid; pt[i].uid = pinfo[i].ki_ruid; pt[i].euid = pinfo[i].ki_uid; pt[i].gid = pinfo[i].ki_rgid; pt[i].starttime = pinfo[i].ki_start.tv_sec; pt[i].cputime = (long)(pinfo[i].ki_runtime / 100000); pt[i].mem_kbyte = (unsigned long)(pinfo[i].ki_rssize * pagesize_kbyte); int flags = pinfo[i].ki_stat; char * procname = pinfo[i].ki_comm; if (flags == SZOMB) pt[i].zombie = true; pt[i].cpu_percent = 0; pt[i].time = get_float_time(); char **args; if ((args = kvm_getargv(kvm_handle, &pinfo[i], 0))) { for (int j = 0; args[j]; j++) StringBuffer_append(cmdline, args[j + 1] ? "%s " : "%s", args[j]); pt[i].cmdline = Str_dup(StringBuffer_toString(StringBuffer_trim(cmdline))); } StringBuffer_free(&cmdline); if (! pt[i].cmdline || ! *pt[i].cmdline) { FREE(pt[i].cmdline); pt[i].cmdline = Str_dup(procname); } } *reference = pt; kvm_close(kvm_handle); return treesize; }
/** * Read all processes to initialize the information tree. * @param reference reference of ProcessTree * @param pflags Process engine flags * @return treesize > 0 if succeeded otherwise 0. */ int initprocesstree_sysdep(ProcessTree_T **reference, ProcessEngine_Flags pflags) { kvm_t *kvm_handle = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, prog); if (! kvm_handle) { LogError("system statistic error -- cannot initialize kvm interface\n"); return 0; } int treesize; struct kinfo_proc *pinfo = kvm_getprocs(kvm_handle, KERN_PROC_ALL, 0, &treesize); if (! pinfo || (treesize < 1)) { LogError("system statistic error -- cannot get process tree\n"); kvm_close(kvm_handle); return 0; } ProcessTree_T *pt = CALLOC(sizeof(ProcessTree_T), treesize); StringBuffer_T cmdline = NULL; if (pflags & ProcessEngine_CollectCommandLine) cmdline = StringBuffer_create(64); for (int i = 0; i < treesize; i++) { pt[i].pid = pinfo[i].kp_pid; pt[i].ppid = pinfo[i].kp_ppid; pt[i].cred.uid = pinfo[i].kp_ruid; pt[i].cred.euid = pinfo[i].kp_uid; pt[i].cred.gid = pinfo[i].kp_rgid; pt[i].threads = pinfo[i].kp_nthreads; pt[i].uptime = systeminfo.time / 10. - pinfo[i].kp_start.tv_sec; pt[i].cpu.time = (double)((pinfo[i].kp_lwp.kl_uticks + pinfo[i].kp_lwp.kl_sticks + pinfo[i].kp_lwp.kl_iticks) / 1000000.); pt[i].memory.usage = (uint64_t)pinfo[i].kp_vm_rssize * (uint64_t)pagesize; pt[i].zombie = pinfo[i].kp_stat == SZOMB ? true : false; if (pflags & ProcessEngine_CollectCommandLine) { char **args = kvm_getargv(kvm_handle, &pinfo[i], 0); if (args) { StringBuffer_clear(cmdline); for (int j = 0; args[j]; j++) StringBuffer_append(cmdline, args[j + 1] ? "%s " : "%s", args[j]); if (StringBuffer_length(cmdline)) pt[i].cmdline = Str_dup(StringBuffer_toString(StringBuffer_trim(cmdline))); } if (! pt[i].cmdline || ! *pt[i].cmdline) { FREE(pt[i].cmdline); pt[i].cmdline = Str_dup(pinfo[i].kp_comm); } } } if (pflags & ProcessEngine_CollectCommandLine) StringBuffer_free(&cmdline); *reference = pt; kvm_close(kvm_handle); return treesize; }
static int get_ifdata(const char *device, struct ifnet *result) { struct ifnet_head head; struct ifnet *ifp; char ifname[IFNAMSIZ+1]; kvm_t *kp; int len = 0; int ret = SYSINFO_RET_FAIL; kp = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL); if(kp) { if(kernel_symbols[IFNET_ID].n_type == N_UNDF) { if(kvm_nlist(kp, &kernel_symbols[0]) != 0) { kernel_symbols[IFNET_ID].n_type = N_UNDF; } } if(kernel_symbols[IFNET_ID].n_type != N_UNDF) { len = sizeof(struct ifnet_head); if(kvm_read(kp, kernel_symbols[IFNET_ID].n_value, &head, len) >= len) { len = sizeof(struct ifnet); for(ifp = head.tqh_first; ifp; ifp = result->if_list.tqe_next) { if(kvm_read(kp, (u_long) ifp, result, len) < len) break; memcpy( ifname, result->if_xname, MIN(sizeof(ifname)- 1, IFNAMSIZ) ); ifname[IFNAMSIZ] = '\0'; if(strcmp(device, ifname) == 0) { ret = SYSINFO_RET_OK; break; } } } } kvm_close(kp); } return ret; }
static int swap_init (void) { #if KERNEL_LINUX /* No init stuff */ /* #endif KERNEL_LINUX */ #elif HAVE_LIBKSTAT /* getpagesize(3C) tells me this does not fail.. */ pagesize = (derive_t) getpagesize (); if (get_kstat (&ksp, "unix", 0, "system_pages")) ksp = NULL; /* #endif HAVE_LIBKSTAT */ #elif HAVE_SWAPCTL /* No init stuff */ /* #endif HAVE_SWAPCTL */ #elif defined(VM_SWAPUSAGE) /* No init stuff */ /* #endif defined(VM_SWAPUSAGE) */ #elif HAVE_LIBKVM_GETSWAPINFO if (kvm_obj != NULL) { kvm_close (kvm_obj); kvm_obj = NULL; } kvm_pagesize = getpagesize (); if ((kvm_obj = kvm_open (NULL, /* execfile */ NULL, /* corefile */ NULL, /* swapfile */ O_RDONLY, /* flags */ NULL)) /* errstr */ == NULL) { ERROR ("swap plugin: kvm_open failed."); return (-1); } /* #endif HAVE_LIBKVM_GETSWAPINFO */ #elif HAVE_LIBSTATGRAB /* No init stuff */ /* #endif HAVE_LIBSTATGRAB */ #elif HAVE_PERFSTAT pagesize = getpagesize(); #endif /* HAVE_PERFSTAT */ return (0); }
void initCpuInfo(struct SensorModul *sm) { /* Total CPU load */ registerMonitor("cpu/user", "integer", printCPUUser, printCPUUserInfo, sm); registerMonitor("cpu/nice", "integer", printCPUNice, printCPUNiceInfo, sm); registerMonitor("cpu/sys", "integer", printCPUSys, printCPUSysInfo, sm); registerMonitor("cpu/idle", "integer", printCPUIdle, printCPUIdleInfo, sm); kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open"); kvm_nlist(kd, my_nlist); cp_time_offset = my_nlist[0].n_value; updateCpuInfo(); }
static uint32_t getRunningProcesses( void ) { struct kinfo_proc *kp; int i; int state; int nentries; kvm_t *kd = NULL; int what = KERN_PROC_ALL; uint32_t val32; val32 = 0; // it looks like we don't need to be root to call kvm_open // just to read the process table (in the way that the ps(1) command does). kd=kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, "kvm_open"); if (kd == NULL) { myLog(LOG_ERR, "kvm_open() failed"); } else { #ifdef KERN_PROC_NOTHREADS what |= KERN_PROC_NOTHREADS; #endif if ((kp = kvm_getprocs(kd, what, 0, &nentries)) == 0 || nentries < 0) { myLog(LOG_ERR, "kvm_getprocs() failed"); } else { if(debug) myLog(LOG_INFO,"kvm_getprocs found %u entries", nentries); for (i = 0; i < nentries; kp++, i++) { #ifdef KINFO_PROC_SIZE state = kp->ki_stat; #else state = kp->kp_proc.p_stat; #endif switch(state) { case SRUN: case SIDL: val32++; break; } } } kvm_close(kd); } if (val32 > 0) val32--; // subtract one for me return val32; }
int machine_get_procs(LinkedList * procs) { struct kinfo_proc *kprocs; int nproc, i; procinfo_type *p; kvm_t *kvmd; if ((kvmd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL) { perror("kvm_open"); return (FALSE); } #if OpenBSD >= 201111 kprocs = kvm_getprocs(kvmd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &nproc); #else kprocs = kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &nproc); #endif if (kprocs == NULL) { perror("kvm_getprocs"); kvm_close(kvmd); return (FALSE); } for (i = 0; i < nproc; i++) { p = malloc(sizeof(procinfo_type)); if (!p) { perror("mem_top_malloc"); kvm_close(kvmd); return (FALSE); } #if OpenBSD >= 201111 strncpy(p->name, kprocs->p_comm, 15); p->name[15] = '\0'; p->totl = pagetok(PROCSIZE(kprocs)); p->number = kprocs->p_pid; #else strncpy(p->name, kprocs->kp_proc.p_comm, 15); p->name[15] = '\0'; p->totl = pagetok(PROCSIZE(kprocs->kp_eproc.e_vm)); p->number = kprocs->kp_proc.p_pid; #endif LL_Push(procs, (void *)p); kprocs++; } kvm_close(kvmd); return (TRUE); }