]>
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
) {
65 for (f
= text
; *f
; f
++, l
--) {
73 for (i
= table
; i
->specifier
; i
++)
74 if (i
->specifier
== *f
)
78 _cleanup_free_
char *w
= NULL
;
82 r
= i
->lookup(i
->specifier
, i
->data
, userdata
, &w
);
91 n
= new(char, j
+ k
+ l
+ 1);
104 } else if (strchr(POSSIBLE_SPECIFIERS
, *f
)) {
105 /* Oops, an unknown specifier. */
115 } else if (*f
== '%')
121 /* if string ended with a stray %, also end with % */
130 /* Generic handler for simple string replacements */
132 int specifier_string(char specifier
, void *data
, void *userdata
, char **ret
) {
135 n
= strdup(strempty(data
));
143 int specifier_machine_id(char specifier
, void *data
, void *userdata
, char **ret
) {
148 r
= sd_id128_get_machine(&id
);
156 *ret
= sd_id128_to_string(id
, n
);
160 int specifier_boot_id(char specifier
, void *data
, void *userdata
, char **ret
) {
165 r
= sd_id128_get_boot(&id
);
173 *ret
= sd_id128_to_string(id
, n
);
177 int specifier_host_name(char specifier
, void *data
, void *userdata
, char **ret
) {
180 n
= gethostname_malloc();
188 int specifier_kernel_release(char specifier
, void *data
, void *userdata
, char **ret
) {
197 n
= strdup(uts
.release
);
205 int specifier_user_name(char specifier
, void *data
, void *userdata
, char **ret
) {
208 /* If we are UID 0 (root), this will not result in NSS, otherwise it might. This is good, as we want to be able
209 * to run this in PID 1, where our user ID is 0, but where NSS lookups are not allowed.
211 * We don't use getusername_malloc() here, because we don't want to look at $USER, to remain consistent with
212 * specifer_user_id() below.
215 t
= uid_to_name(getuid());
223 int specifier_user_id(char specifier
, void *data
, void *userdata
, char **ret
) {
225 if (asprintf(ret
, UID_FMT
, getuid()) < 0)
231 int specifier_user_home(char specifier
, void *data
, void *userdata
, char **ret
) {
233 /* On PID 1 (which runs as root) this will not result in NSS,
234 * which is good. See above */
236 return get_home_dir(ret
);
239 int specifier_user_shell(char specifier
, void *data
, void *userdata
, char **ret
) {
241 /* On PID 1 (which runs as root) this will not result in NSS,
242 * which is good. See above */
244 return get_shell(ret
);
247 int specifier_escape_strv(char **l
, char ***ret
) {
252 if (strv_isempty(l
)) {
257 z
= new(char*, strv_length(l
)+1);
261 for (p
= l
, q
= z
; *p
; p
++, q
++) {
263 *q
= specifier_escape(*p
);