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