1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/reboot.h>
31 #include "load-fragment.h"
32 #include "load-dropin.h"
35 #include "unit-name.h"
36 #include "unit-printf.h"
37 #include "dbus-service.h"
39 #include "bus-errors.h"
40 #include "exit-status.h"
42 #include "path-util.h"
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_dead(Service *s, ServiceResult f, bool allow_restart);
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_dead(s, SERVICE_FAILURE_WATCHDOG, true);
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");
334 else if (startswith(name, "rc."))
335 /* Drop rc. prefix */
336 strcpy(stpcpy(r, name + 3), ".service");
338 /* Normal init script name */
339 strcpy(stpcpy(r, name), ".service");
344 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
346 /* We silently ignore the $ prefix here. According to the LSB
347 * spec it simply indicates whether something is a
348 * standardized name or a distribution-specific one. Since we
349 * just follow what already exists and do not introduce new
350 * uses or names we don't care who introduced a new name. */
352 static const char * const table[] = {
353 /* LSB defined facilities */
355 "network", SPECIAL_NETWORK_TARGET,
356 "named", SPECIAL_NSS_LOOKUP_TARGET,
357 "portmap", SPECIAL_RPCBIND_TARGET,
358 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
360 "time", SPECIAL_TIME_SYNC_TARGET,
370 n = *name == '$' ? name + 1 : name;
372 for (i = 0; i < ELEMENTSOF(table); i += 2) {
374 if (!streq(table[i], n))
380 r = strdup(table[i+1]);
387 /* If we don't know this name, fallback heuristics to figure
388 * out whether something is a target or a service alias. */
391 if (!unit_prefix_is_valid(n))
394 /* Facilities starting with $ are most likely targets */
395 r = unit_name_build(n, NULL, ".target");
396 } else if (filename && streq(name, filename))
397 /* Names equaling the file name of the services are redundant */
400 /* Everything else we assume to be normal service names */
401 r = sysv_translate_name(n);
412 static int sysv_fix_order(Service *s) {
418 if (s->sysv_start_priority < 0)
421 /* For each pair of services where at least one lacks a LSB
422 * header, we use the start priority value to order things. */
424 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
427 bool special_s, special_t;
434 if (UNIT(t)->load_state != UNIT_LOADED)
437 if (t->sysv_start_priority < 0)
440 /* If both units have modern headers we don't care
441 * about the priorities */
442 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
443 (UNIT(t)->fragment_path || t->sysv_has_lsb))
446 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
447 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
449 if (special_t && !special_s)
451 else if (special_s && !special_t)
453 else if (t->sysv_start_priority < s->sysv_start_priority)
455 else if (t->sysv_start_priority > s->sysv_start_priority)
460 /* FIXME: Maybe we should compare the name here lexicographically? */
462 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
469 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
472 if (!(c = new0(ExecCommand, 1)))
475 if (!(c->path = strdup(path))) {
480 if (!(c->argv = strv_new(path, arg1, NULL))) {
489 static int sysv_exec_commands(Service *s, const bool supports_reload) {
494 assert(UNIT(s)->source_path);
496 c = exec_command_new(UNIT(s)->source_path, "start");
499 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
501 c = exec_command_new(UNIT(s)->source_path, "stop");
504 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
506 if (supports_reload) {
507 c = exec_command_new(UNIT(s)->source_path, "reload");
510 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
516 static bool usage_contains_reload(const char *line) {
517 return (strcasestr(line, "{reload|") ||
518 strcasestr(line, "{reload}") ||
519 strcasestr(line, "{reload\"") ||
520 strcasestr(line, "|reload|") ||
521 strcasestr(line, "|reload}") ||
522 strcasestr(line, "|reload\""));
525 static int service_load_sysv_path(Service *s, const char *path) {
537 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
539 bool supports_reload = false;
546 f = fopen(path, "re");
548 r = errno == ENOENT ? 0 : -errno;
552 if (fstat(fileno(f), &st) < 0) {
557 free(u->source_path);
558 u->source_path = strdup(path);
559 if (!u->source_path) {
563 u->source_mtime = timespec_load(&st.st_mtim);
565 if (null_or_empty(&st)) {
566 u->load_state = UNIT_MASKED;
574 char l[LINE_MAX], *t;
576 if (!fgets(l, sizeof(l), f)) {
581 log_error_unit(u->id,
582 "Failed to read configuration file '%s': %s",
591 /* Try to figure out whether this init script supports
592 * the reload operation. This heuristic looks for
593 * "Usage" lines which include the reload option. */
594 if ( state == USAGE_CONTINUATION ||
595 (state == NORMAL && strcasestr(t, "usage"))) {
596 if (usage_contains_reload(t)) {
597 supports_reload = true;
599 } else if (t[strlen(t)-1] == '\\')
600 state = USAGE_CONTINUATION;
608 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
610 s->sysv_has_lsb = true;
614 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
620 t += strspn(t, WHITESPACE);
622 if (state == NORMAL) {
624 /* Try to parse Red Hat style chkconfig headers */
626 if (startswith_no_case(t, "chkconfig:")) {
628 char runlevels[16], *k;
632 if (sscanf(t+10, "%15s %i %*i",
634 &start_priority) != 2) {
636 log_warning_unit(u->id,
637 "[%s:%u] Failed to parse chkconfig line. Ignoring.",
642 /* A start priority gathered from the
643 * symlink farms is preferred over the
644 * data from the LSB header. */
645 if (start_priority < 0 || start_priority > 99)
646 log_warning_unit(u->id,
647 "[%s:%u] Start priority out of range. Ignoring.",
650 s->sysv_start_priority = start_priority;
652 char_array_0(runlevels);
653 k = delete_chars(runlevels, WHITESPACE "-");
658 if (!(d = strdup(k))) {
663 free(s->sysv_runlevels);
664 s->sysv_runlevels = d;
667 } else if (startswith_no_case(t, "description:")) {
669 size_t k = strlen(t);
673 if (t[k-1] == '\\') {
678 if ((j = strstrip(t+12)) && *j) {
679 if (!(d = strdup(j))) {
686 free(chkconfig_description);
687 chkconfig_description = d;
689 } else if (startswith_no_case(t, "pidfile:")) {
696 if (!path_is_absolute(fn)) {
697 log_warning_unit(u->id,
698 "[%s:%u] PID file not absolute. Ignoring.",
703 if (!(fn = strdup(fn))) {
712 } else if (state == DESCRIPTION) {
714 /* Try to parse Red Hat style description
717 size_t k = strlen(t);
725 if ((j = strstrip(t)) && *j) {
728 if (chkconfig_description)
729 d = strjoin(chkconfig_description, " ", j, NULL);
738 free(chkconfig_description);
739 chkconfig_description = d;
742 } else if (state == LSB || state == LSB_DESCRIPTION) {
744 if (startswith_no_case(t, "Provides:")) {
750 FOREACH_WORD_QUOTED(w, z, t+9, i) {
753 if (!(n = strndup(w, z))) {
758 r = sysv_translate_facility(n, path_get_file_name(path), &m);
767 if (unit_name_to_type(m) == UNIT_SERVICE)
768 r = unit_add_name(u, m);
775 * indication that the
777 * now available. This
780 * targets do NOT pull
783 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
786 log_error_unit(u->id,
787 "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
788 path, line, m, strerror(-r));
793 } else if (startswith_no_case(t, "Required-Start:") ||
794 startswith_no_case(t, "Should-Start:") ||
795 startswith_no_case(t, "X-Start-Before:") ||
796 startswith_no_case(t, "X-Start-After:")) {
802 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
805 if (!(n = strndup(w, z))) {
810 r = sysv_translate_facility(n, path_get_file_name(path), &m);
812 log_error_unit(u->id,
813 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
814 path, line, n, strerror(-r));
824 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
827 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
828 path, line, m, strerror(-r));
832 } else if (startswith_no_case(t, "Default-Start:")) {
837 k = delete_chars(t+14, WHITESPACE "-");
840 if (!(d = strdup(k))) {
845 free(s->sysv_runlevels);
846 s->sysv_runlevels = d;
849 } else if (startswith_no_case(t, "Description:")) {
852 state = LSB_DESCRIPTION;
854 if ((j = strstrip(t+12)) && *j) {
855 if (!(d = strdup(j))) {
862 free(long_description);
863 long_description = d;
865 } else if (startswith_no_case(t, "Short-Description:")) {
870 if ((j = strstrip(t+18)) && *j) {
871 if (!(d = strdup(j))) {
878 free(short_description);
879 short_description = d;
881 } else if (state == LSB_DESCRIPTION) {
883 if (startswith(l, "#\t") || startswith(l, "# ")) {
886 if ((j = strstrip(t)) && *j) {
889 if (long_description)
890 d = strjoin(long_description, " ", t, NULL);
899 free(long_description);
900 long_description = d;
909 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
912 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
913 /* If there a runlevels configured for this service
914 * but none of the standard ones, then we assume this
915 * is some special kind of service (which might be
916 * needed for early boot) and don't create any links
919 UNIT(s)->default_dependencies = false;
921 /* Don't timeout special services during boot (like fsck) */
922 s->timeout_start_usec = 0;
923 s->timeout_stop_usec = 0;
925 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
926 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
929 /* Special setting for all SysV services */
930 s->type = SERVICE_FORKING;
931 s->remain_after_exit = !s->pid_file;
932 s->guess_main_pid = false;
933 s->restart = SERVICE_RESTART_NO;
934 s->exec_context.ignore_sigpipe = false;
935 s->kill_context.kill_mode = KILL_PROCESS;
937 /* We use the long description only if
938 * no short description is set. */
940 if (short_description)
941 description = short_description;
942 else if (chkconfig_description)
943 description = chkconfig_description;
944 else if (long_description)
945 description = long_description;
952 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
960 /* The priority that has been set in /etc/rcN.d/ hierarchies
961 * takes precedence over what is stored as default in the LSB
963 if (s->sysv_start_priority_from_rcnd >= 0)
964 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
966 u->load_state = UNIT_LOADED;
974 free(short_description);
975 free(long_description);
976 free(chkconfig_description);
981 static int service_load_sysv_name(Service *s, const char *name) {
987 /* For SysV services we strip the rc.* and *.sh
988 * prefixes/suffixes. */
989 if (startswith(name, "rc.") ||
990 endswith(name, ".sh.service"))
993 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
997 path = strjoin(*p, "/", name, NULL);
1001 assert(endswith(path, ".service"));
1002 path[strlen(path)-8] = 0;
1004 r = service_load_sysv_path(s, path);
1006 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1007 /* Try *.sh source'able init scripts */
1008 strcat(path, ".sh");
1009 r = service_load_sysv_path(s, path);
1013 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1014 /* Try rc.* init scripts */
1016 path = strjoin(*p, "/rc.", name, NULL);
1020 /* Drop .service suffix */
1021 path[strlen(path)-8] = 0;
1022 r = service_load_sysv_path(s, path);
1029 if (UNIT(s)->load_state != UNIT_STUB)
1036 static int service_load_sysv(Service *s) {
1043 /* Load service data from SysV init scripts, preferably with
1044 * LSB headers ... */
1046 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1049 if ((t = UNIT(s)->id))
1050 if ((r = service_load_sysv_name(s, t)) < 0)
1053 if (UNIT(s)->load_state == UNIT_STUB)
1054 SET_FOREACH(t, UNIT(s)->names, i) {
1055 if (t == UNIT(s)->id)
1058 if ((r = service_load_sysv_name(s, t)) < 0)
1061 if (UNIT(s)->load_state != UNIT_STUB)
1069 static int fsck_fix_order(Service *s) {
1075 if (s->fsck_passno <= 0)
1078 /* For each pair of services where both have an fsck priority
1079 * we order things based on it. */
1081 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1090 if (UNIT(t)->load_state != UNIT_LOADED)
1093 if (t->fsck_passno <= 0)
1096 if (t->fsck_passno < s->fsck_passno)
1098 else if (t->fsck_passno > s->fsck_passno)
1103 r = unit_add_dependency(UNIT(s), d, UNIT(t), true);
1111 static int service_verify(Service *s) {
1114 if (UNIT(s)->load_state != UNIT_LOADED)
1117 if (!s->exec_command[SERVICE_EXEC_START]) {
1118 log_error_unit(UNIT(s)->id,
1119 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1123 if (s->type != SERVICE_ONESHOT &&
1124 s->exec_command[SERVICE_EXEC_START]->command_next) {
1125 log_error_unit(UNIT(s)->id,
1126 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1130 if (s->type == SERVICE_DBUS && !s->bus_name) {
1131 log_error_unit(UNIT(s)->id,
1132 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1136 if (s->bus_name && s->type != SERVICE_DBUS)
1137 log_warning_unit(UNIT(s)->id,
1138 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1140 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1141 log_error_unit(UNIT(s)->id,
1142 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1149 static int service_add_default_dependencies(Service *s) {
1154 /* Add a number of automatic dependencies useful for the
1155 * majority of services. */
1157 /* First, pull in base system */
1158 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1159 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1160 SPECIAL_BASIC_TARGET, NULL, true);
1164 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1165 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1166 SPECIAL_SOCKETS_TARGET, NULL, true);
1171 /* Second, activate normal shutdown */
1172 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
1173 SPECIAL_SHUTDOWN_TARGET, NULL, true);
1177 static void service_fix_output(Service *s) {
1180 /* If nothing has been explicitly configured, patch default
1181 * output in. If input is socket/tty we avoid this however,
1182 * since in that case we want output to default to the same
1183 * place as we read input from. */
1185 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1186 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1187 s->exec_context.std_input == EXEC_INPUT_NULL)
1188 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1190 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1191 s->exec_context.std_input == EXEC_INPUT_NULL)
1192 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1195 static int service_load(Unit *u) {
1197 Service *s = SERVICE(u);
1201 /* Load a .service file */
1202 if ((r = unit_load_fragment(u)) < 0)
1205 #ifdef HAVE_SYSV_COMPAT
1206 /* Load a classic init script as a fallback, if we couldn't find anything */
1207 if (u->load_state == UNIT_STUB)
1208 if ((r = service_load_sysv(s)) < 0)
1212 /* Still nothing found? Then let's give up */
1213 if (u->load_state == UNIT_STUB)
1216 /* We were able to load something, then let's add in the
1217 * dropin directories. */
1218 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1221 /* This is a new unit? Then let's add in some extras */
1222 if (u->load_state == UNIT_LOADED) {
1223 if (s->type == _SERVICE_TYPE_INVALID)
1224 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1226 /* Oneshot services have disabled start timeout by default */
1227 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1228 s->timeout_start_usec = 0;
1230 service_fix_output(s);
1232 r = unit_add_exec_dependencies(u, &s->exec_context);
1236 r = unit_add_default_cgroups(u);
1240 #ifdef HAVE_SYSV_COMPAT
1241 r = sysv_fix_order(s);
1246 r = fsck_fix_order(s);
1251 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1254 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1255 s->notify_access = NOTIFY_MAIN;
1257 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1258 s->notify_access = NOTIFY_MAIN;
1260 if (s->type == SERVICE_DBUS || s->bus_name) {
1261 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES,
1262 SPECIAL_DBUS_SOCKET, NULL, true);
1267 if (UNIT(s)->default_dependencies) {
1268 r = service_add_default_dependencies(s);
1273 r = unit_exec_context_defaults(u, &s->exec_context);
1278 return service_verify(s);
1281 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1283 ServiceExecCommand c;
1284 Service *s = SERVICE(u);
1285 const char *prefix2;
1286 char _cleanup_free_ *p2 = NULL;
1290 p2 = strappend(prefix, "\t");
1291 prefix2 = p2 ? p2 : prefix;
1294 "%sService State: %s\n"
1296 "%sReload Result: %s\n"
1297 "%sPermissionsStartOnly: %s\n"
1298 "%sRootDirectoryStartOnly: %s\n"
1299 "%sRemainAfterExit: %s\n"
1300 "%sGuessMainPID: %s\n"
1303 "%sNotifyAccess: %s\n",
1304 prefix, service_state_to_string(s->state),
1305 prefix, service_result_to_string(s->result),
1306 prefix, service_result_to_string(s->reload_result),
1307 prefix, yes_no(s->permissions_start_only),
1308 prefix, yes_no(s->root_directory_start_only),
1309 prefix, yes_no(s->remain_after_exit),
1310 prefix, yes_no(s->guess_main_pid),
1311 prefix, service_type_to_string(s->type),
1312 prefix, service_restart_to_string(s->restart),
1313 prefix, notify_access_to_string(s->notify_access));
1315 if (s->control_pid > 0)
1317 "%sControl PID: %lu\n",
1318 prefix, (unsigned long) s->control_pid);
1320 if (s->main_pid > 0)
1323 "%sMain PID Known: %s\n"
1324 "%sMain PID Alien: %s\n",
1325 prefix, (unsigned long) s->main_pid,
1326 prefix, yes_no(s->main_pid_known),
1327 prefix, yes_no(s->main_pid_alien));
1332 prefix, s->pid_file);
1337 "%sBus Name Good: %s\n",
1338 prefix, s->bus_name,
1339 prefix, yes_no(s->bus_name_good));
1341 kill_context_dump(&s->kill_context, f, prefix);
1342 exec_context_dump(&s->exec_context, f, prefix);
1344 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1346 if (!s->exec_command[c])
1349 fprintf(f, "%s-> %s:\n",
1350 prefix, service_exec_command_to_string(c));
1352 exec_command_dump_list(s->exec_command[c], f, prefix2);
1355 #ifdef HAVE_SYSV_COMPAT
1358 "%sSysV Init Script has LSB Header: %s\n"
1359 "%sSysVEnabled: %s\n",
1360 prefix, yes_no(s->sysv_has_lsb),
1361 prefix, yes_no(s->sysv_enabled));
1363 if (s->sysv_start_priority >= 0)
1365 "%sSysVStartPriority: %i\n",
1366 prefix, s->sysv_start_priority);
1368 if (s->sysv_runlevels)
1369 fprintf(f, "%sSysVRunLevels: %s\n",
1370 prefix, s->sysv_runlevels);
1373 if (s->fsck_passno > 0)
1375 "%sFsckPassNo: %i\n",
1376 prefix, s->fsck_passno);
1379 fprintf(f, "%sStatus Text: %s\n",
1380 prefix, s->status_text);
1383 static int service_load_pid_file(Service *s, bool may_warn) {
1384 char _cleanup_free_ *k = NULL;
1393 r = read_one_line_file(s->pid_file, &k);
1396 log_info_unit(UNIT(s)->id,
1397 "PID file %s not readable (yet?) after %s.",
1398 s->pid_file, service_state_to_string(s->state));
1402 r = parse_pid(k, &pid);
1405 log_info_unit(UNIT(s)->id,
1406 "Failed to read PID from file %s: %s",
1407 s->pid_file, strerror(-r));
1411 if (kill(pid, 0) < 0 && errno != EPERM) {
1413 log_info_unit(UNIT(s)->id,
1414 "PID %lu read from file %s does not exist.",
1415 (unsigned long) pid, s->pid_file);
1419 if (s->main_pid_known) {
1420 if (pid == s->main_pid)
1423 log_debug_unit(UNIT(s)->id,
1424 "Main PID changing: %lu -> %lu",
1425 (unsigned long) s->main_pid, (unsigned long) pid);
1426 service_unwatch_main_pid(s);
1427 s->main_pid_known = false;
1429 log_debug_unit(UNIT(s)->id,
1430 "Main PID loaded: %lu", (unsigned long) pid);
1432 r = service_set_main_pid(s, pid);
1436 r = unit_watch_pid(UNIT(s), pid);
1438 /* FIXME: we need to do something here */
1439 log_warning_unit(UNIT(s)->id,
1440 "Failed to watch PID %lu from service %s",
1441 (unsigned long) pid, UNIT(s)->id);
1448 static int service_search_main_pid(Service *s) {
1454 /* If we know it anyway, don't ever fallback to unreliable
1456 if (s->main_pid_known)
1459 if (!s->guess_main_pid)
1462 assert(s->main_pid <= 0);
1464 pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings);
1468 log_debug_unit(UNIT(s)->id,
1469 "Main PID guessed: %lu", (unsigned long) pid);
1470 r = service_set_main_pid(s, pid);
1474 r = unit_watch_pid(UNIT(s), pid);
1476 /* FIXME: we need to do something here */
1482 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1488 /* Notifies all our sockets when we die */
1490 if (s->socket_fd >= 0)
1493 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1494 if (u->type == UNIT_SOCKET)
1495 socket_notify_service_dead(SOCKET(u), failed_permanent);
1500 static void service_set_state(Service *s, ServiceState state) {
1501 ServiceState old_state;
1502 const UnitActiveState *table;
1505 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1507 old_state = s->state;
1510 service_unwatch_pid_file(s);
1512 if (state != SERVICE_START_PRE &&
1513 state != SERVICE_START &&
1514 state != SERVICE_START_POST &&
1515 state != SERVICE_RELOAD &&
1516 state != SERVICE_STOP &&
1517 state != SERVICE_STOP_SIGTERM &&
1518 state != SERVICE_STOP_SIGKILL &&
1519 state != SERVICE_STOP_POST &&
1520 state != SERVICE_FINAL_SIGTERM &&
1521 state != SERVICE_FINAL_SIGKILL &&
1522 state != SERVICE_AUTO_RESTART)
1523 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1525 if (state != SERVICE_START &&
1526 state != SERVICE_START_POST &&
1527 state != SERVICE_RUNNING &&
1528 state != SERVICE_RELOAD &&
1529 state != SERVICE_STOP &&
1530 state != SERVICE_STOP_SIGTERM &&
1531 state != SERVICE_STOP_SIGKILL) {
1532 service_unwatch_main_pid(s);
1533 s->main_command = NULL;
1536 if (state != SERVICE_START_PRE &&
1537 state != SERVICE_START &&
1538 state != SERVICE_START_POST &&
1539 state != SERVICE_RELOAD &&
1540 state != SERVICE_STOP &&
1541 state != SERVICE_STOP_SIGTERM &&
1542 state != SERVICE_STOP_SIGKILL &&
1543 state != SERVICE_STOP_POST &&
1544 state != SERVICE_FINAL_SIGTERM &&
1545 state != SERVICE_FINAL_SIGKILL) {
1546 service_unwatch_control_pid(s);
1547 s->control_command = NULL;
1548 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1551 if (state == SERVICE_FAILED)
1552 service_notify_sockets_dead(s, s->result == SERVICE_FAILURE_START_LIMIT);
1554 if (state == SERVICE_DEAD ||
1555 state == SERVICE_STOP ||
1556 state == SERVICE_STOP_SIGTERM ||
1557 state == SERVICE_STOP_SIGKILL ||
1558 state == SERVICE_STOP_POST ||
1559 state == SERVICE_FINAL_SIGTERM ||
1560 state == SERVICE_FINAL_SIGKILL ||
1561 state == SERVICE_AUTO_RESTART)
1562 service_notify_sockets_dead(s, false);
1564 if (state != SERVICE_START_PRE &&
1565 state != SERVICE_START &&
1566 state != SERVICE_START_POST &&
1567 state != SERVICE_RUNNING &&
1568 state != SERVICE_RELOAD &&
1569 state != SERVICE_STOP &&
1570 state != SERVICE_STOP_SIGTERM &&
1571 state != SERVICE_STOP_SIGKILL &&
1572 state != SERVICE_STOP_POST &&
1573 state != SERVICE_FINAL_SIGTERM &&
1574 state != SERVICE_FINAL_SIGKILL &&
1575 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1576 service_close_socket_fd(s);
1577 service_connection_unref(s);
1580 if (state == SERVICE_STOP || state == SERVICE_STOP_SIGTERM)
1581 service_stop_watchdog(s);
1583 /* For the inactive states unit_notify() will trim the cgroup,
1584 * but for exit we have to do that ourselves... */
1585 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1586 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1588 if (old_state != state)
1589 log_debug_unit(UNIT(s)->id,
1590 "%s changed %s -> %s", UNIT(s)->id,
1591 service_state_to_string(old_state),
1592 service_state_to_string(state));
1594 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1595 s->reload_result = SERVICE_SUCCESS;
1598 static int service_coldplug(Unit *u) {
1599 Service *s = SERVICE(u);
1603 assert(s->state == SERVICE_DEAD);
1605 if (s->deserialized_state != s->state) {
1607 if (s->deserialized_state == SERVICE_START_PRE ||
1608 s->deserialized_state == SERVICE_START ||
1609 s->deserialized_state == SERVICE_START_POST ||
1610 s->deserialized_state == SERVICE_RELOAD ||
1611 s->deserialized_state == SERVICE_STOP ||
1612 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1613 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1614 s->deserialized_state == SERVICE_STOP_POST ||
1615 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1616 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1617 s->deserialized_state == SERVICE_AUTO_RESTART) {
1618 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1621 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1623 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
1629 if ((s->deserialized_state == SERVICE_START &&
1630 (s->type == SERVICE_FORKING ||
1631 s->type == SERVICE_DBUS ||
1632 s->type == SERVICE_ONESHOT ||
1633 s->type == SERVICE_NOTIFY)) ||
1634 s->deserialized_state == SERVICE_START_POST ||
1635 s->deserialized_state == SERVICE_RUNNING ||
1636 s->deserialized_state == SERVICE_RELOAD ||
1637 s->deserialized_state == SERVICE_STOP ||
1638 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1639 s->deserialized_state == SERVICE_STOP_SIGKILL)
1640 if (s->main_pid > 0) {
1641 r = unit_watch_pid(UNIT(s), s->main_pid);
1646 if (s->deserialized_state == SERVICE_START_PRE ||
1647 s->deserialized_state == SERVICE_START ||
1648 s->deserialized_state == SERVICE_START_POST ||
1649 s->deserialized_state == SERVICE_RELOAD ||
1650 s->deserialized_state == SERVICE_STOP ||
1651 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1652 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1653 s->deserialized_state == SERVICE_STOP_POST ||
1654 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1655 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1656 if (s->control_pid > 0) {
1657 r = unit_watch_pid(UNIT(s), s->control_pid);
1662 if (s->deserialized_state == SERVICE_START_POST ||
1663 s->deserialized_state == SERVICE_RUNNING)
1664 service_handle_watchdog(s);
1666 service_set_state(s, s->deserialized_state);
1671 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1675 unsigned rn_fds = 0;
1682 if (s->socket_fd >= 0)
1685 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1690 if (u->type != UNIT_SOCKET)
1695 r = socket_collect_fds(sock, &cfds, &cn_fds);
1708 t = new(int, rn_fds+cn_fds);
1715 memcpy(t, rfds, rn_fds * sizeof(int));
1716 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1721 rn_fds = rn_fds+cn_fds;
1736 static int service_spawn(
1741 bool apply_permissions,
1743 bool apply_tty_stdin,
1744 bool set_notify_socket,
1751 int _cleanup_free_ *fdsbuf = NULL;
1752 unsigned n_fds = 0, n_env = 0;
1753 char _cleanup_strv_free_
1754 **argv = NULL, **final_env = NULL, **our_env = NULL;
1761 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1762 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1763 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1765 if (s->socket_fd >= 0) {
1766 fds = &s->socket_fd;
1769 r = service_collect_fds(s, &fdsbuf, &n_fds);
1777 if (timeout && s->timeout_start_usec) {
1778 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true,
1779 s->timeout_start_usec, &s->timer_watch);
1783 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1785 argv = unit_full_printf_strv(UNIT(s), c->argv);
1791 our_env = new0(char*, 5);
1797 if (set_notify_socket)
1798 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1803 if (s->main_pid > 0)
1804 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1809 if (s->watchdog_usec > 0)
1810 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1815 if (s->meta.manager->running_as != SYSTEMD_SYSTEM)
1816 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1821 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1835 UNIT(s)->manager->confirm_spawn,
1836 UNIT(s)->cgroup_bondings,
1837 UNIT(s)->cgroup_attributes,
1838 is_control ? "control" : NULL,
1840 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1845 r = unit_watch_pid(UNIT(s), pid);
1847 /* FIXME: we need to do something here */
1856 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1861 static int main_pid_good(Service *s) {
1864 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1867 /* If we know the pid file, then lets just check if it is
1869 if (s->main_pid_known) {
1871 /* If it's an alien child let's check if it is still
1873 if (s->main_pid_alien)
1874 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1876 /* .. otherwise assume we'll get a SIGCHLD for it,
1877 * which we really should wait for to collect exit
1878 * status and code */
1879 return s->main_pid > 0;
1882 /* We don't know the pid */
1886 static int control_pid_good(Service *s) {
1889 return s->control_pid > 0;
1892 static int cgroup_good(Service *s) {
1897 r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings);
1904 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1908 if (f != SERVICE_SUCCESS)
1911 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1913 if (allow_restart &&
1914 !s->forbid_restart &&
1915 (s->restart == SERVICE_RESTART_ALWAYS ||
1916 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1917 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1918 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1919 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1920 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1921 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1922 (s->result != SERVICE_FAILURE_SIGNAL ||
1923 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1926 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
1930 service_set_state(s, SERVICE_AUTO_RESTART);
1933 s->forbid_restart = false;
1935 /* we want fresh tmpdirs in case service is started again immediately */
1936 exec_context_tmp_dirs_done(&s->exec_context);
1941 log_warning_unit(UNIT(s)->id,
1942 "%s failed to run install restart timer: %s",
1943 UNIT(s)->id, strerror(-r));
1944 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1947 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1949 static void service_enter_stop_post(Service *s, ServiceResult f) {
1953 if (f != SERVICE_SUCCESS)
1956 service_unwatch_control_pid(s);
1958 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1959 if (s->control_command) {
1960 s->control_command_id = SERVICE_EXEC_STOP_POST;
1962 r = service_spawn(s,
1966 !s->permissions_start_only,
1967 !s->root_directory_start_only,
1976 service_set_state(s, SERVICE_STOP_POST);
1978 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1983 log_warning_unit(UNIT(s)->id,
1984 "%s failed to run 'stop-post' task: %s",
1985 UNIT(s)->id, strerror(-r));
1986 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1989 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1994 if (f != SERVICE_SUCCESS)
1997 r = unit_kill_context(
2000 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
2008 if (s->timeout_stop_usec > 0) {
2009 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true,
2010 s->timeout_stop_usec, &s->timer_watch);
2015 service_set_state(s, state);
2016 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2017 service_enter_stop_post(s, SERVICE_SUCCESS);
2019 service_enter_dead(s, SERVICE_SUCCESS, true);
2024 log_warning_unit(UNIT(s)->id,
2025 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2027 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2028 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2030 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2033 static void service_enter_stop(Service *s, ServiceResult f) {
2038 if (f != SERVICE_SUCCESS)
2041 service_unwatch_control_pid(s);
2043 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
2044 if (s->control_command) {
2045 s->control_command_id = SERVICE_EXEC_STOP;
2047 r = service_spawn(s,
2051 !s->permissions_start_only,
2052 !s->root_directory_start_only,
2060 service_set_state(s, SERVICE_STOP);
2062 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2067 log_warning_unit(UNIT(s)->id,
2068 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2069 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2072 static void service_enter_running(Service *s, ServiceResult f) {
2073 int main_pid_ok, cgroup_ok;
2076 if (f != SERVICE_SUCCESS)
2079 main_pid_ok = main_pid_good(s);
2080 cgroup_ok = cgroup_good(s);
2082 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2083 (s->bus_name_good || s->type != SERVICE_DBUS))
2084 service_set_state(s, SERVICE_RUNNING);
2085 else if (s->remain_after_exit)
2086 service_set_state(s, SERVICE_EXITED);
2088 service_enter_stop(s, SERVICE_SUCCESS);
2091 static void service_enter_start_post(Service *s) {
2095 service_unwatch_control_pid(s);
2097 if (s->watchdog_usec > 0)
2098 service_reset_watchdog(s);
2100 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
2101 if (s->control_command) {
2102 s->control_command_id = SERVICE_EXEC_START_POST;
2104 r = service_spawn(s,
2108 !s->permissions_start_only,
2109 !s->root_directory_start_only,
2117 service_set_state(s, SERVICE_START_POST);
2119 service_enter_running(s, SERVICE_SUCCESS);
2124 log_warning_unit(UNIT(s)->id,
2125 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2126 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2129 static void service_enter_start(Service *s) {
2136 assert(s->exec_command[SERVICE_EXEC_START]);
2137 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2139 if (s->type == SERVICE_FORKING)
2140 service_unwatch_control_pid(s);
2142 service_unwatch_main_pid(s);
2144 /* We want to ensure that nobody leaks processes from
2145 * START_PRE here, so let's go on a killing spree, People
2146 * should not spawn long running processes from START_PRE. */
2147 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2149 if (s->type == SERVICE_FORKING) {
2150 s->control_command_id = SERVICE_EXEC_START;
2151 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2153 s->main_command = NULL;
2155 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2156 s->control_command = NULL;
2158 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2161 r = service_spawn(s,
2163 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
2164 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2169 s->notify_access != NOTIFY_NONE,
2175 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2176 /* For simple services we immediately start
2177 * the START_POST binaries. */
2179 service_set_main_pid(s, pid);
2180 service_enter_start_post(s);
2182 } else if (s->type == SERVICE_FORKING) {
2184 /* For forking services we wait until the start
2185 * process exited. */
2187 s->control_pid = pid;
2188 service_set_state(s, SERVICE_START);
2190 } else if (s->type == SERVICE_ONESHOT ||
2191 s->type == SERVICE_DBUS ||
2192 s->type == SERVICE_NOTIFY) {
2194 /* For oneshot services we wait until the start
2195 * process exited, too, but it is our main process. */
2197 /* For D-Bus services we know the main pid right away,
2198 * but wait for the bus name to appear on the
2199 * bus. Notify services are similar. */
2201 service_set_main_pid(s, pid);
2202 service_set_state(s, SERVICE_START);
2204 assert_not_reached("Unknown service type");
2209 log_warning_unit(UNIT(s)->id,
2210 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2211 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2214 static void service_enter_start_pre(Service *s) {
2219 service_unwatch_control_pid(s);
2221 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
2222 if (s->control_command) {
2224 /* Before we start anything, let's clear up what might
2225 * be left from previous runs. */
2226 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL,
2227 true,true, NULL, "control");
2229 s->control_command_id = SERVICE_EXEC_START_PRE;
2231 r = service_spawn(s,
2235 !s->permissions_start_only,
2236 !s->root_directory_start_only,
2244 service_set_state(s, SERVICE_START_PRE);
2246 service_enter_start(s);
2251 log_warning_unit(UNIT(s)->id,
2252 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2253 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2256 static void service_enter_restart(Service *s) {
2261 dbus_error_init(&error);
2263 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2264 /* Don't restart things if we are going down anyway */
2265 log_info_unit(UNIT(s)->id,
2266 "Stop job pending for unit, delaying automatic restart.");
2268 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
2275 /* Any units that are bound to this service must also be
2276 * restarted. We use JOB_RESTART (instead of the more obvious
2277 * JOB_START) here so that those dependency jobs will be added
2279 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2283 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2284 * it will be canceled as part of the service_stop() call that
2285 * is executed as part of JOB_RESTART. */
2287 log_debug_unit(UNIT(s)->id,
2288 "%s scheduled restart job.", UNIT(s)->id);
2292 log_warning_unit(UNIT(s)->id,
2293 "%s failed to schedule restart job: %s",
2294 UNIT(s)->id, bus_error(&error, -r));
2295 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2297 dbus_error_free(&error);
2300 static void service_enter_reload(Service *s) {
2305 service_unwatch_control_pid(s);
2307 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
2308 if (s->control_command) {
2309 s->control_command_id = SERVICE_EXEC_RELOAD;
2311 r = service_spawn(s,
2315 !s->permissions_start_only,
2316 !s->root_directory_start_only,
2324 service_set_state(s, SERVICE_RELOAD);
2326 service_enter_running(s, SERVICE_SUCCESS);
2331 log_warning_unit(UNIT(s)->id,
2332 "%s failed to run 'reload' task: %s",
2333 UNIT(s)->id, strerror(-r));
2334 s->reload_result = SERVICE_FAILURE_RESOURCES;
2335 service_enter_running(s, SERVICE_SUCCESS);
2338 static void service_run_next_control(Service *s) {
2342 assert(s->control_command);
2343 assert(s->control_command->command_next);
2345 assert(s->control_command_id != SERVICE_EXEC_START);
2347 s->control_command = s->control_command->command_next;
2348 service_unwatch_control_pid(s);
2350 r = service_spawn(s,
2354 !s->permissions_start_only,
2355 !s->root_directory_start_only,
2356 s->control_command_id == SERVICE_EXEC_START_PRE ||
2357 s->control_command_id == SERVICE_EXEC_STOP_POST,
2367 log_warning_unit(UNIT(s)->id,
2368 "%s failed to run next control task: %s",
2369 UNIT(s)->id, strerror(-r));
2371 if (s->state == SERVICE_START_PRE)
2372 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2373 else if (s->state == SERVICE_STOP)
2374 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2375 else if (s->state == SERVICE_STOP_POST)
2376 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2377 else if (s->state == SERVICE_RELOAD) {
2378 s->reload_result = SERVICE_FAILURE_RESOURCES;
2379 service_enter_running(s, SERVICE_SUCCESS);
2381 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2384 static void service_run_next_main(Service *s) {
2389 assert(s->main_command);
2390 assert(s->main_command->command_next);
2391 assert(s->type == SERVICE_ONESHOT);
2393 s->main_command = s->main_command->command_next;
2394 service_unwatch_main_pid(s);
2396 r = service_spawn(s,
2403 s->notify_access != NOTIFY_NONE,
2409 service_set_main_pid(s, pid);
2414 log_warning_unit(UNIT(s)->id,
2415 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2416 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2419 static int service_start_limit_test(Service *s) {
2422 if (ratelimit_test(&s->start_limit))
2425 switch (s->start_limit_action) {
2427 case SERVICE_START_LIMIT_NONE:
2428 log_warning_unit(UNIT(s)->id,
2429 "%s start request repeated too quickly, refusing to start.",
2433 case SERVICE_START_LIMIT_REBOOT: {
2437 dbus_error_init(&error);
2439 log_warning_unit(UNIT(s)->id,
2440 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2442 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
2443 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
2444 true, &error, NULL);
2446 log_error_unit(UNIT(s)->id,
2447 "Failed to reboot: %s.", bus_error(&error, r));
2448 dbus_error_free(&error);
2454 case SERVICE_START_LIMIT_REBOOT_FORCE:
2455 log_warning_unit(UNIT(s)->id,
2456 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2457 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2460 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2461 log_warning_unit(UNIT(s)->id,
2462 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2464 reboot(RB_AUTOBOOT);
2468 log_error_unit(UNIT(s)->id,
2469 "start limit action=%i", s->start_limit_action);
2470 assert_not_reached("Unknown StartLimitAction.");
2476 static int service_start(Unit *u) {
2477 Service *s = SERVICE(u);
2482 /* We cannot fulfill this request right now, try again later
2484 if (s->state == SERVICE_STOP ||
2485 s->state == SERVICE_STOP_SIGTERM ||
2486 s->state == SERVICE_STOP_SIGKILL ||
2487 s->state == SERVICE_STOP_POST ||
2488 s->state == SERVICE_FINAL_SIGTERM ||
2489 s->state == SERVICE_FINAL_SIGKILL)
2492 /* Already on it! */
2493 if (s->state == SERVICE_START_PRE ||
2494 s->state == SERVICE_START ||
2495 s->state == SERVICE_START_POST)
2498 /* A service that will be restarted must be stopped first to
2499 * trigger BindsTo and/or OnFailure dependencies. If a user
2500 * does not want to wait for the holdoff time to elapse, the
2501 * service should be manually restarted, not started. We
2502 * simply return EAGAIN here, so that any start jobs stay
2503 * queued, and assume that the auto restart timer will
2504 * eventually trigger the restart. */
2505 if (s->state == SERVICE_AUTO_RESTART)
2508 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2510 /* Make sure we don't enter a busy loop of some kind. */
2511 r = service_start_limit_test(s);
2513 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2517 s->result = SERVICE_SUCCESS;
2518 s->reload_result = SERVICE_SUCCESS;
2519 s->main_pid_known = false;
2520 s->main_pid_alien = false;
2521 s->forbid_restart = false;
2523 service_enter_start_pre(s);
2527 static int service_stop(Unit *u) {
2528 Service *s = SERVICE(u);
2532 /* Don't create restart jobs from here. */
2533 s->forbid_restart = true;
2536 if (s->state == SERVICE_STOP ||
2537 s->state == SERVICE_STOP_SIGTERM ||
2538 s->state == SERVICE_STOP_SIGKILL ||
2539 s->state == SERVICE_STOP_POST ||
2540 s->state == SERVICE_FINAL_SIGTERM ||
2541 s->state == SERVICE_FINAL_SIGKILL)
2544 /* A restart will be scheduled or is in progress. */
2545 if (s->state == SERVICE_AUTO_RESTART) {
2546 service_set_state(s, SERVICE_DEAD);
2550 /* If there's already something running we go directly into
2552 if (s->state == SERVICE_START_PRE ||
2553 s->state == SERVICE_START ||
2554 s->state == SERVICE_START_POST ||
2555 s->state == SERVICE_RELOAD) {
2556 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2560 assert(s->state == SERVICE_RUNNING ||
2561 s->state == SERVICE_EXITED);
2563 service_enter_stop(s, SERVICE_SUCCESS);
2567 static int service_reload(Unit *u) {
2568 Service *s = SERVICE(u);
2572 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2574 service_enter_reload(s);
2578 static bool service_can_reload(Unit *u) {
2579 Service *s = SERVICE(u);
2583 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2586 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2587 Service *s = SERVICE(u);
2593 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2594 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2595 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2597 if (s->control_pid > 0)
2598 unit_serialize_item_format(u, f, "control-pid", "%lu",
2599 (unsigned long) s->control_pid);
2601 if (s->main_pid_known && s->main_pid > 0)
2602 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2604 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2607 unit_serialize_item(u, f, "status-text", s->status_text);
2609 /* FIXME: There's a minor uncleanliness here: if there are
2610 * multiple commands attached here, we will start from the
2611 * first one again */
2612 if (s->control_command_id >= 0)
2613 unit_serialize_item(u, f, "control-command",
2614 service_exec_command_to_string(s->control_command_id));
2616 if (s->socket_fd >= 0) {
2619 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2622 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2625 if (s->main_exec_status.pid > 0) {
2626 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu",
2627 (unsigned long) s->main_exec_status.pid);
2628 dual_timestamp_serialize(f, "main-exec-status-start",
2629 &s->main_exec_status.start_timestamp);
2630 dual_timestamp_serialize(f, "main-exec-status-exit",
2631 &s->main_exec_status.exit_timestamp);
2633 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2634 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
2635 s->main_exec_status.code);
2636 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
2637 s->main_exec_status.status);
2640 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2641 dual_timestamp_serialize(f, "watchdog-timestamp",
2642 &s->watchdog_timestamp);
2644 if (s->exec_context.tmp_dir)
2645 unit_serialize_item(u, f, "tmp-dir", s->exec_context.tmp_dir);
2647 if (s->exec_context.var_tmp_dir)
2648 unit_serialize_item(u, f, "var-tmp-dir", s->exec_context.var_tmp_dir);
2653 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2654 Service *s = SERVICE(u);
2661 if (streq(key, "state")) {
2664 state = service_state_from_string(value);
2666 log_debug_unit(u->id, "Failed to parse state value %s", value);
2668 s->deserialized_state = state;
2669 } else if (streq(key, "result")) {
2672 f = service_result_from_string(value);
2674 log_debug_unit(u->id, "Failed to parse result value %s", value);
2675 else if (f != SERVICE_SUCCESS)
2678 } else if (streq(key, "reload-result")) {
2681 f = service_result_from_string(value);
2683 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2684 else if (f != SERVICE_SUCCESS)
2685 s->reload_result = f;
2687 } else if (streq(key, "control-pid")) {
2690 if (parse_pid(value, &pid) < 0)
2691 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2693 s->control_pid = pid;
2694 } else if (streq(key, "main-pid")) {
2697 if (parse_pid(value, &pid) < 0)
2698 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2700 service_set_main_pid(s, (pid_t) pid);
2701 } else if (streq(key, "main-pid-known")) {
2704 b = parse_boolean(value);
2706 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2708 s->main_pid_known = b;
2709 } else if (streq(key, "status-text")) {
2716 free(s->status_text);
2720 } else if (streq(key, "control-command")) {
2721 ServiceExecCommand id;
2723 id = service_exec_command_from_string(value);
2725 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2727 s->control_command_id = id;
2728 s->control_command = s->exec_command[id];
2730 } else if (streq(key, "socket-fd")) {
2733 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2734 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2737 if (s->socket_fd >= 0)
2738 close_nointr_nofail(s->socket_fd);
2739 s->socket_fd = fdset_remove(fds, fd);
2741 } else if (streq(key, "main-exec-status-pid")) {
2744 if (parse_pid(value, &pid) < 0)
2745 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2747 s->main_exec_status.pid = pid;
2748 } else if (streq(key, "main-exec-status-code")) {
2751 if (safe_atoi(value, &i) < 0)
2752 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2754 s->main_exec_status.code = i;
2755 } else if (streq(key, "main-exec-status-status")) {
2758 if (safe_atoi(value, &i) < 0)
2759 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2761 s->main_exec_status.status = i;
2762 } else if (streq(key, "main-exec-status-start"))
2763 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2764 else if (streq(key, "main-exec-status-exit"))
2765 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2766 else if (streq(key, "watchdog-timestamp"))
2767 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2768 else if (streq(key, "tmp-dir")) {
2775 s->exec_context.tmp_dir = t;
2776 } else if (streq(key, "var-tmp-dir")) {
2783 s->exec_context.var_tmp_dir = t;
2785 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2790 static UnitActiveState service_active_state(Unit *u) {
2791 const UnitActiveState *table;
2795 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2797 return table[SERVICE(u)->state];
2800 static const char *service_sub_state_to_string(Unit *u) {
2803 return service_state_to_string(SERVICE(u)->state);
2806 static bool service_check_gc(Unit *u) {
2807 Service *s = SERVICE(u);
2811 /* Never clean up services that still have a process around,
2812 * even if the service is formally dead. */
2813 if (cgroup_good(s) > 0 ||
2814 main_pid_good(s) > 0 ||
2815 control_pid_good(s) > 0)
2818 #ifdef HAVE_SYSV_COMPAT
2826 static bool service_check_snapshot(Unit *u) {
2827 Service *s = SERVICE(u);
2831 return !s->got_socket_fd;
2834 static int service_retry_pid_file(Service *s) {
2837 assert(s->pid_file);
2838 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2840 r = service_load_pid_file(s, false);
2844 service_unwatch_pid_file(s);
2846 service_enter_running(s, SERVICE_SUCCESS);
2850 static int service_watch_pid_file(Service *s) {
2853 log_debug_unit(UNIT(s)->id,
2854 "Setting watch for %s's PID file %s",
2855 UNIT(s)->id, s->pid_file_pathspec->path);
2856 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2860 /* the pidfile might have appeared just before we set the watch */
2861 log_debug_unit(UNIT(s)->id,
2862 "Trying to read %s's PID file %s in case it changed",
2863 UNIT(s)->id, s->pid_file_pathspec->path);
2864 service_retry_pid_file(s);
2868 log_error_unit(UNIT(s)->id,
2869 "Failed to set a watch for %s's PID file %s: %s",
2870 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2871 service_unwatch_pid_file(s);
2875 static int service_demand_pid_file(Service *s) {
2878 assert(s->pid_file);
2879 assert(!s->pid_file_pathspec);
2881 ps = new0(PathSpec, 1);
2885 ps->path = strdup(s->pid_file);
2891 path_kill_slashes(ps->path);
2893 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2894 * keep their PID file open all the time. */
2895 ps->type = PATH_MODIFIED;
2896 ps->inotify_fd = -1;
2898 s->pid_file_pathspec = ps;
2900 return service_watch_pid_file(s);
2903 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2904 Service *s = SERVICE(u);
2908 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2909 assert(s->pid_file_pathspec);
2910 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2912 log_debug_unit(u->id, "inotify event for %s", u->id);
2914 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2917 if (service_retry_pid_file(s) == 0)
2920 if (service_watch_pid_file(s) < 0)
2925 service_unwatch_pid_file(s);
2926 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2929 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2930 Service *s = SERVICE(u);
2936 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2937 is_clean_exit_lsb(code, status, &s->success_status))
2938 f = SERVICE_SUCCESS;
2939 else if (code == CLD_EXITED)
2940 f = SERVICE_FAILURE_EXIT_CODE;
2941 else if (code == CLD_KILLED)
2942 f = SERVICE_FAILURE_SIGNAL;
2943 else if (code == CLD_DUMPED)
2944 f = SERVICE_FAILURE_CORE_DUMP;
2946 assert_not_reached("Unknown code");
2948 if (s->main_pid == pid) {
2949 /* Forking services may occasionally move to a new PID.
2950 * As long as they update the PID file before exiting the old
2951 * PID, they're fine. */
2952 if (service_load_pid_file(s, false) == 0)
2956 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2958 if (s->main_command) {
2959 /* If this is not a forking service than the
2960 * main process got started and hence we copy
2961 * the exit status so that it is recorded both
2962 * as main and as control process exit
2965 s->main_command->exec_status = s->main_exec_status;
2967 if (s->main_command->ignore)
2968 f = SERVICE_SUCCESS;
2969 } else if (s->exec_command[SERVICE_EXEC_START]) {
2971 /* If this is a forked process, then we should
2972 * ignore the return value if this was
2973 * configured for the starter process */
2975 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2976 f = SERVICE_SUCCESS;
2979 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2981 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2982 u->id, sigchld_code_to_string(code), status,
2983 strna(code == CLD_EXITED
2984 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2985 : signal_to_string(status)),
2986 "EXIT_CODE=%s", sigchld_code_to_string(code),
2987 "EXIT_STATUS=%i", status,
2990 if (f != SERVICE_SUCCESS)
2993 if (s->main_command &&
2994 s->main_command->command_next &&
2995 f == SERVICE_SUCCESS) {
2997 /* There is another command to *
2998 * execute, so let's do that. */
3000 log_debug_unit(u->id,
3001 "%s running next main command for state %s",
3002 u->id, service_state_to_string(s->state));
3003 service_run_next_main(s);
3007 /* The service exited, so the service is officially
3009 s->main_command = NULL;
3013 case SERVICE_START_POST:
3014 case SERVICE_RELOAD:
3016 /* Need to wait until the operation is
3021 if (s->type == SERVICE_ONESHOT) {
3022 /* This was our main goal, so let's go on */
3023 if (f == SERVICE_SUCCESS)
3024 service_enter_start_post(s);
3026 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3032 case SERVICE_RUNNING:
3033 service_enter_running(s, f);
3036 case SERVICE_STOP_SIGTERM:
3037 case SERVICE_STOP_SIGKILL:
3039 if (!control_pid_good(s))
3040 service_enter_stop_post(s, f);
3042 /* If there is still a control process, wait for that first */
3046 assert_not_reached("Uh, main process died at wrong time.");
3050 } else if (s->control_pid == pid) {
3054 if (s->control_command) {
3055 exec_status_exit(&s->control_command->exec_status,
3056 &s->exec_context, pid, code, status);
3058 if (s->control_command->ignore)
3059 f = SERVICE_SUCCESS;
3062 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
3063 "%s: control process exited, code=%s status=%i",
3064 u->id, sigchld_code_to_string(code), status);
3066 if (f != SERVICE_SUCCESS)
3069 /* Immediately get rid of the cgroup, so that the
3070 * kernel doesn't delay the cgroup empty messages for
3071 * the service cgroup any longer than necessary */
3072 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL,
3073 true, true, NULL, "control");
3075 if (s->control_command &&
3076 s->control_command->command_next &&
3077 f == SERVICE_SUCCESS) {
3079 /* There is another command to *
3080 * execute, so let's do that. */
3082 log_debug_unit(u->id,
3083 "%s running next control command for state %s",
3084 u->id, service_state_to_string(s->state));
3085 service_run_next_control(s);
3088 /* No further commands for this step, so let's
3089 * figure out what to do next */
3091 s->control_command = NULL;
3092 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3094 log_debug_unit(u->id,
3095 "%s got final SIGCHLD for state %s",
3096 u->id, service_state_to_string(s->state));
3100 case SERVICE_START_PRE:
3101 if (f == SERVICE_SUCCESS)
3102 service_enter_start(s);
3104 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3108 if (s->type != SERVICE_FORKING)
3109 /* Maybe spurious event due to a reload that changed the type? */
3112 if (f != SERVICE_SUCCESS) {
3113 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3118 bool has_start_post;
3121 /* Let's try to load the pid file here if we can.
3122 * The PID file might actually be created by a START_POST
3123 * script. In that case don't worry if the loading fails. */
3125 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3126 r = service_load_pid_file(s, !has_start_post);
3127 if (!has_start_post && r < 0) {
3128 r = service_demand_pid_file(s);
3129 if (r < 0 || !cgroup_good(s))
3130 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3134 service_search_main_pid(s);
3136 service_enter_start_post(s);
3139 case SERVICE_START_POST:
3140 if (f != SERVICE_SUCCESS) {
3141 service_enter_stop(s, f);
3148 r = service_load_pid_file(s, true);
3150 r = service_demand_pid_file(s);
3151 if (r < 0 || !cgroup_good(s))
3152 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3156 service_search_main_pid(s);
3158 service_enter_running(s, SERVICE_SUCCESS);
3161 case SERVICE_RELOAD:
3162 if (f == SERVICE_SUCCESS) {
3163 service_load_pid_file(s, true);
3164 service_search_main_pid(s);
3167 s->reload_result = f;
3168 service_enter_running(s, SERVICE_SUCCESS);
3172 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3175 case SERVICE_STOP_SIGTERM:
3176 case SERVICE_STOP_SIGKILL:
3177 if (main_pid_good(s) <= 0)
3178 service_enter_stop_post(s, f);
3180 /* If there is still a service
3181 * process around, wait until
3182 * that one quit, too */
3185 case SERVICE_STOP_POST:
3186 case SERVICE_FINAL_SIGTERM:
3187 case SERVICE_FINAL_SIGKILL:
3188 service_enter_dead(s, f, true);
3192 assert_not_reached("Uh, control process died at wrong time.");
3197 /* Notify clients about changed exit status */
3198 unit_add_to_dbus_queue(u);
3201 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3202 Service *s = SERVICE(u);
3205 assert(elapsed == 1);
3207 if (w == &s->watchdog_watch) {
3208 service_handle_watchdog(s);
3212 assert(w == &s->timer_watch);
3216 case SERVICE_START_PRE:
3218 log_warning_unit(u->id,
3219 "%s operation timed out. Terminating.", u->id);
3220 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3223 case SERVICE_START_POST:
3224 log_warning_unit(u->id,
3225 "%s operation timed out. Stopping.", u->id);
3226 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3229 case SERVICE_RELOAD:
3230 log_warning_unit(u->id,
3231 "%s operation timed out. Stopping.", u->id);
3232 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3233 service_enter_running(s, SERVICE_SUCCESS);
3237 log_warning_unit(u->id,
3238 "%s stopping timed out. Terminating.", u->id);
3239 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3242 case SERVICE_STOP_SIGTERM:
3243 if (s->kill_context.send_sigkill) {
3244 log_warning_unit(u->id,
3245 "%s stopping timed out. Killing.", u->id);
3246 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3248 log_warning_unit(u->id,
3249 "%s stopping timed out. Skipping SIGKILL.", u->id);
3250 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3255 case SERVICE_STOP_SIGKILL:
3256 /* Uh, we sent a SIGKILL and it is still not gone?
3257 * Must be something we cannot kill, so let's just be
3258 * weirded out and continue */
3260 log_warning_unit(u->id,
3261 "%s still around after SIGKILL. Ignoring.", u->id);
3262 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3265 case SERVICE_STOP_POST:
3266 log_warning_unit(u->id,
3267 "%s stopping timed out (2). Terminating.", u->id);
3268 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3271 case SERVICE_FINAL_SIGTERM:
3272 if (s->kill_context.send_sigkill) {
3273 log_warning_unit(u->id,
3274 "%s stopping timed out (2). Killing.", u->id);
3275 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3277 log_warning_unit(u->id,
3278 "%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.",
3280 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3285 case SERVICE_FINAL_SIGKILL:
3286 log_warning_unit(u->id,
3287 "%s still around after SIGKILL (2). Entering failed mode.", u->id);
3288 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3291 case SERVICE_AUTO_RESTART:
3292 log_info_unit(u->id,
3293 "%s holdoff time over, scheduling restart.", u->id);
3294 service_enter_restart(s);
3298 assert_not_reached("Timeout at wrong time.");
3302 static void service_cgroup_notify_event(Unit *u) {
3303 Service *s = SERVICE(u);
3307 log_debug_unit(u->id,
3308 "%s: cgroup is empty", u->id);
3312 /* Waiting for SIGCHLD is usually more interesting,
3313 * because it includes return codes/signals. Which is
3314 * why we ignore the cgroup events for most cases,
3315 * except when we don't know pid which to expect the
3319 case SERVICE_START_POST:
3320 /* If we were hoping for the daemon to write its PID file,
3321 * we can give up now. */
3322 if (s->pid_file_pathspec) {
3323 log_warning_unit(u->id,
3324 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3325 service_unwatch_pid_file(s);
3326 if (s->state == SERVICE_START)
3327 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3329 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3333 case SERVICE_RUNNING:
3334 /* service_enter_running() will figure out what to do */
3335 service_enter_running(s, SERVICE_SUCCESS);
3338 case SERVICE_STOP_SIGTERM:
3339 case SERVICE_STOP_SIGKILL:
3341 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3342 service_enter_stop_post(s, SERVICE_SUCCESS);
3346 case SERVICE_FINAL_SIGTERM:
3347 case SERVICE_FINAL_SIGKILL:
3348 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3349 service_enter_dead(s, SERVICE_SUCCESS, true);
3358 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3359 Service *s = SERVICE(u);
3364 if (s->notify_access == NOTIFY_NONE) {
3365 log_warning_unit(u->id,
3366 "%s: Got notification message from PID %lu, but reception is disabled.",
3367 u->id, (unsigned long) pid);
3371 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3372 log_warning_unit(u->id,
3373 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3374 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3378 log_debug_unit(u->id,
3379 "%s: Got message", u->id);
3381 /* Interpret MAINPID= */
3382 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3383 (s->state == SERVICE_START ||
3384 s->state == SERVICE_START_POST ||
3385 s->state == SERVICE_RUNNING ||
3386 s->state == SERVICE_RELOAD)) {
3388 if (parse_pid(e + 8, &pid) < 0)
3389 log_warning_unit(u->id,
3390 "Failed to parse notification message %s", e);
3392 log_debug_unit(u->id,
3393 "%s: got %s", u->id, e);
3394 service_set_main_pid(s, pid);
3398 /* Interpret READY= */
3399 if (s->type == SERVICE_NOTIFY &&
3400 s->state == SERVICE_START &&
3401 strv_find(tags, "READY=1")) {
3402 log_debug_unit(u->id,
3403 "%s: got READY=1", u->id);
3405 service_enter_start_post(s);
3408 /* Interpret STATUS= */
3409 e = strv_find_prefix(tags, "STATUS=");
3415 if (!utf8_is_valid(e+7)) {
3416 log_warning_unit(u->id,
3417 "Status message in notification is not UTF-8 clean.");
3423 log_error_unit(u->id,
3424 "Failed to allocate string.");
3428 log_debug_unit(u->id,
3429 "%s: got %s", u->id, e);
3431 free(s->status_text);
3434 free(s->status_text);
3435 s->status_text = NULL;
3439 if (strv_find(tags, "WATCHDOG=1")) {
3440 log_debug_unit(u->id,
3441 "%s: got WATCHDOG=1", u->id);
3442 if (dual_timestamp_is_set(&s->watchdog_timestamp))
3443 service_reset_watchdog(s);
3446 /* Notify clients about changed status or main pid */
3447 unit_add_to_dbus_queue(u);
3450 #ifdef HAVE_SYSV_COMPAT
3452 static int service_enumerate(Manager *m) {
3455 DIR _cleanup_closedir_ *d = NULL;
3456 char _cleanup_free_ *path = NULL, *fpath = NULL, *name = NULL;
3457 Set *runlevel_services[ELEMENTSOF(rcnd_table)];
3458 Set _cleanup_set_free_ *shutdown_services = NULL;
3465 if (m->running_as != SYSTEMD_SYSTEM)
3468 zero(runlevel_services);
3470 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3471 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3475 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3486 if (errno != ENOENT)
3487 log_warning("opendir(%s) failed: %s", path, strerror(errno));
3492 while ((de = readdir(d))) {
3495 if (ignore_file(de->d_name))
3498 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3501 if (strlen(de->d_name) < 4)
3504 a = undecchar(de->d_name[1]);
3505 b = undecchar(de->d_name[2]);
3511 fpath = strjoin(path, "/", de->d_name, NULL);
3517 if (access(fpath, X_OK) < 0) {
3519 if (errno != ENOENT)
3520 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3526 name = sysv_translate_name(de->d_name + 3);
3532 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3534 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3538 if (de->d_name[0] == 'S') {
3540 if (rcnd_table[i].type == RUNLEVEL_UP) {
3541 SERVICE(service)->sysv_start_priority_from_rcnd =
3542 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3544 SERVICE(service)->sysv_enabled = true;
3547 r = set_ensure_allocated(&runlevel_services[i],
3548 trivial_hash_func, trivial_compare_func);
3552 r = set_put(runlevel_services[i], service);
3556 } else if (de->d_name[0] == 'K' &&
3557 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3559 r = set_ensure_allocated(&shutdown_services,
3560 trivial_hash_func, trivial_compare_func);
3564 r = set_put(shutdown_services, service);
3571 /* Now we loaded all stubs and are aware of the lowest
3572 start-up priority for all services, not let's actually load
3573 the services, this will also tell us which services are
3574 actually native now */
3575 manager_dispatch_load_queue(m);
3577 /* If this is a native service, rely on native ways to pull in
3578 * a service, don't pull it in via sysv rcN.d links. */
3579 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3580 SET_FOREACH(service, runlevel_services[i], j) {
3581 service = unit_follow_merge(service);
3583 if (service->fragment_path)
3586 r = unit_add_two_dependencies_by_name_inverse(
3587 service, UNIT_AFTER, UNIT_WANTS,
3588 rcnd_table[i].target, NULL, true);
3593 /* We honour K links only for halt/reboot. For the normal
3594 * runlevels we assume the stop jobs will be implicitly added
3595 * by the core logic. Also, we don't really distinguish here
3596 * between the runlevels 0 and 6 and just add them to the
3597 * special shutdown target. */
3598 SET_FOREACH(service, shutdown_services, j) {
3599 service = unit_follow_merge(service);
3601 if (service->fragment_path)
3604 r = unit_add_two_dependencies_by_name(
3605 service, UNIT_BEFORE, UNIT_CONFLICTS,
3606 SPECIAL_SHUTDOWN_TARGET, NULL, true);
3615 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3616 set_free(runlevel_services[i]);
3622 static void service_bus_name_owner_change(
3625 const char *old_owner,
3626 const char *new_owner) {
3628 Service *s = SERVICE(u);
3633 assert(streq(s->bus_name, name));
3634 assert(old_owner || new_owner);
3636 if (old_owner && new_owner)
3637 log_debug_unit(u->id,
3638 "%s's D-Bus name %s changed owner from %s to %s",
3639 u->id, name, old_owner, new_owner);
3641 log_debug_unit(u->id,
3642 "%s's D-Bus name %s no longer registered by %s",
3643 u->id, name, old_owner);
3645 log_debug_unit(u->id,
3646 "%s's D-Bus name %s now registered by %s",
3647 u->id, name, new_owner);
3649 s->bus_name_good = !!new_owner;
3651 if (s->type == SERVICE_DBUS) {
3653 /* service_enter_running() will figure out what to
3655 if (s->state == SERVICE_RUNNING)
3656 service_enter_running(s, SERVICE_SUCCESS);
3657 else if (s->state == SERVICE_START && new_owner)
3658 service_enter_start_post(s);
3660 } else if (new_owner &&
3662 (s->state == SERVICE_START ||
3663 s->state == SERVICE_START_POST ||
3664 s->state == SERVICE_RUNNING ||
3665 s->state == SERVICE_RELOAD)) {
3667 /* Try to acquire PID from bus service */
3668 log_debug_unit(u->id,
3669 "Trying to acquire PID from D-Bus name...");
3671 bus_query_pid(u->manager, name);
3675 static void service_bus_query_pid_done(
3680 Service *s = SERVICE(u);
3685 log_debug_unit(u->id,
3686 "%s's D-Bus name %s is now owned by process %u",
3687 u->id, name, (unsigned) pid);
3689 if (s->main_pid <= 0 &&
3690 (s->state == SERVICE_START ||
3691 s->state == SERVICE_START_POST ||
3692 s->state == SERVICE_RUNNING ||
3693 s->state == SERVICE_RELOAD))
3694 service_set_main_pid(s, pid);
3697 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3702 /* This is called by the socket code when instantiating a new
3703 * service for a stream socket and the socket needs to be
3706 if (UNIT(s)->load_state != UNIT_LOADED)
3709 if (s->socket_fd >= 0)
3712 if (s->state != SERVICE_DEAD)
3716 s->got_socket_fd = true;
3718 unit_ref_set(&s->accept_socket, UNIT(sock));
3720 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3723 static void service_reset_failed(Unit *u) {
3724 Service *s = SERVICE(u);
3728 if (s->state == SERVICE_FAILED)
3729 service_set_state(s, SERVICE_DEAD);
3731 s->result = SERVICE_SUCCESS;
3732 s->reload_result = SERVICE_SUCCESS;
3734 RATELIMIT_RESET(s->start_limit);
3737 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3738 Service *s = SERVICE(u);
3739 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
3742 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3743 [SERVICE_DEAD] = "dead",
3744 [SERVICE_START_PRE] = "start-pre",
3745 [SERVICE_START] = "start",
3746 [SERVICE_START_POST] = "start-post",
3747 [SERVICE_RUNNING] = "running",
3748 [SERVICE_EXITED] = "exited",
3749 [SERVICE_RELOAD] = "reload",
3750 [SERVICE_STOP] = "stop",
3751 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3752 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3753 [SERVICE_STOP_POST] = "stop-post",
3754 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3755 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3756 [SERVICE_FAILED] = "failed",
3757 [SERVICE_AUTO_RESTART] = "auto-restart",
3760 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3762 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3763 [SERVICE_RESTART_NO] = "no",
3764 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3765 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3766 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3767 [SERVICE_RESTART_ALWAYS] = "always"
3770 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3772 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3773 [SERVICE_SIMPLE] = "simple",
3774 [SERVICE_FORKING] = "forking",
3775 [SERVICE_ONESHOT] = "oneshot",
3776 [SERVICE_DBUS] = "dbus",
3777 [SERVICE_NOTIFY] = "notify",
3778 [SERVICE_IDLE] = "idle"
3781 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3783 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3784 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3785 [SERVICE_EXEC_START] = "ExecStart",
3786 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3787 [SERVICE_EXEC_RELOAD] = "ExecReload",
3788 [SERVICE_EXEC_STOP] = "ExecStop",
3789 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3792 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3794 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3795 [NOTIFY_NONE] = "none",
3796 [NOTIFY_MAIN] = "main",
3797 [NOTIFY_ALL] = "all"
3800 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3802 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3803 [SERVICE_SUCCESS] = "success",
3804 [SERVICE_FAILURE_RESOURCES] = "resources",
3805 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3806 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3807 [SERVICE_FAILURE_SIGNAL] = "signal",
3808 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3809 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3810 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3813 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3815 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3816 [SERVICE_START_LIMIT_NONE] = "none",
3817 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3818 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3819 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3821 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3823 const UnitVTable service_vtable = {
3824 .object_size = sizeof(Service),
3831 .exec_context_offset = offsetof(Service, exec_context),
3832 .exec_section = "Service",
3834 .init = service_init,
3835 .done = service_done,
3836 .load = service_load,
3838 .coldplug = service_coldplug,
3840 .dump = service_dump,
3842 .start = service_start,
3843 .stop = service_stop,
3844 .reload = service_reload,
3846 .can_reload = service_can_reload,
3848 .kill = service_kill,
3850 .serialize = service_serialize,
3851 .deserialize_item = service_deserialize_item,
3853 .active_state = service_active_state,
3854 .sub_state_to_string = service_sub_state_to_string,
3856 .check_gc = service_check_gc,
3857 .check_snapshot = service_check_snapshot,
3859 .sigchld_event = service_sigchld_event,
3860 .timer_event = service_timer_event,
3861 .fd_event = service_fd_event,
3863 .reset_failed = service_reset_failed,
3865 .cgroup_notify_empty = service_cgroup_notify_event,
3866 .notify_message = service_notify_message,
3868 .bus_name_owner_change = service_bus_name_owner_change,
3869 .bus_query_pid_done = service_bus_query_pid_done,
3871 .bus_interface = "org.freedesktop.systemd1.Service",
3872 .bus_message_handler = bus_service_message_handler,
3873 .bus_invalidating_properties = bus_service_invalidating_properties,
3875 #ifdef HAVE_SYSV_COMPAT
3876 .enumerate = service_enumerate,
3878 .status_message_formats = {
3879 .starting_stopping = {
3880 [0] = "Starting %s...",
3881 [1] = "Stopping %s...",
3883 .finished_start_job = {
3884 [JOB_DONE] = "Started %s.",
3885 [JOB_FAILED] = "Failed to start %s.",
3886 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3887 [JOB_TIMEOUT] = "Timed out starting %s.",
3889 .finished_stop_job = {
3890 [JOB_DONE] = "Stopped %s.",
3891 [JOB_FAILED] = "Stopped (with error) %s.",
3892 [JOB_TIMEOUT] = "Timed out stopping %s.",