Debian Codegraph @

src/core/unit-printf.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-path.h"
4
5 #include "alloc-util.h"
6 #include "creds-util.h"
7 #include "env-util.h"
8 #include "fd-util.h"
9 #include "fileio.h"
10 #include "manager.h"
11 #include "specifier.h"
12 #include "string-util.h"
13 #include "unit.h"
14 #include "unit-name.h"
15 #include "unit-printf.h"
16
17 static int specifier_prefix_and_instance(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
18 const Unit *u = ASSERT_PTR(userdata);
19
20 return unit_name_to_prefix_and_instance(u->id, ret);
21 }
22
23 static int specifier_prefix(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
24 const Unit *u = ASSERT_PTR(userdata);
25
26 return unit_name_to_prefix(u->id, ret);
27 }
28
29 static int specifier_prefix_unescaped(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
30 _cleanup_free_ char *p = NULL;
31 const Unit *u = ASSERT_PTR(userdata);
32 int r;
33
34 r = unit_name_to_prefix(u->id, &p);
35 if (r < 0)
36 return r;
37
38 return unit_name_unescape(p, ret);
39 }
40
41 static int specifier_instance_unescaped(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
42 const Unit *u = ASSERT_PTR(userdata);
43
44 return unit_name_unescape(strempty(u->instance), ret);
45 }
46
47 static int specifier_last_component(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
48 const Unit *u = ASSERT_PTR(userdata);
49 _cleanup_free_ char *prefix = NULL;
50 char *dash;
51 int r;
52
53 assert(ret);
54
55 r = unit_name_to_prefix(u->id, &prefix);
56 if (r < 0)
57 return r;
58
59 dash = strrchr(prefix, '-');
60 if (dash)
61 return specifier_string(specifier, dash + 1, root, userdata, ret);
62
63 *ret = TAKE_PTR(prefix);
64 return 0;
65 }
66
67 static int specifier_last_component_unescaped(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
68 _cleanup_free_ char *p = NULL;
69 int r;
70
71 r = specifier_last_component(specifier, data, root, userdata, &p);
72 if (r < 0)
73 return r;
74
75 return unit_name_unescape(p, ret);
76 }
77
78 static int specifier_filename(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
79 const Unit *u = ASSERT_PTR(userdata);
80
81 if (u->instance)
82 return unit_name_path_unescape(u->instance, ret);
83 else
84 return unit_name_to_path(u->id, ret);
85 }
86
87 static void bad_specifier(const Unit *u, char specifier) {
88 log_unit_warning(u, "Specifier '%%%c' used in unit configuration, which is deprecated. Please update your unit file, as it does not work as intended.", specifier);
89 }
90
91 static int specifier_cgroup(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
92 const Unit *u = ASSERT_PTR(userdata);
93
94 bad_specifier(u, specifier);
95
96 return unit_get_cgroup_path_with_fallback(u, ret);
97 }
98
99 static int specifier_cgroup_root(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
100 const Unit *u = ASSERT_PTR(userdata);
101
102 bad_specifier(u, specifier);
103
104 return strdup_to(ret, u->manager->cgroup_root);
105 }
106
107 static int specifier_cgroup_slice(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
108 const Unit *u = ASSERT_PTR(userdata), *slice;
109
110 bad_specifier(u, specifier);
111
112 slice = UNIT_GET_SLICE(u);
113 if (slice)
114 return unit_get_cgroup_path_with_fallback(slice, ret);
115
116 return strdup_to(ret, u->manager->cgroup_root);
117 }
118
119 static int specifier_special_directory(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
120 const Unit *u = ASSERT_PTR(userdata);
121
122 return strdup_to(ret, u->manager->prefix[PTR_TO_UINT(data)]);
123 }
124
125 static int specifier_credentials_dir(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
126 const Unit *u = ASSERT_PTR(userdata);
127 char *d;
128
129 assert(ret);
130
131 d = strjoin(u->manager->prefix[EXEC_DIRECTORY_RUNTIME], "/credentials/", u->id);
132 if (!d)
133 return -ENOMEM;
134
135 *ret = d;
136 return 0;
137 }
138
139 static int specifier_shared_data_dir(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
140 const Unit *u = ASSERT_PTR(userdata);
141
142 assert(ret);
143
144 return sd_path_lookup(MANAGER_IS_SYSTEM(u->manager) ? SD_PATH_SYSTEM_SHARED : SD_PATH_USER_SHARED, NULL, ret);
145 }
146
147 int unit_name_printf(const Unit *u, const char *format, char **ret) {
148 /*
149 * This will use the passed string as format string and replace the following specifiers (which should all be
150 * safe for inclusion in unit names):
151 *
152 * %n: the full id of the unit ([email protected])
153 * %N: the id of the unit without the suffix (foo-aaa@bar)
154 * %p: the prefix (foo-aaa)
155 * %i: the instance (bar)
156 * %j: the last component of the prefix (aaa)
157 */
158
159 const Specifier table[] = {
160 { 'i', specifier_string, u->instance },
161 { 'j', specifier_last_component, NULL },
162 { 'n', specifier_string, u->id },
163 { 'N', specifier_prefix_and_instance, NULL },
164 { 'p', specifier_prefix, NULL },
165
166 COMMON_SYSTEM_SPECIFIERS,
167
168 COMMON_CREDS_SPECIFIERS(u->manager->runtime_scope),
169 {}
170 };
171
172 assert(u);
173 assert(format);
174 assert(ret);
175
176 return specifier_printf(format, UNIT_NAME_MAX, table, NULL, u, ret);
177 }
178
179 int unit_full_printf_full(const Unit *u, const char *format, size_t max_length, char **ret) {
180 /* This is similar to unit_name_printf() but also supports unescaping. Also, adds a couple of
181 * additional codes (which are likely not suitable for unescaped inclusion in unit names):
182 *
183 * %f: the unescaped instance if set, otherwise the id unescaped as path
184 *
185 * %c: cgroup path of unit (deprecated)
186 * %r: where units in this slice are placed in the cgroup tree (deprecated)
187 * %R: the root of this systemd's instance tree (deprecated)
188 *
189 * %C: the cache directory root (e.g. /var/cache or $XDG_CACHE_HOME)
190 * %d: the credentials directory ($CREDENTIALS_DIRECTORY)
191 * %D: the shared data root (e.g. /usr/share or $XDG_DATA_HOME)
192 * %E: the configuration directory root (e.g. /etc or $XDG_CONFIG_HOME)
193 * %L: the log directory root (e.g. /var/log or $XDG_STATE_HOME/log)
194 * %S: the state directory root (e.g. /var/lib or $XDG_STATE_HOME)
195 * %t: the runtime directory root (e.g. /run or $XDG_RUNTIME_DIR)
196 *
197 * %h: the homedir of the running user
198 * %s: the shell of the running user
199 *
200 * NOTICE: When you add new entries here, please be careful: specifiers which depend on settings of
201 * the unit file itself are broken by design, as they would resolve differently depending on whether
202 * they are used before or after the relevant configuration setting. Hence: don't add them.
203 */
204
205 assert(u);
206 assert(format);
207 assert(ret);
208
209 const Specifier table[] = {
210 { 'i', specifier_string, u->instance },
211 { 'I', specifier_instance_unescaped, NULL },
212 { 'j', specifier_last_component, NULL },
213 { 'J', specifier_last_component_unescaped, NULL },
214 { 'n', specifier_string, u->id },
215 { 'N', specifier_prefix_and_instance, NULL },
216 { 'p', specifier_prefix, NULL },
217 { 'P', specifier_prefix_unescaped, NULL },
218
219 { 'f', specifier_filename, NULL },
220 { 'y', specifier_real_path, u->fragment_path },
221 { 'Y', specifier_real_directory, u->fragment_path },
222
223 { 'c', specifier_cgroup, NULL }, /* deprecated, see 1b89b0c499cd4bf0ff389caab4ecaae6e75f9d4e */
224 { 'r', specifier_cgroup_slice, NULL }, /* deprecated, see 1b89b0c499cd4bf0ff389caab4ecaae6e75f9d4e */
225 { 'R', specifier_cgroup_root, NULL }, /* deprecated, see 1b89b0c499cd4bf0ff389caab4ecaae6e75f9d4e */
226
227 { 'C', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_CACHE) },
228 { 'd', specifier_credentials_dir, NULL },
229 { 'D', specifier_shared_data_dir, NULL },
230 { 'E', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_CONFIGURATION) },
231 { 'L', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_LOGS) },
232 { 'S', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_STATE) },
233 { 't', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_RUNTIME) },
234
235 { 'h', specifier_user_home, NULL },
236 { 's', specifier_user_shell, NULL },
237
238 COMMON_SYSTEM_SPECIFIERS,
239
240 COMMON_CREDS_SPECIFIERS(u->manager->runtime_scope),
241
242 COMMON_TMP_SPECIFIERS,
243 {}
244 };
245
246 return specifier_printf(format, max_length, table, NULL, u, ret);
247 }
248
249 int unit_full_printf(const Unit *u, const char *text, char **ret) {
250 return unit_full_printf_full(u, text, LONG_LINE_MAX, ret);
251 }
252
253 int unit_path_printf(const Unit *u, const char *text, char **ret) {
254 return unit_full_printf_full(u, text, PATH_MAX-1, ret);
255 }
256
257 int unit_fd_printf(const Unit *u, const char *text, char **ret) {
258 return unit_full_printf_full(u, text, FDNAME_MAX, ret);
259 }
260
261 int unit_cred_printf(const Unit *u, const char *text, char **ret) {
262 return unit_full_printf_full(u, text, CREDENTIAL_NAME_MAX, ret);
263 }
264
265 int unit_env_printf(const Unit *u, const char *text, char **ret) {
266 return unit_full_printf_full(u, text, sc_arg_max(), ret);
267 }