]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/core/dbus-manager.c
core: make sure we generate a nicer error when a linked unit is attempted to be enabled
[thirdparty/systemd.git] / src / core / dbus-manager.c
index 41f27ec26bb1e6fe85dab1631278c586b3d3ade7..0939a5518216c8c2f2503466b1c324ecf46b93bb 100644 (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);
 }