Add support similar to ndctl and libndctl for parsing config files. This
allows storing a config file path/list in the daxctl_ctx, and adds APIs
for setting and retrieving it.

Cc: QI Fuli <[email protected]>
Reviewed-by: QI Fuli <[email protected]>
Signed-off-by: Vishal Verma <[email protected]>
---
 .../daxctl/daxctl-reconfigure-device.txt      |  8 ++++
 configure.ac                                  |  3 ++
 daxctl/lib/libdaxctl.c                        | 39 +++++++++++++++++++
 daxctl/libdaxctl.h                            |  2 +
 Documentation/daxctl/Makefile.am              | 11 +++++-
 daxctl/Makefile.am                            |  1 +
 daxctl/lib/Makefile.am                        |  4 ++
 daxctl/lib/libdaxctl.sym                      |  2 +
 8 files changed, 69 insertions(+), 1 deletion(-)

diff --git a/Documentation/daxctl/daxctl-reconfigure-device.txt 
b/Documentation/daxctl/daxctl-reconfigure-device.txt
index aa87d45..09556cc 100644
--- a/Documentation/daxctl/daxctl-reconfigure-device.txt
+++ b/Documentation/daxctl/daxctl-reconfigure-device.txt
@@ -1,5 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
+include::attrs.adoc[]
+
 daxctl-reconfigure-device(1)
 ============================
 
@@ -250,6 +252,12 @@ ndctl create-namespace --mode=devdax | \
        jq -r "\"[reconfigure-device $(uuidgen)]\", \"nvdimm.uuid = \(.uuid)\", 
\"mode = system-ram\"" >> $config_path
 ----
 
+The default location for daxctl config files is under {daxctl_confdir}/,
+and any file with a '.conf' suffix at this location is considered. It is
+acceptable to have multiple files containing ini-style config sections,
+but the {section, subsection} tuple must be unique across all config files
+under {daxctl_confdir}/.
+
 include::../copyright.txt[]
 
 SEE ALSO
diff --git a/configure.ac b/configure.ac
index 9e1c6db..e779b51 100644
--- a/configure.ac
+++ b/configure.ac
@@ -178,6 +178,9 @@ AC_SUBST([ndctl_confdir])
 AC_SUBST([ndctl_conf])
 AC_SUBST([ndctl_monitorconf])
 
+daxctl_confdir=${sysconfdir}/daxctl
+AC_SUBST([daxctl_confdir])
+
 daxctl_modprobe_datadir=${datadir}/daxctl
 daxctl_modprobe_data=daxctl.conf
 AC_SUBST([daxctl_modprobe_datadir])
diff --git a/daxctl/lib/libdaxctl.c b/daxctl/lib/libdaxctl.c
index 860bd9c..2de3616 100644
--- a/daxctl/lib/libdaxctl.c
+++ b/daxctl/lib/libdaxctl.c
@@ -17,6 +17,8 @@
 #include <util/log.h>
 #include <util/sysfs.h>
 #include <util/iomem.h>
+#include <util/strbuf.h>
+#include <util/parse-configs.h>
 #include <daxctl/libdaxctl.h>
 #include "libdaxctl-private.h"
 
@@ -37,6 +39,7 @@ struct daxctl_ctx {
        struct log_ctx ctx;
        int refcount;
        void *userdata;
+       const char *configs;
        int regions_init;
        struct list_head regions;
        struct kmod_ctx *kmod_ctx;
@@ -68,6 +71,42 @@ DAXCTL_EXPORT void daxctl_set_userdata(struct daxctl_ctx 
*ctx, void *userdata)
        ctx->userdata = userdata;
 }
 
+DAXCTL_EXPORT void daxctl_set_configs_dir(struct daxctl_ctx **ctx, char 
*conf_dir)
+{
+       struct dirent **namelist;
+       struct strbuf value = STRBUF_INIT;
+       int rc;
+
+       if ((!ctx) || (!conf_dir))
+               return;
+
+       rc = scandir(conf_dir, &namelist, filter_conf_files, alphasort);
+       if (rc == -1) {
+               if (errno != ENOENT)
+                       err(*ctx, "scandir for configs failed: %s\n",
+                               strerror(errno));
+               return;
+       }
+
+       while (rc--) {
+               if (value.len)
+                       strbuf_addstr(&value, " ");
+               strbuf_addstr(&value, conf_dir);
+               strbuf_addstr(&value, "/");
+               strbuf_addstr(&value, namelist[rc]->d_name);
+               free(namelist[rc]);
+       }
+       (*ctx)->configs = strbuf_detach(&value, NULL);
+       free(namelist);
+}
+
+DAXCTL_EXPORT const char *daxctl_get_configs_dir(struct daxctl_ctx *ctx)
+{
+       if (ctx == NULL)
+               return NULL;
+       return ctx->configs;
+}
+
 /**
  * daxctl_new - instantiate a new library context
  * @ctx: context to establish
diff --git a/daxctl/libdaxctl.h b/daxctl/libdaxctl.h
index 683ae9c..6be09d9 100644
--- a/daxctl/libdaxctl.h
+++ b/daxctl/libdaxctl.h
@@ -28,6 +28,8 @@ int daxctl_get_log_priority(struct daxctl_ctx *ctx);
 void daxctl_set_log_priority(struct daxctl_ctx *ctx, int priority);
 void daxctl_set_userdata(struct daxctl_ctx *ctx, void *userdata);
 void *daxctl_get_userdata(struct daxctl_ctx *ctx);
+void daxctl_set_configs_dir(struct daxctl_ctx **ctx, char *conf_dir);
+const char *daxctl_get_configs_dir(struct daxctl_ctx *ctx);
 
 struct daxctl_region;
 struct daxctl_region *daxctl_new_region(struct daxctl_ctx *ctx, int id,
diff --git a/Documentation/daxctl/Makefile.am b/Documentation/daxctl/Makefile.am
index 5991731..9c43e61 100644
--- a/Documentation/daxctl/Makefile.am
+++ b/Documentation/daxctl/Makefile.am
@@ -33,11 +33,20 @@ EXTRA_DIST = $(man1_MANS)
 
 CLEANFILES = $(man1_MANS)
 
+.ONESHELL:
+attrs.adoc: $(srcdir)/Makefile.am
+       $(AM_V_GEN) cat <<- EOF >$@
+               :daxctl_confdir: $(daxctl_confdir)
+               :daxctl_conf: $(daxctl_conf)
+               :ndctl_keysdir: $(ndctl_keysdir)
+               EOF
+
 XML_DEPS = \
        ../../version.m4 \
        ../copyright.txt \
        Makefile \
-       $(CONFFILE)
+       $(CONFFILE) \
+       attrs.adoc
 
 RM ?= rm -f
 
diff --git a/daxctl/Makefile.am b/daxctl/Makefile.am
index 9b1313a..a5c6de1 100644
--- a/daxctl/Makefile.am
+++ b/daxctl/Makefile.am
@@ -10,6 +10,7 @@ config.h: $(srcdir)/Makefile.am
                "$(daxctl_modprobe_datadir)/$(daxctl_modprobe_data)"' >>$@ && \
        echo '#define DAXCTL_MODPROBE_INSTALL \
                "$(sysconfdir)/modprobe.d/$(daxctl_modprobe_data)"' >>$@
+       $(AM_V_GEN) echo '#define DAXCTL_CONF_DIR  "$(daxctl_confdir)"' >>$@
 
 daxctl_SOURCES =\
                daxctl.c \
diff --git a/daxctl/lib/Makefile.am b/daxctl/lib/Makefile.am
index db2351e..7a53598 100644
--- a/daxctl/lib/Makefile.am
+++ b/daxctl/lib/Makefile.am
@@ -13,6 +13,10 @@ libdaxctl_la_SOURCES =\
        ../../util/iomem.h \
        ../../util/sysfs.c \
        ../../util/sysfs.h \
+       ../../util/strbuf.h \
+       ../../util/strbuf.c \
+       ../../util/wrapper.c \
+       ../../util/usage.c \
        ../../util/log.c \
        ../../util/log.h \
        ../../util/parse-configs.h \
diff --git a/daxctl/lib/libdaxctl.sym b/daxctl/lib/libdaxctl.sym
index a13e93d..7098aba 100644
--- a/daxctl/lib/libdaxctl.sym
+++ b/daxctl/lib/libdaxctl.sym
@@ -96,4 +96,6 @@ LIBDAXCTL_9 {
 global:
        daxctl_dev_will_auto_online_memory;
        daxctl_dev_has_online_memory;
+       daxctl_set_configs_dir;
+       daxctl_get_configs_dir;
 } LIBDAXCTL_8;
-- 
2.33.1


Reply via email to