1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Zbigniew Jędrzejewski-Szmek
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 "format-util.h"
27 #include "install-printf.h"
30 #include "specifier.h"
31 #include "string-util.h"
32 #include "unit-name.h"
33 #include "user-util.h"
35 static int specifier_prefix_and_instance(char specifier
, void *data
, void *userdata
, char **ret
) {
36 const UnitFileInstallInfo
*i
= userdata
;
37 _cleanup_free_
char *prefix
= NULL
;
42 r
= unit_name_to_prefix_and_instance(i
->name
, &prefix
);
46 if (endswith(prefix
, "@") && i
->default_instance
) {
49 ans
= strjoin(prefix
, i
->default_instance
);
61 static int specifier_name(char specifier
, void *data
, void *userdata
, char **ret
) {
62 const UnitFileInstallInfo
*i
= userdata
;
67 if (unit_name_is_valid(i
->name
, UNIT_NAME_TEMPLATE
) && i
->default_instance
)
68 return unit_name_replace_instance(i
->name
, i
->default_instance
, ret
);
70 ans
= strdup(i
->name
);
77 static int specifier_prefix(char specifier
, void *data
, void *userdata
, char **ret
) {
78 const UnitFileInstallInfo
*i
= userdata
;
82 return unit_name_to_prefix(i
->name
, ret
);
85 static int specifier_instance(char specifier
, void *data
, void *userdata
, char **ret
) {
86 const UnitFileInstallInfo
*i
= userdata
;
92 r
= unit_name_to_instance(i
->name
, &instance
);
96 if (isempty(instance
)) {
97 r
= free_and_strdup(&instance
, i
->default_instance
?: "");
106 static int specifier_user_name(char specifier
, void *data
, void *userdata
, char **ret
) {
109 /* If we are UID 0 (root), this will not result in NSS,
110 * otherwise it might. This is good, as we want to be able to
111 * run this in PID 1, where our user ID is 0, but where NSS
112 * lookups are not allowed.
114 * We don't user getusername_malloc() here, because we don't want to look
115 * at $USER, to remain consistent with specifer_user_id() below.
118 t
= uid_to_name(getuid());
126 static int specifier_user_id(char specifier
, void *data
, void *userdata
, char **ret
) {
128 if (asprintf(ret
, UID_FMT
, getuid()) < 0)
134 int install_full_printf(UnitFileInstallInfo
*i
, const char *format
, char **ret
) {
136 /* This is similar to unit_full_printf() but does not support
137 * anything path-related.
139 * %n: the full id of the unit (foo@bar.waldo)
140 * %N: the id of the unit without the suffix (foo@bar)
141 * %p: the prefix (foo)
142 * %i: the instance (bar)
144 * %U the UID of the running user
145 * %u the username of running user
146 * %m the machine ID of the running system
147 * %H the host name of the running system
148 * %b the boot ID of the running system
149 * %v `uname -r` of the running system
152 const Specifier table
[] = {
153 { 'n', specifier_name
, NULL
},
154 { 'N', specifier_prefix_and_instance
, NULL
},
155 { 'p', specifier_prefix
, NULL
},
156 { 'i', specifier_instance
, NULL
},
158 { 'U', specifier_user_id
, NULL
},
159 { 'u', specifier_user_name
, NULL
},
161 { 'm', specifier_machine_id
, NULL
},
162 { 'H', specifier_host_name
, NULL
},
163 { 'b', specifier_boot_id
, NULL
},
164 { 'v', specifier_kernel_release
, NULL
},
172 return specifier_printf(format
, table
, i
, ret
);