1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-02 02:21:44 +03:00

unit: unify some code with new unit_new_for_name() call

This commit is contained in:
Lennart Poettering 2016-10-25 00:29:05 +02:00
parent 11222d0fe0
commit a581e45ae8
7 changed files with 28 additions and 42 deletions

View File

@ -331,11 +331,7 @@ static int device_setup_unit(Manager *m, struct udev_device *dev, const char *pa
if (!u) { if (!u) {
delete = true; delete = true;
u = unit_new(m, sizeof(Device)); r = unit_new_for_name(m, sizeof(Device), e, &u);
if (!u)
return log_oom();
r = unit_add_name(u, e);
if (r < 0) if (r < 0)
goto fail; goto fail;

View File

@ -1404,11 +1404,7 @@ static int mount_setup_unit(
if (!u) { if (!u) {
delete = true; delete = true;
u = unit_new(m, sizeof(Mount)); r = unit_new_for_name(m, sizeof(Mount), e, &u);
if (!u)
return log_oom();
r = unit_add_name(u, e);
if (r < 0) if (r < 0)
goto fail; goto fail;
@ -1614,16 +1610,9 @@ static void synthesize_root_mount(Manager *m) {
u = manager_get_unit(m, SPECIAL_ROOT_MOUNT); u = manager_get_unit(m, SPECIAL_ROOT_MOUNT);
if (!u) { if (!u) {
u = unit_new(m, sizeof(Mount)); r = unit_new_for_name(m, sizeof(Mount), SPECIAL_ROOT_MOUNT, &u);
if (!u) {
log_oom();
return;
}
r = unit_add_name(u, SPECIAL_ROOT_MOUNT);
if (r < 0) { if (r < 0) {
unit_free(u); log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_MOUNT " unit: %m");
log_error_errno(r, "Failed to add the " SPECIAL_ROOT_MOUNT " name: %m");
return; return;
} }
} }

View File

@ -563,16 +563,9 @@ static void scope_enumerate(Manager *m) {
u = manager_get_unit(m, SPECIAL_INIT_SCOPE); u = manager_get_unit(m, SPECIAL_INIT_SCOPE);
if (!u) { if (!u) {
u = unit_new(m, sizeof(Scope)); r = unit_new_for_name(m, sizeof(Scope), SPECIAL_INIT_SCOPE, &u);
if (!u) {
log_oom();
return;
}
r = unit_add_name(u, SPECIAL_INIT_SCOPE);
if (r < 0) { if (r < 0) {
unit_free(u); log_error_errno(r, "Failed to allocate the special " SPECIAL_INIT_SCOPE " unit: %m");
log_error_errno(r, "Failed to add the " SPECIAL_INIT_SCOPE " name: %m");
return; return;
} }
} }

View File

@ -299,16 +299,9 @@ static void slice_enumerate(Manager *m) {
u = manager_get_unit(m, SPECIAL_ROOT_SLICE); u = manager_get_unit(m, SPECIAL_ROOT_SLICE);
if (!u) { if (!u) {
u = unit_new(m, sizeof(Slice)); r = unit_new_for_name(m, sizeof(Slice), SPECIAL_ROOT_SLICE, &u);
if (!u) {
log_oom();
return;
}
r = unit_add_name(u, SPECIAL_ROOT_SLICE);
if (r < 0) { if (r < 0) {
unit_free(u); log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_SLICE " unit: %m");
log_error_errno(r, "Failed to add the " SPECIAL_ROOT_SLICE " name: %m");
return; return;
} }
} }

View File

@ -381,11 +381,7 @@ static int swap_setup_unit(
if (!u) { if (!u) {
delete = true; delete = true;
u = unit_new(m, sizeof(Swap)); r = unit_new_for_name(m, sizeof(Swap), e, &u);
if (!u)
return log_oom();
r = unit_add_name(u, e);
if (r < 0) if (r < 0)
goto fail; goto fail;

View File

@ -109,6 +109,24 @@ Unit *unit_new(Manager *m, size_t size) {
return u; return u;
} }
int unit_new_for_name(Manager *m, size_t size, const char *name, Unit **ret) {
Unit *u;
int r;
u = unit_new(m, size);
if (!u)
return -ENOMEM;
r = unit_add_name(u, name);
if (r < 0) {
unit_free(u);
return r;
}
*ret = u;
return r;
}
bool unit_has_name(Unit *u, const char *name) { bool unit_has_name(Unit *u, const char *name) {
assert(u); assert(u);
assert(name); assert(name);

View File

@ -481,6 +481,7 @@ DEFINE_CAST(SCOPE, Scope);
Unit *unit_new(Manager *m, size_t size); Unit *unit_new(Manager *m, size_t size);
void unit_free(Unit *u); void unit_free(Unit *u);
int unit_new_for_name(Manager *m, size_t size, const char *name, Unit **ret);
int unit_add_name(Unit *u, const char *name); int unit_add_name(Unit *u, const char *name);
int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference); int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference);