diff options
author | Alan D. Brunelle <alan.brunelle@hp.com> | 2009-02-11 13:40:09 -0500 |
---|---|---|
committer | Alan D. Brunelle <alan.brunelle@hp.com> | 2009-02-11 13:40:09 -0500 |
commit | e6855475478967e7cf92f8beece05ea55d31e6f1 (patch) | |
tree | 0a11a61c9bc827ddd452b135168833f51636bb70 | |
parent | 6488ca487c5695b784db56c79b67007e92eeb2ac (diff) | |
download | blktrace-e6855475478967e7cf92f8beece05ea55d31e6f1.tar.gz |
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 <alan.brunelle@hp.com>
-rw-r--r-- | btt/globals.h | 1 | ||||
-rw-r--r-- | btt/misc.c | 64 | ||||
-rw-r--r-- | btt/mmap.c | 3 |
3 files changed, 42 insertions, 26 deletions
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 */ @@ -26,6 +26,7 @@ #include <sys/stat.h> #include <sys/time.h> #include <sys/resource.h> +#include <fcntl.h> #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) {} @@ -28,6 +28,7 @@ #include <string.h> #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); |