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 if (s->main_pid == pid && s->main_pid_known)
197 if (s->main_pid != pid) {
198 service_unwatch_main_pid(s);
199 exec_status_start(&s->main_exec_status, pid);
203 s->main_pid_known = true;
205 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
206 log_warning_unit(UNIT(s)->id,
207 "%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
208 UNIT(s)->id, (unsigned long) pid);
210 s->main_pid_alien = true;
212 s->main_pid_alien = false;
217 static void service_close_socket_fd(Service *s) {
220 if (s->socket_fd < 0)
223 close_nointr_nofail(s->socket_fd);
227 static void service_connection_unref(Service *s) {
230 if (!UNIT_ISSET(s->accept_socket))
233 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
234 unit_ref_unset(&s->accept_socket);
237 static void service_stop_watchdog(Service *s) {
240 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
241 s->watchdog_timestamp.realtime = 0;
242 s->watchdog_timestamp.monotonic = 0;
245 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
247 static void service_handle_watchdog(Service *s) {
253 if (s->watchdog_usec == 0)
256 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
257 if (offset >= s->watchdog_usec) {
258 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
259 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_WATCHDOG);
263 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->watchdog_usec - offset, &s->watchdog_watch);
265 log_warning_unit(UNIT(s)->id,
266 "%s failed to install watchdog timer: %s",
267 UNIT(s)->id, strerror(-r));
270 static void service_reset_watchdog(Service *s) {
273 dual_timestamp_get(&s->watchdog_timestamp);
274 service_handle_watchdog(s);
277 static void service_done(Unit *u) {
278 Service *s = SERVICE(u);
285 #ifdef HAVE_SYSV_COMPAT
286 free(s->sysv_runlevels);
287 s->sysv_runlevels = NULL;
290 free(s->status_text);
291 s->status_text = NULL;
293 cgroup_context_done(&s->cgroup_context);
294 exec_context_done(&s->exec_context, manager_is_reloading_or_reexecuting(u->manager));
295 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
296 s->control_command = NULL;
297 s->main_command = NULL;
299 set_free(s->restart_ignore_status.code);
300 s->restart_ignore_status.code = NULL;
301 set_free(s->restart_ignore_status.signal);
302 s->restart_ignore_status.signal = NULL;
304 set_free(s->success_status.code);
305 s->success_status.code = NULL;
306 set_free(s->success_status.signal);
307 s->success_status.signal = NULL;
309 /* This will leak a process, but at least no memory or any of
311 service_unwatch_main_pid(s);
312 service_unwatch_control_pid(s);
313 service_unwatch_pid_file(s);
316 unit_unwatch_bus_name(u, s->bus_name);
321 service_close_socket_fd(s);
322 service_connection_unref(s);
324 unit_ref_unset(&s->accept_socket);
326 service_stop_watchdog(s);
328 unit_unwatch_timer(u, &s->timer_watch);
331 #ifdef HAVE_SYSV_COMPAT
332 static char *sysv_translate_name(const char *name) {
335 r = new(char, strlen(name) + sizeof(".service"));
339 if (endswith(name, ".sh"))
340 /* Drop .sh suffix */
341 strcpy(stpcpy(r, name) - 3, ".service");
343 /* Normal init script name */
344 strcpy(stpcpy(r, name), ".service");
349 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
351 /* We silently ignore the $ prefix here. According to the LSB
352 * spec it simply indicates whether something is a
353 * standardized name or a distribution-specific one. Since we
354 * just follow what already exists and do not introduce new
355 * uses or names we don't care who introduced a new name. */
357 static const char * const table[] = {
358 /* LSB defined facilities */
360 "network", SPECIAL_NETWORK_TARGET,
361 "named", SPECIAL_NSS_LOOKUP_TARGET,
362 "portmap", SPECIAL_RPCBIND_TARGET,
363 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
365 "time", SPECIAL_TIME_SYNC_TARGET,
375 n = *name == '$' ? name + 1 : name;
377 for (i = 0; i < ELEMENTSOF(table); i += 2) {
379 if (!streq(table[i], n))
385 r = strdup(table[i+1]);
392 /* If we don't know this name, fallback heuristics to figure
393 * out whether something is a target or a service alias. */
396 if (!unit_prefix_is_valid(n))
399 /* Facilities starting with $ are most likely targets */
400 r = unit_name_build(n, NULL, ".target");
401 } else if (filename && streq(name, filename))
402 /* Names equaling the file name of the services are redundant */
405 /* Everything else we assume to be normal service names */
406 r = sysv_translate_name(n);
417 static int sysv_fix_order(Service *s) {
423 if (s->sysv_start_priority < 0)
426 /* For each pair of services where at least one lacks a LSB
427 * header, we use the start priority value to order things. */
429 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
432 bool special_s, special_t;
439 if (UNIT(t)->load_state != UNIT_LOADED)
442 if (t->sysv_start_priority < 0)
445 /* If both units have modern headers we don't care
446 * about the priorities */
447 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
448 (UNIT(t)->fragment_path || t->sysv_has_lsb))
451 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
452 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
454 if (special_t && !special_s)
456 else if (special_s && !special_t)
458 else if (t->sysv_start_priority < s->sysv_start_priority)
460 else if (t->sysv_start_priority > s->sysv_start_priority)
465 /* FIXME: Maybe we should compare the name here lexicographically? */
467 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
474 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
477 if (!(c = new0(ExecCommand, 1)))
480 if (!(c->path = strdup(path))) {
485 if (!(c->argv = strv_new(path, arg1, NULL))) {
494 static int sysv_exec_commands(Service *s, const bool supports_reload) {
499 assert(UNIT(s)->source_path);
501 c = exec_command_new(UNIT(s)->source_path, "start");
504 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
506 c = exec_command_new(UNIT(s)->source_path, "stop");
509 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
511 if (supports_reload) {
512 c = exec_command_new(UNIT(s)->source_path, "reload");
515 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
521 static bool usage_contains_reload(const char *line) {
522 return (strcasestr(line, "{reload|") ||
523 strcasestr(line, "{reload}") ||
524 strcasestr(line, "{reload\"") ||
525 strcasestr(line, "|reload|") ||
526 strcasestr(line, "|reload}") ||
527 strcasestr(line, "|reload\""));
530 static int service_load_sysv_path(Service *s, const char *path) {
542 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
544 bool supports_reload = false;
551 f = fopen(path, "re");
553 r = errno == ENOENT ? 0 : -errno;
557 if (fstat(fileno(f), &st) < 0) {
562 free(u->source_path);
563 u->source_path = strdup(path);
564 if (!u->source_path) {
568 u->source_mtime = timespec_load(&st.st_mtim);
570 if (null_or_empty(&st)) {
571 u->load_state = UNIT_MASKED;
579 char l[LINE_MAX], *t;
581 if (!fgets(l, sizeof(l), f)) {
586 log_error_unit(u->id,
587 "Failed to read configuration file '%s': %s",
596 /* Try to figure out whether this init script supports
597 * the reload operation. This heuristic looks for
598 * "Usage" lines which include the reload option. */
599 if ( state == USAGE_CONTINUATION ||
600 (state == NORMAL && strcasestr(t, "usage"))) {
601 if (usage_contains_reload(t)) {
602 supports_reload = true;
604 } else if (t[strlen(t)-1] == '\\')
605 state = USAGE_CONTINUATION;
613 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
615 s->sysv_has_lsb = true;
619 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
625 t += strspn(t, WHITESPACE);
627 if (state == NORMAL) {
629 /* Try to parse Red Hat style chkconfig headers */
631 if (startswith_no_case(t, "chkconfig:")) {
633 char runlevels[16], *k;
637 if (sscanf(t+10, "%15s %i %*i",
639 &start_priority) != 2) {
641 log_warning_unit(u->id,
642 "[%s:%u] Failed to parse chkconfig line. Ignoring.",
647 /* A start priority gathered from the
648 * symlink farms is preferred over the
649 * data from the LSB header. */
650 if (start_priority < 0 || start_priority > 99)
651 log_warning_unit(u->id,
652 "[%s:%u] Start priority out of range. Ignoring.",
655 s->sysv_start_priority = start_priority;
657 char_array_0(runlevels);
658 k = delete_chars(runlevels, WHITESPACE "-");
663 if (!(d = strdup(k))) {
668 free(s->sysv_runlevels);
669 s->sysv_runlevels = d;
672 } else if (startswith_no_case(t, "description:")) {
674 size_t k = strlen(t);
678 if (t[k-1] == '\\') {
683 if ((j = strstrip(t+12)) && *j) {
684 if (!(d = strdup(j))) {
691 free(chkconfig_description);
692 chkconfig_description = d;
694 } else if (startswith_no_case(t, "pidfile:")) {
701 if (!path_is_absolute(fn)) {
702 log_warning_unit(u->id,
703 "[%s:%u] PID file not absolute. Ignoring.",
708 if (!(fn = strdup(fn))) {
717 } else if (state == DESCRIPTION) {
719 /* Try to parse Red Hat style description
722 size_t k = strlen(t);
730 if ((j = strstrip(t)) && *j) {
733 if (chkconfig_description)
734 d = strjoin(chkconfig_description, " ", j, NULL);
743 free(chkconfig_description);
744 chkconfig_description = d;
747 } else if (state == LSB || state == LSB_DESCRIPTION) {
749 if (startswith_no_case(t, "Provides:")) {
755 FOREACH_WORD_QUOTED(w, z, t+9, i) {
758 if (!(n = strndup(w, z))) {
763 r = sysv_translate_facility(n, path_get_file_name(path), &m);
772 if (unit_name_to_type(m) == UNIT_SERVICE)
773 r = unit_merge_by_name(u, m);
780 * indication that the
782 * now available. This
785 * targets do NOT pull
788 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
791 log_error_unit(u->id,
792 "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
793 path, line, m, strerror(-r));
798 } else if (startswith_no_case(t, "Required-Start:") ||
799 startswith_no_case(t, "Should-Start:") ||
800 startswith_no_case(t, "X-Start-Before:") ||
801 startswith_no_case(t, "X-Start-After:")) {
807 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
810 if (!(n = strndup(w, z))) {
815 r = sysv_translate_facility(n, path_get_file_name(path), &m);
817 log_error_unit(u->id,
818 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
819 path, line, n, strerror(-r));
829 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
832 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
833 path, line, m, strerror(-r));
837 } else if (startswith_no_case(t, "Default-Start:")) {
842 k = delete_chars(t+14, WHITESPACE "-");
845 if (!(d = strdup(k))) {
850 free(s->sysv_runlevels);
851 s->sysv_runlevels = d;
854 } else if (startswith_no_case(t, "Description:")) {
857 state = LSB_DESCRIPTION;
859 if ((j = strstrip(t+12)) && *j) {
860 if (!(d = strdup(j))) {
867 free(long_description);
868 long_description = d;
870 } else if (startswith_no_case(t, "Short-Description:")) {
875 if ((j = strstrip(t+18)) && *j) {
876 if (!(d = strdup(j))) {
883 free(short_description);
884 short_description = d;
886 } else if (state == LSB_DESCRIPTION) {
888 if (startswith(l, "#\t") || startswith(l, "# ")) {
891 if ((j = strstrip(t)) && *j) {
894 if (long_description)
895 d = strjoin(long_description, " ", t, NULL);
904 free(long_description);
905 long_description = d;
914 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
917 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
918 /* If there a runlevels configured for this service
919 * but none of the standard ones, then we assume this
920 * is some special kind of service (which might be
921 * needed for early boot) and don't create any links
924 UNIT(s)->default_dependencies = false;
926 /* Don't timeout special services during boot (like fsck) */
927 s->timeout_start_usec = 0;
928 s->timeout_stop_usec = 0;
930 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
931 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
934 /* Special setting for all SysV services */
935 s->type = SERVICE_FORKING;
936 s->remain_after_exit = !s->pid_file;
937 s->guess_main_pid = false;
938 s->restart = SERVICE_RESTART_NO;
939 s->exec_context.ignore_sigpipe = false;
940 s->kill_context.kill_mode = KILL_PROCESS;
942 /* We use the long description only if
943 * no short description is set. */
945 if (short_description)
946 description = short_description;
947 else if (chkconfig_description)
948 description = chkconfig_description;
949 else if (long_description)
950 description = long_description;
957 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
965 /* The priority that has been set in /etc/rcN.d/ hierarchies
966 * takes precedence over what is stored as default in the LSB
968 if (s->sysv_start_priority_from_rcnd >= 0)
969 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
971 u->load_state = UNIT_LOADED;
979 free(short_description);
980 free(long_description);
981 free(chkconfig_description);
986 static int service_load_sysv_name(Service *s, const char *name) {
992 /* For SysV services we strip the *.sh suffixes. */
993 if (endswith(name, ".sh.service"))
996 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1000 path = strjoin(*p, "/", name, NULL);
1004 assert(endswith(path, ".service"));
1005 path[strlen(path)-8] = 0;
1007 r = service_load_sysv_path(s, path);
1009 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1010 /* Try *.sh source'able init scripts */
1011 strcat(path, ".sh");
1012 r = service_load_sysv_path(s, path);
1019 if (UNIT(s)->load_state != UNIT_STUB)
1026 static int service_load_sysv(Service *s) {
1033 /* Load service data from SysV init scripts, preferably with
1034 * LSB headers ... */
1036 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1039 if ((t = UNIT(s)->id))
1040 if ((r = service_load_sysv_name(s, t)) < 0)
1043 if (UNIT(s)->load_state == UNIT_STUB)
1044 SET_FOREACH(t, UNIT(s)->names, i) {
1045 if (t == UNIT(s)->id)
1048 if ((r = service_load_sysv_name(s, t)) < 0)
1051 if (UNIT(s)->load_state != UNIT_STUB)
1059 static int service_verify(Service *s) {
1062 if (UNIT(s)->load_state != UNIT_LOADED)
1065 if (!s->exec_command[SERVICE_EXEC_START]) {
1066 log_error_unit(UNIT(s)->id,
1067 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1071 if (s->type != SERVICE_ONESHOT &&
1072 s->exec_command[SERVICE_EXEC_START]->command_next) {
1073 log_error_unit(UNIT(s)->id,
1074 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1078 if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
1079 log_error_unit(UNIT(s)->id,
1080 "%s has Restart setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1084 if (s->type == SERVICE_DBUS && !s->bus_name) {
1085 log_error_unit(UNIT(s)->id,
1086 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1090 if (s->bus_name && s->type != SERVICE_DBUS)
1091 log_warning_unit(UNIT(s)->id,
1092 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1094 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1095 log_error_unit(UNIT(s)->id,
1096 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1103 static int service_add_default_dependencies(Service *s) {
1108 /* Add a number of automatic dependencies useful for the
1109 * majority of services. */
1111 /* First, pull in base system */
1112 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1113 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1114 SPECIAL_BASIC_TARGET, NULL, true);
1118 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1119 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1120 SPECIAL_SOCKETS_TARGET, NULL, true);
1124 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1125 SPECIAL_TIMERS_TARGET, NULL, true);
1129 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1130 SPECIAL_PATHS_TARGET, NULL, true);
1135 /* Second, activate normal shutdown */
1136 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
1137 SPECIAL_SHUTDOWN_TARGET, NULL, true);
1141 static void service_fix_output(Service *s) {
1144 /* If nothing has been explicitly configured, patch default
1145 * output in. If input is socket/tty we avoid this however,
1146 * since in that case we want output to default to the same
1147 * place as we read input from. */
1149 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1150 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1151 s->exec_context.std_input == EXEC_INPUT_NULL)
1152 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1154 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1155 s->exec_context.std_input == EXEC_INPUT_NULL)
1156 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1159 static int service_load(Unit *u) {
1161 Service *s = SERVICE(u);
1165 /* Load a .service file */
1166 r = unit_load_fragment(u);
1170 #ifdef HAVE_SYSV_COMPAT
1171 /* Load a classic init script as a fallback, if we couldn't find anything */
1172 if (u->load_state == UNIT_STUB) {
1173 r = service_load_sysv(s);
1179 /* Still nothing found? Then let's give up */
1180 if (u->load_state == UNIT_STUB)
1183 /* This is a new unit? Then let's add in some extras */
1184 if (u->load_state == UNIT_LOADED) {
1186 /* We were able to load something, then let's add in
1187 * the dropin directories. */
1188 r = unit_load_dropin(u);
1192 if (s->type == _SERVICE_TYPE_INVALID)
1193 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1195 /* Oneshot services have disabled start timeout by default */
1196 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1197 s->timeout_start_usec = 0;
1199 service_fix_output(s);
1201 r = unit_add_exec_dependencies(u, &s->exec_context);
1205 r = unit_add_default_slice(u);
1209 #ifdef HAVE_SYSV_COMPAT
1210 r = sysv_fix_order(s);
1216 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1219 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1220 s->notify_access = NOTIFY_MAIN;
1222 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1223 s->notify_access = NOTIFY_MAIN;
1225 if (s->type == SERVICE_DBUS || s->bus_name) {
1226 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES,
1227 SPECIAL_DBUS_SOCKET, NULL, true);
1232 if (UNIT(s)->default_dependencies) {
1233 r = service_add_default_dependencies(s);
1238 r = unit_exec_context_defaults(u, &s->exec_context);
1243 return service_verify(s);
1246 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1248 ServiceExecCommand c;
1249 Service *s = SERVICE(u);
1250 const char *prefix2;
1251 _cleanup_free_ char *p2 = NULL;
1255 p2 = strappend(prefix, "\t");
1256 prefix2 = p2 ? p2 : prefix;
1259 "%sService State: %s\n"
1261 "%sReload Result: %s\n"
1262 "%sPermissionsStartOnly: %s\n"
1263 "%sRootDirectoryStartOnly: %s\n"
1264 "%sRemainAfterExit: %s\n"
1265 "%sGuessMainPID: %s\n"
1268 "%sNotifyAccess: %s\n",
1269 prefix, service_state_to_string(s->state),
1270 prefix, service_result_to_string(s->result),
1271 prefix, service_result_to_string(s->reload_result),
1272 prefix, yes_no(s->permissions_start_only),
1273 prefix, yes_no(s->root_directory_start_only),
1274 prefix, yes_no(s->remain_after_exit),
1275 prefix, yes_no(s->guess_main_pid),
1276 prefix, service_type_to_string(s->type),
1277 prefix, service_restart_to_string(s->restart),
1278 prefix, notify_access_to_string(s->notify_access));
1280 if (s->control_pid > 0)
1282 "%sControl PID: %lu\n",
1283 prefix, (unsigned long) s->control_pid);
1285 if (s->main_pid > 0)
1288 "%sMain PID Known: %s\n"
1289 "%sMain PID Alien: %s\n",
1290 prefix, (unsigned long) s->main_pid,
1291 prefix, yes_no(s->main_pid_known),
1292 prefix, yes_no(s->main_pid_alien));
1297 prefix, s->pid_file);
1302 "%sBus Name Good: %s\n",
1303 prefix, s->bus_name,
1304 prefix, yes_no(s->bus_name_good));
1306 kill_context_dump(&s->kill_context, f, prefix);
1307 exec_context_dump(&s->exec_context, f, prefix);
1309 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1311 if (!s->exec_command[c])
1314 fprintf(f, "%s-> %s:\n",
1315 prefix, service_exec_command_to_string(c));
1317 exec_command_dump_list(s->exec_command[c], f, prefix2);
1320 #ifdef HAVE_SYSV_COMPAT
1323 "%sSysV Init Script has LSB Header: %s\n"
1324 "%sSysVEnabled: %s\n",
1325 prefix, yes_no(s->sysv_has_lsb),
1326 prefix, yes_no(s->sysv_enabled));
1328 if (s->sysv_start_priority >= 0)
1330 "%sSysVStartPriority: %i\n",
1331 prefix, s->sysv_start_priority);
1333 if (s->sysv_runlevels)
1334 fprintf(f, "%sSysVRunLevels: %s\n",
1335 prefix, s->sysv_runlevels);
1339 fprintf(f, "%sStatus Text: %s\n",
1340 prefix, s->status_text);
1343 static int service_load_pid_file(Service *s, bool may_warn) {
1344 _cleanup_free_ char *k = NULL;
1353 r = read_one_line_file(s->pid_file, &k);
1356 log_info_unit(UNIT(s)->id,
1357 "PID file %s not readable (yet?) after %s.",
1358 s->pid_file, service_state_to_string(s->state));
1362 r = parse_pid(k, &pid);
1365 log_info_unit(UNIT(s)->id,
1366 "Failed to read PID from file %s: %s",
1367 s->pid_file, strerror(-r));
1371 if (kill(pid, 0) < 0 && errno != EPERM) {
1373 log_info_unit(UNIT(s)->id,
1374 "PID %lu read from file %s does not exist.",
1375 (unsigned long) pid, s->pid_file);
1379 if (s->main_pid_known) {
1380 if (pid == s->main_pid)
1383 log_debug_unit(UNIT(s)->id,
1384 "Main PID changing: %lu -> %lu",
1385 (unsigned long) s->main_pid, (unsigned long) pid);
1386 service_unwatch_main_pid(s);
1387 s->main_pid_known = false;
1389 log_debug_unit(UNIT(s)->id,
1390 "Main PID loaded: %lu", (unsigned long) pid);
1392 r = service_set_main_pid(s, pid);
1396 r = unit_watch_pid(UNIT(s), pid);
1398 /* FIXME: we need to do something here */
1399 log_warning_unit(UNIT(s)->id,
1400 "Failed to watch PID %lu from service %s",
1401 (unsigned long) pid, UNIT(s)->id);
1408 static int service_search_main_pid(Service *s) {
1414 /* If we know it anyway, don't ever fallback to unreliable
1416 if (s->main_pid_known)
1419 if (!s->guess_main_pid)
1422 assert(s->main_pid <= 0);
1424 pid = unit_search_main_pid(UNIT(s));
1428 log_debug_unit(UNIT(s)->id,
1429 "Main PID guessed: %lu", (unsigned long) pid);
1430 r = service_set_main_pid(s, pid);
1434 r = unit_watch_pid(UNIT(s), pid);
1436 /* FIXME: we need to do something here */
1437 log_warning_unit(UNIT(s)->id,
1438 "Failed to watch PID %lu from service %s",
1439 (unsigned long) pid, UNIT(s)->id);
1445 static void service_set_state(Service *s, ServiceState state) {
1446 ServiceState old_state;
1447 const UnitActiveState *table;
1450 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1452 old_state = s->state;
1455 service_unwatch_pid_file(s);
1457 if (state != SERVICE_START_PRE &&
1458 state != SERVICE_START &&
1459 state != SERVICE_START_POST &&
1460 state != SERVICE_RELOAD &&
1461 state != SERVICE_STOP &&
1462 state != SERVICE_STOP_SIGTERM &&
1463 state != SERVICE_STOP_SIGKILL &&
1464 state != SERVICE_STOP_POST &&
1465 state != SERVICE_FINAL_SIGTERM &&
1466 state != SERVICE_FINAL_SIGKILL &&
1467 state != SERVICE_AUTO_RESTART)
1468 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1470 if (state != SERVICE_START &&
1471 state != SERVICE_START_POST &&
1472 state != SERVICE_RUNNING &&
1473 state != SERVICE_RELOAD &&
1474 state != SERVICE_STOP &&
1475 state != SERVICE_STOP_SIGTERM &&
1476 state != SERVICE_STOP_SIGKILL) {
1477 service_unwatch_main_pid(s);
1478 s->main_command = NULL;
1481 if (state != SERVICE_START_PRE &&
1482 state != SERVICE_START &&
1483 state != SERVICE_START_POST &&
1484 state != SERVICE_RELOAD &&
1485 state != SERVICE_STOP &&
1486 state != SERVICE_STOP_SIGTERM &&
1487 state != SERVICE_STOP_SIGKILL &&
1488 state != SERVICE_STOP_POST &&
1489 state != SERVICE_FINAL_SIGTERM &&
1490 state != SERVICE_FINAL_SIGKILL) {
1491 service_unwatch_control_pid(s);
1492 s->control_command = NULL;
1493 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1496 if (state != SERVICE_START_PRE &&
1497 state != SERVICE_START &&
1498 state != SERVICE_START_POST &&
1499 state != SERVICE_RUNNING &&
1500 state != SERVICE_RELOAD &&
1501 state != SERVICE_STOP &&
1502 state != SERVICE_STOP_SIGTERM &&
1503 state != SERVICE_STOP_SIGKILL &&
1504 state != SERVICE_STOP_POST &&
1505 state != SERVICE_FINAL_SIGTERM &&
1506 state != SERVICE_FINAL_SIGKILL &&
1507 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1508 service_close_socket_fd(s);
1509 service_connection_unref(s);
1512 if (state == SERVICE_STOP || state == SERVICE_STOP_SIGTERM)
1513 service_stop_watchdog(s);
1515 /* For the inactive states unit_notify() will trim the cgroup,
1516 * but for exit we have to do that ourselves... */
1517 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1518 unit_destroy_cgroup(UNIT(s));
1520 if (old_state != state)
1521 log_debug_unit(UNIT(s)->id,
1522 "%s changed %s -> %s", UNIT(s)->id,
1523 service_state_to_string(old_state),
1524 service_state_to_string(state));
1526 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1527 s->reload_result = SERVICE_SUCCESS;
1530 static int service_coldplug(Unit *u) {
1531 Service *s = SERVICE(u);
1535 assert(s->state == SERVICE_DEAD);
1537 if (s->deserialized_state != s->state) {
1539 if (s->deserialized_state == SERVICE_START_PRE ||
1540 s->deserialized_state == SERVICE_START ||
1541 s->deserialized_state == SERVICE_START_POST ||
1542 s->deserialized_state == SERVICE_RELOAD ||
1543 s->deserialized_state == SERVICE_STOP ||
1544 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1545 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1546 s->deserialized_state == SERVICE_STOP_POST ||
1547 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1548 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1549 s->deserialized_state == SERVICE_AUTO_RESTART) {
1551 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1554 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1556 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
1562 if ((s->deserialized_state == SERVICE_START &&
1563 (s->type == SERVICE_FORKING ||
1564 s->type == SERVICE_DBUS ||
1565 s->type == SERVICE_ONESHOT ||
1566 s->type == SERVICE_NOTIFY)) ||
1567 s->deserialized_state == SERVICE_START_POST ||
1568 s->deserialized_state == SERVICE_RUNNING ||
1569 s->deserialized_state == SERVICE_RELOAD ||
1570 s->deserialized_state == SERVICE_STOP ||
1571 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1572 s->deserialized_state == SERVICE_STOP_SIGKILL)
1573 if (s->main_pid > 0) {
1574 r = unit_watch_pid(UNIT(s), s->main_pid);
1579 if (s->deserialized_state == SERVICE_START_PRE ||
1580 s->deserialized_state == SERVICE_START ||
1581 s->deserialized_state == SERVICE_START_POST ||
1582 s->deserialized_state == SERVICE_RELOAD ||
1583 s->deserialized_state == SERVICE_STOP ||
1584 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1585 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1586 s->deserialized_state == SERVICE_STOP_POST ||
1587 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1588 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1589 if (s->control_pid > 0) {
1590 r = unit_watch_pid(UNIT(s), s->control_pid);
1595 if (s->deserialized_state == SERVICE_START_POST ||
1596 s->deserialized_state == SERVICE_RUNNING)
1597 service_handle_watchdog(s);
1599 service_set_state(s, s->deserialized_state);
1604 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1608 unsigned rn_fds = 0;
1615 if (s->socket_fd >= 0)
1618 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1623 if (u->type != UNIT_SOCKET)
1628 r = socket_collect_fds(sock, &cfds, &cn_fds);
1641 t = new(int, rn_fds+cn_fds);
1648 memcpy(t, rfds, rn_fds * sizeof(int));
1649 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1654 rn_fds = rn_fds+cn_fds;
1669 static int service_spawn(
1674 bool apply_permissions,
1676 bool apply_tty_stdin,
1677 bool set_notify_socket,
1684 _cleanup_free_ int *fdsbuf = NULL;
1685 unsigned n_fds = 0, n_env = 0;
1686 _cleanup_strv_free_ char
1687 **argv = NULL, **final_env = NULL, **our_env = NULL;
1694 unit_realize_cgroup(UNIT(s));
1697 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1698 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1699 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1701 if (s->socket_fd >= 0) {
1702 fds = &s->socket_fd;
1705 r = service_collect_fds(s, &fdsbuf, &n_fds);
1713 if (timeout && s->timeout_start_usec) {
1714 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true,
1715 s->timeout_start_usec, &s->timer_watch);
1719 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1721 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
1725 our_env = new0(char*, 5);
1731 if (set_notify_socket)
1732 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1737 if (s->main_pid > 0)
1738 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1743 if (s->watchdog_usec > 0)
1744 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1749 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
1750 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1755 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1761 if (is_control && UNIT(s)->cgroup_path) {
1762 path = strappenda(UNIT(s)->cgroup_path, "/control");
1763 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1765 path = UNIT(s)->cgroup_path;
1775 UNIT(s)->manager->confirm_spawn,
1776 UNIT(s)->manager->cgroup_supported,
1779 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1784 r = unit_watch_pid(UNIT(s), pid);
1786 /* FIXME: we need to do something here */
1795 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1800 static int main_pid_good(Service *s) {
1803 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1806 /* If we know the pid file, then lets just check if it is
1808 if (s->main_pid_known) {
1810 /* If it's an alien child let's check if it is still
1812 if (s->main_pid_alien && s->main_pid > 0)
1813 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1815 /* .. otherwise assume we'll get a SIGCHLD for it,
1816 * which we really should wait for to collect exit
1817 * status and code */
1818 return s->main_pid > 0;
1821 /* We don't know the pid */
1825 _pure_ static int control_pid_good(Service *s) {
1828 return s->control_pid > 0;
1831 static int cgroup_good(Service *s) {
1836 if (!UNIT(s)->cgroup_path)
1839 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1846 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1850 if (f != SERVICE_SUCCESS)
1853 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1855 if (allow_restart &&
1856 !s->forbid_restart &&
1857 (s->restart == SERVICE_RESTART_ALWAYS ||
1858 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1859 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1860 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1861 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1862 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1863 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1864 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1865 (s->result != SERVICE_FAILURE_SIGNAL ||
1866 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1869 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
1873 service_set_state(s, SERVICE_AUTO_RESTART);
1876 s->forbid_restart = false;
1878 /* we want fresh tmpdirs in case service is started again immediately */
1879 exec_context_tmp_dirs_done(&s->exec_context);
1881 /* Try to delete the pid file. At this point it will be
1882 * out-of-date, and some software might be confused by it, so
1883 * let's remove it. */
1885 unlink_noerrno(s->pid_file);
1890 log_warning_unit(UNIT(s)->id,
1891 "%s failed to run install restart timer: %s",
1892 UNIT(s)->id, strerror(-r));
1893 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1896 static void service_enter_stop_post(Service *s, ServiceResult f) {
1900 if (f != SERVICE_SUCCESS)
1903 service_unwatch_control_pid(s);
1905 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1906 if (s->control_command) {
1907 s->control_command_id = SERVICE_EXEC_STOP_POST;
1909 r = service_spawn(s,
1913 !s->permissions_start_only,
1914 !s->root_directory_start_only,
1923 service_set_state(s, SERVICE_STOP_POST);
1925 service_enter_dead(s, SERVICE_SUCCESS, true);
1930 log_warning_unit(UNIT(s)->id,
1931 "%s failed to run 'stop-post' task: %s",
1932 UNIT(s)->id, strerror(-r));
1933 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1936 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1941 if (f != SERVICE_SUCCESS)
1944 r = unit_kill_context(
1947 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1955 if (s->timeout_stop_usec > 0) {
1956 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true,
1957 s->timeout_stop_usec, &s->timer_watch);
1962 service_set_state(s, state);
1963 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1964 service_enter_stop_post(s, SERVICE_SUCCESS);
1966 service_enter_dead(s, SERVICE_SUCCESS, true);
1971 log_warning_unit(UNIT(s)->id,
1972 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1974 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1975 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1977 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1980 static void service_enter_stop(Service *s, ServiceResult f) {
1985 if (f != SERVICE_SUCCESS)
1988 service_unwatch_control_pid(s);
1990 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
1991 if (s->control_command) {
1992 s->control_command_id = SERVICE_EXEC_STOP;
1994 r = service_spawn(s,
1998 !s->permissions_start_only,
1999 !s->root_directory_start_only,
2007 service_set_state(s, SERVICE_STOP);
2009 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2014 log_warning_unit(UNIT(s)->id,
2015 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2016 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2019 static void service_enter_running(Service *s, ServiceResult f) {
2020 int main_pid_ok, cgroup_ok;
2023 if (f != SERVICE_SUCCESS)
2026 main_pid_ok = main_pid_good(s);
2027 cgroup_ok = cgroup_good(s);
2029 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2030 (s->bus_name_good || s->type != SERVICE_DBUS))
2031 service_set_state(s, SERVICE_RUNNING);
2032 else if (s->remain_after_exit)
2033 service_set_state(s, SERVICE_EXITED);
2035 service_enter_stop(s, SERVICE_SUCCESS);
2038 static void service_enter_start_post(Service *s) {
2042 service_unwatch_control_pid(s);
2044 if (s->watchdog_usec > 0)
2045 service_reset_watchdog(s);
2047 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
2048 if (s->control_command) {
2049 s->control_command_id = SERVICE_EXEC_START_POST;
2051 r = service_spawn(s,
2055 !s->permissions_start_only,
2056 !s->root_directory_start_only,
2064 service_set_state(s, SERVICE_START_POST);
2066 service_enter_running(s, SERVICE_SUCCESS);
2071 log_warning_unit(UNIT(s)->id,
2072 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2073 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2076 static void service_kill_control_processes(Service *s) {
2079 if (!UNIT(s)->cgroup_path)
2082 p = strappenda(UNIT(s)->cgroup_path, "/control");
2083 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
2086 static void service_enter_start(Service *s) {
2093 assert(s->exec_command[SERVICE_EXEC_START]);
2094 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2096 service_unwatch_control_pid(s);
2097 service_unwatch_main_pid(s);
2099 /* We want to ensure that nobody leaks processes from
2100 * START_PRE here, so let's go on a killing spree, People
2101 * should not spawn long running processes from START_PRE. */
2102 service_kill_control_processes(s);
2104 if (s->type == SERVICE_FORKING) {
2105 s->control_command_id = SERVICE_EXEC_START;
2106 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2108 s->main_command = NULL;
2110 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2111 s->control_command = NULL;
2113 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2116 r = service_spawn(s,
2118 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
2119 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2124 s->notify_access != NOTIFY_NONE,
2130 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2131 /* For simple services we immediately start
2132 * the START_POST binaries. */
2134 service_set_main_pid(s, pid);
2135 service_enter_start_post(s);
2137 } else if (s->type == SERVICE_FORKING) {
2139 /* For forking services we wait until the start
2140 * process exited. */
2142 s->control_pid = pid;
2143 service_set_state(s, SERVICE_START);
2145 } else if (s->type == SERVICE_ONESHOT ||
2146 s->type == SERVICE_DBUS ||
2147 s->type == SERVICE_NOTIFY) {
2149 /* For oneshot services we wait until the start
2150 * process exited, too, but it is our main process. */
2152 /* For D-Bus services we know the main pid right away,
2153 * but wait for the bus name to appear on the
2154 * bus. Notify services are similar. */
2156 service_set_main_pid(s, pid);
2157 service_set_state(s, SERVICE_START);
2159 assert_not_reached("Unknown service type");
2164 log_warning_unit(UNIT(s)->id,
2165 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2166 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2169 static void service_enter_start_pre(Service *s) {
2174 service_unwatch_control_pid(s);
2176 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
2177 if (s->control_command) {
2178 /* Before we start anything, let's clear up what might
2179 * be left from previous runs. */
2180 service_kill_control_processes(s);
2182 s->control_command_id = SERVICE_EXEC_START_PRE;
2184 r = service_spawn(s,
2188 !s->permissions_start_only,
2189 !s->root_directory_start_only,
2197 service_set_state(s, SERVICE_START_PRE);
2199 service_enter_start(s);
2204 log_warning_unit(UNIT(s)->id,
2205 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2206 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2209 static void service_enter_restart(Service *s) {
2214 dbus_error_init(&error);
2216 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2217 /* Don't restart things if we are going down anyway */
2218 log_info_unit(UNIT(s)->id,
2219 "Stop job pending for unit, delaying automatic restart.");
2221 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
2228 /* Any units that are bound to this service must also be
2229 * restarted. We use JOB_RESTART (instead of the more obvious
2230 * JOB_START) here so that those dependency jobs will be added
2232 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2236 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2237 * it will be canceled as part of the service_stop() call that
2238 * is executed as part of JOB_RESTART. */
2240 log_debug_unit(UNIT(s)->id,
2241 "%s scheduled restart job.", UNIT(s)->id);
2245 log_warning_unit(UNIT(s)->id,
2246 "%s failed to schedule restart job: %s",
2247 UNIT(s)->id, bus_error(&error, -r));
2248 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2250 dbus_error_free(&error);
2253 static void service_enter_reload(Service *s) {
2258 service_unwatch_control_pid(s);
2260 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
2261 if (s->control_command) {
2262 s->control_command_id = SERVICE_EXEC_RELOAD;
2264 r = service_spawn(s,
2268 !s->permissions_start_only,
2269 !s->root_directory_start_only,
2277 service_set_state(s, SERVICE_RELOAD);
2279 service_enter_running(s, SERVICE_SUCCESS);
2284 log_warning_unit(UNIT(s)->id,
2285 "%s failed to run 'reload' task: %s",
2286 UNIT(s)->id, strerror(-r));
2287 s->reload_result = SERVICE_FAILURE_RESOURCES;
2288 service_enter_running(s, SERVICE_SUCCESS);
2291 static void service_run_next_control(Service *s) {
2295 assert(s->control_command);
2296 assert(s->control_command->command_next);
2298 assert(s->control_command_id != SERVICE_EXEC_START);
2300 s->control_command = s->control_command->command_next;
2301 service_unwatch_control_pid(s);
2303 r = service_spawn(s,
2307 !s->permissions_start_only,
2308 !s->root_directory_start_only,
2309 s->control_command_id == SERVICE_EXEC_START_PRE ||
2310 s->control_command_id == SERVICE_EXEC_STOP_POST,
2320 log_warning_unit(UNIT(s)->id,
2321 "%s failed to run next control task: %s",
2322 UNIT(s)->id, strerror(-r));
2324 if (s->state == SERVICE_START_PRE)
2325 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2326 else if (s->state == SERVICE_STOP)
2327 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2328 else if (s->state == SERVICE_STOP_POST)
2329 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2330 else if (s->state == SERVICE_RELOAD) {
2331 s->reload_result = SERVICE_FAILURE_RESOURCES;
2332 service_enter_running(s, SERVICE_SUCCESS);
2334 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2337 static void service_run_next_main(Service *s) {
2342 assert(s->main_command);
2343 assert(s->main_command->command_next);
2344 assert(s->type == SERVICE_ONESHOT);
2346 s->main_command = s->main_command->command_next;
2347 service_unwatch_main_pid(s);
2349 r = service_spawn(s,
2356 s->notify_access != NOTIFY_NONE,
2362 service_set_main_pid(s, pid);
2367 log_warning_unit(UNIT(s)->id,
2368 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2369 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2372 static int service_start_limit_test(Service *s) {
2375 if (ratelimit_test(&s->start_limit))
2378 switch (s->start_limit_action) {
2380 case SERVICE_START_LIMIT_NONE:
2381 log_warning_unit(UNIT(s)->id,
2382 "%s start request repeated too quickly, refusing to start.",
2386 case SERVICE_START_LIMIT_REBOOT: {
2390 dbus_error_init(&error);
2392 log_warning_unit(UNIT(s)->id,
2393 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2395 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
2396 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
2397 true, &error, NULL);
2399 log_error_unit(UNIT(s)->id,
2400 "Failed to reboot: %s.", bus_error(&error, r));
2401 dbus_error_free(&error);
2407 case SERVICE_START_LIMIT_REBOOT_FORCE:
2408 log_warning_unit(UNIT(s)->id,
2409 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2410 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2413 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2414 log_warning_unit(UNIT(s)->id,
2415 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2417 reboot(RB_AUTOBOOT);
2421 log_error_unit(UNIT(s)->id,
2422 "start limit action=%i", s->start_limit_action);
2423 assert_not_reached("Unknown StartLimitAction.");
2429 static int service_start(Unit *u) {
2430 Service *s = SERVICE(u);
2435 /* We cannot fulfill this request right now, try again later
2437 if (s->state == SERVICE_STOP ||
2438 s->state == SERVICE_STOP_SIGTERM ||
2439 s->state == SERVICE_STOP_SIGKILL ||
2440 s->state == SERVICE_STOP_POST ||
2441 s->state == SERVICE_FINAL_SIGTERM ||
2442 s->state == SERVICE_FINAL_SIGKILL)
2445 /* Already on it! */
2446 if (s->state == SERVICE_START_PRE ||
2447 s->state == SERVICE_START ||
2448 s->state == SERVICE_START_POST)
2451 /* A service that will be restarted must be stopped first to
2452 * trigger BindsTo and/or OnFailure dependencies. If a user
2453 * does not want to wait for the holdoff time to elapse, the
2454 * service should be manually restarted, not started. We
2455 * simply return EAGAIN here, so that any start jobs stay
2456 * queued, and assume that the auto restart timer will
2457 * eventually trigger the restart. */
2458 if (s->state == SERVICE_AUTO_RESTART)
2461 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2463 /* Make sure we don't enter a busy loop of some kind. */
2464 r = service_start_limit_test(s);
2466 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2470 s->result = SERVICE_SUCCESS;
2471 s->reload_result = SERVICE_SUCCESS;
2472 s->main_pid_known = false;
2473 s->main_pid_alien = false;
2474 s->forbid_restart = false;
2476 service_enter_start_pre(s);
2480 static int service_stop(Unit *u) {
2481 Service *s = SERVICE(u);
2485 /* Don't create restart jobs from here. */
2486 s->forbid_restart = true;
2489 if (s->state == SERVICE_STOP ||
2490 s->state == SERVICE_STOP_SIGTERM ||
2491 s->state == SERVICE_STOP_SIGKILL ||
2492 s->state == SERVICE_STOP_POST ||
2493 s->state == SERVICE_FINAL_SIGTERM ||
2494 s->state == SERVICE_FINAL_SIGKILL)
2497 /* A restart will be scheduled or is in progress. */
2498 if (s->state == SERVICE_AUTO_RESTART) {
2499 service_set_state(s, SERVICE_DEAD);
2503 /* If there's already something running we go directly into
2505 if (s->state == SERVICE_START_PRE ||
2506 s->state == SERVICE_START ||
2507 s->state == SERVICE_START_POST ||
2508 s->state == SERVICE_RELOAD) {
2509 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2513 assert(s->state == SERVICE_RUNNING ||
2514 s->state == SERVICE_EXITED);
2516 service_enter_stop(s, SERVICE_SUCCESS);
2520 static int service_reload(Unit *u) {
2521 Service *s = SERVICE(u);
2525 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2527 service_enter_reload(s);
2531 _pure_ static bool service_can_reload(Unit *u) {
2532 Service *s = SERVICE(u);
2536 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2539 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2540 Service *s = SERVICE(u);
2546 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2547 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2548 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2550 if (s->control_pid > 0)
2551 unit_serialize_item_format(u, f, "control-pid", "%lu",
2552 (unsigned long) s->control_pid);
2554 if (s->main_pid_known && s->main_pid > 0)
2555 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2557 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2560 unit_serialize_item(u, f, "status-text", s->status_text);
2562 /* FIXME: There's a minor uncleanliness here: if there are
2563 * multiple commands attached here, we will start from the
2564 * first one again */
2565 if (s->control_command_id >= 0)
2566 unit_serialize_item(u, f, "control-command",
2567 service_exec_command_to_string(s->control_command_id));
2569 if (s->socket_fd >= 0) {
2572 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2575 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2578 if (s->main_exec_status.pid > 0) {
2579 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu",
2580 (unsigned long) s->main_exec_status.pid);
2581 dual_timestamp_serialize(f, "main-exec-status-start",
2582 &s->main_exec_status.start_timestamp);
2583 dual_timestamp_serialize(f, "main-exec-status-exit",
2584 &s->main_exec_status.exit_timestamp);
2586 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2587 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
2588 s->main_exec_status.code);
2589 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
2590 s->main_exec_status.status);
2593 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2594 dual_timestamp_serialize(f, "watchdog-timestamp",
2595 &s->watchdog_timestamp);
2597 if (s->exec_context.tmp_dir)
2598 unit_serialize_item(u, f, "tmp-dir", s->exec_context.tmp_dir);
2600 if (s->exec_context.var_tmp_dir)
2601 unit_serialize_item(u, f, "var-tmp-dir", s->exec_context.var_tmp_dir);
2603 if (s->forbid_restart)
2604 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
2609 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2610 Service *s = SERVICE(u);
2617 if (streq(key, "state")) {
2620 state = service_state_from_string(value);
2622 log_debug_unit(u->id, "Failed to parse state value %s", value);
2624 s->deserialized_state = state;
2625 } else if (streq(key, "result")) {
2628 f = service_result_from_string(value);
2630 log_debug_unit(u->id, "Failed to parse result value %s", value);
2631 else if (f != SERVICE_SUCCESS)
2634 } else if (streq(key, "reload-result")) {
2637 f = service_result_from_string(value);
2639 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2640 else if (f != SERVICE_SUCCESS)
2641 s->reload_result = f;
2643 } else if (streq(key, "control-pid")) {
2646 if (parse_pid(value, &pid) < 0)
2647 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2649 s->control_pid = pid;
2650 } else if (streq(key, "main-pid")) {
2653 if (parse_pid(value, &pid) < 0)
2654 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2656 service_set_main_pid(s, pid);
2657 unit_watch_pid(UNIT(s), pid);
2659 } else if (streq(key, "main-pid-known")) {
2662 b = parse_boolean(value);
2664 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2666 s->main_pid_known = b;
2667 } else if (streq(key, "status-text")) {
2674 free(s->status_text);
2678 } else if (streq(key, "control-command")) {
2679 ServiceExecCommand id;
2681 id = service_exec_command_from_string(value);
2683 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2685 s->control_command_id = id;
2686 s->control_command = s->exec_command[id];
2688 } else if (streq(key, "socket-fd")) {
2691 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2692 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2695 if (s->socket_fd >= 0)
2696 close_nointr_nofail(s->socket_fd);
2697 s->socket_fd = fdset_remove(fds, fd);
2699 } else if (streq(key, "main-exec-status-pid")) {
2702 if (parse_pid(value, &pid) < 0)
2703 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2705 s->main_exec_status.pid = pid;
2706 } else if (streq(key, "main-exec-status-code")) {
2709 if (safe_atoi(value, &i) < 0)
2710 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2712 s->main_exec_status.code = i;
2713 } else if (streq(key, "main-exec-status-status")) {
2716 if (safe_atoi(value, &i) < 0)
2717 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2719 s->main_exec_status.status = i;
2720 } else if (streq(key, "main-exec-status-start"))
2721 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2722 else if (streq(key, "main-exec-status-exit"))
2723 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2724 else if (streq(key, "watchdog-timestamp"))
2725 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2726 else if (streq(key, "tmp-dir")) {
2733 s->exec_context.tmp_dir = t;
2734 } else if (streq(key, "var-tmp-dir")) {
2741 s->exec_context.var_tmp_dir = t;
2742 } else if (streq(key, "forbid-restart")) {
2745 b = parse_boolean(value);
2747 log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value);
2749 s->forbid_restart = b;
2751 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2756 _pure_ 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 _pure_ 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 log_debug_unit(UNIT(s)->id,
2828 "Trying to read %s's PID file %s in case it changed",
2829 UNIT(s)->id, s->pid_file_pathspec->path);
2830 service_retry_pid_file(s);
2834 log_error_unit(UNIT(s)->id,
2835 "Failed to set a watch for %s's PID file %s: %s",
2836 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2837 service_unwatch_pid_file(s);
2841 static int service_demand_pid_file(Service *s) {
2844 assert(s->pid_file);
2845 assert(!s->pid_file_pathspec);
2847 ps = new0(PathSpec, 1);
2851 ps->path = strdup(s->pid_file);
2857 path_kill_slashes(ps->path);
2859 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2860 * keep their PID file open all the time. */
2861 ps->type = PATH_MODIFIED;
2862 ps->inotify_fd = -1;
2864 s->pid_file_pathspec = ps;
2866 return service_watch_pid_file(s);
2869 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2870 Service *s = SERVICE(u);
2874 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2875 assert(s->pid_file_pathspec);
2876 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2878 log_debug_unit(u->id, "inotify event for %s", u->id);
2880 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2883 if (service_retry_pid_file(s) == 0)
2886 if (service_watch_pid_file(s) < 0)
2891 service_unwatch_pid_file(s);
2892 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2895 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2896 Service *s = SERVICE(u);
2902 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2903 is_clean_exit_lsb(code, status, &s->success_status))
2904 f = SERVICE_SUCCESS;
2905 else if (code == CLD_EXITED)
2906 f = SERVICE_FAILURE_EXIT_CODE;
2907 else if (code == CLD_KILLED)
2908 f = SERVICE_FAILURE_SIGNAL;
2909 else if (code == CLD_DUMPED)
2910 f = SERVICE_FAILURE_CORE_DUMP;
2912 assert_not_reached("Unknown code");
2914 if (s->main_pid == pid) {
2915 /* Forking services may occasionally move to a new PID.
2916 * As long as they update the PID file before exiting the old
2917 * PID, they're fine. */
2918 if (service_load_pid_file(s, false) == 0)
2922 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2924 if (s->main_command) {
2925 /* If this is not a forking service than the
2926 * main process got started and hence we copy
2927 * the exit status so that it is recorded both
2928 * as main and as control process exit
2931 s->main_command->exec_status = s->main_exec_status;
2933 if (s->main_command->ignore)
2934 f = SERVICE_SUCCESS;
2935 } else if (s->exec_command[SERVICE_EXEC_START]) {
2937 /* If this is a forked process, then we should
2938 * ignore the return value if this was
2939 * configured for the starter process */
2941 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2942 f = SERVICE_SUCCESS;
2945 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2947 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2948 u->id, sigchld_code_to_string(code), status,
2949 strna(code == CLD_EXITED
2950 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2951 : signal_to_string(status)),
2952 "EXIT_CODE=%s", sigchld_code_to_string(code),
2953 "EXIT_STATUS=%i", status,
2956 if (f != SERVICE_SUCCESS)
2959 if (s->main_command &&
2960 s->main_command->command_next &&
2961 f == SERVICE_SUCCESS) {
2963 /* There is another command to *
2964 * execute, so let's do that. */
2966 log_debug_unit(u->id,
2967 "%s running next main command for state %s",
2968 u->id, service_state_to_string(s->state));
2969 service_run_next_main(s);
2973 /* The service exited, so the service is officially
2975 s->main_command = NULL;
2979 case SERVICE_START_POST:
2980 case SERVICE_RELOAD:
2982 /* Need to wait until the operation is
2987 if (s->type == SERVICE_ONESHOT) {
2988 /* This was our main goal, so let's go on */
2989 if (f == SERVICE_SUCCESS)
2990 service_enter_start_post(s);
2992 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2998 case SERVICE_RUNNING:
2999 service_enter_running(s, f);
3002 case SERVICE_STOP_SIGTERM:
3003 case SERVICE_STOP_SIGKILL:
3005 if (!control_pid_good(s))
3006 service_enter_stop_post(s, f);
3008 /* If there is still a control process, wait for that first */
3012 assert_not_reached("Uh, main process died at wrong time.");
3016 } else if (s->control_pid == pid) {
3019 if (s->control_command) {
3020 exec_status_exit(&s->control_command->exec_status,
3021 &s->exec_context, pid, code, status);
3023 if (s->control_command->ignore)
3024 f = SERVICE_SUCCESS;
3027 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
3028 "%s: control process exited, code=%s status=%i",
3029 u->id, sigchld_code_to_string(code), status);
3031 if (f != SERVICE_SUCCESS)
3034 /* Immediately get rid of the cgroup, so that the
3035 * kernel doesn't delay the cgroup empty messages for
3036 * the service cgroup any longer than necessary */
3037 service_kill_control_processes(s);
3039 if (s->control_command &&
3040 s->control_command->command_next &&
3041 f == SERVICE_SUCCESS) {
3043 /* There is another command to *
3044 * execute, so let's do that. */
3046 log_debug_unit(u->id,
3047 "%s running next control command for state %s",
3048 u->id, service_state_to_string(s->state));
3049 service_run_next_control(s);
3052 /* No further commands for this step, so let's
3053 * figure out what to do next */
3055 s->control_command = NULL;
3056 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3058 log_debug_unit(u->id,
3059 "%s got final SIGCHLD for state %s",
3060 u->id, service_state_to_string(s->state));
3064 case SERVICE_START_PRE:
3065 if (f == SERVICE_SUCCESS)
3066 service_enter_start(s);
3068 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3072 if (s->type != SERVICE_FORKING)
3073 /* Maybe spurious event due to a reload that changed the type? */
3076 if (f != SERVICE_SUCCESS) {
3077 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3082 bool has_start_post;
3085 /* Let's try to load the pid file here if we can.
3086 * The PID file might actually be created by a START_POST
3087 * script. In that case don't worry if the loading fails. */
3089 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3090 r = service_load_pid_file(s, !has_start_post);
3091 if (!has_start_post && r < 0) {
3092 r = service_demand_pid_file(s);
3093 if (r < 0 || !cgroup_good(s))
3094 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3098 service_search_main_pid(s);
3100 service_enter_start_post(s);
3103 case SERVICE_START_POST:
3104 if (f != SERVICE_SUCCESS) {
3105 service_enter_stop(s, f);
3112 r = service_load_pid_file(s, true);
3114 r = service_demand_pid_file(s);
3115 if (r < 0 || !cgroup_good(s))
3116 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3120 service_search_main_pid(s);
3122 service_enter_running(s, SERVICE_SUCCESS);
3125 case SERVICE_RELOAD:
3126 if (f == SERVICE_SUCCESS) {
3127 service_load_pid_file(s, true);
3128 service_search_main_pid(s);
3131 s->reload_result = f;
3132 service_enter_running(s, SERVICE_SUCCESS);
3136 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3139 case SERVICE_STOP_SIGTERM:
3140 case SERVICE_STOP_SIGKILL:
3141 if (main_pid_good(s) <= 0)
3142 service_enter_stop_post(s, f);
3144 /* If there is still a service
3145 * process around, wait until
3146 * that one quit, too */
3149 case SERVICE_STOP_POST:
3150 case SERVICE_FINAL_SIGTERM:
3151 case SERVICE_FINAL_SIGKILL:
3152 service_enter_dead(s, f, true);
3156 assert_not_reached("Uh, control process died at wrong time.");
3161 /* Notify clients about changed exit status */
3162 unit_add_to_dbus_queue(u);
3165 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3166 Service *s = SERVICE(u);
3169 assert(elapsed == 1);
3171 if (w == &s->watchdog_watch) {
3172 service_handle_watchdog(s);
3176 assert(w == &s->timer_watch);
3180 case SERVICE_START_PRE:
3182 log_warning_unit(u->id,
3183 "%s operation timed out. Terminating.", u->id);
3184 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3187 case SERVICE_START_POST:
3188 log_warning_unit(u->id,
3189 "%s operation timed out. Stopping.", u->id);
3190 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3193 case SERVICE_RELOAD:
3194 log_warning_unit(u->id,
3195 "%s operation timed out. Stopping.", u->id);
3196 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3197 service_enter_running(s, SERVICE_SUCCESS);
3201 log_warning_unit(u->id,
3202 "%s stopping timed out. Terminating.", u->id);
3203 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3206 case SERVICE_STOP_SIGTERM:
3207 if (s->kill_context.send_sigkill) {
3208 log_warning_unit(u->id,
3209 "%s stopping timed out. Killing.", u->id);
3210 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3212 log_warning_unit(u->id,
3213 "%s stopping timed out. Skipping SIGKILL.", u->id);
3214 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3219 case SERVICE_STOP_SIGKILL:
3220 /* Uh, we sent a SIGKILL and it is still not gone?
3221 * Must be something we cannot kill, so let's just be
3222 * weirded out and continue */
3224 log_warning_unit(u->id,
3225 "%s still around after SIGKILL. Ignoring.", u->id);
3226 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3229 case SERVICE_STOP_POST:
3230 log_warning_unit(u->id,
3231 "%s stopping timed out (2). Terminating.", u->id);
3232 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3235 case SERVICE_FINAL_SIGTERM:
3236 if (s->kill_context.send_sigkill) {
3237 log_warning_unit(u->id,
3238 "%s stopping timed out (2). Killing.", u->id);
3239 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3241 log_warning_unit(u->id,
3242 "%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.",
3244 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3249 case SERVICE_FINAL_SIGKILL:
3250 log_warning_unit(u->id,
3251 "%s still around after SIGKILL (2). Entering failed mode.", u->id);
3252 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3255 case SERVICE_AUTO_RESTART:
3256 log_info_unit(u->id,
3257 "%s holdoff time over, scheduling restart.", u->id);
3258 service_enter_restart(s);
3262 assert_not_reached("Timeout at wrong time.");
3266 static void service_notify_cgroup_empty_event(Unit *u) {
3267 Service *s = SERVICE(u);
3271 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
3275 /* Waiting for SIGCHLD is usually more interesting,
3276 * because it includes return codes/signals. Which is
3277 * why we ignore the cgroup events for most cases,
3278 * except when we don't know pid which to expect the
3282 case SERVICE_START_POST:
3283 /* If we were hoping for the daemon to write its PID file,
3284 * we can give up now. */
3285 if (s->pid_file_pathspec) {
3286 log_warning_unit(u->id,
3287 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3288 service_unwatch_pid_file(s);
3289 if (s->state == SERVICE_START)
3290 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3292 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3296 case SERVICE_RUNNING:
3297 /* service_enter_running() will figure out what to do */
3298 service_enter_running(s, SERVICE_SUCCESS);
3301 case SERVICE_STOP_SIGTERM:
3302 case SERVICE_STOP_SIGKILL:
3304 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3305 service_enter_stop_post(s, SERVICE_SUCCESS);
3309 case SERVICE_FINAL_SIGTERM:
3310 case SERVICE_FINAL_SIGKILL:
3311 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3312 service_enter_dead(s, SERVICE_SUCCESS, true);
3321 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3322 Service *s = SERVICE(u);
3327 if (s->notify_access == NOTIFY_NONE) {
3328 log_warning_unit(u->id,
3329 "%s: Got notification message from PID %lu, but reception is disabled.",
3330 u->id, (unsigned long) pid);
3334 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3335 log_warning_unit(u->id,
3336 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3337 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3341 log_debug_unit(u->id,
3342 "%s: Got message", u->id);
3344 /* Interpret MAINPID= */
3345 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3346 (s->state == SERVICE_START ||
3347 s->state == SERVICE_START_POST ||
3348 s->state == SERVICE_RUNNING ||
3349 s->state == SERVICE_RELOAD)) {
3351 if (parse_pid(e + 8, &pid) < 0)
3352 log_warning_unit(u->id,
3353 "Failed to parse notification message %s", e);
3355 log_debug_unit(u->id,
3356 "%s: got %s", u->id, e);
3357 service_set_main_pid(s, pid);
3358 unit_watch_pid(UNIT(s), pid);
3362 /* Interpret READY= */
3363 if (s->type == SERVICE_NOTIFY &&
3364 s->state == SERVICE_START &&
3365 strv_find(tags, "READY=1")) {
3366 log_debug_unit(u->id,
3367 "%s: got READY=1", u->id);
3369 service_enter_start_post(s);
3372 /* Interpret STATUS= */
3373 e = strv_find_prefix(tags, "STATUS=");
3379 if (!utf8_is_valid(e+7)) {
3380 log_warning_unit(u->id,
3381 "Status message in notification is not UTF-8 clean.");
3387 log_error_unit(u->id,
3388 "Failed to allocate string.");
3392 log_debug_unit(u->id,
3393 "%s: got %s", u->id, e);
3395 free(s->status_text);
3398 free(s->status_text);
3399 s->status_text = NULL;
3403 if (strv_find(tags, "WATCHDOG=1")) {
3404 log_debug_unit(u->id,
3405 "%s: got WATCHDOG=1", u->id);
3406 if (dual_timestamp_is_set(&s->watchdog_timestamp))
3407 service_reset_watchdog(s);
3410 /* Notify clients about changed status or main pid */
3411 unit_add_to_dbus_queue(u);
3414 #ifdef HAVE_SYSV_COMPAT
3416 static int service_enumerate(Manager *m) {
3419 _cleanup_closedir_ DIR *d = NULL;
3420 _cleanup_free_ char *path = NULL, *fpath = NULL, *name = NULL;
3421 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
3422 _cleanup_set_free_ Set *shutdown_services = NULL;
3429 if (m->running_as != SYSTEMD_SYSTEM)
3432 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3433 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3437 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3448 if (errno != ENOENT)
3449 log_warning("opendir(%s) failed: %s", path, strerror(errno));
3454 while ((de = readdir(d))) {
3457 if (ignore_file(de->d_name))
3460 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3463 if (strlen(de->d_name) < 4)
3466 a = undecchar(de->d_name[1]);
3467 b = undecchar(de->d_name[2]);
3473 fpath = strjoin(path, "/", de->d_name, NULL);
3479 if (access(fpath, X_OK) < 0) {
3481 if (errno != ENOENT)
3482 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3488 name = sysv_translate_name(de->d_name + 3);
3494 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3496 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3500 if (de->d_name[0] == 'S') {
3502 if (rcnd_table[i].type == RUNLEVEL_UP) {
3503 SERVICE(service)->sysv_start_priority_from_rcnd =
3504 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3506 SERVICE(service)->sysv_enabled = true;
3509 r = set_ensure_allocated(&runlevel_services[i],
3510 trivial_hash_func, trivial_compare_func);
3514 r = set_put(runlevel_services[i], service);
3518 } else if (de->d_name[0] == 'K' &&
3519 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3521 r = set_ensure_allocated(&shutdown_services,
3522 trivial_hash_func, trivial_compare_func);
3526 r = set_put(shutdown_services, service);
3533 /* Now we loaded all stubs and are aware of the lowest
3534 start-up priority for all services, not let's actually load
3535 the services, this will also tell us which services are
3536 actually native now */
3537 manager_dispatch_load_queue(m);
3539 /* If this is a native service, rely on native ways to pull in
3540 * a service, don't pull it in via sysv rcN.d links. */
3541 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3542 SET_FOREACH(service, runlevel_services[i], j) {
3543 service = unit_follow_merge(service);
3545 if (service->fragment_path)
3548 r = unit_add_two_dependencies_by_name_inverse(
3549 service, UNIT_AFTER, UNIT_WANTS,
3550 rcnd_table[i].target, NULL, true);
3555 /* We honour K links only for halt/reboot. For the normal
3556 * runlevels we assume the stop jobs will be implicitly added
3557 * by the core logic. Also, we don't really distinguish here
3558 * between the runlevels 0 and 6 and just add them to the
3559 * special shutdown target. */
3560 SET_FOREACH(service, shutdown_services, j) {
3561 service = unit_follow_merge(service);
3563 if (service->fragment_path)
3566 r = unit_add_two_dependencies_by_name(
3567 service, UNIT_BEFORE, UNIT_CONFLICTS,
3568 SPECIAL_SHUTDOWN_TARGET, NULL, true);
3577 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3578 set_free(runlevel_services[i]);
3584 static void service_bus_name_owner_change(
3587 const char *old_owner,
3588 const char *new_owner) {
3590 Service *s = SERVICE(u);
3595 assert(streq(s->bus_name, name));
3596 assert(old_owner || new_owner);
3598 if (old_owner && new_owner)
3599 log_debug_unit(u->id,
3600 "%s's D-Bus name %s changed owner from %s to %s",
3601 u->id, name, old_owner, new_owner);
3603 log_debug_unit(u->id,
3604 "%s's D-Bus name %s no longer registered by %s",
3605 u->id, name, old_owner);
3607 log_debug_unit(u->id,
3608 "%s's D-Bus name %s now registered by %s",
3609 u->id, name, new_owner);
3611 s->bus_name_good = !!new_owner;
3613 if (s->type == SERVICE_DBUS) {
3615 /* service_enter_running() will figure out what to
3617 if (s->state == SERVICE_RUNNING)
3618 service_enter_running(s, SERVICE_SUCCESS);
3619 else if (s->state == SERVICE_START && new_owner)
3620 service_enter_start_post(s);
3622 } else if (new_owner &&
3624 (s->state == SERVICE_START ||
3625 s->state == SERVICE_START_POST ||
3626 s->state == SERVICE_RUNNING ||
3627 s->state == SERVICE_RELOAD)) {
3629 /* Try to acquire PID from bus service */
3630 log_debug_unit(u->id,
3631 "Trying to acquire PID from D-Bus name...");
3633 bus_query_pid(u->manager, name);
3637 static void service_bus_query_pid_done(
3642 Service *s = SERVICE(u);
3647 log_debug_unit(u->id,
3648 "%s's D-Bus name %s is now owned by process %u",
3649 u->id, name, (unsigned) pid);
3651 if (s->main_pid <= 0 &&
3652 (s->state == SERVICE_START ||
3653 s->state == SERVICE_START_POST ||
3654 s->state == SERVICE_RUNNING ||
3655 s->state == SERVICE_RELOAD)){
3656 service_set_main_pid(s, pid);
3657 unit_watch_pid(UNIT(s), pid);
3661 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3666 /* This is called by the socket code when instantiating a new
3667 * service for a stream socket and the socket needs to be
3670 if (UNIT(s)->load_state != UNIT_LOADED)
3673 if (s->socket_fd >= 0)
3676 if (s->state != SERVICE_DEAD)
3680 s->got_socket_fd = true;
3682 unit_ref_set(&s->accept_socket, UNIT(sock));
3684 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3687 static void service_reset_failed(Unit *u) {
3688 Service *s = SERVICE(u);
3692 if (s->state == SERVICE_FAILED)
3693 service_set_state(s, SERVICE_DEAD);
3695 s->result = SERVICE_SUCCESS;
3696 s->reload_result = SERVICE_SUCCESS;
3698 RATELIMIT_RESET(s->start_limit);
3701 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3702 Service *s = SERVICE(u);
3704 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
3707 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3708 [SERVICE_DEAD] = "dead",
3709 [SERVICE_START_PRE] = "start-pre",
3710 [SERVICE_START] = "start",
3711 [SERVICE_START_POST] = "start-post",
3712 [SERVICE_RUNNING] = "running",
3713 [SERVICE_EXITED] = "exited",
3714 [SERVICE_RELOAD] = "reload",
3715 [SERVICE_STOP] = "stop",
3716 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3717 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3718 [SERVICE_STOP_POST] = "stop-post",
3719 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3720 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3721 [SERVICE_FAILED] = "failed",
3722 [SERVICE_AUTO_RESTART] = "auto-restart",
3725 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3727 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3728 [SERVICE_RESTART_NO] = "no",
3729 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3730 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3731 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
3732 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3733 [SERVICE_RESTART_ALWAYS] = "always"
3736 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3738 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3739 [SERVICE_SIMPLE] = "simple",
3740 [SERVICE_FORKING] = "forking",
3741 [SERVICE_ONESHOT] = "oneshot",
3742 [SERVICE_DBUS] = "dbus",
3743 [SERVICE_NOTIFY] = "notify",
3744 [SERVICE_IDLE] = "idle"
3747 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3749 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3750 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3751 [SERVICE_EXEC_START] = "ExecStart",
3752 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3753 [SERVICE_EXEC_RELOAD] = "ExecReload",
3754 [SERVICE_EXEC_STOP] = "ExecStop",
3755 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3758 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3760 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3761 [NOTIFY_NONE] = "none",
3762 [NOTIFY_MAIN] = "main",
3763 [NOTIFY_ALL] = "all"
3766 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3768 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3769 [SERVICE_SUCCESS] = "success",
3770 [SERVICE_FAILURE_RESOURCES] = "resources",
3771 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3772 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3773 [SERVICE_FAILURE_SIGNAL] = "signal",
3774 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3775 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3776 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3779 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3781 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3782 [SERVICE_START_LIMIT_NONE] = "none",
3783 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3784 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3785 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3787 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3789 const UnitVTable service_vtable = {
3790 .object_size = sizeof(Service),
3797 .private_section = "Service",
3798 .exec_context_offset = offsetof(Service, exec_context),
3799 .cgroup_context_offset = offsetof(Service, cgroup_context),
3801 .init = service_init,
3802 .done = service_done,
3803 .load = service_load,
3805 .coldplug = service_coldplug,
3807 .dump = service_dump,
3809 .start = service_start,
3810 .stop = service_stop,
3811 .reload = service_reload,
3813 .can_reload = service_can_reload,
3815 .kill = service_kill,
3817 .serialize = service_serialize,
3818 .deserialize_item = service_deserialize_item,
3820 .active_state = service_active_state,
3821 .sub_state_to_string = service_sub_state_to_string,
3823 .check_gc = service_check_gc,
3824 .check_snapshot = service_check_snapshot,
3826 .sigchld_event = service_sigchld_event,
3827 .timer_event = service_timer_event,
3828 .fd_event = service_fd_event,
3830 .reset_failed = service_reset_failed,
3832 .notify_cgroup_empty = service_notify_cgroup_empty_event,
3833 .notify_message = service_notify_message,
3835 .bus_name_owner_change = service_bus_name_owner_change,
3836 .bus_query_pid_done = service_bus_query_pid_done,
3838 .bus_interface = "org.freedesktop.systemd1.Service",
3839 .bus_message_handler = bus_service_message_handler,
3840 .bus_invalidating_properties = bus_service_invalidating_properties,
3841 .bus_set_property = bus_service_set_property,
3842 .bus_commit_properties = bus_service_commit_properties,
3844 .can_transient = true,
3846 #ifdef HAVE_SYSV_COMPAT
3847 .enumerate = service_enumerate,
3849 .status_message_formats = {
3850 .starting_stopping = {
3851 [0] = "Starting %s...",
3852 [1] = "Stopping %s...",
3854 .finished_start_job = {
3855 [JOB_DONE] = "Started %s.",
3856 [JOB_FAILED] = "Failed to start %s.",
3857 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3858 [JOB_TIMEOUT] = "Timed out starting %s.",
3860 .finished_stop_job = {
3861 [JOB_DONE] = "Stopped %s.",
3862 [JOB_FAILED] = "Stopped (with error) %s.",
3863 [JOB_TIMEOUT] = "Timed out stopping %s.",