When mangling names, optionally emit a warning (#8400)

The warning is not emitted for absolute paths like /dev/sda or /home, which are
converted to .device and .mount unit names without any fuss.

Most of the time it's unlikely that users use invalid unit names on purpose,
so let's warn them. Warnings are silenced when --quiet is used.

$ build/systemctl show -p Id hello@foo-bar/baz
Invalid unit name "hello@foo-bar/baz" was escaped as "hello@foo-bar-baz" (maybe you should use systemd-escape?)
Id=hello@foo-bar-baz.service

$ build/systemd-run --user --slice foo-bar/baz --unit foo-bar/foo true
Invalid unit name "foo-bar/foo" was escaped as "foo-bar-foo" (maybe you should use systemd-escape?)
Invalid unit name "foo-bar/baz" was escaped as "foo-bar-baz" (maybe you should use systemd-escape?)
Running as unit: foo-bar-foo.service

Fixes #8302.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2018-03-21 15:26:47 +01:00
committed by Lennart Poettering
parent b85aca4f92
commit 37cbc1d579
12 changed files with 71 additions and 62 deletions
+17 -17
View File
@@ -162,10 +162,10 @@ static void test_unit_name_to_path(void) {
test_unit_name_to_path_one("home/foo", NULL, -EINVAL);
}
static void test_unit_name_mangle_one(UnitNameMangle allow_globs, const char *pattern, const char *expect, int ret) {
static void test_unit_name_mangle_one(bool allow_globs, const char *pattern, const char *expect, int ret) {
_cleanup_free_ char *t = NULL;
assert_se(unit_name_mangle(pattern, allow_globs, &t) == ret);
assert_se(unit_name_mangle(pattern, (allow_globs * UNIT_NAME_MANGLE_GLOB) | UNIT_NAME_MANGLE_WARN, &t) == ret);
puts(strna(t));
assert_se(streq_ptr(t, expect));
@@ -173,29 +173,29 @@ static void test_unit_name_mangle_one(UnitNameMangle allow_globs, const char *pa
_cleanup_free_ char *k = NULL;
assert_se(unit_name_is_valid(t, UNIT_NAME_ANY) ||
(allow_globs == UNIT_NAME_GLOB && string_is_glob(t)));
(allow_globs && string_is_glob(t)));
assert_se(unit_name_mangle(t, allow_globs, &k) == 0);
assert_se(unit_name_mangle(t, (allow_globs * UNIT_NAME_MANGLE_GLOB) | UNIT_NAME_MANGLE_WARN, &k) == 0);
assert_se(streq_ptr(t, k));
}
}
static void test_unit_name_mangle(void) {
puts("-------------------------------------------------");
test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "foo.service", "foo.service", 0);
test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "/home", "home.mount", 1);
test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "/dev/sda", "dev-sda.device", 1);
test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "üxknürz.service", "\\xc3\\xbcxkn\\xc3\\xbcrz.service", 1);
test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "foobar-meh...waldi.service", "foobar-meh...waldi.service", 0);
test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "_____####----.....service", "_____\\x23\\x23\\x23\\x23----.....service", 1);
test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "_____##@;;;,,,##----.....service", "_____\\x23\\x23@\\x3b\\x3b\\x3b\\x2c\\x2c\\x2c\\x23\\x23----.....service", 1);
test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "xxx@@@@/////\\\\\\\\\\yyy.service", "xxx@@@@-----\\\\\\\\\\yyy.service", 1);
test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "", NULL, -EINVAL);
test_unit_name_mangle_one(false, "foo.service", "foo.service", 0);
test_unit_name_mangle_one(false, "/home", "home.mount", 1);
test_unit_name_mangle_one(false, "/dev/sda", "dev-sda.device", 1);
test_unit_name_mangle_one(false, "üxknürz.service", "\\xc3\\xbcxkn\\xc3\\xbcrz.service", 1);
test_unit_name_mangle_one(false, "foobar-meh...waldi.service", "foobar-meh...waldi.service", 0);
test_unit_name_mangle_one(false, "_____####----.....service", "_____\\x23\\x23\\x23\\x23----.....service", 1);
test_unit_name_mangle_one(false, "_____##@;;;,,,##----.....service", "_____\\x23\\x23@\\x3b\\x3b\\x3b\\x2c\\x2c\\x2c\\x23\\x23----.....service", 1);
test_unit_name_mangle_one(false, "xxx@@@@/////\\\\\\\\\\yyy.service", "xxx@@@@-----\\\\\\\\\\yyy.service", 1);
test_unit_name_mangle_one(false, "", NULL, -EINVAL);
test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo.service", "foo.service", 0);
test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo", "foo.service", 1);
test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo*", "foo*", 0);
test_unit_name_mangle_one(UNIT_NAME_GLOB, "ü*", "\\xc3\\xbc*", 1);
test_unit_name_mangle_one(true, "foo.service", "foo.service", 0);
test_unit_name_mangle_one(true, "foo", "foo.service", 1);
test_unit_name_mangle_one(true, "foo*", "foo*", 0);
test_unit_name_mangle_one(true, "ü*", "\\xc3\\xbc*", 1);
}
static int test_unit_printf(void) {