]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
systemd: Escape only needed characters for machined
authorMartin Kletzander <mkletzan@redhat.com>
Fri, 27 Nov 2015 13:24:38 +0000 (14:24 +0100)
committerMartin Kletzander <mkletzan@redhat.com>
Fri, 27 Nov 2015 15:39:46 +0000 (16:39 +0100)
Machine name escaping follows the same rules as serice name escape,
except that '.' and '-' must not be escaped in machine names, due
to a bug in systemd-machined.

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 abd883c73844d3aa65bdb86b0c6cd06329b8a174..337d6a208ab61e067fe8c6e1fe3fd97f42521ca6 100644 (file)
 
 VIR_LOG_INIT("util.systemd");
 
+/**
+ * virSystemdEscapeName:
+ *
+ * This function escapes various characters in @name and appends that
+ * escaped string to @buf, in order to comply with the requirements
+ * from systemd/machined.  Parameter @full_escape decides whether to
+ * also escape dot as a first character and '-'.
+ */
 static void virSystemdEscapeName(virBufferPtr buf,
-                                 const char *name)
+                                 const char *name,
+                                 bool full_escape)
 {
     static const char hextable[16] = "0123456789abcdef";
 
@@ -57,7 +66,7 @@ static void virSystemdEscapeName(virBufferPtr buf,
         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"            \
         ":-_.\\"
 
-    if (*name == '.') {
+    if (full_escape && *name == '.') {
         ESCAPE(*name);
         name++;
     }
@@ -65,7 +74,7 @@ static void virSystemdEscapeName(virBufferPtr buf,
     while (*name) {
         if (*name == '/')
             virBufferAddChar(buf, '-');
-        else if (*name == '-' ||
+        else if ((full_escape && *name == '-') ||
                  *name == '\\' ||
                  !strchr(VALID_CHARS, *name))
             ESCAPE(*name);
@@ -85,9 +94,9 @@ char *virSystemdMakeScopeName(const char *name,
     virBuffer buf = VIR_BUFFER_INITIALIZER;
 
     virBufferAddLit(&buf, "machine-");
-    virSystemdEscapeName(&buf, drivername);
+    virSystemdEscapeName(&buf, drivername, true);
     virBufferAddLit(&buf, "\\x2d");
-    virSystemdEscapeName(&buf, name);
+    virSystemdEscapeName(&buf, name, true);
     virBufferAddLit(&buf, ".scope");
 
     if (virBufferCheckError(&buf) < 0)
@@ -104,7 +113,7 @@ char *virSystemdMakeSliceName(const char *partition)
     if (*partition == '/')
         partition++;
 
-    virSystemdEscapeName(&buf, partition);
+    virSystemdEscapeName(&buf, partition, true);
     virBufferAddLit(&buf, ".slice");
 
     if (virBufferCheckError(&buf) < 0)
@@ -130,7 +139,7 @@ char *virSystemdMakeMachineName(const char *name,
         virBufferAsprintf(&buf, "%s-%s-", username, drivername);
     }
 
-    virSystemdEscapeName(&buf, name);
+    virSystemdEscapeName(&buf, name, false);
 
     machinename = virBufferContentAndReset(&buf);
  cleanup:
index 06fec5495bc2e4aa5937214c7e4402c52eb09ad0..49d37c2032ecd4c6a3a6f98d8ee7f8ac18e17aea 100644 (file)
@@ -517,9 +517,9 @@ mymain(void)
     } while (0)
 
     TEST_MACHINE("demo", "qemu-demo");
-    TEST_MACHINE("demo-name", "qemu-demo\\x2dname");
+    TEST_MACHINE("demo-name", "qemu-demo-name");
     TEST_MACHINE("demo!name", "qemu-demo\\x21name");
-    TEST_MACHINE(".demo", "qemu-\\x2edemo");
+    TEST_MACHINE(".demo", "qemu-.demo");
     TEST_MACHINE("bull\U0001f4a9", "qemu-bull\\xf0\\x9f\\x92\\xa9");
 
 # define TESTS_PM_SUPPORT_HELPER(name, function)                        \