summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-04-30 20:21:00 +0200
committerLennart Poettering <lennart@poettering.net>2015-05-05 15:06:42 -0700
commit7410616cd9dbbec97cf98d75324da5cda2b2f7a2 (patch)
tree6d968995b3bdf961603ab4853bf078c0dbdce27c /src/core
parent6442185ab674cc202d63c18605057b9a51ca2722 (diff)
core: rework unit name validation and manipulation logic
A variety of changes: - Make sure all our calls distuingish OOM from other errors if OOM is not the only error possible. - Be much stricter when parsing escaped paths, do not accept trailing or leading escaped slashes. - Change unit validation to take a bit mask for allowing plain names, instance names or template names or an combination thereof. - Refuse manipulating invalid unit name
Diffstat (limited to 'src/core')
-rw-r--r--src/core/automount.c19
-rw-r--r--src/core/busname.c6
-rw-r--r--src/core/dbus-unit.c4
-rw-r--r--src/core/device.c19
-rw-r--r--src/core/load-fragment.c16
-rw-r--r--src/core/manager.c17
-rw-r--r--src/core/mount.c23
-rw-r--r--src/core/snapshot.c4
-rw-r--r--src/core/socket.c23
-rw-r--r--src/core/swap.c39
-rw-r--r--src/core/unit-printf.c55
-rw-r--r--src/core/unit.c160
12 files changed, 179 insertions, 206 deletions
diff --git a/src/core/automount.c b/src/core/automount.c
index 73b75f163e..1806fa39d3 100644
--- a/src/core/automount.c
+++ b/src/core/automount.c
@@ -167,8 +167,9 @@ static int automount_add_default_dependencies(Automount *a) {
}
static int automount_verify(Automount *a) {
- bool b;
_cleanup_free_ char *e = NULL;
+ int r;
+
assert(a);
if (UNIT(a)->load_state != UNIT_LOADED)
@@ -179,13 +180,11 @@ static int automount_verify(Automount *a) {
return -EINVAL;
}
- e = unit_name_from_path(a->where, ".automount");
- if (!e)
- return -ENOMEM;
-
- b = unit_has_name(UNIT(a), e);
+ r = unit_name_from_path(a->where, ".automount", &e);
+ if (r < 0)
+ return log_unit_error(UNIT(a)->id, "Failed to generate unit name from path: %m");
- if (!b) {
+ if (!unit_has_name(UNIT(a), e)) {
log_unit_error(UNIT(a)->id, "%s's Where setting doesn't match unit name. Refusing.", UNIT(a)->id);
return -EINVAL;
}
@@ -209,9 +208,9 @@ static int automount_load(Unit *u) {
Unit *x;
if (!a->where) {
- a->where = unit_name_to_path(u->id);
- if (!a->where)
- return -ENOMEM;
+ r = unit_name_to_path(u->id, &a->where);
+ if (r < 0)
+ return r;
}
path_kill_slashes(a->where);
diff --git a/src/core/busname.c b/src/core/busname.c
index 20d49fe3e9..8e92fb47fe 100644
--- a/src/core/busname.c
+++ b/src/core/busname.c
@@ -163,9 +163,9 @@ static int busname_add_extras(BusName *n) {
assert(n);
if (!n->name) {
- n->name = unit_name_to_prefix(u->id);
- if (!n->name)
- return -ENOMEM;
+ r = unit_name_to_prefix(u->id, &n->name);
+ if (r < 0)
+ return r;
}
if (!u->description) {
diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c
index 2699e81492..056c7117b2 100644
--- a/src/core/dbus-unit.c
+++ b/src/core/dbus-unit.c
@@ -940,7 +940,7 @@ static int bus_unit_set_transient_property(
if (r < 0)
return r;
- if (!unit_name_is_valid(s, TEMPLATE_INVALID) || !endswith(s, ".slice"))
+ if (!unit_name_is_valid(s, UNIT_NAME_PLAIN) || !endswith(s, ".slice"))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid slice name %s", s);
if (isempty(s)) {
@@ -988,7 +988,7 @@ static int bus_unit_set_transient_property(
return r;
while ((r = sd_bus_message_read(message, "s", &other)) > 0) {
- if (!unit_name_is_valid(other, TEMPLATE_INVALID))
+ if (!unit_name_is_valid(other, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name %s", other);
if (mode != UNIT_CHECK) {
diff --git a/src/core/device.c b/src/core/device.c
index f8e65b876f..2c56c5a2d2 100644
--- a/src/core/device.c
+++ b/src/core/device.c
@@ -279,9 +279,9 @@ static int device_add_udev_wants(Unit *u, struct udev_device *dev) {
memcpy(e, word, l);
e[l] = 0;
- n = unit_name_mangle(e, MANGLE_NOGLOB);
- if (!n)
- return log_oom();
+ r = unit_name_mangle(e, UNIT_NAME_NOGLOB, &n);
+ if (r < 0)
+ return log_unit_error_errno(u->id, r, "Failed to mangle unit name: %m");
r = unit_add_dependency_by_name(u, UNIT_WANTS, n, NULL, true);
if (r < 0)
@@ -308,9 +308,9 @@ static int device_setup_unit(Manager *m, struct udev_device *dev, const char *pa
if (!sysfs)
return 0;
- e = unit_name_from_path(path, ".device");
- if (!e)
- return log_oom();
+ r = unit_name_from_path(path, ".device", &e);
+ if (r < 0)
+ return log_unit_error_errno(u->id, r, "Failed to generate device name: %m");
u = manager_get_unit(m, e);
@@ -491,6 +491,7 @@ static int device_update_found_by_sysfs(Manager *m, const char *sysfs, bool add,
static int device_update_found_by_name(Manager *m, const char *path, bool add, DeviceFound found, bool now) {
_cleanup_free_ char *e = NULL;
Unit *u;
+ int r;
assert(m);
assert(path);
@@ -498,9 +499,9 @@ static int device_update_found_by_name(Manager *m, const char *path, bool add, D
if (found == DEVICE_NOT_FOUND)
return 0;
- e = unit_name_from_path(path, ".device");
- if (!e)
- return log_oom();
+ r = unit_name_from_path(path, ".device", &e);
+ if (r < 0)
+ return log_error_errno(r, "Failed to generate unit name from device path: %m");
u = manager_get_unit(m, e);
if (!u)
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
index b76656b931..970120ab70 100644
--- a/src/core/load-fragment.c
+++ b/src/core/load-fragment.c
@@ -3394,7 +3394,7 @@ static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
* unit name. */
name = basename(*filename);
- if (unit_name_is_valid(name, TEMPLATE_VALID)) {
+ if (unit_name_is_valid(name, UNIT_NAME_ANY)) {
id = set_get(names, name);
if (!id) {
@@ -3642,11 +3642,11 @@ int unit_load_fragment(Unit *u) {
/* Look for a template */
if (u->load_state == UNIT_STUB && u->instance) {
- _cleanup_free_ char *k;
+ _cleanup_free_ char *k = NULL;
- k = unit_name_template(u->id);
- if (!k)
- return -ENOMEM;
+ r = unit_name_template(u->id, &k);
+ if (r < 0)
+ return r;
r = load_from_path(u, k);
if (r < 0)
@@ -3659,9 +3659,9 @@ int unit_load_fragment(Unit *u) {
if (t == u->id)
continue;
- z = unit_name_template(t);
- if (!z)
- return -ENOMEM;
+ r = unit_name_template(t, &z);
+ if (r < 0)
+ return r;
r = load_from_path(u, z);
if (r < 0)
diff --git a/src/core/manager.c b/src/core/manager.c
index b49452151b..8c8645b68d 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -1306,7 +1306,7 @@ int manager_load_unit_prepare(
t = unit_name_to_type(name);
- if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, TEMPLATE_INVALID))
+ if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name);
ret = manager_get_unit(m, name);
@@ -2093,7 +2093,7 @@ void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
#ifdef HAVE_AUDIT
_cleanup_free_ char *p = NULL;
const char *msg;
- int audit_fd;
+ int audit_fd, r;
audit_fd = get_audit_fd();
if (audit_fd < 0)
@@ -2110,9 +2110,9 @@ void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
if (u->type != UNIT_SERVICE)
return;
- p = unit_name_to_prefix_and_instance(u->id);
- if (!p) {
- log_oom();
+ r = unit_name_to_prefix_and_instance(u->id, &p);
+ if (r < 0) {
+ log_error_errno(r, "Failed to extract prefix and instance of unit name: %m");
return;
}
@@ -3027,15 +3027,16 @@ void manager_status_printf(Manager *m, StatusType type, const char *status, cons
int manager_get_unit_by_path(Manager *m, const char *path, const char *suffix, Unit **_found) {
_cleanup_free_ char *p = NULL;
Unit *found;
+ int r;
assert(m);
assert(path);
assert(suffix);
assert(_found);
- p = unit_name_from_path(path, suffix);
- if (!p)
- return -ENOMEM;
+ r = unit_name_from_path(path, suffix, &p);
+ if (r < 0)
+ return r;
found = manager_get_unit(m, p);
if (!found) {
diff --git a/src/core/mount.c b/src/core/mount.c
index d0c41a73a9..65a66b468f 100644
--- a/src/core/mount.c
+++ b/src/core/mount.c
@@ -435,7 +435,7 @@ static int mount_add_default_dependencies(Mount *m) {
static int mount_verify(Mount *m) {
_cleanup_free_ char *e = NULL;
- bool b;
+ int r;
assert(m);
@@ -445,12 +445,11 @@ static int mount_verify(Mount *m) {
if (!m->from_fragment && !m->from_proc_self_mountinfo)
return -ENOENT;
- e = unit_name_from_path(m->where, ".mount");
- if (!e)
- return -ENOMEM;
+ r = unit_name_from_path(m->where, ".mount", &e);
+ if (r < 0)
+ return log_unit_error_errno(UNIT(m)->id, r, "Failed to generate unit name from mount path: %m");
- b = unit_has_name(UNIT(m), e);
- if (!b) {
+ if (!unit_has_name(UNIT(m), e)) {
log_unit_error(UNIT(m)->id, "%s's Where= setting doesn't match unit name. Refusing.", UNIT(m)->id);
return -EINVAL;
}
@@ -483,9 +482,9 @@ static int mount_add_extras(Mount *m) {
m->from_fragment = true;
if (!m->where) {
- m->where = unit_name_to_path(u->id);
- if (!m->where)
- return -ENOMEM;
+ r = unit_name_to_path(u->id, &m->where);
+ if (r < 0)
+ return r;
}
path_kill_slashes(m->where);
@@ -1419,9 +1418,9 @@ static int mount_setup_unit(
if (!is_path(where))
return 0;
- e = unit_name_from_path(where, ".mount");
- if (!e)
- return -ENOMEM;
+ r = unit_name_from_path(where, ".mount", &e);
+ if (r < 0)
+ return r;
u = manager_get_unit(m, e);
if (!u) {
diff --git a/src/core/snapshot.c b/src/core/snapshot.c
index b70c3beb60..2c00680f16 100644
--- a/src/core/snapshot.c
+++ b/src/core/snapshot.c
@@ -201,10 +201,10 @@ int snapshot_create(Manager *m, const char *name, bool cleanup, sd_bus_error *e,
assert(_s);
if (name) {
- if (!unit_name_is_valid(name, TEMPLATE_INVALID))
+ if (!unit_name_is_valid(name, UNIT_NAME_PLAIN))
return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name);
- if (unit_name_to_type(name) != UNIT_SNAPSHOT)
+ if (!endswith(name, ".snapshot"))
return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s lacks snapshot suffix.", name);
if (manager_get_unit(m, name))
diff --git a/src/core/socket.c b/src/core/socket.c
index 67beda44d6..55334e4a54 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -196,12 +196,15 @@ int socket_instantiate_service(Socket *s) {
* here. For Accept=no this is mostly a NOP since the service
* is figured out at load time anyway. */
- if (UNIT_DEREF(s->service) || !s->accept)
+ if (UNIT_DEREF(s->service))
return 0;
- prefix = unit_name_to_prefix(UNIT(s)->id);
- if (!prefix)
- return -ENOMEM;
+ if (!s->accept)
+ return 0;
+
+ r = unit_name_to_prefix(UNIT(s)->id, &prefix);
+ if (r < 0)
+ return r;
if (asprintf(&name, "%s@%u.service", prefix, s->n_accepted) < 0)
return -ENOMEM;
@@ -1836,17 +1839,13 @@ static void socket_enter_running(Socket *s, int cfd) {
return;
}
- prefix = unit_name_to_prefix(UNIT(s)->id);
- if (!prefix) {
- r = -ENOMEM;
+ r = unit_name_to_prefix(UNIT(s)->id, &prefix);
+ if (r < 0)
goto fail;
- }
- name = unit_name_build(prefix, instance, ".service");
- if (!name) {
- r = -ENOMEM;
+ r = unit_name_build(prefix, instance, ".service", &name);
+ if (r < 0)
goto fail;
- }
r = unit_add_name(UNIT_DEREF(s->service), name);
if (r < 0)
diff --git a/src/core/swap.c b/src/core/swap.c
index a9834a7fe5..3327cd958f 100644
--- a/src/core/swap.c
+++ b/src/core/swap.c
@@ -222,18 +222,17 @@ static int swap_add_default_dependencies(Swap *s) {
}
static int swap_verify(Swap *s) {
- bool b;
_cleanup_free_ char *e = NULL;
+ int r;
if (UNIT(s)->load_state != UNIT_LOADED)
return 0;
- e = unit_name_from_path(s->what, ".swap");
- if (!e)
- return log_oom();
+ r = unit_name_from_path(s->what, ".swap", &e);
+ if (r < 0)
+ return log_unit_error_errno(UNIT(s)->id, r, "%s: failed to generate unit name from path: %m", UNIT(s)->id);
- b = unit_has_name(UNIT(s), e);
- if (!b) {
+ if (!unit_has_name(UNIT(s), e)) {
log_unit_error(UNIT(s)->id, "%s: Value of \"What\" and unit name do not match, not loading.", UNIT(s)->id);
return -EINVAL;
}
@@ -289,8 +288,11 @@ static int swap_load(Unit *u) {
s->what = strdup(s->parameters_fragment.what);
else if (s->parameters_proc_swaps.what)
s->what = strdup(s->parameters_proc_swaps.what);
- else
- s->what = unit_name_to_path(u->id);
+ else {
+ r = unit_name_to_path(u->id, &s->what);
+ if (r < 0)
+ return r;
+ }
if (!s->what)
return -ENOMEM;
@@ -355,9 +357,9 @@ static int swap_setup_unit(
assert(what);
assert(what_proc_swaps);
- e = unit_name_from_path(what, ".swap");
- if (!e)
- return log_oom();
+ r = unit_name_from_path(what, ".swap", &e);
+ if (r < 0)
+ return log_unit_error_errno(u->id, r, "Failed to generate unit name from path: %m");
u = manager_get_unit(m, e);
@@ -1329,9 +1331,9 @@ int swap_process_device_new(Manager *m, struct udev_device *dev) {
if (!dn)
return 0;
- e = unit_name_from_path(dn, ".swap");
- if (!e)
- return -ENOMEM;
+ r = unit_name_from_path(dn, ".swap", &e);
+ if (r < 0)
+ return r;
s = hashmap_get(m->units, e);
if (s)
@@ -1340,15 +1342,14 @@ int swap_process_device_new(Manager *m, struct udev_device *dev) {
first = udev_device_get_devlinks_list_entry(dev);
udev_list_entry_foreach(item, first) {
_cleanup_free_ char *n = NULL;
+ int q;
- n = unit_name_from_path(udev_list_entry_get_name(item), ".swap");
- if (!n)
- return -ENOMEM;
+ q = unit_name_from_path(udev_list_entry_get_name(item), ".swap", &n);
+ if (q < 0)
+ return q;
s = hashmap_get(m->units, n);
if (s) {
- int q;
-
q = swap_set_devnode(s, dn);
if (q < 0)
r = q;
diff --git a/src/core/unit-printf.c b/src/core/unit-printf.c
index 5513fe7d9c..8050e2fd66 100644
--- a/src/core/unit-printf.c
+++ b/src/core/unit-printf.c
@@ -30,83 +30,54 @@
static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
- char *n;
assert(u);
- n = unit_name_to_prefix_and_instance(u->id);
- if (!n)
- return -ENOMEM;
-
- *ret = n;
- return 0;
+ return unit_name_to_prefix_and_instance(u->id, ret);
}
static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
- char *n;
assert(u);
- n = unit_name_to_prefix(u->id);
- if (!n)
- return -ENOMEM;
-
- *ret = n;
- return 0;
+ return unit_name_to_prefix(u->id, ret);
}
static int specifier_prefix_unescaped(char specifier, void *data, void *userdata, char **ret) {
- Unit *u = userdata;
_cleanup_free_ char *p = NULL;
- char *n;
+ Unit *u = userdata;
+ int r;
assert(u);
- p = unit_name_to_prefix(u->id);
- if (!p)
- return -ENOMEM;
-
- n = unit_name_unescape(p);
- if (!n)
- return -ENOMEM;
+ r = unit_name_to_prefix(u->id, &p);
+ if (r < 0)
+ return r;
- *ret = n;
- return 0;
+ return unit_name_unescape(p, ret);
}
static int specifier_instance_unescaped(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
- char *n;
assert(u);
if (!u->instance)
- return -EOPNOTSUPP;
-
- n = unit_name_unescape(u->instance);
- if (!n)
- return -ENOMEM;
+ return -EINVAL;
- *ret = n;
- return 0;
+ return unit_name_unescape(u->instance, ret);
}
static int specifier_filename(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
- char *n;
assert(u);
if (u->instance)
- n = unit_name_path_unescape(u->instance);
+ return unit_name_path_unescape(u->instance, ret);
else
- n = unit_name_to_path(u->id);
- if (!n)
- return -ENOMEM;
-
- *ret = n;
- return 0;
+ return unit_name_to_path(u->id, ret);
}
static int specifier_cgroup(char specifier, void *data, void *userdata, char **ret) {
@@ -195,7 +166,7 @@ static int specifier_user_name(char specifier, void *data, void *userdata, char
c = unit_get_exec_context(u);
if (!c)
- return -EOPNOTSUPP;
+ return -EINVAL;
if (u->manager->running_as == SYSTEMD_SYSTEM) {
diff --git a/src/core/unit.c b/src/core/unit.c
index 6071bd51b1..6513bcb1cb 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -143,21 +143,31 @@ int unit_add_name(Unit *u, const char *text) {
assert(u);
assert(text);
- if (unit_name_is_template(text)) {
+ if (unit_name_is_valid(text, UNIT_NAME_TEMPLATE)) {
if (!u->instance)
return -EINVAL;
- s = unit_name_replace_instance(text, u->instance);
- } else
+ r = unit_name_replace_instance(text, u->instance, &s);
+ if (r < 0)
+ return r;
+ } else {
s = strdup(text);
- if (!s)
- return -ENOMEM;
+ if (!s)
+ return -ENOMEM;
+ }
- if (!unit_name_is_valid(s, TEMPLATE_INVALID))
+ if (set_contains(u->names, s))
+ return 0;
+ if (hashmap_contains(u->manager->units, s))
+ return -EEXIST;
+
+ if (!unit_name_is_valid(s, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
return -EINVAL;
- assert_se((t = unit_name_to_type(s)) >= 0);
+ t = unit_name_to_type(s);
+ if (t < 0)
+ return -EINVAL;
if (u->type != _UNIT_TYPE_INVALID && t != u->type)
return -EINVAL;
@@ -170,25 +180,25 @@ int unit_add_name(Unit *u, const char *text) {
return -EINVAL;
/* Ensure that this unit is either instanced or not instanced,
- * but not both. */
+ * but not both. Note that we do allow names with different
+ * instance names however! */
if (u->type != _UNIT_TYPE_INVALID && !u->instance != !i)
return -EINVAL;
- if (unit_vtable[t]->no_alias &&
- !set_isempty(u->names) &&
- !set_get(u->names, s))
+ if (unit_vtable[t]->no_alias && !set_isempty(u->names))
return -EEXIST;
if (hashmap_size(u->manager->units) >= MANAGER_MAX_NAMES)
return -E2BIG;
r = set_put(u->names, s);
- if (r <= 0)
+ if (r < 0)
return r;
+ assert(r > 0);
r = hashmap_put(u->manager->units, s, u);
if (r < 0) {
- set_remove(u->names, s);
+ (void) set_remove(u->names, s);
return r;
}
@@ -218,14 +228,14 @@ int unit_choose_id(Unit *u, const char *name) {
assert(u);
assert(name);
- if (unit_name_is_template(name)) {
+ if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) {
if (!u->instance)
return -EINVAL;
- t = unit_name_replace_instance(name, u->instance);
- if (!t)
- return -ENOMEM;
+ r = unit_name_replace_instance(name, u->instance, &t);
+ if (r < 0)
+ return r;
name = t;
}
@@ -235,6 +245,7 @@ int unit_choose_id(Unit *u, const char *name) {
if (!s)
return -ENOENT;
+ /* Determine the new instance from the new id */
r = unit_name_to_instance(s, &i);
if (r < 0)
return r;
@@ -748,24 +759,22 @@ int unit_merge_by_name(Unit *u, const char *name) {
assert(u);
assert(name);
- if (unit_name_is_template(name)) {
+ if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) {
if (!u->instance)
return -EINVAL;
- s = unit_name_replace_instance(name, u->instance);
- if (!s)
- return -ENOMEM;
+ r = unit_name_replace_instance(name, u->instance, &s);
+ if (r < 0)
+ return r;
name = s;
}
other = manager_get_unit(u->manager, name);
- if (!other)
- r = unit_add_name(u, name);
- else
- r = unit_merge(u, other);
+ if (other)
+ return unit_merge(u, other);
- return r;
+ return unit_add_name(u, name);
}
Unit* unit_follow_merge(Unit *u) {
@@ -2294,58 +2303,55 @@ int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit
if (r < 0)
return r;
- r = unit_add_dependency(u, e, other, add_reference);
- if (r < 0)
- return r;
-
- return 0;
+ return unit_add_dependency(u, e, other, add_reference);
}
-static const char *resolve_template(Unit *u, const char *name, const char*path, char **p) {
- char *s;
+static int resolve_template(Unit *u, const char *name, const char*path, char **buf, const char **ret) {
+ int r;
assert(u);
assert(name || path);
- assert(p);
+ assert(buf);
+ assert(ret);
if (!name)
name = basename(path);
- if (!unit_name_is_template(name)) {
- *p = NULL;
- return name;
+ if (!unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) {
+ *buf = NULL;
+ *ret = name;
+ return 0;
}
if (u->instance)
- s = unit_name_replace_instance(name, u->instance);
+ r = unit_name_replace_instance(name, u->instance, buf);
else {
_cleanup_free_ char *i = NULL;
- i = unit_name_to_prefix(u->id);
- if (!i)
- return NULL;
+ r = unit_name_to_prefix(u->id, &i);
+ if (r < 0)
+ return r;
- s = unit_name_replace_instance(name, i);
+ r = unit_name_replace_instance(name, i, buf);
}
+ if (r < 0)
+ return r;
- if (!s)
- return NULL;
-
- *p = s;
- return s;
+ *ret = *buf;
+ return 0;
}
int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
+ _cleanup_free_ char *buf = NULL;
Unit *other;
int r;
- _cleanup_free_ char *s = NULL;
assert(u);
assert(name || path);
- name = resolve_template(u, name, path, &s);
- if (!name)
- return -ENOMEM;
+ r = resolve_template(u, name, path, &buf, &name);
+ if (r < 0)
+ return r;
r = manager_load_unit(u->manager, name, path, NULL, &other);
if (r < 0)
@@ -2355,16 +2361,16 @@ int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, con
}
int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
- _cleanup_free_ char *s = NULL;
+ _cleanup_free_ char *buf = NULL;
Unit *other;
int r;
assert(u);
assert(name || path);
- name = resolve_template(u, name, path, &s);
- if (!name)
- return -ENOMEM;
+ r = resolve_template(u, name, path, &buf, &name);
+ if (r < 0)
+ return r;
r = manager_load_unit(u->manager, name, path, NULL, &other);
if (r < 0)
@@ -2374,16 +2380,16 @@ int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency
}
int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
+ _cleanup_free_ char *buf = NULL;
Unit *other;
int r;
- _cleanup_free_ char *s = NULL;
assert(u);
assert(name || path);
- name = resolve_template(u, name, path, &s);
- if (!name)
- return -ENOMEM;
+ r = resolve_template(u, name, path, &buf, &name);
+ if (r < 0)
+ return r;
r = manager_load_unit(u->manager, name, path, NULL, &other);
if (r < 0)
@@ -2393,26 +2399,22 @@ int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *n
}
int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
+ _cleanup_free_ char *buf = NULL;
Unit *other;
int r;
- _cleanup_free_ char *s = NULL;
assert(u);
assert(name || path);
- name = resolve_template(u, name, path, &s);
- if (!name)
- return -ENOMEM;
-
- r = manager_load_unit(u->manager, name, path, NULL, &other);
+ r = resolve_template(u, name, path, &buf, &name);
if (r < 0)
return r;
- r = unit_add_two_dependencies(other, d, e, u, add_reference);
+ r = manager_load_unit(u->manager, name, path, NULL, &other);
if (r < 0)
return r;
- return r;
+ return unit_add_two_dependencies(other, d, e, u, add_reference);
}
int set_unit_path(const char *p) {
@@ -2475,14 +2477,14 @@ int unit_add_default_slice(Unit *u, CGroupContext *c) {
/* Implicitly place all instantiated units in their
* own per-template slice */
- prefix = unit_name_to_prefix(u->id);
- if (!prefix)
- return -ENOMEM;
+ r = unit_name_to_prefix(u->id, &prefix);
+ if (r < 0)
+ return r;
/* The prefix is already escaped, but it might include
* "-" which has a special meaning for slice units,
* hence escape it here extra. */
- escaped = strreplace(prefix, "-", "\\x2d");
+ escaped = unit_name_escape(prefix);
if (!escaped)
return -ENOMEM;
@@ -2525,11 +2527,11 @@ int unit_load_related_unit(Unit *u, const char *type, Unit **_found) {
assert(type);
assert(_found);
- t = unit_name_change_suffix(u->id, type);
- if (!t)
- return -ENOMEM;
-
- assert(!unit_has_name(u, t));
+ r = unit_name_change_suffix(u->id, type, &t);
+ if (r < 0)
+ return r;
+ if (unit_has_name(u, t))
+ return -EINVAL;
r = manager_load_unit(u->manager, t, NULL, NULL, _found);
assert(r < 0 || *_found != u);
@@ -2858,9 +2860,9 @@ int unit_add_node_link(Unit *u, const char *what, bool wants) {
if (!unit_type_supported(UNIT_DEVICE))
return 0;
- e = unit_name_from_path(what, ".device");
- if (!e)
- return -ENOMEM;
+ r = unit_name_from_path(what, ".device", &e);
+ if (r < 0)
+ return r;
r = manager_load_unit(u->manager, e, NULL, NULL, &device);
if (r < 0)