Skip to content
Snippets Groups Projects
Unverified Commit 3e292ba0 authored by Matthias Schiffer's avatar Matthias Schiffer
Browse files

gluon-web: close FDs after mmap()

parent 99b4d2ea
No related branches found
No related tags found
No related merge requests found
...@@ -128,14 +128,20 @@ static lmo_archive_t * lmo_open(const char *file) ...@@ -128,14 +128,20 @@ static lmo_archive_t * lmo_open(const char *file)
lmo_archive_t *ar = NULL; lmo_archive_t *ar = NULL;
struct stat s; struct stat s;
if ((fd = open(file, O_RDONLY|O_CLOEXEC)) == -1) fd = open(file, O_RDONLY|O_CLOEXEC);
if (fd < 0)
goto err; goto err;
if (fstat(fd, &s) == -1) if (fstat(fd, &s))
goto err; goto err;
if ((ar = calloc(1, sizeof(*ar))) != NULL) { if ((ar = calloc(1, sizeof(*ar))) != NULL) {
if ((ar->data = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) ar->data = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
fd = -1;
if (ar->data == MAP_FAILED)
goto err; goto err;
ar->end = ar->data + s.st_size; ar->end = ar->data + s.st_size;
......
...@@ -56,7 +56,6 @@ struct template_chunk { ...@@ -56,7 +56,6 @@ struct template_chunk {
/* parser state */ /* parser state */
struct template_parser { struct template_parser {
int fd;
size_t size; size_t size;
char *data; char *data;
char *off; char *off;
...@@ -96,24 +95,28 @@ static struct template_parser * template_init(struct template_parser *parser) ...@@ -96,24 +95,28 @@ static struct template_parser * template_init(struct template_parser *parser)
struct template_parser * template_open(const char *file) struct template_parser * template_open(const char *file)
{ {
int fd = -1;
struct stat s; struct stat s;
struct template_parser *parser; struct template_parser *parser;
if (!(parser = calloc(1, sizeof(*parser)))) if (!(parser = calloc(1, sizeof(*parser))))
goto err; goto err;
parser->fd = -1;
parser->file = file; parser->file = file;
if ((parser->fd = open(file, O_RDONLY|O_CLOEXEC)) < 0) fd = open(file, O_RDONLY|O_CLOEXEC);
if (fd < 0)
goto err; goto err;
if (fstat(parser->fd, &s)) if (fstat(fd, &s))
goto err; goto err;
parser->size = s.st_size; parser->size = s.st_size;
parser->data = mmap(NULL, parser->size, PROT_READ, MAP_PRIVATE, parser->data = mmap(NULL, parser->size, PROT_READ, MAP_PRIVATE,
parser->fd, 0); fd, 0);
close(fd);
fd = -1;
if (parser->data == MAP_FAILED) if (parser->data == MAP_FAILED)
goto err; goto err;
...@@ -121,6 +124,8 @@ struct template_parser * template_open(const char *file) ...@@ -121,6 +124,8 @@ struct template_parser * template_open(const char *file)
return template_init(parser); return template_init(parser);
err: err:
if (fd >= 0)
close(fd);
template_close(parser); template_close(parser);
return NULL; return NULL;
} }
...@@ -132,8 +137,6 @@ struct template_parser * template_string(const char *str, size_t len) ...@@ -132,8 +137,6 @@ struct template_parser * template_string(const char *str, size_t len)
if (!(parser = calloc(1, sizeof(*parser)))) if (!(parser = calloc(1, sizeof(*parser))))
goto err; goto err;
parser->fd = -1;
parser->size = len; parser->size = len;
parser->data = (char *)str; parser->data = (char *)str;
...@@ -155,9 +158,6 @@ void template_close(struct template_parser *parser) ...@@ -155,9 +158,6 @@ void template_close(struct template_parser *parser)
if (parser->file) { if (parser->file) {
if ((parser->data != NULL) && (parser->data != MAP_FAILED)) if ((parser->data != NULL) && (parser->data != MAP_FAILED))
munmap(parser->data, parser->size); munmap(parser->data, parser->size);
if (parser->fd >= 0)
close(parser->fd);
} }
free(parser); free(parser);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment