]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
systemd: Escape machine name for machined
authorMartin Kletzander <mkletzan@redhat.com>
Tue, 24 Nov 2015 14:56:12 +0000 (15:56 +0100)
committerMartin Kletzander <mkletzan@redhat.com>
Thu, 26 Nov 2015 14:15:41 +0000 (15:15 +0100)
According to the documentation, CreateMachine accepts only 7bit ASCII
characters in the machinename parameter, so let's make sure we can start
machines with unicode names with systemd.  We already have a function
for that, we just forgot to use it.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1062943
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1282846

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
src/util/virsystemd.c
tests/virsystemdtest.c

index 1354b08b9e04c4be7a98af854d34effc34f63d77..abd883c73844d3aa65bdb86b0c6cd06329b8a174 100644 (file)
@@ -119,16 +119,20 @@ char *virSystemdMakeMachineName(const char *name,
 {
     char *machinename = NULL;
     char *username = NULL;
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+
     if (privileged) {
-        if (virAsprintf(&machinename, "%s-%s", drivername, name) < 0)
-            goto cleanup;
+        virBufferAsprintf(&buf, "%s-", drivername);
     } else {
         if (!(username = virGetUserName(geteuid())))
             goto cleanup;
-        if (virAsprintf(&machinename, "%s-%s-%s", username, drivername, name) < 0)
-            goto cleanup;
+
+        virBufferAsprintf(&buf, "%s-%s-", username, drivername);
     }
 
+    virSystemdEscapeName(&buf, name);
+
+    machinename = virBufferContentAndReset(&buf);
  cleanup:
     VIR_FREE(username);
 
index d0b9335b24ae28c6f26876478e878c3a827d121b..06fec5495bc2e4aa5937214c7e4402c52eb09ad0 100644 (file)
@@ -338,7 +338,7 @@ static int testCreateNetwork(const void *opaque ATTRIBUTE_UNUSED)
 }
 
 
-struct testScopeData {
+struct testNameData {
     const char *name;
     const char *expected;
 };
@@ -346,7 +346,7 @@ struct testScopeData {
 static int
 testScopeName(const void *opaque)
 {
-    const struct testScopeData *data = opaque;
+    const struct testNameData *data = opaque;
     int ret = -1;
     char *actual = NULL;
 
@@ -366,6 +366,29 @@ testScopeName(const void *opaque)
     return ret;
 }
 
+static int
+testMachineName(const void *opaque)
+{
+    const struct testNameData *data = opaque;
+    int ret = -1;
+    char *actual = NULL;
+
+    if (!(actual = virSystemdMakeMachineName(data->name, "qemu", true)))
+        goto cleanup;
+
+    if (STRNEQ(actual, data->expected)) {
+        fprintf(stderr, "Expected '%s' but got '%s'\n",
+                data->expected, actual);
+        goto cleanup;
+    }
+
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(actual);
+    return ret;
+}
+
 typedef int (*virSystemdCanHelper)(bool * result);
 struct testPMSupportData {
     virSystemdCanHelper tested;
@@ -471,7 +494,7 @@ mymain(void)
 
 # define TEST_SCOPE(name, unitname)                                     \
     do {                                                                \
-        struct testScopeData data = {                                   \
+        struct testNameData data = {                                    \
             name, unitname                                              \
         };                                                              \
         if (virtTestRun("Test scopename", testScopeName, &data) < 0)    \
@@ -482,6 +505,22 @@ mymain(void)
     TEST_SCOPE("demo-name", "machine-lxc\\x2ddemo\\x2dname.scope");
     TEST_SCOPE("demo!name", "machine-lxc\\x2ddemo\\x21name.scope");
     TEST_SCOPE(".demo", "machine-lxc\\x2d\\x2edemo.scope");
+    TEST_SCOPE("bull💩", "machine-lxc\\x2dbull\\xf0\\x9f\\x92\\xa9.scope");
+
+# define TEST_MACHINE(name, machinename)                                \
+    do {                                                                \
+        struct testNameData data = {                                    \
+            name, machinename                                           \
+        };                                                              \
+        if (virtTestRun("Test scopename", testMachineName, &data) < 0)  \
+            ret = -1;                                                   \
+    } while (0)
+
+    TEST_MACHINE("demo", "qemu-demo");
+    TEST_MACHINE("demo-name", "qemu-demo\\x2dname");
+    TEST_MACHINE("demo!name", "qemu-demo\\x21name");
+    TEST_MACHINE(".demo", "qemu-\\x2edemo");
+    TEST_MACHINE("bull\U0001f4a9", "qemu-bull\\xf0\\x9f\\x92\\xa9");
 
 # define TESTS_PM_SUPPORT_HELPER(name, function)                        \
     do {                                                                \