]> git.ipfire.org Git - people/ms/systemd.git/blame - unit.c
manager: fix GC algorithm
[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"
9e2f7c11
LP
38#include "unit-name.h"
39#include "specifier.h"
4139c1b2 40#include "dbus-unit.h"
87f0e418
LP
41
42const 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
87f0e418
LP
53Unit *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
f278026d
LP
72bool 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
87f0e418
LP
79int unit_add_name(Unit *u, const char *text) {
80 UnitType t;
9e2f7c11 81 char *s = NULL, *i = NULL;
87f0e418
LP
82 int r;
83
84 assert(u);
85 assert(text);
86
9e2f7c11
LP
87 if (unit_name_is_template(text)) {
88 if (!u->meta.instance)
89 return -EINVAL;
87f0e418 90
9e2f7c11
LP
91 s = unit_name_replace_instance(text, u->meta.instance);
92 } else
93 s = strdup(text);
87f0e418 94
9e2f7c11
LP
95 if (!s)
96 return -ENOMEM;
87f0e418 97
9e2f7c11
LP
98 if (!unit_name_is_valid(s)) {
99 r = -EINVAL;
100 goto fail;
101 }
e537352b 102
9e2f7c11 103 assert_se((t = unit_name_to_type(s)) >= 0);
87f0e418 104
9e2f7c11
LP
105 if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type) {
106 r = -EINVAL;
107 goto fail;
108 }
87f0e418 109
9e2f7c11
LP
110 if ((r = unit_name_to_instance(s, &i)) < 0)
111 goto fail;
87f0e418 112
9e2f7c11
LP
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
4f0f902f
LP
128 if (hashmap_size(u->meta.manager->units) >= MANAGER_MAX_NAMES) {
129 r = -E2BIG;
130 goto fail;
131 }
132
9e2f7c11
LP
133 if ((r = set_put(u->meta.names, s)) < 0) {
134 if (r == -EEXIST)
135 r = 0;
136 goto fail;
87f0e418
LP
137 }
138
139 if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
140 set_remove(u->meta.names, s);
9e2f7c11 141 goto fail;
87f0e418
LP
142 }
143
e537352b 144 if (u->meta.type == _UNIT_TYPE_INVALID) {
ef734fd6 145
e537352b 146 u->meta.type = t;
9e2f7c11
LP
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);
e537352b
LP
151
152 if (UNIT_VTABLE(u)->init)
153 UNIT_VTABLE(u)->init(u);
9e2f7c11
LP
154 } else
155 free(i);
87f0e418 156
c1e1601e 157 unit_add_to_dbus_queue(u);
87f0e418 158 return 0;
9e2f7c11
LP
159
160fail:
161 free(s);
162 free(i);
163
164 return r;
87f0e418
LP
165}
166
0ae97ec1 167int unit_choose_id(Unit *u, const char *name) {
9e2f7c11 168 char *s, *t = NULL;
0ae97ec1
LP
169
170 assert(u);
171 assert(name);
172
9e2f7c11
LP
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
0ae97ec1 184 /* Selects one of the names of this unit as the id */
9e2f7c11
LP
185 s = set_get(u->meta.names, (char*) name);
186 free(t);
0ae97ec1 187
9e2f7c11 188 if (!s)
0ae97ec1
LP
189 return -ENOENT;
190
191 u->meta.id = s;
c1e1601e 192 unit_add_to_dbus_queue(u);
9e2f7c11 193
0ae97ec1
LP
194 return 0;
195}
196
f50e0a01
LP
197int 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;
c1e1601e
LP
207
208 unit_add_to_dbus_queue(u);
f50e0a01
LP
209 return 0;
210}
211
701cc384
LP
212bool 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
87f0e418
LP
231void unit_add_to_load_queue(Unit *u) {
232 assert(u);
819e213f 233 assert(u->meta.type != _UNIT_TYPE_INVALID);
87f0e418
LP
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
23a177ef
LP
242void 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
701cc384
LP
252void 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
c1e1601e
LP
270void unit_add_to_dbus_queue(Unit *u) {
271 assert(u);
819e213f 272 assert(u->meta.type != _UNIT_TYPE_INVALID);
c1e1601e
LP
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
87f0e418
LP
281static 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);
701cc384
LP
295
296 unit_add_to_gc_queue(other);
87f0e418
LP
297 }
298
299 set_free(s);
300}
301
302void unit_free(Unit *u) {
303 UnitDependency d;
304 Iterator i;
305 char *t;
306
307 assert(u);
308
c1e1601e
LP
309 bus_unit_send_removed_signal(u);
310
87f0e418 311 /* Detach from next 'bigger' objects */
87f0e418
LP
312 SET_FOREACH(t, u->meta.names, i)
313 hashmap_remove_value(u->meta.manager->units, t, u);
314
ef734fd6
LP
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
87f0e418
LP
318 if (u->meta.in_load_queue)
319 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
320
c1e1601e
LP
321 if (u->meta.in_dbus_queue)
322 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
323
23a177ef
LP
324 if (u->meta.in_cleanup_queue)
325 LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
326
701cc384
LP
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
e537352b
LP
332 /* Free data and next 'smaller' objects */
333 if (u->meta.job)
334 job_free(u->meta.job);
335
23a177ef 336 if (u->meta.load_state != UNIT_STUB)
87f0e418
LP
337 if (UNIT_VTABLE(u)->done)
338 UNIT_VTABLE(u)->done(u);
339
e537352b 340 cgroup_bonding_free_list(u->meta.cgroup_bondings);
87f0e418
LP
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);
6be1e7d5 346 free(u->meta.fragment_path);
87f0e418
LP
347
348 while ((t = set_steal_first(u->meta.names)))
349 free(t);
350 set_free(u->meta.names);
351
9e2f7c11
LP
352 free(u->meta.instance);
353
87f0e418
LP
354 free(u);
355}
356
357UnitActiveState 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
10a94420
LP
366const char* unit_sub_state_to_string(Unit *u) {
367 assert(u);
368
369 return UNIT_VTABLE(u)->sub_state_to_string(u);
370}
371
23a177ef
LP
372static void complete_move(Set **s, Set **other) {
373 assert(s);
374 assert(other);
87f0e418 375
23a177ef
LP
376 if (!*other)
377 return;
87f0e418
LP
378
379 if (*s)
23a177ef
LP
380 set_move(*s, *other);
381 else {
382 *s = *other;
383 *other = NULL;
384 }
385}
87f0e418 386
23a177ef
LP
387static void merge_names(Unit *u, Unit *other) {
388 char *t;
389 Iterator i;
87f0e418 390
23a177ef
LP
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);
87f0e418
LP
405}
406
23a177ef
LP
407static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
408 Iterator i;
409 Unit *back;
87f0e418 410 int r;
23a177ef
LP
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
435int unit_merge(Unit *u, Unit *other) {
87f0e418
LP
436 UnitDependency d;
437
438 assert(u);
439 assert(other);
440 assert(u->meta.manager == other->meta.manager);
9e2f7c11 441 assert(u->meta.type != _UNIT_TYPE_INVALID);
87f0e418 442
cc916967
LP
443 other = unit_follow_merge(other);
444
23a177ef
LP
445 if (other == u)
446 return 0;
447
9e2f7c11
LP
448 if (u->meta.type != other->meta.type)
449 return -EINVAL;
450
451 if (!streq_ptr(u->meta.instance, other->meta.instance))
87f0e418
LP
452 return -EINVAL;
453
cc916967
LP
454 if (other->meta.load_state != UNIT_STUB &&
455 other->meta.load_state != UNIT_FAILED)
23a177ef 456 return -EEXIST;
87f0e418 457
819e213f
LP
458 if (other->meta.job)
459 return -EEXIST;
460
461 if (unit_active_state(other) != UNIT_INACTIVE)
462 return -EEXIST;
463
87f0e418 464 /* Merge names */
23a177ef 465 merge_names(u, other);
87f0e418
LP
466
467 /* Merge dependencies */
468 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
23a177ef 469 merge_dependencies(u, other, d);
87f0e418 470
23a177ef
LP
471 other->meta.load_state = UNIT_MERGED;
472 other->meta.merged_into = u;
473
3616a49c
LP
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);
23a177ef
LP
481 unit_add_to_cleanup_queue(other);
482
483 return 0;
484}
485
486int unit_merge_by_name(Unit *u, const char *name) {
487 Unit *other;
9e2f7c11
LP
488 int r;
489 char *s = NULL;
23a177ef
LP
490
491 assert(u);
492 assert(name);
493
9e2f7c11
LP
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
23a177ef 504 if (!(other = manager_get_unit(u->meta.manager, name)))
9e2f7c11
LP
505 r = unit_add_name(u, name);
506 else
507 r = unit_merge(u, other);
23a177ef 508
9e2f7c11
LP
509 free(s);
510 return r;
23a177ef
LP
511}
512
513Unit* 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
522int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
523 int r;
524
525 assert(u);
526 assert(c);
527
80876c20 528 if (c->std_output != EXEC_OUTPUT_KERNEL && c->std_output != EXEC_OUTPUT_SYSLOG)
23a177ef
LP
529 return 0;
530
531 /* If syslog or kernel logging is requested, make sure our own
532 * logging daemon is run first. */
533
701cc384 534 if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
23a177ef
LP
535 return r;
536
537 if (u->meta.manager->running_as != MANAGER_SESSION)
701cc384 538 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
23a177ef
LP
539 return r;
540
87f0e418
LP
541 return 0;
542}
543
87f0e418
LP
544const char *unit_description(Unit *u) {
545 assert(u);
546
547 if (u->meta.description)
548 return u->meta.description;
549
9e2f7c11 550 return u->meta.id;
87f0e418
LP
551}
552
553void unit_dump(Unit *u, FILE *f, const char *prefix) {
87f0e418
LP
554 char *t;
555 UnitDependency d;
556 Iterator i;
47be870b
LP
557 char *p2;
558 const char *prefix2;
8e274523 559 CGroupBonding *b;
2fad8195 560 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
87f0e418
LP
561
562 assert(u);
9e2f7c11 563 assert(u->meta.type >= 0);
87f0e418
LP
564
565 if (!prefix)
566 prefix = "";
47be870b
LP
567 p2 = strappend(prefix, "\t");
568 prefix2 = p2 ? p2 : prefix;
87f0e418
LP
569
570 fprintf(f,
571 "%s→ Unit %s:\n"
572 "%s\tDescription: %s\n"
9e2f7c11 573 "%s\tInstance: %s\n"
87f0e418 574 "%s\tUnit Load State: %s\n"
2fad8195
LP
575 "%s\tUnit Active State: %s\n"
576 "%s\tActive Enter Timestamp: %s\n"
701cc384
LP
577 "%s\tActive Exit Timestamp: %s\n"
578 "%s\tGC Check Good: %s\n",
9e2f7c11 579 prefix, u->meta.id,
87f0e418 580 prefix, unit_description(u),
9e2f7c11 581 prefix, strna(u->meta.instance),
94f04347 582 prefix, unit_load_state_to_string(u->meta.load_state),
2fad8195
LP
583 prefix, unit_active_state_to_string(unit_active_state(u)),
584 prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.active_enter_timestamp)),
701cc384
LP
585 prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->meta.active_exit_timestamp)),
586 prefix, yes_no(unit_check_gc(u)));
0301abf4 587
87f0e418
LP
588 SET_FOREACH(t, u->meta.names, i)
589 fprintf(f, "%s\tName: %s\n", prefix, t);
590
23a177ef
LP
591 if (u->meta.fragment_path)
592 fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);
593
87f0e418
LP
594 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
595 Unit *other;
596
87f0e418 597 SET_FOREACH(other, u->meta.dependencies[d], i)
9e2f7c11 598 fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), other->meta.id);
87f0e418
LP
599 }
600
23a177ef 601 if (u->meta.load_state == UNIT_LOADED) {
b0650475
LP
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
23a177ef
LP
608 LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
609 fprintf(f, "%s\tControlGroup: %s:%s\n",
610 prefix, b->controller, b->path);
8e274523 611
23a177ef
LP
612 if (UNIT_VTABLE(u)->dump)
613 UNIT_VTABLE(u)->dump(u, f, prefix2);
b0650475
LP
614
615 } else if (u->meta.load_state == UNIT_MERGED)
616 fprintf(f,
617 "%s\tMerged into: %s\n",
9e2f7c11 618 prefix, u->meta.merged_into->meta.id);
87f0e418
LP
619
620 if (u->meta.job)
621 job_dump(u->meta.job, f, prefix2);
622
47be870b 623 free(p2);
87f0e418
LP
624}
625
626/* Common implementation for multiple backends */
e537352b 627int unit_load_fragment_and_dropin(Unit *u) {
23a177ef
LP
628 int r;
629
630 assert(u);
23a177ef
LP
631
632 /* Load a .service file */
e537352b 633 if ((r = unit_load_fragment(u)) < 0)
23a177ef
LP
634 return r;
635
e537352b 636 if (u->meta.load_state == UNIT_STUB)
23a177ef
LP
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 */
e537352b 647int unit_load_fragment_and_dropin_optional(Unit *u) {
23a177ef 648 int r;
87f0e418
LP
649
650 assert(u);
651
23a177ef
LP
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 */
e537352b 656 if ((r = unit_load_fragment(u)) < 0)
87f0e418
LP
657 return r;
658
e537352b
LP
659 if (u->meta.load_state == UNIT_STUB)
660 u->meta.load_state = UNIT_LOADED;
d46de8a1 661
87f0e418 662 /* Load drop-in directory data */
23a177ef 663 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
87f0e418
LP
664 return r;
665
23a177ef 666 return 0;
87f0e418
LP
667}
668
a16e1123
LP
669/* Common implementation for multiple backends */
670int 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
87f0e418
LP
679int 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
e537352b
LP
689 if (u->meta.type == _UNIT_TYPE_INVALID)
690 return -EINVAL;
691
87f0e418
LP
692 if (u->meta.load_state != UNIT_STUB)
693 return 0;
694
e537352b
LP
695 if (UNIT_VTABLE(u)->load)
696 if ((r = UNIT_VTABLE(u)->load(u)) < 0)
87f0e418 697 goto fail;
23a177ef 698
e537352b 699 if (u->meta.load_state == UNIT_STUB) {
23a177ef
LP
700 r = -ENOENT;
701 goto fail;
702 }
703
23a177ef
LP
704 assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);
705
706 unit_add_to_dbus_queue(unit_follow_merge(u));
701cc384 707 unit_add_to_gc_queue(u);
87f0e418 708
87f0e418
LP
709 return 0;
710
711fail:
712 u->meta.load_state = UNIT_FAILED;
c1e1601e 713 unit_add_to_dbus_queue(u);
23a177ef 714
9e2f7c11 715 log_error("Failed to load configuration for %s: %s", u->meta.id, strerror(-r));
23a177ef 716
87f0e418
LP
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 */
725int unit_start(Unit *u) {
726 UnitActiveState state;
727
728 assert(u);
729
7898b0cf
LP
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. */
87f0e418
LP
734 state = unit_active_state(u);
735 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
736 return -EALREADY;
737
7898b0cf
LP
738 /* If it is stopped, but we cannot start it, then fail */
739 if (!UNIT_VTABLE(u)->start)
740 return -EBADR;
741
87f0e418
LP
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
c1e1601e 748 unit_add_to_dbus_queue(u);
87f0e418
LP
749 return UNIT_VTABLE(u)->start(u);
750}
751
752bool 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 */
763int unit_stop(Unit *u) {
764 UnitActiveState state;
765
766 assert(u);
767
87f0e418
LP
768 state = unit_active_state(u);
769 if (state == UNIT_INACTIVE)
770 return -EALREADY;
771
7898b0cf
LP
772 if (!UNIT_VTABLE(u)->stop)
773 return -EBADR;
774
c1e1601e 775 unit_add_to_dbus_queue(u);
87f0e418
LP
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 */
784int 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
c1e1601e 799 unit_add_to_dbus_queue(u);
87f0e418
LP
800 return UNIT_VTABLE(u)->reload(u);
801}
802
803bool 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
f3bff0eb
LP
815static 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
9e2f7c11 834 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
f3bff0eb
LP
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
9e2f7c11 842 log_debug("Service %s is not needed anymore. Stopping.", u->meta.id);
f3bff0eb
LP
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
87f0e418
LP
848static 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
9e2f7c11 859 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
87f0e418
LP
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
876static 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
f3bff0eb
LP
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)
87f0e418 892 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
f3bff0eb 893 unit_check_uneeded(other);
9e2f7c11 894 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
f3bff0eb
LP
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);
9e2f7c11 903 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
f3bff0eb
LP
904 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
905 unit_check_uneeded(other);
87f0e418
LP
906}
907
908void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
a096ed36
LP
909 bool unexpected = false;
910
87f0e418
LP
911 assert(u);
912 assert(os < _UNIT_ACTIVE_STATE_MAX);
913 assert(ns < _UNIT_ACTIVE_STATE_MAX);
87f0e418 914
8e471ccd
LP
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! */
87f0e418
LP
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. */
c1e1601e 934 job_add_to_run_queue(u->meta.job);
87f0e418
LP
935
936 else {
937 assert(u->meta.job->state == JOB_RUNNING);
938
f50e0a01 939 /* Let's check whether this state change
87f0e418
LP
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
a096ed36 949 if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
87f0e418 950 job_finish_and_invalidate(u->meta.job, true);
a096ed36
LP
951 else if (ns != UNIT_ACTIVATING) {
952 unexpected = true;
87f0e418 953 job_finish_and_invalidate(u->meta.job, false);
a096ed36 954 }
87f0e418
LP
955
956 break;
957
958 case JOB_RELOAD:
959 case JOB_RELOAD_OR_START:
960
a096ed36 961 if (ns == UNIT_ACTIVE)
87f0e418 962 job_finish_and_invalidate(u->meta.job, true);
a096ed36
LP
963 else if (ns != UNIT_ACTIVATING && ns != UNIT_ACTIVE_RELOADING) {
964 unexpected = true;
87f0e418 965 job_finish_and_invalidate(u->meta.job, false);
a096ed36 966 }
87f0e418
LP
967
968 break;
969
970 case JOB_STOP:
971 case JOB_RESTART:
972 case JOB_TRY_RESTART:
973
a096ed36 974 if (ns == UNIT_INACTIVE)
87f0e418 975 job_finish_and_invalidate(u->meta.job, true);
a096ed36
LP
976 else if (ns != UNIT_DEACTIVATING) {
977 unexpected = true;
87f0e418 978 job_finish_and_invalidate(u->meta.job, false);
a096ed36 979 }
87f0e418
LP
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
a096ed36
LP
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 }
f3bff0eb 998
e537352b
LP
999 /* Some names are special */
1000 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
f278026d 1001 if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) {
e537352b
LP
1002 /* The bus just might have become available,
1003 * hence try to connect to it, if we aren't
1004 * yet connected. */
f278026d
LP
1005 bus_init_system(u->meta.manager);
1006 bus_init_api(u->meta.manager);
1007 }
1008
e537352b
LP
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. */
2882c7ed 1013 log_open_syslog();
f278026d 1014
e537352b
LP
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)) {
f278026d
LP
1026
1027 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
e537352b
LP
1028 /* The syslog daemon might just have
1029 * terminated, hence try to disconnect from
1030 * it. */
2882c7ed 1031 log_close_syslog();
f278026d
LP
1032
1033 /* We don't care about D-Bus here, since we'll get an
1034 * asynchronous notification for it anyway. */
1035 }
1036
f3bff0eb
LP
1037 /* Maybe we finished startup and are now ready for being
1038 * stopped because unneeded? */
1039 unit_check_uneeded(u);
c1e1601e
LP
1040
1041 unit_add_to_dbus_queue(u);
701cc384 1042 unit_add_to_gc_queue(u);
87f0e418
LP
1043}
1044
acbb0225 1045int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
87f0e418
LP
1046 struct epoll_event ev;
1047
1048 assert(u);
1049 assert(fd >= 0);
acbb0225 1050 assert(w);
ea430986 1051 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
87f0e418
LP
1052
1053 zero(ev);
acbb0225 1054 ev.data.ptr = w;
87f0e418
LP
1055 ev.events = events;
1056
acbb0225
LP
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;
87f0e418 1062
acbb0225
LP
1063 w->fd = fd;
1064 w->type = WATCH_FD;
ea430986 1065 w->data.unit = u;
87f0e418 1066
acbb0225 1067 return 0;
87f0e418
LP
1068}
1069
acbb0225 1070void unit_unwatch_fd(Unit *u, Watch *w) {
87f0e418 1071 assert(u);
acbb0225 1072 assert(w);
87f0e418 1073
acbb0225
LP
1074 if (w->type == WATCH_INVALID)
1075 return;
1076
ea430986
LP
1077 assert(w->type == WATCH_FD);
1078 assert(w->data.unit == u);
acbb0225
LP
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;
ea430986 1083 w->data.unit = NULL;
87f0e418
LP
1084}
1085
1086int unit_watch_pid(Unit *u, pid_t pid) {
1087 assert(u);
1088 assert(pid >= 1);
1089
05e343b7
LP
1090 /* Watch a specific PID. We only support one unit watching
1091 * each PID for now. */
1092
87f0e418
LP
1093 return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
1094}
1095
1096void unit_unwatch_pid(Unit *u, pid_t pid) {
1097 assert(u);
1098 assert(pid >= 1);
1099
05e343b7 1100 hashmap_remove_value(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
87f0e418
LP
1101}
1102
acbb0225 1103int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
87f0e418 1104 struct itimerspec its;
acbb0225 1105 int flags, fd;
87f0e418
LP
1106 bool ours;
1107
1108 assert(u);
acbb0225 1109 assert(w);
ea430986 1110 assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->data.unit == u));
87f0e418
LP
1111
1112 /* This will try to reuse the old timer if there is one */
1113
acbb0225 1114 if (w->type == WATCH_TIMER) {
87f0e418 1115 ours = false;
acbb0225 1116 fd = w->fd;
87f0e418
LP
1117 } else {
1118 ours = true;
87f0e418
LP
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
acbb0225
LP
1141 if (w->type == WATCH_INVALID) {
1142 struct epoll_event ev;
87f0e418 1143
acbb0225
LP
1144 zero(ev);
1145 ev.data.ptr = w;
f94ea366 1146 ev.events = EPOLLIN;
acbb0225
LP
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;
ea430986 1154 w->data.unit = u;
87f0e418 1155
87f0e418
LP
1156 return 0;
1157
1158fail:
1159 if (ours)
ea430986 1160 close_nointr_nofail(fd);
87f0e418
LP
1161
1162 return -errno;
1163}
1164
acbb0225 1165void unit_unwatch_timer(Unit *u, Watch *w) {
87f0e418 1166 assert(u);
acbb0225 1167 assert(w);
87f0e418 1168
acbb0225 1169 if (w->type == WATCH_INVALID)
87f0e418
LP
1170 return;
1171
ea430986 1172 assert(w->type == WATCH_TIMER && w->data.unit == u);
acbb0225
LP
1173
1174 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
a16e1123 1175 close_nointr_nofail(w->fd);
acbb0225
LP
1176
1177 w->fd = -1;
1178 w->type = WATCH_INVALID;
ea430986 1179 w->data.unit = NULL;
87f0e418
LP
1180}
1181
1182bool 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
701cc384 1208int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference) {
87f0e418
LP
1209
1210 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
1211 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
9e2f7c11 1212 [UNIT_REQUIRES_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE,
87f0e418
LP
1213 [UNIT_WANTS] = UNIT_WANTED_BY,
1214 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
9e2f7c11 1215 [UNIT_REQUISITE_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE,
87f0e418 1216 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
9e2f7c11 1217 [UNIT_REQUIRED_BY_OVERRIDABLE] = _UNIT_DEPENDENCY_INVALID,
87f0e418
LP
1218 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
1219 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
1220 [UNIT_BEFORE] = UNIT_AFTER,
701cc384
LP
1221 [UNIT_AFTER] = UNIT_BEFORE,
1222 [UNIT_REFERENCES] = UNIT_REFERENCED_BY,
1223 [UNIT_REFERENCED_BY] = UNIT_REFERENCES
87f0e418 1224 };
701cc384 1225 int r, q = 0, v = 0, w = 0;
87f0e418
LP
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
9e2f7c11
LP
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
701cc384
LP
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)
87f0e418
LP
1247 return r;
1248
701cc384
LP
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;
87f0e418 1253
701cc384
LP
1254 if ((q = set_put(u->meta.dependencies[d], other)) < 0)
1255 return q;
87f0e418 1256
701cc384
LP
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;
87f0e418
LP
1270 }
1271
c1e1601e 1272 unit_add_to_dbus_queue(u);
87f0e418 1273 return 0;
701cc384
LP
1274
1275fail:
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;
87f0e418 1286}
0301abf4 1287
9e2f7c11
LP
1288static 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
701cc384 1321int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
09b6b09f
LP
1322 Unit *other;
1323 int r;
9e2f7c11 1324 char *s;
09b6b09f 1325
9e2f7c11
LP
1326 assert(u);
1327 assert(name || path);
09b6b09f 1328
9e2f7c11
LP
1329 if (!(name = resolve_template(u, name, path, &s)))
1330 return -ENOMEM;
09b6b09f 1331
9e2f7c11
LP
1332 if ((r = manager_load_unit(u->meta.manager, name, path, &other)) < 0)
1333 goto finish;
1334
701cc384 1335 r = unit_add_dependency(u, d, other, add_reference);
9e2f7c11
LP
1336
1337finish:
1338 free(s);
1339 return r;
09b6b09f
LP
1340}
1341
701cc384 1342int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
bd77d0fc
LP
1343 Unit *other;
1344 int r;
9e2f7c11 1345 char *s;
bd77d0fc 1346
9e2f7c11
LP
1347 assert(u);
1348 assert(name || path);
bd77d0fc 1349
9e2f7c11
LP
1350 if (!(name = resolve_template(u, name, path, &s)))
1351 return -ENOMEM;
bd77d0fc 1352
9e2f7c11
LP
1353 if ((r = manager_load_unit(u->meta.manager, name, path, &other)) < 0)
1354 goto finish;
1355
701cc384 1356 r = unit_add_dependency(other, d, u, add_reference);
9e2f7c11
LP
1357
1358finish:
1359 free(s);
1360 return r;
bd77d0fc
LP
1361}
1362
0301abf4
LP
1363int 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
036643a2 1383 if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
0301abf4
LP
1384 r = -errno;
1385 free(c);
1386 return r;
1387 }
1388
1389 return 0;
1390}
88066b3a 1391
ea430986
LP
1392char *unit_dbus_path(Unit *u) {
1393 char *p, *e;
1394
1395 assert(u);
1396
9e2f7c11 1397 if (!(e = bus_path_escape(u->meta.id)))
ea430986
LP
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
8e274523
LP
1409int 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
013b87c0
LP
1434static char *default_cgroup_path(Unit *u) {
1435 char *p;
4f2d528d 1436 int r;
013b87c0
LP
1437
1438 assert(u);
1439
4f2d528d
LP
1440 if (u->meta.instance) {
1441 char *t;
013b87c0 1442
4f2d528d
LP
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;
013b87c0
LP
1452}
1453
8e274523
LP
1454int unit_add_cgroup_from_text(Unit *u, const char *name) {
1455 size_t n;
013b87c0
LP
1456 char *controller = NULL, *path = NULL;
1457 CGroupBonding *b = NULL;
8e274523
LP
1458 int r;
1459
1460 assert(u);
1461 assert(name);
1462
1463 /* Detect controller name */
013b87c0 1464 n = strcspn(name, ":");
8e274523 1465
013b87c0
LP
1466 if (name[n] == 0 ||
1467 (name[n] == ':' && name[n+1] == 0)) {
8e274523 1468
013b87c0 1469 /* Only controller name, no path? */
8e274523 1470
013b87c0
LP
1471 if (!(path = default_cgroup_path(u)))
1472 return -ENOMEM;
8e274523 1473
013b87c0
LP
1474 } else {
1475 const char *p;
8e274523 1476
013b87c0
LP
1477 /* Controller name, and path. */
1478 p = name+n+1;
8e274523 1479
013b87c0
LP
1480 if (!path_is_absolute(p))
1481 return -EINVAL;
1482
1483 if (!(path = strdup(p)))
1484 return -ENOMEM;
8e274523
LP
1485 }
1486
013b87c0
LP
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;
8e274523
LP
1495 }
1496
013b87c0
LP
1497 if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
1498 r = -EEXIST;
1499 goto fail;
1500 }
8e274523 1501
013b87c0 1502 if (!(b = new0(CGroupBonding, 1))) {
8e274523
LP
1503 r = -ENOMEM;
1504 goto fail;
1505 }
1506
013b87c0
LP
1507 b->controller = controller;
1508 b->path = path;
8e274523
LP
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
1517fail:
013b87c0
LP
1518 free(path);
1519 free(controller);
8e274523
LP
1520 free(b);
1521
1522 return r;
1523}
1524
1525int 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
013b87c0 1542 if (!(b->path = default_cgroup_path(u)))
8e274523
LP
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
1553fail:
1554 free(b->path);
1555 free(b->controller);
1556 free(b);
1557
1558 return r;
1559}
1560
1561CGroupBonding* 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
f6ff8c29
LP
1567int 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
9e2f7c11 1575 if (!(t = unit_name_change_suffix(u->meta.id, type)))
f6ff8c29
LP
1576 return -ENOMEM;
1577
1578 assert(!unit_has_name(u, t));
1579
9e2f7c11 1580 r = manager_load_unit(u->meta.manager, t, NULL, _found);
f6ff8c29
LP
1581 free(t);
1582
9e2f7c11 1583 assert(r < 0 || *_found != u);
f6ff8c29
LP
1584
1585 return r;
1586}
1587
a16e1123
LP
1588int 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
9e2f7c11
LP
1611static 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
1618static 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
1625static 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
1640static 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
1650char *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
1676char *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
1697char **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
1716fail:
1717 j--;
1718 while (j >= r)
1719 free(*j);
1720
1721 free(r);
1722
1723 return NULL;
1724}
1725
05e343b7
LP
1726int 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
1736void 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
a16e1123
LP
1743bool unit_can_serialize(Unit *u) {
1744 assert(u);
1745
1746 return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item;
1747}
1748
1749int 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
1767void 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
1785void 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
1794int 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
94f04347
LP
1834static 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
1845DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
1846
1847static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
1848 [UNIT_STUB] = "stub",
1849 [UNIT_LOADED] = "loaded",
23a177ef
LP
1850 [UNIT_FAILED] = "failed",
1851 [UNIT_MERGED] = "merged"
94f04347
LP
1852};
1853
1854DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
1855
1856static 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
1863DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
1864
1865static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
1866 [UNIT_REQUIRES] = "Requires",
9e2f7c11 1867 [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable",
94f04347
LP
1868 [UNIT_WANTS] = "Wants",
1869 [UNIT_REQUISITE] = "Requisite",
9e2f7c11 1870 [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable",
94f04347 1871 [UNIT_REQUIRED_BY] = "RequiredBy",
9e2f7c11 1872 [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable",
94f04347
LP
1873 [UNIT_WANTED_BY] = "WantedBy",
1874 [UNIT_CONFLICTS] = "Conflicts",
1875 [UNIT_BEFORE] = "Before",
1876 [UNIT_AFTER] = "After",
701cc384
LP
1877 [UNIT_REFERENCES] = "References",
1878 [UNIT_REFERENCED_BY] = "ReferencedBy"
94f04347
LP
1879};
1880
1881DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);
50159e6a
LP
1882
1883static const char* const kill_mode_table[_KILL_MODE_MAX] = {
80876c20 1884 [KILL_CONTROL_GROUP] = "control-group",
50159e6a 1885 [KILL_PROCESS_GROUP] = "process-group",
80876c20
LP
1886 [KILL_PROCESS] = "process",
1887 [KILL_NONE] = "none"
50159e6a
LP
1888};
1889
1890DEFINE_STRING_TABLE_LOOKUP(kill_mode, KillMode);