+
+int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) {
+ Unit *other;
+ int r;
+
+ if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
+ return r;
+
+ if ((r = unit_add_dependency(u, d, other)) < 0)
+ return r;
+
+ return 0;
+}
+
+const char *unit_path(void) {
+ char *e;
+
+ if ((e = getenv("UNIT_PATH")))
+ if (path_is_absolute(e))
+ return e;
+
+ return UNIT_PATH;
+}
+
+int set_unit_path(const char *p) {
+ char *cwd, *c;
+ int r;
+
+ /* This is mostly for debug purposes */
+
+ if (path_is_absolute(p)) {
+ if (!(c = strdup(p)))
+ return -ENOMEM;
+ } else {
+ if (!(cwd = get_current_dir_name()))
+ return -errno;
+
+ r = asprintf(&c, "%s/%s", cwd, p);
+ free(cwd);
+
+ if (r < 0)
+ return -ENOMEM;
+ }
+
+ if (setenv("UNIT_PATH", c, 0) < 0) {
+ r = -errno;
+ free(c);
+ return r;
+ }
+
+ return 0;
+}
+
+char *unit_name_escape_path(const char *prefix, const char *path, const char *suffix) {
+ char *r, *t;
+ const char *f;
+ size_t a, b, c;
+
+ assert(path);
+
+ /* Takes a path and a suffix and prefix and makes a nice
+ * string suitable as unit name of it, escaping all weird
+ * chars on the way.
+ *
+ * / becomes ., and all chars not alloweed in a unit name get
+ * escaped as \xFF, including \ and ., of course. This
+ * escaping is hence reversible.
+ */
+
+ if (!prefix)
+ prefix = "";
+
+ if (!suffix)
+ suffix = "";
+
+ a = strlen(prefix);
+ b = strlen(path);
+ c = strlen(suffix);
+
+ if (!(r = new(char, a+b*4+c+1)))
+ return NULL;
+
+ memcpy(r, prefix, a);
+
+ for (f = path, t = r+a; *f; f++) {
+ if (*f == '/')
+ *(t++) = '.';
+ else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
+ *(t++) = '\\';
+ *(t++) = 'x';
+ *(t++) = hexchar(*f > 4);
+ *(t++) = hexchar(*f);
+ } else
+ *(t++) = *f;
+ }
+
+ memcpy(t, suffix, c+1);
+
+ return r;
+}
+
+char *unit_dbus_path(Unit *u) {
+ char *p, *e;
+
+ assert(u);
+
+ if (!(e = bus_path_escape(unit_id(u))))
+ return NULL;
+
+ if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) {
+ free(e);
+ return NULL;
+ }
+
+ free(e);
+ return p;
+}
+
+static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
+ [UNIT_SERVICE] = "service",
+ [UNIT_TIMER] = "timer",
+ [UNIT_SOCKET] = "socket",
+ [UNIT_TARGET] = "target",
+ [UNIT_DEVICE] = "device",
+ [UNIT_MOUNT] = "mount",
+ [UNIT_AUTOMOUNT] = "automount",
+ [UNIT_SNAPSHOT] = "snapshot"
+};
+
+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_FAILED] = "failed"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
+
+static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
+ [UNIT_ACTIVE] = "active",
+ [UNIT_INACTIVE] = "inactive",
+ [UNIT_ACTIVATING] = "activating",
+ [UNIT_DEACTIVATING] = "deactivating"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
+
+static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
+ [UNIT_REQUIRES] = "Requires",
+ [UNIT_SOFT_REQUIRES] = "SoftRequires",
+ [UNIT_WANTS] = "Wants",
+ [UNIT_REQUISITE] = "Requisite",
+ [UNIT_SOFT_REQUISITE] = "SoftRequisite",
+ [UNIT_REQUIRED_BY] = "RequiredBy",
+ [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
+ [UNIT_WANTED_BY] = "WantedBy",
+ [UNIT_CONFLICTS] = "Conflicts",
+ [UNIT_BEFORE] = "Before",
+ [UNIT_AFTER] = "After",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);