1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "load-fragment.h"
30 #include "load-dropin.h"
33 #include "unit-name.h"
34 #include "dbus-service.h"
36 #define COMMENTS "#;\n"
37 #define NEWLINES "\n\r"
40 typedef enum RunlevelType {
49 const RunlevelType type;
51 /* Standard SysV runlevels */
52 { "rc0.d", SPECIAL_RUNLEVEL0_TARGET, RUNLEVEL_DOWN },
53 { "rc1.d", SPECIAL_RUNLEVEL1_TARGET, RUNLEVEL_UP },
54 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
55 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
56 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
57 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
58 { "rc6.d", SPECIAL_RUNLEVEL6_TARGET, RUNLEVEL_DOWN },
60 /* SUSE style boot.d */
61 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
63 /* Debian style rcS.d */
64 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
67 #define RUNLEVELS_UP "12345"
68 /* #define RUNLEVELS_DOWN "06" */
69 /* #define RUNLEVELS_BOOT "bBsS" */
71 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
72 [SERVICE_DEAD] = UNIT_INACTIVE,
73 [SERVICE_START_PRE] = UNIT_ACTIVATING,
74 [SERVICE_START] = UNIT_ACTIVATING,
75 [SERVICE_START_POST] = UNIT_ACTIVATING,
76 [SERVICE_RUNNING] = UNIT_ACTIVE,
77 [SERVICE_EXITED] = UNIT_ACTIVE,
78 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
79 [SERVICE_STOP] = UNIT_DEACTIVATING,
80 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
81 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
82 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
83 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
84 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
85 [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
86 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
89 static void service_init(Unit *u) {
90 Service *s = SERVICE(u);
93 assert(u->meta.load_state == UNIT_STUB);
95 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
96 s->restart_usec = DEFAULT_RESTART_USEC;
97 s->timer_watch.type = WATCH_INVALID;
98 s->sysv_start_priority = -1;
101 exec_context_init(&s->exec_context);
103 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
105 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
108 static void service_unwatch_control_pid(Service *s) {
111 if (s->control_pid <= 0)
114 unit_unwatch_pid(UNIT(s), s->control_pid);
118 static void service_unwatch_main_pid(Service *s) {
121 if (s->main_pid <= 0)
124 unit_unwatch_pid(UNIT(s), s->main_pid);
128 static void service_close_socket_fd(Service *s) {
131 if (s->socket_fd < 0)
134 close_nointr_nofail(s->socket_fd);
138 static void service_done(Unit *u) {
139 Service *s = SERVICE(u);
149 free(s->sysv_runlevels);
150 s->sysv_runlevels = NULL;
152 free(s->status_text);
153 s->status_text = NULL;
155 exec_context_done(&s->exec_context);
156 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
157 s->control_command = NULL;
159 /* This will leak a process, but at least no memory or any of
161 service_unwatch_main_pid(s);
162 service_unwatch_control_pid(s);
165 unit_unwatch_bus_name(UNIT(u), s->bus_name);
170 service_close_socket_fd(s);
172 unit_unwatch_timer(u, &s->timer_watch);
175 static char *sysv_translate_name(const char *name) {
178 if (!(r = new(char, strlen(name) + sizeof(".service"))))
181 if (startswith(name, "boot."))
182 /* Drop SuSE-style boot. prefix */
183 strcpy(stpcpy(r, name + 5), ".service");
184 else if (endswith(name, ".sh"))
185 /* Drop Debian-style .sh suffix */
186 strcpy(stpcpy(r, name) - 3, ".service");
188 /* Normal init scripts */
189 strcpy(stpcpy(r, name), ".service");
194 static int sysv_translate_facility(const char *name, char **_r) {
196 static const char * const table[] = {
197 /* LSB defined facilities */
198 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
199 "$network", SPECIAL_NETWORK_TARGET,
200 "$named", SPECIAL_NSS_LOOKUP_TARGET,
201 "$portmap", SPECIAL_RPCBIND_TARGET,
202 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
203 "$syslog", SPECIAL_SYSLOG_TARGET,
204 "$time", SPECIAL_RTC_SET_TARGET,
206 /* Debian extensions */
207 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
208 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
209 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
215 for (i = 0; i < ELEMENTSOF(table); i += 2)
216 if (streq(table[i], name)) {
217 if (!(r = strdup(table[i+1])))
226 if (!(r = sysv_translate_name(name)))
237 static int sysv_fix_order(Service *s) {
243 if (s->sysv_start_priority < 0)
246 /* For each pair of services where at least one lacks a LSB
247 * header, we use the start priority value to order things. */
249 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
253 t = (Service*) other;
258 if (t->sysv_start_priority < 0)
261 /* If both units have modern headers we don't care
262 * about the priorities */
263 if ((!s->sysv_path || s->sysv_has_lsb) &&
264 (!t->sysv_path || t->sysv_has_lsb))
267 if (t->sysv_start_priority < s->sysv_start_priority)
269 else if (t->sysv_start_priority > s->sysv_start_priority)
274 /* FIXME: Maybe we should compare the name here lexicographically? */
276 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
283 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
286 if (!(c = new0(ExecCommand, 1)))
289 if (!(c->path = strdup(path))) {
294 if (!(c->argv = strv_new(path, arg1, NULL))) {
303 static int sysv_exec_commands(Service *s) {
307 assert(s->sysv_path);
309 if (!(c = exec_command_new(s->sysv_path, "start")))
311 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
313 if (!(c = exec_command_new(s->sysv_path, "stop")))
315 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
317 if (!(c = exec_command_new(s->sysv_path, "reload")))
319 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
324 static int service_load_sysv_path(Service *s, const char *path) {
341 if (!(f = fopen(path, "re"))) {
342 r = errno == ENOENT ? 0 : -errno;
347 if (!(s->sysv_path = strdup(path))) {
353 char l[LINE_MAX], *t;
355 if (!fgets(l, sizeof(l), f)) {
360 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
370 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
372 s->sysv_has_lsb = true;
376 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
382 t += strspn(t, WHITESPACE);
384 if (state == NORMAL) {
386 /* Try to parse Red Hat style chkconfig headers */
388 if (startswith_no_case(t, "chkconfig:")) {
390 char runlevels[16], *k;
394 if (sscanf(t+10, "%15s %i %*i",
396 &start_priority) != 2) {
398 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
402 /* A start priority gathered from the
403 * symlink farms is preferred over the
404 * data from the LSB header. */
405 if (start_priority < 0 || start_priority > 99)
406 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
407 else if (s->sysv_start_priority < 0)
408 s->sysv_start_priority = start_priority;
410 char_array_0(runlevels);
411 k = delete_chars(runlevels, WHITESPACE "-");
416 if (!(d = strdup(k))) {
421 free(s->sysv_runlevels);
422 s->sysv_runlevels = d;
425 } else if (startswith_no_case(t, "description:")) {
427 size_t k = strlen(t);
430 if (t[k-1] == '\\') {
435 if (!(d = strdup(strstrip(t+12)))) {
440 free(u->meta.description);
441 u->meta.description = d;
443 } else if (startswith_no_case(t, "pidfile:")) {
450 if (!path_is_absolute(fn)) {
451 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
455 if (!(fn = strdup(fn))) {
464 } else if (state == DESCRIPTION) {
466 /* Try to parse Red Hat style description
469 size_t k = strlen(t);
477 assert(u->meta.description);
478 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
483 free(u->meta.description);
484 u->meta.description = d;
486 } else if (state == LSB || state == LSB_DESCRIPTION) {
488 if (startswith_no_case(t, "Provides:")) {
494 FOREACH_WORD(w, z, t+9, i) {
497 if (!(n = strndup(w, z))) {
502 r = sysv_translate_facility(n, &m);
511 if (unit_name_to_type(m) == UNIT_SERVICE)
512 r = unit_add_name(u, m);
514 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL, true)) >= 0)
515 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
524 } else if (startswith_no_case(t, "Required-Start:") ||
525 startswith_no_case(t, "Should-Start:") ||
526 startswith_no_case(t, "X-Start-Before:") ||
527 startswith_no_case(t, "X-Start-After:")) {
533 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
536 if (!(n = strndup(w, z))) {
541 r = sysv_translate_facility(n, &m);
550 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
556 } else if (startswith_no_case(t, "Default-Start:")) {
561 k = delete_chars(t+14, WHITESPACE "-");
564 if (!(d = strdup(k))) {
569 free(s->sysv_runlevels);
570 s->sysv_runlevels = d;
573 } else if (startswith_no_case(t, "Description:")) {
576 state = LSB_DESCRIPTION;
578 if (!(d = strdup(strstrip(t+12)))) {
583 free(u->meta.description);
584 u->meta.description = d;
586 } else if (startswith_no_case(t, "Short-Description:") &&
587 !u->meta.description) {
590 /* We use the short description only
591 * if no long description is set. */
595 if (!(d = strdup(strstrip(t+18)))) {
600 u->meta.description = d;
602 } else if (startswith_no_case(t, "X-Interactive:")) {
605 if ((b = parse_boolean(strstrip(t+14))) < 0) {
606 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
611 s->exec_context.std_input = EXEC_INPUT_TTY;
613 s->exec_context.std_input = EXEC_INPUT_NULL;
615 } else if (state == LSB_DESCRIPTION) {
617 if (startswith(l, "#\t") || startswith(l, "# ")) {
620 assert(u->meta.description);
621 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
626 free(u->meta.description);
627 u->meta.description = d;
634 if ((r = sysv_exec_commands(s)) < 0)
637 if (!s->sysv_runlevels || chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
638 /* If there a runlevels configured for this service
639 * but none of the standard ones, then we assume this
640 * is some special kind of service (which might be
641 * needed for early boot) and don't create any links
644 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0 ||
645 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
649 /* Don't timeout special services during boot (like fsck) */
652 /* Special setting for all SysV services */
653 s->type = SERVICE_FORKING;
654 s->valid_no_process = true;
655 s->kill_mode = KILL_PROCESS_GROUP;
656 s->restart = SERVICE_ONCE;
658 u->meta.load_state = UNIT_LOADED;
669 static int service_load_sysv_name(Service *s, const char *name) {
675 /* For SysV services we strip the boot. or .sh
676 * prefixes/suffixes. */
677 if (startswith(name, "boot.") ||
678 endswith(name, ".sh.service"))
681 STRV_FOREACH(p, UNIT(s)->meta.manager->lookup_paths.sysvinit_path) {
685 if (asprintf(&path, "%s/%s", *p, name) < 0)
688 assert(endswith(path, ".service"));
689 path[strlen(path)-8] = 0;
691 r = service_load_sysv_path(s, path);
693 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
694 /* Try Debian style xxx.sh source'able init scripts */
696 r = service_load_sysv_path(s, path);
701 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
702 /* Try SUSE style boot.xxx init scripts */
704 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
707 path[strlen(path)-8] = 0;
708 r = service_load_sysv_path(s, path);
715 if ((UNIT(s)->meta.load_state != UNIT_STUB))
722 static int service_load_sysv(Service *s) {
729 /* Load service data from SysV init scripts, preferably with
732 if (strv_isempty(UNIT(s)->meta.manager->lookup_paths.sysvinit_path))
735 if ((t = UNIT(s)->meta.id))
736 if ((r = service_load_sysv_name(s, t)) < 0)
739 if (UNIT(s)->meta.load_state == UNIT_STUB)
740 SET_FOREACH(t, UNIT(s)->meta.names, i) {
741 if (t == UNIT(s)->meta.id)
744 if ((r == service_load_sysv_name(s, t)) < 0)
747 if (UNIT(s)->meta.load_state != UNIT_STUB)
754 static int service_add_bus_name(Service *s) {
761 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
764 r = unit_merge_by_name(UNIT(s), n);
770 static int service_verify(Service *s) {
773 if (UNIT(s)->meta.load_state != UNIT_LOADED)
776 if (!s->exec_command[SERVICE_EXEC_START]) {
777 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
781 if (s->type == SERVICE_DBUS && !s->bus_name) {
782 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->meta.id);
789 static int service_load(Unit *u) {
791 Service *s = SERVICE(u);
795 /* Load a .service file */
796 if ((r = unit_load_fragment(u)) < 0)
799 /* Load a classic init script as a fallback, if we couldn't find anything */
800 if (u->meta.load_state == UNIT_STUB)
801 if ((r = service_load_sysv(s)) < 0)
804 /* Still nothing found? Then let's give up */
805 if (u->meta.load_state == UNIT_STUB)
808 /* We were able to load something, then let's add in the
809 * dropin directories. */
810 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
813 /* This is a new unit? Then let's add in some extras */
814 if (u->meta.load_state == UNIT_LOADED) {
815 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
818 if ((r = unit_add_default_cgroup(u)) < 0)
821 if ((r = sysv_fix_order(s)) < 0)
825 if ((r = service_add_bus_name(s)) < 0)
828 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
833 return service_verify(s);
836 static void service_dump(Unit *u, FILE *f, const char *prefix) {
838 ServiceExecCommand c;
839 Service *s = SERVICE(u);
845 p2 = strappend(prefix, "\t");
846 prefix2 = p2 ? p2 : prefix;
849 "%sService State: %s\n"
850 "%sPermissionsStartOnly: %s\n"
851 "%sRootDirectoryStartOnly: %s\n"
852 "%sValidNoProcess: %s\n"
855 prefix, service_state_to_string(s->state),
856 prefix, yes_no(s->permissions_start_only),
857 prefix, yes_no(s->root_directory_start_only),
858 prefix, yes_no(s->valid_no_process),
859 prefix, kill_mode_to_string(s->kill_mode),
860 prefix, service_type_to_string(s->type));
862 if (s->control_pid > 0)
864 "%sControl PID: %llu\n",
865 prefix, (unsigned long long) s->control_pid);
869 "%sMain PID: %llu\n",
870 prefix, (unsigned long long) s->main_pid);
875 prefix, s->pid_file);
880 "%sBus Name Good: %s\n",
882 prefix, yes_no(s->bus_name_good));
884 exec_context_dump(&s->exec_context, f, prefix);
886 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
888 if (!s->exec_command[c])
891 fprintf(f, "%s-> %s:\n",
892 prefix, service_exec_command_to_string(c));
894 exec_command_dump_list(s->exec_command[c], f, prefix2);
899 "%sSysV Init Script Path: %s\n"
900 "%sSysV Init Script has LSB Header: %s\n",
901 prefix, s->sysv_path,
902 prefix, yes_no(s->sysv_has_lsb));
904 if (s->sysv_start_priority >= 0)
906 "%sSysVStartPriority: %i\n",
907 prefix, s->sysv_start_priority);
909 if (s->sysv_runlevels)
910 fprintf(f, "%sSysVRunLevels: %s\n",
911 prefix, s->sysv_runlevels);
914 fprintf(f, "%sStatus Text: %s\n",
915 prefix, s->status_text);
920 static int service_load_pid_file(Service *s) {
927 if (s->main_pid_known)
930 assert(s->main_pid <= 0);
935 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
938 if ((r = safe_atolu(k, &p)) < 0) {
943 if ((unsigned long) (pid_t) p != p)
949 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
950 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
951 (unsigned long long) p, s->pid_file);
955 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
956 /* FIXME: we need to do something here */
959 s->main_pid = (pid_t) p;
960 s->main_pid_known = true;
965 static int service_get_sockets(Service *s, Set **_set) {
974 /* Collects all Socket objects that belong to this
975 * service. Note that a service might have multiple sockets
976 * via multiple names. */
978 if (!(set = set_new(NULL, NULL)))
981 SET_FOREACH(t, UNIT(s)->meta.names, i) {
985 /* Look for all socket objects that go by any of our
986 * units and collect their fds */
988 if (!(k = unit_name_change_suffix(t, ".socket"))) {
993 p = manager_get_unit(UNIT(s)->meta.manager, k);
999 if ((r = set_put(set, p)) < 0)
1011 static int service_notify_sockets_dead(Service *s) {
1019 /* Notifies all our sockets when we die */
1020 if ((r = service_get_sockets(s, &set)) < 0)
1023 SET_FOREACH(sock, set, i)
1024 socket_notify_service_dead(sock);
1031 static void service_set_state(Service *s, ServiceState state) {
1032 ServiceState old_state;
1035 old_state = s->state;
1038 if (state != SERVICE_START_PRE &&
1039 state != SERVICE_START &&
1040 state != SERVICE_START_POST &&
1041 state != SERVICE_RELOAD &&
1042 state != SERVICE_STOP &&
1043 state != SERVICE_STOP_SIGTERM &&
1044 state != SERVICE_STOP_SIGKILL &&
1045 state != SERVICE_STOP_POST &&
1046 state != SERVICE_FINAL_SIGTERM &&
1047 state != SERVICE_FINAL_SIGKILL &&
1048 state != SERVICE_AUTO_RESTART)
1049 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1051 if (state != SERVICE_START &&
1052 state != SERVICE_START_POST &&
1053 state != SERVICE_RUNNING &&
1054 state != SERVICE_RELOAD &&
1055 state != SERVICE_STOP &&
1056 state != SERVICE_STOP_SIGTERM &&
1057 state != SERVICE_STOP_SIGKILL)
1058 service_unwatch_main_pid(s);
1060 if (state != SERVICE_START_PRE &&
1061 state != SERVICE_START &&
1062 state != SERVICE_START_POST &&
1063 state != SERVICE_RELOAD &&
1064 state != SERVICE_STOP &&
1065 state != SERVICE_STOP_SIGTERM &&
1066 state != SERVICE_STOP_SIGKILL &&
1067 state != SERVICE_STOP_POST &&
1068 state != SERVICE_FINAL_SIGTERM &&
1069 state != SERVICE_FINAL_SIGKILL) {
1070 service_unwatch_control_pid(s);
1071 s->control_command = NULL;
1072 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1075 if (state == SERVICE_DEAD ||
1076 state == SERVICE_STOP ||
1077 state == SERVICE_STOP_SIGTERM ||
1078 state == SERVICE_STOP_SIGKILL ||
1079 state == SERVICE_STOP_POST ||
1080 state == SERVICE_FINAL_SIGTERM ||
1081 state == SERVICE_FINAL_SIGKILL ||
1082 state == SERVICE_MAINTAINANCE ||
1083 state == SERVICE_AUTO_RESTART)
1084 service_notify_sockets_dead(s);
1086 if (state != SERVICE_START_PRE &&
1087 state != SERVICE_START &&
1088 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1089 service_close_socket_fd(s);
1091 if (old_state != state)
1092 log_debug("%s changed %s -> %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1094 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1097 static int service_coldplug(Unit *u) {
1098 Service *s = SERVICE(u);
1102 assert(s->state == SERVICE_DEAD);
1104 if (s->deserialized_state != s->state) {
1106 if (s->deserialized_state == SERVICE_START_PRE ||
1107 s->deserialized_state == SERVICE_START ||
1108 s->deserialized_state == SERVICE_START_POST ||
1109 s->deserialized_state == SERVICE_RELOAD ||
1110 s->deserialized_state == SERVICE_STOP ||
1111 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1112 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1113 s->deserialized_state == SERVICE_STOP_POST ||
1114 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1115 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1116 s->deserialized_state == SERVICE_AUTO_RESTART) {
1118 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1121 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1123 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1128 if ((s->deserialized_state == SERVICE_START &&
1129 (s->type == SERVICE_FORKING ||
1130 s->type == SERVICE_DBUS ||
1131 s->type == SERVICE_FINISH ||
1132 s->type == SERVICE_NOTIFY)) ||
1133 s->deserialized_state == SERVICE_START_POST ||
1134 s->deserialized_state == SERVICE_RUNNING ||
1135 s->deserialized_state == SERVICE_RELOAD ||
1136 s->deserialized_state == SERVICE_STOP ||
1137 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1138 s->deserialized_state == SERVICE_STOP_SIGKILL)
1139 if (s->main_pid > 0)
1140 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1143 if (s->deserialized_state == SERVICE_START_PRE ||
1144 s->deserialized_state == SERVICE_START ||
1145 s->deserialized_state == SERVICE_START_POST ||
1146 s->deserialized_state == SERVICE_RELOAD ||
1147 s->deserialized_state == SERVICE_STOP ||
1148 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1149 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1150 s->deserialized_state == SERVICE_STOP_POST ||
1151 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1152 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1153 if (s->control_pid > 0)
1154 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1157 service_set_state(s, s->deserialized_state);
1163 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1167 unsigned rn_fds = 0;
1175 if ((r = service_get_sockets(s, &set)) < 0)
1178 SET_FOREACH(sock, set, i) {
1182 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1194 if (!(t = new(int, rn_fds+cn_fds))) {
1200 memcpy(t, rfds, rn_fds);
1201 memcpy(t+rn_fds, cfds, cn_fds);
1206 rn_fds = rn_fds+cn_fds;
1224 static int service_spawn(
1229 bool apply_permissions,
1244 if (s->socket_fd >= 0) {
1245 fds = &s->socket_fd;
1247 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1251 if (timeout && s->timeout_usec) {
1252 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1255 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1257 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1266 s->meta.manager->environment,
1269 UNIT(s)->meta.manager->confirm_spawn,
1270 UNIT(s)->meta.cgroup_bondings,
1278 if (s->socket_fd >= 0)
1279 service_close_socket_fd(s);
1284 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1285 /* FIXME: we need to do something here */
1296 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1301 static int main_pid_good(Service *s) {
1304 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1307 /* If we know the pid file, then lets just check if it is
1309 if (s->main_pid_known)
1310 return s->main_pid > 0;
1312 /* We don't know the pid */
1316 static int control_pid_good(Service *s) {
1319 return s->control_pid > 0;
1322 static int cgroup_good(Service *s) {
1327 if (s->valid_no_process)
1330 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1336 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1343 if (allow_restart &&
1345 (s->restart == SERVICE_RESTART_ALWAYS ||
1346 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1348 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1351 service_set_state(s, SERVICE_AUTO_RESTART);
1353 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1358 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
1359 service_enter_dead(s, false, false);
1362 static void service_enter_signal(Service *s, ServiceState state, bool success);
1364 static void service_enter_stop_post(Service *s, bool success) {
1371 service_unwatch_control_pid(s);
1373 s->control_command_id = SERVICE_EXEC_STOP_POST;
1374 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1375 if ((r = service_spawn(s,
1379 !s->permissions_start_only,
1380 !s->root_directory_start_only,
1381 &s->control_pid)) < 0)
1385 service_set_state(s, SERVICE_STOP_POST);
1387 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1392 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->meta.id, strerror(-r));
1393 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1396 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1405 if (s->kill_mode != KILL_NONE) {
1406 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1408 if (s->kill_mode == KILL_CONTROL_GROUP) {
1410 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1411 if (r != -EAGAIN && r != -ESRCH)
1420 if (s->main_pid > 0) {
1421 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1427 if (s->control_pid > 0) {
1428 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1439 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1440 if (s->timeout_usec > 0)
1441 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1444 service_set_state(s, state);
1445 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1446 service_enter_stop_post(s, true);
1448 service_enter_dead(s, true, true);
1453 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
1455 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1456 service_enter_stop_post(s, false);
1458 service_enter_dead(s, false, true);
1461 static void service_enter_stop(Service *s, bool success) {
1468 service_unwatch_control_pid(s);
1470 s->control_command_id = SERVICE_EXEC_STOP;
1471 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1472 if ((r = service_spawn(s,
1476 !s->permissions_start_only,
1477 !s->root_directory_start_only,
1478 &s->control_pid)) < 0)
1481 service_set_state(s, SERVICE_STOP);
1483 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1488 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->meta.id, strerror(-r));
1489 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1492 static void service_enter_running(Service *s, bool success) {
1498 if (main_pid_good(s) != 0 &&
1499 cgroup_good(s) != 0 &&
1500 (s->bus_name_good || s->type != SERVICE_DBUS))
1501 service_set_state(s, SERVICE_RUNNING);
1502 else if (s->valid_no_process)
1503 service_set_state(s, SERVICE_EXITED);
1505 service_enter_stop(s, true);
1508 static void service_enter_start_post(Service *s) {
1512 service_unwatch_control_pid(s);
1514 s->control_command_id = SERVICE_EXEC_START_POST;
1515 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1516 if ((r = service_spawn(s,
1520 !s->permissions_start_only,
1521 !s->root_directory_start_only,
1522 &s->control_pid)) < 0)
1526 service_set_state(s, SERVICE_START_POST);
1528 service_enter_running(s, true);
1533 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->meta.id, strerror(-r));
1534 service_enter_stop(s, false);
1537 static void service_enter_start(Service *s) {
1543 assert(s->exec_command[SERVICE_EXEC_START]);
1544 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1546 if (s->type == SERVICE_FORKING)
1547 service_unwatch_control_pid(s);
1549 service_unwatch_main_pid(s);
1551 if ((r = service_spawn(s,
1552 s->exec_command[SERVICE_EXEC_START],
1553 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1560 if (s->type == SERVICE_SIMPLE) {
1561 /* For simple services we immediately start
1562 * the START_POST binaries. */
1565 s->main_pid_known = true;
1567 service_enter_start_post(s);
1569 } else if (s->type == SERVICE_FORKING) {
1571 /* For forking services we wait until the start
1572 * process exited. */
1574 s->control_pid = pid;
1576 s->control_command_id = SERVICE_EXEC_START;
1577 s->control_command = s->exec_command[SERVICE_EXEC_START];
1578 service_set_state(s, SERVICE_START);
1580 } else if (s->type == SERVICE_FINISH ||
1581 s->type == SERVICE_DBUS ||
1582 s->type == SERVICE_NOTIFY) {
1584 /* For finishing services we wait until the start
1585 * process exited, too, but it is our main process. */
1587 /* For D-Bus services we know the main pid right away,
1588 * but wait for the bus name to appear on the
1589 * bus. Notify services are similar. */
1592 s->main_pid_known = true;
1594 service_set_state(s, SERVICE_START);
1596 assert_not_reached("Unknown service type");
1601 log_warning("%s failed to run 'start' task: %s", UNIT(s)->meta.id, strerror(-r));
1602 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1605 static void service_enter_start_pre(Service *s) {
1610 service_unwatch_control_pid(s);
1612 s->control_command_id = SERVICE_EXEC_START_PRE;
1613 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1614 if ((r = service_spawn(s,
1618 !s->permissions_start_only,
1619 !s->root_directory_start_only,
1620 &s->control_pid)) < 0)
1623 service_set_state(s, SERVICE_START_PRE);
1625 service_enter_start(s);
1630 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->meta.id, strerror(-r));
1631 service_enter_dead(s, false, true);
1634 static void service_enter_restart(Service *s) {
1638 service_enter_dead(s, true, false);
1640 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1643 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
1648 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
1649 service_enter_dead(s, false, false);
1652 static void service_enter_reload(Service *s) {
1657 service_unwatch_control_pid(s);
1659 s->control_command_id = SERVICE_EXEC_RELOAD;
1660 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1661 if ((r = service_spawn(s,
1665 !s->permissions_start_only,
1666 !s->root_directory_start_only,
1667 &s->control_pid)) < 0)
1670 service_set_state(s, SERVICE_RELOAD);
1672 service_enter_running(s, true);
1677 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->meta.id, strerror(-r));
1678 service_enter_stop(s, false);
1681 static void service_run_next(Service *s, bool success) {
1685 assert(s->control_command);
1686 assert(s->control_command->command_next);
1691 s->control_command = s->control_command->command_next;
1693 service_unwatch_control_pid(s);
1695 if ((r = service_spawn(s,
1699 !s->permissions_start_only,
1700 !s->root_directory_start_only,
1701 &s->control_pid)) < 0)
1707 log_warning("%s failed to run spawn next task: %s", UNIT(s)->meta.id, strerror(-r));
1709 if (s->state == SERVICE_START_PRE)
1710 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1711 else if (s->state == SERVICE_STOP)
1712 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1713 else if (s->state == SERVICE_STOP_POST)
1714 service_enter_dead(s, false, true);
1716 service_enter_stop(s, false);
1719 static int service_start(Unit *u) {
1720 Service *s = SERVICE(u);
1724 /* We cannot fulfill this request right now, try again later
1726 if (s->state == SERVICE_STOP ||
1727 s->state == SERVICE_STOP_SIGTERM ||
1728 s->state == SERVICE_STOP_SIGKILL ||
1729 s->state == SERVICE_STOP_POST ||
1730 s->state == SERVICE_FINAL_SIGTERM ||
1731 s->state == SERVICE_FINAL_SIGKILL)
1734 /* Already on it! */
1735 if (s->state == SERVICE_START_PRE ||
1736 s->state == SERVICE_START ||
1737 s->state == SERVICE_START_POST)
1740 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1742 /* Make sure we don't enter a busy loop of some kind. */
1743 if (!ratelimit_test(&s->ratelimit)) {
1744 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1749 s->main_pid_known = false;
1750 s->allow_restart = true;
1752 service_enter_start_pre(s);
1756 static int service_stop(Unit *u) {
1757 Service *s = SERVICE(u);
1761 /* Cannot do this now */
1762 if (s->state == SERVICE_START_PRE ||
1763 s->state == SERVICE_START ||
1764 s->state == SERVICE_START_POST ||
1765 s->state == SERVICE_RELOAD)
1769 if (s->state == SERVICE_STOP ||
1770 s->state == SERVICE_STOP_SIGTERM ||
1771 s->state == SERVICE_STOP_SIGKILL ||
1772 s->state == SERVICE_STOP_POST ||
1773 s->state == SERVICE_FINAL_SIGTERM ||
1774 s->state == SERVICE_FINAL_SIGKILL)
1777 if (s->state == SERVICE_AUTO_RESTART) {
1778 service_set_state(s, SERVICE_DEAD);
1782 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1784 /* This is a user request, so don't do restarts on this
1786 s->allow_restart = false;
1788 service_enter_stop(s, true);
1792 static int service_reload(Unit *u) {
1793 Service *s = SERVICE(u);
1797 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1799 service_enter_reload(s);
1803 static bool service_can_reload(Unit *u) {
1804 Service *s = SERVICE(u);
1808 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1811 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1812 Service *s = SERVICE(u);
1818 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1819 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1821 if (s->control_pid > 0)
1822 unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) (s->control_pid));
1824 if (s->main_pid > 0)
1825 unit_serialize_item_format(u, f, "main-pid", "%u", (unsigned) (s->main_pid));
1827 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1829 /* There's a minor uncleanliness here: if there are multiple
1830 * commands attached here, we will start from the first one
1832 if (s->control_command_id >= 0)
1833 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
1835 if (s->socket_fd >= 0) {
1838 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1841 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1847 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1848 Service *s = SERVICE(u);
1856 if (streq(key, "state")) {
1859 if ((state = service_state_from_string(value)) < 0)
1860 log_debug("Failed to parse state value %s", value);
1862 s->deserialized_state = state;
1863 } else if (streq(key, "failure")) {
1866 if ((b = parse_boolean(value)) < 0)
1867 log_debug("Failed to parse failure value %s", value);
1869 s->failure = b || s->failure;
1870 } else if (streq(key, "control-pid")) {
1873 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1874 log_debug("Failed to parse control-pid value %s", value);
1876 s->control_pid = (pid_t) pid;
1877 } else if (streq(key, "main-pid")) {
1880 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1881 log_debug("Failed to parse main-pid value %s", value);
1883 s->main_pid = (pid_t) pid;
1884 } else if (streq(key, "main-pid-known")) {
1887 if ((b = parse_boolean(value)) < 0)
1888 log_debug("Failed to parse main-pid-known value %s", value);
1890 s->main_pid_known = b;
1891 } else if (streq(key, "control-command")) {
1892 ServiceExecCommand id;
1894 if ((id = service_exec_command_from_string(value)) < 0)
1895 log_debug("Failed to parse exec-command value %s", value);
1897 s->control_command_id = id;
1898 s->control_command = s->exec_command[id];
1900 } else if (streq(key, "socket-fd")) {
1903 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1904 log_debug("Failed to parse socket-fd value %s", value);
1907 if (s->socket_fd >= 0)
1908 close_nointr_nofail(s->socket_fd);
1909 s->socket_fd = fdset_remove(fds, fd);
1912 log_debug("Unknown serialization key '%s'", key);
1917 static UnitActiveState service_active_state(Unit *u) {
1920 return state_translation_table[SERVICE(u)->state];
1923 static const char *service_sub_state_to_string(Unit *u) {
1926 return service_state_to_string(SERVICE(u)->state);
1929 static bool service_check_gc(Unit *u) {
1930 Service *s = SERVICE(u);
1934 return !!s->sysv_path;
1937 static bool service_check_snapshot(Unit *u) {
1938 Service *s = SERVICE(u);
1942 return !s->got_socket_fd;
1945 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1946 Service *s = SERVICE(u);
1952 success = is_clean_exit(code, status);
1953 s->failure = s->failure || !success;
1955 if (s->main_pid == pid) {
1957 exec_status_fill(&s->main_exec_status, pid, code, status);
1960 if (s->type != SERVICE_FORKING) {
1961 assert(s->exec_command[SERVICE_EXEC_START]);
1962 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1965 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
1967 /* The service exited, so the service is officially
1972 case SERVICE_START_POST:
1973 case SERVICE_RELOAD:
1975 /* Need to wait until the operation is
1980 if (s->type == SERVICE_FINISH) {
1981 /* This was our main goal, so let's go on */
1983 service_enter_start_post(s);
1985 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1988 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
1993 case SERVICE_RUNNING:
1994 service_enter_running(s, success);
1997 case SERVICE_STOP_SIGTERM:
1998 case SERVICE_STOP_SIGKILL:
2000 if (!control_pid_good(s))
2001 service_enter_stop_post(s, success);
2003 /* If there is still a control process, wait for that first */
2007 assert_not_reached("Uh, main process died at wrong time.");
2010 } else if (s->control_pid == pid) {
2012 if (s->control_command)
2013 exec_status_fill(&s->control_command->exec_status, pid, code, status);
2017 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2019 /* If we are shutting things down anyway we
2020 * don't care about failing commands. */
2022 if (s->control_command && s->control_command->command_next && success) {
2024 /* There is another command to *
2025 * execute, so let's do that. */
2027 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
2028 service_run_next(s, success);
2031 /* No further commands for this step, so let's
2032 * figure out what to do next */
2034 s->control_command = NULL;
2035 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2037 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2041 case SERVICE_START_PRE:
2043 service_enter_start(s);
2045 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2049 assert(s->type == SERVICE_FORKING);
2051 /* Let's try to load the pid
2052 * file here if we can. We
2053 * ignore the return value,
2054 * since the PID file might
2055 * actually be created by a
2056 * START_POST script */
2060 service_load_pid_file(s);
2062 service_enter_start_post(s);
2064 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2068 case SERVICE_START_POST:
2069 if (success && s->pid_file && !s->main_pid_known) {
2072 /* Hmm, let's see if we can
2073 * load the pid now after the
2074 * start-post scripts got
2077 if ((r = service_load_pid_file(s)) < 0)
2078 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
2083 case SERVICE_RELOAD:
2085 service_enter_running(s, true);
2087 service_enter_stop(s, false);
2092 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2095 case SERVICE_STOP_SIGTERM:
2096 case SERVICE_STOP_SIGKILL:
2097 if (main_pid_good(s) <= 0)
2098 service_enter_stop_post(s, success);
2100 /* If there is still a service
2101 * process around, wait until
2102 * that one quit, too */
2105 case SERVICE_STOP_POST:
2106 case SERVICE_FINAL_SIGTERM:
2107 case SERVICE_FINAL_SIGKILL:
2108 service_enter_dead(s, success, true);
2112 assert_not_reached("Uh, control process died at wrong time.");
2118 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2119 Service *s = SERVICE(u);
2122 assert(elapsed == 1);
2124 assert(w == &s->timer_watch);
2128 case SERVICE_START_PRE:
2130 log_warning("%s operation timed out. Terminating.", u->meta.id);
2131 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2134 case SERVICE_START_POST:
2135 case SERVICE_RELOAD:
2136 log_warning("%s operation timed out. Stopping.", u->meta.id);
2137 service_enter_stop(s, false);
2141 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2142 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2145 case SERVICE_STOP_SIGTERM:
2146 log_warning("%s stopping timed out. Killing.", u->meta.id);
2147 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2150 case SERVICE_STOP_SIGKILL:
2151 /* Uh, wie sent a SIGKILL and it is still not gone?
2152 * Must be something we cannot kill, so let's just be
2153 * weirded out and continue */
2155 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2156 service_enter_stop_post(s, false);
2159 case SERVICE_STOP_POST:
2160 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2161 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2164 case SERVICE_FINAL_SIGTERM:
2165 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2166 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2169 case SERVICE_FINAL_SIGKILL:
2170 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u->meta.id);
2171 service_enter_dead(s, false, true);
2174 case SERVICE_AUTO_RESTART:
2175 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
2176 service_enter_restart(s);
2180 assert_not_reached("Timeout at wrong time.");
2184 static void service_cgroup_notify_event(Unit *u) {
2185 Service *s = SERVICE(u);
2189 log_debug("%s: cgroup is empty", u->meta.id);
2193 /* Waiting for SIGCHLD is usually more interesting,
2194 * because it includes return codes/signals. Which is
2195 * why we ignore the cgroup events for most cases,
2196 * except when we don't know pid which to expect the
2199 case SERVICE_RUNNING:
2200 service_enter_running(s, true);
2208 static void service_notify_message(Unit *u, char **tags) {
2209 Service *s = SERVICE(u);
2214 log_debug("%s: Got message", u->meta.id);
2216 /* Interpret MAINPID= */
2217 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2218 (s->state == SERVICE_START ||
2219 s->state == SERVICE_START_POST ||
2220 s->state == SERVICE_RUNNING ||
2221 s->state == SERVICE_RELOAD)) {
2224 if (safe_atolu(e + 8, &pid) < 0 ||
2225 (unsigned long) (pid_t) pid != pid ||
2227 log_warning("Failed to parse %s", e);
2229 log_debug("%s: got %s", u->meta.id, e);
2230 s->main_pid = (pid_t) pid;
2234 /* Interpret READY= */
2235 if (s->type == SERVICE_NOTIFY &&
2236 s->state == SERVICE_START &&
2237 strv_find(tags, "READY=1")) {
2238 log_debug("%s: got READY=1", u->meta.id);
2240 service_enter_start_post(s);
2243 /* Interpret STATUS= */
2244 if ((e = strv_find_prefix(tags, "STATUS="))) {
2247 if (!(t = strdup(e+7))) {
2248 log_error("Failed to allocate string.");
2252 log_debug("%s: got %s", u->meta.id, e);
2254 free(s->status_text);
2259 static int service_enumerate(Manager *m) {
2263 char *path = NULL, *fpath = NULL, *name = NULL;
2268 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2269 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2274 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2282 if (!(d = opendir(path))) {
2283 if (errno != ENOENT)
2284 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2289 while ((de = readdir(d))) {
2293 if (ignore_file(de->d_name))
2296 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2299 if (strlen(de->d_name) < 4)
2302 a = undecchar(de->d_name[1]);
2303 b = undecchar(de->d_name[2]);
2310 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2315 if (access(fpath, X_OK) < 0) {
2317 if (errno != ENOENT)
2318 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2324 if (!(name = sysv_translate_name(de->d_name + 3))) {
2329 if ((r = manager_load_unit_prepare(m, name, NULL, &service)) < 0) {
2330 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2334 if (de->d_name[0] == 'S' &&
2335 (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT))
2336 SERVICE(service)->sysv_start_priority =
2337 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2339 manager_dispatch_load_queue(m);
2340 service = unit_follow_merge(service);
2342 if (de->d_name[0] == 'S') {
2343 Unit *runlevel_target;
2345 if ((r = manager_load_unit(m, rcnd_table[i].target, NULL, &runlevel_target)) < 0)
2348 if ((r = unit_add_dependency(runlevel_target, UNIT_WANTS, service, true)) < 0)
2351 if ((r = unit_add_dependency(service, UNIT_BEFORE, runlevel_target, true)) < 0)
2354 } else if (de->d_name[0] == 'K' &&
2355 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2356 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2357 Unit *shutdown_target;
2359 /* We honour K links only for
2360 * halt/reboot. For the normal
2361 * runlevels we assume the
2363 * implicitly added by the
2364 * core logic. Also, we don't
2365 * really distuingish here
2366 * between the runlevels 0 and
2367 * 6 and just add them to the
2368 * special shutdown target. On
2369 * SUSE the boot.d/ runlevel
2370 * is also used for shutdown,
2371 * so we add links for that
2372 * too to the shutdown
2375 if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
2378 if ((r = unit_add_dependency(service, UNIT_CONFLICTS, shutdown_target, true)) < 0)
2381 if ((r = unit_add_dependency(service, UNIT_BEFORE, shutdown_target, true)) < 0)
2400 static void service_bus_name_owner_change(
2403 const char *old_owner,
2404 const char *new_owner) {
2406 Service *s = SERVICE(u);
2411 assert(streq(s->bus_name, name));
2412 assert(old_owner || new_owner);
2414 if (old_owner && new_owner)
2415 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2417 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2419 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2421 s->bus_name_good = !!new_owner;
2423 if (s->type == SERVICE_DBUS) {
2425 /* service_enter_running() will figure out what to
2427 if (s->state == SERVICE_RUNNING)
2428 service_enter_running(s, true);
2429 else if (s->state == SERVICE_START && new_owner)
2430 service_enter_start_post(s);
2432 } else if (new_owner &&
2434 (s->state == SERVICE_START ||
2435 s->state == SERVICE_START_POST ||
2436 s->state == SERVICE_RUNNING ||
2437 s->state == SERVICE_RELOAD)) {
2439 /* Try to acquire PID from bus service */
2440 log_debug("Trying to acquire PID from D-Bus name...");
2442 bus_query_pid(u->meta.manager, name);
2446 static void service_bus_query_pid_done(
2451 Service *s = SERVICE(u);
2456 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2458 if (s->main_pid <= 0 &&
2459 (s->state == SERVICE_START ||
2460 s->state == SERVICE_START_POST ||
2461 s->state == SERVICE_RUNNING ||
2462 s->state == SERVICE_RELOAD))
2466 int service_set_socket_fd(Service *s, int fd) {
2470 /* This is called by the socket code when instantiating a new
2471 * service for a stream socket and the socket needs to be
2474 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2477 if (s->socket_fd >= 0)
2480 if (s->state != SERVICE_DEAD)
2484 s->got_socket_fd = true;
2488 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2489 [SERVICE_DEAD] = "dead",
2490 [SERVICE_START_PRE] = "start-pre",
2491 [SERVICE_START] = "start",
2492 [SERVICE_START_POST] = "start-post",
2493 [SERVICE_RUNNING] = "running",
2494 [SERVICE_EXITED] = "exited",
2495 [SERVICE_RELOAD] = "reload",
2496 [SERVICE_STOP] = "stop",
2497 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2498 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2499 [SERVICE_STOP_POST] = "stop-post",
2500 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2501 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2502 [SERVICE_MAINTAINANCE] = "maintainance",
2503 [SERVICE_AUTO_RESTART] = "auto-restart",
2506 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2508 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2509 [SERVICE_ONCE] = "once",
2510 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2511 [SERVICE_RESTART_ALWAYS] = "restart-always",
2514 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2516 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2517 [SERVICE_FORKING] = "forking",
2518 [SERVICE_SIMPLE] = "simple",
2519 [SERVICE_FINISH] = "finish",
2520 [SERVICE_DBUS] = "dbus",
2521 [SERVICE_NOTIFY] = "notify"
2524 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2526 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2527 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2528 [SERVICE_EXEC_START] = "ExecStart",
2529 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2530 [SERVICE_EXEC_RELOAD] = "ExecReload",
2531 [SERVICE_EXEC_STOP] = "ExecStop",
2532 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2535 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2537 const UnitVTable service_vtable = {
2538 .suffix = ".service",
2540 .init = service_init,
2541 .done = service_done,
2542 .load = service_load,
2544 .coldplug = service_coldplug,
2546 .dump = service_dump,
2548 .start = service_start,
2549 .stop = service_stop,
2550 .reload = service_reload,
2552 .can_reload = service_can_reload,
2554 .serialize = service_serialize,
2555 .deserialize_item = service_deserialize_item,
2557 .active_state = service_active_state,
2558 .sub_state_to_string = service_sub_state_to_string,
2560 .check_gc = service_check_gc,
2561 .check_snapshot = service_check_snapshot,
2563 .sigchld_event = service_sigchld_event,
2564 .timer_event = service_timer_event,
2566 .cgroup_notify_empty = service_cgroup_notify_event,
2567 .notify_message = service_notify_message,
2569 .bus_name_owner_change = service_bus_name_owner_change,
2570 .bus_query_pid_done = service_bus_query_pid_done,
2572 .bus_message_handler = bus_service_message_handler,
2574 .enumerate = service_enumerate