]> git.ipfire.org Git - people/ms/systemd.git/blob - unit.c
load-fragment: replace % specifiers in descriptions
[people/ms/systemd.git] / unit.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <assert.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <sys/epoll.h>
26 #include <sys/timerfd.h>
27 #include <sys/poll.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30
31 #include "set.h"
32 #include "unit.h"
33 #include "macro.h"
34 #include "strv.h"
35 #include "load-fragment.h"
36 #include "load-dropin.h"
37 #include "log.h"
38 #include "unit-name.h"
39 #include "specifier.h"
40 #include "dbus-unit.h"
41
42 const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
43 [UNIT_SERVICE] = &service_vtable,
44 [UNIT_TIMER] = &timer_vtable,
45 [UNIT_SOCKET] = &socket_vtable,
46 [UNIT_TARGET] = &target_vtable,
47 [UNIT_DEVICE] = &device_vtable,
48 [UNIT_MOUNT] = &mount_vtable,
49 [UNIT_AUTOMOUNT] = &automount_vtable,
50 [UNIT_SNAPSHOT] = &snapshot_vtable
51 };
52
53 Unit *unit_new(Manager *m) {
54 Unit *u;
55
56 assert(m);
57
58 if (!(u = new0(Unit, 1)))
59 return NULL;
60
61 if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) {
62 free(u);
63 return NULL;
64 }
65
66 u->meta.manager = m;
67 u->meta.type = _UNIT_TYPE_INVALID;
68
69 return u;
70 }
71
72 bool unit_has_name(Unit *u, const char *name) {
73 assert(u);
74 assert(name);
75
76 return !!set_get(u->meta.names, (char*) name);
77 }
78
79 int unit_add_name(Unit *u, const char *text) {
80 UnitType t;
81 char *s = NULL, *i = NULL;
82 int r;
83
84 assert(u);
85 assert(text);
86
87 if (unit_name_is_template(text)) {
88 if (!u->meta.instance)
89 return -EINVAL;
90
91 s = unit_name_replace_instance(text, u->meta.instance);
92 } else
93 s = strdup(text);
94
95 if (!s)
96 return -ENOMEM;
97
98 if (!unit_name_is_valid(s)) {
99 r = -EINVAL;
100 goto fail;
101 }
102
103 assert_se((t = unit_name_to_type(s)) >= 0);
104
105 if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type) {
106 r = -EINVAL;
107 goto fail;
108 }
109
110 if ((r = unit_name_to_instance(s, &i)) < 0)
111 goto fail;
112
113 if (i && unit_vtable[t]->no_instances)
114 goto fail;
115
116 if (u->meta.type != _UNIT_TYPE_INVALID && !streq_ptr(u->meta.instance, i)) {
117 r = -EINVAL;
118 goto fail;
119 }
120
121 if (unit_vtable[t]->no_alias &&
122 !set_isempty(u->meta.names) &&
123 !set_get(u->meta.names, s)) {
124 r = -EEXIST;
125 goto fail;
126 }
127
128 if (hashmap_size(u->meta.manager->units) >= MANAGER_MAX_NAMES) {
129 r = -E2BIG;
130 goto fail;
131 }
132
133 if ((r = set_put(u->meta.names, s)) < 0) {
134 if (r == -EEXIST)
135 r = 0;
136 goto fail;
137 }
138
139 if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
140 set_remove(u->meta.names, s);
141 goto fail;
142 }
143
144 if (u->meta.type == _UNIT_TYPE_INVALID) {
145
146 u->meta.type = t;
147 u->meta.id = s;
148 u->meta.instance = i;
149
150 LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);
151
152 if (UNIT_VTABLE(u)->init)
153 UNIT_VTABLE(u)->init(u);
154 } else
155 free(i);
156
157 unit_add_to_dbus_queue(u);
158 return 0;
159
160 fail:
161 free(s);
162 free(i);
163
164 return r;
165 }
166
167 int unit_choose_id(Unit *u, const char *name) {
168 char *s, *t = NULL;
169
170 assert(u);
171 assert(name);
172
173 if (unit_name_is_template(name)) {
174
175 if (!u->meta.instance)
176 return -EINVAL;
177
178 if (!(t = unit_name_replace_instance(name, u->meta.instance)))
179 return -ENOMEM;
180
181 name = t;
182 }
183
184 /* Selects one of the names of this unit as the id */
185 s = set_get(u->meta.names, (char*) name);
186 free(t);
187
188 if (!s)
189 return -ENOENT;
190
191 u->meta.id = s;
192 unit_add_to_dbus_queue(u);
193
194 return 0;
195 }
196
197 int unit_set_description(Unit *u, const char *description) {
198 char *s;
199
200 assert(u);
201
202 if (!(s = strdup(description)))
203 return -ENOMEM;
204
205 free(u->meta.description);
206 u->meta.description = s;
207
208 unit_add_to_dbus_queue(u);
209 return 0;
210 }
211
212 bool unit_check_gc(Unit *u) {
213 assert(u);
214
215 if (UNIT_VTABLE(u)->no_gc)
216 return true;
217
218 if (u->meta.job)
219 return true;
220
221 if (unit_active_state(u) != UNIT_INACTIVE)
222 return true;
223
224 if (UNIT_VTABLE(u)->check_gc)
225 if (UNIT_VTABLE(u)->check_gc(u))
226 return true;
227
228 return false;
229 }
230
231 void unit_add_to_load_queue(Unit *u) {
232 assert(u);
233 assert(u->meta.type != _UNIT_TYPE_INVALID);
234
235 if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
236 return;
237
238 LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
239 u->meta.in_load_queue = true;
240 }
241
242 void unit_add_to_cleanup_queue(Unit *u) {
243 assert(u);
244
245 if (u->meta.in_cleanup_queue)
246 return;
247
248 LIST_PREPEND(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
249 u->meta.in_cleanup_queue = true;
250 }
251
252 void unit_add_to_gc_queue(Unit *u) {
253 assert(u);
254
255 if (u->meta.in_gc_queue || u->meta.in_cleanup_queue)
256 return;
257
258 if (unit_check_gc(u))
259 return;
260
261 LIST_PREPEND(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta);
262 u->meta.in_gc_queue = true;
263
264 u->meta.manager->n_in_gc_queue ++;
265
266 if (u->meta.manager->gc_queue_timestamp <= 0)
267 u->meta.manager->gc_queue_timestamp = now(CLOCK_MONOTONIC);
268 }
269
270 void unit_add_to_dbus_queue(Unit *u) {
271 assert(u);
272 assert(u->meta.type != _UNIT_TYPE_INVALID);
273
274 if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue || set_isempty(u->meta.manager->subscribed))
275 return;
276
277 LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
278 u->meta.in_dbus_queue = true;
279 }
280
281 static void bidi_set_free(Unit *u, Set *s) {
282 Iterator i;
283 Unit *other;
284
285 assert(u);
286
287 /* Frees the set and makes sure we are dropped from the
288 * inverse pointers */
289
290 SET_FOREACH(other, s, i) {
291 UnitDependency d;
292
293 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
294 set_remove(other->meta.dependencies[d], u);
295
296 unit_add_to_gc_queue(other);
297 }
298
299 set_free(s);
300 }
301
302 void unit_free(Unit *u) {
303 UnitDependency d;
304 Iterator i;
305 char *t;
306
307 assert(u);
308
309 bus_unit_send_removed_signal(u);
310
311 /* Detach from next 'bigger' objects */
312 SET_FOREACH(t, u->meta.names, i)
313 hashmap_remove_value(u->meta.manager->units, t, u);
314
315 if (u->meta.type != _UNIT_TYPE_INVALID)
316 LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta);
317
318 if (u->meta.in_load_queue)
319 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
320
321 if (u->meta.in_dbus_queue)
322 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
323
324 if (u->meta.in_cleanup_queue)
325 LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
326
327 if (u->meta.in_gc_queue) {
328 LIST_REMOVE(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta);
329 u->meta.manager->n_in_gc_queue--;
330 }
331
332 /* Free data and next 'smaller' objects */
333 if (u->meta.job)
334 job_free(u->meta.job);
335
336 if (u->meta.load_state != UNIT_STUB)
337 if (UNIT_VTABLE(u)->done)
338 UNIT_VTABLE(u)->done(u);
339
340 cgroup_bonding_free_list(u->meta.cgroup_bondings);
341
342 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
343 bidi_set_free(u, u->meta.dependencies[d]);
344
345 free(u->meta.description);
346 free(u->meta.fragment_path);
347
348 while ((t = set_steal_first(u->meta.names)))
349 free(t);
350 set_free(u->meta.names);
351
352 free(u->meta.instance);
353
354 free(u);
355 }
356
357 UnitActiveState unit_active_state(Unit *u) {
358 assert(u);
359
360 if (u->meta.load_state != UNIT_LOADED)
361 return UNIT_INACTIVE;
362
363 return UNIT_VTABLE(u)->active_state(u);
364 }
365
366 const char* unit_sub_state_to_string(Unit *u) {
367 assert(u);
368
369 return UNIT_VTABLE(u)->sub_state_to_string(u);
370 }
371
372 static void complete_move(Set **s, Set **other) {
373 assert(s);
374 assert(other);
375
376 if (!*other)
377 return;
378
379 if (*s)
380 set_move(*s, *other);
381 else {
382 *s = *other;
383 *other = NULL;
384 }
385 }
386
387 static void merge_names(Unit *u, Unit *other) {
388 char *t;
389 Iterator i;
390
391 assert(u);
392 assert(other);
393
394 complete_move(&u->meta.names, &other->meta.names);
395
396 while ((t = set_steal_first(other->meta.names)))
397 free(t);
398
399 set_free(other->meta.names);
400 other->meta.names = NULL;
401 other->meta.id = NULL;
402
403 SET_FOREACH(t, u->meta.names, i)
404 assert_se(hashmap_replace(u->meta.manager->units, t, u) == 0);
405 }
406
407 static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
408 Iterator i;
409 Unit *back;
410 int r;
411
412 assert(u);
413 assert(other);
414 assert(d < _UNIT_DEPENDENCY_MAX);
415
416 SET_FOREACH(back, other->meta.dependencies[d], i) {
417 UnitDependency k;
418
419 for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++)
420 if ((r = set_remove_and_put(back->meta.dependencies[k], other, u)) < 0) {
421
422 if (r == -EEXIST)
423 set_remove(back->meta.dependencies[k], other);
424 else
425 assert(r == -ENOENT);
426 }
427 }
428
429 complete_move(&u->meta.dependencies[d], &other->meta.dependencies[d]);
430
431 set_free(other->meta.dependencies[d]);
432 other->meta.dependencies[d] = NULL;
433 }
434
435 int unit_merge(Unit *u, Unit *other) {
436 UnitDependency d;
437
438 assert(u);
439 assert(other);
440 assert(u->meta.manager == other->meta.manager);
441 assert(u->meta.type != _UNIT_TYPE_INVALID);
442
443 other = unit_follow_merge(other);
444
445 if (other == u)
446 return 0;
447
448 if (u->meta.type != other->meta.type)
449 return -EINVAL;
450
451 if (!streq_ptr(u->meta.instance, other->meta.instance))
452 return -EINVAL;
453
454 if (other->meta.load_state != UNIT_STUB &&
455 other->meta.load_state != UNIT_FAILED)
456 return -EEXIST;
457
458 if (other->meta.job)
459 return -EEXIST;
460
461 if (unit_active_state(other) != UNIT_INACTIVE)
462 return -EEXIST;
463
464 /* Merge names */
465 merge_names(u, other);
466
467 /* Merge dependencies */
468 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
469 merge_dependencies(u, other, d);
470
471 other->meta.load_state = UNIT_MERGED;
472 other->meta.merged_into = u;
473
474 /* If there is still some data attached to the other node, we
475 * don't need it anymore, and can free it. */
476 if (other->meta.load_state != UNIT_STUB)
477 if (UNIT_VTABLE(other)->done)
478 UNIT_VTABLE(other)->done(other);
479
480 unit_add_to_dbus_queue(u);
481 unit_add_to_cleanup_queue(other);
482
483 return 0;
484 }
485
486 int unit_merge_by_name(Unit *u, const char *name) {
487 Unit *other;
488 int r;
489 char *s = NULL;
490
491 assert(u);
492 assert(name);
493
494 if (unit_name_is_template(name)) {
495 if (!u->meta.instance)
496 return -EINVAL;
497
498 if (!(s = unit_name_replace_instance(name, u->meta.instance)))
499 return -ENOMEM;
500
501 name = s;
502 }
503
504 if (!(other = manager_get_unit(u->meta.manager, name)))
505 r = unit_add_name(u, name);
506 else
507 r = unit_merge(u, other);
508
509 free(s);
510 return r;
511 }
512
513 Unit* unit_follow_merge(Unit *u) {
514 assert(u);
515
516 while (u->meta.load_state == UNIT_MERGED)
517 assert_se(u = u->meta.merged_into);
518
519 return u;
520 }
521
522 int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
523 int r;
524
525 assert(u);
526 assert(c);
527
528 if (c->std_output != EXEC_OUTPUT_KERNEL && c->std_output != EXEC_OUTPUT_SYSLOG)
529 return 0;
530
531 /* If syslog or kernel logging is requested, make sure our own
532 * logging daemon is run first. */
533
534 if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
535 return r;
536
537 if (u->meta.manager->running_as != MANAGER_SESSION)
538 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
539 return r;
540
541 return 0;
542 }
543
544 const char *unit_description(Unit *u) {
545 assert(u);
546
547 if (u->meta.description)
548 return u->meta.description;
549
550 return u->meta.id;
551 }
552
553 void unit_dump(Unit *u, FILE *f, const char *prefix) {
554 char *t;
555 UnitDependency d;
556 Iterator i;
557 char *p2;
558 const char *prefix2;
559 CGroupBonding *b;
560 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
561
562 assert(u);
563 assert(u->meta.type >= 0);
564
565 if (!prefix)
566 prefix = "";
567 p2 = strappend(prefix, "\t");
568 prefix2 = p2 ? p2 : prefix;
569
570 fprintf(f,
571 "%s-> Unit %s:\n"
572 "%s\tDescription: %s\n"
573 "%s\tInstance: %s\n"
574 "%s\tUnit Load State: %s\n"
575 "%s\tUnit Active State: %s\n"
576 "%s\tActive Enter Timestamp: %s\n"
577 "%s\tActive Exit Timestamp: %s\n"
578 "%s\tGC Check Good: %s\n",
579 prefix, u->meta.id,
580 prefix, unit_description(u),
581 prefix, strna(u->meta.instance),
582 prefix, unit_load_state_to_string(u->meta.load_state),
583 prefix, unit_active_state_to_string(unit_active_state(u)),
584 prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.active_enter_timestamp)),
585 prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->meta.active_exit_timestamp)),
586 prefix, yes_no(unit_check_gc(u)));
587
588 SET_FOREACH(t, u->meta.names, i)
589 fprintf(f, "%s\tName: %s\n", prefix, t);
590
591 if (u->meta.fragment_path)
592 fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);
593
594 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
595 Unit *other;
596
597 SET_FOREACH(other, u->meta.dependencies[d], i)
598 fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), other->meta.id);
599 }
600
601 if (u->meta.load_state == UNIT_LOADED) {
602 fprintf(f,
603 "%s\tRecursive Stop: %s\n"
604 "%s\tStop When Unneeded: %s\n",
605 prefix, yes_no(u->meta.recursive_stop),
606 prefix, yes_no(u->meta.stop_when_unneeded));
607
608 LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
609 fprintf(f, "%s\tControlGroup: %s:%s\n",
610 prefix, b->controller, b->path);
611
612 if (UNIT_VTABLE(u)->dump)
613 UNIT_VTABLE(u)->dump(u, f, prefix2);
614
615 } else if (u->meta.load_state == UNIT_MERGED)
616 fprintf(f,
617 "%s\tMerged into: %s\n",
618 prefix, u->meta.merged_into->meta.id);
619
620 if (u->meta.job)
621 job_dump(u->meta.job, f, prefix2);
622
623 free(p2);
624 }
625
626 /* Common implementation for multiple backends */
627 int unit_load_fragment_and_dropin(Unit *u) {
628 int r;
629
630 assert(u);
631
632 /* Load a .service file */
633 if ((r = unit_load_fragment(u)) < 0)
634 return r;
635
636 if (u->meta.load_state == UNIT_STUB)
637 return -ENOENT;
638
639 /* Load drop-in directory data */
640 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
641 return r;
642
643 return 0;
644 }
645
646 /* Common implementation for multiple backends */
647 int unit_load_fragment_and_dropin_optional(Unit *u) {
648 int r;
649
650 assert(u);
651
652 /* Same as unit_load_fragment_and_dropin(), but whether
653 * something can be loaded or not doesn't matter. */
654
655 /* Load a .service file */
656 if ((r = unit_load_fragment(u)) < 0)
657 return r;
658
659 if (u->meta.load_state == UNIT_STUB)
660 u->meta.load_state = UNIT_LOADED;
661
662 /* Load drop-in directory data */
663 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
664 return r;
665
666 return 0;
667 }
668
669 /* Common implementation for multiple backends */
670 int unit_load_nop(Unit *u) {
671 assert(u);
672
673 if (u->meta.load_state == UNIT_STUB)
674 u->meta.load_state = UNIT_LOADED;
675
676 return 0;
677 }
678
679 int unit_load(Unit *u) {
680 int r;
681
682 assert(u);
683
684 if (u->meta.in_load_queue) {
685 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
686 u->meta.in_load_queue = false;
687 }
688
689 if (u->meta.type == _UNIT_TYPE_INVALID)
690 return -EINVAL;
691
692 if (u->meta.load_state != UNIT_STUB)
693 return 0;
694
695 if (UNIT_VTABLE(u)->load)
696 if ((r = UNIT_VTABLE(u)->load(u)) < 0)
697 goto fail;
698
699 if (u->meta.load_state == UNIT_STUB) {
700 r = -ENOENT;
701 goto fail;
702 }
703
704 assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);
705
706 unit_add_to_dbus_queue(unit_follow_merge(u));
707 unit_add_to_gc_queue(u);
708
709 return 0;
710
711 fail:
712 u->meta.load_state = UNIT_FAILED;
713 unit_add_to_dbus_queue(u);
714
715 log_error("Failed to load configuration for %s: %s", u->meta.id, strerror(-r));
716
717 return r;
718 }
719
720 /* Errors:
721 * -EBADR: This unit type does not support starting.
722 * -EALREADY: Unit is already started.
723 * -EAGAIN: An operation is already in progress. Retry later.
724 */
725 int unit_start(Unit *u) {
726 UnitActiveState state;
727
728 assert(u);
729
730 /* If this is already (being) started, then this will
731 * succeed. Note that this will even succeed if this unit is
732 * not startable by the user. This is relied on to detect when
733 * we need to wait for units and when waiting is finished. */
734 state = unit_active_state(u);
735 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
736 return -EALREADY;
737
738 /* If it is stopped, but we cannot start it, then fail */
739 if (!UNIT_VTABLE(u)->start)
740 return -EBADR;
741
742 /* We don't suppress calls to ->start() here when we are
743 * already starting, to allow this request to be used as a
744 * "hurry up" call, for example when the unit is in some "auto
745 * restart" state where it waits for a holdoff timer to elapse
746 * before it will start again. */
747
748 unit_add_to_dbus_queue(u);
749 return UNIT_VTABLE(u)->start(u);
750 }
751
752 bool unit_can_start(Unit *u) {
753 assert(u);
754
755 return !!UNIT_VTABLE(u)->start;
756 }
757
758 /* Errors:
759 * -EBADR: This unit type does not support stopping.
760 * -EALREADY: Unit is already stopped.
761 * -EAGAIN: An operation is already in progress. Retry later.
762 */
763 int unit_stop(Unit *u) {
764 UnitActiveState state;
765
766 assert(u);
767
768 state = unit_active_state(u);
769 if (state == UNIT_INACTIVE)
770 return -EALREADY;
771
772 if (!UNIT_VTABLE(u)->stop)
773 return -EBADR;
774
775 unit_add_to_dbus_queue(u);
776 return UNIT_VTABLE(u)->stop(u);
777 }
778
779 /* Errors:
780 * -EBADR: This unit type does not support reloading.
781 * -ENOEXEC: Unit is not started.
782 * -EAGAIN: An operation is already in progress. Retry later.
783 */
784 int unit_reload(Unit *u) {
785 UnitActiveState state;
786
787 assert(u);
788
789 if (!unit_can_reload(u))
790 return -EBADR;
791
792 state = unit_active_state(u);
793 if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
794 return -EALREADY;
795
796 if (unit_active_state(u) != UNIT_ACTIVE)
797 return -ENOEXEC;
798
799 unit_add_to_dbus_queue(u);
800 return UNIT_VTABLE(u)->reload(u);
801 }
802
803 bool unit_can_reload(Unit *u) {
804 assert(u);
805
806 if (!UNIT_VTABLE(u)->reload)
807 return false;
808
809 if (!UNIT_VTABLE(u)->can_reload)
810 return true;
811
812 return UNIT_VTABLE(u)->can_reload(u);
813 }
814
815 static void unit_check_uneeded(Unit *u) {
816 Iterator i;
817 Unit *other;
818
819 assert(u);
820
821 /* If this service shall be shut down when unneeded then do
822 * so. */
823
824 if (!u->meta.stop_when_unneeded)
825 return;
826
827 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
828 return;
829
830 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
831 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
832 return;
833
834 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
835 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
836 return;
837
838 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
839 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
840 return;
841
842 log_debug("Service %s is not needed anymore. Stopping.", u->meta.id);
843
844 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
845 manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL);
846 }
847
848 static void retroactively_start_dependencies(Unit *u) {
849 Iterator i;
850 Unit *other;
851
852 assert(u);
853 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
854
855 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
856 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
857 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
858
859 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
860 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
861 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
862
863 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
864 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
865 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
866
867 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
868 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
869 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
870
871 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
872 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
873 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
874 }
875
876 static void retroactively_stop_dependencies(Unit *u) {
877 Iterator i;
878 Unit *other;
879
880 assert(u);
881 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
882
883 if (u->meta.recursive_stop) {
884 /* Pull down units need us recursively if enabled */
885 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
886 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
887 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
888 }
889
890 /* Garbage collect services that might not be needed anymore, if enabled */
891 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
892 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
893 unit_check_uneeded(other);
894 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
895 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
896 unit_check_uneeded(other);
897 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
898 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
899 unit_check_uneeded(other);
900 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
901 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
902 unit_check_uneeded(other);
903 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
904 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
905 unit_check_uneeded(other);
906 }
907
908 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
909 bool unexpected = false;
910
911 assert(u);
912 assert(os < _UNIT_ACTIVE_STATE_MAX);
913 assert(ns < _UNIT_ACTIVE_STATE_MAX);
914
915 /* Note that this is called for all low-level state changes,
916 * even if they might map to the same high-level
917 * UnitActiveState! That means that ns == os is OK an expected
918 * behaviour here. For example: if a mount point is remounted
919 * this function will be called too and the utmp code below
920 * relies on that! */
921
922 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
923 u->meta.active_enter_timestamp = now(CLOCK_REALTIME);
924 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
925 u->meta.active_exit_timestamp = now(CLOCK_REALTIME);
926
927 if (u->meta.job) {
928
929 if (u->meta.job->state == JOB_WAITING)
930
931 /* So we reached a different state for this
932 * job. Let's see if we can run it now if it
933 * failed previously due to EAGAIN. */
934 job_add_to_run_queue(u->meta.job);
935
936 else {
937 assert(u->meta.job->state == JOB_RUNNING);
938
939 /* Let's check whether this state change
940 * constitutes a finished job, or maybe
941 * cotradicts a running job and hence needs to
942 * invalidate jobs. */
943
944 switch (u->meta.job->type) {
945
946 case JOB_START:
947 case JOB_VERIFY_ACTIVE:
948
949 if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
950 job_finish_and_invalidate(u->meta.job, true);
951 else if (ns != UNIT_ACTIVATING) {
952 unexpected = true;
953 job_finish_and_invalidate(u->meta.job, false);
954 }
955
956 break;
957
958 case JOB_RELOAD:
959 case JOB_RELOAD_OR_START:
960
961 if (ns == UNIT_ACTIVE)
962 job_finish_and_invalidate(u->meta.job, true);
963 else if (ns != UNIT_ACTIVATING && ns != UNIT_ACTIVE_RELOADING) {
964 unexpected = true;
965 job_finish_and_invalidate(u->meta.job, false);
966 }
967
968 break;
969
970 case JOB_STOP:
971 case JOB_RESTART:
972 case JOB_TRY_RESTART:
973
974 if (ns == UNIT_INACTIVE)
975 job_finish_and_invalidate(u->meta.job, true);
976 else if (ns != UNIT_DEACTIVATING) {
977 unexpected = true;
978 job_finish_and_invalidate(u->meta.job, false);
979 }
980
981 break;
982
983 default:
984 assert_not_reached("Job type unknown");
985 }
986 }
987 }
988
989 /* If this state change happened without being requested by a
990 * job, then let's retroactively start or stop dependencies */
991
992 if (unexpected) {
993 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
994 retroactively_start_dependencies(u);
995 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
996 retroactively_stop_dependencies(u);
997 }
998
999 /* Some names are special */
1000 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
1001 if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) {
1002 /* The bus just might have become available,
1003 * hence try to connect to it, if we aren't
1004 * yet connected. */
1005 bus_init_system(u->meta.manager);
1006 bus_init_api(u->meta.manager);
1007 }
1008
1009 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
1010 /* The syslog daemon just might have become
1011 * available, hence try to connect to it, if
1012 * we aren't yet connected. */
1013 log_open_syslog();
1014
1015 if (u->meta.type == UNIT_MOUNT)
1016 /* Another directory became available, let's
1017 * check if that is enough to write our utmp
1018 * entry. */
1019 manager_write_utmp_reboot(u->meta.manager);
1020
1021 if (u->meta.type == UNIT_TARGET)
1022 /* A target got activated, maybe this is a runlevel? */
1023 manager_write_utmp_runlevel(u->meta.manager, u);
1024
1025 } else if (!UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
1026
1027 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
1028 /* The syslog daemon might just have
1029 * terminated, hence try to disconnect from
1030 * it. */
1031 log_close_syslog();
1032
1033 /* We don't care about D-Bus here, since we'll get an
1034 * asynchronous notification for it anyway. */
1035 }
1036
1037 /* Maybe we finished startup and are now ready for being
1038 * stopped because unneeded? */
1039 unit_check_uneeded(u);
1040
1041 unit_add_to_dbus_queue(u);
1042 unit_add_to_gc_queue(u);
1043 }
1044
1045 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
1046 struct epoll_event ev;
1047
1048 assert(u);
1049 assert(fd >= 0);
1050 assert(w);
1051 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
1052
1053 zero(ev);
1054 ev.data.ptr = w;
1055 ev.events = events;
1056
1057 if (epoll_ctl(u->meta.manager->epoll_fd,
1058 w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
1059 fd,
1060 &ev) < 0)
1061 return -errno;
1062
1063 w->fd = fd;
1064 w->type = WATCH_FD;
1065 w->data.unit = u;
1066
1067 return 0;
1068 }
1069
1070 void unit_unwatch_fd(Unit *u, Watch *w) {
1071 assert(u);
1072 assert(w);
1073
1074 if (w->type == WATCH_INVALID)
1075 return;
1076
1077 assert(w->type == WATCH_FD);
1078 assert(w->data.unit == u);
1079 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1080
1081 w->fd = -1;
1082 w->type = WATCH_INVALID;
1083 w->data.unit = NULL;
1084 }
1085
1086 int unit_watch_pid(Unit *u, pid_t pid) {
1087 assert(u);
1088 assert(pid >= 1);
1089
1090 /* Watch a specific PID. We only support one unit watching
1091 * each PID for now. */
1092
1093 return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
1094 }
1095
1096 void unit_unwatch_pid(Unit *u, pid_t pid) {
1097 assert(u);
1098 assert(pid >= 1);
1099
1100 hashmap_remove_value(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
1101 }
1102
1103 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
1104 struct itimerspec its;
1105 int flags, fd;
1106 bool ours;
1107
1108 assert(u);
1109 assert(w);
1110 assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->data.unit == u));
1111
1112 /* This will try to reuse the old timer if there is one */
1113
1114 if (w->type == WATCH_TIMER) {
1115 ours = false;
1116 fd = w->fd;
1117 } else {
1118 ours = true;
1119 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
1120 return -errno;
1121 }
1122
1123 zero(its);
1124
1125 if (delay <= 0) {
1126 /* Set absolute time in the past, but not 0, since we
1127 * don't want to disarm the timer */
1128 its.it_value.tv_sec = 0;
1129 its.it_value.tv_nsec = 1;
1130
1131 flags = TFD_TIMER_ABSTIME;
1132 } else {
1133 timespec_store(&its.it_value, delay);
1134 flags = 0;
1135 }
1136
1137 /* This will also flush the elapse counter */
1138 if (timerfd_settime(fd, flags, &its, NULL) < 0)
1139 goto fail;
1140
1141 if (w->type == WATCH_INVALID) {
1142 struct epoll_event ev;
1143
1144 zero(ev);
1145 ev.data.ptr = w;
1146 ev.events = EPOLLIN;
1147
1148 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
1149 goto fail;
1150 }
1151
1152 w->fd = fd;
1153 w->type = WATCH_TIMER;
1154 w->data.unit = u;
1155
1156 return 0;
1157
1158 fail:
1159 if (ours)
1160 close_nointr_nofail(fd);
1161
1162 return -errno;
1163 }
1164
1165 void unit_unwatch_timer(Unit *u, Watch *w) {
1166 assert(u);
1167 assert(w);
1168
1169 if (w->type == WATCH_INVALID)
1170 return;
1171
1172 assert(w->type == WATCH_TIMER && w->data.unit == u);
1173
1174 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1175 close_nointr_nofail(w->fd);
1176
1177 w->fd = -1;
1178 w->type = WATCH_INVALID;
1179 w->data.unit = NULL;
1180 }
1181
1182 bool unit_job_is_applicable(Unit *u, JobType j) {
1183 assert(u);
1184 assert(j >= 0 && j < _JOB_TYPE_MAX);
1185
1186 switch (j) {
1187
1188 case JOB_VERIFY_ACTIVE:
1189 case JOB_START:
1190 return true;
1191
1192 case JOB_STOP:
1193 case JOB_RESTART:
1194 case JOB_TRY_RESTART:
1195 return unit_can_start(u);
1196
1197 case JOB_RELOAD:
1198 return unit_can_reload(u);
1199
1200 case JOB_RELOAD_OR_START:
1201 return unit_can_reload(u) && unit_can_start(u);
1202
1203 default:
1204 assert_not_reached("Invalid job type");
1205 }
1206 }
1207
1208 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference) {
1209
1210 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
1211 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
1212 [UNIT_REQUIRES_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE,
1213 [UNIT_WANTS] = UNIT_WANTED_BY,
1214 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
1215 [UNIT_REQUISITE_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE,
1216 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1217 [UNIT_REQUIRED_BY_OVERRIDABLE] = _UNIT_DEPENDENCY_INVALID,
1218 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
1219 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
1220 [UNIT_BEFORE] = UNIT_AFTER,
1221 [UNIT_AFTER] = UNIT_BEFORE,
1222 [UNIT_REFERENCES] = UNIT_REFERENCED_BY,
1223 [UNIT_REFERENCED_BY] = UNIT_REFERENCES
1224 };
1225 int r, q = 0, v = 0, w = 0;
1226
1227 assert(u);
1228 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
1229 assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
1230 assert(other);
1231
1232 /* We won't allow dependencies on ourselves. We will not
1233 * consider them an error however. */
1234 if (u == other)
1235 return 0;
1236
1237 if (UNIT_VTABLE(u)->no_requires &&
1238 (d == UNIT_REQUIRES ||
1239 d == UNIT_REQUIRES_OVERRIDABLE ||
1240 d == UNIT_REQUISITE ||
1241 d == UNIT_REQUISITE_OVERRIDABLE)) {
1242 return -EINVAL;
1243 }
1244
1245 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0 ||
1246 (r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
1247 return r;
1248
1249 if (add_reference)
1250 if ((r = set_ensure_allocated(&u->meta.dependencies[UNIT_REFERENCES], trivial_hash_func, trivial_compare_func)) < 0 ||
1251 (r = set_ensure_allocated(&other->meta.dependencies[UNIT_REFERENCED_BY], trivial_hash_func, trivial_compare_func)) < 0)
1252 return r;
1253
1254 if ((q = set_put(u->meta.dependencies[d], other)) < 0)
1255 return q;
1256
1257 if ((v = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
1258 r = v;
1259 goto fail;
1260 }
1261
1262 if (add_reference) {
1263 if ((w = set_put(u->meta.dependencies[UNIT_REFERENCES], other)) < 0) {
1264 r = w;
1265 goto fail;
1266 }
1267
1268 if ((r = set_put(other->meta.dependencies[UNIT_REFERENCED_BY], u)) < 0)
1269 goto fail;
1270 }
1271
1272 unit_add_to_dbus_queue(u);
1273 return 0;
1274
1275 fail:
1276 if (q > 0)
1277 set_remove(u->meta.dependencies[d], other);
1278
1279 if (v > 0)
1280 set_remove(other->meta.dependencies[inverse_table[d]], u);
1281
1282 if (w > 0)
1283 set_remove(u->meta.dependencies[UNIT_REFERENCES], other);
1284
1285 return r;
1286 }
1287
1288 static const char *resolve_template(Unit *u, const char *name, const char*path, char **p) {
1289 char *s;
1290
1291 assert(u);
1292 assert(name || path);
1293
1294 if (!name)
1295 name = file_name_from_path(path);
1296
1297 if (!unit_name_is_template(name)) {
1298 *p = NULL;
1299 return name;
1300 }
1301
1302 if (u->meta.instance)
1303 s = unit_name_replace_instance(name, u->meta.instance);
1304 else {
1305 char *i;
1306
1307 if (!(i = unit_name_to_prefix(u->meta.id)))
1308 return NULL;
1309
1310 s = unit_name_replace_instance(name, i);
1311 free(i);
1312 }
1313
1314 if (!s)
1315 return NULL;
1316
1317 *p = s;
1318 return s;
1319 }
1320
1321 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
1322 Unit *other;
1323 int r;
1324 char *s;
1325
1326 assert(u);
1327 assert(name || path);
1328
1329 if (!(name = resolve_template(u, name, path, &s)))
1330 return -ENOMEM;
1331
1332 if ((r = manager_load_unit(u->meta.manager, name, path, &other)) < 0)
1333 goto finish;
1334
1335 r = unit_add_dependency(u, d, other, add_reference);
1336
1337 finish:
1338 free(s);
1339 return r;
1340 }
1341
1342 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
1343 Unit *other;
1344 int r;
1345 char *s;
1346
1347 assert(u);
1348 assert(name || path);
1349
1350 if (!(name = resolve_template(u, name, path, &s)))
1351 return -ENOMEM;
1352
1353 if ((r = manager_load_unit(u->meta.manager, name, path, &other)) < 0)
1354 goto finish;
1355
1356 r = unit_add_dependency(other, d, u, add_reference);
1357
1358 finish:
1359 free(s);
1360 return r;
1361 }
1362
1363 int set_unit_path(const char *p) {
1364 char *cwd, *c;
1365 int r;
1366
1367 /* This is mostly for debug purposes */
1368
1369 if (path_is_absolute(p)) {
1370 if (!(c = strdup(p)))
1371 return -ENOMEM;
1372 } else {
1373 if (!(cwd = get_current_dir_name()))
1374 return -errno;
1375
1376 r = asprintf(&c, "%s/%s", cwd, p);
1377 free(cwd);
1378
1379 if (r < 0)
1380 return -ENOMEM;
1381 }
1382
1383 if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
1384 r = -errno;
1385 free(c);
1386 return r;
1387 }
1388
1389 return 0;
1390 }
1391
1392 char *unit_dbus_path(Unit *u) {
1393 char *p, *e;
1394
1395 assert(u);
1396
1397 if (!(e = bus_path_escape(u->meta.id)))
1398 return NULL;
1399
1400 if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) {
1401 free(e);
1402 return NULL;
1403 }
1404
1405 free(e);
1406 return p;
1407 }
1408
1409 int unit_add_cgroup(Unit *u, CGroupBonding *b) {
1410 CGroupBonding *l;
1411 int r;
1412
1413 assert(u);
1414 assert(b);
1415 assert(b->path);
1416
1417 /* Ensure this hasn't been added yet */
1418 assert(!b->unit);
1419
1420 l = hashmap_get(u->meta.manager->cgroup_bondings, b->path);
1421 LIST_PREPEND(CGroupBonding, by_path, l, b);
1422
1423 if ((r = hashmap_replace(u->meta.manager->cgroup_bondings, b->path, l)) < 0) {
1424 LIST_REMOVE(CGroupBonding, by_path, l, b);
1425 return r;
1426 }
1427
1428 LIST_PREPEND(CGroupBonding, by_unit, u->meta.cgroup_bondings, b);
1429 b->unit = u;
1430
1431 return 0;
1432 }
1433
1434 static char *default_cgroup_path(Unit *u) {
1435 char *p;
1436 int r;
1437
1438 assert(u);
1439
1440 if (u->meta.instance) {
1441 char *t;
1442
1443 if (!(t = unit_name_template(u->meta.id)))
1444 return NULL;
1445
1446 r = asprintf(&p, "%s/%s/%s", u->meta.manager->cgroup_hierarchy, t, u->meta.instance);
1447 free(t);
1448 } else
1449 r = asprintf(&p, "%s/%s", u->meta.manager->cgroup_hierarchy, u->meta.id);
1450
1451 return r < 0 ? NULL : p;
1452 }
1453
1454 int unit_add_cgroup_from_text(Unit *u, const char *name) {
1455 size_t n;
1456 char *controller = NULL, *path = NULL;
1457 CGroupBonding *b = NULL;
1458 int r;
1459
1460 assert(u);
1461 assert(name);
1462
1463 /* Detect controller name */
1464 n = strcspn(name, ":");
1465
1466 if (name[n] == 0 ||
1467 (name[n] == ':' && name[n+1] == 0)) {
1468
1469 /* Only controller name, no path? */
1470
1471 if (!(path = default_cgroup_path(u)))
1472 return -ENOMEM;
1473
1474 } else {
1475 const char *p;
1476
1477 /* Controller name, and path. */
1478 p = name+n+1;
1479
1480 if (!path_is_absolute(p))
1481 return -EINVAL;
1482
1483 if (!(path = strdup(p)))
1484 return -ENOMEM;
1485 }
1486
1487 if (n > 0)
1488 controller = strndup(name, n);
1489 else
1490 controller = strdup(u->meta.manager->cgroup_controller);
1491
1492 if (!controller) {
1493 r = -ENOMEM;
1494 goto fail;
1495 }
1496
1497 if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
1498 r = -EEXIST;
1499 goto fail;
1500 }
1501
1502 if (!(b = new0(CGroupBonding, 1))) {
1503 r = -ENOMEM;
1504 goto fail;
1505 }
1506
1507 b->controller = controller;
1508 b->path = path;
1509 b->only_us = false;
1510 b->clean_up = false;
1511
1512 if ((r = unit_add_cgroup(u, b)) < 0)
1513 goto fail;
1514
1515 return 0;
1516
1517 fail:
1518 free(path);
1519 free(controller);
1520 free(b);
1521
1522 return r;
1523 }
1524
1525 int unit_add_default_cgroup(Unit *u) {
1526 CGroupBonding *b;
1527 int r = -ENOMEM;
1528
1529 assert(u);
1530
1531 /* Adds in the default cgroup data, if it wasn't specified yet */
1532
1533 if (unit_get_default_cgroup(u))
1534 return 0;
1535
1536 if (!(b = new0(CGroupBonding, 1)))
1537 return -ENOMEM;
1538
1539 if (!(b->controller = strdup(u->meta.manager->cgroup_controller)))
1540 goto fail;
1541
1542 if (!(b->path = default_cgroup_path(u)))
1543 goto fail;
1544
1545 b->clean_up = true;
1546 b->only_us = true;
1547
1548 if ((r = unit_add_cgroup(u, b)) < 0)
1549 goto fail;
1550
1551 return 0;
1552
1553 fail:
1554 free(b->path);
1555 free(b->controller);
1556 free(b);
1557
1558 return r;
1559 }
1560
1561 CGroupBonding* unit_get_default_cgroup(Unit *u) {
1562 assert(u);
1563
1564 return cgroup_bonding_find_list(u->meta.cgroup_bondings, u->meta.manager->cgroup_controller);
1565 }
1566
1567 int unit_load_related_unit(Unit *u, const char *type, Unit **_found) {
1568 char *t;
1569 int r;
1570
1571 assert(u);
1572 assert(type);
1573 assert(_found);
1574
1575 if (!(t = unit_name_change_suffix(u->meta.id, type)))
1576 return -ENOMEM;
1577
1578 assert(!unit_has_name(u, t));
1579
1580 r = manager_load_unit(u->meta.manager, t, NULL, _found);
1581 free(t);
1582
1583 assert(r < 0 || *_found != u);
1584
1585 return r;
1586 }
1587
1588 int unit_get_related_unit(Unit *u, const char *type, Unit **_found) {
1589 Unit *found;
1590 char *t;
1591
1592 assert(u);
1593 assert(type);
1594 assert(_found);
1595
1596 if (!(t = unit_name_change_suffix(u->meta.id, type)))
1597 return -ENOMEM;
1598
1599 assert(!unit_has_name(u, t));
1600
1601 found = manager_get_unit(u->meta.manager, t);
1602 free(t);
1603
1604 if (!found)
1605 return -ENOENT;
1606
1607 *_found = found;
1608 return 0;
1609 }
1610
1611 static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) {
1612 Unit *u = userdata;
1613 assert(u);
1614
1615 return unit_name_to_prefix_and_instance(u->meta.id);
1616 }
1617
1618 static char *specifier_prefix(char specifier, void *data, void *userdata) {
1619 Unit *u = userdata;
1620 assert(u);
1621
1622 return unit_name_to_prefix(u->meta.id);
1623 }
1624
1625 static char *specifier_prefix_unescaped(char specifier, void *data, void *userdata) {
1626 Unit *u = userdata;
1627 char *p, *r;
1628
1629 assert(u);
1630
1631 if (!(p = unit_name_to_prefix(u->meta.id)))
1632 return NULL;
1633
1634 r = unit_name_unescape(p);
1635 free(p);
1636
1637 return r;
1638 }
1639
1640 static char *specifier_instance_unescaped(char specifier, void *data, void *userdata) {
1641 Unit *u = userdata;
1642 assert(u);
1643
1644 if (u->meta.instance)
1645 return unit_name_unescape(u->meta.instance);
1646
1647 return strdup("");
1648 }
1649
1650 char *unit_name_printf(Unit *u, const char* format) {
1651
1652 /*
1653 * This will use the passed string as format string and
1654 * replace the following specifiers:
1655 *
1656 * %n: the full id of the unit (foo@bar.waldo)
1657 * %N: the id of the unit without the suffix (foo@bar)
1658 * %p: the prefix (foo)
1659 * %i: the instance (bar)
1660 */
1661
1662 const Specifier table[] = {
1663 { 'n', specifier_string, u->meta.id },
1664 { 'N', specifier_prefix_and_instance, NULL },
1665 { 'p', specifier_prefix, NULL },
1666 { 'i', specifier_string, u->meta.instance },
1667 { 0, NULL, NULL }
1668 };
1669
1670 assert(u);
1671 assert(format);
1672
1673 return specifier_printf(format, table, u);
1674 }
1675
1676 char *unit_full_printf(Unit *u, const char *format) {
1677
1678 /* This is similar to unit_name_printf() but also supports
1679 * unescaping */
1680
1681 const Specifier table[] = {
1682 { 'n', specifier_string, u->meta.id },
1683 { 'N', specifier_prefix_and_instance, NULL },
1684 { 'p', specifier_prefix, NULL },
1685 { 'P', specifier_prefix_unescaped, NULL },
1686 { 'i', specifier_string, u->meta.instance },
1687 { 'I', specifier_instance_unescaped, NULL },
1688 { 0, NULL, NULL }
1689 };
1690
1691 assert(u);
1692 assert(format);
1693
1694 return specifier_printf(format, table, u);
1695 }
1696
1697 char **unit_full_printf_strv(Unit *u, char **l) {
1698 size_t n;
1699 char **r, **i, **j;
1700
1701 /* Applies unit_full_printf to every entry in l */
1702
1703 assert(u);
1704
1705 n = strv_length(l);
1706 if (!(r = new(char*, n+1)))
1707 return NULL;
1708
1709 for (i = l, j = r; *i; i++, j++)
1710 if (!(*j = unit_full_printf(u, *i)))
1711 goto fail;
1712
1713 *j = NULL;
1714 return r;
1715
1716 fail:
1717 j--;
1718 while (j >= r)
1719 free(*j);
1720
1721 free(r);
1722
1723 return NULL;
1724 }
1725
1726 int unit_watch_bus_name(Unit *u, const char *name) {
1727 assert(u);
1728 assert(name);
1729
1730 /* Watch a specific name on the bus. We only support one unit
1731 * watching each name for now. */
1732
1733 return hashmap_put(u->meta.manager->watch_bus, name, u);
1734 }
1735
1736 void unit_unwatch_bus_name(Unit *u, const char *name) {
1737 assert(u);
1738 assert(name);
1739
1740 hashmap_remove_value(u->meta.manager->watch_bus, name, u);
1741 }
1742
1743 bool unit_can_serialize(Unit *u) {
1744 assert(u);
1745
1746 return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item;
1747 }
1748
1749 int unit_serialize(Unit *u, FILE *f, FDSet *fds) {
1750 int r;
1751
1752 assert(u);
1753 assert(f);
1754 assert(fds);
1755
1756 if (!unit_can_serialize(u))
1757 return 0;
1758
1759 if ((r = UNIT_VTABLE(u)->serialize(u, f, fds)) < 0)
1760 return r;
1761
1762 /* End marker */
1763 fputc('\n', f);
1764 return 0;
1765 }
1766
1767 void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *format, ...) {
1768 va_list ap;
1769
1770 assert(u);
1771 assert(f);
1772 assert(key);
1773 assert(format);
1774
1775 fputs(key, f);
1776 fputc('=', f);
1777
1778 va_start(ap, format);
1779 vfprintf(f, format, ap);
1780 va_end(ap);
1781
1782 fputc('\n', f);
1783 }
1784
1785 void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value) {
1786 assert(u);
1787 assert(f);
1788 assert(key);
1789 assert(value);
1790
1791 fprintf(f, "%s=%s\n", key, value);
1792 }
1793
1794 int unit_deserialize(Unit *u, FILE *f, FDSet *fds) {
1795 int r;
1796
1797 assert(u);
1798 assert(f);
1799 assert(fds);
1800
1801 if (!unit_can_serialize(u))
1802 return 0;
1803
1804 for (;;) {
1805 char line[1024], *l, *v;
1806 size_t k;
1807
1808 if (!fgets(line, sizeof(line), f)) {
1809 if (feof(f))
1810 return 0;
1811 return -errno;
1812 }
1813
1814 l = strstrip(line);
1815
1816 /* End marker */
1817 if (l[0] == 0)
1818 return 0;
1819
1820 k = strcspn(l, "=");
1821
1822 if (l[k] == '=') {
1823 l[k] = 0;
1824 v = l+k+1;
1825 } else
1826 v = l+k;
1827
1828 if ((r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds)) < 0)
1829 return r;
1830 }
1831 }
1832
1833
1834 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
1835 [UNIT_SERVICE] = "service",
1836 [UNIT_TIMER] = "timer",
1837 [UNIT_SOCKET] = "socket",
1838 [UNIT_TARGET] = "target",
1839 [UNIT_DEVICE] = "device",
1840 [UNIT_MOUNT] = "mount",
1841 [UNIT_AUTOMOUNT] = "automount",
1842 [UNIT_SNAPSHOT] = "snapshot"
1843 };
1844
1845 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
1846
1847 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
1848 [UNIT_STUB] = "stub",
1849 [UNIT_LOADED] = "loaded",
1850 [UNIT_FAILED] = "failed",
1851 [UNIT_MERGED] = "merged"
1852 };
1853
1854 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
1855
1856 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
1857 [UNIT_ACTIVE] = "active",
1858 [UNIT_INACTIVE] = "inactive",
1859 [UNIT_ACTIVATING] = "activating",
1860 [UNIT_DEACTIVATING] = "deactivating"
1861 };
1862
1863 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
1864
1865 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
1866 [UNIT_REQUIRES] = "Requires",
1867 [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable",
1868 [UNIT_WANTS] = "Wants",
1869 [UNIT_REQUISITE] = "Requisite",
1870 [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable",
1871 [UNIT_REQUIRED_BY] = "RequiredBy",
1872 [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable",
1873 [UNIT_WANTED_BY] = "WantedBy",
1874 [UNIT_CONFLICTS] = "Conflicts",
1875 [UNIT_BEFORE] = "Before",
1876 [UNIT_AFTER] = "After",
1877 [UNIT_REFERENCES] = "References",
1878 [UNIT_REFERENCED_BY] = "ReferencedBy"
1879 };
1880
1881 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);
1882
1883 static const char* const kill_mode_table[_KILL_MODE_MAX] = {
1884 [KILL_CONTROL_GROUP] = "control-group",
1885 [KILL_PROCESS_GROUP] = "process-group",
1886 [KILL_PROCESS] = "process",
1887 [KILL_NONE] = "none"
1888 };
1889
1890 DEFINE_STRING_TABLE_LOOKUP(kill_mode, KillMode);