1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "load-fragment.h"
30 #include "load-dropin.h"
33 #include "unit-name.h"
34 #include "dbus-service.h"
36 #define COMMENTS "#;\n"
37 #define NEWLINES "\n\r"
40 typedef enum RunlevelType {
49 const RunlevelType type;
51 /* Standard SysV runlevels */
52 { "rc0.d", SPECIAL_RUNLEVEL0_TARGET, RUNLEVEL_DOWN },
53 { "rc1.d", SPECIAL_RUNLEVEL1_TARGET, RUNLEVEL_UP },
54 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
55 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
56 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
57 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
58 { "rc6.d", SPECIAL_RUNLEVEL6_TARGET, RUNLEVEL_DOWN },
60 /* SUSE style boot.d */
61 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
63 /* Debian style rcS.d */
64 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
67 #define RUNLEVELS_UP "12345"
68 /* #define RUNLEVELS_DOWN "06" */
69 /* #define RUNLEVELS_BOOT "bBsS" */
71 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
72 [SERVICE_DEAD] = UNIT_INACTIVE,
73 [SERVICE_START_PRE] = UNIT_ACTIVATING,
74 [SERVICE_START] = UNIT_ACTIVATING,
75 [SERVICE_START_POST] = UNIT_ACTIVATING,
76 [SERVICE_RUNNING] = UNIT_ACTIVE,
77 [SERVICE_EXITED] = UNIT_ACTIVE,
78 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
79 [SERVICE_STOP] = UNIT_DEACTIVATING,
80 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
81 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
82 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
83 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
84 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
85 [SERVICE_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;
344 if (!(s->sysv_path = strdup(path))) {
350 char l[LINE_MAX], *t;
352 if (!fgets(l, sizeof(l), f)) {
357 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
367 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
369 s->sysv_has_lsb = true;
373 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
379 t += strspn(t, WHITESPACE);
381 if (state == NORMAL) {
383 /* Try to parse Red Hat style chkconfig headers */
385 if (startswith_no_case(t, "chkconfig:")) {
387 char runlevels[16], *k;
391 if (sscanf(t+10, "%15s %i %*i",
393 &start_priority) != 2) {
395 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
399 /* A start priority gathered from the
400 * symlink farms is preferred over the
401 * data from the LSB header. */
402 if (start_priority < 0 || start_priority > 99)
403 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
404 else if (s->sysv_start_priority < 0)
405 s->sysv_start_priority = start_priority;
407 char_array_0(runlevels);
408 k = delete_chars(runlevels, WHITESPACE "-");
413 if (!(d = strdup(k))) {
418 free(s->sysv_runlevels);
419 s->sysv_runlevels = d;
422 } else if (startswith_no_case(t, "description:")) {
424 size_t k = strlen(t);
427 if (t[k-1] == '\\') {
432 if (!(d = strdup(strstrip(t+12)))) {
437 free(u->meta.description);
438 u->meta.description = d;
440 } else if (startswith_no_case(t, "pidfile:")) {
447 if (!path_is_absolute(fn)) {
448 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
452 if (!(fn = strdup(fn))) {
461 } else if (state == DESCRIPTION) {
463 /* Try to parse Red Hat style description
466 size_t k = strlen(t);
474 assert(u->meta.description);
475 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
480 free(u->meta.description);
481 u->meta.description = d;
483 } else if (state == LSB || state == LSB_DESCRIPTION) {
485 if (startswith_no_case(t, "Provides:")) {
491 FOREACH_WORD(w, z, t+9, i) {
494 if (!(n = strndup(w, z))) {
499 r = sysv_translate_facility(n, &m);
508 if (unit_name_to_type(m) == UNIT_SERVICE)
509 r = unit_add_name(u, m);
511 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL, true)) >= 0)
512 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
521 } else if (startswith_no_case(t, "Required-Start:") ||
522 startswith_no_case(t, "Should-Start:") ||
523 startswith_no_case(t, "X-Start-Before:") ||
524 startswith_no_case(t, "X-Start-After:")) {
530 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
533 if (!(n = strndup(w, z))) {
538 r = sysv_translate_facility(n, &m);
547 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
553 } else if (startswith_no_case(t, "Default-Start:")) {
558 k = delete_chars(t+14, WHITESPACE "-");
561 if (!(d = strdup(k))) {
566 free(s->sysv_runlevels);
567 s->sysv_runlevels = d;
570 } else if (startswith_no_case(t, "Description:")) {
573 state = LSB_DESCRIPTION;
575 if (!(d = strdup(strstrip(t+12)))) {
580 free(u->meta.description);
581 u->meta.description = d;
583 } else if (startswith_no_case(t, "Short-Description:") &&
584 !u->meta.description) {
587 /* We use the short description only
588 * if no long description is set. */
592 if (!(d = strdup(strstrip(t+18)))) {
597 u->meta.description = d;
599 } else if (startswith_no_case(t, "X-Interactive:")) {
602 if ((b = parse_boolean(strstrip(t+14))) < 0) {
603 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
608 s->exec_context.std_input = EXEC_INPUT_TTY;
610 s->exec_context.std_input = EXEC_INPUT_NULL;
612 } else if (state == LSB_DESCRIPTION) {
614 if (startswith(l, "#\t") || startswith(l, "# ")) {
617 assert(u->meta.description);
618 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
623 free(u->meta.description);
624 u->meta.description = d;
631 if ((r = sysv_exec_commands(s)) < 0)
634 if (!s->sysv_runlevels || chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
635 /* If there a runlevels configured for this service
636 * but none of the standard ones, then we assume this
637 * is some special kind of service (which might be
638 * needed for early boot) and don't create any links
641 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0 ||
642 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
646 /* Don't timeout special services during boot (like fsck) */
649 /* Special setting for all SysV services */
650 s->type = SERVICE_FORKING;
651 s->valid_no_process = true;
652 s->kill_mode = KILL_PROCESS_GROUP;
653 s->restart = SERVICE_ONCE;
655 u->meta.load_state = UNIT_LOADED;
666 static int service_load_sysv_name(Service *s, const char *name) {
672 /* For SysV services we strip the boot. or .sh
673 * prefixes/suffixes. */
674 if (startswith(name, "boot.") ||
675 endswith(name, ".sh.service"))
678 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
682 if (asprintf(&path, "%s/%s", *p, name) < 0)
685 assert(endswith(path, ".service"));
686 path[strlen(path)-8] = 0;
688 r = service_load_sysv_path(s, path);
690 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
691 /* Try Debian style xxx.sh source'able init scripts */
693 r = service_load_sysv_path(s, path);
698 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
699 /* Try SUSE style boot.xxx init scripts */
701 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
704 path[strlen(path)-8] = 0;
705 r = service_load_sysv_path(s, path);
712 if ((UNIT(s)->meta.load_state != UNIT_STUB))
719 static int service_load_sysv(Service *s) {
726 /* Load service data from SysV init scripts, preferably with
729 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
732 if ((t = UNIT(s)->meta.id))
733 if ((r = service_load_sysv_name(s, t)) < 0)
736 if (UNIT(s)->meta.load_state == UNIT_STUB)
737 SET_FOREACH(t, UNIT(s)->meta.names, i) {
738 if (t == UNIT(s)->meta.id)
741 if ((r == service_load_sysv_name(s, t)) < 0)
744 if (UNIT(s)->meta.load_state != UNIT_STUB)
751 static int service_add_bus_name(Service *s) {
758 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
761 r = unit_merge_by_name(UNIT(s), n);
767 static int service_verify(Service *s) {
770 if (UNIT(s)->meta.load_state != UNIT_LOADED)
773 if (!s->exec_command[SERVICE_EXEC_START]) {
774 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
778 if (s->type == SERVICE_DBUS && !s->bus_name) {
779 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->meta.id);
786 static int service_load(Unit *u) {
788 Service *s = SERVICE(u);
792 /* Load a .service file */
793 if ((r = unit_load_fragment(u)) < 0)
796 /* Load a classic init script as a fallback, if we couldn't find anything */
797 if (u->meta.load_state == UNIT_STUB)
798 if ((r = service_load_sysv(s)) < 0)
801 /* Still nothing found? Then let's give up */
802 if (u->meta.load_state == UNIT_STUB)
805 /* We were able to load something, then let's add in the
806 * dropin directories. */
807 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
810 /* This is a new unit? Then let's add in some extras */
811 if (u->meta.load_state == UNIT_LOADED) {
812 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
815 if ((r = unit_add_default_cgroup(u)) < 0)
818 if ((r = sysv_fix_order(s)) < 0)
822 if ((r = service_add_bus_name(s)) < 0)
825 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
830 return service_verify(s);
833 static void service_dump(Unit *u, FILE *f, const char *prefix) {
835 ServiceExecCommand c;
836 Service *s = SERVICE(u);
842 p2 = strappend(prefix, "\t");
843 prefix2 = p2 ? p2 : prefix;
846 "%sService State: %s\n"
847 "%sPermissionsStartOnly: %s\n"
848 "%sRootDirectoryStartOnly: %s\n"
849 "%sValidNoProcess: %s\n"
852 prefix, service_state_to_string(s->state),
853 prefix, yes_no(s->permissions_start_only),
854 prefix, yes_no(s->root_directory_start_only),
855 prefix, yes_no(s->valid_no_process),
856 prefix, kill_mode_to_string(s->kill_mode),
857 prefix, service_type_to_string(s->type));
859 if (s->control_pid > 0)
861 "%sControl PID: %llu\n",
862 prefix, (unsigned long long) s->control_pid);
866 "%sMain PID: %llu\n",
867 prefix, (unsigned long long) s->main_pid);
872 prefix, s->pid_file);
877 "%sBus Name Good: %s\n",
879 prefix, yes_no(s->bus_name_good));
881 exec_context_dump(&s->exec_context, f, prefix);
883 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
885 if (!s->exec_command[c])
888 fprintf(f, "%s-> %s:\n",
889 prefix, service_exec_command_to_string(c));
891 exec_command_dump_list(s->exec_command[c], f, prefix2);
896 "%sSysV Init Script Path: %s\n"
897 "%sSysV Init Script has LSB Header: %s\n",
898 prefix, s->sysv_path,
899 prefix, yes_no(s->sysv_has_lsb));
901 if (s->sysv_start_priority >= 0)
903 "%sSysVStartPriority: %i\n",
904 prefix, s->sysv_start_priority);
906 if (s->sysv_runlevels)
907 fprintf(f, "%sSysVRunLevels: %s\n",
908 prefix, s->sysv_runlevels);
913 static int service_load_pid_file(Service *s) {
920 if (s->main_pid_known)
923 assert(s->main_pid <= 0);
928 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
931 if ((r = safe_atolu(k, &p)) < 0) {
936 if ((unsigned long) (pid_t) p != p)
939 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
940 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
941 (unsigned long long) p, s->pid_file);
945 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
946 /* FIXME: we need to do something here */
949 s->main_pid = (pid_t) p;
950 s->main_pid_known = true;
955 static int service_get_sockets(Service *s, Set **_set) {
964 /* Collects all Socket objects that belong to this
965 * service. Note that a service might have multiple sockets
966 * via multiple names. */
968 if (!(set = set_new(NULL, NULL)))
971 SET_FOREACH(t, UNIT(s)->meta.names, i) {
975 /* Look for all socket objects that go by any of our
976 * units and collect their fds */
978 if (!(k = unit_name_change_suffix(t, ".socket"))) {
983 p = manager_get_unit(UNIT(s)->meta.manager, k);
989 if ((r = set_put(set, p)) < 0)
1001 static int service_notify_sockets_dead(Service *s) {
1009 /* Notifies all our sockets when we die */
1010 if ((r = service_get_sockets(s, &set)) < 0)
1013 SET_FOREACH(sock, set, i)
1014 socket_notify_service_dead(sock);
1021 static void service_set_state(Service *s, ServiceState state) {
1022 ServiceState old_state;
1025 old_state = s->state;
1028 if (state != SERVICE_START_PRE &&
1029 state != SERVICE_START &&
1030 state != SERVICE_START_POST &&
1031 state != SERVICE_RELOAD &&
1032 state != SERVICE_STOP &&
1033 state != SERVICE_STOP_SIGTERM &&
1034 state != SERVICE_STOP_SIGKILL &&
1035 state != SERVICE_STOP_POST &&
1036 state != SERVICE_FINAL_SIGTERM &&
1037 state != SERVICE_FINAL_SIGKILL &&
1038 state != SERVICE_AUTO_RESTART)
1039 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1041 if (state != SERVICE_START &&
1042 state != SERVICE_START_POST &&
1043 state != SERVICE_RUNNING &&
1044 state != SERVICE_RELOAD &&
1045 state != SERVICE_STOP &&
1046 state != SERVICE_STOP_SIGTERM &&
1047 state != SERVICE_STOP_SIGKILL)
1048 service_unwatch_main_pid(s);
1050 if (state != SERVICE_START_PRE &&
1051 state != SERVICE_START &&
1052 state != SERVICE_START_POST &&
1053 state != SERVICE_RELOAD &&
1054 state != SERVICE_STOP &&
1055 state != SERVICE_STOP_SIGTERM &&
1056 state != SERVICE_STOP_SIGKILL &&
1057 state != SERVICE_STOP_POST &&
1058 state != SERVICE_FINAL_SIGTERM &&
1059 state != SERVICE_FINAL_SIGKILL) {
1060 service_unwatch_control_pid(s);
1061 s->control_command = NULL;
1062 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1065 if (state == SERVICE_DEAD ||
1066 state == SERVICE_STOP ||
1067 state == SERVICE_STOP_SIGTERM ||
1068 state == SERVICE_STOP_SIGKILL ||
1069 state == SERVICE_STOP_POST ||
1070 state == SERVICE_FINAL_SIGTERM ||
1071 state == SERVICE_FINAL_SIGKILL ||
1072 state == SERVICE_MAINTAINANCE ||
1073 state == SERVICE_AUTO_RESTART)
1074 service_notify_sockets_dead(s);
1076 if (state != SERVICE_START_PRE &&
1077 state != SERVICE_START &&
1078 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1079 service_close_socket_fd(s);
1081 if (old_state != state)
1082 log_debug("%s changed %s -> %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1084 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1087 static int service_coldplug(Unit *u) {
1088 Service *s = SERVICE(u);
1092 assert(s->state == SERVICE_DEAD);
1094 if (s->deserialized_state != s->state) {
1096 if (s->deserialized_state == SERVICE_START_PRE ||
1097 s->deserialized_state == SERVICE_START ||
1098 s->deserialized_state == SERVICE_START_POST ||
1099 s->deserialized_state == SERVICE_RELOAD ||
1100 s->deserialized_state == SERVICE_STOP ||
1101 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1102 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1103 s->deserialized_state == SERVICE_STOP_POST ||
1104 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1105 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1106 s->deserialized_state == SERVICE_AUTO_RESTART) {
1108 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1111 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1113 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1118 if ((s->deserialized_state == SERVICE_START &&
1119 (s->type == SERVICE_FORKING ||
1120 s->type == SERVICE_DBUS)) ||
1121 s->deserialized_state == SERVICE_START_POST ||
1122 s->deserialized_state == SERVICE_RUNNING ||
1123 s->deserialized_state == SERVICE_RELOAD ||
1124 s->deserialized_state == SERVICE_STOP ||
1125 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1126 s->deserialized_state == SERVICE_STOP_SIGKILL)
1127 if (s->main_pid > 0)
1128 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1131 if (s->deserialized_state == SERVICE_START_PRE ||
1132 s->deserialized_state == SERVICE_START ||
1133 s->deserialized_state == SERVICE_START_POST ||
1134 s->deserialized_state == SERVICE_RELOAD ||
1135 s->deserialized_state == SERVICE_STOP ||
1136 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1137 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1138 s->deserialized_state == SERVICE_STOP_POST ||
1139 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1140 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1141 if (s->control_pid > 0)
1142 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1145 service_set_state(s, s->deserialized_state);
1151 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1155 unsigned rn_fds = 0;
1163 if ((r = service_get_sockets(s, &set)) < 0)
1166 SET_FOREACH(sock, set, i) {
1170 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1182 if (!(t = new(int, rn_fds+cn_fds))) {
1188 memcpy(t, rfds, rn_fds);
1189 memcpy(t+rn_fds, cfds, cn_fds);
1194 rn_fds = rn_fds+cn_fds;
1212 static int service_spawn(
1217 bool apply_permissions,
1232 if (s->socket_fd >= 0) {
1233 fds = &s->socket_fd;
1235 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1239 if (timeout && s->timeout_usec) {
1240 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1243 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1245 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1254 s->meta.manager->environment,
1257 UNIT(s)->meta.manager->confirm_spawn,
1258 UNIT(s)->meta.cgroup_bondings,
1266 if (s->socket_fd >= 0)
1267 service_close_socket_fd(s);
1272 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1273 /* FIXME: we need to do something here */
1284 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1289 static int main_pid_good(Service *s) {
1292 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1295 /* If we know the pid file, then lets just check if it is
1297 if (s->main_pid_known)
1298 return s->main_pid > 0;
1300 /* We don't know the pid */
1304 static int control_pid_good(Service *s) {
1307 return s->control_pid > 0;
1310 static int cgroup_good(Service *s) {
1315 if (s->valid_no_process)
1318 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1324 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1331 if (allow_restart &&
1333 (s->restart == SERVICE_RESTART_ALWAYS ||
1334 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1336 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1339 service_set_state(s, SERVICE_AUTO_RESTART);
1341 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1346 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
1347 service_enter_dead(s, false, false);
1350 static void service_enter_signal(Service *s, ServiceState state, bool success);
1352 static void service_enter_stop_post(Service *s, bool success) {
1359 service_unwatch_control_pid(s);
1361 s->control_command_id = SERVICE_EXEC_STOP_POST;
1362 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1363 if ((r = service_spawn(s,
1367 !s->permissions_start_only,
1368 !s->root_directory_start_only,
1369 &s->control_pid)) < 0)
1373 service_set_state(s, SERVICE_STOP_POST);
1375 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1380 log_warning("%s failed to run stop-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1381 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1384 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1393 if (s->kill_mode != KILL_NONE) {
1394 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1396 if (s->kill_mode == KILL_CONTROL_GROUP) {
1398 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1399 if (r != -EAGAIN && r != -ESRCH)
1408 if (s->main_pid > 0) {
1409 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1415 if (s->control_pid > 0) {
1416 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1427 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1428 if (s->timeout_usec > 0)
1429 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1432 service_set_state(s, state);
1433 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1434 service_enter_stop_post(s, true);
1436 service_enter_dead(s, true, true);
1441 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
1443 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1444 service_enter_stop_post(s, false);
1446 service_enter_dead(s, false, true);
1449 static void service_enter_stop(Service *s, bool success) {
1456 service_unwatch_control_pid(s);
1458 s->control_command_id = SERVICE_EXEC_STOP;
1459 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1460 if ((r = service_spawn(s,
1464 !s->permissions_start_only,
1465 !s->root_directory_start_only,
1466 &s->control_pid)) < 0)
1469 service_set_state(s, SERVICE_STOP);
1471 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1476 log_warning("%s failed to run stop executable: %s", UNIT(s)->meta.id, strerror(-r));
1477 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1480 static void service_enter_running(Service *s, bool success) {
1486 if (main_pid_good(s) != 0 &&
1487 cgroup_good(s) != 0 &&
1488 (s->bus_name_good || s->type != SERVICE_DBUS))
1489 service_set_state(s, SERVICE_RUNNING);
1490 else if (s->valid_no_process)
1491 service_set_state(s, SERVICE_EXITED);
1493 service_enter_stop(s, true);
1496 static void service_enter_start_post(Service *s) {
1500 service_unwatch_control_pid(s);
1502 s->control_command_id = SERVICE_EXEC_START_POST;
1503 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1504 if ((r = service_spawn(s,
1508 !s->permissions_start_only,
1509 !s->root_directory_start_only,
1510 &s->control_pid)) < 0)
1514 service_set_state(s, SERVICE_START_POST);
1516 service_enter_running(s, true);
1521 log_warning("%s failed to run start-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1522 service_enter_stop(s, false);
1525 static void service_enter_start(Service *s) {
1531 assert(s->exec_command[SERVICE_EXEC_START]);
1532 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1534 if (s->type == SERVICE_FORKING)
1535 service_unwatch_control_pid(s);
1537 service_unwatch_main_pid(s);
1539 if ((r = service_spawn(s,
1540 s->exec_command[SERVICE_EXEC_START],
1541 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS,
1548 if (s->type == SERVICE_SIMPLE) {
1549 /* For simple services we immediately start
1550 * the START_POST binaries. */
1553 s->main_pid_known = true;
1555 service_enter_start_post(s);
1557 } else if (s->type == SERVICE_FORKING) {
1559 /* For forking services we wait until the start
1560 * process exited. */
1562 s->control_pid = pid;
1564 s->control_command_id = SERVICE_EXEC_START;
1565 s->control_command = s->exec_command[SERVICE_EXEC_START];
1566 service_set_state(s, SERVICE_START);
1568 } else if (s->type == SERVICE_FINISH ||
1569 s->type == SERVICE_DBUS) {
1571 /* For finishing services we wait until the start
1572 * process exited, too, but it is our main process. */
1574 /* For D-Bus services we know the main pid right away,
1575 * but wait for the bus name to appear on the bus. */
1578 s->main_pid_known = true;
1580 service_set_state(s, SERVICE_START);
1582 assert_not_reached("Unknown service type");
1587 log_warning("%s failed to run start exectuable: %s", UNIT(s)->meta.id, strerror(-r));
1588 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1591 static void service_enter_start_pre(Service *s) {
1596 service_unwatch_control_pid(s);
1598 s->control_command_id = SERVICE_EXEC_START_PRE;
1599 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1600 if ((r = service_spawn(s,
1604 !s->permissions_start_only,
1605 !s->root_directory_start_only,
1606 &s->control_pid)) < 0)
1609 service_set_state(s, SERVICE_START_PRE);
1611 service_enter_start(s);
1616 log_warning("%s failed to run start-pre executable: %s", UNIT(s)->meta.id, strerror(-r));
1617 service_enter_dead(s, false, true);
1620 static void service_enter_restart(Service *s) {
1624 service_enter_dead(s, true, false);
1626 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1629 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
1634 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
1635 service_enter_dead(s, false, false);
1638 static void service_enter_reload(Service *s) {
1643 service_unwatch_control_pid(s);
1645 s->control_command_id = SERVICE_EXEC_RELOAD;
1646 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1647 if ((r = service_spawn(s,
1651 !s->permissions_start_only,
1652 !s->root_directory_start_only,
1653 &s->control_pid)) < 0)
1656 service_set_state(s, SERVICE_RELOAD);
1658 service_enter_running(s, true);
1663 log_warning("%s failed to run reload executable: %s", UNIT(s)->meta.id, strerror(-r));
1664 service_enter_stop(s, false);
1667 static void service_run_next(Service *s, bool success) {
1671 assert(s->control_command);
1672 assert(s->control_command->command_next);
1677 s->control_command = s->control_command->command_next;
1679 service_unwatch_control_pid(s);
1681 if ((r = service_spawn(s,
1685 !s->permissions_start_only,
1686 !s->root_directory_start_only,
1687 &s->control_pid)) < 0)
1693 log_warning("%s failed to run spawn next executable: %s", UNIT(s)->meta.id, strerror(-r));
1695 if (s->state == SERVICE_START_PRE)
1696 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1697 else if (s->state == SERVICE_STOP)
1698 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1699 else if (s->state == SERVICE_STOP_POST)
1700 service_enter_dead(s, false, true);
1702 service_enter_stop(s, false);
1705 static int service_start(Unit *u) {
1706 Service *s = SERVICE(u);
1710 /* We cannot fulfill this request right now, try again later
1712 if (s->state == SERVICE_STOP ||
1713 s->state == SERVICE_STOP_SIGTERM ||
1714 s->state == SERVICE_STOP_SIGKILL ||
1715 s->state == SERVICE_STOP_POST ||
1716 s->state == SERVICE_FINAL_SIGTERM ||
1717 s->state == SERVICE_FINAL_SIGKILL)
1720 /* Already on it! */
1721 if (s->state == SERVICE_START_PRE ||
1722 s->state == SERVICE_START ||
1723 s->state == SERVICE_START_POST)
1726 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1728 /* Make sure we don't enter a busy loop of some kind. */
1729 if (!ratelimit_test(&s->ratelimit)) {
1730 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1735 s->main_pid_known = false;
1736 s->allow_restart = true;
1738 service_enter_start_pre(s);
1742 static int service_stop(Unit *u) {
1743 Service *s = SERVICE(u);
1747 /* Cannot do this now */
1748 if (s->state == SERVICE_START_PRE ||
1749 s->state == SERVICE_START ||
1750 s->state == SERVICE_START_POST ||
1751 s->state == SERVICE_RELOAD)
1755 if (s->state == SERVICE_STOP ||
1756 s->state == SERVICE_STOP_SIGTERM ||
1757 s->state == SERVICE_STOP_SIGKILL ||
1758 s->state == SERVICE_STOP_POST ||
1759 s->state == SERVICE_FINAL_SIGTERM ||
1760 s->state == SERVICE_FINAL_SIGKILL)
1763 if (s->state == SERVICE_AUTO_RESTART) {
1764 service_set_state(s, SERVICE_DEAD);
1768 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1770 /* This is a user request, so don't do restarts on this
1772 s->allow_restart = false;
1774 service_enter_stop(s, true);
1778 static int service_reload(Unit *u) {
1779 Service *s = SERVICE(u);
1783 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1785 service_enter_reload(s);
1789 static bool service_can_reload(Unit *u) {
1790 Service *s = SERVICE(u);
1794 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1797 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1798 Service *s = SERVICE(u);
1804 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1805 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1807 if (s->control_pid > 0)
1808 unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) (s->control_pid));
1810 if (s->main_pid > 0)
1811 unit_serialize_item_format(u, f, "main-pid", "%u", (unsigned) (s->main_pid));
1813 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1815 /* There's a minor uncleanliness here: if there are multiple
1816 * commands attached here, we will start from the first one
1818 if (s->control_command_id >= 0)
1819 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
1821 if (s->socket_fd >= 0) {
1824 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1827 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1833 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1834 Service *s = SERVICE(u);
1842 if (streq(key, "state")) {
1845 if ((state = service_state_from_string(value)) < 0)
1846 log_debug("Failed to parse state value %s", value);
1848 s->deserialized_state = state;
1849 } else if (streq(key, "failure")) {
1852 if ((b = parse_boolean(value)) < 0)
1853 log_debug("Failed to parse failure value %s", value);
1855 s->failure = b || s->failure;
1856 } else if (streq(key, "control-pid")) {
1859 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1860 log_debug("Failed to parse control-pid value %s", value);
1862 s->control_pid = (pid_t) pid;
1863 } else if (streq(key, "main-pid")) {
1866 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1867 log_debug("Failed to parse main-pid value %s", value);
1869 s->main_pid = (pid_t) pid;
1870 } else if (streq(key, "main-pid-known")) {
1873 if ((b = parse_boolean(value)) < 0)
1874 log_debug("Failed to parse main-pid-known value %s", value);
1876 s->main_pid_known = b;
1877 } else if (streq(key, "control-command")) {
1878 ServiceExecCommand id;
1880 if ((id = service_exec_command_from_string(value)) < 0)
1881 log_debug("Failed to parse exec-command value %s", value);
1883 s->control_command_id = id;
1884 s->control_command = s->exec_command[id];
1886 } else if (streq(key, "socket-fd")) {
1889 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1890 log_debug("Failed to parse socket-fd value %s", value);
1893 if (s->socket_fd >= 0)
1894 close_nointr_nofail(s->socket_fd);
1895 s->socket_fd = fdset_remove(fds, fd);
1898 log_debug("Unknown serialization key '%s'", key);
1903 static UnitActiveState service_active_state(Unit *u) {
1906 return state_translation_table[SERVICE(u)->state];
1909 static const char *service_sub_state_to_string(Unit *u) {
1912 return service_state_to_string(SERVICE(u)->state);
1915 static bool service_check_gc(Unit *u) {
1916 Service *s = SERVICE(u);
1920 return !!s->sysv_path;
1923 static bool service_check_snapshot(Unit *u) {
1924 Service *s = SERVICE(u);
1928 return !s->got_socket_fd;
1931 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1932 Service *s = SERVICE(u);
1938 success = is_clean_exit(code, status);
1939 s->failure = s->failure || !success;
1941 if (s->main_pid == pid) {
1943 exec_status_fill(&s->main_exec_status, pid, code, status);
1946 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1947 assert(s->exec_command[SERVICE_EXEC_START]);
1948 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1951 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
1953 /* The service exited, so the service is officially
1958 case SERVICE_START_POST:
1959 case SERVICE_RELOAD:
1961 /* Need to wait until the operation is
1966 if (s->type == SERVICE_FINISH) {
1967 /* This was our main goal, so let's go on */
1969 service_enter_start_post(s);
1971 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1974 assert(s->type == SERVICE_DBUS);
1979 case SERVICE_RUNNING:
1980 service_enter_running(s, success);
1983 case SERVICE_STOP_SIGTERM:
1984 case SERVICE_STOP_SIGKILL:
1986 if (!control_pid_good(s))
1987 service_enter_stop_post(s, success);
1989 /* If there is still a control process, wait for that first */
1993 assert_not_reached("Uh, main process died at wrong time.");
1996 } else if (s->control_pid == pid) {
1998 if (s->control_command)
1999 exec_status_fill(&s->control_command->exec_status, pid, code, status);
2003 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2005 /* If we are shutting things down anyway we
2006 * don't care about failing commands. */
2008 if (s->control_command && s->control_command->command_next && success) {
2010 /* There is another command to *
2011 * execute, so let's do that. */
2013 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
2014 service_run_next(s, success);
2017 /* No further commands for this step, so let's
2018 * figure out what to do next */
2020 s->control_command = NULL;
2021 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2023 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2027 case SERVICE_START_PRE:
2029 service_enter_start(s);
2031 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2035 assert(s->type == SERVICE_FORKING);
2037 /* Let's try to load the pid
2038 * file here if we can. We
2039 * ignore the return value,
2040 * since the PID file might
2041 * actually be created by a
2042 * START_POST script */
2046 service_load_pid_file(s);
2048 service_enter_start_post(s);
2050 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2054 case SERVICE_START_POST:
2055 if (success && s->pid_file && !s->main_pid_known) {
2058 /* Hmm, let's see if we can
2059 * load the pid now after the
2060 * start-post scripts got
2063 if ((r = service_load_pid_file(s)) < 0)
2064 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
2069 case SERVICE_RELOAD:
2071 service_enter_running(s, true);
2073 service_enter_stop(s, false);
2078 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2081 case SERVICE_STOP_SIGTERM:
2082 case SERVICE_STOP_SIGKILL:
2083 if (main_pid_good(s) <= 0)
2084 service_enter_stop_post(s, success);
2086 /* If there is still a service
2087 * process around, wait until
2088 * that one quit, too */
2091 case SERVICE_STOP_POST:
2092 case SERVICE_FINAL_SIGTERM:
2093 case SERVICE_FINAL_SIGKILL:
2094 service_enter_dead(s, success, true);
2098 assert_not_reached("Uh, control process died at wrong time.");
2102 assert_not_reached("Got SIGCHLD for unkown PID");
2105 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2106 Service *s = SERVICE(u);
2109 assert(elapsed == 1);
2111 assert(w == &s->timer_watch);
2115 case SERVICE_START_PRE:
2117 log_warning("%s operation timed out. Terminating.", u->meta.id);
2118 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2121 case SERVICE_START_POST:
2122 case SERVICE_RELOAD:
2123 log_warning("%s operation timed out. Stopping.", u->meta.id);
2124 service_enter_stop(s, false);
2128 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2129 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2132 case SERVICE_STOP_SIGTERM:
2133 log_warning("%s stopping timed out. Killing.", u->meta.id);
2134 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2137 case SERVICE_STOP_SIGKILL:
2138 /* Uh, wie sent a SIGKILL and it is still not gone?
2139 * Must be something we cannot kill, so let's just be
2140 * weirded out and continue */
2142 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2143 service_enter_stop_post(s, false);
2146 case SERVICE_STOP_POST:
2147 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2148 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2151 case SERVICE_FINAL_SIGTERM:
2152 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2153 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2156 case SERVICE_FINAL_SIGKILL:
2157 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u->meta.id);
2158 service_enter_dead(s, false, true);
2161 case SERVICE_AUTO_RESTART:
2162 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
2163 service_enter_restart(s);
2167 assert_not_reached("Timeout at wrong time.");
2171 static void service_cgroup_notify_event(Unit *u) {
2172 Service *s = SERVICE(u);
2176 log_debug("%s: cgroup is empty", u->meta.id);
2180 /* Waiting for SIGCHLD is usually more interesting,
2181 * because it includes return codes/signals. Which is
2182 * why we ignore the cgroup events for most cases,
2183 * except when we don't know pid which to expect the
2186 case SERVICE_RUNNING:
2187 service_enter_running(s, true);
2195 static int service_enumerate(Manager *m) {
2199 char *path = NULL, *fpath = NULL, *name = NULL;
2204 STRV_FOREACH(p, m->sysvrcnd_path)
2205 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2210 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2218 if (!(d = opendir(path))) {
2219 if (errno != ENOENT)
2220 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2225 while ((de = readdir(d))) {
2229 if (ignore_file(de->d_name))
2232 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2235 if (strlen(de->d_name) < 4)
2238 a = undecchar(de->d_name[1]);
2239 b = undecchar(de->d_name[2]);
2246 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2251 if (access(fpath, X_OK) < 0) {
2253 if (errno != ENOENT)
2254 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2260 if (!(name = sysv_translate_name(de->d_name + 3))) {
2265 if ((r = manager_load_unit_prepare(m, name, NULL, &service)) < 0) {
2266 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2270 if (de->d_name[0] == 'S' &&
2271 (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT))
2272 SERVICE(service)->sysv_start_priority =
2273 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2275 manager_dispatch_load_queue(m);
2276 service = unit_follow_merge(service);
2278 if (de->d_name[0] == 'S') {
2279 Unit *runlevel_target;
2281 if ((r = manager_load_unit(m, rcnd_table[i].target, NULL, &runlevel_target)) < 0)
2284 if ((r = unit_add_dependency(runlevel_target, UNIT_WANTS, service, true)) < 0)
2287 if ((r = unit_add_dependency(service, UNIT_BEFORE, runlevel_target, true)) < 0)
2290 } else if (de->d_name[0] == 'K' &&
2291 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2292 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2293 Unit *shutdown_target;
2295 /* We honour K links only for
2296 * halt/reboot. For the normal
2297 * runlevels we assume the
2299 * implicitly added by the
2300 * core logic. Also, we don't
2301 * really distuingish here
2302 * between the runlevels 0 and
2303 * 6 and just add them to the
2304 * special shutdown target. On
2305 * SUSE the boot.d/ runlevel
2306 * is also used for shutdown,
2307 * so we add links for that
2308 * too to the shutdown
2311 if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
2314 if ((r = unit_add_dependency(service, UNIT_CONFLICTS, shutdown_target, true)) < 0)
2317 if ((r = unit_add_dependency(service, UNIT_BEFORE, shutdown_target, true)) < 0)
2336 static void service_bus_name_owner_change(
2339 const char *old_owner,
2340 const char *new_owner) {
2342 Service *s = SERVICE(u);
2347 assert(streq(s->bus_name, name));
2348 assert(old_owner || new_owner);
2350 if (old_owner && new_owner)
2351 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2353 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2355 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2357 s->bus_name_good = !!new_owner;
2359 if (s->type == SERVICE_DBUS) {
2361 /* service_enter_running() will figure out what to
2363 if (s->state == SERVICE_RUNNING)
2364 service_enter_running(s, true);
2365 else if (s->state == SERVICE_START && new_owner)
2366 service_enter_start_post(s);
2368 } else if (new_owner &&
2370 (s->state == SERVICE_START ||
2371 s->state == SERVICE_START_POST ||
2372 s->state == SERVICE_RUNNING ||
2373 s->state == SERVICE_RELOAD)) {
2375 /* Try to acquire PID from bus service */
2376 log_debug("Trying to acquire PID from D-Bus name...");
2378 bus_query_pid(u->meta.manager, name);
2382 static void service_bus_query_pid_done(
2387 Service *s = SERVICE(u);
2392 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2394 if (s->main_pid <= 0 &&
2395 (s->state == SERVICE_START ||
2396 s->state == SERVICE_START_POST ||
2397 s->state == SERVICE_RUNNING ||
2398 s->state == SERVICE_RELOAD))
2402 int service_set_socket_fd(Service *s, int fd) {
2406 /* This is called by the socket code when instantiating a new
2407 * service for a stream socket and the socket needs to be
2410 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2413 if (s->socket_fd >= 0)
2416 if (s->state != SERVICE_DEAD)
2420 s->got_socket_fd = true;
2424 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2425 [SERVICE_DEAD] = "dead",
2426 [SERVICE_START_PRE] = "start-pre",
2427 [SERVICE_START] = "start",
2428 [SERVICE_START_POST] = "start-post",
2429 [SERVICE_RUNNING] = "running",
2430 [SERVICE_EXITED] = "exited",
2431 [SERVICE_RELOAD] = "reload",
2432 [SERVICE_STOP] = "stop",
2433 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2434 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2435 [SERVICE_STOP_POST] = "stop-post",
2436 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2437 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2438 [SERVICE_MAINTAINANCE] = "maintainance",
2439 [SERVICE_AUTO_RESTART] = "auto-restart",
2442 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2444 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2445 [SERVICE_ONCE] = "once",
2446 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2447 [SERVICE_RESTART_ALWAYS] = "restart-always",
2450 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2452 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2453 [SERVICE_FORKING] = "forking",
2454 [SERVICE_SIMPLE] = "simple",
2455 [SERVICE_FINISH] = "finish",
2456 [SERVICE_DBUS] = "dbus"
2459 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2461 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2462 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2463 [SERVICE_EXEC_START] = "ExecStart",
2464 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2465 [SERVICE_EXEC_RELOAD] = "ExecReload",
2466 [SERVICE_EXEC_STOP] = "ExecStop",
2467 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2470 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2472 const UnitVTable service_vtable = {
2473 .suffix = ".service",
2475 .init = service_init,
2476 .done = service_done,
2477 .load = service_load,
2479 .coldplug = service_coldplug,
2481 .dump = service_dump,
2483 .start = service_start,
2484 .stop = service_stop,
2485 .reload = service_reload,
2487 .can_reload = service_can_reload,
2489 .serialize = service_serialize,
2490 .deserialize_item = service_deserialize_item,
2492 .active_state = service_active_state,
2493 .sub_state_to_string = service_sub_state_to_string,
2495 .check_gc = service_check_gc,
2496 .check_snapshot = service_check_snapshot,
2498 .sigchld_event = service_sigchld_event,
2499 .timer_event = service_timer_event,
2501 .cgroup_notify_empty = service_cgroup_notify_event,
2503 .bus_name_owner_change = service_bus_name_owner_change,
2504 .bus_query_pid_done = service_bus_query_pid_done,
2506 .bus_message_handler = bus_service_message_handler,
2508 .enumerate = service_enumerate