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 bool unit_has_name(Unit *u, const char *name) {
138 return !!set_get(u->meta.names, (char*) name);
141 int unit_add_name(Unit *u, const char *text) {
149 if (!unit_name_is_valid(text))
152 if ((t = unit_name_to_type(text)) == _UNIT_TYPE_INVALID)
155 if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type)
158 if (u->meta.type != _UNIT_TYPE_INVALID &&
159 UNIT_VTABLE(u)->no_alias &&
160 !set_isempty(u->meta.names))
163 if (!(s = strdup(text)))
166 if ((r = set_put(u->meta.names, s)) < 0) {
175 if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
176 set_remove(u->meta.names, s);
181 if (u->meta.type == _UNIT_TYPE_INVALID) {
182 LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);
186 if (UNIT_VTABLE(u)->init)
187 UNIT_VTABLE(u)->init(u);
193 unit_add_to_dbus_queue(u);
197 int unit_choose_id(Unit *u, const char *name) {
203 /* Selects one of the names of this unit as the id */
205 if (!(s = set_get(u->meta.names, (char*) name)))
210 unit_add_to_dbus_queue(u);
214 int unit_set_description(Unit *u, const char *description) {
219 if (!(s = strdup(description)))
222 free(u->meta.description);
223 u->meta.description = s;
225 unit_add_to_dbus_queue(u);
229 void unit_add_to_load_queue(Unit *u) {
231 assert(u->meta.type != _UNIT_TYPE_INVALID);
233 if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
236 LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
237 u->meta.in_load_queue = true;
240 void unit_add_to_cleanup_queue(Unit *u) {
243 if (u->meta.in_cleanup_queue)
246 LIST_PREPEND(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
247 u->meta.in_cleanup_queue = true;
250 void unit_add_to_dbus_queue(Unit *u) {
252 assert(u->meta.type != _UNIT_TYPE_INVALID);
254 if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue || set_isempty(u->meta.manager->subscribed))
257 LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
258 u->meta.in_dbus_queue = true;
261 static void bidi_set_free(Unit *u, Set *s) {
267 /* Frees the set and makes sure we are dropped from the
268 * inverse pointers */
270 SET_FOREACH(other, s, i) {
273 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
274 set_remove(other->meta.dependencies[d], u);
280 void unit_free(Unit *u) {
287 bus_unit_send_removed_signal(u);
289 /* Detach from next 'bigger' objects */
290 SET_FOREACH(t, u->meta.names, i)
291 hashmap_remove_value(u->meta.manager->units, t, u);
293 if (u->meta.type != _UNIT_TYPE_INVALID)
294 LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta);
296 if (u->meta.in_load_queue)
297 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
299 if (u->meta.in_dbus_queue)
300 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
302 if (u->meta.in_cleanup_queue)
303 LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
305 /* Free data and next 'smaller' objects */
307 job_free(u->meta.job);
309 if (u->meta.load_state != UNIT_STUB)
310 if (UNIT_VTABLE(u)->done)
311 UNIT_VTABLE(u)->done(u);
313 cgroup_bonding_free_list(u->meta.cgroup_bondings);
315 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
316 bidi_set_free(u, u->meta.dependencies[d]);
318 free(u->meta.description);
319 free(u->meta.fragment_path);
321 while ((t = set_steal_first(u->meta.names)))
323 set_free(u->meta.names);
328 UnitActiveState unit_active_state(Unit *u) {
331 if (u->meta.load_state != UNIT_LOADED)
332 return UNIT_INACTIVE;
334 return UNIT_VTABLE(u)->active_state(u);
337 static void complete_move(Set **s, Set **other) {
345 set_move(*s, *other);
352 static void merge_names(Unit *u, Unit *other) {
359 complete_move(&u->meta.names, &other->meta.names);
361 while ((t = set_steal_first(other->meta.names)))
364 set_free(other->meta.names);
365 other->meta.names = NULL;
366 other->meta.id = NULL;
368 SET_FOREACH(t, u->meta.names, i)
369 assert_se(hashmap_replace(u->meta.manager->units, t, u) == 0);
372 static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
379 assert(d < _UNIT_DEPENDENCY_MAX);
381 SET_FOREACH(back, other->meta.dependencies[d], i) {
384 for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++)
385 if ((r = set_remove_and_put(back->meta.dependencies[k], other, u)) < 0) {
388 set_remove(back->meta.dependencies[k], other);
390 assert(r == -ENOENT);
394 complete_move(&u->meta.dependencies[d], &other->meta.dependencies[d]);
396 set_free(other->meta.dependencies[d]);
397 other->meta.dependencies[d] = NULL;
400 int unit_merge(Unit *u, Unit *other) {
405 assert(u->meta.manager == other->meta.manager);
407 other = unit_follow_merge(other);
412 if (u->meta.type != u->meta.type)
415 if (other->meta.load_state != UNIT_STUB &&
416 other->meta.load_state != UNIT_FAILED)
422 if (unit_active_state(other) != UNIT_INACTIVE)
426 merge_names(u, other);
428 /* Merge dependencies */
429 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
430 merge_dependencies(u, other, d);
432 other->meta.load_state = UNIT_MERGED;
433 other->meta.merged_into = u;
435 /* If there is still some data attached to the other node, we
436 * don't need it anymore, and can free it. */
437 if (other->meta.load_state != UNIT_STUB)
438 if (UNIT_VTABLE(other)->done)
439 UNIT_VTABLE(other)->done(other);
441 unit_add_to_dbus_queue(u);
442 unit_add_to_cleanup_queue(other);
447 int unit_merge_by_name(Unit *u, const char *name) {
453 if (!(other = manager_get_unit(u->meta.manager, name)))
454 return unit_add_name(u, name);
456 return unit_merge(u, other);
459 Unit* unit_follow_merge(Unit *u) {
462 while (u->meta.load_state == UNIT_MERGED)
463 assert_se(u = u->meta.merged_into);
468 int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
474 if (c->std_output != EXEC_OUTPUT_KERNEL && c->std_output != EXEC_OUTPUT_SYSLOG)
477 /* If syslog or kernel logging is requested, make sure our own
478 * logging daemon is run first. */
480 if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET)) < 0)
483 if (u->meta.manager->running_as != MANAGER_SESSION)
484 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET)) < 0)
490 const char* unit_id(Unit *u) {
496 return set_first(u->meta.names);
499 const char *unit_description(Unit *u) {
502 if (u->meta.description)
503 return u->meta.description;
508 void unit_dump(Unit *u, FILE *f, const char *prefix) {
515 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
521 p2 = strappend(prefix, "\t");
522 prefix2 = p2 ? p2 : prefix;
526 "%s\tDescription: %s\n"
527 "%s\tUnit Load State: %s\n"
528 "%s\tUnit Active State: %s\n"
529 "%s\tActive Enter Timestamp: %s\n"
530 "%s\tActive Exit Timestamp: %s\n",
532 prefix, unit_description(u),
533 prefix, unit_load_state_to_string(u->meta.load_state),
534 prefix, unit_active_state_to_string(unit_active_state(u)),
535 prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.active_enter_timestamp)),
536 prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->meta.active_exit_timestamp)));
538 SET_FOREACH(t, u->meta.names, i)
539 fprintf(f, "%s\tName: %s\n", prefix, t);
541 if (u->meta.fragment_path)
542 fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);
544 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
547 SET_FOREACH(other, u->meta.dependencies[d], i)
548 fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), unit_id(other));
551 if (u->meta.load_state == UNIT_LOADED) {
553 "%s\tRecursive Stop: %s\n"
554 "%s\tStop When Unneeded: %s\n",
555 prefix, yes_no(u->meta.recursive_stop),
556 prefix, yes_no(u->meta.stop_when_unneeded));
558 LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
559 fprintf(f, "%s\tControlGroup: %s:%s\n",
560 prefix, b->controller, b->path);
562 if (UNIT_VTABLE(u)->dump)
563 UNIT_VTABLE(u)->dump(u, f, prefix2);
565 } else if (u->meta.load_state == UNIT_MERGED)
567 "%s\tMerged into: %s\n",
568 prefix, unit_id(u->meta.merged_into));
571 job_dump(u->meta.job, f, prefix2);
576 /* Common implementation for multiple backends */
577 int unit_load_fragment_and_dropin(Unit *u) {
582 /* Load a .service file */
583 if ((r = unit_load_fragment(u)) < 0)
586 if (u->meta.load_state == UNIT_STUB)
589 /* Load drop-in directory data */
590 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
596 /* Common implementation for multiple backends */
597 int unit_load_fragment_and_dropin_optional(Unit *u) {
602 /* Same as unit_load_fragment_and_dropin(), but whether
603 * something can be loaded or not doesn't matter. */
605 /* Load a .service file */
606 if ((r = unit_load_fragment(u)) < 0)
609 if (u->meta.load_state == UNIT_STUB)
610 u->meta.load_state = UNIT_LOADED;
612 /* Load drop-in directory data */
613 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
619 int unit_load(Unit *u) {
624 if (u->meta.in_load_queue) {
625 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
626 u->meta.in_load_queue = false;
629 if (u->meta.type == _UNIT_TYPE_INVALID)
632 if (u->meta.load_state != UNIT_STUB)
635 if (UNIT_VTABLE(u)->load)
636 if ((r = UNIT_VTABLE(u)->load(u)) < 0)
639 if (u->meta.load_state == UNIT_STUB) {
644 assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);
646 unit_add_to_dbus_queue(unit_follow_merge(u));
651 u->meta.load_state = UNIT_FAILED;
652 unit_add_to_dbus_queue(u);
654 log_error("Failed to load configuration for %s: %s", unit_id(u), strerror(-r));
660 * -EBADR: This unit type does not support starting.
661 * -EALREADY: Unit is already started.
662 * -EAGAIN: An operation is already in progress. Retry later.
664 int unit_start(Unit *u) {
665 UnitActiveState state;
669 /* If this is already (being) started, then this will
670 * succeed. Note that this will even succeed if this unit is
671 * not startable by the user. This is relied on to detect when
672 * we need to wait for units and when waiting is finished. */
673 state = unit_active_state(u);
674 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
677 /* If it is stopped, but we cannot start it, then fail */
678 if (!UNIT_VTABLE(u)->start)
681 /* We don't suppress calls to ->start() here when we are
682 * already starting, to allow this request to be used as a
683 * "hurry up" call, for example when the unit is in some "auto
684 * restart" state where it waits for a holdoff timer to elapse
685 * before it will start again. */
687 unit_add_to_dbus_queue(u);
688 return UNIT_VTABLE(u)->start(u);
691 bool unit_can_start(Unit *u) {
694 return !!UNIT_VTABLE(u)->start;
698 * -EBADR: This unit type does not support stopping.
699 * -EALREADY: Unit is already stopped.
700 * -EAGAIN: An operation is already in progress. Retry later.
702 int unit_stop(Unit *u) {
703 UnitActiveState state;
707 state = unit_active_state(u);
708 if (state == UNIT_INACTIVE)
711 if (!UNIT_VTABLE(u)->stop)
714 unit_add_to_dbus_queue(u);
715 return UNIT_VTABLE(u)->stop(u);
719 * -EBADR: This unit type does not support reloading.
720 * -ENOEXEC: Unit is not started.
721 * -EAGAIN: An operation is already in progress. Retry later.
723 int unit_reload(Unit *u) {
724 UnitActiveState state;
728 if (!unit_can_reload(u))
731 state = unit_active_state(u);
732 if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
735 if (unit_active_state(u) != UNIT_ACTIVE)
738 unit_add_to_dbus_queue(u);
739 return UNIT_VTABLE(u)->reload(u);
742 bool unit_can_reload(Unit *u) {
745 if (!UNIT_VTABLE(u)->reload)
748 if (!UNIT_VTABLE(u)->can_reload)
751 return UNIT_VTABLE(u)->can_reload(u);
754 static void unit_check_uneeded(Unit *u) {
760 /* If this service shall be shut down when unneeded then do
763 if (!u->meta.stop_when_unneeded)
766 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
769 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
770 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
773 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
774 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
777 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
778 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
781 log_debug("Service %s is not needed anymore. Stopping.", unit_id(u));
783 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
784 manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL);
787 static void retroactively_start_dependencies(Unit *u) {
792 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
794 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
795 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
796 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
798 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
799 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
800 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
802 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
803 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
804 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
806 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
807 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
808 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
810 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
811 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
812 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
815 static void retroactively_stop_dependencies(Unit *u) {
820 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
822 if (u->meta.recursive_stop) {
823 /* Pull down units need us recursively if enabled */
824 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
825 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
826 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
829 /* Garbage collect services that might not be needed anymore, if enabled */
830 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
831 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
832 unit_check_uneeded(other);
833 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
834 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
835 unit_check_uneeded(other);
836 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
837 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
838 unit_check_uneeded(other);
839 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
840 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
841 unit_check_uneeded(other);
842 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUISITE], i)
843 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
844 unit_check_uneeded(other);
847 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
848 bool unexpected = false;
851 assert(os < _UNIT_ACTIVE_STATE_MAX);
852 assert(ns < _UNIT_ACTIVE_STATE_MAX);
853 assert(!(os == UNIT_ACTIVE && ns == UNIT_ACTIVATING));
854 assert(!(os == UNIT_INACTIVE && ns == UNIT_DEACTIVATING));
856 /* Note that this is called for all low-level state changes,
857 * even if they might map to the same high-level
858 * UnitActiveState! That means that ns == os is OK an expected
859 * behaviour here. For example: if a mount point is remounted
860 * this function will be called too and the utmp code below
863 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
864 u->meta.active_enter_timestamp = now(CLOCK_REALTIME);
865 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
866 u->meta.active_exit_timestamp = now(CLOCK_REALTIME);
870 if (u->meta.job->state == JOB_WAITING)
872 /* So we reached a different state for this
873 * job. Let's see if we can run it now if it
874 * failed previously due to EAGAIN. */
875 job_add_to_run_queue(u->meta.job);
878 assert(u->meta.job->state == JOB_RUNNING);
880 /* Let's check whether this state change
881 * constitutes a finished job, or maybe
882 * cotradicts a running job and hence needs to
883 * invalidate jobs. */
885 switch (u->meta.job->type) {
888 case JOB_VERIFY_ACTIVE:
890 if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
891 job_finish_and_invalidate(u->meta.job, true);
892 else if (ns != UNIT_ACTIVATING) {
894 job_finish_and_invalidate(u->meta.job, false);
900 case JOB_RELOAD_OR_START:
902 if (ns == UNIT_ACTIVE)
903 job_finish_and_invalidate(u->meta.job, true);
904 else if (ns != UNIT_ACTIVATING && ns != UNIT_ACTIVE_RELOADING) {
906 job_finish_and_invalidate(u->meta.job, false);
913 case JOB_TRY_RESTART:
915 if (ns == UNIT_INACTIVE)
916 job_finish_and_invalidate(u->meta.job, true);
917 else if (ns != UNIT_DEACTIVATING) {
919 job_finish_and_invalidate(u->meta.job, false);
925 assert_not_reached("Job type unknown");
930 /* If this state change happened without being requested by a
931 * job, then let's retroactively start or stop dependencies */
934 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
935 retroactively_start_dependencies(u);
936 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
937 retroactively_stop_dependencies(u);
940 /* Some names are special */
941 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
942 if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) {
943 /* The bus just might have become available,
944 * hence try to connect to it, if we aren't
946 bus_init_system(u->meta.manager);
947 bus_init_api(u->meta.manager);
950 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
951 /* The syslog daemon just might have become
952 * available, hence try to connect to it, if
953 * we aren't yet connected. */
956 if (u->meta.type == UNIT_MOUNT)
957 /* Another directory became available, let's
958 * check if that is enough to write our utmp
960 manager_write_utmp_reboot(u->meta.manager);
962 if (u->meta.type == UNIT_TARGET)
963 /* A target got activated, maybe this is a runlevel? */
964 manager_write_utmp_runlevel(u->meta.manager, u);
966 } else if (!UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
968 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
969 /* The syslog daemon might just have
970 * terminated, hence try to disconnect from
974 /* We don't care about D-Bus here, since we'll get an
975 * asynchronous notification for it anyway. */
978 /* Maybe we finished startup and are now ready for being
979 * stopped because unneeded? */
980 unit_check_uneeded(u);
982 unit_add_to_dbus_queue(u);
985 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
986 struct epoll_event ev;
991 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
997 if (epoll_ctl(u->meta.manager->epoll_fd,
998 w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
1010 void unit_unwatch_fd(Unit *u, Watch *w) {
1014 if (w->type == WATCH_INVALID)
1017 assert(w->type == WATCH_FD);
1018 assert(w->data.unit == u);
1019 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1022 w->type = WATCH_INVALID;
1023 w->data.unit = NULL;
1026 int unit_watch_pid(Unit *u, pid_t pid) {
1030 return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
1033 void unit_unwatch_pid(Unit *u, pid_t pid) {
1037 hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
1040 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
1041 struct itimerspec its;
1047 assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->data.unit == u));
1049 /* This will try to reuse the old timer if there is one */
1051 if (w->type == WATCH_TIMER) {
1056 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
1063 /* Set absolute time in the past, but not 0, since we
1064 * don't want to disarm the timer */
1065 its.it_value.tv_sec = 0;
1066 its.it_value.tv_nsec = 1;
1068 flags = TFD_TIMER_ABSTIME;
1070 timespec_store(&its.it_value, delay);
1074 /* This will also flush the elapse counter */
1075 if (timerfd_settime(fd, flags, &its, NULL) < 0)
1078 if (w->type == WATCH_INVALID) {
1079 struct epoll_event ev;
1083 ev.events = EPOLLIN;
1085 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
1090 w->type = WATCH_TIMER;
1097 close_nointr_nofail(fd);
1102 void unit_unwatch_timer(Unit *u, Watch *w) {
1106 if (w->type == WATCH_INVALID)
1109 assert(w->type == WATCH_TIMER && w->data.unit == u);
1111 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1112 assert_se(close_nointr(w->fd) == 0);
1115 w->type = WATCH_INVALID;
1116 w->data.unit = NULL;
1119 bool unit_job_is_applicable(Unit *u, JobType j) {
1121 assert(j >= 0 && j < _JOB_TYPE_MAX);
1125 case JOB_VERIFY_ACTIVE:
1131 case JOB_TRY_RESTART:
1132 return unit_can_start(u);
1135 return unit_can_reload(u);
1137 case JOB_RELOAD_OR_START:
1138 return unit_can_reload(u) && unit_can_start(u);
1141 assert_not_reached("Invalid job type");
1145 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {
1147 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
1148 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
1149 [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY,
1150 [UNIT_WANTS] = UNIT_WANTED_BY,
1151 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
1152 [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY,
1153 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1154 [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1155 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
1156 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
1157 [UNIT_BEFORE] = UNIT_AFTER,
1158 [UNIT_AFTER] = UNIT_BEFORE
1163 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
1164 assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
1167 /* We won't allow dependencies on ourselves. We will not
1168 * consider them an error however. */
1172 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
1175 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
1178 if ((r = set_put(u->meta.dependencies[d], other)) < 0)
1181 if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
1182 set_remove(u->meta.dependencies[d], other);
1186 unit_add_to_dbus_queue(u);
1190 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) {
1194 if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
1197 if ((r = unit_add_dependency(u, d, other)) < 0)
1203 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name) {
1207 if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
1210 if ((r = unit_add_dependency(other, d, u)) < 0)
1216 int set_unit_path(const char *p) {
1220 /* This is mostly for debug purposes */
1222 if (path_is_absolute(p)) {
1223 if (!(c = strdup(p)))
1226 if (!(cwd = get_current_dir_name()))
1229 r = asprintf(&c, "%s/%s", cwd, p);
1236 if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
1245 char *unit_name_escape_path(const char *path, const char *suffix) {
1252 /* Takes a path and a suffix and prefix and makes a nice
1253 * string suitable as unit name of it, escaping all weird
1256 * / becomes ., and all chars not alloweed in a unit name get
1257 * escaped as \xFF, including \ and ., of course. This
1258 * escaping is hence reversible.
1267 if (!(r = new(char, a*4+b+1)))
1270 for (f = path, t = r; *f; f++) {
1273 else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
1276 *(t++) = hexchar(*f > 4);
1277 *(t++) = hexchar(*f);
1282 memcpy(t, suffix, b+1);
1287 char *unit_dbus_path(Unit *u) {
1292 if (!(e = bus_path_escape(unit_id(u))))
1295 if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) {
1304 int unit_add_cgroup(Unit *u, CGroupBonding *b) {
1312 /* Ensure this hasn't been added yet */
1315 l = hashmap_get(u->meta.manager->cgroup_bondings, b->path);
1316 LIST_PREPEND(CGroupBonding, by_path, l, b);
1318 if ((r = hashmap_replace(u->meta.manager->cgroup_bondings, b->path, l)) < 0) {
1319 LIST_REMOVE(CGroupBonding, by_path, l, b);
1323 LIST_PREPEND(CGroupBonding, by_unit, u->meta.cgroup_bondings, b);
1329 static char *default_cgroup_path(Unit *u) {
1334 if (asprintf(&p, "%s/%s", u->meta.manager->cgroup_hierarchy, unit_id(u)) < 0)
1340 int unit_add_cgroup_from_text(Unit *u, const char *name) {
1342 char *controller = NULL, *path = NULL;
1343 CGroupBonding *b = NULL;
1349 /* Detect controller name */
1350 n = strcspn(name, ":");
1353 (name[n] == ':' && name[n+1] == 0)) {
1355 /* Only controller name, no path? */
1357 if (!(path = default_cgroup_path(u)))
1363 /* Controller name, and path. */
1366 if (!path_is_absolute(p))
1369 if (!(path = strdup(p)))
1374 controller = strndup(name, n);
1376 controller = strdup(u->meta.manager->cgroup_controller);
1383 if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
1388 if (!(b = new0(CGroupBonding, 1))) {
1393 b->controller = controller;
1396 b->clean_up = false;
1398 if ((r = unit_add_cgroup(u, b)) < 0)
1411 int unit_add_default_cgroup(Unit *u) {
1417 /* Adds in the default cgroup data, if it wasn't specified yet */
1419 if (unit_get_default_cgroup(u))
1422 if (!(b = new0(CGroupBonding, 1)))
1425 if (!(b->controller = strdup(u->meta.manager->cgroup_controller)))
1428 if (!(b->path = default_cgroup_path(u)))
1434 if ((r = unit_add_cgroup(u, b)) < 0)
1441 free(b->controller);
1447 CGroupBonding* unit_get_default_cgroup(Unit *u) {
1450 return cgroup_bonding_find_list(u->meta.cgroup_bondings, u->meta.manager->cgroup_controller);
1453 int unit_load_related_unit(Unit *u, const char *type, Unit **_found) {
1461 if (!(t = unit_name_change_suffix(unit_id(u), type)))
1464 assert(!unit_has_name(u, t));
1466 r = manager_load_unit(u->meta.manager, t, _found);
1470 assert(*_found != u);
1475 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
1476 [UNIT_SERVICE] = "service",
1477 [UNIT_TIMER] = "timer",
1478 [UNIT_SOCKET] = "socket",
1479 [UNIT_TARGET] = "target",
1480 [UNIT_DEVICE] = "device",
1481 [UNIT_MOUNT] = "mount",
1482 [UNIT_AUTOMOUNT] = "automount",
1483 [UNIT_SNAPSHOT] = "snapshot"
1486 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
1488 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
1489 [UNIT_STUB] = "stub",
1490 [UNIT_LOADED] = "loaded",
1491 [UNIT_FAILED] = "failed",
1492 [UNIT_MERGED] = "merged"
1495 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
1497 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
1498 [UNIT_ACTIVE] = "active",
1499 [UNIT_INACTIVE] = "inactive",
1500 [UNIT_ACTIVATING] = "activating",
1501 [UNIT_DEACTIVATING] = "deactivating"
1504 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
1506 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
1507 [UNIT_REQUIRES] = "Requires",
1508 [UNIT_SOFT_REQUIRES] = "SoftRequires",
1509 [UNIT_WANTS] = "Wants",
1510 [UNIT_REQUISITE] = "Requisite",
1511 [UNIT_SOFT_REQUISITE] = "SoftRequisite",
1512 [UNIT_REQUIRED_BY] = "RequiredBy",
1513 [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
1514 [UNIT_WANTED_BY] = "WantedBy",
1515 [UNIT_CONFLICTS] = "Conflicts",
1516 [UNIT_BEFORE] = "Before",
1517 [UNIT_AFTER] = "After",
1520 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);
1522 static const char* const kill_mode_table[_KILL_MODE_MAX] = {
1523 [KILL_CONTROL_GROUP] = "control-group",
1524 [KILL_PROCESS_GROUP] = "process-group",
1525 [KILL_PROCESS] = "process",
1526 [KILL_NONE] = "none"
1529 DEFINE_STRING_TABLE_LOOKUP(kill_mode, KillMode);