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