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