summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2014-09-08 16:19:08 +0200
committerTobias Klauser <tklauser@distanz.ch>2014-09-08 16:19:08 +0200
commit35edaad835f4f5685e7510b0a6c574c1f138c79c (patch)
treef1440a6e94a1cd98c4d454e2312d9adb5a75d8c2
parent01056309418e2ef06b23da03f129d7490aa13d34 (diff)
flowtop: Use integer conversion to determine PID proc entries
Try to convert the directory entry name to an unsigned integer with strtoul() instead of using strspn() to determine if a proc entry is a PID. If it is a valid PID (i.e. strtoul returned a value != 0), we can directly use it to pass into walk_process() and there set flow_entry->proc_num. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r--flowtop.c33
1 files changed, 20 insertions, 13 deletions
diff --git a/flowtop.c b/flowtop.c
index 0fa9663..c2d9db9 100644
--- a/flowtop.c
+++ b/flowtop.c
@@ -51,7 +51,8 @@ struct flow_entry {
char rev_dns_src[256], rev_dns_dst[256];
char cmdline[256];
struct flow_entry *next;
- int procnum, inode;
+ int inode;
+ unsigned int procnum;
};
struct flow_list {
@@ -388,15 +389,15 @@ static void flow_list_destroy(struct flow_list *fl)
spinlock_destroy(&fl->lock);
}
-static int walk_process(const char *process, struct flow_entry *n)
+static int walk_process(unsigned int pid, struct flow_entry *n)
{
int ret;
DIR *dir;
struct dirent *ent;
char path[1024];
- if (snprintf(path, sizeof(path), "/proc/%s/fd", process) == -1)
- panic("giant process name! %s\n", process);
+ if (snprintf(path, sizeof(path), "/proc/%u/fd", pid) == -1)
+ panic("giant process name! %u\n", pid);
dir = opendir(path);
if (!dir)
@@ -405,8 +406,8 @@ static int walk_process(const char *process, struct flow_entry *n)
while ((ent = readdir(dir))) {
struct stat statbuf;
- if (snprintf(path, sizeof(path), "/proc/%s/fd/%s",
- process, ent->d_name) < 0)
+ if (snprintf(path, sizeof(path), "/proc/%u/fd/%s",
+ pid, ent->d_name) < 0)
continue;
if (stat(path, &statbuf) < 0)
@@ -415,14 +416,14 @@ static int walk_process(const char *process, struct flow_entry *n)
if (S_ISSOCK(statbuf.st_mode) && (ino_t) n->inode == statbuf.st_ino) {
memset(n->cmdline, 0, sizeof(n->cmdline));
- snprintf(path, sizeof(path), "/proc/%s/exe", process);
+ snprintf(path, sizeof(path), "/proc/%u/exe", pid);
ret = readlink(path, n->cmdline,
sizeof(n->cmdline) - 1);
if (ret < 0)
panic("readlink error: %s\n", strerror(errno));
- n->procnum = atoi(process);
+ n->procnum = pid;
closedir(dir);
return 1;
}
@@ -449,11 +450,17 @@ static void walk_processes(struct flow_entry *n)
panic("Cannot open /proc!\n");
while ((ent = readdir(dir))) {
- if (strspn(ent->d_name, "0123456789") == strlen(ent->d_name)) {
- ret = walk_process(ent->d_name, n);
- if (ret > 0)
- break;
- }
+ const char *name = ent->d_name;
+ char *end;
+ unsigned int pid = strtoul(name, &end, 10);
+
+ /* not a PID */
+ if (pid == 0 && end == name)
+ continue;
+
+ ret = walk_process(pid, n);
+ if (ret > 0)
+ break;
}
closedir(dir);