]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-settings.c
tree-wide: clean up log_syntax() usage
[thirdparty/systemd.git] / src / nspawn / nspawn-settings.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2015 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "util.h"
23 #include "conf-parser.h"
24 #include "strv.h"
25 #include "cap-list.h"
26
27 #include "nspawn-settings.h"
28
29 int settings_load(FILE *f, const char *path, Settings **ret) {
30 _cleanup_(settings_freep) Settings *s = NULL;
31 int r;
32
33 assert(path);
34 assert(ret);
35
36 s = new0(Settings, 1);
37 if (!s)
38 return -ENOMEM;
39
40 s->boot = -1;
41 s->personality = PERSONALITY_INVALID;
42
43 s->read_only = -1;
44 s->volatile_mode = _VOLATILE_MODE_INVALID;
45
46 s->private_network = -1;
47 s->network_veth = -1;
48
49 r = config_parse(NULL, path, f,
50 "Exec\0"
51 "Network\0"
52 "Files\0",
53 config_item_perf_lookup, nspawn_gperf_lookup,
54 false,
55 false,
56 true,
57 s);
58 if (r < 0)
59 return r;
60
61 *ret = s;
62 s = NULL;
63
64 return 0;
65 }
66
67 Settings* settings_free(Settings *s) {
68
69 if (!s)
70 return NULL;
71
72 strv_free(s->parameters);
73 strv_free(s->environment);
74 free(s->user);
75
76 strv_free(s->network_interfaces);
77 strv_free(s->network_macvlan);
78 strv_free(s->network_ipvlan);
79 free(s->network_bridge);
80 expose_port_free_all(s->expose_ports);
81
82 custom_mount_free_all(s->custom_mounts, s->n_custom_mounts);
83 free(s);
84
85 return NULL;
86 }
87
88 DEFINE_CONFIG_PARSE_ENUM(config_parse_volatile_mode, volatile_mode, VolatileMode, "Failed to parse volatile mode");
89
90 int config_parse_expose_port(
91 const char *unit,
92 const char *filename,
93 unsigned line,
94 const char *section,
95 unsigned section_line,
96 const char *lvalue,
97 int ltype,
98 const char *rvalue,
99 void *data,
100 void *userdata) {
101
102 Settings *s = data;
103 int r;
104
105 assert(filename);
106 assert(lvalue);
107 assert(rvalue);
108
109 r = expose_port_parse(&s->expose_ports, rvalue);
110 if (r == -EEXIST) {
111 log_syntax(unit, LOG_ERR, filename, line, r, "Duplicate port specification, ignoring: %s", rvalue);
112 return 0;
113 }
114 if (r < 0) {
115 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse host port %s: %m", rvalue);
116 return 0;
117 }
118
119 return 0;
120 }
121
122 int config_parse_capability(
123 const char *unit,
124 const char *filename,
125 unsigned line,
126 const char *section,
127 unsigned section_line,
128 const char *lvalue,
129 int ltype,
130 const char *rvalue,
131 void *data,
132 void *userdata) {
133
134 uint64_t u = 0, *result = data;
135 int r;
136
137 assert(filename);
138 assert(lvalue);
139 assert(rvalue);
140
141 for (;;) {
142 _cleanup_free_ char *word = NULL;
143 int cap;
144
145 r = extract_first_word(&rvalue, &word, NULL, 0);
146 if (r < 0) {
147 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract capability string, ignoring: %s", rvalue);
148 return 0;
149 }
150 if (r == 0)
151 break;
152
153 cap = capability_from_name(word);
154 if (cap < 0) {
155 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse capability, ignoring: %s", word);
156 continue;
157 }
158
159 u |= 1 << ((uint64_t) cap);
160 }
161
162 if (u == 0)
163 return 0;
164
165 *result |= u;
166 return 0;
167 }
168
169 int config_parse_id128(
170 const char *unit,
171 const char *filename,
172 unsigned line,
173 const char *section,
174 unsigned section_line,
175 const char *lvalue,
176 int ltype,
177 const char *rvalue,
178 void *data,
179 void *userdata) {
180
181 sd_id128_t t, *result = data;
182 int r;
183
184 assert(filename);
185 assert(lvalue);
186 assert(rvalue);
187
188 r = sd_id128_from_string(rvalue, &t);
189 if (r < 0) {
190 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse 128bit ID/UUID, ignoring: %s", rvalue);
191 return 0;
192 }
193
194 *result = t;
195 return 0;
196 }
197
198 int config_parse_bind(
199 const char *unit,
200 const char *filename,
201 unsigned line,
202 const char *section,
203 unsigned section_line,
204 const char *lvalue,
205 int ltype,
206 const char *rvalue,
207 void *data,
208 void *userdata) {
209
210 Settings *settings = data;
211 int r;
212
213 assert(filename);
214 assert(lvalue);
215 assert(rvalue);
216
217 r = bind_mount_parse(&settings->custom_mounts, &settings->n_custom_mounts, rvalue, ltype);
218 if (r < 0) {
219 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid bind mount specification %s: %m", rvalue);
220 return 0;
221 }
222
223 return 0;
224 }
225
226 int config_parse_tmpfs(
227 const char *unit,
228 const char *filename,
229 unsigned line,
230 const char *section,
231 unsigned section_line,
232 const char *lvalue,
233 int ltype,
234 const char *rvalue,
235 void *data,
236 void *userdata) {
237
238 Settings *settings = data;
239 int r;
240
241 assert(filename);
242 assert(lvalue);
243 assert(rvalue);
244
245 r = tmpfs_mount_parse(&settings->custom_mounts, &settings->n_custom_mounts, rvalue);
246 if (r < 0) {
247 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid temporary file system specification %s: %m", rvalue);
248 return 0;
249 }
250
251 if (settings->network_bridge)
252 settings->network_veth = true;
253
254 if (settings->network_interfaces ||
255 settings->network_macvlan ||
256 settings->network_ipvlan ||
257 settings->network_bridge ||
258 settings->network_veth)
259 settings->private_network = true;
260
261 return 0;
262 }