43fe83
From 351d57a6576d6026ac0e3807030b07e877e5de07 Mon Sep 17 00:00:00 2001
43fe83
Message-Id: <351d57a6576d6026ac0e3807030b07e877e5de07.1381871412.git.jdenemar@redhat.com>
43fe83
From: Gao feng <gaofeng@cn.fujitsu.com>
43fe83
Date: Mon, 14 Oct 2013 16:45:13 +0100
43fe83
Subject: [PATCH] DBus: introduce virDBusIsServiceEnabled
43fe83
43fe83
For
43fe83
43fe83
  https://bugzilla.redhat.com/show_bug.cgi?id=1018730
43fe83
43fe83
This patch introduces virDBusIsServiceEnabled, we can use
43fe83
this method to get if the service is supported.
43fe83
43fe83
In one case, if org.freedesktop.machine1 is unavailable on
43fe83
host, we should skip creating machine through systemd.
43fe83
43fe83
Signed-off-by: Gao feng <gaofeng@cn.fujitsu.com>
43fe83
(cherry picked from commit 7ada155cdf2bbfac16ce08f64abb455a940e2cf7)
43fe83
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
43fe83
---
43fe83
 src/util/virdbus.c    | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++
43fe83
 src/util/virdbus.h    |  1 +
43fe83
 src/util/virsystemd.c | 17 +++++---------
43fe83
 3 files changed, 68 insertions(+), 11 deletions(-)
43fe83
43fe83
diff --git a/src/util/virdbus.c b/src/util/virdbus.c
43fe83
index 62c31be..a2c4b4e 100644
43fe83
--- a/src/util/virdbus.c
43fe83
+++ b/src/util/virdbus.c
43fe83
@@ -1207,6 +1207,61 @@ int virDBusMessageRead(DBusMessage *msg,
43fe83
     return ret;
43fe83
 }
43fe83
 
43fe83
+/**
43fe83
+ * virDBusIsServiceEnabled:
43fe83
+ * @name: service name
43fe83
+ *
43fe83
+ * Retruns 0 if service is available, -1 on fatal error, or -2 if service is not available
43fe83
+ */
43fe83
+int virDBusIsServiceEnabled(const char *name)
43fe83
+{
43fe83
+    DBusConnection *conn;
43fe83
+    DBusMessage *reply = NULL;
43fe83
+    DBusMessageIter iter, sub;
43fe83
+    int ret = -1;
43fe83
+
43fe83
+    if (!virDBusHasSystemBus())
43fe83
+        return -2;
43fe83
+
43fe83
+    conn = virDBusGetSystemBus();
43fe83
+
43fe83
+    if (virDBusCallMethod(conn,
43fe83
+                          &reply,
43fe83
+                          "org.freedesktop.DBus",
43fe83
+                          "/org/freedesktop/DBus",
43fe83
+                          "org.freedesktop.DBus",
43fe83
+                          "ListActivatableNames",
43fe83
+                          DBUS_TYPE_INVALID) < 0)
43fe83
+        return ret;
43fe83
+
43fe83
+    if (!dbus_message_iter_init(reply, &iter) ||
43fe83
+        dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
43fe83
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
43fe83
+                       _("Reply message incorrect"));
43fe83
+        goto cleanup;
43fe83
+    }
43fe83
+
43fe83
+    ret = -2;
43fe83
+    dbus_message_iter_recurse(&iter, &sub);
43fe83
+    while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING) {
43fe83
+        const char *service = NULL;
43fe83
+
43fe83
+        dbus_message_iter_get_basic(&sub, &service);
43fe83
+        dbus_message_iter_next(&sub);
43fe83
+
43fe83
+        if (STREQ(service, name)) {
43fe83
+            ret = 0;
43fe83
+            break;
43fe83
+        }
43fe83
+    }
43fe83
+
43fe83
+    VIR_DEBUG("Service %s is %s", name, ret ? "unavailable" : "available");
43fe83
+
43fe83
+ cleanup:
43fe83
+    dbus_message_unref(reply);
43fe83
+    return ret;
43fe83
+}
43fe83
+
43fe83
 
43fe83
 #else /* ! WITH_DBUS */
43fe83
 DBusConnection *virDBusGetSystemBus(void)
43fe83
@@ -1271,4 +1326,10 @@ int virDBusMessageDecode(DBusMessage* msg ATTRIBUTE_UNUSED,
43fe83
     return -1;
43fe83
 }
43fe83
 
43fe83
+int virDBusIsServiceEnabled(const char *name ATTRIBUTE_UNUSED)
43fe83
+{
43fe83
+    VIR_DEBUG("DBus support not compiled into this binary");
43fe83
+    return -2;
43fe83
+}
43fe83
+
43fe83
 #endif /* ! WITH_DBUS */
43fe83
diff --git a/src/util/virdbus.h b/src/util/virdbus.h
43fe83
index a5aab56..194a01a 100644
43fe83
--- a/src/util/virdbus.h
43fe83
+++ b/src/util/virdbus.h
43fe83
@@ -45,4 +45,5 @@ int virDBusCallMethod(DBusConnection *conn,
43fe83
 int virDBusMessageRead(DBusMessage *msg,
43fe83
                        const char *types, ...);
43fe83
 
43fe83
+int virDBusIsServiceEnabled(const char *name);
43fe83
 #endif /* __VIR_DBUS_H__ */
43fe83
diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c
43fe83
index 13370b5..e72b7f0 100644
43fe83
--- a/src/util/virsystemd.c
43fe83
+++ b/src/util/virsystemd.c
43fe83
@@ -138,18 +138,20 @@ int virSystemdCreateMachine(const char *name,
43fe83
                             bool iscontainer,
43fe83
                             const char *partition)
43fe83
 {
43fe83
-    int ret = -1;
43fe83
+    int ret;
43fe83
     DBusConnection *conn;
43fe83
     char *machinename = NULL;
43fe83
     char *creatorname = NULL;
43fe83
     char *username = NULL;
43fe83
     char *slicename = NULL;
43fe83
 
43fe83
-    if (!virDBusHasSystemBus())
43fe83
-        return -2;
43fe83
+    ret = virDBusIsServiceEnabled("org.freedesktop.machine1");
43fe83
+    if (ret < 0)
43fe83
+        return ret;
43fe83
 
43fe83
     conn = virDBusGetSystemBus();
43fe83
 
43fe83
+    ret = -1;
43fe83
     if (privileged) {
43fe83
         if (virAsprintf(&machinename, "%s-%s", drivername, name) < 0)
43fe83
             goto cleanup;
43fe83
@@ -228,15 +230,8 @@ int virSystemdCreateMachine(const char *name,
43fe83
                           (unsigned int)pidleader,
43fe83
                           rootdir ? rootdir : "",
43fe83
                           1, "Slice", "s",
43fe83
-                          slicename) < 0) {
43fe83
-        virErrorPtr err = virGetLastError();
43fe83
-        if (err->code == VIR_ERR_DBUS_SERVICE &&
43fe83
-            STREQ(err->str2, "org.freedesktop.DBus.Error.ServiceUnknown")) {
43fe83
-            virResetLastError();
43fe83
-            ret = -2;
43fe83
-        }
43fe83
+                          slicename) < 0)
43fe83
         goto cleanup;
43fe83
-    }
43fe83
 
43fe83
     ret = 0;
43fe83
 
43fe83
-- 
43fe83
1.8.3.2
43fe83