Example #1
0
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, &current, 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;
}
Example #2
0
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;
}
Example #3
0
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);
}
Example #4
0
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;
}
Example #5
0
/**
 * 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;
}
Example #7
0
int
psutil_get_proc_list(kinfo_proc **procList, size_t *procCount) {
    // Returns a list of all BSD processes on the system.  This routine
    // allocates the list and puts it in *procList and a count of the
    // number of entries in *procCount.  You are responsible for freeing
    // this list (use "free" from System framework).
    // On success, the function returns 0.
    // On error, the function returns a BSD errno value.
    kinfo_proc *result;
    int done;
    static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC, 0 };
    // Declaring name as const requires us to cast it when passing it to
    // sysctl because the prototype doesn't include the const modifier.
    size_t length;
    char errbuf[_POSIX2_LINE_MAX];
    kinfo_proc *x;
    int cnt;
    kvm_t *kd;

    assert( procList != NULL);
    assert(*procList == NULL);
    assert(procCount != NULL);

    kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);

    if (kd == NULL) {
        PyErr_Format(PyExc_RuntimeError, "kvm_openfiles() failed: %s", errbuf);
        return errno;
    }

    result = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(kinfo_proc), &cnt);
    if (result == NULL) {
        PyErr_Format(PyExc_RuntimeError, "kvm_getproc2() failed");
        kvm_close(kd);
        return errno;
    }

    *procCount = (size_t)cnt;

    size_t mlen = cnt * sizeof(kinfo_proc);

    if ((*procList = malloc(mlen)) == NULL) {
        PyErr_NoMemory();
        kvm_close(kd);
        return errno;
    }

    memcpy(*procList, result, mlen);
    assert(*procList != NULL);
    kvm_close(kd);

    return 0;
}
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);
}
Example #9
0
int
kcore_close(struct kcore_data *kc)
{
	int retval;

	if (kc == NULL) {
		retval = kvm_close(kcore_global.kd);
		return(retval);
	}

	retval = kvm_close(kc->kd);
	free(kc);
	return(retval);
}
Example #10
0
void
if_close(void)
{
	init_ok = 0;
	if (kvm_close(kvmd) == -1)
		fprintf(stderr, "Error in kvm_close\n");
}
Example #11
0
/*
 * HOST RESOURCES mib module finalization hook.
 * Returns 0 on success, < 0 on error
 */
static int
hostres_fini(void)
{

	if (hr_kd != NULL)
		(void)kvm_close(hr_kd);

	fini_storage_tbl();
	fini_fs_tbl();
	fini_processor_tbl();
	fini_disk_storage_tbl();
	fini_device_tbl();
	fini_partition_tbl();
	fini_network_tbl();
	fini_printer_tbl();

	fini_swrun_tbl();
	fini_swins_tbl();

	fini_scalars();

	if (host_registration_id > 0)
		or_unregister(host_registration_id);

	HRDBG("done.");
	return (0);
}
Example #12
0
File: unix.c Project: mubawa/TDE
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;
}
Example #13
0
static kvm_t *
kopen(char const *memf)
{
	kvm_t	*kvmd = NULL;
	char	 errbuf[_POSIX2_LINE_MAX];

	kvmd = kvm_openfiles(NULL, memf, NULL, O_RDONLY, errbuf);
	if (setgid(getgid()) != 0)
		err(1, "setgid");
	if (kvmd == NULL) {
		warnx("kvm_openfiles: %s", errbuf);
		return (NULL);
	}

	if (kvm_nlist(kvmd, nl) < 0) {
		warnx("kvm_nlist: %s", kvm_geterr(kvmd));
		goto fail;
	}

	if (nl[0].n_type == 0) {
		warnx("kvm_nlist: no namelist");
		goto fail;
	}

	return (kvmd);
fail:
	kvm_close(kvmd);

	return (NULL);
} /* kopen */
Example #14
0
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);
}
Example #15
0
static int pid_to_cmd(pid_t pid, char *cmd, size_t cmd_size)
{
	int cnt, ret;
	kvm_t *kd;
	struct kinfo_proc *kp;

	ret = -1;

	if ((kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, NULL)) == NULL)
		return ret;
	if ((kp = kvm_getprocs(kd, KERN_PROC_PID, (int)pid, sizeof(*kp), &cnt)) == NULL)
		goto out;
	if ((kp->p_flag & P_SYSTEM) != 0)
		goto out;
	if (cnt != 1)
		goto out;
	if (strlcpy(cmd, kp[0].p_comm, cmd_size) >= cmd_size)
		goto out;

	ret = 0;

out:
	kvm_close(kd);
	return ret;
}
Example #16
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;
}
Example #17
0
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;

}
Example #18
0
File: kernel.c Project: 274914765/C
/** Closes the kernel memory support.
 */
void free_kmem (void)
{
    if (kd != NULL)
    {
        kvm_close (kd);
        kd = NULL;
    }
}
Example #19
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;
}
Example #20
0
void
tst_close(void)
{
	int i;

	printf("kvm_close()\n");
	if ((i = kvm_close(cookie)) != 0)
		printf("ERROR: kvm_close returned %d\n", i);
}
Example #21
0
void
procstat_close(struct procstat *procstat)
{

	assert(procstat);
	if (procstat->type == PROCSTAT_KVM)
		kvm_close(procstat->kd);
	free(procstat);
}
int
machine_close(void)
{
	if (kvmd != NULL) {
		kvm_close(kvmd);
		kvmd = NULL;
	}
	return (TRUE);
}
Example #23
0
 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
 }
Example #24
0
 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
 }
Example #25
0
PyObject *
psutil_swap_mem(PyObject *self, PyObject *args) {
    // Return swap memory stats (see 'swapinfo' cmdline tool)
    kvm_t *kd;
    struct kvm_swap kvmsw[1];
    unsigned int swapin, swapout, nodein, nodeout;
    size_t size = sizeof(unsigned int);

    kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open failed");
    if (kd == NULL) {
        PyErr_SetString(PyExc_RuntimeError, "kvm_open failed");
        return NULL;
    }

    if (kvm_getswapinfo(kd, kvmsw, 1, 0) < 0) {
        kvm_close(kd);
        PyErr_SetString(PyExc_RuntimeError, "kvm_getswapinfo failed");
        return NULL;
    }

    kvm_close(kd);

    if (sysctlbyname("vm.stats.vm.v_swapin", &swapin, &size, NULL, 0) == -1)
        goto sbn_error;
    if (sysctlbyname("vm.stats.vm.v_swapout", &swapout, &size, NULL, 0) == -1)
        goto sbn_error;
    if (sysctlbyname("vm.stats.vm.v_vnodein", &nodein, &size, NULL, 0) == -1)
        goto sbn_error;
    if (sysctlbyname("vm.stats.vm.v_vnodeout", &nodeout, &size, NULL, 0) == -1)
        goto sbn_error;

    return Py_BuildValue("(iiiII)",
                         kvmsw[0].ksw_total,                     // total
                         kvmsw[0].ksw_used,                      // used
                         kvmsw[0].ksw_total - kvmsw[0].ksw_used, // free
                         swapin + swapout,                       // swap in
                         nodein + nodeout);                      // swap out

sbn_error:
    PyErr_SetFromErrno(PyExc_OSError);
    return NULL;
}
Example #26
0
int ProcessInfo::getResidentSize() {
    kvm_t* kd = NULL;
    int cnt = 0;
    char err[_POSIX2_LINE_MAX] = {0};
    if ((kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, err)) == NULL) {
        log() << "Unable to get res mem size: " << err << endl;
        return -1;
    }
    kinfo_proc* task = kvm_getprocs(kd, KERN_PROC_PID, _pid.toNative(), sizeof(kinfo_proc), &cnt);
    kvm_close(kd);
    return (task->p_vm_rssize * sysconf(_SC_PAGESIZE)) / 1048576;  // convert from pages to MB
}
Example #27
0
File: net.c Project: Shmuma/z
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;
}
Example #28
0
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);
}
  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;
  }
Example #30
0
GHashTable *
ck_unix_pid_get_env_hash (pid_t pid)
{
        GHashTable       *hash = NULL;
        char            **penv;
        char              errbuf[_POSIX2_LINE_MAX];
        kvm_t            *kd;
        struct kinfo_proc p;
        int               i;

        kd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, errbuf);
        if (kd == NULL) {
		g_warning ("kvm_openfiles failed: %s", errbuf);
                return NULL;
        }

        if (! get_kinfo_proc (pid, &p)) {
		g_warning ("get_kinfo_proc failed: %s", g_strerror (errno));
		goto fail;
        }

        penv = kvm_getenvv (kd, &p, 0);
        if (penv == NULL) {
		g_warning ("kvm_getenvv failed: %s", kvm_geterr (kd));
		goto fail;
        }

        hash = g_hash_table_new_full (g_str_hash,
                                      g_str_equal,
                                      g_free,
                                      g_free);

        for (i = 0; penv[i] != NULL; i++) {
                char **vals;

                if (!penv[i][0]) continue;

                vals = g_strsplit (penv[i], "=", 2);
                if (vals != NULL) {
                        g_hash_table_insert (hash,
                                             g_strdup (vals[0]),
                                             g_strdup (vals[1]));
                        g_strfreev (vals);
                }
        }

fail:
        kvm_close (kd);

        return hash;
}