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