]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a7334b09 | 2 | |
87f0e418 | 3 | #include <errno.h> |
0301abf4 | 4 | #include <stdlib.h> |
4f5dd394 | 5 | #include <string.h> |
4c253ed1 | 6 | #include <sys/prctl.h> |
45fb0699 | 7 | #include <sys/stat.h> |
4f5dd394 | 8 | #include <unistd.h> |
87f0e418 | 9 | |
718db961 LP |
10 | #include "sd-id128.h" |
11 | #include "sd-messages.h" | |
4f5dd394 | 12 | |
57b7a260 | 13 | #include "all-units.h" |
d68c645b | 14 | #include "alloc-util.h" |
fab34748 | 15 | #include "bpf-firewall.h" |
4f5dd394 LP |
16 | #include "bus-common-errors.h" |
17 | #include "bus-util.h" | |
c6c18be3 | 18 | #include "cgroup-util.h" |
4f5dd394 LP |
19 | #include "dbus-unit.h" |
20 | #include "dbus.h" | |
21 | #include "dropin.h" | |
22 | #include "escape.h" | |
23 | #include "execute.h" | |
6a48d82f | 24 | #include "fd-util.h" |
a5c32cff | 25 | #include "fileio-label.h" |
ee228be1 | 26 | #include "fileio.h" |
f97b34a6 | 27 | #include "format-util.h" |
d3070fbd | 28 | #include "fs-util.h" |
4b58153d | 29 | #include "id128-util.h" |
915b1d01 | 30 | #include "io-util.h" |
5cfa33e0 | 31 | #include "install.h" |
4f5dd394 LP |
32 | #include "load-dropin.h" |
33 | #include "load-fragment.h" | |
34 | #include "log.h" | |
35 | #include "macro.h" | |
36 | #include "missing.h" | |
37 | #include "mkdir.h" | |
6bedfcbb | 38 | #include "parse-util.h" |
4f5dd394 | 39 | #include "path-util.h" |
0b452006 | 40 | #include "process-util.h" |
d68c645b | 41 | #include "serialize.h" |
4f5dd394 | 42 | #include "set.h" |
6eb7c172 | 43 | #include "signal-util.h" |
d3070fbd | 44 | #include "sparse-endian.h" |
e9db43d5 | 45 | #include "special.h" |
2e59b241 | 46 | #include "specifier.h" |
8fcde012 | 47 | #include "stat-util.h" |
d054f0a4 | 48 | #include "stdio-util.h" |
5afe510c | 49 | #include "string-table.h" |
07630cea | 50 | #include "string-util.h" |
4f5dd394 | 51 | #include "strv.h" |
5b262f74 | 52 | #include "terminal-util.h" |
e4de7287 | 53 | #include "tmpfile-util.h" |
4f4afc88 | 54 | #include "umask-util.h" |
4f5dd394 | 55 | #include "unit-name.h" |
e9db43d5 | 56 | #include "unit.h" |
b1d4f8e1 LP |
57 | #include "user-util.h" |
58 | #include "virt.h" | |
87f0e418 LP |
59 | |
60 | const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = { | |
61 | [UNIT_SERVICE] = &service_vtable, | |
87f0e418 LP |
62 | [UNIT_SOCKET] = &socket_vtable, |
63 | [UNIT_TARGET] = &target_vtable, | |
64 | [UNIT_DEVICE] = &device_vtable, | |
65 | [UNIT_MOUNT] = &mount_vtable, | |
66 | [UNIT_AUTOMOUNT] = &automount_vtable, | |
01f78473 | 67 | [UNIT_SWAP] = &swap_vtable, |
e821075a | 68 | [UNIT_TIMER] = &timer_vtable, |
a016b922 | 69 | [UNIT_PATH] = &path_vtable, |
6c12b52e | 70 | [UNIT_SLICE] = &slice_vtable, |
5afe510c | 71 | [UNIT_SCOPE] = &scope_vtable, |
87f0e418 LP |
72 | }; |
73 | ||
f2341e0a | 74 | static void maybe_warn_about_dependency(Unit *u, const char *other, UnitDependency dependency); |
d1fab3fe | 75 | |
7d17cfbc | 76 | Unit *unit_new(Manager *m, size_t size) { |
87f0e418 LP |
77 | Unit *u; |
78 | ||
79 | assert(m); | |
ac155bb8 | 80 | assert(size >= sizeof(Unit)); |
87f0e418 | 81 | |
7d17cfbc MS |
82 | u = malloc0(size); |
83 | if (!u) | |
87f0e418 LP |
84 | return NULL; |
85 | ||
d5099efc | 86 | u->names = set_new(&string_hash_ops); |
6b430fdb ZJS |
87 | if (!u->names) |
88 | return mfree(u); | |
87f0e418 | 89 | |
ac155bb8 MS |
90 | u->manager = m; |
91 | u->type = _UNIT_TYPE_INVALID; | |
ac155bb8 MS |
92 | u->default_dependencies = true; |
93 | u->unit_file_state = _UNIT_FILE_STATE_INVALID; | |
d2dc52db | 94 | u->unit_file_preset = -1; |
d420282b | 95 | u->on_failure_job_mode = JOB_REPLACE; |
0bb814c2 | 96 | u->cgroup_control_inotify_wd = -1; |
afcfaa69 | 97 | u->cgroup_memory_inotify_wd = -1; |
36c16a7c | 98 | u->job_timeout = USEC_INFINITY; |
a2df3ea4 | 99 | u->job_running_timeout = USEC_INFINITY; |
00d9ef85 LP |
100 | u->ref_uid = UID_INVALID; |
101 | u->ref_gid = GID_INVALID; | |
fe700f46 | 102 | u->cpu_usage_last = NSEC_INFINITY; |
17f14955 | 103 | u->cgroup_invalidated_mask |= CGROUP_MASK_BPF_FIREWALL; |
7af67e9a | 104 | u->failure_action_exit_status = u->success_action_exit_status = -1; |
87f0e418 | 105 | |
6a48d82f DM |
106 | u->ip_accounting_ingress_map_fd = -1; |
107 | u->ip_accounting_egress_map_fd = -1; | |
108 | u->ipv4_allow_map_fd = -1; | |
109 | u->ipv6_allow_map_fd = -1; | |
110 | u->ipv4_deny_map_fd = -1; | |
111 | u->ipv6_deny_map_fd = -1; | |
112 | ||
2e59b241 LP |
113 | u->last_section_private = -1; |
114 | ||
6bf0f408 | 115 | RATELIMIT_INIT(u->start_limit, m->default_start_limit_interval, m->default_start_limit_burst); |
67bfdc97 | 116 | RATELIMIT_INIT(u->auto_stop_ratelimit, 10 * USEC_PER_SEC, 16); |
bea355da | 117 | |
fbe14fc9 LP |
118 | for (CGroupIOAccountingMetric i = 0; i < _CGROUP_IO_ACCOUNTING_METRIC_MAX; i++) |
119 | u->io_accounting_last[i] = UINT64_MAX; | |
120 | ||
87f0e418 LP |
121 | return u; |
122 | } | |
123 | ||
a581e45a | 124 | int unit_new_for_name(Manager *m, size_t size, const char *name, Unit **ret) { |
dc409696 | 125 | _cleanup_(unit_freep) Unit *u = NULL; |
a581e45a LP |
126 | int r; |
127 | ||
128 | u = unit_new(m, size); | |
129 | if (!u) | |
130 | return -ENOMEM; | |
131 | ||
132 | r = unit_add_name(u, name); | |
dc409696 | 133 | if (r < 0) |
a581e45a | 134 | return r; |
a581e45a | 135 | |
1cc6c93a YW |
136 | *ret = TAKE_PTR(u); |
137 | ||
a581e45a LP |
138 | return r; |
139 | } | |
140 | ||
303ee601 | 141 | bool unit_has_name(const Unit *u, const char *name) { |
f278026d LP |
142 | assert(u); |
143 | assert(name); | |
144 | ||
390bc2b1 | 145 | return set_contains(u->names, (char*) name); |
f278026d LP |
146 | } |
147 | ||
598459ce LP |
148 | static void unit_init(Unit *u) { |
149 | CGroupContext *cc; | |
150 | ExecContext *ec; | |
151 | KillContext *kc; | |
152 | ||
153 | assert(u); | |
154 | assert(u->manager); | |
155 | assert(u->type >= 0); | |
156 | ||
157 | cc = unit_get_cgroup_context(u); | |
158 | if (cc) { | |
159 | cgroup_context_init(cc); | |
160 | ||
161 | /* Copy in the manager defaults into the cgroup | |
162 | * context, _before_ the rest of the settings have | |
163 | * been initialized */ | |
164 | ||
165 | cc->cpu_accounting = u->manager->default_cpu_accounting; | |
13c31542 | 166 | cc->io_accounting = u->manager->default_io_accounting; |
598459ce LP |
167 | cc->blockio_accounting = u->manager->default_blockio_accounting; |
168 | cc->memory_accounting = u->manager->default_memory_accounting; | |
03a7b521 | 169 | cc->tasks_accounting = u->manager->default_tasks_accounting; |
6a48d82f | 170 | cc->ip_accounting = u->manager->default_ip_accounting; |
0af20ea2 LP |
171 | |
172 | if (u->type != UNIT_SLICE) | |
173 | cc->tasks_max = u->manager->default_tasks_max; | |
598459ce LP |
174 | } |
175 | ||
176 | ec = unit_get_exec_context(u); | |
b1edf445 | 177 | if (ec) { |
598459ce LP |
178 | exec_context_init(ec); |
179 | ||
b1edf445 | 180 | ec->keyring_mode = MANAGER_IS_SYSTEM(u->manager) ? |
00f5ad93 | 181 | EXEC_KEYRING_SHARED : EXEC_KEYRING_INHERIT; |
b1edf445 LP |
182 | } |
183 | ||
598459ce LP |
184 | kc = unit_get_kill_context(u); |
185 | if (kc) | |
186 | kill_context_init(kc); | |
187 | ||
188 | if (UNIT_VTABLE(u)->init) | |
189 | UNIT_VTABLE(u)->init(u); | |
190 | } | |
191 | ||
87f0e418 | 192 | int unit_add_name(Unit *u, const char *text) { |
598459ce | 193 | _cleanup_free_ char *s = NULL, *i = NULL; |
87f0e418 | 194 | UnitType t; |
87f0e418 LP |
195 | int r; |
196 | ||
197 | assert(u); | |
198 | assert(text); | |
199 | ||
7410616c | 200 | if (unit_name_is_valid(text, UNIT_NAME_TEMPLATE)) { |
598459ce | 201 | |
ac155bb8 | 202 | if (!u->instance) |
9e2f7c11 | 203 | return -EINVAL; |
87f0e418 | 204 | |
7410616c LP |
205 | r = unit_name_replace_instance(text, u->instance, &s); |
206 | if (r < 0) | |
207 | return r; | |
208 | } else { | |
9e2f7c11 | 209 | s = strdup(text); |
7410616c LP |
210 | if (!s) |
211 | return -ENOMEM; | |
212 | } | |
87f0e418 | 213 | |
7410616c LP |
214 | if (set_contains(u->names, s)) |
215 | return 0; | |
216 | if (hashmap_contains(u->manager->units, s)) | |
217 | return -EEXIST; | |
218 | ||
219 | if (!unit_name_is_valid(s, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) | |
598459ce | 220 | return -EINVAL; |
e537352b | 221 | |
7410616c LP |
222 | t = unit_name_to_type(s); |
223 | if (t < 0) | |
224 | return -EINVAL; | |
87f0e418 | 225 | |
598459ce LP |
226 | if (u->type != _UNIT_TYPE_INVALID && t != u->type) |
227 | return -EINVAL; | |
87f0e418 | 228 | |
e48614c4 ZJS |
229 | r = unit_name_to_instance(s, &i); |
230 | if (r < 0) | |
598459ce | 231 | return r; |
87f0e418 | 232 | |
ce99c68a | 233 | if (i && !unit_type_may_template(t)) |
598459ce | 234 | return -EINVAL; |
9e2f7c11 | 235 | |
276c3e78 | 236 | /* Ensure that this unit is either instanced or not instanced, |
7410616c LP |
237 | * but not both. Note that we do allow names with different |
238 | * instance names however! */ | |
598459ce LP |
239 | if (u->type != _UNIT_TYPE_INVALID && !u->instance != !i) |
240 | return -EINVAL; | |
9e2f7c11 | 241 | |
8a993b61 | 242 | if (!unit_type_may_alias(t) && !set_isempty(u->names)) |
598459ce | 243 | return -EEXIST; |
9e2f7c11 | 244 | |
598459ce LP |
245 | if (hashmap_size(u->manager->units) >= MANAGER_MAX_NAMES) |
246 | return -E2BIG; | |
4f0f902f | 247 | |
e48614c4 | 248 | r = set_put(u->names, s); |
7410616c | 249 | if (r < 0) |
598459ce | 250 | return r; |
7410616c | 251 | assert(r > 0); |
87f0e418 | 252 | |
e48614c4 ZJS |
253 | r = hashmap_put(u->manager->units, s, u); |
254 | if (r < 0) { | |
7410616c | 255 | (void) set_remove(u->names, s); |
598459ce | 256 | return r; |
87f0e418 LP |
257 | } |
258 | ||
ac155bb8 | 259 | if (u->type == _UNIT_TYPE_INVALID) { |
ac155bb8 MS |
260 | u->type = t; |
261 | u->id = s; | |
1cc6c93a | 262 | u->instance = TAKE_PTR(i); |
9e2f7c11 | 263 | |
71fda00f | 264 | LIST_PREPEND(units_by_type, u->manager->units_by_type[t], u); |
e537352b | 265 | |
598459ce | 266 | unit_init(u); |
598459ce | 267 | } |
9e2f7c11 | 268 | |
598459ce | 269 | s = NULL; |
9e2f7c11 | 270 | |
598459ce LP |
271 | unit_add_to_dbus_queue(u); |
272 | return 0; | |
87f0e418 LP |
273 | } |
274 | ||
0ae97ec1 | 275 | int unit_choose_id(Unit *u, const char *name) { |
68eda4bd | 276 | _cleanup_free_ char *t = NULL; |
598459ce | 277 | char *s, *i; |
276c3e78 | 278 | int r; |
0ae97ec1 LP |
279 | |
280 | assert(u); | |
281 | assert(name); | |
282 | ||
7410616c | 283 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
9e2f7c11 | 284 | |
ac155bb8 | 285 | if (!u->instance) |
9e2f7c11 LP |
286 | return -EINVAL; |
287 | ||
7410616c LP |
288 | r = unit_name_replace_instance(name, u->instance, &t); |
289 | if (r < 0) | |
290 | return r; | |
9e2f7c11 LP |
291 | |
292 | name = t; | |
293 | } | |
294 | ||
0ae97ec1 | 295 | /* Selects one of the names of this unit as the id */ |
ac155bb8 | 296 | s = set_get(u->names, (char*) name); |
9e2f7c11 | 297 | if (!s) |
0ae97ec1 LP |
298 | return -ENOENT; |
299 | ||
7410616c | 300 | /* Determine the new instance from the new id */ |
e48614c4 ZJS |
301 | r = unit_name_to_instance(s, &i); |
302 | if (r < 0) | |
276c3e78 LP |
303 | return r; |
304 | ||
ac155bb8 | 305 | u->id = s; |
276c3e78 | 306 | |
ac155bb8 MS |
307 | free(u->instance); |
308 | u->instance = i; | |
276c3e78 | 309 | |
c1e1601e | 310 | unit_add_to_dbus_queue(u); |
9e2f7c11 | 311 | |
0ae97ec1 LP |
312 | return 0; |
313 | } | |
314 | ||
f50e0a01 | 315 | int unit_set_description(Unit *u, const char *description) { |
84b26d51 | 316 | int r; |
f50e0a01 LP |
317 | |
318 | assert(u); | |
319 | ||
84b26d51 LP |
320 | r = free_and_strdup(&u->description, empty_to_null(description)); |
321 | if (r < 0) | |
322 | return r; | |
323 | if (r > 0) | |
324 | unit_add_to_dbus_queue(u); | |
c1e1601e | 325 | |
f50e0a01 LP |
326 | return 0; |
327 | } | |
328 | ||
f2f725e5 | 329 | bool unit_may_gc(Unit *u) { |
a354329f | 330 | UnitActiveState state; |
e98b2fbb | 331 | int r; |
5afe510c | 332 | |
701cc384 LP |
333 | assert(u); |
334 | ||
f2f725e5 ZJS |
335 | /* Checks whether the unit is ready to be unloaded for garbage collection. |
336 | * Returns true when the unit may be collected, and false if there's some | |
2641f02e ZJS |
337 | * reason to keep it loaded. |
338 | * | |
339 | * References from other units are *not* checked here. Instead, this is done | |
340 | * in unit_gc_sweep(), but using markers to properly collect dependency loops. | |
341 | */ | |
5afe510c | 342 | |
a354329f | 343 | if (u->job) |
f2f725e5 | 344 | return false; |
701cc384 | 345 | |
a354329f | 346 | if (u->nop_job) |
f2f725e5 | 347 | return false; |
6c073082 | 348 | |
a354329f LP |
349 | state = unit_active_state(u); |
350 | ||
7eb2a8a1 | 351 | /* If the unit is inactive and failed and no job is queued for it, then release its runtime resources */ |
a354329f LP |
352 | if (UNIT_IS_INACTIVE_OR_FAILED(state) && |
353 | UNIT_VTABLE(u)->release_resources) | |
7eb2a8a1 | 354 | UNIT_VTABLE(u)->release_resources(u); |
a354329f | 355 | |
f5869324 | 356 | if (u->perpetual) |
f2f725e5 | 357 | return false; |
9d576438 | 358 | |
05a98afd | 359 | if (sd_bus_track_count(u->bus_track) > 0) |
f2f725e5 | 360 | return false; |
05a98afd | 361 | |
5afe510c LP |
362 | /* But we keep the unit object around for longer when it is referenced or configured to not be gc'ed */ |
363 | switch (u->collect_mode) { | |
364 | ||
365 | case COLLECT_INACTIVE: | |
366 | if (state != UNIT_INACTIVE) | |
f2f725e5 | 367 | return false; |
5afe510c LP |
368 | |
369 | break; | |
370 | ||
371 | case COLLECT_INACTIVE_OR_FAILED: | |
372 | if (!IN_SET(state, UNIT_INACTIVE, UNIT_FAILED)) | |
f2f725e5 | 373 | return false; |
5afe510c LP |
374 | |
375 | break; | |
376 | ||
377 | default: | |
378 | assert_not_reached("Unknown garbage collection mode"); | |
379 | } | |
380 | ||
e98b2fbb LP |
381 | if (u->cgroup_path) { |
382 | /* If the unit has a cgroup, then check whether there's anything in it. If so, we should stay | |
383 | * around. Units with active processes should never be collected. */ | |
384 | ||
385 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path); | |
386 | if (r < 0) | |
387 | log_unit_debug_errno(u, r, "Failed to determine whether cgroup %s is empty: %m", u->cgroup_path); | |
388 | if (r <= 0) | |
f2f725e5 | 389 | return false; |
e98b2fbb LP |
390 | } |
391 | ||
f2f725e5 ZJS |
392 | if (UNIT_VTABLE(u)->may_gc && !UNIT_VTABLE(u)->may_gc(u)) |
393 | return false; | |
701cc384 | 394 | |
f2f725e5 | 395 | return true; |
701cc384 LP |
396 | } |
397 | ||
87f0e418 LP |
398 | void unit_add_to_load_queue(Unit *u) { |
399 | assert(u); | |
ac155bb8 | 400 | assert(u->type != _UNIT_TYPE_INVALID); |
87f0e418 | 401 | |
ac155bb8 | 402 | if (u->load_state != UNIT_STUB || u->in_load_queue) |
87f0e418 LP |
403 | return; |
404 | ||
71fda00f | 405 | LIST_PREPEND(load_queue, u->manager->load_queue, u); |
ac155bb8 | 406 | u->in_load_queue = true; |
87f0e418 LP |
407 | } |
408 | ||
23a177ef LP |
409 | void unit_add_to_cleanup_queue(Unit *u) { |
410 | assert(u); | |
411 | ||
ac155bb8 | 412 | if (u->in_cleanup_queue) |
23a177ef LP |
413 | return; |
414 | ||
71fda00f | 415 | LIST_PREPEND(cleanup_queue, u->manager->cleanup_queue, u); |
ac155bb8 | 416 | u->in_cleanup_queue = true; |
23a177ef LP |
417 | } |
418 | ||
701cc384 LP |
419 | void unit_add_to_gc_queue(Unit *u) { |
420 | assert(u); | |
421 | ||
ac155bb8 | 422 | if (u->in_gc_queue || u->in_cleanup_queue) |
701cc384 LP |
423 | return; |
424 | ||
f2f725e5 | 425 | if (!unit_may_gc(u)) |
701cc384 LP |
426 | return; |
427 | ||
c5a97ed1 | 428 | LIST_PREPEND(gc_queue, u->manager->gc_unit_queue, u); |
ac155bb8 | 429 | u->in_gc_queue = true; |
701cc384 LP |
430 | } |
431 | ||
c1e1601e LP |
432 | void unit_add_to_dbus_queue(Unit *u) { |
433 | assert(u); | |
ac155bb8 | 434 | assert(u->type != _UNIT_TYPE_INVALID); |
c1e1601e | 435 | |
ac155bb8 | 436 | if (u->load_state == UNIT_STUB || u->in_dbus_queue) |
c1e1601e LP |
437 | return; |
438 | ||
a567261a | 439 | /* Shortcut things if nobody cares */ |
8f8f05a9 | 440 | if (sd_bus_track_count(u->manager->subscribed) <= 0 && |
ae572acd | 441 | sd_bus_track_count(u->bus_track) <= 0 && |
8f8f05a9 | 442 | set_isempty(u->manager->private_buses)) { |
ac155bb8 | 443 | u->sent_dbus_new_signal = true; |
94b6dfa2 LP |
444 | return; |
445 | } | |
446 | ||
71fda00f | 447 | LIST_PREPEND(dbus_queue, u->manager->dbus_unit_queue, u); |
ac155bb8 | 448 | u->in_dbus_queue = true; |
c1e1601e LP |
449 | } |
450 | ||
fda09318 | 451 | void unit_submit_to_stop_when_unneeded_queue(Unit *u) { |
a3c1168a LP |
452 | assert(u); |
453 | ||
454 | if (u->in_stop_when_unneeded_queue) | |
455 | return; | |
456 | ||
457 | if (!u->stop_when_unneeded) | |
458 | return; | |
459 | ||
460 | if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) | |
461 | return; | |
462 | ||
463 | LIST_PREPEND(stop_when_unneeded_queue, u->manager->stop_when_unneeded_queue, u); | |
464 | u->in_stop_when_unneeded_queue = true; | |
465 | } | |
466 | ||
eef85c4a | 467 | static void bidi_set_free(Unit *u, Hashmap *h) { |
87f0e418 | 468 | Unit *other; |
eef85c4a LP |
469 | Iterator i; |
470 | void *v; | |
87f0e418 LP |
471 | |
472 | assert(u); | |
473 | ||
eef85c4a | 474 | /* Frees the hashmap and makes sure we are dropped from the inverse pointers */ |
87f0e418 | 475 | |
eef85c4a | 476 | HASHMAP_FOREACH_KEY(v, other, h, i) { |
87f0e418 LP |
477 | UnitDependency d; |
478 | ||
479 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
eef85c4a | 480 | hashmap_remove(other->dependencies[d], u); |
701cc384 LP |
481 | |
482 | unit_add_to_gc_queue(other); | |
87f0e418 LP |
483 | } |
484 | ||
eef85c4a | 485 | hashmap_free(h); |
87f0e418 LP |
486 | } |
487 | ||
c2756a68 LP |
488 | static void unit_remove_transient(Unit *u) { |
489 | char **i; | |
490 | ||
491 | assert(u); | |
492 | ||
493 | if (!u->transient) | |
494 | return; | |
495 | ||
496 | if (u->fragment_path) | |
3f5e8115 | 497 | (void) unlink(u->fragment_path); |
c2756a68 LP |
498 | |
499 | STRV_FOREACH(i, u->dropin_paths) { | |
39591351 | 500 | _cleanup_free_ char *p = NULL, *pp = NULL; |
c2756a68 | 501 | |
39591351 LP |
502 | p = dirname_malloc(*i); /* Get the drop-in directory from the drop-in file */ |
503 | if (!p) | |
504 | continue; | |
505 | ||
506 | pp = dirname_malloc(p); /* Get the config directory from the drop-in directory */ | |
507 | if (!pp) | |
508 | continue; | |
509 | ||
510 | /* Only drop transient drop-ins */ | |
511 | if (!path_equal(u->manager->lookup_paths.transient, pp)) | |
512 | continue; | |
c2756a68 | 513 | |
39591351 LP |
514 | (void) unlink(*i); |
515 | (void) rmdir(p); | |
c2756a68 LP |
516 | } |
517 | } | |
518 | ||
a57f7e2c | 519 | static void unit_free_requires_mounts_for(Unit *u) { |
eef85c4a | 520 | assert(u); |
a57f7e2c | 521 | |
eef85c4a LP |
522 | for (;;) { |
523 | _cleanup_free_ char *path; | |
a57f7e2c | 524 | |
eef85c4a LP |
525 | path = hashmap_steal_first_key(u->requires_mounts_for); |
526 | if (!path) | |
527 | break; | |
528 | else { | |
529 | char s[strlen(path) + 1]; | |
a57f7e2c | 530 | |
eef85c4a LP |
531 | PATH_FOREACH_PREFIX_MORE(s, path) { |
532 | char *y; | |
533 | Set *x; | |
534 | ||
535 | x = hashmap_get2(u->manager->units_requiring_mounts_for, s, (void**) &y); | |
536 | if (!x) | |
537 | continue; | |
a57f7e2c | 538 | |
eef85c4a | 539 | (void) set_remove(x, u); |
a57f7e2c | 540 | |
eef85c4a LP |
541 | if (set_isempty(x)) { |
542 | (void) hashmap_remove(u->manager->units_requiring_mounts_for, y); | |
543 | free(y); | |
544 | set_free(x); | |
545 | } | |
a57f7e2c LP |
546 | } |
547 | } | |
548 | } | |
549 | ||
eef85c4a | 550 | u->requires_mounts_for = hashmap_free(u->requires_mounts_for); |
a57f7e2c LP |
551 | } |
552 | ||
598459ce LP |
553 | static void unit_done(Unit *u) { |
554 | ExecContext *ec; | |
555 | CGroupContext *cc; | |
556 | ||
557 | assert(u); | |
558 | ||
559 | if (u->type < 0) | |
560 | return; | |
561 | ||
562 | if (UNIT_VTABLE(u)->done) | |
563 | UNIT_VTABLE(u)->done(u); | |
564 | ||
565 | ec = unit_get_exec_context(u); | |
566 | if (ec) | |
567 | exec_context_done(ec); | |
568 | ||
569 | cc = unit_get_cgroup_context(u); | |
570 | if (cc) | |
571 | cgroup_context_done(cc); | |
572 | } | |
573 | ||
87f0e418 LP |
574 | void unit_free(Unit *u) { |
575 | UnitDependency d; | |
576 | Iterator i; | |
577 | char *t; | |
578 | ||
c9d5c9c0 LP |
579 | if (!u) |
580 | return; | |
87f0e418 | 581 | |
b8b6f321 LP |
582 | if (UNIT_ISSET(u->slice)) { |
583 | /* A unit is being dropped from the tree, make sure our parent slice recalculates the member mask */ | |
584 | unit_invalidate_cgroup_members_masks(UNIT_DEREF(u->slice)); | |
585 | ||
586 | /* And make sure the parent is realized again, updating cgroup memberships */ | |
587 | unit_add_to_cgroup_realize_queue(UNIT_DEREF(u->slice)); | |
588 | } | |
589 | ||
50fb00b7 | 590 | u->transient_file = safe_fclose(u->transient_file); |
4f4afc88 | 591 | |
2c289ea8 | 592 | if (!MANAGER_IS_RELOADING(u->manager)) |
c2756a68 LP |
593 | unit_remove_transient(u); |
594 | ||
c1e1601e LP |
595 | bus_unit_send_removed_signal(u); |
596 | ||
598459ce | 597 | unit_done(u); |
a013b84b | 598 | |
50be4f4a | 599 | unit_dequeue_rewatch_pids(u); |
cf9fd508 | 600 | |
50be4f4a | 601 | sd_bus_slot_unref(u->match_bus_slot); |
05a98afd LP |
602 | sd_bus_track_unref(u->bus_track); |
603 | u->deserialized_refs = strv_free(u->deserialized_refs); | |
604 | ||
a57f7e2c LP |
605 | unit_free_requires_mounts_for(u); |
606 | ||
ac155bb8 MS |
607 | SET_FOREACH(t, u->names, i) |
608 | hashmap_remove_value(u->manager->units, t, u); | |
87f0e418 | 609 | |
4b58153d LP |
610 | if (!sd_id128_is_null(u->invocation_id)) |
611 | hashmap_remove_value(u->manager->units_by_invocation_id, &u->invocation_id, u); | |
612 | ||
97e7d748 MS |
613 | if (u->job) { |
614 | Job *j = u->job; | |
615 | job_uninstall(j); | |
616 | job_free(j); | |
617 | } | |
964e0949 | 618 | |
e0209d83 MS |
619 | if (u->nop_job) { |
620 | Job *j = u->nop_job; | |
621 | job_uninstall(j); | |
622 | job_free(j); | |
623 | } | |
624 | ||
964e0949 | 625 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) |
ac155bb8 | 626 | bidi_set_free(u, u->dependencies[d]); |
964e0949 | 627 | |
adefcf28 LP |
628 | if (u->on_console) |
629 | manager_unref_console(u->manager); | |
630 | ||
efdb0237 | 631 | unit_release_cgroup(u); |
72673e86 | 632 | |
d3070fbd LP |
633 | if (!MANAGER_IS_RELOADING(u->manager)) |
634 | unit_unlink_state_files(u); | |
635 | ||
00d9ef85 LP |
636 | unit_unref_uid_gid(u, false); |
637 | ||
5269eb6b | 638 | (void) manager_update_failed_units(u->manager, u, false); |
db785129 | 639 | set_remove(u->manager->startup_units, u); |
f755e3b7 | 640 | |
a911bb9a LP |
641 | unit_unwatch_all_pids(u); |
642 | ||
702a2d8f | 643 | unit_ref_unset(&u->slice); |
7f7d01ed ZJS |
644 | while (u->refs_by_target) |
645 | unit_ref_unset(u->refs_by_target); | |
57020a3a | 646 | |
ac155bb8 | 647 | if (u->type != _UNIT_TYPE_INVALID) |
71fda00f | 648 | LIST_REMOVE(units_by_type, u->manager->units_by_type[u->type], u); |
ef734fd6 | 649 | |
ac155bb8 | 650 | if (u->in_load_queue) |
71fda00f | 651 | LIST_REMOVE(load_queue, u->manager->load_queue, u); |
87f0e418 | 652 | |
ac155bb8 | 653 | if (u->in_dbus_queue) |
71fda00f | 654 | LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u); |
c1e1601e | 655 | |
a2d72e26 | 656 | if (u->in_gc_queue) |
c5a97ed1 | 657 | LIST_REMOVE(gc_queue, u->manager->gc_unit_queue, u); |
701cc384 | 658 | |
91a6073e LP |
659 | if (u->in_cgroup_realize_queue) |
660 | LIST_REMOVE(cgroup_realize_queue, u->manager->cgroup_realize_queue, u); | |
87f0e418 | 661 | |
09e24654 LP |
662 | if (u->in_cgroup_empty_queue) |
663 | LIST_REMOVE(cgroup_empty_queue, u->manager->cgroup_empty_queue, u); | |
664 | ||
1bdf2790 ZJS |
665 | if (u->in_cleanup_queue) |
666 | LIST_REMOVE(cleanup_queue, u->manager->cleanup_queue, u); | |
adefcf28 | 667 | |
19496554 MS |
668 | if (u->in_target_deps_queue) |
669 | LIST_REMOVE(target_deps_queue, u->manager->target_deps_queue, u); | |
670 | ||
a3c1168a LP |
671 | if (u->in_stop_when_unneeded_queue) |
672 | LIST_REMOVE(stop_when_unneeded_queue, u->manager->stop_when_unneeded_queue, u); | |
673 | ||
6a48d82f DM |
674 | safe_close(u->ip_accounting_ingress_map_fd); |
675 | safe_close(u->ip_accounting_egress_map_fd); | |
72673e86 | 676 | |
6a48d82f DM |
677 | safe_close(u->ipv4_allow_map_fd); |
678 | safe_close(u->ipv6_allow_map_fd); | |
679 | safe_close(u->ipv4_deny_map_fd); | |
680 | safe_close(u->ipv6_deny_map_fd); | |
d3070fbd | 681 | |
6a48d82f | 682 | bpf_program_unref(u->ip_bpf_ingress); |
aa2b6f1d | 683 | bpf_program_unref(u->ip_bpf_ingress_installed); |
6a48d82f | 684 | bpf_program_unref(u->ip_bpf_egress); |
aa2b6f1d | 685 | bpf_program_unref(u->ip_bpf_egress_installed); |
00d9ef85 | 686 | |
fab34748 KL |
687 | set_free(u->ip_bpf_custom_ingress); |
688 | set_free(u->ip_bpf_custom_egress); | |
689 | set_free(u->ip_bpf_custom_ingress_installed); | |
690 | set_free(u->ip_bpf_custom_egress_installed); | |
691 | ||
084c7007 RG |
692 | bpf_program_unref(u->bpf_device_control_installed); |
693 | ||
a946fa9b ZJS |
694 | condition_free_list(u->conditions); |
695 | condition_free_list(u->asserts); | |
f755e3b7 | 696 | |
ac155bb8 | 697 | free(u->description); |
49dbfa7b | 698 | strv_free(u->documentation); |
ac155bb8 | 699 | free(u->fragment_path); |
1b64d026 | 700 | free(u->source_path); |
ae7a7182 | 701 | strv_free(u->dropin_paths); |
ac155bb8 | 702 | free(u->instance); |
87f0e418 | 703 | |
f189ab18 LP |
704 | free(u->job_timeout_reboot_arg); |
705 | ||
ac155bb8 | 706 | set_free_free(u->names); |
87f0e418 | 707 | |
6bf0f408 LP |
708 | free(u->reboot_arg); |
709 | ||
87f0e418 LP |
710 | free(u); |
711 | } | |
712 | ||
713 | UnitActiveState unit_active_state(Unit *u) { | |
714 | assert(u); | |
715 | ||
ac155bb8 | 716 | if (u->load_state == UNIT_MERGED) |
6124958c LP |
717 | return unit_active_state(unit_follow_merge(u)); |
718 | ||
719 | /* After a reload it might happen that a unit is not correctly | |
720 | * loaded but still has a process around. That's why we won't | |
fdf20a31 | 721 | * shortcut failed loading to UNIT_INACTIVE_FAILED. */ |
87f0e418 LP |
722 | |
723 | return UNIT_VTABLE(u)->active_state(u); | |
724 | } | |
725 | ||
10a94420 LP |
726 | const char* unit_sub_state_to_string(Unit *u) { |
727 | assert(u); | |
728 | ||
729 | return UNIT_VTABLE(u)->sub_state_to_string(u); | |
730 | } | |
731 | ||
eef85c4a LP |
732 | static int set_complete_move(Set **s, Set **other) { |
733 | assert(s); | |
734 | assert(other); | |
735 | ||
736 | if (!other) | |
737 | return 0; | |
738 | ||
739 | if (*s) | |
740 | return set_move(*s, *other); | |
ae2a15bc LP |
741 | else |
742 | *s = TAKE_PTR(*other); | |
7c0b05e5 | 743 | |
eef85c4a LP |
744 | return 0; |
745 | } | |
746 | ||
747 | static int hashmap_complete_move(Hashmap **s, Hashmap **other) { | |
23a177ef LP |
748 | assert(s); |
749 | assert(other); | |
87f0e418 | 750 | |
23a177ef | 751 | if (!*other) |
7c0b05e5 | 752 | return 0; |
87f0e418 | 753 | |
eef85c4a LP |
754 | if (*s) |
755 | return hashmap_move(*s, *other); | |
ae2a15bc LP |
756 | else |
757 | *s = TAKE_PTR(*other); | |
7c0b05e5 MS |
758 | |
759 | return 0; | |
23a177ef | 760 | } |
87f0e418 | 761 | |
7c0b05e5 | 762 | static int merge_names(Unit *u, Unit *other) { |
23a177ef LP |
763 | char *t; |
764 | Iterator i; | |
7c0b05e5 | 765 | int r; |
87f0e418 | 766 | |
23a177ef LP |
767 | assert(u); |
768 | assert(other); | |
769 | ||
eef85c4a | 770 | r = set_complete_move(&u->names, &other->names); |
7c0b05e5 MS |
771 | if (r < 0) |
772 | return r; | |
23a177ef | 773 | |
ac155bb8 MS |
774 | set_free_free(other->names); |
775 | other->names = NULL; | |
776 | other->id = NULL; | |
23a177ef | 777 | |
ac155bb8 MS |
778 | SET_FOREACH(t, u->names, i) |
779 | assert_se(hashmap_replace(u->manager->units, t, u) == 0); | |
7c0b05e5 MS |
780 | |
781 | return 0; | |
87f0e418 LP |
782 | } |
783 | ||
09a65f92 MS |
784 | static int reserve_dependencies(Unit *u, Unit *other, UnitDependency d) { |
785 | unsigned n_reserve; | |
786 | ||
787 | assert(u); | |
788 | assert(other); | |
789 | assert(d < _UNIT_DEPENDENCY_MAX); | |
790 | ||
791 | /* | |
792 | * If u does not have this dependency set allocated, there is no need | |
f131770b | 793 | * to reserve anything. In that case other's set will be transferred |
09a65f92 MS |
794 | * as a whole to u by complete_move(). |
795 | */ | |
796 | if (!u->dependencies[d]) | |
797 | return 0; | |
798 | ||
799 | /* merge_dependencies() will skip a u-on-u dependency */ | |
eef85c4a | 800 | n_reserve = hashmap_size(other->dependencies[d]) - !!hashmap_get(other->dependencies[d], u); |
09a65f92 | 801 | |
eef85c4a | 802 | return hashmap_reserve(u->dependencies[d], n_reserve); |
09a65f92 MS |
803 | } |
804 | ||
d1fab3fe | 805 | static void merge_dependencies(Unit *u, Unit *other, const char *other_id, UnitDependency d) { |
23a177ef LP |
806 | Iterator i; |
807 | Unit *back; | |
eef85c4a | 808 | void *v; |
87f0e418 | 809 | int r; |
23a177ef | 810 | |
eef85c4a LP |
811 | /* Merges all dependencies of type 'd' of the unit 'other' into the deps of the unit 'u' */ |
812 | ||
23a177ef LP |
813 | assert(u); |
814 | assert(other); | |
815 | assert(d < _UNIT_DEPENDENCY_MAX); | |
816 | ||
5238e957 | 817 | /* Fix backwards pointers. Let's iterate through all dependent units of the other unit. */ |
eef85c4a | 818 | HASHMAP_FOREACH_KEY(v, back, other->dependencies[d], i) { |
23a177ef LP |
819 | UnitDependency k; |
820 | ||
eef85c4a LP |
821 | /* Let's now iterate through the dependencies of that dependencies of the other units, looking for |
822 | * pointers back, and let's fix them up, to instead point to 'u'. */ | |
823 | ||
e48614c4 | 824 | for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++) { |
e66047ff | 825 | if (back == u) { |
eef85c4a LP |
826 | /* Do not add dependencies between u and itself. */ |
827 | if (hashmap_remove(back->dependencies[k], other)) | |
f2341e0a | 828 | maybe_warn_about_dependency(u, other_id, k); |
e66047ff | 829 | } else { |
eef85c4a LP |
830 | UnitDependencyInfo di_u, di_other, di_merged; |
831 | ||
832 | /* Let's drop this dependency between "back" and "other", and let's create it between | |
833 | * "back" and "u" instead. Let's merge the bit masks of the dependency we are moving, | |
834 | * and any such dependency which might already exist */ | |
835 | ||
836 | di_other.data = hashmap_get(back->dependencies[k], other); | |
837 | if (!di_other.data) | |
838 | continue; /* dependency isn't set, let's try the next one */ | |
839 | ||
840 | di_u.data = hashmap_get(back->dependencies[k], u); | |
841 | ||
842 | di_merged = (UnitDependencyInfo) { | |
843 | .origin_mask = di_u.origin_mask | di_other.origin_mask, | |
844 | .destination_mask = di_u.destination_mask | di_other.destination_mask, | |
845 | }; | |
846 | ||
847 | r = hashmap_remove_and_replace(back->dependencies[k], other, u, di_merged.data); | |
848 | if (r < 0) | |
849 | log_warning_errno(r, "Failed to remove/replace: back=%s other=%s u=%s: %m", back->id, other_id, u->id); | |
850 | assert(r >= 0); | |
851 | ||
852 | /* assert_se(hashmap_remove_and_replace(back->dependencies[k], other, u, di_merged.data) >= 0); */ | |
e66047ff | 853 | } |
e48614c4 | 854 | } |
eef85c4a | 855 | |
23a177ef LP |
856 | } |
857 | ||
e66047ff | 858 | /* Also do not move dependencies on u to itself */ |
eef85c4a | 859 | back = hashmap_remove(other->dependencies[d], u); |
d1fab3fe | 860 | if (back) |
f2341e0a | 861 | maybe_warn_about_dependency(u, other_id, d); |
e66047ff | 862 | |
7c0b05e5 | 863 | /* The move cannot fail. The caller must have performed a reservation. */ |
eef85c4a | 864 | assert_se(hashmap_complete_move(&u->dependencies[d], &other->dependencies[d]) == 0); |
23a177ef | 865 | |
eef85c4a | 866 | other->dependencies[d] = hashmap_free(other->dependencies[d]); |
23a177ef LP |
867 | } |
868 | ||
869 | int unit_merge(Unit *u, Unit *other) { | |
87f0e418 | 870 | UnitDependency d; |
d1fab3fe | 871 | const char *other_id = NULL; |
09a65f92 | 872 | int r; |
87f0e418 LP |
873 | |
874 | assert(u); | |
875 | assert(other); | |
ac155bb8 MS |
876 | assert(u->manager == other->manager); |
877 | assert(u->type != _UNIT_TYPE_INVALID); | |
87f0e418 | 878 | |
cc916967 LP |
879 | other = unit_follow_merge(other); |
880 | ||
23a177ef LP |
881 | if (other == u) |
882 | return 0; | |
883 | ||
ac155bb8 | 884 | if (u->type != other->type) |
9e2f7c11 LP |
885 | return -EINVAL; |
886 | ||
ac155bb8 | 887 | if (!u->instance != !other->instance) |
87f0e418 LP |
888 | return -EINVAL; |
889 | ||
8a993b61 | 890 | if (!unit_type_may_alias(u->type)) /* Merging only applies to unit names that support aliases */ |
934e749e LP |
891 | return -EEXIST; |
892 | ||
ec2ce0c5 | 893 | if (!IN_SET(other->load_state, UNIT_STUB, UNIT_NOT_FOUND)) |
23a177ef | 894 | return -EEXIST; |
87f0e418 | 895 | |
ac155bb8 | 896 | if (other->job) |
819e213f LP |
897 | return -EEXIST; |
898 | ||
e0209d83 MS |
899 | if (other->nop_job) |
900 | return -EEXIST; | |
901 | ||
fdf20a31 | 902 | if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) |
819e213f LP |
903 | return -EEXIST; |
904 | ||
d1fab3fe ZJS |
905 | if (other->id) |
906 | other_id = strdupa(other->id); | |
907 | ||
09a65f92 MS |
908 | /* Make reservations to ensure merge_dependencies() won't fail */ |
909 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { | |
910 | r = reserve_dependencies(u, other, d); | |
911 | /* | |
912 | * We don't rollback reservations if we fail. We don't have | |
913 | * a way to undo reservations. A reservation is not a leak. | |
914 | */ | |
915 | if (r < 0) | |
916 | return r; | |
917 | } | |
918 | ||
87f0e418 | 919 | /* Merge names */ |
7c0b05e5 MS |
920 | r = merge_names(u, other); |
921 | if (r < 0) | |
922 | return r; | |
87f0e418 | 923 | |
57020a3a | 924 | /* Redirect all references */ |
7f7d01ed ZJS |
925 | while (other->refs_by_target) |
926 | unit_ref_set(other->refs_by_target, other->refs_by_target->source, u); | |
57020a3a | 927 | |
87f0e418 LP |
928 | /* Merge dependencies */ |
929 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
d1fab3fe | 930 | merge_dependencies(u, other, other_id, d); |
87f0e418 | 931 | |
ac155bb8 MS |
932 | other->load_state = UNIT_MERGED; |
933 | other->merged_into = u; | |
23a177ef | 934 | |
3616a49c LP |
935 | /* If there is still some data attached to the other node, we |
936 | * don't need it anymore, and can free it. */ | |
ac155bb8 | 937 | if (other->load_state != UNIT_STUB) |
3616a49c LP |
938 | if (UNIT_VTABLE(other)->done) |
939 | UNIT_VTABLE(other)->done(other); | |
940 | ||
941 | unit_add_to_dbus_queue(u); | |
23a177ef LP |
942 | unit_add_to_cleanup_queue(other); |
943 | ||
944 | return 0; | |
945 | } | |
946 | ||
947 | int unit_merge_by_name(Unit *u, const char *name) { | |
934e749e | 948 | _cleanup_free_ char *s = NULL; |
23a177ef | 949 | Unit *other; |
9e2f7c11 | 950 | int r; |
23a177ef LP |
951 | |
952 | assert(u); | |
953 | assert(name); | |
954 | ||
7410616c | 955 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
ac155bb8 | 956 | if (!u->instance) |
9e2f7c11 LP |
957 | return -EINVAL; |
958 | ||
7410616c LP |
959 | r = unit_name_replace_instance(name, u->instance, &s); |
960 | if (r < 0) | |
961 | return r; | |
9e2f7c11 LP |
962 | |
963 | name = s; | |
964 | } | |
965 | ||
c2756a68 | 966 | other = manager_get_unit(u->manager, name); |
7410616c LP |
967 | if (other) |
968 | return unit_merge(u, other); | |
23a177ef | 969 | |
7410616c | 970 | return unit_add_name(u, name); |
23a177ef LP |
971 | } |
972 | ||
973 | Unit* unit_follow_merge(Unit *u) { | |
974 | assert(u); | |
975 | ||
ac155bb8 MS |
976 | while (u->load_state == UNIT_MERGED) |
977 | assert_se(u = u->merged_into); | |
23a177ef LP |
978 | |
979 | return u; | |
980 | } | |
981 | ||
982 | int unit_add_exec_dependencies(Unit *u, ExecContext *c) { | |
ada5e276 YW |
983 | ExecDirectoryType dt; |
984 | char **dp; | |
23a177ef LP |
985 | int r; |
986 | ||
987 | assert(u); | |
988 | assert(c); | |
989 | ||
e1e74614 | 990 | if (c->working_directory && !c->working_directory_missing_ok) { |
eef85c4a | 991 | r = unit_require_mounts_for(u, c->working_directory, UNIT_DEPENDENCY_FILE); |
36be24c8 ZJS |
992 | if (r < 0) |
993 | return r; | |
994 | } | |
995 | ||
996 | if (c->root_directory) { | |
eef85c4a | 997 | r = unit_require_mounts_for(u, c->root_directory, UNIT_DEPENDENCY_FILE); |
36be24c8 ZJS |
998 | if (r < 0) |
999 | return r; | |
1000 | } | |
1001 | ||
915e6d16 | 1002 | if (c->root_image) { |
eef85c4a | 1003 | r = unit_require_mounts_for(u, c->root_image, UNIT_DEPENDENCY_FILE); |
915e6d16 LP |
1004 | if (r < 0) |
1005 | return r; | |
1006 | } | |
1007 | ||
72fd1768 | 1008 | for (dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++) { |
ada5e276 YW |
1009 | if (!u->manager->prefix[dt]) |
1010 | continue; | |
1011 | ||
1012 | STRV_FOREACH(dp, c->directories[dt].paths) { | |
1013 | _cleanup_free_ char *p; | |
1014 | ||
657ee2d8 | 1015 | p = path_join(u->manager->prefix[dt], *dp); |
ada5e276 YW |
1016 | if (!p) |
1017 | return -ENOMEM; | |
1018 | ||
eef85c4a | 1019 | r = unit_require_mounts_for(u, p, UNIT_DEPENDENCY_FILE); |
ada5e276 YW |
1020 | if (r < 0) |
1021 | return r; | |
1022 | } | |
1023 | } | |
1024 | ||
463d0d15 | 1025 | if (!MANAGER_IS_SYSTEM(u->manager)) |
b46a529c LP |
1026 | return 0; |
1027 | ||
1028 | if (c->private_tmp) { | |
d71f0505 LP |
1029 | const char *p; |
1030 | ||
1031 | FOREACH_STRING(p, "/tmp", "/var/tmp") { | |
eef85c4a | 1032 | r = unit_require_mounts_for(u, p, UNIT_DEPENDENCY_FILE); |
d71f0505 LP |
1033 | if (r < 0) |
1034 | return r; | |
1035 | } | |
b46a529c | 1036 | |
35d8c19a | 1037 | r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_TMPFILES_SETUP_SERVICE, true, UNIT_DEPENDENCY_FILE); |
b46a529c LP |
1038 | if (r < 0) |
1039 | return r; | |
1040 | } | |
1041 | ||
52c239d7 LB |
1042 | if (!IN_SET(c->std_output, |
1043 | EXEC_OUTPUT_JOURNAL, EXEC_OUTPUT_JOURNAL_AND_CONSOLE, | |
1044 | EXEC_OUTPUT_KMSG, EXEC_OUTPUT_KMSG_AND_CONSOLE, | |
1045 | EXEC_OUTPUT_SYSLOG, EXEC_OUTPUT_SYSLOG_AND_CONSOLE) && | |
1046 | !IN_SET(c->std_error, | |
1047 | EXEC_OUTPUT_JOURNAL, EXEC_OUTPUT_JOURNAL_AND_CONSOLE, | |
1048 | EXEC_OUTPUT_KMSG, EXEC_OUTPUT_KMSG_AND_CONSOLE, | |
1049 | EXEC_OUTPUT_SYSLOG, EXEC_OUTPUT_SYSLOG_AND_CONSOLE)) | |
23a177ef LP |
1050 | return 0; |
1051 | ||
1052 | /* If syslog or kernel logging is requested, make sure our own | |
1053 | * logging daemon is run first. */ | |
1054 | ||
35d8c19a | 1055 | r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_JOURNALD_SOCKET, true, UNIT_DEPENDENCY_FILE); |
b46a529c LP |
1056 | if (r < 0) |
1057 | return r; | |
23a177ef | 1058 | |
87f0e418 LP |
1059 | return 0; |
1060 | } | |
1061 | ||
87f0e418 LP |
1062 | const char *unit_description(Unit *u) { |
1063 | assert(u); | |
1064 | ||
ac155bb8 MS |
1065 | if (u->description) |
1066 | return u->description; | |
87f0e418 | 1067 | |
ac155bb8 | 1068 | return strna(u->id); |
87f0e418 LP |
1069 | } |
1070 | ||
2a8f53c6 ZJS |
1071 | const char *unit_status_string(Unit *u) { |
1072 | assert(u); | |
1073 | ||
1074 | if (u->manager->status_unit_format == STATUS_UNIT_FORMAT_NAME && u->id) | |
1075 | return u->id; | |
1076 | ||
1077 | return unit_description(u); | |
1078 | } | |
1079 | ||
eef85c4a LP |
1080 | static void print_unit_dependency_mask(FILE *f, const char *kind, UnitDependencyMask mask, bool *space) { |
1081 | const struct { | |
1082 | UnitDependencyMask mask; | |
1083 | const char *name; | |
1084 | } table[] = { | |
1085 | { UNIT_DEPENDENCY_FILE, "file" }, | |
1086 | { UNIT_DEPENDENCY_IMPLICIT, "implicit" }, | |
1087 | { UNIT_DEPENDENCY_DEFAULT, "default" }, | |
1088 | { UNIT_DEPENDENCY_UDEV, "udev" }, | |
1089 | { UNIT_DEPENDENCY_PATH, "path" }, | |
1090 | { UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT, "mountinfo-implicit" }, | |
1091 | { UNIT_DEPENDENCY_MOUNTINFO_DEFAULT, "mountinfo-default" }, | |
1092 | { UNIT_DEPENDENCY_PROC_SWAP, "proc-swap" }, | |
1093 | }; | |
1094 | size_t i; | |
1095 | ||
1096 | assert(f); | |
1097 | assert(kind); | |
1098 | assert(space); | |
1099 | ||
1100 | for (i = 0; i < ELEMENTSOF(table); i++) { | |
1101 | ||
1102 | if (mask == 0) | |
1103 | break; | |
1104 | ||
d94a24ca | 1105 | if (FLAGS_SET(mask, table[i].mask)) { |
eef85c4a LP |
1106 | if (*space) |
1107 | fputc(' ', f); | |
1108 | else | |
1109 | *space = true; | |
1110 | ||
1111 | fputs(kind, f); | |
1112 | fputs("-", f); | |
1113 | fputs(table[i].name, f); | |
1114 | ||
1115 | mask &= ~table[i].mask; | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | assert(mask == 0); | |
1120 | } | |
1121 | ||
87f0e418 | 1122 | void unit_dump(Unit *u, FILE *f, const char *prefix) { |
49dbfa7b | 1123 | char *t, **j; |
87f0e418 LP |
1124 | UnitDependency d; |
1125 | Iterator i; | |
47be870b | 1126 | const char *prefix2; |
8d5e5931 | 1127 | char timestamp[5][FORMAT_TIMESTAMP_MAX], timespan[FORMAT_TIMESPAN_MAX]; |
a7f241db | 1128 | Unit *following; |
eeaedb7c | 1129 | _cleanup_set_free_ Set *following_set = NULL; |
05a98afd | 1130 | const char *n; |
02638280 LP |
1131 | CGroupMask m; |
1132 | int r; | |
87f0e418 LP |
1133 | |
1134 | assert(u); | |
ac155bb8 | 1135 | assert(u->type >= 0); |
87f0e418 | 1136 | |
4c940960 | 1137 | prefix = strempty(prefix); |
63c372cb | 1138 | prefix2 = strjoina(prefix, "\t"); |
87f0e418 LP |
1139 | |
1140 | fprintf(f, | |
0121d1f2 ZJS |
1141 | "%s-> Unit %s:\n", |
1142 | prefix, u->id); | |
1143 | ||
1144 | SET_FOREACH(t, u->names, i) | |
1145 | if (!streq(t, u->id)) | |
1146 | fprintf(f, "%s\tAlias: %s\n", prefix, t); | |
1147 | ||
1148 | fprintf(f, | |
87f0e418 | 1149 | "%s\tDescription: %s\n" |
9e2f7c11 | 1150 | "%s\tInstance: %s\n" |
87f0e418 | 1151 | "%s\tUnit Load State: %s\n" |
2fad8195 | 1152 | "%s\tUnit Active State: %s\n" |
b895d155 | 1153 | "%s\tState Change Timestamp: %s\n" |
173e3821 | 1154 | "%s\tInactive Exit Timestamp: %s\n" |
2fad8195 | 1155 | "%s\tActive Enter Timestamp: %s\n" |
701cc384 | 1156 | "%s\tActive Exit Timestamp: %s\n" |
173e3821 | 1157 | "%s\tInactive Enter Timestamp: %s\n" |
f2f725e5 | 1158 | "%s\tMay GC: %s\n" |
9444b1f2 | 1159 | "%s\tNeed Daemon Reload: %s\n" |
c2756a68 | 1160 | "%s\tTransient: %s\n" |
f5869324 | 1161 | "%s\tPerpetual: %s\n" |
5afe510c | 1162 | "%s\tGarbage Collection Mode: %s\n" |
4ad49000 LP |
1163 | "%s\tSlice: %s\n" |
1164 | "%s\tCGroup: %s\n" | |
aae7e17f | 1165 | "%s\tCGroup realized: %s\n", |
87f0e418 | 1166 | prefix, unit_description(u), |
ac155bb8 MS |
1167 | prefix, strna(u->instance), |
1168 | prefix, unit_load_state_to_string(u->load_state), | |
2fad8195 | 1169 | prefix, unit_active_state_to_string(unit_active_state(u)), |
8d5e5931 ZJS |
1170 | prefix, strna(format_timestamp(timestamp[0], sizeof(timestamp[0]), u->state_change_timestamp.realtime)), |
1171 | prefix, strna(format_timestamp(timestamp[1], sizeof(timestamp[1]), u->inactive_exit_timestamp.realtime)), | |
1172 | prefix, strna(format_timestamp(timestamp[2], sizeof(timestamp[2]), u->active_enter_timestamp.realtime)), | |
1173 | prefix, strna(format_timestamp(timestamp[3], sizeof(timestamp[3]), u->active_exit_timestamp.realtime)), | |
1174 | prefix, strna(format_timestamp(timestamp[4], sizeof(timestamp[4]), u->inactive_enter_timestamp.realtime)), | |
f2f725e5 | 1175 | prefix, yes_no(unit_may_gc(u)), |
9444b1f2 | 1176 | prefix, yes_no(unit_need_daemon_reload(u)), |
c2756a68 | 1177 | prefix, yes_no(u->transient), |
f5869324 | 1178 | prefix, yes_no(u->perpetual), |
5afe510c | 1179 | prefix, collect_mode_to_string(u->collect_mode), |
4ad49000 LP |
1180 | prefix, strna(unit_slice_name(u)), |
1181 | prefix, strna(u->cgroup_path), | |
aae7e17f FB |
1182 | prefix, yes_no(u->cgroup_realized)); |
1183 | ||
1184 | if (u->cgroup_realized_mask != 0) { | |
1185 | _cleanup_free_ char *s = NULL; | |
1186 | (void) cg_mask_to_string(u->cgroup_realized_mask, &s); | |
02638280 LP |
1187 | fprintf(f, "%s\tCGroup realized mask: %s\n", prefix, strnull(s)); |
1188 | } | |
0adf88b6 | 1189 | |
02638280 LP |
1190 | if (u->cgroup_enabled_mask != 0) { |
1191 | _cleanup_free_ char *s = NULL; | |
1192 | (void) cg_mask_to_string(u->cgroup_enabled_mask, &s); | |
1193 | fprintf(f, "%s\tCGroup enabled mask: %s\n", prefix, strnull(s)); | |
1194 | } | |
0adf88b6 | 1195 | |
02638280 LP |
1196 | m = unit_get_own_mask(u); |
1197 | if (m != 0) { | |
1198 | _cleanup_free_ char *s = NULL; | |
1199 | (void) cg_mask_to_string(m, &s); | |
1200 | fprintf(f, "%s\tCGroup own mask: %s\n", prefix, strnull(s)); | |
aae7e17f | 1201 | } |
0adf88b6 | 1202 | |
02638280 LP |
1203 | m = unit_get_members_mask(u); |
1204 | if (m != 0) { | |
aae7e17f | 1205 | _cleanup_free_ char *s = NULL; |
02638280 | 1206 | (void) cg_mask_to_string(m, &s); |
aae7e17f FB |
1207 | fprintf(f, "%s\tCGroup members mask: %s\n", prefix, strnull(s)); |
1208 | } | |
0301abf4 | 1209 | |
0adf88b6 LP |
1210 | m = unit_get_delegate_mask(u); |
1211 | if (m != 0) { | |
1212 | _cleanup_free_ char *s = NULL; | |
1213 | (void) cg_mask_to_string(m, &s); | |
1214 | fprintf(f, "%s\tCGroup delegate mask: %s\n", prefix, strnull(s)); | |
1215 | } | |
1216 | ||
4b58153d LP |
1217 | if (!sd_id128_is_null(u->invocation_id)) |
1218 | fprintf(f, "%s\tInvocation ID: " SD_ID128_FORMAT_STR "\n", | |
1219 | prefix, SD_ID128_FORMAT_VAL(u->invocation_id)); | |
1220 | ||
49dbfa7b LP |
1221 | STRV_FOREACH(j, u->documentation) |
1222 | fprintf(f, "%s\tDocumentation: %s\n", prefix, *j); | |
1223 | ||
eeaedb7c LP |
1224 | following = unit_following(u); |
1225 | if (following) | |
ac155bb8 | 1226 | fprintf(f, "%s\tFollowing: %s\n", prefix, following->id); |
8fe914ec | 1227 | |
eeaedb7c LP |
1228 | r = unit_following_set(u, &following_set); |
1229 | if (r >= 0) { | |
1230 | Unit *other; | |
1231 | ||
1232 | SET_FOREACH(other, following_set, i) | |
1233 | fprintf(f, "%s\tFollowing Set Member: %s\n", prefix, other->id); | |
1234 | } | |
1235 | ||
ac155bb8 MS |
1236 | if (u->fragment_path) |
1237 | fprintf(f, "%s\tFragment Path: %s\n", prefix, u->fragment_path); | |
23a177ef | 1238 | |
1b64d026 LP |
1239 | if (u->source_path) |
1240 | fprintf(f, "%s\tSource Path: %s\n", prefix, u->source_path); | |
1241 | ||
ae7a7182 | 1242 | STRV_FOREACH(j, u->dropin_paths) |
2875e22b | 1243 | fprintf(f, "%s\tDropIn Path: %s\n", prefix, *j); |
ae7a7182 | 1244 | |
e7dfbb4e LP |
1245 | if (u->failure_action != EMERGENCY_ACTION_NONE) |
1246 | fprintf(f, "%s\tFailure Action: %s\n", prefix, emergency_action_to_string(u->failure_action)); | |
7af67e9a LP |
1247 | if (u->failure_action_exit_status >= 0) |
1248 | fprintf(f, "%s\tFailure Action Exit Status: %i\n", prefix, u->failure_action_exit_status); | |
e7dfbb4e LP |
1249 | if (u->success_action != EMERGENCY_ACTION_NONE) |
1250 | fprintf(f, "%s\tSuccess Action: %s\n", prefix, emergency_action_to_string(u->success_action)); | |
7af67e9a LP |
1251 | if (u->success_action_exit_status >= 0) |
1252 | fprintf(f, "%s\tSuccess Action Exit Status: %i\n", prefix, u->success_action_exit_status); | |
e7dfbb4e | 1253 | |
36c16a7c | 1254 | if (u->job_timeout != USEC_INFINITY) |
2fa4092c | 1255 | fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->job_timeout, 0)); |
faf919f1 | 1256 | |
87a47f99 LN |
1257 | if (u->job_timeout_action != EMERGENCY_ACTION_NONE) |
1258 | fprintf(f, "%s\tJob Timeout Action: %s\n", prefix, emergency_action_to_string(u->job_timeout_action)); | |
f189ab18 LP |
1259 | |
1260 | if (u->job_timeout_reboot_arg) | |
1261 | fprintf(f, "%s\tJob Timeout Reboot Argument: %s\n", prefix, u->job_timeout_reboot_arg); | |
1262 | ||
59fccdc5 LP |
1263 | condition_dump_list(u->conditions, f, prefix, condition_type_to_string); |
1264 | condition_dump_list(u->asserts, f, prefix, assert_type_to_string); | |
52661efd | 1265 | |
ac155bb8 | 1266 | if (dual_timestamp_is_set(&u->condition_timestamp)) |
2791a8f8 LP |
1267 | fprintf(f, |
1268 | "%s\tCondition Timestamp: %s\n" | |
1269 | "%s\tCondition Result: %s\n", | |
8d5e5931 | 1270 | prefix, strna(format_timestamp(timestamp[0], sizeof(timestamp[0]), u->condition_timestamp.realtime)), |
ac155bb8 | 1271 | prefix, yes_no(u->condition_result)); |
2791a8f8 | 1272 | |
59fccdc5 LP |
1273 | if (dual_timestamp_is_set(&u->assert_timestamp)) |
1274 | fprintf(f, | |
1275 | "%s\tAssert Timestamp: %s\n" | |
1276 | "%s\tAssert Result: %s\n", | |
8d5e5931 | 1277 | prefix, strna(format_timestamp(timestamp[0], sizeof(timestamp[0]), u->assert_timestamp.realtime)), |
59fccdc5 LP |
1278 | prefix, yes_no(u->assert_result)); |
1279 | ||
87f0e418 | 1280 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { |
eef85c4a | 1281 | UnitDependencyInfo di; |
87f0e418 LP |
1282 | Unit *other; |
1283 | ||
eef85c4a LP |
1284 | HASHMAP_FOREACH_KEY(di.data, other, u->dependencies[d], i) { |
1285 | bool space = false; | |
1286 | ||
1287 | fprintf(f, "%s\t%s: %s (", prefix, unit_dependency_to_string(d), other->id); | |
1288 | ||
1289 | print_unit_dependency_mask(f, "origin", di.origin_mask, &space); | |
1290 | print_unit_dependency_mask(f, "destination", di.destination_mask, &space); | |
1291 | ||
1292 | fputs(")\n", f); | |
1293 | } | |
87f0e418 LP |
1294 | } |
1295 | ||
eef85c4a LP |
1296 | if (!hashmap_isempty(u->requires_mounts_for)) { |
1297 | UnitDependencyInfo di; | |
1298 | const char *path; | |
1299 | ||
1300 | HASHMAP_FOREACH_KEY(di.data, path, u->requires_mounts_for, i) { | |
1301 | bool space = false; | |
7c8fa05c | 1302 | |
eef85c4a | 1303 | fprintf(f, "%s\tRequiresMountsFor: %s (", prefix, path); |
7c8fa05c | 1304 | |
eef85c4a LP |
1305 | print_unit_dependency_mask(f, "origin", di.origin_mask, &space); |
1306 | print_unit_dependency_mask(f, "destination", di.destination_mask, &space); | |
1307 | ||
1308 | fputs(")\n", f); | |
1309 | } | |
7c8fa05c LP |
1310 | } |
1311 | ||
ac155bb8 | 1312 | if (u->load_state == UNIT_LOADED) { |
ab1f0633 | 1313 | |
b0650475 | 1314 | fprintf(f, |
a40eb732 | 1315 | "%s\tStopWhenUnneeded: %s\n" |
b5e9dba8 LP |
1316 | "%s\tRefuseManualStart: %s\n" |
1317 | "%s\tRefuseManualStop: %s\n" | |
222ae6a8 | 1318 | "%s\tDefaultDependencies: %s\n" |
d420282b | 1319 | "%s\tOnFailureJobMode: %s\n" |
36b4a7ba | 1320 | "%s\tIgnoreOnIsolate: %s\n", |
ac155bb8 MS |
1321 | prefix, yes_no(u->stop_when_unneeded), |
1322 | prefix, yes_no(u->refuse_manual_start), | |
1323 | prefix, yes_no(u->refuse_manual_stop), | |
1324 | prefix, yes_no(u->default_dependencies), | |
d420282b | 1325 | prefix, job_mode_to_string(u->on_failure_job_mode), |
36b4a7ba | 1326 | prefix, yes_no(u->ignore_on_isolate)); |
ac155bb8 | 1327 | |
23a177ef LP |
1328 | if (UNIT_VTABLE(u)->dump) |
1329 | UNIT_VTABLE(u)->dump(u, f, prefix2); | |
b0650475 | 1330 | |
ac155bb8 | 1331 | } else if (u->load_state == UNIT_MERGED) |
b0650475 LP |
1332 | fprintf(f, |
1333 | "%s\tMerged into: %s\n", | |
ac155bb8 MS |
1334 | prefix, u->merged_into->id); |
1335 | else if (u->load_state == UNIT_ERROR) | |
4bbccb02 | 1336 | fprintf(f, "%s\tLoad Error Code: %s\n", prefix, strerror_safe(u->load_error)); |
8821a00f | 1337 | |
05a98afd LP |
1338 | for (n = sd_bus_track_first(u->bus_track); n; n = sd_bus_track_next(u->bus_track)) |
1339 | fprintf(f, "%s\tBus Ref: %s\n", prefix, n); | |
87f0e418 | 1340 | |
ac155bb8 MS |
1341 | if (u->job) |
1342 | job_dump(u->job, f, prefix2); | |
87f0e418 | 1343 | |
e0209d83 MS |
1344 | if (u->nop_job) |
1345 | job_dump(u->nop_job, f, prefix2); | |
87f0e418 LP |
1346 | } |
1347 | ||
1348 | /* Common implementation for multiple backends */ | |
e537352b | 1349 | int unit_load_fragment_and_dropin(Unit *u) { |
23a177ef LP |
1350 | int r; |
1351 | ||
1352 | assert(u); | |
23a177ef | 1353 | |
e48614c4 | 1354 | /* Load a .{service,socket,...} file */ |
4ad49000 LP |
1355 | r = unit_load_fragment(u); |
1356 | if (r < 0) | |
23a177ef LP |
1357 | return r; |
1358 | ||
ac155bb8 | 1359 | if (u->load_state == UNIT_STUB) |
23a177ef LP |
1360 | return -ENOENT; |
1361 | ||
9e4ea9cc ZJS |
1362 | /* Load drop-in directory data. If u is an alias, we might be reloading the |
1363 | * target unit needlessly. But we cannot be sure which drops-ins have already | |
1364 | * been loaded and which not, at least without doing complicated book-keeping, | |
1365 | * so let's always reread all drop-ins. */ | |
1366 | return unit_load_dropin(unit_follow_merge(u)); | |
23a177ef LP |
1367 | } |
1368 | ||
1369 | /* Common implementation for multiple backends */ | |
e537352b | 1370 | int unit_load_fragment_and_dropin_optional(Unit *u) { |
23a177ef | 1371 | int r; |
87f0e418 LP |
1372 | |
1373 | assert(u); | |
1374 | ||
23a177ef LP |
1375 | /* Same as unit_load_fragment_and_dropin(), but whether |
1376 | * something can be loaded or not doesn't matter. */ | |
1377 | ||
e9e8cbc8 | 1378 | /* Load a .service/.socket/.slice/… file */ |
4ad49000 LP |
1379 | r = unit_load_fragment(u); |
1380 | if (r < 0) | |
87f0e418 LP |
1381 | return r; |
1382 | ||
ac155bb8 MS |
1383 | if (u->load_state == UNIT_STUB) |
1384 | u->load_state = UNIT_LOADED; | |
d46de8a1 | 1385 | |
9e4ea9cc ZJS |
1386 | /* Load drop-in directory data */ |
1387 | return unit_load_dropin(unit_follow_merge(u)); | |
87f0e418 LP |
1388 | } |
1389 | ||
19496554 MS |
1390 | void unit_add_to_target_deps_queue(Unit *u) { |
1391 | Manager *m = u->manager; | |
1392 | ||
1393 | assert(u); | |
1394 | ||
1395 | if (u->in_target_deps_queue) | |
1396 | return; | |
1397 | ||
1398 | LIST_PREPEND(target_deps_queue, m->target_deps_queue, u); | |
1399 | u->in_target_deps_queue = true; | |
1400 | } | |
1401 | ||
bba34eed | 1402 | int unit_add_default_target_dependency(Unit *u, Unit *target) { |
98bc2000 LP |
1403 | assert(u); |
1404 | assert(target); | |
1405 | ||
ac155bb8 | 1406 | if (target->type != UNIT_TARGET) |
98bc2000 LP |
1407 | return 0; |
1408 | ||
35b8ca3a | 1409 | /* Only add the dependency if both units are loaded, so that |
bba34eed | 1410 | * that loop check below is reliable */ |
ac155bb8 MS |
1411 | if (u->load_state != UNIT_LOADED || |
1412 | target->load_state != UNIT_LOADED) | |
bba34eed LP |
1413 | return 0; |
1414 | ||
21256a2b LP |
1415 | /* If either side wants no automatic dependencies, then let's |
1416 | * skip this */ | |
ac155bb8 MS |
1417 | if (!u->default_dependencies || |
1418 | !target->default_dependencies) | |
21256a2b LP |
1419 | return 0; |
1420 | ||
98bc2000 | 1421 | /* Don't create loops */ |
eef85c4a | 1422 | if (hashmap_get(target->dependencies[UNIT_BEFORE], u)) |
98bc2000 LP |
1423 | return 0; |
1424 | ||
eef85c4a | 1425 | return unit_add_dependency(target, UNIT_AFTER, u, true, UNIT_DEPENDENCY_DEFAULT); |
98bc2000 LP |
1426 | } |
1427 | ||
e954c9cf | 1428 | static int unit_add_slice_dependencies(Unit *u) { |
eef85c4a | 1429 | UnitDependencyMask mask; |
e954c9cf | 1430 | assert(u); |
b81884e7 | 1431 | |
35b7ff80 | 1432 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) |
e954c9cf LP |
1433 | return 0; |
1434 | ||
eef85c4a LP |
1435 | /* Slice units are implicitly ordered against their parent slices (as this relationship is encoded in the |
1436 | name), while all other units are ordered based on configuration (as in their case Slice= configures the | |
1437 | relationship). */ | |
1438 | mask = u->type == UNIT_SLICE ? UNIT_DEPENDENCY_IMPLICIT : UNIT_DEPENDENCY_FILE; | |
1439 | ||
e954c9cf | 1440 | if (UNIT_ISSET(u->slice)) |
eef85c4a | 1441 | return unit_add_two_dependencies(u, UNIT_AFTER, UNIT_REQUIRES, UNIT_DEREF(u->slice), true, mask); |
e954c9cf | 1442 | |
8c8da0e0 | 1443 | if (unit_has_name(u, SPECIAL_ROOT_SLICE)) |
d1fab3fe ZJS |
1444 | return 0; |
1445 | ||
5a724170 | 1446 | return unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_ROOT_SLICE, true, mask); |
98bc2000 LP |
1447 | } |
1448 | ||
e954c9cf | 1449 | static int unit_add_mount_dependencies(Unit *u) { |
eef85c4a LP |
1450 | UnitDependencyInfo di; |
1451 | const char *path; | |
1452 | Iterator i; | |
9588bc32 LP |
1453 | int r; |
1454 | ||
1455 | assert(u); | |
1456 | ||
eef85c4a LP |
1457 | HASHMAP_FOREACH_KEY(di.data, path, u->requires_mounts_for, i) { |
1458 | char prefix[strlen(path) + 1]; | |
9588bc32 | 1459 | |
eef85c4a | 1460 | PATH_FOREACH_PREFIX_MORE(prefix, path) { |
c7c89abb | 1461 | _cleanup_free_ char *p = NULL; |
9588bc32 LP |
1462 | Unit *m; |
1463 | ||
c7c89abb | 1464 | r = unit_name_from_path(prefix, ".mount", &p); |
9588bc32 LP |
1465 | if (r < 0) |
1466 | return r; | |
c7c89abb FB |
1467 | |
1468 | m = manager_get_unit(u->manager, p); | |
1469 | if (!m) { | |
1470 | /* Make sure to load the mount unit if | |
1471 | * it exists. If so the dependencies | |
1472 | * on this unit will be added later | |
1473 | * during the loading of the mount | |
1474 | * unit. */ | |
1475 | (void) manager_load_unit_prepare(u->manager, p, NULL, NULL, &m); | |
9588bc32 | 1476 | continue; |
c7c89abb | 1477 | } |
9588bc32 LP |
1478 | if (m == u) |
1479 | continue; | |
1480 | ||
1481 | if (m->load_state != UNIT_LOADED) | |
1482 | continue; | |
1483 | ||
eef85c4a | 1484 | r = unit_add_dependency(u, UNIT_AFTER, m, true, di.origin_mask); |
9588bc32 LP |
1485 | if (r < 0) |
1486 | return r; | |
1487 | ||
1488 | if (m->fragment_path) { | |
eef85c4a | 1489 | r = unit_add_dependency(u, UNIT_REQUIRES, m, true, di.origin_mask); |
9588bc32 LP |
1490 | if (r < 0) |
1491 | return r; | |
1492 | } | |
1493 | } | |
1494 | } | |
1495 | ||
1496 | return 0; | |
1497 | } | |
1498 | ||
95ae05c0 WC |
1499 | static int unit_add_startup_units(Unit *u) { |
1500 | CGroupContext *c; | |
5269eb6b | 1501 | int r; |
95ae05c0 WC |
1502 | |
1503 | c = unit_get_cgroup_context(u); | |
db785129 LP |
1504 | if (!c) |
1505 | return 0; | |
1506 | ||
d53d9474 | 1507 | if (c->startup_cpu_shares == CGROUP_CPU_SHARES_INVALID && |
13c31542 | 1508 | c->startup_io_weight == CGROUP_WEIGHT_INVALID && |
d53d9474 | 1509 | c->startup_blockio_weight == CGROUP_BLKIO_WEIGHT_INVALID) |
db785129 LP |
1510 | return 0; |
1511 | ||
5269eb6b LP |
1512 | r = set_ensure_allocated(&u->manager->startup_units, NULL); |
1513 | if (r < 0) | |
1514 | return r; | |
1515 | ||
756c09e6 | 1516 | return set_put(u->manager->startup_units, u); |
95ae05c0 WC |
1517 | } |
1518 | ||
87f0e418 LP |
1519 | int unit_load(Unit *u) { |
1520 | int r; | |
1521 | ||
1522 | assert(u); | |
1523 | ||
ac155bb8 | 1524 | if (u->in_load_queue) { |
71fda00f | 1525 | LIST_REMOVE(load_queue, u->manager->load_queue, u); |
ac155bb8 | 1526 | u->in_load_queue = false; |
87f0e418 LP |
1527 | } |
1528 | ||
ac155bb8 | 1529 | if (u->type == _UNIT_TYPE_INVALID) |
e537352b LP |
1530 | return -EINVAL; |
1531 | ||
ac155bb8 | 1532 | if (u->load_state != UNIT_STUB) |
87f0e418 LP |
1533 | return 0; |
1534 | ||
4f4afc88 | 1535 | if (u->transient_file) { |
66fa4bdd LP |
1536 | /* Finalize transient file: if this is a transient unit file, as soon as we reach unit_load() the setup |
1537 | * is complete, hence let's synchronize the unit file we just wrote to disk. */ | |
1538 | ||
4f4afc88 LP |
1539 | r = fflush_and_check(u->transient_file); |
1540 | if (r < 0) | |
1541 | goto fail; | |
1542 | ||
50fb00b7 | 1543 | u->transient_file = safe_fclose(u->transient_file); |
f76707da | 1544 | u->fragment_mtime = now(CLOCK_REALTIME); |
4f4afc88 LP |
1545 | } |
1546 | ||
c2756a68 LP |
1547 | if (UNIT_VTABLE(u)->load) { |
1548 | r = UNIT_VTABLE(u)->load(u); | |
1549 | if (r < 0) | |
87f0e418 | 1550 | goto fail; |
c2756a68 | 1551 | } |
23a177ef | 1552 | |
ac155bb8 | 1553 | if (u->load_state == UNIT_STUB) { |
23a177ef LP |
1554 | r = -ENOENT; |
1555 | goto fail; | |
1556 | } | |
1557 | ||
7c8fa05c | 1558 | if (u->load_state == UNIT_LOADED) { |
19496554 | 1559 | unit_add_to_target_deps_queue(u); |
e954c9cf LP |
1560 | |
1561 | r = unit_add_slice_dependencies(u); | |
1562 | if (r < 0) | |
1563 | goto fail; | |
c2756a68 | 1564 | |
e954c9cf | 1565 | r = unit_add_mount_dependencies(u); |
7c8fa05c | 1566 | if (r < 0) |
c2756a68 | 1567 | goto fail; |
7c8fa05c | 1568 | |
95ae05c0 WC |
1569 | r = unit_add_startup_units(u); |
1570 | if (r < 0) | |
1571 | goto fail; | |
1572 | ||
eef85c4a | 1573 | if (u->on_failure_job_mode == JOB_ISOLATE && hashmap_size(u->dependencies[UNIT_ON_FAILURE]) > 1) { |
f2341e0a | 1574 | log_unit_error(u, "More than one OnFailure= dependencies specified but OnFailureJobMode=isolate set. Refusing."); |
6f40aa45 | 1575 | r = -ENOEXEC; |
c2756a68 LP |
1576 | goto fail; |
1577 | } | |
bc432dc7 | 1578 | |
a2df3ea4 MK |
1579 | if (u->job_running_timeout != USEC_INFINITY && u->job_running_timeout > u->job_timeout) |
1580 | log_unit_warning(u, "JobRunningTimeoutSec= is greater than JobTimeoutSec=, it has no effect."); | |
1581 | ||
5af88058 LP |
1582 | /* We finished loading, let's ensure our parents recalculate the members mask */ |
1583 | unit_invalidate_cgroup_members_masks(u); | |
f68319bb LP |
1584 | } |
1585 | ||
ac155bb8 | 1586 | assert((u->load_state != UNIT_MERGED) == !u->merged_into); |
23a177ef LP |
1587 | |
1588 | unit_add_to_dbus_queue(unit_follow_merge(u)); | |
701cc384 | 1589 | unit_add_to_gc_queue(u); |
87f0e418 | 1590 | |
87f0e418 LP |
1591 | return 0; |
1592 | ||
1593 | fail: | |
c4555ad8 LP |
1594 | /* We convert ENOEXEC errors to the UNIT_BAD_SETTING load state here. Configuration parsing code should hence |
1595 | * return ENOEXEC to ensure units are placed in this state after loading */ | |
1596 | ||
1597 | u->load_state = u->load_state == UNIT_STUB ? UNIT_NOT_FOUND : | |
1598 | r == -ENOEXEC ? UNIT_BAD_SETTING : | |
1599 | UNIT_ERROR; | |
ac155bb8 | 1600 | u->load_error = r; |
c4555ad8 | 1601 | |
c1e1601e | 1602 | unit_add_to_dbus_queue(u); |
9a46fc3b | 1603 | unit_add_to_gc_queue(u); |
23a177ef | 1604 | |
c4555ad8 | 1605 | return log_unit_debug_errno(u, r, "Failed to load configuration: %m"); |
87f0e418 LP |
1606 | } |
1607 | ||
f9f88198 YW |
1608 | _printf_(7, 8) |
1609 | static int log_unit_internal(void *userdata, int level, int error, const char *file, int line, const char *func, const char *format, ...) { | |
1610 | Unit *u = userdata; | |
1611 | va_list ap; | |
1612 | int r; | |
49365733 | 1613 | |
f9f88198 YW |
1614 | va_start(ap, format); |
1615 | if (u) | |
1616 | r = log_object_internalv(level, error, file, line, func, | |
1617 | u->manager->unit_log_field, | |
1618 | u->id, | |
1619 | u->manager->invocation_log_field, | |
1620 | u->invocation_id_string, | |
1621 | format, ap); | |
1622 | else | |
1623 | r = log_internalv(level, error, file, line, func, format, ap); | |
1624 | va_end(ap); | |
49365733 | 1625 | |
f9f88198 | 1626 | return r; |
49365733 LP |
1627 | } |
1628 | ||
97a3f4ee | 1629 | static bool unit_test_condition(Unit *u) { |
90bbc946 LP |
1630 | assert(u); |
1631 | ||
ac155bb8 | 1632 | dual_timestamp_get(&u->condition_timestamp); |
f9f88198 | 1633 | u->condition_result = condition_test_list(u->conditions, condition_type_to_string, log_unit_internal, u); |
90bbc946 | 1634 | |
e18f8852 LP |
1635 | unit_add_to_dbus_queue(u); |
1636 | ||
ac155bb8 | 1637 | return u->condition_result; |
90bbc946 LP |
1638 | } |
1639 | ||
97a3f4ee | 1640 | static bool unit_test_assert(Unit *u) { |
59fccdc5 LP |
1641 | assert(u); |
1642 | ||
1643 | dual_timestamp_get(&u->assert_timestamp); | |
f9f88198 | 1644 | u->assert_result = condition_test_list(u->asserts, assert_type_to_string, log_unit_internal, u); |
59fccdc5 | 1645 | |
e18f8852 LP |
1646 | unit_add_to_dbus_queue(u); |
1647 | ||
59fccdc5 LP |
1648 | return u->assert_result; |
1649 | } | |
1650 | ||
df446f96 | 1651 | void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg_format) { |
5b262f74 LP |
1652 | const char *d; |
1653 | ||
2a8f53c6 | 1654 | d = unit_status_string(u); |
5b262f74 LP |
1655 | if (log_get_show_color()) |
1656 | d = strjoina(ANSI_HIGHLIGHT, d, ANSI_NORMAL); | |
1657 | ||
df446f96 | 1658 | DISABLE_WARNING_FORMAT_NONLITERAL; |
5b262f74 | 1659 | manager_status_printf(u->manager, STATUS_TYPE_NORMAL, status, unit_status_msg_format, d); |
df446f96 LP |
1660 | REENABLE_WARNING; |
1661 | } | |
1662 | ||
97a3f4ee | 1663 | int unit_test_start_limit(Unit *u) { |
429926e9 TR |
1664 | const char *reason; |
1665 | ||
6bf0f408 LP |
1666 | assert(u); |
1667 | ||
7994ac1d | 1668 | if (ratelimit_below(&u->start_limit)) { |
6bf0f408 LP |
1669 | u->start_limit_hit = false; |
1670 | return 0; | |
1671 | } | |
1672 | ||
1673 | log_unit_warning(u, "Start request repeated too quickly."); | |
1674 | u->start_limit_hit = true; | |
1675 | ||
429926e9 TR |
1676 | reason = strjoina("unit ", u->id, " failed"); |
1677 | ||
36c4dc08 LP |
1678 | emergency_action(u->manager, u->start_limit_action, |
1679 | EMERGENCY_ACTION_IS_WATCHDOG|EMERGENCY_ACTION_WARN, | |
1680 | u->reboot_arg, -1, reason); | |
1681 | ||
1682 | return -ECANCELED; | |
6bf0f408 LP |
1683 | } |
1684 | ||
c891efaf | 1685 | bool unit_shall_confirm_spawn(Unit *u) { |
631b676b | 1686 | assert(u); |
c891efaf FB |
1687 | |
1688 | if (manager_is_confirm_spawn_disabled(u->manager)) | |
1689 | return false; | |
1690 | ||
1691 | /* For some reasons units remaining in the same process group | |
1692 | * as PID 1 fail to acquire the console even if it's not used | |
1693 | * by any process. So skip the confirmation question for them. */ | |
1694 | return !unit_get_exec_context(u)->same_pgrp; | |
1695 | } | |
1696 | ||
631b676b LP |
1697 | static bool unit_verify_deps(Unit *u) { |
1698 | Unit *other; | |
1699 | Iterator j; | |
eef85c4a | 1700 | void *v; |
631b676b LP |
1701 | |
1702 | assert(u); | |
1703 | ||
1704 | /* Checks whether all BindsTo= dependencies of this unit are fulfilled — if they are also combined with | |
1705 | * After=. We do not check Requires= or Requisite= here as they only should have an effect on the job | |
1706 | * processing, but do not have any effect afterwards. We don't check BindsTo= dependencies that are not used in | |
1707 | * conjunction with After= as for them any such check would make things entirely racy. */ | |
1708 | ||
eef85c4a | 1709 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_BINDS_TO], j) { |
631b676b | 1710 | |
eef85c4a | 1711 | if (!hashmap_contains(u->dependencies[UNIT_AFTER], other)) |
631b676b LP |
1712 | continue; |
1713 | ||
1714 | if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(other))) { | |
1715 | log_unit_notice(u, "Bound to unit %s, but unit isn't active.", other->id); | |
1716 | return false; | |
1717 | } | |
1718 | } | |
1719 | ||
1720 | return true; | |
1721 | } | |
1722 | ||
9adb6959 | 1723 | /* Errors that aren't really errors: |
6bf0f408 | 1724 | * -EALREADY: Unit is already started. |
9adb6959 | 1725 | * -ECOMM: Condition failed |
6bf0f408 | 1726 | * -EAGAIN: An operation is already in progress. Retry later. |
9adb6959 LP |
1727 | * |
1728 | * Errors that are real errors: | |
1729 | * -EBADR: This unit type does not support starting. | |
2de9b979 | 1730 | * -ECANCELED: Start limit hit, too many requests for now |
6bf0f408 LP |
1731 | * -EPROTO: Assert failed |
1732 | * -EINVAL: Unit not loaded | |
1733 | * -EOPNOTSUPP: Unit type not supported | |
631b676b | 1734 | * -ENOLINK: The necessary dependencies are not fulfilled. |
d4fd1cf2 | 1735 | * -ESTALE: This unit has been started before and can't be started a second time |
a4191c9f | 1736 | * -ENOENT: This is a triggering unit and unit to trigger is not loaded |
87f0e418 LP |
1737 | */ |
1738 | int unit_start(Unit *u) { | |
1739 | UnitActiveState state; | |
92ab323c | 1740 | Unit *following; |
2de9b979 | 1741 | int r; |
87f0e418 LP |
1742 | |
1743 | assert(u); | |
1744 | ||
5766aca8 LP |
1745 | /* If this is already started, then this will succeed. Note that this will even succeed if this unit |
1746 | * is not startable by the user. This is relied on to detect when we need to wait for units and when | |
1747 | * waiting is finished. */ | |
87f0e418 LP |
1748 | state = unit_active_state(u); |
1749 | if (UNIT_IS_ACTIVE_OR_RELOADING(state)) | |
1750 | return -EALREADY; | |
380dc8b0 LP |
1751 | if (state == UNIT_MAINTENANCE) |
1752 | return -EAGAIN; | |
87f0e418 | 1753 | |
6bf0f408 LP |
1754 | /* Units that aren't loaded cannot be started */ |
1755 | if (u->load_state != UNIT_LOADED) | |
1756 | return -EINVAL; | |
1757 | ||
d4fd1cf2 LP |
1758 | /* Refuse starting scope units more than once */ |
1759 | if (UNIT_VTABLE(u)->once_only && dual_timestamp_is_set(&u->inactive_enter_timestamp)) | |
1760 | return -ESTALE; | |
1761 | ||
5766aca8 LP |
1762 | /* If the conditions failed, don't do anything at all. If we already are activating this call might |
1763 | * still be useful to speed up activation in case there is some hold-off time, but we don't want to | |
1764 | * recheck the condition in that case. */ | |
a82e5507 | 1765 | if (state != UNIT_ACTIVATING && |
2de9b979 LP |
1766 | !unit_test_condition(u)) { |
1767 | ||
1768 | /* Let's also check the start limit here. Normally, the start limit is only checked by the | |
1769 | * .start() method of the unit type after it did some additional checks verifying everything | |
1770 | * is in order (so that those other checks can propagate errors properly). However, if a | |
1771 | * condition check doesn't hold we don't get that far but we should still ensure we are not | |
1772 | * called in a tight loop without a rate limit check enforced, hence do the check here. Note | |
1773 | * that ECOMM is generally not a reason for a job to fail, unlike most other errors here, | |
1774 | * hence the chance is big that any triggering unit for us will trigger us again. Note this | |
1775 | * condition check is a bit different from the condition check inside the per-unit .start() | |
1776 | * function, as this one will not change the unit's state in any way (and we shouldn't here, | |
1777 | * after all the condition failed). */ | |
1778 | ||
1779 | r = unit_test_start_limit(u); | |
1780 | if (r < 0) | |
1781 | return r; | |
1782 | ||
5766aca8 | 1783 | return log_unit_debug_errno(u, SYNTHETIC_ERRNO(ECOMM), "Starting requested but condition failed. Not starting unit."); |
2de9b979 | 1784 | } |
52661efd | 1785 | |
59fccdc5 LP |
1786 | /* If the asserts failed, fail the entire job */ |
1787 | if (state != UNIT_ACTIVATING && | |
5766aca8 LP |
1788 | !unit_test_assert(u)) |
1789 | return log_unit_notice_errno(u, SYNTHETIC_ERRNO(EPROTO), "Starting requested but asserts failed."); | |
59fccdc5 | 1790 | |
5766aca8 LP |
1791 | /* Units of types that aren't supported cannot be started. Note that we do this test only after the |
1792 | * condition checks, so that we rather return condition check errors (which are usually not | |
1793 | * considered a true failure) than "not supported" errors (which are considered a failure). | |
d11a7645 | 1794 | */ |
96cf3ec9 | 1795 | if (!unit_type_supported(u->type)) |
d11a7645 LP |
1796 | return -EOPNOTSUPP; |
1797 | ||
5766aca8 LP |
1798 | /* Let's make sure that the deps really are in order before we start this. Normally the job engine |
1799 | * should have taken care of this already, but let's check this here again. After all, our | |
1800 | * dependencies might not be in effect anymore, due to a reload or due to a failed condition. */ | |
631b676b LP |
1801 | if (!unit_verify_deps(u)) |
1802 | return -ENOLINK; | |
1803 | ||
92ab323c | 1804 | /* Forward to the main object, if we aren't it. */ |
52990c2e ZJS |
1805 | following = unit_following(u); |
1806 | if (following) { | |
f2341e0a | 1807 | log_unit_debug(u, "Redirecting start request from %s to %s.", u->id, following->id); |
92ab323c LP |
1808 | return unit_start(following); |
1809 | } | |
1810 | ||
1811 | /* If it is stopped, but we cannot start it, then fail */ | |
1812 | if (!UNIT_VTABLE(u)->start) | |
1813 | return -EBADR; | |
1814 | ||
5766aca8 LP |
1815 | /* We don't suppress calls to ->start() here when we are already starting, to allow this request to |
1816 | * be used as a "hurry up" call, for example when the unit is in some "auto restart" state where it | |
1817 | * waits for a holdoff timer to elapse before it will start again. */ | |
87f0e418 | 1818 | |
c1e1601e | 1819 | unit_add_to_dbus_queue(u); |
9e58ff9c | 1820 | |
d1a34ae9 | 1821 | return UNIT_VTABLE(u)->start(u); |
87f0e418 LP |
1822 | } |
1823 | ||
1824 | bool unit_can_start(Unit *u) { | |
1825 | assert(u); | |
1826 | ||
8ff4d2ab LP |
1827 | if (u->load_state != UNIT_LOADED) |
1828 | return false; | |
1829 | ||
96cf3ec9 | 1830 | if (!unit_type_supported(u->type)) |
8ff4d2ab LP |
1831 | return false; |
1832 | ||
d4fd1cf2 LP |
1833 | /* Scope units may be started only once */ |
1834 | if (UNIT_VTABLE(u)->once_only && dual_timestamp_is_set(&u->inactive_exit_timestamp)) | |
1835 | return false; | |
1836 | ||
87f0e418 LP |
1837 | return !!UNIT_VTABLE(u)->start; |
1838 | } | |
1839 | ||
2528a7a6 LP |
1840 | bool unit_can_isolate(Unit *u) { |
1841 | assert(u); | |
1842 | ||
1843 | return unit_can_start(u) && | |
ac155bb8 | 1844 | u->allow_isolate; |
2528a7a6 LP |
1845 | } |
1846 | ||
87f0e418 LP |
1847 | /* Errors: |
1848 | * -EBADR: This unit type does not support stopping. | |
1849 | * -EALREADY: Unit is already stopped. | |
1850 | * -EAGAIN: An operation is already in progress. Retry later. | |
1851 | */ | |
1852 | int unit_stop(Unit *u) { | |
1853 | UnitActiveState state; | |
92ab323c | 1854 | Unit *following; |
87f0e418 LP |
1855 | |
1856 | assert(u); | |
1857 | ||
87f0e418 | 1858 | state = unit_active_state(u); |
fdf20a31 | 1859 | if (UNIT_IS_INACTIVE_OR_FAILED(state)) |
87f0e418 LP |
1860 | return -EALREADY; |
1861 | ||
0faacd47 LP |
1862 | following = unit_following(u); |
1863 | if (following) { | |
f2341e0a | 1864 | log_unit_debug(u, "Redirecting stop request from %s to %s.", u->id, following->id); |
92ab323c LP |
1865 | return unit_stop(following); |
1866 | } | |
1867 | ||
7898b0cf LP |
1868 | if (!UNIT_VTABLE(u)->stop) |
1869 | return -EBADR; | |
1870 | ||
c1e1601e | 1871 | unit_add_to_dbus_queue(u); |
9e58ff9c | 1872 | |
d1a34ae9 | 1873 | return UNIT_VTABLE(u)->stop(u); |
87f0e418 LP |
1874 | } |
1875 | ||
f5869324 LP |
1876 | bool unit_can_stop(Unit *u) { |
1877 | assert(u); | |
1878 | ||
96cf3ec9 | 1879 | if (!unit_type_supported(u->type)) |
f5869324 LP |
1880 | return false; |
1881 | ||
1882 | if (u->perpetual) | |
1883 | return false; | |
1884 | ||
1885 | return !!UNIT_VTABLE(u)->stop; | |
1886 | } | |
1887 | ||
87f0e418 LP |
1888 | /* Errors: |
1889 | * -EBADR: This unit type does not support reloading. | |
1890 | * -ENOEXEC: Unit is not started. | |
1891 | * -EAGAIN: An operation is already in progress. Retry later. | |
1892 | */ | |
1893 | int unit_reload(Unit *u) { | |
1894 | UnitActiveState state; | |
92ab323c | 1895 | Unit *following; |
87f0e418 LP |
1896 | |
1897 | assert(u); | |
1898 | ||
ac155bb8 | 1899 | if (u->load_state != UNIT_LOADED) |
6124958c LP |
1900 | return -EINVAL; |
1901 | ||
87f0e418 LP |
1902 | if (!unit_can_reload(u)) |
1903 | return -EBADR; | |
1904 | ||
1905 | state = unit_active_state(u); | |
e364ad06 | 1906 | if (state == UNIT_RELOADING) |
6255af75 | 1907 | return -EAGAIN; |
87f0e418 | 1908 | |
6a371e23 | 1909 | if (state != UNIT_ACTIVE) { |
f2341e0a | 1910 | log_unit_warning(u, "Unit cannot be reloaded because it is inactive."); |
87f0e418 | 1911 | return -ENOEXEC; |
6a371e23 | 1912 | } |
87f0e418 | 1913 | |
e48614c4 ZJS |
1914 | following = unit_following(u); |
1915 | if (following) { | |
f2341e0a | 1916 | log_unit_debug(u, "Redirecting reload request from %s to %s.", u->id, following->id); |
92ab323c LP |
1917 | return unit_reload(following); |
1918 | } | |
1919 | ||
c1e1601e | 1920 | unit_add_to_dbus_queue(u); |
82a2b6bb | 1921 | |
f54bcca5 JR |
1922 | if (!UNIT_VTABLE(u)->reload) { |
1923 | /* Unit doesn't have a reload function, but we need to propagate the reload anyway */ | |
2ad2e41a | 1924 | unit_notify(u, unit_active_state(u), unit_active_state(u), 0); |
f54bcca5 JR |
1925 | return 0; |
1926 | } | |
1927 | ||
d1a34ae9 | 1928 | return UNIT_VTABLE(u)->reload(u); |
87f0e418 LP |
1929 | } |
1930 | ||
1931 | bool unit_can_reload(Unit *u) { | |
1932 | assert(u); | |
1933 | ||
f54bcca5 JR |
1934 | if (UNIT_VTABLE(u)->can_reload) |
1935 | return UNIT_VTABLE(u)->can_reload(u); | |
87f0e418 | 1936 | |
eef85c4a | 1937 | if (!hashmap_isempty(u->dependencies[UNIT_PROPAGATES_RELOAD_TO])) |
87f0e418 LP |
1938 | return true; |
1939 | ||
f54bcca5 | 1940 | return UNIT_VTABLE(u)->reload; |
87f0e418 LP |
1941 | } |
1942 | ||
a3c1168a LP |
1943 | bool unit_is_unneeded(Unit *u) { |
1944 | static const UnitDependency deps[] = { | |
be7d9ff7 | 1945 | UNIT_REQUIRED_BY, |
084918ba | 1946 | UNIT_REQUISITE_OF, |
be7d9ff7 LP |
1947 | UNIT_WANTED_BY, |
1948 | UNIT_BOUND_BY, | |
1949 | }; | |
a3c1168a | 1950 | size_t j; |
f3bff0eb LP |
1951 | |
1952 | assert(u); | |
1953 | ||
ac155bb8 | 1954 | if (!u->stop_when_unneeded) |
a3c1168a | 1955 | return false; |
f3bff0eb | 1956 | |
a3c1168a LP |
1957 | /* Don't clean up while the unit is transitioning or is even inactive. */ |
1958 | if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) | |
1959 | return false; | |
1960 | if (u->job) | |
1961 | return false; | |
f3bff0eb | 1962 | |
a3c1168a | 1963 | for (j = 0; j < ELEMENTSOF(deps); j++) { |
eef85c4a LP |
1964 | Unit *other; |
1965 | Iterator i; | |
1966 | void *v; | |
1967 | ||
fda09318 | 1968 | /* If a dependent unit has a job queued, is active or transitioning, or is marked for |
a3c1168a | 1969 | * restart, then don't clean this one up. */ |
b81884e7 | 1970 | |
a3c1168a | 1971 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[deps[j]], i) { |
93d4cb09 | 1972 | if (other->job) |
a3c1168a LP |
1973 | return false; |
1974 | ||
1975 | if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) | |
1976 | return false; | |
1977 | ||
1978 | if (unit_will_restart(other)) | |
1979 | return false; | |
1980 | } | |
bea355da LP |
1981 | } |
1982 | ||
a3c1168a LP |
1983 | return true; |
1984 | } | |
f3bff0eb | 1985 | |
a3c1168a LP |
1986 | static void check_unneeded_dependencies(Unit *u) { |
1987 | ||
1988 | static const UnitDependency deps[] = { | |
1989 | UNIT_REQUIRES, | |
1990 | UNIT_REQUISITE, | |
1991 | UNIT_WANTS, | |
1992 | UNIT_BINDS_TO, | |
1993 | }; | |
1994 | size_t j; | |
1995 | ||
1996 | assert(u); | |
1997 | ||
1998 | /* Add all units this unit depends on to the queue that processes StopWhenUnneeded= behaviour. */ | |
1999 | ||
2000 | for (j = 0; j < ELEMENTSOF(deps); j++) { | |
2001 | Unit *other; | |
2002 | Iterator i; | |
2003 | void *v; | |
2004 | ||
2005 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[deps[j]], i) | |
fda09318 | 2006 | unit_submit_to_stop_when_unneeded_queue(other); |
a3c1168a | 2007 | } |
f3bff0eb LP |
2008 | } |
2009 | ||
ff502445 | 2010 | static void unit_check_binds_to(Unit *u) { |
4afd3348 | 2011 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
ff502445 LP |
2012 | bool stop = false; |
2013 | Unit *other; | |
2014 | Iterator i; | |
eef85c4a | 2015 | void *v; |
67bfdc97 | 2016 | int r; |
ff502445 LP |
2017 | |
2018 | assert(u); | |
2019 | ||
2020 | if (u->job) | |
2021 | return; | |
2022 | ||
2023 | if (unit_active_state(u) != UNIT_ACTIVE) | |
2024 | return; | |
2025 | ||
eef85c4a | 2026 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_BINDS_TO], i) { |
ff502445 LP |
2027 | if (other->job) |
2028 | continue; | |
13ddc3fc ZJS |
2029 | |
2030 | if (!other->coldplugged) | |
2031 | /* We might yet create a job for the other unit… */ | |
2032 | continue; | |
ff502445 LP |
2033 | |
2034 | if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) | |
2035 | continue; | |
2036 | ||
2037 | stop = true; | |
98f738b6 | 2038 | break; |
ff502445 LP |
2039 | } |
2040 | ||
2041 | if (!stop) | |
2042 | return; | |
2043 | ||
595bfe7d | 2044 | /* If stopping a unit fails continuously we might enter a stop |
67bfdc97 LP |
2045 | * loop here, hence stop acting on the service being |
2046 | * unnecessary after a while. */ | |
7994ac1d | 2047 | if (!ratelimit_below(&u->auto_stop_ratelimit)) { |
67bfdc97 LP |
2048 | log_unit_warning(u, "Unit is bound to inactive unit %s, but not stopping since we tried this too often recently.", other->id); |
2049 | return; | |
2050 | } | |
2051 | ||
98f738b6 | 2052 | assert(other); |
f2341e0a | 2053 | log_unit_info(u, "Unit is bound to inactive unit %s. Stopping, too.", other->id); |
ff502445 LP |
2054 | |
2055 | /* A unit we need to run is gone. Sniff. Let's stop this. */ | |
50cbaba4 | 2056 | r = manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, NULL, &error, NULL); |
67bfdc97 | 2057 | if (r < 0) |
4bd29fe5 | 2058 | log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r)); |
ff502445 LP |
2059 | } |
2060 | ||
87f0e418 LP |
2061 | static void retroactively_start_dependencies(Unit *u) { |
2062 | Iterator i; | |
2063 | Unit *other; | |
eef85c4a | 2064 | void *v; |
87f0e418 LP |
2065 | |
2066 | assert(u); | |
2067 | assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))); | |
2068 | ||
eef85c4a LP |
2069 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_REQUIRES], i) |
2070 | if (!hashmap_get(u->dependencies[UNIT_AFTER], other) && | |
b81884e7 | 2071 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) |
50cbaba4 | 2072 | manager_add_job(u->manager, JOB_START, other, JOB_REPLACE, NULL, NULL, NULL); |
b81884e7 | 2073 | |
eef85c4a LP |
2074 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_BINDS_TO], i) |
2075 | if (!hashmap_get(u->dependencies[UNIT_AFTER], other) && | |
b81884e7 | 2076 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) |
50cbaba4 | 2077 | manager_add_job(u->manager, JOB_START, other, JOB_REPLACE, NULL, NULL, NULL); |
87f0e418 | 2078 | |
eef85c4a LP |
2079 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_WANTS], i) |
2080 | if (!hashmap_get(u->dependencies[UNIT_AFTER], other) && | |
b81884e7 | 2081 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) |
50cbaba4 | 2082 | manager_add_job(u->manager, JOB_START, other, JOB_FAIL, NULL, NULL, NULL); |
87f0e418 | 2083 | |
eef85c4a | 2084 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_CONFLICTS], i) |
b81884e7 | 2085 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
50cbaba4 | 2086 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, NULL, NULL, NULL); |
69dd2852 | 2087 | |
eef85c4a | 2088 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_CONFLICTED_BY], i) |
b81884e7 | 2089 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
50cbaba4 | 2090 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, NULL, NULL, NULL); |
87f0e418 LP |
2091 | } |
2092 | ||
2093 | static void retroactively_stop_dependencies(Unit *u) { | |
87f0e418 | 2094 | Unit *other; |
eef85c4a LP |
2095 | Iterator i; |
2096 | void *v; | |
87f0e418 LP |
2097 | |
2098 | assert(u); | |
2099 | assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))); | |
2100 | ||
b81884e7 | 2101 | /* Pull down units which are bound to us recursively if enabled */ |
eef85c4a | 2102 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_BOUND_BY], i) |
b81884e7 | 2103 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
50cbaba4 | 2104 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, NULL, NULL, NULL); |
cd0504d0 MS |
2105 | } |
2106 | ||
3ecaa09b | 2107 | void unit_start_on_failure(Unit *u) { |
c0daa706 LP |
2108 | Unit *other; |
2109 | Iterator i; | |
eef85c4a | 2110 | void *v; |
7f66b026 | 2111 | int r; |
c0daa706 LP |
2112 | |
2113 | assert(u); | |
2114 | ||
eef85c4a | 2115 | if (hashmap_size(u->dependencies[UNIT_ON_FAILURE]) <= 0) |
222ae6a8 LP |
2116 | return; |
2117 | ||
f2341e0a | 2118 | log_unit_info(u, "Triggering OnFailure= dependencies."); |
222ae6a8 | 2119 | |
eef85c4a | 2120 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_ON_FAILURE], i) { |
7f66b026 | 2121 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
222ae6a8 | 2122 | |
50cbaba4 | 2123 | r = manager_add_job(u->manager, JOB_START, other, u->on_failure_job_mode, NULL, &error, NULL); |
c1b6628d | 2124 | if (r < 0) |
7f66b026 | 2125 | log_unit_warning_errno(u, r, "Failed to enqueue OnFailure= job, ignoring: %s", bus_error_message(&error, r)); |
222ae6a8 | 2126 | } |
c0daa706 LP |
2127 | } |
2128 | ||
3ecaa09b LP |
2129 | void unit_trigger_notify(Unit *u) { |
2130 | Unit *other; | |
2131 | Iterator i; | |
eef85c4a | 2132 | void *v; |
3ecaa09b LP |
2133 | |
2134 | assert(u); | |
2135 | ||
eef85c4a | 2136 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_TRIGGERED_BY], i) |
3ecaa09b LP |
2137 | if (UNIT_VTABLE(other)->trigger_notify) |
2138 | UNIT_VTABLE(other)->trigger_notify(other, u); | |
2139 | } | |
2140 | ||
915b1d01 | 2141 | static int unit_log_resources(Unit *u) { |
bc40a20e LP |
2142 | struct iovec iovec[1 + _CGROUP_IP_ACCOUNTING_METRIC_MAX + _CGROUP_IO_ACCOUNTING_METRIC_MAX + 4]; |
2143 | bool any_traffic = false, have_ip_accounting = false, any_io = false, have_io_accounting = false; | |
2144 | _cleanup_free_ char *igress = NULL, *egress = NULL, *rr = NULL, *wr = NULL; | |
915b1d01 | 2145 | size_t n_message_parts = 0, n_iovec = 0; |
bc40a20e | 2146 | char* message_parts[1 + 2 + 2 + 1], *t; |
915b1d01 LP |
2147 | nsec_t nsec = NSEC_INFINITY; |
2148 | CGroupIPAccountingMetric m; | |
2149 | size_t i; | |
2150 | int r; | |
2151 | const char* const ip_fields[_CGROUP_IP_ACCOUNTING_METRIC_MAX] = { | |
2152 | [CGROUP_IP_INGRESS_BYTES] = "IP_METRIC_INGRESS_BYTES", | |
2153 | [CGROUP_IP_INGRESS_PACKETS] = "IP_METRIC_INGRESS_PACKETS", | |
2154 | [CGROUP_IP_EGRESS_BYTES] = "IP_METRIC_EGRESS_BYTES", | |
2155 | [CGROUP_IP_EGRESS_PACKETS] = "IP_METRIC_EGRESS_PACKETS", | |
2156 | }; | |
bc40a20e LP |
2157 | const char* const io_fields[_CGROUP_IO_ACCOUNTING_METRIC_MAX] = { |
2158 | [CGROUP_IO_READ_BYTES] = "IO_METRIC_READ_BYTES", | |
2159 | [CGROUP_IO_WRITE_BYTES] = "IO_METRIC_WRITE_BYTES", | |
2160 | [CGROUP_IO_READ_OPERATIONS] = "IO_METRIC_READ_OPERATIONS", | |
2161 | [CGROUP_IO_WRITE_OPERATIONS] = "IO_METRIC_WRITE_OPERATIONS", | |
2162 | }; | |
915b1d01 LP |
2163 | |
2164 | assert(u); | |
2165 | ||
2166 | /* Invoked whenever a unit enters failed or dead state. Logs information about consumed resources if resource | |
2167 | * accounting was enabled for a unit. It does this in two ways: a friendly human readable string with reduced | |
2168 | * information and the complete data in structured fields. */ | |
2169 | ||
2170 | (void) unit_get_cpu_usage(u, &nsec); | |
2171 | if (nsec != NSEC_INFINITY) { | |
2172 | char buf[FORMAT_TIMESPAN_MAX] = ""; | |
2173 | ||
2174 | /* Format the CPU time for inclusion in the structured log message */ | |
2175 | if (asprintf(&t, "CPU_USAGE_NSEC=%" PRIu64, nsec) < 0) { | |
2176 | r = log_oom(); | |
2177 | goto finish; | |
2178 | } | |
2179 | iovec[n_iovec++] = IOVEC_MAKE_STRING(t); | |
2180 | ||
2181 | /* Format the CPU time for inclusion in the human language message string */ | |
2182 | format_timespan(buf, sizeof(buf), nsec / NSEC_PER_USEC, USEC_PER_MSEC); | |
ec9d636b | 2183 | t = strjoin("consumed ", buf, " CPU time"); |
915b1d01 LP |
2184 | if (!t) { |
2185 | r = log_oom(); | |
2186 | goto finish; | |
2187 | } | |
2188 | ||
2189 | message_parts[n_message_parts++] = t; | |
2190 | } | |
2191 | ||
bc40a20e LP |
2192 | for (CGroupIOAccountingMetric k = 0; k < _CGROUP_IO_ACCOUNTING_METRIC_MAX; k++) { |
2193 | char buf[FORMAT_BYTES_MAX] = ""; | |
2194 | uint64_t value = UINT64_MAX; | |
2195 | ||
2196 | assert(io_fields[k]); | |
2197 | ||
2198 | (void) unit_get_io_accounting(u, k, k > 0, &value); | |
2199 | if (value == UINT64_MAX) | |
2200 | continue; | |
2201 | ||
2202 | have_io_accounting = true; | |
2203 | if (value > 0) | |
2204 | any_io = true; | |
2205 | ||
2206 | /* Format IO accounting data for inclusion in the structured log message */ | |
2207 | if (asprintf(&t, "%s=%" PRIu64, io_fields[k], value) < 0) { | |
2208 | r = log_oom(); | |
2209 | goto finish; | |
2210 | } | |
2211 | iovec[n_iovec++] = IOVEC_MAKE_STRING(t); | |
2212 | ||
2213 | /* Format the IO accounting data for inclusion in the human language message string, but only | |
2214 | * for the bytes counters (and not for the operations counters) */ | |
2215 | if (k == CGROUP_IO_READ_BYTES) { | |
2216 | assert(!rr); | |
2217 | rr = strjoin("read ", format_bytes(buf, sizeof(buf), value), " from disk"); | |
2218 | if (!rr) { | |
2219 | r = log_oom(); | |
2220 | goto finish; | |
2221 | } | |
2222 | } else if (k == CGROUP_IO_WRITE_BYTES) { | |
2223 | assert(!wr); | |
2224 | wr = strjoin("written ", format_bytes(buf, sizeof(buf), value), " to disk"); | |
2225 | if (!wr) { | |
2226 | r = log_oom(); | |
2227 | goto finish; | |
2228 | } | |
2229 | } | |
2230 | } | |
2231 | ||
2232 | if (have_io_accounting) { | |
2233 | if (any_io) { | |
2234 | if (rr) | |
2235 | message_parts[n_message_parts++] = TAKE_PTR(rr); | |
2236 | if (wr) | |
2237 | message_parts[n_message_parts++] = TAKE_PTR(wr); | |
2238 | ||
2239 | } else { | |
2240 | char *k; | |
2241 | ||
2242 | k = strdup("no IO"); | |
2243 | if (!k) { | |
2244 | r = log_oom(); | |
2245 | goto finish; | |
2246 | } | |
2247 | ||
2248 | message_parts[n_message_parts++] = k; | |
2249 | } | |
2250 | } | |
2251 | ||
915b1d01 LP |
2252 | for (m = 0; m < _CGROUP_IP_ACCOUNTING_METRIC_MAX; m++) { |
2253 | char buf[FORMAT_BYTES_MAX] = ""; | |
2254 | uint64_t value = UINT64_MAX; | |
2255 | ||
2256 | assert(ip_fields[m]); | |
2257 | ||
2258 | (void) unit_get_ip_accounting(u, m, &value); | |
2259 | if (value == UINT64_MAX) | |
2260 | continue; | |
82044702 LP |
2261 | |
2262 | have_ip_accounting = true; | |
a87b1faa LP |
2263 | if (value > 0) |
2264 | any_traffic = true; | |
915b1d01 LP |
2265 | |
2266 | /* Format IP accounting data for inclusion in the structured log message */ | |
2267 | if (asprintf(&t, "%s=%" PRIu64, ip_fields[m], value) < 0) { | |
2268 | r = log_oom(); | |
2269 | goto finish; | |
2270 | } | |
2271 | iovec[n_iovec++] = IOVEC_MAKE_STRING(t); | |
2272 | ||
2273 | /* Format the IP accounting data for inclusion in the human language message string, but only for the | |
2274 | * bytes counters (and not for the packets counters) */ | |
a87b1faa LP |
2275 | if (m == CGROUP_IP_INGRESS_BYTES) { |
2276 | assert(!igress); | |
ec9d636b | 2277 | igress = strjoin("received ", format_bytes(buf, sizeof(buf), value), " IP traffic"); |
a87b1faa LP |
2278 | if (!igress) { |
2279 | r = log_oom(); | |
2280 | goto finish; | |
2281 | } | |
2282 | } else if (m == CGROUP_IP_EGRESS_BYTES) { | |
2283 | assert(!egress); | |
ec9d636b | 2284 | egress = strjoin("sent ", format_bytes(buf, sizeof(buf), value), " IP traffic"); |
a87b1faa LP |
2285 | if (!egress) { |
2286 | r = log_oom(); | |
2287 | goto finish; | |
2288 | } | |
2289 | } | |
2290 | } | |
2291 | ||
82044702 LP |
2292 | if (have_ip_accounting) { |
2293 | if (any_traffic) { | |
2294 | if (igress) | |
2295 | message_parts[n_message_parts++] = TAKE_PTR(igress); | |
2296 | if (egress) | |
2297 | message_parts[n_message_parts++] = TAKE_PTR(egress); | |
a87b1faa | 2298 | |
82044702 LP |
2299 | } else { |
2300 | char *k; | |
915b1d01 | 2301 | |
82044702 LP |
2302 | k = strdup("no IP traffic"); |
2303 | if (!k) { | |
2304 | r = log_oom(); | |
2305 | goto finish; | |
2306 | } | |
2307 | ||
2308 | message_parts[n_message_parts++] = k; | |
2309 | } | |
915b1d01 LP |
2310 | } |
2311 | ||
2312 | /* Is there any accounting data available at all? */ | |
2313 | if (n_iovec == 0) { | |
2314 | r = 0; | |
2315 | goto finish; | |
2316 | } | |
2317 | ||
2318 | if (n_message_parts == 0) | |
a87b1faa | 2319 | t = strjoina("MESSAGE=", u->id, ": Completed."); |
915b1d01 LP |
2320 | else { |
2321 | _cleanup_free_ char *joined; | |
2322 | ||
2323 | message_parts[n_message_parts] = NULL; | |
2324 | ||
2325 | joined = strv_join(message_parts, ", "); | |
2326 | if (!joined) { | |
2327 | r = log_oom(); | |
2328 | goto finish; | |
2329 | } | |
2330 | ||
ec9d636b | 2331 | joined[0] = ascii_toupper(joined[0]); |
a87b1faa | 2332 | t = strjoina("MESSAGE=", u->id, ": ", joined, "."); |
915b1d01 LP |
2333 | } |
2334 | ||
2335 | /* The following four fields we allocate on the stack or are static strings, we hence don't want to free them, | |
2336 | * and hence don't increase n_iovec for them */ | |
2337 | iovec[n_iovec] = IOVEC_MAKE_STRING(t); | |
2338 | iovec[n_iovec + 1] = IOVEC_MAKE_STRING("MESSAGE_ID=" SD_MESSAGE_UNIT_RESOURCES_STR); | |
2339 | ||
2340 | t = strjoina(u->manager->unit_log_field, u->id); | |
2341 | iovec[n_iovec + 2] = IOVEC_MAKE_STRING(t); | |
2342 | ||
2343 | t = strjoina(u->manager->invocation_log_field, u->invocation_id_string); | |
2344 | iovec[n_iovec + 3] = IOVEC_MAKE_STRING(t); | |
2345 | ||
2346 | log_struct_iovec(LOG_INFO, iovec, n_iovec + 4); | |
2347 | r = 0; | |
2348 | ||
2349 | finish: | |
2350 | for (i = 0; i < n_message_parts; i++) | |
2351 | free(message_parts[i]); | |
2352 | ||
2353 | for (i = 0; i < n_iovec; i++) | |
2354 | free(iovec[i].iov_base); | |
2355 | ||
2356 | return r; | |
2357 | ||
2358 | } | |
2359 | ||
adefcf28 LP |
2360 | static void unit_update_on_console(Unit *u) { |
2361 | bool b; | |
2362 | ||
2363 | assert(u); | |
2364 | ||
2365 | b = unit_needs_console(u); | |
2366 | if (u->on_console == b) | |
2367 | return; | |
2368 | ||
2369 | u->on_console = b; | |
2370 | if (b) | |
2371 | manager_ref_console(u->manager); | |
2372 | else | |
2373 | manager_unref_console(u->manager); | |
adefcf28 LP |
2374 | } |
2375 | ||
6eb65e7c LP |
2376 | static void unit_emit_audit_start(Unit *u) { |
2377 | assert(u); | |
2378 | ||
2379 | if (u->type != UNIT_SERVICE) | |
2380 | return; | |
2381 | ||
2382 | /* Write audit record if we have just finished starting up */ | |
2383 | manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_START, true); | |
2384 | u->in_audit = true; | |
2385 | } | |
2386 | ||
2387 | static void unit_emit_audit_stop(Unit *u, UnitActiveState state) { | |
2388 | assert(u); | |
2389 | ||
2390 | if (u->type != UNIT_SERVICE) | |
2391 | return; | |
2392 | ||
2393 | if (u->in_audit) { | |
2394 | /* Write audit record if we have just finished shutting down */ | |
2395 | manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_STOP, state == UNIT_INACTIVE); | |
2396 | u->in_audit = false; | |
2397 | } else { | |
2398 | /* Hmm, if there was no start record written write it now, so that we always have a nice pair */ | |
2399 | manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_START, state == UNIT_INACTIVE); | |
2400 | ||
2401 | if (state == UNIT_INACTIVE) | |
2402 | manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_STOP, true); | |
2403 | } | |
2404 | } | |
2405 | ||
16c74914 LP |
2406 | static bool unit_process_job(Job *j, UnitActiveState ns, UnitNotifyFlags flags) { |
2407 | bool unexpected = false; | |
31cd5f63 | 2408 | JobResult result; |
16c74914 LP |
2409 | |
2410 | assert(j); | |
2411 | ||
2412 | if (j->state == JOB_WAITING) | |
2413 | ||
2414 | /* So we reached a different state for this job. Let's see if we can run it now if it failed previously | |
2415 | * due to EAGAIN. */ | |
2416 | job_add_to_run_queue(j); | |
2417 | ||
2418 | /* Let's check whether the unit's new state constitutes a finished job, or maybe contradicts a running job and | |
2419 | * hence needs to invalidate jobs. */ | |
2420 | ||
2421 | switch (j->type) { | |
2422 | ||
2423 | case JOB_START: | |
2424 | case JOB_VERIFY_ACTIVE: | |
2425 | ||
2426 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
2427 | job_finish_and_invalidate(j, JOB_DONE, true, false); | |
2428 | else if (j->state == JOB_RUNNING && ns != UNIT_ACTIVATING) { | |
2429 | unexpected = true; | |
2430 | ||
31cd5f63 AZ |
2431 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) { |
2432 | if (ns == UNIT_FAILED) | |
2433 | result = JOB_FAILED; | |
2434 | else if (FLAGS_SET(flags, UNIT_NOTIFY_SKIP_CONDITION)) | |
2435 | result = JOB_SKIPPED; | |
2436 | else | |
2437 | result = JOB_DONE; | |
2438 | ||
2439 | job_finish_and_invalidate(j, result, true, false); | |
2440 | } | |
16c74914 LP |
2441 | } |
2442 | ||
2443 | break; | |
2444 | ||
2445 | case JOB_RELOAD: | |
2446 | case JOB_RELOAD_OR_START: | |
2447 | case JOB_TRY_RELOAD: | |
2448 | ||
2449 | if (j->state == JOB_RUNNING) { | |
2450 | if (ns == UNIT_ACTIVE) | |
2451 | job_finish_and_invalidate(j, (flags & UNIT_NOTIFY_RELOAD_FAILURE) ? JOB_FAILED : JOB_DONE, true, false); | |
2452 | else if (!IN_SET(ns, UNIT_ACTIVATING, UNIT_RELOADING)) { | |
2453 | unexpected = true; | |
2454 | ||
2455 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) | |
2456 | job_finish_and_invalidate(j, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true, false); | |
2457 | } | |
2458 | } | |
2459 | ||
2460 | break; | |
2461 | ||
2462 | case JOB_STOP: | |
2463 | case JOB_RESTART: | |
2464 | case JOB_TRY_RESTART: | |
2465 | ||
2466 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) | |
2467 | job_finish_and_invalidate(j, JOB_DONE, true, false); | |
2468 | else if (j->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) { | |
2469 | unexpected = true; | |
2470 | job_finish_and_invalidate(j, JOB_FAILED, true, false); | |
2471 | } | |
2472 | ||
2473 | break; | |
2474 | ||
2475 | default: | |
2476 | assert_not_reached("Job type unknown"); | |
2477 | } | |
2478 | ||
2479 | return unexpected; | |
2480 | } | |
2481 | ||
2ad2e41a | 2482 | void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, UnitNotifyFlags flags) { |
429926e9 | 2483 | const char *reason; |
8559b3b7 | 2484 | Manager *m; |
a096ed36 | 2485 | |
87f0e418 LP |
2486 | assert(u); |
2487 | assert(os < _UNIT_ACTIVE_STATE_MAX); | |
2488 | assert(ns < _UNIT_ACTIVE_STATE_MAX); | |
87f0e418 | 2489 | |
8559b3b7 LP |
2490 | /* Note that this is called for all low-level state changes, even if they might map to the same high-level |
2491 | * UnitActiveState! That means that ns == os is an expected behavior here. For example: if a mount point is | |
2492 | * remounted this function will be called too! */ | |
87f0e418 | 2493 | |
546ac4f0 MS |
2494 | m = u->manager; |
2495 | ||
3c4832ad LP |
2496 | /* Let's enqueue the change signal early. In case this unit has a job associated we want that this unit is in |
2497 | * the bus queue, so that any job change signal queued will force out the unit change signal first. */ | |
2498 | unit_add_to_dbus_queue(u); | |
2499 | ||
f755e3b7 | 2500 | /* Update timestamps for state changes */ |
2c289ea8 | 2501 | if (!MANAGER_IS_RELOADING(m)) { |
a483fb59 | 2502 | dual_timestamp_get(&u->state_change_timestamp); |
173e3821 | 2503 | |
bdbf9951 | 2504 | if (UNIT_IS_INACTIVE_OR_FAILED(os) && !UNIT_IS_INACTIVE_OR_FAILED(ns)) |
a483fb59 | 2505 | u->inactive_exit_timestamp = u->state_change_timestamp; |
bdbf9951 | 2506 | else if (!UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_INACTIVE_OR_FAILED(ns)) |
a483fb59 | 2507 | u->inactive_enter_timestamp = u->state_change_timestamp; |
bdbf9951 LP |
2508 | |
2509 | if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
a483fb59 | 2510 | u->active_enter_timestamp = u->state_change_timestamp; |
bdbf9951 | 2511 | else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) |
a483fb59 | 2512 | u->active_exit_timestamp = u->state_change_timestamp; |
bdbf9951 | 2513 | } |
87f0e418 | 2514 | |
865cc19a | 2515 | /* Keep track of failed units */ |
8724defe | 2516 | (void) manager_update_failed_units(m, u, ns == UNIT_FAILED); |
f755e3b7 | 2517 | |
d3070fbd LP |
2518 | /* Make sure the cgroup and state files are always removed when we become inactive */ |
2519 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) { | |
efdb0237 | 2520 | unit_prune_cgroup(u); |
d3070fbd LP |
2521 | unit_unlink_state_files(u); |
2522 | } | |
fb385181 | 2523 | |
adefcf28 | 2524 | unit_update_on_console(u); |
7ed9f6cd | 2525 | |
2c289ea8 | 2526 | if (!MANAGER_IS_RELOADING(m)) { |
a1c7334b LP |
2527 | bool unexpected; |
2528 | ||
2529 | /* Let's propagate state changes to the job */ | |
2530 | if (u->job) | |
2531 | unexpected = unit_process_job(u->job, ns, flags); | |
2532 | else | |
2533 | unexpected = true; | |
f3bff0eb | 2534 | |
a1c7334b LP |
2535 | /* If this state change happened without being requested by a job, then let's retroactively start or |
2536 | * stop dependencies. We skip that step when deserializing, since we don't want to create any | |
2537 | * additional jobs just because something is already activated. */ | |
bdbf9951 LP |
2538 | |
2539 | if (unexpected) { | |
2540 | if (UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns)) | |
2541 | retroactively_start_dependencies(u); | |
2542 | else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns)) | |
2543 | retroactively_stop_dependencies(u); | |
2544 | } | |
5de9682c | 2545 | |
cd0504d0 | 2546 | /* stop unneeded units regardless if going down was expected or not */ |
a3c1168a | 2547 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
cd0504d0 MS |
2548 | check_unneeded_dependencies(u); |
2549 | ||
bdbf9951 | 2550 | if (ns != os && ns == UNIT_FAILED) { |
ed77d407 | 2551 | log_unit_debug(u, "Unit entered failed state."); |
2ad2e41a LP |
2552 | |
2553 | if (!(flags & UNIT_NOTIFY_WILL_AUTO_RESTART)) | |
2554 | unit_start_on_failure(u); | |
cd6d0a45 | 2555 | } |
e537352b | 2556 | |
256f65d0 LP |
2557 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns) && !UNIT_IS_ACTIVE_OR_RELOADING(os)) { |
2558 | /* This unit just finished starting up */ | |
3b2775c5 | 2559 | |
6eb65e7c | 2560 | unit_emit_audit_start(u); |
546ac4f0 | 2561 | manager_send_unit_plymouth(m, u); |
256f65d0 | 2562 | } |
bdbf9951 | 2563 | |
256f65d0 | 2564 | if (UNIT_IS_INACTIVE_OR_FAILED(ns) && !UNIT_IS_INACTIVE_OR_FAILED(os)) { |
915b1d01 | 2565 | /* This unit just stopped/failed. */ |
256f65d0 | 2566 | |
6eb65e7c | 2567 | unit_emit_audit_stop(u, ns); |
915b1d01 | 2568 | unit_log_resources(u); |
cd6d0a45 | 2569 | } |
f278026d LP |
2570 | } |
2571 | ||
31dc1ca3 LP |
2572 | manager_recheck_journal(m); |
2573 | manager_recheck_dbus(m); | |
e63ebf71 | 2574 | |
3ecaa09b | 2575 | unit_trigger_notify(u); |
3b2775c5 | 2576 | |
8724defe | 2577 | if (!MANAGER_IS_RELOADING(m)) { |
8559b3b7 | 2578 | /* Maybe we finished startup and are now ready for being stopped because unneeded? */ |
fda09318 | 2579 | unit_submit_to_stop_when_unneeded_queue(u); |
c1e1601e | 2580 | |
8559b3b7 LP |
2581 | /* Maybe we finished startup, but something we needed has vanished? Let's die then. (This happens when |
2582 | * something BindsTo= to a Type=oneshot unit, as these units go directly from starting to inactive, | |
ff502445 LP |
2583 | * without ever entering started.) */ |
2584 | unit_check_binds_to(u); | |
53c35a76 | 2585 | |
429926e9 TR |
2586 | if (os != UNIT_FAILED && ns == UNIT_FAILED) { |
2587 | reason = strjoina("unit ", u->id, " failed"); | |
36c4dc08 | 2588 | emergency_action(m, u->failure_action, 0, u->reboot_arg, unit_failure_action_exit_status(u), reason); |
429926e9 TR |
2589 | } else if (!UNIT_IS_INACTIVE_OR_FAILED(os) && ns == UNIT_INACTIVE) { |
2590 | reason = strjoina("unit ", u->id, " succeeded"); | |
36c4dc08 | 2591 | emergency_action(m, u->success_action, 0, u->reboot_arg, unit_success_action_exit_status(u), reason); |
429926e9 | 2592 | } |
ff502445 LP |
2593 | } |
2594 | ||
701cc384 | 2595 | unit_add_to_gc_queue(u); |
87f0e418 LP |
2596 | } |
2597 | ||
f75f613d | 2598 | int unit_watch_pid(Unit *u, pid_t pid, bool exclusive) { |
62a76913 | 2599 | int r; |
a911bb9a | 2600 | |
87f0e418 | 2601 | assert(u); |
62a76913 | 2602 | assert(pid_is_valid(pid)); |
87f0e418 | 2603 | |
62a76913 | 2604 | /* Watch a specific PID */ |
5ba6985b | 2605 | |
f75f613d FB |
2606 | /* Caller might be sure that this PID belongs to this unit only. Let's take this |
2607 | * opportunity to remove any stalled references to this PID as they can be created | |
2608 | * easily (when watching a process which is not our direct child). */ | |
2609 | if (exclusive) | |
2610 | manager_unwatch_pid(u->manager, pid); | |
2611 | ||
d5099efc | 2612 | r = set_ensure_allocated(&u->pids, NULL); |
5ba6985b LP |
2613 | if (r < 0) |
2614 | return r; | |
2615 | ||
62a76913 | 2616 | r = hashmap_ensure_allocated(&u->manager->watch_pids, NULL); |
a911bb9a LP |
2617 | if (r < 0) |
2618 | return r; | |
2619 | ||
62a76913 LP |
2620 | /* First try, let's add the unit keyed by "pid". */ |
2621 | r = hashmap_put(u->manager->watch_pids, PID_TO_PTR(pid), u); | |
2622 | if (r == -EEXIST) { | |
2623 | Unit **array; | |
2624 | bool found = false; | |
2625 | size_t n = 0; | |
05e343b7 | 2626 | |
62a76913 LP |
2627 | /* OK, the "pid" key is already assigned to a different unit. Let's see if the "-pid" key (which points |
2628 | * to an array of Units rather than just a Unit), lists us already. */ | |
a911bb9a | 2629 | |
62a76913 LP |
2630 | array = hashmap_get(u->manager->watch_pids, PID_TO_PTR(-pid)); |
2631 | if (array) | |
2632 | for (; array[n]; n++) | |
2633 | if (array[n] == u) | |
2634 | found = true; | |
a911bb9a | 2635 | |
62a76913 LP |
2636 | if (found) /* Found it already? if so, do nothing */ |
2637 | r = 0; | |
2638 | else { | |
2639 | Unit **new_array; | |
2640 | ||
2641 | /* Allocate a new array */ | |
2642 | new_array = new(Unit*, n + 2); | |
2643 | if (!new_array) | |
2644 | return -ENOMEM; | |
2645 | ||
2646 | memcpy_safe(new_array, array, sizeof(Unit*) * n); | |
2647 | new_array[n] = u; | |
2648 | new_array[n+1] = NULL; | |
2649 | ||
2650 | /* Add or replace the old array */ | |
2651 | r = hashmap_replace(u->manager->watch_pids, PID_TO_PTR(-pid), new_array); | |
2652 | if (r < 0) { | |
2653 | free(new_array); | |
2654 | return r; | |
2655 | } | |
2656 | ||
2657 | free(array); | |
2658 | } | |
2659 | } else if (r < 0) | |
2660 | return r; | |
2661 | ||
2662 | r = set_put(u->pids, PID_TO_PTR(pid)); | |
2663 | if (r < 0) | |
2664 | return r; | |
2665 | ||
2666 | return 0; | |
87f0e418 LP |
2667 | } |
2668 | ||
2669 | void unit_unwatch_pid(Unit *u, pid_t pid) { | |
62a76913 LP |
2670 | Unit **array; |
2671 | ||
87f0e418 | 2672 | assert(u); |
62a76913 LP |
2673 | assert(pid_is_valid(pid)); |
2674 | ||
2675 | /* First let's drop the unit in case it's keyed as "pid". */ | |
2676 | (void) hashmap_remove_value(u->manager->watch_pids, PID_TO_PTR(pid), u); | |
2677 | ||
2678 | /* Then, let's also drop the unit, in case it's in the array keyed by -pid */ | |
2679 | array = hashmap_get(u->manager->watch_pids, PID_TO_PTR(-pid)); | |
2680 | if (array) { | |
2681 | size_t n, m = 0; | |
2682 | ||
2683 | /* Let's iterate through the array, dropping our own entry */ | |
2684 | for (n = 0; array[n]; n++) | |
2685 | if (array[n] != u) | |
2686 | array[m++] = array[n]; | |
2687 | array[m] = NULL; | |
2688 | ||
2689 | if (m == 0) { | |
2690 | /* The array is now empty, remove the entire entry */ | |
2691 | assert(hashmap_remove(u->manager->watch_pids, PID_TO_PTR(-pid)) == array); | |
2692 | free(array); | |
2693 | } | |
2694 | } | |
87f0e418 | 2695 | |
fea72cc0 | 2696 | (void) set_remove(u->pids, PID_TO_PTR(pid)); |
a911bb9a LP |
2697 | } |
2698 | ||
bd44e61b LP |
2699 | void unit_unwatch_all_pids(Unit *u) { |
2700 | assert(u); | |
2701 | ||
2702 | while (!set_isempty(u->pids)) | |
fea72cc0 | 2703 | unit_unwatch_pid(u, PTR_TO_PID(set_first(u->pids))); |
bd44e61b | 2704 | |
efdb0237 | 2705 | u->pids = set_free(u->pids); |
a911bb9a LP |
2706 | } |
2707 | ||
50be4f4a LP |
2708 | static void unit_tidy_watch_pids(Unit *u) { |
2709 | pid_t except1, except2; | |
a911bb9a LP |
2710 | Iterator i; |
2711 | void *e; | |
2712 | ||
2713 | assert(u); | |
2714 | ||
2715 | /* Cleans dead PIDs from our list */ | |
2716 | ||
50be4f4a LP |
2717 | except1 = unit_main_pid(u); |
2718 | except2 = unit_control_pid(u); | |
2719 | ||
a911bb9a | 2720 | SET_FOREACH(e, u->pids, i) { |
fea72cc0 | 2721 | pid_t pid = PTR_TO_PID(e); |
a911bb9a LP |
2722 | |
2723 | if (pid == except1 || pid == except2) | |
2724 | continue; | |
2725 | ||
9f5650ae | 2726 | if (!pid_is_unwaited(pid)) |
bd44e61b | 2727 | unit_unwatch_pid(u, pid); |
a911bb9a | 2728 | } |
87f0e418 LP |
2729 | } |
2730 | ||
50be4f4a LP |
2731 | static int on_rewatch_pids_event(sd_event_source *s, void *userdata) { |
2732 | Unit *u = userdata; | |
2733 | ||
2734 | assert(s); | |
2735 | assert(u); | |
2736 | ||
2737 | unit_tidy_watch_pids(u); | |
2738 | unit_watch_all_pids(u); | |
2739 | ||
2740 | /* If the PID set is empty now, then let's finish this off. */ | |
2741 | unit_synthesize_cgroup_empty_event(u); | |
2742 | ||
2743 | return 0; | |
2744 | } | |
2745 | ||
2746 | int unit_enqueue_rewatch_pids(Unit *u) { | |
2747 | int r; | |
2748 | ||
2749 | assert(u); | |
2750 | ||
2751 | if (!u->cgroup_path) | |
2752 | return -ENOENT; | |
2753 | ||
2754 | r = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER); | |
2755 | if (r < 0) | |
2756 | return r; | |
2757 | if (r > 0) /* On unified we can use proper notifications */ | |
2758 | return 0; | |
2759 | ||
2760 | /* Enqueues a low-priority job that will clean up dead PIDs from our list of PIDs to watch and subscribe to new | |
2761 | * PIDs that might have appeared. We do this in a delayed job because the work might be quite slow, as it | |
2762 | * involves issuing kill(pid, 0) on all processes we watch. */ | |
2763 | ||
2764 | if (!u->rewatch_pids_event_source) { | |
2765 | _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; | |
2766 | ||
2767 | r = sd_event_add_defer(u->manager->event, &s, on_rewatch_pids_event, u); | |
2768 | if (r < 0) | |
2769 | return log_error_errno(r, "Failed to allocate event source for tidying watched PIDs: %m"); | |
2770 | ||
2771 | r = sd_event_source_set_priority(s, SD_EVENT_PRIORITY_IDLE); | |
2772 | if (r < 0) | |
2773 | return log_error_errno(r, "Failed to adjust priority of event source for tidying watched PIDs: m"); | |
2774 | ||
2775 | (void) sd_event_source_set_description(s, "tidy-watch-pids"); | |
2776 | ||
2777 | u->rewatch_pids_event_source = TAKE_PTR(s); | |
2778 | } | |
2779 | ||
2780 | r = sd_event_source_set_enabled(u->rewatch_pids_event_source, SD_EVENT_ONESHOT); | |
2781 | if (r < 0) | |
2782 | return log_error_errno(r, "Failed to enable event source for tidying watched PIDs: %m"); | |
2783 | ||
2784 | return 0; | |
2785 | } | |
2786 | ||
2787 | void unit_dequeue_rewatch_pids(Unit *u) { | |
2788 | int r; | |
2789 | assert(u); | |
2790 | ||
2791 | if (!u->rewatch_pids_event_source) | |
2792 | return; | |
2793 | ||
2794 | r = sd_event_source_set_enabled(u->rewatch_pids_event_source, SD_EVENT_OFF); | |
2795 | if (r < 0) | |
2796 | log_warning_errno(r, "Failed to disable event source for tidying watched PIDs, ignoring: %m"); | |
2797 | ||
2798 | u->rewatch_pids_event_source = sd_event_source_unref(u->rewatch_pids_event_source); | |
2799 | } | |
2800 | ||
87f0e418 LP |
2801 | bool unit_job_is_applicable(Unit *u, JobType j) { |
2802 | assert(u); | |
2803 | assert(j >= 0 && j < _JOB_TYPE_MAX); | |
2804 | ||
2805 | switch (j) { | |
2806 | ||
2807 | case JOB_VERIFY_ACTIVE: | |
2808 | case JOB_START: | |
e0209d83 | 2809 | case JOB_NOP: |
f5869324 LP |
2810 | /* Note that we don't check unit_can_start() here. That's because .device units and suchlike are not |
2811 | * startable by us but may appear due to external events, and it thus makes sense to permit enqueing | |
2812 | * jobs for it. */ | |
87f0e418 LP |
2813 | return true; |
2814 | ||
f5869324 LP |
2815 | case JOB_STOP: |
2816 | /* Similar as above. However, perpetual units can never be stopped (neither explicitly nor due to | |
2817 | * external events), hence it makes no sense to permit enqueing such a request either. */ | |
2818 | return !u->perpetual; | |
2819 | ||
87f0e418 LP |
2820 | case JOB_RESTART: |
2821 | case JOB_TRY_RESTART: | |
f5869324 | 2822 | return unit_can_stop(u) && unit_can_start(u); |
87f0e418 LP |
2823 | |
2824 | case JOB_RELOAD: | |
3282591d | 2825 | case JOB_TRY_RELOAD: |
87f0e418 LP |
2826 | return unit_can_reload(u); |
2827 | ||
2828 | case JOB_RELOAD_OR_START: | |
2829 | return unit_can_reload(u) && unit_can_start(u); | |
2830 | ||
2831 | default: | |
2832 | assert_not_reached("Invalid job type"); | |
2833 | } | |
2834 | } | |
2835 | ||
f2341e0a LP |
2836 | static void maybe_warn_about_dependency(Unit *u, const char *other, UnitDependency dependency) { |
2837 | assert(u); | |
d1fab3fe | 2838 | |
f2341e0a LP |
2839 | /* Only warn about some unit types */ |
2840 | if (!IN_SET(dependency, UNIT_CONFLICTS, UNIT_CONFLICTED_BY, UNIT_BEFORE, UNIT_AFTER, UNIT_ON_FAILURE, UNIT_TRIGGERS, UNIT_TRIGGERED_BY)) | |
2841 | return; | |
3f3cc397 | 2842 | |
f2341e0a LP |
2843 | if (streq_ptr(u->id, other)) |
2844 | log_unit_warning(u, "Dependency %s=%s dropped", unit_dependency_to_string(dependency), u->id); | |
2845 | else | |
2846 | log_unit_warning(u, "Dependency %s=%s dropped, merged into %s", unit_dependency_to_string(dependency), strna(other), u->id); | |
d1fab3fe ZJS |
2847 | } |
2848 | ||
eef85c4a LP |
2849 | static int unit_add_dependency_hashmap( |
2850 | Hashmap **h, | |
2851 | Unit *other, | |
2852 | UnitDependencyMask origin_mask, | |
2853 | UnitDependencyMask destination_mask) { | |
2854 | ||
2855 | UnitDependencyInfo info; | |
2856 | int r; | |
2857 | ||
2858 | assert(h); | |
2859 | assert(other); | |
2860 | assert(origin_mask < _UNIT_DEPENDENCY_MASK_FULL); | |
2861 | assert(destination_mask < _UNIT_DEPENDENCY_MASK_FULL); | |
2862 | assert(origin_mask > 0 || destination_mask > 0); | |
2863 | ||
2864 | r = hashmap_ensure_allocated(h, NULL); | |
2865 | if (r < 0) | |
2866 | return r; | |
2867 | ||
2868 | assert_cc(sizeof(void*) == sizeof(info)); | |
2869 | ||
2870 | info.data = hashmap_get(*h, other); | |
2871 | if (info.data) { | |
2872 | /* Entry already exists. Add in our mask. */ | |
2873 | ||
d94a24ca ZJS |
2874 | if (FLAGS_SET(origin_mask, info.origin_mask) && |
2875 | FLAGS_SET(destination_mask, info.destination_mask)) | |
eef85c4a LP |
2876 | return 0; /* NOP */ |
2877 | ||
2878 | info.origin_mask |= origin_mask; | |
2879 | info.destination_mask |= destination_mask; | |
2880 | ||
2881 | r = hashmap_update(*h, other, info.data); | |
2882 | } else { | |
2883 | info = (UnitDependencyInfo) { | |
2884 | .origin_mask = origin_mask, | |
2885 | .destination_mask = destination_mask, | |
2886 | }; | |
2887 | ||
2888 | r = hashmap_put(*h, other, info.data); | |
2889 | } | |
2890 | if (r < 0) | |
2891 | return r; | |
2892 | ||
2893 | return 1; | |
2894 | } | |
2895 | ||
2896 | int unit_add_dependency( | |
2897 | Unit *u, | |
2898 | UnitDependency d, | |
2899 | Unit *other, | |
2900 | bool add_reference, | |
2901 | UnitDependencyMask mask) { | |
87f0e418 LP |
2902 | |
2903 | static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = { | |
2904 | [UNIT_REQUIRES] = UNIT_REQUIRED_BY, | |
87f0e418 | 2905 | [UNIT_WANTS] = UNIT_WANTED_BY, |
be7d9ff7 | 2906 | [UNIT_REQUISITE] = UNIT_REQUISITE_OF, |
7f2cddae | 2907 | [UNIT_BINDS_TO] = UNIT_BOUND_BY, |
60649f17 | 2908 | [UNIT_PART_OF] = UNIT_CONSISTS_OF, |
be7d9ff7 | 2909 | [UNIT_REQUIRED_BY] = UNIT_REQUIRES, |
be7d9ff7 | 2910 | [UNIT_REQUISITE_OF] = UNIT_REQUISITE, |
be7d9ff7 | 2911 | [UNIT_WANTED_BY] = UNIT_WANTS, |
7f2cddae | 2912 | [UNIT_BOUND_BY] = UNIT_BINDS_TO, |
60649f17 | 2913 | [UNIT_CONSISTS_OF] = UNIT_PART_OF, |
69dd2852 LP |
2914 | [UNIT_CONFLICTS] = UNIT_CONFLICTED_BY, |
2915 | [UNIT_CONFLICTED_BY] = UNIT_CONFLICTS, | |
87f0e418 | 2916 | [UNIT_BEFORE] = UNIT_AFTER, |
701cc384 | 2917 | [UNIT_AFTER] = UNIT_BEFORE, |
5de9682c | 2918 | [UNIT_ON_FAILURE] = _UNIT_DEPENDENCY_INVALID, |
701cc384 | 2919 | [UNIT_REFERENCES] = UNIT_REFERENCED_BY, |
57020a3a LP |
2920 | [UNIT_REFERENCED_BY] = UNIT_REFERENCES, |
2921 | [UNIT_TRIGGERS] = UNIT_TRIGGERED_BY, | |
4dcc1cb4 | 2922 | [UNIT_TRIGGERED_BY] = UNIT_TRIGGERS, |
7f2cddae | 2923 | [UNIT_PROPAGATES_RELOAD_TO] = UNIT_RELOAD_PROPAGATED_FROM, |
85e9a101 | 2924 | [UNIT_RELOAD_PROPAGATED_FROM] = UNIT_PROPAGATES_RELOAD_TO, |
613b411c | 2925 | [UNIT_JOINS_NAMESPACE_OF] = UNIT_JOINS_NAMESPACE_OF, |
87f0e418 | 2926 | }; |
eef85c4a LP |
2927 | Unit *original_u = u, *original_other = other; |
2928 | int r; | |
87f0e418 LP |
2929 | |
2930 | assert(u); | |
2931 | assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX); | |
87f0e418 LP |
2932 | assert(other); |
2933 | ||
9f151f29 LP |
2934 | u = unit_follow_merge(u); |
2935 | other = unit_follow_merge(other); | |
2936 | ||
87f0e418 LP |
2937 | /* We won't allow dependencies on ourselves. We will not |
2938 | * consider them an error however. */ | |
d1fab3fe | 2939 | if (u == other) { |
eef85c4a | 2940 | maybe_warn_about_dependency(original_u, original_other->id, d); |
87f0e418 | 2941 | return 0; |
d1fab3fe | 2942 | } |
87f0e418 | 2943 | |
eef85c4a LP |
2944 | if ((d == UNIT_BEFORE && other->type == UNIT_DEVICE) || |
2945 | (d == UNIT_AFTER && u->type == UNIT_DEVICE)) { | |
dd5e7000 ZJS |
2946 | log_unit_warning(u, "Dependency Before=%s ignored (.device units cannot be delayed)", other->id); |
2947 | return 0; | |
2948 | } | |
2949 | ||
eef85c4a | 2950 | r = unit_add_dependency_hashmap(u->dependencies + d, other, mask, 0); |
613b411c | 2951 | if (r < 0) |
87f0e418 LP |
2952 | return r; |
2953 | ||
eef85c4a LP |
2954 | if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID && inverse_table[d] != d) { |
2955 | r = unit_add_dependency_hashmap(other->dependencies + inverse_table[d], u, 0, mask); | |
613b411c LP |
2956 | if (r < 0) |
2957 | return r; | |
2958 | } | |
2959 | ||
2960 | if (add_reference) { | |
eef85c4a | 2961 | r = unit_add_dependency_hashmap(u->dependencies + UNIT_REFERENCES, other, mask, 0); |
613b411c | 2962 | if (r < 0) |
5de9682c LP |
2963 | return r; |
2964 | ||
eef85c4a | 2965 | r = unit_add_dependency_hashmap(other->dependencies + UNIT_REFERENCED_BY, u, 0, mask); |
613b411c | 2966 | if (r < 0) |
701cc384 | 2967 | return r; |
613b411c | 2968 | } |
87f0e418 | 2969 | |
c1e1601e | 2970 | unit_add_to_dbus_queue(u); |
87f0e418 LP |
2971 | return 0; |
2972 | } | |
0301abf4 | 2973 | |
eef85c4a | 2974 | int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference, UnitDependencyMask mask) { |
2c966c03 LP |
2975 | int r; |
2976 | ||
2977 | assert(u); | |
2978 | ||
eef85c4a | 2979 | r = unit_add_dependency(u, d, other, add_reference, mask); |
3f3cc397 | 2980 | if (r < 0) |
2c966c03 LP |
2981 | return r; |
2982 | ||
eef85c4a | 2983 | return unit_add_dependency(u, e, other, add_reference, mask); |
2c966c03 LP |
2984 | } |
2985 | ||
23e8c796 | 2986 | static int resolve_template(Unit *u, const char *name, char **buf, const char **ret) { |
7410616c | 2987 | int r; |
9e2f7c11 LP |
2988 | |
2989 | assert(u); | |
23e8c796 | 2990 | assert(name); |
7410616c LP |
2991 | assert(buf); |
2992 | assert(ret); | |
9e2f7c11 | 2993 | |
7410616c LP |
2994 | if (!unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
2995 | *buf = NULL; | |
2996 | *ret = name; | |
2997 | return 0; | |
9e2f7c11 LP |
2998 | } |
2999 | ||
ac155bb8 | 3000 | if (u->instance) |
7410616c | 3001 | r = unit_name_replace_instance(name, u->instance, buf); |
9e2f7c11 | 3002 | else { |
ae018d9b | 3003 | _cleanup_free_ char *i = NULL; |
9e2f7c11 | 3004 | |
7410616c LP |
3005 | r = unit_name_to_prefix(u->id, &i); |
3006 | if (r < 0) | |
3007 | return r; | |
9e2f7c11 | 3008 | |
7410616c | 3009 | r = unit_name_replace_instance(name, i, buf); |
9e2f7c11 | 3010 | } |
7410616c LP |
3011 | if (r < 0) |
3012 | return r; | |
9e2f7c11 | 3013 | |
7410616c LP |
3014 | *ret = *buf; |
3015 | return 0; | |
9e2f7c11 LP |
3016 | } |
3017 | ||
35d8c19a | 3018 | int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, bool add_reference, UnitDependencyMask mask) { |
7410616c | 3019 | _cleanup_free_ char *buf = NULL; |
09b6b09f LP |
3020 | Unit *other; |
3021 | int r; | |
3022 | ||
9e2f7c11 | 3023 | assert(u); |
35d8c19a | 3024 | assert(name); |
09b6b09f | 3025 | |
23e8c796 | 3026 | r = resolve_template(u, name, &buf, &name); |
7410616c LP |
3027 | if (r < 0) |
3028 | return r; | |
09b6b09f | 3029 | |
35d8c19a | 3030 | r = manager_load_unit(u->manager, name, NULL, NULL, &other); |
8afbb8e1 LP |
3031 | if (r < 0) |
3032 | return r; | |
9e2f7c11 | 3033 | |
eef85c4a | 3034 | return unit_add_dependency(u, d, other, add_reference, mask); |
09b6b09f LP |
3035 | } |
3036 | ||
5a724170 | 3037 | int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, bool add_reference, UnitDependencyMask mask) { |
7410616c | 3038 | _cleanup_free_ char *buf = NULL; |
2c966c03 LP |
3039 | Unit *other; |
3040 | int r; | |
2c966c03 LP |
3041 | |
3042 | assert(u); | |
5a724170 | 3043 | assert(name); |
2c966c03 | 3044 | |
23e8c796 | 3045 | r = resolve_template(u, name, &buf, &name); |
7410616c LP |
3046 | if (r < 0) |
3047 | return r; | |
2c966c03 | 3048 | |
5a724170 | 3049 | r = manager_load_unit(u->manager, name, NULL, NULL, &other); |
3f3cc397 | 3050 | if (r < 0) |
68eda4bd | 3051 | return r; |
2c966c03 | 3052 | |
eef85c4a | 3053 | return unit_add_two_dependencies(u, d, e, other, add_reference, mask); |
2c966c03 LP |
3054 | } |
3055 | ||
0301abf4 | 3056 | int set_unit_path(const char *p) { |
0301abf4 | 3057 | /* This is mostly for debug purposes */ |
cbe46ead | 3058 | if (setenv("SYSTEMD_UNIT_PATH", p, 1) < 0) |
26d04f86 | 3059 | return -errno; |
0301abf4 LP |
3060 | |
3061 | return 0; | |
3062 | } | |
88066b3a | 3063 | |
ea430986 | 3064 | char *unit_dbus_path(Unit *u) { |
ea430986 LP |
3065 | assert(u); |
3066 | ||
ac155bb8 | 3067 | if (!u->id) |
04ade7d2 LP |
3068 | return NULL; |
3069 | ||
48899192 | 3070 | return unit_dbus_path_from_name(u->id); |
ea430986 LP |
3071 | } |
3072 | ||
4b58153d LP |
3073 | char *unit_dbus_path_invocation_id(Unit *u) { |
3074 | assert(u); | |
3075 | ||
3076 | if (sd_id128_is_null(u->invocation_id)) | |
3077 | return NULL; | |
3078 | ||
3079 | return unit_dbus_path_from_name(u->invocation_id_string); | |
3080 | } | |
3081 | ||
d79200e2 LP |
3082 | int unit_set_slice(Unit *u, Unit *slice) { |
3083 | assert(u); | |
3084 | assert(slice); | |
3085 | ||
3086 | /* Sets the unit slice if it has not been set before. Is extra | |
3087 | * careful, to only allow this for units that actually have a | |
3088 | * cgroup context. Also, we don't allow to set this for slices | |
3089 | * (since the parent slice is derived from the name). Make | |
3090 | * sure the unit we set is actually a slice. */ | |
3091 | ||
3092 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) | |
3093 | return -EOPNOTSUPP; | |
3094 | ||
3095 | if (u->type == UNIT_SLICE) | |
3096 | return -EINVAL; | |
3097 | ||
102ef982 LP |
3098 | if (unit_active_state(u) != UNIT_INACTIVE) |
3099 | return -EBUSY; | |
3100 | ||
d79200e2 LP |
3101 | if (slice->type != UNIT_SLICE) |
3102 | return -EINVAL; | |
3103 | ||
efdb0237 LP |
3104 | if (unit_has_name(u, SPECIAL_INIT_SCOPE) && |
3105 | !unit_has_name(slice, SPECIAL_ROOT_SLICE)) | |
3106 | return -EPERM; | |
3107 | ||
d79200e2 LP |
3108 | if (UNIT_DEREF(u->slice) == slice) |
3109 | return 0; | |
3110 | ||
99e66921 TH |
3111 | /* Disallow slice changes if @u is already bound to cgroups */ |
3112 | if (UNIT_ISSET(u->slice) && u->cgroup_realized) | |
d79200e2 LP |
3113 | return -EBUSY; |
3114 | ||
7f7d01ed | 3115 | unit_ref_set(&u->slice, u, slice); |
d79200e2 LP |
3116 | return 1; |
3117 | } | |
3118 | ||
3119 | int unit_set_default_slice(Unit *u) { | |
a8833944 | 3120 | const char *slice_name; |
a016b922 LP |
3121 | Unit *slice; |
3122 | int r; | |
3123 | ||
3124 | assert(u); | |
3125 | ||
9444b1f2 | 3126 | if (UNIT_ISSET(u->slice)) |
a016b922 LP |
3127 | return 0; |
3128 | ||
a8833944 LP |
3129 | if (u->instance) { |
3130 | _cleanup_free_ char *prefix = NULL, *escaped = NULL; | |
68eda4bd | 3131 | |
a8833944 LP |
3132 | /* Implicitly place all instantiated units in their |
3133 | * own per-template slice */ | |
3134 | ||
7410616c LP |
3135 | r = unit_name_to_prefix(u->id, &prefix); |
3136 | if (r < 0) | |
3137 | return r; | |
a8833944 LP |
3138 | |
3139 | /* The prefix is already escaped, but it might include | |
3140 | * "-" which has a special meaning for slice units, | |
3141 | * hence escape it here extra. */ | |
7410616c | 3142 | escaped = unit_name_escape(prefix); |
a8833944 LP |
3143 | if (!escaped) |
3144 | return -ENOMEM; | |
3145 | ||
463d0d15 | 3146 | if (MANAGER_IS_SYSTEM(u->manager)) |
00e7b3c8 | 3147 | slice_name = strjoina("system-", escaped, ".slice"); |
a8833944 | 3148 | else |
00e7b3c8 | 3149 | slice_name = strjoina(escaped, ".slice"); |
a8833944 LP |
3150 | } else |
3151 | slice_name = | |
463d0d15 | 3152 | MANAGER_IS_SYSTEM(u->manager) && !unit_has_name(u, SPECIAL_INIT_SCOPE) |
a8833944 LP |
3153 | ? SPECIAL_SYSTEM_SLICE |
3154 | : SPECIAL_ROOT_SLICE; | |
3155 | ||
3156 | r = manager_load_unit(u->manager, slice_name, NULL, NULL, &slice); | |
a016b922 LP |
3157 | if (r < 0) |
3158 | return r; | |
3159 | ||
d79200e2 | 3160 | return unit_set_slice(u, slice); |
a016b922 LP |
3161 | } |
3162 | ||
9444b1f2 LP |
3163 | const char *unit_slice_name(Unit *u) { |
3164 | assert(u); | |
3165 | ||
3166 | if (!UNIT_ISSET(u->slice)) | |
3167 | return NULL; | |
3168 | ||
3169 | return UNIT_DEREF(u->slice)->id; | |
3170 | } | |
3171 | ||
f6ff8c29 | 3172 | int unit_load_related_unit(Unit *u, const char *type, Unit **_found) { |
78edb35a | 3173 | _cleanup_free_ char *t = NULL; |
f6ff8c29 LP |
3174 | int r; |
3175 | ||
3176 | assert(u); | |
3177 | assert(type); | |
3178 | assert(_found); | |
3179 | ||
7410616c LP |
3180 | r = unit_name_change_suffix(u->id, type, &t); |
3181 | if (r < 0) | |
3182 | return r; | |
3183 | if (unit_has_name(u, t)) | |
3184 | return -EINVAL; | |
f6ff8c29 | 3185 | |
ac155bb8 | 3186 | r = manager_load_unit(u->manager, t, NULL, NULL, _found); |
9e2f7c11 | 3187 | assert(r < 0 || *_found != u); |
f6ff8c29 LP |
3188 | return r; |
3189 | } | |
3190 | ||
bbc29086 DM |
3191 | static int signal_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
3192 | const char *name, *old_owner, *new_owner; | |
3193 | Unit *u = userdata; | |
3194 | int r; | |
3195 | ||
3196 | assert(message); | |
3197 | assert(u); | |
3198 | ||
3199 | r = sd_bus_message_read(message, "sss", &name, &old_owner, &new_owner); | |
3200 | if (r < 0) { | |
3201 | bus_log_parse_error(r); | |
3202 | return 0; | |
3203 | } | |
3204 | ||
a8ea93a5 LP |
3205 | old_owner = empty_to_null(old_owner); |
3206 | new_owner = empty_to_null(new_owner); | |
b0076268 | 3207 | |
bbc29086 DM |
3208 | if (UNIT_VTABLE(u)->bus_name_owner_change) |
3209 | UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner); | |
3210 | ||
3211 | return 0; | |
3212 | } | |
3213 | ||
9806e87d LP |
3214 | int unit_install_bus_match(Unit *u, sd_bus *bus, const char *name) { |
3215 | const char *match; | |
bbc29086 | 3216 | |
9806e87d LP |
3217 | assert(u); |
3218 | assert(bus); | |
3219 | assert(name); | |
bbc29086 DM |
3220 | |
3221 | if (u->match_bus_slot) | |
3222 | return -EBUSY; | |
3223 | ||
9806e87d | 3224 | match = strjoina("type='signal'," |
81d62103 ZJS |
3225 | "sender='org.freedesktop.DBus'," |
3226 | "path='/org/freedesktop/DBus'," | |
3227 | "interface='org.freedesktop.DBus'," | |
3228 | "member='NameOwnerChanged'," | |
3229 | "arg0='", name, "'"); | |
bbc29086 | 3230 | |
75152a4d | 3231 | return sd_bus_add_match_async(bus, &u->match_bus_slot, match, signal_name_owner_changed, NULL, u); |
bbc29086 DM |
3232 | } |
3233 | ||
05e343b7 | 3234 | int unit_watch_bus_name(Unit *u, const char *name) { |
bbc29086 DM |
3235 | int r; |
3236 | ||
05e343b7 LP |
3237 | assert(u); |
3238 | assert(name); | |
3239 | ||
3240 | /* Watch a specific name on the bus. We only support one unit | |
3241 | * watching each name for now. */ | |
3242 | ||
bbc29086 DM |
3243 | if (u->manager->api_bus) { |
3244 | /* If the bus is already available, install the match directly. | |
3245 | * Otherwise, just put the name in the list. bus_setup_api() will take care later. */ | |
9806e87d | 3246 | r = unit_install_bus_match(u, u->manager->api_bus, name); |
bbc29086 | 3247 | if (r < 0) |
8ea823b6 | 3248 | return log_warning_errno(r, "Failed to subscribe to NameOwnerChanged signal for '%s': %m", name); |
bbc29086 DM |
3249 | } |
3250 | ||
3251 | r = hashmap_put(u->manager->watch_bus, name, u); | |
3252 | if (r < 0) { | |
3253 | u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); | |
3254 | return log_warning_errno(r, "Failed to put bus name to hashmap: %m"); | |
3255 | } | |
3256 | ||
3257 | return 0; | |
05e343b7 LP |
3258 | } |
3259 | ||
3260 | void unit_unwatch_bus_name(Unit *u, const char *name) { | |
3261 | assert(u); | |
3262 | assert(name); | |
3263 | ||
8367fea5 | 3264 | (void) hashmap_remove_value(u->manager->watch_bus, name, u); |
bbc29086 | 3265 | u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); |
05e343b7 LP |
3266 | } |
3267 | ||
a16e1123 LP |
3268 | bool unit_can_serialize(Unit *u) { |
3269 | assert(u); | |
3270 | ||
3271 | return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item; | |
3272 | } | |
3273 | ||
d68c645b | 3274 | static int serialize_cgroup_mask(FILE *f, const char *key, CGroupMask mask) { |
8b108bd0 | 3275 | _cleanup_free_ char *s = NULL; |
d68c645b | 3276 | int r; |
8b108bd0 FB |
3277 | |
3278 | assert(f); | |
3279 | assert(key); | |
3280 | ||
d68c645b LP |
3281 | if (mask == 0) |
3282 | return 0; | |
3283 | ||
3284 | r = cg_mask_to_string(mask, &s); | |
3285 | if (r < 0) | |
3286 | return log_error_errno(r, "Failed to format cgroup mask: %m"); | |
3287 | ||
3288 | return serialize_item(f, key, s); | |
8b108bd0 FB |
3289 | } |
3290 | ||
b82f71c7 | 3291 | static const char *const ip_accounting_metric_field[_CGROUP_IP_ACCOUNTING_METRIC_MAX] = { |
6b659ed8 LP |
3292 | [CGROUP_IP_INGRESS_BYTES] = "ip-accounting-ingress-bytes", |
3293 | [CGROUP_IP_INGRESS_PACKETS] = "ip-accounting-ingress-packets", | |
3294 | [CGROUP_IP_EGRESS_BYTES] = "ip-accounting-egress-bytes", | |
3295 | [CGROUP_IP_EGRESS_PACKETS] = "ip-accounting-egress-packets", | |
3296 | }; | |
3297 | ||
fbe14fc9 LP |
3298 | static const char *const io_accounting_metric_field_base[_CGROUP_IO_ACCOUNTING_METRIC_MAX] = { |
3299 | [CGROUP_IO_READ_BYTES] = "io-accounting-read-bytes-base", | |
3300 | [CGROUP_IO_WRITE_BYTES] = "io-accounting-write-bytes-base", | |
3301 | [CGROUP_IO_READ_OPERATIONS] = "io-accounting-read-operations-base", | |
3302 | [CGROUP_IO_WRITE_OPERATIONS] = "io-accounting-write-operations-base", | |
3303 | }; | |
3304 | ||
3305 | static const char *const io_accounting_metric_field_last[_CGROUP_IO_ACCOUNTING_METRIC_MAX] = { | |
3306 | [CGROUP_IO_READ_BYTES] = "io-accounting-read-bytes-last", | |
3307 | [CGROUP_IO_WRITE_BYTES] = "io-accounting-write-bytes-last", | |
3308 | [CGROUP_IO_READ_OPERATIONS] = "io-accounting-read-operations-last", | |
3309 | [CGROUP_IO_WRITE_OPERATIONS] = "io-accounting-write-operations-last", | |
3310 | }; | |
3311 | ||
6b78f9b4 | 3312 | int unit_serialize(Unit *u, FILE *f, FDSet *fds, bool serialize_jobs) { |
6b659ed8 | 3313 | CGroupIPAccountingMetric m; |
a16e1123 LP |
3314 | int r; |
3315 | ||
3316 | assert(u); | |
3317 | assert(f); | |
3318 | assert(fds); | |
3319 | ||
9bdb98c5 | 3320 | if (unit_can_serialize(u)) { |
9bdb98c5 | 3321 | r = UNIT_VTABLE(u)->serialize(u, f, fds); |
613b411c LP |
3322 | if (r < 0) |
3323 | return r; | |
e0209d83 MS |
3324 | } |
3325 | ||
d68c645b | 3326 | (void) serialize_dual_timestamp(f, "state-change-timestamp", &u->state_change_timestamp); |
a483fb59 | 3327 | |
d68c645b LP |
3328 | (void) serialize_dual_timestamp(f, "inactive-exit-timestamp", &u->inactive_exit_timestamp); |
3329 | (void) serialize_dual_timestamp(f, "active-enter-timestamp", &u->active_enter_timestamp); | |
3330 | (void) serialize_dual_timestamp(f, "active-exit-timestamp", &u->active_exit_timestamp); | |
3331 | (void) serialize_dual_timestamp(f, "inactive-enter-timestamp", &u->inactive_enter_timestamp); | |
a483fb59 | 3332 | |
d68c645b LP |
3333 | (void) serialize_dual_timestamp(f, "condition-timestamp", &u->condition_timestamp); |
3334 | (void) serialize_dual_timestamp(f, "assert-timestamp", &u->assert_timestamp); | |
2791a8f8 | 3335 | |
ac155bb8 | 3336 | if (dual_timestamp_is_set(&u->condition_timestamp)) |
d68c645b | 3337 | (void) serialize_bool(f, "condition-result", u->condition_result); |
10717a1a | 3338 | |
59fccdc5 | 3339 | if (dual_timestamp_is_set(&u->assert_timestamp)) |
d68c645b | 3340 | (void) serialize_bool(f, "assert-result", u->assert_result); |
59fccdc5 | 3341 | |
d68c645b LP |
3342 | (void) serialize_bool(f, "transient", u->transient); |
3343 | (void) serialize_bool(f, "in-audit", u->in_audit); | |
fe700f46 | 3344 | |
d68c645b LP |
3345 | (void) serialize_bool(f, "exported-invocation-id", u->exported_invocation_id); |
3346 | (void) serialize_bool(f, "exported-log-level-max", u->exported_log_level_max); | |
3347 | (void) serialize_bool(f, "exported-log-extra-fields", u->exported_log_extra_fields); | |
3348 | (void) serialize_bool(f, "exported-log-rate-limit-interval", u->exported_log_rate_limit_interval); | |
3349 | (void) serialize_bool(f, "exported-log-rate-limit-burst", u->exported_log_rate_limit_burst); | |
0e699122 | 3350 | |
d68c645b | 3351 | (void) serialize_item_format(f, "cpu-usage-base", "%" PRIu64, u->cpu_usage_base); |
fe700f46 | 3352 | if (u->cpu_usage_last != NSEC_INFINITY) |
d68c645b | 3353 | (void) serialize_item_format(f, "cpu-usage-last", "%" PRIu64, u->cpu_usage_last); |
c2756a68 | 3354 | |
afcfaa69 LP |
3355 | if (u->oom_kill_last > 0) |
3356 | (void) serialize_item_format(f, "oom-kill-last", "%" PRIu64, u->oom_kill_last); | |
3357 | ||
fbe14fc9 LP |
3358 | for (CGroupIOAccountingMetric im = 0; im < _CGROUP_IO_ACCOUNTING_METRIC_MAX; im++) { |
3359 | (void) serialize_item_format(f, io_accounting_metric_field_base[im], "%" PRIu64, u->io_accounting_base[im]); | |
3360 | ||
3361 | if (u->io_accounting_last[im] != UINT64_MAX) | |
3362 | (void) serialize_item_format(f, io_accounting_metric_field_last[im], "%" PRIu64, u->io_accounting_last[im]); | |
3363 | } | |
3364 | ||
c2756a68 | 3365 | if (u->cgroup_path) |
d68c645b LP |
3366 | (void) serialize_item(f, "cgroup", u->cgroup_path); |
3367 | ||
3368 | (void) serialize_bool(f, "cgroup-realized", u->cgroup_realized); | |
3369 | (void) serialize_cgroup_mask(f, "cgroup-realized-mask", u->cgroup_realized_mask); | |
3370 | (void) serialize_cgroup_mask(f, "cgroup-enabled-mask", u->cgroup_enabled_mask); | |
3371 | (void) serialize_cgroup_mask(f, "cgroup-invalidated-mask", u->cgroup_invalidated_mask); | |
c2756a68 | 3372 | |
00d9ef85 | 3373 | if (uid_is_valid(u->ref_uid)) |
d68c645b | 3374 | (void) serialize_item_format(f, "ref-uid", UID_FMT, u->ref_uid); |
00d9ef85 | 3375 | if (gid_is_valid(u->ref_gid)) |
d68c645b | 3376 | (void) serialize_item_format(f, "ref-gid", GID_FMT, u->ref_gid); |
00d9ef85 | 3377 | |
4b58153d | 3378 | if (!sd_id128_is_null(u->invocation_id)) |
d68c645b | 3379 | (void) serialize_item_format(f, "invocation-id", SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(u->invocation_id)); |
4b58153d | 3380 | |
05a98afd LP |
3381 | bus_track_serialize(u->bus_track, f, "ref"); |
3382 | ||
6b659ed8 LP |
3383 | for (m = 0; m < _CGROUP_IP_ACCOUNTING_METRIC_MAX; m++) { |
3384 | uint64_t v; | |
3385 | ||
3386 | r = unit_get_ip_accounting(u, m, &v); | |
3387 | if (r >= 0) | |
d68c645b | 3388 | (void) serialize_item_format(f, ip_accounting_metric_field[m], "%" PRIu64, v); |
6b659ed8 LP |
3389 | } |
3390 | ||
613b411c LP |
3391 | if (serialize_jobs) { |
3392 | if (u->job) { | |
d68c645b | 3393 | fputs("job\n", f); |
05a98afd | 3394 | job_serialize(u->job, f); |
613b411c LP |
3395 | } |
3396 | ||
3397 | if (u->nop_job) { | |
d68c645b | 3398 | fputs("job\n", f); |
05a98afd | 3399 | job_serialize(u->nop_job, f); |
613b411c LP |
3400 | } |
3401 | } | |
3402 | ||
a16e1123 LP |
3403 | /* End marker */ |
3404 | fputc('\n', f); | |
3405 | return 0; | |
3406 | } | |
3407 | ||
b17c9620 LP |
3408 | static int unit_deserialize_job(Unit *u, FILE *f) { |
3409 | _cleanup_(job_freep) Job *j = NULL; | |
3410 | int r; | |
3411 | ||
3412 | assert(u); | |
3413 | assert(f); | |
3414 | ||
3415 | j = job_new_raw(u); | |
3416 | if (!j) | |
3417 | return log_oom(); | |
3418 | ||
3419 | r = job_deserialize(j, f); | |
3420 | if (r < 0) | |
3421 | return r; | |
3422 | ||
3423 | r = job_install_deserialized(j); | |
3424 | if (r < 0) | |
3425 | return r; | |
3426 | ||
3427 | TAKE_PTR(j); | |
3428 | return 0; | |
3429 | } | |
3430 | ||
a16e1123 LP |
3431 | int unit_deserialize(Unit *u, FILE *f, FDSet *fds) { |
3432 | int r; | |
3433 | ||
3434 | assert(u); | |
3435 | assert(f); | |
3436 | assert(fds); | |
3437 | ||
a16e1123 | 3438 | for (;;) { |
8948b341 | 3439 | _cleanup_free_ char *line = NULL; |
8948b341 | 3440 | char *l, *v; |
83f18c91 | 3441 | ssize_t m; |
a16e1123 LP |
3442 | size_t k; |
3443 | ||
8948b341 LP |
3444 | r = read_line(f, LONG_LINE_MAX, &line); |
3445 | if (r < 0) | |
3446 | return log_error_errno(r, "Failed to read serialization line: %m"); | |
3447 | if (r == 0) /* eof */ | |
3448 | break; | |
a16e1123 LP |
3449 | |
3450 | l = strstrip(line); | |
8948b341 | 3451 | if (isempty(l)) /* End marker */ |
a483fb59 | 3452 | break; |
a16e1123 LP |
3453 | |
3454 | k = strcspn(l, "="); | |
3455 | ||
3456 | if (l[k] == '=') { | |
3457 | l[k] = 0; | |
3458 | v = l+k+1; | |
3459 | } else | |
3460 | v = l+k; | |
3461 | ||
cca098b0 | 3462 | if (streq(l, "job")) { |
39a18c60 | 3463 | if (v[0] == '\0') { |
b17c9620 LP |
3464 | /* New-style serialized job */ |
3465 | r = unit_deserialize_job(u, f); | |
3466 | if (r < 0) | |
e0209d83 | 3467 | return r; |
b17c9620 | 3468 | } else /* Legacy for pre-44 */ |
ed10fa8c | 3469 | log_unit_warning(u, "Update from too old systemd versions are unsupported, cannot deserialize job: %s", v); |
cca098b0 | 3470 | continue; |
a483fb59 | 3471 | } else if (streq(l, "state-change-timestamp")) { |
d68c645b | 3472 | (void) deserialize_dual_timestamp(v, &u->state_change_timestamp); |
a483fb59 | 3473 | continue; |
8aaf019b | 3474 | } else if (streq(l, "inactive-exit-timestamp")) { |
d68c645b | 3475 | (void) deserialize_dual_timestamp(v, &u->inactive_exit_timestamp); |
8aaf019b LP |
3476 | continue; |
3477 | } else if (streq(l, "active-enter-timestamp")) { | |
d68c645b | 3478 | (void) deserialize_dual_timestamp(v, &u->active_enter_timestamp); |
8aaf019b LP |
3479 | continue; |
3480 | } else if (streq(l, "active-exit-timestamp")) { | |
d68c645b | 3481 | (void) deserialize_dual_timestamp(v, &u->active_exit_timestamp); |
8aaf019b LP |
3482 | continue; |
3483 | } else if (streq(l, "inactive-enter-timestamp")) { | |
d68c645b | 3484 | (void) deserialize_dual_timestamp(v, &u->inactive_enter_timestamp); |
8aaf019b | 3485 | continue; |
2791a8f8 | 3486 | } else if (streq(l, "condition-timestamp")) { |
d68c645b | 3487 | (void) deserialize_dual_timestamp(v, &u->condition_timestamp); |
2791a8f8 | 3488 | continue; |
59fccdc5 | 3489 | } else if (streq(l, "assert-timestamp")) { |
d68c645b | 3490 | (void) deserialize_dual_timestamp(v, &u->assert_timestamp); |
59fccdc5 | 3491 | continue; |
2791a8f8 | 3492 | } else if (streq(l, "condition-result")) { |
2791a8f8 | 3493 | |
e911de99 LP |
3494 | r = parse_boolean(v); |
3495 | if (r < 0) | |
f2341e0a | 3496 | log_unit_debug(u, "Failed to parse condition result value %s, ignoring.", v); |
2791a8f8 | 3497 | else |
e911de99 | 3498 | u->condition_result = r; |
efbac6d2 LP |
3499 | |
3500 | continue; | |
c2756a68 | 3501 | |
59fccdc5 | 3502 | } else if (streq(l, "assert-result")) { |
59fccdc5 | 3503 | |
e911de99 LP |
3504 | r = parse_boolean(v); |
3505 | if (r < 0) | |
f2341e0a | 3506 | log_unit_debug(u, "Failed to parse assert result value %s, ignoring.", v); |
59fccdc5 | 3507 | else |
e911de99 | 3508 | u->assert_result = r; |
59fccdc5 LP |
3509 | |
3510 | continue; | |
3511 | ||
c2756a68 | 3512 | } else if (streq(l, "transient")) { |
c2756a68 | 3513 | |
e911de99 LP |
3514 | r = parse_boolean(v); |
3515 | if (r < 0) | |
f2341e0a | 3516 | log_unit_debug(u, "Failed to parse transient bool %s, ignoring.", v); |
c2756a68 | 3517 | else |
e911de99 | 3518 | u->transient = r; |
c2756a68 LP |
3519 | |
3520 | continue; | |
e911de99 | 3521 | |
0e699122 LP |
3522 | } else if (streq(l, "in-audit")) { |
3523 | ||
3524 | r = parse_boolean(v); | |
3525 | if (r < 0) | |
3526 | log_unit_debug(u, "Failed to parse in-audit bool %s, ignoring.", v); | |
3527 | else | |
3528 | u->in_audit = r; | |
3529 | ||
3530 | continue; | |
3531 | ||
d3070fbd LP |
3532 | } else if (streq(l, "exported-invocation-id")) { |
3533 | ||
3534 | r = parse_boolean(v); | |
3535 | if (r < 0) | |
3536 | log_unit_debug(u, "Failed to parse exported invocation ID bool %s, ignoring.", v); | |
3537 | else | |
3538 | u->exported_invocation_id = r; | |
3539 | ||
3540 | continue; | |
3541 | ||
3542 | } else if (streq(l, "exported-log-level-max")) { | |
3543 | ||
3544 | r = parse_boolean(v); | |
3545 | if (r < 0) | |
3546 | log_unit_debug(u, "Failed to parse exported log level max bool %s, ignoring.", v); | |
3547 | else | |
3548 | u->exported_log_level_max = r; | |
3549 | ||
3550 | continue; | |
3551 | ||
3552 | } else if (streq(l, "exported-log-extra-fields")) { | |
3553 | ||
3554 | r = parse_boolean(v); | |
3555 | if (r < 0) | |
3556 | log_unit_debug(u, "Failed to parse exported log extra fields bool %s, ignoring.", v); | |
3557 | else | |
3558 | u->exported_log_extra_fields = r; | |
3559 | ||
3560 | continue; | |
3561 | ||
90fc172e AZ |
3562 | } else if (streq(l, "exported-log-rate-limit-interval")) { |
3563 | ||
3564 | r = parse_boolean(v); | |
3565 | if (r < 0) | |
3566 | log_unit_debug(u, "Failed to parse exported log rate limit interval %s, ignoring.", v); | |
3567 | else | |
3568 | u->exported_log_rate_limit_interval = r; | |
3569 | ||
3570 | continue; | |
3571 | ||
3572 | } else if (streq(l, "exported-log-rate-limit-burst")) { | |
3573 | ||
3574 | r = parse_boolean(v); | |
3575 | if (r < 0) | |
3576 | log_unit_debug(u, "Failed to parse exported log rate limit burst %s, ignoring.", v); | |
3577 | else | |
3578 | u->exported_log_rate_limit_burst = r; | |
3579 | ||
3580 | continue; | |
3581 | ||
fe700f46 | 3582 | } else if (STR_IN_SET(l, "cpu-usage-base", "cpuacct-usage-base")) { |
5ad096b3 | 3583 | |
66ebf6c0 | 3584 | r = safe_atou64(v, &u->cpu_usage_base); |
5ad096b3 | 3585 | if (r < 0) |
fe700f46 LP |
3586 | log_unit_debug(u, "Failed to parse CPU usage base %s, ignoring.", v); |
3587 | ||
3588 | continue; | |
3589 | ||
3590 | } else if (streq(l, "cpu-usage-last")) { | |
3591 | ||
3592 | r = safe_atou64(v, &u->cpu_usage_last); | |
3593 | if (r < 0) | |
3594 | log_unit_debug(u, "Failed to read CPU usage last %s, ignoring.", v); | |
5ad096b3 | 3595 | |
0f908397 | 3596 | continue; |
4e595329 | 3597 | |
afcfaa69 LP |
3598 | } else if (streq(l, "oom-kill-last")) { |
3599 | ||
3600 | r = safe_atou64(v, &u->oom_kill_last); | |
3601 | if (r < 0) | |
3602 | log_unit_debug(u, "Failed to read OOM kill last %s, ignoring.", v); | |
3603 | ||
3604 | continue; | |
3605 | ||
e911de99 | 3606 | } else if (streq(l, "cgroup")) { |
72673e86 | 3607 | |
e911de99 LP |
3608 | r = unit_set_cgroup_path(u, v); |
3609 | if (r < 0) | |
f2341e0a | 3610 | log_unit_debug_errno(u, r, "Failed to set cgroup path %s, ignoring: %m", v); |
4e595329 | 3611 | |
efdb0237 | 3612 | (void) unit_watch_cgroup(u); |
afcfaa69 | 3613 | (void) unit_watch_cgroup_memory(u); |
efdb0237 | 3614 | |
de1d4f9b WF |
3615 | continue; |
3616 | } else if (streq(l, "cgroup-realized")) { | |
3617 | int b; | |
3618 | ||
3619 | b = parse_boolean(v); | |
3620 | if (b < 0) | |
3621 | log_unit_debug(u, "Failed to parse cgroup-realized bool %s, ignoring.", v); | |
3622 | else | |
3623 | u->cgroup_realized = b; | |
3624 | ||
c2756a68 | 3625 | continue; |
00d9ef85 | 3626 | |
8b108bd0 FB |
3627 | } else if (streq(l, "cgroup-realized-mask")) { |
3628 | ||
3629 | r = cg_mask_from_string(v, &u->cgroup_realized_mask); | |
3630 | if (r < 0) | |
3631 | log_unit_debug(u, "Failed to parse cgroup-realized-mask %s, ignoring.", v); | |
3632 | continue; | |
3633 | ||
3634 | } else if (streq(l, "cgroup-enabled-mask")) { | |
3635 | ||
3636 | r = cg_mask_from_string(v, &u->cgroup_enabled_mask); | |
3637 | if (r < 0) | |
3638 | log_unit_debug(u, "Failed to parse cgroup-enabled-mask %s, ignoring.", v); | |
3639 | continue; | |
3640 | ||
17f14955 | 3641 | } else if (streq(l, "cgroup-invalidated-mask")) { |
906c06f6 | 3642 | |
17f14955 | 3643 | r = cg_mask_from_string(v, &u->cgroup_invalidated_mask); |
906c06f6 | 3644 | if (r < 0) |
17f14955 | 3645 | log_unit_debug(u, "Failed to parse cgroup-invalidated-mask %s, ignoring.", v); |
906c06f6 DM |
3646 | continue; |
3647 | ||
00d9ef85 LP |
3648 | } else if (streq(l, "ref-uid")) { |
3649 | uid_t uid; | |
3650 | ||
3651 | r = parse_uid(v, &uid); | |
3652 | if (r < 0) | |
3653 | log_unit_debug(u, "Failed to parse referenced UID %s, ignoring.", v); | |
3654 | else | |
3655 | unit_ref_uid_gid(u, uid, GID_INVALID); | |
3656 | ||
3657 | continue; | |
3658 | ||
3659 | } else if (streq(l, "ref-gid")) { | |
3660 | gid_t gid; | |
3661 | ||
3662 | r = parse_gid(v, &gid); | |
3663 | if (r < 0) | |
3664 | log_unit_debug(u, "Failed to parse referenced GID %s, ignoring.", v); | |
3665 | else | |
3666 | unit_ref_uid_gid(u, UID_INVALID, gid); | |
3667 | ||
5f616d5f LP |
3668 | continue; |
3669 | ||
05a98afd LP |
3670 | } else if (streq(l, "ref")) { |
3671 | ||
3672 | r = strv_extend(&u->deserialized_refs, v); | |
3673 | if (r < 0) | |
d68c645b | 3674 | return log_oom(); |
05a98afd | 3675 | |
4b58153d LP |
3676 | continue; |
3677 | } else if (streq(l, "invocation-id")) { | |
3678 | sd_id128_t id; | |
3679 | ||
3680 | r = sd_id128_from_string(v, &id); | |
3681 | if (r < 0) | |
3682 | log_unit_debug(u, "Failed to parse invocation id %s, ignoring.", v); | |
3683 | else { | |
3684 | r = unit_set_invocation_id(u, id); | |
3685 | if (r < 0) | |
3686 | log_unit_warning_errno(u, r, "Failed to set invocation ID for unit: %m"); | |
3687 | } | |
3688 | ||
00d9ef85 | 3689 | continue; |
8aaf019b | 3690 | } |
cca098b0 | 3691 | |
6b659ed8 | 3692 | /* Check if this is an IP accounting metric serialization field */ |
83f18c91 LP |
3693 | m = string_table_lookup(ip_accounting_metric_field, ELEMENTSOF(ip_accounting_metric_field), l); |
3694 | if (m >= 0) { | |
6b659ed8 LP |
3695 | uint64_t c; |
3696 | ||
3697 | r = safe_atou64(v, &c); | |
3698 | if (r < 0) | |
3699 | log_unit_debug(u, "Failed to parse IP accounting value %s, ignoring.", v); | |
3700 | else | |
3701 | u->ip_accounting_extra[m] = c; | |
3702 | continue; | |
3703 | } | |
3704 | ||
fbe14fc9 LP |
3705 | m = string_table_lookup(io_accounting_metric_field_base, ELEMENTSOF(io_accounting_metric_field_base), l); |
3706 | if (m >= 0) { | |
3707 | uint64_t c; | |
3708 | ||
3709 | r = safe_atou64(v, &c); | |
3710 | if (r < 0) | |
3711 | log_unit_debug(u, "Failed to parse IO accounting base value %s, ignoring.", v); | |
3712 | else | |
3713 | u->io_accounting_base[m] = c; | |
3714 | continue; | |
3715 | } | |
3716 | ||
3717 | m = string_table_lookup(io_accounting_metric_field_last, ELEMENTSOF(io_accounting_metric_field_last), l); | |
3718 | if (m >= 0) { | |
3719 | uint64_t c; | |
3720 | ||
3721 | r = safe_atou64(v, &c); | |
3722 | if (r < 0) | |
3723 | log_unit_debug(u, "Failed to parse IO accounting last value %s, ignoring.", v); | |
3724 | else | |
3725 | u->io_accounting_last[m] = c; | |
3726 | continue; | |
3727 | } | |
3728 | ||
9bdb98c5 | 3729 | if (unit_can_serialize(u)) { |
e8a565cb YW |
3730 | r = exec_runtime_deserialize_compat(u, l, v, fds); |
3731 | if (r < 0) { | |
3732 | log_unit_warning(u, "Failed to deserialize runtime parameter '%s', ignoring.", l); | |
3733 | continue; | |
9bdb98c5 LP |
3734 | } |
3735 | ||
e8a565cb YW |
3736 | /* Returns positive if key was handled by the call */ |
3737 | if (r > 0) | |
3738 | continue; | |
3739 | ||
9bdb98c5 | 3740 | r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds); |
613b411c | 3741 | if (r < 0) |
f2341e0a | 3742 | log_unit_warning(u, "Failed to deserialize unit parameter '%s', ignoring.", l); |
613b411c | 3743 | } |
a16e1123 | 3744 | } |
a483fb59 LP |
3745 | |
3746 | /* Versions before 228 did not carry a state change timestamp. In this case, take the current time. This is | |
3747 | * useful, so that timeouts based on this timestamp don't trigger too early, and is in-line with the logic from | |
1f133e0d | 3748 | * before 228 where the base for timeouts was not persistent across reboots. */ |
a483fb59 LP |
3749 | |
3750 | if (!dual_timestamp_is_set(&u->state_change_timestamp)) | |
3751 | dual_timestamp_get(&u->state_change_timestamp); | |
3752 | ||
58d83430 LP |
3753 | /* Let's make sure that everything that is deserialized also gets any potential new cgroup settings applied |
3754 | * after we are done. For that we invalidate anything already realized, so that we can realize it again. */ | |
3755 | unit_invalidate_cgroup(u, _CGROUP_MASK_ALL); | |
3756 | unit_invalidate_cgroup_bpf(u); | |
3757 | ||
a483fb59 | 3758 | return 0; |
a16e1123 LP |
3759 | } |
3760 | ||
8948b341 LP |
3761 | int unit_deserialize_skip(FILE *f) { |
3762 | int r; | |
07429866 ZJS |
3763 | assert(f); |
3764 | ||
3765 | /* Skip serialized data for this unit. We don't know what it is. */ | |
3766 | ||
3767 | for (;;) { | |
8948b341 LP |
3768 | _cleanup_free_ char *line = NULL; |
3769 | char *l; | |
07429866 | 3770 | |
8948b341 LP |
3771 | r = read_line(f, LONG_LINE_MAX, &line); |
3772 | if (r < 0) | |
3773 | return log_error_errno(r, "Failed to read serialization line: %m"); | |
3774 | if (r == 0) | |
3775 | return 0; | |
07429866 | 3776 | |
07429866 ZJS |
3777 | l = strstrip(line); |
3778 | ||
3779 | /* End marker */ | |
3780 | if (isempty(l)) | |
8948b341 | 3781 | return 1; |
07429866 ZJS |
3782 | } |
3783 | } | |
3784 | ||
eef85c4a | 3785 | int unit_add_node_dependency(Unit *u, const char *what, bool wants, UnitDependency dep, UnitDependencyMask mask) { |
6e2ef85b | 3786 | Unit *device; |
68eda4bd | 3787 | _cleanup_free_ char *e = NULL; |
6e2ef85b LP |
3788 | int r; |
3789 | ||
3790 | assert(u); | |
3791 | ||
6e2ef85b | 3792 | /* Adds in links to the device node that this unit is based on */ |
47bc12e1 LP |
3793 | if (isempty(what)) |
3794 | return 0; | |
6e2ef85b | 3795 | |
8407a5d0 | 3796 | if (!is_device_path(what)) |
6e2ef85b LP |
3797 | return 0; |
3798 | ||
47bc12e1 LP |
3799 | /* When device units aren't supported (such as in a |
3800 | * container), don't create dependencies on them. */ | |
1c2e9646 | 3801 | if (!unit_type_supported(UNIT_DEVICE)) |
47bc12e1 LP |
3802 | return 0; |
3803 | ||
7410616c LP |
3804 | r = unit_name_from_path(what, ".device", &e); |
3805 | if (r < 0) | |
3806 | return r; | |
6e2ef85b | 3807 | |
ac155bb8 | 3808 | r = manager_load_unit(u->manager, e, NULL, NULL, &device); |
6e2ef85b LP |
3809 | if (r < 0) |
3810 | return r; | |
3811 | ||
ebc8968b FB |
3812 | if (dep == UNIT_REQUIRES && device_shall_be_bound_by(device, u)) |
3813 | dep = UNIT_BINDS_TO; | |
3814 | ||
9d06297e | 3815 | r = unit_add_two_dependencies(u, UNIT_AFTER, |
463d0d15 | 3816 | MANAGER_IS_SYSTEM(u->manager) ? dep : UNIT_WANTS, |
eef85c4a | 3817 | device, true, mask); |
faa368e3 | 3818 | if (r < 0) |
6e2ef85b LP |
3819 | return r; |
3820 | ||
faa368e3 | 3821 | if (wants) { |
eef85c4a | 3822 | r = unit_add_dependency(device, UNIT_WANTS, u, false, mask); |
faa368e3 | 3823 | if (r < 0) |
6e2ef85b | 3824 | return r; |
faa368e3 | 3825 | } |
6e2ef85b LP |
3826 | |
3827 | return 0; | |
3828 | } | |
a16e1123 | 3829 | |
be847e82 | 3830 | int unit_coldplug(Unit *u) { |
05a98afd LP |
3831 | int r = 0, q; |
3832 | char **i; | |
cca098b0 LP |
3833 | |
3834 | assert(u); | |
3835 | ||
f0831ed2 | 3836 | /* Make sure we don't enter a loop, when coldplugging recursively. */ |
f78f265f LP |
3837 | if (u->coldplugged) |
3838 | return 0; | |
3839 | ||
3840 | u->coldplugged = true; | |
3841 | ||
05a98afd LP |
3842 | STRV_FOREACH(i, u->deserialized_refs) { |
3843 | q = bus_unit_track_add_name(u, *i); | |
3844 | if (q < 0 && r >= 0) | |
3845 | r = q; | |
3846 | } | |
3847 | u->deserialized_refs = strv_free(u->deserialized_refs); | |
cca098b0 | 3848 | |
05a98afd LP |
3849 | if (UNIT_VTABLE(u)->coldplug) { |
3850 | q = UNIT_VTABLE(u)->coldplug(u); | |
3851 | if (q < 0 && r >= 0) | |
3852 | r = q; | |
3853 | } | |
5a6158b6 | 3854 | |
05a98afd LP |
3855 | if (u->job) { |
3856 | q = job_coldplug(u->job); | |
3857 | if (q < 0 && r >= 0) | |
3858 | r = q; | |
3859 | } | |
cca098b0 | 3860 | |
05a98afd | 3861 | return r; |
cca098b0 LP |
3862 | } |
3863 | ||
f0831ed2 LP |
3864 | void unit_catchup(Unit *u) { |
3865 | assert(u); | |
3866 | ||
3867 | if (UNIT_VTABLE(u)->catchup) | |
3868 | UNIT_VTABLE(u)->catchup(u); | |
3869 | } | |
3870 | ||
ba25d39e | 3871 | static bool fragment_mtime_newer(const char *path, usec_t mtime, bool path_masked) { |
21b95806 ZJS |
3872 | struct stat st; |
3873 | ||
3874 | if (!path) | |
3875 | return false; | |
3876 | ||
77969722 LP |
3877 | /* If the source is some virtual kernel file system, then we assume we watch it anyway, and hence pretend we |
3878 | * are never out-of-date. */ | |
3879 | if (PATH_STARTSWITH_SET(path, "/proc", "/sys")) | |
3880 | return false; | |
3881 | ||
21b95806 ZJS |
3882 | if (stat(path, &st) < 0) |
3883 | /* What, cannot access this anymore? */ | |
3884 | return true; | |
3885 | ||
ba25d39e ZJS |
3886 | if (path_masked) |
3887 | /* For masked files check if they are still so */ | |
3888 | return !null_or_empty(&st); | |
3889 | else | |
3a8db9fe | 3890 | /* For non-empty files check the mtime */ |
87ec20ef | 3891 | return timespec_load(&st.st_mtim) > mtime; |
21b95806 ZJS |
3892 | |
3893 | return false; | |
3894 | } | |
3895 | ||
45fb0699 | 3896 | bool unit_need_daemon_reload(Unit *u) { |
ae7a7182 OS |
3897 | _cleanup_strv_free_ char **t = NULL; |
3898 | char **path; | |
1b64d026 | 3899 | |
45fb0699 LP |
3900 | assert(u); |
3901 | ||
ba25d39e ZJS |
3902 | /* For unit files, we allow masking… */ |
3903 | if (fragment_mtime_newer(u->fragment_path, u->fragment_mtime, | |
3904 | u->load_state == UNIT_MASKED)) | |
21b95806 | 3905 | return true; |
5f4b19f4 | 3906 | |
ba25d39e ZJS |
3907 | /* Source paths should not be masked… */ |
3908 | if (fragment_mtime_newer(u->source_path, u->source_mtime, false)) | |
ab932a62 | 3909 | return true; |
ae7a7182 | 3910 | |
19a44dfe LR |
3911 | if (u->load_state == UNIT_LOADED) |
3912 | (void) unit_find_dropin_paths(u, &t); | |
ab932a62 LP |
3913 | if (!strv_equal(u->dropin_paths, t)) |
3914 | return true; | |
6d10d308 | 3915 | |
ba25d39e | 3916 | /* … any drop-ins that are masked are simply omitted from the list. */ |
ab932a62 | 3917 | STRV_FOREACH(path, u->dropin_paths) |
ba25d39e | 3918 | if (fragment_mtime_newer(*path, u->dropin_mtime, false)) |
ab932a62 | 3919 | return true; |
21b95806 | 3920 | |
ab932a62 | 3921 | return false; |
45fb0699 LP |
3922 | } |
3923 | ||
fdf20a31 | 3924 | void unit_reset_failed(Unit *u) { |
5632e374 LP |
3925 | assert(u); |
3926 | ||
fdf20a31 MM |
3927 | if (UNIT_VTABLE(u)->reset_failed) |
3928 | UNIT_VTABLE(u)->reset_failed(u); | |
6bf0f408 LP |
3929 | |
3930 | RATELIMIT_RESET(u->start_limit); | |
3931 | u->start_limit_hit = false; | |
5632e374 LP |
3932 | } |
3933 | ||
a7f241db LP |
3934 | Unit *unit_following(Unit *u) { |
3935 | assert(u); | |
3936 | ||
3937 | if (UNIT_VTABLE(u)->following) | |
3938 | return UNIT_VTABLE(u)->following(u); | |
3939 | ||
3940 | return NULL; | |
3941 | } | |
3942 | ||
31afa0a4 | 3943 | bool unit_stop_pending(Unit *u) { |
18ffdfda LP |
3944 | assert(u); |
3945 | ||
31afa0a4 LP |
3946 | /* This call does check the current state of the unit. It's |
3947 | * hence useful to be called from state change calls of the | |
3948 | * unit itself, where the state isn't updated yet. This is | |
3949 | * different from unit_inactive_or_pending() which checks both | |
3950 | * the current state and for a queued job. */ | |
18ffdfda | 3951 | |
31afa0a4 LP |
3952 | return u->job && u->job->type == JOB_STOP; |
3953 | } | |
3954 | ||
3955 | bool unit_inactive_or_pending(Unit *u) { | |
3956 | assert(u); | |
3957 | ||
3958 | /* Returns true if the unit is inactive or going down */ | |
18ffdfda | 3959 | |
d956ac29 LP |
3960 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))) |
3961 | return true; | |
3962 | ||
31afa0a4 | 3963 | if (unit_stop_pending(u)) |
18ffdfda LP |
3964 | return true; |
3965 | ||
3966 | return false; | |
3967 | } | |
3968 | ||
31afa0a4 | 3969 | bool unit_active_or_pending(Unit *u) { |
f976f3f6 LP |
3970 | assert(u); |
3971 | ||
f60c2665 | 3972 | /* Returns true if the unit is active or going up */ |
f976f3f6 LP |
3973 | |
3974 | if (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) | |
3975 | return true; | |
3976 | ||
ac155bb8 | 3977 | if (u->job && |
3742095b | 3978 | IN_SET(u->job->type, JOB_START, JOB_RELOAD_OR_START, JOB_RESTART)) |
f976f3f6 LP |
3979 | return true; |
3980 | ||
3981 | return false; | |
3982 | } | |
3983 | ||
deb4e708 MK |
3984 | bool unit_will_restart(Unit *u) { |
3985 | assert(u); | |
3986 | ||
3987 | if (!UNIT_VTABLE(u)->will_restart) | |
3988 | return false; | |
3989 | ||
3990 | return UNIT_VTABLE(u)->will_restart(u); | |
3991 | } | |
3992 | ||
718db961 | 3993 | int unit_kill(Unit *u, KillWho w, int signo, sd_bus_error *error) { |
8a0867d6 LP |
3994 | assert(u); |
3995 | assert(w >= 0 && w < _KILL_WHO_MAX); | |
6eb7c172 | 3996 | assert(SIGNAL_VALID(signo)); |
8a0867d6 | 3997 | |
8a0867d6 | 3998 | if (!UNIT_VTABLE(u)->kill) |
15411c0c | 3999 | return -EOPNOTSUPP; |
8a0867d6 | 4000 | |
c74f17d9 | 4001 | return UNIT_VTABLE(u)->kill(u, w, signo, error); |
8a0867d6 LP |
4002 | } |
4003 | ||
82659fd7 | 4004 | static Set *unit_pid_set(pid_t main_pid, pid_t control_pid) { |
af4fa99d | 4005 | _cleanup_set_free_ Set *pid_set = NULL; |
82659fd7 LP |
4006 | int r; |
4007 | ||
d5099efc | 4008 | pid_set = set_new(NULL); |
82659fd7 LP |
4009 | if (!pid_set) |
4010 | return NULL; | |
4011 | ||
4012 | /* Exclude the main/control pids from being killed via the cgroup */ | |
4013 | if (main_pid > 0) { | |
fea72cc0 | 4014 | r = set_put(pid_set, PID_TO_PTR(main_pid)); |
82659fd7 | 4015 | if (r < 0) |
95f14a3e | 4016 | return NULL; |
82659fd7 LP |
4017 | } |
4018 | ||
4019 | if (control_pid > 0) { | |
fea72cc0 | 4020 | r = set_put(pid_set, PID_TO_PTR(control_pid)); |
82659fd7 | 4021 | if (r < 0) |
95f14a3e | 4022 | return NULL; |
82659fd7 LP |
4023 | } |
4024 | ||
95f14a3e | 4025 | return TAKE_PTR(pid_set); |
82659fd7 LP |
4026 | } |
4027 | ||
d91c34f2 LP |
4028 | int unit_kill_common( |
4029 | Unit *u, | |
4030 | KillWho who, | |
4031 | int signo, | |
4032 | pid_t main_pid, | |
4033 | pid_t control_pid, | |
718db961 | 4034 | sd_bus_error *error) { |
d91c34f2 | 4035 | |
814cc562 | 4036 | int r = 0; |
ac5e3a50 | 4037 | bool killed = false; |
814cc562 | 4038 | |
ac5e3a50 | 4039 | if (IN_SET(who, KILL_MAIN, KILL_MAIN_FAIL)) { |
814cc562 | 4040 | if (main_pid < 0) |
7358dc02 | 4041 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no main processes", unit_type_to_string(u->type)); |
52f448c3 | 4042 | else if (main_pid == 0) |
7358dc02 | 4043 | return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill"); |
814cc562 MS |
4044 | } |
4045 | ||
ac5e3a50 | 4046 | if (IN_SET(who, KILL_CONTROL, KILL_CONTROL_FAIL)) { |
814cc562 | 4047 | if (control_pid < 0) |
7358dc02 | 4048 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no control processes", unit_type_to_string(u->type)); |
52f448c3 | 4049 | else if (control_pid == 0) |
7358dc02 | 4050 | return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill"); |
814cc562 MS |
4051 | } |
4052 | ||
ac5e3a50 JS |
4053 | if (IN_SET(who, KILL_CONTROL, KILL_CONTROL_FAIL, KILL_ALL, KILL_ALL_FAIL)) |
4054 | if (control_pid > 0) { | |
814cc562 MS |
4055 | if (kill(control_pid, signo) < 0) |
4056 | r = -errno; | |
ac5e3a50 JS |
4057 | else |
4058 | killed = true; | |
4059 | } | |
814cc562 | 4060 | |
ac5e3a50 JS |
4061 | if (IN_SET(who, KILL_MAIN, KILL_MAIN_FAIL, KILL_ALL, KILL_ALL_FAIL)) |
4062 | if (main_pid > 0) { | |
814cc562 MS |
4063 | if (kill(main_pid, signo) < 0) |
4064 | r = -errno; | |
ac5e3a50 JS |
4065 | else |
4066 | killed = true; | |
4067 | } | |
814cc562 | 4068 | |
ac5e3a50 | 4069 | if (IN_SET(who, KILL_ALL, KILL_ALL_FAIL) && u->cgroup_path) { |
814cc562 MS |
4070 | _cleanup_set_free_ Set *pid_set = NULL; |
4071 | int q; | |
4072 | ||
82659fd7 LP |
4073 | /* Exclude the main/control pids from being killed via the cgroup */ |
4074 | pid_set = unit_pid_set(main_pid, control_pid); | |
814cc562 MS |
4075 | if (!pid_set) |
4076 | return -ENOMEM; | |
4077 | ||
1d98fef1 | 4078 | q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, signo, 0, pid_set, NULL, NULL); |
4c701096 | 4079 | if (q < 0 && !IN_SET(q, -EAGAIN, -ESRCH, -ENOENT)) |
814cc562 | 4080 | r = q; |
ac5e3a50 JS |
4081 | else |
4082 | killed = true; | |
814cc562 MS |
4083 | } |
4084 | ||
201f0c91 | 4085 | if (r == 0 && !killed && IN_SET(who, KILL_ALL_FAIL, KILL_CONTROL_FAIL)) |
ac5e3a50 JS |
4086 | return -ESRCH; |
4087 | ||
814cc562 MS |
4088 | return r; |
4089 | } | |
4090 | ||
6210e7fc LP |
4091 | int unit_following_set(Unit *u, Set **s) { |
4092 | assert(u); | |
4093 | assert(s); | |
4094 | ||
4095 | if (UNIT_VTABLE(u)->following_set) | |
4096 | return UNIT_VTABLE(u)->following_set(u, s); | |
4097 | ||
4098 | *s = NULL; | |
4099 | return 0; | |
4100 | } | |
4101 | ||
a4375746 | 4102 | UnitFileState unit_get_unit_file_state(Unit *u) { |
0ec0deaa LP |
4103 | int r; |
4104 | ||
a4375746 LP |
4105 | assert(u); |
4106 | ||
0ec0deaa LP |
4107 | if (u->unit_file_state < 0 && u->fragment_path) { |
4108 | r = unit_file_get_state( | |
463d0d15 | 4109 | u->manager->unit_file_scope, |
0ec0deaa | 4110 | NULL, |
9ea3a0e7 | 4111 | u->id, |
0ec0deaa LP |
4112 | &u->unit_file_state); |
4113 | if (r < 0) | |
4114 | u->unit_file_state = UNIT_FILE_BAD; | |
4115 | } | |
a4375746 | 4116 | |
ac155bb8 | 4117 | return u->unit_file_state; |
a4375746 LP |
4118 | } |
4119 | ||
d2dc52db LP |
4120 | int unit_get_unit_file_preset(Unit *u) { |
4121 | assert(u); | |
4122 | ||
4123 | if (u->unit_file_preset < 0 && u->fragment_path) | |
4124 | u->unit_file_preset = unit_file_query_preset( | |
463d0d15 | 4125 | u->manager->unit_file_scope, |
0ec0deaa LP |
4126 | NULL, |
4127 | basename(u->fragment_path)); | |
d2dc52db LP |
4128 | |
4129 | return u->unit_file_preset; | |
4130 | } | |
4131 | ||
7f7d01ed | 4132 | Unit* unit_ref_set(UnitRef *ref, Unit *source, Unit *target) { |
57020a3a | 4133 | assert(ref); |
7f7d01ed ZJS |
4134 | assert(source); |
4135 | assert(target); | |
57020a3a | 4136 | |
7f7d01ed | 4137 | if (ref->target) |
57020a3a LP |
4138 | unit_ref_unset(ref); |
4139 | ||
7f7d01ed ZJS |
4140 | ref->source = source; |
4141 | ref->target = target; | |
4142 | LIST_PREPEND(refs_by_target, target->refs_by_target, ref); | |
4143 | return target; | |
57020a3a LP |
4144 | } |
4145 | ||
4146 | void unit_ref_unset(UnitRef *ref) { | |
4147 | assert(ref); | |
4148 | ||
7f7d01ed | 4149 | if (!ref->target) |
57020a3a LP |
4150 | return; |
4151 | ||
b75102e5 LP |
4152 | /* We are about to drop a reference to the unit, make sure the garbage collection has a look at it as it might |
4153 | * be unreferenced now. */ | |
7f7d01ed | 4154 | unit_add_to_gc_queue(ref->target); |
b75102e5 | 4155 | |
7f7d01ed ZJS |
4156 | LIST_REMOVE(refs_by_target, ref->target->refs_by_target, ref); |
4157 | ref->source = ref->target = NULL; | |
57020a3a LP |
4158 | } |
4159 | ||
29206d46 LP |
4160 | static int user_from_unit_name(Unit *u, char **ret) { |
4161 | ||
4162 | static const uint8_t hash_key[] = { | |
4163 | 0x58, 0x1a, 0xaf, 0xe6, 0x28, 0x58, 0x4e, 0x96, | |
4164 | 0xb4, 0x4e, 0xf5, 0x3b, 0x8c, 0x92, 0x07, 0xec | |
4165 | }; | |
4166 | ||
4167 | _cleanup_free_ char *n = NULL; | |
4168 | int r; | |
4169 | ||
4170 | r = unit_name_to_prefix(u->id, &n); | |
4171 | if (r < 0) | |
4172 | return r; | |
4173 | ||
4174 | if (valid_user_group_name(n)) { | |
ae2a15bc | 4175 | *ret = TAKE_PTR(n); |
29206d46 LP |
4176 | return 0; |
4177 | } | |
4178 | ||
4179 | /* If we can't use the unit name as a user name, then let's hash it and use that */ | |
4180 | if (asprintf(ret, "_du%016" PRIx64, siphash24(n, strlen(n), hash_key)) < 0) | |
4181 | return -ENOMEM; | |
4182 | ||
4183 | return 0; | |
4184 | } | |
4185 | ||
598459ce LP |
4186 | int unit_patch_contexts(Unit *u) { |
4187 | CGroupContext *cc; | |
4188 | ExecContext *ec; | |
cba6e062 LP |
4189 | unsigned i; |
4190 | int r; | |
4191 | ||
e06c73cc | 4192 | assert(u); |
e06c73cc | 4193 | |
598459ce LP |
4194 | /* Patch in the manager defaults into the exec and cgroup |
4195 | * contexts, _after_ the rest of the settings have been | |
4196 | * initialized */ | |
085afe36 | 4197 | |
598459ce LP |
4198 | ec = unit_get_exec_context(u); |
4199 | if (ec) { | |
4200 | /* This only copies in the ones that need memory */ | |
4201 | for (i = 0; i < _RLIMIT_MAX; i++) | |
4202 | if (u->manager->rlimit[i] && !ec->rlimit[i]) { | |
4203 | ec->rlimit[i] = newdup(struct rlimit, u->manager->rlimit[i], 1); | |
4204 | if (!ec->rlimit[i]) | |
4205 | return -ENOMEM; | |
4206 | } | |
4207 | ||
463d0d15 | 4208 | if (MANAGER_IS_USER(u->manager) && |
598459ce LP |
4209 | !ec->working_directory) { |
4210 | ||
4211 | r = get_home_dir(&ec->working_directory); | |
4212 | if (r < 0) | |
4213 | return r; | |
4c08c824 LP |
4214 | |
4215 | /* Allow user services to run, even if the | |
4216 | * home directory is missing */ | |
4217 | ec->working_directory_missing_ok = true; | |
cba6e062 LP |
4218 | } |
4219 | ||
598459ce | 4220 | if (ec->private_devices) |
2cd0a735 | 4221 | ec->capability_bounding_set &= ~((UINT64_C(1) << CAP_MKNOD) | (UINT64_C(1) << CAP_SYS_RAWIO)); |
502d704e DH |
4222 | |
4223 | if (ec->protect_kernel_modules) | |
4224 | ec->capability_bounding_set &= ~(UINT64_C(1) << CAP_SYS_MODULE); | |
29206d46 LP |
4225 | |
4226 | if (ec->dynamic_user) { | |
4227 | if (!ec->user) { | |
4228 | r = user_from_unit_name(u, &ec->user); | |
4229 | if (r < 0) | |
4230 | return r; | |
4231 | } | |
4232 | ||
4233 | if (!ec->group) { | |
4234 | ec->group = strdup(ec->user); | |
4235 | if (!ec->group) | |
4236 | return -ENOMEM; | |
4237 | } | |
4238 | ||
bf65b7e0 LP |
4239 | /* If the dynamic user option is on, let's make sure that the unit can't leave its |
4240 | * UID/GID around in the file system or on IPC objects. Hence enforce a strict | |
4241 | * sandbox. */ | |
63bb64a0 | 4242 | |
29206d46 | 4243 | ec->private_tmp = true; |
00d9ef85 | 4244 | ec->remove_ipc = true; |
63bb64a0 LP |
4245 | ec->protect_system = PROTECT_SYSTEM_STRICT; |
4246 | if (ec->protect_home == PROTECT_HOME_NO) | |
4247 | ec->protect_home = PROTECT_HOME_READ_ONLY; | |
bf65b7e0 LP |
4248 | |
4249 | /* Make sure this service can neither benefit from SUID/SGID binaries nor create | |
4250 | * them. */ | |
4251 | ec->no_new_privileges = true; | |
4252 | ec->restrict_suid_sgid = true; | |
29206d46 | 4253 | } |
cba6e062 LP |
4254 | } |
4255 | ||
598459ce | 4256 | cc = unit_get_cgroup_context(u); |
fe65e88b | 4257 | if (cc && ec) { |
f513e420 | 4258 | |
fe65e88b | 4259 | if (ec->private_devices && |
598459ce LP |
4260 | cc->device_policy == CGROUP_AUTO) |
4261 | cc->device_policy = CGROUP_CLOSED; | |
fe65e88b YW |
4262 | |
4263 | if (ec->root_image && | |
4264 | (cc->device_policy != CGROUP_AUTO || cc->device_allow)) { | |
4265 | ||
4266 | /* When RootImage= is specified, the following devices are touched. */ | |
4267 | r = cgroup_add_device_allow(cc, "/dev/loop-control", "rw"); | |
4268 | if (r < 0) | |
4269 | return r; | |
4270 | ||
4271 | r = cgroup_add_device_allow(cc, "block-loop", "rwm"); | |
4272 | if (r < 0) | |
4273 | return r; | |
4274 | ||
4275 | r = cgroup_add_device_allow(cc, "block-blkext", "rwm"); | |
4276 | if (r < 0) | |
4277 | return r; | |
4278 | } | |
598459ce | 4279 | } |
f1660f96 | 4280 | |
cba6e062 | 4281 | return 0; |
e06c73cc LP |
4282 | } |
4283 | ||
3ef63c31 LP |
4284 | ExecContext *unit_get_exec_context(Unit *u) { |
4285 | size_t offset; | |
4286 | assert(u); | |
4287 | ||
598459ce LP |
4288 | if (u->type < 0) |
4289 | return NULL; | |
4290 | ||
3ef63c31 LP |
4291 | offset = UNIT_VTABLE(u)->exec_context_offset; |
4292 | if (offset <= 0) | |
4293 | return NULL; | |
4294 | ||
4295 | return (ExecContext*) ((uint8_t*) u + offset); | |
4296 | } | |
4297 | ||
718db961 LP |
4298 | KillContext *unit_get_kill_context(Unit *u) { |
4299 | size_t offset; | |
4300 | assert(u); | |
4301 | ||
598459ce LP |
4302 | if (u->type < 0) |
4303 | return NULL; | |
4304 | ||
718db961 LP |
4305 | offset = UNIT_VTABLE(u)->kill_context_offset; |
4306 | if (offset <= 0) | |
4307 | return NULL; | |
4308 | ||
4309 | return (KillContext*) ((uint8_t*) u + offset); | |
4310 | } | |
4311 | ||
4ad49000 LP |
4312 | CGroupContext *unit_get_cgroup_context(Unit *u) { |
4313 | size_t offset; | |
4314 | ||
598459ce LP |
4315 | if (u->type < 0) |
4316 | return NULL; | |
4317 | ||
4ad49000 LP |
4318 | offset = UNIT_VTABLE(u)->cgroup_context_offset; |
4319 | if (offset <= 0) | |
4320 | return NULL; | |
4321 | ||
4322 | return (CGroupContext*) ((uint8_t*) u + offset); | |
4323 | } | |
4324 | ||
613b411c LP |
4325 | ExecRuntime *unit_get_exec_runtime(Unit *u) { |
4326 | size_t offset; | |
4327 | ||
598459ce LP |
4328 | if (u->type < 0) |
4329 | return NULL; | |
4330 | ||
613b411c LP |
4331 | offset = UNIT_VTABLE(u)->exec_runtime_offset; |
4332 | if (offset <= 0) | |
4333 | return NULL; | |
4334 | ||
4335 | return *(ExecRuntime**) ((uint8_t*) u + offset); | |
4336 | } | |
4337 | ||
2e59b241 | 4338 | static const char* unit_drop_in_dir(Unit *u, UnitWriteFlags flags) { |
3f5e8115 LP |
4339 | assert(u); |
4340 | ||
2e59b241 | 4341 | if (UNIT_WRITE_FLAGS_NOOP(flags)) |
4f4afc88 LP |
4342 | return NULL; |
4343 | ||
39591351 LP |
4344 | if (u->transient) /* Redirect drop-ins for transient units always into the transient directory. */ |
4345 | return u->manager->lookup_paths.transient; | |
26d04f86 | 4346 | |
2e59b241 | 4347 | if (flags & UNIT_PERSISTENT) |
4f4afc88 | 4348 | return u->manager->lookup_paths.persistent_control; |
26d04f86 | 4349 | |
2e59b241 LP |
4350 | if (flags & UNIT_RUNTIME) |
4351 | return u->manager->lookup_paths.runtime_control; | |
4352 | ||
39591351 | 4353 | return NULL; |
71645aca LP |
4354 | } |
4355 | ||
2e59b241 LP |
4356 | char* unit_escape_setting(const char *s, UnitWriteFlags flags, char **buf) { |
4357 | char *ret = NULL; | |
4358 | ||
4359 | if (!s) | |
4360 | return NULL; | |
4361 | ||
4362 | /* Escapes the input string as requested. Returns the escaped string. If 'buf' is specified then the allocated | |
4363 | * return buffer pointer is also written to *buf, except if no escaping was necessary, in which case *buf is | |
4364 | * set to NULL, and the input pointer is returned as-is. This means the return value always contains a properly | |
4365 | * escaped version, but *buf when passed only contains a pointer if an allocation was necessary. If *buf is | |
4366 | * not specified, then the return value always needs to be freed. Callers can use this to optimize memory | |
4367 | * allocations. */ | |
4368 | ||
4369 | if (flags & UNIT_ESCAPE_SPECIFIERS) { | |
4370 | ret = specifier_escape(s); | |
4371 | if (!ret) | |
4372 | return NULL; | |
4373 | ||
4374 | s = ret; | |
4375 | } | |
4376 | ||
4377 | if (flags & UNIT_ESCAPE_C) { | |
4378 | char *a; | |
4379 | ||
4380 | a = cescape(s); | |
4381 | free(ret); | |
4382 | if (!a) | |
4383 | return NULL; | |
4384 | ||
4385 | ret = a; | |
4386 | } | |
4387 | ||
4388 | if (buf) { | |
4389 | *buf = ret; | |
4390 | return ret ?: (char*) s; | |
4391 | } | |
4392 | ||
4393 | return ret ?: strdup(s); | |
4394 | } | |
4395 | ||
4396 | char* unit_concat_strv(char **l, UnitWriteFlags flags) { | |
4397 | _cleanup_free_ char *result = NULL; | |
4398 | size_t n = 0, allocated = 0; | |
ae2a15bc | 4399 | char **i; |
2e59b241 LP |
4400 | |
4401 | /* Takes a list of strings, escapes them, and concatenates them. This may be used to format command lines in a | |
4402 | * way suitable for ExecStart= stanzas */ | |
4403 | ||
4404 | STRV_FOREACH(i, l) { | |
4405 | _cleanup_free_ char *buf = NULL; | |
4406 | const char *p; | |
4407 | size_t a; | |
4408 | char *q; | |
4409 | ||
4410 | p = unit_escape_setting(*i, flags, &buf); | |
4411 | if (!p) | |
4412 | return NULL; | |
4413 | ||
4414 | a = (n > 0) + 1 + strlen(p) + 1; /* separating space + " + entry + " */ | |
4415 | if (!GREEDY_REALLOC(result, allocated, n + a + 1)) | |
4416 | return NULL; | |
4417 | ||
4418 | q = result + n; | |
4419 | if (n > 0) | |
4420 | *(q++) = ' '; | |
4421 | ||
4422 | *(q++) = '"'; | |
4423 | q = stpcpy(q, p); | |
4424 | *(q++) = '"'; | |
4425 | ||
4426 | n += a; | |
4427 | } | |
4428 | ||
4429 | if (!GREEDY_REALLOC(result, allocated, n + 1)) | |
4430 | return NULL; | |
4431 | ||
4432 | result[n] = 0; | |
4433 | ||
ae2a15bc | 4434 | return TAKE_PTR(result); |
2e59b241 LP |
4435 | } |
4436 | ||
4437 | int unit_write_setting(Unit *u, UnitWriteFlags flags, const char *name, const char *data) { | |
4438 | _cleanup_free_ char *p = NULL, *q = NULL, *escaped = NULL; | |
2a9a6f8a | 4439 | const char *dir, *wrapped; |
26d04f86 | 4440 | int r; |
71645aca LP |
4441 | |
4442 | assert(u); | |
2e59b241 LP |
4443 | assert(name); |
4444 | assert(data); | |
4445 | ||
4446 | if (UNIT_WRITE_FLAGS_NOOP(flags)) | |
4447 | return 0; | |
4448 | ||
4449 | data = unit_escape_setting(data, flags, &escaped); | |
4450 | if (!data) | |
4451 | return -ENOMEM; | |
4452 | ||
4453 | /* Prefix the section header. If we are writing this out as transient file, then let's suppress this if the | |
4454 | * previous section header is the same */ | |
4455 | ||
4456 | if (flags & UNIT_PRIVATE) { | |
4457 | if (!UNIT_VTABLE(u)->private_section) | |
4458 | return -EINVAL; | |
4459 | ||
4460 | if (!u->transient_file || u->last_section_private < 0) | |
4461 | data = strjoina("[", UNIT_VTABLE(u)->private_section, "]\n", data); | |
4462 | else if (u->last_section_private == 0) | |
4463 | data = strjoina("\n[", UNIT_VTABLE(u)->private_section, "]\n", data); | |
4464 | } else { | |
4465 | if (!u->transient_file || u->last_section_private < 0) | |
4466 | data = strjoina("[Unit]\n", data); | |
4467 | else if (u->last_section_private > 0) | |
4468 | data = strjoina("\n[Unit]\n", data); | |
4469 | } | |
71645aca | 4470 | |
4f4afc88 LP |
4471 | if (u->transient_file) { |
4472 | /* When this is a transient unit file in creation, then let's not create a new drop-in but instead | |
4473 | * write to the transient unit file. */ | |
4474 | fputs(data, u->transient_file); | |
4f4afc88 | 4475 | |
2e59b241 LP |
4476 | if (!endswith(data, "\n")) |
4477 | fputc('\n', u->transient_file); | |
4478 | ||
4479 | /* Remember which section we wrote this entry to */ | |
4480 | u->last_section_private = !!(flags & UNIT_PRIVATE); | |
8e2af478 | 4481 | return 0; |
2e59b241 | 4482 | } |
8e2af478 | 4483 | |
2e59b241 | 4484 | dir = unit_drop_in_dir(u, flags); |
39591351 LP |
4485 | if (!dir) |
4486 | return -EINVAL; | |
71645aca | 4487 | |
2a9a6f8a | 4488 | wrapped = strjoina("# This is a drop-in unit file extension, created via \"systemctl set-property\"\n" |
3f71dec5 | 4489 | "# or an equivalent operation. Do not edit.\n", |
2a9a6f8a ZJS |
4490 | data, |
4491 | "\n"); | |
e20b2a86 | 4492 | |
815b09d3 | 4493 | r = drop_in_file(dir, u->id, 50, name, &p, &q); |
adb76a70 WC |
4494 | if (r < 0) |
4495 | return r; | |
4496 | ||
45639f1b | 4497 | (void) mkdir_p_label(p, 0755); |
2a9a6f8a | 4498 | r = write_string_file_atomic_label(q, wrapped); |
adb76a70 WC |
4499 | if (r < 0) |
4500 | return r; | |
4501 | ||
815b09d3 | 4502 | r = strv_push(&u->dropin_paths, q); |
adb76a70 WC |
4503 | if (r < 0) |
4504 | return r; | |
815b09d3 | 4505 | q = NULL; |
adb76a70 | 4506 | |
adb76a70 WC |
4507 | strv_uniq(u->dropin_paths); |
4508 | ||
4509 | u->dropin_mtime = now(CLOCK_REALTIME); | |
4510 | ||
4511 | return 0; | |
26d04f86 | 4512 | } |
71645aca | 4513 | |
2e59b241 | 4514 | int unit_write_settingf(Unit *u, UnitWriteFlags flags, const char *name, const char *format, ...) { |
b9ec9359 LP |
4515 | _cleanup_free_ char *p = NULL; |
4516 | va_list ap; | |
4517 | int r; | |
4518 | ||
4519 | assert(u); | |
4520 | assert(name); | |
4521 | assert(format); | |
4522 | ||
2e59b241 | 4523 | if (UNIT_WRITE_FLAGS_NOOP(flags)) |
b9ec9359 LP |
4524 | return 0; |
4525 | ||
4526 | va_start(ap, format); | |
4527 | r = vasprintf(&p, format, ap); | |
4528 | va_end(ap); | |
4529 | ||
4530 | if (r < 0) | |
4531 | return -ENOMEM; | |
4532 | ||
2e59b241 | 4533 | return unit_write_setting(u, flags, name, p); |
b9ec9359 | 4534 | } |
71645aca | 4535 | |
c2756a68 | 4536 | int unit_make_transient(Unit *u) { |
0126c8f3 | 4537 | _cleanup_free_ char *path = NULL; |
4f4afc88 | 4538 | FILE *f; |
4f4afc88 | 4539 | |
c2756a68 LP |
4540 | assert(u); |
4541 | ||
3f5e8115 LP |
4542 | if (!UNIT_VTABLE(u)->can_transient) |
4543 | return -EOPNOTSUPP; | |
4544 | ||
45639f1b LP |
4545 | (void) mkdir_p_label(u->manager->lookup_paths.transient, 0755); |
4546 | ||
657ee2d8 | 4547 | path = path_join(u->manager->lookup_paths.transient, u->id); |
4f4afc88 LP |
4548 | if (!path) |
4549 | return -ENOMEM; | |
4550 | ||
4551 | /* Let's open the file we'll write the transient settings into. This file is kept open as long as we are | |
4552 | * creating the transient, and is closed in unit_load(), as soon as we start loading the file. */ | |
4553 | ||
78e334b5 | 4554 | RUN_WITH_UMASK(0022) { |
4f4afc88 | 4555 | f = fopen(path, "we"); |
0126c8f3 | 4556 | if (!f) |
78e334b5 | 4557 | return -errno; |
4f4afc88 LP |
4558 | } |
4559 | ||
0126c8f3 | 4560 | safe_fclose(u->transient_file); |
4f4afc88 LP |
4561 | u->transient_file = f; |
4562 | ||
0126c8f3 | 4563 | free_and_replace(u->fragment_path, path); |
7c65093a | 4564 | |
7c65093a LP |
4565 | u->source_path = mfree(u->source_path); |
4566 | u->dropin_paths = strv_free(u->dropin_paths); | |
4567 | u->fragment_mtime = u->source_mtime = u->dropin_mtime = 0; | |
4568 | ||
4f4afc88 LP |
4569 | u->load_state = UNIT_STUB; |
4570 | u->load_error = 0; | |
4571 | u->transient = true; | |
4572 | ||
7c65093a LP |
4573 | unit_add_to_dbus_queue(u); |
4574 | unit_add_to_gc_queue(u); | |
c2756a68 | 4575 | |
4f4afc88 LP |
4576 | fputs("# This is a transient unit file, created programmatically via the systemd API. Do not edit.\n", |
4577 | u->transient_file); | |
4578 | ||
3f5e8115 | 4579 | return 0; |
c2756a68 LP |
4580 | } |
4581 | ||
c53d2d54 | 4582 | static int log_kill(pid_t pid, int sig, void *userdata) { |
1d98fef1 LP |
4583 | _cleanup_free_ char *comm = NULL; |
4584 | ||
4585 | (void) get_process_comm(pid, &comm); | |
4586 | ||
4587 | /* Don't log about processes marked with brackets, under the assumption that these are temporary processes | |
4588 | only, like for example systemd's own PAM stub process. */ | |
4589 | if (comm && comm[0] == '(') | |
c53d2d54 | 4590 | return 0; |
1d98fef1 LP |
4591 | |
4592 | log_unit_notice(userdata, | |
4593 | "Killing process " PID_FMT " (%s) with signal SIG%s.", | |
4594 | pid, | |
4595 | strna(comm), | |
4596 | signal_to_string(sig)); | |
c53d2d54 DB |
4597 | |
4598 | return 1; | |
1d98fef1 LP |
4599 | } |
4600 | ||
4601 | static int operation_to_signal(KillContext *c, KillOperation k) { | |
4602 | assert(c); | |
4603 | ||
4604 | switch (k) { | |
4605 | ||
4606 | case KILL_TERMINATE: | |
4607 | case KILL_TERMINATE_AND_LOG: | |
4608 | return c->kill_signal; | |
4609 | ||
4610 | case KILL_KILL: | |
fbb48d4c | 4611 | return c->final_kill_signal; |
1d98fef1 | 4612 | |
c87700a1 AZ |
4613 | case KILL_WATCHDOG: |
4614 | return c->watchdog_signal; | |
1d98fef1 LP |
4615 | |
4616 | default: | |
4617 | assert_not_reached("KillOperation unknown"); | |
4618 | } | |
4619 | } | |
4620 | ||
cd2086fe LP |
4621 | int unit_kill_context( |
4622 | Unit *u, | |
4623 | KillContext *c, | |
db2cb23b | 4624 | KillOperation k, |
cd2086fe LP |
4625 | pid_t main_pid, |
4626 | pid_t control_pid, | |
4627 | bool main_pid_alien) { | |
4628 | ||
1d98fef1 | 4629 | bool wait_for_exit = false, send_sighup; |
59ec09a8 | 4630 | cg_kill_log_func_t log_func = NULL; |
b821a397 | 4631 | int sig, r; |
cd2086fe LP |
4632 | |
4633 | assert(u); | |
4634 | assert(c); | |
4635 | ||
59ec09a8 ZJS |
4636 | /* Kill the processes belonging to this unit, in preparation for shutting the unit down. |
4637 | * Returns > 0 if we killed something worth waiting for, 0 otherwise. */ | |
1d98fef1 | 4638 | |
cd2086fe LP |
4639 | if (c->kill_mode == KILL_NONE) |
4640 | return 0; | |
4641 | ||
1d98fef1 LP |
4642 | sig = operation_to_signal(c, k); |
4643 | ||
4644 | send_sighup = | |
4645 | c->send_sighup && | |
4646 | IN_SET(k, KILL_TERMINATE, KILL_TERMINATE_AND_LOG) && | |
4647 | sig != SIGHUP; | |
4648 | ||
59ec09a8 ZJS |
4649 | if (k != KILL_TERMINATE || IN_SET(sig, SIGKILL, SIGABRT)) |
4650 | log_func = log_kill; | |
cd2086fe LP |
4651 | |
4652 | if (main_pid > 0) { | |
1d98fef1 LP |
4653 | if (log_func) |
4654 | log_func(main_pid, sig, u); | |
cd2086fe | 4655 | |
1d98fef1 | 4656 | r = kill_and_sigcont(main_pid, sig); |
cd2086fe LP |
4657 | if (r < 0 && r != -ESRCH) { |
4658 | _cleanup_free_ char *comm = NULL; | |
1d98fef1 | 4659 | (void) get_process_comm(main_pid, &comm); |
cd2086fe | 4660 | |
b821a397 | 4661 | log_unit_warning_errno(u, r, "Failed to kill main process " PID_FMT " (%s), ignoring: %m", main_pid, strna(comm)); |
82659fd7 | 4662 | } else { |
bc6aed7b LP |
4663 | if (!main_pid_alien) |
4664 | wait_for_exit = true; | |
82659fd7 | 4665 | |
1d98fef1 | 4666 | if (r != -ESRCH && send_sighup) |
d0667321 | 4667 | (void) kill(main_pid, SIGHUP); |
82659fd7 | 4668 | } |
cd2086fe LP |
4669 | } |
4670 | ||
4671 | if (control_pid > 0) { | |
1d98fef1 LP |
4672 | if (log_func) |
4673 | log_func(control_pid, sig, u); | |
cd2086fe | 4674 | |
1d98fef1 | 4675 | r = kill_and_sigcont(control_pid, sig); |
cd2086fe LP |
4676 | if (r < 0 && r != -ESRCH) { |
4677 | _cleanup_free_ char *comm = NULL; | |
1d98fef1 | 4678 | (void) get_process_comm(control_pid, &comm); |
cd2086fe | 4679 | |
b821a397 | 4680 | log_unit_warning_errno(u, r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", control_pid, strna(comm)); |
82659fd7 | 4681 | } else { |
cd2086fe | 4682 | wait_for_exit = true; |
82659fd7 | 4683 | |
1d98fef1 | 4684 | if (r != -ESRCH && send_sighup) |
d0667321 | 4685 | (void) kill(control_pid, SIGHUP); |
82659fd7 | 4686 | } |
cd2086fe LP |
4687 | } |
4688 | ||
b821a397 LP |
4689 | if (u->cgroup_path && |
4690 | (c->kill_mode == KILL_CONTROL_GROUP || (c->kill_mode == KILL_MIXED && k == KILL_KILL))) { | |
cd2086fe LP |
4691 | _cleanup_set_free_ Set *pid_set = NULL; |
4692 | ||
82659fd7 LP |
4693 | /* Exclude the main/control pids from being killed via the cgroup */ |
4694 | pid_set = unit_pid_set(main_pid, control_pid); | |
cd2086fe LP |
4695 | if (!pid_set) |
4696 | return -ENOMEM; | |
4697 | ||
1d98fef1 LP |
4698 | r = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, |
4699 | sig, | |
4700 | CGROUP_SIGCONT|CGROUP_IGNORE_SELF, | |
4701 | pid_set, | |
4702 | log_func, u); | |
cd2086fe | 4703 | if (r < 0) { |
4c701096 | 4704 | if (!IN_SET(r, -EAGAIN, -ESRCH, -ENOENT)) |
b821a397 LP |
4705 | log_unit_warning_errno(u, r, "Failed to kill control group %s, ignoring: %m", u->cgroup_path); |
4706 | ||
82659fd7 | 4707 | } else if (r > 0) { |
bc6aed7b | 4708 | |
1d9cc876 LP |
4709 | /* FIXME: For now, on the legacy hierarchy, we will not wait for the cgroup members to die if |
4710 | * we are running in a container or if this is a delegation unit, simply because cgroup | |
4711 | * notification is unreliable in these cases. It doesn't work at all in containers, and outside | |
4712 | * of containers it can be confused easily by left-over directories in the cgroup — which | |
4713 | * however should not exist in non-delegated units. On the unified hierarchy that's different, | |
4714 | * there we get proper events. Hence rely on them. */ | |
efdb0237 | 4715 | |
c22800e4 | 4716 | if (cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) > 0 || |
1d9cc876 | 4717 | (detect_container() == 0 && !unit_cgroup_delegate(u))) |
e9db43d5 | 4718 | wait_for_exit = true; |
58ea275a | 4719 | |
1d98fef1 | 4720 | if (send_sighup) { |
82659fd7 LP |
4721 | set_free(pid_set); |
4722 | ||
4723 | pid_set = unit_pid_set(main_pid, control_pid); | |
4724 | if (!pid_set) | |
4725 | return -ENOMEM; | |
4726 | ||
1d98fef1 LP |
4727 | cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, |
4728 | SIGHUP, | |
4729 | CGROUP_IGNORE_SELF, | |
4730 | pid_set, | |
4731 | NULL, NULL); | |
82659fd7 LP |
4732 | } |
4733 | } | |
cd2086fe LP |
4734 | } |
4735 | ||
4736 | return wait_for_exit; | |
4737 | } | |
4738 | ||
eef85c4a | 4739 | int unit_require_mounts_for(Unit *u, const char *path, UnitDependencyMask mask) { |
ca8700e9 | 4740 | _cleanup_free_ char *p = NULL; |
eef85c4a | 4741 | UnitDependencyInfo di; |
a57f7e2c LP |
4742 | int r; |
4743 | ||
4744 | assert(u); | |
4745 | assert(path); | |
4746 | ||
eef85c4a LP |
4747 | /* Registers a unit for requiring a certain path and all its prefixes. We keep a hashtable of these paths in |
4748 | * the unit (from the path to the UnitDependencyInfo structure indicating how to the dependency came to | |
4749 | * be). However, we build a prefix table for all possible prefixes so that new appearing mount units can easily | |
4750 | * determine which units to make themselves a dependency of. */ | |
a57f7e2c | 4751 | |
70b64bd3 ZJS |
4752 | if (!path_is_absolute(path)) |
4753 | return -EINVAL; | |
4754 | ||
548f6937 | 4755 | r = hashmap_ensure_allocated(&u->requires_mounts_for, &path_hash_ops); |
eef85c4a LP |
4756 | if (r < 0) |
4757 | return r; | |
4758 | ||
a57f7e2c LP |
4759 | p = strdup(path); |
4760 | if (!p) | |
4761 | return -ENOMEM; | |
4762 | ||
106bf8e4 | 4763 | path = path_simplify(p, true); |
a57f7e2c | 4764 | |
ca8700e9 | 4765 | if (!path_is_normalized(path)) |
a57f7e2c | 4766 | return -EPERM; |
a57f7e2c | 4767 | |
ca8700e9 | 4768 | if (hashmap_contains(u->requires_mounts_for, path)) |
a57f7e2c | 4769 | return 0; |
a57f7e2c | 4770 | |
eef85c4a LP |
4771 | di = (UnitDependencyInfo) { |
4772 | .origin_mask = mask | |
4773 | }; | |
4774 | ||
ca8700e9 ZJS |
4775 | r = hashmap_put(u->requires_mounts_for, path, di.data); |
4776 | if (r < 0) | |
a57f7e2c | 4777 | return r; |
ca8700e9 | 4778 | p = NULL; |
a57f7e2c | 4779 | |
4cb06c59 | 4780 | char prefix[strlen(path) + 1]; |
ca8700e9 | 4781 | PATH_FOREACH_PREFIX_MORE(prefix, path) { |
a57f7e2c LP |
4782 | Set *x; |
4783 | ||
4784 | x = hashmap_get(u->manager->units_requiring_mounts_for, prefix); | |
4785 | if (!x) { | |
ca8700e9 | 4786 | _cleanup_free_ char *q = NULL; |
a57f7e2c | 4787 | |
548f6937 | 4788 | r = hashmap_ensure_allocated(&u->manager->units_requiring_mounts_for, &path_hash_ops); |
742f41ad LP |
4789 | if (r < 0) |
4790 | return r; | |
a57f7e2c LP |
4791 | |
4792 | q = strdup(prefix); | |
4793 | if (!q) | |
4794 | return -ENOMEM; | |
4795 | ||
d5099efc | 4796 | x = set_new(NULL); |
ca8700e9 | 4797 | if (!x) |
a57f7e2c | 4798 | return -ENOMEM; |
a57f7e2c LP |
4799 | |
4800 | r = hashmap_put(u->manager->units_requiring_mounts_for, q, x); | |
4801 | if (r < 0) { | |
a57f7e2c LP |
4802 | set_free(x); |
4803 | return r; | |
4804 | } | |
ca8700e9 | 4805 | q = NULL; |
a57f7e2c LP |
4806 | } |
4807 | ||
4808 | r = set_put(x, u); | |
4809 | if (r < 0) | |
4810 | return r; | |
4811 | } | |
4812 | ||
4813 | return 0; | |
4814 | } | |
4815 | ||
613b411c LP |
4816 | int unit_setup_exec_runtime(Unit *u) { |
4817 | ExecRuntime **rt; | |
4818 | size_t offset; | |
613b411c | 4819 | Unit *other; |
eef85c4a LP |
4820 | Iterator i; |
4821 | void *v; | |
e8a565cb | 4822 | int r; |
613b411c LP |
4823 | |
4824 | offset = UNIT_VTABLE(u)->exec_runtime_offset; | |
4825 | assert(offset > 0); | |
4826 | ||
06b643e7 | 4827 | /* Check if there already is an ExecRuntime for this unit? */ |
613b411c LP |
4828 | rt = (ExecRuntime**) ((uint8_t*) u + offset); |
4829 | if (*rt) | |
4830 | return 0; | |
4831 | ||
4832 | /* Try to get it from somebody else */ | |
eef85c4a | 4833 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_JOINS_NAMESPACE_OF], i) { |
e8a565cb YW |
4834 | r = exec_runtime_acquire(u->manager, NULL, other->id, false, rt); |
4835 | if (r == 1) | |
4836 | return 1; | |
613b411c LP |
4837 | } |
4838 | ||
e8a565cb | 4839 | return exec_runtime_acquire(u->manager, unit_get_exec_context(u), u->id, true, rt); |
613b411c LP |
4840 | } |
4841 | ||
29206d46 LP |
4842 | int unit_setup_dynamic_creds(Unit *u) { |
4843 | ExecContext *ec; | |
4844 | DynamicCreds *dcreds; | |
4845 | size_t offset; | |
4846 | ||
4847 | assert(u); | |
4848 | ||
4849 | offset = UNIT_VTABLE(u)->dynamic_creds_offset; | |
4850 | assert(offset > 0); | |
4851 | dcreds = (DynamicCreds*) ((uint8_t*) u + offset); | |
4852 | ||
4853 | ec = unit_get_exec_context(u); | |
4854 | assert(ec); | |
4855 | ||
4856 | if (!ec->dynamic_user) | |
4857 | return 0; | |
4858 | ||
4859 | return dynamic_creds_acquire(dcreds, u->manager, ec->user, ec->group); | |
4860 | } | |
4861 | ||
1c2e9646 LP |
4862 | bool unit_type_supported(UnitType t) { |
4863 | if (_unlikely_(t < 0)) | |
4864 | return false; | |
4865 | if (_unlikely_(t >= _UNIT_TYPE_MAX)) | |
4866 | return false; | |
4867 | ||
4868 | if (!unit_vtable[t]->supported) | |
4869 | return true; | |
4870 | ||
4871 | return unit_vtable[t]->supported(); | |
4872 | } | |
4873 | ||
8b4305c7 LP |
4874 | void unit_warn_if_dir_nonempty(Unit *u, const char* where) { |
4875 | int r; | |
4876 | ||
4877 | assert(u); | |
4878 | assert(where); | |
4879 | ||
4880 | r = dir_is_empty(where); | |
3f602115 | 4881 | if (r > 0 || r == -ENOTDIR) |
8b4305c7 LP |
4882 | return; |
4883 | if (r < 0) { | |
4884 | log_unit_warning_errno(u, r, "Failed to check directory %s: %m", where); | |
4885 | return; | |
4886 | } | |
4887 | ||
4888 | log_struct(LOG_NOTICE, | |
2b044526 | 4889 | "MESSAGE_ID=" SD_MESSAGE_OVERMOUNTING_STR, |
8b4305c7 | 4890 | LOG_UNIT_ID(u), |
f1c50bec | 4891 | LOG_UNIT_INVOCATION_ID(u), |
8b4305c7 | 4892 | LOG_UNIT_MESSAGE(u, "Directory %s to mount over is not empty, mounting anyway.", where), |
a1230ff9 | 4893 | "WHERE=%s", where); |
8b4305c7 LP |
4894 | } |
4895 | ||
25cd4964 | 4896 | int unit_fail_if_noncanonical(Unit *u, const char* where) { |
58d9d89b | 4897 | _cleanup_free_ char *canonical_where = NULL; |
8b4305c7 LP |
4898 | int r; |
4899 | ||
4900 | assert(u); | |
4901 | assert(where); | |
4902 | ||
25cd4964 | 4903 | r = chase_symlinks(where, NULL, CHASE_NONEXISTENT, &canonical_where); |
8b4305c7 | 4904 | if (r < 0) { |
25cd4964 | 4905 | log_unit_debug_errno(u, r, "Failed to check %s for symlinks, ignoring: %m", where); |
8b4305c7 LP |
4906 | return 0; |
4907 | } | |
25cd4964 AJ |
4908 | |
4909 | /* We will happily ignore a trailing slash (or any redundant slashes) */ | |
4910 | if (path_equal(where, canonical_where)) | |
8b4305c7 LP |
4911 | return 0; |
4912 | ||
25cd4964 | 4913 | /* No need to mention "." or "..", they would already have been rejected by unit_name_from_path() */ |
8b4305c7 | 4914 | log_struct(LOG_ERR, |
2b044526 | 4915 | "MESSAGE_ID=" SD_MESSAGE_OVERMOUNTING_STR, |
8b4305c7 | 4916 | LOG_UNIT_ID(u), |
f1c50bec | 4917 | LOG_UNIT_INVOCATION_ID(u), |
25cd4964 | 4918 | LOG_UNIT_MESSAGE(u, "Mount path %s is not canonical (contains a symlink).", where), |
a1230ff9 | 4919 | "WHERE=%s", where); |
8b4305c7 LP |
4920 | |
4921 | return -ELOOP; | |
4922 | } | |
0f13f3bd LP |
4923 | |
4924 | bool unit_is_pristine(Unit *u) { | |
4925 | assert(u); | |
4926 | ||
7c65093a | 4927 | /* Check if the unit already exists or is already around, |
0f13f3bd LP |
4928 | * in a number of different ways. Note that to cater for unit |
4929 | * types such as slice, we are generally fine with units that | |
e9e8cbc8 ZJS |
4930 | * are marked UNIT_LOADED even though nothing was actually |
4931 | * loaded, as those unit types don't require a file on disk. */ | |
0f13f3bd LP |
4932 | |
4933 | return !(!IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_LOADED) || | |
4934 | u->fragment_path || | |
4935 | u->source_path || | |
4936 | !strv_isempty(u->dropin_paths) || | |
0f13f3bd LP |
4937 | u->job || |
4938 | u->merged_into); | |
4939 | } | |
291d565a LP |
4940 | |
4941 | pid_t unit_control_pid(Unit *u) { | |
4942 | assert(u); | |
4943 | ||
4944 | if (UNIT_VTABLE(u)->control_pid) | |
4945 | return UNIT_VTABLE(u)->control_pid(u); | |
4946 | ||
4947 | return 0; | |
4948 | } | |
4949 | ||
4950 | pid_t unit_main_pid(Unit *u) { | |
4951 | assert(u); | |
4952 | ||
4953 | if (UNIT_VTABLE(u)->main_pid) | |
4954 | return UNIT_VTABLE(u)->main_pid(u); | |
4955 | ||
4956 | return 0; | |
4957 | } | |
00d9ef85 LP |
4958 | |
4959 | static void unit_unref_uid_internal( | |
4960 | Unit *u, | |
4961 | uid_t *ref_uid, | |
4962 | bool destroy_now, | |
4963 | void (*_manager_unref_uid)(Manager *m, uid_t uid, bool destroy_now)) { | |
4964 | ||
4965 | assert(u); | |
4966 | assert(ref_uid); | |
4967 | assert(_manager_unref_uid); | |
4968 | ||
4969 | /* Generic implementation of both unit_unref_uid() and unit_unref_gid(), under the assumption that uid_t and | |
4970 | * gid_t are actually the same time, with the same validity rules. | |
4971 | * | |
4972 | * Drops a reference to UID/GID from a unit. */ | |
4973 | ||
4974 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
4975 | assert_cc(UID_INVALID == (uid_t) GID_INVALID); | |
4976 | ||
4977 | if (!uid_is_valid(*ref_uid)) | |
4978 | return; | |
4979 | ||
4980 | _manager_unref_uid(u->manager, *ref_uid, destroy_now); | |
4981 | *ref_uid = UID_INVALID; | |
4982 | } | |
4983 | ||
4984 | void unit_unref_uid(Unit *u, bool destroy_now) { | |
4985 | unit_unref_uid_internal(u, &u->ref_uid, destroy_now, manager_unref_uid); | |
4986 | } | |
4987 | ||
4988 | void unit_unref_gid(Unit *u, bool destroy_now) { | |
4989 | unit_unref_uid_internal(u, (uid_t*) &u->ref_gid, destroy_now, manager_unref_gid); | |
4990 | } | |
4991 | ||
4992 | static int unit_ref_uid_internal( | |
4993 | Unit *u, | |
4994 | uid_t *ref_uid, | |
4995 | uid_t uid, | |
4996 | bool clean_ipc, | |
4997 | int (*_manager_ref_uid)(Manager *m, uid_t uid, bool clean_ipc)) { | |
4998 | ||
4999 | int r; | |
5000 | ||
5001 | assert(u); | |
5002 | assert(ref_uid); | |
5003 | assert(uid_is_valid(uid)); | |
5004 | assert(_manager_ref_uid); | |
5005 | ||
5006 | /* Generic implementation of both unit_ref_uid() and unit_ref_guid(), under the assumption that uid_t and gid_t | |
5007 | * are actually the same type, and have the same validity rules. | |
5008 | * | |
5009 | * Adds a reference on a specific UID/GID to this unit. Each unit referencing the same UID/GID maintains a | |
5010 | * reference so that we can destroy the UID/GID's IPC resources as soon as this is requested and the counter | |
5011 | * drops to zero. */ | |
5012 | ||
5013 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
5014 | assert_cc(UID_INVALID == (uid_t) GID_INVALID); | |
5015 | ||
5016 | if (*ref_uid == uid) | |
5017 | return 0; | |
5018 | ||
5019 | if (uid_is_valid(*ref_uid)) /* Already set? */ | |
5020 | return -EBUSY; | |
5021 | ||
5022 | r = _manager_ref_uid(u->manager, uid, clean_ipc); | |
5023 | if (r < 0) | |
5024 | return r; | |
5025 | ||
5026 | *ref_uid = uid; | |
5027 | return 1; | |
5028 | } | |
5029 | ||
5030 | int unit_ref_uid(Unit *u, uid_t uid, bool clean_ipc) { | |
5031 | return unit_ref_uid_internal(u, &u->ref_uid, uid, clean_ipc, manager_ref_uid); | |
5032 | } | |
5033 | ||
5034 | int unit_ref_gid(Unit *u, gid_t gid, bool clean_ipc) { | |
5035 | return unit_ref_uid_internal(u, (uid_t*) &u->ref_gid, (uid_t) gid, clean_ipc, manager_ref_gid); | |
5036 | } | |
5037 | ||
5038 | static int unit_ref_uid_gid_internal(Unit *u, uid_t uid, gid_t gid, bool clean_ipc) { | |
5039 | int r = 0, q = 0; | |
5040 | ||
5041 | assert(u); | |
5042 | ||
5043 | /* Reference both a UID and a GID in one go. Either references both, or neither. */ | |
5044 | ||
5045 | if (uid_is_valid(uid)) { | |
5046 | r = unit_ref_uid(u, uid, clean_ipc); | |
5047 | if (r < 0) | |
5048 | return r; | |
5049 | } | |
5050 | ||
5051 | if (gid_is_valid(gid)) { | |
5052 | q = unit_ref_gid(u, gid, clean_ipc); | |
5053 | if (q < 0) { | |
5054 | if (r > 0) | |
5055 | unit_unref_uid(u, false); | |
5056 | ||
5057 | return q; | |
5058 | } | |
5059 | } | |
5060 | ||
5061 | return r > 0 || q > 0; | |
5062 | } | |
5063 | ||
5064 | int unit_ref_uid_gid(Unit *u, uid_t uid, gid_t gid) { | |
5065 | ExecContext *c; | |
5066 | int r; | |
5067 | ||
5068 | assert(u); | |
5069 | ||
5070 | c = unit_get_exec_context(u); | |
5071 | ||
5072 | r = unit_ref_uid_gid_internal(u, uid, gid, c ? c->remove_ipc : false); | |
5073 | if (r < 0) | |
5074 | return log_unit_warning_errno(u, r, "Couldn't add UID/GID reference to unit, proceeding without: %m"); | |
5075 | ||
5076 | return r; | |
5077 | } | |
5078 | ||
5079 | void unit_unref_uid_gid(Unit *u, bool destroy_now) { | |
5080 | assert(u); | |
5081 | ||
5082 | unit_unref_uid(u, destroy_now); | |
5083 | unit_unref_gid(u, destroy_now); | |
5084 | } | |
5085 | ||
5086 | void unit_notify_user_lookup(Unit *u, uid_t uid, gid_t gid) { | |
5087 | int r; | |
5088 | ||
5089 | assert(u); | |
5090 | ||
5091 | /* This is invoked whenever one of the forked off processes let's us know the UID/GID its user name/group names | |
5092 | * resolved to. We keep track of which UID/GID is currently assigned in order to be able to destroy its IPC | |
5093 | * objects when no service references the UID/GID anymore. */ | |
5094 | ||
5095 | r = unit_ref_uid_gid(u, uid, gid); | |
5096 | if (r > 0) | |
37d0b962 | 5097 | unit_add_to_dbus_queue(u); |
00d9ef85 | 5098 | } |
4b58153d LP |
5099 | |
5100 | int unit_set_invocation_id(Unit *u, sd_id128_t id) { | |
5101 | int r; | |
5102 | ||
5103 | assert(u); | |
5104 | ||
5105 | /* Set the invocation ID for this unit. If we cannot, this will not roll back, but reset the whole thing. */ | |
5106 | ||
5107 | if (sd_id128_equal(u->invocation_id, id)) | |
5108 | return 0; | |
5109 | ||
5110 | if (!sd_id128_is_null(u->invocation_id)) | |
5111 | (void) hashmap_remove_value(u->manager->units_by_invocation_id, &u->invocation_id, u); | |
5112 | ||
5113 | if (sd_id128_is_null(id)) { | |
5114 | r = 0; | |
5115 | goto reset; | |
5116 | } | |
5117 | ||
5118 | r = hashmap_ensure_allocated(&u->manager->units_by_invocation_id, &id128_hash_ops); | |
5119 | if (r < 0) | |
5120 | goto reset; | |
5121 | ||
5122 | u->invocation_id = id; | |
5123 | sd_id128_to_string(id, u->invocation_id_string); | |
5124 | ||
5125 | r = hashmap_put(u->manager->units_by_invocation_id, &u->invocation_id, u); | |
5126 | if (r < 0) | |
5127 | goto reset; | |
5128 | ||
5129 | return 0; | |
5130 | ||
5131 | reset: | |
5132 | u->invocation_id = SD_ID128_NULL; | |
5133 | u->invocation_id_string[0] = 0; | |
5134 | return r; | |
5135 | } | |
5136 | ||
5137 | int unit_acquire_invocation_id(Unit *u) { | |
5138 | sd_id128_t id; | |
5139 | int r; | |
5140 | ||
5141 | assert(u); | |
5142 | ||
5143 | r = sd_id128_randomize(&id); | |
5144 | if (r < 0) | |
5145 | return log_unit_error_errno(u, r, "Failed to generate invocation ID for unit: %m"); | |
5146 | ||
5147 | r = unit_set_invocation_id(u, id); | |
5148 | if (r < 0) | |
5149 | return log_unit_error_errno(u, r, "Failed to set invocation ID for unit: %m"); | |
5150 | ||
af92c603 | 5151 | unit_add_to_dbus_queue(u); |
4b58153d LP |
5152 | return 0; |
5153 | } | |
f0d47797 | 5154 | |
1ad6e8b3 LP |
5155 | int unit_set_exec_params(Unit *u, ExecParameters *p) { |
5156 | int r; | |
5157 | ||
7960b0c7 LP |
5158 | assert(u); |
5159 | assert(p); | |
f0d47797 | 5160 | |
004c7f16 | 5161 | /* Copy parameters from manager */ |
1ad6e8b3 LP |
5162 | r = manager_get_effective_environment(u->manager, &p->environment); |
5163 | if (r < 0) | |
5164 | return r; | |
5165 | ||
004c7f16 LP |
5166 | p->confirm_spawn = manager_get_confirm_spawn(u->manager); |
5167 | p->cgroup_supported = u->manager->cgroup_supported; | |
5168 | p->prefix = u->manager->prefix; | |
5169 | SET_FLAG(p->flags, EXEC_PASS_LOG_UNIT|EXEC_CHOWN_DIRECTORIES, MANAGER_IS_SYSTEM(u->manager)); | |
5170 | ||
5238e957 | 5171 | /* Copy parameters from unit */ |
7960b0c7 | 5172 | p->cgroup_path = u->cgroup_path; |
1d9cc876 | 5173 | SET_FLAG(p->flags, EXEC_CGROUP_DELEGATE, unit_cgroup_delegate(u)); |
1ad6e8b3 LP |
5174 | |
5175 | return 0; | |
f0d47797 | 5176 | } |
a79279c7 | 5177 | |
4c253ed1 | 5178 | int unit_fork_helper_process(Unit *u, const char *name, pid_t *ret) { |
a79279c7 LP |
5179 | int r; |
5180 | ||
5181 | assert(u); | |
5182 | assert(ret); | |
5183 | ||
5184 | /* Forks off a helper process and makes sure it is a member of the unit's cgroup. Returns == 0 in the child, | |
5185 | * and > 0 in the parent. The pid parameter is always filled in with the child's PID. */ | |
5186 | ||
5187 | (void) unit_realize_cgroup(u); | |
5188 | ||
4c253ed1 LP |
5189 | r = safe_fork(name, FORK_REOPEN_LOG, ret); |
5190 | if (r != 0) | |
5191 | return r; | |
a79279c7 | 5192 | |
4c253ed1 LP |
5193 | (void) default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1); |
5194 | (void) ignore_signals(SIGPIPE, -1); | |
a79279c7 | 5195 | |
4c253ed1 | 5196 | (void) prctl(PR_SET_PDEATHSIG, SIGTERM); |
a79279c7 | 5197 | |
4c253ed1 LP |
5198 | if (u->cgroup_path) { |
5199 | r = cg_attach_everywhere(u->manager->cgroup_supported, u->cgroup_path, 0, NULL, NULL); | |
5200 | if (r < 0) { | |
5201 | log_unit_error_errno(u, r, "Failed to join unit cgroup %s: %m", u->cgroup_path); | |
5202 | _exit(EXIT_CGROUP); | |
a79279c7 | 5203 | } |
a79279c7 LP |
5204 | } |
5205 | ||
4c253ed1 | 5206 | return 0; |
a79279c7 | 5207 | } |
c999cf38 LP |
5208 | |
5209 | static void unit_update_dependency_mask(Unit *u, UnitDependency d, Unit *other, UnitDependencyInfo di) { | |
5210 | assert(u); | |
5211 | assert(d >= 0); | |
5212 | assert(d < _UNIT_DEPENDENCY_MAX); | |
5213 | assert(other); | |
5214 | ||
5215 | if (di.origin_mask == 0 && di.destination_mask == 0) { | |
5216 | /* No bit set anymore, let's drop the whole entry */ | |
5217 | assert_se(hashmap_remove(u->dependencies[d], other)); | |
5218 | log_unit_debug(u, "%s lost dependency %s=%s", u->id, unit_dependency_to_string(d), other->id); | |
5219 | } else | |
5220 | /* Mask was reduced, let's update the entry */ | |
5221 | assert_se(hashmap_update(u->dependencies[d], other, di.data) == 0); | |
5222 | } | |
5223 | ||
5224 | void unit_remove_dependencies(Unit *u, UnitDependencyMask mask) { | |
5225 | UnitDependency d; | |
5226 | ||
5227 | assert(u); | |
5228 | ||
5229 | /* Removes all dependencies u has on other units marked for ownership by 'mask'. */ | |
5230 | ||
5231 | if (mask == 0) | |
5232 | return; | |
5233 | ||
5234 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { | |
5235 | bool done; | |
5236 | ||
5237 | do { | |
5238 | UnitDependencyInfo di; | |
5239 | Unit *other; | |
5240 | Iterator i; | |
5241 | ||
5242 | done = true; | |
5243 | ||
5244 | HASHMAP_FOREACH_KEY(di.data, other, u->dependencies[d], i) { | |
5245 | UnitDependency q; | |
5246 | ||
5247 | if ((di.origin_mask & ~mask) == di.origin_mask) | |
5248 | continue; | |
5249 | di.origin_mask &= ~mask; | |
5250 | unit_update_dependency_mask(u, d, other, di); | |
5251 | ||
5252 | /* We updated the dependency from our unit to the other unit now. But most dependencies | |
5253 | * imply a reverse dependency. Hence, let's delete that one too. For that we go through | |
5254 | * all dependency types on the other unit and delete all those which point to us and | |
5255 | * have the right mask set. */ | |
5256 | ||
5257 | for (q = 0; q < _UNIT_DEPENDENCY_MAX; q++) { | |
5258 | UnitDependencyInfo dj; | |
5259 | ||
5260 | dj.data = hashmap_get(other->dependencies[q], u); | |
5261 | if ((dj.destination_mask & ~mask) == dj.destination_mask) | |
5262 | continue; | |
5263 | dj.destination_mask &= ~mask; | |
5264 | ||
5265 | unit_update_dependency_mask(other, q, u, dj); | |
5266 | } | |
5267 | ||
5268 | unit_add_to_gc_queue(other); | |
5269 | ||
5270 | done = false; | |
5271 | break; | |
5272 | } | |
5273 | ||
5274 | } while (!done); | |
5275 | } | |
5276 | } | |
d3070fbd LP |
5277 | |
5278 | static int unit_export_invocation_id(Unit *u) { | |
5279 | const char *p; | |
5280 | int r; | |
5281 | ||
5282 | assert(u); | |
5283 | ||
5284 | if (u->exported_invocation_id) | |
5285 | return 0; | |
5286 | ||
5287 | if (sd_id128_is_null(u->invocation_id)) | |
5288 | return 0; | |
5289 | ||
5290 | p = strjoina("/run/systemd/units/invocation:", u->id); | |
5291 | r = symlink_atomic(u->invocation_id_string, p); | |
5292 | if (r < 0) | |
5293 | return log_unit_debug_errno(u, r, "Failed to create invocation ID symlink %s: %m", p); | |
5294 | ||
5295 | u->exported_invocation_id = true; | |
5296 | return 0; | |
5297 | } | |
5298 | ||
5299 | static int unit_export_log_level_max(Unit *u, const ExecContext *c) { | |
5300 | const char *p; | |
5301 | char buf[2]; | |
5302 | int r; | |
5303 | ||
5304 | assert(u); | |
5305 | assert(c); | |
5306 | ||
5307 | if (u->exported_log_level_max) | |
5308 | return 0; | |
5309 | ||
5310 | if (c->log_level_max < 0) | |
5311 | return 0; | |
5312 | ||
5313 | assert(c->log_level_max <= 7); | |
5314 | ||
5315 | buf[0] = '0' + c->log_level_max; | |
5316 | buf[1] = 0; | |
5317 | ||
5318 | p = strjoina("/run/systemd/units/log-level-max:", u->id); | |
5319 | r = symlink_atomic(buf, p); | |
5320 | if (r < 0) | |
5321 | return log_unit_debug_errno(u, r, "Failed to create maximum log level symlink %s: %m", p); | |
5322 | ||
5323 | u->exported_log_level_max = true; | |
5324 | return 0; | |
5325 | } | |
5326 | ||
5327 | static int unit_export_log_extra_fields(Unit *u, const ExecContext *c) { | |
5328 | _cleanup_close_ int fd = -1; | |
5329 | struct iovec *iovec; | |
5330 | const char *p; | |
5331 | char *pattern; | |
5332 | le64_t *sizes; | |
5333 | ssize_t n; | |
5334 | size_t i; | |
5335 | int r; | |
5336 | ||
5337 | if (u->exported_log_extra_fields) | |
5338 | return 0; | |
5339 | ||
5340 | if (c->n_log_extra_fields <= 0) | |
5341 | return 0; | |
5342 | ||
5343 | sizes = newa(le64_t, c->n_log_extra_fields); | |
5344 | iovec = newa(struct iovec, c->n_log_extra_fields * 2); | |
5345 | ||
5346 | for (i = 0; i < c->n_log_extra_fields; i++) { | |
5347 | sizes[i] = htole64(c->log_extra_fields[i].iov_len); | |
5348 | ||
5349 | iovec[i*2] = IOVEC_MAKE(sizes + i, sizeof(le64_t)); | |
5350 | iovec[i*2+1] = c->log_extra_fields[i]; | |
5351 | } | |
5352 | ||
5353 | p = strjoina("/run/systemd/units/log-extra-fields:", u->id); | |
5354 | pattern = strjoina(p, ".XXXXXX"); | |
5355 | ||
5356 | fd = mkostemp_safe(pattern); | |
5357 | if (fd < 0) | |
5358 | return log_unit_debug_errno(u, fd, "Failed to create extra fields file %s: %m", p); | |
5359 | ||
5360 | n = writev(fd, iovec, c->n_log_extra_fields*2); | |
5361 | if (n < 0) { | |
5362 | r = log_unit_debug_errno(u, errno, "Failed to write extra fields: %m"); | |
5363 | goto fail; | |
5364 | } | |
5365 | ||
5366 | (void) fchmod(fd, 0644); | |
5367 | ||
5368 | if (rename(pattern, p) < 0) { | |
5369 | r = log_unit_debug_errno(u, errno, "Failed to rename extra fields file: %m"); | |
5370 | goto fail; | |
5371 | } | |
5372 | ||
5373 | u->exported_log_extra_fields = true; | |
5374 | return 0; | |
5375 | ||
5376 | fail: | |
5377 | (void) unlink(pattern); | |
5378 | return r; | |
5379 | } | |
5380 | ||
90fc172e AZ |
5381 | static int unit_export_log_rate_limit_interval(Unit *u, const ExecContext *c) { |
5382 | _cleanup_free_ char *buf = NULL; | |
5383 | const char *p; | |
5384 | int r; | |
5385 | ||
5386 | assert(u); | |
5387 | assert(c); | |
5388 | ||
5389 | if (u->exported_log_rate_limit_interval) | |
5390 | return 0; | |
5391 | ||
5392 | if (c->log_rate_limit_interval_usec == 0) | |
5393 | return 0; | |
5394 | ||
5395 | p = strjoina("/run/systemd/units/log-rate-limit-interval:", u->id); | |
5396 | ||
5397 | if (asprintf(&buf, "%" PRIu64, c->log_rate_limit_interval_usec) < 0) | |
5398 | return log_oom(); | |
5399 | ||
5400 | r = symlink_atomic(buf, p); | |
5401 | if (r < 0) | |
5402 | return log_unit_debug_errno(u, r, "Failed to create log rate limit interval symlink %s: %m", p); | |
5403 | ||
5404 | u->exported_log_rate_limit_interval = true; | |
5405 | return 0; | |
5406 | } | |
5407 | ||
5408 | static int unit_export_log_rate_limit_burst(Unit *u, const ExecContext *c) { | |
5409 | _cleanup_free_ char *buf = NULL; | |
5410 | const char *p; | |
5411 | int r; | |
5412 | ||
5413 | assert(u); | |
5414 | assert(c); | |
5415 | ||
5416 | if (u->exported_log_rate_limit_burst) | |
5417 | return 0; | |
5418 | ||
5419 | if (c->log_rate_limit_burst == 0) | |
5420 | return 0; | |
5421 | ||
5422 | p = strjoina("/run/systemd/units/log-rate-limit-burst:", u->id); | |
5423 | ||
5424 | if (asprintf(&buf, "%u", c->log_rate_limit_burst) < 0) | |
5425 | return log_oom(); | |
5426 | ||
5427 | r = symlink_atomic(buf, p); | |
5428 | if (r < 0) | |
5429 | return log_unit_debug_errno(u, r, "Failed to create log rate limit burst symlink %s: %m", p); | |
5430 | ||
5431 | u->exported_log_rate_limit_burst = true; | |
5432 | return 0; | |
5433 | } | |
5434 | ||
d3070fbd LP |
5435 | void unit_export_state_files(Unit *u) { |
5436 | const ExecContext *c; | |
5437 | ||
5438 | assert(u); | |
5439 | ||
5440 | if (!u->id) | |
5441 | return; | |
5442 | ||
5443 | if (!MANAGER_IS_SYSTEM(u->manager)) | |
5444 | return; | |
5445 | ||
638cece4 | 5446 | if (MANAGER_IS_TEST_RUN(u->manager)) |
8f632531 LP |
5447 | return; |
5448 | ||
d3070fbd LP |
5449 | /* Exports a couple of unit properties to /run/systemd/units/, so that journald can quickly query this data |
5450 | * from there. Ideally, journald would use IPC to query this, like everybody else, but that's hard, as long as | |
5451 | * the IPC system itself and PID 1 also log to the journal. | |
5452 | * | |
5453 | * Note that these files really shouldn't be considered API for anyone else, as use a runtime file system as | |
5454 | * IPC replacement is not compatible with today's world of file system namespaces. However, this doesn't really | |
5455 | * apply to communication between the journal and systemd, as we assume that these two daemons live in the same | |
5456 | * namespace at least. | |
5457 | * | |
5458 | * Note that some of the "files" exported here are actually symlinks and not regular files. Symlinks work | |
5459 | * better for storing small bits of data, in particular as we can write them with two system calls, and read | |
5460 | * them with one. */ | |
5461 | ||
5462 | (void) unit_export_invocation_id(u); | |
5463 | ||
5464 | c = unit_get_exec_context(u); | |
5465 | if (c) { | |
5466 | (void) unit_export_log_level_max(u, c); | |
5467 | (void) unit_export_log_extra_fields(u, c); | |
90fc172e AZ |
5468 | (void) unit_export_log_rate_limit_interval(u, c); |
5469 | (void) unit_export_log_rate_limit_burst(u, c); | |
d3070fbd LP |
5470 | } |
5471 | } | |
5472 | ||
5473 | void unit_unlink_state_files(Unit *u) { | |
5474 | const char *p; | |
5475 | ||
5476 | assert(u); | |
5477 | ||
5478 | if (!u->id) | |
5479 | return; | |
5480 | ||
5481 | if (!MANAGER_IS_SYSTEM(u->manager)) | |
5482 | return; | |
5483 | ||
5484 | /* Undoes the effect of unit_export_state() */ | |
5485 | ||
5486 | if (u->exported_invocation_id) { | |
5487 | p = strjoina("/run/systemd/units/invocation:", u->id); | |
5488 | (void) unlink(p); | |
5489 | ||
5490 | u->exported_invocation_id = false; | |
5491 | } | |
5492 | ||
5493 | if (u->exported_log_level_max) { | |
5494 | p = strjoina("/run/systemd/units/log-level-max:", u->id); | |
5495 | (void) unlink(p); | |
5496 | ||
5497 | u->exported_log_level_max = false; | |
5498 | } | |
5499 | ||
5500 | if (u->exported_log_extra_fields) { | |
5501 | p = strjoina("/run/systemd/units/extra-fields:", u->id); | |
5502 | (void) unlink(p); | |
5503 | ||
5504 | u->exported_log_extra_fields = false; | |
5505 | } | |
90fc172e AZ |
5506 | |
5507 | if (u->exported_log_rate_limit_interval) { | |
5508 | p = strjoina("/run/systemd/units/log-rate-limit-interval:", u->id); | |
5509 | (void) unlink(p); | |
5510 | ||
5511 | u->exported_log_rate_limit_interval = false; | |
5512 | } | |
5513 | ||
5514 | if (u->exported_log_rate_limit_burst) { | |
5515 | p = strjoina("/run/systemd/units/log-rate-limit-burst:", u->id); | |
5516 | (void) unlink(p); | |
5517 | ||
5518 | u->exported_log_rate_limit_burst = false; | |
5519 | } | |
d3070fbd | 5520 | } |
5afe510c | 5521 | |
3c7416b6 LP |
5522 | int unit_prepare_exec(Unit *u) { |
5523 | int r; | |
5524 | ||
5525 | assert(u); | |
5526 | ||
fab34748 KL |
5527 | /* Load any custom firewall BPF programs here once to test if they are existing and actually loadable. |
5528 | * Fail here early since later errors in the call chain unit_realize_cgroup to cgroup_context_apply are ignored. */ | |
5529 | r = bpf_firewall_load_custom(u); | |
5530 | if (r < 0) | |
5531 | return r; | |
5532 | ||
3c7416b6 LP |
5533 | /* Prepares everything so that we can fork of a process for this unit */ |
5534 | ||
5535 | (void) unit_realize_cgroup(u); | |
5536 | ||
5537 | if (u->reset_accounting) { | |
9b2559a1 | 5538 | (void) unit_reset_accounting(u); |
3c7416b6 LP |
5539 | u->reset_accounting = false; |
5540 | } | |
5541 | ||
5542 | unit_export_state_files(u); | |
5543 | ||
5544 | r = unit_setup_exec_runtime(u); | |
5545 | if (r < 0) | |
5546 | return r; | |
5547 | ||
5548 | r = unit_setup_dynamic_creds(u); | |
5549 | if (r < 0) | |
5550 | return r; | |
5551 | ||
5552 | return 0; | |
5553 | } | |
5554 | ||
c53d2d54 | 5555 | static int log_leftover(pid_t pid, int sig, void *userdata) { |
a4634b21 LP |
5556 | _cleanup_free_ char *comm = NULL; |
5557 | ||
5558 | (void) get_process_comm(pid, &comm); | |
5559 | ||
5560 | if (comm && comm[0] == '(') /* Most likely our own helper process (PAM?), ignore */ | |
c53d2d54 | 5561 | return 0; |
a4634b21 LP |
5562 | |
5563 | log_unit_warning(userdata, | |
5564 | "Found left-over process " PID_FMT " (%s) in control group while starting unit. Ignoring.\n" | |
5565 | "This usually indicates unclean termination of a previous run, or service implementation deficiencies.", | |
5566 | pid, strna(comm)); | |
c53d2d54 DB |
5567 | |
5568 | return 1; | |
a4634b21 LP |
5569 | } |
5570 | ||
c53d2d54 | 5571 | int unit_warn_leftover_processes(Unit *u) { |
a4634b21 LP |
5572 | assert(u); |
5573 | ||
5574 | (void) unit_pick_cgroup_path(u); | |
5575 | ||
5576 | if (!u->cgroup_path) | |
c53d2d54 | 5577 | return 0; |
a4634b21 | 5578 | |
c53d2d54 | 5579 | return cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, 0, 0, NULL, log_leftover, u); |
a4634b21 LP |
5580 | } |
5581 | ||
bb2c7685 LP |
5582 | bool unit_needs_console(Unit *u) { |
5583 | ExecContext *ec; | |
5584 | UnitActiveState state; | |
5585 | ||
5586 | assert(u); | |
5587 | ||
5588 | state = unit_active_state(u); | |
5589 | ||
5590 | if (UNIT_IS_INACTIVE_OR_FAILED(state)) | |
5591 | return false; | |
5592 | ||
5593 | if (UNIT_VTABLE(u)->needs_console) | |
5594 | return UNIT_VTABLE(u)->needs_console(u); | |
5595 | ||
5596 | /* If this unit type doesn't implement this call, let's use a generic fallback implementation: */ | |
5597 | ec = unit_get_exec_context(u); | |
5598 | if (!ec) | |
5599 | return false; | |
5600 | ||
5601 | return exec_context_may_touch_console(ec); | |
5602 | } | |
5603 | ||
81e9871e LP |
5604 | const char *unit_label_path(Unit *u) { |
5605 | const char *p; | |
5606 | ||
5607 | /* Returns the file system path to use for MAC access decisions, i.e. the file to read the SELinux label off | |
5608 | * when validating access checks. */ | |
5609 | ||
5610 | p = u->source_path ?: u->fragment_path; | |
5611 | if (!p) | |
5612 | return NULL; | |
5613 | ||
5614 | /* If a unit is masked, then don't read the SELinux label of /dev/null, as that really makes no sense */ | |
5615 | if (path_equal(p, "/dev/null")) | |
5616 | return NULL; | |
5617 | ||
5618 | return p; | |
5619 | } | |
5620 | ||
6592b975 LP |
5621 | int unit_pid_attachable(Unit *u, pid_t pid, sd_bus_error *error) { |
5622 | int r; | |
5623 | ||
5624 | assert(u); | |
5625 | ||
5626 | /* Checks whether the specified PID is generally good for attaching, i.e. a valid PID, not our manager itself, | |
5627 | * and not a kernel thread either */ | |
5628 | ||
5629 | /* First, a simple range check */ | |
5630 | if (!pid_is_valid(pid)) | |
5631 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process identifier " PID_FMT " is not valid.", pid); | |
5632 | ||
5633 | /* Some extra safety check */ | |
5634 | if (pid == 1 || pid == getpid_cached()) | |
3fe91079 | 5635 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process " PID_FMT " is a manager process, refusing.", pid); |
6592b975 LP |
5636 | |
5637 | /* Don't even begin to bother with kernel threads */ | |
5638 | r = is_kernel_thread(pid); | |
5639 | if (r == -ESRCH) | |
5640 | return sd_bus_error_setf(error, SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN, "Process with ID " PID_FMT " does not exist.", pid); | |
5641 | if (r < 0) | |
5642 | return sd_bus_error_set_errnof(error, r, "Failed to determine whether process " PID_FMT " is a kernel thread: %m", pid); | |
5643 | if (r > 0) | |
5644 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process " PID_FMT " is a kernel thread, refusing.", pid); | |
5645 | ||
5646 | return 0; | |
5647 | } | |
5648 | ||
523ee2d4 LP |
5649 | void unit_log_success(Unit *u) { |
5650 | assert(u); | |
5651 | ||
5652 | log_struct(LOG_INFO, | |
5653 | "MESSAGE_ID=" SD_MESSAGE_UNIT_SUCCESS_STR, | |
5654 | LOG_UNIT_ID(u), | |
5655 | LOG_UNIT_INVOCATION_ID(u), | |
5656 | LOG_UNIT_MESSAGE(u, "Succeeded.")); | |
5657 | } | |
5658 | ||
7c047d74 LP |
5659 | void unit_log_failure(Unit *u, const char *result) { |
5660 | assert(u); | |
5661 | assert(result); | |
5662 | ||
5663 | log_struct(LOG_WARNING, | |
5664 | "MESSAGE_ID=" SD_MESSAGE_UNIT_FAILURE_RESULT_STR, | |
5665 | LOG_UNIT_ID(u), | |
5666 | LOG_UNIT_INVOCATION_ID(u), | |
5667 | LOG_UNIT_MESSAGE(u, "Failed with result '%s'.", result), | |
5668 | "UNIT_RESULT=%s", result); | |
5669 | } | |
5670 | ||
31cd5f63 AZ |
5671 | void unit_log_skip(Unit *u, const char *result) { |
5672 | assert(u); | |
5673 | assert(result); | |
5674 | ||
5675 | log_struct(LOG_INFO, | |
5676 | "MESSAGE_ID=" SD_MESSAGE_UNIT_SKIPPED_STR, | |
5677 | LOG_UNIT_ID(u), | |
5678 | LOG_UNIT_INVOCATION_ID(u), | |
5679 | LOG_UNIT_MESSAGE(u, "Skipped due to '%s'.", result), | |
5680 | "UNIT_RESULT=%s", result); | |
5681 | } | |
5682 | ||
91bbd9b7 LP |
5683 | void unit_log_process_exit( |
5684 | Unit *u, | |
5685 | int level, | |
5686 | const char *kind, | |
5687 | const char *command, | |
5688 | int code, | |
5689 | int status) { | |
5690 | ||
5691 | assert(u); | |
5692 | assert(kind); | |
5693 | ||
5694 | if (code != CLD_EXITED) | |
5695 | level = LOG_WARNING; | |
5696 | ||
5697 | log_struct(level, | |
5698 | "MESSAGE_ID=" SD_MESSAGE_UNIT_PROCESS_EXIT_STR, | |
5699 | LOG_UNIT_MESSAGE(u, "%s exited, code=%s, status=%i/%s", | |
5700 | kind, | |
5701 | sigchld_code_to_string(code), status, | |
5702 | strna(code == CLD_EXITED | |
5703 | ? exit_status_to_string(status, EXIT_STATUS_FULL) | |
5704 | : signal_to_string(status))), | |
5705 | "EXIT_CODE=%s", sigchld_code_to_string(code), | |
5706 | "EXIT_STATUS=%i", status, | |
5707 | "COMMAND=%s", strna(command), | |
5708 | LOG_UNIT_ID(u), | |
5709 | LOG_UNIT_INVOCATION_ID(u)); | |
5710 | } | |
5711 | ||
7af67e9a LP |
5712 | int unit_exit_status(Unit *u) { |
5713 | assert(u); | |
5714 | ||
5715 | /* Returns the exit status to propagate for the most recent cycle of this unit. Returns a value in the range | |
5716 | * 0…255 if there's something to propagate. EOPNOTSUPP if the concept does not apply to this unit type, ENODATA | |
5717 | * if no data is currently known (for example because the unit hasn't deactivated yet) and EBADE if the main | |
5718 | * service process has exited abnormally (signal/coredump). */ | |
5719 | ||
5720 | if (!UNIT_VTABLE(u)->exit_status) | |
5721 | return -EOPNOTSUPP; | |
5722 | ||
5723 | return UNIT_VTABLE(u)->exit_status(u); | |
5724 | } | |
5725 | ||
5726 | int unit_failure_action_exit_status(Unit *u) { | |
5727 | int r; | |
5728 | ||
5729 | assert(u); | |
5730 | ||
5731 | /* Returns the exit status to propagate on failure, or an error if there's nothing to propagate */ | |
5732 | ||
5733 | if (u->failure_action_exit_status >= 0) | |
5734 | return u->failure_action_exit_status; | |
5735 | ||
5736 | r = unit_exit_status(u); | |
5737 | if (r == -EBADE) /* Exited, but not cleanly (i.e. by signal or such) */ | |
5738 | return 255; | |
5739 | ||
5740 | return r; | |
5741 | } | |
5742 | ||
5743 | int unit_success_action_exit_status(Unit *u) { | |
5744 | int r; | |
5745 | ||
5746 | assert(u); | |
5747 | ||
5748 | /* Returns the exit status to propagate on success, or an error if there's nothing to propagate */ | |
5749 | ||
5750 | if (u->success_action_exit_status >= 0) | |
5751 | return u->success_action_exit_status; | |
5752 | ||
5753 | r = unit_exit_status(u); | |
5754 | if (r == -EBADE) /* Exited, but not cleanly (i.e. by signal or such) */ | |
5755 | return 255; | |
5756 | ||
5757 | return r; | |
5758 | } | |
5759 | ||
a4191c9f LP |
5760 | int unit_test_trigger_loaded(Unit *u) { |
5761 | Unit *trigger; | |
5762 | ||
5763 | /* Tests whether the unit to trigger is loaded */ | |
5764 | ||
5765 | trigger = UNIT_TRIGGER(u); | |
5766 | if (!trigger) | |
e7b9f4d9 ZJS |
5767 | return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOENT), |
5768 | "Refusing to start, no unit to trigger."); | |
a4191c9f | 5769 | if (trigger->load_state != UNIT_LOADED) |
e7b9f4d9 ZJS |
5770 | return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOENT), |
5771 | "Refusing to start, unit %s to trigger not loaded.", trigger->id); | |
a4191c9f LP |
5772 | |
5773 | return 0; | |
5774 | } | |
5775 | ||
380dc8b0 LP |
5776 | int unit_clean(Unit *u, ExecCleanMask mask) { |
5777 | UnitActiveState state; | |
5778 | ||
5779 | assert(u); | |
5780 | ||
5781 | /* Special return values: | |
5782 | * | |
5783 | * -EOPNOTSUPP → cleaning not supported for this unit type | |
5784 | * -EUNATCH → cleaning not defined for this resource type | |
5785 | * -EBUSY → unit currently can't be cleaned since it's running or not properly loaded, or has | |
5786 | * a job queued or similar | |
5787 | */ | |
5788 | ||
5789 | if (!UNIT_VTABLE(u)->clean) | |
5790 | return -EOPNOTSUPP; | |
5791 | ||
5792 | if (mask == 0) | |
5793 | return -EUNATCH; | |
5794 | ||
5795 | if (u->load_state != UNIT_LOADED) | |
5796 | return -EBUSY; | |
5797 | ||
5798 | if (u->job) | |
5799 | return -EBUSY; | |
5800 | ||
5801 | state = unit_active_state(u); | |
5802 | if (!IN_SET(state, UNIT_INACTIVE)) | |
5803 | return -EBUSY; | |
5804 | ||
5805 | return UNIT_VTABLE(u)->clean(u, mask); | |
5806 | } | |
5807 | ||
5808 | int unit_can_clean(Unit *u, ExecCleanMask *ret) { | |
5809 | assert(u); | |
5810 | ||
5811 | if (!UNIT_VTABLE(u)->clean || | |
5812 | u->load_state != UNIT_LOADED) { | |
5813 | *ret = 0; | |
5814 | return 0; | |
5815 | } | |
5816 | ||
5817 | /* When the clean() method is set, can_clean() really should be set too */ | |
5818 | assert(UNIT_VTABLE(u)->can_clean); | |
5819 | ||
5820 | return UNIT_VTABLE(u)->can_clean(u, ret); | |
5821 | } | |
5822 | ||
5afe510c LP |
5823 | static const char* const collect_mode_table[_COLLECT_MODE_MAX] = { |
5824 | [COLLECT_INACTIVE] = "inactive", | |
5825 | [COLLECT_INACTIVE_OR_FAILED] = "inactive-or-failed", | |
5826 | }; | |
5827 | ||
5828 | DEFINE_STRING_TABLE_LOOKUP(collect_mode, CollectMode); |