diff mbox series

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

Message ID 20201029210401.446244-2-steved@redhat.com (mailing list archive)
State New, archived
Headers show
Series Enable config.d directory to be processed. | expand

Commit Message

Steve Dickson Oct. 29, 2020, 9:04 p.m. UTC
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 mbox series

Patch

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);
 }