]> git.itanic.dy.fi Git - scan-pagemap/commitdiff
parser: Add support for parsing through all PIDs
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 3 Jul 2010 20:10:53 +0000 (23:10 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 3 Jul 2010 20:11:41 +0000 (23:11 +0300)
This reads the entire /proc directory and parses all pids it can find
there.

Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
parse.c
parse.h

diff --git a/parse.c b/parse.c
index 55d6402f8e8d8d64cfc7f83b79e87f9c45e5901b..32660e2007e5217d02536b0bce937c075582117d 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -1,3 +1,5 @@
+#include <sys/types.h>
+#include <dirent.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -218,3 +220,50 @@ err_out:
 
        return;
 }
+
+static int get_next_pid(void)
+{
+       static DIR *dir = NULL;
+       struct dirent *dirent;
+       int error;
+
+       if (!dir) {
+               dir = opendir("/proc");
+               if (!dir) {
+                       error = errno;
+                       printf("Failed to open /proc directory: %s\n",
+                               strerror(error));
+                       return -1;
+               }
+       }
+
+restart:
+       dirent = readdir(dir);
+       if (!dirent) {
+               if (errno == 0) {
+                       closedir(dir);
+                       dir = NULL;
+                       return 0;
+               }
+               error = errno;
+               printf("Failed to read directory: %s\n", strerror(error));
+               return -1;
+       }
+
+       printf("%s\n", dirent->d_name);
+       if (dirent->d_name[0] < '0' || dirent->d_name[0] > '9')
+               goto restart;
+
+       return atoi(dirent->d_name);
+}
+
+void scan_all_pids(struct pageframe *pf)
+{
+       int pid;
+       while(1) {
+               pid = get_next_pid();
+               if (pid <= 0)
+                       break;
+               read_pageframe(pid, pf);
+       }
+}
diff --git a/parse.h b/parse.h
index dcbe1f25329a11662ac26036af6757bad480e8b6..b3092d2f17c06afbea26714a5e27b82608b0a5dd 100644 (file)
--- a/parse.h
+++ b/parse.h
@@ -4,5 +4,6 @@
 #include "pagemap.h"
 
 void read_pageframe(int pid, struct pageframe *pageframe);
+void scan_all_pids(struct pageframe *pf);
 
 #endif