]> git.itanic.dy.fi Git - scan-pagemap/commitdiff
parser: Factor out reading cmdline to separate function
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Fri, 23 Jul 2010 10:39:59 +0000 (13:39 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Fri, 23 Jul 2010 10:39:59 +0000 (13:39 +0300)
This will be needed from different places in future, so factor it out
in a separate function.

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

diff --git a/parse.c b/parse.c
index 36f632bc65bf7bf292e0490a9f2d93060f7723cd..117026b1c3a4e2fd4894965622fe9805dfc10433 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -124,6 +124,26 @@ 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 int check_parse_opts(struct parse_opts *opts, struct pageframe *pf,
                struct maps *map)
 {
@@ -241,7 +261,6 @@ void read_pageframe(int pid, int tid, struct pageframe *pageframe,
        struct process *process;
        FILE *file;
        char path[512];
-       int ret;
 
        process = malloc(sizeof(*process));
        memset(process, 0, sizeof(*process));
@@ -274,17 +293,9 @@ void read_pageframe(int pid, int tid, struct pageframe *pageframe,
        parse_pageframe(file, pageframe, maps, opts);
        fclose(file);
 
-       snprintf(path, sizeof(path), "/proc/%d/task/%d/cmdline", pid, tid);
-       file = fopen(path, "rb");
-
-       if (!file)
+       if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
                return;
 
-       ret = fread(process->name, 1, sizeof(process->name), file);
-       if (ret > 0)
-               process->name[ret - 1] = 0;
-       fclose(file);
-
        if (maps == NULL)
                return;