From 4109ede7788c90e961b08c0da7d4da2d402931d7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 12 Apr 2018 15:13:14 +0200 Subject: [PATCH] core/manager: split out function to verify that unit is loaded and not masked No functional change. --- src/core/main.c | 27 +++++++-------------------- src/core/manager.c | 29 +++++++++++++++++++++++++++++ src/core/manager.h | 1 + 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index 9e95b6f..4d113bd 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1906,28 +1906,15 @@ static int do_queue_default_job( log_debug("Activating default unit: %s", arg_default_unit); - r = manager_load_unit(m, arg_default_unit, NULL, &error, &target); - if (r < 0) - log_error("Failed to load default target: %s", bus_error_message(&error, r)); - else if (IN_SET(target->load_state, UNIT_ERROR, UNIT_NOT_FOUND)) - log_error_errno(target->load_error, "Failed to load default target: %m"); - else if (target->load_state == UNIT_MASKED) - log_error("Default target masked."); - - if (!target || target->load_state != UNIT_LOADED) { - log_info("Trying to load rescue target..."); + r = manager_load_startable_unit_or_warn(m, arg_default_unit, NULL, &target); + if (r < 0) { + log_info("Falling back to rescue target: " SPECIAL_RESCUE_TARGET); - r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &error, &target); + r = manager_load_startable_unit_or_warn(m, SPECIAL_RESCUE_TARGET, NULL, &target); if (r < 0) { - *ret_error_message = "Failed to load rescue target"; - return log_emergency_errno(r, "Failed to load rescue target: %s", bus_error_message(&error, r)); - } else if (IN_SET(target->load_state, UNIT_ERROR, UNIT_NOT_FOUND)) { - *ret_error_message = "Failed to load rescue target"; - return log_emergency_errno(target->load_error, "Failed to load rescue target: %m"); - } else if (target->load_state == UNIT_MASKED) { - *ret_error_message = "Rescue target masked"; - log_emergency("Rescue target masked."); - return -ERFKILL; + *ret_error_message = r == -ERFKILL ? "Rescue target masked" + : "Failed to load rescue target"; + return r; } } diff --git a/src/core/manager.c b/src/core/manager.c index e67f744..8fccd3b 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -1823,7 +1823,36 @@ int manager_load_unit( manager_dispatch_load_queue(m); *_ret = unit_follow_merge(*_ret); + return 0; +} + +int manager_load_startable_unit_or_warn( + Manager *m, + const char *name, + const char *path, + Unit **ret) { + + /* Load a unit, make sure it loaded fully and is not masked. */ + + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + Unit *unit; + int r; + + r = manager_load_unit(m, name, path, &error, &unit); + if (r < 0) + return log_error_errno(r, "Failed to load %s %s: %s", + name ? "unit" : "file", name ?: path, + bus_error_message(&error, r)); + else if (IN_SET(unit->load_state, UNIT_ERROR, UNIT_NOT_FOUND)) + return log_error_errno(unit->load_error, "Failed to load %s %s: %m", + name ? "unit" : "file", name ?: path); + else if (unit->load_state == UNIT_MASKED) { + log_error("%s %s is masked.", + name ? "Unit" : "File", name ?: path); + return -ERFKILL; + } + *ret = unit; return 0; } diff --git a/src/core/manager.h b/src/core/manager.h index e09e0cd..1a09721 100644 --- a/src/core/manager.h +++ b/src/core/manager.h @@ -396,6 +396,7 @@ int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j); int manager_load_unit_prepare(Manager *m, const char *name, const char *path, sd_bus_error *e, Unit **_ret); int manager_load_unit(Manager *m, const char *name, const char *path, sd_bus_error *e, Unit **_ret); +int manager_load_startable_unit_or_warn(Manager *m, const char *name, const char *path, Unit **ret); int manager_load_unit_from_dbus_path(Manager *m, const char *s, sd_bus_error *e, Unit **_u); int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, sd_bus_error *e, Job **_ret); -- 2.7.4