btt: fixed open in setup_ifile
authorAlan D. Brunelle <alan.brunelle@hp.com>
Wed, 11 Feb 2009 18:40:09 +0000 (13:40 -0500)
committerAlan D. Brunelle <alan.brunelle@hp.com>
Wed, 11 Feb 2009 18:40:09 +0000 (13:40 -0500)
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>
btt/globals.h
btt/misc.c
btt/mmap.c

index cc83095a368c597716cb0bcdbe7ffba2b5a90806..9b73a98e4c96bf13450a56d95c48f2bfd04756a6 100644 (file)
@@ -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 */
index c79da2c3991997c8a76143100be3ad6cb37641ee..4ec2c18aa5255a25845a7c5bdef6427d1b342203 100644 (file)
@@ -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) {}
index 050d4f84765796ef1fd543d632c17a88307b4694..83cc708af047b63e95827581ee309f9532188111 100644 (file)
@@ -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);