From: Marc-André Lureau <marcandre.lureau@xxxxxxxxxx> Learn to check presence of services on a given connection. Signed-off-by: Marc-André Lureau <marcandre.lureau@xxxxxxxxxx> --- src/util/virdbus.c | 65 +++++++++++++++++++++++++++++++++++----------- src/util/virdbus.h | 3 +++ 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/util/virdbus.c b/src/util/virdbus.c index 858291e2ba..03ed8fd87a 100644 --- a/src/util/virdbus.c +++ b/src/util/virdbus.c @@ -1650,19 +1650,14 @@ int virDBusCallMethod(DBusConnection *conn, } -static int virDBusIsServiceInList(const char *listMethod, const char *name) +static int virDBusIsServiceInList(DBusConnection *conn, + const char *listMethod, + const char *name) { - DBusConnection *conn; DBusMessage *reply = NULL; DBusMessageIter iter, sub; int ret = -1; - if (!virDBusHasSystemBus()) - return -2; - - if (!(conn = virDBusGetSystemBus())) - return -1; - if (virDBusCallMethod(conn, &reply, NULL, @@ -1699,6 +1694,38 @@ static int virDBusIsServiceInList(const char *listMethod, const char *name) return ret; } +/** + * virDBusIsServiceEnabled: + * @conn: a DBus connection + * @name: a service name + * + * Returns 0 if service is available, -1 on fatal error, or -2 if service is not available + */ +int virDBusIsServiceEnabled(DBusConnection *conn, const char *name) +{ + int ret = virDBusIsServiceInList(conn, "ListActivatableNames", name); + + VIR_DEBUG("Service %s is %s", name, ret ? "unavailable" : "available"); + + return ret; +} + +/** + * virDBusIsServiceRegistered: + * @conn: a DBus connection + * @name: a service name + * + * Returns 0 if service is registered, -1 on fatal error, or -2 if service is not available + */ +int virDBusIsServiceRegistered(DBusConnection *conn, const char *name) +{ + int ret = virDBusIsServiceInList(conn, "ListNames", name); + + VIR_DEBUG("Service %s is %s", name, ret ? "not registered" : "registered"); + + return ret; +} + /** * virDBusSystemIsServiceEnabled: * @name: service name @@ -1707,26 +1734,34 @@ static int virDBusIsServiceInList(const char *listMethod, const char *name) */ int virDBusSystemIsServiceEnabled(const char *name) { - int ret = virDBusIsServiceInList("ListActivatableNames", name); + DBusConnection *conn; - VIR_DEBUG("Service %s is %s", name, ret ? "unavailable" : "available"); + if (!virDBusHasSystemBus()) + return -2; - return ret; + if (!(conn = virDBusGetSystemBus())) + return -1; + + return virDBusIsServiceEnabled(conn, name); } /** - * virDBusSystemIsServiceRegistered + * virDBusSystemIsServiceRegistered: * @name: service name * * Returns 0 if service is registered, -1 on fatal error, or -2 if service is not registered */ int virDBusSystemIsServiceRegistered(const char *name) { - int ret = virDBusIsServiceInList("ListNames", name); + DBusConnection *conn; - VIR_DEBUG("Service %s is %s", name, ret ? "not registered" : "registered"); + if (!virDBusHasSystemBus()) + return -2; - return ret; + if (!(conn = virDBusGetSystemBus())) + return -1; + + return virDBusIsServiceRegistered(conn, name); } void virDBusMessageUnref(DBusMessage *msg) diff --git a/src/util/virdbus.h b/src/util/virdbus.h index b3b0260a4e..2b2d273b1b 100644 --- a/src/util/virdbus.h +++ b/src/util/virdbus.h @@ -70,6 +70,9 @@ int virDBusMessageDecode(DBusMessage *msg, const char *types, ...); void virDBusMessageUnref(DBusMessage *msg); +int virDBusIsServiceEnabled(DBusConnection *conn, const char *name); +int virDBusIsServiceRegistered(DBusConnection *conn, const char *name); + int virDBusSystemIsServiceEnabled(const char *name); int virDBusSystemIsServiceRegistered(const char *name); -- 2.26.0.rc2.42.g98cedd0233