core/manager: split out function to verify that unit is loaded and not masked

No functional change.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-04-12 15:13:14 +02:00
parent c0af656c52
commit 4109ede778
3 changed files with 37 additions and 20 deletions

View File

@ -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.");
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);
if (!target || target->load_state != UNIT_LOADED) {
log_info("Trying to load 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;
}
}

View File

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

View File

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