commit 5be9e5b63815cb632b4944f00dd67f6f9dcbff70
Author: Andrew Beekhof <andrew@beekhof.net>
Date: Mon Feb 3 14:43:38 2014 +1100
Refactor: systemd: Simplify dbus API usage
(cherry picked from commit e3aa2f12bb4f00d4b0d8a38f1dd5727de2eba351)
diff --git a/lib/services/dbus.c b/lib/services/dbus.c
index a3286f2..a9b1eba 100644
--- a/lib/services/dbus.c
+++ b/lib/services/dbus.c
@@ -179,7 +179,22 @@ bool pcmk_dbus_send(DBusMessage *msg, DBusConnection *connection,
bool pcmk_dbus_type_check(DBusMessage *msg, DBusMessageIter *field, int expected, const char *function, int line)
{
- int dtype = dbus_message_iter_get_arg_type(field);
+ int dtype = 0;
+ DBusMessageIter lfield;
+
+ if(field == NULL) {
+ if(dbus_message_iter_init(msg, &lfield)) {
+ field = &lfield;
+ }
+ }
+
+ if(field == NULL) {
+ do_crm_log_alias(LOG_ERR, __FILE__, function, line,
+ "Empty parameter list in reply expecting '%c'", expected);
+ return FALSE;
+ }
+
+ dtype = dbus_message_iter_get_arg_type(field);
if(dtype != expected) {
DBusMessageIter args;
diff --git a/lib/services/systemd.c b/lib/services/systemd.c
index a06d547..b461c5f 100644
--- a/lib/services/systemd.c
+++ b/lib/services/systemd.c
@@ -119,7 +119,6 @@ static gboolean
systemd_unit_by_name(const gchar * arg_name, gchar ** out_unit)
{
DBusMessage *msg;
- DBusMessageIter args;
DBusMessage *reply = NULL;
const char *method = "GetUnit";
char *name = NULL;
@@ -137,6 +136,10 @@ systemd_unit_by_name(const gchar * arg_name, gchar ** out_unit)
</method>
*/
+ if (systemd_init() == FALSE) {
+ return FALSE;
+ }
+
name = systemd_service_name(arg_name);
while(*out_unit == NULL) {
@@ -152,17 +155,19 @@ systemd_unit_by_name(const gchar * arg_name, gchar ** out_unit)
if(error.name) {
crm_info("Call to %s failed: %s", method, error.name);
- } else if (dbus_message_iter_init(reply, &args)) {
+ } else if(pcmk_dbus_type_check(reply, NULL, DBUS_TYPE_OBJECT_PATH, __FUNCTION__, __LINE__)) {
+ if(out_unit) {
+ char *path = NULL;
- if(pcmk_dbus_type_check(reply, &args, DBUS_TYPE_OBJECT_PATH, __FUNCTION__, __LINE__)) {
- DBusBasicValue value;
+ dbus_message_get_args (reply, NULL,
+ DBUS_TYPE_OBJECT_PATH, &path,
+ DBUS_TYPE_INVALID);
- dbus_message_iter_get_basic(&args, &value);
- *out_unit = strdup(value.str);
- dbus_message_unref(reply);
- free(name);
- return TRUE;
+ *out_unit = strdup(path);
}
+ dbus_message_unref(reply);
+ free(name);
+ return TRUE;
}
if(strcmp(method, "LoadUnit") != 0) {
@@ -171,6 +176,7 @@ systemd_unit_by_name(const gchar * arg_name, gchar ** out_unit)
systemd_daemon_reload();
if(reply) {
dbus_message_unref(reply);
+ reply = NULL;
}
} else {
@@ -264,20 +270,7 @@ systemd_unit_listall(void)
gboolean
systemd_unit_exists(const char *name)
{
- char *path = NULL;
- gboolean pass = FALSE;
-
- if (systemd_init() == FALSE) {
- return FALSE;
- }
-
- if(systemd_unit_by_name(name, &path) && path) {
- crm_trace("Got %s", path);
- pass = TRUE;
- }
-
- free(path);
- return pass;
+ return systemd_unit_by_name(name, NULL);
}
static char *
@@ -360,12 +353,7 @@ systemd_async_dispatch(DBusPendingCall *pending, void *user_data)
}
} else {
- DBusMessageIter args;
-
- if(!dbus_message_iter_init(reply, &args)) {
- crm_err("Call to %s failed: no arguments", op->action);
-
- } else if(!pcmk_dbus_type_check(reply, &args, DBUS_TYPE_OBJECT_PATH, __FUNCTION__, __LINE__)) {
+ if(!pcmk_dbus_type_check(reply, NULL, DBUS_TYPE_OBJECT_PATH, __FUNCTION__, __LINE__)) {
crm_warn("Call to %s passed but return type was unexpected", op->action);
op->rc = PCMK_OCF_OK;
@@ -403,7 +391,6 @@ systemd_unit_exec(svc_action_t * op, gboolean synchronous)
char *name = systemd_service_name(op->agent);
DBusMessage *msg = NULL;
DBusMessage *reply = NULL;
- DBusMessageIter args;
dbus_error_init(&error);
op->rc = PCMK_OCF_UNKNOWN_ERROR;
@@ -499,6 +486,7 @@ systemd_unit_exec(svc_action_t * op, gboolean synchronous)
return pcmk_dbus_send(msg, systemd_proxy, systemd_async_dispatch, op);
}
+ dbus_error_init(&error);
reply = pcmk_dbus_send_recv(msg, systemd_proxy, &error);
if(error.name) {
@@ -508,11 +496,7 @@ systemd_unit_exec(svc_action_t * op, gboolean synchronous)
}
goto cleanup;
- } else if(!dbus_message_iter_init(reply, &args)) {
- crm_err("Call to %s failed: no arguments", method);
- goto cleanup;
-
- } else if(!pcmk_dbus_type_check(reply, &args, DBUS_TYPE_OBJECT_PATH, __FUNCTION__, __LINE__)) {
+ } else if(!pcmk_dbus_type_check(reply, NULL, DBUS_TYPE_OBJECT_PATH, __FUNCTION__, __LINE__)) {
crm_warn("Call to %s passed but return type was unexpected", op->action);
op->rc = PCMK_OCF_OK;