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);
145 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
147 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
150 static void service_unwatch_control_pid(Service *s) {
153 if (s->control_pid <= 0)
156 unit_unwatch_pid(UNIT(s), s->control_pid);
160 static void service_unwatch_main_pid(Service *s) {
163 if (s->main_pid <= 0)
166 unit_unwatch_pid(UNIT(s), s->main_pid);
170 static void service_unwatch_pid_file(Service *s) {
171 if (!s->pid_file_pathspec)
174 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
175 UNIT(s)->id, s->pid_file_pathspec->path);
176 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
177 path_spec_done(s->pid_file_pathspec);
178 free(s->pid_file_pathspec);
179 s->pid_file_pathspec = NULL;
182 static int service_set_main_pid(Service *s, pid_t pid) {
194 s->main_pid_known = true;
196 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
197 log_warning_unit(UNIT(s)->id,
198 "%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
199 UNIT(s)->id, (unsigned long) pid);
201 s->main_pid_alien = true;
203 s->main_pid_alien = false;
205 exec_status_start(&s->main_exec_status, pid);
210 static void service_close_socket_fd(Service *s) {
213 if (s->socket_fd < 0)
216 close_nointr_nofail(s->socket_fd);
220 static void service_connection_unref(Service *s) {
223 if (!UNIT_DEREF(s->accept_socket))
226 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
227 unit_ref_unset(&s->accept_socket);
230 static void service_stop_watchdog(Service *s) {
233 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
234 s->watchdog_timestamp.realtime = 0;
235 s->watchdog_timestamp.monotonic = 0;
238 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
240 static void service_handle_watchdog(Service *s) {
246 if (s->watchdog_usec == 0)
249 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
250 if (offset >= s->watchdog_usec) {
251 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
252 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_WATCHDOG);
256 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->watchdog_usec - offset, &s->watchdog_watch);
258 log_warning_unit(UNIT(s)->id,
259 "%s failed to install watchdog timer: %s",
260 UNIT(s)->id, strerror(-r));
263 static void service_reset_watchdog(Service *s) {
266 dual_timestamp_get(&s->watchdog_timestamp);
267 service_handle_watchdog(s);
270 static void service_done(Unit *u) {
271 Service *s = SERVICE(u);
278 #ifdef HAVE_SYSV_COMPAT
279 free(s->sysv_runlevels);
280 s->sysv_runlevels = NULL;
283 free(s->status_text);
284 s->status_text = NULL;
286 exec_context_done(&s->exec_context, manager_is_reloading_or_reexecuting(u->manager));
287 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
288 s->control_command = NULL;
289 s->main_command = NULL;
291 set_free(s->restart_ignore_status.code);
292 s->restart_ignore_status.code = NULL;
293 set_free(s->restart_ignore_status.signal);
294 s->restart_ignore_status.signal = NULL;
296 set_free(s->success_status.code);
297 s->success_status.code = NULL;
298 set_free(s->success_status.signal);
299 s->success_status.signal = NULL;
301 /* This will leak a process, but at least no memory or any of
303 service_unwatch_main_pid(s);
304 service_unwatch_control_pid(s);
305 service_unwatch_pid_file(s);
308 unit_unwatch_bus_name(u, s->bus_name);
313 service_close_socket_fd(s);
314 service_connection_unref(s);
316 unit_ref_unset(&s->accept_socket);
318 service_stop_watchdog(s);
320 unit_unwatch_timer(u, &s->timer_watch);
323 #ifdef HAVE_SYSV_COMPAT
324 static char *sysv_translate_name(const char *name) {
327 r = new(char, strlen(name) + sizeof(".service"));
331 if (endswith(name, ".sh"))
332 /* Drop .sh suffix */
333 strcpy(stpcpy(r, name) - 3, ".service");
335 /* Normal init script name */
336 strcpy(stpcpy(r, name), ".service");
341 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
343 /* We silently ignore the $ prefix here. According to the LSB
344 * spec it simply indicates whether something is a
345 * standardized name or a distribution-specific one. Since we
346 * just follow what already exists and do not introduce new
347 * uses or names we don't care who introduced a new name. */
349 static const char * const table[] = {
350 /* LSB defined facilities */
352 "network", SPECIAL_NETWORK_TARGET,
353 "named", SPECIAL_NSS_LOOKUP_TARGET,
354 "portmap", SPECIAL_RPCBIND_TARGET,
355 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
357 "time", SPECIAL_TIME_SYNC_TARGET,
367 n = *name == '$' ? name + 1 : name;
369 for (i = 0; i < ELEMENTSOF(table); i += 2) {
371 if (!streq(table[i], n))
377 r = strdup(table[i+1]);
384 /* If we don't know this name, fallback heuristics to figure
385 * out whether something is a target or a service alias. */
388 if (!unit_prefix_is_valid(n))
391 /* Facilities starting with $ are most likely targets */
392 r = unit_name_build(n, NULL, ".target");
393 } else if (filename && streq(name, filename))
394 /* Names equaling the file name of the services are redundant */
397 /* Everything else we assume to be normal service names */
398 r = sysv_translate_name(n);
409 static int sysv_fix_order(Service *s) {
415 if (s->sysv_start_priority < 0)
418 /* For each pair of services where at least one lacks a LSB
419 * header, we use the start priority value to order things. */
421 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
424 bool special_s, special_t;
431 if (UNIT(t)->load_state != UNIT_LOADED)
434 if (t->sysv_start_priority < 0)
437 /* If both units have modern headers we don't care
438 * about the priorities */
439 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
440 (UNIT(t)->fragment_path || t->sysv_has_lsb))
443 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
444 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
446 if (special_t && !special_s)
448 else if (special_s && !special_t)
450 else if (t->sysv_start_priority < s->sysv_start_priority)
452 else if (t->sysv_start_priority > s->sysv_start_priority)
457 /* FIXME: Maybe we should compare the name here lexicographically? */
459 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
466 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
469 if (!(c = new0(ExecCommand, 1)))
472 if (!(c->path = strdup(path))) {
477 if (!(c->argv = strv_new(path, arg1, NULL))) {
486 static int sysv_exec_commands(Service *s, const bool supports_reload) {
491 assert(UNIT(s)->source_path);
493 c = exec_command_new(UNIT(s)->source_path, "start");
496 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
498 c = exec_command_new(UNIT(s)->source_path, "stop");
501 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
503 if (supports_reload) {
504 c = exec_command_new(UNIT(s)->source_path, "reload");
507 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
513 static bool usage_contains_reload(const char *line) {
514 return (strcasestr(line, "{reload|") ||
515 strcasestr(line, "{reload}") ||
516 strcasestr(line, "{reload\"") ||
517 strcasestr(line, "|reload|") ||
518 strcasestr(line, "|reload}") ||
519 strcasestr(line, "|reload\""));
522 static int service_load_sysv_path(Service *s, const char *path) {
534 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
536 bool supports_reload = false;
543 f = fopen(path, "re");
545 r = errno == ENOENT ? 0 : -errno;
549 if (fstat(fileno(f), &st) < 0) {
554 free(u->source_path);
555 u->source_path = strdup(path);
556 if (!u->source_path) {
560 u->source_mtime = timespec_load(&st.st_mtim);
562 if (null_or_empty(&st)) {
563 u->load_state = UNIT_MASKED;
571 char l[LINE_MAX], *t;
573 if (!fgets(l, sizeof(l), f)) {
578 log_error_unit(u->id,
579 "Failed to read configuration file '%s': %s",
588 /* Try to figure out whether this init script supports
589 * the reload operation. This heuristic looks for
590 * "Usage" lines which include the reload option. */
591 if ( state == USAGE_CONTINUATION ||
592 (state == NORMAL && strcasestr(t, "usage"))) {
593 if (usage_contains_reload(t)) {
594 supports_reload = true;
596 } else if (t[strlen(t)-1] == '\\')
597 state = USAGE_CONTINUATION;
605 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
607 s->sysv_has_lsb = true;
611 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
617 t += strspn(t, WHITESPACE);
619 if (state == NORMAL) {
621 /* Try to parse Red Hat style chkconfig headers */
623 if (startswith_no_case(t, "chkconfig:")) {
625 char runlevels[16], *k;
629 if (sscanf(t+10, "%15s %i %*i",
631 &start_priority) != 2) {
633 log_warning_unit(u->id,
634 "[%s:%u] Failed to parse chkconfig line. Ignoring.",
639 /* A start priority gathered from the
640 * symlink farms is preferred over the
641 * data from the LSB header. */
642 if (start_priority < 0 || start_priority > 99)
643 log_warning_unit(u->id,
644 "[%s:%u] Start priority out of range. Ignoring.",
647 s->sysv_start_priority = start_priority;
649 char_array_0(runlevels);
650 k = delete_chars(runlevels, WHITESPACE "-");
655 if (!(d = strdup(k))) {
660 free(s->sysv_runlevels);
661 s->sysv_runlevels = d;
664 } else if (startswith_no_case(t, "description:")) {
666 size_t k = strlen(t);
670 if (t[k-1] == '\\') {
675 if ((j = strstrip(t+12)) && *j) {
676 if (!(d = strdup(j))) {
683 free(chkconfig_description);
684 chkconfig_description = d;
686 } else if (startswith_no_case(t, "pidfile:")) {
693 if (!path_is_absolute(fn)) {
694 log_warning_unit(u->id,
695 "[%s:%u] PID file not absolute. Ignoring.",
700 if (!(fn = strdup(fn))) {
709 } else if (state == DESCRIPTION) {
711 /* Try to parse Red Hat style description
714 size_t k = strlen(t);
722 if ((j = strstrip(t)) && *j) {
725 if (chkconfig_description)
726 d = strjoin(chkconfig_description, " ", j, NULL);
735 free(chkconfig_description);
736 chkconfig_description = d;
739 } else if (state == LSB || state == LSB_DESCRIPTION) {
741 if (startswith_no_case(t, "Provides:")) {
747 FOREACH_WORD_QUOTED(w, z, t+9, i) {
750 if (!(n = strndup(w, z))) {
755 r = sysv_translate_facility(n, path_get_file_name(path), &m);
764 if (unit_name_to_type(m) == UNIT_SERVICE)
765 r = unit_merge_by_name(u, m);
772 * indication that the
774 * now available. This
777 * targets do NOT pull
780 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
783 log_error_unit(u->id,
784 "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
785 path, line, m, strerror(-r));
790 } else if (startswith_no_case(t, "Required-Start:") ||
791 startswith_no_case(t, "Should-Start:") ||
792 startswith_no_case(t, "X-Start-Before:") ||
793 startswith_no_case(t, "X-Start-After:")) {
799 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
802 if (!(n = strndup(w, z))) {
807 r = sysv_translate_facility(n, path_get_file_name(path), &m);
809 log_error_unit(u->id,
810 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
811 path, line, n, strerror(-r));
821 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
824 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
825 path, line, m, strerror(-r));
829 } else if (startswith_no_case(t, "Default-Start:")) {
834 k = delete_chars(t+14, WHITESPACE "-");
837 if (!(d = strdup(k))) {
842 free(s->sysv_runlevels);
843 s->sysv_runlevels = d;
846 } else if (startswith_no_case(t, "Description:")) {
849 state = LSB_DESCRIPTION;
851 if ((j = strstrip(t+12)) && *j) {
852 if (!(d = strdup(j))) {
859 free(long_description);
860 long_description = d;
862 } else if (startswith_no_case(t, "Short-Description:")) {
867 if ((j = strstrip(t+18)) && *j) {
868 if (!(d = strdup(j))) {
875 free(short_description);
876 short_description = d;
878 } else if (state == LSB_DESCRIPTION) {
880 if (startswith(l, "#\t") || startswith(l, "# ")) {
883 if ((j = strstrip(t)) && *j) {
886 if (long_description)
887 d = strjoin(long_description, " ", t, NULL);
896 free(long_description);
897 long_description = d;
906 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
909 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
910 /* If there a runlevels configured for this service
911 * but none of the standard ones, then we assume this
912 * is some special kind of service (which might be
913 * needed for early boot) and don't create any links
916 UNIT(s)->default_dependencies = false;
918 /* Don't timeout special services during boot (like fsck) */
919 s->timeout_start_usec = 0;
920 s->timeout_stop_usec = 0;
922 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
923 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
926 /* Special setting for all SysV services */
927 s->type = SERVICE_FORKING;
928 s->remain_after_exit = !s->pid_file;
929 s->guess_main_pid = false;
930 s->restart = SERVICE_RESTART_NO;
931 s->exec_context.ignore_sigpipe = false;
932 s->kill_context.kill_mode = KILL_PROCESS;
934 /* We use the long description only if
935 * no short description is set. */
937 if (short_description)
938 description = short_description;
939 else if (chkconfig_description)
940 description = chkconfig_description;
941 else if (long_description)
942 description = long_description;
949 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
957 /* The priority that has been set in /etc/rcN.d/ hierarchies
958 * takes precedence over what is stored as default in the LSB
960 if (s->sysv_start_priority_from_rcnd >= 0)
961 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
963 u->load_state = UNIT_LOADED;
971 free(short_description);
972 free(long_description);
973 free(chkconfig_description);
978 static int service_load_sysv_name(Service *s, const char *name) {
984 /* For SysV services we strip the *.sh suffixes. */
985 if (endswith(name, ".sh.service"))
988 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
992 path = strjoin(*p, "/", name, NULL);
996 assert(endswith(path, ".service"));
997 path[strlen(path)-8] = 0;
999 r = service_load_sysv_path(s, path);
1001 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1002 /* Try *.sh source'able init scripts */
1003 strcat(path, ".sh");
1004 r = service_load_sysv_path(s, path);
1011 if (UNIT(s)->load_state != UNIT_STUB)
1018 static int service_load_sysv(Service *s) {
1025 /* Load service data from SysV init scripts, preferably with
1026 * LSB headers ... */
1028 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1031 if ((t = UNIT(s)->id))
1032 if ((r = service_load_sysv_name(s, t)) < 0)
1035 if (UNIT(s)->load_state == UNIT_STUB)
1036 SET_FOREACH(t, UNIT(s)->names, i) {
1037 if (t == UNIT(s)->id)
1040 if ((r = service_load_sysv_name(s, t)) < 0)
1043 if (UNIT(s)->load_state != UNIT_STUB)
1051 static int fsck_fix_order(Service *s) {
1057 if (s->fsck_passno <= 0)
1060 /* For each pair of services where both have an fsck priority
1061 * we order things based on it. */
1063 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1072 if (UNIT(t)->load_state != UNIT_LOADED)
1075 if (t->fsck_passno <= 0)
1078 if (t->fsck_passno < s->fsck_passno)
1080 else if (t->fsck_passno > s->fsck_passno)
1085 r = unit_add_dependency(UNIT(s), d, UNIT(t), true);
1093 static int service_verify(Service *s) {
1096 if (UNIT(s)->load_state != UNIT_LOADED)
1099 if (!s->exec_command[SERVICE_EXEC_START]) {
1100 log_error_unit(UNIT(s)->id,
1101 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1105 if (s->type != SERVICE_ONESHOT &&
1106 s->exec_command[SERVICE_EXEC_START]->command_next) {
1107 log_error_unit(UNIT(s)->id,
1108 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1112 if (s->type == SERVICE_DBUS && !s->bus_name) {
1113 log_error_unit(UNIT(s)->id,
1114 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1118 if (s->bus_name && s->type != SERVICE_DBUS)
1119 log_warning_unit(UNIT(s)->id,
1120 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1122 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1123 log_error_unit(UNIT(s)->id,
1124 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1131 static int service_add_default_dependencies(Service *s) {
1136 /* Add a number of automatic dependencies useful for the
1137 * majority of services. */
1139 /* First, pull in base system */
1140 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1141 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1142 SPECIAL_BASIC_TARGET, NULL, true);
1146 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1147 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1148 SPECIAL_SOCKETS_TARGET, NULL, true);
1152 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1153 SPECIAL_TIMERS_TARGET, NULL, true);
1157 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1158 SPECIAL_PATHS_TARGET, NULL, true);
1163 /* Second, activate normal shutdown */
1164 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
1165 SPECIAL_SHUTDOWN_TARGET, NULL, true);
1169 static void service_fix_output(Service *s) {
1172 /* If nothing has been explicitly configured, patch default
1173 * output in. If input is socket/tty we avoid this however,
1174 * since in that case we want output to default to the same
1175 * place as we read input from. */
1177 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1178 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1179 s->exec_context.std_input == EXEC_INPUT_NULL)
1180 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1182 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1183 s->exec_context.std_input == EXEC_INPUT_NULL)
1184 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1187 static int service_load(Unit *u) {
1189 Service *s = SERVICE(u);
1193 /* Load a .service file */
1194 if ((r = unit_load_fragment(u)) < 0)
1197 #ifdef HAVE_SYSV_COMPAT
1198 /* Load a classic init script as a fallback, if we couldn't find anything */
1199 if (u->load_state == UNIT_STUB)
1200 if ((r = service_load_sysv(s)) < 0)
1204 /* Still nothing found? Then let's give up */
1205 if (u->load_state == UNIT_STUB)
1208 /* We were able to load something, then let's add in the
1209 * dropin directories. */
1210 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1213 /* This is a new unit? Then let's add in some extras */
1214 if (u->load_state == UNIT_LOADED) {
1215 if (s->type == _SERVICE_TYPE_INVALID)
1216 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1218 /* Oneshot services have disabled start timeout by default */
1219 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1220 s->timeout_start_usec = 0;
1222 service_fix_output(s);
1224 r = unit_add_exec_dependencies(u, &s->exec_context);
1228 r = unit_add_default_cgroups(u);
1232 #ifdef HAVE_SYSV_COMPAT
1233 r = sysv_fix_order(s);
1238 r = fsck_fix_order(s);
1243 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1246 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1247 s->notify_access = NOTIFY_MAIN;
1249 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1250 s->notify_access = NOTIFY_MAIN;
1252 if (s->type == SERVICE_DBUS || s->bus_name) {
1253 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES,
1254 SPECIAL_DBUS_SOCKET, NULL, true);
1259 if (UNIT(s)->default_dependencies) {
1260 r = service_add_default_dependencies(s);
1265 r = unit_exec_context_defaults(u, &s->exec_context);
1270 return service_verify(s);
1273 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1275 ServiceExecCommand c;
1276 Service *s = SERVICE(u);
1277 const char *prefix2;
1278 _cleanup_free_ char *p2 = NULL;
1282 p2 = strappend(prefix, "\t");
1283 prefix2 = p2 ? p2 : prefix;
1286 "%sService State: %s\n"
1288 "%sReload Result: %s\n"
1289 "%sPermissionsStartOnly: %s\n"
1290 "%sRootDirectoryStartOnly: %s\n"
1291 "%sRemainAfterExit: %s\n"
1292 "%sGuessMainPID: %s\n"
1295 "%sNotifyAccess: %s\n",
1296 prefix, service_state_to_string(s->state),
1297 prefix, service_result_to_string(s->result),
1298 prefix, service_result_to_string(s->reload_result),
1299 prefix, yes_no(s->permissions_start_only),
1300 prefix, yes_no(s->root_directory_start_only),
1301 prefix, yes_no(s->remain_after_exit),
1302 prefix, yes_no(s->guess_main_pid),
1303 prefix, service_type_to_string(s->type),
1304 prefix, service_restart_to_string(s->restart),
1305 prefix, notify_access_to_string(s->notify_access));
1307 if (s->control_pid > 0)
1309 "%sControl PID: %lu\n",
1310 prefix, (unsigned long) s->control_pid);
1312 if (s->main_pid > 0)
1315 "%sMain PID Known: %s\n"
1316 "%sMain PID Alien: %s\n",
1317 prefix, (unsigned long) s->main_pid,
1318 prefix, yes_no(s->main_pid_known),
1319 prefix, yes_no(s->main_pid_alien));
1324 prefix, s->pid_file);
1329 "%sBus Name Good: %s\n",
1330 prefix, s->bus_name,
1331 prefix, yes_no(s->bus_name_good));
1333 kill_context_dump(&s->kill_context, f, prefix);
1334 exec_context_dump(&s->exec_context, f, prefix);
1336 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1338 if (!s->exec_command[c])
1341 fprintf(f, "%s-> %s:\n",
1342 prefix, service_exec_command_to_string(c));
1344 exec_command_dump_list(s->exec_command[c], f, prefix2);
1347 #ifdef HAVE_SYSV_COMPAT
1350 "%sSysV Init Script has LSB Header: %s\n"
1351 "%sSysVEnabled: %s\n",
1352 prefix, yes_no(s->sysv_has_lsb),
1353 prefix, yes_no(s->sysv_enabled));
1355 if (s->sysv_start_priority >= 0)
1357 "%sSysVStartPriority: %i\n",
1358 prefix, s->sysv_start_priority);
1360 if (s->sysv_runlevels)
1361 fprintf(f, "%sSysVRunLevels: %s\n",
1362 prefix, s->sysv_runlevels);
1365 if (s->fsck_passno > 0)
1367 "%sFsckPassNo: %i\n",
1368 prefix, s->fsck_passno);
1371 fprintf(f, "%sStatus Text: %s\n",
1372 prefix, s->status_text);
1375 static int service_load_pid_file(Service *s, bool may_warn) {
1376 _cleanup_free_ char *k = NULL;
1385 r = read_one_line_file(s->pid_file, &k);
1388 log_info_unit(UNIT(s)->id,
1389 "PID file %s not readable (yet?) after %s.",
1390 s->pid_file, service_state_to_string(s->state));
1394 r = parse_pid(k, &pid);
1397 log_info_unit(UNIT(s)->id,
1398 "Failed to read PID from file %s: %s",
1399 s->pid_file, strerror(-r));
1403 if (kill(pid, 0) < 0 && errno != EPERM) {
1405 log_info_unit(UNIT(s)->id,
1406 "PID %lu read from file %s does not exist.",
1407 (unsigned long) pid, s->pid_file);
1411 if (s->main_pid_known) {
1412 if (pid == s->main_pid)
1415 log_debug_unit(UNIT(s)->id,
1416 "Main PID changing: %lu -> %lu",
1417 (unsigned long) s->main_pid, (unsigned long) pid);
1418 service_unwatch_main_pid(s);
1419 s->main_pid_known = false;
1421 log_debug_unit(UNIT(s)->id,
1422 "Main PID loaded: %lu", (unsigned long) pid);
1424 r = service_set_main_pid(s, pid);
1428 r = unit_watch_pid(UNIT(s), pid);
1430 /* FIXME: we need to do something here */
1431 log_warning_unit(UNIT(s)->id,
1432 "Failed to watch PID %lu from service %s",
1433 (unsigned long) pid, UNIT(s)->id);
1440 static int service_search_main_pid(Service *s) {
1446 /* If we know it anyway, don't ever fallback to unreliable
1448 if (s->main_pid_known)
1451 if (!s->guess_main_pid)
1454 assert(s->main_pid <= 0);
1456 pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings);
1460 log_debug_unit(UNIT(s)->id,
1461 "Main PID guessed: %lu", (unsigned long) pid);
1462 r = service_set_main_pid(s, pid);
1466 r = unit_watch_pid(UNIT(s), pid);
1468 /* FIXME: we need to do something here */
1469 log_warning_unit(UNIT(s)->id,
1470 "Failed to watch PID %lu from service %s",
1471 (unsigned long) pid, UNIT(s)->id);
1477 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1483 /* Notifies all our sockets when we die */
1485 if (s->socket_fd >= 0)
1488 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1489 if (u->type == UNIT_SOCKET)
1490 socket_notify_service_dead(SOCKET(u), failed_permanent);
1495 static void service_set_state(Service *s, ServiceState state) {
1496 ServiceState old_state;
1497 const UnitActiveState *table;
1500 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1502 old_state = s->state;
1505 service_unwatch_pid_file(s);
1507 if (state != SERVICE_START_PRE &&
1508 state != SERVICE_START &&
1509 state != SERVICE_START_POST &&
1510 state != SERVICE_RELOAD &&
1511 state != SERVICE_STOP &&
1512 state != SERVICE_STOP_SIGTERM &&
1513 state != SERVICE_STOP_SIGKILL &&
1514 state != SERVICE_STOP_POST &&
1515 state != SERVICE_FINAL_SIGTERM &&
1516 state != SERVICE_FINAL_SIGKILL &&
1517 state != SERVICE_AUTO_RESTART)
1518 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1520 if (state != SERVICE_START &&
1521 state != SERVICE_START_POST &&
1522 state != SERVICE_RUNNING &&
1523 state != SERVICE_RELOAD &&
1524 state != SERVICE_STOP &&
1525 state != SERVICE_STOP_SIGTERM &&
1526 state != SERVICE_STOP_SIGKILL) {
1527 service_unwatch_main_pid(s);
1528 s->main_command = NULL;
1531 if (state != SERVICE_START_PRE &&
1532 state != SERVICE_START &&
1533 state != SERVICE_START_POST &&
1534 state != SERVICE_RELOAD &&
1535 state != SERVICE_STOP &&
1536 state != SERVICE_STOP_SIGTERM &&
1537 state != SERVICE_STOP_SIGKILL &&
1538 state != SERVICE_STOP_POST &&
1539 state != SERVICE_FINAL_SIGTERM &&
1540 state != SERVICE_FINAL_SIGKILL) {
1541 service_unwatch_control_pid(s);
1542 s->control_command = NULL;
1543 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1546 if (state == SERVICE_FAILED)
1547 service_notify_sockets_dead(s, s->result == SERVICE_FAILURE_START_LIMIT);
1549 if (state == SERVICE_DEAD ||
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 state == SERVICE_AUTO_RESTART)
1557 service_notify_sockets_dead(s, false);
1559 if (state != SERVICE_START_PRE &&
1560 state != SERVICE_START &&
1561 state != SERVICE_START_POST &&
1562 state != SERVICE_RUNNING &&
1563 state != SERVICE_RELOAD &&
1564 state != SERVICE_STOP &&
1565 state != SERVICE_STOP_SIGTERM &&
1566 state != SERVICE_STOP_SIGKILL &&
1567 state != SERVICE_STOP_POST &&
1568 state != SERVICE_FINAL_SIGTERM &&
1569 state != SERVICE_FINAL_SIGKILL &&
1570 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1571 service_close_socket_fd(s);
1572 service_connection_unref(s);
1575 if (state == SERVICE_STOP || state == SERVICE_STOP_SIGTERM)
1576 service_stop_watchdog(s);
1578 /* For the inactive states unit_notify() will trim the cgroup,
1579 * but for exit we have to do that ourselves... */
1580 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1581 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1583 if (old_state != state)
1584 log_debug_unit(UNIT(s)->id,
1585 "%s changed %s -> %s", UNIT(s)->id,
1586 service_state_to_string(old_state),
1587 service_state_to_string(state));
1589 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1590 s->reload_result = SERVICE_SUCCESS;
1593 static int service_coldplug(Unit *u) {
1594 Service *s = SERVICE(u);
1598 assert(s->state == SERVICE_DEAD);
1600 if (s->deserialized_state != s->state) {
1602 if (s->deserialized_state == SERVICE_START_PRE ||
1603 s->deserialized_state == SERVICE_START ||
1604 s->deserialized_state == SERVICE_START_POST ||
1605 s->deserialized_state == SERVICE_RELOAD ||
1606 s->deserialized_state == SERVICE_STOP ||
1607 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1608 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1609 s->deserialized_state == SERVICE_STOP_POST ||
1610 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1611 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1612 s->deserialized_state == SERVICE_AUTO_RESTART) {
1613 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1616 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1618 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
1624 if ((s->deserialized_state == SERVICE_START &&
1625 (s->type == SERVICE_FORKING ||
1626 s->type == SERVICE_DBUS ||
1627 s->type == SERVICE_ONESHOT ||
1628 s->type == SERVICE_NOTIFY)) ||
1629 s->deserialized_state == SERVICE_START_POST ||
1630 s->deserialized_state == SERVICE_RUNNING ||
1631 s->deserialized_state == SERVICE_RELOAD ||
1632 s->deserialized_state == SERVICE_STOP ||
1633 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1634 s->deserialized_state == SERVICE_STOP_SIGKILL)
1635 if (s->main_pid > 0) {
1636 r = unit_watch_pid(UNIT(s), s->main_pid);
1641 if (s->deserialized_state == SERVICE_START_PRE ||
1642 s->deserialized_state == SERVICE_START ||
1643 s->deserialized_state == SERVICE_START_POST ||
1644 s->deserialized_state == SERVICE_RELOAD ||
1645 s->deserialized_state == SERVICE_STOP ||
1646 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1647 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1648 s->deserialized_state == SERVICE_STOP_POST ||
1649 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1650 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1651 if (s->control_pid > 0) {
1652 r = unit_watch_pid(UNIT(s), s->control_pid);
1657 if (s->deserialized_state == SERVICE_START_POST ||
1658 s->deserialized_state == SERVICE_RUNNING)
1659 service_handle_watchdog(s);
1661 service_set_state(s, s->deserialized_state);
1666 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1670 unsigned rn_fds = 0;
1677 if (s->socket_fd >= 0)
1680 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1685 if (u->type != UNIT_SOCKET)
1690 r = socket_collect_fds(sock, &cfds, &cn_fds);
1703 t = new(int, rn_fds+cn_fds);
1710 memcpy(t, rfds, rn_fds * sizeof(int));
1711 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1716 rn_fds = rn_fds+cn_fds;
1731 static int service_spawn(
1736 bool apply_permissions,
1738 bool apply_tty_stdin,
1739 bool set_notify_socket,
1746 _cleanup_free_ int *fdsbuf = NULL;
1747 unsigned n_fds = 0, n_env = 0;
1748 _cleanup_strv_free_ char
1749 **argv = NULL, **final_env = NULL, **our_env = NULL;
1756 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1757 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1758 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1760 if (s->socket_fd >= 0) {
1761 fds = &s->socket_fd;
1764 r = service_collect_fds(s, &fdsbuf, &n_fds);
1772 if (timeout && s->timeout_start_usec) {
1773 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true,
1774 s->timeout_start_usec, &s->timer_watch);
1778 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1780 argv = unit_full_printf_strv(UNIT(s), c->argv);
1786 our_env = new0(char*, 5);
1792 if (set_notify_socket)
1793 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1798 if (s->main_pid > 0)
1799 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1804 if (s->watchdog_usec > 0)
1805 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1810 if (s->meta.manager->running_as != SYSTEMD_SYSTEM)
1811 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1816 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1830 UNIT(s)->manager->confirm_spawn,
1831 UNIT(s)->cgroup_bondings,
1832 UNIT(s)->cgroup_attributes,
1833 is_control ? "control" : NULL,
1835 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1840 r = unit_watch_pid(UNIT(s), pid);
1842 /* FIXME: we need to do something here */
1851 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1856 static int main_pid_good(Service *s) {
1859 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1862 /* If we know the pid file, then lets just check if it is
1864 if (s->main_pid_known) {
1866 /* If it's an alien child let's check if it is still
1868 if (s->main_pid_alien && s->main_pid > 0)
1869 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1871 /* .. otherwise assume we'll get a SIGCHLD for it,
1872 * which we really should wait for to collect exit
1873 * status and code */
1874 return s->main_pid > 0;
1877 /* We don't know the pid */
1881 _pure_ static int control_pid_good(Service *s) {
1884 return s->control_pid > 0;
1887 static int cgroup_good(Service *s) {
1892 r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings);
1899 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1903 if (f != SERVICE_SUCCESS)
1906 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1908 if (allow_restart &&
1909 !s->forbid_restart &&
1910 (s->restart == SERVICE_RESTART_ALWAYS ||
1911 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1912 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1913 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1914 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1915 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1916 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1917 (s->result != SERVICE_FAILURE_SIGNAL ||
1918 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1921 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
1925 service_set_state(s, SERVICE_AUTO_RESTART);
1928 s->forbid_restart = false;
1930 /* we want fresh tmpdirs in case service is started again immediately */
1931 exec_context_tmp_dirs_done(&s->exec_context);
1936 log_warning_unit(UNIT(s)->id,
1937 "%s failed to run install restart timer: %s",
1938 UNIT(s)->id, strerror(-r));
1939 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1942 static void service_enter_stop_post(Service *s, ServiceResult f) {
1946 if (f != SERVICE_SUCCESS)
1949 service_unwatch_control_pid(s);
1951 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1952 if (s->control_command) {
1953 s->control_command_id = SERVICE_EXEC_STOP_POST;
1955 r = service_spawn(s,
1959 !s->permissions_start_only,
1960 !s->root_directory_start_only,
1969 service_set_state(s, SERVICE_STOP_POST);
1971 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1976 log_warning_unit(UNIT(s)->id,
1977 "%s failed to run 'stop-post' task: %s",
1978 UNIT(s)->id, strerror(-r));
1979 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1982 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1987 if (f != SERVICE_SUCCESS)
1990 r = unit_kill_context(
1993 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
2001 if (s->timeout_stop_usec > 0) {
2002 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true,
2003 s->timeout_stop_usec, &s->timer_watch);
2008 service_set_state(s, state);
2009 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2010 service_enter_stop_post(s, SERVICE_SUCCESS);
2012 service_enter_dead(s, SERVICE_SUCCESS, true);
2017 log_warning_unit(UNIT(s)->id,
2018 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2020 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2021 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2023 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2026 static void service_enter_stop(Service *s, ServiceResult f) {
2031 if (f != SERVICE_SUCCESS)
2034 service_unwatch_control_pid(s);
2036 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
2037 if (s->control_command) {
2038 s->control_command_id = SERVICE_EXEC_STOP;
2040 r = service_spawn(s,
2044 !s->permissions_start_only,
2045 !s->root_directory_start_only,
2053 service_set_state(s, SERVICE_STOP);
2055 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2060 log_warning_unit(UNIT(s)->id,
2061 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2062 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2065 static void service_enter_running(Service *s, ServiceResult f) {
2066 int main_pid_ok, cgroup_ok;
2069 if (f != SERVICE_SUCCESS)
2072 main_pid_ok = main_pid_good(s);
2073 cgroup_ok = cgroup_good(s);
2075 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2076 (s->bus_name_good || s->type != SERVICE_DBUS))
2077 service_set_state(s, SERVICE_RUNNING);
2078 else if (s->remain_after_exit)
2079 service_set_state(s, SERVICE_EXITED);
2081 service_enter_stop(s, SERVICE_SUCCESS);
2084 static void service_enter_start_post(Service *s) {
2088 service_unwatch_control_pid(s);
2090 if (s->watchdog_usec > 0)
2091 service_reset_watchdog(s);
2093 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
2094 if (s->control_command) {
2095 s->control_command_id = SERVICE_EXEC_START_POST;
2097 r = service_spawn(s,
2101 !s->permissions_start_only,
2102 !s->root_directory_start_only,
2110 service_set_state(s, SERVICE_START_POST);
2112 service_enter_running(s, SERVICE_SUCCESS);
2117 log_warning_unit(UNIT(s)->id,
2118 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2119 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2122 static void service_enter_start(Service *s) {
2129 assert(s->exec_command[SERVICE_EXEC_START]);
2130 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2132 if (s->type == SERVICE_FORKING)
2133 service_unwatch_control_pid(s);
2135 service_unwatch_main_pid(s);
2137 /* We want to ensure that nobody leaks processes from
2138 * START_PRE here, so let's go on a killing spree, People
2139 * should not spawn long running processes from START_PRE. */
2140 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2142 if (s->type == SERVICE_FORKING) {
2143 s->control_command_id = SERVICE_EXEC_START;
2144 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2146 s->main_command = NULL;
2148 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2149 s->control_command = NULL;
2151 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2154 r = service_spawn(s,
2156 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
2157 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2162 s->notify_access != NOTIFY_NONE,
2168 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2169 /* For simple services we immediately start
2170 * the START_POST binaries. */
2172 service_set_main_pid(s, pid);
2173 service_enter_start_post(s);
2175 } else if (s->type == SERVICE_FORKING) {
2177 /* For forking services we wait until the start
2178 * process exited. */
2180 s->control_pid = pid;
2181 service_set_state(s, SERVICE_START);
2183 } else if (s->type == SERVICE_ONESHOT ||
2184 s->type == SERVICE_DBUS ||
2185 s->type == SERVICE_NOTIFY) {
2187 /* For oneshot services we wait until the start
2188 * process exited, too, but it is our main process. */
2190 /* For D-Bus services we know the main pid right away,
2191 * but wait for the bus name to appear on the
2192 * bus. Notify services are similar. */
2194 service_set_main_pid(s, pid);
2195 service_set_state(s, SERVICE_START);
2197 assert_not_reached("Unknown service type");
2202 log_warning_unit(UNIT(s)->id,
2203 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2204 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2207 static void service_enter_start_pre(Service *s) {
2212 service_unwatch_control_pid(s);
2214 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
2215 if (s->control_command) {
2217 /* Before we start anything, let's clear up what might
2218 * be left from previous runs. */
2219 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL,
2220 true,true, NULL, "control");
2222 s->control_command_id = SERVICE_EXEC_START_PRE;
2224 r = service_spawn(s,
2228 !s->permissions_start_only,
2229 !s->root_directory_start_only,
2237 service_set_state(s, SERVICE_START_PRE);
2239 service_enter_start(s);
2244 log_warning_unit(UNIT(s)->id,
2245 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2246 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2249 static void service_enter_restart(Service *s) {
2254 dbus_error_init(&error);
2256 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2257 /* Don't restart things if we are going down anyway */
2258 log_info_unit(UNIT(s)->id,
2259 "Stop job pending for unit, delaying automatic restart.");
2261 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
2268 /* Any units that are bound to this service must also be
2269 * restarted. We use JOB_RESTART (instead of the more obvious
2270 * JOB_START) here so that those dependency jobs will be added
2272 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2276 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2277 * it will be canceled as part of the service_stop() call that
2278 * is executed as part of JOB_RESTART. */
2280 log_debug_unit(UNIT(s)->id,
2281 "%s scheduled restart job.", UNIT(s)->id);
2285 log_warning_unit(UNIT(s)->id,
2286 "%s failed to schedule restart job: %s",
2287 UNIT(s)->id, bus_error(&error, -r));
2288 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2290 dbus_error_free(&error);
2293 static void service_enter_reload(Service *s) {
2298 service_unwatch_control_pid(s);
2300 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
2301 if (s->control_command) {
2302 s->control_command_id = SERVICE_EXEC_RELOAD;
2304 r = service_spawn(s,
2308 !s->permissions_start_only,
2309 !s->root_directory_start_only,
2317 service_set_state(s, SERVICE_RELOAD);
2319 service_enter_running(s, SERVICE_SUCCESS);
2324 log_warning_unit(UNIT(s)->id,
2325 "%s failed to run 'reload' task: %s",
2326 UNIT(s)->id, strerror(-r));
2327 s->reload_result = SERVICE_FAILURE_RESOURCES;
2328 service_enter_running(s, SERVICE_SUCCESS);
2331 static void service_run_next_control(Service *s) {
2335 assert(s->control_command);
2336 assert(s->control_command->command_next);
2338 assert(s->control_command_id != SERVICE_EXEC_START);
2340 s->control_command = s->control_command->command_next;
2341 service_unwatch_control_pid(s);
2343 r = service_spawn(s,
2347 !s->permissions_start_only,
2348 !s->root_directory_start_only,
2349 s->control_command_id == SERVICE_EXEC_START_PRE ||
2350 s->control_command_id == SERVICE_EXEC_STOP_POST,
2360 log_warning_unit(UNIT(s)->id,
2361 "%s failed to run next control task: %s",
2362 UNIT(s)->id, strerror(-r));
2364 if (s->state == SERVICE_START_PRE)
2365 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2366 else if (s->state == SERVICE_STOP)
2367 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2368 else if (s->state == SERVICE_STOP_POST)
2369 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2370 else if (s->state == SERVICE_RELOAD) {
2371 s->reload_result = SERVICE_FAILURE_RESOURCES;
2372 service_enter_running(s, SERVICE_SUCCESS);
2374 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2377 static void service_run_next_main(Service *s) {
2382 assert(s->main_command);
2383 assert(s->main_command->command_next);
2384 assert(s->type == SERVICE_ONESHOT);
2386 s->main_command = s->main_command->command_next;
2387 service_unwatch_main_pid(s);
2389 r = service_spawn(s,
2396 s->notify_access != NOTIFY_NONE,
2402 service_set_main_pid(s, pid);
2407 log_warning_unit(UNIT(s)->id,
2408 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2409 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2412 static int service_start_limit_test(Service *s) {
2415 if (ratelimit_test(&s->start_limit))
2418 switch (s->start_limit_action) {
2420 case SERVICE_START_LIMIT_NONE:
2421 log_warning_unit(UNIT(s)->id,
2422 "%s start request repeated too quickly, refusing to start.",
2426 case SERVICE_START_LIMIT_REBOOT: {
2430 dbus_error_init(&error);
2432 log_warning_unit(UNIT(s)->id,
2433 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2435 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
2436 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
2437 true, &error, NULL);
2439 log_error_unit(UNIT(s)->id,
2440 "Failed to reboot: %s.", bus_error(&error, r));
2441 dbus_error_free(&error);
2447 case SERVICE_START_LIMIT_REBOOT_FORCE:
2448 log_warning_unit(UNIT(s)->id,
2449 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2450 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2453 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2454 log_warning_unit(UNIT(s)->id,
2455 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2457 reboot(RB_AUTOBOOT);
2461 log_error_unit(UNIT(s)->id,
2462 "start limit action=%i", s->start_limit_action);
2463 assert_not_reached("Unknown StartLimitAction.");
2469 static int service_start(Unit *u) {
2470 Service *s = SERVICE(u);
2475 /* We cannot fulfill this request right now, try again later
2477 if (s->state == SERVICE_STOP ||
2478 s->state == SERVICE_STOP_SIGTERM ||
2479 s->state == SERVICE_STOP_SIGKILL ||
2480 s->state == SERVICE_STOP_POST ||
2481 s->state == SERVICE_FINAL_SIGTERM ||
2482 s->state == SERVICE_FINAL_SIGKILL)
2485 /* Already on it! */
2486 if (s->state == SERVICE_START_PRE ||
2487 s->state == SERVICE_START ||
2488 s->state == SERVICE_START_POST)
2491 /* A service that will be restarted must be stopped first to
2492 * trigger BindsTo and/or OnFailure dependencies. If a user
2493 * does not want to wait for the holdoff time to elapse, the
2494 * service should be manually restarted, not started. We
2495 * simply return EAGAIN here, so that any start jobs stay
2496 * queued, and assume that the auto restart timer will
2497 * eventually trigger the restart. */
2498 if (s->state == SERVICE_AUTO_RESTART)
2501 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2503 /* Make sure we don't enter a busy loop of some kind. */
2504 r = service_start_limit_test(s);
2506 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2510 s->result = SERVICE_SUCCESS;
2511 s->reload_result = SERVICE_SUCCESS;
2512 s->main_pid_known = false;
2513 s->main_pid_alien = false;
2514 s->forbid_restart = false;
2516 service_enter_start_pre(s);
2520 static int service_stop(Unit *u) {
2521 Service *s = SERVICE(u);
2525 /* Don't create restart jobs from here. */
2526 s->forbid_restart = true;
2529 if (s->state == SERVICE_STOP ||
2530 s->state == SERVICE_STOP_SIGTERM ||
2531 s->state == SERVICE_STOP_SIGKILL ||
2532 s->state == SERVICE_STOP_POST ||
2533 s->state == SERVICE_FINAL_SIGTERM ||
2534 s->state == SERVICE_FINAL_SIGKILL)
2537 /* A restart will be scheduled or is in progress. */
2538 if (s->state == SERVICE_AUTO_RESTART) {
2539 service_set_state(s, SERVICE_DEAD);
2543 /* If there's already something running we go directly into
2545 if (s->state == SERVICE_START_PRE ||
2546 s->state == SERVICE_START ||
2547 s->state == SERVICE_START_POST ||
2548 s->state == SERVICE_RELOAD) {
2549 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2553 assert(s->state == SERVICE_RUNNING ||
2554 s->state == SERVICE_EXITED);
2556 service_enter_stop(s, SERVICE_SUCCESS);
2560 static int service_reload(Unit *u) {
2561 Service *s = SERVICE(u);
2565 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2567 service_enter_reload(s);
2571 _pure_ static bool service_can_reload(Unit *u) {
2572 Service *s = SERVICE(u);
2576 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2579 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2580 Service *s = SERVICE(u);
2586 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2587 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2588 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2590 if (s->control_pid > 0)
2591 unit_serialize_item_format(u, f, "control-pid", "%lu",
2592 (unsigned long) s->control_pid);
2594 if (s->main_pid_known && s->main_pid > 0)
2595 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2597 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2600 unit_serialize_item(u, f, "status-text", s->status_text);
2602 /* FIXME: There's a minor uncleanliness here: if there are
2603 * multiple commands attached here, we will start from the
2604 * first one again */
2605 if (s->control_command_id >= 0)
2606 unit_serialize_item(u, f, "control-command",
2607 service_exec_command_to_string(s->control_command_id));
2609 if (s->socket_fd >= 0) {
2612 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2615 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2618 if (s->main_exec_status.pid > 0) {
2619 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu",
2620 (unsigned long) s->main_exec_status.pid);
2621 dual_timestamp_serialize(f, "main-exec-status-start",
2622 &s->main_exec_status.start_timestamp);
2623 dual_timestamp_serialize(f, "main-exec-status-exit",
2624 &s->main_exec_status.exit_timestamp);
2626 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2627 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
2628 s->main_exec_status.code);
2629 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
2630 s->main_exec_status.status);
2633 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2634 dual_timestamp_serialize(f, "watchdog-timestamp",
2635 &s->watchdog_timestamp);
2637 if (s->exec_context.tmp_dir)
2638 unit_serialize_item(u, f, "tmp-dir", s->exec_context.tmp_dir);
2640 if (s->exec_context.var_tmp_dir)
2641 unit_serialize_item(u, f, "var-tmp-dir", s->exec_context.var_tmp_dir);
2646 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2647 Service *s = SERVICE(u);
2654 if (streq(key, "state")) {
2657 state = service_state_from_string(value);
2659 log_debug_unit(u->id, "Failed to parse state value %s", value);
2661 s->deserialized_state = state;
2662 } else if (streq(key, "result")) {
2665 f = service_result_from_string(value);
2667 log_debug_unit(u->id, "Failed to parse result value %s", value);
2668 else if (f != SERVICE_SUCCESS)
2671 } else if (streq(key, "reload-result")) {
2674 f = service_result_from_string(value);
2676 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2677 else if (f != SERVICE_SUCCESS)
2678 s->reload_result = f;
2680 } else if (streq(key, "control-pid")) {
2683 if (parse_pid(value, &pid) < 0)
2684 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2686 s->control_pid = pid;
2687 } else if (streq(key, "main-pid")) {
2690 if (parse_pid(value, &pid) < 0)
2691 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2693 service_set_main_pid(s, (pid_t) pid);
2694 } else if (streq(key, "main-pid-known")) {
2697 b = parse_boolean(value);
2699 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2701 s->main_pid_known = b;
2702 } else if (streq(key, "status-text")) {
2709 free(s->status_text);
2713 } else if (streq(key, "control-command")) {
2714 ServiceExecCommand id;
2716 id = service_exec_command_from_string(value);
2718 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2720 s->control_command_id = id;
2721 s->control_command = s->exec_command[id];
2723 } else if (streq(key, "socket-fd")) {
2726 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2727 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2730 if (s->socket_fd >= 0)
2731 close_nointr_nofail(s->socket_fd);
2732 s->socket_fd = fdset_remove(fds, fd);
2734 } else if (streq(key, "main-exec-status-pid")) {
2737 if (parse_pid(value, &pid) < 0)
2738 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2740 s->main_exec_status.pid = pid;
2741 } else if (streq(key, "main-exec-status-code")) {
2744 if (safe_atoi(value, &i) < 0)
2745 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2747 s->main_exec_status.code = i;
2748 } else if (streq(key, "main-exec-status-status")) {
2751 if (safe_atoi(value, &i) < 0)
2752 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2754 s->main_exec_status.status = i;
2755 } else if (streq(key, "main-exec-status-start"))
2756 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2757 else if (streq(key, "main-exec-status-exit"))
2758 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2759 else if (streq(key, "watchdog-timestamp"))
2760 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2761 else if (streq(key, "tmp-dir")) {
2768 s->exec_context.tmp_dir = t;
2769 } else if (streq(key, "var-tmp-dir")) {
2776 s->exec_context.var_tmp_dir = t;
2778 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2783 _pure_ static UnitActiveState service_active_state(Unit *u) {
2784 const UnitActiveState *table;
2788 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2790 return table[SERVICE(u)->state];
2793 static const char *service_sub_state_to_string(Unit *u) {
2796 return service_state_to_string(SERVICE(u)->state);
2799 static bool service_check_gc(Unit *u) {
2800 Service *s = SERVICE(u);
2804 /* Never clean up services that still have a process around,
2805 * even if the service is formally dead. */
2806 if (cgroup_good(s) > 0 ||
2807 main_pid_good(s) > 0 ||
2808 control_pid_good(s) > 0)
2811 #ifdef HAVE_SYSV_COMPAT
2819 _pure_ static bool service_check_snapshot(Unit *u) {
2820 Service *s = SERVICE(u);
2824 return !s->got_socket_fd;
2827 static int service_retry_pid_file(Service *s) {
2830 assert(s->pid_file);
2831 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2833 r = service_load_pid_file(s, false);
2837 service_unwatch_pid_file(s);
2839 service_enter_running(s, SERVICE_SUCCESS);
2843 static int service_watch_pid_file(Service *s) {
2846 log_debug_unit(UNIT(s)->id,
2847 "Setting watch for %s's PID file %s",
2848 UNIT(s)->id, s->pid_file_pathspec->path);
2849 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2853 /* the pidfile might have appeared just before we set the watch */
2854 log_debug_unit(UNIT(s)->id,
2855 "Trying to read %s's PID file %s in case it changed",
2856 UNIT(s)->id, s->pid_file_pathspec->path);
2857 service_retry_pid_file(s);
2861 log_error_unit(UNIT(s)->id,
2862 "Failed to set a watch for %s's PID file %s: %s",
2863 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2864 service_unwatch_pid_file(s);
2868 static int service_demand_pid_file(Service *s) {
2871 assert(s->pid_file);
2872 assert(!s->pid_file_pathspec);
2874 ps = new0(PathSpec, 1);
2878 ps->path = strdup(s->pid_file);
2884 path_kill_slashes(ps->path);
2886 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2887 * keep their PID file open all the time. */
2888 ps->type = PATH_MODIFIED;
2889 ps->inotify_fd = -1;
2891 s->pid_file_pathspec = ps;
2893 return service_watch_pid_file(s);
2896 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2897 Service *s = SERVICE(u);
2901 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2902 assert(s->pid_file_pathspec);
2903 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2905 log_debug_unit(u->id, "inotify event for %s", u->id);
2907 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2910 if (service_retry_pid_file(s) == 0)
2913 if (service_watch_pid_file(s) < 0)
2918 service_unwatch_pid_file(s);
2919 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2922 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2923 Service *s = SERVICE(u);
2929 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2930 is_clean_exit_lsb(code, status, &s->success_status))
2931 f = SERVICE_SUCCESS;
2932 else if (code == CLD_EXITED)
2933 f = SERVICE_FAILURE_EXIT_CODE;
2934 else if (code == CLD_KILLED)
2935 f = SERVICE_FAILURE_SIGNAL;
2936 else if (code == CLD_DUMPED)
2937 f = SERVICE_FAILURE_CORE_DUMP;
2939 assert_not_reached("Unknown code");
2941 if (s->main_pid == pid) {
2942 /* Forking services may occasionally move to a new PID.
2943 * As long as they update the PID file before exiting the old
2944 * PID, they're fine. */
2945 if (service_load_pid_file(s, false) == 0)
2949 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2951 if (s->main_command) {
2952 /* If this is not a forking service than the
2953 * main process got started and hence we copy
2954 * the exit status so that it is recorded both
2955 * as main and as control process exit
2958 s->main_command->exec_status = s->main_exec_status;
2960 if (s->main_command->ignore)
2961 f = SERVICE_SUCCESS;
2962 } else if (s->exec_command[SERVICE_EXEC_START]) {
2964 /* If this is a forked process, then we should
2965 * ignore the return value if this was
2966 * configured for the starter process */
2968 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2969 f = SERVICE_SUCCESS;
2972 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2974 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2975 u->id, sigchld_code_to_string(code), status,
2976 strna(code == CLD_EXITED
2977 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2978 : signal_to_string(status)),
2979 "EXIT_CODE=%s", sigchld_code_to_string(code),
2980 "EXIT_STATUS=%i", status,
2983 if (f != SERVICE_SUCCESS)
2986 if (s->main_command &&
2987 s->main_command->command_next &&
2988 f == SERVICE_SUCCESS) {
2990 /* There is another command to *
2991 * execute, so let's do that. */
2993 log_debug_unit(u->id,
2994 "%s running next main command for state %s",
2995 u->id, service_state_to_string(s->state));
2996 service_run_next_main(s);
3000 /* The service exited, so the service is officially
3002 s->main_command = NULL;
3006 case SERVICE_START_POST:
3007 case SERVICE_RELOAD:
3009 /* Need to wait until the operation is
3014 if (s->type == SERVICE_ONESHOT) {
3015 /* This was our main goal, so let's go on */
3016 if (f == SERVICE_SUCCESS)
3017 service_enter_start_post(s);
3019 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3025 case SERVICE_RUNNING:
3026 service_enter_running(s, f);
3029 case SERVICE_STOP_SIGTERM:
3030 case SERVICE_STOP_SIGKILL:
3032 if (!control_pid_good(s))
3033 service_enter_stop_post(s, f);
3035 /* If there is still a control process, wait for that first */
3039 assert_not_reached("Uh, main process died at wrong time.");
3043 } else if (s->control_pid == pid) {
3047 if (s->control_command) {
3048 exec_status_exit(&s->control_command->exec_status,
3049 &s->exec_context, pid, code, status);
3051 if (s->control_command->ignore)
3052 f = SERVICE_SUCCESS;
3055 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
3056 "%s: control process exited, code=%s status=%i",
3057 u->id, sigchld_code_to_string(code), status);
3059 if (f != SERVICE_SUCCESS)
3062 /* Immediately get rid of the cgroup, so that the
3063 * kernel doesn't delay the cgroup empty messages for
3064 * the service cgroup any longer than necessary */
3065 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL,
3066 true, true, NULL, "control");
3068 if (s->control_command &&
3069 s->control_command->command_next &&
3070 f == SERVICE_SUCCESS) {
3072 /* There is another command to *
3073 * execute, so let's do that. */
3075 log_debug_unit(u->id,
3076 "%s running next control command for state %s",
3077 u->id, service_state_to_string(s->state));
3078 service_run_next_control(s);
3081 /* No further commands for this step, so let's
3082 * figure out what to do next */
3084 s->control_command = NULL;
3085 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3087 log_debug_unit(u->id,
3088 "%s got final SIGCHLD for state %s",
3089 u->id, service_state_to_string(s->state));
3093 case SERVICE_START_PRE:
3094 if (f == SERVICE_SUCCESS)
3095 service_enter_start(s);
3097 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3101 if (s->type != SERVICE_FORKING)
3102 /* Maybe spurious event due to a reload that changed the type? */
3105 if (f != SERVICE_SUCCESS) {
3106 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3111 bool has_start_post;
3114 /* Let's try to load the pid file here if we can.
3115 * The PID file might actually be created by a START_POST
3116 * script. In that case don't worry if the loading fails. */
3118 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3119 r = service_load_pid_file(s, !has_start_post);
3120 if (!has_start_post && r < 0) {
3121 r = service_demand_pid_file(s);
3122 if (r < 0 || !cgroup_good(s))
3123 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3127 service_search_main_pid(s);
3129 service_enter_start_post(s);
3132 case SERVICE_START_POST:
3133 if (f != SERVICE_SUCCESS) {
3134 service_enter_stop(s, f);
3141 r = service_load_pid_file(s, true);
3143 r = service_demand_pid_file(s);
3144 if (r < 0 || !cgroup_good(s))
3145 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3149 service_search_main_pid(s);
3151 service_enter_running(s, SERVICE_SUCCESS);
3154 case SERVICE_RELOAD:
3155 if (f == SERVICE_SUCCESS) {
3156 service_load_pid_file(s, true);
3157 service_search_main_pid(s);
3160 s->reload_result = f;
3161 service_enter_running(s, SERVICE_SUCCESS);
3165 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3168 case SERVICE_STOP_SIGTERM:
3169 case SERVICE_STOP_SIGKILL:
3170 if (main_pid_good(s) <= 0)
3171 service_enter_stop_post(s, f);
3173 /* If there is still a service
3174 * process around, wait until
3175 * that one quit, too */
3178 case SERVICE_STOP_POST:
3179 case SERVICE_FINAL_SIGTERM:
3180 case SERVICE_FINAL_SIGKILL:
3181 service_enter_dead(s, f, true);
3185 assert_not_reached("Uh, control process died at wrong time.");
3190 /* Notify clients about changed exit status */
3191 unit_add_to_dbus_queue(u);
3194 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3195 Service *s = SERVICE(u);
3198 assert(elapsed == 1);
3200 if (w == &s->watchdog_watch) {
3201 service_handle_watchdog(s);
3205 assert(w == &s->timer_watch);
3209 case SERVICE_START_PRE:
3211 log_warning_unit(u->id,
3212 "%s operation timed out. Terminating.", u->id);
3213 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3216 case SERVICE_START_POST:
3217 log_warning_unit(u->id,
3218 "%s operation timed out. Stopping.", u->id);
3219 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3222 case SERVICE_RELOAD:
3223 log_warning_unit(u->id,
3224 "%s operation timed out. Stopping.", u->id);
3225 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3226 service_enter_running(s, SERVICE_SUCCESS);
3230 log_warning_unit(u->id,
3231 "%s stopping timed out. Terminating.", u->id);
3232 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3235 case SERVICE_STOP_SIGTERM:
3236 if (s->kill_context.send_sigkill) {
3237 log_warning_unit(u->id,
3238 "%s stopping timed out. Killing.", u->id);
3239 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3241 log_warning_unit(u->id,
3242 "%s stopping timed out. Skipping SIGKILL.", u->id);
3243 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3248 case SERVICE_STOP_SIGKILL:
3249 /* Uh, we sent a SIGKILL and it is still not gone?
3250 * Must be something we cannot kill, so let's just be
3251 * weirded out and continue */
3253 log_warning_unit(u->id,
3254 "%s still around after SIGKILL. Ignoring.", u->id);
3255 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3258 case SERVICE_STOP_POST:
3259 log_warning_unit(u->id,
3260 "%s stopping timed out (2). Terminating.", u->id);
3261 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3264 case SERVICE_FINAL_SIGTERM:
3265 if (s->kill_context.send_sigkill) {
3266 log_warning_unit(u->id,
3267 "%s stopping timed out (2). Killing.", u->id);
3268 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3270 log_warning_unit(u->id,
3271 "%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.",
3273 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3278 case SERVICE_FINAL_SIGKILL:
3279 log_warning_unit(u->id,
3280 "%s still around after SIGKILL (2). Entering failed mode.", u->id);
3281 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3284 case SERVICE_AUTO_RESTART:
3285 log_info_unit(u->id,
3286 "%s holdoff time over, scheduling restart.", u->id);
3287 service_enter_restart(s);
3291 assert_not_reached("Timeout at wrong time.");
3295 static void service_cgroup_notify_event(Unit *u) {
3296 Service *s = SERVICE(u);
3300 log_debug_unit(u->id,
3301 "%s: cgroup is empty", u->id);
3305 /* Waiting for SIGCHLD is usually more interesting,
3306 * because it includes return codes/signals. Which is
3307 * why we ignore the cgroup events for most cases,
3308 * except when we don't know pid which to expect the
3312 case SERVICE_START_POST:
3313 /* If we were hoping for the daemon to write its PID file,
3314 * we can give up now. */
3315 if (s->pid_file_pathspec) {
3316 log_warning_unit(u->id,
3317 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3318 service_unwatch_pid_file(s);
3319 if (s->state == SERVICE_START)
3320 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3322 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3326 case SERVICE_RUNNING:
3327 /* service_enter_running() will figure out what to do */
3328 service_enter_running(s, SERVICE_SUCCESS);
3331 case SERVICE_STOP_SIGTERM:
3332 case SERVICE_STOP_SIGKILL:
3334 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3335 service_enter_stop_post(s, SERVICE_SUCCESS);
3339 case SERVICE_FINAL_SIGTERM:
3340 case SERVICE_FINAL_SIGKILL:
3341 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3342 service_enter_dead(s, SERVICE_SUCCESS, true);
3351 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3352 Service *s = SERVICE(u);
3357 if (s->notify_access == NOTIFY_NONE) {
3358 log_warning_unit(u->id,
3359 "%s: Got notification message from PID %lu, but reception is disabled.",
3360 u->id, (unsigned long) pid);
3364 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3365 log_warning_unit(u->id,
3366 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3367 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3371 log_debug_unit(u->id,
3372 "%s: Got message", u->id);
3374 /* Interpret MAINPID= */
3375 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3376 (s->state == SERVICE_START ||
3377 s->state == SERVICE_START_POST ||
3378 s->state == SERVICE_RUNNING ||
3379 s->state == SERVICE_RELOAD)) {
3381 if (parse_pid(e + 8, &pid) < 0)
3382 log_warning_unit(u->id,
3383 "Failed to parse notification message %s", e);
3385 log_debug_unit(u->id,
3386 "%s: got %s", u->id, e);
3387 service_set_main_pid(s, pid);
3391 /* Interpret READY= */
3392 if (s->type == SERVICE_NOTIFY &&
3393 s->state == SERVICE_START &&
3394 strv_find(tags, "READY=1")) {
3395 log_debug_unit(u->id,
3396 "%s: got READY=1", u->id);
3398 service_enter_start_post(s);
3401 /* Interpret STATUS= */
3402 e = strv_find_prefix(tags, "STATUS=");
3408 if (!utf8_is_valid(e+7)) {
3409 log_warning_unit(u->id,
3410 "Status message in notification is not UTF-8 clean.");
3416 log_error_unit(u->id,
3417 "Failed to allocate string.");
3421 log_debug_unit(u->id,
3422 "%s: got %s", u->id, e);
3424 free(s->status_text);
3427 free(s->status_text);
3428 s->status_text = NULL;
3432 if (strv_find(tags, "WATCHDOG=1")) {
3433 log_debug_unit(u->id,
3434 "%s: got WATCHDOG=1", u->id);
3435 if (dual_timestamp_is_set(&s->watchdog_timestamp))
3436 service_reset_watchdog(s);
3439 /* Notify clients about changed status or main pid */
3440 unit_add_to_dbus_queue(u);
3443 #ifdef HAVE_SYSV_COMPAT
3445 static int service_enumerate(Manager *m) {
3448 _cleanup_closedir_ DIR *d = NULL;
3449 _cleanup_free_ char *path = NULL, *fpath = NULL, *name = NULL;
3450 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
3451 _cleanup_set_free_ Set *shutdown_services = NULL;
3458 if (m->running_as != SYSTEMD_SYSTEM)
3461 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3462 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3466 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3477 if (errno != ENOENT)
3478 log_warning("opendir(%s) failed: %s", path, strerror(errno));
3483 while ((de = readdir(d))) {
3486 if (ignore_file(de->d_name))
3489 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3492 if (strlen(de->d_name) < 4)
3495 a = undecchar(de->d_name[1]);
3496 b = undecchar(de->d_name[2]);
3502 fpath = strjoin(path, "/", de->d_name, NULL);
3508 if (access(fpath, X_OK) < 0) {
3510 if (errno != ENOENT)
3511 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3517 name = sysv_translate_name(de->d_name + 3);
3523 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3525 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3529 if (de->d_name[0] == 'S') {
3531 if (rcnd_table[i].type == RUNLEVEL_UP) {
3532 SERVICE(service)->sysv_start_priority_from_rcnd =
3533 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3535 SERVICE(service)->sysv_enabled = true;
3538 r = set_ensure_allocated(&runlevel_services[i],
3539 trivial_hash_func, trivial_compare_func);
3543 r = set_put(runlevel_services[i], service);
3547 } else if (de->d_name[0] == 'K' &&
3548 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3550 r = set_ensure_allocated(&shutdown_services,
3551 trivial_hash_func, trivial_compare_func);
3555 r = set_put(shutdown_services, service);
3562 /* Now we loaded all stubs and are aware of the lowest
3563 start-up priority for all services, not let's actually load
3564 the services, this will also tell us which services are
3565 actually native now */
3566 manager_dispatch_load_queue(m);
3568 /* If this is a native service, rely on native ways to pull in
3569 * a service, don't pull it in via sysv rcN.d links. */
3570 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3571 SET_FOREACH(service, runlevel_services[i], j) {
3572 service = unit_follow_merge(service);
3574 if (service->fragment_path)
3577 r = unit_add_two_dependencies_by_name_inverse(
3578 service, UNIT_AFTER, UNIT_WANTS,
3579 rcnd_table[i].target, NULL, true);
3584 /* We honour K links only for halt/reboot. For the normal
3585 * runlevels we assume the stop jobs will be implicitly added
3586 * by the core logic. Also, we don't really distinguish here
3587 * between the runlevels 0 and 6 and just add them to the
3588 * special shutdown target. */
3589 SET_FOREACH(service, shutdown_services, j) {
3590 service = unit_follow_merge(service);
3592 if (service->fragment_path)
3595 r = unit_add_two_dependencies_by_name(
3596 service, UNIT_BEFORE, UNIT_CONFLICTS,
3597 SPECIAL_SHUTDOWN_TARGET, NULL, true);
3606 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3607 set_free(runlevel_services[i]);
3613 static void service_bus_name_owner_change(
3616 const char *old_owner,
3617 const char *new_owner) {
3619 Service *s = SERVICE(u);
3624 assert(streq(s->bus_name, name));
3625 assert(old_owner || new_owner);
3627 if (old_owner && new_owner)
3628 log_debug_unit(u->id,
3629 "%s's D-Bus name %s changed owner from %s to %s",
3630 u->id, name, old_owner, new_owner);
3632 log_debug_unit(u->id,
3633 "%s's D-Bus name %s no longer registered by %s",
3634 u->id, name, old_owner);
3636 log_debug_unit(u->id,
3637 "%s's D-Bus name %s now registered by %s",
3638 u->id, name, new_owner);
3640 s->bus_name_good = !!new_owner;
3642 if (s->type == SERVICE_DBUS) {
3644 /* service_enter_running() will figure out what to
3646 if (s->state == SERVICE_RUNNING)
3647 service_enter_running(s, SERVICE_SUCCESS);
3648 else if (s->state == SERVICE_START && new_owner)
3649 service_enter_start_post(s);
3651 } else if (new_owner &&
3653 (s->state == SERVICE_START ||
3654 s->state == SERVICE_START_POST ||
3655 s->state == SERVICE_RUNNING ||
3656 s->state == SERVICE_RELOAD)) {
3658 /* Try to acquire PID from bus service */
3659 log_debug_unit(u->id,
3660 "Trying to acquire PID from D-Bus name...");
3662 bus_query_pid(u->manager, name);
3666 static void service_bus_query_pid_done(
3671 Service *s = SERVICE(u);
3676 log_debug_unit(u->id,
3677 "%s's D-Bus name %s is now owned by process %u",
3678 u->id, name, (unsigned) pid);
3680 if (s->main_pid <= 0 &&
3681 (s->state == SERVICE_START ||
3682 s->state == SERVICE_START_POST ||
3683 s->state == SERVICE_RUNNING ||
3684 s->state == SERVICE_RELOAD))
3685 service_set_main_pid(s, pid);
3688 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3693 /* This is called by the socket code when instantiating a new
3694 * service for a stream socket and the socket needs to be
3697 if (UNIT(s)->load_state != UNIT_LOADED)
3700 if (s->socket_fd >= 0)
3703 if (s->state != SERVICE_DEAD)
3707 s->got_socket_fd = true;
3709 unit_ref_set(&s->accept_socket, UNIT(sock));
3711 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3714 static void service_reset_failed(Unit *u) {
3715 Service *s = SERVICE(u);
3719 if (s->state == SERVICE_FAILED)
3720 service_set_state(s, SERVICE_DEAD);
3722 s->result = SERVICE_SUCCESS;
3723 s->reload_result = SERVICE_SUCCESS;
3725 RATELIMIT_RESET(s->start_limit);
3728 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3729 Service *s = SERVICE(u);
3730 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
3733 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3734 [SERVICE_DEAD] = "dead",
3735 [SERVICE_START_PRE] = "start-pre",
3736 [SERVICE_START] = "start",
3737 [SERVICE_START_POST] = "start-post",
3738 [SERVICE_RUNNING] = "running",
3739 [SERVICE_EXITED] = "exited",
3740 [SERVICE_RELOAD] = "reload",
3741 [SERVICE_STOP] = "stop",
3742 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3743 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3744 [SERVICE_STOP_POST] = "stop-post",
3745 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3746 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3747 [SERVICE_FAILED] = "failed",
3748 [SERVICE_AUTO_RESTART] = "auto-restart",
3751 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3753 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3754 [SERVICE_RESTART_NO] = "no",
3755 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3756 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3757 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3758 [SERVICE_RESTART_ALWAYS] = "always"
3761 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3763 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3764 [SERVICE_SIMPLE] = "simple",
3765 [SERVICE_FORKING] = "forking",
3766 [SERVICE_ONESHOT] = "oneshot",
3767 [SERVICE_DBUS] = "dbus",
3768 [SERVICE_NOTIFY] = "notify",
3769 [SERVICE_IDLE] = "idle"
3772 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3774 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3775 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3776 [SERVICE_EXEC_START] = "ExecStart",
3777 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3778 [SERVICE_EXEC_RELOAD] = "ExecReload",
3779 [SERVICE_EXEC_STOP] = "ExecStop",
3780 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3783 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3785 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3786 [NOTIFY_NONE] = "none",
3787 [NOTIFY_MAIN] = "main",
3788 [NOTIFY_ALL] = "all"
3791 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3793 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3794 [SERVICE_SUCCESS] = "success",
3795 [SERVICE_FAILURE_RESOURCES] = "resources",
3796 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3797 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3798 [SERVICE_FAILURE_SIGNAL] = "signal",
3799 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3800 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3801 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3804 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3806 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3807 [SERVICE_START_LIMIT_NONE] = "none",
3808 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3809 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3810 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3812 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3814 const UnitVTable service_vtable = {
3815 .object_size = sizeof(Service),
3822 .exec_context_offset = offsetof(Service, exec_context),
3823 .exec_section = "Service",
3825 .init = service_init,
3826 .done = service_done,
3827 .load = service_load,
3829 .coldplug = service_coldplug,
3831 .dump = service_dump,
3833 .start = service_start,
3834 .stop = service_stop,
3835 .reload = service_reload,
3837 .can_reload = service_can_reload,
3839 .kill = service_kill,
3841 .serialize = service_serialize,
3842 .deserialize_item = service_deserialize_item,
3844 .active_state = service_active_state,
3845 .sub_state_to_string = service_sub_state_to_string,
3847 .check_gc = service_check_gc,
3848 .check_snapshot = service_check_snapshot,
3850 .sigchld_event = service_sigchld_event,
3851 .timer_event = service_timer_event,
3852 .fd_event = service_fd_event,
3854 .reset_failed = service_reset_failed,
3856 .cgroup_notify_empty = service_cgroup_notify_event,
3857 .notify_message = service_notify_message,
3859 .bus_name_owner_change = service_bus_name_owner_change,
3860 .bus_query_pid_done = service_bus_query_pid_done,
3862 .bus_interface = "org.freedesktop.systemd1.Service",
3863 .bus_message_handler = bus_service_message_handler,
3864 .bus_invalidating_properties = bus_service_invalidating_properties,
3866 #ifdef HAVE_SYSV_COMPAT
3867 .enumerate = service_enumerate,
3869 .status_message_formats = {
3870 .starting_stopping = {
3871 [0] = "Starting %s...",
3872 [1] = "Stopping %s...",
3874 .finished_start_job = {
3875 [JOB_DONE] = "Started %s.",
3876 [JOB_FAILED] = "Failed to start %s.",
3877 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3878 [JOB_TIMEOUT] = "Timed out starting %s.",
3880 .finished_stop_job = {
3881 [JOB_DONE] = "Stopped %s.",
3882 [JOB_FAILED] = "Stopped (with error) %s.",
3883 [JOB_TIMEOUT] = "Timed out stopping %s.",