696e2f2
From 3cb3b0145ed8439eb604b43596e6456ed3292c46 Mon Sep 17 00:00:00 2001
696e2f2
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
696e2f2
Date: Sun, 21 Aug 2016 09:10:51 -0400
696e2f2
Subject: [PATCH] shared/install: do not enable masked instances (#4005)
696e2f2
MIME-Version: 1.0
696e2f2
Content-Type: text/plain; charset=UTF-8
696e2f2
Content-Transfer-Encoding: 8bit
696e2f2
696e2f2
When told to enable a template unit, and the DefaultInstance specified in that
696e2f2
unit was masked, we would do this. Such a unit cannot be started or loaded, so
696e2f2
reporting successful enabling is misleading and unexpected.
696e2f2
696e2f2
$ systemctl mask getty@tty1
696e2f2
Created symlink /etc/systemd/system/getty@tty1.service → /dev/null.
696e2f2
$ systemctl --root=/ enable getty@tty1
696e2f2
(unchanged)
696e2f2
Failed to enable unit, unit /etc/systemd/system/getty@tty1.service is masked.
696e2f2
696e2f2
$ systemctl --root=/ enable getty@
696e2f2
(before)
696e2f2
Created symlink /etc/systemd/system/getty.target.wants/getty@tty1.service → /usr/lib/systemd/system/getty@.service.
696e2f2
(now)
696e2f2
Failed to enable unit, unit /etc/systemd/system/getty@tty1.service is masked.
696e2f2
696e2f2
The same error is emitted for enable and preset. And an error is emmited, not a
696e2f2
warning, so the failure to enable DefaultInstance is treated the same as if the
696e2f2
instance was specified on the command line. I think that this makes most sense,
696e2f2
for most template units.
696e2f2
696e2f2
Fixes #2513.
696e2f2
(cherry picked from commit 047d91f9c8cf1bcf5a30f428668babd619533944)
696e2f2
---
696e2f2
 src/shared/install.c | 35 ++++++++++++++++++++++++++++-------
696e2f2
 1 file changed, 28 insertions(+), 7 deletions(-)
696e2f2
696e2f2
diff --git a/src/shared/install.c b/src/shared/install.c
696e2f2
index 9d9f4dff4f..cb2a2e7e0d 100644
696e2f2
--- a/src/shared/install.c
696e2f2
+++ b/src/shared/install.c
696e2f2
@@ -912,8 +912,8 @@ static int install_info_may_process(
696e2f2
         assert(i);
696e2f2
         assert(paths);
696e2f2
 
696e2f2
-        /* Checks whether the loaded unit file is one we should process, or is masked, transient or generated and thus
696e2f2
-         * not subject to enable/disable operations. */
696e2f2
+        /* Checks whether the loaded unit file is one we should process, or is masked,
696e2f2
+         * transient or generated and thus not subject to enable/disable operations. */
696e2f2
 
696e2f2
         if (i->type == UNIT_FILE_TYPE_MASKED) {
696e2f2
                 unit_file_changes_add(changes, n_changes, -ERFKILL, i->path, NULL);
696e2f2
@@ -1134,7 +1134,6 @@ static int unit_file_load(
696e2f2
         struct stat st;
696e2f2
         int r;
696e2f2
 
696e2f2
-        assert(c);
696e2f2
         assert(info);
696e2f2
         assert(path);
696e2f2
 
696e2f2
@@ -1163,6 +1162,9 @@ static int unit_file_load(
696e2f2
                 return 0;
696e2f2
         }
696e2f2
 
696e2f2
+        /* c is only needed if we actually load the file */
696e2f2
+        assert(c);
696e2f2
+
696e2f2
         fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
696e2f2
         if (fd < 0)
696e2f2
                 return -errno;
696e2f2
@@ -1275,7 +1277,6 @@ static int unit_file_search(
696e2f2
         char **p;
696e2f2
         int r;
696e2f2
 
696e2f2
-        assert(c);
696e2f2
         assert(info);
696e2f2
         assert(paths);
696e2f2
 
696e2f2
@@ -1546,7 +1547,14 @@ static int install_info_symlink_wants(
696e2f2
         assert(paths);
696e2f2
         assert(config_path);
696e2f2
 
696e2f2
+        if (strv_isempty(list))
696e2f2
+                return 0;
696e2f2
+
696e2f2
         if (unit_name_is_valid(i->name, UNIT_NAME_TEMPLATE)) {
696e2f2
+                UnitFileInstallInfo instance = {
696e2f2
+                        .type = _UNIT_FILE_TYPE_INVALID,
696e2f2
+                };
696e2f2
+                _cleanup_free_ char *path = NULL;
696e2f2
 
696e2f2
                 /* Don't install any symlink if there's no default
696e2f2
                  * instance configured */
696e2f2
@@ -1558,6 +1566,19 @@ static int install_info_symlink_wants(
696e2f2
                 if (r < 0)
696e2f2
                         return r;
696e2f2
 
696e2f2
+                instance.name = buf;
696e2f2
+                r = unit_file_search(NULL, &instance, paths, SEARCH_FOLLOW_CONFIG_SYMLINKS);
696e2f2
+                if (r < 0)
696e2f2
+                        return r;
696e2f2
+
696e2f2
+                path = instance.path;
696e2f2
+                instance.path = NULL;
696e2f2
+
696e2f2
+                if (instance.type == UNIT_FILE_TYPE_MASKED) {
696e2f2
+                        unit_file_changes_add(changes, n_changes, -ERFKILL, path, NULL);
696e2f2
+                        return -ERFKILL;
696e2f2
+                }
696e2f2
+
696e2f2
                 n = buf;
696e2f2
         } else
696e2f2
                 n = i->name;
696e2f2
@@ -1687,12 +1708,12 @@ static int install_context_apply(
696e2f2
                         return r;
696e2f2
 
696e2f2
                 /* We can attempt to process a masked unit when a different unit
696e2f2
-                 * that we were processing specifies it in DefaultInstance= or Also=. */
696e2f2
+                 * that we were processing specifies it in Also=. */
696e2f2
                 if (i->type == UNIT_FILE_TYPE_MASKED) {
696e2f2
                         unit_file_changes_add(changes, n_changes, UNIT_FILE_IS_MASKED, i->path, NULL);
696e2f2
                         if (r >= 0)
696e2f2
-                                /* Assume that some *could* have been enabled here, avoid
696e2f2
-                                 * "empty [Install] section" warning. */
696e2f2
+                                /* Assume that something *could* have been enabled here,
696e2f2
+                                 * avoid "empty [Install] section" warning. */
696e2f2
                                 r += 1;
696e2f2
                         continue;
696e2f2
                 }
696e2f2
-- 
696e2f2
2.9.0
696e2f2