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_close_socket_fd(Service *s) {
94 close_nointr_nofail(s->socket_fd);
98 static void service_done(Unit *u) {
99 Service *s = SERVICE(u);
109 free(s->sysv_runlevels);
110 s->sysv_runlevels = NULL;
112 exec_context_done(&s->exec_context);
113 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
114 s->control_command = NULL;
116 /* This will leak a process, but at least no memory or any of
118 service_unwatch_main_pid(s);
119 service_unwatch_control_pid(s);
121 service_close_socket_fd(s);
123 unit_unwatch_timer(u, &s->timer_watch);
126 static int sysv_translate_name(const char *name, char **_r) {
128 static const char * const table[] = {
129 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
130 "$network", SPECIAL_NETWORK_TARGET,
131 "$named", SPECIAL_NSS_LOOKUP_TARGET,
132 "$portmap", SPECIAL_RPCBIND_TARGET,
133 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
134 "$syslog", SPECIAL_SYSLOG_TARGET,
135 "$time", SPECIAL_RTC_SET_TARGET
141 for (i = 0; i < ELEMENTSOF(table); i += 2)
142 if (streq(table[i], name)) {
143 if (!(r = strdup(table[i+1])))
152 if (asprintf(&r, "%s.service", name) < 0)
163 static int sysv_chkconfig_order(Service *s) {
169 if (s->sysv_start_priority < 0)
172 /* For each pair of services where at least one lacks a LSB
173 * header, we use the start priority value to order things. */
175 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
179 t = (Service*) other;
184 if (t->sysv_start_priority < 0)
187 /* If both units have modern headers we don't care
188 * about the priorities */
189 if ((!s->sysv_path || s->sysv_has_lsb) &&
190 (!t->sysv_path || t->sysv_has_lsb))
193 if (t->sysv_start_priority < s->sysv_start_priority)
195 else if (t->sysv_start_priority > s->sysv_start_priority)
200 /* FIXME: Maybe we should compare the name here lexicographically? */
202 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t))) < 0)
209 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
212 if (!(c = new0(ExecCommand, 1)))
215 if (!(c->path = strdup(path))) {
220 if (!(c->argv = strv_new(path, arg1, NULL))) {
229 static int sysv_exec_commands(Service *s) {
233 assert(s->sysv_path);
235 if (!(c = exec_command_new(s->sysv_path, "start")))
237 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
239 if (!(c = exec_command_new(s->sysv_path, "stop")))
241 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
243 if (!(c = exec_command_new(s->sysv_path, "reload")))
245 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
250 static int priority_from_rcd(Service *s, const char *init_script) {
254 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvrcnd_path)
255 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
260 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0)
268 log_warning("opendir() failed on %s: %s", path, strerror(errno));
273 while ((de = readdir(d))) {
276 if (ignore_file(de->d_name))
279 if (de->d_name[0] != 'S')
282 if (strlen(de->d_name) < 4)
285 if (!streq(de->d_name + 3, init_script))
288 /* Yay, we found it! Now decode the priority */
290 a = undecchar(de->d_name[1]);
291 b = undecchar(de->d_name[2]);
296 s->sysv_start_priority = a*10 + b;
298 log_debug("Determined priority %i from link farm for %s", s->sysv_start_priority, UNIT(s)->meta.id);
310 static int service_load_sysv_path(Service *s, const char *path) {
327 if (!(f = fopen(path, "re"))) {
328 r = errno == ENOENT ? 0 : -errno;
332 s->type = SERVICE_FORKING;
333 s->restart = SERVICE_ONCE;
336 if (!(s->sysv_path = strdup(path))) {
342 char l[LINE_MAX], *t;
344 if (!fgets(l, sizeof(l), f)) {
349 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
359 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
361 s->sysv_has_lsb = true;
365 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
371 t += strspn(t, WHITESPACE);
373 if (state == NORMAL) {
375 /* Try to parse Red Hat style chkconfig headers */
377 if (startswith(t, "chkconfig:")) {
379 char runlevels[16], *k;
383 if (sscanf(t+10, "%15s %i %*i",
385 &start_priority) != 2) {
387 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
391 if (start_priority < 0 || start_priority > 99)
392 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
394 s->sysv_start_priority = start_priority;
396 char_array_0(runlevels);
397 k = delete_chars(runlevels, WHITESPACE "-");
402 if (!(d = strdup(k))) {
407 free(s->sysv_runlevels);
408 s->sysv_runlevels = d;
412 } else if (startswith(t, "description:")) {
414 size_t k = strlen(t);
417 if (t[k-1] == '\\') {
422 if (!(d = strdup(strstrip(t+12)))) {
427 free(u->meta.description);
428 u->meta.description = d;
430 } else if (startswith(t, "pidfile:")) {
437 if (!path_is_absolute(fn)) {
438 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
442 if (!(fn = strdup(fn))) {
451 } else if (state == DESCRIPTION) {
453 /* Try to parse Red Hat style description
456 size_t k = strlen(t);
464 assert(u->meta.description);
465 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
470 free(u->meta.description);
471 u->meta.description = d;
473 } else if (state == LSB || state == LSB_DESCRIPTION) {
475 if (startswith(t, "Provides:")) {
481 FOREACH_WORD(w, z, t+9, i) {
484 if (!(n = strndup(w, z))) {
489 r = sysv_translate_name(n, &m);
498 if (unit_name_to_type(m) == UNIT_SERVICE)
499 r = unit_add_name(u, m);
501 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL)) >= 0)
502 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL);
511 } else if (startswith(t, "Required-Start:") ||
512 startswith(t, "Should-Start:")) {
518 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
521 if (!(n = strndup(w, z))) {
526 r = sysv_translate_name(n, &m);
535 r = unit_add_dependency_by_name(u, UNIT_AFTER, m, NULL);
541 } else if (startswith(t, "Default-Start:")) {
546 k = delete_chars(t+14, WHITESPACE "-");
549 if (!(d = strdup(k))) {
554 free(s->sysv_runlevels);
555 s->sysv_runlevels = d;
558 } else if (startswith(t, "Description:")) {
561 state = LSB_DESCRIPTION;
563 if (!(d = strdup(strstrip(t+12)))) {
568 free(u->meta.description);
569 u->meta.description = d;
571 } else if (startswith(t, "Short-Description:") &&
572 !u->meta.description) {
575 /* We use the short description only
576 * if no long description is set. */
580 if (!(d = strdup(strstrip(t+18)))) {
585 u->meta.description = d;
587 } else if (state == LSB_DESCRIPTION) {
589 if (startswith(l, "#\t") || startswith(l, "# ")) {
592 assert(u->meta.description);
593 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
598 free(u->meta.description);
599 u->meta.description = d;
606 /* If init scripts have no LSB header, then we enforce the
607 * ordering via the chkconfig priorities. We try to determine
608 * a priority for *all* init scripts here, since they are
609 * needed as soon as at least one non-LSB script is used. */
611 if (s->sysv_start_priority < 0) {
612 log_debug("%s has no chkconfig header, trying to determine SysV priority from link farm.", u->meta.id);
614 if ((r = priority_from_rcd(s, file_name_from_path(path))) < 0)
617 if (s->sysv_start_priority < 0)
618 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", u->meta.id);
621 if ((r = sysv_exec_commands(s)) < 0)
624 if (!s->sysv_runlevels || chars_intersect("12345", s->sysv_runlevels)) {
625 /* If there a runlevels configured for this service
626 * but none of the standard ones, then we assume this
627 * is some special kind of service (which might be
628 * needed for early boot) and don't create any links
631 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL)) < 0 ||
632 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL)) < 0)
636 /* Special setting for all SysV services */
637 s->valid_no_process = true;
639 /* Don't timeout special services during boot (like fsck) */
640 if (s->sysv_runlevels && !chars_intersect("12345", s->sysv_runlevels))
641 s->timeout_usec = -1;
643 u->meta.load_state = UNIT_LOADED;
654 static int service_load_sysv_name(Service *s, const char *name) {
660 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
664 if (asprintf(&path, "%s/%s", *p, name) < 0)
667 assert(endswith(path, ".service"));
668 path[strlen(path)-8] = 0;
670 r = service_load_sysv_path(s, path);
676 if ((UNIT(s)->meta.load_state != UNIT_STUB))
683 static int service_load_sysv(Service *s) {
690 /* Load service data from SysV init scripts, preferably with
693 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
696 if ((t = UNIT(s)->meta.id))
697 if ((r = service_load_sysv_name(s, t)) < 0)
700 if (UNIT(s)->meta.load_state == UNIT_STUB)
701 SET_FOREACH(t, UNIT(s)->meta.names, i) {
702 if (t == UNIT(s)->meta.id)
705 if ((r == service_load_sysv_name(s, t)) < 0)
708 if (UNIT(s)->meta.load_state != UNIT_STUB)
715 static void service_init(Unit *u) {
716 Service *s = SERVICE(u);
719 assert(u->meta.load_state == UNIT_STUB);
724 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
725 s->restart_usec = DEFAULT_RESTART_USEC;
727 exec_context_init(&s->exec_context);
729 s->timer_watch.type = WATCH_INVALID;
731 s->state = SERVICE_DEAD;
733 s->sysv_start_priority = -1;
734 s->permissions_start_only = false;
735 s->root_directory_start_only = false;
736 s->valid_no_process = false;
738 s->sysv_has_lsb = false;
739 s->main_pid = s->control_pid = 0;
740 s->main_pid_known = false;
745 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
748 static int service_verify(Service *s) {
751 if (UNIT(s)->meta.load_state != UNIT_LOADED)
754 if (!s->exec_command[SERVICE_EXEC_START]) {
755 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
762 static int service_load(Unit *u) {
764 Service *s = SERVICE(u);
768 /* Load a .service file */
769 if ((r = unit_load_fragment(u)) < 0)
772 /* Load a classic init script as a fallback, if we couldn't find anything */
773 if (u->meta.load_state == UNIT_STUB)
774 if ((r = service_load_sysv(s)) < 0)
777 /* Still nothing found? Then let's give up */
778 if (u->meta.load_state == UNIT_STUB)
781 /* We were able to load something, then let's add in the
782 * dropin directories. */
783 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
786 /* This is a new unit? Then let's add in some extras */
787 if (u->meta.load_state == UNIT_LOADED) {
788 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
791 if ((r = unit_add_default_cgroup(u)) < 0)
794 if ((r = sysv_chkconfig_order(s)) < 0)
798 return service_verify(s);
801 static void service_dump(Unit *u, FILE *f, const char *prefix) {
803 ServiceExecCommand c;
804 Service *s = SERVICE(u);
810 p2 = strappend(prefix, "\t");
811 prefix2 = p2 ? p2 : prefix;
814 "%sService State: %s\n"
815 "%sPermissionsStartOnly: %s\n"
816 "%sRootDirectoryStartOnly: %s\n"
817 "%sValidNoProcess: %s\n"
820 prefix, service_state_to_string(s->state),
821 prefix, yes_no(s->permissions_start_only),
822 prefix, yes_no(s->root_directory_start_only),
823 prefix, yes_no(s->valid_no_process),
824 prefix, kill_mode_to_string(s->kill_mode),
825 prefix, service_type_to_string(s->type));
827 if (s->control_pid > 0)
829 "%sControl PID: %llu\n",
830 prefix, (unsigned long long) s->control_pid);
834 "%sMain PID: %llu\n",
835 prefix, (unsigned long long) s->main_pid);
840 prefix, s->pid_file);
842 exec_context_dump(&s->exec_context, f, prefix);
844 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
846 if (!s->exec_command[c])
849 fprintf(f, "%s→ %s:\n",
850 prefix, service_exec_command_to_string(c));
852 exec_command_dump_list(s->exec_command[c], f, prefix2);
857 "%sSysV Init Script Path: %s\n"
858 "%sSysV Init Script has LSB Header: %s\n",
859 prefix, s->sysv_path,
860 prefix, yes_no(s->sysv_has_lsb));
862 if (s->sysv_start_priority >= 0)
864 "%sSysVStartPriority: %i\n",
865 prefix, s->sysv_start_priority);
867 if (s->sysv_runlevels)
868 fprintf(f, "%sSysVRunLevels: %s\n",
869 prefix, s->sysv_runlevels);
874 static int service_load_pid_file(Service *s) {
881 if (s->main_pid_known)
884 assert(s->main_pid <= 0);
889 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
892 if ((r = safe_atolu(k, &p)) < 0) {
897 if ((unsigned long) (pid_t) p != p)
900 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
901 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
902 (unsigned long long) p, s->pid_file);
906 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
907 /* FIXME: we need to do something here */
910 s->main_pid = (pid_t) p;
911 s->main_pid_known = true;
916 static int service_get_sockets(Service *s, Set **_set) {
925 /* Collects all Socket objects that belong to this
926 * service. Note that a service might have multiple sockets
927 * via multiple names. */
929 if (!(set = set_new(NULL, NULL)))
932 SET_FOREACH(t, UNIT(s)->meta.names, i) {
936 /* Look for all socket objects that go by any of our
937 * units and collect their fds */
939 if (!(k = unit_name_change_suffix(t, ".socket"))) {
944 p = manager_get_unit(UNIT(s)->meta.manager, k);
949 if ((r = set_put(set, p)) < 0)
961 static int service_notify_sockets_dead(Service *s) {
969 /* Notifies all our sockets when we die */
970 if ((r = service_get_sockets(s, &set)) < 0)
973 SET_FOREACH(sock, set, i)
974 socket_notify_service_dead(sock);
981 static void service_set_state(Service *s, ServiceState state) {
982 ServiceState old_state;
985 old_state = s->state;
988 if (state != SERVICE_START_PRE &&
989 state != SERVICE_START &&
990 state != SERVICE_START_POST &&
991 state != SERVICE_RELOAD &&
992 state != SERVICE_STOP &&
993 state != SERVICE_STOP_SIGTERM &&
994 state != SERVICE_STOP_SIGKILL &&
995 state != SERVICE_STOP_POST &&
996 state != SERVICE_FINAL_SIGTERM &&
997 state != SERVICE_FINAL_SIGKILL &&
998 state != SERVICE_AUTO_RESTART)
999 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1001 if (state != SERVICE_START &&
1002 state != SERVICE_START_POST &&
1003 state != SERVICE_RUNNING &&
1004 state != SERVICE_RELOAD &&
1005 state != SERVICE_STOP &&
1006 state != SERVICE_STOP_SIGTERM &&
1007 state != SERVICE_STOP_SIGKILL)
1008 service_unwatch_main_pid(s);
1010 if (state != SERVICE_START_PRE &&
1011 state != SERVICE_START &&
1012 state != SERVICE_START_POST &&
1013 state != SERVICE_RELOAD &&
1014 state != SERVICE_STOP &&
1015 state != SERVICE_STOP_SIGTERM &&
1016 state != SERVICE_STOP_SIGKILL &&
1017 state != SERVICE_STOP_POST &&
1018 state != SERVICE_FINAL_SIGTERM &&
1019 state != SERVICE_FINAL_SIGKILL) {
1020 service_unwatch_control_pid(s);
1021 s->control_command = NULL;
1024 if (state == SERVICE_DEAD ||
1025 state == SERVICE_STOP ||
1026 state == SERVICE_STOP_SIGTERM ||
1027 state == SERVICE_STOP_SIGKILL ||
1028 state == SERVICE_STOP_POST ||
1029 state == SERVICE_FINAL_SIGTERM ||
1030 state == SERVICE_FINAL_SIGKILL ||
1031 state == SERVICE_MAINTAINANCE ||
1032 state == SERVICE_AUTO_RESTART)
1033 service_notify_sockets_dead(s);
1035 if (state != SERVICE_START_PRE &&
1036 state != SERVICE_START &&
1037 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1038 service_close_socket_fd(s);
1040 if (old_state != state)
1041 log_debug("%s changed %s → %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1043 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1046 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1050 unsigned rn_fds = 0;
1058 if ((r = service_get_sockets(s, &set)) < 0)
1061 SET_FOREACH(sock, set, i) {
1065 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1077 if (!(t = new(int, rn_fds+cn_fds))) {
1083 memcpy(t, rfds, rn_fds);
1084 memcpy(t+rn_fds, cfds, cn_fds);
1089 rn_fds = rn_fds+cn_fds;
1107 static int service_spawn(
1112 bool apply_permissions,
1127 if (s->socket_fd >= 0) {
1128 fds = &s->socket_fd;
1130 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1135 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1138 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1140 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1151 UNIT(s)->meta.manager->confirm_spawn,
1152 UNIT(s)->meta.cgroup_bondings,
1160 if (s->socket_fd >= 0)
1161 service_close_socket_fd(s);
1166 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1167 /* FIXME: we need to do something here */
1178 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1183 static int main_pid_good(Service *s) {
1186 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1189 /* If we know the pid file, then lets just check if it is
1191 if (s->main_pid_known)
1192 return s->main_pid > 0;
1194 /* We don't know the pid */
1198 static int control_pid_good(Service *s) {
1201 return s->control_pid > 0;
1204 static int cgroup_good(Service *s) {
1209 if (s->valid_no_process)
1212 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1218 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1225 if (allow_restart &&
1226 (s->restart == SERVICE_RESTART_ALWAYS ||
1227 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1229 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1232 service_set_state(s, SERVICE_AUTO_RESTART);
1234 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1239 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
1240 service_enter_dead(s, false, false);
1243 static void service_enter_signal(Service *s, ServiceState state, bool success);
1245 static void service_enter_stop_post(Service *s, bool success) {
1252 service_unwatch_control_pid(s);
1254 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1255 if ((r = service_spawn(s,
1259 !s->permissions_start_only,
1260 !s->root_directory_start_only,
1261 &s->control_pid)) < 0)
1265 service_set_state(s, SERVICE_STOP_POST);
1267 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1272 log_warning("%s failed to run stop-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1273 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1276 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1285 if (s->kill_mode != KILL_NONE) {
1286 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1288 if (s->kill_mode == KILL_CONTROL_GROUP) {
1290 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1291 if (r != -EAGAIN && r != -ESRCH)
1300 if (s->main_pid > 0) {
1301 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1307 if (s->control_pid > 0) {
1308 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1320 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1323 service_set_state(s, state);
1324 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1325 service_enter_stop_post(s, true);
1327 service_enter_dead(s, true, true);
1332 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
1334 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1335 service_enter_stop_post(s, false);
1337 service_enter_dead(s, false, true);
1340 static void service_enter_stop(Service *s, bool success) {
1347 service_unwatch_control_pid(s);
1349 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1350 if ((r = service_spawn(s,
1354 !s->permissions_start_only,
1355 !s->root_directory_start_only,
1356 &s->control_pid)) < 0)
1359 service_set_state(s, SERVICE_STOP);
1361 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1366 log_warning("%s failed to run stop executable: %s", UNIT(s)->meta.id, strerror(-r));
1367 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1370 static void service_enter_running(Service *s, bool success) {
1376 if (main_pid_good(s) != 0 && cgroup_good(s) != 0)
1377 service_set_state(s, SERVICE_RUNNING);
1378 else if (s->valid_no_process)
1379 service_set_state(s, SERVICE_EXITED);
1381 service_enter_stop(s, true);
1384 static void service_enter_start_post(Service *s) {
1388 service_unwatch_control_pid(s);
1390 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1391 if ((r = service_spawn(s,
1395 !s->permissions_start_only,
1396 !s->root_directory_start_only,
1397 &s->control_pid)) < 0)
1401 service_set_state(s, SERVICE_START_POST);
1403 service_enter_running(s, true);
1408 log_warning("%s failed to run start-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1409 service_enter_stop(s, false);
1412 static void service_enter_start(Service *s) {
1418 assert(s->exec_command[SERVICE_EXEC_START]);
1419 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1421 if (s->type == SERVICE_FORKING)
1422 service_unwatch_control_pid(s);
1424 service_unwatch_main_pid(s);
1426 if ((r = service_spawn(s,
1427 s->exec_command[SERVICE_EXEC_START],
1428 s->type == SERVICE_FORKING,
1435 if (s->type == SERVICE_SIMPLE) {
1436 /* For simple services we immediately start
1437 * the START_POST binaries. */
1440 s->main_pid_known = true;
1442 service_enter_start_post(s);
1444 } else if (s->type == SERVICE_FORKING) {
1446 /* For forking services we wait until the start
1447 * process exited. */
1449 s->control_pid = pid;
1451 s->control_command = s->exec_command[SERVICE_EXEC_START];
1452 service_set_state(s, SERVICE_START);
1454 } else if (s->type == SERVICE_FINISH) {
1456 /* For finishing services we wait until the start
1457 * process exited, too, but it is our main process. */
1460 s->main_pid_known = true;
1462 s->control_command = s->exec_command[SERVICE_EXEC_START];
1463 service_set_state(s, SERVICE_START);
1465 assert_not_reached("Unknown service type");
1470 log_warning("%s failed to run start exectuable: %s", UNIT(s)->meta.id, strerror(-r));
1471 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1474 static void service_enter_start_pre(Service *s) {
1479 service_unwatch_control_pid(s);
1481 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1482 if ((r = service_spawn(s,
1486 !s->permissions_start_only,
1487 !s->root_directory_start_only,
1488 &s->control_pid)) < 0)
1491 service_set_state(s, SERVICE_START_PRE);
1493 service_enter_start(s);
1498 log_warning("%s failed to run start-pre executable: %s", UNIT(s)->meta.id, strerror(-r));
1499 service_enter_dead(s, false, true);
1502 static void service_enter_restart(Service *s) {
1506 service_enter_dead(s, true, false);
1508 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1511 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
1516 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
1517 service_enter_dead(s, false, false);
1520 static void service_enter_reload(Service *s) {
1525 service_unwatch_control_pid(s);
1527 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1528 if ((r = service_spawn(s,
1532 !s->permissions_start_only,
1533 !s->root_directory_start_only,
1534 &s->control_pid)) < 0)
1537 service_set_state(s, SERVICE_RELOAD);
1539 service_enter_running(s, true);
1544 log_warning("%s failed to run reload executable: %s", UNIT(s)->meta.id, strerror(-r));
1545 service_enter_stop(s, false);
1548 static void service_run_next(Service *s, bool success) {
1552 assert(s->control_command);
1553 assert(s->control_command->command_next);
1558 s->control_command = s->control_command->command_next;
1560 service_unwatch_control_pid(s);
1562 if ((r = service_spawn(s,
1566 !s->permissions_start_only,
1567 !s->root_directory_start_only,
1568 &s->control_pid)) < 0)
1574 log_warning("%s failed to run spawn next executable: %s", UNIT(s)->meta.id, strerror(-r));
1576 if (s->state == SERVICE_START_PRE)
1577 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1578 else if (s->state == SERVICE_STOP)
1579 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1580 else if (s->state == SERVICE_STOP_POST)
1581 service_enter_dead(s, false, true);
1583 service_enter_stop(s, false);
1586 static int service_start(Unit *u) {
1587 Service *s = SERVICE(u);
1591 /* We cannot fulfill this request right now, try again later
1593 if (s->state == SERVICE_STOP ||
1594 s->state == SERVICE_STOP_SIGTERM ||
1595 s->state == SERVICE_STOP_SIGKILL ||
1596 s->state == SERVICE_STOP_POST ||
1597 s->state == SERVICE_FINAL_SIGTERM ||
1598 s->state == SERVICE_FINAL_SIGKILL)
1601 /* Already on it! */
1602 if (s->state == SERVICE_START_PRE ||
1603 s->state == SERVICE_START ||
1604 s->state == SERVICE_START_POST)
1607 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1609 /* Make sure we don't enter a busy loop of some kind. */
1610 if (!ratelimit_test(&s->ratelimit)) {
1611 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1616 s->main_pid_known = false;
1618 service_enter_start_pre(s);
1622 static int service_stop(Unit *u) {
1623 Service *s = SERVICE(u);
1627 /* Cannot do this now */
1628 if (s->state == SERVICE_START_PRE ||
1629 s->state == SERVICE_START ||
1630 s->state == SERVICE_START_POST ||
1631 s->state == SERVICE_RELOAD)
1635 if (s->state == SERVICE_STOP ||
1636 s->state == SERVICE_STOP_SIGTERM ||
1637 s->state == SERVICE_STOP_SIGKILL ||
1638 s->state == SERVICE_STOP_POST ||
1639 s->state == SERVICE_FINAL_SIGTERM ||
1640 s->state == SERVICE_FINAL_SIGKILL)
1643 if (s->state == SERVICE_AUTO_RESTART) {
1644 service_set_state(s, SERVICE_DEAD);
1648 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1650 service_enter_stop(s, true);
1654 static int service_reload(Unit *u) {
1655 Service *s = SERVICE(u);
1659 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1661 service_enter_reload(s);
1665 static bool service_can_reload(Unit *u) {
1666 Service *s = SERVICE(u);
1670 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1673 static UnitActiveState service_active_state(Unit *u) {
1676 return state_translation_table[SERVICE(u)->state];
1679 static const char *service_sub_state_to_string(Unit *u) {
1682 return service_state_to_string(SERVICE(u)->state);
1685 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1686 Service *s = SERVICE(u);
1692 success = code == CLD_EXITED && status == 0;
1693 s->failure = s->failure || !success;
1695 if (s->main_pid == pid) {
1697 exec_status_fill(&s->main_exec_status, pid, code, status);
1700 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1701 assert(s->exec_command[SERVICE_EXEC_START]);
1702 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1705 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
1707 /* The service exited, so the service is officially
1712 case SERVICE_START_POST:
1713 case SERVICE_RELOAD:
1715 /* Need to wait until the operation is
1720 assert(s->type == SERVICE_FINISH);
1722 /* This was our main goal, so let's go on */
1724 service_enter_start_post(s);
1726 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1729 case SERVICE_RUNNING:
1730 service_enter_running(s, success);
1733 case SERVICE_STOP_SIGTERM:
1734 case SERVICE_STOP_SIGKILL:
1736 if (!control_pid_good(s))
1737 service_enter_stop_post(s, success);
1739 /* If there is still a control process, wait for that first */
1743 assert_not_reached("Uh, main process died at wrong time.");
1746 } else if (s->control_pid == pid) {
1747 assert(s->control_command);
1749 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1752 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
1754 /* If we are shutting things down anyway we
1755 * don't care about failing commands. */
1757 if (s->control_command->command_next && success) {
1759 /* There is another command to *
1760 * execute, so let's do that. */
1762 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
1763 service_run_next(s, success);
1766 /* No further commands for this step, so let's
1767 * figure out what to do next */
1769 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
1773 case SERVICE_START_PRE:
1775 service_enter_start(s);
1777 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1781 assert(s->type == SERVICE_FORKING);
1783 /* Let's try to load the pid
1784 * file here if we can. We
1785 * ignore the return value,
1786 * since the PID file might
1787 * actually be created by a
1788 * START_POST script */
1792 service_load_pid_file(s);
1794 service_enter_start_post(s);
1796 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1800 case SERVICE_START_POST:
1801 if (success && s->pid_file && !s->main_pid_known) {
1804 /* Hmm, let's see if we can
1805 * load the pid now after the
1806 * start-post scripts got
1809 if ((r = service_load_pid_file(s)) < 0)
1810 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
1815 case SERVICE_RELOAD:
1817 service_enter_running(s, true);
1819 service_enter_stop(s, false);
1824 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
1827 case SERVICE_STOP_SIGTERM:
1828 case SERVICE_STOP_SIGKILL:
1829 if (main_pid_good(s) <= 0)
1830 service_enter_stop_post(s, success);
1832 /* If there is still a service
1833 * process around, wait until
1834 * that one quit, too */
1837 case SERVICE_STOP_POST:
1838 case SERVICE_FINAL_SIGTERM:
1839 case SERVICE_FINAL_SIGKILL:
1840 service_enter_dead(s, success, true);
1844 assert_not_reached("Uh, control process died at wrong time.");
1848 assert_not_reached("Got SIGCHLD for unkown PID");
1851 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
1852 Service *s = SERVICE(u);
1855 assert(elapsed == 1);
1857 assert(w == &s->timer_watch);
1861 case SERVICE_START_PRE:
1863 log_warning("%s operation timed out. Terminating.", u->meta.id);
1864 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1867 case SERVICE_START_POST:
1868 case SERVICE_RELOAD:
1869 log_warning("%s operation timed out. Stopping.", u->meta.id);
1870 service_enter_stop(s, false);
1874 log_warning("%s stopping timed out. Terminating.", u->meta.id);
1875 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1878 case SERVICE_STOP_SIGTERM:
1879 log_warning("%s stopping timed out. Killing.", u->meta.id);
1880 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
1883 case SERVICE_STOP_SIGKILL:
1884 /* Uh, wie sent a SIGKILL and it is still not gone?
1885 * Must be something we cannot kill, so let's just be
1886 * weirded out and continue */
1888 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
1889 service_enter_stop_post(s, false);
1892 case SERVICE_STOP_POST:
1893 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
1894 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1897 case SERVICE_FINAL_SIGTERM:
1898 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
1899 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1902 case SERVICE_FINAL_SIGKILL:
1903 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u->meta.id);
1904 service_enter_dead(s, false, true);
1907 case SERVICE_AUTO_RESTART:
1908 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
1909 service_enter_restart(s);
1913 assert_not_reached("Timeout at wrong time.");
1917 static void service_cgroup_notify_event(Unit *u) {
1918 Service *s = SERVICE(u);
1922 log_debug("%s: cgroup is empty", u->meta.id);
1926 /* Waiting for SIGCHLD is usually more interesting,
1927 * because it includes return codes/signals. Which is
1928 * why we ignore the cgroup events for most cases,
1929 * except when we don't know pid which to expect the
1932 case SERVICE_RUNNING:
1933 service_enter_running(s, true);
1941 static int service_enumerate(Manager *m) {
1945 char *path = NULL, *fpath = NULL, *name = NULL;
1950 STRV_FOREACH(p, m->sysvrcnd_path)
1951 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
1956 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0) {
1964 if (!(d = opendir(path))) {
1965 if (errno != ENOENT)
1966 log_warning("opendir() failed on %s: %s", path, strerror(errno));
1971 while ((de = readdir(d))) {
1972 Unit *runlevel, *service;
1974 if (ignore_file(de->d_name))
1977 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
1980 if (strlen(de->d_name) < 4)
1985 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i], de->d_name) < 0) {
1990 if (access(fpath, X_OK) < 0) {
1992 if (errno != ENOENT)
1993 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2000 if (asprintf(&name, "%s.service", de->d_name+3) < 0) {
2005 if ((r = manager_load_unit(m, name, NULL, &service)) < 0)
2008 if ((r = manager_load_unit(m, rcnd_table[i+1], NULL, &runlevel)) < 0)
2011 if (de->d_name[0] == 'S') {
2012 if ((r = unit_add_dependency(runlevel, UNIT_WANTS, service)) < 0)
2015 if ((r = unit_add_dependency(runlevel, UNIT_AFTER, service)) < 0)
2018 } else if (de->d_name[0] == 'K' &&
2019 (streq(rcnd_table[i+1], SPECIAL_RUNLEVEL0_TARGET) ||
2020 streq(rcnd_table[i+1], SPECIAL_RUNLEVEL6_TARGET))) {
2022 /* We honour K links only for
2023 * halt/reboot. For the normal
2024 * runlevels we assume the
2026 * implicitly added by the
2029 if ((r = unit_add_dependency(runlevel, UNIT_CONFLICTS, service)) < 0)
2032 if ((r = unit_add_dependency(runlevel, UNIT_BEFORE, service)) < 0)
2049 int service_set_socket_fd(Service *s, int fd) {
2053 /* This is called by the socket code when instantiating a new
2054 * service for a stream socket and the socket needs to be
2057 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2060 if (s->socket_fd >= 0)
2063 if (s->state != SERVICE_DEAD)
2070 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2071 [SERVICE_DEAD] = "dead",
2072 [SERVICE_START_PRE] = "start-pre",
2073 [SERVICE_START] = "start",
2074 [SERVICE_START_POST] = "start-post",
2075 [SERVICE_RUNNING] = "running",
2076 [SERVICE_EXITED] = "exited",
2077 [SERVICE_RELOAD] = "reload",
2078 [SERVICE_STOP] = "stop",
2079 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2080 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2081 [SERVICE_STOP_POST] = "stop-post",
2082 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2083 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2084 [SERVICE_MAINTAINANCE] = "maintainance",
2085 [SERVICE_AUTO_RESTART] = "auto-restart",
2088 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2090 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2091 [SERVICE_ONCE] = "once",
2092 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2093 [SERVICE_RESTART_ALWAYS] = "restart-always",
2096 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2098 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2099 [SERVICE_FORKING] = "forking",
2100 [SERVICE_SIMPLE] = "simple",
2101 [SERVICE_FINISH] = "finish"
2104 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2106 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2107 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2108 [SERVICE_EXEC_START] = "ExecStart",
2109 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2110 [SERVICE_EXEC_RELOAD] = "ExecReload",
2111 [SERVICE_EXEC_STOP] = "ExecStop",
2112 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2115 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2117 const UnitVTable service_vtable = {
2118 .suffix = ".service",
2120 .init = service_init,
2121 .load = service_load,
2122 .done = service_done,
2124 .dump = service_dump,
2126 .start = service_start,
2127 .stop = service_stop,
2128 .reload = service_reload,
2130 .can_reload = service_can_reload,
2132 .active_state = service_active_state,
2133 .sub_state_to_string = service_sub_state_to_string,
2135 .sigchld_event = service_sigchld_event,
2136 .timer_event = service_timer_event,
2138 .cgroup_notify_empty = service_cgroup_notify_event,
2140 .enumerate = service_enumerate