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 (!(s = strdup(text)))
161 if ((r = set_put(u->meta.names, s)) < 0) {
170 if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
171 set_remove(u->meta.names, s);
176 if (u->meta.type == _UNIT_TYPE_INVALID)
177 LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);
184 unit_add_to_dbus_queue(u);
188 int unit_choose_id(Unit *u, const char *name) {
194 /* Selects one of the names of this unit as the id */
196 if (!(s = set_get(u->meta.names, (char*) name)))
201 unit_add_to_dbus_queue(u);
205 int unit_set_description(Unit *u, const char *description) {
210 if (!(s = strdup(description)))
213 free(u->meta.description);
214 u->meta.description = s;
216 unit_add_to_dbus_queue(u);
220 void unit_add_to_load_queue(Unit *u) {
222 assert(u->meta.type != _UNIT_TYPE_INVALID);
224 if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
227 LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
228 u->meta.in_load_queue = true;
231 void unit_add_to_cleanup_queue(Unit *u) {
234 if (u->meta.in_cleanup_queue)
237 LIST_PREPEND(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
238 u->meta.in_cleanup_queue = true;
241 void unit_add_to_dbus_queue(Unit *u) {
243 assert(u->meta.type != _UNIT_TYPE_INVALID);
245 if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue || set_isempty(u->meta.manager->subscribed))
248 LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
249 u->meta.in_dbus_queue = true;
252 static void bidi_set_free(Unit *u, Set *s) {
258 /* Frees the set and makes sure we are dropped from the
259 * inverse pointers */
261 SET_FOREACH(other, s, i) {
264 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
265 set_remove(other->meta.dependencies[d], u);
271 void unit_free(Unit *u) {
278 bus_unit_send_removed_signal(u);
280 /* Detach from next 'bigger' objects */
282 cgroup_bonding_free_list(u->meta.cgroup_bondings);
284 SET_FOREACH(t, u->meta.names, i)
285 hashmap_remove_value(u->meta.manager->units, t, u);
287 if (u->meta.type != _UNIT_TYPE_INVALID)
288 LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta);
290 if (u->meta.in_load_queue)
291 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
293 if (u->meta.in_dbus_queue)
294 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
296 if (u->meta.in_cleanup_queue)
297 LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
299 if (u->meta.load_state != UNIT_STUB)
300 if (UNIT_VTABLE(u)->done)
301 UNIT_VTABLE(u)->done(u);
303 /* Free data and next 'smaller' objects */
305 job_free(u->meta.job);
307 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
308 bidi_set_free(u, u->meta.dependencies[d]);
310 free(u->meta.description);
311 free(u->meta.fragment_path);
313 while ((t = set_steal_first(u->meta.names)))
315 set_free(u->meta.names);
320 UnitActiveState unit_active_state(Unit *u) {
323 if (u->meta.load_state != UNIT_LOADED)
324 return UNIT_INACTIVE;
326 return UNIT_VTABLE(u)->active_state(u);
329 static void complete_move(Set **s, Set **other) {
337 set_move(*s, *other);
344 static void merge_names(Unit *u, Unit *other) {
351 complete_move(&u->meta.names, &other->meta.names);
353 while ((t = set_steal_first(other->meta.names)))
356 set_free(other->meta.names);
357 other->meta.names = NULL;
358 other->meta.id = NULL;
360 SET_FOREACH(t, u->meta.names, i)
361 assert_se(hashmap_replace(u->meta.manager->units, t, u) == 0);
364 static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
371 assert(d < _UNIT_DEPENDENCY_MAX);
373 SET_FOREACH(back, other->meta.dependencies[d], i) {
376 for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++)
377 if ((r = set_remove_and_put(back->meta.dependencies[k], other, u)) < 0) {
380 set_remove(back->meta.dependencies[k], other);
382 assert(r == -ENOENT);
386 complete_move(&u->meta.dependencies[d], &other->meta.dependencies[d]);
388 set_free(other->meta.dependencies[d]);
389 other->meta.dependencies[d] = NULL;
392 int unit_merge(Unit *u, Unit *other) {
397 assert(u->meta.manager == other->meta.manager);
399 other = unit_follow_merge(other);
404 /* This merges 'other' into 'unit'. FIXME: This does not
405 * rollback on failure. */
407 if (u->meta.type != u->meta.type)
410 if (other->meta.load_state != UNIT_STUB &&
411 other->meta.load_state != UNIT_FAILED)
417 if (unit_active_state(other) != UNIT_INACTIVE)
421 merge_names(u, other);
423 /* Merge dependencies */
424 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
425 merge_dependencies(u, other, d);
427 other->meta.load_state = UNIT_MERGED;
428 other->meta.merged_into = u;
430 /* If there is still some data attached to the other node, we
431 * don't need it anymore, and can free it. */
432 if (other->meta.load_state != UNIT_STUB)
433 if (UNIT_VTABLE(other)->done)
434 UNIT_VTABLE(other)->done(other);
436 unit_add_to_dbus_queue(u);
437 unit_add_to_cleanup_queue(other);
442 int unit_merge_by_name(Unit *u, const char *name) {
448 if (!(other = manager_get_unit(u->meta.manager, name)))
449 return unit_add_name(u, name);
451 return unit_merge(u, other);
454 Unit* unit_follow_merge(Unit *u) {
457 while (u->meta.load_state == UNIT_MERGED)
458 assert_se(u = u->meta.merged_into);
463 int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
469 if (c->output != EXEC_OUTPUT_KERNEL && c->output != EXEC_OUTPUT_SYSLOG)
472 /* If syslog or kernel logging is requested, make sure our own
473 * logging daemon is run first. */
475 if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET)) < 0)
478 if (u->meta.manager->running_as != MANAGER_SESSION)
479 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET)) < 0)
485 const char* unit_id(Unit *u) {
491 return set_first(u->meta.names);
494 const char *unit_description(Unit *u) {
497 if (u->meta.description)
498 return u->meta.description;
503 void unit_dump(Unit *u, FILE *f, const char *prefix) {
510 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
516 p2 = strappend(prefix, "\t");
517 prefix2 = p2 ? p2 : prefix;
521 "%s\tDescription: %s\n"
522 "%s\tUnit Load State: %s\n"
523 "%s\tUnit Active State: %s\n"
524 "%s\tActive Enter Timestamp: %s\n"
525 "%s\tActive Exit Timestamp: %s\n",
527 prefix, unit_description(u),
528 prefix, unit_load_state_to_string(u->meta.load_state),
529 prefix, unit_active_state_to_string(unit_active_state(u)),
530 prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.active_enter_timestamp)),
531 prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->meta.active_exit_timestamp)));
533 SET_FOREACH(t, u->meta.names, i)
534 fprintf(f, "%s\tName: %s\n", prefix, t);
536 if (u->meta.fragment_path)
537 fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);
539 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
542 if (set_isempty(u->meta.dependencies[d]))
545 SET_FOREACH(other, u->meta.dependencies[d], i)
546 fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), unit_id(other));
550 "%s\tRecursive Stop: %s\n"
551 "%s\tStop When Unneeded: %s\n",
552 prefix, yes_no(u->meta.recursive_stop),
553 prefix, yes_no(u->meta.stop_when_unneeded));
555 if (u->meta.load_state == UNIT_LOADED) {
556 LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
557 fprintf(f, "%s\tControlGroup: %s:%s\n",
558 prefix, b->controller, b->path);
560 if (UNIT_VTABLE(u)->dump)
561 UNIT_VTABLE(u)->dump(u, f, prefix2);
565 job_dump(u->meta.job, f, prefix2);
570 /* Common implementation for multiple backends */
571 int unit_load_fragment_and_dropin(Unit *u, UnitLoadState *new_state) {
576 assert(*new_state == UNIT_STUB || *new_state == UNIT_LOADED);
578 /* Load a .service file */
579 if ((r = unit_load_fragment(u, new_state)) < 0)
582 if (*new_state == UNIT_STUB)
585 /* Load drop-in directory data */
586 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
592 /* Common implementation for multiple backends */
593 int unit_load_fragment_and_dropin_optional(Unit *u, UnitLoadState *new_state) {
598 assert(*new_state == UNIT_STUB || *new_state == UNIT_LOADED);
600 /* Same as unit_load_fragment_and_dropin(), but whether
601 * something can be loaded or not doesn't matter. */
603 /* Load a .service file */
604 if ((r = unit_load_fragment(u, new_state)) < 0)
607 if (*new_state == UNIT_STUB)
608 *new_state = UNIT_LOADED;
610 /* Load drop-in directory data */
611 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
617 int unit_load(Unit *u) {
623 if (u->meta.in_load_queue) {
624 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
625 u->meta.in_load_queue = false;
628 if (u->meta.load_state != UNIT_STUB)
631 if (UNIT_VTABLE(u)->init) {
633 if ((r = UNIT_VTABLE(u)->init(u, &res)) < 0)
637 if (res == UNIT_STUB) {
642 u->meta.load_state = res;
643 assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);
645 unit_add_to_dbus_queue(unit_follow_merge(u));
650 u->meta.load_state = UNIT_FAILED;
651 unit_add_to_dbus_queue(u);
653 log_error("Failed to load configuration for %s: %s", unit_id(u), strerror(-r));
659 * -EBADR: This unit type does not support starting.
660 * -EALREADY: Unit is already started.
661 * -EAGAIN: An operation is already in progress. Retry later.
663 int unit_start(Unit *u) {
664 UnitActiveState state;
668 /* If this is already (being) started, then this will
669 * succeed. Note that this will even succeed if this unit is
670 * not startable by the user. This is relied on to detect when
671 * we need to wait for units and when waiting is finished. */
672 state = unit_active_state(u);
673 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
676 /* If it is stopped, but we cannot start it, then fail */
677 if (!UNIT_VTABLE(u)->start)
680 /* We don't suppress calls to ->start() here when we are
681 * already starting, to allow this request to be used as a
682 * "hurry up" call, for example when the unit is in some "auto
683 * restart" state where it waits for a holdoff timer to elapse
684 * before it will start again. */
686 unit_add_to_dbus_queue(u);
687 return UNIT_VTABLE(u)->start(u);
690 bool unit_can_start(Unit *u) {
693 return !!UNIT_VTABLE(u)->start;
697 * -EBADR: This unit type does not support stopping.
698 * -EALREADY: Unit is already stopped.
699 * -EAGAIN: An operation is already in progress. Retry later.
701 int unit_stop(Unit *u) {
702 UnitActiveState state;
706 state = unit_active_state(u);
707 if (state == UNIT_INACTIVE)
710 if (!UNIT_VTABLE(u)->stop)
713 if (state == UNIT_DEACTIVATING)
716 unit_add_to_dbus_queue(u);
717 return UNIT_VTABLE(u)->stop(u);
721 * -EBADR: This unit type does not support reloading.
722 * -ENOEXEC: Unit is not started.
723 * -EAGAIN: An operation is already in progress. Retry later.
725 int unit_reload(Unit *u) {
726 UnitActiveState state;
730 if (!unit_can_reload(u))
733 state = unit_active_state(u);
734 if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
737 if (unit_active_state(u) != UNIT_ACTIVE)
740 unit_add_to_dbus_queue(u);
741 return UNIT_VTABLE(u)->reload(u);
744 bool unit_can_reload(Unit *u) {
747 if (!UNIT_VTABLE(u)->reload)
750 if (!UNIT_VTABLE(u)->can_reload)
753 return UNIT_VTABLE(u)->can_reload(u);
756 static void unit_check_uneeded(Unit *u) {
762 /* If this service shall be shut down when unneeded then do
765 if (!u->meta.stop_when_unneeded)
768 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
771 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
772 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
775 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
776 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
779 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
780 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
783 log_debug("Service %s is not needed anymore. Stopping.", unit_id(u));
785 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
786 manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL);
789 static void retroactively_start_dependencies(Unit *u) {
794 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
796 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
797 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
798 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
800 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
801 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
802 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
804 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
805 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
806 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
808 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
809 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
810 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
812 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
813 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
814 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
817 static void retroactively_stop_dependencies(Unit *u) {
822 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
824 if (u->meta.recursive_stop) {
825 /* Pull down units need us recursively if enabled */
826 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
827 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
828 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
831 /* Garbage collect services that might not be needed anymore, if enabled */
832 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
833 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
834 unit_check_uneeded(other);
835 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
836 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
837 unit_check_uneeded(other);
838 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
839 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
840 unit_check_uneeded(other);
841 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
842 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
843 unit_check_uneeded(other);
844 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUISITE], i)
845 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
846 unit_check_uneeded(other);
849 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
850 bool unexpected = false;
853 assert(os < _UNIT_ACTIVE_STATE_MAX);
854 assert(ns < _UNIT_ACTIVE_STATE_MAX);
855 assert(!(os == UNIT_ACTIVE && ns == UNIT_ACTIVATING));
856 assert(!(os == UNIT_INACTIVE && ns == UNIT_DEACTIVATING));
861 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
862 u->meta.active_enter_timestamp = now(CLOCK_REALTIME);
863 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
864 u->meta.active_exit_timestamp = now(CLOCK_REALTIME);
868 if (u->meta.job->state == JOB_WAITING)
870 /* So we reached a different state for this
871 * job. Let's see if we can run it now if it
872 * failed previously due to EAGAIN. */
873 job_add_to_run_queue(u->meta.job);
876 assert(u->meta.job->state == JOB_RUNNING);
878 /* Let's check whether this state change
879 * constitutes a finished job, or maybe
880 * cotradicts a running job and hence needs to
881 * invalidate jobs. */
883 switch (u->meta.job->type) {
886 case JOB_VERIFY_ACTIVE:
888 if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
889 job_finish_and_invalidate(u->meta.job, true);
890 else if (ns != UNIT_ACTIVATING) {
892 job_finish_and_invalidate(u->meta.job, false);
898 case JOB_RELOAD_OR_START:
900 if (ns == UNIT_ACTIVE)
901 job_finish_and_invalidate(u->meta.job, true);
902 else if (ns != UNIT_ACTIVATING && ns != UNIT_ACTIVE_RELOADING) {
904 job_finish_and_invalidate(u->meta.job, false);
911 case JOB_TRY_RESTART:
913 if (ns == UNIT_INACTIVE)
914 job_finish_and_invalidate(u->meta.job, true);
915 else if (ns != UNIT_DEACTIVATING) {
917 job_finish_and_invalidate(u->meta.job, false);
923 assert_not_reached("Job type unknown");
928 /* If this state change happened without being requested by a
929 * job, then let's retroactively start or stop dependencies */
932 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
933 retroactively_start_dependencies(u);
934 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
935 retroactively_stop_dependencies(u);
938 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
940 if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) {
941 log_info("D-Bus became available, trying to reconnect.");
942 /* The bus just got started, hence try to connect to it. */
943 bus_init_system(u->meta.manager);
944 bus_init_api(u->meta.manager);
947 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE)) {
948 /* The syslog daemon just got started, hence try to connect to it. */
949 log_info("Syslog became available, trying to reconnect.");
953 } else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
955 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
956 /* The syslog daemon just got terminated, hence try to disconnect from it. */
959 /* We don't care about D-Bus here, since we'll get an
960 * asynchronous notification for it anyway. */
963 /* Maybe we finished startup and are now ready for being
964 * stopped because unneeded? */
965 unit_check_uneeded(u);
967 unit_add_to_dbus_queue(u);
970 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
971 struct epoll_event ev;
976 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
982 if (epoll_ctl(u->meta.manager->epoll_fd,
983 w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
995 void unit_unwatch_fd(Unit *u, Watch *w) {
999 if (w->type == WATCH_INVALID)
1002 assert(w->type == WATCH_FD);
1003 assert(w->data.unit == u);
1004 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1007 w->type = WATCH_INVALID;
1008 w->data.unit = NULL;
1011 int unit_watch_pid(Unit *u, pid_t pid) {
1015 return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
1018 void unit_unwatch_pid(Unit *u, pid_t pid) {
1022 hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
1025 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
1026 struct itimerspec its;
1032 assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->data.unit == u));
1034 /* This will try to reuse the old timer if there is one */
1036 if (w->type == WATCH_TIMER) {
1041 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
1048 /* Set absolute time in the past, but not 0, since we
1049 * don't want to disarm the timer */
1050 its.it_value.tv_sec = 0;
1051 its.it_value.tv_nsec = 1;
1053 flags = TFD_TIMER_ABSTIME;
1055 timespec_store(&its.it_value, delay);
1059 /* This will also flush the elapse counter */
1060 if (timerfd_settime(fd, flags, &its, NULL) < 0)
1063 if (w->type == WATCH_INVALID) {
1064 struct epoll_event ev;
1068 ev.events = EPOLLIN;
1070 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
1075 w->type = WATCH_TIMER;
1082 close_nointr_nofail(fd);
1087 void unit_unwatch_timer(Unit *u, Watch *w) {
1091 if (w->type == WATCH_INVALID)
1094 assert(w->type == WATCH_TIMER && w->data.unit == u);
1096 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1097 assert_se(close_nointr(w->fd) == 0);
1100 w->type = WATCH_INVALID;
1101 w->data.unit = NULL;
1104 bool unit_job_is_applicable(Unit *u, JobType j) {
1106 assert(j >= 0 && j < _JOB_TYPE_MAX);
1110 case JOB_VERIFY_ACTIVE:
1116 case JOB_TRY_RESTART:
1117 return unit_can_start(u);
1120 return unit_can_reload(u);
1122 case JOB_RELOAD_OR_START:
1123 return unit_can_reload(u) && unit_can_start(u);
1126 assert_not_reached("Invalid job type");
1130 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {
1132 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
1133 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
1134 [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY,
1135 [UNIT_WANTS] = UNIT_WANTED_BY,
1136 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
1137 [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY,
1138 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1139 [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1140 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
1141 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
1142 [UNIT_BEFORE] = UNIT_AFTER,
1143 [UNIT_AFTER] = UNIT_BEFORE
1148 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
1149 assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
1152 /* We won't allow dependencies on ourselves. We will not
1153 * consider them an error however. */
1157 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
1160 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
1163 if ((r = set_put(u->meta.dependencies[d], other)) < 0)
1166 if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
1167 set_remove(u->meta.dependencies[d], other);
1171 unit_add_to_dbus_queue(u);
1175 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) {
1179 if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
1182 if ((r = unit_add_dependency(u, d, other)) < 0)
1188 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name) {
1192 if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
1195 if ((r = unit_add_dependency(other, d, u)) < 0)
1201 int set_unit_path(const char *p) {
1205 /* This is mostly for debug purposes */
1207 if (path_is_absolute(p)) {
1208 if (!(c = strdup(p)))
1211 if (!(cwd = get_current_dir_name()))
1214 r = asprintf(&c, "%s/%s", cwd, p);
1221 if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
1230 char *unit_name_escape_path(const char *path, const char *suffix) {
1237 /* Takes a path and a suffix and prefix and makes a nice
1238 * string suitable as unit name of it, escaping all weird
1241 * / becomes ., and all chars not alloweed in a unit name get
1242 * escaped as \xFF, including \ and ., of course. This
1243 * escaping is hence reversible.
1252 if (!(r = new(char, a*4+b+1)))
1255 for (f = path, t = r; *f; f++) {
1258 else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
1261 *(t++) = hexchar(*f > 4);
1262 *(t++) = hexchar(*f);
1267 memcpy(t, suffix, b+1);
1272 char *unit_dbus_path(Unit *u) {
1277 if (!(e = bus_path_escape(unit_id(u))))
1280 if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) {
1289 int unit_add_cgroup(Unit *u, CGroupBonding *b) {
1297 /* Ensure this hasn't been added yet */
1300 l = hashmap_get(u->meta.manager->cgroup_bondings, b->path);
1301 LIST_PREPEND(CGroupBonding, by_path, l, b);
1303 if ((r = hashmap_replace(u->meta.manager->cgroup_bondings, b->path, l)) < 0) {
1304 LIST_REMOVE(CGroupBonding, by_path, l, b);
1308 LIST_PREPEND(CGroupBonding, by_unit, u->meta.cgroup_bondings, b);
1314 int unit_add_cgroup_from_text(Unit *u, const char *name) {
1324 /* Detect controller name */
1325 n = strcspn(name, ":/");
1327 /* Only controller name, no path? No path? */
1339 /* Insist in absolute paths */
1343 if (!(controller = strndup(name, n)))
1346 if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
1351 if (!(b = new0(CGroupBonding, 1))) {
1356 b->controller = controller;
1358 if (!(b->path = strdup(p))) {
1364 b->clean_up = false;
1366 if ((r = unit_add_cgroup(u, b)) < 0)
1373 free(b->controller);
1379 int unit_add_default_cgroup(Unit *u) {
1385 /* Adds in the default cgroup data, if it wasn't specified yet */
1387 if (unit_get_default_cgroup(u))
1390 if (!(b = new0(CGroupBonding, 1)))
1393 if (!(b->controller = strdup(u->meta.manager->cgroup_controller)))
1396 if (asprintf(&b->path, "%s/%s", u->meta.manager->cgroup_hierarchy, unit_id(u)) < 0)
1402 if ((r = unit_add_cgroup(u, b)) < 0)
1409 free(b->controller);
1415 CGroupBonding* unit_get_default_cgroup(Unit *u) {
1418 return cgroup_bonding_find_list(u->meta.cgroup_bondings, u->meta.manager->cgroup_controller);
1421 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
1422 [UNIT_SERVICE] = "service",
1423 [UNIT_TIMER] = "timer",
1424 [UNIT_SOCKET] = "socket",
1425 [UNIT_TARGET] = "target",
1426 [UNIT_DEVICE] = "device",
1427 [UNIT_MOUNT] = "mount",
1428 [UNIT_AUTOMOUNT] = "automount",
1429 [UNIT_SNAPSHOT] = "snapshot"
1432 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
1434 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
1435 [UNIT_STUB] = "stub",
1436 [UNIT_LOADED] = "loaded",
1437 [UNIT_FAILED] = "failed",
1438 [UNIT_MERGED] = "merged"
1441 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
1443 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
1444 [UNIT_ACTIVE] = "active",
1445 [UNIT_INACTIVE] = "inactive",
1446 [UNIT_ACTIVATING] = "activating",
1447 [UNIT_DEACTIVATING] = "deactivating"
1450 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
1452 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
1453 [UNIT_REQUIRES] = "Requires",
1454 [UNIT_SOFT_REQUIRES] = "SoftRequires",
1455 [UNIT_WANTS] = "Wants",
1456 [UNIT_REQUISITE] = "Requisite",
1457 [UNIT_SOFT_REQUISITE] = "SoftRequisite",
1458 [UNIT_REQUIRED_BY] = "RequiredBy",
1459 [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
1460 [UNIT_WANTED_BY] = "WantedBy",
1461 [UNIT_CONFLICTS] = "Conflicts",
1462 [UNIT_BEFORE] = "Before",
1463 [UNIT_AFTER] = "After",
1466 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);
1468 static const char* const kill_mode_table[_KILL_MODE_MAX] = {
1469 [KILL_PROCESS] = "process",
1470 [KILL_PROCESS_GROUP] = "process-group",
1471 [KILL_CONTROL_GROUP] = "control-group"
1474 DEFINE_STRING_TABLE_LOOKUP(kill_mode, KillMode);