richardphibel / rpms / systemd

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