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) {
223 if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
226 LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
227 u->meta.in_load_queue = true;
230 void unit_add_to_cleanup_queue(Unit *u) {
233 if (u->meta.in_cleanup_queue)
236 LIST_PREPEND(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
237 u->meta.in_cleanup_queue = true;
240 void unit_add_to_dbus_queue(Unit *u) {
243 if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue || set_isempty(u->meta.manager->subscribed))
246 LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
247 u->meta.in_dbus_queue = true;
250 static void bidi_set_free(Unit *u, Set *s) {
256 /* Frees the set and makes sure we are dropped from the
257 * inverse pointers */
259 SET_FOREACH(other, s, i) {
262 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
263 set_remove(other->meta.dependencies[d], u);
269 void unit_free(Unit *u) {
276 bus_unit_send_removed_signal(u);
278 /* Detach from next 'bigger' objects */
280 cgroup_bonding_free_list(u->meta.cgroup_bondings);
282 SET_FOREACH(t, u->meta.names, i)
283 hashmap_remove_value(u->meta.manager->units, t, u);
285 if (u->meta.type != _UNIT_TYPE_INVALID)
286 LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta);
288 if (u->meta.in_load_queue)
289 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
291 if (u->meta.in_dbus_queue)
292 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
294 if (u->meta.in_cleanup_queue)
295 LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
297 if (u->meta.load_state != UNIT_STUB)
298 if (UNIT_VTABLE(u)->done)
299 UNIT_VTABLE(u)->done(u);
301 /* Free data and next 'smaller' objects */
303 job_free(u->meta.job);
305 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
306 bidi_set_free(u, u->meta.dependencies[d]);
308 free(u->meta.description);
309 free(u->meta.fragment_path);
311 while ((t = set_steal_first(u->meta.names)))
313 set_free(u->meta.names);
318 UnitActiveState unit_active_state(Unit *u) {
321 if (u->meta.load_state != UNIT_LOADED)
322 return UNIT_INACTIVE;
324 return UNIT_VTABLE(u)->active_state(u);
327 static void complete_move(Set **s, Set **other) {
335 set_move(*s, *other);
342 static void merge_names(Unit *u, Unit *other) {
349 complete_move(&u->meta.names, &other->meta.names);
351 while ((t = set_steal_first(other->meta.names)))
354 set_free(other->meta.names);
355 other->meta.names = NULL;
356 other->meta.id = NULL;
358 SET_FOREACH(t, u->meta.names, i)
359 assert_se(hashmap_replace(u->meta.manager->units, t, u) == 0);
362 static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
369 assert(d < _UNIT_DEPENDENCY_MAX);
371 SET_FOREACH(back, other->meta.dependencies[d], i) {
374 for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++)
375 if ((r = set_remove_and_put(back->meta.dependencies[k], other, u)) < 0) {
378 set_remove(back->meta.dependencies[k], other);
380 assert(r == -ENOENT);
384 complete_move(&u->meta.dependencies[d], &other->meta.dependencies[d]);
386 set_free(other->meta.dependencies[d]);
387 other->meta.dependencies[d] = NULL;
390 int unit_merge(Unit *u, Unit *other) {
395 assert(u->meta.manager == other->meta.manager);
400 /* This merges 'other' into 'unit'. FIXME: This does not
401 * rollback on failure. */
403 if (u->meta.type != u->meta.type)
406 if (other->meta.load_state != UNIT_STUB)
410 merge_names(u, other);
412 /* Merge dependencies */
413 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
414 merge_dependencies(u, other, d);
416 unit_add_to_dbus_queue(u);
418 other->meta.load_state = UNIT_MERGED;
419 other->meta.merged_into = u;
421 unit_add_to_cleanup_queue(other);
426 int unit_merge_by_name(Unit *u, const char *name) {
432 if (!(other = manager_get_unit(u->meta.manager, name)))
433 return unit_add_name(u, name);
435 return unit_merge(u, other);
438 Unit* unit_follow_merge(Unit *u) {
441 while (u->meta.load_state == UNIT_MERGED)
442 assert_se(u = u->meta.merged_into);
447 int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
453 if (c->output != EXEC_OUTPUT_KERNEL && c->output != EXEC_OUTPUT_SYSLOG)
456 /* If syslog or kernel logging is requested, make sure our own
457 * logging daemon is run first. */
459 if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET)) < 0)
462 if (u->meta.manager->running_as != MANAGER_SESSION)
463 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET)) < 0)
469 const char* unit_id(Unit *u) {
475 return set_first(u->meta.names);
478 const char *unit_description(Unit *u) {
481 if (u->meta.description)
482 return u->meta.description;
487 void unit_dump(Unit *u, FILE *f, const char *prefix) {
499 p2 = strappend(prefix, "\t");
500 prefix2 = p2 ? p2 : prefix;
504 "%s\tDescription: %s\n"
505 "%s\tUnit Load State: %s\n"
506 "%s\tUnit Active State: %s\n",
508 prefix, unit_description(u),
509 prefix, unit_load_state_to_string(u->meta.load_state),
510 prefix, unit_active_state_to_string(unit_active_state(u)));
512 SET_FOREACH(t, u->meta.names, i)
513 fprintf(f, "%s\tName: %s\n", prefix, t);
515 if (u->meta.fragment_path)
516 fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);
518 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
521 if (set_isempty(u->meta.dependencies[d]))
524 SET_FOREACH(other, u->meta.dependencies[d], i)
525 fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), unit_id(other));
529 "%s\tRecursive Stop: %s\n"
530 "%s\tStop When Unneeded: %s\n",
531 prefix, yes_no(u->meta.recursive_stop),
532 prefix, yes_no(u->meta.stop_when_unneeded));
534 if (u->meta.load_state == UNIT_LOADED) {
535 LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
536 fprintf(f, "%s\tControlGroup: %s:%s\n",
537 prefix, b->controller, b->path);
539 if (UNIT_VTABLE(u)->dump)
540 UNIT_VTABLE(u)->dump(u, f, prefix2);
544 job_dump(u->meta.job, f, prefix2);
549 /* Common implementation for multiple backends */
550 int unit_load_fragment_and_dropin(Unit *u, UnitLoadState *new_state) {
555 assert(*new_state == UNIT_STUB || *new_state == UNIT_LOADED);
557 /* Load a .service file */
558 if ((r = unit_load_fragment(u, new_state)) < 0)
561 if (*new_state == UNIT_STUB)
564 /* Load drop-in directory data */
565 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
571 /* Common implementation for multiple backends */
572 int unit_load_fragment_and_dropin_optional(Unit *u, UnitLoadState *new_state) {
577 assert(*new_state == UNIT_STUB || *new_state == UNIT_LOADED);
579 /* Same as unit_load_fragment_and_dropin(), but whether
580 * something can be loaded or not doesn't matter. */
582 /* Load a .service file */
583 if ((r = unit_load_fragment(u, new_state)) < 0)
586 if (*new_state == UNIT_STUB)
587 *new_state = UNIT_LOADED;
589 /* Load drop-in directory data */
590 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
596 int unit_load(Unit *u) {
602 if (u->meta.in_load_queue) {
603 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
604 u->meta.in_load_queue = false;
607 if (u->meta.load_state != UNIT_STUB)
610 if (UNIT_VTABLE(u)->init) {
612 if ((r = UNIT_VTABLE(u)->init(u, &res)) < 0)
616 if (res == UNIT_STUB) {
621 u->meta.load_state = res;
622 assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);
624 unit_add_to_dbus_queue(unit_follow_merge(u));
629 u->meta.load_state = UNIT_FAILED;
630 unit_add_to_dbus_queue(u);
632 log_error("Failed to load configuration for %s: %s", unit_id(u), strerror(-r));
638 * -EBADR: This unit type does not support starting.
639 * -EALREADY: Unit is already started.
640 * -EAGAIN: An operation is already in progress. Retry later.
642 int unit_start(Unit *u) {
643 UnitActiveState state;
647 /* If this is already (being) started, then this will
648 * succeed. Note that this will even succeed if this unit is
649 * not startable by the user. This is relied on to detect when
650 * we need to wait for units and when waiting is finished. */
651 state = unit_active_state(u);
652 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
655 /* If it is stopped, but we cannot start it, then fail */
656 if (!UNIT_VTABLE(u)->start)
659 /* We don't suppress calls to ->start() here when we are
660 * already starting, to allow this request to be used as a
661 * "hurry up" call, for example when the unit is in some "auto
662 * restart" state where it waits for a holdoff timer to elapse
663 * before it will start again. */
665 unit_add_to_dbus_queue(u);
666 return UNIT_VTABLE(u)->start(u);
669 bool unit_can_start(Unit *u) {
672 return !!UNIT_VTABLE(u)->start;
676 * -EBADR: This unit type does not support stopping.
677 * -EALREADY: Unit is already stopped.
678 * -EAGAIN: An operation is already in progress. Retry later.
680 int unit_stop(Unit *u) {
681 UnitActiveState state;
685 state = unit_active_state(u);
686 if (state == UNIT_INACTIVE)
689 if (!UNIT_VTABLE(u)->stop)
692 if (state == UNIT_DEACTIVATING)
695 unit_add_to_dbus_queue(u);
696 return UNIT_VTABLE(u)->stop(u);
700 * -EBADR: This unit type does not support reloading.
701 * -ENOEXEC: Unit is not started.
702 * -EAGAIN: An operation is already in progress. Retry later.
704 int unit_reload(Unit *u) {
705 UnitActiveState state;
709 if (!unit_can_reload(u))
712 state = unit_active_state(u);
713 if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
716 if (unit_active_state(u) != UNIT_ACTIVE)
719 unit_add_to_dbus_queue(u);
720 return UNIT_VTABLE(u)->reload(u);
723 bool unit_can_reload(Unit *u) {
726 if (!UNIT_VTABLE(u)->reload)
729 if (!UNIT_VTABLE(u)->can_reload)
732 return UNIT_VTABLE(u)->can_reload(u);
735 static void unit_check_uneeded(Unit *u) {
741 /* If this service shall be shut down when unneeded then do
744 if (!u->meta.stop_when_unneeded)
747 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
750 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
751 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
754 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
755 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
758 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
759 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
762 log_debug("Service %s is not needed anymore. Stopping.", unit_id(u));
764 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
765 manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL);
768 static void retroactively_start_dependencies(Unit *u) {
773 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
775 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
776 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
777 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
779 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
780 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
781 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
783 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
784 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
785 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
787 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
788 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
789 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
791 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
792 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
793 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
796 static void retroactively_stop_dependencies(Unit *u) {
801 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
803 if (u->meta.recursive_stop) {
804 /* Pull down units need us recursively if enabled */
805 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
806 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
807 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
810 /* Garbage collect services that might not be needed anymore, if enabled */
811 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
812 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
813 unit_check_uneeded(other);
814 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
815 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
816 unit_check_uneeded(other);
817 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
818 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
819 unit_check_uneeded(other);
820 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
821 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
822 unit_check_uneeded(other);
823 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUISITE], i)
824 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
825 unit_check_uneeded(other);
828 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
830 assert(os < _UNIT_ACTIVE_STATE_MAX);
831 assert(ns < _UNIT_ACTIVE_STATE_MAX);
832 assert(!(os == UNIT_ACTIVE && ns == UNIT_ACTIVATING));
833 assert(!(os == UNIT_INACTIVE && ns == UNIT_DEACTIVATING));
838 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
839 u->meta.active_enter_timestamp = now(CLOCK_REALTIME);
840 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
841 u->meta.active_exit_timestamp = now(CLOCK_REALTIME);
845 if (u->meta.job->state == JOB_WAITING)
847 /* So we reached a different state for this
848 * job. Let's see if we can run it now if it
849 * failed previously due to EAGAIN. */
850 job_add_to_run_queue(u->meta.job);
853 assert(u->meta.job->state == JOB_RUNNING);
855 /* Let's check whether this state change
856 * constitutes a finished job, or maybe
857 * cotradicts a running job and hence needs to
858 * invalidate jobs. */
860 switch (u->meta.job->type) {
863 case JOB_VERIFY_ACTIVE:
865 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
866 job_finish_and_invalidate(u->meta.job, true);
868 } else if (ns == UNIT_ACTIVATING)
871 job_finish_and_invalidate(u->meta.job, false);
876 case JOB_RELOAD_OR_START:
878 if (ns == UNIT_ACTIVE) {
879 job_finish_and_invalidate(u->meta.job, true);
881 } else if (ns == UNIT_ACTIVATING || ns == UNIT_ACTIVE_RELOADING)
884 job_finish_and_invalidate(u->meta.job, false);
890 case JOB_TRY_RESTART:
892 if (ns == UNIT_INACTIVE) {
893 job_finish_and_invalidate(u->meta.job, true);
895 } else if (ns == UNIT_DEACTIVATING)
898 job_finish_and_invalidate(u->meta.job, false);
903 assert_not_reached("Job type unknown");
908 /* If this state change happened without being requested by a
909 * job, then let's retroactively start or stop dependencies */
911 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
912 retroactively_start_dependencies(u);
913 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
914 retroactively_stop_dependencies(u);
916 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
918 if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) {
919 /* The bus just got started, hence try to connect to it. */
920 bus_init_system(u->meta.manager);
921 bus_init_api(u->meta.manager);
924 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
925 /* The syslog daemon just got started, hence try to connect to it. */
928 } else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
930 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
931 /* The syslog daemon just got terminated, hence try to disconnect from it. */
934 /* We don't care about D-Bus here, since we'll get an
935 * asynchronous notification for it anyway. */
938 /* Maybe we finished startup and are now ready for being
939 * stopped because unneeded? */
940 unit_check_uneeded(u);
942 unit_add_to_dbus_queue(u);
945 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
946 struct epoll_event ev;
951 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
957 if (epoll_ctl(u->meta.manager->epoll_fd,
958 w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
970 void unit_unwatch_fd(Unit *u, Watch *w) {
974 if (w->type == WATCH_INVALID)
977 assert(w->type == WATCH_FD);
978 assert(w->data.unit == u);
979 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
982 w->type = WATCH_INVALID;
986 int unit_watch_pid(Unit *u, pid_t pid) {
990 return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
993 void unit_unwatch_pid(Unit *u, pid_t pid) {
997 hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
1000 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
1001 struct itimerspec its;
1007 assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->data.unit == u));
1009 /* This will try to reuse the old timer if there is one */
1011 if (w->type == WATCH_TIMER) {
1016 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
1023 /* Set absolute time in the past, but not 0, since we
1024 * don't want to disarm the timer */
1025 its.it_value.tv_sec = 0;
1026 its.it_value.tv_nsec = 1;
1028 flags = TFD_TIMER_ABSTIME;
1030 timespec_store(&its.it_value, delay);
1034 /* This will also flush the elapse counter */
1035 if (timerfd_settime(fd, flags, &its, NULL) < 0)
1038 if (w->type == WATCH_INVALID) {
1039 struct epoll_event ev;
1043 ev.events = EPOLLIN;
1045 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
1050 w->type = WATCH_TIMER;
1057 close_nointr_nofail(fd);
1062 void unit_unwatch_timer(Unit *u, Watch *w) {
1066 if (w->type == WATCH_INVALID)
1069 assert(w->type == WATCH_TIMER && w->data.unit == u);
1071 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1072 assert_se(close_nointr(w->fd) == 0);
1075 w->type = WATCH_INVALID;
1076 w->data.unit = NULL;
1079 bool unit_job_is_applicable(Unit *u, JobType j) {
1081 assert(j >= 0 && j < _JOB_TYPE_MAX);
1085 case JOB_VERIFY_ACTIVE:
1091 case JOB_TRY_RESTART:
1092 return unit_can_start(u);
1095 return unit_can_reload(u);
1097 case JOB_RELOAD_OR_START:
1098 return unit_can_reload(u) && unit_can_start(u);
1101 assert_not_reached("Invalid job type");
1105 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {
1107 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
1108 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
1109 [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY,
1110 [UNIT_WANTS] = UNIT_WANTED_BY,
1111 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
1112 [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY,
1113 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1114 [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1115 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
1116 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
1117 [UNIT_BEFORE] = UNIT_AFTER,
1118 [UNIT_AFTER] = UNIT_BEFORE
1123 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
1124 assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
1127 /* We won't allow dependencies on ourselves. We will not
1128 * consider them an error however. */
1132 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
1135 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
1138 if ((r = set_put(u->meta.dependencies[d], other)) < 0)
1141 if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
1142 set_remove(u->meta.dependencies[d], other);
1146 unit_add_to_dbus_queue(u);
1150 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) {
1154 if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
1157 if ((r = unit_add_dependency(u, d, other)) < 0)
1163 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name) {
1167 if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
1170 if ((r = unit_add_dependency(other, d, u)) < 0)
1176 int set_unit_path(const char *p) {
1180 /* This is mostly for debug purposes */
1182 if (path_is_absolute(p)) {
1183 if (!(c = strdup(p)))
1186 if (!(cwd = get_current_dir_name()))
1189 r = asprintf(&c, "%s/%s", cwd, p);
1196 if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
1205 char *unit_name_escape_path(const char *path, const char *suffix) {
1212 /* Takes a path and a suffix and prefix and makes a nice
1213 * string suitable as unit name of it, escaping all weird
1216 * / becomes ., and all chars not alloweed in a unit name get
1217 * escaped as \xFF, including \ and ., of course. This
1218 * escaping is hence reversible.
1227 if (!(r = new(char, a*4+b+1)))
1230 for (f = path, t = r; *f; f++) {
1233 else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
1236 *(t++) = hexchar(*f > 4);
1237 *(t++) = hexchar(*f);
1242 memcpy(t, suffix, b+1);
1247 char *unit_dbus_path(Unit *u) {
1252 if (!(e = bus_path_escape(unit_id(u))))
1255 if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) {
1264 int unit_add_cgroup(Unit *u, CGroupBonding *b) {
1272 /* Ensure this hasn't been added yet */
1275 l = hashmap_get(u->meta.manager->cgroup_bondings, b->path);
1276 LIST_PREPEND(CGroupBonding, by_path, l, b);
1278 if ((r = hashmap_replace(u->meta.manager->cgroup_bondings, b->path, l)) < 0) {
1279 LIST_REMOVE(CGroupBonding, by_path, l, b);
1283 LIST_PREPEND(CGroupBonding, by_unit, u->meta.cgroup_bondings, b);
1289 int unit_add_cgroup_from_text(Unit *u, const char *name) {
1299 /* Detect controller name */
1300 n = strcspn(name, ":/");
1302 /* Only controller name, no path? No path? */
1314 /* Insist in absolute paths */
1318 if (!(controller = strndup(name, n)))
1321 if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
1326 if (!(b = new0(CGroupBonding, 1))) {
1331 b->controller = controller;
1333 if (!(b->path = strdup(p))) {
1339 b->clean_up = false;
1341 if ((r = unit_add_cgroup(u, b)) < 0)
1348 free(b->controller);
1354 int unit_add_default_cgroup(Unit *u) {
1360 /* Adds in the default cgroup data, if it wasn't specified yet */
1362 if (unit_get_default_cgroup(u))
1365 if (!(b = new0(CGroupBonding, 1)))
1368 if (!(b->controller = strdup(u->meta.manager->cgroup_controller)))
1371 if (asprintf(&b->path, "%s/%s", u->meta.manager->cgroup_hierarchy, unit_id(u)) < 0)
1377 if ((r = unit_add_cgroup(u, b)) < 0)
1384 free(b->controller);
1390 CGroupBonding* unit_get_default_cgroup(Unit *u) {
1393 return cgroup_bonding_find_list(u->meta.cgroup_bondings, u->meta.manager->cgroup_controller);
1396 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
1397 [UNIT_SERVICE] = "service",
1398 [UNIT_TIMER] = "timer",
1399 [UNIT_SOCKET] = "socket",
1400 [UNIT_TARGET] = "target",
1401 [UNIT_DEVICE] = "device",
1402 [UNIT_MOUNT] = "mount",
1403 [UNIT_AUTOMOUNT] = "automount",
1404 [UNIT_SNAPSHOT] = "snapshot"
1407 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
1409 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
1410 [UNIT_STUB] = "stub",
1411 [UNIT_LOADED] = "loaded",
1412 [UNIT_FAILED] = "failed",
1413 [UNIT_MERGED] = "merged"
1416 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
1418 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
1419 [UNIT_ACTIVE] = "active",
1420 [UNIT_INACTIVE] = "inactive",
1421 [UNIT_ACTIVATING] = "activating",
1422 [UNIT_DEACTIVATING] = "deactivating"
1425 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
1427 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
1428 [UNIT_REQUIRES] = "Requires",
1429 [UNIT_SOFT_REQUIRES] = "SoftRequires",
1430 [UNIT_WANTS] = "Wants",
1431 [UNIT_REQUISITE] = "Requisite",
1432 [UNIT_SOFT_REQUISITE] = "SoftRequisite",
1433 [UNIT_REQUIRED_BY] = "RequiredBy",
1434 [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
1435 [UNIT_WANTED_BY] = "WantedBy",
1436 [UNIT_CONFLICTS] = "Conflicts",
1437 [UNIT_BEFORE] = "Before",
1438 [UNIT_AFTER] = "After",
1441 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);