]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl: make unit_is_masked always query manager
authorMike Yuan <me@yhndnzj.com>
Wed, 27 Sep 2023 15:43:47 +0000 (23:43 +0800)
committerMike Yuan <me@yhndnzj.com>
Wed, 27 Sep 2023 21:24:51 +0000 (05:24 +0800)
src/systemctl/systemctl-edit.c
src/systemctl/systemctl-util.c
src/systemctl/systemctl-util.h

index e3f25d52d5242568482082bce20644c4df4d9f03..ac0bbb454297bba5c2fd1890ecc25689a10fb779 100644 (file)
@@ -318,7 +318,6 @@ int verb_edit(int argc, char *argv[], void *userdata) {
                 .remove_parent = !arg_full,
                 .overwrite_with_origin = true,
         };
-        _cleanup_(lookup_paths_free) LookupPaths lp = {};
         _cleanup_strv_free_ char **names = NULL;
         sd_bus *bus;
         int r;
@@ -329,10 +328,6 @@ int verb_edit(int argc, char *argv[], void *userdata) {
         if (arg_transport != BUS_TRANSPORT_LOCAL)
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units remotely.");
 
-        r = lookup_paths_init_or_warn(&lp, arg_runtime_scope, 0, arg_root);
-        if (r < 0)
-                return r;
-
         r = mac_init();
         if (r < 0)
                 return r;
@@ -348,7 +343,7 @@ int verb_edit(int argc, char *argv[], void *userdata) {
                 return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No units matched the specified patterns.");
 
         STRV_FOREACH(tmp, names) {
-                r = unit_is_masked(bus, &lp, *tmp);
+                r = unit_is_masked(bus, *tmp);
                 if (r < 0)
                         return r;
                 if (r > 0)
index 074bb8d83be87be654178b04ab55928fe2851072..10df0087b4450babb79ff81812458a074bdcaf7e 100644 (file)
@@ -624,25 +624,31 @@ static int unit_find_template_path(
         return r;
 }
 
-int unit_is_masked(sd_bus *bus, LookupPaths *lp, const char *name) {
+int unit_is_masked(sd_bus *bus, const char *unit) {
         _cleanup_free_ char *load_state = NULL;
         int r;
 
-        if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) {
-                _cleanup_free_ char *path = NULL;
+        assert(bus);
+        assert(unit);
 
-                /* A template cannot be loaded, but it can be still masked, so
-                 * we need to use a different method. */
+        if (unit_name_is_valid(unit, UNIT_NAME_TEMPLATE)) {
+                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+                _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
+                const char *state;
 
-                r = unit_file_find_path(lp, name, &path);
+                r = bus_call_method(bus, bus_systemd_mgr, "GetUnitFileState", &error, &reply, "s", unit);
                 if (r < 0)
-                        return r;
-                if (r == 0)
-                        return false;
-                return null_or_empty_path(path);
+                        return log_debug_errno(r, "Failed to get UnitFileState for '%s': %s",
+                                               unit, bus_error_message(&error, r));
+
+                r = sd_bus_message_read(reply, "s", &state);
+                if (r < 0)
+                        return bus_log_parse_error_debug(r);
+
+                return STR_IN_SET(state, "masked", "masked-runtime");
         }
 
-        r = unit_load_state(bus, name, &load_state);
+        r = unit_load_state(bus, unit, &load_state);
         if (r < 0)
                 return r;
 
index 317bab75b73fbd1cccbf7a64152d8f974999e465..344075722adc27597cd3a50fa1a6097866bea8a2 100644 (file)
@@ -38,7 +38,7 @@ int maybe_extend_with_unit_dependencies(sd_bus *bus, char ***list);
 int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **ret_unit_path);
 int unit_find_paths(sd_bus *bus, const char *unit_name, LookupPaths *lp, bool force_client_side, Hashmap **cached_id_map, Hashmap **cached_name_map, char **ret_fragment_path, char ***ret_dropin_paths);
 
-int unit_is_masked(sd_bus *bus, LookupPaths *lp, const char *name);
+int unit_is_masked(sd_bus *bus, const char *unit);
 int unit_exists(LookupPaths *lp, const char *unit);
 
 int unit_get_dependencies(sd_bus *bus, const char *name, char ***ret);