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