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