594167
From 256e0d19abd531ab34ed28181ffdae5d5c0dcfed Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Wed, 9 Mar 2022 16:06:24 +0100
594167
Subject: [PATCH] shared/install: provide proper error messages when invalid
594167
 specifiers are used
594167
594167
$ build/systemctl --root=/tmp/systemctl-test.KXY8fu enable some-some-link6@.socket
594167
Failed to enable unit, invalid specifier in "target@C:%C.socket".
594167
594167
(cherry picked from commit 19b9d5d0d14f2c9b8be8d0a026b0445168808b94)
594167
594167
Related: #2082131
594167
---
594167
 src/shared/install.c          | 11 +++++-----
594167
 test/test-systemctl-enable.sh | 40 ++++++++++++-----------------------
594167
 2 files changed, 20 insertions(+), 31 deletions(-)
594167
594167
diff --git a/src/shared/install.c b/src/shared/install.c
594167
index ea5bc36482..fd57488024 100644
594167
--- a/src/shared/install.c
594167
+++ b/src/shared/install.c
594167
@@ -374,12 +374,16 @@ void unit_file_dump_changes(int r, const char *verb, const UnitFileChange *chang
594167
                                         verb, changes[i].path);
594167
                         logged = true;
594167
                         break;
594167
-
594167
                 case -EADDRNOTAVAIL:
594167
                         log_error_errno(changes[i].type_or_errno, "Failed to %s unit, unit %s is transient or generated.",
594167
                                         verb, changes[i].path);
594167
                         logged = true;
594167
                         break;
594167
+                case -EBADSLT:
594167
+                        log_error_errno(changes[i].type_or_errno, "Failed to %s unit, invalid specifier in \"%s\".",
594167
+                                        verb, changes[i].path);
594167
+                        logged = true;
594167
+                        break;
594167
                 case -EIDRM:
594167
                         log_error_errno(changes[i].type_or_errno, "Failed to %s %s, destination unit %s is a non-template unit.",
594167
                                         verb, changes[i].source, changes[i].path);
594167
@@ -396,18 +400,15 @@ void unit_file_dump_changes(int r, const char *verb, const UnitFileChange *chang
594167
                                         verb, changes[i].path);
594167
                         logged = true;
594167
                         break;
594167
-
594167
                 case -ENOENT:
594167
                         log_error_errno(changes[i].type_or_errno, "Failed to %s unit, unit %s does not exist.", verb, changes[i].path);
594167
                         logged = true;
594167
                         break;
594167
-
594167
                 case -EUNATCH:
594167
                         log_error_errno(changes[i].type_or_errno, "Failed to %s unit, cannot resolve specifiers in \"%s\".",
594167
                                         verb, changes[i].path);
594167
                         logged = true;
594167
                         break;
594167
-
594167
                 default:
594167
                         assert(changes[i].type_or_errno < 0);
594167
                         log_error_errno(changes[i].type_or_errno, "Failed to %s unit, file \"%s\": %m",
594167
@@ -3343,7 +3344,7 @@ int unit_file_preset_all(
594167
 
594167
                         r = preset_prepare_one(scope, &plus, &minus, &lp, de->d_name, &presets, changes, n_changes);
594167
                         if (r < 0 &&
594167
-                            !IN_SET(r, -EEXIST, -ERFKILL, -EADDRNOTAVAIL, -EIDRM, -EUCLEAN, -ELOOP, -ENOENT, -EUNATCH))
594167
+                            !IN_SET(r, -EEXIST, -ERFKILL, -EADDRNOTAVAIL, -EBADSLT, -EIDRM, -EUCLEAN, -ELOOP, -ENOENT, -EUNATCH))
594167
                                 /* Ignore generated/transient/missing/invalid units when applying preset, propagate other errors.
594167
                                  * Coordinate with unit_file_dump_changes() above. */
594167
                                 return r;
594167
diff --git a/test/test-systemctl-enable.sh b/test/test-systemctl-enable.sh
594167
index 43a2c0a0fb..da1fffe944 100644
594167
--- a/test/test-systemctl-enable.sh
594167
+++ b/test/test-systemctl-enable.sh
594167
@@ -485,33 +485,31 @@ check_alias W 'right'
594167
 
594167
 check_alias b "$(systemd-id128 boot-id)"
594167
 
594167
-# FIXME: Failed to enable: Invalid slot.
594167
-# Alias=target@%C.socket
594167
-# Alias=target@%E.socket
594167
-# Alias=target@%f.socket
594167
+# Specifiers not available for [Install]
594167
+check_alias C '' && { echo "Expected failure" >&2; exit 1; }
594167
+check_alias E '' && { echo "Expected failure" >&2; exit 1; }
594167
+check_alias f '' && { echo "Expected failure" >&2; exit 1; }
594167
+check_alias h '' && { echo "Expected failure" >&2; exit 1; }
594167
+check_alias I '' && { echo "Expected failure" >&2; exit 1; }
594167
+check_alias J '' && { echo "Expected failure" >&2; exit 1; }
594167
+check_alias L '' && { echo "Expected failure" >&2; exit 1; }
594167
+check_alias P '' && { echo "Expected failure" >&2; exit 1; }
594167
+check_alias s '' && { echo "Expected failure" >&2; exit 1; }
594167
+check_alias S '' && { echo "Expected failure" >&2; exit 1; }
594167
+check_alias t '' && { echo "Expected failure" >&2; exit 1; }
594167
+check_alias T '' && { echo "Expected failure" >&2; exit 1; }
594167
+check_alias V '' && { echo "Expected failure" >&2; exit 1; }
594167
 
594167
 # FIXME: we use the calling user instead of root :(
594167
 check_alias g root || :
594167
 check_alias G 0 || :
594167
 
594167
-# FIXME: Failed to enable: Invalid slot.
594167
-# Alias=target@%h.socket
594167
-
594167
 check_alias i ""
594167
 
594167
-# FIXME: Failed to enable: Invalid slot.
594167
-# Alias=target@%I.socket
594167
-
594167
 check_alias j 'link6'
594167
 
594167
-# FIXME: Failed to enable: Invalid slot.
594167
-# Alias=target@%J.socket
594167
-
594167
 check_alias l "$(uname -n | sed 's/\..*//')"
594167
 
594167
-# FIXME: Failed to enable: Invalid slot.
594167
-# Alias=target@%L.socket
594167
-
594167
 test ! -e "$root/etc/machine-id"
594167
 check_alias m '' && { echo "Expected failure" >&2; exit 1; }
594167
 
594167
@@ -523,22 +521,12 @@ check_alias N 'some-some-link6@'
594167
 
594167
 check_alias p 'some-some-link6'
594167
 
594167
-# FIXME: Failed to enable: Invalid slot.
594167
-# Alias=target@%P.socket
594167
-# Alias=target@%s.socket
594167
-# Alias=target@%S.socket
594167
-# Alias=target@%t.socket
594167
-# Alias=target@%T.socket
594167
-
594167
 # FIXME: we use the calling user instead of root :(
594167
 check_alias u root || :
594167
 check_alias U 0 || :
594167
 
594167
 check_alias v "$(uname -r)"
594167
 
594167
-# FIXME: Failed to enable: Invalid slot.
594167
-# Alias=target@%V.socket
594167
-
594167
 check_alias % '%' && { echo "Expected failure because % is not legal in unit name" >&2; exit 1; }
594167
 
594167
 check_alias z 'z' && { echo "Expected failure because %z is not known" >&2; exit 1; }