From: Martin Mares Date: Mon, 24 Apr 2006 21:49:55 +0000 (+0200) Subject: bopen_safe() in conf2 was leaking file descriptors. I've replaced it X-Git-Tag: holmes-import~645^2~11^2~49 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=4f3517ad5229aa39d1bbe403577328813800f8d2;p=libucw.git bopen_safe() in conf2 was leaking file descriptors. I've replaced it by a new bopen_try() in the fastbuf library and also cleaned up error messages on unsuccessful file opens. --- diff --git a/lib/conf2.c b/lib/conf2.c index 57b487b9..f6be8c6b 100644 --- a/lib/conf2.c +++ b/lib/conf2.c @@ -1207,17 +1207,6 @@ split_command(void) /* Parsing multiple files */ -static struct fastbuf * -bopen_safe(byte *name) -{ - int fd = sh_open(name, O_RDONLY); - if (fd < 0) { - log(L_ERROR, "Cannot open %s", name); - return NULL; - } - return bopen(name, O_RDONLY, 1<<14); -} - static byte * parse_fastbuf(byte *name_fb, struct fastbuf *fb, uns depth) { @@ -1248,9 +1237,9 @@ parse_fastbuf(byte *name_fb, struct fastbuf *fb, uns depth) msg = "The input command must be the last one on a line"; if (msg) goto error; - struct fastbuf *new_fb = bopen_safe(pars[0]); + struct fastbuf *new_fb = bopen_try(pars[0], O_RDONLY, 1<<14); if (!new_fb) { - msg = "Cannot open file"; + msg = cf_printf("Cannot open file %s: %m", pars[0]); goto error; } uns ll = line_num; @@ -1305,9 +1294,11 @@ static int load_file(byte *file) { init_stack(); - struct fastbuf *fb = bopen_safe(file); - if (!fb) + struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14); + if (!fb) { + log(L_ERROR, "Cannot open %s: %m", file); return 1; + } byte *msg = parse_fastbuf(file, fb, 0); bclose(fb); int err = !!msg || done_stack(); @@ -1352,7 +1343,7 @@ cf_get_opt(int argc, char * const argv[], const char *short_opts, const struct o die("Cannot set %s", optarg); } else { if (cf_load(optarg)) - die("Cannot load %s", optarg); + die("Cannot load config file %s", optarg); } } else { /* unhandled option or end of options */ diff --git a/lib/fastbuf.h b/lib/fastbuf.h index 219fec19..af6787ae 100644 --- a/lib/fastbuf.h +++ b/lib/fastbuf.h @@ -81,6 +81,7 @@ struct fastbuf { /* FastIO on standard files (specify buffer size 0 to enable mmaping) */ struct fastbuf *bopen(byte *name, uns mode, uns buflen); +struct fastbuf *bopen_try(byte *name, uns mode, uns buflen); struct fastbuf *bopen_tmp(uns buflen); struct fastbuf *bfdopen(int fd, uns buflen); struct fastbuf *bfdopen_shared(int fd, uns buflen); diff --git a/lib/fb-file.c b/lib/fb-file.c index 9caf9a18..9ef992a8 100644 --- a/lib/fb-file.c +++ b/lib/fb-file.c @@ -118,20 +118,26 @@ bfdopen_internal(int fd, uns buflen, byte *name) } struct fastbuf * -bopen(byte *name, uns mode, uns buflen) +bopen_try(byte *name, uns mode, uns buflen) { - struct fastbuf *b; - int fd; + int fd = sh_open(name, mode, 0666); + if (fd < 0) + return NULL; + struct fastbuf *b = bfdopen_internal(fd, buflen, name); + if (mode & O_APPEND) + bfd_seek(b, 0, SEEK_END); + return b; +} +struct fastbuf * +bopen(byte *name, uns mode, uns buflen) +{ if (!buflen) return bopen_mm(name, mode); - fd = sh_open(name, mode, 0666); - if (fd < 0) + struct fastbuf *b = bopen_try(name, mode, buflen); + if (!b) die("Unable to %s file %s: %m", (mode & O_CREAT) ? "create" : "open", name); - b = bfdopen_internal(fd, buflen, name); - if (mode & O_APPEND) - bfd_seek(b, 0, SEEK_END); return b; }