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 "exit-status.h"
41 #include "path-util.h"
46 #include "bus-error.h"
49 #ifdef HAVE_SYSV_COMPAT
51 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
53 typedef enum RunlevelType {
61 const RunlevelType type;
63 /* Standard SysV runlevels for start-up */
64 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
65 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
66 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
67 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
68 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
70 /* Standard SysV runlevels for shutdown */
71 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
72 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
74 /* Note that the order here matters, as we read the
75 directories in this order, and we want to make sure that
76 sysv_start_priority is known when we first load the
77 unit. And that value we only know from S links. Hence
78 UP must be read before DOWN */
81 #define RUNLEVELS_UP "12345"
84 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
85 [SERVICE_DEAD] = UNIT_INACTIVE,
86 [SERVICE_START_PRE] = UNIT_ACTIVATING,
87 [SERVICE_START] = UNIT_ACTIVATING,
88 [SERVICE_START_POST] = UNIT_ACTIVATING,
89 [SERVICE_RUNNING] = UNIT_ACTIVE,
90 [SERVICE_EXITED] = UNIT_ACTIVE,
91 [SERVICE_RELOAD] = UNIT_RELOADING,
92 [SERVICE_STOP] = UNIT_DEACTIVATING,
93 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
94 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
95 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
96 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
97 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
98 [SERVICE_FAILED] = UNIT_FAILED,
99 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
102 /* For Type=idle we never want to delay any other jobs, hence we
103 * consider idle jobs active as soon as we start working on them */
104 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
105 [SERVICE_DEAD] = UNIT_INACTIVE,
106 [SERVICE_START_PRE] = UNIT_ACTIVE,
107 [SERVICE_START] = UNIT_ACTIVE,
108 [SERVICE_START_POST] = UNIT_ACTIVE,
109 [SERVICE_RUNNING] = UNIT_ACTIVE,
110 [SERVICE_EXITED] = UNIT_ACTIVE,
111 [SERVICE_RELOAD] = UNIT_RELOADING,
112 [SERVICE_STOP] = UNIT_DEACTIVATING,
113 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
114 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
115 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
116 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
117 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
118 [SERVICE_FAILED] = UNIT_FAILED,
119 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
122 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata);
123 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
124 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata);
126 static void service_init(Unit *u) {
127 Service *s = SERVICE(u);
130 assert(u->load_state == UNIT_STUB);
132 s->timeout_start_usec = u->manager->default_timeout_start_usec;
133 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
134 s->restart_usec = u->manager->default_restart_usec;
135 s->type = _SERVICE_TYPE_INVALID;
137 #ifdef HAVE_SYSV_COMPAT
138 s->sysv_start_priority = -1;
139 s->sysv_start_priority_from_rcnd = -1;
142 s->guess_main_pid = true;
144 exec_context_init(&s->exec_context);
145 kill_context_init(&s->kill_context);
146 cgroup_context_init(&s->cgroup_context);
148 RATELIMIT_INIT(s->start_limit,
149 u->manager->default_start_limit_interval,
150 u->manager->default_start_limit_burst);
152 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
155 static void service_unwatch_control_pid(Service *s) {
158 if (s->control_pid <= 0)
161 unit_unwatch_pid(UNIT(s), s->control_pid);
165 static void service_unwatch_main_pid(Service *s) {
168 if (s->main_pid <= 0)
171 unit_unwatch_pid(UNIT(s), s->main_pid);
175 static void service_unwatch_pid_file(Service *s) {
176 if (!s->pid_file_pathspec)
179 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
180 UNIT(s)->id, s->pid_file_pathspec->path);
181 path_spec_unwatch(s->pid_file_pathspec);
182 path_spec_done(s->pid_file_pathspec);
183 free(s->pid_file_pathspec);
184 s->pid_file_pathspec = NULL;
187 static int service_set_main_pid(Service *s, pid_t pid) {
198 if (s->main_pid == pid && s->main_pid_known)
201 if (s->main_pid != pid) {
202 service_unwatch_main_pid(s);
203 exec_status_start(&s->main_exec_status, pid);
207 s->main_pid_known = true;
209 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
210 log_warning_unit(UNIT(s)->id,
211 "%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
212 UNIT(s)->id, (unsigned long) pid);
214 s->main_pid_alien = true;
216 s->main_pid_alien = false;
221 static void service_close_socket_fd(Service *s) {
224 if (s->socket_fd < 0)
227 close_nointr_nofail(s->socket_fd);
231 static void service_connection_unref(Service *s) {
234 if (!UNIT_ISSET(s->accept_socket))
237 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
238 unit_ref_unset(&s->accept_socket);
241 static void service_stop_watchdog(Service *s) {
244 s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source);
245 s->watchdog_timestamp = (struct dual_timestamp) { 0, 0 };
248 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
250 static void service_handle_watchdog(Service *s) {
256 if (s->watchdog_usec == 0)
259 nw = now(CLOCK_MONOTONIC);
260 if (nw >= s->watchdog_timestamp.monotonic + s->watchdog_usec) {
261 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
262 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_WATCHDOG);
266 if (s->watchdog_event_source) {
267 r = sd_event_source_set_time(s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec);
269 log_warning_unit(UNIT(s)->id, "%s failed to reset watchdog timer: %s", UNIT(s)->id, strerror(-r));
273 r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ON);
275 r = sd_event_add_monotonic(UNIT(s)->manager->event, s->watchdog_timestamp.monotonic + s->watchdog_usec, 0, service_dispatch_watchdog, s, &s->watchdog_event_source);
278 log_warning_unit(UNIT(s)->id,
279 "%s failed to install watchdog timer: %s",
280 UNIT(s)->id, strerror(-r));
283 static void service_reset_watchdog(Service *s) {
286 dual_timestamp_get(&s->watchdog_timestamp);
287 service_handle_watchdog(s);
290 static void service_done(Unit *u) {
291 Service *s = SERVICE(u);
298 #ifdef HAVE_SYSV_COMPAT
299 free(s->sysv_runlevels);
300 s->sysv_runlevels = NULL;
303 free(s->status_text);
304 s->status_text = NULL;
306 cgroup_context_done(&s->cgroup_context);
307 exec_context_done(&s->exec_context, manager_is_reloading_or_reexecuting(u->manager));
308 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
309 s->control_command = NULL;
310 s->main_command = NULL;
312 set_free(s->restart_ignore_status.code);
313 s->restart_ignore_status.code = NULL;
314 set_free(s->restart_ignore_status.signal);
315 s->restart_ignore_status.signal = NULL;
317 set_free(s->success_status.code);
318 s->success_status.code = NULL;
319 set_free(s->success_status.signal);
320 s->success_status.signal = NULL;
322 /* This will leak a process, but at least no memory or any of
324 service_unwatch_main_pid(s);
325 service_unwatch_control_pid(s);
326 service_unwatch_pid_file(s);
329 unit_unwatch_bus_name(u, s->bus_name);
334 service_close_socket_fd(s);
335 service_connection_unref(s);
337 unit_ref_unset(&s->accept_socket);
339 service_stop_watchdog(s);
341 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
344 static int service_arm_timer(Service *s, usec_t usec) {
350 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
354 if (s->timer_event_source) {
355 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec);
359 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
362 return sd_event_add_monotonic(UNIT(s)->manager->event, now(CLOCK_MONOTONIC) + usec, 0, service_dispatch_timer, s, &s->timer_event_source);
365 #ifdef HAVE_SYSV_COMPAT
366 static char *sysv_translate_name(const char *name) {
369 r = new(char, strlen(name) + sizeof(".service"));
373 if (endswith(name, ".sh"))
374 /* Drop .sh suffix */
375 strcpy(stpcpy(r, name) - 3, ".service");
377 /* Normal init script name */
378 strcpy(stpcpy(r, name), ".service");
383 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
385 /* We silently ignore the $ prefix here. According to the LSB
386 * spec it simply indicates whether something is a
387 * standardized name or a distribution-specific one. Since we
388 * just follow what already exists and do not introduce new
389 * uses or names we don't care who introduced a new name. */
391 static const char * const table[] = {
392 /* LSB defined facilities */
394 "network", SPECIAL_NETWORK_TARGET,
395 "named", SPECIAL_NSS_LOOKUP_TARGET,
396 "portmap", SPECIAL_RPCBIND_TARGET,
397 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
399 "time", SPECIAL_TIME_SYNC_TARGET,
409 n = *name == '$' ? name + 1 : name;
411 for (i = 0; i < ELEMENTSOF(table); i += 2) {
413 if (!streq(table[i], n))
419 r = strdup(table[i+1]);
426 /* If we don't know this name, fallback heuristics to figure
427 * out whether something is a target or a service alias. */
430 if (!unit_prefix_is_valid(n))
433 /* Facilities starting with $ are most likely targets */
434 r = unit_name_build(n, NULL, ".target");
435 } else if (filename && streq(name, filename))
436 /* Names equaling the file name of the services are redundant */
439 /* Everything else we assume to be normal service names */
440 r = sysv_translate_name(n);
451 static int sysv_fix_order(Service *s) {
457 if (s->sysv_start_priority < 0)
460 /* For each pair of services where at least one lacks a LSB
461 * header, we use the start priority value to order things. */
463 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
466 bool special_s, special_t;
473 if (UNIT(t)->load_state != UNIT_LOADED)
476 if (t->sysv_start_priority < 0)
479 /* If both units have modern headers we don't care
480 * about the priorities */
481 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
482 (UNIT(t)->fragment_path || t->sysv_has_lsb))
485 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
486 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
488 if (special_t && !special_s)
490 else if (special_s && !special_t)
492 else if (t->sysv_start_priority < s->sysv_start_priority)
494 else if (t->sysv_start_priority > s->sysv_start_priority)
499 /* FIXME: Maybe we should compare the name here lexicographically? */
501 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
508 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
511 if (!(c = new0(ExecCommand, 1)))
514 if (!(c->path = strdup(path))) {
519 if (!(c->argv = strv_new(path, arg1, NULL))) {
528 static int sysv_exec_commands(Service *s, const bool supports_reload) {
533 assert(UNIT(s)->source_path);
535 c = exec_command_new(UNIT(s)->source_path, "start");
538 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
540 c = exec_command_new(UNIT(s)->source_path, "stop");
543 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
545 if (supports_reload) {
546 c = exec_command_new(UNIT(s)->source_path, "reload");
549 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
555 static bool usage_contains_reload(const char *line) {
556 return (strcasestr(line, "{reload|") ||
557 strcasestr(line, "{reload}") ||
558 strcasestr(line, "{reload\"") ||
559 strcasestr(line, "|reload|") ||
560 strcasestr(line, "|reload}") ||
561 strcasestr(line, "|reload\""));
564 static int service_load_sysv_path(Service *s, const char *path) {
576 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
578 bool supports_reload = false;
585 f = fopen(path, "re");
587 r = errno == ENOENT ? 0 : -errno;
591 if (fstat(fileno(f), &st) < 0) {
596 free(u->source_path);
597 u->source_path = strdup(path);
598 if (!u->source_path) {
602 u->source_mtime = timespec_load(&st.st_mtim);
604 if (null_or_empty(&st)) {
605 u->load_state = UNIT_MASKED;
613 char l[LINE_MAX], *t;
615 if (!fgets(l, sizeof(l), f)) {
620 log_error_unit(u->id,
621 "Failed to read configuration file '%s': %s",
630 /* Try to figure out whether this init script supports
631 * the reload operation. This heuristic looks for
632 * "Usage" lines which include the reload option. */
633 if ( state == USAGE_CONTINUATION ||
634 (state == NORMAL && strcasestr(t, "usage"))) {
635 if (usage_contains_reload(t)) {
636 supports_reload = true;
638 } else if (t[strlen(t)-1] == '\\')
639 state = USAGE_CONTINUATION;
647 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
649 s->sysv_has_lsb = true;
653 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
659 t += strspn(t, WHITESPACE);
661 if (state == NORMAL) {
663 /* Try to parse Red Hat style chkconfig headers */
665 if (startswith_no_case(t, "chkconfig:")) {
667 char runlevels[16], *k;
671 if (sscanf(t+10, "%15s %i %*i",
673 &start_priority) != 2) {
675 log_warning_unit(u->id,
676 "[%s:%u] Failed to parse chkconfig line. Ignoring.",
681 /* A start priority gathered from the
682 * symlink farms is preferred over the
683 * data from the LSB header. */
684 if (start_priority < 0 || start_priority > 99)
685 log_warning_unit(u->id,
686 "[%s:%u] Start priority out of range. Ignoring.",
689 s->sysv_start_priority = start_priority;
691 char_array_0(runlevels);
692 k = delete_chars(runlevels, WHITESPACE "-");
697 if (!(d = strdup(k))) {
702 free(s->sysv_runlevels);
703 s->sysv_runlevels = d;
706 } else if (startswith_no_case(t, "description:")) {
708 size_t k = strlen(t);
712 if (t[k-1] == '\\') {
717 if ((j = strstrip(t+12)) && *j) {
718 if (!(d = strdup(j))) {
725 free(chkconfig_description);
726 chkconfig_description = d;
728 } else if (startswith_no_case(t, "pidfile:")) {
735 if (!path_is_absolute(fn)) {
736 log_warning_unit(u->id,
737 "[%s:%u] PID file not absolute. Ignoring.",
742 if (!(fn = strdup(fn))) {
751 } else if (state == DESCRIPTION) {
753 /* Try to parse Red Hat style description
756 size_t k = strlen(t);
764 if ((j = strstrip(t)) && *j) {
767 if (chkconfig_description)
768 d = strjoin(chkconfig_description, " ", j, NULL);
777 free(chkconfig_description);
778 chkconfig_description = d;
781 } else if (state == LSB || state == LSB_DESCRIPTION) {
783 if (startswith_no_case(t, "Provides:")) {
789 FOREACH_WORD_QUOTED(w, z, t+9, i) {
792 if (!(n = strndup(w, z))) {
797 r = sysv_translate_facility(n, path_get_file_name(path), &m);
806 if (unit_name_to_type(m) == UNIT_SERVICE)
807 r = unit_merge_by_name(u, m);
814 * indication that the
816 * now available. This
819 * targets do NOT pull
822 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
825 log_error_unit(u->id,
826 "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
827 path, line, m, strerror(-r));
832 } else if (startswith_no_case(t, "Required-Start:") ||
833 startswith_no_case(t, "Should-Start:") ||
834 startswith_no_case(t, "X-Start-Before:") ||
835 startswith_no_case(t, "X-Start-After:")) {
841 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
844 if (!(n = strndup(w, z))) {
849 r = sysv_translate_facility(n, path_get_file_name(path), &m);
851 log_error_unit(u->id,
852 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
853 path, line, n, strerror(-r));
863 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
866 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
867 path, line, m, strerror(-r));
871 } else if (startswith_no_case(t, "Default-Start:")) {
876 k = delete_chars(t+14, WHITESPACE "-");
879 if (!(d = strdup(k))) {
884 free(s->sysv_runlevels);
885 s->sysv_runlevels = d;
888 } else if (startswith_no_case(t, "Description:")) {
891 state = LSB_DESCRIPTION;
893 if ((j = strstrip(t+12)) && *j) {
894 if (!(d = strdup(j))) {
901 free(long_description);
902 long_description = d;
904 } else if (startswith_no_case(t, "Short-Description:")) {
909 if ((j = strstrip(t+18)) && *j) {
910 if (!(d = strdup(j))) {
917 free(short_description);
918 short_description = d;
920 } else if (state == LSB_DESCRIPTION) {
922 if (startswith(l, "#\t") || startswith(l, "# ")) {
925 if ((j = strstrip(t)) && *j) {
928 if (long_description)
929 d = strjoin(long_description, " ", t, NULL);
938 free(long_description);
939 long_description = d;
948 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
951 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
952 /* If there a runlevels configured for this service
953 * but none of the standard ones, then we assume this
954 * is some special kind of service (which might be
955 * needed for early boot) and don't create any links
958 UNIT(s)->default_dependencies = false;
960 /* Don't timeout special services during boot (like fsck) */
961 s->timeout_start_usec = 0;
962 s->timeout_stop_usec = 0;
964 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
965 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
968 /* Special setting for all SysV services */
969 s->type = SERVICE_FORKING;
970 s->remain_after_exit = !s->pid_file;
971 s->guess_main_pid = false;
972 s->restart = SERVICE_RESTART_NO;
973 s->exec_context.ignore_sigpipe = false;
974 s->kill_context.kill_mode = KILL_PROCESS;
976 /* We use the long description only if
977 * no short description is set. */
979 if (short_description)
980 description = short_description;
981 else if (chkconfig_description)
982 description = chkconfig_description;
983 else if (long_description)
984 description = long_description;
991 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
999 /* The priority that has been set in /etc/rcN.d/ hierarchies
1000 * takes precedence over what is stored as default in the LSB
1002 if (s->sysv_start_priority_from_rcnd >= 0)
1003 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
1005 u->load_state = UNIT_LOADED;
1013 free(short_description);
1014 free(long_description);
1015 free(chkconfig_description);
1020 static int service_load_sysv_name(Service *s, const char *name) {
1026 /* For SysV services we strip the *.sh suffixes. */
1027 if (endswith(name, ".sh.service"))
1030 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1034 path = strjoin(*p, "/", name, NULL);
1038 assert(endswith(path, ".service"));
1039 path[strlen(path)-8] = 0;
1041 r = service_load_sysv_path(s, path);
1043 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1044 /* Try *.sh source'able init scripts */
1045 strcat(path, ".sh");
1046 r = service_load_sysv_path(s, path);
1053 if (UNIT(s)->load_state != UNIT_STUB)
1060 static int service_load_sysv(Service *s) {
1067 /* Load service data from SysV init scripts, preferably with
1068 * LSB headers ... */
1070 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1073 if ((t = UNIT(s)->id))
1074 if ((r = service_load_sysv_name(s, t)) < 0)
1077 if (UNIT(s)->load_state == UNIT_STUB)
1078 SET_FOREACH(t, UNIT(s)->names, i) {
1079 if (t == UNIT(s)->id)
1082 if ((r = service_load_sysv_name(s, t)) < 0)
1085 if (UNIT(s)->load_state != UNIT_STUB)
1093 static int service_verify(Service *s) {
1096 if (UNIT(s)->load_state != UNIT_LOADED)
1099 if (!s->exec_command[SERVICE_EXEC_START]) {
1100 log_error_unit(UNIT(s)->id,
1101 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1105 if (s->type != SERVICE_ONESHOT &&
1106 s->exec_command[SERVICE_EXEC_START]->command_next) {
1107 log_error_unit(UNIT(s)->id,
1108 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1112 if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
1113 log_error_unit(UNIT(s)->id,
1114 "%s has Restart setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1118 if (s->type == SERVICE_DBUS && !s->bus_name) {
1119 log_error_unit(UNIT(s)->id,
1120 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1124 if (s->bus_name && s->type != SERVICE_DBUS)
1125 log_warning_unit(UNIT(s)->id,
1126 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1128 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1129 log_error_unit(UNIT(s)->id,
1130 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1137 static int service_add_default_dependencies(Service *s) {
1142 /* Add a number of automatic dependencies useful for the
1143 * majority of services. */
1145 /* First, pull in base system */
1146 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1147 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1148 SPECIAL_BASIC_TARGET, NULL, true);
1152 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1153 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1154 SPECIAL_SOCKETS_TARGET, NULL, true);
1158 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1159 SPECIAL_TIMERS_TARGET, NULL, true);
1163 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1164 SPECIAL_PATHS_TARGET, NULL, true);
1169 /* Second, activate normal shutdown */
1170 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
1171 SPECIAL_SHUTDOWN_TARGET, NULL, true);
1175 static void service_fix_output(Service *s) {
1178 /* If nothing has been explicitly configured, patch default
1179 * output in. If input is socket/tty we avoid this however,
1180 * since in that case we want output to default to the same
1181 * place as we read input from. */
1183 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1184 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1185 s->exec_context.std_input == EXEC_INPUT_NULL)
1186 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1188 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1189 s->exec_context.std_input == EXEC_INPUT_NULL)
1190 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1193 static int service_load(Unit *u) {
1195 Service *s = SERVICE(u);
1199 /* Load a .service file */
1200 r = unit_load_fragment(u);
1204 #ifdef HAVE_SYSV_COMPAT
1205 /* Load a classic init script as a fallback, if we couldn't find anything */
1206 if (u->load_state == UNIT_STUB) {
1207 r = service_load_sysv(s);
1213 /* Still nothing found? Then let's give up */
1214 if (u->load_state == UNIT_STUB)
1217 /* This is a new unit? Then let's add in some extras */
1218 if (u->load_state == UNIT_LOADED) {
1220 /* We were able to load something, then let's add in
1221 * the dropin directories. */
1222 r = unit_load_dropin(u);
1226 if (s->type == _SERVICE_TYPE_INVALID)
1227 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1229 /* Oneshot services have disabled start timeout by default */
1230 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1231 s->timeout_start_usec = 0;
1233 service_fix_output(s);
1235 r = unit_add_exec_dependencies(u, &s->exec_context);
1239 r = unit_add_default_slice(u);
1243 #ifdef HAVE_SYSV_COMPAT
1244 r = sysv_fix_order(s);
1250 r = unit_watch_bus_name(u, s->bus_name);
1255 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1256 s->notify_access = NOTIFY_MAIN;
1258 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1259 s->notify_access = NOTIFY_MAIN;
1261 if (s->type == SERVICE_DBUS || s->bus_name) {
1262 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES,
1263 SPECIAL_DBUS_SOCKET, NULL, true);
1268 if (UNIT(s)->default_dependencies) {
1269 r = service_add_default_dependencies(s);
1274 r = unit_exec_context_defaults(u, &s->exec_context);
1279 return service_verify(s);
1282 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1284 ServiceExecCommand c;
1285 Service *s = SERVICE(u);
1286 const char *prefix2;
1287 _cleanup_free_ char *p2 = NULL;
1291 p2 = strappend(prefix, "\t");
1292 prefix2 = p2 ? p2 : prefix;
1295 "%sService State: %s\n"
1297 "%sReload Result: %s\n"
1298 "%sPermissionsStartOnly: %s\n"
1299 "%sRootDirectoryStartOnly: %s\n"
1300 "%sRemainAfterExit: %s\n"
1301 "%sGuessMainPID: %s\n"
1304 "%sNotifyAccess: %s\n",
1305 prefix, service_state_to_string(s->state),
1306 prefix, service_result_to_string(s->result),
1307 prefix, service_result_to_string(s->reload_result),
1308 prefix, yes_no(s->permissions_start_only),
1309 prefix, yes_no(s->root_directory_start_only),
1310 prefix, yes_no(s->remain_after_exit),
1311 prefix, yes_no(s->guess_main_pid),
1312 prefix, service_type_to_string(s->type),
1313 prefix, service_restart_to_string(s->restart),
1314 prefix, notify_access_to_string(s->notify_access));
1316 if (s->control_pid > 0)
1318 "%sControl PID: %lu\n",
1319 prefix, (unsigned long) s->control_pid);
1321 if (s->main_pid > 0)
1324 "%sMain PID Known: %s\n"
1325 "%sMain PID Alien: %s\n",
1326 prefix, (unsigned long) s->main_pid,
1327 prefix, yes_no(s->main_pid_known),
1328 prefix, yes_no(s->main_pid_alien));
1333 prefix, s->pid_file);
1338 "%sBus Name Good: %s\n",
1339 prefix, s->bus_name,
1340 prefix, yes_no(s->bus_name_good));
1342 kill_context_dump(&s->kill_context, f, prefix);
1343 exec_context_dump(&s->exec_context, f, prefix);
1345 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1347 if (!s->exec_command[c])
1350 fprintf(f, "%s-> %s:\n",
1351 prefix, service_exec_command_to_string(c));
1353 exec_command_dump_list(s->exec_command[c], f, prefix2);
1356 #ifdef HAVE_SYSV_COMPAT
1359 "%sSysV Init Script has LSB Header: %s\n"
1360 "%sSysVEnabled: %s\n",
1361 prefix, yes_no(s->sysv_has_lsb),
1362 prefix, yes_no(s->sysv_enabled));
1364 if (s->sysv_start_priority >= 0)
1366 "%sSysVStartPriority: %i\n",
1367 prefix, s->sysv_start_priority);
1369 if (s->sysv_runlevels)
1370 fprintf(f, "%sSysVRunLevels: %s\n",
1371 prefix, s->sysv_runlevels);
1375 fprintf(f, "%sStatus Text: %s\n",
1376 prefix, s->status_text);
1379 static int service_load_pid_file(Service *s, bool may_warn) {
1380 _cleanup_free_ char *k = NULL;
1389 r = read_one_line_file(s->pid_file, &k);
1392 log_info_unit(UNIT(s)->id,
1393 "PID file %s not readable (yet?) after %s.",
1394 s->pid_file, service_state_to_string(s->state));
1398 r = parse_pid(k, &pid);
1401 log_info_unit(UNIT(s)->id,
1402 "Failed to read PID from file %s: %s",
1403 s->pid_file, strerror(-r));
1407 if (kill(pid, 0) < 0 && errno != EPERM) {
1409 log_info_unit(UNIT(s)->id,
1410 "PID %lu read from file %s does not exist.",
1411 (unsigned long) pid, s->pid_file);
1415 if (s->main_pid_known) {
1416 if (pid == s->main_pid)
1419 log_debug_unit(UNIT(s)->id,
1420 "Main PID changing: %lu -> %lu",
1421 (unsigned long) s->main_pid, (unsigned long) pid);
1422 service_unwatch_main_pid(s);
1423 s->main_pid_known = false;
1425 log_debug_unit(UNIT(s)->id,
1426 "Main PID loaded: %lu", (unsigned long) pid);
1428 r = service_set_main_pid(s, pid);
1432 r = unit_watch_pid(UNIT(s), pid);
1434 /* FIXME: we need to do something here */
1435 log_warning_unit(UNIT(s)->id,
1436 "Failed to watch PID %lu from service %s",
1437 (unsigned long) pid, UNIT(s)->id);
1444 static int service_search_main_pid(Service *s) {
1450 /* If we know it anyway, don't ever fallback to unreliable
1452 if (s->main_pid_known)
1455 if (!s->guess_main_pid)
1458 assert(s->main_pid <= 0);
1460 pid = unit_search_main_pid(UNIT(s));
1464 log_debug_unit(UNIT(s)->id,
1465 "Main PID guessed: %lu", (unsigned long) pid);
1466 r = service_set_main_pid(s, pid);
1470 r = unit_watch_pid(UNIT(s), pid);
1472 /* FIXME: we need to do something here */
1473 log_warning_unit(UNIT(s)->id,
1474 "Failed to watch PID %lu from service %s",
1475 (unsigned long) pid, UNIT(s)->id);
1481 static void service_set_state(Service *s, ServiceState state) {
1482 ServiceState old_state;
1483 const UnitActiveState *table;
1486 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1488 old_state = s->state;
1491 service_unwatch_pid_file(s);
1493 if (state != SERVICE_START_PRE &&
1494 state != SERVICE_START &&
1495 state != SERVICE_START_POST &&
1496 state != SERVICE_RELOAD &&
1497 state != SERVICE_STOP &&
1498 state != SERVICE_STOP_SIGTERM &&
1499 state != SERVICE_STOP_SIGKILL &&
1500 state != SERVICE_STOP_POST &&
1501 state != SERVICE_FINAL_SIGTERM &&
1502 state != SERVICE_FINAL_SIGKILL &&
1503 state != SERVICE_AUTO_RESTART)
1504 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1506 if (state != SERVICE_START &&
1507 state != SERVICE_START_POST &&
1508 state != SERVICE_RUNNING &&
1509 state != SERVICE_RELOAD &&
1510 state != SERVICE_STOP &&
1511 state != SERVICE_STOP_SIGTERM &&
1512 state != SERVICE_STOP_SIGKILL) {
1513 service_unwatch_main_pid(s);
1514 s->main_command = NULL;
1517 if (state != SERVICE_START_PRE &&
1518 state != SERVICE_START &&
1519 state != SERVICE_START_POST &&
1520 state != SERVICE_RELOAD &&
1521 state != SERVICE_STOP &&
1522 state != SERVICE_STOP_SIGTERM &&
1523 state != SERVICE_STOP_SIGKILL &&
1524 state != SERVICE_STOP_POST &&
1525 state != SERVICE_FINAL_SIGTERM &&
1526 state != SERVICE_FINAL_SIGKILL) {
1527 service_unwatch_control_pid(s);
1528 s->control_command = NULL;
1529 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1532 if (state != SERVICE_START_PRE &&
1533 state != SERVICE_START &&
1534 state != SERVICE_START_POST &&
1535 state != SERVICE_RUNNING &&
1536 state != SERVICE_RELOAD &&
1537 state != SERVICE_STOP &&
1538 state != SERVICE_STOP_SIGTERM &&
1539 state != SERVICE_STOP_SIGKILL &&
1540 state != SERVICE_STOP_POST &&
1541 state != SERVICE_FINAL_SIGTERM &&
1542 state != SERVICE_FINAL_SIGKILL &&
1543 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1544 service_close_socket_fd(s);
1545 service_connection_unref(s);
1548 if (state == SERVICE_STOP || state == SERVICE_STOP_SIGTERM)
1549 service_stop_watchdog(s);
1551 /* For the inactive states unit_notify() will trim the cgroup,
1552 * but for exit we have to do that ourselves... */
1553 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1554 unit_destroy_cgroup(UNIT(s));
1556 /* For remain_after_exit services, let's see if we can "release" the
1557 * hold on the console, since unit_notify() only does that in case of
1558 * change of state */
1559 if (state == SERVICE_EXITED && s->remain_after_exit &&
1560 UNIT(s)->manager->n_on_console > 0) {
1561 ExecContext *ec = unit_get_exec_context(UNIT(s));
1562 if (ec && exec_context_may_touch_console(ec)) {
1563 Manager *m = UNIT(s)->manager;
1566 if (m->n_on_console == 0)
1567 /* unset no_console_output flag, since the console is free */
1568 m->no_console_output = false;
1572 if (old_state != state)
1573 log_debug_unit(UNIT(s)->id,
1574 "%s changed %s -> %s", UNIT(s)->id,
1575 service_state_to_string(old_state),
1576 service_state_to_string(state));
1578 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1579 s->reload_result = SERVICE_SUCCESS;
1582 static int service_coldplug(Unit *u) {
1583 Service *s = SERVICE(u);
1587 assert(s->state == SERVICE_DEAD);
1589 if (s->deserialized_state != s->state) {
1591 if (s->deserialized_state == SERVICE_START_PRE ||
1592 s->deserialized_state == SERVICE_START ||
1593 s->deserialized_state == SERVICE_START_POST ||
1594 s->deserialized_state == SERVICE_RELOAD ||
1595 s->deserialized_state == SERVICE_STOP ||
1596 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1597 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1598 s->deserialized_state == SERVICE_STOP_POST ||
1599 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1600 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1601 s->deserialized_state == SERVICE_AUTO_RESTART) {
1603 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1605 r = service_arm_timer(s,
1606 s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec :
1607 s->deserialized_state == SERVICE_START_PRE || s->deserialized_state == SERVICE_START ||
1608 s->deserialized_state == SERVICE_START_POST || s->deserialized_state == SERVICE_RELOAD ? s->timeout_start_usec :
1609 s->timeout_stop_usec);
1615 if ((s->deserialized_state == SERVICE_START &&
1616 (s->type == SERVICE_FORKING ||
1617 s->type == SERVICE_DBUS ||
1618 s->type == SERVICE_ONESHOT ||
1619 s->type == SERVICE_NOTIFY)) ||
1620 s->deserialized_state == SERVICE_START_POST ||
1621 s->deserialized_state == SERVICE_RUNNING ||
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 if (s->main_pid > 0) {
1627 r = unit_watch_pid(UNIT(s), s->main_pid);
1632 if (s->deserialized_state == SERVICE_START_PRE ||
1633 s->deserialized_state == SERVICE_START ||
1634 s->deserialized_state == SERVICE_START_POST ||
1635 s->deserialized_state == SERVICE_RELOAD ||
1636 s->deserialized_state == SERVICE_STOP ||
1637 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1638 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1639 s->deserialized_state == SERVICE_STOP_POST ||
1640 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1641 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1642 if (s->control_pid > 0) {
1643 r = unit_watch_pid(UNIT(s), s->control_pid);
1648 if (s->deserialized_state == SERVICE_START_POST ||
1649 s->deserialized_state == SERVICE_RUNNING)
1650 service_handle_watchdog(s);
1652 service_set_state(s, s->deserialized_state);
1657 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1661 unsigned rn_fds = 0;
1668 if (s->socket_fd >= 0)
1671 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1676 if (u->type != UNIT_SOCKET)
1681 r = socket_collect_fds(sock, &cfds, &cn_fds);
1694 t = new(int, rn_fds+cn_fds);
1701 memcpy(t, rfds, rn_fds * sizeof(int));
1702 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1707 rn_fds = rn_fds+cn_fds;
1722 static int service_spawn(
1727 bool apply_permissions,
1729 bool apply_tty_stdin,
1730 bool set_notify_socket,
1737 _cleanup_free_ int *fdsbuf = NULL;
1738 unsigned n_fds = 0, n_env = 0;
1739 _cleanup_strv_free_ char
1740 **argv = NULL, **final_env = NULL, **our_env = NULL;
1747 unit_realize_cgroup(UNIT(s));
1750 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1751 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1752 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1754 if (s->socket_fd >= 0) {
1755 fds = &s->socket_fd;
1758 r = service_collect_fds(s, &fdsbuf, &n_fds);
1766 r = service_arm_timer(s, timeout ? s->timeout_start_usec : 0);
1770 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
1774 our_env = new0(char*, 5);
1780 if (set_notify_socket)
1781 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1786 if (s->main_pid > 0)
1787 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1792 if (s->watchdog_usec > 0)
1793 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1798 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
1799 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1804 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1810 if (is_control && UNIT(s)->cgroup_path) {
1811 path = strappenda(UNIT(s)->cgroup_path, "/control");
1812 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1814 path = UNIT(s)->cgroup_path;
1824 UNIT(s)->manager->confirm_spawn,
1825 UNIT(s)->manager->cgroup_supported,
1828 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1833 r = unit_watch_pid(UNIT(s), pid);
1835 /* FIXME: we need to do something here */
1844 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1849 static int main_pid_good(Service *s) {
1852 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1855 /* If we know the pid file, then lets just check if it is
1857 if (s->main_pid_known) {
1859 /* If it's an alien child let's check if it is still
1861 if (s->main_pid_alien && s->main_pid > 0)
1862 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1864 /* .. otherwise assume we'll get a SIGCHLD for it,
1865 * which we really should wait for to collect exit
1866 * status and code */
1867 return s->main_pid > 0;
1870 /* We don't know the pid */
1874 _pure_ static int control_pid_good(Service *s) {
1877 return s->control_pid > 0;
1880 static int cgroup_good(Service *s) {
1885 if (!UNIT(s)->cgroup_path)
1888 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1895 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1899 if (f != SERVICE_SUCCESS)
1902 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1904 if (allow_restart &&
1905 !s->forbid_restart &&
1906 (s->restart == SERVICE_RESTART_ALWAYS ||
1907 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1908 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1909 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1910 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1911 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1912 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1913 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1914 (s->result != SERVICE_FAILURE_SIGNAL ||
1915 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))) {
1917 r = service_arm_timer(s, s->restart_usec);
1921 service_set_state(s, SERVICE_AUTO_RESTART);
1924 s->forbid_restart = false;
1926 /* we want fresh tmpdirs in case service is started again immediately */
1927 exec_context_tmp_dirs_done(&s->exec_context);
1929 /* Try to delete the pid file. At this point it will be
1930 * out-of-date, and some software might be confused by it, so
1931 * let's remove it. */
1933 unlink_noerrno(s->pid_file);
1938 log_warning_unit(UNIT(s)->id,
1939 "%s failed to run install restart timer: %s",
1940 UNIT(s)->id, strerror(-r));
1941 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1944 static void service_enter_stop_post(Service *s, ServiceResult f) {
1948 if (f != SERVICE_SUCCESS)
1951 service_unwatch_control_pid(s);
1953 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1954 if (s->control_command) {
1955 s->control_command_id = SERVICE_EXEC_STOP_POST;
1957 r = service_spawn(s,
1961 !s->permissions_start_only,
1962 !s->root_directory_start_only,
1971 service_set_state(s, SERVICE_STOP_POST);
1973 service_enter_dead(s, SERVICE_SUCCESS, true);
1978 log_warning_unit(UNIT(s)->id,
1979 "%s failed to run 'stop-post' task: %s",
1980 UNIT(s)->id, strerror(-r));
1981 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1984 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1989 if (f != SERVICE_SUCCESS)
1992 r = unit_kill_context(
1995 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
2003 if (s->timeout_stop_usec > 0) {
2004 r = service_arm_timer(s, s->timeout_stop_usec);
2009 service_set_state(s, state);
2010 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2011 service_enter_stop_post(s, SERVICE_SUCCESS);
2013 service_enter_dead(s, SERVICE_SUCCESS, true);
2018 log_warning_unit(UNIT(s)->id,
2019 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2021 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2022 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2024 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2027 static void service_enter_stop(Service *s, ServiceResult f) {
2032 if (f != SERVICE_SUCCESS)
2035 service_unwatch_control_pid(s);
2037 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
2038 if (s->control_command) {
2039 s->control_command_id = SERVICE_EXEC_STOP;
2041 r = service_spawn(s,
2045 !s->permissions_start_only,
2046 !s->root_directory_start_only,
2054 service_set_state(s, SERVICE_STOP);
2056 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2061 log_warning_unit(UNIT(s)->id,
2062 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2063 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2066 static void service_enter_running(Service *s, ServiceResult f) {
2067 int main_pid_ok, cgroup_ok;
2070 if (f != SERVICE_SUCCESS)
2073 main_pid_ok = main_pid_good(s);
2074 cgroup_ok = cgroup_good(s);
2076 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2077 (s->bus_name_good || s->type != SERVICE_DBUS))
2078 service_set_state(s, SERVICE_RUNNING);
2079 else if (s->remain_after_exit)
2080 service_set_state(s, SERVICE_EXITED);
2082 service_enter_stop(s, SERVICE_SUCCESS);
2085 static void service_enter_start_post(Service *s) {
2089 service_unwatch_control_pid(s);
2091 if (s->watchdog_usec > 0)
2092 service_reset_watchdog(s);
2094 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
2095 if (s->control_command) {
2096 s->control_command_id = SERVICE_EXEC_START_POST;
2098 r = service_spawn(s,
2102 !s->permissions_start_only,
2103 !s->root_directory_start_only,
2111 service_set_state(s, SERVICE_START_POST);
2113 service_enter_running(s, SERVICE_SUCCESS);
2118 log_warning_unit(UNIT(s)->id,
2119 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2120 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2123 static void service_kill_control_processes(Service *s) {
2126 if (!UNIT(s)->cgroup_path)
2129 p = strappenda(UNIT(s)->cgroup_path, "/control");
2130 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
2133 static void service_enter_start(Service *s) {
2140 assert(s->exec_command[SERVICE_EXEC_START]);
2141 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2143 service_unwatch_control_pid(s);
2144 service_unwatch_main_pid(s);
2146 /* We want to ensure that nobody leaks processes from
2147 * START_PRE here, so let's go on a killing spree, People
2148 * should not spawn long running processes from START_PRE. */
2149 service_kill_control_processes(s);
2151 if (s->type == SERVICE_FORKING) {
2152 s->control_command_id = SERVICE_EXEC_START;
2153 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2155 s->main_command = NULL;
2157 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2158 s->control_command = NULL;
2160 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2163 r = service_spawn(s,
2165 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
2166 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2171 s->notify_access != NOTIFY_NONE,
2177 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2178 /* For simple services we immediately start
2179 * the START_POST binaries. */
2181 service_set_main_pid(s, pid);
2182 service_enter_start_post(s);
2184 } else if (s->type == SERVICE_FORKING) {
2186 /* For forking services we wait until the start
2187 * process exited. */
2189 s->control_pid = pid;
2190 service_set_state(s, SERVICE_START);
2192 } else if (s->type == SERVICE_ONESHOT ||
2193 s->type == SERVICE_DBUS ||
2194 s->type == SERVICE_NOTIFY) {
2196 /* For oneshot services we wait until the start
2197 * process exited, too, but it is our main process. */
2199 /* For D-Bus services we know the main pid right away,
2200 * but wait for the bus name to appear on the
2201 * bus. Notify services are similar. */
2203 service_set_main_pid(s, pid);
2204 service_set_state(s, SERVICE_START);
2206 assert_not_reached("Unknown service type");
2211 log_warning_unit(UNIT(s)->id,
2212 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2213 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2216 static void service_enter_start_pre(Service *s) {
2221 service_unwatch_control_pid(s);
2223 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
2224 if (s->control_command) {
2225 /* Before we start anything, let's clear up what might
2226 * be left from previous runs. */
2227 service_kill_control_processes(s);
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) {
2257 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2262 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2263 /* Don't restart things if we are going down anyway */
2264 log_info_unit(UNIT(s)->id,
2265 "Stop job pending for unit, delaying automatic restart.");
2267 r = service_arm_timer(s, s->restart_usec);
2274 /* Any units that are bound to this service must also be
2275 * restarted. We use JOB_RESTART (instead of the more obvious
2276 * JOB_START) here so that those dependency jobs will be added
2278 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2282 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2283 * it will be canceled as part of the service_stop() call that
2284 * is executed as part of JOB_RESTART. */
2286 log_debug_unit(UNIT(s)->id,
2287 "%s scheduled restart job.", UNIT(s)->id);
2291 log_warning_unit(UNIT(s)->id,
2292 "%s failed to schedule restart job: %s",
2293 UNIT(s)->id, bus_error_message(&error, -r));
2294 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2297 static void service_enter_reload(Service *s) {
2302 service_unwatch_control_pid(s);
2304 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
2305 if (s->control_command) {
2306 s->control_command_id = SERVICE_EXEC_RELOAD;
2308 r = service_spawn(s,
2312 !s->permissions_start_only,
2313 !s->root_directory_start_only,
2321 service_set_state(s, SERVICE_RELOAD);
2323 service_enter_running(s, SERVICE_SUCCESS);
2328 log_warning_unit(UNIT(s)->id,
2329 "%s failed to run 'reload' task: %s",
2330 UNIT(s)->id, strerror(-r));
2331 s->reload_result = SERVICE_FAILURE_RESOURCES;
2332 service_enter_running(s, SERVICE_SUCCESS);
2335 static void service_run_next_control(Service *s) {
2339 assert(s->control_command);
2340 assert(s->control_command->command_next);
2342 assert(s->control_command_id != SERVICE_EXEC_START);
2344 s->control_command = s->control_command->command_next;
2345 service_unwatch_control_pid(s);
2347 r = service_spawn(s,
2351 !s->permissions_start_only,
2352 !s->root_directory_start_only,
2353 s->control_command_id == SERVICE_EXEC_START_PRE ||
2354 s->control_command_id == SERVICE_EXEC_STOP_POST,
2364 log_warning_unit(UNIT(s)->id,
2365 "%s failed to run next control task: %s",
2366 UNIT(s)->id, strerror(-r));
2368 if (s->state == SERVICE_START_PRE)
2369 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2370 else if (s->state == SERVICE_STOP)
2371 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2372 else if (s->state == SERVICE_STOP_POST)
2373 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2374 else if (s->state == SERVICE_RELOAD) {
2375 s->reload_result = SERVICE_FAILURE_RESOURCES;
2376 service_enter_running(s, SERVICE_SUCCESS);
2378 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2381 static void service_run_next_main(Service *s) {
2386 assert(s->main_command);
2387 assert(s->main_command->command_next);
2388 assert(s->type == SERVICE_ONESHOT);
2390 s->main_command = s->main_command->command_next;
2391 service_unwatch_main_pid(s);
2393 r = service_spawn(s,
2400 s->notify_access != NOTIFY_NONE,
2406 service_set_main_pid(s, pid);
2411 log_warning_unit(UNIT(s)->id,
2412 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2413 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2416 static int service_start_limit_test(Service *s) {
2419 if (ratelimit_test(&s->start_limit))
2422 switch (s->start_limit_action) {
2424 case SERVICE_START_LIMIT_NONE:
2425 log_warning_unit(UNIT(s)->id,
2426 "%s start request repeated too quickly, refusing to start.",
2430 case SERVICE_START_LIMIT_REBOOT: {
2431 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2434 log_warning_unit(UNIT(s)->id,
2435 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2437 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
2438 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
2439 true, &error, NULL);
2441 log_error_unit(UNIT(s)->id,
2442 "Failed to reboot: %s.", bus_error_message(&error, r));
2447 case SERVICE_START_LIMIT_REBOOT_FORCE:
2448 log_warning_unit(UNIT(s)->id,
2449 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2450 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2453 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2454 log_warning_unit(UNIT(s)->id,
2455 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2457 reboot(RB_AUTOBOOT);
2461 log_error_unit(UNIT(s)->id,
2462 "start limit action=%i", s->start_limit_action);
2463 assert_not_reached("Unknown StartLimitAction.");
2469 static int service_start(Unit *u) {
2470 Service *s = SERVICE(u);
2475 /* We cannot fulfill this request right now, try again later
2477 if (s->state == SERVICE_STOP ||
2478 s->state == SERVICE_STOP_SIGTERM ||
2479 s->state == SERVICE_STOP_SIGKILL ||
2480 s->state == SERVICE_STOP_POST ||
2481 s->state == SERVICE_FINAL_SIGTERM ||
2482 s->state == SERVICE_FINAL_SIGKILL)
2485 /* Already on it! */
2486 if (s->state == SERVICE_START_PRE ||
2487 s->state == SERVICE_START ||
2488 s->state == SERVICE_START_POST)
2491 /* A service that will be restarted must be stopped first to
2492 * trigger BindsTo and/or OnFailure dependencies. If a user
2493 * does not want to wait for the holdoff time to elapse, the
2494 * service should be manually restarted, not started. We
2495 * simply return EAGAIN here, so that any start jobs stay
2496 * queued, and assume that the auto restart timer will
2497 * eventually trigger the restart. */
2498 if (s->state == SERVICE_AUTO_RESTART)
2501 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2503 /* Make sure we don't enter a busy loop of some kind. */
2504 r = service_start_limit_test(s);
2506 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2510 s->result = SERVICE_SUCCESS;
2511 s->reload_result = SERVICE_SUCCESS;
2512 s->main_pid_known = false;
2513 s->main_pid_alien = false;
2514 s->forbid_restart = false;
2516 service_enter_start_pre(s);
2520 static int service_stop(Unit *u) {
2521 Service *s = SERVICE(u);
2525 /* Don't create restart jobs from here. */
2526 s->forbid_restart = true;
2529 if (s->state == SERVICE_STOP ||
2530 s->state == SERVICE_STOP_SIGTERM ||
2531 s->state == SERVICE_STOP_SIGKILL ||
2532 s->state == SERVICE_STOP_POST ||
2533 s->state == SERVICE_FINAL_SIGTERM ||
2534 s->state == SERVICE_FINAL_SIGKILL)
2537 /* A restart will be scheduled or is in progress. */
2538 if (s->state == SERVICE_AUTO_RESTART) {
2539 service_set_state(s, SERVICE_DEAD);
2543 /* If there's already something running we go directly into
2545 if (s->state == SERVICE_START_PRE ||
2546 s->state == SERVICE_START ||
2547 s->state == SERVICE_START_POST ||
2548 s->state == SERVICE_RELOAD) {
2549 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2553 assert(s->state == SERVICE_RUNNING ||
2554 s->state == SERVICE_EXITED);
2556 service_enter_stop(s, SERVICE_SUCCESS);
2560 static int service_reload(Unit *u) {
2561 Service *s = SERVICE(u);
2565 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2567 service_enter_reload(s);
2571 _pure_ static bool service_can_reload(Unit *u) {
2572 Service *s = SERVICE(u);
2576 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2579 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2580 Service *s = SERVICE(u);
2586 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2587 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2588 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2590 if (s->control_pid > 0)
2591 unit_serialize_item_format(u, f, "control-pid", "%lu",
2592 (unsigned long) s->control_pid);
2594 if (s->main_pid_known && s->main_pid > 0)
2595 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2597 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2600 unit_serialize_item(u, f, "status-text", s->status_text);
2602 /* FIXME: There's a minor uncleanliness here: if there are
2603 * multiple commands attached here, we will start from the
2604 * first one again */
2605 if (s->control_command_id >= 0)
2606 unit_serialize_item(u, f, "control-command",
2607 service_exec_command_to_string(s->control_command_id));
2609 if (s->socket_fd >= 0) {
2612 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2615 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2618 if (s->main_exec_status.pid > 0) {
2619 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu",
2620 (unsigned long) s->main_exec_status.pid);
2621 dual_timestamp_serialize(f, "main-exec-status-start",
2622 &s->main_exec_status.start_timestamp);
2623 dual_timestamp_serialize(f, "main-exec-status-exit",
2624 &s->main_exec_status.exit_timestamp);
2626 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2627 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
2628 s->main_exec_status.code);
2629 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
2630 s->main_exec_status.status);
2633 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2634 dual_timestamp_serialize(f, "watchdog-timestamp",
2635 &s->watchdog_timestamp);
2637 if (s->exec_context.tmp_dir)
2638 unit_serialize_item(u, f, "tmp-dir", s->exec_context.tmp_dir);
2640 if (s->exec_context.var_tmp_dir)
2641 unit_serialize_item(u, f, "var-tmp-dir", s->exec_context.var_tmp_dir);
2643 if (s->forbid_restart)
2644 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
2649 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2650 Service *s = SERVICE(u);
2657 if (streq(key, "state")) {
2660 state = service_state_from_string(value);
2662 log_debug_unit(u->id, "Failed to parse state value %s", value);
2664 s->deserialized_state = state;
2665 } else if (streq(key, "result")) {
2668 f = service_result_from_string(value);
2670 log_debug_unit(u->id, "Failed to parse result value %s", value);
2671 else if (f != SERVICE_SUCCESS)
2674 } else if (streq(key, "reload-result")) {
2677 f = service_result_from_string(value);
2679 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2680 else if (f != SERVICE_SUCCESS)
2681 s->reload_result = f;
2683 } else if (streq(key, "control-pid")) {
2686 if (parse_pid(value, &pid) < 0)
2687 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2689 s->control_pid = pid;
2690 } else if (streq(key, "main-pid")) {
2693 if (parse_pid(value, &pid) < 0)
2694 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2696 service_set_main_pid(s, pid);
2697 unit_watch_pid(UNIT(s), pid);
2699 } else if (streq(key, "main-pid-known")) {
2702 b = parse_boolean(value);
2704 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2706 s->main_pid_known = b;
2707 } else if (streq(key, "status-text")) {
2714 free(s->status_text);
2718 } else if (streq(key, "control-command")) {
2719 ServiceExecCommand id;
2721 id = service_exec_command_from_string(value);
2723 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2725 s->control_command_id = id;
2726 s->control_command = s->exec_command[id];
2728 } else if (streq(key, "socket-fd")) {
2731 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2732 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2735 if (s->socket_fd >= 0)
2736 close_nointr_nofail(s->socket_fd);
2737 s->socket_fd = fdset_remove(fds, fd);
2739 } else if (streq(key, "main-exec-status-pid")) {
2742 if (parse_pid(value, &pid) < 0)
2743 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2745 s->main_exec_status.pid = pid;
2746 } else if (streq(key, "main-exec-status-code")) {
2749 if (safe_atoi(value, &i) < 0)
2750 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2752 s->main_exec_status.code = i;
2753 } else if (streq(key, "main-exec-status-status")) {
2756 if (safe_atoi(value, &i) < 0)
2757 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2759 s->main_exec_status.status = i;
2760 } else if (streq(key, "main-exec-status-start"))
2761 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2762 else if (streq(key, "main-exec-status-exit"))
2763 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2764 else if (streq(key, "watchdog-timestamp"))
2765 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2766 else if (streq(key, "tmp-dir")) {
2773 s->exec_context.tmp_dir = t;
2774 } else if (streq(key, "var-tmp-dir")) {
2781 s->exec_context.var_tmp_dir = t;
2782 } else if (streq(key, "forbid-restart")) {
2785 b = parse_boolean(value);
2787 log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value);
2789 s->forbid_restart = b;
2791 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2796 _pure_ static UnitActiveState service_active_state(Unit *u) {
2797 const UnitActiveState *table;
2801 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2803 return table[SERVICE(u)->state];
2806 static const char *service_sub_state_to_string(Unit *u) {
2809 return service_state_to_string(SERVICE(u)->state);
2812 static bool service_check_gc(Unit *u) {
2813 Service *s = SERVICE(u);
2817 /* Never clean up services that still have a process around,
2818 * even if the service is formally dead. */
2819 if (cgroup_good(s) > 0 ||
2820 main_pid_good(s) > 0 ||
2821 control_pid_good(s) > 0)
2824 #ifdef HAVE_SYSV_COMPAT
2832 _pure_ static bool service_check_snapshot(Unit *u) {
2833 Service *s = SERVICE(u);
2837 return !s->got_socket_fd;
2840 static int service_retry_pid_file(Service *s) {
2843 assert(s->pid_file);
2844 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2846 r = service_load_pid_file(s, false);
2850 service_unwatch_pid_file(s);
2852 service_enter_running(s, SERVICE_SUCCESS);
2856 static int service_watch_pid_file(Service *s) {
2859 log_debug_unit(UNIT(s)->id,
2860 "Setting watch for %s's PID file %s",
2861 UNIT(s)->id, s->pid_file_pathspec->path);
2862 r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
2866 /* the pidfile might have appeared just before we set the watch */
2867 log_debug_unit(UNIT(s)->id,
2868 "Trying to read %s's PID file %s in case it changed",
2869 UNIT(s)->id, s->pid_file_pathspec->path);
2870 service_retry_pid_file(s);
2874 log_error_unit(UNIT(s)->id,
2875 "Failed to set a watch for %s's PID file %s: %s",
2876 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2877 service_unwatch_pid_file(s);
2881 static int service_demand_pid_file(Service *s) {
2884 assert(s->pid_file);
2885 assert(!s->pid_file_pathspec);
2887 ps = new0(PathSpec, 1);
2892 ps->path = strdup(s->pid_file);
2898 path_kill_slashes(ps->path);
2900 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2901 * keep their PID file open all the time. */
2902 ps->type = PATH_MODIFIED;
2903 ps->inotify_fd = -1;
2905 s->pid_file_pathspec = ps;
2907 return service_watch_pid_file(s);
2910 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
2911 Service *s = SERVICE(userdata);
2915 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2916 assert(s->pid_file_pathspec);
2917 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2919 log_debug_unit(UNIT(s)->id, "inotify event for %s", UNIT(s)->id);
2921 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2924 if (service_retry_pid_file(s) == 0)
2927 if (service_watch_pid_file(s) < 0)
2933 service_unwatch_pid_file(s);
2934 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2938 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2939 Service *s = SERVICE(u);
2945 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2946 is_clean_exit_lsb(code, status, &s->success_status))
2947 f = SERVICE_SUCCESS;
2948 else if (code == CLD_EXITED)
2949 f = SERVICE_FAILURE_EXIT_CODE;
2950 else if (code == CLD_KILLED)
2951 f = SERVICE_FAILURE_SIGNAL;
2952 else if (code == CLD_DUMPED)
2953 f = SERVICE_FAILURE_CORE_DUMP;
2955 assert_not_reached("Unknown code");
2957 if (s->main_pid == pid) {
2958 /* Forking services may occasionally move to a new PID.
2959 * As long as they update the PID file before exiting the old
2960 * PID, they're fine. */
2961 if (service_load_pid_file(s, false) == 0)
2965 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2967 if (s->main_command) {
2968 /* If this is not a forking service than the
2969 * main process got started and hence we copy
2970 * the exit status so that it is recorded both
2971 * as main and as control process exit
2974 s->main_command->exec_status = s->main_exec_status;
2976 if (s->main_command->ignore)
2977 f = SERVICE_SUCCESS;
2978 } else if (s->exec_command[SERVICE_EXEC_START]) {
2980 /* If this is a forked process, then we should
2981 * ignore the return value if this was
2982 * configured for the starter process */
2984 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2985 f = SERVICE_SUCCESS;
2988 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2990 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2991 u->id, sigchld_code_to_string(code), status,
2992 strna(code == CLD_EXITED
2993 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2994 : signal_to_string(status)),
2995 "EXIT_CODE=%s", sigchld_code_to_string(code),
2996 "EXIT_STATUS=%i", status,
2999 if (f != SERVICE_SUCCESS)
3002 if (s->main_command &&
3003 s->main_command->command_next &&
3004 f == SERVICE_SUCCESS) {
3006 /* There is another command to *
3007 * execute, so let's do that. */
3009 log_debug_unit(u->id,
3010 "%s running next main command for state %s",
3011 u->id, service_state_to_string(s->state));
3012 service_run_next_main(s);
3016 /* The service exited, so the service is officially
3018 s->main_command = NULL;
3022 case SERVICE_START_POST:
3023 case SERVICE_RELOAD:
3025 /* Need to wait until the operation is
3030 if (s->type == SERVICE_ONESHOT) {
3031 /* This was our main goal, so let's go on */
3032 if (f == SERVICE_SUCCESS)
3033 service_enter_start_post(s);
3035 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3041 case SERVICE_RUNNING:
3042 service_enter_running(s, f);
3045 case SERVICE_STOP_SIGTERM:
3046 case SERVICE_STOP_SIGKILL:
3048 if (!control_pid_good(s))
3049 service_enter_stop_post(s, f);
3051 /* If there is still a control process, wait for that first */
3055 assert_not_reached("Uh, main process died at wrong time.");
3059 } else if (s->control_pid == pid) {
3062 if (s->control_command) {
3063 exec_status_exit(&s->control_command->exec_status,
3064 &s->exec_context, pid, code, status);
3066 if (s->control_command->ignore)
3067 f = SERVICE_SUCCESS;
3070 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
3071 "%s: control process exited, code=%s status=%i",
3072 u->id, sigchld_code_to_string(code), status);
3074 if (f != SERVICE_SUCCESS)
3077 /* Immediately get rid of the cgroup, so that the
3078 * kernel doesn't delay the cgroup empty messages for
3079 * the service cgroup any longer than necessary */
3080 service_kill_control_processes(s);
3082 if (s->control_command &&
3083 s->control_command->command_next &&
3084 f == SERVICE_SUCCESS) {
3086 /* There is another command to *
3087 * execute, so let's do that. */
3089 log_debug_unit(u->id,
3090 "%s running next control command for state %s",
3091 u->id, service_state_to_string(s->state));
3092 service_run_next_control(s);
3095 /* No further commands for this step, so let's
3096 * figure out what to do next */
3098 s->control_command = NULL;
3099 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3101 log_debug_unit(u->id,
3102 "%s got final SIGCHLD for state %s",
3103 u->id, service_state_to_string(s->state));
3107 case SERVICE_START_PRE:
3108 if (f == SERVICE_SUCCESS)
3109 service_enter_start(s);
3111 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3115 if (s->type != SERVICE_FORKING)
3116 /* Maybe spurious event due to a reload that changed the type? */
3119 if (f != SERVICE_SUCCESS) {
3120 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3125 bool has_start_post;
3128 /* Let's try to load the pid file here if we can.
3129 * The PID file might actually be created by a START_POST
3130 * script. In that case don't worry if the loading fails. */
3132 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3133 r = service_load_pid_file(s, !has_start_post);
3134 if (!has_start_post && r < 0) {
3135 r = service_demand_pid_file(s);
3136 if (r < 0 || !cgroup_good(s))
3137 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3141 service_search_main_pid(s);
3143 service_enter_start_post(s);
3146 case SERVICE_START_POST:
3147 if (f != SERVICE_SUCCESS) {
3148 service_enter_stop(s, f);
3155 r = service_load_pid_file(s, true);
3157 r = service_demand_pid_file(s);
3158 if (r < 0 || !cgroup_good(s))
3159 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3163 service_search_main_pid(s);
3165 service_enter_running(s, SERVICE_SUCCESS);
3168 case SERVICE_RELOAD:
3169 if (f == SERVICE_SUCCESS) {
3170 service_load_pid_file(s, true);
3171 service_search_main_pid(s);
3174 s->reload_result = f;
3175 service_enter_running(s, SERVICE_SUCCESS);
3179 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3182 case SERVICE_STOP_SIGTERM:
3183 case SERVICE_STOP_SIGKILL:
3184 if (main_pid_good(s) <= 0)
3185 service_enter_stop_post(s, f);
3187 /* If there is still a service
3188 * process around, wait until
3189 * that one quit, too */
3192 case SERVICE_STOP_POST:
3193 case SERVICE_FINAL_SIGTERM:
3194 case SERVICE_FINAL_SIGKILL:
3195 service_enter_dead(s, f, true);
3199 assert_not_reached("Uh, control process died at wrong time.");
3204 /* Notify clients about changed exit status */
3205 unit_add_to_dbus_queue(u);
3208 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
3209 Service *s = SERVICE(userdata);
3212 assert(source == s->timer_event_source);
3216 case SERVICE_START_PRE:
3218 log_warning_unit(UNIT(s)->id,
3219 "%s operation timed out. Terminating.", UNIT(s)->id);
3220 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3223 case SERVICE_START_POST:
3224 log_warning_unit(UNIT(s)->id,
3225 "%s operation timed out. Stopping.", UNIT(s)->id);
3226 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3229 case SERVICE_RELOAD:
3230 log_warning_unit(UNIT(s)->id,
3231 "%s operation timed out. Stopping.", UNIT(s)->id);
3232 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3233 service_enter_running(s, SERVICE_SUCCESS);
3237 log_warning_unit(UNIT(s)->id,
3238 "%s stopping timed out. Terminating.", UNIT(s)->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(UNIT(s)->id,
3245 "%s stopping timed out. Killing.", UNIT(s)->id);
3246 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3248 log_warning_unit(UNIT(s)->id,
3249 "%s stopping timed out. Skipping SIGKILL.", UNIT(s)->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(UNIT(s)->id,
3261 "%s still around after SIGKILL. Ignoring.", UNIT(s)->id);
3262 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3265 case SERVICE_STOP_POST:
3266 log_warning_unit(UNIT(s)->id,
3267 "%s stopping timed out (2). Terminating.", UNIT(s)->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(UNIT(s)->id,
3274 "%s stopping timed out (2). Killing.", UNIT(s)->id);
3275 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3277 log_warning_unit(UNIT(s)->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(UNIT(s)->id,
3287 "%s still around after SIGKILL (2). Entering failed mode.", UNIT(s)->id);
3288 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3291 case SERVICE_AUTO_RESTART:
3292 log_info_unit(UNIT(s)->id,
3293 "%s holdoff time over, scheduling restart.", UNIT(s)->id);
3294 service_enter_restart(s);
3298 assert_not_reached("Timeout at wrong time.");
3304 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
3305 Service *s = SERVICE(userdata);
3308 assert(source == s->watchdog_event_source);
3310 service_handle_watchdog(s);
3314 static void service_notify_cgroup_empty_event(Unit *u) {
3315 Service *s = SERVICE(u);
3319 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
3323 /* Waiting for SIGCHLD is usually more interesting,
3324 * because it includes return codes/signals. Which is
3325 * why we ignore the cgroup events for most cases,
3326 * except when we don't know pid which to expect the
3330 case SERVICE_START_POST:
3331 /* If we were hoping for the daemon to write its PID file,
3332 * we can give up now. */
3333 if (s->pid_file_pathspec) {
3334 log_warning_unit(u->id,
3335 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3336 service_unwatch_pid_file(s);
3337 if (s->state == SERVICE_START)
3338 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3340 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3344 case SERVICE_RUNNING:
3345 /* service_enter_running() will figure out what to do */
3346 service_enter_running(s, SERVICE_SUCCESS);
3349 case SERVICE_STOP_SIGTERM:
3350 case SERVICE_STOP_SIGKILL:
3352 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3353 service_enter_stop_post(s, SERVICE_SUCCESS);
3357 case SERVICE_FINAL_SIGTERM:
3358 case SERVICE_FINAL_SIGKILL:
3359 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3360 service_enter_dead(s, SERVICE_SUCCESS, true);
3369 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3370 Service *s = SERVICE(u);
3375 if (s->notify_access == NOTIFY_NONE) {
3376 log_warning_unit(u->id,
3377 "%s: Got notification message from PID %lu, but reception is disabled.",
3378 u->id, (unsigned long) pid);
3382 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3383 log_warning_unit(u->id,
3384 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3385 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3389 log_debug_unit(u->id,
3390 "%s: Got message", u->id);
3392 /* Interpret MAINPID= */
3393 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3394 (s->state == SERVICE_START ||
3395 s->state == SERVICE_START_POST ||
3396 s->state == SERVICE_RUNNING ||
3397 s->state == SERVICE_RELOAD)) {
3399 if (parse_pid(e + 8, &pid) < 0)
3400 log_warning_unit(u->id,
3401 "Failed to parse notification message %s", e);
3403 log_debug_unit(u->id,
3404 "%s: got %s", u->id, e);
3405 service_set_main_pid(s, pid);
3406 unit_watch_pid(UNIT(s), pid);
3410 /* Interpret READY= */
3411 if (s->type == SERVICE_NOTIFY &&
3412 s->state == SERVICE_START &&
3413 strv_find(tags, "READY=1")) {
3414 log_debug_unit(u->id,
3415 "%s: got READY=1", u->id);
3417 service_enter_start_post(s);
3420 /* Interpret STATUS= */
3421 e = strv_find_prefix(tags, "STATUS=");
3427 if (!utf8_is_valid(e+7)) {
3428 log_warning_unit(u->id,
3429 "Status message in notification is not UTF-8 clean.");
3435 log_error_unit(u->id,
3436 "Failed to allocate string.");
3440 log_debug_unit(u->id,
3441 "%s: got %s", u->id, e);
3443 free(s->status_text);
3446 free(s->status_text);
3447 s->status_text = NULL;
3451 if (strv_find(tags, "WATCHDOG=1")) {
3452 log_debug_unit(u->id,
3453 "%s: got WATCHDOG=1", u->id);
3454 if (dual_timestamp_is_set(&s->watchdog_timestamp))
3455 service_reset_watchdog(s);
3458 /* Notify clients about changed status or main pid */
3459 unit_add_to_dbus_queue(u);
3462 #ifdef HAVE_SYSV_COMPAT
3464 static int service_enumerate(Manager *m) {
3467 _cleanup_closedir_ DIR *d = NULL;
3468 _cleanup_free_ char *path = NULL, *fpath = NULL, *name = NULL;
3469 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
3470 _cleanup_set_free_ Set *shutdown_services = NULL;
3477 if (m->running_as != SYSTEMD_SYSTEM)
3480 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3481 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3485 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3496 if (errno != ENOENT)
3497 log_warning("opendir(%s) failed: %s", path, strerror(errno));
3502 while ((de = readdir(d))) {
3505 if (ignore_file(de->d_name))
3508 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3511 if (strlen(de->d_name) < 4)
3514 a = undecchar(de->d_name[1]);
3515 b = undecchar(de->d_name[2]);
3521 fpath = strjoin(path, "/", de->d_name, NULL);
3527 if (access(fpath, X_OK) < 0) {
3529 if (errno != ENOENT)
3530 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3536 name = sysv_translate_name(de->d_name + 3);
3542 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3544 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3548 if (de->d_name[0] == 'S') {
3550 if (rcnd_table[i].type == RUNLEVEL_UP) {
3551 SERVICE(service)->sysv_start_priority_from_rcnd =
3552 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3554 SERVICE(service)->sysv_enabled = true;
3557 r = set_ensure_allocated(&runlevel_services[i],
3558 trivial_hash_func, trivial_compare_func);
3562 r = set_put(runlevel_services[i], service);
3566 } else if (de->d_name[0] == 'K' &&
3567 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3569 r = set_ensure_allocated(&shutdown_services,
3570 trivial_hash_func, trivial_compare_func);
3574 r = set_put(shutdown_services, service);
3581 /* Now we loaded all stubs and are aware of the lowest
3582 start-up priority for all services, not let's actually load
3583 the services, this will also tell us which services are
3584 actually native now */
3585 manager_dispatch_load_queue(m);
3587 /* If this is a native service, rely on native ways to pull in
3588 * a service, don't pull it in via sysv rcN.d links. */
3589 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3590 SET_FOREACH(service, runlevel_services[i], j) {
3591 service = unit_follow_merge(service);
3593 if (service->fragment_path)
3596 r = unit_add_two_dependencies_by_name_inverse(
3597 service, UNIT_AFTER, UNIT_WANTS,
3598 rcnd_table[i].target, NULL, true);
3603 /* We honour K links only for halt/reboot. For the normal
3604 * runlevels we assume the stop jobs will be implicitly added
3605 * by the core logic. Also, we don't really distinguish here
3606 * between the runlevels 0 and 6 and just add them to the
3607 * special shutdown target. */
3608 SET_FOREACH(service, shutdown_services, j) {
3609 service = unit_follow_merge(service);
3611 if (service->fragment_path)
3614 r = unit_add_two_dependencies_by_name(
3615 service, UNIT_BEFORE, UNIT_CONFLICTS,
3616 SPECIAL_SHUTDOWN_TARGET, NULL, true);
3625 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3626 set_free(runlevel_services[i]);
3632 static void service_bus_name_owner_change(
3635 const char *old_owner,
3636 const char *new_owner) {
3638 Service *s = SERVICE(u);
3644 assert(streq(s->bus_name, name));
3645 assert(old_owner || new_owner);
3647 if (old_owner && new_owner)
3648 log_debug_unit(u->id,
3649 "%s's D-Bus name %s changed owner from %s to %s",
3650 u->id, name, old_owner, new_owner);
3652 log_debug_unit(u->id,
3653 "%s's D-Bus name %s no longer registered by %s",
3654 u->id, name, old_owner);
3656 log_debug_unit(u->id,
3657 "%s's D-Bus name %s now registered by %s",
3658 u->id, name, new_owner);
3660 s->bus_name_good = !!new_owner;
3662 if (s->type == SERVICE_DBUS) {
3664 /* service_enter_running() will figure out what to
3666 if (s->state == SERVICE_RUNNING)
3667 service_enter_running(s, SERVICE_SUCCESS);
3668 else if (s->state == SERVICE_START && new_owner)
3669 service_enter_start_post(s);
3671 } else if (new_owner &&
3673 (s->state == SERVICE_START ||
3674 s->state == SERVICE_START_POST ||
3675 s->state == SERVICE_RUNNING ||
3676 s->state == SERVICE_RELOAD)) {
3680 /* Try to acquire PID from bus service */
3682 r = sd_bus_get_owner_pid(u->manager->api_bus, name, &pid);
3684 log_debug_unit(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3686 service_set_main_pid(s, pid);
3687 unit_watch_pid(UNIT(s), pid);
3692 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3697 /* This is called by the socket code when instantiating a new
3698 * service for a stream socket and the socket needs to be
3701 if (UNIT(s)->load_state != UNIT_LOADED)
3704 if (s->socket_fd >= 0)
3707 if (s->state != SERVICE_DEAD)
3711 s->got_socket_fd = true;
3713 unit_ref_set(&s->accept_socket, UNIT(sock));
3715 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3718 static void service_reset_failed(Unit *u) {
3719 Service *s = SERVICE(u);
3723 if (s->state == SERVICE_FAILED)
3724 service_set_state(s, SERVICE_DEAD);
3726 s->result = SERVICE_SUCCESS;
3727 s->reload_result = SERVICE_SUCCESS;
3729 RATELIMIT_RESET(s->start_limit);
3732 static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
3733 Service *s = SERVICE(u);
3735 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
3738 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3739 [SERVICE_DEAD] = "dead",
3740 [SERVICE_START_PRE] = "start-pre",
3741 [SERVICE_START] = "start",
3742 [SERVICE_START_POST] = "start-post",
3743 [SERVICE_RUNNING] = "running",
3744 [SERVICE_EXITED] = "exited",
3745 [SERVICE_RELOAD] = "reload",
3746 [SERVICE_STOP] = "stop",
3747 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3748 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3749 [SERVICE_STOP_POST] = "stop-post",
3750 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3751 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3752 [SERVICE_FAILED] = "failed",
3753 [SERVICE_AUTO_RESTART] = "auto-restart",
3756 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3758 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3759 [SERVICE_RESTART_NO] = "no",
3760 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3761 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3762 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
3763 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3764 [SERVICE_RESTART_ALWAYS] = "always"
3767 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3769 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3770 [SERVICE_SIMPLE] = "simple",
3771 [SERVICE_FORKING] = "forking",
3772 [SERVICE_ONESHOT] = "oneshot",
3773 [SERVICE_DBUS] = "dbus",
3774 [SERVICE_NOTIFY] = "notify",
3775 [SERVICE_IDLE] = "idle"
3778 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3780 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3781 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3782 [SERVICE_EXEC_START] = "ExecStart",
3783 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3784 [SERVICE_EXEC_RELOAD] = "ExecReload",
3785 [SERVICE_EXEC_STOP] = "ExecStop",
3786 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3789 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3791 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3792 [NOTIFY_NONE] = "none",
3793 [NOTIFY_MAIN] = "main",
3794 [NOTIFY_ALL] = "all"
3797 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3799 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3800 [SERVICE_SUCCESS] = "success",
3801 [SERVICE_FAILURE_RESOURCES] = "resources",
3802 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3803 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3804 [SERVICE_FAILURE_SIGNAL] = "signal",
3805 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3806 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3807 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3810 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3812 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3813 [SERVICE_START_LIMIT_NONE] = "none",
3814 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3815 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3816 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3818 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3820 const UnitVTable service_vtable = {
3821 .object_size = sizeof(Service),
3822 .exec_context_offset = offsetof(Service, exec_context),
3823 .cgroup_context_offset = offsetof(Service, cgroup_context),
3824 .kill_context_offset = offsetof(Service, kill_context),
3830 .private_section = "Service",
3832 .init = service_init,
3833 .done = service_done,
3834 .load = service_load,
3836 .coldplug = service_coldplug,
3838 .dump = service_dump,
3840 .start = service_start,
3841 .stop = service_stop,
3842 .reload = service_reload,
3844 .can_reload = service_can_reload,
3846 .kill = service_kill,
3848 .serialize = service_serialize,
3849 .deserialize_item = service_deserialize_item,
3851 .active_state = service_active_state,
3852 .sub_state_to_string = service_sub_state_to_string,
3854 .check_gc = service_check_gc,
3855 .check_snapshot = service_check_snapshot,
3857 .sigchld_event = service_sigchld_event,
3859 .reset_failed = service_reset_failed,
3861 .notify_cgroup_empty = service_notify_cgroup_empty_event,
3862 .notify_message = service_notify_message,
3864 .bus_name_owner_change = service_bus_name_owner_change,
3866 .bus_interface = "org.freedesktop.systemd1.Service",
3867 .bus_vtable = bus_service_vtable,
3868 .bus_changing_properties = bus_service_changing_properties,
3869 .bus_set_property = bus_service_set_property,
3870 .bus_commit_properties = bus_service_commit_properties,
3872 #ifdef HAVE_SYSV_COMPAT
3873 .enumerate = service_enumerate,
3876 .can_transient = true,
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.",