summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustin Mitchell <jumitche@redhat.com>2017-06-21 12:01:01 -0400
committerSteve Dickson <steved@redhat.com>2017-06-21 12:04:35 -0400
commit9569237ba50c5857e04bc36c9b3250c570bfbef2 (patch)
treeabed4a440ddf2c5b8b0d5891590cb134cfb3565d
parent34c73d82ed02209bd8933da2f1f4761bb464d1d7 (diff)
downloadnfs-utils-9569237ba50c5857e04bc36c9b3250c570bfbef2.tar.gz
nfs-utils-9569237ba50c5857e04bc36c9b3250c570bfbef2.tar.xz
nfs-utils-9569237ba50c5857e04bc36c9b3250c570bfbef2.zip
Reimplement include functionality in nfs.conf
Re-implement include file functionality as documented. Existing implementation had various issues, one of which was it allowed a subordinate file to inadvertently change which section the subsequent tags back in the master config file got assigned to. Acked-by: NeilBrown <neilb@suse.com> Signed-off-by: Justin Mitchell <jumitche@redhat.com> Signed-off-by: Steve Dickson <steved@redhat.com>
-rw-r--r--support/nfs/conffile.c78
1 files changed, 56 insertions, 22 deletions
diff --git a/support/nfs/conffile.c b/support/nfs/conffile.c
index b209c17..8690218 100644
--- a/support/nfs/conffile.c
+++ b/support/nfs/conffile.c
@@ -56,9 +56,11 @@
#pragma GCC visibility push(hidden)
static void conf_load_defaults(void);
-static int conf_load(int trans, const char *path);
+static char * conf_load(const char *path);
static int conf_set(int , char *, char *, char *,
char *, int , int );
+static void conf_parse(int trans, char *buf,
+ char **section, char **subsection);
struct conf_trans {
TAILQ_ENTRY (conf_trans) link;
@@ -204,7 +206,6 @@ conf_set_now(char *section, char *arg, char *tag,
node->tag = strdup(tag);
node->value = strdup(value);
node->is_default = is_default;
-
LIST_INSERT_HEAD(&conf_bindings[conf_hash (section)], node, link);
return 0;
}
@@ -367,22 +368,47 @@ conf_parse_line(int trans, char *line, int lineno, char **section, char **subsec
return;
}
- /* XXX Perhaps should we not ignore errors? */
- conf_set(trans, *section, *subsection, line, val, 0, 0);
+ if (strcasecmp(line, "include")==0) {
+ /* load and parse subordinate config files */
+ char * subconf = conf_load(val);
+ if (subconf == NULL) {
+ xlog_warn("config file error: line %d: "
+ "error loading included config", lineno);
+ return;
+ }
+
+ /* copy the section data so the included file can inherit it
+ * without accidentally changing it for us */
+ char * inc_section = NULL;
+ char * inc_subsection = NULL;
+ if (*section != NULL) {
+ inc_section = strdup(*section);
+ if (*subsection != NULL)
+ inc_subsection = strdup(*subsection);
+ }
+
+ conf_parse(trans, subconf, &inc_section, &inc_subsection);
+
+ if (inc_section) free(inc_section);
+ if (inc_subsection) free(inc_subsection);
+ free(subconf);
+ } else {
+ /* XXX Perhaps should we not ignore errors? */
+ conf_set(trans, *section, *subsection, line, val, 0, 0);
+ }
}
/* Parse the mapped configuration file. */
static void
-conf_parse(int trans, char *buf, size_t sz)
+conf_parse(int trans, char *buf, char **section, char **subsection)
{
char *cp = buf;
- char *bufend = buf + sz;
+ char *bufend = NULL;
char *line;
- char *section = NULL;
- char *subsection = NULL;
int lineno = 0;
line = cp;
+ bufend = buf + strlen(buf);
while (cp < bufend) {
if (*cp == '\n') {
/* Check for escaped newlines. */
@@ -391,7 +417,7 @@ conf_parse(int trans, char *buf, size_t sz)
else {
*cp = '\0';
lineno++;
- conf_parse_line(trans, line, lineno, &section, &subsection);
+ conf_parse_line(trans, line, lineno, section, subsection);
line = cp + 1;
}
}
@@ -399,8 +425,6 @@ conf_parse(int trans, char *buf, size_t sz)
}
if (cp != line)
xlog_warn("conf_parse: last line non-terminated, ignored.");
- if (section) free(section);
- if (subsection) free(subsection);
}
static void
@@ -410,21 +434,21 @@ conf_load_defaults(void)
return;
}
-static int
-conf_load(int trans, const char *path)
+static char *
+conf_load(const char *path)
{
struct stat sb;
if ((stat (path, &sb) == 0) || (errno != ENOENT)) {
- char *new_conf_addr;
+ char *new_conf_addr = NULL;
size_t sz = sb.st_size;
int fd = open (path, O_RDONLY, 0);
if (fd == -1) {
xlog_warn("conf_reinit: open (\"%s\", O_RDONLY) failed", path);
- return -1;
+ return NULL;
}
- new_conf_addr = malloc(sz);
+ new_conf_addr = malloc(sz+1);
if (!new_conf_addr) {
xlog_warn("conf_reinit: malloc (%lu) failed", (unsigned long)sz);
goto fail;
@@ -439,14 +463,13 @@ conf_load(int trans, const char *path)
close(fd);
/* XXX Should we not care about errors and rollback? */
- conf_parse(trans, new_conf_addr, sz);
- free(new_conf_addr);
- return 0;
+ new_conf_addr[sz] = '\0';
+ return new_conf_addr;
fail:
close(fd);
- free(new_conf_addr);
+ if (new_conf_addr) free(new_conf_addr);
}
- return -1;
+ return NULL;
}
/* remove and free up any existing config state */
@@ -475,14 +498,25 @@ static void
conf_reinit(const char *conf_file)
{
int trans;
+ char * conf_data;
trans = conf_begin();
- if (conf_load(trans, conf_file) < 0)
+ conf_data = conf_load(conf_file);
+
+ if (conf_data == NULL)
return;
/* Load default configuration values. */
conf_load_defaults();
+ /* Parse config contents into the transaction queue */
+ char *section = NULL;
+ char *subsection = NULL;
+ conf_parse(trans, conf_data, &section, &subsection);
+ if (section) free(section);
+ if (subsection) free(subsection);
+ free(conf_data);
+
/* Free potential existing configuration. */
conf_free_bindings();