#include "util.h"
#include "unit-name.h"
#include "def.h"
+#include "strv.h"
#define VALID_CHARS \
DIGITS LETTERS \
* 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;
}
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) {
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) {
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);
size_t a, b;
assert(n);
- assert(unit_name_is_valid(n, TEMPLATE_VALID));
assert(suffix);
assert(suffix[0] == '.');
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)
}
static char *do_escape_char(char c, char *t) {
+ assert(t);
+
*(t++) = '\\';
*(t++) = 'x';
*(t++) = hexchar(c >> 4);
*(t++) = hexchar(c);
+
return 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;
}
char *unit_name_path_escape(const char *f) {
- _cleanup_free_ char *p;
+ _cleanup_free_ char *p = NULL;
assert(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);
return NULL;
if (e[0] != '/') {
- char *w;
-
w = strappend("/", e);
free(e);
-
return w;
}
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)
e = strrchr(f, '.');
if (!e)
- assert_se(e = strchr(f, 0));
+ e = strchr(f, 0);
a = p - f;
b = strlen(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;
}
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);
- r = new(char, a + strlen(e) + 1);
+ a = p - f;
+
+ 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;
}
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");
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;
*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);