summaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorDenys Vlasenko <dvlasenk@redhat.com>2011-02-10 18:04:43 +0100
committerDenys Vlasenko <dvlasenk@redhat.com>2011-02-10 18:04:43 +0100
commit2abf0fc078221715abbd20c8451d300eaf787848 (patch)
tree1dc278a78fda61ad68bcb889fd3f8c94b532900b /src/lib
parent394508ab25eb2a78a40cde19714e9afa2537c393 (diff)
downloadabrt-2abf0fc078221715abbd20c8451d300eaf787848.tar.gz
abrt-2abf0fc078221715abbd20c8451d300eaf787848.tar.xz
abrt-2abf0fc078221715abbd20c8451d300eaf787848.zip
abrt-gtk: make Delete key actually delete the dump dir
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/abrt_dbus.c177
-rw-r--r--src/lib/abrt_dbus.h23
2 files changed, 198 insertions, 2 deletions
diff --git a/src/lib/abrt_dbus.c b/src/lib/abrt_dbus.c
index d1717636..28c2d0da 100644
--- a/src/lib/abrt_dbus.c
+++ b/src/lib/abrt_dbus.c
@@ -598,8 +598,29 @@ static void unregister_vtable(DBusConnection *conn, void* data)
VERB3 log("%s()", __func__);
}
+
+/*
+ * Simple logging handler for dbus errors.
+ */
+int log_dbus_error(const char *msg, DBusError *err)
+{
+ int ret = 0;
+ if (dbus_error_is_set(err))
+ {
+ error_msg("dbus error: %s", err->message);
+ ret = 1;
+ }
+ if (msg)
+ {
+ error_msg(msg);
+ ret = 1;
+ }
+ return ret;
+}
+
+
/*
- * Initialization works as follows:
+ * Initialization. Works as follows:
*
* we have a DBusConnection* (say, obtained with dbus_bus_get)
* we call dbus_connection_set_watch_functions
@@ -673,3 +694,157 @@ void attach_dbus_conn_to_glib_main_loop(DBusConnection* conn,
}
}
}
+
+
+/*
+ * Support functions for clients
+ */
+
+/* helpers */
+static DBusMessage* new_call_msg(const char* method)
+{
+ DBusMessage* msg = dbus_message_new_method_call(ABRTD_DBUS_NAME, ABRTD_DBUS_PATH, ABRTD_DBUS_IFACE, method);
+ if (!msg)
+ die_out_of_memory();
+ return msg;
+}
+
+static DBusMessage* send_get_reply_and_unref(DBusMessage* msg)
+{
+ dbus_uint32_t serial;
+ if (TRUE != dbus_connection_send(g_dbus_conn, msg, &serial))
+ error_msg_and_die("Error sending DBus message");
+ dbus_message_unref(msg);
+
+ while (true)
+ {
+ DBusMessage *received = dbus_connection_pop_message(g_dbus_conn);
+ if (!received)
+ {
+ if (FALSE == dbus_connection_read_write(g_dbus_conn, -1))
+ error_msg_and_die("dbus connection closed");
+ continue;
+ }
+
+ int tp = dbus_message_get_type(received);
+ const char *error_str = dbus_message_get_error_name(received);
+#if 0
+ /* Debugging */
+ printf("type:%u (CALL:%u, RETURN:%u, ERROR:%u, SIGNAL:%u)\n", tp,
+ DBUS_MESSAGE_TYPE_METHOD_CALL,
+ DBUS_MESSAGE_TYPE_METHOD_RETURN,
+ DBUS_MESSAGE_TYPE_ERROR,
+ DBUS_MESSAGE_TYPE_SIGNAL
+ );
+ const char *sender = dbus_message_get_sender(received);
+ if (sender)
+ printf("sender: %s\n", sender);
+ const char *path = dbus_message_get_path(received);
+ if (path)
+ printf("path: %s\n", path);
+ const char *member = dbus_message_get_member(received);
+ if (member)
+ printf("member: %s\n", member);
+ const char *interface = dbus_message_get_interface(received);
+ if (interface)
+ printf("interface: %s\n", interface);
+ const char *destination = dbus_message_get_destination(received);
+ if (destination)
+ printf("destination: %s\n", destination);
+ if (error_str)
+ printf("error: '%s'\n", error_str);
+#endif
+
+ DBusError err;
+ dbus_error_init(&err);
+
+ if (dbus_message_is_signal(received, ABRTD_DBUS_IFACE, "Update"))
+ {
+ const char *update_msg;
+ if (!dbus_message_get_args(received, &err,
+ DBUS_TYPE_STRING, &update_msg,
+ DBUS_TYPE_INVALID))
+ {
+ error_msg_and_die("dbus Update message: arguments mismatch");
+ }
+ printf(">> %s\n", update_msg);
+ }
+ else if (dbus_message_is_signal(received, ABRTD_DBUS_IFACE, "Warning"))
+ {
+ const char *warning_msg;
+ if (!dbus_message_get_args(received, &err,
+ DBUS_TYPE_STRING, &warning_msg,
+ DBUS_TYPE_INVALID))
+ {
+ error_msg_and_die("dbus Warning message: arguments mismatch");
+ }
+ log(">! %s\n", warning_msg);
+ }
+ else
+ if (tp == DBUS_MESSAGE_TYPE_METHOD_RETURN
+ && dbus_message_get_reply_serial(received) == serial
+ ) {
+ return received;
+ }
+ else
+ if (tp == DBUS_MESSAGE_TYPE_ERROR
+ && dbus_message_get_reply_serial(received) == serial
+ ) {
+ error_msg_and_die("dbus call returned error: '%s'", error_str);
+ }
+
+ dbus_message_unref(received);
+ }
+}
+
+int32_t call_DeleteDebugDump(const char *dump_dir_name)
+{
+ DBusMessage* msg = new_call_msg(__func__ + 5);
+ dbus_message_append_args(msg,
+ DBUS_TYPE_STRING, &dump_dir_name,
+ DBUS_TYPE_INVALID);
+
+ DBusMessage *reply = send_get_reply_and_unref(msg);
+
+ DBusMessageIter in_iter;
+ dbus_message_iter_init(reply, &in_iter);
+
+ int32_t result;
+ int r = load_int32(&in_iter, &result);
+ if (r != ABRT_DBUS_LAST_FIELD) /* more values present, or bad type */
+ error_msg_and_die("dbus call %s: return type mismatch", __func__ + 5);
+
+ dbus_message_unref(reply);
+ return result;
+}
+
+int connect_to_abrtd_and_call_DeleteDebugDump(const char *dump_dir_name)
+{
+ DBusError err;
+ dbus_error_init(&err);
+ g_dbus_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
+ if (log_dbus_error(
+ g_dbus_conn ? NULL :
+ "error requesting system DBus, possible reasons: "
+ "dbus config is incorrect; dbus-daemon is not running, "
+ "or dbus daemon needs to be restarted to reload dbus config",
+ &err
+ )
+ ) {
+ if (g_dbus_conn)
+ dbus_connection_unref(g_dbus_conn);
+ g_dbus_conn = NULL;
+ return 1;
+ }
+
+ int ret = call_DeleteDebugDump(dump_dir_name);
+ if (ret == ENOENT)
+ error_msg("Dump directory '%s' is not found", dump_dir_name);
+ else if (ret != 0)
+ error_msg("Can't delete dump directory '%s'", dump_dir_name);
+
+ dbus_connection_unref(g_dbus_conn);
+ g_dbus_conn = NULL;
+
+ return ret;
+}
diff --git a/src/lib/abrt_dbus.h b/src/lib/abrt_dbus.h
index 09d69b39..dd07cb0c 100644
--- a/src/lib/abrt_dbus.h
+++ b/src/lib/abrt_dbus.h
@@ -23,6 +23,11 @@
#include "abrtlib.h"
+#define ABRTD_DBUS_NAME "com.redhat.abrt"
+#define ABRTD_DBUS_PATH "/com/redhat/abrt"
+#define ABRTD_DBUS_IFACE "com.redhat.abrt"
+
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -53,6 +58,7 @@ extern DBusConnection* g_dbus_conn;
* // (note: "iface.sig.emitted.from" is not optional for signals!)
* dbus_message_set_destination(msg, "peer"); // optional
* dbus_connection_send(conn, msg, &serial); // &serial can be NULL
+ * dbus_connection_unref(conn); // if you don't want to *stay* connected
*
* - client which receives and processes signals:
* conn = dbus_bus_get(DBUS_BUS_SYSTEM/SESSION, &err);
@@ -73,6 +79,18 @@ void attach_dbus_conn_to_glib_main_loop(DBusConnection* conn,
DBusHandlerResult (*message_received_func)(DBusConnection *conn, DBusMessage *msg, void* data)
);
+/* Log dbus error if err has it set. Then log msg if it's !NULL.
+ * In both cases return 1. Otherwise return 0.
+ */
+int log_dbus_error(const char *msg, DBusError *err);
+
+/* Perform "DeleteDebugDump" call over g_dbus_conn */
+int32_t call_DeleteDebugDump(const char *dump_dir_name);
+
+/* Connect to system bus, find abrtd, perform "DeleteDebugDump" call, close g_dbus_conn */
+int connect_to_abrtd_and_call_DeleteDebugDump(const char *dump_dir_name);
+
+
/*
* Helpers for building DBus messages
*/
@@ -114,7 +132,10 @@ int load_vector_of_crash_data(DBusMessageIter* iter, vector_of_crash_data_t **va
#endif
-/* C++ style stuff */
+/*
+ * C++ style stuff
+ */
+
#ifdef __cplusplus
#include <map>