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