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_EXITED] = UNIT_ACTIVE,
56 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
57 [SERVICE_STOP] = UNIT_DEACTIVATING,
58 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
59 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
60 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
61 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
62 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
63 [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
64 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
67 static void service_unwatch_control_pid(Service *s) {
70 if (s->control_pid <= 0)
73 unit_unwatch_pid(UNIT(s), s->control_pid);
77 static void service_unwatch_main_pid(Service *s) {
83 unit_unwatch_pid(UNIT(s), s->main_pid);
87 static void service_done(Unit *u) {
88 Service *s = SERVICE(u);
98 free(s->sysv_runlevels);
99 s->sysv_runlevels = NULL;
101 exec_context_done(&s->exec_context);
102 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
103 s->control_command = NULL;
105 /* This will leak a process, but at least no memory or any of
107 service_unwatch_main_pid(s);
108 service_unwatch_control_pid(s);
110 unit_unwatch_timer(u, &s->timer_watch);
113 static int sysv_translate_name(const char *name, char **_r) {
115 static const char * const table[] = {
116 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
117 "$network", SPECIAL_NETWORK_TARGET,
118 "$named", SPECIAL_NSS_LOOKUP_TARGET,
119 "$portmap", SPECIAL_RPCBIND_TARGET,
120 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
121 "$syslog", SPECIAL_SYSLOG_TARGET,
122 "$time", SPECIAL_RTC_SET_TARGET
128 for (i = 0; i < ELEMENTSOF(table); i += 2)
129 if (streq(table[i], name)) {
130 if (!(r = strdup(table[i+1])))
139 if (asprintf(&r, "%s.service", name) < 0)
150 static int sysv_chkconfig_order(Service *s) {
156 if (s->sysv_start_priority < 0)
159 /* For each pair of services where at least one lacks a LSB
160 * header, we use the start priority value to order things. */
162 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
166 t = (Service*) other;
171 if (t->sysv_start_priority < 0)
174 /* If both units have modern headers we don't care
175 * about the priorities */
176 if ((!s->sysv_path || s->sysv_has_lsb) &&
177 (!t->sysv_path || t->sysv_has_lsb))
180 if (t->sysv_start_priority < s->sysv_start_priority)
182 else if (t->sysv_start_priority > s->sysv_start_priority)
187 /* FIXME: Maybe we should compare the name here lexicographically? */
189 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t))) < 0)
196 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
199 if (!(c = new0(ExecCommand, 1)))
202 if (!(c->path = strdup(path))) {
207 if (!(c->argv = strv_new(path, arg1, NULL))) {
216 static int sysv_exec_commands(Service *s) {
220 assert(s->sysv_path);
222 if (!(c = exec_command_new(s->sysv_path, "start")))
224 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
226 if (!(c = exec_command_new(s->sysv_path, "stop")))
228 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
230 if (!(c = exec_command_new(s->sysv_path, "reload")))
232 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
237 static int priority_from_rcd(Service *s, const char *init_script) {
241 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvrcnd_path)
242 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
247 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0)
255 log_warning("opendir() failed on %s: %s", path, strerror(errno));
260 while ((de = readdir(d))) {
263 if (ignore_file(de->d_name))
266 if (de->d_name[0] != 'S')
269 if (strlen(de->d_name) < 4)
272 if (!streq(de->d_name + 3, init_script))
275 /* Yay, we found it! Now decode the priority */
277 a = undecchar(de->d_name[1]);
278 b = undecchar(de->d_name[2]);
283 s->sysv_start_priority = a*10 + b;
285 log_debug("Determined priority %i from link farm for %s", s->sysv_start_priority, unit_id(UNIT(s)));
297 static int service_load_sysv_path(Service *s, const char *path) {
314 if (!(f = fopen(path, "re"))) {
315 r = errno == ENOENT ? 0 : -errno;
319 s->type = SERVICE_FORKING;
320 s->restart = SERVICE_ONCE;
323 if (!(s->sysv_path = strdup(path))) {
329 char l[LINE_MAX], *t;
331 if (!fgets(l, sizeof(l), f)) {
336 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
346 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
348 s->sysv_has_lsb = true;
352 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
358 t += strspn(t, WHITESPACE);
360 if (state == NORMAL) {
362 /* Try to parse Red Hat style chkconfig headers */
364 if (startswith(t, "chkconfig:")) {
366 char runlevels[16], *k;
370 if (sscanf(t+10, "%15s %i %*i",
372 &start_priority) != 2) {
374 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
378 if (start_priority < 0 || start_priority > 99)
379 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
381 s->sysv_start_priority = start_priority;
383 char_array_0(runlevels);
384 k = delete_chars(runlevels, WHITESPACE "-");
389 if (!(d = strdup(k))) {
394 free(s->sysv_runlevels);
395 s->sysv_runlevels = d;
399 } else if (startswith(t, "description:")) {
401 size_t k = strlen(t);
404 if (t[k-1] == '\\') {
409 if (!(d = strdup(strstrip(t+12)))) {
414 free(u->meta.description);
415 u->meta.description = d;
417 } else if (startswith(t, "pidfile:")) {
424 if (!path_is_absolute(fn)) {
425 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
429 if (!(fn = strdup(fn))) {
438 } else if (state == DESCRIPTION) {
440 /* Try to parse Red Hat style description
443 size_t k = strlen(t);
451 assert(u->meta.description);
452 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
457 free(u->meta.description);
458 u->meta.description = d;
460 } else if (state == LSB || state == LSB_DESCRIPTION) {
462 if (startswith(t, "Provides:")) {
468 FOREACH_WORD(w, z, t+9, i) {
471 if (!(n = strndup(w, z))) {
476 r = sysv_translate_name(n, &m);
485 if (unit_name_to_type(m) == UNIT_SERVICE)
486 r = unit_add_name(u, m);
488 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m)) >= 0)
489 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m);
498 } else if (startswith(t, "Required-Start:") ||
499 startswith(t, "Should-Start:")) {
505 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
508 if (!(n = strndup(w, z))) {
513 r = sysv_translate_name(n, &m);
522 r = unit_add_dependency_by_name(u, UNIT_AFTER, m);
528 } else if (startswith(t, "Default-Start:")) {
533 k = delete_chars(t+14, WHITESPACE "-");
536 if (!(d = strdup(k))) {
541 free(s->sysv_runlevels);
542 s->sysv_runlevels = d;
545 } else if (startswith(t, "Description:")) {
548 state = LSB_DESCRIPTION;
550 if (!(d = strdup(strstrip(t+12)))) {
555 free(u->meta.description);
556 u->meta.description = d;
558 } else if (startswith(t, "Short-Description:") &&
559 !u->meta.description) {
562 /* We use the short description only
563 * if no long description is set. */
567 if (!(d = strdup(strstrip(t+18)))) {
572 u->meta.description = d;
574 } else if (state == LSB_DESCRIPTION) {
576 if (startswith(l, "#\t") || startswith(l, "# ")) {
579 assert(u->meta.description);
580 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
585 free(u->meta.description);
586 u->meta.description = d;
593 /* If init scripts have no LSB header, then we enforce the
594 * ordering via the chkconfig priorities. We try to determine
595 * a priority for *all* init scripts here, since they are
596 * needed as soon as at least one non-LSB script is used. */
598 if (s->sysv_start_priority < 0) {
599 log_debug("%s has no chkconfig header, trying to determine SysV priority from link farm.", unit_id(u));
601 if ((r = priority_from_rcd(s, file_name_from_path(path))) < 0)
604 if (s->sysv_start_priority < 0)
605 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", unit_id(u));
608 if ((r = sysv_exec_commands(s)) < 0)
611 if (!s->sysv_runlevels || chars_intersect("12345", s->sysv_runlevels)) {
612 /* If there a runlevels configured for this service
613 * but none of the standard ones, then we assume this
614 * is some special kind of service (which might be
615 * needed for early boot) and don't create any links
618 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET)) < 0 ||
619 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET)) < 0)
623 /* Special setting for all SysV services */
624 s->valid_no_process = true;
626 /* Don't timeout special services during boot (like fsck) */
627 if (!chars_intersect("12345", s->sysv_runlevels))
628 s->timeout_usec = -1;
630 u->meta.load_state = UNIT_LOADED;
641 static int service_load_sysv_name(Service *s, const char *name) {
647 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
651 if (asprintf(&path, "%s/%s", *p, name) < 0)
654 assert(endswith(path, ".service"));
655 path[strlen(path)-8] = 0;
657 r = service_load_sysv_path(s, path);
663 if ((UNIT(s)->meta.load_state != UNIT_STUB))
670 static int service_load_sysv(Service *s) {
677 /* Load service data from SysV init scripts, preferably with
680 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
683 if ((t = unit_id(UNIT(s))))
684 if ((r = service_load_sysv_name(s, t)) < 0)
687 if (UNIT(s)->meta.load_state == UNIT_STUB)
688 SET_FOREACH(t, UNIT(s)->meta.names, i) {
689 if (t == unit_id(UNIT(s)))
692 if ((r == service_load_sysv_name(s, t)) < 0)
695 if (UNIT(s)->meta.load_state != UNIT_STUB)
702 static void service_init(Unit *u) {
703 Service *s = SERVICE(u);
706 assert(u->meta.load_state == UNIT_STUB);
711 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
712 s->restart_usec = DEFAULT_RESTART_USEC;
714 exec_context_init(&s->exec_context);
716 s->timer_watch.type = WATCH_INVALID;
718 s->state = SERVICE_DEAD;
720 s->sysv_start_priority = -1;
721 s->permissions_start_only = false;
722 s->root_directory_start_only = false;
723 s->valid_no_process = false;
725 s->sysv_has_lsb = false;
726 s->main_pid = s->control_pid = 0;
727 s->main_pid_known = false;
730 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
733 static int service_verify(Service *s) {
736 if (UNIT(s)->meta.load_state != UNIT_LOADED)
739 if (!s->exec_command[SERVICE_EXEC_START]) {
740 log_error("%s lacks ExecStart setting. Refusing.", unit_id(UNIT(s)));
747 static int service_load(Unit *u) {
749 Service *s = SERVICE(u);
753 /* Load a .service file */
754 if ((r = unit_load_fragment(u)) < 0)
757 /* Load a classic init script as a fallback, if we couldn't find anything */
758 if (u->meta.load_state == UNIT_STUB)
759 if ((r = service_load_sysv(s)) < 0)
762 /* Still nothing found? Then let's give up */
763 if (u->meta.load_state == UNIT_STUB)
766 /* We were able to load something, then let's add in the
767 * dropin directories. */
768 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
771 /* This is a new unit? Then let's add in some extras */
772 if (u->meta.load_state == UNIT_LOADED) {
773 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
776 if ((r = unit_add_default_cgroup(u)) < 0)
779 if ((r = sysv_chkconfig_order(s)) < 0)
783 return service_verify(s);
786 static void service_dump(Unit *u, FILE *f, const char *prefix) {
788 ServiceExecCommand c;
789 Service *s = SERVICE(u);
795 p2 = strappend(prefix, "\t");
796 prefix2 = p2 ? p2 : prefix;
799 "%sService State: %s\n"
800 "%sPermissionsStartOnly: %s\n"
801 "%sRootDirectoryStartOnly: %s\n"
802 "%sValidNoProcess: %s\n"
805 prefix, service_state_to_string(s->state),
806 prefix, yes_no(s->permissions_start_only),
807 prefix, yes_no(s->root_directory_start_only),
808 prefix, yes_no(s->valid_no_process),
809 prefix, kill_mode_to_string(s->kill_mode),
810 prefix, service_type_to_string(s->type));
812 if (s->control_pid > 0)
814 "%sControl PID: %llu\n",
815 prefix, (unsigned long long) s->control_pid);
819 "%sMain PID: %llu\n",
820 prefix, (unsigned long long) s->main_pid);
825 prefix, s->pid_file);
827 exec_context_dump(&s->exec_context, f, prefix);
829 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
831 if (!s->exec_command[c])
834 fprintf(f, "%s→ %s:\n",
835 prefix, service_exec_command_to_string(c));
837 exec_command_dump_list(s->exec_command[c], f, prefix2);
842 "%sSysV Init Script Path: %s\n"
843 "%sSysV Init Script has LSB Header: %s\n",
844 prefix, s->sysv_path,
845 prefix, yes_no(s->sysv_has_lsb));
847 if (s->sysv_start_priority >= 0)
849 "%sSysVStartPriority: %i\n",
850 prefix, s->sysv_start_priority);
852 if (s->sysv_runlevels)
853 fprintf(f, "%sSysVRunLevels: %s\n",
854 prefix, s->sysv_runlevels);
859 static int service_load_pid_file(Service *s) {
866 if (s->main_pid_known)
869 assert(s->main_pid <= 0);
874 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
877 if ((r = safe_atolu(k, &p)) < 0) {
882 if ((unsigned long) (pid_t) p != p)
885 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
886 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
887 (unsigned long long) p, s->pid_file);
891 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
892 /* FIXME: we need to do something here */
895 s->main_pid = (pid_t) p;
896 s->main_pid_known = true;
901 static int service_get_sockets(Service *s, Set **_set) {
910 /* Collects all Socket objects that belong to this
911 * service. Note that a service might have multiple sockets
912 * via multiple names. */
914 if (!(set = set_new(NULL, NULL)))
917 SET_FOREACH(t, UNIT(s)->meta.names, i) {
921 /* Look for all socket objects that go by any of our
922 * units and collect their fds */
924 if (!(k = unit_name_change_suffix(t, ".socket"))) {
929 p = manager_get_unit(UNIT(s)->meta.manager, k);
934 if ((r = set_put(set, p)) < 0)
947 static int service_notify_sockets_dead(Service *s) {
955 /* Notifies all our sockets when we die */
957 if ((r = service_get_sockets(s, &set)) < 0)
960 SET_FOREACH(sock, set, i)
961 socket_notify_service_dead(sock);
968 static void service_set_state(Service *s, ServiceState state) {
969 ServiceState old_state;
972 old_state = s->state;
975 if (state != SERVICE_START_PRE &&
976 state != SERVICE_START &&
977 state != SERVICE_START_POST &&
978 state != SERVICE_RELOAD &&
979 state != SERVICE_STOP &&
980 state != SERVICE_STOP_SIGTERM &&
981 state != SERVICE_STOP_SIGKILL &&
982 state != SERVICE_STOP_POST &&
983 state != SERVICE_FINAL_SIGTERM &&
984 state != SERVICE_FINAL_SIGKILL &&
985 state != SERVICE_AUTO_RESTART)
986 unit_unwatch_timer(UNIT(s), &s->timer_watch);
988 if (state != SERVICE_START &&
989 state != SERVICE_START_POST &&
990 state != SERVICE_RUNNING &&
991 state != SERVICE_RELOAD &&
992 state != SERVICE_STOP &&
993 state != SERVICE_STOP_SIGTERM &&
994 state != SERVICE_STOP_SIGKILL)
995 service_unwatch_main_pid(s);
997 if (state != SERVICE_START_PRE &&
998 state != SERVICE_START &&
999 state != SERVICE_START_POST &&
1000 state != SERVICE_RELOAD &&
1001 state != SERVICE_STOP &&
1002 state != SERVICE_STOP_SIGTERM &&
1003 state != SERVICE_STOP_SIGKILL &&
1004 state != SERVICE_STOP_POST &&
1005 state != SERVICE_FINAL_SIGTERM &&
1006 state != SERVICE_FINAL_SIGKILL) {
1007 service_unwatch_control_pid(s);
1008 s->control_command = NULL;
1011 if (state == SERVICE_DEAD ||
1012 state == SERVICE_STOP ||
1013 state == SERVICE_STOP_SIGTERM ||
1014 state == SERVICE_STOP_SIGKILL ||
1015 state == SERVICE_STOP_POST ||
1016 state == SERVICE_FINAL_SIGTERM ||
1017 state == SERVICE_FINAL_SIGKILL ||
1018 state == SERVICE_MAINTAINANCE ||
1019 state == SERVICE_AUTO_RESTART)
1020 service_notify_sockets_dead(s);
1022 if (old_state != state)
1023 log_debug("%s changed %s → %s", unit_id(UNIT(s)), service_state_to_string(old_state), service_state_to_string(state));
1025 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1028 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1032 unsigned rn_fds = 0;
1040 if ((r = service_get_sockets(s, &set)) < 0)
1043 SET_FOREACH(sock, set, i) {
1047 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1059 if (!(t = new(int, rn_fds+cn_fds))) {
1065 memcpy(t, rfds, rn_fds);
1066 memcpy(t+rn_fds, cfds, cn_fds);
1071 rn_fds = rn_fds+cn_fds;
1089 static int service_spawn(
1094 bool apply_permissions,
1108 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1112 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1115 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1117 if ((r = exec_spawn(c,
1122 UNIT(s)->meta.manager->confirm_spawn,
1123 UNIT(s)->meta.cgroup_bondings,
1127 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1128 /* FIXME: we need to do something here */
1140 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1145 static int main_pid_good(Service *s) {
1148 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1151 /* If we know the pid file, then lets just check if it is
1153 if (s->main_pid_known)
1154 return s->main_pid > 0;
1156 /* We don't know the pid */
1160 static int control_pid_good(Service *s) {
1163 return s->control_pid > 0;
1166 static int cgroup_good(Service *s) {
1171 if (s->valid_no_process)
1174 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1180 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1187 if (allow_restart &&
1188 (s->restart == SERVICE_RESTART_ALWAYS ||
1189 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1191 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1194 service_set_state(s, SERVICE_AUTO_RESTART);
1196 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1201 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
1202 service_enter_dead(s, false, false);
1205 static void service_enter_signal(Service *s, ServiceState state, bool success);
1207 static void service_enter_stop_post(Service *s, bool success) {
1214 service_unwatch_control_pid(s);
1216 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1217 if ((r = service_spawn(s,
1221 !s->permissions_start_only,
1222 !s->root_directory_start_only,
1223 &s->control_pid)) < 0)
1227 service_set_state(s, SERVICE_STOP_POST);
1229 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1234 log_warning("%s failed to run stop-post executable: %s", unit_id(UNIT(s)), strerror(-r));
1235 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1238 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1247 if (s->kill_mode != KILL_NONE) {
1248 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1250 if (s->kill_mode == KILL_CONTROL_GROUP) {
1252 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1253 if (r != -EAGAIN && r != -ESRCH)
1262 if (s->main_pid > 0) {
1263 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1269 if (s->control_pid > 0) {
1270 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1282 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1285 service_set_state(s, state);
1286 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1287 service_enter_stop_post(s, true);
1289 service_enter_dead(s, true, true);
1294 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
1296 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1297 service_enter_stop_post(s, false);
1299 service_enter_dead(s, false, true);
1302 static void service_enter_stop(Service *s, bool success) {
1309 service_unwatch_control_pid(s);
1311 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1312 if ((r = service_spawn(s,
1316 !s->permissions_start_only,
1317 !s->root_directory_start_only,
1318 &s->control_pid)) < 0)
1321 service_set_state(s, SERVICE_STOP);
1323 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1328 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
1329 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1332 static void service_enter_running(Service *s, bool success) {
1338 if (main_pid_good(s) != 0 && cgroup_good(s) != 0)
1339 service_set_state(s, SERVICE_RUNNING);
1340 else if (s->valid_no_process)
1341 service_set_state(s, SERVICE_EXITED);
1343 service_enter_stop(s, true);
1346 static void service_enter_start_post(Service *s) {
1350 service_unwatch_control_pid(s);
1352 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1353 if ((r = service_spawn(s,
1357 !s->permissions_start_only,
1358 !s->root_directory_start_only,
1359 &s->control_pid)) < 0)
1363 service_set_state(s, SERVICE_START_POST);
1365 service_enter_running(s, true);
1370 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
1371 service_enter_stop(s, false);
1374 static void service_enter_start(Service *s) {
1380 assert(s->exec_command[SERVICE_EXEC_START]);
1381 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1383 if (s->type == SERVICE_FORKING)
1384 service_unwatch_control_pid(s);
1386 service_unwatch_main_pid(s);
1388 if ((r = service_spawn(s,
1389 s->exec_command[SERVICE_EXEC_START],
1390 s->type == SERVICE_FORKING,
1397 if (s->type == SERVICE_SIMPLE) {
1398 /* For simple services we immediately start
1399 * the START_POST binaries. */
1402 s->main_pid_known = true;
1404 service_enter_start_post(s);
1406 } else if (s->type == SERVICE_FORKING) {
1408 /* For forking services we wait until the start
1409 * process exited. */
1411 s->control_pid = pid;
1413 s->control_command = s->exec_command[SERVICE_EXEC_START];
1414 service_set_state(s, SERVICE_START);
1416 } else if (s->type == SERVICE_FINISH) {
1418 /* For finishing services we wait until the start
1419 * process exited, too, but it is our main process. */
1422 s->main_pid_known = true;
1424 s->control_command = s->exec_command[SERVICE_EXEC_START];
1425 service_set_state(s, SERVICE_START);
1427 assert_not_reached("Unknown service type");
1432 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
1433 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1436 static void service_enter_start_pre(Service *s) {
1441 service_unwatch_control_pid(s);
1443 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1444 if ((r = service_spawn(s,
1448 !s->permissions_start_only,
1449 !s->root_directory_start_only,
1450 &s->control_pid)) < 0)
1453 service_set_state(s, SERVICE_START_PRE);
1455 service_enter_start(s);
1460 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
1461 service_enter_dead(s, false, true);
1464 static void service_enter_restart(Service *s) {
1468 service_enter_dead(s, true, false);
1470 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1473 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
1478 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
1479 service_enter_dead(s, false, false);
1482 static void service_enter_reload(Service *s) {
1487 service_unwatch_control_pid(s);
1489 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1490 if ((r = service_spawn(s,
1494 !s->permissions_start_only,
1495 !s->root_directory_start_only,
1496 &s->control_pid)) < 0)
1499 service_set_state(s, SERVICE_RELOAD);
1501 service_enter_running(s, true);
1506 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
1507 service_enter_stop(s, false);
1510 static void service_run_next(Service *s, bool success) {
1514 assert(s->control_command);
1515 assert(s->control_command->command_next);
1520 s->control_command = s->control_command->command_next;
1522 service_unwatch_control_pid(s);
1524 if ((r = service_spawn(s,
1528 !s->permissions_start_only,
1529 !s->root_directory_start_only,
1530 &s->control_pid)) < 0)
1536 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
1538 if (s->state == SERVICE_START_PRE)
1539 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1540 else if (s->state == SERVICE_STOP)
1541 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1542 else if (s->state == SERVICE_STOP_POST)
1543 service_enter_dead(s, false, true);
1545 service_enter_stop(s, false);
1548 static int service_start(Unit *u) {
1549 Service *s = SERVICE(u);
1553 /* We cannot fulfill this request right now, try again later
1555 if (s->state == SERVICE_STOP ||
1556 s->state == SERVICE_STOP_SIGTERM ||
1557 s->state == SERVICE_STOP_SIGKILL ||
1558 s->state == SERVICE_STOP_POST ||
1559 s->state == SERVICE_FINAL_SIGTERM ||
1560 s->state == SERVICE_FINAL_SIGKILL)
1563 /* Already on it! */
1564 if (s->state == SERVICE_START_PRE ||
1565 s->state == SERVICE_START ||
1566 s->state == SERVICE_START_POST)
1569 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1571 /* Make sure we don't enter a busy loop of some kind. */
1572 if (!ratelimit_test(&s->ratelimit)) {
1573 log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u));
1578 s->main_pid_known = false;
1580 service_enter_start_pre(s);
1584 static int service_stop(Unit *u) {
1585 Service *s = SERVICE(u);
1589 /* Cannot do this now */
1590 if (s->state == SERVICE_START_PRE ||
1591 s->state == SERVICE_START ||
1592 s->state == SERVICE_START_POST ||
1593 s->state == SERVICE_RELOAD)
1597 if (s->state == SERVICE_STOP ||
1598 s->state == SERVICE_STOP_SIGTERM ||
1599 s->state == SERVICE_STOP_SIGKILL ||
1600 s->state == SERVICE_STOP_POST ||
1601 s->state == SERVICE_FINAL_SIGTERM ||
1602 s->state == SERVICE_FINAL_SIGKILL)
1605 if (s->state == SERVICE_AUTO_RESTART) {
1606 service_set_state(s, SERVICE_DEAD);
1610 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1612 service_enter_stop(s, true);
1616 static int service_reload(Unit *u) {
1617 Service *s = SERVICE(u);
1621 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1623 service_enter_reload(s);
1627 static bool service_can_reload(Unit *u) {
1628 Service *s = SERVICE(u);
1632 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1635 static UnitActiveState service_active_state(Unit *u) {
1638 return state_translation_table[SERVICE(u)->state];
1641 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1642 Service *s = SERVICE(u);
1648 success = code == CLD_EXITED && status == 0;
1649 s->failure = s->failure || !success;
1651 if (s->main_pid == pid) {
1653 exec_status_fill(&s->main_exec_status, pid, code, status);
1656 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1657 assert(s->exec_command[SERVICE_EXEC_START]);
1658 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1661 log_debug("%s: main process exited, code=%s, status=%i", unit_id(u), sigchld_code_to_string(code), status);
1663 /* The service exited, so the service is officially
1668 case SERVICE_START_POST:
1669 case SERVICE_RELOAD:
1671 /* Need to wait until the operation is
1676 assert(s->type == SERVICE_FINISH);
1678 /* This was our main goal, so let's go on */
1680 service_enter_start_post(s);
1682 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1685 case SERVICE_RUNNING:
1686 service_enter_running(s, success);
1689 case SERVICE_STOP_SIGTERM:
1690 case SERVICE_STOP_SIGKILL:
1692 if (!control_pid_good(s))
1693 service_enter_stop_post(s, success);
1695 /* If there is still a control process, wait for that first */
1699 assert_not_reached("Uh, main process died at wrong time.");
1702 } else if (s->control_pid == pid) {
1703 assert(s->control_command);
1705 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1708 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
1710 /* If we are shutting things down anyway we
1711 * don't care about failing commands. */
1713 if (s->control_command->command_next && success) {
1715 /* There is another command to *
1716 * execute, so let's do that. */
1718 log_debug("%s running next command for state %s", unit_id(u), service_state_to_string(s->state));
1719 service_run_next(s, success);
1722 /* No further commands for this step, so let's
1723 * figure out what to do next */
1725 log_debug("%s got final SIGCHLD for state %s", unit_id(u), service_state_to_string(s->state));
1729 case SERVICE_START_PRE:
1731 service_enter_start(s);
1733 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1737 assert(s->type == SERVICE_FORKING);
1739 /* Let's try to load the pid
1740 * file here if we can. We
1741 * ignore the return value,
1742 * since the PID file might
1743 * actually be created by a
1744 * START_POST script */
1748 service_load_pid_file(s);
1750 service_enter_start_post(s);
1752 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1756 case SERVICE_START_POST:
1757 if (success && s->pid_file && !s->main_pid_known) {
1760 /* Hmm, let's see if we can
1761 * load the pid now after the
1762 * start-post scripts got
1765 if ((r = service_load_pid_file(s)) < 0)
1766 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
1771 case SERVICE_RELOAD:
1773 service_enter_running(s, true);
1775 service_enter_stop(s, false);
1780 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
1783 case SERVICE_STOP_SIGTERM:
1784 case SERVICE_STOP_SIGKILL:
1785 if (main_pid_good(s) <= 0)
1786 service_enter_stop_post(s, success);
1788 /* If there is still a service
1789 * process around, wait until
1790 * that one quit, too */
1793 case SERVICE_STOP_POST:
1794 case SERVICE_FINAL_SIGTERM:
1795 case SERVICE_FINAL_SIGKILL:
1796 service_enter_dead(s, success, true);
1800 assert_not_reached("Uh, control process died at wrong time.");
1804 assert_not_reached("Got SIGCHLD for unkown PID");
1807 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
1808 Service *s = SERVICE(u);
1811 assert(elapsed == 1);
1813 assert(w == &s->timer_watch);
1817 case SERVICE_START_PRE:
1819 log_warning("%s operation timed out. Terminating.", unit_id(u));
1820 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1823 case SERVICE_START_POST:
1824 case SERVICE_RELOAD:
1825 log_warning("%s operation timed out. Stopping.", unit_id(u));
1826 service_enter_stop(s, false);
1830 log_warning("%s stopping timed out. Terminating.", unit_id(u));
1831 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1834 case SERVICE_STOP_SIGTERM:
1835 log_warning("%s stopping timed out. Killing.", unit_id(u));
1836 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
1839 case SERVICE_STOP_SIGKILL:
1840 /* Uh, wie sent a SIGKILL and it is still not gone?
1841 * Must be something we cannot kill, so let's just be
1842 * weirded out and continue */
1844 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
1845 service_enter_stop_post(s, false);
1848 case SERVICE_STOP_POST:
1849 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
1850 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1853 case SERVICE_FINAL_SIGTERM:
1854 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
1855 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1858 case SERVICE_FINAL_SIGKILL:
1859 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
1860 service_enter_dead(s, false, true);
1863 case SERVICE_AUTO_RESTART:
1864 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
1865 service_enter_restart(s);
1869 assert_not_reached("Timeout at wrong time.");
1873 static void service_cgroup_notify_event(Unit *u) {
1874 Service *s = SERVICE(u);
1878 log_debug("%s: cgroup is empty", unit_id(u));
1882 /* Waiting for SIGCHLD is usually more interesting,
1883 * because it includes return codes/signals. Which is
1884 * why we ignore the cgroup events for most cases,
1885 * except when we don't know pid which to expect the
1888 case SERVICE_RUNNING:
1889 service_enter_running(s, true);
1897 static int service_enumerate(Manager *m) {
1901 char *path = NULL, *fpath = NULL, *name = NULL;
1906 STRV_FOREACH(p, m->sysvrcnd_path)
1907 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
1912 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0) {
1920 if (!(d = opendir(path))) {
1921 if (errno != ENOENT)
1922 log_warning("opendir() failed on %s: %s", path, strerror(errno));
1927 while ((de = readdir(d))) {
1928 Unit *runlevel, *service;
1930 if (ignore_file(de->d_name))
1933 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
1936 if (strlen(de->d_name) < 4)
1941 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i], de->d_name) < 0) {
1946 if (access(fpath, X_OK) < 0) {
1948 if (errno != ENOENT)
1949 log_warning("access() failed on %s: %s", fpath, strerror(errno));
1956 if (asprintf(&name, "%s.service", de->d_name+3) < 0) {
1961 if ((r = manager_load_unit(m, name, &service)) < 0)
1964 if ((r = manager_load_unit(m, rcnd_table[i+1], &runlevel)) < 0)
1967 if (de->d_name[0] == 'S') {
1968 if ((r = unit_add_dependency(runlevel, UNIT_WANTS, service)) < 0)
1971 if ((r = unit_add_dependency(runlevel, UNIT_AFTER, service)) < 0)
1974 } else if (de->d_name[0] == 'K' &&
1975 (streq(rcnd_table[i+1], SPECIAL_RUNLEVEL0_TARGET) ||
1976 streq(rcnd_table[i+1], SPECIAL_RUNLEVEL6_TARGET))) {
1978 /* We honour K links only for
1979 * halt/reboot. For the normal
1980 * runlevels we assume the
1982 * implicitly added by the
1985 if ((r = unit_add_dependency(runlevel, UNIT_CONFLICTS, service)) < 0)
1988 if ((r = unit_add_dependency(runlevel, UNIT_BEFORE, service)) < 0)
2005 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2006 [SERVICE_DEAD] = "dead",
2007 [SERVICE_START_PRE] = "start-pre",
2008 [SERVICE_START] = "start",
2009 [SERVICE_START_POST] = "start-post",
2010 [SERVICE_RUNNING] = "running",
2011 [SERVICE_EXITED] = "exited",
2012 [SERVICE_RELOAD] = "reload",
2013 [SERVICE_STOP] = "stop",
2014 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2015 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2016 [SERVICE_STOP_POST] = "stop-post",
2017 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2018 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2019 [SERVICE_MAINTAINANCE] = "maintainance",
2020 [SERVICE_AUTO_RESTART] = "auto-restart",
2023 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2025 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2026 [SERVICE_ONCE] = "once",
2027 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2028 [SERVICE_RESTART_ALWAYS] = "restart-always",
2031 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2033 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2034 [SERVICE_FORKING] = "forking",
2035 [SERVICE_SIMPLE] = "simple",
2036 [SERVICE_FINISH] = "finish"
2039 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2041 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2042 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2043 [SERVICE_EXEC_START] = "ExecStart",
2044 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2045 [SERVICE_EXEC_RELOAD] = "ExecReload",
2046 [SERVICE_EXEC_STOP] = "ExecStop",
2047 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2050 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2052 const UnitVTable service_vtable = {
2053 .suffix = ".service",
2055 .init = service_init,
2056 .load = service_load,
2057 .done = service_done,
2059 .dump = service_dump,
2061 .start = service_start,
2062 .stop = service_stop,
2063 .reload = service_reload,
2065 .can_reload = service_can_reload,
2067 .active_state = service_active_state,
2069 .sigchld_event = service_sigchld_event,
2070 .timer_event = service_timer_event,
2072 .cgroup_notify_empty = service_cgroup_notify_event,
2074 .enumerate = service_enumerate