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