Blob Blame History Raw
From aa86e1a4ba6a472b6eb2af6cad07672322eafded Mon Sep 17 00:00:00 2001
From: Jakub Filak <jfilak@redhat.com>
Date: Sun, 10 Nov 2013 10:19:57 +0100
Subject: [ABRT PATCH 69/76] Load conf from /usr/share/abrt/conf.d and
 /etc/abrt

Related to #752

Signed-off-by: Jakub Filak <jfilak@redhat.com>
---
 configure.ac                                 |  4 ++
 src/daemon/Makefile.am                       |  2 +
 src/daemon/abrt-action-save-package-data.c   | 22 ++++++++---
 src/hooks/Makefile.am                        |  3 ++
 src/hooks/abrt-hook-ccpp.c                   |  2 +-
 src/hooks/abrt_exception_handler.py.in       | 18 ++-------
 src/hooks/abrt_harvest_vmcore.py.in          | 30 +++++++-------
 src/include/libabrt.h                        |  6 +++
 src/lib/Makefile.am                          |  3 ++
 src/lib/abrt_conf.c                          | 22 +++++++++--
 src/plugins/Makefile.am                      |  4 +-
 src/plugins/abrt-action-analyze-xorg.c       |  8 ++--
 src/plugins/abrt-action-generate-backtrace.c |  5 ++-
 src/plugins/abrt-action-install-debuginfo.in | 20 +++-------
 src/python-problem/problem/common.h          |  2 +
 src/python-problem/problem/pyabrt.c          | 59 ++++++++++++++++++++++++++++
 src/python-problem/problem/pyabrtmodule.c    |  2 +
 17 files changed, 151 insertions(+), 61 deletions(-)

diff --git a/configure.ac b/configure.ac
index 113b315..f6ae82e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -132,8 +132,10 @@ AC_CHECK_HEADER([sys/inotify.h], [],
 AC_CHECK_HEADERS([locale.h])
 
 CONF_DIR='${sysconfdir}/${PACKAGE_NAME}'
+DEFAULT_CONF_DIR='${datadir}/${PACKAGE_NAME}/conf.d'
 VAR_RUN='${localstatedir}/run'
 PLUGINS_CONF_DIR='${sysconfdir}/${PACKAGE_NAME}/plugins'
+DEFAULT_PLUGINS_CONF_DIR='${datadir}/${PACKAGE_NAME}/conf.d/plugins'
 EVENTS_DIR='${datadir}/libreport/events'
 EVENTS_CONF_DIR='${sysconfdir}/libreport/events.d'
 ENABLE_SOCKET_OR_DBUS='-DENABLE_DBUS=1'
@@ -191,8 +193,10 @@ AC_ARG_ENABLE([native-unwinder],
 
 
 AC_SUBST(CONF_DIR)
+AC_SUBST(DEFAULT_CONF_DIR)
 AC_SUBST(VAR_RUN)
 AC_SUBST(PLUGINS_CONF_DIR)
+AC_SUBST(DEFAULT_PLUGINS_CONF_DIR)
 AC_SUBST(EVENTS_CONF_DIR)
 AC_SUBST(EVENTS_DIR)
 AC_SUBST(DEFAULT_DUMP_LOCATION)
diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am
index fba6b83..158f3e5 100644
--- a/src/daemon/Makefile.am
+++ b/src/daemon/Makefile.am
@@ -104,6 +104,8 @@ dist_daemonconf_DATA = \
     abrt.conf \
     abrt-action-save-package-data.conf \
     gpg_keys.conf
+defaultdaemonconfdir = $(DEFAULT_CONF_DIR)
+dist_defaultdaemonconf_DATA = $(dist_daemonconf_DATA)
 
 EXTRA_DIST = abrt-handle-upload.in
 
diff --git a/src/daemon/abrt-action-save-package-data.c b/src/daemon/abrt-action-save-package-data.c
index 5da6905..6dbcfc2 100644
--- a/src/daemon/abrt-action-save-package-data.c
+++ b/src/daemon/abrt-action-save-package-data.c
@@ -20,6 +20,8 @@
 #include "libabrt.h"
 #include "rpm.h"
 
+#define GPG_CONF "gpg_keys.conf"
+
 static bool   settings_bOpenGPGCheck = false;
 static GList *settings_setOpenGPGPublicKeys = NULL;
 static GList *settings_setBlackListedPkgs = NULL;
@@ -79,10 +81,9 @@ static void ParseCommon(map_string_t *settings, const char *conf_filename)
 static void load_gpg_keys(void)
 {
     map_string_t *settings = new_map_string();
-    const char *conf_filename = CONF_DIR"/gpg_keys.conf";
-    if (!load_conf_file(conf_filename, settings, /*skip key w/o values:*/ false))
+    if (!load_abrt_conf_file(GPG_CONF, settings))
     {
-        error_msg("Can't open '%s'", conf_filename);
+        error_msg("Can't load '%s'", GPG_CONF);
         return;
     }
 
@@ -106,8 +107,17 @@ static void load_gpg_keys(void)
 static int load_conf(const char *conf_filename)
 {
     map_string_t *settings = new_map_string();
-    if (!load_conf_file(conf_filename, settings, /*skip key w/o values:*/ false))
-        error_msg("Can't open '%s'", conf_filename);
+    if (conf_filename != NULL)
+    {
+        if (!load_conf_file(conf_filename, settings, false))
+            error_msg("Can't open '%s'", conf_filename);
+    }
+    else
+    {
+        conf_filename = "abrt-action-save-package-data.conf";
+        if (!load_abrt_conf_file(conf_filename, settings))
+            error_msg("Can't load '%s'", conf_filename);
+    }
 
     ParseCommon(settings, conf_filename);
     free_map_string(settings);
@@ -365,7 +375,7 @@ int main(int argc, char **argv)
     abrt_init(argv);
 
     const char *dump_dir_name = ".";
-    const char *conf_filename = CONF_DIR"/abrt-action-save-package-data.conf";
+    const char *conf_filename = NULL;
 
     /* Can't keep these strings/structs static: _() doesn't support that */
     const char *program_usage_string = _(
diff --git a/src/hooks/Makefile.am b/src/hooks/Makefile.am
index 567da6a..45ecba4 100644
--- a/src/hooks/Makefile.am
+++ b/src/hooks/Makefile.am
@@ -10,6 +10,9 @@ dist_pluginsconf_DATA = \
     CCpp.conf \
     python.conf
 
+defaultpluginsconfdir = $(DEFAULT_PLUGINS_CONF_DIR)
+dist_defaultpluginsconf_DATA = $(dist_pluginsconf_DATA)
+
 sbin_SCRIPTS = \
     abrt-install-ccpp-hook \
     abrt-harvest-vmcore \
diff --git a/src/hooks/abrt-hook-ccpp.c b/src/hooks/abrt-hook-ccpp.c
index a3f72a7..8e141d4 100644
--- a/src/hooks/abrt-hook-ccpp.c
+++ b/src/hooks/abrt-hook-ccpp.c
@@ -499,7 +499,7 @@ int main(int argc, char** argv)
     bool setting_SaveBinaryImage;
     {
         map_string_t *settings = new_map_string();
-        load_conf_file(PLUGINS_CONF_DIR"/CCpp.conf", settings, /*skip key w/o values:*/ false);
+        load_abrt_plugin_conf_file("CCpp.conf", settings);
         const char *value;
         value = get_map_string_item_or_NULL(settings, "MakeCompatCore");
         setting_MakeCompatCore = value && string_to_bool(value);
diff --git a/src/hooks/abrt_exception_handler.py.in b/src/hooks/abrt_exception_handler.py.in
index 4396178..96a63ff 100644
--- a/src/hooks/abrt_exception_handler.py.in
+++ b/src/hooks/abrt_exception_handler.py.in
@@ -167,23 +167,11 @@ def get_dso_list(tb):
 
 def conf_enabled(var_name):
     try:
-        file = open(@CONF_DIR@ + "/plugins/python.conf", "r")
+        conf = problem.load_plugin_conf_file("python.conf")
     except:
         return -1
-    for line in file:
-        w = line.split("=", 1)  # split on '=' to 2 parts max
-        if len(w) < 2:
-            continue
-        var = w[0].strip()  # remove whitespace
-        if var != var_name:
-            continue
-        val = w[1].strip()  # remove whitespace
-        if val == "yes":
-            return 1
-        if val == "no":
-            return 0
-    file.close()
-    return -1
+    else:
+        conf.get(var_name, -1)
 
 def handleMyException((etype, value, tb)):
     """
diff --git a/src/hooks/abrt_harvest_vmcore.py.in b/src/hooks/abrt_harvest_vmcore.py.in
index 894dd47..25eddb0 100644
--- a/src/hooks/abrt_harvest_vmcore.py.in
+++ b/src/hooks/abrt_harvest_vmcore.py.in
@@ -203,23 +203,21 @@ def harvest_vmcore():
     os.umask(077)
 
     # Check abrt config files for copy/move settings and
-    config = ConfigParser.ConfigParser()
-    # We need to add a root section as ConfigParser doesn't know how to work
-    # with config files without any sections
-    with open('@CONF_DIR@/abrt-harvest-vmcore.conf') as conf_file:
-        conf_str = '[section]\n' + conf_file.read()
-    conf_fp = StringIO.StringIO(conf_str)
-    config.readfp(conf_fp)
-    copyvmcore = config.get('section', 'CopyVMcore')
-    with open('@CONF_DIR@/abrt.conf') as conf_file:
-        conf_str = '[section]\n' + conf_file.read()
-    conf_fp = StringIO.StringIO(conf_str)
-    config = ConfigParser.ConfigParser()
-    config.readfp(conf_fp)
     try:
-        abrtdumpdir = config.get('section', 'DumpLocation')
-    except ConfigParser.NoOptionError:
-        abrtdumpdir = '@DEFAULT_DUMP_LOCATION@'
+        conf = problem.load_conf_file("abrt-harvest-vmcore.conf")
+    except OSError as ex:
+        sys.stderr.write(str(ex))
+        sys.exit(1)
+    else:
+        copyvmcore = conf.get("CopyVMcore", "no")
+
+    try:
+        conf = problem.load_conf_file("abrt.conf")
+    except OSError as ex:
+        sys.stderr.write(str(ex))
+        sys.exit(1)
+    else:
+        abrtdumpdir = conf.get("DumpLocation", "@DEFAULT_DUMP_LOCATION@")
 
     try:
         filelist = os.listdir(dump_dir)
diff --git a/src/include/libabrt.h b/src/include/libabrt.h
index 30399b6..518e989 100644
--- a/src/include/libabrt.h
+++ b/src/include/libabrt.h
@@ -82,6 +82,12 @@ int load_abrt_conf(void);
 #define free_abrt_conf_data abrt_free_abrt_conf_data
 void free_abrt_conf_data(void);
 
+#define load_abrt_conf_file abrt_load_abrt_conf_file
+int load_abrt_conf_file(const char *file, map_string_t *settings);
+
+#define load_abrt_plugin_conf_file abrt_load_abrt_plugin_conf_file
+int load_abrt_plugin_conf_file(const char *file, map_string_t *settings);
+
 
 void migrate_to_xdg_dirs(void);
 
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index c5a6e2f..8823f01 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -23,6 +23,9 @@ libabrt_la_CPPFLAGS = \
     -I$(srcdir)/../lib \
     -DVAR_RUN=\"$(VAR_RUN)\" \
     -DCONF_DIR=\"$(CONF_DIR)\" \
+    -DDEFAULT_CONF_DIR=\"$(DEFAULT_CONF_DIR)\" \
+    -DPLUGINS_CONF_DIR=\"$(PLUGINS_CONF_DIR)\" \
+    -DDEFAULT_PLUGINS_CONF_DIR=\"$(DEFAULT_PLUGINS_CONF_DIR)\" \
     -DEVENTS_DIR=\"$(EVENTS_DIR)\" \
     -DDEFAULT_DUMP_LOCATION=\"$(DEFAULT_DUMP_LOCATION)\" \
     $(GLIB_CFLAGS) \
diff --git a/src/lib/abrt_conf.c b/src/lib/abrt_conf.c
index 7230b77..5bb11e9 100644
--- a/src/lib/abrt_conf.c
+++ b/src/lib/abrt_conf.c
@@ -18,6 +18,8 @@
 */
 #include "libabrt.h"
 
+#define ABRT_CONF "abrt.conf"
+
 char *        g_settings_sWatchCrashdumpArchiveDir = NULL;
 unsigned int  g_settings_nMaxCrashReportsSize = 1000;
 char *        g_settings_dump_location = NULL;
@@ -119,11 +121,25 @@ int load_abrt_conf()
     free_abrt_conf_data();
 
     map_string_t *settings = new_map_string();
-    if (!load_conf_file(CONF_DIR"/abrt.conf", settings, /*skip key w/o values:*/ false))
-        perror_msg("Can't open '%s'", CONF_DIR"/abrt.conf");
+    if (!load_abrt_conf_file(ABRT_CONF, settings))
+        perror_msg("Can't load '%s'", ABRT_CONF);
 
-    ParseCommon(settings, CONF_DIR"/abrt.conf");
+    ParseCommon(settings, ABRT_CONF);
     free_map_string(settings);
 
     return 0;
 }
+
+int load_abrt_conf_file(const char *file, map_string_t *settings)
+{
+    static const char *const base_directories[] = { DEFAULT_CONF_DIR, CONF_DIR, NULL };
+
+    return load_conf_file_from_dirs(file, base_directories, settings, /*skip key w/o values:*/ false);
+}
+
+int load_abrt_plugin_conf_file(const char *file, map_string_t *settings)
+{
+    static const char *const base_directories[] = { DEFAULT_PLUGINS_CONF_DIR, PLUGINS_CONF_DIR, NULL };
+
+    return load_conf_file_from_dirs(file, base_directories, settings, /*skip key w/o values:*/ false);
+}
diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
index 370d5fd..60246f2 100644
--- a/src/plugins/Makefile.am
+++ b/src/plugins/Makefile.am
@@ -96,9 +96,11 @@ EXTRA_DIST = \
     post_report.xml.in \
     abrt-action-analyze-ccpp-local
 
-confdir = $(CONF_DIR)
+confdir = $(PLUGINS_CONF_DIR)
 dist_conf_DATA = \
     xorg.conf
+defaultconfdir = $(DEFAULT_PLUGINS_CONF_DIR)
+dist_defaultconf_DATA = $(dist_conf_DATA)
 
 abrt_watch_log_SOURCES = \
     abrt-watch-log.c
diff --git a/src/plugins/abrt-action-analyze-xorg.c b/src/plugins/abrt-action-analyze-xorg.c
index 5cf6d1e..5d11830 100644
--- a/src/plugins/abrt-action-analyze-xorg.c
+++ b/src/plugins/abrt-action-analyze-xorg.c
@@ -19,6 +19,8 @@
 #include <paths.h>
 #include "libabrt.h"
 
+#define XORG_CONF "xorg.conf"
+
 static
 void trim_spaces(char *str)
 {
@@ -87,9 +89,9 @@ int main(int argc, char **argv)
     export_abrt_envvars(0);
 
     map_string_t *settings = new_map_string();
-    VERB1 log("Loading settings from '%s'", "/etc/abrt/xorg.conf");
-    load_conf_file("/etc/abrt/xorg.conf", settings, /*skip key w/o values:*/ false);
-    VERB3 log("Loaded '%s'", "/etc/abrt/xorg.conf");
+    VERB1 log("Loading settings from '%s'", XORG_CONF);
+    load_abrt_plugin_conf_file(XORG_CONF, settings);
+    VERB3 log("Loaded '%s'", XORG_CONF);
     char *BlacklistedXorgModules = xstrdup(get_map_string_item_or_empty(settings, "BlacklistedXorgModules"));
     trim_spaces(BlacklistedXorgModules);
     free_map_string(settings);
diff --git a/src/plugins/abrt-action-generate-backtrace.c b/src/plugins/abrt-action-generate-backtrace.c
index 5612a1c..30ce446 100644
--- a/src/plugins/abrt-action-generate-backtrace.c
+++ b/src/plugins/abrt-action-generate-backtrace.c
@@ -18,6 +18,7 @@
 */
 #include "libabrt.h"
 
+#define CCPP_CONF "CCpp.conf"
 
 static const char *dump_dir_name = ".";
 /* 60 seconds was too limiting on slow machines */
@@ -61,8 +62,8 @@ int main(int argc, char **argv)
     export_abrt_envvars(0);
 
     map_string_t *settings = new_map_string();
-    if (!load_conf_file(PLUGINS_CONF_DIR"/CCpp.conf", settings, /*skip key w/o values:*/ false))
-        error_msg("Can't open '%s'", PLUGINS_CONF_DIR"/CCpp.conf");
+    if (!load_abrt_plugin_conf_file(CCPP_CONF, settings))
+        error_msg("Can't load '%s'", CCPP_CONF);
 
     const char *value = get_map_string_item_or_NULL(settings, "DebuginfoLocation");
     char *debuginfo_location;
diff --git a/src/plugins/abrt-action-install-debuginfo.in b/src/plugins/abrt-action-install-debuginfo.in
index b734796..5fd3110 100644
--- a/src/plugins/abrt-action-install-debuginfo.in
+++ b/src/plugins/abrt-action-install-debuginfo.in
@@ -14,6 +14,7 @@ from subprocess import Popen, PIPE
 from reportclient import _, verbose, log, log1, log2, set_verbosity, error_msg_and_die, error_msg
 import time
 from reportclient.debuginfo import DebugInfoDownload, filter_installed_debuginfos, build_ids_to_path, clean_up
+import problem
 
 # everything was ok
 RETURN_OK = 0
@@ -144,20 +145,11 @@ if __name__ == "__main__":
 
     if not cachedirs:
         try:
-            fp = open("@sysconfdir@/@PACKAGE_NAME@/plugins/CCpp.conf", "r")
-            for line in fp:
-                stripped = line.lstrip()
-                if len(stripped) == 0:
-                    continue
-                if stripped[0] == "#":
-                    continue
-                if stripped[:len("DebuginfoLocation")] != "DebuginfoLocation":
-                    continue
-
-                cachedirs =  stripped[len("DebuginfoLocation"):].strip(" =\n").split(":")
-            fp.close()
-        except IOError as (errno, strerror):
-            print "I/O error({0}): {1}".format(errno, strerror)
+            conf = problem.load_plugin_conf_file("CCpp.conf")
+        except OSError as ex:
+            print str(ex)
+        else:
+            cachedirs = conf.get("DebuginfoLocation", None)
 
         if not cachedirs:
             cachedirs = ["/var/cache/abrt-di"]
diff --git a/src/python-problem/problem/common.h b/src/python-problem/problem/common.h
index ef323a5..2dd06c7 100644
--- a/src/python-problem/problem/common.h
+++ b/src/python-problem/problem/common.h
@@ -20,3 +20,5 @@
 
 /* module-level functions */
 PyObject *p_notify_new_path(PyObject *pself, PyObject *args);
+PyObject *p_load_conf_file(PyObject *pself, PyObject *args);
+PyObject *p_load_plugin_conf_file(PyObject *pself, PyObject *args);
diff --git a/src/python-problem/problem/pyabrt.c b/src/python-problem/problem/pyabrt.c
index 16621d3..16d455f 100644
--- a/src/python-problem/problem/pyabrt.c
+++ b/src/python-problem/problem/pyabrt.c
@@ -32,3 +32,62 @@ PyObject *p_notify_new_path(PyObject *pself, PyObject *args)
     notify_new_path(path);
     Py_RETURN_NONE;
 }
+
+static PyObject *
+load_settings_to_dict(const char *file, int (*loader)(const char *, map_string_t *))
+{
+    PyObject *dict = NULL;
+    map_string_t *settings = new_map_string();
+    if (!loader(file, settings))
+    {
+        PyErr_SetString(PyExc_OSError, "Failed to load configuration file.");
+        goto lacf_error;
+    }
+
+    dict = PyDict_New();
+    if (dict == NULL)
+    {
+        goto lacf_error;
+    }
+
+    map_string_iter_t iter;
+    const char *key = NULL;
+    const char *value = NULL;
+    init_map_string_iter(&iter, settings);
+    while(next_map_string_iter(&iter, &key, &value))
+    {
+        if (0 != PyDict_SetItemString(dict, key, PyString_FromString(value)))
+        {
+            goto lacf_error;
+        }
+    }
+    free_map_string(settings);
+    return dict;
+
+lacf_error:
+    Py_XDECREF(dict);
+    free_map_string(settings);
+    return NULL;
+}
+
+/* C: void load_abrt_conf_file(const char *file, map_string_t *settings); */
+PyObject *p_load_conf_file(PyObject *pself, PyObject *args)
+{
+    const char *file;
+    if (!PyArg_ParseTuple(args, "s", &file))
+    {
+        return NULL;
+    }
+    return load_settings_to_dict(file, load_abrt_conf_file);
+}
+
+/* C: void load_abrt_plugin_conf_file(const char *file, map_string_t *settings); */
+PyObject *p_load_plugin_conf_file(PyObject *pself, PyObject *args)
+{
+    const char *file;
+    if (!PyArg_ParseTuple(args, "s", &file))
+    {
+        return NULL;
+    }
+    return load_settings_to_dict(file, load_abrt_plugin_conf_file);
+}
diff --git a/src/python-problem/problem/pyabrtmodule.c b/src/python-problem/problem/pyabrtmodule.c
index 7ff58b3..e31d8b4 100644
--- a/src/python-problem/problem/pyabrtmodule.c
+++ b/src/python-problem/problem/pyabrtmodule.c
@@ -24,6 +24,8 @@ static PyMethodDef module_methods[] = {
     /* method_name, func, flags, doc_string */
     /* for include/client.h */
     { "notify_new_path"           , p_notify_new_path         , METH_VARARGS },
+    { "load_conf_file"            , p_load_conf_file          , METH_VARARGS },
+    { "load_plugin_conf_file"     , p_load_plugin_conf_file   , METH_VARARGS },
     { NULL }
 };
 
-- 
1.8.3.1