]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Add logic for handling systemd-machined non-existance
authorDaniel P. Berrange <berrange@redhat.com>
Mon, 22 Jul 2013 15:33:37 +0000 (16:33 +0100)
committerDaniel P. Berrange <berrange@redhat.com>
Tue, 23 Jul 2013 21:35:26 +0000 (22:35 +0100)
If systemd machine does not exist, return -2 instead of -1,
so that applications don't need to repeat the tedious error
checking code

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

index 8477cd3302049a3b4839bd7f98bd9527dfe48f67..11d1153045173a36e3ffcf9bb4507008dbf7b083 100644 (file)
@@ -26,6 +26,7 @@
 #include "virstring.h"
 #include "viralloc.h"
 #include "virutil.h"
+#include "virlog.h"
 
 #define VIR_FROM_THIS VIR_FROM_SYSTEMD
 
@@ -38,6 +39,8 @@
  * @rootdir: root directory of machine filesystem
  * @pidleader: PID of the leader process
  * @slice: name of the slice to place the machine in
+ *
+ * Returns 0 on success, -1 on fatal error, or -2 if systemd-machine is not available
  */
 int virSystemdCreateMachine(const char *name,
                             const char *drivername,
@@ -117,6 +120,7 @@ int virSystemdCreateMachine(const char *name,
      * allow further API calls to be made against the object.
      */
 
+    VIR_DEBUG("Attempting to create machine via systemd");
     if (virDBusCallMethod(conn,
                           NULL,
                           "org.freedesktop.machine1",
@@ -135,8 +139,15 @@ int virSystemdCreateMachine(const char *name,
                           (unsigned int)pidleader,
                           rootdir ? rootdir : "",
                           1, "Slice", "s",
-                          slicename) < 0)
+                          slicename) < 0) {
+        virErrorPtr err = virGetLastError();
+        if (err->code == VIR_ERR_DBUS_SERVICE &&
+            STREQ(err->str2, "org.freedesktop.DBus.Error.ServiceUnknown")) {
+            virResetLastError();
+            ret = -2;
+        }
         goto cleanup;
+    }
 
     ret = 0;
 
index 5f9cce6a9403528cab15e5607a7e40a8fdcdd966..1f4413c60120a4628e1fbdff43e3cf4fa5e2c986 100644 (file)
@@ -60,16 +60,20 @@ dbus_bool_t dbus_connection_set_watch_functions(DBusConnection *connection ATTRI
 DBusMessage *dbus_connection_send_with_reply_and_block(DBusConnection *connection ATTRIBUTE_UNUSED,
                                                        DBusMessage *message,
                                                        int timeout_milliseconds ATTRIBUTE_UNUSED,
-                                                       DBusError *error ATTRIBUTE_UNUSED)
+                                                       DBusError *error)
 {
-    DBusMessage *reply;
+    DBusMessage *reply = NULL;
 
     dbus_message_set_serial(message, 7);
 
-    if (getenv("FAIL_NO_SERVICE"))
+    if (getenv("FAIL_BAD_SERVICE"))
         reply = dbus_message_new_error(message,
-                                       "org.freedesktop.DBus.Error.ServiceUnknown",
-                                       "The name org.freedesktop.machine1 was not provided by any .service files");
+                                       "org.freedesktop.systemd.badthing",
+                                       "Something went wrong creating the machine");
+    else if (getenv("FAIL_NO_SERVICE"))
+        dbus_set_error(error,
+                       "org.freedesktop.DBus.Error.ServiceUnknown",
+                       "%s", "The name org.freedesktop.machine1 was not provided by any .service files");
     else
         reply = dbus_message_new_method_return(message);
 
index 3992722e98704c69c0fb7c1af6f23b33445f2302..bcf3ad32f8180b1980a227c8396870117f9d3082 100644 (file)
@@ -82,35 +82,60 @@ static int testCreateNoSystemd(const void *opaque ATTRIBUTE_UNUSED)
         3, 3, 3, 3,
         4, 4, 4, 4
     };
+    int rv;
 
     setenv("FAIL_NO_SERVICE", "1", 1);
 
-    if (virSystemdCreateMachine("demo",
-                                "qemu",
-                                true,
-                                uuid,
-                                NULL,
-                                123,
-                                false,
-                                NULL) == 0) {
+    if ((rv = virSystemdCreateMachine("demo",
+                                      "qemu",
+                                      true,
+                                      uuid,
+                                      NULL,
+                                      123,
+                                      false,
+                                      NULL)) == 0) {
         fprintf(stderr, "%s", "Unexpected create machine success\n");
         return -1;
     }
 
-    virErrorPtr err = virGetLastError();
+    if (rv != -2) {
+        fprintf(stderr, "%s", "Unexpected create machine error\n");
+        return -1;
+    }
+
+    return 0;
+}
 
-    if (!err) {
-        fprintf(stderr, "No error raised");
+static int testCreateBadSystemd(const void *opaque ATTRIBUTE_UNUSED)
+{
+    unsigned char uuid[VIR_UUID_BUFLEN] = {
+        1, 1, 1, 1,
+        2, 2, 2, 2,
+        3, 3, 3, 3,
+        4, 4, 4, 4
+    };
+    int rv;
+
+    setenv("FAIL_BAD_SERVICE", "1", 1);
+
+    if ((rv = virSystemdCreateMachine("demo",
+                                      "qemu",
+                                      true,
+                                      uuid,
+                                      NULL,
+                                      123,
+                                      false,
+                                      NULL)) == 0) {
+        fprintf(stderr, "%s", "Unexpected create machine success\n");
         return -1;
     }
 
-    if (err->code == VIR_ERR_DBUS_SERVICE &&
-        STREQ(err->str2, "org.freedesktop.DBus.Error.ServiceUnknown"))
-        return 0;
+    if (rv != -1) {
+        fprintf(stderr, "%s", "Unexpected create machine error\n");
+        return -1;
+    }
 
-    fprintf(stderr, "Unexpected error code %d / message %s\n",
-            err->code, err->str2);
-    return -1;
+    return 0;
 }
 
 static int
@@ -122,7 +147,9 @@ mymain(void)
         ret = -1;
     if (virtTestRun("Test create machine ", 1, testCreateMachine, NULL) < 0)
         ret = -1;
-    if (virtTestRun("Test create nosystemd ", 1, testCreateNoSystemd, NULL) < 0)
+    if (virtTestRun("Test create no systemd ", 1, testCreateNoSystemd, NULL) < 0)
+        ret = -1;
+    if (virtTestRun("Test create bad systemd ", 1, testCreateBadSystemd, NULL) < 0)
         ret = -1;
 
     return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;