From ad5fdd391248432e0c105003a8a13f821bde0b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 9 Jun 2021 18:41:17 +0200 Subject: [PATCH] shared/install: ignore enablement of template units w/o instance when presetting When we have a unit which cannot be enabled: # foo@.service: ... [Install] WantedBy=foo.target # there is no instance, so we don't know what to enable we should throw an error when invoked directly with 'enable', but not when doing 'preset' or 'preset-all'. Fixes #19856. --- src/shared/install.c | 16 +++++++++++++--- src/shared/install.h | 9 +++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/shared/install.c b/src/shared/install.c index f61d10532f..ba4fa5b136 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -1916,8 +1916,16 @@ static int install_info_symlink_wants( return q; if (!unit_name_is_valid(dst, valid_dst_type)) { - /* Generate a proper error here: EUCLEAN if the name is generally bad, - * EIDRM if the template status doesn't match. */ + /* Generate a proper error here: EUCLEAN if the name is generally bad, EIDRM if the + * template status doesn't match. If we are doing presets don't bother reporting the + * error. This also covers cases like 'systemctl preset serial-getty@.service', which + * has no DefaultInstance, so there is nothing we can do. At the same time, + * 'systemctl enable serial-getty@.service' should fail, the user should specify an + * instance like in 'systemctl enable serial-getty@ttyS0.service'. + */ + if (file_flags & UNIT_FILE_IGNORE_AUXILIARY_FAILURE) + continue; + if (unit_name_is_valid(dst, UNIT_NAME_ANY)) { unit_file_changes_add(changes, n_changes, -EIDRM, dst, n); r = -EIDRM; @@ -3214,7 +3222,9 @@ static int execute_preset( int q; /* Returns number of symlinks that where supposed to be installed. */ - q = install_context_apply(scope, file_flags, plus, paths, config_path, SEARCH_LOAD, changes, n_changes); + q = install_context_apply(scope, + file_flags | UNIT_FILE_IGNORE_AUXILIARY_FAILURE, + plus, paths, config_path, SEARCH_LOAD, changes, n_changes); if (r >= 0) { if (q < 0) r = q; diff --git a/src/shared/install.h b/src/shared/install.h index 74a45db04c..c3a0249f5f 100644 --- a/src/shared/install.h +++ b/src/shared/install.h @@ -39,10 +39,11 @@ enum { }; enum UnitFileFlags { - UNIT_FILE_RUNTIME = 1 << 0, /* Public API via DBUS, do not change */ - UNIT_FILE_FORCE = 1 << 1, /* Public API via DBUS, do not change */ - UNIT_FILE_PORTABLE = 1 << 2, /* Public API via DBUS, do not change */ - UNIT_FILE_DRY_RUN = 1 << 3, + UNIT_FILE_RUNTIME = 1 << 0, /* Public API via DBUS, do not change */ + UNIT_FILE_FORCE = 1 << 1, /* Public API via DBUS, do not change */ + UNIT_FILE_PORTABLE = 1 << 2, /* Public API via DBUS, do not change */ + UNIT_FILE_DRY_RUN = 1 << 3, + UNIT_FILE_IGNORE_AUXILIARY_FAILURE = 1 << 4, _UNIT_FILE_FLAGS_MASK_PUBLIC = UNIT_FILE_RUNTIME|UNIT_FILE_PORTABLE|UNIT_FILE_FORCE, };