naccyde / rpms / systemd

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