Skip to content

Commit

Permalink
systemctl: make unit_is_masked always query manager
Browse files Browse the repository at this point in the history
(cherry picked from commit 6ea32f6)
  • Loading branch information
YHNdnzj authored and bluca committed Nov 9, 2023
1 parent 4a4c43e commit 589afac
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
7 changes: 1 addition & 6 deletions src/systemctl/systemctl-edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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)
Expand Down
28 changes: 17 additions & 11 deletions src/systemctl/systemctl-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/systemctl/systemctl-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 589afac

Please sign in to comment.