X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Funit-name.c;h=a809713595f69b10849f97c9a0e7637c9f77ac59;hp=4b52f7bc53a581a0944b92e24e27b6068494ee08;hb=e86b80b834016d273196c5ec9687fddcddcf9381;hpb=35eb6b124ebdf82bd77aad6e44962a9a039c4d33 diff --git a/src/shared/unit-name.c b/src/shared/unit-name.c index 4b52f7bc5..a80971359 100644 --- a/src/shared/unit-name.c +++ b/src/shared/unit-name.c @@ -33,7 +33,32 @@ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ ":-_.\\" -bool unit_name_is_valid_no_type(const char *n, bool template_ok) { +static const char* const unit_type_table[_UNIT_TYPE_MAX] = { + [UNIT_SERVICE] = "service", + [UNIT_SOCKET] = "socket", + [UNIT_TARGET] = "target", + [UNIT_DEVICE] = "device", + [UNIT_MOUNT] = "mount", + [UNIT_AUTOMOUNT] = "automount", + [UNIT_SNAPSHOT] = "snapshot", + [UNIT_TIMER] = "timer", + [UNIT_SWAP] = "swap", + [UNIT_PATH] = "path", +}; + +DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType); + +static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = { + [UNIT_STUB] = "stub", + [UNIT_LOADED] = "loaded", + [UNIT_ERROR] = "error", + [UNIT_MERGED] = "merged", + [UNIT_MASKED] = "masked" +}; + +DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState); + +bool unit_name_is_valid(const char *n, bool template_ok) { const char *e, *i, *at; /* Valid formats: @@ -51,6 +76,9 @@ bool unit_name_is_valid_no_type(const char *n, bool template_ok) { if (!e || e == n) return false; + if (unit_type_from_string(e + 1) < 0) + return false; + for (i = n, at = NULL; i < e; i++) { if (*i == '@' && !at) @@ -154,7 +182,7 @@ char *unit_name_change_suffix(const char *n, const char *suffix) { size_t a, b; assert(n); - assert(unit_name_is_valid_no_type(n, true)); + assert(unit_name_is_valid(n, true)); assert(suffix); assert_se(e = strrchr(n, '.')); @@ -180,7 +208,7 @@ char *unit_name_build(const char *prefix, const char *instance, const char *suff if (!instance) return strappend(prefix, suffix); - return join(prefix, "@", instance, suffix, NULL); + return strjoin(prefix, "@", instance, suffix, NULL); } static char *do_escape_char(char c, char *t) { @@ -307,45 +335,51 @@ bool unit_name_is_template(const char *n) { assert(n); - if (!(p = strchr(n, '@'))) + p = strchr(n, '@'); + if (!p) return false; return p[1] == '.'; } +bool unit_name_is_instance(const char *n) { + const char *p; + + assert(n); + + p = strchr(n, '@'); + if (!p) + return false; + + return p[1] != '.'; +} + char *unit_name_replace_instance(const char *f, const char *i) { const char *p, *e; char *r, *k; - size_t a; + size_t a, b; assert(f); p = strchr(f, '@'); - assert_se(e = strrchr(f, '.')); - - a = p - f; - - if (p) { - size_t b; - - b = strlen(i); - - r = new(char, a + 1 + b + strlen(e) + 1); - if (!r) - return NULL; + if (!p) + return strdup(f); - k = mempcpy(r, f, a + 1); - k = mempcpy(k, i, b); - } else { + e = strrchr(f, '.'); + if (!e) + assert_se(e = strchr(f, 0)); - r = new(char, a + strlen(e) + 1); - if (!r) - return NULL; + a = p - f; + b = strlen(i); - k = mempcpy(r, f, a); - } + r = new(char, a + 1 + b + strlen(e) + 1); + if (!r) + return NULL; + k = mempcpy(r, f, a + 1); + k = mempcpy(k, i, b); strcpy(k, e); + return r; } @@ -397,7 +431,7 @@ char *unit_name_from_path_instance(const char *prefix, const char *path, const c if (!p) return NULL; - r = join(prefix, "@", p, suffix, NULL); + r = strjoin(prefix, "@", p, suffix, NULL); free(p); return r; @@ -442,8 +476,7 @@ char *unit_name_mangle(const char *name) { /* Try to turn a string that might not be a unit name into a * sensible unit name. */ - if (path_startswith(name, "/dev/") || - path_startswith(name, "/sys/")) + if (is_device_path(name)) return unit_name_from_path(name, ".device"); if (path_is_absolute(name)) @@ -452,12 +485,11 @@ char *unit_name_mangle(const char *name) { /* We'll only escape the obvious characters here, to play * safe. */ - r = new(char, strlen(name) * 4 + 1); + r = new(char, strlen(name) * 4 + 1 + sizeof(".service")-1); if (!r) return NULL; for (f = name, t = r; *f; f++) { - if (*f == '/') *(t++) = '-'; else if (!strchr("@" VALID_CHARS, *f)) @@ -466,7 +498,52 @@ char *unit_name_mangle(const char *name) { *(t++) = *f; } - *t = 0; + if (unit_name_to_type(name) < 0) + strcpy(t, ".service"); + else + *t = 0; + + return r; +} + +char *snapshot_name_mangle(const char *name) { + char *r, *t; + const char *f; + + assert(name); + + /* Similar to unit_name_mangle(), but is called when we know + * that this is about snapshot units. */ + + r = new(char, strlen(name) * 4 + 1 + sizeof(".snapshot")-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 (!endswith(name, ".snapshot")) + strcpy(t, ".snapshot"); + else + *t = 0; return r; } + +UnitType unit_name_to_type(const char *n) { + const char *e; + + assert(n); + + e = strrchr(n, '.'); + if (!e) + return _UNIT_TYPE_INVALID; + + return unit_type_from_string(e + 1); +}