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 <mkletzan@redhat.com>
(cherry picked from commit e24eda48cf)
This commit is contained in:
Martin Kletzander 2015-11-24 15:56:12 +01:00 committed by Cole Robinson
parent 17a8713577
commit b92cd48c57
2 changed files with 50 additions and 7 deletions

View 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);

View 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 { \