]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/specifier.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include <sys/utsname.h>
12 #include "alloc-util.h"
14 #include "hostname-util.h"
16 #include "specifier.h"
17 #include "string-util.h"
19 #include "user-util.h"
22 * Generic infrastructure for replacing %x style specifiers in
23 * strings. Will call a callback for each replacement.
27 /* Any ASCII character or digit: our pool of potential specifiers,
28 * and "%" used for escaping. */
29 #define POSSIBLE_SPECIFIERS ALPHANUMERICAL "%"
31 int specifier_printf(const char *text
, const Specifier table
[], void *userdata
, char **_ret
) {
32 size_t l
, allocated
= 0;
33 _cleanup_free_
char *ret
= NULL
;
43 if (!GREEDY_REALLOC(ret
, allocated
, l
+ 1))
47 for (f
= text
; *f
; f
++, l
--)
54 for (i
= table
; i
->specifier
; i
++)
55 if (i
->specifier
== *f
)
59 _cleanup_free_
char *w
= NULL
;
62 r
= i
->lookup(i
->specifier
, i
->data
, userdata
, &w
);
69 if (!GREEDY_REALLOC(ret
, allocated
, j
+ k
+ l
+ 1))
71 memcpy(ret
+ j
, w
, k
);
73 } else if (strchr(POSSIBLE_SPECIFIERS
, *f
))
74 /* Oops, an unknown specifier. */
88 /* If string ended with a stray %, also end with % */
93 /* Try to deallocate unused bytes, but don't sweat it too much */
94 if ((size_t)(t
- ret
) < allocated
) {
95 t
= realloc(ret
, t
- ret
);
100 *_ret
= TAKE_PTR(ret
);
104 /* Generic handler for simple string replacements */
106 int specifier_string(char specifier
, void *data
, void *userdata
, char **ret
) {
109 n
= strdup(strempty(data
));
117 int specifier_machine_id(char specifier
, void *data
, void *userdata
, char **ret
) {
122 r
= sd_id128_get_machine(&id
);
130 *ret
= sd_id128_to_string(id
, n
);
134 int specifier_boot_id(char specifier
, void *data
, void *userdata
, char **ret
) {
139 r
= sd_id128_get_boot(&id
);
147 *ret
= sd_id128_to_string(id
, n
);
151 int specifier_host_name(char specifier
, void *data
, void *userdata
, char **ret
) {
154 n
= gethostname_malloc();
162 int specifier_kernel_release(char specifier
, void *data
, void *userdata
, char **ret
) {
171 n
= strdup(uts
.release
);
179 int specifier_user_name(char specifier
, void *data
, void *userdata
, char **ret
) {
182 /* If we are UID 0 (root), this will not result in NSS, otherwise it might. This is good, as we want to be able
183 * to run this in PID 1, where our user ID is 0, but where NSS lookups are not allowed.
185 * We don't use getusername_malloc() here, because we don't want to look at $USER, to remain consistent with
186 * specifer_user_id() below.
189 t
= uid_to_name(getuid());
197 int specifier_user_id(char specifier
, void *data
, void *userdata
, char **ret
) {
199 if (asprintf(ret
, UID_FMT
, getuid()) < 0)
205 int specifier_user_home(char specifier
, void *data
, void *userdata
, char **ret
) {
207 /* On PID 1 (which runs as root) this will not result in NSS,
208 * which is good. See above */
210 return get_home_dir(ret
);
213 int specifier_user_shell(char specifier
, void *data
, void *userdata
, char **ret
) {
215 /* On PID 1 (which runs as root) this will not result in NSS,
216 * which is good. See above */
218 return get_shell(ret
);
221 int specifier_tmp_dir(char specifier
, void *data
, void *userdata
, char **ret
) {
238 int specifier_var_tmp_dir(char specifier
, void *data
, void *userdata
, char **ret
) {
255 int specifier_escape_strv(char **l
, char ***ret
) {
260 if (strv_isempty(l
)) {
265 z
= new(char*, strv_length(l
)+1);
269 for (p
= l
, q
= z
; *p
; p
++, q
++) {
271 *q
= specifier_escape(*p
);