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_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_MAINTENANCE,
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) {
140 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid())
141 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
142 s->meta.id, (unsigned long) pid);
145 s->main_pid_known = true;
150 static void service_close_socket_fd(Service *s) {
153 if (s->socket_fd < 0)
156 close_nointr_nofail(s->socket_fd);
160 static void service_connection_unref(Service *s) {
166 socket_connection_unref(s->socket);
170 static void service_done(Unit *u) {
171 Service *s = SERVICE(u);
181 free(s->sysv_runlevels);
182 s->sysv_runlevels = NULL;
184 free(s->status_text);
185 s->status_text = NULL;
187 exec_context_done(&s->exec_context);
188 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
189 s->control_command = NULL;
191 /* This will leak a process, but at least no memory or any of
193 service_unwatch_main_pid(s);
194 service_unwatch_control_pid(s);
197 unit_unwatch_bus_name(UNIT(u), s->bus_name);
202 service_close_socket_fd(s);
203 service_connection_unref(s);
205 unit_unwatch_timer(u, &s->timer_watch);
208 static char *sysv_translate_name(const char *name) {
211 if (!(r = new(char, strlen(name) + sizeof(".service"))))
214 if (startswith(name, "boot."))
215 /* Drop SuSE-style boot. prefix */
216 strcpy(stpcpy(r, name + 5), ".service");
217 else if (endswith(name, ".sh"))
218 /* Drop Debian-style .sh suffix */
219 strcpy(stpcpy(r, name) - 3, ".service");
221 /* Normal init scripts */
222 strcpy(stpcpy(r, name), ".service");
227 static int sysv_translate_facility(const char *name, char **_r) {
229 static const char * const table[] = {
230 /* LSB defined facilities */
231 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
232 "$network", SPECIAL_NETWORK_TARGET,
233 "$named", SPECIAL_NSS_LOOKUP_TARGET,
234 "$portmap", SPECIAL_RPCBIND_TARGET,
235 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
236 "$syslog", SPECIAL_SYSLOG_TARGET,
237 "$time", SPECIAL_RTC_SET_TARGET,
239 /* Debian extensions */
240 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
241 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
242 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
248 for (i = 0; i < ELEMENTSOF(table); i += 2)
249 if (streq(table[i], name)) {
250 if (!(r = strdup(table[i+1])))
259 if (!(r = sysv_translate_name(name)))
270 static int sysv_fix_order(Service *s) {
276 if (s->sysv_start_priority < 0)
279 /* For each pair of services where at least one lacks a LSB
280 * header, we use the start priority value to order things. */
282 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) {
286 t = (Service*) other;
291 if (t->sysv_start_priority < 0)
294 /* If both units have modern headers we don't care
295 * about the priorities */
296 if ((!s->sysv_path || s->sysv_has_lsb) &&
297 (!t->sysv_path || t->sysv_has_lsb))
300 if (t->sysv_start_priority < s->sysv_start_priority)
302 else if (t->sysv_start_priority > s->sysv_start_priority)
307 /* FIXME: Maybe we should compare the name here lexicographically? */
309 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
316 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
319 if (!(c = new0(ExecCommand, 1)))
322 if (!(c->path = strdup(path))) {
327 if (!(c->argv = strv_new(path, arg1, NULL))) {
336 static int sysv_exec_commands(Service *s) {
340 assert(s->sysv_path);
342 if (!(c = exec_command_new(s->sysv_path, "start")))
344 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
346 if (!(c = exec_command_new(s->sysv_path, "stop")))
348 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
350 if (!(c = exec_command_new(s->sysv_path, "reload")))
352 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
357 static int service_load_sysv_path(Service *s, const char *path) {
374 if (!(f = fopen(path, "re"))) {
375 r = errno == ENOENT ? 0 : -errno;
380 if (!(s->sysv_path = strdup(path))) {
386 char l[LINE_MAX], *t;
388 if (!fgets(l, sizeof(l), f)) {
393 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
403 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
405 s->sysv_has_lsb = true;
409 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
415 t += strspn(t, WHITESPACE);
417 if (state == NORMAL) {
419 /* Try to parse Red Hat style chkconfig headers */
421 if (startswith_no_case(t, "chkconfig:")) {
423 char runlevels[16], *k;
427 if (sscanf(t+10, "%15s %i %*i",
429 &start_priority) != 2) {
431 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
435 /* A start priority gathered from the
436 * symlink farms is preferred over the
437 * data from the LSB header. */
438 if (start_priority < 0 || start_priority > 99)
439 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
440 else if (s->sysv_start_priority < 0)
441 s->sysv_start_priority = start_priority;
443 char_array_0(runlevels);
444 k = delete_chars(runlevels, WHITESPACE "-");
449 if (!(d = strdup(k))) {
454 free(s->sysv_runlevels);
455 s->sysv_runlevels = d;
458 } else if (startswith_no_case(t, "description:")) {
460 size_t k = strlen(t);
463 if (t[k-1] == '\\') {
468 if (!(d = strdup(strstrip(t+12)))) {
473 free(u->meta.description);
474 u->meta.description = d;
476 } else if (startswith_no_case(t, "pidfile:")) {
483 if (!path_is_absolute(fn)) {
484 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
488 if (!(fn = strdup(fn))) {
497 } else if (state == DESCRIPTION) {
499 /* Try to parse Red Hat style description
502 size_t k = strlen(t);
510 assert(u->meta.description);
511 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
516 free(u->meta.description);
517 u->meta.description = d;
519 } else if (state == LSB || state == LSB_DESCRIPTION) {
521 if (startswith_no_case(t, "Provides:")) {
527 FOREACH_WORD(w, z, t+9, i) {
530 if (!(n = strndup(w, z))) {
535 r = sysv_translate_facility(n, &m);
544 if (unit_name_to_type(m) == UNIT_SERVICE)
545 r = unit_add_name(u, m);
547 r = unit_add_two_dependencies_by_name_inverse(u, UNIT_AFTER, UNIT_REQUIRES, m, NULL, true);
555 } else if (startswith_no_case(t, "Required-Start:") ||
556 startswith_no_case(t, "Should-Start:") ||
557 startswith_no_case(t, "X-Start-Before:") ||
558 startswith_no_case(t, "X-Start-After:")) {
564 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
567 if (!(n = strndup(w, z))) {
572 r = sysv_translate_facility(n, &m);
581 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
587 } else if (startswith_no_case(t, "Default-Start:")) {
592 k = delete_chars(t+14, WHITESPACE "-");
595 if (!(d = strdup(k))) {
600 free(s->sysv_runlevels);
601 s->sysv_runlevels = d;
604 } else if (startswith_no_case(t, "Description:")) {
607 state = LSB_DESCRIPTION;
609 if (!(d = strdup(strstrip(t+12)))) {
614 free(u->meta.description);
615 u->meta.description = d;
617 } else if (startswith_no_case(t, "Short-Description:") &&
618 !u->meta.description) {
621 /* We use the short description only
622 * if no long description is set. */
626 if (!(d = strdup(strstrip(t+18)))) {
631 u->meta.description = d;
633 } else if (startswith_no_case(t, "X-Interactive:")) {
636 if ((b = parse_boolean(strstrip(t+14))) < 0) {
637 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
642 s->exec_context.std_input = EXEC_INPUT_TTY;
644 s->exec_context.std_input = EXEC_INPUT_NULL;
646 } else if (state == LSB_DESCRIPTION) {
648 if (startswith(l, "#\t") || startswith(l, "# ")) {
651 assert(u->meta.description);
652 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
657 free(u->meta.description);
658 u->meta.description = d;
665 if ((r = sysv_exec_commands(s)) < 0)
668 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
669 /* If there a runlevels configured for this service
670 * but none of the standard ones, then we assume this
671 * is some special kind of service (which might be
672 * needed for early boot) and don't create any links
675 s->meta.default_dependencies = false;
677 /* 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, 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 && 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 && 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 ((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(s->meta.manager->lookup_paths.sysvinit_path))
764 if ((t = s->meta.id))
765 if ((r = service_load_sysv_name(s, t)) < 0)
768 if (s->meta.load_state == UNIT_STUB)
769 SET_FOREACH(t, s->meta.names, i) {
773 if ((r == service_load_sysv_name(s, t)) < 0)
776 if (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 (s->meta.load_state != UNIT_LOADED)
805 if (!s->exec_command[SERVICE_EXEC_START]) {
806 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
810 if (s->exec_command[SERVICE_EXEC_START]->command_next) {
811 log_error("%s has more than one ExecStart setting. Refusing.", s->meta.id);
815 if (s->type == SERVICE_DBUS && !s->bus_name) {
816 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
820 if (s->exec_context.pam_name && s->kill_mode != KILL_CONTROL_GROUP) {
821 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
828 static int service_add_default_dependencies(Service *s) {
833 /* Add a number of automatic dependencies useful for the
834 * majority of services. */
836 /* First, pull in base system */
837 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
839 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
842 } else if (s->meta.manager->running_as == MANAGER_SESSION) {
844 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
848 /* Second, activate normal shutdown */
849 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
852 static int service_load(Unit *u) {
854 Service *s = SERVICE(u);
858 /* Load a .service file */
859 if ((r = unit_load_fragment(u)) < 0)
862 /* Load a classic init script as a fallback, if we couldn't find anything */
863 if (u->meta.load_state == UNIT_STUB)
864 if ((r = service_load_sysv(s)) < 0)
867 /* Still nothing found? Then let's give up */
868 if (u->meta.load_state == UNIT_STUB)
871 /* We were able to load something, then let's add in the
872 * dropin directories. */
873 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
876 /* This is a new unit? Then let's add in some extras */
877 if (u->meta.load_state == UNIT_LOADED) {
878 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
881 if ((r = unit_add_default_cgroup(u)) < 0)
884 if ((r = sysv_fix_order(s)) < 0)
888 if ((r = service_add_bus_name(s)) < 0)
891 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
895 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
896 s->notify_access = NOTIFY_MAIN;
898 if (s->type == SERVICE_DBUS || s->bus_name)
899 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0)
902 if (s->meta.default_dependencies)
903 if ((r = service_add_default_dependencies(s)) < 0)
907 return service_verify(s);
910 static void service_dump(Unit *u, FILE *f, const char *prefix) {
912 ServiceExecCommand c;
913 Service *s = SERVICE(u);
919 p2 = strappend(prefix, "\t");
920 prefix2 = p2 ? p2 : prefix;
923 "%sService State: %s\n"
924 "%sPermissionsStartOnly: %s\n"
925 "%sRootDirectoryStartOnly: %s\n"
926 "%sValidNoProcess: %s\n"
929 "%sNotifyAccess: %s\n",
930 prefix, service_state_to_string(s->state),
931 prefix, yes_no(s->permissions_start_only),
932 prefix, yes_no(s->root_directory_start_only),
933 prefix, yes_no(s->valid_no_process),
934 prefix, kill_mode_to_string(s->kill_mode),
935 prefix, service_type_to_string(s->type),
936 prefix, notify_access_to_string(s->notify_access));
938 if (s->control_pid > 0)
940 "%sControl PID: %lu\n",
941 prefix, (unsigned long) s->control_pid);
946 prefix, (unsigned long) s->main_pid);
951 prefix, s->pid_file);
956 "%sBus Name Good: %s\n",
958 prefix, yes_no(s->bus_name_good));
960 exec_context_dump(&s->exec_context, f, prefix);
962 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
964 if (!s->exec_command[c])
967 fprintf(f, "%s-> %s:\n",
968 prefix, service_exec_command_to_string(c));
970 exec_command_dump_list(s->exec_command[c], f, prefix2);
975 "%sSysV Init Script Path: %s\n"
976 "%sSysV Init Script has LSB Header: %s\n",
977 prefix, s->sysv_path,
978 prefix, yes_no(s->sysv_has_lsb));
980 if (s->sysv_start_priority >= 0)
982 "%sSysVStartPriority: %i\n",
983 prefix, s->sysv_start_priority);
985 if (s->sysv_runlevels)
986 fprintf(f, "%sSysVRunLevels: %s\n",
987 prefix, s->sysv_runlevels);
990 fprintf(f, "%sStatus Text: %s\n",
991 prefix, s->status_text);
996 static int service_load_pid_file(Service *s) {
1003 if (s->main_pid_known)
1006 assert(s->main_pid <= 0);
1011 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
1014 r = parse_pid(k, &pid);
1020 if (kill(pid, 0) < 0 && errno != EPERM) {
1021 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1022 (unsigned long) pid, s->pid_file);
1026 if ((r = service_set_main_pid(s, pid)) < 0)
1029 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1030 /* FIXME: we need to do something here */
1036 static int service_get_sockets(Service *s, Set **_set) {
1045 if (s->socket_fd >= 0)
1048 /* Collects all Socket objects that belong to this
1049 * service. Note that a service might have multiple sockets
1050 * via multiple names. */
1052 if (!(set = set_new(NULL, NULL)))
1055 SET_FOREACH(t, s->meta.names, i) {
1059 /* Look for all socket objects that go by any of our
1060 * units and collect their fds */
1062 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1067 p = manager_get_unit(s->meta.manager, k);
1073 if ((r = set_put(set, p)) < 0)
1085 static int service_notify_sockets_dead(Service *s) {
1093 if (s->socket_fd >= 0)
1096 /* Notifies all our sockets when we die */
1097 if ((r = service_get_sockets(s, &set)) < 0)
1100 SET_FOREACH(sock, set, i)
1101 socket_notify_service_dead(sock);
1108 static void service_set_state(Service *s, ServiceState state) {
1109 ServiceState old_state;
1112 old_state = s->state;
1115 if (state != SERVICE_START_PRE &&
1116 state != SERVICE_START &&
1117 state != SERVICE_START_POST &&
1118 state != SERVICE_RELOAD &&
1119 state != SERVICE_STOP &&
1120 state != SERVICE_STOP_SIGTERM &&
1121 state != SERVICE_STOP_SIGKILL &&
1122 state != SERVICE_STOP_POST &&
1123 state != SERVICE_FINAL_SIGTERM &&
1124 state != SERVICE_FINAL_SIGKILL &&
1125 state != SERVICE_AUTO_RESTART)
1126 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1128 if (state != SERVICE_START &&
1129 state != SERVICE_START_POST &&
1130 state != SERVICE_RUNNING &&
1131 state != SERVICE_RELOAD &&
1132 state != SERVICE_STOP &&
1133 state != SERVICE_STOP_SIGTERM &&
1134 state != SERVICE_STOP_SIGKILL)
1135 service_unwatch_main_pid(s);
1137 if (state != SERVICE_START_PRE &&
1138 state != SERVICE_START &&
1139 state != SERVICE_START_POST &&
1140 state != SERVICE_RELOAD &&
1141 state != SERVICE_STOP &&
1142 state != SERVICE_STOP_SIGTERM &&
1143 state != SERVICE_STOP_SIGKILL &&
1144 state != SERVICE_STOP_POST &&
1145 state != SERVICE_FINAL_SIGTERM &&
1146 state != SERVICE_FINAL_SIGKILL) {
1147 service_unwatch_control_pid(s);
1148 s->control_command = NULL;
1149 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1152 if (state == SERVICE_DEAD ||
1153 state == SERVICE_STOP ||
1154 state == SERVICE_STOP_SIGTERM ||
1155 state == SERVICE_STOP_SIGKILL ||
1156 state == SERVICE_STOP_POST ||
1157 state == SERVICE_FINAL_SIGTERM ||
1158 state == SERVICE_FINAL_SIGKILL ||
1159 state == SERVICE_MAINTENANCE ||
1160 state == SERVICE_AUTO_RESTART)
1161 service_notify_sockets_dead(s);
1163 if (state != SERVICE_START_PRE &&
1164 state != SERVICE_START &&
1165 state != SERVICE_START_POST &&
1166 state != SERVICE_RUNNING &&
1167 state != SERVICE_RELOAD &&
1168 state != SERVICE_STOP &&
1169 state != SERVICE_STOP_SIGTERM &&
1170 state != SERVICE_STOP_SIGKILL &&
1171 state != SERVICE_STOP_POST &&
1172 state != SERVICE_FINAL_SIGTERM &&
1173 state != SERVICE_FINAL_SIGKILL &&
1174 !(state == SERVICE_DEAD && s->meta.job)) {
1175 service_close_socket_fd(s);
1176 service_connection_unref(s);
1179 if (old_state != state)
1180 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1182 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1185 static int service_coldplug(Unit *u) {
1186 Service *s = SERVICE(u);
1190 assert(s->state == SERVICE_DEAD);
1192 if (s->deserialized_state != s->state) {
1194 if (s->deserialized_state == SERVICE_START_PRE ||
1195 s->deserialized_state == SERVICE_START ||
1196 s->deserialized_state == SERVICE_START_POST ||
1197 s->deserialized_state == SERVICE_RELOAD ||
1198 s->deserialized_state == SERVICE_STOP ||
1199 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1200 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1201 s->deserialized_state == SERVICE_STOP_POST ||
1202 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1203 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1204 s->deserialized_state == SERVICE_AUTO_RESTART) {
1206 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1209 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1211 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1216 if ((s->deserialized_state == SERVICE_START &&
1217 (s->type == SERVICE_FORKING ||
1218 s->type == SERVICE_DBUS ||
1219 s->type == SERVICE_FINISH ||
1220 s->type == SERVICE_NOTIFY)) ||
1221 s->deserialized_state == SERVICE_START_POST ||
1222 s->deserialized_state == SERVICE_RUNNING ||
1223 s->deserialized_state == SERVICE_RELOAD ||
1224 s->deserialized_state == SERVICE_STOP ||
1225 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1226 s->deserialized_state == SERVICE_STOP_SIGKILL)
1227 if (s->main_pid > 0)
1228 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1231 if (s->deserialized_state == SERVICE_START_PRE ||
1232 s->deserialized_state == SERVICE_START ||
1233 s->deserialized_state == SERVICE_START_POST ||
1234 s->deserialized_state == SERVICE_RELOAD ||
1235 s->deserialized_state == SERVICE_STOP ||
1236 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1237 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1238 s->deserialized_state == SERVICE_STOP_POST ||
1239 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1240 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1241 if (s->control_pid > 0)
1242 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1245 service_set_state(s, s->deserialized_state);
1251 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1255 unsigned rn_fds = 0;
1263 if (s->socket_fd >= 0)
1266 if ((r = service_get_sockets(s, &set)) < 0)
1269 SET_FOREACH(sock, set, i) {
1273 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1285 if (!(t = new(int, rn_fds+cn_fds))) {
1291 memcpy(t, rfds, rn_fds);
1292 memcpy(t+rn_fds, cfds, cn_fds);
1297 rn_fds = rn_fds+cn_fds;
1315 static int service_spawn(
1320 bool apply_permissions,
1322 bool set_notify_socket,
1327 int *fds = NULL, *fdsbuf = NULL;
1329 char **argv = NULL, **env = NULL;
1336 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1337 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1338 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1340 if (s->socket_fd >= 0) {
1341 fds = &s->socket_fd;
1344 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1351 if (timeout && s->timeout_usec) {
1352 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1355 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1357 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1362 if (set_notify_socket) {
1365 if (asprintf(&t, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1370 env = strv_env_set(s->meta.manager->environment, t);
1378 env = s->meta.manager->environment;
1387 s->meta.manager->confirm_spawn,
1388 s->meta.cgroup_bondings,
1394 if (set_notify_socket)
1404 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1405 /* FIXME: we need to do something here */
1417 if (set_notify_socket)
1421 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1426 static int main_pid_good(Service *s) {
1429 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1432 /* If we know the pid file, then lets just check if it is
1434 if (s->main_pid_known)
1435 return s->main_pid > 0;
1437 /* We don't know the pid */
1441 static int control_pid_good(Service *s) {
1444 return s->control_pid > 0;
1447 static int cgroup_good(Service *s) {
1452 if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1458 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1465 if (allow_restart &&
1467 (s->restart == SERVICE_RESTART_ALWAYS ||
1468 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1470 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1473 service_set_state(s, SERVICE_AUTO_RESTART);
1475 service_set_state(s, s->failure ? SERVICE_MAINTENANCE : SERVICE_DEAD);
1480 log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1481 service_enter_dead(s, false, false);
1484 static void service_enter_signal(Service *s, ServiceState state, bool success);
1486 static void service_enter_stop_post(Service *s, bool success) {
1493 service_unwatch_control_pid(s);
1495 s->control_command_id = SERVICE_EXEC_STOP_POST;
1496 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1497 if ((r = service_spawn(s,
1501 !s->permissions_start_only,
1502 !s->root_directory_start_only,
1504 &s->control_pid)) < 0)
1508 service_set_state(s, SERVICE_STOP_POST);
1510 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1515 log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1516 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1519 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1528 if (s->kill_mode != KILL_NONE) {
1529 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1531 if (s->kill_mode == KILL_CONTROL_GROUP) {
1533 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig)) < 0) {
1534 if (r != -EAGAIN && r != -ESRCH)
1543 if (s->main_pid > 0) {
1544 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1550 if (s->control_pid > 0) {
1551 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1562 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1563 if (s->timeout_usec > 0)
1564 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1567 service_set_state(s, state);
1568 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1569 service_enter_stop_post(s, true);
1571 service_enter_dead(s, true, true);
1576 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1578 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1579 service_enter_stop_post(s, false);
1581 service_enter_dead(s, false, true);
1584 static void service_enter_stop(Service *s, bool success) {
1592 service_unwatch_control_pid(s);
1594 s->control_command_id = SERVICE_EXEC_STOP;
1595 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1596 if ((r = service_spawn(s,
1600 !s->permissions_start_only,
1601 !s->root_directory_start_only,
1603 &s->control_pid)) < 0)
1606 service_set_state(s, SERVICE_STOP);
1608 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1613 log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1614 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1617 static void service_enter_running(Service *s, bool success) {
1618 int main_pid_ok, cgroup_ok;
1624 main_pid_ok = main_pid_good(s);
1625 cgroup_ok = cgroup_good(s);
1627 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1628 (s->bus_name_good || s->type != SERVICE_DBUS))
1629 service_set_state(s, SERVICE_RUNNING);
1630 else if (s->valid_no_process)
1631 service_set_state(s, SERVICE_EXITED);
1633 service_enter_stop(s, true);
1636 static void service_enter_start_post(Service *s) {
1640 service_unwatch_control_pid(s);
1642 s->control_command_id = SERVICE_EXEC_START_POST;
1643 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1644 if ((r = service_spawn(s,
1648 !s->permissions_start_only,
1649 !s->root_directory_start_only,
1651 &s->control_pid)) < 0)
1654 service_set_state(s, SERVICE_START_POST);
1656 service_enter_running(s, true);
1661 log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1662 service_enter_stop(s, false);
1665 static void service_enter_start(Service *s) {
1671 assert(s->exec_command[SERVICE_EXEC_START]);
1672 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1674 if (s->type == SERVICE_FORKING)
1675 service_unwatch_control_pid(s);
1677 service_unwatch_main_pid(s);
1679 if ((r = service_spawn(s,
1680 s->exec_command[SERVICE_EXEC_START],
1681 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1685 s->notify_access != NOTIFY_NONE,
1689 if (s->type == SERVICE_SIMPLE) {
1690 /* For simple services we immediately start
1691 * the START_POST binaries. */
1693 service_set_main_pid(s, pid);
1694 service_enter_start_post(s);
1696 } else if (s->type == SERVICE_FORKING) {
1698 /* For forking services we wait until the start
1699 * process exited. */
1701 s->control_command_id = SERVICE_EXEC_START;
1702 s->control_command = s->exec_command[SERVICE_EXEC_START];
1704 s->control_pid = pid;
1705 service_set_state(s, SERVICE_START);
1707 } else if (s->type == SERVICE_FINISH ||
1708 s->type == SERVICE_DBUS ||
1709 s->type == SERVICE_NOTIFY) {
1711 /* For finishing services we wait until the start
1712 * process exited, too, but it is our main process. */
1714 /* For D-Bus services we know the main pid right away,
1715 * but wait for the bus name to appear on the
1716 * bus. Notify services are similar. */
1718 service_set_main_pid(s, pid);
1719 service_set_state(s, SERVICE_START);
1721 assert_not_reached("Unknown service type");
1726 log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
1727 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1730 static void service_enter_start_pre(Service *s) {
1735 service_unwatch_control_pid(s);
1737 s->control_command_id = SERVICE_EXEC_START_PRE;
1738 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1739 if ((r = service_spawn(s,
1743 !s->permissions_start_only,
1744 !s->root_directory_start_only,
1746 &s->control_pid)) < 0)
1749 service_set_state(s, SERVICE_START_PRE);
1751 service_enter_start(s);
1756 log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1757 service_enter_dead(s, false, true);
1760 static void service_enter_restart(Service *s) {
1764 service_enter_dead(s, true, false);
1766 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1769 log_debug("%s scheduled restart job.", s->meta.id);
1774 log_warning("%s failed to schedule restart job: %s", s->meta.id, strerror(-r));
1775 service_enter_dead(s, false, false);
1778 static void service_enter_reload(Service *s) {
1783 service_unwatch_control_pid(s);
1785 s->control_command_id = SERVICE_EXEC_RELOAD;
1786 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1787 if ((r = service_spawn(s,
1791 !s->permissions_start_only,
1792 !s->root_directory_start_only,
1794 &s->control_pid)) < 0)
1797 service_set_state(s, SERVICE_RELOAD);
1799 service_enter_running(s, true);
1804 log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
1805 service_enter_stop(s, false);
1808 static void service_run_next(Service *s, bool success) {
1812 assert(s->control_command);
1813 assert(s->control_command->command_next);
1818 s->control_command = s->control_command->command_next;
1820 service_unwatch_control_pid(s);
1822 if ((r = service_spawn(s,
1826 !s->permissions_start_only,
1827 !s->root_directory_start_only,
1829 &s->control_pid)) < 0)
1835 log_warning("%s failed to run next task: %s", s->meta.id, strerror(-r));
1837 if (s->state == SERVICE_START_PRE)
1838 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1839 else if (s->state == SERVICE_STOP)
1840 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1841 else if (s->state == SERVICE_STOP_POST)
1842 service_enter_dead(s, false, true);
1844 service_enter_stop(s, false);
1847 static int service_start(Unit *u) {
1848 Service *s = SERVICE(u);
1852 /* We cannot fulfill this request right now, try again later
1854 if (s->state == SERVICE_STOP ||
1855 s->state == SERVICE_STOP_SIGTERM ||
1856 s->state == SERVICE_STOP_SIGKILL ||
1857 s->state == SERVICE_STOP_POST ||
1858 s->state == SERVICE_FINAL_SIGTERM ||
1859 s->state == SERVICE_FINAL_SIGKILL)
1862 /* Already on it! */
1863 if (s->state == SERVICE_START_PRE ||
1864 s->state == SERVICE_START ||
1865 s->state == SERVICE_START_POST)
1868 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTENANCE || s->state == SERVICE_AUTO_RESTART);
1870 /* Make sure we don't enter a busy loop of some kind. */
1871 if (!ratelimit_test(&s->ratelimit)) {
1872 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1877 s->main_pid_known = false;
1878 s->allow_restart = true;
1880 service_enter_start_pre(s);
1884 static int service_stop(Unit *u) {
1885 Service *s = SERVICE(u);
1889 /* Cannot do this now */
1890 if (s->state == SERVICE_START_PRE ||
1891 s->state == SERVICE_START ||
1892 s->state == SERVICE_START_POST ||
1893 s->state == SERVICE_RELOAD)
1897 if (s->state == SERVICE_STOP ||
1898 s->state == SERVICE_STOP_SIGTERM ||
1899 s->state == SERVICE_STOP_SIGKILL ||
1900 s->state == SERVICE_STOP_POST ||
1901 s->state == SERVICE_FINAL_SIGTERM ||
1902 s->state == SERVICE_FINAL_SIGKILL)
1905 if (s->state == SERVICE_AUTO_RESTART) {
1906 service_set_state(s, SERVICE_DEAD);
1910 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1912 /* This is a user request, so don't do restarts on this
1914 s->allow_restart = false;
1916 service_enter_stop(s, true);
1920 static int service_reload(Unit *u) {
1921 Service *s = SERVICE(u);
1925 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1927 service_enter_reload(s);
1931 static bool service_can_reload(Unit *u) {
1932 Service *s = SERVICE(u);
1936 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1939 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1940 Service *s = SERVICE(u);
1946 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1947 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1949 if (s->control_pid > 0)
1950 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
1952 if (s->main_pid_known && s->main_pid > 0)
1953 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
1955 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1957 /* There's a minor uncleanliness here: if there are multiple
1958 * commands attached here, we will start from the first one
1960 if (s->control_command_id >= 0)
1961 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
1963 if (s->socket_fd >= 0) {
1966 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1969 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1975 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1976 Service *s = SERVICE(u);
1984 if (streq(key, "state")) {
1987 if ((state = service_state_from_string(value)) < 0)
1988 log_debug("Failed to parse state value %s", value);
1990 s->deserialized_state = state;
1991 } else if (streq(key, "failure")) {
1994 if ((b = parse_boolean(value)) < 0)
1995 log_debug("Failed to parse failure value %s", value);
1997 s->failure = b || s->failure;
1998 } else if (streq(key, "control-pid")) {
2001 if ((r = parse_pid(value, &pid)) < 0)
2002 log_debug("Failed to parse control-pid value %s", value);
2004 s->control_pid = pid;
2005 } else if (streq(key, "main-pid")) {
2008 if ((r = parse_pid(value, &pid)) < 0)
2009 log_debug("Failed to parse main-pid value %s", value);
2011 service_set_main_pid(s, (pid_t) pid);
2012 } else if (streq(key, "main-pid-known")) {
2015 if ((b = parse_boolean(value)) < 0)
2016 log_debug("Failed to parse main-pid-known value %s", value);
2018 s->main_pid_known = b;
2019 } else if (streq(key, "control-command")) {
2020 ServiceExecCommand id;
2022 if ((id = service_exec_command_from_string(value)) < 0)
2023 log_debug("Failed to parse exec-command value %s", value);
2025 s->control_command_id = id;
2026 s->control_command = s->exec_command[id];
2028 } else if (streq(key, "socket-fd")) {
2031 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2032 log_debug("Failed to parse socket-fd value %s", value);
2035 if (s->socket_fd >= 0)
2036 close_nointr_nofail(s->socket_fd);
2037 s->socket_fd = fdset_remove(fds, fd);
2040 log_debug("Unknown serialization key '%s'", key);
2045 static UnitActiveState service_active_state(Unit *u) {
2048 return state_translation_table[SERVICE(u)->state];
2051 static const char *service_sub_state_to_string(Unit *u) {
2054 return service_state_to_string(SERVICE(u)->state);
2057 static bool service_check_gc(Unit *u) {
2058 Service *s = SERVICE(u);
2062 return !!s->sysv_path;
2065 static bool service_check_snapshot(Unit *u) {
2066 Service *s = SERVICE(u);
2070 return !s->got_socket_fd;
2073 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2074 Service *s = SERVICE(u);
2080 success = is_clean_exit(code, status);
2081 s->failure = s->failure || !success;
2083 if (s->main_pid == pid) {
2085 exec_status_fill(&s->main_exec_status, pid, code, status);
2088 if (s->type != SERVICE_FORKING) {
2089 assert(s->exec_command[SERVICE_EXEC_START]);
2090 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
2093 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2095 /* The service exited, so the service is officially
2100 case SERVICE_START_POST:
2101 case SERVICE_RELOAD:
2103 /* Need to wait until the operation is
2108 if (s->type == SERVICE_FINISH) {
2109 /* This was our main goal, so let's go on */
2111 service_enter_start_post(s);
2113 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2116 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2121 case SERVICE_RUNNING:
2122 service_enter_running(s, success);
2125 case SERVICE_STOP_SIGTERM:
2126 case SERVICE_STOP_SIGKILL:
2128 if (!control_pid_good(s))
2129 service_enter_stop_post(s, success);
2131 /* If there is still a control process, wait for that first */
2135 assert_not_reached("Uh, main process died at wrong time.");
2138 } else if (s->control_pid == pid) {
2140 if (s->control_command)
2141 exec_status_fill(&s->control_command->exec_status, pid, code, status);
2145 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2147 /* If we are shutting things down anyway we
2148 * don't care about failing commands. */
2150 if (s->control_command && s->control_command->command_next && success) {
2152 /* There is another command to *
2153 * execute, so let's do that. */
2155 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
2156 service_run_next(s, success);
2159 /* No further commands for this step, so let's
2160 * figure out what to do next */
2162 s->control_command = NULL;
2163 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2165 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2169 case SERVICE_START_PRE:
2171 service_enter_start(s);
2173 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2177 assert(s->type == SERVICE_FORKING);
2179 /* Let's try to load the pid
2180 * file here if we can. We
2181 * ignore the return value,
2182 * since the PID file might
2183 * actually be created by a
2184 * START_POST script */
2188 service_load_pid_file(s);
2190 service_enter_start_post(s);
2192 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2196 case SERVICE_START_POST:
2197 if (success && s->pid_file && !s->main_pid_known) {
2200 /* Hmm, let's see if we can
2201 * load the pid now after the
2202 * start-post scripts got
2205 if ((r = service_load_pid_file(s)) < 0)
2206 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2211 case SERVICE_RELOAD:
2213 service_enter_running(s, true);
2215 service_enter_stop(s, false);
2220 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2223 case SERVICE_STOP_SIGTERM:
2224 case SERVICE_STOP_SIGKILL:
2225 if (main_pid_good(s) <= 0)
2226 service_enter_stop_post(s, success);
2228 /* If there is still a service
2229 * process around, wait until
2230 * that one quit, too */
2233 case SERVICE_STOP_POST:
2234 case SERVICE_FINAL_SIGTERM:
2235 case SERVICE_FINAL_SIGKILL:
2236 service_enter_dead(s, success, true);
2240 assert_not_reached("Uh, control process died at wrong time.");
2246 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2247 Service *s = SERVICE(u);
2250 assert(elapsed == 1);
2252 assert(w == &s->timer_watch);
2256 case SERVICE_START_PRE:
2258 log_warning("%s operation timed out. Terminating.", u->meta.id);
2259 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2262 case SERVICE_START_POST:
2263 case SERVICE_RELOAD:
2264 log_warning("%s operation timed out. Stopping.", u->meta.id);
2265 service_enter_stop(s, false);
2269 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2270 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2273 case SERVICE_STOP_SIGTERM:
2274 log_warning("%s stopping timed out. Killing.", u->meta.id);
2275 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2278 case SERVICE_STOP_SIGKILL:
2279 /* Uh, wie sent a SIGKILL and it is still not gone?
2280 * Must be something we cannot kill, so let's just be
2281 * weirded out and continue */
2283 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2284 service_enter_stop_post(s, false);
2287 case SERVICE_STOP_POST:
2288 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2289 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2292 case SERVICE_FINAL_SIGTERM:
2293 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2294 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2297 case SERVICE_FINAL_SIGKILL:
2298 log_warning("%s still around after SIGKILL (2). Entering maintenance mode.", u->meta.id);
2299 service_enter_dead(s, false, true);
2302 case SERVICE_AUTO_RESTART:
2303 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
2304 service_enter_restart(s);
2308 assert_not_reached("Timeout at wrong time.");
2312 static void service_cgroup_notify_event(Unit *u) {
2313 Service *s = SERVICE(u);
2317 log_debug("%s: cgroup is empty", u->meta.id);
2321 /* Waiting for SIGCHLD is usually more interesting,
2322 * because it includes return codes/signals. Which is
2323 * why we ignore the cgroup events for most cases,
2324 * except when we don't know pid which to expect the
2327 case SERVICE_RUNNING:
2328 service_enter_running(s, true);
2336 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2337 Service *s = SERVICE(u);
2342 if (s->notify_access == NOTIFY_NONE) {
2343 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2344 u->meta.id, (unsigned long) pid);
2348 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2349 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2350 u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2354 log_debug("%s: Got message", u->meta.id);
2356 /* Interpret MAINPID= */
2357 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2358 (s->state == SERVICE_START ||
2359 s->state == SERVICE_START_POST ||
2360 s->state == SERVICE_RUNNING ||
2361 s->state == SERVICE_RELOAD)) {
2363 if (parse_pid(e + 8, &pid) < 0)
2364 log_warning("Failed to parse %s", e);
2366 log_debug("%s: got %s", u->meta.id, e);
2367 service_set_main_pid(s, pid);
2371 /* Interpret READY= */
2372 if (s->type == SERVICE_NOTIFY &&
2373 s->state == SERVICE_START &&
2374 strv_find(tags, "READY=1")) {
2375 log_debug("%s: got READY=1", u->meta.id);
2377 service_enter_start_post(s);
2380 /* Interpret STATUS= */
2381 if ((e = strv_find_prefix(tags, "STATUS="))) {
2384 if (!(t = strdup(e+7))) {
2385 log_error("Failed to allocate string.");
2389 log_debug("%s: got %s", u->meta.id, e);
2391 free(s->status_text);
2396 static int service_enumerate(Manager *m) {
2400 char *path = NULL, *fpath = NULL, *name = NULL;
2405 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2406 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2411 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2419 if (!(d = opendir(path))) {
2420 if (errno != ENOENT)
2421 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2426 while ((de = readdir(d))) {
2430 if (ignore_file(de->d_name))
2433 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2436 if (strlen(de->d_name) < 4)
2439 a = undecchar(de->d_name[1]);
2440 b = undecchar(de->d_name[2]);
2447 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2452 if (access(fpath, X_OK) < 0) {
2454 if (errno != ENOENT)
2455 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2461 if (!(name = sysv_translate_name(de->d_name + 3))) {
2466 if ((r = manager_load_unit_prepare(m, name, NULL, &service)) < 0) {
2467 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2471 if (de->d_name[0] == 'S' &&
2472 (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT))
2473 SERVICE(service)->sysv_start_priority =
2474 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2476 manager_dispatch_load_queue(m);
2477 service = unit_follow_merge(service);
2479 if (de->d_name[0] == 'S') {
2481 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2484 } else if (de->d_name[0] == 'K' &&
2485 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2486 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2488 /* We honour K links only for
2489 * halt/reboot. For the normal
2490 * runlevels we assume the
2492 * implicitly added by the
2493 * core logic. Also, we don't
2494 * really distuingish here
2495 * between the runlevels 0 and
2496 * 6 and just add them to the
2497 * special shutdown target. On
2498 * SUSE the boot.d/ runlevel
2499 * is also used for shutdown,
2500 * so we add links for that
2501 * too to the shutdown
2504 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2523 static void service_bus_name_owner_change(
2526 const char *old_owner,
2527 const char *new_owner) {
2529 Service *s = SERVICE(u);
2534 assert(streq(s->bus_name, name));
2535 assert(old_owner || new_owner);
2537 if (old_owner && new_owner)
2538 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2540 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2542 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2544 s->bus_name_good = !!new_owner;
2546 if (s->type == SERVICE_DBUS) {
2548 /* service_enter_running() will figure out what to
2550 if (s->state == SERVICE_RUNNING)
2551 service_enter_running(s, true);
2552 else if (s->state == SERVICE_START && new_owner)
2553 service_enter_start_post(s);
2555 } else if (new_owner &&
2557 (s->state == SERVICE_START ||
2558 s->state == SERVICE_START_POST ||
2559 s->state == SERVICE_RUNNING ||
2560 s->state == SERVICE_RELOAD)) {
2562 /* Try to acquire PID from bus service */
2563 log_debug("Trying to acquire PID from D-Bus name...");
2565 bus_query_pid(u->meta.manager, name);
2569 static void service_bus_query_pid_done(
2574 Service *s = SERVICE(u);
2579 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2581 if (s->main_pid <= 0 &&
2582 (s->state == SERVICE_START ||
2583 s->state == SERVICE_START_POST ||
2584 s->state == SERVICE_RUNNING ||
2585 s->state == SERVICE_RELOAD))
2586 service_set_main_pid(s, pid);
2589 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2593 /* This is called by the socket code when instantiating a new
2594 * service for a stream socket and the socket needs to be
2597 if (s->meta.load_state != UNIT_LOADED)
2600 if (s->socket_fd >= 0)
2603 if (s->state != SERVICE_DEAD)
2607 s->got_socket_fd = true;
2613 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2614 [SERVICE_DEAD] = "dead",
2615 [SERVICE_START_PRE] = "start-pre",
2616 [SERVICE_START] = "start",
2617 [SERVICE_START_POST] = "start-post",
2618 [SERVICE_RUNNING] = "running",
2619 [SERVICE_EXITED] = "exited",
2620 [SERVICE_RELOAD] = "reload",
2621 [SERVICE_STOP] = "stop",
2622 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2623 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2624 [SERVICE_STOP_POST] = "stop-post",
2625 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2626 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2627 [SERVICE_MAINTENANCE] = "maintenance",
2628 [SERVICE_AUTO_RESTART] = "auto-restart",
2631 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2633 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2634 [SERVICE_ONCE] = "once",
2635 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2636 [SERVICE_RESTART_ALWAYS] = "restart-always",
2639 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2641 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2642 [SERVICE_SIMPLE] = "simple",
2643 [SERVICE_FORKING] = "forking",
2644 [SERVICE_FINISH] = "finish",
2645 [SERVICE_DBUS] = "dbus",
2646 [SERVICE_NOTIFY] = "notify"
2649 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2651 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2652 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2653 [SERVICE_EXEC_START] = "ExecStart",
2654 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2655 [SERVICE_EXEC_RELOAD] = "ExecReload",
2656 [SERVICE_EXEC_STOP] = "ExecStop",
2657 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2660 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2662 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2663 [NOTIFY_NONE] = "none",
2664 [NOTIFY_MAIN] = "main",
2665 [NOTIFY_ALL] = "all"
2668 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2670 const UnitVTable service_vtable = {
2671 .suffix = ".service",
2673 .init = service_init,
2674 .done = service_done,
2675 .load = service_load,
2677 .coldplug = service_coldplug,
2679 .dump = service_dump,
2681 .start = service_start,
2682 .stop = service_stop,
2683 .reload = service_reload,
2685 .can_reload = service_can_reload,
2687 .serialize = service_serialize,
2688 .deserialize_item = service_deserialize_item,
2690 .active_state = service_active_state,
2691 .sub_state_to_string = service_sub_state_to_string,
2693 .check_gc = service_check_gc,
2694 .check_snapshot = service_check_snapshot,
2696 .sigchld_event = service_sigchld_event,
2697 .timer_event = service_timer_event,
2699 .cgroup_notify_empty = service_cgroup_notify_event,
2700 .notify_message = service_notify_message,
2702 .bus_name_owner_change = service_bus_name_owner_change,
2703 .bus_query_pid_done = service_bus_query_pid_done,
2705 .bus_message_handler = bus_service_message_handler,
2707 .enumerate = service_enumerate