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"
34 #define COMMENTS "#;\n"
35 #define NEWLINES "\n\r"
38 static const char * const rcnd_table[] = {
39 "/rc0.d", SPECIAL_RUNLEVEL0_TARGET,
40 "/rc1.d", SPECIAL_RUNLEVEL1_TARGET,
41 "/rc2.d", SPECIAL_RUNLEVEL2_TARGET,
42 "/rc3.d", SPECIAL_RUNLEVEL3_TARGET,
43 "/rc4.d", SPECIAL_RUNLEVEL4_TARGET,
44 "/rc5.d", SPECIAL_RUNLEVEL5_TARGET,
45 "/rc6.d", SPECIAL_RUNLEVEL6_TARGET,
46 "/boot.d", SPECIAL_BASIC_TARGET
49 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
50 [SERVICE_DEAD] = UNIT_INACTIVE,
51 [SERVICE_START_PRE] = UNIT_ACTIVATING,
52 [SERVICE_START] = UNIT_ACTIVATING,
53 [SERVICE_START_POST] = UNIT_ACTIVATING,
54 [SERVICE_RUNNING] = UNIT_ACTIVE,
55 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
56 [SERVICE_STOP] = UNIT_DEACTIVATING,
57 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
58 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
59 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
60 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
61 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
62 [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
63 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
66 static void service_done(Unit *u) {
67 Service *s = SERVICE(u);
77 exec_context_done(&s->exec_context);
78 exec_command_free_array(s->exec_command, _SERVICE_EXEC_MAX);
79 s->control_command = NULL;
81 /* This will leak a process, but at least no memory or any of
83 if (s->main_pid > 0) {
84 unit_unwatch_pid(u, s->main_pid);
88 if (s->control_pid > 0) {
89 unit_unwatch_pid(u, s->control_pid);
93 unit_unwatch_timer(u, &s->timer_watch);
96 static int sysv_translate_name(const char *name, char **_r) {
98 static const char * const table[] = {
99 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
100 "$network", SPECIAL_NETWORK_TARGET,
101 "$named", SPECIAL_NSS_LOOKUP_TARGET,
102 "$portmap", SPECIAL_RPCBIND_TARGET,
103 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
104 "$syslog", SPECIAL_SYSLOG_TARGET,
105 "$time", SPECIAL_RTC_SET_TARGET
111 for (i = 0; i < ELEMENTSOF(table); i += 2)
112 if (streq(table[i], name)) {
113 if (!(r = strdup(table[i+1])))
122 if (asprintf(&r, "%s.service", name) < 0)
133 static int sysv_chkconfig_order(Service *s) {
139 if (s->sysv_start_priority < 0)
142 /* For each pair of services where at least one lacks a LSB
143 * header, we use the start priority value to order things. */
145 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
149 t = (Service*) other;
154 if (t->sysv_start_priority < 0)
157 if (s->sysv_has_lsb && t->sysv_has_lsb)
160 if (t->sysv_start_priority < s->sysv_start_priority)
162 else if (t->sysv_start_priority > s->sysv_start_priority)
167 /* FIXME: Maybe we should compare the name here lexicographically? */
169 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t))) < 0)
176 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
179 if (!(c = new0(ExecCommand, 1)))
182 if (!(c->path = strdup(path))) {
187 if (!(c->argv = strv_new(path, arg1, NULL))) {
196 static int sysv_exec_commands(Service *s) {
200 assert(s->sysv_path);
202 if (!(c = exec_command_new(s->sysv_path, "start")))
204 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
206 if (!(c = exec_command_new(s->sysv_path, "stop")))
208 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
210 if (!(c = exec_command_new(s->sysv_path, "reload")))
212 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
217 static int priority_from_rcd(Service *s, const char *init_script) {
221 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvrcnd_path)
222 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
227 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0)
235 log_warning("opendir() failed on %s: %s", path, strerror(errno));
240 while ((de = readdir(d))) {
243 if (ignore_file(de->d_name))
246 if (de->d_name[0] != 'S')
249 if (strlen(de->d_name) < 4)
252 if (!streq(de->d_name + 3, init_script))
255 /* Yay, we found it! Now decode the priority */
257 a = undecchar(de->d_name[1]);
258 b = undecchar(de->d_name[2]);
263 s->sysv_start_priority = a*10 + b;
265 log_debug("Determined priority %i from link farm for %s", s->sysv_start_priority, unit_id(UNIT(s)));
277 static int service_load_sysv_path(Service *s, const char *path, UnitLoadState *new_state) {
295 if (!(f = fopen(path, "re"))) {
296 r = errno == ENOENT ? 0 : -errno;
300 s->type = SERVICE_FORKING;
301 s->restart = SERVICE_ONCE;
304 if (!(s->sysv_path = strdup(path))) {
310 char l[LINE_MAX], *t;
312 if (!fgets(l, sizeof(l), f)) {
317 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
327 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
329 s->sysv_has_lsb = true;
333 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
339 t += strspn(t, WHITESPACE);
341 if (state == NORMAL) {
343 /* Try to parse Red Hat style chkconfig headers */
345 if (startswith(t, "chkconfig:")) {
350 if (sscanf(t+10, "%*15s %i %*i",
351 &start_priority) != 1) {
353 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
357 if (start_priority < 0 || start_priority > 99) {
358 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
362 s->sysv_start_priority = start_priority;
364 } else if (startswith(t, "description:")) {
366 size_t k = strlen(t);
369 if (t[k-1] == '\\') {
374 if (!(d = strdup(strstrip(t+12)))) {
379 free(u->meta.description);
380 u->meta.description = d;
382 } else if (startswith(t, "pidfile:")) {
389 if (!path_is_absolute(fn)) {
390 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
394 if (!(fn = strdup(fn))) {
403 } else if (state == DESCRIPTION) {
405 /* Try to parse Red Hat style description
408 size_t k = strlen(t);
416 assert(u->meta.description);
417 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
422 free(u->meta.description);
423 u->meta.description = d;
425 } else if (state == LSB || state == LSB_DESCRIPTION) {
427 if (startswith(t, "Provides:")) {
433 FOREACH_WORD(w, z, t+9, i) {
436 if (!(n = strndup(w, z))) {
441 r = sysv_translate_name(n, &m);
450 if (unit_name_to_type(m) == UNIT_SERVICE)
451 r = unit_add_name(u, m);
453 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m)) >= 0)
454 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m);
463 } else if (startswith(t, "Required-Start:") ||
464 startswith(t, "Should-Start:")) {
470 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
473 if (!(n = strndup(w, z))) {
478 r = sysv_translate_name(n, &m);
487 r = unit_add_dependency_by_name(u, UNIT_AFTER, m);
494 } else if (startswith(t, "Description:")) {
497 state = LSB_DESCRIPTION;
499 if (!(d = strdup(strstrip(t+12)))) {
504 free(u->meta.description);
505 u->meta.description = d;
507 } else if (startswith(t, "Short-Description:") && !u->meta.description) {
510 /* We use the short description only
511 * if no long description is set. */
515 if (!(d = strdup(strstrip(t+18)))) {
520 free(u->meta.description);
521 u->meta.description = d;
523 } else if (state == LSB_DESCRIPTION) {
525 if (startswith(l, "#\t") || startswith(l, "# ")) {
528 assert(u->meta.description);
529 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
534 free(u->meta.description);
535 u->meta.description = d;
542 /* If init scripts have no LSB header, then we enforce the
543 * ordering via the chkconfig priorities. We try to determine
544 * a priority for *all* init scripts here, since they are
545 * needed as soon as at least one non-LSB script is used. */
547 if (s->sysv_start_priority < 0) {
548 log_debug("%s has no chkconfig header, trying to determine SysV priority from link farm.", unit_id(u));
550 if ((r = priority_from_rcd(s, file_name_from_path(path))) < 0)
553 if (s->sysv_start_priority < 0)
554 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", unit_id(u));
557 if ((r = sysv_exec_commands(s)) < 0)
560 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET)) < 0 ||
561 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET)) < 0)
564 *new_state = UNIT_LOADED;
575 static int service_load_sysv_name(Service *s, const char *name, UnitLoadState *new_state) {
581 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
585 if (asprintf(&path, "%s/%s", *p, name) < 0)
588 assert(endswith(path, ".service"));
589 path[strlen(path)-8] = 0;
591 r = service_load_sysv_path(s, path, new_state);
597 if (*new_state != UNIT_STUB)
604 static int service_load_sysv(Service *s, UnitLoadState *new_state) {
612 /* Load service data from SysV init scripts, preferably with
615 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
618 if ((t = unit_id(UNIT(s))))
619 if ((r = service_load_sysv_name(s, t, new_state)) < 0)
622 if (*new_state == UNIT_STUB)
623 SET_FOREACH(t, UNIT(s)->meta.names, i) {
624 if ((r == service_load_sysv_name(s, t, new_state)) < 0)
627 if (*new_state != UNIT_STUB)
634 static int service_init(Unit *u, UnitLoadState *new_state) {
636 Service *s = SERVICE(u);
640 assert(*new_state == UNIT_STUB);
642 /* First, reset everything to the defaults, in case this is a
648 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
649 s->restart_usec = DEFAULT_RESTART_USEC;
651 exec_context_init(&s->exec_context);
653 s->timer_watch.type = WATCH_INVALID;
655 s->state = SERVICE_DEAD;
657 s->sysv_start_priority = -1;
658 s->permissions_start_only = false;
659 s->root_directory_start_only = false;
661 s->sysv_has_lsb = false;
663 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
665 /* Load a .service file */
666 if ((r = unit_load_fragment(u, new_state)) < 0)
669 /* Load a classic init script as a fallback, if we couldn't find anything */
670 if (*new_state == UNIT_STUB)
671 if ((r = service_load_sysv(s, new_state)) < 0)
674 /* Still nothing found? Then let's give up */
675 if (*new_state == UNIT_STUB)
678 /* We were able to load something, then let's add in the
679 * dropin directories. */
680 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
683 /* This is a new unit? Then let's add in some extras */
684 if (*new_state == UNIT_LOADED) {
685 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
688 if ((r = unit_add_default_cgroup(u)) < 0)
691 if ((r = sysv_chkconfig_order(s)) < 0)
698 static void service_dump(Unit *u, FILE *f, const char *prefix) {
700 ServiceExecCommand c;
701 Service *s = SERVICE(u);
707 p2 = strappend(prefix, "\t");
708 prefix2 = p2 ? p2 : prefix;
711 "%sService State: %s\n"
712 "%sPermissionsStartOnly: %s\n"
713 "%sRootDirectoryStartOnly: %s\n"
714 "%sValidNoProcess: %s\n"
716 prefix, service_state_to_string(s->state),
717 prefix, yes_no(s->permissions_start_only),
718 prefix, yes_no(s->root_directory_start_only),
719 prefix, yes_no(s->valid_no_process),
720 prefix, service_type_to_string(s->type));
725 prefix, s->pid_file);
727 exec_context_dump(&s->exec_context, f, prefix);
729 for (c = 0; c < _SERVICE_EXEC_MAX; c++) {
731 if (!s->exec_command[c])
734 fprintf(f, "%s→ %s:\n",
735 prefix, service_exec_command_to_string(c));
737 exec_command_dump_list(s->exec_command[c], f, prefix2);
742 "%sSysV Init Script Path: %s\n"
743 "%sSysV Init Script has LSB Header: %s\n",
744 prefix, s->sysv_path,
745 prefix, yes_no(s->sysv_has_lsb));
747 if (s->sysv_start_priority >= 0)
749 "%sSysVStartPriority: %i\n",
750 prefix, s->sysv_start_priority);
756 static int service_load_pid_file(Service *s) {
763 if (s->main_pid_known)
769 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
772 if ((r = safe_atolu(k, &p)) < 0) {
777 if ((unsigned long) (pid_t) p != p)
781 s->main_pid_known = true;
786 static int service_get_sockets(Service *s, Set **_set) {
795 /* Collects all Socket objects that belong to this
796 * service. Note that a service might have multiple sockets
797 * via multiple names. */
799 if (!(set = set_new(NULL, NULL)))
802 SET_FOREACH(t, UNIT(s)->meta.names, i) {
806 /* Look for all socket objects that go by any of our
807 * units and collect their fds */
809 if (!(k = unit_name_change_suffix(t, ".socket"))) {
814 p = manager_get_unit(UNIT(s)->meta.manager, k);
819 if ((r = set_put(set, p)) < 0)
832 static int service_notify_sockets(Service *s) {
840 /* Notifies all our sockets when we die */
842 if ((r = service_get_sockets(s, &set)) < 0)
845 SET_FOREACH(sock, set, i)
846 socket_notify_service_dead(sock);
853 static void service_set_state(Service *s, ServiceState state) {
854 ServiceState old_state;
857 old_state = s->state;
860 if (state != SERVICE_START_PRE &&
861 state != SERVICE_START &&
862 state != SERVICE_START_POST &&
863 state != SERVICE_RELOAD &&
864 state != SERVICE_STOP &&
865 state != SERVICE_STOP_SIGTERM &&
866 state != SERVICE_STOP_SIGKILL &&
867 state != SERVICE_STOP_POST &&
868 state != SERVICE_FINAL_SIGTERM &&
869 state != SERVICE_FINAL_SIGKILL &&
870 state != SERVICE_AUTO_RESTART)
871 unit_unwatch_timer(UNIT(s), &s->timer_watch);
873 if (state != SERVICE_START &&
874 state != SERVICE_START_POST &&
875 state != SERVICE_RUNNING &&
876 state != SERVICE_RELOAD &&
877 state != SERVICE_STOP &&
878 state != SERVICE_STOP_SIGTERM &&
879 state != SERVICE_STOP_SIGKILL)
880 if (s->main_pid > 0) {
881 unit_unwatch_pid(UNIT(s), s->main_pid);
885 if (state != SERVICE_START_PRE &&
886 state != SERVICE_START &&
887 state != SERVICE_START_POST &&
888 state != SERVICE_RELOAD &&
889 state != SERVICE_STOP &&
890 state != SERVICE_STOP_SIGTERM &&
891 state != SERVICE_STOP_SIGKILL &&
892 state != SERVICE_STOP_POST &&
893 state != SERVICE_FINAL_SIGTERM &&
894 state != SERVICE_FINAL_SIGKILL)
895 if (s->control_pid > 0) {
896 unit_unwatch_pid(UNIT(s), s->control_pid);
900 if (state != SERVICE_START_PRE &&
901 state != SERVICE_START &&
902 state != SERVICE_START_POST &&
903 state != SERVICE_RELOAD &&
904 state != SERVICE_STOP &&
905 state != SERVICE_STOP_POST)
906 s->control_command = NULL;
908 if (state == SERVICE_DEAD ||
909 state == SERVICE_STOP ||
910 state == SERVICE_STOP_SIGTERM ||
911 state == SERVICE_STOP_SIGKILL ||
912 state == SERVICE_STOP_POST ||
913 state == SERVICE_FINAL_SIGTERM ||
914 state == SERVICE_FINAL_SIGKILL ||
915 state == SERVICE_MAINTAINANCE ||
916 state == SERVICE_AUTO_RESTART)
917 service_notify_sockets(s);
919 log_debug("%s changed %s → %s", unit_id(UNIT(s)), service_state_to_string(old_state), service_state_to_string(state));
921 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
924 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
936 if ((r = service_get_sockets(s, &set)) < 0)
939 SET_FOREACH(sock, set, i) {
943 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
955 if (!(t = new(int, rn_fds+cn_fds))) {
961 memcpy(t, rfds, rn_fds);
962 memcpy(t+rn_fds, cfds, cn_fds);
967 rn_fds = rn_fds+cn_fds;
985 static int service_spawn(
990 bool apply_permissions,
1004 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1008 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1011 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1013 if ((r = exec_spawn(c,
1018 UNIT(s)->meta.cgroup_bondings,
1022 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1023 /* FIXME: we need to do something here */
1035 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1040 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1047 if (allow_restart &&
1048 (s->restart == SERVICE_RESTART_ALWAYS ||
1049 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1051 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1054 service_set_state(s, SERVICE_AUTO_RESTART);
1056 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1061 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
1062 service_enter_dead(s, false, false);
1065 static void service_enter_signal(Service *s, ServiceState state, bool success);
1067 static void service_enter_stop_post(Service *s, bool success) {
1074 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]))
1075 if ((r = service_spawn(s,
1079 !s->permissions_start_only,
1080 !s->root_directory_start_only,
1081 &s->control_pid)) < 0)
1085 service_set_state(s, SERVICE_STOP_POST);
1087 if (!s->control_command)
1088 service_enter_dead(s, true, true);
1093 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
1094 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1097 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1106 if (s->main_pid > 0 || s->control_pid > 0) {
1109 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1112 if (s->main_pid > 0) {
1113 if (kill(s->main_pid, sig) < 0 && errno != ESRCH)
1119 if (s->control_pid > 0) {
1120 if (kill(s->control_pid, sig) < 0 && errno != ESRCH)
1130 service_set_state(s, state);
1132 if (s->main_pid <= 0 && s->control_pid <= 0)
1133 service_enter_dead(s, true, true);
1138 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
1142 service_set_state(s, state);
1143 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1144 service_enter_stop_post(s, false);
1146 service_enter_dead(s, false, true);
1149 static void service_enter_stop(Service *s, bool success) {
1156 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP]))
1157 if ((r = service_spawn(s,
1161 !s->permissions_start_only,
1162 !s->root_directory_start_only,
1163 &s->control_pid)) < 0)
1166 service_set_state(s, SERVICE_STOP);
1168 if (!s->control_command)
1169 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1174 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
1175 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1178 static void service_enter_start_post(Service *s) {
1182 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST]))
1183 if ((r = service_spawn(s,
1187 !s->permissions_start_only,
1188 !s->root_directory_start_only,
1189 &s->control_pid)) < 0)
1193 service_set_state(s, SERVICE_START_POST);
1195 if (!s->control_command)
1196 service_set_state(s, SERVICE_RUNNING);
1201 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
1202 service_enter_stop(s, false);
1205 static void service_enter_start(Service *s) {
1211 assert(s->exec_command[SERVICE_EXEC_START]);
1212 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1214 if ((r = service_spawn(s,
1215 s->exec_command[SERVICE_EXEC_START],
1216 s->type == SERVICE_FORKING,
1223 service_set_state(s, SERVICE_START);
1225 if (s->type == SERVICE_SIMPLE) {
1226 /* For simple services we immediately start
1227 * the START_POST binaries. */
1230 s->main_pid_known = true;
1231 service_enter_start_post(s);
1233 } else if (s->type == SERVICE_FORKING) {
1235 /* For forking services we wait until the start
1236 * process exited. */
1238 s->control_pid = pid;
1239 s->control_command = s->exec_command[SERVICE_EXEC_START];
1240 } else if (s->type == SERVICE_FINISH) {
1242 /* For finishing services we wait until the start
1243 * process exited, too, but it is our main process. */
1246 s->control_command = s->exec_command[SERVICE_EXEC_START];
1248 assert_not_reached("Unknown service type");
1253 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
1254 service_enter_stop(s, false);
1257 static void service_enter_start_pre(Service *s) {
1262 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]))
1263 if ((r = service_spawn(s,
1267 !s->permissions_start_only,
1268 !s->root_directory_start_only,
1269 &s->control_pid)) < 0)
1272 service_set_state(s, SERVICE_START_PRE);
1274 if (!s->control_command)
1275 service_enter_start(s);
1280 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
1281 service_enter_dead(s, false, true);
1284 static void service_enter_restart(Service *s) {
1288 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1291 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
1292 service_enter_dead(s, true, false);
1297 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
1298 service_enter_dead(s, false, false);
1301 static void service_enter_reload(Service *s) {
1306 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]))
1307 if ((r = service_spawn(s,
1311 !s->permissions_start_only,
1312 !s->root_directory_start_only,
1313 &s->control_pid)) < 0)
1316 service_set_state(s, SERVICE_RELOAD);
1318 if (!s->control_command)
1319 service_set_state(s, SERVICE_RUNNING);
1324 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
1325 service_enter_stop(s, false);
1328 static void service_run_next(Service *s, bool success) {
1332 assert(s->control_command);
1333 assert(s->control_command->command_next);
1338 s->control_command = s->control_command->command_next;
1340 if ((r = service_spawn(s,
1344 !s->permissions_start_only,
1345 !s->root_directory_start_only,
1346 &s->control_pid)) < 0)
1352 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
1354 if (s->state == SERVICE_STOP)
1355 service_enter_stop_post(s, false);
1356 else if (s->state == SERVICE_STOP_POST)
1357 service_enter_dead(s, false, true);
1359 service_enter_stop(s, false);
1362 static int service_start(Unit *u) {
1363 Service *s = SERVICE(u);
1367 /* We cannot fulfill this request right now, try again later
1369 if (s->state == SERVICE_STOP ||
1370 s->state == SERVICE_STOP_SIGTERM ||
1371 s->state == SERVICE_STOP_SIGKILL ||
1372 s->state == SERVICE_STOP_POST ||
1373 s->state == SERVICE_FINAL_SIGTERM ||
1374 s->state == SERVICE_FINAL_SIGKILL)
1377 /* Already on it! */
1378 if (s->state == SERVICE_START_PRE ||
1379 s->state == SERVICE_START ||
1380 s->state == SERVICE_START_POST)
1383 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1385 /* Make sure we don't enter a busy loop of some kind. */
1386 if (!ratelimit_test(&s->ratelimit)) {
1387 log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u));
1392 s->main_pid_known = false;
1394 service_enter_start_pre(s);
1398 static int service_stop(Unit *u) {
1399 Service *s = SERVICE(u);
1403 if (s->state == SERVICE_START_PRE ||
1404 s->state == SERVICE_START ||
1405 s->state == SERVICE_START_POST ||
1406 s->state == SERVICE_RELOAD)
1409 if (s->state == SERVICE_AUTO_RESTART) {
1410 service_set_state(s, SERVICE_DEAD);
1414 assert(s->state == SERVICE_RUNNING);
1416 service_enter_stop(s, true);
1420 static int service_reload(Unit *u) {
1421 Service *s = SERVICE(u);
1425 assert(s->state == SERVICE_RUNNING);
1427 service_enter_reload(s);
1431 static bool service_can_reload(Unit *u) {
1432 Service *s = SERVICE(u);
1436 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1439 static UnitActiveState service_active_state(Unit *u) {
1442 return state_translation_table[SERVICE(u)->state];
1445 static int main_pid_good(Service *s) {
1448 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1451 /* If we know the pid file, then lets just check if it is
1453 if (s->main_pid_known)
1454 return s->main_pid > 0;
1456 /* We don't know the pid */
1460 static bool control_pid_good(Service *s) {
1463 return s->control_pid > 0;
1466 static int cgroup_good(Service *s) {
1469 if (s->valid_no_process)
1472 return cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings);
1475 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1476 Service *s = SERVICE(u);
1482 success = code == CLD_EXITED && status == 0;
1483 s->failure = s->failure || !success;
1485 if (s->main_pid == pid) {
1487 exec_status_fill(&s->main_exec_status, pid, code, status);
1490 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1491 assert(s->exec_command[SERVICE_EXEC_START]);
1492 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1495 log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
1497 /* The service exited, so the service is officially
1502 case SERVICE_START_POST:
1503 case SERVICE_RELOAD:
1505 /* Need to wait until the operation is
1510 assert(s->type == SERVICE_FINISH);
1512 /* This was our main goal, so let's go on */
1514 service_enter_start_post(s);
1516 service_enter_stop(s, false);
1519 case SERVICE_RUNNING:
1520 service_enter_stop(s, success);
1523 case SERVICE_STOP_SIGTERM:
1524 case SERVICE_STOP_SIGKILL:
1526 if (!control_pid_good(s))
1527 service_enter_stop_post(s, success);
1529 /* If there is still a control process, wait for that first */
1533 assert_not_reached("Uh, main process died at wrong time.");
1536 } else if (s->control_pid == pid) {
1537 assert(s->control_command);
1539 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1542 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
1544 /* If we are shutting things down anyway we
1545 * don't care about failing commands. */
1547 if (s->control_command->command_next &&
1548 (success || (s->state == SERVICE_STOP || s->state == SERVICE_STOP_POST)))
1550 /* There is another command to *
1551 * execute, so let's do that. */
1553 service_run_next(s, success);
1556 /* No further commands for this step, so let's
1557 * figure out what to do next */
1559 log_debug("%s got final SIGCHLD for state %s", unit_id(u), service_state_to_string(s->state));
1563 case SERVICE_START_PRE:
1565 service_enter_start(s);
1567 service_enter_stop(s, false);
1571 assert(s->type == SERVICE_FORKING);
1573 /* Let's try to load the pid
1574 * file here if we can. We
1575 * ignore the return value,
1576 * since the PID file might
1577 * actually be created by a
1578 * START_POST script */
1582 service_load_pid_file(s);
1584 service_enter_start_post(s);
1586 service_enter_stop(s, false);
1590 case SERVICE_START_POST:
1591 if (success && s->pid_file && !s->main_pid_known) {
1594 /* Hmm, let's see if we can
1595 * load the pid now after the
1596 * start-post scripts got
1599 if ((r = service_load_pid_file(s)) < 0)
1600 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
1605 case SERVICE_RELOAD:
1607 if (main_pid_good(s) != 0 && cgroup_good(s) != 0)
1608 service_set_state(s, SERVICE_RUNNING);
1610 service_enter_stop(s, true);
1612 service_enter_stop(s, false);
1617 if (main_pid_good(s) > 0)
1618 /* Still not dead and we know the PID? Let's go hunting. */
1619 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
1621 service_enter_stop_post(s, success);
1624 case SERVICE_STOP_SIGTERM:
1625 case SERVICE_STOP_SIGKILL:
1626 if (main_pid_good(s) <= 0)
1627 service_enter_stop_post(s, success);
1629 /* If there is still a service
1630 * process around, wait until
1631 * that one quit, too */
1634 case SERVICE_STOP_POST:
1635 case SERVICE_FINAL_SIGTERM:
1636 case SERVICE_FINAL_SIGKILL:
1637 service_enter_dead(s, success, true);
1641 assert_not_reached("Uh, control process died at wrong time.");
1645 assert_not_reached("Got SIGCHLD for unkown PID");
1648 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
1649 Service *s = SERVICE(u);
1652 assert(elapsed == 1);
1654 assert(w == &s->timer_watch);
1658 case SERVICE_START_PRE:
1660 case SERVICE_START_POST:
1661 case SERVICE_RELOAD:
1662 log_warning("%s operation timed out. Stopping.", unit_id(u));
1663 service_enter_stop(s, false);
1667 log_warning("%s stopping timed out. Terminating.", unit_id(u));
1668 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1671 case SERVICE_STOP_SIGTERM:
1672 log_warning("%s stopping timed out. Killing.", unit_id(u));
1673 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
1676 case SERVICE_STOP_SIGKILL:
1677 /* Uh, wie sent a SIGKILL and it is still not gone?
1678 * Must be something we cannot kill, so let's just be
1679 * weirded out and continue */
1681 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
1682 service_enter_stop_post(s, false);
1685 case SERVICE_STOP_POST:
1686 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
1687 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1690 case SERVICE_FINAL_SIGTERM:
1691 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
1692 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1695 case SERVICE_FINAL_SIGKILL:
1696 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
1697 service_enter_dead(s, false, true);
1700 case SERVICE_AUTO_RESTART:
1701 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
1702 service_enter_restart(s);
1706 assert_not_reached("Timeout at wrong time.");
1710 static void service_cgroup_notify_event(Unit *u) {
1711 Service *s = SERVICE(u);
1715 log_debug("%s: cgroup is empty", unit_id(u));
1719 /* Waiting for SIGCHLD is usually more interesting,
1720 * because it includes return codes/signals. Which is
1721 * why we ignore the cgroup events for most cases,
1722 * except when we don't know pid which to expect the
1725 case SERVICE_RUNNING:
1727 if (!s->valid_no_process && main_pid_good(s) <= 0)
1728 service_enter_stop(s, true);
1737 static int service_enumerate(Manager *m) {
1741 char *path = NULL, *fpath = NULL, *name = NULL;
1746 STRV_FOREACH(p, m->sysvrcnd_path)
1747 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
1752 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0) {
1760 if (!(d = opendir(path))) {
1761 if (errno != ENOENT)
1762 log_warning("opendir() failed on %s: %s", path, strerror(errno));
1767 while ((de = readdir(d))) {
1768 Unit *runlevel, *service;
1770 if (ignore_file(de->d_name))
1773 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
1776 if (strlen(de->d_name) < 4)
1781 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i], de->d_name) < 0) {
1786 if (access(fpath, X_OK) < 0) {
1788 if (errno != ENOENT)
1789 log_warning("access() failed on %s: %s", fpath, strerror(errno));
1796 if (asprintf(&name, "%s.service", de->d_name+3) < 0) {
1801 if ((r = manager_load_unit(m, name, &service)) < 0)
1804 if ((r = manager_load_unit(m, rcnd_table[i+1], &runlevel)) < 0)
1807 if (de->d_name[0] == 'S') {
1808 if ((r = unit_add_dependency(runlevel, UNIT_WANTS, service)) < 0)
1811 if ((r = unit_add_dependency(runlevel, UNIT_AFTER, service)) < 0)
1814 } else if (de->d_name[0] == 'K' &&
1815 (streq(rcnd_table[i+1], SPECIAL_RUNLEVEL0_TARGET) ||
1816 streq(rcnd_table[i+1], SPECIAL_RUNLEVEL6_TARGET))) {
1818 /* We honour K links only for
1819 * halt/reboot. For the normal
1820 * runlevels we assume the
1822 * implicitly added by the
1825 if ((r = unit_add_dependency(runlevel, UNIT_CONFLICTS, service)) < 0)
1828 if ((r = unit_add_dependency(runlevel, UNIT_BEFORE, service)) < 0)
1845 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
1846 [SERVICE_DEAD] = "dead",
1847 [SERVICE_START_PRE] = "start-pre",
1848 [SERVICE_START] = "start",
1849 [SERVICE_START_POST] = "start-post",
1850 [SERVICE_RUNNING] = "running",
1851 [SERVICE_RELOAD] = "reload",
1852 [SERVICE_STOP] = "stop",
1853 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
1854 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
1855 [SERVICE_STOP_POST] = "stop-post",
1856 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
1857 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
1858 [SERVICE_MAINTAINANCE] = "maintainance",
1859 [SERVICE_AUTO_RESTART] = "auto-restart",
1862 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
1864 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
1865 [SERVICE_ONCE] = "once",
1866 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
1867 [SERVICE_RESTART_ALWAYS] = "restart-on-failure",
1870 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
1872 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
1873 [SERVICE_FORKING] = "forking",
1874 [SERVICE_SIMPLE] = "simple",
1875 [SERVICE_FINISH] = "finish"
1878 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
1880 static const char* const service_exec_command_table[_SERVICE_EXEC_MAX] = {
1881 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
1882 [SERVICE_EXEC_START] = "ExecStart",
1883 [SERVICE_EXEC_START_POST] = "ExecStartPost",
1884 [SERVICE_EXEC_RELOAD] = "ExecReload",
1885 [SERVICE_EXEC_STOP] = "ExecStop",
1886 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
1889 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
1891 const UnitVTable service_vtable = {
1892 .suffix = ".service",
1894 .init = service_init,
1895 .done = service_done,
1897 .dump = service_dump,
1899 .start = service_start,
1900 .stop = service_stop,
1901 .reload = service_reload,
1903 .can_reload = service_can_reload,
1905 .active_state = service_active_state,
1907 .sigchld_event = service_sigchld_event,
1908 .timer_event = service_timer_event,
1910 .cgroup_notify_empty = service_cgroup_notify_event,
1912 .enumerate = service_enumerate