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