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_MAINTENANCE] = 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 int service_set_main_pid(Service *s, pid_t pid) {
138 s->main_pid_known = true;
143 static int service_set_control_pid(Service *s, pid_t pid) {
152 s->control_pid = pid;
157 static void service_close_socket_fd(Service *s) {
160 if (s->socket_fd < 0)
163 close_nointr_nofail(s->socket_fd);
167 static void service_done(Unit *u) {
168 Service *s = SERVICE(u);
178 free(s->sysv_runlevels);
179 s->sysv_runlevels = NULL;
181 free(s->status_text);
182 s->status_text = NULL;
184 exec_context_done(&s->exec_context);
185 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
186 s->control_command = NULL;
188 /* This will leak a process, but at least no memory or any of
190 service_unwatch_main_pid(s);
191 service_unwatch_control_pid(s);
194 unit_unwatch_bus_name(UNIT(u), s->bus_name);
199 service_close_socket_fd(s);
201 unit_unwatch_timer(u, &s->timer_watch);
204 static char *sysv_translate_name(const char *name) {
207 if (!(r = new(char, strlen(name) + sizeof(".service"))))
210 if (startswith(name, "boot."))
211 /* Drop SuSE-style boot. prefix */
212 strcpy(stpcpy(r, name + 5), ".service");
213 else if (endswith(name, ".sh"))
214 /* Drop Debian-style .sh suffix */
215 strcpy(stpcpy(r, name) - 3, ".service");
217 /* Normal init scripts */
218 strcpy(stpcpy(r, name), ".service");
223 static int sysv_translate_facility(const char *name, char **_r) {
225 static const char * const table[] = {
226 /* LSB defined facilities */
227 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
228 "$network", SPECIAL_NETWORK_TARGET,
229 "$named", SPECIAL_NSS_LOOKUP_TARGET,
230 "$portmap", SPECIAL_RPCBIND_TARGET,
231 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
232 "$syslog", SPECIAL_SYSLOG_TARGET,
233 "$time", SPECIAL_RTC_SET_TARGET,
235 /* Debian extensions */
236 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
237 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
238 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
244 for (i = 0; i < ELEMENTSOF(table); i += 2)
245 if (streq(table[i], name)) {
246 if (!(r = strdup(table[i+1])))
255 if (!(r = sysv_translate_name(name)))
266 static int sysv_fix_order(Service *s) {
272 if (s->sysv_start_priority < 0)
275 /* For each pair of services where at least one lacks a LSB
276 * header, we use the start priority value to order things. */
278 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
282 t = (Service*) other;
287 if (t->sysv_start_priority < 0)
290 /* If both units have modern headers we don't care
291 * about the priorities */
292 if ((!s->sysv_path || s->sysv_has_lsb) &&
293 (!t->sysv_path || t->sysv_has_lsb))
296 if (t->sysv_start_priority < s->sysv_start_priority)
298 else if (t->sysv_start_priority > s->sysv_start_priority)
303 /* FIXME: Maybe we should compare the name here lexicographically? */
305 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
312 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
315 if (!(c = new0(ExecCommand, 1)))
318 if (!(c->path = strdup(path))) {
323 if (!(c->argv = strv_new(path, arg1, NULL))) {
332 static int sysv_exec_commands(Service *s) {
336 assert(s->sysv_path);
338 if (!(c = exec_command_new(s->sysv_path, "start")))
340 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
342 if (!(c = exec_command_new(s->sysv_path, "stop")))
344 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
346 if (!(c = exec_command_new(s->sysv_path, "reload")))
348 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
353 static int service_load_sysv_path(Service *s, const char *path) {
370 if (!(f = fopen(path, "re"))) {
371 r = errno == ENOENT ? 0 : -errno;
376 if (!(s->sysv_path = strdup(path))) {
382 char l[LINE_MAX], *t;
384 if (!fgets(l, sizeof(l), f)) {
389 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
399 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
401 s->sysv_has_lsb = true;
405 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
411 t += strspn(t, WHITESPACE);
413 if (state == NORMAL) {
415 /* Try to parse Red Hat style chkconfig headers */
417 if (startswith_no_case(t, "chkconfig:")) {
419 char runlevels[16], *k;
423 if (sscanf(t+10, "%15s %i %*i",
425 &start_priority) != 2) {
427 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
431 /* A start priority gathered from the
432 * symlink farms is preferred over the
433 * data from the LSB header. */
434 if (start_priority < 0 || start_priority > 99)
435 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
436 else if (s->sysv_start_priority < 0)
437 s->sysv_start_priority = start_priority;
439 char_array_0(runlevels);
440 k = delete_chars(runlevels, WHITESPACE "-");
445 if (!(d = strdup(k))) {
450 free(s->sysv_runlevels);
451 s->sysv_runlevels = d;
454 } else if (startswith_no_case(t, "description:")) {
456 size_t k = strlen(t);
459 if (t[k-1] == '\\') {
464 if (!(d = strdup(strstrip(t+12)))) {
469 free(u->meta.description);
470 u->meta.description = d;
472 } else if (startswith_no_case(t, "pidfile:")) {
479 if (!path_is_absolute(fn)) {
480 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
484 if (!(fn = strdup(fn))) {
493 } else if (state == DESCRIPTION) {
495 /* Try to parse Red Hat style description
498 size_t k = strlen(t);
506 assert(u->meta.description);
507 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
512 free(u->meta.description);
513 u->meta.description = d;
515 } else if (state == LSB || state == LSB_DESCRIPTION) {
517 if (startswith_no_case(t, "Provides:")) {
523 FOREACH_WORD(w, z, t+9, i) {
526 if (!(n = strndup(w, z))) {
531 r = sysv_translate_facility(n, &m);
540 if (unit_name_to_type(m) == UNIT_SERVICE)
541 r = unit_add_name(u, m);
543 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL, true)) >= 0)
544 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
553 } else if (startswith_no_case(t, "Required-Start:") ||
554 startswith_no_case(t, "Should-Start:") ||
555 startswith_no_case(t, "X-Start-Before:") ||
556 startswith_no_case(t, "X-Start-After:")) {
562 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
565 if (!(n = strndup(w, z))) {
570 r = sysv_translate_facility(n, &m);
579 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
585 } else if (startswith_no_case(t, "Default-Start:")) {
590 k = delete_chars(t+14, WHITESPACE "-");
593 if (!(d = strdup(k))) {
598 free(s->sysv_runlevels);
599 s->sysv_runlevels = d;
602 } else if (startswith_no_case(t, "Description:")) {
605 state = LSB_DESCRIPTION;
607 if (!(d = strdup(strstrip(t+12)))) {
612 free(u->meta.description);
613 u->meta.description = d;
615 } else if (startswith_no_case(t, "Short-Description:") &&
616 !u->meta.description) {
619 /* We use the short description only
620 * if no long description is set. */
624 if (!(d = strdup(strstrip(t+18)))) {
629 u->meta.description = d;
631 } else if (startswith_no_case(t, "X-Interactive:")) {
634 if ((b = parse_boolean(strstrip(t+14))) < 0) {
635 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
640 s->exec_context.std_input = EXEC_INPUT_TTY;
642 s->exec_context.std_input = EXEC_INPUT_NULL;
644 } else if (state == LSB_DESCRIPTION) {
646 if (startswith(l, "#\t") || startswith(l, "# ")) {
649 assert(u->meta.description);
650 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
655 free(u->meta.description);
656 u->meta.description = d;
663 if ((r = sysv_exec_commands(s)) < 0)
666 if (!s->sysv_runlevels || chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
667 /* If there a runlevels configured for this service
668 * but none of the standard ones, then we assume this
669 * is some special kind of service (which might be
670 * needed for early boot) and don't create any links
673 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0 ||
674 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
678 /* Don't timeout special services during boot (like fsck) */
681 /* Special setting for all SysV services */
682 s->type = SERVICE_FORKING;
683 s->valid_no_process = true;
684 s->kill_mode = KILL_PROCESS_GROUP;
685 s->restart = SERVICE_ONCE;
687 u->meta.load_state = UNIT_LOADED;
698 static int service_load_sysv_name(Service *s, const char *name) {
704 /* For SysV services we strip the boot. or .sh
705 * prefixes/suffixes. */
706 if (startswith(name, "boot.") ||
707 endswith(name, ".sh.service"))
710 STRV_FOREACH(p, UNIT(s)->meta.manager->lookup_paths.sysvinit_path) {
714 if (asprintf(&path, "%s/%s", *p, name) < 0)
717 assert(endswith(path, ".service"));
718 path[strlen(path)-8] = 0;
720 r = service_load_sysv_path(s, path);
722 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
723 /* Try Debian style xxx.sh source'able init scripts */
725 r = service_load_sysv_path(s, path);
730 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
731 /* Try SUSE style boot.xxx init scripts */
733 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
736 path[strlen(path)-8] = 0;
737 r = service_load_sysv_path(s, path);
744 if ((UNIT(s)->meta.load_state != UNIT_STUB))
751 static int service_load_sysv(Service *s) {
758 /* Load service data from SysV init scripts, preferably with
761 if (strv_isempty(UNIT(s)->meta.manager->lookup_paths.sysvinit_path))
764 if ((t = UNIT(s)->meta.id))
765 if ((r = service_load_sysv_name(s, t)) < 0)
768 if (UNIT(s)->meta.load_state == UNIT_STUB)
769 SET_FOREACH(t, UNIT(s)->meta.names, i) {
770 if (t == UNIT(s)->meta.id)
773 if ((r == service_load_sysv_name(s, t)) < 0)
776 if (UNIT(s)->meta.load_state != UNIT_STUB)
783 static int service_add_bus_name(Service *s) {
790 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
793 r = unit_merge_by_name(UNIT(s), n);
799 static int service_verify(Service *s) {
802 if (UNIT(s)->meta.load_state != UNIT_LOADED)
805 if (!s->exec_command[SERVICE_EXEC_START]) {
806 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
810 if (s->type == SERVICE_DBUS && !s->bus_name) {
811 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->meta.id);
818 static int service_load(Unit *u) {
820 Service *s = SERVICE(u);
824 /* Load a .service file */
825 if ((r = unit_load_fragment(u)) < 0)
828 /* Load a classic init script as a fallback, if we couldn't find anything */
829 if (u->meta.load_state == UNIT_STUB)
830 if ((r = service_load_sysv(s)) < 0)
833 /* Still nothing found? Then let's give up */
834 if (u->meta.load_state == UNIT_STUB)
837 /* We were able to load something, then let's add in the
838 * dropin directories. */
839 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
842 /* This is a new unit? Then let's add in some extras */
843 if (u->meta.load_state == UNIT_LOADED) {
844 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
847 if ((r = unit_add_default_cgroup(u)) < 0)
850 if ((r = sysv_fix_order(s)) < 0)
854 if ((r = service_add_bus_name(s)) < 0)
857 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
862 return service_verify(s);
865 static void service_dump(Unit *u, FILE *f, const char *prefix) {
867 ServiceExecCommand c;
868 Service *s = SERVICE(u);
874 p2 = strappend(prefix, "\t");
875 prefix2 = p2 ? p2 : prefix;
878 "%sService State: %s\n"
879 "%sPermissionsStartOnly: %s\n"
880 "%sRootDirectoryStartOnly: %s\n"
881 "%sValidNoProcess: %s\n"
884 prefix, service_state_to_string(s->state),
885 prefix, yes_no(s->permissions_start_only),
886 prefix, yes_no(s->root_directory_start_only),
887 prefix, yes_no(s->valid_no_process),
888 prefix, kill_mode_to_string(s->kill_mode),
889 prefix, service_type_to_string(s->type));
891 if (s->control_pid > 0)
893 "%sControl PID: %llu\n",
894 prefix, (unsigned long long) s->control_pid);
898 "%sMain PID: %llu\n",
899 prefix, (unsigned long long) s->main_pid);
904 prefix, s->pid_file);
909 "%sBus Name Good: %s\n",
911 prefix, yes_no(s->bus_name_good));
913 exec_context_dump(&s->exec_context, f, prefix);
915 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
917 if (!s->exec_command[c])
920 fprintf(f, "%s-> %s:\n",
921 prefix, service_exec_command_to_string(c));
923 exec_command_dump_list(s->exec_command[c], f, prefix2);
928 "%sSysV Init Script Path: %s\n"
929 "%sSysV Init Script has LSB Header: %s\n",
930 prefix, s->sysv_path,
931 prefix, yes_no(s->sysv_has_lsb));
933 if (s->sysv_start_priority >= 0)
935 "%sSysVStartPriority: %i\n",
936 prefix, s->sysv_start_priority);
938 if (s->sysv_runlevels)
939 fprintf(f, "%sSysVRunLevels: %s\n",
940 prefix, s->sysv_runlevels);
943 fprintf(f, "%sStatus Text: %s\n",
944 prefix, s->status_text);
949 static int service_load_pid_file(Service *s) {
956 if (s->main_pid_known)
959 assert(s->main_pid <= 0);
964 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
967 r = parse_pid(k, &pid);
973 if (kill(pid, 0) < 0 && errno != EPERM) {
974 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
975 (unsigned long) pid, s->pid_file);
979 if ((r = service_set_main_pid(s, pid)) < 0)
982 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
983 /* FIXME: we need to do something here */
989 static int service_get_sockets(Service *s, Set **_set) {
998 /* Collects all Socket objects that belong to this
999 * service. Note that a service might have multiple sockets
1000 * via multiple names. */
1002 if (!(set = set_new(NULL, NULL)))
1005 SET_FOREACH(t, UNIT(s)->meta.names, i) {
1009 /* Look for all socket objects that go by any of our
1010 * units and collect their fds */
1012 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1017 p = manager_get_unit(UNIT(s)->meta.manager, k);
1023 if ((r = set_put(set, p)) < 0)
1035 static int service_notify_sockets_dead(Service *s) {
1043 /* Notifies all our sockets when we die */
1044 if ((r = service_get_sockets(s, &set)) < 0)
1047 SET_FOREACH(sock, set, i)
1048 socket_notify_service_dead(sock);
1055 static void service_set_state(Service *s, ServiceState state) {
1056 ServiceState old_state;
1059 old_state = s->state;
1062 if (state != SERVICE_START_PRE &&
1063 state != SERVICE_START &&
1064 state != SERVICE_START_POST &&
1065 state != SERVICE_RELOAD &&
1066 state != SERVICE_STOP &&
1067 state != SERVICE_STOP_SIGTERM &&
1068 state != SERVICE_STOP_SIGKILL &&
1069 state != SERVICE_STOP_POST &&
1070 state != SERVICE_FINAL_SIGTERM &&
1071 state != SERVICE_FINAL_SIGKILL &&
1072 state != SERVICE_AUTO_RESTART)
1073 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1075 if (state != SERVICE_START &&
1076 state != SERVICE_START_POST &&
1077 state != SERVICE_RUNNING &&
1078 state != SERVICE_RELOAD &&
1079 state != SERVICE_STOP &&
1080 state != SERVICE_STOP_SIGTERM &&
1081 state != SERVICE_STOP_SIGKILL)
1082 service_unwatch_main_pid(s);
1084 if (state != SERVICE_START_PRE &&
1085 state != SERVICE_START &&
1086 state != SERVICE_START_POST &&
1087 state != SERVICE_RELOAD &&
1088 state != SERVICE_STOP &&
1089 state != SERVICE_STOP_SIGTERM &&
1090 state != SERVICE_STOP_SIGKILL &&
1091 state != SERVICE_STOP_POST &&
1092 state != SERVICE_FINAL_SIGTERM &&
1093 state != SERVICE_FINAL_SIGKILL) {
1094 service_unwatch_control_pid(s);
1095 s->control_command = NULL;
1096 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1099 if (state == SERVICE_DEAD ||
1100 state == SERVICE_STOP ||
1101 state == SERVICE_STOP_SIGTERM ||
1102 state == SERVICE_STOP_SIGKILL ||
1103 state == SERVICE_STOP_POST ||
1104 state == SERVICE_FINAL_SIGTERM ||
1105 state == SERVICE_FINAL_SIGKILL ||
1106 state == SERVICE_MAINTENANCE ||
1107 state == SERVICE_AUTO_RESTART)
1108 service_notify_sockets_dead(s);
1110 if (state != SERVICE_START_PRE &&
1111 state != SERVICE_START &&
1112 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1113 service_close_socket_fd(s);
1115 if (old_state != state)
1116 log_debug("%s changed %s -> %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1118 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1121 static int service_coldplug(Unit *u) {
1122 Service *s = SERVICE(u);
1126 assert(s->state == SERVICE_DEAD);
1128 if (s->deserialized_state != s->state) {
1130 if (s->deserialized_state == SERVICE_START_PRE ||
1131 s->deserialized_state == SERVICE_START ||
1132 s->deserialized_state == SERVICE_START_POST ||
1133 s->deserialized_state == SERVICE_RELOAD ||
1134 s->deserialized_state == SERVICE_STOP ||
1135 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1136 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1137 s->deserialized_state == SERVICE_STOP_POST ||
1138 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1139 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1140 s->deserialized_state == SERVICE_AUTO_RESTART) {
1142 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1145 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1147 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1152 if ((s->deserialized_state == SERVICE_START &&
1153 (s->type == SERVICE_FORKING ||
1154 s->type == SERVICE_DBUS ||
1155 s->type == SERVICE_FINISH ||
1156 s->type == SERVICE_NOTIFY)) ||
1157 s->deserialized_state == SERVICE_START_POST ||
1158 s->deserialized_state == SERVICE_RUNNING ||
1159 s->deserialized_state == SERVICE_RELOAD ||
1160 s->deserialized_state == SERVICE_STOP ||
1161 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1162 s->deserialized_state == SERVICE_STOP_SIGKILL)
1163 if (s->main_pid > 0)
1164 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1167 if (s->deserialized_state == SERVICE_START_PRE ||
1168 s->deserialized_state == SERVICE_START ||
1169 s->deserialized_state == SERVICE_START_POST ||
1170 s->deserialized_state == SERVICE_RELOAD ||
1171 s->deserialized_state == SERVICE_STOP ||
1172 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1173 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1174 s->deserialized_state == SERVICE_STOP_POST ||
1175 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1176 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1177 if (s->control_pid > 0)
1178 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1181 service_set_state(s, s->deserialized_state);
1187 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1191 unsigned rn_fds = 0;
1199 if ((r = service_get_sockets(s, &set)) < 0)
1202 SET_FOREACH(sock, set, i) {
1206 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1218 if (!(t = new(int, rn_fds+cn_fds))) {
1224 memcpy(t, rfds, rn_fds);
1225 memcpy(t+rn_fds, cfds, cn_fds);
1230 rn_fds = rn_fds+cn_fds;
1248 static int service_spawn(
1253 bool apply_permissions,
1268 if (s->socket_fd >= 0) {
1269 fds = &s->socket_fd;
1271 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1275 if (timeout && s->timeout_usec) {
1276 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1279 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1281 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1290 s->meta.manager->environment,
1293 UNIT(s)->meta.manager->confirm_spawn,
1294 UNIT(s)->meta.cgroup_bondings,
1302 if (s->socket_fd >= 0)
1303 service_close_socket_fd(s);
1308 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1309 /* FIXME: we need to do something here */
1320 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1325 static int main_pid_good(Service *s) {
1328 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1331 /* If we know the pid file, then lets just check if it is
1333 if (s->main_pid_known)
1334 return s->main_pid > 0;
1336 /* We don't know the pid */
1340 static int control_pid_good(Service *s) {
1343 return s->control_pid > 0;
1346 static int cgroup_good(Service *s) {
1351 if (s->valid_no_process)
1354 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1360 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1367 if (allow_restart &&
1369 (s->restart == SERVICE_RESTART_ALWAYS ||
1370 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1372 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1375 service_set_state(s, SERVICE_AUTO_RESTART);
1377 service_set_state(s, s->failure ? SERVICE_MAINTENANCE : SERVICE_DEAD);
1382 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
1383 service_enter_dead(s, false, false);
1386 static void service_enter_signal(Service *s, ServiceState state, bool success);
1388 static void service_enter_stop_post(Service *s, bool success) {
1395 service_unwatch_control_pid(s);
1397 s->control_command_id = SERVICE_EXEC_STOP_POST;
1398 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1399 if ((r = service_spawn(s,
1403 !s->permissions_start_only,
1404 !s->root_directory_start_only,
1405 &s->control_pid)) < 0)
1409 service_set_state(s, SERVICE_STOP_POST);
1411 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1416 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->meta.id, strerror(-r));
1417 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1420 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1429 if (s->kill_mode != KILL_NONE) {
1430 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1432 if (s->kill_mode == KILL_CONTROL_GROUP) {
1434 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1435 if (r != -EAGAIN && r != -ESRCH)
1444 if (s->main_pid > 0) {
1445 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1451 if (s->control_pid > 0) {
1452 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1463 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1464 if (s->timeout_usec > 0)
1465 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1468 service_set_state(s, state);
1469 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1470 service_enter_stop_post(s, true);
1472 service_enter_dead(s, true, true);
1477 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
1479 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1480 service_enter_stop_post(s, false);
1482 service_enter_dead(s, false, true);
1485 static void service_enter_stop(Service *s, bool success) {
1494 service_unwatch_control_pid(s);
1496 s->control_command_id = SERVICE_EXEC_STOP;
1497 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1498 if ((r = service_spawn(s,
1502 !s->permissions_start_only,
1503 !s->root_directory_start_only,
1507 service_set_control_pid(s, pid);
1508 service_set_state(s, SERVICE_STOP);
1510 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1515 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->meta.id, strerror(-r));
1516 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1519 static void service_enter_running(Service *s, bool success) {
1525 if (main_pid_good(s) != 0 &&
1526 cgroup_good(s) != 0 &&
1527 (s->bus_name_good || s->type != SERVICE_DBUS))
1528 service_set_state(s, SERVICE_RUNNING);
1529 else if (s->valid_no_process)
1530 service_set_state(s, SERVICE_EXITED);
1532 service_enter_stop(s, true);
1535 static void service_enter_start_post(Service *s) {
1540 service_unwatch_control_pid(s);
1542 s->control_command_id = SERVICE_EXEC_START_POST;
1543 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1544 if ((r = service_spawn(s,
1548 !s->permissions_start_only,
1549 !s->root_directory_start_only,
1553 service_set_control_pid(s, pid);
1554 service_set_state(s, SERVICE_START_POST);
1556 service_enter_running(s, true);
1561 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->meta.id, strerror(-r));
1562 service_enter_stop(s, false);
1565 static void service_enter_start(Service *s) {
1571 assert(s->exec_command[SERVICE_EXEC_START]);
1572 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1574 if (s->type == SERVICE_FORKING)
1575 service_unwatch_control_pid(s);
1577 service_unwatch_main_pid(s);
1579 if ((r = service_spawn(s,
1580 s->exec_command[SERVICE_EXEC_START],
1581 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1588 if (s->type == SERVICE_SIMPLE) {
1589 /* For simple services we immediately start
1590 * the START_POST binaries. */
1592 service_set_main_pid(s, pid);
1593 service_enter_start_post(s);
1595 } else if (s->type == SERVICE_FORKING) {
1597 /* For forking services we wait until the start
1598 * process exited. */
1600 s->control_command_id = SERVICE_EXEC_START;
1601 s->control_command = s->exec_command[SERVICE_EXEC_START];
1603 service_set_control_pid(s, pid);
1604 service_set_state(s, SERVICE_START);
1606 } else if (s->type == SERVICE_FINISH ||
1607 s->type == SERVICE_DBUS ||
1608 s->type == SERVICE_NOTIFY) {
1610 /* For finishing services we wait until the start
1611 * process exited, too, but it is our main process. */
1613 /* For D-Bus services we know the main pid right away,
1614 * but wait for the bus name to appear on the
1615 * bus. Notify services are similar. */
1617 service_set_main_pid(s, pid);
1618 service_set_state(s, SERVICE_START);
1620 assert_not_reached("Unknown service type");
1625 log_warning("%s failed to run 'start' task: %s", UNIT(s)->meta.id, strerror(-r));
1626 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1629 static void service_enter_start_pre(Service *s) {
1635 service_unwatch_control_pid(s);
1637 s->control_command_id = SERVICE_EXEC_START_PRE;
1638 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1639 if ((r = service_spawn(s,
1643 !s->permissions_start_only,
1644 !s->root_directory_start_only,
1648 service_set_control_pid(s, pid);
1649 service_set_state(s, SERVICE_START_PRE);
1651 service_enter_start(s);
1656 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->meta.id, strerror(-r));
1657 service_enter_dead(s, false, true);
1660 static void service_enter_restart(Service *s) {
1664 service_enter_dead(s, true, false);
1666 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1669 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
1674 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
1675 service_enter_dead(s, false, false);
1678 static void service_enter_reload(Service *s) {
1684 service_unwatch_control_pid(s);
1686 s->control_command_id = SERVICE_EXEC_RELOAD;
1687 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1688 if ((r = service_spawn(s,
1692 !s->permissions_start_only,
1693 !s->root_directory_start_only,
1697 service_set_control_pid(s, pid);
1698 service_set_state(s, SERVICE_RELOAD);
1700 service_enter_running(s, true);
1705 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->meta.id, strerror(-r));
1706 service_enter_stop(s, false);
1709 static void service_run_next(Service *s, bool success) {
1714 assert(s->control_command);
1715 assert(s->control_command->command_next);
1720 s->control_command = s->control_command->command_next;
1722 service_unwatch_control_pid(s);
1724 if ((r = service_spawn(s,
1728 !s->permissions_start_only,
1729 !s->root_directory_start_only,
1733 service_set_control_pid(s, pid);
1737 log_warning("%s failed to run spawn next task: %s", UNIT(s)->meta.id, strerror(-r));
1739 if (s->state == SERVICE_START_PRE)
1740 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1741 else if (s->state == SERVICE_STOP)
1742 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1743 else if (s->state == SERVICE_STOP_POST)
1744 service_enter_dead(s, false, true);
1746 service_enter_stop(s, false);
1749 static int service_start(Unit *u) {
1750 Service *s = SERVICE(u);
1754 /* We cannot fulfill this request right now, try again later
1756 if (s->state == SERVICE_STOP ||
1757 s->state == SERVICE_STOP_SIGTERM ||
1758 s->state == SERVICE_STOP_SIGKILL ||
1759 s->state == SERVICE_STOP_POST ||
1760 s->state == SERVICE_FINAL_SIGTERM ||
1761 s->state == SERVICE_FINAL_SIGKILL)
1764 /* Already on it! */
1765 if (s->state == SERVICE_START_PRE ||
1766 s->state == SERVICE_START ||
1767 s->state == SERVICE_START_POST)
1770 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTENANCE || s->state == SERVICE_AUTO_RESTART);
1772 /* Make sure we don't enter a busy loop of some kind. */
1773 if (!ratelimit_test(&s->ratelimit)) {
1774 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1779 s->main_pid_known = false;
1780 s->allow_restart = true;
1782 service_enter_start_pre(s);
1786 static int service_stop(Unit *u) {
1787 Service *s = SERVICE(u);
1791 /* Cannot do this now */
1792 if (s->state == SERVICE_START_PRE ||
1793 s->state == SERVICE_START ||
1794 s->state == SERVICE_START_POST ||
1795 s->state == SERVICE_RELOAD)
1799 if (s->state == SERVICE_STOP ||
1800 s->state == SERVICE_STOP_SIGTERM ||
1801 s->state == SERVICE_STOP_SIGKILL ||
1802 s->state == SERVICE_STOP_POST ||
1803 s->state == SERVICE_FINAL_SIGTERM ||
1804 s->state == SERVICE_FINAL_SIGKILL)
1807 if (s->state == SERVICE_AUTO_RESTART) {
1808 service_set_state(s, SERVICE_DEAD);
1812 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1814 /* This is a user request, so don't do restarts on this
1816 s->allow_restart = false;
1818 service_enter_stop(s, true);
1822 static int service_reload(Unit *u) {
1823 Service *s = SERVICE(u);
1827 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1829 service_enter_reload(s);
1833 static bool service_can_reload(Unit *u) {
1834 Service *s = SERVICE(u);
1838 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1841 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1842 Service *s = SERVICE(u);
1848 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1849 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1851 if (s->control_pid > 0)
1852 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
1854 if (s->main_pid_known && s->main_pid > 0)
1855 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
1857 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1859 /* There's a minor uncleanliness here: if there are multiple
1860 * commands attached here, we will start from the first one
1862 if (s->control_command_id >= 0)
1863 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
1865 if (s->socket_fd >= 0) {
1868 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1871 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1877 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1878 Service *s = SERVICE(u);
1886 if (streq(key, "state")) {
1889 if ((state = service_state_from_string(value)) < 0)
1890 log_debug("Failed to parse state value %s", value);
1892 s->deserialized_state = state;
1893 } else if (streq(key, "failure")) {
1896 if ((b = parse_boolean(value)) < 0)
1897 log_debug("Failed to parse failure value %s", value);
1899 s->failure = b || s->failure;
1900 } else if (streq(key, "control-pid")) {
1903 if ((r = parse_pid(value, &pid)) < 0)
1904 log_debug("Failed to parse control-pid value %s", value);
1906 service_set_control_pid(s, pid);
1907 } else if (streq(key, "main-pid")) {
1910 if ((r = parse_pid(value, &pid)) < 0)
1911 log_debug("Failed to parse main-pid value %s", value);
1913 service_set_main_pid(s, (pid_t) pid);
1914 } else if (streq(key, "main-pid-known")) {
1917 if ((b = parse_boolean(value)) < 0)
1918 log_debug("Failed to parse main-pid-known value %s", value);
1920 s->main_pid_known = b;
1921 } else if (streq(key, "control-command")) {
1922 ServiceExecCommand id;
1924 if ((id = service_exec_command_from_string(value)) < 0)
1925 log_debug("Failed to parse exec-command value %s", value);
1927 s->control_command_id = id;
1928 s->control_command = s->exec_command[id];
1930 } else if (streq(key, "socket-fd")) {
1933 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1934 log_debug("Failed to parse socket-fd value %s", value);
1937 if (s->socket_fd >= 0)
1938 close_nointr_nofail(s->socket_fd);
1939 s->socket_fd = fdset_remove(fds, fd);
1942 log_debug("Unknown serialization key '%s'", key);
1947 static UnitActiveState service_active_state(Unit *u) {
1950 return state_translation_table[SERVICE(u)->state];
1953 static const char *service_sub_state_to_string(Unit *u) {
1956 return service_state_to_string(SERVICE(u)->state);
1959 static bool service_check_gc(Unit *u) {
1960 Service *s = SERVICE(u);
1964 return !!s->sysv_path;
1967 static bool service_check_snapshot(Unit *u) {
1968 Service *s = SERVICE(u);
1972 return !s->got_socket_fd;
1975 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1976 Service *s = SERVICE(u);
1982 success = is_clean_exit(code, status);
1983 s->failure = s->failure || !success;
1985 if (s->main_pid == pid) {
1987 exec_status_fill(&s->main_exec_status, pid, code, status);
1990 if (s->type != SERVICE_FORKING) {
1991 assert(s->exec_command[SERVICE_EXEC_START]);
1992 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1995 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
1997 /* The service exited, so the service is officially
2002 case SERVICE_START_POST:
2003 case SERVICE_RELOAD:
2005 /* Need to wait until the operation is
2010 if (s->type == SERVICE_FINISH) {
2011 /* This was our main goal, so let's go on */
2013 service_enter_start_post(s);
2015 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2018 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2023 case SERVICE_RUNNING:
2024 service_enter_running(s, success);
2027 case SERVICE_STOP_SIGTERM:
2028 case SERVICE_STOP_SIGKILL:
2030 if (!control_pid_good(s))
2031 service_enter_stop_post(s, success);
2033 /* If there is still a control process, wait for that first */
2037 assert_not_reached("Uh, main process died at wrong time.");
2040 } else if (s->control_pid == pid) {
2042 if (s->control_command)
2043 exec_status_fill(&s->control_command->exec_status, pid, code, status);
2047 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2049 /* If we are shutting things down anyway we
2050 * don't care about failing commands. */
2052 if (s->control_command && s->control_command->command_next && success) {
2054 /* There is another command to *
2055 * execute, so let's do that. */
2057 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
2058 service_run_next(s, success);
2061 /* No further commands for this step, so let's
2062 * figure out what to do next */
2064 s->control_command = NULL;
2065 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2067 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2071 case SERVICE_START_PRE:
2073 service_enter_start(s);
2075 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2079 assert(s->type == SERVICE_FORKING);
2081 /* Let's try to load the pid
2082 * file here if we can. We
2083 * ignore the return value,
2084 * since the PID file might
2085 * actually be created by a
2086 * START_POST script */
2090 service_load_pid_file(s);
2092 service_enter_start_post(s);
2094 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2098 case SERVICE_START_POST:
2099 if (success && s->pid_file && !s->main_pid_known) {
2102 /* Hmm, let's see if we can
2103 * load the pid now after the
2104 * start-post scripts got
2107 if ((r = service_load_pid_file(s)) < 0)
2108 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
2113 case SERVICE_RELOAD:
2115 service_enter_running(s, true);
2117 service_enter_stop(s, false);
2122 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2125 case SERVICE_STOP_SIGTERM:
2126 case SERVICE_STOP_SIGKILL:
2127 if (main_pid_good(s) <= 0)
2128 service_enter_stop_post(s, success);
2130 /* If there is still a service
2131 * process around, wait until
2132 * that one quit, too */
2135 case SERVICE_STOP_POST:
2136 case SERVICE_FINAL_SIGTERM:
2137 case SERVICE_FINAL_SIGKILL:
2138 service_enter_dead(s, success, true);
2142 assert_not_reached("Uh, control process died at wrong time.");
2148 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2149 Service *s = SERVICE(u);
2152 assert(elapsed == 1);
2154 assert(w == &s->timer_watch);
2158 case SERVICE_START_PRE:
2160 log_warning("%s operation timed out. Terminating.", u->meta.id);
2161 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2164 case SERVICE_START_POST:
2165 case SERVICE_RELOAD:
2166 log_warning("%s operation timed out. Stopping.", u->meta.id);
2167 service_enter_stop(s, false);
2171 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2172 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2175 case SERVICE_STOP_SIGTERM:
2176 log_warning("%s stopping timed out. Killing.", u->meta.id);
2177 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2180 case SERVICE_STOP_SIGKILL:
2181 /* Uh, wie sent a SIGKILL and it is still not gone?
2182 * Must be something we cannot kill, so let's just be
2183 * weirded out and continue */
2185 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2186 service_enter_stop_post(s, false);
2189 case SERVICE_STOP_POST:
2190 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2191 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2194 case SERVICE_FINAL_SIGTERM:
2195 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2196 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2199 case SERVICE_FINAL_SIGKILL:
2200 log_warning("%s still around after SIGKILL (2). Entering maintenance mode.", u->meta.id);
2201 service_enter_dead(s, false, true);
2204 case SERVICE_AUTO_RESTART:
2205 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
2206 service_enter_restart(s);
2210 assert_not_reached("Timeout at wrong time.");
2214 static void service_cgroup_notify_event(Unit *u) {
2215 Service *s = SERVICE(u);
2219 log_debug("%s: cgroup is empty", u->meta.id);
2223 /* Waiting for SIGCHLD is usually more interesting,
2224 * because it includes return codes/signals. Which is
2225 * why we ignore the cgroup events for most cases,
2226 * except when we don't know pid which to expect the
2229 case SERVICE_RUNNING:
2230 service_enter_running(s, true);
2238 static void service_notify_message(Unit *u, char **tags) {
2239 Service *s = SERVICE(u);
2244 log_debug("%s: Got message", u->meta.id);
2246 /* Interpret MAINPID= */
2247 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2248 (s->state == SERVICE_START ||
2249 s->state == SERVICE_START_POST ||
2250 s->state == SERVICE_RUNNING ||
2251 s->state == SERVICE_RELOAD)) {
2254 if (parse_pid(e + 8, &pid) < 0)
2255 log_warning("Failed to parse %s", e);
2257 log_debug("%s: got %s", u->meta.id, e);
2258 service_set_main_pid(s, pid);
2262 /* Interpret READY= */
2263 if (s->type == SERVICE_NOTIFY &&
2264 s->state == SERVICE_START &&
2265 strv_find(tags, "READY=1")) {
2266 log_debug("%s: got READY=1", u->meta.id);
2268 service_enter_start_post(s);
2271 /* Interpret STATUS= */
2272 if ((e = strv_find_prefix(tags, "STATUS="))) {
2275 if (!(t = strdup(e+7))) {
2276 log_error("Failed to allocate string.");
2280 log_debug("%s: got %s", u->meta.id, e);
2282 free(s->status_text);
2287 static int service_enumerate(Manager *m) {
2291 char *path = NULL, *fpath = NULL, *name = NULL;
2296 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2297 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2302 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2310 if (!(d = opendir(path))) {
2311 if (errno != ENOENT)
2312 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2317 while ((de = readdir(d))) {
2321 if (ignore_file(de->d_name))
2324 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2327 if (strlen(de->d_name) < 4)
2330 a = undecchar(de->d_name[1]);
2331 b = undecchar(de->d_name[2]);
2338 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2343 if (access(fpath, X_OK) < 0) {
2345 if (errno != ENOENT)
2346 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2352 if (!(name = sysv_translate_name(de->d_name + 3))) {
2357 if ((r = manager_load_unit_prepare(m, name, NULL, &service)) < 0) {
2358 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2362 if (de->d_name[0] == 'S' &&
2363 (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT))
2364 SERVICE(service)->sysv_start_priority =
2365 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2367 manager_dispatch_load_queue(m);
2368 service = unit_follow_merge(service);
2370 if (de->d_name[0] == 'S') {
2371 Unit *runlevel_target;
2373 if ((r = manager_load_unit(m, rcnd_table[i].target, NULL, &runlevel_target)) < 0)
2376 if ((r = unit_add_dependency(runlevel_target, UNIT_WANTS, service, true)) < 0)
2379 if ((r = unit_add_dependency(service, UNIT_BEFORE, runlevel_target, true)) < 0)
2382 } else if (de->d_name[0] == 'K' &&
2383 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2384 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2385 Unit *shutdown_target;
2387 /* We honour K links only for
2388 * halt/reboot. For the normal
2389 * runlevels we assume the
2391 * implicitly added by the
2392 * core logic. Also, we don't
2393 * really distuingish here
2394 * between the runlevels 0 and
2395 * 6 and just add them to the
2396 * special shutdown target. On
2397 * SUSE the boot.d/ runlevel
2398 * is also used for shutdown,
2399 * so we add links for that
2400 * too to the shutdown
2403 if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
2406 if ((r = unit_add_dependency(service, UNIT_CONFLICTS, shutdown_target, true)) < 0)
2409 if ((r = unit_add_dependency(service, UNIT_BEFORE, shutdown_target, true)) < 0)
2428 static void service_bus_name_owner_change(
2431 const char *old_owner,
2432 const char *new_owner) {
2434 Service *s = SERVICE(u);
2439 assert(streq(s->bus_name, name));
2440 assert(old_owner || new_owner);
2442 if (old_owner && new_owner)
2443 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2445 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2447 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2449 s->bus_name_good = !!new_owner;
2451 if (s->type == SERVICE_DBUS) {
2453 /* service_enter_running() will figure out what to
2455 if (s->state == SERVICE_RUNNING)
2456 service_enter_running(s, true);
2457 else if (s->state == SERVICE_START && new_owner)
2458 service_enter_start_post(s);
2460 } else if (new_owner &&
2462 (s->state == SERVICE_START ||
2463 s->state == SERVICE_START_POST ||
2464 s->state == SERVICE_RUNNING ||
2465 s->state == SERVICE_RELOAD)) {
2467 /* Try to acquire PID from bus service */
2468 log_debug("Trying to acquire PID from D-Bus name...");
2470 bus_query_pid(u->meta.manager, name);
2474 static void service_bus_query_pid_done(
2479 Service *s = SERVICE(u);
2484 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2486 if (s->main_pid <= 0 &&
2487 (s->state == SERVICE_START ||
2488 s->state == SERVICE_START_POST ||
2489 s->state == SERVICE_RUNNING ||
2490 s->state == SERVICE_RELOAD))
2491 service_set_main_pid(s, pid);
2494 int service_set_socket_fd(Service *s, int fd) {
2498 /* This is called by the socket code when instantiating a new
2499 * service for a stream socket and the socket needs to be
2502 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2505 if (s->socket_fd >= 0)
2508 if (s->state != SERVICE_DEAD)
2512 s->got_socket_fd = true;
2516 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2517 [SERVICE_DEAD] = "dead",
2518 [SERVICE_START_PRE] = "start-pre",
2519 [SERVICE_START] = "start",
2520 [SERVICE_START_POST] = "start-post",
2521 [SERVICE_RUNNING] = "running",
2522 [SERVICE_EXITED] = "exited",
2523 [SERVICE_RELOAD] = "reload",
2524 [SERVICE_STOP] = "stop",
2525 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2526 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2527 [SERVICE_STOP_POST] = "stop-post",
2528 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2529 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2530 [SERVICE_MAINTENANCE] = "maintenance",
2531 [SERVICE_AUTO_RESTART] = "auto-restart",
2534 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2536 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2537 [SERVICE_ONCE] = "once",
2538 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2539 [SERVICE_RESTART_ALWAYS] = "restart-always",
2542 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2544 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2545 [SERVICE_FORKING] = "forking",
2546 [SERVICE_SIMPLE] = "simple",
2547 [SERVICE_FINISH] = "finish",
2548 [SERVICE_DBUS] = "dbus",
2549 [SERVICE_NOTIFY] = "notify"
2552 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2554 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2555 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2556 [SERVICE_EXEC_START] = "ExecStart",
2557 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2558 [SERVICE_EXEC_RELOAD] = "ExecReload",
2559 [SERVICE_EXEC_STOP] = "ExecStop",
2560 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2563 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2565 const UnitVTable service_vtable = {
2566 .suffix = ".service",
2568 .init = service_init,
2569 .done = service_done,
2570 .load = service_load,
2572 .coldplug = service_coldplug,
2574 .dump = service_dump,
2576 .start = service_start,
2577 .stop = service_stop,
2578 .reload = service_reload,
2580 .can_reload = service_can_reload,
2582 .serialize = service_serialize,
2583 .deserialize_item = service_deserialize_item,
2585 .active_state = service_active_state,
2586 .sub_state_to_string = service_sub_state_to_string,
2588 .check_gc = service_check_gc,
2589 .check_snapshot = service_check_snapshot,
2591 .sigchld_event = service_sigchld_event,
2592 .timer_event = service_timer_event,
2594 .cgroup_notify_empty = service_cgroup_notify_event,
2595 .notify_message = service_notify_message,
2597 .bus_name_owner_change = service_bus_name_owner_change,
2598 .bus_query_pid_done = service_bus_query_pid_done,
2600 .bus_message_handler = bus_service_message_handler,
2602 .enumerate = service_enumerate