1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "load-fragment.h"
30 #include "load-dropin.h"
34 #define COMMENTS "#;\n"
35 #define NEWLINES "\n\r"
38 static const char * const rcnd_table[] = {
39 "/rc0.d", SPECIAL_RUNLEVEL0_TARGET,
40 "/rc1.d", SPECIAL_RUNLEVEL1_TARGET,
41 "/rc2.d", SPECIAL_RUNLEVEL2_TARGET,
42 "/rc3.d", SPECIAL_RUNLEVEL3_TARGET,
43 "/rc4.d", SPECIAL_RUNLEVEL4_TARGET,
44 "/rc5.d", SPECIAL_RUNLEVEL5_TARGET,
45 "/rc6.d", SPECIAL_RUNLEVEL6_TARGET,
46 "/boot.d", SPECIAL_BASIC_TARGET
49 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
50 [SERVICE_DEAD] = UNIT_INACTIVE,
51 [SERVICE_START_PRE] = UNIT_ACTIVATING,
52 [SERVICE_START] = UNIT_ACTIVATING,
53 [SERVICE_START_POST] = UNIT_ACTIVATING,
54 [SERVICE_RUNNING] = UNIT_ACTIVE,
55 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
56 [SERVICE_STOP] = UNIT_DEACTIVATING,
57 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
58 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
59 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
60 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
61 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
62 [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
63 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
66 static void service_done(Unit *u) {
67 Service *s = SERVICE(u);
77 free(s->sysv_runlevels);
78 s->sysv_runlevels = NULL;
80 exec_context_done(&s->exec_context);
81 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
82 s->control_command = NULL;
84 /* This will leak a process, but at least no memory or any of
86 if (s->main_pid > 0) {
87 unit_unwatch_pid(u, s->main_pid);
91 if (s->control_pid > 0) {
92 unit_unwatch_pid(u, s->control_pid);
96 unit_unwatch_timer(u, &s->timer_watch);
99 static int sysv_translate_name(const char *name, char **_r) {
101 static const char * const table[] = {
102 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
103 "$network", SPECIAL_NETWORK_TARGET,
104 "$named", SPECIAL_NSS_LOOKUP_TARGET,
105 "$portmap", SPECIAL_RPCBIND_TARGET,
106 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
107 "$syslog", SPECIAL_SYSLOG_TARGET,
108 "$time", SPECIAL_RTC_SET_TARGET
114 for (i = 0; i < ELEMENTSOF(table); i += 2)
115 if (streq(table[i], name)) {
116 if (!(r = strdup(table[i+1])))
125 if (asprintf(&r, "%s.service", name) < 0)
136 static int sysv_chkconfig_order(Service *s) {
142 if (s->sysv_start_priority < 0)
145 /* For each pair of services where at least one lacks a LSB
146 * header, we use the start priority value to order things. */
148 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
152 t = (Service*) other;
157 if (t->sysv_start_priority < 0)
160 /* If both units have modern headers we don't care
161 * about the priorities */
162 if ((!s->sysv_path || s->sysv_has_lsb) &&
163 (!t->sysv_path || t->sysv_has_lsb))
166 if (t->sysv_start_priority < s->sysv_start_priority)
168 else if (t->sysv_start_priority > s->sysv_start_priority)
173 /* FIXME: Maybe we should compare the name here lexicographically? */
175 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t))) < 0)
182 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
185 if (!(c = new0(ExecCommand, 1)))
188 if (!(c->path = strdup(path))) {
193 if (!(c->argv = strv_new(path, arg1, NULL))) {
202 static int sysv_exec_commands(Service *s) {
206 assert(s->sysv_path);
208 if (!(c = exec_command_new(s->sysv_path, "start")))
210 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
212 if (!(c = exec_command_new(s->sysv_path, "stop")))
214 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
216 if (!(c = exec_command_new(s->sysv_path, "reload")))
218 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
223 static int priority_from_rcd(Service *s, const char *init_script) {
227 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvrcnd_path)
228 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
233 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0)
241 log_warning("opendir() failed on %s: %s", path, strerror(errno));
246 while ((de = readdir(d))) {
249 if (ignore_file(de->d_name))
252 if (de->d_name[0] != 'S')
255 if (strlen(de->d_name) < 4)
258 if (!streq(de->d_name + 3, init_script))
261 /* Yay, we found it! Now decode the priority */
263 a = undecchar(de->d_name[1]);
264 b = undecchar(de->d_name[2]);
269 s->sysv_start_priority = a*10 + b;
271 log_debug("Determined priority %i from link farm for %s", s->sysv_start_priority, unit_id(UNIT(s)));
283 static int service_load_sysv_path(Service *s, const char *path) {
300 if (!(f = fopen(path, "re"))) {
301 r = errno == ENOENT ? 0 : -errno;
305 s->type = SERVICE_FORKING;
306 s->restart = SERVICE_ONCE;
309 if (!(s->sysv_path = strdup(path))) {
315 char l[LINE_MAX], *t;
317 if (!fgets(l, sizeof(l), f)) {
322 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
332 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
334 s->sysv_has_lsb = true;
338 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
344 t += strspn(t, WHITESPACE);
346 if (state == NORMAL) {
348 /* Try to parse Red Hat style chkconfig headers */
350 if (startswith(t, "chkconfig:")) {
352 char runlevels[16], *k;
356 if (sscanf(t+10, "%15s %i %*i",
358 &start_priority) != 2) {
360 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
364 if (start_priority < 0 || start_priority > 99)
365 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
367 s->sysv_start_priority = start_priority;
369 char_array_0(runlevels);
370 k = delete_chars(runlevels, WHITESPACE "-");
375 if (!(d = strdup(k))) {
380 free(s->sysv_runlevels);
381 s->sysv_runlevels = d;
385 } else if (startswith(t, "description:")) {
387 size_t k = strlen(t);
390 if (t[k-1] == '\\') {
395 if (!(d = strdup(strstrip(t+12)))) {
400 free(u->meta.description);
401 u->meta.description = d;
403 } else if (startswith(t, "pidfile:")) {
410 if (!path_is_absolute(fn)) {
411 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
415 if (!(fn = strdup(fn))) {
424 } else if (state == DESCRIPTION) {
426 /* Try to parse Red Hat style description
429 size_t k = strlen(t);
437 assert(u->meta.description);
438 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
443 free(u->meta.description);
444 u->meta.description = d;
446 } else if (state == LSB || state == LSB_DESCRIPTION) {
448 if (startswith(t, "Provides:")) {
454 FOREACH_WORD(w, z, t+9, i) {
457 if (!(n = strndup(w, z))) {
462 r = sysv_translate_name(n, &m);
471 if (unit_name_to_type(m) == UNIT_SERVICE)
472 r = unit_add_name(u, m);
474 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m)) >= 0)
475 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m);
484 } else if (startswith(t, "Required-Start:") ||
485 startswith(t, "Should-Start:")) {
491 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
494 if (!(n = strndup(w, z))) {
499 r = sysv_translate_name(n, &m);
508 r = unit_add_dependency_by_name(u, UNIT_AFTER, m);
514 } else if (startswith(t, "Default-Start:")) {
519 k = delete_chars(t+14, WHITESPACE "-");
522 if (!(d = strdup(k))) {
527 free(s->sysv_runlevels);
528 s->sysv_runlevels = d;
531 } else if (startswith(t, "Description:")) {
534 state = LSB_DESCRIPTION;
536 if (!(d = strdup(strstrip(t+12)))) {
541 free(u->meta.description);
542 u->meta.description = d;
544 } else if (startswith(t, "Short-Description:") &&
545 !u->meta.description) {
548 /* We use the short description only
549 * if no long description is set. */
553 if (!(d = strdup(strstrip(t+18)))) {
558 u->meta.description = d;
560 } else if (state == LSB_DESCRIPTION) {
562 if (startswith(l, "#\t") || startswith(l, "# ")) {
565 assert(u->meta.description);
566 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
571 free(u->meta.description);
572 u->meta.description = d;
579 /* If init scripts have no LSB header, then we enforce the
580 * ordering via the chkconfig priorities. We try to determine
581 * a priority for *all* init scripts here, since they are
582 * needed as soon as at least one non-LSB script is used. */
584 if (s->sysv_start_priority < 0) {
585 log_debug("%s has no chkconfig header, trying to determine SysV priority from link farm.", unit_id(u));
587 if ((r = priority_from_rcd(s, file_name_from_path(path))) < 0)
590 if (s->sysv_start_priority < 0)
591 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", unit_id(u));
594 if ((r = sysv_exec_commands(s)) < 0)
597 if (!s->sysv_runlevels || chars_intersect("12345", s->sysv_runlevels)) {
598 /* If there a runlevels configured for this service
599 * but none of the standard ones, then we assume this
600 * is some special kind of service (which might be
601 * needed for early boot) and don't create any links
604 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET)) < 0 ||
605 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET)) < 0)
609 u->meta.load_state = UNIT_LOADED;
620 static int service_load_sysv_name(Service *s, const char *name) {
626 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
630 if (asprintf(&path, "%s/%s", *p, name) < 0)
633 assert(endswith(path, ".service"));
634 path[strlen(path)-8] = 0;
636 r = service_load_sysv_path(s, path);
642 if ((UNIT(s)->meta.load_state != UNIT_STUB))
649 static int service_load_sysv(Service *s) {
656 /* Load service data from SysV init scripts, preferably with
659 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
662 if ((t = unit_id(UNIT(s))))
663 if ((r = service_load_sysv_name(s, t)) < 0)
666 if (UNIT(s)->meta.load_state == UNIT_STUB)
667 SET_FOREACH(t, UNIT(s)->meta.names, i) {
668 if (t == unit_id(UNIT(s)))
671 if ((r == service_load_sysv_name(s, t)) < 0)
674 if (UNIT(s)->meta.load_state != UNIT_STUB)
681 static void service_init(Unit *u) {
682 Service *s = SERVICE(u);
685 assert(u->meta.load_state == UNIT_STUB);
690 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
691 s->restart_usec = DEFAULT_RESTART_USEC;
693 exec_context_init(&s->exec_context);
695 s->timer_watch.type = WATCH_INVALID;
697 s->state = SERVICE_DEAD;
699 s->sysv_start_priority = -1;
700 s->permissions_start_only = false;
701 s->root_directory_start_only = false;
702 s->valid_no_process = false;
704 s->sysv_has_lsb = false;
705 s->main_pid = s->control_pid = 0;
706 s->main_pid_known = false;
709 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
712 static int service_load(Unit *u) {
714 Service *s = SERVICE(u);
718 /* Load a .service file */
719 if ((r = unit_load_fragment(u)) < 0)
722 /* Load a classic init script as a fallback, if we couldn't find anything */
723 if (u->meta.load_state == UNIT_STUB)
724 if ((r = service_load_sysv(s)) < 0)
727 /* Still nothing found? Then let's give up */
728 if (u->meta.load_state == UNIT_STUB)
731 /* We were able to load something, then let's add in the
732 * dropin directories. */
733 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
736 /* This is a new unit? Then let's add in some extras */
737 if (u->meta.load_state == UNIT_LOADED) {
738 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
741 if ((r = unit_add_default_cgroup(u)) < 0)
744 if ((r = sysv_chkconfig_order(s)) < 0)
751 static void service_dump(Unit *u, FILE *f, const char *prefix) {
753 ServiceExecCommand c;
754 Service *s = SERVICE(u);
760 p2 = strappend(prefix, "\t");
761 prefix2 = p2 ? p2 : prefix;
764 "%sService State: %s\n"
765 "%sPermissionsStartOnly: %s\n"
766 "%sRootDirectoryStartOnly: %s\n"
767 "%sValidNoProcess: %s\n"
770 prefix, service_state_to_string(s->state),
771 prefix, yes_no(s->permissions_start_only),
772 prefix, yes_no(s->root_directory_start_only),
773 prefix, yes_no(s->valid_no_process),
774 prefix, kill_mode_to_string(s->kill_mode),
775 prefix, service_type_to_string(s->type));
777 if (s->control_pid > 0)
779 "%sControl PID: %llu\n",
780 prefix, (unsigned long long) s->control_pid);
784 "%sMain PID: %llu\n",
785 prefix, (unsigned long long) s->main_pid);
790 prefix, s->pid_file);
792 exec_context_dump(&s->exec_context, f, prefix);
794 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
796 if (!s->exec_command[c])
799 fprintf(f, "%s→ %s:\n",
800 prefix, service_exec_command_to_string(c));
802 exec_command_dump_list(s->exec_command[c], f, prefix2);
807 "%sSysV Init Script Path: %s\n"
808 "%sSysV Init Script has LSB Header: %s\n",
809 prefix, s->sysv_path,
810 prefix, yes_no(s->sysv_has_lsb));
812 if (s->sysv_start_priority >= 0)
814 "%sSysVStartPriority: %i\n",
815 prefix, s->sysv_start_priority);
817 if (s->sysv_runlevels)
818 fprintf(f, "%sSysVRunLevels: %s\n",
819 prefix, s->sysv_runlevels);
824 static int service_load_pid_file(Service *s) {
831 if (s->main_pid_known)
837 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
840 if ((r = safe_atolu(k, &p)) < 0) {
845 if ((unsigned long) (pid_t) p != p)
848 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
849 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
850 (unsigned long long) p, s->pid_file);
854 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
855 /* FIXME: we need to do something here */
858 s->main_pid = (pid_t) p;
859 s->main_pid_known = true;
864 static int service_get_sockets(Service *s, Set **_set) {
873 /* Collects all Socket objects that belong to this
874 * service. Note that a service might have multiple sockets
875 * via multiple names. */
877 if (!(set = set_new(NULL, NULL)))
880 SET_FOREACH(t, UNIT(s)->meta.names, i) {
884 /* Look for all socket objects that go by any of our
885 * units and collect their fds */
887 if (!(k = unit_name_change_suffix(t, ".socket"))) {
892 p = manager_get_unit(UNIT(s)->meta.manager, k);
897 if ((r = set_put(set, p)) < 0)
910 static int service_notify_sockets_dead(Service *s) {
918 /* Notifies all our sockets when we die */
920 if ((r = service_get_sockets(s, &set)) < 0)
923 SET_FOREACH(sock, set, i)
924 socket_notify_service_dead(sock);
931 static void service_set_state(Service *s, ServiceState state) {
932 ServiceState old_state;
935 old_state = s->state;
938 if (state != SERVICE_START_PRE &&
939 state != SERVICE_START &&
940 state != SERVICE_START_POST &&
941 state != SERVICE_RELOAD &&
942 state != SERVICE_STOP &&
943 state != SERVICE_STOP_SIGTERM &&
944 state != SERVICE_STOP_SIGKILL &&
945 state != SERVICE_STOP_POST &&
946 state != SERVICE_FINAL_SIGTERM &&
947 state != SERVICE_FINAL_SIGKILL &&
948 state != SERVICE_AUTO_RESTART)
949 unit_unwatch_timer(UNIT(s), &s->timer_watch);
951 if (state != SERVICE_START &&
952 state != SERVICE_START_POST &&
953 state != SERVICE_RUNNING &&
954 state != SERVICE_RELOAD &&
955 state != SERVICE_STOP &&
956 state != SERVICE_STOP_SIGTERM &&
957 state != SERVICE_STOP_SIGKILL)
958 if (s->main_pid > 0) {
959 unit_unwatch_pid(UNIT(s), s->main_pid);
963 if (state != SERVICE_START_PRE &&
964 state != SERVICE_START &&
965 state != SERVICE_START_POST &&
966 state != SERVICE_RELOAD &&
967 state != SERVICE_STOP &&
968 state != SERVICE_STOP_SIGTERM &&
969 state != SERVICE_STOP_SIGKILL &&
970 state != SERVICE_STOP_POST &&
971 state != SERVICE_FINAL_SIGTERM &&
972 state != SERVICE_FINAL_SIGKILL) {
973 if (s->control_pid > 0) {
974 unit_unwatch_pid(UNIT(s), s->control_pid);
978 s->control_command = NULL;
981 if (state == SERVICE_DEAD ||
982 state == SERVICE_STOP ||
983 state == SERVICE_STOP_SIGTERM ||
984 state == SERVICE_STOP_SIGKILL ||
985 state == SERVICE_STOP_POST ||
986 state == SERVICE_FINAL_SIGTERM ||
987 state == SERVICE_FINAL_SIGKILL ||
988 state == SERVICE_MAINTAINANCE ||
989 state == SERVICE_AUTO_RESTART)
990 service_notify_sockets_dead(s);
992 if (old_state != state)
993 log_debug("%s changed %s → %s", unit_id(UNIT(s)), service_state_to_string(old_state), service_state_to_string(state));
995 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
998 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1002 unsigned rn_fds = 0;
1010 if ((r = service_get_sockets(s, &set)) < 0)
1013 SET_FOREACH(sock, set, i) {
1017 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1029 if (!(t = new(int, rn_fds+cn_fds))) {
1035 memcpy(t, rfds, rn_fds);
1036 memcpy(t+rn_fds, cfds, cn_fds);
1041 rn_fds = rn_fds+cn_fds;
1059 static int service_spawn(
1064 bool apply_permissions,
1078 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1082 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1085 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1087 if ((r = exec_spawn(c,
1092 UNIT(s)->meta.cgroup_bondings,
1096 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1097 /* FIXME: we need to do something here */
1109 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1114 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1121 if (allow_restart &&
1122 (s->restart == SERVICE_RESTART_ALWAYS ||
1123 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1125 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1128 service_set_state(s, SERVICE_AUTO_RESTART);
1130 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1135 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
1136 service_enter_dead(s, false, false);
1139 static void service_enter_signal(Service *s, ServiceState state, bool success);
1141 static void service_enter_stop_post(Service *s, bool success) {
1148 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]))
1149 if ((r = service_spawn(s,
1153 !s->permissions_start_only,
1154 !s->root_directory_start_only,
1155 &s->control_pid)) < 0)
1159 service_set_state(s, SERVICE_STOP_POST);
1161 if (!s->control_command)
1162 service_enter_dead(s, true, true);
1167 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
1168 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1171 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1180 if (s->main_pid > 0 || s->control_pid > 0) {
1183 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1185 if (s->kill_mode == KILL_CONTROL_GROUP) {
1187 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1188 if (r != -EAGAIN && r != -ESRCH)
1196 if (s->main_pid > 0) {
1197 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1203 if (s->control_pid > 0) {
1204 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1215 service_set_state(s, state);
1217 if (s->main_pid <= 0 && s->control_pid <= 0)
1218 service_enter_dead(s, true, true);
1223 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
1227 service_set_state(s, state);
1228 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1229 service_enter_stop_post(s, false);
1231 service_enter_dead(s, false, true);
1234 static void service_enter_stop(Service *s, bool success) {
1241 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP]))
1242 if ((r = service_spawn(s,
1246 !s->permissions_start_only,
1247 !s->root_directory_start_only,
1248 &s->control_pid)) < 0)
1251 service_set_state(s, SERVICE_STOP);
1253 if (!s->control_command)
1254 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1259 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
1260 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1263 static void service_enter_start_post(Service *s) {
1267 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST]))
1268 if ((r = service_spawn(s,
1272 !s->permissions_start_only,
1273 !s->root_directory_start_only,
1274 &s->control_pid)) < 0)
1278 service_set_state(s, SERVICE_START_POST);
1280 if (!s->control_command)
1281 service_set_state(s, SERVICE_RUNNING);
1286 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
1287 service_enter_stop(s, false);
1290 static void service_enter_start(Service *s) {
1296 assert(s->exec_command[SERVICE_EXEC_START]);
1297 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1299 if ((r = service_spawn(s,
1300 s->exec_command[SERVICE_EXEC_START],
1301 s->type == SERVICE_FORKING,
1308 service_set_state(s, SERVICE_START);
1310 if (s->type == SERVICE_SIMPLE) {
1311 /* For simple services we immediately start
1312 * the START_POST binaries. */
1315 s->main_pid_known = true;
1316 service_enter_start_post(s);
1318 } else if (s->type == SERVICE_FORKING) {
1320 /* For forking services we wait until the start
1321 * process exited. */
1323 s->control_pid = pid;
1324 s->control_command = s->exec_command[SERVICE_EXEC_START];
1325 } else if (s->type == SERVICE_FINISH) {
1327 /* For finishing services we wait until the start
1328 * process exited, too, but it is our main process. */
1331 s->control_command = s->exec_command[SERVICE_EXEC_START];
1333 assert_not_reached("Unknown service type");
1338 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
1339 service_enter_stop(s, false);
1342 static void service_enter_start_pre(Service *s) {
1347 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]))
1348 if ((r = service_spawn(s,
1352 !s->permissions_start_only,
1353 !s->root_directory_start_only,
1354 &s->control_pid)) < 0)
1357 service_set_state(s, SERVICE_START_PRE);
1359 if (!s->control_command)
1360 service_enter_start(s);
1365 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
1366 service_enter_dead(s, false, true);
1369 static void service_enter_restart(Service *s) {
1373 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1376 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
1377 service_enter_dead(s, true, false);
1382 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
1383 service_enter_dead(s, false, false);
1386 static void service_enter_reload(Service *s) {
1391 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]))
1392 if ((r = service_spawn(s,
1396 !s->permissions_start_only,
1397 !s->root_directory_start_only,
1398 &s->control_pid)) < 0)
1401 service_set_state(s, SERVICE_RELOAD);
1403 if (!s->control_command)
1404 service_set_state(s, SERVICE_RUNNING);
1409 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
1410 service_enter_stop(s, false);
1413 static void service_run_next(Service *s, bool success) {
1417 assert(s->control_command);
1418 assert(s->control_command->command_next);
1423 s->control_command = s->control_command->command_next;
1425 if ((r = service_spawn(s,
1429 !s->permissions_start_only,
1430 !s->root_directory_start_only,
1431 &s->control_pid)) < 0)
1437 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
1439 if (s->state == SERVICE_STOP)
1440 service_enter_stop_post(s, false);
1441 else if (s->state == SERVICE_STOP_POST)
1442 service_enter_dead(s, false, true);
1444 service_enter_stop(s, false);
1447 static int service_start(Unit *u) {
1448 Service *s = SERVICE(u);
1452 /* We cannot fulfill this request right now, try again later
1454 if (s->state == SERVICE_STOP ||
1455 s->state == SERVICE_STOP_SIGTERM ||
1456 s->state == SERVICE_STOP_SIGKILL ||
1457 s->state == SERVICE_STOP_POST ||
1458 s->state == SERVICE_FINAL_SIGTERM ||
1459 s->state == SERVICE_FINAL_SIGKILL)
1462 /* Already on it! */
1463 if (s->state == SERVICE_START_PRE ||
1464 s->state == SERVICE_START ||
1465 s->state == SERVICE_START_POST)
1468 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1470 /* Make sure we don't enter a busy loop of some kind. */
1471 if (!ratelimit_test(&s->ratelimit)) {
1472 log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u));
1477 s->main_pid_known = false;
1479 service_enter_start_pre(s);
1483 static int service_stop(Unit *u) {
1484 Service *s = SERVICE(u);
1488 /* Cannot do this now */
1489 if (s->state == SERVICE_START_PRE ||
1490 s->state == SERVICE_START ||
1491 s->state == SERVICE_START_POST ||
1492 s->state == SERVICE_RELOAD)
1496 if (s->state == SERVICE_STOP ||
1497 s->state == SERVICE_STOP_SIGTERM ||
1498 s->state == SERVICE_STOP_SIGKILL ||
1499 s->state == SERVICE_STOP_POST ||
1500 s->state == SERVICE_FINAL_SIGTERM ||
1501 s->state == SERVICE_FINAL_SIGKILL)
1504 if (s->state == SERVICE_AUTO_RESTART) {
1505 service_set_state(s, SERVICE_DEAD);
1509 assert(s->state == SERVICE_RUNNING);
1511 service_enter_stop(s, true);
1515 static int service_reload(Unit *u) {
1516 Service *s = SERVICE(u);
1520 assert(s->state == SERVICE_RUNNING);
1522 service_enter_reload(s);
1526 static bool service_can_reload(Unit *u) {
1527 Service *s = SERVICE(u);
1531 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1534 static UnitActiveState service_active_state(Unit *u) {
1537 return state_translation_table[SERVICE(u)->state];
1540 static int main_pid_good(Service *s) {
1543 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1546 /* If we know the pid file, then lets just check if it is
1548 if (s->main_pid_known)
1549 return s->main_pid > 0;
1551 /* We don't know the pid */
1555 static bool control_pid_good(Service *s) {
1558 return s->control_pid > 0;
1561 static int cgroup_good(Service *s) {
1564 if (s->valid_no_process)
1567 return cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings);
1570 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1571 Service *s = SERVICE(u);
1577 success = code == CLD_EXITED && status == 0;
1578 s->failure = s->failure || !success;
1580 if (s->main_pid == pid) {
1582 exec_status_fill(&s->main_exec_status, pid, code, status);
1585 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1586 assert(s->exec_command[SERVICE_EXEC_START]);
1587 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1590 log_debug("%s: main process exited, code=%s, status=%i", unit_id(u), sigchld_code_to_string(code), status);
1592 /* The service exited, so the service is officially
1597 case SERVICE_START_POST:
1598 case SERVICE_RELOAD:
1600 /* Need to wait until the operation is
1605 assert(s->type == SERVICE_FINISH);
1607 /* This was our main goal, so let's go on */
1609 service_enter_start_post(s);
1611 service_enter_stop(s, false);
1614 case SERVICE_RUNNING:
1615 service_enter_stop(s, success);
1618 case SERVICE_STOP_SIGTERM:
1619 case SERVICE_STOP_SIGKILL:
1621 if (!control_pid_good(s))
1622 service_enter_stop_post(s, success);
1624 /* If there is still a control process, wait for that first */
1628 assert_not_reached("Uh, main process died at wrong time.");
1631 } else if (s->control_pid == pid) {
1632 assert(s->control_command);
1634 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1637 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
1639 /* If we are shutting things down anyway we
1640 * don't care about failing commands. */
1642 if (s->control_command->command_next &&
1643 (success || (s->state == SERVICE_STOP || s->state == SERVICE_STOP_POST)))
1645 /* There is another command to *
1646 * execute, so let's do that. */
1648 service_run_next(s, success);
1651 /* No further commands for this step, so let's
1652 * figure out what to do next */
1654 log_debug("%s got final SIGCHLD for state %s", unit_id(u), service_state_to_string(s->state));
1658 case SERVICE_START_PRE:
1660 service_enter_start(s);
1662 service_enter_stop(s, false);
1666 assert(s->type == SERVICE_FORKING);
1668 /* Let's try to load the pid
1669 * file here if we can. We
1670 * ignore the return value,
1671 * since the PID file might
1672 * actually be created by a
1673 * START_POST script */
1677 service_load_pid_file(s);
1679 service_enter_start_post(s);
1681 service_enter_stop(s, false);
1685 case SERVICE_START_POST:
1686 if (success && s->pid_file && !s->main_pid_known) {
1689 /* Hmm, let's see if we can
1690 * load the pid now after the
1691 * start-post scripts got
1694 if ((r = service_load_pid_file(s)) < 0)
1695 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
1700 case SERVICE_RELOAD:
1702 if (main_pid_good(s) != 0 && cgroup_good(s) != 0)
1703 service_set_state(s, SERVICE_RUNNING);
1705 service_enter_stop(s, true);
1707 service_enter_stop(s, false);
1712 if (main_pid_good(s) > 0)
1713 /* Still not dead and we know the PID? Let's go hunting. */
1714 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
1716 service_enter_stop_post(s, success);
1719 case SERVICE_STOP_SIGTERM:
1720 case SERVICE_STOP_SIGKILL:
1721 if (main_pid_good(s) <= 0)
1722 service_enter_stop_post(s, success);
1724 /* If there is still a service
1725 * process around, wait until
1726 * that one quit, too */
1729 case SERVICE_STOP_POST:
1730 case SERVICE_FINAL_SIGTERM:
1731 case SERVICE_FINAL_SIGKILL:
1732 service_enter_dead(s, success, true);
1736 assert_not_reached("Uh, control process died at wrong time.");
1740 assert_not_reached("Got SIGCHLD for unkown PID");
1743 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
1744 Service *s = SERVICE(u);
1747 assert(elapsed == 1);
1749 assert(w == &s->timer_watch);
1753 case SERVICE_START_PRE:
1755 case SERVICE_START_POST:
1756 case SERVICE_RELOAD:
1757 log_warning("%s operation timed out. Stopping.", unit_id(u));
1758 service_enter_stop(s, false);
1762 log_warning("%s stopping timed out. Terminating.", unit_id(u));
1763 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1766 case SERVICE_STOP_SIGTERM:
1767 log_warning("%s stopping timed out. Killing.", unit_id(u));
1768 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
1771 case SERVICE_STOP_SIGKILL:
1772 /* Uh, wie sent a SIGKILL and it is still not gone?
1773 * Must be something we cannot kill, so let's just be
1774 * weirded out and continue */
1776 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
1777 service_enter_stop_post(s, false);
1780 case SERVICE_STOP_POST:
1781 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
1782 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1785 case SERVICE_FINAL_SIGTERM:
1786 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
1787 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1790 case SERVICE_FINAL_SIGKILL:
1791 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
1792 service_enter_dead(s, false, true);
1795 case SERVICE_AUTO_RESTART:
1796 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
1797 service_enter_restart(s);
1801 assert_not_reached("Timeout at wrong time.");
1805 static void service_cgroup_notify_event(Unit *u) {
1806 Service *s = SERVICE(u);
1810 log_debug("%s: cgroup is empty", unit_id(u));
1814 /* Waiting for SIGCHLD is usually more interesting,
1815 * because it includes return codes/signals. Which is
1816 * why we ignore the cgroup events for most cases,
1817 * except when we don't know pid which to expect the
1820 case SERVICE_RUNNING:
1822 if (!s->valid_no_process && main_pid_good(s) <= 0)
1823 service_enter_stop(s, true);
1832 static int service_enumerate(Manager *m) {
1836 char *path = NULL, *fpath = NULL, *name = NULL;
1841 STRV_FOREACH(p, m->sysvrcnd_path)
1842 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
1847 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0) {
1855 if (!(d = opendir(path))) {
1856 if (errno != ENOENT)
1857 log_warning("opendir() failed on %s: %s", path, strerror(errno));
1862 while ((de = readdir(d))) {
1863 Unit *runlevel, *service;
1865 if (ignore_file(de->d_name))
1868 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
1871 if (strlen(de->d_name) < 4)
1876 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i], de->d_name) < 0) {
1881 if (access(fpath, X_OK) < 0) {
1883 if (errno != ENOENT)
1884 log_warning("access() failed on %s: %s", fpath, strerror(errno));
1891 if (asprintf(&name, "%s.service", de->d_name+3) < 0) {
1896 if ((r = manager_load_unit(m, name, &service)) < 0)
1899 if ((r = manager_load_unit(m, rcnd_table[i+1], &runlevel)) < 0)
1902 if (de->d_name[0] == 'S') {
1903 if ((r = unit_add_dependency(runlevel, UNIT_WANTS, service)) < 0)
1906 if ((r = unit_add_dependency(runlevel, UNIT_AFTER, service)) < 0)
1909 } else if (de->d_name[0] == 'K' &&
1910 (streq(rcnd_table[i+1], SPECIAL_RUNLEVEL0_TARGET) ||
1911 streq(rcnd_table[i+1], SPECIAL_RUNLEVEL6_TARGET))) {
1913 /* We honour K links only for
1914 * halt/reboot. For the normal
1915 * runlevels we assume the
1917 * implicitly added by the
1920 if ((r = unit_add_dependency(runlevel, UNIT_CONFLICTS, service)) < 0)
1923 if ((r = unit_add_dependency(runlevel, UNIT_BEFORE, service)) < 0)
1940 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
1941 [SERVICE_DEAD] = "dead",
1942 [SERVICE_START_PRE] = "start-pre",
1943 [SERVICE_START] = "start",
1944 [SERVICE_START_POST] = "start-post",
1945 [SERVICE_RUNNING] = "running",
1946 [SERVICE_RELOAD] = "reload",
1947 [SERVICE_STOP] = "stop",
1948 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
1949 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
1950 [SERVICE_STOP_POST] = "stop-post",
1951 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
1952 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
1953 [SERVICE_MAINTAINANCE] = "maintainance",
1954 [SERVICE_AUTO_RESTART] = "auto-restart",
1957 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
1959 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
1960 [SERVICE_ONCE] = "once",
1961 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
1962 [SERVICE_RESTART_ALWAYS] = "restart-on-failure",
1965 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
1967 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
1968 [SERVICE_FORKING] = "forking",
1969 [SERVICE_SIMPLE] = "simple",
1970 [SERVICE_FINISH] = "finish"
1973 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
1975 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
1976 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
1977 [SERVICE_EXEC_START] = "ExecStart",
1978 [SERVICE_EXEC_START_POST] = "ExecStartPost",
1979 [SERVICE_EXEC_RELOAD] = "ExecReload",
1980 [SERVICE_EXEC_STOP] = "ExecStop",
1981 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
1984 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
1986 const UnitVTable service_vtable = {
1987 .suffix = ".service",
1989 .init = service_init,
1990 .load = service_load,
1991 .done = service_done,
1993 .dump = service_dump,
1995 .start = service_start,
1996 .stop = service_stop,
1997 .reload = service_reload,
1999 .can_reload = service_can_reload,
2001 .active_state = service_active_state,
2003 .sigchld_event = service_sigchld_event,
2004 .timer_event = service_timer_event,
2006 .cgroup_notify_empty = service_cgroup_notify_event,
2008 .enumerate = service_enumerate