]> git.itanic.dy.fi Git - scan-pagemap/blobdiff - parse.c
Factor out all pid handling functions in a separate pidlib.c
[scan-pagemap] / parse.c
diff --git a/parse.c b/parse.c
index 278f17e4b701915eabd80076b0f439f60cc22ec0..6434b72c9b498c116dc66df39c42e06deae4d0ad 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -1,13 +1,11 @@
-#include <sys/types.h>
-#include <dirent.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
-#include <libgen.h>
 
 #include "parse.h"
 #include "pagemap.h"
+#include "pidlib.h"
 
 static struct maps_list *alloc_maplist(void)
 {
@@ -103,46 +101,6 @@ struct bintree_ops pageframe_ops = {
        .compare = compare_pageframe,
 };
 
-static int read_cmdline(int pid, int tid, char *cmdline, size_t len)
-{
-       FILE *file;
-       char path[512];
-       int ret;
-
-       snprintf(path, sizeof(path), "/proc/%d/task/%d/cmdline", pid, tid);
-       file = fopen(path, "rb");
-
-       if (!file)
-               return -1;
-
-       ret = fread(cmdline, 1, len, file);
-       if (ret > 0)
-               cmdline[ret - 1] = 0;
-       fclose(file);
-
-       return ret > 0 ? 0 : -1;
-}
-
-static char *get_name_by_pid(int pid)
-{
-       static int last_pid;
-       static char cmdline[128];
-       static char *bname;
-
-       if (last_pid == pid)
-               return bname;
-
-       if (read_cmdline(pid, pid, cmdline, sizeof(cmdline))) {
-               bname = NULL;
-               return NULL;
-       }
-
-       bname = basename(cmdline);
-
-       last_pid = pid;
-       return bname;
-}
-
 static int should_scan_process(struct parse_opts *opts, struct process *process)
 {
        struct pidlist *pid;
@@ -348,93 +306,6 @@ free:
        return 0;
 }
 
-static int parse_pid(DIR **dir)
-{
-       struct dirent *dirent;
-       int error;
-
-restart:
-       dirent = readdir(*dir);
-       if (!dirent) {
-               if (errno == 0) {
-                       closedir(*dir);
-                       *dir = NULL;
-                       return 0;
-               }
-               error = errno;
-               printf("Failed to read /proc directory: %s\n", strerror(error));
-               return -1;
-       }
-
-       if (dirent->d_name[0] < '0' || dirent->d_name[0] > '9')
-               goto restart;
-
-       return atoi(dirent->d_name);
-}
-
-static int opendir_check(DIR **dir, const char *path)
-{
-       int error;
-
-       if (!*dir) {
-               *dir = opendir(path);
-               if (!dir) {
-                       error = errno;
-                       fprintf(stderr, "Failed to open %s directory: %s\n",
-                               path, strerror(error));
-                       return -1;
-               }
-       }
-
-       return 0;
-}
-
-static int get_next_tid(int pid, DIR **dir)
-{
-       if (*dir == NULL) {
-               char path[64];
-
-               snprintf(path, sizeof(path), "/proc/%d/task/", pid);
-               if (opendir_check(dir, path))
-                       return -1;
-       }
-
-       return parse_pid(dir);
-}
-
-static int get_next_pid(DIR **dir)
-{
-       if (opendir_check(dir, "/proc"))
-               return -1;
-
-       return parse_pid(dir);
-}
-
-static int get_next_pid_by_name(DIR **dir, char *name)
-{
-       int pid;
-       char *pname;
-
-       if (opendir_check(dir, "/proc"))
-               return -1;
-
-       while (1) {
-               pid = parse_pid(dir);
-               if (pid <= 0)
-                       break;
-
-               pname = get_name_by_pid(pid);
-               if (pname == NULL)
-                       continue;
-               if (strcmp(pname, name))
-                       continue;
-
-               return pid;
-       }
-
-       return 0;
-}
-
 static int read_pageframe_with_threads(int pid,
                                struct pageframe *pageframe,
                                struct process *process_list,