summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2016-10-20 00:27:46 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2016-10-20 09:28:46 -0400
commitcfd559c9a8bc398ce03d604957fa31fbbcaef2ef (patch)
tree3de20bd1fad786554a73d9060c6f897a570e1a2e
parent6e3136b209679d88c4347ce2f6d5e22c031e4d3e (diff)
shared/install: fix DefaultInstance expansion in %n, %N
We should substitute DefaultInstance if the instance is not specified. Test case: [Install] DefaultInstance=bond1 WantedBy= foobar-n-%n.device WantedBy= foobar-N-%N.device $ systemctl --root=/ enable testing4@.service Created symlink /etc/systemd/system/foobar-n-testing4@bond1.service.device.wants/testing4@bond1.service → /etc/systemd/system/testing4@.service. Created symlink /etc/systemd/system/foobar-N-testing4@bond1.device.wants/testing4@bond1.service → /etc/systemd/system/testing4@.service. (before, the symlink would be created with empty %n, %N parts).
-rw-r--r--src/shared/install-printf.c42
1 files changed, 38 insertions, 4 deletions
diff --git a/src/shared/install-printf.c b/src/shared/install-printf.c
index 9fc915d62e..267762d84f 100644
--- a/src/shared/install-printf.c
+++ b/src/shared/install-printf.c
@@ -32,15 +32,49 @@
#include "user-util.h"
static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
- UnitFileInstallInfo *i = userdata;
+ const UnitFileInstallInfo *i = userdata;
+ _cleanup_free_ char *prefix = NULL;
+ int r;
+
+ assert(i);
+
+ r = unit_name_to_prefix_and_instance(i->name, &prefix);
+ if (r < 0)
+ return r;
+
+ if (endswith(prefix, "@") && i->default_instance) {
+ char *ans;
+
+ ans = strjoin(prefix, i->default_instance, NULL);
+ if (!ans)
+ return -ENOMEM;
+ *ret = ans;
+ } else {
+ *ret = prefix;
+ prefix = NULL;
+ }
+
+ return 0;
+}
+
+static int specifier_name(char specifier, void *data, void *userdata, char **ret) {
+ const UnitFileInstallInfo *i = userdata;
+ char *ans;
assert(i);
- return unit_name_to_prefix_and_instance(i->name, ret);
+ if (unit_name_is_valid(i->name, UNIT_NAME_TEMPLATE) && i->default_instance)
+ return unit_name_replace_instance(i->name, i->default_instance, ret);
+
+ ans = strdup(i->name);
+ if (!ans)
+ return -ENOMEM;
+ *ret = ans;
+ return 0;
}
static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
- UnitFileInstallInfo *i = userdata;
+ const UnitFileInstallInfo *i = userdata;
assert(i);
@@ -111,7 +145,7 @@ int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret)
*/
const Specifier table[] = {
- { 'n', specifier_string, i->name },
+ { 'n', specifier_name, NULL },
{ 'N', specifier_prefix_and_instance, NULL },
{ 'p', specifier_prefix, NULL },
{ 'i', specifier_instance, NULL },