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
This commit is contained in:
Lennart Poettering
2015-04-30 20:21:00 +02:00
parent 6442185ab6
commit 7410616cd9
34 changed files with 1066 additions and 867 deletions

View File

@@ -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;
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");
b = unit_has_name(UNIT(a), e);
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);

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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)

View File

@@ -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)

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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))

View File

@@ -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)

View File

@@ -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;

View File

@@ -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;
r = unit_name_to_prefix(u->id, &p);
if (r < 0)
return r;
n = unit_name_unescape(p);
if (!n)
return -ENOMEM;
*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;
return -EINVAL;
n = unit_name_unescape(u->instance);
if (!n)
return -ENOMEM;
*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) {

View File

@@ -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 = 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)
return r;
r = unit_add_two_dependencies(other, d, e, u, add_reference);
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)

View File

@@ -78,9 +78,9 @@ static int create_disk(
if (!e)
return log_oom();
n = unit_name_build("systemd-cryptsetup", e, ".service");
if (!n)
return log_oom();
r = unit_name_build("systemd-cryptsetup", e, ".service", &n);
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
p = strjoin(arg_dest, "/", n, NULL);
if (!p)
@@ -90,9 +90,9 @@ static int create_disk(
if (!u)
return log_oom();
d = unit_name_from_path(u, ".device");
if (!d)
return log_oom();
r = unit_name_from_path(u, ".device", &d);
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
f = fopen(p, "wxe");
if (!f)
@@ -128,11 +128,11 @@ static int create_disk(
if (!path_equal(uu, "/dev/null")) {
if (is_device_path(uu)) {
_cleanup_free_ char *dd;
_cleanup_free_ char *dd = NULL;
dd = unit_name_from_path(uu, ".device");
if (!dd)
return log_oom();
r = unit_name_from_path(uu, ".device", &dd);
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
fprintf(f, "After=%1$s\nRequires=%1$s\n", dd);
} else

View File

@@ -188,7 +188,7 @@ static int add_dbus(const char *path, const char *fname, const char *type) {
}
if (service) {
if (!unit_name_is_valid(service, TEMPLATE_INVALID)) {
if (!unit_name_is_valid(service, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) {
log_warning("Unit name %s is not valid, ignoring.", service);
return 0;
}

View File

@@ -41,9 +41,9 @@ static int parse_proc_cmdline_item(const char *key, const char *value) {
else {
char *n;
n = unit_name_mangle(value, MANGLE_NOGLOB);
if (!n)
return log_oom();
r = unit_name_mangle(value, UNIT_NAME_NOGLOB, &n);
if (r < 0)
return log_error_errno(r, "Failed to glob unit name: %m");
r = strv_consume(&arg_mask, n);
if (r < 0)
@@ -57,9 +57,9 @@ static int parse_proc_cmdline_item(const char *key, const char *value) {
else {
char *n;
n = unit_name_mangle(value, MANGLE_NOGLOB);
if (!n)
return log_oom();
r = unit_name_mangle(value, UNIT_NAME_NOGLOB, &n);
if (r < 0)
return log_error_errno(r, "Failed to glob unit name: %m");
r = strv_consume(&arg_wants, n);
if (r < 0)

View File

@@ -99,7 +99,7 @@ static int parse_argv(int argc, char *argv[]) {
case ARG_TEMPLATE:
if (!unit_name_is_valid(optarg, true) || !unit_name_is_template(optarg)) {
if (!unit_name_is_valid(optarg, UNIT_NAME_TEMPLATE)) {
log_error("Template name %s is not valid.", optarg);
return -EINVAL;
}
@@ -166,22 +166,26 @@ int main(int argc, char *argv[]) {
switch (arg_action) {
case ACTION_ESCAPE:
if (arg_path)
e = unit_name_path_escape(*i);
else
if (arg_path) {
r = unit_name_path_escape(*i, &e);
if (r < 0) {
log_error_errno(r, "Failed to escape string: %m");
goto finish;
}
} else {
e = unit_name_escape(*i);
if (!e) {
r = log_oom();
goto finish;
if (!e) {
r = log_oom();
goto finish;
}
}
if (arg_template) {
char *x;
x = unit_name_replace_instance(arg_template, e);
if (!x) {
r = log_oom();
r = unit_name_replace_instance(arg_template, e, &x);
if (r < 0) {
log_error_errno(r, "Failed to replace instance: %m");
goto finish;
}
@@ -204,20 +208,20 @@ int main(int argc, char *argv[]) {
case ACTION_UNESCAPE:
if (arg_path)
e = unit_name_path_unescape(*i);
r = unit_name_path_unescape(*i, &e);
else
e = unit_name_unescape(*i);
r = unit_name_unescape(*i, &e);
if (!e) {
r = log_oom();
if (r < 0) {
log_error_errno(r, "Failed to unescape string: %m");
goto finish;
}
break;
case ACTION_MANGLE:
e = unit_name_mangle(*i, MANGLE_NOGLOB);
if (!e) {
r = log_oom();
r = unit_name_mangle(*i, UNIT_NAME_NOGLOB, &e);
if (r < 0) {
log_error_errno(r, "Failed to mangle name: %m");
goto finish;
}
break;

View File

@@ -83,9 +83,9 @@ static int add_swap(
opts = filtered;
}
name = unit_name_from_path(what, ".swap");
if (!name)
return log_oom();
r = unit_name_from_path(what, ".swap", &name);
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
unit = strjoin(arg_dest, "/", name, NULL);
if (!unit)
@@ -224,9 +224,9 @@ static int add_mount(
noauto = nofail = automount = false;
}
name = unit_name_from_path(where, ".mount");
if (!name)
return log_oom();
r = unit_name_from_path(where, ".mount", &name);
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
unit = strjoin(arg_dest, "/", name, NULL);
if (!unit)
@@ -290,9 +290,9 @@ static int add_mount(
}
if (automount) {
automount_name = unit_name_from_path(where, ".automount");
if (!automount_name)
return log_oom();
r = unit_name_from_path(where, ".automount", &automount_name);
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
automount_unit = strjoin(arg_dest, "/", automount_name, NULL);
if (!automount_unit)

View File

@@ -50,13 +50,11 @@ static int add_symlink(const char *fservice, const char *tservice) {
r = symlink(from, to);
if (r < 0) {
/* In case console=hvc0 is passed this will very likely result in EEXIST */
if (errno == EEXIST)
/* In case console=hvc0 is passed this will very likely result in EEXIST */
return 0;
else {
log_error_errno(errno, "Failed to create symlink %s: %m", to);
return -errno;
}
return log_error_errno(errno, "Failed to create symlink %s: %m", to);
}
return 0;
@@ -64,28 +62,30 @@ static int add_symlink(const char *fservice, const char *tservice) {
static int add_serial_getty(const char *tty) {
_cleanup_free_ char *n = NULL;
int r;
assert(tty);
log_debug("Automatically adding serial getty for /dev/%s.", tty);
n = unit_name_from_path_instance("serial-getty", tty, ".service");
if (!n)
return log_oom();
r = unit_name_from_path_instance("serial-getty", tty, ".service", &n);
if (r < 0)
return log_error_errno(r, "Failed to generate service name: %m");
return add_symlink("serial-getty@.service", n);
}
static int add_container_getty(const char *tty) {
_cleanup_free_ char *n = NULL;
int r;
assert(tty);
log_debug("Automatically adding container getty for /dev/pts/%s.", tty);
n = unit_name_from_path_instance("container-getty", tty, ".service");
if (!n)
return log_oom();
r = unit_name_from_path_instance("container-getty", tty, ".service", &n);
if (r < 0)
return log_error_errno(r, "Failed to generate service name: %m");
return add_symlink("container-getty@.service", n);
}

View File

@@ -49,14 +49,15 @@ static bool arg_root_rw = false;
static int add_swap(const char *path) {
_cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
assert(path);
log_debug("Adding swap: %s", path);
name = unit_name_from_path(path, ".swap");
if (!name)
return log_oom();
r = unit_name_from_path(path, ".swap", &name);
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
unit = strjoin(arg_dest, "/", name, NULL);
if (!unit)
@@ -100,17 +101,17 @@ static int add_cryptsetup(const char *id, const char *what, bool rw, char **devi
assert(what);
assert(device);
d = unit_name_from_path(what, ".device");
if (!d)
return log_oom();
r = unit_name_from_path(what, ".device", &d);
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
e = unit_name_escape(id);
if (!e)
return log_oom();
n = unit_name_build("systemd-cryptsetup", e, ".service");
if (!n)
return log_oom();
r = unit_name_build("systemd-cryptsetup", e, ".service", &n);
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
p = strjoin(arg_dest, "/", n, NULL);
if (!p)
@@ -224,9 +225,9 @@ static int add_mount(
fstype = NULL;
}
unit = unit_name_from_path(where, ".mount");
if (!unit)
return log_oom();
r = unit_name_from_path(where, ".mount", &unit);
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
p = strjoin(arg_dest, "/", unit, NULL);
if (!p)

View File

@@ -32,6 +32,7 @@ static const char *arg_dest = "/tmp";
static char *arg_resume_dev = NULL;
static int parse_proc_cmdline_item(const char *key, const char *value) {
if (streq(key, "resume") && value) {
free(arg_resume_dev);
arg_resume_dev = fstab_node_to_udev_node(value);
@@ -44,13 +45,14 @@ static int parse_proc_cmdline_item(const char *key, const char *value) {
static int process_resume(void) {
_cleanup_free_ char *name = NULL, *lnk = NULL;
int r;
if (!arg_resume_dev)
return 0;
name = unit_name_from_path_instance("systemd-hibernate-resume", arg_resume_dev, ".service");
if (!name)
return log_oom();
r = unit_name_from_path_instance("systemd-hibernate-resume", arg_resume_dev, ".service", &name);
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
lnk = strjoin(arg_dest, "/" SPECIAL_SYSINIT_TARGET ".wants/", name, NULL);
if (!lnk)

Some files were not shown because too many files have changed in this diff Show More