From: Martin Kletzander Date: Tue, 24 Nov 2015 14:56:12 +0000 (+0100) Subject: systemd: Escape machine name for machined X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=e24eda48cfae84a9003456b68eaf753a26123639;p=people%2Fliuw%2Flibxenctrl-split%2Flibvirt.git systemd: Escape machine name for machined 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 --- diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c index 1354b08b9..abd883c73 100644 --- a/src/util/virsystemd.c +++ b/src/util/virsystemd.c @@ -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); diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c index d0b9335b2..06fec5495 100644 --- a/tests/virsystemdtest.c +++ b/tests/virsystemdtest.c @@ -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 { \