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