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"
35 #define COMMENTS "#;\n"
36 #define NEWLINES "\n\r"
39 static const char * const rcnd_table[] = {
40 "/rc0.d", SPECIAL_RUNLEVEL0_TARGET,
41 "/rc1.d", SPECIAL_RUNLEVEL1_TARGET,
42 "/rc2.d", SPECIAL_RUNLEVEL2_TARGET,
43 "/rc3.d", SPECIAL_RUNLEVEL3_TARGET,
44 "/rc4.d", SPECIAL_RUNLEVEL4_TARGET,
45 "/rc5.d", SPECIAL_RUNLEVEL5_TARGET,
46 "/rc6.d", SPECIAL_RUNLEVEL6_TARGET,
47 "/boot.d", SPECIAL_BASIC_TARGET
50 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
51 [SERVICE_DEAD] = UNIT_INACTIVE,
52 [SERVICE_START_PRE] = UNIT_ACTIVATING,
53 [SERVICE_START] = UNIT_ACTIVATING,
54 [SERVICE_START_POST] = UNIT_ACTIVATING,
55 [SERVICE_RUNNING] = UNIT_ACTIVE,
56 [SERVICE_EXITED] = UNIT_ACTIVE,
57 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
58 [SERVICE_STOP] = UNIT_DEACTIVATING,
59 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
60 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
61 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
62 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
63 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
64 [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
65 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
68 static void service_unwatch_control_pid(Service *s) {
71 if (s->control_pid <= 0)
74 unit_unwatch_pid(UNIT(s), s->control_pid);
78 static void service_unwatch_main_pid(Service *s) {
84 unit_unwatch_pid(UNIT(s), s->main_pid);
88 static void service_done(Unit *u) {
89 Service *s = SERVICE(u);
99 free(s->sysv_runlevels);
100 s->sysv_runlevels = NULL;
102 exec_context_done(&s->exec_context);
103 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
104 s->control_command = NULL;
106 /* This will leak a process, but at least no memory or any of
108 service_unwatch_main_pid(s);
109 service_unwatch_control_pid(s);
111 unit_unwatch_timer(u, &s->timer_watch);
114 static int sysv_translate_name(const char *name, char **_r) {
116 static const char * const table[] = {
117 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
118 "$network", SPECIAL_NETWORK_TARGET,
119 "$named", SPECIAL_NSS_LOOKUP_TARGET,
120 "$portmap", SPECIAL_RPCBIND_TARGET,
121 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
122 "$syslog", SPECIAL_SYSLOG_TARGET,
123 "$time", SPECIAL_RTC_SET_TARGET
129 for (i = 0; i < ELEMENTSOF(table); i += 2)
130 if (streq(table[i], name)) {
131 if (!(r = strdup(table[i+1])))
140 if (asprintf(&r, "%s.service", name) < 0)
151 static int sysv_chkconfig_order(Service *s) {
157 if (s->sysv_start_priority < 0)
160 /* For each pair of services where at least one lacks a LSB
161 * header, we use the start priority value to order things. */
163 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
167 t = (Service*) other;
172 if (t->sysv_start_priority < 0)
175 /* If both units have modern headers we don't care
176 * about the priorities */
177 if ((!s->sysv_path || s->sysv_has_lsb) &&
178 (!t->sysv_path || t->sysv_has_lsb))
181 if (t->sysv_start_priority < s->sysv_start_priority)
183 else if (t->sysv_start_priority > s->sysv_start_priority)
188 /* FIXME: Maybe we should compare the name here lexicographically? */
190 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t))) < 0)
197 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
200 if (!(c = new0(ExecCommand, 1)))
203 if (!(c->path = strdup(path))) {
208 if (!(c->argv = strv_new(path, arg1, NULL))) {
217 static int sysv_exec_commands(Service *s) {
221 assert(s->sysv_path);
223 if (!(c = exec_command_new(s->sysv_path, "start")))
225 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
227 if (!(c = exec_command_new(s->sysv_path, "stop")))
229 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
231 if (!(c = exec_command_new(s->sysv_path, "reload")))
233 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
238 static int priority_from_rcd(Service *s, const char *init_script) {
242 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvrcnd_path)
243 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
248 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0)
256 log_warning("opendir() failed on %s: %s", path, strerror(errno));
261 while ((de = readdir(d))) {
264 if (ignore_file(de->d_name))
267 if (de->d_name[0] != 'S')
270 if (strlen(de->d_name) < 4)
273 if (!streq(de->d_name + 3, init_script))
276 /* Yay, we found it! Now decode the priority */
278 a = undecchar(de->d_name[1]);
279 b = undecchar(de->d_name[2]);
284 s->sysv_start_priority = a*10 + b;
286 log_debug("Determined priority %i from link farm for %s", s->sysv_start_priority, UNIT(s)->meta.id);
298 static int service_load_sysv_path(Service *s, const char *path) {
315 if (!(f = fopen(path, "re"))) {
316 r = errno == ENOENT ? 0 : -errno;
320 s->type = SERVICE_FORKING;
321 s->restart = SERVICE_ONCE;
324 if (!(s->sysv_path = strdup(path))) {
330 char l[LINE_MAX], *t;
332 if (!fgets(l, sizeof(l), f)) {
337 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
347 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
349 s->sysv_has_lsb = true;
353 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
359 t += strspn(t, WHITESPACE);
361 if (state == NORMAL) {
363 /* Try to parse Red Hat style chkconfig headers */
365 if (startswith(t, "chkconfig:")) {
367 char runlevels[16], *k;
371 if (sscanf(t+10, "%15s %i %*i",
373 &start_priority) != 2) {
375 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
379 if (start_priority < 0 || start_priority > 99)
380 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
382 s->sysv_start_priority = start_priority;
384 char_array_0(runlevels);
385 k = delete_chars(runlevels, WHITESPACE "-");
390 if (!(d = strdup(k))) {
395 free(s->sysv_runlevels);
396 s->sysv_runlevels = d;
400 } else if (startswith(t, "description:")) {
402 size_t k = strlen(t);
405 if (t[k-1] == '\\') {
410 if (!(d = strdup(strstrip(t+12)))) {
415 free(u->meta.description);
416 u->meta.description = d;
418 } else if (startswith(t, "pidfile:")) {
425 if (!path_is_absolute(fn)) {
426 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
430 if (!(fn = strdup(fn))) {
439 } else if (state == DESCRIPTION) {
441 /* Try to parse Red Hat style description
444 size_t k = strlen(t);
452 assert(u->meta.description);
453 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
458 free(u->meta.description);
459 u->meta.description = d;
461 } else if (state == LSB || state == LSB_DESCRIPTION) {
463 if (startswith(t, "Provides:")) {
469 FOREACH_WORD(w, z, t+9, i) {
472 if (!(n = strndup(w, z))) {
477 r = sysv_translate_name(n, &m);
486 if (unit_name_to_type(m) == UNIT_SERVICE)
487 r = unit_add_name(u, m);
489 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL)) >= 0)
490 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL);
499 } else if (startswith(t, "Required-Start:") ||
500 startswith(t, "Should-Start:")) {
506 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
509 if (!(n = strndup(w, z))) {
514 r = sysv_translate_name(n, &m);
523 r = unit_add_dependency_by_name(u, UNIT_AFTER, m, NULL);
529 } else if (startswith(t, "Default-Start:")) {
534 k = delete_chars(t+14, WHITESPACE "-");
537 if (!(d = strdup(k))) {
542 free(s->sysv_runlevels);
543 s->sysv_runlevels = d;
546 } else if (startswith(t, "Description:")) {
549 state = LSB_DESCRIPTION;
551 if (!(d = strdup(strstrip(t+12)))) {
556 free(u->meta.description);
557 u->meta.description = d;
559 } else if (startswith(t, "Short-Description:") &&
560 !u->meta.description) {
563 /* We use the short description only
564 * if no long description is set. */
568 if (!(d = strdup(strstrip(t+18)))) {
573 u->meta.description = d;
575 } else if (state == LSB_DESCRIPTION) {
577 if (startswith(l, "#\t") || startswith(l, "# ")) {
580 assert(u->meta.description);
581 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
586 free(u->meta.description);
587 u->meta.description = d;
594 /* If init scripts have no LSB header, then we enforce the
595 * ordering via the chkconfig priorities. We try to determine
596 * a priority for *all* init scripts here, since they are
597 * needed as soon as at least one non-LSB script is used. */
599 if (s->sysv_start_priority < 0) {
600 log_debug("%s has no chkconfig header, trying to determine SysV priority from link farm.", u->meta.id);
602 if ((r = priority_from_rcd(s, file_name_from_path(path))) < 0)
605 if (s->sysv_start_priority < 0)
606 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", u->meta.id);
609 if ((r = sysv_exec_commands(s)) < 0)
612 if (!s->sysv_runlevels || chars_intersect("12345", s->sysv_runlevels)) {
613 /* If there a runlevels configured for this service
614 * but none of the standard ones, then we assume this
615 * is some special kind of service (which might be
616 * needed for early boot) and don't create any links
619 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL)) < 0 ||
620 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL)) < 0)
624 /* Special setting for all SysV services */
625 s->valid_no_process = true;
627 /* Don't timeout special services during boot (like fsck) */
628 if (s->sysv_runlevels && !chars_intersect("12345", s->sysv_runlevels))
629 s->timeout_usec = -1;
631 u->meta.load_state = UNIT_LOADED;
642 static int service_load_sysv_name(Service *s, const char *name) {
648 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
652 if (asprintf(&path, "%s/%s", *p, name) < 0)
655 assert(endswith(path, ".service"));
656 path[strlen(path)-8] = 0;
658 r = service_load_sysv_path(s, path);
664 if ((UNIT(s)->meta.load_state != UNIT_STUB))
671 static int service_load_sysv(Service *s) {
678 /* Load service data from SysV init scripts, preferably with
681 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
684 if ((t = UNIT(s)->meta.id))
685 if ((r = service_load_sysv_name(s, t)) < 0)
688 if (UNIT(s)->meta.load_state == UNIT_STUB)
689 SET_FOREACH(t, UNIT(s)->meta.names, i) {
690 if (t == UNIT(s)->meta.id)
693 if ((r == service_load_sysv_name(s, t)) < 0)
696 if (UNIT(s)->meta.load_state != UNIT_STUB)
703 static void service_init(Unit *u) {
704 Service *s = SERVICE(u);
707 assert(u->meta.load_state == UNIT_STUB);
712 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
713 s->restart_usec = DEFAULT_RESTART_USEC;
715 exec_context_init(&s->exec_context);
717 s->timer_watch.type = WATCH_INVALID;
719 s->state = SERVICE_DEAD;
721 s->sysv_start_priority = -1;
722 s->permissions_start_only = false;
723 s->root_directory_start_only = false;
724 s->valid_no_process = false;
726 s->sysv_has_lsb = false;
727 s->main_pid = s->control_pid = 0;
728 s->main_pid_known = false;
731 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
734 static int service_verify(Service *s) {
737 if (UNIT(s)->meta.load_state != UNIT_LOADED)
740 if (!s->exec_command[SERVICE_EXEC_START]) {
741 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
748 static int service_load(Unit *u) {
750 Service *s = SERVICE(u);
754 /* Load a .service file */
755 if ((r = unit_load_fragment(u)) < 0)
758 /* Load a classic init script as a fallback, if we couldn't find anything */
759 if (u->meta.load_state == UNIT_STUB)
760 if ((r = service_load_sysv(s)) < 0)
763 /* Still nothing found? Then let's give up */
764 if (u->meta.load_state == UNIT_STUB)
767 /* We were able to load something, then let's add in the
768 * dropin directories. */
769 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
772 /* This is a new unit? Then let's add in some extras */
773 if (u->meta.load_state == UNIT_LOADED) {
774 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
777 if ((r = unit_add_default_cgroup(u)) < 0)
780 if ((r = sysv_chkconfig_order(s)) < 0)
784 return service_verify(s);
787 static void service_dump(Unit *u, FILE *f, const char *prefix) {
789 ServiceExecCommand c;
790 Service *s = SERVICE(u);
796 p2 = strappend(prefix, "\t");
797 prefix2 = p2 ? p2 : prefix;
800 "%sService State: %s\n"
801 "%sPermissionsStartOnly: %s\n"
802 "%sRootDirectoryStartOnly: %s\n"
803 "%sValidNoProcess: %s\n"
806 prefix, service_state_to_string(s->state),
807 prefix, yes_no(s->permissions_start_only),
808 prefix, yes_no(s->root_directory_start_only),
809 prefix, yes_no(s->valid_no_process),
810 prefix, kill_mode_to_string(s->kill_mode),
811 prefix, service_type_to_string(s->type));
813 if (s->control_pid > 0)
815 "%sControl PID: %llu\n",
816 prefix, (unsigned long long) s->control_pid);
820 "%sMain PID: %llu\n",
821 prefix, (unsigned long long) s->main_pid);
826 prefix, s->pid_file);
828 exec_context_dump(&s->exec_context, f, prefix);
830 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
832 if (!s->exec_command[c])
835 fprintf(f, "%s→ %s:\n",
836 prefix, service_exec_command_to_string(c));
838 exec_command_dump_list(s->exec_command[c], f, prefix2);
843 "%sSysV Init Script Path: %s\n"
844 "%sSysV Init Script has LSB Header: %s\n",
845 prefix, s->sysv_path,
846 prefix, yes_no(s->sysv_has_lsb));
848 if (s->sysv_start_priority >= 0)
850 "%sSysVStartPriority: %i\n",
851 prefix, s->sysv_start_priority);
853 if (s->sysv_runlevels)
854 fprintf(f, "%sSysVRunLevels: %s\n",
855 prefix, s->sysv_runlevels);
860 static int service_load_pid_file(Service *s) {
867 if (s->main_pid_known)
870 assert(s->main_pid <= 0);
875 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
878 if ((r = safe_atolu(k, &p)) < 0) {
883 if ((unsigned long) (pid_t) p != p)
886 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
887 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
888 (unsigned long long) p, s->pid_file);
892 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
893 /* FIXME: we need to do something here */
896 s->main_pid = (pid_t) p;
897 s->main_pid_known = true;
902 static int service_get_sockets(Service *s, Set **_set) {
911 /* Collects all Socket objects that belong to this
912 * service. Note that a service might have multiple sockets
913 * via multiple names. */
915 if (!(set = set_new(NULL, NULL)))
918 SET_FOREACH(t, UNIT(s)->meta.names, i) {
922 /* Look for all socket objects that go by any of our
923 * units and collect their fds */
925 if (!(k = unit_name_change_suffix(t, ".socket"))) {
930 p = manager_get_unit(UNIT(s)->meta.manager, k);
935 if ((r = set_put(set, p)) < 0)
948 static int service_notify_sockets_dead(Service *s) {
956 /* Notifies all our sockets when we die */
958 if ((r = service_get_sockets(s, &set)) < 0)
961 SET_FOREACH(sock, set, i)
962 socket_notify_service_dead(sock);
969 static void service_set_state(Service *s, ServiceState state) {
970 ServiceState old_state;
973 old_state = s->state;
976 if (state != SERVICE_START_PRE &&
977 state != SERVICE_START &&
978 state != SERVICE_START_POST &&
979 state != SERVICE_RELOAD &&
980 state != SERVICE_STOP &&
981 state != SERVICE_STOP_SIGTERM &&
982 state != SERVICE_STOP_SIGKILL &&
983 state != SERVICE_STOP_POST &&
984 state != SERVICE_FINAL_SIGTERM &&
985 state != SERVICE_FINAL_SIGKILL &&
986 state != SERVICE_AUTO_RESTART)
987 unit_unwatch_timer(UNIT(s), &s->timer_watch);
989 if (state != SERVICE_START &&
990 state != SERVICE_START_POST &&
991 state != SERVICE_RUNNING &&
992 state != SERVICE_RELOAD &&
993 state != SERVICE_STOP &&
994 state != SERVICE_STOP_SIGTERM &&
995 state != SERVICE_STOP_SIGKILL)
996 service_unwatch_main_pid(s);
998 if (state != SERVICE_START_PRE &&
999 state != SERVICE_START &&
1000 state != SERVICE_START_POST &&
1001 state != SERVICE_RELOAD &&
1002 state != SERVICE_STOP &&
1003 state != SERVICE_STOP_SIGTERM &&
1004 state != SERVICE_STOP_SIGKILL &&
1005 state != SERVICE_STOP_POST &&
1006 state != SERVICE_FINAL_SIGTERM &&
1007 state != SERVICE_FINAL_SIGKILL) {
1008 service_unwatch_control_pid(s);
1009 s->control_command = NULL;
1012 if (state == SERVICE_DEAD ||
1013 state == SERVICE_STOP ||
1014 state == SERVICE_STOP_SIGTERM ||
1015 state == SERVICE_STOP_SIGKILL ||
1016 state == SERVICE_STOP_POST ||
1017 state == SERVICE_FINAL_SIGTERM ||
1018 state == SERVICE_FINAL_SIGKILL ||
1019 state == SERVICE_MAINTAINANCE ||
1020 state == SERVICE_AUTO_RESTART)
1021 service_notify_sockets_dead(s);
1023 if (old_state != state)
1024 log_debug("%s changed %s → %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1026 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1029 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1033 unsigned rn_fds = 0;
1041 if ((r = service_get_sockets(s, &set)) < 0)
1044 SET_FOREACH(sock, set, i) {
1048 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1060 if (!(t = new(int, rn_fds+cn_fds))) {
1066 memcpy(t, rfds, rn_fds);
1067 memcpy(t+rn_fds, cfds, cn_fds);
1072 rn_fds = rn_fds+cn_fds;
1090 static int service_spawn(
1095 bool apply_permissions,
1110 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1114 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1117 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1119 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1130 UNIT(s)->meta.manager->confirm_spawn,
1131 UNIT(s)->meta.cgroup_bondings,
1138 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1139 /* FIXME: we need to do something here */
1151 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1156 static int main_pid_good(Service *s) {
1159 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1162 /* If we know the pid file, then lets just check if it is
1164 if (s->main_pid_known)
1165 return s->main_pid > 0;
1167 /* We don't know the pid */
1171 static int control_pid_good(Service *s) {
1174 return s->control_pid > 0;
1177 static int cgroup_good(Service *s) {
1182 if (s->valid_no_process)
1185 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1191 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1198 if (allow_restart &&
1199 (s->restart == SERVICE_RESTART_ALWAYS ||
1200 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1202 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1205 service_set_state(s, SERVICE_AUTO_RESTART);
1207 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1212 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
1213 service_enter_dead(s, false, false);
1216 static void service_enter_signal(Service *s, ServiceState state, bool success);
1218 static void service_enter_stop_post(Service *s, bool success) {
1225 service_unwatch_control_pid(s);
1227 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1228 if ((r = service_spawn(s,
1232 !s->permissions_start_only,
1233 !s->root_directory_start_only,
1234 &s->control_pid)) < 0)
1238 service_set_state(s, SERVICE_STOP_POST);
1240 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1245 log_warning("%s failed to run stop-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1246 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1249 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1258 if (s->kill_mode != KILL_NONE) {
1259 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1261 if (s->kill_mode == KILL_CONTROL_GROUP) {
1263 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1264 if (r != -EAGAIN && r != -ESRCH)
1273 if (s->main_pid > 0) {
1274 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1280 if (s->control_pid > 0) {
1281 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1293 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1296 service_set_state(s, state);
1297 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1298 service_enter_stop_post(s, true);
1300 service_enter_dead(s, true, true);
1305 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
1307 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1308 service_enter_stop_post(s, false);
1310 service_enter_dead(s, false, true);
1313 static void service_enter_stop(Service *s, bool success) {
1320 service_unwatch_control_pid(s);
1322 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1323 if ((r = service_spawn(s,
1327 !s->permissions_start_only,
1328 !s->root_directory_start_only,
1329 &s->control_pid)) < 0)
1332 service_set_state(s, SERVICE_STOP);
1334 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1339 log_warning("%s failed to run stop executable: %s", UNIT(s)->meta.id, strerror(-r));
1340 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1343 static void service_enter_running(Service *s, bool success) {
1349 if (main_pid_good(s) != 0 && cgroup_good(s) != 0)
1350 service_set_state(s, SERVICE_RUNNING);
1351 else if (s->valid_no_process)
1352 service_set_state(s, SERVICE_EXITED);
1354 service_enter_stop(s, true);
1357 static void service_enter_start_post(Service *s) {
1361 service_unwatch_control_pid(s);
1363 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1364 if ((r = service_spawn(s,
1368 !s->permissions_start_only,
1369 !s->root_directory_start_only,
1370 &s->control_pid)) < 0)
1374 service_set_state(s, SERVICE_START_POST);
1376 service_enter_running(s, true);
1381 log_warning("%s failed to run start-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1382 service_enter_stop(s, false);
1385 static void service_enter_start(Service *s) {
1391 assert(s->exec_command[SERVICE_EXEC_START]);
1392 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1394 if (s->type == SERVICE_FORKING)
1395 service_unwatch_control_pid(s);
1397 service_unwatch_main_pid(s);
1399 if ((r = service_spawn(s,
1400 s->exec_command[SERVICE_EXEC_START],
1401 s->type == SERVICE_FORKING,
1408 if (s->type == SERVICE_SIMPLE) {
1409 /* For simple services we immediately start
1410 * the START_POST binaries. */
1413 s->main_pid_known = true;
1415 service_enter_start_post(s);
1417 } else if (s->type == SERVICE_FORKING) {
1419 /* For forking services we wait until the start
1420 * process exited. */
1422 s->control_pid = pid;
1424 s->control_command = s->exec_command[SERVICE_EXEC_START];
1425 service_set_state(s, SERVICE_START);
1427 } else if (s->type == SERVICE_FINISH) {
1429 /* For finishing services we wait until the start
1430 * process exited, too, but it is our main process. */
1433 s->main_pid_known = true;
1435 s->control_command = s->exec_command[SERVICE_EXEC_START];
1436 service_set_state(s, SERVICE_START);
1438 assert_not_reached("Unknown service type");
1443 log_warning("%s failed to run start exectuable: %s", UNIT(s)->meta.id, strerror(-r));
1444 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1447 static void service_enter_start_pre(Service *s) {
1452 service_unwatch_control_pid(s);
1454 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1455 if ((r = service_spawn(s,
1459 !s->permissions_start_only,
1460 !s->root_directory_start_only,
1461 &s->control_pid)) < 0)
1464 service_set_state(s, SERVICE_START_PRE);
1466 service_enter_start(s);
1471 log_warning("%s failed to run start-pre executable: %s", UNIT(s)->meta.id, strerror(-r));
1472 service_enter_dead(s, false, true);
1475 static void service_enter_restart(Service *s) {
1479 service_enter_dead(s, true, false);
1481 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1484 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
1489 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
1490 service_enter_dead(s, false, false);
1493 static void service_enter_reload(Service *s) {
1498 service_unwatch_control_pid(s);
1500 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1501 if ((r = service_spawn(s,
1505 !s->permissions_start_only,
1506 !s->root_directory_start_only,
1507 &s->control_pid)) < 0)
1510 service_set_state(s, SERVICE_RELOAD);
1512 service_enter_running(s, true);
1517 log_warning("%s failed to run reload executable: %s", UNIT(s)->meta.id, strerror(-r));
1518 service_enter_stop(s, false);
1521 static void service_run_next(Service *s, bool success) {
1525 assert(s->control_command);
1526 assert(s->control_command->command_next);
1531 s->control_command = s->control_command->command_next;
1533 service_unwatch_control_pid(s);
1535 if ((r = service_spawn(s,
1539 !s->permissions_start_only,
1540 !s->root_directory_start_only,
1541 &s->control_pid)) < 0)
1547 log_warning("%s failed to run spawn next executable: %s", UNIT(s)->meta.id, strerror(-r));
1549 if (s->state == SERVICE_START_PRE)
1550 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1551 else if (s->state == SERVICE_STOP)
1552 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1553 else if (s->state == SERVICE_STOP_POST)
1554 service_enter_dead(s, false, true);
1556 service_enter_stop(s, false);
1559 static int service_start(Unit *u) {
1560 Service *s = SERVICE(u);
1564 /* We cannot fulfill this request right now, try again later
1566 if (s->state == SERVICE_STOP ||
1567 s->state == SERVICE_STOP_SIGTERM ||
1568 s->state == SERVICE_STOP_SIGKILL ||
1569 s->state == SERVICE_STOP_POST ||
1570 s->state == SERVICE_FINAL_SIGTERM ||
1571 s->state == SERVICE_FINAL_SIGKILL)
1574 /* Already on it! */
1575 if (s->state == SERVICE_START_PRE ||
1576 s->state == SERVICE_START ||
1577 s->state == SERVICE_START_POST)
1580 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1582 /* Make sure we don't enter a busy loop of some kind. */
1583 if (!ratelimit_test(&s->ratelimit)) {
1584 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1589 s->main_pid_known = false;
1591 service_enter_start_pre(s);
1595 static int service_stop(Unit *u) {
1596 Service *s = SERVICE(u);
1600 /* Cannot do this now */
1601 if (s->state == SERVICE_START_PRE ||
1602 s->state == SERVICE_START ||
1603 s->state == SERVICE_START_POST ||
1604 s->state == SERVICE_RELOAD)
1608 if (s->state == SERVICE_STOP ||
1609 s->state == SERVICE_STOP_SIGTERM ||
1610 s->state == SERVICE_STOP_SIGKILL ||
1611 s->state == SERVICE_STOP_POST ||
1612 s->state == SERVICE_FINAL_SIGTERM ||
1613 s->state == SERVICE_FINAL_SIGKILL)
1616 if (s->state == SERVICE_AUTO_RESTART) {
1617 service_set_state(s, SERVICE_DEAD);
1621 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1623 service_enter_stop(s, true);
1627 static int service_reload(Unit *u) {
1628 Service *s = SERVICE(u);
1632 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1634 service_enter_reload(s);
1638 static bool service_can_reload(Unit *u) {
1639 Service *s = SERVICE(u);
1643 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1646 static UnitActiveState service_active_state(Unit *u) {
1649 return state_translation_table[SERVICE(u)->state];
1652 static const char *service_sub_state_to_string(Unit *u) {
1655 return service_state_to_string(SERVICE(u)->state);
1658 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1659 Service *s = SERVICE(u);
1665 success = code == CLD_EXITED && status == 0;
1666 s->failure = s->failure || !success;
1668 if (s->main_pid == pid) {
1670 exec_status_fill(&s->main_exec_status, pid, code, status);
1673 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1674 assert(s->exec_command[SERVICE_EXEC_START]);
1675 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1678 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
1680 /* The service exited, so the service is officially
1685 case SERVICE_START_POST:
1686 case SERVICE_RELOAD:
1688 /* Need to wait until the operation is
1693 assert(s->type == SERVICE_FINISH);
1695 /* This was our main goal, so let's go on */
1697 service_enter_start_post(s);
1699 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1702 case SERVICE_RUNNING:
1703 service_enter_running(s, success);
1706 case SERVICE_STOP_SIGTERM:
1707 case SERVICE_STOP_SIGKILL:
1709 if (!control_pid_good(s))
1710 service_enter_stop_post(s, success);
1712 /* If there is still a control process, wait for that first */
1716 assert_not_reached("Uh, main process died at wrong time.");
1719 } else if (s->control_pid == pid) {
1720 assert(s->control_command);
1722 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1725 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
1727 /* If we are shutting things down anyway we
1728 * don't care about failing commands. */
1730 if (s->control_command->command_next && success) {
1732 /* There is another command to *
1733 * execute, so let's do that. */
1735 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
1736 service_run_next(s, success);
1739 /* No further commands for this step, so let's
1740 * figure out what to do next */
1742 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
1746 case SERVICE_START_PRE:
1748 service_enter_start(s);
1750 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1754 assert(s->type == SERVICE_FORKING);
1756 /* Let's try to load the pid
1757 * file here if we can. We
1758 * ignore the return value,
1759 * since the PID file might
1760 * actually be created by a
1761 * START_POST script */
1765 service_load_pid_file(s);
1767 service_enter_start_post(s);
1769 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1773 case SERVICE_START_POST:
1774 if (success && s->pid_file && !s->main_pid_known) {
1777 /* Hmm, let's see if we can
1778 * load the pid now after the
1779 * start-post scripts got
1782 if ((r = service_load_pid_file(s)) < 0)
1783 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
1788 case SERVICE_RELOAD:
1790 service_enter_running(s, true);
1792 service_enter_stop(s, false);
1797 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
1800 case SERVICE_STOP_SIGTERM:
1801 case SERVICE_STOP_SIGKILL:
1802 if (main_pid_good(s) <= 0)
1803 service_enter_stop_post(s, success);
1805 /* If there is still a service
1806 * process around, wait until
1807 * that one quit, too */
1810 case SERVICE_STOP_POST:
1811 case SERVICE_FINAL_SIGTERM:
1812 case SERVICE_FINAL_SIGKILL:
1813 service_enter_dead(s, success, true);
1817 assert_not_reached("Uh, control process died at wrong time.");
1821 assert_not_reached("Got SIGCHLD for unkown PID");
1824 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
1825 Service *s = SERVICE(u);
1828 assert(elapsed == 1);
1830 assert(w == &s->timer_watch);
1834 case SERVICE_START_PRE:
1836 log_warning("%s operation timed out. Terminating.", u->meta.id);
1837 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1840 case SERVICE_START_POST:
1841 case SERVICE_RELOAD:
1842 log_warning("%s operation timed out. Stopping.", u->meta.id);
1843 service_enter_stop(s, false);
1847 log_warning("%s stopping timed out. Terminating.", u->meta.id);
1848 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1851 case SERVICE_STOP_SIGTERM:
1852 log_warning("%s stopping timed out. Killing.", u->meta.id);
1853 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
1856 case SERVICE_STOP_SIGKILL:
1857 /* Uh, wie sent a SIGKILL and it is still not gone?
1858 * Must be something we cannot kill, so let's just be
1859 * weirded out and continue */
1861 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
1862 service_enter_stop_post(s, false);
1865 case SERVICE_STOP_POST:
1866 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
1867 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1870 case SERVICE_FINAL_SIGTERM:
1871 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
1872 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1875 case SERVICE_FINAL_SIGKILL:
1876 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u->meta.id);
1877 service_enter_dead(s, false, true);
1880 case SERVICE_AUTO_RESTART:
1881 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
1882 service_enter_restart(s);
1886 assert_not_reached("Timeout at wrong time.");
1890 static void service_cgroup_notify_event(Unit *u) {
1891 Service *s = SERVICE(u);
1895 log_debug("%s: cgroup is empty", u->meta.id);
1899 /* Waiting for SIGCHLD is usually more interesting,
1900 * because it includes return codes/signals. Which is
1901 * why we ignore the cgroup events for most cases,
1902 * except when we don't know pid which to expect the
1905 case SERVICE_RUNNING:
1906 service_enter_running(s, true);
1914 static int service_enumerate(Manager *m) {
1918 char *path = NULL, *fpath = NULL, *name = NULL;
1923 STRV_FOREACH(p, m->sysvrcnd_path)
1924 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
1929 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0) {
1937 if (!(d = opendir(path))) {
1938 if (errno != ENOENT)
1939 log_warning("opendir() failed on %s: %s", path, strerror(errno));
1944 while ((de = readdir(d))) {
1945 Unit *runlevel, *service;
1947 if (ignore_file(de->d_name))
1950 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
1953 if (strlen(de->d_name) < 4)
1958 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i], de->d_name) < 0) {
1963 if (access(fpath, X_OK) < 0) {
1965 if (errno != ENOENT)
1966 log_warning("access() failed on %s: %s", fpath, strerror(errno));
1973 if (asprintf(&name, "%s.service", de->d_name+3) < 0) {
1978 if ((r = manager_load_unit(m, name, NULL, &service)) < 0)
1981 if ((r = manager_load_unit(m, rcnd_table[i+1], NULL, &runlevel)) < 0)
1984 if (de->d_name[0] == 'S') {
1985 if ((r = unit_add_dependency(runlevel, UNIT_WANTS, service)) < 0)
1988 if ((r = unit_add_dependency(runlevel, UNIT_AFTER, service)) < 0)
1991 } else if (de->d_name[0] == 'K' &&
1992 (streq(rcnd_table[i+1], SPECIAL_RUNLEVEL0_TARGET) ||
1993 streq(rcnd_table[i+1], SPECIAL_RUNLEVEL6_TARGET))) {
1995 /* We honour K links only for
1996 * halt/reboot. For the normal
1997 * runlevels we assume the
1999 * implicitly added by the
2002 if ((r = unit_add_dependency(runlevel, UNIT_CONFLICTS, service)) < 0)
2005 if ((r = unit_add_dependency(runlevel, UNIT_BEFORE, service)) < 0)
2022 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2023 [SERVICE_DEAD] = "dead",
2024 [SERVICE_START_PRE] = "start-pre",
2025 [SERVICE_START] = "start",
2026 [SERVICE_START_POST] = "start-post",
2027 [SERVICE_RUNNING] = "running",
2028 [SERVICE_EXITED] = "exited",
2029 [SERVICE_RELOAD] = "reload",
2030 [SERVICE_STOP] = "stop",
2031 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2032 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2033 [SERVICE_STOP_POST] = "stop-post",
2034 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2035 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2036 [SERVICE_MAINTAINANCE] = "maintainance",
2037 [SERVICE_AUTO_RESTART] = "auto-restart",
2040 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2042 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2043 [SERVICE_ONCE] = "once",
2044 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2045 [SERVICE_RESTART_ALWAYS] = "restart-always",
2048 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2050 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2051 [SERVICE_FORKING] = "forking",
2052 [SERVICE_SIMPLE] = "simple",
2053 [SERVICE_FINISH] = "finish"
2056 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2058 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2059 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2060 [SERVICE_EXEC_START] = "ExecStart",
2061 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2062 [SERVICE_EXEC_RELOAD] = "ExecReload",
2063 [SERVICE_EXEC_STOP] = "ExecStop",
2064 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2067 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2069 const UnitVTable service_vtable = {
2070 .suffix = ".service",
2072 .init = service_init,
2073 .load = service_load,
2074 .done = service_done,
2076 .dump = service_dump,
2078 .start = service_start,
2079 .stop = service_stop,
2080 .reload = service_reload,
2082 .can_reload = service_can_reload,
2084 .active_state = service_active_state,
2085 .sub_state_to_string = service_sub_state_to_string,
2087 .sigchld_event = service_sigchld_event,
2088 .timer_event = service_timer_event,
2090 .cgroup_notify_empty = service_cgroup_notify_event,
2092 .enumerate = service_enumerate