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 "bus-errors.h"
40 #include "exit-status.h"
42 #include "path-util.h"
47 #ifdef HAVE_SYSV_COMPAT
49 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
51 typedef enum RunlevelType {
59 const RunlevelType type;
61 /* Standard SysV runlevels for start-up */
62 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
63 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
64 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
65 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
66 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
68 /* Standard SysV runlevels for shutdown */
69 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
70 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
72 /* Note that the order here matters, as we read the
73 directories in this order, and we want to make sure that
74 sysv_start_priority is known when we first load the
75 unit. And that value we only know from S links. Hence
76 UP must be read before DOWN */
79 #define RUNLEVELS_UP "12345"
82 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
83 [SERVICE_DEAD] = UNIT_INACTIVE,
84 [SERVICE_START_PRE] = UNIT_ACTIVATING,
85 [SERVICE_START] = UNIT_ACTIVATING,
86 [SERVICE_START_POST] = UNIT_ACTIVATING,
87 [SERVICE_RUNNING] = UNIT_ACTIVE,
88 [SERVICE_EXITED] = UNIT_ACTIVE,
89 [SERVICE_RELOAD] = UNIT_RELOADING,
90 [SERVICE_STOP] = UNIT_DEACTIVATING,
91 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
92 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
93 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
94 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
95 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
96 [SERVICE_FAILED] = UNIT_FAILED,
97 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
100 /* For Type=idle we never want to delay any other jobs, hence we
101 * consider idle jobs active as soon as we start working on them */
102 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
103 [SERVICE_DEAD] = UNIT_INACTIVE,
104 [SERVICE_START_PRE] = UNIT_ACTIVE,
105 [SERVICE_START] = UNIT_ACTIVE,
106 [SERVICE_START_POST] = UNIT_ACTIVE,
107 [SERVICE_RUNNING] = UNIT_ACTIVE,
108 [SERVICE_EXITED] = UNIT_ACTIVE,
109 [SERVICE_RELOAD] = UNIT_RELOADING,
110 [SERVICE_STOP] = UNIT_DEACTIVATING,
111 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
112 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
113 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
114 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
115 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
116 [SERVICE_FAILED] = UNIT_FAILED,
117 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
120 static void service_init(Unit *u) {
121 Service *s = SERVICE(u);
124 assert(u->load_state == UNIT_STUB);
126 s->timeout_start_usec = DEFAULT_TIMEOUT_USEC;
127 s->timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
128 s->restart_usec = DEFAULT_RESTART_USEC;
129 s->type = _SERVICE_TYPE_INVALID;
131 watch_init(&s->watchdog_watch);
132 watch_init(&s->timer_watch);
134 #ifdef HAVE_SYSV_COMPAT
135 s->sysv_start_priority = -1;
136 s->sysv_start_priority_from_rcnd = -1;
139 s->guess_main_pid = true;
141 exec_context_init(&s->exec_context);
142 kill_context_init(&s->kill_context);
144 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
146 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
149 static void service_unwatch_control_pid(Service *s) {
152 if (s->control_pid <= 0)
155 unit_unwatch_pid(UNIT(s), s->control_pid);
159 static void service_unwatch_main_pid(Service *s) {
162 if (s->main_pid <= 0)
165 unit_unwatch_pid(UNIT(s), s->main_pid);
169 static void service_unwatch_pid_file(Service *s) {
170 if (!s->pid_file_pathspec)
173 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
174 UNIT(s)->id, s->pid_file_pathspec->path);
175 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
176 path_spec_done(s->pid_file_pathspec);
177 free(s->pid_file_pathspec);
178 s->pid_file_pathspec = NULL;
181 static int service_set_main_pid(Service *s, pid_t pid) {
193 s->main_pid_known = true;
195 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
196 log_warning_unit(UNIT(s)->id,
197 "%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
198 UNIT(s)->id, (unsigned long) pid);
200 s->main_pid_alien = true;
202 s->main_pid_alien = false;
204 exec_status_start(&s->main_exec_status, pid);
209 static void service_close_socket_fd(Service *s) {
212 if (s->socket_fd < 0)
215 close_nointr_nofail(s->socket_fd);
219 static void service_connection_unref(Service *s) {
222 if (!UNIT_DEREF(s->accept_socket))
225 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
226 unit_ref_unset(&s->accept_socket);
229 static void service_stop_watchdog(Service *s) {
232 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
233 s->watchdog_timestamp.realtime = 0;
234 s->watchdog_timestamp.monotonic = 0;
237 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart);
239 static void service_handle_watchdog(Service *s) {
245 if (s->watchdog_usec == 0)
248 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
249 if (offset >= s->watchdog_usec) {
250 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
251 service_enter_dead(s, SERVICE_FAILURE_WATCHDOG, true);
255 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->watchdog_usec - offset, &s->watchdog_watch);
257 log_warning_unit(UNIT(s)->id,
258 "%s failed to install watchdog timer: %s",
259 UNIT(s)->id, strerror(-r));
262 static void service_reset_watchdog(Service *s) {
265 dual_timestamp_get(&s->watchdog_timestamp);
266 service_handle_watchdog(s);
269 static void service_done(Unit *u) {
270 Service *s = SERVICE(u);
277 #ifdef HAVE_SYSV_COMPAT
278 free(s->sysv_runlevels);
279 s->sysv_runlevels = NULL;
282 free(s->status_text);
283 s->status_text = NULL;
285 exec_context_done(&s->exec_context);
286 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
287 s->control_command = NULL;
288 s->main_command = NULL;
290 set_free(s->restart_ignore_status.code);
291 s->restart_ignore_status.code = NULL;
292 set_free(s->restart_ignore_status.signal);
293 s->restart_ignore_status.signal = NULL;
295 set_free(s->success_status.code);
296 s->success_status.code = NULL;
297 set_free(s->success_status.signal);
298 s->success_status.signal = NULL;
300 /* This will leak a process, but at least no memory or any of
302 service_unwatch_main_pid(s);
303 service_unwatch_control_pid(s);
304 service_unwatch_pid_file(s);
307 unit_unwatch_bus_name(u, s->bus_name);
312 service_close_socket_fd(s);
313 service_connection_unref(s);
315 unit_ref_unset(&s->accept_socket);
317 service_stop_watchdog(s);
319 unit_unwatch_timer(u, &s->timer_watch);
322 #ifdef HAVE_SYSV_COMPAT
323 static char *sysv_translate_name(const char *name) {
326 r = new(char, strlen(name) + sizeof(".service"));
330 if (endswith(name, ".sh"))
331 /* Drop .sh suffix */
332 strcpy(stpcpy(r, name) - 3, ".service");
333 if (startswith(name, "rc."))
334 /* Drop rc. prefix */
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_add_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 rc.* and *.sh
987 * prefixes/suffixes. */
988 if (startswith(name, "rc.") ||
989 endswith(name, ".sh.service"))
992 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
996 path = strjoin(*p, "/", name, NULL);
1000 assert(endswith(path, ".service"));
1001 path[strlen(path)-8] = 0;
1003 r = service_load_sysv_path(s, path);
1005 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1006 /* Try *.sh source'able init scripts */
1007 strcat(path, ".sh");
1008 r = service_load_sysv_path(s, path);
1012 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1013 /* Try rc.* init scripts */
1015 path = strjoin(*p, "/rc.", name, NULL);
1019 /* Drop .service suffix */
1020 path[strlen(path)-8] = 0;
1021 r = service_load_sysv_path(s, path);
1028 if (UNIT(s)->load_state != UNIT_STUB)
1035 static int service_load_sysv(Service *s) {
1042 /* Load service data from SysV init scripts, preferably with
1043 * LSB headers ... */
1045 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1048 if ((t = UNIT(s)->id))
1049 if ((r = service_load_sysv_name(s, t)) < 0)
1052 if (UNIT(s)->load_state == UNIT_STUB)
1053 SET_FOREACH(t, UNIT(s)->names, i) {
1054 if (t == UNIT(s)->id)
1057 if ((r = service_load_sysv_name(s, t)) < 0)
1060 if (UNIT(s)->load_state != UNIT_STUB)
1068 static int fsck_fix_order(Service *s) {
1074 if (s->fsck_passno <= 0)
1077 /* For each pair of services where both have an fsck priority
1078 * we order things based on it. */
1080 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1089 if (UNIT(t)->load_state != UNIT_LOADED)
1092 if (t->fsck_passno <= 0)
1095 if (t->fsck_passno < s->fsck_passno)
1097 else if (t->fsck_passno > s->fsck_passno)
1102 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1109 static int service_verify(Service *s) {
1112 if (UNIT(s)->load_state != UNIT_LOADED)
1115 if (!s->exec_command[SERVICE_EXEC_START]) {
1116 log_error_unit(UNIT(s)->id,
1117 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1121 if (s->type != SERVICE_ONESHOT &&
1122 s->exec_command[SERVICE_EXEC_START]->command_next) {
1123 log_error_unit(UNIT(s)->id,
1124 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1128 if (s->type == SERVICE_DBUS && !s->bus_name) {
1129 log_error_unit(UNIT(s)->id,
1130 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1134 if (s->bus_name && s->type != SERVICE_DBUS)
1135 log_warning_unit(UNIT(s)->id,
1136 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1138 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1139 log_error_unit(UNIT(s)->id,
1140 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1147 static int service_add_default_dependencies(Service *s) {
1152 /* Add a number of automatic dependencies useful for the
1153 * majority of services. */
1155 /* First, pull in base system */
1156 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1158 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1161 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1163 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1167 /* Second, activate normal shutdown */
1168 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, 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 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1229 if ((r = unit_add_default_cgroups(u)) < 0)
1232 #ifdef HAVE_SYSV_COMPAT
1233 if ((r = sysv_fix_order(s)) < 0)
1237 if ((r = fsck_fix_order(s)) < 0)
1241 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1244 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1245 s->notify_access = NOTIFY_MAIN;
1247 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1248 s->notify_access = NOTIFY_MAIN;
1250 if (s->type == SERVICE_DBUS || s->bus_name)
1251 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1254 if (UNIT(s)->default_dependencies)
1255 if ((r = service_add_default_dependencies(s)) < 0)
1258 r = unit_exec_context_defaults(u, &s->exec_context);
1263 return service_verify(s);
1266 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1268 ServiceExecCommand c;
1269 Service *s = SERVICE(u);
1270 const char *prefix2;
1275 p2 = strappend(prefix, "\t");
1276 prefix2 = p2 ? p2 : prefix;
1279 "%sService State: %s\n"
1281 "%sReload Result: %s\n"
1282 "%sPermissionsStartOnly: %s\n"
1283 "%sRootDirectoryStartOnly: %s\n"
1284 "%sRemainAfterExit: %s\n"
1285 "%sGuessMainPID: %s\n"
1288 "%sNotifyAccess: %s\n",
1289 prefix, service_state_to_string(s->state),
1290 prefix, service_result_to_string(s->result),
1291 prefix, service_result_to_string(s->reload_result),
1292 prefix, yes_no(s->permissions_start_only),
1293 prefix, yes_no(s->root_directory_start_only),
1294 prefix, yes_no(s->remain_after_exit),
1295 prefix, yes_no(s->guess_main_pid),
1296 prefix, service_type_to_string(s->type),
1297 prefix, service_restart_to_string(s->restart),
1298 prefix, notify_access_to_string(s->notify_access));
1300 if (s->control_pid > 0)
1302 "%sControl PID: %lu\n",
1303 prefix, (unsigned long) s->control_pid);
1305 if (s->main_pid > 0)
1308 "%sMain PID Known: %s\n"
1309 "%sMain PID Alien: %s\n",
1310 prefix, (unsigned long) s->main_pid,
1311 prefix, yes_no(s->main_pid_known),
1312 prefix, yes_no(s->main_pid_alien));
1317 prefix, s->pid_file);
1322 "%sBus Name Good: %s\n",
1323 prefix, s->bus_name,
1324 prefix, yes_no(s->bus_name_good));
1326 kill_context_dump(&s->kill_context, f, prefix);
1327 exec_context_dump(&s->exec_context, f, prefix);
1329 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1331 if (!s->exec_command[c])
1334 fprintf(f, "%s-> %s:\n",
1335 prefix, service_exec_command_to_string(c));
1337 exec_command_dump_list(s->exec_command[c], f, prefix2);
1340 #ifdef HAVE_SYSV_COMPAT
1343 "%sSysV Init Script has LSB Header: %s\n"
1344 "%sSysVEnabled: %s\n",
1345 prefix, yes_no(s->sysv_has_lsb),
1346 prefix, yes_no(s->sysv_enabled));
1348 if (s->sysv_start_priority >= 0)
1350 "%sSysVStartPriority: %i\n",
1351 prefix, s->sysv_start_priority);
1353 if (s->sysv_runlevels)
1354 fprintf(f, "%sSysVRunLevels: %s\n",
1355 prefix, s->sysv_runlevels);
1358 if (s->fsck_passno > 0)
1360 "%sFsckPassNo: %i\n",
1361 prefix, s->fsck_passno);
1364 fprintf(f, "%sStatus Text: %s\n",
1365 prefix, s->status_text);
1370 static int service_load_pid_file(Service *s, bool may_warn) {
1380 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1382 log_info_unit(UNIT(s)->id,
1383 "PID file %s not readable (yet?) after %s.",
1384 s->pid_file, service_state_to_string(s->state));
1388 r = parse_pid(k, &pid);
1394 if (kill(pid, 0) < 0 && errno != EPERM) {
1396 log_info_unit(UNIT(s)->id,
1397 "PID %lu read from file %s does not exist.",
1398 (unsigned long) pid, s->pid_file);
1402 if (s->main_pid_known) {
1403 if (pid == s->main_pid)
1406 log_debug_unit(UNIT(s)->id,
1407 "Main PID changing: %lu -> %lu",
1408 (unsigned long) s->main_pid, (unsigned long) pid);
1409 service_unwatch_main_pid(s);
1410 s->main_pid_known = false;
1412 log_debug_unit(UNIT(s)->id,
1413 "Main PID loaded: %lu", (unsigned long) pid);
1415 if ((r = service_set_main_pid(s, pid)) < 0)
1418 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1419 /* FIXME: we need to do something here */
1425 static int service_search_main_pid(Service *s) {
1431 /* If we know it anyway, don't ever fallback to unreliable
1433 if (s->main_pid_known)
1436 if (!s->guess_main_pid)
1439 assert(s->main_pid <= 0);
1441 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1444 log_debug_unit(UNIT(s)->id,
1445 "Main PID guessed: %lu", (unsigned long) pid);
1446 if ((r = service_set_main_pid(s, pid)) < 0)
1449 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1450 /* FIXME: we need to do something here */
1456 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1462 /* Notifies all our sockets when we die */
1464 if (s->socket_fd >= 0)
1467 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1468 if (u->type == UNIT_SOCKET)
1469 socket_notify_service_dead(SOCKET(u), failed_permanent);
1474 static void service_set_state(Service *s, ServiceState state) {
1475 ServiceState old_state;
1476 const UnitActiveState *table;
1479 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1481 old_state = s->state;
1484 service_unwatch_pid_file(s);
1486 if (state != SERVICE_START_PRE &&
1487 state != SERVICE_START &&
1488 state != SERVICE_START_POST &&
1489 state != SERVICE_RELOAD &&
1490 state != SERVICE_STOP &&
1491 state != SERVICE_STOP_SIGTERM &&
1492 state != SERVICE_STOP_SIGKILL &&
1493 state != SERVICE_STOP_POST &&
1494 state != SERVICE_FINAL_SIGTERM &&
1495 state != SERVICE_FINAL_SIGKILL &&
1496 state != SERVICE_AUTO_RESTART)
1497 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1499 if (state != SERVICE_START &&
1500 state != SERVICE_START_POST &&
1501 state != SERVICE_RUNNING &&
1502 state != SERVICE_RELOAD &&
1503 state != SERVICE_STOP &&
1504 state != SERVICE_STOP_SIGTERM &&
1505 state != SERVICE_STOP_SIGKILL) {
1506 service_unwatch_main_pid(s);
1507 s->main_command = NULL;
1510 if (state != SERVICE_START_PRE &&
1511 state != SERVICE_START &&
1512 state != SERVICE_START_POST &&
1513 state != SERVICE_RELOAD &&
1514 state != SERVICE_STOP &&
1515 state != SERVICE_STOP_SIGTERM &&
1516 state != SERVICE_STOP_SIGKILL &&
1517 state != SERVICE_STOP_POST &&
1518 state != SERVICE_FINAL_SIGTERM &&
1519 state != SERVICE_FINAL_SIGKILL) {
1520 service_unwatch_control_pid(s);
1521 s->control_command = NULL;
1522 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1525 if (state == SERVICE_FAILED)
1526 service_notify_sockets_dead(s, s->result == SERVICE_FAILURE_START_LIMIT);
1528 if (state == SERVICE_DEAD ||
1529 state == SERVICE_STOP ||
1530 state == SERVICE_STOP_SIGTERM ||
1531 state == SERVICE_STOP_SIGKILL ||
1532 state == SERVICE_STOP_POST ||
1533 state == SERVICE_FINAL_SIGTERM ||
1534 state == SERVICE_FINAL_SIGKILL ||
1535 state == SERVICE_AUTO_RESTART)
1536 service_notify_sockets_dead(s, false);
1538 if (state != SERVICE_START_PRE &&
1539 state != SERVICE_START &&
1540 state != SERVICE_START_POST &&
1541 state != SERVICE_RUNNING &&
1542 state != SERVICE_RELOAD &&
1543 state != SERVICE_STOP &&
1544 state != SERVICE_STOP_SIGTERM &&
1545 state != SERVICE_STOP_SIGKILL &&
1546 state != SERVICE_STOP_POST &&
1547 state != SERVICE_FINAL_SIGTERM &&
1548 state != SERVICE_FINAL_SIGKILL &&
1549 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1550 service_close_socket_fd(s);
1551 service_connection_unref(s);
1554 if (state == SERVICE_STOP || state == SERVICE_STOP_SIGTERM)
1555 service_stop_watchdog(s);
1557 /* For the inactive states unit_notify() will trim the cgroup,
1558 * but for exit we have to do that ourselves... */
1559 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1560 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1562 if (old_state != state)
1563 log_debug_unit(UNIT(s)->id,
1564 "%s changed %s -> %s", UNIT(s)->id,
1565 service_state_to_string(old_state),
1566 service_state_to_string(state));
1568 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1569 s->reload_result = SERVICE_SUCCESS;
1572 static int service_coldplug(Unit *u) {
1573 Service *s = SERVICE(u);
1577 assert(s->state == SERVICE_DEAD);
1579 if (s->deserialized_state != s->state) {
1581 if (s->deserialized_state == SERVICE_START_PRE ||
1582 s->deserialized_state == SERVICE_START ||
1583 s->deserialized_state == SERVICE_START_POST ||
1584 s->deserialized_state == SERVICE_RELOAD ||
1585 s->deserialized_state == SERVICE_STOP ||
1586 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1587 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1588 s->deserialized_state == SERVICE_STOP_POST ||
1589 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1590 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1591 s->deserialized_state == SERVICE_AUTO_RESTART) {
1592 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1595 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1597 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
1603 if ((s->deserialized_state == SERVICE_START &&
1604 (s->type == SERVICE_FORKING ||
1605 s->type == SERVICE_DBUS ||
1606 s->type == SERVICE_ONESHOT ||
1607 s->type == SERVICE_NOTIFY)) ||
1608 s->deserialized_state == SERVICE_START_POST ||
1609 s->deserialized_state == SERVICE_RUNNING ||
1610 s->deserialized_state == SERVICE_RELOAD ||
1611 s->deserialized_state == SERVICE_STOP ||
1612 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1613 s->deserialized_state == SERVICE_STOP_SIGKILL)
1614 if (s->main_pid > 0)
1615 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1618 if (s->deserialized_state == SERVICE_START_PRE ||
1619 s->deserialized_state == SERVICE_START ||
1620 s->deserialized_state == SERVICE_START_POST ||
1621 s->deserialized_state == SERVICE_RELOAD ||
1622 s->deserialized_state == SERVICE_STOP ||
1623 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1624 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1625 s->deserialized_state == SERVICE_STOP_POST ||
1626 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1627 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1628 if (s->control_pid > 0)
1629 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1632 if (s->deserialized_state == SERVICE_START_POST ||
1633 s->deserialized_state == SERVICE_RUNNING)
1634 service_handle_watchdog(s);
1636 service_set_state(s, s->deserialized_state);
1641 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1645 unsigned rn_fds = 0;
1652 if (s->socket_fd >= 0)
1655 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1660 if (u->type != UNIT_SOCKET)
1665 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1677 if (!(t = new(int, rn_fds+cn_fds))) {
1683 memcpy(t, rfds, rn_fds * sizeof(int));
1684 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1689 rn_fds = rn_fds+cn_fds;
1704 static int service_spawn(
1709 bool apply_permissions,
1711 bool apply_tty_stdin,
1712 bool set_notify_socket,
1718 int *fds = NULL, *fdsbuf = NULL;
1719 unsigned n_fds = 0, n_env = 0;
1720 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1727 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1728 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1729 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1731 if (s->socket_fd >= 0) {
1732 fds = &s->socket_fd;
1735 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1742 if (timeout && s->timeout_start_usec) {
1743 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_start_usec, &s->timer_watch);
1747 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1749 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1754 our_env = new0(char*, 5);
1760 if (set_notify_socket)
1761 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1766 if (s->main_pid > 0)
1767 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1772 if (s->watchdog_usec > 0)
1773 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1778 if (s->meta.manager->running_as != SYSTEMD_SYSTEM)
1779 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1784 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1798 UNIT(s)->manager->confirm_spawn,
1799 UNIT(s)->cgroup_bondings,
1800 UNIT(s)->cgroup_attributes,
1801 is_control ? "control" : NULL,
1803 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1809 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1810 /* FIXME: we need to do something here */
1816 strv_free(final_env);
1826 strv_free(final_env);
1829 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1834 static int main_pid_good(Service *s) {
1837 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1840 /* If we know the pid file, then lets just check if it is
1842 if (s->main_pid_known) {
1844 /* If it's an alien child let's check if it is still
1846 if (s->main_pid_alien)
1847 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1849 /* .. otherwise assume we'll get a SIGCHLD for it,
1850 * which we really should wait for to collect exit
1851 * status and code */
1852 return s->main_pid > 0;
1855 /* We don't know the pid */
1859 static int control_pid_good(Service *s) {
1862 return s->control_pid > 0;
1865 static int cgroup_good(Service *s) {
1870 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1876 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1880 if (f != SERVICE_SUCCESS)
1883 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1885 if (allow_restart &&
1886 !s->forbid_restart &&
1887 (s->restart == SERVICE_RESTART_ALWAYS ||
1888 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1889 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1890 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1891 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1892 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1893 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1894 (s->result != SERVICE_FAILURE_SIGNAL ||
1895 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1898 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
1902 service_set_state(s, SERVICE_AUTO_RESTART);
1905 s->forbid_restart = false;
1910 log_warning_unit(UNIT(s)->id,
1911 "%s failed to run install restart timer: %s",
1912 UNIT(s)->id, strerror(-r));
1913 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1916 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1918 static void service_enter_stop_post(Service *s, ServiceResult f) {
1922 if (f != SERVICE_SUCCESS)
1925 service_unwatch_control_pid(s);
1927 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1928 s->control_command_id = SERVICE_EXEC_STOP_POST;
1930 r = service_spawn(s,
1934 !s->permissions_start_only,
1935 !s->root_directory_start_only,
1944 service_set_state(s, SERVICE_STOP_POST);
1946 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1951 log_warning_unit(UNIT(s)->id,
1952 "%s failed to run 'stop-post' task: %s",
1953 UNIT(s)->id, strerror(-r));
1954 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1957 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1962 if (f != SERVICE_SUCCESS)
1965 r = unit_kill_context(
1968 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1976 if (s->timeout_stop_usec > 0) {
1977 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_stop_usec, &s->timer_watch);
1982 service_set_state(s, state);
1983 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1984 service_enter_stop_post(s, SERVICE_SUCCESS);
1986 service_enter_dead(s, SERVICE_SUCCESS, true);
1991 log_warning_unit(UNIT(s)->id,
1992 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1994 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1995 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1997 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2000 static void service_enter_stop(Service *s, ServiceResult f) {
2005 if (f != SERVICE_SUCCESS)
2008 service_unwatch_control_pid(s);
2010 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2011 s->control_command_id = SERVICE_EXEC_STOP;
2013 r = service_spawn(s,
2017 !s->permissions_start_only,
2018 !s->root_directory_start_only,
2026 service_set_state(s, SERVICE_STOP);
2028 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2033 log_warning_unit(UNIT(s)->id,
2034 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2035 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2038 static void service_enter_running(Service *s, ServiceResult f) {
2039 int main_pid_ok, cgroup_ok;
2042 if (f != SERVICE_SUCCESS)
2045 main_pid_ok = main_pid_good(s);
2046 cgroup_ok = cgroup_good(s);
2048 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2049 (s->bus_name_good || s->type != SERVICE_DBUS))
2050 service_set_state(s, SERVICE_RUNNING);
2051 else if (s->remain_after_exit)
2052 service_set_state(s, SERVICE_EXITED);
2054 service_enter_stop(s, SERVICE_SUCCESS);
2057 static void service_enter_start_post(Service *s) {
2061 service_unwatch_control_pid(s);
2063 if (s->watchdog_usec > 0)
2064 service_reset_watchdog(s);
2066 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2067 s->control_command_id = SERVICE_EXEC_START_POST;
2069 r = service_spawn(s,
2073 !s->permissions_start_only,
2074 !s->root_directory_start_only,
2082 service_set_state(s, SERVICE_START_POST);
2084 service_enter_running(s, SERVICE_SUCCESS);
2089 log_warning_unit(UNIT(s)->id,
2090 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2091 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2094 static void service_enter_start(Service *s) {
2101 assert(s->exec_command[SERVICE_EXEC_START]);
2102 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2104 if (s->type == SERVICE_FORKING)
2105 service_unwatch_control_pid(s);
2107 service_unwatch_main_pid(s);
2109 /* We want to ensure that nobody leaks processes from
2110 * START_PRE here, so let's go on a killing spree, People
2111 * should not spawn long running processes from START_PRE. */
2112 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2114 if (s->type == SERVICE_FORKING) {
2115 s->control_command_id = SERVICE_EXEC_START;
2116 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2118 s->main_command = NULL;
2120 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2121 s->control_command = NULL;
2123 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2126 r = service_spawn(s,
2128 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2133 s->notify_access != NOTIFY_NONE,
2139 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2140 /* For simple services we immediately start
2141 * the START_POST binaries. */
2143 service_set_main_pid(s, pid);
2144 service_enter_start_post(s);
2146 } else if (s->type == SERVICE_FORKING) {
2148 /* For forking services we wait until the start
2149 * process exited. */
2151 s->control_pid = pid;
2152 service_set_state(s, SERVICE_START);
2154 } else if (s->type == SERVICE_ONESHOT ||
2155 s->type == SERVICE_DBUS ||
2156 s->type == SERVICE_NOTIFY) {
2158 /* For oneshot services we wait until the start
2159 * process exited, too, but it is our main process. */
2161 /* For D-Bus services we know the main pid right away,
2162 * but wait for the bus name to appear on the
2163 * bus. Notify services are similar. */
2165 service_set_main_pid(s, pid);
2166 service_set_state(s, SERVICE_START);
2168 assert_not_reached("Unknown service type");
2173 log_warning_unit(UNIT(s)->id,
2174 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2175 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2178 static void service_enter_start_pre(Service *s) {
2183 service_unwatch_control_pid(s);
2185 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2187 /* Before we start anything, let's clear up what might
2188 * be left from previous runs. */
2189 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2191 s->control_command_id = SERVICE_EXEC_START_PRE;
2193 r = service_spawn(s,
2197 !s->permissions_start_only,
2198 !s->root_directory_start_only,
2206 service_set_state(s, SERVICE_START_PRE);
2208 service_enter_start(s);
2213 log_warning_unit(UNIT(s)->id,
2214 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2215 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2218 static void service_enter_restart(Service *s) {
2223 dbus_error_init(&error);
2225 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2226 /* Don't restart things if we are going down anyway */
2227 log_info_unit(UNIT(s)->id,
2228 "Stop job pending for unit, delaying automatic restart.");
2230 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
2237 /* Any units that are bound to this service must also be
2238 * restarted. We use JOB_RESTART (instead of the more obvious
2239 * JOB_START) here so that those dependency jobs will be added
2241 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2245 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2246 * it will be canceled as part of the service_stop() call that
2247 * is executed as part of JOB_RESTART. */
2249 log_debug_unit(UNIT(s)->id,
2250 "%s scheduled restart job.", UNIT(s)->id);
2254 log_warning_unit(UNIT(s)->id,
2255 "%s failed to schedule restart job: %s",
2256 UNIT(s)->id, bus_error(&error, -r));
2257 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2259 dbus_error_free(&error);
2262 static void service_enter_reload(Service *s) {
2267 service_unwatch_control_pid(s);
2269 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2270 s->control_command_id = SERVICE_EXEC_RELOAD;
2272 r = service_spawn(s,
2276 !s->permissions_start_only,
2277 !s->root_directory_start_only,
2285 service_set_state(s, SERVICE_RELOAD);
2287 service_enter_running(s, SERVICE_SUCCESS);
2292 log_warning_unit(UNIT(s)->id,
2293 "%s failed to run 'reload' task: %s",
2294 UNIT(s)->id, strerror(-r));
2295 s->reload_result = SERVICE_FAILURE_RESOURCES;
2296 service_enter_running(s, SERVICE_SUCCESS);
2299 static void service_run_next_control(Service *s) {
2303 assert(s->control_command);
2304 assert(s->control_command->command_next);
2306 assert(s->control_command_id != SERVICE_EXEC_START);
2308 s->control_command = s->control_command->command_next;
2309 service_unwatch_control_pid(s);
2311 r = service_spawn(s,
2315 !s->permissions_start_only,
2316 !s->root_directory_start_only,
2317 s->control_command_id == SERVICE_EXEC_START_PRE ||
2318 s->control_command_id == SERVICE_EXEC_STOP_POST,
2328 log_warning_unit(UNIT(s)->id,
2329 "%s failed to run next control task: %s",
2330 UNIT(s)->id, strerror(-r));
2332 if (s->state == SERVICE_START_PRE)
2333 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2334 else if (s->state == SERVICE_STOP)
2335 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2336 else if (s->state == SERVICE_STOP_POST)
2337 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2338 else if (s->state == SERVICE_RELOAD) {
2339 s->reload_result = SERVICE_FAILURE_RESOURCES;
2340 service_enter_running(s, SERVICE_SUCCESS);
2342 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2345 static void service_run_next_main(Service *s) {
2350 assert(s->main_command);
2351 assert(s->main_command->command_next);
2352 assert(s->type == SERVICE_ONESHOT);
2354 s->main_command = s->main_command->command_next;
2355 service_unwatch_main_pid(s);
2357 r = service_spawn(s,
2364 s->notify_access != NOTIFY_NONE,
2370 service_set_main_pid(s, pid);
2375 log_warning_unit(UNIT(s)->id,
2376 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2377 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2380 static int service_start_limit_test(Service *s) {
2383 if (ratelimit_test(&s->start_limit))
2386 switch (s->start_limit_action) {
2388 case SERVICE_START_LIMIT_NONE:
2389 log_warning_unit(UNIT(s)->id,
2390 "%s start request repeated too quickly, refusing to start.",
2394 case SERVICE_START_LIMIT_REBOOT: {
2398 dbus_error_init(&error);
2400 log_warning_unit(UNIT(s)->id,
2401 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2403 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2405 log_error_unit(UNIT(s)->id,
2406 "Failed to reboot: %s.", bus_error(&error, r));
2407 dbus_error_free(&error);
2413 case SERVICE_START_LIMIT_REBOOT_FORCE:
2414 log_warning_unit(UNIT(s)->id,
2415 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2416 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2419 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2420 log_warning_unit(UNIT(s)->id,
2421 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2423 reboot(RB_AUTOBOOT);
2427 log_error_unit(UNIT(s)->id,
2428 "start limit action=%i", s->start_limit_action);
2429 assert_not_reached("Unknown StartLimitAction.");
2435 static int service_start(Unit *u) {
2436 Service *s = SERVICE(u);
2441 /* We cannot fulfill this request right now, try again later
2443 if (s->state == SERVICE_STOP ||
2444 s->state == SERVICE_STOP_SIGTERM ||
2445 s->state == SERVICE_STOP_SIGKILL ||
2446 s->state == SERVICE_STOP_POST ||
2447 s->state == SERVICE_FINAL_SIGTERM ||
2448 s->state == SERVICE_FINAL_SIGKILL)
2451 /* Already on it! */
2452 if (s->state == SERVICE_START_PRE ||
2453 s->state == SERVICE_START ||
2454 s->state == SERVICE_START_POST)
2457 /* A service that will be restarted must be stopped first to
2458 * trigger BindsTo and/or OnFailure dependencies. If a user
2459 * does not want to wait for the holdoff time to elapse, the
2460 * service should be manually restarted, not started. We
2461 * simply return EAGAIN here, so that any start jobs stay
2462 * queued, and assume that the auto restart timer will
2463 * eventually trigger the restart. */
2464 if (s->state == SERVICE_AUTO_RESTART)
2467 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2469 /* Make sure we don't enter a busy loop of some kind. */
2470 r = service_start_limit_test(s);
2472 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2476 s->result = SERVICE_SUCCESS;
2477 s->reload_result = SERVICE_SUCCESS;
2478 s->main_pid_known = false;
2479 s->main_pid_alien = false;
2480 s->forbid_restart = false;
2482 service_enter_start_pre(s);
2486 static int service_stop(Unit *u) {
2487 Service *s = SERVICE(u);
2491 /* Don't create restart jobs from here. */
2492 s->forbid_restart = true;
2495 if (s->state == SERVICE_STOP ||
2496 s->state == SERVICE_STOP_SIGTERM ||
2497 s->state == SERVICE_STOP_SIGKILL ||
2498 s->state == SERVICE_STOP_POST ||
2499 s->state == SERVICE_FINAL_SIGTERM ||
2500 s->state == SERVICE_FINAL_SIGKILL)
2503 /* A restart will be scheduled or is in progress. */
2504 if (s->state == SERVICE_AUTO_RESTART) {
2505 service_set_state(s, SERVICE_DEAD);
2509 /* If there's already something running we go directly into
2511 if (s->state == SERVICE_START_PRE ||
2512 s->state == SERVICE_START ||
2513 s->state == SERVICE_START_POST ||
2514 s->state == SERVICE_RELOAD) {
2515 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2519 assert(s->state == SERVICE_RUNNING ||
2520 s->state == SERVICE_EXITED);
2522 service_enter_stop(s, SERVICE_SUCCESS);
2526 static int service_reload(Unit *u) {
2527 Service *s = SERVICE(u);
2531 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2533 service_enter_reload(s);
2537 static bool service_can_reload(Unit *u) {
2538 Service *s = SERVICE(u);
2542 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2545 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2546 Service *s = SERVICE(u);
2552 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2553 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2554 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2556 if (s->control_pid > 0)
2557 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2559 if (s->main_pid_known && s->main_pid > 0)
2560 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2562 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2565 unit_serialize_item(u, f, "status-text", s->status_text);
2567 /* FIXME: There's a minor uncleanliness here: if there are
2568 * multiple commands attached here, we will start from the
2569 * first one again */
2570 if (s->control_command_id >= 0)
2571 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2573 if (s->socket_fd >= 0) {
2576 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2579 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2582 if (s->main_exec_status.pid > 0) {
2583 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2584 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2585 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2587 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2588 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2589 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2592 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2593 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2598 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2599 Service *s = SERVICE(u);
2606 if (streq(key, "state")) {
2609 if ((state = service_state_from_string(value)) < 0)
2610 log_debug_unit(u->id, "Failed to parse state value %s", value);
2612 s->deserialized_state = state;
2613 } else if (streq(key, "result")) {
2616 f = service_result_from_string(value);
2618 log_debug_unit(u->id, "Failed to parse result value %s", value);
2619 else if (f != SERVICE_SUCCESS)
2622 } else if (streq(key, "reload-result")) {
2625 f = service_result_from_string(value);
2627 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2628 else if (f != SERVICE_SUCCESS)
2629 s->reload_result = f;
2631 } else if (streq(key, "control-pid")) {
2634 if (parse_pid(value, &pid) < 0)
2635 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2637 s->control_pid = pid;
2638 } else if (streq(key, "main-pid")) {
2641 if (parse_pid(value, &pid) < 0)
2642 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2644 service_set_main_pid(s, (pid_t) pid);
2645 } else if (streq(key, "main-pid-known")) {
2648 if ((b = parse_boolean(value)) < 0)
2649 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2651 s->main_pid_known = b;
2652 } else if (streq(key, "status-text")) {
2655 if ((t = strdup(value))) {
2656 free(s->status_text);
2660 } else if (streq(key, "control-command")) {
2661 ServiceExecCommand id;
2663 if ((id = service_exec_command_from_string(value)) < 0)
2664 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2666 s->control_command_id = id;
2667 s->control_command = s->exec_command[id];
2669 } else if (streq(key, "socket-fd")) {
2672 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2673 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2676 if (s->socket_fd >= 0)
2677 close_nointr_nofail(s->socket_fd);
2678 s->socket_fd = fdset_remove(fds, fd);
2680 } else if (streq(key, "main-exec-status-pid")) {
2683 if (parse_pid(value, &pid) < 0)
2684 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2686 s->main_exec_status.pid = pid;
2687 } else if (streq(key, "main-exec-status-code")) {
2690 if (safe_atoi(value, &i) < 0)
2691 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2693 s->main_exec_status.code = i;
2694 } else if (streq(key, "main-exec-status-status")) {
2697 if (safe_atoi(value, &i) < 0)
2698 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2700 s->main_exec_status.status = i;
2701 } else if (streq(key, "main-exec-status-start"))
2702 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2703 else if (streq(key, "main-exec-status-exit"))
2704 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2705 else if (streq(key, "watchdog-timestamp"))
2706 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2708 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2713 static UnitActiveState service_active_state(Unit *u) {
2714 const UnitActiveState *table;
2718 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2720 return table[SERVICE(u)->state];
2723 static const char *service_sub_state_to_string(Unit *u) {
2726 return service_state_to_string(SERVICE(u)->state);
2729 static bool service_check_gc(Unit *u) {
2730 Service *s = SERVICE(u);
2734 /* Never clean up services that still have a process around,
2735 * even if the service is formally dead. */
2736 if (cgroup_good(s) > 0 ||
2737 main_pid_good(s) > 0 ||
2738 control_pid_good(s) > 0)
2741 #ifdef HAVE_SYSV_COMPAT
2749 static bool service_check_snapshot(Unit *u) {
2750 Service *s = SERVICE(u);
2754 return !s->got_socket_fd;
2757 static int service_retry_pid_file(Service *s) {
2760 assert(s->pid_file);
2761 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2763 r = service_load_pid_file(s, false);
2767 service_unwatch_pid_file(s);
2769 service_enter_running(s, SERVICE_SUCCESS);
2773 static int service_watch_pid_file(Service *s) {
2776 log_debug_unit(UNIT(s)->id,
2777 "Setting watch for %s's PID file %s",
2778 UNIT(s)->id, s->pid_file_pathspec->path);
2779 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2783 /* the pidfile might have appeared just before we set the watch */
2784 service_retry_pid_file(s);
2788 log_error_unit(UNIT(s)->id,
2789 "Failed to set a watch for %s's PID file %s: %s",
2790 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2791 service_unwatch_pid_file(s);
2795 static int service_demand_pid_file(Service *s) {
2798 assert(s->pid_file);
2799 assert(!s->pid_file_pathspec);
2801 ps = new0(PathSpec, 1);
2805 ps->path = strdup(s->pid_file);
2811 path_kill_slashes(ps->path);
2813 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2814 * keep their PID file open all the time. */
2815 ps->type = PATH_MODIFIED;
2816 ps->inotify_fd = -1;
2818 s->pid_file_pathspec = ps;
2820 return service_watch_pid_file(s);
2823 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2824 Service *s = SERVICE(u);
2828 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2829 assert(s->pid_file_pathspec);
2830 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2832 log_debug_unit(u->id, "inotify event for %s", u->id);
2834 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2837 if (service_retry_pid_file(s) == 0)
2840 if (service_watch_pid_file(s) < 0)
2845 service_unwatch_pid_file(s);
2846 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2849 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2850 Service *s = SERVICE(u);
2856 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2857 is_clean_exit_lsb(code, status, &s->success_status))
2858 f = SERVICE_SUCCESS;
2859 else if (code == CLD_EXITED)
2860 f = SERVICE_FAILURE_EXIT_CODE;
2861 else if (code == CLD_KILLED)
2862 f = SERVICE_FAILURE_SIGNAL;
2863 else if (code == CLD_DUMPED)
2864 f = SERVICE_FAILURE_CORE_DUMP;
2866 assert_not_reached("Unknown code");
2868 if (s->main_pid == pid) {
2869 /* Forking services may occasionally move to a new PID.
2870 * As long as they update the PID file before exiting the old
2871 * PID, they're fine. */
2872 if (service_load_pid_file(s, false) == 0)
2876 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2878 if (s->main_command) {
2879 /* If this is not a forking service than the
2880 * main process got started and hence we copy
2881 * the exit status so that it is recorded both
2882 * as main and as control process exit
2885 s->main_command->exec_status = s->main_exec_status;
2887 if (s->main_command->ignore)
2888 f = SERVICE_SUCCESS;
2889 } else if (s->exec_command[SERVICE_EXEC_START]) {
2891 /* If this is a forked process, then we should
2892 * ignore the return value if this was
2893 * configured for the starter process */
2895 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2896 f = SERVICE_SUCCESS;
2899 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2901 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2902 u->id, sigchld_code_to_string(code), status,
2903 strna(code == CLD_EXITED
2904 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2905 : signal_to_string(status)),
2906 "EXIT_CODE=%s", sigchld_code_to_string(code),
2907 "EXIT_STATUS=%i", status,
2910 if (f != SERVICE_SUCCESS)
2913 if (s->main_command &&
2914 s->main_command->command_next &&
2915 f == SERVICE_SUCCESS) {
2917 /* There is another command to *
2918 * execute, so let's do that. */
2920 log_debug_unit(u->id,
2921 "%s running next main command for state %s",
2922 u->id, service_state_to_string(s->state));
2923 service_run_next_main(s);
2927 /* The service exited, so the service is officially
2929 s->main_command = NULL;
2933 case SERVICE_START_POST:
2934 case SERVICE_RELOAD:
2936 /* Need to wait until the operation is
2941 if (s->type == SERVICE_ONESHOT) {
2942 /* This was our main goal, so let's go on */
2943 if (f == SERVICE_SUCCESS)
2944 service_enter_start_post(s);
2946 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2952 case SERVICE_RUNNING:
2953 service_enter_running(s, f);
2956 case SERVICE_STOP_SIGTERM:
2957 case SERVICE_STOP_SIGKILL:
2959 if (!control_pid_good(s))
2960 service_enter_stop_post(s, f);
2962 /* If there is still a control process, wait for that first */
2966 assert_not_reached("Uh, main process died at wrong time.");
2970 } else if (s->control_pid == pid) {
2974 if (s->control_command) {
2975 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2977 if (s->control_command->ignore)
2978 f = SERVICE_SUCCESS;
2981 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
2982 "%s: control process exited, code=%s status=%i",
2983 u->id, sigchld_code_to_string(code), status);
2985 if (f != SERVICE_SUCCESS)
2988 /* Immediately get rid of the cgroup, so that the
2989 * kernel doesn't delay the cgroup empty messages for
2990 * the service cgroup any longer than necessary */
2991 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2993 if (s->control_command &&
2994 s->control_command->command_next &&
2995 f == SERVICE_SUCCESS) {
2997 /* There is another command to *
2998 * execute, so let's do that. */
3000 log_debug_unit(u->id,
3001 "%s running next control command for state %s",
3002 u->id, service_state_to_string(s->state));
3003 service_run_next_control(s);
3006 /* No further commands for this step, so let's
3007 * figure out what to do next */
3009 s->control_command = NULL;
3010 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3012 log_debug_unit(u->id,
3013 "%s got final SIGCHLD for state %s",
3014 u->id, service_state_to_string(s->state));
3018 case SERVICE_START_PRE:
3019 if (f == SERVICE_SUCCESS)
3020 service_enter_start(s);
3022 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3026 if (s->type != SERVICE_FORKING)
3027 /* Maybe spurious event due to a reload that changed the type? */
3030 if (f != SERVICE_SUCCESS) {
3031 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3036 bool has_start_post;
3039 /* Let's try to load the pid file here if we can.
3040 * The PID file might actually be created by a START_POST
3041 * script. In that case don't worry if the loading fails. */
3043 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3044 r = service_load_pid_file(s, !has_start_post);
3045 if (!has_start_post && r < 0) {
3046 r = service_demand_pid_file(s);
3047 if (r < 0 || !cgroup_good(s))
3048 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3052 service_search_main_pid(s);
3054 service_enter_start_post(s);
3057 case SERVICE_START_POST:
3058 if (f != SERVICE_SUCCESS) {
3059 service_enter_stop(s, f);
3066 r = service_load_pid_file(s, true);
3068 r = service_demand_pid_file(s);
3069 if (r < 0 || !cgroup_good(s))
3070 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3074 service_search_main_pid(s);
3076 service_enter_running(s, SERVICE_SUCCESS);
3079 case SERVICE_RELOAD:
3080 if (f == SERVICE_SUCCESS) {
3081 service_load_pid_file(s, true);
3082 service_search_main_pid(s);
3085 s->reload_result = f;
3086 service_enter_running(s, SERVICE_SUCCESS);
3090 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3093 case SERVICE_STOP_SIGTERM:
3094 case SERVICE_STOP_SIGKILL:
3095 if (main_pid_good(s) <= 0)
3096 service_enter_stop_post(s, f);
3098 /* If there is still a service
3099 * process around, wait until
3100 * that one quit, too */
3103 case SERVICE_STOP_POST:
3104 case SERVICE_FINAL_SIGTERM:
3105 case SERVICE_FINAL_SIGKILL:
3106 service_enter_dead(s, f, true);
3110 assert_not_reached("Uh, control process died at wrong time.");
3115 /* Notify clients about changed exit status */
3116 unit_add_to_dbus_queue(u);
3119 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3120 Service *s = SERVICE(u);
3123 assert(elapsed == 1);
3125 if (w == &s->watchdog_watch) {
3126 service_handle_watchdog(s);
3130 assert(w == &s->timer_watch);
3134 case SERVICE_START_PRE:
3136 log_warning_unit(u->id,
3137 "%s operation timed out. Terminating.", u->id);
3138 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3141 case SERVICE_START_POST:
3142 log_warning_unit(u->id,
3143 "%s operation timed out. Stopping.", u->id);
3144 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3147 case SERVICE_RELOAD:
3148 log_warning_unit(u->id,
3149 "%s operation timed out. Stopping.", u->id);
3150 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3151 service_enter_running(s, SERVICE_SUCCESS);
3155 log_warning_unit(u->id,
3156 "%s stopping timed out. Terminating.", u->id);
3157 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3160 case SERVICE_STOP_SIGTERM:
3161 if (s->kill_context.send_sigkill) {
3162 log_warning_unit(u->id,
3163 "%s stopping timed out. Killing.", u->id);
3164 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3166 log_warning_unit(u->id,
3167 "%s stopping timed out. Skipping SIGKILL.", u->id);
3168 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3173 case SERVICE_STOP_SIGKILL:
3174 /* Uh, we sent a SIGKILL and it is still not gone?
3175 * Must be something we cannot kill, so let's just be
3176 * weirded out and continue */
3178 log_warning_unit(u->id,
3179 "%s still around after SIGKILL. Ignoring.", u->id);
3180 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3183 case SERVICE_STOP_POST:
3184 log_warning_unit(u->id,
3185 "%s stopping timed out (2). Terminating.", u->id);
3186 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3189 case SERVICE_FINAL_SIGTERM:
3190 if (s->kill_context.send_sigkill) {
3191 log_warning_unit(u->id,
3192 "%s stopping timed out (2). Killing.", u->id);
3193 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3195 log_warning_unit(u->id,
3196 "%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.",
3198 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3203 case SERVICE_FINAL_SIGKILL:
3204 log_warning_unit(u->id,
3205 "%s still around after SIGKILL (2). Entering failed mode.", u->id);
3206 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3209 case SERVICE_AUTO_RESTART:
3210 log_info_unit(u->id,
3211 "%s holdoff time over, scheduling restart.", u->id);
3212 service_enter_restart(s);
3216 assert_not_reached("Timeout at wrong time.");
3220 static void service_cgroup_notify_event(Unit *u) {
3221 Service *s = SERVICE(u);
3225 log_debug_unit(u->id,
3226 "%s: cgroup is empty", u->id);
3230 /* Waiting for SIGCHLD is usually more interesting,
3231 * because it includes return codes/signals. Which is
3232 * why we ignore the cgroup events for most cases,
3233 * except when we don't know pid which to expect the
3237 case SERVICE_START_POST:
3238 /* If we were hoping for the daemon to write its PID file,
3239 * we can give up now. */
3240 if (s->pid_file_pathspec) {
3241 log_warning_unit(u->id,
3242 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3243 service_unwatch_pid_file(s);
3244 if (s->state == SERVICE_START)
3245 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3247 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3251 case SERVICE_RUNNING:
3252 /* service_enter_running() will figure out what to do */
3253 service_enter_running(s, SERVICE_SUCCESS);
3256 case SERVICE_STOP_SIGTERM:
3257 case SERVICE_STOP_SIGKILL:
3259 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3260 service_enter_stop_post(s, SERVICE_SUCCESS);
3264 case SERVICE_FINAL_SIGTERM:
3265 case SERVICE_FINAL_SIGKILL:
3266 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3267 service_enter_dead(s, SERVICE_SUCCESS, true);
3276 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3277 Service *s = SERVICE(u);
3282 if (s->notify_access == NOTIFY_NONE) {
3283 log_warning_unit(u->id,
3284 "%s: Got notification message from PID %lu, but reception is disabled.",
3285 u->id, (unsigned long) pid);
3289 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3290 log_warning_unit(u->id,
3291 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3292 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3296 log_debug_unit(u->id,
3297 "%s: Got message", u->id);
3299 /* Interpret MAINPID= */
3300 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3301 (s->state == SERVICE_START ||
3302 s->state == SERVICE_START_POST ||
3303 s->state == SERVICE_RUNNING ||
3304 s->state == SERVICE_RELOAD)) {
3306 if (parse_pid(e + 8, &pid) < 0)
3307 log_warning_unit(u->id,
3308 "Failed to parse notification message %s", e);
3310 log_debug_unit(u->id,
3311 "%s: got %s", u->id, e);
3312 service_set_main_pid(s, pid);
3316 /* Interpret READY= */
3317 if (s->type == SERVICE_NOTIFY &&
3318 s->state == SERVICE_START &&
3319 strv_find(tags, "READY=1")) {
3320 log_debug_unit(u->id,
3321 "%s: got READY=1", u->id);
3323 service_enter_start_post(s);
3326 /* Interpret STATUS= */
3327 e = strv_find_prefix(tags, "STATUS=");
3333 if (!utf8_is_valid(e+7)) {
3334 log_warning_unit(u->id,
3335 "Status message in notification is not UTF-8 clean.");
3341 log_error_unit(u->id,
3342 "Failed to allocate string.");
3346 log_debug_unit(u->id,
3347 "%s: got %s", u->id, e);
3349 free(s->status_text);
3352 free(s->status_text);
3353 s->status_text = NULL;
3357 if (strv_find(tags, "WATCHDOG=1")) {
3358 log_debug_unit(u->id,
3359 "%s: got WATCHDOG=1", u->id);
3360 if (dual_timestamp_is_set(&s->watchdog_timestamp))
3361 service_reset_watchdog(s);
3364 /* Notify clients about changed status or main pid */
3365 unit_add_to_dbus_queue(u);
3368 #ifdef HAVE_SYSV_COMPAT
3370 static int service_enumerate(Manager *m) {
3374 char *path = NULL, *fpath = NULL, *name = NULL;
3375 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3382 if (m->running_as != SYSTEMD_SYSTEM)
3385 zero(runlevel_services);
3387 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3388 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3392 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3401 if (!(d = opendir(path))) {
3402 if (errno != ENOENT)
3403 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3408 while ((de = readdir(d))) {
3411 if (ignore_file(de->d_name))
3414 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3417 if (strlen(de->d_name) < 4)
3420 a = undecchar(de->d_name[1]);
3421 b = undecchar(de->d_name[2]);
3427 fpath = strjoin(path, "/", de->d_name, NULL);
3433 if (access(fpath, X_OK) < 0) {
3435 if (errno != ENOENT)
3436 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3442 if (!(name = sysv_translate_name(de->d_name + 3))) {
3447 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3449 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3453 if (de->d_name[0] == 'S') {
3455 if (rcnd_table[i].type == RUNLEVEL_UP) {
3456 SERVICE(service)->sysv_start_priority_from_rcnd =
3457 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3459 SERVICE(service)->sysv_enabled = true;
3462 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3465 if ((r = set_put(runlevel_services[i], service)) < 0)
3468 } else if (de->d_name[0] == 'K' &&
3469 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3471 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3474 if ((r = set_put(shutdown_services, service)) < 0)
3480 /* Now we loaded all stubs and are aware of the lowest
3481 start-up priority for all services, not let's actually load
3482 the services, this will also tell us which services are
3483 actually native now */
3484 manager_dispatch_load_queue(m);
3486 /* If this is a native service, rely on native ways to pull in
3487 * a service, don't pull it in via sysv rcN.d links. */
3488 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3489 SET_FOREACH(service, runlevel_services[i], j) {
3490 service = unit_follow_merge(service);
3492 if (service->fragment_path)
3495 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3499 /* We honour K links only for halt/reboot. For the normal
3500 * runlevels we assume the stop jobs will be implicitly added
3501 * by the core logic. Also, we don't really distinguish here
3502 * between the runlevels 0 and 6 and just add them to the
3503 * special shutdown target. */
3504 SET_FOREACH(service, shutdown_services, j) {
3505 service = unit_follow_merge(service);
3507 if (service->fragment_path)
3510 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3521 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3522 set_free(runlevel_services[i]);
3523 set_free(shutdown_services);
3532 static void service_bus_name_owner_change(
3535 const char *old_owner,
3536 const char *new_owner) {
3538 Service *s = SERVICE(u);
3543 assert(streq(s->bus_name, name));
3544 assert(old_owner || new_owner);
3546 if (old_owner && new_owner)
3547 log_debug_unit(u->id,
3548 "%s's D-Bus name %s changed owner from %s to %s",
3549 u->id, name, old_owner, new_owner);
3551 log_debug_unit(u->id,
3552 "%s's D-Bus name %s no longer registered by %s",
3553 u->id, name, old_owner);
3555 log_debug_unit(u->id,
3556 "%s's D-Bus name %s now registered by %s",
3557 u->id, name, new_owner);
3559 s->bus_name_good = !!new_owner;
3561 if (s->type == SERVICE_DBUS) {
3563 /* service_enter_running() will figure out what to
3565 if (s->state == SERVICE_RUNNING)
3566 service_enter_running(s, SERVICE_SUCCESS);
3567 else if (s->state == SERVICE_START && new_owner)
3568 service_enter_start_post(s);
3570 } else if (new_owner &&
3572 (s->state == SERVICE_START ||
3573 s->state == SERVICE_START_POST ||
3574 s->state == SERVICE_RUNNING ||
3575 s->state == SERVICE_RELOAD)) {
3577 /* Try to acquire PID from bus service */
3578 log_debug_unit(u->id,
3579 "Trying to acquire PID from D-Bus name...");
3581 bus_query_pid(u->manager, name);
3585 static void service_bus_query_pid_done(
3590 Service *s = SERVICE(u);
3595 log_debug_unit(u->id,
3596 "%s's D-Bus name %s is now owned by process %u",
3597 u->id, name, (unsigned) pid);
3599 if (s->main_pid <= 0 &&
3600 (s->state == SERVICE_START ||
3601 s->state == SERVICE_START_POST ||
3602 s->state == SERVICE_RUNNING ||
3603 s->state == SERVICE_RELOAD))
3604 service_set_main_pid(s, pid);
3607 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3612 /* This is called by the socket code when instantiating a new
3613 * service for a stream socket and the socket needs to be
3616 if (UNIT(s)->load_state != UNIT_LOADED)
3619 if (s->socket_fd >= 0)
3622 if (s->state != SERVICE_DEAD)
3626 s->got_socket_fd = true;
3628 unit_ref_set(&s->accept_socket, UNIT(sock));
3630 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3633 static void service_reset_failed(Unit *u) {
3634 Service *s = SERVICE(u);
3638 if (s->state == SERVICE_FAILED)
3639 service_set_state(s, SERVICE_DEAD);
3641 s->result = SERVICE_SUCCESS;
3642 s->reload_result = SERVICE_SUCCESS;
3644 RATELIMIT_RESET(s->start_limit);
3647 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3648 Service *s = SERVICE(u);
3650 Set *pid_set = NULL;
3654 if (s->main_pid <= 0 && who == KILL_MAIN) {
3655 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3659 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3660 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3664 if (who == KILL_CONTROL || who == KILL_ALL)
3665 if (s->control_pid > 0)
3666 if (kill(s->control_pid, signo) < 0)
3669 if (who == KILL_MAIN || who == KILL_ALL)
3670 if (s->main_pid > 0)
3671 if (kill(s->main_pid, signo) < 0)
3674 if (who == KILL_ALL) {
3677 pid_set = set_new(trivial_hash_func, trivial_compare_func);
3681 /* Exclude the control/main pid from being killed via the cgroup */
3682 if (s->control_pid > 0) {
3683 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
3690 if (s->main_pid > 0) {
3691 q = set_put(pid_set, LONG_TO_PTR(s->main_pid));
3698 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3699 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3710 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3711 [SERVICE_DEAD] = "dead",
3712 [SERVICE_START_PRE] = "start-pre",
3713 [SERVICE_START] = "start",
3714 [SERVICE_START_POST] = "start-post",
3715 [SERVICE_RUNNING] = "running",
3716 [SERVICE_EXITED] = "exited",
3717 [SERVICE_RELOAD] = "reload",
3718 [SERVICE_STOP] = "stop",
3719 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3720 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3721 [SERVICE_STOP_POST] = "stop-post",
3722 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3723 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3724 [SERVICE_FAILED] = "failed",
3725 [SERVICE_AUTO_RESTART] = "auto-restart",
3728 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3730 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3731 [SERVICE_RESTART_NO] = "no",
3732 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3733 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3734 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3735 [SERVICE_RESTART_ALWAYS] = "always"
3738 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3740 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3741 [SERVICE_SIMPLE] = "simple",
3742 [SERVICE_FORKING] = "forking",
3743 [SERVICE_ONESHOT] = "oneshot",
3744 [SERVICE_DBUS] = "dbus",
3745 [SERVICE_NOTIFY] = "notify",
3746 [SERVICE_IDLE] = "idle"
3749 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3751 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3752 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3753 [SERVICE_EXEC_START] = "ExecStart",
3754 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3755 [SERVICE_EXEC_RELOAD] = "ExecReload",
3756 [SERVICE_EXEC_STOP] = "ExecStop",
3757 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3760 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3762 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3763 [NOTIFY_NONE] = "none",
3764 [NOTIFY_MAIN] = "main",
3765 [NOTIFY_ALL] = "all"
3768 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3770 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3771 [SERVICE_SUCCESS] = "success",
3772 [SERVICE_FAILURE_RESOURCES] = "resources",
3773 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3774 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3775 [SERVICE_FAILURE_SIGNAL] = "signal",
3776 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3777 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3778 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3781 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3783 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3784 [SERVICE_START_LIMIT_NONE] = "none",
3785 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3786 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3787 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3789 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3791 const UnitVTable service_vtable = {
3792 .object_size = sizeof(Service),
3799 .exec_context_offset = offsetof(Service, exec_context),
3800 .exec_section = "Service",
3802 .init = service_init,
3803 .done = service_done,
3804 .load = service_load,
3806 .coldplug = service_coldplug,
3808 .dump = service_dump,
3810 .start = service_start,
3811 .stop = service_stop,
3812 .reload = service_reload,
3814 .can_reload = service_can_reload,
3816 .kill = service_kill,
3818 .serialize = service_serialize,
3819 .deserialize_item = service_deserialize_item,
3821 .active_state = service_active_state,
3822 .sub_state_to_string = service_sub_state_to_string,
3824 .check_gc = service_check_gc,
3825 .check_snapshot = service_check_snapshot,
3827 .sigchld_event = service_sigchld_event,
3828 .timer_event = service_timer_event,
3829 .fd_event = service_fd_event,
3831 .reset_failed = service_reset_failed,
3833 .cgroup_notify_empty = service_cgroup_notify_event,
3834 .notify_message = service_notify_message,
3836 .bus_name_owner_change = service_bus_name_owner_change,
3837 .bus_query_pid_done = service_bus_query_pid_done,
3839 .bus_interface = "org.freedesktop.systemd1.Service",
3840 .bus_message_handler = bus_service_message_handler,
3841 .bus_invalidating_properties = bus_service_invalidating_properties,
3843 #ifdef HAVE_SYSV_COMPAT
3844 .enumerate = service_enumerate,
3846 .status_message_formats = {
3847 .starting_stopping = {
3848 [0] = "Starting %s...",
3849 [1] = "Stopping %s...",
3851 .finished_start_job = {
3852 [JOB_DONE] = "Started %s.",
3853 [JOB_FAILED] = "Failed to start %s.",
3854 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3855 [JOB_TIMEOUT] = "Timed out starting %s.",
3857 .finished_stop_job = {
3858 [JOB_DONE] = "Stopped %s.",
3859 [JOB_FAILED] = "Stopped (with error) %s.",
3860 [JOB_TIMEOUT] = "Timed out stopping %s.",