chiark / gitweb /
device: ignore a couple of 'API' devices
[elogind.git] / unit.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU 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
31 #include "set.h"
32 #include "unit.h"
33 #include "macro.h"
34 #include "strv.h"
35 #include "load-fragment.h"
36 #include "load-dropin.h"
37 #include "log.h"
38
39 const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
40         [UNIT_SERVICE] = &service_vtable,
41         [UNIT_TIMER] = &timer_vtable,
42         [UNIT_SOCKET] = &socket_vtable,
43         [UNIT_TARGET] = &target_vtable,
44         [UNIT_DEVICE] = &device_vtable,
45         [UNIT_MOUNT] = &mount_vtable,
46         [UNIT_AUTOMOUNT] = &automount_vtable,
47         [UNIT_SNAPSHOT] = &snapshot_vtable
48 };
49
50 UnitType unit_name_to_type(const char *n) {
51         UnitType t;
52
53         assert(n);
54
55         for (t = 0; t < _UNIT_TYPE_MAX; t++)
56                 if (endswith(n, unit_vtable[t]->suffix))
57                         return t;
58
59         return _UNIT_TYPE_INVALID;
60 }
61
62 #define VALID_CHARS                             \
63         "0123456789"                            \
64         "abcdefghijklmnopqrstuvwxyz"            \
65         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"            \
66         "-_.\\"
67
68 bool unit_name_is_valid(const char *n) {
69         UnitType t;
70         const char *e, *i;
71
72         assert(n);
73
74         if (strlen(n) >= UNIT_NAME_MAX)
75                 return false;
76
77         t = unit_name_to_type(n);
78         if (t < 0 || t >= _UNIT_TYPE_MAX)
79                 return false;
80
81         if (!(e = strrchr(n, '.')))
82                 return false;
83
84         if (e == n)
85                 return false;
86
87         for (i = n; i < e; i++)
88                 if (!strchr(VALID_CHARS, *i))
89                         return false;
90
91         return true;
92 }
93
94 char *unit_name_change_suffix(const char *n, const char *suffix) {
95         char *e, *r;
96         size_t a, b;
97
98         assert(n);
99         assert(unit_name_is_valid(n));
100         assert(suffix);
101
102         assert_se(e = strrchr(n, '.'));
103         a = e - n;
104         b = strlen(suffix);
105
106         if (!(r = new(char, a + b + 1)))
107                 return NULL;
108
109         memcpy(r, n, a);
110         memcpy(r+a, suffix, b+1);
111
112         return r;
113 }
114
115 Unit *unit_new(Manager *m) {
116         Unit *u;
117
118         assert(m);
119
120         if (!(u = new0(Unit, 1)))
121                 return NULL;
122
123         if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) {
124                 free(u);
125                 return NULL;
126         }
127
128         u->meta.manager = m;
129         u->meta.type = _UNIT_TYPE_INVALID;
130
131         return u;
132 }
133
134 bool unit_has_name(Unit *u, const char *name) {
135         assert(u);
136         assert(name);
137
138         return !!set_get(u->meta.names, (char*) name);
139 }
140
141 int unit_add_name(Unit *u, const char *text) {
142         UnitType t;
143         char *s;
144         int r;
145
146         assert(u);
147         assert(text);
148
149         if (!unit_name_is_valid(text))
150                 return -EINVAL;
151
152         if ((t = unit_name_to_type(text)) == _UNIT_TYPE_INVALID)
153                 return -EINVAL;
154
155         if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type)
156                 return -EINVAL;
157
158         if (!(s = strdup(text)))
159                 return -ENOMEM;
160
161         if ((r = set_put(u->meta.names, s)) < 0) {
162                 free(s);
163
164                 if (r == -EEXIST)
165                         return 0;
166
167                 return r;
168         }
169
170         if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
171                 set_remove(u->meta.names, s);
172                 free(s);
173                 return r;
174         }
175
176         if (u->meta.type == _UNIT_TYPE_INVALID)
177                 LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);
178
179         u->meta.type = t;
180
181         if (!u->meta.id)
182                 u->meta.id = s;
183
184         unit_add_to_dbus_queue(u);
185         return 0;
186 }
187
188 int unit_choose_id(Unit *u, const char *name) {
189         char *s;
190
191         assert(u);
192         assert(name);
193
194         /* Selects one of the names of this unit as the id */
195
196         if (!(s = set_get(u->meta.names, (char*) name)))
197                 return -ENOENT;
198
199         u->meta.id = s;
200
201         unit_add_to_dbus_queue(u);
202         return 0;
203 }
204
205 int unit_set_description(Unit *u, const char *description) {
206         char *s;
207
208         assert(u);
209
210         if (!(s = strdup(description)))
211                 return -ENOMEM;
212
213         free(u->meta.description);
214         u->meta.description = s;
215
216         unit_add_to_dbus_queue(u);
217         return 0;
218 }
219
220 void unit_add_to_load_queue(Unit *u) {
221         assert(u);
222         assert(u->meta.type != _UNIT_TYPE_INVALID);
223
224         if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
225                 return;
226
227         LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
228         u->meta.in_load_queue = true;
229 }
230
231 void unit_add_to_cleanup_queue(Unit *u) {
232         assert(u);
233
234         if (u->meta.in_cleanup_queue)
235                 return;
236
237         LIST_PREPEND(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
238         u->meta.in_cleanup_queue = true;
239 }
240
241 void unit_add_to_dbus_queue(Unit *u) {
242         assert(u);
243         assert(u->meta.type != _UNIT_TYPE_INVALID);
244
245         if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue || set_isempty(u->meta.manager->subscribed))
246                 return;
247
248         LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
249         u->meta.in_dbus_queue = true;
250 }
251
252 static void bidi_set_free(Unit *u, Set *s) {
253         Iterator i;
254         Unit *other;
255
256         assert(u);
257
258         /* Frees the set and makes sure we are dropped from the
259          * inverse pointers */
260
261         SET_FOREACH(other, s, i) {
262                 UnitDependency d;
263
264                 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
265                         set_remove(other->meta.dependencies[d], u);
266         }
267
268         set_free(s);
269 }
270
271 void unit_free(Unit *u) {
272         UnitDependency d;
273         Iterator i;
274         char *t;
275
276         assert(u);
277
278         bus_unit_send_removed_signal(u);
279
280         /* Detach from next 'bigger' objects */
281
282         cgroup_bonding_free_list(u->meta.cgroup_bondings);
283
284         SET_FOREACH(t, u->meta.names, i)
285                 hashmap_remove_value(u->meta.manager->units, t, u);
286
287         if (u->meta.type != _UNIT_TYPE_INVALID)
288                 LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta);
289
290         if (u->meta.in_load_queue)
291                 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
292
293         if (u->meta.in_dbus_queue)
294                 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
295
296         if (u->meta.in_cleanup_queue)
297                 LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
298
299         if (u->meta.load_state != UNIT_STUB)
300                 if (UNIT_VTABLE(u)->done)
301                         UNIT_VTABLE(u)->done(u);
302
303         /* Free data and next 'smaller' objects */
304         if (u->meta.job)
305                 job_free(u->meta.job);
306
307         for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
308                 bidi_set_free(u, u->meta.dependencies[d]);
309
310         free(u->meta.description);
311         free(u->meta.fragment_path);
312
313         while ((t = set_steal_first(u->meta.names)))
314                 free(t);
315         set_free(u->meta.names);
316
317         free(u);
318 }
319
320 UnitActiveState unit_active_state(Unit *u) {
321         assert(u);
322
323         if (u->meta.load_state != UNIT_LOADED)
324                 return UNIT_INACTIVE;
325
326         return UNIT_VTABLE(u)->active_state(u);
327 }
328
329 static void complete_move(Set **s, Set **other) {
330         assert(s);
331         assert(other);
332
333         if (!*other)
334                 return;
335
336         if (*s)
337                 set_move(*s, *other);
338         else {
339                 *s = *other;
340                 *other = NULL;
341         }
342 }
343
344 static void merge_names(Unit *u, Unit *other) {
345         char *t;
346         Iterator i;
347
348         assert(u);
349         assert(other);
350
351         complete_move(&u->meta.names, &other->meta.names);
352
353         while ((t = set_steal_first(other->meta.names)))
354                 free(t);
355
356         set_free(other->meta.names);
357         other->meta.names = NULL;
358         other->meta.id = NULL;
359
360         SET_FOREACH(t, u->meta.names, i)
361                 assert_se(hashmap_replace(u->meta.manager->units, t, u) == 0);
362 }
363
364 static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
365         Iterator i;
366         Unit *back;
367         int r;
368
369         assert(u);
370         assert(other);
371         assert(d < _UNIT_DEPENDENCY_MAX);
372
373         SET_FOREACH(back, other->meta.dependencies[d], i) {
374                 UnitDependency k;
375
376                 for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++)
377                         if ((r = set_remove_and_put(back->meta.dependencies[k], other, u)) < 0) {
378
379                                 if (r == -EEXIST)
380                                         set_remove(back->meta.dependencies[k], other);
381                                 else
382                                         assert(r == -ENOENT);
383                         }
384         }
385
386         complete_move(&u->meta.dependencies[d], &other->meta.dependencies[d]);
387
388         set_free(other->meta.dependencies[d]);
389         other->meta.dependencies[d] = NULL;
390 }
391
392 int unit_merge(Unit *u, Unit *other) {
393         UnitDependency d;
394
395         assert(u);
396         assert(other);
397         assert(u->meta.manager == other->meta.manager);
398
399         other = unit_follow_merge(other);
400
401         if (other == u)
402                 return 0;
403
404         /* This merges 'other' into 'unit'. FIXME: This does not
405          * rollback on failure. */
406
407         if (u->meta.type != u->meta.type)
408                 return -EINVAL;
409
410         if (other->meta.load_state != UNIT_STUB &&
411             other->meta.load_state != UNIT_FAILED)
412                 return -EEXIST;
413
414         if (other->meta.job)
415                 return -EEXIST;
416
417         if (unit_active_state(other) != UNIT_INACTIVE)
418                 return -EEXIST;
419
420         /* Merge names */
421         merge_names(u, other);
422
423         /* Merge dependencies */
424         for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
425                 merge_dependencies(u, other, d);
426
427         other->meta.load_state = UNIT_MERGED;
428         other->meta.merged_into = u;
429
430         /* If there is still some data attached to the other node, we
431          * don't need it anymore, and can free it. */
432         if (other->meta.load_state != UNIT_STUB)
433                 if (UNIT_VTABLE(other)->done)
434                         UNIT_VTABLE(other)->done(other);
435
436         unit_add_to_dbus_queue(u);
437         unit_add_to_cleanup_queue(other);
438
439         return 0;
440 }
441
442 int unit_merge_by_name(Unit *u, const char *name) {
443         Unit *other;
444
445         assert(u);
446         assert(name);
447
448         if (!(other = manager_get_unit(u->meta.manager, name)))
449                 return unit_add_name(u, name);
450
451         return unit_merge(u, other);
452 }
453
454 Unit* unit_follow_merge(Unit *u) {
455         assert(u);
456
457         while (u->meta.load_state == UNIT_MERGED)
458                 assert_se(u = u->meta.merged_into);
459
460         return u;
461 }
462
463 int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
464         int r;
465
466         assert(u);
467         assert(c);
468
469         if (c->output != EXEC_OUTPUT_KERNEL && c->output != EXEC_OUTPUT_SYSLOG)
470                 return 0;
471
472         /* If syslog or kernel logging is requested, make sure our own
473          * logging daemon is run first. */
474
475         if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET)) < 0)
476                 return r;
477
478         if (u->meta.manager->running_as != MANAGER_SESSION)
479                 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET)) < 0)
480                         return r;
481
482         return 0;
483 }
484
485 const char* unit_id(Unit *u) {
486         assert(u);
487
488         if (u->meta.id)
489                 return u->meta.id;
490
491         return set_first(u->meta.names);
492 }
493
494 const char *unit_description(Unit *u) {
495         assert(u);
496
497         if (u->meta.description)
498                 return u->meta.description;
499
500         return unit_id(u);
501 }
502
503 void unit_dump(Unit *u, FILE *f, const char *prefix) {
504         char *t;
505         UnitDependency d;
506         Iterator i;
507         char *p2;
508         const char *prefix2;
509         CGroupBonding *b;
510         char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
511
512         assert(u);
513
514         if (!prefix)
515                 prefix = "";
516         p2 = strappend(prefix, "\t");
517         prefix2 = p2 ? p2 : prefix;
518
519         fprintf(f,
520                 "%s→ Unit %s:\n"
521                 "%s\tDescription: %s\n"
522                 "%s\tUnit Load State: %s\n"
523                 "%s\tUnit Active State: %s\n"
524                 "%s\tActive Enter Timestamp: %s\n"
525                 "%s\tActive Exit Timestamp: %s\n",
526                 prefix, unit_id(u),
527                 prefix, unit_description(u),
528                 prefix, unit_load_state_to_string(u->meta.load_state),
529                 prefix, unit_active_state_to_string(unit_active_state(u)),
530                 prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.active_enter_timestamp)),
531                 prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->meta.active_exit_timestamp)));
532
533         SET_FOREACH(t, u->meta.names, i)
534                 fprintf(f, "%s\tName: %s\n", prefix, t);
535
536         if (u->meta.fragment_path)
537                 fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);
538
539         for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
540                 Unit *other;
541
542                 SET_FOREACH(other, u->meta.dependencies[d], i)
543                         fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), unit_id(other));
544         }
545
546         if (u->meta.load_state == UNIT_LOADED) {
547                 fprintf(f,
548                         "%s\tRecursive Stop: %s\n"
549                         "%s\tStop When Unneeded: %s\n",
550                         prefix, yes_no(u->meta.recursive_stop),
551                         prefix, yes_no(u->meta.stop_when_unneeded));
552
553                 LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
554                         fprintf(f, "%s\tControlGroup: %s:%s\n",
555                                 prefix, b->controller, b->path);
556
557                 if (UNIT_VTABLE(u)->dump)
558                         UNIT_VTABLE(u)->dump(u, f, prefix2);
559
560         } else if (u->meta.load_state == UNIT_MERGED)
561                 fprintf(f,
562                         "%s\tMerged into: %s\n",
563                         prefix, unit_id(u->meta.merged_into));
564
565         if (u->meta.job)
566                 job_dump(u->meta.job, f, prefix2);
567
568         free(p2);
569 }
570
571 /* Common implementation for multiple backends */
572 int unit_load_fragment_and_dropin(Unit *u, UnitLoadState *new_state) {
573         int r;
574
575         assert(u);
576         assert(new_state);
577         assert(*new_state == UNIT_STUB || *new_state == UNIT_LOADED);
578
579         /* Load a .service file */
580         if ((r = unit_load_fragment(u, new_state)) < 0)
581                 return r;
582
583         if (*new_state == UNIT_STUB)
584                 return -ENOENT;
585
586         /* Load drop-in directory data */
587         if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
588                 return r;
589
590         return 0;
591 }
592
593 /* Common implementation for multiple backends */
594 int unit_load_fragment_and_dropin_optional(Unit *u, UnitLoadState *new_state) {
595         int r;
596
597         assert(u);
598         assert(new_state);
599         assert(*new_state == UNIT_STUB || *new_state == UNIT_LOADED);
600
601         /* Same as unit_load_fragment_and_dropin(), but whether
602          * something can be loaded or not doesn't matter. */
603
604         /* Load a .service file */
605         if ((r = unit_load_fragment(u, new_state)) < 0)
606                 return r;
607
608         if (*new_state == UNIT_STUB)
609                 *new_state = UNIT_LOADED;
610
611         /* Load drop-in directory data */
612         if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
613                 return r;
614
615         return 0;
616 }
617
618 int unit_load(Unit *u) {
619         int r;
620         UnitLoadState res;
621
622         assert(u);
623
624         if (u->meta.in_load_queue) {
625                 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
626                 u->meta.in_load_queue = false;
627         }
628
629         if (u->meta.load_state != UNIT_STUB)
630                 return 0;
631
632         if (UNIT_VTABLE(u)->init) {
633                 res = UNIT_STUB;
634                 if ((r = UNIT_VTABLE(u)->init(u, &res)) < 0)
635                         goto fail;
636         }
637
638         if (res == UNIT_STUB) {
639                 r = -ENOENT;
640                 goto fail;
641         }
642
643         u->meta.load_state = res;
644         assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);
645
646         unit_add_to_dbus_queue(unit_follow_merge(u));
647
648         return 0;
649
650 fail:
651         u->meta.load_state = UNIT_FAILED;
652         unit_add_to_dbus_queue(u);
653
654         log_error("Failed to load configuration for %s: %s", unit_id(u), strerror(-r));
655
656         return r;
657 }
658
659 /* Errors:
660  *         -EBADR:    This unit type does not support starting.
661  *         -EALREADY: Unit is already started.
662  *         -EAGAIN:   An operation is already in progress. Retry later.
663  */
664 int unit_start(Unit *u) {
665         UnitActiveState state;
666
667         assert(u);
668
669         /* If this is already (being) started, then this will
670          * succeed. Note that this will even succeed if this unit is
671          * not startable by the user. This is relied on to detect when
672          * we need to wait for units and when waiting is finished. */
673         state = unit_active_state(u);
674         if (UNIT_IS_ACTIVE_OR_RELOADING(state))
675                 return -EALREADY;
676
677         /* If it is stopped, but we cannot start it, then fail */
678         if (!UNIT_VTABLE(u)->start)
679                 return -EBADR;
680
681         /* We don't suppress calls to ->start() here when we are
682          * already starting, to allow this request to be used as a
683          * "hurry up" call, for example when the unit is in some "auto
684          * restart" state where it waits for a holdoff timer to elapse
685          * before it will start again. */
686
687         unit_add_to_dbus_queue(u);
688         return UNIT_VTABLE(u)->start(u);
689 }
690
691 bool unit_can_start(Unit *u) {
692         assert(u);
693
694         return !!UNIT_VTABLE(u)->start;
695 }
696
697 /* Errors:
698  *         -EBADR:    This unit type does not support stopping.
699  *         -EALREADY: Unit is already stopped.
700  *         -EAGAIN:   An operation is already in progress. Retry later.
701  */
702 int unit_stop(Unit *u) {
703         UnitActiveState state;
704
705         assert(u);
706
707         state = unit_active_state(u);
708         if (state == UNIT_INACTIVE)
709                 return -EALREADY;
710
711         if (!UNIT_VTABLE(u)->stop)
712                 return -EBADR;
713
714         if (state == UNIT_DEACTIVATING)
715                 return 0;
716
717         unit_add_to_dbus_queue(u);
718         return UNIT_VTABLE(u)->stop(u);
719 }
720
721 /* Errors:
722  *         -EBADR:    This unit type does not support reloading.
723  *         -ENOEXEC:  Unit is not started.
724  *         -EAGAIN:   An operation is already in progress. Retry later.
725  */
726 int unit_reload(Unit *u) {
727         UnitActiveState state;
728
729         assert(u);
730
731         if (!unit_can_reload(u))
732                 return -EBADR;
733
734         state = unit_active_state(u);
735         if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
736                 return -EALREADY;
737
738         if (unit_active_state(u) != UNIT_ACTIVE)
739                 return -ENOEXEC;
740
741         unit_add_to_dbus_queue(u);
742         return UNIT_VTABLE(u)->reload(u);
743 }
744
745 bool unit_can_reload(Unit *u) {
746         assert(u);
747
748         if (!UNIT_VTABLE(u)->reload)
749                 return false;
750
751         if (!UNIT_VTABLE(u)->can_reload)
752                 return true;
753
754         return UNIT_VTABLE(u)->can_reload(u);
755 }
756
757 static void unit_check_uneeded(Unit *u) {
758         Iterator i;
759         Unit *other;
760
761         assert(u);
762
763         /* If this service shall be shut down when unneeded then do
764          * so. */
765
766         if (!u->meta.stop_when_unneeded)
767                 return;
768
769         if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
770                 return;
771
772         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
773                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
774                         return;
775
776         SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
777                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
778                         return;
779
780         SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
781                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
782                         return;
783
784         log_debug("Service %s is not needed anymore. Stopping.", unit_id(u));
785
786         /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
787         manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL);
788 }
789
790 static void retroactively_start_dependencies(Unit *u) {
791         Iterator i;
792         Unit *other;
793
794         assert(u);
795         assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
796
797         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
798                 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
799                         manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
800
801         SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
802                 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
803                         manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
804
805         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
806                 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
807                         manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
808
809         SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
810                 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
811                         manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
812
813         SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
814                 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
815                         manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
816 }
817
818 static void retroactively_stop_dependencies(Unit *u) {
819         Iterator i;
820         Unit *other;
821
822         assert(u);
823         assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
824
825         if (u->meta.recursive_stop) {
826                 /* Pull down units need us recursively if enabled */
827                 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
828                         if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
829                                 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
830         }
831
832         /* Garbage collect services that might not be needed anymore, if enabled */
833         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
834                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
835                         unit_check_uneeded(other);
836         SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
837                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
838                         unit_check_uneeded(other);
839         SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
840                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
841                         unit_check_uneeded(other);
842         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
843                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
844                         unit_check_uneeded(other);
845         SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUISITE], i)
846                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
847                         unit_check_uneeded(other);
848 }
849
850 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
851         bool unexpected = false;
852
853         assert(u);
854         assert(os < _UNIT_ACTIVE_STATE_MAX);
855         assert(ns < _UNIT_ACTIVE_STATE_MAX);
856         assert(!(os == UNIT_ACTIVE && ns == UNIT_ACTIVATING));
857         assert(!(os == UNIT_INACTIVE && ns == UNIT_DEACTIVATING));
858
859         /* Note that this is called for all low-level state changes,
860          * even if they might map to the same high-level
861          * UnitActiveState! That means that ns == os is OK an expected
862          * behaviour here. For example: if a mount point is remounted
863          * this function will be called too and the utmp code below
864          * relies on that! */
865
866         if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
867                 u->meta.active_enter_timestamp = now(CLOCK_REALTIME);
868         else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
869                 u->meta.active_exit_timestamp = now(CLOCK_REALTIME);
870
871         if (u->meta.job) {
872
873                 if (u->meta.job->state == JOB_WAITING)
874
875                         /* So we reached a different state for this
876                          * job. Let's see if we can run it now if it
877                          * failed previously due to EAGAIN. */
878                         job_add_to_run_queue(u->meta.job);
879
880                 else {
881                         assert(u->meta.job->state == JOB_RUNNING);
882
883                         /* Let's check whether this state change
884                          * constitutes a finished job, or maybe
885                          * cotradicts a running job and hence needs to
886                          * invalidate jobs. */
887
888                         switch (u->meta.job->type) {
889
890                         case JOB_START:
891                         case JOB_VERIFY_ACTIVE:
892
893                                 if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
894                                         job_finish_and_invalidate(u->meta.job, true);
895                                 else if (ns != UNIT_ACTIVATING) {
896                                         unexpected = true;
897                                         job_finish_and_invalidate(u->meta.job, false);
898                                 }
899
900                                 break;
901
902                         case JOB_RELOAD:
903                         case JOB_RELOAD_OR_START:
904
905                                 if (ns == UNIT_ACTIVE)
906                                         job_finish_and_invalidate(u->meta.job, true);
907                                 else if (ns != UNIT_ACTIVATING && ns != UNIT_ACTIVE_RELOADING) {
908                                         unexpected = true;
909                                         job_finish_and_invalidate(u->meta.job, false);
910                                 }
911
912                                 break;
913
914                         case JOB_STOP:
915                         case JOB_RESTART:
916                         case JOB_TRY_RESTART:
917
918                                 if (ns == UNIT_INACTIVE)
919                                         job_finish_and_invalidate(u->meta.job, true);
920                                 else if (ns != UNIT_DEACTIVATING) {
921                                         unexpected = true;
922                                         job_finish_and_invalidate(u->meta.job, false);
923                                 }
924
925                                 break;
926
927                         default:
928                                 assert_not_reached("Job type unknown");
929                         }
930                 }
931         }
932
933         /* If this state change happened without being requested by a
934          * job, then let's retroactively start or stop dependencies */
935
936         if (unexpected) {
937                 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
938                         retroactively_start_dependencies(u);
939                 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
940                         retroactively_stop_dependencies(u);
941         }
942
943         if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
944
945                 if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) {
946                         log_info("D-Bus became available, trying to reconnect.");
947                         /* The bus just got started, hence try to connect to it. */
948                         bus_init_system(u->meta.manager);
949                         bus_init_api(u->meta.manager);
950                 }
951
952                 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE)) {
953                         /* The syslog daemon just got started, hence try to connect to it. */
954                         log_info("Syslog became available, trying to reconnect.");
955                         log_open_syslog();
956                 }
957
958         } else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
959
960                 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
961                         /* The syslog daemon just got terminated, hence try to disconnect from it. */
962                         log_close_syslog();
963
964                 /* We don't care about D-Bus here, since we'll get an
965                  * asynchronous notification for it anyway. */
966         }
967
968         /* Maybe we finished startup and are now ready for being
969          * stopped because unneeded? */
970         unit_check_uneeded(u);
971
972         unit_add_to_dbus_queue(u);
973 }
974
975 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
976         struct epoll_event ev;
977
978         assert(u);
979         assert(fd >= 0);
980         assert(w);
981         assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
982
983         zero(ev);
984         ev.data.ptr = w;
985         ev.events = events;
986
987         if (epoll_ctl(u->meta.manager->epoll_fd,
988                       w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
989                       fd,
990                       &ev) < 0)
991                 return -errno;
992
993         w->fd = fd;
994         w->type = WATCH_FD;
995         w->data.unit = u;
996
997         return 0;
998 }
999
1000 void unit_unwatch_fd(Unit *u, Watch *w) {
1001         assert(u);
1002         assert(w);
1003
1004         if (w->type == WATCH_INVALID)
1005                 return;
1006
1007         assert(w->type == WATCH_FD);
1008         assert(w->data.unit == u);
1009         assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1010
1011         w->fd = -1;
1012         w->type = WATCH_INVALID;
1013         w->data.unit = NULL;
1014 }
1015
1016 int unit_watch_pid(Unit *u, pid_t pid) {
1017         assert(u);
1018         assert(pid >= 1);
1019
1020         return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
1021 }
1022
1023 void unit_unwatch_pid(Unit *u, pid_t pid) {
1024         assert(u);
1025         assert(pid >= 1);
1026
1027         hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
1028 }
1029
1030 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
1031         struct itimerspec its;
1032         int flags, fd;
1033         bool ours;
1034
1035         assert(u);
1036         assert(w);
1037         assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->data.unit == u));
1038
1039         /* This will try to reuse the old timer if there is one */
1040
1041         if (w->type == WATCH_TIMER) {
1042                 ours = false;
1043                 fd = w->fd;
1044         } else {
1045                 ours = true;
1046                 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
1047                         return -errno;
1048         }
1049
1050         zero(its);
1051
1052         if (delay <= 0) {
1053                 /* Set absolute time in the past, but not 0, since we
1054                  * don't want to disarm the timer */
1055                 its.it_value.tv_sec = 0;
1056                 its.it_value.tv_nsec = 1;
1057
1058                 flags = TFD_TIMER_ABSTIME;
1059         } else {
1060                 timespec_store(&its.it_value, delay);
1061                 flags = 0;
1062         }
1063
1064         /* This will also flush the elapse counter */
1065         if (timerfd_settime(fd, flags, &its, NULL) < 0)
1066                 goto fail;
1067
1068         if (w->type == WATCH_INVALID) {
1069                 struct epoll_event ev;
1070
1071                 zero(ev);
1072                 ev.data.ptr = w;
1073                 ev.events = EPOLLIN;
1074
1075                 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
1076                         goto fail;
1077         }
1078
1079         w->fd = fd;
1080         w->type = WATCH_TIMER;
1081         w->data.unit = u;
1082
1083         return 0;
1084
1085 fail:
1086         if (ours)
1087                 close_nointr_nofail(fd);
1088
1089         return -errno;
1090 }
1091
1092 void unit_unwatch_timer(Unit *u, Watch *w) {
1093         assert(u);
1094         assert(w);
1095
1096         if (w->type == WATCH_INVALID)
1097                 return;
1098
1099         assert(w->type == WATCH_TIMER && w->data.unit == u);
1100
1101         assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1102         assert_se(close_nointr(w->fd) == 0);
1103
1104         w->fd = -1;
1105         w->type = WATCH_INVALID;
1106         w->data.unit = NULL;
1107 }
1108
1109 bool unit_job_is_applicable(Unit *u, JobType j) {
1110         assert(u);
1111         assert(j >= 0 && j < _JOB_TYPE_MAX);
1112
1113         switch (j) {
1114
1115         case JOB_VERIFY_ACTIVE:
1116         case JOB_START:
1117                 return true;
1118
1119         case JOB_STOP:
1120         case JOB_RESTART:
1121         case JOB_TRY_RESTART:
1122                 return unit_can_start(u);
1123
1124         case JOB_RELOAD:
1125                 return unit_can_reload(u);
1126
1127         case JOB_RELOAD_OR_START:
1128                 return unit_can_reload(u) && unit_can_start(u);
1129
1130         default:
1131                 assert_not_reached("Invalid job type");
1132         }
1133 }
1134
1135 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {
1136
1137         static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
1138                 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
1139                 [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY,
1140                 [UNIT_WANTS] = UNIT_WANTED_BY,
1141                 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
1142                 [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY,
1143                 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1144                 [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1145                 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
1146                 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
1147                 [UNIT_BEFORE] = UNIT_AFTER,
1148                 [UNIT_AFTER] = UNIT_BEFORE
1149         };
1150         int r;
1151
1152         assert(u);
1153         assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
1154         assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
1155         assert(other);
1156
1157         /* We won't allow dependencies on ourselves. We will not
1158          * consider them an error however. */
1159         if (u == other)
1160                 return 0;
1161
1162         if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
1163                 return r;
1164
1165         if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
1166                 return r;
1167
1168         if ((r = set_put(u->meta.dependencies[d], other)) < 0)
1169                 return r;
1170
1171         if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
1172                 set_remove(u->meta.dependencies[d], other);
1173                 return r;
1174         }
1175
1176         unit_add_to_dbus_queue(u);
1177         return 0;
1178 }
1179
1180 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) {
1181         Unit *other;
1182         int r;
1183
1184         if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
1185                 return r;
1186
1187         if ((r = unit_add_dependency(u, d, other)) < 0)
1188                 return r;
1189
1190         return 0;
1191 }
1192
1193 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name) {
1194         Unit *other;
1195         int r;
1196
1197         if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
1198                 return r;
1199
1200         if ((r = unit_add_dependency(other, d, u)) < 0)
1201                 return r;
1202
1203         return 0;
1204 }
1205
1206 int set_unit_path(const char *p) {
1207         char *cwd, *c;
1208         int r;
1209
1210         /* This is mostly for debug purposes */
1211
1212         if (path_is_absolute(p)) {
1213                 if (!(c = strdup(p)))
1214                         return -ENOMEM;
1215         } else {
1216                 if (!(cwd = get_current_dir_name()))
1217                         return -errno;
1218
1219                 r = asprintf(&c, "%s/%s", cwd, p);
1220                 free(cwd);
1221
1222                 if (r < 0)
1223                         return -ENOMEM;
1224         }
1225
1226         if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
1227                 r = -errno;
1228                 free(c);
1229                 return r;
1230         }
1231
1232         return 0;
1233 }
1234
1235 char *unit_name_escape_path(const char *path, const char *suffix) {
1236         char *r, *t;
1237         const char *f;
1238         size_t a, b;
1239
1240         assert(path);
1241
1242         /* Takes a path and a suffix and prefix and makes a nice
1243          * string suitable as unit name of it, escaping all weird
1244          * chars on the way.
1245          *
1246          * / becomes ., and all chars not alloweed in a unit name get
1247          * escaped as \xFF, including \ and ., of course. This
1248          * escaping is hence reversible.
1249          */
1250
1251         if (!suffix)
1252                 suffix = "";
1253
1254         a = strlen(path);
1255         b = strlen(suffix);
1256
1257         if (!(r = new(char, a*4+b+1)))
1258                 return NULL;
1259
1260         for (f = path, t = r; *f; f++) {
1261                 if (*f == '/')
1262                         *(t++) = '.';
1263                 else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
1264                         *(t++) = '\\';
1265                         *(t++) = 'x';
1266                         *(t++) = hexchar(*f > 4);
1267                         *(t++) = hexchar(*f);
1268                 } else
1269                         *(t++) = *f;
1270         }
1271
1272         memcpy(t, suffix, b+1);
1273
1274         return r;
1275 }
1276
1277 char *unit_dbus_path(Unit *u) {
1278         char *p, *e;
1279
1280         assert(u);
1281
1282         if (!(e = bus_path_escape(unit_id(u))))
1283                 return NULL;
1284
1285         if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) {
1286                 free(e);
1287                 return NULL;
1288         }
1289
1290         free(e);
1291         return p;
1292 }
1293
1294 int unit_add_cgroup(Unit *u, CGroupBonding *b) {
1295         CGroupBonding *l;
1296         int r;
1297
1298         assert(u);
1299         assert(b);
1300         assert(b->path);
1301
1302         /* Ensure this hasn't been added yet */
1303         assert(!b->unit);
1304
1305         l = hashmap_get(u->meta.manager->cgroup_bondings, b->path);
1306         LIST_PREPEND(CGroupBonding, by_path, l, b);
1307
1308         if ((r = hashmap_replace(u->meta.manager->cgroup_bondings, b->path, l)) < 0) {
1309                 LIST_REMOVE(CGroupBonding, by_path, l, b);
1310                 return r;
1311         }
1312
1313         LIST_PREPEND(CGroupBonding, by_unit, u->meta.cgroup_bondings, b);
1314         b->unit = u;
1315
1316         return 0;
1317 }
1318
1319 static char *default_cgroup_path(Unit *u) {
1320         char *p;
1321
1322         assert(u);
1323
1324         if (asprintf(&p, "%s/%s", u->meta.manager->cgroup_hierarchy, unit_id(u)) < 0)
1325                 return NULL;
1326
1327         return p;
1328 }
1329
1330 int unit_add_cgroup_from_text(Unit *u, const char *name) {
1331         size_t n;
1332         char *controller = NULL, *path = NULL;
1333         CGroupBonding *b = NULL;
1334         int r;
1335
1336         assert(u);
1337         assert(name);
1338
1339         /* Detect controller name */
1340         n = strcspn(name, ":");
1341
1342         if (name[n] == 0 ||
1343             (name[n] == ':' && name[n+1] == 0)) {
1344
1345                 /* Only controller name, no path? */
1346
1347                 if (!(path = default_cgroup_path(u)))
1348                         return -ENOMEM;
1349
1350         } else {
1351                 const char *p;
1352
1353                 /* Controller name, and path. */
1354                 p = name+n+1;
1355
1356                 if (!path_is_absolute(p))
1357                         return -EINVAL;
1358
1359                 if (!(path = strdup(p)))
1360                         return -ENOMEM;
1361         }
1362
1363         if (n > 0)
1364                 controller = strndup(name, n);
1365         else
1366                 controller = strdup(u->meta.manager->cgroup_controller);
1367
1368         if (!controller) {
1369                 r = -ENOMEM;
1370                 goto fail;
1371         }
1372
1373         if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
1374                 r = -EEXIST;
1375                 goto fail;
1376         }
1377
1378         if (!(b = new0(CGroupBonding, 1))) {
1379                 r = -ENOMEM;
1380                 goto fail;
1381         }
1382
1383         b->controller = controller;
1384         b->path = path;
1385         b->only_us = false;
1386         b->clean_up = false;
1387
1388         if ((r = unit_add_cgroup(u, b)) < 0)
1389                 goto fail;
1390
1391         return 0;
1392
1393 fail:
1394         free(path);
1395         free(controller);
1396         free(b);
1397
1398         return r;
1399 }
1400
1401 int unit_add_default_cgroup(Unit *u) {
1402         CGroupBonding *b;
1403         int r = -ENOMEM;
1404
1405         assert(u);
1406
1407         /* Adds in the default cgroup data, if it wasn't specified yet */
1408
1409         if (unit_get_default_cgroup(u))
1410                 return 0;
1411
1412         if (!(b = new0(CGroupBonding, 1)))
1413                 return -ENOMEM;
1414
1415         if (!(b->controller = strdup(u->meta.manager->cgroup_controller)))
1416                 goto fail;
1417
1418         if (!(b->path = default_cgroup_path(u)))
1419                 goto fail;
1420
1421         b->clean_up = true;
1422         b->only_us = true;
1423
1424         if ((r = unit_add_cgroup(u, b)) < 0)
1425                 goto fail;
1426
1427         return 0;
1428
1429 fail:
1430         free(b->path);
1431         free(b->controller);
1432         free(b);
1433
1434         return r;
1435 }
1436
1437 CGroupBonding* unit_get_default_cgroup(Unit *u) {
1438         assert(u);
1439
1440         return cgroup_bonding_find_list(u->meta.cgroup_bondings, u->meta.manager->cgroup_controller);
1441 }
1442
1443 int unit_load_related_unit(Unit *u, const char *type, Unit **_found) {
1444         char *t;
1445         int r;
1446
1447         assert(u);
1448         assert(type);
1449         assert(_found);
1450
1451         if (!(t = unit_name_change_suffix(unit_id(u), type)))
1452                 return -ENOMEM;
1453
1454         assert(!unit_has_name(u, t));
1455
1456         r = manager_load_unit(u->meta.manager, t, _found);
1457         free(t);
1458
1459         if (r >= 0)
1460                 assert(*_found != u);
1461
1462         return r;
1463 }
1464
1465 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
1466         [UNIT_SERVICE] = "service",
1467         [UNIT_TIMER] = "timer",
1468         [UNIT_SOCKET] = "socket",
1469         [UNIT_TARGET] = "target",
1470         [UNIT_DEVICE] = "device",
1471         [UNIT_MOUNT] = "mount",
1472         [UNIT_AUTOMOUNT] = "automount",
1473         [UNIT_SNAPSHOT] = "snapshot"
1474 };
1475
1476 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
1477
1478 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
1479         [UNIT_STUB] = "stub",
1480         [UNIT_LOADED] = "loaded",
1481         [UNIT_FAILED] = "failed",
1482         [UNIT_MERGED] = "merged"
1483 };
1484
1485 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
1486
1487 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
1488         [UNIT_ACTIVE] = "active",
1489         [UNIT_INACTIVE] = "inactive",
1490         [UNIT_ACTIVATING] = "activating",
1491         [UNIT_DEACTIVATING] = "deactivating"
1492 };
1493
1494 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
1495
1496 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
1497         [UNIT_REQUIRES] = "Requires",
1498         [UNIT_SOFT_REQUIRES] = "SoftRequires",
1499         [UNIT_WANTS] = "Wants",
1500         [UNIT_REQUISITE] = "Requisite",
1501         [UNIT_SOFT_REQUISITE] = "SoftRequisite",
1502         [UNIT_REQUIRED_BY] = "RequiredBy",
1503         [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
1504         [UNIT_WANTED_BY] = "WantedBy",
1505         [UNIT_CONFLICTS] = "Conflicts",
1506         [UNIT_BEFORE] = "Before",
1507         [UNIT_AFTER] = "After",
1508 };
1509
1510 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);
1511
1512 static const char* const kill_mode_table[_KILL_MODE_MAX] = {
1513         [KILL_PROCESS] = "process",
1514         [KILL_PROCESS_GROUP] = "process-group",
1515         [KILL_CONTROL_GROUP] = "control-group"
1516 };
1517
1518 DEFINE_STRING_TABLE_LOOKUP(kill_mode, KillMode);