Browse Source

gluon-web: clean up opening files

Open with O_CLOEXEC, use fstat() instead of stat().
Matthias Schiffer 6 years ago
parent
commit
7e5f0fe1d5
2 changed files with 4 additions and 6 deletions
  1. 2 4
      package/gluon-web/src/template_lmo.c
  2. 2 2
      package/gluon-web/src/template_parser.c

+ 2 - 4
package/gluon-web/src/template_lmo.c

@@ -74,10 +74,10 @@ static lmo_archive_t * lmo_open(const char *file)
 
 	lmo_archive_t *ar = NULL;
 
-	if (stat(file, &s) == -1)
+	if ((in = open(file, O_RDONLY|O_CLOEXEC)) == -1)
 		goto err;
 
-	if ((in = open(file, O_RDONLY)) == -1)
+	if (fstat(in, &s) == -1)
 		goto err;
 
 	if ((ar = calloc(1, sizeof(*ar))) != NULL) {
@@ -85,8 +85,6 @@ static lmo_archive_t * lmo_open(const char *file)
 		ar->fd     = in;
 		ar->size = s.st_size;
 
-		fcntl(ar->fd, F_SETFD, fcntl(ar->fd, F_GETFD) | FD_CLOEXEC);
-
 		if ((ar->mmap = mmap(NULL, ar->size, PROT_READ, MAP_SHARED, ar->fd, 0)) == MAP_FAILED)
 			goto err;
 

+ 2 - 2
package/gluon-web/src/template_parser.c

@@ -45,10 +45,10 @@ struct template_parser * template_open(const char *file)
 	parser->fd = -1;
 	parser->file = file;
 
-	if (stat(file, &s))
+	if ((parser->fd = open(file, O_RDONLY|O_CLOEXEC)) < 0)
 		goto err;
 
-	if ((parser->fd = open(file, O_RDONLY)) < 0)
+	if (fstat(parser->fd, &s))
 		goto err;
 
 	parser->size = s.st_size;