]> git.ipfire.org Git - people/ms/systemd.git/blame - unit.c
git: ignore Makefile
[people/ms/systemd.git] / unit.c
CommitLineData
87f0e418
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
a7334b09
LP
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
87f0e418
LP
22#include <assert.h>
23#include <errno.h>
24#include <string.h>
25#include <sys/epoll.h>
26#include <sys/timerfd.h>
27#include <sys/poll.h>
0301abf4
LP
28#include <stdlib.h>
29#include <unistd.h>
87f0e418
LP
30
31#include "set.h"
32#include "unit.h"
33#include "macro.h"
34#include "strv.h"
35#include "load-fragment.h"
36#include "load-dropin.h"
37#include "log.h"
38
39const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
40 [UNIT_SERVICE] = &service_vtable,
41 [UNIT_TIMER] = &timer_vtable,
42 [UNIT_SOCKET] = &socket_vtable,
43 [UNIT_TARGET] = &target_vtable,
44 [UNIT_DEVICE] = &device_vtable,
45 [UNIT_MOUNT] = &mount_vtable,
46 [UNIT_AUTOMOUNT] = &automount_vtable,
47 [UNIT_SNAPSHOT] = &snapshot_vtable
48};
49
50UnitType unit_name_to_type(const char *n) {
51 UnitType t;
52
53 assert(n);
54
55 for (t = 0; t < _UNIT_TYPE_MAX; t++)
56 if (endswith(n, unit_vtable[t]->suffix))
57 return t;
58
59 return _UNIT_TYPE_INVALID;
60}
61
62#define VALID_CHARS \
63 "0123456789" \
64 "abcdefghijklmnopqrstuvwxyz" \
65 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
b5ea5d95 66 "-_.\\"
87f0e418
LP
67
68bool unit_name_is_valid(const char *n) {
69 UnitType t;
70 const char *e, *i;
71
72 assert(n);
73
74 if (strlen(n) >= UNIT_NAME_MAX)
75 return false;
76
77 t = unit_name_to_type(n);
78 if (t < 0 || t >= _UNIT_TYPE_MAX)
79 return false;
80
81 if (!(e = strrchr(n, '.')))
82 return false;
83
48e11fe8
LP
84 if (e == n)
85 return false;
86
87f0e418
LP
87 for (i = n; i < e; i++)
88 if (!strchr(VALID_CHARS, *i))
89 return false;
90
91 return true;
92}
93
94char *unit_name_change_suffix(const char *n, const char *suffix) {
95 char *e, *r;
96 size_t a, b;
97
98 assert(n);
99 assert(unit_name_is_valid(n));
100 assert(suffix);
101
102 assert_se(e = strrchr(n, '.'));
103 a = e - n;
104 b = strlen(suffix);
105
106 if (!(r = new(char, a + b + 1)))
107 return NULL;
108
109 memcpy(r, n, a);
110 memcpy(r+a, suffix, b+1);
111
112 return r;
113}
114
115Unit *unit_new(Manager *m) {
116 Unit *u;
117
118 assert(m);
119
120 if (!(u = new0(Unit, 1)))
121 return NULL;
122
123 if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) {
124 free(u);
125 return NULL;
126 }
127
128 u->meta.manager = m;
129 u->meta.type = _UNIT_TYPE_INVALID;
130
131 return u;
132}
133
134int unit_add_name(Unit *u, const char *text) {
135 UnitType t;
136 char *s;
137 int r;
138
139 assert(u);
140 assert(text);
141
142 if (!unit_name_is_valid(text))
143 return -EINVAL;
144
145 if ((t = unit_name_to_type(text)) == _UNIT_TYPE_INVALID)
146 return -EINVAL;
147
148 if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type)
149 return -EINVAL;
150
151 if (!(s = strdup(text)))
152 return -ENOMEM;
153
154 if ((r = set_put(u->meta.names, s)) < 0) {
155 free(s);
156
157 if (r == -EEXIST)
158 return 0;
159
160 return r;
161 }
162
163 if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
164 set_remove(u->meta.names, s);
165 free(s);
166 return r;
167 }
168
ef734fd6
LP
169 if (u->meta.type == _UNIT_TYPE_INVALID)
170 LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);
171
87f0e418
LP
172 u->meta.type = t;
173
174 if (!u->meta.id)
175 u->meta.id = s;
176
c1e1601e 177 unit_add_to_dbus_queue(u);
87f0e418
LP
178 return 0;
179}
180
0ae97ec1
LP
181int unit_choose_id(Unit *u, const char *name) {
182 char *s;
183
184 assert(u);
185 assert(name);
186
187 /* Selects one of the names of this unit as the id */
188
189 if (!(s = set_get(u->meta.names, (char*) name)))
190 return -ENOENT;
191
192 u->meta.id = s;
c1e1601e
LP
193
194 unit_add_to_dbus_queue(u);
0ae97ec1
LP
195 return 0;
196}
197
f50e0a01
LP
198int unit_set_description(Unit *u, const char *description) {
199 char *s;
200
201 assert(u);
202
203 if (!(s = strdup(description)))
204 return -ENOMEM;
205
206 free(u->meta.description);
207 u->meta.description = s;
c1e1601e
LP
208
209 unit_add_to_dbus_queue(u);
f50e0a01
LP
210 return 0;
211}
212
87f0e418
LP
213void unit_add_to_load_queue(Unit *u) {
214 assert(u);
215
216 if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
217 return;
218
219 LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
220 u->meta.in_load_queue = true;
221}
222
c1e1601e
LP
223void unit_add_to_dbus_queue(Unit *u) {
224 assert(u);
225
226 if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue || set_isempty(u->meta.manager->subscribed))
227 return;
228
229 LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
230 u->meta.in_dbus_queue = true;
231}
232
87f0e418
LP
233static void bidi_set_free(Unit *u, Set *s) {
234 Iterator i;
235 Unit *other;
236
237 assert(u);
238
239 /* Frees the set and makes sure we are dropped from the
240 * inverse pointers */
241
242 SET_FOREACH(other, s, i) {
243 UnitDependency d;
244
245 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
246 set_remove(other->meta.dependencies[d], u);
247 }
248
249 set_free(s);
250}
251
252void unit_free(Unit *u) {
253 UnitDependency d;
254 Iterator i;
255 char *t;
256
257 assert(u);
258
c1e1601e
LP
259 bus_unit_send_removed_signal(u);
260
87f0e418
LP
261 /* Detach from next 'bigger' objects */
262
263 SET_FOREACH(t, u->meta.names, i)
264 hashmap_remove_value(u->meta.manager->units, t, u);
265
ef734fd6
LP
266 if (u->meta.type != _UNIT_TYPE_INVALID)
267 LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta);
268
87f0e418
LP
269 if (u->meta.in_load_queue)
270 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
271
c1e1601e
LP
272 if (u->meta.in_dbus_queue)
273 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
274
87f0e418
LP
275 if (u->meta.load_state == UNIT_LOADED)
276 if (UNIT_VTABLE(u)->done)
277 UNIT_VTABLE(u)->done(u);
278
279 /* Free data and next 'smaller' objects */
280 if (u->meta.job)
281 job_free(u->meta.job);
282
283 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
284 bidi_set_free(u, u->meta.dependencies[d]);
285
286 free(u->meta.description);
0301abf4 287 free(u->meta.load_path);
87f0e418
LP
288
289 while ((t = set_steal_first(u->meta.names)))
290 free(t);
291 set_free(u->meta.names);
292
293 free(u);
294}
295
296UnitActiveState unit_active_state(Unit *u) {
297 assert(u);
298
299 if (u->meta.load_state != UNIT_LOADED)
300 return UNIT_INACTIVE;
301
302 return UNIT_VTABLE(u)->active_state(u);
303}
304
305static int ensure_merge(Set **s, Set *other) {
306
307 if (!other)
308 return 0;
309
310 if (*s)
311 return set_merge(*s, other);
312
313 if (!(*s = set_copy(other)))
314 return -ENOMEM;
315
316 return 0;
317}
318
fdf88f5f
LP
319/* FIXME: Does not rollback on failure! Needs to fix special unit
320 * pointers. Needs to merge names and dependencies properly.*/
87f0e418
LP
321int unit_merge(Unit *u, Unit *other) {
322 int r;
323 UnitDependency d;
324
325 assert(u);
326 assert(other);
327 assert(u->meta.manager == other->meta.manager);
328
329 /* This merges 'other' into 'unit'. FIXME: This does not
330 * rollback on failure. */
331
332 if (u->meta.type != u->meta.type)
333 return -EINVAL;
334
335 if (u->meta.load_state != UNIT_STUB)
336 return -EINVAL;
337
338 /* Merge names */
339 if ((r = ensure_merge(&u->meta.names, other->meta.names)) < 0)
340 return r;
341
342 /* Merge dependencies */
343 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
344 /* fixme, the inverse mapping is missing */
345 if ((r = ensure_merge(&u->meta.dependencies[d], other->meta.dependencies[d])) < 0)
346 return r;
347
c1e1601e
LP
348 unit_add_to_dbus_queue(u);
349
87f0e418
LP
350 return 0;
351}
352
353const char* unit_id(Unit *u) {
354 assert(u);
355
356 if (u->meta.id)
357 return u->meta.id;
358
359 return set_first(u->meta.names);
360}
361
362const char *unit_description(Unit *u) {
363 assert(u);
364
365 if (u->meta.description)
366 return u->meta.description;
367
368 return unit_id(u);
369}
370
371void unit_dump(Unit *u, FILE *f, const char *prefix) {
372
87f0e418
LP
373 char *t;
374 UnitDependency d;
375 Iterator i;
47be870b
LP
376 char *p2;
377 const char *prefix2;
87f0e418
LP
378
379 assert(u);
380
381 if (!prefix)
382 prefix = "";
47be870b
LP
383 p2 = strappend(prefix, "\t");
384 prefix2 = p2 ? p2 : prefix;
87f0e418
LP
385
386 fprintf(f,
387 "%s→ Unit %s:\n"
388 "%s\tDescription: %s\n"
389 "%s\tUnit Load State: %s\n"
f3bff0eb 390 "%s\tUnit Active State: %s\n"
94f04347 391 "%s\tRecursive Stop: %s\n"
f3bff0eb 392 "%s\tStop When Unneeded: %s\n",
87f0e418
LP
393 prefix, unit_id(u),
394 prefix, unit_description(u),
94f04347
LP
395 prefix, unit_load_state_to_string(u->meta.load_state),
396 prefix, unit_active_state_to_string(unit_active_state(u)),
f3bff0eb
LP
397 prefix, yes_no(u->meta.recursive_stop),
398 prefix, yes_no(u->meta.stop_when_unneeded));
87f0e418 399
0301abf4
LP
400 if (u->meta.load_path)
401 fprintf(f, "%s\tLoad Path: %s\n", prefix, u->meta.load_path);
402
87f0e418
LP
403 SET_FOREACH(t, u->meta.names, i)
404 fprintf(f, "%s\tName: %s\n", prefix, t);
405
406 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
407 Unit *other;
408
409 if (set_isempty(u->meta.dependencies[d]))
410 continue;
411
412 SET_FOREACH(other, u->meta.dependencies[d], i)
94f04347 413 fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), unit_id(other));
87f0e418
LP
414 }
415
416 if (UNIT_VTABLE(u)->dump)
417 UNIT_VTABLE(u)->dump(u, f, prefix2);
418
419 if (u->meta.job)
420 job_dump(u->meta.job, f, prefix2);
421
47be870b 422 free(p2);
87f0e418
LP
423}
424
425/* Common implementation for multiple backends */
426int unit_load_fragment_and_dropin(Unit *u) {
d46de8a1 427 int r, ret;
87f0e418
LP
428
429 assert(u);
430
431 /* Load a .socket file */
432 if ((r = unit_load_fragment(u)) < 0)
433 return r;
434
d46de8a1
LP
435 ret = r > 0;
436
87f0e418
LP
437 /* Load drop-in directory data */
438 if ((r = unit_load_dropin(u)) < 0)
439 return r;
440
d46de8a1 441 return ret;
87f0e418
LP
442}
443
444int unit_load(Unit *u) {
445 int r;
446
447 assert(u);
448
449 if (u->meta.in_load_queue) {
450 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
451 u->meta.in_load_queue = false;
452 }
453
454 if (u->meta.load_state != UNIT_STUB)
455 return 0;
456
457 if (UNIT_VTABLE(u)->init)
458 if ((r = UNIT_VTABLE(u)->init(u)) < 0)
459 goto fail;
460
461 u->meta.load_state = UNIT_LOADED;
c1e1601e 462 unit_add_to_dbus_queue(u);
87f0e418
LP
463 return 0;
464
465fail:
466 u->meta.load_state = UNIT_FAILED;
c1e1601e 467 unit_add_to_dbus_queue(u);
87f0e418
LP
468 return r;
469}
470
471/* Errors:
472 * -EBADR: This unit type does not support starting.
473 * -EALREADY: Unit is already started.
474 * -EAGAIN: An operation is already in progress. Retry later.
475 */
476int unit_start(Unit *u) {
477 UnitActiveState state;
478
479 assert(u);
480
7898b0cf
LP
481 /* If this is already (being) started, then this will
482 * succeed. Note that this will even succeed if this unit is
483 * not startable by the user. This is relied on to detect when
484 * we need to wait for units and when waiting is finished. */
87f0e418
LP
485 state = unit_active_state(u);
486 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
487 return -EALREADY;
488
7898b0cf
LP
489 /* If it is stopped, but we cannot start it, then fail */
490 if (!UNIT_VTABLE(u)->start)
491 return -EBADR;
492
87f0e418
LP
493 /* We don't suppress calls to ->start() here when we are
494 * already starting, to allow this request to be used as a
495 * "hurry up" call, for example when the unit is in some "auto
496 * restart" state where it waits for a holdoff timer to elapse
497 * before it will start again. */
498
c1e1601e 499 unit_add_to_dbus_queue(u);
87f0e418
LP
500 return UNIT_VTABLE(u)->start(u);
501}
502
503bool unit_can_start(Unit *u) {
504 assert(u);
505
506 return !!UNIT_VTABLE(u)->start;
507}
508
509/* Errors:
510 * -EBADR: This unit type does not support stopping.
511 * -EALREADY: Unit is already stopped.
512 * -EAGAIN: An operation is already in progress. Retry later.
513 */
514int unit_stop(Unit *u) {
515 UnitActiveState state;
516
517 assert(u);
518
87f0e418
LP
519 state = unit_active_state(u);
520 if (state == UNIT_INACTIVE)
521 return -EALREADY;
522
7898b0cf
LP
523 if (!UNIT_VTABLE(u)->stop)
524 return -EBADR;
525
87f0e418
LP
526 if (state == UNIT_DEACTIVATING)
527 return 0;
528
c1e1601e 529 unit_add_to_dbus_queue(u);
87f0e418
LP
530 return UNIT_VTABLE(u)->stop(u);
531}
532
533/* Errors:
534 * -EBADR: This unit type does not support reloading.
535 * -ENOEXEC: Unit is not started.
536 * -EAGAIN: An operation is already in progress. Retry later.
537 */
538int unit_reload(Unit *u) {
539 UnitActiveState state;
540
541 assert(u);
542
543 if (!unit_can_reload(u))
544 return -EBADR;
545
546 state = unit_active_state(u);
547 if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
548 return -EALREADY;
549
550 if (unit_active_state(u) != UNIT_ACTIVE)
551 return -ENOEXEC;
552
c1e1601e 553 unit_add_to_dbus_queue(u);
87f0e418
LP
554 return UNIT_VTABLE(u)->reload(u);
555}
556
557bool unit_can_reload(Unit *u) {
558 assert(u);
559
560 if (!UNIT_VTABLE(u)->reload)
561 return false;
562
563 if (!UNIT_VTABLE(u)->can_reload)
564 return true;
565
566 return UNIT_VTABLE(u)->can_reload(u);
567}
568
f3bff0eb
LP
569static void unit_check_uneeded(Unit *u) {
570 Iterator i;
571 Unit *other;
572
573 assert(u);
574
575 /* If this service shall be shut down when unneeded then do
576 * so. */
577
578 if (!u->meta.stop_when_unneeded)
579 return;
580
581 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
582 return;
583
584 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
585 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
586 return;
587
588 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
589 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
590 return;
591
592 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
593 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
594 return;
595
596 log_debug("Service %s is not needed anymore. Stopping.", unit_id(u));
597
598 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
599 manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL);
600}
601
87f0e418
LP
602static void retroactively_start_dependencies(Unit *u) {
603 Iterator i;
604 Unit *other;
605
606 assert(u);
607 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
608
609 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
610 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
611 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
612
613 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
614 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
615 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
616
617 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
618 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
619 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
620
621 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
622 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
623 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
624
625 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
626 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
627 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
628}
629
630static void retroactively_stop_dependencies(Unit *u) {
631 Iterator i;
632 Unit *other;
633
634 assert(u);
635 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
636
f3bff0eb
LP
637 if (u->meta.recursive_stop) {
638 /* Pull down units need us recursively if enabled */
639 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
640 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
641 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
642 }
643
644 /* Garbage collect services that might not be needed anymore, if enabled */
645 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
87f0e418 646 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
f3bff0eb
LP
647 unit_check_uneeded(other);
648 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
649 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
650 unit_check_uneeded(other);
651 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
652 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
653 unit_check_uneeded(other);
654 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
655 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
656 unit_check_uneeded(other);
657 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUISITE], i)
658 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
659 unit_check_uneeded(other);
87f0e418
LP
660}
661
662void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
663 assert(u);
664 assert(os < _UNIT_ACTIVE_STATE_MAX);
665 assert(ns < _UNIT_ACTIVE_STATE_MAX);
666 assert(!(os == UNIT_ACTIVE && ns == UNIT_ACTIVATING));
667 assert(!(os == UNIT_INACTIVE && ns == UNIT_DEACTIVATING));
668
669 if (os == ns)
670 return;
671
672 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
673 u->meta.active_enter_timestamp = now(CLOCK_REALTIME);
674 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
675 u->meta.active_exit_timestamp = now(CLOCK_REALTIME);
676
677 if (u->meta.job) {
678
679 if (u->meta.job->state == JOB_WAITING)
680
681 /* So we reached a different state for this
682 * job. Let's see if we can run it now if it
683 * failed previously due to EAGAIN. */
c1e1601e 684 job_add_to_run_queue(u->meta.job);
87f0e418
LP
685
686 else {
687 assert(u->meta.job->state == JOB_RUNNING);
688
f50e0a01 689 /* Let's check whether this state change
87f0e418
LP
690 * constitutes a finished job, or maybe
691 * cotradicts a running job and hence needs to
692 * invalidate jobs. */
693
694 switch (u->meta.job->type) {
695
696 case JOB_START:
697 case JOB_VERIFY_ACTIVE:
698
699 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
700 job_finish_and_invalidate(u->meta.job, true);
701 return;
702 } else if (ns == UNIT_ACTIVATING)
703 return;
704 else
705 job_finish_and_invalidate(u->meta.job, false);
706
707 break;
708
709 case JOB_RELOAD:
710 case JOB_RELOAD_OR_START:
711
712 if (ns == UNIT_ACTIVE) {
713 job_finish_and_invalidate(u->meta.job, true);
714 return;
715 } else if (ns == UNIT_ACTIVATING || ns == UNIT_ACTIVE_RELOADING)
716 return;
717 else
718 job_finish_and_invalidate(u->meta.job, false);
719
720 break;
721
722 case JOB_STOP:
723 case JOB_RESTART:
724 case JOB_TRY_RESTART:
725
726 if (ns == UNIT_INACTIVE) {
727 job_finish_and_invalidate(u->meta.job, true);
728 return;
729 } else if (ns == UNIT_DEACTIVATING)
730 return;
731 else
732 job_finish_and_invalidate(u->meta.job, false);
733
734 break;
735
736 default:
737 assert_not_reached("Job type unknown");
738 }
739 }
740 }
741
742 /* If this state change happened without being requested by a
743 * job, then let's retroactively start or stop dependencies */
744
745 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
746 retroactively_start_dependencies(u);
747 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
748 retroactively_stop_dependencies(u);
f3bff0eb
LP
749
750 /* Maybe we finished startup and are now ready for being
751 * stopped because unneeded? */
752 unit_check_uneeded(u);
c1e1601e
LP
753
754 unit_add_to_dbus_queue(u);
87f0e418
LP
755}
756
acbb0225 757int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
87f0e418
LP
758 struct epoll_event ev;
759
760 assert(u);
761 assert(fd >= 0);
acbb0225 762 assert(w);
ea430986 763 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
87f0e418
LP
764
765 zero(ev);
acbb0225 766 ev.data.ptr = w;
87f0e418
LP
767 ev.events = events;
768
acbb0225
LP
769 if (epoll_ctl(u->meta.manager->epoll_fd,
770 w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
771 fd,
772 &ev) < 0)
773 return -errno;
87f0e418 774
acbb0225
LP
775 w->fd = fd;
776 w->type = WATCH_FD;
ea430986 777 w->data.unit = u;
87f0e418 778
acbb0225 779 return 0;
87f0e418
LP
780}
781
acbb0225 782void unit_unwatch_fd(Unit *u, Watch *w) {
87f0e418 783 assert(u);
acbb0225 784 assert(w);
87f0e418 785
acbb0225
LP
786 if (w->type == WATCH_INVALID)
787 return;
788
ea430986
LP
789 assert(w->type == WATCH_FD);
790 assert(w->data.unit == u);
acbb0225
LP
791 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
792
793 w->fd = -1;
794 w->type = WATCH_INVALID;
ea430986 795 w->data.unit = NULL;
87f0e418
LP
796}
797
798int unit_watch_pid(Unit *u, pid_t pid) {
799 assert(u);
800 assert(pid >= 1);
801
802 return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
803}
804
805void unit_unwatch_pid(Unit *u, pid_t pid) {
806 assert(u);
807 assert(pid >= 1);
808
809 hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
810}
811
acbb0225 812int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
87f0e418 813 struct itimerspec its;
acbb0225 814 int flags, fd;
87f0e418
LP
815 bool ours;
816
817 assert(u);
acbb0225 818 assert(w);
ea430986 819 assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->data.unit == u));
87f0e418
LP
820
821 /* This will try to reuse the old timer if there is one */
822
acbb0225 823 if (w->type == WATCH_TIMER) {
87f0e418 824 ours = false;
acbb0225 825 fd = w->fd;
87f0e418
LP
826 } else {
827 ours = true;
87f0e418
LP
828 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
829 return -errno;
830 }
831
832 zero(its);
833
834 if (delay <= 0) {
835 /* Set absolute time in the past, but not 0, since we
836 * don't want to disarm the timer */
837 its.it_value.tv_sec = 0;
838 its.it_value.tv_nsec = 1;
839
840 flags = TFD_TIMER_ABSTIME;
841 } else {
842 timespec_store(&its.it_value, delay);
843 flags = 0;
844 }
845
846 /* This will also flush the elapse counter */
847 if (timerfd_settime(fd, flags, &its, NULL) < 0)
848 goto fail;
849
acbb0225
LP
850 if (w->type == WATCH_INVALID) {
851 struct epoll_event ev;
87f0e418 852
acbb0225
LP
853 zero(ev);
854 ev.data.ptr = w;
f94ea366 855 ev.events = EPOLLIN;
acbb0225
LP
856
857 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
858 goto fail;
859 }
860
861 w->fd = fd;
862 w->type = WATCH_TIMER;
ea430986 863 w->data.unit = u;
87f0e418 864
87f0e418
LP
865 return 0;
866
867fail:
868 if (ours)
ea430986 869 close_nointr_nofail(fd);
87f0e418
LP
870
871 return -errno;
872}
873
acbb0225 874void unit_unwatch_timer(Unit *u, Watch *w) {
87f0e418 875 assert(u);
acbb0225 876 assert(w);
87f0e418 877
acbb0225 878 if (w->type == WATCH_INVALID)
87f0e418
LP
879 return;
880
ea430986 881 assert(w->type == WATCH_TIMER && w->data.unit == u);
acbb0225
LP
882
883 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
884 assert_se(close_nointr(w->fd) == 0);
885
886 w->fd = -1;
887 w->type = WATCH_INVALID;
ea430986 888 w->data.unit = NULL;
87f0e418
LP
889}
890
891bool unit_job_is_applicable(Unit *u, JobType j) {
892 assert(u);
893 assert(j >= 0 && j < _JOB_TYPE_MAX);
894
895 switch (j) {
896
897 case JOB_VERIFY_ACTIVE:
898 case JOB_START:
899 return true;
900
901 case JOB_STOP:
902 case JOB_RESTART:
903 case JOB_TRY_RESTART:
904 return unit_can_start(u);
905
906 case JOB_RELOAD:
907 return unit_can_reload(u);
908
909 case JOB_RELOAD_OR_START:
910 return unit_can_reload(u) && unit_can_start(u);
911
912 default:
913 assert_not_reached("Invalid job type");
914 }
915}
916
917int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {
918
919 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
920 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
921 [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY,
922 [UNIT_WANTS] = UNIT_WANTED_BY,
923 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
924 [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY,
925 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
926 [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
927 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
928 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
929 [UNIT_BEFORE] = UNIT_AFTER,
930 [UNIT_AFTER] = UNIT_BEFORE
931 };
932 int r;
933
934 assert(u);
935 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
936 assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
937 assert(other);
938
939 /* We won't allow dependencies on ourselves. We will not
940 * consider them an error however. */
941 if (u == other)
942 return 0;
943
944 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
945 return r;
946
947 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
948 return r;
949
950 if ((r = set_put(u->meta.dependencies[d], other)) < 0)
951 return r;
952
953 if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
954 set_remove(u->meta.dependencies[d], other);
955 return r;
956 }
957
c1e1601e 958 unit_add_to_dbus_queue(u);
87f0e418
LP
959 return 0;
960}
0301abf4 961
09b6b09f
LP
962int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) {
963 Unit *other;
964 int r;
965
966 if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
967 return r;
968
969 if ((r = unit_add_dependency(u, d, other)) < 0)
970 return r;
971
972 return 0;
973}
974
0301abf4
LP
975int set_unit_path(const char *p) {
976 char *cwd, *c;
977 int r;
978
979 /* This is mostly for debug purposes */
980
981 if (path_is_absolute(p)) {
982 if (!(c = strdup(p)))
983 return -ENOMEM;
984 } else {
985 if (!(cwd = get_current_dir_name()))
986 return -errno;
987
988 r = asprintf(&c, "%s/%s", cwd, p);
989 free(cwd);
990
991 if (r < 0)
992 return -ENOMEM;
993 }
994
036643a2 995 if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
0301abf4
LP
996 r = -errno;
997 free(c);
998 return r;
999 }
1000
1001 return 0;
1002}
88066b3a 1003
b08d03ff 1004char *unit_name_escape_path(const char *prefix, const char *path, const char *suffix) {
88066b3a
LP
1005 char *r, *t;
1006 const char *f;
b08d03ff 1007 size_t a, b, c;
88066b3a
LP
1008
1009 assert(path);
88066b3a 1010
b08d03ff
LP
1011 /* Takes a path and a suffix and prefix and makes a nice
1012 * string suitable as unit name of it, escaping all weird
1013 * chars on the way.
88066b3a 1014 *
b08d03ff
LP
1015 * / becomes ., and all chars not alloweed in a unit name get
1016 * escaped as \xFF, including \ and ., of course. This
1017 * escaping is hence reversible.
88066b3a
LP
1018 */
1019
b08d03ff
LP
1020 if (!prefix)
1021 prefix = "";
1022
1023 if (!suffix)
1024 suffix = "";
88066b3a 1025
b08d03ff
LP
1026 a = strlen(prefix);
1027 b = strlen(path);
1028 c = strlen(suffix);
1029
1030 if (!(r = new(char, a+b*4+c+1)))
88066b3a
LP
1031 return NULL;
1032
b08d03ff
LP
1033 memcpy(r, prefix, a);
1034
1035 for (f = path, t = r+a; *f; f++) {
88066b3a 1036 if (*f == '/')
b08d03ff
LP
1037 *(t++) = '.';
1038 else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
88066b3a
LP
1039 *(t++) = '\\';
1040 *(t++) = 'x';
1041 *(t++) = hexchar(*f > 4);
1042 *(t++) = hexchar(*f);
1043 } else
1044 *(t++) = *f;
1045 }
1046
b08d03ff 1047 memcpy(t, suffix, c+1);
88066b3a
LP
1048
1049 return r;
1050}
94f04347 1051
ea430986
LP
1052char *unit_dbus_path(Unit *u) {
1053 char *p, *e;
1054
1055 assert(u);
1056
1057 if (!(e = bus_path_escape(unit_id(u))))
1058 return NULL;
1059
1060 if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) {
1061 free(e);
1062 return NULL;
1063 }
1064
1065 free(e);
1066 return p;
1067}
1068
94f04347
LP
1069static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
1070 [UNIT_SERVICE] = "service",
1071 [UNIT_TIMER] = "timer",
1072 [UNIT_SOCKET] = "socket",
1073 [UNIT_TARGET] = "target",
1074 [UNIT_DEVICE] = "device",
1075 [UNIT_MOUNT] = "mount",
1076 [UNIT_AUTOMOUNT] = "automount",
1077 [UNIT_SNAPSHOT] = "snapshot"
1078};
1079
1080DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
1081
1082static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
1083 [UNIT_STUB] = "stub",
1084 [UNIT_LOADED] = "loaded",
1085 [UNIT_FAILED] = "failed"
1086};
1087
1088DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
1089
1090static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
1091 [UNIT_ACTIVE] = "active",
1092 [UNIT_INACTIVE] = "inactive",
1093 [UNIT_ACTIVATING] = "activating",
1094 [UNIT_DEACTIVATING] = "deactivating"
1095};
1096
1097DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
1098
1099static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
1100 [UNIT_REQUIRES] = "Requires",
1101 [UNIT_SOFT_REQUIRES] = "SoftRequires",
1102 [UNIT_WANTS] = "Wants",
1103 [UNIT_REQUISITE] = "Requisite",
1104 [UNIT_SOFT_REQUISITE] = "SoftRequisite",
1105 [UNIT_REQUIRED_BY] = "RequiredBy",
1106 [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
1107 [UNIT_WANTED_BY] = "WantedBy",
1108 [UNIT_CONFLICTS] = "Conflicts",
1109 [UNIT_BEFORE] = "Before",
1110 [UNIT_AFTER] = "After",
1111};
1112
1113DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);