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