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"
37 #define COMMENTS "#;\n"
38 #define NEWLINES "\n\r"
41 typedef enum RunlevelType {
50 const RunlevelType type;
52 /* Standard SysV runlevels */
53 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
54 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
55 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
56 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
57 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
58 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
59 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN },
61 /* SUSE style boot.d */
62 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
64 /* Debian style rcS.d */
65 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
68 #define RUNLEVELS_UP "12345"
69 /* #define RUNLEVELS_DOWN "06" */
70 /* #define RUNLEVELS_BOOT "bBsS" */
72 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
73 [SERVICE_DEAD] = UNIT_INACTIVE,
74 [SERVICE_START_PRE] = UNIT_ACTIVATING,
75 [SERVICE_START] = UNIT_ACTIVATING,
76 [SERVICE_START_POST] = UNIT_ACTIVATING,
77 [SERVICE_RUNNING] = UNIT_ACTIVE,
78 [SERVICE_EXITED] = UNIT_ACTIVE,
79 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
80 [SERVICE_STOP] = UNIT_DEACTIVATING,
81 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
82 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
83 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
84 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
85 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
86 [SERVICE_MAINTENANCE] = UNIT_INACTIVE,
87 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
90 static void service_init(Unit *u) {
91 Service *s = SERVICE(u);
94 assert(u->meta.load_state == UNIT_STUB);
96 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
97 s->restart_usec = DEFAULT_RESTART_USEC;
98 s->timer_watch.type = WATCH_INVALID;
99 s->sysv_start_priority = -1;
102 exec_context_init(&s->exec_context);
104 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
106 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
109 static void service_unwatch_control_pid(Service *s) {
112 if (s->control_pid <= 0)
115 unit_unwatch_pid(UNIT(s), s->control_pid);
119 static void service_unwatch_main_pid(Service *s) {
122 if (s->main_pid <= 0)
125 unit_unwatch_pid(UNIT(s), s->main_pid);
129 static int service_set_main_pid(Service *s, pid_t pid) {
139 s->main_pid_known = true;
144 static int service_set_control_pid(Service *s, pid_t pid) {
153 s->control_pid = pid;
158 static void service_close_socket_fd(Service *s) {
161 if (s->socket_fd < 0)
164 close_nointr_nofail(s->socket_fd);
168 static void service_done(Unit *u) {
169 Service *s = SERVICE(u);
179 free(s->sysv_runlevels);
180 s->sysv_runlevels = NULL;
182 free(s->status_text);
183 s->status_text = NULL;
185 exec_context_done(&s->exec_context);
186 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
187 s->control_command = NULL;
189 /* This will leak a process, but at least no memory or any of
191 service_unwatch_main_pid(s);
192 service_unwatch_control_pid(s);
195 unit_unwatch_bus_name(UNIT(u), s->bus_name);
200 service_close_socket_fd(s);
202 unit_unwatch_timer(u, &s->timer_watch);
205 static char *sysv_translate_name(const char *name) {
208 if (!(r = new(char, strlen(name) + sizeof(".service"))))
211 if (startswith(name, "boot."))
212 /* Drop SuSE-style boot. prefix */
213 strcpy(stpcpy(r, name + 5), ".service");
214 else if (endswith(name, ".sh"))
215 /* Drop Debian-style .sh suffix */
216 strcpy(stpcpy(r, name) - 3, ".service");
218 /* Normal init scripts */
219 strcpy(stpcpy(r, name), ".service");
224 static int sysv_translate_facility(const char *name, char **_r) {
226 static const char * const table[] = {
227 /* LSB defined facilities */
228 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
229 "$network", SPECIAL_NETWORK_TARGET,
230 "$named", SPECIAL_NSS_LOOKUP_TARGET,
231 "$portmap", SPECIAL_RPCBIND_TARGET,
232 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
233 "$syslog", SPECIAL_SYSLOG_TARGET,
234 "$time", SPECIAL_RTC_SET_TARGET,
236 /* Debian extensions */
237 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
238 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
239 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
245 for (i = 0; i < ELEMENTSOF(table); i += 2)
246 if (streq(table[i], name)) {
247 if (!(r = strdup(table[i+1])))
256 if (!(r = sysv_translate_name(name)))
267 static int sysv_fix_order(Service *s) {
273 if (s->sysv_start_priority < 0)
276 /* For each pair of services where at least one lacks a LSB
277 * header, we use the start priority value to order things. */
279 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
283 t = (Service*) other;
288 if (t->sysv_start_priority < 0)
291 /* If both units have modern headers we don't care
292 * about the priorities */
293 if ((!s->sysv_path || s->sysv_has_lsb) &&
294 (!t->sysv_path || t->sysv_has_lsb))
297 if (t->sysv_start_priority < s->sysv_start_priority)
299 else if (t->sysv_start_priority > s->sysv_start_priority)
304 /* FIXME: Maybe we should compare the name here lexicographically? */
306 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
313 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
316 if (!(c = new0(ExecCommand, 1)))
319 if (!(c->path = strdup(path))) {
324 if (!(c->argv = strv_new(path, arg1, NULL))) {
333 static int sysv_exec_commands(Service *s) {
337 assert(s->sysv_path);
339 if (!(c = exec_command_new(s->sysv_path, "start")))
341 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
343 if (!(c = exec_command_new(s->sysv_path, "stop")))
345 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
347 if (!(c = exec_command_new(s->sysv_path, "reload")))
349 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
354 static int service_load_sysv_path(Service *s, const char *path) {
371 if (!(f = fopen(path, "re"))) {
372 r = errno == ENOENT ? 0 : -errno;
377 if (!(s->sysv_path = strdup(path))) {
383 char l[LINE_MAX], *t;
385 if (!fgets(l, sizeof(l), f)) {
390 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
400 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
402 s->sysv_has_lsb = true;
406 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
412 t += strspn(t, WHITESPACE);
414 if (state == NORMAL) {
416 /* Try to parse Red Hat style chkconfig headers */
418 if (startswith_no_case(t, "chkconfig:")) {
420 char runlevels[16], *k;
424 if (sscanf(t+10, "%15s %i %*i",
426 &start_priority) != 2) {
428 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
432 /* A start priority gathered from the
433 * symlink farms is preferred over the
434 * data from the LSB header. */
435 if (start_priority < 0 || start_priority > 99)
436 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
437 else if (s->sysv_start_priority < 0)
438 s->sysv_start_priority = start_priority;
440 char_array_0(runlevels);
441 k = delete_chars(runlevels, WHITESPACE "-");
446 if (!(d = strdup(k))) {
451 free(s->sysv_runlevels);
452 s->sysv_runlevels = d;
455 } else if (startswith_no_case(t, "description:")) {
457 size_t k = strlen(t);
460 if (t[k-1] == '\\') {
465 if (!(d = strdup(strstrip(t+12)))) {
470 free(u->meta.description);
471 u->meta.description = d;
473 } else if (startswith_no_case(t, "pidfile:")) {
480 if (!path_is_absolute(fn)) {
481 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
485 if (!(fn = strdup(fn))) {
494 } else if (state == DESCRIPTION) {
496 /* Try to parse Red Hat style description
499 size_t k = strlen(t);
507 assert(u->meta.description);
508 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
513 free(u->meta.description);
514 u->meta.description = d;
516 } else if (state == LSB || state == LSB_DESCRIPTION) {
518 if (startswith_no_case(t, "Provides:")) {
524 FOREACH_WORD(w, z, t+9, i) {
527 if (!(n = strndup(w, z))) {
532 r = sysv_translate_facility(n, &m);
541 if (unit_name_to_type(m) == UNIT_SERVICE)
542 r = unit_add_name(u, m);
544 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL, true)) >= 0)
545 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
554 } else if (startswith_no_case(t, "Required-Start:") ||
555 startswith_no_case(t, "Should-Start:") ||
556 startswith_no_case(t, "X-Start-Before:") ||
557 startswith_no_case(t, "X-Start-After:")) {
563 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
566 if (!(n = strndup(w, z))) {
571 r = sysv_translate_facility(n, &m);
580 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
586 } else if (startswith_no_case(t, "Default-Start:")) {
591 k = delete_chars(t+14, WHITESPACE "-");
594 if (!(d = strdup(k))) {
599 free(s->sysv_runlevels);
600 s->sysv_runlevels = d;
603 } else if (startswith_no_case(t, "Description:")) {
606 state = LSB_DESCRIPTION;
608 if (!(d = strdup(strstrip(t+12)))) {
613 free(u->meta.description);
614 u->meta.description = d;
616 } else if (startswith_no_case(t, "Short-Description:") &&
617 !u->meta.description) {
620 /* We use the short description only
621 * if no long description is set. */
625 if (!(d = strdup(strstrip(t+18)))) {
630 u->meta.description = d;
632 } else if (startswith_no_case(t, "X-Interactive:")) {
635 if ((b = parse_boolean(strstrip(t+14))) < 0) {
636 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
641 s->exec_context.std_input = EXEC_INPUT_TTY;
643 s->exec_context.std_input = EXEC_INPUT_NULL;
645 } else if (state == LSB_DESCRIPTION) {
647 if (startswith(l, "#\t") || startswith(l, "# ")) {
650 assert(u->meta.description);
651 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
656 free(u->meta.description);
657 u->meta.description = d;
664 if ((r = sysv_exec_commands(s)) < 0)
667 if (!s->sysv_runlevels || chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
668 /* If there a runlevels configured for this service
669 * but none of the standard ones, then we assume this
670 * is some special kind of service (which might be
671 * needed for early boot) and don't create any links
674 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0 ||
675 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
679 /* Don't timeout special services during boot (like fsck) */
682 /* Special setting for all SysV services */
683 s->type = SERVICE_FORKING;
684 s->valid_no_process = true;
685 s->kill_mode = KILL_PROCESS_GROUP;
686 s->restart = SERVICE_ONCE;
688 u->meta.load_state = UNIT_LOADED;
699 static int service_load_sysv_name(Service *s, const char *name) {
705 /* For SysV services we strip the boot. or .sh
706 * prefixes/suffixes. */
707 if (startswith(name, "boot.") ||
708 endswith(name, ".sh.service"))
711 STRV_FOREACH(p, UNIT(s)->meta.manager->lookup_paths.sysvinit_path) {
715 if (asprintf(&path, "%s/%s", *p, name) < 0)
718 assert(endswith(path, ".service"));
719 path[strlen(path)-8] = 0;
721 r = service_load_sysv_path(s, path);
723 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
724 /* Try Debian style xxx.sh source'able init scripts */
726 r = service_load_sysv_path(s, path);
731 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
732 /* Try SUSE style boot.xxx init scripts */
734 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
737 path[strlen(path)-8] = 0;
738 r = service_load_sysv_path(s, path);
745 if ((UNIT(s)->meta.load_state != UNIT_STUB))
752 static int service_load_sysv(Service *s) {
759 /* Load service data from SysV init scripts, preferably with
762 if (strv_isempty(UNIT(s)->meta.manager->lookup_paths.sysvinit_path))
765 if ((t = UNIT(s)->meta.id))
766 if ((r = service_load_sysv_name(s, t)) < 0)
769 if (UNIT(s)->meta.load_state == UNIT_STUB)
770 SET_FOREACH(t, UNIT(s)->meta.names, i) {
771 if (t == UNIT(s)->meta.id)
774 if ((r == service_load_sysv_name(s, t)) < 0)
777 if (UNIT(s)->meta.load_state != UNIT_STUB)
784 static int service_add_bus_name(Service *s) {
791 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
794 r = unit_merge_by_name(UNIT(s), n);
800 static int service_verify(Service *s) {
803 if (UNIT(s)->meta.load_state != UNIT_LOADED)
806 if (!s->exec_command[SERVICE_EXEC_START]) {
807 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
811 if (s->type == SERVICE_DBUS && !s->bus_name) {
812 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->meta.id);
819 static int service_load(Unit *u) {
821 Service *s = SERVICE(u);
825 /* Load a .service file */
826 if ((r = unit_load_fragment(u)) < 0)
829 /* Load a classic init script as a fallback, if we couldn't find anything */
830 if (u->meta.load_state == UNIT_STUB)
831 if ((r = service_load_sysv(s)) < 0)
834 /* Still nothing found? Then let's give up */
835 if (u->meta.load_state == UNIT_STUB)
838 /* We were able to load something, then let's add in the
839 * dropin directories. */
840 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
843 /* This is a new unit? Then let's add in some extras */
844 if (u->meta.load_state == UNIT_LOADED) {
845 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
848 if ((r = unit_add_default_cgroup(u)) < 0)
851 if ((r = sysv_fix_order(s)) < 0)
855 if ((r = service_add_bus_name(s)) < 0)
858 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
863 return service_verify(s);
866 static void service_dump(Unit *u, FILE *f, const char *prefix) {
868 ServiceExecCommand c;
869 Service *s = SERVICE(u);
875 p2 = strappend(prefix, "\t");
876 prefix2 = p2 ? p2 : prefix;
879 "%sService State: %s\n"
880 "%sPermissionsStartOnly: %s\n"
881 "%sRootDirectoryStartOnly: %s\n"
882 "%sValidNoProcess: %s\n"
885 prefix, service_state_to_string(s->state),
886 prefix, yes_no(s->permissions_start_only),
887 prefix, yes_no(s->root_directory_start_only),
888 prefix, yes_no(s->valid_no_process),
889 prefix, kill_mode_to_string(s->kill_mode),
890 prefix, service_type_to_string(s->type));
892 if (s->control_pid > 0)
894 "%sControl PID: %llu\n",
895 prefix, (unsigned long long) s->control_pid);
899 "%sMain PID: %llu\n",
900 prefix, (unsigned long long) s->main_pid);
905 prefix, s->pid_file);
910 "%sBus Name Good: %s\n",
912 prefix, yes_no(s->bus_name_good));
914 exec_context_dump(&s->exec_context, f, prefix);
916 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
918 if (!s->exec_command[c])
921 fprintf(f, "%s-> %s:\n",
922 prefix, service_exec_command_to_string(c));
924 exec_command_dump_list(s->exec_command[c], f, prefix2);
929 "%sSysV Init Script Path: %s\n"
930 "%sSysV Init Script has LSB Header: %s\n",
931 prefix, s->sysv_path,
932 prefix, yes_no(s->sysv_has_lsb));
934 if (s->sysv_start_priority >= 0)
936 "%sSysVStartPriority: %i\n",
937 prefix, s->sysv_start_priority);
939 if (s->sysv_runlevels)
940 fprintf(f, "%sSysVRunLevels: %s\n",
941 prefix, s->sysv_runlevels);
944 fprintf(f, "%sStatus Text: %s\n",
945 prefix, s->status_text);
950 static int service_load_pid_file(Service *s) {
957 if (s->main_pid_known)
960 assert(s->main_pid <= 0);
965 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
968 r = parse_pid(k, &pid);
974 if (kill(pid, 0) < 0 && errno != EPERM) {
975 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
976 (unsigned long) pid, s->pid_file);
980 if ((r = service_set_main_pid(s, pid)) < 0)
983 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
984 /* FIXME: we need to do something here */
990 static int service_get_sockets(Service *s, Set **_set) {
999 /* Collects all Socket objects that belong to this
1000 * service. Note that a service might have multiple sockets
1001 * via multiple names. */
1003 if (!(set = set_new(NULL, NULL)))
1006 SET_FOREACH(t, UNIT(s)->meta.names, i) {
1010 /* Look for all socket objects that go by any of our
1011 * units and collect their fds */
1013 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1018 p = manager_get_unit(UNIT(s)->meta.manager, k);
1024 if ((r = set_put(set, p)) < 0)
1036 static int service_notify_sockets_dead(Service *s) {
1044 /* Notifies all our sockets when we die */
1045 if ((r = service_get_sockets(s, &set)) < 0)
1048 SET_FOREACH(sock, set, i)
1049 socket_notify_service_dead(sock);
1056 static void service_set_state(Service *s, ServiceState state) {
1057 ServiceState old_state;
1060 old_state = s->state;
1063 if (state != SERVICE_START_PRE &&
1064 state != SERVICE_START &&
1065 state != SERVICE_START_POST &&
1066 state != SERVICE_RELOAD &&
1067 state != SERVICE_STOP &&
1068 state != SERVICE_STOP_SIGTERM &&
1069 state != SERVICE_STOP_SIGKILL &&
1070 state != SERVICE_STOP_POST &&
1071 state != SERVICE_FINAL_SIGTERM &&
1072 state != SERVICE_FINAL_SIGKILL &&
1073 state != SERVICE_AUTO_RESTART)
1074 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1076 if (state != SERVICE_START &&
1077 state != SERVICE_START_POST &&
1078 state != SERVICE_RUNNING &&
1079 state != SERVICE_RELOAD &&
1080 state != SERVICE_STOP &&
1081 state != SERVICE_STOP_SIGTERM &&
1082 state != SERVICE_STOP_SIGKILL)
1083 service_unwatch_main_pid(s);
1085 if (state != SERVICE_START_PRE &&
1086 state != SERVICE_START &&
1087 state != SERVICE_START_POST &&
1088 state != SERVICE_RELOAD &&
1089 state != SERVICE_STOP &&
1090 state != SERVICE_STOP_SIGTERM &&
1091 state != SERVICE_STOP_SIGKILL &&
1092 state != SERVICE_STOP_POST &&
1093 state != SERVICE_FINAL_SIGTERM &&
1094 state != SERVICE_FINAL_SIGKILL) {
1095 service_unwatch_control_pid(s);
1096 s->control_command = NULL;
1097 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1100 if (state == SERVICE_DEAD ||
1101 state == SERVICE_STOP ||
1102 state == SERVICE_STOP_SIGTERM ||
1103 state == SERVICE_STOP_SIGKILL ||
1104 state == SERVICE_STOP_POST ||
1105 state == SERVICE_FINAL_SIGTERM ||
1106 state == SERVICE_FINAL_SIGKILL ||
1107 state == SERVICE_MAINTENANCE ||
1108 state == SERVICE_AUTO_RESTART)
1109 service_notify_sockets_dead(s);
1111 if (state != SERVICE_START_PRE &&
1112 state != SERVICE_START &&
1113 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1114 service_close_socket_fd(s);
1116 if (old_state != state)
1117 log_debug("%s changed %s -> %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1119 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1122 static int service_coldplug(Unit *u) {
1123 Service *s = SERVICE(u);
1127 assert(s->state == SERVICE_DEAD);
1129 if (s->deserialized_state != s->state) {
1131 if (s->deserialized_state == SERVICE_START_PRE ||
1132 s->deserialized_state == SERVICE_START ||
1133 s->deserialized_state == SERVICE_START_POST ||
1134 s->deserialized_state == SERVICE_RELOAD ||
1135 s->deserialized_state == SERVICE_STOP ||
1136 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1137 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1138 s->deserialized_state == SERVICE_STOP_POST ||
1139 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1140 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1141 s->deserialized_state == SERVICE_AUTO_RESTART) {
1143 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1146 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1148 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1153 if ((s->deserialized_state == SERVICE_START &&
1154 (s->type == SERVICE_FORKING ||
1155 s->type == SERVICE_DBUS ||
1156 s->type == SERVICE_FINISH ||
1157 s->type == SERVICE_NOTIFY)) ||
1158 s->deserialized_state == SERVICE_START_POST ||
1159 s->deserialized_state == SERVICE_RUNNING ||
1160 s->deserialized_state == SERVICE_RELOAD ||
1161 s->deserialized_state == SERVICE_STOP ||
1162 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1163 s->deserialized_state == SERVICE_STOP_SIGKILL)
1164 if (s->main_pid > 0)
1165 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1168 if (s->deserialized_state == SERVICE_START_PRE ||
1169 s->deserialized_state == SERVICE_START ||
1170 s->deserialized_state == SERVICE_START_POST ||
1171 s->deserialized_state == SERVICE_RELOAD ||
1172 s->deserialized_state == SERVICE_STOP ||
1173 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1174 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1175 s->deserialized_state == SERVICE_STOP_POST ||
1176 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1177 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1178 if (s->control_pid > 0)
1179 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1182 service_set_state(s, s->deserialized_state);
1188 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1192 unsigned rn_fds = 0;
1200 if ((r = service_get_sockets(s, &set)) < 0)
1203 SET_FOREACH(sock, set, i) {
1207 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1219 if (!(t = new(int, rn_fds+cn_fds))) {
1225 memcpy(t, rfds, rn_fds);
1226 memcpy(t+rn_fds, cfds, cn_fds);
1231 rn_fds = rn_fds+cn_fds;
1249 static int service_spawn(
1254 bool apply_permissions,
1269 if (s->socket_fd >= 0) {
1270 fds = &s->socket_fd;
1272 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1276 if (timeout && s->timeout_usec) {
1277 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1280 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1282 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1291 s->meta.manager->environment,
1294 UNIT(s)->meta.manager->confirm_spawn,
1295 UNIT(s)->meta.cgroup_bondings,
1303 if (s->socket_fd >= 0)
1304 service_close_socket_fd(s);
1309 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1310 /* FIXME: we need to do something here */
1321 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1326 static int main_pid_good(Service *s) {
1329 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1332 /* If we know the pid file, then lets just check if it is
1334 if (s->main_pid_known)
1335 return s->main_pid > 0;
1337 /* We don't know the pid */
1341 static int control_pid_good(Service *s) {
1344 return s->control_pid > 0;
1347 static int cgroup_good(Service *s) {
1352 if (s->valid_no_process)
1355 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1361 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1368 if (allow_restart &&
1370 (s->restart == SERVICE_RESTART_ALWAYS ||
1371 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1373 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1376 service_set_state(s, SERVICE_AUTO_RESTART);
1378 service_set_state(s, s->failure ? SERVICE_MAINTENANCE : SERVICE_DEAD);
1383 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
1384 service_enter_dead(s, false, false);
1387 static void service_enter_signal(Service *s, ServiceState state, bool success);
1389 static void service_enter_stop_post(Service *s, bool success) {
1396 service_unwatch_control_pid(s);
1398 s->control_command_id = SERVICE_EXEC_STOP_POST;
1399 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1400 if ((r = service_spawn(s,
1404 !s->permissions_start_only,
1405 !s->root_directory_start_only,
1406 &s->control_pid)) < 0)
1410 service_set_state(s, SERVICE_STOP_POST);
1412 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1417 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->meta.id, strerror(-r));
1418 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1421 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1430 if (s->kill_mode != KILL_NONE) {
1431 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1433 if (s->kill_mode == KILL_CONTROL_GROUP) {
1435 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1436 if (r != -EAGAIN && r != -ESRCH)
1445 if (s->main_pid > 0) {
1446 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1452 if (s->control_pid > 0) {
1453 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1464 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1465 if (s->timeout_usec > 0)
1466 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1469 service_set_state(s, state);
1470 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1471 service_enter_stop_post(s, true);
1473 service_enter_dead(s, true, true);
1478 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
1480 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1481 service_enter_stop_post(s, false);
1483 service_enter_dead(s, false, true);
1486 static void service_enter_stop(Service *s, bool success) {
1495 service_unwatch_control_pid(s);
1497 s->control_command_id = SERVICE_EXEC_STOP;
1498 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1499 if ((r = service_spawn(s,
1503 !s->permissions_start_only,
1504 !s->root_directory_start_only,
1508 service_set_control_pid(s, pid);
1509 service_set_state(s, SERVICE_STOP);
1511 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1516 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->meta.id, strerror(-r));
1517 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1520 static void service_enter_running(Service *s, bool success) {
1526 if (main_pid_good(s) != 0 &&
1527 cgroup_good(s) != 0 &&
1528 (s->bus_name_good || s->type != SERVICE_DBUS))
1529 service_set_state(s, SERVICE_RUNNING);
1530 else if (s->valid_no_process)
1531 service_set_state(s, SERVICE_EXITED);
1533 service_enter_stop(s, true);
1536 static void service_enter_start_post(Service *s) {
1541 service_unwatch_control_pid(s);
1543 s->control_command_id = SERVICE_EXEC_START_POST;
1544 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1545 if ((r = service_spawn(s,
1549 !s->permissions_start_only,
1550 !s->root_directory_start_only,
1554 service_set_control_pid(s, pid);
1555 service_set_state(s, SERVICE_START_POST);
1557 service_enter_running(s, true);
1562 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->meta.id, strerror(-r));
1563 service_enter_stop(s, false);
1566 static void service_enter_start(Service *s) {
1572 assert(s->exec_command[SERVICE_EXEC_START]);
1573 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1575 if (s->type == SERVICE_FORKING)
1576 service_unwatch_control_pid(s);
1578 service_unwatch_main_pid(s);
1580 if ((r = service_spawn(s,
1581 s->exec_command[SERVICE_EXEC_START],
1582 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1589 if (s->type == SERVICE_SIMPLE) {
1590 /* For simple services we immediately start
1591 * the START_POST binaries. */
1593 service_set_main_pid(s, pid);
1594 service_enter_start_post(s);
1596 } else if (s->type == SERVICE_FORKING) {
1598 /* For forking services we wait until the start
1599 * process exited. */
1601 s->control_command_id = SERVICE_EXEC_START;
1602 s->control_command = s->exec_command[SERVICE_EXEC_START];
1604 service_set_control_pid(s, pid);
1605 service_set_state(s, SERVICE_START);
1607 } else if (s->type == SERVICE_FINISH ||
1608 s->type == SERVICE_DBUS ||
1609 s->type == SERVICE_NOTIFY) {
1611 /* For finishing services we wait until the start
1612 * process exited, too, but it is our main process. */
1614 /* For D-Bus services we know the main pid right away,
1615 * but wait for the bus name to appear on the
1616 * bus. Notify services are similar. */
1618 service_set_main_pid(s, pid);
1619 service_set_state(s, SERVICE_START);
1621 assert_not_reached("Unknown service type");
1626 log_warning("%s failed to run 'start' task: %s", UNIT(s)->meta.id, strerror(-r));
1627 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1630 static void service_enter_start_pre(Service *s) {
1636 service_unwatch_control_pid(s);
1638 s->control_command_id = SERVICE_EXEC_START_PRE;
1639 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1640 if ((r = service_spawn(s,
1644 !s->permissions_start_only,
1645 !s->root_directory_start_only,
1649 service_set_control_pid(s, pid);
1650 service_set_state(s, SERVICE_START_PRE);
1652 service_enter_start(s);
1657 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->meta.id, strerror(-r));
1658 service_enter_dead(s, false, true);
1661 static void service_enter_restart(Service *s) {
1665 service_enter_dead(s, true, false);
1667 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1670 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
1675 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
1676 service_enter_dead(s, false, false);
1679 static void service_enter_reload(Service *s) {
1685 service_unwatch_control_pid(s);
1687 s->control_command_id = SERVICE_EXEC_RELOAD;
1688 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1689 if ((r = service_spawn(s,
1693 !s->permissions_start_only,
1694 !s->root_directory_start_only,
1698 service_set_control_pid(s, pid);
1699 service_set_state(s, SERVICE_RELOAD);
1701 service_enter_running(s, true);
1706 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->meta.id, strerror(-r));
1707 service_enter_stop(s, false);
1710 static void service_run_next(Service *s, bool success) {
1715 assert(s->control_command);
1716 assert(s->control_command->command_next);
1721 s->control_command = s->control_command->command_next;
1723 service_unwatch_control_pid(s);
1725 if ((r = service_spawn(s,
1729 !s->permissions_start_only,
1730 !s->root_directory_start_only,
1734 service_set_control_pid(s, pid);
1738 log_warning("%s failed to run spawn next task: %s", UNIT(s)->meta.id, strerror(-r));
1740 if (s->state == SERVICE_START_PRE)
1741 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1742 else if (s->state == SERVICE_STOP)
1743 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1744 else if (s->state == SERVICE_STOP_POST)
1745 service_enter_dead(s, false, true);
1747 service_enter_stop(s, false);
1750 static int service_start(Unit *u) {
1751 Service *s = SERVICE(u);
1755 /* We cannot fulfill this request right now, try again later
1757 if (s->state == SERVICE_STOP ||
1758 s->state == SERVICE_STOP_SIGTERM ||
1759 s->state == SERVICE_STOP_SIGKILL ||
1760 s->state == SERVICE_STOP_POST ||
1761 s->state == SERVICE_FINAL_SIGTERM ||
1762 s->state == SERVICE_FINAL_SIGKILL)
1765 /* Already on it! */
1766 if (s->state == SERVICE_START_PRE ||
1767 s->state == SERVICE_START ||
1768 s->state == SERVICE_START_POST)
1771 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTENANCE || s->state == SERVICE_AUTO_RESTART);
1773 /* Make sure we don't enter a busy loop of some kind. */
1774 if (!ratelimit_test(&s->ratelimit)) {
1775 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1780 s->main_pid_known = false;
1781 s->allow_restart = true;
1783 service_enter_start_pre(s);
1787 static int service_stop(Unit *u) {
1788 Service *s = SERVICE(u);
1792 /* Cannot do this now */
1793 if (s->state == SERVICE_START_PRE ||
1794 s->state == SERVICE_START ||
1795 s->state == SERVICE_START_POST ||
1796 s->state == SERVICE_RELOAD)
1800 if (s->state == SERVICE_STOP ||
1801 s->state == SERVICE_STOP_SIGTERM ||
1802 s->state == SERVICE_STOP_SIGKILL ||
1803 s->state == SERVICE_STOP_POST ||
1804 s->state == SERVICE_FINAL_SIGTERM ||
1805 s->state == SERVICE_FINAL_SIGKILL)
1808 if (s->state == SERVICE_AUTO_RESTART) {
1809 service_set_state(s, SERVICE_DEAD);
1813 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1815 /* This is a user request, so don't do restarts on this
1817 s->allow_restart = false;
1819 service_enter_stop(s, true);
1823 static int service_reload(Unit *u) {
1824 Service *s = SERVICE(u);
1828 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1830 service_enter_reload(s);
1834 static bool service_can_reload(Unit *u) {
1835 Service *s = SERVICE(u);
1839 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1842 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1843 Service *s = SERVICE(u);
1849 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1850 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1852 if (s->control_pid > 0)
1853 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
1855 if (s->main_pid_known && s->main_pid > 0)
1856 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
1858 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1860 /* There's a minor uncleanliness here: if there are multiple
1861 * commands attached here, we will start from the first one
1863 if (s->control_command_id >= 0)
1864 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
1866 if (s->socket_fd >= 0) {
1869 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1872 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1878 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1879 Service *s = SERVICE(u);
1887 if (streq(key, "state")) {
1890 if ((state = service_state_from_string(value)) < 0)
1891 log_debug("Failed to parse state value %s", value);
1893 s->deserialized_state = state;
1894 } else if (streq(key, "failure")) {
1897 if ((b = parse_boolean(value)) < 0)
1898 log_debug("Failed to parse failure value %s", value);
1900 s->failure = b || s->failure;
1901 } else if (streq(key, "control-pid")) {
1904 if ((r = parse_pid(value, &pid)) < 0)
1905 log_debug("Failed to parse control-pid value %s", value);
1907 service_set_control_pid(s, pid);
1908 } else if (streq(key, "main-pid")) {
1911 if ((r = parse_pid(value, &pid)) < 0)
1912 log_debug("Failed to parse main-pid value %s", value);
1914 service_set_main_pid(s, (pid_t) pid);
1915 } else if (streq(key, "main-pid-known")) {
1918 if ((b = parse_boolean(value)) < 0)
1919 log_debug("Failed to parse main-pid-known value %s", value);
1921 s->main_pid_known = b;
1922 } else if (streq(key, "control-command")) {
1923 ServiceExecCommand id;
1925 if ((id = service_exec_command_from_string(value)) < 0)
1926 log_debug("Failed to parse exec-command value %s", value);
1928 s->control_command_id = id;
1929 s->control_command = s->exec_command[id];
1931 } else if (streq(key, "socket-fd")) {
1934 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1935 log_debug("Failed to parse socket-fd value %s", value);
1938 if (s->socket_fd >= 0)
1939 close_nointr_nofail(s->socket_fd);
1940 s->socket_fd = fdset_remove(fds, fd);
1943 log_debug("Unknown serialization key '%s'", key);
1948 static UnitActiveState service_active_state(Unit *u) {
1951 return state_translation_table[SERVICE(u)->state];
1954 static const char *service_sub_state_to_string(Unit *u) {
1957 return service_state_to_string(SERVICE(u)->state);
1960 static bool service_check_gc(Unit *u) {
1961 Service *s = SERVICE(u);
1965 return !!s->sysv_path;
1968 static bool service_check_snapshot(Unit *u) {
1969 Service *s = SERVICE(u);
1973 return !s->got_socket_fd;
1976 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1977 Service *s = SERVICE(u);
1983 success = is_clean_exit(code, status);
1984 s->failure = s->failure || !success;
1986 if (s->main_pid == pid) {
1988 exec_status_fill(&s->main_exec_status, pid, code, status);
1991 if (s->type != SERVICE_FORKING) {
1992 assert(s->exec_command[SERVICE_EXEC_START]);
1993 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1996 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
1998 /* The service exited, so the service is officially
2003 case SERVICE_START_POST:
2004 case SERVICE_RELOAD:
2006 /* Need to wait until the operation is
2011 if (s->type == SERVICE_FINISH) {
2012 /* This was our main goal, so let's go on */
2014 service_enter_start_post(s);
2016 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2019 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2024 case SERVICE_RUNNING:
2025 service_enter_running(s, success);
2028 case SERVICE_STOP_SIGTERM:
2029 case SERVICE_STOP_SIGKILL:
2031 if (!control_pid_good(s))
2032 service_enter_stop_post(s, success);
2034 /* If there is still a control process, wait for that first */
2038 assert_not_reached("Uh, main process died at wrong time.");
2041 } else if (s->control_pid == pid) {
2043 if (s->control_command)
2044 exec_status_fill(&s->control_command->exec_status, pid, code, status);
2048 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2050 /* If we are shutting things down anyway we
2051 * don't care about failing commands. */
2053 if (s->control_command && s->control_command->command_next && success) {
2055 /* There is another command to *
2056 * execute, so let's do that. */
2058 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
2059 service_run_next(s, success);
2062 /* No further commands for this step, so let's
2063 * figure out what to do next */
2065 s->control_command = NULL;
2066 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2068 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2072 case SERVICE_START_PRE:
2074 service_enter_start(s);
2076 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2080 assert(s->type == SERVICE_FORKING);
2082 /* Let's try to load the pid
2083 * file here if we can. We
2084 * ignore the return value,
2085 * since the PID file might
2086 * actually be created by a
2087 * START_POST script */
2091 service_load_pid_file(s);
2093 service_enter_start_post(s);
2095 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2099 case SERVICE_START_POST:
2100 if (success && s->pid_file && !s->main_pid_known) {
2103 /* Hmm, let's see if we can
2104 * load the pid now after the
2105 * start-post scripts got
2108 if ((r = service_load_pid_file(s)) < 0)
2109 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
2114 case SERVICE_RELOAD:
2116 service_enter_running(s, true);
2118 service_enter_stop(s, false);
2123 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2126 case SERVICE_STOP_SIGTERM:
2127 case SERVICE_STOP_SIGKILL:
2128 if (main_pid_good(s) <= 0)
2129 service_enter_stop_post(s, success);
2131 /* If there is still a service
2132 * process around, wait until
2133 * that one quit, too */
2136 case SERVICE_STOP_POST:
2137 case SERVICE_FINAL_SIGTERM:
2138 case SERVICE_FINAL_SIGKILL:
2139 service_enter_dead(s, success, true);
2143 assert_not_reached("Uh, control process died at wrong time.");
2149 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2150 Service *s = SERVICE(u);
2153 assert(elapsed == 1);
2155 assert(w == &s->timer_watch);
2159 case SERVICE_START_PRE:
2161 log_warning("%s operation timed out. Terminating.", u->meta.id);
2162 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2165 case SERVICE_START_POST:
2166 case SERVICE_RELOAD:
2167 log_warning("%s operation timed out. Stopping.", u->meta.id);
2168 service_enter_stop(s, false);
2172 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2173 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2176 case SERVICE_STOP_SIGTERM:
2177 log_warning("%s stopping timed out. Killing.", u->meta.id);
2178 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2181 case SERVICE_STOP_SIGKILL:
2182 /* Uh, wie sent a SIGKILL and it is still not gone?
2183 * Must be something we cannot kill, so let's just be
2184 * weirded out and continue */
2186 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2187 service_enter_stop_post(s, false);
2190 case SERVICE_STOP_POST:
2191 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2192 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2195 case SERVICE_FINAL_SIGTERM:
2196 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2197 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2200 case SERVICE_FINAL_SIGKILL:
2201 log_warning("%s still around after SIGKILL (2). Entering maintenance mode.", u->meta.id);
2202 service_enter_dead(s, false, true);
2205 case SERVICE_AUTO_RESTART:
2206 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
2207 service_enter_restart(s);
2211 assert_not_reached("Timeout at wrong time.");
2215 static void service_cgroup_notify_event(Unit *u) {
2216 Service *s = SERVICE(u);
2220 log_debug("%s: cgroup is empty", u->meta.id);
2224 /* Waiting for SIGCHLD is usually more interesting,
2225 * because it includes return codes/signals. Which is
2226 * why we ignore the cgroup events for most cases,
2227 * except when we don't know pid which to expect the
2230 case SERVICE_RUNNING:
2231 service_enter_running(s, true);
2239 static void service_notify_message(Unit *u, char **tags) {
2240 Service *s = SERVICE(u);
2245 log_debug("%s: Got message", u->meta.id);
2247 /* Interpret MAINPID= */
2248 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2249 (s->state == SERVICE_START ||
2250 s->state == SERVICE_START_POST ||
2251 s->state == SERVICE_RUNNING ||
2252 s->state == SERVICE_RELOAD)) {
2255 if (parse_pid(e + 8, &pid) < 0)
2256 log_warning("Failed to parse %s", e);
2258 log_debug("%s: got %s", u->meta.id, e);
2259 service_set_main_pid(s, pid);
2263 /* Interpret READY= */
2264 if (s->type == SERVICE_NOTIFY &&
2265 s->state == SERVICE_START &&
2266 strv_find(tags, "READY=1")) {
2267 log_debug("%s: got READY=1", u->meta.id);
2269 service_enter_start_post(s);
2272 /* Interpret STATUS= */
2273 if ((e = strv_find_prefix(tags, "STATUS="))) {
2276 if (!(t = strdup(e+7))) {
2277 log_error("Failed to allocate string.");
2281 log_debug("%s: got %s", u->meta.id, e);
2283 free(s->status_text);
2288 static int service_enumerate(Manager *m) {
2292 char *path = NULL, *fpath = NULL, *name = NULL;
2297 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2298 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2303 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2311 if (!(d = opendir(path))) {
2312 if (errno != ENOENT)
2313 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2318 while ((de = readdir(d))) {
2322 if (ignore_file(de->d_name))
2325 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2328 if (strlen(de->d_name) < 4)
2331 a = undecchar(de->d_name[1]);
2332 b = undecchar(de->d_name[2]);
2339 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2344 if (access(fpath, X_OK) < 0) {
2346 if (errno != ENOENT)
2347 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2353 if (!(name = sysv_translate_name(de->d_name + 3))) {
2358 if ((r = manager_load_unit_prepare(m, name, NULL, &service)) < 0) {
2359 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2363 if (de->d_name[0] == 'S' &&
2364 (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT))
2365 SERVICE(service)->sysv_start_priority =
2366 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2368 manager_dispatch_load_queue(m);
2369 service = unit_follow_merge(service);
2371 if (de->d_name[0] == 'S') {
2372 Unit *runlevel_target;
2374 if ((r = manager_load_unit(m, rcnd_table[i].target, NULL, &runlevel_target)) < 0)
2377 if ((r = unit_add_dependency(runlevel_target, UNIT_WANTS, service, true)) < 0)
2380 if ((r = unit_add_dependency(service, UNIT_BEFORE, runlevel_target, true)) < 0)
2383 } else if (de->d_name[0] == 'K' &&
2384 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2385 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2386 Unit *shutdown_target;
2388 /* We honour K links only for
2389 * halt/reboot. For the normal
2390 * runlevels we assume the
2392 * implicitly added by the
2393 * core logic. Also, we don't
2394 * really distuingish here
2395 * between the runlevels 0 and
2396 * 6 and just add them to the
2397 * special shutdown target. On
2398 * SUSE the boot.d/ runlevel
2399 * is also used for shutdown,
2400 * so we add links for that
2401 * too to the shutdown
2404 if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
2407 if ((r = unit_add_dependency(service, UNIT_CONFLICTS, shutdown_target, true)) < 0)
2410 if ((r = unit_add_dependency(service, UNIT_BEFORE, shutdown_target, true)) < 0)
2429 static void service_bus_name_owner_change(
2432 const char *old_owner,
2433 const char *new_owner) {
2435 Service *s = SERVICE(u);
2440 assert(streq(s->bus_name, name));
2441 assert(old_owner || new_owner);
2443 if (old_owner && new_owner)
2444 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2446 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2448 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2450 s->bus_name_good = !!new_owner;
2452 if (s->type == SERVICE_DBUS) {
2454 /* service_enter_running() will figure out what to
2456 if (s->state == SERVICE_RUNNING)
2457 service_enter_running(s, true);
2458 else if (s->state == SERVICE_START && new_owner)
2459 service_enter_start_post(s);
2461 } else if (new_owner &&
2463 (s->state == SERVICE_START ||
2464 s->state == SERVICE_START_POST ||
2465 s->state == SERVICE_RUNNING ||
2466 s->state == SERVICE_RELOAD)) {
2468 /* Try to acquire PID from bus service */
2469 log_debug("Trying to acquire PID from D-Bus name...");
2471 bus_query_pid(u->meta.manager, name);
2475 static void service_bus_query_pid_done(
2480 Service *s = SERVICE(u);
2485 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2487 if (s->main_pid <= 0 &&
2488 (s->state == SERVICE_START ||
2489 s->state == SERVICE_START_POST ||
2490 s->state == SERVICE_RUNNING ||
2491 s->state == SERVICE_RELOAD))
2492 service_set_main_pid(s, pid);
2495 int service_set_socket_fd(Service *s, int fd) {
2499 /* This is called by the socket code when instantiating a new
2500 * service for a stream socket and the socket needs to be
2503 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2506 if (s->socket_fd >= 0)
2509 if (s->state != SERVICE_DEAD)
2513 s->got_socket_fd = true;
2517 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2518 [SERVICE_DEAD] = "dead",
2519 [SERVICE_START_PRE] = "start-pre",
2520 [SERVICE_START] = "start",
2521 [SERVICE_START_POST] = "start-post",
2522 [SERVICE_RUNNING] = "running",
2523 [SERVICE_EXITED] = "exited",
2524 [SERVICE_RELOAD] = "reload",
2525 [SERVICE_STOP] = "stop",
2526 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2527 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2528 [SERVICE_STOP_POST] = "stop-post",
2529 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2530 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2531 [SERVICE_MAINTENANCE] = "maintenance",
2532 [SERVICE_AUTO_RESTART] = "auto-restart",
2535 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2537 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2538 [SERVICE_ONCE] = "once",
2539 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2540 [SERVICE_RESTART_ALWAYS] = "restart-always",
2543 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2545 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2546 [SERVICE_FORKING] = "forking",
2547 [SERVICE_SIMPLE] = "simple",
2548 [SERVICE_FINISH] = "finish",
2549 [SERVICE_DBUS] = "dbus",
2550 [SERVICE_NOTIFY] = "notify"
2553 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2555 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2556 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2557 [SERVICE_EXEC_START] = "ExecStart",
2558 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2559 [SERVICE_EXEC_RELOAD] = "ExecReload",
2560 [SERVICE_EXEC_STOP] = "ExecStop",
2561 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2564 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2566 const UnitVTable service_vtable = {
2567 .suffix = ".service",
2569 .init = service_init,
2570 .done = service_done,
2571 .load = service_load,
2573 .coldplug = service_coldplug,
2575 .dump = service_dump,
2577 .start = service_start,
2578 .stop = service_stop,
2579 .reload = service_reload,
2581 .can_reload = service_can_reload,
2583 .serialize = service_serialize,
2584 .deserialize_item = service_deserialize_item,
2586 .active_state = service_active_state,
2587 .sub_state_to_string = service_sub_state_to_string,
2589 .check_gc = service_check_gc,
2590 .check_snapshot = service_check_snapshot,
2592 .sigchld_event = service_sigchld_event,
2593 .timer_event = service_timer_event,
2595 .cgroup_notify_empty = service_cgroup_notify_event,
2596 .notify_message = service_notify_message,
2598 .bus_name_owner_change = service_bus_name_owner_change,
2599 .bus_query_pid_done = service_bus_query_pid_done,
2601 .bus_message_handler = bus_service_message_handler,
2603 .enumerate = service_enumerate