fbsd: meh kvm read.

The more information needed, the more this is necessary.
Could use other userspace libraries, but why?
:)
This commit is contained in:
Alastair Poole 2021-02-26 17:34:56 +00:00
parent 4a1c064972
commit 831168115c
2 changed files with 59 additions and 47 deletions

View File

@ -14,14 +14,10 @@ cc = meson.get_compiler('c')
deps = dependency('elementary', version: efl_version) deps = dependency('elementary', version: efl_version)
deps_os = declare_dependency(link_args : [] ) deps_os = declare_dependency(link_args : [] )
if host_os == 'openbsd' or host_os == 'dragonfly' if host_os == 'freebsd' or host_os == 'openbsd' or host_os == 'dragonfly'
deps_os = declare_dependency(link_args : [ '-lkvm' ]) deps_os = declare_dependency(link_args : [ '-lkvm' ])
endif endif
if host_os == 'freebsd'
deps_os = declare_dependency(link_args : [ '-lkvm', '-lprocstat' ])
endif
##### dir locations ##### dir locations
dir_prefix = get_option('prefix') dir_prefix = get_option('prefix')
dir_bin = join_paths(dir_prefix, get_option('bindir')) dir_bin = join_paths(dir_prefix, get_option('bindir'))

View File

@ -22,7 +22,9 @@
#endif #endif
#if defined(__FreeBSD__) #if defined(__FreeBSD__)
#include <libprocstat.h> # define _WANT_FILE
# include <sys/file.h>
# include <sys/filedesc.h>
#endif #endif
#if defined(__MacOS__) #if defined(__MacOS__)
@ -864,44 +866,73 @@ _pid_max(void)
} }
static void static void
_cmd_get(Proc_Info *p, struct kinfo_proc *kp) _kvm_get(Proc_Info *p, struct kinfo_proc *kp)
{ {
kvm_t * kern; kvm_t * kern;
char **args;
char name[4096]; char name[4096];
Eina_Bool have_command = 0; Eina_Bool have_command = 0;
kern = kvm_open(NULL, "/dev/null", NULL, O_RDONLY, "kvm_open"); kern = kvm_open(NULL, "/dev/mem", NULL, O_RDONLY, "kvm_open");
if (kern) if (!kern) goto nokvm;
char **args;
if ((args = kvm_getargv(kern, kp, sizeof(name)-1)) && (args[0]))
{ {
if ((args = kvm_getargv(kern, kp, sizeof(name)-1)) && (args[0])) char *base = strdup(args[0]);
if (base)
{ {
char *base = strdup(args[0]); char *spc = strchr(base, ' ');
if (base) if (spc) *spc = '\0';
{
char *spc = strchr(base, ' ');
if (spc) *spc = '\0';
if (ecore_file_exists(base)) if (ecore_file_exists(base))
{
snprintf(name, sizeof(name), "%s", basename(base));
have_command = 1;
}
free(base);
}
Eina_Strbuf *buf = eina_strbuf_new();
for (int i = 0; args[i] != NULL; i++)
{ {
eina_strbuf_append(buf, args[i]); snprintf(name, sizeof(name), "%s", basename(base));
if (args[i + 1]) have_command = 1;
eina_strbuf_append(buf, " ");
} }
p->arguments = eina_strbuf_string_steal(buf); free(base);
eina_strbuf_free(buf);
} }
kvm_close(kern); Eina_Strbuf *buf = eina_strbuf_new();
for (int i = 0; args[i] != NULL; 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);
} }
struct filedesc filed;
struct fdescenttbl *fdt;
unsigned int n;
char buf[64];
if (!kvm_read(kern, (unsigned long)kp->ki_fd, &filed, sizeof(filed)))
goto kvmerror;
if (!kvm_read(kern, (unsigned long)filed.fd_files, &n, sizeof(n)))
goto kvmerror;
unsigned int size = sizeof(*fdt) + n * sizeof(struct filedescent);
fdt = malloc(size);
if (fdt)
{
if (kvm_read(kern, (unsigned long)filed.fd_files, fdt, size))
{
for (int i = 0; i < n; i++)
{
if (!fdt->fdt_ofiles[i].fde_file) continue;
snprintf(buf, sizeof(buf), "%i", i);
p->fds = eina_list_append(p->fds, strdup(buf));
p->numfiles++;
}
}
free(fdt);
}
kvmerror:
if (kern)
kvm_close(kern);
nokvm:
if (!have_command) if (!have_command)
snprintf(name, sizeof(name), "%s", kp->ki_comm); snprintf(name, sizeof(name), "%s", kp->ki_comm);
@ -914,7 +945,6 @@ _proc_thread_info(struct kinfo_proc *kp, Eina_Bool is_thread)
struct rusage *usage; struct rusage *usage;
const char *state; const char *state;
Proc_Info *p; Proc_Info *p;
char buf[128];
static int pagesize = 0; static int pagesize = 0;
if (!pagesize) pagesize = getpagesize(); if (!pagesize) pagesize = getpagesize();
@ -927,7 +957,7 @@ _proc_thread_info(struct kinfo_proc *kp, Eina_Bool is_thread)
p->uid = kp->ki_uid; p->uid = kp->ki_uid;
if (!is_thread) if (!is_thread)
_cmd_get(p, kp); _kvm_get(p, kp);
p->cpu_id = kp->ki_oncpu; p->cpu_id = kp->ki_oncpu;
if (p->cpu_id == -1) if (p->cpu_id == -1)
@ -954,20 +984,6 @@ _proc_thread_info(struct kinfo_proc *kp, Eina_Bool is_thread)
p->priority = kp->ki_pri.pri_level - PZERO; p->priority = kp->ki_pri.pri_level - PZERO;
p->numthreads = kp->ki_numthreads; p->numthreads = kp->ki_numthreads;
struct procstat *procstat = procstat_open_sysctl();
struct filestat *fst;
struct filestat_list *head, *next;
head = procstat_getfiles(procstat, kp, 0);
STAILQ_FOREACH(fst, head, next)
{
if (fst->fs_fd < 0) continue;
snprintf(buf, sizeof(buf), "%i", fst->fs_fd);
p->fds = eina_list_append(p->fds, strdup(buf));
}
p->numfiles = eina_list_count(p->fds);
procstat_freefiles(procstat, head);
p->tid = kp->ki_tid; p->tid = kp->ki_tid;
p->thread_name = strdup(kp->ki_tdname); p->thread_name = strdup(kp->ki_tdname);
if (kp->ki_flag & P_KPROC) p->is_kernel = 1; if (kp->ki_flag & P_KPROC) p->is_kernel = 1;