]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/specifier.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/utsname.h>
30 #include "alloc-util.h"
31 #include "hostname-util.h"
33 #include "specifier.h"
34 #include "string-util.h"
36 #include "user-util.h"
39 * Generic infrastructure for replacing %x style specifiers in
40 * strings. Will call a callback for each replacement.
44 /* Any ASCII character or digit: our pool of potential specifiers,
45 * and "%" used for escaping. */
46 #define POSSIBLE_SPECIFIERS ALPHANUMERICAL "%"
48 int specifier_printf(const char *text
, const Specifier table
[], void *userdata
, char **_ret
) {
50 _cleanup_free_
char *ret
= NULL
;
66 for (f
= text
; *f
; f
++, l
--) {
74 for (i
= table
; i
->specifier
; i
++)
75 if (i
->specifier
== *f
)
79 _cleanup_free_
char *w
= NULL
;
83 r
= i
->lookup(i
->specifier
, i
->data
, userdata
, &w
);
90 n
= new(char, j
+ k
+ l
+ 1);
97 free_and_replace(ret
, n
);
99 } else if (strchr(POSSIBLE_SPECIFIERS
, *f
))
100 /* Oops, an unknown specifier. */
109 } else if (*f
== '%')
115 /* if string ended with a stray %, also end with % */
120 *_ret
= TAKE_PTR(ret
);
124 /* Generic handler for simple string replacements */
126 int specifier_string(char specifier
, void *data
, void *userdata
, char **ret
) {
129 n
= strdup(strempty(data
));
137 int specifier_machine_id(char specifier
, void *data
, void *userdata
, char **ret
) {
142 r
= sd_id128_get_machine(&id
);
150 *ret
= sd_id128_to_string(id
, n
);
154 int specifier_boot_id(char specifier
, void *data
, void *userdata
, char **ret
) {
159 r
= sd_id128_get_boot(&id
);
167 *ret
= sd_id128_to_string(id
, n
);
171 int specifier_host_name(char specifier
, void *data
, void *userdata
, char **ret
) {
174 n
= gethostname_malloc();
182 int specifier_kernel_release(char specifier
, void *data
, void *userdata
, char **ret
) {
191 n
= strdup(uts
.release
);
199 int specifier_user_name(char specifier
, void *data
, void *userdata
, char **ret
) {
202 /* If we are UID 0 (root), this will not result in NSS, otherwise it might. This is good, as we want to be able
203 * to run this in PID 1, where our user ID is 0, but where NSS lookups are not allowed.
205 * We don't use getusername_malloc() here, because we don't want to look at $USER, to remain consistent with
206 * specifer_user_id() below.
209 t
= uid_to_name(getuid());
217 int specifier_user_id(char specifier
, void *data
, void *userdata
, char **ret
) {
219 if (asprintf(ret
, UID_FMT
, getuid()) < 0)
225 int specifier_user_home(char specifier
, void *data
, void *userdata
, char **ret
) {
227 /* On PID 1 (which runs as root) this will not result in NSS,
228 * which is good. See above */
230 return get_home_dir(ret
);
233 int specifier_user_shell(char specifier
, void *data
, void *userdata
, char **ret
) {
235 /* On PID 1 (which runs as root) this will not result in NSS,
236 * which is good. See above */
238 return get_shell(ret
);
241 int specifier_escape_strv(char **l
, char ***ret
) {
246 if (strv_isempty(l
)) {
251 z
= new(char*, strv_length(l
)+1);
255 for (p
= l
, q
= z
; *p
; p
++, q
++) {
257 *q
= specifier_escape(*p
);