From: Alan D. Brunelle Date: Wed, 11 Feb 2009 18:40:09 +0000 (-0500) Subject: btt: fixed open in setup_ifile X-Git-Tag: blktrace-1.0.1~27 X-Git-Url: https://git.kernel.dk/?a=commitdiff_plain;h=e6855475478967e7cf92f8beece05ea55d31e6f1;p=blktrace.git btt: fixed open in setup_ifile Took my_open & my_fopen code from blktrace 2.0: needed to add in open resource limit increasing stuff. Signed-off-by: Alan D. Brunelle --- diff --git a/btt/globals.h b/btt/globals.h index cc83095..9b73a98 100644 --- a/btt/globals.h +++ b/btt/globals.h @@ -263,6 +263,7 @@ void add_buf(void *buf); void clean_bufs(void); char *make_dev_hdr(char *pad, size_t len, struct d_info *dip, int add_parens); FILE *my_fopen(const char *path, const char *mode); +int my_open(const char *path, int flags); void dbg_ping(void); /* mmap.c */ diff --git a/btt/misc.c b/btt/misc.c index c79da2c..4ec2c18 100644 --- a/btt/misc.c +++ b/btt/misc.c @@ -26,6 +26,7 @@ #include #include #include +#include #define INLINE_DECLARE #include "globals.h" @@ -126,38 +127,51 @@ char *make_dev_hdr(char *pad, size_t len, struct d_info *dip, int add_parens) * * Root users will probably be OK with this, others... */ +static int increase_limit(int resource, rlim_t increase) +{ + struct rlimit rlim; + int save_errno = errno; + + if (!getrlimit(resource, &rlim)) { + rlim.rlim_cur += increase; + if (rlim.rlim_cur >= rlim.rlim_max) + rlim.rlim_max = rlim.rlim_cur + increase; + + if (!setrlimit(resource, &rlim)) + return 1; + } + + errno = save_errno; + return 0; +} + +static int handle_open_failure(void) +{ + if (errno == ENFILE || errno == EMFILE) + return increase_limit(RLIMIT_NOFILE, 16); + return 0; +} + FILE *my_fopen(const char *path, const char *mode) { FILE *fp; - fp = fopen(path, mode); - while (fp == NULL) { - if (errno == ENFILE || errno == EMFILE) { - struct rlimit rlim; - - if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { - perror("get: RLIMIT_NOFILE"); - return NULL; - } - - rlim.rlim_cur++; - if (rlim.rlim_cur >= rlim.rlim_max) - rlim.rlim_max++; - - if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) { - perror("set: RLIMIT_NOFILE"); - return NULL; - } - } - else { - perror(path); - return NULL; - } - + do { fp = fopen(path, mode); - } + } while (fp == NULL && handle_open_failure()); return fp; } +int my_open(const char *path, int flags) +{ + int fd; + + do { + fd = open(path, flags); + } while (fd < 0 && handle_open_failure()); + + return fd; +} + void dbg_ping(void) {} diff --git a/btt/mmap.c b/btt/mmap.c index 050d4f8..83cc708 100644 --- a/btt/mmap.c +++ b/btt/mmap.c @@ -28,6 +28,7 @@ #include #include "blktrace.h" +#include "globals.h" #define DEF_LEN (16 * 1024 * 1024) @@ -102,7 +103,7 @@ void setup_ifile(char *fname) pgsz = sysconf(_SC_PAGESIZE); - fd = open(fname, O_RDONLY); + fd = my_open(fname, O_RDONLY); if (fd < 0) { perror(fname); exit(1);