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