1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/reboot.h>
31 #include "load-fragment.h"
32 #include "load-dropin.h"
35 #include "unit-name.h"
36 #include "unit-printf.h"
37 #include "dbus-service.h"
39 #include "dbus-common.h"
40 #include "exit-status.h"
42 #include "path-util.h"
48 #ifdef HAVE_SYSV_COMPAT
50 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
52 typedef enum RunlevelType {
60 const RunlevelType type;
62 /* Standard SysV runlevels for start-up */
63 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
64 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
65 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
66 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
67 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
69 /* Standard SysV runlevels for shutdown */
70 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
71 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
73 /* Note that the order here matters, as we read the
74 directories in this order, and we want to make sure that
75 sysv_start_priority is known when we first load the
76 unit. And that value we only know from S links. Hence
77 UP must be read before DOWN */
80 #define RUNLEVELS_UP "12345"
83 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
84 [SERVICE_DEAD] = UNIT_INACTIVE,
85 [SERVICE_START_PRE] = UNIT_ACTIVATING,
86 [SERVICE_START] = UNIT_ACTIVATING,
87 [SERVICE_START_POST] = UNIT_ACTIVATING,
88 [SERVICE_RUNNING] = UNIT_ACTIVE,
89 [SERVICE_EXITED] = UNIT_ACTIVE,
90 [SERVICE_RELOAD] = UNIT_RELOADING,
91 [SERVICE_STOP] = UNIT_DEACTIVATING,
92 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
93 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
94 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
95 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
96 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
97 [SERVICE_FAILED] = UNIT_FAILED,
98 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
101 /* For Type=idle we never want to delay any other jobs, hence we
102 * consider idle jobs active as soon as we start working on them */
103 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
104 [SERVICE_DEAD] = UNIT_INACTIVE,
105 [SERVICE_START_PRE] = UNIT_ACTIVE,
106 [SERVICE_START] = UNIT_ACTIVE,
107 [SERVICE_START_POST] = UNIT_ACTIVE,
108 [SERVICE_RUNNING] = UNIT_ACTIVE,
109 [SERVICE_EXITED] = UNIT_ACTIVE,
110 [SERVICE_RELOAD] = UNIT_RELOADING,
111 [SERVICE_STOP] = UNIT_DEACTIVATING,
112 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
113 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
114 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
115 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
116 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
117 [SERVICE_FAILED] = UNIT_FAILED,
118 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
121 static void service_init(Unit *u) {
122 Service *s = SERVICE(u);
125 assert(u->load_state == UNIT_STUB);
127 s->timeout_start_usec = DEFAULT_TIMEOUT_USEC;
128 s->timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
129 s->restart_usec = DEFAULT_RESTART_USEC;
130 s->type = _SERVICE_TYPE_INVALID;
132 watch_init(&s->watchdog_watch);
133 watch_init(&s->timer_watch);
135 #ifdef HAVE_SYSV_COMPAT
136 s->sysv_start_priority = -1;
137 s->sysv_start_priority_from_rcnd = -1;
140 s->guess_main_pid = true;
142 exec_context_init(&s->exec_context);
143 kill_context_init(&s->kill_context);
144 cgroup_context_init(&s->cgroup_context);
146 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
148 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
151 static void service_unwatch_control_pid(Service *s) {
154 if (s->control_pid <= 0)
157 unit_unwatch_pid(UNIT(s), s->control_pid);
161 static void service_unwatch_main_pid(Service *s) {
164 if (s->main_pid <= 0)
167 unit_unwatch_pid(UNIT(s), s->main_pid);
171 static void service_unwatch_pid_file(Service *s) {
172 if (!s->pid_file_pathspec)
175 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
176 UNIT(s)->id, s->pid_file_pathspec->path);
177 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
178 path_spec_done(s->pid_file_pathspec);
179 free(s->pid_file_pathspec);
180 s->pid_file_pathspec = NULL;
183 static int service_set_main_pid(Service *s, pid_t pid) {
194 service_unwatch_main_pid(s);
197 s->main_pid_known = true;
199 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
200 log_warning_unit(UNIT(s)->id,
201 "%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
202 UNIT(s)->id, (unsigned long) pid);
204 s->main_pid_alien = true;
206 s->main_pid_alien = false;
208 exec_status_start(&s->main_exec_status, pid);
213 static void service_close_socket_fd(Service *s) {
216 if (s->socket_fd < 0)
219 close_nointr_nofail(s->socket_fd);
223 static void service_connection_unref(Service *s) {
226 if (!UNIT_ISSET(s->accept_socket))
229 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
230 unit_ref_unset(&s->accept_socket);
233 static void service_stop_watchdog(Service *s) {
236 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
237 s->watchdog_timestamp.realtime = 0;
238 s->watchdog_timestamp.monotonic = 0;
241 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
243 static void service_handle_watchdog(Service *s) {
249 if (s->watchdog_usec == 0)
252 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
253 if (offset >= s->watchdog_usec) {
254 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
255 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_WATCHDOG);
259 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->watchdog_usec - offset, &s->watchdog_watch);
261 log_warning_unit(UNIT(s)->id,
262 "%s failed to install watchdog timer: %s",
263 UNIT(s)->id, strerror(-r));
266 static void service_reset_watchdog(Service *s) {
269 dual_timestamp_get(&s->watchdog_timestamp);
270 service_handle_watchdog(s);
273 static void service_done(Unit *u) {
274 Service *s = SERVICE(u);
281 #ifdef HAVE_SYSV_COMPAT
282 free(s->sysv_runlevels);
283 s->sysv_runlevels = NULL;
286 free(s->status_text);
287 s->status_text = NULL;
289 cgroup_context_done(&s->cgroup_context);
290 exec_context_done(&s->exec_context, manager_is_reloading_or_reexecuting(u->manager));
291 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
292 s->control_command = NULL;
293 s->main_command = NULL;
295 set_free(s->restart_ignore_status.code);
296 s->restart_ignore_status.code = NULL;
297 set_free(s->restart_ignore_status.signal);
298 s->restart_ignore_status.signal = NULL;
300 set_free(s->success_status.code);
301 s->success_status.code = NULL;
302 set_free(s->success_status.signal);
303 s->success_status.signal = NULL;
305 /* This will leak a process, but at least no memory or any of
307 service_unwatch_main_pid(s);
308 service_unwatch_control_pid(s);
309 service_unwatch_pid_file(s);
312 unit_unwatch_bus_name(u, s->bus_name);
317 service_close_socket_fd(s);
318 service_connection_unref(s);
320 unit_ref_unset(&s->accept_socket);
322 service_stop_watchdog(s);
324 unit_unwatch_timer(u, &s->timer_watch);
327 #ifdef HAVE_SYSV_COMPAT
328 static char *sysv_translate_name(const char *name) {
331 r = new(char, strlen(name) + sizeof(".service"));
335 if (endswith(name, ".sh"))
336 /* Drop .sh suffix */
337 strcpy(stpcpy(r, name) - 3, ".service");
339 /* Normal init script name */
340 strcpy(stpcpy(r, name), ".service");
345 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
347 /* We silently ignore the $ prefix here. According to the LSB
348 * spec it simply indicates whether something is a
349 * standardized name or a distribution-specific one. Since we
350 * just follow what already exists and do not introduce new
351 * uses or names we don't care who introduced a new name. */
353 static const char * const table[] = {
354 /* LSB defined facilities */
356 "network", SPECIAL_NETWORK_TARGET,
357 "named", SPECIAL_NSS_LOOKUP_TARGET,
358 "portmap", SPECIAL_RPCBIND_TARGET,
359 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
361 "time", SPECIAL_TIME_SYNC_TARGET,
371 n = *name == '$' ? name + 1 : name;
373 for (i = 0; i < ELEMENTSOF(table); i += 2) {
375 if (!streq(table[i], n))
381 r = strdup(table[i+1]);
388 /* If we don't know this name, fallback heuristics to figure
389 * out whether something is a target or a service alias. */
392 if (!unit_prefix_is_valid(n))
395 /* Facilities starting with $ are most likely targets */
396 r = unit_name_build(n, NULL, ".target");
397 } else if (filename && streq(name, filename))
398 /* Names equaling the file name of the services are redundant */
401 /* Everything else we assume to be normal service names */
402 r = sysv_translate_name(n);
413 static int sysv_fix_order(Service *s) {
419 if (s->sysv_start_priority < 0)
422 /* For each pair of services where at least one lacks a LSB
423 * header, we use the start priority value to order things. */
425 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
428 bool special_s, special_t;
435 if (UNIT(t)->load_state != UNIT_LOADED)
438 if (t->sysv_start_priority < 0)
441 /* If both units have modern headers we don't care
442 * about the priorities */
443 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
444 (UNIT(t)->fragment_path || t->sysv_has_lsb))
447 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
448 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
450 if (special_t && !special_s)
452 else if (special_s && !special_t)
454 else if (t->sysv_start_priority < s->sysv_start_priority)
456 else if (t->sysv_start_priority > s->sysv_start_priority)
461 /* FIXME: Maybe we should compare the name here lexicographically? */
463 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
470 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
473 if (!(c = new0(ExecCommand, 1)))
476 if (!(c->path = strdup(path))) {
481 if (!(c->argv = strv_new(path, arg1, NULL))) {
490 static int sysv_exec_commands(Service *s, const bool supports_reload) {
495 assert(UNIT(s)->source_path);
497 c = exec_command_new(UNIT(s)->source_path, "start");
500 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
502 c = exec_command_new(UNIT(s)->source_path, "stop");
505 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
507 if (supports_reload) {
508 c = exec_command_new(UNIT(s)->source_path, "reload");
511 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
517 static bool usage_contains_reload(const char *line) {
518 return (strcasestr(line, "{reload|") ||
519 strcasestr(line, "{reload}") ||
520 strcasestr(line, "{reload\"") ||
521 strcasestr(line, "|reload|") ||
522 strcasestr(line, "|reload}") ||
523 strcasestr(line, "|reload\""));
526 static int service_load_sysv_path(Service *s, const char *path) {
538 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
540 bool supports_reload = false;
547 f = fopen(path, "re");
549 r = errno == ENOENT ? 0 : -errno;
553 if (fstat(fileno(f), &st) < 0) {
558 free(u->source_path);
559 u->source_path = strdup(path);
560 if (!u->source_path) {
564 u->source_mtime = timespec_load(&st.st_mtim);
566 if (null_or_empty(&st)) {
567 u->load_state = UNIT_MASKED;
575 char l[LINE_MAX], *t;
577 if (!fgets(l, sizeof(l), f)) {
582 log_error_unit(u->id,
583 "Failed to read configuration file '%s': %s",
592 /* Try to figure out whether this init script supports
593 * the reload operation. This heuristic looks for
594 * "Usage" lines which include the reload option. */
595 if ( state == USAGE_CONTINUATION ||
596 (state == NORMAL && strcasestr(t, "usage"))) {
597 if (usage_contains_reload(t)) {
598 supports_reload = true;
600 } else if (t[strlen(t)-1] == '\\')
601 state = USAGE_CONTINUATION;
609 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
611 s->sysv_has_lsb = true;
615 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
621 t += strspn(t, WHITESPACE);
623 if (state == NORMAL) {
625 /* Try to parse Red Hat style chkconfig headers */
627 if (startswith_no_case(t, "chkconfig:")) {
629 char runlevels[16], *k;
633 if (sscanf(t+10, "%15s %i %*i",
635 &start_priority) != 2) {
637 log_warning_unit(u->id,
638 "[%s:%u] Failed to parse chkconfig line. Ignoring.",
643 /* A start priority gathered from the
644 * symlink farms is preferred over the
645 * data from the LSB header. */
646 if (start_priority < 0 || start_priority > 99)
647 log_warning_unit(u->id,
648 "[%s:%u] Start priority out of range. Ignoring.",
651 s->sysv_start_priority = start_priority;
653 char_array_0(runlevels);
654 k = delete_chars(runlevels, WHITESPACE "-");
659 if (!(d = strdup(k))) {
664 free(s->sysv_runlevels);
665 s->sysv_runlevels = d;
668 } else if (startswith_no_case(t, "description:")) {
670 size_t k = strlen(t);
674 if (t[k-1] == '\\') {
679 if ((j = strstrip(t+12)) && *j) {
680 if (!(d = strdup(j))) {
687 free(chkconfig_description);
688 chkconfig_description = d;
690 } else if (startswith_no_case(t, "pidfile:")) {
697 if (!path_is_absolute(fn)) {
698 log_warning_unit(u->id,
699 "[%s:%u] PID file not absolute. Ignoring.",
704 if (!(fn = strdup(fn))) {
713 } else if (state == DESCRIPTION) {
715 /* Try to parse Red Hat style description
718 size_t k = strlen(t);
726 if ((j = strstrip(t)) && *j) {
729 if (chkconfig_description)
730 d = strjoin(chkconfig_description, " ", j, NULL);
739 free(chkconfig_description);
740 chkconfig_description = d;
743 } else if (state == LSB || state == LSB_DESCRIPTION) {
745 if (startswith_no_case(t, "Provides:")) {
751 FOREACH_WORD_QUOTED(w, z, t+9, i) {
754 if (!(n = strndup(w, z))) {
759 r = sysv_translate_facility(n, path_get_file_name(path), &m);
768 if (unit_name_to_type(m) == UNIT_SERVICE)
769 r = unit_merge_by_name(u, m);
776 * indication that the
778 * now available. This
781 * targets do NOT pull
784 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
787 log_error_unit(u->id,
788 "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
789 path, line, m, strerror(-r));
794 } else if (startswith_no_case(t, "Required-Start:") ||
795 startswith_no_case(t, "Should-Start:") ||
796 startswith_no_case(t, "X-Start-Before:") ||
797 startswith_no_case(t, "X-Start-After:")) {
803 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
806 if (!(n = strndup(w, z))) {
811 r = sysv_translate_facility(n, path_get_file_name(path), &m);
813 log_error_unit(u->id,
814 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
815 path, line, n, strerror(-r));
825 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
828 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
829 path, line, m, strerror(-r));
833 } else if (startswith_no_case(t, "Default-Start:")) {
838 k = delete_chars(t+14, WHITESPACE "-");
841 if (!(d = strdup(k))) {
846 free(s->sysv_runlevels);
847 s->sysv_runlevels = d;
850 } else if (startswith_no_case(t, "Description:")) {
853 state = LSB_DESCRIPTION;
855 if ((j = strstrip(t+12)) && *j) {
856 if (!(d = strdup(j))) {
863 free(long_description);
864 long_description = d;
866 } else if (startswith_no_case(t, "Short-Description:")) {
871 if ((j = strstrip(t+18)) && *j) {
872 if (!(d = strdup(j))) {
879 free(short_description);
880 short_description = d;
882 } else if (state == LSB_DESCRIPTION) {
884 if (startswith(l, "#\t") || startswith(l, "# ")) {
887 if ((j = strstrip(t)) && *j) {
890 if (long_description)
891 d = strjoin(long_description, " ", t, NULL);
900 free(long_description);
901 long_description = d;
910 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
913 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
914 /* If there a runlevels configured for this service
915 * but none of the standard ones, then we assume this
916 * is some special kind of service (which might be
917 * needed for early boot) and don't create any links
920 UNIT(s)->default_dependencies = false;
922 /* Don't timeout special services during boot (like fsck) */
923 s->timeout_start_usec = 0;
924 s->timeout_stop_usec = 0;
926 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
927 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
930 /* Special setting for all SysV services */
931 s->type = SERVICE_FORKING;
932 s->remain_after_exit = !s->pid_file;
933 s->guess_main_pid = false;
934 s->restart = SERVICE_RESTART_NO;
935 s->exec_context.ignore_sigpipe = false;
936 s->kill_context.kill_mode = KILL_PROCESS;
938 /* We use the long description only if
939 * no short description is set. */
941 if (short_description)
942 description = short_description;
943 else if (chkconfig_description)
944 description = chkconfig_description;
945 else if (long_description)
946 description = long_description;
953 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
961 /* The priority that has been set in /etc/rcN.d/ hierarchies
962 * takes precedence over what is stored as default in the LSB
964 if (s->sysv_start_priority_from_rcnd >= 0)
965 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
967 u->load_state = UNIT_LOADED;
975 free(short_description);
976 free(long_description);
977 free(chkconfig_description);
982 static int service_load_sysv_name(Service *s, const char *name) {
988 /* For SysV services we strip the *.sh suffixes. */
989 if (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);
1015 if (UNIT(s)->load_state != UNIT_STUB)
1022 static int service_load_sysv(Service *s) {
1029 /* Load service data from SysV init scripts, preferably with
1030 * LSB headers ... */
1032 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1035 if ((t = UNIT(s)->id))
1036 if ((r = service_load_sysv_name(s, t)) < 0)
1039 if (UNIT(s)->load_state == UNIT_STUB)
1040 SET_FOREACH(t, UNIT(s)->names, i) {
1041 if (t == UNIT(s)->id)
1044 if ((r = service_load_sysv_name(s, t)) < 0)
1047 if (UNIT(s)->load_state != UNIT_STUB)
1055 static int fsck_fix_order(Service *s) {
1061 if (s->fsck_passno <= 0)
1064 /* For each pair of services where both have an fsck priority
1065 * we order things based on it. */
1067 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1076 if (UNIT(t)->load_state != UNIT_LOADED)
1079 if (t->fsck_passno <= 0)
1082 if (t->fsck_passno < s->fsck_passno)
1084 else if (t->fsck_passno > s->fsck_passno)
1089 r = unit_add_dependency(UNIT(s), d, UNIT(t), true);
1097 static int service_verify(Service *s) {
1100 if (UNIT(s)->load_state != UNIT_LOADED)
1103 if (!s->exec_command[SERVICE_EXEC_START]) {
1104 log_error_unit(UNIT(s)->id,
1105 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1109 if (s->type != SERVICE_ONESHOT &&
1110 s->exec_command[SERVICE_EXEC_START]->command_next) {
1111 log_error_unit(UNIT(s)->id,
1112 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1116 if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
1117 log_error_unit(UNIT(s)->id,
1118 "%s has Restart setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1122 if (s->type == SERVICE_DBUS && !s->bus_name) {
1123 log_error_unit(UNIT(s)->id,
1124 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1128 if (s->bus_name && s->type != SERVICE_DBUS)
1129 log_warning_unit(UNIT(s)->id,
1130 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1132 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1133 log_error_unit(UNIT(s)->id,
1134 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1141 static int service_add_default_dependencies(Service *s) {
1146 /* Add a number of automatic dependencies useful for the
1147 * majority of services. */
1149 /* First, pull in base system */
1150 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1151 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1152 SPECIAL_BASIC_TARGET, NULL, true);
1156 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1157 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1158 SPECIAL_SOCKETS_TARGET, NULL, true);
1162 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1163 SPECIAL_TIMERS_TARGET, NULL, true);
1167 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1168 SPECIAL_PATHS_TARGET, NULL, true);
1173 /* Second, activate normal shutdown */
1174 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
1175 SPECIAL_SHUTDOWN_TARGET, NULL, true);
1179 static void service_fix_output(Service *s) {
1182 /* If nothing has been explicitly configured, patch default
1183 * output in. If input is socket/tty we avoid this however,
1184 * since in that case we want output to default to the same
1185 * place as we read input from. */
1187 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1188 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1189 s->exec_context.std_input == EXEC_INPUT_NULL)
1190 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1192 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1193 s->exec_context.std_input == EXEC_INPUT_NULL)
1194 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1197 static int service_load(Unit *u) {
1199 Service *s = SERVICE(u);
1203 /* Load a .service file */
1204 r = unit_load_fragment(u);
1208 #ifdef HAVE_SYSV_COMPAT
1209 /* Load a classic init script as a fallback, if we couldn't find anything */
1210 if (u->load_state == UNIT_STUB) {
1211 r = service_load_sysv(s);
1217 /* Still nothing found? Then let's give up */
1218 if (u->load_state == UNIT_STUB)
1221 /* This is a new unit? Then let's add in some extras */
1222 if (u->load_state == UNIT_LOADED) {
1224 /* We were able to load something, then let's add in
1225 * the dropin directories. */
1226 r = unit_load_dropin(u);
1230 if (s->type == _SERVICE_TYPE_INVALID)
1231 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1233 /* Oneshot services have disabled start timeout by default */
1234 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1235 s->timeout_start_usec = 0;
1237 service_fix_output(s);
1239 r = unit_add_exec_dependencies(u, &s->exec_context);
1243 r = unit_add_default_slice(u);
1247 #ifdef HAVE_SYSV_COMPAT
1248 r = sysv_fix_order(s);
1253 r = fsck_fix_order(s);
1258 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1261 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1262 s->notify_access = NOTIFY_MAIN;
1264 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1265 s->notify_access = NOTIFY_MAIN;
1267 if (s->type == SERVICE_DBUS || s->bus_name) {
1268 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES,
1269 SPECIAL_DBUS_SOCKET, NULL, true);
1274 if (UNIT(s)->default_dependencies) {
1275 r = service_add_default_dependencies(s);
1280 r = unit_exec_context_defaults(u, &s->exec_context);
1285 return service_verify(s);
1288 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1290 ServiceExecCommand c;
1291 Service *s = SERVICE(u);
1292 const char *prefix2;
1293 _cleanup_free_ char *p2 = NULL;
1297 p2 = strappend(prefix, "\t");
1298 prefix2 = p2 ? p2 : prefix;
1301 "%sService State: %s\n"
1303 "%sReload Result: %s\n"
1304 "%sPermissionsStartOnly: %s\n"
1305 "%sRootDirectoryStartOnly: %s\n"
1306 "%sRemainAfterExit: %s\n"
1307 "%sGuessMainPID: %s\n"
1310 "%sNotifyAccess: %s\n",
1311 prefix, service_state_to_string(s->state),
1312 prefix, service_result_to_string(s->result),
1313 prefix, service_result_to_string(s->reload_result),
1314 prefix, yes_no(s->permissions_start_only),
1315 prefix, yes_no(s->root_directory_start_only),
1316 prefix, yes_no(s->remain_after_exit),
1317 prefix, yes_no(s->guess_main_pid),
1318 prefix, service_type_to_string(s->type),
1319 prefix, service_restart_to_string(s->restart),
1320 prefix, notify_access_to_string(s->notify_access));
1322 if (s->control_pid > 0)
1324 "%sControl PID: %lu\n",
1325 prefix, (unsigned long) s->control_pid);
1327 if (s->main_pid > 0)
1330 "%sMain PID Known: %s\n"
1331 "%sMain PID Alien: %s\n",
1332 prefix, (unsigned long) s->main_pid,
1333 prefix, yes_no(s->main_pid_known),
1334 prefix, yes_no(s->main_pid_alien));
1339 prefix, s->pid_file);
1344 "%sBus Name Good: %s\n",
1345 prefix, s->bus_name,
1346 prefix, yes_no(s->bus_name_good));
1348 kill_context_dump(&s->kill_context, f, prefix);
1349 exec_context_dump(&s->exec_context, f, prefix);
1351 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1353 if (!s->exec_command[c])
1356 fprintf(f, "%s-> %s:\n",
1357 prefix, service_exec_command_to_string(c));
1359 exec_command_dump_list(s->exec_command[c], f, prefix2);
1362 #ifdef HAVE_SYSV_COMPAT
1365 "%sSysV Init Script has LSB Header: %s\n"
1366 "%sSysVEnabled: %s\n",
1367 prefix, yes_no(s->sysv_has_lsb),
1368 prefix, yes_no(s->sysv_enabled));
1370 if (s->sysv_start_priority >= 0)
1372 "%sSysVStartPriority: %i\n",
1373 prefix, s->sysv_start_priority);
1375 if (s->sysv_runlevels)
1376 fprintf(f, "%sSysVRunLevels: %s\n",
1377 prefix, s->sysv_runlevels);
1380 if (s->fsck_passno > 0)
1382 "%sFsckPassNo: %i\n",
1383 prefix, s->fsck_passno);
1386 fprintf(f, "%sStatus Text: %s\n",
1387 prefix, s->status_text);
1390 static int service_load_pid_file(Service *s, bool may_warn) {
1391 _cleanup_free_ char *k = NULL;
1400 r = read_one_line_file(s->pid_file, &k);
1403 log_info_unit(UNIT(s)->id,
1404 "PID file %s not readable (yet?) after %s.",
1405 s->pid_file, service_state_to_string(s->state));
1409 r = parse_pid(k, &pid);
1412 log_info_unit(UNIT(s)->id,
1413 "Failed to read PID from file %s: %s",
1414 s->pid_file, strerror(-r));
1418 if (kill(pid, 0) < 0 && errno != EPERM) {
1420 log_info_unit(UNIT(s)->id,
1421 "PID %lu read from file %s does not exist.",
1422 (unsigned long) pid, s->pid_file);
1426 if (s->main_pid_known) {
1427 if (pid == s->main_pid)
1430 log_debug_unit(UNIT(s)->id,
1431 "Main PID changing: %lu -> %lu",
1432 (unsigned long) s->main_pid, (unsigned long) pid);
1433 service_unwatch_main_pid(s);
1434 s->main_pid_known = false;
1436 log_debug_unit(UNIT(s)->id,
1437 "Main PID loaded: %lu", (unsigned long) pid);
1439 r = service_set_main_pid(s, pid);
1443 r = unit_watch_pid(UNIT(s), pid);
1445 /* FIXME: we need to do something here */
1446 log_warning_unit(UNIT(s)->id,
1447 "Failed to watch PID %lu from service %s",
1448 (unsigned long) pid, UNIT(s)->id);
1455 static int service_search_main_pid(Service *s) {
1461 /* If we know it anyway, don't ever fallback to unreliable
1463 if (s->main_pid_known)
1466 if (!s->guess_main_pid)
1469 assert(s->main_pid <= 0);
1471 pid = unit_search_main_pid(UNIT(s));
1475 log_debug_unit(UNIT(s)->id,
1476 "Main PID guessed: %lu", (unsigned long) pid);
1477 r = service_set_main_pid(s, pid);
1481 r = unit_watch_pid(UNIT(s), pid);
1483 /* FIXME: we need to do something here */
1484 log_warning_unit(UNIT(s)->id,
1485 "Failed to watch PID %lu from service %s",
1486 (unsigned long) pid, UNIT(s)->id);
1492 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1498 /* Notifies all our sockets when we die */
1500 if (s->socket_fd >= 0)
1503 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1504 if (u->type == UNIT_SOCKET)
1505 socket_notify_service_dead(SOCKET(u), failed_permanent);
1510 static void service_set_state(Service *s, ServiceState state) {
1511 ServiceState old_state;
1512 const UnitActiveState *table;
1515 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1517 old_state = s->state;
1520 service_unwatch_pid_file(s);
1522 if (state != SERVICE_START_PRE &&
1523 state != SERVICE_START &&
1524 state != SERVICE_START_POST &&
1525 state != SERVICE_RELOAD &&
1526 state != SERVICE_STOP &&
1527 state != SERVICE_STOP_SIGTERM &&
1528 state != SERVICE_STOP_SIGKILL &&
1529 state != SERVICE_STOP_POST &&
1530 state != SERVICE_FINAL_SIGTERM &&
1531 state != SERVICE_FINAL_SIGKILL &&
1532 state != SERVICE_AUTO_RESTART)
1533 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1535 if (state != SERVICE_START &&
1536 state != SERVICE_START_POST &&
1537 state != SERVICE_RUNNING &&
1538 state != SERVICE_RELOAD &&
1539 state != SERVICE_STOP &&
1540 state != SERVICE_STOP_SIGTERM &&
1541 state != SERVICE_STOP_SIGKILL) {
1542 service_unwatch_main_pid(s);
1543 s->main_command = NULL;
1546 if (state != SERVICE_START_PRE &&
1547 state != SERVICE_START &&
1548 state != SERVICE_START_POST &&
1549 state != SERVICE_RELOAD &&
1550 state != SERVICE_STOP &&
1551 state != SERVICE_STOP_SIGTERM &&
1552 state != SERVICE_STOP_SIGKILL &&
1553 state != SERVICE_STOP_POST &&
1554 state != SERVICE_FINAL_SIGTERM &&
1555 state != SERVICE_FINAL_SIGKILL) {
1556 service_unwatch_control_pid(s);
1557 s->control_command = NULL;
1558 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1561 if (state == SERVICE_FAILED)
1562 service_notify_sockets_dead(s, s->result == SERVICE_FAILURE_START_LIMIT);
1564 if (state == SERVICE_DEAD ||
1565 state == SERVICE_STOP ||
1566 state == SERVICE_STOP_SIGTERM ||
1567 state == SERVICE_STOP_SIGKILL ||
1568 state == SERVICE_STOP_POST ||
1569 state == SERVICE_FINAL_SIGTERM ||
1570 state == SERVICE_FINAL_SIGKILL ||
1571 state == SERVICE_AUTO_RESTART)
1572 service_notify_sockets_dead(s, false);
1574 if (state != SERVICE_START_PRE &&
1575 state != SERVICE_START &&
1576 state != SERVICE_START_POST &&
1577 state != SERVICE_RUNNING &&
1578 state != SERVICE_RELOAD &&
1579 state != SERVICE_STOP &&
1580 state != SERVICE_STOP_SIGTERM &&
1581 state != SERVICE_STOP_SIGKILL &&
1582 state != SERVICE_STOP_POST &&
1583 state != SERVICE_FINAL_SIGTERM &&
1584 state != SERVICE_FINAL_SIGKILL &&
1585 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1586 service_close_socket_fd(s);
1587 service_connection_unref(s);
1590 if (state == SERVICE_STOP || state == SERVICE_STOP_SIGTERM)
1591 service_stop_watchdog(s);
1593 /* For the inactive states unit_notify() will trim the cgroup,
1594 * but for exit we have to do that ourselves... */
1595 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1596 unit_destroy_cgroup(UNIT(s));
1598 if (old_state != state)
1599 log_debug_unit(UNIT(s)->id,
1600 "%s changed %s -> %s", UNIT(s)->id,
1601 service_state_to_string(old_state),
1602 service_state_to_string(state));
1604 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1605 s->reload_result = SERVICE_SUCCESS;
1608 static int service_coldplug(Unit *u) {
1609 Service *s = SERVICE(u);
1613 assert(s->state == SERVICE_DEAD);
1615 if (s->deserialized_state != s->state) {
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 s->deserialized_state == SERVICE_AUTO_RESTART) {
1629 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1632 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1634 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
1640 if ((s->deserialized_state == SERVICE_START &&
1641 (s->type == SERVICE_FORKING ||
1642 s->type == SERVICE_DBUS ||
1643 s->type == SERVICE_ONESHOT ||
1644 s->type == SERVICE_NOTIFY)) ||
1645 s->deserialized_state == SERVICE_START_POST ||
1646 s->deserialized_state == SERVICE_RUNNING ||
1647 s->deserialized_state == SERVICE_RELOAD ||
1648 s->deserialized_state == SERVICE_STOP ||
1649 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1650 s->deserialized_state == SERVICE_STOP_SIGKILL)
1651 if (s->main_pid > 0) {
1652 r = unit_watch_pid(UNIT(s), s->main_pid);
1657 if (s->deserialized_state == SERVICE_START_PRE ||
1658 s->deserialized_state == SERVICE_START ||
1659 s->deserialized_state == SERVICE_START_POST ||
1660 s->deserialized_state == SERVICE_RELOAD ||
1661 s->deserialized_state == SERVICE_STOP ||
1662 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1663 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1664 s->deserialized_state == SERVICE_STOP_POST ||
1665 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1666 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1667 if (s->control_pid > 0) {
1668 r = unit_watch_pid(UNIT(s), s->control_pid);
1673 if (s->deserialized_state == SERVICE_START_POST ||
1674 s->deserialized_state == SERVICE_RUNNING)
1675 service_handle_watchdog(s);
1677 service_set_state(s, s->deserialized_state);
1682 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1686 unsigned rn_fds = 0;
1693 if (s->socket_fd >= 0)
1696 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1701 if (u->type != UNIT_SOCKET)
1706 r = socket_collect_fds(sock, &cfds, &cn_fds);
1719 t = new(int, rn_fds+cn_fds);
1726 memcpy(t, rfds, rn_fds * sizeof(int));
1727 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1732 rn_fds = rn_fds+cn_fds;
1747 static int service_spawn(
1752 bool apply_permissions,
1754 bool apply_tty_stdin,
1755 bool set_notify_socket,
1762 _cleanup_free_ int *fdsbuf = NULL;
1763 unsigned n_fds = 0, n_env = 0;
1764 _cleanup_strv_free_ char
1765 **argv = NULL, **final_env = NULL, **our_env = NULL;
1772 unit_realize_cgroup(UNIT(s));
1775 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1776 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1777 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1779 if (s->socket_fd >= 0) {
1780 fds = &s->socket_fd;
1783 r = service_collect_fds(s, &fdsbuf, &n_fds);
1791 if (timeout && s->timeout_start_usec) {
1792 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true,
1793 s->timeout_start_usec, &s->timer_watch);
1797 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1799 argv = unit_full_printf_strv(UNIT(s), c->argv);
1805 our_env = new0(char*, 5);
1811 if (set_notify_socket)
1812 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1817 if (s->main_pid > 0)
1818 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1823 if (s->watchdog_usec > 0)
1824 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1829 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
1830 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1835 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1841 if (is_control && UNIT(s)->cgroup_path) {
1842 path = strappenda(UNIT(s)->cgroup_path, "/control");
1843 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1845 path = UNIT(s)->cgroup_path;
1855 UNIT(s)->manager->confirm_spawn,
1856 UNIT(s)->cgroup_mask,
1859 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1864 r = unit_watch_pid(UNIT(s), pid);
1866 /* FIXME: we need to do something here */
1875 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1880 static int main_pid_good(Service *s) {
1883 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1886 /* If we know the pid file, then lets just check if it is
1888 if (s->main_pid_known) {
1890 /* If it's an alien child let's check if it is still
1892 if (s->main_pid_alien && s->main_pid > 0)
1893 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1895 /* .. otherwise assume we'll get a SIGCHLD for it,
1896 * which we really should wait for to collect exit
1897 * status and code */
1898 return s->main_pid > 0;
1901 /* We don't know the pid */
1905 _pure_ static int control_pid_good(Service *s) {
1908 return s->control_pid > 0;
1911 static int cgroup_good(Service *s) {
1916 if (!UNIT(s)->cgroup_path)
1919 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1926 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1930 if (f != SERVICE_SUCCESS)
1933 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1935 if (allow_restart &&
1936 !s->forbid_restart &&
1937 (s->restart == SERVICE_RESTART_ALWAYS ||
1938 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1939 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1940 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1941 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1942 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1943 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1944 (s->result != SERVICE_FAILURE_SIGNAL ||
1945 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1948 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
1952 service_set_state(s, SERVICE_AUTO_RESTART);
1955 s->forbid_restart = false;
1957 /* we want fresh tmpdirs in case service is started again immediately */
1958 exec_context_tmp_dirs_done(&s->exec_context);
1960 /* Try to delete the pid file. At this point it will be
1961 * out-of-date, and some software might be confused by it, so
1962 * let's remove it. */
1964 unlink_noerrno(s->pid_file);
1969 log_warning_unit(UNIT(s)->id,
1970 "%s failed to run install restart timer: %s",
1971 UNIT(s)->id, strerror(-r));
1972 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1975 static void service_enter_stop_post(Service *s, ServiceResult f) {
1979 if (f != SERVICE_SUCCESS)
1982 service_unwatch_control_pid(s);
1984 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1985 if (s->control_command) {
1986 s->control_command_id = SERVICE_EXEC_STOP_POST;
1988 r = service_spawn(s,
1992 !s->permissions_start_only,
1993 !s->root_directory_start_only,
2002 service_set_state(s, SERVICE_STOP_POST);
2004 service_enter_dead(s, SERVICE_SUCCESS, true);
2009 log_warning_unit(UNIT(s)->id,
2010 "%s failed to run 'stop-post' task: %s",
2011 UNIT(s)->id, strerror(-r));
2012 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2015 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
2020 if (f != SERVICE_SUCCESS)
2023 r = unit_kill_context(
2026 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
2034 if (s->timeout_stop_usec > 0) {
2035 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true,
2036 s->timeout_stop_usec, &s->timer_watch);
2041 service_set_state(s, state);
2042 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2043 service_enter_stop_post(s, SERVICE_SUCCESS);
2045 service_enter_dead(s, SERVICE_SUCCESS, true);
2050 log_warning_unit(UNIT(s)->id,
2051 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2053 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2054 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2056 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2059 static void service_enter_stop(Service *s, ServiceResult f) {
2064 if (f != SERVICE_SUCCESS)
2067 service_unwatch_control_pid(s);
2069 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
2070 if (s->control_command) {
2071 s->control_command_id = SERVICE_EXEC_STOP;
2073 r = service_spawn(s,
2077 !s->permissions_start_only,
2078 !s->root_directory_start_only,
2086 service_set_state(s, SERVICE_STOP);
2088 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2093 log_warning_unit(UNIT(s)->id,
2094 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2095 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2098 static void service_enter_running(Service *s, ServiceResult f) {
2099 int main_pid_ok, cgroup_ok;
2102 if (f != SERVICE_SUCCESS)
2105 main_pid_ok = main_pid_good(s);
2106 cgroup_ok = cgroup_good(s);
2108 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2109 (s->bus_name_good || s->type != SERVICE_DBUS))
2110 service_set_state(s, SERVICE_RUNNING);
2111 else if (s->remain_after_exit)
2112 service_set_state(s, SERVICE_EXITED);
2114 service_enter_stop(s, SERVICE_SUCCESS);
2117 static void service_enter_start_post(Service *s) {
2121 service_unwatch_control_pid(s);
2123 if (s->watchdog_usec > 0)
2124 service_reset_watchdog(s);
2126 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
2127 if (s->control_command) {
2128 s->control_command_id = SERVICE_EXEC_START_POST;
2130 r = service_spawn(s,
2134 !s->permissions_start_only,
2135 !s->root_directory_start_only,
2143 service_set_state(s, SERVICE_START_POST);
2145 service_enter_running(s, SERVICE_SUCCESS);
2150 log_warning_unit(UNIT(s)->id,
2151 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2152 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2155 static void service_kill_control_processes(Service *s) {
2158 if (!UNIT(s)->cgroup_path)
2161 p = strappenda(UNIT(s)->cgroup_path, "/control");
2162 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
2165 static void service_enter_start(Service *s) {
2172 assert(s->exec_command[SERVICE_EXEC_START]);
2173 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2175 service_unwatch_control_pid(s);
2176 service_unwatch_main_pid(s);
2178 /* We want to ensure that nobody leaks processes from
2179 * START_PRE here, so let's go on a killing spree, People
2180 * should not spawn long running processes from START_PRE. */
2181 service_kill_control_processes(s);
2183 if (s->type == SERVICE_FORKING) {
2184 s->control_command_id = SERVICE_EXEC_START;
2185 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2187 s->main_command = NULL;
2189 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2190 s->control_command = NULL;
2192 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2195 r = service_spawn(s,
2197 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
2198 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2203 s->notify_access != NOTIFY_NONE,
2209 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2210 /* For simple services we immediately start
2211 * the START_POST binaries. */
2213 service_set_main_pid(s, pid);
2214 service_enter_start_post(s);
2216 } else if (s->type == SERVICE_FORKING) {
2218 /* For forking services we wait until the start
2219 * process exited. */
2221 s->control_pid = pid;
2222 service_set_state(s, SERVICE_START);
2224 } else if (s->type == SERVICE_ONESHOT ||
2225 s->type == SERVICE_DBUS ||
2226 s->type == SERVICE_NOTIFY) {
2228 /* For oneshot services we wait until the start
2229 * process exited, too, but it is our main process. */
2231 /* For D-Bus services we know the main pid right away,
2232 * but wait for the bus name to appear on the
2233 * bus. Notify services are similar. */
2235 service_set_main_pid(s, pid);
2236 service_set_state(s, SERVICE_START);
2238 assert_not_reached("Unknown service type");
2243 log_warning_unit(UNIT(s)->id,
2244 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2245 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2248 static void service_enter_start_pre(Service *s) {
2253 service_unwatch_control_pid(s);
2255 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
2256 if (s->control_command) {
2257 /* Before we start anything, let's clear up what might
2258 * be left from previous runs. */
2259 service_kill_control_processes(s);
2261 s->control_command_id = SERVICE_EXEC_START_PRE;
2263 r = service_spawn(s,
2267 !s->permissions_start_only,
2268 !s->root_directory_start_only,
2276 service_set_state(s, SERVICE_START_PRE);
2278 service_enter_start(s);
2283 log_warning_unit(UNIT(s)->id,
2284 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2285 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2288 static void service_enter_restart(Service *s) {
2293 dbus_error_init(&error);
2295 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2296 /* Don't restart things if we are going down anyway */
2297 log_info_unit(UNIT(s)->id,
2298 "Stop job pending for unit, delaying automatic restart.");
2300 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
2307 /* Any units that are bound to this service must also be
2308 * restarted. We use JOB_RESTART (instead of the more obvious
2309 * JOB_START) here so that those dependency jobs will be added
2311 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2315 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2316 * it will be canceled as part of the service_stop() call that
2317 * is executed as part of JOB_RESTART. */
2319 log_debug_unit(UNIT(s)->id,
2320 "%s scheduled restart job.", UNIT(s)->id);
2324 log_warning_unit(UNIT(s)->id,
2325 "%s failed to schedule restart job: %s",
2326 UNIT(s)->id, bus_error(&error, -r));
2327 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2329 dbus_error_free(&error);
2332 static void service_enter_reload(Service *s) {
2337 service_unwatch_control_pid(s);
2339 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
2340 if (s->control_command) {
2341 s->control_command_id = SERVICE_EXEC_RELOAD;
2343 r = service_spawn(s,
2347 !s->permissions_start_only,
2348 !s->root_directory_start_only,
2356 service_set_state(s, SERVICE_RELOAD);
2358 service_enter_running(s, SERVICE_SUCCESS);
2363 log_warning_unit(UNIT(s)->id,
2364 "%s failed to run 'reload' task: %s",
2365 UNIT(s)->id, strerror(-r));
2366 s->reload_result = SERVICE_FAILURE_RESOURCES;
2367 service_enter_running(s, SERVICE_SUCCESS);
2370 static void service_run_next_control(Service *s) {
2374 assert(s->control_command);
2375 assert(s->control_command->command_next);
2377 assert(s->control_command_id != SERVICE_EXEC_START);
2379 s->control_command = s->control_command->command_next;
2380 service_unwatch_control_pid(s);
2382 r = service_spawn(s,
2386 !s->permissions_start_only,
2387 !s->root_directory_start_only,
2388 s->control_command_id == SERVICE_EXEC_START_PRE ||
2389 s->control_command_id == SERVICE_EXEC_STOP_POST,
2399 log_warning_unit(UNIT(s)->id,
2400 "%s failed to run next control task: %s",
2401 UNIT(s)->id, strerror(-r));
2403 if (s->state == SERVICE_START_PRE)
2404 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2405 else if (s->state == SERVICE_STOP)
2406 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2407 else if (s->state == SERVICE_STOP_POST)
2408 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2409 else if (s->state == SERVICE_RELOAD) {
2410 s->reload_result = SERVICE_FAILURE_RESOURCES;
2411 service_enter_running(s, SERVICE_SUCCESS);
2413 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2416 static void service_run_next_main(Service *s) {
2421 assert(s->main_command);
2422 assert(s->main_command->command_next);
2423 assert(s->type == SERVICE_ONESHOT);
2425 s->main_command = s->main_command->command_next;
2426 service_unwatch_main_pid(s);
2428 r = service_spawn(s,
2435 s->notify_access != NOTIFY_NONE,
2441 service_set_main_pid(s, pid);
2446 log_warning_unit(UNIT(s)->id,
2447 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2448 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2451 static int service_start_limit_test(Service *s) {
2454 if (ratelimit_test(&s->start_limit))
2457 switch (s->start_limit_action) {
2459 case SERVICE_START_LIMIT_NONE:
2460 log_warning_unit(UNIT(s)->id,
2461 "%s start request repeated too quickly, refusing to start.",
2465 case SERVICE_START_LIMIT_REBOOT: {
2469 dbus_error_init(&error);
2471 log_warning_unit(UNIT(s)->id,
2472 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2474 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
2475 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
2476 true, &error, NULL);
2478 log_error_unit(UNIT(s)->id,
2479 "Failed to reboot: %s.", bus_error(&error, r));
2480 dbus_error_free(&error);
2486 case SERVICE_START_LIMIT_REBOOT_FORCE:
2487 log_warning_unit(UNIT(s)->id,
2488 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2489 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2492 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2493 log_warning_unit(UNIT(s)->id,
2494 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2496 reboot(RB_AUTOBOOT);
2500 log_error_unit(UNIT(s)->id,
2501 "start limit action=%i", s->start_limit_action);
2502 assert_not_reached("Unknown StartLimitAction.");
2508 static int service_start(Unit *u) {
2509 Service *s = SERVICE(u);
2514 /* We cannot fulfill this request right now, try again later
2516 if (s->state == SERVICE_STOP ||
2517 s->state == SERVICE_STOP_SIGTERM ||
2518 s->state == SERVICE_STOP_SIGKILL ||
2519 s->state == SERVICE_STOP_POST ||
2520 s->state == SERVICE_FINAL_SIGTERM ||
2521 s->state == SERVICE_FINAL_SIGKILL)
2524 /* Already on it! */
2525 if (s->state == SERVICE_START_PRE ||
2526 s->state == SERVICE_START ||
2527 s->state == SERVICE_START_POST)
2530 /* A service that will be restarted must be stopped first to
2531 * trigger BindsTo and/or OnFailure dependencies. If a user
2532 * does not want to wait for the holdoff time to elapse, the
2533 * service should be manually restarted, not started. We
2534 * simply return EAGAIN here, so that any start jobs stay
2535 * queued, and assume that the auto restart timer will
2536 * eventually trigger the restart. */
2537 if (s->state == SERVICE_AUTO_RESTART)
2540 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2542 /* Make sure we don't enter a busy loop of some kind. */
2543 r = service_start_limit_test(s);
2545 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2549 s->result = SERVICE_SUCCESS;
2550 s->reload_result = SERVICE_SUCCESS;
2551 s->main_pid_known = false;
2552 s->main_pid_alien = false;
2553 s->forbid_restart = false;
2555 service_enter_start_pre(s);
2559 static int service_stop(Unit *u) {
2560 Service *s = SERVICE(u);
2564 /* Don't create restart jobs from here. */
2565 s->forbid_restart = true;
2568 if (s->state == SERVICE_STOP ||
2569 s->state == SERVICE_STOP_SIGTERM ||
2570 s->state == SERVICE_STOP_SIGKILL ||
2571 s->state == SERVICE_STOP_POST ||
2572 s->state == SERVICE_FINAL_SIGTERM ||
2573 s->state == SERVICE_FINAL_SIGKILL)
2576 /* A restart will be scheduled or is in progress. */
2577 if (s->state == SERVICE_AUTO_RESTART) {
2578 service_set_state(s, SERVICE_DEAD);
2582 /* If there's already something running we go directly into
2584 if (s->state == SERVICE_START_PRE ||
2585 s->state == SERVICE_START ||
2586 s->state == SERVICE_START_POST ||
2587 s->state == SERVICE_RELOAD) {
2588 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2592 assert(s->state == SERVICE_RUNNING ||
2593 s->state == SERVICE_EXITED);
2595 service_enter_stop(s, SERVICE_SUCCESS);
2599 static int service_reload(Unit *u) {
2600 Service *s = SERVICE(u);
2604 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2606 service_enter_reload(s);
2610 _pure_ static bool service_can_reload(Unit *u) {
2611 Service *s = SERVICE(u);
2615 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2618 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2619 Service *s = SERVICE(u);
2625 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2626 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2627 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2629 if (s->control_pid > 0)
2630 unit_serialize_item_format(u, f, "control-pid", "%lu",
2631 (unsigned long) s->control_pid);
2633 if (s->main_pid_known && s->main_pid > 0)
2634 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2636 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2639 unit_serialize_item(u, f, "status-text", s->status_text);
2641 /* FIXME: There's a minor uncleanliness here: if there are
2642 * multiple commands attached here, we will start from the
2643 * first one again */
2644 if (s->control_command_id >= 0)
2645 unit_serialize_item(u, f, "control-command",
2646 service_exec_command_to_string(s->control_command_id));
2648 if (s->socket_fd >= 0) {
2651 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2654 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2657 if (s->main_exec_status.pid > 0) {
2658 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu",
2659 (unsigned long) s->main_exec_status.pid);
2660 dual_timestamp_serialize(f, "main-exec-status-start",
2661 &s->main_exec_status.start_timestamp);
2662 dual_timestamp_serialize(f, "main-exec-status-exit",
2663 &s->main_exec_status.exit_timestamp);
2665 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2666 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
2667 s->main_exec_status.code);
2668 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
2669 s->main_exec_status.status);
2672 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2673 dual_timestamp_serialize(f, "watchdog-timestamp",
2674 &s->watchdog_timestamp);
2676 if (s->exec_context.tmp_dir)
2677 unit_serialize_item(u, f, "tmp-dir", s->exec_context.tmp_dir);
2679 if (s->exec_context.var_tmp_dir)
2680 unit_serialize_item(u, f, "var-tmp-dir", s->exec_context.var_tmp_dir);
2685 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2686 Service *s = SERVICE(u);
2693 if (streq(key, "state")) {
2696 state = service_state_from_string(value);
2698 log_debug_unit(u->id, "Failed to parse state value %s", value);
2700 s->deserialized_state = state;
2701 } else if (streq(key, "result")) {
2704 f = service_result_from_string(value);
2706 log_debug_unit(u->id, "Failed to parse result value %s", value);
2707 else if (f != SERVICE_SUCCESS)
2710 } else if (streq(key, "reload-result")) {
2713 f = service_result_from_string(value);
2715 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2716 else if (f != SERVICE_SUCCESS)
2717 s->reload_result = f;
2719 } else if (streq(key, "control-pid")) {
2722 if (parse_pid(value, &pid) < 0)
2723 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2725 s->control_pid = pid;
2726 } else if (streq(key, "main-pid")) {
2729 if (parse_pid(value, &pid) < 0)
2730 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2732 service_set_main_pid(s, (pid_t) pid);
2733 } else if (streq(key, "main-pid-known")) {
2736 b = parse_boolean(value);
2738 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2740 s->main_pid_known = b;
2741 } else if (streq(key, "status-text")) {
2748 free(s->status_text);
2752 } else if (streq(key, "control-command")) {
2753 ServiceExecCommand id;
2755 id = service_exec_command_from_string(value);
2757 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2759 s->control_command_id = id;
2760 s->control_command = s->exec_command[id];
2762 } else if (streq(key, "socket-fd")) {
2765 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2766 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2769 if (s->socket_fd >= 0)
2770 close_nointr_nofail(s->socket_fd);
2771 s->socket_fd = fdset_remove(fds, fd);
2773 } else if (streq(key, "main-exec-status-pid")) {
2776 if (parse_pid(value, &pid) < 0)
2777 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2779 s->main_exec_status.pid = pid;
2780 } else if (streq(key, "main-exec-status-code")) {
2783 if (safe_atoi(value, &i) < 0)
2784 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2786 s->main_exec_status.code = i;
2787 } else if (streq(key, "main-exec-status-status")) {
2790 if (safe_atoi(value, &i) < 0)
2791 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2793 s->main_exec_status.status = i;
2794 } else if (streq(key, "main-exec-status-start"))
2795 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2796 else if (streq(key, "main-exec-status-exit"))
2797 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2798 else if (streq(key, "watchdog-timestamp"))
2799 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2800 else if (streq(key, "tmp-dir")) {
2807 s->exec_context.tmp_dir = t;
2808 } else if (streq(key, "var-tmp-dir")) {
2815 s->exec_context.var_tmp_dir = t;
2817 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2822 _pure_ static UnitActiveState service_active_state(Unit *u) {
2823 const UnitActiveState *table;
2827 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2829 return table[SERVICE(u)->state];
2832 static const char *service_sub_state_to_string(Unit *u) {
2835 return service_state_to_string(SERVICE(u)->state);
2838 static bool service_check_gc(Unit *u) {
2839 Service *s = SERVICE(u);
2843 /* Never clean up services that still have a process around,
2844 * even if the service is formally dead. */
2845 if (cgroup_good(s) > 0 ||
2846 main_pid_good(s) > 0 ||
2847 control_pid_good(s) > 0)
2850 #ifdef HAVE_SYSV_COMPAT
2858 _pure_ static bool service_check_snapshot(Unit *u) {
2859 Service *s = SERVICE(u);
2863 return !s->got_socket_fd;
2866 static int service_retry_pid_file(Service *s) {
2869 assert(s->pid_file);
2870 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2872 r = service_load_pid_file(s, false);
2876 service_unwatch_pid_file(s);
2878 service_enter_running(s, SERVICE_SUCCESS);
2882 static int service_watch_pid_file(Service *s) {
2885 log_debug_unit(UNIT(s)->id,
2886 "Setting watch for %s's PID file %s",
2887 UNIT(s)->id, s->pid_file_pathspec->path);
2888 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2892 /* the pidfile might have appeared just before we set the watch */
2893 log_debug_unit(UNIT(s)->id,
2894 "Trying to read %s's PID file %s in case it changed",
2895 UNIT(s)->id, s->pid_file_pathspec->path);
2896 service_retry_pid_file(s);
2900 log_error_unit(UNIT(s)->id,
2901 "Failed to set a watch for %s's PID file %s: %s",
2902 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2903 service_unwatch_pid_file(s);
2907 static int service_demand_pid_file(Service *s) {
2910 assert(s->pid_file);
2911 assert(!s->pid_file_pathspec);
2913 ps = new0(PathSpec, 1);
2917 ps->path = strdup(s->pid_file);
2923 path_kill_slashes(ps->path);
2925 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2926 * keep their PID file open all the time. */
2927 ps->type = PATH_MODIFIED;
2928 ps->inotify_fd = -1;
2930 s->pid_file_pathspec = ps;
2932 return service_watch_pid_file(s);
2935 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2936 Service *s = SERVICE(u);
2940 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2941 assert(s->pid_file_pathspec);
2942 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2944 log_debug_unit(u->id, "inotify event for %s", u->id);
2946 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2949 if (service_retry_pid_file(s) == 0)
2952 if (service_watch_pid_file(s) < 0)
2957 service_unwatch_pid_file(s);
2958 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2961 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2962 Service *s = SERVICE(u);
2968 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2969 is_clean_exit_lsb(code, status, &s->success_status))
2970 f = SERVICE_SUCCESS;
2971 else if (code == CLD_EXITED)
2972 f = SERVICE_FAILURE_EXIT_CODE;
2973 else if (code == CLD_KILLED)
2974 f = SERVICE_FAILURE_SIGNAL;
2975 else if (code == CLD_DUMPED)
2976 f = SERVICE_FAILURE_CORE_DUMP;
2978 assert_not_reached("Unknown code");
2980 if (s->main_pid == pid) {
2981 /* Forking services may occasionally move to a new PID.
2982 * As long as they update the PID file before exiting the old
2983 * PID, they're fine. */
2984 if (service_load_pid_file(s, false) == 0)
2988 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2990 if (s->main_command) {
2991 /* If this is not a forking service than the
2992 * main process got started and hence we copy
2993 * the exit status so that it is recorded both
2994 * as main and as control process exit
2997 s->main_command->exec_status = s->main_exec_status;
2999 if (s->main_command->ignore)
3000 f = SERVICE_SUCCESS;
3001 } else if (s->exec_command[SERVICE_EXEC_START]) {
3003 /* If this is a forked process, then we should
3004 * ignore the return value if this was
3005 * configured for the starter process */
3007 if (s->exec_command[SERVICE_EXEC_START]->ignore)
3008 f = SERVICE_SUCCESS;
3011 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
3013 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
3014 u->id, sigchld_code_to_string(code), status,
3015 strna(code == CLD_EXITED
3016 ? exit_status_to_string(status, EXIT_STATUS_FULL)
3017 : signal_to_string(status)),
3018 "EXIT_CODE=%s", sigchld_code_to_string(code),
3019 "EXIT_STATUS=%i", status,
3022 if (f != SERVICE_SUCCESS)
3025 if (s->main_command &&
3026 s->main_command->command_next &&
3027 f == SERVICE_SUCCESS) {
3029 /* There is another command to *
3030 * execute, so let's do that. */
3032 log_debug_unit(u->id,
3033 "%s running next main command for state %s",
3034 u->id, service_state_to_string(s->state));
3035 service_run_next_main(s);
3039 /* The service exited, so the service is officially
3041 s->main_command = NULL;
3045 case SERVICE_START_POST:
3046 case SERVICE_RELOAD:
3048 /* Need to wait until the operation is
3053 if (s->type == SERVICE_ONESHOT) {
3054 /* This was our main goal, so let's go on */
3055 if (f == SERVICE_SUCCESS)
3056 service_enter_start_post(s);
3058 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3064 case SERVICE_RUNNING:
3065 service_enter_running(s, f);
3068 case SERVICE_STOP_SIGTERM:
3069 case SERVICE_STOP_SIGKILL:
3071 if (!control_pid_good(s))
3072 service_enter_stop_post(s, f);
3074 /* If there is still a control process, wait for that first */
3078 assert_not_reached("Uh, main process died at wrong time.");
3082 } else if (s->control_pid == pid) {
3085 if (s->control_command) {
3086 exec_status_exit(&s->control_command->exec_status,
3087 &s->exec_context, pid, code, status);
3089 if (s->control_command->ignore)
3090 f = SERVICE_SUCCESS;
3093 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
3094 "%s: control process exited, code=%s status=%i",
3095 u->id, sigchld_code_to_string(code), status);
3097 if (f != SERVICE_SUCCESS)
3100 /* Immediately get rid of the cgroup, so that the
3101 * kernel doesn't delay the cgroup empty messages for
3102 * the service cgroup any longer than necessary */
3103 service_kill_control_processes(s);
3105 if (s->control_command &&
3106 s->control_command->command_next &&
3107 f == SERVICE_SUCCESS) {
3109 /* There is another command to *
3110 * execute, so let's do that. */
3112 log_debug_unit(u->id,
3113 "%s running next control command for state %s",
3114 u->id, service_state_to_string(s->state));
3115 service_run_next_control(s);
3118 /* No further commands for this step, so let's
3119 * figure out what to do next */
3121 s->control_command = NULL;
3122 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3124 log_debug_unit(u->id,
3125 "%s got final SIGCHLD for state %s",
3126 u->id, service_state_to_string(s->state));
3130 case SERVICE_START_PRE:
3131 if (f == SERVICE_SUCCESS)
3132 service_enter_start(s);
3134 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3138 if (s->type != SERVICE_FORKING)
3139 /* Maybe spurious event due to a reload that changed the type? */
3142 if (f != SERVICE_SUCCESS) {
3143 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3148 bool has_start_post;
3151 /* Let's try to load the pid file here if we can.
3152 * The PID file might actually be created by a START_POST
3153 * script. In that case don't worry if the loading fails. */
3155 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3156 r = service_load_pid_file(s, !has_start_post);
3157 if (!has_start_post && r < 0) {
3158 r = service_demand_pid_file(s);
3159 if (r < 0 || !cgroup_good(s))
3160 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3164 service_search_main_pid(s);
3166 service_enter_start_post(s);
3169 case SERVICE_START_POST:
3170 if (f != SERVICE_SUCCESS) {
3171 service_enter_stop(s, f);
3178 r = service_load_pid_file(s, true);
3180 r = service_demand_pid_file(s);
3181 if (r < 0 || !cgroup_good(s))
3182 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3186 service_search_main_pid(s);
3188 service_enter_running(s, SERVICE_SUCCESS);
3191 case SERVICE_RELOAD:
3192 if (f == SERVICE_SUCCESS) {
3193 service_load_pid_file(s, true);
3194 service_search_main_pid(s);
3197 s->reload_result = f;
3198 service_enter_running(s, SERVICE_SUCCESS);
3202 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3205 case SERVICE_STOP_SIGTERM:
3206 case SERVICE_STOP_SIGKILL:
3207 if (main_pid_good(s) <= 0)
3208 service_enter_stop_post(s, f);
3210 /* If there is still a service
3211 * process around, wait until
3212 * that one quit, too */
3215 case SERVICE_STOP_POST:
3216 case SERVICE_FINAL_SIGTERM:
3217 case SERVICE_FINAL_SIGKILL:
3218 service_enter_dead(s, f, true);
3222 assert_not_reached("Uh, control process died at wrong time.");
3227 /* Notify clients about changed exit status */
3228 unit_add_to_dbus_queue(u);
3231 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3232 Service *s = SERVICE(u);
3235 assert(elapsed == 1);
3237 if (w == &s->watchdog_watch) {
3238 service_handle_watchdog(s);
3242 assert(w == &s->timer_watch);
3246 case SERVICE_START_PRE:
3248 log_warning_unit(u->id,
3249 "%s operation timed out. Terminating.", u->id);
3250 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3253 case SERVICE_START_POST:
3254 log_warning_unit(u->id,
3255 "%s operation timed out. Stopping.", u->id);
3256 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3259 case SERVICE_RELOAD:
3260 log_warning_unit(u->id,
3261 "%s operation timed out. Stopping.", u->id);
3262 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3263 service_enter_running(s, SERVICE_SUCCESS);
3267 log_warning_unit(u->id,
3268 "%s stopping timed out. Terminating.", u->id);
3269 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3272 case SERVICE_STOP_SIGTERM:
3273 if (s->kill_context.send_sigkill) {
3274 log_warning_unit(u->id,
3275 "%s stopping timed out. Killing.", u->id);
3276 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3278 log_warning_unit(u->id,
3279 "%s stopping timed out. Skipping SIGKILL.", u->id);
3280 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3285 case SERVICE_STOP_SIGKILL:
3286 /* Uh, we sent a SIGKILL and it is still not gone?
3287 * Must be something we cannot kill, so let's just be
3288 * weirded out and continue */
3290 log_warning_unit(u->id,
3291 "%s still around after SIGKILL. Ignoring.", u->id);
3292 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3295 case SERVICE_STOP_POST:
3296 log_warning_unit(u->id,
3297 "%s stopping timed out (2). Terminating.", u->id);
3298 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3301 case SERVICE_FINAL_SIGTERM:
3302 if (s->kill_context.send_sigkill) {
3303 log_warning_unit(u->id,
3304 "%s stopping timed out (2). Killing.", u->id);
3305 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3307 log_warning_unit(u->id,
3308 "%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.",
3310 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3315 case SERVICE_FINAL_SIGKILL:
3316 log_warning_unit(u->id,
3317 "%s still around after SIGKILL (2). Entering failed mode.", u->id);
3318 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3321 case SERVICE_AUTO_RESTART:
3322 log_info_unit(u->id,
3323 "%s holdoff time over, scheduling restart.", u->id);
3324 service_enter_restart(s);
3328 assert_not_reached("Timeout at wrong time.");
3332 static void service_notify_cgroup_empty_event(Unit *u) {
3333 Service *s = SERVICE(u);
3337 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
3341 /* Waiting for SIGCHLD is usually more interesting,
3342 * because it includes return codes/signals. Which is
3343 * why we ignore the cgroup events for most cases,
3344 * except when we don't know pid which to expect the
3348 case SERVICE_START_POST:
3349 /* If we were hoping for the daemon to write its PID file,
3350 * we can give up now. */
3351 if (s->pid_file_pathspec) {
3352 log_warning_unit(u->id,
3353 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3354 service_unwatch_pid_file(s);
3355 if (s->state == SERVICE_START)
3356 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3358 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3362 case SERVICE_RUNNING:
3363 /* service_enter_running() will figure out what to do */
3364 service_enter_running(s, SERVICE_SUCCESS);
3367 case SERVICE_STOP_SIGTERM:
3368 case SERVICE_STOP_SIGKILL:
3370 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3371 service_enter_stop_post(s, SERVICE_SUCCESS);
3375 case SERVICE_FINAL_SIGTERM:
3376 case SERVICE_FINAL_SIGKILL:
3377 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3378 service_enter_dead(s, SERVICE_SUCCESS, true);
3387 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3388 Service *s = SERVICE(u);
3393 if (s->notify_access == NOTIFY_NONE) {
3394 log_warning_unit(u->id,
3395 "%s: Got notification message from PID %lu, but reception is disabled.",
3396 u->id, (unsigned long) pid);
3400 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3401 log_warning_unit(u->id,
3402 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3403 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3407 log_debug_unit(u->id,
3408 "%s: Got message", u->id);
3410 /* Interpret MAINPID= */
3411 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3412 (s->state == SERVICE_START ||
3413 s->state == SERVICE_START_POST ||
3414 s->state == SERVICE_RUNNING ||
3415 s->state == SERVICE_RELOAD)) {
3417 if (parse_pid(e + 8, &pid) < 0)
3418 log_warning_unit(u->id,
3419 "Failed to parse notification message %s", e);
3421 log_debug_unit(u->id,
3422 "%s: got %s", u->id, e);
3423 service_set_main_pid(s, pid);
3427 /* Interpret READY= */
3428 if (s->type == SERVICE_NOTIFY &&
3429 s->state == SERVICE_START &&
3430 strv_find(tags, "READY=1")) {
3431 log_debug_unit(u->id,
3432 "%s: got READY=1", u->id);
3434 service_enter_start_post(s);
3437 /* Interpret STATUS= */
3438 e = strv_find_prefix(tags, "STATUS=");
3444 if (!utf8_is_valid(e+7)) {
3445 log_warning_unit(u->id,
3446 "Status message in notification is not UTF-8 clean.");
3452 log_error_unit(u->id,
3453 "Failed to allocate string.");
3457 log_debug_unit(u->id,
3458 "%s: got %s", u->id, e);
3460 free(s->status_text);
3463 free(s->status_text);
3464 s->status_text = NULL;
3468 if (strv_find(tags, "WATCHDOG=1")) {
3469 log_debug_unit(u->id,
3470 "%s: got WATCHDOG=1", u->id);
3471 if (dual_timestamp_is_set(&s->watchdog_timestamp))
3472 service_reset_watchdog(s);
3475 /* Notify clients about changed status or main pid */
3476 unit_add_to_dbus_queue(u);
3479 #ifdef HAVE_SYSV_COMPAT
3481 static int service_enumerate(Manager *m) {
3484 _cleanup_closedir_ DIR *d = NULL;
3485 _cleanup_free_ char *path = NULL, *fpath = NULL, *name = NULL;
3486 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
3487 _cleanup_set_free_ Set *shutdown_services = NULL;
3494 if (m->running_as != SYSTEMD_SYSTEM)
3497 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3498 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3502 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3513 if (errno != ENOENT)
3514 log_warning("opendir(%s) failed: %s", path, strerror(errno));
3519 while ((de = readdir(d))) {
3522 if (ignore_file(de->d_name))
3525 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3528 if (strlen(de->d_name) < 4)
3531 a = undecchar(de->d_name[1]);
3532 b = undecchar(de->d_name[2]);
3538 fpath = strjoin(path, "/", de->d_name, NULL);
3544 if (access(fpath, X_OK) < 0) {
3546 if (errno != ENOENT)
3547 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3553 name = sysv_translate_name(de->d_name + 3);
3559 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3561 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3565 if (de->d_name[0] == 'S') {
3567 if (rcnd_table[i].type == RUNLEVEL_UP) {
3568 SERVICE(service)->sysv_start_priority_from_rcnd =
3569 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3571 SERVICE(service)->sysv_enabled = true;
3574 r = set_ensure_allocated(&runlevel_services[i],
3575 trivial_hash_func, trivial_compare_func);
3579 r = set_put(runlevel_services[i], service);
3583 } else if (de->d_name[0] == 'K' &&
3584 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3586 r = set_ensure_allocated(&shutdown_services,
3587 trivial_hash_func, trivial_compare_func);
3591 r = set_put(shutdown_services, service);
3598 /* Now we loaded all stubs and are aware of the lowest
3599 start-up priority for all services, not let's actually load
3600 the services, this will also tell us which services are
3601 actually native now */
3602 manager_dispatch_load_queue(m);
3604 /* If this is a native service, rely on native ways to pull in
3605 * a service, don't pull it in via sysv rcN.d links. */
3606 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3607 SET_FOREACH(service, runlevel_services[i], j) {
3608 service = unit_follow_merge(service);
3610 if (service->fragment_path)
3613 r = unit_add_two_dependencies_by_name_inverse(
3614 service, UNIT_AFTER, UNIT_WANTS,
3615 rcnd_table[i].target, NULL, true);
3620 /* We honour K links only for halt/reboot. For the normal
3621 * runlevels we assume the stop jobs will be implicitly added
3622 * by the core logic. Also, we don't really distinguish here
3623 * between the runlevels 0 and 6 and just add them to the
3624 * special shutdown target. */
3625 SET_FOREACH(service, shutdown_services, j) {
3626 service = unit_follow_merge(service);
3628 if (service->fragment_path)
3631 r = unit_add_two_dependencies_by_name(
3632 service, UNIT_BEFORE, UNIT_CONFLICTS,
3633 SPECIAL_SHUTDOWN_TARGET, NULL, true);
3642 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3643 set_free(runlevel_services[i]);
3649 static void service_bus_name_owner_change(
3652 const char *old_owner,
3653 const char *new_owner) {
3655 Service *s = SERVICE(u);
3660 assert(streq(s->bus_name, name));
3661 assert(old_owner || new_owner);
3663 if (old_owner && new_owner)
3664 log_debug_unit(u->id,
3665 "%s's D-Bus name %s changed owner from %s to %s",
3666 u->id, name, old_owner, new_owner);
3668 log_debug_unit(u->id,
3669 "%s's D-Bus name %s no longer registered by %s",
3670 u->id, name, old_owner);
3672 log_debug_unit(u->id,
3673 "%s's D-Bus name %s now registered by %s",
3674 u->id, name, new_owner);
3676 s->bus_name_good = !!new_owner;
3678 if (s->type == SERVICE_DBUS) {
3680 /* service_enter_running() will figure out what to
3682 if (s->state == SERVICE_RUNNING)
3683 service_enter_running(s, SERVICE_SUCCESS);
3684 else if (s->state == SERVICE_START && new_owner)
3685 service_enter_start_post(s);
3687 } else if (new_owner &&
3689 (s->state == SERVICE_START ||
3690 s->state == SERVICE_START_POST ||
3691 s->state == SERVICE_RUNNING ||
3692 s->state == SERVICE_RELOAD)) {
3694 /* Try to acquire PID from bus service */
3695 log_debug_unit(u->id,
3696 "Trying to acquire PID from D-Bus name...");
3698 bus_query_pid(u->manager, name);
3702 static void service_bus_query_pid_done(
3707 Service *s = SERVICE(u);
3712 log_debug_unit(u->id,
3713 "%s's D-Bus name %s is now owned by process %u",
3714 u->id, name, (unsigned) pid);
3716 if (s->main_pid <= 0 &&
3717 (s->state == SERVICE_START ||
3718 s->state == SERVICE_START_POST ||
3719 s->state == SERVICE_RUNNING ||
3720 s->state == SERVICE_RELOAD))
3721 service_set_main_pid(s, pid);
3724 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3729 /* This is called by the socket code when instantiating a new
3730 * service for a stream socket and the socket needs to be
3733 if (UNIT(s)->load_state != UNIT_LOADED)
3736 if (s->socket_fd >= 0)
3739 if (s->state != SERVICE_DEAD)
3743 s->got_socket_fd = true;
3745 unit_ref_set(&s->accept_socket, UNIT(sock));
3747 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3750 static void service_reset_failed(Unit *u) {
3751 Service *s = SERVICE(u);
3755 if (s->state == SERVICE_FAILED)
3756 service_set_state(s, SERVICE_DEAD);
3758 s->result = SERVICE_SUCCESS;
3759 s->reload_result = SERVICE_SUCCESS;
3761 RATELIMIT_RESET(s->start_limit);
3764 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3765 Service *s = SERVICE(u);
3767 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
3770 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3771 [SERVICE_DEAD] = "dead",
3772 [SERVICE_START_PRE] = "start-pre",
3773 [SERVICE_START] = "start",
3774 [SERVICE_START_POST] = "start-post",
3775 [SERVICE_RUNNING] = "running",
3776 [SERVICE_EXITED] = "exited",
3777 [SERVICE_RELOAD] = "reload",
3778 [SERVICE_STOP] = "stop",
3779 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3780 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3781 [SERVICE_STOP_POST] = "stop-post",
3782 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3783 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3784 [SERVICE_FAILED] = "failed",
3785 [SERVICE_AUTO_RESTART] = "auto-restart",
3788 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3790 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3791 [SERVICE_RESTART_NO] = "no",
3792 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3793 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3794 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3795 [SERVICE_RESTART_ALWAYS] = "always"
3798 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3800 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3801 [SERVICE_SIMPLE] = "simple",
3802 [SERVICE_FORKING] = "forking",
3803 [SERVICE_ONESHOT] = "oneshot",
3804 [SERVICE_DBUS] = "dbus",
3805 [SERVICE_NOTIFY] = "notify",
3806 [SERVICE_IDLE] = "idle"
3809 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3811 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3812 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3813 [SERVICE_EXEC_START] = "ExecStart",
3814 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3815 [SERVICE_EXEC_RELOAD] = "ExecReload",
3816 [SERVICE_EXEC_STOP] = "ExecStop",
3817 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3820 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3822 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3823 [NOTIFY_NONE] = "none",
3824 [NOTIFY_MAIN] = "main",
3825 [NOTIFY_ALL] = "all"
3828 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3830 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3831 [SERVICE_SUCCESS] = "success",
3832 [SERVICE_FAILURE_RESOURCES] = "resources",
3833 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3834 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3835 [SERVICE_FAILURE_SIGNAL] = "signal",
3836 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3837 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3838 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3841 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3843 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3844 [SERVICE_START_LIMIT_NONE] = "none",
3845 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3846 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3847 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3849 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3851 const UnitVTable service_vtable = {
3852 .object_size = sizeof(Service),
3859 .private_section = "Service",
3860 .exec_context_offset = offsetof(Service, exec_context),
3861 .cgroup_context_offset = offsetof(Service, cgroup_context),
3863 .init = service_init,
3864 .done = service_done,
3865 .load = service_load,
3867 .coldplug = service_coldplug,
3869 .dump = service_dump,
3871 .start = service_start,
3872 .stop = service_stop,
3873 .reload = service_reload,
3875 .can_reload = service_can_reload,
3877 .kill = service_kill,
3879 .serialize = service_serialize,
3880 .deserialize_item = service_deserialize_item,
3882 .active_state = service_active_state,
3883 .sub_state_to_string = service_sub_state_to_string,
3885 .check_gc = service_check_gc,
3886 .check_snapshot = service_check_snapshot,
3888 .sigchld_event = service_sigchld_event,
3889 .timer_event = service_timer_event,
3890 .fd_event = service_fd_event,
3892 .reset_failed = service_reset_failed,
3894 .notify_cgroup_empty = service_notify_cgroup_empty_event,
3895 .notify_message = service_notify_message,
3897 .bus_name_owner_change = service_bus_name_owner_change,
3898 .bus_query_pid_done = service_bus_query_pid_done,
3900 .bus_interface = "org.freedesktop.systemd1.Service",
3901 .bus_message_handler = bus_service_message_handler,
3902 .bus_invalidating_properties = bus_service_invalidating_properties,
3903 .bus_set_property = bus_service_set_property,
3904 .bus_commit_properties = bus_service_commit_properties,
3906 .can_transient = true,
3908 #ifdef HAVE_SYSV_COMPAT
3909 .enumerate = service_enumerate,
3911 .status_message_formats = {
3912 .starting_stopping = {
3913 [0] = "Starting %s...",
3914 [1] = "Stopping %s...",
3916 .finished_start_job = {
3917 [JOB_DONE] = "Started %s.",
3918 [JOB_FAILED] = "Failed to start %s.",
3919 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3920 [JOB_TIMEOUT] = "Timed out starting %s.",
3922 .finished_stop_job = {
3923 [JOB_DONE] = "Stopped %s.",
3924 [JOB_FAILED] = "Stopped (with error) %s.",
3925 [JOB_TIMEOUT] = "Timed out stopping %s.",