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