]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
Allow caller to handle DBus error messages
authorDaniel P. Berrange <berrange@redhat.com>
Mon, 17 Mar 2014 17:56:48 +0000 (17:56 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Fri, 21 Mar 2014 11:26:34 +0000 (11:26 +0000)
The caller may not want all DBus error conditions to be turned
into libvirt errors, so provide a way for the caller to get
back the full DBusError object. They can then check the errors
and only report those that they consider to be fatal.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
src/util/virdbus.c
src/util/virdbus.h
src/util/virsystemd.c

index 2c62301d4d20dd017c3249f4609744e7e0536a4e..e7b5a39c9c68c003cf2a390d9d7c9239717e86a8 100644 (file)
@@ -1388,6 +1388,7 @@ int virDBusCreateReply(DBusMessage **reply,
  * @conn: a DBus connection
  * @call: pointer to a message to send
  * @replyout: pointer to receive reply message, or NULL
+ * @error: pointer to receive error message
  *
  * This invokes a method encoded in @call on a remote
  * service on the DBus bus @conn. The optional @replyout
@@ -1395,31 +1396,43 @@ int virDBusCreateReply(DBusMessage **reply,
  * call. The virDBusMethodReply method can be used to
  * decode the return values.
  *
+ * If @error is NULL then a libvirt error will be raised
+ * when a DBus error is received and the return value will
+ * be -1. If @error is non-NULL then any DBus error will
+ * be saved into that object and the return value will
+ * be 0.
+ *
  * Returns 0 on success, or -1 upon error
  */
 int virDBusCall(DBusConnection *conn,
                 DBusMessage *call,
-                DBusMessage **replyout)
+                DBusMessage **replyout,
+                DBusError *error)
 {
     DBusMessage *reply = NULL;
-    DBusError error;
+    DBusError localerror;
     int ret = -1;
 
-    dbus_error_init(&error);
+    if (!error)
+        dbus_error_init(&localerror);
 
     if (!(reply = dbus_connection_send_with_reply_and_block(conn,
                                                             call,
                                                             VIR_DBUS_METHOD_CALL_TIMEOUT_MILLIS,
-                                                            &error))) {
-        virReportDBusServiceError(error.message ? error.message : "unknown error",
-                                  error.name);
+                                                            error ? error : &localerror))) {
+        if (error)
+            ret = 0;
+        else
+            virReportDBusServiceError(localerror.message ? localerror.message : "unknown error",
+                                      localerror.name);
         goto cleanup;
     }
 
     ret = 0;
 
  cleanup:
-    dbus_error_free(&error);
+    if (!error)
+        dbus_error_free(&localerror);
     if (reply) {
         if (ret == 0 && replyout)
             *replyout = reply;
@@ -1454,10 +1467,20 @@ int virDBusCall(DBusConnection *conn,
  * as variadic args. See virDBusCreateMethodV for a
  * description of this parameter.
  *
- * Returns: 0 on success, -1 on error
+ *
+ * If @error is NULL then a libvirt error will be raised
+ * when a DBus error is received and the return value will
+ * be -1. If @error is non-NULL then any DBus error will
+ * be saved into that object and the return value will
+ * be 0. If an error occurs while encoding method args
+ * the return value will always be -1 regardless of whether
+ * @error is set.
+ *
+ * Returns 0 on success, or -1 upon error
  */
 int virDBusCallMethod(DBusConnection *conn,
                       DBusMessage **replyout,
+                      DBusError *error,
                       const char *destination,
                       const char *path,
                       const char *iface,
@@ -1477,7 +1500,7 @@ int virDBusCallMethod(DBusConnection *conn,
 
     ret = -1;
 
-    ret = virDBusCall(conn, call, replyout);
+    ret = virDBusCall(conn, call, replyout, error);
 
  cleanup:
     if (call)
@@ -1527,6 +1550,7 @@ static int virDBusIsServiceInList(const char *listMethod, const char *name)
 
     if (virDBusCallMethod(conn,
                           &reply,
+                          NULL,
                           "org.freedesktop.DBus",
                           "/org/freedesktop/DBus",
                           "org.freedesktop.DBus",
@@ -1650,7 +1674,8 @@ int virDBusCreateMethodV(DBusMessage **call ATTRIBUTE_UNUSED,
 
 int virDBusCall(DBusConnection *conn ATTRIBUTE_UNUSED,
                 DBusMessage *call ATTRIBUTE_UNUSED,
-                DBusMessage **reply ATTRIBUTE_UNUSED)
+                DBusMessage **reply ATTRIBUTE_UNUSED,
+                DBusError *error ATTRIBUTE_UNUSED)
 {
     virReportError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("DBus support not compiled into this binary"));
@@ -1659,6 +1684,7 @@ int virDBusCall(DBusConnection *conn ATTRIBUTE_UNUSED,
 
 int virDBusCallMethod(DBusConnection *conn ATTRIBUTE_UNUSED,
                       DBusMessage **reply ATTRIBUTE_UNUSED,
+                      DBusError *error ATTRIBUTE_UNUSED,
                       const char *destination ATTRIBUTE_UNUSED,
                       const char *path ATTRIBUTE_UNUSED,
                       const char *iface ATTRIBUTE_UNUSED,
index 4fbda8799b512acf5aa547ff142bbe41e2d0457c..0f2182180fb4d73b549b3f26d92dd2707870760d 100644 (file)
@@ -28,6 +28,7 @@
 # else
 #  define DBusConnection void
 #  define DBusMessage void
+#  define DBusError void
 # endif
 # include "internal.h"
 
@@ -61,6 +62,7 @@ int virDBusCreateReplyV(DBusMessage **reply,
 
 int virDBusCallMethod(DBusConnection *conn,
                       DBusMessage **reply,
+                      DBusError *error,
                       const char *destination,
                       const char *path,
                       const char *iface,
@@ -68,7 +70,8 @@ int virDBusCallMethod(DBusConnection *conn,
                       const char *types, ...);
 int virDBusCall(DBusConnection *conn,
                 DBusMessage *call,
-                DBusMessage **reply);
+                DBusMessage **reply,
+                DBusError *error);
 int virDBusMessageRead(DBusMessage *msg,
                        const char *types, ...);
 
index f2eeb8cee346458973856ac7168975c0d80091ef..0281158a1964224011ae778a87921a7ca15733f8 100644 (file)
@@ -235,6 +235,7 @@ int virSystemdCreateMachine(const char *name,
 
     VIR_DEBUG("Attempting to create machine via systemd");
     if (virDBusCallMethod(conn,
+                          NULL,
                           NULL,
                           "org.freedesktop.machine1",
                           "/org/freedesktop/machine1",
@@ -300,6 +301,7 @@ int virSystemdTerminateMachine(const char *name,
 
     VIR_DEBUG("Attempting to terminate machine via systemd");
     if (virDBusCallMethod(conn,
+                          NULL,
                           NULL,
                           "org.freedesktop.machine1",
                           "/org/freedesktop/machine1",