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