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"
46 #ifdef HAVE_SYSV_COMPAT
48 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
50 typedef enum RunlevelType {
58 const RunlevelType type;
60 /* Standard SysV runlevels for start-up */
61 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
62 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
63 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
64 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
65 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
67 /* Standard SysV runlevels for shutdown */
68 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
69 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
71 /* Note that the order here matters, as we read the
72 directories in this order, and we want to make sure that
73 sysv_start_priority is known when we first load the
74 unit. And that value we only know from S links. Hence
75 UP must be read before DOWN */
78 #define RUNLEVELS_UP "12345"
81 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
82 [SERVICE_DEAD] = UNIT_INACTIVE,
83 [SERVICE_START_PRE] = UNIT_ACTIVATING,
84 [SERVICE_START] = UNIT_ACTIVATING,
85 [SERVICE_START_POST] = UNIT_ACTIVATING,
86 [SERVICE_RUNNING] = UNIT_ACTIVE,
87 [SERVICE_EXITED] = UNIT_ACTIVE,
88 [SERVICE_RELOAD] = UNIT_RELOADING,
89 [SERVICE_STOP] = UNIT_DEACTIVATING,
90 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
91 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
92 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
93 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
94 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
95 [SERVICE_FAILED] = UNIT_FAILED,
96 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
99 /* For Type=idle we never want to delay any other jobs, hence we
100 * consider idle jobs active as soon as we start working on them */
101 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
102 [SERVICE_DEAD] = UNIT_INACTIVE,
103 [SERVICE_START_PRE] = UNIT_ACTIVE,
104 [SERVICE_START] = UNIT_ACTIVE,
105 [SERVICE_START_POST] = UNIT_ACTIVE,
106 [SERVICE_RUNNING] = UNIT_ACTIVE,
107 [SERVICE_EXITED] = UNIT_ACTIVE,
108 [SERVICE_RELOAD] = UNIT_RELOADING,
109 [SERVICE_STOP] = UNIT_DEACTIVATING,
110 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
111 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
112 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
113 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
114 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
115 [SERVICE_FAILED] = UNIT_FAILED,
116 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
119 static void service_init(Unit *u) {
120 Service *s = SERVICE(u);
123 assert(u->load_state == UNIT_STUB);
125 s->timeout_start_usec = DEFAULT_TIMEOUT_USEC;
126 s->timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
127 s->restart_usec = DEFAULT_RESTART_USEC;
128 s->type = _SERVICE_TYPE_INVALID;
130 watch_init(&s->watchdog_watch);
131 watch_init(&s->timer_watch);
133 #ifdef HAVE_SYSV_COMPAT
134 s->sysv_start_priority = -1;
135 s->sysv_start_priority_from_rcnd = -1;
138 s->guess_main_pid = true;
140 exec_context_init(&s->exec_context);
141 kill_context_init(&s->kill_context);
143 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
145 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
148 static void service_unwatch_control_pid(Service *s) {
151 if (s->control_pid <= 0)
154 unit_unwatch_pid(UNIT(s), s->control_pid);
158 static void service_unwatch_main_pid(Service *s) {
161 if (s->main_pid <= 0)
164 unit_unwatch_pid(UNIT(s), s->main_pid);
168 static void service_unwatch_pid_file(Service *s) {
169 if (!s->pid_file_pathspec)
172 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
173 UNIT(s)->id, s->pid_file_pathspec->path);
174 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
175 path_spec_done(s->pid_file_pathspec);
176 free(s->pid_file_pathspec);
177 s->pid_file_pathspec = NULL;
180 static int service_set_main_pid(Service *s, pid_t pid) {
192 s->main_pid_known = true;
194 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
195 log_warning_unit(UNIT(s)->id,
196 "%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
197 UNIT(s)->id, (unsigned long) pid);
199 s->main_pid_alien = true;
201 s->main_pid_alien = false;
203 exec_status_start(&s->main_exec_status, pid);
208 static void service_close_socket_fd(Service *s) {
211 if (s->socket_fd < 0)
214 close_nointr_nofail(s->socket_fd);
218 static void service_connection_unref(Service *s) {
221 if (!UNIT_DEREF(s->accept_socket))
224 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
225 unit_ref_unset(&s->accept_socket);
228 static void service_stop_watchdog(Service *s) {
231 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
232 s->watchdog_timestamp.realtime = 0;
233 s->watchdog_timestamp.monotonic = 0;
236 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart);
238 static void service_handle_watchdog(Service *s) {
244 if (s->watchdog_usec == 0)
247 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
248 if (offset >= s->watchdog_usec) {
249 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
250 service_enter_dead(s, SERVICE_FAILURE_WATCHDOG, true);
254 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->watchdog_usec - offset, &s->watchdog_watch);
256 log_warning_unit(UNIT(s)->id,
257 "%s failed to install watchdog timer: %s",
258 UNIT(s)->id, strerror(-r));
261 static void service_reset_watchdog(Service *s) {
264 dual_timestamp_get(&s->watchdog_timestamp);
265 service_handle_watchdog(s);
268 static void service_done(Unit *u) {
269 Service *s = SERVICE(u);
276 #ifdef HAVE_SYSV_COMPAT
277 free(s->sysv_runlevels);
278 s->sysv_runlevels = NULL;
281 free(s->status_text);
282 s->status_text = NULL;
284 exec_context_done(&s->exec_context);
285 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
286 s->control_command = NULL;
287 s->main_command = NULL;
289 set_free(s->restart_ignore_status.code);
290 s->restart_ignore_status.code = NULL;
291 set_free(s->restart_ignore_status.signal);
292 s->restart_ignore_status.signal = NULL;
294 set_free(s->success_status.code);
295 s->success_status.code = NULL;
296 set_free(s->success_status.signal);
297 s->success_status.signal = NULL;
299 /* This will leak a process, but at least no memory or any of
301 service_unwatch_main_pid(s);
302 service_unwatch_control_pid(s);
303 service_unwatch_pid_file(s);
306 unit_unwatch_bus_name(u, s->bus_name);
311 service_close_socket_fd(s);
312 service_connection_unref(s);
314 unit_ref_unset(&s->accept_socket);
316 service_stop_watchdog(s);
318 unit_unwatch_timer(u, &s->timer_watch);
321 #ifdef HAVE_SYSV_COMPAT
322 static char *sysv_translate_name(const char *name) {
325 r = new(char, strlen(name) + sizeof(".service"));
329 if (endswith(name, ".sh"))
330 /* Drop .sh suffix */
331 strcpy(stpcpy(r, name) - 3, ".service");
332 if (startswith(name, "rc."))
333 /* Drop rc. prefix */
334 strcpy(stpcpy(r, name + 3), ".service");
336 /* Normal init script name */
337 strcpy(stpcpy(r, name), ".service");
342 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
344 /* We silently ignore the $ prefix here. According to the LSB
345 * spec it simply indicates whether something is a
346 * standardized name or a distribution-specific one. Since we
347 * just follow what already exists and do not introduce new
348 * uses or names we don't care who introduced a new name. */
350 static const char * const table[] = {
351 /* LSB defined facilities */
353 "network", SPECIAL_NETWORK_TARGET,
354 "named", SPECIAL_NSS_LOOKUP_TARGET,
355 "portmap", SPECIAL_RPCBIND_TARGET,
356 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
358 "time", SPECIAL_TIME_SYNC_TARGET,
368 n = *name == '$' ? name + 1 : name;
370 for (i = 0; i < ELEMENTSOF(table); i += 2) {
372 if (!streq(table[i], n))
378 r = strdup(table[i+1]);
385 /* If we don't know this name, fallback heuristics to figure
386 * out whether something is a target or a service alias. */
389 if (!unit_prefix_is_valid(n))
392 /* Facilities starting with $ are most likely targets */
393 r = unit_name_build(n, NULL, ".target");
394 } else if (filename && streq(name, filename))
395 /* Names equaling the file name of the services are redundant */
398 /* Everything else we assume to be normal service names */
399 r = sysv_translate_name(n);
410 static int sysv_fix_order(Service *s) {
416 if (s->sysv_start_priority < 0)
419 /* For each pair of services where at least one lacks a LSB
420 * header, we use the start priority value to order things. */
422 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
425 bool special_s, special_t;
432 if (UNIT(t)->load_state != UNIT_LOADED)
435 if (t->sysv_start_priority < 0)
438 /* If both units have modern headers we don't care
439 * about the priorities */
440 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
441 (UNIT(t)->fragment_path || t->sysv_has_lsb))
444 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
445 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
447 if (special_t && !special_s)
449 else if (special_s && !special_t)
451 else if (t->sysv_start_priority < s->sysv_start_priority)
453 else if (t->sysv_start_priority > s->sysv_start_priority)
458 /* FIXME: Maybe we should compare the name here lexicographically? */
460 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
467 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
470 if (!(c = new0(ExecCommand, 1)))
473 if (!(c->path = strdup(path))) {
478 if (!(c->argv = strv_new(path, arg1, NULL))) {
487 static int sysv_exec_commands(Service *s, const bool supports_reload) {
492 assert(UNIT(s)->source_path);
494 c = exec_command_new(UNIT(s)->source_path, "start");
497 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
499 c = exec_command_new(UNIT(s)->source_path, "stop");
502 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
504 if (supports_reload) {
505 c = exec_command_new(UNIT(s)->source_path, "reload");
508 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
514 static bool usage_contains_reload(const char *line) {
515 return (strcasestr(line, "{reload|") ||
516 strcasestr(line, "{reload}") ||
517 strcasestr(line, "{reload\"") ||
518 strcasestr(line, "|reload|") ||
519 strcasestr(line, "|reload}") ||
520 strcasestr(line, "|reload\""));
523 static int service_load_sysv_path(Service *s, const char *path) {
535 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
537 bool supports_reload = false;
544 f = fopen(path, "re");
546 r = errno == ENOENT ? 0 : -errno;
550 if (fstat(fileno(f), &st) < 0) {
555 free(u->source_path);
556 u->source_path = strdup(path);
557 if (!u->source_path) {
561 u->source_mtime = timespec_load(&st.st_mtim);
563 if (null_or_empty(&st)) {
564 u->load_state = UNIT_MASKED;
572 char l[LINE_MAX], *t;
574 if (!fgets(l, sizeof(l), f)) {
579 log_error_unit(u->id,
580 "Failed to read configuration file '%s': %s",
589 /* Try to figure out whether this init script supports
590 * the reload operation. This heuristic looks for
591 * "Usage" lines which include the reload option. */
592 if ( state == USAGE_CONTINUATION ||
593 (state == NORMAL && strcasestr(t, "usage"))) {
594 if (usage_contains_reload(t)) {
595 supports_reload = true;
597 } else if (t[strlen(t)-1] == '\\')
598 state = USAGE_CONTINUATION;
606 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
608 s->sysv_has_lsb = true;
612 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
618 t += strspn(t, WHITESPACE);
620 if (state == NORMAL) {
622 /* Try to parse Red Hat style chkconfig headers */
624 if (startswith_no_case(t, "chkconfig:")) {
626 char runlevels[16], *k;
630 if (sscanf(t+10, "%15s %i %*i",
632 &start_priority) != 2) {
634 log_warning_unit(u->id,
635 "[%s:%u] Failed to parse chkconfig line. Ignoring.",
640 /* A start priority gathered from the
641 * symlink farms is preferred over the
642 * data from the LSB header. */
643 if (start_priority < 0 || start_priority > 99)
644 log_warning_unit(u->id,
645 "[%s:%u] Start priority out of range. Ignoring.",
648 s->sysv_start_priority = start_priority;
650 char_array_0(runlevels);
651 k = delete_chars(runlevels, WHITESPACE "-");
656 if (!(d = strdup(k))) {
661 free(s->sysv_runlevels);
662 s->sysv_runlevels = d;
665 } else if (startswith_no_case(t, "description:")) {
667 size_t k = strlen(t);
671 if (t[k-1] == '\\') {
676 if ((j = strstrip(t+12)) && *j) {
677 if (!(d = strdup(j))) {
684 free(chkconfig_description);
685 chkconfig_description = d;
687 } else if (startswith_no_case(t, "pidfile:")) {
694 if (!path_is_absolute(fn)) {
695 log_warning_unit(u->id,
696 "[%s:%u] PID file not absolute. Ignoring.",
701 if (!(fn = strdup(fn))) {
710 } else if (state == DESCRIPTION) {
712 /* Try to parse Red Hat style description
715 size_t k = strlen(t);
723 if ((j = strstrip(t)) && *j) {
726 if (chkconfig_description)
727 d = strjoin(chkconfig_description, " ", j, NULL);
736 free(chkconfig_description);
737 chkconfig_description = d;
740 } else if (state == LSB || state == LSB_DESCRIPTION) {
742 if (startswith_no_case(t, "Provides:")) {
748 FOREACH_WORD_QUOTED(w, z, t+9, i) {
751 if (!(n = strndup(w, z))) {
756 r = sysv_translate_facility(n, path_get_file_name(path), &m);
765 if (unit_name_to_type(m) == UNIT_SERVICE)
766 r = unit_add_name(u, m);
773 * indication that the
775 * now available. This
778 * targets do NOT pull
781 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
784 log_error_unit(u->id,
785 "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
786 path, line, m, strerror(-r));
791 } else if (startswith_no_case(t, "Required-Start:") ||
792 startswith_no_case(t, "Should-Start:") ||
793 startswith_no_case(t, "X-Start-Before:") ||
794 startswith_no_case(t, "X-Start-After:")) {
800 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
803 if (!(n = strndup(w, z))) {
808 r = sysv_translate_facility(n, path_get_file_name(path), &m);
810 log_error_unit(u->id,
811 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
812 path, line, n, strerror(-r));
822 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
825 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
826 path, line, m, strerror(-r));
830 } else if (startswith_no_case(t, "Default-Start:")) {
835 k = delete_chars(t+14, WHITESPACE "-");
838 if (!(d = strdup(k))) {
843 free(s->sysv_runlevels);
844 s->sysv_runlevels = d;
847 } else if (startswith_no_case(t, "Description:")) {
850 state = LSB_DESCRIPTION;
852 if ((j = strstrip(t+12)) && *j) {
853 if (!(d = strdup(j))) {
860 free(long_description);
861 long_description = d;
863 } else if (startswith_no_case(t, "Short-Description:")) {
868 if ((j = strstrip(t+18)) && *j) {
869 if (!(d = strdup(j))) {
876 free(short_description);
877 short_description = d;
879 } else if (state == LSB_DESCRIPTION) {
881 if (startswith(l, "#\t") || startswith(l, "# ")) {
884 if ((j = strstrip(t)) && *j) {
887 if (long_description)
888 d = strjoin(long_description, " ", t, NULL);
897 free(long_description);
898 long_description = d;
907 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
910 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
911 /* If there a runlevels configured for this service
912 * but none of the standard ones, then we assume this
913 * is some special kind of service (which might be
914 * needed for early boot) and don't create any links
917 UNIT(s)->default_dependencies = false;
919 /* Don't timeout special services during boot (like fsck) */
920 s->timeout_start_usec = 0;
921 s->timeout_stop_usec = 0;
923 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
924 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
927 /* Special setting for all SysV services */
928 s->type = SERVICE_FORKING;
929 s->remain_after_exit = !s->pid_file;
930 s->guess_main_pid = false;
931 s->restart = SERVICE_RESTART_NO;
932 s->exec_context.ignore_sigpipe = false;
933 s->kill_context.kill_mode = KILL_PROCESS;
935 /* We use the long description only if
936 * no short description is set. */
938 if (short_description)
939 description = short_description;
940 else if (chkconfig_description)
941 description = chkconfig_description;
942 else if (long_description)
943 description = long_description;
950 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
958 /* The priority that has been set in /etc/rcN.d/ hierarchies
959 * takes precedence over what is stored as default in the LSB
961 if (s->sysv_start_priority_from_rcnd >= 0)
962 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
964 u->load_state = UNIT_LOADED;
972 free(short_description);
973 free(long_description);
974 free(chkconfig_description);
979 static int service_load_sysv_name(Service *s, const char *name) {
985 /* For SysV services we strip the rc.* and *.sh
986 * prefixes/suffixes. */
987 if (startswith(name, "rc.") ||
988 endswith(name, ".sh.service"))
991 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
995 path = strjoin(*p, "/", name, NULL);
999 assert(endswith(path, ".service"));
1000 path[strlen(path)-8] = 0;
1002 r = service_load_sysv_path(s, path);
1004 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1005 /* Try *.sh source'able init scripts */
1006 strcat(path, ".sh");
1007 r = service_load_sysv_path(s, path);
1011 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1012 /* Try rc.* init scripts */
1014 path = strjoin(*p, "/rc.", name, NULL);
1018 /* Drop .service suffix */
1019 path[strlen(path)-8] = 0;
1020 r = service_load_sysv_path(s, path);
1027 if (UNIT(s)->load_state != UNIT_STUB)
1034 static int service_load_sysv(Service *s) {
1041 /* Load service data from SysV init scripts, preferably with
1042 * LSB headers ... */
1044 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1047 if ((t = UNIT(s)->id))
1048 if ((r = service_load_sysv_name(s, t)) < 0)
1051 if (UNIT(s)->load_state == UNIT_STUB)
1052 SET_FOREACH(t, UNIT(s)->names, i) {
1053 if (t == UNIT(s)->id)
1056 if ((r = service_load_sysv_name(s, t)) < 0)
1059 if (UNIT(s)->load_state != UNIT_STUB)
1067 static int fsck_fix_order(Service *s) {
1073 if (s->fsck_passno <= 0)
1076 /* For each pair of services where both have an fsck priority
1077 * we order things based on it. */
1079 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1088 if (UNIT(t)->load_state != UNIT_LOADED)
1091 if (t->fsck_passno <= 0)
1094 if (t->fsck_passno < s->fsck_passno)
1096 else if (t->fsck_passno > s->fsck_passno)
1101 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1108 static int service_verify(Service *s) {
1111 if (UNIT(s)->load_state != UNIT_LOADED)
1114 if (!s->exec_command[SERVICE_EXEC_START]) {
1115 log_error_unit(UNIT(s)->id,
1116 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1120 if (s->type != SERVICE_ONESHOT &&
1121 s->exec_command[SERVICE_EXEC_START]->command_next) {
1122 log_error_unit(UNIT(s)->id,
1123 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1127 if (s->type == SERVICE_DBUS && !s->bus_name) {
1128 log_error_unit(UNIT(s)->id,
1129 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1133 if (s->bus_name && s->type != SERVICE_DBUS)
1134 log_warning_unit(UNIT(s)->id,
1135 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1137 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1138 log_error_unit(UNIT(s)->id,
1139 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1146 static int service_add_default_dependencies(Service *s) {
1151 /* Add a number of automatic dependencies useful for the
1152 * majority of services. */
1154 /* First, pull in base system */
1155 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1157 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1160 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1162 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1166 /* Second, activate normal shutdown */
1167 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1170 static void service_fix_output(Service *s) {
1173 /* If nothing has been explicitly configured, patch default
1174 * output in. If input is socket/tty we avoid this however,
1175 * since in that case we want output to default to the same
1176 * place as we read input from. */
1178 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1179 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1180 s->exec_context.std_input == EXEC_INPUT_NULL)
1181 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1183 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1184 s->exec_context.std_input == EXEC_INPUT_NULL)
1185 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1188 static int service_load(Unit *u) {
1190 Service *s = SERVICE(u);
1194 /* Load a .service file */
1195 if ((r = unit_load_fragment(u)) < 0)
1198 #ifdef HAVE_SYSV_COMPAT
1199 /* Load a classic init script as a fallback, if we couldn't find anything */
1200 if (u->load_state == UNIT_STUB)
1201 if ((r = service_load_sysv(s)) < 0)
1205 /* Still nothing found? Then let's give up */
1206 if (u->load_state == UNIT_STUB)
1209 /* We were able to load something, then let's add in the
1210 * dropin directories. */
1211 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1214 /* This is a new unit? Then let's add in some extras */
1215 if (u->load_state == UNIT_LOADED) {
1216 if (s->type == _SERVICE_TYPE_INVALID)
1217 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1219 /* Oneshot services have disabled start timeout by default */
1220 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1221 s->timeout_start_usec = 0;
1223 service_fix_output(s);
1225 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1228 if ((r = unit_add_default_cgroups(u)) < 0)
1231 #ifdef HAVE_SYSV_COMPAT
1232 if ((r = sysv_fix_order(s)) < 0)
1236 if ((r = fsck_fix_order(s)) < 0)
1240 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1243 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1244 s->notify_access = NOTIFY_MAIN;
1246 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1247 s->notify_access = NOTIFY_MAIN;
1249 if (s->type == SERVICE_DBUS || s->bus_name)
1250 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1253 if (UNIT(s)->default_dependencies)
1254 if ((r = service_add_default_dependencies(s)) < 0)
1257 r = unit_exec_context_defaults(u, &s->exec_context);
1262 return service_verify(s);
1265 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1267 ServiceExecCommand c;
1268 Service *s = SERVICE(u);
1269 const char *prefix2;
1274 p2 = strappend(prefix, "\t");
1275 prefix2 = p2 ? p2 : prefix;
1278 "%sService State: %s\n"
1280 "%sReload Result: %s\n"
1281 "%sPermissionsStartOnly: %s\n"
1282 "%sRootDirectoryStartOnly: %s\n"
1283 "%sRemainAfterExit: %s\n"
1284 "%sGuessMainPID: %s\n"
1287 "%sNotifyAccess: %s\n",
1288 prefix, service_state_to_string(s->state),
1289 prefix, service_result_to_string(s->result),
1290 prefix, service_result_to_string(s->reload_result),
1291 prefix, yes_no(s->permissions_start_only),
1292 prefix, yes_no(s->root_directory_start_only),
1293 prefix, yes_no(s->remain_after_exit),
1294 prefix, yes_no(s->guess_main_pid),
1295 prefix, service_type_to_string(s->type),
1296 prefix, service_restart_to_string(s->restart),
1297 prefix, notify_access_to_string(s->notify_access));
1299 if (s->control_pid > 0)
1301 "%sControl PID: %lu\n",
1302 prefix, (unsigned long) s->control_pid);
1304 if (s->main_pid > 0)
1307 "%sMain PID Known: %s\n"
1308 "%sMain PID Alien: %s\n",
1309 prefix, (unsigned long) s->main_pid,
1310 prefix, yes_no(s->main_pid_known),
1311 prefix, yes_no(s->main_pid_alien));
1316 prefix, s->pid_file);
1321 "%sBus Name Good: %s\n",
1322 prefix, s->bus_name,
1323 prefix, yes_no(s->bus_name_good));
1325 kill_context_dump(&s->kill_context, f, prefix);
1326 exec_context_dump(&s->exec_context, f, prefix);
1328 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1330 if (!s->exec_command[c])
1333 fprintf(f, "%s-> %s:\n",
1334 prefix, service_exec_command_to_string(c));
1336 exec_command_dump_list(s->exec_command[c], f, prefix2);
1339 #ifdef HAVE_SYSV_COMPAT
1342 "%sSysV Init Script has LSB Header: %s\n"
1343 "%sSysVEnabled: %s\n",
1344 prefix, yes_no(s->sysv_has_lsb),
1345 prefix, yes_no(s->sysv_enabled));
1347 if (s->sysv_start_priority >= 0)
1349 "%sSysVStartPriority: %i\n",
1350 prefix, s->sysv_start_priority);
1352 if (s->sysv_runlevels)
1353 fprintf(f, "%sSysVRunLevels: %s\n",
1354 prefix, s->sysv_runlevels);
1357 if (s->fsck_passno > 0)
1359 "%sFsckPassNo: %i\n",
1360 prefix, s->fsck_passno);
1363 fprintf(f, "%sStatus Text: %s\n",
1364 prefix, s->status_text);
1369 static int service_load_pid_file(Service *s, bool may_warn) {
1379 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1381 log_info_unit(UNIT(s)->id,
1382 "PID file %s not readable (yet?) after %s.",
1383 s->pid_file, service_state_to_string(s->state));
1387 r = parse_pid(k, &pid);
1393 if (kill(pid, 0) < 0 && errno != EPERM) {
1395 log_info_unit(UNIT(s)->id,
1396 "PID %lu read from file %s does not exist.",
1397 (unsigned long) pid, s->pid_file);
1401 if (s->main_pid_known) {
1402 if (pid == s->main_pid)
1405 log_debug_unit(UNIT(s)->id,
1406 "Main PID changing: %lu -> %lu",
1407 (unsigned long) s->main_pid, (unsigned long) pid);
1408 service_unwatch_main_pid(s);
1409 s->main_pid_known = false;
1411 log_debug_unit(UNIT(s)->id,
1412 "Main PID loaded: %lu", (unsigned long) pid);
1414 if ((r = service_set_main_pid(s, pid)) < 0)
1417 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1418 /* FIXME: we need to do something here */
1424 static int service_search_main_pid(Service *s) {
1430 /* If we know it anyway, don't ever fallback to unreliable
1432 if (s->main_pid_known)
1435 if (!s->guess_main_pid)
1438 assert(s->main_pid <= 0);
1440 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1443 log_debug_unit(UNIT(s)->id,
1444 "Main PID guessed: %lu", (unsigned long) pid);
1445 if ((r = service_set_main_pid(s, pid)) < 0)
1448 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1449 /* FIXME: we need to do something here */
1455 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1461 /* Notifies all our sockets when we die */
1463 if (s->socket_fd >= 0)
1466 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1467 if (u->type == UNIT_SOCKET)
1468 socket_notify_service_dead(SOCKET(u), failed_permanent);
1473 static void service_set_state(Service *s, ServiceState state) {
1474 ServiceState old_state;
1475 const UnitActiveState *table;
1478 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1480 old_state = s->state;
1483 service_unwatch_pid_file(s);
1485 if (state != SERVICE_START_PRE &&
1486 state != SERVICE_START &&
1487 state != SERVICE_START_POST &&
1488 state != SERVICE_RELOAD &&
1489 state != SERVICE_STOP &&
1490 state != SERVICE_STOP_SIGTERM &&
1491 state != SERVICE_STOP_SIGKILL &&
1492 state != SERVICE_STOP_POST &&
1493 state != SERVICE_FINAL_SIGTERM &&
1494 state != SERVICE_FINAL_SIGKILL &&
1495 state != SERVICE_AUTO_RESTART)
1496 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1498 if (state != SERVICE_START &&
1499 state != SERVICE_START_POST &&
1500 state != SERVICE_RUNNING &&
1501 state != SERVICE_RELOAD &&
1502 state != SERVICE_STOP &&
1503 state != SERVICE_STOP_SIGTERM &&
1504 state != SERVICE_STOP_SIGKILL) {
1505 service_unwatch_main_pid(s);
1506 s->main_command = NULL;
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 service_unwatch_control_pid(s);
1520 s->control_command = NULL;
1521 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1524 if (state == SERVICE_FAILED)
1525 service_notify_sockets_dead(s, s->result == SERVICE_FAILURE_START_LIMIT);
1527 if (state == SERVICE_DEAD ||
1528 state == SERVICE_STOP ||
1529 state == SERVICE_STOP_SIGTERM ||
1530 state == SERVICE_STOP_SIGKILL ||
1531 state == SERVICE_STOP_POST ||
1532 state == SERVICE_FINAL_SIGTERM ||
1533 state == SERVICE_FINAL_SIGKILL ||
1534 state == SERVICE_AUTO_RESTART)
1535 service_notify_sockets_dead(s, false);
1537 if (state != SERVICE_START_PRE &&
1538 state != SERVICE_START &&
1539 state != SERVICE_START_POST &&
1540 state != SERVICE_RUNNING &&
1541 state != SERVICE_RELOAD &&
1542 state != SERVICE_STOP &&
1543 state != SERVICE_STOP_SIGTERM &&
1544 state != SERVICE_STOP_SIGKILL &&
1545 state != SERVICE_STOP_POST &&
1546 state != SERVICE_FINAL_SIGTERM &&
1547 state != SERVICE_FINAL_SIGKILL &&
1548 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1549 service_close_socket_fd(s);
1550 service_connection_unref(s);
1553 if (state == SERVICE_STOP)
1554 service_stop_watchdog(s);
1556 /* For the inactive states unit_notify() will trim the cgroup,
1557 * but for exit we have to do that ourselves... */
1558 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1559 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1561 if (old_state != state)
1562 log_debug_unit(UNIT(s)->id,
1563 "%s changed %s -> %s", UNIT(s)->id,
1564 service_state_to_string(old_state),
1565 service_state_to_string(state));
1567 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1568 s->reload_result = SERVICE_SUCCESS;
1571 static int service_coldplug(Unit *u) {
1572 Service *s = SERVICE(u);
1576 assert(s->state == SERVICE_DEAD);
1578 if (s->deserialized_state != s->state) {
1580 if (s->deserialized_state == SERVICE_START_PRE ||
1581 s->deserialized_state == SERVICE_START ||
1582 s->deserialized_state == SERVICE_START_POST ||
1583 s->deserialized_state == SERVICE_RELOAD ||
1584 s->deserialized_state == SERVICE_STOP ||
1585 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1586 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1587 s->deserialized_state == SERVICE_STOP_POST ||
1588 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1589 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1590 s->deserialized_state == SERVICE_AUTO_RESTART) {
1591 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1594 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1596 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
1602 if ((s->deserialized_state == SERVICE_START &&
1603 (s->type == SERVICE_FORKING ||
1604 s->type == SERVICE_DBUS ||
1605 s->type == SERVICE_ONESHOT ||
1606 s->type == SERVICE_NOTIFY)) ||
1607 s->deserialized_state == SERVICE_START_POST ||
1608 s->deserialized_state == SERVICE_RUNNING ||
1609 s->deserialized_state == SERVICE_RELOAD ||
1610 s->deserialized_state == SERVICE_STOP ||
1611 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1612 s->deserialized_state == SERVICE_STOP_SIGKILL)
1613 if (s->main_pid > 0)
1614 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1617 if (s->deserialized_state == SERVICE_START_PRE ||
1618 s->deserialized_state == SERVICE_START ||
1619 s->deserialized_state == SERVICE_START_POST ||
1620 s->deserialized_state == SERVICE_RELOAD ||
1621 s->deserialized_state == SERVICE_STOP ||
1622 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1623 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1624 s->deserialized_state == SERVICE_STOP_POST ||
1625 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1626 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1627 if (s->control_pid > 0)
1628 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1631 if (s->deserialized_state == SERVICE_START_POST ||
1632 s->deserialized_state == SERVICE_RUNNING)
1633 service_handle_watchdog(s);
1635 service_set_state(s, s->deserialized_state);
1640 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1644 unsigned rn_fds = 0;
1651 if (s->socket_fd >= 0)
1654 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1659 if (u->type != UNIT_SOCKET)
1664 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1676 if (!(t = new(int, rn_fds+cn_fds))) {
1682 memcpy(t, rfds, rn_fds * sizeof(int));
1683 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1688 rn_fds = rn_fds+cn_fds;
1703 static int service_spawn(
1708 bool apply_permissions,
1710 bool apply_tty_stdin,
1711 bool set_notify_socket,
1717 int *fds = NULL, *fdsbuf = NULL;
1718 unsigned n_fds = 0, n_env = 0;
1719 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1726 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1727 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1728 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1730 if (s->socket_fd >= 0) {
1731 fds = &s->socket_fd;
1734 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1741 if (timeout && s->timeout_start_usec) {
1742 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_start_usec, &s->timer_watch);
1746 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1748 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1753 our_env = new0(char*, 5);
1759 if (set_notify_socket)
1760 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1765 if (s->main_pid > 0)
1766 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1771 if (s->watchdog_usec > 0)
1772 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1777 if (s->meta.manager->running_as != SYSTEMD_SYSTEM)
1778 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1783 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1797 UNIT(s)->manager->confirm_spawn,
1798 UNIT(s)->cgroup_bondings,
1799 UNIT(s)->cgroup_attributes,
1800 is_control ? "control" : NULL,
1802 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1808 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1809 /* FIXME: we need to do something here */
1815 strv_free(final_env);
1825 strv_free(final_env);
1828 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1833 static int main_pid_good(Service *s) {
1836 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1839 /* If we know the pid file, then lets just check if it is
1841 if (s->main_pid_known) {
1843 /* If it's an alien child let's check if it is still
1845 if (s->main_pid_alien)
1846 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1848 /* .. otherwise assume we'll get a SIGCHLD for it,
1849 * which we really should wait for to collect exit
1850 * status and code */
1851 return s->main_pid > 0;
1854 /* We don't know the pid */
1858 static int control_pid_good(Service *s) {
1861 return s->control_pid > 0;
1864 static int cgroup_good(Service *s) {
1869 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1875 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1879 if (f != SERVICE_SUCCESS)
1882 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1884 if (allow_restart &&
1885 !s->forbid_restart &&
1886 (s->restart == SERVICE_RESTART_ALWAYS ||
1887 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1888 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1889 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1890 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1891 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1892 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1893 (s->result != SERVICE_FAILURE_SIGNAL ||
1894 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1897 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
1901 service_set_state(s, SERVICE_AUTO_RESTART);
1904 s->forbid_restart = false;
1909 log_warning_unit(UNIT(s)->id,
1910 "%s failed to run install restart timer: %s",
1911 UNIT(s)->id, strerror(-r));
1912 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1915 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1917 static void service_enter_stop_post(Service *s, ServiceResult f) {
1921 if (f != SERVICE_SUCCESS)
1924 service_unwatch_control_pid(s);
1926 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1927 s->control_command_id = SERVICE_EXEC_STOP_POST;
1929 r = service_spawn(s,
1933 !s->permissions_start_only,
1934 !s->root_directory_start_only,
1943 service_set_state(s, SERVICE_STOP_POST);
1945 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1950 log_warning_unit(UNIT(s)->id,
1951 "%s failed to run 'stop-post' task: %s",
1952 UNIT(s)->id, strerror(-r));
1953 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1956 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1958 Set *pid_set = NULL;
1959 bool wait_for_exit = false;
1963 if (f != SERVICE_SUCCESS)
1966 if (s->kill_context.kill_mode != KILL_NONE) {
1967 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->kill_context.kill_signal : SIGKILL;
1969 if (s->main_pid > 0) {
1970 if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
1971 log_warning_unit(UNIT(s)->id,
1972 "Failed to kill main process %li: %m", (long) s->main_pid);
1974 wait_for_exit = !s->main_pid_alien;
1977 if (s->control_pid > 0) {
1978 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1979 log_warning_unit(UNIT(s)->id,
1980 "Failed to kill control process %li: %m", (long) s->control_pid);
1982 wait_for_exit = true;
1985 if (s->kill_context.kill_mode == KILL_CONTROL_GROUP) {
1987 pid_set = set_new(trivial_hash_func, trivial_compare_func);
1993 /* Exclude the main/control pids from being killed via the cgroup */
1994 if (s->main_pid > 0)
1995 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1998 if (s->control_pid > 0)
1999 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
2002 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
2004 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
2005 log_warning_unit(UNIT(s)->id,
2006 "Failed to kill control group: %s", strerror(-r));
2008 wait_for_exit = true;
2015 if (wait_for_exit) {
2016 if (s->timeout_stop_usec > 0) {
2017 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_stop_usec, &s->timer_watch);
2022 service_set_state(s, state);
2023 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2024 service_enter_stop_post(s, SERVICE_SUCCESS);
2026 service_enter_dead(s, SERVICE_SUCCESS, true);
2031 log_warning_unit(UNIT(s)->id,
2032 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2034 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2035 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2037 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2043 static void service_enter_stop(Service *s, ServiceResult f) {
2048 if (f != SERVICE_SUCCESS)
2051 service_unwatch_control_pid(s);
2053 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2054 s->control_command_id = SERVICE_EXEC_STOP;
2056 r = service_spawn(s,
2060 !s->permissions_start_only,
2061 !s->root_directory_start_only,
2069 service_set_state(s, SERVICE_STOP);
2071 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2076 log_warning_unit(UNIT(s)->id,
2077 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2078 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2081 static void service_enter_running(Service *s, ServiceResult f) {
2082 int main_pid_ok, cgroup_ok;
2085 if (f != SERVICE_SUCCESS)
2088 main_pid_ok = main_pid_good(s);
2089 cgroup_ok = cgroup_good(s);
2091 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2092 (s->bus_name_good || s->type != SERVICE_DBUS))
2093 service_set_state(s, SERVICE_RUNNING);
2094 else if (s->remain_after_exit)
2095 service_set_state(s, SERVICE_EXITED);
2097 service_enter_stop(s, SERVICE_SUCCESS);
2100 static void service_enter_start_post(Service *s) {
2104 service_unwatch_control_pid(s);
2106 if (s->watchdog_usec > 0)
2107 service_reset_watchdog(s);
2109 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2110 s->control_command_id = SERVICE_EXEC_START_POST;
2112 r = service_spawn(s,
2116 !s->permissions_start_only,
2117 !s->root_directory_start_only,
2125 service_set_state(s, SERVICE_START_POST);
2127 service_enter_running(s, SERVICE_SUCCESS);
2132 log_warning_unit(UNIT(s)->id,
2133 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2134 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2137 static void service_enter_start(Service *s) {
2144 assert(s->exec_command[SERVICE_EXEC_START]);
2145 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2147 if (s->type == SERVICE_FORKING)
2148 service_unwatch_control_pid(s);
2150 service_unwatch_main_pid(s);
2152 /* We want to ensure that nobody leaks processes from
2153 * START_PRE here, so let's go on a killing spree, People
2154 * should not spawn long running processes from START_PRE. */
2155 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2157 if (s->type == SERVICE_FORKING) {
2158 s->control_command_id = SERVICE_EXEC_START;
2159 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2161 s->main_command = NULL;
2163 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2164 s->control_command = NULL;
2166 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2169 r = service_spawn(s,
2171 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2176 s->notify_access != NOTIFY_NONE,
2182 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2183 /* For simple services we immediately start
2184 * the START_POST binaries. */
2186 service_set_main_pid(s, pid);
2187 service_enter_start_post(s);
2189 } else if (s->type == SERVICE_FORKING) {
2191 /* For forking services we wait until the start
2192 * process exited. */
2194 s->control_pid = pid;
2195 service_set_state(s, SERVICE_START);
2197 } else if (s->type == SERVICE_ONESHOT ||
2198 s->type == SERVICE_DBUS ||
2199 s->type == SERVICE_NOTIFY) {
2201 /* For oneshot services we wait until the start
2202 * process exited, too, but it is our main process. */
2204 /* For D-Bus services we know the main pid right away,
2205 * but wait for the bus name to appear on the
2206 * bus. Notify services are similar. */
2208 service_set_main_pid(s, pid);
2209 service_set_state(s, SERVICE_START);
2211 assert_not_reached("Unknown service type");
2216 log_warning_unit(UNIT(s)->id,
2217 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2218 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2221 static void service_enter_start_pre(Service *s) {
2226 service_unwatch_control_pid(s);
2228 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2230 /* Before we start anything, let's clear up what might
2231 * be left from previous runs. */
2232 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2234 s->control_command_id = SERVICE_EXEC_START_PRE;
2236 r = service_spawn(s,
2240 !s->permissions_start_only,
2241 !s->root_directory_start_only,
2249 service_set_state(s, SERVICE_START_PRE);
2251 service_enter_start(s);
2256 log_warning_unit(UNIT(s)->id,
2257 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2258 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2261 static void service_enter_restart(Service *s) {
2266 dbus_error_init(&error);
2268 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2269 /* Don't restart things if we are going down anyway */
2270 log_info_unit(UNIT(s)->id,
2271 "Stop job pending for unit, delaying automatic restart.");
2273 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
2280 /* Any units that are bound to this service must also be
2281 * restarted. We use JOB_RESTART (instead of the more obvious
2282 * JOB_START) here so that those dependency jobs will be added
2284 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2288 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2289 * it will be canceled as part of the service_stop() call that
2290 * is executed as part of JOB_RESTART. */
2292 log_debug_unit(UNIT(s)->id,
2293 "%s scheduled restart job.", UNIT(s)->id);
2297 log_warning_unit(UNIT(s)->id,
2298 "%s failed to schedule restart job: %s",
2299 UNIT(s)->id, bus_error(&error, -r));
2300 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2302 dbus_error_free(&error);
2305 static void service_enter_reload(Service *s) {
2310 service_unwatch_control_pid(s);
2312 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2313 s->control_command_id = SERVICE_EXEC_RELOAD;
2315 r = service_spawn(s,
2319 !s->permissions_start_only,
2320 !s->root_directory_start_only,
2328 service_set_state(s, SERVICE_RELOAD);
2330 service_enter_running(s, SERVICE_SUCCESS);
2335 log_warning_unit(UNIT(s)->id,
2336 "%s failed to run 'reload' task: %s",
2337 UNIT(s)->id, strerror(-r));
2338 s->reload_result = SERVICE_FAILURE_RESOURCES;
2339 service_enter_running(s, SERVICE_SUCCESS);
2342 static void service_run_next_control(Service *s) {
2346 assert(s->control_command);
2347 assert(s->control_command->command_next);
2349 assert(s->control_command_id != SERVICE_EXEC_START);
2351 s->control_command = s->control_command->command_next;
2352 service_unwatch_control_pid(s);
2354 r = service_spawn(s,
2358 !s->permissions_start_only,
2359 !s->root_directory_start_only,
2360 s->control_command_id == SERVICE_EXEC_START_PRE ||
2361 s->control_command_id == SERVICE_EXEC_STOP_POST,
2371 log_warning_unit(UNIT(s)->id,
2372 "%s failed to run next control task: %s",
2373 UNIT(s)->id, strerror(-r));
2375 if (s->state == SERVICE_START_PRE)
2376 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2377 else if (s->state == SERVICE_STOP)
2378 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2379 else if (s->state == SERVICE_STOP_POST)
2380 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2381 else if (s->state == SERVICE_RELOAD) {
2382 s->reload_result = SERVICE_FAILURE_RESOURCES;
2383 service_enter_running(s, SERVICE_SUCCESS);
2385 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2388 static void service_run_next_main(Service *s) {
2393 assert(s->main_command);
2394 assert(s->main_command->command_next);
2395 assert(s->type == SERVICE_ONESHOT);
2397 s->main_command = s->main_command->command_next;
2398 service_unwatch_main_pid(s);
2400 r = service_spawn(s,
2407 s->notify_access != NOTIFY_NONE,
2413 service_set_main_pid(s, pid);
2418 log_warning_unit(UNIT(s)->id,
2419 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2420 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2423 static int service_start_limit_test(Service *s) {
2426 if (ratelimit_test(&s->start_limit))
2429 switch (s->start_limit_action) {
2431 case SERVICE_START_LIMIT_NONE:
2432 log_warning_unit(UNIT(s)->id,
2433 "%s start request repeated too quickly, refusing to start.",
2437 case SERVICE_START_LIMIT_REBOOT: {
2441 dbus_error_init(&error);
2443 log_warning_unit(UNIT(s)->id,
2444 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2446 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2448 log_error_unit(UNIT(s)->id,
2449 "Failed to reboot: %s.", bus_error(&error, r));
2450 dbus_error_free(&error);
2456 case SERVICE_START_LIMIT_REBOOT_FORCE:
2457 log_warning_unit(UNIT(s)->id,
2458 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2459 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2462 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2463 log_warning_unit(UNIT(s)->id,
2464 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2466 reboot(RB_AUTOBOOT);
2470 log_error_unit(UNIT(s)->id,
2471 "start limit action=%i", s->start_limit_action);
2472 assert_not_reached("Unknown StartLimitAction.");
2478 static int service_start(Unit *u) {
2479 Service *s = SERVICE(u);
2484 /* We cannot fulfill this request right now, try again later
2486 if (s->state == SERVICE_STOP ||
2487 s->state == SERVICE_STOP_SIGTERM ||
2488 s->state == SERVICE_STOP_SIGKILL ||
2489 s->state == SERVICE_STOP_POST ||
2490 s->state == SERVICE_FINAL_SIGTERM ||
2491 s->state == SERVICE_FINAL_SIGKILL)
2494 /* Already on it! */
2495 if (s->state == SERVICE_START_PRE ||
2496 s->state == SERVICE_START ||
2497 s->state == SERVICE_START_POST)
2500 /* A service that will be restarted must be stopped first to
2501 * trigger BindsTo and/or OnFailure dependencies. If a user
2502 * does not want to wait for the holdoff time to elapse, the
2503 * service should be manually restarted, not started. We
2504 * simply return EAGAIN here, so that any start jobs stay
2505 * queued, and assume that the auto restart timer will
2506 * eventually trigger the restart. */
2507 if (s->state == SERVICE_AUTO_RESTART)
2510 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2512 /* Make sure we don't enter a busy loop of some kind. */
2513 r = service_start_limit_test(s);
2515 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2519 s->result = SERVICE_SUCCESS;
2520 s->reload_result = SERVICE_SUCCESS;
2521 s->main_pid_known = false;
2522 s->main_pid_alien = false;
2523 s->forbid_restart = false;
2525 service_enter_start_pre(s);
2529 static int service_stop(Unit *u) {
2530 Service *s = SERVICE(u);
2534 /* Don't create restart jobs from here. */
2535 s->forbid_restart = true;
2538 if (s->state == SERVICE_STOP ||
2539 s->state == SERVICE_STOP_SIGTERM ||
2540 s->state == SERVICE_STOP_SIGKILL ||
2541 s->state == SERVICE_STOP_POST ||
2542 s->state == SERVICE_FINAL_SIGTERM ||
2543 s->state == SERVICE_FINAL_SIGKILL)
2546 /* A restart will be scheduled or is in progress. */
2547 if (s->state == SERVICE_AUTO_RESTART) {
2548 service_set_state(s, SERVICE_DEAD);
2552 /* If there's already something running we go directly into
2554 if (s->state == SERVICE_START_PRE ||
2555 s->state == SERVICE_START ||
2556 s->state == SERVICE_START_POST ||
2557 s->state == SERVICE_RELOAD) {
2558 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2562 assert(s->state == SERVICE_RUNNING ||
2563 s->state == SERVICE_EXITED);
2565 service_enter_stop(s, SERVICE_SUCCESS);
2569 static int service_reload(Unit *u) {
2570 Service *s = SERVICE(u);
2574 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2576 service_enter_reload(s);
2580 static bool service_can_reload(Unit *u) {
2581 Service *s = SERVICE(u);
2585 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2588 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2589 Service *s = SERVICE(u);
2595 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2596 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2597 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2599 if (s->control_pid > 0)
2600 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2602 if (s->main_pid_known && s->main_pid > 0)
2603 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2605 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2608 unit_serialize_item(u, f, "status-text", s->status_text);
2610 /* FIXME: There's a minor uncleanliness here: if there are
2611 * multiple commands attached here, we will start from the
2612 * first one again */
2613 if (s->control_command_id >= 0)
2614 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2616 if (s->socket_fd >= 0) {
2619 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2622 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2625 if (s->main_exec_status.pid > 0) {
2626 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2627 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2628 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2630 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2631 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2632 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2635 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2636 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2641 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2642 Service *s = SERVICE(u);
2649 if (streq(key, "state")) {
2652 if ((state = service_state_from_string(value)) < 0)
2653 log_debug_unit(u->id, "Failed to parse state value %s", value);
2655 s->deserialized_state = state;
2656 } else if (streq(key, "result")) {
2659 f = service_result_from_string(value);
2661 log_debug_unit(u->id, "Failed to parse result value %s", value);
2662 else if (f != SERVICE_SUCCESS)
2665 } else if (streq(key, "reload-result")) {
2668 f = service_result_from_string(value);
2670 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2671 else if (f != SERVICE_SUCCESS)
2672 s->reload_result = f;
2674 } else if (streq(key, "control-pid")) {
2677 if (parse_pid(value, &pid) < 0)
2678 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2680 s->control_pid = pid;
2681 } else if (streq(key, "main-pid")) {
2684 if (parse_pid(value, &pid) < 0)
2685 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2687 service_set_main_pid(s, (pid_t) pid);
2688 } else if (streq(key, "main-pid-known")) {
2691 if ((b = parse_boolean(value)) < 0)
2692 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2694 s->main_pid_known = b;
2695 } else if (streq(key, "status-text")) {
2698 if ((t = strdup(value))) {
2699 free(s->status_text);
2703 } else if (streq(key, "control-command")) {
2704 ServiceExecCommand id;
2706 if ((id = service_exec_command_from_string(value)) < 0)
2707 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2709 s->control_command_id = id;
2710 s->control_command = s->exec_command[id];
2712 } else if (streq(key, "socket-fd")) {
2715 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2716 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2719 if (s->socket_fd >= 0)
2720 close_nointr_nofail(s->socket_fd);
2721 s->socket_fd = fdset_remove(fds, fd);
2723 } else if (streq(key, "main-exec-status-pid")) {
2726 if (parse_pid(value, &pid) < 0)
2727 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2729 s->main_exec_status.pid = pid;
2730 } else if (streq(key, "main-exec-status-code")) {
2733 if (safe_atoi(value, &i) < 0)
2734 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2736 s->main_exec_status.code = i;
2737 } else if (streq(key, "main-exec-status-status")) {
2740 if (safe_atoi(value, &i) < 0)
2741 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2743 s->main_exec_status.status = i;
2744 } else if (streq(key, "main-exec-status-start"))
2745 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2746 else if (streq(key, "main-exec-status-exit"))
2747 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2748 else if (streq(key, "watchdog-timestamp"))
2749 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2751 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2756 static UnitActiveState service_active_state(Unit *u) {
2757 const UnitActiveState *table;
2761 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2763 return table[SERVICE(u)->state];
2766 static const char *service_sub_state_to_string(Unit *u) {
2769 return service_state_to_string(SERVICE(u)->state);
2772 static bool service_check_gc(Unit *u) {
2773 Service *s = SERVICE(u);
2777 /* Never clean up services that still have a process around,
2778 * even if the service is formally dead. */
2779 if (cgroup_good(s) > 0 ||
2780 main_pid_good(s) > 0 ||
2781 control_pid_good(s) > 0)
2784 #ifdef HAVE_SYSV_COMPAT
2792 static bool service_check_snapshot(Unit *u) {
2793 Service *s = SERVICE(u);
2797 return !s->got_socket_fd;
2800 static int service_retry_pid_file(Service *s) {
2803 assert(s->pid_file);
2804 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2806 r = service_load_pid_file(s, false);
2810 service_unwatch_pid_file(s);
2812 service_enter_running(s, SERVICE_SUCCESS);
2816 static int service_watch_pid_file(Service *s) {
2819 log_debug_unit(UNIT(s)->id,
2820 "Setting watch for %s's PID file %s",
2821 UNIT(s)->id, s->pid_file_pathspec->path);
2822 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2826 /* the pidfile might have appeared just before we set the watch */
2827 service_retry_pid_file(s);
2831 log_error_unit(UNIT(s)->id,
2832 "Failed to set a watch for %s's PID file %s: %s",
2833 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2834 service_unwatch_pid_file(s);
2838 static int service_demand_pid_file(Service *s) {
2841 assert(s->pid_file);
2842 assert(!s->pid_file_pathspec);
2844 ps = new0(PathSpec, 1);
2848 ps->path = strdup(s->pid_file);
2854 path_kill_slashes(ps->path);
2856 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2857 * keep their PID file open all the time. */
2858 ps->type = PATH_MODIFIED;
2859 ps->inotify_fd = -1;
2861 s->pid_file_pathspec = ps;
2863 return service_watch_pid_file(s);
2866 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2867 Service *s = SERVICE(u);
2871 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2872 assert(s->pid_file_pathspec);
2873 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2875 log_debug_unit(u->id, "inotify event for %s", u->id);
2877 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2880 if (service_retry_pid_file(s) == 0)
2883 if (service_watch_pid_file(s) < 0)
2888 service_unwatch_pid_file(s);
2889 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2892 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2893 Service *s = SERVICE(u);
2899 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2900 is_clean_exit_lsb(code, status, &s->success_status))
2901 f = SERVICE_SUCCESS;
2902 else if (code == CLD_EXITED)
2903 f = SERVICE_FAILURE_EXIT_CODE;
2904 else if (code == CLD_KILLED)
2905 f = SERVICE_FAILURE_SIGNAL;
2906 else if (code == CLD_DUMPED)
2907 f = SERVICE_FAILURE_CORE_DUMP;
2909 assert_not_reached("Unknown code");
2911 if (s->main_pid == pid) {
2912 /* Forking services may occasionally move to a new PID.
2913 * As long as they update the PID file before exiting the old
2914 * PID, they're fine. */
2915 if (service_load_pid_file(s, false) == 0)
2919 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2921 if (s->main_command) {
2922 /* If this is not a forking service than the
2923 * main process got started and hence we copy
2924 * the exit status so that it is recorded both
2925 * as main and as control process exit
2928 s->main_command->exec_status = s->main_exec_status;
2930 if (s->main_command->ignore)
2931 f = SERVICE_SUCCESS;
2932 } else if (s->exec_command[SERVICE_EXEC_START]) {
2934 /* If this is a forked process, then we should
2935 * ignore the return value if this was
2936 * configured for the starter process */
2938 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2939 f = SERVICE_SUCCESS;
2942 log_struct(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2943 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2944 u->id, sigchld_code_to_string(code), status,
2945 strna(code == CLD_EXITED
2946 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2947 : signal_to_string(status)),
2949 "EXIT_CODE=%s", sigchld_code_to_string(code),
2950 "EXIT_STATUS=%i", status,
2953 if (f != SERVICE_SUCCESS)
2956 if (s->main_command &&
2957 s->main_command->command_next &&
2958 f == SERVICE_SUCCESS) {
2960 /* There is another command to *
2961 * execute, so let's do that. */
2963 log_debug_unit(u->id,
2964 "%s running next main command for state %s",
2965 u->id, service_state_to_string(s->state));
2966 service_run_next_main(s);
2970 /* The service exited, so the service is officially
2972 s->main_command = NULL;
2976 case SERVICE_START_POST:
2977 case SERVICE_RELOAD:
2979 /* Need to wait until the operation is
2984 if (s->type == SERVICE_ONESHOT) {
2985 /* This was our main goal, so let's go on */
2986 if (f == SERVICE_SUCCESS)
2987 service_enter_start_post(s);
2989 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2995 case SERVICE_RUNNING:
2996 service_enter_running(s, f);
2999 case SERVICE_STOP_SIGTERM:
3000 case SERVICE_STOP_SIGKILL:
3002 if (!control_pid_good(s))
3003 service_enter_stop_post(s, f);
3005 /* If there is still a control process, wait for that first */
3009 assert_not_reached("Uh, main process died at wrong time.");
3013 } else if (s->control_pid == pid) {
3017 if (s->control_command) {
3018 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
3020 if (s->control_command->ignore)
3021 f = SERVICE_SUCCESS;
3024 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
3025 "%s: control process exited, code=%s status=%i",
3026 u->id, sigchld_code_to_string(code), status);
3028 if (f != SERVICE_SUCCESS)
3031 /* Immediately get rid of the cgroup, so that the
3032 * kernel doesn't delay the cgroup empty messages for
3033 * the service cgroup any longer than necessary */
3034 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
3036 if (s->control_command &&
3037 s->control_command->command_next &&
3038 f == SERVICE_SUCCESS) {
3040 /* There is another command to *
3041 * execute, so let's do that. */
3043 log_debug_unit(u->id,
3044 "%s running next control command for state %s",
3045 u->id, service_state_to_string(s->state));
3046 service_run_next_control(s);
3049 /* No further commands for this step, so let's
3050 * figure out what to do next */
3052 s->control_command = NULL;
3053 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3055 log_debug_unit(u->id,
3056 "%s got final SIGCHLD for state %s",
3057 u->id, service_state_to_string(s->state));
3061 case SERVICE_START_PRE:
3062 if (f == SERVICE_SUCCESS)
3063 service_enter_start(s);
3065 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3069 if (s->type != SERVICE_FORKING)
3070 /* Maybe spurious event due to a reload that changed the type? */
3073 if (f != SERVICE_SUCCESS) {
3074 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3079 bool has_start_post;
3082 /* Let's try to load the pid file here if we can.
3083 * The PID file might actually be created by a START_POST
3084 * script. In that case don't worry if the loading fails. */
3086 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3087 r = service_load_pid_file(s, !has_start_post);
3088 if (!has_start_post && r < 0) {
3089 r = service_demand_pid_file(s);
3090 if (r < 0 || !cgroup_good(s))
3091 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3095 service_search_main_pid(s);
3097 service_enter_start_post(s);
3100 case SERVICE_START_POST:
3101 if (f != SERVICE_SUCCESS) {
3102 service_enter_stop(s, f);
3109 r = service_load_pid_file(s, true);
3111 r = service_demand_pid_file(s);
3112 if (r < 0 || !cgroup_good(s))
3113 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3117 service_search_main_pid(s);
3119 service_enter_running(s, SERVICE_SUCCESS);
3122 case SERVICE_RELOAD:
3123 if (f == SERVICE_SUCCESS) {
3124 service_load_pid_file(s, true);
3125 service_search_main_pid(s);
3128 s->reload_result = f;
3129 service_enter_running(s, SERVICE_SUCCESS);
3133 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3136 case SERVICE_STOP_SIGTERM:
3137 case SERVICE_STOP_SIGKILL:
3138 if (main_pid_good(s) <= 0)
3139 service_enter_stop_post(s, f);
3141 /* If there is still a service
3142 * process around, wait until
3143 * that one quit, too */
3146 case SERVICE_STOP_POST:
3147 case SERVICE_FINAL_SIGTERM:
3148 case SERVICE_FINAL_SIGKILL:
3149 service_enter_dead(s, f, true);
3153 assert_not_reached("Uh, control process died at wrong time.");
3158 /* Notify clients about changed exit status */
3159 unit_add_to_dbus_queue(u);
3162 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3163 Service *s = SERVICE(u);
3166 assert(elapsed == 1);
3168 if (w == &s->watchdog_watch) {
3169 service_handle_watchdog(s);
3173 assert(w == &s->timer_watch);
3177 case SERVICE_START_PRE:
3179 log_warning_unit(u->id,
3180 "%s operation timed out. Terminating.", u->id);
3181 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3184 case SERVICE_START_POST:
3185 log_warning_unit(u->id,
3186 "%s operation timed out. Stopping.", u->id);
3187 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3190 case SERVICE_RELOAD:
3191 log_warning_unit(u->id,
3192 "%s operation timed out. Stopping.", u->id);
3193 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3194 service_enter_running(s, SERVICE_SUCCESS);
3198 log_warning_unit(u->id,
3199 "%s stopping timed out. Terminating.", u->id);
3200 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3203 case SERVICE_STOP_SIGTERM:
3204 if (s->kill_context.send_sigkill) {
3205 log_warning_unit(u->id,
3206 "%s stopping timed out. Killing.", u->id);
3207 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3209 log_warning_unit(u->id,
3210 "%s stopping timed out. Skipping SIGKILL.", u->id);
3211 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3216 case SERVICE_STOP_SIGKILL:
3217 /* Uh, we sent a SIGKILL and it is still not gone?
3218 * Must be something we cannot kill, so let's just be
3219 * weirded out and continue */
3221 log_warning_unit(u->id,
3222 "%s still around after SIGKILL. Ignoring.", u->id);
3223 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3226 case SERVICE_STOP_POST:
3227 log_warning_unit(u->id,
3228 "%s stopping timed out (2). Terminating.", u->id);
3229 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3232 case SERVICE_FINAL_SIGTERM:
3233 if (s->kill_context.send_sigkill) {
3234 log_warning_unit(u->id,
3235 "%s stopping timed out (2). Killing.", u->id);
3236 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3238 log_warning_unit(u->id,
3239 "%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.",
3241 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3246 case SERVICE_FINAL_SIGKILL:
3247 log_warning_unit(u->id,
3248 "%s still around after SIGKILL (2). Entering failed mode.", u->id);
3249 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3252 case SERVICE_AUTO_RESTART:
3253 log_info_unit(u->id,
3254 "%s holdoff time over, scheduling restart.", u->id);
3255 service_enter_restart(s);
3259 assert_not_reached("Timeout at wrong time.");
3263 static void service_cgroup_notify_event(Unit *u) {
3264 Service *s = SERVICE(u);
3268 log_debug_unit(u->id,
3269 "%s: cgroup is empty", u->id);
3273 /* Waiting for SIGCHLD is usually more interesting,
3274 * because it includes return codes/signals. Which is
3275 * why we ignore the cgroup events for most cases,
3276 * except when we don't know pid which to expect the
3280 case SERVICE_START_POST:
3281 /* If we were hoping for the daemon to write its PID file,
3282 * we can give up now. */
3283 if (s->pid_file_pathspec) {
3284 log_warning_unit(u->id,
3285 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3286 service_unwatch_pid_file(s);
3287 if (s->state == SERVICE_START)
3288 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3290 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3294 case SERVICE_RUNNING:
3295 /* service_enter_running() will figure out what to do */
3296 service_enter_running(s, SERVICE_SUCCESS);
3299 case SERVICE_STOP_SIGTERM:
3300 case SERVICE_STOP_SIGKILL:
3302 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3303 service_enter_stop_post(s, SERVICE_SUCCESS);
3307 case SERVICE_FINAL_SIGTERM:
3308 case SERVICE_FINAL_SIGKILL:
3309 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3310 service_enter_dead(s, SERVICE_SUCCESS, true);
3319 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3320 Service *s = SERVICE(u);
3325 if (s->notify_access == NOTIFY_NONE) {
3326 log_warning_unit(u->id,
3327 "%s: Got notification message from PID %lu, but reception is disabled.",
3328 u->id, (unsigned long) pid);
3332 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3333 log_warning_unit(u->id,
3334 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3335 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3339 log_debug_unit(u->id,
3340 "%s: Got message", u->id);
3342 /* Interpret MAINPID= */
3343 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3344 (s->state == SERVICE_START ||
3345 s->state == SERVICE_START_POST ||
3346 s->state == SERVICE_RUNNING ||
3347 s->state == SERVICE_RELOAD)) {
3349 if (parse_pid(e + 8, &pid) < 0)
3350 log_warning_unit(u->id,
3351 "Failed to parse notification message %s", e);
3353 log_debug_unit(u->id,
3354 "%s: got %s", u->id, e);
3355 service_set_main_pid(s, pid);
3359 /* Interpret READY= */
3360 if (s->type == SERVICE_NOTIFY &&
3361 s->state == SERVICE_START &&
3362 strv_find(tags, "READY=1")) {
3363 log_debug_unit(u->id,
3364 "%s: got READY=1", u->id);
3366 service_enter_start_post(s);
3369 /* Interpret STATUS= */
3370 e = strv_find_prefix(tags, "STATUS=");
3376 if (!utf8_is_valid(e+7)) {
3377 log_warning_unit(u->id,
3378 "Status message in notification is not UTF-8 clean.");
3384 log_error_unit(u->id,
3385 "Failed to allocate string.");
3389 log_debug_unit(u->id,
3390 "%s: got %s", u->id, e);
3392 free(s->status_text);
3395 free(s->status_text);
3396 s->status_text = NULL;
3400 if (strv_find(tags, "WATCHDOG=1")) {
3401 log_debug_unit(u->id,
3402 "%s: got WATCHDOG=1", u->id);
3403 service_reset_watchdog(s);
3406 /* Notify clients about changed status or main pid */
3407 unit_add_to_dbus_queue(u);
3410 #ifdef HAVE_SYSV_COMPAT
3412 static int service_enumerate(Manager *m) {
3416 char *path = NULL, *fpath = NULL, *name = NULL;
3417 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3424 if (m->running_as != SYSTEMD_SYSTEM)
3427 zero(runlevel_services);
3429 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3430 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3434 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3443 if (!(d = opendir(path))) {
3444 if (errno != ENOENT)
3445 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3450 while ((de = readdir(d))) {
3453 if (ignore_file(de->d_name))
3456 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3459 if (strlen(de->d_name) < 4)
3462 a = undecchar(de->d_name[1]);
3463 b = undecchar(de->d_name[2]);
3469 fpath = strjoin(path, "/", de->d_name, NULL);
3475 if (access(fpath, X_OK) < 0) {
3477 if (errno != ENOENT)
3478 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3484 if (!(name = sysv_translate_name(de->d_name + 3))) {
3489 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3491 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3495 if (de->d_name[0] == 'S') {
3497 if (rcnd_table[i].type == RUNLEVEL_UP) {
3498 SERVICE(service)->sysv_start_priority_from_rcnd =
3499 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3501 SERVICE(service)->sysv_enabled = true;
3504 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3507 if ((r = set_put(runlevel_services[i], service)) < 0)
3510 } else if (de->d_name[0] == 'K' &&
3511 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3513 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3516 if ((r = set_put(shutdown_services, service)) < 0)
3522 /* Now we loaded all stubs and are aware of the lowest
3523 start-up priority for all services, not let's actually load
3524 the services, this will also tell us which services are
3525 actually native now */
3526 manager_dispatch_load_queue(m);
3528 /* If this is a native service, rely on native ways to pull in
3529 * a service, don't pull it in via sysv rcN.d links. */
3530 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3531 SET_FOREACH(service, runlevel_services[i], j) {
3532 service = unit_follow_merge(service);
3534 if (service->fragment_path)
3537 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3541 /* We honour K links only for halt/reboot. For the normal
3542 * runlevels we assume the stop jobs will be implicitly added
3543 * by the core logic. Also, we don't really distinguish here
3544 * between the runlevels 0 and 6 and just add them to the
3545 * special shutdown target. */
3546 SET_FOREACH(service, shutdown_services, j) {
3547 service = unit_follow_merge(service);
3549 if (service->fragment_path)
3552 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3563 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3564 set_free(runlevel_services[i]);
3565 set_free(shutdown_services);
3574 static void service_bus_name_owner_change(
3577 const char *old_owner,
3578 const char *new_owner) {
3580 Service *s = SERVICE(u);
3585 assert(streq(s->bus_name, name));
3586 assert(old_owner || new_owner);
3588 if (old_owner && new_owner)
3589 log_debug_unit(u->id,
3590 "%s's D-Bus name %s changed owner from %s to %s",
3591 u->id, name, old_owner, new_owner);
3593 log_debug_unit(u->id,
3594 "%s's D-Bus name %s no longer registered by %s",
3595 u->id, name, old_owner);
3597 log_debug_unit(u->id,
3598 "%s's D-Bus name %s now registered by %s",
3599 u->id, name, new_owner);
3601 s->bus_name_good = !!new_owner;
3603 if (s->type == SERVICE_DBUS) {
3605 /* service_enter_running() will figure out what to
3607 if (s->state == SERVICE_RUNNING)
3608 service_enter_running(s, SERVICE_SUCCESS);
3609 else if (s->state == SERVICE_START && new_owner)
3610 service_enter_start_post(s);
3612 } else if (new_owner &&
3614 (s->state == SERVICE_START ||
3615 s->state == SERVICE_START_POST ||
3616 s->state == SERVICE_RUNNING ||
3617 s->state == SERVICE_RELOAD)) {
3619 /* Try to acquire PID from bus service */
3620 log_debug_unit(u->id,
3621 "Trying to acquire PID from D-Bus name...");
3623 bus_query_pid(u->manager, name);
3627 static void service_bus_query_pid_done(
3632 Service *s = SERVICE(u);
3637 log_debug_unit(u->id,
3638 "%s's D-Bus name %s is now owned by process %u",
3639 u->id, name, (unsigned) pid);
3641 if (s->main_pid <= 0 &&
3642 (s->state == SERVICE_START ||
3643 s->state == SERVICE_START_POST ||
3644 s->state == SERVICE_RUNNING ||
3645 s->state == SERVICE_RELOAD))
3646 service_set_main_pid(s, pid);
3649 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3654 /* This is called by the socket code when instantiating a new
3655 * service for a stream socket and the socket needs to be
3658 if (UNIT(s)->load_state != UNIT_LOADED)
3661 if (s->socket_fd >= 0)
3664 if (s->state != SERVICE_DEAD)
3668 s->got_socket_fd = true;
3670 unit_ref_set(&s->accept_socket, UNIT(sock));
3672 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3675 static void service_reset_failed(Unit *u) {
3676 Service *s = SERVICE(u);
3680 if (s->state == SERVICE_FAILED)
3681 service_set_state(s, SERVICE_DEAD);
3683 s->result = SERVICE_SUCCESS;
3684 s->reload_result = SERVICE_SUCCESS;
3686 RATELIMIT_RESET(s->start_limit);
3689 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3690 Service *s = SERVICE(u);
3692 Set *pid_set = NULL;
3696 if (s->main_pid <= 0 && who == KILL_MAIN) {
3697 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3701 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3702 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3706 if (who == KILL_CONTROL || who == KILL_ALL)
3707 if (s->control_pid > 0)
3708 if (kill(s->control_pid, signo) < 0)
3711 if (who == KILL_MAIN || who == KILL_ALL)
3712 if (s->main_pid > 0)
3713 if (kill(s->main_pid, signo) < 0)
3716 if (who == KILL_ALL) {
3719 pid_set = set_new(trivial_hash_func, trivial_compare_func);
3723 /* Exclude the control/main pid from being killed via the cgroup */
3724 if (s->control_pid > 0) {
3725 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
3732 if (s->main_pid > 0) {
3733 q = set_put(pid_set, LONG_TO_PTR(s->main_pid));
3740 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3741 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3752 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3753 [SERVICE_DEAD] = "dead",
3754 [SERVICE_START_PRE] = "start-pre",
3755 [SERVICE_START] = "start",
3756 [SERVICE_START_POST] = "start-post",
3757 [SERVICE_RUNNING] = "running",
3758 [SERVICE_EXITED] = "exited",
3759 [SERVICE_RELOAD] = "reload",
3760 [SERVICE_STOP] = "stop",
3761 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3762 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3763 [SERVICE_STOP_POST] = "stop-post",
3764 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3765 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3766 [SERVICE_FAILED] = "failed",
3767 [SERVICE_AUTO_RESTART] = "auto-restart",
3770 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3772 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3773 [SERVICE_RESTART_NO] = "no",
3774 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3775 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3776 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3777 [SERVICE_RESTART_ALWAYS] = "always"
3780 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3782 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3783 [SERVICE_SIMPLE] = "simple",
3784 [SERVICE_FORKING] = "forking",
3785 [SERVICE_ONESHOT] = "oneshot",
3786 [SERVICE_DBUS] = "dbus",
3787 [SERVICE_NOTIFY] = "notify",
3788 [SERVICE_IDLE] = "idle"
3791 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3793 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3794 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3795 [SERVICE_EXEC_START] = "ExecStart",
3796 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3797 [SERVICE_EXEC_RELOAD] = "ExecReload",
3798 [SERVICE_EXEC_STOP] = "ExecStop",
3799 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3802 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3804 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3805 [NOTIFY_NONE] = "none",
3806 [NOTIFY_MAIN] = "main",
3807 [NOTIFY_ALL] = "all"
3810 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3812 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3813 [SERVICE_SUCCESS] = "success",
3814 [SERVICE_FAILURE_RESOURCES] = "resources",
3815 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3816 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3817 [SERVICE_FAILURE_SIGNAL] = "signal",
3818 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3819 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3820 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3823 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3825 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3826 [SERVICE_START_LIMIT_NONE] = "none",
3827 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3828 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3829 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3831 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3833 const UnitVTable service_vtable = {
3834 .object_size = sizeof(Service),
3835 .exec_context_offset = offsetof(Service, exec_context),
3842 .init = service_init,
3843 .done = service_done,
3844 .load = service_load,
3846 .coldplug = service_coldplug,
3848 .dump = service_dump,
3850 .start = service_start,
3851 .stop = service_stop,
3852 .reload = service_reload,
3854 .can_reload = service_can_reload,
3856 .kill = service_kill,
3858 .serialize = service_serialize,
3859 .deserialize_item = service_deserialize_item,
3861 .active_state = service_active_state,
3862 .sub_state_to_string = service_sub_state_to_string,
3864 .check_gc = service_check_gc,
3865 .check_snapshot = service_check_snapshot,
3867 .sigchld_event = service_sigchld_event,
3868 .timer_event = service_timer_event,
3869 .fd_event = service_fd_event,
3871 .reset_failed = service_reset_failed,
3873 .cgroup_notify_empty = service_cgroup_notify_event,
3874 .notify_message = service_notify_message,
3876 .bus_name_owner_change = service_bus_name_owner_change,
3877 .bus_query_pid_done = service_bus_query_pid_done,
3879 .bus_interface = "org.freedesktop.systemd1.Service",
3880 .bus_message_handler = bus_service_message_handler,
3881 .bus_invalidating_properties = bus_service_invalidating_properties,
3883 #ifdef HAVE_SYSV_COMPAT
3884 .enumerate = service_enumerate,
3886 .status_message_formats = {
3887 .starting_stopping = {
3888 [0] = "Starting %s...",
3889 [1] = "Stopping %s...",
3891 .finished_start_job = {
3892 [JOB_DONE] = "Started %s.",
3893 [JOB_FAILED] = "Failed to start %s.",
3894 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3895 [JOB_TIMEOUT] = "Timed out starting %s.",
3897 .finished_stop_job = {
3898 [JOB_DONE] = "Stopped %s.",
3899 [JOB_FAILED] = "Stopped (with error) %s.",
3900 [JOB_TIMEOUT] = "Timed out stopping %s.",