Zbigniew Jędrzejewski-Szmek 696e2f
From 35b8a55c85aa69af9634af337085da777d438bea Mon Sep 17 00:00:00 2001
Zbigniew Jędrzejewski-Szmek 696e2f
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Zbigniew Jędrzejewski-Szmek 696e2f
Date: Sat, 13 Aug 2016 01:20:29 -0400
Zbigniew Jędrzejewski-Szmek 696e2f
Subject: [PATCH] shared/install: ignore unit symlinks when doing preset-all
Zbigniew Jędrzejewski-Szmek 696e2f
Zbigniew Jędrzejewski-Szmek 696e2f
Before, when interating over unit files during preset-all, behaviour was the
Zbigniew Jędrzejewski-Szmek 696e2f
following:
Zbigniew Jędrzejewski-Szmek 696e2f
Zbigniew Jędrzejewski-Szmek 696e2f
- if we hit the real unit name first, presets were queried for that name, and
Zbigniew Jędrzejewski-Szmek 696e2f
  that unit was enabled or disabled accordingly,
Zbigniew Jędrzejewski-Szmek 696e2f
Zbigniew Jędrzejewski-Szmek 696e2f
- if we hit an alias first (one of the symlinks chaining to the real unit), we
Zbigniew Jędrzejewski-Szmek 696e2f
  checked the presets using the symlink name, and then proceeded to enable or
Zbigniew Jędrzejewski-Szmek 696e2f
  disable the real unit.
Zbigniew Jędrzejewski-Szmek 696e2f
Zbigniew Jędrzejewski-Szmek 696e2f
E.g. for systemd-networkd.service we have the alias dbus-org.freedesktop.network1.service
Zbigniew Jędrzejewski-Szmek 696e2f
(/usr/lib/systemd/system/dbus-org.freedesktop.network1.service), but the preset
Zbigniew Jędrzejewski-Szmek 696e2f
is only for the systemd-networkd.service name. The service would be enabled or
Zbigniew Jędrzejewski-Szmek 696e2f
disabled pseudorandomly depending on the order of iteration.
Zbigniew Jędrzejewski-Szmek 696e2f
Zbigniew Jędrzejewski-Szmek 696e2f
For "preset", behaviour was analogous: preset on the alias name disabled the
Zbigniew Jędrzejewski-Szmek 696e2f
service (following the default disable policy), preset on the "real" name
Zbigniew Jędrzejewski-Szmek 696e2f
applied the presets.
Zbigniew Jędrzejewski-Szmek 696e2f
Zbigniew Jędrzejewski-Szmek 696e2f
With the patch, for "preset" and "preset-all" we silently skip symlinks. This
Zbigniew Jędrzejewski-Szmek 696e2f
gives mostly the right behaviour, with the limitation that presets on aliases
Zbigniew Jędrzejewski-Szmek 696e2f
are ignored.  I think that presets on aliases are not that common (at least my
Zbigniew Jędrzejewski-Szmek 696e2f
preset files on Fedora don't exhibit any such usage), and should not be
Zbigniew Jędrzejewski-Szmek 696e2f
necessary, since whoever installs the preset can just refer to the real unit
Zbigniew Jędrzejewski-Szmek 696e2f
file. It would be possible to overcome this limitation by gathering a list of
Zbigniew Jędrzejewski-Szmek 696e2f
names of a unit first, and then checking whether *any* of the names matches the
Zbigniew Jędrzejewski-Szmek 696e2f
presets list. That would require a significant redesign of the code, and be
Zbigniew Jędrzejewski-Szmek 696e2f
a lot slower (since we would have to fully read all unit directories to preset
Zbigniew Jędrzejewski-Szmek 696e2f
one unit) to so I'm not doing that for now.
Zbigniew Jędrzejewski-Szmek 696e2f
Zbigniew Jędrzejewski-Szmek 696e2f
With this patch, two properties are satisfied:
Zbigniew Jędrzejewski-Szmek 696e2f
- preset-all and preset are idempotent, and the second and subsequent invocations
Zbigniew Jędrzejewski-Szmek 696e2f
  do not produce any changes,
Zbigniew Jędrzejewski-Szmek 696e2f
- preset-all and preset for a specific name produce the same state for that unit.
Zbigniew Jędrzejewski-Szmek 696e2f
Zbigniew Jędrzejewski-Szmek 696e2f
Fixes #3616.
Zbigniew Jędrzejewski-Szmek 696e2f
Zbigniew Jędrzejewski-Szmek 696e2f
(cherry picked from commit 11e11fd57a837ea1cb142009c3048882392f3ed3)
Zbigniew Jędrzejewski-Szmek 696e2f
---
Zbigniew Jędrzejewski-Szmek 696e2f
 src/shared/install.c | 13 ++++++++++---
Zbigniew Jędrzejewski-Szmek 696e2f
 1 file changed, 10 insertions(+), 3 deletions(-)
Zbigniew Jędrzejewski-Szmek 696e2f
Zbigniew Jędrzejewski-Szmek 696e2f
diff --git a/src/shared/install.c b/src/shared/install.c
Zbigniew Jędrzejewski-Szmek 696e2f
index 5e0f9c5d0c..ef5a56391c 100644
Zbigniew Jędrzejewski-Szmek 696e2f
--- a/src/shared/install.c
Zbigniew Jędrzejewski-Szmek 696e2f
+++ b/src/shared/install.c
Zbigniew Jędrzejewski-Szmek 696e2f
@@ -1974,7 +1974,6 @@ int unit_file_revert(
Zbigniew Jędrzejewski-Szmek 696e2f
                 unsigned *n_changes) {
Zbigniew Jędrzejewski-Szmek 696e2f
 
Zbigniew Jędrzejewski-Szmek 696e2f
         _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
Zbigniew Jędrzejewski-Szmek 696e2f
-        /* _cleanup_(install_context_done) InstallContext c = {}; */
Zbigniew Jędrzejewski-Szmek 696e2f
         _cleanup_lookup_paths_free_ LookupPaths paths = {};
Zbigniew Jędrzejewski-Szmek 696e2f
         _cleanup_strv_free_ char **todo = NULL;
Zbigniew Jędrzejewski-Szmek 696e2f
         size_t n_todo = 0, n_allocated = 0;
Zbigniew Jędrzejewski-Szmek 696e2f
@@ -2697,13 +2696,21 @@ static int preset_prepare_one(
Zbigniew Jędrzejewski-Szmek 696e2f
                 UnitFileChange **changes,
Zbigniew Jędrzejewski-Szmek 696e2f
                 unsigned *n_changes) {
Zbigniew Jędrzejewski-Szmek 696e2f
 
Zbigniew Jędrzejewski-Szmek 696e2f
+        _cleanup_(install_context_done) InstallContext tmp = {};
Zbigniew Jędrzejewski-Szmek 696e2f
         UnitFileInstallInfo *i;
Zbigniew Jędrzejewski-Szmek 696e2f
         int r;
Zbigniew Jędrzejewski-Szmek 696e2f
 
Zbigniew Jędrzejewski-Szmek 696e2f
-        if (install_info_find(plus, name) ||
Zbigniew Jędrzejewski-Szmek 696e2f
-            install_info_find(minus, name))
Zbigniew Jędrzejewski-Szmek 696e2f
+        if (install_info_find(plus, name) || install_info_find(minus, name))
Zbigniew Jędrzejewski-Szmek 696e2f
                 return 0;
Zbigniew Jędrzejewski-Szmek 696e2f
 
Zbigniew Jędrzejewski-Szmek 696e2f
+        r = install_info_discover(scope, &tmp, paths, name, SEARCH_FOLLOW_CONFIG_SYMLINKS, &i);
Zbigniew Jędrzejewski-Szmek 696e2f
+        if (r < 0)
Zbigniew Jędrzejewski-Szmek 696e2f
+                return r;
Zbigniew Jędrzejewski-Szmek 696e2f
+        if (!streq(name, i->name)) {
Zbigniew Jędrzejewski-Szmek 696e2f
+                log_debug("Skipping %s because is an alias for %s", name, i->name);
Zbigniew Jędrzejewski-Szmek 696e2f
+                return 0;
Zbigniew Jędrzejewski-Szmek 696e2f
+        }
Zbigniew Jędrzejewski-Szmek 696e2f
+
Zbigniew Jędrzejewski-Szmek 696e2f
         r = query_presets(name, presets);
Zbigniew Jędrzejewski-Szmek 696e2f
         if (r < 0)
Zbigniew Jędrzejewski-Szmek 696e2f
                 return r;