1
0

Allow caller to handle DBus error messages

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>
This commit is contained in:
Daniel P. Berrange 2014-03-17 17:56:48 +00:00
parent 2c64603366
commit edff187a39
3 changed files with 42 additions and 11 deletions

View File

@ -1388,6 +1388,7 @@ int virDBusCreateReply(DBusMessage **reply,
* @conn: a DBus connection * @conn: a DBus connection
* @call: pointer to a message to send * @call: pointer to a message to send
* @replyout: pointer to receive reply message, or NULL * @replyout: pointer to receive reply message, or NULL
* @error: pointer to receive error message
* *
* This invokes a method encoded in @call on a remote * This invokes a method encoded in @call on a remote
* service on the DBus bus @conn. The optional @replyout * 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 * call. The virDBusMethodReply method can be used to
* decode the return values. * 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 * Returns 0 on success, or -1 upon error
*/ */
int virDBusCall(DBusConnection *conn, int virDBusCall(DBusConnection *conn,
DBusMessage *call, DBusMessage *call,
DBusMessage **replyout) DBusMessage **replyout,
DBusError *error)
{ {
DBusMessage *reply = NULL; DBusMessage *reply = NULL;
DBusError error; DBusError localerror;
int ret = -1; int ret = -1;
dbus_error_init(&error); if (!error)
dbus_error_init(&localerror);
if (!(reply = dbus_connection_send_with_reply_and_block(conn, if (!(reply = dbus_connection_send_with_reply_and_block(conn,
call, call,
VIR_DBUS_METHOD_CALL_TIMEOUT_MILLIS, VIR_DBUS_METHOD_CALL_TIMEOUT_MILLIS,
&error))) { error ? error : &localerror))) {
virReportDBusServiceError(error.message ? error.message : "unknown error", if (error)
error.name); ret = 0;
else
virReportDBusServiceError(localerror.message ? localerror.message : "unknown error",
localerror.name);
goto cleanup; goto cleanup;
} }
ret = 0; ret = 0;
cleanup: cleanup:
dbus_error_free(&error); if (!error)
dbus_error_free(&localerror);
if (reply) { if (reply) {
if (ret == 0 && replyout) if (ret == 0 && replyout)
*replyout = reply; *replyout = reply;
@ -1454,10 +1467,20 @@ int virDBusCall(DBusConnection *conn,
* as variadic args. See virDBusCreateMethodV for a * as variadic args. See virDBusCreateMethodV for a
* description of this parameter. * 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, int virDBusCallMethod(DBusConnection *conn,
DBusMessage **replyout, DBusMessage **replyout,
DBusError *error,
const char *destination, const char *destination,
const char *path, const char *path,
const char *iface, const char *iface,
@ -1477,7 +1500,7 @@ int virDBusCallMethod(DBusConnection *conn,
ret = -1; ret = -1;
ret = virDBusCall(conn, call, replyout); ret = virDBusCall(conn, call, replyout, error);
cleanup: cleanup:
if (call) if (call)
@ -1527,6 +1550,7 @@ static int virDBusIsServiceInList(const char *listMethod, const char *name)
if (virDBusCallMethod(conn, if (virDBusCallMethod(conn,
&reply, &reply,
NULL,
"org.freedesktop.DBus", "org.freedesktop.DBus",
"/org/freedesktop/DBus", "/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, int virDBusCall(DBusConnection *conn ATTRIBUTE_UNUSED,
DBusMessage *call ATTRIBUTE_UNUSED, DBusMessage *call ATTRIBUTE_UNUSED,
DBusMessage **reply ATTRIBUTE_UNUSED) DBusMessage **reply ATTRIBUTE_UNUSED,
DBusError *error ATTRIBUTE_UNUSED)
{ {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("DBus support not compiled into this binary")); "%s", _("DBus support not compiled into this binary"));
@ -1659,6 +1684,7 @@ int virDBusCall(DBusConnection *conn ATTRIBUTE_UNUSED,
int virDBusCallMethod(DBusConnection *conn ATTRIBUTE_UNUSED, int virDBusCallMethod(DBusConnection *conn ATTRIBUTE_UNUSED,
DBusMessage **reply ATTRIBUTE_UNUSED, DBusMessage **reply ATTRIBUTE_UNUSED,
DBusError *error ATTRIBUTE_UNUSED,
const char *destination ATTRIBUTE_UNUSED, const char *destination ATTRIBUTE_UNUSED,
const char *path ATTRIBUTE_UNUSED, const char *path ATTRIBUTE_UNUSED,
const char *iface ATTRIBUTE_UNUSED, const char *iface ATTRIBUTE_UNUSED,

View File

@ -28,6 +28,7 @@
# else # else
# define DBusConnection void # define DBusConnection void
# define DBusMessage void # define DBusMessage void
# define DBusError void
# endif # endif
# include "internal.h" # include "internal.h"
@ -61,6 +62,7 @@ int virDBusCreateReplyV(DBusMessage **reply,
int virDBusCallMethod(DBusConnection *conn, int virDBusCallMethod(DBusConnection *conn,
DBusMessage **reply, DBusMessage **reply,
DBusError *error,
const char *destination, const char *destination,
const char *path, const char *path,
const char *iface, const char *iface,
@ -68,7 +70,8 @@ int virDBusCallMethod(DBusConnection *conn,
const char *types, ...); const char *types, ...);
int virDBusCall(DBusConnection *conn, int virDBusCall(DBusConnection *conn,
DBusMessage *call, DBusMessage *call,
DBusMessage **reply); DBusMessage **reply,
DBusError *error);
int virDBusMessageRead(DBusMessage *msg, int virDBusMessageRead(DBusMessage *msg,
const char *types, ...); const char *types, ...);

View File

@ -235,6 +235,7 @@ int virSystemdCreateMachine(const char *name,
VIR_DEBUG("Attempting to create machine via systemd"); VIR_DEBUG("Attempting to create machine via systemd");
if (virDBusCallMethod(conn, if (virDBusCallMethod(conn,
NULL,
NULL, NULL,
"org.freedesktop.machine1", "org.freedesktop.machine1",
"/org/freedesktop/machine1", "/org/freedesktop/machine1",
@ -300,6 +301,7 @@ int virSystemdTerminateMachine(const char *name,
VIR_DEBUG("Attempting to terminate machine via systemd"); VIR_DEBUG("Attempting to terminate machine via systemd");
if (virDBusCallMethod(conn, if (virDBusCallMethod(conn,
NULL,
NULL, NULL,
"org.freedesktop.machine1", "org.freedesktop.machine1",
"/org/freedesktop/machine1", "/org/freedesktop/machine1",