diff mbox

[API-NEXT,PATCHv3,3/5] linux-gen: init: adding configuration file parsing

Message ID 1481034217-16689-4-git-send-email-christophe.milard@linaro.org
State Superseded
Headers show

Commit Message

Christophe Milard Dec. 6, 2016, 2:23 p.m. UTC
The parsing of the odp.conf configuration file is added.
The file is searched first in the local directory (.) and
then is the $prefix/etc directory.
This requires libconfig (sudo apt-get install libconfig-dev)

Signed-off-by: Christophe Milard <christophe.milard@linaro.org>

---
 DEPENDENCIES                                  |  2 +-
 platform/linux-generic/Makefile.am            |  1 +
 platform/linux-generic/include/odp_internal.h |  2 +
 platform/linux-generic/m4/configure.m4        | 11 +++++
 platform/linux-generic/odp_init.c             | 70 +++++++++++++++++++++++++++
 5 files changed, 85 insertions(+), 1 deletion(-)

-- 
2.7.4
diff mbox

Patch

diff --git a/DEPENDENCIES b/DEPENDENCIES
index f1f0edd..a24b1c8 100644
--- a/DEPENDENCIES
+++ b/DEPENDENCIES
@@ -18,7 +18,7 @@  Prerequisites for building the OpenDataPlane (ODP) API
 
 3. Required libraries
 
-   Libraries currently required to link: openssl
+   Libraries currently required to link: openssl, libconfig-dev
 
 3.1 OpenSSL native compile
 
diff --git a/platform/linux-generic/Makefile.am b/platform/linux-generic/Makefile.am
index 1e2e7f1..a38d47e 100644
--- a/platform/linux-generic/Makefile.am
+++ b/platform/linux-generic/Makefile.am
@@ -8,6 +8,7 @@  AM_CFLAGS +=  -I$(srcdir)/include
 AM_CFLAGS +=  -I$(top_srcdir)/include
 AM_CFLAGS +=  -I$(top_builddir)/include
 AM_CFLAGS +=  -Iinclude
+AM_CFLAGS +=  -DSYSCONFDIR=\"@sysconfdir@\"
 
 include_HEADERS = \
 		  $(top_srcdir)/include/odp.h \
diff --git a/platform/linux-generic/include/odp_internal.h b/platform/linux-generic/include/odp_internal.h
index 6a80d9d..9d2c78b 100644
--- a/platform/linux-generic/include/odp_internal.h
+++ b/platform/linux-generic/include/odp_internal.h
@@ -22,6 +22,7 @@  extern "C" {
 #include <odp/api/thread.h>
 #include <stdio.h>
 #include <sys/types.h>
+#include <libconfig.h>
 
 extern __thread int __odp_errno;
 
@@ -51,6 +52,7 @@  struct odp_global_data_s {
 	odp_cpumask_t worker_cpus;
 	int num_cpus_installed;
 	int ipc_ns;
+	config_t configuration;
 };
 
 enum init_stage {
diff --git a/platform/linux-generic/m4/configure.m4 b/platform/linux-generic/m4/configure.m4
index d3e5528..5fab0cc 100644
--- a/platform/linux-generic/m4/configure.m4
+++ b/platform/linux-generic/m4/configure.m4
@@ -28,6 +28,17 @@  AC_LINK_IFELSE(
     echo "Use newer version. For gcc > 4.7.0"
     exit -1)
 
+# Check for libconfig (required)
+AC_CHECK_HEADERS([libconfig.h], HEADER_LIBCONFIG="yes")
+PKG_CHECK_MODULES([PKGCONFIG], [libconfig >= 1.3.2], LIBRARY_LIBCONFIG="yes")
+if test "x$LIBRARY_LIBCONFIG" != "x" && test "x$HEADER_LIBCONFIG" != "x" ; then
+    CFLAGS="$CFLAGS $PKGCONFIG_CFLAGS"
+    LIBS="$LIBS $PKGCONFIG_LIBS"
+    AM_CPPFLAGS="$AM_CPPFLAGS `pkg-config --cflags-only-I libconfig`"
+else
+    AC_MSG_FAILURE([libconfig not found (required)])
+fi
+
 m4_include([platform/linux-generic/m4/odp_pthread.m4])
 m4_include([platform/linux-generic/m4/odp_openssl.m4])
 m4_include([platform/linux-generic/m4/odp_pcap.m4])
diff --git a/platform/linux-generic/odp_init.c b/platform/linux-generic/odp_init.c
index d40a83c..797c60a 100644
--- a/platform/linux-generic/odp_init.c
+++ b/platform/linux-generic/odp_init.c
@@ -10,6 +10,8 @@ 
 #include <odp_internal.h>
 #include <odp_schedule_if.h>
 #include <string.h>
+#include <libconfig.h>
+#include <stdlib.h>
 #include <stdio.h>
 #include <linux/limits.h>
 #include <dirent.h>
@@ -21,6 +23,15 @@ 
 #define _ODP_FILES_FMT "odp-%d-"
 #define _ODP_TMPDIR    "/tmp"
 
+/* the name of the ODP configuration file: */
+#define CONFIGURATION_FILE_ENV_NONE "none"
+#define CONFIGURATION_FILE "odp.conf"
+#define CONFIGURATION_FILE_REL ("./" CONFIGURATION_FILE)
+#define CONFIGURATION_FILE_ABS (SYSCONFDIR "/" CONFIGURATION_FILE)
+
+/* the ODP configuration file name can also be oveerwritten by env. variable: */
+#define ODP_SYSCONFIG_FILE_ENV "ODP_SYSCONFIG_FILE"
+
 struct odp_global_data_s odp_global_data;
 
 /* remove all files staring with "odp-<pid>" from a directory "dir" */
@@ -65,6 +76,62 @@  static int cleanup_files(const char *dirpath, int odp_pid)
 	return 0;
 }
 
+/* read the odp configuration file
+ *
+ * the configuration file is read from:
+ * 1) Wherever env variable ODP_SYSCONFIG_FILE says (or "none")
+ * 2) ./odp.conf
+ * 3) the @sysconfig@/odp.conf
+ * (checked in reversed order overwritting each-other)
+ * So the environment variable setting supperseeds any other file.
+ * If the environment variable exists and set to the string "none"
+ * the configuration file reading is inibited (used to prevent
+ * test which do not need a file to read the user or system files)
+ */
+static int read_configfile(void)
+{
+	char *env_config_filename;
+	const char *config_filename;
+	config_t *cf;
+
+	/* initialize and read the configuration file if any: */
+	cf = &odp_global_data.configuration;
+	config_init(cf);
+	config_filename = NULL;
+	if (access(CONFIGURATION_FILE_ABS, R_OK) != -1)
+		config_filename = CONFIGURATION_FILE_ABS;
+	if (access(CONFIGURATION_FILE_REL, R_OK) != -1)
+		config_filename = CONFIGURATION_FILE_REL;
+	env_config_filename = getenv(ODP_SYSCONFIG_FILE_ENV);
+	if (env_config_filename) {
+		if (!strcmp(env_config_filename, CONFIGURATION_FILE_ENV_NONE))
+			return 0;
+		if (access(env_config_filename, R_OK) != -1) {
+			config_filename = env_config_filename;
+		} else {
+			ODP_ERR("Cannot read ODP configurattion file %s "
+				"(set by env variable "
+				ODP_SYSCONFIG_FILE_ENV ")\n",
+				env_config_filename);
+			config_filename = NULL;
+			return -1;
+		}
+	}
+	if (config_filename) {
+		ODP_DBG("Reading configuration file: %s\n", config_filename);
+		if (!config_read_file(cf, config_filename)) {
+			ODP_ERR("%s:%d - %s\n",
+				config_error_file(cf),
+				config_error_line(cf),
+				config_error_text(cf));
+			config_destroy(cf);
+			return(-1);
+		}
+	}
+
+	return 0;
+}
+
 int odp_init_global(odp_instance_t *instance,
 		    const odp_init_t *params,
 		    const odp_platform_init_t *platform_params)
@@ -89,6 +156,9 @@  int odp_init_global(odp_instance_t *instance,
 			odp_global_data.abort_fn = params->abort_fn;
 	}
 
+	if (read_configfile())
+		goto init_failed;
+
 	if (odp_cpumask_init_global(params)) {
 		ODP_ERR("ODP cpumask init failed.\n");
 		goto init_failed;