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 u->meta.load_state = UNIT_LOADED;
637 static int service_load_sysv_name(Service *s, const char *name) {
643 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
647 if (asprintf(&path, "%s/%s", *p, name) < 0)
650 assert(endswith(path, ".service"));
651 path[strlen(path)-8] = 0;
653 r = service_load_sysv_path(s, path);
659 if ((UNIT(s)->meta.load_state != UNIT_STUB))
666 static int service_load_sysv(Service *s) {
673 /* Load service data from SysV init scripts, preferably with
676 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
679 if ((t = unit_id(UNIT(s))))
680 if ((r = service_load_sysv_name(s, t)) < 0)
683 if (UNIT(s)->meta.load_state == UNIT_STUB)
684 SET_FOREACH(t, UNIT(s)->meta.names, i) {
685 if (t == unit_id(UNIT(s)))
688 if ((r == service_load_sysv_name(s, t)) < 0)
691 if (UNIT(s)->meta.load_state != UNIT_STUB)
698 static void service_init(Unit *u) {
699 Service *s = SERVICE(u);
702 assert(u->meta.load_state == UNIT_STUB);
707 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
708 s->restart_usec = DEFAULT_RESTART_USEC;
710 exec_context_init(&s->exec_context);
712 s->timer_watch.type = WATCH_INVALID;
714 s->state = SERVICE_DEAD;
716 s->sysv_start_priority = -1;
717 s->permissions_start_only = false;
718 s->root_directory_start_only = false;
719 s->valid_no_process = false;
721 s->sysv_has_lsb = false;
722 s->main_pid = s->control_pid = 0;
723 s->main_pid_known = false;
726 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
729 static int service_load(Unit *u) {
731 Service *s = SERVICE(u);
735 /* Load a .service file */
736 if ((r = unit_load_fragment(u)) < 0)
739 /* Load a classic init script as a fallback, if we couldn't find anything */
740 if (u->meta.load_state == UNIT_STUB)
741 if ((r = service_load_sysv(s)) < 0)
744 /* Still nothing found? Then let's give up */
745 if (u->meta.load_state == UNIT_STUB)
748 /* We were able to load something, then let's add in the
749 * dropin directories. */
750 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
753 /* This is a new unit? Then let's add in some extras */
754 if (u->meta.load_state == UNIT_LOADED) {
755 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
758 if ((r = unit_add_default_cgroup(u)) < 0)
761 if ((r = sysv_chkconfig_order(s)) < 0)
768 static void service_dump(Unit *u, FILE *f, const char *prefix) {
770 ServiceExecCommand c;
771 Service *s = SERVICE(u);
777 p2 = strappend(prefix, "\t");
778 prefix2 = p2 ? p2 : prefix;
781 "%sService State: %s\n"
782 "%sPermissionsStartOnly: %s\n"
783 "%sRootDirectoryStartOnly: %s\n"
784 "%sValidNoProcess: %s\n"
787 prefix, service_state_to_string(s->state),
788 prefix, yes_no(s->permissions_start_only),
789 prefix, yes_no(s->root_directory_start_only),
790 prefix, yes_no(s->valid_no_process),
791 prefix, kill_mode_to_string(s->kill_mode),
792 prefix, service_type_to_string(s->type));
794 if (s->control_pid > 0)
796 "%sControl PID: %llu\n",
797 prefix, (unsigned long long) s->control_pid);
801 "%sMain PID: %llu\n",
802 prefix, (unsigned long long) s->main_pid);
807 prefix, s->pid_file);
809 exec_context_dump(&s->exec_context, f, prefix);
811 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
813 if (!s->exec_command[c])
816 fprintf(f, "%s→ %s:\n",
817 prefix, service_exec_command_to_string(c));
819 exec_command_dump_list(s->exec_command[c], f, prefix2);
824 "%sSysV Init Script Path: %s\n"
825 "%sSysV Init Script has LSB Header: %s\n",
826 prefix, s->sysv_path,
827 prefix, yes_no(s->sysv_has_lsb));
829 if (s->sysv_start_priority >= 0)
831 "%sSysVStartPriority: %i\n",
832 prefix, s->sysv_start_priority);
834 if (s->sysv_runlevels)
835 fprintf(f, "%sSysVRunLevels: %s\n",
836 prefix, s->sysv_runlevels);
841 static int service_load_pid_file(Service *s) {
848 if (s->main_pid_known)
851 assert(s->main_pid <= 0);
856 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
859 if ((r = safe_atolu(k, &p)) < 0) {
864 if ((unsigned long) (pid_t) p != p)
867 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
868 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
869 (unsigned long long) p, s->pid_file);
873 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
874 /* FIXME: we need to do something here */
877 s->main_pid = (pid_t) p;
878 s->main_pid_known = true;
883 static int service_get_sockets(Service *s, Set **_set) {
892 /* Collects all Socket objects that belong to this
893 * service. Note that a service might have multiple sockets
894 * via multiple names. */
896 if (!(set = set_new(NULL, NULL)))
899 SET_FOREACH(t, UNIT(s)->meta.names, i) {
903 /* Look for all socket objects that go by any of our
904 * units and collect their fds */
906 if (!(k = unit_name_change_suffix(t, ".socket"))) {
911 p = manager_get_unit(UNIT(s)->meta.manager, k);
916 if ((r = set_put(set, p)) < 0)
929 static int service_notify_sockets_dead(Service *s) {
937 /* Notifies all our sockets when we die */
939 if ((r = service_get_sockets(s, &set)) < 0)
942 SET_FOREACH(sock, set, i)
943 socket_notify_service_dead(sock);
950 static void service_set_state(Service *s, ServiceState state) {
951 ServiceState old_state;
954 old_state = s->state;
957 if (state != SERVICE_START_PRE &&
958 state != SERVICE_START &&
959 state != SERVICE_START_POST &&
960 state != SERVICE_RELOAD &&
961 state != SERVICE_STOP &&
962 state != SERVICE_STOP_SIGTERM &&
963 state != SERVICE_STOP_SIGKILL &&
964 state != SERVICE_STOP_POST &&
965 state != SERVICE_FINAL_SIGTERM &&
966 state != SERVICE_FINAL_SIGKILL &&
967 state != SERVICE_AUTO_RESTART)
968 unit_unwatch_timer(UNIT(s), &s->timer_watch);
970 if (state != SERVICE_START &&
971 state != SERVICE_START_POST &&
972 state != SERVICE_RUNNING &&
973 state != SERVICE_RELOAD &&
974 state != SERVICE_STOP &&
975 state != SERVICE_STOP_SIGTERM &&
976 state != SERVICE_STOP_SIGKILL)
977 service_unwatch_main_pid(s);
979 if (state != SERVICE_START_PRE &&
980 state != SERVICE_START &&
981 state != SERVICE_START_POST &&
982 state != SERVICE_RELOAD &&
983 state != SERVICE_STOP &&
984 state != SERVICE_STOP_SIGTERM &&
985 state != SERVICE_STOP_SIGKILL &&
986 state != SERVICE_STOP_POST &&
987 state != SERVICE_FINAL_SIGTERM &&
988 state != SERVICE_FINAL_SIGKILL) {
989 service_unwatch_control_pid(s);
990 s->control_command = NULL;
993 if (state == SERVICE_DEAD ||
994 state == SERVICE_STOP ||
995 state == SERVICE_STOP_SIGTERM ||
996 state == SERVICE_STOP_SIGKILL ||
997 state == SERVICE_STOP_POST ||
998 state == SERVICE_FINAL_SIGTERM ||
999 state == SERVICE_FINAL_SIGKILL ||
1000 state == SERVICE_MAINTAINANCE ||
1001 state == SERVICE_AUTO_RESTART)
1002 service_notify_sockets_dead(s);
1004 if (old_state != state)
1005 log_debug("%s changed %s → %s", unit_id(UNIT(s)), service_state_to_string(old_state), service_state_to_string(state));
1007 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1010 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1014 unsigned rn_fds = 0;
1022 if ((r = service_get_sockets(s, &set)) < 0)
1025 SET_FOREACH(sock, set, i) {
1029 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1041 if (!(t = new(int, rn_fds+cn_fds))) {
1047 memcpy(t, rfds, rn_fds);
1048 memcpy(t+rn_fds, cfds, cn_fds);
1053 rn_fds = rn_fds+cn_fds;
1071 static int service_spawn(
1076 bool apply_permissions,
1090 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1094 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1097 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1099 if ((r = exec_spawn(c,
1104 UNIT(s)->meta.manager->confirm_spawn,
1105 UNIT(s)->meta.cgroup_bondings,
1109 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1110 /* FIXME: we need to do something here */
1122 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1127 static int main_pid_good(Service *s) {
1130 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1133 /* If we know the pid file, then lets just check if it is
1135 if (s->main_pid_known)
1136 return s->main_pid > 0;
1138 /* We don't know the pid */
1142 static int control_pid_good(Service *s) {
1145 return s->control_pid > 0;
1148 static int cgroup_good(Service *s) {
1153 if (s->valid_no_process)
1156 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1162 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1169 if (allow_restart &&
1170 (s->restart == SERVICE_RESTART_ALWAYS ||
1171 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1173 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1176 service_set_state(s, SERVICE_AUTO_RESTART);
1178 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1183 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
1184 service_enter_dead(s, false, false);
1187 static void service_enter_signal(Service *s, ServiceState state, bool success);
1189 static void service_enter_stop_post(Service *s, bool success) {
1196 service_unwatch_control_pid(s);
1198 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1199 if ((r = service_spawn(s,
1203 !s->permissions_start_only,
1204 !s->root_directory_start_only,
1205 &s->control_pid)) < 0)
1209 service_set_state(s, SERVICE_STOP_POST);
1211 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1216 log_warning("%s failed to run stop-post executable: %s", unit_id(UNIT(s)), strerror(-r));
1217 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1220 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1229 if (s->kill_mode != KILL_NONE) {
1230 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1232 if (s->kill_mode == KILL_CONTROL_GROUP) {
1234 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1235 if (r != -EAGAIN && r != -ESRCH)
1244 if (s->main_pid > 0) {
1245 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1251 if (s->control_pid > 0) {
1252 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1264 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1267 service_set_state(s, state);
1268 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1269 service_enter_stop_post(s, true);
1271 service_enter_dead(s, true, true);
1276 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
1278 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1279 service_enter_stop_post(s, false);
1281 service_enter_dead(s, false, true);
1284 static void service_enter_stop(Service *s, bool success) {
1291 service_unwatch_control_pid(s);
1293 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1294 if ((r = service_spawn(s,
1298 !s->permissions_start_only,
1299 !s->root_directory_start_only,
1300 &s->control_pid)) < 0)
1303 service_set_state(s, SERVICE_STOP);
1305 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1310 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
1311 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1314 static void service_enter_running(Service *s, bool success) {
1320 if (main_pid_good(s) != 0 && cgroup_good(s) != 0)
1321 service_set_state(s, SERVICE_RUNNING);
1322 else if (s->valid_no_process)
1323 service_set_state(s, SERVICE_EXITED);
1325 service_enter_stop(s, true);
1328 static void service_enter_start_post(Service *s) {
1332 service_unwatch_control_pid(s);
1334 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1335 if ((r = service_spawn(s,
1339 !s->permissions_start_only,
1340 !s->root_directory_start_only,
1341 &s->control_pid)) < 0)
1345 service_set_state(s, SERVICE_START_POST);
1347 service_enter_running(s, true);
1352 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
1353 service_enter_stop(s, false);
1356 static void service_enter_start(Service *s) {
1362 assert(s->exec_command[SERVICE_EXEC_START]);
1363 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1365 if (s->type == SERVICE_FORKING)
1366 service_unwatch_control_pid(s);
1368 service_unwatch_main_pid(s);
1370 if ((r = service_spawn(s,
1371 s->exec_command[SERVICE_EXEC_START],
1372 s->type == SERVICE_FORKING,
1379 if (s->type == SERVICE_SIMPLE) {
1380 /* For simple services we immediately start
1381 * the START_POST binaries. */
1384 s->main_pid_known = true;
1386 service_enter_start_post(s);
1388 } else if (s->type == SERVICE_FORKING) {
1390 /* For forking services we wait until the start
1391 * process exited. */
1393 s->control_pid = pid;
1395 s->control_command = s->exec_command[SERVICE_EXEC_START];
1396 service_set_state(s, SERVICE_START);
1398 } else if (s->type == SERVICE_FINISH) {
1400 /* For finishing services we wait until the start
1401 * process exited, too, but it is our main process. */
1404 s->main_pid_known = true;
1406 s->control_command = s->exec_command[SERVICE_EXEC_START];
1407 service_set_state(s, SERVICE_START);
1409 assert_not_reached("Unknown service type");
1414 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
1415 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1418 static void service_enter_start_pre(Service *s) {
1423 service_unwatch_control_pid(s);
1425 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1426 if ((r = service_spawn(s,
1430 !s->permissions_start_only,
1431 !s->root_directory_start_only,
1432 &s->control_pid)) < 0)
1435 service_set_state(s, SERVICE_START_PRE);
1437 service_enter_start(s);
1442 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
1443 service_enter_dead(s, false, true);
1446 static void service_enter_restart(Service *s) {
1450 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1453 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
1454 service_enter_dead(s, true, false);
1459 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
1460 service_enter_dead(s, false, false);
1463 static void service_enter_reload(Service *s) {
1468 service_unwatch_control_pid(s);
1470 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1471 if ((r = service_spawn(s,
1475 !s->permissions_start_only,
1476 !s->root_directory_start_only,
1477 &s->control_pid)) < 0)
1480 service_set_state(s, SERVICE_RELOAD);
1482 service_enter_running(s, true);
1487 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
1488 service_enter_stop(s, false);
1491 static void service_run_next(Service *s, bool success) {
1495 assert(s->control_command);
1496 assert(s->control_command->command_next);
1501 s->control_command = s->control_command->command_next;
1503 service_unwatch_control_pid(s);
1505 if ((r = service_spawn(s,
1509 !s->permissions_start_only,
1510 !s->root_directory_start_only,
1511 &s->control_pid)) < 0)
1517 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
1519 if (s->state == SERVICE_START_PRE)
1520 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1521 else if (s->state == SERVICE_STOP)
1522 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1523 else if (s->state == SERVICE_STOP_POST)
1524 service_enter_dead(s, false, true);
1526 service_enter_stop(s, false);
1529 static int service_start(Unit *u) {
1530 Service *s = SERVICE(u);
1534 /* We cannot fulfill this request right now, try again later
1536 if (s->state == SERVICE_STOP ||
1537 s->state == SERVICE_STOP_SIGTERM ||
1538 s->state == SERVICE_STOP_SIGKILL ||
1539 s->state == SERVICE_STOP_POST ||
1540 s->state == SERVICE_FINAL_SIGTERM ||
1541 s->state == SERVICE_FINAL_SIGKILL)
1544 /* Already on it! */
1545 if (s->state == SERVICE_START_PRE ||
1546 s->state == SERVICE_START ||
1547 s->state == SERVICE_START_POST)
1550 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1552 /* Make sure we don't enter a busy loop of some kind. */
1553 if (!ratelimit_test(&s->ratelimit)) {
1554 log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u));
1559 s->main_pid_known = false;
1561 service_enter_start_pre(s);
1565 static int service_stop(Unit *u) {
1566 Service *s = SERVICE(u);
1570 /* Cannot do this now */
1571 if (s->state == SERVICE_START_PRE ||
1572 s->state == SERVICE_START ||
1573 s->state == SERVICE_START_POST ||
1574 s->state == SERVICE_RELOAD)
1578 if (s->state == SERVICE_STOP ||
1579 s->state == SERVICE_STOP_SIGTERM ||
1580 s->state == SERVICE_STOP_SIGKILL ||
1581 s->state == SERVICE_STOP_POST ||
1582 s->state == SERVICE_FINAL_SIGTERM ||
1583 s->state == SERVICE_FINAL_SIGKILL)
1586 if (s->state == SERVICE_AUTO_RESTART) {
1587 service_set_state(s, SERVICE_DEAD);
1591 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1593 service_enter_stop(s, true);
1597 static int service_reload(Unit *u) {
1598 Service *s = SERVICE(u);
1602 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1604 service_enter_reload(s);
1608 static bool service_can_reload(Unit *u) {
1609 Service *s = SERVICE(u);
1613 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1616 static UnitActiveState service_active_state(Unit *u) {
1619 return state_translation_table[SERVICE(u)->state];
1622 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1623 Service *s = SERVICE(u);
1629 success = code == CLD_EXITED && status == 0;
1630 s->failure = s->failure || !success;
1632 if (s->main_pid == pid) {
1634 exec_status_fill(&s->main_exec_status, pid, code, status);
1637 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1638 assert(s->exec_command[SERVICE_EXEC_START]);
1639 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1642 log_debug("%s: main process exited, code=%s, status=%i", unit_id(u), sigchld_code_to_string(code), status);
1644 /* The service exited, so the service is officially
1649 case SERVICE_START_POST:
1650 case SERVICE_RELOAD:
1652 /* Need to wait until the operation is
1657 assert(s->type == SERVICE_FINISH);
1659 /* This was our main goal, so let's go on */
1661 service_enter_start_post(s);
1663 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1666 case SERVICE_RUNNING:
1667 service_enter_running(s, success);
1670 case SERVICE_STOP_SIGTERM:
1671 case SERVICE_STOP_SIGKILL:
1673 if (!control_pid_good(s))
1674 service_enter_stop_post(s, success);
1676 /* If there is still a control process, wait for that first */
1680 assert_not_reached("Uh, main process died at wrong time.");
1683 } else if (s->control_pid == pid) {
1684 assert(s->control_command);
1686 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1689 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
1691 /* If we are shutting things down anyway we
1692 * don't care about failing commands. */
1694 if (s->control_command->command_next && success) {
1696 /* There is another command to *
1697 * execute, so let's do that. */
1699 log_debug("%s running next command for state %s", unit_id(u), service_state_to_string(s->state));
1700 service_run_next(s, success);
1703 /* No further commands for this step, so let's
1704 * figure out what to do next */
1706 log_debug("%s got final SIGCHLD for state %s", unit_id(u), service_state_to_string(s->state));
1710 case SERVICE_START_PRE:
1712 service_enter_start(s);
1714 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1718 assert(s->type == SERVICE_FORKING);
1720 /* Let's try to load the pid
1721 * file here if we can. We
1722 * ignore the return value,
1723 * since the PID file might
1724 * actually be created by a
1725 * START_POST script */
1729 service_load_pid_file(s);
1731 service_enter_start_post(s);
1733 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1737 case SERVICE_START_POST:
1738 if (success && s->pid_file && !s->main_pid_known) {
1741 /* Hmm, let's see if we can
1742 * load the pid now after the
1743 * start-post scripts got
1746 if ((r = service_load_pid_file(s)) < 0)
1747 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
1752 case SERVICE_RELOAD:
1754 service_enter_running(s, true);
1756 service_enter_stop(s, false);
1761 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
1764 case SERVICE_STOP_SIGTERM:
1765 case SERVICE_STOP_SIGKILL:
1766 if (main_pid_good(s) <= 0)
1767 service_enter_stop_post(s, success);
1769 /* If there is still a service
1770 * process around, wait until
1771 * that one quit, too */
1774 case SERVICE_STOP_POST:
1775 case SERVICE_FINAL_SIGTERM:
1776 case SERVICE_FINAL_SIGKILL:
1777 service_enter_dead(s, success, true);
1781 assert_not_reached("Uh, control process died at wrong time.");
1785 assert_not_reached("Got SIGCHLD for unkown PID");
1788 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
1789 Service *s = SERVICE(u);
1792 assert(elapsed == 1);
1794 assert(w == &s->timer_watch);
1798 case SERVICE_START_PRE:
1800 log_warning("%s operation timed out. Terminating.", unit_id(u));
1801 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1804 case SERVICE_START_POST:
1805 case SERVICE_RELOAD:
1806 log_warning("%s operation timed out. Stopping.", unit_id(u));
1807 service_enter_stop(s, false);
1811 log_warning("%s stopping timed out. Terminating.", unit_id(u));
1812 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1815 case SERVICE_STOP_SIGTERM:
1816 log_warning("%s stopping timed out. Killing.", unit_id(u));
1817 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
1820 case SERVICE_STOP_SIGKILL:
1821 /* Uh, wie sent a SIGKILL and it is still not gone?
1822 * Must be something we cannot kill, so let's just be
1823 * weirded out and continue */
1825 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
1826 service_enter_stop_post(s, false);
1829 case SERVICE_STOP_POST:
1830 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
1831 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1834 case SERVICE_FINAL_SIGTERM:
1835 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
1836 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1839 case SERVICE_FINAL_SIGKILL:
1840 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
1841 service_enter_dead(s, false, true);
1844 case SERVICE_AUTO_RESTART:
1845 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
1846 service_enter_restart(s);
1850 assert_not_reached("Timeout at wrong time.");
1854 static void service_cgroup_notify_event(Unit *u) {
1855 Service *s = SERVICE(u);
1859 log_debug("%s: cgroup is empty", unit_id(u));
1863 /* Waiting for SIGCHLD is usually more interesting,
1864 * because it includes return codes/signals. Which is
1865 * why we ignore the cgroup events for most cases,
1866 * except when we don't know pid which to expect the
1869 case SERVICE_RUNNING:
1870 service_enter_running(s, true);
1878 static int service_enumerate(Manager *m) {
1882 char *path = NULL, *fpath = NULL, *name = NULL;
1887 STRV_FOREACH(p, m->sysvrcnd_path)
1888 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
1893 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0) {
1901 if (!(d = opendir(path))) {
1902 if (errno != ENOENT)
1903 log_warning("opendir() failed on %s: %s", path, strerror(errno));
1908 while ((de = readdir(d))) {
1909 Unit *runlevel, *service;
1911 if (ignore_file(de->d_name))
1914 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
1917 if (strlen(de->d_name) < 4)
1922 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i], de->d_name) < 0) {
1927 if (access(fpath, X_OK) < 0) {
1929 if (errno != ENOENT)
1930 log_warning("access() failed on %s: %s", fpath, strerror(errno));
1937 if (asprintf(&name, "%s.service", de->d_name+3) < 0) {
1942 if ((r = manager_load_unit(m, name, &service)) < 0)
1945 if ((r = manager_load_unit(m, rcnd_table[i+1], &runlevel)) < 0)
1948 if (de->d_name[0] == 'S') {
1949 if ((r = unit_add_dependency(runlevel, UNIT_WANTS, service)) < 0)
1952 if ((r = unit_add_dependency(runlevel, UNIT_AFTER, service)) < 0)
1955 } else if (de->d_name[0] == 'K' &&
1956 (streq(rcnd_table[i+1], SPECIAL_RUNLEVEL0_TARGET) ||
1957 streq(rcnd_table[i+1], SPECIAL_RUNLEVEL6_TARGET))) {
1959 /* We honour K links only for
1960 * halt/reboot. For the normal
1961 * runlevels we assume the
1963 * implicitly added by the
1966 if ((r = unit_add_dependency(runlevel, UNIT_CONFLICTS, service)) < 0)
1969 if ((r = unit_add_dependency(runlevel, UNIT_BEFORE, service)) < 0)
1986 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
1987 [SERVICE_DEAD] = "dead",
1988 [SERVICE_START_PRE] = "start-pre",
1989 [SERVICE_START] = "start",
1990 [SERVICE_START_POST] = "start-post",
1991 [SERVICE_RUNNING] = "running",
1992 [SERVICE_EXITED] = "exited",
1993 [SERVICE_RELOAD] = "reload",
1994 [SERVICE_STOP] = "stop",
1995 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
1996 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
1997 [SERVICE_STOP_POST] = "stop-post",
1998 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
1999 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2000 [SERVICE_MAINTAINANCE] = "maintainance",
2001 [SERVICE_AUTO_RESTART] = "auto-restart",
2004 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2006 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2007 [SERVICE_ONCE] = "once",
2008 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2009 [SERVICE_RESTART_ALWAYS] = "restart-on-failure",
2012 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2014 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2015 [SERVICE_FORKING] = "forking",
2016 [SERVICE_SIMPLE] = "simple",
2017 [SERVICE_FINISH] = "finish"
2020 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2022 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2023 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2024 [SERVICE_EXEC_START] = "ExecStart",
2025 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2026 [SERVICE_EXEC_RELOAD] = "ExecReload",
2027 [SERVICE_EXEC_STOP] = "ExecStop",
2028 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2031 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2033 const UnitVTable service_vtable = {
2034 .suffix = ".service",
2036 .init = service_init,
2037 .load = service_load,
2038 .done = service_done,
2040 .dump = service_dump,
2042 .start = service_start,
2043 .stop = service_stop,
2044 .reload = service_reload,
2046 .can_reload = service_can_reload,
2048 .active_state = service_active_state,
2050 .sigchld_event = service_sigchld_event,
2051 .timer_event = service_timer_event,
2053 .cgroup_notify_empty = service_cgroup_notify_event,
2055 .enumerate = service_enumerate