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