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 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0 ||
676 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
680 /* Don't timeout special services during boot (like fsck) */
683 /* Special setting for all SysV services */
684 s->type = SERVICE_FORKING;
685 s->valid_no_process = true;
686 s->kill_mode = KILL_PROCESS_GROUP;
687 s->restart = SERVICE_ONCE;
689 u->meta.load_state = UNIT_LOADED;
700 static int service_load_sysv_name(Service *s, const char *name) {
706 /* For SysV services we strip the boot. or .sh
707 * prefixes/suffixes. */
708 if (startswith(name, "boot.") ||
709 endswith(name, ".sh.service"))
712 STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
716 if (asprintf(&path, "%s/%s", *p, name) < 0)
719 assert(endswith(path, ".service"));
720 path[strlen(path)-8] = 0;
722 r = service_load_sysv_path(s, path);
724 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
725 /* Try Debian style xxx.sh source'able init scripts */
727 r = service_load_sysv_path(s, path);
732 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
733 /* Try SUSE style boot.xxx init scripts */
735 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
738 path[strlen(path)-8] = 0;
739 r = service_load_sysv_path(s, path);
746 if ((s->meta.load_state != UNIT_STUB))
753 static int service_load_sysv(Service *s) {
760 /* Load service data from SysV init scripts, preferably with
763 if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path))
766 if ((t = s->meta.id))
767 if ((r = service_load_sysv_name(s, t)) < 0)
770 if (s->meta.load_state == UNIT_STUB)
771 SET_FOREACH(t, s->meta.names, i) {
775 if ((r == service_load_sysv_name(s, t)) < 0)
778 if (s->meta.load_state != UNIT_STUB)
785 static int service_add_bus_name(Service *s) {
792 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
795 r = unit_merge_by_name(UNIT(s), n);
801 static int service_verify(Service *s) {
804 if (s->meta.load_state != UNIT_LOADED)
807 if (!s->exec_command[SERVICE_EXEC_START]) {
808 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
812 if (s->exec_command[SERVICE_EXEC_START]->command_next) {
813 log_error("%s has more than one ExecStart setting. Refusing.", s->meta.id);
817 if (s->type == SERVICE_DBUS && !s->bus_name) {
818 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
822 if (s->exec_context.pam_name && s->kill_mode != KILL_CONTROL_GROUP) {
823 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
830 static int service_load(Unit *u) {
832 Service *s = SERVICE(u);
836 /* Load a .service file */
837 if ((r = unit_load_fragment(u)) < 0)
840 /* Load a classic init script as a fallback, if we couldn't find anything */
841 if (u->meta.load_state == UNIT_STUB)
842 if ((r = service_load_sysv(s)) < 0)
845 /* Still nothing found? Then let's give up */
846 if (u->meta.load_state == UNIT_STUB)
849 /* We were able to load something, then let's add in the
850 * dropin directories. */
851 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
854 /* This is a new unit? Then let's add in some extras */
855 if (u->meta.load_state == UNIT_LOADED) {
856 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
859 if ((r = unit_add_default_cgroup(u)) < 0)
862 if ((r = sysv_fix_order(s)) < 0)
866 if ((r = service_add_bus_name(s)) < 0)
869 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
873 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
874 s->notify_access = NOTIFY_MAIN;
877 return service_verify(s);
880 static void service_dump(Unit *u, FILE *f, const char *prefix) {
882 ServiceExecCommand c;
883 Service *s = SERVICE(u);
889 p2 = strappend(prefix, "\t");
890 prefix2 = p2 ? p2 : prefix;
893 "%sService State: %s\n"
894 "%sPermissionsStartOnly: %s\n"
895 "%sRootDirectoryStartOnly: %s\n"
896 "%sValidNoProcess: %s\n"
899 "%sNotifyAccess: %s\n",
900 prefix, service_state_to_string(s->state),
901 prefix, yes_no(s->permissions_start_only),
902 prefix, yes_no(s->root_directory_start_only),
903 prefix, yes_no(s->valid_no_process),
904 prefix, kill_mode_to_string(s->kill_mode),
905 prefix, service_type_to_string(s->type),
906 prefix, notify_access_to_string(s->notify_access));
908 if (s->control_pid > 0)
910 "%sControl PID: %lu\n",
911 prefix, (unsigned long) s->control_pid);
916 prefix, (unsigned long) s->main_pid);
921 prefix, s->pid_file);
926 "%sBus Name Good: %s\n",
928 prefix, yes_no(s->bus_name_good));
930 exec_context_dump(&s->exec_context, f, prefix);
932 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
934 if (!s->exec_command[c])
937 fprintf(f, "%s-> %s:\n",
938 prefix, service_exec_command_to_string(c));
940 exec_command_dump_list(s->exec_command[c], f, prefix2);
945 "%sSysV Init Script Path: %s\n"
946 "%sSysV Init Script has LSB Header: %s\n",
947 prefix, s->sysv_path,
948 prefix, yes_no(s->sysv_has_lsb));
950 if (s->sysv_start_priority >= 0)
952 "%sSysVStartPriority: %i\n",
953 prefix, s->sysv_start_priority);
955 if (s->sysv_runlevels)
956 fprintf(f, "%sSysVRunLevels: %s\n",
957 prefix, s->sysv_runlevels);
960 fprintf(f, "%sStatus Text: %s\n",
961 prefix, s->status_text);
966 static int service_load_pid_file(Service *s) {
973 if (s->main_pid_known)
976 assert(s->main_pid <= 0);
981 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
984 r = parse_pid(k, &pid);
990 if (kill(pid, 0) < 0 && errno != EPERM) {
991 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
992 (unsigned long) pid, s->pid_file);
996 if ((r = service_set_main_pid(s, pid)) < 0)
999 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1000 /* FIXME: we need to do something here */
1006 static int service_get_sockets(Service *s, Set **_set) {
1015 if (s->socket_fd >= 0)
1018 /* Collects all Socket objects that belong to this
1019 * service. Note that a service might have multiple sockets
1020 * via multiple names. */
1022 if (!(set = set_new(NULL, NULL)))
1025 SET_FOREACH(t, s->meta.names, i) {
1029 /* Look for all socket objects that go by any of our
1030 * units and collect their fds */
1032 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1037 p = manager_get_unit(s->meta.manager, k);
1043 if ((r = set_put(set, p)) < 0)
1055 static int service_notify_sockets_dead(Service *s) {
1063 if (s->socket_fd >= 0)
1066 /* Notifies all our sockets when we die */
1067 if ((r = service_get_sockets(s, &set)) < 0)
1070 SET_FOREACH(sock, set, i)
1071 socket_notify_service_dead(sock);
1078 static void service_set_state(Service *s, ServiceState state) {
1079 ServiceState old_state;
1082 old_state = s->state;
1085 if (state != SERVICE_START_PRE &&
1086 state != SERVICE_START &&
1087 state != SERVICE_START_POST &&
1088 state != SERVICE_RELOAD &&
1089 state != SERVICE_STOP &&
1090 state != SERVICE_STOP_SIGTERM &&
1091 state != SERVICE_STOP_SIGKILL &&
1092 state != SERVICE_STOP_POST &&
1093 state != SERVICE_FINAL_SIGTERM &&
1094 state != SERVICE_FINAL_SIGKILL &&
1095 state != SERVICE_AUTO_RESTART)
1096 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1098 if (state != SERVICE_START &&
1099 state != SERVICE_START_POST &&
1100 state != SERVICE_RUNNING &&
1101 state != SERVICE_RELOAD &&
1102 state != SERVICE_STOP &&
1103 state != SERVICE_STOP_SIGTERM &&
1104 state != SERVICE_STOP_SIGKILL)
1105 service_unwatch_main_pid(s);
1107 if (state != SERVICE_START_PRE &&
1108 state != SERVICE_START &&
1109 state != SERVICE_START_POST &&
1110 state != SERVICE_RELOAD &&
1111 state != SERVICE_STOP &&
1112 state != SERVICE_STOP_SIGTERM &&
1113 state != SERVICE_STOP_SIGKILL &&
1114 state != SERVICE_STOP_POST &&
1115 state != SERVICE_FINAL_SIGTERM &&
1116 state != SERVICE_FINAL_SIGKILL) {
1117 service_unwatch_control_pid(s);
1118 s->control_command = NULL;
1119 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1122 if (state == SERVICE_DEAD ||
1123 state == SERVICE_STOP ||
1124 state == SERVICE_STOP_SIGTERM ||
1125 state == SERVICE_STOP_SIGKILL ||
1126 state == SERVICE_STOP_POST ||
1127 state == SERVICE_FINAL_SIGTERM ||
1128 state == SERVICE_FINAL_SIGKILL ||
1129 state == SERVICE_MAINTENANCE ||
1130 state == SERVICE_AUTO_RESTART)
1131 service_notify_sockets_dead(s);
1133 if (state != SERVICE_START_PRE &&
1134 state != SERVICE_START &&
1135 state != SERVICE_START_POST &&
1136 state != SERVICE_RUNNING &&
1137 state != SERVICE_RELOAD &&
1138 state != SERVICE_STOP &&
1139 state != SERVICE_STOP_SIGTERM &&
1140 state != SERVICE_STOP_SIGKILL &&
1141 state != SERVICE_STOP_POST &&
1142 state != SERVICE_FINAL_SIGTERM &&
1143 state != SERVICE_FINAL_SIGKILL &&
1144 !(state == SERVICE_DEAD && s->meta.job)) {
1145 service_close_socket_fd(s);
1146 service_connection_unref(s);
1149 if (old_state != state)
1150 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1152 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1155 static int service_coldplug(Unit *u) {
1156 Service *s = SERVICE(u);
1160 assert(s->state == SERVICE_DEAD);
1162 if (s->deserialized_state != s->state) {
1164 if (s->deserialized_state == SERVICE_START_PRE ||
1165 s->deserialized_state == SERVICE_START ||
1166 s->deserialized_state == SERVICE_START_POST ||
1167 s->deserialized_state == SERVICE_RELOAD ||
1168 s->deserialized_state == SERVICE_STOP ||
1169 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1170 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1171 s->deserialized_state == SERVICE_STOP_POST ||
1172 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1173 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1174 s->deserialized_state == SERVICE_AUTO_RESTART) {
1176 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1179 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1181 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1186 if ((s->deserialized_state == SERVICE_START &&
1187 (s->type == SERVICE_FORKING ||
1188 s->type == SERVICE_DBUS ||
1189 s->type == SERVICE_FINISH ||
1190 s->type == SERVICE_NOTIFY)) ||
1191 s->deserialized_state == SERVICE_START_POST ||
1192 s->deserialized_state == SERVICE_RUNNING ||
1193 s->deserialized_state == SERVICE_RELOAD ||
1194 s->deserialized_state == SERVICE_STOP ||
1195 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1196 s->deserialized_state == SERVICE_STOP_SIGKILL)
1197 if (s->main_pid > 0)
1198 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1201 if (s->deserialized_state == SERVICE_START_PRE ||
1202 s->deserialized_state == SERVICE_START ||
1203 s->deserialized_state == SERVICE_START_POST ||
1204 s->deserialized_state == SERVICE_RELOAD ||
1205 s->deserialized_state == SERVICE_STOP ||
1206 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1207 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1208 s->deserialized_state == SERVICE_STOP_POST ||
1209 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1210 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1211 if (s->control_pid > 0)
1212 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1215 service_set_state(s, s->deserialized_state);
1221 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1225 unsigned rn_fds = 0;
1233 if (s->socket_fd >= 0)
1236 if ((r = service_get_sockets(s, &set)) < 0)
1239 SET_FOREACH(sock, set, i) {
1243 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1255 if (!(t = new(int, rn_fds+cn_fds))) {
1261 memcpy(t, rfds, rn_fds);
1262 memcpy(t+rn_fds, cfds, cn_fds);
1267 rn_fds = rn_fds+cn_fds;
1285 static int service_spawn(
1290 bool apply_permissions,
1292 bool set_notify_socket,
1297 int *fds = NULL, *fdsbuf = NULL;
1299 char **argv = NULL, **env = NULL;
1306 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1307 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1308 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1310 if (s->socket_fd >= 0) {
1311 fds = &s->socket_fd;
1314 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1321 if (timeout && s->timeout_usec) {
1322 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1325 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1327 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1332 if (set_notify_socket) {
1335 if (asprintf(&t, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1340 env = strv_env_set(s->meta.manager->environment, t);
1348 env = s->meta.manager->environment;
1357 s->meta.manager->confirm_spawn,
1358 s->meta.cgroup_bondings,
1364 if (set_notify_socket)
1374 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1375 /* FIXME: we need to do something here */
1387 if (set_notify_socket)
1391 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1396 static int main_pid_good(Service *s) {
1399 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1402 /* If we know the pid file, then lets just check if it is
1404 if (s->main_pid_known)
1405 return s->main_pid > 0;
1407 /* We don't know the pid */
1411 static int control_pid_good(Service *s) {
1414 return s->control_pid > 0;
1417 static int cgroup_good(Service *s) {
1422 if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1428 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1435 if (allow_restart &&
1437 (s->restart == SERVICE_RESTART_ALWAYS ||
1438 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1440 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1443 service_set_state(s, SERVICE_AUTO_RESTART);
1445 service_set_state(s, s->failure ? SERVICE_MAINTENANCE : SERVICE_DEAD);
1450 log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1451 service_enter_dead(s, false, false);
1454 static void service_enter_signal(Service *s, ServiceState state, bool success);
1456 static void service_enter_stop_post(Service *s, bool success) {
1463 service_unwatch_control_pid(s);
1465 s->control_command_id = SERVICE_EXEC_STOP_POST;
1466 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1467 if ((r = service_spawn(s,
1471 !s->permissions_start_only,
1472 !s->root_directory_start_only,
1474 &s->control_pid)) < 0)
1478 service_set_state(s, SERVICE_STOP_POST);
1480 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1485 log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1486 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1489 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1498 if (s->kill_mode != KILL_NONE) {
1499 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1501 if (s->kill_mode == KILL_CONTROL_GROUP) {
1503 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig)) < 0) {
1504 if (r != -EAGAIN && r != -ESRCH)
1513 if (s->main_pid > 0) {
1514 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1520 if (s->control_pid > 0) {
1521 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1532 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1533 if (s->timeout_usec > 0)
1534 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1537 service_set_state(s, state);
1538 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1539 service_enter_stop_post(s, true);
1541 service_enter_dead(s, true, true);
1546 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1548 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1549 service_enter_stop_post(s, false);
1551 service_enter_dead(s, false, true);
1554 static void service_enter_stop(Service *s, bool success) {
1562 service_unwatch_control_pid(s);
1564 s->control_command_id = SERVICE_EXEC_STOP;
1565 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1566 if ((r = service_spawn(s,
1570 !s->permissions_start_only,
1571 !s->root_directory_start_only,
1573 &s->control_pid)) < 0)
1576 service_set_state(s, SERVICE_STOP);
1578 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1583 log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1584 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1587 static void service_enter_running(Service *s, bool success) {
1588 int main_pid_ok, cgroup_ok;
1594 main_pid_ok = main_pid_good(s);
1595 cgroup_ok = cgroup_good(s);
1597 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1598 (s->bus_name_good || s->type != SERVICE_DBUS))
1599 service_set_state(s, SERVICE_RUNNING);
1600 else if (s->valid_no_process)
1601 service_set_state(s, SERVICE_EXITED);
1603 service_enter_stop(s, true);
1606 static void service_enter_start_post(Service *s) {
1610 service_unwatch_control_pid(s);
1612 s->control_command_id = SERVICE_EXEC_START_POST;
1613 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1614 if ((r = service_spawn(s,
1618 !s->permissions_start_only,
1619 !s->root_directory_start_only,
1621 &s->control_pid)) < 0)
1624 service_set_state(s, SERVICE_START_POST);
1626 service_enter_running(s, true);
1631 log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1632 service_enter_stop(s, false);
1635 static void service_enter_start(Service *s) {
1641 assert(s->exec_command[SERVICE_EXEC_START]);
1642 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1644 if (s->type == SERVICE_FORKING)
1645 service_unwatch_control_pid(s);
1647 service_unwatch_main_pid(s);
1649 if ((r = service_spawn(s,
1650 s->exec_command[SERVICE_EXEC_START],
1651 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1655 s->notify_access != NOTIFY_NONE,
1659 if (s->type == SERVICE_SIMPLE) {
1660 /* For simple services we immediately start
1661 * the START_POST binaries. */
1663 service_set_main_pid(s, pid);
1664 service_enter_start_post(s);
1666 } else if (s->type == SERVICE_FORKING) {
1668 /* For forking services we wait until the start
1669 * process exited. */
1671 s->control_command_id = SERVICE_EXEC_START;
1672 s->control_command = s->exec_command[SERVICE_EXEC_START];
1674 s->control_pid = pid;
1675 service_set_state(s, SERVICE_START);
1677 } else if (s->type == SERVICE_FINISH ||
1678 s->type == SERVICE_DBUS ||
1679 s->type == SERVICE_NOTIFY) {
1681 /* For finishing services we wait until the start
1682 * process exited, too, but it is our main process. */
1684 /* For D-Bus services we know the main pid right away,
1685 * but wait for the bus name to appear on the
1686 * bus. Notify services are similar. */
1688 service_set_main_pid(s, pid);
1689 service_set_state(s, SERVICE_START);
1691 assert_not_reached("Unknown service type");
1696 log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
1697 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1700 static void service_enter_start_pre(Service *s) {
1705 service_unwatch_control_pid(s);
1707 s->control_command_id = SERVICE_EXEC_START_PRE;
1708 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1709 if ((r = service_spawn(s,
1713 !s->permissions_start_only,
1714 !s->root_directory_start_only,
1716 &s->control_pid)) < 0)
1719 service_set_state(s, SERVICE_START_PRE);
1721 service_enter_start(s);
1726 log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1727 service_enter_dead(s, false, true);
1730 static void service_enter_restart(Service *s) {
1734 service_enter_dead(s, true, false);
1736 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1739 log_debug("%s scheduled restart job.", s->meta.id);
1744 log_warning("%s failed to schedule restart job: %s", s->meta.id, strerror(-r));
1745 service_enter_dead(s, false, false);
1748 static void service_enter_reload(Service *s) {
1753 service_unwatch_control_pid(s);
1755 s->control_command_id = SERVICE_EXEC_RELOAD;
1756 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1757 if ((r = service_spawn(s,
1761 !s->permissions_start_only,
1762 !s->root_directory_start_only,
1764 &s->control_pid)) < 0)
1767 service_set_state(s, SERVICE_RELOAD);
1769 service_enter_running(s, true);
1774 log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
1775 service_enter_stop(s, false);
1778 static void service_run_next(Service *s, bool success) {
1782 assert(s->control_command);
1783 assert(s->control_command->command_next);
1788 s->control_command = s->control_command->command_next;
1790 service_unwatch_control_pid(s);
1792 if ((r = service_spawn(s,
1796 !s->permissions_start_only,
1797 !s->root_directory_start_only,
1799 &s->control_pid)) < 0)
1805 log_warning("%s failed to run next task: %s", s->meta.id, strerror(-r));
1807 if (s->state == SERVICE_START_PRE)
1808 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1809 else if (s->state == SERVICE_STOP)
1810 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1811 else if (s->state == SERVICE_STOP_POST)
1812 service_enter_dead(s, false, true);
1814 service_enter_stop(s, false);
1817 static int service_start(Unit *u) {
1818 Service *s = SERVICE(u);
1822 /* We cannot fulfill this request right now, try again later
1824 if (s->state == SERVICE_STOP ||
1825 s->state == SERVICE_STOP_SIGTERM ||
1826 s->state == SERVICE_STOP_SIGKILL ||
1827 s->state == SERVICE_STOP_POST ||
1828 s->state == SERVICE_FINAL_SIGTERM ||
1829 s->state == SERVICE_FINAL_SIGKILL)
1832 /* Already on it! */
1833 if (s->state == SERVICE_START_PRE ||
1834 s->state == SERVICE_START ||
1835 s->state == SERVICE_START_POST)
1838 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTENANCE || s->state == SERVICE_AUTO_RESTART);
1840 /* Make sure we don't enter a busy loop of some kind. */
1841 if (!ratelimit_test(&s->ratelimit)) {
1842 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1847 s->main_pid_known = false;
1848 s->allow_restart = true;
1850 service_enter_start_pre(s);
1854 static int service_stop(Unit *u) {
1855 Service *s = SERVICE(u);
1859 /* Cannot do this now */
1860 if (s->state == SERVICE_START_PRE ||
1861 s->state == SERVICE_START ||
1862 s->state == SERVICE_START_POST ||
1863 s->state == SERVICE_RELOAD)
1867 if (s->state == SERVICE_STOP ||
1868 s->state == SERVICE_STOP_SIGTERM ||
1869 s->state == SERVICE_STOP_SIGKILL ||
1870 s->state == SERVICE_STOP_POST ||
1871 s->state == SERVICE_FINAL_SIGTERM ||
1872 s->state == SERVICE_FINAL_SIGKILL)
1875 if (s->state == SERVICE_AUTO_RESTART) {
1876 service_set_state(s, SERVICE_DEAD);
1880 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1882 /* This is a user request, so don't do restarts on this
1884 s->allow_restart = false;
1886 service_enter_stop(s, true);
1890 static int service_reload(Unit *u) {
1891 Service *s = SERVICE(u);
1895 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1897 service_enter_reload(s);
1901 static bool service_can_reload(Unit *u) {
1902 Service *s = SERVICE(u);
1906 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1909 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1910 Service *s = SERVICE(u);
1916 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1917 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1919 if (s->control_pid > 0)
1920 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
1922 if (s->main_pid_known && s->main_pid > 0)
1923 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
1925 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1927 /* There's a minor uncleanliness here: if there are multiple
1928 * commands attached here, we will start from the first one
1930 if (s->control_command_id >= 0)
1931 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
1933 if (s->socket_fd >= 0) {
1936 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1939 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1945 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1946 Service *s = SERVICE(u);
1954 if (streq(key, "state")) {
1957 if ((state = service_state_from_string(value)) < 0)
1958 log_debug("Failed to parse state value %s", value);
1960 s->deserialized_state = state;
1961 } else if (streq(key, "failure")) {
1964 if ((b = parse_boolean(value)) < 0)
1965 log_debug("Failed to parse failure value %s", value);
1967 s->failure = b || s->failure;
1968 } else if (streq(key, "control-pid")) {
1971 if ((r = parse_pid(value, &pid)) < 0)
1972 log_debug("Failed to parse control-pid value %s", value);
1974 s->control_pid = pid;
1975 } else if (streq(key, "main-pid")) {
1978 if ((r = parse_pid(value, &pid)) < 0)
1979 log_debug("Failed to parse main-pid value %s", value);
1981 service_set_main_pid(s, (pid_t) pid);
1982 } else if (streq(key, "main-pid-known")) {
1985 if ((b = parse_boolean(value)) < 0)
1986 log_debug("Failed to parse main-pid-known value %s", value);
1988 s->main_pid_known = b;
1989 } else if (streq(key, "control-command")) {
1990 ServiceExecCommand id;
1992 if ((id = service_exec_command_from_string(value)) < 0)
1993 log_debug("Failed to parse exec-command value %s", value);
1995 s->control_command_id = id;
1996 s->control_command = s->exec_command[id];
1998 } else if (streq(key, "socket-fd")) {
2001 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2002 log_debug("Failed to parse socket-fd value %s", value);
2005 if (s->socket_fd >= 0)
2006 close_nointr_nofail(s->socket_fd);
2007 s->socket_fd = fdset_remove(fds, fd);
2010 log_debug("Unknown serialization key '%s'", key);
2015 static UnitActiveState service_active_state(Unit *u) {
2018 return state_translation_table[SERVICE(u)->state];
2021 static const char *service_sub_state_to_string(Unit *u) {
2024 return service_state_to_string(SERVICE(u)->state);
2027 static bool service_check_gc(Unit *u) {
2028 Service *s = SERVICE(u);
2032 return !!s->sysv_path;
2035 static bool service_check_snapshot(Unit *u) {
2036 Service *s = SERVICE(u);
2040 return !s->got_socket_fd;
2043 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2044 Service *s = SERVICE(u);
2050 success = is_clean_exit(code, status);
2051 s->failure = s->failure || !success;
2053 if (s->main_pid == pid) {
2055 exec_status_fill(&s->main_exec_status, pid, code, status);
2058 if (s->type != SERVICE_FORKING) {
2059 assert(s->exec_command[SERVICE_EXEC_START]);
2060 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
2063 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2065 /* The service exited, so the service is officially
2070 case SERVICE_START_POST:
2071 case SERVICE_RELOAD:
2073 /* Need to wait until the operation is
2078 if (s->type == SERVICE_FINISH) {
2079 /* This was our main goal, so let's go on */
2081 service_enter_start_post(s);
2083 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2086 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2091 case SERVICE_RUNNING:
2092 service_enter_running(s, success);
2095 case SERVICE_STOP_SIGTERM:
2096 case SERVICE_STOP_SIGKILL:
2098 if (!control_pid_good(s))
2099 service_enter_stop_post(s, success);
2101 /* If there is still a control process, wait for that first */
2105 assert_not_reached("Uh, main process died at wrong time.");
2108 } else if (s->control_pid == pid) {
2110 if (s->control_command)
2111 exec_status_fill(&s->control_command->exec_status, pid, code, status);
2115 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2117 /* If we are shutting things down anyway we
2118 * don't care about failing commands. */
2120 if (s->control_command && s->control_command->command_next && success) {
2122 /* There is another command to *
2123 * execute, so let's do that. */
2125 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
2126 service_run_next(s, success);
2129 /* No further commands for this step, so let's
2130 * figure out what to do next */
2132 s->control_command = NULL;
2133 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2135 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2139 case SERVICE_START_PRE:
2141 service_enter_start(s);
2143 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2147 assert(s->type == SERVICE_FORKING);
2149 /* Let's try to load the pid
2150 * file here if we can. We
2151 * ignore the return value,
2152 * since the PID file might
2153 * actually be created by a
2154 * START_POST script */
2158 service_load_pid_file(s);
2160 service_enter_start_post(s);
2162 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2166 case SERVICE_START_POST:
2167 if (success && s->pid_file && !s->main_pid_known) {
2170 /* Hmm, let's see if we can
2171 * load the pid now after the
2172 * start-post scripts got
2175 if ((r = service_load_pid_file(s)) < 0)
2176 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2181 case SERVICE_RELOAD:
2183 service_enter_running(s, true);
2185 service_enter_stop(s, false);
2190 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2193 case SERVICE_STOP_SIGTERM:
2194 case SERVICE_STOP_SIGKILL:
2195 if (main_pid_good(s) <= 0)
2196 service_enter_stop_post(s, success);
2198 /* If there is still a service
2199 * process around, wait until
2200 * that one quit, too */
2203 case SERVICE_STOP_POST:
2204 case SERVICE_FINAL_SIGTERM:
2205 case SERVICE_FINAL_SIGKILL:
2206 service_enter_dead(s, success, true);
2210 assert_not_reached("Uh, control process died at wrong time.");
2216 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2217 Service *s = SERVICE(u);
2220 assert(elapsed == 1);
2222 assert(w == &s->timer_watch);
2226 case SERVICE_START_PRE:
2228 log_warning("%s operation timed out. Terminating.", u->meta.id);
2229 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2232 case SERVICE_START_POST:
2233 case SERVICE_RELOAD:
2234 log_warning("%s operation timed out. Stopping.", u->meta.id);
2235 service_enter_stop(s, false);
2239 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2240 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2243 case SERVICE_STOP_SIGTERM:
2244 log_warning("%s stopping timed out. Killing.", u->meta.id);
2245 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2248 case SERVICE_STOP_SIGKILL:
2249 /* Uh, wie sent a SIGKILL and it is still not gone?
2250 * Must be something we cannot kill, so let's just be
2251 * weirded out and continue */
2253 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2254 service_enter_stop_post(s, false);
2257 case SERVICE_STOP_POST:
2258 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2259 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2262 case SERVICE_FINAL_SIGTERM:
2263 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2264 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2267 case SERVICE_FINAL_SIGKILL:
2268 log_warning("%s still around after SIGKILL (2). Entering maintenance mode.", u->meta.id);
2269 service_enter_dead(s, false, true);
2272 case SERVICE_AUTO_RESTART:
2273 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
2274 service_enter_restart(s);
2278 assert_not_reached("Timeout at wrong time.");
2282 static void service_cgroup_notify_event(Unit *u) {
2283 Service *s = SERVICE(u);
2287 log_debug("%s: cgroup is empty", u->meta.id);
2291 /* Waiting for SIGCHLD is usually more interesting,
2292 * because it includes return codes/signals. Which is
2293 * why we ignore the cgroup events for most cases,
2294 * except when we don't know pid which to expect the
2297 case SERVICE_RUNNING:
2298 service_enter_running(s, true);
2306 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2307 Service *s = SERVICE(u);
2312 if (s->notify_access == NOTIFY_NONE) {
2313 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2314 u->meta.id, (unsigned long) pid);
2318 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2319 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2320 u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2324 log_debug("%s: Got message", u->meta.id);
2326 /* Interpret MAINPID= */
2327 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2328 (s->state == SERVICE_START ||
2329 s->state == SERVICE_START_POST ||
2330 s->state == SERVICE_RUNNING ||
2331 s->state == SERVICE_RELOAD)) {
2333 if (parse_pid(e + 8, &pid) < 0)
2334 log_warning("Failed to parse %s", e);
2336 log_debug("%s: got %s", u->meta.id, e);
2337 service_set_main_pid(s, pid);
2341 /* Interpret READY= */
2342 if (s->type == SERVICE_NOTIFY &&
2343 s->state == SERVICE_START &&
2344 strv_find(tags, "READY=1")) {
2345 log_debug("%s: got READY=1", u->meta.id);
2347 service_enter_start_post(s);
2350 /* Interpret STATUS= */
2351 if ((e = strv_find_prefix(tags, "STATUS="))) {
2354 if (!(t = strdup(e+7))) {
2355 log_error("Failed to allocate string.");
2359 log_debug("%s: got %s", u->meta.id, e);
2361 free(s->status_text);
2366 static int service_enumerate(Manager *m) {
2370 char *path = NULL, *fpath = NULL, *name = NULL;
2375 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2376 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2381 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2389 if (!(d = opendir(path))) {
2390 if (errno != ENOENT)
2391 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2396 while ((de = readdir(d))) {
2400 if (ignore_file(de->d_name))
2403 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2406 if (strlen(de->d_name) < 4)
2409 a = undecchar(de->d_name[1]);
2410 b = undecchar(de->d_name[2]);
2417 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2422 if (access(fpath, X_OK) < 0) {
2424 if (errno != ENOENT)
2425 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2431 if (!(name = sysv_translate_name(de->d_name + 3))) {
2436 if ((r = manager_load_unit_prepare(m, name, NULL, &service)) < 0) {
2437 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2441 if (de->d_name[0] == 'S' &&
2442 (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT))
2443 SERVICE(service)->sysv_start_priority =
2444 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2446 manager_dispatch_load_queue(m);
2447 service = unit_follow_merge(service);
2449 if (de->d_name[0] == 'S') {
2451 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2454 } else if (de->d_name[0] == 'K' &&
2455 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2456 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2458 /* We honour K links only for
2459 * halt/reboot. For the normal
2460 * runlevels we assume the
2462 * implicitly added by the
2463 * core logic. Also, we don't
2464 * really distuingish here
2465 * between the runlevels 0 and
2466 * 6 and just add them to the
2467 * special shutdown target. On
2468 * SUSE the boot.d/ runlevel
2469 * is also used for shutdown,
2470 * so we add links for that
2471 * too to the shutdown
2474 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2493 static void service_bus_name_owner_change(
2496 const char *old_owner,
2497 const char *new_owner) {
2499 Service *s = SERVICE(u);
2504 assert(streq(s->bus_name, name));
2505 assert(old_owner || new_owner);
2507 if (old_owner && new_owner)
2508 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2510 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2512 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2514 s->bus_name_good = !!new_owner;
2516 if (s->type == SERVICE_DBUS) {
2518 /* service_enter_running() will figure out what to
2520 if (s->state == SERVICE_RUNNING)
2521 service_enter_running(s, true);
2522 else if (s->state == SERVICE_START && new_owner)
2523 service_enter_start_post(s);
2525 } else if (new_owner &&
2527 (s->state == SERVICE_START ||
2528 s->state == SERVICE_START_POST ||
2529 s->state == SERVICE_RUNNING ||
2530 s->state == SERVICE_RELOAD)) {
2532 /* Try to acquire PID from bus service */
2533 log_debug("Trying to acquire PID from D-Bus name...");
2535 bus_query_pid(u->meta.manager, name);
2539 static void service_bus_query_pid_done(
2544 Service *s = SERVICE(u);
2549 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2551 if (s->main_pid <= 0 &&
2552 (s->state == SERVICE_START ||
2553 s->state == SERVICE_START_POST ||
2554 s->state == SERVICE_RUNNING ||
2555 s->state == SERVICE_RELOAD))
2556 service_set_main_pid(s, pid);
2559 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2563 /* This is called by the socket code when instantiating a new
2564 * service for a stream socket and the socket needs to be
2567 if (s->meta.load_state != UNIT_LOADED)
2570 if (s->socket_fd >= 0)
2573 if (s->state != SERVICE_DEAD)
2577 s->got_socket_fd = true;
2583 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2584 [SERVICE_DEAD] = "dead",
2585 [SERVICE_START_PRE] = "start-pre",
2586 [SERVICE_START] = "start",
2587 [SERVICE_START_POST] = "start-post",
2588 [SERVICE_RUNNING] = "running",
2589 [SERVICE_EXITED] = "exited",
2590 [SERVICE_RELOAD] = "reload",
2591 [SERVICE_STOP] = "stop",
2592 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2593 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2594 [SERVICE_STOP_POST] = "stop-post",
2595 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2596 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2597 [SERVICE_MAINTENANCE] = "maintenance",
2598 [SERVICE_AUTO_RESTART] = "auto-restart",
2601 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2603 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2604 [SERVICE_ONCE] = "once",
2605 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2606 [SERVICE_RESTART_ALWAYS] = "restart-always",
2609 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2611 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2612 [SERVICE_SIMPLE] = "simple",
2613 [SERVICE_FORKING] = "forking",
2614 [SERVICE_FINISH] = "finish",
2615 [SERVICE_DBUS] = "dbus",
2616 [SERVICE_NOTIFY] = "notify"
2619 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2621 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2622 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2623 [SERVICE_EXEC_START] = "ExecStart",
2624 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2625 [SERVICE_EXEC_RELOAD] = "ExecReload",
2626 [SERVICE_EXEC_STOP] = "ExecStop",
2627 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2630 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2632 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2633 [NOTIFY_NONE] = "none",
2634 [NOTIFY_MAIN] = "main",
2635 [NOTIFY_ALL] = "all"
2638 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2640 const UnitVTable service_vtable = {
2641 .suffix = ".service",
2643 .init = service_init,
2644 .done = service_done,
2645 .load = service_load,
2647 .coldplug = service_coldplug,
2649 .dump = service_dump,
2651 .start = service_start,
2652 .stop = service_stop,
2653 .reload = service_reload,
2655 .can_reload = service_can_reload,
2657 .serialize = service_serialize,
2658 .deserialize_item = service_deserialize_item,
2660 .active_state = service_active_state,
2661 .sub_state_to_string = service_sub_state_to_string,
2663 .check_gc = service_check_gc,
2664 .check_snapshot = service_check_snapshot,
2666 .sigchld_event = service_sigchld_event,
2667 .timer_event = service_timer_event,
2669 .cgroup_notify_empty = service_cgroup_notify_event,
2670 .notify_message = service_notify_message,
2672 .bus_name_owner_change = service_bus_name_owner_change,
2673 .bus_query_pid_done = service_bus_query_pid_done,
2675 .bus_message_handler = bus_service_message_handler,
2677 .enumerate = service_enumerate