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