From: Mike Yuan Date: Mon, 3 Jul 2023 22:55:55 +0000 (+0800) Subject: core/transaction: return early when appropriate to reduce nesting X-Git-Tag: v254-rc1~32^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=87d17581af0a27cbcd093d5506e73232c7e2c5b9;p=thirdparty%2Fsystemd.git core/transaction: return early when appropriate to reduce nesting --- diff --git a/src/core/transaction.c b/src/core/transaction.c index 68614de7a06..ba8f19bca17 100644 --- a/src/core/transaction.c +++ b/src/core/transaction.c @@ -911,7 +911,6 @@ int transaction_add_job_and_dependencies( sd_bus_error *e) { bool is_new; - Unit *dep; Job *ret; int r; @@ -982,139 +981,140 @@ int transaction_add_job_and_dependencies( tr->anchor_job = ret; } - if (is_new && !FLAGS_SET(flags, TRANSACTION_IGNORE_REQUIREMENTS) && type != JOB_NOP) { - _cleanup_set_free_ Set *following = NULL; - - /* If we are following some other unit, make sure we add all dependencies of everybody - * following. */ - if (unit_following_set(ret->unit, &following) > 0) - SET_FOREACH(dep, following) { - r = transaction_add_job_and_dependencies(tr, type, dep, ret, flags & TRANSACTION_IGNORE_ORDER, e); - if (r < 0) { - log_unit_full_errno(dep, r == -ERFKILL ? LOG_INFO : LOG_WARNING, r, - "Cannot add dependency job, ignoring: %s", - bus_error_message(e, r)); - sd_bus_error_free(e); - } + if (!is_new || FLAGS_SET(flags, TRANSACTION_IGNORE_REQUIREMENTS) || type == JOB_NOP) + return 0; + + _cleanup_set_free_ Set *following = NULL; + Unit *dep; + + /* If we are following some other unit, make sure we add all dependencies of everybody following. */ + if (unit_following_set(ret->unit, &following) > 0) + SET_FOREACH(dep, following) { + r = transaction_add_job_and_dependencies(tr, type, dep, ret, flags & TRANSACTION_IGNORE_ORDER, e); + if (r < 0) { + log_unit_full_errno(dep, r == -ERFKILL ? LOG_INFO : LOG_WARNING, r, + "Cannot add dependency job, ignoring: %s", + bus_error_message(e, r)); + sd_bus_error_free(e); } + } - /* Finally, recursively add in all dependencies. */ - if (IN_SET(type, JOB_START, JOB_RESTART)) { - UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_START) { - r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER), e); - if (r < 0) { - if (r != -EBADR) /* job type not applicable */ - goto fail; + /* Finally, recursively add in all dependencies. */ + if (IN_SET(type, JOB_START, JOB_RESTART)) { + UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_START) { + r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER), e); + if (r < 0) { + if (r != -EBADR) /* job type not applicable */ + goto fail; - sd_bus_error_free(e); - } + sd_bus_error_free(e); } + } - UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_START_IGNORED) { - r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, flags & TRANSACTION_IGNORE_ORDER, e); - if (r < 0) { - /* unit masked, job type not applicable and unit not found are not - * considered as errors. */ - log_unit_full_errno(dep, - IN_SET(r, -ERFKILL, -EBADR, -ENOENT) ? LOG_DEBUG : LOG_WARNING, - r, "Cannot add dependency job, ignoring: %s", - bus_error_message(e, r)); - sd_bus_error_free(e); - } + UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_START_IGNORED) { + r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, flags & TRANSACTION_IGNORE_ORDER, e); + if (r < 0) { + /* unit masked, job type not applicable and unit not found are not considered + * as errors. */ + log_unit_full_errno(dep, + IN_SET(r, -ERFKILL, -EBADR, -ENOENT) ? LOG_DEBUG : LOG_WARNING, + r, "Cannot add dependency job, ignoring: %s", + bus_error_message(e, r)); + sd_bus_error_free(e); } + } - UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_VERIFY) { - r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER), e); - if (r < 0) { - if (r != -EBADR) /* job type not applicable */ - goto fail; + UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_VERIFY) { + r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER), e); + if (r < 0) { + if (r != -EBADR) /* job type not applicable */ + goto fail; - sd_bus_error_free(e); - } + sd_bus_error_free(e); } + } - UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_STOP) { - r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, TRANSACTION_MATTERS | TRANSACTION_CONFLICTS | (flags & TRANSACTION_IGNORE_ORDER), e); - if (r < 0) { - if (r != -EBADR) /* job type not applicable */ - goto fail; + UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_STOP) { + r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, TRANSACTION_MATTERS | TRANSACTION_CONFLICTS | (flags & TRANSACTION_IGNORE_ORDER), e); + if (r < 0) { + if (r != -EBADR) /* job type not applicable */ + goto fail; - sd_bus_error_free(e); - } + sd_bus_error_free(e); } + } - UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_STOP_IGNORED) { - r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, flags & TRANSACTION_IGNORE_ORDER, e); - if (r < 0) { - log_unit_warning(dep, - "Cannot add dependency job, ignoring: %s", - bus_error_message(e, r)); - sd_bus_error_free(e); - } + UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_STOP_IGNORED) { + r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, flags & TRANSACTION_IGNORE_ORDER, e); + if (r < 0) { + log_unit_warning(dep, + "Cannot add dependency job, ignoring: %s", + bus_error_message(e, r)); + sd_bus_error_free(e); } } + } - _cleanup_set_free_ Set *propagated_restart = NULL; + _cleanup_set_free_ Set *propagated_restart = NULL; - if (type == JOB_RESTART || (type == JOB_START && FLAGS_SET(flags, TRANSACTION_PROPAGATE_START_AS_RESTART))) { + if (type == JOB_RESTART || (type == JOB_START && FLAGS_SET(flags, TRANSACTION_PROPAGATE_START_AS_RESTART))) { - /* We propagate RESTART only as TRY_RESTART, in order not to start dependencies that - * are not around. */ + /* We propagate RESTART only as TRY_RESTART, in order not to start dependencies that + * are not around. */ - UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PROPAGATE_RESTART) { - JobType nt; + UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PROPAGATE_RESTART) { + JobType nt; - r = set_ensure_put(&propagated_restart, NULL, dep); - if (r < 0) - return r; + r = set_ensure_put(&propagated_restart, NULL, dep); + if (r < 0) + return r; - nt = job_type_collapse(JOB_TRY_RESTART, dep); - if (nt == JOB_NOP) - continue; + nt = job_type_collapse(JOB_TRY_RESTART, dep); + if (nt == JOB_NOP) + continue; - r = transaction_add_job_and_dependencies(tr, nt, dep, ret, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER), e); - if (r < 0) { - if (r != -EBADR) /* job type not applicable */ - return r; + r = transaction_add_job_and_dependencies(tr, nt, dep, ret, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER), e); + if (r < 0) { + if (r != -EBADR) /* job type not applicable */ + return r; - sd_bus_error_free(e); - } + sd_bus_error_free(e); } } + } - if (type == JOB_STOP) { - /* The 'stop' part of a restart job is also propagated to units with - * UNIT_ATOM_PROPAGATE_STOP */ + if (type == JOB_STOP) { + /* The 'stop' part of a restart job is also propagated to units with UNIT_ATOM_PROPAGATE_STOP. */ - UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PROPAGATE_STOP) { - /* Units experienced restart propagation are skipped */ - if (set_contains(propagated_restart, dep)) - continue; + UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PROPAGATE_STOP) { + /* Units experienced restart propagation are skipped */ + if (set_contains(propagated_restart, dep)) + continue; - r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER), e); - if (r < 0) { - if (r != -EBADR) /* job type not applicable */ - return r; + r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER), e); + if (r < 0) { + if (r != -EBADR) /* job type not applicable */ + return r; - sd_bus_error_free(e); - } + sd_bus_error_free(e); } } + } - if (type == JOB_RELOAD) - transaction_add_propagate_reload_jobs(tr, ret->unit, ret, flags & TRANSACTION_IGNORE_ORDER); + if (type == JOB_RELOAD) + transaction_add_propagate_reload_jobs(tr, ret->unit, ret, flags & TRANSACTION_IGNORE_ORDER); - /* JOB_VERIFY_ACTIVE requires no dependency handling */ - } + /* JOB_VERIFY_ACTIVE requires no dependency handling */ return 0; + fail: /* Recursive call failed to add required jobs so let's drop top level job as well. */ log_unit_debug_errno(unit, r, "Cannot add dependency job to transaction, deleting job %s/%s again: %s", unit->id, job_type_to_string(type), bus_error_message(e, r)); + transaction_delete_job(tr, ret, /* delete_dependencies= */ false); return r; - } static bool shall_stop_on_isolate(Transaction *tr, Unit *u) {