1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "load-fragment.h"
30 #include "load-dropin.h"
33 #include "unit-name.h"
34 #include "dbus-service.h"
36 #define COMMENTS "#;\n"
37 #define NEWLINES "\n\r"
40 typedef enum RunlevelType {
49 const RunlevelType type;
51 /* Standard SysV runlevels */
52 { "rc0.d", SPECIAL_RUNLEVEL0_TARGET, RUNLEVEL_DOWN },
53 { "rc1.d", SPECIAL_RUNLEVEL1_TARGET, RUNLEVEL_UP },
54 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
55 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
56 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
57 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
58 { "rc6.d", SPECIAL_RUNLEVEL6_TARGET, RUNLEVEL_DOWN },
60 /* SUSE style boot.d */
61 { "boot.d", SPECIAL_BASIC_TARGET, RUNLEVEL_BASIC },
63 /* Debian style rcS.d */
64 { "rcS.d", SPECIAL_BASIC_TARGET, RUNLEVEL_BASIC },
67 #define RUNLEVELS_UP "12345"
68 /* #define RUNLEVELS_DOWN "06" */
69 /* #define RUNLEVELS_BOOT "bBsS" */
71 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
72 [SERVICE_DEAD] = UNIT_INACTIVE,
73 [SERVICE_START_PRE] = UNIT_ACTIVATING,
74 [SERVICE_START] = UNIT_ACTIVATING,
75 [SERVICE_START_POST] = UNIT_ACTIVATING,
76 [SERVICE_RUNNING] = UNIT_ACTIVE,
77 [SERVICE_EXITED] = UNIT_ACTIVE,
78 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
79 [SERVICE_STOP] = UNIT_DEACTIVATING,
80 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
81 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
82 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
83 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
84 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
85 [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
86 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
89 static void service_init(Unit *u) {
90 Service *s = SERVICE(u);
93 assert(u->meta.load_state == UNIT_STUB);
95 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
96 s->restart_usec = DEFAULT_RESTART_USEC;
97 s->timer_watch.type = WATCH_INVALID;
98 s->sysv_start_priority = -1;
101 exec_context_init(&s->exec_context);
103 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
105 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
108 static void service_unwatch_control_pid(Service *s) {
111 if (s->control_pid <= 0)
114 unit_unwatch_pid(UNIT(s), s->control_pid);
118 static void service_unwatch_main_pid(Service *s) {
121 if (s->main_pid <= 0)
124 unit_unwatch_pid(UNIT(s), s->main_pid);
128 static void service_close_socket_fd(Service *s) {
131 if (s->socket_fd < 0)
134 close_nointr_nofail(s->socket_fd);
138 static void service_done(Unit *u) {
139 Service *s = SERVICE(u);
149 free(s->sysv_runlevels);
150 s->sysv_runlevels = NULL;
152 exec_context_done(&s->exec_context);
153 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
154 s->control_command = NULL;
156 /* This will leak a process, but at least no memory or any of
158 service_unwatch_main_pid(s);
159 service_unwatch_control_pid(s);
162 unit_unwatch_bus_name(UNIT(u), s->bus_name);
167 service_close_socket_fd(s);
169 unit_unwatch_timer(u, &s->timer_watch);
172 static char *sysv_translate_name(const char *name) {
175 if (!(r = new(char, strlen(name) + sizeof(".service"))))
178 if (startswith(name, "boot."))
179 /* Drop SuSE-style boot. prefix */
180 strcpy(stpcpy(r, name + 5), ".service");
181 else if (endswith(name, ".sh"))
182 /* Drop Debian-style .sh suffix */
183 strcpy(stpcpy(r, name) - 3, ".service");
185 /* Normal init scripts */
186 strcpy(stpcpy(r, name), ".service");
191 static int sysv_translate_facility(const char *name, char **_r) {
193 static const char * const table[] = {
194 /* LSB defined facilities */
195 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
196 "$network", SPECIAL_NETWORK_TARGET,
197 "$named", SPECIAL_NSS_LOOKUP_TARGET,
198 "$portmap", SPECIAL_RPCBIND_TARGET,
199 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
200 "$syslog", SPECIAL_SYSLOG_TARGET,
201 "$time", SPECIAL_RTC_SET_TARGET,
203 /* Debian extensions */
204 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
205 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
206 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
212 for (i = 0; i < ELEMENTSOF(table); i += 2)
213 if (streq(table[i], name)) {
214 if (!(r = strdup(table[i+1])))
223 if (!(r = sysv_translate_name(name)))
234 static int sysv_fix_order(Service *s) {
240 if (s->sysv_start_priority < 0)
243 /* For each pair of services where at least one lacks a LSB
244 * header, we use the start priority value to order things. */
246 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
250 t = (Service*) other;
255 if (t->sysv_start_priority < 0)
258 /* If both units have modern headers we don't care
259 * about the priorities */
260 if ((!s->sysv_path || s->sysv_has_lsb) &&
261 (!t->sysv_path || t->sysv_has_lsb))
264 if (t->sysv_start_priority < s->sysv_start_priority)
266 else if (t->sysv_start_priority > s->sysv_start_priority)
271 /* FIXME: Maybe we should compare the name here lexicographically? */
273 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
280 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
283 if (!(c = new0(ExecCommand, 1)))
286 if (!(c->path = strdup(path))) {
291 if (!(c->argv = strv_new(path, arg1, NULL))) {
300 static int sysv_exec_commands(Service *s) {
304 assert(s->sysv_path);
306 if (!(c = exec_command_new(s->sysv_path, "start")))
308 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
310 if (!(c = exec_command_new(s->sysv_path, "stop")))
312 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
314 if (!(c = exec_command_new(s->sysv_path, "reload")))
316 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
321 static int service_load_sysv_path(Service *s, const char *path) {
338 if (!(f = fopen(path, "re"))) {
339 r = errno == ENOENT ? 0 : -errno;
343 s->type = SERVICE_FORKING;
344 s->restart = SERVICE_ONCE;
347 if (!(s->sysv_path = strdup(path))) {
353 char l[LINE_MAX], *t;
355 if (!fgets(l, sizeof(l), f)) {
360 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
370 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
372 s->sysv_has_lsb = true;
376 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
382 t += strspn(t, WHITESPACE);
384 if (state == NORMAL) {
386 /* Try to parse Red Hat style chkconfig headers */
388 if (startswith_no_case(t, "chkconfig:")) {
390 char runlevels[16], *k;
394 if (sscanf(t+10, "%15s %i %*i",
396 &start_priority) != 2) {
398 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
402 /* A start priority gathered from the
403 * symlink farms is preferred over the
404 * data from the LSB header. */
405 if (start_priority < 0 || start_priority > 99)
406 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
407 else if (s->sysv_start_priority < 0)
408 s->sysv_start_priority = start_priority;
410 char_array_0(runlevels);
411 k = delete_chars(runlevels, WHITESPACE "-");
416 if (!(d = strdup(k))) {
421 free(s->sysv_runlevels);
422 s->sysv_runlevels = d;
425 } else if (startswith_no_case(t, "description:")) {
427 size_t k = strlen(t);
430 if (t[k-1] == '\\') {
435 if (!(d = strdup(strstrip(t+12)))) {
440 free(u->meta.description);
441 u->meta.description = d;
443 } else if (startswith_no_case(t, "pidfile:")) {
450 if (!path_is_absolute(fn)) {
451 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
455 if (!(fn = strdup(fn))) {
464 } else if (state == DESCRIPTION) {
466 /* Try to parse Red Hat style description
469 size_t k = strlen(t);
477 assert(u->meta.description);
478 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
483 free(u->meta.description);
484 u->meta.description = d;
486 } else if (state == LSB || state == LSB_DESCRIPTION) {
488 if (startswith_no_case(t, "Provides:")) {
494 FOREACH_WORD(w, z, t+9, i) {
497 if (!(n = strndup(w, z))) {
502 r = sysv_translate_facility(n, &m);
511 if (unit_name_to_type(m) == UNIT_SERVICE)
512 r = unit_add_name(u, m);
514 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL, true)) >= 0)
515 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
524 } else if (startswith_no_case(t, "Required-Start:") ||
525 startswith_no_case(t, "Should-Start:") ||
526 startswith_no_case(t, "X-Start-Before:") ||
527 startswith_no_case(t, "X-Start-After:")) {
533 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
536 if (!(n = strndup(w, z))) {
541 r = sysv_translate_facility(n, &m);
550 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
556 } else if (startswith_no_case(t, "Default-Start:")) {
561 k = delete_chars(t+14, WHITESPACE "-");
564 if (!(d = strdup(k))) {
569 free(s->sysv_runlevels);
570 s->sysv_runlevels = d;
573 } else if (startswith_no_case(t, "Description:")) {
576 state = LSB_DESCRIPTION;
578 if (!(d = strdup(strstrip(t+12)))) {
583 free(u->meta.description);
584 u->meta.description = d;
586 } else if (startswith_no_case(t, "Short-Description:") &&
587 !u->meta.description) {
590 /* We use the short description only
591 * if no long description is set. */
595 if (!(d = strdup(strstrip(t+18)))) {
600 u->meta.description = d;
602 } else if (startswith_no_case(t, "X-Interactive:")) {
605 if ((b = parse_boolean(strstrip(t+14))) < 0) {
606 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
611 s->exec_context.std_input = EXEC_INPUT_TTY;
613 s->exec_context.std_input = EXEC_INPUT_NULL;
615 } else if (state == LSB_DESCRIPTION) {
617 if (startswith(l, "#\t") || startswith(l, "# ")) {
620 assert(u->meta.description);
621 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
626 free(u->meta.description);
627 u->meta.description = d;
634 if ((r = sysv_exec_commands(s)) < 0)
637 if (!s->sysv_runlevels || chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
638 /* If there a runlevels configured for this service
639 * but none of the standard ones, then we assume this
640 * is some special kind of service (which might be
641 * needed for early boot) and don't create any links
644 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0 ||
645 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
649 /* Don't timeout special services during boot (like fsck) */
652 /* Special setting for all SysV services */
653 s->valid_no_process = true;
654 s->kill_mode = KILL_PROCESS_GROUP;
656 u->meta.load_state = UNIT_LOADED;
667 static int service_load_sysv_name(Service *s, const char *name) {
673 /* For SysV services we strip the boot. or .sh
674 * prefixes/suffixes. */
675 if (startswith(name, "boot.") ||
676 endswith(name, ".sh.service"))
679 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
683 if (asprintf(&path, "%s/%s", *p, name) < 0)
686 assert(endswith(path, ".service"));
687 path[strlen(path)-8] = 0;
689 r = service_load_sysv_path(s, path);
691 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
692 /* Try Debian style xxx.sh source'able init scripts */
694 r = service_load_sysv_path(s, path);
699 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
700 /* Try SUSE style boot.xxx init scripts */
702 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
705 path[strlen(path)-8] = 0;
706 r = service_load_sysv_path(s, path);
713 if ((UNIT(s)->meta.load_state != UNIT_STUB))
720 static int service_load_sysv(Service *s) {
727 /* Load service data from SysV init scripts, preferably with
730 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
733 if ((t = UNIT(s)->meta.id))
734 if ((r = service_load_sysv_name(s, t)) < 0)
737 if (UNIT(s)->meta.load_state == UNIT_STUB)
738 SET_FOREACH(t, UNIT(s)->meta.names, i) {
739 if (t == UNIT(s)->meta.id)
742 if ((r == service_load_sysv_name(s, t)) < 0)
745 if (UNIT(s)->meta.load_state != UNIT_STUB)
752 static int service_add_bus_name(Service *s) {
759 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
762 r = unit_merge_by_name(UNIT(s), n);
768 static int service_verify(Service *s) {
771 if (UNIT(s)->meta.load_state != UNIT_LOADED)
774 if (!s->exec_command[SERVICE_EXEC_START]) {
775 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
779 if (s->type == SERVICE_DBUS && !s->bus_name) {
780 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->meta.id);
787 static int service_load(Unit *u) {
789 Service *s = SERVICE(u);
793 /* Load a .service file */
794 if ((r = unit_load_fragment(u)) < 0)
797 /* Load a classic init script as a fallback, if we couldn't find anything */
798 if (u->meta.load_state == UNIT_STUB)
799 if ((r = service_load_sysv(s)) < 0)
802 /* Still nothing found? Then let's give up */
803 if (u->meta.load_state == UNIT_STUB)
806 /* We were able to load something, then let's add in the
807 * dropin directories. */
808 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
811 /* This is a new unit? Then let's add in some extras */
812 if (u->meta.load_state == UNIT_LOADED) {
813 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
816 if ((r = unit_add_default_cgroup(u)) < 0)
819 if ((r = sysv_fix_order(s)) < 0)
823 if ((r = service_add_bus_name(s)) < 0)
826 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
831 return service_verify(s);
834 static void service_dump(Unit *u, FILE *f, const char *prefix) {
836 ServiceExecCommand c;
837 Service *s = SERVICE(u);
843 p2 = strappend(prefix, "\t");
844 prefix2 = p2 ? p2 : prefix;
847 "%sService State: %s\n"
848 "%sPermissionsStartOnly: %s\n"
849 "%sRootDirectoryStartOnly: %s\n"
850 "%sValidNoProcess: %s\n"
853 prefix, service_state_to_string(s->state),
854 prefix, yes_no(s->permissions_start_only),
855 prefix, yes_no(s->root_directory_start_only),
856 prefix, yes_no(s->valid_no_process),
857 prefix, kill_mode_to_string(s->kill_mode),
858 prefix, service_type_to_string(s->type));
860 if (s->control_pid > 0)
862 "%sControl PID: %llu\n",
863 prefix, (unsigned long long) s->control_pid);
867 "%sMain PID: %llu\n",
868 prefix, (unsigned long long) s->main_pid);
873 prefix, s->pid_file);
878 "%sBus Name Good: %s\n",
880 prefix, yes_no(s->bus_name_good));
882 exec_context_dump(&s->exec_context, f, prefix);
884 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
886 if (!s->exec_command[c])
889 fprintf(f, "%s-> %s:\n",
890 prefix, service_exec_command_to_string(c));
892 exec_command_dump_list(s->exec_command[c], f, prefix2);
897 "%sSysV Init Script Path: %s\n"
898 "%sSysV Init Script has LSB Header: %s\n",
899 prefix, s->sysv_path,
900 prefix, yes_no(s->sysv_has_lsb));
902 if (s->sysv_start_priority >= 0)
904 "%sSysVStartPriority: %i\n",
905 prefix, s->sysv_start_priority);
907 if (s->sysv_runlevels)
908 fprintf(f, "%sSysVRunLevels: %s\n",
909 prefix, s->sysv_runlevels);
914 static int service_load_pid_file(Service *s) {
921 if (s->main_pid_known)
924 assert(s->main_pid <= 0);
929 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
932 if ((r = safe_atolu(k, &p)) < 0) {
937 if ((unsigned long) (pid_t) p != p)
940 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
941 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
942 (unsigned long long) p, s->pid_file);
946 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
947 /* FIXME: we need to do something here */
950 s->main_pid = (pid_t) p;
951 s->main_pid_known = true;
956 static int service_get_sockets(Service *s, Set **_set) {
965 /* Collects all Socket objects that belong to this
966 * service. Note that a service might have multiple sockets
967 * via multiple names. */
969 if (!(set = set_new(NULL, NULL)))
972 SET_FOREACH(t, UNIT(s)->meta.names, i) {
976 /* Look for all socket objects that go by any of our
977 * units and collect their fds */
979 if (!(k = unit_name_change_suffix(t, ".socket"))) {
984 p = manager_get_unit(UNIT(s)->meta.manager, k);
990 if ((r = set_put(set, p)) < 0)
1002 static int service_notify_sockets_dead(Service *s) {
1010 /* Notifies all our sockets when we die */
1011 if ((r = service_get_sockets(s, &set)) < 0)
1014 SET_FOREACH(sock, set, i)
1015 socket_notify_service_dead(sock);
1022 static void service_set_state(Service *s, ServiceState state) {
1023 ServiceState old_state;
1026 old_state = s->state;
1029 if (state != SERVICE_START_PRE &&
1030 state != SERVICE_START &&
1031 state != SERVICE_START_POST &&
1032 state != SERVICE_RELOAD &&
1033 state != SERVICE_STOP &&
1034 state != SERVICE_STOP_SIGTERM &&
1035 state != SERVICE_STOP_SIGKILL &&
1036 state != SERVICE_STOP_POST &&
1037 state != SERVICE_FINAL_SIGTERM &&
1038 state != SERVICE_FINAL_SIGKILL &&
1039 state != SERVICE_AUTO_RESTART)
1040 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1042 if (state != SERVICE_START &&
1043 state != SERVICE_START_POST &&
1044 state != SERVICE_RUNNING &&
1045 state != SERVICE_RELOAD &&
1046 state != SERVICE_STOP &&
1047 state != SERVICE_STOP_SIGTERM &&
1048 state != SERVICE_STOP_SIGKILL)
1049 service_unwatch_main_pid(s);
1051 if (state != SERVICE_START_PRE &&
1052 state != SERVICE_START &&
1053 state != SERVICE_START_POST &&
1054 state != SERVICE_RELOAD &&
1055 state != SERVICE_STOP &&
1056 state != SERVICE_STOP_SIGTERM &&
1057 state != SERVICE_STOP_SIGKILL &&
1058 state != SERVICE_STOP_POST &&
1059 state != SERVICE_FINAL_SIGTERM &&
1060 state != SERVICE_FINAL_SIGKILL) {
1061 service_unwatch_control_pid(s);
1062 s->control_command = NULL;
1063 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1066 if (state == SERVICE_DEAD ||
1067 state == SERVICE_STOP ||
1068 state == SERVICE_STOP_SIGTERM ||
1069 state == SERVICE_STOP_SIGKILL ||
1070 state == SERVICE_STOP_POST ||
1071 state == SERVICE_FINAL_SIGTERM ||
1072 state == SERVICE_FINAL_SIGKILL ||
1073 state == SERVICE_MAINTAINANCE ||
1074 state == SERVICE_AUTO_RESTART)
1075 service_notify_sockets_dead(s);
1077 if (state != SERVICE_START_PRE &&
1078 state != SERVICE_START &&
1079 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1080 service_close_socket_fd(s);
1082 if (old_state != state)
1083 log_debug("%s changed %s -> %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1085 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1088 static int service_coldplug(Unit *u) {
1089 Service *s = SERVICE(u);
1093 assert(s->state == SERVICE_DEAD);
1095 if (s->deserialized_state != s->state) {
1097 if (s->deserialized_state == SERVICE_START_PRE ||
1098 s->deserialized_state == SERVICE_START ||
1099 s->deserialized_state == SERVICE_START_POST ||
1100 s->deserialized_state == SERVICE_RELOAD ||
1101 s->deserialized_state == SERVICE_STOP ||
1102 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1103 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1104 s->deserialized_state == SERVICE_STOP_POST ||
1105 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1106 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1107 s->deserialized_state == SERVICE_AUTO_RESTART) {
1109 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1112 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1114 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1119 if ((s->deserialized_state == SERVICE_START &&
1120 (s->type == SERVICE_FORKING ||
1121 s->type == SERVICE_DBUS)) ||
1122 s->deserialized_state == SERVICE_START_POST ||
1123 s->deserialized_state == SERVICE_RUNNING ||
1124 s->deserialized_state == SERVICE_RELOAD ||
1125 s->deserialized_state == SERVICE_STOP ||
1126 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1127 s->deserialized_state == SERVICE_STOP_SIGKILL)
1128 if (s->main_pid > 0)
1129 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1132 if (s->deserialized_state == SERVICE_START_PRE ||
1133 s->deserialized_state == SERVICE_START ||
1134 s->deserialized_state == SERVICE_START_POST ||
1135 s->deserialized_state == SERVICE_RELOAD ||
1136 s->deserialized_state == SERVICE_STOP ||
1137 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1138 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1139 s->deserialized_state == SERVICE_STOP_POST ||
1140 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1141 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1142 if (s->control_pid > 0)
1143 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1146 service_set_state(s, s->deserialized_state);
1152 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1156 unsigned rn_fds = 0;
1164 if ((r = service_get_sockets(s, &set)) < 0)
1167 SET_FOREACH(sock, set, i) {
1171 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1183 if (!(t = new(int, rn_fds+cn_fds))) {
1189 memcpy(t, rfds, rn_fds);
1190 memcpy(t+rn_fds, cfds, cn_fds);
1195 rn_fds = rn_fds+cn_fds;
1213 static int service_spawn(
1218 bool apply_permissions,
1233 if (s->socket_fd >= 0) {
1234 fds = &s->socket_fd;
1236 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1240 if (timeout && s->timeout_usec) {
1241 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1244 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1246 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1255 s->meta.manager->environment,
1258 UNIT(s)->meta.manager->confirm_spawn,
1259 UNIT(s)->meta.cgroup_bondings,
1267 if (s->socket_fd >= 0)
1268 service_close_socket_fd(s);
1273 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1274 /* FIXME: we need to do something here */
1285 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1290 static int main_pid_good(Service *s) {
1293 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1296 /* If we know the pid file, then lets just check if it is
1298 if (s->main_pid_known)
1299 return s->main_pid > 0;
1301 /* We don't know the pid */
1305 static int control_pid_good(Service *s) {
1308 return s->control_pid > 0;
1311 static int cgroup_good(Service *s) {
1316 if (s->valid_no_process)
1319 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1325 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1332 if (allow_restart &&
1334 (s->restart == SERVICE_RESTART_ALWAYS ||
1335 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1337 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1340 service_set_state(s, SERVICE_AUTO_RESTART);
1342 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1347 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
1348 service_enter_dead(s, false, false);
1351 static void service_enter_signal(Service *s, ServiceState state, bool success);
1353 static void service_enter_stop_post(Service *s, bool success) {
1360 service_unwatch_control_pid(s);
1362 s->control_command_id = SERVICE_EXEC_STOP_POST;
1363 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1364 if ((r = service_spawn(s,
1368 !s->permissions_start_only,
1369 !s->root_directory_start_only,
1370 &s->control_pid)) < 0)
1374 service_set_state(s, SERVICE_STOP_POST);
1376 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1381 log_warning("%s failed to run stop-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1382 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1385 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1394 if (s->kill_mode != KILL_NONE) {
1395 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1397 if (s->kill_mode == KILL_CONTROL_GROUP) {
1399 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1400 if (r != -EAGAIN && r != -ESRCH)
1409 if (s->main_pid > 0) {
1410 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1416 if (s->control_pid > 0) {
1417 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1428 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1429 if (s->timeout_usec > 0)
1430 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1433 service_set_state(s, state);
1434 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1435 service_enter_stop_post(s, true);
1437 service_enter_dead(s, true, true);
1442 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
1444 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1445 service_enter_stop_post(s, false);
1447 service_enter_dead(s, false, true);
1450 static void service_enter_stop(Service *s, bool success) {
1457 service_unwatch_control_pid(s);
1459 s->control_command_id = SERVICE_EXEC_STOP;
1460 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1461 if ((r = service_spawn(s,
1465 !s->permissions_start_only,
1466 !s->root_directory_start_only,
1467 &s->control_pid)) < 0)
1470 service_set_state(s, SERVICE_STOP);
1472 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1477 log_warning("%s failed to run stop executable: %s", UNIT(s)->meta.id, strerror(-r));
1478 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1481 static void service_enter_running(Service *s, bool success) {
1487 if (main_pid_good(s) != 0 &&
1488 cgroup_good(s) != 0 &&
1489 (s->bus_name_good || s->type != SERVICE_DBUS))
1490 service_set_state(s, SERVICE_RUNNING);
1491 else if (s->valid_no_process)
1492 service_set_state(s, SERVICE_EXITED);
1494 service_enter_stop(s, true);
1497 static void service_enter_start_post(Service *s) {
1501 service_unwatch_control_pid(s);
1503 s->control_command_id = SERVICE_EXEC_START_POST;
1504 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1505 if ((r = service_spawn(s,
1509 !s->permissions_start_only,
1510 !s->root_directory_start_only,
1511 &s->control_pid)) < 0)
1515 service_set_state(s, SERVICE_START_POST);
1517 service_enter_running(s, true);
1522 log_warning("%s failed to run start-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1523 service_enter_stop(s, false);
1526 static void service_enter_start(Service *s) {
1532 assert(s->exec_command[SERVICE_EXEC_START]);
1533 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1535 if (s->type == SERVICE_FORKING)
1536 service_unwatch_control_pid(s);
1538 service_unwatch_main_pid(s);
1540 if ((r = service_spawn(s,
1541 s->exec_command[SERVICE_EXEC_START],
1542 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS,
1549 if (s->type == SERVICE_SIMPLE) {
1550 /* For simple services we immediately start
1551 * the START_POST binaries. */
1554 s->main_pid_known = true;
1556 service_enter_start_post(s);
1558 } else if (s->type == SERVICE_FORKING) {
1560 /* For forking services we wait until the start
1561 * process exited. */
1563 s->control_pid = pid;
1565 s->control_command_id = SERVICE_EXEC_START;
1566 s->control_command = s->exec_command[SERVICE_EXEC_START];
1567 service_set_state(s, SERVICE_START);
1569 } else if (s->type == SERVICE_FINISH ||
1570 s->type == SERVICE_DBUS) {
1572 /* For finishing services we wait until the start
1573 * process exited, too, but it is our main process. */
1575 /* For D-Bus services we know the main pid right away,
1576 * but wait for the bus name to appear on the bus. */
1579 s->main_pid_known = true;
1581 service_set_state(s, SERVICE_START);
1583 assert_not_reached("Unknown service type");
1588 log_warning("%s failed to run start exectuable: %s", UNIT(s)->meta.id, strerror(-r));
1589 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1592 static void service_enter_start_pre(Service *s) {
1597 service_unwatch_control_pid(s);
1599 s->control_command_id = SERVICE_EXEC_START_PRE;
1600 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1601 if ((r = service_spawn(s,
1605 !s->permissions_start_only,
1606 !s->root_directory_start_only,
1607 &s->control_pid)) < 0)
1610 service_set_state(s, SERVICE_START_PRE);
1612 service_enter_start(s);
1617 log_warning("%s failed to run start-pre executable: %s", UNIT(s)->meta.id, strerror(-r));
1618 service_enter_dead(s, false, true);
1621 static void service_enter_restart(Service *s) {
1625 service_enter_dead(s, true, false);
1627 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1630 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
1635 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
1636 service_enter_dead(s, false, false);
1639 static void service_enter_reload(Service *s) {
1644 service_unwatch_control_pid(s);
1646 s->control_command_id = SERVICE_EXEC_RELOAD;
1647 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1648 if ((r = service_spawn(s,
1652 !s->permissions_start_only,
1653 !s->root_directory_start_only,
1654 &s->control_pid)) < 0)
1657 service_set_state(s, SERVICE_RELOAD);
1659 service_enter_running(s, true);
1664 log_warning("%s failed to run reload executable: %s", UNIT(s)->meta.id, strerror(-r));
1665 service_enter_stop(s, false);
1668 static void service_run_next(Service *s, bool success) {
1672 assert(s->control_command);
1673 assert(s->control_command->command_next);
1678 s->control_command = s->control_command->command_next;
1680 service_unwatch_control_pid(s);
1682 if ((r = service_spawn(s,
1686 !s->permissions_start_only,
1687 !s->root_directory_start_only,
1688 &s->control_pid)) < 0)
1694 log_warning("%s failed to run spawn next executable: %s", UNIT(s)->meta.id, strerror(-r));
1696 if (s->state == SERVICE_START_PRE)
1697 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1698 else if (s->state == SERVICE_STOP)
1699 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1700 else if (s->state == SERVICE_STOP_POST)
1701 service_enter_dead(s, false, true);
1703 service_enter_stop(s, false);
1706 static int service_start(Unit *u) {
1707 Service *s = SERVICE(u);
1711 /* We cannot fulfill this request right now, try again later
1713 if (s->state == SERVICE_STOP ||
1714 s->state == SERVICE_STOP_SIGTERM ||
1715 s->state == SERVICE_STOP_SIGKILL ||
1716 s->state == SERVICE_STOP_POST ||
1717 s->state == SERVICE_FINAL_SIGTERM ||
1718 s->state == SERVICE_FINAL_SIGKILL)
1721 /* Already on it! */
1722 if (s->state == SERVICE_START_PRE ||
1723 s->state == SERVICE_START ||
1724 s->state == SERVICE_START_POST)
1727 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1729 /* Make sure we don't enter a busy loop of some kind. */
1730 if (!ratelimit_test(&s->ratelimit)) {
1731 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1736 s->main_pid_known = false;
1737 s->allow_restart = true;
1739 service_enter_start_pre(s);
1743 static int service_stop(Unit *u) {
1744 Service *s = SERVICE(u);
1748 /* Cannot do this now */
1749 if (s->state == SERVICE_START_PRE ||
1750 s->state == SERVICE_START ||
1751 s->state == SERVICE_START_POST ||
1752 s->state == SERVICE_RELOAD)
1756 if (s->state == SERVICE_STOP ||
1757 s->state == SERVICE_STOP_SIGTERM ||
1758 s->state == SERVICE_STOP_SIGKILL ||
1759 s->state == SERVICE_STOP_POST ||
1760 s->state == SERVICE_FINAL_SIGTERM ||
1761 s->state == SERVICE_FINAL_SIGKILL)
1764 if (s->state == SERVICE_AUTO_RESTART) {
1765 service_set_state(s, SERVICE_DEAD);
1769 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1771 /* This is a user request, so don't do restarts on this
1773 s->allow_restart = false;
1775 service_enter_stop(s, true);
1779 static int service_reload(Unit *u) {
1780 Service *s = SERVICE(u);
1784 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1786 service_enter_reload(s);
1790 static bool service_can_reload(Unit *u) {
1791 Service *s = SERVICE(u);
1795 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1798 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1799 Service *s = SERVICE(u);
1805 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1806 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1808 if (s->control_pid > 0)
1809 unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) (s->control_pid));
1811 if (s->main_pid > 0)
1812 unit_serialize_item_format(u, f, "main-pid", "%u", (unsigned) (s->main_pid));
1814 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1816 /* There's a minor uncleanliness here: if there are multiple
1817 * commands attached here, we will start from the first one
1819 if (s->control_command_id >= 0)
1820 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
1822 if (s->socket_fd >= 0) {
1825 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1828 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1834 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1835 Service *s = SERVICE(u);
1843 if (streq(key, "state")) {
1846 if ((state = service_state_from_string(value)) < 0)
1847 log_debug("Failed to parse state value %s", value);
1849 s->deserialized_state = state;
1850 } else if (streq(key, "failure")) {
1853 if ((b = parse_boolean(value)) < 0)
1854 log_debug("Failed to parse failure value %s", value);
1856 s->failure = b || s->failure;
1857 } else if (streq(key, "control-pid")) {
1860 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1861 log_debug("Failed to parse control-pid value %s", value);
1863 s->control_pid = (pid_t) pid;
1864 } else if (streq(key, "main-pid")) {
1867 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1868 log_debug("Failed to parse main-pid value %s", value);
1870 s->main_pid = (pid_t) pid;
1871 } else if (streq(key, "main-pid-known")) {
1874 if ((b = parse_boolean(value)) < 0)
1875 log_debug("Failed to parse main-pid-known value %s", value);
1877 s->main_pid_known = b;
1878 } else if (streq(key, "control-command")) {
1879 ServiceExecCommand id;
1881 if ((id = service_exec_command_from_string(value)) < 0)
1882 log_debug("Failed to parse exec-command value %s", value);
1884 s->control_command_id = id;
1885 s->control_command = s->exec_command[id];
1887 } else if (streq(key, "socket-fd")) {
1890 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1891 log_debug("Failed to parse socket-fd value %s", value);
1894 if (s->socket_fd >= 0)
1895 close_nointr_nofail(s->socket_fd);
1896 s->socket_fd = fdset_remove(fds, fd);
1899 log_debug("Unknown serialization key '%s'", key);
1904 static UnitActiveState service_active_state(Unit *u) {
1907 return state_translation_table[SERVICE(u)->state];
1910 static const char *service_sub_state_to_string(Unit *u) {
1913 return service_state_to_string(SERVICE(u)->state);
1916 static bool service_check_gc(Unit *u) {
1917 Service *s = SERVICE(u);
1921 return !!s->sysv_path;
1924 static bool service_check_snapshot(Unit *u) {
1925 Service *s = SERVICE(u);
1929 return !s->got_socket_fd;
1932 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1933 Service *s = SERVICE(u);
1939 success = is_clean_exit(code, status);
1940 s->failure = s->failure || !success;
1942 if (s->main_pid == pid) {
1944 exec_status_fill(&s->main_exec_status, pid, code, status);
1947 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1948 assert(s->exec_command[SERVICE_EXEC_START]);
1949 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1952 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
1954 /* The service exited, so the service is officially
1959 case SERVICE_START_POST:
1960 case SERVICE_RELOAD:
1962 /* Need to wait until the operation is
1967 assert(s->type == SERVICE_FINISH);
1969 /* This was our main goal, so let's go on */
1971 service_enter_start_post(s);
1973 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1976 case SERVICE_RUNNING:
1977 service_enter_running(s, success);
1980 case SERVICE_STOP_SIGTERM:
1981 case SERVICE_STOP_SIGKILL:
1983 if (!control_pid_good(s))
1984 service_enter_stop_post(s, success);
1986 /* If there is still a control process, wait for that first */
1990 assert_not_reached("Uh, main process died at wrong time.");
1993 } else if (s->control_pid == pid) {
1995 if (s->control_command)
1996 exec_status_fill(&s->control_command->exec_status, pid, code, status);
2000 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2002 /* If we are shutting things down anyway we
2003 * don't care about failing commands. */
2005 if (s->control_command && s->control_command->command_next && success) {
2007 /* There is another command to *
2008 * execute, so let's do that. */
2010 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
2011 service_run_next(s, success);
2014 /* No further commands for this step, so let's
2015 * figure out what to do next */
2017 s->control_command = NULL;
2018 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2020 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2024 case SERVICE_START_PRE:
2026 service_enter_start(s);
2028 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2032 assert(s->type == SERVICE_FORKING);
2034 /* Let's try to load the pid
2035 * file here if we can. We
2036 * ignore the return value,
2037 * since the PID file might
2038 * actually be created by a
2039 * START_POST script */
2043 service_load_pid_file(s);
2045 service_enter_start_post(s);
2047 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2051 case SERVICE_START_POST:
2052 if (success && s->pid_file && !s->main_pid_known) {
2055 /* Hmm, let's see if we can
2056 * load the pid now after the
2057 * start-post scripts got
2060 if ((r = service_load_pid_file(s)) < 0)
2061 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
2066 case SERVICE_RELOAD:
2068 service_enter_running(s, true);
2070 service_enter_stop(s, false);
2075 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2078 case SERVICE_STOP_SIGTERM:
2079 case SERVICE_STOP_SIGKILL:
2080 if (main_pid_good(s) <= 0)
2081 service_enter_stop_post(s, success);
2083 /* If there is still a service
2084 * process around, wait until
2085 * that one quit, too */
2088 case SERVICE_STOP_POST:
2089 case SERVICE_FINAL_SIGTERM:
2090 case SERVICE_FINAL_SIGKILL:
2091 service_enter_dead(s, success, true);
2095 assert_not_reached("Uh, control process died at wrong time.");
2099 assert_not_reached("Got SIGCHLD for unkown PID");
2102 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2103 Service *s = SERVICE(u);
2106 assert(elapsed == 1);
2108 assert(w == &s->timer_watch);
2112 case SERVICE_START_PRE:
2114 log_warning("%s operation timed out. Terminating.", u->meta.id);
2115 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2118 case SERVICE_START_POST:
2119 case SERVICE_RELOAD:
2120 log_warning("%s operation timed out. Stopping.", u->meta.id);
2121 service_enter_stop(s, false);
2125 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2126 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2129 case SERVICE_STOP_SIGTERM:
2130 log_warning("%s stopping timed out. Killing.", u->meta.id);
2131 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2134 case SERVICE_STOP_SIGKILL:
2135 /* Uh, wie sent a SIGKILL and it is still not gone?
2136 * Must be something we cannot kill, so let's just be
2137 * weirded out and continue */
2139 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2140 service_enter_stop_post(s, false);
2143 case SERVICE_STOP_POST:
2144 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2145 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2148 case SERVICE_FINAL_SIGTERM:
2149 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2150 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2153 case SERVICE_FINAL_SIGKILL:
2154 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u->meta.id);
2155 service_enter_dead(s, false, true);
2158 case SERVICE_AUTO_RESTART:
2159 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
2160 service_enter_restart(s);
2164 assert_not_reached("Timeout at wrong time.");
2168 static void service_cgroup_notify_event(Unit *u) {
2169 Service *s = SERVICE(u);
2173 log_debug("%s: cgroup is empty", u->meta.id);
2177 /* Waiting for SIGCHLD is usually more interesting,
2178 * because it includes return codes/signals. Which is
2179 * why we ignore the cgroup events for most cases,
2180 * except when we don't know pid which to expect the
2183 case SERVICE_RUNNING:
2184 service_enter_running(s, true);
2192 static int service_enumerate(Manager *m) {
2196 char *path = NULL, *fpath = NULL, *name = NULL;
2201 STRV_FOREACH(p, m->sysvrcnd_path)
2202 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2207 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2215 if (!(d = opendir(path))) {
2216 if (errno != ENOENT)
2217 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2222 while ((de = readdir(d))) {
2226 if (ignore_file(de->d_name))
2229 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2232 if (strlen(de->d_name) < 4)
2235 a = undecchar(de->d_name[1]);
2236 b = undecchar(de->d_name[2]);
2243 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2248 if (access(fpath, X_OK) < 0) {
2250 if (errno != ENOENT)
2251 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2257 if (!(name = sysv_translate_name(de->d_name + 3))) {
2262 if ((r = manager_load_unit_prepare(m, name, NULL, &service)) < 0) {
2263 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2267 if (de->d_name[0] == 'S' &&
2268 (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_BASIC))
2269 SERVICE(service)->sysv_start_priority =
2270 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2272 manager_dispatch_load_queue(m);
2273 service = unit_follow_merge(service);
2275 if (de->d_name[0] == 'S') {
2276 Unit *runlevel_target;
2278 if ((r = manager_load_unit(m, rcnd_table[i].target, NULL, &runlevel_target)) < 0)
2281 if ((r = unit_add_dependency(runlevel_target, UNIT_WANTS, service, true)) < 0)
2284 if ((r = unit_add_dependency(service, UNIT_BEFORE, runlevel_target, true)) < 0)
2287 } else if (de->d_name[0] == 'K' && rcnd_table[i].type == RUNLEVEL_DOWN) {
2288 Unit *shutdown_target;
2290 /* We honour K links only for
2291 * halt/reboot. For the normal
2292 * runlevels we assume the
2294 * implicitly added by the
2295 * core logic. Also, we don't
2296 * really distuingish here
2297 * between the runlevels 0 and
2298 * 6 and just add them to the
2299 * special shutdown target. */
2301 if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
2304 if ((r = unit_add_dependency(service, UNIT_CONFLICTS, shutdown_target, true)) < 0)
2307 if ((r = unit_add_dependency(service, UNIT_BEFORE, shutdown_target, true)) < 0)
2326 static void service_bus_name_owner_change(
2329 const char *old_owner,
2330 const char *new_owner) {
2332 Service *s = SERVICE(u);
2337 assert(streq(s->bus_name, name));
2338 assert(old_owner || new_owner);
2340 if (old_owner && new_owner)
2341 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2343 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2345 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2347 s->bus_name_good = !!new_owner;
2349 if (s->type == SERVICE_DBUS) {
2351 /* service_enter_running() will figure out what to
2353 if (s->state == SERVICE_RUNNING)
2354 service_enter_running(s, true);
2355 else if (s->state == SERVICE_START && new_owner)
2356 service_enter_start_post(s);
2358 } else if (new_owner &&
2360 (s->state == SERVICE_START ||
2361 s->state == SERVICE_START_POST ||
2362 s->state == SERVICE_RUNNING ||
2363 s->state == SERVICE_RELOAD)) {
2365 /* Try to acquire PID from bus service */
2366 log_debug("Trying to acquire PID from D-Bus name...");
2368 bus_query_pid(u->meta.manager, name);
2372 static void service_bus_query_pid_done(
2377 Service *s = SERVICE(u);
2382 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2384 if (s->main_pid <= 0 &&
2385 (s->state == SERVICE_START ||
2386 s->state == SERVICE_START_POST ||
2387 s->state == SERVICE_RUNNING ||
2388 s->state == SERVICE_RELOAD))
2392 int service_set_socket_fd(Service *s, int fd) {
2396 /* This is called by the socket code when instantiating a new
2397 * service for a stream socket and the socket needs to be
2400 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2403 if (s->socket_fd >= 0)
2406 if (s->state != SERVICE_DEAD)
2410 s->got_socket_fd = true;
2414 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2415 [SERVICE_DEAD] = "dead",
2416 [SERVICE_START_PRE] = "start-pre",
2417 [SERVICE_START] = "start",
2418 [SERVICE_START_POST] = "start-post",
2419 [SERVICE_RUNNING] = "running",
2420 [SERVICE_EXITED] = "exited",
2421 [SERVICE_RELOAD] = "reload",
2422 [SERVICE_STOP] = "stop",
2423 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2424 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2425 [SERVICE_STOP_POST] = "stop-post",
2426 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2427 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2428 [SERVICE_MAINTAINANCE] = "maintainance",
2429 [SERVICE_AUTO_RESTART] = "auto-restart",
2432 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2434 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2435 [SERVICE_ONCE] = "once",
2436 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2437 [SERVICE_RESTART_ALWAYS] = "restart-always",
2440 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2442 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2443 [SERVICE_FORKING] = "forking",
2444 [SERVICE_SIMPLE] = "simple",
2445 [SERVICE_FINISH] = "finish",
2446 [SERVICE_DBUS] = "dbus"
2449 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2451 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2452 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2453 [SERVICE_EXEC_START] = "ExecStart",
2454 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2455 [SERVICE_EXEC_RELOAD] = "ExecReload",
2456 [SERVICE_EXEC_STOP] = "ExecStop",
2457 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2460 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2462 const UnitVTable service_vtable = {
2463 .suffix = ".service",
2465 .init = service_init,
2466 .done = service_done,
2467 .load = service_load,
2469 .coldplug = service_coldplug,
2471 .dump = service_dump,
2473 .start = service_start,
2474 .stop = service_stop,
2475 .reload = service_reload,
2477 .can_reload = service_can_reload,
2479 .serialize = service_serialize,
2480 .deserialize_item = service_deserialize_item,
2482 .active_state = service_active_state,
2483 .sub_state_to_string = service_sub_state_to_string,
2485 .check_gc = service_check_gc,
2486 .check_snapshot = service_check_snapshot,
2488 .sigchld_event = service_sigchld_event,
2489 .timer_event = service_timer_event,
2491 .cgroup_notify_empty = service_cgroup_notify_event,
2493 .bus_name_owner_change = service_bus_name_owner_change,
2494 .bus_query_pid_done = service_bus_query_pid_done,
2496 .bus_message_handler = bus_service_message_handler,
2498 .enumerate = service_enumerate