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->lookup_paths.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->lookup_paths.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)
942 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
943 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
944 (unsigned long long) p, s->pid_file);
948 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
949 /* FIXME: we need to do something here */
952 s->main_pid = (pid_t) p;
953 s->main_pid_known = true;
958 static int service_get_sockets(Service *s, Set **_set) {
967 /* Collects all Socket objects that belong to this
968 * service. Note that a service might have multiple sockets
969 * via multiple names. */
971 if (!(set = set_new(NULL, NULL)))
974 SET_FOREACH(t, UNIT(s)->meta.names, i) {
978 /* Look for all socket objects that go by any of our
979 * units and collect their fds */
981 if (!(k = unit_name_change_suffix(t, ".socket"))) {
986 p = manager_get_unit(UNIT(s)->meta.manager, k);
992 if ((r = set_put(set, p)) < 0)
1004 static int service_notify_sockets_dead(Service *s) {
1012 /* Notifies all our sockets when we die */
1013 if ((r = service_get_sockets(s, &set)) < 0)
1016 SET_FOREACH(sock, set, i)
1017 socket_notify_service_dead(sock);
1024 static void service_set_state(Service *s, ServiceState state) {
1025 ServiceState old_state;
1028 old_state = s->state;
1031 if (state != SERVICE_START_PRE &&
1032 state != SERVICE_START &&
1033 state != SERVICE_START_POST &&
1034 state != SERVICE_RELOAD &&
1035 state != SERVICE_STOP &&
1036 state != SERVICE_STOP_SIGTERM &&
1037 state != SERVICE_STOP_SIGKILL &&
1038 state != SERVICE_STOP_POST &&
1039 state != SERVICE_FINAL_SIGTERM &&
1040 state != SERVICE_FINAL_SIGKILL &&
1041 state != SERVICE_AUTO_RESTART)
1042 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1044 if (state != SERVICE_START &&
1045 state != SERVICE_START_POST &&
1046 state != SERVICE_RUNNING &&
1047 state != SERVICE_RELOAD &&
1048 state != SERVICE_STOP &&
1049 state != SERVICE_STOP_SIGTERM &&
1050 state != SERVICE_STOP_SIGKILL)
1051 service_unwatch_main_pid(s);
1053 if (state != SERVICE_START_PRE &&
1054 state != SERVICE_START &&
1055 state != SERVICE_START_POST &&
1056 state != SERVICE_RELOAD &&
1057 state != SERVICE_STOP &&
1058 state != SERVICE_STOP_SIGTERM &&
1059 state != SERVICE_STOP_SIGKILL &&
1060 state != SERVICE_STOP_POST &&
1061 state != SERVICE_FINAL_SIGTERM &&
1062 state != SERVICE_FINAL_SIGKILL) {
1063 service_unwatch_control_pid(s);
1064 s->control_command = NULL;
1065 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1068 if (state == SERVICE_DEAD ||
1069 state == SERVICE_STOP ||
1070 state == SERVICE_STOP_SIGTERM ||
1071 state == SERVICE_STOP_SIGKILL ||
1072 state == SERVICE_STOP_POST ||
1073 state == SERVICE_FINAL_SIGTERM ||
1074 state == SERVICE_FINAL_SIGKILL ||
1075 state == SERVICE_MAINTAINANCE ||
1076 state == SERVICE_AUTO_RESTART)
1077 service_notify_sockets_dead(s);
1079 if (state != SERVICE_START_PRE &&
1080 state != SERVICE_START &&
1081 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1082 service_close_socket_fd(s);
1084 if (old_state != state)
1085 log_debug("%s changed %s -> %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1087 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1090 static int service_coldplug(Unit *u) {
1091 Service *s = SERVICE(u);
1095 assert(s->state == SERVICE_DEAD);
1097 if (s->deserialized_state != s->state) {
1099 if (s->deserialized_state == SERVICE_START_PRE ||
1100 s->deserialized_state == SERVICE_START ||
1101 s->deserialized_state == SERVICE_START_POST ||
1102 s->deserialized_state == SERVICE_RELOAD ||
1103 s->deserialized_state == SERVICE_STOP ||
1104 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1105 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1106 s->deserialized_state == SERVICE_STOP_POST ||
1107 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1108 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1109 s->deserialized_state == SERVICE_AUTO_RESTART) {
1111 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1114 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1116 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1121 if ((s->deserialized_state == SERVICE_START &&
1122 (s->type == SERVICE_FORKING ||
1123 s->type == SERVICE_DBUS)) ||
1124 s->deserialized_state == SERVICE_START_POST ||
1125 s->deserialized_state == SERVICE_RUNNING ||
1126 s->deserialized_state == SERVICE_RELOAD ||
1127 s->deserialized_state == SERVICE_STOP ||
1128 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1129 s->deserialized_state == SERVICE_STOP_SIGKILL)
1130 if (s->main_pid > 0)
1131 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1134 if (s->deserialized_state == SERVICE_START_PRE ||
1135 s->deserialized_state == SERVICE_START ||
1136 s->deserialized_state == SERVICE_START_POST ||
1137 s->deserialized_state == SERVICE_RELOAD ||
1138 s->deserialized_state == SERVICE_STOP ||
1139 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1140 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1141 s->deserialized_state == SERVICE_STOP_POST ||
1142 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1143 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1144 if (s->control_pid > 0)
1145 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1148 service_set_state(s, s->deserialized_state);
1154 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1158 unsigned rn_fds = 0;
1166 if ((r = service_get_sockets(s, &set)) < 0)
1169 SET_FOREACH(sock, set, i) {
1173 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1185 if (!(t = new(int, rn_fds+cn_fds))) {
1191 memcpy(t, rfds, rn_fds);
1192 memcpy(t+rn_fds, cfds, cn_fds);
1197 rn_fds = rn_fds+cn_fds;
1215 static int service_spawn(
1220 bool apply_permissions,
1235 if (s->socket_fd >= 0) {
1236 fds = &s->socket_fd;
1238 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1242 if (timeout && s->timeout_usec) {
1243 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1246 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1248 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1257 s->meta.manager->environment,
1260 UNIT(s)->meta.manager->confirm_spawn,
1261 UNIT(s)->meta.cgroup_bondings,
1269 if (s->socket_fd >= 0)
1270 service_close_socket_fd(s);
1275 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1276 /* FIXME: we need to do something here */
1287 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1292 static int main_pid_good(Service *s) {
1295 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1298 /* If we know the pid file, then lets just check if it is
1300 if (s->main_pid_known)
1301 return s->main_pid > 0;
1303 /* We don't know the pid */
1307 static int control_pid_good(Service *s) {
1310 return s->control_pid > 0;
1313 static int cgroup_good(Service *s) {
1318 if (s->valid_no_process)
1321 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1327 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1334 if (allow_restart &&
1336 (s->restart == SERVICE_RESTART_ALWAYS ||
1337 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1339 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1342 service_set_state(s, SERVICE_AUTO_RESTART);
1344 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1349 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
1350 service_enter_dead(s, false, false);
1353 static void service_enter_signal(Service *s, ServiceState state, bool success);
1355 static void service_enter_stop_post(Service *s, bool success) {
1362 service_unwatch_control_pid(s);
1364 s->control_command_id = SERVICE_EXEC_STOP_POST;
1365 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1366 if ((r = service_spawn(s,
1370 !s->permissions_start_only,
1371 !s->root_directory_start_only,
1372 &s->control_pid)) < 0)
1376 service_set_state(s, SERVICE_STOP_POST);
1378 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1383 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->meta.id, strerror(-r));
1384 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1387 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1396 if (s->kill_mode != KILL_NONE) {
1397 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1399 if (s->kill_mode == KILL_CONTROL_GROUP) {
1401 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1402 if (r != -EAGAIN && r != -ESRCH)
1411 if (s->main_pid > 0) {
1412 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1418 if (s->control_pid > 0) {
1419 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1430 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1431 if (s->timeout_usec > 0)
1432 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1435 service_set_state(s, state);
1436 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1437 service_enter_stop_post(s, true);
1439 service_enter_dead(s, true, true);
1444 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
1446 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1447 service_enter_stop_post(s, false);
1449 service_enter_dead(s, false, true);
1452 static void service_enter_stop(Service *s, bool success) {
1459 service_unwatch_control_pid(s);
1461 s->control_command_id = SERVICE_EXEC_STOP;
1462 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1463 if ((r = service_spawn(s,
1467 !s->permissions_start_only,
1468 !s->root_directory_start_only,
1469 &s->control_pid)) < 0)
1472 service_set_state(s, SERVICE_STOP);
1474 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1479 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->meta.id, strerror(-r));
1480 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1483 static void service_enter_running(Service *s, bool success) {
1489 if (main_pid_good(s) != 0 &&
1490 cgroup_good(s) != 0 &&
1491 (s->bus_name_good || s->type != SERVICE_DBUS))
1492 service_set_state(s, SERVICE_RUNNING);
1493 else if (s->valid_no_process)
1494 service_set_state(s, SERVICE_EXITED);
1496 service_enter_stop(s, true);
1499 static void service_enter_start_post(Service *s) {
1503 service_unwatch_control_pid(s);
1505 s->control_command_id = SERVICE_EXEC_START_POST;
1506 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1507 if ((r = service_spawn(s,
1511 !s->permissions_start_only,
1512 !s->root_directory_start_only,
1513 &s->control_pid)) < 0)
1517 service_set_state(s, SERVICE_START_POST);
1519 service_enter_running(s, true);
1524 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->meta.id, strerror(-r));
1525 service_enter_stop(s, false);
1528 static void service_enter_start(Service *s) {
1534 assert(s->exec_command[SERVICE_EXEC_START]);
1535 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1537 if (s->type == SERVICE_FORKING)
1538 service_unwatch_control_pid(s);
1540 service_unwatch_main_pid(s);
1542 if ((r = service_spawn(s,
1543 s->exec_command[SERVICE_EXEC_START],
1544 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS,
1551 if (s->type == SERVICE_SIMPLE) {
1552 /* For simple services we immediately start
1553 * the START_POST binaries. */
1556 s->main_pid_known = true;
1558 service_enter_start_post(s);
1560 } else if (s->type == SERVICE_FORKING) {
1562 /* For forking services we wait until the start
1563 * process exited. */
1565 s->control_pid = pid;
1567 s->control_command_id = SERVICE_EXEC_START;
1568 s->control_command = s->exec_command[SERVICE_EXEC_START];
1569 service_set_state(s, SERVICE_START);
1571 } else if (s->type == SERVICE_FINISH ||
1572 s->type == SERVICE_DBUS) {
1574 /* For finishing services we wait until the start
1575 * process exited, too, but it is our main process. */
1577 /* For D-Bus services we know the main pid right away,
1578 * but wait for the bus name to appear on the bus. */
1581 s->main_pid_known = true;
1583 service_set_state(s, SERVICE_START);
1585 assert_not_reached("Unknown service type");
1590 log_warning("%s failed to run 'start' task: %s", UNIT(s)->meta.id, strerror(-r));
1591 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1594 static void service_enter_start_pre(Service *s) {
1599 service_unwatch_control_pid(s);
1601 s->control_command_id = SERVICE_EXEC_START_PRE;
1602 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1603 if ((r = service_spawn(s,
1607 !s->permissions_start_only,
1608 !s->root_directory_start_only,
1609 &s->control_pid)) < 0)
1612 service_set_state(s, SERVICE_START_PRE);
1614 service_enter_start(s);
1619 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->meta.id, strerror(-r));
1620 service_enter_dead(s, false, true);
1623 static void service_enter_restart(Service *s) {
1627 service_enter_dead(s, true, false);
1629 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1632 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
1637 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
1638 service_enter_dead(s, false, false);
1641 static void service_enter_reload(Service *s) {
1646 service_unwatch_control_pid(s);
1648 s->control_command_id = SERVICE_EXEC_RELOAD;
1649 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1650 if ((r = service_spawn(s,
1654 !s->permissions_start_only,
1655 !s->root_directory_start_only,
1656 &s->control_pid)) < 0)
1659 service_set_state(s, SERVICE_RELOAD);
1661 service_enter_running(s, true);
1666 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->meta.id, strerror(-r));
1667 service_enter_stop(s, false);
1670 static void service_run_next(Service *s, bool success) {
1674 assert(s->control_command);
1675 assert(s->control_command->command_next);
1680 s->control_command = s->control_command->command_next;
1682 service_unwatch_control_pid(s);
1684 if ((r = service_spawn(s,
1688 !s->permissions_start_only,
1689 !s->root_directory_start_only,
1690 &s->control_pid)) < 0)
1696 log_warning("%s failed to run spawn next task: %s", UNIT(s)->meta.id, strerror(-r));
1698 if (s->state == SERVICE_START_PRE)
1699 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1700 else if (s->state == SERVICE_STOP)
1701 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1702 else if (s->state == SERVICE_STOP_POST)
1703 service_enter_dead(s, false, true);
1705 service_enter_stop(s, false);
1708 static int service_start(Unit *u) {
1709 Service *s = SERVICE(u);
1713 /* We cannot fulfill this request right now, try again later
1715 if (s->state == SERVICE_STOP ||
1716 s->state == SERVICE_STOP_SIGTERM ||
1717 s->state == SERVICE_STOP_SIGKILL ||
1718 s->state == SERVICE_STOP_POST ||
1719 s->state == SERVICE_FINAL_SIGTERM ||
1720 s->state == SERVICE_FINAL_SIGKILL)
1723 /* Already on it! */
1724 if (s->state == SERVICE_START_PRE ||
1725 s->state == SERVICE_START ||
1726 s->state == SERVICE_START_POST)
1729 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1731 /* Make sure we don't enter a busy loop of some kind. */
1732 if (!ratelimit_test(&s->ratelimit)) {
1733 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1738 s->main_pid_known = false;
1739 s->allow_restart = true;
1741 service_enter_start_pre(s);
1745 static int service_stop(Unit *u) {
1746 Service *s = SERVICE(u);
1750 /* Cannot do this now */
1751 if (s->state == SERVICE_START_PRE ||
1752 s->state == SERVICE_START ||
1753 s->state == SERVICE_START_POST ||
1754 s->state == SERVICE_RELOAD)
1758 if (s->state == SERVICE_STOP ||
1759 s->state == SERVICE_STOP_SIGTERM ||
1760 s->state == SERVICE_STOP_SIGKILL ||
1761 s->state == SERVICE_STOP_POST ||
1762 s->state == SERVICE_FINAL_SIGTERM ||
1763 s->state == SERVICE_FINAL_SIGKILL)
1766 if (s->state == SERVICE_AUTO_RESTART) {
1767 service_set_state(s, SERVICE_DEAD);
1771 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1773 /* This is a user request, so don't do restarts on this
1775 s->allow_restart = false;
1777 service_enter_stop(s, true);
1781 static int service_reload(Unit *u) {
1782 Service *s = SERVICE(u);
1786 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1788 service_enter_reload(s);
1792 static bool service_can_reload(Unit *u) {
1793 Service *s = SERVICE(u);
1797 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1800 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1801 Service *s = SERVICE(u);
1807 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1808 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1810 if (s->control_pid > 0)
1811 unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) (s->control_pid));
1813 if (s->main_pid > 0)
1814 unit_serialize_item_format(u, f, "main-pid", "%u", (unsigned) (s->main_pid));
1816 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1818 /* There's a minor uncleanliness here: if there are multiple
1819 * commands attached here, we will start from the first one
1821 if (s->control_command_id >= 0)
1822 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
1824 if (s->socket_fd >= 0) {
1827 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1830 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1836 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1837 Service *s = SERVICE(u);
1845 if (streq(key, "state")) {
1848 if ((state = service_state_from_string(value)) < 0)
1849 log_debug("Failed to parse state value %s", value);
1851 s->deserialized_state = state;
1852 } else if (streq(key, "failure")) {
1855 if ((b = parse_boolean(value)) < 0)
1856 log_debug("Failed to parse failure value %s", value);
1858 s->failure = b || s->failure;
1859 } else if (streq(key, "control-pid")) {
1862 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1863 log_debug("Failed to parse control-pid value %s", value);
1865 s->control_pid = (pid_t) pid;
1866 } else if (streq(key, "main-pid")) {
1869 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1870 log_debug("Failed to parse main-pid value %s", value);
1872 s->main_pid = (pid_t) pid;
1873 } else if (streq(key, "main-pid-known")) {
1876 if ((b = parse_boolean(value)) < 0)
1877 log_debug("Failed to parse main-pid-known value %s", value);
1879 s->main_pid_known = b;
1880 } else if (streq(key, "control-command")) {
1881 ServiceExecCommand id;
1883 if ((id = service_exec_command_from_string(value)) < 0)
1884 log_debug("Failed to parse exec-command value %s", value);
1886 s->control_command_id = id;
1887 s->control_command = s->exec_command[id];
1889 } else if (streq(key, "socket-fd")) {
1892 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1893 log_debug("Failed to parse socket-fd value %s", value);
1896 if (s->socket_fd >= 0)
1897 close_nointr_nofail(s->socket_fd);
1898 s->socket_fd = fdset_remove(fds, fd);
1901 log_debug("Unknown serialization key '%s'", key);
1906 static UnitActiveState service_active_state(Unit *u) {
1909 return state_translation_table[SERVICE(u)->state];
1912 static const char *service_sub_state_to_string(Unit *u) {
1915 return service_state_to_string(SERVICE(u)->state);
1918 static bool service_check_gc(Unit *u) {
1919 Service *s = SERVICE(u);
1923 return !!s->sysv_path;
1926 static bool service_check_snapshot(Unit *u) {
1927 Service *s = SERVICE(u);
1931 return !s->got_socket_fd;
1934 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1935 Service *s = SERVICE(u);
1941 success = is_clean_exit(code, status);
1942 s->failure = s->failure || !success;
1944 if (s->main_pid == pid) {
1946 exec_status_fill(&s->main_exec_status, pid, code, status);
1949 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1950 assert(s->exec_command[SERVICE_EXEC_START]);
1951 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1954 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
1956 /* The service exited, so the service is officially
1961 case SERVICE_START_POST:
1962 case SERVICE_RELOAD:
1964 /* Need to wait until the operation is
1969 if (s->type == SERVICE_FINISH) {
1970 /* This was our main goal, so let's go on */
1972 service_enter_start_post(s);
1974 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1977 assert(s->type == SERVICE_DBUS);
1982 case SERVICE_RUNNING:
1983 service_enter_running(s, success);
1986 case SERVICE_STOP_SIGTERM:
1987 case SERVICE_STOP_SIGKILL:
1989 if (!control_pid_good(s))
1990 service_enter_stop_post(s, success);
1992 /* If there is still a control process, wait for that first */
1996 assert_not_reached("Uh, main process died at wrong time.");
1999 } else if (s->control_pid == pid) {
2001 if (s->control_command)
2002 exec_status_fill(&s->control_command->exec_status, pid, code, status);
2006 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2008 /* If we are shutting things down anyway we
2009 * don't care about failing commands. */
2011 if (s->control_command && s->control_command->command_next && success) {
2013 /* There is another command to *
2014 * execute, so let's do that. */
2016 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
2017 service_run_next(s, success);
2020 /* No further commands for this step, so let's
2021 * figure out what to do next */
2023 s->control_command = NULL;
2024 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2026 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2030 case SERVICE_START_PRE:
2032 service_enter_start(s);
2034 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2038 assert(s->type == SERVICE_FORKING);
2040 /* Let's try to load the pid
2041 * file here if we can. We
2042 * ignore the return value,
2043 * since the PID file might
2044 * actually be created by a
2045 * START_POST script */
2049 service_load_pid_file(s);
2051 service_enter_start_post(s);
2053 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2057 case SERVICE_START_POST:
2058 if (success && s->pid_file && !s->main_pid_known) {
2061 /* Hmm, let's see if we can
2062 * load the pid now after the
2063 * start-post scripts got
2066 if ((r = service_load_pid_file(s)) < 0)
2067 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
2072 case SERVICE_RELOAD:
2074 service_enter_running(s, true);
2076 service_enter_stop(s, false);
2081 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2084 case SERVICE_STOP_SIGTERM:
2085 case SERVICE_STOP_SIGKILL:
2086 if (main_pid_good(s) <= 0)
2087 service_enter_stop_post(s, success);
2089 /* If there is still a service
2090 * process around, wait until
2091 * that one quit, too */
2094 case SERVICE_STOP_POST:
2095 case SERVICE_FINAL_SIGTERM:
2096 case SERVICE_FINAL_SIGKILL:
2097 service_enter_dead(s, success, true);
2101 assert_not_reached("Uh, control process died at wrong time.");
2105 assert_not_reached("Got SIGCHLD for unkown PID");
2108 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2109 Service *s = SERVICE(u);
2112 assert(elapsed == 1);
2114 assert(w == &s->timer_watch);
2118 case SERVICE_START_PRE:
2120 log_warning("%s operation timed out. Terminating.", u->meta.id);
2121 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2124 case SERVICE_START_POST:
2125 case SERVICE_RELOAD:
2126 log_warning("%s operation timed out. Stopping.", u->meta.id);
2127 service_enter_stop(s, false);
2131 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2132 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2135 case SERVICE_STOP_SIGTERM:
2136 log_warning("%s stopping timed out. Killing.", u->meta.id);
2137 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2140 case SERVICE_STOP_SIGKILL:
2141 /* Uh, wie sent a SIGKILL and it is still not gone?
2142 * Must be something we cannot kill, so let's just be
2143 * weirded out and continue */
2145 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2146 service_enter_stop_post(s, false);
2149 case SERVICE_STOP_POST:
2150 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2151 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2154 case SERVICE_FINAL_SIGTERM:
2155 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2156 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2159 case SERVICE_FINAL_SIGKILL:
2160 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u->meta.id);
2161 service_enter_dead(s, false, true);
2164 case SERVICE_AUTO_RESTART:
2165 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
2166 service_enter_restart(s);
2170 assert_not_reached("Timeout at wrong time.");
2174 static void service_cgroup_notify_event(Unit *u) {
2175 Service *s = SERVICE(u);
2179 log_debug("%s: cgroup is empty", u->meta.id);
2183 /* Waiting for SIGCHLD is usually more interesting,
2184 * because it includes return codes/signals. Which is
2185 * why we ignore the cgroup events for most cases,
2186 * except when we don't know pid which to expect the
2189 case SERVICE_RUNNING:
2190 service_enter_running(s, true);
2198 static int service_enumerate(Manager *m) {
2202 char *path = NULL, *fpath = NULL, *name = NULL;
2207 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2208 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2213 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2221 if (!(d = opendir(path))) {
2222 if (errno != ENOENT)
2223 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2228 while ((de = readdir(d))) {
2232 if (ignore_file(de->d_name))
2235 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2238 if (strlen(de->d_name) < 4)
2241 a = undecchar(de->d_name[1]);
2242 b = undecchar(de->d_name[2]);
2249 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2254 if (access(fpath, X_OK) < 0) {
2256 if (errno != ENOENT)
2257 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2263 if (!(name = sysv_translate_name(de->d_name + 3))) {
2268 if ((r = manager_load_unit_prepare(m, name, NULL, &service)) < 0) {
2269 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2273 if (de->d_name[0] == 'S' &&
2274 (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT))
2275 SERVICE(service)->sysv_start_priority =
2276 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2278 manager_dispatch_load_queue(m);
2279 service = unit_follow_merge(service);
2281 if (de->d_name[0] == 'S') {
2282 Unit *runlevel_target;
2284 if ((r = manager_load_unit(m, rcnd_table[i].target, NULL, &runlevel_target)) < 0)
2287 if ((r = unit_add_dependency(runlevel_target, UNIT_WANTS, service, true)) < 0)
2290 if ((r = unit_add_dependency(service, UNIT_BEFORE, runlevel_target, true)) < 0)
2293 } else if (de->d_name[0] == 'K' &&
2294 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2295 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2296 Unit *shutdown_target;
2298 /* We honour K links only for
2299 * halt/reboot. For the normal
2300 * runlevels we assume the
2302 * implicitly added by the
2303 * core logic. Also, we don't
2304 * really distuingish here
2305 * between the runlevels 0 and
2306 * 6 and just add them to the
2307 * special shutdown target. On
2308 * SUSE the boot.d/ runlevel
2309 * is also used for shutdown,
2310 * so we add links for that
2311 * too to the shutdown
2314 if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
2317 if ((r = unit_add_dependency(service, UNIT_CONFLICTS, shutdown_target, true)) < 0)
2320 if ((r = unit_add_dependency(service, UNIT_BEFORE, shutdown_target, true)) < 0)
2339 static void service_bus_name_owner_change(
2342 const char *old_owner,
2343 const char *new_owner) {
2345 Service *s = SERVICE(u);
2350 assert(streq(s->bus_name, name));
2351 assert(old_owner || new_owner);
2353 if (old_owner && new_owner)
2354 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2356 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2358 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2360 s->bus_name_good = !!new_owner;
2362 if (s->type == SERVICE_DBUS) {
2364 /* service_enter_running() will figure out what to
2366 if (s->state == SERVICE_RUNNING)
2367 service_enter_running(s, true);
2368 else if (s->state == SERVICE_START && new_owner)
2369 service_enter_start_post(s);
2371 } else if (new_owner &&
2373 (s->state == SERVICE_START ||
2374 s->state == SERVICE_START_POST ||
2375 s->state == SERVICE_RUNNING ||
2376 s->state == SERVICE_RELOAD)) {
2378 /* Try to acquire PID from bus service */
2379 log_debug("Trying to acquire PID from D-Bus name...");
2381 bus_query_pid(u->meta.manager, name);
2385 static void service_bus_query_pid_done(
2390 Service *s = SERVICE(u);
2395 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2397 if (s->main_pid <= 0 &&
2398 (s->state == SERVICE_START ||
2399 s->state == SERVICE_START_POST ||
2400 s->state == SERVICE_RUNNING ||
2401 s->state == SERVICE_RELOAD))
2405 int service_set_socket_fd(Service *s, int fd) {
2409 /* This is called by the socket code when instantiating a new
2410 * service for a stream socket and the socket needs to be
2413 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2416 if (s->socket_fd >= 0)
2419 if (s->state != SERVICE_DEAD)
2423 s->got_socket_fd = true;
2427 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2428 [SERVICE_DEAD] = "dead",
2429 [SERVICE_START_PRE] = "start-pre",
2430 [SERVICE_START] = "start",
2431 [SERVICE_START_POST] = "start-post",
2432 [SERVICE_RUNNING] = "running",
2433 [SERVICE_EXITED] = "exited",
2434 [SERVICE_RELOAD] = "reload",
2435 [SERVICE_STOP] = "stop",
2436 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2437 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2438 [SERVICE_STOP_POST] = "stop-post",
2439 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2440 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2441 [SERVICE_MAINTAINANCE] = "maintainance",
2442 [SERVICE_AUTO_RESTART] = "auto-restart",
2445 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2447 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2448 [SERVICE_ONCE] = "once",
2449 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2450 [SERVICE_RESTART_ALWAYS] = "restart-always",
2453 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2455 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2456 [SERVICE_FORKING] = "forking",
2457 [SERVICE_SIMPLE] = "simple",
2458 [SERVICE_FINISH] = "finish",
2459 [SERVICE_DBUS] = "dbus"
2462 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2464 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2465 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2466 [SERVICE_EXEC_START] = "ExecStart",
2467 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2468 [SERVICE_EXEC_RELOAD] = "ExecReload",
2469 [SERVICE_EXEC_STOP] = "ExecStop",
2470 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2473 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2475 const UnitVTable service_vtable = {
2476 .suffix = ".service",
2478 .init = service_init,
2479 .done = service_done,
2480 .load = service_load,
2482 .coldplug = service_coldplug,
2484 .dump = service_dump,
2486 .start = service_start,
2487 .stop = service_stop,
2488 .reload = service_reload,
2490 .can_reload = service_can_reload,
2492 .serialize = service_serialize,
2493 .deserialize_item = service_deserialize_item,
2495 .active_state = service_active_state,
2496 .sub_state_to_string = service_sub_state_to_string,
2498 .check_gc = service_check_gc,
2499 .check_snapshot = service_check_snapshot,
2501 .sigchld_event = service_sigchld_event,
2502 .timer_event = service_timer_event,
2504 .cgroup_notify_empty = service_cgroup_notify_event,
2506 .bus_name_owner_change = service_bus_name_owner_change,
2507 .bus_query_pid_done = service_bus_query_pid_done,
2509 .bus_message_handler = bus_service_message_handler,
2511 .enumerate = service_enumerate