803fb7
From 2047b2d0db643a168144b708cf58091ca47acb21 Mon Sep 17 00:00:00 2001
803fb7
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
803fb7
Date: Wed, 4 May 2016 10:10:57 -0400
803fb7
Subject: [PATCH] shared/install: handle dangling aliases as an explicit case,
803fb7
 report nicely
803fb7
803fb7
This fixes 'preset-all' with a unit that is a dangling symlink.
803fb7
803fb7
$ systemctl --root=/ preset-all
803fb7
Unit syslog.service is an alias to a unit that is not present, ignoring.
803fb7
Unit auditd.service is masked, ignoring.
803fb7
Unit NetworkManager.service is masked, ignoring.
803fb7
803fb7
Cherry-picked from: 893275df36c8c358d3c0b851ca255a6169dac138
803fb7
Resolves: #1375097
803fb7
---
803fb7
 src/libsystemd/sd-bus/bus-util.c |  2 ++
803fb7
 src/shared/install.c             | 17 +++++++++++++----
803fb7
 src/shared/install.h             |  1 +
803fb7
 src/systemctl/systemctl.c        |  3 +++
803fb7
 4 files changed, 19 insertions(+), 4 deletions(-)
803fb7
803fb7
diff --git a/src/libsystemd/sd-bus/bus-util.c b/src/libsystemd/sd-bus/bus-util.c
803fb7
index 75d03708e..d35776087 100644
803fb7
--- a/src/libsystemd/sd-bus/bus-util.c
803fb7
+++ b/src/libsystemd/sd-bus/bus-util.c
803fb7
@@ -1897,6 +1897,8 @@ int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet, Un
803fb7
                                 log_info("Removed symlink %s.", path);
803fb7
                         else if (streq(type, "masked"))
803fb7
                                 log_info("Unit %s is masked, ignoring.", path);
803fb7
+                        else if (streq(type, "dangling"))
803fb7
+                                log_info("Unit %s is an alias to a unit that is not present, ignoring.", path);
803fb7
                         else
803fb7
                                 log_notice("Manager reported unknown change type \"%s\" for %s.", type, path);
803fb7
                 }
803fb7
diff --git a/src/shared/install.c b/src/shared/install.c
803fb7
index 62da52d3b..b0a29ddd7 100644
803fb7
--- a/src/shared/install.c
803fb7
+++ b/src/shared/install.c
803fb7
@@ -1253,12 +1253,15 @@ static int install_info_traverse(
803fb7
                         if (r < 0)
803fb7
                                 return r;
803fb7
 
803fb7
+                        /* Try again, with the new target we found. */
803fb7
                         r = unit_file_search(c, i, paths, root_dir, flags);
803fb7
-                        if (r < 0)
803fb7
-                                return r;
803fb7
+                        if (r == -ENOENT)
803fb7
+                                /* Translate error code to highlight this specific case */
803fb7
+                                return -ENOLINK;
803fb7
                 }
803fb7
 
803fb7
-                /* Try again, with the new target we found. */
803fb7
+                if (r < 0)
803fb7
+                        return r;
803fb7
         }
803fb7
 
803fb7
         if (ret)
803fb7
@@ -1528,7 +1531,9 @@ static int install_context_mark_for_removal(
803fb7
                         return r;
803fb7
 
803fb7
                 r = install_info_traverse(scope, c, root_dir, paths, i, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS, NULL);
803fb7
-                if (r < 0)
803fb7
+                if (r == -ENOLINK)
803fb7
+                        return 0;
803fb7
+                else if (r < 0)
803fb7
                         return r;
803fb7
 
803fb7
                 if (i->type != UNIT_FILE_TYPE_REGULAR)
803fb7
@@ -2405,6 +2410,9 @@ int unit_file_preset_all(
803fb7
                         if (r == -ESHUTDOWN)
803fb7
                                 r = unit_file_changes_add(changes, n_changes,
803fb7
                                                           UNIT_FILE_IS_MASKED, de->d_name, NULL);
803fb7
+                        else if (r == -ENOLINK)
803fb7
+                                r = unit_file_changes_add(changes, n_changes,
803fb7
+                                                          UNIT_FILE_IS_DANGLING, de->d_name, NULL);
803fb7
                         if (r < 0)
803fb7
                                 return r;
803fb7
                 }
803fb7
@@ -2539,6 +2547,7 @@ static const char* const unit_file_change_type_table[_UNIT_FILE_CHANGE_TYPE_MAX]
803fb7
         [UNIT_FILE_SYMLINK] = "symlink",
803fb7
         [UNIT_FILE_UNLINK] = "unlink",
803fb7
         [UNIT_FILE_IS_MASKED] = "masked",
803fb7
+        [UNIT_FILE_IS_DANGLING] = "dangling",
803fb7
 };
803fb7
 
803fb7
 DEFINE_STRING_TABLE_LOOKUP(unit_file_change_type, UnitFileChangeType);
803fb7
diff --git a/src/shared/install.h b/src/shared/install.h
803fb7
index f0e36661b..7e40445d3 100644
803fb7
--- a/src/shared/install.h
803fb7
+++ b/src/shared/install.h
803fb7
@@ -61,6 +61,7 @@ typedef enum UnitFileChangeType {
803fb7
         UNIT_FILE_SYMLINK,
803fb7
         UNIT_FILE_UNLINK,
803fb7
         UNIT_FILE_IS_MASKED,
803fb7
+        UNIT_FILE_IS_DANGLING,
803fb7
         _UNIT_FILE_CHANGE_TYPE_MAX,
803fb7
         _UNIT_FILE_CHANGE_TYPE_INVALID = -1
803fb7
 } UnitFileChangeType;
803fb7
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
803fb7
index a688d6905..39f0150e5 100644
803fb7
--- a/src/systemctl/systemctl.c
803fb7
+++ b/src/systemctl/systemctl.c
803fb7
@@ -1955,6 +1955,9 @@ static void dump_unit_file_changes(const UnitFileChange *changes, unsigned n_cha
803fb7
                 case UNIT_FILE_IS_MASKED:
803fb7
                         log_info("Unit %s is masked, ignoring.", changes[i].path);
803fb7
                         break;
803fb7
+                case UNIT_FILE_IS_DANGLING:
803fb7
+                        log_info("Unit %s is an alias to a unit that is not present, ignoring.", changes[i].path);
803fb7
+                        break;
803fb7
                 default:
803fb7
                         assert_not_reached("bad change type");
803fb7
                 }