[PATCH 1/1] conffile: process config.d directory config files.

Steve Dickson
2020-10-29T21:07:06Z

When a /etc/nfs.conf.d or /etc/nfsmount.conf.d directory
exists and there are config file(s) in those directory
exist, those config file(s) are used instead of the given
/etc/nfs.conf and /etc/nfsmount.conf files.

Signed-off-by: Steve Dickson <steved@redhat.com>
---
support/nfs/conffile.c | 78 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 77 insertions(+), 1 deletion(-)

diff --git a/support/nfs/conffile.c b/support/nfs/conffile.c
index 3d13610..c60e511 100644
--- a/support/nfs/conffile.c
+++ b/support/nfs/conffile.c
@@ -52,6 +52,7 @@
#include <libgen.h>
#include <sys/file.h>
#include <time.h>
+#include <dirent.h>

#include "conffile.h"
#include "xlog.h"
@@ -609,6 +610,69 @@ conf_load_file(const char *conf_file)
return 0;
}

+static int
+conf_init_dir(const char *conf_file)
+{
+ struct dirent **namelist = NULL;
+ char *dname, fname[PATH_MAX + 1];
+ int n = 0, nfiles = 0, i, fname_len, dname_len;
+
+ dname = malloc(strlen(conf_file) + 3);
+ if (dname == NULL) {
+ xlog(L_WARNING, "conf_init_dir: malloc: %s", strerror(errno));
+ return nfiles;
+ }
+ sprintf(dname, "%s.d", conf_file);
+
+ n = scandir(dname, &namelist, NULL, versionsort);
+ if (n < 0) {
+ if (errno != ENOENT) {
+ xlog(L_WARNING, "conf_init_dir: scandir %s: %s",
+ dname, strerror(errno));
+ }
+ free(dname);
+ return nfiles;
+ } else if (n == 0) {
+ free(dname);
+ return nfiles;
+ }
+
+ dname_len = strlen(dname);
+ for (i = 0; i < n; i++ ) {
+ struct dirent *d = namelist[i];
+
+ switch (d->d_type) {
+ case DT_UNKNOWN:
+ case DT_REG:
+ case DT_LNK:
+ break;
+ default:
+ continue;
+ }
+ if (*d->d_name == '.')
+ continue;
+
+ fname_len = strlen(d->d_name);
+ if (!fname_len || (fname_len + dname_len) > PATH_MAX) {
+ xlog(L_WARNING, "conf_init_dir: Too long file name: %s in %s",
+ d->d_name, dname);
+ continue;
+ }
+ sprintf(fname, "%s/%s", dname, d->d_name);
+
+ if (conf_load_file(fname))
+ continue;
+ nfiles++;
+ }
+
+ for (i = 0; i < n; i++)
+ free(namelist[i]);
+ free(namelist);
+ free(dname);
+
+ return nfiles;
+}
+
int
conf_init_file(const char *conf_file)
{
@@ -619,7 +683,19 @@ conf_init_file(const char *conf_file)

TAILQ_INIT (&conf_trans_queue);

- if (conf_file == NULL) conf_file=NFS_CONFFILE;
+ if (conf_file == NULL)
+ conf_file=NFS_CONFFILE;
+
+ /*
+ * Check to see if there is a config directory
+ * if so use those file(s) if they exist
+ */
+ if (conf_init_dir(conf_file) != 0)
+ return 0;
+
+ /*
+ * Otherwise using the giving config file
+ */
return conf_load_file(conf_file);
}

--
2.26.2