1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
25 #include <sys/epoll.h>
26 #include <sys/timerfd.h>
35 #include "load-fragment.h"
36 #include "load-dropin.h"
39 const 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
50 UnitType unit_name_to_type(const char *n) {
55 for (t = 0; t < _UNIT_TYPE_MAX; t++)
56 if (endswith(n, unit_vtable[t]->suffix))
59 return _UNIT_TYPE_INVALID;
64 "abcdefghijklmnopqrstuvwxyz" \
65 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
68 bool unit_name_is_valid(const char *n) {
74 if (strlen(n) >= UNIT_NAME_MAX)
77 t = unit_name_to_type(n);
78 if (t < 0 || t >= _UNIT_TYPE_MAX)
81 if (!(e = strrchr(n, '.')))
87 for (i = n; i < e; i++)
88 if (!strchr(VALID_CHARS, *i))
94 char *unit_name_change_suffix(const char *n, const char *suffix) {
99 assert(unit_name_is_valid(n));
102 assert_se(e = strrchr(n, '.'));
106 if (!(r = new(char, a + b + 1)))
110 memcpy(r+a, suffix, b+1);
115 Unit *unit_new(Manager *m) {
120 if (!(u = new0(Unit, 1)))
123 if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) {
129 u->meta.type = _UNIT_TYPE_INVALID;
134 int unit_add_name(Unit *u, const char *text) {
142 if (!unit_name_is_valid(text))
145 if ((t = unit_name_to_type(text)) == _UNIT_TYPE_INVALID)
148 if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type)
151 if (!(s = strdup(text)))
154 if ((r = set_put(u->meta.names, s)) < 0) {
163 if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
164 set_remove(u->meta.names, s);
169 if (u->meta.type == _UNIT_TYPE_INVALID)
170 LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);
177 unit_add_to_dbus_queue(u);
181 int unit_choose_id(Unit *u, const char *name) {
187 /* Selects one of the names of this unit as the id */
189 if (!(s = set_get(u->meta.names, (char*) name)))
194 unit_add_to_dbus_queue(u);
198 int unit_set_description(Unit *u, const char *description) {
203 if (!(s = strdup(description)))
206 free(u->meta.description);
207 u->meta.description = s;
209 unit_add_to_dbus_queue(u);
213 void unit_add_to_load_queue(Unit *u) {
216 if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
219 LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
220 u->meta.in_load_queue = true;
223 void unit_add_to_dbus_queue(Unit *u) {
226 if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue || set_isempty(u->meta.manager->subscribed))
229 LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
230 u->meta.in_dbus_queue = true;
233 static void bidi_set_free(Unit *u, Set *s) {
239 /* Frees the set and makes sure we are dropped from the
240 * inverse pointers */
242 SET_FOREACH(other, s, i) {
245 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
246 set_remove(other->meta.dependencies[d], u);
252 void unit_free(Unit *u) {
259 bus_unit_send_removed_signal(u);
261 /* Detach from next 'bigger' objects */
263 SET_FOREACH(t, u->meta.names, i)
264 hashmap_remove_value(u->meta.manager->units, t, u);
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);
269 if (u->meta.in_load_queue)
270 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
272 if (u->meta.in_dbus_queue)
273 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
275 if (u->meta.load_state == UNIT_LOADED)
276 if (UNIT_VTABLE(u)->done)
277 UNIT_VTABLE(u)->done(u);
279 /* Free data and next 'smaller' objects */
281 job_free(u->meta.job);
283 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
284 bidi_set_free(u, u->meta.dependencies[d]);
286 free(u->meta.description);
287 free(u->meta.load_path);
289 while ((t = set_steal_first(u->meta.names)))
291 set_free(u->meta.names);
296 UnitActiveState unit_active_state(Unit *u) {
299 if (u->meta.load_state != UNIT_LOADED)
300 return UNIT_INACTIVE;
302 return UNIT_VTABLE(u)->active_state(u);
305 static int ensure_merge(Set **s, Set *other) {
311 return set_merge(*s, other);
313 if (!(*s = set_copy(other)))
319 /* FIXME: Does not rollback on failure! Needs to fix special unit
320 * pointers. Needs to merge names and dependencies properly.*/
321 int unit_merge(Unit *u, Unit *other) {
327 assert(u->meta.manager == other->meta.manager);
329 /* This merges 'other' into 'unit'. FIXME: This does not
330 * rollback on failure. */
332 if (u->meta.type != u->meta.type)
335 if (u->meta.load_state != UNIT_STUB)
339 if ((r = ensure_merge(&u->meta.names, other->meta.names)) < 0)
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)
348 unit_add_to_dbus_queue(u);
353 const char* unit_id(Unit *u) {
359 return set_first(u->meta.names);
362 const char *unit_description(Unit *u) {
365 if (u->meta.description)
366 return u->meta.description;
371 void unit_dump(Unit *u, FILE *f, const char *prefix) {
383 p2 = strappend(prefix, "\t");
384 prefix2 = p2 ? p2 : prefix;
388 "%s\tDescription: %s\n"
389 "%s\tUnit Load State: %s\n"
390 "%s\tUnit Active State: %s\n"
391 "%s\tRecursive Stop: %s\n"
392 "%s\tStop When Unneeded: %s\n",
394 prefix, unit_description(u),
395 prefix, unit_load_state_to_string(u->meta.load_state),
396 prefix, unit_active_state_to_string(unit_active_state(u)),
397 prefix, yes_no(u->meta.recursive_stop),
398 prefix, yes_no(u->meta.stop_when_unneeded));
400 if (u->meta.load_path)
401 fprintf(f, "%s\tLoad Path: %s\n", prefix, u->meta.load_path);
403 SET_FOREACH(t, u->meta.names, i)
404 fprintf(f, "%s\tName: %s\n", prefix, t);
406 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
409 if (set_isempty(u->meta.dependencies[d]))
412 SET_FOREACH(other, u->meta.dependencies[d], i)
413 fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), unit_id(other));
416 if (UNIT_VTABLE(u)->dump)
417 UNIT_VTABLE(u)->dump(u, f, prefix2);
420 job_dump(u->meta.job, f, prefix2);
425 /* Common implementation for multiple backends */
426 int unit_load_fragment_and_dropin(Unit *u) {
431 /* Load a .socket file */
432 if ((r = unit_load_fragment(u)) < 0)
437 /* Load drop-in directory data */
438 if ((r = unit_load_dropin(u)) < 0)
444 int unit_load(Unit *u) {
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;
454 if (u->meta.load_state != UNIT_STUB)
457 if (UNIT_VTABLE(u)->init)
458 if ((r = UNIT_VTABLE(u)->init(u)) < 0)
461 u->meta.load_state = UNIT_LOADED;
462 unit_add_to_dbus_queue(u);
466 u->meta.load_state = UNIT_FAILED;
467 unit_add_to_dbus_queue(u);
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.
476 int unit_start(Unit *u) {
477 UnitActiveState state;
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. */
485 state = unit_active_state(u);
486 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
489 /* If it is stopped, but we cannot start it, then fail */
490 if (!UNIT_VTABLE(u)->start)
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. */
499 unit_add_to_dbus_queue(u);
500 return UNIT_VTABLE(u)->start(u);
503 bool unit_can_start(Unit *u) {
506 return !!UNIT_VTABLE(u)->start;
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.
514 int unit_stop(Unit *u) {
515 UnitActiveState state;
519 state = unit_active_state(u);
520 if (state == UNIT_INACTIVE)
523 if (!UNIT_VTABLE(u)->stop)
526 if (state == UNIT_DEACTIVATING)
529 unit_add_to_dbus_queue(u);
530 return UNIT_VTABLE(u)->stop(u);
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.
538 int unit_reload(Unit *u) {
539 UnitActiveState state;
543 if (!unit_can_reload(u))
546 state = unit_active_state(u);
547 if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
550 if (unit_active_state(u) != UNIT_ACTIVE)
553 unit_add_to_dbus_queue(u);
554 return UNIT_VTABLE(u)->reload(u);
557 bool unit_can_reload(Unit *u) {
560 if (!UNIT_VTABLE(u)->reload)
563 if (!UNIT_VTABLE(u)->can_reload)
566 return UNIT_VTABLE(u)->can_reload(u);
569 static void unit_check_uneeded(Unit *u) {
575 /* If this service shall be shut down when unneeded then do
578 if (!u->meta.stop_when_unneeded)
581 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
584 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
585 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
588 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
589 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
592 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
593 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
596 log_debug("Service %s is not needed anymore. Stopping.", unit_id(u));
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);
602 static void retroactively_start_dependencies(Unit *u) {
607 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
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);
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);
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);
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);
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);
630 static void retroactively_stop_dependencies(Unit *u) {
635 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
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);
644 /* Garbage collect services that might not be needed anymore, if enabled */
645 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
646 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
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);
662 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
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));
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);
679 if (u->meta.job->state == JOB_WAITING)
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. */
684 job_add_to_run_queue(u->meta.job);
687 assert(u->meta.job->state == JOB_RUNNING);
689 /* Let's check whether this state change
690 * constitutes a finished job, or maybe
691 * cotradicts a running job and hence needs to
692 * invalidate jobs. */
694 switch (u->meta.job->type) {
697 case JOB_VERIFY_ACTIVE:
699 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
700 job_finish_and_invalidate(u->meta.job, true);
702 } else if (ns == UNIT_ACTIVATING)
705 job_finish_and_invalidate(u->meta.job, false);
710 case JOB_RELOAD_OR_START:
712 if (ns == UNIT_ACTIVE) {
713 job_finish_and_invalidate(u->meta.job, true);
715 } else if (ns == UNIT_ACTIVATING || ns == UNIT_ACTIVE_RELOADING)
718 job_finish_and_invalidate(u->meta.job, false);
724 case JOB_TRY_RESTART:
726 if (ns == UNIT_INACTIVE) {
727 job_finish_and_invalidate(u->meta.job, true);
729 } else if (ns == UNIT_DEACTIVATING)
732 job_finish_and_invalidate(u->meta.job, false);
737 assert_not_reached("Job type unknown");
742 /* If this state change happened without being requested by a
743 * job, then let's retroactively start or stop dependencies */
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);
750 /* Maybe we finished startup and are now ready for being
751 * stopped because unneeded? */
752 unit_check_uneeded(u);
754 unit_add_to_dbus_queue(u);
757 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
758 struct epoll_event ev;
763 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
769 if (epoll_ctl(u->meta.manager->epoll_fd,
770 w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
782 void unit_unwatch_fd(Unit *u, Watch *w) {
786 if (w->type == WATCH_INVALID)
789 assert(w->type == WATCH_FD);
790 assert(w->data.unit == u);
791 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
794 w->type = WATCH_INVALID;
798 int unit_watch_pid(Unit *u, pid_t pid) {
802 return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
805 void unit_unwatch_pid(Unit *u, pid_t pid) {
809 hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
812 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
813 struct itimerspec its;
819 assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->data.unit == u));
821 /* This will try to reuse the old timer if there is one */
823 if (w->type == WATCH_TIMER) {
828 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 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;
840 flags = TFD_TIMER_ABSTIME;
842 timespec_store(&its.it_value, delay);
846 /* This will also flush the elapse counter */
847 if (timerfd_settime(fd, flags, &its, NULL) < 0)
850 if (w->type == WATCH_INVALID) {
851 struct epoll_event ev;
857 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
862 w->type = WATCH_TIMER;
869 close_nointr_nofail(fd);
874 void unit_unwatch_timer(Unit *u, Watch *w) {
878 if (w->type == WATCH_INVALID)
881 assert(w->type == WATCH_TIMER && w->data.unit == u);
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);
887 w->type = WATCH_INVALID;
891 bool unit_job_is_applicable(Unit *u, JobType j) {
893 assert(j >= 0 && j < _JOB_TYPE_MAX);
897 case JOB_VERIFY_ACTIVE:
903 case JOB_TRY_RESTART:
904 return unit_can_start(u);
907 return unit_can_reload(u);
909 case JOB_RELOAD_OR_START:
910 return unit_can_reload(u) && unit_can_start(u);
913 assert_not_reached("Invalid job type");
917 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {
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
935 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
936 assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
939 /* We won't allow dependencies on ourselves. We will not
940 * consider them an error however. */
944 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
947 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
950 if ((r = set_put(u->meta.dependencies[d], other)) < 0)
953 if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
954 set_remove(u->meta.dependencies[d], other);
958 unit_add_to_dbus_queue(u);
962 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) {
966 if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
969 if ((r = unit_add_dependency(u, d, other)) < 0)
975 const char *unit_path(void) {
978 if ((e = getenv("UNIT_PATH")))
979 if (path_is_absolute(e))
985 int set_unit_path(const char *p) {
989 /* This is mostly for debug purposes */
991 if (path_is_absolute(p)) {
992 if (!(c = strdup(p)))
995 if (!(cwd = get_current_dir_name()))
998 r = asprintf(&c, "%s/%s", cwd, p);
1005 if (setenv("UNIT_PATH", c, 0) < 0) {
1014 char *unit_name_escape_path(const char *prefix, const char *path, const char *suffix) {
1021 /* Takes a path and a suffix and prefix and makes a nice
1022 * string suitable as unit name of it, escaping all weird
1025 * / becomes ., and all chars not alloweed in a unit name get
1026 * escaped as \xFF, including \ and ., of course. This
1027 * escaping is hence reversible.
1040 if (!(r = new(char, a+b*4+c+1)))
1043 memcpy(r, prefix, a);
1045 for (f = path, t = r+a; *f; f++) {
1048 else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
1051 *(t++) = hexchar(*f > 4);
1052 *(t++) = hexchar(*f);
1057 memcpy(t, suffix, c+1);
1062 char *unit_dbus_path(Unit *u) {
1067 if (!(e = bus_path_escape(unit_id(u))))
1070 if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) {
1079 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
1080 [UNIT_SERVICE] = "service",
1081 [UNIT_TIMER] = "timer",
1082 [UNIT_SOCKET] = "socket",
1083 [UNIT_TARGET] = "target",
1084 [UNIT_DEVICE] = "device",
1085 [UNIT_MOUNT] = "mount",
1086 [UNIT_AUTOMOUNT] = "automount",
1087 [UNIT_SNAPSHOT] = "snapshot"
1090 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
1092 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
1093 [UNIT_STUB] = "stub",
1094 [UNIT_LOADED] = "loaded",
1095 [UNIT_FAILED] = "failed"
1098 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
1100 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
1101 [UNIT_ACTIVE] = "active",
1102 [UNIT_INACTIVE] = "inactive",
1103 [UNIT_ACTIVATING] = "activating",
1104 [UNIT_DEACTIVATING] = "deactivating"
1107 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
1109 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
1110 [UNIT_REQUIRES] = "Requires",
1111 [UNIT_SOFT_REQUIRES] = "SoftRequires",
1112 [UNIT_WANTS] = "Wants",
1113 [UNIT_REQUISITE] = "Requisite",
1114 [UNIT_SOFT_REQUISITE] = "SoftRequisite",
1115 [UNIT_REQUIRED_BY] = "RequiredBy",
1116 [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
1117 [UNIT_WANTED_BY] = "WantedBy",
1118 [UNIT_CONFLICTS] = "Conflicts",
1119 [UNIT_BEFORE] = "Before",
1120 [UNIT_AFTER] = "After",
1123 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);