Commit 31bbf66e authored by Wu Fengguang's avatar Wu Fengguang Committed by Linus Torvalds

page-types: introduce checked_open()

This helps merge duplicate code (now and future) and outstand the main
logic.
Signed-off-by: default avatarWu Fengguang <fengguang.wu@intel.com>
Cc: Andi Kleen <andi@firstfloor.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 0c57effe
...@@ -214,6 +214,18 @@ static void fatal(const char *x, ...) ...@@ -214,6 +214,18 @@ static void fatal(const char *x, ...)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
int checked_open(const char *pathname, int flags)
{
int fd = open(pathname, flags);
if (fd < 0) {
perror(pathname);
exit(EXIT_FAILURE);
}
return fd;
}
/* /*
* page flag names * page flag names
...@@ -534,11 +546,7 @@ static void walk_addr_ranges(void) ...@@ -534,11 +546,7 @@ static void walk_addr_ranges(void)
{ {
int i; int i;
kpageflags_fd = open(PROC_KPAGEFLAGS, O_RDONLY); kpageflags_fd = checked_open(PROC_KPAGEFLAGS, O_RDONLY);
if (kpageflags_fd < 0) {
perror(PROC_KPAGEFLAGS);
exit(EXIT_FAILURE);
}
if (!nr_addr_ranges) if (!nr_addr_ranges)
add_addr_range(0, ULONG_MAX); add_addr_range(0, ULONG_MAX);
...@@ -631,11 +639,7 @@ static void parse_pid(const char *str) ...@@ -631,11 +639,7 @@ static void parse_pid(const char *str)
opt_pid = parse_number(str); opt_pid = parse_number(str);
sprintf(buf, "/proc/%d/pagemap", opt_pid); sprintf(buf, "/proc/%d/pagemap", opt_pid);
pagemap_fd = open(buf, O_RDONLY); pagemap_fd = checked_open(buf, O_RDONLY);
if (pagemap_fd < 0) {
perror(buf);
exit(EXIT_FAILURE);
}
sprintf(buf, "/proc/%d/maps", opt_pid); sprintf(buf, "/proc/%d/maps", opt_pid);
file = fopen(buf, "r"); file = fopen(buf, "r");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment