summaryrefslogtreecommitdiff
path: root/sysdeps/freebsd
diff options
context:
space:
mode:
authorBenoît Dejean <benoit@placenet.org>2007-07-01 13:21:07 +0000
committerBenoît Dejean <bdejean@src.gnome.org>2007-07-01 13:21:07 +0000
commit0285e018f4a461926941769a2064f5f349da8384 (patch)
treee9ede5b4417105bb51722956383a30cf503e1d3a /sysdeps/freebsd
parent01f1e7e955fbe1d559a6a104023fea77c70a9d4d (diff)
downloadlibgtop-0285e018f4a461926941769a2064f5f349da8384.tar.gz
Fixe some problems as well as implements procwd and procopenfiles for
2007-07-01 Benoît Dejean <benoit@placenet.org> Fixe some problems as well as implements procwd and procopenfiles for FreeBSD using a wrapper around lsof. This same backend should also work on kFreeBSD. Patch by Joe Marcus Clarke <marcus@freebsd.org> svn path=/trunk/; revision=2617
Diffstat (limited to 'sysdeps/freebsd')
-rw-r--r--sysdeps/freebsd/ChangeLog8
-rw-r--r--sysdeps/freebsd/Makefile.am8
-rw-r--r--sysdeps/freebsd/glibtop_server.h1
-rw-r--r--sysdeps/freebsd/proclist.c16
-rw-r--r--sysdeps/freebsd/procopenfiles.c139
5 files changed, 158 insertions, 14 deletions
diff --git a/sysdeps/freebsd/ChangeLog b/sysdeps/freebsd/ChangeLog
index dcff7cc5..29489cd1 100644
--- a/sysdeps/freebsd/ChangeLog
+++ b/sysdeps/freebsd/ChangeLog
@@ -1,3 +1,11 @@
+2007-07-01 Benoît Dejean <benoit@placenet.org>
+
+ Fixe some problems as well as implements procwd and procopenfiles
+ for FreeBSD using a wrapper around lsof. This same backend should
+ also work on kFreeBSD.
+
+ Patch by Joe Marcus Clarke <marcus@freebsd.org>
+
2007-02-18 Benoît Dejean <benoit@placenet.org>
* AUTHORS:
diff --git a/sysdeps/freebsd/Makefile.am b/sysdeps/freebsd/Makefile.am
index ee18d5c8..a48bb3bc 100644
--- a/sysdeps/freebsd/Makefile.am
+++ b/sysdeps/freebsd/Makefile.am
@@ -5,7 +5,9 @@ noinst_LTLIBRARIES = libgtop_sysdeps-2.0.la libgtop_sysdeps_suid-2.0.la
libgtop_sysdeps_2_0_la_SOURCES = nosuid.c siglist.c sysinfo.c shm_limits.c \
cpu.c msg_limits.c sem_limits.c loadavg.c \
- uptime.c netlist.c fsusage.c mem.c
+ uptime.c netlist.c fsusage.c mem.c \
+ procopenfiles.c procwd.c \
+ glibtop_private.c
libgtop_sysdeps_2_0_la_LDFLAGS = $(LT_VERSION_INFO)
@@ -14,11 +16,11 @@ libgtop_sysdeps_suid_2_0_la_SOURCES = open.c close.c swap.c \
proclist.c procstate.c procuid.c \
proctime.c procmem.c procsignal.c \
prockernel.c procsegment.c procargs.c \
- procmap.c netload.c ppp.c procopenfiles.c
+ procmap.c netload.c ppp.c
libgtop_sysdeps_suid_2_0_la_LDFLAGS = $(LT_VERSION_INFO)
libgtopinclude_HEADERS = glibtop_server.h glibtop_machine.h \
- glibtop_suid.h
+ glibtop_suid.h glibtop_private.h
libgtopincludedir = $(includedir)/libgtop-2.0
diff --git a/sysdeps/freebsd/glibtop_server.h b/sysdeps/freebsd/glibtop_server.h
index 9a20a5de..d73c645c 100644
--- a/sysdeps/freebsd/glibtop_server.h
+++ b/sysdeps/freebsd/glibtop_server.h
@@ -45,6 +45,7 @@ G_BEGIN_DECLS
#define GLIBTOP_SUID_MSG_LIMITS 0
#define GLIBTOP_SUID_SEM_LIMITS 0
#define GLIBTOP_SUID_NETLIST 0
+#define GLIBTOP_SUID_PROC_WD 0
G_END_DECLS
diff --git a/sysdeps/freebsd/proclist.c b/sysdeps/freebsd/proclist.c
index f335de8c..d65732d7 100644
--- a/sysdeps/freebsd/proclist.c
+++ b/sysdeps/freebsd/proclist.c
@@ -40,7 +40,7 @@ static const unsigned long _glibtop_sysdeps_proclist =
* buf->flags is only set if the call succeeded, in this case pids_chain,
* a list of the pids of all currently running processes is returned,
* buf->number is the number of elements of this list and buf->size is
- * the size of one single element (sizeof (unsigned)). The total size is
+ * the size of one single element (sizeof (pid_t)). The total size is
* stored in buf->total.
*
* The calling function has to free the memory to which a pointer is returned.
@@ -61,7 +61,7 @@ _glibtop_init_proclist_p (glibtop *server)
server->sysdeps.proclist = _glibtop_sysdeps_proclist;
}
-unsigned *
+pid_t *
glibtop_get_proclist_p (glibtop *server, glibtop_proclist *buf,
gint64 which, gint64 arg)
{
@@ -90,18 +90,18 @@ glibtop_get_proclist_p (glibtop *server, glibtop_proclist *buf,
len /= sizeof (struct kinfo_proc);
- pids = g_array_sized_new (FALSE, FALSE, sizeof (unsigned), len);
+ pids = g_array_sized_new (FALSE, FALSE, sizeof (pid_t), len);
for (i = 0; i < len; i++) {
- unsigned pid;
+ pid_t pid;
- pid = (unsigned) pinfo[i].ki_pid;
+ pid = (pid_t) pinfo[i].ki_pid;
switch (which & GLIBTOP_KERN_PROC_MASK) {
case GLIBTOP_KERN_PROC_ALL:
break;
case GLIBTOP_KERN_PROC_PID:
- if ((unsigned) arg != pid)
+ if ((pid_t) arg != pid)
continue;
break;
case GLIBTOP_KERN_PROC_UID:
@@ -144,9 +144,9 @@ glibtop_get_proclist_p (glibtop *server, glibtop_proclist *buf,
g_free (pinfo);
buf->flags = _glibtop_sysdeps_proclist;
- buf->size = sizeof (unsigned);
+ buf->size = sizeof (pid_t);
buf->number = pids->len;
buf->total = buf->number * buf->size;
- return (unsigned *) g_array_free (pids, FALSE);
+ return (pid_t *) g_array_free (pids, FALSE);
}
diff --git a/sysdeps/freebsd/procopenfiles.c b/sysdeps/freebsd/procopenfiles.c
index be5d20db..af00021c 100644
--- a/sysdeps/freebsd/procopenfiles.c
+++ b/sysdeps/freebsd/procopenfiles.c
@@ -1,5 +1,6 @@
/* Copyright (C) 1998-99 Martin Baulig
Copyright (C) 2004 Nicolás Lichtmaier
+ Copyright (C) 2007 Joe Marcus Clarke
This file is part of LibGTop 1.0.
Modified by Nicolás Lichtmaier to give a process open files.
@@ -26,8 +27,11 @@
#include <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/procopenfiles.h>
+#include <sys/types.h>
+#include <string.h>
+#include <stdlib.h>
-#include <glibtop_suid.h>
+#include "glibtop_private.h"
static const unsigned long _glibtop_sysdeps_proc_open_files =
(1L << GLIBTOP_PROC_OPEN_FILES_NUMBER)|
@@ -42,10 +46,139 @@ _glibtop_init_proc_open_files_s (glibtop *server)
server->sysdeps.proc_open_files = _glibtop_sysdeps_proc_open_files;
}
+static GArray *
+parse_output(const char *output) {
+ GArray *entries;
+ char **lines;
+ char *ftype = NULL;
+ char *fname = NULL;
+ guint i;
+ guint len;
+ int fd = -1;
+
+ entries = g_array_new(FALSE, FALSE, sizeof(glibtop_open_files_entry));
+
+ lines = g_strsplit(output, "\n", 0);
+ len = g_strv_length(lines);
+
+ for (i = 0; i < len && lines[i]; i++) {
+ glibtop_open_files_entry entry = {0};
+
+ if (strlen(lines[i]) < 2)
+ continue;
+
+ if (!g_str_has_prefix(lines[i], "f") &&
+ !g_str_has_prefix(lines[i], "t") &&
+ !g_str_has_prefix(lines[i], "n"))
+ continue;
+
+ if (g_str_has_prefix(lines[i], "f")) {
+ if (!g_ascii_isdigit(*(lines[i] + 1)))
+ i += 2;
+ else
+ fd = atoi(lines[i] + 1);
+ continue;
+ }
+
+ if (g_str_has_prefix(lines[i], "t")) {
+ ftype = lines[i];
+ ftype++;
+ continue;
+ } else {
+ fname = lines[i];
+ fname++;
+ }
+
+ if (ftype == NULL || fname == NULL)
+ continue;
+
+ if (!strcmp(ftype, "unix")) {
+ entry.type = GLIBTOP_FILE_TYPE_LOCALSOCKET;
+ g_strlcpy(entry.info.localsock.name, fname,
+ sizeof(entry.info.localsock.name));
+ } else if (!strcmp(ftype, "PIPE")) {
+ entry.type = GLIBTOP_FILE_TYPE_PIPE;
+ } else if (!strcmp(ftype, "VREG") ||
+ !strcmp(ftype, "GDIR") ||
+ !strcmp(ftype, "GREG") ||
+ !strcmp(ftype, "VCHR") ||
+ !strcmp(ftype, "VBLK") ||
+ !strcmp(ftype, "DIR") ||
+ !strcmp(ftype, "LINK") ||
+ !strcmp(ftype, "REG") ||
+ !strcmp(ftype, "VDIR")) {
+ entry.type = GLIBTOP_FILE_TYPE_FILE;
+ g_strlcpy(entry.info.file.name, fname,
+ sizeof(entry.info.file.name));
+ } else if (!strcmp(ftype, "IPv4")) {
+ char **hosts;
+ char **remote_host;
+
+ if (!strstr(fname, "->")) {
+ remote_host = g_strsplit(fname, ":", 0);
+ } else {
+ hosts = g_strsplit(fname, "->", 0);
+ if (g_strv_length(hosts) < 2) {
+ g_strfreev(hosts);
+ continue;
+ }
+
+ remote_host = g_strsplit(hosts[1], ":", 0);
+ g_strfreev(hosts);
+ }
+
+ if (g_strv_length(remote_host) < 2) {
+ g_strfreev(remote_host);
+ continue;
+ }
+
+ entry.type = GLIBTOP_FILE_TYPE_INETSOCKET;
+ if (!strcmp(remote_host[0], "*"))
+ g_strlcpy(entry.info.sock.dest_host, "0.0.0.0",
+ sizeof(entry.info.sock.dest_host));
+ else
+ g_strlcpy(entry.info.sock.dest_host,
+ remote_host[0],
+ sizeof(entry.info.sock.dest_host));
+ entry.info.sock.dest_port = atoi(remote_host[1]);
+
+ g_strfreev(remote_host);
+ } else
+ continue;
+
+ entry.fd = fd;
+
+ fd = -1;
+ ftype = NULL;
+ fname = NULL;
+
+ g_array_append_val(entries, entry);
+ }
+
+ g_strfreev(lines);
+
+ return entries;
+}
-/* XXX Unimplemented on FreeBSD */
glibtop_open_files_entry *
glibtop_get_proc_open_files_s (glibtop *server, glibtop_proc_open_files *buf, pid_t pid)
{
- return NULL;
+ char *output;
+ GArray *entries;
+
+ memset(buf, 0, sizeof (glibtop_proc_open_files));
+
+ output = execute_lsof(pid);
+ if (output == NULL) return NULL;
+
+ entries = parse_output(output);
+
+ g_free(output);
+
+ buf->flags = _glibtop_sysdeps_proc_open_files;
+ buf->number = entries->len;
+ buf->size = sizeof(glibtop_open_files_entry);
+ buf->total = buf->number * buf->size;
+
+ return (glibtop_open_files_entry*)g_array_free(entries, FALSE);
}