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_cleanup_queue(Unit *u) {
226 if (u->meta.in_cleanup_queue)
229 LIST_PREPEND(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
230 u->meta.in_cleanup_queue = true;
233 void unit_add_to_dbus_queue(Unit *u) {
236 if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue || set_isempty(u->meta.manager->subscribed))
239 LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
240 u->meta.in_dbus_queue = true;
243 static void bidi_set_free(Unit *u, Set *s) {
249 /* Frees the set and makes sure we are dropped from the
250 * inverse pointers */
252 SET_FOREACH(other, s, i) {
255 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
256 set_remove(other->meta.dependencies[d], u);
262 void unit_free(Unit *u) {
269 bus_unit_send_removed_signal(u);
271 /* Detach from next 'bigger' objects */
273 cgroup_bonding_free_list(u->meta.cgroup_bondings);
275 SET_FOREACH(t, u->meta.names, i)
276 hashmap_remove_value(u->meta.manager->units, t, u);
278 if (u->meta.type != _UNIT_TYPE_INVALID)
279 LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta);
281 if (u->meta.in_load_queue)
282 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
284 if (u->meta.in_dbus_queue)
285 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
287 if (u->meta.in_cleanup_queue)
288 LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
290 if (u->meta.load_state != UNIT_STUB)
291 if (UNIT_VTABLE(u)->done)
292 UNIT_VTABLE(u)->done(u);
294 /* Free data and next 'smaller' objects */
296 job_free(u->meta.job);
298 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
299 bidi_set_free(u, u->meta.dependencies[d]);
301 free(u->meta.description);
302 free(u->meta.fragment_path);
304 while ((t = set_steal_first(u->meta.names)))
306 set_free(u->meta.names);
311 UnitActiveState unit_active_state(Unit *u) {
314 if (u->meta.load_state != UNIT_LOADED)
315 return UNIT_INACTIVE;
317 return UNIT_VTABLE(u)->active_state(u);
320 static void complete_move(Set **s, Set **other) {
328 set_move(*s, *other);
335 static void merge_names(Unit *u, Unit *other) {
342 complete_move(&u->meta.names, &other->meta.names);
344 while ((t = set_steal_first(other->meta.names)))
347 set_free(other->meta.names);
348 other->meta.names = NULL;
349 other->meta.id = NULL;
351 SET_FOREACH(t, u->meta.names, i)
352 assert_se(hashmap_replace(u->meta.manager->units, t, u) == 0);
355 static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
362 assert(d < _UNIT_DEPENDENCY_MAX);
364 SET_FOREACH(back, other->meta.dependencies[d], i) {
367 for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++)
368 if ((r = set_remove_and_put(back->meta.dependencies[k], other, u)) < 0) {
371 set_remove(back->meta.dependencies[k], other);
373 assert(r == -ENOENT);
377 complete_move(&u->meta.dependencies[d], &other->meta.dependencies[d]);
379 set_free(other->meta.dependencies[d]);
380 other->meta.dependencies[d] = NULL;
383 int unit_merge(Unit *u, Unit *other) {
388 assert(u->meta.manager == other->meta.manager);
393 /* This merges 'other' into 'unit'. FIXME: This does not
394 * rollback on failure. */
396 if (u->meta.type != u->meta.type)
399 if (other->meta.load_state != UNIT_STUB)
403 merge_names(u, other);
405 /* Merge dependencies */
406 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
407 merge_dependencies(u, other, d);
409 unit_add_to_dbus_queue(u);
411 other->meta.load_state = UNIT_MERGED;
412 other->meta.merged_into = u;
414 unit_add_to_cleanup_queue(other);
419 int unit_merge_by_name(Unit *u, const char *name) {
425 if (!(other = manager_get_unit(u->meta.manager, name)))
426 return unit_add_name(u, name);
428 return unit_merge(u, other);
431 Unit* unit_follow_merge(Unit *u) {
434 while (u->meta.load_state == UNIT_MERGED)
435 assert_se(u = u->meta.merged_into);
440 int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
446 if (c->output != EXEC_OUTPUT_KERNEL && c->output != EXEC_OUTPUT_SYSLOG)
449 /* If syslog or kernel logging is requested, make sure our own
450 * logging daemon is run first. */
452 if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET)) < 0)
455 if (u->meta.manager->running_as != MANAGER_SESSION)
456 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET)) < 0)
462 const char* unit_id(Unit *u) {
468 return set_first(u->meta.names);
471 const char *unit_description(Unit *u) {
474 if (u->meta.description)
475 return u->meta.description;
480 void unit_dump(Unit *u, FILE *f, const char *prefix) {
492 p2 = strappend(prefix, "\t");
493 prefix2 = p2 ? p2 : prefix;
497 "%s\tDescription: %s\n"
498 "%s\tUnit Load State: %s\n"
499 "%s\tUnit Active State: %s\n",
501 prefix, unit_description(u),
502 prefix, unit_load_state_to_string(u->meta.load_state),
503 prefix, unit_active_state_to_string(unit_active_state(u)));
505 SET_FOREACH(t, u->meta.names, i)
506 fprintf(f, "%s\tName: %s\n", prefix, t);
508 if (u->meta.fragment_path)
509 fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);
511 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
514 if (set_isempty(u->meta.dependencies[d]))
517 SET_FOREACH(other, u->meta.dependencies[d], i)
518 fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), unit_id(other));
522 "%s\tRecursive Stop: %s\n"
523 "%s\tStop When Unneeded: %s\n",
524 prefix, yes_no(u->meta.recursive_stop),
525 prefix, yes_no(u->meta.stop_when_unneeded));
527 if (u->meta.load_state == UNIT_LOADED) {
528 LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
529 fprintf(f, "%s\tControlGroup: %s:%s\n",
530 prefix, b->controller, b->path);
532 if (UNIT_VTABLE(u)->dump)
533 UNIT_VTABLE(u)->dump(u, f, prefix2);
537 job_dump(u->meta.job, f, prefix2);
542 /* Common implementation for multiple backends */
543 int unit_load_fragment_and_dropin(Unit *u, UnitLoadState *new_state) {
548 assert(*new_state == UNIT_STUB || *new_state == UNIT_LOADED);
550 /* Load a .service file */
551 if ((r = unit_load_fragment(u, new_state)) < 0)
554 if (*new_state == UNIT_STUB)
557 /* Load drop-in directory data */
558 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
564 /* Common implementation for multiple backends */
565 int unit_load_fragment_and_dropin_optional(Unit *u, UnitLoadState *new_state) {
570 assert(*new_state == UNIT_STUB || *new_state == UNIT_LOADED);
572 /* Same as unit_load_fragment_and_dropin(), but whether
573 * something can be loaded or not doesn't matter. */
575 /* Load a .service file */
576 if ((r = unit_load_fragment(u, new_state)) < 0)
579 if (*new_state == UNIT_STUB)
580 *new_state = UNIT_LOADED;
582 /* Load drop-in directory data */
583 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
589 int unit_load(Unit *u) {
595 if (u->meta.in_load_queue) {
596 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
597 u->meta.in_load_queue = false;
600 if (u->meta.load_state != UNIT_STUB)
603 if (UNIT_VTABLE(u)->init) {
605 if ((r = UNIT_VTABLE(u)->init(u, &res)) < 0)
609 if (res == UNIT_STUB) {
614 u->meta.load_state = res;
615 assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);
617 unit_add_to_dbus_queue(unit_follow_merge(u));
622 u->meta.load_state = UNIT_FAILED;
623 unit_add_to_dbus_queue(u);
625 log_error("Failed to load configuration for %s: %s", unit_id(u), strerror(-r));
631 * -EBADR: This unit type does not support starting.
632 * -EALREADY: Unit is already started.
633 * -EAGAIN: An operation is already in progress. Retry later.
635 int unit_start(Unit *u) {
636 UnitActiveState state;
640 /* If this is already (being) started, then this will
641 * succeed. Note that this will even succeed if this unit is
642 * not startable by the user. This is relied on to detect when
643 * we need to wait for units and when waiting is finished. */
644 state = unit_active_state(u);
645 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
648 /* If it is stopped, but we cannot start it, then fail */
649 if (!UNIT_VTABLE(u)->start)
652 /* We don't suppress calls to ->start() here when we are
653 * already starting, to allow this request to be used as a
654 * "hurry up" call, for example when the unit is in some "auto
655 * restart" state where it waits for a holdoff timer to elapse
656 * before it will start again. */
658 unit_add_to_dbus_queue(u);
659 return UNIT_VTABLE(u)->start(u);
662 bool unit_can_start(Unit *u) {
665 return !!UNIT_VTABLE(u)->start;
669 * -EBADR: This unit type does not support stopping.
670 * -EALREADY: Unit is already stopped.
671 * -EAGAIN: An operation is already in progress. Retry later.
673 int unit_stop(Unit *u) {
674 UnitActiveState state;
678 state = unit_active_state(u);
679 if (state == UNIT_INACTIVE)
682 if (!UNIT_VTABLE(u)->stop)
685 if (state == UNIT_DEACTIVATING)
688 unit_add_to_dbus_queue(u);
689 return UNIT_VTABLE(u)->stop(u);
693 * -EBADR: This unit type does not support reloading.
694 * -ENOEXEC: Unit is not started.
695 * -EAGAIN: An operation is already in progress. Retry later.
697 int unit_reload(Unit *u) {
698 UnitActiveState state;
702 if (!unit_can_reload(u))
705 state = unit_active_state(u);
706 if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
709 if (unit_active_state(u) != UNIT_ACTIVE)
712 unit_add_to_dbus_queue(u);
713 return UNIT_VTABLE(u)->reload(u);
716 bool unit_can_reload(Unit *u) {
719 if (!UNIT_VTABLE(u)->reload)
722 if (!UNIT_VTABLE(u)->can_reload)
725 return UNIT_VTABLE(u)->can_reload(u);
728 static void unit_check_uneeded(Unit *u) {
734 /* If this service shall be shut down when unneeded then do
737 if (!u->meta.stop_when_unneeded)
740 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
743 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
744 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
747 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
748 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
751 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
752 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
755 log_debug("Service %s is not needed anymore. Stopping.", unit_id(u));
757 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
758 manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL);
761 static void retroactively_start_dependencies(Unit *u) {
766 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
768 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
769 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
770 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
772 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
773 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
774 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
776 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
777 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
778 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
780 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
781 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
782 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
784 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
785 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
786 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
789 static void retroactively_stop_dependencies(Unit *u) {
794 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
796 if (u->meta.recursive_stop) {
797 /* Pull down units need us recursively if enabled */
798 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
799 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
800 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
803 /* Garbage collect services that might not be needed anymore, if enabled */
804 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
805 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
806 unit_check_uneeded(other);
807 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
808 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
809 unit_check_uneeded(other);
810 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
811 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
812 unit_check_uneeded(other);
813 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
814 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
815 unit_check_uneeded(other);
816 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUISITE], i)
817 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
818 unit_check_uneeded(other);
821 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
823 assert(os < _UNIT_ACTIVE_STATE_MAX);
824 assert(ns < _UNIT_ACTIVE_STATE_MAX);
825 assert(!(os == UNIT_ACTIVE && ns == UNIT_ACTIVATING));
826 assert(!(os == UNIT_INACTIVE && ns == UNIT_DEACTIVATING));
831 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
832 u->meta.active_enter_timestamp = now(CLOCK_REALTIME);
833 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
834 u->meta.active_exit_timestamp = now(CLOCK_REALTIME);
838 if (u->meta.job->state == JOB_WAITING)
840 /* So we reached a different state for this
841 * job. Let's see if we can run it now if it
842 * failed previously due to EAGAIN. */
843 job_add_to_run_queue(u->meta.job);
846 assert(u->meta.job->state == JOB_RUNNING);
848 /* Let's check whether this state change
849 * constitutes a finished job, or maybe
850 * cotradicts a running job and hence needs to
851 * invalidate jobs. */
853 switch (u->meta.job->type) {
856 case JOB_VERIFY_ACTIVE:
858 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
859 job_finish_and_invalidate(u->meta.job, true);
861 } else if (ns == UNIT_ACTIVATING)
864 job_finish_and_invalidate(u->meta.job, false);
869 case JOB_RELOAD_OR_START:
871 if (ns == UNIT_ACTIVE) {
872 job_finish_and_invalidate(u->meta.job, true);
874 } else if (ns == UNIT_ACTIVATING || ns == UNIT_ACTIVE_RELOADING)
877 job_finish_and_invalidate(u->meta.job, false);
883 case JOB_TRY_RESTART:
885 if (ns == UNIT_INACTIVE) {
886 job_finish_and_invalidate(u->meta.job, true);
888 } else if (ns == UNIT_DEACTIVATING)
891 job_finish_and_invalidate(u->meta.job, false);
896 assert_not_reached("Job type unknown");
901 /* If this state change happened without being requested by a
902 * job, then let's retroactively start or stop dependencies */
904 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
905 retroactively_start_dependencies(u);
906 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
907 retroactively_stop_dependencies(u);
909 /* Maybe we finished startup and are now ready for being
910 * stopped because unneeded? */
911 unit_check_uneeded(u);
913 unit_add_to_dbus_queue(u);
916 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
917 struct epoll_event ev;
922 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
928 if (epoll_ctl(u->meta.manager->epoll_fd,
929 w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
941 void unit_unwatch_fd(Unit *u, Watch *w) {
945 if (w->type == WATCH_INVALID)
948 assert(w->type == WATCH_FD);
949 assert(w->data.unit == u);
950 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
953 w->type = WATCH_INVALID;
957 int unit_watch_pid(Unit *u, pid_t pid) {
961 return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
964 void unit_unwatch_pid(Unit *u, pid_t pid) {
968 hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
971 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
972 struct itimerspec its;
978 assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->data.unit == u));
980 /* This will try to reuse the old timer if there is one */
982 if (w->type == WATCH_TIMER) {
987 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
994 /* Set absolute time in the past, but not 0, since we
995 * don't want to disarm the timer */
996 its.it_value.tv_sec = 0;
997 its.it_value.tv_nsec = 1;
999 flags = TFD_TIMER_ABSTIME;
1001 timespec_store(&its.it_value, delay);
1005 /* This will also flush the elapse counter */
1006 if (timerfd_settime(fd, flags, &its, NULL) < 0)
1009 if (w->type == WATCH_INVALID) {
1010 struct epoll_event ev;
1014 ev.events = EPOLLIN;
1016 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
1021 w->type = WATCH_TIMER;
1028 close_nointr_nofail(fd);
1033 void unit_unwatch_timer(Unit *u, Watch *w) {
1037 if (w->type == WATCH_INVALID)
1040 assert(w->type == WATCH_TIMER && w->data.unit == u);
1042 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1043 assert_se(close_nointr(w->fd) == 0);
1046 w->type = WATCH_INVALID;
1047 w->data.unit = NULL;
1050 bool unit_job_is_applicable(Unit *u, JobType j) {
1052 assert(j >= 0 && j < _JOB_TYPE_MAX);
1056 case JOB_VERIFY_ACTIVE:
1062 case JOB_TRY_RESTART:
1063 return unit_can_start(u);
1066 return unit_can_reload(u);
1068 case JOB_RELOAD_OR_START:
1069 return unit_can_reload(u) && unit_can_start(u);
1072 assert_not_reached("Invalid job type");
1076 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {
1078 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
1079 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
1080 [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY,
1081 [UNIT_WANTS] = UNIT_WANTED_BY,
1082 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
1083 [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY,
1084 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1085 [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1086 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
1087 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
1088 [UNIT_BEFORE] = UNIT_AFTER,
1089 [UNIT_AFTER] = UNIT_BEFORE
1094 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
1095 assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
1098 /* We won't allow dependencies on ourselves. We will not
1099 * consider them an error however. */
1103 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
1106 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
1109 if ((r = set_put(u->meta.dependencies[d], other)) < 0)
1112 if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
1113 set_remove(u->meta.dependencies[d], other);
1117 unit_add_to_dbus_queue(u);
1121 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) {
1125 if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
1128 if ((r = unit_add_dependency(u, d, other)) < 0)
1134 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name) {
1138 if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
1141 if ((r = unit_add_dependency(other, d, u)) < 0)
1147 int set_unit_path(const char *p) {
1151 /* This is mostly for debug purposes */
1153 if (path_is_absolute(p)) {
1154 if (!(c = strdup(p)))
1157 if (!(cwd = get_current_dir_name()))
1160 r = asprintf(&c, "%s/%s", cwd, p);
1167 if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
1176 char *unit_name_escape_path(const char *path, const char *suffix) {
1183 /* Takes a path and a suffix and prefix and makes a nice
1184 * string suitable as unit name of it, escaping all weird
1187 * / becomes ., and all chars not alloweed in a unit name get
1188 * escaped as \xFF, including \ and ., of course. This
1189 * escaping is hence reversible.
1198 if (!(r = new(char, a*4+b+1)))
1201 for (f = path, t = r; *f; f++) {
1204 else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
1207 *(t++) = hexchar(*f > 4);
1208 *(t++) = hexchar(*f);
1213 memcpy(t, suffix, b+1);
1218 char *unit_dbus_path(Unit *u) {
1223 if (!(e = bus_path_escape(unit_id(u))))
1226 if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) {
1235 int unit_add_cgroup(Unit *u, CGroupBonding *b) {
1243 /* Ensure this hasn't been added yet */
1246 l = hashmap_get(u->meta.manager->cgroup_bondings, b->path);
1247 LIST_PREPEND(CGroupBonding, by_path, l, b);
1249 if ((r = hashmap_replace(u->meta.manager->cgroup_bondings, b->path, l)) < 0) {
1250 LIST_REMOVE(CGroupBonding, by_path, l, b);
1254 LIST_PREPEND(CGroupBonding, by_unit, u->meta.cgroup_bondings, b);
1260 int unit_add_cgroup_from_text(Unit *u, const char *name) {
1270 /* Detect controller name */
1271 n = strcspn(name, ":/");
1273 /* Only controller name, no path? No path? */
1285 /* Insist in absolute paths */
1289 if (!(controller = strndup(name, n)))
1292 if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
1297 if (!(b = new0(CGroupBonding, 1))) {
1302 b->controller = controller;
1304 if (!(b->path = strdup(p))) {
1310 b->clean_up = false;
1312 if ((r = unit_add_cgroup(u, b)) < 0)
1319 free(b->controller);
1325 int unit_add_default_cgroup(Unit *u) {
1331 /* Adds in the default cgroup data, if it wasn't specified yet */
1333 if (unit_get_default_cgroup(u))
1336 if (!(b = new0(CGroupBonding, 1)))
1339 if (!(b->controller = strdup(u->meta.manager->cgroup_controller)))
1342 if (asprintf(&b->path, "%s/%s", u->meta.manager->cgroup_hierarchy, unit_id(u)) < 0)
1348 if ((r = unit_add_cgroup(u, b)) < 0)
1355 free(b->controller);
1361 CGroupBonding* unit_get_default_cgroup(Unit *u) {
1364 return cgroup_bonding_find_list(u->meta.cgroup_bondings, u->meta.manager->cgroup_controller);
1367 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
1368 [UNIT_SERVICE] = "service",
1369 [UNIT_TIMER] = "timer",
1370 [UNIT_SOCKET] = "socket",
1371 [UNIT_TARGET] = "target",
1372 [UNIT_DEVICE] = "device",
1373 [UNIT_MOUNT] = "mount",
1374 [UNIT_AUTOMOUNT] = "automount",
1375 [UNIT_SNAPSHOT] = "snapshot"
1378 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
1380 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
1381 [UNIT_STUB] = "stub",
1382 [UNIT_LOADED] = "loaded",
1383 [UNIT_FAILED] = "failed",
1384 [UNIT_MERGED] = "merged"
1387 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
1389 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
1390 [UNIT_ACTIVE] = "active",
1391 [UNIT_INACTIVE] = "inactive",
1392 [UNIT_ACTIVATING] = "activating",
1393 [UNIT_DEACTIVATING] = "deactivating"
1396 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
1398 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
1399 [UNIT_REQUIRES] = "Requires",
1400 [UNIT_SOFT_REQUIRES] = "SoftRequires",
1401 [UNIT_WANTS] = "Wants",
1402 [UNIT_REQUISITE] = "Requisite",
1403 [UNIT_SOFT_REQUISITE] = "SoftRequisite",
1404 [UNIT_REQUIRED_BY] = "RequiredBy",
1405 [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
1406 [UNIT_WANTED_BY] = "WantedBy",
1407 [UNIT_CONFLICTS] = "Conflicts",
1408 [UNIT_BEFORE] = "Before",
1409 [UNIT_AFTER] = "After",
1412 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);