summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan D. Brunelle <alan.brunelle@hp.com>2009-02-11 13:40:09 -0500
committerAlan D. Brunelle <alan.brunelle@hp.com>2009-02-11 13:40:09 -0500
commite6855475478967e7cf92f8beece05ea55d31e6f1 (patch)
tree0a11a61c9bc827ddd452b135168833f51636bb70
parent6488ca487c5695b784db56c79b67007e92eeb2ac (diff)
downloadblktrace-e6855475478967e7cf92f8beece05ea55d31e6f1.tar.gz
blktrace-e6855475478967e7cf92f8beece05ea55d31e6f1.tar.bz2
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.h1
-rw-r--r--btt/misc.c64
-rw-r--r--btt/mmap.c3
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 */
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 <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) {}
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 <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);