1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/reboot.h>
31 #include "load-fragment.h"
32 #include "load-dropin.h"
35 #include "unit-name.h"
36 #include "unit-printf.h"
37 #include "dbus-service.h"
39 #include "exit-status.h"
41 #include "path-util.h"
46 #include "bus-error.h"
49 #ifdef HAVE_SYSV_COMPAT
51 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
53 typedef enum RunlevelType {
61 const RunlevelType type;
63 /* Standard SysV runlevels for start-up */
64 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
65 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
66 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
67 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
68 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
70 /* Standard SysV runlevels for shutdown */
71 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
72 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
74 /* Note that the order here matters, as we read the
75 directories in this order, and we want to make sure that
76 sysv_start_priority is known when we first load the
77 unit. And that value we only know from S links. Hence
78 UP must be read before DOWN */
81 #define RUNLEVELS_UP "12345"
84 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
85 [SERVICE_DEAD] = UNIT_INACTIVE,
86 [SERVICE_START_PRE] = UNIT_ACTIVATING,
87 [SERVICE_START] = UNIT_ACTIVATING,
88 [SERVICE_START_POST] = UNIT_ACTIVATING,
89 [SERVICE_RUNNING] = UNIT_ACTIVE,
90 [SERVICE_EXITED] = UNIT_ACTIVE,
91 [SERVICE_RELOAD] = UNIT_RELOADING,
92 [SERVICE_STOP] = UNIT_DEACTIVATING,
93 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
94 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
95 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
96 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
97 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
98 [SERVICE_FAILED] = UNIT_FAILED,
99 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
102 /* For Type=idle we never want to delay any other jobs, hence we
103 * consider idle jobs active as soon as we start working on them */
104 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
105 [SERVICE_DEAD] = UNIT_INACTIVE,
106 [SERVICE_START_PRE] = UNIT_ACTIVE,
107 [SERVICE_START] = UNIT_ACTIVE,
108 [SERVICE_START_POST] = UNIT_ACTIVE,
109 [SERVICE_RUNNING] = UNIT_ACTIVE,
110 [SERVICE_EXITED] = UNIT_ACTIVE,
111 [SERVICE_RELOAD] = UNIT_RELOADING,
112 [SERVICE_STOP] = UNIT_DEACTIVATING,
113 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
114 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
115 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
116 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
117 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
118 [SERVICE_FAILED] = UNIT_FAILED,
119 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
122 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata);
123 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
124 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata);
126 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
128 static void service_init(Unit *u) {
129 Service *s = SERVICE(u);
132 assert(u->load_state == UNIT_STUB);
134 s->timeout_start_usec = u->manager->default_timeout_start_usec;
135 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
136 s->restart_usec = u->manager->default_restart_usec;
137 s->type = _SERVICE_TYPE_INVALID;
139 #ifdef HAVE_SYSV_COMPAT
140 s->sysv_start_priority = -1;
141 s->sysv_start_priority_from_rcnd = -1;
144 s->guess_main_pid = true;
146 exec_context_init(&s->exec_context);
147 kill_context_init(&s->kill_context);
148 cgroup_context_init(&s->cgroup_context);
150 RATELIMIT_INIT(s->start_limit,
151 u->manager->default_start_limit_interval,
152 u->manager->default_start_limit_burst);
154 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
157 static void service_unwatch_control_pid(Service *s) {
160 if (s->control_pid <= 0)
163 unit_unwatch_pid(UNIT(s), s->control_pid);
167 static void service_unwatch_main_pid(Service *s) {
170 if (s->main_pid <= 0)
173 unit_unwatch_pid(UNIT(s), s->main_pid);
177 static void service_unwatch_pid_file(Service *s) {
178 if (!s->pid_file_pathspec)
181 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
182 UNIT(s)->id, s->pid_file_pathspec->path);
183 path_spec_unwatch(s->pid_file_pathspec);
184 path_spec_done(s->pid_file_pathspec);
185 free(s->pid_file_pathspec);
186 s->pid_file_pathspec = NULL;
189 static int service_set_main_pid(Service *s, pid_t pid) {
200 if (s->main_pid == pid && s->main_pid_known)
203 if (s->main_pid != pid) {
204 service_unwatch_main_pid(s);
205 exec_status_start(&s->main_exec_status, pid);
209 s->main_pid_known = true;
211 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
212 log_warning_unit(UNIT(s)->id,
213 "%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
214 UNIT(s)->id, (unsigned long) pid);
216 s->main_pid_alien = true;
218 s->main_pid_alien = false;
223 static void service_close_socket_fd(Service *s) {
226 if (s->socket_fd < 0)
229 close_nointr_nofail(s->socket_fd);
233 static void service_connection_unref(Service *s) {
236 if (!UNIT_ISSET(s->accept_socket))
239 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
240 unit_ref_unset(&s->accept_socket);
243 static void service_stop_watchdog(Service *s) {
246 s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source);
247 s->watchdog_timestamp = DUAL_TIMESTAMP_NULL;
250 static void service_start_watchdog(Service *s) {
255 if (s->watchdog_usec <= 0)
258 if (s->watchdog_event_source) {
259 r = sd_event_source_set_time(s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec);
261 log_warning_unit(UNIT(s)->id, "%s failed to reset watchdog timer: %s", UNIT(s)->id, strerror(-r));
265 r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT);
267 r = sd_event_add_monotonic(UNIT(s)->manager->event, s->watchdog_timestamp.monotonic + s->watchdog_usec, 0, service_dispatch_watchdog, s, &s->watchdog_event_source);
270 log_warning_unit(UNIT(s)->id, "%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
273 static void service_reset_watchdog(Service *s) {
276 dual_timestamp_get(&s->watchdog_timestamp);
277 service_start_watchdog(s);
280 static void service_done(Unit *u) {
281 Service *s = SERVICE(u);
288 #ifdef HAVE_SYSV_COMPAT
289 free(s->sysv_runlevels);
290 s->sysv_runlevels = NULL;
293 free(s->status_text);
294 s->status_text = NULL;
296 cgroup_context_done(&s->cgroup_context);
297 exec_context_done(&s->exec_context);
298 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
299 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
300 s->control_command = NULL;
301 s->main_command = NULL;
303 set_free(s->restart_ignore_status.code);
304 s->restart_ignore_status.code = NULL;
305 set_free(s->restart_ignore_status.signal);
306 s->restart_ignore_status.signal = NULL;
308 set_free(s->success_status.code);
309 s->success_status.code = NULL;
310 set_free(s->success_status.signal);
311 s->success_status.signal = NULL;
313 /* This will leak a process, but at least no memory or any of
315 service_unwatch_main_pid(s);
316 service_unwatch_control_pid(s);
317 service_unwatch_pid_file(s);
320 unit_unwatch_bus_name(u, s->bus_name);
325 service_close_socket_fd(s);
326 service_connection_unref(s);
328 unit_ref_unset(&s->accept_socket);
330 service_stop_watchdog(s);
332 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
335 static int service_arm_timer(Service *s, usec_t usec) {
340 if (s->timer_event_source) {
341 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec);
345 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
348 return sd_event_add_monotonic(UNIT(s)->manager->event, now(CLOCK_MONOTONIC) + usec, 0, service_dispatch_timer, s, &s->timer_event_source);
351 #ifdef HAVE_SYSV_COMPAT
352 static char *sysv_translate_name(const char *name) {
355 r = new(char, strlen(name) + sizeof(".service"));
359 if (endswith(name, ".sh"))
360 /* Drop .sh suffix */
361 strcpy(stpcpy(r, name) - 3, ".service");
363 /* Normal init script name */
364 strcpy(stpcpy(r, name), ".service");
369 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
371 /* We silently ignore the $ prefix here. According to the LSB
372 * spec it simply indicates whether something is a
373 * standardized name or a distribution-specific one. Since we
374 * just follow what already exists and do not introduce new
375 * uses or names we don't care who introduced a new name. */
377 static const char * const table[] = {
378 /* LSB defined facilities */
380 "network", SPECIAL_NETWORK_TARGET,
381 "named", SPECIAL_NSS_LOOKUP_TARGET,
382 "portmap", SPECIAL_RPCBIND_TARGET,
383 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
385 "time", SPECIAL_TIME_SYNC_TARGET,
395 n = *name == '$' ? name + 1 : name;
397 for (i = 0; i < ELEMENTSOF(table); i += 2) {
399 if (!streq(table[i], n))
405 r = strdup(table[i+1]);
412 /* If we don't know this name, fallback heuristics to figure
413 * out whether something is a target or a service alias. */
416 if (!unit_prefix_is_valid(n))
419 /* Facilities starting with $ are most likely targets */
420 r = unit_name_build(n, NULL, ".target");
421 } else if (filename && streq(name, filename))
422 /* Names equaling the file name of the services are redundant */
425 /* Everything else we assume to be normal service names */
426 r = sysv_translate_name(n);
437 static int sysv_fix_order(Service *s) {
443 if (s->sysv_start_priority < 0)
446 /* For each pair of services where at least one lacks a LSB
447 * header, we use the start priority value to order things. */
449 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
452 bool special_s, special_t;
459 if (UNIT(t)->load_state != UNIT_LOADED)
462 if (t->sysv_start_priority < 0)
465 /* If both units have modern headers we don't care
466 * about the priorities */
467 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
468 (UNIT(t)->fragment_path || t->sysv_has_lsb))
471 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
472 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
474 if (special_t && !special_s)
476 else if (special_s && !special_t)
478 else if (t->sysv_start_priority < s->sysv_start_priority)
480 else if (t->sysv_start_priority > s->sysv_start_priority)
485 /* FIXME: Maybe we should compare the name here lexicographically? */
487 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
494 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
497 if (!(c = new0(ExecCommand, 1)))
500 if (!(c->path = strdup(path))) {
505 if (!(c->argv = strv_new(path, arg1, NULL))) {
514 static int sysv_exec_commands(Service *s, const bool supports_reload) {
519 assert(UNIT(s)->source_path);
521 c = exec_command_new(UNIT(s)->source_path, "start");
524 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
526 c = exec_command_new(UNIT(s)->source_path, "stop");
529 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
531 if (supports_reload) {
532 c = exec_command_new(UNIT(s)->source_path, "reload");
535 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
541 static bool usage_contains_reload(const char *line) {
542 return (strcasestr(line, "{reload|") ||
543 strcasestr(line, "{reload}") ||
544 strcasestr(line, "{reload\"") ||
545 strcasestr(line, "|reload|") ||
546 strcasestr(line, "|reload}") ||
547 strcasestr(line, "|reload\""));
550 static int service_load_sysv_path(Service *s, const char *path) {
562 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
564 bool supports_reload = false;
571 f = fopen(path, "re");
573 r = errno == ENOENT ? 0 : -errno;
577 if (fstat(fileno(f), &st) < 0) {
582 free(u->source_path);
583 u->source_path = strdup(path);
584 if (!u->source_path) {
588 u->source_mtime = timespec_load(&st.st_mtim);
590 if (null_or_empty(&st)) {
591 u->load_state = UNIT_MASKED;
599 char l[LINE_MAX], *t;
601 if (!fgets(l, sizeof(l), f)) {
606 log_error_unit(u->id,
607 "Failed to read configuration file '%s': %s",
616 /* Try to figure out whether this init script supports
617 * the reload operation. This heuristic looks for
618 * "Usage" lines which include the reload option. */
619 if ( state == USAGE_CONTINUATION ||
620 (state == NORMAL && strcasestr(t, "usage"))) {
621 if (usage_contains_reload(t)) {
622 supports_reload = true;
624 } else if (t[strlen(t)-1] == '\\')
625 state = USAGE_CONTINUATION;
633 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
635 s->sysv_has_lsb = true;
639 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
645 t += strspn(t, WHITESPACE);
647 if (state == NORMAL) {
649 /* Try to parse Red Hat style chkconfig headers */
651 if (startswith_no_case(t, "chkconfig:")) {
653 char runlevels[16], *k;
657 if (sscanf(t+10, "%15s %i %*i",
659 &start_priority) != 2) {
661 log_warning_unit(u->id,
662 "[%s:%u] Failed to parse chkconfig line. Ignoring.",
667 /* A start priority gathered from the
668 * symlink farms is preferred over the
669 * data from the LSB header. */
670 if (start_priority < 0 || start_priority > 99)
671 log_warning_unit(u->id,
672 "[%s:%u] Start priority out of range. Ignoring.",
675 s->sysv_start_priority = start_priority;
677 char_array_0(runlevels);
678 k = delete_chars(runlevels, WHITESPACE "-");
683 if (!(d = strdup(k))) {
688 free(s->sysv_runlevels);
689 s->sysv_runlevels = d;
692 } else if (startswith_no_case(t, "description:")) {
694 size_t k = strlen(t);
698 if (t[k-1] == '\\') {
703 if ((j = strstrip(t+12)) && *j) {
704 if (!(d = strdup(j))) {
711 free(chkconfig_description);
712 chkconfig_description = d;
714 } else if (startswith_no_case(t, "pidfile:")) {
721 if (!path_is_absolute(fn)) {
722 log_warning_unit(u->id,
723 "[%s:%u] PID file not absolute. Ignoring.",
728 if (!(fn = strdup(fn))) {
737 } else if (state == DESCRIPTION) {
739 /* Try to parse Red Hat style description
742 size_t k = strlen(t);
750 if ((j = strstrip(t)) && *j) {
753 if (chkconfig_description)
754 d = strjoin(chkconfig_description, " ", j, NULL);
763 free(chkconfig_description);
764 chkconfig_description = d;
767 } else if (state == LSB || state == LSB_DESCRIPTION) {
769 if (startswith_no_case(t, "Provides:")) {
775 FOREACH_WORD_QUOTED(w, z, t+9, i) {
778 if (!(n = strndup(w, z))) {
783 r = sysv_translate_facility(n, basename(path), &m);
792 if (unit_name_to_type(m) == UNIT_SERVICE)
793 r = unit_merge_by_name(u, m);
800 * indication that the
802 * now available. This
805 * targets do NOT pull
808 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
811 log_error_unit(u->id,
812 "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
813 path, line, m, strerror(-r));
818 } else if (startswith_no_case(t, "Required-Start:") ||
819 startswith_no_case(t, "Should-Start:") ||
820 startswith_no_case(t, "X-Start-Before:") ||
821 startswith_no_case(t, "X-Start-After:")) {
827 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
830 if (!(n = strndup(w, z))) {
835 r = sysv_translate_facility(n, basename(path), &m);
837 log_error_unit(u->id,
838 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
839 path, line, n, strerror(-r));
849 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
852 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
853 path, line, m, strerror(-r));
857 } else if (startswith_no_case(t, "Default-Start:")) {
862 k = delete_chars(t+14, WHITESPACE "-");
865 if (!(d = strdup(k))) {
870 free(s->sysv_runlevels);
871 s->sysv_runlevels = d;
874 } else if (startswith_no_case(t, "Description:")) {
877 state = LSB_DESCRIPTION;
879 if ((j = strstrip(t+12)) && *j) {
880 if (!(d = strdup(j))) {
887 free(long_description);
888 long_description = d;
890 } else if (startswith_no_case(t, "Short-Description:")) {
895 if ((j = strstrip(t+18)) && *j) {
896 if (!(d = strdup(j))) {
903 free(short_description);
904 short_description = d;
906 } else if (state == LSB_DESCRIPTION) {
908 if (startswith(l, "#\t") || startswith(l, "# ")) {
911 if ((j = strstrip(t)) && *j) {
914 if (long_description)
915 d = strjoin(long_description, " ", t, NULL);
924 free(long_description);
925 long_description = d;
934 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
937 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
938 /* If there a runlevels configured for this service
939 * but none of the standard ones, then we assume this
940 * is some special kind of service (which might be
941 * needed for early boot) and don't create any links
944 UNIT(s)->default_dependencies = false;
946 /* Don't timeout special services during boot (like fsck) */
947 s->timeout_start_usec = 0;
948 s->timeout_stop_usec = 0;
950 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
951 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
954 /* Special setting for all SysV services */
955 s->type = SERVICE_FORKING;
956 s->remain_after_exit = !s->pid_file;
957 s->guess_main_pid = false;
958 s->restart = SERVICE_RESTART_NO;
959 s->exec_context.ignore_sigpipe = false;
960 s->kill_context.kill_mode = KILL_PROCESS;
962 /* We use the long description only if
963 * no short description is set. */
965 if (short_description)
966 description = short_description;
967 else if (chkconfig_description)
968 description = chkconfig_description;
969 else if (long_description)
970 description = long_description;
977 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
985 /* The priority that has been set in /etc/rcN.d/ hierarchies
986 * takes precedence over what is stored as default in the LSB
988 if (s->sysv_start_priority_from_rcnd >= 0)
989 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
991 u->load_state = UNIT_LOADED;
999 free(short_description);
1000 free(long_description);
1001 free(chkconfig_description);
1006 static int service_load_sysv_name(Service *s, const char *name) {
1012 /* For SysV services we strip the *.sh suffixes. */
1013 if (endswith(name, ".sh.service"))
1016 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1020 path = strjoin(*p, "/", name, NULL);
1024 assert(endswith(path, ".service"));
1025 path[strlen(path)-8] = 0;
1027 r = service_load_sysv_path(s, path);
1029 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1030 /* Try *.sh source'able init scripts */
1031 strcat(path, ".sh");
1032 r = service_load_sysv_path(s, path);
1039 if (UNIT(s)->load_state != UNIT_STUB)
1046 static int service_load_sysv(Service *s) {
1053 /* Load service data from SysV init scripts, preferably with
1054 * LSB headers ... */
1056 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1059 if ((t = UNIT(s)->id))
1060 if ((r = service_load_sysv_name(s, t)) < 0)
1063 if (UNIT(s)->load_state == UNIT_STUB)
1064 SET_FOREACH(t, UNIT(s)->names, i) {
1065 if (t == UNIT(s)->id)
1068 if ((r = service_load_sysv_name(s, t)) < 0)
1071 if (UNIT(s)->load_state != UNIT_STUB)
1079 static int service_verify(Service *s) {
1082 if (UNIT(s)->load_state != UNIT_LOADED)
1085 if (!s->exec_command[SERVICE_EXEC_START]) {
1086 log_error_unit(UNIT(s)->id,
1087 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1091 if (s->type != SERVICE_ONESHOT &&
1092 s->exec_command[SERVICE_EXEC_START]->command_next) {
1093 log_error_unit(UNIT(s)->id,
1094 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1098 if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
1099 log_error_unit(UNIT(s)->id,
1100 "%s has Restart setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1104 if (s->type == SERVICE_DBUS && !s->bus_name) {
1105 log_error_unit(UNIT(s)->id,
1106 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1110 if (s->bus_name && s->type != SERVICE_DBUS)
1111 log_warning_unit(UNIT(s)->id,
1112 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1114 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1115 log_error_unit(UNIT(s)->id,
1116 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1123 static int service_add_default_dependencies(Service *s) {
1128 /* Add a number of automatic dependencies useful for the
1129 * majority of services. */
1131 /* First, pull in base system */
1132 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1133 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1134 SPECIAL_BASIC_TARGET, NULL, true);
1138 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1139 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1140 SPECIAL_SOCKETS_TARGET, NULL, true);
1144 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1145 SPECIAL_TIMERS_TARGET, NULL, true);
1149 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1150 SPECIAL_PATHS_TARGET, NULL, true);
1155 /* Second, activate normal shutdown */
1156 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
1157 SPECIAL_SHUTDOWN_TARGET, NULL, true);
1161 static void service_fix_output(Service *s) {
1164 /* If nothing has been explicitly configured, patch default
1165 * output in. If input is socket/tty we avoid this however,
1166 * since in that case we want output to default to the same
1167 * place as we read input from. */
1169 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1170 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1171 s->exec_context.std_input == EXEC_INPUT_NULL)
1172 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1174 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1175 s->exec_context.std_input == EXEC_INPUT_NULL)
1176 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1179 static int service_load(Unit *u) {
1181 Service *s = SERVICE(u);
1185 /* Load a .service file */
1186 r = unit_load_fragment(u);
1190 #ifdef HAVE_SYSV_COMPAT
1191 /* Load a classic init script as a fallback, if we couldn't find anything */
1192 if (u->load_state == UNIT_STUB) {
1193 r = service_load_sysv(s);
1199 /* Still nothing found? Then let's give up */
1200 if (u->load_state == UNIT_STUB)
1203 /* This is a new unit? Then let's add in some extras */
1204 if (u->load_state == UNIT_LOADED) {
1206 /* We were able to load something, then let's add in
1207 * the dropin directories. */
1208 r = unit_load_dropin(u);
1212 if (s->type == _SERVICE_TYPE_INVALID)
1213 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1215 /* Oneshot services have disabled start timeout by default */
1216 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1217 s->timeout_start_usec = 0;
1219 service_fix_output(s);
1221 r = unit_add_exec_dependencies(u, &s->exec_context);
1225 r = unit_add_default_slice(u);
1229 #ifdef HAVE_SYSV_COMPAT
1230 r = sysv_fix_order(s);
1236 r = unit_watch_bus_name(u, s->bus_name);
1241 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1242 s->notify_access = NOTIFY_MAIN;
1244 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1245 s->notify_access = NOTIFY_MAIN;
1247 if (s->type == SERVICE_DBUS || s->bus_name) {
1248 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES,
1249 SPECIAL_DBUS_SOCKET, NULL, true);
1254 if (UNIT(s)->default_dependencies) {
1255 r = service_add_default_dependencies(s);
1260 r = unit_exec_context_defaults(u, &s->exec_context);
1265 return service_verify(s);
1268 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1270 ServiceExecCommand c;
1271 Service *s = SERVICE(u);
1272 const char *prefix2;
1273 _cleanup_free_ char *p2 = NULL;
1277 p2 = strappend(prefix, "\t");
1278 prefix2 = p2 ? p2 : prefix;
1281 "%sService State: %s\n"
1283 "%sReload Result: %s\n"
1284 "%sPermissionsStartOnly: %s\n"
1285 "%sRootDirectoryStartOnly: %s\n"
1286 "%sRemainAfterExit: %s\n"
1287 "%sGuessMainPID: %s\n"
1290 "%sNotifyAccess: %s\n",
1291 prefix, service_state_to_string(s->state),
1292 prefix, service_result_to_string(s->result),
1293 prefix, service_result_to_string(s->reload_result),
1294 prefix, yes_no(s->permissions_start_only),
1295 prefix, yes_no(s->root_directory_start_only),
1296 prefix, yes_no(s->remain_after_exit),
1297 prefix, yes_no(s->guess_main_pid),
1298 prefix, service_type_to_string(s->type),
1299 prefix, service_restart_to_string(s->restart),
1300 prefix, notify_access_to_string(s->notify_access));
1302 if (s->control_pid > 0)
1304 "%sControl PID: %lu\n",
1305 prefix, (unsigned long) s->control_pid);
1307 if (s->main_pid > 0)
1310 "%sMain PID Known: %s\n"
1311 "%sMain PID Alien: %s\n",
1312 prefix, (unsigned long) s->main_pid,
1313 prefix, yes_no(s->main_pid_known),
1314 prefix, yes_no(s->main_pid_alien));
1319 prefix, s->pid_file);
1324 "%sBus Name Good: %s\n",
1325 prefix, s->bus_name,
1326 prefix, yes_no(s->bus_name_good));
1328 kill_context_dump(&s->kill_context, f, prefix);
1329 exec_context_dump(&s->exec_context, f, prefix);
1331 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1333 if (!s->exec_command[c])
1336 fprintf(f, "%s-> %s:\n",
1337 prefix, service_exec_command_to_string(c));
1339 exec_command_dump_list(s->exec_command[c], f, prefix2);
1342 #ifdef HAVE_SYSV_COMPAT
1345 "%sSysV Init Script has LSB Header: %s\n"
1346 "%sSysVEnabled: %s\n",
1347 prefix, yes_no(s->sysv_has_lsb),
1348 prefix, yes_no(s->sysv_enabled));
1350 if (s->sysv_start_priority >= 0)
1352 "%sSysVStartPriority: %i\n",
1353 prefix, s->sysv_start_priority);
1355 if (s->sysv_runlevels)
1356 fprintf(f, "%sSysVRunLevels: %s\n",
1357 prefix, s->sysv_runlevels);
1361 fprintf(f, "%sStatus Text: %s\n",
1362 prefix, s->status_text);
1365 static int service_load_pid_file(Service *s, bool may_warn) {
1366 _cleanup_free_ char *k = NULL;
1375 r = read_one_line_file(s->pid_file, &k);
1378 log_info_unit(UNIT(s)->id,
1379 "PID file %s not readable (yet?) after %s.",
1380 s->pid_file, service_state_to_string(s->state));
1384 r = parse_pid(k, &pid);
1387 log_info_unit(UNIT(s)->id,
1388 "Failed to read PID from file %s: %s",
1389 s->pid_file, strerror(-r));
1393 if (kill(pid, 0) < 0 && errno != EPERM) {
1395 log_info_unit(UNIT(s)->id,
1396 "PID %lu read from file %s does not exist.",
1397 (unsigned long) pid, s->pid_file);
1401 if (s->main_pid_known) {
1402 if (pid == s->main_pid)
1405 log_debug_unit(UNIT(s)->id,
1406 "Main PID changing: %lu -> %lu",
1407 (unsigned long) s->main_pid, (unsigned long) pid);
1408 service_unwatch_main_pid(s);
1409 s->main_pid_known = false;
1411 log_debug_unit(UNIT(s)->id,
1412 "Main PID loaded: %lu", (unsigned long) pid);
1414 r = service_set_main_pid(s, pid);
1418 r = unit_watch_pid(UNIT(s), pid);
1420 /* FIXME: we need to do something here */
1421 log_warning_unit(UNIT(s)->id,
1422 "Failed to watch PID %lu from service %s",
1423 (unsigned long) pid, UNIT(s)->id);
1430 static int service_search_main_pid(Service *s) {
1436 /* If we know it anyway, don't ever fallback to unreliable
1438 if (s->main_pid_known)
1441 if (!s->guess_main_pid)
1444 assert(s->main_pid <= 0);
1446 pid = unit_search_main_pid(UNIT(s));
1450 log_debug_unit(UNIT(s)->id,
1451 "Main PID guessed: %lu", (unsigned long) pid);
1452 r = service_set_main_pid(s, pid);
1456 r = unit_watch_pid(UNIT(s), pid);
1458 /* FIXME: we need to do something here */
1459 log_warning_unit(UNIT(s)->id,
1460 "Failed to watch PID %lu from service %s",
1461 (unsigned long) pid, UNIT(s)->id);
1467 static void service_set_state(Service *s, ServiceState state) {
1468 ServiceState old_state;
1469 const UnitActiveState *table;
1473 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1475 old_state = s->state;
1478 service_unwatch_pid_file(s);
1481 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1483 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
1484 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
1485 SERVICE_AUTO_RESTART))
1486 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1489 SERVICE_START, SERVICE_START_POST,
1490 SERVICE_RUNNING, SERVICE_RELOAD,
1491 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) {
1492 service_unwatch_main_pid(s);
1493 s->main_command = NULL;
1497 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1499 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
1500 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
1501 service_unwatch_control_pid(s);
1502 s->control_command = NULL;
1503 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1507 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1508 SERVICE_RUNNING, SERVICE_RELOAD,
1509 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
1510 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) &&
1511 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1512 service_close_socket_fd(s);
1513 service_connection_unref(s);
1516 if (IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
1517 service_stop_watchdog(s);
1519 /* For the inactive states unit_notify() will trim the cgroup,
1520 * but for exit we have to do that ourselves... */
1521 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1522 unit_destroy_cgroup(UNIT(s));
1524 /* For remain_after_exit services, let's see if we can "release" the
1525 * hold on the console, since unit_notify() only does that in case of
1526 * change of state */
1527 if (state == SERVICE_EXITED && s->remain_after_exit &&
1528 UNIT(s)->manager->n_on_console > 0) {
1529 ExecContext *ec = unit_get_exec_context(UNIT(s));
1530 if (ec && exec_context_may_touch_console(ec)) {
1531 Manager *m = UNIT(s)->manager;
1534 if (m->n_on_console == 0)
1535 /* unset no_console_output flag, since the console is free */
1536 m->no_console_output = false;
1540 if (old_state != state)
1541 log_debug_unit(UNIT(s)->id, "%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
1543 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1544 s->reload_result = SERVICE_SUCCESS;
1547 static int service_coldplug(Unit *u) {
1548 Service *s = SERVICE(u);
1552 assert(s->state == SERVICE_DEAD);
1554 if (s->deserialized_state != s->state) {
1556 if (s->deserialized_state == SERVICE_START_PRE ||
1557 s->deserialized_state == SERVICE_START ||
1558 s->deserialized_state == SERVICE_START_POST ||
1559 s->deserialized_state == SERVICE_RELOAD ||
1560 s->deserialized_state == SERVICE_STOP ||
1561 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1562 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1563 s->deserialized_state == SERVICE_STOP_POST ||
1564 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1565 s->deserialized_state == SERVICE_FINAL_SIGKILL) {
1569 k = s->deserialized_state == SERVICE_START_PRE || s->deserialized_state == SERVICE_START ||
1570 s->deserialized_state == SERVICE_START_POST || s->deserialized_state == SERVICE_RELOAD ?
1571 s->timeout_start_usec : s->timeout_stop_usec;
1573 /* For the start/stop timeouts 0 means off */
1575 r = service_arm_timer(s, k);
1581 if (s->deserialized_state == SERVICE_AUTO_RESTART) {
1583 /* The restart timeouts 0 means immediately */
1584 r = service_arm_timer(s, s->restart_usec);
1589 if ((s->deserialized_state == SERVICE_START &&
1590 (s->type == SERVICE_FORKING ||
1591 s->type == SERVICE_DBUS ||
1592 s->type == SERVICE_ONESHOT ||
1593 s->type == SERVICE_NOTIFY)) ||
1594 s->deserialized_state == SERVICE_START_POST ||
1595 s->deserialized_state == SERVICE_RUNNING ||
1596 s->deserialized_state == SERVICE_RELOAD ||
1597 s->deserialized_state == SERVICE_STOP ||
1598 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1599 s->deserialized_state == SERVICE_STOP_SIGKILL)
1600 if (s->main_pid > 0) {
1601 r = unit_watch_pid(UNIT(s), s->main_pid);
1606 if (s->deserialized_state == SERVICE_START_PRE ||
1607 s->deserialized_state == SERVICE_START ||
1608 s->deserialized_state == SERVICE_START_POST ||
1609 s->deserialized_state == SERVICE_RELOAD ||
1610 s->deserialized_state == SERVICE_STOP ||
1611 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1612 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1613 s->deserialized_state == SERVICE_STOP_POST ||
1614 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1615 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1616 if (s->control_pid > 0) {
1617 r = unit_watch_pid(UNIT(s), s->control_pid);
1622 if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
1623 service_start_watchdog(s);
1625 service_set_state(s, s->deserialized_state);
1631 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1635 unsigned rn_fds = 0;
1642 if (s->socket_fd >= 0)
1645 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1650 if (u->type != UNIT_SOCKET)
1655 r = socket_collect_fds(sock, &cfds, &cn_fds);
1668 t = new(int, rn_fds+cn_fds);
1675 memcpy(t, rfds, rn_fds * sizeof(int));
1676 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1681 rn_fds = rn_fds+cn_fds;
1696 static int service_spawn(
1701 bool apply_permissions,
1703 bool apply_tty_stdin,
1704 bool set_notify_socket,
1711 _cleanup_free_ int *fdsbuf = NULL;
1712 unsigned n_fds = 0, n_env = 0;
1713 _cleanup_strv_free_ char
1714 **argv = NULL, **final_env = NULL, **our_env = NULL;
1721 unit_realize_cgroup(UNIT(s));
1723 r = unit_setup_exec_runtime(UNIT(s));
1728 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1729 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1730 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1732 if (s->socket_fd >= 0) {
1733 fds = &s->socket_fd;
1736 r = service_collect_fds(s, &fdsbuf, &n_fds);
1744 if (timeout && s->timeout_start_usec > 0) {
1745 r = service_arm_timer(s, s->timeout_start_usec);
1749 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1751 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
1755 our_env = new0(char*, 5);
1761 if (set_notify_socket)
1762 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1767 if (s->main_pid > 0)
1768 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1773 if (s->watchdog_usec > 0)
1774 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1779 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
1780 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1785 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1791 if (is_control && UNIT(s)->cgroup_path) {
1792 path = strappenda(UNIT(s)->cgroup_path, "/control");
1793 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1795 path = UNIT(s)->cgroup_path;
1805 UNIT(s)->manager->confirm_spawn,
1806 UNIT(s)->manager->cgroup_supported,
1809 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1815 r = unit_watch_pid(UNIT(s), pid);
1817 /* FIXME: we need to do something here */
1826 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1831 static int main_pid_good(Service *s) {
1834 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1837 /* If we know the pid file, then lets just check if it is
1839 if (s->main_pid_known) {
1841 /* If it's an alien child let's check if it is still
1843 if (s->main_pid_alien && s->main_pid > 0)
1844 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1846 /* .. otherwise assume we'll get a SIGCHLD for it,
1847 * which we really should wait for to collect exit
1848 * status and code */
1849 return s->main_pid > 0;
1852 /* We don't know the pid */
1856 _pure_ static int control_pid_good(Service *s) {
1859 return s->control_pid > 0;
1862 static int cgroup_good(Service *s) {
1867 if (!UNIT(s)->cgroup_path)
1870 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1877 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1881 if (f != SERVICE_SUCCESS)
1884 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1886 if (allow_restart &&
1887 !s->forbid_restart &&
1888 (s->restart == SERVICE_RESTART_ALWAYS ||
1889 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1890 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1891 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1892 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1893 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1894 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1895 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1896 (s->result != SERVICE_FAILURE_SIGNAL ||
1897 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))) {
1899 r = service_arm_timer(s, s->restart_usec);
1903 service_set_state(s, SERVICE_AUTO_RESTART);
1906 s->forbid_restart = false;
1908 /* we want fresh tmpdirs in case service is started again immediately */
1909 exec_runtime_destroy(s->exec_runtime);
1910 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
1912 /* Try to delete the pid file. At this point it will be
1913 * out-of-date, and some software might be confused by it, so
1914 * let's remove it. */
1916 unlink_noerrno(s->pid_file);
1921 log_warning_unit(UNIT(s)->id,
1922 "%s failed to run install restart timer: %s",
1923 UNIT(s)->id, strerror(-r));
1924 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1927 static void service_enter_stop_post(Service *s, ServiceResult f) {
1931 if (f != SERVICE_SUCCESS)
1934 service_unwatch_control_pid(s);
1936 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1937 if (s->control_command) {
1938 s->control_command_id = SERVICE_EXEC_STOP_POST;
1940 r = service_spawn(s,
1944 !s->permissions_start_only,
1945 !s->root_directory_start_only,
1954 service_set_state(s, SERVICE_STOP_POST);
1956 service_enter_dead(s, SERVICE_SUCCESS, true);
1961 log_warning_unit(UNIT(s)->id,
1962 "%s failed to run 'stop-post' task: %s",
1963 UNIT(s)->id, strerror(-r));
1964 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1967 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1972 if (f != SERVICE_SUCCESS)
1975 r = unit_kill_context(
1978 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1986 if (s->timeout_stop_usec > 0) {
1987 r = service_arm_timer(s, s->timeout_stop_usec);
1992 service_set_state(s, state);
1993 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1994 service_enter_stop_post(s, SERVICE_SUCCESS);
1996 service_enter_dead(s, SERVICE_SUCCESS, true);
2001 log_warning_unit(UNIT(s)->id,
2002 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2004 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2005 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2007 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2010 static void service_enter_stop(Service *s, ServiceResult f) {
2015 if (f != SERVICE_SUCCESS)
2018 service_unwatch_control_pid(s);
2020 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
2021 if (s->control_command) {
2022 s->control_command_id = SERVICE_EXEC_STOP;
2024 r = service_spawn(s,
2028 !s->permissions_start_only,
2029 !s->root_directory_start_only,
2037 service_set_state(s, SERVICE_STOP);
2039 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2044 log_warning_unit(UNIT(s)->id,
2045 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2046 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2049 static void service_enter_running(Service *s, ServiceResult f) {
2050 int main_pid_ok, cgroup_ok;
2053 if (f != SERVICE_SUCCESS)
2056 main_pid_ok = main_pid_good(s);
2057 cgroup_ok = cgroup_good(s);
2059 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2060 (s->bus_name_good || s->type != SERVICE_DBUS))
2061 service_set_state(s, SERVICE_RUNNING);
2062 else if (s->remain_after_exit)
2063 service_set_state(s, SERVICE_EXITED);
2065 service_enter_stop(s, SERVICE_SUCCESS);
2068 static void service_enter_start_post(Service *s) {
2072 service_unwatch_control_pid(s);
2073 service_reset_watchdog(s);
2075 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
2076 if (s->control_command) {
2077 s->control_command_id = SERVICE_EXEC_START_POST;
2079 r = service_spawn(s,
2083 !s->permissions_start_only,
2084 !s->root_directory_start_only,
2092 service_set_state(s, SERVICE_START_POST);
2094 service_enter_running(s, SERVICE_SUCCESS);
2099 log_warning_unit(UNIT(s)->id,
2100 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2101 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2104 static void service_kill_control_processes(Service *s) {
2107 if (!UNIT(s)->cgroup_path)
2110 p = strappenda(UNIT(s)->cgroup_path, "/control");
2111 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
2114 static void service_enter_start(Service *s) {
2121 assert(s->exec_command[SERVICE_EXEC_START]);
2122 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2124 service_unwatch_control_pid(s);
2125 service_unwatch_main_pid(s);
2127 /* We want to ensure that nobody leaks processes from
2128 * START_PRE here, so let's go on a killing spree, People
2129 * should not spawn long running processes from START_PRE. */
2130 service_kill_control_processes(s);
2132 if (s->type == SERVICE_FORKING) {
2133 s->control_command_id = SERVICE_EXEC_START;
2134 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2136 s->main_command = NULL;
2138 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2139 s->control_command = NULL;
2141 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2144 r = service_spawn(s,
2146 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
2147 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2152 s->notify_access != NOTIFY_NONE,
2158 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2159 /* For simple services we immediately start
2160 * the START_POST binaries. */
2162 service_set_main_pid(s, pid);
2163 service_enter_start_post(s);
2165 } else if (s->type == SERVICE_FORKING) {
2167 /* For forking services we wait until the start
2168 * process exited. */
2170 s->control_pid = pid;
2171 service_set_state(s, SERVICE_START);
2173 } else if (s->type == SERVICE_ONESHOT ||
2174 s->type == SERVICE_DBUS ||
2175 s->type == SERVICE_NOTIFY) {
2177 /* For oneshot services we wait until the start
2178 * process exited, too, but it is our main process. */
2180 /* For D-Bus services we know the main pid right away,
2181 * but wait for the bus name to appear on the
2182 * bus. Notify services are similar. */
2184 service_set_main_pid(s, pid);
2185 service_set_state(s, SERVICE_START);
2187 assert_not_reached("Unknown service type");
2192 log_warning_unit(UNIT(s)->id,
2193 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2194 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2197 static void service_enter_start_pre(Service *s) {
2202 service_unwatch_control_pid(s);
2204 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
2205 if (s->control_command) {
2206 /* Before we start anything, let's clear up what might
2207 * be left from previous runs. */
2208 service_kill_control_processes(s);
2210 s->control_command_id = SERVICE_EXEC_START_PRE;
2212 r = service_spawn(s,
2216 !s->permissions_start_only,
2217 !s->root_directory_start_only,
2225 service_set_state(s, SERVICE_START_PRE);
2227 service_enter_start(s);
2232 log_warning_unit(UNIT(s)->id,
2233 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2234 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2237 static void service_enter_restart(Service *s) {
2238 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2243 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2244 /* Don't restart things if we are going down anyway */
2245 log_info_unit(UNIT(s)->id,
2246 "Stop job pending for unit, delaying automatic restart.");
2248 r = service_arm_timer(s, s->restart_usec);
2255 /* Any units that are bound to this service must also be
2256 * restarted. We use JOB_RESTART (instead of the more obvious
2257 * JOB_START) here so that those dependency jobs will be added
2259 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2263 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2264 * it will be canceled as part of the service_stop() call that
2265 * is executed as part of JOB_RESTART. */
2267 log_debug_unit(UNIT(s)->id,
2268 "%s scheduled restart job.", UNIT(s)->id);
2272 log_warning_unit(UNIT(s)->id,
2273 "%s failed to schedule restart job: %s",
2274 UNIT(s)->id, bus_error_message(&error, -r));
2275 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2278 static void service_enter_reload(Service *s) {
2283 service_unwatch_control_pid(s);
2285 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
2286 if (s->control_command) {
2287 s->control_command_id = SERVICE_EXEC_RELOAD;
2289 r = service_spawn(s,
2293 !s->permissions_start_only,
2294 !s->root_directory_start_only,
2302 service_set_state(s, SERVICE_RELOAD);
2304 service_enter_running(s, SERVICE_SUCCESS);
2309 log_warning_unit(UNIT(s)->id,
2310 "%s failed to run 'reload' task: %s",
2311 UNIT(s)->id, strerror(-r));
2312 s->reload_result = SERVICE_FAILURE_RESOURCES;
2313 service_enter_running(s, SERVICE_SUCCESS);
2316 static void service_run_next_control(Service *s) {
2320 assert(s->control_command);
2321 assert(s->control_command->command_next);
2323 assert(s->control_command_id != SERVICE_EXEC_START);
2325 s->control_command = s->control_command->command_next;
2326 service_unwatch_control_pid(s);
2328 r = service_spawn(s,
2332 !s->permissions_start_only,
2333 !s->root_directory_start_only,
2334 s->control_command_id == SERVICE_EXEC_START_PRE ||
2335 s->control_command_id == SERVICE_EXEC_STOP_POST,
2345 log_warning_unit(UNIT(s)->id,
2346 "%s failed to run next control task: %s",
2347 UNIT(s)->id, strerror(-r));
2349 if (s->state == SERVICE_START_PRE)
2350 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2351 else if (s->state == SERVICE_STOP)
2352 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2353 else if (s->state == SERVICE_STOP_POST)
2354 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2355 else if (s->state == SERVICE_RELOAD) {
2356 s->reload_result = SERVICE_FAILURE_RESOURCES;
2357 service_enter_running(s, SERVICE_SUCCESS);
2359 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2362 static void service_run_next_main(Service *s) {
2367 assert(s->main_command);
2368 assert(s->main_command->command_next);
2369 assert(s->type == SERVICE_ONESHOT);
2371 s->main_command = s->main_command->command_next;
2372 service_unwatch_main_pid(s);
2374 r = service_spawn(s,
2381 s->notify_access != NOTIFY_NONE,
2387 service_set_main_pid(s, pid);
2392 log_warning_unit(UNIT(s)->id,
2393 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2394 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2397 static int service_start_limit_test(Service *s) {
2400 if (ratelimit_test(&s->start_limit))
2403 switch (s->start_limit_action) {
2405 case SERVICE_START_LIMIT_NONE:
2406 log_warning_unit(UNIT(s)->id,
2407 "%s start request repeated too quickly, refusing to start.",
2411 case SERVICE_START_LIMIT_REBOOT: {
2412 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2415 log_warning_unit(UNIT(s)->id,
2416 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2418 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
2419 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
2420 true, &error, NULL);
2422 log_error_unit(UNIT(s)->id,
2423 "Failed to reboot: %s.", bus_error_message(&error, r));
2428 case SERVICE_START_LIMIT_REBOOT_FORCE:
2429 log_warning_unit(UNIT(s)->id,
2430 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2431 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2434 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2435 log_warning_unit(UNIT(s)->id,
2436 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2438 reboot(RB_AUTOBOOT);
2442 log_error_unit(UNIT(s)->id,
2443 "start limit action=%i", s->start_limit_action);
2444 assert_not_reached("Unknown StartLimitAction.");
2450 static int service_start(Unit *u) {
2451 Service *s = SERVICE(u);
2456 /* We cannot fulfill this request right now, try again later
2458 if (s->state == SERVICE_STOP ||
2459 s->state == SERVICE_STOP_SIGTERM ||
2460 s->state == SERVICE_STOP_SIGKILL ||
2461 s->state == SERVICE_STOP_POST ||
2462 s->state == SERVICE_FINAL_SIGTERM ||
2463 s->state == SERVICE_FINAL_SIGKILL)
2466 /* Already on it! */
2467 if (s->state == SERVICE_START_PRE ||
2468 s->state == SERVICE_START ||
2469 s->state == SERVICE_START_POST)
2472 /* A service that will be restarted must be stopped first to
2473 * trigger BindsTo and/or OnFailure dependencies. If a user
2474 * does not want to wait for the holdoff time to elapse, the
2475 * service should be manually restarted, not started. We
2476 * simply return EAGAIN here, so that any start jobs stay
2477 * queued, and assume that the auto restart timer will
2478 * eventually trigger the restart. */
2479 if (s->state == SERVICE_AUTO_RESTART)
2482 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2484 /* Make sure we don't enter a busy loop of some kind. */
2485 r = service_start_limit_test(s);
2487 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2491 s->result = SERVICE_SUCCESS;
2492 s->reload_result = SERVICE_SUCCESS;
2493 s->main_pid_known = false;
2494 s->main_pid_alien = false;
2495 s->forbid_restart = false;
2497 service_enter_start_pre(s);
2501 static int service_stop(Unit *u) {
2502 Service *s = SERVICE(u);
2506 /* Don't create restart jobs from here. */
2507 s->forbid_restart = true;
2510 if (s->state == SERVICE_STOP ||
2511 s->state == SERVICE_STOP_SIGTERM ||
2512 s->state == SERVICE_STOP_SIGKILL ||
2513 s->state == SERVICE_STOP_POST ||
2514 s->state == SERVICE_FINAL_SIGTERM ||
2515 s->state == SERVICE_FINAL_SIGKILL)
2518 /* A restart will be scheduled or is in progress. */
2519 if (s->state == SERVICE_AUTO_RESTART) {
2520 service_set_state(s, SERVICE_DEAD);
2524 /* If there's already something running we go directly into
2526 if (s->state == SERVICE_START_PRE ||
2527 s->state == SERVICE_START ||
2528 s->state == SERVICE_START_POST ||
2529 s->state == SERVICE_RELOAD) {
2530 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2534 assert(s->state == SERVICE_RUNNING ||
2535 s->state == SERVICE_EXITED);
2537 service_enter_stop(s, SERVICE_SUCCESS);
2541 static int service_reload(Unit *u) {
2542 Service *s = SERVICE(u);
2546 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2548 service_enter_reload(s);
2552 _pure_ static bool service_can_reload(Unit *u) {
2553 Service *s = SERVICE(u);
2557 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2560 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2561 Service *s = SERVICE(u);
2567 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2568 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2569 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2571 if (s->control_pid > 0)
2572 unit_serialize_item_format(u, f, "control-pid", "%lu",
2573 (unsigned long) s->control_pid);
2575 if (s->main_pid_known && s->main_pid > 0)
2576 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2578 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2581 unit_serialize_item(u, f, "status-text", s->status_text);
2583 /* FIXME: There's a minor uncleanliness here: if there are
2584 * multiple commands attached here, we will start from the
2585 * first one again */
2586 if (s->control_command_id >= 0)
2587 unit_serialize_item(u, f, "control-command",
2588 service_exec_command_to_string(s->control_command_id));
2590 if (s->socket_fd >= 0) {
2593 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2596 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2599 if (s->main_exec_status.pid > 0) {
2600 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu",
2601 (unsigned long) s->main_exec_status.pid);
2602 dual_timestamp_serialize(f, "main-exec-status-start",
2603 &s->main_exec_status.start_timestamp);
2604 dual_timestamp_serialize(f, "main-exec-status-exit",
2605 &s->main_exec_status.exit_timestamp);
2607 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2608 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
2609 s->main_exec_status.code);
2610 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
2611 s->main_exec_status.status);
2614 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2615 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2617 if (s->forbid_restart)
2618 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
2623 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2624 Service *s = SERVICE(u);
2631 if (streq(key, "state")) {
2634 state = service_state_from_string(value);
2636 log_debug_unit(u->id, "Failed to parse state value %s", value);
2638 s->deserialized_state = state;
2639 } else if (streq(key, "result")) {
2642 f = service_result_from_string(value);
2644 log_debug_unit(u->id, "Failed to parse result value %s", value);
2645 else if (f != SERVICE_SUCCESS)
2648 } else if (streq(key, "reload-result")) {
2651 f = service_result_from_string(value);
2653 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2654 else if (f != SERVICE_SUCCESS)
2655 s->reload_result = f;
2657 } else if (streq(key, "control-pid")) {
2660 if (parse_pid(value, &pid) < 0)
2661 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2663 s->control_pid = pid;
2664 } else if (streq(key, "main-pid")) {
2667 if (parse_pid(value, &pid) < 0)
2668 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2670 service_set_main_pid(s, pid);
2671 unit_watch_pid(UNIT(s), pid);
2673 } else if (streq(key, "main-pid-known")) {
2676 b = parse_boolean(value);
2678 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2680 s->main_pid_known = b;
2681 } else if (streq(key, "status-text")) {
2688 free(s->status_text);
2692 } else if (streq(key, "control-command")) {
2693 ServiceExecCommand id;
2695 id = service_exec_command_from_string(value);
2697 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2699 s->control_command_id = id;
2700 s->control_command = s->exec_command[id];
2702 } else if (streq(key, "socket-fd")) {
2705 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2706 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2709 if (s->socket_fd >= 0)
2710 close_nointr_nofail(s->socket_fd);
2711 s->socket_fd = fdset_remove(fds, fd);
2713 } else if (streq(key, "main-exec-status-pid")) {
2716 if (parse_pid(value, &pid) < 0)
2717 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2719 s->main_exec_status.pid = pid;
2720 } else if (streq(key, "main-exec-status-code")) {
2723 if (safe_atoi(value, &i) < 0)
2724 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2726 s->main_exec_status.code = i;
2727 } else if (streq(key, "main-exec-status-status")) {
2730 if (safe_atoi(value, &i) < 0)
2731 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2733 s->main_exec_status.status = i;
2734 } else if (streq(key, "main-exec-status-start"))
2735 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2736 else if (streq(key, "main-exec-status-exit"))
2737 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2738 else if (streq(key, "watchdog-timestamp"))
2739 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2740 else if (streq(key, "forbid-restart")) {
2743 b = parse_boolean(value);
2745 log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value);
2747 s->forbid_restart = b;
2749 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2754 _pure_ static UnitActiveState service_active_state(Unit *u) {
2755 const UnitActiveState *table;
2759 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2761 return table[SERVICE(u)->state];
2764 static const char *service_sub_state_to_string(Unit *u) {
2767 return service_state_to_string(SERVICE(u)->state);
2770 static bool service_check_gc(Unit *u) {
2771 Service *s = SERVICE(u);
2775 /* Never clean up services that still have a process around,
2776 * even if the service is formally dead. */
2777 if (cgroup_good(s) > 0 ||
2778 main_pid_good(s) > 0 ||
2779 control_pid_good(s) > 0)
2782 #ifdef HAVE_SYSV_COMPAT
2790 _pure_ static bool service_check_snapshot(Unit *u) {
2791 Service *s = SERVICE(u);
2795 return (s->socket_fd < 0);
2798 static int service_retry_pid_file(Service *s) {
2801 assert(s->pid_file);
2802 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2804 r = service_load_pid_file(s, false);
2808 service_unwatch_pid_file(s);
2810 service_enter_running(s, SERVICE_SUCCESS);
2814 static int service_watch_pid_file(Service *s) {
2817 log_debug_unit(UNIT(s)->id,
2818 "Setting watch for %s's PID file %s",
2819 UNIT(s)->id, s->pid_file_pathspec->path);
2820 r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
2824 /* the pidfile might have appeared just before we set the watch */
2825 log_debug_unit(UNIT(s)->id,
2826 "Trying to read %s's PID file %s in case it changed",
2827 UNIT(s)->id, s->pid_file_pathspec->path);
2828 service_retry_pid_file(s);
2832 log_error_unit(UNIT(s)->id,
2833 "Failed to set a watch for %s's PID file %s: %s",
2834 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2835 service_unwatch_pid_file(s);
2839 static int service_demand_pid_file(Service *s) {
2842 assert(s->pid_file);
2843 assert(!s->pid_file_pathspec);
2845 ps = new0(PathSpec, 1);
2850 ps->path = strdup(s->pid_file);
2856 path_kill_slashes(ps->path);
2858 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2859 * keep their PID file open all the time. */
2860 ps->type = PATH_MODIFIED;
2861 ps->inotify_fd = -1;
2863 s->pid_file_pathspec = ps;
2865 return service_watch_pid_file(s);
2868 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
2869 PathSpec *p = userdata;
2874 s = SERVICE(p->unit);
2878 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2879 assert(s->pid_file_pathspec);
2880 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2882 log_debug_unit(UNIT(s)->id, "inotify event for %s", UNIT(s)->id);
2884 if (path_spec_fd_event(p, events) < 0)
2887 if (service_retry_pid_file(s) == 0)
2890 if (service_watch_pid_file(s) < 0)
2896 service_unwatch_pid_file(s);
2897 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2901 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2902 Service *s = SERVICE(u);
2908 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2909 is_clean_exit_lsb(code, status, &s->success_status))
2910 f = SERVICE_SUCCESS;
2911 else if (code == CLD_EXITED)
2912 f = SERVICE_FAILURE_EXIT_CODE;
2913 else if (code == CLD_KILLED)
2914 f = SERVICE_FAILURE_SIGNAL;
2915 else if (code == CLD_DUMPED)
2916 f = SERVICE_FAILURE_CORE_DUMP;
2918 assert_not_reached("Unknown code");
2920 if (s->main_pid == pid) {
2921 /* Forking services may occasionally move to a new PID.
2922 * As long as they update the PID file before exiting the old
2923 * PID, they're fine. */
2924 if (service_load_pid_file(s, false) == 0)
2928 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2930 if (s->main_command) {
2931 /* If this is not a forking service than the
2932 * main process got started and hence we copy
2933 * the exit status so that it is recorded both
2934 * as main and as control process exit
2937 s->main_command->exec_status = s->main_exec_status;
2939 if (s->main_command->ignore)
2940 f = SERVICE_SUCCESS;
2941 } else if (s->exec_command[SERVICE_EXEC_START]) {
2943 /* If this is a forked process, then we should
2944 * ignore the return value if this was
2945 * configured for the starter process */
2947 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2948 f = SERVICE_SUCCESS;
2951 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2953 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2954 u->id, sigchld_code_to_string(code), status,
2955 strna(code == CLD_EXITED
2956 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2957 : signal_to_string(status)),
2958 "EXIT_CODE=%s", sigchld_code_to_string(code),
2959 "EXIT_STATUS=%i", status,
2962 if (f != SERVICE_SUCCESS)
2965 if (s->main_command &&
2966 s->main_command->command_next &&
2967 f == SERVICE_SUCCESS) {
2969 /* There is another command to *
2970 * execute, so let's do that. */
2972 log_debug_unit(u->id,
2973 "%s running next main command for state %s",
2974 u->id, service_state_to_string(s->state));
2975 service_run_next_main(s);
2979 /* The service exited, so the service is officially
2981 s->main_command = NULL;
2985 case SERVICE_START_POST:
2986 case SERVICE_RELOAD:
2988 /* Need to wait until the operation is
2993 if (s->type == SERVICE_ONESHOT) {
2994 /* This was our main goal, so let's go on */
2995 if (f == SERVICE_SUCCESS)
2996 service_enter_start_post(s);
2998 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3004 case SERVICE_RUNNING:
3005 service_enter_running(s, f);
3008 case SERVICE_STOP_SIGTERM:
3009 case SERVICE_STOP_SIGKILL:
3011 if (!control_pid_good(s))
3012 service_enter_stop_post(s, f);
3014 /* If there is still a control process, wait for that first */
3018 assert_not_reached("Uh, main process died at wrong time.");
3022 } else if (s->control_pid == pid) {
3025 if (s->control_command) {
3026 exec_status_exit(&s->control_command->exec_status,
3027 &s->exec_context, pid, code, status);
3029 if (s->control_command->ignore)
3030 f = SERVICE_SUCCESS;
3033 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
3034 "%s: control process exited, code=%s status=%i",
3035 u->id, sigchld_code_to_string(code), status);
3037 if (f != SERVICE_SUCCESS)
3040 /* Immediately get rid of the cgroup, so that the
3041 * kernel doesn't delay the cgroup empty messages for
3042 * the service cgroup any longer than necessary */
3043 service_kill_control_processes(s);
3045 if (s->control_command &&
3046 s->control_command->command_next &&
3047 f == SERVICE_SUCCESS) {
3049 /* There is another command to *
3050 * execute, so let's do that. */
3052 log_debug_unit(u->id,
3053 "%s running next control command for state %s",
3054 u->id, service_state_to_string(s->state));
3055 service_run_next_control(s);
3058 /* No further commands for this step, so let's
3059 * figure out what to do next */
3061 s->control_command = NULL;
3062 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3064 log_debug_unit(u->id,
3065 "%s got final SIGCHLD for state %s",
3066 u->id, service_state_to_string(s->state));
3070 case SERVICE_START_PRE:
3071 if (f == SERVICE_SUCCESS)
3072 service_enter_start(s);
3074 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3078 if (s->type != SERVICE_FORKING)
3079 /* Maybe spurious event due to a reload that changed the type? */
3082 if (f != SERVICE_SUCCESS) {
3083 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3088 bool has_start_post;
3091 /* Let's try to load the pid file here if we can.
3092 * The PID file might actually be created by a START_POST
3093 * script. In that case don't worry if the loading fails. */
3095 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3096 r = service_load_pid_file(s, !has_start_post);
3097 if (!has_start_post && r < 0) {
3098 r = service_demand_pid_file(s);
3099 if (r < 0 || !cgroup_good(s))
3100 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3104 service_search_main_pid(s);
3106 service_enter_start_post(s);
3109 case SERVICE_START_POST:
3110 if (f != SERVICE_SUCCESS) {
3111 service_enter_stop(s, f);
3118 r = service_load_pid_file(s, true);
3120 r = service_demand_pid_file(s);
3121 if (r < 0 || !cgroup_good(s))
3122 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3126 service_search_main_pid(s);
3128 service_enter_running(s, SERVICE_SUCCESS);
3131 case SERVICE_RELOAD:
3132 if (f == SERVICE_SUCCESS) {
3133 service_load_pid_file(s, true);
3134 service_search_main_pid(s);
3137 s->reload_result = f;
3138 service_enter_running(s, SERVICE_SUCCESS);
3142 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3145 case SERVICE_STOP_SIGTERM:
3146 case SERVICE_STOP_SIGKILL:
3147 if (main_pid_good(s) <= 0)
3148 service_enter_stop_post(s, f);
3150 /* If there is still a service
3151 * process around, wait until
3152 * that one quit, too */
3155 case SERVICE_STOP_POST:
3156 case SERVICE_FINAL_SIGTERM:
3157 case SERVICE_FINAL_SIGKILL:
3158 service_enter_dead(s, f, true);
3162 assert_not_reached("Uh, control process died at wrong time.");
3167 /* Notify clients about changed exit status */
3168 unit_add_to_dbus_queue(u);
3171 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
3172 Service *s = SERVICE(userdata);
3175 assert(source == s->timer_event_source);
3179 case SERVICE_START_PRE:
3181 log_warning_unit(UNIT(s)->id,
3182 "%s operation timed out. Terminating.", UNIT(s)->id);
3183 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3186 case SERVICE_START_POST:
3187 log_warning_unit(UNIT(s)->id,
3188 "%s operation timed out. Stopping.", UNIT(s)->id);
3189 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3192 case SERVICE_RELOAD:
3193 log_warning_unit(UNIT(s)->id,
3194 "%s operation timed out. Stopping.", UNIT(s)->id);
3195 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3196 service_enter_running(s, SERVICE_SUCCESS);
3200 log_warning_unit(UNIT(s)->id,
3201 "%s stopping timed out. Terminating.", UNIT(s)->id);
3202 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3205 case SERVICE_STOP_SIGTERM:
3206 if (s->kill_context.send_sigkill) {
3207 log_warning_unit(UNIT(s)->id,
3208 "%s stopping timed out. Killing.", UNIT(s)->id);
3209 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3211 log_warning_unit(UNIT(s)->id,
3212 "%s stopping timed out. Skipping SIGKILL.", UNIT(s)->id);
3213 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3218 case SERVICE_STOP_SIGKILL:
3219 /* Uh, we sent a SIGKILL and it is still not gone?
3220 * Must be something we cannot kill, so let's just be
3221 * weirded out and continue */
3223 log_warning_unit(UNIT(s)->id,
3224 "%s still around after SIGKILL. Ignoring.", UNIT(s)->id);
3225 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3228 case SERVICE_STOP_POST:
3229 log_warning_unit(UNIT(s)->id,
3230 "%s stopping timed out (2). Terminating.", UNIT(s)->id);
3231 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3234 case SERVICE_FINAL_SIGTERM:
3235 if (s->kill_context.send_sigkill) {
3236 log_warning_unit(UNIT(s)->id,
3237 "%s stopping timed out (2). Killing.", UNIT(s)->id);
3238 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3240 log_warning_unit(UNIT(s)->id,
3241 "%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.",
3243 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3248 case SERVICE_FINAL_SIGKILL:
3249 log_warning_unit(UNIT(s)->id,
3250 "%s still around after SIGKILL (2). Entering failed mode.", UNIT(s)->id);
3251 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3254 case SERVICE_AUTO_RESTART:
3255 log_info_unit(UNIT(s)->id,
3256 "%s holdoff time over, scheduling restart.", UNIT(s)->id);
3257 service_enter_restart(s);
3261 assert_not_reached("Timeout at wrong time.");
3267 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
3268 Service *s = SERVICE(userdata);
3271 assert(source == s->watchdog_event_source);
3273 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
3274 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_WATCHDOG);
3279 static void service_notify_cgroup_empty_event(Unit *u) {
3280 Service *s = SERVICE(u);
3284 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
3288 /* Waiting for SIGCHLD is usually more interesting,
3289 * because it includes return codes/signals. Which is
3290 * why we ignore the cgroup events for most cases,
3291 * except when we don't know pid which to expect the
3295 case SERVICE_START_POST:
3296 /* If we were hoping for the daemon to write its PID file,
3297 * we can give up now. */
3298 if (s->pid_file_pathspec) {
3299 log_warning_unit(u->id,
3300 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3301 service_unwatch_pid_file(s);
3302 if (s->state == SERVICE_START)
3303 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3305 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3309 case SERVICE_RUNNING:
3310 /* service_enter_running() will figure out what to do */
3311 service_enter_running(s, SERVICE_SUCCESS);
3314 case SERVICE_STOP_SIGTERM:
3315 case SERVICE_STOP_SIGKILL:
3317 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3318 service_enter_stop_post(s, SERVICE_SUCCESS);
3322 case SERVICE_FINAL_SIGTERM:
3323 case SERVICE_FINAL_SIGKILL:
3324 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3325 service_enter_dead(s, SERVICE_SUCCESS, true);
3334 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3335 Service *s = SERVICE(u);
3340 if (s->notify_access == NOTIFY_NONE) {
3341 log_warning_unit(u->id,
3342 "%s: Got notification message from PID %lu, but reception is disabled.",
3343 u->id, (unsigned long) pid);
3347 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3348 log_warning_unit(u->id,
3349 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3350 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3354 log_debug_unit(u->id,
3355 "%s: Got message", u->id);
3357 /* Interpret MAINPID= */
3358 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3359 (s->state == SERVICE_START ||
3360 s->state == SERVICE_START_POST ||
3361 s->state == SERVICE_RUNNING ||
3362 s->state == SERVICE_RELOAD)) {
3364 if (parse_pid(e + 8, &pid) < 0)
3365 log_warning_unit(u->id,
3366 "Failed to parse notification message %s", e);
3368 log_debug_unit(u->id,
3369 "%s: got %s", u->id, e);
3370 service_set_main_pid(s, pid);
3371 unit_watch_pid(UNIT(s), pid);
3375 /* Interpret READY= */
3376 if (s->type == SERVICE_NOTIFY &&
3377 s->state == SERVICE_START &&
3378 strv_find(tags, "READY=1")) {
3379 log_debug_unit(u->id,
3380 "%s: got READY=1", u->id);
3382 service_enter_start_post(s);
3385 /* Interpret STATUS= */
3386 e = strv_find_prefix(tags, "STATUS=");
3392 if (!utf8_is_valid(e+7)) {
3393 log_warning_unit(u->id,
3394 "Status message in notification is not UTF-8 clean.");
3400 log_error_unit(u->id,
3401 "Failed to allocate string.");
3405 log_debug_unit(u->id,
3406 "%s: got %s", u->id, e);
3408 free(s->status_text);
3411 free(s->status_text);
3412 s->status_text = NULL;
3417 if (strv_find(tags, "WATCHDOG=1")) {
3418 log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id);
3419 service_reset_watchdog(s);
3422 /* Notify clients about changed status or main pid */
3423 unit_add_to_dbus_queue(u);
3426 #ifdef HAVE_SYSV_COMPAT
3428 static int service_enumerate(Manager *m) {
3431 _cleanup_closedir_ DIR *d = NULL;
3432 _cleanup_free_ char *path = NULL, *fpath = NULL, *name = NULL;
3433 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
3434 _cleanup_set_free_ Set *shutdown_services = NULL;
3441 if (m->running_as != SYSTEMD_SYSTEM)
3444 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3445 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3449 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3460 if (errno != ENOENT)
3461 log_warning("opendir(%s) failed: %m", path);
3466 while ((de = readdir(d))) {
3469 if (ignore_file(de->d_name))
3472 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3475 if (strlen(de->d_name) < 4)
3478 a = undecchar(de->d_name[1]);
3479 b = undecchar(de->d_name[2]);
3485 fpath = strjoin(path, "/", de->d_name, NULL);
3491 if (access(fpath, X_OK) < 0) {
3493 if (errno != ENOENT)
3494 log_warning("access() failed on %s: %m", fpath);
3500 name = sysv_translate_name(de->d_name + 3);
3506 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3508 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3512 if (de->d_name[0] == 'S') {
3514 if (rcnd_table[i].type == RUNLEVEL_UP) {
3515 SERVICE(service)->sysv_start_priority_from_rcnd =
3516 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3518 SERVICE(service)->sysv_enabled = true;
3521 r = set_ensure_allocated(&runlevel_services[i],
3522 trivial_hash_func, trivial_compare_func);
3526 r = set_put(runlevel_services[i], service);
3530 } else if (de->d_name[0] == 'K' &&
3531 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3533 r = set_ensure_allocated(&shutdown_services,
3534 trivial_hash_func, trivial_compare_func);
3538 r = set_put(shutdown_services, service);
3545 /* Now we loaded all stubs and are aware of the lowest
3546 start-up priority for all services, not let's actually load
3547 the services, this will also tell us which services are
3548 actually native now */
3549 manager_dispatch_load_queue(m);
3551 /* If this is a native service, rely on native ways to pull in
3552 * a service, don't pull it in via sysv rcN.d links. */
3553 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3554 SET_FOREACH(service, runlevel_services[i], j) {
3555 service = unit_follow_merge(service);
3557 if (service->fragment_path)
3560 r = unit_add_two_dependencies_by_name_inverse(
3561 service, UNIT_AFTER, UNIT_WANTS,
3562 rcnd_table[i].target, NULL, true);
3567 /* We honour K links only for halt/reboot. For the normal
3568 * runlevels we assume the stop jobs will be implicitly added
3569 * by the core logic. Also, we don't really distinguish here
3570 * between the runlevels 0 and 6 and just add them to the
3571 * special shutdown target. */
3572 SET_FOREACH(service, shutdown_services, j) {
3573 service = unit_follow_merge(service);
3575 if (service->fragment_path)
3578 r = unit_add_two_dependencies_by_name(
3579 service, UNIT_BEFORE, UNIT_CONFLICTS,
3580 SPECIAL_SHUTDOWN_TARGET, NULL, true);
3589 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3590 set_free(runlevel_services[i]);
3596 static void service_bus_name_owner_change(
3599 const char *old_owner,
3600 const char *new_owner) {
3602 Service *s = SERVICE(u);
3608 assert(streq(s->bus_name, name));
3609 assert(old_owner || new_owner);
3611 if (old_owner && new_owner)
3612 log_debug_unit(u->id,
3613 "%s's D-Bus name %s changed owner from %s to %s",
3614 u->id, name, old_owner, new_owner);
3616 log_debug_unit(u->id,
3617 "%s's D-Bus name %s no longer registered by %s",
3618 u->id, name, old_owner);
3620 log_debug_unit(u->id,
3621 "%s's D-Bus name %s now registered by %s",
3622 u->id, name, new_owner);
3624 s->bus_name_good = !!new_owner;
3626 if (s->type == SERVICE_DBUS) {
3628 /* service_enter_running() will figure out what to
3630 if (s->state == SERVICE_RUNNING)
3631 service_enter_running(s, SERVICE_SUCCESS);
3632 else if (s->state == SERVICE_START && new_owner)
3633 service_enter_start_post(s);
3635 } else if (new_owner &&
3637 (s->state == SERVICE_START ||
3638 s->state == SERVICE_START_POST ||
3639 s->state == SERVICE_RUNNING ||
3640 s->state == SERVICE_RELOAD)) {
3642 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
3645 /* Try to acquire PID from bus service */
3647 r = sd_bus_get_owner(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
3649 r = sd_bus_creds_get_pid(creds, &pid);
3651 log_debug_unit(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3653 service_set_main_pid(s, pid);
3654 unit_watch_pid(UNIT(s), pid);
3659 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3664 /* This is called by the socket code when instantiating a new
3665 * service for a stream socket and the socket needs to be
3668 if (UNIT(s)->load_state != UNIT_LOADED)
3671 if (s->socket_fd >= 0)
3674 if (s->state != SERVICE_DEAD)
3679 unit_ref_set(&s->accept_socket, UNIT(sock));
3681 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3684 static void service_reset_failed(Unit *u) {
3685 Service *s = SERVICE(u);
3689 if (s->state == SERVICE_FAILED)
3690 service_set_state(s, SERVICE_DEAD);
3692 s->result = SERVICE_SUCCESS;
3693 s->reload_result = SERVICE_SUCCESS;
3695 RATELIMIT_RESET(s->start_limit);
3698 static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
3699 Service *s = SERVICE(u);
3701 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
3704 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3705 [SERVICE_DEAD] = "dead",
3706 [SERVICE_START_PRE] = "start-pre",
3707 [SERVICE_START] = "start",
3708 [SERVICE_START_POST] = "start-post",
3709 [SERVICE_RUNNING] = "running",
3710 [SERVICE_EXITED] = "exited",
3711 [SERVICE_RELOAD] = "reload",
3712 [SERVICE_STOP] = "stop",
3713 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3714 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3715 [SERVICE_STOP_POST] = "stop-post",
3716 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3717 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3718 [SERVICE_FAILED] = "failed",
3719 [SERVICE_AUTO_RESTART] = "auto-restart",
3722 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3724 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3725 [SERVICE_RESTART_NO] = "no",
3726 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3727 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3728 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
3729 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3730 [SERVICE_RESTART_ALWAYS] = "always"
3733 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3735 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3736 [SERVICE_SIMPLE] = "simple",
3737 [SERVICE_FORKING] = "forking",
3738 [SERVICE_ONESHOT] = "oneshot",
3739 [SERVICE_DBUS] = "dbus",
3740 [SERVICE_NOTIFY] = "notify",
3741 [SERVICE_IDLE] = "idle"
3744 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3746 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3747 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3748 [SERVICE_EXEC_START] = "ExecStart",
3749 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3750 [SERVICE_EXEC_RELOAD] = "ExecReload",
3751 [SERVICE_EXEC_STOP] = "ExecStop",
3752 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3755 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3757 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3758 [NOTIFY_NONE] = "none",
3759 [NOTIFY_MAIN] = "main",
3760 [NOTIFY_ALL] = "all"
3763 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3765 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3766 [SERVICE_SUCCESS] = "success",
3767 [SERVICE_FAILURE_RESOURCES] = "resources",
3768 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3769 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3770 [SERVICE_FAILURE_SIGNAL] = "signal",
3771 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3772 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3773 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3776 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3778 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3779 [SERVICE_START_LIMIT_NONE] = "none",
3780 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3781 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3782 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3784 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3786 const UnitVTable service_vtable = {
3787 .object_size = sizeof(Service),
3788 .exec_context_offset = offsetof(Service, exec_context),
3789 .cgroup_context_offset = offsetof(Service, cgroup_context),
3790 .kill_context_offset = offsetof(Service, kill_context),
3791 .exec_runtime_offset = offsetof(Service, exec_runtime),
3797 .private_section = "Service",
3799 .init = service_init,
3800 .done = service_done,
3801 .load = service_load,
3803 .coldplug = service_coldplug,
3805 .dump = service_dump,
3807 .start = service_start,
3808 .stop = service_stop,
3809 .reload = service_reload,
3811 .can_reload = service_can_reload,
3813 .kill = service_kill,
3815 .serialize = service_serialize,
3816 .deserialize_item = service_deserialize_item,
3818 .active_state = service_active_state,
3819 .sub_state_to_string = service_sub_state_to_string,
3821 .check_gc = service_check_gc,
3822 .check_snapshot = service_check_snapshot,
3824 .sigchld_event = service_sigchld_event,
3826 .reset_failed = service_reset_failed,
3828 .notify_cgroup_empty = service_notify_cgroup_empty_event,
3829 .notify_message = service_notify_message,
3831 .bus_name_owner_change = service_bus_name_owner_change,
3833 .bus_interface = "org.freedesktop.systemd1.Service",
3834 .bus_vtable = bus_service_vtable,
3835 .bus_changing_properties = bus_service_changing_properties,
3836 .bus_set_property = bus_service_set_property,
3837 .bus_commit_properties = bus_service_commit_properties,
3839 #ifdef HAVE_SYSV_COMPAT
3840 .enumerate = service_enumerate,
3843 .can_transient = true,
3845 .status_message_formats = {
3846 .starting_stopping = {
3847 [0] = "Starting %s...",
3848 [1] = "Stopping %s...",
3850 .finished_start_job = {
3851 [JOB_DONE] = "Started %s.",
3852 [JOB_FAILED] = "Failed to start %s.",
3853 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3854 [JOB_TIMEOUT] = "Timed out starting %s.",
3856 .finished_stop_job = {
3857 [JOB_DONE] = "Stopped %s.",
3858 [JOB_FAILED] = "Stopped (with error) %s.",
3859 [JOB_TIMEOUT] = "Timed out stopping %s.",