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