]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/unit.c
service: add dependencies on configured sockets
[thirdparty/systemd.git] / src / unit.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
87f0e418 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>
45fb0699 30#include <sys/stat.h>
87f0e418
LP
31
32#include "set.h"
33#include "unit.h"
34#include "macro.h"
35#include "strv.h"
36#include "load-fragment.h"
37#include "load-dropin.h"
38#include "log.h"
9e2f7c11
LP
39#include "unit-name.h"
40#include "specifier.h"
4139c1b2 41#include "dbus-unit.h"
514f4ef5 42#include "special.h"
c6c18be3 43#include "cgroup-util.h"
4927fcae 44#include "missing.h"
ab1f0633 45#include "cgroup-attr.h"
87f0e418
LP
46
47const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
48 [UNIT_SERVICE] = &service_vtable,
49 [UNIT_TIMER] = &timer_vtable,
50 [UNIT_SOCKET] = &socket_vtable,
51 [UNIT_TARGET] = &target_vtable,
52 [UNIT_DEVICE] = &device_vtable,
53 [UNIT_MOUNT] = &mount_vtable,
54 [UNIT_AUTOMOUNT] = &automount_vtable,
07b0b134 55 [UNIT_SNAPSHOT] = &snapshot_vtable,
01f78473
LP
56 [UNIT_SWAP] = &swap_vtable,
57 [UNIT_PATH] = &path_vtable
87f0e418
LP
58};
59
87f0e418
LP
60Unit *unit_new(Manager *m) {
61 Unit *u;
62
63 assert(m);
64
65 if (!(u = new0(Unit, 1)))
66 return NULL;
67
68 if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) {
69 free(u);
70 return NULL;
71 }
72
73 u->meta.manager = m;
74 u->meta.type = _UNIT_TYPE_INVALID;
cca098b0 75 u->meta.deserialized_job = _JOB_TYPE_INVALID;
a40eb732 76 u->meta.default_dependencies = true;
a4375746 77 u->meta.unit_file_state = _UNIT_FILE_STATE_INVALID;
87f0e418
LP
78
79 return u;
80}
81
f278026d
LP
82bool unit_has_name(Unit *u, const char *name) {
83 assert(u);
84 assert(name);
85
86 return !!set_get(u->meta.names, (char*) name);
87}
88
87f0e418
LP
89int unit_add_name(Unit *u, const char *text) {
90 UnitType t;
276c3e78 91 char *s, *i = NULL;
87f0e418
LP
92 int r;
93
94 assert(u);
95 assert(text);
96
9e2f7c11
LP
97 if (unit_name_is_template(text)) {
98 if (!u->meta.instance)
99 return -EINVAL;
87f0e418 100
9e2f7c11
LP
101 s = unit_name_replace_instance(text, u->meta.instance);
102 } else
103 s = strdup(text);
87f0e418 104
9e2f7c11
LP
105 if (!s)
106 return -ENOMEM;
87f0e418 107
b9c0d441 108 if (!unit_name_is_valid(s, false)) {
9e2f7c11
LP
109 r = -EINVAL;
110 goto fail;
111 }
e537352b 112
9e2f7c11 113 assert_se((t = unit_name_to_type(s)) >= 0);
87f0e418 114
9e2f7c11
LP
115 if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type) {
116 r = -EINVAL;
117 goto fail;
118 }
87f0e418 119
9e2f7c11
LP
120 if ((r = unit_name_to_instance(s, &i)) < 0)
121 goto fail;
87f0e418 122
796ba554
LP
123 if (i && unit_vtable[t]->no_instances) {
124 r = -EINVAL;
9e2f7c11 125 goto fail;
796ba554 126 }
9e2f7c11 127
276c3e78
LP
128 /* Ensure that this unit is either instanced or not instanced,
129 * but not both. */
130 if (u->meta.type != _UNIT_TYPE_INVALID && !u->meta.instance != !i) {
9e2f7c11
LP
131 r = -EINVAL;
132 goto fail;
133 }
134
135 if (unit_vtable[t]->no_alias &&
136 !set_isempty(u->meta.names) &&
137 !set_get(u->meta.names, s)) {
138 r = -EEXIST;
139 goto fail;
140 }
141
4f0f902f
LP
142 if (hashmap_size(u->meta.manager->units) >= MANAGER_MAX_NAMES) {
143 r = -E2BIG;
144 goto fail;
145 }
146
9e2f7c11
LP
147 if ((r = set_put(u->meta.names, s)) < 0) {
148 if (r == -EEXIST)
149 r = 0;
150 goto fail;
87f0e418
LP
151 }
152
153 if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
154 set_remove(u->meta.names, s);
9e2f7c11 155 goto fail;
87f0e418
LP
156 }
157
e537352b 158 if (u->meta.type == _UNIT_TYPE_INVALID) {
ef734fd6 159
e537352b 160 u->meta.type = t;
9e2f7c11
LP
161 u->meta.id = s;
162 u->meta.instance = i;
163
ab5c3e3f 164 LIST_PREPEND(Meta, units_by_type, u->meta.manager->units_by_type[t], &u->meta);
e537352b
LP
165
166 if (UNIT_VTABLE(u)->init)
167 UNIT_VTABLE(u)->init(u);
9e2f7c11
LP
168 } else
169 free(i);
87f0e418 170
c1e1601e 171 unit_add_to_dbus_queue(u);
87f0e418 172 return 0;
9e2f7c11
LP
173
174fail:
175 free(s);
176 free(i);
177
178 return r;
87f0e418
LP
179}
180
0ae97ec1 181int unit_choose_id(Unit *u, const char *name) {
276c3e78
LP
182 char *s, *t = NULL, *i;
183 int r;
0ae97ec1
LP
184
185 assert(u);
186 assert(name);
187
9e2f7c11
LP
188 if (unit_name_is_template(name)) {
189
190 if (!u->meta.instance)
191 return -EINVAL;
192
193 if (!(t = unit_name_replace_instance(name, u->meta.instance)))
194 return -ENOMEM;
195
196 name = t;
197 }
198
0ae97ec1 199 /* Selects one of the names of this unit as the id */
9e2f7c11
LP
200 s = set_get(u->meta.names, (char*) name);
201 free(t);
0ae97ec1 202
9e2f7c11 203 if (!s)
0ae97ec1
LP
204 return -ENOENT;
205
276c3e78
LP
206 if ((r = unit_name_to_instance(s, &i)) < 0)
207 return r;
208
0ae97ec1 209 u->meta.id = s;
276c3e78
LP
210
211 free(u->meta.instance);
212 u->meta.instance = i;
213
c1e1601e 214 unit_add_to_dbus_queue(u);
9e2f7c11 215
0ae97ec1
LP
216 return 0;
217}
218
f50e0a01
LP
219int unit_set_description(Unit *u, const char *description) {
220 char *s;
221
222 assert(u);
223
224 if (!(s = strdup(description)))
225 return -ENOMEM;
226
227 free(u->meta.description);
228 u->meta.description = s;
c1e1601e
LP
229
230 unit_add_to_dbus_queue(u);
f50e0a01
LP
231 return 0;
232}
233
701cc384
LP
234bool unit_check_gc(Unit *u) {
235 assert(u);
236
b86d44e5
LP
237 if (u->meta.load_state == UNIT_STUB)
238 return true;
239
701cc384
LP
240 if (UNIT_VTABLE(u)->no_gc)
241 return true;
242
6c073082
LP
243 if (u->meta.no_gc)
244 return true;
245
701cc384
LP
246 if (u->meta.job)
247 return true;
248
249 if (unit_active_state(u) != UNIT_INACTIVE)
250 return true;
251
252 if (UNIT_VTABLE(u)->check_gc)
253 if (UNIT_VTABLE(u)->check_gc(u))
254 return true;
255
256 return false;
257}
258
87f0e418
LP
259void unit_add_to_load_queue(Unit *u) {
260 assert(u);
819e213f 261 assert(u->meta.type != _UNIT_TYPE_INVALID);
87f0e418
LP
262
263 if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
264 return;
265
266 LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
267 u->meta.in_load_queue = true;
268}
269
23a177ef
LP
270void unit_add_to_cleanup_queue(Unit *u) {
271 assert(u);
272
273 if (u->meta.in_cleanup_queue)
274 return;
275
276 LIST_PREPEND(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
277 u->meta.in_cleanup_queue = true;
278}
279
701cc384
LP
280void unit_add_to_gc_queue(Unit *u) {
281 assert(u);
282
283 if (u->meta.in_gc_queue || u->meta.in_cleanup_queue)
284 return;
285
286 if (unit_check_gc(u))
287 return;
288
289 LIST_PREPEND(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta);
290 u->meta.in_gc_queue = true;
291
292 u->meta.manager->n_in_gc_queue ++;
293
294 if (u->meta.manager->gc_queue_timestamp <= 0)
295 u->meta.manager->gc_queue_timestamp = now(CLOCK_MONOTONIC);
296}
297
c1e1601e
LP
298void unit_add_to_dbus_queue(Unit *u) {
299 assert(u);
819e213f 300 assert(u->meta.type != _UNIT_TYPE_INVALID);
c1e1601e 301
94b6dfa2 302 if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue)
c1e1601e
LP
303 return;
304
a567261a
LP
305 /* Shortcut things if nobody cares */
306 if (!bus_has_subscriber(u->meta.manager)) {
94b6dfa2
LP
307 u->meta.sent_dbus_new_signal = true;
308 return;
309 }
310
c1e1601e
LP
311 LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
312 u->meta.in_dbus_queue = true;
313}
314
87f0e418
LP
315static void bidi_set_free(Unit *u, Set *s) {
316 Iterator i;
317 Unit *other;
318
319 assert(u);
320
321 /* Frees the set and makes sure we are dropped from the
322 * inverse pointers */
323
324 SET_FOREACH(other, s, i) {
325 UnitDependency d;
326
327 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
328 set_remove(other->meta.dependencies[d], u);
701cc384
LP
329
330 unit_add_to_gc_queue(other);
87f0e418
LP
331 }
332
333 set_free(s);
334}
335
336void unit_free(Unit *u) {
337 UnitDependency d;
338 Iterator i;
339 char *t;
340
341 assert(u);
342
c1e1601e
LP
343 bus_unit_send_removed_signal(u);
344
a013b84b
LP
345 if (u->meta.load_state != UNIT_STUB)
346 if (UNIT_VTABLE(u)->done)
347 UNIT_VTABLE(u)->done(u);
348
87f0e418
LP
349 SET_FOREACH(t, u->meta.names, i)
350 hashmap_remove_value(u->meta.manager->units, t, u);
351
964e0949
LP
352 if (u->meta.job)
353 job_free(u->meta.job);
354
355 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
356 bidi_set_free(u, u->meta.dependencies[d]);
357
ef734fd6 358 if (u->meta.type != _UNIT_TYPE_INVALID)
ab5c3e3f 359 LIST_REMOVE(Meta, units_by_type, u->meta.manager->units_by_type[u->meta.type], &u->meta);
ef734fd6 360
87f0e418
LP
361 if (u->meta.in_load_queue)
362 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
363
c1e1601e
LP
364 if (u->meta.in_dbus_queue)
365 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
366
23a177ef
LP
367 if (u->meta.in_cleanup_queue)
368 LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
369
701cc384
LP
370 if (u->meta.in_gc_queue) {
371 LIST_REMOVE(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta);
372 u->meta.manager->n_in_gc_queue--;
373 }
374
a7556052 375 cgroup_bonding_free_list(u->meta.cgroup_bondings, u->meta.manager->n_reloading <= 0);
ab1f0633 376 cgroup_attribute_free_list(u->meta.cgroup_attributes);
87f0e418 377
87f0e418 378 free(u->meta.description);
6be1e7d5 379 free(u->meta.fragment_path);
87f0e418 380
53ec43c6 381 set_free_free(u->meta.names);
87f0e418 382
52661efd
LP
383 condition_free_list(u->meta.conditions);
384
9e2f7c11 385 free(u->meta.instance);
87f0e418
LP
386 free(u);
387}
388
389UnitActiveState unit_active_state(Unit *u) {
390 assert(u);
391
6124958c
LP
392 if (u->meta.load_state == UNIT_MERGED)
393 return unit_active_state(unit_follow_merge(u));
394
395 /* After a reload it might happen that a unit is not correctly
396 * loaded but still has a process around. That's why we won't
fdf20a31 397 * shortcut failed loading to UNIT_INACTIVE_FAILED. */
87f0e418
LP
398
399 return UNIT_VTABLE(u)->active_state(u);
400}
401
10a94420
LP
402const char* unit_sub_state_to_string(Unit *u) {
403 assert(u);
404
405 return UNIT_VTABLE(u)->sub_state_to_string(u);
406}
407
23a177ef
LP
408static void complete_move(Set **s, Set **other) {
409 assert(s);
410 assert(other);
87f0e418 411
23a177ef
LP
412 if (!*other)
413 return;
87f0e418
LP
414
415 if (*s)
23a177ef
LP
416 set_move(*s, *other);
417 else {
418 *s = *other;
419 *other = NULL;
420 }
421}
87f0e418 422
23a177ef
LP
423static void merge_names(Unit *u, Unit *other) {
424 char *t;
425 Iterator i;
87f0e418 426
23a177ef
LP
427 assert(u);
428 assert(other);
429
430 complete_move(&u->meta.names, &other->meta.names);
431
53ec43c6 432 set_free_free(other->meta.names);
23a177ef
LP
433 other->meta.names = NULL;
434 other->meta.id = NULL;
435
436 SET_FOREACH(t, u->meta.names, i)
437 assert_se(hashmap_replace(u->meta.manager->units, t, u) == 0);
87f0e418
LP
438}
439
23a177ef
LP
440static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
441 Iterator i;
442 Unit *back;
87f0e418 443 int r;
23a177ef
LP
444
445 assert(u);
446 assert(other);
447 assert(d < _UNIT_DEPENDENCY_MAX);
448
83a95334 449 /* Fix backwards pointers */
23a177ef
LP
450 SET_FOREACH(back, other->meta.dependencies[d], i) {
451 UnitDependency k;
452
453 for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++)
454 if ((r = set_remove_and_put(back->meta.dependencies[k], other, u)) < 0) {
455
456 if (r == -EEXIST)
457 set_remove(back->meta.dependencies[k], other);
458 else
459 assert(r == -ENOENT);
460 }
461 }
462
463 complete_move(&u->meta.dependencies[d], &other->meta.dependencies[d]);
464
465 set_free(other->meta.dependencies[d]);
466 other->meta.dependencies[d] = NULL;
467}
468
469int unit_merge(Unit *u, Unit *other) {
87f0e418
LP
470 UnitDependency d;
471
472 assert(u);
473 assert(other);
474 assert(u->meta.manager == other->meta.manager);
9e2f7c11 475 assert(u->meta.type != _UNIT_TYPE_INVALID);
87f0e418 476
cc916967
LP
477 other = unit_follow_merge(other);
478
23a177ef
LP
479 if (other == u)
480 return 0;
481
9e2f7c11
LP
482 if (u->meta.type != other->meta.type)
483 return -EINVAL;
484
276c3e78 485 if (!u->meta.instance != !other->meta.instance)
87f0e418
LP
486 return -EINVAL;
487
cc916967 488 if (other->meta.load_state != UNIT_STUB &&
fdf20a31 489 other->meta.load_state != UNIT_ERROR)
23a177ef 490 return -EEXIST;
87f0e418 491
819e213f
LP
492 if (other->meta.job)
493 return -EEXIST;
494
fdf20a31 495 if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other)))
819e213f
LP
496 return -EEXIST;
497
87f0e418 498 /* Merge names */
23a177ef 499 merge_names(u, other);
87f0e418
LP
500
501 /* Merge dependencies */
502 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
23a177ef 503 merge_dependencies(u, other, d);
87f0e418 504
23a177ef
LP
505 other->meta.load_state = UNIT_MERGED;
506 other->meta.merged_into = u;
507
3616a49c
LP
508 /* If there is still some data attached to the other node, we
509 * don't need it anymore, and can free it. */
510 if (other->meta.load_state != UNIT_STUB)
511 if (UNIT_VTABLE(other)->done)
512 UNIT_VTABLE(other)->done(other);
513
514 unit_add_to_dbus_queue(u);
23a177ef
LP
515 unit_add_to_cleanup_queue(other);
516
517 return 0;
518}
519
520int unit_merge_by_name(Unit *u, const char *name) {
521 Unit *other;
9e2f7c11
LP
522 int r;
523 char *s = NULL;
23a177ef
LP
524
525 assert(u);
526 assert(name);
527
9e2f7c11
LP
528 if (unit_name_is_template(name)) {
529 if (!u->meta.instance)
530 return -EINVAL;
531
532 if (!(s = unit_name_replace_instance(name, u->meta.instance)))
533 return -ENOMEM;
534
535 name = s;
536 }
537
23a177ef 538 if (!(other = manager_get_unit(u->meta.manager, name)))
9e2f7c11
LP
539 r = unit_add_name(u, name);
540 else
541 r = unit_merge(u, other);
23a177ef 542
9e2f7c11
LP
543 free(s);
544 return r;
23a177ef
LP
545}
546
547Unit* unit_follow_merge(Unit *u) {
548 assert(u);
549
550 while (u->meta.load_state == UNIT_MERGED)
551 assert_se(u = u->meta.merged_into);
552
553 return u;
554}
555
556int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
557 int r;
558
559 assert(u);
560 assert(c);
561
ecc6e2b8
LP
562 if (c->std_output != EXEC_OUTPUT_KMSG &&
563 c->std_output != EXEC_OUTPUT_SYSLOG &&
706343f4 564 c->std_output != EXEC_OUTPUT_JOURNAL &&
28dbc1e8
LP
565 c->std_output != EXEC_OUTPUT_KMSG_AND_CONSOLE &&
566 c->std_output != EXEC_OUTPUT_SYSLOG_AND_CONSOLE &&
706343f4 567 c->std_output != EXEC_OUTPUT_JOURNAL_AND_CONSOLE &&
ecc6e2b8 568 c->std_error != EXEC_OUTPUT_KMSG &&
085c98af 569 c->std_error != EXEC_OUTPUT_SYSLOG &&
706343f4 570 c->std_error != EXEC_OUTPUT_JOURNAL &&
085c98af 571 c->std_error != EXEC_OUTPUT_KMSG_AND_CONSOLE &&
706343f4 572 c->std_error != EXEC_OUTPUT_JOURNAL_AND_CONSOLE &&
28dbc1e8 573 c->std_error != EXEC_OUTPUT_SYSLOG_AND_CONSOLE)
23a177ef
LP
574 return 0;
575
576 /* If syslog or kernel logging is requested, make sure our own
577 * logging daemon is run first. */
578
a3d4e06d 579 if (u->meta.manager->running_as == MANAGER_SYSTEM)
54fe0cdb 580 if ((r = unit_add_two_dependencies_by_name(u, UNIT_REQUIRES, UNIT_AFTER, SPECIAL_JOURNALD_SOCKET, NULL, true)) < 0)
23a177ef
LP
581 return r;
582
87f0e418
LP
583 return 0;
584}
585
87f0e418
LP
586const char *unit_description(Unit *u) {
587 assert(u);
588
589 if (u->meta.description)
590 return u->meta.description;
591
9f151f29 592 return strna(u->meta.id);
87f0e418
LP
593}
594
595void unit_dump(Unit *u, FILE *f, const char *prefix) {
87f0e418
LP
596 char *t;
597 UnitDependency d;
598 Iterator i;
47be870b
LP
599 char *p2;
600 const char *prefix2;
173e3821
LP
601 char
602 timestamp1[FORMAT_TIMESTAMP_MAX],
603 timestamp2[FORMAT_TIMESTAMP_MAX],
604 timestamp3[FORMAT_TIMESTAMP_MAX],
faf919f1
LP
605 timestamp4[FORMAT_TIMESTAMP_MAX],
606 timespan[FORMAT_TIMESPAN_MAX];
a7f241db 607 Unit *following;
87f0e418
LP
608
609 assert(u);
9e2f7c11 610 assert(u->meta.type >= 0);
87f0e418
LP
611
612 if (!prefix)
613 prefix = "";
47be870b
LP
614 p2 = strappend(prefix, "\t");
615 prefix2 = p2 ? p2 : prefix;
87f0e418
LP
616
617 fprintf(f,
40d50879 618 "%s-> Unit %s:\n"
87f0e418 619 "%s\tDescription: %s\n"
9e2f7c11 620 "%s\tInstance: %s\n"
87f0e418 621 "%s\tUnit Load State: %s\n"
2fad8195 622 "%s\tUnit Active State: %s\n"
173e3821 623 "%s\tInactive Exit Timestamp: %s\n"
2fad8195 624 "%s\tActive Enter Timestamp: %s\n"
701cc384 625 "%s\tActive Exit Timestamp: %s\n"
173e3821 626 "%s\tInactive Enter Timestamp: %s\n"
45fb0699
LP
627 "%s\tGC Check Good: %s\n"
628 "%s\tNeed Daemon Reload: %s\n",
9e2f7c11 629 prefix, u->meta.id,
87f0e418 630 prefix, unit_description(u),
9e2f7c11 631 prefix, strna(u->meta.instance),
94f04347 632 prefix, unit_load_state_to_string(u->meta.load_state),
2fad8195 633 prefix, unit_active_state_to_string(unit_active_state(u)),
871d7de4
LP
634 prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.inactive_exit_timestamp.realtime)),
635 prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->meta.active_enter_timestamp.realtime)),
636 prefix, strna(format_timestamp(timestamp3, sizeof(timestamp3), u->meta.active_exit_timestamp.realtime)),
637 prefix, strna(format_timestamp(timestamp4, sizeof(timestamp4), u->meta.inactive_enter_timestamp.realtime)),
45fb0699
LP
638 prefix, yes_no(unit_check_gc(u)),
639 prefix, yes_no(unit_need_daemon_reload(u)));
0301abf4 640
87f0e418
LP
641 SET_FOREACH(t, u->meta.names, i)
642 fprintf(f, "%s\tName: %s\n", prefix, t);
643
a7f241db
LP
644 if ((following = unit_following(u)))
645 fprintf(f, "%s\tFollowing: %s\n", prefix, following->meta.id);
8fe914ec 646
23a177ef
LP
647 if (u->meta.fragment_path)
648 fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);
649
faf919f1
LP
650 if (u->meta.job_timeout > 0)
651 fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->meta.job_timeout));
652
52661efd
LP
653 condition_dump_list(u->meta.conditions, f, prefix);
654
2791a8f8
LP
655 if (dual_timestamp_is_set(&u->meta.condition_timestamp))
656 fprintf(f,
657 "%s\tCondition Timestamp: %s\n"
658 "%s\tCondition Result: %s\n",
659 prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.condition_timestamp.realtime)),
660 prefix, yes_no(u->meta.condition_result));
661
87f0e418
LP
662 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
663 Unit *other;
664
87f0e418 665 SET_FOREACH(other, u->meta.dependencies[d], i)
9e2f7c11 666 fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), other->meta.id);
87f0e418
LP
667 }
668
23a177ef 669 if (u->meta.load_state == UNIT_LOADED) {
ab1f0633
LP
670 CGroupBonding *b;
671 CGroupAttribute *a;
672
b0650475 673 fprintf(f,
a40eb732 674 "%s\tStopWhenUnneeded: %s\n"
b5e9dba8
LP
675 "%s\tRefuseManualStart: %s\n"
676 "%s\tRefuseManualStop: %s\n"
222ae6a8 677 "%s\tDefaultDependencies: %s\n"
c8f4d764 678 "%s\tOnFailureIsolate: %s\n"
7a6000a6
LP
679 "%s\tIgnoreOnIsolate: %s\n"
680 "%s\tIgnoreOnSnapshot: %s\n",
a40eb732 681 prefix, yes_no(u->meta.stop_when_unneeded),
b5e9dba8
LP
682 prefix, yes_no(u->meta.refuse_manual_start),
683 prefix, yes_no(u->meta.refuse_manual_stop),
222ae6a8 684 prefix, yes_no(u->meta.default_dependencies),
c8f4d764 685 prefix, yes_no(u->meta.on_failure_isolate),
7a6000a6
LP
686 prefix, yes_no(u->meta.ignore_on_isolate),
687 prefix, yes_no(u->meta.ignore_on_snapshot));
b0650475 688
23a177ef
LP
689 LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
690 fprintf(f, "%s\tControlGroup: %s:%s\n",
691 prefix, b->controller, b->path);
8e274523 692
d8bbda91
LP
693 LIST_FOREACH(by_unit, a, u->meta.cgroup_attributes) {
694 char *v = NULL;
695
696 if (a->map_callback)
697 a->map_callback(a->controller, a->name, a->value, &v);
698
ab1f0633 699 fprintf(f, "%s\tControlGroupAttribute: %s %s \"%s\"\n",
d8bbda91
LP
700 prefix, a->controller, a->name, v ? v : a->value);
701
702 free(v);
703 }
ab1f0633 704
23a177ef
LP
705 if (UNIT_VTABLE(u)->dump)
706 UNIT_VTABLE(u)->dump(u, f, prefix2);
b0650475
LP
707
708 } else if (u->meta.load_state == UNIT_MERGED)
709 fprintf(f,
710 "%s\tMerged into: %s\n",
9e2f7c11 711 prefix, u->meta.merged_into->meta.id);
fdf20a31 712 else if (u->meta.load_state == UNIT_ERROR)
8821a00f
LP
713 fprintf(f, "%s\tLoad Error Code: %s\n", prefix, strerror(-u->meta.load_error));
714
87f0e418
LP
715
716 if (u->meta.job)
717 job_dump(u->meta.job, f, prefix2);
718
47be870b 719 free(p2);
87f0e418
LP
720}
721
722/* Common implementation for multiple backends */
e537352b 723int unit_load_fragment_and_dropin(Unit *u) {
23a177ef
LP
724 int r;
725
726 assert(u);
23a177ef
LP
727
728 /* Load a .service file */
e537352b 729 if ((r = unit_load_fragment(u)) < 0)
23a177ef
LP
730 return r;
731
e537352b 732 if (u->meta.load_state == UNIT_STUB)
23a177ef
LP
733 return -ENOENT;
734
735 /* Load drop-in directory data */
736 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
737 return r;
738
739 return 0;
740}
741
742/* Common implementation for multiple backends */
e537352b 743int unit_load_fragment_and_dropin_optional(Unit *u) {
23a177ef 744 int r;
87f0e418
LP
745
746 assert(u);
747
23a177ef
LP
748 /* Same as unit_load_fragment_and_dropin(), but whether
749 * something can be loaded or not doesn't matter. */
750
751 /* Load a .service file */
e537352b 752 if ((r = unit_load_fragment(u)) < 0)
87f0e418
LP
753 return r;
754
e537352b
LP
755 if (u->meta.load_state == UNIT_STUB)
756 u->meta.load_state = UNIT_LOADED;
d46de8a1 757
87f0e418 758 /* Load drop-in directory data */
23a177ef 759 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
87f0e418
LP
760 return r;
761
23a177ef 762 return 0;
87f0e418
LP
763}
764
bba34eed 765int unit_add_default_target_dependency(Unit *u, Unit *target) {
98bc2000
LP
766 assert(u);
767 assert(target);
768
769 if (target->meta.type != UNIT_TARGET)
770 return 0;
771
35b8ca3a 772 /* Only add the dependency if both units are loaded, so that
bba34eed
LP
773 * that loop check below is reliable */
774 if (u->meta.load_state != UNIT_LOADED ||
775 target->meta.load_state != UNIT_LOADED)
776 return 0;
777
21256a2b
LP
778 /* If either side wants no automatic dependencies, then let's
779 * skip this */
780 if (!u->meta.default_dependencies ||
a65cb51f 781 !target->meta.default_dependencies)
21256a2b
LP
782 return 0;
783
98bc2000
LP
784 /* Don't create loops */
785 if (set_get(target->meta.dependencies[UNIT_BEFORE], u))
786 return 0;
787
788 return unit_add_dependency(target, UNIT_AFTER, u, true);
789}
790
791static int unit_add_default_dependencies(Unit *u) {
21256a2b
LP
792 static const UnitDependency deps[] = {
793 UNIT_REQUIRED_BY,
794 UNIT_REQUIRED_BY_OVERRIDABLE,
795 UNIT_WANTED_BY,
796 UNIT_BOUND_BY
797 };
798
bba34eed 799 Unit *target;
98bc2000
LP
800 Iterator i;
801 int r;
21256a2b 802 unsigned k;
98bc2000
LP
803
804 assert(u);
805
21256a2b
LP
806 for (k = 0; k < ELEMENTSOF(deps); k++)
807 SET_FOREACH(target, u->meta.dependencies[deps[k]], i)
808 if ((r = unit_add_default_target_dependency(u, target)) < 0)
809 return r;
b81884e7 810
98bc2000
LP
811 return 0;
812}
813
87f0e418
LP
814int unit_load(Unit *u) {
815 int r;
816
817 assert(u);
818
819 if (u->meta.in_load_queue) {
820 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
821 u->meta.in_load_queue = false;
822 }
823
e537352b
LP
824 if (u->meta.type == _UNIT_TYPE_INVALID)
825 return -EINVAL;
826
87f0e418
LP
827 if (u->meta.load_state != UNIT_STUB)
828 return 0;
829
e537352b
LP
830 if (UNIT_VTABLE(u)->load)
831 if ((r = UNIT_VTABLE(u)->load(u)) < 0)
87f0e418 832 goto fail;
23a177ef 833
e537352b 834 if (u->meta.load_state == UNIT_STUB) {
23a177ef
LP
835 r = -ENOENT;
836 goto fail;
837 }
838
98bc2000
LP
839 if (u->meta.load_state == UNIT_LOADED &&
840 u->meta.default_dependencies)
841 if ((r = unit_add_default_dependencies(u)) < 0)
842 goto fail;
843
f68319bb
LP
844 if (u->meta.on_failure_isolate &&
845 set_size(u->meta.dependencies[UNIT_ON_FAILURE]) > 1) {
846
847 log_error("More than one OnFailure= dependencies specified for %s but OnFailureIsolate= enabled. Refusing.",
848 u->meta.id);
849
850 r = -EINVAL;
851 goto fail;
852 }
853
23a177ef
LP
854 assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);
855
856 unit_add_to_dbus_queue(unit_follow_merge(u));
701cc384 857 unit_add_to_gc_queue(u);
87f0e418 858
87f0e418
LP
859 return 0;
860
861fail:
fdf20a31 862 u->meta.load_state = UNIT_ERROR;
8821a00f 863 u->meta.load_error = r;
c1e1601e 864 unit_add_to_dbus_queue(u);
9a46fc3b 865 unit_add_to_gc_queue(u);
23a177ef 866
8821a00f 867 log_debug("Failed to load configuration for %s: %s", u->meta.id, strerror(-r));
23a177ef 868
87f0e418
LP
869 return r;
870}
871
90bbc946
LP
872bool unit_condition_test(Unit *u) {
873 assert(u);
874
875 dual_timestamp_get(&u->meta.condition_timestamp);
876 u->meta.condition_result = condition_test_list(u->meta.conditions);
877
878 return u->meta.condition_result;
879}
880
87f0e418 881/* Errors:
d5159713
LP
882 * -EBADR: This unit type does not support starting.
883 * -EALREADY: Unit is already started.
884 * -EAGAIN: An operation is already in progress. Retry later.
885 * -ECANCELED: Too many requests for now.
87f0e418
LP
886 */
887int unit_start(Unit *u) {
888 UnitActiveState state;
92ab323c 889 Unit *following;
87f0e418
LP
890
891 assert(u);
892
6124958c
LP
893 if (u->meta.load_state != UNIT_LOADED)
894 return -EINVAL;
895
a82e5507
LP
896 /* If this is already started, then this will succeed. Note
897 * that this will even succeed if this unit is not startable
898 * by the user. This is relied on to detect when we need to
899 * wait for units and when waiting is finished. */
87f0e418
LP
900 state = unit_active_state(u);
901 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
902 return -EALREADY;
903
a82e5507
LP
904 /* If the conditions failed, don't do anything at all. If we
905 * already are activating this call might still be useful to
906 * speed up activation in case there is some hold-off time,
907 * but we don't want to recheck the condition in that case. */
908 if (state != UNIT_ACTIVATING &&
909 !unit_condition_test(u)) {
52661efd
LP
910 log_debug("Starting of %s requested but condition failed. Ignoring.", u->meta.id);
911 return -EALREADY;
912 }
913
92ab323c
LP
914 /* Forward to the main object, if we aren't it. */
915 if ((following = unit_following(u))) {
916 log_debug("Redirecting start request from %s to %s.", u->meta.id, following->meta.id);
917 return unit_start(following);
918 }
919
920 /* If it is stopped, but we cannot start it, then fail */
921 if (!UNIT_VTABLE(u)->start)
922 return -EBADR;
923
87f0e418
LP
924 /* We don't suppress calls to ->start() here when we are
925 * already starting, to allow this request to be used as a
926 * "hurry up" call, for example when the unit is in some "auto
927 * restart" state where it waits for a holdoff timer to elapse
928 * before it will start again. */
929
c1e1601e 930 unit_add_to_dbus_queue(u);
9e58ff9c 931
5831e9b7 932 unit_status_printf(u, NULL, "Starting %s...", unit_description(u));
87f0e418
LP
933 return UNIT_VTABLE(u)->start(u);
934}
935
936bool unit_can_start(Unit *u) {
937 assert(u);
938
939 return !!UNIT_VTABLE(u)->start;
940}
941
2528a7a6
LP
942bool unit_can_isolate(Unit *u) {
943 assert(u);
944
945 return unit_can_start(u) &&
946 u->meta.allow_isolate;
947}
948
87f0e418
LP
949/* Errors:
950 * -EBADR: This unit type does not support stopping.
951 * -EALREADY: Unit is already stopped.
952 * -EAGAIN: An operation is already in progress. Retry later.
953 */
954int unit_stop(Unit *u) {
955 UnitActiveState state;
92ab323c 956 Unit *following;
87f0e418
LP
957
958 assert(u);
959
87f0e418 960 state = unit_active_state(u);
fdf20a31 961 if (UNIT_IS_INACTIVE_OR_FAILED(state))
87f0e418
LP
962 return -EALREADY;
963
92ab323c
LP
964 if ((following = unit_following(u))) {
965 log_debug("Redirecting stop request from %s to %s.", u->meta.id, following->meta.id);
966 return unit_stop(following);
967 }
968
7898b0cf
LP
969 if (!UNIT_VTABLE(u)->stop)
970 return -EBADR;
971
c1e1601e 972 unit_add_to_dbus_queue(u);
9e58ff9c 973
5831e9b7 974 unit_status_printf(u, NULL, "Stopping %s...", unit_description(u));
87f0e418
LP
975 return UNIT_VTABLE(u)->stop(u);
976}
977
978/* Errors:
979 * -EBADR: This unit type does not support reloading.
980 * -ENOEXEC: Unit is not started.
981 * -EAGAIN: An operation is already in progress. Retry later.
982 */
983int unit_reload(Unit *u) {
984 UnitActiveState state;
92ab323c 985 Unit *following;
87f0e418
LP
986
987 assert(u);
988
6124958c
LP
989 if (u->meta.load_state != UNIT_LOADED)
990 return -EINVAL;
991
87f0e418
LP
992 if (!unit_can_reload(u))
993 return -EBADR;
994
995 state = unit_active_state(u);
e364ad06 996 if (state == UNIT_RELOADING)
87f0e418
LP
997 return -EALREADY;
998
e364ad06 999 if (state != UNIT_ACTIVE)
87f0e418
LP
1000 return -ENOEXEC;
1001
92ab323c
LP
1002 if ((following = unit_following(u))) {
1003 log_debug("Redirecting reload request from %s to %s.", u->meta.id, following->meta.id);
1004 return unit_reload(following);
1005 }
1006
c1e1601e 1007 unit_add_to_dbus_queue(u);
87f0e418
LP
1008 return UNIT_VTABLE(u)->reload(u);
1009}
1010
1011bool unit_can_reload(Unit *u) {
1012 assert(u);
1013
1014 if (!UNIT_VTABLE(u)->reload)
1015 return false;
1016
1017 if (!UNIT_VTABLE(u)->can_reload)
1018 return true;
1019
1020 return UNIT_VTABLE(u)->can_reload(u);
1021}
1022
b4a16b7b 1023static void unit_check_unneeded(Unit *u) {
f3bff0eb
LP
1024 Iterator i;
1025 Unit *other;
1026
1027 assert(u);
1028
1029 /* If this service shall be shut down when unneeded then do
1030 * so. */
1031
1032 if (!u->meta.stop_when_unneeded)
1033 return;
1034
1035 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
1036 return;
1037
1038 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
f60c2665 1039 if (unit_pending_active(other))
f3bff0eb
LP
1040 return;
1041
9e2f7c11 1042 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
f60c2665 1043 if (unit_pending_active(other))
f3bff0eb
LP
1044 return;
1045
1046 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
f60c2665 1047 if (unit_pending_active(other))
f3bff0eb
LP
1048 return;
1049
b81884e7 1050 SET_FOREACH(other, u->meta.dependencies[UNIT_BOUND_BY], i)
f60c2665 1051 if (unit_pending_active(other))
b81884e7
LP
1052 return;
1053
54165a39 1054 log_info("Service %s is not needed anymore. Stopping.", u->meta.id);
f3bff0eb
LP
1055
1056 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
398ef8ba 1057 manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL, NULL);
f3bff0eb
LP
1058}
1059
87f0e418
LP
1060static void retroactively_start_dependencies(Unit *u) {
1061 Iterator i;
1062 Unit *other;
1063
1064 assert(u);
1065 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
1066
1067 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
b81884e7
LP
1068 if (!set_get(u->meta.dependencies[UNIT_AFTER], other) &&
1069 !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
1070 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
1071
1072 SET_FOREACH(other, u->meta.dependencies[UNIT_BIND_TO], i)
1073 if (!set_get(u->meta.dependencies[UNIT_AFTER], other) &&
1074 !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
398ef8ba 1075 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
87f0e418 1076
9e2f7c11 1077 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
b81884e7
LP
1078 if (!set_get(u->meta.dependencies[UNIT_AFTER], other) &&
1079 !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
398ef8ba 1080 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL);
87f0e418
LP
1081
1082 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
b81884e7
LP
1083 if (!set_get(u->meta.dependencies[UNIT_AFTER], other) &&
1084 !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
398ef8ba 1085 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
87f0e418
LP
1086
1087 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
b81884e7
LP
1088 if (!set_get(u->meta.dependencies[UNIT_AFTER], other) &&
1089 !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
398ef8ba 1090 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL);
87f0e418
LP
1091
1092 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
b81884e7 1093 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
398ef8ba 1094 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
69dd2852
LP
1095
1096 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTED_BY], i)
b81884e7 1097 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
69dd2852 1098 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
87f0e418
LP
1099}
1100
1101static void retroactively_stop_dependencies(Unit *u) {
1102 Iterator i;
1103 Unit *other;
1104
1105 assert(u);
1106 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
1107
b81884e7
LP
1108 /* Pull down units which are bound to us recursively if enabled */
1109 SET_FOREACH(other, u->meta.dependencies[UNIT_BOUND_BY], i)
1110 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
f14e15f8 1111 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
cd0504d0
MS
1112}
1113
1114static void check_unneeded_dependencies(Unit *u) {
1115 Iterator i;
1116 Unit *other;
1117
1118 assert(u);
1119 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
f3bff0eb
LP
1120
1121 /* Garbage collect services that might not be needed anymore, if enabled */
1122 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
87f0e418 1123 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
b4a16b7b 1124 unit_check_unneeded(other);
9e2f7c11 1125 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
f3bff0eb 1126 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
b4a16b7b 1127 unit_check_unneeded(other);
f3bff0eb
LP
1128 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
1129 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
b4a16b7b 1130 unit_check_unneeded(other);
f3bff0eb
LP
1131 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
1132 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
b4a16b7b 1133 unit_check_unneeded(other);
9e2f7c11 1134 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
f3bff0eb 1135 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
b4a16b7b 1136 unit_check_unneeded(other);
b81884e7
LP
1137 SET_FOREACH(other, u->meta.dependencies[UNIT_BIND_TO], i)
1138 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
1139 unit_check_unneeded(other);
87f0e418
LP
1140}
1141
c0daa706
LP
1142void unit_trigger_on_failure(Unit *u) {
1143 Unit *other;
1144 Iterator i;
1145
1146 assert(u);
1147
222ae6a8
LP
1148 if (set_size(u->meta.dependencies[UNIT_ON_FAILURE]) <= 0)
1149 return;
1150
1151 log_info("Triggering OnFailure= dependencies of %s.", u->meta.id);
1152
1153 SET_FOREACH(other, u->meta.dependencies[UNIT_ON_FAILURE], i) {
1154 int r;
1155
1156 if ((r = manager_add_job(u->meta.manager, JOB_START, other, u->meta.on_failure_isolate ? JOB_ISOLATE : JOB_REPLACE, true, NULL, NULL)) < 0)
1157 log_error("Failed to enqueue OnFailure= job: %s", strerror(-r));
1158 }
c0daa706
LP
1159}
1160
e2f3b44c 1161void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_success) {
7e6e7b06 1162 bool unexpected;
a096ed36 1163
87f0e418
LP
1164 assert(u);
1165 assert(os < _UNIT_ACTIVE_STATE_MAX);
1166 assert(ns < _UNIT_ACTIVE_STATE_MAX);
87f0e418 1167
8e471ccd
LP
1168 /* Note that this is called for all low-level state changes,
1169 * even if they might map to the same high-level
1170 * UnitActiveState! That means that ns == os is OK an expected
1171 * behaviour here. For example: if a mount point is remounted
cd6d0a45 1172 * this function will be called too! */
87f0e418 1173
a7556052 1174 if (u->meta.manager->n_reloading <= 0) {
bdbf9951 1175 dual_timestamp ts;
173e3821 1176
bdbf9951 1177 dual_timestamp_get(&ts);
173e3821 1178
bdbf9951
LP
1179 if (UNIT_IS_INACTIVE_OR_FAILED(os) && !UNIT_IS_INACTIVE_OR_FAILED(ns))
1180 u->meta.inactive_exit_timestamp = ts;
1181 else if (!UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_INACTIVE_OR_FAILED(ns))
1182 u->meta.inactive_enter_timestamp = ts;
1183
1184 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
1185 u->meta.active_enter_timestamp = ts;
1186 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
1187 u->meta.active_exit_timestamp = ts;
1188
1189 timer_unit_notify(u, ns);
1190 path_unit_notify(u, ns);
1191 }
87f0e418 1192
fdf20a31 1193 if (UNIT_IS_INACTIVE_OR_FAILED(ns))
fb385181
LP
1194 cgroup_bonding_trim_list(u->meta.cgroup_bondings, true);
1195
87f0e418 1196 if (u->meta.job) {
7e6e7b06 1197 unexpected = false;
87f0e418
LP
1198
1199 if (u->meta.job->state == JOB_WAITING)
1200
1201 /* So we reached a different state for this
1202 * job. Let's see if we can run it now if it
1203 * failed previously due to EAGAIN. */
c1e1601e 1204 job_add_to_run_queue(u->meta.job);
87f0e418 1205
b410e6b9 1206 /* Let's check whether this state change constitutes a
35b8ca3a 1207 * finished job, or maybe contradicts a running job and
b410e6b9 1208 * hence needs to invalidate jobs. */
87f0e418 1209
b410e6b9 1210 switch (u->meta.job->type) {
87f0e418 1211
b410e6b9
LP
1212 case JOB_START:
1213 case JOB_VERIFY_ACTIVE:
87f0e418 1214
b410e6b9 1215 if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
5d44db4a 1216 job_finish_and_invalidate(u->meta.job, JOB_DONE);
b410e6b9
LP
1217 else if (u->meta.job->state == JOB_RUNNING && ns != UNIT_ACTIVATING) {
1218 unexpected = true;
41b02ec7 1219
fdf20a31 1220 if (UNIT_IS_INACTIVE_OR_FAILED(ns))
5d44db4a 1221 job_finish_and_invalidate(u->meta.job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE);
b410e6b9 1222 }
87f0e418 1223
b410e6b9 1224 break;
87f0e418 1225
b410e6b9
LP
1226 case JOB_RELOAD:
1227 case JOB_RELOAD_OR_START:
87f0e418 1228
b410e6b9 1229 if (u->meta.job->state == JOB_RUNNING) {
a096ed36 1230 if (ns == UNIT_ACTIVE)
5d44db4a 1231 job_finish_and_invalidate(u->meta.job, reload_success ? JOB_DONE : JOB_FAILED);
032ff4af 1232 else if (ns != UNIT_ACTIVATING && ns != UNIT_RELOADING) {
a096ed36 1233 unexpected = true;
41b02ec7 1234
fdf20a31 1235 if (UNIT_IS_INACTIVE_OR_FAILED(ns))
5d44db4a 1236 job_finish_and_invalidate(u->meta.job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE);
a096ed36 1237 }
b410e6b9 1238 }
87f0e418 1239
b410e6b9 1240 break;
87f0e418 1241
b410e6b9
LP
1242 case JOB_STOP:
1243 case JOB_RESTART:
1244 case JOB_TRY_RESTART:
87f0e418 1245
fdf20a31 1246 if (UNIT_IS_INACTIVE_OR_FAILED(ns))
5d44db4a 1247 job_finish_and_invalidate(u->meta.job, JOB_DONE);
b410e6b9
LP
1248 else if (u->meta.job->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) {
1249 unexpected = true;
5d44db4a 1250 job_finish_and_invalidate(u->meta.job, JOB_FAILED);
b410e6b9 1251 }
87f0e418 1252
b410e6b9 1253 break;
87f0e418 1254
b410e6b9
LP
1255 default:
1256 assert_not_reached("Job type unknown");
87f0e418 1257 }
87f0e418 1258
7e6e7b06
LP
1259 } else
1260 unexpected = true;
1261
a7556052 1262 if (u->meta.manager->n_reloading <= 0) {
f3bff0eb 1263
bdbf9951
LP
1264 /* If this state change happened without being
1265 * requested by a job, then let's retroactively start
1266 * or stop dependencies. We skip that step when
1267 * deserializing, since we don't want to create any
1268 * additional jobs just because something is already
1269 * activated. */
1270
1271 if (unexpected) {
1272 if (UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
1273 retroactively_start_dependencies(u);
1274 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
1275 retroactively_stop_dependencies(u);
1276 }
5de9682c 1277
cd0504d0
MS
1278 /* stop unneeded units regardless if going down was expected or not */
1279 if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
1280 check_unneeded_dependencies(u);
1281
bdbf9951
LP
1282 if (ns != os && ns == UNIT_FAILED) {
1283 log_notice("Unit %s entered failed state.", u->meta.id);
1284 unit_trigger_on_failure(u);
cd6d0a45 1285 }
3b2775c5 1286 }
e537352b 1287
3b2775c5
LP
1288 /* Some names are special */
1289 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
1290
1291 if (unit_has_name(u, SPECIAL_DBUS_SERVICE))
1292 /* The bus just might have become available,
1293 * hence try to connect to it, if we aren't
1294 * yet connected. */
1295 bus_init(u->meta.manager, true);
1296
1297 if (u->meta.type == UNIT_SERVICE &&
1298 !UNIT_IS_ACTIVE_OR_RELOADING(os) &&
a7556052 1299 u->meta.manager->n_reloading <= 0) {
3b2775c5
LP
1300 /* Write audit record if we have just finished starting up */
1301 manager_send_unit_audit(u->meta.manager, u, AUDIT_SERVICE_START, true);
1302 u->meta.in_audit = true;
1303 }
e983b760 1304
3b2775c5
LP
1305 if (!UNIT_IS_ACTIVE_OR_RELOADING(os))
1306 manager_send_unit_plymouth(u->meta.manager, u);
bdbf9951 1307
3b2775c5 1308 } else {
bdbf9951 1309
3b2775c5
LP
1310 /* We don't care about D-Bus here, since we'll get an
1311 * asynchronous notification for it anyway. */
cd6d0a45 1312
3b2775c5
LP
1313 if (u->meta.type == UNIT_SERVICE &&
1314 UNIT_IS_INACTIVE_OR_FAILED(ns) &&
1315 !UNIT_IS_INACTIVE_OR_FAILED(os) &&
a7556052 1316 u->meta.manager->n_reloading <= 0) {
4927fcae 1317
3b2775c5
LP
1318 /* Hmm, if there was no start record written
1319 * write it now, so that we always have a nice
1320 * pair */
1321 if (!u->meta.in_audit) {
1322 manager_send_unit_audit(u->meta.manager, u, AUDIT_SERVICE_START, ns == UNIT_INACTIVE);
cd6d0a45 1323
3b2775c5
LP
1324 if (ns == UNIT_INACTIVE)
1325 manager_send_unit_audit(u->meta.manager, u, AUDIT_SERVICE_STOP, true);
1326 } else
1327 /* Write audit record if we have just finished shutting down */
1328 manager_send_unit_audit(u->meta.manager, u, AUDIT_SERVICE_STOP, ns == UNIT_INACTIVE);
bdbf9951 1329
3b2775c5 1330 u->meta.in_audit = false;
cd6d0a45 1331 }
f278026d
LP
1332 }
1333
3b2775c5
LP
1334 manager_recheck_syslog(u->meta.manager);
1335
f3bff0eb
LP
1336 /* Maybe we finished startup and are now ready for being
1337 * stopped because unneeded? */
b4a16b7b 1338 unit_check_unneeded(u);
c1e1601e
LP
1339
1340 unit_add_to_dbus_queue(u);
701cc384 1341 unit_add_to_gc_queue(u);
87f0e418
LP
1342}
1343
acbb0225 1344int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
87f0e418
LP
1345 struct epoll_event ev;
1346
1347 assert(u);
1348 assert(fd >= 0);
acbb0225 1349 assert(w);
ea430986 1350 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
87f0e418
LP
1351
1352 zero(ev);
acbb0225 1353 ev.data.ptr = w;
87f0e418
LP
1354 ev.events = events;
1355
acbb0225
LP
1356 if (epoll_ctl(u->meta.manager->epoll_fd,
1357 w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
1358 fd,
1359 &ev) < 0)
1360 return -errno;
87f0e418 1361
acbb0225
LP
1362 w->fd = fd;
1363 w->type = WATCH_FD;
ea430986 1364 w->data.unit = u;
87f0e418 1365
acbb0225 1366 return 0;
87f0e418
LP
1367}
1368
acbb0225 1369void unit_unwatch_fd(Unit *u, Watch *w) {
87f0e418 1370 assert(u);
acbb0225 1371 assert(w);
87f0e418 1372
acbb0225
LP
1373 if (w->type == WATCH_INVALID)
1374 return;
1375
ea430986
LP
1376 assert(w->type == WATCH_FD);
1377 assert(w->data.unit == u);
acbb0225
LP
1378 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1379
1380 w->fd = -1;
1381 w->type = WATCH_INVALID;
ea430986 1382 w->data.unit = NULL;
87f0e418
LP
1383}
1384
1385int unit_watch_pid(Unit *u, pid_t pid) {
1386 assert(u);
1387 assert(pid >= 1);
1388
05e343b7
LP
1389 /* Watch a specific PID. We only support one unit watching
1390 * each PID for now. */
1391
c6c18be3 1392 return hashmap_put(u->meta.manager->watch_pids, LONG_TO_PTR(pid), u);
87f0e418
LP
1393}
1394
1395void unit_unwatch_pid(Unit *u, pid_t pid) {
1396 assert(u);
1397 assert(pid >= 1);
1398
c6c18be3 1399 hashmap_remove_value(u->meta.manager->watch_pids, LONG_TO_PTR(pid), u);
87f0e418
LP
1400}
1401
acbb0225 1402int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
87f0e418 1403 struct itimerspec its;
acbb0225 1404 int flags, fd;
87f0e418
LP
1405 bool ours;
1406
1407 assert(u);
acbb0225 1408 assert(w);
faf919f1 1409 assert(w->type == WATCH_INVALID || (w->type == WATCH_UNIT_TIMER && w->data.unit == u));
87f0e418
LP
1410
1411 /* This will try to reuse the old timer if there is one */
1412
faf919f1
LP
1413 if (w->type == WATCH_UNIT_TIMER) {
1414 assert(w->data.unit == u);
1415 assert(w->fd >= 0);
1416
87f0e418 1417 ours = false;
acbb0225 1418 fd = w->fd;
faf919f1
LP
1419 } else if (w->type == WATCH_INVALID) {
1420
87f0e418 1421 ours = true;
87f0e418
LP
1422 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
1423 return -errno;
faf919f1
LP
1424 } else
1425 assert_not_reached("Invalid watch type");
87f0e418
LP
1426
1427 zero(its);
1428
1429 if (delay <= 0) {
1430 /* Set absolute time in the past, but not 0, since we
1431 * don't want to disarm the timer */
1432 its.it_value.tv_sec = 0;
1433 its.it_value.tv_nsec = 1;
1434
1435 flags = TFD_TIMER_ABSTIME;
1436 } else {
1437 timespec_store(&its.it_value, delay);
1438 flags = 0;
1439 }
1440
1441 /* This will also flush the elapse counter */
1442 if (timerfd_settime(fd, flags, &its, NULL) < 0)
1443 goto fail;
1444
acbb0225
LP
1445 if (w->type == WATCH_INVALID) {
1446 struct epoll_event ev;
87f0e418 1447
acbb0225
LP
1448 zero(ev);
1449 ev.data.ptr = w;
f94ea366 1450 ev.events = EPOLLIN;
acbb0225
LP
1451
1452 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
1453 goto fail;
1454 }
1455
faf919f1 1456 w->type = WATCH_UNIT_TIMER;
acbb0225 1457 w->fd = fd;
ea430986 1458 w->data.unit = u;
87f0e418 1459
87f0e418
LP
1460 return 0;
1461
1462fail:
1463 if (ours)
ea430986 1464 close_nointr_nofail(fd);
87f0e418
LP
1465
1466 return -errno;
1467}
1468
acbb0225 1469void unit_unwatch_timer(Unit *u, Watch *w) {
87f0e418 1470 assert(u);
acbb0225 1471 assert(w);
87f0e418 1472
acbb0225 1473 if (w->type == WATCH_INVALID)
87f0e418
LP
1474 return;
1475
faf919f1
LP
1476 assert(w->type == WATCH_UNIT_TIMER);
1477 assert(w->data.unit == u);
1478 assert(w->fd >= 0);
acbb0225
LP
1479
1480 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
a16e1123 1481 close_nointr_nofail(w->fd);
acbb0225
LP
1482
1483 w->fd = -1;
1484 w->type = WATCH_INVALID;
ea430986 1485 w->data.unit = NULL;
87f0e418
LP
1486}
1487
1488bool unit_job_is_applicable(Unit *u, JobType j) {
1489 assert(u);
1490 assert(j >= 0 && j < _JOB_TYPE_MAX);
1491
1492 switch (j) {
1493
1494 case JOB_VERIFY_ACTIVE:
1495 case JOB_START:
57339f47 1496 case JOB_STOP:
87f0e418
LP
1497 return true;
1498
87f0e418
LP
1499 case JOB_RESTART:
1500 case JOB_TRY_RESTART:
1501 return unit_can_start(u);
1502
1503 case JOB_RELOAD:
1504 return unit_can_reload(u);
1505
1506 case JOB_RELOAD_OR_START:
1507 return unit_can_reload(u) && unit_can_start(u);
1508
1509 default:
1510 assert_not_reached("Invalid job type");
1511 }
1512}
1513
701cc384 1514int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference) {
87f0e418
LP
1515
1516 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
1517 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
9e2f7c11 1518 [UNIT_REQUIRES_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE,
87f0e418
LP
1519 [UNIT_WANTS] = UNIT_WANTED_BY,
1520 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
9e2f7c11 1521 [UNIT_REQUISITE_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE,
b81884e7 1522 [UNIT_BIND_TO] = UNIT_BOUND_BY,
87f0e418 1523 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
9e2f7c11 1524 [UNIT_REQUIRED_BY_OVERRIDABLE] = _UNIT_DEPENDENCY_INVALID,
87f0e418 1525 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
b81884e7 1526 [UNIT_BOUND_BY] = UNIT_BIND_TO,
69dd2852
LP
1527 [UNIT_CONFLICTS] = UNIT_CONFLICTED_BY,
1528 [UNIT_CONFLICTED_BY] = UNIT_CONFLICTS,
87f0e418 1529 [UNIT_BEFORE] = UNIT_AFTER,
701cc384 1530 [UNIT_AFTER] = UNIT_BEFORE,
5de9682c 1531 [UNIT_ON_FAILURE] = _UNIT_DEPENDENCY_INVALID,
701cc384
LP
1532 [UNIT_REFERENCES] = UNIT_REFERENCED_BY,
1533 [UNIT_REFERENCED_BY] = UNIT_REFERENCES
87f0e418 1534 };
701cc384 1535 int r, q = 0, v = 0, w = 0;
87f0e418
LP
1536
1537 assert(u);
1538 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
87f0e418
LP
1539 assert(other);
1540
9f151f29
LP
1541 u = unit_follow_merge(u);
1542 other = unit_follow_merge(other);
1543
87f0e418
LP
1544 /* We won't allow dependencies on ourselves. We will not
1545 * consider them an error however. */
1546 if (u == other)
1547 return 0;
1548
5de9682c 1549 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
87f0e418
LP
1550 return r;
1551
5de9682c
LP
1552 if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID)
1553 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
1554 return r;
1555
701cc384
LP
1556 if (add_reference)
1557 if ((r = set_ensure_allocated(&u->meta.dependencies[UNIT_REFERENCES], trivial_hash_func, trivial_compare_func)) < 0 ||
1558 (r = set_ensure_allocated(&other->meta.dependencies[UNIT_REFERENCED_BY], trivial_hash_func, trivial_compare_func)) < 0)
1559 return r;
87f0e418 1560
701cc384
LP
1561 if ((q = set_put(u->meta.dependencies[d], other)) < 0)
1562 return q;
87f0e418 1563
5de9682c
LP
1564 if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID)
1565 if ((v = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
1566 r = v;
1567 goto fail;
1568 }
701cc384
LP
1569
1570 if (add_reference) {
1571 if ((w = set_put(u->meta.dependencies[UNIT_REFERENCES], other)) < 0) {
1572 r = w;
1573 goto fail;
1574 }
1575
1576 if ((r = set_put(other->meta.dependencies[UNIT_REFERENCED_BY], u)) < 0)
1577 goto fail;
87f0e418
LP
1578 }
1579
c1e1601e 1580 unit_add_to_dbus_queue(u);
87f0e418 1581 return 0;
701cc384
LP
1582
1583fail:
1584 if (q > 0)
1585 set_remove(u->meta.dependencies[d], other);
1586
1587 if (v > 0)
1588 set_remove(other->meta.dependencies[inverse_table[d]], u);
1589
1590 if (w > 0)
1591 set_remove(u->meta.dependencies[UNIT_REFERENCES], other);
1592
1593 return r;
87f0e418 1594}
0301abf4 1595
2c966c03
LP
1596int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference) {
1597 int r;
1598
1599 assert(u);
1600
1601 if ((r = unit_add_dependency(u, d, other, add_reference)) < 0)
1602 return r;
1603
1604 if ((r = unit_add_dependency(u, e, other, add_reference)) < 0)
1605 return r;
1606
1607 return 0;
1608}
1609
9e2f7c11
LP
1610static const char *resolve_template(Unit *u, const char *name, const char*path, char **p) {
1611 char *s;
1612
1613 assert(u);
1614 assert(name || path);
1615
1616 if (!name)
1617 name = file_name_from_path(path);
1618
1619 if (!unit_name_is_template(name)) {
1620 *p = NULL;
1621 return name;
1622 }
1623
1624 if (u->meta.instance)
1625 s = unit_name_replace_instance(name, u->meta.instance);
1626 else {
1627 char *i;
1628
1629 if (!(i = unit_name_to_prefix(u->meta.id)))
1630 return NULL;
1631
1632 s = unit_name_replace_instance(name, i);
1633 free(i);
1634 }
1635
1636 if (!s)
1637 return NULL;
1638
1639 *p = s;
1640 return s;
1641}
1642
701cc384 1643int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
09b6b09f
LP
1644 Unit *other;
1645 int r;
9e2f7c11 1646 char *s;
09b6b09f 1647
9e2f7c11
LP
1648 assert(u);
1649 assert(name || path);
09b6b09f 1650
9e2f7c11
LP
1651 if (!(name = resolve_template(u, name, path, &s)))
1652 return -ENOMEM;
09b6b09f 1653
398ef8ba 1654 if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
9e2f7c11
LP
1655 goto finish;
1656
701cc384 1657 r = unit_add_dependency(u, d, other, add_reference);
9e2f7c11
LP
1658
1659finish:
1660 free(s);
1661 return r;
09b6b09f
LP
1662}
1663
2c966c03
LP
1664int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
1665 Unit *other;
1666 int r;
1667 char *s;
1668
1669 assert(u);
1670 assert(name || path);
1671
1672 if (!(name = resolve_template(u, name, path, &s)))
1673 return -ENOMEM;
1674
398ef8ba 1675 if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
2c966c03
LP
1676 goto finish;
1677
1678 r = unit_add_two_dependencies(u, d, e, other, add_reference);
1679
1680finish:
1681 free(s);
1682 return r;
1683}
1684
701cc384 1685int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
bd77d0fc
LP
1686 Unit *other;
1687 int r;
9e2f7c11 1688 char *s;
bd77d0fc 1689
9e2f7c11
LP
1690 assert(u);
1691 assert(name || path);
bd77d0fc 1692
9e2f7c11
LP
1693 if (!(name = resolve_template(u, name, path, &s)))
1694 return -ENOMEM;
bd77d0fc 1695
398ef8ba 1696 if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
9e2f7c11
LP
1697 goto finish;
1698
701cc384 1699 r = unit_add_dependency(other, d, u, add_reference);
9e2f7c11
LP
1700
1701finish:
1702 free(s);
1703 return r;
bd77d0fc
LP
1704}
1705
2c966c03
LP
1706int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
1707 Unit *other;
1708 int r;
1709 char *s;
1710
1711 assert(u);
1712 assert(name || path);
1713
1714 if (!(name = resolve_template(u, name, path, &s)))
1715 return -ENOMEM;
1716
398ef8ba 1717 if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
2c966c03
LP
1718 goto finish;
1719
1720 if ((r = unit_add_two_dependencies(other, d, e, u, add_reference)) < 0)
1721 goto finish;
1722
1723finish:
1724 free(s);
1725 return r;
1726}
1727
0301abf4
LP
1728int set_unit_path(const char *p) {
1729 char *cwd, *c;
1730 int r;
1731
1732 /* This is mostly for debug purposes */
1733
1734 if (path_is_absolute(p)) {
1735 if (!(c = strdup(p)))
1736 return -ENOMEM;
1737 } else {
1738 if (!(cwd = get_current_dir_name()))
1739 return -errno;
1740
1741 r = asprintf(&c, "%s/%s", cwd, p);
1742 free(cwd);
1743
1744 if (r < 0)
1745 return -ENOMEM;
1746 }
1747
036643a2 1748 if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
0301abf4
LP
1749 r = -errno;
1750 free(c);
1751 return r;
1752 }
1753
1754 return 0;
1755}
88066b3a 1756
ea430986
LP
1757char *unit_dbus_path(Unit *u) {
1758 char *p, *e;
1759
1760 assert(u);
1761
04ade7d2
LP
1762 if (!u->meta.id)
1763 return NULL;
1764
9e2f7c11 1765 if (!(e = bus_path_escape(u->meta.id)))
ea430986
LP
1766 return NULL;
1767
35d2e7ec 1768 p = strappend("/org/freedesktop/systemd1/unit/", e);
ea430986 1769 free(e);
35d2e7ec 1770
ea430986
LP
1771 return p;
1772}
1773
8e274523 1774int unit_add_cgroup(Unit *u, CGroupBonding *b) {
8e274523
LP
1775 int r;
1776
1777 assert(u);
1778 assert(b);
35d2e7ec 1779
8e274523
LP
1780 assert(b->path);
1781
e025b4c3 1782 if (!b->controller) {
35d2e7ec
LP
1783 if (!(b->controller = strdup(SYSTEMD_CGROUP_CONTROLLER)))
1784 return -ENOMEM;
1785
e025b4c3
LP
1786 b->ours = true;
1787 }
1788
8e274523
LP
1789 /* Ensure this hasn't been added yet */
1790 assert(!b->unit);
1791
d686d8a9
LP
1792 if (streq(b->controller, SYSTEMD_CGROUP_CONTROLLER)) {
1793 CGroupBonding *l;
8e274523 1794
d686d8a9
LP
1795 l = hashmap_get(u->meta.manager->cgroup_bondings, b->path);
1796 LIST_PREPEND(CGroupBonding, by_path, l, b);
1797
1798 if ((r = hashmap_replace(u->meta.manager->cgroup_bondings, b->path, l)) < 0) {
1799 LIST_REMOVE(CGroupBonding, by_path, l, b);
1800 return r;
1801 }
8e274523
LP
1802 }
1803
1804 LIST_PREPEND(CGroupBonding, by_unit, u->meta.cgroup_bondings, b);
1805 b->unit = u;
1806
1807 return 0;
1808}
1809
013b87c0
LP
1810static char *default_cgroup_path(Unit *u) {
1811 char *p;
1812
1813 assert(u);
1814
4f2d528d
LP
1815 if (u->meta.instance) {
1816 char *t;
013b87c0 1817
cd0ed1db
LP
1818 t = unit_name_template(u->meta.id);
1819 if (!t)
4f2d528d
LP
1820 return NULL;
1821
cd0ed1db 1822 p = join(u->meta.manager->cgroup_hierarchy, "/", t, "/", u->meta.instance, NULL);
4f2d528d
LP
1823 free(t);
1824 } else
cd0ed1db 1825 p = join(u->meta.manager->cgroup_hierarchy, "/", u->meta.id, NULL);
4f2d528d 1826
cd0ed1db 1827 return p;
013b87c0
LP
1828}
1829
8e274523 1830int unit_add_cgroup_from_text(Unit *u, const char *name) {
013b87c0
LP
1831 char *controller = NULL, *path = NULL;
1832 CGroupBonding *b = NULL;
e025b4c3 1833 bool ours = false;
8e274523
LP
1834 int r;
1835
1836 assert(u);
1837 assert(name);
1838
35d2e7ec
LP
1839 if ((r = cg_split_spec(name, &controller, &path)) < 0)
1840 return r;
8e274523 1841
e025b4c3 1842 if (!path) {
35d2e7ec 1843 path = default_cgroup_path(u);
e025b4c3
LP
1844 ours = true;
1845 }
013b87c0 1846
e025b4c3 1847 if (!controller) {
55096547 1848 controller = strdup(SYSTEMD_CGROUP_CONTROLLER);
e025b4c3
LP
1849 ours = true;
1850 }
013b87c0 1851
35d2e7ec
LP
1852 if (!path || !controller) {
1853 free(path);
1854 free(controller);
1855
1856 return -ENOMEM;
8e274523
LP
1857 }
1858
013b87c0
LP
1859 if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
1860 r = -EEXIST;
1861 goto fail;
1862 }
8e274523 1863
013b87c0 1864 if (!(b = new0(CGroupBonding, 1))) {
8e274523
LP
1865 r = -ENOMEM;
1866 goto fail;
1867 }
1868
013b87c0
LP
1869 b->controller = controller;
1870 b->path = path;
e025b4c3
LP
1871 b->ours = ours;
1872 b->essential = streq(controller, SYSTEMD_CGROUP_CONTROLLER);
8e274523
LP
1873
1874 if ((r = unit_add_cgroup(u, b)) < 0)
1875 goto fail;
1876
1877 return 0;
1878
1879fail:
013b87c0
LP
1880 free(path);
1881 free(controller);
8e274523
LP
1882 free(b);
1883
1884 return r;
1885}
1886
06d4c99a 1887static int unit_add_one_default_cgroup(Unit *u, const char *controller) {
d686d8a9 1888 CGroupBonding *b = NULL;
8e274523
LP
1889 int r = -ENOMEM;
1890
1891 assert(u);
1892
06d4c99a
LP
1893 if (!controller)
1894 controller = SYSTEMD_CGROUP_CONTROLLER;
8e274523 1895
06d4c99a
LP
1896 if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller))
1897 return 0;
8e274523 1898
06d4c99a
LP
1899 if (!(b = new0(CGroupBonding, 1)))
1900 return -ENOMEM;
8e274523 1901
06d4c99a
LP
1902 if (!(b->controller = strdup(controller)))
1903 goto fail;
d686d8a9 1904
06d4c99a
LP
1905 if (!(b->path = default_cgroup_path(u)))
1906 goto fail;
d686d8a9 1907
06d4c99a
LP
1908 b->ours = true;
1909 b->essential = streq(controller, SYSTEMD_CGROUP_CONTROLLER);
d686d8a9 1910
06d4c99a
LP
1911 if ((r = unit_add_cgroup(u, b)) < 0)
1912 goto fail;
8e274523
LP
1913
1914 return 0;
1915
1916fail:
06d4c99a
LP
1917 free(b->path);
1918 free(b->controller);
1919 free(b);
8e274523
LP
1920
1921 return r;
1922}
1923
06d4c99a 1924int unit_add_default_cgroups(Unit *u) {
ab1f0633 1925 CGroupAttribute *a;
06d4c99a
LP
1926 char **c;
1927 int r;
ab1f0633 1928
06d4c99a
LP
1929 assert(u);
1930
1931 /* Adds in the default cgroups, if they weren't specified
1932 * otherwise. */
1933
df18d8c8
LP
1934 if (!u->meta.manager->cgroup_hierarchy)
1935 return 0;
1936
06d4c99a
LP
1937 if ((r = unit_add_one_default_cgroup(u, NULL)) < 0)
1938 return r;
1939
1940 STRV_FOREACH(c, u->meta.manager->default_controllers)
ab1f0633
LP
1941 unit_add_one_default_cgroup(u, *c);
1942
1943 LIST_FOREACH(by_unit, a, u->meta.cgroup_attributes)
1944 unit_add_one_default_cgroup(u, a->controller);
06d4c99a
LP
1945
1946 return 0;
1947}
1948
8e274523
LP
1949CGroupBonding* unit_get_default_cgroup(Unit *u) {
1950 assert(u);
1951
55096547 1952 return cgroup_bonding_find_list(u->meta.cgroup_bondings, SYSTEMD_CGROUP_CONTROLLER);
8e274523
LP
1953}
1954
ab1f0633
LP
1955int unit_add_cgroup_attribute(Unit *u, const char *controller, const char *name, const char *value, CGroupAttributeMapCallback map_callback) {
1956 int r;
1957 char *c = NULL;
1958 CGroupAttribute *a;
1959
1960 assert(u);
1961 assert(name);
1962 assert(value);
1963
1964 if (!controller) {
1965 const char *dot;
1966
1967 dot = strchr(name, '.');
1968 if (!dot)
1969 return -EINVAL;
1970
1971 c = strndup(name, dot - name);
1972 if (!c)
1973 return -ENOMEM;
1974
1975 controller = c;
1976 }
1977
1978 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
1979 r = -EINVAL;
1980 goto finish;
1981 }
1982
1983 a = new0(CGroupAttribute, 1);
1984 if (!a) {
1985 r = -ENOMEM;
1986 goto finish;
1987 }
1988
1989 if (c) {
1990 a->controller = c;
1991 c = NULL;
1992 } else
1993 a->controller = strdup(controller);
1994
1995 a->name = strdup(name);
1996 a->value = strdup(value);
1997
1998 if (!a->controller || !a->name || !a->value) {
1999 free(a->controller);
2000 free(a->name);
2001 free(a->value);
2002 free(a);
2003
2004 return -ENOMEM;
2005 }
2006
2007 a->map_callback = map_callback;
2008
2009 LIST_PREPEND(CGroupAttribute, by_unit, u->meta.cgroup_attributes, a);
2010
2011 r = 0;
2012
2013finish:
2014 free(c);
2015 return r;
2016}
2017
f6ff8c29
LP
2018int unit_load_related_unit(Unit *u, const char *type, Unit **_found) {
2019 char *t;
2020 int r;
2021
2022 assert(u);
2023 assert(type);
2024 assert(_found);
2025
9e2f7c11 2026 if (!(t = unit_name_change_suffix(u->meta.id, type)))
f6ff8c29
LP
2027 return -ENOMEM;
2028
2029 assert(!unit_has_name(u, t));
2030
398ef8ba 2031 r = manager_load_unit(u->meta.manager, t, NULL, NULL, _found);
f6ff8c29
LP
2032 free(t);
2033
9e2f7c11 2034 assert(r < 0 || *_found != u);
f6ff8c29
LP
2035
2036 return r;
2037}
2038
a16e1123
LP
2039int unit_get_related_unit(Unit *u, const char *type, Unit **_found) {
2040 Unit *found;
2041 char *t;
2042
2043 assert(u);
2044 assert(type);
2045 assert(_found);
2046
2047 if (!(t = unit_name_change_suffix(u->meta.id, type)))
2048 return -ENOMEM;
2049
2050 assert(!unit_has_name(u, t));
2051
2052 found = manager_get_unit(u->meta.manager, t);
2053 free(t);
2054
2055 if (!found)
2056 return -ENOENT;
2057
2058 *_found = found;
2059 return 0;
2060}
2061
9e2f7c11
LP
2062static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) {
2063 Unit *u = userdata;
2064 assert(u);
2065
2066 return unit_name_to_prefix_and_instance(u->meta.id);
2067}
2068
2069static char *specifier_prefix(char specifier, void *data, void *userdata) {
2070 Unit *u = userdata;
2071 assert(u);
2072
2073 return unit_name_to_prefix(u->meta.id);
2074}
2075
2076static char *specifier_prefix_unescaped(char specifier, void *data, void *userdata) {
2077 Unit *u = userdata;
2078 char *p, *r;
2079
2080 assert(u);
2081
2082 if (!(p = unit_name_to_prefix(u->meta.id)))
2083 return NULL;
2084
2085 r = unit_name_unescape(p);
2086 free(p);
2087
2088 return r;
2089}
2090
2091static char *specifier_instance_unescaped(char specifier, void *data, void *userdata) {
2092 Unit *u = userdata;
2093 assert(u);
2094
2095 if (u->meta.instance)
2096 return unit_name_unescape(u->meta.instance);
2097
2098 return strdup("");
2099}
2100
9fc50704
LP
2101static char *specifier_filename(char specifier, void *data, void *userdata) {
2102 Unit *u = userdata;
2103 assert(u);
2104
2105 if (u->meta.instance)
2106 return unit_name_path_unescape(u->meta.instance);
2107
2108 return unit_name_to_path(u->meta.instance);
2109}
2110
0aef4345
LP
2111static char *specifier_cgroup(char specifier, void *data, void *userdata) {
2112 Unit *u = userdata;
2113 assert(u);
2114
2115 return default_cgroup_path(u);
2116}
2117
2118static char *specifier_cgroup_root(char specifier, void *data, void *userdata) {
2119 Unit *u = userdata;
2120 char *p;
2121 assert(u);
2122
2123 if (specifier == 'r')
2124 return strdup(u->meta.manager->cgroup_hierarchy);
2125
2126 if (parent_of_path(u->meta.manager->cgroup_hierarchy, &p) < 0)
2127 return strdup("");
2128
2129 if (streq(p, "/")) {
2130 free(p);
2131 return strdup("");
2132 }
2133
2134 return p;
2135}
2136
2137static char *specifier_runtime(char specifier, void *data, void *userdata) {
2138 Unit *u = userdata;
2139 assert(u);
2140
2141 if (u->meta.manager->running_as == MANAGER_USER) {
2142 const char *e;
2143
2144 e = getenv("XDG_RUNTIME_DIR");
2145 if (e)
2146 return strdup(e);
2147 }
2148
2149 return strdup("/run");
2150}
2151
9e2f7c11
LP
2152char *unit_name_printf(Unit *u, const char* format) {
2153
2154 /*
2155 * This will use the passed string as format string and
2156 * replace the following specifiers:
2157 *
2158 * %n: the full id of the unit (foo@bar.waldo)
2159 * %N: the id of the unit without the suffix (foo@bar)
2160 * %p: the prefix (foo)
2161 * %i: the instance (bar)
2162 */
2163
2164 const Specifier table[] = {
2165 { 'n', specifier_string, u->meta.id },
2166 { 'N', specifier_prefix_and_instance, NULL },
2167 { 'p', specifier_prefix, NULL },
2168 { 'i', specifier_string, u->meta.instance },
2169 { 0, NULL, NULL }
2170 };
2171
2172 assert(u);
2173 assert(format);
2174
2175 return specifier_printf(format, table, u);
2176}
2177
2178char *unit_full_printf(Unit *u, const char *format) {
2179
2180 /* This is similar to unit_name_printf() but also supports
0aef4345
LP
2181 * unescaping. Also, adds a couple of additional codes:
2182 *
2183 * %c cgroup path of unit
2184 * %r root cgroup path of this systemd instance (e.g. "/user/lennart/shared/systemd-4711")
2185 * %R parent of root cgroup path (e.g. "/usr/lennart/shared")
2186 * %t the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR)
2187 */
9e2f7c11
LP
2188
2189 const Specifier table[] = {
2190 { 'n', specifier_string, u->meta.id },
2191 { 'N', specifier_prefix_and_instance, NULL },
2192 { 'p', specifier_prefix, NULL },
2193 { 'P', specifier_prefix_unescaped, NULL },
2194 { 'i', specifier_string, u->meta.instance },
2195 { 'I', specifier_instance_unescaped, NULL },
9fc50704 2196 { 'f', specifier_filename, NULL },
0aef4345
LP
2197 { 'c', specifier_cgroup, NULL },
2198 { 'r', specifier_cgroup_root, NULL },
2199 { 'R', specifier_cgroup_root, NULL },
2200 { 't', specifier_runtime, NULL },
9e2f7c11
LP
2201 { 0, NULL, NULL }
2202 };
2203
2204 assert(u);
2205 assert(format);
2206
2207 return specifier_printf(format, table, u);
2208}
2209
2210char **unit_full_printf_strv(Unit *u, char **l) {
2211 size_t n;
2212 char **r, **i, **j;
2213
2214 /* Applies unit_full_printf to every entry in l */
2215
2216 assert(u);
2217
2218 n = strv_length(l);
2219 if (!(r = new(char*, n+1)))
2220 return NULL;
2221
2222 for (i = l, j = r; *i; i++, j++)
2223 if (!(*j = unit_full_printf(u, *i)))
2224 goto fail;
2225
2226 *j = NULL;
2227 return r;
2228
2229fail:
da19d5c1 2230 for (j--; j >= r; j--)
9e2f7c11
LP
2231 free(*j);
2232
2233 free(r);
2234
2235 return NULL;
2236}
2237
05e343b7
LP
2238int unit_watch_bus_name(Unit *u, const char *name) {
2239 assert(u);
2240 assert(name);
2241
2242 /* Watch a specific name on the bus. We only support one unit
2243 * watching each name for now. */
2244
2245 return hashmap_put(u->meta.manager->watch_bus, name, u);
2246}
2247
2248void unit_unwatch_bus_name(Unit *u, const char *name) {
2249 assert(u);
2250 assert(name);
2251
2252 hashmap_remove_value(u->meta.manager->watch_bus, name, u);
2253}
2254
a16e1123
LP
2255bool unit_can_serialize(Unit *u) {
2256 assert(u);
2257
2258 return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item;
2259}
2260
2261int unit_serialize(Unit *u, FILE *f, FDSet *fds) {
2262 int r;
2263
2264 assert(u);
2265 assert(f);
2266 assert(fds);
2267
2268 if (!unit_can_serialize(u))
2269 return 0;
2270
2271 if ((r = UNIT_VTABLE(u)->serialize(u, f, fds)) < 0)
2272 return r;
2273
cca098b0
LP
2274 if (u->meta.job)
2275 unit_serialize_item(u, f, "job", job_type_to_string(u->meta.job->type));
2276
10717a1a
LP
2277 dual_timestamp_serialize(f, "inactive-exit-timestamp", &u->meta.inactive_exit_timestamp);
2278 dual_timestamp_serialize(f, "active-enter-timestamp", &u->meta.active_enter_timestamp);
2279 dual_timestamp_serialize(f, "active-exit-timestamp", &u->meta.active_exit_timestamp);
2280 dual_timestamp_serialize(f, "inactive-enter-timestamp", &u->meta.inactive_enter_timestamp);
2791a8f8
LP
2281 dual_timestamp_serialize(f, "condition-timestamp", &u->meta.condition_timestamp);
2282
2283 if (dual_timestamp_is_set(&u->meta.condition_timestamp))
2284 unit_serialize_item(u, f, "condition-result", yes_no(u->meta.condition_result));
10717a1a 2285
a16e1123
LP
2286 /* End marker */
2287 fputc('\n', f);
2288 return 0;
2289}
2290
2291void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *format, ...) {
2292 va_list ap;
2293
2294 assert(u);
2295 assert(f);
2296 assert(key);
2297 assert(format);
2298
2299 fputs(key, f);
2300 fputc('=', f);
2301
2302 va_start(ap, format);
2303 vfprintf(f, format, ap);
2304 va_end(ap);
2305
2306 fputc('\n', f);
2307}
2308
2309void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value) {
2310 assert(u);
2311 assert(f);
2312 assert(key);
2313 assert(value);
2314
2315 fprintf(f, "%s=%s\n", key, value);
2316}
2317
2318int unit_deserialize(Unit *u, FILE *f, FDSet *fds) {
2319 int r;
2320
2321 assert(u);
2322 assert(f);
2323 assert(fds);
2324
2325 if (!unit_can_serialize(u))
2326 return 0;
2327
2328 for (;;) {
20c03b7b 2329 char line[LINE_MAX], *l, *v;
a16e1123
LP
2330 size_t k;
2331
2332 if (!fgets(line, sizeof(line), f)) {
2333 if (feof(f))
2334 return 0;
2335 return -errno;
2336 }
2337
10f8e83c 2338 char_array_0(line);
a16e1123
LP
2339 l = strstrip(line);
2340
2341 /* End marker */
2342 if (l[0] == 0)
2343 return 0;
2344
2345 k = strcspn(l, "=");
2346
2347 if (l[k] == '=') {
2348 l[k] = 0;
2349 v = l+k+1;
2350 } else
2351 v = l+k;
2352
cca098b0
LP
2353 if (streq(l, "job")) {
2354 JobType type;
2355
2356 if ((type = job_type_from_string(v)) < 0)
2357 log_debug("Failed to parse job type value %s", v);
2358 else
2359 u->meta.deserialized_job = type;
2360
2361 continue;
8aaf019b 2362 } else if (streq(l, "inactive-exit-timestamp")) {
799fd0fd 2363 dual_timestamp_deserialize(v, &u->meta.inactive_exit_timestamp);
8aaf019b
LP
2364 continue;
2365 } else if (streq(l, "active-enter-timestamp")) {
799fd0fd 2366 dual_timestamp_deserialize(v, &u->meta.active_enter_timestamp);
8aaf019b
LP
2367 continue;
2368 } else if (streq(l, "active-exit-timestamp")) {
799fd0fd 2369 dual_timestamp_deserialize(v, &u->meta.active_exit_timestamp);
8aaf019b
LP
2370 continue;
2371 } else if (streq(l, "inactive-enter-timestamp")) {
799fd0fd 2372 dual_timestamp_deserialize(v, &u->meta.inactive_enter_timestamp);
8aaf019b 2373 continue;
2791a8f8
LP
2374 } else if (streq(l, "condition-timestamp")) {
2375 dual_timestamp_deserialize(v, &u->meta.condition_timestamp);
2376 continue;
2377 } else if (streq(l, "condition-result")) {
2378 int b;
2379
2380 if ((b = parse_boolean(v)) < 0)
2381 log_debug("Failed to parse condition result value %s", v);
2382 else
2383 u->meta.condition_result = b;
efbac6d2
LP
2384
2385 continue;
8aaf019b 2386 }
cca098b0 2387
a16e1123
LP
2388 if ((r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds)) < 0)
2389 return r;
2390 }
2391}
2392
6e2ef85b
LP
2393int unit_add_node_link(Unit *u, const char *what, bool wants) {
2394 Unit *device;
2395 char *e;
2396 int r;
2397
2398 assert(u);
2399
2400 if (!what)
2401 return 0;
2402
2403 /* Adds in links to the device node that this unit is based on */
2404
8407a5d0 2405 if (!is_device_path(what))
6e2ef85b
LP
2406 return 0;
2407
2408 if (!(e = unit_name_build_escape(what+1, NULL, ".device")))
2409 return -ENOMEM;
2410
398ef8ba 2411 r = manager_load_unit(u->meta.manager, e, NULL, NULL, &device);
6e2ef85b
LP
2412 free(e);
2413
2414 if (r < 0)
2415 return r;
2416
b81884e7 2417 if ((r = unit_add_two_dependencies(u, UNIT_AFTER, UNIT_BIND_TO, device, true)) < 0)
6e2ef85b
LP
2418 return r;
2419
2420 if (wants)
2421 if ((r = unit_add_dependency(device, UNIT_WANTS, u, false)) < 0)
2422 return r;
2423
2424 return 0;
2425}
a16e1123 2426
cca098b0
LP
2427int unit_coldplug(Unit *u) {
2428 int r;
2429
2430 assert(u);
2431
2432 if (UNIT_VTABLE(u)->coldplug)
2433 if ((r = UNIT_VTABLE(u)->coldplug(u)) < 0)
2434 return r;
2435
2436 if (u->meta.deserialized_job >= 0) {
cebe0d41 2437 if ((r = manager_add_job(u->meta.manager, u->meta.deserialized_job, u, JOB_IGNORE_REQUIREMENTS, false, NULL, NULL)) < 0)
cca098b0
LP
2438 return r;
2439
2440 u->meta.deserialized_job = _JOB_TYPE_INVALID;
2441 }
2442
2443 return 0;
2444}
2445
5831e9b7 2446void unit_status_printf(Unit *u, const char *status, const char *format, ...) {
9e58ff9c
LP
2447 va_list ap;
2448
2449 assert(u);
2450 assert(format);
2451
2452 if (!UNIT_VTABLE(u)->show_status)
2453 return;
2454
27d340c7 2455 if (!manager_get_show_status(u->meta.manager))
9e58ff9c
LP
2456 return;
2457
2458 if (!manager_is_booting_or_shutting_down(u->meta.manager))
2459 return;
2460
2461 va_start(ap, format);
67e5cc4f 2462 status_vprintf(status, true, format, ap);
9e58ff9c
LP
2463 va_end(ap);
2464}
2465
45fb0699 2466bool unit_need_daemon_reload(Unit *u) {
45fb0699
LP
2467 assert(u);
2468
5f4b19f4
LP
2469 if (u->meta.fragment_path) {
2470 struct stat st;
45fb0699 2471
5f4b19f4
LP
2472 zero(st);
2473 if (stat(u->meta.fragment_path, &st) < 0)
2474 /* What, cannot access this anymore? */
2475 return true;
45fb0699 2476
5f4b19f4
LP
2477 if (u->meta.fragment_mtime > 0 &&
2478 timespec_load(&st.st_mtim) != u->meta.fragment_mtime)
2479 return true;
2480 }
2481
2482 if (UNIT_VTABLE(u)->need_daemon_reload)
2483 return UNIT_VTABLE(u)->need_daemon_reload(u);
2484
2485 return false;
45fb0699
LP
2486}
2487
fdf20a31 2488void unit_reset_failed(Unit *u) {
5632e374
LP
2489 assert(u);
2490
fdf20a31
MM
2491 if (UNIT_VTABLE(u)->reset_failed)
2492 UNIT_VTABLE(u)->reset_failed(u);
5632e374
LP
2493}
2494
a7f241db
LP
2495Unit *unit_following(Unit *u) {
2496 assert(u);
2497
2498 if (UNIT_VTABLE(u)->following)
2499 return UNIT_VTABLE(u)->following(u);
2500
2501 return NULL;
2502}
2503
18ffdfda
LP
2504bool unit_pending_inactive(Unit *u) {
2505 assert(u);
2506
2507 /* Returns true if the unit is inactive or going down */
2508
2509 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)))
2510 return true;
2511
2512 if (u->meta.job && u->meta.job->type == JOB_STOP)
2513 return true;
2514
2515 return false;
2516}
2517
f976f3f6
LP
2518bool unit_pending_active(Unit *u) {
2519 assert(u);
2520
f60c2665 2521 /* Returns true if the unit is active or going up */
f976f3f6
LP
2522
2523 if (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
2524 return true;
2525
2526 if (u->meta.job &&
2527 (u->meta.job->type == JOB_START ||
2528 u->meta.job->type == JOB_RELOAD_OR_START ||
2529 u->meta.job->type == JOB_RESTART))
2530 return true;
2531
2532 return false;
2533}
2534
71fad675
LP
2535UnitType unit_name_to_type(const char *n) {
2536 UnitType t;
2537
2538 assert(n);
2539
2540 for (t = 0; t < _UNIT_TYPE_MAX; t++)
2541 if (endswith(n, unit_vtable[t]->suffix))
2542 return t;
2543
2544 return _UNIT_TYPE_INVALID;
2545}
2546
b9c0d441 2547bool unit_name_is_valid(const char *n, bool template_ok) {
71fad675
LP
2548 UnitType t;
2549
2550 t = unit_name_to_type(n);
2551 if (t < 0 || t >= _UNIT_TYPE_MAX)
2552 return false;
2553
b9c0d441 2554 return unit_name_is_valid_no_type(n, template_ok);
71fad675
LP
2555}
2556
8a0867d6
LP
2557int unit_kill(Unit *u, KillWho w, KillMode m, int signo, DBusError *error) {
2558 assert(u);
2559 assert(w >= 0 && w < _KILL_WHO_MAX);
2560 assert(m >= 0 && m < _KILL_MODE_MAX);
2561 assert(signo > 0);
2562 assert(signo < _NSIG);
2563
2564 if (m == KILL_NONE)
2565 return 0;
2566
2567 if (!UNIT_VTABLE(u)->kill)
2568 return -ENOTSUP;
2569
2570 return UNIT_VTABLE(u)->kill(u, w, m, signo, error);
2571}
2572
6210e7fc
LP
2573int unit_following_set(Unit *u, Set **s) {
2574 assert(u);
2575 assert(s);
2576
2577 if (UNIT_VTABLE(u)->following_set)
2578 return UNIT_VTABLE(u)->following_set(u, s);
2579
2580 *s = NULL;
2581 return 0;
2582}
2583
a4375746
LP
2584UnitFileState unit_get_unit_file_state(Unit *u) {
2585 assert(u);
2586
2587 if (u->meta.unit_file_state < 0 && u->meta.fragment_path)
2588 u->meta.unit_file_state = unit_file_get_state(
2589 u->meta.manager->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER,
2590 NULL, file_name_from_path(u->meta.fragment_path));
2591
2592 return u->meta.unit_file_state;
2593}
2594
94f04347
LP
2595static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
2596 [UNIT_STUB] = "stub",
2597 [UNIT_LOADED] = "loaded",
fdf20a31 2598 [UNIT_ERROR] = "error",
00dc5d76 2599 [UNIT_MERGED] = "merged",
6daf4f90 2600 [UNIT_MASKED] = "masked"
94f04347
LP
2601};
2602
2603DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
2604
2605static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
2606 [UNIT_ACTIVE] = "active",
032ff4af 2607 [UNIT_RELOADING] = "reloading",
94f04347 2608 [UNIT_INACTIVE] = "inactive",
fdf20a31 2609 [UNIT_FAILED] = "failed",
94f04347
LP
2610 [UNIT_ACTIVATING] = "activating",
2611 [UNIT_DEACTIVATING] = "deactivating"
2612};
2613
2614DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
2615
2616static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
2617 [UNIT_REQUIRES] = "Requires",
9e2f7c11 2618 [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable",
94f04347
LP
2619 [UNIT_WANTS] = "Wants",
2620 [UNIT_REQUISITE] = "Requisite",
9e2f7c11 2621 [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable",
94f04347 2622 [UNIT_REQUIRED_BY] = "RequiredBy",
9e2f7c11 2623 [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable",
b81884e7 2624 [UNIT_BIND_TO] = "BindTo",
94f04347
LP
2625 [UNIT_WANTED_BY] = "WantedBy",
2626 [UNIT_CONFLICTS] = "Conflicts",
69dd2852 2627 [UNIT_CONFLICTED_BY] = "ConflictedBy",
b81884e7 2628 [UNIT_BOUND_BY] = "BoundBy",
94f04347
LP
2629 [UNIT_BEFORE] = "Before",
2630 [UNIT_AFTER] = "After",
701cc384 2631 [UNIT_REFERENCES] = "References",
5de9682c
LP
2632 [UNIT_REFERENCED_BY] = "ReferencedBy",
2633 [UNIT_ON_FAILURE] = "OnFailure"
94f04347
LP
2634};
2635
2636DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);