Parses /proc/net/tcp only once.
* procopenfiles.c: (get_all_sockets), (glibtop_get_proc_open_files_s): Parses /proc/net/tcp only once.
This commit is contained in:
@@ -1,3 +1,8 @@
|
|||||||
|
2005-01-12 Benoît Dejean <TazForEver@dlfp.org>
|
||||||
|
|
||||||
|
* procopenfiles.c: (get_all_sockets),
|
||||||
|
(glibtop_get_proc_open_files_s): Parses /proc/net/tcp only once.
|
||||||
|
|
||||||
2005-01-10 Benoît Dejean <TazForEver@dlfp.org>
|
2005-01-10 Benoît Dejean <TazForEver@dlfp.org>
|
||||||
|
|
||||||
* procopenfiles.c: (get_socket_endpoint): Fixed.
|
* procopenfiles.c: (get_socket_endpoint): Fixed.
|
||||||
|
@@ -49,41 +49,49 @@ glibtop_init_proc_open_files_s (glibtop *server)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
get_socket_endpoint(char *buf, int *prmtport, int s)
|
struct SocketEndPoint
|
||||||
{
|
{
|
||||||
FILE *f;
|
char host[GLIBTOP_OPEN_DEST_HOST_LEN + 1];
|
||||||
|
int port;
|
||||||
|
int sock;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static GArray* get_all_sockets()
|
||||||
|
{
|
||||||
|
GArray *socks;
|
||||||
|
FILE *tcp;
|
||||||
char line[1024];
|
char line[1024];
|
||||||
|
|
||||||
buf[0] = '\0';
|
socks = g_array_new(FALSE, FALSE, sizeof(struct SocketEndPoint));
|
||||||
*prmtport = 0;
|
|
||||||
|
|
||||||
f = fopen("/proc/net/tcp", "r");
|
g_return_val_if_fail((tcp = fopen("/proc/net/tcp", "r")), socks);
|
||||||
if(!f)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if(!fgets(line, sizeof line, f)) goto eof;
|
if(!fgets(line, sizeof line, tcp)) goto eof;
|
||||||
|
|
||||||
|
while(fgets(line, sizeof line, tcp))
|
||||||
while(fgets(line, sizeof line, f))
|
|
||||||
{
|
{
|
||||||
unsigned int rmt;
|
struct SocketEndPoint sep;
|
||||||
int sock = -42;
|
unsigned addr;
|
||||||
|
|
||||||
sscanf(line, "%*d: %*x:%*x %8x:%4x %*x %*x:%*x %*x:%*x %*d %*d %*d %d",
|
if(sscanf(line, "%*d: %*x:%*x %8x:%4x %*x %*x:%*x %*x:%*x %*d %*d %*d %d",
|
||||||
&rmt, prmtport, &sock);
|
&addr, &sep.port, &sep.sock) != 3)
|
||||||
|
continue;
|
||||||
|
|
||||||
if(sock == s)
|
if(!inet_ntop(AF_INET, &addr, sep.host, sizeof sep.host))
|
||||||
{
|
continue;
|
||||||
inet_ntop(AF_INET, &rmt, buf, GLIBTOP_OPEN_DEST_HOST_LEN);
|
|
||||||
break;
|
g_array_append_val(socks, sep);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
eof:
|
eof:
|
||||||
fclose(f);
|
fclose(tcp);
|
||||||
|
return socks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Provides detailed information about a process' open files */
|
/* Provides detailed information about a process' open files */
|
||||||
|
|
||||||
glibtop_open_files_entry *
|
glibtop_open_files_entry *
|
||||||
@@ -91,8 +99,8 @@ glibtop_get_proc_open_files_s (glibtop *server, glibtop_proc_open_files *buf, pi
|
|||||||
{
|
{
|
||||||
char fn [BUFSIZ];
|
char fn [BUFSIZ];
|
||||||
GArray *entries;
|
GArray *entries;
|
||||||
|
GArray *socks = NULL;
|
||||||
struct dirent *direntry;
|
struct dirent *direntry;
|
||||||
int rv;
|
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
|
|
||||||
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_OPEN_FILES, 0);
|
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_OPEN_FILES, 0);
|
||||||
@@ -108,6 +116,7 @@ glibtop_get_proc_open_files_s (glibtop *server, glibtop_proc_open_files *buf, pi
|
|||||||
|
|
||||||
while((direntry = readdir(dir))) {
|
while((direntry = readdir(dir))) {
|
||||||
char tgt [BUFSIZ];
|
char tgt [BUFSIZ];
|
||||||
|
int rv;
|
||||||
glibtop_open_files_entry entry = {0};
|
glibtop_open_files_entry entry = {0};
|
||||||
|
|
||||||
|
|
||||||
@@ -125,12 +134,33 @@ glibtop_get_proc_open_files_s (glibtop *server, glibtop_proc_open_files *buf, pi
|
|||||||
|
|
||||||
if(g_str_has_prefix(tgt, "socket:["))
|
if(g_str_has_prefix(tgt, "socket:["))
|
||||||
{
|
{
|
||||||
|
unsigned i;
|
||||||
int sockfd;
|
int sockfd;
|
||||||
|
|
||||||
|
if(!socks)
|
||||||
|
socks = get_all_sockets();
|
||||||
|
|
||||||
entry.type = GLIBTOP_FILE_TYPE_INETSOCKET;
|
entry.type = GLIBTOP_FILE_TYPE_INETSOCKET;
|
||||||
|
|
||||||
sockfd = atoi(tgt + 8);
|
sockfd = atoi(tgt + 8);
|
||||||
get_socket_endpoint(entry.info.sock.dest_host,
|
|
||||||
&(entry.info.sock.dest_port),
|
for(i = 0; i < socks->len; ++i)
|
||||||
sockfd);
|
{
|
||||||
|
const struct SocketEndPoint *sep;
|
||||||
|
|
||||||
|
sep = & g_array_index(socks,
|
||||||
|
struct SocketEndPoint,
|
||||||
|
i);
|
||||||
|
|
||||||
|
if(sep->sock == sockfd)
|
||||||
|
{
|
||||||
|
entry.info.sock.dest_port = sep->port;
|
||||||
|
memcpy(entry.info.sock.dest_host,
|
||||||
|
sep->host,
|
||||||
|
sizeof sep->host);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if(g_str_has_prefix(tgt, "pipe:["))
|
else if(g_str_has_prefix(tgt, "pipe:["))
|
||||||
{
|
{
|
||||||
@@ -147,6 +177,9 @@ glibtop_get_proc_open_files_s (glibtop *server, glibtop_proc_open_files *buf, pi
|
|||||||
|
|
||||||
closedir (dir);
|
closedir (dir);
|
||||||
|
|
||||||
|
if(socks)
|
||||||
|
g_array_free(socks, TRUE);
|
||||||
|
|
||||||
buf->flags = _glibtop_sysdeps_proc_open_files;
|
buf->flags = _glibtop_sysdeps_proc_open_files;
|
||||||
buf->number = entries->len;
|
buf->number = entries->len;
|
||||||
buf->size = sizeof(glibtop_open_files_entry);
|
buf->size = sizeof(glibtop_open_files_entry);
|
||||||
|
Reference in New Issue
Block a user