1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-11 05:17:44 +03:00

shared/install: use cleanup func for InstallInfo*

In the next commit cleanup will be used in one more place. This change
avoids proliferation of the open-coded cleanup calls.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2022-08-29 10:11:41 +02:00
parent 0047d54d42
commit acb5b83438

View File

@ -1040,22 +1040,28 @@ static int find_symlinks_in_scope(
return 0; return 0;
} }
static void install_info_free(InstallInfo *i) { static void install_info_clear(InstallInfo *i) {
if (!i) if (!i)
return; return;
free(i->name); i->name = mfree(i->name);
free(i->path); i->path = mfree(i->path);
free(i->root); i->root = mfree(i->root);
strv_free(i->aliases); i->aliases = strv_free(i->aliases);
strv_free(i->wanted_by); i->wanted_by = strv_free(i->wanted_by);
strv_free(i->required_by); i->required_by = strv_free(i->required_by);
strv_free(i->also); i->also = strv_free(i->also);
free(i->default_instance); i->default_instance = mfree(i->default_instance);
free(i->symlink_target); i->symlink_target = mfree(i->symlink_target);
free(i);
} }
static InstallInfo* install_info_free(InstallInfo *i) {
install_info_clear(i);
return mfree(i);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(InstallInfo*, install_info_free);
static void install_context_done(InstallContext *ctx) { static void install_context_done(InstallContext *ctx) {
assert(ctx); assert(ctx);
@ -1111,7 +1117,6 @@ static int install_info_add(
bool auxiliary, bool auxiliary,
InstallInfo **ret) { InstallInfo **ret) {
InstallInfo *i = NULL;
int r; int r;
assert(ctx); assert(ctx);
@ -1127,7 +1132,7 @@ static int install_info_add(
if (!unit_name_is_valid(name, UNIT_NAME_ANY)) if (!unit_name_is_valid(name, UNIT_NAME_ANY))
return -EINVAL; return -EINVAL;
i = install_info_find(ctx, name); InstallInfo *i = install_info_find(ctx, name);
if (i) { if (i) {
i->auxiliary = i->auxiliary && auxiliary; i->auxiliary = i->auxiliary && auxiliary;
@ -1136,49 +1141,39 @@ static int install_info_add(
return 0; return 0;
} }
i = new(InstallInfo, 1); _cleanup_(install_info_freep) InstallInfo *alloc = new(InstallInfo, 1);
if (!i) if (!alloc)
return -ENOMEM; return -ENOMEM;
*i = (InstallInfo) { *alloc = (InstallInfo) {
.install_mode = _INSTALL_MODE_INVALID, .install_mode = _INSTALL_MODE_INVALID,
.auxiliary = auxiliary, .auxiliary = auxiliary,
}; };
i->name = strdup(name); alloc->name = strdup(name);
if (!i->name) { if (!alloc->name)
r = -ENOMEM; return -ENOMEM;
goto fail;
}
if (root) { if (root) {
i->root = strdup(root); alloc->root = strdup(root);
if (!i->root) { if (!alloc->root)
r = -ENOMEM; return -ENOMEM;
goto fail;
}
} }
if (path) { if (path) {
i->path = strdup(path); alloc->path = strdup(path);
if (!i->path) { if (!alloc->path)
r = -ENOMEM; return -ENOMEM;
goto fail;
}
} }
r = ordered_hashmap_ensure_put(&ctx->will_process, &string_hash_ops, i->name, i); r = ordered_hashmap_ensure_put(&ctx->will_process, &string_hash_ops, alloc->name, alloc);
if (r < 0) if (r < 0)
goto fail; return r;
i = TAKE_PTR(alloc);
if (ret) if (ret)
*ret = i; *ret = i;
return 1; return 1;
fail:
install_info_free(i);
return r;
} }
static int config_parse_alias( static int config_parse_alias(
@ -1924,7 +1919,10 @@ static int install_info_symlink_wants(
InstallChange **changes, InstallChange **changes,
size_t *n_changes) { size_t *n_changes) {
_cleanup_free_ char *buf = NULL; _cleanup_(install_info_clear) InstallInfo instance = {
.install_mode = _INSTALL_MODE_INVALID,
};
UnitNameFlags valid_dst_type = UNIT_NAME_ANY; UnitNameFlags valid_dst_type = UNIT_NAME_ANY;
const char *n; const char *n;
int r = 0, q; int r = 0, q;
@ -1941,30 +1939,22 @@ static int install_info_symlink_wants(
n = info->name; n = info->name;
else if (info->default_instance) { else if (info->default_instance) {
InstallInfo instance = {
.install_mode = _INSTALL_MODE_INVALID,
};
_cleanup_free_ char *path = NULL;
/* If this is a template, and we have a default instance, use it. */ /* If this is a template, and we have a default instance, use it. */
r = unit_name_replace_instance(info->name, info->default_instance, &buf); r = unit_name_replace_instance(info->name, info->default_instance, &instance.name);
if (r < 0) if (r < 0)
return r; return r;
instance.name = buf;
r = unit_file_search(NULL, &instance, lp, SEARCH_FOLLOW_CONFIG_SYMLINKS); r = unit_file_search(NULL, &instance, lp, SEARCH_FOLLOW_CONFIG_SYMLINKS);
if (r < 0) if (r < 0)
return r; return r;
path = TAKE_PTR(instance.path);
if (instance.install_mode == INSTALL_MODE_MASKED) { if (instance.install_mode == INSTALL_MODE_MASKED) {
install_changes_add(changes, n_changes, -ERFKILL, path, NULL); install_changes_add(changes, n_changes, -ERFKILL, instance.path, NULL);
return -ERFKILL; return -ERFKILL;
} }
n = buf; n = instance.name;
} else { } else {
/* We have a template, but no instance yet. When used with an instantiated unit, we will get /* We have a template, but no instance yet. When used with an instantiated unit, we will get