diff options
author | Mike Yuan <me@yhndnzj.com> | 2022-11-18 15:43:34 +0800 |
---|---|---|
committer | Mike Yuan <me@yhndnzj.com> | 2022-12-03 20:26:14 +0800 |
commit | bf1bea43f15b04152a3948702ba1695a0835c2bf (patch) | |
tree | 3c919627634b4b63dca861de01699b90308a9032 /src/shared/install.c | |
parent | bef69ae8780da17a807881d078247259170f868e (diff) | |
download | systemd-bf1bea43f15b04152a3948702ba1695a0835c2bf.tar.gz |
systemctl: warn if trying to disable a unit with no install info
Trying to disable a unit with no install info is mostly useless, so
adding a warning like we do for enable (with the new dbus method
'DisableUnitFilesWithFlagsAndInstallInfo()'). Note that it would
still find and remove symlinks to the unit in /etc, regardless of
whether it has install info or not, just like before. And if there are
actually files to remove, we suppress the warning.
Fixes #17689
Diffstat (limited to 'src/shared/install.c')
-rw-r--r-- | src/shared/install.c | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/shared/install.c b/src/shared/install.c index 51aa60bb52..c38ba1bd7a 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -2790,25 +2790,38 @@ static int do_unit_file_disable( _cleanup_(install_context_done) InstallContext ctx = { .scope = scope }; _cleanup_set_free_free_ Set *remove_symlinks_to = NULL; + InstallInfo *info; + bool has_install_info = false; int r; STRV_FOREACH(name, names) { if (!unit_name_is_valid(*name, UNIT_NAME_ANY)) return install_changes_add(changes, n_changes, -EUCLEAN, *name, NULL); - r = install_info_add(&ctx, *name, NULL, lp->root_dir, /* auxiliary= */ false, NULL); + r = install_info_add(&ctx, *name, NULL, lp->root_dir, /* auxiliary= */ false, &info); + if (r >= 0) + r = install_info_traverse(&ctx, lp, info, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS, NULL); + if (r < 0) - return r; + return install_changes_add(changes, n_changes, r, *name, NULL); + + /* If we enable multiple units, some with install info and others without, + * the "empty [Install] section" warning is not shown. Let's make the behavior + * of disable align with that. */ + has_install_info = has_install_info || install_info_has_rules(info) || install_info_has_also(info); } r = install_context_mark_for_removal(&ctx, lp, &remove_symlinks_to, config_path, changes, n_changes); + if (r >= 0) + r = remove_marked_symlinks(remove_symlinks_to, config_path, lp, flags & UNIT_FILE_DRY_RUN, changes, n_changes); + if (r < 0) return r; - return remove_marked_symlinks(remove_symlinks_to, config_path, lp, flags & UNIT_FILE_DRY_RUN, changes, n_changes); + /* The warning is shown only if it's a no-op */ + return install_changes_have_modification(*changes, *n_changes) || has_install_info; } - int unit_file_disable( LookupScope scope, UnitFileFlags flags, |