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