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