From: Timo Kokkonen Date: Sun, 15 Aug 2010 16:39:10 +0000 (+0300) Subject: read_pageframe: Do not add empty entries in process list X-Git-Url: http://git.itanic.dy.fi/?p=scan-pagemap;a=commitdiff_plain;h=2f693b11674a3d4f3a84beb56e6073a101f56233 read_pageframe: Do not add empty entries in process list If reading of some of the process details fails, the process is most likely died while we were processing its details. Thus, there is no good to add the entry to the process list either. The entry is now freed instead of adding the half-read entry to the list. Signed-off-by: Timo Kokkonen --- diff --git a/parse.c b/parse.c index 850b2a1..3934fff 100644 --- a/parse.c +++ b/parse.c @@ -297,13 +297,12 @@ static int read_pageframe(int pid, int tid, struct pageframe *pageframe, process->pid = pid; process->tid = tid; - list_add_tail(&process->list, &(*process_list)->list); snprintf(path, sizeof(path), "/proc/%d/task/%d/maps", pid, tid); file = fopen(path, "rb"); if (!file) - return 0; + goto free; maps = parse_maps(file, pid, tid); fclose(file); @@ -313,23 +312,28 @@ static int read_pageframe(int pid, int tid, struct pageframe *pageframe, file = fopen(path, "rb"); if (!file) - return 0; + goto free; parse_pageframe(file, pageframe, maps, opts); fclose(file); if (read_cmdline(pid, tid, process->name, sizeof(process->name))) - return 1; - - if (maps == NULL) - return 1; + goto free; - list_for_each_entry(maps, &process->maps->list, list) { - process->pages_present += maps->pages_present; - process->pages_swapped += maps->pages_swapped; + if (maps != NULL) { + list_for_each_entry(maps, &process->maps->list, list) { + process->pages_present += maps->pages_present; + process->pages_swapped += maps->pages_swapped; + } } + list_add_tail(&process->list, &(*process_list)->list); + return 1; +free: + free(process); + + return 0; } static int parse_pid(DIR **dir)