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>
33 #include "load-fragment.h"
34 #include "load-dropin.h"
37 #include "unit-name.h"
38 #include "unit-printf.h"
39 #include "dbus-service.h"
41 #include "exit-status.h"
43 #include "path-util.h"
48 #include "bus-error.h"
51 #ifdef HAVE_SYSV_COMPAT
53 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
55 typedef enum RunlevelType {
63 const RunlevelType type;
65 /* Standard SysV runlevels for start-up */
66 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
67 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
68 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
69 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
70 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
72 /* Standard SysV runlevels for shutdown */
73 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
74 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
76 /* Note that the order here matters, as we read the
77 directories in this order, and we want to make sure that
78 sysv_start_priority is known when we first load the
79 unit. And that value we only know from S links. Hence
80 UP must be read before DOWN */
83 #define RUNLEVELS_UP "12345"
86 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
87 [SERVICE_DEAD] = UNIT_INACTIVE,
88 [SERVICE_START_PRE] = UNIT_ACTIVATING,
89 [SERVICE_START] = UNIT_ACTIVATING,
90 [SERVICE_START_POST] = UNIT_ACTIVATING,
91 [SERVICE_RUNNING] = UNIT_ACTIVE,
92 [SERVICE_EXITED] = UNIT_ACTIVE,
93 [SERVICE_RELOAD] = UNIT_RELOADING,
94 [SERVICE_STOP] = UNIT_DEACTIVATING,
95 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
96 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
97 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
98 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
99 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
100 [SERVICE_FAILED] = UNIT_FAILED,
101 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
104 /* For Type=idle we never want to delay any other jobs, hence we
105 * consider idle jobs active as soon as we start working on them */
106 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
107 [SERVICE_DEAD] = UNIT_INACTIVE,
108 [SERVICE_START_PRE] = UNIT_ACTIVE,
109 [SERVICE_START] = UNIT_ACTIVE,
110 [SERVICE_START_POST] = UNIT_ACTIVE,
111 [SERVICE_RUNNING] = UNIT_ACTIVE,
112 [SERVICE_EXITED] = UNIT_ACTIVE,
113 [SERVICE_RELOAD] = UNIT_RELOADING,
114 [SERVICE_STOP] = UNIT_DEACTIVATING,
115 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
116 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
117 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
118 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
119 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
120 [SERVICE_FAILED] = UNIT_FAILED,
121 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
124 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata);
125 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
126 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata);
128 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
130 static void service_init(Unit *u) {
131 Service *s = SERVICE(u);
134 assert(u->load_state == UNIT_STUB);
136 s->timeout_start_usec = u->manager->default_timeout_start_usec;
137 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
138 s->restart_usec = u->manager->default_restart_usec;
139 s->type = _SERVICE_TYPE_INVALID;
141 #ifdef HAVE_SYSV_COMPAT
142 s->sysv_start_priority = -1;
143 s->sysv_start_priority_from_rcnd = -1;
146 s->guess_main_pid = true;
148 RATELIMIT_INIT(s->start_limit, u->manager->default_start_limit_interval, u->manager->default_start_limit_burst);
150 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
153 static void service_unwatch_control_pid(Service *s) {
156 if (s->control_pid <= 0)
159 unit_unwatch_pid(UNIT(s), s->control_pid);
163 static void service_unwatch_main_pid(Service *s) {
166 if (s->main_pid <= 0)
169 unit_unwatch_pid(UNIT(s), s->main_pid);
173 static void service_unwatch_pid_file(Service *s) {
174 if (!s->pid_file_pathspec)
177 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
178 UNIT(s)->id, s->pid_file_pathspec->path);
179 path_spec_unwatch(s->pid_file_pathspec);
180 path_spec_done(s->pid_file_pathspec);
181 free(s->pid_file_pathspec);
182 s->pid_file_pathspec = NULL;
185 static int service_set_main_pid(Service *s, pid_t pid) {
196 if (s->main_pid == pid && s->main_pid_known)
199 if (s->main_pid != pid) {
200 service_unwatch_main_pid(s);
201 exec_status_start(&s->main_exec_status, pid);
205 s->main_pid_known = true;
207 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
208 log_warning_unit(UNIT(s)->id,
209 "%s: Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.",
212 s->main_pid_alien = true;
214 s->main_pid_alien = false;
219 static void service_close_socket_fd(Service *s) {
222 if (s->socket_fd < 0)
225 s->socket_fd = safe_close(s->socket_fd);
228 static void service_connection_unref(Service *s) {
231 if (!UNIT_ISSET(s->accept_socket))
234 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
235 unit_ref_unset(&s->accept_socket);
238 static void service_stop_watchdog(Service *s) {
241 s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source);
242 s->watchdog_timestamp = DUAL_TIMESTAMP_NULL;
245 static void service_start_watchdog(Service *s) {
250 if (s->watchdog_usec <= 0)
253 if (s->watchdog_event_source) {
254 r = sd_event_source_set_time(s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec);
256 log_warning_unit(UNIT(s)->id, "%s failed to reset watchdog timer: %s", UNIT(s)->id, strerror(-r));
260 r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT);
262 r = sd_event_add_time(
263 UNIT(s)->manager->event,
264 &s->watchdog_event_source,
266 s->watchdog_timestamp.monotonic + s->watchdog_usec, 0,
267 service_dispatch_watchdog, s);
269 log_warning_unit(UNIT(s)->id, "%s failed to add watchdog timer: %s", UNIT(s)->id, strerror(-r));
273 /* Let's process everything else which might be a sign
274 * of living before we consider a service died. */
275 r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE);
279 log_warning_unit(UNIT(s)->id, "%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
282 static void service_reset_watchdog(Service *s) {
285 dual_timestamp_get(&s->watchdog_timestamp);
286 service_start_watchdog(s);
289 static void service_done(Unit *u) {
290 Service *s = SERVICE(u);
297 #ifdef HAVE_SYSV_COMPAT
298 free(s->sysv_runlevels);
299 s->sysv_runlevels = NULL;
302 free(s->status_text);
303 s->status_text = NULL;
306 s->reboot_arg = NULL;
308 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
309 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
310 s->control_command = NULL;
311 s->main_command = NULL;
313 set_free(s->restart_ignore_status.code);
314 s->restart_ignore_status.code = NULL;
315 set_free(s->restart_ignore_status.signal);
316 s->restart_ignore_status.signal = NULL;
318 set_free(s->success_status.code);
319 s->success_status.code = NULL;
320 set_free(s->success_status.signal);
321 s->success_status.signal = NULL;
323 /* This will leak a process, but at least no memory or any of
325 service_unwatch_main_pid(s);
326 service_unwatch_control_pid(s);
327 service_unwatch_pid_file(s);
330 unit_unwatch_bus_name(u, s->bus_name);
335 service_close_socket_fd(s);
336 service_connection_unref(s);
338 unit_ref_unset(&s->accept_socket);
340 service_stop_watchdog(s);
342 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
345 static int service_arm_timer(Service *s, usec_t usec) {
350 if (s->timer_event_source) {
351 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec);
355 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
358 return sd_event_add_time(
359 UNIT(s)->manager->event,
360 &s->timer_event_source,
362 now(CLOCK_MONOTONIC) + usec, 0,
363 service_dispatch_timer, s);
366 #ifdef HAVE_SYSV_COMPAT
367 static char *sysv_translate_name(const char *name) {
370 r = new(char, strlen(name) + strlen(".service") + 1);
374 if (endswith(name, ".sh"))
375 /* Drop .sh suffix */
376 strcpy(stpcpy(r, name) - 3, ".service");
378 /* Normal init script name */
379 strcpy(stpcpy(r, name), ".service");
384 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
386 /* We silently ignore the $ prefix here. According to the LSB
387 * spec it simply indicates whether something is a
388 * standardized name or a distribution-specific one. Since we
389 * just follow what already exists and do not introduce new
390 * uses or names we don't care who introduced a new name. */
392 static const char * const table[] = {
393 /* LSB defined facilities */
395 "network", SPECIAL_NETWORK_TARGET,
396 "named", SPECIAL_NSS_LOOKUP_TARGET,
397 "portmap", SPECIAL_RPCBIND_TARGET,
398 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
400 "time", SPECIAL_TIME_SYNC_TARGET,
410 n = *name == '$' ? name + 1 : name;
412 for (i = 0; i < ELEMENTSOF(table); i += 2) {
414 if (!streq(table[i], n))
420 r = strdup(table[i+1]);
427 /* If we don't know this name, fallback heuristics to figure
428 * out whether something is a target or a service alias. */
431 if (!unit_prefix_is_valid(n))
434 /* Facilities starting with $ are most likely targets */
435 r = unit_name_build(n, NULL, ".target");
436 } else if (filename && streq(name, filename))
437 /* Names equaling the file name of the services are redundant */
440 /* Everything else we assume to be normal service names */
441 r = sysv_translate_name(n);
452 static int sysv_fix_order(Service *s) {
458 if (s->sysv_start_priority < 0)
461 /* For each pair of services where at least one lacks a LSB
462 * header, we use the start priority value to order things. */
464 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
467 bool special_s, special_t;
474 if (UNIT(t)->load_state != UNIT_LOADED)
477 if (t->sysv_start_priority < 0)
480 /* If both units have modern headers we don't care
481 * about the priorities */
482 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
483 (UNIT(t)->fragment_path || t->sysv_has_lsb))
486 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
487 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
489 if (special_t && !special_s)
491 else if (special_s && !special_t)
493 else if (t->sysv_start_priority < s->sysv_start_priority)
495 else if (t->sysv_start_priority > s->sysv_start_priority)
500 /* FIXME: Maybe we should compare the name here lexicographically? */
502 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
509 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
512 if (!(c = new0(ExecCommand, 1)))
515 if (!(c->path = strdup(path))) {
520 if (!(c->argv = strv_new(path, arg1, NULL))) {
529 static int sysv_exec_commands(Service *s, const bool supports_reload) {
534 assert(UNIT(s)->source_path);
536 c = exec_command_new(UNIT(s)->source_path, "start");
539 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
541 c = exec_command_new(UNIT(s)->source_path, "stop");
544 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
546 if (supports_reload) {
547 c = exec_command_new(UNIT(s)->source_path, "reload");
550 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
556 static bool usage_contains_reload(const char *line) {
557 return (strcasestr(line, "{reload|") ||
558 strcasestr(line, "{reload}") ||
559 strcasestr(line, "{reload\"") ||
560 strcasestr(line, "|reload|") ||
561 strcasestr(line, "|reload}") ||
562 strcasestr(line, "|reload\""));
565 static int service_load_sysv_path(Service *s, const char *path) {
577 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
579 bool supports_reload = false;
586 f = fopen(path, "re");
588 r = errno == ENOENT ? 0 : -errno;
592 if (fstat(fileno(f), &st) < 0) {
597 free(u->source_path);
598 u->source_path = strdup(path);
599 if (!u->source_path) {
603 u->source_mtime = timespec_load(&st.st_mtim);
605 if (null_or_empty(&st)) {
606 u->load_state = UNIT_MASKED;
614 char l[LINE_MAX], *t;
616 if (!fgets(l, sizeof(l), f)) {
621 log_error_unit(u->id,
622 "Failed to read configuration file '%s': %s",
631 /* Try to figure out whether this init script supports
632 * the reload operation. This heuristic looks for
633 * "Usage" lines which include the reload option. */
634 if ( state == USAGE_CONTINUATION ||
635 (state == NORMAL && strcasestr(t, "usage"))) {
636 if (usage_contains_reload(t)) {
637 supports_reload = true;
639 } else if (t[strlen(t)-1] == '\\')
640 state = USAGE_CONTINUATION;
648 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
650 s->sysv_has_lsb = true;
654 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
660 t += strspn(t, WHITESPACE);
662 if (state == NORMAL) {
664 /* Try to parse Red Hat style chkconfig headers */
666 if (startswith_no_case(t, "chkconfig:")) {
668 char runlevels[16], *k;
672 if (sscanf(t+10, "%15s %i %*i", runlevels, &start_priority) != 2) {
673 log_warning_unit(u->id, "[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
677 /* A start priority gathered from the
678 * symlink farms is preferred over the
679 * data from the LSB header. */
680 if (start_priority < 0 || start_priority > 99)
681 log_warning_unit(u->id, "[%s:%u] Start priority out of range. Ignoring.", path, line);
683 log_debug_unit(u->id, "[%s:%u] Ignoring start priority set in the chkconfig file.", path, line);
685 char_array_0(runlevels);
686 k = delete_chars(runlevels, WHITESPACE "-");
696 free(s->sysv_runlevels);
697 s->sysv_runlevels = d;
700 } else if (startswith_no_case(t, "description:")) {
702 size_t k = strlen(t);
706 if (t[k-1] == '\\') {
711 if ((j = strstrip(t+12)) && *j) {
712 if (!(d = strdup(j))) {
719 free(chkconfig_description);
720 chkconfig_description = d;
722 } else if (startswith_no_case(t, "pidfile:")) {
729 if (!path_is_absolute(fn)) {
730 log_warning_unit(u->id,
731 "[%s:%u] PID file not absolute. Ignoring.",
736 if (!(fn = strdup(fn))) {
745 } else if (state == DESCRIPTION) {
747 /* Try to parse Red Hat style description
750 size_t k = strlen(t);
758 if ((j = strstrip(t)) && *j) {
761 if (chkconfig_description)
762 d = strjoin(chkconfig_description, " ", j, NULL);
771 free(chkconfig_description);
772 chkconfig_description = d;
775 } else if (state == LSB || state == LSB_DESCRIPTION) {
777 if (startswith_no_case(t, "Provides:")) {
783 FOREACH_WORD_QUOTED(w, z, t+9, i) {
786 if (!(n = strndup(w, z))) {
791 r = sysv_translate_facility(n, basename(path), &m);
800 if (unit_name_to_type(m) == UNIT_SERVICE)
801 r = unit_merge_by_name(u, m);
808 * indication that the
810 * now available. This
813 * targets do NOT pull
816 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
819 log_error_unit(u->id,
820 "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
821 path, line, m, strerror(-r));
826 } else if (startswith_no_case(t, "Required-Start:") ||
827 startswith_no_case(t, "Should-Start:") ||
828 startswith_no_case(t, "X-Start-Before:") ||
829 startswith_no_case(t, "X-Start-After:")) {
835 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
838 if (!(n = strndup(w, z))) {
843 r = sysv_translate_facility(n, basename(path), &m);
845 log_error_unit(u->id,
846 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
847 path, line, n, strerror(-r));
857 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
860 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
861 path, line, m, strerror(-r));
865 } else if (startswith_no_case(t, "Default-Start:")) {
870 k = delete_chars(t+14, WHITESPACE "-");
873 if (!(d = strdup(k))) {
878 free(s->sysv_runlevels);
879 s->sysv_runlevels = d;
882 } else if (startswith_no_case(t, "Description:")) {
885 state = LSB_DESCRIPTION;
887 if ((j = strstrip(t+12)) && *j) {
888 if (!(d = strdup(j))) {
895 free(long_description);
896 long_description = d;
898 } else if (startswith_no_case(t, "Short-Description:")) {
903 if ((j = strstrip(t+18)) && *j) {
904 if (!(d = strdup(j))) {
911 free(short_description);
912 short_description = d;
914 } else if (state == LSB_DESCRIPTION) {
916 if (startswith(l, "#\t") || startswith(l, "# ")) {
919 if ((j = strstrip(t)) && *j) {
922 if (long_description)
923 d = strjoin(long_description, " ", t, NULL);
932 free(long_description);
933 long_description = d;
942 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
945 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
946 /* If there a runlevels configured for this service
947 * but none of the standard ones, then we assume this
948 * is some special kind of service (which might be
949 * needed for early boot) and don't create any links
952 UNIT(s)->default_dependencies = false;
954 /* Don't timeout special services during boot (like fsck) */
955 s->timeout_start_usec = 0;
956 s->timeout_stop_usec = 0;
958 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
959 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
962 /* Special setting for all SysV services */
963 s->type = SERVICE_FORKING;
964 s->remain_after_exit = !s->pid_file;
965 s->guess_main_pid = false;
966 s->restart = SERVICE_RESTART_NO;
967 s->exec_context.ignore_sigpipe = false;
968 s->kill_context.kill_mode = KILL_PROCESS;
970 /* We use the long description only if
971 * no short description is set. */
973 if (short_description)
974 description = short_description;
975 else if (chkconfig_description)
976 description = chkconfig_description;
977 else if (long_description)
978 description = long_description;
985 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
993 /* Initialize the start priority from what has been set in the
994 * /etc/rcN.d/ hierarchies if we load the unit file as SysV
996 if (s->sysv_start_priority_from_rcnd >= 0)
997 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
999 u->load_state = UNIT_LOADED;
1007 free(short_description);
1008 free(long_description);
1009 free(chkconfig_description);
1014 static int service_load_sysv_name(Service *s, const char *name) {
1020 /* For SysV services we strip the *.sh suffixes. */
1021 if (endswith(name, ".sh.service"))
1024 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1028 path = strjoin(*p, "/", name, NULL);
1032 assert(endswith(path, ".service"));
1033 path[strlen(path)-8] = 0;
1035 r = service_load_sysv_path(s, path);
1037 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1038 /* Try *.sh source'able init scripts */
1039 strcat(path, ".sh");
1040 r = service_load_sysv_path(s, path);
1047 if (UNIT(s)->load_state != UNIT_STUB)
1054 static int service_load_sysv(Service *s) {
1061 /* Load service data from SysV init scripts, preferably with
1062 * LSB headers ... */
1064 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1067 if ((t = UNIT(s)->id))
1068 if ((r = service_load_sysv_name(s, t)) < 0)
1071 if (UNIT(s)->load_state == UNIT_STUB)
1072 SET_FOREACH(t, UNIT(s)->names, i) {
1073 if (t == UNIT(s)->id)
1076 if ((r = service_load_sysv_name(s, t)) < 0)
1079 if (UNIT(s)->load_state != UNIT_STUB)
1087 static int service_verify(Service *s) {
1090 if (UNIT(s)->load_state != UNIT_LOADED)
1093 if (!s->exec_command[SERVICE_EXEC_START]) {
1094 log_error_unit(UNIT(s)->id, "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1098 if (s->type != SERVICE_ONESHOT &&
1099 s->exec_command[SERVICE_EXEC_START]->command_next) {
1100 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);
1104 if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
1105 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);
1109 if (s->type == SERVICE_DBUS && !s->bus_name) {
1110 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);
1114 if (s->bus_name && s->type != SERVICE_DBUS)
1115 log_warning_unit(UNIT(s)->id, "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1117 if (s->exec_context.pam_name && !(s->kill_context.kill_mode == KILL_CONTROL_GROUP || s->kill_context.kill_mode == KILL_MIXED)) {
1118 log_error_unit(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.", UNIT(s)->id);
1125 static int service_add_default_dependencies(Service *s) {
1130 /* Add a number of automatic dependencies useful for the
1131 * majority of services. */
1133 /* First, pull in base system */
1134 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1135 SPECIAL_BASIC_TARGET, NULL, true);
1139 /* Second, activate normal shutdown */
1140 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
1141 SPECIAL_SHUTDOWN_TARGET, NULL, true);
1145 static void service_fix_output(Service *s) {
1148 /* If nothing has been explicitly configured, patch default
1149 * output in. If input is socket/tty we avoid this however,
1150 * since in that case we want output to default to the same
1151 * place as we read input from. */
1153 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1154 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1155 s->exec_context.std_input == EXEC_INPUT_NULL)
1156 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1158 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1159 s->exec_context.std_input == EXEC_INPUT_NULL)
1160 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1163 static int service_load(Unit *u) {
1165 Service *s = SERVICE(u);
1169 /* Load a .service file */
1170 r = unit_load_fragment(u);
1174 #ifdef HAVE_SYSV_COMPAT
1175 /* Load a classic init script as a fallback, if we couldn't find anything */
1176 if (u->load_state == UNIT_STUB) {
1177 r = service_load_sysv(s);
1183 /* Still nothing found? Then let's give up */
1184 if (u->load_state == UNIT_STUB)
1187 /* This is a new unit? Then let's add in some extras */
1188 if (u->load_state == UNIT_LOADED) {
1190 /* We were able to load something, then let's add in
1191 * the dropin directories. */
1192 r = unit_load_dropin(u);
1196 if (s->type == _SERVICE_TYPE_INVALID)
1197 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1199 /* Oneshot services have disabled start timeout by default */
1200 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1201 s->timeout_start_usec = 0;
1203 service_fix_output(s);
1205 r = unit_patch_contexts(u);
1209 r = unit_add_exec_dependencies(u, &s->exec_context);
1213 r = unit_add_default_slice(u, &s->cgroup_context);
1217 #ifdef HAVE_SYSV_COMPAT
1218 r = sysv_fix_order(s);
1223 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1224 s->notify_access = NOTIFY_MAIN;
1226 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1227 s->notify_access = NOTIFY_MAIN;
1230 r = unit_watch_bus_name(u, s->bus_name);
1235 if (u->default_dependencies) {
1236 r = service_add_default_dependencies(s);
1242 return service_verify(s);
1245 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1247 ServiceExecCommand c;
1248 Service *s = SERVICE(u);
1249 const char *prefix2;
1250 _cleanup_free_ char *p2 = NULL;
1254 p2 = strappend(prefix, "\t");
1255 prefix2 = p2 ? p2 : prefix;
1258 "%sService State: %s\n"
1260 "%sReload Result: %s\n"
1261 "%sPermissionsStartOnly: %s\n"
1262 "%sRootDirectoryStartOnly: %s\n"
1263 "%sRemainAfterExit: %s\n"
1264 "%sGuessMainPID: %s\n"
1267 "%sNotifyAccess: %s\n",
1268 prefix, service_state_to_string(s->state),
1269 prefix, service_result_to_string(s->result),
1270 prefix, service_result_to_string(s->reload_result),
1271 prefix, yes_no(s->permissions_start_only),
1272 prefix, yes_no(s->root_directory_start_only),
1273 prefix, yes_no(s->remain_after_exit),
1274 prefix, yes_no(s->guess_main_pid),
1275 prefix, service_type_to_string(s->type),
1276 prefix, service_restart_to_string(s->restart),
1277 prefix, notify_access_to_string(s->notify_access));
1279 if (s->control_pid > 0)
1281 "%sControl PID: "PID_FMT"\n",
1282 prefix, s->control_pid);
1284 if (s->main_pid > 0)
1286 "%sMain PID: "PID_FMT"\n"
1287 "%sMain PID Known: %s\n"
1288 "%sMain PID Alien: %s\n",
1289 prefix, s->main_pid,
1290 prefix, yes_no(s->main_pid_known),
1291 prefix, yes_no(s->main_pid_alien));
1296 prefix, s->pid_file);
1301 "%sBus Name Good: %s\n",
1302 prefix, s->bus_name,
1303 prefix, yes_no(s->bus_name_good));
1305 kill_context_dump(&s->kill_context, f, prefix);
1306 exec_context_dump(&s->exec_context, f, prefix);
1308 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1310 if (!s->exec_command[c])
1313 fprintf(f, "%s-> %s:\n",
1314 prefix, service_exec_command_to_string(c));
1316 exec_command_dump_list(s->exec_command[c], f, prefix2);
1319 #ifdef HAVE_SYSV_COMPAT
1322 "%sSysV Init Script has LSB Header: %s\n"
1323 "%sSysVEnabled: %s\n",
1324 prefix, yes_no(s->sysv_has_lsb),
1325 prefix, yes_no(s->sysv_enabled));
1327 if (s->sysv_start_priority >= 0)
1329 "%sSysVStartPriority: %i\n",
1330 prefix, s->sysv_start_priority);
1332 if (s->sysv_runlevels)
1333 fprintf(f, "%sSysVRunLevels: %s\n",
1334 prefix, s->sysv_runlevels);
1338 fprintf(f, "%sStatus Text: %s\n",
1339 prefix, s->status_text);
1342 static int service_load_pid_file(Service *s, bool may_warn) {
1343 _cleanup_free_ char *k = NULL;
1352 r = read_one_line_file(s->pid_file, &k);
1355 log_info_unit(UNIT(s)->id,
1356 "PID file %s not readable (yet?) after %s.",
1357 s->pid_file, service_state_to_string(s->state));
1361 r = parse_pid(k, &pid);
1364 log_info_unit(UNIT(s)->id,
1365 "Failed to read PID from file %s: %s",
1366 s->pid_file, strerror(-r));
1370 if (!pid_is_alive(pid)) {
1372 log_info_unit(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file);
1377 if (s->main_pid_known) {
1378 if (pid == s->main_pid)
1381 log_debug_unit(UNIT(s)->id,
1382 "Main PID changing: "PID_FMT" -> "PID_FMT,
1384 service_unwatch_main_pid(s);
1385 s->main_pid_known = false;
1387 log_debug_unit(UNIT(s)->id,
1388 "Main PID loaded: "PID_FMT, pid);
1390 r = service_set_main_pid(s, pid);
1394 r = unit_watch_pid(UNIT(s), pid);
1396 /* FIXME: we need to do something here */
1397 log_warning_unit(UNIT(s)->id,
1398 "Failed to watch PID "PID_FMT" from service %s",
1406 static int service_search_main_pid(Service *s) {
1412 /* If we know it anyway, don't ever fallback to unreliable
1414 if (s->main_pid_known)
1417 if (!s->guess_main_pid)
1420 assert(s->main_pid <= 0);
1422 pid = unit_search_main_pid(UNIT(s));
1426 log_debug_unit(UNIT(s)->id,
1427 "Main PID guessed: "PID_FMT, 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 "PID_FMT" from service %s",
1441 static void service_set_state(Service *s, ServiceState state) {
1442 ServiceState old_state;
1443 const UnitActiveState *table;
1447 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1449 old_state = s->state;
1452 service_unwatch_pid_file(s);
1455 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1457 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1459 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
1460 SERVICE_AUTO_RESTART))
1461 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1464 SERVICE_START, SERVICE_START_POST,
1465 SERVICE_RUNNING, SERVICE_RELOAD,
1466 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1468 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
1469 service_unwatch_main_pid(s);
1470 s->main_command = NULL;
1474 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1476 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1478 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
1479 service_unwatch_control_pid(s);
1480 s->control_command = NULL;
1481 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1484 if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
1485 unit_unwatch_all_pids(UNIT(s));
1488 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1489 SERVICE_RUNNING, SERVICE_RELOAD,
1490 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
1491 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) &&
1492 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1493 service_close_socket_fd(s);
1494 service_connection_unref(s);
1497 if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
1498 service_stop_watchdog(s);
1500 /* For the inactive states unit_notify() will trim the cgroup,
1501 * but for exit we have to do that ourselves... */
1502 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1503 unit_destroy_cgroup(UNIT(s));
1505 /* For remain_after_exit services, let's see if we can "release" the
1506 * hold on the console, since unit_notify() only does that in case of
1507 * change of state */
1508 if (state == SERVICE_EXITED && s->remain_after_exit &&
1509 UNIT(s)->manager->n_on_console > 0) {
1510 ExecContext *ec = unit_get_exec_context(UNIT(s));
1511 if (ec && exec_context_may_touch_console(ec)) {
1512 Manager *m = UNIT(s)->manager;
1515 if (m->n_on_console == 0)
1516 /* unset no_console_output flag, since the console is free */
1517 m->no_console_output = false;
1521 if (old_state != state)
1522 log_debug_unit(UNIT(s)->id, "%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
1524 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1525 s->reload_result = SERVICE_SUCCESS;
1528 static int service_coldplug(Unit *u) {
1529 Service *s = SERVICE(u);
1533 assert(s->state == SERVICE_DEAD);
1535 if (s->deserialized_state != s->state) {
1537 if (IN_SET(s->deserialized_state,
1538 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1540 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1542 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
1546 k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec;
1548 /* For the start/stop timeouts 0 means off */
1550 r = service_arm_timer(s, k);
1556 if (s->deserialized_state == SERVICE_AUTO_RESTART) {
1558 /* The restart timeouts 0 means immediately */
1559 r = service_arm_timer(s, s->restart_usec);
1564 if (pid_is_unwaited(s->main_pid) &&
1565 ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) ||
1566 IN_SET(s->deserialized_state,
1567 SERVICE_START, SERVICE_START_POST,
1568 SERVICE_RUNNING, SERVICE_RELOAD,
1569 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1571 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) {
1572 r = unit_watch_pid(UNIT(s), s->main_pid);
1577 if (pid_is_unwaited(s->control_pid) &&
1578 IN_SET(s->deserialized_state,
1579 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1581 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1583 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
1584 r = unit_watch_pid(UNIT(s), s->control_pid);
1589 if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
1590 unit_watch_all_pids(UNIT(s));
1592 if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
1593 service_start_watchdog(s);
1595 service_set_state(s, s->deserialized_state);
1601 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1605 unsigned rn_fds = 0;
1612 if (s->socket_fd >= 0)
1615 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1620 if (u->type != UNIT_SOCKET)
1625 r = socket_collect_fds(sock, &cfds, &cn_fds);
1638 t = new(int, rn_fds+cn_fds);
1645 memcpy(t, rfds, rn_fds * sizeof(int));
1646 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1651 rn_fds = rn_fds+cn_fds;
1666 static int service_spawn(
1671 bool apply_permissions,
1673 bool apply_tty_stdin,
1674 bool set_notify_socket,
1681 _cleanup_free_ int *fdsbuf = NULL;
1682 unsigned n_fds = 0, n_env = 0;
1683 _cleanup_strv_free_ char
1684 **argv = NULL, **final_env = NULL, **our_env = NULL;
1691 unit_realize_cgroup(UNIT(s));
1693 r = unit_setup_exec_runtime(UNIT(s));
1698 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1699 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1700 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1702 if (s->socket_fd >= 0) {
1703 fds = &s->socket_fd;
1706 r = service_collect_fds(s, &fdsbuf, &n_fds);
1714 if (timeout && s->timeout_start_usec > 0) {
1715 r = service_arm_timer(s, s->timeout_start_usec);
1719 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1721 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
1725 our_env = new0(char*, 4);
1731 if (set_notify_socket)
1732 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1737 if (s->main_pid > 0)
1738 if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) {
1743 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
1744 if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) {
1749 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1755 if (is_control && UNIT(s)->cgroup_path) {
1756 path = strappenda(UNIT(s)->cgroup_path, "/control");
1757 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1759 path = UNIT(s)->cgroup_path;
1769 UNIT(s)->manager->confirm_spawn,
1770 UNIT(s)->manager->cgroup_supported,
1772 manager_get_runtime_prefix(UNIT(s)->manager),
1775 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1781 r = unit_watch_pid(UNIT(s), pid);
1783 /* FIXME: we need to do something here */
1792 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1797 static int main_pid_good(Service *s) {
1800 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1803 /* If we know the pid file, then lets just check if it is
1805 if (s->main_pid_known) {
1807 /* If it's an alien child let's check if it is still
1809 if (s->main_pid_alien && s->main_pid > 0)
1810 return pid_is_alive(s->main_pid);
1812 /* .. otherwise assume we'll get a SIGCHLD for it,
1813 * which we really should wait for to collect exit
1814 * status and code */
1815 return s->main_pid > 0;
1818 /* We don't know the pid */
1822 _pure_ static int control_pid_good(Service *s) {
1825 return s->control_pid > 0;
1828 static int cgroup_good(Service *s) {
1833 if (!UNIT(s)->cgroup_path)
1836 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1843 static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none);
1845 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1849 if (f != SERVICE_SUCCESS)
1852 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1854 if (s->result != SERVICE_SUCCESS)
1855 service_execute_action(s, s->failure_action, "failed", false);
1857 if (allow_restart &&
1858 !s->forbid_restart &&
1859 (s->restart == SERVICE_RESTART_ALWAYS ||
1860 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1861 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1862 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1863 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1864 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1865 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1866 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1867 (s->result != SERVICE_FAILURE_SIGNAL ||
1868 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))) {
1870 r = service_arm_timer(s, s->restart_usec);
1874 service_set_state(s, SERVICE_AUTO_RESTART);
1877 s->forbid_restart = false;
1879 /* We want fresh tmpdirs in case service is started again immediately */
1880 exec_runtime_destroy(s->exec_runtime);
1881 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
1883 /* Also, remove the runtime directory in */
1884 exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
1886 /* Try to delete the pid file. At this point it will be
1887 * out-of-date, and some software might be confused by it, so
1888 * let's remove it. */
1890 unlink_noerrno(s->pid_file);
1895 log_warning_unit(UNIT(s)->id,
1896 "%s failed to run install restart timer: %s",
1897 UNIT(s)->id, strerror(-r));
1898 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1901 static void service_enter_stop_post(Service *s, ServiceResult f) {
1905 if (f != SERVICE_SUCCESS)
1908 service_unwatch_control_pid(s);
1909 unit_watch_all_pids(UNIT(s));
1911 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1912 if (s->control_command) {
1913 s->control_command_id = SERVICE_EXEC_STOP_POST;
1915 r = service_spawn(s,
1919 !s->permissions_start_only,
1920 !s->root_directory_start_only,
1928 service_set_state(s, SERVICE_STOP_POST);
1930 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1935 log_warning_unit(UNIT(s)->id,
1936 "%s failed to run 'stop-post' task: %s",
1937 UNIT(s)->id, strerror(-r));
1938 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1941 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1946 if (f != SERVICE_SUCCESS)
1949 unit_watch_all_pids(UNIT(s));
1951 r = unit_kill_context(
1954 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1963 if (s->timeout_stop_usec > 0) {
1964 r = service_arm_timer(s, s->timeout_stop_usec);
1969 service_set_state(s, state);
1970 } else if (state == SERVICE_STOP_SIGTERM)
1971 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS);
1972 else if (state == SERVICE_STOP_SIGKILL)
1973 service_enter_stop_post(s, SERVICE_SUCCESS);
1974 else if (state == SERVICE_FINAL_SIGTERM)
1975 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS);
1977 service_enter_dead(s, SERVICE_SUCCESS, true);
1982 log_warning_unit(UNIT(s)->id,
1983 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1985 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1986 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1988 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1991 static void service_enter_stop(Service *s, ServiceResult f) {
1996 if (f != SERVICE_SUCCESS)
1999 service_unwatch_control_pid(s);
2000 unit_watch_all_pids(UNIT(s));
2002 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
2003 if (s->control_command) {
2004 s->control_command_id = SERVICE_EXEC_STOP;
2006 r = service_spawn(s,
2010 !s->permissions_start_only,
2011 !s->root_directory_start_only,
2019 service_set_state(s, SERVICE_STOP);
2021 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2026 log_warning_unit(UNIT(s)->id,
2027 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2028 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2031 static void service_enter_running(Service *s, ServiceResult f) {
2032 int main_pid_ok, cgroup_ok;
2035 if (f != SERVICE_SUCCESS)
2038 main_pid_ok = main_pid_good(s);
2039 cgroup_ok = cgroup_good(s);
2041 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2042 (s->bus_name_good || s->type != SERVICE_DBUS))
2043 service_set_state(s, SERVICE_RUNNING);
2044 else if (s->remain_after_exit)
2045 service_set_state(s, SERVICE_EXITED);
2047 service_enter_stop(s, SERVICE_SUCCESS);
2050 static void service_enter_start_post(Service *s) {
2054 service_unwatch_control_pid(s);
2055 service_reset_watchdog(s);
2057 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
2058 if (s->control_command) {
2059 s->control_command_id = SERVICE_EXEC_START_POST;
2061 r = service_spawn(s,
2065 !s->permissions_start_only,
2066 !s->root_directory_start_only,
2074 service_set_state(s, SERVICE_START_POST);
2076 service_enter_running(s, SERVICE_SUCCESS);
2081 log_warning_unit(UNIT(s)->id,
2082 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2083 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2086 static void service_kill_control_processes(Service *s) {
2089 if (!UNIT(s)->cgroup_path)
2092 p = strappenda(UNIT(s)->cgroup_path, "/control");
2093 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
2096 static void service_enter_start(Service *s) {
2103 assert(s->exec_command[SERVICE_EXEC_START]);
2104 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2106 service_unwatch_control_pid(s);
2107 service_unwatch_main_pid(s);
2109 /* We want to ensure that nobody leaks processes from
2110 * START_PRE here, so let's go on a killing spree, People
2111 * should not spawn long running processes from START_PRE. */
2112 service_kill_control_processes(s);
2114 if (s->type == SERVICE_FORKING) {
2115 s->control_command_id = SERVICE_EXEC_START;
2116 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2118 s->main_command = NULL;
2120 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2121 s->control_command = NULL;
2123 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2126 r = service_spawn(s,
2128 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
2129 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2134 s->notify_access != NOTIFY_NONE,
2140 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2141 /* For simple services we immediately start
2142 * the START_POST binaries. */
2144 service_set_main_pid(s, pid);
2145 service_enter_start_post(s);
2147 } else if (s->type == SERVICE_FORKING) {
2149 /* For forking services we wait until the start
2150 * process exited. */
2152 s->control_pid = pid;
2153 service_set_state(s, SERVICE_START);
2155 } else if (s->type == SERVICE_ONESHOT ||
2156 s->type == SERVICE_DBUS ||
2157 s->type == SERVICE_NOTIFY) {
2159 /* For oneshot services we wait until the start
2160 * process exited, too, but it is our main process. */
2162 /* For D-Bus services we know the main pid right away,
2163 * but wait for the bus name to appear on the
2164 * bus. Notify services are similar. */
2166 service_set_main_pid(s, pid);
2167 service_set_state(s, SERVICE_START);
2169 assert_not_reached("Unknown service type");
2174 log_warning_unit(UNIT(s)->id,
2175 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2176 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2179 static void service_enter_start_pre(Service *s) {
2184 service_unwatch_control_pid(s);
2186 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
2187 if (s->control_command) {
2188 /* Before we start anything, let's clear up what might
2189 * be left from previous runs. */
2190 service_kill_control_processes(s);
2192 s->control_command_id = SERVICE_EXEC_START_PRE;
2194 r = service_spawn(s,
2198 !s->permissions_start_only,
2199 !s->root_directory_start_only,
2207 service_set_state(s, SERVICE_START_PRE);
2209 service_enter_start(s);
2214 log_warning_unit(UNIT(s)->id,
2215 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2216 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2219 static void service_enter_restart(Service *s) {
2220 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2225 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2226 /* Don't restart things if we are going down anyway */
2227 log_info_unit(UNIT(s)->id,
2228 "Stop job pending for unit, delaying automatic restart.");
2230 r = service_arm_timer(s, s->restart_usec);
2237 /* Any units that are bound to this service must also be
2238 * restarted. We use JOB_RESTART (instead of the more obvious
2239 * JOB_START) here so that those dependency jobs will be added
2241 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2245 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2246 * it will be canceled as part of the service_stop() call that
2247 * is executed as part of JOB_RESTART. */
2249 log_debug_unit(UNIT(s)->id,
2250 "%s scheduled restart job.", UNIT(s)->id);
2254 log_warning_unit(UNIT(s)->id,
2255 "%s failed to schedule restart job: %s",
2256 UNIT(s)->id, bus_error_message(&error, -r));
2257 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2260 static void service_enter_reload(Service *s) {
2265 service_unwatch_control_pid(s);
2267 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
2268 if (s->control_command) {
2269 s->control_command_id = SERVICE_EXEC_RELOAD;
2271 r = service_spawn(s,
2275 !s->permissions_start_only,
2276 !s->root_directory_start_only,
2284 service_set_state(s, SERVICE_RELOAD);
2286 service_enter_running(s, SERVICE_SUCCESS);
2291 log_warning_unit(UNIT(s)->id,
2292 "%s failed to run 'reload' task: %s",
2293 UNIT(s)->id, strerror(-r));
2294 s->reload_result = SERVICE_FAILURE_RESOURCES;
2295 service_enter_running(s, SERVICE_SUCCESS);
2298 static void service_run_next_control(Service *s) {
2302 assert(s->control_command);
2303 assert(s->control_command->command_next);
2305 assert(s->control_command_id != SERVICE_EXEC_START);
2307 s->control_command = s->control_command->command_next;
2308 service_unwatch_control_pid(s);
2310 r = service_spawn(s,
2314 !s->permissions_start_only,
2315 !s->root_directory_start_only,
2316 s->control_command_id == SERVICE_EXEC_START_PRE ||
2317 s->control_command_id == SERVICE_EXEC_STOP_POST,
2327 log_warning_unit(UNIT(s)->id,
2328 "%s failed to run next control task: %s",
2329 UNIT(s)->id, strerror(-r));
2331 if (s->state == SERVICE_START_PRE)
2332 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2333 else if (s->state == SERVICE_STOP)
2334 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2335 else if (s->state == SERVICE_STOP_POST)
2336 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2337 else if (s->state == SERVICE_RELOAD) {
2338 s->reload_result = SERVICE_FAILURE_RESOURCES;
2339 service_enter_running(s, SERVICE_SUCCESS);
2341 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2344 static void service_run_next_main(Service *s) {
2349 assert(s->main_command);
2350 assert(s->main_command->command_next);
2351 assert(s->type == SERVICE_ONESHOT);
2353 s->main_command = s->main_command->command_next;
2354 service_unwatch_main_pid(s);
2356 r = service_spawn(s,
2363 s->notify_access != NOTIFY_NONE,
2369 service_set_main_pid(s, pid);
2374 log_warning_unit(UNIT(s)->id,
2375 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2376 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2379 static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none) {
2382 if (action == SERVICE_FAILURE_ACTION_REBOOT ||
2383 action == SERVICE_FAILURE_ACTION_REBOOT_FORCE)
2384 update_reboot_param_file(s->reboot_arg);
2388 case SERVICE_FAILURE_ACTION_NONE:
2389 if (log_action_none)
2390 log_warning_unit(UNIT(s)->id,
2391 "%s %s, refusing to start.", UNIT(s)->id, reason);
2394 case SERVICE_FAILURE_ACTION_REBOOT: {
2395 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2398 log_warning_unit(UNIT(s)->id,
2399 "%s %s, rebooting.", UNIT(s)->id, reason);
2401 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
2402 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
2403 true, &error, NULL);
2405 log_error_unit(UNIT(s)->id,
2406 "Failed to reboot: %s.", bus_error_message(&error, r));
2411 case SERVICE_FAILURE_ACTION_REBOOT_FORCE:
2412 log_warning_unit(UNIT(s)->id,
2413 "%s %s, forcibly rebooting.", UNIT(s)->id, reason);
2414 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2417 case SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE:
2418 log_warning_unit(UNIT(s)->id,
2419 "%s %s, rebooting immediately.", UNIT(s)->id, reason);
2421 if (s->reboot_arg) {
2422 log_info("Rebooting with argument '%s'.", s->reboot_arg);
2423 syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
2424 LINUX_REBOOT_CMD_RESTART2, s->reboot_arg);
2427 log_info("Rebooting.");
2428 reboot(RB_AUTOBOOT);
2432 log_error_unit(UNIT(s)->id,
2433 "failure action=%i", action);
2434 assert_not_reached("Unknown FailureAction.");
2440 static int service_start_limit_test(Service *s) {
2443 if (ratelimit_test(&s->start_limit))
2446 return service_execute_action(s, s->start_limit_action, "start request repeated too quickly", true);
2449 static int service_start(Unit *u) {
2450 Service *s = SERVICE(u);
2455 /* We cannot fulfill this request right now, try again later
2457 if (s->state == SERVICE_STOP ||
2458 s->state == SERVICE_STOP_SIGTERM ||
2459 s->state == SERVICE_STOP_SIGKILL ||
2460 s->state == SERVICE_STOP_POST ||
2461 s->state == SERVICE_FINAL_SIGTERM ||
2462 s->state == SERVICE_FINAL_SIGKILL)
2465 /* Already on it! */
2466 if (s->state == SERVICE_START_PRE ||
2467 s->state == SERVICE_START ||
2468 s->state == SERVICE_START_POST)
2471 /* A service that will be restarted must be stopped first to
2472 * trigger BindsTo and/or OnFailure dependencies. If a user
2473 * does not want to wait for the holdoff time to elapse, the
2474 * service should be manually restarted, not started. We
2475 * simply return EAGAIN here, so that any start jobs stay
2476 * queued, and assume that the auto restart timer will
2477 * eventually trigger the restart. */
2478 if (s->state == SERVICE_AUTO_RESTART)
2481 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2483 /* Make sure we don't enter a busy loop of some kind. */
2484 r = service_start_limit_test(s);
2486 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2490 s->result = SERVICE_SUCCESS;
2491 s->reload_result = SERVICE_SUCCESS;
2492 s->main_pid_known = false;
2493 s->main_pid_alien = false;
2494 s->forbid_restart = false;
2496 service_enter_start_pre(s);
2500 static int service_stop(Unit *u) {
2501 Service *s = SERVICE(u);
2505 /* Don't create restart jobs from here. */
2506 s->forbid_restart = true;
2509 if (s->state == SERVICE_STOP ||
2510 s->state == SERVICE_STOP_SIGTERM ||
2511 s->state == SERVICE_STOP_SIGKILL ||
2512 s->state == SERVICE_STOP_POST ||
2513 s->state == SERVICE_FINAL_SIGTERM ||
2514 s->state == SERVICE_FINAL_SIGKILL)
2517 /* A restart will be scheduled or is in progress. */
2518 if (s->state == SERVICE_AUTO_RESTART) {
2519 service_set_state(s, SERVICE_DEAD);
2523 /* If there's already something running we go directly into
2525 if (s->state == SERVICE_START_PRE ||
2526 s->state == SERVICE_START ||
2527 s->state == SERVICE_START_POST ||
2528 s->state == SERVICE_RELOAD) {
2529 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2533 assert(s->state == SERVICE_RUNNING ||
2534 s->state == SERVICE_EXITED);
2536 service_enter_stop(s, SERVICE_SUCCESS);
2540 static int service_reload(Unit *u) {
2541 Service *s = SERVICE(u);
2545 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2547 service_enter_reload(s);
2551 _pure_ static bool service_can_reload(Unit *u) {
2552 Service *s = SERVICE(u);
2556 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2559 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2560 Service *s = SERVICE(u);
2566 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2567 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2568 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2570 if (s->control_pid > 0)
2571 unit_serialize_item_format(u, f, "control-pid", PID_FMT,
2574 if (s->main_pid_known && s->main_pid > 0)
2575 unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid);
2577 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2580 unit_serialize_item(u, f, "status-text", s->status_text);
2582 /* FIXME: There's a minor uncleanliness here: if there are
2583 * multiple commands attached here, we will start from the
2584 * first one again */
2585 if (s->control_command_id >= 0)
2586 unit_serialize_item(u, f, "control-command",
2587 service_exec_command_to_string(s->control_command_id));
2589 if (s->socket_fd >= 0) {
2592 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2595 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2598 if (s->main_exec_status.pid > 0) {
2599 unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT,
2600 s->main_exec_status.pid);
2601 dual_timestamp_serialize(f, "main-exec-status-start",
2602 &s->main_exec_status.start_timestamp);
2603 dual_timestamp_serialize(f, "main-exec-status-exit",
2604 &s->main_exec_status.exit_timestamp);
2606 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2607 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
2608 s->main_exec_status.code);
2609 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
2610 s->main_exec_status.status);
2613 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2614 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2616 if (s->forbid_restart)
2617 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
2622 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2623 Service *s = SERVICE(u);
2630 if (streq(key, "state")) {
2633 state = service_state_from_string(value);
2635 log_debug_unit(u->id, "Failed to parse state value %s", value);
2637 s->deserialized_state = state;
2638 } else if (streq(key, "result")) {
2641 f = service_result_from_string(value);
2643 log_debug_unit(u->id, "Failed to parse result value %s", value);
2644 else if (f != SERVICE_SUCCESS)
2647 } else if (streq(key, "reload-result")) {
2650 f = service_result_from_string(value);
2652 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2653 else if (f != SERVICE_SUCCESS)
2654 s->reload_result = f;
2656 } else if (streq(key, "control-pid")) {
2659 if (parse_pid(value, &pid) < 0)
2660 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2662 s->control_pid = pid;
2663 } else if (streq(key, "main-pid")) {
2666 if (parse_pid(value, &pid) < 0)
2667 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2669 service_set_main_pid(s, pid);
2670 unit_watch_pid(UNIT(s), pid);
2672 } else if (streq(key, "main-pid-known")) {
2675 b = parse_boolean(value);
2677 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2679 s->main_pid_known = b;
2680 } else if (streq(key, "status-text")) {
2687 free(s->status_text);
2691 } else if (streq(key, "control-command")) {
2692 ServiceExecCommand id;
2694 id = service_exec_command_from_string(value);
2696 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2698 s->control_command_id = id;
2699 s->control_command = s->exec_command[id];
2701 } else if (streq(key, "socket-fd")) {
2704 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2705 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2708 safe_close(s->socket_fd);
2709 s->socket_fd = fdset_remove(fds, fd);
2711 } else if (streq(key, "main-exec-status-pid")) {
2714 if (parse_pid(value, &pid) < 0)
2715 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2717 s->main_exec_status.pid = pid;
2718 } else if (streq(key, "main-exec-status-code")) {
2721 if (safe_atoi(value, &i) < 0)
2722 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2724 s->main_exec_status.code = i;
2725 } else if (streq(key, "main-exec-status-status")) {
2728 if (safe_atoi(value, &i) < 0)
2729 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2731 s->main_exec_status.status = i;
2732 } else if (streq(key, "main-exec-status-start"))
2733 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2734 else if (streq(key, "main-exec-status-exit"))
2735 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2736 else if (streq(key, "watchdog-timestamp"))
2737 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2738 else if (streq(key, "forbid-restart")) {
2741 b = parse_boolean(value);
2743 log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value);
2745 s->forbid_restart = b;
2747 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2752 _pure_ static UnitActiveState service_active_state(Unit *u) {
2753 const UnitActiveState *table;
2757 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2759 return table[SERVICE(u)->state];
2762 static const char *service_sub_state_to_string(Unit *u) {
2765 return service_state_to_string(SERVICE(u)->state);
2768 static bool service_check_gc(Unit *u) {
2769 Service *s = SERVICE(u);
2773 /* Never clean up services that still have a process around,
2774 * even if the service is formally dead. */
2775 if (cgroup_good(s) > 0 ||
2776 main_pid_good(s) > 0 ||
2777 control_pid_good(s) > 0)
2780 #ifdef HAVE_SYSV_COMPAT
2788 _pure_ static bool service_check_snapshot(Unit *u) {
2789 Service *s = SERVICE(u);
2793 return (s->socket_fd < 0);
2796 static int service_retry_pid_file(Service *s) {
2799 assert(s->pid_file);
2800 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2802 r = service_load_pid_file(s, false);
2806 service_unwatch_pid_file(s);
2808 service_enter_running(s, SERVICE_SUCCESS);
2812 static int service_watch_pid_file(Service *s) {
2815 log_debug_unit(UNIT(s)->id,
2816 "Setting watch for %s's PID file %s",
2817 UNIT(s)->id, s->pid_file_pathspec->path);
2818 r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
2822 /* the pidfile might have appeared just before we set the watch */
2823 log_debug_unit(UNIT(s)->id,
2824 "Trying to read %s's PID file %s in case it changed",
2825 UNIT(s)->id, s->pid_file_pathspec->path);
2826 service_retry_pid_file(s);
2830 log_error_unit(UNIT(s)->id,
2831 "Failed to set a watch for %s's PID file %s: %s",
2832 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2833 service_unwatch_pid_file(s);
2837 static int service_demand_pid_file(Service *s) {
2840 assert(s->pid_file);
2841 assert(!s->pid_file_pathspec);
2843 ps = new0(PathSpec, 1);
2848 ps->path = strdup(s->pid_file);
2854 path_kill_slashes(ps->path);
2856 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2857 * keep their PID file open all the time. */
2858 ps->type = PATH_MODIFIED;
2859 ps->inotify_fd = -1;
2861 s->pid_file_pathspec = ps;
2863 return service_watch_pid_file(s);
2866 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
2867 PathSpec *p = userdata;
2872 s = SERVICE(p->unit);
2876 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2877 assert(s->pid_file_pathspec);
2878 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2880 log_debug_unit(UNIT(s)->id, "inotify event for %s", UNIT(s)->id);
2882 if (path_spec_fd_event(p, events) < 0)
2885 if (service_retry_pid_file(s) == 0)
2888 if (service_watch_pid_file(s) < 0)
2894 service_unwatch_pid_file(s);
2895 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2899 static void service_notify_cgroup_empty_event(Unit *u) {
2900 Service *s = SERVICE(u);
2904 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
2908 /* Waiting for SIGCHLD is usually more interesting,
2909 * because it includes return codes/signals. Which is
2910 * why we ignore the cgroup events for most cases,
2911 * except when we don't know pid which to expect the
2915 case SERVICE_START_POST:
2916 /* If we were hoping for the daemon to write its PID file,
2917 * we can give up now. */
2918 if (s->pid_file_pathspec) {
2919 log_warning_unit(u->id,
2920 "%s never wrote its PID file. Failing.", UNIT(s)->id);
2921 service_unwatch_pid_file(s);
2922 if (s->state == SERVICE_START)
2923 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2925 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2929 case SERVICE_RUNNING:
2930 /* service_enter_running() will figure out what to do */
2931 service_enter_running(s, SERVICE_SUCCESS);
2934 case SERVICE_STOP_SIGTERM:
2935 case SERVICE_STOP_SIGKILL:
2937 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2938 service_enter_stop_post(s, SERVICE_SUCCESS);
2942 case SERVICE_STOP_POST:
2943 case SERVICE_FINAL_SIGTERM:
2944 case SERVICE_FINAL_SIGKILL:
2945 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2946 service_enter_dead(s, SERVICE_SUCCESS, true);
2955 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2956 Service *s = SERVICE(u);
2962 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2963 is_clean_exit_lsb(code, status, &s->success_status))
2964 f = SERVICE_SUCCESS;
2965 else if (code == CLD_EXITED)
2966 f = SERVICE_FAILURE_EXIT_CODE;
2967 else if (code == CLD_KILLED)
2968 f = SERVICE_FAILURE_SIGNAL;
2969 else if (code == CLD_DUMPED)
2970 f = SERVICE_FAILURE_CORE_DUMP;
2972 assert_not_reached("Unknown code");
2974 if (s->main_pid == pid) {
2975 /* Forking services may occasionally move to a new PID.
2976 * As long as they update the PID file before exiting the old
2977 * PID, they're fine. */
2978 if (service_load_pid_file(s, false) == 0)
2982 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2984 if (s->main_command) {
2985 /* If this is not a forking service than the
2986 * main process got started and hence we copy
2987 * the exit status so that it is recorded both
2988 * as main and as control process exit
2991 s->main_command->exec_status = s->main_exec_status;
2993 if (s->main_command->ignore)
2994 f = SERVICE_SUCCESS;
2995 } else if (s->exec_command[SERVICE_EXEC_START]) {
2997 /* If this is a forked process, then we should
2998 * ignore the return value if this was
2999 * configured for the starter process */
3001 if (s->exec_command[SERVICE_EXEC_START]->ignore)
3002 f = SERVICE_SUCCESS;
3005 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
3007 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
3008 u->id, sigchld_code_to_string(code), status,
3009 strna(code == CLD_EXITED
3010 ? exit_status_to_string(status, EXIT_STATUS_FULL)
3011 : signal_to_string(status)),
3012 "EXIT_CODE=%s", sigchld_code_to_string(code),
3013 "EXIT_STATUS=%i", status,
3016 if (f != SERVICE_SUCCESS)
3019 if (s->main_command &&
3020 s->main_command->command_next &&
3021 f == SERVICE_SUCCESS) {
3023 /* There is another command to *
3024 * execute, so let's do that. */
3026 log_debug_unit(u->id,
3027 "%s running next main command for state %s",
3028 u->id, service_state_to_string(s->state));
3029 service_run_next_main(s);
3033 /* The service exited, so the service is officially
3035 s->main_command = NULL;
3039 case SERVICE_START_POST:
3040 case SERVICE_RELOAD:
3042 /* Need to wait until the operation is
3047 if (s->type == SERVICE_ONESHOT) {
3048 /* This was our main goal, so let's go on */
3049 if (f == SERVICE_SUCCESS)
3050 service_enter_start_post(s);
3052 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3058 case SERVICE_RUNNING:
3059 service_enter_running(s, f);
3062 case SERVICE_STOP_SIGTERM:
3063 case SERVICE_STOP_SIGKILL:
3065 if (!control_pid_good(s))
3066 service_enter_stop_post(s, f);
3068 /* If there is still a control process, wait for that first */
3071 case SERVICE_STOP_POST:
3072 case SERVICE_FINAL_SIGTERM:
3073 case SERVICE_FINAL_SIGKILL:
3075 if (!control_pid_good(s))
3076 service_enter_dead(s, f, true);
3080 assert_not_reached("Uh, main process died at wrong time.");
3084 } else if (s->control_pid == pid) {
3087 if (s->control_command) {
3088 exec_status_exit(&s->control_command->exec_status,
3089 &s->exec_context, pid, code, status);
3091 if (s->control_command->ignore)
3092 f = SERVICE_SUCCESS;
3095 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
3096 "%s: control process exited, code=%s status=%i",
3097 u->id, sigchld_code_to_string(code), status);
3099 if (f != SERVICE_SUCCESS)
3102 /* Immediately get rid of the cgroup, so that the
3103 * kernel doesn't delay the cgroup empty messages for
3104 * the service cgroup any longer than necessary */
3105 service_kill_control_processes(s);
3107 if (s->control_command &&
3108 s->control_command->command_next &&
3109 f == SERVICE_SUCCESS) {
3111 /* There is another command to *
3112 * execute, so let's do that. */
3114 log_debug_unit(u->id,
3115 "%s running next control command for state %s",
3116 u->id, service_state_to_string(s->state));
3117 service_run_next_control(s);
3120 /* No further commands for this step, so let's
3121 * figure out what to do next */
3123 s->control_command = NULL;
3124 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3126 log_debug_unit(u->id,
3127 "%s got final SIGCHLD for state %s",
3128 u->id, service_state_to_string(s->state));
3132 case SERVICE_START_PRE:
3133 if (f == SERVICE_SUCCESS)
3134 service_enter_start(s);
3136 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3140 if (s->type != SERVICE_FORKING)
3141 /* Maybe spurious event due to a reload that changed the type? */
3144 if (f != SERVICE_SUCCESS) {
3145 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3150 bool has_start_post;
3153 /* Let's try to load the pid file here if we can.
3154 * The PID file might actually be created by a START_POST
3155 * script. In that case don't worry if the loading fails. */
3157 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3158 r = service_load_pid_file(s, !has_start_post);
3159 if (!has_start_post && r < 0) {
3160 r = service_demand_pid_file(s);
3161 if (r < 0 || !cgroup_good(s))
3162 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3166 service_search_main_pid(s);
3168 service_enter_start_post(s);
3171 case SERVICE_START_POST:
3172 if (f != SERVICE_SUCCESS) {
3173 service_enter_stop(s, f);
3180 r = service_load_pid_file(s, true);
3182 r = service_demand_pid_file(s);
3183 if (r < 0 || !cgroup_good(s))
3184 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3188 service_search_main_pid(s);
3190 service_enter_running(s, SERVICE_SUCCESS);
3193 case SERVICE_RELOAD:
3194 if (f == SERVICE_SUCCESS) {
3195 service_load_pid_file(s, true);
3196 service_search_main_pid(s);
3199 s->reload_result = f;
3200 service_enter_running(s, SERVICE_SUCCESS);
3204 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3207 case SERVICE_STOP_SIGTERM:
3208 case SERVICE_STOP_SIGKILL:
3209 if (main_pid_good(s) <= 0)
3210 service_enter_stop_post(s, f);
3212 /* If there is still a service
3213 * process around, wait until
3214 * that one quit, too */
3217 case SERVICE_STOP_POST:
3218 case SERVICE_FINAL_SIGTERM:
3219 case SERVICE_FINAL_SIGKILL:
3220 if (main_pid_good(s) <= 0)
3221 service_enter_dead(s, f, true);
3225 assert_not_reached("Uh, control process died at wrong time.");
3230 /* Notify clients about changed exit status */
3231 unit_add_to_dbus_queue(u);
3233 /* We got one SIGCHLD for the service, let's watch all
3234 * processes that are now running of the service, and watch
3235 * that. Among the PIDs we then watch will be children
3236 * reassigned to us, which hopefully allows us to identify
3237 * when all children are gone */
3238 unit_tidy_watch_pids(u, s->main_pid, s->control_pid);
3239 unit_watch_all_pids(u);
3241 /* If the PID set is empty now, then let's finish this off */
3242 if (set_isempty(u->pids))
3243 service_notify_cgroup_empty_event(u);
3246 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
3247 Service *s = SERVICE(userdata);
3250 assert(source == s->timer_event_source);
3254 case SERVICE_START_PRE:
3256 log_warning_unit(UNIT(s)->id,
3257 "%s %s operation timed out. Terminating.",
3259 s->state == SERVICE_START ? "start" : "start-pre");
3260 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3263 case SERVICE_START_POST:
3264 log_warning_unit(UNIT(s)->id,
3265 "%s start-post operation timed out. Stopping.", UNIT(s)->id);
3266 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3269 case SERVICE_RELOAD:
3270 log_warning_unit(UNIT(s)->id,
3271 "%s reload operation timed out. Stopping.", UNIT(s)->id);
3272 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3273 service_enter_running(s, SERVICE_SUCCESS);
3277 log_warning_unit(UNIT(s)->id,
3278 "%s stopping timed out. Terminating.", UNIT(s)->id);
3279 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3282 case SERVICE_STOP_SIGTERM:
3283 if (s->kill_context.send_sigkill) {
3284 log_warning_unit(UNIT(s)->id,
3285 "%s stop-sigterm timed out. Killing.", UNIT(s)->id);
3286 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3288 log_warning_unit(UNIT(s)->id,
3289 "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s)->id);
3290 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3295 case SERVICE_STOP_SIGKILL:
3296 /* Uh, we sent a SIGKILL and it is still not gone?
3297 * Must be something we cannot kill, so let's just be
3298 * weirded out and continue */
3300 log_warning_unit(UNIT(s)->id,
3301 "%s still around after SIGKILL. Ignoring.", UNIT(s)->id);
3302 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3305 case SERVICE_STOP_POST:
3306 log_warning_unit(UNIT(s)->id,
3307 "%s stop-post timed out. Terminating.", UNIT(s)->id);
3308 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3311 case SERVICE_FINAL_SIGTERM:
3312 if (s->kill_context.send_sigkill) {
3313 log_warning_unit(UNIT(s)->id,
3314 "%s stop-final-sigterm timed out. Killing.", UNIT(s)->id);
3315 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3317 log_warning_unit(UNIT(s)->id,
3318 "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.",
3320 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3325 case SERVICE_FINAL_SIGKILL:
3326 log_warning_unit(UNIT(s)->id,
3327 "%s still around after final SIGKILL. Entering failed mode.", UNIT(s)->id);
3328 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3331 case SERVICE_AUTO_RESTART:
3332 log_info_unit(UNIT(s)->id,
3333 s->restart_usec > 0 ?
3334 "%s holdoff time over, scheduling restart." :
3335 "%s has no holdoff time, scheduling restart.",
3337 service_enter_restart(s);
3341 assert_not_reached("Timeout at wrong time.");
3347 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
3348 Service *s = SERVICE(userdata);
3351 assert(source == s->watchdog_event_source);
3353 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
3354 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_WATCHDOG);
3359 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3360 Service *s = SERVICE(u);
3362 bool notify_dbus = false;
3366 log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT" (%s...)",
3367 u->id, pid, tags && *tags ? tags[0] : "(empty)");
3369 if (s->notify_access == NOTIFY_NONE) {
3370 log_warning_unit(u->id,
3371 "%s: Got notification message from PID "PID_FMT", but reception is disabled.",
3376 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3378 if (s->main_pid != 0)
3379 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);
3381 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);
3385 /* Interpret MAINPID= */
3386 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3387 (s->state == SERVICE_START ||
3388 s->state == SERVICE_START_POST ||
3389 s->state == SERVICE_RUNNING ||
3390 s->state == SERVICE_RELOAD)) {
3392 if (parse_pid(e + 8, &pid) < 0)
3393 log_warning_unit(u->id, "Failed to parse notification message %s", e);
3395 log_debug_unit(u->id, "%s: got %s", u->id, e);
3396 service_set_main_pid(s, pid);
3397 unit_watch_pid(UNIT(s), pid);
3402 /* Interpret READY= */
3403 if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START && strv_find(tags, "READY=1")) {
3404 log_debug_unit(u->id, "%s: got READY=1", u->id);
3405 service_enter_start_post(s);
3409 /* Interpret STATUS= */
3410 e = strv_find_prefix(tags, "STATUS=");
3415 if (!utf8_is_valid(e+7)) {
3416 log_warning_unit(u->id, "Status message in notification is not UTF-8 clean.");
3420 log_debug_unit(u->id, "%s: got %s", u->id, e);
3431 if (!streq_ptr(s->status_text, t)) {
3432 free(s->status_text);
3439 /* Interpret WATCHDOG= */
3440 if (strv_find(tags, "WATCHDOG=1")) {
3441 log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id);
3442 service_reset_watchdog(s);
3445 /* Notify clients about changed status or main pid */
3447 unit_add_to_dbus_queue(u);
3450 static int service_get_timeout(Unit *u, uint64_t *timeout) {
3451 Service *s = SERVICE(u);
3454 if (!s->timer_event_source)
3457 r = sd_event_source_get_time(s->timer_event_source, timeout);
3464 #ifdef HAVE_SYSV_COMPAT
3466 static int service_enumerate(Manager *m) {
3469 _cleanup_closedir_ DIR *d = NULL;
3470 _cleanup_free_ char *path = NULL, *fpath = NULL, *name = NULL;
3471 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
3472 _cleanup_set_free_ Set *shutdown_services = NULL;
3479 if (m->running_as != SYSTEMD_SYSTEM)
3482 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3483 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3487 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3498 if (errno != ENOENT)
3499 log_warning("opendir(%s) failed: %m", path);
3504 while ((de = readdir(d))) {
3507 if (ignore_file(de->d_name))
3510 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3513 if (strlen(de->d_name) < 4)
3516 a = undecchar(de->d_name[1]);
3517 b = undecchar(de->d_name[2]);
3523 fpath = strjoin(path, "/", de->d_name, NULL);
3529 if (access(fpath, X_OK) < 0) {
3531 if (errno != ENOENT)
3532 log_warning("access() failed on %s: %m", fpath);
3538 name = sysv_translate_name(de->d_name + 3);
3544 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3546 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3550 if (de->d_name[0] == 'S') {
3552 if (rcnd_table[i].type == RUNLEVEL_UP) {
3553 SERVICE(service)->sysv_start_priority_from_rcnd =
3554 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3556 SERVICE(service)->sysv_enabled = true;
3559 r = set_ensure_allocated(&runlevel_services[i],
3560 trivial_hash_func, trivial_compare_func);
3564 r = set_put(runlevel_services[i], service);
3568 } else if (de->d_name[0] == 'K' &&
3569 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3571 r = set_ensure_allocated(&shutdown_services,
3572 trivial_hash_func, trivial_compare_func);
3576 r = set_put(shutdown_services, service);
3583 /* Now we loaded all stubs and are aware of the lowest
3584 start-up priority for all services, not let's actually load
3585 the services, this will also tell us which services are
3586 actually native now */
3587 manager_dispatch_load_queue(m);
3589 /* If this is a native service, rely on native ways to pull in
3590 * a service, don't pull it in via sysv rcN.d links. */
3591 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3592 SET_FOREACH(service, runlevel_services[i], j) {
3593 service = unit_follow_merge(service);
3595 if (service->fragment_path)
3598 r = unit_add_two_dependencies_by_name_inverse(
3599 service, UNIT_AFTER, UNIT_WANTS,
3600 rcnd_table[i].target, NULL, true);
3605 /* We honour K links only for halt/reboot. For the normal
3606 * runlevels we assume the stop jobs will be implicitly added
3607 * by the core logic. Also, we don't really distinguish here
3608 * between the runlevels 0 and 6 and just add them to the
3609 * special shutdown target. */
3610 SET_FOREACH(service, shutdown_services, j) {
3611 service = unit_follow_merge(service);
3613 if (service->fragment_path)
3616 r = unit_add_two_dependencies_by_name(
3617 service, UNIT_BEFORE, UNIT_CONFLICTS,
3618 SPECIAL_SHUTDOWN_TARGET, NULL, true);
3627 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3628 set_free(runlevel_services[i]);
3634 static void service_bus_name_owner_change(
3637 const char *old_owner,
3638 const char *new_owner) {
3640 Service *s = SERVICE(u);
3646 assert(streq(s->bus_name, name));
3647 assert(old_owner || new_owner);
3649 if (old_owner && new_owner)
3650 log_debug_unit(u->id,
3651 "%s's D-Bus name %s changed owner from %s to %s",
3652 u->id, name, old_owner, new_owner);
3654 log_debug_unit(u->id,
3655 "%s's D-Bus name %s no longer registered by %s",
3656 u->id, name, old_owner);
3658 log_debug_unit(u->id,
3659 "%s's D-Bus name %s now registered by %s",
3660 u->id, name, new_owner);
3662 s->bus_name_good = !!new_owner;
3664 if (s->type == SERVICE_DBUS) {
3666 /* service_enter_running() will figure out what to
3668 if (s->state == SERVICE_RUNNING)
3669 service_enter_running(s, SERVICE_SUCCESS);
3670 else if (s->state == SERVICE_START && new_owner)
3671 service_enter_start_post(s);
3673 } else if (new_owner &&
3675 (s->state == SERVICE_START ||
3676 s->state == SERVICE_START_POST ||
3677 s->state == SERVICE_RUNNING ||
3678 s->state == SERVICE_RELOAD)) {
3680 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
3683 /* Try to acquire PID from bus service */
3685 r = sd_bus_get_owner(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
3687 r = sd_bus_creds_get_pid(creds, &pid);
3689 log_debug_unit(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3691 service_set_main_pid(s, pid);
3692 unit_watch_pid(UNIT(s), pid);
3697 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3698 _cleanup_free_ char *peer = NULL;
3704 /* This is called by the socket code when instantiating a new
3705 * service for a stream socket and the socket needs to be
3708 if (UNIT(s)->load_state != UNIT_LOADED)
3711 if (s->socket_fd >= 0)
3714 if (s->state != SERVICE_DEAD)
3717 if (getpeername_pretty(fd, &peer) >= 0) {
3719 if (UNIT(s)->description) {
3720 _cleanup_free_ char *a;
3722 a = strjoin(UNIT(s)->description, " (", peer, ")", NULL);
3726 r = unit_set_description(UNIT(s), a);
3728 r = unit_set_description(UNIT(s), peer);
3736 unit_ref_set(&s->accept_socket, UNIT(sock));
3738 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3741 static void service_reset_failed(Unit *u) {
3742 Service *s = SERVICE(u);
3746 if (s->state == SERVICE_FAILED)
3747 service_set_state(s, SERVICE_DEAD);
3749 s->result = SERVICE_SUCCESS;
3750 s->reload_result = SERVICE_SUCCESS;
3752 RATELIMIT_RESET(s->start_limit);
3755 static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
3756 Service *s = SERVICE(u);
3758 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
3761 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3762 [SERVICE_DEAD] = "dead",
3763 [SERVICE_START_PRE] = "start-pre",
3764 [SERVICE_START] = "start",
3765 [SERVICE_START_POST] = "start-post",
3766 [SERVICE_RUNNING] = "running",
3767 [SERVICE_EXITED] = "exited",
3768 [SERVICE_RELOAD] = "reload",
3769 [SERVICE_STOP] = "stop",
3770 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3771 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3772 [SERVICE_STOP_POST] = "stop-post",
3773 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3774 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3775 [SERVICE_FAILED] = "failed",
3776 [SERVICE_AUTO_RESTART] = "auto-restart",
3779 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3781 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3782 [SERVICE_RESTART_NO] = "no",
3783 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3784 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3785 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
3786 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3787 [SERVICE_RESTART_ALWAYS] = "always"
3790 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3792 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3793 [SERVICE_SIMPLE] = "simple",
3794 [SERVICE_FORKING] = "forking",
3795 [SERVICE_ONESHOT] = "oneshot",
3796 [SERVICE_DBUS] = "dbus",
3797 [SERVICE_NOTIFY] = "notify",
3798 [SERVICE_IDLE] = "idle"
3801 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3803 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3804 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3805 [SERVICE_EXEC_START] = "ExecStart",
3806 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3807 [SERVICE_EXEC_RELOAD] = "ExecReload",
3808 [SERVICE_EXEC_STOP] = "ExecStop",
3809 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3812 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3814 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3815 [NOTIFY_NONE] = "none",
3816 [NOTIFY_MAIN] = "main",
3817 [NOTIFY_ALL] = "all"
3820 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3822 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3823 [SERVICE_SUCCESS] = "success",
3824 [SERVICE_FAILURE_RESOURCES] = "resources",
3825 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3826 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3827 [SERVICE_FAILURE_SIGNAL] = "signal",
3828 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3829 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3830 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3833 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3835 static const char* const failure_action_table[_SERVICE_FAILURE_ACTION_MAX] = {
3836 [SERVICE_FAILURE_ACTION_NONE] = "none",
3837 [SERVICE_FAILURE_ACTION_REBOOT] = "reboot",
3838 [SERVICE_FAILURE_ACTION_REBOOT_FORCE] = "reboot-force",
3839 [SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate"
3841 DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction);
3843 const UnitVTable service_vtable = {
3844 .object_size = sizeof(Service),
3845 .exec_context_offset = offsetof(Service, exec_context),
3846 .cgroup_context_offset = offsetof(Service, cgroup_context),
3847 .kill_context_offset = offsetof(Service, kill_context),
3848 .exec_runtime_offset = offsetof(Service, exec_runtime),
3854 .private_section = "Service",
3856 .init = service_init,
3857 .done = service_done,
3858 .load = service_load,
3860 .coldplug = service_coldplug,
3862 .dump = service_dump,
3864 .start = service_start,
3865 .stop = service_stop,
3866 .reload = service_reload,
3868 .can_reload = service_can_reload,
3870 .kill = service_kill,
3872 .serialize = service_serialize,
3873 .deserialize_item = service_deserialize_item,
3875 .active_state = service_active_state,
3876 .sub_state_to_string = service_sub_state_to_string,
3878 .check_gc = service_check_gc,
3879 .check_snapshot = service_check_snapshot,
3881 .sigchld_event = service_sigchld_event,
3883 .reset_failed = service_reset_failed,
3885 .notify_cgroup_empty = service_notify_cgroup_empty_event,
3886 .notify_message = service_notify_message,
3888 .bus_name_owner_change = service_bus_name_owner_change,
3890 .bus_interface = "org.freedesktop.systemd1.Service",
3891 .bus_vtable = bus_service_vtable,
3892 .bus_set_property = bus_service_set_property,
3893 .bus_commit_properties = bus_service_commit_properties,
3895 .get_timeout = service_get_timeout,
3897 #ifdef HAVE_SYSV_COMPAT
3898 .enumerate = service_enumerate,
3901 .can_transient = true,
3903 .status_message_formats = {
3904 .starting_stopping = {
3905 [0] = "Starting %s...",
3906 [1] = "Stopping %s...",
3908 .finished_start_job = {
3909 [JOB_DONE] = "Started %s.",
3910 [JOB_FAILED] = "Failed to start %s.",
3911 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3912 [JOB_TIMEOUT] = "Timed out starting %s.",
3914 .finished_stop_job = {
3915 [JOB_DONE] = "Stopped %s.",
3916 [JOB_FAILED] = "Stopped (with error) %s.",
3917 [JOB_TIMEOUT] = "Timed out stopping %s.",