b677e7
From dfb4e03e0865d189a5c171072d6d7b31f49e1088 Mon Sep 17 00:00:00 2001
b677e7
From: Jan Synacek <jsynacek@redhat.com>
b677e7
Date: Wed, 3 Jun 2020 10:33:21 +0200
b677e7
Subject: [PATCH] install: warn if WantedBy targets don't exist
b677e7
b677e7
Currently, if [Install] section contains WantedBy=target that doesn't exist,
b677e7
systemd creates the symlinks anyway. That is just user-unfriendly.
b677e7
Let's be nice and warn about installing non-existent targets.
b677e7
b677e7
Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1835351.
b677e7
b677e7
Replaces: #15834
b677e7
(cherry picked from commit 8ae27441c2dcf585f58242991302b09778d4d710)
b677e7
b677e7
Resolves: #1835351
b677e7
---
b677e7
 src/shared/install.c | 25 ++++++++++++++++++-------
b677e7
 src/shared/install.h |  1 +
b677e7
 2 files changed, 19 insertions(+), 7 deletions(-)
b677e7
b677e7
diff --git a/src/shared/install.c b/src/shared/install.c
b677e7
index c9fef6bde2..055b09f98c 100644
b677e7
--- a/src/shared/install.c
b677e7
+++ b/src/shared/install.c
b677e7
@@ -362,6 +362,11 @@ void unit_file_dump_changes(int r, const char *verb, const UnitFileChange *chang
b677e7
                                 log_info("Unit %s is an alias to a unit that is not present, ignoring.",
b677e7
                                          changes[i].path);
b677e7
                         break;
b677e7
+                case UNIT_FILE_DESTINATION_NOT_PRESENT:
b677e7
+                        if (!quiet)
b677e7
+                                log_warning("Unit %s is added as a dependency to a non-existent unit %s.",
b677e7
+                                            changes[i].source, changes[i].path);
b677e7
+                        break;
b677e7
                 case -EEXIST:
b677e7
                         if (changes[i].source)
b677e7
                                 log_error_errno(changes[i].type,
b677e7
@@ -1730,6 +1735,7 @@ static int install_info_symlink_alias(
b677e7
 }
b677e7
 
b677e7
 static int install_info_symlink_wants(
b677e7
+                UnitFileScope scope,
b677e7
                 UnitFileInstallInfo *i,
b677e7
                 const LookupPaths *paths,
b677e7
                 const char *config_path,
b677e7
@@ -1795,6 +1801,9 @@ static int install_info_symlink_wants(
b677e7
                 q = create_symlink(paths, i->path, path, true, changes, n_changes);
b677e7
                 if (r == 0)
b677e7
                         r = q;
b677e7
+
b677e7
+                if (unit_file_exists(scope, paths, dst) == 0)
b677e7
+                        unit_file_changes_add(changes, n_changes, UNIT_FILE_DESTINATION_NOT_PRESENT, dst, i->path);
b677e7
         }
b677e7
 
b677e7
         return r;
b677e7
@@ -1830,6 +1839,7 @@ static int install_info_symlink_link(
b677e7
 }
b677e7
 
b677e7
 static int install_info_apply(
b677e7
+                UnitFileScope scope,
b677e7
                 UnitFileInstallInfo *i,
b677e7
                 const LookupPaths *paths,
b677e7
                 const char *config_path,
b677e7
@@ -1848,11 +1858,11 @@ static int install_info_apply(
b677e7
 
b677e7
         r = install_info_symlink_alias(i, paths, config_path, force, changes, n_changes);
b677e7
 
b677e7
-        q = install_info_symlink_wants(i, paths, config_path, i->wanted_by, ".wants/", changes, n_changes);
b677e7
+        q = install_info_symlink_wants(scope, i, paths, config_path, i->wanted_by, ".wants/", changes, n_changes);
b677e7
         if (r == 0)
b677e7
                 r = q;
b677e7
 
b677e7
-        q = install_info_symlink_wants(i, paths, config_path, i->required_by, ".requires/", changes, n_changes);
b677e7
+        q = install_info_symlink_wants(scope, i, paths, config_path, i->required_by, ".requires/", changes, n_changes);
b677e7
         if (r == 0)
b677e7
                 r = q;
b677e7
 
b677e7
@@ -1916,7 +1926,7 @@ static int install_context_apply(
b677e7
                 if (i->type != UNIT_FILE_TYPE_REGULAR)
b677e7
                         continue;
b677e7
 
b677e7
-                q = install_info_apply(i, paths, config_path, force, changes, n_changes);
b677e7
+                q = install_info_apply(scope, i, paths, config_path, force, changes, n_changes);
b677e7
                 if (r >= 0) {
b677e7
                         if (q < 0)
b677e7
                                 r = q;
b677e7
@@ -3324,10 +3334,11 @@ static const char* const unit_file_state_table[_UNIT_FILE_STATE_MAX] = {
b677e7
 DEFINE_STRING_TABLE_LOOKUP(unit_file_state, UnitFileState);
b677e7
 
b677e7
 static const char* const unit_file_change_type_table[_UNIT_FILE_CHANGE_TYPE_MAX] = {
b677e7
-        [UNIT_FILE_SYMLINK] = "symlink",
b677e7
-        [UNIT_FILE_UNLINK] = "unlink",
b677e7
-        [UNIT_FILE_IS_MASKED] = "masked",
b677e7
-        [UNIT_FILE_IS_DANGLING] = "dangling",
b677e7
+        [UNIT_FILE_SYMLINK]                 = "symlink",
b677e7
+        [UNIT_FILE_UNLINK]                  = "unlink",
b677e7
+        [UNIT_FILE_IS_MASKED]               = "masked",
b677e7
+        [UNIT_FILE_IS_DANGLING]             = "dangling",
b677e7
+        [UNIT_FILE_DESTINATION_NOT_PRESENT] = "destination not present",
b677e7
 };
b677e7
 
b677e7
 DEFINE_STRING_TABLE_LOOKUP(unit_file_change_type, UnitFileChangeType);
b677e7
diff --git a/src/shared/install.h b/src/shared/install.h
b677e7
index e452940991..f07bebb415 100644
b677e7
--- a/src/shared/install.h
b677e7
+++ b/src/shared/install.h
b677e7
@@ -57,6 +57,7 @@ enum UnitFileChangeType {
b677e7
         UNIT_FILE_UNLINK,
b677e7
         UNIT_FILE_IS_MASKED,
b677e7
         UNIT_FILE_IS_DANGLING,
b677e7
+        UNIT_FILE_DESTINATION_NOT_PRESENT,
b677e7
         _UNIT_FILE_CHANGE_TYPE_MAX,
b677e7
         _UNIT_FILE_CHANGE_TYPE_INVALID = INT_MIN
b677e7
 };