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_verify(Service *s) {
732 if (UNIT(s)->meta.load_state != UNIT_LOADED)
735 if (!s->exec_command[SERVICE_EXEC_START]) {
736 log_error("%s lacks ExecStart setting. Refusing.", unit_id(UNIT(s)));
743 static int service_load(Unit *u) {
745 Service *s = SERVICE(u);
749 /* Load a .service file */
750 if ((r = unit_load_fragment(u)) < 0)
753 /* Load a classic init script as a fallback, if we couldn't find anything */
754 if (u->meta.load_state == UNIT_STUB)
755 if ((r = service_load_sysv(s)) < 0)
758 /* Still nothing found? Then let's give up */
759 if (u->meta.load_state == UNIT_STUB)
762 /* We were able to load something, then let's add in the
763 * dropin directories. */
764 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
767 /* This is a new unit? Then let's add in some extras */
768 if (u->meta.load_state == UNIT_LOADED) {
769 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
772 if ((r = unit_add_default_cgroup(u)) < 0)
775 if ((r = sysv_chkconfig_order(s)) < 0)
779 return service_verify(s);
782 static void service_dump(Unit *u, FILE *f, const char *prefix) {
784 ServiceExecCommand c;
785 Service *s = SERVICE(u);
791 p2 = strappend(prefix, "\t");
792 prefix2 = p2 ? p2 : prefix;
795 "%sService State: %s\n"
796 "%sPermissionsStartOnly: %s\n"
797 "%sRootDirectoryStartOnly: %s\n"
798 "%sValidNoProcess: %s\n"
801 prefix, service_state_to_string(s->state),
802 prefix, yes_no(s->permissions_start_only),
803 prefix, yes_no(s->root_directory_start_only),
804 prefix, yes_no(s->valid_no_process),
805 prefix, kill_mode_to_string(s->kill_mode),
806 prefix, service_type_to_string(s->type));
808 if (s->control_pid > 0)
810 "%sControl PID: %llu\n",
811 prefix, (unsigned long long) s->control_pid);
815 "%sMain PID: %llu\n",
816 prefix, (unsigned long long) s->main_pid);
821 prefix, s->pid_file);
823 exec_context_dump(&s->exec_context, f, prefix);
825 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
827 if (!s->exec_command[c])
830 fprintf(f, "%s→ %s:\n",
831 prefix, service_exec_command_to_string(c));
833 exec_command_dump_list(s->exec_command[c], f, prefix2);
838 "%sSysV Init Script Path: %s\n"
839 "%sSysV Init Script has LSB Header: %s\n",
840 prefix, s->sysv_path,
841 prefix, yes_no(s->sysv_has_lsb));
843 if (s->sysv_start_priority >= 0)
845 "%sSysVStartPriority: %i\n",
846 prefix, s->sysv_start_priority);
848 if (s->sysv_runlevels)
849 fprintf(f, "%sSysVRunLevels: %s\n",
850 prefix, s->sysv_runlevels);
855 static int service_load_pid_file(Service *s) {
862 if (s->main_pid_known)
865 assert(s->main_pid <= 0);
870 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
873 if ((r = safe_atolu(k, &p)) < 0) {
878 if ((unsigned long) (pid_t) p != p)
881 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
882 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
883 (unsigned long long) p, s->pid_file);
887 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
888 /* FIXME: we need to do something here */
891 s->main_pid = (pid_t) p;
892 s->main_pid_known = true;
897 static int service_get_sockets(Service *s, Set **_set) {
906 /* Collects all Socket objects that belong to this
907 * service. Note that a service might have multiple sockets
908 * via multiple names. */
910 if (!(set = set_new(NULL, NULL)))
913 SET_FOREACH(t, UNIT(s)->meta.names, i) {
917 /* Look for all socket objects that go by any of our
918 * units and collect their fds */
920 if (!(k = unit_name_change_suffix(t, ".socket"))) {
925 p = manager_get_unit(UNIT(s)->meta.manager, k);
930 if ((r = set_put(set, p)) < 0)
943 static int service_notify_sockets_dead(Service *s) {
951 /* Notifies all our sockets when we die */
953 if ((r = service_get_sockets(s, &set)) < 0)
956 SET_FOREACH(sock, set, i)
957 socket_notify_service_dead(sock);
964 static void service_set_state(Service *s, ServiceState state) {
965 ServiceState old_state;
968 old_state = s->state;
971 if (state != SERVICE_START_PRE &&
972 state != SERVICE_START &&
973 state != SERVICE_START_POST &&
974 state != SERVICE_RELOAD &&
975 state != SERVICE_STOP &&
976 state != SERVICE_STOP_SIGTERM &&
977 state != SERVICE_STOP_SIGKILL &&
978 state != SERVICE_STOP_POST &&
979 state != SERVICE_FINAL_SIGTERM &&
980 state != SERVICE_FINAL_SIGKILL &&
981 state != SERVICE_AUTO_RESTART)
982 unit_unwatch_timer(UNIT(s), &s->timer_watch);
984 if (state != SERVICE_START &&
985 state != SERVICE_START_POST &&
986 state != SERVICE_RUNNING &&
987 state != SERVICE_RELOAD &&
988 state != SERVICE_STOP &&
989 state != SERVICE_STOP_SIGTERM &&
990 state != SERVICE_STOP_SIGKILL)
991 service_unwatch_main_pid(s);
993 if (state != SERVICE_START_PRE &&
994 state != SERVICE_START &&
995 state != SERVICE_START_POST &&
996 state != SERVICE_RELOAD &&
997 state != SERVICE_STOP &&
998 state != SERVICE_STOP_SIGTERM &&
999 state != SERVICE_STOP_SIGKILL &&
1000 state != SERVICE_STOP_POST &&
1001 state != SERVICE_FINAL_SIGTERM &&
1002 state != SERVICE_FINAL_SIGKILL) {
1003 service_unwatch_control_pid(s);
1004 s->control_command = NULL;
1007 if (state == SERVICE_DEAD ||
1008 state == SERVICE_STOP ||
1009 state == SERVICE_STOP_SIGTERM ||
1010 state == SERVICE_STOP_SIGKILL ||
1011 state == SERVICE_STOP_POST ||
1012 state == SERVICE_FINAL_SIGTERM ||
1013 state == SERVICE_FINAL_SIGKILL ||
1014 state == SERVICE_MAINTAINANCE ||
1015 state == SERVICE_AUTO_RESTART)
1016 service_notify_sockets_dead(s);
1018 if (old_state != state)
1019 log_debug("%s changed %s → %s", unit_id(UNIT(s)), service_state_to_string(old_state), service_state_to_string(state));
1021 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1024 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1028 unsigned rn_fds = 0;
1036 if ((r = service_get_sockets(s, &set)) < 0)
1039 SET_FOREACH(sock, set, i) {
1043 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1055 if (!(t = new(int, rn_fds+cn_fds))) {
1061 memcpy(t, rfds, rn_fds);
1062 memcpy(t+rn_fds, cfds, cn_fds);
1067 rn_fds = rn_fds+cn_fds;
1085 static int service_spawn(
1090 bool apply_permissions,
1104 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1108 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1111 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1113 if ((r = exec_spawn(c,
1118 UNIT(s)->meta.manager->confirm_spawn,
1119 UNIT(s)->meta.cgroup_bondings,
1123 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1124 /* FIXME: we need to do something here */
1136 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1141 static int main_pid_good(Service *s) {
1144 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1147 /* If we know the pid file, then lets just check if it is
1149 if (s->main_pid_known)
1150 return s->main_pid > 0;
1152 /* We don't know the pid */
1156 static int control_pid_good(Service *s) {
1159 return s->control_pid > 0;
1162 static int cgroup_good(Service *s) {
1167 if (s->valid_no_process)
1170 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1176 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1183 if (allow_restart &&
1184 (s->restart == SERVICE_RESTART_ALWAYS ||
1185 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1187 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1190 service_set_state(s, SERVICE_AUTO_RESTART);
1192 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1197 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
1198 service_enter_dead(s, false, false);
1201 static void service_enter_signal(Service *s, ServiceState state, bool success);
1203 static void service_enter_stop_post(Service *s, bool success) {
1210 service_unwatch_control_pid(s);
1212 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1213 if ((r = service_spawn(s,
1217 !s->permissions_start_only,
1218 !s->root_directory_start_only,
1219 &s->control_pid)) < 0)
1223 service_set_state(s, SERVICE_STOP_POST);
1225 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1230 log_warning("%s failed to run stop-post executable: %s", unit_id(UNIT(s)), strerror(-r));
1231 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1234 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1243 if (s->kill_mode != KILL_NONE) {
1244 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1246 if (s->kill_mode == KILL_CONTROL_GROUP) {
1248 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1249 if (r != -EAGAIN && r != -ESRCH)
1258 if (s->main_pid > 0) {
1259 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1265 if (s->control_pid > 0) {
1266 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1278 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1281 service_set_state(s, state);
1282 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1283 service_enter_stop_post(s, true);
1285 service_enter_dead(s, true, true);
1290 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
1292 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1293 service_enter_stop_post(s, false);
1295 service_enter_dead(s, false, true);
1298 static void service_enter_stop(Service *s, bool success) {
1305 service_unwatch_control_pid(s);
1307 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1308 if ((r = service_spawn(s,
1312 !s->permissions_start_only,
1313 !s->root_directory_start_only,
1314 &s->control_pid)) < 0)
1317 service_set_state(s, SERVICE_STOP);
1319 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1324 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
1325 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1328 static void service_enter_running(Service *s, bool success) {
1334 if (main_pid_good(s) != 0 && cgroup_good(s) != 0)
1335 service_set_state(s, SERVICE_RUNNING);
1336 else if (s->valid_no_process)
1337 service_set_state(s, SERVICE_EXITED);
1339 service_enter_stop(s, true);
1342 static void service_enter_start_post(Service *s) {
1346 service_unwatch_control_pid(s);
1348 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1349 if ((r = service_spawn(s,
1353 !s->permissions_start_only,
1354 !s->root_directory_start_only,
1355 &s->control_pid)) < 0)
1359 service_set_state(s, SERVICE_START_POST);
1361 service_enter_running(s, true);
1366 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
1367 service_enter_stop(s, false);
1370 static void service_enter_start(Service *s) {
1376 assert(s->exec_command[SERVICE_EXEC_START]);
1377 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1379 if (s->type == SERVICE_FORKING)
1380 service_unwatch_control_pid(s);
1382 service_unwatch_main_pid(s);
1384 if ((r = service_spawn(s,
1385 s->exec_command[SERVICE_EXEC_START],
1386 s->type == SERVICE_FORKING,
1393 if (s->type == SERVICE_SIMPLE) {
1394 /* For simple services we immediately start
1395 * the START_POST binaries. */
1398 s->main_pid_known = true;
1400 service_enter_start_post(s);
1402 } else if (s->type == SERVICE_FORKING) {
1404 /* For forking services we wait until the start
1405 * process exited. */
1407 s->control_pid = pid;
1409 s->control_command = s->exec_command[SERVICE_EXEC_START];
1410 service_set_state(s, SERVICE_START);
1412 } else if (s->type == SERVICE_FINISH) {
1414 /* For finishing services we wait until the start
1415 * process exited, too, but it is our main process. */
1418 s->main_pid_known = true;
1420 s->control_command = s->exec_command[SERVICE_EXEC_START];
1421 service_set_state(s, SERVICE_START);
1423 assert_not_reached("Unknown service type");
1428 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
1429 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1432 static void service_enter_start_pre(Service *s) {
1437 service_unwatch_control_pid(s);
1439 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1440 if ((r = service_spawn(s,
1444 !s->permissions_start_only,
1445 !s->root_directory_start_only,
1446 &s->control_pid)) < 0)
1449 service_set_state(s, SERVICE_START_PRE);
1451 service_enter_start(s);
1456 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
1457 service_enter_dead(s, false, true);
1460 static void service_enter_restart(Service *s) {
1464 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1467 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
1468 service_enter_dead(s, true, false);
1473 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
1474 service_enter_dead(s, false, false);
1477 static void service_enter_reload(Service *s) {
1482 service_unwatch_control_pid(s);
1484 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1485 if ((r = service_spawn(s,
1489 !s->permissions_start_only,
1490 !s->root_directory_start_only,
1491 &s->control_pid)) < 0)
1494 service_set_state(s, SERVICE_RELOAD);
1496 service_enter_running(s, true);
1501 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
1502 service_enter_stop(s, false);
1505 static void service_run_next(Service *s, bool success) {
1509 assert(s->control_command);
1510 assert(s->control_command->command_next);
1515 s->control_command = s->control_command->command_next;
1517 service_unwatch_control_pid(s);
1519 if ((r = service_spawn(s,
1523 !s->permissions_start_only,
1524 !s->root_directory_start_only,
1525 &s->control_pid)) < 0)
1531 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
1533 if (s->state == SERVICE_START_PRE)
1534 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1535 else if (s->state == SERVICE_STOP)
1536 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1537 else if (s->state == SERVICE_STOP_POST)
1538 service_enter_dead(s, false, true);
1540 service_enter_stop(s, false);
1543 static int service_start(Unit *u) {
1544 Service *s = SERVICE(u);
1548 /* We cannot fulfill this request right now, try again later
1550 if (s->state == SERVICE_STOP ||
1551 s->state == SERVICE_STOP_SIGTERM ||
1552 s->state == SERVICE_STOP_SIGKILL ||
1553 s->state == SERVICE_STOP_POST ||
1554 s->state == SERVICE_FINAL_SIGTERM ||
1555 s->state == SERVICE_FINAL_SIGKILL)
1558 /* Already on it! */
1559 if (s->state == SERVICE_START_PRE ||
1560 s->state == SERVICE_START ||
1561 s->state == SERVICE_START_POST)
1564 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1566 /* Make sure we don't enter a busy loop of some kind. */
1567 if (!ratelimit_test(&s->ratelimit)) {
1568 log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u));
1573 s->main_pid_known = false;
1575 service_enter_start_pre(s);
1579 static int service_stop(Unit *u) {
1580 Service *s = SERVICE(u);
1584 /* Cannot do this now */
1585 if (s->state == SERVICE_START_PRE ||
1586 s->state == SERVICE_START ||
1587 s->state == SERVICE_START_POST ||
1588 s->state == SERVICE_RELOAD)
1592 if (s->state == SERVICE_STOP ||
1593 s->state == SERVICE_STOP_SIGTERM ||
1594 s->state == SERVICE_STOP_SIGKILL ||
1595 s->state == SERVICE_STOP_POST ||
1596 s->state == SERVICE_FINAL_SIGTERM ||
1597 s->state == SERVICE_FINAL_SIGKILL)
1600 if (s->state == SERVICE_AUTO_RESTART) {
1601 service_set_state(s, SERVICE_DEAD);
1605 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1607 service_enter_stop(s, true);
1611 static int service_reload(Unit *u) {
1612 Service *s = SERVICE(u);
1616 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1618 service_enter_reload(s);
1622 static bool service_can_reload(Unit *u) {
1623 Service *s = SERVICE(u);
1627 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1630 static UnitActiveState service_active_state(Unit *u) {
1633 return state_translation_table[SERVICE(u)->state];
1636 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1637 Service *s = SERVICE(u);
1643 success = code == CLD_EXITED && status == 0;
1644 s->failure = s->failure || !success;
1646 if (s->main_pid == pid) {
1648 exec_status_fill(&s->main_exec_status, pid, code, status);
1651 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1652 assert(s->exec_command[SERVICE_EXEC_START]);
1653 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1656 log_debug("%s: main process exited, code=%s, status=%i", unit_id(u), sigchld_code_to_string(code), status);
1658 /* The service exited, so the service is officially
1663 case SERVICE_START_POST:
1664 case SERVICE_RELOAD:
1666 /* Need to wait until the operation is
1671 assert(s->type == SERVICE_FINISH);
1673 /* This was our main goal, so let's go on */
1675 service_enter_start_post(s);
1677 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1680 case SERVICE_RUNNING:
1681 service_enter_running(s, success);
1684 case SERVICE_STOP_SIGTERM:
1685 case SERVICE_STOP_SIGKILL:
1687 if (!control_pid_good(s))
1688 service_enter_stop_post(s, success);
1690 /* If there is still a control process, wait for that first */
1694 assert_not_reached("Uh, main process died at wrong time.");
1697 } else if (s->control_pid == pid) {
1698 assert(s->control_command);
1700 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1703 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
1705 /* If we are shutting things down anyway we
1706 * don't care about failing commands. */
1708 if (s->control_command->command_next && success) {
1710 /* There is another command to *
1711 * execute, so let's do that. */
1713 log_debug("%s running next command for state %s", unit_id(u), service_state_to_string(s->state));
1714 service_run_next(s, success);
1717 /* No further commands for this step, so let's
1718 * figure out what to do next */
1720 log_debug("%s got final SIGCHLD for state %s", unit_id(u), service_state_to_string(s->state));
1724 case SERVICE_START_PRE:
1726 service_enter_start(s);
1728 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1732 assert(s->type == SERVICE_FORKING);
1734 /* Let's try to load the pid
1735 * file here if we can. We
1736 * ignore the return value,
1737 * since the PID file might
1738 * actually be created by a
1739 * START_POST script */
1743 service_load_pid_file(s);
1745 service_enter_start_post(s);
1747 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1751 case SERVICE_START_POST:
1752 if (success && s->pid_file && !s->main_pid_known) {
1755 /* Hmm, let's see if we can
1756 * load the pid now after the
1757 * start-post scripts got
1760 if ((r = service_load_pid_file(s)) < 0)
1761 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
1766 case SERVICE_RELOAD:
1768 service_enter_running(s, true);
1770 service_enter_stop(s, false);
1775 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
1778 case SERVICE_STOP_SIGTERM:
1779 case SERVICE_STOP_SIGKILL:
1780 if (main_pid_good(s) <= 0)
1781 service_enter_stop_post(s, success);
1783 /* If there is still a service
1784 * process around, wait until
1785 * that one quit, too */
1788 case SERVICE_STOP_POST:
1789 case SERVICE_FINAL_SIGTERM:
1790 case SERVICE_FINAL_SIGKILL:
1791 service_enter_dead(s, success, true);
1795 assert_not_reached("Uh, control process died at wrong time.");
1799 assert_not_reached("Got SIGCHLD for unkown PID");
1802 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
1803 Service *s = SERVICE(u);
1806 assert(elapsed == 1);
1808 assert(w == &s->timer_watch);
1812 case SERVICE_START_PRE:
1814 log_warning("%s operation timed out. Terminating.", unit_id(u));
1815 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1818 case SERVICE_START_POST:
1819 case SERVICE_RELOAD:
1820 log_warning("%s operation timed out. Stopping.", unit_id(u));
1821 service_enter_stop(s, false);
1825 log_warning("%s stopping timed out. Terminating.", unit_id(u));
1826 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1829 case SERVICE_STOP_SIGTERM:
1830 log_warning("%s stopping timed out. Killing.", unit_id(u));
1831 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
1834 case SERVICE_STOP_SIGKILL:
1835 /* Uh, wie sent a SIGKILL and it is still not gone?
1836 * Must be something we cannot kill, so let's just be
1837 * weirded out and continue */
1839 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
1840 service_enter_stop_post(s, false);
1843 case SERVICE_STOP_POST:
1844 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
1845 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1848 case SERVICE_FINAL_SIGTERM:
1849 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
1850 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1853 case SERVICE_FINAL_SIGKILL:
1854 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
1855 service_enter_dead(s, false, true);
1858 case SERVICE_AUTO_RESTART:
1859 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
1860 service_enter_restart(s);
1864 assert_not_reached("Timeout at wrong time.");
1868 static void service_cgroup_notify_event(Unit *u) {
1869 Service *s = SERVICE(u);
1873 log_debug("%s: cgroup is empty", unit_id(u));
1877 /* Waiting for SIGCHLD is usually more interesting,
1878 * because it includes return codes/signals. Which is
1879 * why we ignore the cgroup events for most cases,
1880 * except when we don't know pid which to expect the
1883 case SERVICE_RUNNING:
1884 service_enter_running(s, true);
1892 static int service_enumerate(Manager *m) {
1896 char *path = NULL, *fpath = NULL, *name = NULL;
1901 STRV_FOREACH(p, m->sysvrcnd_path)
1902 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
1907 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0) {
1915 if (!(d = opendir(path))) {
1916 if (errno != ENOENT)
1917 log_warning("opendir() failed on %s: %s", path, strerror(errno));
1922 while ((de = readdir(d))) {
1923 Unit *runlevel, *service;
1925 if (ignore_file(de->d_name))
1928 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
1931 if (strlen(de->d_name) < 4)
1936 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i], de->d_name) < 0) {
1941 if (access(fpath, X_OK) < 0) {
1943 if (errno != ENOENT)
1944 log_warning("access() failed on %s: %s", fpath, strerror(errno));
1951 if (asprintf(&name, "%s.service", de->d_name+3) < 0) {
1956 if ((r = manager_load_unit(m, name, &service)) < 0)
1959 if ((r = manager_load_unit(m, rcnd_table[i+1], &runlevel)) < 0)
1962 if (de->d_name[0] == 'S') {
1963 if ((r = unit_add_dependency(runlevel, UNIT_WANTS, service)) < 0)
1966 if ((r = unit_add_dependency(runlevel, UNIT_AFTER, service)) < 0)
1969 } else if (de->d_name[0] == 'K' &&
1970 (streq(rcnd_table[i+1], SPECIAL_RUNLEVEL0_TARGET) ||
1971 streq(rcnd_table[i+1], SPECIAL_RUNLEVEL6_TARGET))) {
1973 /* We honour K links only for
1974 * halt/reboot. For the normal
1975 * runlevels we assume the
1977 * implicitly added by the
1980 if ((r = unit_add_dependency(runlevel, UNIT_CONFLICTS, service)) < 0)
1983 if ((r = unit_add_dependency(runlevel, UNIT_BEFORE, service)) < 0)
2000 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2001 [SERVICE_DEAD] = "dead",
2002 [SERVICE_START_PRE] = "start-pre",
2003 [SERVICE_START] = "start",
2004 [SERVICE_START_POST] = "start-post",
2005 [SERVICE_RUNNING] = "running",
2006 [SERVICE_EXITED] = "exited",
2007 [SERVICE_RELOAD] = "reload",
2008 [SERVICE_STOP] = "stop",
2009 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2010 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2011 [SERVICE_STOP_POST] = "stop-post",
2012 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2013 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2014 [SERVICE_MAINTAINANCE] = "maintainance",
2015 [SERVICE_AUTO_RESTART] = "auto-restart",
2018 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2020 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2021 [SERVICE_ONCE] = "once",
2022 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2023 [SERVICE_RESTART_ALWAYS] = "restart-always",
2026 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2028 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2029 [SERVICE_FORKING] = "forking",
2030 [SERVICE_SIMPLE] = "simple",
2031 [SERVICE_FINISH] = "finish"
2034 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2036 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2037 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2038 [SERVICE_EXEC_START] = "ExecStart",
2039 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2040 [SERVICE_EXEC_RELOAD] = "ExecReload",
2041 [SERVICE_EXEC_STOP] = "ExecStop",
2042 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2045 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2047 const UnitVTable service_vtable = {
2048 .suffix = ".service",
2050 .init = service_init,
2051 .load = service_load,
2052 .done = service_done,
2054 .dump = service_dump,
2056 .start = service_start,
2057 .stop = service_stop,
2058 .reload = service_reload,
2060 .can_reload = service_can_reload,
2062 .active_state = service_active_state,
2064 .sigchld_event = service_sigchld_event,
2065 .timer_event = service_timer_event,
2067 .cgroup_notify_empty = service_cgroup_notify_event,
2069 .enumerate = service_enumerate