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>
27 #include <linux/reboot.h>
28 #include <sys/syscall.h>
34 #include "load-fragment.h"
35 #include "load-dropin.h"
38 #include "unit-name.h"
39 #include "unit-printf.h"
40 #include "dbus-service.h"
42 #include "exit-status.h"
44 #include "path-util.h"
49 #include "bus-error.h"
52 #ifdef HAVE_SYSV_COMPAT
54 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
56 typedef enum RunlevelType {
64 const RunlevelType type;
66 /* Standard SysV runlevels for start-up */
67 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
68 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
69 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
70 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
71 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
73 /* Standard SysV runlevels for shutdown */
74 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
75 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
77 /* Note that the order here matters, as we read the
78 directories in this order, and we want to make sure that
79 sysv_start_priority is known when we first load the
80 unit. And that value we only know from S links. Hence
81 UP must be read before DOWN */
84 #define RUNLEVELS_UP "12345"
87 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
88 [SERVICE_DEAD] = UNIT_INACTIVE,
89 [SERVICE_START_PRE] = UNIT_ACTIVATING,
90 [SERVICE_START] = UNIT_ACTIVATING,
91 [SERVICE_START_POST] = UNIT_ACTIVATING,
92 [SERVICE_RUNNING] = UNIT_ACTIVE,
93 [SERVICE_EXITED] = UNIT_ACTIVE,
94 [SERVICE_RELOAD] = UNIT_RELOADING,
95 [SERVICE_STOP] = UNIT_DEACTIVATING,
96 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
97 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
98 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
99 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
100 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
101 [SERVICE_FAILED] = UNIT_FAILED,
102 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
105 /* For Type=idle we never want to delay any other jobs, hence we
106 * consider idle jobs active as soon as we start working on them */
107 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
108 [SERVICE_DEAD] = UNIT_INACTIVE,
109 [SERVICE_START_PRE] = UNIT_ACTIVE,
110 [SERVICE_START] = UNIT_ACTIVE,
111 [SERVICE_START_POST] = UNIT_ACTIVE,
112 [SERVICE_RUNNING] = UNIT_ACTIVE,
113 [SERVICE_EXITED] = UNIT_ACTIVE,
114 [SERVICE_RELOAD] = UNIT_RELOADING,
115 [SERVICE_STOP] = UNIT_DEACTIVATING,
116 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
117 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
118 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
119 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
120 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
121 [SERVICE_FAILED] = UNIT_FAILED,
122 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
125 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata);
126 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
127 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata);
129 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
131 static void service_init(Unit *u) {
132 Service *s = SERVICE(u);
135 assert(u->load_state == UNIT_STUB);
137 s->timeout_start_usec = u->manager->default_timeout_start_usec;
138 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
139 s->restart_usec = u->manager->default_restart_usec;
140 s->type = _SERVICE_TYPE_INVALID;
142 #ifdef HAVE_SYSV_COMPAT
143 s->sysv_start_priority = -1;
144 s->sysv_start_priority_from_rcnd = -1;
147 s->guess_main_pid = true;
149 RATELIMIT_INIT(s->start_limit, u->manager->default_start_limit_interval, u->manager->default_start_limit_burst);
151 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
154 static void service_unwatch_control_pid(Service *s) {
157 if (s->control_pid <= 0)
160 unit_unwatch_pid(UNIT(s), s->control_pid);
164 static void service_unwatch_main_pid(Service *s) {
167 if (s->main_pid <= 0)
170 unit_unwatch_pid(UNIT(s), s->main_pid);
174 static void service_unwatch_pid_file(Service *s) {
175 if (!s->pid_file_pathspec)
178 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
179 UNIT(s)->id, s->pid_file_pathspec->path);
180 path_spec_unwatch(s->pid_file_pathspec);
181 path_spec_done(s->pid_file_pathspec);
182 free(s->pid_file_pathspec);
183 s->pid_file_pathspec = NULL;
186 static int service_set_main_pid(Service *s, pid_t pid) {
197 if (s->main_pid == pid && s->main_pid_known)
200 if (s->main_pid != pid) {
201 service_unwatch_main_pid(s);
202 exec_status_start(&s->main_exec_status, pid);
206 s->main_pid_known = true;
208 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
209 log_warning_unit(UNIT(s)->id,
210 "%s: Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.",
213 s->main_pid_alien = true;
215 s->main_pid_alien = false;
220 static void service_close_socket_fd(Service *s) {
223 if (s->socket_fd < 0)
226 s->socket_fd = asynchronous_close(s->socket_fd);
229 static void service_connection_unref(Service *s) {
232 if (!UNIT_ISSET(s->accept_socket))
235 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
236 unit_ref_unset(&s->accept_socket);
239 static void service_stop_watchdog(Service *s) {
242 s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source);
243 s->watchdog_timestamp = DUAL_TIMESTAMP_NULL;
246 static void service_start_watchdog(Service *s) {
251 if (s->watchdog_usec <= 0)
254 if (s->watchdog_event_source) {
255 r = sd_event_source_set_time(s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec);
257 log_warning_unit(UNIT(s)->id, "%s failed to reset watchdog timer: %s", UNIT(s)->id, strerror(-r));
261 r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT);
263 r = sd_event_add_time(
264 UNIT(s)->manager->event,
265 &s->watchdog_event_source,
267 s->watchdog_timestamp.monotonic + s->watchdog_usec, 0,
268 service_dispatch_watchdog, s);
270 log_warning_unit(UNIT(s)->id, "%s failed to add watchdog timer: %s", UNIT(s)->id, strerror(-r));
274 /* Let's process everything else which might be a sign
275 * of living before we consider a service died. */
276 r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE);
280 log_warning_unit(UNIT(s)->id, "%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
283 static void service_reset_watchdog(Service *s) {
286 dual_timestamp_get(&s->watchdog_timestamp);
287 service_start_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;
307 s->reboot_arg = NULL;
309 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
310 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
311 s->control_command = NULL;
312 s->main_command = NULL;
314 set_free(s->restart_ignore_status.code);
315 s->restart_ignore_status.code = NULL;
316 set_free(s->restart_ignore_status.signal);
317 s->restart_ignore_status.signal = NULL;
319 set_free(s->success_status.code);
320 s->success_status.code = NULL;
321 set_free(s->success_status.signal);
322 s->success_status.signal = NULL;
324 /* This will leak a process, but at least no memory or any of
326 service_unwatch_main_pid(s);
327 service_unwatch_control_pid(s);
328 service_unwatch_pid_file(s);
331 unit_unwatch_bus_name(u, s->bus_name);
336 service_close_socket_fd(s);
337 service_connection_unref(s);
339 unit_ref_unset(&s->accept_socket);
341 service_stop_watchdog(s);
343 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
346 static int service_arm_timer(Service *s, usec_t usec) {
351 if (s->timer_event_source) {
352 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec);
356 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
359 return sd_event_add_time(
360 UNIT(s)->manager->event,
361 &s->timer_event_source,
363 now(CLOCK_MONOTONIC) + usec, 0,
364 service_dispatch_timer, s);
367 #ifdef HAVE_SYSV_COMPAT
368 static char *sysv_translate_name(const char *name) {
371 r = new(char, strlen(name) + strlen(".service") + 1);
375 if (endswith(name, ".sh"))
376 /* Drop .sh suffix */
377 strcpy(stpcpy(r, name) - 3, ".service");
379 /* Normal init script name */
380 strcpy(stpcpy(r, name), ".service");
385 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
387 /* We silently ignore the $ prefix here. According to the LSB
388 * spec it simply indicates whether something is a
389 * standardized name or a distribution-specific one. Since we
390 * just follow what already exists and do not introduce new
391 * uses or names we don't care who introduced a new name. */
393 static const char * const table[] = {
394 /* LSB defined facilities */
396 "network", SPECIAL_NETWORK_ONLINE_TARGET,
397 "named", SPECIAL_NSS_LOOKUP_TARGET,
398 "portmap", SPECIAL_RPCBIND_TARGET,
399 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
401 "time", SPECIAL_TIME_SYNC_TARGET,
411 n = *name == '$' ? name + 1 : name;
413 for (i = 0; i < ELEMENTSOF(table); i += 2) {
415 if (!streq(table[i], n))
421 r = strdup(table[i+1]);
428 /* If we don't know this name, fallback heuristics to figure
429 * out whether something is a target or a service alias. */
432 if (!unit_prefix_is_valid(n))
435 /* Facilities starting with $ are most likely targets */
436 r = unit_name_build(n, NULL, ".target");
437 } else if (filename && streq(name, filename))
438 /* Names equaling the file name of the services are redundant */
441 /* Everything else we assume to be normal service names */
442 r = sysv_translate_name(n);
453 static int sysv_fix_order(Service *s) {
459 if (s->sysv_start_priority < 0)
462 /* For each pair of services where at least one lacks a LSB
463 * header, we use the start priority value to order things. */
465 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
468 bool special_s, special_t;
475 if (UNIT(t)->load_state != UNIT_LOADED)
478 if (t->sysv_start_priority < 0)
481 /* If both units have modern headers we don't care
482 * about the priorities */
483 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
484 (UNIT(t)->fragment_path || t->sysv_has_lsb))
487 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
488 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
490 if (special_t && !special_s)
492 else if (special_s && !special_t)
494 else if (t->sysv_start_priority < s->sysv_start_priority)
496 else if (t->sysv_start_priority > s->sysv_start_priority)
501 /* FIXME: Maybe we should compare the name here lexicographically? */
503 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
510 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
513 if (!(c = new0(ExecCommand, 1)))
516 if (!(c->path = strdup(path))) {
521 if (!(c->argv = strv_new(path, arg1, NULL))) {
530 static int sysv_exec_commands(Service *s, const bool supports_reload) {
535 assert(UNIT(s)->source_path);
537 c = exec_command_new(UNIT(s)->source_path, "start");
540 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
542 c = exec_command_new(UNIT(s)->source_path, "stop");
545 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
547 if (supports_reload) {
548 c = exec_command_new(UNIT(s)->source_path, "reload");
551 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
557 static bool usage_contains_reload(const char *line) {
558 return (strcasestr(line, "{reload|") ||
559 strcasestr(line, "{reload}") ||
560 strcasestr(line, "{reload\"") ||
561 strcasestr(line, "|reload|") ||
562 strcasestr(line, "|reload}") ||
563 strcasestr(line, "|reload\""));
566 static int service_load_sysv_path(Service *s, const char *path) {
578 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
580 bool supports_reload = false;
587 f = fopen(path, "re");
589 r = errno == ENOENT ? 0 : -errno;
593 if (fstat(fileno(f), &st) < 0) {
598 free(u->source_path);
599 u->source_path = strdup(path);
600 if (!u->source_path) {
604 u->source_mtime = timespec_load(&st.st_mtim);
606 if (null_or_empty(&st)) {
607 u->load_state = UNIT_MASKED;
615 char l[LINE_MAX], *t;
617 if (!fgets(l, sizeof(l), f)) {
622 log_error_unit(u->id,
623 "Failed to read configuration file '%s': %s",
632 /* Try to figure out whether this init script supports
633 * the reload operation. This heuristic looks for
634 * "Usage" lines which include the reload option. */
635 if ( state == USAGE_CONTINUATION ||
636 (state == NORMAL && strcasestr(t, "usage"))) {
637 if (usage_contains_reload(t)) {
638 supports_reload = true;
640 } else if (t[strlen(t)-1] == '\\')
641 state = USAGE_CONTINUATION;
649 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
651 s->sysv_has_lsb = true;
655 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
661 t += strspn(t, WHITESPACE);
663 if (state == NORMAL) {
665 /* Try to parse Red Hat style chkconfig headers */
667 if (startswith_no_case(t, "chkconfig:")) {
669 char runlevels[16], *k;
673 if (sscanf(t+10, "%15s %i %*i", runlevels, &start_priority) != 2) {
674 log_warning_unit(u->id, "[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
678 /* A start priority gathered from the
679 * symlink farms is preferred over the
680 * data from the LSB header. */
681 if (start_priority < 0 || start_priority > 99)
682 log_warning_unit(u->id, "[%s:%u] Start priority out of range. Ignoring.", path, line);
684 log_debug_unit(u->id, "[%s:%u] Ignoring start priority set in the chkconfig file.", path, line);
686 char_array_0(runlevels);
687 k = delete_chars(runlevels, WHITESPACE "-");
697 free(s->sysv_runlevels);
698 s->sysv_runlevels = d;
701 } else if (startswith_no_case(t, "description:")) {
703 size_t k = strlen(t);
707 if (t[k-1] == '\\') {
712 if ((j = strstrip(t+12)) && *j) {
713 if (!(d = strdup(j))) {
720 free(chkconfig_description);
721 chkconfig_description = d;
723 } else if (startswith_no_case(t, "pidfile:")) {
730 if (!path_is_absolute(fn)) {
731 log_warning_unit(u->id,
732 "[%s:%u] PID file not absolute. Ignoring.",
737 if (!(fn = strdup(fn))) {
746 } else if (state == DESCRIPTION) {
748 /* Try to parse Red Hat style description
751 size_t k = strlen(t);
759 if ((j = strstrip(t)) && *j) {
762 if (chkconfig_description)
763 d = strjoin(chkconfig_description, " ", j, NULL);
772 free(chkconfig_description);
773 chkconfig_description = d;
776 } else if (state == LSB || state == LSB_DESCRIPTION) {
778 if (startswith_no_case(t, "Provides:")) {
784 FOREACH_WORD_QUOTED(w, z, t+9, i) {
787 if (!(n = strndup(w, z))) {
792 r = sysv_translate_facility(n, basename(path), &m);
801 if (unit_name_to_type(m) == UNIT_SERVICE)
802 r = unit_merge_by_name(u, m);
809 * indication that the
811 * now available. This
814 * targets do NOT pull
817 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
820 log_error_unit(u->id,
821 "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
822 path, line, m, strerror(-r));
827 } else if (startswith_no_case(t, "Required-Start:") ||
828 startswith_no_case(t, "Should-Start:") ||
829 startswith_no_case(t, "X-Start-Before:") ||
830 startswith_no_case(t, "X-Start-After:")) {
836 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
840 if (!(n = strndup(w, z))) {
845 r = sysv_translate_facility(n, basename(path), &m);
847 log_error_unit(u->id,
848 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
849 path, line, n, strerror(-r));
859 is_before = startswith_no_case(t, "X-Start-Before:");
861 if (streq(m, SPECIAL_NETWORK_ONLINE_TARGET) && !is_before)
862 /* the network-online target is special, as it needs to be actively pulled in */
863 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_WANTS, m, NULL, true);
865 r = unit_add_dependency_by_name(u, is_before ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
868 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
869 path, line, m, strerror(-r));
873 } else if (startswith_no_case(t, "Default-Start:")) {
878 k = delete_chars(t+14, WHITESPACE "-");
881 if (!(d = strdup(k))) {
886 free(s->sysv_runlevels);
887 s->sysv_runlevels = d;
890 } else if (startswith_no_case(t, "Description:")) {
893 state = LSB_DESCRIPTION;
895 if ((j = strstrip(t+12)) && *j) {
896 if (!(d = strdup(j))) {
903 free(long_description);
904 long_description = d;
906 } else if (startswith_no_case(t, "Short-Description:")) {
911 if ((j = strstrip(t+18)) && *j) {
912 if (!(d = strdup(j))) {
919 free(short_description);
920 short_description = d;
922 } else if (state == LSB_DESCRIPTION) {
924 if (startswith(l, "#\t") || startswith(l, "# ")) {
927 if ((j = strstrip(t)) && *j) {
930 if (long_description)
931 d = strjoin(long_description, " ", t, NULL);
940 free(long_description);
941 long_description = d;
950 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
953 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
954 /* If there a runlevels configured for this service
955 * but none of the standard ones, then we assume this
956 * is some special kind of service (which might be
957 * needed for early boot) and don't create any links
960 UNIT(s)->default_dependencies = false;
962 /* Don't timeout special services during boot (like fsck) */
963 s->timeout_start_usec = 0;
964 s->timeout_stop_usec = 0;
966 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
967 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
970 /* Special setting for all SysV services */
971 s->type = SERVICE_FORKING;
972 s->remain_after_exit = !s->pid_file;
973 s->guess_main_pid = false;
974 s->restart = SERVICE_RESTART_NO;
975 s->exec_context.ignore_sigpipe = false;
976 s->kill_context.kill_mode = KILL_PROCESS;
978 /* We use the long description only if
979 * no short description is set. */
981 if (short_description)
982 description = short_description;
983 else if (chkconfig_description)
984 description = chkconfig_description;
985 else if (long_description)
986 description = long_description;
993 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
1001 /* Initialize the start priority from what has been set in the
1002 * /etc/rcN.d/ hierarchies if we load the unit file as SysV
1004 if (s->sysv_start_priority_from_rcnd >= 0)
1005 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
1007 u->load_state = UNIT_LOADED;
1015 free(short_description);
1016 free(long_description);
1017 free(chkconfig_description);
1022 static int service_load_sysv_name(Service *s, const char *name) {
1028 /* For SysV services we strip the *.sh suffixes. */
1029 if (endswith(name, ".sh.service"))
1032 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1036 path = strjoin(*p, "/", name, NULL);
1040 assert(endswith(path, ".service"));
1041 path[strlen(path)-8] = 0;
1043 r = service_load_sysv_path(s, path);
1045 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1046 /* Try *.sh source'able init scripts */
1047 strcat(path, ".sh");
1048 r = service_load_sysv_path(s, path);
1055 if (UNIT(s)->load_state != UNIT_STUB)
1062 static int service_load_sysv(Service *s) {
1069 /* Load service data from SysV init scripts, preferably with
1070 * LSB headers ... */
1072 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1075 if ((t = UNIT(s)->id))
1076 if ((r = service_load_sysv_name(s, t)) < 0)
1079 if (UNIT(s)->load_state == UNIT_STUB)
1080 SET_FOREACH(t, UNIT(s)->names, i) {
1081 if (t == UNIT(s)->id)
1084 if ((r = service_load_sysv_name(s, t)) < 0)
1087 if (UNIT(s)->load_state != UNIT_STUB)
1095 static int service_verify(Service *s) {
1098 if (UNIT(s)->load_state != UNIT_LOADED)
1101 if (!s->exec_command[SERVICE_EXEC_START]) {
1102 log_error_unit(UNIT(s)->id, "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1106 if (s->type != SERVICE_ONESHOT &&
1107 s->exec_command[SERVICE_EXEC_START]->command_next) {
1108 log_error_unit(UNIT(s)->id, "%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, "%s has Restart setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1117 if (s->type == SERVICE_DBUS && !s->bus_name) {
1118 log_error_unit(UNIT(s)->id, "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1122 if (s->bus_name && s->type != SERVICE_DBUS)
1123 log_warning_unit(UNIT(s)->id, "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1125 if (s->exec_context.pam_name && !(s->kill_context.kill_mode == KILL_CONTROL_GROUP || s->kill_context.kill_mode == KILL_MIXED)) {
1126 log_error_unit(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.", UNIT(s)->id);
1133 static int service_add_default_dependencies(Service *s) {
1138 /* Add a number of automatic dependencies useful for the
1139 * majority of services. */
1141 /* First, pull in base system */
1142 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1143 SPECIAL_BASIC_TARGET, NULL, true);
1147 /* Second, activate normal shutdown */
1148 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
1149 SPECIAL_SHUTDOWN_TARGET, NULL, true);
1153 static void service_fix_output(Service *s) {
1156 /* If nothing has been explicitly configured, patch default
1157 * output in. If input is socket/tty we avoid this however,
1158 * since in that case we want output to default to the same
1159 * place as we read input from. */
1161 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1162 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1163 s->exec_context.std_input == EXEC_INPUT_NULL)
1164 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1166 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1167 s->exec_context.std_input == EXEC_INPUT_NULL)
1168 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1171 static int service_load(Unit *u) {
1173 Service *s = SERVICE(u);
1177 /* Load a .service file */
1178 r = unit_load_fragment(u);
1182 #ifdef HAVE_SYSV_COMPAT
1183 /* Load a classic init script as a fallback, if we couldn't find anything */
1184 if (u->load_state == UNIT_STUB) {
1185 r = service_load_sysv(s);
1191 /* Still nothing found? Then let's give up */
1192 if (u->load_state == UNIT_STUB)
1195 /* This is a new unit? Then let's add in some extras */
1196 if (u->load_state == UNIT_LOADED) {
1198 /* We were able to load something, then let's add in
1199 * the dropin directories. */
1200 r = unit_load_dropin(u);
1204 if (s->type == _SERVICE_TYPE_INVALID)
1205 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1207 /* Oneshot services have disabled start timeout by default */
1208 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1209 s->timeout_start_usec = 0;
1211 service_fix_output(s);
1213 r = unit_patch_contexts(u);
1217 r = unit_add_exec_dependencies(u, &s->exec_context);
1221 r = unit_add_default_slice(u, &s->cgroup_context);
1225 #ifdef HAVE_SYSV_COMPAT
1226 r = sysv_fix_order(s);
1231 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1232 s->notify_access = NOTIFY_MAIN;
1234 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1235 s->notify_access = NOTIFY_MAIN;
1238 r = unit_watch_bus_name(u, s->bus_name);
1243 if (u->default_dependencies) {
1244 r = service_add_default_dependencies(s);
1250 return service_verify(s);
1253 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1255 ServiceExecCommand c;
1256 Service *s = SERVICE(u);
1257 const char *prefix2;
1258 _cleanup_free_ char *p2 = NULL;
1262 p2 = strappend(prefix, "\t");
1263 prefix2 = p2 ? p2 : prefix;
1266 "%sService State: %s\n"
1268 "%sReload Result: %s\n"
1269 "%sPermissionsStartOnly: %s\n"
1270 "%sRootDirectoryStartOnly: %s\n"
1271 "%sRemainAfterExit: %s\n"
1272 "%sGuessMainPID: %s\n"
1275 "%sNotifyAccess: %s\n",
1276 prefix, service_state_to_string(s->state),
1277 prefix, service_result_to_string(s->result),
1278 prefix, service_result_to_string(s->reload_result),
1279 prefix, yes_no(s->permissions_start_only),
1280 prefix, yes_no(s->root_directory_start_only),
1281 prefix, yes_no(s->remain_after_exit),
1282 prefix, yes_no(s->guess_main_pid),
1283 prefix, service_type_to_string(s->type),
1284 prefix, service_restart_to_string(s->restart),
1285 prefix, notify_access_to_string(s->notify_access));
1287 if (s->control_pid > 0)
1289 "%sControl PID: "PID_FMT"\n",
1290 prefix, s->control_pid);
1292 if (s->main_pid > 0)
1294 "%sMain PID: "PID_FMT"\n"
1295 "%sMain PID Known: %s\n"
1296 "%sMain PID Alien: %s\n",
1297 prefix, s->main_pid,
1298 prefix, yes_no(s->main_pid_known),
1299 prefix, yes_no(s->main_pid_alien));
1304 prefix, s->pid_file);
1309 "%sBus Name Good: %s\n",
1310 prefix, s->bus_name,
1311 prefix, yes_no(s->bus_name_good));
1313 kill_context_dump(&s->kill_context, f, prefix);
1314 exec_context_dump(&s->exec_context, f, prefix);
1316 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1318 if (!s->exec_command[c])
1321 fprintf(f, "%s-> %s:\n",
1322 prefix, service_exec_command_to_string(c));
1324 exec_command_dump_list(s->exec_command[c], f, prefix2);
1327 #ifdef HAVE_SYSV_COMPAT
1330 "%sSysV Init Script has LSB Header: %s\n"
1331 "%sSysVEnabled: %s\n",
1332 prefix, yes_no(s->sysv_has_lsb),
1333 prefix, yes_no(s->sysv_enabled));
1335 if (s->sysv_start_priority >= 0)
1337 "%sSysVStartPriority: %i\n",
1338 prefix, s->sysv_start_priority);
1340 if (s->sysv_runlevels)
1341 fprintf(f, "%sSysVRunLevels: %s\n",
1342 prefix, s->sysv_runlevels);
1346 fprintf(f, "%sStatus Text: %s\n",
1347 prefix, s->status_text);
1350 static int service_load_pid_file(Service *s, bool may_warn) {
1351 _cleanup_free_ char *k = NULL;
1360 r = read_one_line_file(s->pid_file, &k);
1363 log_info_unit(UNIT(s)->id,
1364 "PID file %s not readable (yet?) after %s.",
1365 s->pid_file, service_state_to_string(s->state));
1369 r = parse_pid(k, &pid);
1372 log_info_unit(UNIT(s)->id,
1373 "Failed to read PID from file %s: %s",
1374 s->pid_file, strerror(-r));
1378 if (!pid_is_alive(pid)) {
1380 log_info_unit(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file);
1385 if (s->main_pid_known) {
1386 if (pid == s->main_pid)
1389 log_debug_unit(UNIT(s)->id,
1390 "Main PID changing: "PID_FMT" -> "PID_FMT,
1392 service_unwatch_main_pid(s);
1393 s->main_pid_known = false;
1395 log_debug_unit(UNIT(s)->id,
1396 "Main PID loaded: "PID_FMT, pid);
1398 r = service_set_main_pid(s, pid);
1402 r = unit_watch_pid(UNIT(s), pid);
1404 /* FIXME: we need to do something here */
1405 log_warning_unit(UNIT(s)->id,
1406 "Failed to watch PID "PID_FMT" from service %s",
1414 static int service_search_main_pid(Service *s) {
1420 /* If we know it anyway, don't ever fallback to unreliable
1422 if (s->main_pid_known)
1425 if (!s->guess_main_pid)
1428 assert(s->main_pid <= 0);
1430 pid = unit_search_main_pid(UNIT(s));
1434 log_debug_unit(UNIT(s)->id,
1435 "Main PID guessed: "PID_FMT, pid);
1436 r = service_set_main_pid(s, pid);
1440 r = unit_watch_pid(UNIT(s), pid);
1442 /* FIXME: we need to do something here */
1443 log_warning_unit(UNIT(s)->id,
1444 "Failed to watch PID "PID_FMT" from service %s",
1449 static void service_set_state(Service *s, ServiceState state) {
1450 ServiceState old_state;
1451 const UnitActiveState *table;
1455 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1457 old_state = s->state;
1460 service_unwatch_pid_file(s);
1463 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1465 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1467 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
1468 SERVICE_AUTO_RESTART))
1469 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1472 SERVICE_START, SERVICE_START_POST,
1473 SERVICE_RUNNING, SERVICE_RELOAD,
1474 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1476 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
1477 service_unwatch_main_pid(s);
1478 s->main_command = NULL;
1482 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1484 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1486 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
1487 service_unwatch_control_pid(s);
1488 s->control_command = NULL;
1489 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1492 if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
1493 unit_unwatch_all_pids(UNIT(s));
1496 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1497 SERVICE_RUNNING, SERVICE_RELOAD,
1498 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
1499 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) &&
1500 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1501 service_close_socket_fd(s);
1502 service_connection_unref(s);
1505 if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
1506 service_stop_watchdog(s);
1508 /* For the inactive states unit_notify() will trim the cgroup,
1509 * but for exit we have to do that ourselves... */
1510 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1511 unit_destroy_cgroup(UNIT(s));
1513 /* For remain_after_exit services, let's see if we can "release" the
1514 * hold on the console, since unit_notify() only does that in case of
1515 * change of state */
1516 if (state == SERVICE_EXITED && s->remain_after_exit &&
1517 UNIT(s)->manager->n_on_console > 0) {
1518 ExecContext *ec = unit_get_exec_context(UNIT(s));
1519 if (ec && exec_context_may_touch_console(ec)) {
1520 Manager *m = UNIT(s)->manager;
1523 if (m->n_on_console == 0)
1524 /* unset no_console_output flag, since the console is free */
1525 m->no_console_output = false;
1529 if (old_state != state)
1530 log_debug_unit(UNIT(s)->id, "%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
1532 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1533 s->reload_result = SERVICE_SUCCESS;
1536 static int service_coldplug(Unit *u) {
1537 Service *s = SERVICE(u);
1541 assert(s->state == SERVICE_DEAD);
1543 if (s->deserialized_state != s->state) {
1545 if (IN_SET(s->deserialized_state,
1546 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1548 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1550 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
1554 k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec;
1556 /* For the start/stop timeouts 0 means off */
1558 r = service_arm_timer(s, k);
1564 if (s->deserialized_state == SERVICE_AUTO_RESTART) {
1566 /* The restart timeouts 0 means immediately */
1567 r = service_arm_timer(s, s->restart_usec);
1572 if (pid_is_unwaited(s->main_pid) &&
1573 ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) ||
1574 IN_SET(s->deserialized_state,
1575 SERVICE_START, SERVICE_START_POST,
1576 SERVICE_RUNNING, SERVICE_RELOAD,
1577 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1579 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) {
1580 r = unit_watch_pid(UNIT(s), s->main_pid);
1585 if (pid_is_unwaited(s->control_pid) &&
1586 IN_SET(s->deserialized_state,
1587 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1589 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1591 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
1592 r = unit_watch_pid(UNIT(s), s->control_pid);
1597 if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
1598 unit_watch_all_pids(UNIT(s));
1600 if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
1601 service_start_watchdog(s);
1603 service_set_state(s, s->deserialized_state);
1609 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1613 unsigned rn_fds = 0;
1620 if (s->socket_fd >= 0)
1623 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1628 if (u->type != UNIT_SOCKET)
1633 r = socket_collect_fds(sock, &cfds, &cn_fds);
1646 t = new(int, rn_fds+cn_fds);
1653 memcpy(t, rfds, rn_fds * sizeof(int));
1654 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1659 rn_fds = rn_fds+cn_fds;
1674 static int service_spawn(
1679 bool apply_permissions,
1681 bool apply_tty_stdin,
1682 bool set_notify_socket,
1689 _cleanup_free_ int *fdsbuf = NULL;
1690 unsigned n_fds = 0, n_env = 0;
1691 _cleanup_strv_free_ char
1692 **argv = NULL, **final_env = NULL, **our_env = NULL;
1699 unit_realize_cgroup(UNIT(s));
1701 r = unit_setup_exec_runtime(UNIT(s));
1706 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1707 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1708 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1710 if (s->socket_fd >= 0) {
1711 fds = &s->socket_fd;
1714 r = service_collect_fds(s, &fdsbuf, &n_fds);
1722 if (timeout && s->timeout_start_usec > 0) {
1723 r = service_arm_timer(s, s->timeout_start_usec);
1727 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1729 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
1733 our_env = new0(char*, 4);
1739 if (set_notify_socket)
1740 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1745 if (s->main_pid > 0)
1746 if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) {
1751 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
1752 if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) {
1757 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1763 if (is_control && UNIT(s)->cgroup_path) {
1764 path = strappenda(UNIT(s)->cgroup_path, "/control");
1765 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1767 path = UNIT(s)->cgroup_path;
1777 UNIT(s)->manager->confirm_spawn,
1778 UNIT(s)->manager->cgroup_supported,
1780 manager_get_runtime_prefix(UNIT(s)->manager),
1783 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1789 r = unit_watch_pid(UNIT(s), pid);
1791 /* FIXME: we need to do something here */
1800 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1805 static int main_pid_good(Service *s) {
1808 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1811 /* If we know the pid file, then lets just check if it is
1813 if (s->main_pid_known) {
1815 /* If it's an alien child let's check if it is still
1817 if (s->main_pid_alien && s->main_pid > 0)
1818 return pid_is_alive(s->main_pid);
1820 /* .. otherwise assume we'll get a SIGCHLD for it,
1821 * which we really should wait for to collect exit
1822 * status and code */
1823 return s->main_pid > 0;
1826 /* We don't know the pid */
1830 _pure_ static int control_pid_good(Service *s) {
1833 return s->control_pid > 0;
1836 static int cgroup_good(Service *s) {
1841 if (!UNIT(s)->cgroup_path)
1844 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1851 static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none);
1853 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1857 if (f != SERVICE_SUCCESS)
1860 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1862 if (s->result != SERVICE_SUCCESS)
1863 service_execute_action(s, s->failure_action, "failed", false);
1865 if (allow_restart &&
1866 !s->forbid_restart &&
1867 (s->restart == SERVICE_RESTART_ALWAYS ||
1868 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1869 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1870 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1871 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1872 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1873 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1874 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1875 (s->result != SERVICE_FAILURE_SIGNAL ||
1876 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))) {
1878 r = service_arm_timer(s, s->restart_usec);
1882 service_set_state(s, SERVICE_AUTO_RESTART);
1885 s->forbid_restart = false;
1887 /* We want fresh tmpdirs in case service is started again immediately */
1888 exec_runtime_destroy(s->exec_runtime);
1889 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
1891 /* Also, remove the runtime directory in */
1892 exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
1894 /* Try to delete the pid file. At this point it will be
1895 * out-of-date, and some software might be confused by it, so
1896 * let's remove it. */
1898 unlink_noerrno(s->pid_file);
1903 log_warning_unit(UNIT(s)->id,
1904 "%s failed to run install restart timer: %s",
1905 UNIT(s)->id, strerror(-r));
1906 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1909 static void service_enter_stop_post(Service *s, ServiceResult f) {
1913 if (f != SERVICE_SUCCESS)
1916 service_unwatch_control_pid(s);
1917 unit_watch_all_pids(UNIT(s));
1919 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1920 if (s->control_command) {
1921 s->control_command_id = SERVICE_EXEC_STOP_POST;
1923 r = service_spawn(s,
1927 !s->permissions_start_only,
1928 !s->root_directory_start_only,
1936 service_set_state(s, SERVICE_STOP_POST);
1938 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1943 log_warning_unit(UNIT(s)->id,
1944 "%s failed to run 'stop-post' task: %s",
1945 UNIT(s)->id, strerror(-r));
1946 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1949 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1954 if (f != SERVICE_SUCCESS)
1957 unit_watch_all_pids(UNIT(s));
1959 r = unit_kill_context(
1962 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1971 if (s->timeout_stop_usec > 0) {
1972 r = service_arm_timer(s, s->timeout_stop_usec);
1977 service_set_state(s, state);
1978 } else if (state == SERVICE_STOP_SIGTERM)
1979 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS);
1980 else if (state == SERVICE_STOP_SIGKILL)
1981 service_enter_stop_post(s, SERVICE_SUCCESS);
1982 else if (state == SERVICE_FINAL_SIGTERM)
1983 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS);
1985 service_enter_dead(s, SERVICE_SUCCESS, true);
1990 log_warning_unit(UNIT(s)->id,
1991 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1993 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1994 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1996 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1999 static void service_enter_stop(Service *s, ServiceResult f) {
2004 if (f != SERVICE_SUCCESS)
2007 service_unwatch_control_pid(s);
2008 unit_watch_all_pids(UNIT(s));
2010 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
2011 if (s->control_command) {
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);
2063 service_reset_watchdog(s);
2065 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
2066 if (s->control_command) {
2067 s->control_command_id = SERVICE_EXEC_START_POST;
2069 r = service_spawn(s,
2073 !s->permissions_start_only,
2074 !s->root_directory_start_only,
2082 service_set_state(s, SERVICE_START_POST);
2084 service_enter_running(s, SERVICE_SUCCESS);
2089 log_warning_unit(UNIT(s)->id,
2090 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2091 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2094 static void service_kill_control_processes(Service *s) {
2097 if (!UNIT(s)->cgroup_path)
2100 p = strappenda(UNIT(s)->cgroup_path, "/control");
2101 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
2104 static void service_enter_start(Service *s) {
2111 assert(s->exec_command[SERVICE_EXEC_START]);
2112 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2114 service_unwatch_control_pid(s);
2115 service_unwatch_main_pid(s);
2117 /* We want to ensure that nobody leaks processes from
2118 * START_PRE here, so let's go on a killing spree, People
2119 * should not spawn long running processes from START_PRE. */
2120 service_kill_control_processes(s);
2122 if (s->type == SERVICE_FORKING) {
2123 s->control_command_id = SERVICE_EXEC_START;
2124 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2126 s->main_command = NULL;
2128 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2129 s->control_command = NULL;
2131 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2134 r = service_spawn(s,
2136 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
2137 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2142 s->notify_access != NOTIFY_NONE,
2148 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2149 /* For simple services we immediately start
2150 * the START_POST binaries. */
2152 service_set_main_pid(s, pid);
2153 service_enter_start_post(s);
2155 } else if (s->type == SERVICE_FORKING) {
2157 /* For forking services we wait until the start
2158 * process exited. */
2160 s->control_pid = pid;
2161 service_set_state(s, SERVICE_START);
2163 } else if (s->type == SERVICE_ONESHOT ||
2164 s->type == SERVICE_DBUS ||
2165 s->type == SERVICE_NOTIFY) {
2167 /* For oneshot services we wait until the start
2168 * process exited, too, but it is our main process. */
2170 /* For D-Bus services we know the main pid right away,
2171 * but wait for the bus name to appear on the
2172 * bus. Notify services are similar. */
2174 service_set_main_pid(s, pid);
2175 service_set_state(s, SERVICE_START);
2177 assert_not_reached("Unknown service type");
2182 log_warning_unit(UNIT(s)->id,
2183 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2184 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2187 static void service_enter_start_pre(Service *s) {
2192 service_unwatch_control_pid(s);
2194 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
2195 if (s->control_command) {
2196 /* Before we start anything, let's clear up what might
2197 * be left from previous runs. */
2198 service_kill_control_processes(s);
2200 s->control_command_id = SERVICE_EXEC_START_PRE;
2202 r = service_spawn(s,
2206 !s->permissions_start_only,
2207 !s->root_directory_start_only,
2215 service_set_state(s, SERVICE_START_PRE);
2217 service_enter_start(s);
2222 log_warning_unit(UNIT(s)->id,
2223 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2224 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2227 static void service_enter_restart(Service *s) {
2228 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2233 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2234 /* Don't restart things if we are going down anyway */
2235 log_info_unit(UNIT(s)->id,
2236 "Stop job pending for unit, delaying automatic restart.");
2238 r = service_arm_timer(s, s->restart_usec);
2245 /* Any units that are bound to this service must also be
2246 * restarted. We use JOB_RESTART (instead of the more obvious
2247 * JOB_START) here so that those dependency jobs will be added
2249 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2253 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2254 * it will be canceled as part of the service_stop() call that
2255 * is executed as part of JOB_RESTART. */
2257 log_debug_unit(UNIT(s)->id,
2258 "%s scheduled restart job.", UNIT(s)->id);
2262 log_warning_unit(UNIT(s)->id,
2263 "%s failed to schedule restart job: %s",
2264 UNIT(s)->id, bus_error_message(&error, -r));
2265 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2268 static void service_enter_reload(Service *s) {
2273 service_unwatch_control_pid(s);
2275 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
2276 if (s->control_command) {
2277 s->control_command_id = SERVICE_EXEC_RELOAD;
2279 r = service_spawn(s,
2283 !s->permissions_start_only,
2284 !s->root_directory_start_only,
2292 service_set_state(s, SERVICE_RELOAD);
2294 service_enter_running(s, SERVICE_SUCCESS);
2299 log_warning_unit(UNIT(s)->id,
2300 "%s failed to run 'reload' task: %s",
2301 UNIT(s)->id, strerror(-r));
2302 s->reload_result = SERVICE_FAILURE_RESOURCES;
2303 service_enter_running(s, SERVICE_SUCCESS);
2306 static void service_run_next_control(Service *s) {
2310 assert(s->control_command);
2311 assert(s->control_command->command_next);
2313 assert(s->control_command_id != SERVICE_EXEC_START);
2315 s->control_command = s->control_command->command_next;
2316 service_unwatch_control_pid(s);
2318 r = service_spawn(s,
2322 !s->permissions_start_only,
2323 !s->root_directory_start_only,
2324 s->control_command_id == SERVICE_EXEC_START_PRE ||
2325 s->control_command_id == SERVICE_EXEC_STOP_POST,
2335 log_warning_unit(UNIT(s)->id,
2336 "%s failed to run next control task: %s",
2337 UNIT(s)->id, strerror(-r));
2339 if (s->state == SERVICE_START_PRE)
2340 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2341 else if (s->state == SERVICE_STOP)
2342 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2343 else if (s->state == SERVICE_STOP_POST)
2344 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2345 else if (s->state == SERVICE_RELOAD) {
2346 s->reload_result = SERVICE_FAILURE_RESOURCES;
2347 service_enter_running(s, SERVICE_SUCCESS);
2349 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2352 static void service_run_next_main(Service *s) {
2357 assert(s->main_command);
2358 assert(s->main_command->command_next);
2359 assert(s->type == SERVICE_ONESHOT);
2361 s->main_command = s->main_command->command_next;
2362 service_unwatch_main_pid(s);
2364 r = service_spawn(s,
2371 s->notify_access != NOTIFY_NONE,
2377 service_set_main_pid(s, pid);
2382 log_warning_unit(UNIT(s)->id,
2383 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2384 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2387 static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none) {
2390 if (action == SERVICE_FAILURE_ACTION_REBOOT ||
2391 action == SERVICE_FAILURE_ACTION_REBOOT_FORCE)
2392 update_reboot_param_file(s->reboot_arg);
2396 case SERVICE_FAILURE_ACTION_NONE:
2397 if (log_action_none)
2398 log_warning_unit(UNIT(s)->id,
2399 "%s %s, refusing to start.", UNIT(s)->id, reason);
2402 case SERVICE_FAILURE_ACTION_REBOOT: {
2403 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2406 log_warning_unit(UNIT(s)->id,
2407 "%s %s, rebooting.", UNIT(s)->id, reason);
2409 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
2410 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
2411 true, &error, NULL);
2413 log_error_unit(UNIT(s)->id,
2414 "Failed to reboot: %s.", bus_error_message(&error, r));
2419 case SERVICE_FAILURE_ACTION_REBOOT_FORCE:
2420 log_warning_unit(UNIT(s)->id,
2421 "%s %s, forcibly rebooting.", UNIT(s)->id, reason);
2422 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2425 case SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE:
2426 log_warning_unit(UNIT(s)->id,
2427 "%s %s, rebooting immediately.", UNIT(s)->id, reason);
2429 if (s->reboot_arg) {
2430 log_info("Rebooting with argument '%s'.", s->reboot_arg);
2431 syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
2432 LINUX_REBOOT_CMD_RESTART2, s->reboot_arg);
2435 log_info("Rebooting.");
2436 reboot(RB_AUTOBOOT);
2440 log_error_unit(UNIT(s)->id,
2441 "failure action=%i", action);
2442 assert_not_reached("Unknown FailureAction.");
2448 static int service_start_limit_test(Service *s) {
2451 if (ratelimit_test(&s->start_limit))
2454 return service_execute_action(s, s->start_limit_action, "start request repeated too quickly", true);
2457 static int service_start(Unit *u) {
2458 Service *s = SERVICE(u);
2463 /* We cannot fulfill this request right now, try again later
2465 if (s->state == SERVICE_STOP ||
2466 s->state == SERVICE_STOP_SIGTERM ||
2467 s->state == SERVICE_STOP_SIGKILL ||
2468 s->state == SERVICE_STOP_POST ||
2469 s->state == SERVICE_FINAL_SIGTERM ||
2470 s->state == SERVICE_FINAL_SIGKILL)
2473 /* Already on it! */
2474 if (s->state == SERVICE_START_PRE ||
2475 s->state == SERVICE_START ||
2476 s->state == SERVICE_START_POST)
2479 /* A service that will be restarted must be stopped first to
2480 * trigger BindsTo and/or OnFailure dependencies. If a user
2481 * does not want to wait for the holdoff time to elapse, the
2482 * service should be manually restarted, not started. We
2483 * simply return EAGAIN here, so that any start jobs stay
2484 * queued, and assume that the auto restart timer will
2485 * eventually trigger the restart. */
2486 if (s->state == SERVICE_AUTO_RESTART)
2489 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2491 /* Make sure we don't enter a busy loop of some kind. */
2492 r = service_start_limit_test(s);
2494 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2498 s->result = SERVICE_SUCCESS;
2499 s->reload_result = SERVICE_SUCCESS;
2500 s->main_pid_known = false;
2501 s->main_pid_alien = false;
2502 s->forbid_restart = false;
2504 service_enter_start_pre(s);
2508 static int service_stop(Unit *u) {
2509 Service *s = SERVICE(u);
2513 /* Don't create restart jobs from here. */
2514 s->forbid_restart = true;
2517 if (s->state == SERVICE_STOP ||
2518 s->state == SERVICE_STOP_SIGTERM ||
2519 s->state == SERVICE_STOP_SIGKILL ||
2520 s->state == SERVICE_STOP_POST ||
2521 s->state == SERVICE_FINAL_SIGTERM ||
2522 s->state == SERVICE_FINAL_SIGKILL)
2525 /* A restart will be scheduled or is in progress. */
2526 if (s->state == SERVICE_AUTO_RESTART) {
2527 service_set_state(s, SERVICE_DEAD);
2531 /* If there's already something running we go directly into
2533 if (s->state == SERVICE_START_PRE ||
2534 s->state == SERVICE_START ||
2535 s->state == SERVICE_START_POST ||
2536 s->state == SERVICE_RELOAD) {
2537 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2541 assert(s->state == SERVICE_RUNNING ||
2542 s->state == SERVICE_EXITED);
2544 service_enter_stop(s, SERVICE_SUCCESS);
2548 static int service_reload(Unit *u) {
2549 Service *s = SERVICE(u);
2553 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2555 service_enter_reload(s);
2559 _pure_ static bool service_can_reload(Unit *u) {
2560 Service *s = SERVICE(u);
2564 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2567 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2568 Service *s = SERVICE(u);
2574 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2575 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2576 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2578 if (s->control_pid > 0)
2579 unit_serialize_item_format(u, f, "control-pid", PID_FMT,
2582 if (s->main_pid_known && s->main_pid > 0)
2583 unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid);
2585 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2588 unit_serialize_item(u, f, "status-text", s->status_text);
2590 /* FIXME: There's a minor uncleanliness here: if there are
2591 * multiple commands attached here, we will start from the
2592 * first one again */
2593 if (s->control_command_id >= 0)
2594 unit_serialize_item(u, f, "control-command",
2595 service_exec_command_to_string(s->control_command_id));
2597 if (s->socket_fd >= 0) {
2600 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2603 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2606 if (s->main_exec_status.pid > 0) {
2607 unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT,
2608 s->main_exec_status.pid);
2609 dual_timestamp_serialize(f, "main-exec-status-start",
2610 &s->main_exec_status.start_timestamp);
2611 dual_timestamp_serialize(f, "main-exec-status-exit",
2612 &s->main_exec_status.exit_timestamp);
2614 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2615 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
2616 s->main_exec_status.code);
2617 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
2618 s->main_exec_status.status);
2621 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2622 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2624 if (s->forbid_restart)
2625 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
2630 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2631 Service *s = SERVICE(u);
2638 if (streq(key, "state")) {
2641 state = service_state_from_string(value);
2643 log_debug_unit(u->id, "Failed to parse state value %s", value);
2645 s->deserialized_state = state;
2646 } else if (streq(key, "result")) {
2649 f = service_result_from_string(value);
2651 log_debug_unit(u->id, "Failed to parse result value %s", value);
2652 else if (f != SERVICE_SUCCESS)
2655 } else if (streq(key, "reload-result")) {
2658 f = service_result_from_string(value);
2660 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2661 else if (f != SERVICE_SUCCESS)
2662 s->reload_result = f;
2664 } else if (streq(key, "control-pid")) {
2667 if (parse_pid(value, &pid) < 0)
2668 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2670 s->control_pid = pid;
2671 } else if (streq(key, "main-pid")) {
2674 if (parse_pid(value, &pid) < 0)
2675 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2677 service_set_main_pid(s, pid);
2678 unit_watch_pid(UNIT(s), pid);
2680 } else if (streq(key, "main-pid-known")) {
2683 b = parse_boolean(value);
2685 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2687 s->main_pid_known = b;
2688 } else if (streq(key, "status-text")) {
2695 free(s->status_text);
2699 } else if (streq(key, "control-command")) {
2700 ServiceExecCommand id;
2702 id = service_exec_command_from_string(value);
2704 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2706 s->control_command_id = id;
2707 s->control_command = s->exec_command[id];
2709 } else if (streq(key, "socket-fd")) {
2712 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2713 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2716 asynchronous_close(s->socket_fd);
2717 s->socket_fd = fdset_remove(fds, fd);
2719 } else if (streq(key, "main-exec-status-pid")) {
2722 if (parse_pid(value, &pid) < 0)
2723 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2725 s->main_exec_status.pid = pid;
2726 } else if (streq(key, "main-exec-status-code")) {
2729 if (safe_atoi(value, &i) < 0)
2730 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2732 s->main_exec_status.code = i;
2733 } else if (streq(key, "main-exec-status-status")) {
2736 if (safe_atoi(value, &i) < 0)
2737 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2739 s->main_exec_status.status = i;
2740 } else if (streq(key, "main-exec-status-start"))
2741 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2742 else if (streq(key, "main-exec-status-exit"))
2743 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2744 else if (streq(key, "watchdog-timestamp"))
2745 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2746 else if (streq(key, "forbid-restart")) {
2749 b = parse_boolean(value);
2751 log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value);
2753 s->forbid_restart = b;
2755 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2760 _pure_ static UnitActiveState service_active_state(Unit *u) {
2761 const UnitActiveState *table;
2765 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2767 return table[SERVICE(u)->state];
2770 static const char *service_sub_state_to_string(Unit *u) {
2773 return service_state_to_string(SERVICE(u)->state);
2776 static bool service_check_gc(Unit *u) {
2777 Service *s = SERVICE(u);
2781 /* Never clean up services that still have a process around,
2782 * even if the service is formally dead. */
2783 if (cgroup_good(s) > 0 ||
2784 main_pid_good(s) > 0 ||
2785 control_pid_good(s) > 0)
2788 #ifdef HAVE_SYSV_COMPAT
2796 _pure_ static bool service_check_snapshot(Unit *u) {
2797 Service *s = SERVICE(u);
2801 return (s->socket_fd < 0);
2804 static int service_retry_pid_file(Service *s) {
2807 assert(s->pid_file);
2808 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2810 r = service_load_pid_file(s, false);
2814 service_unwatch_pid_file(s);
2816 service_enter_running(s, SERVICE_SUCCESS);
2820 static int service_watch_pid_file(Service *s) {
2823 log_debug_unit(UNIT(s)->id,
2824 "Setting watch for %s's PID file %s",
2825 UNIT(s)->id, s->pid_file_pathspec->path);
2826 r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
2830 /* the pidfile might have appeared just before we set the watch */
2831 log_debug_unit(UNIT(s)->id,
2832 "Trying to read %s's PID file %s in case it changed",
2833 UNIT(s)->id, s->pid_file_pathspec->path);
2834 service_retry_pid_file(s);
2838 log_error_unit(UNIT(s)->id,
2839 "Failed to set a watch for %s's PID file %s: %s",
2840 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2841 service_unwatch_pid_file(s);
2845 static int service_demand_pid_file(Service *s) {
2848 assert(s->pid_file);
2849 assert(!s->pid_file_pathspec);
2851 ps = new0(PathSpec, 1);
2856 ps->path = strdup(s->pid_file);
2862 path_kill_slashes(ps->path);
2864 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2865 * keep their PID file open all the time. */
2866 ps->type = PATH_MODIFIED;
2867 ps->inotify_fd = -1;
2869 s->pid_file_pathspec = ps;
2871 return service_watch_pid_file(s);
2874 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
2875 PathSpec *p = userdata;
2880 s = SERVICE(p->unit);
2884 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2885 assert(s->pid_file_pathspec);
2886 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2888 log_debug_unit(UNIT(s)->id, "inotify event for %s", UNIT(s)->id);
2890 if (path_spec_fd_event(p, events) < 0)
2893 if (service_retry_pid_file(s) == 0)
2896 if (service_watch_pid_file(s) < 0)
2902 service_unwatch_pid_file(s);
2903 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2907 static void service_notify_cgroup_empty_event(Unit *u) {
2908 Service *s = SERVICE(u);
2912 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
2916 /* Waiting for SIGCHLD is usually more interesting,
2917 * because it includes return codes/signals. Which is
2918 * why we ignore the cgroup events for most cases,
2919 * except when we don't know pid which to expect the
2923 case SERVICE_START_POST:
2924 /* If we were hoping for the daemon to write its PID file,
2925 * we can give up now. */
2926 if (s->pid_file_pathspec) {
2927 log_warning_unit(u->id,
2928 "%s never wrote its PID file. Failing.", UNIT(s)->id);
2929 service_unwatch_pid_file(s);
2930 if (s->state == SERVICE_START)
2931 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2933 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2937 case SERVICE_RUNNING:
2938 /* service_enter_running() will figure out what to do */
2939 service_enter_running(s, SERVICE_SUCCESS);
2942 case SERVICE_STOP_SIGTERM:
2943 case SERVICE_STOP_SIGKILL:
2945 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2946 service_enter_stop_post(s, SERVICE_SUCCESS);
2950 case SERVICE_STOP_POST:
2951 case SERVICE_FINAL_SIGTERM:
2952 case SERVICE_FINAL_SIGKILL:
2953 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2954 service_enter_dead(s, SERVICE_SUCCESS, true);
2963 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2964 Service *s = SERVICE(u);
2970 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2971 is_clean_exit_lsb(code, status, &s->success_status))
2972 f = SERVICE_SUCCESS;
2973 else if (code == CLD_EXITED)
2974 f = SERVICE_FAILURE_EXIT_CODE;
2975 else if (code == CLD_KILLED)
2976 f = SERVICE_FAILURE_SIGNAL;
2977 else if (code == CLD_DUMPED)
2978 f = SERVICE_FAILURE_CORE_DUMP;
2980 assert_not_reached("Unknown code");
2982 if (s->main_pid == pid) {
2983 /* Forking services may occasionally move to a new PID.
2984 * As long as they update the PID file before exiting the old
2985 * PID, they're fine. */
2986 if (service_load_pid_file(s, false) == 0)
2990 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2992 if (s->main_command) {
2993 /* If this is not a forking service than the
2994 * main process got started and hence we copy
2995 * the exit status so that it is recorded both
2996 * as main and as control process exit
2999 s->main_command->exec_status = s->main_exec_status;
3001 if (s->main_command->ignore)
3002 f = SERVICE_SUCCESS;
3003 } else if (s->exec_command[SERVICE_EXEC_START]) {
3005 /* If this is a forked process, then we should
3006 * ignore the return value if this was
3007 * configured for the starter process */
3009 if (s->exec_command[SERVICE_EXEC_START]->ignore)
3010 f = SERVICE_SUCCESS;
3013 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
3015 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
3016 u->id, sigchld_code_to_string(code), status,
3017 strna(code == CLD_EXITED
3018 ? exit_status_to_string(status, EXIT_STATUS_FULL)
3019 : signal_to_string(status)),
3020 "EXIT_CODE=%s", sigchld_code_to_string(code),
3021 "EXIT_STATUS=%i", status,
3024 if (f != SERVICE_SUCCESS)
3027 if (s->main_command &&
3028 s->main_command->command_next &&
3029 f == SERVICE_SUCCESS) {
3031 /* There is another command to *
3032 * execute, so let's do that. */
3034 log_debug_unit(u->id,
3035 "%s running next main command for state %s",
3036 u->id, service_state_to_string(s->state));
3037 service_run_next_main(s);
3041 /* The service exited, so the service is officially
3043 s->main_command = NULL;
3047 case SERVICE_START_POST:
3048 case SERVICE_RELOAD:
3050 /* Need to wait until the operation is
3055 if (s->type == SERVICE_ONESHOT) {
3056 /* This was our main goal, so let's go on */
3057 if (f == SERVICE_SUCCESS)
3058 service_enter_start_post(s);
3060 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3066 case SERVICE_RUNNING:
3067 service_enter_running(s, f);
3070 case SERVICE_STOP_SIGTERM:
3071 case SERVICE_STOP_SIGKILL:
3073 if (!control_pid_good(s))
3074 service_enter_stop_post(s, f);
3076 /* If there is still a control process, wait for that first */
3079 case SERVICE_STOP_POST:
3080 case SERVICE_FINAL_SIGTERM:
3081 case SERVICE_FINAL_SIGKILL:
3083 if (!control_pid_good(s))
3084 service_enter_dead(s, f, true);
3088 assert_not_reached("Uh, main process died at wrong time.");
3092 } else if (s->control_pid == pid) {
3095 if (s->control_command) {
3096 exec_status_exit(&s->control_command->exec_status,
3097 &s->exec_context, pid, code, status);
3099 if (s->control_command->ignore)
3100 f = SERVICE_SUCCESS;
3103 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
3104 "%s: control process exited, code=%s status=%i",
3105 u->id, sigchld_code_to_string(code), status);
3107 if (f != SERVICE_SUCCESS)
3110 /* Immediately get rid of the cgroup, so that the
3111 * kernel doesn't delay the cgroup empty messages for
3112 * the service cgroup any longer than necessary */
3113 service_kill_control_processes(s);
3115 if (s->control_command &&
3116 s->control_command->command_next &&
3117 f == SERVICE_SUCCESS) {
3119 /* There is another command to *
3120 * execute, so let's do that. */
3122 log_debug_unit(u->id,
3123 "%s running next control command for state %s",
3124 u->id, service_state_to_string(s->state));
3125 service_run_next_control(s);
3128 /* No further commands for this step, so let's
3129 * figure out what to do next */
3131 s->control_command = NULL;
3132 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3134 log_debug_unit(u->id,
3135 "%s got final SIGCHLD for state %s",
3136 u->id, service_state_to_string(s->state));
3140 case SERVICE_START_PRE:
3141 if (f == SERVICE_SUCCESS)
3142 service_enter_start(s);
3144 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3148 if (s->type != SERVICE_FORKING)
3149 /* Maybe spurious event due to a reload that changed the type? */
3152 if (f != SERVICE_SUCCESS) {
3153 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3158 bool has_start_post;
3161 /* Let's try to load the pid file here if we can.
3162 * The PID file might actually be created by a START_POST
3163 * script. In that case don't worry if the loading fails. */
3165 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3166 r = service_load_pid_file(s, !has_start_post);
3167 if (!has_start_post && r < 0) {
3168 r = service_demand_pid_file(s);
3169 if (r < 0 || !cgroup_good(s))
3170 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3174 service_search_main_pid(s);
3176 service_enter_start_post(s);
3179 case SERVICE_START_POST:
3180 if (f != SERVICE_SUCCESS) {
3181 service_enter_stop(s, f);
3188 r = service_load_pid_file(s, true);
3190 r = service_demand_pid_file(s);
3191 if (r < 0 || !cgroup_good(s))
3192 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3196 service_search_main_pid(s);
3198 service_enter_running(s, SERVICE_SUCCESS);
3201 case SERVICE_RELOAD:
3202 if (f == SERVICE_SUCCESS) {
3203 service_load_pid_file(s, true);
3204 service_search_main_pid(s);
3207 s->reload_result = f;
3208 service_enter_running(s, SERVICE_SUCCESS);
3212 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3215 case SERVICE_STOP_SIGTERM:
3216 case SERVICE_STOP_SIGKILL:
3217 if (main_pid_good(s) <= 0)
3218 service_enter_stop_post(s, f);
3220 /* If there is still a service
3221 * process around, wait until
3222 * that one quit, too */
3225 case SERVICE_STOP_POST:
3226 case SERVICE_FINAL_SIGTERM:
3227 case SERVICE_FINAL_SIGKILL:
3228 if (main_pid_good(s) <= 0)
3229 service_enter_dead(s, f, true);
3233 assert_not_reached("Uh, control process died at wrong time.");
3238 /* Notify clients about changed exit status */
3239 unit_add_to_dbus_queue(u);
3241 /* We got one SIGCHLD for the service, let's watch all
3242 * processes that are now running of the service, and watch
3243 * that. Among the PIDs we then watch will be children
3244 * reassigned to us, which hopefully allows us to identify
3245 * when all children are gone */
3246 unit_tidy_watch_pids(u, s->main_pid, s->control_pid);
3247 unit_watch_all_pids(u);
3249 /* If the PID set is empty now, then let's finish this off */
3250 if (set_isempty(u->pids))
3251 service_notify_cgroup_empty_event(u);
3254 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
3255 Service *s = SERVICE(userdata);
3258 assert(source == s->timer_event_source);
3262 case SERVICE_START_PRE:
3264 log_warning_unit(UNIT(s)->id,
3265 "%s %s operation timed out. Terminating.",
3267 s->state == SERVICE_START ? "start" : "start-pre");
3268 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3271 case SERVICE_START_POST:
3272 log_warning_unit(UNIT(s)->id,
3273 "%s start-post operation timed out. Stopping.", UNIT(s)->id);
3274 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3277 case SERVICE_RELOAD:
3278 log_warning_unit(UNIT(s)->id,
3279 "%s reload operation timed out. Stopping.", UNIT(s)->id);
3280 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3281 service_enter_running(s, SERVICE_SUCCESS);
3285 log_warning_unit(UNIT(s)->id,
3286 "%s stopping timed out. Terminating.", UNIT(s)->id);
3287 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3290 case SERVICE_STOP_SIGTERM:
3291 if (s->kill_context.send_sigkill) {
3292 log_warning_unit(UNIT(s)->id,
3293 "%s stop-sigterm timed out. Killing.", UNIT(s)->id);
3294 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3296 log_warning_unit(UNIT(s)->id,
3297 "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s)->id);
3298 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3303 case SERVICE_STOP_SIGKILL:
3304 /* Uh, we sent a SIGKILL and it is still not gone?
3305 * Must be something we cannot kill, so let's just be
3306 * weirded out and continue */
3308 log_warning_unit(UNIT(s)->id,
3309 "%s still around after SIGKILL. Ignoring.", UNIT(s)->id);
3310 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3313 case SERVICE_STOP_POST:
3314 log_warning_unit(UNIT(s)->id,
3315 "%s stop-post timed out. Terminating.", UNIT(s)->id);
3316 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3319 case SERVICE_FINAL_SIGTERM:
3320 if (s->kill_context.send_sigkill) {
3321 log_warning_unit(UNIT(s)->id,
3322 "%s stop-final-sigterm timed out. Killing.", UNIT(s)->id);
3323 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3325 log_warning_unit(UNIT(s)->id,
3326 "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.",
3328 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3333 case SERVICE_FINAL_SIGKILL:
3334 log_warning_unit(UNIT(s)->id,
3335 "%s still around after final SIGKILL. Entering failed mode.", UNIT(s)->id);
3336 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3339 case SERVICE_AUTO_RESTART:
3340 log_info_unit(UNIT(s)->id,
3341 s->restart_usec > 0 ?
3342 "%s holdoff time over, scheduling restart." :
3343 "%s has no holdoff time, scheduling restart.",
3345 service_enter_restart(s);
3349 assert_not_reached("Timeout at wrong time.");
3355 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
3356 Service *s = SERVICE(userdata);
3359 assert(source == s->watchdog_event_source);
3361 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
3362 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_WATCHDOG);
3367 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3368 Service *s = SERVICE(u);
3370 bool notify_dbus = false;
3374 log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT" (%s...)",
3375 u->id, pid, tags && *tags ? tags[0] : "(empty)");
3377 if (s->notify_access == NOTIFY_NONE) {
3378 log_warning_unit(u->id,
3379 "%s: Got notification message from PID "PID_FMT", but reception is disabled.",
3384 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3386 if (s->main_pid != 0)
3387 log_warning_unit(u->id, "%s: Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, u->id, pid, s->main_pid);
3389 log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT", but reception only permitted for main PID which is currently not known", u->id, pid);
3393 /* Interpret MAINPID= */
3394 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3395 (s->state == SERVICE_START ||
3396 s->state == SERVICE_START_POST ||
3397 s->state == SERVICE_RUNNING ||
3398 s->state == SERVICE_RELOAD)) {
3400 if (parse_pid(e + 8, &pid) < 0)
3401 log_warning_unit(u->id, "Failed to parse notification message %s", e);
3403 log_debug_unit(u->id, "%s: got %s", u->id, e);
3404 service_set_main_pid(s, pid);
3405 unit_watch_pid(UNIT(s), pid);
3410 /* Interpret READY= */
3411 if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START && strv_find(tags, "READY=1")) {
3412 log_debug_unit(u->id, "%s: got READY=1", u->id);
3413 service_enter_start_post(s);
3417 /* Interpret STATUS= */
3418 e = strv_find_prefix(tags, "STATUS=");
3423 if (!utf8_is_valid(e+7)) {
3424 log_warning_unit(u->id, "Status message in notification is not UTF-8 clean.");
3428 log_debug_unit(u->id, "%s: got %s", u->id, e);
3439 if (!streq_ptr(s->status_text, t)) {
3440 free(s->status_text);
3447 /* Interpret WATCHDOG= */
3448 if (strv_find(tags, "WATCHDOG=1")) {
3449 log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id);
3450 service_reset_watchdog(s);
3453 /* Notify clients about changed status or main pid */
3455 unit_add_to_dbus_queue(u);
3458 static int service_get_timeout(Unit *u, uint64_t *timeout) {
3459 Service *s = SERVICE(u);
3462 if (!s->timer_event_source)
3465 r = sd_event_source_get_time(s->timer_event_source, timeout);
3472 #ifdef HAVE_SYSV_COMPAT
3474 static int service_enumerate(Manager *m) {
3477 _cleanup_closedir_ DIR *d = NULL;
3478 _cleanup_free_ char *path = NULL, *fpath = NULL, *name = NULL;
3479 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
3480 _cleanup_set_free_ Set *shutdown_services = NULL;
3487 if (m->running_as != SYSTEMD_SYSTEM)
3490 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3491 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3495 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3506 if (errno != ENOENT)
3507 log_warning("opendir(%s) failed: %m", path);
3512 while ((de = readdir(d))) {
3515 if (ignore_file(de->d_name))
3518 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3521 if (strlen(de->d_name) < 4)
3524 a = undecchar(de->d_name[1]);
3525 b = undecchar(de->d_name[2]);
3531 fpath = strjoin(path, "/", de->d_name, NULL);
3537 if (access(fpath, X_OK) < 0) {
3539 if (errno != ENOENT)
3540 log_warning("access() failed on %s: %m", fpath);
3546 name = sysv_translate_name(de->d_name + 3);
3552 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3554 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3558 if (de->d_name[0] == 'S') {
3560 if (rcnd_table[i].type == RUNLEVEL_UP) {
3561 SERVICE(service)->sysv_start_priority_from_rcnd =
3562 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3564 SERVICE(service)->sysv_enabled = true;
3567 r = set_ensure_allocated(&runlevel_services[i],
3568 trivial_hash_func, trivial_compare_func);
3572 r = set_put(runlevel_services[i], service);
3576 } else if (de->d_name[0] == 'K' &&
3577 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3579 r = set_ensure_allocated(&shutdown_services,
3580 trivial_hash_func, trivial_compare_func);
3584 r = set_put(shutdown_services, service);
3591 /* Now we loaded all stubs and are aware of the lowest
3592 start-up priority for all services, not let's actually load
3593 the services, this will also tell us which services are
3594 actually native now */
3595 manager_dispatch_load_queue(m);
3597 /* If this is a native service, rely on native ways to pull in
3598 * a service, don't pull it in via sysv rcN.d links. */
3599 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3600 SET_FOREACH(service, runlevel_services[i], j) {
3601 service = unit_follow_merge(service);
3603 if (service->fragment_path)
3606 r = unit_add_two_dependencies_by_name_inverse(
3607 service, UNIT_AFTER, UNIT_WANTS,
3608 rcnd_table[i].target, NULL, true);
3613 /* We honour K links only for halt/reboot. For the normal
3614 * runlevels we assume the stop jobs will be implicitly added
3615 * by the core logic. Also, we don't really distinguish here
3616 * between the runlevels 0 and 6 and just add them to the
3617 * special shutdown target. */
3618 SET_FOREACH(service, shutdown_services, j) {
3619 service = unit_follow_merge(service);
3621 if (service->fragment_path)
3624 r = unit_add_two_dependencies_by_name(
3625 service, UNIT_BEFORE, UNIT_CONFLICTS,
3626 SPECIAL_SHUTDOWN_TARGET, NULL, true);
3635 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3636 set_free(runlevel_services[i]);
3642 static void service_bus_name_owner_change(
3645 const char *old_owner,
3646 const char *new_owner) {
3648 Service *s = SERVICE(u);
3654 assert(streq(s->bus_name, name));
3655 assert(old_owner || new_owner);
3657 if (old_owner && new_owner)
3658 log_debug_unit(u->id,
3659 "%s's D-Bus name %s changed owner from %s to %s",
3660 u->id, name, old_owner, new_owner);
3662 log_debug_unit(u->id,
3663 "%s's D-Bus name %s no longer registered by %s",
3664 u->id, name, old_owner);
3666 log_debug_unit(u->id,
3667 "%s's D-Bus name %s now registered by %s",
3668 u->id, name, new_owner);
3670 s->bus_name_good = !!new_owner;
3672 if (s->type == SERVICE_DBUS) {
3674 /* service_enter_running() will figure out what to
3676 if (s->state == SERVICE_RUNNING)
3677 service_enter_running(s, SERVICE_SUCCESS);
3678 else if (s->state == SERVICE_START && new_owner)
3679 service_enter_start_post(s);
3681 } else if (new_owner &&
3683 (s->state == SERVICE_START ||
3684 s->state == SERVICE_START_POST ||
3685 s->state == SERVICE_RUNNING ||
3686 s->state == SERVICE_RELOAD)) {
3688 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
3691 /* Try to acquire PID from bus service */
3693 r = sd_bus_get_owner(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
3695 r = sd_bus_creds_get_pid(creds, &pid);
3697 log_debug_unit(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3699 service_set_main_pid(s, pid);
3700 unit_watch_pid(UNIT(s), pid);
3705 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3706 _cleanup_free_ char *peer = NULL;
3712 /* This is called by the socket code when instantiating a new
3713 * service for a stream socket and the socket needs to be
3716 if (UNIT(s)->load_state != UNIT_LOADED)
3719 if (s->socket_fd >= 0)
3722 if (s->state != SERVICE_DEAD)
3725 if (getpeername_pretty(fd, &peer) >= 0) {
3727 if (UNIT(s)->description) {
3728 _cleanup_free_ char *a;
3730 a = strjoin(UNIT(s)->description, " (", peer, ")", NULL);
3734 r = unit_set_description(UNIT(s), a);
3736 r = unit_set_description(UNIT(s), peer);
3744 unit_ref_set(&s->accept_socket, UNIT(sock));
3746 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3749 static void service_reset_failed(Unit *u) {
3750 Service *s = SERVICE(u);
3754 if (s->state == SERVICE_FAILED)
3755 service_set_state(s, SERVICE_DEAD);
3757 s->result = SERVICE_SUCCESS;
3758 s->reload_result = SERVICE_SUCCESS;
3760 RATELIMIT_RESET(s->start_limit);
3763 static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
3764 Service *s = SERVICE(u);
3766 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
3769 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3770 [SERVICE_DEAD] = "dead",
3771 [SERVICE_START_PRE] = "start-pre",
3772 [SERVICE_START] = "start",
3773 [SERVICE_START_POST] = "start-post",
3774 [SERVICE_RUNNING] = "running",
3775 [SERVICE_EXITED] = "exited",
3776 [SERVICE_RELOAD] = "reload",
3777 [SERVICE_STOP] = "stop",
3778 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3779 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3780 [SERVICE_STOP_POST] = "stop-post",
3781 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3782 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3783 [SERVICE_FAILED] = "failed",
3784 [SERVICE_AUTO_RESTART] = "auto-restart",
3787 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3789 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3790 [SERVICE_RESTART_NO] = "no",
3791 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3792 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3793 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
3794 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3795 [SERVICE_RESTART_ALWAYS] = "always"
3798 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3800 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3801 [SERVICE_SIMPLE] = "simple",
3802 [SERVICE_FORKING] = "forking",
3803 [SERVICE_ONESHOT] = "oneshot",
3804 [SERVICE_DBUS] = "dbus",
3805 [SERVICE_NOTIFY] = "notify",
3806 [SERVICE_IDLE] = "idle"
3809 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3811 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3812 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3813 [SERVICE_EXEC_START] = "ExecStart",
3814 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3815 [SERVICE_EXEC_RELOAD] = "ExecReload",
3816 [SERVICE_EXEC_STOP] = "ExecStop",
3817 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3820 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3822 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3823 [NOTIFY_NONE] = "none",
3824 [NOTIFY_MAIN] = "main",
3825 [NOTIFY_ALL] = "all"
3828 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3830 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3831 [SERVICE_SUCCESS] = "success",
3832 [SERVICE_FAILURE_RESOURCES] = "resources",
3833 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3834 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3835 [SERVICE_FAILURE_SIGNAL] = "signal",
3836 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3837 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3838 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3841 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3843 static const char* const failure_action_table[_SERVICE_FAILURE_ACTION_MAX] = {
3844 [SERVICE_FAILURE_ACTION_NONE] = "none",
3845 [SERVICE_FAILURE_ACTION_REBOOT] = "reboot",
3846 [SERVICE_FAILURE_ACTION_REBOOT_FORCE] = "reboot-force",
3847 [SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate"
3849 DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction);
3851 const UnitVTable service_vtable = {
3852 .object_size = sizeof(Service),
3853 .exec_context_offset = offsetof(Service, exec_context),
3854 .cgroup_context_offset = offsetof(Service, cgroup_context),
3855 .kill_context_offset = offsetof(Service, kill_context),
3856 .exec_runtime_offset = offsetof(Service, exec_runtime),
3862 .private_section = "Service",
3864 .init = service_init,
3865 .done = service_done,
3866 .load = service_load,
3868 .coldplug = service_coldplug,
3870 .dump = service_dump,
3872 .start = service_start,
3873 .stop = service_stop,
3874 .reload = service_reload,
3876 .can_reload = service_can_reload,
3878 .kill = service_kill,
3880 .serialize = service_serialize,
3881 .deserialize_item = service_deserialize_item,
3883 .active_state = service_active_state,
3884 .sub_state_to_string = service_sub_state_to_string,
3886 .check_gc = service_check_gc,
3887 .check_snapshot = service_check_snapshot,
3889 .sigchld_event = service_sigchld_event,
3891 .reset_failed = service_reset_failed,
3893 .notify_cgroup_empty = service_notify_cgroup_empty_event,
3894 .notify_message = service_notify_message,
3896 .bus_name_owner_change = service_bus_name_owner_change,
3898 .bus_interface = "org.freedesktop.systemd1.Service",
3899 .bus_vtable = bus_service_vtable,
3900 .bus_set_property = bus_service_set_property,
3901 .bus_commit_properties = bus_service_commit_properties,
3903 .get_timeout = service_get_timeout,
3905 #ifdef HAVE_SYSV_COMPAT
3906 .enumerate = service_enumerate,
3909 .can_transient = true,
3911 .status_message_formats = {
3912 .starting_stopping = {
3913 [0] = "Starting %s...",
3914 [1] = "Stopping %s...",
3916 .finished_start_job = {
3917 [JOB_DONE] = "Started %s.",
3918 [JOB_FAILED] = "Failed to start %s.",
3919 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3920 [JOB_TIMEOUT] = "Timed out starting %s.",
3922 .finished_stop_job = {
3923 [JOB_DONE] = "Stopped %s.",
3924 [JOB_FAILED] = "Stopped (with error) %s.",
3925 [JOB_TIMEOUT] = "Timed out stopping %s.",