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 cgroup_bonding_free_list(u->meta.cgroup_bondings);
265 SET_FOREACH(t, u->meta.names, i)
266 hashmap_remove_value(u->meta.manager->units, t, u);
268 if (u->meta.type != _UNIT_TYPE_INVALID)
269 LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta);
271 if (u->meta.in_load_queue)
272 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
274 if (u->meta.in_dbus_queue)
275 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
277 if (u->meta.load_state == UNIT_LOADED)
278 if (UNIT_VTABLE(u)->done)
279 UNIT_VTABLE(u)->done(u);
281 /* Free data and next 'smaller' objects */
283 job_free(u->meta.job);
285 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
286 bidi_set_free(u, u->meta.dependencies[d]);
288 free(u->meta.description);
289 free(u->meta.fragment_path);
291 while ((t = set_steal_first(u->meta.names)))
293 set_free(u->meta.names);
298 UnitActiveState unit_active_state(Unit *u) {
301 if (u->meta.load_state != UNIT_LOADED)
302 return UNIT_INACTIVE;
304 return UNIT_VTABLE(u)->active_state(u);
307 static int ensure_merge(Set **s, Set *other) {
313 return set_merge(*s, other);
315 if (!(*s = set_copy(other)))
321 /* FIXME: Does not rollback on failure! Needs to fix special unit
322 * pointers. Needs to merge names and dependencies properly.*/
323 int unit_merge(Unit *u, Unit *other) {
329 assert(u->meta.manager == other->meta.manager);
331 /* This merges 'other' into 'unit'. FIXME: This does not
332 * rollback on failure. */
334 if (u->meta.type != u->meta.type)
337 if (u->meta.load_state != UNIT_STUB)
341 if ((r = ensure_merge(&u->meta.names, other->meta.names)) < 0)
344 /* Merge dependencies */
345 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
346 /* fixme, the inverse mapping is missing */
347 if ((r = ensure_merge(&u->meta.dependencies[d], other->meta.dependencies[d])) < 0)
350 unit_add_to_dbus_queue(u);
355 const char* unit_id(Unit *u) {
361 return set_first(u->meta.names);
364 const char *unit_description(Unit *u) {
367 if (u->meta.description)
368 return u->meta.description;
373 void unit_dump(Unit *u, FILE *f, const char *prefix) {
385 p2 = strappend(prefix, "\t");
386 prefix2 = p2 ? p2 : prefix;
390 "%s\tDescription: %s\n"
391 "%s\tUnit Load State: %s\n"
392 "%s\tUnit Active State: %s\n"
393 "%s\tRecursive Stop: %s\n"
394 "%s\tStop When Unneeded: %s\n",
396 prefix, unit_description(u),
397 prefix, unit_load_state_to_string(u->meta.load_state),
398 prefix, unit_active_state_to_string(unit_active_state(u)),
399 prefix, yes_no(u->meta.recursive_stop),
400 prefix, yes_no(u->meta.stop_when_unneeded));
402 if (u->meta.fragment_path)
403 fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);
405 SET_FOREACH(t, u->meta.names, i)
406 fprintf(f, "%s\tName: %s\n", prefix, t);
408 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
411 if (set_isempty(u->meta.dependencies[d]))
414 SET_FOREACH(other, u->meta.dependencies[d], i)
415 fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), unit_id(other));
418 LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
419 fprintf(f, "%s\tControlGroup: %s:%s\n",
420 prefix, b->controller, b->path);
422 if (UNIT_VTABLE(u)->dump)
423 UNIT_VTABLE(u)->dump(u, f, prefix2);
426 job_dump(u->meta.job, f, prefix2);
431 /* Common implementation for multiple backends */
432 int unit_load_fragment_and_dropin(Unit *u) {
437 /* Load a .socket file */
438 if ((r = unit_load_fragment(u)) < 0)
443 /* Load drop-in directory data */
444 if ((r = unit_load_dropin(u)) < 0)
450 int unit_load(Unit *u) {
455 if (u->meta.in_load_queue) {
456 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
457 u->meta.in_load_queue = false;
460 if (u->meta.load_state != UNIT_STUB)
463 if (UNIT_VTABLE(u)->init)
464 if ((r = UNIT_VTABLE(u)->init(u)) < 0)
467 u->meta.load_state = UNIT_LOADED;
468 unit_add_to_dbus_queue(u);
472 u->meta.load_state = UNIT_FAILED;
473 unit_add_to_dbus_queue(u);
478 * -EBADR: This unit type does not support starting.
479 * -EALREADY: Unit is already started.
480 * -EAGAIN: An operation is already in progress. Retry later.
482 int unit_start(Unit *u) {
483 UnitActiveState state;
487 /* If this is already (being) started, then this will
488 * succeed. Note that this will even succeed if this unit is
489 * not startable by the user. This is relied on to detect when
490 * we need to wait for units and when waiting is finished. */
491 state = unit_active_state(u);
492 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
495 /* If it is stopped, but we cannot start it, then fail */
496 if (!UNIT_VTABLE(u)->start)
499 /* We don't suppress calls to ->start() here when we are
500 * already starting, to allow this request to be used as a
501 * "hurry up" call, for example when the unit is in some "auto
502 * restart" state where it waits for a holdoff timer to elapse
503 * before it will start again. */
505 unit_add_to_dbus_queue(u);
506 return UNIT_VTABLE(u)->start(u);
509 bool unit_can_start(Unit *u) {
512 return !!UNIT_VTABLE(u)->start;
516 * -EBADR: This unit type does not support stopping.
517 * -EALREADY: Unit is already stopped.
518 * -EAGAIN: An operation is already in progress. Retry later.
520 int unit_stop(Unit *u) {
521 UnitActiveState state;
525 state = unit_active_state(u);
526 if (state == UNIT_INACTIVE)
529 if (!UNIT_VTABLE(u)->stop)
532 if (state == UNIT_DEACTIVATING)
535 unit_add_to_dbus_queue(u);
536 return UNIT_VTABLE(u)->stop(u);
540 * -EBADR: This unit type does not support reloading.
541 * -ENOEXEC: Unit is not started.
542 * -EAGAIN: An operation is already in progress. Retry later.
544 int unit_reload(Unit *u) {
545 UnitActiveState state;
549 if (!unit_can_reload(u))
552 state = unit_active_state(u);
553 if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
556 if (unit_active_state(u) != UNIT_ACTIVE)
559 unit_add_to_dbus_queue(u);
560 return UNIT_VTABLE(u)->reload(u);
563 bool unit_can_reload(Unit *u) {
566 if (!UNIT_VTABLE(u)->reload)
569 if (!UNIT_VTABLE(u)->can_reload)
572 return UNIT_VTABLE(u)->can_reload(u);
575 static void unit_check_uneeded(Unit *u) {
581 /* If this service shall be shut down when unneeded then do
584 if (!u->meta.stop_when_unneeded)
587 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
590 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
591 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
594 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
595 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
598 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
599 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
602 log_debug("Service %s is not needed anymore. Stopping.", unit_id(u));
604 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
605 manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL);
608 static void retroactively_start_dependencies(Unit *u) {
613 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
615 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
616 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
617 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
619 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
620 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
621 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
623 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
624 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
625 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
627 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
628 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
629 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
631 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
632 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
633 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
636 static void retroactively_stop_dependencies(Unit *u) {
641 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
643 if (u->meta.recursive_stop) {
644 /* Pull down units need us recursively if enabled */
645 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
646 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
647 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
650 /* Garbage collect services that might not be needed anymore, if enabled */
651 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], 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_SOFT_REQUIRES], 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_WANTS], i)
658 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
659 unit_check_uneeded(other);
660 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
661 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
662 unit_check_uneeded(other);
663 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUISITE], i)
664 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
665 unit_check_uneeded(other);
668 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
670 assert(os < _UNIT_ACTIVE_STATE_MAX);
671 assert(ns < _UNIT_ACTIVE_STATE_MAX);
672 assert(!(os == UNIT_ACTIVE && ns == UNIT_ACTIVATING));
673 assert(!(os == UNIT_INACTIVE && ns == UNIT_DEACTIVATING));
678 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
679 u->meta.active_enter_timestamp = now(CLOCK_REALTIME);
680 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
681 u->meta.active_exit_timestamp = now(CLOCK_REALTIME);
685 if (u->meta.job->state == JOB_WAITING)
687 /* So we reached a different state for this
688 * job. Let's see if we can run it now if it
689 * failed previously due to EAGAIN. */
690 job_add_to_run_queue(u->meta.job);
693 assert(u->meta.job->state == JOB_RUNNING);
695 /* Let's check whether this state change
696 * constitutes a finished job, or maybe
697 * cotradicts a running job and hence needs to
698 * invalidate jobs. */
700 switch (u->meta.job->type) {
703 case JOB_VERIFY_ACTIVE:
705 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
706 job_finish_and_invalidate(u->meta.job, true);
708 } else if (ns == UNIT_ACTIVATING)
711 job_finish_and_invalidate(u->meta.job, false);
716 case JOB_RELOAD_OR_START:
718 if (ns == UNIT_ACTIVE) {
719 job_finish_and_invalidate(u->meta.job, true);
721 } else if (ns == UNIT_ACTIVATING || ns == UNIT_ACTIVE_RELOADING)
724 job_finish_and_invalidate(u->meta.job, false);
730 case JOB_TRY_RESTART:
732 if (ns == UNIT_INACTIVE) {
733 job_finish_and_invalidate(u->meta.job, true);
735 } else if (ns == UNIT_DEACTIVATING)
738 job_finish_and_invalidate(u->meta.job, false);
743 assert_not_reached("Job type unknown");
748 /* If this state change happened without being requested by a
749 * job, then let's retroactively start or stop dependencies */
751 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
752 retroactively_start_dependencies(u);
753 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
754 retroactively_stop_dependencies(u);
756 /* Maybe we finished startup and are now ready for being
757 * stopped because unneeded? */
758 unit_check_uneeded(u);
760 unit_add_to_dbus_queue(u);
763 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
764 struct epoll_event ev;
769 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
775 if (epoll_ctl(u->meta.manager->epoll_fd,
776 w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
788 void unit_unwatch_fd(Unit *u, Watch *w) {
792 if (w->type == WATCH_INVALID)
795 assert(w->type == WATCH_FD);
796 assert(w->data.unit == u);
797 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
800 w->type = WATCH_INVALID;
804 int unit_watch_pid(Unit *u, pid_t pid) {
808 return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
811 void unit_unwatch_pid(Unit *u, pid_t pid) {
815 hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
818 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
819 struct itimerspec its;
825 assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->data.unit == u));
827 /* This will try to reuse the old timer if there is one */
829 if (w->type == WATCH_TIMER) {
834 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
841 /* Set absolute time in the past, but not 0, since we
842 * don't want to disarm the timer */
843 its.it_value.tv_sec = 0;
844 its.it_value.tv_nsec = 1;
846 flags = TFD_TIMER_ABSTIME;
848 timespec_store(&its.it_value, delay);
852 /* This will also flush the elapse counter */
853 if (timerfd_settime(fd, flags, &its, NULL) < 0)
856 if (w->type == WATCH_INVALID) {
857 struct epoll_event ev;
863 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
868 w->type = WATCH_TIMER;
875 close_nointr_nofail(fd);
880 void unit_unwatch_timer(Unit *u, Watch *w) {
884 if (w->type == WATCH_INVALID)
887 assert(w->type == WATCH_TIMER && w->data.unit == u);
889 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
890 assert_se(close_nointr(w->fd) == 0);
893 w->type = WATCH_INVALID;
897 bool unit_job_is_applicable(Unit *u, JobType j) {
899 assert(j >= 0 && j < _JOB_TYPE_MAX);
903 case JOB_VERIFY_ACTIVE:
909 case JOB_TRY_RESTART:
910 return unit_can_start(u);
913 return unit_can_reload(u);
915 case JOB_RELOAD_OR_START:
916 return unit_can_reload(u) && unit_can_start(u);
919 assert_not_reached("Invalid job type");
923 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {
925 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
926 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
927 [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY,
928 [UNIT_WANTS] = UNIT_WANTED_BY,
929 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
930 [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY,
931 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
932 [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
933 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
934 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
935 [UNIT_BEFORE] = UNIT_AFTER,
936 [UNIT_AFTER] = UNIT_BEFORE
941 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
942 assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
945 /* We won't allow dependencies on ourselves. We will not
946 * consider them an error however. */
950 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
953 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
956 if ((r = set_put(u->meta.dependencies[d], other)) < 0)
959 if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
960 set_remove(u->meta.dependencies[d], other);
964 unit_add_to_dbus_queue(u);
968 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) {
972 if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
975 if ((r = unit_add_dependency(u, d, other)) < 0)
981 int set_unit_path(const char *p) {
985 /* This is mostly for debug purposes */
987 if (path_is_absolute(p)) {
988 if (!(c = strdup(p)))
991 if (!(cwd = get_current_dir_name()))
994 r = asprintf(&c, "%s/%s", cwd, p);
1001 if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
1010 char *unit_name_escape_path(const char *path, const char *suffix) {
1017 /* Takes a path and a suffix and prefix and makes a nice
1018 * string suitable as unit name of it, escaping all weird
1021 * / becomes ., and all chars not alloweed in a unit name get
1022 * escaped as \xFF, including \ and ., of course. This
1023 * escaping is hence reversible.
1032 if (!(r = new(char, a*4+b+1)))
1035 for (f = path, t = r; *f; f++) {
1038 else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
1041 *(t++) = hexchar(*f > 4);
1042 *(t++) = hexchar(*f);
1047 memcpy(t, suffix, b+1);
1052 char *unit_dbus_path(Unit *u) {
1057 if (!(e = bus_path_escape(unit_id(u))))
1060 if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) {
1069 int unit_add_cgroup(Unit *u, CGroupBonding *b) {
1077 /* Ensure this hasn't been added yet */
1080 l = hashmap_get(u->meta.manager->cgroup_bondings, b->path);
1081 LIST_PREPEND(CGroupBonding, by_path, l, b);
1083 if ((r = hashmap_replace(u->meta.manager->cgroup_bondings, b->path, l)) < 0) {
1084 LIST_REMOVE(CGroupBonding, by_path, l, b);
1088 LIST_PREPEND(CGroupBonding, by_unit, u->meta.cgroup_bondings, b);
1094 int unit_add_cgroup_from_text(Unit *u, const char *name) {
1104 /* Detect controller name */
1105 n = strcspn(name, ":/");
1107 /* Only controller name, no path? No path? */
1119 /* Insist in absolute paths */
1123 if (!(controller = strndup(name, n)))
1126 if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
1131 if (!(b = new0(CGroupBonding, 1))) {
1136 b->controller = controller;
1138 if (!(b->path = strdup(p))) {
1144 b->clean_up = false;
1146 if ((r = unit_add_cgroup(u, b)) < 0)
1153 free(b->controller);
1159 int unit_add_default_cgroup(Unit *u) {
1165 /* Adds in the default cgroup data, if it wasn't specified yet */
1167 if (unit_get_default_cgroup(u))
1170 if (!(b = new0(CGroupBonding, 1)))
1173 if (!(b->controller = strdup(u->meta.manager->cgroup_controller)))
1176 if (asprintf(&b->path, "%s/%s", u->meta.manager->cgroup_hierarchy, unit_id(u)) < 0)
1182 if ((r = unit_add_cgroup(u, b)) < 0)
1189 free(b->controller);
1195 CGroupBonding* unit_get_default_cgroup(Unit *u) {
1198 return cgroup_bonding_find_list(u->meta.cgroup_bondings, u->meta.manager->cgroup_controller);
1201 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
1202 [UNIT_SERVICE] = "service",
1203 [UNIT_TIMER] = "timer",
1204 [UNIT_SOCKET] = "socket",
1205 [UNIT_TARGET] = "target",
1206 [UNIT_DEVICE] = "device",
1207 [UNIT_MOUNT] = "mount",
1208 [UNIT_AUTOMOUNT] = "automount",
1209 [UNIT_SNAPSHOT] = "snapshot"
1212 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
1214 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
1215 [UNIT_STUB] = "stub",
1216 [UNIT_LOADED] = "loaded",
1217 [UNIT_FAILED] = "failed"
1220 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
1222 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
1223 [UNIT_ACTIVE] = "active",
1224 [UNIT_INACTIVE] = "inactive",
1225 [UNIT_ACTIVATING] = "activating",
1226 [UNIT_DEACTIVATING] = "deactivating"
1229 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
1231 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
1232 [UNIT_REQUIRES] = "Requires",
1233 [UNIT_SOFT_REQUIRES] = "SoftRequires",
1234 [UNIT_WANTS] = "Wants",
1235 [UNIT_REQUISITE] = "Requisite",
1236 [UNIT_SOFT_REQUISITE] = "SoftRequisite",
1237 [UNIT_REQUIRED_BY] = "RequiredBy",
1238 [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
1239 [UNIT_WANTED_BY] = "WantedBy",
1240 [UNIT_CONFLICTS] = "Conflicts",
1241 [UNIT_BEFORE] = "Before",
1242 [UNIT_AFTER] = "After",
1245 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);