From: Mike Yuan Date: Sun, 12 Feb 2023 13:08:28 +0000 (+0800) Subject: systemctl: warn if units disabled in user scope are still enabled globally X-Git-Tag: v254-rc1~1193 X-Git-Url: http://git-history.diyao.me/?a=commitdiff_plain;h=e774d3c9f11eb1767202ca4af38f2d3a9238c046;p=systemd%2F.git systemctl: warn if units disabled in user scope are still enabled globally Fixes #18271 --- diff --git a/man/systemctl.xml b/man/systemctl.xml index f32d2f23cf..e0f499c41e 100644 --- a/man/systemctl.xml +++ b/man/systemctl.xml @@ -842,6 +842,11 @@ Jan 12 10:46:45 example.com bluetoothd[8900]: gatt-time-server: Input/output err executed. This output may be suppressed by passing . + When this command is used with , the units being operated on might + still be enabled in global scope, and thus get started automatically even after a successful + disablement in user scope. In this case, a warning about it is shown, which can be suppressed + using . + This command honors , , , and in a similar way as enable. @@ -2045,7 +2050,11 @@ Jan 12 10:46:45 example.com bluetoothd[8900]: gatt-time-server: Input/output err when using enable or disable on units without - install information (i.e. don't have or have an empty [Install] section). + install information (i.e. don't have or have an empty [Install] section), + + + when using disable combined with on units + that are enabled in global scope. diff --git a/src/systemctl/systemctl-enable.c b/src/systemctl/systemctl-enable.c index 86d9f602fa..4ebe5888ac 100644 --- a/src/systemctl/systemctl-enable.c +++ b/src/systemctl/systemctl-enable.c @@ -263,6 +263,43 @@ int verb_enable(int argc, char *argv[], void *userdata) { " instance name specified.", special_glyph(SPECIAL_GLYPH_BULLET)); + if (streq(verb, "disable") && arg_scope == LOOKUP_SCOPE_USER && !arg_quiet && !arg_no_warn) { + /* If some of the units are disabled in user scope but still enabled in global scope, + * we emit a warning for that. */ + + _cleanup_strv_free_ char **enabled_in_global_scope = NULL; + + STRV_FOREACH(name, names) { + UnitFileState state; + + r = unit_file_get_state(LOOKUP_SCOPE_GLOBAL, arg_root, *name, &state); + if (r == -ENOENT) + continue; + if (r < 0) + return log_error_errno(r, "Failed to get unit file state for %s: %m", *name); + + if (IN_SET(state, UNIT_FILE_ENABLED, UNIT_FILE_ENABLED_RUNTIME)) { + r = strv_extend(&enabled_in_global_scope, *name); + if (r < 0) + return log_oom(); + } + } + + if (!strv_isempty(enabled_in_global_scope)) { + _cleanup_free_ char *units = NULL; + + units = strv_join(enabled_in_global_scope, ", "); + if (!units) + return log_oom(); + + log_notice("The following unit files have been enabled in global scope. This means\n" + "they will still be started automatically after a successful disablement\n" + "in user scope:\n" + "%s", + units); + } + } + if (arg_now && STR_IN_SET(argv[0], "enable", "disable", "mask")) { sd_bus *bus; size_t len, i;