shared: fail reading a directory as a config file

open() will happily open directories and other non-normal files.
Attempting to parse them as config files makes no sense, so check that
the opened file is indeed a regular file.

Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
Reviewed-by: Jonny Lamb <jonny.lamb@collabora.co.uk>
Reviewed-by: Nobuhiko Tanibata <NOBUHIKO_TANIBATA@xddp.denso.co.jp>
Reviewed-by: Bryce Harrington <bryce@osg.samsung.com>
diff --git a/shared/config-parser.c b/shared/config-parser.c
index 4542ca6..ff6814c 100644
--- a/shared/config-parser.c
+++ b/shared/config-parser.c
@@ -326,6 +326,7 @@
 {
 	FILE *fp;
 	char line[512], *p;
+	struct stat filestat;
 	struct weston_config *config;
 	struct weston_config_section *section = NULL;
 	int i, fd;
@@ -342,6 +343,13 @@
 		return NULL;
 	}
 
+	if (fstat(fd, &filestat) < 0 ||
+	    !S_ISREG(filestat.st_mode)) {
+		close(fd);
+		free(config);
+		return NULL;
+	}
+
 	fp = fdopen(fd, "r");
 	if (fp == NULL) {
 		free(config);