]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a7334b09 | 2 | /*** |
96b2fb93 | 3 | Copyright © 2012 Holger Hans Peter Freyther |
a7334b09 LP |
4 | ***/ |
5 | ||
3efd4195 | 6 | #include <errno.h> |
87f0e418 | 7 | #include <fcntl.h> |
25e870b5 | 8 | #include <linux/fs.h> |
5f5d8eab | 9 | #include <linux/oom.h> |
349cc4a5 | 10 | #if HAVE_SECCOMP |
618234a5 LP |
11 | #include <seccomp.h> |
12 | #endif | |
5f5d8eab LP |
13 | #include <sched.h> |
14 | #include <string.h> | |
3d57c6ab | 15 | #include <sys/resource.h> |
5f5d8eab | 16 | #include <sys/stat.h> |
3efd4195 | 17 | |
5f5d8eab | 18 | #include "af-list.h" |
cf0fbc49 | 19 | #include "alloc-util.h" |
57b7a260 | 20 | #include "all-units.h" |
5f5d8eab LP |
21 | #include "bus-error.h" |
22 | #include "bus-internal.h" | |
23 | #include "bus-util.h" | |
24 | #include "cap-list.h" | |
a103496c | 25 | #include "capability-util.h" |
5f5d8eab | 26 | #include "cgroup.h" |
3efd4195 | 27 | #include "conf-parser.h" |
618234a5 | 28 | #include "cpu-set-util.h" |
5f5d8eab LP |
29 | #include "env-util.h" |
30 | #include "errno-list.h" | |
4f5dd394 | 31 | #include "escape.h" |
3ffd4af2 | 32 | #include "fd-util.h" |
f4f15635 | 33 | #include "fs-util.h" |
08f3be7a | 34 | #include "hexdecoct.h" |
d3070fbd | 35 | #include "io-util.h" |
9eba9da4 | 36 | #include "ioprio.h" |
d3070fbd | 37 | #include "journal-util.h" |
3ffd4af2 | 38 | #include "load-fragment.h" |
5f5d8eab | 39 | #include "log.h" |
94f04347 | 40 | #include "missing.h" |
83555251 | 41 | #include "mount-util.h" |
6bedfcbb | 42 | #include "parse-util.h" |
9eb977db | 43 | #include "path-util.h" |
7b3e062c | 44 | #include "process-util.h" |
349cc4a5 | 45 | #if HAVE_SECCOMP |
57183d11 LP |
46 | #include "seccomp-util.h" |
47 | #endif | |
5f5d8eab | 48 | #include "securebits.h" |
07d46372 | 49 | #include "securebits-util.h" |
5f5d8eab | 50 | #include "signal-util.h" |
e045e325 | 51 | #include "socket-protocol-list.h" |
8fcde012 | 52 | #include "stat-util.h" |
07630cea | 53 | #include "string-util.h" |
5f5d8eab LP |
54 | #include "strv.h" |
55 | #include "unit-name.h" | |
56 | #include "unit-printf.h" | |
66dccd8d | 57 | #include "user-util.h" |
49cf4170 | 58 | #include "web-util.h" |
57183d11 | 59 | |
53577580 YW |
60 | static int supported_socket_protocol_from_string(const char *s) { |
61 | int r; | |
62 | ||
63 | if (isempty(s)) | |
64 | return IPPROTO_IP; | |
65 | ||
66 | r = socket_protocol_from_name(s); | |
67 | if (r < 0) | |
68 | return -EINVAL; | |
69 | if (!IN_SET(r, IPPROTO_UDPLITE, IPPROTO_SCTP)) | |
70 | return -EPROTONOSUPPORT; | |
71 | ||
72 | return r; | |
73 | } | |
74 | ||
75 | DEFINE_CONFIG_PARSE(config_parse_socket_protocol, supported_socket_protocol_from_string, "Failed to parse socket protocol"); | |
76 | DEFINE_CONFIG_PARSE(config_parse_exec_secure_bits, secure_bits_from_string, "Failed to parse secure bits"); | |
5afe510c | 77 | DEFINE_CONFIG_PARSE_ENUM(config_parse_collect_mode, collect_mode, CollectMode, "Failed to parse garbage collection mode"); |
53577580 YW |
78 | DEFINE_CONFIG_PARSE_ENUM(config_parse_device_policy, cgroup_device_policy, CGroupDevicePolicy, "Failed to parse device policy"); |
79 | DEFINE_CONFIG_PARSE_ENUM(config_parse_emergency_action, emergency_action, EmergencyAction, "Failed to parse failure action specifier"); | |
80 | DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_keyring_mode, exec_keyring_mode, ExecKeyringMode, "Failed to parse keyring mode"); | |
81 | DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_utmp_mode, exec_utmp_mode, ExecUtmpMode, "Failed to parse utmp mode"); | |
82 | DEFINE_CONFIG_PARSE_ENUM(config_parse_job_mode, job_mode, JobMode, "Failed to parse job mode"); | |
83 | DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode"); | |
84 | DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier"); | |
1e8c7bd5 YW |
85 | DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_home, protect_home, ProtectHome, "Failed to parse protect home value"); |
86 | DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_system, protect_system, ProtectSystem, "Failed to parse protect system value"); | |
53577580 YW |
87 | DEFINE_CONFIG_PARSE_ENUM(config_parse_runtime_preserve_mode, exec_preserve_mode, ExecPreserveMode, "Failed to parse runtime directory preserve mode"); |
88 | DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type"); | |
89 | DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier"); | |
90 | DEFINE_CONFIG_PARSE_ENUM(config_parse_socket_bind, socket_address_bind_ipv6_only_or_bool, SocketAddressBindIPv6Only, "Failed to parse bind IPv6 only value"); | |
91 | DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_ip_tos, ip_tos, int, -1, "Failed to parse IP TOS value"); | |
92 | DEFINE_CONFIG_PARSE_PTR(config_parse_blockio_weight, cg_blkio_weight_parse, uint64_t, "Invalid block IO weight"); | |
93 | DEFINE_CONFIG_PARSE_PTR(config_parse_cg_weight, cg_weight_parse, uint64_t, "Invalid weight"); | |
94 | DEFINE_CONFIG_PARSE_PTR(config_parse_cpu_shares, cg_cpu_shares_parse, uint64_t, "Invalid CPU shares"); | |
95 | DEFINE_CONFIG_PARSE_PTR(config_parse_exec_mount_flags, mount_propagation_flags_from_string, unsigned long, "Failed to parse mount flag"); | |
5afe510c | 96 | |
f32b43bd LP |
97 | int config_parse_unit_deps( |
98 | const char *unit, | |
99 | const char *filename, | |
100 | unsigned line, | |
101 | const char *section, | |
102 | unsigned section_line, | |
103 | const char *lvalue, | |
104 | int ltype, | |
105 | const char *rvalue, | |
106 | void *data, | |
107 | void *userdata) { | |
3efd4195 | 108 | |
f975e971 | 109 | UnitDependency d = ltype; |
87f0e418 | 110 | Unit *u = userdata; |
3d793d29 | 111 | const char *p; |
3efd4195 LP |
112 | |
113 | assert(filename); | |
114 | assert(lvalue); | |
115 | assert(rvalue); | |
3efd4195 | 116 | |
3d793d29 | 117 | p = rvalue; |
9ed794a3 | 118 | for (;;) { |
3d793d29 | 119 | _cleanup_free_ char *word = NULL, *k = NULL; |
3efd4195 | 120 | int r; |
3efd4195 | 121 | |
c89f52ac | 122 | r = extract_first_word(&p, &word, NULL, EXTRACT_RETAIN_ESCAPE); |
3d793d29 SS |
123 | if (r == 0) |
124 | break; | |
125 | if (r == -ENOMEM) | |
74051b9b | 126 | return log_oom(); |
3d793d29 SS |
127 | if (r < 0) { |
128 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); | |
129 | break; | |
130 | } | |
3efd4195 | 131 | |
3d793d29 | 132 | r = unit_name_printf(u, word, &k); |
19f6d710 | 133 | if (r < 0) { |
063c4b1a | 134 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word); |
19f6d710 LP |
135 | continue; |
136 | } | |
9e2f7c11 | 137 | |
eef85c4a | 138 | r = unit_add_dependency_by_name(u, d, k, NULL, true, UNIT_DEPENDENCY_FILE); |
57020a3a | 139 | if (r < 0) |
12ca818f | 140 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k); |
3efd4195 LP |
141 | } |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
f32b43bd LP |
146 | int config_parse_obsolete_unit_deps( |
147 | const char *unit, | |
148 | const char *filename, | |
149 | unsigned line, | |
150 | const char *section, | |
151 | unsigned section_line, | |
152 | const char *lvalue, | |
153 | int ltype, | |
154 | const char *rvalue, | |
155 | void *data, | |
156 | void *userdata) { | |
157 | ||
158 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
159 | "Unit dependency type %s= is obsolete, replacing by %s=, please update your unit file", lvalue, unit_dependency_to_string(ltype)); | |
160 | ||
161 | return config_parse_unit_deps(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata); | |
162 | } | |
163 | ||
b02cb41c LP |
164 | int config_parse_unit_string_printf( |
165 | const char *unit, | |
166 | const char *filename, | |
167 | unsigned line, | |
168 | const char *section, | |
169 | unsigned section_line, | |
170 | const char *lvalue, | |
171 | int ltype, | |
172 | const char *rvalue, | |
173 | void *data, | |
174 | void *userdata) { | |
932921b5 | 175 | |
74051b9b | 176 | _cleanup_free_ char *k = NULL; |
b02cb41c | 177 | Unit *u = userdata; |
19f6d710 | 178 | int r; |
932921b5 LP |
179 | |
180 | assert(filename); | |
181 | assert(lvalue); | |
182 | assert(rvalue); | |
f2d3769a | 183 | assert(u); |
932921b5 | 184 | |
19f6d710 | 185 | r = unit_full_printf(u, rvalue, &k); |
b02cb41c | 186 | if (r < 0) { |
063c4b1a | 187 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
b02cb41c LP |
188 | return 0; |
189 | } | |
932921b5 | 190 | |
b02cb41c | 191 | return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata); |
932921b5 LP |
192 | } |
193 | ||
12ca818f LP |
194 | int config_parse_unit_strv_printf( |
195 | const char *unit, | |
196 | const char *filename, | |
197 | unsigned line, | |
198 | const char *section, | |
199 | unsigned section_line, | |
200 | const char *lvalue, | |
201 | int ltype, | |
202 | const char *rvalue, | |
203 | void *data, | |
204 | void *userdata) { | |
8fef7659 LP |
205 | |
206 | Unit *u = userdata; | |
74051b9b | 207 | _cleanup_free_ char *k = NULL; |
19f6d710 | 208 | int r; |
8fef7659 LP |
209 | |
210 | assert(filename); | |
211 | assert(lvalue); | |
212 | assert(rvalue); | |
213 | assert(u); | |
214 | ||
19f6d710 | 215 | r = unit_full_printf(u, rvalue, &k); |
12ca818f | 216 | if (r < 0) { |
063c4b1a | 217 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
12ca818f LP |
218 | return 0; |
219 | } | |
8fef7659 | 220 | |
12ca818f | 221 | return config_parse_strv(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata); |
8fef7659 LP |
222 | } |
223 | ||
5f5d8eab LP |
224 | int config_parse_unit_path_printf( |
225 | const char *unit, | |
226 | const char *filename, | |
227 | unsigned line, | |
228 | const char *section, | |
229 | unsigned section_line, | |
230 | const char *lvalue, | |
231 | int ltype, | |
232 | const char *rvalue, | |
233 | void *data, | |
234 | void *userdata) { | |
6ea832a2 | 235 | |
74051b9b | 236 | _cleanup_free_ char *k = NULL; |
811ba7a0 | 237 | Unit *u = userdata; |
19f6d710 | 238 | int r; |
2c75fb73 | 239 | bool fatal = ltype; |
6ea832a2 LP |
240 | |
241 | assert(filename); | |
242 | assert(lvalue); | |
243 | assert(rvalue); | |
6ea832a2 LP |
244 | assert(u); |
245 | ||
e3c3d676 ZJS |
246 | /* Let's not bother with anything that is too long */ |
247 | if (strlen(rvalue) >= PATH_MAX) { | |
248 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
249 | "%s value too long%s.", | |
250 | lvalue, fatal ? "" : ", ignoring"); | |
251 | return fatal ? -ENAMETOOLONG : 0; | |
252 | } | |
253 | ||
19f6d710 | 254 | r = unit_full_printf(u, rvalue, &k); |
811ba7a0 | 255 | if (r < 0) { |
2c75fb73 | 256 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a | 257 | "Failed to resolve unit specifiers in '%s'%s: %m", |
e3c3d676 | 258 | rvalue, fatal ? "" : ", ignoring"); |
2c75fb73 | 259 | return fatal ? -ENOEXEC : 0; |
811ba7a0 | 260 | } |
6ea832a2 | 261 | |
811ba7a0 LP |
262 | return config_parse_path(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata); |
263 | } | |
264 | ||
265 | int config_parse_unit_path_strv_printf( | |
266 | const char *unit, | |
267 | const char *filename, | |
268 | unsigned line, | |
269 | const char *section, | |
270 | unsigned section_line, | |
271 | const char *lvalue, | |
272 | int ltype, | |
273 | const char *rvalue, | |
274 | void *data, | |
275 | void *userdata) { | |
276 | ||
a2a5291b | 277 | char ***x = data; |
811ba7a0 | 278 | Unit *u = userdata; |
811ba7a0 | 279 | int r; |
035fe294 | 280 | const char *p; |
811ba7a0 LP |
281 | |
282 | assert(filename); | |
283 | assert(lvalue); | |
284 | assert(rvalue); | |
285 | assert(u); | |
286 | ||
499295fb | 287 | if (isempty(rvalue)) { |
9f2d41a6 | 288 | *x = strv_free(*x); |
499295fb YW |
289 | return 0; |
290 | } | |
291 | ||
035fe294 ZJS |
292 | for (p = rvalue;;) { |
293 | _cleanup_free_ char *word = NULL, *k = NULL; | |
811ba7a0 | 294 | |
035fe294 ZJS |
295 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
296 | if (r == 0) | |
297 | return 0; | |
298 | if (r == -ENOMEM) | |
299 | return log_oom(); | |
300 | if (r < 0) { | |
301 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
302 | "Invalid syntax, ignoring: %s", rvalue); | |
303 | return 0; | |
304 | } | |
811ba7a0 | 305 | |
035fe294 | 306 | r = unit_full_printf(u, word, &k); |
811ba7a0 | 307 | if (r < 0) { |
035fe294 | 308 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a | 309 | "Failed to resolve unit specifiers in '%s', ignoring: %m", word); |
811ba7a0 LP |
310 | return 0; |
311 | } | |
312 | ||
2f4d31c1 YW |
313 | r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
314 | if (r < 0) | |
811ba7a0 | 315 | return 0; |
811ba7a0 LP |
316 | |
317 | r = strv_push(x, k); | |
318 | if (r < 0) | |
319 | return log_oom(); | |
811ba7a0 LP |
320 | k = NULL; |
321 | } | |
6ea832a2 LP |
322 | } |
323 | ||
e8e581bf ZJS |
324 | int config_parse_socket_listen(const char *unit, |
325 | const char *filename, | |
326 | unsigned line, | |
327 | const char *section, | |
71a61510 | 328 | unsigned section_line, |
e8e581bf ZJS |
329 | const char *lvalue, |
330 | int ltype, | |
331 | const char *rvalue, | |
332 | void *data, | |
333 | void *userdata) { | |
42f4e3c4 | 334 | |
b1389b0d ZJS |
335 | _cleanup_free_ SocketPort *p = NULL; |
336 | SocketPort *tail; | |
542563ba | 337 | Socket *s; |
19f6d710 | 338 | int r; |
16354eff | 339 | |
42f4e3c4 LP |
340 | assert(filename); |
341 | assert(lvalue); | |
342 | assert(rvalue); | |
343 | assert(data); | |
344 | ||
595ed347 | 345 | s = SOCKET(data); |
542563ba | 346 | |
74051b9b LP |
347 | if (isempty(rvalue)) { |
348 | /* An empty assignment removes all ports */ | |
349 | socket_free_ports(s); | |
350 | return 0; | |
351 | } | |
352 | ||
7f110ff9 LP |
353 | p = new0(SocketPort, 1); |
354 | if (!p) | |
74051b9b | 355 | return log_oom(); |
916abb21 | 356 | |
74051b9b | 357 | if (ltype != SOCKET_SOCKET) { |
2f4d31c1 | 358 | _cleanup_free_ char *k = NULL; |
916abb21 | 359 | |
2f4d31c1 | 360 | r = unit_full_printf(UNIT(s), rvalue, &k); |
19f6d710 | 361 | if (r < 0) { |
063c4b1a | 362 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
12ca818f | 363 | return 0; |
916abb21 LP |
364 | } |
365 | ||
2f4d31c1 YW |
366 | r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
367 | if (r < 0) | |
368 | return 0; | |
369 | ||
370 | free_and_replace(p->path, k); | |
371 | p->type = ltype; | |
916abb21 | 372 | |
7a22745a | 373 | } else if (streq(lvalue, "ListenNetlink")) { |
74051b9b | 374 | _cleanup_free_ char *k = NULL; |
1fd45a90 | 375 | |
19f6d710 | 376 | r = unit_full_printf(UNIT(s), rvalue, &k); |
12ca818f | 377 | if (r < 0) { |
063c4b1a | 378 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
12ca818f LP |
379 | return 0; |
380 | } | |
7a22745a | 381 | |
12ca818f | 382 | r = socket_address_parse_netlink(&p->address, k); |
1fd45a90 | 383 | if (r < 0) { |
063c4b1a | 384 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address value in '%s', ignoring: %m", k); |
7a22745a LP |
385 | return 0; |
386 | } | |
387 | ||
2f4d31c1 YW |
388 | p->type = SOCKET_SOCKET; |
389 | ||
542563ba | 390 | } else { |
74051b9b | 391 | _cleanup_free_ char *k = NULL; |
1fd45a90 | 392 | |
19f6d710 | 393 | r = unit_full_printf(UNIT(s), rvalue, &k); |
12ca818f | 394 | if (r < 0) { |
063c4b1a | 395 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
12ca818f LP |
396 | return 0; |
397 | } | |
542563ba | 398 | |
12ca818f | 399 | r = socket_address_parse_and_warn(&p->address, k); |
1fd45a90 | 400 | if (r < 0) { |
f847b8b7 | 401 | if (r != -EAFNOSUPPORT) |
063c4b1a | 402 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address value in '%s', ignoring: %m", k); |
c0b34696 | 403 | return 0; |
542563ba LP |
404 | } |
405 | ||
406 | if (streq(lvalue, "ListenStream")) | |
407 | p->address.type = SOCK_STREAM; | |
408 | else if (streq(lvalue, "ListenDatagram")) | |
409 | p->address.type = SOCK_DGRAM; | |
410 | else { | |
411 | assert(streq(lvalue, "ListenSequentialPacket")); | |
412 | p->address.type = SOCK_SEQPACKET; | |
413 | } | |
414 | ||
415 | if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) { | |
12ca818f | 416 | log_syntax(unit, LOG_ERR, filename, line, 0, "Address family not supported, ignoring: %s", rvalue); |
c0b34696 | 417 | return 0; |
542563ba | 418 | } |
2f4d31c1 YW |
419 | |
420 | p->type = SOCKET_SOCKET; | |
16354eff LP |
421 | } |
422 | ||
542563ba | 423 | p->fd = -1; |
15087cdb PS |
424 | p->auxiliary_fds = NULL; |
425 | p->n_auxiliary_fds = 0; | |
2e41a51e | 426 | p->socket = s; |
49f91047 | 427 | |
533f8a67 YW |
428 | LIST_FIND_TAIL(port, s->ports, tail); |
429 | LIST_INSERT_AFTER(port, s->ports, tail, p); | |
430 | ||
b1389b0d | 431 | p = NULL; |
542563ba | 432 | |
16354eff | 433 | return 0; |
42f4e3c4 LP |
434 | } |
435 | ||
41bf0590 LP |
436 | int config_parse_exec_nice( |
437 | const char *unit, | |
438 | const char *filename, | |
439 | unsigned line, | |
440 | const char *section, | |
441 | unsigned section_line, | |
442 | const char *lvalue, | |
443 | int ltype, | |
444 | const char *rvalue, | |
445 | void *data, | |
446 | void *userdata) { | |
034c6ed7 | 447 | |
fb33a393 | 448 | ExecContext *c = data; |
e8e581bf | 449 | int priority, r; |
034c6ed7 LP |
450 | |
451 | assert(filename); | |
452 | assert(lvalue); | |
453 | assert(rvalue); | |
454 | assert(data); | |
455 | ||
de5e6038 YW |
456 | if (isempty(rvalue)) { |
457 | c->nice_set = false; | |
458 | return 0; | |
459 | } | |
460 | ||
41bf0590 | 461 | r = parse_nice(rvalue, &priority); |
e8e581bf | 462 | if (r < 0) { |
41bf0590 LP |
463 | if (r == -ERANGE) |
464 | log_syntax(unit, LOG_ERR, filename, line, r, "Nice priority out of range, ignoring: %s", rvalue); | |
465 | else | |
063c4b1a | 466 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse nice priority '%s', ignoring: %m", rvalue); |
c0b34696 | 467 | return 0; |
034c6ed7 LP |
468 | } |
469 | ||
fb33a393 | 470 | c->nice = priority; |
71155933 | 471 | c->nice_set = true; |
fb33a393 | 472 | |
034c6ed7 LP |
473 | return 0; |
474 | } | |
475 | ||
e9eb2c02 LP |
476 | int config_parse_exec_oom_score_adjust( |
477 | const char* unit, | |
478 | const char *filename, | |
479 | unsigned line, | |
480 | const char *section, | |
481 | unsigned section_line, | |
482 | const char *lvalue, | |
483 | int ltype, | |
484 | const char *rvalue, | |
485 | void *data, | |
486 | void *userdata) { | |
034c6ed7 | 487 | |
fb33a393 | 488 | ExecContext *c = data; |
e8e581bf | 489 | int oa, r; |
034c6ed7 LP |
490 | |
491 | assert(filename); | |
492 | assert(lvalue); | |
493 | assert(rvalue); | |
494 | assert(data); | |
495 | ||
e9eb2c02 LP |
496 | if (isempty(rvalue)) { |
497 | c->oom_score_adjust_set = false; | |
c0b34696 | 498 | return 0; |
034c6ed7 LP |
499 | } |
500 | ||
e9eb2c02 | 501 | r = parse_oom_score_adjust(rvalue, &oa); |
e9eb2c02 | 502 | if (r < 0) { |
063c4b1a YW |
503 | if (r == -ERANGE) |
504 | log_syntax(unit, LOG_ERR, filename, line, r, "OOM score adjust value out of range, ignoring: %s", rvalue); | |
505 | else | |
506 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse the OOM score adjust value '%s', ignoring: %m", rvalue); | |
c0b34696 | 507 | return 0; |
034c6ed7 LP |
508 | } |
509 | ||
dd6c17b1 LP |
510 | c->oom_score_adjust = oa; |
511 | c->oom_score_adjust_set = true; | |
fb33a393 | 512 | |
034c6ed7 LP |
513 | return 0; |
514 | } | |
515 | ||
527b7a42 LP |
516 | int config_parse_exec( |
517 | const char *unit, | |
518 | const char *filename, | |
519 | unsigned line, | |
520 | const char *section, | |
521 | unsigned section_line, | |
522 | const char *lvalue, | |
523 | int ltype, | |
524 | const char *rvalue, | |
525 | void *data, | |
526 | void *userdata) { | |
034c6ed7 | 527 | |
46a0d98a | 528 | ExecCommand **e = data; |
5125e762 | 529 | Unit *u = userdata; |
46a0d98a FB |
530 | const char *p; |
531 | bool semicolon; | |
7f110ff9 | 532 | int r; |
034c6ed7 LP |
533 | |
534 | assert(filename); | |
535 | assert(lvalue); | |
536 | assert(rvalue); | |
61e5d8ed | 537 | assert(e); |
034c6ed7 | 538 | |
74051b9b | 539 | e += ltype; |
c83f1f30 | 540 | rvalue += strspn(rvalue, WHITESPACE); |
c83f1f30 | 541 | |
74051b9b LP |
542 | if (isempty(rvalue)) { |
543 | /* An empty assignment resets the list */ | |
f1acf85a | 544 | *e = exec_command_free_list(*e); |
74051b9b LP |
545 | return 0; |
546 | } | |
547 | ||
bd1b973f | 548 | p = rvalue; |
46a0d98a | 549 | do { |
dea7b6b0 | 550 | _cleanup_free_ char *path = NULL, *firstword = NULL; |
165a31c0 LP |
551 | ExecCommandFlags flags = 0; |
552 | bool ignore = false, separate_argv0 = false; | |
dea7b6b0 | 553 | _cleanup_free_ ExecCommand *nce = NULL; |
46a0d98a FB |
554 | _cleanup_strv_free_ char **n = NULL; |
555 | size_t nlen = 0, nbufsize = 0; | |
5125e762 | 556 | const char *f; |
6c666e26 | 557 | |
46a0d98a FB |
558 | semicolon = false; |
559 | ||
9a82ab95 | 560 | r = extract_first_word_and_warn(&p, &firstword, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, unit, filename, line, rvalue); |
46a0d98a FB |
561 | if (r <= 0) |
562 | return 0; | |
6c666e26 | 563 | |
46a0d98a | 564 | f = firstword; |
007f48bb | 565 | for (;;) { |
165a31c0 LP |
566 | /* We accept an absolute path as first argument. If it's prefixed with - and the path doesn't |
567 | * exist, we ignore it instead of erroring out; if it's prefixed with @, we allow overriding of | |
568 | * argv[0]; if it's prefixed with +, it will be run with full privileges and no sandboxing; if | |
569 | * it's prefixed with '!' we apply sandboxing, but do not change user/group credentials; if | |
570 | * it's prefixed with '!!', then we apply user/group credentials if the kernel supports ambient | |
571 | * capabilities -- if it doesn't we don't apply the credentials themselves, but do apply most | |
572 | * other sandboxing, with some special exceptions for changing UID. | |
573 | * | |
574 | * The idea is that '!!' may be used to write services that can take benefit of systemd's | |
575 | * UID/GID dropping if the kernel supports ambient creds, but provide an automatic fallback to | |
576 | * privilege dropping within the daemon if the kernel does not offer that. */ | |
577 | ||
578 | if (*f == '-' && !(flags & EXEC_COMMAND_IGNORE_FAILURE)) { | |
579 | flags |= EXEC_COMMAND_IGNORE_FAILURE; | |
46a0d98a | 580 | ignore = true; |
165a31c0 | 581 | } else if (*f == '@' && !separate_argv0) |
46a0d98a | 582 | separate_argv0 = true; |
165a31c0 LP |
583 | else if (*f == '+' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC))) |
584 | flags |= EXEC_COMMAND_FULLY_PRIVILEGED; | |
585 | else if (*f == '!' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC))) | |
586 | flags |= EXEC_COMMAND_NO_SETUID; | |
587 | else if (*f == '!' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_AMBIENT_MAGIC))) { | |
588 | flags &= ~EXEC_COMMAND_NO_SETUID; | |
589 | flags |= EXEC_COMMAND_AMBIENT_MAGIC; | |
590 | } else | |
46a0d98a | 591 | break; |
313cefa1 | 592 | f++; |
61e5d8ed | 593 | } |
46a0d98a | 594 | |
5125e762 LP |
595 | r = unit_full_printf(u, f, &path); |
596 | if (r < 0) { | |
bb28e684 | 597 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a | 598 | "Failed to resolve unit specifiers in '%s'%s: %m", |
bb28e684 ZJS |
599 | f, ignore ? ", ignoring" : ""); |
600 | return ignore ? 0 : -ENOEXEC; | |
5125e762 LP |
601 | } |
602 | ||
603 | if (isempty(path)) { | |
46a0d98a | 604 | /* First word is either "-" or "@" with no command. */ |
bb28e684 | 605 | log_syntax(unit, LOG_ERR, filename, line, 0, |
063c4b1a | 606 | "Empty path in command line%s: '%s'", |
bb28e684 ZJS |
607 | ignore ? ", ignoring" : "", rvalue); |
608 | return ignore ? 0 : -ENOEXEC; | |
b2fadec6 | 609 | } |
5125e762 | 610 | if (!string_is_safe(path)) { |
bb28e684 | 611 | log_syntax(unit, LOG_ERR, filename, line, 0, |
5008da1e ZJS |
612 | "Executable name contains special characters%s: %s", |
613 | ignore ? ", ignoring" : "", path); | |
bb28e684 | 614 | return ignore ? 0 : -ENOEXEC; |
46a0d98a | 615 | } |
5125e762 | 616 | if (endswith(path, "/")) { |
bb28e684 ZJS |
617 | log_syntax(unit, LOG_ERR, filename, line, 0, |
618 | "Executable path specifies a directory%s: %s", | |
5008da1e | 619 | ignore ? ", ignoring" : "", path); |
bb28e684 | 620 | return ignore ? 0 : -ENOEXEC; |
46a0d98a | 621 | } |
61e5d8ed | 622 | |
5008da1e ZJS |
623 | if (!path_is_absolute(path)) { |
624 | const char *prefix; | |
625 | bool found = false; | |
626 | ||
627 | if (!filename_is_valid(path)) { | |
628 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
629 | "Neither a valid executable name nor an absolute path%s: %s", | |
630 | ignore ? ", ignoring" : "", path); | |
631 | return ignore ? 0 : -ENOEXEC; | |
632 | } | |
633 | ||
634 | /* Resolve a single-component name to a full path */ | |
635 | NULSTR_FOREACH(prefix, DEFAULT_PATH_NULSTR) { | |
636 | _cleanup_free_ char *fullpath = NULL; | |
637 | ||
638 | fullpath = strjoin(prefix, "/", path); | |
639 | if (!fullpath) | |
640 | return log_oom(); | |
641 | ||
642 | if (access(fullpath, F_OK) >= 0) { | |
643 | free_and_replace(path, fullpath); | |
644 | found = true; | |
645 | break; | |
646 | } | |
647 | } | |
648 | ||
649 | if (!found) { | |
650 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
651 | "Executable \"%s\" not found in path \"%s\"%s", | |
652 | path, DEFAULT_PATH, ignore ? ", ignoring" : ""); | |
653 | return ignore ? 0 : -ENOEXEC; | |
654 | } | |
655 | } | |
656 | ||
46a0d98a | 657 | if (!separate_argv0) { |
5125e762 LP |
658 | char *w = NULL; |
659 | ||
46a0d98a FB |
660 | if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) |
661 | return log_oom(); | |
5125e762 LP |
662 | |
663 | w = strdup(path); | |
664 | if (!w) | |
46a0d98a | 665 | return log_oom(); |
5125e762 | 666 | n[nlen++] = w; |
46a0d98a FB |
667 | n[nlen] = NULL; |
668 | } | |
7f110ff9 | 669 | |
858d36c1 | 670 | path_simplify(path, false); |
46a0d98a | 671 | |
4b1c1753 | 672 | while (!isempty(p)) { |
5125e762 | 673 | _cleanup_free_ char *word = NULL, *resolved = NULL; |
46a0d98a FB |
674 | |
675 | /* Check explicitly for an unquoted semicolon as | |
676 | * command separator token. */ | |
677 | if (p[0] == ';' && (!p[1] || strchr(WHITESPACE, p[1]))) { | |
313cefa1 | 678 | p++; |
46a0d98a FB |
679 | p += strspn(p, WHITESPACE); |
680 | semicolon = true; | |
681 | break; | |
c8539536 | 682 | } |
7f110ff9 | 683 | |
5125e762 LP |
684 | /* Check for \; explicitly, to not confuse it with \\; or "\;" or "\\;" etc. |
685 | * extract_first_word() would return the same for all of those. */ | |
46a0d98a | 686 | if (p[0] == '\\' && p[1] == ';' && (!p[2] || strchr(WHITESPACE, p[2]))) { |
5125e762 LP |
687 | char *w; |
688 | ||
46a0d98a FB |
689 | p += 2; |
690 | p += strspn(p, WHITESPACE); | |
5125e762 | 691 | |
46a0d98a FB |
692 | if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) |
693 | return log_oom(); | |
5125e762 LP |
694 | |
695 | w = strdup(";"); | |
696 | if (!w) | |
46a0d98a | 697 | return log_oom(); |
5125e762 | 698 | n[nlen++] = w; |
46a0d98a FB |
699 | n[nlen] = NULL; |
700 | continue; | |
61e5d8ed | 701 | } |
c8539536 | 702 | |
9a82ab95 | 703 | r = extract_first_word_and_warn(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, unit, filename, line, rvalue); |
46a0d98a FB |
704 | if (r == 0) |
705 | break; | |
5125e762 | 706 | if (r < 0) |
bb28e684 | 707 | return ignore ? 0 : -ENOEXEC; |
5125e762 LP |
708 | |
709 | r = unit_full_printf(u, word, &resolved); | |
710 | if (r < 0) { | |
bb28e684 | 711 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a | 712 | "Failed to resolve unit specifiers in %s%s: %m", |
bb28e684 ZJS |
713 | word, ignore ? ", ignoring" : ""); |
714 | return ignore ? 0 : -ENOEXEC; | |
5125e762 | 715 | } |
46a0d98a FB |
716 | |
717 | if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) | |
718 | return log_oom(); | |
1cc6c93a YW |
719 | |
720 | n[nlen++] = TAKE_PTR(resolved); | |
46a0d98a | 721 | n[nlen] = NULL; |
61e5d8ed LP |
722 | } |
723 | ||
46a0d98a | 724 | if (!n || !n[0]) { |
bb28e684 ZJS |
725 | log_syntax(unit, LOG_ERR, filename, line, 0, |
726 | "Empty executable name or zeroeth argument%s: %s", | |
727 | ignore ? ", ignoring" : "", rvalue); | |
728 | return ignore ? 0 : -ENOEXEC; | |
7f110ff9 | 729 | } |
6c666e26 | 730 | |
7f110ff9 | 731 | nce = new0(ExecCommand, 1); |
46a0d98a FB |
732 | if (!nce) |
733 | return log_oom(); | |
61e5d8ed | 734 | |
1cc6c93a YW |
735 | nce->argv = TAKE_PTR(n); |
736 | nce->path = TAKE_PTR(path); | |
165a31c0 | 737 | nce->flags = flags; |
034c6ed7 | 738 | |
61e5d8ed | 739 | exec_command_append_list(e, nce); |
01f78473 | 740 | |
46a0d98a | 741 | /* Do not _cleanup_free_ these. */ |
46a0d98a | 742 | nce = NULL; |
034c6ed7 | 743 | |
46a0d98a FB |
744 | rvalue = p; |
745 | } while (semicolon); | |
034c6ed7 | 746 | |
46a0d98a | 747 | return 0; |
034c6ed7 LP |
748 | } |
749 | ||
d31645ad LP |
750 | int config_parse_socket_bindtodevice( |
751 | const char* unit, | |
752 | const char *filename, | |
753 | unsigned line, | |
754 | const char *section, | |
755 | unsigned section_line, | |
756 | const char *lvalue, | |
757 | int ltype, | |
758 | const char *rvalue, | |
759 | void *data, | |
760 | void *userdata) { | |
acbb0225 LP |
761 | |
762 | Socket *s = data; | |
acbb0225 LP |
763 | |
764 | assert(filename); | |
765 | assert(lvalue); | |
766 | assert(rvalue); | |
767 | assert(data); | |
768 | ||
063c4b1a YW |
769 | if (isempty(rvalue) || streq(rvalue, "*")) { |
770 | s->bind_to_device = mfree(s->bind_to_device); | |
771 | return 0; | |
772 | } | |
d31645ad | 773 | |
063c4b1a YW |
774 | if (!ifname_valid(rvalue)) { |
775 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid interface name, ignoring: %s", rvalue); | |
776 | return 0; | |
777 | } | |
acbb0225 | 778 | |
3c381a67 YW |
779 | if (free_and_strdup(&s->bind_to_device, rvalue) < 0) |
780 | return log_oom(); | |
acbb0225 LP |
781 | |
782 | return 0; | |
783 | } | |
784 | ||
9bd6a50e LP |
785 | int config_parse_exec_input( |
786 | const char *unit, | |
787 | const char *filename, | |
788 | unsigned line, | |
789 | const char *section, | |
790 | unsigned section_line, | |
791 | const char *lvalue, | |
792 | int ltype, | |
793 | const char *rvalue, | |
794 | void *data, | |
795 | void *userdata) { | |
52c239d7 | 796 | |
52c239d7 | 797 | ExecContext *c = data; |
2038c3f5 LP |
798 | Unit *u = userdata; |
799 | const char *n; | |
800 | ExecInput ei; | |
52c239d7 LB |
801 | int r; |
802 | ||
803 | assert(data); | |
804 | assert(filename); | |
805 | assert(line); | |
806 | assert(rvalue); | |
807 | ||
2038c3f5 LP |
808 | n = startswith(rvalue, "fd:"); |
809 | if (n) { | |
810 | _cleanup_free_ char *resolved = NULL; | |
811 | ||
812 | r = unit_full_printf(u, n, &resolved); | |
813 | if (r < 0) | |
063c4b1a | 814 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", n); |
2038c3f5 LP |
815 | |
816 | if (isempty(resolved)) | |
817 | resolved = mfree(resolved); | |
818 | else if (!fdname_is_valid(resolved)) { | |
819 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name: %s", resolved); | |
6f40aa45 | 820 | return -ENOEXEC; |
52c239d7 | 821 | } |
9bd6a50e | 822 | |
2038c3f5 LP |
823 | free_and_replace(c->stdio_fdname[STDIN_FILENO], resolved); |
824 | ||
825 | ei = EXEC_INPUT_NAMED_FD; | |
826 | ||
827 | } else if ((n = startswith(rvalue, "file:"))) { | |
828 | _cleanup_free_ char *resolved = NULL; | |
829 | ||
830 | r = unit_full_printf(u, n, &resolved); | |
52c239d7 | 831 | if (r < 0) |
063c4b1a | 832 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", n); |
9bd6a50e | 833 | |
2f4d31c1 YW |
834 | r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue); |
835 | if (r < 0) | |
6f40aa45 | 836 | return -ENOEXEC; |
2038c3f5 LP |
837 | |
838 | free_and_replace(c->stdio_file[STDIN_FILENO], resolved); | |
839 | ||
840 | ei = EXEC_INPUT_FILE; | |
9bd6a50e | 841 | |
52c239d7 | 842 | } else { |
9bd6a50e LP |
843 | ei = exec_input_from_string(rvalue); |
844 | if (ei < 0) { | |
52c239d7 | 845 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse input specifier, ignoring: %s", rvalue); |
9bd6a50e LP |
846 | return 0; |
847 | } | |
52c239d7 | 848 | } |
9bd6a50e | 849 | |
2038c3f5 | 850 | c->std_input = ei; |
9bd6a50e | 851 | return 0; |
52c239d7 LB |
852 | } |
853 | ||
08f3be7a LP |
854 | int config_parse_exec_input_text( |
855 | const char *unit, | |
856 | const char *filename, | |
857 | unsigned line, | |
858 | const char *section, | |
859 | unsigned section_line, | |
860 | const char *lvalue, | |
861 | int ltype, | |
862 | const char *rvalue, | |
863 | void *data, | |
864 | void *userdata) { | |
865 | ||
866 | _cleanup_free_ char *unescaped = NULL, *resolved = NULL; | |
867 | ExecContext *c = data; | |
868 | Unit *u = userdata; | |
869 | size_t sz; | |
870 | void *p; | |
871 | int r; | |
872 | ||
873 | assert(data); | |
874 | assert(filename); | |
875 | assert(line); | |
876 | assert(rvalue); | |
877 | ||
878 | if (isempty(rvalue)) { | |
879 | /* Reset if the empty string is assigned */ | |
880 | c->stdin_data = mfree(c->stdin_data); | |
881 | c->stdin_data_size = 0; | |
52c239d7 LB |
882 | return 0; |
883 | } | |
08f3be7a LP |
884 | |
885 | r = cunescape(rvalue, 0, &unescaped); | |
2038c3f5 | 886 | if (r < 0) |
063c4b1a | 887 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to decode C escaped text '%s': %m", rvalue); |
08f3be7a LP |
888 | |
889 | r = unit_full_printf(u, unescaped, &resolved); | |
2038c3f5 | 890 | if (r < 0) |
063c4b1a | 891 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", unescaped); |
08f3be7a LP |
892 | |
893 | sz = strlen(resolved); | |
894 | if (c->stdin_data_size + sz + 1 < c->stdin_data_size || /* check for overflow */ | |
895 | c->stdin_data_size + sz + 1 > EXEC_STDIN_DATA_MAX) { | |
2038c3f5 LP |
896 | log_syntax(unit, LOG_ERR, filename, line, 0, "Standard input data too large (%zu), maximum of %zu permitted, ignoring.", c->stdin_data_size + sz, (size_t) EXEC_STDIN_DATA_MAX); |
897 | return -E2BIG; | |
08f3be7a LP |
898 | } |
899 | ||
900 | p = realloc(c->stdin_data, c->stdin_data_size + sz + 1); | |
901 | if (!p) | |
902 | return log_oom(); | |
903 | ||
904 | *((char*) mempcpy((char*) p + c->stdin_data_size, resolved, sz)) = '\n'; | |
905 | ||
906 | c->stdin_data = p; | |
907 | c->stdin_data_size += sz + 1; | |
908 | ||
909 | return 0; | |
52c239d7 LB |
910 | } |
911 | ||
08f3be7a LP |
912 | int config_parse_exec_input_data( |
913 | const char *unit, | |
914 | const char *filename, | |
915 | unsigned line, | |
916 | const char *section, | |
917 | unsigned section_line, | |
918 | const char *lvalue, | |
919 | int ltype, | |
920 | const char *rvalue, | |
921 | void *data, | |
922 | void *userdata) { | |
923 | ||
08f3be7a LP |
924 | _cleanup_free_ void *p = NULL; |
925 | ExecContext *c = data; | |
926 | size_t sz; | |
927 | void *q; | |
928 | int r; | |
929 | ||
930 | assert(data); | |
931 | assert(filename); | |
932 | assert(line); | |
933 | assert(rvalue); | |
934 | ||
935 | if (isempty(rvalue)) { | |
936 | /* Reset if the empty string is assigned */ | |
937 | c->stdin_data = mfree(c->stdin_data); | |
938 | c->stdin_data_size = 0; | |
939 | return 0; | |
940 | } | |
941 | ||
081f36d8 | 942 | r = unbase64mem(rvalue, (size_t) -1, &p, &sz); |
2038c3f5 | 943 | if (r < 0) |
081f36d8 | 944 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to decode base64 data, ignoring: %s", rvalue); |
08f3be7a LP |
945 | |
946 | assert(sz > 0); | |
947 | ||
948 | if (c->stdin_data_size + sz < c->stdin_data_size || /* check for overflow */ | |
949 | c->stdin_data_size + sz > EXEC_STDIN_DATA_MAX) { | |
2038c3f5 LP |
950 | log_syntax(unit, LOG_ERR, filename, line, 0, "Standard input data too large (%zu), maximum of %zu permitted, ignoring.", c->stdin_data_size + sz, (size_t) EXEC_STDIN_DATA_MAX); |
951 | return -E2BIG; | |
08f3be7a LP |
952 | } |
953 | ||
954 | q = realloc(c->stdin_data, c->stdin_data_size + sz); | |
955 | if (!q) | |
956 | return log_oom(); | |
957 | ||
958 | memcpy((uint8_t*) q + c->stdin_data_size, p, sz); | |
959 | ||
960 | c->stdin_data = q; | |
961 | c->stdin_data_size += sz; | |
962 | ||
963 | return 0; | |
964 | } | |
965 | ||
2038c3f5 LP |
966 | int config_parse_exec_output( |
967 | const char *unit, | |
968 | const char *filename, | |
969 | unsigned line, | |
970 | const char *section, | |
971 | unsigned section_line, | |
972 | const char *lvalue, | |
973 | int ltype, | |
974 | const char *rvalue, | |
975 | void *data, | |
976 | void *userdata) { | |
977 | ||
978 | _cleanup_free_ char *resolved = NULL; | |
979 | const char *n; | |
52c239d7 | 980 | ExecContext *c = data; |
2038c3f5 | 981 | Unit *u = userdata; |
52c239d7 | 982 | ExecOutput eo; |
52c239d7 LB |
983 | int r; |
984 | ||
985 | assert(data); | |
986 | assert(filename); | |
987 | assert(line); | |
988 | assert(lvalue); | |
989 | assert(rvalue); | |
990 | ||
2038c3f5 LP |
991 | n = startswith(rvalue, "fd:"); |
992 | if (n) { | |
993 | r = unit_full_printf(u, n, &resolved); | |
994 | if (r < 0) | |
063c4b1a | 995 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n); |
2038c3f5 LP |
996 | |
997 | if (isempty(resolved)) | |
998 | resolved = mfree(resolved); | |
999 | else if (!fdname_is_valid(resolved)) { | |
1000 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name: %s", resolved); | |
6f40aa45 | 1001 | return -ENOEXEC; |
52c239d7 | 1002 | } |
2038c3f5 | 1003 | |
52c239d7 | 1004 | eo = EXEC_OUTPUT_NAMED_FD; |
2038c3f5 LP |
1005 | |
1006 | } else if ((n = startswith(rvalue, "file:"))) { | |
1007 | ||
1008 | r = unit_full_printf(u, n, &resolved); | |
1009 | if (r < 0) | |
063c4b1a | 1010 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n); |
2038c3f5 | 1011 | |
2f4d31c1 YW |
1012 | r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue); |
1013 | if (r < 0) | |
6f40aa45 | 1014 | return -ENOEXEC; |
2038c3f5 LP |
1015 | |
1016 | eo = EXEC_OUTPUT_FILE; | |
1017 | ||
566b7d23 ZD |
1018 | } else if ((n = startswith(rvalue, "append:"))) { |
1019 | ||
1020 | r = unit_full_printf(u, n, &resolved); | |
1021 | if (r < 0) | |
1022 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n); | |
1023 | ||
1024 | r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue); | |
1025 | if (r < 0) | |
1026 | return -ENOEXEC; | |
1027 | ||
1028 | eo = EXEC_OUTPUT_FILE_APPEND; | |
52c239d7 LB |
1029 | } else { |
1030 | eo = exec_output_from_string(rvalue); | |
2038c3f5 | 1031 | if (eo < 0) { |
52c239d7 LB |
1032 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse output specifier, ignoring: %s", rvalue); |
1033 | return 0; | |
1034 | } | |
1035 | } | |
1036 | ||
1037 | if (streq(lvalue, "StandardOutput")) { | |
2038c3f5 LP |
1038 | if (eo == EXEC_OUTPUT_NAMED_FD) |
1039 | free_and_replace(c->stdio_fdname[STDOUT_FILENO], resolved); | |
1040 | else | |
1041 | free_and_replace(c->stdio_file[STDOUT_FILENO], resolved); | |
1042 | ||
52c239d7 | 1043 | c->std_output = eo; |
2038c3f5 | 1044 | |
52c239d7 | 1045 | } else { |
2038c3f5 LP |
1046 | assert(streq(lvalue, "StandardError")); |
1047 | ||
1048 | if (eo == EXEC_OUTPUT_NAMED_FD) | |
1049 | free_and_replace(c->stdio_fdname[STDERR_FILENO], resolved); | |
1050 | else | |
1051 | free_and_replace(c->stdio_file[STDERR_FILENO], resolved); | |
1052 | ||
1053 | c->std_error = eo; | |
52c239d7 | 1054 | } |
2038c3f5 LP |
1055 | |
1056 | return 0; | |
52c239d7 | 1057 | } |
87f0e418 | 1058 | |
e8e581bf ZJS |
1059 | int config_parse_exec_io_class(const char *unit, |
1060 | const char *filename, | |
1061 | unsigned line, | |
1062 | const char *section, | |
71a61510 | 1063 | unsigned section_line, |
e8e581bf ZJS |
1064 | const char *lvalue, |
1065 | int ltype, | |
1066 | const char *rvalue, | |
1067 | void *data, | |
1068 | void *userdata) { | |
94f04347 LP |
1069 | |
1070 | ExecContext *c = data; | |
1071 | int x; | |
1072 | ||
1073 | assert(filename); | |
1074 | assert(lvalue); | |
1075 | assert(rvalue); | |
1076 | assert(data); | |
1077 | ||
617d253a YW |
1078 | if (isempty(rvalue)) { |
1079 | c->ioprio_set = false; | |
1080 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0); | |
1081 | return 0; | |
1082 | } | |
1083 | ||
f8b69d1d MS |
1084 | x = ioprio_class_from_string(rvalue); |
1085 | if (x < 0) { | |
12ca818f | 1086 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IO scheduling class, ignoring: %s", rvalue); |
c0b34696 | 1087 | return 0; |
0d87eb42 | 1088 | } |
94f04347 LP |
1089 | |
1090 | c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio)); | |
1091 | c->ioprio_set = true; | |
1092 | ||
1093 | return 0; | |
1094 | } | |
1095 | ||
e8e581bf ZJS |
1096 | int config_parse_exec_io_priority(const char *unit, |
1097 | const char *filename, | |
1098 | unsigned line, | |
1099 | const char *section, | |
71a61510 | 1100 | unsigned section_line, |
e8e581bf ZJS |
1101 | const char *lvalue, |
1102 | int ltype, | |
1103 | const char *rvalue, | |
1104 | void *data, | |
1105 | void *userdata) { | |
94f04347 LP |
1106 | |
1107 | ExecContext *c = data; | |
e8e581bf | 1108 | int i, r; |
94f04347 LP |
1109 | |
1110 | assert(filename); | |
1111 | assert(lvalue); | |
1112 | assert(rvalue); | |
1113 | assert(data); | |
1114 | ||
617d253a YW |
1115 | if (isempty(rvalue)) { |
1116 | c->ioprio_set = false; | |
1117 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0); | |
1118 | return 0; | |
1119 | } | |
1120 | ||
7f452159 LP |
1121 | r = ioprio_parse_priority(rvalue, &i); |
1122 | if (r < 0) { | |
12ca818f | 1123 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse IO priority, ignoring: %s", rvalue); |
c0b34696 | 1124 | return 0; |
071830ff LP |
1125 | } |
1126 | ||
94f04347 LP |
1127 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i); |
1128 | c->ioprio_set = true; | |
1129 | ||
071830ff LP |
1130 | return 0; |
1131 | } | |
1132 | ||
e8e581bf ZJS |
1133 | int config_parse_exec_cpu_sched_policy(const char *unit, |
1134 | const char *filename, | |
1135 | unsigned line, | |
1136 | const char *section, | |
71a61510 | 1137 | unsigned section_line, |
e8e581bf ZJS |
1138 | const char *lvalue, |
1139 | int ltype, | |
1140 | const char *rvalue, | |
1141 | void *data, | |
1142 | void *userdata) { | |
9eba9da4 | 1143 | |
94f04347 LP |
1144 | ExecContext *c = data; |
1145 | int x; | |
1146 | ||
1147 | assert(filename); | |
1148 | assert(lvalue); | |
1149 | assert(rvalue); | |
1150 | assert(data); | |
1151 | ||
b00e1a9e YW |
1152 | if (isempty(rvalue)) { |
1153 | c->cpu_sched_set = false; | |
1154 | c->cpu_sched_policy = SCHED_OTHER; | |
1155 | c->cpu_sched_priority = 0; | |
1156 | return 0; | |
1157 | } | |
1158 | ||
f8b69d1d MS |
1159 | x = sched_policy_from_string(rvalue); |
1160 | if (x < 0) { | |
12ca818f | 1161 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse CPU scheduling policy, ignoring: %s", rvalue); |
c0b34696 | 1162 | return 0; |
0d87eb42 | 1163 | } |
94f04347 LP |
1164 | |
1165 | c->cpu_sched_policy = x; | |
bb112710 HHPF |
1166 | /* Moving to or from real-time policy? We need to adjust the priority */ |
1167 | c->cpu_sched_priority = CLAMP(c->cpu_sched_priority, sched_get_priority_min(x), sched_get_priority_max(x)); | |
94f04347 LP |
1168 | c->cpu_sched_set = true; |
1169 | ||
1170 | return 0; | |
1171 | } | |
1172 | ||
e8e581bf ZJS |
1173 | int config_parse_exec_cpu_sched_prio(const char *unit, |
1174 | const char *filename, | |
1175 | unsigned line, | |
1176 | const char *section, | |
71a61510 | 1177 | unsigned section_line, |
e8e581bf ZJS |
1178 | const char *lvalue, |
1179 | int ltype, | |
1180 | const char *rvalue, | |
1181 | void *data, | |
1182 | void *userdata) { | |
9eba9da4 LP |
1183 | |
1184 | ExecContext *c = data; | |
e8e581bf | 1185 | int i, min, max, r; |
9eba9da4 LP |
1186 | |
1187 | assert(filename); | |
1188 | assert(lvalue); | |
1189 | assert(rvalue); | |
1190 | assert(data); | |
1191 | ||
e8e581bf ZJS |
1192 | r = safe_atoi(rvalue, &i); |
1193 | if (r < 0) { | |
063c4b1a | 1194 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU scheduling priority, ignoring: %s", rvalue); |
c0b34696 | 1195 | return 0; |
94f04347 | 1196 | } |
9eba9da4 | 1197 | |
bb112710 HHPF |
1198 | /* On Linux RR/FIFO range from 1 to 99 and OTHER/BATCH may only be 0 */ |
1199 | min = sched_get_priority_min(c->cpu_sched_policy); | |
1200 | max = sched_get_priority_max(c->cpu_sched_policy); | |
1201 | ||
1202 | if (i < min || i > max) { | |
12ca818f | 1203 | log_syntax(unit, LOG_ERR, filename, line, 0, "CPU scheduling priority is out of range, ignoring: %s", rvalue); |
bb112710 HHPF |
1204 | return 0; |
1205 | } | |
1206 | ||
94f04347 LP |
1207 | c->cpu_sched_priority = i; |
1208 | c->cpu_sched_set = true; | |
1209 | ||
1210 | return 0; | |
1211 | } | |
1212 | ||
e8e581bf ZJS |
1213 | int config_parse_exec_cpu_affinity(const char *unit, |
1214 | const char *filename, | |
1215 | unsigned line, | |
1216 | const char *section, | |
71a61510 | 1217 | unsigned section_line, |
e8e581bf ZJS |
1218 | const char *lvalue, |
1219 | int ltype, | |
1220 | const char *rvalue, | |
1221 | void *data, | |
1222 | void *userdata) { | |
94f04347 LP |
1223 | |
1224 | ExecContext *c = data; | |
9d5ca7f8 FB |
1225 | _cleanup_cpu_free_ cpu_set_t *cpuset = NULL; |
1226 | int ncpus; | |
94f04347 LP |
1227 | |
1228 | assert(filename); | |
1229 | assert(lvalue); | |
1230 | assert(rvalue); | |
1231 | assert(data); | |
1232 | ||
765d143b | 1233 | ncpus = parse_cpu_set_and_warn(rvalue, &cpuset, unit, filename, line, lvalue); |
9d5ca7f8 FB |
1234 | if (ncpus < 0) |
1235 | return ncpus; | |
487393e9 | 1236 | |
501941aa | 1237 | if (ncpus == 0) { |
9d5ca7f8 | 1238 | /* An empty assignment resets the CPU list */ |
501941aa YW |
1239 | c->cpuset = cpu_set_mfree(c->cpuset); |
1240 | c->cpuset_ncpus = 0; | |
1241 | return 0; | |
1242 | } | |
1243 | ||
1244 | if (!c->cpuset) { | |
ae2a15bc | 1245 | c->cpuset = TAKE_PTR(cpuset); |
501941aa YW |
1246 | c->cpuset_ncpus = (unsigned) ncpus; |
1247 | return 0; | |
1248 | } | |
1249 | ||
1250 | if (c->cpuset_ncpus < (unsigned) ncpus) { | |
1251 | CPU_OR_S(CPU_ALLOC_SIZE(c->cpuset_ncpus), cpuset, c->cpuset, cpuset); | |
1252 | CPU_FREE(c->cpuset); | |
ae2a15bc | 1253 | c->cpuset = TAKE_PTR(cpuset); |
501941aa YW |
1254 | c->cpuset_ncpus = (unsigned) ncpus; |
1255 | return 0; | |
9eba9da4 | 1256 | } |
501941aa YW |
1257 | |
1258 | CPU_OR_S(CPU_ALLOC_SIZE((unsigned) ncpus), c->cpuset, c->cpuset, cpuset); | |
9eba9da4 | 1259 | |
94f04347 LP |
1260 | return 0; |
1261 | } | |
1262 | ||
a103496c | 1263 | int config_parse_capability_set( |
65dce264 LP |
1264 | const char *unit, |
1265 | const char *filename, | |
1266 | unsigned line, | |
1267 | const char *section, | |
1268 | unsigned section_line, | |
1269 | const char *lvalue, | |
1270 | int ltype, | |
1271 | const char *rvalue, | |
1272 | void *data, | |
1273 | void *userdata) { | |
94f04347 | 1274 | |
a103496c IP |
1275 | uint64_t *capability_set = data; |
1276 | uint64_t sum = 0, initial = 0; | |
260abb78 | 1277 | bool invert = false; |
dd1f5bd0 | 1278 | int r; |
94f04347 LP |
1279 | |
1280 | assert(filename); | |
1281 | assert(lvalue); | |
1282 | assert(rvalue); | |
1283 | assert(data); | |
1284 | ||
260abb78 LP |
1285 | if (rvalue[0] == '~') { |
1286 | invert = true; | |
1287 | rvalue++; | |
1288 | } | |
1289 | ||
70d54d90 | 1290 | if (streq(lvalue, "CapabilityBoundingSet")) |
a103496c | 1291 | initial = CAP_ALL; /* initialized to all bits on */ |
755d4b67 | 1292 | /* else "AmbientCapabilities" initialized to all bits off */ |
260abb78 | 1293 | |
dd1f5bd0 | 1294 | r = capability_set_from_string(rvalue, &sum); |
dd1f5bd0 | 1295 | if (r < 0) { |
063c4b1a | 1296 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue, rvalue); |
dd1f5bd0 | 1297 | return 0; |
94f04347 | 1298 | } |
9eba9da4 | 1299 | |
a103496c | 1300 | if (sum == 0 || *capability_set == initial) |
c792ec2e IP |
1301 | /* "", "~" or uninitialized data -> replace */ |
1302 | *capability_set = invert ? ~sum : sum; | |
1303 | else { | |
a103496c | 1304 | /* previous data -> merge */ |
c792ec2e IP |
1305 | if (invert) |
1306 | *capability_set &= ~sum; | |
1307 | else | |
1308 | *capability_set |= sum; | |
1309 | } | |
260abb78 | 1310 | |
9eba9da4 LP |
1311 | return 0; |
1312 | } | |
1313 | ||
5f8640fb LP |
1314 | int config_parse_exec_selinux_context( |
1315 | const char *unit, | |
1316 | const char *filename, | |
1317 | unsigned line, | |
1318 | const char *section, | |
1319 | unsigned section_line, | |
1320 | const char *lvalue, | |
1321 | int ltype, | |
1322 | const char *rvalue, | |
1323 | void *data, | |
1324 | void *userdata) { | |
1325 | ||
1326 | ExecContext *c = data; | |
1327 | Unit *u = userdata; | |
1328 | bool ignore; | |
1329 | char *k; | |
1330 | int r; | |
1331 | ||
1332 | assert(filename); | |
1333 | assert(lvalue); | |
1334 | assert(rvalue); | |
1335 | assert(data); | |
1336 | ||
1337 | if (isempty(rvalue)) { | |
a1e58e8e | 1338 | c->selinux_context = mfree(c->selinux_context); |
5f8640fb LP |
1339 | c->selinux_context_ignore = false; |
1340 | return 0; | |
1341 | } | |
1342 | ||
1343 | if (rvalue[0] == '-') { | |
1344 | ignore = true; | |
1345 | rvalue++; | |
1346 | } else | |
1347 | ignore = false; | |
1348 | ||
18913df9 | 1349 | r = unit_full_printf(u, rvalue, &k); |
5f8640fb | 1350 | if (r < 0) { |
bb28e684 | 1351 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a YW |
1352 | "Failed to resolve unit specifiers in '%s'%s: %m", |
1353 | rvalue, ignore ? ", ignoring" : ""); | |
bb28e684 | 1354 | return ignore ? 0 : -ENOEXEC; |
5f8640fb LP |
1355 | } |
1356 | ||
063c4b1a | 1357 | free_and_replace(c->selinux_context, k); |
5f8640fb LP |
1358 | c->selinux_context_ignore = ignore; |
1359 | ||
1360 | return 0; | |
1361 | } | |
1362 | ||
eef65bf3 MS |
1363 | int config_parse_exec_apparmor_profile( |
1364 | const char *unit, | |
1365 | const char *filename, | |
1366 | unsigned line, | |
1367 | const char *section, | |
1368 | unsigned section_line, | |
1369 | const char *lvalue, | |
1370 | int ltype, | |
1371 | const char *rvalue, | |
1372 | void *data, | |
1373 | void *userdata) { | |
1374 | ||
1375 | ExecContext *c = data; | |
1376 | Unit *u = userdata; | |
1377 | bool ignore; | |
1378 | char *k; | |
1379 | int r; | |
1380 | ||
1381 | assert(filename); | |
1382 | assert(lvalue); | |
1383 | assert(rvalue); | |
1384 | assert(data); | |
1385 | ||
1386 | if (isempty(rvalue)) { | |
a1e58e8e | 1387 | c->apparmor_profile = mfree(c->apparmor_profile); |
eef65bf3 MS |
1388 | c->apparmor_profile_ignore = false; |
1389 | return 0; | |
1390 | } | |
1391 | ||
1392 | if (rvalue[0] == '-') { | |
1393 | ignore = true; | |
1394 | rvalue++; | |
1395 | } else | |
1396 | ignore = false; | |
1397 | ||
18913df9 | 1398 | r = unit_full_printf(u, rvalue, &k); |
eef65bf3 | 1399 | if (r < 0) { |
bb28e684 | 1400 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a YW |
1401 | "Failed to resolve unit specifiers in '%s'%s: %m", |
1402 | rvalue, ignore ? ", ignoring" : ""); | |
bb28e684 | 1403 | return ignore ? 0 : -ENOEXEC; |
eef65bf3 MS |
1404 | } |
1405 | ||
063c4b1a | 1406 | free_and_replace(c->apparmor_profile, k); |
eef65bf3 MS |
1407 | c->apparmor_profile_ignore = ignore; |
1408 | ||
1409 | return 0; | |
1410 | } | |
1411 | ||
2ca620c4 WC |
1412 | int config_parse_exec_smack_process_label( |
1413 | const char *unit, | |
1414 | const char *filename, | |
1415 | unsigned line, | |
1416 | const char *section, | |
1417 | unsigned section_line, | |
1418 | const char *lvalue, | |
1419 | int ltype, | |
1420 | const char *rvalue, | |
1421 | void *data, | |
1422 | void *userdata) { | |
1423 | ||
1424 | ExecContext *c = data; | |
1425 | Unit *u = userdata; | |
1426 | bool ignore; | |
1427 | char *k; | |
1428 | int r; | |
1429 | ||
1430 | assert(filename); | |
1431 | assert(lvalue); | |
1432 | assert(rvalue); | |
1433 | assert(data); | |
1434 | ||
1435 | if (isempty(rvalue)) { | |
a1e58e8e | 1436 | c->smack_process_label = mfree(c->smack_process_label); |
2ca620c4 WC |
1437 | c->smack_process_label_ignore = false; |
1438 | return 0; | |
1439 | } | |
1440 | ||
1441 | if (rvalue[0] == '-') { | |
1442 | ignore = true; | |
1443 | rvalue++; | |
1444 | } else | |
1445 | ignore = false; | |
1446 | ||
18913df9 | 1447 | r = unit_full_printf(u, rvalue, &k); |
2ca620c4 | 1448 | if (r < 0) { |
bb28e684 | 1449 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a YW |
1450 | "Failed to resolve unit specifiers in '%s'%s: %m", |
1451 | rvalue, ignore ? ", ignoring" : ""); | |
bb28e684 | 1452 | return ignore ? 0 : -ENOEXEC; |
2ca620c4 WC |
1453 | } |
1454 | ||
063c4b1a | 1455 | free_and_replace(c->smack_process_label, k); |
2ca620c4 WC |
1456 | c->smack_process_label_ignore = ignore; |
1457 | ||
1458 | return 0; | |
1459 | } | |
1460 | ||
e8e581bf ZJS |
1461 | int config_parse_timer(const char *unit, |
1462 | const char *filename, | |
1463 | unsigned line, | |
1464 | const char *section, | |
71a61510 | 1465 | unsigned section_line, |
e8e581bf ZJS |
1466 | const char *lvalue, |
1467 | int ltype, | |
1468 | const char *rvalue, | |
1469 | void *data, | |
1470 | void *userdata) { | |
871d7de4 LP |
1471 | |
1472 | Timer *t = data; | |
2507992f | 1473 | usec_t usec = 0; |
871d7de4 LP |
1474 | TimerValue *v; |
1475 | TimerBase b; | |
921b5987 | 1476 | _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; |
2507992f DC |
1477 | Unit *u = userdata; |
1478 | _cleanup_free_ char *k = NULL; | |
1479 | int r; | |
871d7de4 LP |
1480 | |
1481 | assert(filename); | |
1482 | assert(lvalue); | |
1483 | assert(rvalue); | |
1484 | assert(data); | |
1485 | ||
74051b9b LP |
1486 | if (isempty(rvalue)) { |
1487 | /* Empty assignment resets list */ | |
1488 | timer_free_values(t); | |
1489 | return 0; | |
1490 | } | |
1491 | ||
36697dc0 LP |
1492 | b = timer_base_from_string(lvalue); |
1493 | if (b < 0) { | |
12ca818f | 1494 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse timer base, ignoring: %s", lvalue); |
c0b34696 | 1495 | return 0; |
871d7de4 LP |
1496 | } |
1497 | ||
2507992f DC |
1498 | r = unit_full_printf(u, rvalue, &k); |
1499 | if (r < 0) { | |
063c4b1a | 1500 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
2507992f DC |
1501 | return 0; |
1502 | } | |
1503 | ||
36697dc0 | 1504 | if (b == TIMER_CALENDAR) { |
2507992f DC |
1505 | if (calendar_spec_from_string(k, &c) < 0) { |
1506 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse calendar specification, ignoring: %s", k); | |
36697dc0 LP |
1507 | return 0; |
1508 | } | |
063c4b1a | 1509 | } else |
2507992f DC |
1510 | if (parse_sec(k, &usec) < 0) { |
1511 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse timer value, ignoring: %s", k); | |
36697dc0 LP |
1512 | return 0; |
1513 | } | |
871d7de4 | 1514 | |
36697dc0 | 1515 | v = new0(TimerValue, 1); |
921b5987 | 1516 | if (!v) |
74051b9b | 1517 | return log_oom(); |
871d7de4 LP |
1518 | |
1519 | v->base = b; | |
2507992f | 1520 | v->value = usec; |
921b5987 | 1521 | v->calendar_spec = TAKE_PTR(c); |
871d7de4 | 1522 | |
71fda00f | 1523 | LIST_PREPEND(value, t->values, v); |
871d7de4 LP |
1524 | |
1525 | return 0; | |
1526 | } | |
1527 | ||
3ecaa09b LP |
1528 | int config_parse_trigger_unit( |
1529 | const char *unit, | |
1530 | const char *filename, | |
1531 | unsigned line, | |
1532 | const char *section, | |
71a61510 | 1533 | unsigned section_line, |
3ecaa09b LP |
1534 | const char *lvalue, |
1535 | int ltype, | |
1536 | const char *rvalue, | |
1537 | void *data, | |
1538 | void *userdata) { | |
871d7de4 | 1539 | |
74051b9b | 1540 | _cleanup_free_ char *p = NULL; |
3ecaa09b LP |
1541 | Unit *u = data; |
1542 | UnitType type; | |
1543 | int r; | |
398ef8ba LP |
1544 | |
1545 | assert(filename); | |
1546 | assert(lvalue); | |
1547 | assert(rvalue); | |
1548 | assert(data); | |
1549 | ||
eef85c4a | 1550 | if (!hashmap_isempty(u->dependencies[UNIT_TRIGGERS])) { |
12ca818f | 1551 | log_syntax(unit, LOG_ERR, filename, line, 0, "Multiple units to trigger specified, ignoring: %s", rvalue); |
3ecaa09b LP |
1552 | return 0; |
1553 | } | |
871d7de4 | 1554 | |
19f6d710 | 1555 | r = unit_name_printf(u, rvalue, &p); |
12ca818f | 1556 | if (r < 0) { |
063c4b1a | 1557 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue); |
12ca818f LP |
1558 | return 0; |
1559 | } | |
74051b9b | 1560 | |
12ca818f | 1561 | type = unit_name_to_type(p); |
3ecaa09b | 1562 | if (type < 0) { |
12ca818f | 1563 | log_syntax(unit, LOG_ERR, filename, line, 0, "Unit type not valid, ignoring: %s", rvalue); |
c0b34696 | 1564 | return 0; |
871d7de4 | 1565 | } |
49219a1c JR |
1566 | if (unit_has_name(u, p)) { |
1567 | log_syntax(unit, LOG_ERR, filename, line, 0, "Units cannot trigger themselves, ignoring: %s", rvalue); | |
3ecaa09b LP |
1568 | return 0; |
1569 | } | |
1570 | ||
eef85c4a | 1571 | r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_TRIGGERS, p, NULL, true, UNIT_DEPENDENCY_FILE); |
57020a3a | 1572 | if (r < 0) { |
12ca818f | 1573 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add trigger on %s, ignoring: %m", p); |
c0b34696 | 1574 | return 0; |
871d7de4 LP |
1575 | } |
1576 | ||
1577 | return 0; | |
1578 | } | |
1579 | ||
e8e581bf ZJS |
1580 | int config_parse_path_spec(const char *unit, |
1581 | const char *filename, | |
1582 | unsigned line, | |
1583 | const char *section, | |
71a61510 | 1584 | unsigned section_line, |
e8e581bf ZJS |
1585 | const char *lvalue, |
1586 | int ltype, | |
1587 | const char *rvalue, | |
1588 | void *data, | |
1589 | void *userdata) { | |
01f78473 LP |
1590 | |
1591 | Path *p = data; | |
1592 | PathSpec *s; | |
1593 | PathType b; | |
7fd1b19b | 1594 | _cleanup_free_ char *k = NULL; |
19f6d710 | 1595 | int r; |
01f78473 LP |
1596 | |
1597 | assert(filename); | |
1598 | assert(lvalue); | |
1599 | assert(rvalue); | |
1600 | assert(data); | |
1601 | ||
74051b9b LP |
1602 | if (isempty(rvalue)) { |
1603 | /* Empty assignment clears list */ | |
1604 | path_free_specs(p); | |
1605 | return 0; | |
1606 | } | |
1607 | ||
93e4c84b LP |
1608 | b = path_type_from_string(lvalue); |
1609 | if (b < 0) { | |
12ca818f | 1610 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse path type, ignoring: %s", lvalue); |
c0b34696 | 1611 | return 0; |
01f78473 LP |
1612 | } |
1613 | ||
19f6d710 LP |
1614 | r = unit_full_printf(UNIT(p), rvalue, &k); |
1615 | if (r < 0) { | |
063c4b1a | 1616 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue); |
12ca818f | 1617 | return 0; |
487060c2 | 1618 | } |
93e4c84b | 1619 | |
2f4d31c1 YW |
1620 | r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
1621 | if (r < 0) | |
c0b34696 | 1622 | return 0; |
01f78473 | 1623 | |
93e4c84b | 1624 | s = new0(PathSpec, 1); |
543295ad | 1625 | if (!s) |
93e4c84b | 1626 | return log_oom(); |
01f78473 | 1627 | |
718db961 | 1628 | s->unit = UNIT(p); |
063c4b1a | 1629 | s->path = TAKE_PTR(k); |
01f78473 LP |
1630 | s->type = b; |
1631 | s->inotify_fd = -1; | |
1632 | ||
71fda00f | 1633 | LIST_PREPEND(spec, p->specs, s); |
01f78473 LP |
1634 | |
1635 | return 0; | |
1636 | } | |
1637 | ||
b02cb41c LP |
1638 | int config_parse_socket_service( |
1639 | const char *unit, | |
1640 | const char *filename, | |
1641 | unsigned line, | |
1642 | const char *section, | |
1643 | unsigned section_line, | |
1644 | const char *lvalue, | |
1645 | int ltype, | |
1646 | const char *rvalue, | |
1647 | void *data, | |
1648 | void *userdata) { | |
d9ff321a | 1649 | |
4afd3348 | 1650 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
8dd4c05b | 1651 | _cleanup_free_ char *p = NULL; |
d9ff321a | 1652 | Socket *s = data; |
4ff77f66 | 1653 | Unit *x; |
8dd4c05b | 1654 | int r; |
d9ff321a LP |
1655 | |
1656 | assert(filename); | |
1657 | assert(lvalue); | |
1658 | assert(rvalue); | |
1659 | assert(data); | |
1660 | ||
19f6d710 | 1661 | r = unit_name_printf(UNIT(s), rvalue, &p); |
613b411c | 1662 | if (r < 0) { |
063c4b1a | 1663 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue); |
bb28e684 | 1664 | return -ENOEXEC; |
613b411c | 1665 | } |
74051b9b | 1666 | |
613b411c | 1667 | if (!endswith(p, ".service")) { |
bb28e684 ZJS |
1668 | log_syntax(unit, LOG_ERR, filename, line, 0, "Unit must be of type service: %s", rvalue); |
1669 | return -ENOEXEC; | |
d9ff321a LP |
1670 | } |
1671 | ||
613b411c | 1672 | r = manager_load_unit(UNIT(s)->manager, p, NULL, &error, &x); |
4ff77f66 | 1673 | if (r < 0) { |
bb28e684 ZJS |
1674 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to load unit %s: %s", rvalue, bus_error_message(&error, r)); |
1675 | return -ENOEXEC; | |
d9ff321a LP |
1676 | } |
1677 | ||
7f7d01ed | 1678 | unit_ref_set(&s->service, UNIT(s), x); |
4ff77f66 | 1679 | |
d9ff321a LP |
1680 | return 0; |
1681 | } | |
1682 | ||
8dd4c05b LP |
1683 | int config_parse_fdname( |
1684 | const char *unit, | |
1685 | const char *filename, | |
1686 | unsigned line, | |
1687 | const char *section, | |
1688 | unsigned section_line, | |
1689 | const char *lvalue, | |
1690 | int ltype, | |
1691 | const char *rvalue, | |
1692 | void *data, | |
1693 | void *userdata) { | |
1694 | ||
1695 | _cleanup_free_ char *p = NULL; | |
1696 | Socket *s = data; | |
1697 | int r; | |
1698 | ||
1699 | assert(filename); | |
1700 | assert(lvalue); | |
1701 | assert(rvalue); | |
1702 | assert(data); | |
1703 | ||
1704 | if (isempty(rvalue)) { | |
1705 | s->fdname = mfree(s->fdname); | |
1706 | return 0; | |
1707 | } | |
1708 | ||
18913df9 | 1709 | r = unit_full_printf(UNIT(s), rvalue, &p); |
8dd4c05b | 1710 | if (r < 0) { |
063c4b1a | 1711 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
8dd4c05b LP |
1712 | return 0; |
1713 | } | |
1714 | ||
1715 | if (!fdname_is_valid(p)) { | |
1716 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name, ignoring: %s", p); | |
1717 | return 0; | |
1718 | } | |
1719 | ||
3b319885 | 1720 | return free_and_replace(s->fdname, p); |
8dd4c05b LP |
1721 | } |
1722 | ||
b02cb41c LP |
1723 | int config_parse_service_sockets( |
1724 | const char *unit, | |
1725 | const char *filename, | |
1726 | unsigned line, | |
1727 | const char *section, | |
1728 | unsigned section_line, | |
1729 | const char *lvalue, | |
1730 | int ltype, | |
1731 | const char *rvalue, | |
1732 | void *data, | |
1733 | void *userdata) { | |
f976f3f6 LP |
1734 | |
1735 | Service *s = data; | |
7b2313f5 | 1736 | const char *p; |
b02cb41c | 1737 | int r; |
f976f3f6 LP |
1738 | |
1739 | assert(filename); | |
1740 | assert(lvalue); | |
1741 | assert(rvalue); | |
1742 | assert(data); | |
1743 | ||
7b2313f5 | 1744 | p = rvalue; |
9ed794a3 | 1745 | for (;;) { |
6a0f3175 | 1746 | _cleanup_free_ char *word = NULL, *k = NULL; |
f976f3f6 | 1747 | |
7b2313f5 SS |
1748 | r = extract_first_word(&p, &word, NULL, 0); |
1749 | if (r == 0) | |
1750 | break; | |
1751 | if (r == -ENOMEM) | |
74051b9b | 1752 | return log_oom(); |
7b2313f5 SS |
1753 | if (r < 0) { |
1754 | log_syntax(unit, LOG_ERR, filename, line, r, "Trailing garbage in sockets, ignoring: %s", rvalue); | |
1755 | break; | |
1756 | } | |
f976f3f6 | 1757 | |
7b2313f5 | 1758 | r = unit_name_printf(UNIT(s), word, &k); |
b02cb41c | 1759 | if (r < 0) { |
063c4b1a | 1760 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word); |
b02cb41c LP |
1761 | continue; |
1762 | } | |
57020a3a | 1763 | |
b02cb41c | 1764 | if (!endswith(k, ".socket")) { |
12ca818f | 1765 | log_syntax(unit, LOG_ERR, filename, line, 0, "Unit must be of type socket, ignoring: %s", k); |
f976f3f6 LP |
1766 | continue; |
1767 | } | |
1768 | ||
eef85c4a | 1769 | r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_WANTS, UNIT_AFTER, k, NULL, true, UNIT_DEPENDENCY_FILE); |
57020a3a | 1770 | if (r < 0) |
b02cb41c | 1771 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k); |
f976f3f6 | 1772 | |
eef85c4a | 1773 | r = unit_add_dependency_by_name(UNIT(s), UNIT_TRIGGERED_BY, k, NULL, true, UNIT_DEPENDENCY_FILE); |
57020a3a | 1774 | if (r < 0) |
b02cb41c | 1775 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k); |
f976f3f6 LP |
1776 | } |
1777 | ||
1778 | return 0; | |
1779 | } | |
1780 | ||
b02cb41c LP |
1781 | int config_parse_bus_name( |
1782 | const char *unit, | |
1783 | const char *filename, | |
1784 | unsigned line, | |
1785 | const char *section, | |
1786 | unsigned section_line, | |
1787 | const char *lvalue, | |
1788 | int ltype, | |
1789 | const char *rvalue, | |
1790 | void *data, | |
1791 | void *userdata) { | |
1792 | ||
1793 | _cleanup_free_ char *k = NULL; | |
1794 | Unit *u = userdata; | |
1795 | int r; | |
1796 | ||
1797 | assert(filename); | |
1798 | assert(lvalue); | |
1799 | assert(rvalue); | |
1800 | assert(u); | |
1801 | ||
1802 | r = unit_full_printf(u, rvalue, &k); | |
1803 | if (r < 0) { | |
063c4b1a | 1804 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue); |
b02cb41c LP |
1805 | return 0; |
1806 | } | |
1807 | ||
1808 | if (!service_name_is_valid(k)) { | |
063c4b1a | 1809 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid bus name, ignoring: %s", k); |
b02cb41c LP |
1810 | return 0; |
1811 | } | |
1812 | ||
1813 | return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata); | |
1814 | } | |
1815 | ||
aad41f08 LP |
1816 | int config_parse_service_timeout( |
1817 | const char *unit, | |
1818 | const char *filename, | |
1819 | unsigned line, | |
1820 | const char *section, | |
1821 | unsigned section_line, | |
1822 | const char *lvalue, | |
1823 | int ltype, | |
1824 | const char *rvalue, | |
1825 | void *data, | |
1826 | void *userdata) { | |
98709151 LN |
1827 | |
1828 | Service *s = userdata; | |
aad41f08 | 1829 | usec_t usec; |
98709151 LN |
1830 | int r; |
1831 | ||
1832 | assert(filename); | |
1833 | assert(lvalue); | |
1834 | assert(rvalue); | |
1835 | assert(s); | |
1836 | ||
6c58305a | 1837 | /* This is called for two cases: TimeoutSec= and TimeoutStartSec=. */ |
98709151 | 1838 | |
fb27be3f YW |
1839 | /* Traditionally, these options accepted 0 to disable the timeouts. However, a timeout of 0 suggests it happens |
1840 | * immediately, hence fix this to become USEC_INFINITY instead. This is in-line with how we internally handle | |
1841 | * all other timeouts. */ | |
1842 | r = parse_sec_fix_0(rvalue, &usec); | |
aad41f08 LP |
1843 | if (r < 0) { |
1844 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue); | |
1845 | return 0; | |
1846 | } | |
d568a335 | 1847 | |
6c58305a YW |
1848 | s->start_timeout_defined = true; |
1849 | s->timeout_start_usec = usec; | |
36c16a7c | 1850 | |
6c58305a | 1851 | if (streq(lvalue, "TimeoutSec")) |
aad41f08 | 1852 | s->timeout_stop_usec = usec; |
36c16a7c | 1853 | |
d568a335 | 1854 | return 0; |
98709151 LN |
1855 | } |
1856 | ||
89beff89 LP |
1857 | int config_parse_sec_fix_0( |
1858 | const char *unit, | |
1859 | const char *filename, | |
1860 | unsigned line, | |
1861 | const char *section, | |
1862 | unsigned section_line, | |
1863 | const char *lvalue, | |
1864 | int ltype, | |
1865 | const char *rvalue, | |
1866 | void *data, | |
1867 | void *userdata) { | |
1868 | ||
1869 | usec_t *usec = data; | |
1870 | int r; | |
1871 | ||
1872 | assert(filename); | |
1873 | assert(lvalue); | |
1874 | assert(rvalue); | |
1875 | assert(usec); | |
1876 | ||
1877 | /* This is pretty much like config_parse_sec(), except that this treats a time of 0 as infinity, for | |
1878 | * compatibility with older versions of systemd where 0 instead of infinity was used as indicator to turn off a | |
1879 | * timeout. */ | |
1880 | ||
0004f698 | 1881 | r = parse_sec_fix_0(rvalue, usec); |
89beff89 LP |
1882 | if (r < 0) { |
1883 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue); | |
1884 | return 0; | |
1885 | } | |
1886 | ||
89beff89 LP |
1887 | return 0; |
1888 | } | |
1889 | ||
66dccd8d LP |
1890 | int config_parse_user_group( |
1891 | const char *unit, | |
1892 | const char *filename, | |
1893 | unsigned line, | |
1894 | const char *section, | |
1895 | unsigned section_line, | |
1896 | const char *lvalue, | |
1897 | int ltype, | |
1898 | const char *rvalue, | |
1899 | void *data, | |
1900 | void *userdata) { | |
1901 | ||
063c4b1a YW |
1902 | _cleanup_free_ char *k = NULL; |
1903 | char **user = data; | |
66dccd8d LP |
1904 | Unit *u = userdata; |
1905 | int r; | |
1906 | ||
1907 | assert(filename); | |
1908 | assert(lvalue); | |
1909 | assert(rvalue); | |
1910 | assert(u); | |
1911 | ||
063c4b1a YW |
1912 | if (isempty(rvalue)) { |
1913 | *user = mfree(*user); | |
1914 | return 0; | |
1915 | } | |
66dccd8d | 1916 | |
063c4b1a YW |
1917 | r = unit_full_printf(u, rvalue, &k); |
1918 | if (r < 0) { | |
1919 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue); | |
1920 | return -ENOEXEC; | |
66dccd8d LP |
1921 | } |
1922 | ||
063c4b1a YW |
1923 | if (!valid_user_group_name_or_id(k)) { |
1924 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k); | |
1925 | return -ENOEXEC; | |
1926 | } | |
66dccd8d | 1927 | |
063c4b1a | 1928 | return free_and_replace(*user, k); |
66dccd8d LP |
1929 | } |
1930 | ||
1931 | int config_parse_user_group_strv( | |
1932 | const char *unit, | |
1933 | const char *filename, | |
1934 | unsigned line, | |
1935 | const char *section, | |
1936 | unsigned section_line, | |
1937 | const char *lvalue, | |
1938 | int ltype, | |
1939 | const char *rvalue, | |
1940 | void *data, | |
1941 | void *userdata) { | |
1942 | ||
1943 | char ***users = data; | |
1944 | Unit *u = userdata; | |
063c4b1a | 1945 | const char *p = rvalue; |
66dccd8d LP |
1946 | int r; |
1947 | ||
1948 | assert(filename); | |
1949 | assert(lvalue); | |
1950 | assert(rvalue); | |
1951 | assert(u); | |
1952 | ||
1953 | if (isempty(rvalue)) { | |
9f2d41a6 | 1954 | *users = strv_free(*users); |
66dccd8d LP |
1955 | return 0; |
1956 | } | |
1957 | ||
66dccd8d LP |
1958 | for (;;) { |
1959 | _cleanup_free_ char *word = NULL, *k = NULL; | |
1960 | ||
9a82ab95 | 1961 | r = extract_first_word(&p, &word, NULL, 0); |
66dccd8d LP |
1962 | if (r == 0) |
1963 | break; | |
1964 | if (r == -ENOMEM) | |
1965 | return log_oom(); | |
1966 | if (r < 0) { | |
bb28e684 ZJS |
1967 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax: %s", rvalue); |
1968 | return -ENOEXEC; | |
66dccd8d LP |
1969 | } |
1970 | ||
1971 | r = unit_full_printf(u, word, &k); | |
1972 | if (r < 0) { | |
bb28e684 ZJS |
1973 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", word); |
1974 | return -ENOEXEC; | |
66dccd8d LP |
1975 | } |
1976 | ||
1977 | if (!valid_user_group_name_or_id(k)) { | |
bb28e684 ZJS |
1978 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k); |
1979 | return -ENOEXEC; | |
66dccd8d LP |
1980 | } |
1981 | ||
1982 | r = strv_push(users, k); | |
1983 | if (r < 0) | |
1984 | return log_oom(); | |
1985 | ||
1986 | k = NULL; | |
1987 | } | |
1988 | ||
1989 | return 0; | |
1990 | } | |
1991 | ||
5f5d8eab LP |
1992 | int config_parse_working_directory( |
1993 | const char *unit, | |
1994 | const char *filename, | |
1995 | unsigned line, | |
1996 | const char *section, | |
1997 | unsigned section_line, | |
1998 | const char *lvalue, | |
1999 | int ltype, | |
2000 | const char *rvalue, | |
2001 | void *data, | |
2002 | void *userdata) { | |
2003 | ||
2004 | ExecContext *c = data; | |
2005 | Unit *u = userdata; | |
2006 | bool missing_ok; | |
2007 | int r; | |
2008 | ||
2009 | assert(filename); | |
2010 | assert(lvalue); | |
2011 | assert(rvalue); | |
2012 | assert(c); | |
2013 | assert(u); | |
2014 | ||
862fcffd YW |
2015 | if (isempty(rvalue)) { |
2016 | c->working_directory_home = false; | |
2017 | c->working_directory = mfree(c->working_directory); | |
2018 | return 0; | |
2019 | } | |
2020 | ||
5f5d8eab LP |
2021 | if (rvalue[0] == '-') { |
2022 | missing_ok = true; | |
2023 | rvalue++; | |
2024 | } else | |
2025 | missing_ok = false; | |
2026 | ||
2027 | if (streq(rvalue, "~")) { | |
2028 | c->working_directory_home = true; | |
2029 | c->working_directory = mfree(c->working_directory); | |
2030 | } else { | |
2031 | _cleanup_free_ char *k = NULL; | |
2032 | ||
2033 | r = unit_full_printf(u, rvalue, &k); | |
2034 | if (r < 0) { | |
bb28e684 ZJS |
2035 | log_syntax(unit, LOG_ERR, filename, line, r, |
2036 | "Failed to resolve unit specifiers in working directory path '%s'%s: %m", | |
2037 | rvalue, missing_ok ? ", ignoring" : ""); | |
2038 | return missing_ok ? 0 : -ENOEXEC; | |
5f5d8eab LP |
2039 | } |
2040 | ||
2f4d31c1 YW |
2041 | r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE | (missing_ok ? 0 : PATH_CHECK_FATAL), unit, filename, line, lvalue); |
2042 | if (r < 0) | |
bb28e684 | 2043 | return missing_ok ? 0 : -ENOEXEC; |
5f5d8eab | 2044 | |
5f5d8eab | 2045 | c->working_directory_home = false; |
bb28e684 | 2046 | free_and_replace(c->working_directory, k); |
5f5d8eab LP |
2047 | } |
2048 | ||
2049 | c->working_directory_missing_ok = missing_ok; | |
2050 | return 0; | |
2051 | } | |
2052 | ||
e8e581bf ZJS |
2053 | int config_parse_unit_env_file(const char *unit, |
2054 | const char *filename, | |
2055 | unsigned line, | |
2056 | const char *section, | |
71a61510 | 2057 | unsigned section_line, |
e8e581bf ZJS |
2058 | const char *lvalue, |
2059 | int ltype, | |
2060 | const char *rvalue, | |
2061 | void *data, | |
2062 | void *userdata) { | |
ddb26e18 | 2063 | |
853b8397 | 2064 | char ***env = data; |
8fef7659 | 2065 | Unit *u = userdata; |
19f6d710 | 2066 | _cleanup_free_ char *n = NULL; |
853b8397 | 2067 | int r; |
ddb26e18 LP |
2068 | |
2069 | assert(filename); | |
2070 | assert(lvalue); | |
2071 | assert(rvalue); | |
2072 | assert(data); | |
2073 | ||
74051b9b LP |
2074 | if (isempty(rvalue)) { |
2075 | /* Empty assignment frees the list */ | |
6796073e | 2076 | *env = strv_free(*env); |
74051b9b LP |
2077 | return 0; |
2078 | } | |
2079 | ||
19f6d710 | 2080 | r = unit_full_printf(u, rvalue, &n); |
12ca818f | 2081 | if (r < 0) { |
063c4b1a | 2082 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
12ca818f LP |
2083 | return 0; |
2084 | } | |
8fef7659 | 2085 | |
2f4d31c1 YW |
2086 | r = path_simplify_and_warn(n[0] == '-' ? n + 1 : n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
2087 | if (r < 0) | |
afe4bfe2 | 2088 | return 0; |
afe4bfe2 | 2089 | |
2f4d31c1 | 2090 | r = strv_push(env, n); |
853b8397 LP |
2091 | if (r < 0) |
2092 | return log_oom(); | |
2093 | ||
2f4d31c1 YW |
2094 | n = NULL; |
2095 | ||
853b8397 LP |
2096 | return 0; |
2097 | } | |
2098 | ||
f7f3f5c3 LP |
2099 | int config_parse_environ( |
2100 | const char *unit, | |
2101 | const char *filename, | |
2102 | unsigned line, | |
2103 | const char *section, | |
2104 | unsigned section_line, | |
2105 | const char *lvalue, | |
2106 | int ltype, | |
2107 | const char *rvalue, | |
2108 | void *data, | |
2109 | void *userdata) { | |
853b8397 LP |
2110 | |
2111 | Unit *u = userdata; | |
035fe294 ZJS |
2112 | char ***env = data; |
2113 | const char *p; | |
19f6d710 | 2114 | int r; |
853b8397 LP |
2115 | |
2116 | assert(filename); | |
2117 | assert(lvalue); | |
2118 | assert(rvalue); | |
97d0e5f8 | 2119 | assert(data); |
853b8397 LP |
2120 | |
2121 | if (isempty(rvalue)) { | |
2122 | /* Empty assignment resets the list */ | |
6796073e | 2123 | *env = strv_free(*env); |
853b8397 LP |
2124 | return 0; |
2125 | } | |
2126 | ||
035fe294 ZJS |
2127 | for (p = rvalue;; ) { |
2128 | _cleanup_free_ char *word = NULL, *k = NULL; | |
035fe294 ZJS |
2129 | |
2130 | r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_QUOTES); | |
2131 | if (r == 0) | |
2132 | return 0; | |
2133 | if (r == -ENOMEM) | |
2134 | return log_oom(); | |
12ca818f | 2135 | if (r < 0) { |
035fe294 ZJS |
2136 | log_syntax(unit, LOG_WARNING, filename, line, r, |
2137 | "Invalid syntax, ignoring: %s", rvalue); | |
12ca818f LP |
2138 | return 0; |
2139 | } | |
97d0e5f8 | 2140 | |
035fe294 ZJS |
2141 | if (u) { |
2142 | r = unit_full_printf(u, word, &k); | |
2143 | if (r < 0) { | |
2144 | log_syntax(unit, LOG_ERR, filename, line, r, | |
063c4b1a | 2145 | "Failed to resolve unit specifiers in %s, ignoring: %m", word); |
035fe294 ZJS |
2146 | continue; |
2147 | } | |
ae2a15bc LP |
2148 | } else |
2149 | k = TAKE_PTR(word); | |
853b8397 | 2150 | |
035fe294 ZJS |
2151 | if (!env_assignment_is_valid(k)) { |
2152 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
2153 | "Invalid environment assignment, ignoring: %s", k); | |
853b8397 LP |
2154 | continue; |
2155 | } | |
2156 | ||
54ac3494 ZJS |
2157 | r = strv_env_replace(env, k); |
2158 | if (r < 0) | |
853b8397 | 2159 | return log_oom(); |
f7f3f5c3 | 2160 | |
54ac3494 | 2161 | k = NULL; |
853b8397 | 2162 | } |
ddb26e18 LP |
2163 | } |
2164 | ||
00819cc1 LP |
2165 | int config_parse_pass_environ( |
2166 | const char *unit, | |
2167 | const char *filename, | |
2168 | unsigned line, | |
2169 | const char *section, | |
2170 | unsigned section_line, | |
2171 | const char *lvalue, | |
2172 | int ltype, | |
2173 | const char *rvalue, | |
2174 | void *data, | |
2175 | void *userdata) { | |
b4c14404 | 2176 | |
b4c14404 FB |
2177 | _cleanup_strv_free_ char **n = NULL; |
2178 | size_t nlen = 0, nbufsize = 0; | |
41de9cc2 | 2179 | char*** passenv = data; |
063c4b1a | 2180 | const char *p = rvalue; |
41de9cc2 | 2181 | Unit *u = userdata; |
b4c14404 FB |
2182 | int r; |
2183 | ||
2184 | assert(filename); | |
2185 | assert(lvalue); | |
2186 | assert(rvalue); | |
2187 | assert(data); | |
2188 | ||
2189 | if (isempty(rvalue)) { | |
2190 | /* Empty assignment resets the list */ | |
2191 | *passenv = strv_free(*passenv); | |
2192 | return 0; | |
2193 | } | |
2194 | ||
2195 | for (;;) { | |
41de9cc2 | 2196 | _cleanup_free_ char *word = NULL, *k = NULL; |
b4c14404 | 2197 | |
063c4b1a | 2198 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
b4c14404 FB |
2199 | if (r == 0) |
2200 | break; | |
2201 | if (r == -ENOMEM) | |
2202 | return log_oom(); | |
2203 | if (r < 0) { | |
2204 | log_syntax(unit, LOG_ERR, filename, line, r, | |
063c4b1a | 2205 | "Trailing garbage in %s, ignoring: %s", lvalue, rvalue); |
b4c14404 FB |
2206 | break; |
2207 | } | |
2208 | ||
41de9cc2 LP |
2209 | if (u) { |
2210 | r = unit_full_printf(u, word, &k); | |
2211 | if (r < 0) { | |
2212 | log_syntax(unit, LOG_ERR, filename, line, r, | |
063c4b1a | 2213 | "Failed to resolve specifiers in %s, ignoring: %m", word); |
41de9cc2 LP |
2214 | continue; |
2215 | } | |
ae2a15bc LP |
2216 | } else |
2217 | k = TAKE_PTR(word); | |
41de9cc2 LP |
2218 | |
2219 | if (!env_name_is_valid(k)) { | |
2220 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
2221 | "Invalid environment name for %s, ignoring: %s", lvalue, k); | |
b4c14404 FB |
2222 | continue; |
2223 | } | |
2224 | ||
2225 | if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) | |
2226 | return log_oom(); | |
41de9cc2 | 2227 | |
1cc6c93a | 2228 | n[nlen++] = TAKE_PTR(k); |
b4c14404 | 2229 | n[nlen] = NULL; |
b4c14404 FB |
2230 | } |
2231 | ||
2232 | if (n) { | |
2233 | r = strv_extend_strv(passenv, n, true); | |
2234 | if (r < 0) | |
2235 | return r; | |
2236 | } | |
2237 | ||
2238 | return 0; | |
2239 | } | |
2240 | ||
00819cc1 LP |
2241 | int config_parse_unset_environ( |
2242 | const char *unit, | |
2243 | const char *filename, | |
2244 | unsigned line, | |
2245 | const char *section, | |
2246 | unsigned section_line, | |
2247 | const char *lvalue, | |
2248 | int ltype, | |
2249 | const char *rvalue, | |
2250 | void *data, | |
2251 | void *userdata) { | |
2252 | ||
2253 | _cleanup_strv_free_ char **n = NULL; | |
00819cc1 LP |
2254 | size_t nlen = 0, nbufsize = 0; |
2255 | char*** unsetenv = data; | |
063c4b1a | 2256 | const char *p = rvalue; |
00819cc1 LP |
2257 | Unit *u = userdata; |
2258 | int r; | |
2259 | ||
2260 | assert(filename); | |
2261 | assert(lvalue); | |
2262 | assert(rvalue); | |
2263 | assert(data); | |
2264 | ||
2265 | if (isempty(rvalue)) { | |
2266 | /* Empty assignment resets the list */ | |
2267 | *unsetenv = strv_free(*unsetenv); | |
2268 | return 0; | |
2269 | } | |
2270 | ||
2271 | for (;;) { | |
2272 | _cleanup_free_ char *word = NULL, *k = NULL; | |
2273 | ||
063c4b1a | 2274 | r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_QUOTES); |
00819cc1 LP |
2275 | if (r == 0) |
2276 | break; | |
2277 | if (r == -ENOMEM) | |
2278 | return log_oom(); | |
2279 | if (r < 0) { | |
2280 | log_syntax(unit, LOG_ERR, filename, line, r, | |
063c4b1a | 2281 | "Trailing garbage in %s, ignoring: %s", lvalue, rvalue); |
00819cc1 LP |
2282 | break; |
2283 | } | |
2284 | ||
2285 | if (u) { | |
2286 | r = unit_full_printf(u, word, &k); | |
2287 | if (r < 0) { | |
2288 | log_syntax(unit, LOG_ERR, filename, line, r, | |
063c4b1a | 2289 | "Failed to resolve unit specifiers in %s, ignoring: %m", word); |
00819cc1 LP |
2290 | continue; |
2291 | } | |
ae2a15bc LP |
2292 | } else |
2293 | k = TAKE_PTR(word); | |
00819cc1 LP |
2294 | |
2295 | if (!env_assignment_is_valid(k) && !env_name_is_valid(k)) { | |
2296 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
2297 | "Invalid environment name or assignment %s, ignoring: %s", lvalue, k); | |
2298 | continue; | |
2299 | } | |
2300 | ||
2301 | if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) | |
2302 | return log_oom(); | |
2303 | ||
1cc6c93a | 2304 | n[nlen++] = TAKE_PTR(k); |
00819cc1 | 2305 | n[nlen] = NULL; |
00819cc1 LP |
2306 | } |
2307 | ||
2308 | if (n) { | |
2309 | r = strv_extend_strv(unsetenv, n, true); | |
2310 | if (r < 0) | |
2311 | return r; | |
2312 | } | |
2313 | ||
2314 | return 0; | |
2315 | } | |
2316 | ||
d3070fbd LP |
2317 | int config_parse_log_extra_fields( |
2318 | const char *unit, | |
2319 | const char *filename, | |
2320 | unsigned line, | |
2321 | const char *section, | |
2322 | unsigned section_line, | |
2323 | const char *lvalue, | |
2324 | int ltype, | |
2325 | const char *rvalue, | |
2326 | void *data, | |
2327 | void *userdata) { | |
2328 | ||
2329 | ExecContext *c = data; | |
2330 | Unit *u = userdata; | |
063c4b1a | 2331 | const char *p = rvalue; |
d3070fbd LP |
2332 | int r; |
2333 | ||
2334 | assert(filename); | |
2335 | assert(lvalue); | |
2336 | assert(rvalue); | |
2337 | assert(c); | |
2338 | ||
2339 | if (isempty(rvalue)) { | |
2340 | exec_context_free_log_extra_fields(c); | |
2341 | return 0; | |
2342 | } | |
2343 | ||
063c4b1a | 2344 | for (;;) { |
d3070fbd LP |
2345 | _cleanup_free_ char *word = NULL, *k = NULL; |
2346 | struct iovec *t; | |
2347 | const char *eq; | |
2348 | ||
2349 | r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_QUOTES); | |
2350 | if (r == 0) | |
063c4b1a | 2351 | return 0; |
d3070fbd LP |
2352 | if (r == -ENOMEM) |
2353 | return log_oom(); | |
2354 | if (r < 0) { | |
2355 | log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); | |
2356 | return 0; | |
2357 | } | |
2358 | ||
2359 | r = unit_full_printf(u, word, &k); | |
2360 | if (r < 0) { | |
063c4b1a | 2361 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", word); |
d3070fbd LP |
2362 | continue; |
2363 | } | |
2364 | ||
2365 | eq = strchr(k, '='); | |
2366 | if (!eq) { | |
063c4b1a | 2367 | log_syntax(unit, LOG_ERR, filename, line, 0, "Log field lacks '=' character, ignoring: %s", k); |
d3070fbd LP |
2368 | continue; |
2369 | } | |
2370 | ||
2371 | if (!journal_field_valid(k, eq-k, false)) { | |
063c4b1a | 2372 | log_syntax(unit, LOG_ERR, filename, line, 0, "Log field name is invalid, ignoring: %s", k); |
d3070fbd LP |
2373 | continue; |
2374 | } | |
2375 | ||
aa484f35 | 2376 | t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec)); |
d3070fbd LP |
2377 | if (!t) |
2378 | return log_oom(); | |
2379 | ||
2380 | c->log_extra_fields = t; | |
2381 | c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE_STRING(k); | |
2382 | ||
2383 | k = NULL; | |
2384 | } | |
d3070fbd LP |
2385 | } |
2386 | ||
59fccdc5 LP |
2387 | int config_parse_unit_condition_path( |
2388 | const char *unit, | |
2389 | const char *filename, | |
2390 | unsigned line, | |
2391 | const char *section, | |
2392 | unsigned section_line, | |
2393 | const char *lvalue, | |
2394 | int ltype, | |
2395 | const char *rvalue, | |
2396 | void *data, | |
2397 | void *userdata) { | |
52661efd | 2398 | |
2fbe635a | 2399 | _cleanup_free_ char *p = NULL; |
59fccdc5 LP |
2400 | Condition **list = data, *c; |
2401 | ConditionType t = ltype; | |
2402 | bool trigger, negate; | |
2403 | Unit *u = userdata; | |
19f6d710 | 2404 | int r; |
52661efd LP |
2405 | |
2406 | assert(filename); | |
2407 | assert(lvalue); | |
2408 | assert(rvalue); | |
2409 | assert(data); | |
2410 | ||
74051b9b LP |
2411 | if (isempty(rvalue)) { |
2412 | /* Empty assignment resets the list */ | |
447021aa | 2413 | *list = condition_free_list(*list); |
74051b9b LP |
2414 | return 0; |
2415 | } | |
2416 | ||
ab7f148f LP |
2417 | trigger = rvalue[0] == '|'; |
2418 | if (trigger) | |
267632f0 LP |
2419 | rvalue++; |
2420 | ||
ab7f148f LP |
2421 | negate = rvalue[0] == '!'; |
2422 | if (negate) | |
52661efd LP |
2423 | rvalue++; |
2424 | ||
19f6d710 | 2425 | r = unit_full_printf(u, rvalue, &p); |
59fccdc5 | 2426 | if (r < 0) { |
063c4b1a | 2427 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue); |
59fccdc5 | 2428 | return 0; |
19f6d710 | 2429 | } |
095b2d7a | 2430 | |
2f4d31c1 YW |
2431 | r = path_simplify_and_warn(p, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
2432 | if (r < 0) | |
52661efd | 2433 | return 0; |
52661efd | 2434 | |
59fccdc5 | 2435 | c = condition_new(t, p, trigger, negate); |
ab7f148f | 2436 | if (!c) |
74051b9b | 2437 | return log_oom(); |
52661efd | 2438 | |
59fccdc5 | 2439 | LIST_PREPEND(conditions, *list, c); |
52661efd LP |
2440 | return 0; |
2441 | } | |
2442 | ||
59fccdc5 LP |
2443 | int config_parse_unit_condition_string( |
2444 | const char *unit, | |
2445 | const char *filename, | |
2446 | unsigned line, | |
2447 | const char *section, | |
2448 | unsigned section_line, | |
2449 | const char *lvalue, | |
2450 | int ltype, | |
2451 | const char *rvalue, | |
2452 | void *data, | |
2453 | void *userdata) { | |
039655a4 | 2454 | |
2fbe635a | 2455 | _cleanup_free_ char *s = NULL; |
59fccdc5 LP |
2456 | Condition **list = data, *c; |
2457 | ConditionType t = ltype; | |
2458 | bool trigger, negate; | |
2459 | Unit *u = userdata; | |
19f6d710 | 2460 | int r; |
039655a4 LP |
2461 | |
2462 | assert(filename); | |
2463 | assert(lvalue); | |
2464 | assert(rvalue); | |
2465 | assert(data); | |
2466 | ||
74051b9b LP |
2467 | if (isempty(rvalue)) { |
2468 | /* Empty assignment resets the list */ | |
447021aa | 2469 | *list = condition_free_list(*list); |
74051b9b LP |
2470 | return 0; |
2471 | } | |
2472 | ||
c0d6e764 LP |
2473 | trigger = rvalue[0] == '|'; |
2474 | if (trigger) | |
267632f0 LP |
2475 | rvalue++; |
2476 | ||
c0d6e764 LP |
2477 | negate = rvalue[0] == '!'; |
2478 | if (negate) | |
039655a4 LP |
2479 | rvalue++; |
2480 | ||
19f6d710 | 2481 | r = unit_full_printf(u, rvalue, &s); |
59fccdc5 | 2482 | if (r < 0) { |
063c4b1a | 2483 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
59fccdc5 | 2484 | return 0; |
19f6d710 | 2485 | } |
095b2d7a | 2486 | |
59fccdc5 | 2487 | c = condition_new(t, s, trigger, negate); |
c0d6e764 LP |
2488 | if (!c) |
2489 | return log_oom(); | |
039655a4 | 2490 | |
59fccdc5 | 2491 | LIST_PREPEND(conditions, *list, c); |
039655a4 LP |
2492 | return 0; |
2493 | } | |
2494 | ||
59fccdc5 LP |
2495 | int config_parse_unit_condition_null( |
2496 | const char *unit, | |
2497 | const char *filename, | |
2498 | unsigned line, | |
2499 | const char *section, | |
2500 | unsigned section_line, | |
2501 | const char *lvalue, | |
2502 | int ltype, | |
2503 | const char *rvalue, | |
2504 | void *data, | |
2505 | void *userdata) { | |
d257ddef | 2506 | |
59fccdc5 | 2507 | Condition **list = data, *c; |
267632f0 | 2508 | bool trigger, negate; |
d257ddef LP |
2509 | int b; |
2510 | ||
2511 | assert(filename); | |
2512 | assert(lvalue); | |
2513 | assert(rvalue); | |
2514 | assert(data); | |
2515 | ||
74051b9b LP |
2516 | if (isempty(rvalue)) { |
2517 | /* Empty assignment resets the list */ | |
447021aa | 2518 | *list = condition_free_list(*list); |
74051b9b LP |
2519 | return 0; |
2520 | } | |
2521 | ||
2522 | trigger = rvalue[0] == '|'; | |
2523 | if (trigger) | |
267632f0 LP |
2524 | rvalue++; |
2525 | ||
74051b9b LP |
2526 | negate = rvalue[0] == '!'; |
2527 | if (negate) | |
d257ddef LP |
2528 | rvalue++; |
2529 | ||
74051b9b LP |
2530 | b = parse_boolean(rvalue); |
2531 | if (b < 0) { | |
12ca818f | 2532 | log_syntax(unit, LOG_ERR, filename, line, b, "Failed to parse boolean value in condition, ignoring: %s", rvalue); |
d257ddef LP |
2533 | return 0; |
2534 | } | |
2535 | ||
2536 | if (!b) | |
2537 | negate = !negate; | |
2538 | ||
74051b9b LP |
2539 | c = condition_new(CONDITION_NULL, NULL, trigger, negate); |
2540 | if (!c) | |
2541 | return log_oom(); | |
d257ddef | 2542 | |
59fccdc5 | 2543 | LIST_PREPEND(conditions, *list, c); |
d257ddef LP |
2544 | return 0; |
2545 | } | |
2546 | ||
a57f7e2c LP |
2547 | int config_parse_unit_requires_mounts_for( |
2548 | const char *unit, | |
2549 | const char *filename, | |
2550 | unsigned line, | |
2551 | const char *section, | |
71a61510 | 2552 | unsigned section_line, |
a57f7e2c LP |
2553 | const char *lvalue, |
2554 | int ltype, | |
2555 | const char *rvalue, | |
2556 | void *data, | |
2557 | void *userdata) { | |
7c8fa05c | 2558 | |
063c4b1a | 2559 | const char *p = rvalue; |
7c8fa05c | 2560 | Unit *u = userdata; |
035fe294 | 2561 | int r; |
7c8fa05c LP |
2562 | |
2563 | assert(filename); | |
2564 | assert(lvalue); | |
2565 | assert(rvalue); | |
2566 | assert(data); | |
2567 | ||
063c4b1a | 2568 | for (;;) { |
744bb5b1 | 2569 | _cleanup_free_ char *word = NULL, *resolved = NULL; |
a57f7e2c | 2570 | |
035fe294 ZJS |
2571 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
2572 | if (r == 0) | |
2573 | return 0; | |
2574 | if (r == -ENOMEM) | |
a57f7e2c | 2575 | return log_oom(); |
035fe294 ZJS |
2576 | if (r < 0) { |
2577 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
2578 | "Invalid syntax, ignoring: %s", rvalue); | |
2579 | return 0; | |
2580 | } | |
7c8fa05c | 2581 | |
744bb5b1 LP |
2582 | r = unit_full_printf(u, word, &resolved); |
2583 | if (r < 0) { | |
063c4b1a | 2584 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word); |
744bb5b1 LP |
2585 | continue; |
2586 | } | |
2587 | ||
2f4d31c1 YW |
2588 | r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
2589 | if (r < 0) | |
2590 | continue; | |
2591 | ||
eef85c4a | 2592 | r = unit_require_mounts_for(u, resolved, UNIT_DEPENDENCY_FILE); |
a57f7e2c | 2593 | if (r < 0) { |
063c4b1a | 2594 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add required mount '%s', ignoring: %m", resolved); |
a57f7e2c LP |
2595 | continue; |
2596 | } | |
2597 | } | |
7c8fa05c | 2598 | } |
9e372868 | 2599 | |
e8e581bf ZJS |
2600 | int config_parse_documentation(const char *unit, |
2601 | const char *filename, | |
2602 | unsigned line, | |
2603 | const char *section, | |
71a61510 | 2604 | unsigned section_line, |
e8e581bf ZJS |
2605 | const char *lvalue, |
2606 | int ltype, | |
2607 | const char *rvalue, | |
2608 | void *data, | |
2609 | void *userdata) { | |
49dbfa7b LP |
2610 | |
2611 | Unit *u = userdata; | |
2612 | int r; | |
2613 | char **a, **b; | |
2614 | ||
2615 | assert(filename); | |
2616 | assert(lvalue); | |
2617 | assert(rvalue); | |
2618 | assert(u); | |
2619 | ||
74051b9b LP |
2620 | if (isempty(rvalue)) { |
2621 | /* Empty assignment resets the list */ | |
6796073e | 2622 | u->documentation = strv_free(u->documentation); |
74051b9b LP |
2623 | return 0; |
2624 | } | |
2625 | ||
71a61510 | 2626 | r = config_parse_unit_strv_printf(unit, filename, line, section, section_line, lvalue, ltype, |
e8e581bf | 2627 | rvalue, data, userdata); |
49dbfa7b LP |
2628 | if (r < 0) |
2629 | return r; | |
2630 | ||
2631 | for (a = b = u->documentation; a && *a; a++) { | |
2632 | ||
a2e03378 | 2633 | if (documentation_url_is_valid(*a)) |
49dbfa7b LP |
2634 | *(b++) = *a; |
2635 | else { | |
12ca818f | 2636 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid URL, ignoring: %s", *a); |
49dbfa7b LP |
2637 | free(*a); |
2638 | } | |
2639 | } | |
f6d2d421 ZJS |
2640 | if (b) |
2641 | *b = NULL; | |
49dbfa7b LP |
2642 | |
2643 | return r; | |
2644 | } | |
2645 | ||
349cc4a5 | 2646 | #if HAVE_SECCOMP |
17df7223 LP |
2647 | int config_parse_syscall_filter( |
2648 | const char *unit, | |
2649 | const char *filename, | |
2650 | unsigned line, | |
2651 | const char *section, | |
2652 | unsigned section_line, | |
2653 | const char *lvalue, | |
2654 | int ltype, | |
2655 | const char *rvalue, | |
2656 | void *data, | |
2657 | void *userdata) { | |
2658 | ||
8351ceae LP |
2659 | ExecContext *c = data; |
2660 | Unit *u = userdata; | |
b5fb3789 | 2661 | bool invert = false; |
8130926d | 2662 | const char *p; |
17df7223 | 2663 | int r; |
8351ceae LP |
2664 | |
2665 | assert(filename); | |
2666 | assert(lvalue); | |
2667 | assert(rvalue); | |
2668 | assert(u); | |
2669 | ||
74051b9b LP |
2670 | if (isempty(rvalue)) { |
2671 | /* Empty assignment resets the list */ | |
8cfa775f | 2672 | c->syscall_filter = hashmap_free(c->syscall_filter); |
17df7223 | 2673 | c->syscall_whitelist = false; |
74051b9b LP |
2674 | return 0; |
2675 | } | |
2676 | ||
8351ceae LP |
2677 | if (rvalue[0] == '~') { |
2678 | invert = true; | |
2679 | rvalue++; | |
2680 | } | |
2681 | ||
17df7223 | 2682 | if (!c->syscall_filter) { |
8cfa775f | 2683 | c->syscall_filter = hashmap_new(NULL); |
17df7223 LP |
2684 | if (!c->syscall_filter) |
2685 | return log_oom(); | |
2686 | ||
c0467cf3 | 2687 | if (invert) |
17df7223 LP |
2688 | /* Allow everything but the ones listed */ |
2689 | c->syscall_whitelist = false; | |
c0467cf3 | 2690 | else { |
17df7223 LP |
2691 | /* Allow nothing but the ones listed */ |
2692 | c->syscall_whitelist = true; | |
8351ceae | 2693 | |
17df7223 | 2694 | /* Accept default syscalls if we are on a whitelist */ |
13d92c63 | 2695 | r = seccomp_parse_syscall_filter("@default", -1, c->syscall_filter, SECCOMP_PARSE_WHITELIST); |
201c1cc2 TM |
2696 | if (r < 0) |
2697 | return r; | |
c0467cf3 | 2698 | } |
8351ceae LP |
2699 | } |
2700 | ||
8130926d LP |
2701 | p = rvalue; |
2702 | for (;;) { | |
8cfa775f YW |
2703 | _cleanup_free_ char *word = NULL, *name = NULL; |
2704 | int num; | |
8351ceae | 2705 | |
8130926d LP |
2706 | r = extract_first_word(&p, &word, NULL, 0); |
2707 | if (r == 0) | |
063c4b1a | 2708 | return 0; |
8130926d | 2709 | if (r == -ENOMEM) |
74051b9b | 2710 | return log_oom(); |
8130926d LP |
2711 | if (r < 0) { |
2712 | log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); | |
063c4b1a | 2713 | return 0; |
8130926d | 2714 | } |
8351ceae | 2715 | |
8cfa775f YW |
2716 | r = parse_syscall_and_errno(word, &name, &num); |
2717 | if (r < 0) { | |
2718 | log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse syscall:errno, ignoring: %s", word); | |
2719 | continue; | |
2720 | } | |
2721 | ||
13d92c63 LP |
2722 | r = seccomp_parse_syscall_filter_full(name, num, c->syscall_filter, |
2723 | SECCOMP_PARSE_LOG|SECCOMP_PARSE_PERMISSIVE|(invert ? SECCOMP_PARSE_INVERT : 0)|(c->syscall_whitelist ? SECCOMP_PARSE_WHITELIST : 0), | |
2724 | unit, filename, line); | |
201c1cc2 TM |
2725 | if (r < 0) |
2726 | return r; | |
c0467cf3 | 2727 | } |
17df7223 LP |
2728 | } |
2729 | ||
57183d11 LP |
2730 | int config_parse_syscall_archs( |
2731 | const char *unit, | |
2732 | const char *filename, | |
2733 | unsigned line, | |
2734 | const char *section, | |
2735 | unsigned section_line, | |
2736 | const char *lvalue, | |
2737 | int ltype, | |
2738 | const char *rvalue, | |
2739 | void *data, | |
2740 | void *userdata) { | |
2741 | ||
063c4b1a | 2742 | const char *p = rvalue; |
d3b1c508 | 2743 | Set **archs = data; |
57183d11 LP |
2744 | int r; |
2745 | ||
2746 | if (isempty(rvalue)) { | |
525d3cc7 | 2747 | *archs = set_free(*archs); |
57183d11 LP |
2748 | return 0; |
2749 | } | |
2750 | ||
d5099efc | 2751 | r = set_ensure_allocated(archs, NULL); |
57183d11 LP |
2752 | if (r < 0) |
2753 | return log_oom(); | |
2754 | ||
063c4b1a | 2755 | for (;;) { |
035fe294 | 2756 | _cleanup_free_ char *word = NULL; |
57183d11 LP |
2757 | uint32_t a; |
2758 | ||
035fe294 ZJS |
2759 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
2760 | if (r == 0) | |
2761 | return 0; | |
2762 | if (r == -ENOMEM) | |
57183d11 | 2763 | return log_oom(); |
035fe294 ZJS |
2764 | if (r < 0) { |
2765 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
2766 | "Invalid syntax, ignoring: %s", rvalue); | |
2767 | return 0; | |
2768 | } | |
57183d11 | 2769 | |
035fe294 | 2770 | r = seccomp_arch_from_string(word, &a); |
57183d11 | 2771 | if (r < 0) { |
035fe294 ZJS |
2772 | log_syntax(unit, LOG_ERR, filename, line, r, |
2773 | "Failed to parse system call architecture \"%s\", ignoring: %m", word); | |
57183d11 LP |
2774 | continue; |
2775 | } | |
2776 | ||
d3b1c508 | 2777 | r = set_put(*archs, UINT32_TO_PTR(a + 1)); |
57183d11 LP |
2778 | if (r < 0) |
2779 | return log_oom(); | |
2780 | } | |
57183d11 LP |
2781 | } |
2782 | ||
17df7223 LP |
2783 | int config_parse_syscall_errno( |
2784 | const char *unit, | |
2785 | const char *filename, | |
2786 | unsigned line, | |
2787 | const char *section, | |
2788 | unsigned section_line, | |
2789 | const char *lvalue, | |
2790 | int ltype, | |
2791 | const char *rvalue, | |
2792 | void *data, | |
2793 | void *userdata) { | |
2794 | ||
2795 | ExecContext *c = data; | |
2796 | int e; | |
2797 | ||
2798 | assert(filename); | |
2799 | assert(lvalue); | |
2800 | assert(rvalue); | |
2801 | ||
2802 | if (isempty(rvalue)) { | |
2803 | /* Empty assignment resets to KILL */ | |
2804 | c->syscall_errno = 0; | |
2805 | return 0; | |
8351ceae LP |
2806 | } |
2807 | ||
3df90f24 YW |
2808 | e = parse_errno(rvalue); |
2809 | if (e <= 0) { | |
12ca818f | 2810 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse error number, ignoring: %s", rvalue); |
17df7223 LP |
2811 | return 0; |
2812 | } | |
8351ceae | 2813 | |
17df7223 | 2814 | c->syscall_errno = e; |
8351ceae LP |
2815 | return 0; |
2816 | } | |
4298d0b5 LP |
2817 | |
2818 | int config_parse_address_families( | |
2819 | const char *unit, | |
2820 | const char *filename, | |
2821 | unsigned line, | |
2822 | const char *section, | |
2823 | unsigned section_line, | |
2824 | const char *lvalue, | |
2825 | int ltype, | |
2826 | const char *rvalue, | |
2827 | void *data, | |
2828 | void *userdata) { | |
2829 | ||
2830 | ExecContext *c = data; | |
4298d0b5 | 2831 | bool invert = false; |
035fe294 | 2832 | const char *p; |
4298d0b5 LP |
2833 | int r; |
2834 | ||
2835 | assert(filename); | |
2836 | assert(lvalue); | |
2837 | assert(rvalue); | |
4298d0b5 LP |
2838 | |
2839 | if (isempty(rvalue)) { | |
2840 | /* Empty assignment resets the list */ | |
525d3cc7 | 2841 | c->address_families = set_free(c->address_families); |
4298d0b5 LP |
2842 | c->address_families_whitelist = false; |
2843 | return 0; | |
2844 | } | |
2845 | ||
2846 | if (rvalue[0] == '~') { | |
2847 | invert = true; | |
2848 | rvalue++; | |
2849 | } | |
2850 | ||
2851 | if (!c->address_families) { | |
d5099efc | 2852 | c->address_families = set_new(NULL); |
4298d0b5 LP |
2853 | if (!c->address_families) |
2854 | return log_oom(); | |
2855 | ||
2856 | c->address_families_whitelist = !invert; | |
2857 | } | |
2858 | ||
035fe294 ZJS |
2859 | for (p = rvalue;;) { |
2860 | _cleanup_free_ char *word = NULL; | |
4298d0b5 LP |
2861 | int af; |
2862 | ||
035fe294 ZJS |
2863 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
2864 | if (r == 0) | |
2865 | return 0; | |
2866 | if (r == -ENOMEM) | |
4298d0b5 | 2867 | return log_oom(); |
035fe294 ZJS |
2868 | if (r < 0) { |
2869 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
2870 | "Invalid syntax, ignoring: %s", rvalue); | |
2871 | return 0; | |
2872 | } | |
4298d0b5 | 2873 | |
035fe294 | 2874 | af = af_from_name(word); |
063c4b1a | 2875 | if (af <= 0) { |
035fe294 | 2876 | log_syntax(unit, LOG_ERR, filename, line, 0, |
063c4b1a | 2877 | "Failed to parse address family, ignoring: %s", word); |
4298d0b5 LP |
2878 | continue; |
2879 | } | |
2880 | ||
2881 | /* If we previously wanted to forbid an address family and now | |
035fe294 | 2882 | * we want to allow it, then just remove it from the list. |
4298d0b5 LP |
2883 | */ |
2884 | if (!invert == c->address_families_whitelist) { | |
2885 | r = set_put(c->address_families, INT_TO_PTR(af)); | |
4298d0b5 LP |
2886 | if (r < 0) |
2887 | return log_oom(); | |
2888 | } else | |
2889 | set_remove(c->address_families, INT_TO_PTR(af)); | |
2890 | } | |
4298d0b5 | 2891 | } |
add00535 LP |
2892 | |
2893 | int config_parse_restrict_namespaces( | |
2894 | const char *unit, | |
2895 | const char *filename, | |
2896 | unsigned line, | |
2897 | const char *section, | |
2898 | unsigned section_line, | |
2899 | const char *lvalue, | |
2900 | int ltype, | |
2901 | const char *rvalue, | |
2902 | void *data, | |
2903 | void *userdata) { | |
2904 | ||
2905 | ExecContext *c = data; | |
aa9d574d | 2906 | unsigned long flags; |
add00535 LP |
2907 | bool invert = false; |
2908 | int r; | |
2909 | ||
2910 | if (isempty(rvalue)) { | |
2911 | /* Reset to the default. */ | |
aa9d574d YW |
2912 | c->restrict_namespaces = NAMESPACE_FLAGS_INITIAL; |
2913 | return 0; | |
2914 | } | |
2915 | ||
2916 | /* Boolean parameter ignores the previous settings */ | |
2917 | r = parse_boolean(rvalue); | |
2918 | if (r > 0) { | |
2919 | c->restrict_namespaces = 0; | |
2920 | return 0; | |
2921 | } else if (r == 0) { | |
add00535 LP |
2922 | c->restrict_namespaces = NAMESPACE_FLAGS_ALL; |
2923 | return 0; | |
2924 | } | |
2925 | ||
2926 | if (rvalue[0] == '~') { | |
2927 | invert = true; | |
2928 | rvalue++; | |
2929 | } | |
2930 | ||
aa9d574d YW |
2931 | /* Not a boolean argument, in this case it's a list of namespace types. */ |
2932 | r = namespace_flags_from_string(rvalue, &flags); | |
2933 | if (r < 0) { | |
2934 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse namespace type string, ignoring: %s", rvalue); | |
2935 | return 0; | |
add00535 LP |
2936 | } |
2937 | ||
aa9d574d YW |
2938 | if (c->restrict_namespaces == NAMESPACE_FLAGS_INITIAL) |
2939 | /* Initial assignment. Just set the value. */ | |
2940 | c->restrict_namespaces = invert ? (~flags) & NAMESPACE_FLAGS_ALL : flags; | |
2941 | else | |
2942 | /* Merge the value with the previous one. */ | |
2943 | SET_FLAG(c->restrict_namespaces, flags, !invert); | |
add00535 LP |
2944 | |
2945 | return 0; | |
2946 | } | |
c0467cf3 | 2947 | #endif |
8351ceae | 2948 | |
a016b922 LP |
2949 | int config_parse_unit_slice( |
2950 | const char *unit, | |
2951 | const char *filename, | |
2952 | unsigned line, | |
2953 | const char *section, | |
71a61510 | 2954 | unsigned section_line, |
a016b922 LP |
2955 | const char *lvalue, |
2956 | int ltype, | |
2957 | const char *rvalue, | |
2958 | void *data, | |
2959 | void *userdata) { | |
2960 | ||
063c4b1a | 2961 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
a016b922 | 2962 | _cleanup_free_ char *k = NULL; |
d79200e2 | 2963 | Unit *u = userdata, *slice = NULL; |
a016b922 LP |
2964 | int r; |
2965 | ||
2966 | assert(filename); | |
2967 | assert(lvalue); | |
2968 | assert(rvalue); | |
2969 | assert(u); | |
2970 | ||
19f6d710 | 2971 | r = unit_name_printf(u, rvalue, &k); |
d79200e2 | 2972 | if (r < 0) { |
063c4b1a | 2973 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue); |
d79200e2 | 2974 | return 0; |
19f6d710 | 2975 | } |
a016b922 | 2976 | |
063c4b1a | 2977 | r = manager_load_unit(u->manager, k, NULL, &error, &slice); |
a016b922 | 2978 | if (r < 0) { |
063c4b1a | 2979 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to load slice unit %s, ignoring: %s", k, bus_error_message(&error, r)); |
a016b922 LP |
2980 | return 0; |
2981 | } | |
2982 | ||
d79200e2 LP |
2983 | r = unit_set_slice(u, slice); |
2984 | if (r < 0) { | |
063c4b1a | 2985 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to assign slice %s to unit %s, ignoring: %m", slice->id, u->id); |
a016b922 LP |
2986 | return 0; |
2987 | } | |
2988 | ||
a016b922 LP |
2989 | return 0; |
2990 | } | |
2991 | ||
b2f8b02e LP |
2992 | int config_parse_cpu_quota( |
2993 | const char *unit, | |
2994 | const char *filename, | |
2995 | unsigned line, | |
2996 | const char *section, | |
2997 | unsigned section_line, | |
2998 | const char *lvalue, | |
2999 | int ltype, | |
3000 | const char *rvalue, | |
3001 | void *data, | |
3002 | void *userdata) { | |
3003 | ||
3004 | CGroupContext *c = data; | |
9184ca48 | 3005 | int r; |
b2f8b02e LP |
3006 | |
3007 | assert(filename); | |
3008 | assert(lvalue); | |
3009 | assert(rvalue); | |
3010 | ||
3011 | if (isempty(rvalue)) { | |
3a43da28 | 3012 | c->cpu_quota_per_sec_usec = USEC_INFINITY; |
b2f8b02e LP |
3013 | return 0; |
3014 | } | |
3015 | ||
f806dfd3 | 3016 | r = parse_permille_unbounded(rvalue); |
9184ca48 | 3017 | if (r <= 0) { |
063c4b1a | 3018 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid CPU quota '%s', ignoring.", rvalue); |
9a054909 | 3019 | return 0; |
b2f8b02e LP |
3020 | } |
3021 | ||
f806dfd3 | 3022 | c->cpu_quota_per_sec_usec = ((usec_t) r * USEC_PER_SEC) / 1000U; |
b2f8b02e LP |
3023 | return 0; |
3024 | } | |
3025 | ||
4ad49000 LP |
3026 | int config_parse_memory_limit( |
3027 | const char *unit, | |
3028 | const char *filename, | |
3029 | unsigned line, | |
3030 | const char *section, | |
71a61510 | 3031 | unsigned section_line, |
4ad49000 LP |
3032 | const char *lvalue, |
3033 | int ltype, | |
3034 | const char *rvalue, | |
3035 | void *data, | |
3036 | void *userdata) { | |
3037 | ||
3038 | CGroupContext *c = data; | |
da4d897e | 3039 | uint64_t bytes = CGROUP_LIMIT_MAX; |
4ad49000 LP |
3040 | int r; |
3041 | ||
e57c9ce1 | 3042 | if (!isempty(rvalue) && !streq(rvalue, "infinity")) { |
875ae566 | 3043 | |
f806dfd3 | 3044 | r = parse_permille(rvalue); |
875ae566 LP |
3045 | if (r < 0) { |
3046 | r = parse_size(rvalue, 1024, &bytes); | |
3047 | if (r < 0) { | |
063c4b1a | 3048 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid memory limit '%s', ignoring: %m", rvalue); |
875ae566 LP |
3049 | return 0; |
3050 | } | |
3051 | } else | |
f806dfd3 | 3052 | bytes = physical_memory_scale(r, 1000U); |
875ae566 | 3053 | |
906bdbf5 YW |
3054 | if (bytes >= UINT64_MAX || |
3055 | (bytes <= 0 && !streq(lvalue, "MemorySwapMax"))) { | |
063c4b1a | 3056 | log_syntax(unit, LOG_ERR, filename, line, 0, "Memory limit '%s' out of range, ignoring.", rvalue); |
da4d897e TH |
3057 | return 0; |
3058 | } | |
4ad49000 LP |
3059 | } |
3060 | ||
48422635 TH |
3061 | if (streq(lvalue, "MemoryMin")) |
3062 | c->memory_min = bytes; | |
3063 | else if (streq(lvalue, "MemoryLow")) | |
da4d897e TH |
3064 | c->memory_low = bytes; |
3065 | else if (streq(lvalue, "MemoryHigh")) | |
3066 | c->memory_high = bytes; | |
3067 | else if (streq(lvalue, "MemoryMax")) | |
3068 | c->memory_max = bytes; | |
96e131ea WC |
3069 | else if (streq(lvalue, "MemorySwapMax")) |
3070 | c->memory_swap_max = bytes; | |
3071 | else if (streq(lvalue, "MemoryLimit")) | |
da4d897e | 3072 | c->memory_limit = bytes; |
96e131ea WC |
3073 | else |
3074 | return -EINVAL; | |
4ad49000 | 3075 | |
4ad49000 LP |
3076 | return 0; |
3077 | } | |
3078 | ||
03a7b521 LP |
3079 | int config_parse_tasks_max( |
3080 | const char *unit, | |
3081 | const char *filename, | |
3082 | unsigned line, | |
3083 | const char *section, | |
3084 | unsigned section_line, | |
3085 | const char *lvalue, | |
3086 | int ltype, | |
3087 | const char *rvalue, | |
3088 | void *data, | |
3089 | void *userdata) { | |
3090 | ||
f5058264 TH |
3091 | uint64_t *tasks_max = data, v; |
3092 | Unit *u = userdata; | |
03a7b521 LP |
3093 | int r; |
3094 | ||
f5058264 TH |
3095 | if (isempty(rvalue)) { |
3096 | *tasks_max = u->manager->default_tasks_max; | |
3097 | return 0; | |
3098 | } | |
3099 | ||
3100 | if (streq(rvalue, "infinity")) { | |
3101 | *tasks_max = CGROUP_LIMIT_MAX; | |
03a7b521 LP |
3102 | return 0; |
3103 | } | |
3104 | ||
f806dfd3 | 3105 | r = parse_permille(rvalue); |
83f8e808 | 3106 | if (r < 0) { |
f5058264 | 3107 | r = safe_atou64(rvalue, &v); |
83f8e808 | 3108 | if (r < 0) { |
063c4b1a | 3109 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid maximum tasks value '%s', ignoring: %m", rvalue); |
83f8e808 LP |
3110 | return 0; |
3111 | } | |
3112 | } else | |
f806dfd3 | 3113 | v = system_tasks_max_scale(r, 1000U); |
83f8e808 | 3114 | |
f5058264 | 3115 | if (v <= 0 || v >= UINT64_MAX) { |
063c4b1a | 3116 | log_syntax(unit, LOG_ERR, filename, line, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue); |
03a7b521 LP |
3117 | return 0; |
3118 | } | |
3119 | ||
f5058264 | 3120 | *tasks_max = v; |
03a7b521 LP |
3121 | return 0; |
3122 | } | |
3123 | ||
02638280 LP |
3124 | int config_parse_delegate( |
3125 | const char *unit, | |
3126 | const char *filename, | |
3127 | unsigned line, | |
3128 | const char *section, | |
3129 | unsigned section_line, | |
3130 | const char *lvalue, | |
3131 | int ltype, | |
3132 | const char *rvalue, | |
3133 | void *data, | |
3134 | void *userdata) { | |
3135 | ||
3136 | CGroupContext *c = data; | |
ecae73d7 | 3137 | UnitType t; |
02638280 LP |
3138 | int r; |
3139 | ||
ecae73d7 ZJS |
3140 | t = unit_name_to_type(unit); |
3141 | assert(t != _UNIT_TYPE_INVALID); | |
3142 | ||
3143 | if (!unit_vtable[t]->can_delegate) { | |
3144 | log_syntax(unit, LOG_ERR, filename, line, 0, "Delegate= setting not supported for this unit type, ignoring."); | |
3145 | return 0; | |
3146 | } | |
3147 | ||
02638280 LP |
3148 | /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or turn it |
3149 | * off for all. Or it takes a list of controller names, in which case we add the specified controllers to the | |
3150 | * mask to delegate. */ | |
3151 | ||
1bdfc7b9 | 3152 | if (isempty(rvalue)) { |
d48013f8 YW |
3153 | /* An empty string resets controllers and set Delegate=yes. */ |
3154 | c->delegate = true; | |
1bdfc7b9 YW |
3155 | c->delegate_controllers = 0; |
3156 | return 0; | |
3157 | } | |
3158 | ||
02638280 LP |
3159 | r = parse_boolean(rvalue); |
3160 | if (r < 0) { | |
3161 | const char *p = rvalue; | |
3162 | CGroupMask mask = 0; | |
3163 | ||
3164 | for (;;) { | |
3165 | _cleanup_free_ char *word = NULL; | |
3166 | CGroupController cc; | |
3167 | ||
3168 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); | |
3169 | if (r == 0) | |
3170 | break; | |
3171 | if (r == -ENOMEM) | |
3172 | return log_oom(); | |
3173 | if (r < 0) { | |
3174 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); | |
ff1b8455 | 3175 | return 0; |
02638280 LP |
3176 | } |
3177 | ||
3178 | cc = cgroup_controller_from_string(word); | |
3179 | if (cc < 0) { | |
063c4b1a | 3180 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid controller name '%s', ignoring", word); |
02638280 LP |
3181 | continue; |
3182 | } | |
3183 | ||
3184 | mask |= CGROUP_CONTROLLER_TO_MASK(cc); | |
3185 | } | |
3186 | ||
3187 | c->delegate = true; | |
3188 | c->delegate_controllers |= mask; | |
3189 | ||
3190 | } else if (r > 0) { | |
3191 | c->delegate = true; | |
3192 | c->delegate_controllers = _CGROUP_MASK_ALL; | |
3193 | } else { | |
3194 | c->delegate = false; | |
3195 | c->delegate_controllers = 0; | |
3196 | } | |
3197 | ||
3198 | return 0; | |
3199 | } | |
3200 | ||
4ad49000 LP |
3201 | int config_parse_device_allow( |
3202 | const char *unit, | |
3203 | const char *filename, | |
3204 | unsigned line, | |
3205 | const char *section, | |
71a61510 | 3206 | unsigned section_line, |
4ad49000 LP |
3207 | const char *lvalue, |
3208 | int ltype, | |
3209 | const char *rvalue, | |
3210 | void *data, | |
3211 | void *userdata) { | |
3212 | ||
c9f620bf | 3213 | _cleanup_free_ char *path = NULL, *resolved = NULL; |
4ad49000 | 3214 | CGroupContext *c = data; |
c9f620bf | 3215 | const char *p = rvalue; |
1116e14c | 3216 | int r; |
4ad49000 LP |
3217 | |
3218 | if (isempty(rvalue)) { | |
3219 | while (c->device_allow) | |
3220 | cgroup_context_free_device_allow(c, c->device_allow); | |
3221 | ||
3222 | return 0; | |
3223 | } | |
3224 | ||
c9f620bf YW |
3225 | r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES); |
3226 | if (r == -ENOMEM) | |
3227 | return log_oom(); | |
508f63b4 | 3228 | if (r < 0) { |
1116e14c | 3229 | log_syntax(unit, LOG_WARNING, filename, line, r, |
c9f620bf YW |
3230 | "Invalid syntax, ignoring: %s", rvalue); |
3231 | return 0; | |
3232 | } | |
3233 | if (r == 0) { | |
3234 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
3235 | "Failed to extract device path and rights from '%s', ignoring.", rvalue); | |
20d52ab6 | 3236 | return 0; |
1116e14c NBS |
3237 | } |
3238 | ||
c9f620bf YW |
3239 | r = unit_full_printf(userdata, path, &resolved); |
3240 | if (r < 0) { | |
3241 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3242 | "Failed to resolve unit specifiers in '%s', ignoring: %m", path); | |
4ad49000 LP |
3243 | return 0; |
3244 | } | |
3245 | ||
57e84e75 | 3246 | if (!startswith(resolved, "block-") && !startswith(resolved, "char-")) { |
2f4d31c1 | 3247 | |
57e84e75 LP |
3248 | r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue); |
3249 | if (r < 0) | |
3250 | return 0; | |
3251 | ||
3252 | if (!valid_device_node_path(resolved)) { | |
3253 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved); | |
3254 | return 0; | |
3255 | } | |
c9f620bf | 3256 | } |
4ad49000 | 3257 | |
c9f620bf YW |
3258 | if (!isempty(p) && !in_charset(p, "rwm")) { |
3259 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device rights '%s', ignoring.", p); | |
4ad49000 LP |
3260 | return 0; |
3261 | } | |
3262 | ||
fd870bac | 3263 | return cgroup_add_device_allow(c, resolved, p); |
4ad49000 LP |
3264 | } |
3265 | ||
13c31542 TH |
3266 | int config_parse_io_device_weight( |
3267 | const char *unit, | |
3268 | const char *filename, | |
3269 | unsigned line, | |
3270 | const char *section, | |
3271 | unsigned section_line, | |
3272 | const char *lvalue, | |
3273 | int ltype, | |
3274 | const char *rvalue, | |
3275 | void *data, | |
3276 | void *userdata) { | |
3277 | ||
c9f620bf | 3278 | _cleanup_free_ char *path = NULL, *resolved = NULL; |
13c31542 TH |
3279 | CGroupIODeviceWeight *w; |
3280 | CGroupContext *c = data; | |
c9f620bf | 3281 | const char *p = rvalue; |
13c31542 | 3282 | uint64_t u; |
13c31542 TH |
3283 | int r; |
3284 | ||
3285 | assert(filename); | |
3286 | assert(lvalue); | |
3287 | assert(rvalue); | |
3288 | ||
3289 | if (isempty(rvalue)) { | |
3290 | while (c->io_device_weights) | |
3291 | cgroup_context_free_io_device_weight(c, c->io_device_weights); | |
3292 | ||
3293 | return 0; | |
3294 | } | |
3295 | ||
c9f620bf YW |
3296 | r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES); |
3297 | if (r == -ENOMEM) | |
3298 | return log_oom(); | |
3299 | if (r < 0) { | |
3300 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3301 | "Invalid syntax, ignoring: %s", rvalue); | |
3302 | return 0; | |
3303 | } | |
3304 | if (r == 0 || isempty(p)) { | |
3305 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
3306 | "Failed to extract device path and weight from '%s', ignoring.", rvalue); | |
13c31542 TH |
3307 | return 0; |
3308 | } | |
3309 | ||
c9f620bf YW |
3310 | r = unit_full_printf(userdata, path, &resolved); |
3311 | if (r < 0) { | |
3312 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3313 | "Failed to resolve unit specifiers in '%s', ignoring: %m", path); | |
3314 | return 0; | |
3315 | } | |
13c31542 | 3316 | |
2f4d31c1 YW |
3317 | r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue); |
3318 | if (r < 0) | |
3319 | return 0; | |
3320 | ||
c9f620bf | 3321 | r = cg_weight_parse(p, &u); |
13c31542 | 3322 | if (r < 0) { |
c9f620bf | 3323 | log_syntax(unit, LOG_ERR, filename, line, r, "IO weight '%s' invalid, ignoring: %m", p); |
13c31542 TH |
3324 | return 0; |
3325 | } | |
3326 | ||
3327 | assert(u != CGROUP_WEIGHT_INVALID); | |
3328 | ||
3329 | w = new0(CGroupIODeviceWeight, 1); | |
3330 | if (!w) | |
3331 | return log_oom(); | |
3332 | ||
c9f620bf | 3333 | w->path = TAKE_PTR(resolved); |
13c31542 TH |
3334 | w->weight = u; |
3335 | ||
3336 | LIST_PREPEND(device_weights, c->io_device_weights, w); | |
3337 | return 0; | |
3338 | } | |
3339 | ||
6ae4283c TH |
3340 | int config_parse_io_device_latency( |
3341 | const char *unit, | |
3342 | const char *filename, | |
3343 | unsigned line, | |
3344 | const char *section, | |
3345 | unsigned section_line, | |
3346 | const char *lvalue, | |
3347 | int ltype, | |
3348 | const char *rvalue, | |
3349 | void *data, | |
3350 | void *userdata) { | |
3351 | ||
3352 | _cleanup_free_ char *path = NULL, *resolved = NULL; | |
3353 | CGroupIODeviceLatency *l; | |
3354 | CGroupContext *c = data; | |
3355 | const char *p = rvalue; | |
3356 | usec_t usec; | |
3357 | int r; | |
3358 | ||
3359 | assert(filename); | |
3360 | assert(lvalue); | |
3361 | assert(rvalue); | |
3362 | ||
3363 | if (isempty(rvalue)) { | |
3364 | while (c->io_device_latencies) | |
3365 | cgroup_context_free_io_device_latency(c, c->io_device_latencies); | |
3366 | ||
3367 | return 0; | |
3368 | } | |
3369 | ||
3370 | r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES); | |
3371 | if (r == -ENOMEM) | |
3372 | return log_oom(); | |
3373 | if (r < 0) { | |
3374 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3375 | "Invalid syntax, ignoring: %s", rvalue); | |
3376 | return 0; | |
3377 | } | |
3378 | if (r == 0 || isempty(p)) { | |
3379 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
3380 | "Failed to extract device path and latency from '%s', ignoring.", rvalue); | |
3381 | return 0; | |
3382 | } | |
3383 | ||
3384 | r = unit_full_printf(userdata, path, &resolved); | |
3385 | if (r < 0) { | |
3386 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3387 | "Failed to resolve unit specifiers in '%s', ignoring: %m", path); | |
3388 | return 0; | |
3389 | } | |
3390 | ||
3391 | r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue); | |
3392 | if (r < 0) | |
3393 | return 0; | |
3394 | ||
3395 | if (parse_sec(p, &usec) < 0) { | |
3396 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse timer value, ignoring: %s", p); | |
3397 | return 0; | |
3398 | } | |
3399 | ||
3400 | l = new0(CGroupIODeviceLatency, 1); | |
3401 | if (!l) | |
3402 | return log_oom(); | |
3403 | ||
3404 | l->path = TAKE_PTR(resolved); | |
3405 | l->target_usec = usec; | |
3406 | ||
3407 | LIST_PREPEND(device_latencies, c->io_device_latencies, l); | |
3408 | return 0; | |
3409 | } | |
3410 | ||
13c31542 TH |
3411 | int config_parse_io_limit( |
3412 | const char *unit, | |
3413 | const char *filename, | |
3414 | unsigned line, | |
3415 | const char *section, | |
3416 | unsigned section_line, | |
3417 | const char *lvalue, | |
3418 | int ltype, | |
3419 | const char *rvalue, | |
3420 | void *data, | |
3421 | void *userdata) { | |
3422 | ||
c9f620bf | 3423 | _cleanup_free_ char *path = NULL, *resolved = NULL; |
13c31542 TH |
3424 | CGroupIODeviceLimit *l = NULL, *t; |
3425 | CGroupContext *c = data; | |
9be57249 | 3426 | CGroupIOLimitType type; |
c9f620bf | 3427 | const char *p = rvalue; |
13c31542 | 3428 | uint64_t num; |
13c31542 TH |
3429 | int r; |
3430 | ||
3431 | assert(filename); | |
3432 | assert(lvalue); | |
3433 | assert(rvalue); | |
3434 | ||
9be57249 TH |
3435 | type = cgroup_io_limit_type_from_string(lvalue); |
3436 | assert(type >= 0); | |
13c31542 TH |
3437 | |
3438 | if (isempty(rvalue)) { | |
3439 | LIST_FOREACH(device_limits, l, c->io_device_limits) | |
9be57249 | 3440 | l->limits[type] = cgroup_io_limit_defaults[type]; |
13c31542 TH |
3441 | return 0; |
3442 | } | |
3443 | ||
c9f620bf YW |
3444 | r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES); |
3445 | if (r == -ENOMEM) | |
3446 | return log_oom(); | |
3447 | if (r < 0) { | |
3448 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3449 | "Invalid syntax, ignoring: %s", rvalue); | |
3450 | return 0; | |
3451 | } | |
3452 | if (r == 0 || isempty(p)) { | |
3453 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
3454 | "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue); | |
13c31542 TH |
3455 | return 0; |
3456 | } | |
3457 | ||
c9f620bf YW |
3458 | r = unit_full_printf(userdata, path, &resolved); |
3459 | if (r < 0) { | |
3460 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3461 | "Failed to resolve unit specifiers in '%s', ignoring: %m", path); | |
3462 | return 0; | |
3463 | } | |
13c31542 | 3464 | |
2f4d31c1 YW |
3465 | r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue); |
3466 | if (r < 0) | |
3467 | return 0; | |
3468 | ||
9d5e9b4a | 3469 | if (streq("infinity", p)) |
13c31542 | 3470 | num = CGROUP_LIMIT_MAX; |
9d5e9b4a | 3471 | else { |
c9f620bf | 3472 | r = parse_size(p, 1000, &num); |
13c31542 | 3473 | if (r < 0 || num <= 0) { |
c9f620bf | 3474 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid IO limit '%s', ignoring.", p); |
13c31542 TH |
3475 | return 0; |
3476 | } | |
3477 | } | |
3478 | ||
3479 | LIST_FOREACH(device_limits, t, c->io_device_limits) { | |
c9f620bf | 3480 | if (path_equal(resolved, t->path)) { |
13c31542 TH |
3481 | l = t; |
3482 | break; | |
3483 | } | |
3484 | } | |
3485 | ||
3486 | if (!l) { | |
9be57249 TH |
3487 | CGroupIOLimitType ttype; |
3488 | ||
13c31542 TH |
3489 | l = new0(CGroupIODeviceLimit, 1); |
3490 | if (!l) | |
3491 | return log_oom(); | |
3492 | ||
c9f620bf | 3493 | l->path = TAKE_PTR(resolved); |
9be57249 TH |
3494 | for (ttype = 0; ttype < _CGROUP_IO_LIMIT_TYPE_MAX; ttype++) |
3495 | l->limits[ttype] = cgroup_io_limit_defaults[ttype]; | |
13c31542 TH |
3496 | |
3497 | LIST_PREPEND(device_limits, c->io_device_limits, l); | |
3498 | } | |
3499 | ||
9be57249 | 3500 | l->limits[type] = num; |
13c31542 TH |
3501 | |
3502 | return 0; | |
3503 | } | |
3504 | ||
8e7076ca LP |
3505 | int config_parse_blockio_device_weight( |
3506 | const char *unit, | |
3507 | const char *filename, | |
3508 | unsigned line, | |
3509 | const char *section, | |
71a61510 | 3510 | unsigned section_line, |
8e7076ca LP |
3511 | const char *lvalue, |
3512 | int ltype, | |
3513 | const char *rvalue, | |
3514 | void *data, | |
3515 | void *userdata) { | |
3516 | ||
c9f620bf | 3517 | _cleanup_free_ char *path = NULL, *resolved = NULL; |
8e7076ca | 3518 | CGroupBlockIODeviceWeight *w; |
4ad49000 | 3519 | CGroupContext *c = data; |
c9f620bf | 3520 | const char *p = rvalue; |
d53d9474 | 3521 | uint64_t u; |
4ad49000 LP |
3522 | int r; |
3523 | ||
3524 | assert(filename); | |
3525 | assert(lvalue); | |
3526 | assert(rvalue); | |
3527 | ||
3528 | if (isempty(rvalue)) { | |
4ad49000 LP |
3529 | while (c->blockio_device_weights) |
3530 | cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights); | |
3531 | ||
3532 | return 0; | |
3533 | } | |
3534 | ||
c9f620bf YW |
3535 | r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES); |
3536 | if (r == -ENOMEM) | |
3537 | return log_oom(); | |
3538 | if (r < 0) { | |
3539 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3540 | "Invalid syntax, ignoring: %s", rvalue); | |
3541 | return 0; | |
3542 | } | |
3543 | if (r == 0 || isempty(p)) { | |
3544 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
3545 | "Failed to extract device node and weight from '%s', ignoring.", rvalue); | |
8e7076ca LP |
3546 | return 0; |
3547 | } | |
4ad49000 | 3548 | |
c9f620bf YW |
3549 | r = unit_full_printf(userdata, path, &resolved); |
3550 | if (r < 0) { | |
3551 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3552 | "Failed to resolve unit specifiers in '%s', ignoring: %m", path); | |
3553 | return 0; | |
3554 | } | |
4ad49000 | 3555 | |
2f4d31c1 YW |
3556 | r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue); |
3557 | if (r < 0) | |
3558 | return 0; | |
3559 | ||
c9f620bf | 3560 | r = cg_blkio_weight_parse(p, &u); |
d53d9474 | 3561 | if (r < 0) { |
c9f620bf | 3562 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid block IO weight '%s', ignoring: %m", p); |
4ad49000 LP |
3563 | return 0; |
3564 | } | |
3565 | ||
d53d9474 LP |
3566 | assert(u != CGROUP_BLKIO_WEIGHT_INVALID); |
3567 | ||
8e7076ca LP |
3568 | w = new0(CGroupBlockIODeviceWeight, 1); |
3569 | if (!w) | |
3570 | return log_oom(); | |
4ad49000 | 3571 | |
c9f620bf | 3572 | w->path = TAKE_PTR(resolved); |
d53d9474 | 3573 | w->weight = u; |
4ad49000 | 3574 | |
71fda00f | 3575 | LIST_PREPEND(device_weights, c->blockio_device_weights, w); |
4ad49000 LP |
3576 | return 0; |
3577 | } | |
3578 | ||
3579 | int config_parse_blockio_bandwidth( | |
3580 | const char *unit, | |
3581 | const char *filename, | |
3582 | unsigned line, | |
3583 | const char *section, | |
71a61510 | 3584 | unsigned section_line, |
4ad49000 LP |
3585 | const char *lvalue, |
3586 | int ltype, | |
3587 | const char *rvalue, | |
3588 | void *data, | |
3589 | void *userdata) { | |
3590 | ||
c9f620bf | 3591 | _cleanup_free_ char *path = NULL, *resolved = NULL; |
979d0311 | 3592 | CGroupBlockIODeviceBandwidth *b = NULL, *t; |
4ad49000 | 3593 | CGroupContext *c = data; |
c9f620bf | 3594 | const char *p = rvalue; |
59f448cf | 3595 | uint64_t bytes; |
47c0980d | 3596 | bool read; |
4ad49000 LP |
3597 | int r; |
3598 | ||
3599 | assert(filename); | |
3600 | assert(lvalue); | |
3601 | assert(rvalue); | |
3602 | ||
47c0980d G |
3603 | read = streq("BlockIOReadBandwidth", lvalue); |
3604 | ||
4ad49000 | 3605 | if (isempty(rvalue)) { |
979d0311 TH |
3606 | LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) { |
3607 | b->rbps = CGROUP_LIMIT_MAX; | |
3608 | b->wbps = CGROUP_LIMIT_MAX; | |
3609 | } | |
4ad49000 LP |
3610 | return 0; |
3611 | } | |
3612 | ||
c9f620bf YW |
3613 | r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES); |
3614 | if (r == -ENOMEM) | |
3615 | return log_oom(); | |
3616 | if (r < 0) { | |
3617 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3618 | "Invalid syntax, ignoring: %s", rvalue); | |
3619 | return 0; | |
3620 | } | |
3621 | if (r == 0 || isempty(p)) { | |
3622 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
3623 | "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue); | |
4ad49000 LP |
3624 | return 0; |
3625 | } | |
3626 | ||
c9f620bf YW |
3627 | r = unit_full_printf(userdata, path, &resolved); |
3628 | if (r < 0) { | |
3629 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3630 | "Failed to resolve unit specifiers in '%s', ignoring: %m", path); | |
3631 | return 0; | |
3632 | } | |
4ad49000 | 3633 | |
2f4d31c1 YW |
3634 | r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue); |
3635 | if (r < 0) | |
3636 | return 0; | |
3637 | ||
c9f620bf | 3638 | r = parse_size(p, 1000, &bytes); |
4ad49000 | 3639 | if (r < 0 || bytes <= 0) { |
c9f620bf | 3640 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid Block IO Bandwidth '%s', ignoring.", p); |
4ad49000 LP |
3641 | return 0; |
3642 | } | |
3643 | ||
979d0311 | 3644 | LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths) { |
c9f620bf | 3645 | if (path_equal(resolved, t->path)) { |
979d0311 TH |
3646 | b = t; |
3647 | break; | |
3648 | } | |
3649 | } | |
4ad49000 | 3650 | |
979d0311 TH |
3651 | if (!t) { |
3652 | b = new0(CGroupBlockIODeviceBandwidth, 1); | |
3653 | if (!b) | |
3654 | return log_oom(); | |
3655 | ||
c9f620bf | 3656 | b->path = TAKE_PTR(resolved); |
979d0311 TH |
3657 | b->rbps = CGROUP_LIMIT_MAX; |
3658 | b->wbps = CGROUP_LIMIT_MAX; | |
3659 | ||
3660 | LIST_PREPEND(device_bandwidths, c->blockio_device_bandwidths, b); | |
3661 | } | |
4ad49000 | 3662 | |
979d0311 TH |
3663 | if (read) |
3664 | b->rbps = bytes; | |
3665 | else | |
3666 | b->wbps = bytes; | |
4ad49000 LP |
3667 | |
3668 | return 0; | |
3669 | } | |
3670 | ||
d420282b LP |
3671 | int config_parse_job_mode_isolate( |
3672 | const char *unit, | |
3673 | const char *filename, | |
3674 | unsigned line, | |
3675 | const char *section, | |
3676 | unsigned section_line, | |
3677 | const char *lvalue, | |
3678 | int ltype, | |
3679 | const char *rvalue, | |
3680 | void *data, | |
3681 | void *userdata) { | |
3682 | ||
3683 | JobMode *m = data; | |
3684 | int r; | |
3685 | ||
3686 | assert(filename); | |
3687 | assert(lvalue); | |
3688 | assert(rvalue); | |
3689 | ||
3690 | r = parse_boolean(rvalue); | |
3691 | if (r < 0) { | |
12ca818f | 3692 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse boolean, ignoring: %s", rvalue); |
d420282b LP |
3693 | return 0; |
3694 | } | |
3695 | ||
8ab39347 YW |
3696 | log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue); |
3697 | ||
d420282b LP |
3698 | *m = r ? JOB_ISOLATE : JOB_REPLACE; |
3699 | return 0; | |
3700 | } | |
3701 | ||
3536f49e | 3702 | int config_parse_exec_directories( |
e66cf1a3 LP |
3703 | const char *unit, |
3704 | const char *filename, | |
3705 | unsigned line, | |
3706 | const char *section, | |
3707 | unsigned section_line, | |
3708 | const char *lvalue, | |
3709 | int ltype, | |
3710 | const char *rvalue, | |
3711 | void *data, | |
3712 | void *userdata) { | |
3713 | ||
a2a5291b | 3714 | char***rt = data; |
9b5864d9 | 3715 | Unit *u = userdata; |
035fe294 | 3716 | const char *p; |
e66cf1a3 LP |
3717 | int r; |
3718 | ||
3719 | assert(filename); | |
3720 | assert(lvalue); | |
3721 | assert(rvalue); | |
3722 | assert(data); | |
3723 | ||
3724 | if (isempty(rvalue)) { | |
3725 | /* Empty assignment resets the list */ | |
6796073e | 3726 | *rt = strv_free(*rt); |
e66cf1a3 LP |
3727 | return 0; |
3728 | } | |
3729 | ||
035fe294 ZJS |
3730 | for (p = rvalue;;) { |
3731 | _cleanup_free_ char *word = NULL, *k = NULL; | |
e66cf1a3 | 3732 | |
035fe294 | 3733 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
035fe294 | 3734 | if (r == -ENOMEM) |
e66cf1a3 | 3735 | return log_oom(); |
035fe294 ZJS |
3736 | if (r < 0) { |
3737 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3738 | "Invalid syntax, ignoring: %s", rvalue); | |
3739 | return 0; | |
3740 | } | |
091e9efe LP |
3741 | if (r == 0) |
3742 | return 0; | |
e66cf1a3 | 3743 | |
18913df9 | 3744 | r = unit_full_printf(u, word, &k); |
9b5864d9 | 3745 | if (r < 0) { |
035fe294 | 3746 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a | 3747 | "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word); |
9b5864d9 MG |
3748 | continue; |
3749 | } | |
3750 | ||
2f4d31c1 YW |
3751 | r = path_simplify_and_warn(k, PATH_CHECK_RELATIVE, unit, filename, line, lvalue); |
3752 | if (r < 0) | |
e8865688 | 3753 | continue; |
e8865688 LP |
3754 | |
3755 | if (path_startswith(k, "private")) { | |
3756 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
2f4d31c1 | 3757 | "%s= path can't be 'private', ingoring assignment: %s", lvalue, word); |
e66cf1a3 LP |
3758 | continue; |
3759 | } | |
3760 | ||
035fe294 | 3761 | r = strv_push(rt, k); |
e66cf1a3 LP |
3762 | if (r < 0) |
3763 | return log_oom(); | |
035fe294 | 3764 | k = NULL; |
e66cf1a3 | 3765 | } |
e66cf1a3 LP |
3766 | } |
3767 | ||
3af00fb8 LP |
3768 | int config_parse_set_status( |
3769 | const char *unit, | |
3770 | const char *filename, | |
3771 | unsigned line, | |
3772 | const char *section, | |
3773 | unsigned section_line, | |
3774 | const char *lvalue, | |
3775 | int ltype, | |
3776 | const char *rvalue, | |
3777 | void *data, | |
3778 | void *userdata) { | |
3779 | ||
3af00fb8 | 3780 | size_t l; |
a2a5291b | 3781 | const char *word, *state; |
3af00fb8 LP |
3782 | int r; |
3783 | ExitStatusSet *status_set = data; | |
3784 | ||
3785 | assert(filename); | |
3786 | assert(lvalue); | |
3787 | assert(rvalue); | |
3788 | assert(data); | |
3789 | ||
3e2d435b | 3790 | /* Empty assignment resets the list */ |
3af00fb8 | 3791 | if (isempty(rvalue)) { |
3e2d435b | 3792 | exit_status_set_free(status_set); |
3af00fb8 LP |
3793 | return 0; |
3794 | } | |
3795 | ||
a2a5291b | 3796 | FOREACH_WORD(word, l, rvalue, state) { |
3af00fb8 LP |
3797 | _cleanup_free_ char *temp; |
3798 | int val; | |
61593865 | 3799 | Set **set; |
3af00fb8 | 3800 | |
a2a5291b | 3801 | temp = strndup(word, l); |
3af00fb8 LP |
3802 | if (!temp) |
3803 | return log_oom(); | |
3804 | ||
3805 | r = safe_atoi(temp, &val); | |
3806 | if (r < 0) { | |
29a3db75 | 3807 | val = signal_from_string(temp); |
3af00fb8 | 3808 | |
1e2fd62d | 3809 | if (val <= 0) { |
12ca818f | 3810 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse value, ignoring: %s", word); |
61593865 | 3811 | continue; |
3af00fb8 | 3812 | } |
61593865 | 3813 | set = &status_set->signal; |
3af00fb8 | 3814 | } else { |
1e2fd62d | 3815 | if (val < 0 || val > 255) { |
12ca818f | 3816 | log_syntax(unit, LOG_ERR, filename, line, 0, "Value %d is outside range 0-255, ignoring", val); |
1e2fd62d | 3817 | continue; |
3af00fb8 | 3818 | } |
61593865 | 3819 | set = &status_set->status; |
3af00fb8 | 3820 | } |
1e2fd62d | 3821 | |
61593865 | 3822 | r = set_ensure_allocated(set, NULL); |
1e2fd62d ZJS |
3823 | if (r < 0) |
3824 | return log_oom(); | |
3825 | ||
61593865 | 3826 | r = set_put(*set, INT_TO_PTR(val)); |
063c4b1a YW |
3827 | if (r < 0) |
3828 | return log_oom(); | |
3af00fb8 | 3829 | } |
b2fadec6 | 3830 | if (!isempty(state)) |
12ca818f | 3831 | log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring."); |
3af00fb8 LP |
3832 | |
3833 | return 0; | |
3834 | } | |
3835 | ||
94828d2d LP |
3836 | int config_parse_namespace_path_strv( |
3837 | const char *unit, | |
3838 | const char *filename, | |
3839 | unsigned line, | |
3840 | const char *section, | |
3841 | unsigned section_line, | |
3842 | const char *lvalue, | |
3843 | int ltype, | |
3844 | const char *rvalue, | |
3845 | void *data, | |
3846 | void *userdata) { | |
3847 | ||
7b07e993 | 3848 | Unit *u = userdata; |
a2a5291b | 3849 | char*** sv = data; |
063c4b1a | 3850 | const char *p = rvalue; |
94828d2d LP |
3851 | int r; |
3852 | ||
3853 | assert(filename); | |
3854 | assert(lvalue); | |
3855 | assert(rvalue); | |
3856 | assert(data); | |
3857 | ||
3858 | if (isempty(rvalue)) { | |
3859 | /* Empty assignment resets the list */ | |
6796073e | 3860 | *sv = strv_free(*sv); |
94828d2d LP |
3861 | return 0; |
3862 | } | |
3863 | ||
727f76d7 | 3864 | for (;;) { |
7b07e993 | 3865 | _cleanup_free_ char *word = NULL, *resolved = NULL, *joined = NULL; |
20b7a007 LP |
3866 | const char *w; |
3867 | bool ignore_enoent = false, shall_prefix = false; | |
94828d2d | 3868 | |
063c4b1a | 3869 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
0293a7a8 EV |
3870 | if (r == 0) |
3871 | break; | |
3872 | if (r == -ENOMEM) | |
3873 | return log_oom(); | |
727f76d7 | 3874 | if (r < 0) { |
7b07e993 | 3875 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue); |
727f76d7 EV |
3876 | return 0; |
3877 | } | |
94828d2d | 3878 | |
20b7a007 LP |
3879 | w = word; |
3880 | if (startswith(w, "-")) { | |
3881 | ignore_enoent = true; | |
3882 | w++; | |
3883 | } | |
3884 | if (startswith(w, "+")) { | |
3885 | shall_prefix = true; | |
3886 | w++; | |
3887 | } | |
7b07e993 | 3888 | |
20b7a007 | 3889 | r = unit_full_printf(u, w, &resolved); |
7b07e993 | 3890 | if (r < 0) { |
063c4b1a | 3891 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", w); |
94828d2d LP |
3892 | continue; |
3893 | } | |
3894 | ||
2f4d31c1 YW |
3895 | r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
3896 | if (r < 0) | |
7b07e993 | 3897 | continue; |
94828d2d | 3898 | |
20b7a007 LP |
3899 | joined = strjoin(ignore_enoent ? "-" : "", |
3900 | shall_prefix ? "+" : "", | |
3901 | resolved); | |
7b07e993 LP |
3902 | |
3903 | r = strv_push(sv, joined); | |
94828d2d LP |
3904 | if (r < 0) |
3905 | return log_oom(); | |
3906 | ||
7b07e993 | 3907 | joined = NULL; |
94828d2d LP |
3908 | } |
3909 | ||
3910 | return 0; | |
3911 | } | |
3912 | ||
2abd4e38 YW |
3913 | int config_parse_temporary_filesystems( |
3914 | const char *unit, | |
3915 | const char *filename, | |
3916 | unsigned line, | |
3917 | const char *section, | |
3918 | unsigned section_line, | |
3919 | const char *lvalue, | |
3920 | int ltype, | |
3921 | const char *rvalue, | |
3922 | void *data, | |
3923 | void *userdata) { | |
3924 | ||
3925 | Unit *u = userdata; | |
3926 | ExecContext *c = data; | |
063c4b1a | 3927 | const char *p = rvalue; |
2abd4e38 YW |
3928 | int r; |
3929 | ||
3930 | assert(filename); | |
3931 | assert(lvalue); | |
3932 | assert(rvalue); | |
3933 | assert(data); | |
3934 | ||
3935 | if (isempty(rvalue)) { | |
3936 | /* Empty assignment resets the list */ | |
3937 | temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems); | |
3938 | c->temporary_filesystems = NULL; | |
3939 | c->n_temporary_filesystems = 0; | |
3940 | return 0; | |
3941 | } | |
3942 | ||
2abd4e38 YW |
3943 | for (;;) { |
3944 | _cleanup_free_ char *word = NULL, *path = NULL, *resolved = NULL; | |
3945 | const char *w; | |
3946 | ||
063c4b1a | 3947 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
2abd4e38 | 3948 | if (r == 0) |
063c4b1a | 3949 | return 0; |
2abd4e38 YW |
3950 | if (r == -ENOMEM) |
3951 | return log_oom(); | |
3952 | if (r < 0) { | |
3953 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue); | |
3954 | return 0; | |
3955 | } | |
3956 | ||
3957 | w = word; | |
3958 | r = extract_first_word(&w, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS); | |
063c4b1a YW |
3959 | if (r == -ENOMEM) |
3960 | return log_oom(); | |
3961 | if (r < 0) { | |
3962 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", word); | |
3963 | continue; | |
3964 | } | |
3965 | if (r == 0) { | |
3966 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid syntax, ignoring: %s", word); | |
3967 | continue; | |
3968 | } | |
2abd4e38 YW |
3969 | |
3970 | r = unit_full_printf(u, path, &resolved); | |
3971 | if (r < 0) { | |
063c4b1a | 3972 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", path); |
2abd4e38 YW |
3973 | continue; |
3974 | } | |
3975 | ||
2f4d31c1 YW |
3976 | r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
3977 | if (r < 0) | |
2abd4e38 | 3978 | continue; |
2abd4e38 | 3979 | |
a26fec24 | 3980 | r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, resolved, w); |
6302d1ea | 3981 | if (r < 0) |
2abd4e38 | 3982 | return log_oom(); |
2abd4e38 | 3983 | } |
2abd4e38 YW |
3984 | } |
3985 | ||
d2d6c096 LP |
3986 | int config_parse_bind_paths( |
3987 | const char *unit, | |
3988 | const char *filename, | |
3989 | unsigned line, | |
3990 | const char *section, | |
3991 | unsigned section_line, | |
3992 | const char *lvalue, | |
3993 | int ltype, | |
3994 | const char *rvalue, | |
3995 | void *data, | |
3996 | void *userdata) { | |
3997 | ||
3998 | ExecContext *c = data; | |
42d43f21 | 3999 | Unit *u = userdata; |
d2d6c096 LP |
4000 | const char *p; |
4001 | int r; | |
4002 | ||
4003 | assert(filename); | |
4004 | assert(lvalue); | |
4005 | assert(rvalue); | |
4006 | assert(data); | |
4007 | ||
4008 | if (isempty(rvalue)) { | |
4009 | /* Empty assignment resets the list */ | |
4010 | bind_mount_free_many(c->bind_mounts, c->n_bind_mounts); | |
4011 | c->bind_mounts = NULL; | |
4012 | c->n_bind_mounts = 0; | |
4013 | return 0; | |
4014 | } | |
4015 | ||
4016 | p = rvalue; | |
4017 | for (;;) { | |
4018 | _cleanup_free_ char *source = NULL, *destination = NULL; | |
42d43f21 | 4019 | _cleanup_free_ char *sresolved = NULL, *dresolved = NULL; |
d2d6c096 LP |
4020 | char *s = NULL, *d = NULL; |
4021 | bool rbind = true, ignore_enoent = false; | |
4022 | ||
4023 | r = extract_first_word(&p, &source, ":" WHITESPACE, EXTRACT_QUOTES|EXTRACT_DONT_COALESCE_SEPARATORS); | |
4024 | if (r == 0) | |
4025 | break; | |
4026 | if (r == -ENOMEM) | |
4027 | return log_oom(); | |
4028 | if (r < 0) { | |
2f4d31c1 | 4029 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue); |
d2d6c096 LP |
4030 | return 0; |
4031 | } | |
4032 | ||
42d43f21 DC |
4033 | r = unit_full_printf(u, source, &sresolved); |
4034 | if (r < 0) { | |
4035 | log_syntax(unit, LOG_ERR, filename, line, r, | |
063c4b1a | 4036 | "Failed to resolved unit specifiers in \"%s\", ignoring: %m", source); |
2f4d31c1 | 4037 | continue; |
42d43f21 DC |
4038 | } |
4039 | ||
4040 | s = sresolved; | |
d2d6c096 LP |
4041 | if (s[0] == '-') { |
4042 | ignore_enoent = true; | |
4043 | s++; | |
4044 | } | |
4045 | ||
2f4d31c1 YW |
4046 | r = path_simplify_and_warn(s, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
4047 | if (r < 0) | |
4048 | continue; | |
d2d6c096 LP |
4049 | |
4050 | /* Optionally, the destination is specified. */ | |
4051 | if (p && p[-1] == ':') { | |
4052 | r = extract_first_word(&p, &destination, ":" WHITESPACE, EXTRACT_QUOTES|EXTRACT_DONT_COALESCE_SEPARATORS); | |
4053 | if (r == -ENOMEM) | |
4054 | return log_oom(); | |
4055 | if (r < 0) { | |
2f4d31c1 | 4056 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue); |
d2d6c096 LP |
4057 | return 0; |
4058 | } | |
4059 | if (r == 0) { | |
2f4d31c1 YW |
4060 | log_syntax(unit, LOG_ERR, filename, line, 0, "Missing argument after ':', ignoring: %s", s); |
4061 | continue; | |
d2d6c096 LP |
4062 | } |
4063 | ||
42d43f21 DC |
4064 | r = unit_full_printf(u, destination, &dresolved); |
4065 | if (r < 0) { | |
4066 | log_syntax(unit, LOG_ERR, filename, line, r, | |
4067 | "Failed to resolved specifiers in \"%s\", ignoring: %m", destination); | |
2f4d31c1 | 4068 | continue; |
42d43f21 DC |
4069 | } |
4070 | ||
2f4d31c1 YW |
4071 | r = path_simplify_and_warn(dresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
4072 | if (r < 0) | |
4073 | continue; | |
d2d6c096 | 4074 | |
2f4d31c1 | 4075 | d = dresolved; |
d2d6c096 LP |
4076 | |
4077 | /* Optionally, there's also a short option string specified */ | |
4078 | if (p && p[-1] == ':') { | |
4079 | _cleanup_free_ char *options = NULL; | |
4080 | ||
4081 | r = extract_first_word(&p, &options, NULL, EXTRACT_QUOTES); | |
4082 | if (r == -ENOMEM) | |
4083 | return log_oom(); | |
4084 | if (r < 0) { | |
4085 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s: %s", lvalue, rvalue); | |
4086 | return 0; | |
4087 | } | |
4088 | ||
4089 | if (isempty(options) || streq(options, "rbind")) | |
4090 | rbind = true; | |
4091 | else if (streq(options, "norbind")) | |
4092 | rbind = false; | |
4093 | else { | |
4094 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid option string, ignoring setting: %s", options); | |
2f4d31c1 | 4095 | continue; |
d2d6c096 LP |
4096 | } |
4097 | } | |
4098 | } else | |
4099 | d = s; | |
4100 | ||
4101 | r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts, | |
4102 | &(BindMount) { | |
4103 | .source = s, | |
4104 | .destination = d, | |
4105 | .read_only = !!strstr(lvalue, "ReadOnly"), | |
4106 | .recursive = rbind, | |
4107 | .ignore_enoent = ignore_enoent, | |
4108 | }); | |
4109 | if (r < 0) | |
4110 | return log_oom(); | |
4111 | } | |
4112 | ||
4113 | return 0; | |
4114 | } | |
4115 | ||
eae51da3 LP |
4116 | int config_parse_job_timeout_sec( |
4117 | const char* unit, | |
4118 | const char *filename, | |
4119 | unsigned line, | |
4120 | const char *section, | |
4121 | unsigned section_line, | |
4122 | const char *lvalue, | |
4123 | int ltype, | |
4124 | const char *rvalue, | |
4125 | void *data, | |
4126 | void *userdata) { | |
4127 | ||
4128 | Unit *u = data; | |
4129 | usec_t usec; | |
4130 | int r; | |
4131 | ||
4132 | assert(filename); | |
4133 | assert(lvalue); | |
4134 | assert(rvalue); | |
4135 | assert(u); | |
4136 | ||
4137 | r = parse_sec_fix_0(rvalue, &usec); | |
4138 | if (r < 0) { | |
4139 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue); | |
4140 | return 0; | |
4141 | } | |
4142 | ||
4143 | /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old | |
c05f3c8f | 4144 | * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should |
eae51da3 LP |
4145 | * count. */ |
4146 | ||
4147 | if (!u->job_running_timeout_set) | |
4148 | u->job_running_timeout = usec; | |
4149 | ||
4150 | u->job_timeout = usec; | |
4151 | ||
4152 | return 0; | |
4153 | } | |
4154 | ||
4155 | int config_parse_job_running_timeout_sec( | |
4156 | const char* unit, | |
4157 | const char *filename, | |
4158 | unsigned line, | |
4159 | const char *section, | |
4160 | unsigned section_line, | |
4161 | const char *lvalue, | |
4162 | int ltype, | |
4163 | const char *rvalue, | |
4164 | void *data, | |
4165 | void *userdata) { | |
4166 | ||
4167 | Unit *u = data; | |
4168 | usec_t usec; | |
4169 | int r; | |
4170 | ||
4171 | assert(filename); | |
4172 | assert(lvalue); | |
4173 | assert(rvalue); | |
4174 | assert(u); | |
4175 | ||
4176 | r = parse_sec_fix_0(rvalue, &usec); | |
4177 | if (r < 0) { | |
4178 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue); | |
4179 | return 0; | |
4180 | } | |
4181 | ||
4182 | u->job_running_timeout = usec; | |
4183 | u->job_running_timeout_set = true; | |
4184 | ||
4185 | return 0; | |
4186 | } | |
4187 | ||
071830ff | 4188 | #define FOLLOW_MAX 8 |
87f0e418 | 4189 | |
9e2f7c11 | 4190 | static int open_follow(char **filename, FILE **_f, Set *names, char **_final) { |
a837f088 | 4191 | char *id = NULL; |
0301abf4 | 4192 | unsigned c = 0; |
87f0e418 LP |
4193 | int fd, r; |
4194 | FILE *f; | |
87f0e418 LP |
4195 | |
4196 | assert(filename); | |
4197 | assert(*filename); | |
4198 | assert(_f); | |
4199 | assert(names); | |
4200 | ||
0301abf4 LP |
4201 | /* This will update the filename pointer if the loaded file is |
4202 | * reached by a symlink. The old string will be freed. */ | |
87f0e418 | 4203 | |
0301abf4 | 4204 | for (;;) { |
2c7108c4 | 4205 | char *target, *name; |
87f0e418 | 4206 | |
0301abf4 LP |
4207 | if (c++ >= FOLLOW_MAX) |
4208 | return -ELOOP; | |
4209 | ||
858d36c1 | 4210 | path_simplify(*filename, false); |
b08d03ff | 4211 | |
87f0e418 | 4212 | /* Add the file name we are currently looking at to |
8f05424d LP |
4213 | * the names of this unit, but only if it is a valid |
4214 | * unit name. */ | |
2b6bf07d | 4215 | name = basename(*filename); |
7410616c | 4216 | if (unit_name_is_valid(name, UNIT_NAME_ANY)) { |
8f05424d | 4217 | |
15e11d81 LP |
4218 | id = set_get(names, name); |
4219 | if (!id) { | |
4220 | id = strdup(name); | |
4221 | if (!id) | |
8f05424d | 4222 | return -ENOMEM; |
87f0e418 | 4223 | |
ef42202a ZJS |
4224 | r = set_consume(names, id); |
4225 | if (r < 0) | |
8f05424d | 4226 | return r; |
87f0e418 | 4227 | } |
87f0e418 LP |
4228 | } |
4229 | ||
0301abf4 | 4230 | /* Try to open the file name, but don't if its a symlink */ |
9946996c LP |
4231 | fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW); |
4232 | if (fd >= 0) | |
87f0e418 LP |
4233 | break; |
4234 | ||
0301abf4 LP |
4235 | if (errno != ELOOP) |
4236 | return -errno; | |
4237 | ||
87f0e418 | 4238 | /* Hmm, so this is a symlink. Let's read the name, and follow it manually */ |
9946996c LP |
4239 | r = readlink_and_make_absolute(*filename, &target); |
4240 | if (r < 0) | |
0301abf4 | 4241 | return r; |
87f0e418 | 4242 | |
17ee3d72 | 4243 | free_and_replace(*filename, target); |
87f0e418 LP |
4244 | } |
4245 | ||
9946996c LP |
4246 | f = fdopen(fd, "re"); |
4247 | if (!f) { | |
03e334a1 | 4248 | safe_close(fd); |
d4ad27a1 | 4249 | return -errno; |
87f0e418 LP |
4250 | } |
4251 | ||
4252 | *_f = f; | |
9e2f7c11 | 4253 | *_final = id; |
a837f088 | 4254 | |
0301abf4 | 4255 | return 0; |
87f0e418 LP |
4256 | } |
4257 | ||
23a177ef LP |
4258 | static int merge_by_names(Unit **u, Set *names, const char *id) { |
4259 | char *k; | |
4260 | int r; | |
4261 | ||
4262 | assert(u); | |
4263 | assert(*u); | |
4264 | assert(names); | |
4265 | ||
4266 | /* Let's try to add in all symlink names we found */ | |
4267 | while ((k = set_steal_first(names))) { | |
4268 | ||
4269 | /* First try to merge in the other name into our | |
4270 | * unit */ | |
9946996c LP |
4271 | r = unit_merge_by_name(*u, k); |
4272 | if (r < 0) { | |
23a177ef LP |
4273 | Unit *other; |
4274 | ||
4275 | /* Hmm, we couldn't merge the other unit into | |
4276 | * ours? Then let's try it the other way | |
4277 | * round */ | |
4278 | ||
7aad67e7 MS |
4279 | /* If the symlink name we are looking at is unit template, then |
4280 | we must search for instance of this template */ | |
9d3e3406 | 4281 | if (unit_name_is_valid(k, UNIT_NAME_TEMPLATE) && (*u)->instance) { |
7aad67e7 MS |
4282 | _cleanup_free_ char *instance = NULL; |
4283 | ||
4284 | r = unit_name_replace_instance(k, (*u)->instance, &instance); | |
4285 | if (r < 0) | |
4286 | return r; | |
4287 | ||
4288 | other = manager_get_unit((*u)->manager, instance); | |
4289 | } else | |
4290 | other = manager_get_unit((*u)->manager, k); | |
4291 | ||
23a177ef LP |
4292 | free(k); |
4293 | ||
9946996c LP |
4294 | if (other) { |
4295 | r = unit_merge(other, *u); | |
4296 | if (r >= 0) { | |
23a177ef LP |
4297 | *u = other; |
4298 | return merge_by_names(u, names, NULL); | |
4299 | } | |
9946996c | 4300 | } |
23a177ef LP |
4301 | |
4302 | return r; | |
4303 | } | |
4304 | ||
4305 | if (id == k) | |
4306 | unit_choose_id(*u, id); | |
4307 | ||
4308 | free(k); | |
4309 | } | |
4310 | ||
4311 | return 0; | |
4312 | } | |
4313 | ||
e537352b | 4314 | static int load_from_path(Unit *u, const char *path) { |
e48614c4 ZJS |
4315 | _cleanup_set_free_free_ Set *symlink_names = NULL; |
4316 | _cleanup_fclose_ FILE *f = NULL; | |
4317 | _cleanup_free_ char *filename = NULL; | |
4318 | char *id = NULL; | |
23a177ef | 4319 | Unit *merged; |
45fb0699 | 4320 | struct stat st; |
a837f088 | 4321 | int r; |
23a177ef LP |
4322 | |
4323 | assert(u); | |
e537352b | 4324 | assert(path); |
3efd4195 | 4325 | |
d5099efc | 4326 | symlink_names = set_new(&string_hash_ops); |
f975e971 | 4327 | if (!symlink_names) |
87f0e418 | 4328 | return -ENOMEM; |
3efd4195 | 4329 | |
036643a2 LP |
4330 | if (path_is_absolute(path)) { |
4331 | ||
9946996c | 4332 | filename = strdup(path); |
e48614c4 ZJS |
4333 | if (!filename) |
4334 | return -ENOMEM; | |
036643a2 | 4335 | |
9946996c LP |
4336 | r = open_follow(&filename, &f, symlink_names, &id); |
4337 | if (r < 0) { | |
97b11eed | 4338 | filename = mfree(filename); |
036643a2 | 4339 | if (r != -ENOENT) |
e48614c4 | 4340 | return r; |
036643a2 LP |
4341 | } |
4342 | ||
4343 | } else { | |
4344 | char **p; | |
4345 | ||
a3c4eb07 | 4346 | STRV_FOREACH(p, u->manager->lookup_paths.search_path) { |
036643a2 LP |
4347 | |
4348 | /* Instead of opening the path right away, we manually | |
4349 | * follow all symlinks and add their name to our unit | |
4350 | * name set while doing so */ | |
9946996c | 4351 | filename = path_make_absolute(path, *p); |
e48614c4 ZJS |
4352 | if (!filename) |
4353 | return -ENOMEM; | |
036643a2 | 4354 | |
ac155bb8 MS |
4355 | if (u->manager->unit_path_cache && |
4356 | !set_get(u->manager->unit_path_cache, filename)) | |
fe51822e LP |
4357 | r = -ENOENT; |
4358 | else | |
4359 | r = open_follow(&filename, &f, symlink_names, &id); | |
a837f088 LP |
4360 | if (r >= 0) |
4361 | break; | |
4362 | filename = mfree(filename); | |
a1feacf7 ZJS |
4363 | |
4364 | /* ENOENT means that the file is missing or is a dangling symlink. | |
4365 | * ENOTDIR means that one of paths we expect to be is a directory | |
4366 | * is not a directory, we should just ignore that. | |
4367 | * EACCES means that the directory or file permissions are wrong. | |
4368 | */ | |
4369 | if (r == -EACCES) | |
4370 | log_debug_errno(r, "Cannot access \"%s\": %m", filename); | |
4371 | else if (!IN_SET(r, -ENOENT, -ENOTDIR)) | |
a837f088 | 4372 | return r; |
fe51822e | 4373 | |
a837f088 LP |
4374 | /* Empty the symlink names for the next run */ |
4375 | set_clear_free(symlink_names); | |
036643a2 LP |
4376 | } |
4377 | } | |
034c6ed7 | 4378 | |
e48614c4 | 4379 | if (!filename) |
8f05424d | 4380 | /* Hmm, no suitable file found? */ |
e48614c4 | 4381 | return 0; |
87f0e418 | 4382 | |
8a993b61 | 4383 | if (!unit_type_may_alias(u->type) && set_size(symlink_names) > 1) { |
a837f088 LP |
4384 | log_unit_warning(u, "Unit type of %s does not support alias names, refusing loading via symlink.", u->id); |
4385 | return -ELOOP; | |
4386 | } | |
4387 | ||
23a177ef | 4388 | merged = u; |
9946996c LP |
4389 | r = merge_by_names(&merged, symlink_names, id); |
4390 | if (r < 0) | |
e48614c4 | 4391 | return r; |
87f0e418 | 4392 | |
23a177ef | 4393 | if (merged != u) { |
ac155bb8 | 4394 | u->load_state = UNIT_MERGED; |
e48614c4 | 4395 | return 0; |
034c6ed7 LP |
4396 | } |
4397 | ||
e48614c4 ZJS |
4398 | if (fstat(fileno(f), &st) < 0) |
4399 | return -errno; | |
45fb0699 | 4400 | |
3a8db9fe | 4401 | if (null_or_empty(&st)) { |
ac155bb8 | 4402 | u->load_state = UNIT_MASKED; |
3a8db9fe ZJS |
4403 | u->fragment_mtime = 0; |
4404 | } else { | |
c2756a68 | 4405 | u->load_state = UNIT_LOADED; |
3a8db9fe | 4406 | u->fragment_mtime = timespec_load(&st.st_mtim); |
c2756a68 | 4407 | |
00dc5d76 | 4408 | /* Now, parse the file contents */ |
36f822c4 ZJS |
4409 | r = config_parse(u->id, filename, f, |
4410 | UNIT_VTABLE(u)->sections, | |
4411 | config_item_perf_lookup, load_fragment_gperf_lookup, | |
bcde742e | 4412 | CONFIG_PARSE_ALLOW_INCLUDE, u); |
f975e971 | 4413 | if (r < 0) |
e48614c4 | 4414 | return r; |
00dc5d76 | 4415 | } |
b08d03ff | 4416 | |
1cc6c93a | 4417 | free_and_replace(u->fragment_path, filename); |
87f0e418 | 4418 | |
1b64d026 LP |
4419 | if (u->source_path) { |
4420 | if (stat(u->source_path, &st) >= 0) | |
4421 | u->source_mtime = timespec_load(&st.st_mtim); | |
4422 | else | |
4423 | u->source_mtime = 0; | |
4424 | } | |
4425 | ||
e48614c4 | 4426 | return 0; |
0301abf4 LP |
4427 | } |
4428 | ||
e537352b | 4429 | int unit_load_fragment(Unit *u) { |
23a177ef | 4430 | int r; |
294d81f1 LP |
4431 | Iterator i; |
4432 | const char *t; | |
0301abf4 LP |
4433 | |
4434 | assert(u); | |
ac155bb8 MS |
4435 | assert(u->load_state == UNIT_STUB); |
4436 | assert(u->id); | |
23a177ef | 4437 | |
3f5e8115 LP |
4438 | if (u->transient) { |
4439 | u->load_state = UNIT_LOADED; | |
4440 | return 0; | |
4441 | } | |
4442 | ||
294d81f1 LP |
4443 | /* First, try to find the unit under its id. We always look |
4444 | * for unit files in the default directories, to make it easy | |
4445 | * to override things by placing things in /etc/systemd/system */ | |
9946996c LP |
4446 | r = load_from_path(u, u->id); |
4447 | if (r < 0) | |
294d81f1 LP |
4448 | return r; |
4449 | ||
4450 | /* Try to find an alias we can load this with */ | |
abc08d4d | 4451 | if (u->load_state == UNIT_STUB) { |
ac155bb8 | 4452 | SET_FOREACH(t, u->names, i) { |
294d81f1 | 4453 | |
ac155bb8 | 4454 | if (t == u->id) |
294d81f1 LP |
4455 | continue; |
4456 | ||
9946996c LP |
4457 | r = load_from_path(u, t); |
4458 | if (r < 0) | |
294d81f1 LP |
4459 | return r; |
4460 | ||
ac155bb8 | 4461 | if (u->load_state != UNIT_STUB) |
294d81f1 LP |
4462 | break; |
4463 | } | |
abc08d4d | 4464 | } |
23a177ef | 4465 | |
294d81f1 | 4466 | /* And now, try looking for it under the suggested (originally linked) path */ |
ac155bb8 | 4467 | if (u->load_state == UNIT_STUB && u->fragment_path) { |
6ccb1b44 | 4468 | |
9946996c LP |
4469 | r = load_from_path(u, u->fragment_path); |
4470 | if (r < 0) | |
23a177ef | 4471 | return r; |
0301abf4 | 4472 | |
ece174c5 | 4473 | if (u->load_state == UNIT_STUB) |
6ccb1b44 LP |
4474 | /* Hmm, this didn't work? Then let's get rid |
4475 | * of the fragment path stored for us, so that | |
4476 | * we don't point to an invalid location. */ | |
a1e58e8e | 4477 | u->fragment_path = mfree(u->fragment_path); |
6ccb1b44 LP |
4478 | } |
4479 | ||
294d81f1 | 4480 | /* Look for a template */ |
ac155bb8 | 4481 | if (u->load_state == UNIT_STUB && u->instance) { |
7410616c | 4482 | _cleanup_free_ char *k = NULL; |
294d81f1 | 4483 | |
7410616c LP |
4484 | r = unit_name_template(u->id, &k); |
4485 | if (r < 0) | |
4486 | return r; | |
294d81f1 LP |
4487 | |
4488 | r = load_from_path(u, k); | |
bb28e684 ZJS |
4489 | if (r < 0) { |
4490 | if (r == -ENOEXEC) | |
4491 | log_unit_notice(u, "Unit configuration has fatal error, unit will not be started."); | |
9e2f7c11 | 4492 | return r; |
bb28e684 | 4493 | } |
890f434c | 4494 | |
abc08d4d | 4495 | if (u->load_state == UNIT_STUB) { |
ac155bb8 | 4496 | SET_FOREACH(t, u->names, i) { |
bc9fd78c | 4497 | _cleanup_free_ char *z = NULL; |
87f0e418 | 4498 | |
ac155bb8 | 4499 | if (t == u->id) |
23a177ef | 4500 | continue; |
071830ff | 4501 | |
7410616c LP |
4502 | r = unit_name_template(t, &z); |
4503 | if (r < 0) | |
4504 | return r; | |
294d81f1 | 4505 | |
bc9fd78c | 4506 | r = load_from_path(u, z); |
294d81f1 | 4507 | if (r < 0) |
23a177ef | 4508 | return r; |
890f434c | 4509 | |
ac155bb8 | 4510 | if (u->load_state != UNIT_STUB) |
23a177ef LP |
4511 | break; |
4512 | } | |
abc08d4d | 4513 | } |
071830ff LP |
4514 | } |
4515 | ||
23a177ef | 4516 | return 0; |
3efd4195 | 4517 | } |
e537352b LP |
4518 | |
4519 | void unit_dump_config_items(FILE *f) { | |
f975e971 LP |
4520 | static const struct { |
4521 | const ConfigParserCallback callback; | |
4522 | const char *rvalue; | |
4523 | } table[] = { | |
17df7223 | 4524 | { config_parse_warn_compat, "NOTSUPPORTED" }, |
f975e971 LP |
4525 | { config_parse_int, "INTEGER" }, |
4526 | { config_parse_unsigned, "UNSIGNED" }, | |
5556b5fe | 4527 | { config_parse_iec_size, "SIZE" }, |
59f448cf | 4528 | { config_parse_iec_uint64, "SIZE" }, |
5556b5fe | 4529 | { config_parse_si_size, "SIZE" }, |
f975e971 LP |
4530 | { config_parse_bool, "BOOLEAN" }, |
4531 | { config_parse_string, "STRING" }, | |
4532 | { config_parse_path, "PATH" }, | |
4533 | { config_parse_unit_path_printf, "PATH" }, | |
4534 | { config_parse_strv, "STRING [...]" }, | |
4535 | { config_parse_exec_nice, "NICE" }, | |
4536 | { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" }, | |
4537 | { config_parse_exec_io_class, "IOCLASS" }, | |
4538 | { config_parse_exec_io_priority, "IOPRIORITY" }, | |
4539 | { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" }, | |
4540 | { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" }, | |
4541 | { config_parse_exec_cpu_affinity, "CPUAFFINITY" }, | |
4542 | { config_parse_mode, "MODE" }, | |
4543 | { config_parse_unit_env_file, "FILE" }, | |
52c239d7 LB |
4544 | { config_parse_exec_output, "OUTPUT" }, |
4545 | { config_parse_exec_input, "INPUT" }, | |
ca37242e LP |
4546 | { config_parse_log_facility, "FACILITY" }, |
4547 | { config_parse_log_level, "LEVEL" }, | |
f975e971 | 4548 | { config_parse_exec_secure_bits, "SECUREBITS" }, |
a103496c | 4549 | { config_parse_capability_set, "BOUNDINGSET" }, |
4f424df7 | 4550 | { config_parse_rlimit, "LIMIT" }, |
f975e971 | 4551 | { config_parse_unit_deps, "UNIT [...]" }, |
f975e971 LP |
4552 | { config_parse_exec, "PATH [ARGUMENT [...]]" }, |
4553 | { config_parse_service_type, "SERVICETYPE" }, | |
4554 | { config_parse_service_restart, "SERVICERESTART" }, | |
f975e971 | 4555 | { config_parse_kill_mode, "KILLMODE" }, |
f757855e | 4556 | { config_parse_signal, "SIGNAL" }, |
f975e971 LP |
4557 | { config_parse_socket_listen, "SOCKET [...]" }, |
4558 | { config_parse_socket_bind, "SOCKETBIND" }, | |
4559 | { config_parse_socket_bindtodevice, "NETWORKINTERFACE" }, | |
7f602784 | 4560 | { config_parse_sec, "SECONDS" }, |
d88a251b | 4561 | { config_parse_nsec, "NANOSECONDS" }, |
94828d2d | 4562 | { config_parse_namespace_path_strv, "PATH [...]" }, |
d2d6c096 | 4563 | { config_parse_bind_paths, "PATH[:PATH[:OPTIONS]] [...]" }, |
7c8fa05c | 4564 | { config_parse_unit_requires_mounts_for, "PATH [...]" }, |
f975e971 LP |
4565 | { config_parse_exec_mount_flags, "MOUNTFLAG [...]" }, |
4566 | { config_parse_unit_string_printf, "STRING" }, | |
3ecaa09b | 4567 | { config_parse_trigger_unit, "UNIT" }, |
f975e971 | 4568 | { config_parse_timer, "TIMER" }, |
f975e971 | 4569 | { config_parse_path_spec, "PATH" }, |
f975e971 LP |
4570 | { config_parse_notify_access, "ACCESS" }, |
4571 | { config_parse_ip_tos, "TOS" }, | |
4572 | { config_parse_unit_condition_path, "CONDITION" }, | |
4573 | { config_parse_unit_condition_string, "CONDITION" }, | |
4574 | { config_parse_unit_condition_null, "CONDITION" }, | |
a016b922 | 4575 | { config_parse_unit_slice, "SLICE" }, |
7f0386f6 LP |
4576 | { config_parse_documentation, "URL" }, |
4577 | { config_parse_service_timeout, "SECONDS" }, | |
87a47f99 | 4578 | { config_parse_emergency_action, "ACTION" }, |
7f0386f6 LP |
4579 | { config_parse_set_status, "STATUS" }, |
4580 | { config_parse_service_sockets, "SOCKETS" }, | |
7f0386f6 | 4581 | { config_parse_environ, "ENVIRON" }, |
349cc4a5 | 4582 | #if HAVE_SECCOMP |
17df7223 | 4583 | { config_parse_syscall_filter, "SYSCALLS" }, |
6a6751fe | 4584 | { config_parse_syscall_archs, "ARCHS" }, |
17df7223 | 4585 | { config_parse_syscall_errno, "ERRNO" }, |
4298d0b5 | 4586 | { config_parse_address_families, "FAMILIES" }, |
add00535 | 4587 | { config_parse_restrict_namespaces, "NAMESPACES" }, |
c0467cf3 | 4588 | #endif |
7f0386f6 | 4589 | { config_parse_cpu_shares, "SHARES" }, |
984faf29 | 4590 | { config_parse_cg_weight, "WEIGHT" }, |
7f0386f6 LP |
4591 | { config_parse_memory_limit, "LIMIT" }, |
4592 | { config_parse_device_allow, "DEVICE" }, | |
4593 | { config_parse_device_policy, "POLICY" }, | |
13c31542 | 4594 | { config_parse_io_limit, "LIMIT" }, |
13c31542 | 4595 | { config_parse_io_device_weight, "DEVICEWEIGHT" }, |
6ae4283c | 4596 | { config_parse_io_device_latency, "DEVICELATENCY" }, |
7f0386f6 LP |
4597 | { config_parse_blockio_bandwidth, "BANDWIDTH" }, |
4598 | { config_parse_blockio_weight, "WEIGHT" }, | |
4599 | { config_parse_blockio_device_weight, "DEVICEWEIGHT" }, | |
4600 | { config_parse_long, "LONG" }, | |
4601 | { config_parse_socket_service, "SERVICE" }, | |
349cc4a5 | 4602 | #if HAVE_SELINUX |
6a6751fe LP |
4603 | { config_parse_exec_selinux_context, "LABEL" }, |
4604 | #endif | |
4605 | { config_parse_job_mode, "MODE" }, | |
4606 | { config_parse_job_mode_isolate, "BOOLEAN" }, | |
4298d0b5 | 4607 | { config_parse_personality, "PERSONALITY" }, |
f975e971 LP |
4608 | }; |
4609 | ||
4610 | const char *prev = NULL; | |
4611 | const char *i; | |
4612 | ||
4613 | assert(f); | |
e537352b | 4614 | |
f975e971 LP |
4615 | NULSTR_FOREACH(i, load_fragment_gperf_nulstr) { |
4616 | const char *rvalue = "OTHER", *lvalue; | |
313b7856 | 4617 | const ConfigPerfItem *p; |
f975e971 LP |
4618 | size_t prefix_len; |
4619 | const char *dot; | |
313b7856 | 4620 | unsigned j; |
f975e971 LP |
4621 | |
4622 | assert_se(p = load_fragment_gperf_lookup(i, strlen(i))); | |
4623 | ||
313b7856 LP |
4624 | /* Hide legacy settings */ |
4625 | if (p->parse == config_parse_warn_compat && | |
4626 | p->ltype == DISABLED_LEGACY) | |
4627 | continue; | |
4628 | ||
4629 | for (j = 0; j < ELEMENTSOF(table); j++) | |
4630 | if (p->parse == table[j].callback) { | |
4631 | rvalue = table[j].rvalue; | |
4632 | break; | |
4633 | } | |
4634 | ||
f975e971 LP |
4635 | dot = strchr(i, '.'); |
4636 | lvalue = dot ? dot + 1 : i; | |
4637 | prefix_len = dot-i; | |
4638 | ||
4639 | if (dot) | |
641906e9 | 4640 | if (!prev || !strneq(prev, i, prefix_len+1)) { |
f975e971 LP |
4641 | if (prev) |
4642 | fputc('\n', f); | |
4643 | ||
4644 | fprintf(f, "[%.*s]\n", (int) prefix_len, i); | |
4645 | } | |
4646 | ||
f975e971 LP |
4647 | fprintf(f, "%s=%s\n", lvalue, rvalue); |
4648 | prev = i; | |
4649 | } | |
e537352b | 4650 | } |