From f80f470f55d24497985e7932cbb639f6dd907ab9 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Fri, 23 Jul 2010 13:39:59 +0300 Subject: [PATCH] parser: Factor out reading cmdline to separate function This will be needed from different places in future, so factor it out in a separate function. Signed-off-by: Timo Kokkonen --- parse.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/parse.c b/parse.c index 36f632b..117026b 100644 --- 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; -- 2.44.0