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);
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 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 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1110 static int service_verify(Service *s) {
1113 if (UNIT(s)->load_state != UNIT_LOADED)
1116 if (!s->exec_command[SERVICE_EXEC_START]) {
1117 log_error_unit(UNIT(s)->id,
1118 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1122 if (s->type != SERVICE_ONESHOT &&
1123 s->exec_command[SERVICE_EXEC_START]->command_next) {
1124 log_error_unit(UNIT(s)->id,
1125 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1129 if (s->type == SERVICE_DBUS && !s->bus_name) {
1130 log_error_unit(UNIT(s)->id,
1131 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1135 if (s->bus_name && s->type != SERVICE_DBUS)
1136 log_warning_unit(UNIT(s)->id,
1137 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1139 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1140 log_error_unit(UNIT(s)->id,
1141 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1148 static int service_add_default_dependencies(Service *s) {
1153 /* Add a number of automatic dependencies useful for the
1154 * majority of services. */
1156 /* First, pull in base system */
1157 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1159 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1162 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1164 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1168 /* Second, activate normal shutdown */
1169 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1172 static void service_fix_output(Service *s) {
1175 /* If nothing has been explicitly configured, patch default
1176 * output in. If input is socket/tty we avoid this however,
1177 * since in that case we want output to default to the same
1178 * place as we read input from. */
1180 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1181 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1182 s->exec_context.std_input == EXEC_INPUT_NULL)
1183 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1185 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1186 s->exec_context.std_input == EXEC_INPUT_NULL)
1187 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1190 static int service_load(Unit *u) {
1192 Service *s = SERVICE(u);
1196 /* Load a .service file */
1197 if ((r = unit_load_fragment(u)) < 0)
1200 #ifdef HAVE_SYSV_COMPAT
1201 /* Load a classic init script as a fallback, if we couldn't find anything */
1202 if (u->load_state == UNIT_STUB)
1203 if ((r = service_load_sysv(s)) < 0)
1207 /* Still nothing found? Then let's give up */
1208 if (u->load_state == UNIT_STUB)
1211 /* We were able to load something, then let's add in the
1212 * dropin directories. */
1213 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1216 /* This is a new unit? Then let's add in some extras */
1217 if (u->load_state == UNIT_LOADED) {
1218 if (s->type == _SERVICE_TYPE_INVALID)
1219 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1221 /* Oneshot services have disabled start timeout by default */
1222 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1223 s->timeout_start_usec = 0;
1225 service_fix_output(s);
1227 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1230 if ((r = unit_add_default_cgroups(u)) < 0)
1233 #ifdef HAVE_SYSV_COMPAT
1234 if ((r = sysv_fix_order(s)) < 0)
1238 if ((r = fsck_fix_order(s)) < 0)
1242 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1245 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1246 s->notify_access = NOTIFY_MAIN;
1248 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1249 s->notify_access = NOTIFY_MAIN;
1251 if (s->type == SERVICE_DBUS || s->bus_name)
1252 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1255 if (UNIT(s)->default_dependencies)
1256 if ((r = service_add_default_dependencies(s)) < 0)
1259 r = unit_exec_context_defaults(u, &s->exec_context);
1264 return service_verify(s);
1267 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1269 ServiceExecCommand c;
1270 Service *s = SERVICE(u);
1271 const char *prefix2;
1276 p2 = strappend(prefix, "\t");
1277 prefix2 = p2 ? p2 : prefix;
1280 "%sService State: %s\n"
1282 "%sReload Result: %s\n"
1283 "%sPermissionsStartOnly: %s\n"
1284 "%sRootDirectoryStartOnly: %s\n"
1285 "%sRemainAfterExit: %s\n"
1286 "%sGuessMainPID: %s\n"
1289 "%sNotifyAccess: %s\n",
1290 prefix, service_state_to_string(s->state),
1291 prefix, service_result_to_string(s->result),
1292 prefix, service_result_to_string(s->reload_result),
1293 prefix, yes_no(s->permissions_start_only),
1294 prefix, yes_no(s->root_directory_start_only),
1295 prefix, yes_no(s->remain_after_exit),
1296 prefix, yes_no(s->guess_main_pid),
1297 prefix, service_type_to_string(s->type),
1298 prefix, service_restart_to_string(s->restart),
1299 prefix, notify_access_to_string(s->notify_access));
1301 if (s->control_pid > 0)
1303 "%sControl PID: %lu\n",
1304 prefix, (unsigned long) s->control_pid);
1306 if (s->main_pid > 0)
1309 "%sMain PID Known: %s\n"
1310 "%sMain PID Alien: %s\n",
1311 prefix, (unsigned long) s->main_pid,
1312 prefix, yes_no(s->main_pid_known),
1313 prefix, yes_no(s->main_pid_alien));
1318 prefix, s->pid_file);
1323 "%sBus Name Good: %s\n",
1324 prefix, s->bus_name,
1325 prefix, yes_no(s->bus_name_good));
1327 kill_context_dump(&s->kill_context, f, prefix);
1328 exec_context_dump(&s->exec_context, f, prefix);
1330 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1332 if (!s->exec_command[c])
1335 fprintf(f, "%s-> %s:\n",
1336 prefix, service_exec_command_to_string(c));
1338 exec_command_dump_list(s->exec_command[c], f, prefix2);
1341 #ifdef HAVE_SYSV_COMPAT
1344 "%sSysV Init Script has LSB Header: %s\n"
1345 "%sSysVEnabled: %s\n",
1346 prefix, yes_no(s->sysv_has_lsb),
1347 prefix, yes_no(s->sysv_enabled));
1349 if (s->sysv_start_priority >= 0)
1351 "%sSysVStartPriority: %i\n",
1352 prefix, s->sysv_start_priority);
1354 if (s->sysv_runlevels)
1355 fprintf(f, "%sSysVRunLevels: %s\n",
1356 prefix, s->sysv_runlevels);
1359 if (s->fsck_passno > 0)
1361 "%sFsckPassNo: %i\n",
1362 prefix, s->fsck_passno);
1365 fprintf(f, "%sStatus Text: %s\n",
1366 prefix, s->status_text);
1371 static int service_load_pid_file(Service *s, bool may_warn) {
1381 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1383 log_info_unit(UNIT(s)->id,
1384 "PID file %s not readable (yet?) after %s.",
1385 s->pid_file, service_state_to_string(s->state));
1389 r = parse_pid(k, &pid);
1395 if (kill(pid, 0) < 0 && errno != EPERM) {
1397 log_info_unit(UNIT(s)->id,
1398 "PID %lu read from file %s does not exist.",
1399 (unsigned long) pid, s->pid_file);
1403 if (s->main_pid_known) {
1404 if (pid == s->main_pid)
1407 log_debug_unit(UNIT(s)->id,
1408 "Main PID changing: %lu -> %lu",
1409 (unsigned long) s->main_pid, (unsigned long) pid);
1410 service_unwatch_main_pid(s);
1411 s->main_pid_known = false;
1413 log_debug_unit(UNIT(s)->id,
1414 "Main PID loaded: %lu", (unsigned long) pid);
1416 if ((r = service_set_main_pid(s, pid)) < 0)
1419 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1420 /* FIXME: we need to do something here */
1426 static int service_search_main_pid(Service *s) {
1432 /* If we know it anyway, don't ever fallback to unreliable
1434 if (s->main_pid_known)
1437 if (!s->guess_main_pid)
1440 assert(s->main_pid <= 0);
1442 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1445 log_debug_unit(UNIT(s)->id,
1446 "Main PID guessed: %lu", (unsigned long) pid);
1447 if ((r = service_set_main_pid(s, pid)) < 0)
1450 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1451 /* FIXME: we need to do something here */
1457 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1463 /* Notifies all our sockets when we die */
1465 if (s->socket_fd >= 0)
1468 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1469 if (u->type == UNIT_SOCKET)
1470 socket_notify_service_dead(SOCKET(u), failed_permanent);
1475 static void service_set_state(Service *s, ServiceState state) {
1476 ServiceState old_state;
1477 const UnitActiveState *table;
1480 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1482 old_state = s->state;
1485 service_unwatch_pid_file(s);
1487 if (state != SERVICE_START_PRE &&
1488 state != SERVICE_START &&
1489 state != SERVICE_START_POST &&
1490 state != SERVICE_RELOAD &&
1491 state != SERVICE_STOP &&
1492 state != SERVICE_STOP_SIGTERM &&
1493 state != SERVICE_STOP_SIGKILL &&
1494 state != SERVICE_STOP_POST &&
1495 state != SERVICE_FINAL_SIGTERM &&
1496 state != SERVICE_FINAL_SIGKILL &&
1497 state != SERVICE_AUTO_RESTART)
1498 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1500 if (state != SERVICE_START &&
1501 state != SERVICE_START_POST &&
1502 state != SERVICE_RUNNING &&
1503 state != SERVICE_RELOAD &&
1504 state != SERVICE_STOP &&
1505 state != SERVICE_STOP_SIGTERM &&
1506 state != SERVICE_STOP_SIGKILL) {
1507 service_unwatch_main_pid(s);
1508 s->main_command = NULL;
1511 if (state != SERVICE_START_PRE &&
1512 state != SERVICE_START &&
1513 state != SERVICE_START_POST &&
1514 state != SERVICE_RELOAD &&
1515 state != SERVICE_STOP &&
1516 state != SERVICE_STOP_SIGTERM &&
1517 state != SERVICE_STOP_SIGKILL &&
1518 state != SERVICE_STOP_POST &&
1519 state != SERVICE_FINAL_SIGTERM &&
1520 state != SERVICE_FINAL_SIGKILL) {
1521 service_unwatch_control_pid(s);
1522 s->control_command = NULL;
1523 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1526 if (state == SERVICE_FAILED)
1527 service_notify_sockets_dead(s, s->result == SERVICE_FAILURE_START_LIMIT);
1529 if (state == SERVICE_DEAD ||
1530 state == SERVICE_STOP ||
1531 state == SERVICE_STOP_SIGTERM ||
1532 state == SERVICE_STOP_SIGKILL ||
1533 state == SERVICE_STOP_POST ||
1534 state == SERVICE_FINAL_SIGTERM ||
1535 state == SERVICE_FINAL_SIGKILL ||
1536 state == SERVICE_AUTO_RESTART)
1537 service_notify_sockets_dead(s, false);
1539 if (state != SERVICE_START_PRE &&
1540 state != SERVICE_START &&
1541 state != SERVICE_START_POST &&
1542 state != SERVICE_RUNNING &&
1543 state != SERVICE_RELOAD &&
1544 state != SERVICE_STOP &&
1545 state != SERVICE_STOP_SIGTERM &&
1546 state != SERVICE_STOP_SIGKILL &&
1547 state != SERVICE_STOP_POST &&
1548 state != SERVICE_FINAL_SIGTERM &&
1549 state != SERVICE_FINAL_SIGKILL &&
1550 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1551 service_close_socket_fd(s);
1552 service_connection_unref(s);
1555 if (state == SERVICE_STOP || state == SERVICE_STOP_SIGTERM)
1556 service_stop_watchdog(s);
1558 /* For the inactive states unit_notify() will trim the cgroup,
1559 * but for exit we have to do that ourselves... */
1560 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1561 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1563 if (old_state != state)
1564 log_debug_unit(UNIT(s)->id,
1565 "%s changed %s -> %s", UNIT(s)->id,
1566 service_state_to_string(old_state),
1567 service_state_to_string(state));
1569 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1570 s->reload_result = SERVICE_SUCCESS;
1573 static int service_coldplug(Unit *u) {
1574 Service *s = SERVICE(u);
1578 assert(s->state == SERVICE_DEAD);
1580 if (s->deserialized_state != s->state) {
1582 if (s->deserialized_state == SERVICE_START_PRE ||
1583 s->deserialized_state == SERVICE_START ||
1584 s->deserialized_state == SERVICE_START_POST ||
1585 s->deserialized_state == SERVICE_RELOAD ||
1586 s->deserialized_state == SERVICE_STOP ||
1587 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1588 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1589 s->deserialized_state == SERVICE_STOP_POST ||
1590 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1591 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1592 s->deserialized_state == SERVICE_AUTO_RESTART) {
1593 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1596 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1598 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
1604 if ((s->deserialized_state == SERVICE_START &&
1605 (s->type == SERVICE_FORKING ||
1606 s->type == SERVICE_DBUS ||
1607 s->type == SERVICE_ONESHOT ||
1608 s->type == SERVICE_NOTIFY)) ||
1609 s->deserialized_state == SERVICE_START_POST ||
1610 s->deserialized_state == SERVICE_RUNNING ||
1611 s->deserialized_state == SERVICE_RELOAD ||
1612 s->deserialized_state == SERVICE_STOP ||
1613 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1614 s->deserialized_state == SERVICE_STOP_SIGKILL)
1615 if (s->main_pid > 0)
1616 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1619 if (s->deserialized_state == SERVICE_START_PRE ||
1620 s->deserialized_state == SERVICE_START ||
1621 s->deserialized_state == SERVICE_START_POST ||
1622 s->deserialized_state == SERVICE_RELOAD ||
1623 s->deserialized_state == SERVICE_STOP ||
1624 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1625 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1626 s->deserialized_state == SERVICE_STOP_POST ||
1627 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1628 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1629 if (s->control_pid > 0)
1630 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1633 if (s->deserialized_state == SERVICE_START_POST ||
1634 s->deserialized_state == SERVICE_RUNNING)
1635 service_handle_watchdog(s);
1637 service_set_state(s, s->deserialized_state);
1642 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1646 unsigned rn_fds = 0;
1653 if (s->socket_fd >= 0)
1656 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1661 if (u->type != UNIT_SOCKET)
1666 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1678 if (!(t = new(int, rn_fds+cn_fds))) {
1684 memcpy(t, rfds, rn_fds * sizeof(int));
1685 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1690 rn_fds = rn_fds+cn_fds;
1705 static int service_spawn(
1710 bool apply_permissions,
1712 bool apply_tty_stdin,
1713 bool set_notify_socket,
1719 int *fds = NULL, *fdsbuf = NULL;
1720 unsigned n_fds = 0, n_env = 0;
1721 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1728 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1729 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1730 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1732 if (s->socket_fd >= 0) {
1733 fds = &s->socket_fd;
1736 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1743 if (timeout && s->timeout_start_usec) {
1744 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_start_usec, &s->timer_watch);
1748 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1750 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1755 our_env = new0(char*, 5);
1761 if (set_notify_socket)
1762 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1767 if (s->main_pid > 0)
1768 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1773 if (s->watchdog_usec > 0)
1774 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1779 if (s->meta.manager->running_as != SYSTEMD_SYSTEM)
1780 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1785 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1799 UNIT(s)->manager->confirm_spawn,
1800 UNIT(s)->cgroup_bondings,
1801 UNIT(s)->cgroup_attributes,
1802 is_control ? "control" : NULL,
1804 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1810 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1811 /* FIXME: we need to do something here */
1817 strv_free(final_env);
1827 strv_free(final_env);
1830 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1835 static int main_pid_good(Service *s) {
1838 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1841 /* If we know the pid file, then lets just check if it is
1843 if (s->main_pid_known) {
1845 /* If it's an alien child let's check if it is still
1847 if (s->main_pid_alien)
1848 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1850 /* .. otherwise assume we'll get a SIGCHLD for it,
1851 * which we really should wait for to collect exit
1852 * status and code */
1853 return s->main_pid > 0;
1856 /* We don't know the pid */
1860 static int control_pid_good(Service *s) {
1863 return s->control_pid > 0;
1866 static int cgroup_good(Service *s) {
1871 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1877 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1881 if (f != SERVICE_SUCCESS)
1884 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1886 if (allow_restart &&
1887 !s->forbid_restart &&
1888 (s->restart == SERVICE_RESTART_ALWAYS ||
1889 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1890 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1891 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1892 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1893 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1894 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1895 (s->result != SERVICE_FAILURE_SIGNAL ||
1896 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1899 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
1903 service_set_state(s, SERVICE_AUTO_RESTART);
1906 s->forbid_restart = false;
1911 log_warning_unit(UNIT(s)->id,
1912 "%s failed to run install restart timer: %s",
1913 UNIT(s)->id, strerror(-r));
1914 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1917 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1919 static void service_enter_stop_post(Service *s, ServiceResult f) {
1923 if (f != SERVICE_SUCCESS)
1926 service_unwatch_control_pid(s);
1928 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1929 s->control_command_id = SERVICE_EXEC_STOP_POST;
1931 r = service_spawn(s,
1935 !s->permissions_start_only,
1936 !s->root_directory_start_only,
1945 service_set_state(s, SERVICE_STOP_POST);
1947 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1952 log_warning_unit(UNIT(s)->id,
1953 "%s failed to run 'stop-post' task: %s",
1954 UNIT(s)->id, strerror(-r));
1955 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1958 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1963 if (f != SERVICE_SUCCESS)
1966 r = unit_kill_context(
1969 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1977 if (s->timeout_stop_usec > 0) {
1978 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_stop_usec, &s->timer_watch);
1983 service_set_state(s, state);
1984 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1985 service_enter_stop_post(s, SERVICE_SUCCESS);
1987 service_enter_dead(s, SERVICE_SUCCESS, true);
1992 log_warning_unit(UNIT(s)->id,
1993 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1995 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1996 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1998 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2001 static void service_enter_stop(Service *s, ServiceResult f) {
2006 if (f != SERVICE_SUCCESS)
2009 service_unwatch_control_pid(s);
2011 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2012 s->control_command_id = SERVICE_EXEC_STOP;
2014 r = service_spawn(s,
2018 !s->permissions_start_only,
2019 !s->root_directory_start_only,
2027 service_set_state(s, SERVICE_STOP);
2029 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2034 log_warning_unit(UNIT(s)->id,
2035 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2036 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2039 static void service_enter_running(Service *s, ServiceResult f) {
2040 int main_pid_ok, cgroup_ok;
2043 if (f != SERVICE_SUCCESS)
2046 main_pid_ok = main_pid_good(s);
2047 cgroup_ok = cgroup_good(s);
2049 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2050 (s->bus_name_good || s->type != SERVICE_DBUS))
2051 service_set_state(s, SERVICE_RUNNING);
2052 else if (s->remain_after_exit)
2053 service_set_state(s, SERVICE_EXITED);
2055 service_enter_stop(s, SERVICE_SUCCESS);
2058 static void service_enter_start_post(Service *s) {
2062 service_unwatch_control_pid(s);
2064 if (s->watchdog_usec > 0)
2065 service_reset_watchdog(s);
2067 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2068 s->control_command_id = SERVICE_EXEC_START_POST;
2070 r = service_spawn(s,
2074 !s->permissions_start_only,
2075 !s->root_directory_start_only,
2083 service_set_state(s, SERVICE_START_POST);
2085 service_enter_running(s, SERVICE_SUCCESS);
2090 log_warning_unit(UNIT(s)->id,
2091 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2092 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2095 static void service_enter_start(Service *s) {
2102 assert(s->exec_command[SERVICE_EXEC_START]);
2103 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2105 if (s->type == SERVICE_FORKING)
2106 service_unwatch_control_pid(s);
2108 service_unwatch_main_pid(s);
2110 /* We want to ensure that nobody leaks processes from
2111 * START_PRE here, so let's go on a killing spree, People
2112 * should not spawn long running processes from START_PRE. */
2113 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2115 if (s->type == SERVICE_FORKING) {
2116 s->control_command_id = SERVICE_EXEC_START;
2117 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2119 s->main_command = NULL;
2121 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2122 s->control_command = NULL;
2124 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2127 r = service_spawn(s,
2129 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2134 s->notify_access != NOTIFY_NONE,
2140 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2141 /* For simple services we immediately start
2142 * the START_POST binaries. */
2144 service_set_main_pid(s, pid);
2145 service_enter_start_post(s);
2147 } else if (s->type == SERVICE_FORKING) {
2149 /* For forking services we wait until the start
2150 * process exited. */
2152 s->control_pid = pid;
2153 service_set_state(s, SERVICE_START);
2155 } else if (s->type == SERVICE_ONESHOT ||
2156 s->type == SERVICE_DBUS ||
2157 s->type == SERVICE_NOTIFY) {
2159 /* For oneshot services we wait until the start
2160 * process exited, too, but it is our main process. */
2162 /* For D-Bus services we know the main pid right away,
2163 * but wait for the bus name to appear on the
2164 * bus. Notify services are similar. */
2166 service_set_main_pid(s, pid);
2167 service_set_state(s, SERVICE_START);
2169 assert_not_reached("Unknown service type");
2174 log_warning_unit(UNIT(s)->id,
2175 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2176 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2179 static void service_enter_start_pre(Service *s) {
2184 service_unwatch_control_pid(s);
2186 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2188 /* Before we start anything, let's clear up what might
2189 * be left from previous runs. */
2190 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2192 s->control_command_id = SERVICE_EXEC_START_PRE;
2194 r = service_spawn(s,
2198 !s->permissions_start_only,
2199 !s->root_directory_start_only,
2207 service_set_state(s, SERVICE_START_PRE);
2209 service_enter_start(s);
2214 log_warning_unit(UNIT(s)->id,
2215 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2216 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2219 static void service_enter_restart(Service *s) {
2224 dbus_error_init(&error);
2226 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2227 /* Don't restart things if we are going down anyway */
2228 log_info_unit(UNIT(s)->id,
2229 "Stop job pending for unit, delaying automatic restart.");
2231 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
2238 /* Any units that are bound to this service must also be
2239 * restarted. We use JOB_RESTART (instead of the more obvious
2240 * JOB_START) here so that those dependency jobs will be added
2242 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2246 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2247 * it will be canceled as part of the service_stop() call that
2248 * is executed as part of JOB_RESTART. */
2250 log_debug_unit(UNIT(s)->id,
2251 "%s scheduled restart job.", UNIT(s)->id);
2255 log_warning_unit(UNIT(s)->id,
2256 "%s failed to schedule restart job: %s",
2257 UNIT(s)->id, bus_error(&error, -r));
2258 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2260 dbus_error_free(&error);
2263 static void service_enter_reload(Service *s) {
2268 service_unwatch_control_pid(s);
2270 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2271 s->control_command_id = SERVICE_EXEC_RELOAD;
2273 r = service_spawn(s,
2277 !s->permissions_start_only,
2278 !s->root_directory_start_only,
2286 service_set_state(s, SERVICE_RELOAD);
2288 service_enter_running(s, SERVICE_SUCCESS);
2293 log_warning_unit(UNIT(s)->id,
2294 "%s failed to run 'reload' task: %s",
2295 UNIT(s)->id, strerror(-r));
2296 s->reload_result = SERVICE_FAILURE_RESOURCES;
2297 service_enter_running(s, SERVICE_SUCCESS);
2300 static void service_run_next_control(Service *s) {
2304 assert(s->control_command);
2305 assert(s->control_command->command_next);
2307 assert(s->control_command_id != SERVICE_EXEC_START);
2309 s->control_command = s->control_command->command_next;
2310 service_unwatch_control_pid(s);
2312 r = service_spawn(s,
2316 !s->permissions_start_only,
2317 !s->root_directory_start_only,
2318 s->control_command_id == SERVICE_EXEC_START_PRE ||
2319 s->control_command_id == SERVICE_EXEC_STOP_POST,
2329 log_warning_unit(UNIT(s)->id,
2330 "%s failed to run next control task: %s",
2331 UNIT(s)->id, strerror(-r));
2333 if (s->state == SERVICE_START_PRE)
2334 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2335 else if (s->state == SERVICE_STOP)
2336 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2337 else if (s->state == SERVICE_STOP_POST)
2338 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2339 else if (s->state == SERVICE_RELOAD) {
2340 s->reload_result = SERVICE_FAILURE_RESOURCES;
2341 service_enter_running(s, SERVICE_SUCCESS);
2343 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2346 static void service_run_next_main(Service *s) {
2351 assert(s->main_command);
2352 assert(s->main_command->command_next);
2353 assert(s->type == SERVICE_ONESHOT);
2355 s->main_command = s->main_command->command_next;
2356 service_unwatch_main_pid(s);
2358 r = service_spawn(s,
2365 s->notify_access != NOTIFY_NONE,
2371 service_set_main_pid(s, pid);
2376 log_warning_unit(UNIT(s)->id,
2377 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2378 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2381 static int service_start_limit_test(Service *s) {
2384 if (ratelimit_test(&s->start_limit))
2387 switch (s->start_limit_action) {
2389 case SERVICE_START_LIMIT_NONE:
2390 log_warning_unit(UNIT(s)->id,
2391 "%s start request repeated too quickly, refusing to start.",
2395 case SERVICE_START_LIMIT_REBOOT: {
2399 dbus_error_init(&error);
2401 log_warning_unit(UNIT(s)->id,
2402 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2404 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2406 log_error_unit(UNIT(s)->id,
2407 "Failed to reboot: %s.", bus_error(&error, r));
2408 dbus_error_free(&error);
2414 case SERVICE_START_LIMIT_REBOOT_FORCE:
2415 log_warning_unit(UNIT(s)->id,
2416 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2417 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2420 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2421 log_warning_unit(UNIT(s)->id,
2422 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2424 reboot(RB_AUTOBOOT);
2428 log_error_unit(UNIT(s)->id,
2429 "start limit action=%i", s->start_limit_action);
2430 assert_not_reached("Unknown StartLimitAction.");
2436 static int service_start(Unit *u) {
2437 Service *s = SERVICE(u);
2442 /* We cannot fulfill this request right now, try again later
2444 if (s->state == SERVICE_STOP ||
2445 s->state == SERVICE_STOP_SIGTERM ||
2446 s->state == SERVICE_STOP_SIGKILL ||
2447 s->state == SERVICE_STOP_POST ||
2448 s->state == SERVICE_FINAL_SIGTERM ||
2449 s->state == SERVICE_FINAL_SIGKILL)
2452 /* Already on it! */
2453 if (s->state == SERVICE_START_PRE ||
2454 s->state == SERVICE_START ||
2455 s->state == SERVICE_START_POST)
2458 /* A service that will be restarted must be stopped first to
2459 * trigger BindsTo and/or OnFailure dependencies. If a user
2460 * does not want to wait for the holdoff time to elapse, the
2461 * service should be manually restarted, not started. We
2462 * simply return EAGAIN here, so that any start jobs stay
2463 * queued, and assume that the auto restart timer will
2464 * eventually trigger the restart. */
2465 if (s->state == SERVICE_AUTO_RESTART)
2468 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2470 /* Make sure we don't enter a busy loop of some kind. */
2471 r = service_start_limit_test(s);
2473 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2477 s->result = SERVICE_SUCCESS;
2478 s->reload_result = SERVICE_SUCCESS;
2479 s->main_pid_known = false;
2480 s->main_pid_alien = false;
2481 s->forbid_restart = false;
2483 service_enter_start_pre(s);
2487 static int service_stop(Unit *u) {
2488 Service *s = SERVICE(u);
2492 /* Don't create restart jobs from here. */
2493 s->forbid_restart = true;
2496 if (s->state == SERVICE_STOP ||
2497 s->state == SERVICE_STOP_SIGTERM ||
2498 s->state == SERVICE_STOP_SIGKILL ||
2499 s->state == SERVICE_STOP_POST ||
2500 s->state == SERVICE_FINAL_SIGTERM ||
2501 s->state == SERVICE_FINAL_SIGKILL)
2504 /* A restart will be scheduled or is in progress. */
2505 if (s->state == SERVICE_AUTO_RESTART) {
2506 service_set_state(s, SERVICE_DEAD);
2510 /* If there's already something running we go directly into
2512 if (s->state == SERVICE_START_PRE ||
2513 s->state == SERVICE_START ||
2514 s->state == SERVICE_START_POST ||
2515 s->state == SERVICE_RELOAD) {
2516 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2520 assert(s->state == SERVICE_RUNNING ||
2521 s->state == SERVICE_EXITED);
2523 service_enter_stop(s, SERVICE_SUCCESS);
2527 static int service_reload(Unit *u) {
2528 Service *s = SERVICE(u);
2532 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2534 service_enter_reload(s);
2538 static bool service_can_reload(Unit *u) {
2539 Service *s = SERVICE(u);
2543 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2546 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2547 Service *s = SERVICE(u);
2553 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2554 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2555 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2557 if (s->control_pid > 0)
2558 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2560 if (s->main_pid_known && s->main_pid > 0)
2561 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2563 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2566 unit_serialize_item(u, f, "status-text", s->status_text);
2568 /* FIXME: There's a minor uncleanliness here: if there are
2569 * multiple commands attached here, we will start from the
2570 * first one again */
2571 if (s->control_command_id >= 0)
2572 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2574 if (s->socket_fd >= 0) {
2577 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2580 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2583 if (s->main_exec_status.pid > 0) {
2584 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2585 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2586 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2588 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2589 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2590 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2593 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2594 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2599 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2600 Service *s = SERVICE(u);
2607 if (streq(key, "state")) {
2610 if ((state = service_state_from_string(value)) < 0)
2611 log_debug_unit(u->id, "Failed to parse state value %s", value);
2613 s->deserialized_state = state;
2614 } else if (streq(key, "result")) {
2617 f = service_result_from_string(value);
2619 log_debug_unit(u->id, "Failed to parse result value %s", value);
2620 else if (f != SERVICE_SUCCESS)
2623 } else if (streq(key, "reload-result")) {
2626 f = service_result_from_string(value);
2628 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2629 else if (f != SERVICE_SUCCESS)
2630 s->reload_result = f;
2632 } else if (streq(key, "control-pid")) {
2635 if (parse_pid(value, &pid) < 0)
2636 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2638 s->control_pid = pid;
2639 } else if (streq(key, "main-pid")) {
2642 if (parse_pid(value, &pid) < 0)
2643 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2645 service_set_main_pid(s, (pid_t) pid);
2646 } else if (streq(key, "main-pid-known")) {
2649 if ((b = parse_boolean(value)) < 0)
2650 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2652 s->main_pid_known = b;
2653 } else if (streq(key, "status-text")) {
2656 if ((t = strdup(value))) {
2657 free(s->status_text);
2661 } else if (streq(key, "control-command")) {
2662 ServiceExecCommand id;
2664 if ((id = service_exec_command_from_string(value)) < 0)
2665 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2667 s->control_command_id = id;
2668 s->control_command = s->exec_command[id];
2670 } else if (streq(key, "socket-fd")) {
2673 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2674 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2677 if (s->socket_fd >= 0)
2678 close_nointr_nofail(s->socket_fd);
2679 s->socket_fd = fdset_remove(fds, fd);
2681 } else if (streq(key, "main-exec-status-pid")) {
2684 if (parse_pid(value, &pid) < 0)
2685 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2687 s->main_exec_status.pid = pid;
2688 } else if (streq(key, "main-exec-status-code")) {
2691 if (safe_atoi(value, &i) < 0)
2692 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2694 s->main_exec_status.code = i;
2695 } else if (streq(key, "main-exec-status-status")) {
2698 if (safe_atoi(value, &i) < 0)
2699 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2701 s->main_exec_status.status = i;
2702 } else if (streq(key, "main-exec-status-start"))
2703 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2704 else if (streq(key, "main-exec-status-exit"))
2705 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2706 else if (streq(key, "watchdog-timestamp"))
2707 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2709 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2714 static UnitActiveState service_active_state(Unit *u) {
2715 const UnitActiveState *table;
2719 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2721 return table[SERVICE(u)->state];
2724 static const char *service_sub_state_to_string(Unit *u) {
2727 return service_state_to_string(SERVICE(u)->state);
2730 static bool service_check_gc(Unit *u) {
2731 Service *s = SERVICE(u);
2735 /* Never clean up services that still have a process around,
2736 * even if the service is formally dead. */
2737 if (cgroup_good(s) > 0 ||
2738 main_pid_good(s) > 0 ||
2739 control_pid_good(s) > 0)
2742 #ifdef HAVE_SYSV_COMPAT
2750 static bool service_check_snapshot(Unit *u) {
2751 Service *s = SERVICE(u);
2755 return !s->got_socket_fd;
2758 static int service_retry_pid_file(Service *s) {
2761 assert(s->pid_file);
2762 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2764 r = service_load_pid_file(s, false);
2768 service_unwatch_pid_file(s);
2770 service_enter_running(s, SERVICE_SUCCESS);
2774 static int service_watch_pid_file(Service *s) {
2777 log_debug_unit(UNIT(s)->id,
2778 "Setting watch for %s's PID file %s",
2779 UNIT(s)->id, s->pid_file_pathspec->path);
2780 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2784 /* the pidfile might have appeared just before we set the watch */
2785 service_retry_pid_file(s);
2789 log_error_unit(UNIT(s)->id,
2790 "Failed to set a watch for %s's PID file %s: %s",
2791 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2792 service_unwatch_pid_file(s);
2796 static int service_demand_pid_file(Service *s) {
2799 assert(s->pid_file);
2800 assert(!s->pid_file_pathspec);
2802 ps = new0(PathSpec, 1);
2806 ps->path = strdup(s->pid_file);
2812 path_kill_slashes(ps->path);
2814 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2815 * keep their PID file open all the time. */
2816 ps->type = PATH_MODIFIED;
2817 ps->inotify_fd = -1;
2819 s->pid_file_pathspec = ps;
2821 return service_watch_pid_file(s);
2824 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2825 Service *s = SERVICE(u);
2829 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2830 assert(s->pid_file_pathspec);
2831 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2833 log_debug_unit(u->id, "inotify event for %s", u->id);
2835 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2838 if (service_retry_pid_file(s) == 0)
2841 if (service_watch_pid_file(s) < 0)
2846 service_unwatch_pid_file(s);
2847 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2850 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2851 Service *s = SERVICE(u);
2857 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2858 is_clean_exit_lsb(code, status, &s->success_status))
2859 f = SERVICE_SUCCESS;
2860 else if (code == CLD_EXITED)
2861 f = SERVICE_FAILURE_EXIT_CODE;
2862 else if (code == CLD_KILLED)
2863 f = SERVICE_FAILURE_SIGNAL;
2864 else if (code == CLD_DUMPED)
2865 f = SERVICE_FAILURE_CORE_DUMP;
2867 assert_not_reached("Unknown code");
2869 if (s->main_pid == pid) {
2870 /* Forking services may occasionally move to a new PID.
2871 * As long as they update the PID file before exiting the old
2872 * PID, they're fine. */
2873 if (service_load_pid_file(s, false) == 0)
2877 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2879 if (s->main_command) {
2880 /* If this is not a forking service than the
2881 * main process got started and hence we copy
2882 * the exit status so that it is recorded both
2883 * as main and as control process exit
2886 s->main_command->exec_status = s->main_exec_status;
2888 if (s->main_command->ignore)
2889 f = SERVICE_SUCCESS;
2890 } else if (s->exec_command[SERVICE_EXEC_START]) {
2892 /* If this is a forked process, then we should
2893 * ignore the return value if this was
2894 * configured for the starter process */
2896 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2897 f = SERVICE_SUCCESS;
2900 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2902 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2903 u->id, sigchld_code_to_string(code), status,
2904 strna(code == CLD_EXITED
2905 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2906 : signal_to_string(status)),
2907 "EXIT_CODE=%s", sigchld_code_to_string(code),
2908 "EXIT_STATUS=%i", status,
2911 if (f != SERVICE_SUCCESS)
2914 if (s->main_command &&
2915 s->main_command->command_next &&
2916 f == SERVICE_SUCCESS) {
2918 /* There is another command to *
2919 * execute, so let's do that. */
2921 log_debug_unit(u->id,
2922 "%s running next main command for state %s",
2923 u->id, service_state_to_string(s->state));
2924 service_run_next_main(s);
2928 /* The service exited, so the service is officially
2930 s->main_command = NULL;
2934 case SERVICE_START_POST:
2935 case SERVICE_RELOAD:
2937 /* Need to wait until the operation is
2942 if (s->type == SERVICE_ONESHOT) {
2943 /* This was our main goal, so let's go on */
2944 if (f == SERVICE_SUCCESS)
2945 service_enter_start_post(s);
2947 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2953 case SERVICE_RUNNING:
2954 service_enter_running(s, f);
2957 case SERVICE_STOP_SIGTERM:
2958 case SERVICE_STOP_SIGKILL:
2960 if (!control_pid_good(s))
2961 service_enter_stop_post(s, f);
2963 /* If there is still a control process, wait for that first */
2967 assert_not_reached("Uh, main process died at wrong time.");
2971 } else if (s->control_pid == pid) {
2975 if (s->control_command) {
2976 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2978 if (s->control_command->ignore)
2979 f = SERVICE_SUCCESS;
2982 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
2983 "%s: control process exited, code=%s status=%i",
2984 u->id, sigchld_code_to_string(code), status);
2986 if (f != SERVICE_SUCCESS)
2989 /* Immediately get rid of the cgroup, so that the
2990 * kernel doesn't delay the cgroup empty messages for
2991 * the service cgroup any longer than necessary */
2992 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2994 if (s->control_command &&
2995 s->control_command->command_next &&
2996 f == SERVICE_SUCCESS) {
2998 /* There is another command to *
2999 * execute, so let's do that. */
3001 log_debug_unit(u->id,
3002 "%s running next control command for state %s",
3003 u->id, service_state_to_string(s->state));
3004 service_run_next_control(s);
3007 /* No further commands for this step, so let's
3008 * figure out what to do next */
3010 s->control_command = NULL;
3011 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3013 log_debug_unit(u->id,
3014 "%s got final SIGCHLD for state %s",
3015 u->id, service_state_to_string(s->state));
3019 case SERVICE_START_PRE:
3020 if (f == SERVICE_SUCCESS)
3021 service_enter_start(s);
3023 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3027 if (s->type != SERVICE_FORKING)
3028 /* Maybe spurious event due to a reload that changed the type? */
3031 if (f != SERVICE_SUCCESS) {
3032 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3037 bool has_start_post;
3040 /* Let's try to load the pid file here if we can.
3041 * The PID file might actually be created by a START_POST
3042 * script. In that case don't worry if the loading fails. */
3044 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3045 r = service_load_pid_file(s, !has_start_post);
3046 if (!has_start_post && r < 0) {
3047 r = service_demand_pid_file(s);
3048 if (r < 0 || !cgroup_good(s))
3049 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3053 service_search_main_pid(s);
3055 service_enter_start_post(s);
3058 case SERVICE_START_POST:
3059 if (f != SERVICE_SUCCESS) {
3060 service_enter_stop(s, f);
3067 r = service_load_pid_file(s, true);
3069 r = service_demand_pid_file(s);
3070 if (r < 0 || !cgroup_good(s))
3071 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3075 service_search_main_pid(s);
3077 service_enter_running(s, SERVICE_SUCCESS);
3080 case SERVICE_RELOAD:
3081 if (f == SERVICE_SUCCESS) {
3082 service_load_pid_file(s, true);
3083 service_search_main_pid(s);
3086 s->reload_result = f;
3087 service_enter_running(s, SERVICE_SUCCESS);
3091 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3094 case SERVICE_STOP_SIGTERM:
3095 case SERVICE_STOP_SIGKILL:
3096 if (main_pid_good(s) <= 0)
3097 service_enter_stop_post(s, f);
3099 /* If there is still a service
3100 * process around, wait until
3101 * that one quit, too */
3104 case SERVICE_STOP_POST:
3105 case SERVICE_FINAL_SIGTERM:
3106 case SERVICE_FINAL_SIGKILL:
3107 service_enter_dead(s, f, true);
3111 assert_not_reached("Uh, control process died at wrong time.");
3116 /* Notify clients about changed exit status */
3117 unit_add_to_dbus_queue(u);
3120 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3121 Service *s = SERVICE(u);
3124 assert(elapsed == 1);
3126 if (w == &s->watchdog_watch) {
3127 service_handle_watchdog(s);
3131 assert(w == &s->timer_watch);
3135 case SERVICE_START_PRE:
3137 log_warning_unit(u->id,
3138 "%s operation timed out. Terminating.", u->id);
3139 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3142 case SERVICE_START_POST:
3143 log_warning_unit(u->id,
3144 "%s operation timed out. Stopping.", u->id);
3145 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3148 case SERVICE_RELOAD:
3149 log_warning_unit(u->id,
3150 "%s operation timed out. Stopping.", u->id);
3151 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3152 service_enter_running(s, SERVICE_SUCCESS);
3156 log_warning_unit(u->id,
3157 "%s stopping timed out. Terminating.", u->id);
3158 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3161 case SERVICE_STOP_SIGTERM:
3162 if (s->kill_context.send_sigkill) {
3163 log_warning_unit(u->id,
3164 "%s stopping timed out. Killing.", u->id);
3165 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3167 log_warning_unit(u->id,
3168 "%s stopping timed out. Skipping SIGKILL.", u->id);
3169 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3174 case SERVICE_STOP_SIGKILL:
3175 /* Uh, we sent a SIGKILL and it is still not gone?
3176 * Must be something we cannot kill, so let's just be
3177 * weirded out and continue */
3179 log_warning_unit(u->id,
3180 "%s still around after SIGKILL. Ignoring.", u->id);
3181 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3184 case SERVICE_STOP_POST:
3185 log_warning_unit(u->id,
3186 "%s stopping timed out (2). Terminating.", u->id);
3187 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3190 case SERVICE_FINAL_SIGTERM:
3191 if (s->kill_context.send_sigkill) {
3192 log_warning_unit(u->id,
3193 "%s stopping timed out (2). Killing.", u->id);
3194 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3196 log_warning_unit(u->id,
3197 "%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.",
3199 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3204 case SERVICE_FINAL_SIGKILL:
3205 log_warning_unit(u->id,
3206 "%s still around after SIGKILL (2). Entering failed mode.", u->id);
3207 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3210 case SERVICE_AUTO_RESTART:
3211 log_info_unit(u->id,
3212 "%s holdoff time over, scheduling restart.", u->id);
3213 service_enter_restart(s);
3217 assert_not_reached("Timeout at wrong time.");
3221 static void service_cgroup_notify_event(Unit *u) {
3222 Service *s = SERVICE(u);
3226 log_debug_unit(u->id,
3227 "%s: cgroup is empty", u->id);
3231 /* Waiting for SIGCHLD is usually more interesting,
3232 * because it includes return codes/signals. Which is
3233 * why we ignore the cgroup events for most cases,
3234 * except when we don't know pid which to expect the
3238 case SERVICE_START_POST:
3239 /* If we were hoping for the daemon to write its PID file,
3240 * we can give up now. */
3241 if (s->pid_file_pathspec) {
3242 log_warning_unit(u->id,
3243 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3244 service_unwatch_pid_file(s);
3245 if (s->state == SERVICE_START)
3246 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3248 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3252 case SERVICE_RUNNING:
3253 /* service_enter_running() will figure out what to do */
3254 service_enter_running(s, SERVICE_SUCCESS);
3257 case SERVICE_STOP_SIGTERM:
3258 case SERVICE_STOP_SIGKILL:
3260 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3261 service_enter_stop_post(s, SERVICE_SUCCESS);
3265 case SERVICE_FINAL_SIGTERM:
3266 case SERVICE_FINAL_SIGKILL:
3267 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3268 service_enter_dead(s, SERVICE_SUCCESS, true);
3277 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3278 Service *s = SERVICE(u);
3283 if (s->notify_access == NOTIFY_NONE) {
3284 log_warning_unit(u->id,
3285 "%s: Got notification message from PID %lu, but reception is disabled.",
3286 u->id, (unsigned long) pid);
3290 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3291 log_warning_unit(u->id,
3292 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3293 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3297 log_debug_unit(u->id,
3298 "%s: Got message", u->id);
3300 /* Interpret MAINPID= */
3301 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3302 (s->state == SERVICE_START ||
3303 s->state == SERVICE_START_POST ||
3304 s->state == SERVICE_RUNNING ||
3305 s->state == SERVICE_RELOAD)) {
3307 if (parse_pid(e + 8, &pid) < 0)
3308 log_warning_unit(u->id,
3309 "Failed to parse notification message %s", e);
3311 log_debug_unit(u->id,
3312 "%s: got %s", u->id, e);
3313 service_set_main_pid(s, pid);
3317 /* Interpret READY= */
3318 if (s->type == SERVICE_NOTIFY &&
3319 s->state == SERVICE_START &&
3320 strv_find(tags, "READY=1")) {
3321 log_debug_unit(u->id,
3322 "%s: got READY=1", u->id);
3324 service_enter_start_post(s);
3327 /* Interpret STATUS= */
3328 e = strv_find_prefix(tags, "STATUS=");
3334 if (!utf8_is_valid(e+7)) {
3335 log_warning_unit(u->id,
3336 "Status message in notification is not UTF-8 clean.");
3342 log_error_unit(u->id,
3343 "Failed to allocate string.");
3347 log_debug_unit(u->id,
3348 "%s: got %s", u->id, e);
3350 free(s->status_text);
3353 free(s->status_text);
3354 s->status_text = NULL;
3358 if (strv_find(tags, "WATCHDOG=1")) {
3359 log_debug_unit(u->id,
3360 "%s: got WATCHDOG=1", u->id);
3361 if (dual_timestamp_is_set(&s->watchdog_timestamp))
3362 service_reset_watchdog(s);
3365 /* Notify clients about changed status or main pid */
3366 unit_add_to_dbus_queue(u);
3369 #ifdef HAVE_SYSV_COMPAT
3371 static int service_enumerate(Manager *m) {
3375 char *path = NULL, *fpath = NULL, *name = NULL;
3376 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3383 if (m->running_as != SYSTEMD_SYSTEM)
3386 zero(runlevel_services);
3388 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3389 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3393 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3402 if (!(d = opendir(path))) {
3403 if (errno != ENOENT)
3404 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3409 while ((de = readdir(d))) {
3412 if (ignore_file(de->d_name))
3415 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3418 if (strlen(de->d_name) < 4)
3421 a = undecchar(de->d_name[1]);
3422 b = undecchar(de->d_name[2]);
3428 fpath = strjoin(path, "/", de->d_name, NULL);
3434 if (access(fpath, X_OK) < 0) {
3436 if (errno != ENOENT)
3437 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3443 if (!(name = sysv_translate_name(de->d_name + 3))) {
3448 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3450 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3454 if (de->d_name[0] == 'S') {
3456 if (rcnd_table[i].type == RUNLEVEL_UP) {
3457 SERVICE(service)->sysv_start_priority_from_rcnd =
3458 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3460 SERVICE(service)->sysv_enabled = true;
3463 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3466 if ((r = set_put(runlevel_services[i], service)) < 0)
3469 } else if (de->d_name[0] == 'K' &&
3470 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3472 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3475 if ((r = set_put(shutdown_services, service)) < 0)
3481 /* Now we loaded all stubs and are aware of the lowest
3482 start-up priority for all services, not let's actually load
3483 the services, this will also tell us which services are
3484 actually native now */
3485 manager_dispatch_load_queue(m);
3487 /* If this is a native service, rely on native ways to pull in
3488 * a service, don't pull it in via sysv rcN.d links. */
3489 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3490 SET_FOREACH(service, runlevel_services[i], j) {
3491 service = unit_follow_merge(service);
3493 if (service->fragment_path)
3496 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3500 /* We honour K links only for halt/reboot. For the normal
3501 * runlevels we assume the stop jobs will be implicitly added
3502 * by the core logic. Also, we don't really distinguish here
3503 * between the runlevels 0 and 6 and just add them to the
3504 * special shutdown target. */
3505 SET_FOREACH(service, shutdown_services, j) {
3506 service = unit_follow_merge(service);
3508 if (service->fragment_path)
3511 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3522 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3523 set_free(runlevel_services[i]);
3524 set_free(shutdown_services);
3533 static void service_bus_name_owner_change(
3536 const char *old_owner,
3537 const char *new_owner) {
3539 Service *s = SERVICE(u);
3544 assert(streq(s->bus_name, name));
3545 assert(old_owner || new_owner);
3547 if (old_owner && new_owner)
3548 log_debug_unit(u->id,
3549 "%s's D-Bus name %s changed owner from %s to %s",
3550 u->id, name, old_owner, new_owner);
3552 log_debug_unit(u->id,
3553 "%s's D-Bus name %s no longer registered by %s",
3554 u->id, name, old_owner);
3556 log_debug_unit(u->id,
3557 "%s's D-Bus name %s now registered by %s",
3558 u->id, name, new_owner);
3560 s->bus_name_good = !!new_owner;
3562 if (s->type == SERVICE_DBUS) {
3564 /* service_enter_running() will figure out what to
3566 if (s->state == SERVICE_RUNNING)
3567 service_enter_running(s, SERVICE_SUCCESS);
3568 else if (s->state == SERVICE_START && new_owner)
3569 service_enter_start_post(s);
3571 } else if (new_owner &&
3573 (s->state == SERVICE_START ||
3574 s->state == SERVICE_START_POST ||
3575 s->state == SERVICE_RUNNING ||
3576 s->state == SERVICE_RELOAD)) {
3578 /* Try to acquire PID from bus service */
3579 log_debug_unit(u->id,
3580 "Trying to acquire PID from D-Bus name...");
3582 bus_query_pid(u->manager, name);
3586 static void service_bus_query_pid_done(
3591 Service *s = SERVICE(u);
3596 log_debug_unit(u->id,
3597 "%s's D-Bus name %s is now owned by process %u",
3598 u->id, name, (unsigned) pid);
3600 if (s->main_pid <= 0 &&
3601 (s->state == SERVICE_START ||
3602 s->state == SERVICE_START_POST ||
3603 s->state == SERVICE_RUNNING ||
3604 s->state == SERVICE_RELOAD))
3605 service_set_main_pid(s, pid);
3608 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3613 /* This is called by the socket code when instantiating a new
3614 * service for a stream socket and the socket needs to be
3617 if (UNIT(s)->load_state != UNIT_LOADED)
3620 if (s->socket_fd >= 0)
3623 if (s->state != SERVICE_DEAD)
3627 s->got_socket_fd = true;
3629 unit_ref_set(&s->accept_socket, UNIT(sock));
3631 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3634 static void service_reset_failed(Unit *u) {
3635 Service *s = SERVICE(u);
3639 if (s->state == SERVICE_FAILED)
3640 service_set_state(s, SERVICE_DEAD);
3642 s->result = SERVICE_SUCCESS;
3643 s->reload_result = SERVICE_SUCCESS;
3645 RATELIMIT_RESET(s->start_limit);
3648 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3649 Service *s = SERVICE(u);
3651 Set *pid_set = NULL;
3655 if (s->main_pid <= 0 && who == KILL_MAIN) {
3656 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3660 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3661 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3665 if (who == KILL_CONTROL || who == KILL_ALL)
3666 if (s->control_pid > 0)
3667 if (kill(s->control_pid, signo) < 0)
3670 if (who == KILL_MAIN || who == KILL_ALL)
3671 if (s->main_pid > 0)
3672 if (kill(s->main_pid, signo) < 0)
3675 if (who == KILL_ALL) {
3678 pid_set = set_new(trivial_hash_func, trivial_compare_func);
3682 /* Exclude the control/main pid from being killed via the cgroup */
3683 if (s->control_pid > 0) {
3684 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
3691 if (s->main_pid > 0) {
3692 q = set_put(pid_set, LONG_TO_PTR(s->main_pid));
3699 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3700 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3711 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3712 [SERVICE_DEAD] = "dead",
3713 [SERVICE_START_PRE] = "start-pre",
3714 [SERVICE_START] = "start",
3715 [SERVICE_START_POST] = "start-post",
3716 [SERVICE_RUNNING] = "running",
3717 [SERVICE_EXITED] = "exited",
3718 [SERVICE_RELOAD] = "reload",
3719 [SERVICE_STOP] = "stop",
3720 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3721 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3722 [SERVICE_STOP_POST] = "stop-post",
3723 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3724 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3725 [SERVICE_FAILED] = "failed",
3726 [SERVICE_AUTO_RESTART] = "auto-restart",
3729 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3731 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3732 [SERVICE_RESTART_NO] = "no",
3733 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3734 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3735 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3736 [SERVICE_RESTART_ALWAYS] = "always"
3739 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3741 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3742 [SERVICE_SIMPLE] = "simple",
3743 [SERVICE_FORKING] = "forking",
3744 [SERVICE_ONESHOT] = "oneshot",
3745 [SERVICE_DBUS] = "dbus",
3746 [SERVICE_NOTIFY] = "notify",
3747 [SERVICE_IDLE] = "idle"
3750 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3752 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3753 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3754 [SERVICE_EXEC_START] = "ExecStart",
3755 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3756 [SERVICE_EXEC_RELOAD] = "ExecReload",
3757 [SERVICE_EXEC_STOP] = "ExecStop",
3758 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3761 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3763 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3764 [NOTIFY_NONE] = "none",
3765 [NOTIFY_MAIN] = "main",
3766 [NOTIFY_ALL] = "all"
3769 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3771 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3772 [SERVICE_SUCCESS] = "success",
3773 [SERVICE_FAILURE_RESOURCES] = "resources",
3774 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3775 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3776 [SERVICE_FAILURE_SIGNAL] = "signal",
3777 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3778 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3779 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3782 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3784 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3785 [SERVICE_START_LIMIT_NONE] = "none",
3786 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3787 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3788 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3790 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3792 const UnitVTable service_vtable = {
3793 .object_size = sizeof(Service),
3800 .exec_context_offset = offsetof(Service, exec_context),
3801 .exec_section = "Service",
3803 .init = service_init,
3804 .done = service_done,
3805 .load = service_load,
3807 .coldplug = service_coldplug,
3809 .dump = service_dump,
3811 .start = service_start,
3812 .stop = service_stop,
3813 .reload = service_reload,
3815 .can_reload = service_can_reload,
3817 .kill = service_kill,
3819 .serialize = service_serialize,
3820 .deserialize_item = service_deserialize_item,
3822 .active_state = service_active_state,
3823 .sub_state_to_string = service_sub_state_to_string,
3825 .check_gc = service_check_gc,
3826 .check_snapshot = service_check_snapshot,
3828 .sigchld_event = service_sigchld_event,
3829 .timer_event = service_timer_event,
3830 .fd_event = service_fd_event,
3832 .reset_failed = service_reset_failed,
3834 .cgroup_notify_empty = service_cgroup_notify_event,
3835 .notify_message = service_notify_message,
3837 .bus_name_owner_change = service_bus_name_owner_change,
3838 .bus_query_pid_done = service_bus_query_pid_done,
3840 .bus_interface = "org.freedesktop.systemd1.Service",
3841 .bus_message_handler = bus_service_message_handler,
3842 .bus_invalidating_properties = bus_service_invalidating_properties,
3844 #ifdef HAVE_SYSV_COMPAT
3845 .enumerate = service_enumerate,
3847 .status_message_formats = {
3848 .starting_stopping = {
3849 [0] = "Starting %s...",
3850 [1] = "Stopping %s...",
3852 .finished_start_job = {
3853 [JOB_DONE] = "Started %s.",
3854 [JOB_FAILED] = "Failed to start %s.",
3855 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3856 [JOB_TIMEOUT] = "Timed out starting %s.",
3858 .finished_stop_job = {
3859 [JOB_DONE] = "Stopped %s.",
3860 [JOB_FAILED] = "Stopped (with error) %s.",
3861 [JOB_TIMEOUT] = "Timed out stopping %s.",