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/>.
29 #include "load-fragment.h"
30 #include "load-dropin.h"
33 #include "unit-name.h"
34 #include "dbus-service.h"
36 #define COMMENTS "#;\n"
37 #define NEWLINES "\n\r"
40 static const char * const rcnd_table[] = {
41 "/rc0.d", SPECIAL_RUNLEVEL0_TARGET,
42 "/rc1.d", SPECIAL_RUNLEVEL1_TARGET,
43 "/rc2.d", SPECIAL_RUNLEVEL2_TARGET,
44 "/rc3.d", SPECIAL_RUNLEVEL3_TARGET,
45 "/rc4.d", SPECIAL_RUNLEVEL4_TARGET,
46 "/rc5.d", SPECIAL_RUNLEVEL5_TARGET,
47 "/rc6.d", SPECIAL_RUNLEVEL6_TARGET,
48 "/boot.d", SPECIAL_BASIC_TARGET
51 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
52 [SERVICE_DEAD] = UNIT_INACTIVE,
53 [SERVICE_START_PRE] = UNIT_ACTIVATING,
54 [SERVICE_START] = UNIT_ACTIVATING,
55 [SERVICE_START_POST] = UNIT_ACTIVATING,
56 [SERVICE_RUNNING] = UNIT_ACTIVE,
57 [SERVICE_EXITED] = UNIT_ACTIVE,
58 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
59 [SERVICE_STOP] = UNIT_DEACTIVATING,
60 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
61 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
62 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
63 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
64 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
65 [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
66 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
69 static void service_init(Unit *u) {
70 Service *s = SERVICE(u);
73 assert(u->meta.load_state == UNIT_STUB);
75 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
76 s->restart_usec = DEFAULT_RESTART_USEC;
77 s->timer_watch.type = WATCH_INVALID;
78 s->sysv_start_priority = -1;
81 exec_context_init(&s->exec_context);
83 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
85 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
88 static void service_unwatch_control_pid(Service *s) {
91 if (s->control_pid <= 0)
94 unit_unwatch_pid(UNIT(s), s->control_pid);
98 static void service_unwatch_main_pid(Service *s) {
101 if (s->main_pid <= 0)
104 unit_unwatch_pid(UNIT(s), s->main_pid);
108 static void service_close_socket_fd(Service *s) {
111 if (s->socket_fd < 0)
114 close_nointr_nofail(s->socket_fd);
118 static void service_done(Unit *u) {
119 Service *s = SERVICE(u);
129 free(s->sysv_runlevels);
130 s->sysv_runlevels = NULL;
132 exec_context_done(&s->exec_context);
133 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
134 s->control_command = NULL;
136 /* This will leak a process, but at least no memory or any of
138 service_unwatch_main_pid(s);
139 service_unwatch_control_pid(s);
142 unit_unwatch_bus_name(UNIT(u), s->bus_name);
147 service_close_socket_fd(s);
149 unit_unwatch_timer(u, &s->timer_watch);
152 static int sysv_translate_name(const char *name, char **_r) {
154 static const char * const table[] = {
155 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
156 "$network", SPECIAL_NETWORK_TARGET,
157 "$named", SPECIAL_NSS_LOOKUP_TARGET,
158 "$portmap", SPECIAL_RPCBIND_TARGET,
159 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
160 "$syslog", SPECIAL_SYSLOG_TARGET,
161 "$time", SPECIAL_RTC_SET_TARGET
167 for (i = 0; i < ELEMENTSOF(table); i += 2)
168 if (streq(table[i], name)) {
169 if (!(r = strdup(table[i+1])))
178 if (asprintf(&r, "%s.service", name) < 0)
189 static int sysv_chkconfig_order(Service *s) {
195 if (s->sysv_start_priority < 0)
198 /* For each pair of services where at least one lacks a LSB
199 * header, we use the start priority value to order things. */
201 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
205 t = (Service*) other;
210 if (t->sysv_start_priority < 0)
213 /* If both units have modern headers we don't care
214 * about the priorities */
215 if ((!s->sysv_path || s->sysv_has_lsb) &&
216 (!t->sysv_path || t->sysv_has_lsb))
219 if (t->sysv_start_priority < s->sysv_start_priority)
221 else if (t->sysv_start_priority > s->sysv_start_priority)
226 /* FIXME: Maybe we should compare the name here lexicographically? */
228 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
235 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
238 if (!(c = new0(ExecCommand, 1)))
241 if (!(c->path = strdup(path))) {
246 if (!(c->argv = strv_new(path, arg1, NULL))) {
255 static int sysv_exec_commands(Service *s) {
259 assert(s->sysv_path);
261 if (!(c = exec_command_new(s->sysv_path, "start")))
263 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
265 if (!(c = exec_command_new(s->sysv_path, "stop")))
267 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
269 if (!(c = exec_command_new(s->sysv_path, "reload")))
271 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
276 static int priority_from_rcd(Service *s, const char *init_script) {
280 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvrcnd_path)
281 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
286 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0)
294 log_warning("opendir() failed on %s: %s", path, strerror(errno));
299 while ((de = readdir(d))) {
302 if (ignore_file(de->d_name))
305 if (de->d_name[0] != 'S')
308 if (strlen(de->d_name) < 4)
311 if (!streq(de->d_name + 3, init_script))
314 /* Yay, we found it! Now decode the priority */
316 a = undecchar(de->d_name[1]);
317 b = undecchar(de->d_name[2]);
322 s->sysv_start_priority = a*10 + b;
324 log_debug("Determined priority %i from link farm for %s", s->sysv_start_priority, UNIT(s)->meta.id);
336 static int service_load_sysv_path(Service *s, const char *path) {
353 if (!(f = fopen(path, "re"))) {
354 r = errno == ENOENT ? 0 : -errno;
358 s->type = SERVICE_FORKING;
359 s->restart = SERVICE_ONCE;
362 if (!(s->sysv_path = strdup(path))) {
368 char l[LINE_MAX], *t;
370 if (!fgets(l, sizeof(l), f)) {
375 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
385 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
387 s->sysv_has_lsb = true;
391 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
397 t += strspn(t, WHITESPACE);
399 if (state == NORMAL) {
401 /* Try to parse Red Hat style chkconfig headers */
403 if (startswith(t, "chkconfig:")) {
405 char runlevels[16], *k;
409 if (sscanf(t+10, "%15s %i %*i",
411 &start_priority) != 2) {
413 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
417 if (start_priority < 0 || start_priority > 99)
418 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
420 s->sysv_start_priority = start_priority;
422 char_array_0(runlevels);
423 k = delete_chars(runlevels, WHITESPACE "-");
428 if (!(d = strdup(k))) {
433 free(s->sysv_runlevels);
434 s->sysv_runlevels = d;
438 } else if (startswith(t, "description:")) {
440 size_t k = strlen(t);
443 if (t[k-1] == '\\') {
448 if (!(d = strdup(strstrip(t+12)))) {
453 free(u->meta.description);
454 u->meta.description = d;
456 } else if (startswith(t, "pidfile:")) {
463 if (!path_is_absolute(fn)) {
464 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
468 if (!(fn = strdup(fn))) {
477 } else if (state == DESCRIPTION) {
479 /* Try to parse Red Hat style description
482 size_t k = strlen(t);
490 assert(u->meta.description);
491 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
496 free(u->meta.description);
497 u->meta.description = d;
499 } else if (state == LSB || state == LSB_DESCRIPTION) {
501 if (startswith(t, "Provides:")) {
507 FOREACH_WORD(w, z, t+9, i) {
510 if (!(n = strndup(w, z))) {
515 r = sysv_translate_name(n, &m);
524 if (unit_name_to_type(m) == UNIT_SERVICE)
525 r = unit_add_name(u, m);
527 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL, true)) >= 0)
528 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
537 } else if (startswith(t, "Required-Start:") ||
538 startswith(t, "Should-Start:")) {
544 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
547 if (!(n = strndup(w, z))) {
552 r = sysv_translate_name(n, &m);
561 r = unit_add_dependency_by_name(u, UNIT_AFTER, m, NULL, true);
567 } else if (startswith(t, "Default-Start:")) {
572 k = delete_chars(t+14, WHITESPACE "-");
575 if (!(d = strdup(k))) {
580 free(s->sysv_runlevels);
581 s->sysv_runlevels = d;
584 } else if (startswith(t, "Description:")) {
587 state = LSB_DESCRIPTION;
589 if (!(d = strdup(strstrip(t+12)))) {
594 free(u->meta.description);
595 u->meta.description = d;
597 } else if (startswith(t, "Short-Description:") &&
598 !u->meta.description) {
601 /* We use the short description only
602 * if no long description is set. */
606 if (!(d = strdup(strstrip(t+18)))) {
611 u->meta.description = d;
613 } else if (state == LSB_DESCRIPTION) {
615 if (startswith(l, "#\t") || startswith(l, "# ")) {
618 assert(u->meta.description);
619 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
624 free(u->meta.description);
625 u->meta.description = d;
632 /* If init scripts have no LSB header, then we enforce the
633 * ordering via the chkconfig priorities. We try to determine
634 * a priority for *all* init scripts here, since they are
635 * needed as soon as at least one non-LSB script is used. */
637 if (s->sysv_start_priority < 0) {
638 log_debug("%s has no chkconfig header, trying to determine SysV priority from link farm.", u->meta.id);
640 if ((r = priority_from_rcd(s, file_name_from_path(path))) < 0)
643 if (s->sysv_start_priority < 0)
644 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", u->meta.id);
647 if ((r = sysv_exec_commands(s)) < 0)
650 if (!s->sysv_runlevels || chars_intersect("12345", s->sysv_runlevels)) {
651 /* If there a runlevels configured for this service
652 * but none of the standard ones, then we assume this
653 * is some special kind of service (which might be
654 * needed for early boot) and don't create any links
657 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0 ||
658 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
662 /* Special setting for all SysV services */
663 s->valid_no_process = true;
665 /* Don't timeout special services during boot (like fsck) */
666 if (s->sysv_runlevels && !chars_intersect("12345", s->sysv_runlevels))
667 s->timeout_usec = -1;
669 u->meta.load_state = UNIT_LOADED;
680 static int service_load_sysv_name(Service *s, const char *name) {
686 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
690 if (asprintf(&path, "%s/%s", *p, name) < 0)
693 assert(endswith(path, ".service"));
694 path[strlen(path)-8] = 0;
696 r = service_load_sysv_path(s, path);
702 if ((UNIT(s)->meta.load_state != UNIT_STUB))
709 static int service_load_sysv(Service *s) {
716 /* Load service data from SysV init scripts, preferably with
719 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
722 if ((t = UNIT(s)->meta.id))
723 if ((r = service_load_sysv_name(s, t)) < 0)
726 if (UNIT(s)->meta.load_state == UNIT_STUB)
727 SET_FOREACH(t, UNIT(s)->meta.names, i) {
728 if (t == UNIT(s)->meta.id)
731 if ((r == service_load_sysv_name(s, t)) < 0)
734 if (UNIT(s)->meta.load_state != UNIT_STUB)
741 static int service_add_bus_name(Service *s) {
748 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
751 r = unit_merge_by_name(UNIT(s), n);
757 static int service_verify(Service *s) {
760 if (UNIT(s)->meta.load_state != UNIT_LOADED)
763 if (!s->exec_command[SERVICE_EXEC_START]) {
764 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
768 if (s->type == SERVICE_DBUS && !s->bus_name) {
769 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->meta.id);
776 static int service_load(Unit *u) {
778 Service *s = SERVICE(u);
782 /* Load a .service file */
783 if ((r = unit_load_fragment(u)) < 0)
786 /* Load a classic init script as a fallback, if we couldn't find anything */
787 if (u->meta.load_state == UNIT_STUB)
788 if ((r = service_load_sysv(s)) < 0)
791 /* Still nothing found? Then let's give up */
792 if (u->meta.load_state == UNIT_STUB)
795 /* We were able to load something, then let's add in the
796 * dropin directories. */
797 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
800 /* This is a new unit? Then let's add in some extras */
801 if (u->meta.load_state == UNIT_LOADED) {
802 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
805 if ((r = unit_add_default_cgroup(u)) < 0)
808 if ((r = sysv_chkconfig_order(s)) < 0)
812 if ((r = service_add_bus_name(s)) < 0)
815 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
820 return service_verify(s);
823 static void service_dump(Unit *u, FILE *f, const char *prefix) {
825 ServiceExecCommand c;
826 Service *s = SERVICE(u);
832 p2 = strappend(prefix, "\t");
833 prefix2 = p2 ? p2 : prefix;
836 "%sService State: %s\n"
837 "%sPermissionsStartOnly: %s\n"
838 "%sRootDirectoryStartOnly: %s\n"
839 "%sValidNoProcess: %s\n"
842 prefix, service_state_to_string(s->state),
843 prefix, yes_no(s->permissions_start_only),
844 prefix, yes_no(s->root_directory_start_only),
845 prefix, yes_no(s->valid_no_process),
846 prefix, kill_mode_to_string(s->kill_mode),
847 prefix, service_type_to_string(s->type));
849 if (s->control_pid > 0)
851 "%sControl PID: %llu\n",
852 prefix, (unsigned long long) s->control_pid);
856 "%sMain PID: %llu\n",
857 prefix, (unsigned long long) s->main_pid);
862 prefix, s->pid_file);
867 "%sBus Name Good: %s\n",
869 prefix, yes_no(s->bus_name_good));
871 exec_context_dump(&s->exec_context, f, prefix);
873 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
875 if (!s->exec_command[c])
878 fprintf(f, "%s-> %s:\n",
879 prefix, service_exec_command_to_string(c));
881 exec_command_dump_list(s->exec_command[c], f, prefix2);
886 "%sSysV Init Script Path: %s\n"
887 "%sSysV Init Script has LSB Header: %s\n",
888 prefix, s->sysv_path,
889 prefix, yes_no(s->sysv_has_lsb));
891 if (s->sysv_start_priority >= 0)
893 "%sSysVStartPriority: %i\n",
894 prefix, s->sysv_start_priority);
896 if (s->sysv_runlevels)
897 fprintf(f, "%sSysVRunLevels: %s\n",
898 prefix, s->sysv_runlevels);
903 static int service_load_pid_file(Service *s) {
910 if (s->main_pid_known)
913 assert(s->main_pid <= 0);
918 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
921 if ((r = safe_atolu(k, &p)) < 0) {
926 if ((unsigned long) (pid_t) p != p)
929 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
930 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
931 (unsigned long long) p, s->pid_file);
935 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
936 /* FIXME: we need to do something here */
939 s->main_pid = (pid_t) p;
940 s->main_pid_known = true;
945 static int service_get_sockets(Service *s, Set **_set) {
954 /* Collects all Socket objects that belong to this
955 * service. Note that a service might have multiple sockets
956 * via multiple names. */
958 if (!(set = set_new(NULL, NULL)))
961 SET_FOREACH(t, UNIT(s)->meta.names, i) {
965 /* Look for all socket objects that go by any of our
966 * units and collect their fds */
968 if (!(k = unit_name_change_suffix(t, ".socket"))) {
973 p = manager_get_unit(UNIT(s)->meta.manager, k);
979 if ((r = set_put(set, p)) < 0)
991 static int service_notify_sockets_dead(Service *s) {
999 /* Notifies all our sockets when we die */
1000 if ((r = service_get_sockets(s, &set)) < 0)
1003 SET_FOREACH(sock, set, i)
1004 socket_notify_service_dead(sock);
1011 static void service_set_state(Service *s, ServiceState state) {
1012 ServiceState old_state;
1015 old_state = s->state;
1018 if (state != SERVICE_START_PRE &&
1019 state != SERVICE_START &&
1020 state != SERVICE_START_POST &&
1021 state != SERVICE_RELOAD &&
1022 state != SERVICE_STOP &&
1023 state != SERVICE_STOP_SIGTERM &&
1024 state != SERVICE_STOP_SIGKILL &&
1025 state != SERVICE_STOP_POST &&
1026 state != SERVICE_FINAL_SIGTERM &&
1027 state != SERVICE_FINAL_SIGKILL &&
1028 state != SERVICE_AUTO_RESTART)
1029 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1031 if (state != SERVICE_START &&
1032 state != SERVICE_START_POST &&
1033 state != SERVICE_RUNNING &&
1034 state != SERVICE_RELOAD &&
1035 state != SERVICE_STOP &&
1036 state != SERVICE_STOP_SIGTERM &&
1037 state != SERVICE_STOP_SIGKILL)
1038 service_unwatch_main_pid(s);
1040 if (state != SERVICE_START_PRE &&
1041 state != SERVICE_START &&
1042 state != SERVICE_START_POST &&
1043 state != SERVICE_RELOAD &&
1044 state != SERVICE_STOP &&
1045 state != SERVICE_STOP_SIGTERM &&
1046 state != SERVICE_STOP_SIGKILL &&
1047 state != SERVICE_STOP_POST &&
1048 state != SERVICE_FINAL_SIGTERM &&
1049 state != SERVICE_FINAL_SIGKILL) {
1050 service_unwatch_control_pid(s);
1051 s->control_command = NULL;
1052 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1055 if (state == SERVICE_DEAD ||
1056 state == SERVICE_STOP ||
1057 state == SERVICE_STOP_SIGTERM ||
1058 state == SERVICE_STOP_SIGKILL ||
1059 state == SERVICE_STOP_POST ||
1060 state == SERVICE_FINAL_SIGTERM ||
1061 state == SERVICE_FINAL_SIGKILL ||
1062 state == SERVICE_MAINTAINANCE ||
1063 state == SERVICE_AUTO_RESTART)
1064 service_notify_sockets_dead(s);
1066 if (state != SERVICE_START_PRE &&
1067 state != SERVICE_START &&
1068 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1069 service_close_socket_fd(s);
1071 if (old_state != state)
1072 log_debug("%s changed %s -> %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1074 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1077 static int service_coldplug(Unit *u) {
1078 Service *s = SERVICE(u);
1082 assert(s->state == SERVICE_DEAD);
1084 if (s->deserialized_state != s->state) {
1086 if (s->deserialized_state == SERVICE_START_PRE ||
1087 s->deserialized_state == SERVICE_START ||
1088 s->deserialized_state == SERVICE_START_POST ||
1089 s->deserialized_state == SERVICE_RELOAD ||
1090 s->deserialized_state == SERVICE_STOP ||
1091 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1092 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1093 s->deserialized_state == SERVICE_STOP_POST ||
1094 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1095 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1096 s->deserialized_state == SERVICE_AUTO_RESTART) {
1098 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1101 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1103 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1108 if ((s->deserialized_state == SERVICE_START &&
1109 (s->type == SERVICE_FORKING ||
1110 s->type == SERVICE_DBUS)) ||
1111 s->deserialized_state == SERVICE_START_POST ||
1112 s->deserialized_state == SERVICE_RUNNING ||
1113 s->deserialized_state == SERVICE_RELOAD ||
1114 s->deserialized_state == SERVICE_STOP ||
1115 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1116 s->deserialized_state == SERVICE_STOP_SIGKILL)
1117 if (s->main_pid > 0)
1118 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1121 if (s->deserialized_state == SERVICE_START_PRE ||
1122 s->deserialized_state == SERVICE_START ||
1123 s->deserialized_state == SERVICE_START_POST ||
1124 s->deserialized_state == SERVICE_RELOAD ||
1125 s->deserialized_state == SERVICE_STOP ||
1126 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1127 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1128 s->deserialized_state == SERVICE_STOP_POST ||
1129 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1130 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1131 if (s->control_pid > 0)
1132 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1135 service_set_state(s, s->deserialized_state);
1141 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1145 unsigned rn_fds = 0;
1153 if ((r = service_get_sockets(s, &set)) < 0)
1156 SET_FOREACH(sock, set, i) {
1160 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1172 if (!(t = new(int, rn_fds+cn_fds))) {
1178 memcpy(t, rfds, rn_fds);
1179 memcpy(t+rn_fds, cfds, cn_fds);
1184 rn_fds = rn_fds+cn_fds;
1202 static int service_spawn(
1207 bool apply_permissions,
1222 if (s->socket_fd >= 0) {
1223 fds = &s->socket_fd;
1225 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1229 if (timeout && s->timeout_usec) {
1230 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1233 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1235 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1246 UNIT(s)->meta.manager->confirm_spawn,
1247 UNIT(s)->meta.cgroup_bondings,
1255 if (s->socket_fd >= 0)
1256 service_close_socket_fd(s);
1261 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1262 /* FIXME: we need to do something here */
1273 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1278 static int main_pid_good(Service *s) {
1281 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1284 /* If we know the pid file, then lets just check if it is
1286 if (s->main_pid_known)
1287 return s->main_pid > 0;
1289 /* We don't know the pid */
1293 static int control_pid_good(Service *s) {
1296 return s->control_pid > 0;
1299 static int cgroup_good(Service *s) {
1304 if (s->valid_no_process)
1307 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1313 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1320 if (allow_restart &&
1321 (s->restart == SERVICE_RESTART_ALWAYS ||
1322 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1324 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1327 service_set_state(s, SERVICE_AUTO_RESTART);
1329 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1334 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
1335 service_enter_dead(s, false, false);
1338 static void service_enter_signal(Service *s, ServiceState state, bool success);
1340 static void service_enter_stop_post(Service *s, bool success) {
1347 service_unwatch_control_pid(s);
1349 s->control_command_id = SERVICE_EXEC_STOP_POST;
1350 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1351 if ((r = service_spawn(s,
1355 !s->permissions_start_only,
1356 !s->root_directory_start_only,
1357 &s->control_pid)) < 0)
1361 service_set_state(s, SERVICE_STOP_POST);
1363 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1368 log_warning("%s failed to run stop-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1369 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1372 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1381 if (s->kill_mode != KILL_NONE) {
1382 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1384 if (s->kill_mode == KILL_CONTROL_GROUP) {
1386 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1387 if (r != -EAGAIN && r != -ESRCH)
1396 if (s->main_pid > 0) {
1397 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1403 if (s->control_pid > 0) {
1404 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1416 if (s->timeout_usec > 0)
1417 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1420 service_set_state(s, state);
1421 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1422 service_enter_stop_post(s, true);
1424 service_enter_dead(s, true, true);
1429 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
1431 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1432 service_enter_stop_post(s, false);
1434 service_enter_dead(s, false, true);
1437 static void service_enter_stop(Service *s, bool success) {
1444 service_unwatch_control_pid(s);
1446 s->control_command_id = SERVICE_EXEC_STOP;
1447 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1448 if ((r = service_spawn(s,
1452 !s->permissions_start_only,
1453 !s->root_directory_start_only,
1454 &s->control_pid)) < 0)
1457 service_set_state(s, SERVICE_STOP);
1459 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1464 log_warning("%s failed to run stop executable: %s", UNIT(s)->meta.id, strerror(-r));
1465 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1468 static void service_enter_running(Service *s, bool success) {
1474 if (main_pid_good(s) != 0 &&
1475 cgroup_good(s) != 0 &&
1476 (s->bus_name_good || s->type != SERVICE_DBUS))
1477 service_set_state(s, SERVICE_RUNNING);
1478 else if (s->valid_no_process)
1479 service_set_state(s, SERVICE_EXITED);
1481 service_enter_stop(s, true);
1484 static void service_enter_start_post(Service *s) {
1488 service_unwatch_control_pid(s);
1490 s->control_command_id = SERVICE_EXEC_START_POST;
1491 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1492 if ((r = service_spawn(s,
1496 !s->permissions_start_only,
1497 !s->root_directory_start_only,
1498 &s->control_pid)) < 0)
1502 service_set_state(s, SERVICE_START_POST);
1504 service_enter_running(s, true);
1509 log_warning("%s failed to run start-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1510 service_enter_stop(s, false);
1513 static void service_enter_start(Service *s) {
1519 assert(s->exec_command[SERVICE_EXEC_START]);
1520 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1522 if (s->type == SERVICE_FORKING)
1523 service_unwatch_control_pid(s);
1525 service_unwatch_main_pid(s);
1527 if ((r = service_spawn(s,
1528 s->exec_command[SERVICE_EXEC_START],
1529 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS,
1536 if (s->type == SERVICE_SIMPLE) {
1537 /* For simple services we immediately start
1538 * the START_POST binaries. */
1541 s->main_pid_known = true;
1543 service_enter_start_post(s);
1545 } else if (s->type == SERVICE_FORKING) {
1547 /* For forking services we wait until the start
1548 * process exited. */
1550 s->control_pid = pid;
1552 s->control_command_id = SERVICE_EXEC_START;
1553 s->control_command = s->exec_command[SERVICE_EXEC_START];
1554 service_set_state(s, SERVICE_START);
1556 } else if (s->type == SERVICE_FINISH ||
1557 s->type == SERVICE_DBUS) {
1559 /* For finishing services we wait until the start
1560 * process exited, too, but it is our main process. */
1562 /* For D-Bus services we know the main pid right away,
1563 * but wait for the bus name to appear on the bus. */
1566 s->main_pid_known = true;
1568 service_set_state(s, SERVICE_START);
1570 assert_not_reached("Unknown service type");
1575 log_warning("%s failed to run start exectuable: %s", UNIT(s)->meta.id, strerror(-r));
1576 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1579 static void service_enter_start_pre(Service *s) {
1584 service_unwatch_control_pid(s);
1586 s->control_command_id = SERVICE_EXEC_START_PRE;
1587 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1588 if ((r = service_spawn(s,
1592 !s->permissions_start_only,
1593 !s->root_directory_start_only,
1594 &s->control_pid)) < 0)
1597 service_set_state(s, SERVICE_START_PRE);
1599 service_enter_start(s);
1604 log_warning("%s failed to run start-pre executable: %s", UNIT(s)->meta.id, strerror(-r));
1605 service_enter_dead(s, false, true);
1608 static void service_enter_restart(Service *s) {
1612 service_enter_dead(s, true, false);
1614 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1617 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
1622 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
1623 service_enter_dead(s, false, false);
1626 static void service_enter_reload(Service *s) {
1631 service_unwatch_control_pid(s);
1633 s->control_command_id = SERVICE_EXEC_RELOAD;
1634 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1635 if ((r = service_spawn(s,
1639 !s->permissions_start_only,
1640 !s->root_directory_start_only,
1641 &s->control_pid)) < 0)
1644 service_set_state(s, SERVICE_RELOAD);
1646 service_enter_running(s, true);
1651 log_warning("%s failed to run reload executable: %s", UNIT(s)->meta.id, strerror(-r));
1652 service_enter_stop(s, false);
1655 static void service_run_next(Service *s, bool success) {
1659 assert(s->control_command);
1660 assert(s->control_command->command_next);
1665 s->control_command = s->control_command->command_next;
1667 service_unwatch_control_pid(s);
1669 if ((r = service_spawn(s,
1673 !s->permissions_start_only,
1674 !s->root_directory_start_only,
1675 &s->control_pid)) < 0)
1681 log_warning("%s failed to run spawn next executable: %s", UNIT(s)->meta.id, strerror(-r));
1683 if (s->state == SERVICE_START_PRE)
1684 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1685 else if (s->state == SERVICE_STOP)
1686 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1687 else if (s->state == SERVICE_STOP_POST)
1688 service_enter_dead(s, false, true);
1690 service_enter_stop(s, false);
1693 static int service_start(Unit *u) {
1694 Service *s = SERVICE(u);
1698 /* We cannot fulfill this request right now, try again later
1700 if (s->state == SERVICE_STOP ||
1701 s->state == SERVICE_STOP_SIGTERM ||
1702 s->state == SERVICE_STOP_SIGKILL ||
1703 s->state == SERVICE_STOP_POST ||
1704 s->state == SERVICE_FINAL_SIGTERM ||
1705 s->state == SERVICE_FINAL_SIGKILL)
1708 /* Already on it! */
1709 if (s->state == SERVICE_START_PRE ||
1710 s->state == SERVICE_START ||
1711 s->state == SERVICE_START_POST)
1714 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1716 /* Make sure we don't enter a busy loop of some kind. */
1717 if (!ratelimit_test(&s->ratelimit)) {
1718 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1723 s->main_pid_known = false;
1725 service_enter_start_pre(s);
1729 static int service_stop(Unit *u) {
1730 Service *s = SERVICE(u);
1734 /* Cannot do this now */
1735 if (s->state == SERVICE_START_PRE ||
1736 s->state == SERVICE_START ||
1737 s->state == SERVICE_START_POST ||
1738 s->state == SERVICE_RELOAD)
1742 if (s->state == SERVICE_STOP ||
1743 s->state == SERVICE_STOP_SIGTERM ||
1744 s->state == SERVICE_STOP_SIGKILL ||
1745 s->state == SERVICE_STOP_POST ||
1746 s->state == SERVICE_FINAL_SIGTERM ||
1747 s->state == SERVICE_FINAL_SIGKILL)
1750 if (s->state == SERVICE_AUTO_RESTART) {
1751 service_set_state(s, SERVICE_DEAD);
1755 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1757 service_enter_stop(s, true);
1761 static int service_reload(Unit *u) {
1762 Service *s = SERVICE(u);
1766 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1768 service_enter_reload(s);
1772 static bool service_can_reload(Unit *u) {
1773 Service *s = SERVICE(u);
1777 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1780 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1781 Service *s = SERVICE(u);
1787 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1788 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1790 if (s->control_pid > 0)
1791 unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) (s->control_pid));
1793 if (s->main_pid > 0)
1794 unit_serialize_item_format(u, f, "main-pid", "%u", (unsigned) (s->main_pid));
1796 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1798 /* There's a minor uncleanliness here: if there are multiple
1799 * commands attached here, we will start from the first one
1801 if (s->control_command_id >= 0)
1802 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(s->control_command_id));
1804 if (s->socket_fd >= 0) {
1807 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1810 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1816 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1817 Service *s = SERVICE(u);
1825 if (streq(key, "state")) {
1828 if ((state = service_state_from_string(value)) < 0)
1829 log_debug("Failed to parse state value %s", value);
1831 s->deserialized_state = state;
1832 } else if (streq(key, "failure")) {
1835 if ((b = parse_boolean(value)) < 0)
1836 log_debug("Failed to parse failure value %s", value);
1838 s->failure = b || s->failure;
1839 } else if (streq(key, "control-pid")) {
1842 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1843 log_debug("Failed to parse control-pid value %s", value);
1845 s->control_pid = (pid_t) pid;
1846 } else if (streq(key, "main-pid")) {
1849 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1850 log_debug("Failed to parse main-pid value %s", value);
1852 s->main_pid = (pid_t) pid;
1853 } else if (streq(key, "main-pid-known")) {
1856 if ((b = parse_boolean(value)) < 0)
1857 log_debug("Failed to parse main-pid-known value %s", value);
1859 s->main_pid_known = b;
1860 } else if (streq(key, "control-command")) {
1861 ServiceExecCommand id;
1863 if ((id = service_exec_command_from_string(value)) < 0)
1864 log_debug("Failed to parse exec-command value %s", value);
1866 s->control_command_id = id;
1867 s->control_command = s->exec_command[id];
1869 } else if (streq(key, "socket-fd")) {
1872 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1873 log_debug("Failed to parse socket-fd value %s", value);
1876 if (s->socket_fd >= 0)
1877 close_nointr_nofail(s->socket_fd);
1878 s->socket_fd = fdset_remove(fds, fd);
1881 log_debug("Unknown serialization key '%s'", key);
1886 static UnitActiveState service_active_state(Unit *u) {
1889 return state_translation_table[SERVICE(u)->state];
1892 static const char *service_sub_state_to_string(Unit *u) {
1895 return service_state_to_string(SERVICE(u)->state);
1898 static bool service_check_gc(Unit *u) {
1899 Service *s = SERVICE(u);
1903 return !!s->sysv_path;
1906 static bool service_check_snapshot(Unit *u) {
1907 Service *s = SERVICE(u);
1911 return !s->got_socket_fd;
1914 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1915 Service *s = SERVICE(u);
1921 success = code == CLD_EXITED && status == 0;
1922 s->failure = s->failure || !success;
1924 if (s->main_pid == pid) {
1926 exec_status_fill(&s->main_exec_status, pid, code, status);
1929 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1930 assert(s->exec_command[SERVICE_EXEC_START]);
1931 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1934 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
1936 /* The service exited, so the service is officially
1941 case SERVICE_START_POST:
1942 case SERVICE_RELOAD:
1944 /* Need to wait until the operation is
1949 assert(s->type == SERVICE_FINISH);
1951 /* This was our main goal, so let's go on */
1953 service_enter_start_post(s);
1955 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1958 case SERVICE_RUNNING:
1959 service_enter_running(s, success);
1962 case SERVICE_STOP_SIGTERM:
1963 case SERVICE_STOP_SIGKILL:
1965 if (!control_pid_good(s))
1966 service_enter_stop_post(s, success);
1968 /* If there is still a control process, wait for that first */
1972 assert_not_reached("Uh, main process died at wrong time.");
1975 } else if (s->control_pid == pid) {
1977 if (s->control_command)
1978 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1982 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
1984 /* If we are shutting things down anyway we
1985 * don't care about failing commands. */
1987 if (s->control_command && s->control_command->command_next && success) {
1989 /* There is another command to *
1990 * execute, so let's do that. */
1992 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
1993 service_run_next(s, success);
1996 /* No further commands for this step, so let's
1997 * figure out what to do next */
1999 s->control_command = NULL;
2000 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2002 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2006 case SERVICE_START_PRE:
2008 service_enter_start(s);
2010 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2014 assert(s->type == SERVICE_FORKING);
2016 /* Let's try to load the pid
2017 * file here if we can. We
2018 * ignore the return value,
2019 * since the PID file might
2020 * actually be created by a
2021 * START_POST script */
2025 service_load_pid_file(s);
2027 service_enter_start_post(s);
2029 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2033 case SERVICE_START_POST:
2034 if (success && s->pid_file && !s->main_pid_known) {
2037 /* Hmm, let's see if we can
2038 * load the pid now after the
2039 * start-post scripts got
2042 if ((r = service_load_pid_file(s)) < 0)
2043 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
2048 case SERVICE_RELOAD:
2050 service_enter_running(s, true);
2052 service_enter_stop(s, false);
2057 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2060 case SERVICE_STOP_SIGTERM:
2061 case SERVICE_STOP_SIGKILL:
2062 if (main_pid_good(s) <= 0)
2063 service_enter_stop_post(s, success);
2065 /* If there is still a service
2066 * process around, wait until
2067 * that one quit, too */
2070 case SERVICE_STOP_POST:
2071 case SERVICE_FINAL_SIGTERM:
2072 case SERVICE_FINAL_SIGKILL:
2073 service_enter_dead(s, success, true);
2077 assert_not_reached("Uh, control process died at wrong time.");
2081 assert_not_reached("Got SIGCHLD for unkown PID");
2084 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2085 Service *s = SERVICE(u);
2088 assert(elapsed == 1);
2090 assert(w == &s->timer_watch);
2094 case SERVICE_START_PRE:
2096 log_warning("%s operation timed out. Terminating.", u->meta.id);
2097 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2100 case SERVICE_START_POST:
2101 case SERVICE_RELOAD:
2102 log_warning("%s operation timed out. Stopping.", u->meta.id);
2103 service_enter_stop(s, false);
2107 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2108 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2111 case SERVICE_STOP_SIGTERM:
2112 log_warning("%s stopping timed out. Killing.", u->meta.id);
2113 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2116 case SERVICE_STOP_SIGKILL:
2117 /* Uh, wie sent a SIGKILL and it is still not gone?
2118 * Must be something we cannot kill, so let's just be
2119 * weirded out and continue */
2121 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2122 service_enter_stop_post(s, false);
2125 case SERVICE_STOP_POST:
2126 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2127 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2130 case SERVICE_FINAL_SIGTERM:
2131 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2132 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2135 case SERVICE_FINAL_SIGKILL:
2136 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u->meta.id);
2137 service_enter_dead(s, false, true);
2140 case SERVICE_AUTO_RESTART:
2141 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
2142 service_enter_restart(s);
2146 assert_not_reached("Timeout at wrong time.");
2150 static void service_cgroup_notify_event(Unit *u) {
2151 Service *s = SERVICE(u);
2155 log_debug("%s: cgroup is empty", u->meta.id);
2159 /* Waiting for SIGCHLD is usually more interesting,
2160 * because it includes return codes/signals. Which is
2161 * why we ignore the cgroup events for most cases,
2162 * except when we don't know pid which to expect the
2165 case SERVICE_RUNNING:
2166 service_enter_running(s, true);
2174 static int service_enumerate(Manager *m) {
2178 char *path = NULL, *fpath = NULL, *name = NULL;
2183 STRV_FOREACH(p, m->sysvrcnd_path)
2184 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
2189 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0) {
2197 if (!(d = opendir(path))) {
2198 if (errno != ENOENT)
2199 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2204 while ((de = readdir(d))) {
2207 if (ignore_file(de->d_name))
2210 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2213 if (strlen(de->d_name) < 4)
2218 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i], de->d_name) < 0) {
2223 if (access(fpath, X_OK) < 0) {
2225 if (errno != ENOENT)
2226 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2233 if (asprintf(&name, "%s.service", de->d_name+3) < 0) {
2238 if ((r = manager_load_unit(m, name, NULL, &service)) < 0)
2241 if (de->d_name[0] == 'S') {
2242 Unit *runlevel_target;
2244 if ((r = manager_load_unit(m, rcnd_table[i+1], NULL, &runlevel_target)) < 0)
2247 if ((r = unit_add_dependency(runlevel_target, UNIT_WANTS, service, true)) < 0)
2250 if ((r = unit_add_dependency(runlevel_target, UNIT_AFTER, service, true)) < 0)
2253 } else if (de->d_name[0] == 'K' &&
2254 (streq(rcnd_table[i+1], SPECIAL_RUNLEVEL0_TARGET) ||
2255 streq(rcnd_table[i+1], SPECIAL_RUNLEVEL6_TARGET))) {
2257 Unit *shutdown_target;
2259 /* We honour K links only for
2260 * halt/reboot. For the normal
2261 * runlevels we assume the
2263 * implicitly added by the
2264 * core logic. Also, we don't
2265 * really distuingish here
2266 * between the runlevels 0 and
2267 * 6 and just add them to the
2268 * special shutdown target. */
2270 if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
2273 if ((r = unit_add_dependency(shutdown_target, UNIT_CONFLICTS, service, true)) < 0)
2276 if ((r = unit_add_dependency(shutdown_target, UNIT_BEFORE, service, true)) < 0)
2293 static void service_bus_name_owner_change(
2296 const char *old_owner,
2297 const char *new_owner) {
2299 Service *s = SERVICE(u);
2304 assert(streq(s->bus_name, name));
2305 assert(old_owner || new_owner);
2307 if (old_owner && new_owner)
2308 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2310 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2312 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2314 s->bus_name_good = !!new_owner;
2316 if (s->type == SERVICE_DBUS) {
2318 /* service_enter_running() will figure out what to
2320 if (s->state == SERVICE_RUNNING)
2321 service_enter_running(s, true);
2322 else if (s->state == SERVICE_START && new_owner)
2323 service_enter_start_post(s);
2325 } else if (new_owner &&
2327 (s->state == SERVICE_START ||
2328 s->state == SERVICE_START_POST ||
2329 s->state == SERVICE_RUNNING ||
2330 s->state == SERVICE_RELOAD)) {
2332 /* Try to acquire PID from bus service */
2333 log_debug("Trying to acquire PID from D-Bus name...");
2335 bus_query_pid(u->meta.manager, name);
2339 static void service_bus_query_pid_done(
2344 Service *s = SERVICE(u);
2349 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2351 if (s->main_pid <= 0 &&
2352 (s->state == SERVICE_START ||
2353 s->state == SERVICE_START_POST ||
2354 s->state == SERVICE_RUNNING ||
2355 s->state == SERVICE_RELOAD))
2359 int service_set_socket_fd(Service *s, int fd) {
2363 /* This is called by the socket code when instantiating a new
2364 * service for a stream socket and the socket needs to be
2367 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2370 if (s->socket_fd >= 0)
2373 if (s->state != SERVICE_DEAD)
2377 s->got_socket_fd = true;
2381 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2382 [SERVICE_DEAD] = "dead",
2383 [SERVICE_START_PRE] = "start-pre",
2384 [SERVICE_START] = "start",
2385 [SERVICE_START_POST] = "start-post",
2386 [SERVICE_RUNNING] = "running",
2387 [SERVICE_EXITED] = "exited",
2388 [SERVICE_RELOAD] = "reload",
2389 [SERVICE_STOP] = "stop",
2390 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2391 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2392 [SERVICE_STOP_POST] = "stop-post",
2393 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2394 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2395 [SERVICE_MAINTAINANCE] = "maintainance",
2396 [SERVICE_AUTO_RESTART] = "auto-restart",
2399 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2401 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2402 [SERVICE_ONCE] = "once",
2403 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2404 [SERVICE_RESTART_ALWAYS] = "restart-always",
2407 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2409 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2410 [SERVICE_FORKING] = "forking",
2411 [SERVICE_SIMPLE] = "simple",
2412 [SERVICE_FINISH] = "finish",
2413 [SERVICE_DBUS] = "dbus"
2416 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2418 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2419 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2420 [SERVICE_EXEC_START] = "ExecStart",
2421 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2422 [SERVICE_EXEC_RELOAD] = "ExecReload",
2423 [SERVICE_EXEC_STOP] = "ExecStop",
2424 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2427 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2429 const UnitVTable service_vtable = {
2430 .suffix = ".service",
2432 .init = service_init,
2433 .done = service_done,
2434 .load = service_load,
2436 .coldplug = service_coldplug,
2438 .dump = service_dump,
2440 .start = service_start,
2441 .stop = service_stop,
2442 .reload = service_reload,
2444 .can_reload = service_can_reload,
2446 .serialize = service_serialize,
2447 .deserialize_item = service_deserialize_item,
2449 .active_state = service_active_state,
2450 .sub_state_to_string = service_sub_state_to_string,
2452 .check_gc = service_check_gc,
2453 .check_snapshot = service_check_snapshot,
2455 .sigchld_event = service_sigchld_event,
2456 .timer_event = service_timer_event,
2458 .cgroup_notify_empty = service_cgroup_notify_event,
2460 .bus_name_owner_change = service_bus_name_owner_change,
2461 .bus_query_pid_done = service_bus_query_pid_done,
2463 .bus_message_handler = bus_service_message_handler,
2465 .enumerate = service_enumerate