]>
Commit | Line | Data |
---|---|---|
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ | |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <errno.h> | |
23 | #include <string.h> | |
24 | #include <stdlib.h> | |
25 | #include <unistd.h> | |
26 | #include <sys/stat.h> | |
27 | ||
28 | #include "sd-id128.h" | |
29 | #include "sd-messages.h" | |
30 | #include "set.h" | |
31 | #include "macro.h" | |
32 | #include "strv.h" | |
33 | #include "path-util.h" | |
34 | #include "log.h" | |
35 | #include "cgroup-util.h" | |
36 | #include "missing.h" | |
37 | #include "mkdir.h" | |
38 | #include "fileio-label.h" | |
39 | #include "formats-util.h" | |
40 | #include "process-util.h" | |
41 | #include "virt.h" | |
42 | #include "bus-common-errors.h" | |
43 | #include "bus-util.h" | |
44 | #include "dropin.h" | |
45 | #include "unit-name.h" | |
46 | #include "special.h" | |
47 | #include "unit.h" | |
48 | #include "load-fragment.h" | |
49 | #include "load-dropin.h" | |
50 | #include "dbus.h" | |
51 | #include "dbus-unit.h" | |
52 | #include "execute.h" | |
53 | ||
54 | const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = { | |
55 | [UNIT_SERVICE] = &service_vtable, | |
56 | [UNIT_SOCKET] = &socket_vtable, | |
57 | [UNIT_BUSNAME] = &busname_vtable, | |
58 | [UNIT_TARGET] = &target_vtable, | |
59 | [UNIT_SNAPSHOT] = &snapshot_vtable, | |
60 | [UNIT_DEVICE] = &device_vtable, | |
61 | [UNIT_MOUNT] = &mount_vtable, | |
62 | [UNIT_AUTOMOUNT] = &automount_vtable, | |
63 | [UNIT_SWAP] = &swap_vtable, | |
64 | [UNIT_TIMER] = &timer_vtable, | |
65 | [UNIT_PATH] = &path_vtable, | |
66 | [UNIT_SLICE] = &slice_vtable, | |
67 | [UNIT_SCOPE] = &scope_vtable | |
68 | }; | |
69 | ||
70 | static void maybe_warn_about_dependency(Unit *u, const char *other, UnitDependency dependency); | |
71 | ||
72 | Unit *unit_new(Manager *m, size_t size) { | |
73 | Unit *u; | |
74 | ||
75 | assert(m); | |
76 | assert(size >= sizeof(Unit)); | |
77 | ||
78 | u = malloc0(size); | |
79 | if (!u) | |
80 | return NULL; | |
81 | ||
82 | u->names = set_new(&string_hash_ops); | |
83 | if (!u->names) { | |
84 | free(u); | |
85 | return NULL; | |
86 | } | |
87 | ||
88 | u->manager = m; | |
89 | u->type = _UNIT_TYPE_INVALID; | |
90 | u->default_dependencies = true; | |
91 | u->unit_file_state = _UNIT_FILE_STATE_INVALID; | |
92 | u->unit_file_preset = -1; | |
93 | u->on_failure_job_mode = JOB_REPLACE; | |
94 | u->cgroup_inotify_wd = -1; | |
95 | ||
96 | RATELIMIT_INIT(u->auto_stop_ratelimit, 10 * USEC_PER_SEC, 16); | |
97 | ||
98 | return u; | |
99 | } | |
100 | ||
101 | bool unit_has_name(Unit *u, const char *name) { | |
102 | assert(u); | |
103 | assert(name); | |
104 | ||
105 | return !!set_get(u->names, (char*) name); | |
106 | } | |
107 | ||
108 | static void unit_init(Unit *u) { | |
109 | CGroupContext *cc; | |
110 | ExecContext *ec; | |
111 | KillContext *kc; | |
112 | ||
113 | assert(u); | |
114 | assert(u->manager); | |
115 | assert(u->type >= 0); | |
116 | ||
117 | cc = unit_get_cgroup_context(u); | |
118 | if (cc) { | |
119 | cgroup_context_init(cc); | |
120 | ||
121 | /* Copy in the manager defaults into the cgroup | |
122 | * context, _before_ the rest of the settings have | |
123 | * been initialized */ | |
124 | ||
125 | cc->cpu_accounting = u->manager->default_cpu_accounting; | |
126 | cc->blockio_accounting = u->manager->default_blockio_accounting; | |
127 | cc->memory_accounting = u->manager->default_memory_accounting; | |
128 | cc->tasks_accounting = u->manager->default_tasks_accounting; | |
129 | } | |
130 | ||
131 | ec = unit_get_exec_context(u); | |
132 | if (ec) | |
133 | exec_context_init(ec); | |
134 | ||
135 | kc = unit_get_kill_context(u); | |
136 | if (kc) | |
137 | kill_context_init(kc); | |
138 | ||
139 | if (UNIT_VTABLE(u)->init) | |
140 | UNIT_VTABLE(u)->init(u); | |
141 | } | |
142 | ||
143 | int unit_add_name(Unit *u, const char *text) { | |
144 | _cleanup_free_ char *s = NULL, *i = NULL; | |
145 | UnitType t; | |
146 | int r; | |
147 | ||
148 | assert(u); | |
149 | assert(text); | |
150 | ||
151 | if (unit_name_is_valid(text, UNIT_NAME_TEMPLATE)) { | |
152 | ||
153 | if (!u->instance) | |
154 | return -EINVAL; | |
155 | ||
156 | r = unit_name_replace_instance(text, u->instance, &s); | |
157 | if (r < 0) | |
158 | return r; | |
159 | } else { | |
160 | s = strdup(text); | |
161 | if (!s) | |
162 | return -ENOMEM; | |
163 | } | |
164 | ||
165 | if (set_contains(u->names, s)) | |
166 | return 0; | |
167 | if (hashmap_contains(u->manager->units, s)) | |
168 | return -EEXIST; | |
169 | ||
170 | if (!unit_name_is_valid(s, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) | |
171 | return -EINVAL; | |
172 | ||
173 | t = unit_name_to_type(s); | |
174 | if (t < 0) | |
175 | return -EINVAL; | |
176 | ||
177 | if (u->type != _UNIT_TYPE_INVALID && t != u->type) | |
178 | return -EINVAL; | |
179 | ||
180 | r = unit_name_to_instance(s, &i); | |
181 | if (r < 0) | |
182 | return r; | |
183 | ||
184 | if (i && unit_vtable[t]->no_instances) | |
185 | return -EINVAL; | |
186 | ||
187 | /* Ensure that this unit is either instanced or not instanced, | |
188 | * but not both. Note that we do allow names with different | |
189 | * instance names however! */ | |
190 | if (u->type != _UNIT_TYPE_INVALID && !u->instance != !i) | |
191 | return -EINVAL; | |
192 | ||
193 | if (unit_vtable[t]->no_alias && !set_isempty(u->names)) | |
194 | return -EEXIST; | |
195 | ||
196 | if (hashmap_size(u->manager->units) >= MANAGER_MAX_NAMES) | |
197 | return -E2BIG; | |
198 | ||
199 | r = set_put(u->names, s); | |
200 | if (r < 0) | |
201 | return r; | |
202 | assert(r > 0); | |
203 | ||
204 | r = hashmap_put(u->manager->units, s, u); | |
205 | if (r < 0) { | |
206 | (void) set_remove(u->names, s); | |
207 | return r; | |
208 | } | |
209 | ||
210 | if (u->type == _UNIT_TYPE_INVALID) { | |
211 | u->type = t; | |
212 | u->id = s; | |
213 | u->instance = i; | |
214 | ||
215 | LIST_PREPEND(units_by_type, u->manager->units_by_type[t], u); | |
216 | ||
217 | unit_init(u); | |
218 | ||
219 | i = NULL; | |
220 | } | |
221 | ||
222 | s = NULL; | |
223 | ||
224 | unit_add_to_dbus_queue(u); | |
225 | return 0; | |
226 | } | |
227 | ||
228 | int unit_choose_id(Unit *u, const char *name) { | |
229 | _cleanup_free_ char *t = NULL; | |
230 | char *s, *i; | |
231 | int r; | |
232 | ||
233 | assert(u); | |
234 | assert(name); | |
235 | ||
236 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { | |
237 | ||
238 | if (!u->instance) | |
239 | return -EINVAL; | |
240 | ||
241 | r = unit_name_replace_instance(name, u->instance, &t); | |
242 | if (r < 0) | |
243 | return r; | |
244 | ||
245 | name = t; | |
246 | } | |
247 | ||
248 | /* Selects one of the names of this unit as the id */ | |
249 | s = set_get(u->names, (char*) name); | |
250 | if (!s) | |
251 | return -ENOENT; | |
252 | ||
253 | /* Determine the new instance from the new id */ | |
254 | r = unit_name_to_instance(s, &i); | |
255 | if (r < 0) | |
256 | return r; | |
257 | ||
258 | u->id = s; | |
259 | ||
260 | free(u->instance); | |
261 | u->instance = i; | |
262 | ||
263 | unit_add_to_dbus_queue(u); | |
264 | ||
265 | return 0; | |
266 | } | |
267 | ||
268 | int unit_set_description(Unit *u, const char *description) { | |
269 | char *s; | |
270 | ||
271 | assert(u); | |
272 | ||
273 | if (isempty(description)) | |
274 | s = NULL; | |
275 | else { | |
276 | s = strdup(description); | |
277 | if (!s) | |
278 | return -ENOMEM; | |
279 | } | |
280 | ||
281 | free(u->description); | |
282 | u->description = s; | |
283 | ||
284 | unit_add_to_dbus_queue(u); | |
285 | return 0; | |
286 | } | |
287 | ||
288 | bool unit_check_gc(Unit *u) { | |
289 | UnitActiveState state; | |
290 | assert(u); | |
291 | ||
292 | if (u->job) | |
293 | return true; | |
294 | ||
295 | if (u->nop_job) | |
296 | return true; | |
297 | ||
298 | state = unit_active_state(u); | |
299 | ||
300 | /* If the unit is inactive and failed and no job is queued for | |
301 | * it, then release its runtime resources */ | |
302 | if (UNIT_IS_INACTIVE_OR_FAILED(state) && | |
303 | UNIT_VTABLE(u)->release_resources) | |
304 | UNIT_VTABLE(u)->release_resources(u); | |
305 | ||
306 | /* But we keep the unit object around for longer when it is | |
307 | * referenced or configured to not be gc'ed */ | |
308 | if (state != UNIT_INACTIVE) | |
309 | return true; | |
310 | ||
311 | if (UNIT_VTABLE(u)->no_gc) | |
312 | return true; | |
313 | ||
314 | if (u->no_gc) | |
315 | return true; | |
316 | ||
317 | if (u->refs) | |
318 | return true; | |
319 | ||
320 | if (UNIT_VTABLE(u)->check_gc) | |
321 | if (UNIT_VTABLE(u)->check_gc(u)) | |
322 | return true; | |
323 | ||
324 | return false; | |
325 | } | |
326 | ||
327 | void unit_add_to_load_queue(Unit *u) { | |
328 | assert(u); | |
329 | assert(u->type != _UNIT_TYPE_INVALID); | |
330 | ||
331 | if (u->load_state != UNIT_STUB || u->in_load_queue) | |
332 | return; | |
333 | ||
334 | LIST_PREPEND(load_queue, u->manager->load_queue, u); | |
335 | u->in_load_queue = true; | |
336 | } | |
337 | ||
338 | void unit_add_to_cleanup_queue(Unit *u) { | |
339 | assert(u); | |
340 | ||
341 | if (u->in_cleanup_queue) | |
342 | return; | |
343 | ||
344 | LIST_PREPEND(cleanup_queue, u->manager->cleanup_queue, u); | |
345 | u->in_cleanup_queue = true; | |
346 | } | |
347 | ||
348 | void unit_add_to_gc_queue(Unit *u) { | |
349 | assert(u); | |
350 | ||
351 | if (u->in_gc_queue || u->in_cleanup_queue) | |
352 | return; | |
353 | ||
354 | if (unit_check_gc(u)) | |
355 | return; | |
356 | ||
357 | LIST_PREPEND(gc_queue, u->manager->gc_queue, u); | |
358 | u->in_gc_queue = true; | |
359 | ||
360 | u->manager->n_in_gc_queue ++; | |
361 | } | |
362 | ||
363 | void unit_add_to_dbus_queue(Unit *u) { | |
364 | assert(u); | |
365 | assert(u->type != _UNIT_TYPE_INVALID); | |
366 | ||
367 | if (u->load_state == UNIT_STUB || u->in_dbus_queue) | |
368 | return; | |
369 | ||
370 | /* Shortcut things if nobody cares */ | |
371 | if (sd_bus_track_count(u->manager->subscribed) <= 0 && | |
372 | set_isempty(u->manager->private_buses)) { | |
373 | u->sent_dbus_new_signal = true; | |
374 | return; | |
375 | } | |
376 | ||
377 | LIST_PREPEND(dbus_queue, u->manager->dbus_unit_queue, u); | |
378 | u->in_dbus_queue = true; | |
379 | } | |
380 | ||
381 | static void bidi_set_free(Unit *u, Set *s) { | |
382 | Iterator i; | |
383 | Unit *other; | |
384 | ||
385 | assert(u); | |
386 | ||
387 | /* Frees the set and makes sure we are dropped from the | |
388 | * inverse pointers */ | |
389 | ||
390 | SET_FOREACH(other, s, i) { | |
391 | UnitDependency d; | |
392 | ||
393 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
394 | set_remove(other->dependencies[d], u); | |
395 | ||
396 | unit_add_to_gc_queue(other); | |
397 | } | |
398 | ||
399 | set_free(s); | |
400 | } | |
401 | ||
402 | static void unit_remove_transient(Unit *u) { | |
403 | char **i; | |
404 | ||
405 | assert(u); | |
406 | ||
407 | if (!u->transient) | |
408 | return; | |
409 | ||
410 | if (u->fragment_path) | |
411 | (void) unlink(u->fragment_path); | |
412 | ||
413 | STRV_FOREACH(i, u->dropin_paths) { | |
414 | _cleanup_free_ char *p = NULL; | |
415 | int r; | |
416 | ||
417 | (void) unlink(*i); | |
418 | ||
419 | r = path_get_parent(*i, &p); | |
420 | if (r >= 0) | |
421 | (void) rmdir(p); | |
422 | } | |
423 | } | |
424 | ||
425 | static void unit_free_requires_mounts_for(Unit *u) { | |
426 | char **j; | |
427 | ||
428 | STRV_FOREACH(j, u->requires_mounts_for) { | |
429 | char s[strlen(*j) + 1]; | |
430 | ||
431 | PATH_FOREACH_PREFIX_MORE(s, *j) { | |
432 | char *y; | |
433 | Set *x; | |
434 | ||
435 | x = hashmap_get2(u->manager->units_requiring_mounts_for, s, (void**) &y); | |
436 | if (!x) | |
437 | continue; | |
438 | ||
439 | set_remove(x, u); | |
440 | ||
441 | if (set_isempty(x)) { | |
442 | hashmap_remove(u->manager->units_requiring_mounts_for, y); | |
443 | free(y); | |
444 | set_free(x); | |
445 | } | |
446 | } | |
447 | } | |
448 | ||
449 | u->requires_mounts_for = strv_free(u->requires_mounts_for); | |
450 | } | |
451 | ||
452 | static void unit_done(Unit *u) { | |
453 | ExecContext *ec; | |
454 | CGroupContext *cc; | |
455 | int r; | |
456 | ||
457 | assert(u); | |
458 | ||
459 | if (u->type < 0) | |
460 | return; | |
461 | ||
462 | if (UNIT_VTABLE(u)->done) | |
463 | UNIT_VTABLE(u)->done(u); | |
464 | ||
465 | ec = unit_get_exec_context(u); | |
466 | if (ec) | |
467 | exec_context_done(ec); | |
468 | ||
469 | cc = unit_get_cgroup_context(u); | |
470 | if (cc) | |
471 | cgroup_context_done(cc); | |
472 | ||
473 | r = unit_remove_from_netclass_cgroup(u); | |
474 | if (r < 0) | |
475 | log_warning_errno(r, "Unable to remove unit from netclass group: %m"); | |
476 | } | |
477 | ||
478 | void unit_free(Unit *u) { | |
479 | UnitDependency d; | |
480 | Iterator i; | |
481 | char *t; | |
482 | ||
483 | assert(u); | |
484 | ||
485 | if (u->manager->n_reloading <= 0) | |
486 | unit_remove_transient(u); | |
487 | ||
488 | bus_unit_send_removed_signal(u); | |
489 | ||
490 | unit_done(u); | |
491 | ||
492 | sd_bus_slot_unref(u->match_bus_slot); | |
493 | ||
494 | unit_free_requires_mounts_for(u); | |
495 | ||
496 | SET_FOREACH(t, u->names, i) | |
497 | hashmap_remove_value(u->manager->units, t, u); | |
498 | ||
499 | if (u->job) { | |
500 | Job *j = u->job; | |
501 | job_uninstall(j); | |
502 | job_free(j); | |
503 | } | |
504 | ||
505 | if (u->nop_job) { | |
506 | Job *j = u->nop_job; | |
507 | job_uninstall(j); | |
508 | job_free(j); | |
509 | } | |
510 | ||
511 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
512 | bidi_set_free(u, u->dependencies[d]); | |
513 | ||
514 | if (u->type != _UNIT_TYPE_INVALID) | |
515 | LIST_REMOVE(units_by_type, u->manager->units_by_type[u->type], u); | |
516 | ||
517 | if (u->in_load_queue) | |
518 | LIST_REMOVE(load_queue, u->manager->load_queue, u); | |
519 | ||
520 | if (u->in_dbus_queue) | |
521 | LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u); | |
522 | ||
523 | if (u->in_cleanup_queue) | |
524 | LIST_REMOVE(cleanup_queue, u->manager->cleanup_queue, u); | |
525 | ||
526 | if (u->in_gc_queue) { | |
527 | LIST_REMOVE(gc_queue, u->manager->gc_queue, u); | |
528 | u->manager->n_in_gc_queue--; | |
529 | } | |
530 | ||
531 | if (u->in_cgroup_queue) | |
532 | LIST_REMOVE(cgroup_queue, u->manager->cgroup_queue, u); | |
533 | ||
534 | unit_release_cgroup(u); | |
535 | ||
536 | (void) manager_update_failed_units(u->manager, u, false); | |
537 | set_remove(u->manager->startup_units, u); | |
538 | ||
539 | free(u->description); | |
540 | strv_free(u->documentation); | |
541 | free(u->fragment_path); | |
542 | free(u->source_path); | |
543 | strv_free(u->dropin_paths); | |
544 | free(u->instance); | |
545 | ||
546 | free(u->job_timeout_reboot_arg); | |
547 | ||
548 | set_free_free(u->names); | |
549 | ||
550 | unit_unwatch_all_pids(u); | |
551 | ||
552 | condition_free_list(u->conditions); | |
553 | condition_free_list(u->asserts); | |
554 | ||
555 | unit_ref_unset(&u->slice); | |
556 | ||
557 | while (u->refs) | |
558 | unit_ref_unset(u->refs); | |
559 | ||
560 | free(u); | |
561 | } | |
562 | ||
563 | UnitActiveState unit_active_state(Unit *u) { | |
564 | assert(u); | |
565 | ||
566 | if (u->load_state == UNIT_MERGED) | |
567 | return unit_active_state(unit_follow_merge(u)); | |
568 | ||
569 | /* After a reload it might happen that a unit is not correctly | |
570 | * loaded but still has a process around. That's why we won't | |
571 | * shortcut failed loading to UNIT_INACTIVE_FAILED. */ | |
572 | ||
573 | return UNIT_VTABLE(u)->active_state(u); | |
574 | } | |
575 | ||
576 | const char* unit_sub_state_to_string(Unit *u) { | |
577 | assert(u); | |
578 | ||
579 | return UNIT_VTABLE(u)->sub_state_to_string(u); | |
580 | } | |
581 | ||
582 | static int complete_move(Set **s, Set **other) { | |
583 | int r; | |
584 | ||
585 | assert(s); | |
586 | assert(other); | |
587 | ||
588 | if (!*other) | |
589 | return 0; | |
590 | ||
591 | if (*s) { | |
592 | r = set_move(*s, *other); | |
593 | if (r < 0) | |
594 | return r; | |
595 | } else { | |
596 | *s = *other; | |
597 | *other = NULL; | |
598 | } | |
599 | ||
600 | return 0; | |
601 | } | |
602 | ||
603 | static int merge_names(Unit *u, Unit *other) { | |
604 | char *t; | |
605 | Iterator i; | |
606 | int r; | |
607 | ||
608 | assert(u); | |
609 | assert(other); | |
610 | ||
611 | r = complete_move(&u->names, &other->names); | |
612 | if (r < 0) | |
613 | return r; | |
614 | ||
615 | set_free_free(other->names); | |
616 | other->names = NULL; | |
617 | other->id = NULL; | |
618 | ||
619 | SET_FOREACH(t, u->names, i) | |
620 | assert_se(hashmap_replace(u->manager->units, t, u) == 0); | |
621 | ||
622 | return 0; | |
623 | } | |
624 | ||
625 | static int reserve_dependencies(Unit *u, Unit *other, UnitDependency d) { | |
626 | unsigned n_reserve; | |
627 | ||
628 | assert(u); | |
629 | assert(other); | |
630 | assert(d < _UNIT_DEPENDENCY_MAX); | |
631 | ||
632 | /* | |
633 | * If u does not have this dependency set allocated, there is no need | |
634 | * to reserve anything. In that case other's set will be transferred | |
635 | * as a whole to u by complete_move(). | |
636 | */ | |
637 | if (!u->dependencies[d]) | |
638 | return 0; | |
639 | ||
640 | /* merge_dependencies() will skip a u-on-u dependency */ | |
641 | n_reserve = set_size(other->dependencies[d]) - !!set_get(other->dependencies[d], u); | |
642 | ||
643 | return set_reserve(u->dependencies[d], n_reserve); | |
644 | } | |
645 | ||
646 | static void merge_dependencies(Unit *u, Unit *other, const char *other_id, UnitDependency d) { | |
647 | Iterator i; | |
648 | Unit *back; | |
649 | int r; | |
650 | ||
651 | assert(u); | |
652 | assert(other); | |
653 | assert(d < _UNIT_DEPENDENCY_MAX); | |
654 | ||
655 | /* Fix backwards pointers */ | |
656 | SET_FOREACH(back, other->dependencies[d], i) { | |
657 | UnitDependency k; | |
658 | ||
659 | for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++) { | |
660 | /* Do not add dependencies between u and itself */ | |
661 | if (back == u) { | |
662 | if (set_remove(back->dependencies[k], other)) | |
663 | maybe_warn_about_dependency(u, other_id, k); | |
664 | } else { | |
665 | r = set_remove_and_put(back->dependencies[k], other, u); | |
666 | if (r == -EEXIST) | |
667 | set_remove(back->dependencies[k], other); | |
668 | else | |
669 | assert(r >= 0 || r == -ENOENT); | |
670 | } | |
671 | } | |
672 | } | |
673 | ||
674 | /* Also do not move dependencies on u to itself */ | |
675 | back = set_remove(other->dependencies[d], u); | |
676 | if (back) | |
677 | maybe_warn_about_dependency(u, other_id, d); | |
678 | ||
679 | /* The move cannot fail. The caller must have performed a reservation. */ | |
680 | assert_se(complete_move(&u->dependencies[d], &other->dependencies[d]) == 0); | |
681 | ||
682 | other->dependencies[d] = set_free(other->dependencies[d]); | |
683 | } | |
684 | ||
685 | int unit_merge(Unit *u, Unit *other) { | |
686 | UnitDependency d; | |
687 | const char *other_id = NULL; | |
688 | int r; | |
689 | ||
690 | assert(u); | |
691 | assert(other); | |
692 | assert(u->manager == other->manager); | |
693 | assert(u->type != _UNIT_TYPE_INVALID); | |
694 | ||
695 | other = unit_follow_merge(other); | |
696 | ||
697 | if (other == u) | |
698 | return 0; | |
699 | ||
700 | if (u->type != other->type) | |
701 | return -EINVAL; | |
702 | ||
703 | if (!u->instance != !other->instance) | |
704 | return -EINVAL; | |
705 | ||
706 | if (other->load_state != UNIT_STUB && | |
707 | other->load_state != UNIT_NOT_FOUND) | |
708 | return -EEXIST; | |
709 | ||
710 | if (other->job) | |
711 | return -EEXIST; | |
712 | ||
713 | if (other->nop_job) | |
714 | return -EEXIST; | |
715 | ||
716 | if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) | |
717 | return -EEXIST; | |
718 | ||
719 | if (other->id) | |
720 | other_id = strdupa(other->id); | |
721 | ||
722 | /* Make reservations to ensure merge_dependencies() won't fail */ | |
723 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { | |
724 | r = reserve_dependencies(u, other, d); | |
725 | /* | |
726 | * We don't rollback reservations if we fail. We don't have | |
727 | * a way to undo reservations. A reservation is not a leak. | |
728 | */ | |
729 | if (r < 0) | |
730 | return r; | |
731 | } | |
732 | ||
733 | /* Merge names */ | |
734 | r = merge_names(u, other); | |
735 | if (r < 0) | |
736 | return r; | |
737 | ||
738 | /* Redirect all references */ | |
739 | while (other->refs) | |
740 | unit_ref_set(other->refs, u); | |
741 | ||
742 | /* Merge dependencies */ | |
743 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
744 | merge_dependencies(u, other, other_id, d); | |
745 | ||
746 | other->load_state = UNIT_MERGED; | |
747 | other->merged_into = u; | |
748 | ||
749 | /* If there is still some data attached to the other node, we | |
750 | * don't need it anymore, and can free it. */ | |
751 | if (other->load_state != UNIT_STUB) | |
752 | if (UNIT_VTABLE(other)->done) | |
753 | UNIT_VTABLE(other)->done(other); | |
754 | ||
755 | unit_add_to_dbus_queue(u); | |
756 | unit_add_to_cleanup_queue(other); | |
757 | ||
758 | return 0; | |
759 | } | |
760 | ||
761 | int unit_merge_by_name(Unit *u, const char *name) { | |
762 | Unit *other; | |
763 | int r; | |
764 | _cleanup_free_ char *s = NULL; | |
765 | ||
766 | assert(u); | |
767 | assert(name); | |
768 | ||
769 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { | |
770 | if (!u->instance) | |
771 | return -EINVAL; | |
772 | ||
773 | r = unit_name_replace_instance(name, u->instance, &s); | |
774 | if (r < 0) | |
775 | return r; | |
776 | ||
777 | name = s; | |
778 | } | |
779 | ||
780 | other = manager_get_unit(u->manager, name); | |
781 | if (other) | |
782 | return unit_merge(u, other); | |
783 | ||
784 | return unit_add_name(u, name); | |
785 | } | |
786 | ||
787 | Unit* unit_follow_merge(Unit *u) { | |
788 | assert(u); | |
789 | ||
790 | while (u->load_state == UNIT_MERGED) | |
791 | assert_se(u = u->merged_into); | |
792 | ||
793 | return u; | |
794 | } | |
795 | ||
796 | int unit_add_exec_dependencies(Unit *u, ExecContext *c) { | |
797 | int r; | |
798 | ||
799 | assert(u); | |
800 | assert(c); | |
801 | ||
802 | if (c->working_directory) { | |
803 | r = unit_require_mounts_for(u, c->working_directory); | |
804 | if (r < 0) | |
805 | return r; | |
806 | } | |
807 | ||
808 | if (c->root_directory) { | |
809 | r = unit_require_mounts_for(u, c->root_directory); | |
810 | if (r < 0) | |
811 | return r; | |
812 | } | |
813 | ||
814 | if (u->manager->running_as != MANAGER_SYSTEM) | |
815 | return 0; | |
816 | ||
817 | if (c->private_tmp) { | |
818 | r = unit_require_mounts_for(u, "/tmp"); | |
819 | if (r < 0) | |
820 | return r; | |
821 | ||
822 | r = unit_require_mounts_for(u, "/var/tmp"); | |
823 | if (r < 0) | |
824 | return r; | |
825 | } | |
826 | ||
827 | if (c->std_output != EXEC_OUTPUT_KMSG && | |
828 | c->std_output != EXEC_OUTPUT_SYSLOG && | |
829 | c->std_output != EXEC_OUTPUT_JOURNAL && | |
830 | c->std_output != EXEC_OUTPUT_KMSG_AND_CONSOLE && | |
831 | c->std_output != EXEC_OUTPUT_SYSLOG_AND_CONSOLE && | |
832 | c->std_output != EXEC_OUTPUT_JOURNAL_AND_CONSOLE && | |
833 | c->std_error != EXEC_OUTPUT_KMSG && | |
834 | c->std_error != EXEC_OUTPUT_SYSLOG && | |
835 | c->std_error != EXEC_OUTPUT_JOURNAL && | |
836 | c->std_error != EXEC_OUTPUT_KMSG_AND_CONSOLE && | |
837 | c->std_error != EXEC_OUTPUT_JOURNAL_AND_CONSOLE && | |
838 | c->std_error != EXEC_OUTPUT_SYSLOG_AND_CONSOLE) | |
839 | return 0; | |
840 | ||
841 | /* If syslog or kernel logging is requested, make sure our own | |
842 | * logging daemon is run first. */ | |
843 | ||
844 | r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_JOURNALD_SOCKET, NULL, true); | |
845 | if (r < 0) | |
846 | return r; | |
847 | ||
848 | return 0; | |
849 | } | |
850 | ||
851 | const char *unit_description(Unit *u) { | |
852 | assert(u); | |
853 | ||
854 | if (u->description) | |
855 | return u->description; | |
856 | ||
857 | return strna(u->id); | |
858 | } | |
859 | ||
860 | void unit_dump(Unit *u, FILE *f, const char *prefix) { | |
861 | char *t, **j; | |
862 | UnitDependency d; | |
863 | Iterator i; | |
864 | const char *prefix2; | |
865 | char | |
866 | timestamp1[FORMAT_TIMESTAMP_MAX], | |
867 | timestamp2[FORMAT_TIMESTAMP_MAX], | |
868 | timestamp3[FORMAT_TIMESTAMP_MAX], | |
869 | timestamp4[FORMAT_TIMESTAMP_MAX], | |
870 | timespan[FORMAT_TIMESPAN_MAX]; | |
871 | Unit *following; | |
872 | _cleanup_set_free_ Set *following_set = NULL; | |
873 | int r; | |
874 | ||
875 | assert(u); | |
876 | assert(u->type >= 0); | |
877 | ||
878 | prefix = strempty(prefix); | |
879 | prefix2 = strjoina(prefix, "\t"); | |
880 | ||
881 | fprintf(f, | |
882 | "%s-> Unit %s:\n" | |
883 | "%s\tDescription: %s\n" | |
884 | "%s\tInstance: %s\n" | |
885 | "%s\tUnit Load State: %s\n" | |
886 | "%s\tUnit Active State: %s\n" | |
887 | "%s\tInactive Exit Timestamp: %s\n" | |
888 | "%s\tActive Enter Timestamp: %s\n" | |
889 | "%s\tActive Exit Timestamp: %s\n" | |
890 | "%s\tInactive Enter Timestamp: %s\n" | |
891 | "%s\tGC Check Good: %s\n" | |
892 | "%s\tNeed Daemon Reload: %s\n" | |
893 | "%s\tTransient: %s\n" | |
894 | "%s\tSlice: %s\n" | |
895 | "%s\tCGroup: %s\n" | |
896 | "%s\tCGroup realized: %s\n" | |
897 | "%s\tCGroup mask: 0x%x\n" | |
898 | "%s\tCGroup members mask: 0x%x\n", | |
899 | prefix, u->id, | |
900 | prefix, unit_description(u), | |
901 | prefix, strna(u->instance), | |
902 | prefix, unit_load_state_to_string(u->load_state), | |
903 | prefix, unit_active_state_to_string(unit_active_state(u)), | |
904 | prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->inactive_exit_timestamp.realtime)), | |
905 | prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->active_enter_timestamp.realtime)), | |
906 | prefix, strna(format_timestamp(timestamp3, sizeof(timestamp3), u->active_exit_timestamp.realtime)), | |
907 | prefix, strna(format_timestamp(timestamp4, sizeof(timestamp4), u->inactive_enter_timestamp.realtime)), | |
908 | prefix, yes_no(unit_check_gc(u)), | |
909 | prefix, yes_no(unit_need_daemon_reload(u)), | |
910 | prefix, yes_no(u->transient), | |
911 | prefix, strna(unit_slice_name(u)), | |
912 | prefix, strna(u->cgroup_path), | |
913 | prefix, yes_no(u->cgroup_realized), | |
914 | prefix, u->cgroup_realized_mask, | |
915 | prefix, u->cgroup_members_mask); | |
916 | ||
917 | SET_FOREACH(t, u->names, i) | |
918 | fprintf(f, "%s\tName: %s\n", prefix, t); | |
919 | ||
920 | STRV_FOREACH(j, u->documentation) | |
921 | fprintf(f, "%s\tDocumentation: %s\n", prefix, *j); | |
922 | ||
923 | following = unit_following(u); | |
924 | if (following) | |
925 | fprintf(f, "%s\tFollowing: %s\n", prefix, following->id); | |
926 | ||
927 | r = unit_following_set(u, &following_set); | |
928 | if (r >= 0) { | |
929 | Unit *other; | |
930 | ||
931 | SET_FOREACH(other, following_set, i) | |
932 | fprintf(f, "%s\tFollowing Set Member: %s\n", prefix, other->id); | |
933 | } | |
934 | ||
935 | if (u->fragment_path) | |
936 | fprintf(f, "%s\tFragment Path: %s\n", prefix, u->fragment_path); | |
937 | ||
938 | if (u->source_path) | |
939 | fprintf(f, "%s\tSource Path: %s\n", prefix, u->source_path); | |
940 | ||
941 | STRV_FOREACH(j, u->dropin_paths) | |
942 | fprintf(f, "%s\tDropIn Path: %s\n", prefix, *j); | |
943 | ||
944 | if (u->job_timeout > 0) | |
945 | fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->job_timeout, 0)); | |
946 | ||
947 | if (u->job_timeout_action != FAILURE_ACTION_NONE) | |
948 | fprintf(f, "%s\tJob Timeout Action: %s\n", prefix, failure_action_to_string(u->job_timeout_action)); | |
949 | ||
950 | if (u->job_timeout_reboot_arg) | |
951 | fprintf(f, "%s\tJob Timeout Reboot Argument: %s\n", prefix, u->job_timeout_reboot_arg); | |
952 | ||
953 | condition_dump_list(u->conditions, f, prefix, condition_type_to_string); | |
954 | condition_dump_list(u->asserts, f, prefix, assert_type_to_string); | |
955 | ||
956 | if (dual_timestamp_is_set(&u->condition_timestamp)) | |
957 | fprintf(f, | |
958 | "%s\tCondition Timestamp: %s\n" | |
959 | "%s\tCondition Result: %s\n", | |
960 | prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->condition_timestamp.realtime)), | |
961 | prefix, yes_no(u->condition_result)); | |
962 | ||
963 | if (dual_timestamp_is_set(&u->assert_timestamp)) | |
964 | fprintf(f, | |
965 | "%s\tAssert Timestamp: %s\n" | |
966 | "%s\tAssert Result: %s\n", | |
967 | prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->assert_timestamp.realtime)), | |
968 | prefix, yes_no(u->assert_result)); | |
969 | ||
970 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { | |
971 | Unit *other; | |
972 | ||
973 | SET_FOREACH(other, u->dependencies[d], i) | |
974 | fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), other->id); | |
975 | } | |
976 | ||
977 | if (!strv_isempty(u->requires_mounts_for)) { | |
978 | fprintf(f, | |
979 | "%s\tRequiresMountsFor:", prefix); | |
980 | ||
981 | STRV_FOREACH(j, u->requires_mounts_for) | |
982 | fprintf(f, " %s", *j); | |
983 | ||
984 | fputs("\n", f); | |
985 | } | |
986 | ||
987 | if (u->load_state == UNIT_LOADED) { | |
988 | ||
989 | fprintf(f, | |
990 | "%s\tStopWhenUnneeded: %s\n" | |
991 | "%s\tRefuseManualStart: %s\n" | |
992 | "%s\tRefuseManualStop: %s\n" | |
993 | "%s\tDefaultDependencies: %s\n" | |
994 | "%s\tOnFailureJobMode: %s\n" | |
995 | "%s\tIgnoreOnIsolate: %s\n" | |
996 | "%s\tIgnoreOnSnapshot: %s\n", | |
997 | prefix, yes_no(u->stop_when_unneeded), | |
998 | prefix, yes_no(u->refuse_manual_start), | |
999 | prefix, yes_no(u->refuse_manual_stop), | |
1000 | prefix, yes_no(u->default_dependencies), | |
1001 | prefix, job_mode_to_string(u->on_failure_job_mode), | |
1002 | prefix, yes_no(u->ignore_on_isolate), | |
1003 | prefix, yes_no(u->ignore_on_snapshot)); | |
1004 | ||
1005 | if (UNIT_VTABLE(u)->dump) | |
1006 | UNIT_VTABLE(u)->dump(u, f, prefix2); | |
1007 | ||
1008 | } else if (u->load_state == UNIT_MERGED) | |
1009 | fprintf(f, | |
1010 | "%s\tMerged into: %s\n", | |
1011 | prefix, u->merged_into->id); | |
1012 | else if (u->load_state == UNIT_ERROR) | |
1013 | fprintf(f, "%s\tLoad Error Code: %s\n", prefix, strerror(-u->load_error)); | |
1014 | ||
1015 | ||
1016 | if (u->job) | |
1017 | job_dump(u->job, f, prefix2); | |
1018 | ||
1019 | if (u->nop_job) | |
1020 | job_dump(u->nop_job, f, prefix2); | |
1021 | ||
1022 | } | |
1023 | ||
1024 | /* Common implementation for multiple backends */ | |
1025 | int unit_load_fragment_and_dropin(Unit *u) { | |
1026 | int r; | |
1027 | ||
1028 | assert(u); | |
1029 | ||
1030 | /* Load a .{service,socket,...} file */ | |
1031 | r = unit_load_fragment(u); | |
1032 | if (r < 0) | |
1033 | return r; | |
1034 | ||
1035 | if (u->load_state == UNIT_STUB) | |
1036 | return -ENOENT; | |
1037 | ||
1038 | /* Load drop-in directory data */ | |
1039 | r = unit_load_dropin(unit_follow_merge(u)); | |
1040 | if (r < 0) | |
1041 | return r; | |
1042 | ||
1043 | return 0; | |
1044 | } | |
1045 | ||
1046 | /* Common implementation for multiple backends */ | |
1047 | int unit_load_fragment_and_dropin_optional(Unit *u) { | |
1048 | int r; | |
1049 | ||
1050 | assert(u); | |
1051 | ||
1052 | /* Same as unit_load_fragment_and_dropin(), but whether | |
1053 | * something can be loaded or not doesn't matter. */ | |
1054 | ||
1055 | /* Load a .service file */ | |
1056 | r = unit_load_fragment(u); | |
1057 | if (r < 0) | |
1058 | return r; | |
1059 | ||
1060 | if (u->load_state == UNIT_STUB) | |
1061 | u->load_state = UNIT_LOADED; | |
1062 | ||
1063 | /* Load drop-in directory data */ | |
1064 | r = unit_load_dropin(unit_follow_merge(u)); | |
1065 | if (r < 0) | |
1066 | return r; | |
1067 | ||
1068 | return 0; | |
1069 | } | |
1070 | ||
1071 | int unit_add_default_target_dependency(Unit *u, Unit *target) { | |
1072 | assert(u); | |
1073 | assert(target); | |
1074 | ||
1075 | if (target->type != UNIT_TARGET) | |
1076 | return 0; | |
1077 | ||
1078 | /* Only add the dependency if both units are loaded, so that | |
1079 | * that loop check below is reliable */ | |
1080 | if (u->load_state != UNIT_LOADED || | |
1081 | target->load_state != UNIT_LOADED) | |
1082 | return 0; | |
1083 | ||
1084 | /* If either side wants no automatic dependencies, then let's | |
1085 | * skip this */ | |
1086 | if (!u->default_dependencies || | |
1087 | !target->default_dependencies) | |
1088 | return 0; | |
1089 | ||
1090 | /* Don't create loops */ | |
1091 | if (set_get(target->dependencies[UNIT_BEFORE], u)) | |
1092 | return 0; | |
1093 | ||
1094 | return unit_add_dependency(target, UNIT_AFTER, u, true); | |
1095 | } | |
1096 | ||
1097 | static int unit_add_target_dependencies(Unit *u) { | |
1098 | ||
1099 | static const UnitDependency deps[] = { | |
1100 | UNIT_REQUIRED_BY, | |
1101 | UNIT_REQUIRED_BY_OVERRIDABLE, | |
1102 | UNIT_REQUISITE_OF, | |
1103 | UNIT_REQUISITE_OF_OVERRIDABLE, | |
1104 | UNIT_WANTED_BY, | |
1105 | UNIT_BOUND_BY | |
1106 | }; | |
1107 | ||
1108 | Unit *target; | |
1109 | Iterator i; | |
1110 | unsigned k; | |
1111 | int r = 0; | |
1112 | ||
1113 | assert(u); | |
1114 | ||
1115 | for (k = 0; k < ELEMENTSOF(deps); k++) | |
1116 | SET_FOREACH(target, u->dependencies[deps[k]], i) { | |
1117 | r = unit_add_default_target_dependency(u, target); | |
1118 | if (r < 0) | |
1119 | return r; | |
1120 | } | |
1121 | ||
1122 | return r; | |
1123 | } | |
1124 | ||
1125 | static int unit_add_slice_dependencies(Unit *u) { | |
1126 | assert(u); | |
1127 | ||
1128 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) | |
1129 | return 0; | |
1130 | ||
1131 | if (UNIT_ISSET(u->slice)) | |
1132 | return unit_add_two_dependencies(u, UNIT_AFTER, UNIT_WANTS, UNIT_DEREF(u->slice), true); | |
1133 | ||
1134 | if (streq(u->id, SPECIAL_ROOT_SLICE)) | |
1135 | return 0; | |
1136 | ||
1137 | return unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_WANTS, SPECIAL_ROOT_SLICE, NULL, true); | |
1138 | } | |
1139 | ||
1140 | static int unit_add_mount_dependencies(Unit *u) { | |
1141 | char **i; | |
1142 | int r; | |
1143 | ||
1144 | assert(u); | |
1145 | ||
1146 | STRV_FOREACH(i, u->requires_mounts_for) { | |
1147 | char prefix[strlen(*i) + 1]; | |
1148 | ||
1149 | PATH_FOREACH_PREFIX_MORE(prefix, *i) { | |
1150 | Unit *m; | |
1151 | ||
1152 | r = manager_get_unit_by_path(u->manager, prefix, ".mount", &m); | |
1153 | if (r < 0) | |
1154 | return r; | |
1155 | if (r == 0) | |
1156 | continue; | |
1157 | if (m == u) | |
1158 | continue; | |
1159 | ||
1160 | if (m->load_state != UNIT_LOADED) | |
1161 | continue; | |
1162 | ||
1163 | r = unit_add_dependency(u, UNIT_AFTER, m, true); | |
1164 | if (r < 0) | |
1165 | return r; | |
1166 | ||
1167 | if (m->fragment_path) { | |
1168 | r = unit_add_dependency(u, UNIT_REQUIRES, m, true); | |
1169 | if (r < 0) | |
1170 | return r; | |
1171 | } | |
1172 | } | |
1173 | } | |
1174 | ||
1175 | return 0; | |
1176 | } | |
1177 | ||
1178 | static int unit_add_startup_units(Unit *u) { | |
1179 | CGroupContext *c; | |
1180 | int r; | |
1181 | ||
1182 | c = unit_get_cgroup_context(u); | |
1183 | if (!c) | |
1184 | return 0; | |
1185 | ||
1186 | if (c->startup_cpu_shares == CGROUP_CPU_SHARES_INVALID && | |
1187 | c->startup_blockio_weight == CGROUP_BLKIO_WEIGHT_INVALID) | |
1188 | return 0; | |
1189 | ||
1190 | r = set_ensure_allocated(&u->manager->startup_units, NULL); | |
1191 | if (r < 0) | |
1192 | return r; | |
1193 | ||
1194 | return set_put(u->manager->startup_units, u); | |
1195 | } | |
1196 | ||
1197 | int unit_load(Unit *u) { | |
1198 | int r; | |
1199 | ||
1200 | assert(u); | |
1201 | ||
1202 | if (u->in_load_queue) { | |
1203 | LIST_REMOVE(load_queue, u->manager->load_queue, u); | |
1204 | u->in_load_queue = false; | |
1205 | } | |
1206 | ||
1207 | if (u->type == _UNIT_TYPE_INVALID) | |
1208 | return -EINVAL; | |
1209 | ||
1210 | if (u->load_state != UNIT_STUB) | |
1211 | return 0; | |
1212 | ||
1213 | if (UNIT_VTABLE(u)->load) { | |
1214 | r = UNIT_VTABLE(u)->load(u); | |
1215 | if (r < 0) | |
1216 | goto fail; | |
1217 | } | |
1218 | ||
1219 | if (u->load_state == UNIT_STUB) { | |
1220 | r = -ENOENT; | |
1221 | goto fail; | |
1222 | } | |
1223 | ||
1224 | if (u->load_state == UNIT_LOADED) { | |
1225 | ||
1226 | r = unit_add_target_dependencies(u); | |
1227 | if (r < 0) | |
1228 | goto fail; | |
1229 | ||
1230 | r = unit_add_slice_dependencies(u); | |
1231 | if (r < 0) | |
1232 | goto fail; | |
1233 | ||
1234 | r = unit_add_mount_dependencies(u); | |
1235 | if (r < 0) | |
1236 | goto fail; | |
1237 | ||
1238 | r = unit_add_startup_units(u); | |
1239 | if (r < 0) | |
1240 | goto fail; | |
1241 | ||
1242 | if (u->on_failure_job_mode == JOB_ISOLATE && set_size(u->dependencies[UNIT_ON_FAILURE]) > 1) { | |
1243 | log_unit_error(u, "More than one OnFailure= dependencies specified but OnFailureJobMode=isolate set. Refusing."); | |
1244 | r = -EINVAL; | |
1245 | goto fail; | |
1246 | } | |
1247 | ||
1248 | unit_update_cgroup_members_masks(u); | |
1249 | ||
1250 | /* If we are reloading, we need to wait for the deserializer | |
1251 | * to restore the net_cls ids that have been set previously */ | |
1252 | if (u->manager->n_reloading <= 0) { | |
1253 | r = unit_add_to_netclass_cgroup(u); | |
1254 | if (r < 0) | |
1255 | return r; | |
1256 | } | |
1257 | } | |
1258 | ||
1259 | assert((u->load_state != UNIT_MERGED) == !u->merged_into); | |
1260 | ||
1261 | unit_add_to_dbus_queue(unit_follow_merge(u)); | |
1262 | unit_add_to_gc_queue(u); | |
1263 | ||
1264 | return 0; | |
1265 | ||
1266 | fail: | |
1267 | u->load_state = u->load_state == UNIT_STUB ? UNIT_NOT_FOUND : UNIT_ERROR; | |
1268 | u->load_error = r; | |
1269 | unit_add_to_dbus_queue(u); | |
1270 | unit_add_to_gc_queue(u); | |
1271 | ||
1272 | log_unit_debug_errno(u, r, "Failed to load configuration: %m"); | |
1273 | ||
1274 | return r; | |
1275 | } | |
1276 | ||
1277 | static bool unit_condition_test_list(Unit *u, Condition *first, const char *(*to_string)(ConditionType t)) { | |
1278 | Condition *c; | |
1279 | int triggered = -1; | |
1280 | ||
1281 | assert(u); | |
1282 | assert(to_string); | |
1283 | ||
1284 | /* If the condition list is empty, then it is true */ | |
1285 | if (!first) | |
1286 | return true; | |
1287 | ||
1288 | /* Otherwise, if all of the non-trigger conditions apply and | |
1289 | * if any of the trigger conditions apply (unless there are | |
1290 | * none) we return true */ | |
1291 | LIST_FOREACH(conditions, c, first) { | |
1292 | int r; | |
1293 | ||
1294 | r = condition_test(c); | |
1295 | if (r < 0) | |
1296 | log_unit_warning(u, | |
1297 | "Couldn't determine result for %s=%s%s%s, assuming failed: %m", | |
1298 | to_string(c->type), | |
1299 | c->trigger ? "|" : "", | |
1300 | c->negate ? "!" : "", | |
1301 | c->parameter); | |
1302 | else | |
1303 | log_unit_debug(u, | |
1304 | "%s=%s%s%s %s.", | |
1305 | to_string(c->type), | |
1306 | c->trigger ? "|" : "", | |
1307 | c->negate ? "!" : "", | |
1308 | c->parameter, | |
1309 | condition_result_to_string(c->result)); | |
1310 | ||
1311 | if (!c->trigger && r <= 0) | |
1312 | return false; | |
1313 | ||
1314 | if (c->trigger && triggered <= 0) | |
1315 | triggered = r > 0; | |
1316 | } | |
1317 | ||
1318 | return triggered != 0; | |
1319 | } | |
1320 | ||
1321 | static bool unit_condition_test(Unit *u) { | |
1322 | assert(u); | |
1323 | ||
1324 | dual_timestamp_get(&u->condition_timestamp); | |
1325 | u->condition_result = unit_condition_test_list(u, u->conditions, condition_type_to_string); | |
1326 | ||
1327 | return u->condition_result; | |
1328 | } | |
1329 | ||
1330 | static bool unit_assert_test(Unit *u) { | |
1331 | assert(u); | |
1332 | ||
1333 | dual_timestamp_get(&u->assert_timestamp); | |
1334 | u->assert_result = unit_condition_test_list(u, u->asserts, assert_type_to_string); | |
1335 | ||
1336 | return u->assert_result; | |
1337 | } | |
1338 | ||
1339 | _pure_ static const char* unit_get_status_message_format(Unit *u, JobType t) { | |
1340 | const char *format; | |
1341 | const UnitStatusMessageFormats *format_table; | |
1342 | ||
1343 | assert(u); | |
1344 | assert(t == JOB_START || t == JOB_STOP || t == JOB_RELOAD); | |
1345 | ||
1346 | if (t != JOB_RELOAD) { | |
1347 | format_table = &UNIT_VTABLE(u)->status_message_formats; | |
1348 | if (format_table) { | |
1349 | format = format_table->starting_stopping[t == JOB_STOP]; | |
1350 | if (format) | |
1351 | return format; | |
1352 | } | |
1353 | } | |
1354 | ||
1355 | /* Return generic strings */ | |
1356 | if (t == JOB_START) | |
1357 | return "Starting %s."; | |
1358 | else if (t == JOB_STOP) | |
1359 | return "Stopping %s."; | |
1360 | else | |
1361 | return "Reloading %s."; | |
1362 | } | |
1363 | ||
1364 | static void unit_status_print_starting_stopping(Unit *u, JobType t) { | |
1365 | const char *format; | |
1366 | ||
1367 | assert(u); | |
1368 | ||
1369 | format = unit_get_status_message_format(u, t); | |
1370 | ||
1371 | DISABLE_WARNING_FORMAT_NONLITERAL; | |
1372 | unit_status_printf(u, "", format); | |
1373 | REENABLE_WARNING; | |
1374 | } | |
1375 | ||
1376 | static void unit_status_log_starting_stopping_reloading(Unit *u, JobType t) { | |
1377 | const char *format; | |
1378 | char buf[LINE_MAX]; | |
1379 | sd_id128_t mid; | |
1380 | ||
1381 | assert(u); | |
1382 | ||
1383 | if (t != JOB_START && t != JOB_STOP && t != JOB_RELOAD) | |
1384 | return; | |
1385 | ||
1386 | if (log_on_console()) | |
1387 | return; | |
1388 | ||
1389 | /* We log status messages for all units and all operations. */ | |
1390 | ||
1391 | format = unit_get_status_message_format(u, t); | |
1392 | ||
1393 | DISABLE_WARNING_FORMAT_NONLITERAL; | |
1394 | snprintf(buf, sizeof(buf), format, unit_description(u)); | |
1395 | REENABLE_WARNING; | |
1396 | ||
1397 | mid = t == JOB_START ? SD_MESSAGE_UNIT_STARTING : | |
1398 | t == JOB_STOP ? SD_MESSAGE_UNIT_STOPPING : | |
1399 | SD_MESSAGE_UNIT_RELOADING; | |
1400 | ||
1401 | /* Note that we deliberately use LOG_MESSAGE() instead of | |
1402 | * LOG_UNIT_MESSAGE() here, since this is supposed to mimic | |
1403 | * closely what is written to screen using the status output, | |
1404 | * which is supposed the highest level, friendliest output | |
1405 | * possible, which means we should avoid the low-level unit | |
1406 | * name. */ | |
1407 | log_struct(LOG_INFO, | |
1408 | LOG_MESSAGE_ID(mid), | |
1409 | LOG_UNIT_ID(u), | |
1410 | LOG_MESSAGE("%s", buf), | |
1411 | NULL); | |
1412 | } | |
1413 | ||
1414 | void unit_status_emit_starting_stopping_reloading(Unit *u, JobType t) { | |
1415 | ||
1416 | unit_status_log_starting_stopping_reloading(u, t); | |
1417 | ||
1418 | /* Reload status messages have traditionally not been printed to console. */ | |
1419 | if (t != JOB_RELOAD) | |
1420 | unit_status_print_starting_stopping(u, t); | |
1421 | } | |
1422 | ||
1423 | /* Errors: | |
1424 | * -EBADR: This unit type does not support starting. | |
1425 | * -EALREADY: Unit is already started. | |
1426 | * -EAGAIN: An operation is already in progress. Retry later. | |
1427 | * -ECANCELED: Too many requests for now. | |
1428 | * -EPROTO: Assert failed | |
1429 | */ | |
1430 | int unit_start(Unit *u) { | |
1431 | UnitActiveState state; | |
1432 | Unit *following; | |
1433 | ||
1434 | assert(u); | |
1435 | ||
1436 | /* Units that aren't loaded cannot be started */ | |
1437 | if (u->load_state != UNIT_LOADED) | |
1438 | return -EINVAL; | |
1439 | ||
1440 | /* If this is already started, then this will succeed. Note | |
1441 | * that this will even succeed if this unit is not startable | |
1442 | * by the user. This is relied on to detect when we need to | |
1443 | * wait for units and when waiting is finished. */ | |
1444 | state = unit_active_state(u); | |
1445 | if (UNIT_IS_ACTIVE_OR_RELOADING(state)) | |
1446 | return -EALREADY; | |
1447 | ||
1448 | /* If the conditions failed, don't do anything at all. If we | |
1449 | * already are activating this call might still be useful to | |
1450 | * speed up activation in case there is some hold-off time, | |
1451 | * but we don't want to recheck the condition in that case. */ | |
1452 | if (state != UNIT_ACTIVATING && | |
1453 | !unit_condition_test(u)) { | |
1454 | log_unit_debug(u, "Starting requested but condition failed. Not starting unit."); | |
1455 | return -EALREADY; | |
1456 | } | |
1457 | ||
1458 | /* If the asserts failed, fail the entire job */ | |
1459 | if (state != UNIT_ACTIVATING && | |
1460 | !unit_assert_test(u)) { | |
1461 | log_unit_notice(u, "Starting requested but asserts failed."); | |
1462 | return -EPROTO; | |
1463 | } | |
1464 | ||
1465 | /* Units of types that aren't supported cannot be | |
1466 | * started. Note that we do this test only after the condition | |
1467 | * checks, so that we rather return condition check errors | |
1468 | * (which are usually not considered a true failure) than "not | |
1469 | * supported" errors (which are considered a failure). | |
1470 | */ | |
1471 | if (!unit_supported(u)) | |
1472 | return -EOPNOTSUPP; | |
1473 | ||
1474 | /* Forward to the main object, if we aren't it. */ | |
1475 | following = unit_following(u); | |
1476 | if (following) { | |
1477 | log_unit_debug(u, "Redirecting start request from %s to %s.", u->id, following->id); | |
1478 | return unit_start(following); | |
1479 | } | |
1480 | ||
1481 | /* If it is stopped, but we cannot start it, then fail */ | |
1482 | if (!UNIT_VTABLE(u)->start) | |
1483 | return -EBADR; | |
1484 | ||
1485 | /* We don't suppress calls to ->start() here when we are | |
1486 | * already starting, to allow this request to be used as a | |
1487 | * "hurry up" call, for example when the unit is in some "auto | |
1488 | * restart" state where it waits for a holdoff timer to elapse | |
1489 | * before it will start again. */ | |
1490 | ||
1491 | unit_add_to_dbus_queue(u); | |
1492 | ||
1493 | return UNIT_VTABLE(u)->start(u); | |
1494 | } | |
1495 | ||
1496 | bool unit_can_start(Unit *u) { | |
1497 | assert(u); | |
1498 | ||
1499 | if (u->load_state != UNIT_LOADED) | |
1500 | return false; | |
1501 | ||
1502 | if (!unit_supported(u)) | |
1503 | return false; | |
1504 | ||
1505 | return !!UNIT_VTABLE(u)->start; | |
1506 | } | |
1507 | ||
1508 | bool unit_can_isolate(Unit *u) { | |
1509 | assert(u); | |
1510 | ||
1511 | return unit_can_start(u) && | |
1512 | u->allow_isolate; | |
1513 | } | |
1514 | ||
1515 | /* Errors: | |
1516 | * -EBADR: This unit type does not support stopping. | |
1517 | * -EALREADY: Unit is already stopped. | |
1518 | * -EAGAIN: An operation is already in progress. Retry later. | |
1519 | */ | |
1520 | int unit_stop(Unit *u) { | |
1521 | UnitActiveState state; | |
1522 | Unit *following; | |
1523 | ||
1524 | assert(u); | |
1525 | ||
1526 | state = unit_active_state(u); | |
1527 | if (UNIT_IS_INACTIVE_OR_FAILED(state)) | |
1528 | return -EALREADY; | |
1529 | ||
1530 | following = unit_following(u); | |
1531 | if (following) { | |
1532 | log_unit_debug(u, "Redirecting stop request from %s to %s.", u->id, following->id); | |
1533 | return unit_stop(following); | |
1534 | } | |
1535 | ||
1536 | if (!UNIT_VTABLE(u)->stop) | |
1537 | return -EBADR; | |
1538 | ||
1539 | unit_add_to_dbus_queue(u); | |
1540 | ||
1541 | return UNIT_VTABLE(u)->stop(u); | |
1542 | } | |
1543 | ||
1544 | /* Errors: | |
1545 | * -EBADR: This unit type does not support reloading. | |
1546 | * -ENOEXEC: Unit is not started. | |
1547 | * -EAGAIN: An operation is already in progress. Retry later. | |
1548 | */ | |
1549 | int unit_reload(Unit *u) { | |
1550 | UnitActiveState state; | |
1551 | Unit *following; | |
1552 | ||
1553 | assert(u); | |
1554 | ||
1555 | if (u->load_state != UNIT_LOADED) | |
1556 | return -EINVAL; | |
1557 | ||
1558 | if (!unit_can_reload(u)) | |
1559 | return -EBADR; | |
1560 | ||
1561 | state = unit_active_state(u); | |
1562 | if (state == UNIT_RELOADING) | |
1563 | return -EALREADY; | |
1564 | ||
1565 | if (state != UNIT_ACTIVE) { | |
1566 | log_unit_warning(u, "Unit cannot be reloaded because it is inactive."); | |
1567 | return -ENOEXEC; | |
1568 | } | |
1569 | ||
1570 | following = unit_following(u); | |
1571 | if (following) { | |
1572 | log_unit_debug(u, "Redirecting reload request from %s to %s.", u->id, following->id); | |
1573 | return unit_reload(following); | |
1574 | } | |
1575 | ||
1576 | unit_add_to_dbus_queue(u); | |
1577 | ||
1578 | return UNIT_VTABLE(u)->reload(u); | |
1579 | } | |
1580 | ||
1581 | bool unit_can_reload(Unit *u) { | |
1582 | assert(u); | |
1583 | ||
1584 | if (!UNIT_VTABLE(u)->reload) | |
1585 | return false; | |
1586 | ||
1587 | if (!UNIT_VTABLE(u)->can_reload) | |
1588 | return true; | |
1589 | ||
1590 | return UNIT_VTABLE(u)->can_reload(u); | |
1591 | } | |
1592 | ||
1593 | static void unit_check_unneeded(Unit *u) { | |
1594 | ||
1595 | static const UnitDependency needed_dependencies[] = { | |
1596 | UNIT_REQUIRED_BY, | |
1597 | UNIT_REQUIRED_BY_OVERRIDABLE, | |
1598 | UNIT_REQUISITE_OF, | |
1599 | UNIT_REQUISITE_OF_OVERRIDABLE, | |
1600 | UNIT_WANTED_BY, | |
1601 | UNIT_BOUND_BY, | |
1602 | }; | |
1603 | ||
1604 | Unit *other; | |
1605 | Iterator i; | |
1606 | unsigned j; | |
1607 | int r; | |
1608 | ||
1609 | assert(u); | |
1610 | ||
1611 | /* If this service shall be shut down when unneeded then do | |
1612 | * so. */ | |
1613 | ||
1614 | if (!u->stop_when_unneeded) | |
1615 | return; | |
1616 | ||
1617 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) | |
1618 | return; | |
1619 | ||
1620 | for (j = 0; j < ELEMENTSOF(needed_dependencies); j++) | |
1621 | SET_FOREACH(other, u->dependencies[needed_dependencies[j]], i) | |
1622 | if (unit_active_or_pending(other)) | |
1623 | return; | |
1624 | ||
1625 | /* If stopping a unit fails continously we might enter a stop | |
1626 | * loop here, hence stop acting on the service being | |
1627 | * unnecessary after a while. */ | |
1628 | if (!ratelimit_test(&u->auto_stop_ratelimit)) { | |
1629 | log_unit_warning(u, "Unit not needed anymore, but not stopping since we tried this too often recently."); | |
1630 | return; | |
1631 | } | |
1632 | ||
1633 | log_unit_info(u, "Unit not needed anymore. Stopping."); | |
1634 | ||
1635 | /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */ | |
1636 | r = manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, true, NULL, NULL); | |
1637 | if (r < 0) | |
1638 | log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %m"); | |
1639 | } | |
1640 | ||
1641 | static void unit_check_binds_to(Unit *u) { | |
1642 | bool stop = false; | |
1643 | Unit *other; | |
1644 | Iterator i; | |
1645 | int r; | |
1646 | ||
1647 | assert(u); | |
1648 | ||
1649 | if (u->job) | |
1650 | return; | |
1651 | ||
1652 | if (unit_active_state(u) != UNIT_ACTIVE) | |
1653 | return; | |
1654 | ||
1655 | SET_FOREACH(other, u->dependencies[UNIT_BINDS_TO], i) { | |
1656 | if (other->job) | |
1657 | continue; | |
1658 | ||
1659 | if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) | |
1660 | continue; | |
1661 | ||
1662 | stop = true; | |
1663 | break; | |
1664 | } | |
1665 | ||
1666 | if (!stop) | |
1667 | return; | |
1668 | ||
1669 | /* If stopping a unit fails continously we might enter a stop | |
1670 | * loop here, hence stop acting on the service being | |
1671 | * unnecessary after a while. */ | |
1672 | if (!ratelimit_test(&u->auto_stop_ratelimit)) { | |
1673 | log_unit_warning(u, "Unit is bound to inactive unit %s, but not stopping since we tried this too often recently.", other->id); | |
1674 | return; | |
1675 | } | |
1676 | ||
1677 | assert(other); | |
1678 | log_unit_info(u, "Unit is bound to inactive unit %s. Stopping, too.", other->id); | |
1679 | ||
1680 | /* A unit we need to run is gone. Sniff. Let's stop this. */ | |
1681 | r = manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, true, NULL, NULL); | |
1682 | if (r < 0) | |
1683 | log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %m"); | |
1684 | } | |
1685 | ||
1686 | static void retroactively_start_dependencies(Unit *u) { | |
1687 | Iterator i; | |
1688 | Unit *other; | |
1689 | ||
1690 | assert(u); | |
1691 | assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))); | |
1692 | ||
1693 | SET_FOREACH(other, u->dependencies[UNIT_REQUIRES], i) | |
1694 | if (!set_get(u->dependencies[UNIT_AFTER], other) && | |
1695 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
1696 | manager_add_job(u->manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL); | |
1697 | ||
1698 | SET_FOREACH(other, u->dependencies[UNIT_BINDS_TO], i) | |
1699 | if (!set_get(u->dependencies[UNIT_AFTER], other) && | |
1700 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
1701 | manager_add_job(u->manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL); | |
1702 | ||
1703 | SET_FOREACH(other, u->dependencies[UNIT_REQUIRES_OVERRIDABLE], i) | |
1704 | if (!set_get(u->dependencies[UNIT_AFTER], other) && | |
1705 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
1706 | manager_add_job(u->manager, JOB_START, other, JOB_FAIL, false, NULL, NULL); | |
1707 | ||
1708 | SET_FOREACH(other, u->dependencies[UNIT_WANTS], i) | |
1709 | if (!set_get(u->dependencies[UNIT_AFTER], other) && | |
1710 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
1711 | manager_add_job(u->manager, JOB_START, other, JOB_FAIL, false, NULL, NULL); | |
1712 | ||
1713 | SET_FOREACH(other, u->dependencies[UNIT_CONFLICTS], i) | |
1714 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
1715 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL); | |
1716 | ||
1717 | SET_FOREACH(other, u->dependencies[UNIT_CONFLICTED_BY], i) | |
1718 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
1719 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL); | |
1720 | } | |
1721 | ||
1722 | static void retroactively_stop_dependencies(Unit *u) { | |
1723 | Iterator i; | |
1724 | Unit *other; | |
1725 | ||
1726 | assert(u); | |
1727 | assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))); | |
1728 | ||
1729 | /* Pull down units which are bound to us recursively if enabled */ | |
1730 | SET_FOREACH(other, u->dependencies[UNIT_BOUND_BY], i) | |
1731 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
1732 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL); | |
1733 | } | |
1734 | ||
1735 | static void check_unneeded_dependencies(Unit *u) { | |
1736 | Iterator i; | |
1737 | Unit *other; | |
1738 | ||
1739 | assert(u); | |
1740 | assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))); | |
1741 | ||
1742 | /* Garbage collect services that might not be needed anymore, if enabled */ | |
1743 | SET_FOREACH(other, u->dependencies[UNIT_REQUIRES], i) | |
1744 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
1745 | unit_check_unneeded(other); | |
1746 | SET_FOREACH(other, u->dependencies[UNIT_REQUIRES_OVERRIDABLE], i) | |
1747 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
1748 | unit_check_unneeded(other); | |
1749 | SET_FOREACH(other, u->dependencies[UNIT_WANTS], i) | |
1750 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
1751 | unit_check_unneeded(other); | |
1752 | SET_FOREACH(other, u->dependencies[UNIT_REQUISITE], i) | |
1753 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
1754 | unit_check_unneeded(other); | |
1755 | SET_FOREACH(other, u->dependencies[UNIT_REQUISITE_OVERRIDABLE], i) | |
1756 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
1757 | unit_check_unneeded(other); | |
1758 | SET_FOREACH(other, u->dependencies[UNIT_BINDS_TO], i) | |
1759 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
1760 | unit_check_unneeded(other); | |
1761 | } | |
1762 | ||
1763 | void unit_start_on_failure(Unit *u) { | |
1764 | Unit *other; | |
1765 | Iterator i; | |
1766 | ||
1767 | assert(u); | |
1768 | ||
1769 | if (set_size(u->dependencies[UNIT_ON_FAILURE]) <= 0) | |
1770 | return; | |
1771 | ||
1772 | log_unit_info(u, "Triggering OnFailure= dependencies."); | |
1773 | ||
1774 | SET_FOREACH(other, u->dependencies[UNIT_ON_FAILURE], i) { | |
1775 | int r; | |
1776 | ||
1777 | r = manager_add_job(u->manager, JOB_START, other, u->on_failure_job_mode, true, NULL, NULL); | |
1778 | if (r < 0) | |
1779 | log_unit_error_errno(u, r, "Failed to enqueue OnFailure= job: %m"); | |
1780 | } | |
1781 | } | |
1782 | ||
1783 | void unit_trigger_notify(Unit *u) { | |
1784 | Unit *other; | |
1785 | Iterator i; | |
1786 | ||
1787 | assert(u); | |
1788 | ||
1789 | SET_FOREACH(other, u->dependencies[UNIT_TRIGGERED_BY], i) | |
1790 | if (UNIT_VTABLE(other)->trigger_notify) | |
1791 | UNIT_VTABLE(other)->trigger_notify(other, u); | |
1792 | } | |
1793 | ||
1794 | void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_success) { | |
1795 | Manager *m; | |
1796 | bool unexpected; | |
1797 | ||
1798 | assert(u); | |
1799 | assert(os < _UNIT_ACTIVE_STATE_MAX); | |
1800 | assert(ns < _UNIT_ACTIVE_STATE_MAX); | |
1801 | ||
1802 | /* Note that this is called for all low-level state changes, | |
1803 | * even if they might map to the same high-level | |
1804 | * UnitActiveState! That means that ns == os is an expected | |
1805 | * behavior here. For example: if a mount point is remounted | |
1806 | * this function will be called too! */ | |
1807 | ||
1808 | m = u->manager; | |
1809 | ||
1810 | /* Update timestamps for state changes */ | |
1811 | if (m->n_reloading <= 0) { | |
1812 | dual_timestamp ts; | |
1813 | ||
1814 | dual_timestamp_get(&ts); | |
1815 | ||
1816 | if (UNIT_IS_INACTIVE_OR_FAILED(os) && !UNIT_IS_INACTIVE_OR_FAILED(ns)) | |
1817 | u->inactive_exit_timestamp = ts; | |
1818 | else if (!UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_INACTIVE_OR_FAILED(ns)) | |
1819 | u->inactive_enter_timestamp = ts; | |
1820 | ||
1821 | if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
1822 | u->active_enter_timestamp = ts; | |
1823 | else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
1824 | u->active_exit_timestamp = ts; | |
1825 | } | |
1826 | ||
1827 | /* Keep track of failed units */ | |
1828 | (void) manager_update_failed_units(u->manager, u, ns == UNIT_FAILED); | |
1829 | ||
1830 | /* Make sure the cgroup is always removed when we become inactive */ | |
1831 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) | |
1832 | unit_prune_cgroup(u); | |
1833 | ||
1834 | /* Note that this doesn't apply to RemainAfterExit services exiting | |
1835 | * successfully, since there's no change of state in that case. Which is | |
1836 | * why it is handled in service_set_state() */ | |
1837 | if (UNIT_IS_INACTIVE_OR_FAILED(os) != UNIT_IS_INACTIVE_OR_FAILED(ns)) { | |
1838 | ExecContext *ec; | |
1839 | ||
1840 | ec = unit_get_exec_context(u); | |
1841 | if (ec && exec_context_may_touch_console(ec)) { | |
1842 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) { | |
1843 | m->n_on_console --; | |
1844 | ||
1845 | if (m->n_on_console == 0) | |
1846 | /* unset no_console_output flag, since the console is free */ | |
1847 | m->no_console_output = false; | |
1848 | } else | |
1849 | m->n_on_console ++; | |
1850 | } | |
1851 | } | |
1852 | ||
1853 | if (u->job) { | |
1854 | unexpected = false; | |
1855 | ||
1856 | if (u->job->state == JOB_WAITING) | |
1857 | ||
1858 | /* So we reached a different state for this | |
1859 | * job. Let's see if we can run it now if it | |
1860 | * failed previously due to EAGAIN. */ | |
1861 | job_add_to_run_queue(u->job); | |
1862 | ||
1863 | /* Let's check whether this state change constitutes a | |
1864 | * finished job, or maybe contradicts a running job and | |
1865 | * hence needs to invalidate jobs. */ | |
1866 | ||
1867 | switch (u->job->type) { | |
1868 | ||
1869 | case JOB_START: | |
1870 | case JOB_VERIFY_ACTIVE: | |
1871 | ||
1872 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
1873 | job_finish_and_invalidate(u->job, JOB_DONE, true); | |
1874 | else if (u->job->state == JOB_RUNNING && ns != UNIT_ACTIVATING) { | |
1875 | unexpected = true; | |
1876 | ||
1877 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) | |
1878 | job_finish_and_invalidate(u->job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true); | |
1879 | } | |
1880 | ||
1881 | break; | |
1882 | ||
1883 | case JOB_RELOAD: | |
1884 | case JOB_RELOAD_OR_START: | |
1885 | ||
1886 | if (u->job->state == JOB_RUNNING) { | |
1887 | if (ns == UNIT_ACTIVE) | |
1888 | job_finish_and_invalidate(u->job, reload_success ? JOB_DONE : JOB_FAILED, true); | |
1889 | else if (ns != UNIT_ACTIVATING && ns != UNIT_RELOADING) { | |
1890 | unexpected = true; | |
1891 | ||
1892 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) | |
1893 | job_finish_and_invalidate(u->job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true); | |
1894 | } | |
1895 | } | |
1896 | ||
1897 | break; | |
1898 | ||
1899 | case JOB_STOP: | |
1900 | case JOB_RESTART: | |
1901 | case JOB_TRY_RESTART: | |
1902 | ||
1903 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) | |
1904 | job_finish_and_invalidate(u->job, JOB_DONE, true); | |
1905 | else if (u->job->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) { | |
1906 | unexpected = true; | |
1907 | job_finish_and_invalidate(u->job, JOB_FAILED, true); | |
1908 | } | |
1909 | ||
1910 | break; | |
1911 | ||
1912 | default: | |
1913 | assert_not_reached("Job type unknown"); | |
1914 | } | |
1915 | ||
1916 | } else | |
1917 | unexpected = true; | |
1918 | ||
1919 | if (m->n_reloading <= 0) { | |
1920 | ||
1921 | /* If this state change happened without being | |
1922 | * requested by a job, then let's retroactively start | |
1923 | * or stop dependencies. We skip that step when | |
1924 | * deserializing, since we don't want to create any | |
1925 | * additional jobs just because something is already | |
1926 | * activated. */ | |
1927 | ||
1928 | if (unexpected) { | |
1929 | if (UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns)) | |
1930 | retroactively_start_dependencies(u); | |
1931 | else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns)) | |
1932 | retroactively_stop_dependencies(u); | |
1933 | } | |
1934 | ||
1935 | /* stop unneeded units regardless if going down was expected or not */ | |
1936 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(ns)) | |
1937 | check_unneeded_dependencies(u); | |
1938 | ||
1939 | if (ns != os && ns == UNIT_FAILED) { | |
1940 | log_unit_notice(u, "Unit entered failed state."); | |
1941 | unit_start_on_failure(u); | |
1942 | } | |
1943 | } | |
1944 | ||
1945 | /* Some names are special */ | |
1946 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) { | |
1947 | ||
1948 | if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) | |
1949 | /* The bus might have just become available, | |
1950 | * hence try to connect to it, if we aren't | |
1951 | * yet connected. */ | |
1952 | bus_init(m, true); | |
1953 | ||
1954 | if (u->type == UNIT_SERVICE && | |
1955 | !UNIT_IS_ACTIVE_OR_RELOADING(os) && | |
1956 | m->n_reloading <= 0) { | |
1957 | /* Write audit record if we have just finished starting up */ | |
1958 | manager_send_unit_audit(m, u, AUDIT_SERVICE_START, true); | |
1959 | u->in_audit = true; | |
1960 | } | |
1961 | ||
1962 | if (!UNIT_IS_ACTIVE_OR_RELOADING(os)) | |
1963 | manager_send_unit_plymouth(m, u); | |
1964 | ||
1965 | } else { | |
1966 | ||
1967 | /* We don't care about D-Bus here, since we'll get an | |
1968 | * asynchronous notification for it anyway. */ | |
1969 | ||
1970 | if (u->type == UNIT_SERVICE && | |
1971 | UNIT_IS_INACTIVE_OR_FAILED(ns) && | |
1972 | !UNIT_IS_INACTIVE_OR_FAILED(os) && | |
1973 | m->n_reloading <= 0) { | |
1974 | ||
1975 | /* Hmm, if there was no start record written | |
1976 | * write it now, so that we always have a nice | |
1977 | * pair */ | |
1978 | if (!u->in_audit) { | |
1979 | manager_send_unit_audit(m, u, AUDIT_SERVICE_START, ns == UNIT_INACTIVE); | |
1980 | ||
1981 | if (ns == UNIT_INACTIVE) | |
1982 | manager_send_unit_audit(m, u, AUDIT_SERVICE_STOP, true); | |
1983 | } else | |
1984 | /* Write audit record if we have just finished shutting down */ | |
1985 | manager_send_unit_audit(m, u, AUDIT_SERVICE_STOP, ns == UNIT_INACTIVE); | |
1986 | ||
1987 | u->in_audit = false; | |
1988 | } | |
1989 | } | |
1990 | ||
1991 | manager_recheck_journal(m); | |
1992 | unit_trigger_notify(u); | |
1993 | ||
1994 | if (u->manager->n_reloading <= 0) { | |
1995 | /* Maybe we finished startup and are now ready for | |
1996 | * being stopped because unneeded? */ | |
1997 | unit_check_unneeded(u); | |
1998 | ||
1999 | /* Maybe we finished startup, but something we needed | |
2000 | * has vanished? Let's die then. (This happens when | |
2001 | * something BindsTo= to a Type=oneshot unit, as these | |
2002 | * units go directly from starting to inactive, | |
2003 | * without ever entering started.) */ | |
2004 | unit_check_binds_to(u); | |
2005 | } | |
2006 | ||
2007 | unit_add_to_dbus_queue(u); | |
2008 | unit_add_to_gc_queue(u); | |
2009 | } | |
2010 | ||
2011 | int unit_watch_pid(Unit *u, pid_t pid) { | |
2012 | int q, r; | |
2013 | ||
2014 | assert(u); | |
2015 | assert(pid >= 1); | |
2016 | ||
2017 | /* Watch a specific PID. We only support one or two units | |
2018 | * watching each PID for now, not more. */ | |
2019 | ||
2020 | r = set_ensure_allocated(&u->pids, NULL); | |
2021 | if (r < 0) | |
2022 | return r; | |
2023 | ||
2024 | r = hashmap_ensure_allocated(&u->manager->watch_pids1, NULL); | |
2025 | if (r < 0) | |
2026 | return r; | |
2027 | ||
2028 | r = hashmap_put(u->manager->watch_pids1, PID_TO_PTR(pid), u); | |
2029 | if (r == -EEXIST) { | |
2030 | r = hashmap_ensure_allocated(&u->manager->watch_pids2, NULL); | |
2031 | if (r < 0) | |
2032 | return r; | |
2033 | ||
2034 | r = hashmap_put(u->manager->watch_pids2, PID_TO_PTR(pid), u); | |
2035 | } | |
2036 | ||
2037 | q = set_put(u->pids, PID_TO_PTR(pid)); | |
2038 | if (q < 0) | |
2039 | return q; | |
2040 | ||
2041 | return r; | |
2042 | } | |
2043 | ||
2044 | void unit_unwatch_pid(Unit *u, pid_t pid) { | |
2045 | assert(u); | |
2046 | assert(pid >= 1); | |
2047 | ||
2048 | (void) hashmap_remove_value(u->manager->watch_pids1, PID_TO_PTR(pid), u); | |
2049 | (void) hashmap_remove_value(u->manager->watch_pids2, PID_TO_PTR(pid), u); | |
2050 | (void) set_remove(u->pids, PID_TO_PTR(pid)); | |
2051 | } | |
2052 | ||
2053 | void unit_unwatch_all_pids(Unit *u) { | |
2054 | assert(u); | |
2055 | ||
2056 | while (!set_isempty(u->pids)) | |
2057 | unit_unwatch_pid(u, PTR_TO_PID(set_first(u->pids))); | |
2058 | ||
2059 | u->pids = set_free(u->pids); | |
2060 | } | |
2061 | ||
2062 | void unit_tidy_watch_pids(Unit *u, pid_t except1, pid_t except2) { | |
2063 | Iterator i; | |
2064 | void *e; | |
2065 | ||
2066 | assert(u); | |
2067 | ||
2068 | /* Cleans dead PIDs from our list */ | |
2069 | ||
2070 | SET_FOREACH(e, u->pids, i) { | |
2071 | pid_t pid = PTR_TO_PID(e); | |
2072 | ||
2073 | if (pid == except1 || pid == except2) | |
2074 | continue; | |
2075 | ||
2076 | if (!pid_is_unwaited(pid)) | |
2077 | unit_unwatch_pid(u, pid); | |
2078 | } | |
2079 | } | |
2080 | ||
2081 | bool unit_job_is_applicable(Unit *u, JobType j) { | |
2082 | assert(u); | |
2083 | assert(j >= 0 && j < _JOB_TYPE_MAX); | |
2084 | ||
2085 | switch (j) { | |
2086 | ||
2087 | case JOB_VERIFY_ACTIVE: | |
2088 | case JOB_START: | |
2089 | case JOB_STOP: | |
2090 | case JOB_NOP: | |
2091 | return true; | |
2092 | ||
2093 | case JOB_RESTART: | |
2094 | case JOB_TRY_RESTART: | |
2095 | return unit_can_start(u); | |
2096 | ||
2097 | case JOB_RELOAD: | |
2098 | return unit_can_reload(u); | |
2099 | ||
2100 | case JOB_RELOAD_OR_START: | |
2101 | return unit_can_reload(u) && unit_can_start(u); | |
2102 | ||
2103 | default: | |
2104 | assert_not_reached("Invalid job type"); | |
2105 | } | |
2106 | } | |
2107 | ||
2108 | static void maybe_warn_about_dependency(Unit *u, const char *other, UnitDependency dependency) { | |
2109 | assert(u); | |
2110 | ||
2111 | /* Only warn about some unit types */ | |
2112 | if (!IN_SET(dependency, UNIT_CONFLICTS, UNIT_CONFLICTED_BY, UNIT_BEFORE, UNIT_AFTER, UNIT_ON_FAILURE, UNIT_TRIGGERS, UNIT_TRIGGERED_BY)) | |
2113 | return; | |
2114 | ||
2115 | if (streq_ptr(u->id, other)) | |
2116 | log_unit_warning(u, "Dependency %s=%s dropped", unit_dependency_to_string(dependency), u->id); | |
2117 | else | |
2118 | log_unit_warning(u, "Dependency %s=%s dropped, merged into %s", unit_dependency_to_string(dependency), strna(other), u->id); | |
2119 | } | |
2120 | ||
2121 | int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference) { | |
2122 | ||
2123 | static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = { | |
2124 | [UNIT_REQUIRES] = UNIT_REQUIRED_BY, | |
2125 | [UNIT_REQUIRES_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE, | |
2126 | [UNIT_WANTS] = UNIT_WANTED_BY, | |
2127 | [UNIT_REQUISITE] = UNIT_REQUISITE_OF, | |
2128 | [UNIT_REQUISITE_OVERRIDABLE] = UNIT_REQUISITE_OF_OVERRIDABLE, | |
2129 | [UNIT_BINDS_TO] = UNIT_BOUND_BY, | |
2130 | [UNIT_PART_OF] = UNIT_CONSISTS_OF, | |
2131 | [UNIT_REQUIRED_BY] = UNIT_REQUIRES, | |
2132 | [UNIT_REQUIRED_BY_OVERRIDABLE] = UNIT_REQUIRES_OVERRIDABLE, | |
2133 | [UNIT_REQUISITE_OF] = UNIT_REQUISITE, | |
2134 | [UNIT_REQUISITE_OF_OVERRIDABLE] = UNIT_REQUISITE_OVERRIDABLE, | |
2135 | [UNIT_WANTED_BY] = UNIT_WANTS, | |
2136 | [UNIT_BOUND_BY] = UNIT_BINDS_TO, | |
2137 | [UNIT_CONSISTS_OF] = UNIT_PART_OF, | |
2138 | [UNIT_CONFLICTS] = UNIT_CONFLICTED_BY, | |
2139 | [UNIT_CONFLICTED_BY] = UNIT_CONFLICTS, | |
2140 | [UNIT_BEFORE] = UNIT_AFTER, | |
2141 | [UNIT_AFTER] = UNIT_BEFORE, | |
2142 | [UNIT_ON_FAILURE] = _UNIT_DEPENDENCY_INVALID, | |
2143 | [UNIT_REFERENCES] = UNIT_REFERENCED_BY, | |
2144 | [UNIT_REFERENCED_BY] = UNIT_REFERENCES, | |
2145 | [UNIT_TRIGGERS] = UNIT_TRIGGERED_BY, | |
2146 | [UNIT_TRIGGERED_BY] = UNIT_TRIGGERS, | |
2147 | [UNIT_PROPAGATES_RELOAD_TO] = UNIT_RELOAD_PROPAGATED_FROM, | |
2148 | [UNIT_RELOAD_PROPAGATED_FROM] = UNIT_PROPAGATES_RELOAD_TO, | |
2149 | [UNIT_JOINS_NAMESPACE_OF] = UNIT_JOINS_NAMESPACE_OF, | |
2150 | }; | |
2151 | int r, q = 0, v = 0, w = 0; | |
2152 | Unit *orig_u = u, *orig_other = other; | |
2153 | ||
2154 | assert(u); | |
2155 | assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX); | |
2156 | assert(other); | |
2157 | ||
2158 | u = unit_follow_merge(u); | |
2159 | other = unit_follow_merge(other); | |
2160 | ||
2161 | /* We won't allow dependencies on ourselves. We will not | |
2162 | * consider them an error however. */ | |
2163 | if (u == other) { | |
2164 | maybe_warn_about_dependency(orig_u, orig_other->id, d); | |
2165 | return 0; | |
2166 | } | |
2167 | ||
2168 | r = set_ensure_allocated(&u->dependencies[d], NULL); | |
2169 | if (r < 0) | |
2170 | return r; | |
2171 | ||
2172 | if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID) { | |
2173 | r = set_ensure_allocated(&other->dependencies[inverse_table[d]], NULL); | |
2174 | if (r < 0) | |
2175 | return r; | |
2176 | } | |
2177 | ||
2178 | if (add_reference) { | |
2179 | r = set_ensure_allocated(&u->dependencies[UNIT_REFERENCES], NULL); | |
2180 | if (r < 0) | |
2181 | return r; | |
2182 | ||
2183 | r = set_ensure_allocated(&other->dependencies[UNIT_REFERENCED_BY], NULL); | |
2184 | if (r < 0) | |
2185 | return r; | |
2186 | } | |
2187 | ||
2188 | q = set_put(u->dependencies[d], other); | |
2189 | if (q < 0) | |
2190 | return q; | |
2191 | ||
2192 | if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID && inverse_table[d] != d) { | |
2193 | v = set_put(other->dependencies[inverse_table[d]], u); | |
2194 | if (v < 0) { | |
2195 | r = v; | |
2196 | goto fail; | |
2197 | } | |
2198 | } | |
2199 | ||
2200 | if (add_reference) { | |
2201 | w = set_put(u->dependencies[UNIT_REFERENCES], other); | |
2202 | if (w < 0) { | |
2203 | r = w; | |
2204 | goto fail; | |
2205 | } | |
2206 | ||
2207 | r = set_put(other->dependencies[UNIT_REFERENCED_BY], u); | |
2208 | if (r < 0) | |
2209 | goto fail; | |
2210 | } | |
2211 | ||
2212 | unit_add_to_dbus_queue(u); | |
2213 | return 0; | |
2214 | ||
2215 | fail: | |
2216 | if (q > 0) | |
2217 | set_remove(u->dependencies[d], other); | |
2218 | ||
2219 | if (v > 0) | |
2220 | set_remove(other->dependencies[inverse_table[d]], u); | |
2221 | ||
2222 | if (w > 0) | |
2223 | set_remove(u->dependencies[UNIT_REFERENCES], other); | |
2224 | ||
2225 | return r; | |
2226 | } | |
2227 | ||
2228 | int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference) { | |
2229 | int r; | |
2230 | ||
2231 | assert(u); | |
2232 | ||
2233 | r = unit_add_dependency(u, d, other, add_reference); | |
2234 | if (r < 0) | |
2235 | return r; | |
2236 | ||
2237 | return unit_add_dependency(u, e, other, add_reference); | |
2238 | } | |
2239 | ||
2240 | static int resolve_template(Unit *u, const char *name, const char*path, char **buf, const char **ret) { | |
2241 | int r; | |
2242 | ||
2243 | assert(u); | |
2244 | assert(name || path); | |
2245 | assert(buf); | |
2246 | assert(ret); | |
2247 | ||
2248 | if (!name) | |
2249 | name = basename(path); | |
2250 | ||
2251 | if (!unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { | |
2252 | *buf = NULL; | |
2253 | *ret = name; | |
2254 | return 0; | |
2255 | } | |
2256 | ||
2257 | if (u->instance) | |
2258 | r = unit_name_replace_instance(name, u->instance, buf); | |
2259 | else { | |
2260 | _cleanup_free_ char *i = NULL; | |
2261 | ||
2262 | r = unit_name_to_prefix(u->id, &i); | |
2263 | if (r < 0) | |
2264 | return r; | |
2265 | ||
2266 | r = unit_name_replace_instance(name, i, buf); | |
2267 | } | |
2268 | if (r < 0) | |
2269 | return r; | |
2270 | ||
2271 | *ret = *buf; | |
2272 | return 0; | |
2273 | } | |
2274 | ||
2275 | int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) { | |
2276 | _cleanup_free_ char *buf = NULL; | |
2277 | Unit *other; | |
2278 | int r; | |
2279 | ||
2280 | assert(u); | |
2281 | assert(name || path); | |
2282 | ||
2283 | r = resolve_template(u, name, path, &buf, &name); | |
2284 | if (r < 0) | |
2285 | return r; | |
2286 | ||
2287 | r = manager_load_unit(u->manager, name, path, NULL, &other); | |
2288 | if (r < 0) | |
2289 | return r; | |
2290 | ||
2291 | return unit_add_dependency(u, d, other, add_reference); | |
2292 | } | |
2293 | ||
2294 | int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) { | |
2295 | _cleanup_free_ char *buf = NULL; | |
2296 | Unit *other; | |
2297 | int r; | |
2298 | ||
2299 | assert(u); | |
2300 | assert(name || path); | |
2301 | ||
2302 | r = resolve_template(u, name, path, &buf, &name); | |
2303 | if (r < 0) | |
2304 | return r; | |
2305 | ||
2306 | r = manager_load_unit(u->manager, name, path, NULL, &other); | |
2307 | if (r < 0) | |
2308 | return r; | |
2309 | ||
2310 | return unit_add_two_dependencies(u, d, e, other, add_reference); | |
2311 | } | |
2312 | ||
2313 | int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) { | |
2314 | _cleanup_free_ char *buf = NULL; | |
2315 | Unit *other; | |
2316 | int r; | |
2317 | ||
2318 | assert(u); | |
2319 | assert(name || path); | |
2320 | ||
2321 | r = resolve_template(u, name, path, &buf, &name); | |
2322 | if (r < 0) | |
2323 | return r; | |
2324 | ||
2325 | r = manager_load_unit(u->manager, name, path, NULL, &other); | |
2326 | if (r < 0) | |
2327 | return r; | |
2328 | ||
2329 | return unit_add_dependency(other, d, u, add_reference); | |
2330 | } | |
2331 | ||
2332 | int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) { | |
2333 | _cleanup_free_ char *buf = NULL; | |
2334 | Unit *other; | |
2335 | int r; | |
2336 | ||
2337 | assert(u); | |
2338 | assert(name || path); | |
2339 | ||
2340 | r = resolve_template(u, name, path, &buf, &name); | |
2341 | if (r < 0) | |
2342 | return r; | |
2343 | ||
2344 | r = manager_load_unit(u->manager, name, path, NULL, &other); | |
2345 | if (r < 0) | |
2346 | return r; | |
2347 | ||
2348 | return unit_add_two_dependencies(other, d, e, u, add_reference); | |
2349 | } | |
2350 | ||
2351 | int set_unit_path(const char *p) { | |
2352 | /* This is mostly for debug purposes */ | |
2353 | if (setenv("SYSTEMD_UNIT_PATH", p, 0) < 0) | |
2354 | return -errno; | |
2355 | ||
2356 | return 0; | |
2357 | } | |
2358 | ||
2359 | char *unit_dbus_path(Unit *u) { | |
2360 | assert(u); | |
2361 | ||
2362 | if (!u->id) | |
2363 | return NULL; | |
2364 | ||
2365 | return unit_dbus_path_from_name(u->id); | |
2366 | } | |
2367 | ||
2368 | int unit_set_slice(Unit *u, Unit *slice) { | |
2369 | assert(u); | |
2370 | assert(slice); | |
2371 | ||
2372 | /* Sets the unit slice if it has not been set before. Is extra | |
2373 | * careful, to only allow this for units that actually have a | |
2374 | * cgroup context. Also, we don't allow to set this for slices | |
2375 | * (since the parent slice is derived from the name). Make | |
2376 | * sure the unit we set is actually a slice. */ | |
2377 | ||
2378 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) | |
2379 | return -EOPNOTSUPP; | |
2380 | ||
2381 | if (u->type == UNIT_SLICE) | |
2382 | return -EINVAL; | |
2383 | ||
2384 | if (unit_active_state(u) != UNIT_INACTIVE) | |
2385 | return -EBUSY; | |
2386 | ||
2387 | if (slice->type != UNIT_SLICE) | |
2388 | return -EINVAL; | |
2389 | ||
2390 | if (unit_has_name(u, SPECIAL_INIT_SCOPE) && | |
2391 | !unit_has_name(slice, SPECIAL_ROOT_SLICE)) | |
2392 | return -EPERM; | |
2393 | ||
2394 | if (UNIT_DEREF(u->slice) == slice) | |
2395 | return 0; | |
2396 | ||
2397 | if (UNIT_ISSET(u->slice)) | |
2398 | return -EBUSY; | |
2399 | ||
2400 | unit_ref_set(&u->slice, slice); | |
2401 | return 1; | |
2402 | } | |
2403 | ||
2404 | int unit_set_default_slice(Unit *u) { | |
2405 | _cleanup_free_ char *b = NULL; | |
2406 | const char *slice_name; | |
2407 | Unit *slice; | |
2408 | int r; | |
2409 | ||
2410 | assert(u); | |
2411 | ||
2412 | if (UNIT_ISSET(u->slice)) | |
2413 | return 0; | |
2414 | ||
2415 | if (u->instance) { | |
2416 | _cleanup_free_ char *prefix = NULL, *escaped = NULL; | |
2417 | ||
2418 | /* Implicitly place all instantiated units in their | |
2419 | * own per-template slice */ | |
2420 | ||
2421 | r = unit_name_to_prefix(u->id, &prefix); | |
2422 | if (r < 0) | |
2423 | return r; | |
2424 | ||
2425 | /* The prefix is already escaped, but it might include | |
2426 | * "-" which has a special meaning for slice units, | |
2427 | * hence escape it here extra. */ | |
2428 | escaped = unit_name_escape(prefix); | |
2429 | if (!escaped) | |
2430 | return -ENOMEM; | |
2431 | ||
2432 | if (u->manager->running_as == MANAGER_SYSTEM) | |
2433 | b = strjoin("system-", escaped, ".slice", NULL); | |
2434 | else | |
2435 | b = strappend(escaped, ".slice"); | |
2436 | if (!b) | |
2437 | return -ENOMEM; | |
2438 | ||
2439 | slice_name = b; | |
2440 | } else | |
2441 | slice_name = | |
2442 | u->manager->running_as == MANAGER_SYSTEM && !unit_has_name(u, SPECIAL_INIT_SCOPE) | |
2443 | ? SPECIAL_SYSTEM_SLICE | |
2444 | : SPECIAL_ROOT_SLICE; | |
2445 | ||
2446 | r = manager_load_unit(u->manager, slice_name, NULL, NULL, &slice); | |
2447 | if (r < 0) | |
2448 | return r; | |
2449 | ||
2450 | return unit_set_slice(u, slice); | |
2451 | } | |
2452 | ||
2453 | const char *unit_slice_name(Unit *u) { | |
2454 | assert(u); | |
2455 | ||
2456 | if (!UNIT_ISSET(u->slice)) | |
2457 | return NULL; | |
2458 | ||
2459 | return UNIT_DEREF(u->slice)->id; | |
2460 | } | |
2461 | ||
2462 | int unit_load_related_unit(Unit *u, const char *type, Unit **_found) { | |
2463 | _cleanup_free_ char *t = NULL; | |
2464 | int r; | |
2465 | ||
2466 | assert(u); | |
2467 | assert(type); | |
2468 | assert(_found); | |
2469 | ||
2470 | r = unit_name_change_suffix(u->id, type, &t); | |
2471 | if (r < 0) | |
2472 | return r; | |
2473 | if (unit_has_name(u, t)) | |
2474 | return -EINVAL; | |
2475 | ||
2476 | r = manager_load_unit(u->manager, t, NULL, NULL, _found); | |
2477 | assert(r < 0 || *_found != u); | |
2478 | return r; | |
2479 | } | |
2480 | ||
2481 | static int signal_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2482 | const char *name, *old_owner, *new_owner; | |
2483 | Unit *u = userdata; | |
2484 | int r; | |
2485 | ||
2486 | assert(message); | |
2487 | assert(u); | |
2488 | ||
2489 | r = sd_bus_message_read(message, "sss", &name, &old_owner, &new_owner); | |
2490 | if (r < 0) { | |
2491 | bus_log_parse_error(r); | |
2492 | return 0; | |
2493 | } | |
2494 | ||
2495 | if (UNIT_VTABLE(u)->bus_name_owner_change) | |
2496 | UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner); | |
2497 | ||
2498 | return 0; | |
2499 | } | |
2500 | ||
2501 | int unit_install_bus_match(sd_bus *bus, Unit *u, const char *name) { | |
2502 | _cleanup_free_ char *match = NULL; | |
2503 | Manager *m = u->manager; | |
2504 | ||
2505 | assert(m); | |
2506 | ||
2507 | if (u->match_bus_slot) | |
2508 | return -EBUSY; | |
2509 | ||
2510 | match = strjoin("type='signal'," | |
2511 | "sender='org.freedesktop.DBus'," | |
2512 | "path='/org/freedesktop/DBus'," | |
2513 | "interface='org.freedesktop.DBus'," | |
2514 | "member='NameOwnerChanged'," | |
2515 | "arg0='", | |
2516 | name, | |
2517 | "'", | |
2518 | NULL); | |
2519 | if (!match) | |
2520 | return -ENOMEM; | |
2521 | ||
2522 | return sd_bus_add_match(bus, &u->match_bus_slot, match, signal_name_owner_changed, u); | |
2523 | } | |
2524 | ||
2525 | int unit_watch_bus_name(Unit *u, const char *name) { | |
2526 | int r; | |
2527 | ||
2528 | assert(u); | |
2529 | assert(name); | |
2530 | ||
2531 | /* Watch a specific name on the bus. We only support one unit | |
2532 | * watching each name for now. */ | |
2533 | ||
2534 | if (u->manager->api_bus) { | |
2535 | /* If the bus is already available, install the match directly. | |
2536 | * Otherwise, just put the name in the list. bus_setup_api() will take care later. */ | |
2537 | r = unit_install_bus_match(u->manager->api_bus, u, name); | |
2538 | if (r < 0) | |
2539 | return log_warning_errno(r, "Failed to subscribe to NameOwnerChanged signal: %m"); | |
2540 | } | |
2541 | ||
2542 | r = hashmap_put(u->manager->watch_bus, name, u); | |
2543 | if (r < 0) { | |
2544 | u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); | |
2545 | return log_warning_errno(r, "Failed to put bus name to hashmap: %m"); | |
2546 | } | |
2547 | ||
2548 | return 0; | |
2549 | } | |
2550 | ||
2551 | void unit_unwatch_bus_name(Unit *u, const char *name) { | |
2552 | assert(u); | |
2553 | assert(name); | |
2554 | ||
2555 | hashmap_remove_value(u->manager->watch_bus, name, u); | |
2556 | u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); | |
2557 | } | |
2558 | ||
2559 | bool unit_can_serialize(Unit *u) { | |
2560 | assert(u); | |
2561 | ||
2562 | return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item; | |
2563 | } | |
2564 | ||
2565 | int unit_serialize(Unit *u, FILE *f, FDSet *fds, bool serialize_jobs) { | |
2566 | int r; | |
2567 | ||
2568 | assert(u); | |
2569 | assert(f); | |
2570 | assert(fds); | |
2571 | ||
2572 | if (unit_can_serialize(u)) { | |
2573 | ExecRuntime *rt; | |
2574 | ||
2575 | r = UNIT_VTABLE(u)->serialize(u, f, fds); | |
2576 | if (r < 0) | |
2577 | return r; | |
2578 | ||
2579 | rt = unit_get_exec_runtime(u); | |
2580 | if (rt) { | |
2581 | r = exec_runtime_serialize(u, rt, f, fds); | |
2582 | if (r < 0) | |
2583 | return r; | |
2584 | } | |
2585 | } | |
2586 | ||
2587 | dual_timestamp_serialize(f, "inactive-exit-timestamp", &u->inactive_exit_timestamp); | |
2588 | dual_timestamp_serialize(f, "active-enter-timestamp", &u->active_enter_timestamp); | |
2589 | dual_timestamp_serialize(f, "active-exit-timestamp", &u->active_exit_timestamp); | |
2590 | dual_timestamp_serialize(f, "inactive-enter-timestamp", &u->inactive_enter_timestamp); | |
2591 | dual_timestamp_serialize(f, "condition-timestamp", &u->condition_timestamp); | |
2592 | dual_timestamp_serialize(f, "assert-timestamp", &u->assert_timestamp); | |
2593 | ||
2594 | if (dual_timestamp_is_set(&u->condition_timestamp)) | |
2595 | unit_serialize_item(u, f, "condition-result", yes_no(u->condition_result)); | |
2596 | ||
2597 | if (dual_timestamp_is_set(&u->assert_timestamp)) | |
2598 | unit_serialize_item(u, f, "assert-result", yes_no(u->assert_result)); | |
2599 | ||
2600 | unit_serialize_item(u, f, "transient", yes_no(u->transient)); | |
2601 | unit_serialize_item_format(u, f, "cpuacct-usage-base", "%" PRIu64, u->cpuacct_usage_base); | |
2602 | ||
2603 | if (u->cgroup_path) | |
2604 | unit_serialize_item(u, f, "cgroup", u->cgroup_path); | |
2605 | unit_serialize_item(u, f, "cgroup-realized", yes_no(u->cgroup_realized)); | |
2606 | ||
2607 | if (u->cgroup_netclass_id) | |
2608 | unit_serialize_item_format(u, f, "netclass-id", "%" PRIu32, u->cgroup_netclass_id); | |
2609 | ||
2610 | if (serialize_jobs) { | |
2611 | if (u->job) { | |
2612 | fprintf(f, "job\n"); | |
2613 | job_serialize(u->job, f, fds); | |
2614 | } | |
2615 | ||
2616 | if (u->nop_job) { | |
2617 | fprintf(f, "job\n"); | |
2618 | job_serialize(u->nop_job, f, fds); | |
2619 | } | |
2620 | } | |
2621 | ||
2622 | /* End marker */ | |
2623 | fputc('\n', f); | |
2624 | return 0; | |
2625 | } | |
2626 | ||
2627 | void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *format, ...) { | |
2628 | va_list ap; | |
2629 | ||
2630 | assert(u); | |
2631 | assert(f); | |
2632 | assert(key); | |
2633 | assert(format); | |
2634 | ||
2635 | fputs(key, f); | |
2636 | fputc('=', f); | |
2637 | ||
2638 | va_start(ap, format); | |
2639 | vfprintf(f, format, ap); | |
2640 | va_end(ap); | |
2641 | ||
2642 | fputc('\n', f); | |
2643 | } | |
2644 | ||
2645 | void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value) { | |
2646 | assert(u); | |
2647 | assert(f); | |
2648 | assert(key); | |
2649 | assert(value); | |
2650 | ||
2651 | fprintf(f, "%s=%s\n", key, value); | |
2652 | } | |
2653 | ||
2654 | int unit_deserialize(Unit *u, FILE *f, FDSet *fds) { | |
2655 | ExecRuntime **rt = NULL; | |
2656 | size_t offset; | |
2657 | int r; | |
2658 | ||
2659 | assert(u); | |
2660 | assert(f); | |
2661 | assert(fds); | |
2662 | ||
2663 | offset = UNIT_VTABLE(u)->exec_runtime_offset; | |
2664 | if (offset > 0) | |
2665 | rt = (ExecRuntime**) ((uint8_t*) u + offset); | |
2666 | ||
2667 | for (;;) { | |
2668 | char line[LINE_MAX], *l, *v; | |
2669 | size_t k; | |
2670 | ||
2671 | if (!fgets(line, sizeof(line), f)) { | |
2672 | if (feof(f)) | |
2673 | return 0; | |
2674 | return -errno; | |
2675 | } | |
2676 | ||
2677 | char_array_0(line); | |
2678 | l = strstrip(line); | |
2679 | ||
2680 | /* End marker */ | |
2681 | if (isempty(l)) | |
2682 | return 0; | |
2683 | ||
2684 | k = strcspn(l, "="); | |
2685 | ||
2686 | if (l[k] == '=') { | |
2687 | l[k] = 0; | |
2688 | v = l+k+1; | |
2689 | } else | |
2690 | v = l+k; | |
2691 | ||
2692 | if (streq(l, "job")) { | |
2693 | if (v[0] == '\0') { | |
2694 | /* new-style serialized job */ | |
2695 | Job *j; | |
2696 | ||
2697 | j = job_new_raw(u); | |
2698 | if (!j) | |
2699 | return log_oom(); | |
2700 | ||
2701 | r = job_deserialize(j, f, fds); | |
2702 | if (r < 0) { | |
2703 | job_free(j); | |
2704 | return r; | |
2705 | } | |
2706 | ||
2707 | r = hashmap_put(u->manager->jobs, UINT32_TO_PTR(j->id), j); | |
2708 | if (r < 0) { | |
2709 | job_free(j); | |
2710 | return r; | |
2711 | } | |
2712 | ||
2713 | r = job_install_deserialized(j); | |
2714 | if (r < 0) { | |
2715 | hashmap_remove(u->manager->jobs, UINT32_TO_PTR(j->id)); | |
2716 | job_free(j); | |
2717 | return r; | |
2718 | } | |
2719 | } else /* legacy for pre-44 */ | |
2720 | log_unit_warning(u, "Update from too old systemd versions are unsupported, cannot deserialize job: %s", v); | |
2721 | continue; | |
2722 | } else if (streq(l, "inactive-exit-timestamp")) { | |
2723 | dual_timestamp_deserialize(v, &u->inactive_exit_timestamp); | |
2724 | continue; | |
2725 | } else if (streq(l, "active-enter-timestamp")) { | |
2726 | dual_timestamp_deserialize(v, &u->active_enter_timestamp); | |
2727 | continue; | |
2728 | } else if (streq(l, "active-exit-timestamp")) { | |
2729 | dual_timestamp_deserialize(v, &u->active_exit_timestamp); | |
2730 | continue; | |
2731 | } else if (streq(l, "inactive-enter-timestamp")) { | |
2732 | dual_timestamp_deserialize(v, &u->inactive_enter_timestamp); | |
2733 | continue; | |
2734 | } else if (streq(l, "condition-timestamp")) { | |
2735 | dual_timestamp_deserialize(v, &u->condition_timestamp); | |
2736 | continue; | |
2737 | } else if (streq(l, "assert-timestamp")) { | |
2738 | dual_timestamp_deserialize(v, &u->assert_timestamp); | |
2739 | continue; | |
2740 | } else if (streq(l, "condition-result")) { | |
2741 | ||
2742 | r = parse_boolean(v); | |
2743 | if (r < 0) | |
2744 | log_unit_debug(u, "Failed to parse condition result value %s, ignoring.", v); | |
2745 | else | |
2746 | u->condition_result = r; | |
2747 | ||
2748 | continue; | |
2749 | ||
2750 | } else if (streq(l, "assert-result")) { | |
2751 | ||
2752 | r = parse_boolean(v); | |
2753 | if (r < 0) | |
2754 | log_unit_debug(u, "Failed to parse assert result value %s, ignoring.", v); | |
2755 | else | |
2756 | u->assert_result = r; | |
2757 | ||
2758 | continue; | |
2759 | ||
2760 | } else if (streq(l, "transient")) { | |
2761 | ||
2762 | r = parse_boolean(v); | |
2763 | if (r < 0) | |
2764 | log_unit_debug(u, "Failed to parse transient bool %s, ignoring.", v); | |
2765 | else | |
2766 | u->transient = r; | |
2767 | ||
2768 | continue; | |
2769 | ||
2770 | } else if (streq(l, "cpuacct-usage-base")) { | |
2771 | ||
2772 | r = safe_atou64(v, &u->cpuacct_usage_base); | |
2773 | if (r < 0) | |
2774 | log_unit_debug(u, "Failed to parse CPU usage %s, ignoring.", v); | |
2775 | ||
2776 | continue; | |
2777 | ||
2778 | } else if (streq(l, "cgroup")) { | |
2779 | ||
2780 | r = unit_set_cgroup_path(u, v); | |
2781 | if (r < 0) | |
2782 | log_unit_debug_errno(u, r, "Failed to set cgroup path %s, ignoring: %m", v); | |
2783 | ||
2784 | (void) unit_watch_cgroup(u); | |
2785 | ||
2786 | continue; | |
2787 | } else if (streq(l, "cgroup-realized")) { | |
2788 | int b; | |
2789 | ||
2790 | b = parse_boolean(v); | |
2791 | if (b < 0) | |
2792 | log_unit_debug(u, "Failed to parse cgroup-realized bool %s, ignoring.", v); | |
2793 | else | |
2794 | u->cgroup_realized = b; | |
2795 | ||
2796 | continue; | |
2797 | } else if (streq(l, "netclass-id")) { | |
2798 | r = safe_atou32(v, &u->cgroup_netclass_id); | |
2799 | if (r < 0) | |
2800 | log_unit_debug(u, "Failed to parse netclass ID %s, ignoring.", v); | |
2801 | else { | |
2802 | r = unit_add_to_netclass_cgroup(u); | |
2803 | if (r < 0) | |
2804 | log_unit_debug_errno(u, r, "Failed to add unit to netclass cgroup, ignoring: %m"); | |
2805 | } | |
2806 | ||
2807 | continue; | |
2808 | } | |
2809 | ||
2810 | if (unit_can_serialize(u)) { | |
2811 | if (rt) { | |
2812 | r = exec_runtime_deserialize_item(u, rt, l, v, fds); | |
2813 | if (r < 0) { | |
2814 | log_unit_warning(u, "Failed to deserialize runtime parameter '%s', ignoring.", l); | |
2815 | continue; | |
2816 | } | |
2817 | ||
2818 | /* Returns positive if key was handled by the call */ | |
2819 | if (r > 0) | |
2820 | continue; | |
2821 | } | |
2822 | ||
2823 | r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds); | |
2824 | if (r < 0) | |
2825 | log_unit_warning(u, "Failed to deserialize unit parameter '%s', ignoring.", l); | |
2826 | } | |
2827 | } | |
2828 | } | |
2829 | ||
2830 | int unit_add_node_link(Unit *u, const char *what, bool wants) { | |
2831 | Unit *device; | |
2832 | _cleanup_free_ char *e = NULL; | |
2833 | int r; | |
2834 | ||
2835 | assert(u); | |
2836 | ||
2837 | /* Adds in links to the device node that this unit is based on */ | |
2838 | if (isempty(what)) | |
2839 | return 0; | |
2840 | ||
2841 | if (!is_device_path(what)) | |
2842 | return 0; | |
2843 | ||
2844 | /* When device units aren't supported (such as in a | |
2845 | * container), don't create dependencies on them. */ | |
2846 | if (!unit_type_supported(UNIT_DEVICE)) | |
2847 | return 0; | |
2848 | ||
2849 | r = unit_name_from_path(what, ".device", &e); | |
2850 | if (r < 0) | |
2851 | return r; | |
2852 | ||
2853 | r = manager_load_unit(u->manager, e, NULL, NULL, &device); | |
2854 | if (r < 0) | |
2855 | return r; | |
2856 | ||
2857 | r = unit_add_two_dependencies(u, UNIT_AFTER, u->manager->running_as == MANAGER_SYSTEM ? UNIT_BINDS_TO : UNIT_WANTS, device, true); | |
2858 | if (r < 0) | |
2859 | return r; | |
2860 | ||
2861 | if (wants) { | |
2862 | r = unit_add_dependency(device, UNIT_WANTS, u, false); | |
2863 | if (r < 0) | |
2864 | return r; | |
2865 | } | |
2866 | ||
2867 | return 0; | |
2868 | } | |
2869 | ||
2870 | int unit_coldplug(Unit *u) { | |
2871 | int r; | |
2872 | ||
2873 | assert(u); | |
2874 | ||
2875 | /* Make sure we don't enter a loop, when coldplugging | |
2876 | * recursively. */ | |
2877 | if (u->coldplugged) | |
2878 | return 0; | |
2879 | ||
2880 | u->coldplugged = true; | |
2881 | ||
2882 | if (UNIT_VTABLE(u)->coldplug) { | |
2883 | r = UNIT_VTABLE(u)->coldplug(u); | |
2884 | if (r < 0) | |
2885 | return r; | |
2886 | } | |
2887 | ||
2888 | if (u->job) { | |
2889 | r = job_coldplug(u->job); | |
2890 | if (r < 0) | |
2891 | return r; | |
2892 | } | |
2893 | ||
2894 | return 0; | |
2895 | } | |
2896 | ||
2897 | void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg_format) { | |
2898 | DISABLE_WARNING_FORMAT_NONLITERAL; | |
2899 | manager_status_printf(u->manager, STATUS_TYPE_NORMAL, | |
2900 | status, unit_status_msg_format, unit_description(u)); | |
2901 | REENABLE_WARNING; | |
2902 | } | |
2903 | ||
2904 | bool unit_need_daemon_reload(Unit *u) { | |
2905 | _cleanup_strv_free_ char **t = NULL; | |
2906 | char **path; | |
2907 | struct stat st; | |
2908 | unsigned loaded_cnt, current_cnt; | |
2909 | ||
2910 | assert(u); | |
2911 | ||
2912 | if (u->fragment_path) { | |
2913 | zero(st); | |
2914 | if (stat(u->fragment_path, &st) < 0) | |
2915 | /* What, cannot access this anymore? */ | |
2916 | return true; | |
2917 | ||
2918 | if (u->fragment_mtime > 0 && | |
2919 | timespec_load(&st.st_mtim) != u->fragment_mtime) | |
2920 | return true; | |
2921 | } | |
2922 | ||
2923 | if (u->source_path) { | |
2924 | zero(st); | |
2925 | if (stat(u->source_path, &st) < 0) | |
2926 | return true; | |
2927 | ||
2928 | if (u->source_mtime > 0 && | |
2929 | timespec_load(&st.st_mtim) != u->source_mtime) | |
2930 | return true; | |
2931 | } | |
2932 | ||
2933 | (void) unit_find_dropin_paths(u, &t); | |
2934 | loaded_cnt = strv_length(t); | |
2935 | current_cnt = strv_length(u->dropin_paths); | |
2936 | ||
2937 | if (loaded_cnt == current_cnt) { | |
2938 | if (loaded_cnt == 0) | |
2939 | return false; | |
2940 | ||
2941 | if (strv_overlap(u->dropin_paths, t)) { | |
2942 | STRV_FOREACH(path, u->dropin_paths) { | |
2943 | zero(st); | |
2944 | if (stat(*path, &st) < 0) | |
2945 | return true; | |
2946 | ||
2947 | if (u->dropin_mtime > 0 && | |
2948 | timespec_load(&st.st_mtim) > u->dropin_mtime) | |
2949 | return true; | |
2950 | } | |
2951 | ||
2952 | return false; | |
2953 | } else | |
2954 | return true; | |
2955 | } else | |
2956 | return true; | |
2957 | } | |
2958 | ||
2959 | void unit_reset_failed(Unit *u) { | |
2960 | assert(u); | |
2961 | ||
2962 | if (UNIT_VTABLE(u)->reset_failed) | |
2963 | UNIT_VTABLE(u)->reset_failed(u); | |
2964 | } | |
2965 | ||
2966 | Unit *unit_following(Unit *u) { | |
2967 | assert(u); | |
2968 | ||
2969 | if (UNIT_VTABLE(u)->following) | |
2970 | return UNIT_VTABLE(u)->following(u); | |
2971 | ||
2972 | return NULL; | |
2973 | } | |
2974 | ||
2975 | bool unit_stop_pending(Unit *u) { | |
2976 | assert(u); | |
2977 | ||
2978 | /* This call does check the current state of the unit. It's | |
2979 | * hence useful to be called from state change calls of the | |
2980 | * unit itself, where the state isn't updated yet. This is | |
2981 | * different from unit_inactive_or_pending() which checks both | |
2982 | * the current state and for a queued job. */ | |
2983 | ||
2984 | return u->job && u->job->type == JOB_STOP; | |
2985 | } | |
2986 | ||
2987 | bool unit_inactive_or_pending(Unit *u) { | |
2988 | assert(u); | |
2989 | ||
2990 | /* Returns true if the unit is inactive or going down */ | |
2991 | ||
2992 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))) | |
2993 | return true; | |
2994 | ||
2995 | if (unit_stop_pending(u)) | |
2996 | return true; | |
2997 | ||
2998 | return false; | |
2999 | } | |
3000 | ||
3001 | bool unit_active_or_pending(Unit *u) { | |
3002 | assert(u); | |
3003 | ||
3004 | /* Returns true if the unit is active or going up */ | |
3005 | ||
3006 | if (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) | |
3007 | return true; | |
3008 | ||
3009 | if (u->job && | |
3010 | (u->job->type == JOB_START || | |
3011 | u->job->type == JOB_RELOAD_OR_START || | |
3012 | u->job->type == JOB_RESTART)) | |
3013 | return true; | |
3014 | ||
3015 | return false; | |
3016 | } | |
3017 | ||
3018 | int unit_kill(Unit *u, KillWho w, int signo, sd_bus_error *error) { | |
3019 | assert(u); | |
3020 | assert(w >= 0 && w < _KILL_WHO_MAX); | |
3021 | assert(signo > 0); | |
3022 | assert(signo < _NSIG); | |
3023 | ||
3024 | if (!UNIT_VTABLE(u)->kill) | |
3025 | return -EOPNOTSUPP; | |
3026 | ||
3027 | return UNIT_VTABLE(u)->kill(u, w, signo, error); | |
3028 | } | |
3029 | ||
3030 | static Set *unit_pid_set(pid_t main_pid, pid_t control_pid) { | |
3031 | Set *pid_set; | |
3032 | int r; | |
3033 | ||
3034 | pid_set = set_new(NULL); | |
3035 | if (!pid_set) | |
3036 | return NULL; | |
3037 | ||
3038 | /* Exclude the main/control pids from being killed via the cgroup */ | |
3039 | if (main_pid > 0) { | |
3040 | r = set_put(pid_set, PID_TO_PTR(main_pid)); | |
3041 | if (r < 0) | |
3042 | goto fail; | |
3043 | } | |
3044 | ||
3045 | if (control_pid > 0) { | |
3046 | r = set_put(pid_set, PID_TO_PTR(control_pid)); | |
3047 | if (r < 0) | |
3048 | goto fail; | |
3049 | } | |
3050 | ||
3051 | return pid_set; | |
3052 | ||
3053 | fail: | |
3054 | set_free(pid_set); | |
3055 | return NULL; | |
3056 | } | |
3057 | ||
3058 | int unit_kill_common( | |
3059 | Unit *u, | |
3060 | KillWho who, | |
3061 | int signo, | |
3062 | pid_t main_pid, | |
3063 | pid_t control_pid, | |
3064 | sd_bus_error *error) { | |
3065 | ||
3066 | int r = 0; | |
3067 | bool killed = false; | |
3068 | ||
3069 | if (IN_SET(who, KILL_MAIN, KILL_MAIN_FAIL)) { | |
3070 | if (main_pid < 0) | |
3071 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no main processes", unit_type_to_string(u->type)); | |
3072 | else if (main_pid == 0) | |
3073 | return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill"); | |
3074 | } | |
3075 | ||
3076 | if (IN_SET(who, KILL_CONTROL, KILL_CONTROL_FAIL)) { | |
3077 | if (control_pid < 0) | |
3078 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no control processes", unit_type_to_string(u->type)); | |
3079 | else if (control_pid == 0) | |
3080 | return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill"); | |
3081 | } | |
3082 | ||
3083 | if (IN_SET(who, KILL_CONTROL, KILL_CONTROL_FAIL, KILL_ALL, KILL_ALL_FAIL)) | |
3084 | if (control_pid > 0) { | |
3085 | if (kill(control_pid, signo) < 0) | |
3086 | r = -errno; | |
3087 | else | |
3088 | killed = true; | |
3089 | } | |
3090 | ||
3091 | if (IN_SET(who, KILL_MAIN, KILL_MAIN_FAIL, KILL_ALL, KILL_ALL_FAIL)) | |
3092 | if (main_pid > 0) { | |
3093 | if (kill(main_pid, signo) < 0) | |
3094 | r = -errno; | |
3095 | else | |
3096 | killed = true; | |
3097 | } | |
3098 | ||
3099 | if (IN_SET(who, KILL_ALL, KILL_ALL_FAIL) && u->cgroup_path) { | |
3100 | _cleanup_set_free_ Set *pid_set = NULL; | |
3101 | int q; | |
3102 | ||
3103 | /* Exclude the main/control pids from being killed via the cgroup */ | |
3104 | pid_set = unit_pid_set(main_pid, control_pid); | |
3105 | if (!pid_set) | |
3106 | return -ENOMEM; | |
3107 | ||
3108 | q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, signo, false, false, false, pid_set); | |
3109 | if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT) | |
3110 | r = q; | |
3111 | else | |
3112 | killed = true; | |
3113 | } | |
3114 | ||
3115 | if (r == 0 && !killed && IN_SET(who, KILL_ALL_FAIL, KILL_CONTROL_FAIL, KILL_ALL_FAIL)) | |
3116 | return -ESRCH; | |
3117 | ||
3118 | return r; | |
3119 | } | |
3120 | ||
3121 | int unit_following_set(Unit *u, Set **s) { | |
3122 | assert(u); | |
3123 | assert(s); | |
3124 | ||
3125 | if (UNIT_VTABLE(u)->following_set) | |
3126 | return UNIT_VTABLE(u)->following_set(u, s); | |
3127 | ||
3128 | *s = NULL; | |
3129 | return 0; | |
3130 | } | |
3131 | ||
3132 | UnitFileState unit_get_unit_file_state(Unit *u) { | |
3133 | assert(u); | |
3134 | ||
3135 | if (u->unit_file_state < 0 && u->fragment_path) | |
3136 | u->unit_file_state = unit_file_get_state( | |
3137 | u->manager->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, | |
3138 | NULL, basename(u->fragment_path)); | |
3139 | ||
3140 | return u->unit_file_state; | |
3141 | } | |
3142 | ||
3143 | int unit_get_unit_file_preset(Unit *u) { | |
3144 | assert(u); | |
3145 | ||
3146 | if (u->unit_file_preset < 0 && u->fragment_path) | |
3147 | u->unit_file_preset = unit_file_query_preset( | |
3148 | u->manager->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, | |
3149 | NULL, basename(u->fragment_path)); | |
3150 | ||
3151 | return u->unit_file_preset; | |
3152 | } | |
3153 | ||
3154 | Unit* unit_ref_set(UnitRef *ref, Unit *u) { | |
3155 | assert(ref); | |
3156 | assert(u); | |
3157 | ||
3158 | if (ref->unit) | |
3159 | unit_ref_unset(ref); | |
3160 | ||
3161 | ref->unit = u; | |
3162 | LIST_PREPEND(refs, u->refs, ref); | |
3163 | return u; | |
3164 | } | |
3165 | ||
3166 | void unit_ref_unset(UnitRef *ref) { | |
3167 | assert(ref); | |
3168 | ||
3169 | if (!ref->unit) | |
3170 | return; | |
3171 | ||
3172 | LIST_REMOVE(refs, ref->unit->refs, ref); | |
3173 | ref->unit = NULL; | |
3174 | } | |
3175 | ||
3176 | int unit_patch_contexts(Unit *u) { | |
3177 | CGroupContext *cc; | |
3178 | ExecContext *ec; | |
3179 | unsigned i; | |
3180 | int r; | |
3181 | ||
3182 | assert(u); | |
3183 | ||
3184 | /* Patch in the manager defaults into the exec and cgroup | |
3185 | * contexts, _after_ the rest of the settings have been | |
3186 | * initialized */ | |
3187 | ||
3188 | ec = unit_get_exec_context(u); | |
3189 | if (ec) { | |
3190 | /* This only copies in the ones that need memory */ | |
3191 | for (i = 0; i < _RLIMIT_MAX; i++) | |
3192 | if (u->manager->rlimit[i] && !ec->rlimit[i]) { | |
3193 | ec->rlimit[i] = newdup(struct rlimit, u->manager->rlimit[i], 1); | |
3194 | if (!ec->rlimit[i]) | |
3195 | return -ENOMEM; | |
3196 | } | |
3197 | ||
3198 | if (u->manager->running_as == MANAGER_USER && | |
3199 | !ec->working_directory) { | |
3200 | ||
3201 | r = get_home_dir(&ec->working_directory); | |
3202 | if (r < 0) | |
3203 | return r; | |
3204 | ||
3205 | /* Allow user services to run, even if the | |
3206 | * home directory is missing */ | |
3207 | ec->working_directory_missing_ok = true; | |
3208 | } | |
3209 | ||
3210 | if (u->manager->running_as == MANAGER_USER && | |
3211 | (ec->syscall_whitelist || | |
3212 | !set_isempty(ec->syscall_filter) || | |
3213 | !set_isempty(ec->syscall_archs) || | |
3214 | ec->address_families_whitelist || | |
3215 | !set_isempty(ec->address_families))) | |
3216 | ec->no_new_privileges = true; | |
3217 | ||
3218 | if (ec->private_devices) | |
3219 | ec->capability_bounding_set_drop |= (uint64_t) 1ULL << (uint64_t) CAP_MKNOD; | |
3220 | } | |
3221 | ||
3222 | cc = unit_get_cgroup_context(u); | |
3223 | if (cc) { | |
3224 | ||
3225 | if (ec && | |
3226 | ec->private_devices && | |
3227 | cc->device_policy == CGROUP_AUTO) | |
3228 | cc->device_policy = CGROUP_CLOSED; | |
3229 | } | |
3230 | ||
3231 | return 0; | |
3232 | } | |
3233 | ||
3234 | ExecContext *unit_get_exec_context(Unit *u) { | |
3235 | size_t offset; | |
3236 | assert(u); | |
3237 | ||
3238 | if (u->type < 0) | |
3239 | return NULL; | |
3240 | ||
3241 | offset = UNIT_VTABLE(u)->exec_context_offset; | |
3242 | if (offset <= 0) | |
3243 | return NULL; | |
3244 | ||
3245 | return (ExecContext*) ((uint8_t*) u + offset); | |
3246 | } | |
3247 | ||
3248 | KillContext *unit_get_kill_context(Unit *u) { | |
3249 | size_t offset; | |
3250 | assert(u); | |
3251 | ||
3252 | if (u->type < 0) | |
3253 | return NULL; | |
3254 | ||
3255 | offset = UNIT_VTABLE(u)->kill_context_offset; | |
3256 | if (offset <= 0) | |
3257 | return NULL; | |
3258 | ||
3259 | return (KillContext*) ((uint8_t*) u + offset); | |
3260 | } | |
3261 | ||
3262 | CGroupContext *unit_get_cgroup_context(Unit *u) { | |
3263 | size_t offset; | |
3264 | ||
3265 | if (u->type < 0) | |
3266 | return NULL; | |
3267 | ||
3268 | offset = UNIT_VTABLE(u)->cgroup_context_offset; | |
3269 | if (offset <= 0) | |
3270 | return NULL; | |
3271 | ||
3272 | return (CGroupContext*) ((uint8_t*) u + offset); | |
3273 | } | |
3274 | ||
3275 | ExecRuntime *unit_get_exec_runtime(Unit *u) { | |
3276 | size_t offset; | |
3277 | ||
3278 | if (u->type < 0) | |
3279 | return NULL; | |
3280 | ||
3281 | offset = UNIT_VTABLE(u)->exec_runtime_offset; | |
3282 | if (offset <= 0) | |
3283 | return NULL; | |
3284 | ||
3285 | return *(ExecRuntime**) ((uint8_t*) u + offset); | |
3286 | } | |
3287 | ||
3288 | static int unit_drop_in_dir(Unit *u, UnitSetPropertiesMode mode, bool transient, char **dir) { | |
3289 | assert(u); | |
3290 | ||
3291 | if (u->manager->running_as == MANAGER_USER) { | |
3292 | int r; | |
3293 | ||
3294 | if (mode == UNIT_PERSISTENT && !transient) | |
3295 | r = user_config_home(dir); | |
3296 | else | |
3297 | r = user_runtime_dir(dir); | |
3298 | if (r == 0) | |
3299 | return -ENOENT; | |
3300 | ||
3301 | return r; | |
3302 | } | |
3303 | ||
3304 | if (mode == UNIT_PERSISTENT && !transient) | |
3305 | *dir = strdup("/etc/systemd/system"); | |
3306 | else | |
3307 | *dir = strdup("/run/systemd/system"); | |
3308 | if (!*dir) | |
3309 | return -ENOMEM; | |
3310 | ||
3311 | return 0; | |
3312 | } | |
3313 | ||
3314 | static int unit_drop_in_file(Unit *u, UnitSetPropertiesMode mode, const char *name, char **p, char **q) { | |
3315 | _cleanup_free_ char *dir = NULL; | |
3316 | int r; | |
3317 | ||
3318 | assert(u); | |
3319 | ||
3320 | r = unit_drop_in_dir(u, mode, u->transient, &dir); | |
3321 | if (r < 0) | |
3322 | return r; | |
3323 | ||
3324 | return drop_in_file(dir, u->id, 50, name, p, q); | |
3325 | } | |
3326 | ||
3327 | int unit_write_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data) { | |
3328 | ||
3329 | _cleanup_free_ char *dir = NULL, *p = NULL, *q = NULL; | |
3330 | int r; | |
3331 | ||
3332 | assert(u); | |
3333 | ||
3334 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) | |
3335 | return 0; | |
3336 | ||
3337 | r = unit_drop_in_dir(u, mode, u->transient, &dir); | |
3338 | if (r < 0) | |
3339 | return r; | |
3340 | ||
3341 | r = write_drop_in(dir, u->id, 50, name, data); | |
3342 | if (r < 0) | |
3343 | return r; | |
3344 | ||
3345 | r = drop_in_file(dir, u->id, 50, name, &p, &q); | |
3346 | if (r < 0) | |
3347 | return r; | |
3348 | ||
3349 | r = strv_extend(&u->dropin_paths, q); | |
3350 | if (r < 0) | |
3351 | return r; | |
3352 | ||
3353 | strv_sort(u->dropin_paths); | |
3354 | strv_uniq(u->dropin_paths); | |
3355 | ||
3356 | u->dropin_mtime = now(CLOCK_REALTIME); | |
3357 | ||
3358 | return 0; | |
3359 | } | |
3360 | ||
3361 | int unit_write_drop_in_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) { | |
3362 | _cleanup_free_ char *p = NULL; | |
3363 | va_list ap; | |
3364 | int r; | |
3365 | ||
3366 | assert(u); | |
3367 | assert(name); | |
3368 | assert(format); | |
3369 | ||
3370 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) | |
3371 | return 0; | |
3372 | ||
3373 | va_start(ap, format); | |
3374 | r = vasprintf(&p, format, ap); | |
3375 | va_end(ap); | |
3376 | ||
3377 | if (r < 0) | |
3378 | return -ENOMEM; | |
3379 | ||
3380 | return unit_write_drop_in(u, mode, name, p); | |
3381 | } | |
3382 | ||
3383 | int unit_write_drop_in_private(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data) { | |
3384 | _cleanup_free_ char *ndata = NULL; | |
3385 | ||
3386 | assert(u); | |
3387 | assert(name); | |
3388 | assert(data); | |
3389 | ||
3390 | if (!UNIT_VTABLE(u)->private_section) | |
3391 | return -EINVAL; | |
3392 | ||
3393 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) | |
3394 | return 0; | |
3395 | ||
3396 | ndata = strjoin("[", UNIT_VTABLE(u)->private_section, "]\n", data, NULL); | |
3397 | if (!ndata) | |
3398 | return -ENOMEM; | |
3399 | ||
3400 | return unit_write_drop_in(u, mode, name, ndata); | |
3401 | } | |
3402 | ||
3403 | int unit_write_drop_in_private_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) { | |
3404 | _cleanup_free_ char *p = NULL; | |
3405 | va_list ap; | |
3406 | int r; | |
3407 | ||
3408 | assert(u); | |
3409 | assert(name); | |
3410 | assert(format); | |
3411 | ||
3412 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) | |
3413 | return 0; | |
3414 | ||
3415 | va_start(ap, format); | |
3416 | r = vasprintf(&p, format, ap); | |
3417 | va_end(ap); | |
3418 | ||
3419 | if (r < 0) | |
3420 | return -ENOMEM; | |
3421 | ||
3422 | return unit_write_drop_in_private(u, mode, name, p); | |
3423 | } | |
3424 | ||
3425 | int unit_remove_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name) { | |
3426 | _cleanup_free_ char *p = NULL, *q = NULL; | |
3427 | int r; | |
3428 | ||
3429 | assert(u); | |
3430 | ||
3431 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) | |
3432 | return 0; | |
3433 | ||
3434 | r = unit_drop_in_file(u, mode, name, &p, &q); | |
3435 | if (r < 0) | |
3436 | return r; | |
3437 | ||
3438 | if (unlink(q) < 0) | |
3439 | r = errno == ENOENT ? 0 : -errno; | |
3440 | else | |
3441 | r = 1; | |
3442 | ||
3443 | rmdir(p); | |
3444 | return r; | |
3445 | } | |
3446 | ||
3447 | int unit_make_transient(Unit *u) { | |
3448 | assert(u); | |
3449 | ||
3450 | if (!UNIT_VTABLE(u)->can_transient) | |
3451 | return -EOPNOTSUPP; | |
3452 | ||
3453 | u->load_state = UNIT_STUB; | |
3454 | u->load_error = 0; | |
3455 | u->transient = true; | |
3456 | u->fragment_path = mfree(u->fragment_path); | |
3457 | ||
3458 | return 0; | |
3459 | } | |
3460 | ||
3461 | int unit_kill_context( | |
3462 | Unit *u, | |
3463 | KillContext *c, | |
3464 | KillOperation k, | |
3465 | pid_t main_pid, | |
3466 | pid_t control_pid, | |
3467 | bool main_pid_alien) { | |
3468 | ||
3469 | bool wait_for_exit = false; | |
3470 | int sig, r; | |
3471 | ||
3472 | assert(u); | |
3473 | assert(c); | |
3474 | ||
3475 | if (c->kill_mode == KILL_NONE) | |
3476 | return 0; | |
3477 | ||
3478 | switch (k) { | |
3479 | case KILL_KILL: | |
3480 | sig = SIGKILL; | |
3481 | break; | |
3482 | case KILL_ABORT: | |
3483 | sig = SIGABRT; | |
3484 | break; | |
3485 | case KILL_TERMINATE: | |
3486 | sig = c->kill_signal; | |
3487 | break; | |
3488 | default: | |
3489 | assert_not_reached("KillOperation unknown"); | |
3490 | } | |
3491 | ||
3492 | if (main_pid > 0) { | |
3493 | r = kill_and_sigcont(main_pid, sig); | |
3494 | ||
3495 | if (r < 0 && r != -ESRCH) { | |
3496 | _cleanup_free_ char *comm = NULL; | |
3497 | get_process_comm(main_pid, &comm); | |
3498 | ||
3499 | log_unit_warning_errno(u, r, "Failed to kill main process " PID_FMT " (%s), ignoring: %m", main_pid, strna(comm)); | |
3500 | } else { | |
3501 | if (!main_pid_alien) | |
3502 | wait_for_exit = true; | |
3503 | ||
3504 | if (c->send_sighup && k == KILL_TERMINATE) | |
3505 | (void) kill(main_pid, SIGHUP); | |
3506 | } | |
3507 | } | |
3508 | ||
3509 | if (control_pid > 0) { | |
3510 | r = kill_and_sigcont(control_pid, sig); | |
3511 | ||
3512 | if (r < 0 && r != -ESRCH) { | |
3513 | _cleanup_free_ char *comm = NULL; | |
3514 | get_process_comm(control_pid, &comm); | |
3515 | ||
3516 | log_unit_warning_errno(u, r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", control_pid, strna(comm)); | |
3517 | } else { | |
3518 | wait_for_exit = true; | |
3519 | ||
3520 | if (c->send_sighup && k == KILL_TERMINATE) | |
3521 | (void) kill(control_pid, SIGHUP); | |
3522 | } | |
3523 | } | |
3524 | ||
3525 | if (u->cgroup_path && | |
3526 | (c->kill_mode == KILL_CONTROL_GROUP || (c->kill_mode == KILL_MIXED && k == KILL_KILL))) { | |
3527 | _cleanup_set_free_ Set *pid_set = NULL; | |
3528 | ||
3529 | /* Exclude the main/control pids from being killed via the cgroup */ | |
3530 | pid_set = unit_pid_set(main_pid, control_pid); | |
3531 | if (!pid_set) | |
3532 | return -ENOMEM; | |
3533 | ||
3534 | r = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, sig, true, k != KILL_TERMINATE, false, pid_set); | |
3535 | if (r < 0) { | |
3536 | if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) | |
3537 | log_unit_warning_errno(u, r, "Failed to kill control group %s, ignoring: %m", u->cgroup_path); | |
3538 | ||
3539 | } else if (r > 0) { | |
3540 | ||
3541 | /* FIXME: For now, on the legacy hierarchy, we | |
3542 | * will not wait for the cgroup members to die | |
3543 | * if we are running in a container or if this | |
3544 | * is a delegation unit, simply because cgroup | |
3545 | * notification is unreliable in these | |
3546 | * cases. It doesn't work at all in | |
3547 | * containers, and outside of containers it | |
3548 | * can be confused easily by left-over | |
3549 | * directories in the cgroup -- which however | |
3550 | * should not exist in non-delegated units. On | |
3551 | * the unified hierarchy that's different, | |
3552 | * there we get proper events. Hence rely on | |
3553 | * them.*/ | |
3554 | ||
3555 | if (cg_unified() > 0 || | |
3556 | (detect_container() == 0 && !unit_cgroup_delegate(u))) | |
3557 | wait_for_exit = true; | |
3558 | ||
3559 | if (c->send_sighup && k != KILL_KILL) { | |
3560 | set_free(pid_set); | |
3561 | ||
3562 | pid_set = unit_pid_set(main_pid, control_pid); | |
3563 | if (!pid_set) | |
3564 | return -ENOMEM; | |
3565 | ||
3566 | cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, SIGHUP, false, true, false, pid_set); | |
3567 | } | |
3568 | } | |
3569 | } | |
3570 | ||
3571 | return wait_for_exit; | |
3572 | } | |
3573 | ||
3574 | int unit_require_mounts_for(Unit *u, const char *path) { | |
3575 | char prefix[strlen(path) + 1], *p; | |
3576 | int r; | |
3577 | ||
3578 | assert(u); | |
3579 | assert(path); | |
3580 | ||
3581 | /* Registers a unit for requiring a certain path and all its | |
3582 | * prefixes. We keep a simple array of these paths in the | |
3583 | * unit, since its usually short. However, we build a prefix | |
3584 | * table for all possible prefixes so that new appearing mount | |
3585 | * units can easily determine which units to make themselves a | |
3586 | * dependency of. */ | |
3587 | ||
3588 | if (!path_is_absolute(path)) | |
3589 | return -EINVAL; | |
3590 | ||
3591 | p = strdup(path); | |
3592 | if (!p) | |
3593 | return -ENOMEM; | |
3594 | ||
3595 | path_kill_slashes(p); | |
3596 | ||
3597 | if (!path_is_safe(p)) { | |
3598 | free(p); | |
3599 | return -EPERM; | |
3600 | } | |
3601 | ||
3602 | if (strv_contains(u->requires_mounts_for, p)) { | |
3603 | free(p); | |
3604 | return 0; | |
3605 | } | |
3606 | ||
3607 | r = strv_consume(&u->requires_mounts_for, p); | |
3608 | if (r < 0) | |
3609 | return r; | |
3610 | ||
3611 | PATH_FOREACH_PREFIX_MORE(prefix, p) { | |
3612 | Set *x; | |
3613 | ||
3614 | x = hashmap_get(u->manager->units_requiring_mounts_for, prefix); | |
3615 | if (!x) { | |
3616 | char *q; | |
3617 | ||
3618 | r = hashmap_ensure_allocated(&u->manager->units_requiring_mounts_for, &string_hash_ops); | |
3619 | if (r < 0) | |
3620 | return r; | |
3621 | ||
3622 | q = strdup(prefix); | |
3623 | if (!q) | |
3624 | return -ENOMEM; | |
3625 | ||
3626 | x = set_new(NULL); | |
3627 | if (!x) { | |
3628 | free(q); | |
3629 | return -ENOMEM; | |
3630 | } | |
3631 | ||
3632 | r = hashmap_put(u->manager->units_requiring_mounts_for, q, x); | |
3633 | if (r < 0) { | |
3634 | free(q); | |
3635 | set_free(x); | |
3636 | return r; | |
3637 | } | |
3638 | } | |
3639 | ||
3640 | r = set_put(x, u); | |
3641 | if (r < 0) | |
3642 | return r; | |
3643 | } | |
3644 | ||
3645 | return 0; | |
3646 | } | |
3647 | ||
3648 | int unit_setup_exec_runtime(Unit *u) { | |
3649 | ExecRuntime **rt; | |
3650 | size_t offset; | |
3651 | Iterator i; | |
3652 | Unit *other; | |
3653 | ||
3654 | offset = UNIT_VTABLE(u)->exec_runtime_offset; | |
3655 | assert(offset > 0); | |
3656 | ||
3657 | /* Check if there already is an ExecRuntime for this unit? */ | |
3658 | rt = (ExecRuntime**) ((uint8_t*) u + offset); | |
3659 | if (*rt) | |
3660 | return 0; | |
3661 | ||
3662 | /* Try to get it from somebody else */ | |
3663 | SET_FOREACH(other, u->dependencies[UNIT_JOINS_NAMESPACE_OF], i) { | |
3664 | ||
3665 | *rt = unit_get_exec_runtime(other); | |
3666 | if (*rt) { | |
3667 | exec_runtime_ref(*rt); | |
3668 | return 0; | |
3669 | } | |
3670 | } | |
3671 | ||
3672 | return exec_runtime_make(rt, unit_get_exec_context(u), u->id); | |
3673 | } | |
3674 | ||
3675 | bool unit_type_supported(UnitType t) { | |
3676 | if (_unlikely_(t < 0)) | |
3677 | return false; | |
3678 | if (_unlikely_(t >= _UNIT_TYPE_MAX)) | |
3679 | return false; | |
3680 | ||
3681 | if (!unit_vtable[t]->supported) | |
3682 | return true; | |
3683 | ||
3684 | return unit_vtable[t]->supported(); | |
3685 | } | |
3686 | ||
3687 | void unit_warn_if_dir_nonempty(Unit *u, const char* where) { | |
3688 | int r; | |
3689 | ||
3690 | assert(u); | |
3691 | assert(where); | |
3692 | ||
3693 | r = dir_is_empty(where); | |
3694 | if (r > 0) | |
3695 | return; | |
3696 | if (r < 0) { | |
3697 | log_unit_warning_errno(u, r, "Failed to check directory %s: %m", where); | |
3698 | return; | |
3699 | } | |
3700 | ||
3701 | log_struct(LOG_NOTICE, | |
3702 | LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING), | |
3703 | LOG_UNIT_ID(u), | |
3704 | LOG_UNIT_MESSAGE(u, "Directory %s to mount over is not empty, mounting anyway.", where), | |
3705 | "WHERE=%s", where, | |
3706 | NULL); | |
3707 | } | |
3708 | ||
3709 | int unit_fail_if_symlink(Unit *u, const char* where) { | |
3710 | int r; | |
3711 | ||
3712 | assert(u); | |
3713 | assert(where); | |
3714 | ||
3715 | r = is_symlink(where); | |
3716 | if (r < 0) { | |
3717 | log_unit_debug_errno(u, r, "Failed to check symlink %s, ignoring: %m", where); | |
3718 | return 0; | |
3719 | } | |
3720 | if (r == 0) | |
3721 | return 0; | |
3722 | ||
3723 | log_struct(LOG_ERR, | |
3724 | LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING), | |
3725 | LOG_UNIT_ID(u), | |
3726 | LOG_UNIT_MESSAGE(u, "Mount on symlink %s not allowed.", where), | |
3727 | "WHERE=%s", where, | |
3728 | NULL); | |
3729 | ||
3730 | return -ELOOP; | |
3731 | } | |
3732 | ||
3733 | static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = { | |
3734 | [UNIT_ACTIVE] = "active", | |
3735 | [UNIT_RELOADING] = "reloading", | |
3736 | [UNIT_INACTIVE] = "inactive", | |
3737 | [UNIT_FAILED] = "failed", | |
3738 | [UNIT_ACTIVATING] = "activating", | |
3739 | [UNIT_DEACTIVATING] = "deactivating" | |
3740 | }; | |
3741 | ||
3742 | DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState); |