2 This file is part of systemd.
4 Copyright 2013 Zbigniew Jędrzejewski-Szmek
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "formats-util.h"
26 #include "install-printf.h"
29 #include "specifier.h"
30 #include "string-util.h"
31 #include "unit-name.h"
32 #include "user-util.h"
34 static int specifier_prefix_and_instance(char specifier
, void *data
, void *userdata
, char **ret
) {
35 const UnitFileInstallInfo
*i
= userdata
;
36 _cleanup_free_
char *prefix
= NULL
;
41 r
= unit_name_to_prefix_and_instance(i
->name
, &prefix
);
45 if (endswith(prefix
, "@") && i
->default_instance
) {
48 ans
= strjoin(prefix
, i
->default_instance
);
60 static int specifier_name(char specifier
, void *data
, void *userdata
, char **ret
) {
61 const UnitFileInstallInfo
*i
= userdata
;
66 if (unit_name_is_valid(i
->name
, UNIT_NAME_TEMPLATE
) && i
->default_instance
)
67 return unit_name_replace_instance(i
->name
, i
->default_instance
, ret
);
69 ans
= strdup(i
->name
);
76 static int specifier_prefix(char specifier
, void *data
, void *userdata
, char **ret
) {
77 const UnitFileInstallInfo
*i
= userdata
;
81 return unit_name_to_prefix(i
->name
, ret
);
84 static int specifier_instance(char specifier
, void *data
, void *userdata
, char **ret
) {
85 const UnitFileInstallInfo
*i
= userdata
;
91 r
= unit_name_to_instance(i
->name
, &instance
);
95 if (isempty(instance
)) {
96 instance
= strdup(i
->default_instance
?: "");
105 static int specifier_user_name(char specifier
, void *data
, void *userdata
, char **ret
) {
108 /* If we are UID 0 (root), this will not result in NSS,
109 * otherwise it might. This is good, as we want to be able to
110 * run this in PID 1, where our user ID is 0, but where NSS
111 * lookups are not allowed.
113 * We don't user getusername_malloc() here, because we don't want to look
114 * at $USER, to remain consistent with specifer_user_id() below.
117 t
= uid_to_name(getuid());
125 static int specifier_user_id(char specifier
, void *data
, void *userdata
, char **ret
) {
127 if (asprintf(ret
, UID_FMT
, getuid()) < 0)
133 int install_full_printf(UnitFileInstallInfo
*i
, const char *format
, char **ret
) {
135 /* This is similar to unit_full_printf() but does not support
136 * anything path-related.
138 * %n: the full id of the unit (foo@bar.waldo)
139 * %N: the id of the unit without the suffix (foo@bar)
140 * %p: the prefix (foo)
141 * %i: the instance (bar)
143 * %U the UID of the running user
144 * %u the username of running user
145 * %m the machine ID of the running system
146 * %H the host name of the running system
147 * %b the boot ID of the running system
148 * %v `uname -r` of the running system
151 const Specifier table
[] = {
152 { 'n', specifier_name
, NULL
},
153 { 'N', specifier_prefix_and_instance
, NULL
},
154 { 'p', specifier_prefix
, NULL
},
155 { 'i', specifier_instance
, NULL
},
157 { 'U', specifier_user_id
, NULL
},
158 { 'u', specifier_user_name
, NULL
},
160 { 'm', specifier_machine_id
, NULL
},
161 { 'H', specifier_host_name
, NULL
},
162 { 'b', specifier_boot_id
, NULL
},
163 { 'v', specifier_kernel_release
, NULL
},
171 return specifier_printf(format
, table
, i
, ret
);