1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/reboot.h>
31 #include "load-fragment.h"
32 #include "load-dropin.h"
35 #include "unit-name.h"
36 #include "unit-printf.h"
37 #include "dbus-service.h"
39 #include "dbus-common.h"
40 #include "exit-status.h"
42 #include "path-util.h"
48 #ifdef HAVE_SYSV_COMPAT
50 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
52 typedef enum RunlevelType {
60 const RunlevelType type;
62 /* Standard SysV runlevels for start-up */
63 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
64 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
65 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
66 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
67 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
69 /* Standard SysV runlevels for shutdown */
70 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
71 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
73 /* Note that the order here matters, as we read the
74 directories in this order, and we want to make sure that
75 sysv_start_priority is known when we first load the
76 unit. And that value we only know from S links. Hence
77 UP must be read before DOWN */
80 #define RUNLEVELS_UP "12345"
83 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
84 [SERVICE_DEAD] = UNIT_INACTIVE,
85 [SERVICE_START_PRE] = UNIT_ACTIVATING,
86 [SERVICE_START] = UNIT_ACTIVATING,
87 [SERVICE_START_POST] = UNIT_ACTIVATING,
88 [SERVICE_RUNNING] = UNIT_ACTIVE,
89 [SERVICE_EXITED] = UNIT_ACTIVE,
90 [SERVICE_RELOAD] = UNIT_RELOADING,
91 [SERVICE_STOP] = UNIT_DEACTIVATING,
92 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
93 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
94 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
95 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
96 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
97 [SERVICE_FAILED] = UNIT_FAILED,
98 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
101 /* For Type=idle we never want to delay any other jobs, hence we
102 * consider idle jobs active as soon as we start working on them */
103 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
104 [SERVICE_DEAD] = UNIT_INACTIVE,
105 [SERVICE_START_PRE] = UNIT_ACTIVE,
106 [SERVICE_START] = UNIT_ACTIVE,
107 [SERVICE_START_POST] = UNIT_ACTIVE,
108 [SERVICE_RUNNING] = UNIT_ACTIVE,
109 [SERVICE_EXITED] = UNIT_ACTIVE,
110 [SERVICE_RELOAD] = UNIT_RELOADING,
111 [SERVICE_STOP] = UNIT_DEACTIVATING,
112 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
113 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
114 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
115 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
116 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
117 [SERVICE_FAILED] = UNIT_FAILED,
118 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
121 static void service_init(Unit *u) {
122 Service *s = SERVICE(u);
125 assert(u->load_state == UNIT_STUB);
127 s->timeout_start_usec = DEFAULT_TIMEOUT_USEC;
128 s->timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
129 s->restart_usec = DEFAULT_RESTART_USEC;
130 s->type = _SERVICE_TYPE_INVALID;
132 watch_init(&s->watchdog_watch);
133 watch_init(&s->timer_watch);
135 #ifdef HAVE_SYSV_COMPAT
136 s->sysv_start_priority = -1;
137 s->sysv_start_priority_from_rcnd = -1;
140 s->guess_main_pid = true;
142 exec_context_init(&s->exec_context);
143 kill_context_init(&s->kill_context);
144 cgroup_context_init(&s->cgroup_context);
146 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
148 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
151 static void service_unwatch_control_pid(Service *s) {
154 if (s->control_pid <= 0)
157 unit_unwatch_pid(UNIT(s), s->control_pid);
161 static void service_unwatch_main_pid(Service *s) {
164 if (s->main_pid <= 0)
167 unit_unwatch_pid(UNIT(s), s->main_pid);
171 static void service_unwatch_pid_file(Service *s) {
172 if (!s->pid_file_pathspec)
175 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
176 UNIT(s)->id, s->pid_file_pathspec->path);
177 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
178 path_spec_done(s->pid_file_pathspec);
179 free(s->pid_file_pathspec);
180 s->pid_file_pathspec = NULL;
183 static int service_set_main_pid(Service *s, pid_t pid) {
195 s->main_pid_known = true;
197 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
198 log_warning_unit(UNIT(s)->id,
199 "%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
200 UNIT(s)->id, (unsigned long) pid);
202 s->main_pid_alien = true;
204 s->main_pid_alien = false;
206 exec_status_start(&s->main_exec_status, pid);
211 static void service_close_socket_fd(Service *s) {
214 if (s->socket_fd < 0)
217 close_nointr_nofail(s->socket_fd);
221 static void service_connection_unref(Service *s) {
224 if (!UNIT_ISSET(s->accept_socket))
227 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
228 unit_ref_unset(&s->accept_socket);
231 static void service_stop_watchdog(Service *s) {
234 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
235 s->watchdog_timestamp.realtime = 0;
236 s->watchdog_timestamp.monotonic = 0;
239 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
241 static void service_handle_watchdog(Service *s) {
247 if (s->watchdog_usec == 0)
250 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
251 if (offset >= s->watchdog_usec) {
252 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
253 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_WATCHDOG);
257 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->watchdog_usec - offset, &s->watchdog_watch);
259 log_warning_unit(UNIT(s)->id,
260 "%s failed to install watchdog timer: %s",
261 UNIT(s)->id, strerror(-r));
264 static void service_reset_watchdog(Service *s) {
267 dual_timestamp_get(&s->watchdog_timestamp);
268 service_handle_watchdog(s);
271 static void service_done(Unit *u) {
272 Service *s = SERVICE(u);
279 #ifdef HAVE_SYSV_COMPAT
280 free(s->sysv_runlevels);
281 s->sysv_runlevels = NULL;
284 free(s->status_text);
285 s->status_text = NULL;
287 cgroup_context_done(&s->cgroup_context);
288 exec_context_done(&s->exec_context, manager_is_reloading_or_reexecuting(u->manager));
289 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
290 s->control_command = NULL;
291 s->main_command = NULL;
293 set_free(s->restart_ignore_status.code);
294 s->restart_ignore_status.code = NULL;
295 set_free(s->restart_ignore_status.signal);
296 s->restart_ignore_status.signal = NULL;
298 set_free(s->success_status.code);
299 s->success_status.code = NULL;
300 set_free(s->success_status.signal);
301 s->success_status.signal = NULL;
303 /* This will leak a process, but at least no memory or any of
305 service_unwatch_main_pid(s);
306 service_unwatch_control_pid(s);
307 service_unwatch_pid_file(s);
310 unit_unwatch_bus_name(u, s->bus_name);
315 service_close_socket_fd(s);
316 service_connection_unref(s);
318 unit_ref_unset(&s->accept_socket);
320 service_stop_watchdog(s);
322 unit_unwatch_timer(u, &s->timer_watch);
325 #ifdef HAVE_SYSV_COMPAT
326 static char *sysv_translate_name(const char *name) {
329 r = new(char, strlen(name) + sizeof(".service"));
333 if (endswith(name, ".sh"))
334 /* Drop .sh suffix */
335 strcpy(stpcpy(r, name) - 3, ".service");
337 /* Normal init script name */
338 strcpy(stpcpy(r, name), ".service");
343 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
345 /* We silently ignore the $ prefix here. According to the LSB
346 * spec it simply indicates whether something is a
347 * standardized name or a distribution-specific one. Since we
348 * just follow what already exists and do not introduce new
349 * uses or names we don't care who introduced a new name. */
351 static const char * const table[] = {
352 /* LSB defined facilities */
354 "network", SPECIAL_NETWORK_TARGET,
355 "named", SPECIAL_NSS_LOOKUP_TARGET,
356 "portmap", SPECIAL_RPCBIND_TARGET,
357 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
359 "time", SPECIAL_TIME_SYNC_TARGET,
369 n = *name == '$' ? name + 1 : name;
371 for (i = 0; i < ELEMENTSOF(table); i += 2) {
373 if (!streq(table[i], n))
379 r = strdup(table[i+1]);
386 /* If we don't know this name, fallback heuristics to figure
387 * out whether something is a target or a service alias. */
390 if (!unit_prefix_is_valid(n))
393 /* Facilities starting with $ are most likely targets */
394 r = unit_name_build(n, NULL, ".target");
395 } else if (filename && streq(name, filename))
396 /* Names equaling the file name of the services are redundant */
399 /* Everything else we assume to be normal service names */
400 r = sysv_translate_name(n);
411 static int sysv_fix_order(Service *s) {
417 if (s->sysv_start_priority < 0)
420 /* For each pair of services where at least one lacks a LSB
421 * header, we use the start priority value to order things. */
423 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
426 bool special_s, special_t;
433 if (UNIT(t)->load_state != UNIT_LOADED)
436 if (t->sysv_start_priority < 0)
439 /* If both units have modern headers we don't care
440 * about the priorities */
441 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
442 (UNIT(t)->fragment_path || t->sysv_has_lsb))
445 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
446 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
448 if (special_t && !special_s)
450 else if (special_s && !special_t)
452 else if (t->sysv_start_priority < s->sysv_start_priority)
454 else if (t->sysv_start_priority > s->sysv_start_priority)
459 /* FIXME: Maybe we should compare the name here lexicographically? */
461 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
468 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
471 if (!(c = new0(ExecCommand, 1)))
474 if (!(c->path = strdup(path))) {
479 if (!(c->argv = strv_new(path, arg1, NULL))) {
488 static int sysv_exec_commands(Service *s, const bool supports_reload) {
493 assert(UNIT(s)->source_path);
495 c = exec_command_new(UNIT(s)->source_path, "start");
498 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
500 c = exec_command_new(UNIT(s)->source_path, "stop");
503 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
505 if (supports_reload) {
506 c = exec_command_new(UNIT(s)->source_path, "reload");
509 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
515 static bool usage_contains_reload(const char *line) {
516 return (strcasestr(line, "{reload|") ||
517 strcasestr(line, "{reload}") ||
518 strcasestr(line, "{reload\"") ||
519 strcasestr(line, "|reload|") ||
520 strcasestr(line, "|reload}") ||
521 strcasestr(line, "|reload\""));
524 static int service_load_sysv_path(Service *s, const char *path) {
536 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
538 bool supports_reload = false;
545 f = fopen(path, "re");
547 r = errno == ENOENT ? 0 : -errno;
551 if (fstat(fileno(f), &st) < 0) {
556 free(u->source_path);
557 u->source_path = strdup(path);
558 if (!u->source_path) {
562 u->source_mtime = timespec_load(&st.st_mtim);
564 if (null_or_empty(&st)) {
565 u->load_state = UNIT_MASKED;
573 char l[LINE_MAX], *t;
575 if (!fgets(l, sizeof(l), f)) {
580 log_error_unit(u->id,
581 "Failed to read configuration file '%s': %s",
590 /* Try to figure out whether this init script supports
591 * the reload operation. This heuristic looks for
592 * "Usage" lines which include the reload option. */
593 if ( state == USAGE_CONTINUATION ||
594 (state == NORMAL && strcasestr(t, "usage"))) {
595 if (usage_contains_reload(t)) {
596 supports_reload = true;
598 } else if (t[strlen(t)-1] == '\\')
599 state = USAGE_CONTINUATION;
607 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
609 s->sysv_has_lsb = true;
613 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
619 t += strspn(t, WHITESPACE);
621 if (state == NORMAL) {
623 /* Try to parse Red Hat style chkconfig headers */
625 if (startswith_no_case(t, "chkconfig:")) {
627 char runlevels[16], *k;
631 if (sscanf(t+10, "%15s %i %*i",
633 &start_priority) != 2) {
635 log_warning_unit(u->id,
636 "[%s:%u] Failed to parse chkconfig line. Ignoring.",
641 /* A start priority gathered from the
642 * symlink farms is preferred over the
643 * data from the LSB header. */
644 if (start_priority < 0 || start_priority > 99)
645 log_warning_unit(u->id,
646 "[%s:%u] Start priority out of range. Ignoring.",
649 s->sysv_start_priority = start_priority;
651 char_array_0(runlevels);
652 k = delete_chars(runlevels, WHITESPACE "-");
657 if (!(d = strdup(k))) {
662 free(s->sysv_runlevels);
663 s->sysv_runlevels = d;
666 } else if (startswith_no_case(t, "description:")) {
668 size_t k = strlen(t);
672 if (t[k-1] == '\\') {
677 if ((j = strstrip(t+12)) && *j) {
678 if (!(d = strdup(j))) {
685 free(chkconfig_description);
686 chkconfig_description = d;
688 } else if (startswith_no_case(t, "pidfile:")) {
695 if (!path_is_absolute(fn)) {
696 log_warning_unit(u->id,
697 "[%s:%u] PID file not absolute. Ignoring.",
702 if (!(fn = strdup(fn))) {
711 } else if (state == DESCRIPTION) {
713 /* Try to parse Red Hat style description
716 size_t k = strlen(t);
724 if ((j = strstrip(t)) && *j) {
727 if (chkconfig_description)
728 d = strjoin(chkconfig_description, " ", j, NULL);
737 free(chkconfig_description);
738 chkconfig_description = d;
741 } else if (state == LSB || state == LSB_DESCRIPTION) {
743 if (startswith_no_case(t, "Provides:")) {
749 FOREACH_WORD_QUOTED(w, z, t+9, i) {
752 if (!(n = strndup(w, z))) {
757 r = sysv_translate_facility(n, path_get_file_name(path), &m);
766 if (unit_name_to_type(m) == UNIT_SERVICE)
767 r = unit_merge_by_name(u, m);
774 * indication that the
776 * now available. This
779 * targets do NOT pull
782 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
785 log_error_unit(u->id,
786 "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
787 path, line, m, strerror(-r));
792 } else if (startswith_no_case(t, "Required-Start:") ||
793 startswith_no_case(t, "Should-Start:") ||
794 startswith_no_case(t, "X-Start-Before:") ||
795 startswith_no_case(t, "X-Start-After:")) {
801 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
804 if (!(n = strndup(w, z))) {
809 r = sysv_translate_facility(n, path_get_file_name(path), &m);
811 log_error_unit(u->id,
812 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
813 path, line, n, strerror(-r));
823 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
826 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
827 path, line, m, strerror(-r));
831 } else if (startswith_no_case(t, "Default-Start:")) {
836 k = delete_chars(t+14, WHITESPACE "-");
839 if (!(d = strdup(k))) {
844 free(s->sysv_runlevels);
845 s->sysv_runlevels = d;
848 } else if (startswith_no_case(t, "Description:")) {
851 state = LSB_DESCRIPTION;
853 if ((j = strstrip(t+12)) && *j) {
854 if (!(d = strdup(j))) {
861 free(long_description);
862 long_description = d;
864 } else if (startswith_no_case(t, "Short-Description:")) {
869 if ((j = strstrip(t+18)) && *j) {
870 if (!(d = strdup(j))) {
877 free(short_description);
878 short_description = d;
880 } else if (state == LSB_DESCRIPTION) {
882 if (startswith(l, "#\t") || startswith(l, "# ")) {
885 if ((j = strstrip(t)) && *j) {
888 if (long_description)
889 d = strjoin(long_description, " ", t, NULL);
898 free(long_description);
899 long_description = d;
908 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
911 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
912 /* If there a runlevels configured for this service
913 * but none of the standard ones, then we assume this
914 * is some special kind of service (which might be
915 * needed for early boot) and don't create any links
918 UNIT(s)->default_dependencies = false;
920 /* Don't timeout special services during boot (like fsck) */
921 s->timeout_start_usec = 0;
922 s->timeout_stop_usec = 0;
924 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
925 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
928 /* Special setting for all SysV services */
929 s->type = SERVICE_FORKING;
930 s->remain_after_exit = !s->pid_file;
931 s->guess_main_pid = false;
932 s->restart = SERVICE_RESTART_NO;
933 s->exec_context.ignore_sigpipe = false;
934 s->kill_context.kill_mode = KILL_PROCESS;
936 /* We use the long description only if
937 * no short description is set. */
939 if (short_description)
940 description = short_description;
941 else if (chkconfig_description)
942 description = chkconfig_description;
943 else if (long_description)
944 description = long_description;
951 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
959 /* The priority that has been set in /etc/rcN.d/ hierarchies
960 * takes precedence over what is stored as default in the LSB
962 if (s->sysv_start_priority_from_rcnd >= 0)
963 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
965 u->load_state = UNIT_LOADED;
973 free(short_description);
974 free(long_description);
975 free(chkconfig_description);
980 static int service_load_sysv_name(Service *s, const char *name) {
986 /* For SysV services we strip the *.sh suffixes. */
987 if (endswith(name, ".sh.service"))
990 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
994 path = strjoin(*p, "/", name, NULL);
998 assert(endswith(path, ".service"));
999 path[strlen(path)-8] = 0;
1001 r = service_load_sysv_path(s, path);
1003 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1004 /* Try *.sh source'able init scripts */
1005 strcat(path, ".sh");
1006 r = service_load_sysv_path(s, path);
1013 if (UNIT(s)->load_state != UNIT_STUB)
1020 static int service_load_sysv(Service *s) {
1027 /* Load service data from SysV init scripts, preferably with
1028 * LSB headers ... */
1030 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1033 if ((t = UNIT(s)->id))
1034 if ((r = service_load_sysv_name(s, t)) < 0)
1037 if (UNIT(s)->load_state == UNIT_STUB)
1038 SET_FOREACH(t, UNIT(s)->names, i) {
1039 if (t == UNIT(s)->id)
1042 if ((r = service_load_sysv_name(s, t)) < 0)
1045 if (UNIT(s)->load_state != UNIT_STUB)
1053 static int fsck_fix_order(Service *s) {
1059 if (s->fsck_passno <= 0)
1062 /* For each pair of services where both have an fsck priority
1063 * we order things based on it. */
1065 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1074 if (UNIT(t)->load_state != UNIT_LOADED)
1077 if (t->fsck_passno <= 0)
1080 if (t->fsck_passno < s->fsck_passno)
1082 else if (t->fsck_passno > s->fsck_passno)
1087 r = unit_add_dependency(UNIT(s), d, UNIT(t), true);
1095 static int service_verify(Service *s) {
1098 if (UNIT(s)->load_state != UNIT_LOADED)
1101 if (!s->exec_command[SERVICE_EXEC_START]) {
1102 log_error_unit(UNIT(s)->id,
1103 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1107 if (s->type != SERVICE_ONESHOT &&
1108 s->exec_command[SERVICE_EXEC_START]->command_next) {
1109 log_error_unit(UNIT(s)->id,
1110 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1114 if (s->type == SERVICE_DBUS && !s->bus_name) {
1115 log_error_unit(UNIT(s)->id,
1116 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1120 if (s->bus_name && s->type != SERVICE_DBUS)
1121 log_warning_unit(UNIT(s)->id,
1122 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1124 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1125 log_error_unit(UNIT(s)->id,
1126 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1133 static int service_add_default_dependencies(Service *s) {
1138 /* Add a number of automatic dependencies useful for the
1139 * majority of services. */
1141 /* First, pull in base system */
1142 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1143 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1144 SPECIAL_BASIC_TARGET, NULL, true);
1148 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1149 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1150 SPECIAL_SOCKETS_TARGET, NULL, true);
1154 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1155 SPECIAL_TIMERS_TARGET, NULL, true);
1159 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1160 SPECIAL_PATHS_TARGET, NULL, true);
1165 /* Second, activate normal shutdown */
1166 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
1167 SPECIAL_SHUTDOWN_TARGET, NULL, true);
1171 static void service_fix_output(Service *s) {
1174 /* If nothing has been explicitly configured, patch default
1175 * output in. If input is socket/tty we avoid this however,
1176 * since in that case we want output to default to the same
1177 * place as we read input from. */
1179 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1180 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1181 s->exec_context.std_input == EXEC_INPUT_NULL)
1182 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1184 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1185 s->exec_context.std_input == EXEC_INPUT_NULL)
1186 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1189 static int service_load(Unit *u) {
1191 Service *s = SERVICE(u);
1195 /* Load a .service file */
1196 if ((r = unit_load_fragment(u)) < 0)
1199 #ifdef HAVE_SYSV_COMPAT
1200 /* Load a classic init script as a fallback, if we couldn't find anything */
1201 if (u->load_state == UNIT_STUB)
1202 if ((r = service_load_sysv(s)) < 0)
1206 /* Still nothing found? Then let's give up */
1207 if (u->load_state == UNIT_STUB)
1210 /* We were able to load something, then let's add in the
1211 * dropin directories. */
1212 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1215 /* This is a new unit? Then let's add in some extras */
1216 if (u->load_state == UNIT_LOADED) {
1217 if (s->type == _SERVICE_TYPE_INVALID)
1218 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1220 /* Oneshot services have disabled start timeout by default */
1221 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1222 s->timeout_start_usec = 0;
1224 service_fix_output(s);
1226 r = unit_add_exec_dependencies(u, &s->exec_context);
1230 r = unit_add_default_slice(u);
1234 #ifdef HAVE_SYSV_COMPAT
1235 r = sysv_fix_order(s);
1240 r = fsck_fix_order(s);
1245 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1248 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1249 s->notify_access = NOTIFY_MAIN;
1251 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1252 s->notify_access = NOTIFY_MAIN;
1254 if (s->type == SERVICE_DBUS || s->bus_name) {
1255 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES,
1256 SPECIAL_DBUS_SOCKET, NULL, true);
1261 if (UNIT(s)->default_dependencies) {
1262 r = service_add_default_dependencies(s);
1267 r = unit_exec_context_defaults(u, &s->exec_context);
1272 return service_verify(s);
1275 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1277 ServiceExecCommand c;
1278 Service *s = SERVICE(u);
1279 const char *prefix2;
1280 _cleanup_free_ char *p2 = NULL;
1284 p2 = strappend(prefix, "\t");
1285 prefix2 = p2 ? p2 : prefix;
1288 "%sService State: %s\n"
1290 "%sReload Result: %s\n"
1291 "%sPermissionsStartOnly: %s\n"
1292 "%sRootDirectoryStartOnly: %s\n"
1293 "%sRemainAfterExit: %s\n"
1294 "%sGuessMainPID: %s\n"
1297 "%sNotifyAccess: %s\n",
1298 prefix, service_state_to_string(s->state),
1299 prefix, service_result_to_string(s->result),
1300 prefix, service_result_to_string(s->reload_result),
1301 prefix, yes_no(s->permissions_start_only),
1302 prefix, yes_no(s->root_directory_start_only),
1303 prefix, yes_no(s->remain_after_exit),
1304 prefix, yes_no(s->guess_main_pid),
1305 prefix, service_type_to_string(s->type),
1306 prefix, service_restart_to_string(s->restart),
1307 prefix, notify_access_to_string(s->notify_access));
1309 if (s->control_pid > 0)
1311 "%sControl PID: %lu\n",
1312 prefix, (unsigned long) s->control_pid);
1314 if (s->main_pid > 0)
1317 "%sMain PID Known: %s\n"
1318 "%sMain PID Alien: %s\n",
1319 prefix, (unsigned long) s->main_pid,
1320 prefix, yes_no(s->main_pid_known),
1321 prefix, yes_no(s->main_pid_alien));
1326 prefix, s->pid_file);
1331 "%sBus Name Good: %s\n",
1332 prefix, s->bus_name,
1333 prefix, yes_no(s->bus_name_good));
1335 kill_context_dump(&s->kill_context, f, prefix);
1336 exec_context_dump(&s->exec_context, f, prefix);
1338 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1340 if (!s->exec_command[c])
1343 fprintf(f, "%s-> %s:\n",
1344 prefix, service_exec_command_to_string(c));
1346 exec_command_dump_list(s->exec_command[c], f, prefix2);
1349 #ifdef HAVE_SYSV_COMPAT
1352 "%sSysV Init Script has LSB Header: %s\n"
1353 "%sSysVEnabled: %s\n",
1354 prefix, yes_no(s->sysv_has_lsb),
1355 prefix, yes_no(s->sysv_enabled));
1357 if (s->sysv_start_priority >= 0)
1359 "%sSysVStartPriority: %i\n",
1360 prefix, s->sysv_start_priority);
1362 if (s->sysv_runlevels)
1363 fprintf(f, "%sSysVRunLevels: %s\n",
1364 prefix, s->sysv_runlevels);
1367 if (s->fsck_passno > 0)
1369 "%sFsckPassNo: %i\n",
1370 prefix, s->fsck_passno);
1373 fprintf(f, "%sStatus Text: %s\n",
1374 prefix, s->status_text);
1377 static int service_load_pid_file(Service *s, bool may_warn) {
1378 _cleanup_free_ char *k = NULL;
1387 r = read_one_line_file(s->pid_file, &k);
1390 log_info_unit(UNIT(s)->id,
1391 "PID file %s not readable (yet?) after %s.",
1392 s->pid_file, service_state_to_string(s->state));
1396 r = parse_pid(k, &pid);
1399 log_info_unit(UNIT(s)->id,
1400 "Failed to read PID from file %s: %s",
1401 s->pid_file, strerror(-r));
1405 if (kill(pid, 0) < 0 && errno != EPERM) {
1407 log_info_unit(UNIT(s)->id,
1408 "PID %lu read from file %s does not exist.",
1409 (unsigned long) pid, s->pid_file);
1413 if (s->main_pid_known) {
1414 if (pid == s->main_pid)
1417 log_debug_unit(UNIT(s)->id,
1418 "Main PID changing: %lu -> %lu",
1419 (unsigned long) s->main_pid, (unsigned long) pid);
1420 service_unwatch_main_pid(s);
1421 s->main_pid_known = false;
1423 log_debug_unit(UNIT(s)->id,
1424 "Main PID loaded: %lu", (unsigned long) pid);
1426 r = service_set_main_pid(s, pid);
1430 r = unit_watch_pid(UNIT(s), pid);
1432 /* FIXME: we need to do something here */
1433 log_warning_unit(UNIT(s)->id,
1434 "Failed to watch PID %lu from service %s",
1435 (unsigned long) pid, UNIT(s)->id);
1442 static int service_search_main_pid(Service *s) {
1448 /* If we know it anyway, don't ever fallback to unreliable
1450 if (s->main_pid_known)
1453 if (!s->guess_main_pid)
1456 assert(s->main_pid <= 0);
1458 pid = unit_search_main_pid(UNIT(s));
1462 log_debug_unit(UNIT(s)->id,
1463 "Main PID guessed: %lu", (unsigned long) pid);
1464 r = service_set_main_pid(s, pid);
1468 r = unit_watch_pid(UNIT(s), pid);
1470 /* FIXME: we need to do something here */
1471 log_warning_unit(UNIT(s)->id,
1472 "Failed to watch PID %lu from service %s",
1473 (unsigned long) pid, UNIT(s)->id);
1479 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1485 /* Notifies all our sockets when we die */
1487 if (s->socket_fd >= 0)
1490 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1491 if (u->type == UNIT_SOCKET)
1492 socket_notify_service_dead(SOCKET(u), failed_permanent);
1497 static void service_set_state(Service *s, ServiceState state) {
1498 ServiceState old_state;
1499 const UnitActiveState *table;
1502 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1504 old_state = s->state;
1507 service_unwatch_pid_file(s);
1509 if (state != SERVICE_START_PRE &&
1510 state != SERVICE_START &&
1511 state != SERVICE_START_POST &&
1512 state != SERVICE_RELOAD &&
1513 state != SERVICE_STOP &&
1514 state != SERVICE_STOP_SIGTERM &&
1515 state != SERVICE_STOP_SIGKILL &&
1516 state != SERVICE_STOP_POST &&
1517 state != SERVICE_FINAL_SIGTERM &&
1518 state != SERVICE_FINAL_SIGKILL &&
1519 state != SERVICE_AUTO_RESTART)
1520 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1522 if (state != SERVICE_START &&
1523 state != SERVICE_START_POST &&
1524 state != SERVICE_RUNNING &&
1525 state != SERVICE_RELOAD &&
1526 state != SERVICE_STOP &&
1527 state != SERVICE_STOP_SIGTERM &&
1528 state != SERVICE_STOP_SIGKILL) {
1529 service_unwatch_main_pid(s);
1530 s->main_command = NULL;
1533 if (state != SERVICE_START_PRE &&
1534 state != SERVICE_START &&
1535 state != SERVICE_START_POST &&
1536 state != SERVICE_RELOAD &&
1537 state != SERVICE_STOP &&
1538 state != SERVICE_STOP_SIGTERM &&
1539 state != SERVICE_STOP_SIGKILL &&
1540 state != SERVICE_STOP_POST &&
1541 state != SERVICE_FINAL_SIGTERM &&
1542 state != SERVICE_FINAL_SIGKILL) {
1543 service_unwatch_control_pid(s);
1544 s->control_command = NULL;
1545 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1548 if (state == SERVICE_FAILED)
1549 service_notify_sockets_dead(s, s->result == SERVICE_FAILURE_START_LIMIT);
1551 if (state == SERVICE_DEAD ||
1552 state == SERVICE_STOP ||
1553 state == SERVICE_STOP_SIGTERM ||
1554 state == SERVICE_STOP_SIGKILL ||
1555 state == SERVICE_STOP_POST ||
1556 state == SERVICE_FINAL_SIGTERM ||
1557 state == SERVICE_FINAL_SIGKILL ||
1558 state == SERVICE_AUTO_RESTART)
1559 service_notify_sockets_dead(s, false);
1561 if (state != SERVICE_START_PRE &&
1562 state != SERVICE_START &&
1563 state != SERVICE_START_POST &&
1564 state != SERVICE_RUNNING &&
1565 state != SERVICE_RELOAD &&
1566 state != SERVICE_STOP &&
1567 state != SERVICE_STOP_SIGTERM &&
1568 state != SERVICE_STOP_SIGKILL &&
1569 state != SERVICE_STOP_POST &&
1570 state != SERVICE_FINAL_SIGTERM &&
1571 state != SERVICE_FINAL_SIGKILL &&
1572 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1573 service_close_socket_fd(s);
1574 service_connection_unref(s);
1577 if (state == SERVICE_STOP || state == SERVICE_STOP_SIGTERM)
1578 service_stop_watchdog(s);
1580 /* For the inactive states unit_notify() will trim the cgroup,
1581 * but for exit we have to do that ourselves... */
1582 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1583 unit_destroy_cgroup(UNIT(s));
1585 if (old_state != state)
1586 log_debug_unit(UNIT(s)->id,
1587 "%s changed %s -> %s", UNIT(s)->id,
1588 service_state_to_string(old_state),
1589 service_state_to_string(state));
1591 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1592 s->reload_result = SERVICE_SUCCESS;
1595 static int service_coldplug(Unit *u) {
1596 Service *s = SERVICE(u);
1600 assert(s->state == SERVICE_DEAD);
1602 if (s->deserialized_state != s->state) {
1604 if (s->deserialized_state == SERVICE_START_PRE ||
1605 s->deserialized_state == SERVICE_START ||
1606 s->deserialized_state == SERVICE_START_POST ||
1607 s->deserialized_state == SERVICE_RELOAD ||
1608 s->deserialized_state == SERVICE_STOP ||
1609 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1610 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1611 s->deserialized_state == SERVICE_STOP_POST ||
1612 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1613 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1614 s->deserialized_state == SERVICE_AUTO_RESTART) {
1615 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1618 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1620 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
1626 if ((s->deserialized_state == SERVICE_START &&
1627 (s->type == SERVICE_FORKING ||
1628 s->type == SERVICE_DBUS ||
1629 s->type == SERVICE_ONESHOT ||
1630 s->type == SERVICE_NOTIFY)) ||
1631 s->deserialized_state == SERVICE_START_POST ||
1632 s->deserialized_state == SERVICE_RUNNING ||
1633 s->deserialized_state == SERVICE_RELOAD ||
1634 s->deserialized_state == SERVICE_STOP ||
1635 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1636 s->deserialized_state == SERVICE_STOP_SIGKILL)
1637 if (s->main_pid > 0) {
1638 r = unit_watch_pid(UNIT(s), s->main_pid);
1643 if (s->deserialized_state == SERVICE_START_PRE ||
1644 s->deserialized_state == SERVICE_START ||
1645 s->deserialized_state == SERVICE_START_POST ||
1646 s->deserialized_state == SERVICE_RELOAD ||
1647 s->deserialized_state == SERVICE_STOP ||
1648 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1649 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1650 s->deserialized_state == SERVICE_STOP_POST ||
1651 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1652 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1653 if (s->control_pid > 0) {
1654 r = unit_watch_pid(UNIT(s), s->control_pid);
1659 if (s->deserialized_state == SERVICE_START_POST ||
1660 s->deserialized_state == SERVICE_RUNNING)
1661 service_handle_watchdog(s);
1663 service_set_state(s, s->deserialized_state);
1668 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1672 unsigned rn_fds = 0;
1679 if (s->socket_fd >= 0)
1682 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1687 if (u->type != UNIT_SOCKET)
1692 r = socket_collect_fds(sock, &cfds, &cn_fds);
1705 t = new(int, rn_fds+cn_fds);
1712 memcpy(t, rfds, rn_fds * sizeof(int));
1713 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1718 rn_fds = rn_fds+cn_fds;
1733 static int service_spawn(
1738 bool apply_permissions,
1740 bool apply_tty_stdin,
1741 bool set_notify_socket,
1748 _cleanup_free_ int *fdsbuf = NULL;
1749 unsigned n_fds = 0, n_env = 0;
1750 _cleanup_strv_free_ char
1751 **argv = NULL, **final_env = NULL, **our_env = NULL;
1758 unit_realize_cgroup(UNIT(s));
1761 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1762 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1763 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1765 if (s->socket_fd >= 0) {
1766 fds = &s->socket_fd;
1769 r = service_collect_fds(s, &fdsbuf, &n_fds);
1777 if (timeout && s->timeout_start_usec) {
1778 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true,
1779 s->timeout_start_usec, &s->timer_watch);
1783 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1785 argv = unit_full_printf_strv(UNIT(s), c->argv);
1791 our_env = new0(char*, 5);
1797 if (set_notify_socket)
1798 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1803 if (s->main_pid > 0)
1804 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1809 if (s->watchdog_usec > 0)
1810 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1815 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
1816 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1821 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1827 if (is_control && UNIT(s)->cgroup_path) {
1828 path = strappenda(UNIT(s)->cgroup_path, "/control");
1829 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1831 path = UNIT(s)->cgroup_path;
1841 UNIT(s)->manager->confirm_spawn,
1842 UNIT(s)->cgroup_mask,
1845 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1850 r = unit_watch_pid(UNIT(s), pid);
1852 /* FIXME: we need to do something here */
1861 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1866 static int main_pid_good(Service *s) {
1869 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1872 /* If we know the pid file, then lets just check if it is
1874 if (s->main_pid_known) {
1876 /* If it's an alien child let's check if it is still
1878 if (s->main_pid_alien && s->main_pid > 0)
1879 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1881 /* .. otherwise assume we'll get a SIGCHLD for it,
1882 * which we really should wait for to collect exit
1883 * status and code */
1884 return s->main_pid > 0;
1887 /* We don't know the pid */
1891 _pure_ static int control_pid_good(Service *s) {
1894 return s->control_pid > 0;
1897 static int cgroup_good(Service *s) {
1902 if (!UNIT(s)->cgroup_path)
1905 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1912 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1916 if (f != SERVICE_SUCCESS)
1919 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1921 if (allow_restart &&
1922 !s->forbid_restart &&
1923 (s->restart == SERVICE_RESTART_ALWAYS ||
1924 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1925 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1926 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1927 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1928 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1929 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1930 (s->result != SERVICE_FAILURE_SIGNAL ||
1931 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1934 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
1938 service_set_state(s, SERVICE_AUTO_RESTART);
1941 s->forbid_restart = false;
1943 /* we want fresh tmpdirs in case service is started again immediately */
1944 exec_context_tmp_dirs_done(&s->exec_context);
1949 log_warning_unit(UNIT(s)->id,
1950 "%s failed to run install restart timer: %s",
1951 UNIT(s)->id, strerror(-r));
1952 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1955 static void service_enter_stop_post(Service *s, ServiceResult f) {
1959 if (f != SERVICE_SUCCESS)
1962 service_unwatch_control_pid(s);
1964 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1965 if (s->control_command) {
1966 s->control_command_id = SERVICE_EXEC_STOP_POST;
1968 r = service_spawn(s,
1972 !s->permissions_start_only,
1973 !s->root_directory_start_only,
1982 service_set_state(s, SERVICE_STOP_POST);
1984 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1989 log_warning_unit(UNIT(s)->id,
1990 "%s failed to run 'stop-post' task: %s",
1991 UNIT(s)->id, strerror(-r));
1992 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1995 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
2000 if (f != SERVICE_SUCCESS)
2003 r = unit_kill_context(
2006 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
2014 if (s->timeout_stop_usec > 0) {
2015 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true,
2016 s->timeout_stop_usec, &s->timer_watch);
2021 service_set_state(s, state);
2022 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2023 service_enter_stop_post(s, SERVICE_SUCCESS);
2025 service_enter_dead(s, SERVICE_SUCCESS, true);
2030 log_warning_unit(UNIT(s)->id,
2031 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2033 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2034 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2036 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2039 static void service_enter_stop(Service *s, ServiceResult f) {
2044 if (f != SERVICE_SUCCESS)
2047 service_unwatch_control_pid(s);
2049 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
2050 if (s->control_command) {
2051 s->control_command_id = SERVICE_EXEC_STOP;
2053 r = service_spawn(s,
2057 !s->permissions_start_only,
2058 !s->root_directory_start_only,
2066 service_set_state(s, SERVICE_STOP);
2068 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2073 log_warning_unit(UNIT(s)->id,
2074 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2075 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2078 static void service_enter_running(Service *s, ServiceResult f) {
2079 int main_pid_ok, cgroup_ok;
2082 if (f != SERVICE_SUCCESS)
2085 main_pid_ok = main_pid_good(s);
2086 cgroup_ok = cgroup_good(s);
2088 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2089 (s->bus_name_good || s->type != SERVICE_DBUS))
2090 service_set_state(s, SERVICE_RUNNING);
2091 else if (s->remain_after_exit)
2092 service_set_state(s, SERVICE_EXITED);
2094 service_enter_stop(s, SERVICE_SUCCESS);
2097 static void service_enter_start_post(Service *s) {
2101 service_unwatch_control_pid(s);
2103 if (s->watchdog_usec > 0)
2104 service_reset_watchdog(s);
2106 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
2107 if (s->control_command) {
2108 s->control_command_id = SERVICE_EXEC_START_POST;
2110 r = service_spawn(s,
2114 !s->permissions_start_only,
2115 !s->root_directory_start_only,
2123 service_set_state(s, SERVICE_START_POST);
2125 service_enter_running(s, SERVICE_SUCCESS);
2130 log_warning_unit(UNIT(s)->id,
2131 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2132 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2135 static void service_kill_control_processes(Service *s) {
2138 if (!UNIT(s)->cgroup_path)
2141 p = strappenda(UNIT(s)->cgroup_path, "/control");
2143 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
2146 static void service_enter_start(Service *s) {
2153 assert(s->exec_command[SERVICE_EXEC_START]);
2154 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2156 if (s->type == SERVICE_FORKING)
2157 service_unwatch_control_pid(s);
2159 service_unwatch_main_pid(s);
2161 /* We want to ensure that nobody leaks processes from
2162 * START_PRE here, so let's go on a killing spree, People
2163 * should not spawn long running processes from START_PRE. */
2164 service_kill_control_processes(s);
2166 if (s->type == SERVICE_FORKING) {
2167 s->control_command_id = SERVICE_EXEC_START;
2168 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2170 s->main_command = NULL;
2172 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2173 s->control_command = NULL;
2175 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2178 r = service_spawn(s,
2180 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
2181 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2186 s->notify_access != NOTIFY_NONE,
2192 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2193 /* For simple services we immediately start
2194 * the START_POST binaries. */
2196 service_set_main_pid(s, pid);
2197 service_enter_start_post(s);
2199 } else if (s->type == SERVICE_FORKING) {
2201 /* For forking services we wait until the start
2202 * process exited. */
2204 s->control_pid = pid;
2205 service_set_state(s, SERVICE_START);
2207 } else if (s->type == SERVICE_ONESHOT ||
2208 s->type == SERVICE_DBUS ||
2209 s->type == SERVICE_NOTIFY) {
2211 /* For oneshot services we wait until the start
2212 * process exited, too, but it is our main process. */
2214 /* For D-Bus services we know the main pid right away,
2215 * but wait for the bus name to appear on the
2216 * bus. Notify services are similar. */
2218 service_set_main_pid(s, pid);
2219 service_set_state(s, SERVICE_START);
2221 assert_not_reached("Unknown service type");
2226 log_warning_unit(UNIT(s)->id,
2227 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2228 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2231 static void service_enter_start_pre(Service *s) {
2236 service_unwatch_control_pid(s);
2238 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
2239 if (s->control_command) {
2240 /* Before we start anything, let's clear up what might
2241 * be left from previous runs. */
2242 service_kill_control_processes(s);
2244 s->control_command_id = SERVICE_EXEC_START_PRE;
2246 r = service_spawn(s,
2250 !s->permissions_start_only,
2251 !s->root_directory_start_only,
2259 service_set_state(s, SERVICE_START_PRE);
2261 service_enter_start(s);
2266 log_warning_unit(UNIT(s)->id,
2267 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2268 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2271 static void service_enter_restart(Service *s) {
2276 dbus_error_init(&error);
2278 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2279 /* Don't restart things if we are going down anyway */
2280 log_info_unit(UNIT(s)->id,
2281 "Stop job pending for unit, delaying automatic restart.");
2283 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
2290 /* Any units that are bound to this service must also be
2291 * restarted. We use JOB_RESTART (instead of the more obvious
2292 * JOB_START) here so that those dependency jobs will be added
2294 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2298 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2299 * it will be canceled as part of the service_stop() call that
2300 * is executed as part of JOB_RESTART. */
2302 log_debug_unit(UNIT(s)->id,
2303 "%s scheduled restart job.", UNIT(s)->id);
2307 log_warning_unit(UNIT(s)->id,
2308 "%s failed to schedule restart job: %s",
2309 UNIT(s)->id, bus_error(&error, -r));
2310 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2312 dbus_error_free(&error);
2315 static void service_enter_reload(Service *s) {
2320 service_unwatch_control_pid(s);
2322 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
2323 if (s->control_command) {
2324 s->control_command_id = SERVICE_EXEC_RELOAD;
2326 r = service_spawn(s,
2330 !s->permissions_start_only,
2331 !s->root_directory_start_only,
2339 service_set_state(s, SERVICE_RELOAD);
2341 service_enter_running(s, SERVICE_SUCCESS);
2346 log_warning_unit(UNIT(s)->id,
2347 "%s failed to run 'reload' task: %s",
2348 UNIT(s)->id, strerror(-r));
2349 s->reload_result = SERVICE_FAILURE_RESOURCES;
2350 service_enter_running(s, SERVICE_SUCCESS);
2353 static void service_run_next_control(Service *s) {
2357 assert(s->control_command);
2358 assert(s->control_command->command_next);
2360 assert(s->control_command_id != SERVICE_EXEC_START);
2362 s->control_command = s->control_command->command_next;
2363 service_unwatch_control_pid(s);
2365 r = service_spawn(s,
2369 !s->permissions_start_only,
2370 !s->root_directory_start_only,
2371 s->control_command_id == SERVICE_EXEC_START_PRE ||
2372 s->control_command_id == SERVICE_EXEC_STOP_POST,
2382 log_warning_unit(UNIT(s)->id,
2383 "%s failed to run next control task: %s",
2384 UNIT(s)->id, strerror(-r));
2386 if (s->state == SERVICE_START_PRE)
2387 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2388 else if (s->state == SERVICE_STOP)
2389 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2390 else if (s->state == SERVICE_STOP_POST)
2391 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2392 else if (s->state == SERVICE_RELOAD) {
2393 s->reload_result = SERVICE_FAILURE_RESOURCES;
2394 service_enter_running(s, SERVICE_SUCCESS);
2396 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2399 static void service_run_next_main(Service *s) {
2404 assert(s->main_command);
2405 assert(s->main_command->command_next);
2406 assert(s->type == SERVICE_ONESHOT);
2408 s->main_command = s->main_command->command_next;
2409 service_unwatch_main_pid(s);
2411 r = service_spawn(s,
2418 s->notify_access != NOTIFY_NONE,
2424 service_set_main_pid(s, pid);
2429 log_warning_unit(UNIT(s)->id,
2430 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2431 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2434 static int service_start_limit_test(Service *s) {
2437 if (ratelimit_test(&s->start_limit))
2440 switch (s->start_limit_action) {
2442 case SERVICE_START_LIMIT_NONE:
2443 log_warning_unit(UNIT(s)->id,
2444 "%s start request repeated too quickly, refusing to start.",
2448 case SERVICE_START_LIMIT_REBOOT: {
2452 dbus_error_init(&error);
2454 log_warning_unit(UNIT(s)->id,
2455 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2457 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
2458 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
2459 true, &error, NULL);
2461 log_error_unit(UNIT(s)->id,
2462 "Failed to reboot: %s.", bus_error(&error, r));
2463 dbus_error_free(&error);
2469 case SERVICE_START_LIMIT_REBOOT_FORCE:
2470 log_warning_unit(UNIT(s)->id,
2471 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2472 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2475 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2476 log_warning_unit(UNIT(s)->id,
2477 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2479 reboot(RB_AUTOBOOT);
2483 log_error_unit(UNIT(s)->id,
2484 "start limit action=%i", s->start_limit_action);
2485 assert_not_reached("Unknown StartLimitAction.");
2491 static int service_start(Unit *u) {
2492 Service *s = SERVICE(u);
2497 /* We cannot fulfill this request right now, try again later
2499 if (s->state == SERVICE_STOP ||
2500 s->state == SERVICE_STOP_SIGTERM ||
2501 s->state == SERVICE_STOP_SIGKILL ||
2502 s->state == SERVICE_STOP_POST ||
2503 s->state == SERVICE_FINAL_SIGTERM ||
2504 s->state == SERVICE_FINAL_SIGKILL)
2507 /* Already on it! */
2508 if (s->state == SERVICE_START_PRE ||
2509 s->state == SERVICE_START ||
2510 s->state == SERVICE_START_POST)
2513 /* A service that will be restarted must be stopped first to
2514 * trigger BindsTo and/or OnFailure dependencies. If a user
2515 * does not want to wait for the holdoff time to elapse, the
2516 * service should be manually restarted, not started. We
2517 * simply return EAGAIN here, so that any start jobs stay
2518 * queued, and assume that the auto restart timer will
2519 * eventually trigger the restart. */
2520 if (s->state == SERVICE_AUTO_RESTART)
2523 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2525 /* Make sure we don't enter a busy loop of some kind. */
2526 r = service_start_limit_test(s);
2528 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2532 s->result = SERVICE_SUCCESS;
2533 s->reload_result = SERVICE_SUCCESS;
2534 s->main_pid_known = false;
2535 s->main_pid_alien = false;
2536 s->forbid_restart = false;
2538 service_enter_start_pre(s);
2542 static int service_stop(Unit *u) {
2543 Service *s = SERVICE(u);
2547 /* Don't create restart jobs from here. */
2548 s->forbid_restart = true;
2551 if (s->state == SERVICE_STOP ||
2552 s->state == SERVICE_STOP_SIGTERM ||
2553 s->state == SERVICE_STOP_SIGKILL ||
2554 s->state == SERVICE_STOP_POST ||
2555 s->state == SERVICE_FINAL_SIGTERM ||
2556 s->state == SERVICE_FINAL_SIGKILL)
2559 /* A restart will be scheduled or is in progress. */
2560 if (s->state == SERVICE_AUTO_RESTART) {
2561 service_set_state(s, SERVICE_DEAD);
2565 /* If there's already something running we go directly into
2567 if (s->state == SERVICE_START_PRE ||
2568 s->state == SERVICE_START ||
2569 s->state == SERVICE_START_POST ||
2570 s->state == SERVICE_RELOAD) {
2571 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2575 assert(s->state == SERVICE_RUNNING ||
2576 s->state == SERVICE_EXITED);
2578 service_enter_stop(s, SERVICE_SUCCESS);
2582 static int service_reload(Unit *u) {
2583 Service *s = SERVICE(u);
2587 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2589 service_enter_reload(s);
2593 _pure_ static bool service_can_reload(Unit *u) {
2594 Service *s = SERVICE(u);
2598 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2601 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2602 Service *s = SERVICE(u);
2608 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2609 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2610 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2612 if (s->control_pid > 0)
2613 unit_serialize_item_format(u, f, "control-pid", "%lu",
2614 (unsigned long) s->control_pid);
2616 if (s->main_pid_known && s->main_pid > 0)
2617 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2619 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2622 unit_serialize_item(u, f, "status-text", s->status_text);
2624 /* FIXME: There's a minor uncleanliness here: if there are
2625 * multiple commands attached here, we will start from the
2626 * first one again */
2627 if (s->control_command_id >= 0)
2628 unit_serialize_item(u, f, "control-command",
2629 service_exec_command_to_string(s->control_command_id));
2631 if (s->socket_fd >= 0) {
2634 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2637 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2640 if (s->main_exec_status.pid > 0) {
2641 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu",
2642 (unsigned long) s->main_exec_status.pid);
2643 dual_timestamp_serialize(f, "main-exec-status-start",
2644 &s->main_exec_status.start_timestamp);
2645 dual_timestamp_serialize(f, "main-exec-status-exit",
2646 &s->main_exec_status.exit_timestamp);
2648 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2649 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
2650 s->main_exec_status.code);
2651 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
2652 s->main_exec_status.status);
2655 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2656 dual_timestamp_serialize(f, "watchdog-timestamp",
2657 &s->watchdog_timestamp);
2659 if (s->exec_context.tmp_dir)
2660 unit_serialize_item(u, f, "tmp-dir", s->exec_context.tmp_dir);
2662 if (s->exec_context.var_tmp_dir)
2663 unit_serialize_item(u, f, "var-tmp-dir", s->exec_context.var_tmp_dir);
2668 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2669 Service *s = SERVICE(u);
2676 if (streq(key, "state")) {
2679 state = service_state_from_string(value);
2681 log_debug_unit(u->id, "Failed to parse state value %s", value);
2683 s->deserialized_state = state;
2684 } else if (streq(key, "result")) {
2687 f = service_result_from_string(value);
2689 log_debug_unit(u->id, "Failed to parse result value %s", value);
2690 else if (f != SERVICE_SUCCESS)
2693 } else if (streq(key, "reload-result")) {
2696 f = service_result_from_string(value);
2698 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2699 else if (f != SERVICE_SUCCESS)
2700 s->reload_result = f;
2702 } else if (streq(key, "control-pid")) {
2705 if (parse_pid(value, &pid) < 0)
2706 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2708 s->control_pid = pid;
2709 } else if (streq(key, "main-pid")) {
2712 if (parse_pid(value, &pid) < 0)
2713 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2715 service_set_main_pid(s, (pid_t) pid);
2716 } else if (streq(key, "main-pid-known")) {
2719 b = parse_boolean(value);
2721 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2723 s->main_pid_known = b;
2724 } else if (streq(key, "status-text")) {
2731 free(s->status_text);
2735 } else if (streq(key, "control-command")) {
2736 ServiceExecCommand id;
2738 id = service_exec_command_from_string(value);
2740 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2742 s->control_command_id = id;
2743 s->control_command = s->exec_command[id];
2745 } else if (streq(key, "socket-fd")) {
2748 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2749 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2752 if (s->socket_fd >= 0)
2753 close_nointr_nofail(s->socket_fd);
2754 s->socket_fd = fdset_remove(fds, fd);
2756 } else if (streq(key, "main-exec-status-pid")) {
2759 if (parse_pid(value, &pid) < 0)
2760 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2762 s->main_exec_status.pid = pid;
2763 } else if (streq(key, "main-exec-status-code")) {
2766 if (safe_atoi(value, &i) < 0)
2767 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2769 s->main_exec_status.code = i;
2770 } else if (streq(key, "main-exec-status-status")) {
2773 if (safe_atoi(value, &i) < 0)
2774 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2776 s->main_exec_status.status = i;
2777 } else if (streq(key, "main-exec-status-start"))
2778 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2779 else if (streq(key, "main-exec-status-exit"))
2780 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2781 else if (streq(key, "watchdog-timestamp"))
2782 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2783 else if (streq(key, "tmp-dir")) {
2790 s->exec_context.tmp_dir = t;
2791 } else if (streq(key, "var-tmp-dir")) {
2798 s->exec_context.var_tmp_dir = t;
2800 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2805 _pure_ static UnitActiveState service_active_state(Unit *u) {
2806 const UnitActiveState *table;
2810 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2812 return table[SERVICE(u)->state];
2815 static const char *service_sub_state_to_string(Unit *u) {
2818 return service_state_to_string(SERVICE(u)->state);
2821 static bool service_check_gc(Unit *u) {
2822 Service *s = SERVICE(u);
2826 /* Never clean up services that still have a process around,
2827 * even if the service is formally dead. */
2828 if (cgroup_good(s) > 0 ||
2829 main_pid_good(s) > 0 ||
2830 control_pid_good(s) > 0)
2833 #ifdef HAVE_SYSV_COMPAT
2841 _pure_ static bool service_check_snapshot(Unit *u) {
2842 Service *s = SERVICE(u);
2846 return !s->got_socket_fd;
2849 static int service_retry_pid_file(Service *s) {
2852 assert(s->pid_file);
2853 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2855 r = service_load_pid_file(s, false);
2859 service_unwatch_pid_file(s);
2861 service_enter_running(s, SERVICE_SUCCESS);
2865 static int service_watch_pid_file(Service *s) {
2868 log_debug_unit(UNIT(s)->id,
2869 "Setting watch for %s's PID file %s",
2870 UNIT(s)->id, s->pid_file_pathspec->path);
2871 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2875 /* the pidfile might have appeared just before we set the watch */
2876 log_debug_unit(UNIT(s)->id,
2877 "Trying to read %s's PID file %s in case it changed",
2878 UNIT(s)->id, s->pid_file_pathspec->path);
2879 service_retry_pid_file(s);
2883 log_error_unit(UNIT(s)->id,
2884 "Failed to set a watch for %s's PID file %s: %s",
2885 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2886 service_unwatch_pid_file(s);
2890 static int service_demand_pid_file(Service *s) {
2893 assert(s->pid_file);
2894 assert(!s->pid_file_pathspec);
2896 ps = new0(PathSpec, 1);
2900 ps->path = strdup(s->pid_file);
2906 path_kill_slashes(ps->path);
2908 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2909 * keep their PID file open all the time. */
2910 ps->type = PATH_MODIFIED;
2911 ps->inotify_fd = -1;
2913 s->pid_file_pathspec = ps;
2915 return service_watch_pid_file(s);
2918 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2919 Service *s = SERVICE(u);
2923 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2924 assert(s->pid_file_pathspec);
2925 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2927 log_debug_unit(u->id, "inotify event for %s", u->id);
2929 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2932 if (service_retry_pid_file(s) == 0)
2935 if (service_watch_pid_file(s) < 0)
2940 service_unwatch_pid_file(s);
2941 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2944 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2945 Service *s = SERVICE(u);
2951 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2952 is_clean_exit_lsb(code, status, &s->success_status))
2953 f = SERVICE_SUCCESS;
2954 else if (code == CLD_EXITED)
2955 f = SERVICE_FAILURE_EXIT_CODE;
2956 else if (code == CLD_KILLED)
2957 f = SERVICE_FAILURE_SIGNAL;
2958 else if (code == CLD_DUMPED)
2959 f = SERVICE_FAILURE_CORE_DUMP;
2961 assert_not_reached("Unknown code");
2963 if (s->main_pid == pid) {
2964 /* Forking services may occasionally move to a new PID.
2965 * As long as they update the PID file before exiting the old
2966 * PID, they're fine. */
2967 if (service_load_pid_file(s, false) == 0)
2971 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2973 if (s->main_command) {
2974 /* If this is not a forking service than the
2975 * main process got started and hence we copy
2976 * the exit status so that it is recorded both
2977 * as main and as control process exit
2980 s->main_command->exec_status = s->main_exec_status;
2982 if (s->main_command->ignore)
2983 f = SERVICE_SUCCESS;
2984 } else if (s->exec_command[SERVICE_EXEC_START]) {
2986 /* If this is a forked process, then we should
2987 * ignore the return value if this was
2988 * configured for the starter process */
2990 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2991 f = SERVICE_SUCCESS;
2994 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2996 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2997 u->id, sigchld_code_to_string(code), status,
2998 strna(code == CLD_EXITED
2999 ? exit_status_to_string(status, EXIT_STATUS_FULL)
3000 : signal_to_string(status)),
3001 "EXIT_CODE=%s", sigchld_code_to_string(code),
3002 "EXIT_STATUS=%i", status,
3005 if (f != SERVICE_SUCCESS)
3008 if (s->main_command &&
3009 s->main_command->command_next &&
3010 f == SERVICE_SUCCESS) {
3012 /* There is another command to *
3013 * execute, so let's do that. */
3015 log_debug_unit(u->id,
3016 "%s running next main command for state %s",
3017 u->id, service_state_to_string(s->state));
3018 service_run_next_main(s);
3022 /* The service exited, so the service is officially
3024 s->main_command = NULL;
3028 case SERVICE_START_POST:
3029 case SERVICE_RELOAD:
3031 /* Need to wait until the operation is
3036 if (s->type == SERVICE_ONESHOT) {
3037 /* This was our main goal, so let's go on */
3038 if (f == SERVICE_SUCCESS)
3039 service_enter_start_post(s);
3041 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3047 case SERVICE_RUNNING:
3048 service_enter_running(s, f);
3051 case SERVICE_STOP_SIGTERM:
3052 case SERVICE_STOP_SIGKILL:
3054 if (!control_pid_good(s))
3055 service_enter_stop_post(s, f);
3057 /* If there is still a control process, wait for that first */
3061 assert_not_reached("Uh, main process died at wrong time.");
3065 } else if (s->control_pid == pid) {
3068 if (s->control_command) {
3069 exec_status_exit(&s->control_command->exec_status,
3070 &s->exec_context, pid, code, status);
3072 if (s->control_command->ignore)
3073 f = SERVICE_SUCCESS;
3076 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
3077 "%s: control process exited, code=%s status=%i",
3078 u->id, sigchld_code_to_string(code), status);
3080 if (f != SERVICE_SUCCESS)
3083 /* Immediately get rid of the cgroup, so that the
3084 * kernel doesn't delay the cgroup empty messages for
3085 * the service cgroup any longer than necessary */
3086 service_kill_control_processes(s);
3088 if (s->control_command &&
3089 s->control_command->command_next &&
3090 f == SERVICE_SUCCESS) {
3092 /* There is another command to *
3093 * execute, so let's do that. */
3095 log_debug_unit(u->id,
3096 "%s running next control command for state %s",
3097 u->id, service_state_to_string(s->state));
3098 service_run_next_control(s);
3101 /* No further commands for this step, so let's
3102 * figure out what to do next */
3104 s->control_command = NULL;
3105 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3107 log_debug_unit(u->id,
3108 "%s got final SIGCHLD for state %s",
3109 u->id, service_state_to_string(s->state));
3113 case SERVICE_START_PRE:
3114 if (f == SERVICE_SUCCESS)
3115 service_enter_start(s);
3117 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3121 if (s->type != SERVICE_FORKING)
3122 /* Maybe spurious event due to a reload that changed the type? */
3125 if (f != SERVICE_SUCCESS) {
3126 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3131 bool has_start_post;
3134 /* Let's try to load the pid file here if we can.
3135 * The PID file might actually be created by a START_POST
3136 * script. In that case don't worry if the loading fails. */
3138 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3139 r = service_load_pid_file(s, !has_start_post);
3140 if (!has_start_post && r < 0) {
3141 r = service_demand_pid_file(s);
3142 if (r < 0 || !cgroup_good(s))
3143 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3147 service_search_main_pid(s);
3149 service_enter_start_post(s);
3152 case SERVICE_START_POST:
3153 if (f != SERVICE_SUCCESS) {
3154 service_enter_stop(s, f);
3161 r = service_load_pid_file(s, true);
3163 r = service_demand_pid_file(s);
3164 if (r < 0 || !cgroup_good(s))
3165 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3169 service_search_main_pid(s);
3171 service_enter_running(s, SERVICE_SUCCESS);
3174 case SERVICE_RELOAD:
3175 if (f == SERVICE_SUCCESS) {
3176 service_load_pid_file(s, true);
3177 service_search_main_pid(s);
3180 s->reload_result = f;
3181 service_enter_running(s, SERVICE_SUCCESS);
3185 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3188 case SERVICE_STOP_SIGTERM:
3189 case SERVICE_STOP_SIGKILL:
3190 if (main_pid_good(s) <= 0)
3191 service_enter_stop_post(s, f);
3193 /* If there is still a service
3194 * process around, wait until
3195 * that one quit, too */
3198 case SERVICE_STOP_POST:
3199 case SERVICE_FINAL_SIGTERM:
3200 case SERVICE_FINAL_SIGKILL:
3201 service_enter_dead(s, f, true);
3205 assert_not_reached("Uh, control process died at wrong time.");
3210 /* Notify clients about changed exit status */
3211 unit_add_to_dbus_queue(u);
3214 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3215 Service *s = SERVICE(u);
3218 assert(elapsed == 1);
3220 if (w == &s->watchdog_watch) {
3221 service_handle_watchdog(s);
3225 assert(w == &s->timer_watch);
3229 case SERVICE_START_PRE:
3231 log_warning_unit(u->id,
3232 "%s operation timed out. Terminating.", u->id);
3233 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3236 case SERVICE_START_POST:
3237 log_warning_unit(u->id,
3238 "%s operation timed out. Stopping.", u->id);
3239 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3242 case SERVICE_RELOAD:
3243 log_warning_unit(u->id,
3244 "%s operation timed out. Stopping.", u->id);
3245 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3246 service_enter_running(s, SERVICE_SUCCESS);
3250 log_warning_unit(u->id,
3251 "%s stopping timed out. Terminating.", u->id);
3252 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3255 case SERVICE_STOP_SIGTERM:
3256 if (s->kill_context.send_sigkill) {
3257 log_warning_unit(u->id,
3258 "%s stopping timed out. Killing.", u->id);
3259 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3261 log_warning_unit(u->id,
3262 "%s stopping timed out. Skipping SIGKILL.", u->id);
3263 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3268 case SERVICE_STOP_SIGKILL:
3269 /* Uh, we sent a SIGKILL and it is still not gone?
3270 * Must be something we cannot kill, so let's just be
3271 * weirded out and continue */
3273 log_warning_unit(u->id,
3274 "%s still around after SIGKILL. Ignoring.", u->id);
3275 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3278 case SERVICE_STOP_POST:
3279 log_warning_unit(u->id,
3280 "%s stopping timed out (2). Terminating.", u->id);
3281 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3284 case SERVICE_FINAL_SIGTERM:
3285 if (s->kill_context.send_sigkill) {
3286 log_warning_unit(u->id,
3287 "%s stopping timed out (2). Killing.", u->id);
3288 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3290 log_warning_unit(u->id,
3291 "%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.",
3293 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3298 case SERVICE_FINAL_SIGKILL:
3299 log_warning_unit(u->id,
3300 "%s still around after SIGKILL (2). Entering failed mode.", u->id);
3301 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3304 case SERVICE_AUTO_RESTART:
3305 log_info_unit(u->id,
3306 "%s holdoff time over, scheduling restart.", u->id);
3307 service_enter_restart(s);
3311 assert_not_reached("Timeout at wrong time.");
3315 static void service_notify_cgroup_empty_event(Unit *u) {
3316 Service *s = SERVICE(u);
3320 log_debug_unit(u->id,
3321 "%s: cgroup is empty", u->id);
3325 /* Waiting for SIGCHLD is usually more interesting,
3326 * because it includes return codes/signals. Which is
3327 * why we ignore the cgroup events for most cases,
3328 * except when we don't know pid which to expect the
3332 case SERVICE_START_POST:
3333 /* If we were hoping for the daemon to write its PID file,
3334 * we can give up now. */
3335 if (s->pid_file_pathspec) {
3336 log_warning_unit(u->id,
3337 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3338 service_unwatch_pid_file(s);
3339 if (s->state == SERVICE_START)
3340 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3342 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3346 case SERVICE_RUNNING:
3347 /* service_enter_running() will figure out what to do */
3348 service_enter_running(s, SERVICE_SUCCESS);
3351 case SERVICE_STOP_SIGTERM:
3352 case SERVICE_STOP_SIGKILL:
3354 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3355 service_enter_stop_post(s, SERVICE_SUCCESS);
3359 case SERVICE_FINAL_SIGTERM:
3360 case SERVICE_FINAL_SIGKILL:
3361 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3362 service_enter_dead(s, SERVICE_SUCCESS, true);
3371 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3372 Service *s = SERVICE(u);
3377 if (s->notify_access == NOTIFY_NONE) {
3378 log_warning_unit(u->id,
3379 "%s: Got notification message from PID %lu, but reception is disabled.",
3380 u->id, (unsigned long) pid);
3384 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3385 log_warning_unit(u->id,
3386 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3387 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3391 log_debug_unit(u->id,
3392 "%s: Got message", u->id);
3394 /* Interpret MAINPID= */
3395 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3396 (s->state == SERVICE_START ||
3397 s->state == SERVICE_START_POST ||
3398 s->state == SERVICE_RUNNING ||
3399 s->state == SERVICE_RELOAD)) {
3401 if (parse_pid(e + 8, &pid) < 0)
3402 log_warning_unit(u->id,
3403 "Failed to parse notification message %s", e);
3405 log_debug_unit(u->id,
3406 "%s: got %s", u->id, e);
3407 service_set_main_pid(s, pid);
3411 /* Interpret READY= */
3412 if (s->type == SERVICE_NOTIFY &&
3413 s->state == SERVICE_START &&
3414 strv_find(tags, "READY=1")) {
3415 log_debug_unit(u->id,
3416 "%s: got READY=1", u->id);
3418 service_enter_start_post(s);
3421 /* Interpret STATUS= */
3422 e = strv_find_prefix(tags, "STATUS=");
3428 if (!utf8_is_valid(e+7)) {
3429 log_warning_unit(u->id,
3430 "Status message in notification is not UTF-8 clean.");
3436 log_error_unit(u->id,
3437 "Failed to allocate string.");
3441 log_debug_unit(u->id,
3442 "%s: got %s", u->id, e);
3444 free(s->status_text);
3447 free(s->status_text);
3448 s->status_text = NULL;
3452 if (strv_find(tags, "WATCHDOG=1")) {
3453 log_debug_unit(u->id,
3454 "%s: got WATCHDOG=1", u->id);
3455 if (dual_timestamp_is_set(&s->watchdog_timestamp))
3456 service_reset_watchdog(s);
3459 /* Notify clients about changed status or main pid */
3460 unit_add_to_dbus_queue(u);
3463 #ifdef HAVE_SYSV_COMPAT
3465 static int service_enumerate(Manager *m) {
3468 _cleanup_closedir_ DIR *d = NULL;
3469 _cleanup_free_ char *path = NULL, *fpath = NULL, *name = NULL;
3470 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
3471 _cleanup_set_free_ Set *shutdown_services = NULL;
3478 if (m->running_as != SYSTEMD_SYSTEM)
3481 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3482 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3486 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3497 if (errno != ENOENT)
3498 log_warning("opendir(%s) failed: %s", path, strerror(errno));
3503 while ((de = readdir(d))) {
3506 if (ignore_file(de->d_name))
3509 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3512 if (strlen(de->d_name) < 4)
3515 a = undecchar(de->d_name[1]);
3516 b = undecchar(de->d_name[2]);
3522 fpath = strjoin(path, "/", de->d_name, NULL);
3528 if (access(fpath, X_OK) < 0) {
3530 if (errno != ENOENT)
3531 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3537 name = sysv_translate_name(de->d_name + 3);
3543 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3545 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3549 if (de->d_name[0] == 'S') {
3551 if (rcnd_table[i].type == RUNLEVEL_UP) {
3552 SERVICE(service)->sysv_start_priority_from_rcnd =
3553 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3555 SERVICE(service)->sysv_enabled = true;
3558 r = set_ensure_allocated(&runlevel_services[i],
3559 trivial_hash_func, trivial_compare_func);
3563 r = set_put(runlevel_services[i], service);
3567 } else if (de->d_name[0] == 'K' &&
3568 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3570 r = set_ensure_allocated(&shutdown_services,
3571 trivial_hash_func, trivial_compare_func);
3575 r = set_put(shutdown_services, service);
3582 /* Now we loaded all stubs and are aware of the lowest
3583 start-up priority for all services, not let's actually load
3584 the services, this will also tell us which services are
3585 actually native now */
3586 manager_dispatch_load_queue(m);
3588 /* If this is a native service, rely on native ways to pull in
3589 * a service, don't pull it in via sysv rcN.d links. */
3590 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3591 SET_FOREACH(service, runlevel_services[i], j) {
3592 service = unit_follow_merge(service);
3594 if (service->fragment_path)
3597 r = unit_add_two_dependencies_by_name_inverse(
3598 service, UNIT_AFTER, UNIT_WANTS,
3599 rcnd_table[i].target, NULL, true);
3604 /* We honour K links only for halt/reboot. For the normal
3605 * runlevels we assume the stop jobs will be implicitly added
3606 * by the core logic. Also, we don't really distinguish here
3607 * between the runlevels 0 and 6 and just add them to the
3608 * special shutdown target. */
3609 SET_FOREACH(service, shutdown_services, j) {
3610 service = unit_follow_merge(service);
3612 if (service->fragment_path)
3615 r = unit_add_two_dependencies_by_name(
3616 service, UNIT_BEFORE, UNIT_CONFLICTS,
3617 SPECIAL_SHUTDOWN_TARGET, NULL, true);
3626 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3627 set_free(runlevel_services[i]);
3633 static void service_bus_name_owner_change(
3636 const char *old_owner,
3637 const char *new_owner) {
3639 Service *s = SERVICE(u);
3644 assert(streq(s->bus_name, name));
3645 assert(old_owner || new_owner);
3647 if (old_owner && new_owner)
3648 log_debug_unit(u->id,
3649 "%s's D-Bus name %s changed owner from %s to %s",
3650 u->id, name, old_owner, new_owner);
3652 log_debug_unit(u->id,
3653 "%s's D-Bus name %s no longer registered by %s",
3654 u->id, name, old_owner);
3656 log_debug_unit(u->id,
3657 "%s's D-Bus name %s now registered by %s",
3658 u->id, name, new_owner);
3660 s->bus_name_good = !!new_owner;
3662 if (s->type == SERVICE_DBUS) {
3664 /* service_enter_running() will figure out what to
3666 if (s->state == SERVICE_RUNNING)
3667 service_enter_running(s, SERVICE_SUCCESS);
3668 else if (s->state == SERVICE_START && new_owner)
3669 service_enter_start_post(s);
3671 } else if (new_owner &&
3673 (s->state == SERVICE_START ||
3674 s->state == SERVICE_START_POST ||
3675 s->state == SERVICE_RUNNING ||
3676 s->state == SERVICE_RELOAD)) {
3678 /* Try to acquire PID from bus service */
3679 log_debug_unit(u->id,
3680 "Trying to acquire PID from D-Bus name...");
3682 bus_query_pid(u->manager, name);
3686 static void service_bus_query_pid_done(
3691 Service *s = SERVICE(u);
3696 log_debug_unit(u->id,
3697 "%s's D-Bus name %s is now owned by process %u",
3698 u->id, name, (unsigned) pid);
3700 if (s->main_pid <= 0 &&
3701 (s->state == SERVICE_START ||
3702 s->state == SERVICE_START_POST ||
3703 s->state == SERVICE_RUNNING ||
3704 s->state == SERVICE_RELOAD))
3705 service_set_main_pid(s, pid);
3708 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3713 /* This is called by the socket code when instantiating a new
3714 * service for a stream socket and the socket needs to be
3717 if (UNIT(s)->load_state != UNIT_LOADED)
3720 if (s->socket_fd >= 0)
3723 if (s->state != SERVICE_DEAD)
3727 s->got_socket_fd = true;
3729 unit_ref_set(&s->accept_socket, UNIT(sock));
3731 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3734 static void service_reset_failed(Unit *u) {
3735 Service *s = SERVICE(u);
3739 if (s->state == SERVICE_FAILED)
3740 service_set_state(s, SERVICE_DEAD);
3742 s->result = SERVICE_SUCCESS;
3743 s->reload_result = SERVICE_SUCCESS;
3745 RATELIMIT_RESET(s->start_limit);
3748 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3749 Service *s = SERVICE(u);
3750 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
3753 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3754 [SERVICE_DEAD] = "dead",
3755 [SERVICE_START_PRE] = "start-pre",
3756 [SERVICE_START] = "start",
3757 [SERVICE_START_POST] = "start-post",
3758 [SERVICE_RUNNING] = "running",
3759 [SERVICE_EXITED] = "exited",
3760 [SERVICE_RELOAD] = "reload",
3761 [SERVICE_STOP] = "stop",
3762 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3763 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3764 [SERVICE_STOP_POST] = "stop-post",
3765 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3766 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3767 [SERVICE_FAILED] = "failed",
3768 [SERVICE_AUTO_RESTART] = "auto-restart",
3771 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3773 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3774 [SERVICE_RESTART_NO] = "no",
3775 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3776 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3777 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3778 [SERVICE_RESTART_ALWAYS] = "always"
3781 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3783 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3784 [SERVICE_SIMPLE] = "simple",
3785 [SERVICE_FORKING] = "forking",
3786 [SERVICE_ONESHOT] = "oneshot",
3787 [SERVICE_DBUS] = "dbus",
3788 [SERVICE_NOTIFY] = "notify",
3789 [SERVICE_IDLE] = "idle"
3792 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3794 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3795 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3796 [SERVICE_EXEC_START] = "ExecStart",
3797 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3798 [SERVICE_EXEC_RELOAD] = "ExecReload",
3799 [SERVICE_EXEC_STOP] = "ExecStop",
3800 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3803 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3805 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3806 [NOTIFY_NONE] = "none",
3807 [NOTIFY_MAIN] = "main",
3808 [NOTIFY_ALL] = "all"
3811 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3813 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3814 [SERVICE_SUCCESS] = "success",
3815 [SERVICE_FAILURE_RESOURCES] = "resources",
3816 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3817 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3818 [SERVICE_FAILURE_SIGNAL] = "signal",
3819 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3820 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3821 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3824 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3826 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3827 [SERVICE_START_LIMIT_NONE] = "none",
3828 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3829 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3830 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3832 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3834 const UnitVTable service_vtable = {
3835 .object_size = sizeof(Service),
3842 .private_section = "Service",
3843 .exec_context_offset = offsetof(Service, exec_context),
3844 .cgroup_context_offset = offsetof(Service, cgroup_context),
3846 .init = service_init,
3847 .done = service_done,
3848 .load = service_load,
3850 .coldplug = service_coldplug,
3852 .dump = service_dump,
3854 .start = service_start,
3855 .stop = service_stop,
3856 .reload = service_reload,
3858 .can_reload = service_can_reload,
3860 .kill = service_kill,
3862 .serialize = service_serialize,
3863 .deserialize_item = service_deserialize_item,
3865 .active_state = service_active_state,
3866 .sub_state_to_string = service_sub_state_to_string,
3868 .check_gc = service_check_gc,
3869 .check_snapshot = service_check_snapshot,
3871 .sigchld_event = service_sigchld_event,
3872 .timer_event = service_timer_event,
3873 .fd_event = service_fd_event,
3875 .reset_failed = service_reset_failed,
3877 .notify_cgroup_empty = service_notify_cgroup_empty_event,
3878 .notify_message = service_notify_message,
3880 .bus_name_owner_change = service_bus_name_owner_change,
3881 .bus_query_pid_done = service_bus_query_pid_done,
3883 .bus_interface = "org.freedesktop.systemd1.Service",
3884 .bus_message_handler = bus_service_message_handler,
3885 .bus_invalidating_properties = bus_service_invalidating_properties,
3886 .bus_set_property = bus_service_set_property,
3887 .bus_commit_properties = bus_service_commit_properties,
3889 #ifdef HAVE_SYSV_COMPAT
3890 .enumerate = service_enumerate,
3892 .status_message_formats = {
3893 .starting_stopping = {
3894 [0] = "Starting %s...",
3895 [1] = "Stopping %s...",
3897 .finished_start_job = {
3898 [JOB_DONE] = "Started %s.",
3899 [JOB_FAILED] = "Failed to start %s.",
3900 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3901 [JOB_TIMEOUT] = "Timed out starting %s.",
3903 .finished_stop_job = {
3904 [JOB_DONE] = "Stopped %s.",
3905 [JOB_FAILED] = "Stopped (with error) %s.",
3906 [JOB_TIMEOUT] = "Timed out stopping %s.",