Skip to contentSkip to conversation

make ndctl support global configuration files

PATCH Superseded QI Fuli 4 participants 9 messages Aug 22, 2021 – Aug 24, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

QI Fuli

author
#0 lore raw

cover make ndctl support global configuration files

From: QI Fuli <[email protected]>

This patch set is used to make ndctl support global configuration files. All files with .conf suffix under {sysconfdir}/ndctl can be regarded as ndctl global configration files that all ndctl commands can refer to.

QI Fuli (5):
ndctl, ccan: import ciniparser
ndctl, util: add parse-configs helper
ndctl: make ndctl support configuration files
ndctl, config: add the default ndctl configuration file ndctl, monitor: refactor monitor for supporting multiple config files

 Documentation/ndctl/Makefile.am       |   2 +-
 Documentation/ndctl/ndctl-monitor.txt |   8 +-
 Makefile.am                           |   8 +-
 ccan/ciniparser/ciniparser.c          | 480 ++++++++++++++++++++++++++
 ccan/ciniparser/ciniparser.h          | 262 ++++++++++++++
 ccan/ciniparser/dictionary.c          | 266 ++++++++++++++
 ccan/ciniparser/dictionary.h          | 166 +++++++++
 configure.ac                          |   6 +-
 ndctl/Makefile.am                     |  10 +-
 ndctl/lib/Makefile.am                 |   4 +
 ndctl/lib/libndctl.c                  |  52 +++
 ndctl/lib/libndctl.sym                |   2 +
 ndctl/lib/private.h                   |   1 +
 ndctl/libndctl.h                      |   2 +
 ndctl/monitor.c                       |  69 ++--
 ndctl/ndctl.c                         |   1 +
 ndctl/ndctl.conf                      |  56 +++
 util/parse-configs.c                  |  82 +++++
 util/parse-configs.h                  |  34 ++
 19 files changed, 1467 insertions(+), 44 deletions(-)
 create mode 100644 ccan/ciniparser/ciniparser.c
 create mode 100644 ccan/ciniparser/ciniparser.h
 create mode 100644 ccan/ciniparser/dictionary.c
 create mode 100644 ccan/ciniparser/dictionary.h
 create mode 100644 ndctl/ndctl.conf
 create mode 100644 util/parse-configs.c
 create mode 100644 util/parse-configs.h
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH 1/5 ndctl, ccan: import ciniparser

Import ciniparser from ccan[1].
[1] https://ccodearchive.net/info/ciniparser.html

---
 Makefile.am                  |   6 +-
 ccan/ciniparser/ciniparser.c | 480 +++++++++++++++++++++++++++++++++++
 ccan/ciniparser/ciniparser.h | 262 +++++++++++++++++++
 ccan/ciniparser/dictionary.c | 266 +++++++++++++++++++
 ccan/ciniparser/dictionary.h | 166 ++++++++++++
 5 files changed, 1179 insertions(+), 1 deletion(-)
 create mode 100644 ccan/ciniparser/ciniparser.c
 create mode 100644 ccan/ciniparser/ciniparser.h
 create mode 100644 ccan/ciniparser/dictionary.c
 create mode 100644 ccan/ciniparser/dictionary.h
Makefile.am +5 -1
index 60a1998..960b5e9 100644
@@ -64,7 +64,11 @@ libccan_a_SOURCES = \
 	ccan/array_size/array_size.h \
 	ccan/minmax/minmax.h \
 	ccan/short_types/short_types.h \
-	ccan/endian/endian.h
+	ccan/endian/endian.h \
+	ccan/ciniparser/ciniparser.h \
+	ccan/ciniparser/ciniparser.c \
+	ccan/ciniparser/dictionary.h \
+	ccan/ciniparser/dictionary.c
 
 noinst_LIBRARIES += libutil.a
 libutil_a_SOURCES = \
ccan/ciniparser/ciniparser.c new file +480 -0

480 lines not on this page · raw message

ccan/ciniparser/ciniparser.h new file +262 -0

262 lines not on this page · raw message

ccan/ciniparser/dictionary.c new file +266 -0

266 lines not on this page · raw message

ccan/ciniparser/dictionary.h new file +166 -0

166 lines not on this page · raw message

Signature · 1 line
2.31.1
#2 lore raw

Re: [ndctl PATCH 1/5] ndctl, ccan: import ciniparser

Quoted 3 lines · QI Fuli

Import ciniparser from ccan[1].
[1] https://ccodearchive.net/info/ciniparser.html

Hi Qi,

Vishal points out that an updated version of this code is now available as a proper library that ships in distributions called "iniparser".

maintainer
#3 lore raw

Re: [ndctl PATCH 1/5] ndctl, ccan: import ciniparser

Quoted 11 lines · Dan Williams
Quoted 3 lines · QI Fuli

Import ciniparser from ccan[1].
[1] https://ccodearchive.net/info/ciniparser.html

Hi Qi,

Vishal points out that an updated version of this code is now available as a proper library that ships in distributions called "iniparser".

Here is the link to the project - it seems like it is maintained by the same person, just outside of ccan, and as a standalone project:

https://github.com/ndevilla/iniparser

#4 lore raw

RE: [ndctl PATCH 1/5] ndctl, ccan: import ciniparser

Quoted 9 lines · Dan Williams
Quoted 7 lines · QI Fuli
Quoted 3 lines

Import ciniparser from ccan[1].
[1] https://ccodearchive.net/info/ciniparser.html

Hi Qi,

Quoted 8 lines
Quoted 4 lines

Vishal points out that an updated version of this code is now available as a proper library that ships in distributions called "iniparser".

Here is the link to the project - it seems like it is maintained by the same person, just outside of ccan, and as a standalone project:

https://github.com/ndevilla/iniparser

Thank you for the information.
I will send an update patch set.

Qi

author
#5 lore raw

PATCH 2/5 ndctl, util: add parse-configs helper

In reply to QI Fuli

From: QI Fuli <[email protected]>

Add parse-config util to help ndctl commands parse ndctl global configuration file(s).

---
 Makefile.am          |  2 ++
 util/parse-configs.c | 82 ++++++++++++++++++++++++++++++++++++++++++++
 util/parse-configs.h | 34 ++++++++++++++++++
 3 files changed, 118 insertions(+)
 create mode 100644 util/parse-configs.c
 create mode 100644 util/parse-configs.h
Makefile.am +2 -0
index 960b5e9..6e50741 100644
@@ -74,6 +74,8 @@ noinst_LIBRARIES += libutil.a
 libutil_a_SOURCES = \
 	util/parse-options.c \
 	util/parse-options.h \
+	util/parse-configs.c \
+	util/parse-configs.h \
 	util/usage.c \
 	util/size.c \
 	util/main.c \
util/parse-configs.c new file +82 -0

82 lines not on this page · raw message

util/parse-configs.h new file +34 -0
new file mode 100644
index 0000000..f70f58f
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2021, FUJITSU LIMITED. ALL rights reserved.
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <util/util.h>
+
+enum parse_conf_type {
+	CONFIG_STRING,
+	CONFIG_END,
+	MONITOR_CALLBACK,
+};
+
+struct config;
+typedef int parse_conf_cb(const struct config *, const char *config_file);
+
+struct config {
+	enum parse_conf_type type;
+	const char *key;
+	void *value;
+	void *defval;
+	parse_conf_cb *callback;
+};
+
+#define check_vtype(v, type) ( BUILD_BUG_ON_ZERO(!__builtin_types_compatible_p(typeof(v), type)) + v )
+
+#define CONF_END() { .type = CONFIG_END }
+#define CONF_STR(k,v,d) \
+	{ .type = CONFIG_STRING, .key = (k), .value = check_vtype(v, const char **), .defval = (d) }
+#define CONF_MONITOR(k,f) \
+	{ .type = MONITOR_CALLBACK, .key = (k), .callback = (f)}
+
+int parse_configs_prefix(const char *__config_file, const char *prefix,
+				const struct config *configs);
Signature · 1 line
2.31.1
author
#6 lore raw

PATCH 3/5 ndctl: make ndctl support configuration files

In reply to QI Fuli

From: QI Fuli <[email protected]>

Add ndctl_configs to ndctl_ctx for supporting ndctl global configuration files. All files with .conf suffix under {sysconfdir}/ndctl can be regarded as global configuration files that all ndctl commands can refer to. Add ndctl_set_configs() public function for setting ndctl default configuration files. Add ndctl_get_configs() public function for reading configuration files from ndctl_ctx.

---
 configure.ac           |  4 ++--
 ndctl/Makefile.am      |  5 ++--
 ndctl/lib/Makefile.am  |  4 ++++
 ndctl/lib/libndctl.c   | 52 ++++++++++++++++++++++++++++++++++++++++++
 ndctl/lib/libndctl.sym |  2 ++
 ndctl/lib/private.h    |  1 +
 ndctl/libndctl.h       |  2 ++
 ndctl/ndctl.c          |  1 +
 8 files changed, 67 insertions(+), 4 deletions(-)
configure.ac +2 -2
index dc39dbe..42a66e1 100644
@@ -171,9 +171,9 @@ fi
 AC_SUBST([systemd_unitdir])
 AM_CONDITIONAL([ENABLE_SYSTEMD_UNITS], [test "x$with_systemd" = "xyes"])
 
-ndctl_monitorconfdir=${sysconfdir}/ndctl
+ndctl_confdir=${sysconfdir}/ndctl
 ndctl_monitorconf=monitor.conf
-AC_SUBST([ndctl_monitorconfdir])
+AC_SUBST([ndctl_confdir])
 AC_SUBST([ndctl_monitorconf])
 
 daxctl_modprobe_datadir=${datadir}/daxctl
ndctl/Makefile.am +3 -2
index a63b1e0..1caa031 100644
@@ -7,8 +7,9 @@ BUILT_SOURCES = config.h
 config.h: $(srcdir)/Makefile.am
 	$(AM_V_GEN) echo "/* Autogenerated by ndctl/Makefile.am */" >$@ && \
 	echo '#define NDCTL_CONF_FILE \
-		"$(ndctl_monitorconfdir)/$(ndctl_monitorconf)"' >>$@
+		"$(ndctl_confdir)/$(ndctl_monitorconf)"' >>$@
 	$(AM_V_GEN) echo '#define NDCTL_KEYS_DIR  "$(ndctl_keysdir)"' >>$@
+	$(AM_V_GEN) echo '#define NDCTL_CONF_DIR  "$(ndctl_confdir)"' >>$@
 
 ndctl_SOURCES = ndctl.c \
 		builtin.h \
@@ -73,7 +74,7 @@ ndctl_SOURCES += ../test/libndctl.c \
 		 test.c
 endif
 
-monitor_configdir = $(ndctl_monitorconfdir)
+monitor_configdir = $(ndctl_confdir)
 monitor_config_DATA = $(ndctl_monitorconf)
 
 if ENABLE_SYSTEMD_UNITS
ndctl/lib/Makefile.am +4 -0
index e15bb22..f741c44 100644
@@ -14,6 +14,10 @@ libndctl_la_SOURCES =\
 	../../util/log.h \
 	../../util/sysfs.c \
 	../../util/sysfs.h \
+	../../util/strbuf.h \
+	../../util/strbuf.c \
+	../../util/wrapper.c \
+	../../util/usage.c \
 	../../util/fletcher.h \
 	dimm.c \
 	inject.c \
ndctl/lib/libndctl.c +52 -0

70 lines not on this page · raw message

ndctl/lib/libndctl.sym +2 -0
index 58afb74..5bd867d 100644
@@ -454,4 +454,6 @@ LIBNDCTL_25 {
 
 LIBNDCTL_26 {
 	ndctl_bus_nfit_translate_spa;
+	ndctl_set_configs;
+	ndctl_get_configs;
 } LIBNDCTL_25;
ndctl/lib/private.h +1 -0
index 8f4510e..f4ca71f 100644
@@ -129,6 +129,7 @@ struct ndctl_ctx {
 	int regions_init;
 	void *userdata;
 	struct list_head busses;
+	const char *configs;
 	int busses_init;
 	struct udev *udev;
 	struct udev_queue *udev_queue;
ndctl/libndctl.h +2 -0
index 87d07b7..7ab7691 100644
@@ -92,6 +92,8 @@ int ndctl_get_log_priority(struct ndctl_ctx *ctx);
 void ndctl_set_log_priority(struct ndctl_ctx *ctx, int priority);
 void ndctl_set_userdata(struct ndctl_ctx *ctx, void *userdata);
 void *ndctl_get_userdata(struct ndctl_ctx *ctx);
+void ndctl_set_configs(struct ndctl_ctx **ctx, char *conf_dir);
+const char *ndctl_get_configs(struct ndctl_ctx *ctx);
 
 enum ndctl_persistence_domain {
 	PERSISTENCE_NONE = 0,
ndctl/ndctl.c +1 -0
index 31d2c5e..0f00b04 100644
@@ -125,6 +125,7 @@ int main(int argc, const char **argv)
 	rc = ndctl_new(&ctx);
 	if (rc)
 		goto out;
+	ndctl_set_configs(&ctx, NDCTL_CONF_DIR);
 	main_handle_internal_command(argc, argv, ctx, commands,
 			ARRAY_SIZE(commands), PROG_NDCTL);
 	ndctl_unref(ctx);
Signature · 1 line
2.31.1
author
#7 lore raw

PATCH 4/5 ndctl, config: add the default ndctl configuration file

In reply to QI Fuli

From: QI Fuli <[email protected]>

Install ndctl/ndctl.conf as the default ndctl configuration file.

---
 configure.ac      |  2 ++
 ndctl/Makefile.am |  4 +++-
 ndctl/ndctl.conf  | 56 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/ndctl.conf
configure.ac +2 -0
index 42a66e1..9e1c6db 100644
@@ -172,8 +172,10 @@ AC_SUBST([systemd_unitdir])
 AM_CONDITIONAL([ENABLE_SYSTEMD_UNITS], [test "x$with_systemd" = "xyes"])
 
 ndctl_confdir=${sysconfdir}/ndctl
+ndctl_conf=ndctl.conf
 ndctl_monitorconf=monitor.conf
 AC_SUBST([ndctl_confdir])
+AC_SUBST([ndctl_conf])
 AC_SUBST([ndctl_monitorconf])
 
 daxctl_modprobe_datadir=${datadir}/daxctl
ndctl/Makefile.am +3 -1
index 1caa031..fceb3ab 100644
@@ -43,7 +43,7 @@ keys_configdir = $(ndctl_keysdir)
 keys_config_DATA = $(ndctl_keysreadme)
 endif
 
-EXTRA_DIST += keys.readme monitor.conf ndctl-monitor.service
+EXTRA_DIST += keys.readme monitor.conf ndctl-monitor.service ndctl.conf
 
 if ENABLE_DESTRUCTIVE
 ndctl_SOURCES += ../test/blk_namespaces.c \
@@ -74,6 +74,8 @@ ndctl_SOURCES += ../test/libndctl.c \
 		 test.c
 endif
 
+ndctl_configdir = $(ndctl_confdir)
+ndctl_config_DATA = $(ndctl_conf)
 monitor_configdir = $(ndctl_confdir)
 monitor_config_DATA = $(ndctl_monitorconf)
 
ndctl/ndctl.conf new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.31.1
author
#8 lore raw

PATCH 5/5 ndctl, monitor: refactor monitor for supporting multiple config files

In reply to QI Fuli

From: QI Fuli <[email protected]>

In order to support multiple configration files, refactor ndctl monitor by using parse-configs helper.

---
 Documentation/ndctl/Makefile.am       |  2 +-
 Documentation/ndctl/ndctl-monitor.txt |  8 ++--
 ndctl/Makefile.am                     |  1 +
 ndctl/monitor.c                       | 69 ++++++++++++++-------------
 4 files changed, 42 insertions(+), 38 deletions(-)
Documentation/ndctl/Makefile.am +1 -1
index f0d5b21..37855cc 100644
@@ -59,7 +59,7 @@ CLEANFILES = $(man1_MANS)
 .ONESHELL:
 attrs.adoc: $(srcdir)/Makefile.am
 	$(AM_V_GEN) cat <<- EOF >$@
-		:ndctl_monitorconfdir: $(ndctl_monitorconfdir)
+		:ndctl_confdir: $(ndctl_confdir)
 		:ndctl_monitorconf: $(ndctl_monitorconf)
 		:ndctl_keysdir: $(ndctl_keysdir)
 		EOF
Documentation/ndctl/ndctl-monitor.txt +4 -4
index dbc9070..8c8c35b 100644
@@ -21,8 +21,8 @@ objects and dumping the json format notifications to syslog, standard
 output or a logfile.
 
 The objects to monitor and smart events to notify can be selected by
-setting options and/or the configuration file at
-{ndctl_monitorconfdir}/{ndctl_monitorconf}
+setting options and/or configuration files with .conf suffix under
+{ndctl_confdir}
 
 Both, the values in configuration file and in options will work. If
 there is a conflict, the values in options will override the values in
@@ -81,8 +81,8 @@ will not work if "--daemon" is specified.
 
 -c::
 --config-file=::
-	Provide the config file to use. This overrides the default config
-	typically found in {ndctl_monitorconfdir}
+	Provide the config file(s) to use. This overrides the default config
+	typically found in {ndctl_confdir}
 
 --daemon::
 	Run a monitor as a daemon.
ndctl/Makefile.am +1 -0
index fceb3ab..3c59aa7 100644
@@ -55,6 +55,7 @@ ndctl_LDADD =\
 	lib/libndctl.la \
 	../daxctl/lib/libdaxctl.la \
 	../libutil.a \
+	../libccan.a \
 	$(UUID_LIBS) \
 	$(KMOD_LIBS) \
 	$(JSON_LIBS)
ndctl/monitor.c +36 -33

135 lines not on this page · raw message

Signature · 1 line
2.31.1