|
|
|
#if defined(__MACH__) && defined(__APPLE__)
|
|
|
|
# define __MacOS__
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__MacOS__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/sysctl.h>
|
|
|
|
# include <sys/user.h>
|
|
|
|
# include <sys/proc.h>
|
|
|
|
# include <libgen.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
|
|
|
|
# include <libgen.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
# include <kvm.h>
|
|
|
|
# include <limits.h>
|
|
|
|
# include <sys/proc.h>
|
|
|
|
# include <sys/param.h>
|
|
|
|
# include <sys/resource.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__MacOS__)
|
|
|
|
# include <libproc.h>
|
|
|
|
# include <sys/proc_info.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "process.h"
|
|
|
|
#include <Eina.h>
|
|
|
|
#include <Ecore.h>
|
|
|
|
#include <Ecore_File.h>
|
|
|
|
|
|
|
|
#if defined(__linux__) && !defined(PF_KTHREAD)
|
|
|
|
# define PF_KTHREAD 0x00200000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "macros.h"
|
|
|
|
|
|
|
|
static Eina_Bool _show_kthreads = 1;
|
|
|
|
|
|
|
|
void
|
|
|
|
proc_info_kthreads_show_set(Eina_Bool enabled)
|
|
|
|
{
|
|
|
|
_show_kthreads = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
Eina_Bool
|
|
|
|
proc_info_kthreads_show_get(void)
|
|
|
|
{
|
|
|
|
return _show_kthreads;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char * _states[255];
|
|
|
|
|
|
|
|
static void
|
|
|
|
_states_init(void)
|
|
|
|
{
|
|
|
|
#if defined(__linux__)
|
|
|
|
_states['D'] = "dsleep";
|
|
|
|
_states['I'] = "idle";
|
|
|
|
_states['R'] = "run";
|
|
|
|
_states['S'] = "sleep";
|
|
|
|
_states['T'] = "stop";
|
|
|
|
_states['X'] = "dead";
|
|
|
|
_states['Z'] = "zombie";
|
|
|
|
#else
|
|
|
|
_states[SIDL] = "idle";
|
|
|
|
_states[SRUN] = "run";
|
|
|
|
_states[SSLEEP] = "sleep";
|
|
|
|
_states[SSTOP] = "stop";
|
|
|
|
#if !defined(__MacOS__)
|
|
|
|
#if !defined(__OpenBSD__)
|
|
|
|
_states[SWAIT] = "wait";
|
|
|
|
_states[SLOCK] = "lock";
|
|
|
|
_states[SZOMB] = "zombie";
|
|
|
|
#endif
|
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
_states[SDEAD] = "dead";
|
|
|
|
_states[SONPROC] = "run";
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
_process_state_name(char state)
|
|
|
|
{
|
|
|
|
static int init = 0;
|
|
|
|
if (!init)
|
|
|
|
{
|
|
|
|
_states_init();
|
|
|
|
init = 1;
|
|
|
|
}
|
|
|
|
return _states[toupper(state)];
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
|
|
|
|
static unsigned long
|
|
|
|
_parse_line(const char *line)
|
|
|
|
{
|
|
|
|
char *p, *tok;
|
|
|
|
|
|
|
|
p = strchr(line, ':') + 1;
|
|
|
|
while (isspace(*p))
|
|
|
|
p++;
|
|
|
|
tok = strtok(p, " ");
|
|
|
|
|
|
|
|
return atol(tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_mem_size(Proc_Info *proc)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
char buf[4096];
|
|
|
|
unsigned int dummy, size, shared, resident, data, text;
|
|
|
|
static int pagesize = 0;
|
|
|
|
|
|
|
|
if (!pagesize) pagesize = getpagesize();
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "/proc/%d/statm", proc->pid);
|
|
|
|
f = fopen(buf, "r");
|
|
|
|
if (!f) return;
|
|
|
|
|
|
|
|
if (fgets(buf, sizeof(buf), f))
|
|
|
|
{
|
|
|
|
if (sscanf(buf, "%u %u %u %u %u %u %u",
|
|
|
|
&size, &resident, &shared, &text,
|
|
|
|
&dummy, &data, &dummy) == 7)
|
|
|
|
{
|
|
|
|
proc->mem_rss = MEMSZ(resident) * MEMSZ(pagesize);
|
|
|
|
proc->mem_shared = MEMSZ(shared) * MEMSZ(pagesize);
|
|
|
|
proc->mem_size = proc->mem_rss - proc->mem_shared;
|
|
|
|
proc->mem_virt = MEMSZ(size) * MEMSZ(pagesize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_cmd_args(Proc_Info *p, char *name, size_t len)
|
|
|
|
{
|
|
|
|
char buf[8192];
|
|
|
|
int pid = p->pid;
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "/proc/%d/exe", pid);
|
|
|
|
char *link = ecore_file_readlink(buf);
|
|
|
|
if (link)
|
|
|
|
{
|
|
|
|
snprintf(name, len, "%s", ecore_file_file_get(link));
|
|
|
|
free(link);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
|
|
|
|
FILE *f = fopen(buf, "r");
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
if (fgets(buf, sizeof(buf), f))
|
|
|
|
{
|
|
|
|
Eina_Strbuf *b = eina_strbuf_new();
|
|
|
|
const char *n;
|
|
|
|
|
|
|
|
if (ecore_file_exists(buf))
|
|
|
|
snprintf(name, len, "%s", ecore_file_file_get(buf));
|
|
|
|
|
|
|
|
n = buf;
|
|
|
|
while (n && *n && (*n + 1))
|
|
|
|
{
|
|
|
|
eina_strbuf_append(b, n);
|
|
|
|
n = strchr(n, '\0') + 1;
|
|
|
|
if (n && *n && (*n + 1)) eina_strbuf_append(b, " ");
|
|
|
|
}
|
|
|
|
p->arguments = eina_strbuf_release(b);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *end = strchr(name, ' ');
|
|
|
|
if (end) *end = '\0';
|
|
|
|
|
|
|
|
p->command = strdup(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_uid(int pid)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
char buf[4096];
|
|
|
|
int uid = 0;
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf),"/proc/%d/status", pid);
|
|
|
|
f = fopen(buf, "r");
|
|
|
|
if (!f) return -1;
|
|
|
|
|
|
|
|
while ((fgets(buf, sizeof(buf), f)) != NULL)
|
|
|
|
{
|
|
|
|
if (!strncmp(buf, "Uid:", 4))
|
|
|
|
{
|
|
|
|
uid = _parse_line(buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t
|
|
|
|
_boot_time(void)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int64_t boot_time;
|
|
|
|
char buf[4096];
|
|
|
|
double uptime = 0.0;
|
|
|
|
|
|
|
|
f = fopen("/proc/uptime", "r");
|
|
|
|
if (!f) return 0;
|
|
|
|
|
|
|
|
if (fgets(buf, sizeof(buf), f))
|
|
|
|
sscanf(buf, "%lf", &uptime);
|
|
|
|
else boot_time = 0;
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (uptime > 0.0)
|
|
|
|
boot_time = time(NULL) - (time_t) uptime;
|
|
|
|
|
|
|
|
return boot_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int pid, ppid, utime, stime, cutime, cstime;
|
|
|
|
int psr, pri, nice, numthreads;
|
|
|
|
long long int start_time, run_time;
|
|
|
|
char state;
|
|
|
|
unsigned int mem_rss, flags;
|
|
|
|
unsigned long mem_virt;
|
|
|
|
char name[1024];
|
|
|
|
} Stat;
|
|
|
|
|
|
|
|
static Eina_Bool
|
|
|
|
_stat(const char *path, Stat *st)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
char line[4096];
|
|
|
|
char name[1024];
|
|
|
|
int dummy, len = 0;
|
|
|
|
static long tck = 0;
|
|
|
|
static int64_t boot_time = 0;
|
|
|
|
|
|
|
|
if (!boot_time) boot_time = _boot_time();
|
|
|
|
|
|
|
|
memset(st, 0, sizeof(Stat));
|
|
|
|
|
|
|
|
f = fopen(path, "r");
|
|
|
|
if (!f) return 0;
|
|
|
|
|
|
|
|
if (fgets(line, sizeof(line), f))
|
|
|
|
{
|
|
|
|
|
|
|
|
len = sscanf(line, "%d %s %c %d %d %d %d %d %u %u %u %u %u %d %d %d"
|
|
|
|
" %d %d %d %u %u %lld %lu %u %u %u %u %u %u %u %d %d %d %d %u"
|
|
|
|
" %d %d %d %d %d %d %d %d %d", &dummy, name,
|
|
|
|
&st->state, &st->ppid, &dummy, &dummy, &dummy, &dummy, &st->flags,
|
|
|
|
&dummy, &dummy, &dummy, &dummy, &st->utime, &st->stime, &st->cutime,
|
|
|
|
&st->cstime, &st->pri, &st->nice, &st->numthreads, &dummy, &st->start_time,
|
|
|
|
&st->mem_virt, &st->mem_rss, &dummy, &dummy, &dummy, &dummy, &dummy,
|
|
|
|
&dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy,
|
|
|
|
&dummy, &dummy, &st->psr, &dummy, &dummy, &dummy, &dummy, &dummy);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (len != 44) return 0;
|
|
|
|
|
|
|
|
len = strlen(name);
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
name[len-1] = '\0';
|
|
|
|
snprintf(st->name, sizeof(st->name), "%s", &name[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tck) tck = sysconf(_SC_CLK_TCK);
|
|
|
|
|
|
|
|
st->start_time /= tck;
|
|
|
|
st->start_time += boot_time;
|
|
|
|
st->run_time = (st->utime + st->stime) / tck;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_List *
|
|
|
|
_process_list_linux_get(void)
|
|
|
|
{
|
|
|
|
Eina_List *files, *list;
|
|
|
|
const char *state;
|
|
|
|
char *n;
|
|
|
|
char buf[4096];
|
|
|
|
Stat st;
|
|
|
|
|
|
|
|
list = NULL;
|
|
|
|
|
|
|
|
files = ecore_file_ls("/proc");
|
|
|
|
EINA_LIST_FREE(files, n)
|
|
|
|
{
|
|
|
|
int pid = atoi(n);
|
|
|
|
free(n);
|
|
|
|
|
|
|
|
if (!pid) continue;
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "/proc/%d/stat", pid);
|
|
|
|
if (!_stat(buf, &st))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (st.flags & PF_KTHREAD && !proc_info_kthreads_show_get())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Proc_Info *p = calloc(1, sizeof(Proc_Info));
|
|
|
|
if (!p) return NULL;
|
|
|
|
|
|
|
|
p->pid = pid;
|
|
|
|
p->ppid = st.ppid;
|
|
|
|
p->uid = _uid(pid);
|
|
|
|
p->cpu_id = st.psr;
|
|
|
|
p->start = st.start_time;
|
|
|
|
p->run_time = st.run_time;
|
|
|
|
if (toupper(st.state) == 'S') p->ssleep = 1;
|
|
|
|
state = _process_state_name(st.state);
|
|
|
|
snprintf(p->state, sizeof(p->state), "%s", state);
|
|
|
|
p->cpu_time = st.utime + st.stime;
|
|
|
|
p->nice = st.nice;
|
|
|
|
p->priority = st.pri;
|
|
|
|
p->numthreads = st.numthreads;
|
|
|
|
if (st.flags & PF_KTHREAD)
|
|
|
|
p->is_kernel = 1;
|
|
|
|
_mem_size(p);
|
|
|
|
_cmd_args(p, st.name, sizeof(st.name));
|
|
|
|
|
|
|
|
list = eina_list_append(list, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_proc_thread_info(Proc_Info *p)
|
|
|
|
{
|
|
|
|
Eina_List *files;
|
|
|
|
const char *state;
|
|
|
|
char *n;
|
|
|
|
char buf[4096];
|
|
|
|
Stat st;
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "/proc/%d/task", p->pid);
|
|
|
|
files = ecore_file_ls(buf);
|
|
|
|
EINA_LIST_FREE(files, n)
|
|
|
|
{
|
|
|
|
int tid = atoi(n);
|
|
|
|
free(n);
|
|
|
|
snprintf(buf, sizeof(buf), "/proc/%d/task/%d/stat", p->pid, tid);
|
|
|
|
if (!_stat(buf, &st))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Proc_Info *t = calloc(1, sizeof(Proc_Info));
|
|
|
|
if (!t) continue;
|
|
|
|
t->cpu_id = st.psr;
|
|
|
|
if (toupper(st.state) == 'S') p->ssleep = 1;
|
|
|
|
state = _process_state_name(st.state);
|
|
|
|
snprintf(t->state, sizeof(t->state), "%s", state);
|
|
|
|
t->cpu_time = st.utime + st.stime;
|
|
|
|
t->nice = st.nice;
|
|
|
|
t->priority = st.pri;
|
|
|
|
t->numthreads = st.numthreads;
|
|
|
|
t->mem_virt = st.mem_virt;
|
|
|
|
t->mem_rss = st.mem_rss;
|
|
|
|
|
|
|
|
t->tid = tid;
|
|
|
|
t->thread_name = strdup(st.name);
|
|
|
|
|
|
|
|
p->threads = eina_list_append(p->threads, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Proc_Info *
|
|
|
|
proc_info_by_pid(int pid)
|
|
|
|
{
|
|
|
|
const char *state;
|
|
|
|
Stat st;
|
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "/proc/%d/stat", pid);
|
|
|
|
if (!_stat(buf, &st))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Proc_Info *p = calloc(1, sizeof(Proc_Info));
|
|
|
|
if (!p) return NULL;
|
|
|
|
|
|
|
|
p->pid = pid;
|
|
|
|
p->ppid = st.ppid;
|
|
|
|
p->uid = _uid(pid);
|
|
|
|
p->cpu_id = st.psr;
|
|
|
|
p->start = st.start_time;
|
|
|
|
p->run_time = st.run_time;
|
|
|
|
if (toupper(st.state) == 'S') p->ssleep = 1;
|
|
|
|
state = _process_state_name(st.state);
|
|
|
|
snprintf(p->state, sizeof(p->state), "%s", state);
|
|
|
|
p->cpu_time = st.utime + st.stime;
|
|
|
|
p->priority = st.pri;
|
|
|
|
p->nice = st.nice;
|
|
|
|
p->numthreads = st.numthreads;
|
|
|
|
if (st.flags & PF_KTHREAD) p->is_kernel = 1;
|
|
|
|
_mem_size(p);
|
|
|
|
_cmd_args(p, st.name, sizeof(st.name));
|
|
|
|
|
|
|
|
_proc_thread_info(p);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
|
|
|
|
static void
|
|
|
|
_proc_get(Proc_Info *p, struct kinfo_proc *kp)
|
|
|
|
{
|
|
|
|
static int pagesize = 0;
|
|
|
|
const char *state;
|
|
|
|
|
|
|
|
if (!pagesize) pagesize = getpagesize();
|
|
|
|
|
|
|
|
p->pid = kp->p_pid;
|
|
|
|
p->ppid = kp->p_ppid;
|
|
|
|
p->uid = kp->p_uid;
|
|
|
|
p->cpu_id = kp->p_cpuid;
|
|
|
|
p->start = kp->p_ustart_sec;
|
|
|
|
p->run_time = kp->p_uutime_sec + kp->p_ustime_sec +
|
|
|
|
(kp->p_uutime_usec / 1000000) + (kp->p_ustime_usec / 1000000);
|
|
|
|
if (kp->p_stat == SSLEEP)
|
|
|
|
{
|
|
|
|
state = kp->p_wmesg;
|
|
|
|
p->ssleep = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
state = _process_state_name(kp->p_stat);
|
|
|
|
snprintf(p->state, sizeof(p->state), "%s", state);
|
|
|
|
p->cpu_time = kp->p_uticks + kp->p_sticks + kp->p_iticks;
|
|
|
|
p->mem_virt = p->mem_size = (MEMSZ(kp->p_vm_tsize) * MEMSZ(pagesize)) +
|
|
|
|
(MEMSZ(kp->p_vm_dsize) * MEMSZ(pagesize)) + (MEMSZ(kp->p_vm_ssize) * MEMSZ(pagesize));
|
|
|
|
p->mem_rss = MEMSZ(kp->p_vm_rssize) * MEMSZ(pagesize);
|
|
|
|
p->priority = kp->p_priority - PZERO;
|
|
|
|
p->nice = kp->p_nice - NZERO;
|
|
|
|
p->tid = kp->p_tid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_cmd_get(Proc_Info *p, kvm_t *kern, struct kinfo_proc *kp)
|
|
|
|
{
|
|
|
|
char **args;
|
|
|
|
char name[4096];
|
|
|
|
|
|
|
|
if ((args = kvm_getargv(kern, kp, sizeof(name)-1)))
|
|
|
|
{
|
|
|
|
Eina_Strbuf *buf = eina_strbuf_new();
|
|
|
|
for (int i = 0; args[i]; i++)
|
|
|
|
{
|
|
|
|
eina_strbuf_append(buf, args[i]);
|
|
|
|
if (args[i + 1])
|
|
|
|
eina_strbuf_append(buf, " ");
|
|
|
|
}
|
|
|
|
p->arguments = eina_strbuf_string_steal(buf);
|
|
|
|
eina_strbuf_free(buf);
|
|
|
|
|
|
|
|
if (args[0] && ecore_file_exists(args[0]))
|
|
|
|
p->command = strdup(ecore_file_file_get(args[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!p->command)
|
|
|
|
p->command = strdup(kp->p_comm);
|
|
|
|
}
|
|
|
|
|
|
|
|
Proc_Info *
|
|
|
|
proc_info_by_pid(int pid)
|
|
|
|
{
|
|
|
|
struct kinfo_proc *kp, *kpt;
|
|
|
|
kvm_t *kern;
|
|
|
|
char errbuf[_POSIX2_LINE_MAX];
|
|
|
|
int count, pid_count;
|
|
|
|
|
|
|
|
kern = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
|
|
|
|
if (!kern) return NULL;
|
|
|
|
|
|
|
|
kp = kvm_getprocs(kern, KERN_PROC_PID, pid, sizeof(*kp), &count);
|
|
|
|
if (!kp) return NULL;
|
|
|
|
|
|
|
|
if (count == 0) return NULL;
|
|
|
|
|
|
|
|
Proc_Info *p = calloc(1, sizeof(Proc_Info));
|
|
|
|
if (!p) return NULL;
|
|
|
|
|
|
|
|
_proc_get(p, kp);
|
|
|
|
_cmd_get(p, kern, kp);
|
|
|
|
|
|
|
|
kp = kvm_getprocs(kern, KERN_PROC_SHOW_THREADS, 0, sizeof(*kp), &pid_count);
|
|
|
|
|
|
|
|
for (int i = 0; i < pid_count; i++)
|
|
|
|
{
|
|
|
|
if (kp[i].p_pid != p->pid) continue;
|
|
|
|
|
|
|
|
kpt = &kp[i];
|
|
|
|
|
|
|
|
if (kpt->p_tid <= 0) continue;
|
|
|
|
|
|
|
|
Proc_Info *t = calloc(1, sizeof(Proc_Info));
|
|
|
|
if (!t) continue;
|
|
|
|
|
|
|
|
_proc_get(t, kpt);
|
|
|
|
|
|
|
|
t->tid = kpt->p_tid;
|
|
|
|
t->thread_name = strdup(kpt->p_comm);
|
|
|
|
|
|
|
|
p->threads = eina_list_append(p->threads, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
p->numthreads = eina_list_count(p->threads);
|
|
|
|
|
|
|
|
kvm_close(kern);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_List *
|
|
|
|
_process_list_openbsd_get(void)
|
|
|
|
{
|
|
|
|
struct kinfo_proc *kps, *kp;
|
|
|
|
Proc_Info *p;
|
|
|
|
char errbuf[4096];
|
|
|
|
kvm_t *kern;
|
|
|
|
int pid_count;
|
|
|
|
Eina_List *list = NULL;
|
|
|
|
|
|
|
|
kern = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
|
|
|
|
if (!kern) return NULL;
|
|
|
|
|
|
|
|
kps = kvm_getprocs(kern, KERN_PROC_ALL, 0, sizeof(*kps), &pid_count);
|
|
|
|
if (!kps) return NULL;
|
|
|
|
|
|
|
|
for (int i = 0; i < pid_count; i++)
|
|
|
|
{
|
|
|
|
p = calloc(1, sizeof(Proc_Info));
|
|
|
|
if (!p) return NULL;
|
|
|
|
|
|
|
|
kp = &kps[i];
|
|
|
|
|
|
|
|
Proc_Info *p = proc_info_by_pid(kp->p_pid);
|
|
|
|
if (p)
|
|
|
|
list = eina_list_append(list, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
kvm_close(kern);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__MacOS__)
|
|
|
|
static void
|
|
|
|
_cmd_get(Proc_Info *p, int pid)
|
|
|
|
{
|
|
|
|
char *cp, *args, **argv;
|
|
|
|
int mib[3], argmax, argc;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_ARGMAX;
|
|
|
|
|
|
|
|
size = sizeof(argmax);
|
|
|
|
|
|
|
|
if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) return;
|
|
|
|
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_PROCARGS2;
|
|
|
|
mib[2] = pid;
|
|
|
|
|
|
|
|
size = (size_t) argmax;
|
|
|
|
args = malloc(argmax);
|
|
|
|
if (!args) return;
|
|
|
|
|
|
|
|
/* See libtop.c (top) for the origin of this comment, which is necessary as
|
|
|
|
* there is little other documentation...thanks Apple.
|
|
|
|
*
|
|
|
|
* Make a sysctl() call to get the raw argument space of the process.
|
|
|
|
* The layout is documented in start.s, which is part of the Csu
|
|
|
|
* project. In summary, it looks like:
|
|
|
|
*
|
|
|
|
* /---------------\ 0x00000000
|
|
|
|
* : :
|
|
|
|
* : :
|
|
|
|
* |---------------|
|
|
|
|
* | argc |
|
|
|
|
* |---------------|
|
|
|
|
* | arg[0] |
|
|
|
|
* |---------------|
|
|
|
|
* : :
|
|
|
|
* : :
|
|
|
|
* |---------------|
|
|
|
|
* | arg[argc - 1] |
|
|
|
|
* |---------------|
|
|
|
|
* | 0 |
|
|
|
|
* |---------------|
|
|
|
|
* | env[0] |
|
|
|
|
* |---------------|
|
|
|
|
* : :
|
|
|
|
* : :
|
|
|
|
* |---------------|
|
|
|
|
* | env[n] |
|
|
|
|
* |---------------|
|
|
|
|
* | 0 |
|
|
|
|
* |---------------| <-- Beginning of data returned by sysctl() is here.
|
|
|
|
* | argc |
|
|
|
|
* |---------------|
|
|
|
|
* | exec_path |
|
|
|
|
* |:::::::::::::::|
|
|
|
|
* | |
|
|
|
|
* | String area. |
|
|
|
|
* | |
|
|
|
|
* |---------------| <-- Top of stack.
|
|
|
|
* : :
|
|
|
|
* : :
|
|
|
|
* \---------------/ 0xffffffff
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (sysctl(mib, 3, args, &size, NULL, 0) == -1) return;
|
|
|
|
|
|
|
|
memcpy(&argc, args, sizeof(argc));
|
|
|
|
cp = args + sizeof(argc);
|
|
|
|
|
|
|
|
/* Skip exec path */
|
|
|
|
for (;cp < &args[size]; cp++)
|
|
|
|
{
|
|
|
|
if (*cp == '\0') break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cp == &args[size]) return;
|
|
|
|
|
|
|
|
/* Skip any padded NULLs. */
|
|
|
|
for (;cp < &args[size]; cp++)
|
|
|
|
{
|
|
|
|
if (*cp == '\0') break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cp == &args[size]) return;
|
|
|
|
|
|
|
|
argv = malloc(1 + argc * sizeof(char *));
|
|
|
|
if (!argv) return;
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
argv[i] = cp;
|
|
|
|
|
|
|
|
for (cp = args + sizeof(int); cp < &args[size] && i < argc; cp++)
|
|
|
|
{
|
|
|
|
if (*cp == '\0')
|
|
|
|
{
|
|
|
|
while (*cp == '\0') cp++;
|
|
|
|
argv[i++] = cp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == 0) i++;
|
|
|
|
|
|
|
|
argv[i] = NULL;
|
|
|
|
|
|
|
|
p->command = strdup(basename(argv[0]));
|
|
|
|
|
|
|
|
Eina_Strbuf *buf = eina_strbuf_new();
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
eina_strbuf_append_printf(buf, "%s ", argv[i]);
|
|
|
|
|
|
|
|
if (argc > 0)
|
|
|
|
p->arguments = eina_strbuf_release(buf);
|
|
|
|
else
|
|
|
|
eina_strbuf_free(buf);
|
|
|
|
|
|
|
|
free(args);
|
|
|
|
free(argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Proc_Info *
|
|
|
|
_proc_pidinfo(size_t pid)
|
|
|
|
{
|
|
|
|
const char *state;
|
|
|
|
struct proc_taskallinfo taskinfo;
|
|
|
|
int size = proc_pidinfo(pid, PROC_PIDTASKALLINFO, 0, &taskinfo, sizeof(taskinfo));
|
|
|
|
if (size != sizeof(taskinfo)) return NULL;
|
|
|
|
|
|
|
|
Proc_Info *p = calloc(1, sizeof(Proc_Info));
|
|
|
|
if (!p) return NULL;
|
|
|
|
|
|
|
|
p->pid = pid;
|
|
|
|
p->ppid = taskinfo.pbsd.pbi_ppid;
|
|
|
|
p->uid = taskinfo.pbsd.pbi_uid;
|
|
|
|
p->cpu_id = -1;
|
|
|
|
p->cpu_time = taskinfo.ptinfo.pti_total_user +
|
|
|
|
taskinfo.ptinfo.pti_total_system;
|
|
|
|
p->cpu_time /= 10000000;
|
|
|
|
p->start = taskinfo.pbsd.pbi_start_tvsec;
|
|
|
|
state = _process_state_name(taskinfo.pbsd.pbi_status);
|
|
|
|
snprintf(p->state, sizeof(p->state), "%s", state);
|
|
|
|
p->mem_size = p->mem_virt = taskinfo.ptinfo.pti_virtual_size;
|
|
|
|
p->mem_rss = taskinfo.ptinfo.pti_resident_size;
|
|
|
|
p->priority = taskinfo.ptinfo.pti_priority;
|
|
|
|
p->nice = taskinfo.pbsd.pbi_nice;
|
|
|
|
p->numthreads = taskinfo.ptinfo.pti_threadnum;
|
|
|
|
_cmd_get(p, pid);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_List *
|
|
|
|
_process_list_macos_fallback_get(void)
|
|
|
|
{
|
|
|
|
Eina_List *list = NULL;
|
|
|
|
|
|
|
|
for (int i = 1; i <= PID_MAX; i++)
|
|
|
|
{
|
|
|
|
Proc_Info *p = _proc_pidinfo(i);
|
|
|
|
if (p)
|
|
|
|
list = eina_list_append(list, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_List *
|
|
|
|
_process_list_macos_get(void)
|
|
|
|
{
|
|
|
|
Eina_List *list = NULL;
|
|
|
|
pid_t *pids = NULL;
|
|
|
|
int size, pid_count;
|
|
|
|
|
|
|
|
size = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
|
|
|
|
if (size == -1)
|
|
|
|
return _process_list_macos_fallback_get();
|
|
|
|
|
|
|
|
pids = malloc(size * sizeof(pid_t));
|
|
|
|
if (!pids) return NULL;
|
|
|
|
|
|
|
|
size = proc_listpids(PROC_ALL_PIDS, 0, pids, size * sizeof(pid_t));
|
|
|
|
if (size == -1)
|
|
|
|
{
|
|
|
|
free(pids);
|
|
|
|
return _process_list_macos_fallback_get();
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_count = size / sizeof(pid_t);
|
|
|
|
for (int i = 0; i < pid_count; i++)
|
|
|
|
{
|
|
|
|
pid_t pid = pids[i];
|
|
|
|
Proc_Info *p = _proc_pidinfo(pid);
|
|
|
|
if (p)
|
|
|
|
list = eina_list_append(list, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(pids);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
Proc_Info *
|
|
|
|
proc_info_by_pid(int pid)
|
|
|
|
{
|
|
|
|
const char *state;
|
|
|
|
struct proc_taskallinfo taskinfo;
|
|
|
|
struct proc_workqueueinfo workqueue;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
size = proc_pidinfo(pid, PROC_PIDTASKALLINFO, 0, &taskinfo, sizeof(taskinfo));
|
|
|
|
if (size != sizeof(taskinfo))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
size = proc_pidinfo(pid, PROC_PIDWORKQUEUEINFO, 0, &workqueue, sizeof(workqueue));
|
|
|
|
if (size != sizeof(workqueue))
|
|
|
|
memset(&workqueue, 0, sizeof(struct proc_workqueueinfo));
|
|
|
|
|
|
|
|
Proc_Info *p = calloc(1, sizeof(Proc_Info));
|
|
|
|
if (!p) return NULL;
|
|
|
|
|
|
|
|
p->pid = pid;
|
|
|
|
p->uid = taskinfo.pbsd.pbi_uid;
|
|
|
|
p->ppid = taskinfo.pbsd.pbi_ppid;
|
|
|
|
p->cpu_id = workqueue.pwq_nthreads;
|
|
|
|
p->cpu_time = taskinfo.ptinfo.pti_total_user +
|
|
|
|
taskinfo.ptinfo.pti_total_system;
|
|
|
|
p->cpu_time /= 10000000;
|
|
|
|
p->start = taskinfo.pbsd.pbi_start_tvsec;
|
|
|
|
state = _process_state_name(taskinfo.pbsd.pbi_status);
|
|
|
|
snprintf(p->state, sizeof(p->state), "%s", state);
|
|
|
|
p->mem_size = p->mem_virt = taskinfo.ptinfo.pti_virtual_size;
|
|
|
|
p->mem_rss = taskinfo.ptinfo.pti_resident_size;
|
|
|
|
p->priority = taskinfo.ptinfo.pti_priority;
|
|
|
|
p->nice = taskinfo.pbsd.pbi_nice;
|
|
|
|
p->numthreads = taskinfo.ptinfo.pti_threadnum;
|
|
|
|
_cmd_get(p, pid);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
|
|
|
|