]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/install-printf.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / shared / install-printf.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Zbigniew Jędrzejewski-Szmek
6
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.
11
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.
16
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/>.
19 ***/
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "format-util.h"
27 #include "install-printf.h"
28 #include "install.h"
29 #include "macro.h"
30 #include "specifier.h"
31 #include "string-util.h"
32 #include "unit-name.h"
33 #include "user-util.h"
34
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;
38 int r;
39
40 assert(i);
41
42 r = unit_name_to_prefix_and_instance(i->name, &prefix);
43 if (r < 0)
44 return r;
45
46 if (endswith(prefix, "@") && i->default_instance) {
47 char *ans;
48
49 ans = strjoin(prefix, i->default_instance);
50 if (!ans)
51 return -ENOMEM;
52 *ret = ans;
53 } else {
54 *ret = prefix;
55 prefix = NULL;
56 }
57
58 return 0;
59 }
60
61 static int specifier_name(char specifier, void *data, void *userdata, char **ret) {
62 const UnitFileInstallInfo *i = userdata;
63 char *ans;
64
65 assert(i);
66
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);
69
70 ans = strdup(i->name);
71 if (!ans)
72 return -ENOMEM;
73 *ret = ans;
74 return 0;
75 }
76
77 static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
78 const UnitFileInstallInfo *i = userdata;
79
80 assert(i);
81
82 return unit_name_to_prefix(i->name, ret);
83 }
84
85 static int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
86 const UnitFileInstallInfo *i = userdata;
87 char *instance;
88 int r;
89
90 assert(i);
91
92 r = unit_name_to_instance(i->name, &instance);
93 if (r < 0)
94 return r;
95
96 if (isempty(instance)) {
97 r = free_and_strdup(&instance, i->default_instance ?: "");
98 if (r < 0)
99 return r;
100 }
101
102 *ret = instance;
103 return 0;
104 }
105
106 static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
107 char *t;
108
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.
113
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.
116 */
117
118 t = uid_to_name(getuid());
119 if (!t)
120 return -ENOMEM;
121
122 *ret = t;
123 return 0;
124 }
125
126 static int specifier_user_id(char specifier, void *data, void *userdata, char **ret) {
127
128 if (asprintf(ret, UID_FMT, getuid()) < 0)
129 return -ENOMEM;
130
131 return 0;
132 }
133
134 int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) {
135
136 /* This is similar to unit_full_printf() but does not support
137 * anything path-related.
138 *
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)
143
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
150 */
151
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 },
157
158 { 'U', specifier_user_id, NULL },
159 { 'u', specifier_user_name, NULL },
160
161 { 'm', specifier_machine_id, NULL },
162 { 'H', specifier_host_name, NULL },
163 { 'b', specifier_boot_id, NULL },
164 { 'v', specifier_kernel_release, NULL },
165 {}
166 };
167
168 assert(i);
169 assert(format);
170 assert(ret);
171
172 return specifier_printf(format, table, i, ret);
173 }