]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7584d236 | 2 | |
a8fbdf54 TA |
3 | #include <errno.h> |
4 | #include <stdio.h> | |
a8fbdf54 | 5 | #include <unistd.h> |
7584d236 | 6 | |
f97b34a6 | 7 | #include "format-util.h" |
b1d4f8e1 | 8 | #include "install-printf.h" |
a8fbdf54 TA |
9 | #include "install.h" |
10 | #include "macro.h" | |
7584d236 | 11 | #include "specifier.h" |
6e3136b2 | 12 | #include "string-util.h" |
7584d236 | 13 | #include "unit-name.h" |
b1d4f8e1 | 14 | #include "user-util.h" |
7584d236 | 15 | |
303ee601 | 16 | static int specifier_prefix_and_instance(char specifier, const void *data, const void *userdata, char **ret) { |
cfd559c9 ZJS |
17 | const UnitFileInstallInfo *i = userdata; |
18 | _cleanup_free_ char *prefix = NULL; | |
19 | int r; | |
20 | ||
21 | assert(i); | |
22 | ||
23 | r = unit_name_to_prefix_and_instance(i->name, &prefix); | |
24 | if (r < 0) | |
25 | return r; | |
26 | ||
27 | if (endswith(prefix, "@") && i->default_instance) { | |
28 | char *ans; | |
29 | ||
605405c6 | 30 | ans = strjoin(prefix, i->default_instance); |
cfd559c9 ZJS |
31 | if (!ans) |
32 | return -ENOMEM; | |
33 | *ret = ans; | |
ae2a15bc LP |
34 | } else |
35 | *ret = TAKE_PTR(prefix); | |
cfd559c9 ZJS |
36 | |
37 | return 0; | |
38 | } | |
39 | ||
303ee601 | 40 | static int specifier_name(char specifier, const void *data, const void *userdata, char **ret) { |
cfd559c9 ZJS |
41 | const UnitFileInstallInfo *i = userdata; |
42 | char *ans; | |
19f6d710 | 43 | |
7584d236 ZJS |
44 | assert(i); |
45 | ||
cfd559c9 ZJS |
46 | if (unit_name_is_valid(i->name, UNIT_NAME_TEMPLATE) && i->default_instance) |
47 | return unit_name_replace_instance(i->name, i->default_instance, ret); | |
48 | ||
49 | ans = strdup(i->name); | |
50 | if (!ans) | |
51 | return -ENOMEM; | |
52 | *ret = ans; | |
53 | return 0; | |
7584d236 ZJS |
54 | } |
55 | ||
303ee601 | 56 | static int specifier_prefix(char specifier, const void *data, const void *userdata, char **ret) { |
cfd559c9 | 57 | const UnitFileInstallInfo *i = userdata; |
19f6d710 | 58 | |
7584d236 ZJS |
59 | assert(i); |
60 | ||
7410616c | 61 | return unit_name_to_prefix(i->name, ret); |
7584d236 ZJS |
62 | } |
63 | ||
303ee601 | 64 | static int specifier_instance(char specifier, const void *data, const void *userdata, char **ret) { |
6e3136b2 | 65 | const UnitFileInstallInfo *i = userdata; |
7584d236 ZJS |
66 | char *instance; |
67 | int r; | |
68 | ||
69 | assert(i); | |
70 | ||
71 | r = unit_name_to_instance(i->name, &instance); | |
72 | if (r < 0) | |
19f6d710 LP |
73 | return r; |
74 | ||
6e3136b2 | 75 | if (isempty(instance)) { |
87e4e28d | 76 | r = free_and_strdup(&instance, strempty(i->default_instance)); |
402a81c7 ZJS |
77 | if (r < 0) |
78 | return r; | |
19f6d710 LP |
79 | } |
80 | ||
81 | *ret = instance; | |
82 | return 0; | |
7584d236 ZJS |
83 | } |
84 | ||
303ee601 | 85 | static int specifier_last_component(char specifier, const void *data, const void *userdata, char **ret) { |
250e9fad ZJS |
86 | _cleanup_free_ char *prefix = NULL; |
87 | char *dash; | |
88 | int r; | |
89 | ||
90 | r = specifier_prefix(specifier, data, userdata, &prefix); | |
91 | if (r < 0) | |
92 | return r; | |
93 | ||
94 | dash = strrchr(prefix, '-'); | |
95 | if (dash) { | |
96 | dash = strdup(dash + 1); | |
97 | if (!dash) | |
98 | return -ENOMEM; | |
99 | *ret = dash; | |
100 | } else | |
101 | *ret = TAKE_PTR(prefix); | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
303ee601 | 106 | int install_full_printf(const UnitFileInstallInfo *i, const char *format, char **ret) { |
7584d236 ZJS |
107 | /* This is similar to unit_full_printf() but does not support |
108 | * anything path-related. | |
109 | * | |
110 | * %n: the full id of the unit (foo@bar.waldo) | |
111 | * %N: the id of the unit without the suffix (foo@bar) | |
112 | * %p: the prefix (foo) | |
113 | * %i: the instance (bar) | |
114 | ||
79413b67 LP |
115 | * %U the UID of the running user |
116 | * %u the username of running user | |
7584d236 | 117 | * %m the machine ID of the running system |
38b38500 | 118 | * %H the hostname of the running system |
7584d236 | 119 | * %b the boot ID of the running system |
6aaa8c2f | 120 | * %v `uname -r` of the running system |
7584d236 ZJS |
121 | */ |
122 | ||
123 | const Specifier table[] = { | |
cfd559c9 | 124 | { 'n', specifier_name, NULL }, |
7584d236 ZJS |
125 | { 'N', specifier_prefix_and_instance, NULL }, |
126 | { 'p', specifier_prefix, NULL }, | |
127 | { 'i', specifier_instance, NULL }, | |
250e9fad | 128 | { 'j', specifier_last_component, NULL }, |
7584d236 | 129 | |
b75f0c69 DC |
130 | { 'g', specifier_group_name, NULL }, |
131 | { 'G', specifier_group_id, NULL }, | |
79413b67 | 132 | { 'U', specifier_user_id, NULL }, |
3f0b2f0f | 133 | { 'u', specifier_user_name, NULL }, |
7584d236 ZJS |
134 | |
135 | { 'm', specifier_machine_id, NULL }, | |
7584d236 | 136 | { 'b', specifier_boot_id, NULL }, |
268f5a54 | 137 | { 'H', specifier_host_name, NULL }, |
6aaa8c2f | 138 | { 'v', specifier_kernel_release, NULL }, |
268f5a54 LP |
139 | { 'a', specifier_architecture, NULL }, |
140 | { 'o', specifier_os_id, NULL }, | |
141 | { 'w', specifier_os_version_id, NULL }, | |
142 | { 'B', specifier_os_build_id, NULL }, | |
143 | { 'W', specifier_os_variant_id, NULL }, | |
6aaa8c2f | 144 | {} |
7584d236 ZJS |
145 | }; |
146 | ||
147 | assert(i); | |
148 | assert(format); | |
19f6d710 | 149 | assert(ret); |
7584d236 | 150 | |
19f6d710 | 151 | return specifier_printf(format, table, i, ret); |
7584d236 | 152 | } |