chiark / gitweb /
logind: remove redundant entries from logind's default controller lists too
[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 "set.h"
33 #include "unit.h"
34 #include "macro.h"
35 #include "strv.h"
36 #include "load-fragment.h"
37 #include "load-dropin.h"
38 #include "log.h"
39 #include "unit-name.h"
40 #include "specifier.h"
41 #include "dbus-unit.h"
42 #include "special.h"
43 #include "cgroup-util.h"
44 #include "missing.h"
45 #include "cgroup-attr.h"
46
47 const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
48         [UNIT_SERVICE] = &service_vtable,
49         [UNIT_TIMER] = &timer_vtable,
50         [UNIT_SOCKET] = &socket_vtable,
51         [UNIT_TARGET] = &target_vtable,
52         [UNIT_DEVICE] = &device_vtable,
53         [UNIT_MOUNT] = &mount_vtable,
54         [UNIT_AUTOMOUNT] = &automount_vtable,
55         [UNIT_SNAPSHOT] = &snapshot_vtable,
56         [UNIT_SWAP] = &swap_vtable,
57         [UNIT_PATH] = &path_vtable
58 };
59
60 Unit *unit_new(Manager *m, size_t size) {
61         Unit *u;
62
63         assert(m);
64         assert(size >= sizeof(Unit));
65
66         u = malloc0(size);
67         if (!u)
68                 return NULL;
69
70         u->names = set_new(string_hash_func, string_compare_func);
71         if (!u->names) {
72                 free(u);
73                 return NULL;
74         }
75
76         u->manager = m;
77         u->type = _UNIT_TYPE_INVALID;
78         u->deserialized_job = _JOB_TYPE_INVALID;
79         u->default_dependencies = true;
80         u->unit_file_state = _UNIT_FILE_STATE_INVALID;
81
82         return u;
83 }
84
85 bool unit_has_name(Unit *u, const char *name) {
86         assert(u);
87         assert(name);
88
89         return !!set_get(u->names, (char*) name);
90 }
91
92 int unit_add_name(Unit *u, const char *text) {
93         UnitType t;
94         char *s, *i = NULL;
95         int r;
96
97         assert(u);
98         assert(text);
99
100         if (unit_name_is_template(text)) {
101                 if (!u->instance)
102                         return -EINVAL;
103
104                 s = unit_name_replace_instance(text, u->instance);
105         } else
106                 s = strdup(text);
107
108         if (!s)
109                 return -ENOMEM;
110
111         if (!unit_name_is_valid(s, false)) {
112                 r = -EINVAL;
113                 goto fail;
114         }
115
116         assert_se((t = unit_name_to_type(s)) >= 0);
117
118         if (u->type != _UNIT_TYPE_INVALID && t != u->type) {
119                 r = -EINVAL;
120                 goto fail;
121         }
122
123         if ((r = unit_name_to_instance(s, &i)) < 0)
124                 goto fail;
125
126         if (i && unit_vtable[t]->no_instances) {
127                 r = -EINVAL;
128                 goto fail;
129         }
130
131         /* Ensure that this unit is either instanced or not instanced,
132          * but not both. */
133         if (u->type != _UNIT_TYPE_INVALID && !u->instance != !i) {
134                 r = -EINVAL;
135                 goto fail;
136         }
137
138         if (unit_vtable[t]->no_alias &&
139             !set_isempty(u->names) &&
140             !set_get(u->names, s)) {
141                 r = -EEXIST;
142                 goto fail;
143         }
144
145         if (hashmap_size(u->manager->units) >= MANAGER_MAX_NAMES) {
146                 r = -E2BIG;
147                 goto fail;
148         }
149
150         if ((r = set_put(u->names, s)) < 0) {
151                 if (r == -EEXIST)
152                         r = 0;
153                 goto fail;
154         }
155
156         if ((r = hashmap_put(u->manager->units, s, u)) < 0) {
157                 set_remove(u->names, s);
158                 goto fail;
159         }
160
161         if (u->type == _UNIT_TYPE_INVALID) {
162
163                 u->type = t;
164                 u->id = s;
165                 u->instance = i;
166
167                 LIST_PREPEND(Unit, units_by_type, u->manager->units_by_type[t], u);
168
169                 if (UNIT_VTABLE(u)->init)
170                         UNIT_VTABLE(u)->init(u);
171         } else
172                 free(i);
173
174         unit_add_to_dbus_queue(u);
175         return 0;
176
177 fail:
178         free(s);
179         free(i);
180
181         return r;
182 }
183
184 int unit_choose_id(Unit *u, const char *name) {
185         char *s, *t = NULL, *i;
186         int r;
187
188         assert(u);
189         assert(name);
190
191         if (unit_name_is_template(name)) {
192
193                 if (!u->instance)
194                         return -EINVAL;
195
196                 if (!(t = unit_name_replace_instance(name, u->instance)))
197                         return -ENOMEM;
198
199                 name = t;
200         }
201
202         /* Selects one of the names of this unit as the id */
203         s = set_get(u->names, (char*) name);
204         free(t);
205
206         if (!s)
207                 return -ENOENT;
208
209         if ((r = unit_name_to_instance(s, &i)) < 0)
210                 return r;
211
212         u->id = s;
213
214         free(u->instance);
215         u->instance = i;
216
217         unit_add_to_dbus_queue(u);
218
219         return 0;
220 }
221
222 int unit_set_description(Unit *u, const char *description) {
223         char *s;
224
225         assert(u);
226
227         if (!(s = strdup(description)))
228                 return -ENOMEM;
229
230         free(u->description);
231         u->description = s;
232
233         unit_add_to_dbus_queue(u);
234         return 0;
235 }
236
237 bool unit_check_gc(Unit *u) {
238         assert(u);
239
240         if (u->load_state == UNIT_STUB)
241                 return true;
242
243         if (UNIT_VTABLE(u)->no_gc)
244                 return true;
245
246         if (u->no_gc)
247                 return true;
248
249         if (u->job)
250                 return true;
251
252         if (unit_active_state(u) != UNIT_INACTIVE)
253                 return true;
254
255         if (UNIT_VTABLE(u)->check_gc)
256                 if (UNIT_VTABLE(u)->check_gc(u))
257                         return true;
258
259         return false;
260 }
261
262 void unit_add_to_load_queue(Unit *u) {
263         assert(u);
264         assert(u->type != _UNIT_TYPE_INVALID);
265
266         if (u->load_state != UNIT_STUB || u->in_load_queue)
267                 return;
268
269         LIST_PREPEND(Unit, load_queue, u->manager->load_queue, u);
270         u->in_load_queue = true;
271 }
272
273 void unit_add_to_cleanup_queue(Unit *u) {
274         assert(u);
275
276         if (u->in_cleanup_queue)
277                 return;
278
279         LIST_PREPEND(Unit, cleanup_queue, u->manager->cleanup_queue, u);
280         u->in_cleanup_queue = true;
281 }
282
283 void unit_add_to_gc_queue(Unit *u) {
284         assert(u);
285
286         if (u->in_gc_queue || u->in_cleanup_queue)
287                 return;
288
289         if (unit_check_gc(u))
290                 return;
291
292         LIST_PREPEND(Unit, gc_queue, u->manager->gc_queue, u);
293         u->in_gc_queue = true;
294
295         u->manager->n_in_gc_queue ++;
296
297         if (u->manager->gc_queue_timestamp <= 0)
298                 u->manager->gc_queue_timestamp = now(CLOCK_MONOTONIC);
299 }
300
301 void unit_add_to_dbus_queue(Unit *u) {
302         assert(u);
303         assert(u->type != _UNIT_TYPE_INVALID);
304
305         if (u->load_state == UNIT_STUB || u->in_dbus_queue)
306                 return;
307
308         /* Shortcut things if nobody cares */
309         if (!bus_has_subscriber(u->manager)) {
310                 u->sent_dbus_new_signal = true;
311                 return;
312         }
313
314         LIST_PREPEND(Unit, dbus_queue, u->manager->dbus_unit_queue, u);
315         u->in_dbus_queue = true;
316 }
317
318 static void bidi_set_free(Unit *u, Set *s) {
319         Iterator i;
320         Unit *other;
321
322         assert(u);
323
324         /* Frees the set and makes sure we are dropped from the
325          * inverse pointers */
326
327         SET_FOREACH(other, s, i) {
328                 UnitDependency d;
329
330                 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
331                         set_remove(other->dependencies[d], u);
332
333                 unit_add_to_gc_queue(other);
334         }
335
336         set_free(s);
337 }
338
339 void unit_free(Unit *u) {
340         UnitDependency d;
341         Iterator i;
342         char *t;
343
344         assert(u);
345
346         bus_unit_send_removed_signal(u);
347
348         if (u->load_state != UNIT_STUB)
349                 if (UNIT_VTABLE(u)->done)
350                         UNIT_VTABLE(u)->done(u);
351
352         SET_FOREACH(t, u->names, i)
353                 hashmap_remove_value(u->manager->units, t, u);
354
355         if (u->job)
356                 job_free(u->job);
357
358         for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
359                 bidi_set_free(u, u->dependencies[d]);
360
361         if (u->type != _UNIT_TYPE_INVALID)
362                 LIST_REMOVE(Unit, units_by_type, u->manager->units_by_type[u->type], u);
363
364         if (u->in_load_queue)
365                 LIST_REMOVE(Unit, load_queue, u->manager->load_queue, u);
366
367         if (u->in_dbus_queue)
368                 LIST_REMOVE(Unit, dbus_queue, u->manager->dbus_unit_queue, u);
369
370         if (u->in_cleanup_queue)
371                 LIST_REMOVE(Unit, cleanup_queue, u->manager->cleanup_queue, u);
372
373         if (u->in_gc_queue) {
374                 LIST_REMOVE(Unit, gc_queue, u->manager->gc_queue, u);
375                 u->manager->n_in_gc_queue--;
376         }
377
378         cgroup_bonding_free_list(u->cgroup_bondings, u->manager->n_reloading <= 0);
379         cgroup_attribute_free_list(u->cgroup_attributes);
380
381         free(u->description);
382         free(u->fragment_path);
383         free(u->instance);
384
385         set_free_free(u->names);
386
387         condition_free_list(u->conditions);
388
389         while (u->refs)
390                 unit_ref_unset(u->refs);
391
392         free(u);
393 }
394
395 UnitActiveState unit_active_state(Unit *u) {
396         assert(u);
397
398         if (u->load_state == UNIT_MERGED)
399                 return unit_active_state(unit_follow_merge(u));
400
401         /* After a reload it might happen that a unit is not correctly
402          * loaded but still has a process around. That's why we won't
403          * shortcut failed loading to UNIT_INACTIVE_FAILED. */
404
405         return UNIT_VTABLE(u)->active_state(u);
406 }
407
408 const char* unit_sub_state_to_string(Unit *u) {
409         assert(u);
410
411         return UNIT_VTABLE(u)->sub_state_to_string(u);
412 }
413
414 static void complete_move(Set **s, Set **other) {
415         assert(s);
416         assert(other);
417
418         if (!*other)
419                 return;
420
421         if (*s)
422                 set_move(*s, *other);
423         else {
424                 *s = *other;
425                 *other = NULL;
426         }
427 }
428
429 static void merge_names(Unit *u, Unit *other) {
430         char *t;
431         Iterator i;
432
433         assert(u);
434         assert(other);
435
436         complete_move(&u->names, &other->names);
437
438         set_free_free(other->names);
439         other->names = NULL;
440         other->id = NULL;
441
442         SET_FOREACH(t, u->names, i)
443                 assert_se(hashmap_replace(u->manager->units, t, u) == 0);
444 }
445
446 static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
447         Iterator i;
448         Unit *back;
449         int r;
450
451         assert(u);
452         assert(other);
453         assert(d < _UNIT_DEPENDENCY_MAX);
454
455         /* Fix backwards pointers */
456         SET_FOREACH(back, other->dependencies[d], i) {
457                 UnitDependency k;
458
459                 for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++)
460                         if ((r = set_remove_and_put(back->dependencies[k], other, u)) < 0) {
461
462                                 if (r == -EEXIST)
463                                         set_remove(back->dependencies[k], other);
464                                 else
465                                         assert(r == -ENOENT);
466                         }
467         }
468
469         complete_move(&u->dependencies[d], &other->dependencies[d]);
470
471         set_free(other->dependencies[d]);
472         other->dependencies[d] = NULL;
473 }
474
475 int unit_merge(Unit *u, Unit *other) {
476         UnitDependency d;
477
478         assert(u);
479         assert(other);
480         assert(u->manager == other->manager);
481         assert(u->type != _UNIT_TYPE_INVALID);
482
483         other = unit_follow_merge(other);
484
485         if (other == u)
486                 return 0;
487
488         if (u->type != other->type)
489                 return -EINVAL;
490
491         if (!u->instance != !other->instance)
492                 return -EINVAL;
493
494         if (other->load_state != UNIT_STUB &&
495             other->load_state != UNIT_ERROR)
496                 return -EEXIST;
497
498         if (other->job)
499                 return -EEXIST;
500
501         if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other)))
502                 return -EEXIST;
503
504         /* Merge names */
505         merge_names(u, other);
506
507         /* Redirect all references */
508         while (other->refs)
509                 unit_ref_set(other->refs, u);
510
511         /* Merge dependencies */
512         for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
513                 merge_dependencies(u, other, d);
514
515         other->load_state = UNIT_MERGED;
516         other->merged_into = u;
517
518         /* If there is still some data attached to the other node, we
519          * don't need it anymore, and can free it. */
520         if (other->load_state != UNIT_STUB)
521                 if (UNIT_VTABLE(other)->done)
522                         UNIT_VTABLE(other)->done(other);
523
524         unit_add_to_dbus_queue(u);
525         unit_add_to_cleanup_queue(other);
526
527         return 0;
528 }
529
530 int unit_merge_by_name(Unit *u, const char *name) {
531         Unit *other;
532         int r;
533         char *s = NULL;
534
535         assert(u);
536         assert(name);
537
538         if (unit_name_is_template(name)) {
539                 if (!u->instance)
540                         return -EINVAL;
541
542                 if (!(s = unit_name_replace_instance(name, u->instance)))
543                         return -ENOMEM;
544
545                 name = s;
546         }
547
548         if (!(other = manager_get_unit(u->manager, name)))
549                 r = unit_add_name(u, name);
550         else
551                 r = unit_merge(u, other);
552
553         free(s);
554         return r;
555 }
556
557 Unit* unit_follow_merge(Unit *u) {
558         assert(u);
559
560         while (u->load_state == UNIT_MERGED)
561                 assert_se(u = u->merged_into);
562
563         return u;
564 }
565
566 int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
567         int r;
568
569         assert(u);
570         assert(c);
571
572         if (c->std_output != EXEC_OUTPUT_KMSG &&
573             c->std_output != EXEC_OUTPUT_SYSLOG &&
574             c->std_output != EXEC_OUTPUT_JOURNAL &&
575             c->std_output != EXEC_OUTPUT_KMSG_AND_CONSOLE &&
576             c->std_output != EXEC_OUTPUT_SYSLOG_AND_CONSOLE &&
577             c->std_output != EXEC_OUTPUT_JOURNAL_AND_CONSOLE &&
578             c->std_error != EXEC_OUTPUT_KMSG &&
579             c->std_error != EXEC_OUTPUT_SYSLOG &&
580             c->std_error != EXEC_OUTPUT_JOURNAL &&
581             c->std_error != EXEC_OUTPUT_KMSG_AND_CONSOLE &&
582             c->std_error != EXEC_OUTPUT_JOURNAL_AND_CONSOLE &&
583             c->std_error != EXEC_OUTPUT_SYSLOG_AND_CONSOLE)
584                 return 0;
585
586         /* If syslog or kernel logging is requested, make sure our own
587          * logging daemon is run first. */
588
589         if (u->manager->running_as == MANAGER_SYSTEM)
590                 if ((r = unit_add_two_dependencies_by_name(u, UNIT_REQUIRES, UNIT_AFTER, SPECIAL_JOURNALD_SOCKET, NULL, true)) < 0)
591                         return r;
592
593         return 0;
594 }
595
596 const char *unit_description(Unit *u) {
597         assert(u);
598
599         if (u->description)
600                 return u->description;
601
602         return strna(u->id);
603 }
604
605 void unit_dump(Unit *u, FILE *f, const char *prefix) {
606         char *t;
607         UnitDependency d;
608         Iterator i;
609         char *p2;
610         const char *prefix2;
611         char
612                 timestamp1[FORMAT_TIMESTAMP_MAX],
613                 timestamp2[FORMAT_TIMESTAMP_MAX],
614                 timestamp3[FORMAT_TIMESTAMP_MAX],
615                 timestamp4[FORMAT_TIMESTAMP_MAX],
616                 timespan[FORMAT_TIMESPAN_MAX];
617         Unit *following;
618
619         assert(u);
620         assert(u->type >= 0);
621
622         if (!prefix)
623                 prefix = "";
624         p2 = strappend(prefix, "\t");
625         prefix2 = p2 ? p2 : prefix;
626
627         fprintf(f,
628                 "%s-> Unit %s:\n"
629                 "%s\tDescription: %s\n"
630                 "%s\tInstance: %s\n"
631                 "%s\tUnit Load State: %s\n"
632                 "%s\tUnit Active State: %s\n"
633                 "%s\tInactive Exit Timestamp: %s\n"
634                 "%s\tActive Enter Timestamp: %s\n"
635                 "%s\tActive Exit Timestamp: %s\n"
636                 "%s\tInactive Enter Timestamp: %s\n"
637                 "%s\tGC Check Good: %s\n"
638                 "%s\tNeed Daemon Reload: %s\n",
639                 prefix, u->id,
640                 prefix, unit_description(u),
641                 prefix, strna(u->instance),
642                 prefix, unit_load_state_to_string(u->load_state),
643                 prefix, unit_active_state_to_string(unit_active_state(u)),
644                 prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->inactive_exit_timestamp.realtime)),
645                 prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->active_enter_timestamp.realtime)),
646                 prefix, strna(format_timestamp(timestamp3, sizeof(timestamp3), u->active_exit_timestamp.realtime)),
647                 prefix, strna(format_timestamp(timestamp4, sizeof(timestamp4), u->inactive_enter_timestamp.realtime)),
648                 prefix, yes_no(unit_check_gc(u)),
649                 prefix, yes_no(unit_need_daemon_reload(u)));
650
651         SET_FOREACH(t, u->names, i)
652                 fprintf(f, "%s\tName: %s\n", prefix, t);
653
654         if ((following = unit_following(u)))
655                 fprintf(f, "%s\tFollowing: %s\n", prefix, following->id);
656
657         if (u->fragment_path)
658                 fprintf(f, "%s\tFragment Path: %s\n", prefix, u->fragment_path);
659
660         if (u->job_timeout > 0)
661                 fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->job_timeout));
662
663         condition_dump_list(u->conditions, f, prefix);
664
665         if (dual_timestamp_is_set(&u->condition_timestamp))
666                 fprintf(f,
667                         "%s\tCondition Timestamp: %s\n"
668                         "%s\tCondition Result: %s\n",
669                         prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->condition_timestamp.realtime)),
670                         prefix, yes_no(u->condition_result));
671
672         for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
673                 Unit *other;
674
675                 SET_FOREACH(other, u->dependencies[d], i)
676                         fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), other->id);
677         }
678
679         if (u->load_state == UNIT_LOADED) {
680                 CGroupBonding *b;
681                 CGroupAttribute *a;
682
683                 fprintf(f,
684                         "%s\tStopWhenUnneeded: %s\n"
685                         "%s\tRefuseManualStart: %s\n"
686                         "%s\tRefuseManualStop: %s\n"
687                         "%s\tDefaultDependencies: %s\n"
688                         "%s\tOnFailureIsolate: %s\n"
689                         "%s\tIgnoreOnIsolate: %s\n"
690                         "%s\tIgnoreOnSnapshot: %s\n",
691                         prefix, yes_no(u->stop_when_unneeded),
692                         prefix, yes_no(u->refuse_manual_start),
693                         prefix, yes_no(u->refuse_manual_stop),
694                         prefix, yes_no(u->default_dependencies),
695                         prefix, yes_no(u->on_failure_isolate),
696                         prefix, yes_no(u->ignore_on_isolate),
697                         prefix, yes_no(u->ignore_on_snapshot));
698
699                 LIST_FOREACH(by_unit, b, u->cgroup_bondings)
700                         fprintf(f, "%s\tControlGroup: %s:%s\n",
701                                 prefix, b->controller, b->path);
702
703                 LIST_FOREACH(by_unit, a, u->cgroup_attributes) {
704                         char *v = NULL;
705
706                         if (a->map_callback)
707                                 a->map_callback(a->controller, a->name, a->value, &v);
708
709                         fprintf(f, "%s\tControlGroupAttribute: %s %s \"%s\"\n",
710                                 prefix, a->controller, a->name, v ? v : a->value);
711
712                         free(v);
713                 }
714
715                 if (UNIT_VTABLE(u)->dump)
716                         UNIT_VTABLE(u)->dump(u, f, prefix2);
717
718         } else if (u->load_state == UNIT_MERGED)
719                 fprintf(f,
720                         "%s\tMerged into: %s\n",
721                         prefix, u->merged_into->id);
722         else if (u->load_state == UNIT_ERROR)
723                 fprintf(f, "%s\tLoad Error Code: %s\n", prefix, strerror(-u->load_error));
724
725
726         if (u->job)
727                 job_dump(u->job, f, prefix2);
728
729         free(p2);
730 }
731
732 /* Common implementation for multiple backends */
733 int unit_load_fragment_and_dropin(Unit *u) {
734         int r;
735
736         assert(u);
737
738         /* Load a .service file */
739         if ((r = unit_load_fragment(u)) < 0)
740                 return r;
741
742         if (u->load_state == UNIT_STUB)
743                 return -ENOENT;
744
745         /* Load drop-in directory data */
746         if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
747                 return r;
748
749         return 0;
750 }
751
752 /* Common implementation for multiple backends */
753 int unit_load_fragment_and_dropin_optional(Unit *u) {
754         int r;
755
756         assert(u);
757
758         /* Same as unit_load_fragment_and_dropin(), but whether
759          * something can be loaded or not doesn't matter. */
760
761         /* Load a .service file */
762         if ((r = unit_load_fragment(u)) < 0)
763                 return r;
764
765         if (u->load_state == UNIT_STUB)
766                 u->load_state = UNIT_LOADED;
767
768         /* Load drop-in directory data */
769         if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
770                 return r;
771
772         return 0;
773 }
774
775 int unit_add_default_target_dependency(Unit *u, Unit *target) {
776         assert(u);
777         assert(target);
778
779         if (target->type != UNIT_TARGET)
780                 return 0;
781
782         /* Only add the dependency if both units are loaded, so that
783          * that loop check below is reliable */
784         if (u->load_state != UNIT_LOADED ||
785             target->load_state != UNIT_LOADED)
786                 return 0;
787
788         /* If either side wants no automatic dependencies, then let's
789          * skip this */
790         if (!u->default_dependencies ||
791             !target->default_dependencies)
792                 return 0;
793
794         /* Don't create loops */
795         if (set_get(target->dependencies[UNIT_BEFORE], u))
796                 return 0;
797
798         return unit_add_dependency(target, UNIT_AFTER, u, true);
799 }
800
801 static int unit_add_default_dependencies(Unit *u) {
802         static const UnitDependency deps[] = {
803                 UNIT_REQUIRED_BY,
804                 UNIT_REQUIRED_BY_OVERRIDABLE,
805                 UNIT_WANTED_BY,
806                 UNIT_BOUND_BY
807         };
808
809         Unit *target;
810         Iterator i;
811         int r;
812         unsigned k;
813
814         assert(u);
815
816         for (k = 0; k < ELEMENTSOF(deps); k++)
817                 SET_FOREACH(target, u->dependencies[deps[k]], i)
818                         if ((r = unit_add_default_target_dependency(u, target)) < 0)
819                                 return r;
820
821         return 0;
822 }
823
824 int unit_load(Unit *u) {
825         int r;
826
827         assert(u);
828
829         if (u->in_load_queue) {
830                 LIST_REMOVE(Unit, load_queue, u->manager->load_queue, u);
831                 u->in_load_queue = false;
832         }
833
834         if (u->type == _UNIT_TYPE_INVALID)
835                 return -EINVAL;
836
837         if (u->load_state != UNIT_STUB)
838                 return 0;
839
840         if (UNIT_VTABLE(u)->load)
841                 if ((r = UNIT_VTABLE(u)->load(u)) < 0)
842                         goto fail;
843
844         if (u->load_state == UNIT_STUB) {
845                 r = -ENOENT;
846                 goto fail;
847         }
848
849         if (u->load_state == UNIT_LOADED &&
850             u->default_dependencies)
851                 if ((r = unit_add_default_dependencies(u)) < 0)
852                         goto fail;
853
854         if (u->on_failure_isolate &&
855             set_size(u->dependencies[UNIT_ON_FAILURE]) > 1) {
856
857                 log_error("More than one OnFailure= dependencies specified for %s but OnFailureIsolate= enabled. Refusing.",
858                           u->id);
859
860                 r = -EINVAL;
861                 goto fail;
862         }
863
864         assert((u->load_state != UNIT_MERGED) == !u->merged_into);
865
866         unit_add_to_dbus_queue(unit_follow_merge(u));
867         unit_add_to_gc_queue(u);
868
869         return 0;
870
871 fail:
872         u->load_state = UNIT_ERROR;
873         u->load_error = r;
874         unit_add_to_dbus_queue(u);
875         unit_add_to_gc_queue(u);
876
877         log_debug("Failed to load configuration for %s: %s", u->id, strerror(-r));
878
879         return r;
880 }
881
882 bool unit_condition_test(Unit *u) {
883         assert(u);
884
885         dual_timestamp_get(&u->condition_timestamp);
886         u->condition_result = condition_test_list(u->conditions);
887
888         return u->condition_result;
889 }
890
891 /* Errors:
892  *         -EBADR:     This unit type does not support starting.
893  *         -EALREADY:  Unit is already started.
894  *         -EAGAIN:    An operation is already in progress. Retry later.
895  *         -ECANCELED: Too many requests for now.
896  */
897 int unit_start(Unit *u) {
898         UnitActiveState state;
899         Unit *following;
900
901         assert(u);
902
903         if (u->load_state != UNIT_LOADED)
904                 return -EINVAL;
905
906         /* If this is already started, then this will succeed. Note
907          * that this will even succeed if this unit is not startable
908          * by the user. This is relied on to detect when we need to
909          * wait for units and when waiting is finished. */
910         state = unit_active_state(u);
911         if (UNIT_IS_ACTIVE_OR_RELOADING(state))
912                 return -EALREADY;
913
914         /* If the conditions failed, don't do anything at all. If we
915          * already are activating this call might still be useful to
916          * speed up activation in case there is some hold-off time,
917          * but we don't want to recheck the condition in that case. */
918         if (state != UNIT_ACTIVATING &&
919             !unit_condition_test(u)) {
920                 log_debug("Starting of %s requested but condition failed. Ignoring.", u->id);
921                 return -EALREADY;
922         }
923
924         /* Forward to the main object, if we aren't it. */
925         if ((following = unit_following(u))) {
926                 log_debug("Redirecting start request from %s to %s.", u->id, following->id);
927                 return unit_start(following);
928         }
929
930         /* If it is stopped, but we cannot start it, then fail */
931         if (!UNIT_VTABLE(u)->start)
932                 return -EBADR;
933
934         /* We don't suppress calls to ->start() here when we are
935          * already starting, to allow this request to be used as a
936          * "hurry up" call, for example when the unit is in some "auto
937          * restart" state where it waits for a holdoff timer to elapse
938          * before it will start again. */
939
940         unit_add_to_dbus_queue(u);
941
942         unit_status_printf(u, NULL, "Starting %s...", unit_description(u));
943         return UNIT_VTABLE(u)->start(u);
944 }
945
946 bool unit_can_start(Unit *u) {
947         assert(u);
948
949         return !!UNIT_VTABLE(u)->start;
950 }
951
952 bool unit_can_isolate(Unit *u) {
953         assert(u);
954
955         return unit_can_start(u) &&
956                 u->allow_isolate;
957 }
958
959 /* Errors:
960  *         -EBADR:    This unit type does not support stopping.
961  *         -EALREADY: Unit is already stopped.
962  *         -EAGAIN:   An operation is already in progress. Retry later.
963  */
964 int unit_stop(Unit *u) {
965         UnitActiveState state;
966         Unit *following;
967
968         assert(u);
969
970         state = unit_active_state(u);
971         if (UNIT_IS_INACTIVE_OR_FAILED(state))
972                 return -EALREADY;
973
974         if ((following = unit_following(u))) {
975                 log_debug("Redirecting stop request from %s to %s.", u->id, following->id);
976                 return unit_stop(following);
977         }
978
979         if (!UNIT_VTABLE(u)->stop)
980                 return -EBADR;
981
982         unit_add_to_dbus_queue(u);
983
984         unit_status_printf(u, NULL, "Stopping %s...", unit_description(u));
985         return UNIT_VTABLE(u)->stop(u);
986 }
987
988 /* Errors:
989  *         -EBADR:    This unit type does not support reloading.
990  *         -ENOEXEC:  Unit is not started.
991  *         -EAGAIN:   An operation is already in progress. Retry later.
992  */
993 int unit_reload(Unit *u) {
994         UnitActiveState state;
995         Unit *following;
996
997         assert(u);
998
999         if (u->load_state != UNIT_LOADED)
1000                 return -EINVAL;
1001
1002         if (!unit_can_reload(u))
1003                 return -EBADR;
1004
1005         state = unit_active_state(u);
1006         if (state == UNIT_RELOADING)
1007                 return -EALREADY;
1008
1009         if (state != UNIT_ACTIVE)
1010                 return -ENOEXEC;
1011
1012         if ((following = unit_following(u))) {
1013                 log_debug("Redirecting reload request from %s to %s.", u->id, following->id);
1014                 return unit_reload(following);
1015         }
1016
1017         unit_add_to_dbus_queue(u);
1018         return UNIT_VTABLE(u)->reload(u);
1019 }
1020
1021 bool unit_can_reload(Unit *u) {
1022         assert(u);
1023
1024         if (!UNIT_VTABLE(u)->reload)
1025                 return false;
1026
1027         if (!UNIT_VTABLE(u)->can_reload)
1028                 return true;
1029
1030         return UNIT_VTABLE(u)->can_reload(u);
1031 }
1032
1033 static void unit_check_unneeded(Unit *u) {
1034         Iterator i;
1035         Unit *other;
1036
1037         assert(u);
1038
1039         /* If this service shall be shut down when unneeded then do
1040          * so. */
1041
1042         if (!u->stop_when_unneeded)
1043                 return;
1044
1045         if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
1046                 return;
1047
1048         SET_FOREACH(other, u->dependencies[UNIT_REQUIRED_BY], i)
1049                 if (unit_pending_active(other))
1050                         return;
1051
1052         SET_FOREACH(other, u->dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
1053                 if (unit_pending_active(other))
1054                         return;
1055
1056         SET_FOREACH(other, u->dependencies[UNIT_WANTED_BY], i)
1057                 if (unit_pending_active(other))
1058                         return;
1059
1060         SET_FOREACH(other, u->dependencies[UNIT_BOUND_BY], i)
1061                 if (unit_pending_active(other))
1062                         return;
1063
1064         log_info("Service %s is not needed anymore. Stopping.", u->id);
1065
1066         /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
1067         manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, true, NULL, NULL);
1068 }
1069
1070 static void retroactively_start_dependencies(Unit *u) {
1071         Iterator i;
1072         Unit *other;
1073
1074         assert(u);
1075         assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
1076
1077         SET_FOREACH(other, u->dependencies[UNIT_REQUIRES], i)
1078                 if (!set_get(u->dependencies[UNIT_AFTER], other) &&
1079                     !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
1080                         manager_add_job(u->manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
1081
1082         SET_FOREACH(other, u->dependencies[UNIT_BIND_TO], i)
1083                 if (!set_get(u->dependencies[UNIT_AFTER], other) &&
1084                     !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
1085                         manager_add_job(u->manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
1086
1087         SET_FOREACH(other, u->dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
1088                 if (!set_get(u->dependencies[UNIT_AFTER], other) &&
1089                     !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
1090                         manager_add_job(u->manager, JOB_START, other, JOB_FAIL, false, NULL, NULL);
1091
1092         SET_FOREACH(other, u->dependencies[UNIT_REQUISITE], i)
1093                 if (!set_get(u->dependencies[UNIT_AFTER], other) &&
1094                     !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
1095                         manager_add_job(u->manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
1096
1097         SET_FOREACH(other, u->dependencies[UNIT_WANTS], i)
1098                 if (!set_get(u->dependencies[UNIT_AFTER], other) &&
1099                     !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
1100                         manager_add_job(u->manager, JOB_START, other, JOB_FAIL, false, NULL, NULL);
1101
1102         SET_FOREACH(other, u->dependencies[UNIT_CONFLICTS], i)
1103                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
1104                         manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
1105
1106         SET_FOREACH(other, u->dependencies[UNIT_CONFLICTED_BY], i)
1107                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
1108                         manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
1109 }
1110
1111 static void retroactively_stop_dependencies(Unit *u) {
1112         Iterator i;
1113         Unit *other;
1114
1115         assert(u);
1116         assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
1117
1118         /* Pull down units which are bound to us recursively if enabled */
1119         SET_FOREACH(other, u->dependencies[UNIT_BOUND_BY], i)
1120                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
1121                         manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
1122 }
1123
1124 static void check_unneeded_dependencies(Unit *u) {
1125         Iterator i;
1126         Unit *other;
1127
1128         assert(u);
1129         assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
1130
1131         /* Garbage collect services that might not be needed anymore, if enabled */
1132         SET_FOREACH(other, u->dependencies[UNIT_REQUIRES], i)
1133                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
1134                         unit_check_unneeded(other);
1135         SET_FOREACH(other, u->dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
1136                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
1137                         unit_check_unneeded(other);
1138         SET_FOREACH(other, u->dependencies[UNIT_WANTS], i)
1139                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
1140                         unit_check_unneeded(other);
1141         SET_FOREACH(other, u->dependencies[UNIT_REQUISITE], i)
1142                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
1143                         unit_check_unneeded(other);
1144         SET_FOREACH(other, u->dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
1145                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
1146                         unit_check_unneeded(other);
1147         SET_FOREACH(other, u->dependencies[UNIT_BIND_TO], i)
1148                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
1149                         unit_check_unneeded(other);
1150 }
1151
1152 void unit_trigger_on_failure(Unit *u) {
1153         Unit *other;
1154         Iterator i;
1155
1156         assert(u);
1157
1158         if (set_size(u->dependencies[UNIT_ON_FAILURE]) <= 0)
1159                 return;
1160
1161         log_info("Triggering OnFailure= dependencies of %s.", u->id);
1162
1163         SET_FOREACH(other, u->dependencies[UNIT_ON_FAILURE], i) {
1164                 int r;
1165
1166                 if ((r = manager_add_job(u->manager, JOB_START, other, u->on_failure_isolate ? JOB_ISOLATE : JOB_REPLACE, true, NULL, NULL)) < 0)
1167                         log_error("Failed to enqueue OnFailure= job: %s", strerror(-r));
1168         }
1169 }
1170
1171 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_success) {
1172         bool unexpected;
1173
1174         assert(u);
1175         assert(os < _UNIT_ACTIVE_STATE_MAX);
1176         assert(ns < _UNIT_ACTIVE_STATE_MAX);
1177
1178         /* Note that this is called for all low-level state changes,
1179          * even if they might map to the same high-level
1180          * UnitActiveState! That means that ns == os is OK an expected
1181          * behaviour here. For example: if a mount point is remounted
1182          * this function will be called too! */
1183
1184         if (u->manager->n_reloading <= 0) {
1185                 dual_timestamp ts;
1186
1187                 dual_timestamp_get(&ts);
1188
1189                 if (UNIT_IS_INACTIVE_OR_FAILED(os) && !UNIT_IS_INACTIVE_OR_FAILED(ns))
1190                         u->inactive_exit_timestamp = ts;
1191                 else if (!UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_INACTIVE_OR_FAILED(ns))
1192                         u->inactive_enter_timestamp = ts;
1193
1194                 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
1195                         u->active_enter_timestamp = ts;
1196                 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
1197                         u->active_exit_timestamp = ts;
1198
1199                 timer_unit_notify(u, ns);
1200                 path_unit_notify(u, ns);
1201         }
1202
1203         if (UNIT_IS_INACTIVE_OR_FAILED(ns))
1204                 cgroup_bonding_trim_list(u->cgroup_bondings, true);
1205
1206         if (u->job) {
1207                 unexpected = false;
1208
1209                 if (u->job->state == JOB_WAITING)
1210
1211                         /* So we reached a different state for this
1212                          * job. Let's see if we can run it now if it
1213                          * failed previously due to EAGAIN. */
1214                         job_add_to_run_queue(u->job);
1215
1216                 /* Let's check whether this state change constitutes a
1217                  * finished job, or maybe contradicts a running job and
1218                  * hence needs to invalidate jobs. */
1219
1220                 switch (u->job->type) {
1221
1222                 case JOB_START:
1223                 case JOB_VERIFY_ACTIVE:
1224
1225                         if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
1226                                 job_finish_and_invalidate(u->job, JOB_DONE);
1227                         else if (u->job->state == JOB_RUNNING && ns != UNIT_ACTIVATING) {
1228                                 unexpected = true;
1229
1230                                 if (UNIT_IS_INACTIVE_OR_FAILED(ns))
1231                                         job_finish_and_invalidate(u->job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE);
1232                         }
1233
1234                         break;
1235
1236                 case JOB_RELOAD:
1237                 case JOB_RELOAD_OR_START:
1238
1239                         if (u->job->state == JOB_RUNNING) {
1240                                 if (ns == UNIT_ACTIVE)
1241                                         job_finish_and_invalidate(u->job, reload_success ? JOB_DONE : JOB_FAILED);
1242                                 else if (ns != UNIT_ACTIVATING && ns != UNIT_RELOADING) {
1243                                         unexpected = true;
1244
1245                                         if (UNIT_IS_INACTIVE_OR_FAILED(ns))
1246                                                 job_finish_and_invalidate(u->job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE);
1247                                 }
1248                         }
1249
1250                         break;
1251
1252                 case JOB_STOP:
1253                 case JOB_RESTART:
1254                 case JOB_TRY_RESTART:
1255
1256                         if (UNIT_IS_INACTIVE_OR_FAILED(ns))
1257                                 job_finish_and_invalidate(u->job, JOB_DONE);
1258                         else if (u->job->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) {
1259                                 unexpected = true;
1260                                 job_finish_and_invalidate(u->job, JOB_FAILED);
1261                         }
1262
1263                         break;
1264
1265                 default:
1266                         assert_not_reached("Job type unknown");
1267                 }
1268
1269         } else
1270                 unexpected = true;
1271
1272         if (u->manager->n_reloading <= 0) {
1273
1274                 /* If this state change happened without being
1275                  * requested by a job, then let's retroactively start
1276                  * or stop dependencies. We skip that step when
1277                  * deserializing, since we don't want to create any
1278                  * additional jobs just because something is already
1279                  * activated. */
1280
1281                 if (unexpected) {
1282                         if (UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
1283                                 retroactively_start_dependencies(u);
1284                         else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
1285                                 retroactively_stop_dependencies(u);
1286                 }
1287
1288                 /* stop unneeded units regardless if going down was expected or not */
1289                 if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
1290                         check_unneeded_dependencies(u);
1291
1292                 if (ns != os && ns == UNIT_FAILED) {
1293                         log_notice("Unit %s entered failed state.", u->id);
1294                         unit_trigger_on_failure(u);
1295                 }
1296         }
1297
1298         /* Some names are special */
1299         if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
1300
1301                 if (unit_has_name(u, SPECIAL_DBUS_SERVICE))
1302                         /* The bus just might have become available,
1303                          * hence try to connect to it, if we aren't
1304                          * yet connected. */
1305                         bus_init(u->manager, true);
1306
1307                 if (u->type == UNIT_SERVICE &&
1308                     !UNIT_IS_ACTIVE_OR_RELOADING(os) &&
1309                     u->manager->n_reloading <= 0) {
1310                         /* Write audit record if we have just finished starting up */
1311                         manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_START, true);
1312                         u->in_audit = true;
1313                 }
1314
1315                 if (!UNIT_IS_ACTIVE_OR_RELOADING(os))
1316                         manager_send_unit_plymouth(u->manager, u);
1317
1318         } else {
1319
1320                 /* We don't care about D-Bus here, since we'll get an
1321                  * asynchronous notification for it anyway. */
1322
1323                 if (u->type == UNIT_SERVICE &&
1324                     UNIT_IS_INACTIVE_OR_FAILED(ns) &&
1325                     !UNIT_IS_INACTIVE_OR_FAILED(os) &&
1326                     u->manager->n_reloading <= 0) {
1327
1328                         /* Hmm, if there was no start record written
1329                          * write it now, so that we always have a nice
1330                          * pair */
1331                         if (!u->in_audit) {
1332                                 manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_START, ns == UNIT_INACTIVE);
1333
1334                                 if (ns == UNIT_INACTIVE)
1335                                         manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_STOP, true);
1336                         } else
1337                                 /* Write audit record if we have just finished shutting down */
1338                                 manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_STOP, ns == UNIT_INACTIVE);
1339
1340                         u->in_audit = false;
1341                 }
1342         }
1343
1344         manager_recheck_journal(u->manager);
1345
1346         /* Maybe we finished startup and are now ready for being
1347          * stopped because unneeded? */
1348         unit_check_unneeded(u);
1349
1350         unit_add_to_dbus_queue(u);
1351         unit_add_to_gc_queue(u);
1352 }
1353
1354 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
1355         struct epoll_event ev;
1356
1357         assert(u);
1358         assert(fd >= 0);
1359         assert(w);
1360         assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
1361
1362         zero(ev);
1363         ev.data.ptr = w;
1364         ev.events = events;
1365
1366         if (epoll_ctl(u->manager->epoll_fd,
1367                       w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
1368                       fd,
1369                       &ev) < 0)
1370                 return -errno;
1371
1372         w->fd = fd;
1373         w->type = WATCH_FD;
1374         w->data.unit = u;
1375
1376         return 0;
1377 }
1378
1379 void unit_unwatch_fd(Unit *u, Watch *w) {
1380         assert(u);
1381         assert(w);
1382
1383         if (w->type == WATCH_INVALID)
1384                 return;
1385
1386         assert(w->type == WATCH_FD);
1387         assert(w->data.unit == u);
1388         assert_se(epoll_ctl(u->manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1389
1390         w->fd = -1;
1391         w->type = WATCH_INVALID;
1392         w->data.unit = NULL;
1393 }
1394
1395 int unit_watch_pid(Unit *u, pid_t pid) {
1396         assert(u);
1397         assert(pid >= 1);
1398
1399         /* Watch a specific PID. We only support one unit watching
1400          * each PID for now. */
1401
1402         return hashmap_put(u->manager->watch_pids, LONG_TO_PTR(pid), u);
1403 }
1404
1405 void unit_unwatch_pid(Unit *u, pid_t pid) {
1406         assert(u);
1407         assert(pid >= 1);
1408
1409         hashmap_remove_value(u->manager->watch_pids, LONG_TO_PTR(pid), u);
1410 }
1411
1412 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
1413         struct itimerspec its;
1414         int flags, fd;
1415         bool ours;
1416
1417         assert(u);
1418         assert(w);
1419         assert(w->type == WATCH_INVALID || (w->type == WATCH_UNIT_TIMER && w->data.unit == u));
1420
1421         /* This will try to reuse the old timer if there is one */
1422
1423         if (w->type == WATCH_UNIT_TIMER) {
1424                 assert(w->data.unit == u);
1425                 assert(w->fd >= 0);
1426
1427                 ours = false;
1428                 fd = w->fd;
1429         } else if (w->type == WATCH_INVALID) {
1430
1431                 ours = true;
1432                 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
1433                         return -errno;
1434         } else
1435                 assert_not_reached("Invalid watch type");
1436
1437         zero(its);
1438
1439         if (delay <= 0) {
1440                 /* Set absolute time in the past, but not 0, since we
1441                  * don't want to disarm the timer */
1442                 its.it_value.tv_sec = 0;
1443                 its.it_value.tv_nsec = 1;
1444
1445                 flags = TFD_TIMER_ABSTIME;
1446         } else {
1447                 timespec_store(&its.it_value, delay);
1448                 flags = 0;
1449         }
1450
1451         /* This will also flush the elapse counter */
1452         if (timerfd_settime(fd, flags, &its, NULL) < 0)
1453                 goto fail;
1454
1455         if (w->type == WATCH_INVALID) {
1456                 struct epoll_event ev;
1457
1458                 zero(ev);
1459                 ev.data.ptr = w;
1460                 ev.events = EPOLLIN;
1461
1462                 if (epoll_ctl(u->manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
1463                         goto fail;
1464         }
1465
1466         w->type = WATCH_UNIT_TIMER;
1467         w->fd = fd;
1468         w->data.unit = u;
1469
1470         return 0;
1471
1472 fail:
1473         if (ours)
1474                 close_nointr_nofail(fd);
1475
1476         return -errno;
1477 }
1478
1479 void unit_unwatch_timer(Unit *u, Watch *w) {
1480         assert(u);
1481         assert(w);
1482
1483         if (w->type == WATCH_INVALID)
1484                 return;
1485
1486         assert(w->type == WATCH_UNIT_TIMER);
1487         assert(w->data.unit == u);
1488         assert(w->fd >= 0);
1489
1490         assert_se(epoll_ctl(u->manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1491         close_nointr_nofail(w->fd);
1492
1493         w->fd = -1;
1494         w->type = WATCH_INVALID;
1495         w->data.unit = NULL;
1496 }
1497
1498 bool unit_job_is_applicable(Unit *u, JobType j) {
1499         assert(u);
1500         assert(j >= 0 && j < _JOB_TYPE_MAX);
1501
1502         switch (j) {
1503
1504         case JOB_VERIFY_ACTIVE:
1505         case JOB_START:
1506         case JOB_STOP:
1507                 return true;
1508
1509         case JOB_RESTART:
1510         case JOB_TRY_RESTART:
1511                 return unit_can_start(u);
1512
1513         case JOB_RELOAD:
1514                 return unit_can_reload(u);
1515
1516         case JOB_RELOAD_OR_START:
1517                 return unit_can_reload(u) && unit_can_start(u);
1518
1519         default:
1520                 assert_not_reached("Invalid job type");
1521         }
1522 }
1523
1524 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference) {
1525
1526         static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
1527                 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
1528                 [UNIT_REQUIRES_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE,
1529                 [UNIT_WANTS] = UNIT_WANTED_BY,
1530                 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
1531                 [UNIT_REQUISITE_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE,
1532                 [UNIT_BIND_TO] = UNIT_BOUND_BY,
1533                 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1534                 [UNIT_REQUIRED_BY_OVERRIDABLE] = _UNIT_DEPENDENCY_INVALID,
1535                 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
1536                 [UNIT_BOUND_BY] = UNIT_BIND_TO,
1537                 [UNIT_CONFLICTS] = UNIT_CONFLICTED_BY,
1538                 [UNIT_CONFLICTED_BY] = UNIT_CONFLICTS,
1539                 [UNIT_BEFORE] = UNIT_AFTER,
1540                 [UNIT_AFTER] = UNIT_BEFORE,
1541                 [UNIT_ON_FAILURE] = _UNIT_DEPENDENCY_INVALID,
1542                 [UNIT_REFERENCES] = UNIT_REFERENCED_BY,
1543                 [UNIT_REFERENCED_BY] = UNIT_REFERENCES,
1544                 [UNIT_TRIGGERS] = UNIT_TRIGGERED_BY,
1545                 [UNIT_TRIGGERED_BY] = UNIT_TRIGGERS,
1546                 [UNIT_PROPAGATE_RELOAD_TO] = UNIT_PROPAGATE_RELOAD_FROM,
1547                 [UNIT_PROPAGATE_RELOAD_FROM] = UNIT_PROPAGATE_RELOAD_TO
1548         };
1549         int r, q = 0, v = 0, w = 0;
1550
1551         assert(u);
1552         assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
1553         assert(other);
1554
1555         u = unit_follow_merge(u);
1556         other = unit_follow_merge(other);
1557
1558         /* We won't allow dependencies on ourselves. We will not
1559          * consider them an error however. */
1560         if (u == other)
1561                 return 0;
1562
1563         if ((r = set_ensure_allocated(&u->dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
1564                 return r;
1565
1566         if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID)
1567                 if ((r = set_ensure_allocated(&other->dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
1568                         return r;
1569
1570         if (add_reference)
1571                 if ((r = set_ensure_allocated(&u->dependencies[UNIT_REFERENCES], trivial_hash_func, trivial_compare_func)) < 0 ||
1572                     (r = set_ensure_allocated(&other->dependencies[UNIT_REFERENCED_BY], trivial_hash_func, trivial_compare_func)) < 0)
1573                         return r;
1574
1575         if ((q = set_put(u->dependencies[d], other)) < 0)
1576                 return q;
1577
1578         if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID)
1579                 if ((v = set_put(other->dependencies[inverse_table[d]], u)) < 0) {
1580                         r = v;
1581                         goto fail;
1582                 }
1583
1584         if (add_reference) {
1585                 if ((w = set_put(u->dependencies[UNIT_REFERENCES], other)) < 0) {
1586                         r = w;
1587                         goto fail;
1588                 }
1589
1590                 if ((r = set_put(other->dependencies[UNIT_REFERENCED_BY], u)) < 0)
1591                         goto fail;
1592         }
1593
1594         unit_add_to_dbus_queue(u);
1595         return 0;
1596
1597 fail:
1598         if (q > 0)
1599                 set_remove(u->dependencies[d], other);
1600
1601         if (v > 0)
1602                 set_remove(other->dependencies[inverse_table[d]], u);
1603
1604         if (w > 0)
1605                 set_remove(u->dependencies[UNIT_REFERENCES], other);
1606
1607         return r;
1608 }
1609
1610 int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference) {
1611         int r;
1612
1613         assert(u);
1614
1615         if ((r = unit_add_dependency(u, d, other, add_reference)) < 0)
1616                 return r;
1617
1618         if ((r = unit_add_dependency(u, e, other, add_reference)) < 0)
1619                 return r;
1620
1621         return 0;
1622 }
1623
1624 static const char *resolve_template(Unit *u, const char *name, const char*path, char **p) {
1625         char *s;
1626
1627         assert(u);
1628         assert(name || path);
1629
1630         if (!name)
1631                 name = file_name_from_path(path);
1632
1633         if (!unit_name_is_template(name)) {
1634                 *p = NULL;
1635                 return name;
1636         }
1637
1638         if (u->instance)
1639                 s = unit_name_replace_instance(name, u->instance);
1640         else {
1641                 char *i;
1642
1643                 if (!(i = unit_name_to_prefix(u->id)))
1644                         return NULL;
1645
1646                 s = unit_name_replace_instance(name, i);
1647                 free(i);
1648         }
1649
1650         if (!s)
1651                 return NULL;
1652
1653         *p = s;
1654         return s;
1655 }
1656
1657 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
1658         Unit *other;
1659         int r;
1660         char *s;
1661
1662         assert(u);
1663         assert(name || path);
1664
1665         if (!(name = resolve_template(u, name, path, &s)))
1666                 return -ENOMEM;
1667
1668         if ((r = manager_load_unit(u->manager, name, path, NULL, &other)) < 0)
1669                 goto finish;
1670
1671         r = unit_add_dependency(u, d, other, add_reference);
1672
1673 finish:
1674         free(s);
1675         return r;
1676 }
1677
1678 int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
1679         Unit *other;
1680         int r;
1681         char *s;
1682
1683         assert(u);
1684         assert(name || path);
1685
1686         if (!(name = resolve_template(u, name, path, &s)))
1687                 return -ENOMEM;
1688
1689         if ((r = manager_load_unit(u->manager, name, path, NULL, &other)) < 0)
1690                 goto finish;
1691
1692         r = unit_add_two_dependencies(u, d, e, other, add_reference);
1693
1694 finish:
1695         free(s);
1696         return r;
1697 }
1698
1699 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
1700         Unit *other;
1701         int r;
1702         char *s;
1703
1704         assert(u);
1705         assert(name || path);
1706
1707         if (!(name = resolve_template(u, name, path, &s)))
1708                 return -ENOMEM;
1709
1710         if ((r = manager_load_unit(u->manager, name, path, NULL, &other)) < 0)
1711                 goto finish;
1712
1713         r = unit_add_dependency(other, d, u, add_reference);
1714
1715 finish:
1716         free(s);
1717         return r;
1718 }
1719
1720 int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
1721         Unit *other;
1722         int r;
1723         char *s;
1724
1725         assert(u);
1726         assert(name || path);
1727
1728         if (!(name = resolve_template(u, name, path, &s)))
1729                 return -ENOMEM;
1730
1731         if ((r = manager_load_unit(u->manager, name, path, NULL, &other)) < 0)
1732                 goto finish;
1733
1734         if ((r = unit_add_two_dependencies(other, d, e, u, add_reference)) < 0)
1735                 goto finish;
1736
1737 finish:
1738         free(s);
1739         return r;
1740 }
1741
1742 int set_unit_path(const char *p) {
1743         char *cwd, *c;
1744         int r;
1745
1746         /* This is mostly for debug purposes */
1747
1748         if (path_is_absolute(p)) {
1749                 if (!(c = strdup(p)))
1750                         return -ENOMEM;
1751         } else {
1752                 if (!(cwd = get_current_dir_name()))
1753                         return -errno;
1754
1755                 r = asprintf(&c, "%s/%s", cwd, p);
1756                 free(cwd);
1757
1758                 if (r < 0)
1759                         return -ENOMEM;
1760         }
1761
1762         if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
1763                 r = -errno;
1764                 free(c);
1765                 return r;
1766         }
1767
1768         return 0;
1769 }
1770
1771 char *unit_dbus_path(Unit *u) {
1772         char *p, *e;
1773
1774         assert(u);
1775
1776         if (!u->id)
1777                 return NULL;
1778
1779         if (!(e = bus_path_escape(u->id)))
1780                 return NULL;
1781
1782         p = strappend("/org/freedesktop/systemd1/unit/", e);
1783         free(e);
1784
1785         return p;
1786 }
1787
1788 int unit_add_cgroup(Unit *u, CGroupBonding *b) {
1789         int r;
1790
1791         assert(u);
1792         assert(b);
1793
1794         assert(b->path);
1795
1796         if (!b->controller) {
1797                 if (!(b->controller = strdup(SYSTEMD_CGROUP_CONTROLLER)))
1798                         return -ENOMEM;
1799
1800                 b->ours = true;
1801         }
1802
1803         /* Ensure this hasn't been added yet */
1804         assert(!b->unit);
1805
1806         if (streq(b->controller, SYSTEMD_CGROUP_CONTROLLER)) {
1807                 CGroupBonding *l;
1808
1809                 l = hashmap_get(u->manager->cgroup_bondings, b->path);
1810                 LIST_PREPEND(CGroupBonding, by_path, l, b);
1811
1812                 if ((r = hashmap_replace(u->manager->cgroup_bondings, b->path, l)) < 0) {
1813                         LIST_REMOVE(CGroupBonding, by_path, l, b);
1814                         return r;
1815                 }
1816         }
1817
1818         LIST_PREPEND(CGroupBonding, by_unit, u->cgroup_bondings, b);
1819         b->unit = u;
1820
1821         return 0;
1822 }
1823
1824 static char *default_cgroup_path(Unit *u) {
1825         char *p;
1826
1827         assert(u);
1828
1829         if (u->instance) {
1830                 char *t;
1831
1832                 t = unit_name_template(u->id);
1833                 if (!t)
1834                         return NULL;
1835
1836                 p = join(u->manager->cgroup_hierarchy, "/", t, "/", u->instance, NULL);
1837                 free(t);
1838         } else
1839                 p = join(u->manager->cgroup_hierarchy, "/", u->id, NULL);
1840
1841         return p;
1842 }
1843
1844 int unit_add_cgroup_from_text(Unit *u, const char *name) {
1845         char *controller = NULL, *path = NULL;
1846         CGroupBonding *b = NULL;
1847         bool ours = false;
1848         int r;
1849
1850         assert(u);
1851         assert(name);
1852
1853         if ((r = cg_split_spec(name, &controller, &path)) < 0)
1854                 return r;
1855
1856         if (!path) {
1857                 path = default_cgroup_path(u);
1858                 ours = true;
1859         }
1860
1861         if (!controller) {
1862                 controller = strdup(SYSTEMD_CGROUP_CONTROLLER);
1863                 ours = true;
1864         }
1865
1866         if (!path || !controller) {
1867                 free(path);
1868                 free(controller);
1869
1870                 return -ENOMEM;
1871         }
1872
1873         if (cgroup_bonding_find_list(u->cgroup_bondings, controller)) {
1874                 r = -EEXIST;
1875                 goto fail;
1876         }
1877
1878         if (!(b = new0(CGroupBonding, 1))) {
1879                 r = -ENOMEM;
1880                 goto fail;
1881         }
1882
1883         b->controller = controller;
1884         b->path = path;
1885         b->ours = ours;
1886         b->essential = streq(controller, SYSTEMD_CGROUP_CONTROLLER);
1887
1888         if ((r = unit_add_cgroup(u, b)) < 0)
1889                 goto fail;
1890
1891         return 0;
1892
1893 fail:
1894         free(path);
1895         free(controller);
1896         free(b);
1897
1898         return r;
1899 }
1900
1901 static int unit_add_one_default_cgroup(Unit *u, const char *controller) {
1902         CGroupBonding *b = NULL;
1903         int r = -ENOMEM;
1904
1905         assert(u);
1906
1907         if (!controller)
1908                 controller = SYSTEMD_CGROUP_CONTROLLER;
1909
1910         if (cgroup_bonding_find_list(u->cgroup_bondings, controller))
1911                 return 0;
1912
1913         if (!(b = new0(CGroupBonding, 1)))
1914                 return -ENOMEM;
1915
1916         if (!(b->controller = strdup(controller)))
1917                 goto fail;
1918
1919         if (!(b->path = default_cgroup_path(u)))
1920                 goto fail;
1921
1922         b->ours = true;
1923         b->essential = streq(controller, SYSTEMD_CGROUP_CONTROLLER);
1924
1925         if ((r = unit_add_cgroup(u, b)) < 0)
1926                 goto fail;
1927
1928         return 0;
1929
1930 fail:
1931         free(b->path);
1932         free(b->controller);
1933         free(b);
1934
1935         return r;
1936 }
1937
1938 int unit_add_default_cgroups(Unit *u) {
1939         CGroupAttribute *a;
1940         char **c;
1941         int r;
1942
1943         assert(u);
1944
1945         /* Adds in the default cgroups, if they weren't specified
1946          * otherwise. */
1947
1948         if (!u->manager->cgroup_hierarchy)
1949                 return 0;
1950
1951         if ((r = unit_add_one_default_cgroup(u, NULL)) < 0)
1952                 return r;
1953
1954         STRV_FOREACH(c, u->manager->default_controllers)
1955                 unit_add_one_default_cgroup(u, *c);
1956
1957         LIST_FOREACH(by_unit, a, u->cgroup_attributes)
1958                 unit_add_one_default_cgroup(u, a->controller);
1959
1960         return 0;
1961 }
1962
1963 CGroupBonding* unit_get_default_cgroup(Unit *u) {
1964         assert(u);
1965
1966         return cgroup_bonding_find_list(u->cgroup_bondings, SYSTEMD_CGROUP_CONTROLLER);
1967 }
1968
1969 int unit_add_cgroup_attribute(Unit *u, const char *controller, const char *name, const char *value, CGroupAttributeMapCallback map_callback) {
1970         int r;
1971         char *c = NULL;
1972         CGroupAttribute *a;
1973
1974         assert(u);
1975         assert(name);
1976         assert(value);
1977
1978         if (!controller) {
1979                 const char *dot;
1980
1981                 dot = strchr(name, '.');
1982                 if (!dot)
1983                         return -EINVAL;
1984
1985                 c = strndup(name, dot - name);
1986                 if (!c)
1987                         return -ENOMEM;
1988
1989                 controller = c;
1990         }
1991
1992         if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
1993                 r = -EINVAL;
1994                 goto finish;
1995         }
1996
1997         a = new0(CGroupAttribute, 1);
1998         if (!a) {
1999                 r = -ENOMEM;
2000                 goto finish;
2001         }
2002
2003         if (c) {
2004                 a->controller = c;
2005                 c = NULL;
2006         } else
2007                 a->controller = strdup(controller);
2008
2009         a->name = strdup(name);
2010         a->value = strdup(value);
2011
2012         if (!a->controller || !a->name || !a->value) {
2013                 free(a->controller);
2014                 free(a->name);
2015                 free(a->value);
2016                 free(a);
2017
2018                 return -ENOMEM;
2019         }
2020
2021         a->map_callback = map_callback;
2022
2023         LIST_PREPEND(CGroupAttribute, by_unit, u->cgroup_attributes, a);
2024
2025         r = 0;
2026
2027 finish:
2028         free(c);
2029         return r;
2030 }
2031
2032 int unit_load_related_unit(Unit *u, const char *type, Unit **_found) {
2033         char *t;
2034         int r;
2035
2036         assert(u);
2037         assert(type);
2038         assert(_found);
2039
2040         if (!(t = unit_name_change_suffix(u->id, type)))
2041                 return -ENOMEM;
2042
2043         assert(!unit_has_name(u, t));
2044
2045         r = manager_load_unit(u->manager, t, NULL, NULL, _found);
2046         free(t);
2047
2048         assert(r < 0 || *_found != u);
2049
2050         return r;
2051 }
2052
2053 int unit_get_related_unit(Unit *u, const char *type, Unit **_found) {
2054         Unit *found;
2055         char *t;
2056
2057         assert(u);
2058         assert(type);
2059         assert(_found);
2060
2061         if (!(t = unit_name_change_suffix(u->id, type)))
2062                 return -ENOMEM;
2063
2064         assert(!unit_has_name(u, t));
2065
2066         found = manager_get_unit(u->manager, t);
2067         free(t);
2068
2069         if (!found)
2070                 return -ENOENT;
2071
2072         *_found = found;
2073         return 0;
2074 }
2075
2076 static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) {
2077         Unit *u = userdata;
2078         assert(u);
2079
2080         return unit_name_to_prefix_and_instance(u->id);
2081 }
2082
2083 static char *specifier_prefix(char specifier, void *data, void *userdata) {
2084         Unit *u = userdata;
2085         assert(u);
2086
2087         return unit_name_to_prefix(u->id);
2088 }
2089
2090 static char *specifier_prefix_unescaped(char specifier, void *data, void *userdata) {
2091         Unit *u = userdata;
2092         char *p, *r;
2093
2094         assert(u);
2095
2096         if (!(p = unit_name_to_prefix(u->id)))
2097                 return NULL;
2098
2099         r = unit_name_unescape(p);
2100         free(p);
2101
2102         return r;
2103 }
2104
2105 static char *specifier_instance_unescaped(char specifier, void *data, void *userdata) {
2106         Unit *u = userdata;
2107         assert(u);
2108
2109         if (u->instance)
2110                 return unit_name_unescape(u->instance);
2111
2112         return strdup("");
2113 }
2114
2115 static char *specifier_filename(char specifier, void *data, void *userdata) {
2116         Unit *u = userdata;
2117         assert(u);
2118
2119         if (u->instance)
2120                 return unit_name_path_unescape(u->instance);
2121
2122         return unit_name_to_path(u->instance);
2123 }
2124
2125 static char *specifier_cgroup(char specifier, void *data, void *userdata) {
2126         Unit *u = userdata;
2127         assert(u);
2128
2129         return default_cgroup_path(u);
2130 }
2131
2132 static char *specifier_cgroup_root(char specifier, void *data, void *userdata) {
2133         Unit *u = userdata;
2134         char *p;
2135         assert(u);
2136
2137         if (specifier == 'r')
2138                 return strdup(u->manager->cgroup_hierarchy);
2139
2140         if (parent_of_path(u->manager->cgroup_hierarchy, &p) < 0)
2141                 return strdup("");
2142
2143         if (streq(p, "/")) {
2144                 free(p);
2145                 return strdup("");
2146         }
2147
2148         return p;
2149 }
2150
2151 static char *specifier_runtime(char specifier, void *data, void *userdata) {
2152         Unit *u = userdata;
2153         assert(u);
2154
2155         if (u->manager->running_as == MANAGER_USER) {
2156                 const char *e;
2157
2158                 e = getenv("XDG_RUNTIME_DIR");
2159                 if (e)
2160                         return strdup(e);
2161         }
2162
2163         return strdup("/run");
2164 }
2165
2166 char *unit_name_printf(Unit *u, const char* format) {
2167
2168         /*
2169          * This will use the passed string as format string and
2170          * replace the following specifiers:
2171          *
2172          * %n: the full id of the unit                 (foo@bar.waldo)
2173          * %N: the id of the unit without the suffix   (foo@bar)
2174          * %p: the prefix                              (foo)
2175          * %i: the instance                            (bar)
2176          */
2177
2178         const Specifier table[] = {
2179                 { 'n', specifier_string,              u->id },
2180                 { 'N', specifier_prefix_and_instance, NULL },
2181                 { 'p', specifier_prefix,              NULL },
2182                 { 'i', specifier_string,              u->instance },
2183                 { 0, NULL, NULL }
2184         };
2185
2186         assert(u);
2187         assert(format);
2188
2189         return specifier_printf(format, table, u);
2190 }
2191
2192 char *unit_full_printf(Unit *u, const char *format) {
2193
2194         /* This is similar to unit_name_printf() but also supports
2195          * unescaping. Also, adds a couple of additional codes:
2196          *
2197          * %c cgroup path of unit
2198          * %r root cgroup path of this systemd instance (e.g. "/user/lennart/shared/systemd-4711")
2199          * %R parent of root cgroup path (e.g. "/usr/lennart/shared")
2200          * %t the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR)
2201          */
2202
2203         const Specifier table[] = {
2204                 { 'n', specifier_string,              u->id },
2205                 { 'N', specifier_prefix_and_instance, NULL },
2206                 { 'p', specifier_prefix,              NULL },
2207                 { 'P', specifier_prefix_unescaped,    NULL },
2208                 { 'i', specifier_string,              u->instance },
2209                 { 'I', specifier_instance_unescaped,  NULL },
2210                 { 'f', specifier_filename,            NULL },
2211                 { 'c', specifier_cgroup,              NULL },
2212                 { 'r', specifier_cgroup_root,         NULL },
2213                 { 'R', specifier_cgroup_root,         NULL },
2214                 { 't', specifier_runtime,             NULL },
2215                 { 0, NULL, NULL }
2216         };
2217
2218         assert(u);
2219         assert(format);
2220
2221         return specifier_printf(format, table, u);
2222 }
2223
2224 char **unit_full_printf_strv(Unit *u, char **l) {
2225         size_t n;
2226         char **r, **i, **j;
2227
2228         /* Applies unit_full_printf to every entry in l */
2229
2230         assert(u);
2231
2232         n = strv_length(l);
2233         if (!(r = new(char*, n+1)))
2234                 return NULL;
2235
2236         for (i = l, j = r; *i; i++, j++)
2237                 if (!(*j = unit_full_printf(u, *i)))
2238                         goto fail;
2239
2240         *j = NULL;
2241         return r;
2242
2243 fail:
2244         for (j--; j >= r; j--)
2245                 free(*j);
2246
2247         free(r);
2248
2249         return NULL;
2250 }
2251
2252 int unit_watch_bus_name(Unit *u, const char *name) {
2253         assert(u);
2254         assert(name);
2255
2256         /* Watch a specific name on the bus. We only support one unit
2257          * watching each name for now. */
2258
2259         return hashmap_put(u->manager->watch_bus, name, u);
2260 }
2261
2262 void unit_unwatch_bus_name(Unit *u, const char *name) {
2263         assert(u);
2264         assert(name);
2265
2266         hashmap_remove_value(u->manager->watch_bus, name, u);
2267 }
2268
2269 bool unit_can_serialize(Unit *u) {
2270         assert(u);
2271
2272         return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item;
2273 }
2274
2275 int unit_serialize(Unit *u, FILE *f, FDSet *fds) {
2276         int r;
2277
2278         assert(u);
2279         assert(f);
2280         assert(fds);
2281
2282         if (!unit_can_serialize(u))
2283                 return 0;
2284
2285         if ((r = UNIT_VTABLE(u)->serialize(u, f, fds)) < 0)
2286                 return r;
2287
2288         if (u->job)
2289                 unit_serialize_item(u, f, "job", job_type_to_string(u->job->type));
2290
2291         dual_timestamp_serialize(f, "inactive-exit-timestamp", &u->inactive_exit_timestamp);
2292         dual_timestamp_serialize(f, "active-enter-timestamp", &u->active_enter_timestamp);
2293         dual_timestamp_serialize(f, "active-exit-timestamp", &u->active_exit_timestamp);
2294         dual_timestamp_serialize(f, "inactive-enter-timestamp", &u->inactive_enter_timestamp);
2295         dual_timestamp_serialize(f, "condition-timestamp", &u->condition_timestamp);
2296
2297         if (dual_timestamp_is_set(&u->condition_timestamp))
2298                 unit_serialize_item(u, f, "condition-result", yes_no(u->condition_result));
2299
2300         /* End marker */
2301         fputc('\n', f);
2302         return 0;
2303 }
2304
2305 void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *format, ...) {
2306         va_list ap;
2307
2308         assert(u);
2309         assert(f);
2310         assert(key);
2311         assert(format);
2312
2313         fputs(key, f);
2314         fputc('=', f);
2315
2316         va_start(ap, format);
2317         vfprintf(f, format, ap);
2318         va_end(ap);
2319
2320         fputc('\n', f);
2321 }
2322
2323 void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value) {
2324         assert(u);
2325         assert(f);
2326         assert(key);
2327         assert(value);
2328
2329         fprintf(f, "%s=%s\n", key, value);
2330 }
2331
2332 int unit_deserialize(Unit *u, FILE *f, FDSet *fds) {
2333         int r;
2334
2335         assert(u);
2336         assert(f);
2337         assert(fds);
2338
2339         if (!unit_can_serialize(u))
2340                 return 0;
2341
2342         for (;;) {
2343                 char line[LINE_MAX], *l, *v;
2344                 size_t k;
2345
2346                 if (!fgets(line, sizeof(line), f)) {
2347                         if (feof(f))
2348                                 return 0;
2349                         return -errno;
2350                 }
2351
2352                 char_array_0(line);
2353                 l = strstrip(line);
2354
2355                 /* End marker */
2356                 if (l[0] == 0)
2357                         return 0;
2358
2359                 k = strcspn(l, "=");
2360
2361                 if (l[k] == '=') {
2362                         l[k] = 0;
2363                         v = l+k+1;
2364                 } else
2365                         v = l+k;
2366
2367                 if (streq(l, "job")) {
2368                         JobType type;
2369
2370                         if ((type = job_type_from_string(v)) < 0)
2371                                 log_debug("Failed to parse job type value %s", v);
2372                         else
2373                                 u->deserialized_job = type;
2374
2375                         continue;
2376                 } else if (streq(l, "inactive-exit-timestamp")) {
2377                         dual_timestamp_deserialize(v, &u->inactive_exit_timestamp);
2378                         continue;
2379                 } else if (streq(l, "active-enter-timestamp")) {
2380                         dual_timestamp_deserialize(v, &u->active_enter_timestamp);
2381                         continue;
2382                 } else if (streq(l, "active-exit-timestamp")) {
2383                         dual_timestamp_deserialize(v, &u->active_exit_timestamp);
2384                         continue;
2385                 } else if (streq(l, "inactive-enter-timestamp")) {
2386                         dual_timestamp_deserialize(v, &u->inactive_enter_timestamp);
2387                         continue;
2388                 } else if (streq(l, "condition-timestamp")) {
2389                         dual_timestamp_deserialize(v, &u->condition_timestamp);
2390                         continue;
2391                 } else if (streq(l, "condition-result")) {
2392                         int b;
2393
2394                         if ((b = parse_boolean(v)) < 0)
2395                                 log_debug("Failed to parse condition result value %s", v);
2396                         else
2397                                 u->condition_result = b;
2398
2399                         continue;
2400                 }
2401
2402                 if ((r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds)) < 0)
2403                         return r;
2404         }
2405 }
2406
2407 int unit_add_node_link(Unit *u, const char *what, bool wants) {
2408         Unit *device;
2409         char *e;
2410         int r;
2411
2412         assert(u);
2413
2414         if (!what)
2415                 return 0;
2416
2417         /* Adds in links to the device node that this unit is based on */
2418
2419         if (!is_device_path(what))
2420                 return 0;
2421
2422         if (!(e = unit_name_build_escape(what+1, NULL, ".device")))
2423                 return -ENOMEM;
2424
2425         r = manager_load_unit(u->manager, e, NULL, NULL, &device);
2426         free(e);
2427
2428         if (r < 0)
2429                 return r;
2430
2431         if ((r = unit_add_two_dependencies(u, UNIT_AFTER, UNIT_BIND_TO, device, true)) < 0)
2432                 return r;
2433
2434         if (wants)
2435                 if ((r = unit_add_dependency(device, UNIT_WANTS, u, false)) < 0)
2436                         return r;
2437
2438         return 0;
2439 }
2440
2441 int unit_coldplug(Unit *u) {
2442         int r;
2443
2444         assert(u);
2445
2446         if (UNIT_VTABLE(u)->coldplug)
2447                 if ((r = UNIT_VTABLE(u)->coldplug(u)) < 0)
2448                         return r;
2449
2450         if (u->deserialized_job >= 0) {
2451                 if ((r = manager_add_job(u->manager, u->deserialized_job, u, JOB_IGNORE_REQUIREMENTS, false, NULL, NULL)) < 0)
2452                         return r;
2453
2454                 u->deserialized_job = _JOB_TYPE_INVALID;
2455         }
2456
2457         return 0;
2458 }
2459
2460 void unit_status_printf(Unit *u, const char *status, const char *format, ...) {
2461         va_list ap;
2462
2463         assert(u);
2464         assert(format);
2465
2466         if (!UNIT_VTABLE(u)->show_status)
2467                 return;
2468
2469         if (!manager_get_show_status(u->manager))
2470                 return;
2471
2472         if (!manager_is_booting_or_shutting_down(u->manager))
2473                 return;
2474
2475         va_start(ap, format);
2476         status_vprintf(status, true, format, ap);
2477         va_end(ap);
2478 }
2479
2480 bool unit_need_daemon_reload(Unit *u) {
2481         assert(u);
2482
2483         if (u->fragment_path) {
2484                 struct stat st;
2485
2486                 zero(st);
2487                 if (stat(u->fragment_path, &st) < 0)
2488                         /* What, cannot access this anymore? */
2489                         return true;
2490
2491                 if (u->fragment_mtime > 0 &&
2492                     timespec_load(&st.st_mtim) != u->fragment_mtime)
2493                         return true;
2494         }
2495
2496         if (UNIT_VTABLE(u)->need_daemon_reload)
2497                 return UNIT_VTABLE(u)->need_daemon_reload(u);
2498
2499         return false;
2500 }
2501
2502 void unit_reset_failed(Unit *u) {
2503         assert(u);
2504
2505         if (UNIT_VTABLE(u)->reset_failed)
2506                 UNIT_VTABLE(u)->reset_failed(u);
2507 }
2508
2509 Unit *unit_following(Unit *u) {
2510         assert(u);
2511
2512         if (UNIT_VTABLE(u)->following)
2513                 return UNIT_VTABLE(u)->following(u);
2514
2515         return NULL;
2516 }
2517
2518 bool unit_pending_inactive(Unit *u) {
2519         assert(u);
2520
2521         /* Returns true if the unit is inactive or going down */
2522
2523         if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)))
2524                 return true;
2525
2526         if (u->job && u->job->type == JOB_STOP)
2527                 return true;
2528
2529         return false;
2530 }
2531
2532 bool unit_pending_active(Unit *u) {
2533         assert(u);
2534
2535         /* Returns true if the unit is active or going up */
2536
2537         if (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
2538                 return true;
2539
2540         if (u->job &&
2541             (u->job->type == JOB_START ||
2542              u->job->type == JOB_RELOAD_OR_START ||
2543              u->job->type == JOB_RESTART))
2544                 return true;
2545
2546         return false;
2547 }
2548
2549 UnitType unit_name_to_type(const char *n) {
2550         UnitType t;
2551
2552         assert(n);
2553
2554         for (t = 0; t < _UNIT_TYPE_MAX; t++)
2555                 if (endswith(n, unit_vtable[t]->suffix))
2556                         return t;
2557
2558         return _UNIT_TYPE_INVALID;
2559 }
2560
2561 bool unit_name_is_valid(const char *n, bool template_ok) {
2562         UnitType t;
2563
2564         t = unit_name_to_type(n);
2565         if (t < 0 || t >= _UNIT_TYPE_MAX)
2566                 return false;
2567
2568         return unit_name_is_valid_no_type(n, template_ok);
2569 }
2570
2571 int unit_kill(Unit *u, KillWho w, KillMode m, int signo, DBusError *error) {
2572         assert(u);
2573         assert(w >= 0 && w < _KILL_WHO_MAX);
2574         assert(m >= 0 && m < _KILL_MODE_MAX);
2575         assert(signo > 0);
2576         assert(signo < _NSIG);
2577
2578         if (m == KILL_NONE)
2579                 return 0;
2580
2581         if (!UNIT_VTABLE(u)->kill)
2582                 return -ENOTSUP;
2583
2584         return UNIT_VTABLE(u)->kill(u, w, m, signo, error);
2585 }
2586
2587 int unit_following_set(Unit *u, Set **s) {
2588         assert(u);
2589         assert(s);
2590
2591         if (UNIT_VTABLE(u)->following_set)
2592                 return UNIT_VTABLE(u)->following_set(u, s);
2593
2594         *s = NULL;
2595         return 0;
2596 }
2597
2598 UnitFileState unit_get_unit_file_state(Unit *u) {
2599         assert(u);
2600
2601         if (u->unit_file_state < 0 && u->fragment_path)
2602                 u->unit_file_state = unit_file_get_state(
2603                                 u->manager->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER,
2604                                 NULL, file_name_from_path(u->fragment_path));
2605
2606         return u->unit_file_state;
2607 }
2608
2609 Unit* unit_ref_set(UnitRef *ref, Unit *u) {
2610         assert(ref);
2611         assert(u);
2612
2613         if (ref->unit)
2614                 unit_ref_unset(ref);
2615
2616         ref->unit = u;
2617         LIST_PREPEND(UnitRef, refs, u->refs, ref);
2618         return u;
2619 }
2620
2621 void unit_ref_unset(UnitRef *ref) {
2622         assert(ref);
2623
2624         if (!ref->unit)
2625                 return;
2626
2627         LIST_REMOVE(UnitRef, refs, ref->unit->refs, ref);
2628         ref->unit = NULL;
2629 }
2630
2631 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
2632         [UNIT_STUB] = "stub",
2633         [UNIT_LOADED] = "loaded",
2634         [UNIT_ERROR] = "error",
2635         [UNIT_MERGED] = "merged",
2636         [UNIT_MASKED] = "masked"
2637 };
2638
2639 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
2640
2641 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
2642         [UNIT_ACTIVE] = "active",
2643         [UNIT_RELOADING] = "reloading",
2644         [UNIT_INACTIVE] = "inactive",
2645         [UNIT_FAILED] = "failed",
2646         [UNIT_ACTIVATING] = "activating",
2647         [UNIT_DEACTIVATING] = "deactivating"
2648 };
2649
2650 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
2651
2652 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
2653         [UNIT_REQUIRES] = "Requires",
2654         [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable",
2655         [UNIT_WANTS] = "Wants",
2656         [UNIT_REQUISITE] = "Requisite",
2657         [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable",
2658         [UNIT_REQUIRED_BY] = "RequiredBy",
2659         [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable",
2660         [UNIT_BIND_TO] = "BindTo",
2661         [UNIT_WANTED_BY] = "WantedBy",
2662         [UNIT_CONFLICTS] = "Conflicts",
2663         [UNIT_CONFLICTED_BY] = "ConflictedBy",
2664         [UNIT_BOUND_BY] = "BoundBy",
2665         [UNIT_BEFORE] = "Before",
2666         [UNIT_AFTER] = "After",
2667         [UNIT_REFERENCES] = "References",
2668         [UNIT_REFERENCED_BY] = "ReferencedBy",
2669         [UNIT_ON_FAILURE] = "OnFailure",
2670         [UNIT_TRIGGERS] = "Triggers",
2671         [UNIT_TRIGGERED_BY] = "TriggeredBy",
2672         [UNIT_PROPAGATE_RELOAD_TO] = "PropagateReloadTo",
2673         [UNIT_PROPAGATE_RELOAD_FROM] = "PropagateReloadFrom"
2674 };
2675
2676 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);