core: make sure we generate a nicer error when a linked unit is attempted to be enabled

We don't allow using config symlinks to enable units, but the error message we
printed was awful. Fix that, and generate a more readable error.

Fixes #3010.
This commit is contained in:
Lennart Poettering 2016-04-11 17:57:05 +02:00
parent d94c2b06f9
commit 8e20adcaa0
4 changed files with 24 additions and 15 deletions

View File

@ -1607,6 +1607,19 @@ fail:
return r;
}
static int install_error(sd_bus_error *error, int c) {
assert(c < 0);
if (c == -ESHUTDOWN)
return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit file is masked.");
if (c == -EADDRNOTAVAIL)
return sd_bus_error_setf(error, BUS_ERROR_UNIT_GENERATED, "Unit file is transient or generated.");
if (c == -ELOOP)
return sd_bus_error_setf(error, BUS_ERROR_UNIT_LINKED, "Refusing to operate on linked unit file.");
return c;
}
static int method_enable_unit_files_generic(
sd_bus_message *message,
Manager *m,
@ -1638,12 +1651,8 @@ static int method_enable_unit_files_generic(
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = call(m->unit_file_scope, runtime, NULL, l, force, &changes, &n_changes);
if (r == -ESHUTDOWN)
return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit file is masked.");
if (r == -EADDRNOTAVAIL)
return sd_bus_error_setf(error, BUS_ERROR_UNIT_GENERATED, "Unit file is transient or generated.");
if (r < 0)
return r;
return install_error(error, r);
return reply_unit_file_changes_and_free(m, message, carries_install_info ? r : -1, changes, n_changes);
}
@ -1709,7 +1718,7 @@ static int method_preset_unit_files_with_mode(sd_bus_message *message, void *use
r = unit_file_preset(m->unit_file_scope, runtime, NULL, l, mm, force, &changes, &n_changes);
if (r < 0)
return r;
return install_error(error, r);
return reply_unit_file_changes_and_free(m, message, r, changes, n_changes);
}
@ -1745,7 +1754,7 @@ static int method_disable_unit_files_generic(
r = call(m->unit_file_scope, runtime, NULL, l, &changes, &n_changes);
if (r < 0)
return r;
return install_error(error, r);
return reply_unit_file_changes_and_free(m, message, -1, changes, n_changes);
}
@ -1780,7 +1789,7 @@ static int method_revert_unit_files(sd_bus_message *message, void *userdata, sd_
r = unit_file_revert(m->unit_file_scope, NULL, l, &changes, &n_changes);
if (r < 0)
return r;
return install_error(error, r);
return reply_unit_file_changes_and_free(m, message, -1, changes, n_changes);
}
@ -1811,7 +1820,7 @@ static int method_set_default_target(sd_bus_message *message, void *userdata, sd
r = unit_file_set_default(m->unit_file_scope, NULL, name, force, &changes, &n_changes);
if (r < 0)
return r;
return install_error(error, r);
return reply_unit_file_changes_and_free(m, message, -1, changes, n_changes);
}
@ -1852,7 +1861,7 @@ static int method_preset_all_unit_files(sd_bus_message *message, void *userdata,
r = unit_file_preset_all(m->unit_file_scope, runtime, NULL, mm, force, &changes, &n_changes);
if (r < 0) {
unit_file_changes_free(changes, n_changes);
return r;
return install_error(error, r);
}
return reply_unit_file_changes_and_free(m, message, -1, changes, n_changes);
@ -1890,12 +1899,8 @@ static int method_add_dependency_unit_files(sd_bus_message *message, void *userd
return -EINVAL;
r = unit_file_add_dependency(m->unit_file_scope, runtime, NULL, l, target, dep, force, &changes, &n_changes);
if (r == -ESHUTDOWN)
return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit file is masked.");
if (r == -EADDRNOTAVAIL)
return sd_bus_error_setf(error, BUS_ERROR_UNIT_GENERATED, "Unit file is transient or generated.");
if (r < 0)
return r;
return install_error(error, r);
return reply_unit_file_changes_and_free(m, message, -1, changes, n_changes);
}

View File

@ -39,6 +39,7 @@ BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_common_errors[] = {
SD_BUS_ERROR_MAP(BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE, EDEADLK),
SD_BUS_ERROR_MAP(BUS_ERROR_UNIT_MASKED, ESHUTDOWN),
SD_BUS_ERROR_MAP(BUS_ERROR_UNIT_GENERATED, EADDRNOTAVAIL),
SD_BUS_ERROR_MAP(BUS_ERROR_UNIT_LINKED, ELOOP),
SD_BUS_ERROR_MAP(BUS_ERROR_JOB_TYPE_NOT_APPLICABLE, EBADR),
SD_BUS_ERROR_MAP(BUS_ERROR_NO_ISOLATION, EPERM),
SD_BUS_ERROR_MAP(BUS_ERROR_SHUTTING_DOWN, ECANCELED),

View File

@ -35,6 +35,7 @@
#define BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE "org.freedesktop.systemd1.TransactionIsDestructive"
#define BUS_ERROR_UNIT_MASKED "org.freedesktop.systemd1.UnitMasked"
#define BUS_ERROR_UNIT_GENERATED "org.freedesktop.systemd1.UnitGenerated"
#define BUS_ERROR_UNIT_LINKED "org.freedesktop.systemd1.UnitLinked"
#define BUS_ERROR_JOB_TYPE_NOT_APPLICABLE "org.freedesktop.systemd1.JobTypeNotApplicable"
#define BUS_ERROR_NO_ISOLATION "org.freedesktop.systemd1.NoIsolation"
#define BUS_ERROR_SHUTTING_DOWN "org.freedesktop.systemd1.ShuttingDown"

View File

@ -5446,6 +5446,8 @@ static int enable_unit(int argc, char *argv[], void *userdata) {
return log_error_errno(r, "Unit file is masked.");
if (r == -EADDRNOTAVAIL)
return log_error_errno(r, "Unit file is transient or generated.");
if (r == -ELOOP)
return log_error_errno(r, "Refusing to operate on linked unit file.");
if (r < 0)
return log_error_errno(r, "Operation failed: %m");