X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Funit-name.c;h=86d6ce3df1583bb3fd67de6a6ccb3d9f0d4e73b3;hp=6c167b4331d995a9d9bc8b054611d8d905632859;hb=2f07de3b6cacf44462635ab0fff56391b491e454;hpb=f8294e4175918117ca6c131720bcf287eadcd029 diff --git a/src/shared/unit-name.c b/src/shared/unit-name.c index 6c167b433..86d6ce3df 100644 --- a/src/shared/unit-name.c +++ b/src/shared/unit-name.c @@ -21,13 +21,13 @@ #include #include -#include #include "path-util.h" #include "bus-label.h" #include "util.h" #include "unit-name.h" #include "def.h" +#include "strv.h" #define VALID_CHARS \ DIGITS LETTERS \ @@ -71,9 +71,11 @@ bool unit_name_is_valid(const char *n, enum template_valid template_ok) { * string.suffix */ - assert(n); assert(IN_SET(template_ok, TEMPLATE_VALID, TEMPLATE_INVALID)); + if (isempty(n)) + return false; + if (strlen(n) >= UNIT_NAME_MAX) return false; @@ -97,7 +99,7 @@ bool unit_name_is_valid(const char *n, enum template_valid template_ok) { if (at == n) return false; - if (!template_ok == TEMPLATE_VALID && at+1 == e) + if (template_ok != TEMPLATE_VALID && at+1 == e) return false; } @@ -105,36 +107,27 @@ bool unit_name_is_valid(const char *n, enum template_valid template_ok) { } bool unit_instance_is_valid(const char *i) { - assert(i); /* The max length depends on the length of the string, so we * don't really check this here. */ - if (i[0] == 0) + if (isempty(i)) return false; /* We allow additional @ in the instance string, we do not * allow them in the prefix! */ - for (; *i; i++) - if (!strchr("@" VALID_CHARS, *i)) - return false; - - return true; + return in_charset(i, "@" VALID_CHARS); } bool unit_prefix_is_valid(const char *p) { /* We don't allow additional @ in the instance string */ - if (p[0] == 0) + if (isempty(p)) return false; - for (; *p; p++) - if (!strchr(VALID_CHARS, *p)) - return false; - - return true; + return in_charset(p, VALID_CHARS); } int unit_name_to_instance(const char *n, char **instance) { @@ -151,15 +144,18 @@ int unit_name_to_instance(const char *n, char **instance) { return 0; } - assert_se(d = strrchr(n, '.')); - assert(p < d); + d = strrchr(n, '.'); + if (!d) + return -EINVAL; + if (d < p) + return -EINVAL; i = strndup(p+1, d-p-1); if (!i) return -ENOMEM; *instance = i; - return 0; + return 1; } char *unit_name_to_prefix_and_instance(const char *n) { @@ -168,13 +164,14 @@ char *unit_name_to_prefix_and_instance(const char *n) { assert(n); assert_se(d = strrchr(n, '.')); - return strndup(n, d - n); } char *unit_name_to_prefix(const char *n) { const char *p; + assert(n); + p = strchr(n, '@'); if (p) return strndup(n, p - n); @@ -187,7 +184,6 @@ char *unit_name_change_suffix(const char *n, const char *suffix) { size_t a, b; assert(n); - assert(unit_name_is_valid(n, TEMPLATE_VALID)); assert(suffix); assert(suffix[0] == '.'); @@ -199,16 +195,12 @@ char *unit_name_change_suffix(const char *n, const char *suffix) { if (!r) return NULL; - memcpy(r, n, a); - memcpy(r+a, suffix, b+1); - + strcpy(mempcpy(r, n, a), suffix); return r; } char *unit_name_build(const char *prefix, const char *instance, const char *suffix) { assert(prefix); - assert(unit_prefix_is_valid(prefix)); - assert(!instance || unit_instance_is_valid(instance)); assert(suffix); if (!instance) @@ -218,10 +210,13 @@ char *unit_name_build(const char *prefix, const char *instance, const char *suff } static char *do_escape_char(char c, char *t) { + assert(t); + *(t++) = '\\'; *(t++) = 'x'; *(t++) = hexchar(c >> 4); *(t++) = hexchar(c); + return t; } @@ -247,9 +242,35 @@ static char *do_escape(const char *f, char *t) { return t; } +static char *do_escape_mangle(const char *f, enum unit_name_mangle allow_globs, char *t) { + const char *valid_chars; + + assert(f); + assert(IN_SET(allow_globs, MANGLE_GLOB, MANGLE_NOGLOB)); + assert(t); + + /* We'll only escape the obvious characters here, to play + * safe. */ + + valid_chars = allow_globs == MANGLE_GLOB ? "@" VALID_CHARS "[]!-*?" : "@" VALID_CHARS; + + for (; *f; f++) { + if (*f == '/') + *(t++) = '-'; + else if (!strchr(valid_chars, *f)) + t = do_escape_char(*f, t); + else + *(t++) = *f; + } + + return t; +} + char *unit_name_escape(const char *f) { char *r, *t; + assert(f); + r = new(char, strlen(f)*4+1); if (!r) return NULL; @@ -294,7 +315,7 @@ char *unit_name_unescape(const char *f) { } char *unit_name_path_escape(const char *f) { - _cleanup_free_ char *p; + _cleanup_free_ char *p = NULL; assert(f); @@ -304,14 +325,14 @@ char *unit_name_path_escape(const char *f) { path_kill_slashes(p); - if (streq(p, "/") || streq(p, "")) + if (STR_IN_SET(p, "/", "")) return strdup("-"); return unit_name_escape(p[0] == '/' ? p + 1 : p); } char *unit_name_path_unescape(const char *f) { - char *e; + char *e, *w; assert(f); @@ -320,11 +341,8 @@ char *unit_name_path_unescape(const char *f) { return NULL; if (e[0] != '/') { - char *w; - w = strappend("/", e); free(e); - return w; } @@ -332,7 +350,7 @@ char *unit_name_path_unescape(const char *f) { } bool unit_name_is_template(const char *n) { - const char *p; + const char *p, *e; assert(n); @@ -340,11 +358,15 @@ bool unit_name_is_template(const char *n) { if (!p) return false; - return p[1] == '.'; + e = strrchr(p+1, '.'); + if (!e) + return false; + + return e == p + 1; } bool unit_name_is_instance(const char *n) { - const char *p; + const char *p, *e; assert(n); @@ -352,15 +374,20 @@ bool unit_name_is_instance(const char *n) { if (!p) return false; - return p[1] != '.'; + e = strrchr(p+1, '.'); + if (!e) + return false; + + return e > p + 1; } char *unit_name_replace_instance(const char *f, const char *i) { const char *p, *e; - char *r, *k; + char *r; size_t a, b; assert(f); + assert(i); p = strchr(f, '@'); if (!p) @@ -368,7 +395,7 @@ char *unit_name_replace_instance(const char *f, const char *i) { e = strrchr(f, '.'); if (!e) - assert_se(e = strchr(f, 0)); + e = strchr(f, 0); a = p - f; b = strlen(i); @@ -377,10 +404,7 @@ char *unit_name_replace_instance(const char *f, const char *i) { if (!r) return NULL; - k = mempcpy(r, f, a + 1); - k = mempcpy(k, i, b); - strcpy(k, e); - + strcpy(mempcpy(mempcpy(r, f, a + 1), i, b), e); return r; } @@ -389,18 +413,23 @@ char *unit_name_template(const char *f) { char *r; size_t a; + assert(f); + p = strchr(f, '@'); if (!p) return strdup(f); - assert_se(e = strrchr(f, '.')); - a = p - f + 1; + e = strrchr(f, '.'); + if (!e) + e = strchr(f, 0); + + a = p - f; - r = new(char, a + strlen(e) + 1); + r = new(char, a + 1 + strlen(e) + 1); if (!r) return NULL; - strcpy(mempcpy(r, f, a), e); + strcpy(mempcpy(r, f, a + 1), e); return r; } @@ -471,18 +500,19 @@ int unit_name_from_dbus_path(const char *path, char **name) { return 0; } - /** - * Try to turn a string that might not be a unit name into a - * sensible unit name. + * Convert a string to a unit name. /dev/blah is converted to dev-blah.device, + * /blah/blah is converted to blah-blah.mount, anything else is left alone, + * except that @suffix is appended if a valid unit suffix is not present. + * + * If @allow_globs, globs characters are preserved. Otherwise they are escaped. */ -char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) { +char *unit_name_mangle_with_suffix(const char *name, enum unit_name_mangle allow_globs, const char *suffix) { char *r, *t; - const char *f; - const char* valid_chars = allow_globs == MANGLE_GLOB ? "@" VALID_CHARS "[]!-*?" : "@" VALID_CHARS; assert(name); - assert(IN_SET(allow_globs, MANGLE_GLOB, MANGLE_NOGLOB)); + assert(suffix); + assert(suffix[0] == '.'); if (is_device_path(name)) return unit_name_from_path(name, ".device"); @@ -490,57 +520,13 @@ char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) { if (path_is_absolute(name)) return unit_name_from_path(name, ".mount"); - /* We'll only escape the obvious characters here, to play - * safe. */ - - r = new(char, strlen(name) * 4 + strlen(".service") + 1); - if (!r) - return NULL; - - for (f = name, t = r; *f; f++) { - if (*f == '/') - *(t++) = '-'; - else if (!strchr(valid_chars, *f)) - t = do_escape_char(*f, t); - else - *(t++) = *f; - } - - if (unit_name_to_type(name) < 0) - strcpy(t, ".service"); - else - *t = 0; - - return r; -} - - -/** - * Similar to unit_name_mangle(), but is called when we know - * that this is about a specific unit type. - */ -char *unit_name_mangle_with_suffix(const char *name, enum unit_name_mangle allow_globs, const char *suffix) { - char *r, *t; - const char *f; - - assert(name); - assert(suffix); - assert(suffix[0] == '.'); - r = new(char, strlen(name) * 4 + strlen(suffix) + 1); if (!r) return NULL; - for (f = name, t = r; *f; f++) { - if (*f == '/') - *(t++) = '-'; - else if (!strchr(VALID_CHARS, *f)) - t = do_escape_char(*f, t); - else - *(t++) = *f; - } + t = do_escape_mangle(name, allow_globs, r); - if (!endswith(name, suffix)) + if (unit_name_to_type(name) < 0) strcpy(t, suffix); else *t = 0; @@ -586,3 +572,32 @@ int build_subslice(const char *slice, const char*name, char **subslice) { *subslice = ret; return 0; } + +static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = { + [UNIT_REQUIRES] = "Requires", + [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable", + [UNIT_REQUISITE] = "Requisite", + [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable", + [UNIT_WANTS] = "Wants", + [UNIT_BINDS_TO] = "BindsTo", + [UNIT_PART_OF] = "PartOf", + [UNIT_REQUIRED_BY] = "RequiredBy", + [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable", + [UNIT_WANTED_BY] = "WantedBy", + [UNIT_BOUND_BY] = "BoundBy", + [UNIT_CONSISTS_OF] = "ConsistsOf", + [UNIT_CONFLICTS] = "Conflicts", + [UNIT_CONFLICTED_BY] = "ConflictedBy", + [UNIT_BEFORE] = "Before", + [UNIT_AFTER] = "After", + [UNIT_ON_FAILURE] = "OnFailure", + [UNIT_TRIGGERS] = "Triggers", + [UNIT_TRIGGERED_BY] = "TriggeredBy", + [UNIT_PROPAGATES_RELOAD_TO] = "PropagatesReloadTo", + [UNIT_RELOAD_PROPAGATED_FROM] = "ReloadPropagatedFrom", + [UNIT_JOINS_NAMESPACE_OF] = "JoinsNamespaceOf", + [UNIT_REFERENCES] = "References", + [UNIT_REFERENCED_BY] = "ReferencedBy", +}; + +DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);