basic: don't append suffixes to unit name glob expressions

When the user specifies "foo*" as unit name glob expression, we shouldn't turn this into "foo*.service". Hence: only
append a suffix if the specified string isn't a glob expression.

Fixes: #2397
This commit is contained in:
Lennart Poettering
2016-01-27 02:21:28 +01:00
parent 1f00ededc7
commit 2aaafcf570
2 changed files with 54 additions and 24 deletions
+34 -11
View File
@@ -27,6 +27,7 @@
#include "alloc-util.h"
#include "bus-label.h"
#include "glob-util.h"
#include "hexdecoct.h"
#include "macro.h"
#include "path-util.h"
@@ -35,10 +36,22 @@
#include "strv.h"
#include "unit-name.h"
/* Characters valid in a unit name. */
#define VALID_CHARS \
DIGITS LETTERS \
DIGITS \
LETTERS \
":-_.\\"
/* The same, but also permits the single @ character that may appear */
#define VALID_CHARS_WITH_AT \
"@" \
VALID_CHARS
/* All chars valid in a unit name glob */
#define VALID_CHARS_GLOB \
VALID_CHARS_WITH_AT \
"[]!-*?"
bool unit_name_is_valid(const char *n, UnitNameFlags flags) {
const char *e, *i, *at;
@@ -637,7 +650,7 @@ static char *do_escape_mangle(const char *f, UnitNameMangle allow_globs, char *t
/* We'll only escape the obvious characters here, to play
* safe. */
valid_chars = allow_globs == UNIT_NAME_GLOB ? "@" VALID_CHARS "[]!-*?" : "@" VALID_CHARS;
valid_chars = allow_globs == UNIT_NAME_GLOB ? VALID_CHARS_GLOB : VALID_CHARS_WITH_AT;
for (; *f; f++) {
if (*f == '/')
@@ -672,15 +685,15 @@ int unit_name_mangle_with_suffix(const char *name, UnitNameMangle allow_globs, c
if (!unit_suffix_is_valid(suffix))
return -EINVAL;
if (unit_name_is_valid(name, UNIT_NAME_ANY)) {
/* No mangling necessary... */
s = strdup(name);
if (!s)
return -ENOMEM;
/* Already a fully valid unit name? If so, no mangling is necessary... */
if (unit_name_is_valid(name, UNIT_NAME_ANY))
goto good;
*ret = s;
return 0;
}
/* Already a fully valid globbing expression? If so, no mangling is necessary either... */
if (allow_globs == UNIT_NAME_GLOB &&
string_is_glob(name) &&
in_charset(name, VALID_CHARS_GLOB))
goto good;
if (is_device_path(name)) {
r = unit_name_from_path(name, ".device", ret);
@@ -705,11 +718,21 @@ int unit_name_mangle_with_suffix(const char *name, UnitNameMangle allow_globs, c
t = do_escape_mangle(name, allow_globs, s);
*t = 0;
if (unit_name_to_type(s) < 0)
/* Append a suffix if it doesn't have any, but only if this is not a glob, so that we can allow "foo.*" as a
* valid glob. */
if ((allow_globs != UNIT_NAME_GLOB || !string_is_glob(s)) && unit_name_to_type(s) < 0)
strcpy(t, suffix);
*ret = s;
return 1;
good:
s = strdup(name);
if (!s)
return -ENOMEM;
*ret = s;
return 0;
}
int slice_build_parent_slice(const char *slice, char **ret) {