Blob Blame History Raw
From b1d43d73cce12980017dc5fa773d88ef72ec432b Mon Sep 17 00:00:00 2001
From: Jakub Filak <jfilak@redhat.com>
Date: Wed, 8 Apr 2015 08:23:03 +0200
Subject: [PATCH] applet: switch to D-Bus methods

This patch is a part of our efforts to make abrt-applet independent on
the backend.

This patch converts all data manipulation functions to D-Bus calls, so
the notifications are made of data obtained through D-Bus.

The reporting still relies on file system access, though.

Signed-off-by: Jakub Filak <jfilak@redhat.com>
---
 src/applet/applet.c        | 130 ++++++++++++++++++++++++++-------------------
 src/include/libabrt.h      |  16 ++++++
 src/lib/problem_api_dbus.c |  61 +++++++++++++++------
 3 files changed, 137 insertions(+), 70 deletions(-)

diff --git a/src/applet/applet.c b/src/applet/applet.c
index 3198ae5..6474f6f 100644
--- a/src/applet/applet.c
+++ b/src/applet/applet.c
@@ -170,11 +170,6 @@ static bool is_silent_shortened_reporting_enabled(void)
     return get_configured_bool_or_default("SilentShortenedReporting", 0);
 }
 
-static bool is_notification_of_incomplete_problems_enabled(void)
-{
-    return get_configured_bool_or_default("NotifyIncompleteProblems", 0);
-}
-
 /*
  * Converts a NM state value stored in GVariant to boolean.
  *
@@ -307,6 +302,7 @@ typedef struct problem_info {
     bool reported;
     bool was_announced;
     bool is_writable;
+    int time;
 } problem_info_t;
 
 static void push_to_deferred_queue(problem_info_t *pi)
@@ -319,6 +315,26 @@ static const char *problem_info_get_dir(problem_info_t *pi)
     return problem_data_get_content_or_NULL(pi->problem_data, CD_DUMPDIR);
 }
 
+static int problem_info_get_time(problem_info_t *pi)
+{
+    if (pi->time == -1)
+    {
+        const char *time_str = problem_data_get_content_or_NULL(pi->problem_data, FILENAME_TIME);
+
+        if (time_str == NULL)
+            error_msg_and_die("BUG: Problem info has data without the element time");
+
+        pi->time = atoi(time_str);
+    }
+
+    return pi->time;
+}
+
+static bool problem_info_is_reported(problem_info_t *pi)
+{
+    return problem_data_get_content_or_NULL(pi->problem_data, FILENAME_REPORTED_TO) != NULL;
+}
+
 static void problem_info_set_dir(problem_info_t *pi, const char *dir)
 {
     problem_data_add_text_noteditable(pi->problem_data, CD_DUMPDIR, dir);
@@ -358,6 +374,7 @@ static problem_info_t *problem_info_new(const char *dir)
 {
     problem_info_t *pi = xzalloc(sizeof(*pi));
     pi->problem_data = problem_data_new();
+    pi->time = -1;
     problem_info_set_dir(pi, dir);
     return pi;
 }
@@ -1245,6 +1262,7 @@ static void show_problem_notification(problem_info_t *pi, int flags)
 static void Crash(DBusMessage* signal)
 {
     log_debug("Crash recorded");
+
     DBusMessageIter in_iter;
     dbus_message_iter_init(signal, &in_iter);
 
@@ -1316,12 +1334,20 @@ static void Crash(DBusMessage* signal)
 
 
     problem_info_t *pi = problem_info_new(dir);
-    if (uuid != NULL && uuid[0] != '\0')
-        problem_data_add_text_noteditable(pi->problem_data, FILENAME_UUID, uuid);
-    if (duphash != NULL && duphash[0] != '\0')
-        problem_data_add_text_noteditable(pi->problem_data, FILENAME_DUPHASH, duphash);
-    if (package_name != NULL && package_name[0] != '\0')
-        problem_data_add_text_noteditable(pi->problem_data, FILENAME_COMPONENT, package_name);
+
+    static const char *elements[] = {
+        FILENAME_CMDLINE,
+        FILENAME_COUNT,
+        FILENAME_UUID,
+        FILENAME_DUPHASH,
+        FILENAME_COMPONENT,
+        FILENAME_ENVIRON,
+        FILENAME_PID,
+        NULL,
+    };
+
+    fill_problem_data_over_dbus(dir, elements, pi->problem_data);
+
     pi->foreign = foreign_problem;
     show_problem_notification(pi, flags);
 }
@@ -1558,6 +1584,20 @@ xsmp_client_connect(void)
 
 int main(int argc, char** argv)
 {
+    static const char *elements[] = {
+        FILENAME_CMDLINE,
+        FILENAME_COUNT,
+        FILENAME_UUID,
+        FILENAME_DUPHASH,
+        FILENAME_COMPONENT,
+        FILENAME_UID,
+        FILENAME_TIME,
+        FILENAME_REPORTED_TO,
+        FILENAME_NOT_REPORTABLE,
+        NULL
+    };
+
+
     /* I18n */
     setlocale(LC_ALL, "");
 #if ENABLE_NLS
@@ -1675,63 +1715,45 @@ int main(int argc, char** argv)
     /* Age limit = now - 3 days */
     const unsigned long min_born_time = (unsigned long)(time_before_ndays(3));
 
-    const bool notify_incomplete = is_notification_of_incomplete_problems_enabled();
-
-    while (new_dirs)
+    for ( ; new_dirs != NULL; new_dirs = g_list_next(new_dirs))
     {
-        struct dump_dir *dd = dd_opendir((char *)new_dirs->data, DD_OPEN_READONLY);
-        if (dd == NULL)
+        const char *problem_id = (const char *)new_dirs->data;
+        problem_info_t *pi = problem_info_new(problem_id);
+
+        if (fill_problem_data_over_dbus(problem_id, elements, pi->problem_data) != 0)
         {
-            log_notice("'%s' is not a dump dir - ignoring\n", (char *)new_dirs->data);
-            new_dirs = g_list_next(new_dirs);
+            log_notice("'%s' is not a dump dir - ignoring\n", problem_id);
+            problem_info_free(pi);
             continue;
         }
 
-        if (dd->dd_time < min_born_time)
+        /* TODO: add a filter for only complete problems to GetProblems D-Bus method */
+        if (!dbus_problem_is_complete(problem_id))
         {
-            log_notice("Ignoring outdated problem '%s'", (char *)new_dirs->data);
-            goto next;
+            log_notice("Ignoring incomplete problem '%s'", problem_id);
+            problem_info_free(pi);
+            continue;
         }
 
-        const bool incomplete = !problem_dump_dir_is_complete(dd);
-        if (!notify_incomplete && incomplete)
+        /* TODO: add a filter for max-old reported problems to GetProblems D-Bus method */
+        if (problem_info_get_time(pi) < min_born_time)
         {
-            log_notice("Ignoring incomplete problem '%s'", (char *)new_dirs->data);
-            goto next;
+            log_notice("Ignoring outdated problem '%s'", problem_id);
+            problem_info_free(pi);
+            continue;
         }
 
-        if (!dd_exist(dd, FILENAME_REPORTED_TO))
-        {
-            problem_info_t *pi = problem_info_new(new_dirs->data);
-            const char *elements[] = {FILENAME_UUID, FILENAME_DUPHASH, FILENAME_COMPONENT, FILENAME_NOT_REPORTABLE};
-
-            for (size_t i = 0; i < sizeof(elements)/sizeof(*elements); ++i)
-            {
-                char * const value = dd_load_text_ext(dd, elements[i],
-                        DD_FAIL_QUIETLY_ENOENT | DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE);
-                if (value)
-                    problem_data_add_text_noteditable(pi->problem_data, elements[i], value);
-                free(value);
-            }
-
-            pi->incomplete = incomplete;
-
-            /* Can't be foreign because if the problem is foreign then the
-             * dd_opendir() call failed few lines above and the problem is ignored.
-             * */
-            pi->foreign = false;
-
-            notify_list = g_list_prepend(notify_list, pi);
-        }
-        else
+        /* TODO: add a filter for not-yet reported problems to GetProblems D-Bus method */
+        if (problem_info_is_reported(pi))
         {
-            log_notice("Ignoring already reported problem '%s'", (char *)new_dirs->data);
+            log_notice("Ignoring already reported problem '%s'", problem_id);
+            problem_info_free(pi);
+            continue;
         }
 
-next:
-        dd_close(dd);
-
-        new_dirs = g_list_next(new_dirs);
+        /* Can't be foreig because new_dir_exists() returns only own problems */
+        pi->foreign = false;
+        notify_list = g_list_prepend(notify_list, pi);
     }
 
     g_ignore_set = ignored_problems_new(concat_path_file(g_get_user_cache_dir(), "abrt/ignored_problems"));
diff --git a/src/include/libabrt.h b/src/include/libabrt.h
index 0171cb7..65d30a1 100644
--- a/src/include/libabrt.h
+++ b/src/include/libabrt.h
@@ -162,6 +162,15 @@ int chown_dir_over_dbus(const char *problem_dir_path);
 int test_exist_over_dbus(const char *problem_id, const char *element_name);
 
 /**
+  @brief Checks whether the problem corresponding to the given ID is complete
+
+  Might require authorization
+
+  @return Positive number if such the proble is complete, 0 if doesn't and negative number if an error occurs.
+ */
+int dbus_problem_is_complete(const char *problem_id);
+
+/**
   @ Returns value of the given element name
 
   Might require authorization
@@ -180,6 +189,13 @@ char *load_text_over_dbus(const char *problem_id, const char *element_name);
 int delete_problem_dirs_over_dbus(const GList *problem_dir_paths);
 
 /**
+  @brief Fetches given problem elements for specified problem id
+
+  @return on failures returns non zero value and emits error message
+*/
+int fill_problem_data_over_dbus(const char *problem_dir_path, const char **elements, problem_data_t *problem_data);
+
+/**
   @brief Fetches problem information for specified problem id
 
   @return problem_data_t or NULL on failure
diff --git a/src/lib/problem_api_dbus.c b/src/lib/problem_api_dbus.c
index 5148932..ce5c47b 100644
--- a/src/lib/problem_api_dbus.c
+++ b/src/lib/problem_api_dbus.c
@@ -101,23 +101,21 @@ int delete_problem_dirs_over_dbus(const GList *problem_dir_paths)
     return 0;
 }
 
-problem_data_t *get_problem_data_dbus(const char *problem_dir_path)
+int fill_problem_data_over_dbus(const char *problem_id, const char **elements, problem_data_t *problem_data)
 {
     INITIALIZE_LIBABRT();
 
     GDBusProxy *proxy = get_dbus_proxy();
     if (!proxy)
-        return NULL;
+        return -1;
 
-    GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
-    g_variant_builder_add(builder, "s", FILENAME_TIME          );
-    g_variant_builder_add(builder, "s", FILENAME_REASON        );
-    g_variant_builder_add(builder, "s", FILENAME_NOT_REPORTABLE);
-    g_variant_builder_add(builder, "s", FILENAME_COMPONENT     );
-    g_variant_builder_add(builder, "s", FILENAME_EXECUTABLE    );
-    g_variant_builder_add(builder, "s", FILENAME_REPORTED_TO   );
-    GVariant *params = g_variant_new("(sas)", problem_dir_path, builder);
-    g_variant_builder_unref(builder);
+    GVariantBuilder *args_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
+
+    for (const char **iter = elements; *iter; ++iter)
+        g_variant_builder_add(args_builder, "s", *iter);
+
+    GVariant *params = g_variant_new("(sas)", problem_id, args_builder);
+    g_variant_builder_unref(args_builder);
 
     GError *error = NULL;
     GVariant *result = g_dbus_proxy_call_sync(proxy,
@@ -130,20 +128,46 @@ problem_data_t *get_problem_data_dbus(const char *problem_dir_path)
 
     if (error)
     {
-        error_msg(_("Can't get problem data from abrt-dbus: %s"), error->message);
+        error_msg(_("D-Bus GetInfo method call failed: %s"), error->message);
         g_error_free(error);
-        return NULL;
+        return -2;
     }
 
-    problem_data_t *pd = problem_data_new();
+
     char *key, *val;
     GVariantIter *iter;
     g_variant_get(result, "(a{ss})", &iter);
     while (g_variant_iter_loop(iter, "{ss}", &key, &val))
+        problem_data_add_text_noteditable(problem_data, key, val);
+
+    g_variant_unref(result);
+
+    return 0;
+}
+
+problem_data_t *get_problem_data_dbus(const char *problem_dir_path)
+{
+    INITIALIZE_LIBABRT();
+
+    static const char *elements[] = {
+        FILENAME_TIME,
+        FILENAME_REASON,
+        FILENAME_NOT_REPORTABLE,
+        FILENAME_COMPONENT,
+        FILENAME_EXECUTABLE,
+        FILENAME_REPORTED_TO,
+        NULL,
+    };
+
+    problem_data_t *pd = problem_data_new();
+
+    if (fill_problem_data_over_dbus(problem_dir_path, elements, pd) != 0)
     {
-        problem_data_add_text_noteditable(pd, key, val);
+        error_msg(_("Can't get problem data from abrt-dbus"));
+        problem_data_free(pd);
+        return NULL;
     }
-    g_variant_unref(result);
+
     return pd;
 }
 
@@ -259,6 +283,11 @@ int test_exist_over_dbus(const char *problem_id, const char *element_name)
     return retval;
 }
 
+int dbus_problem_is_complete(const char *problem_id)
+{
+    return test_exist_over_dbus(problem_id, FILENAME_COUNT);
+}
+
 char *load_text_over_dbus(const char *problem_id, const char *element_name)
 {
     INITIALIZE_LIBABRT();
-- 
2.1.0