Some configuration attribute are read by OpenSAF daemons as
environment variables. eg.

export FMS_PROMOTE_ACTIVE_TIMER=0

There is no easy way to reload these values without a restart.

ConfigFileReader will parse these files looking for 'export VAR=VAL'
and store them into a map, so a daemon can reload configuration
without a restart.
---
 src/base/Makefile.am           |  2 +
 src/base/config_file_reader.cc | 88 ++++++++++++++++++++++++++++++++++
 src/base/config_file_reader.h  | 30 ++++++++++++
 3 files changed, 120 insertions(+)
 create mode 100644 src/base/config_file_reader.cc
 create mode 100644 src/base/config_file_reader.h

diff --git a/src/base/Makefile.am b/src/base/Makefile.am
index c7dd01900..ce93562e5 100644
--- a/src/base/Makefile.am
+++ b/src/base/Makefile.am
@@ -33,6 +33,7 @@ lib_libopensaf_core_la_LDFLAGS += \
 lib_libopensaf_core_la_SOURCES += \
        src/base/condition_variable.cc \
        src/base/conf.cc \
+       src/base/config_file_reader.cc \
        src/base/daemon.c \
        src/base/file_descriptor.cc \
        src/base/file_notify.cc \
@@ -94,6 +95,7 @@ noinst_HEADERS += \
        src/base/buffer.h \
        src/base/condition_variable.h \
        src/base/conf.h \
+       src/base/config_file_reader.h \
        src/base/daemon.h \
        src/base/file_descriptor.h \
        src/base/file_notify.h \
diff --git a/src/base/config_file_reader.cc b/src/base/config_file_reader.cc
new file mode 100644
index 000000000..a72f45691
--- /dev/null
+++ b/src/base/config_file_reader.cc
@@ -0,0 +1,88 @@
+/*      -*- OpenSAF  -*-
+ *
+ * Copyright Ericsson AB 2018 - All Rights Reserved.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
+ * under the GNU Lesser General Public License Version 2.1, February 1999.
+ * The complete license can be accessed from the following location:
+ * http://opensource.org/licenses/lgpl-license.php
+ * See the Copying file included with the OpenSAF distribution for full
+ * licensing terms.
+ *
+ */
+
+#include <fstream>
+#include <regex>
+#include <string>
+#include "base/config_file_reader.h"
+
+// @todo move into a string library class
+static void left_trim(std::string& str) {
+  str.erase(str.begin(), std::find_if(str.begin(), str.end(),
+                                      [](int c) { return !std::isspace(c); }));
+}
+
+static void right_trim(std::string& str) {
+  str.erase(std::find_if(str.rbegin(), str.rend(),
+                         [](int c) { return !std::isspace(c); })
+                .base(),
+            str.end());
+}
+
+static void trim(std::string& str) {
+  left_trim(str);
+  right_trim(str);
+}
+
+ConfigFileReader::SettingsMap ConfigFileReader::ParseFile(
+    const std::string& filename) {
+  const std::string prefix("export");
+  SettingsMap map;
+  std::ifstream file(filename);
+  std::string line;
+
+  if (file.is_open()) {
+    while (getline(file, line)) {
+      // go through each line of the config file
+      // only process "export key=value" and ignore trailing comments
+      // note: value may be surrounded with quotes
+
+      // trim leading / trailing spaces
+      trim(line);
+
+      // must begin with 'export'
+      if (line.find(prefix) != 0) {
+        continue;
+      }
+
+      // remove 'export'
+      line.erase(0, prefix.length());
+
+      // remove any trailing comments
+      size_t hash = line.find('#');
+      if (hash != std::string::npos) {
+        line.erase(hash, std::string::npos);
+      }
+
+      trim(line);
+
+      size_t equal = line.find('=');
+      if (equal == std::string::npos ||
+          equal == 0) {
+        // must look like "key=value"
+        continue;
+      }
+
+      std::string key = line.substr(0, equal);
+      trim(key);
+      std::string value = line.substr(equal + 1);
+      trim(value);
+
+      map[key] = value;
+    }
+    file.close();
+  }
+  return map;
+}
diff --git a/src/base/config_file_reader.h b/src/base/config_file_reader.h
new file mode 100644
index 000000000..b369a802c
--- /dev/null
+++ b/src/base/config_file_reader.h
@@ -0,0 +1,30 @@
+/*      -*- OpenSAF  -*-
+ *
+ * Copyright Ericsson AB 2018 - All Rights Reserved.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
+ * under the GNU Lesser General Public License Version 2.1, February 1999.
+ * The complete license can be accessed from the following location:
+ * http://opensource.org/licenses/lgpl-license.php
+ * See the Copying file included with the OpenSAF distribution for full
+ * licensing terms.
+ *
+ */
+
+#ifndef BASE_CONFIG_FILE_READER_H_
+#define BASE_CONFIG_FILE_READER_H_
+
+#include <map>
+#include <string>
+
+class ConfigFileReader {
+ public:
+  using SettingsMap = std::map<std::string, std::string>;
+
+  // parses OpenSAF 'config' files and return key-value pairs in a map
+  SettingsMap ParseFile(const std::string& filename);
+};
+
+#endif /* BASE_CONFIG_FILE_READER_H_ */
-- 
2.17.1



_______________________________________________
Opensaf-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensaf-devel

Reply via email to