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);
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;
305 cgroup_context_done(&s->cgroup_context);
306 exec_context_done(&s->exec_context);
307 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
308 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
309 s->control_command = NULL;
310 s->main_command = NULL;
312 set_free(s->restart_ignore_status.code);
313 s->restart_ignore_status.code = NULL;
314 set_free(s->restart_ignore_status.signal);
315 s->restart_ignore_status.signal = NULL;
317 set_free(s->success_status.code);
318 s->success_status.code = NULL;
319 set_free(s->success_status.signal);
320 s->success_status.signal = NULL;
322 /* This will leak a process, but at least no memory or any of
324 service_unwatch_main_pid(s);
325 service_unwatch_control_pid(s);
326 service_unwatch_pid_file(s);
329 unit_unwatch_bus_name(u, s->bus_name);
334 service_close_socket_fd(s);
335 service_connection_unref(s);
337 unit_ref_unset(&s->accept_socket);
339 service_stop_watchdog(s);
341 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
344 static int service_arm_timer(Service *s, usec_t usec) {
349 if (s->timer_event_source) {
350 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec);
354 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
357 return sd_event_add_monotonic(UNIT(s)->manager->event, now(CLOCK_MONOTONIC) + usec, 0, service_dispatch_timer, s, &s->timer_event_source);
360 #ifdef HAVE_SYSV_COMPAT
361 static char *sysv_translate_name(const char *name) {
364 r = new(char, strlen(name) + sizeof(".service"));
368 if (endswith(name, ".sh"))
369 /* Drop .sh suffix */
370 strcpy(stpcpy(r, name) - 3, ".service");
372 /* Normal init script name */
373 strcpy(stpcpy(r, name), ".service");
378 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
380 /* We silently ignore the $ prefix here. According to the LSB
381 * spec it simply indicates whether something is a
382 * standardized name or a distribution-specific one. Since we
383 * just follow what already exists and do not introduce new
384 * uses or names we don't care who introduced a new name. */
386 static const char * const table[] = {
387 /* LSB defined facilities */
389 "network", SPECIAL_NETWORK_TARGET,
390 "named", SPECIAL_NSS_LOOKUP_TARGET,
391 "portmap", SPECIAL_RPCBIND_TARGET,
392 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
394 "time", SPECIAL_TIME_SYNC_TARGET,
404 n = *name == '$' ? name + 1 : name;
406 for (i = 0; i < ELEMENTSOF(table); i += 2) {
408 if (!streq(table[i], n))
414 r = strdup(table[i+1]);
421 /* If we don't know this name, fallback heuristics to figure
422 * out whether something is a target or a service alias. */
425 if (!unit_prefix_is_valid(n))
428 /* Facilities starting with $ are most likely targets */
429 r = unit_name_build(n, NULL, ".target");
430 } else if (filename && streq(name, filename))
431 /* Names equaling the file name of the services are redundant */
434 /* Everything else we assume to be normal service names */
435 r = sysv_translate_name(n);
446 static int sysv_fix_order(Service *s) {
452 if (s->sysv_start_priority < 0)
455 /* For each pair of services where at least one lacks a LSB
456 * header, we use the start priority value to order things. */
458 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
461 bool special_s, special_t;
468 if (UNIT(t)->load_state != UNIT_LOADED)
471 if (t->sysv_start_priority < 0)
474 /* If both units have modern headers we don't care
475 * about the priorities */
476 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
477 (UNIT(t)->fragment_path || t->sysv_has_lsb))
480 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
481 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
483 if (special_t && !special_s)
485 else if (special_s && !special_t)
487 else if (t->sysv_start_priority < s->sysv_start_priority)
489 else if (t->sysv_start_priority > s->sysv_start_priority)
494 /* FIXME: Maybe we should compare the name here lexicographically? */
496 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
503 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
506 if (!(c = new0(ExecCommand, 1)))
509 if (!(c->path = strdup(path))) {
514 if (!(c->argv = strv_new(path, arg1, NULL))) {
523 static int sysv_exec_commands(Service *s, const bool supports_reload) {
528 assert(UNIT(s)->source_path);
530 c = exec_command_new(UNIT(s)->source_path, "start");
533 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
535 c = exec_command_new(UNIT(s)->source_path, "stop");
538 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
540 if (supports_reload) {
541 c = exec_command_new(UNIT(s)->source_path, "reload");
544 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
550 static bool usage_contains_reload(const char *line) {
551 return (strcasestr(line, "{reload|") ||
552 strcasestr(line, "{reload}") ||
553 strcasestr(line, "{reload\"") ||
554 strcasestr(line, "|reload|") ||
555 strcasestr(line, "|reload}") ||
556 strcasestr(line, "|reload\""));
559 static int service_load_sysv_path(Service *s, const char *path) {
571 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
573 bool supports_reload = false;
580 f = fopen(path, "re");
582 r = errno == ENOENT ? 0 : -errno;
586 if (fstat(fileno(f), &st) < 0) {
591 free(u->source_path);
592 u->source_path = strdup(path);
593 if (!u->source_path) {
597 u->source_mtime = timespec_load(&st.st_mtim);
599 if (null_or_empty(&st)) {
600 u->load_state = UNIT_MASKED;
608 char l[LINE_MAX], *t;
610 if (!fgets(l, sizeof(l), f)) {
615 log_error_unit(u->id,
616 "Failed to read configuration file '%s': %s",
625 /* Try to figure out whether this init script supports
626 * the reload operation. This heuristic looks for
627 * "Usage" lines which include the reload option. */
628 if ( state == USAGE_CONTINUATION ||
629 (state == NORMAL && strcasestr(t, "usage"))) {
630 if (usage_contains_reload(t)) {
631 supports_reload = true;
633 } else if (t[strlen(t)-1] == '\\')
634 state = USAGE_CONTINUATION;
642 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
644 s->sysv_has_lsb = true;
648 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
654 t += strspn(t, WHITESPACE);
656 if (state == NORMAL) {
658 /* Try to parse Red Hat style chkconfig headers */
660 if (startswith_no_case(t, "chkconfig:")) {
662 char runlevels[16], *k;
666 if (sscanf(t+10, "%15s %i %*i",
668 &start_priority) != 2) {
670 log_warning_unit(u->id,
671 "[%s:%u] Failed to parse chkconfig line. Ignoring.",
676 /* A start priority gathered from the
677 * symlink farms is preferred over the
678 * data from the LSB header. */
679 if (start_priority < 0 || start_priority > 99)
680 log_warning_unit(u->id,
681 "[%s:%u] Start priority out of range. Ignoring.",
684 s->sysv_start_priority = start_priority;
686 char_array_0(runlevels);
687 k = delete_chars(runlevels, WHITESPACE "-");
692 if (!(d = strdup(k))) {
697 free(s->sysv_runlevels);
698 s->sysv_runlevels = d;
701 } else if (startswith_no_case(t, "description:")) {
703 size_t k = strlen(t);
707 if (t[k-1] == '\\') {
712 if ((j = strstrip(t+12)) && *j) {
713 if (!(d = strdup(j))) {
720 free(chkconfig_description);
721 chkconfig_description = d;
723 } else if (startswith_no_case(t, "pidfile:")) {
730 if (!path_is_absolute(fn)) {
731 log_warning_unit(u->id,
732 "[%s:%u] PID file not absolute. Ignoring.",
737 if (!(fn = strdup(fn))) {
746 } else if (state == DESCRIPTION) {
748 /* Try to parse Red Hat style description
751 size_t k = strlen(t);
759 if ((j = strstrip(t)) && *j) {
762 if (chkconfig_description)
763 d = strjoin(chkconfig_description, " ", j, NULL);
772 free(chkconfig_description);
773 chkconfig_description = d;
776 } else if (state == LSB || state == LSB_DESCRIPTION) {
778 if (startswith_no_case(t, "Provides:")) {
784 FOREACH_WORD_QUOTED(w, z, t+9, i) {
787 if (!(n = strndup(w, z))) {
792 r = sysv_translate_facility(n, basename(path), &m);
801 if (unit_name_to_type(m) == UNIT_SERVICE)
802 r = unit_merge_by_name(u, m);
809 * indication that the
811 * now available. This
814 * targets do NOT pull
817 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
820 log_error_unit(u->id,
821 "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
822 path, line, m, strerror(-r));
827 } else if (startswith_no_case(t, "Required-Start:") ||
828 startswith_no_case(t, "Should-Start:") ||
829 startswith_no_case(t, "X-Start-Before:") ||
830 startswith_no_case(t, "X-Start-After:")) {
836 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
839 if (!(n = strndup(w, z))) {
844 r = sysv_translate_facility(n, basename(path), &m);
846 log_error_unit(u->id,
847 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
848 path, line, n, strerror(-r));
858 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
861 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
862 path, line, m, strerror(-r));
866 } else if (startswith_no_case(t, "Default-Start:")) {
871 k = delete_chars(t+14, WHITESPACE "-");
874 if (!(d = strdup(k))) {
879 free(s->sysv_runlevels);
880 s->sysv_runlevels = d;
883 } else if (startswith_no_case(t, "Description:")) {
886 state = LSB_DESCRIPTION;
888 if ((j = strstrip(t+12)) && *j) {
889 if (!(d = strdup(j))) {
896 free(long_description);
897 long_description = d;
899 } else if (startswith_no_case(t, "Short-Description:")) {
904 if ((j = strstrip(t+18)) && *j) {
905 if (!(d = strdup(j))) {
912 free(short_description);
913 short_description = d;
915 } else if (state == LSB_DESCRIPTION) {
917 if (startswith(l, "#\t") || startswith(l, "# ")) {
920 if ((j = strstrip(t)) && *j) {
923 if (long_description)
924 d = strjoin(long_description, " ", t, NULL);
933 free(long_description);
934 long_description = d;
943 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
946 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
947 /* If there a runlevels configured for this service
948 * but none of the standard ones, then we assume this
949 * is some special kind of service (which might be
950 * needed for early boot) and don't create any links
953 UNIT(s)->default_dependencies = false;
955 /* Don't timeout special services during boot (like fsck) */
956 s->timeout_start_usec = 0;
957 s->timeout_stop_usec = 0;
959 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
960 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
963 /* Special setting for all SysV services */
964 s->type = SERVICE_FORKING;
965 s->remain_after_exit = !s->pid_file;
966 s->guess_main_pid = false;
967 s->restart = SERVICE_RESTART_NO;
968 s->exec_context.ignore_sigpipe = false;
969 s->kill_context.kill_mode = KILL_PROCESS;
971 /* We use the long description only if
972 * no short description is set. */
974 if (short_description)
975 description = short_description;
976 else if (chkconfig_description)
977 description = chkconfig_description;
978 else if (long_description)
979 description = long_description;
986 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
994 /* The priority that has been set in /etc/rcN.d/ hierarchies
995 * takes precedence over what is stored as default in the LSB
997 if (s->sysv_start_priority_from_rcnd >= 0)
998 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
1000 u->load_state = UNIT_LOADED;
1008 free(short_description);
1009 free(long_description);
1010 free(chkconfig_description);
1015 static int service_load_sysv_name(Service *s, const char *name) {
1021 /* For SysV services we strip the *.sh suffixes. */
1022 if (endswith(name, ".sh.service"))
1025 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1029 path = strjoin(*p, "/", name, NULL);
1033 assert(endswith(path, ".service"));
1034 path[strlen(path)-8] = 0;
1036 r = service_load_sysv_path(s, path);
1038 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1039 /* Try *.sh source'able init scripts */
1040 strcat(path, ".sh");
1041 r = service_load_sysv_path(s, path);
1048 if (UNIT(s)->load_state != UNIT_STUB)
1055 static int service_load_sysv(Service *s) {
1062 /* Load service data from SysV init scripts, preferably with
1063 * LSB headers ... */
1065 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1068 if ((t = UNIT(s)->id))
1069 if ((r = service_load_sysv_name(s, t)) < 0)
1072 if (UNIT(s)->load_state == UNIT_STUB)
1073 SET_FOREACH(t, UNIT(s)->names, i) {
1074 if (t == UNIT(s)->id)
1077 if ((r = service_load_sysv_name(s, t)) < 0)
1080 if (UNIT(s)->load_state != UNIT_STUB)
1088 static int service_verify(Service *s) {
1091 if (UNIT(s)->load_state != UNIT_LOADED)
1094 if (!s->exec_command[SERVICE_EXEC_START]) {
1095 log_error_unit(UNIT(s)->id,
1096 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1100 if (s->type != SERVICE_ONESHOT &&
1101 s->exec_command[SERVICE_EXEC_START]->command_next) {
1102 log_error_unit(UNIT(s)->id,
1103 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1107 if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
1108 log_error_unit(UNIT(s)->id,
1109 "%s has Restart setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1113 if (s->type == SERVICE_DBUS && !s->bus_name) {
1114 log_error_unit(UNIT(s)->id,
1115 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1119 if (s->bus_name && s->type != SERVICE_DBUS)
1120 log_warning_unit(UNIT(s)->id,
1121 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1123 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1124 log_error_unit(UNIT(s)->id,
1125 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1132 static int service_add_default_dependencies(Service *s) {
1137 /* Add a number of automatic dependencies useful for the
1138 * majority of services. */
1140 /* First, pull in base system */
1141 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1142 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1143 SPECIAL_BASIC_TARGET, NULL, true);
1147 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1148 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1149 SPECIAL_SOCKETS_TARGET, NULL, true);
1153 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1154 SPECIAL_TIMERS_TARGET, NULL, true);
1158 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1159 SPECIAL_PATHS_TARGET, NULL, true);
1164 /* Second, activate normal shutdown */
1165 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
1166 SPECIAL_SHUTDOWN_TARGET, NULL, true);
1170 static void service_fix_output(Service *s) {
1173 /* If nothing has been explicitly configured, patch default
1174 * output in. If input is socket/tty we avoid this however,
1175 * since in that case we want output to default to the same
1176 * place as we read input from. */
1178 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1179 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1180 s->exec_context.std_input == EXEC_INPUT_NULL)
1181 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1183 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1184 s->exec_context.std_input == EXEC_INPUT_NULL)
1185 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1188 static int service_load(Unit *u) {
1190 Service *s = SERVICE(u);
1194 /* Load a .service file */
1195 r = unit_load_fragment(u);
1199 #ifdef HAVE_SYSV_COMPAT
1200 /* Load a classic init script as a fallback, if we couldn't find anything */
1201 if (u->load_state == UNIT_STUB) {
1202 r = service_load_sysv(s);
1208 /* Still nothing found? Then let's give up */
1209 if (u->load_state == UNIT_STUB)
1212 /* This is a new unit? Then let's add in some extras */
1213 if (u->load_state == UNIT_LOADED) {
1215 /* We were able to load something, then let's add in
1216 * the dropin directories. */
1217 r = unit_load_dropin(u);
1221 if (s->type == _SERVICE_TYPE_INVALID)
1222 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1224 /* Oneshot services have disabled start timeout by default */
1225 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1226 s->timeout_start_usec = 0;
1228 service_fix_output(s);
1230 r = unit_add_exec_dependencies(u, &s->exec_context);
1234 r = unit_add_default_slice(u);
1238 #ifdef HAVE_SYSV_COMPAT
1239 r = sysv_fix_order(s);
1245 r = unit_watch_bus_name(u, s->bus_name);
1250 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1251 s->notify_access = NOTIFY_MAIN;
1253 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1254 s->notify_access = NOTIFY_MAIN;
1256 if (s->type == SERVICE_DBUS || s->bus_name) {
1257 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true);
1262 if (UNIT(s)->default_dependencies) {
1263 r = service_add_default_dependencies(s);
1268 r = unit_exec_context_defaults(u, &s->exec_context);
1273 return service_verify(s);
1276 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1278 ServiceExecCommand c;
1279 Service *s = SERVICE(u);
1280 const char *prefix2;
1281 _cleanup_free_ char *p2 = NULL;
1285 p2 = strappend(prefix, "\t");
1286 prefix2 = p2 ? p2 : prefix;
1289 "%sService State: %s\n"
1291 "%sReload Result: %s\n"
1292 "%sPermissionsStartOnly: %s\n"
1293 "%sRootDirectoryStartOnly: %s\n"
1294 "%sRemainAfterExit: %s\n"
1295 "%sGuessMainPID: %s\n"
1298 "%sNotifyAccess: %s\n",
1299 prefix, service_state_to_string(s->state),
1300 prefix, service_result_to_string(s->result),
1301 prefix, service_result_to_string(s->reload_result),
1302 prefix, yes_no(s->permissions_start_only),
1303 prefix, yes_no(s->root_directory_start_only),
1304 prefix, yes_no(s->remain_after_exit),
1305 prefix, yes_no(s->guess_main_pid),
1306 prefix, service_type_to_string(s->type),
1307 prefix, service_restart_to_string(s->restart),
1308 prefix, notify_access_to_string(s->notify_access));
1310 if (s->control_pid > 0)
1312 "%sControl PID: %lu\n",
1313 prefix, (unsigned long) s->control_pid);
1315 if (s->main_pid > 0)
1318 "%sMain PID Known: %s\n"
1319 "%sMain PID Alien: %s\n",
1320 prefix, (unsigned long) s->main_pid,
1321 prefix, yes_no(s->main_pid_known),
1322 prefix, yes_no(s->main_pid_alien));
1327 prefix, s->pid_file);
1332 "%sBus Name Good: %s\n",
1333 prefix, s->bus_name,
1334 prefix, yes_no(s->bus_name_good));
1336 kill_context_dump(&s->kill_context, f, prefix);
1337 exec_context_dump(&s->exec_context, f, prefix);
1339 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1341 if (!s->exec_command[c])
1344 fprintf(f, "%s-> %s:\n",
1345 prefix, service_exec_command_to_string(c));
1347 exec_command_dump_list(s->exec_command[c], f, prefix2);
1350 #ifdef HAVE_SYSV_COMPAT
1353 "%sSysV Init Script has LSB Header: %s\n"
1354 "%sSysVEnabled: %s\n",
1355 prefix, yes_no(s->sysv_has_lsb),
1356 prefix, yes_no(s->sysv_enabled));
1358 if (s->sysv_start_priority >= 0)
1360 "%sSysVStartPriority: %i\n",
1361 prefix, s->sysv_start_priority);
1363 if (s->sysv_runlevels)
1364 fprintf(f, "%sSysVRunLevels: %s\n",
1365 prefix, s->sysv_runlevels);
1369 fprintf(f, "%sStatus Text: %s\n",
1370 prefix, s->status_text);
1373 static int service_load_pid_file(Service *s, bool may_warn) {
1374 _cleanup_free_ char *k = NULL;
1383 r = read_one_line_file(s->pid_file, &k);
1386 log_info_unit(UNIT(s)->id,
1387 "PID file %s not readable (yet?) after %s.",
1388 s->pid_file, service_state_to_string(s->state));
1392 r = parse_pid(k, &pid);
1395 log_info_unit(UNIT(s)->id,
1396 "Failed to read PID from file %s: %s",
1397 s->pid_file, strerror(-r));
1401 if (kill(pid, 0) < 0 && errno != EPERM) {
1403 log_info_unit(UNIT(s)->id,
1404 "PID %lu read from file %s does not exist.",
1405 (unsigned long) pid, s->pid_file);
1409 if (s->main_pid_known) {
1410 if (pid == s->main_pid)
1413 log_debug_unit(UNIT(s)->id,
1414 "Main PID changing: %lu -> %lu",
1415 (unsigned long) s->main_pid, (unsigned long) pid);
1416 service_unwatch_main_pid(s);
1417 s->main_pid_known = false;
1419 log_debug_unit(UNIT(s)->id,
1420 "Main PID loaded: %lu", (unsigned long) pid);
1422 r = service_set_main_pid(s, pid);
1426 r = unit_watch_pid(UNIT(s), pid);
1428 /* FIXME: we need to do something here */
1429 log_warning_unit(UNIT(s)->id,
1430 "Failed to watch PID %lu from service %s",
1431 (unsigned long) pid, UNIT(s)->id);
1438 static int service_search_main_pid(Service *s) {
1444 /* If we know it anyway, don't ever fallback to unreliable
1446 if (s->main_pid_known)
1449 if (!s->guess_main_pid)
1452 assert(s->main_pid <= 0);
1454 pid = unit_search_main_pid(UNIT(s));
1458 log_debug_unit(UNIT(s)->id,
1459 "Main PID guessed: %lu", (unsigned long) pid);
1460 r = service_set_main_pid(s, pid);
1464 r = unit_watch_pid(UNIT(s), pid);
1466 /* FIXME: we need to do something here */
1467 log_warning_unit(UNIT(s)->id,
1468 "Failed to watch PID %lu from service %s",
1469 (unsigned long) pid, UNIT(s)->id);
1475 static void service_set_state(Service *s, ServiceState state) {
1476 ServiceState old_state;
1477 const UnitActiveState *table;
1481 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1483 old_state = s->state;
1486 service_unwatch_pid_file(s);
1489 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1491 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1493 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
1494 SERVICE_AUTO_RESTART))
1495 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1498 SERVICE_START, SERVICE_START_POST,
1499 SERVICE_RUNNING, SERVICE_RELOAD,
1500 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1502 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
1503 service_unwatch_main_pid(s);
1504 s->main_command = NULL;
1508 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1510 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1512 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
1513 service_unwatch_control_pid(s);
1514 s->control_command = NULL;
1515 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1519 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1520 SERVICE_RUNNING, SERVICE_RELOAD,
1521 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
1522 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) &&
1523 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1524 service_close_socket_fd(s);
1525 service_connection_unref(s);
1528 if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
1529 service_stop_watchdog(s);
1531 /* For the inactive states unit_notify() will trim the cgroup,
1532 * but for exit we have to do that ourselves... */
1533 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1534 unit_destroy_cgroup(UNIT(s));
1536 /* For remain_after_exit services, let's see if we can "release" the
1537 * hold on the console, since unit_notify() only does that in case of
1538 * change of state */
1539 if (state == SERVICE_EXITED && s->remain_after_exit &&
1540 UNIT(s)->manager->n_on_console > 0) {
1541 ExecContext *ec = unit_get_exec_context(UNIT(s));
1542 if (ec && exec_context_may_touch_console(ec)) {
1543 Manager *m = UNIT(s)->manager;
1546 if (m->n_on_console == 0)
1547 /* unset no_console_output flag, since the console is free */
1548 m->no_console_output = false;
1552 if (old_state != state)
1553 log_debug_unit(UNIT(s)->id, "%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
1555 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1556 s->reload_result = SERVICE_SUCCESS;
1559 static int service_coldplug(Unit *u) {
1560 Service *s = SERVICE(u);
1564 assert(s->state == SERVICE_DEAD);
1566 if (s->deserialized_state != s->state) {
1568 if (IN_SET(s->deserialized_state,
1569 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1571 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1573 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
1577 k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec;
1579 /* For the start/stop timeouts 0 means off */
1581 r = service_arm_timer(s, k);
1587 if (s->deserialized_state == SERVICE_AUTO_RESTART) {
1589 /* The restart timeouts 0 means immediately */
1590 r = service_arm_timer(s, s->restart_usec);
1595 if (pid_valid(s->main_pid) &&
1596 ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) ||
1597 IN_SET(s->deserialized_state,
1598 SERVICE_START, SERVICE_START_POST,
1599 SERVICE_RUNNING, SERVICE_RELOAD,
1600 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1602 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) {
1603 r = unit_watch_pid(UNIT(s), s->main_pid);
1608 if (pid_valid(s->control_pid) &&
1609 IN_SET(s->deserialized_state,
1610 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1612 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
1614 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
1615 r = unit_watch_pid(UNIT(s), s->control_pid);
1620 if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
1621 service_start_watchdog(s);
1623 service_set_state(s, s->deserialized_state);
1629 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1633 unsigned rn_fds = 0;
1640 if (s->socket_fd >= 0)
1643 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1648 if (u->type != UNIT_SOCKET)
1653 r = socket_collect_fds(sock, &cfds, &cn_fds);
1666 t = new(int, rn_fds+cn_fds);
1673 memcpy(t, rfds, rn_fds * sizeof(int));
1674 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1679 rn_fds = rn_fds+cn_fds;
1694 static int service_spawn(
1699 bool apply_permissions,
1701 bool apply_tty_stdin,
1702 bool set_notify_socket,
1709 _cleanup_free_ int *fdsbuf = NULL;
1710 unsigned n_fds = 0, n_env = 0;
1711 _cleanup_strv_free_ char
1712 **argv = NULL, **final_env = NULL, **our_env = NULL;
1719 unit_realize_cgroup(UNIT(s));
1721 r = unit_setup_exec_runtime(UNIT(s));
1726 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1727 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1728 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1730 if (s->socket_fd >= 0) {
1731 fds = &s->socket_fd;
1734 r = service_collect_fds(s, &fdsbuf, &n_fds);
1742 if (timeout && s->timeout_start_usec > 0) {
1743 r = service_arm_timer(s, s->timeout_start_usec);
1747 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1749 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
1753 our_env = new0(char*, 4);
1759 if (set_notify_socket)
1760 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1765 if (s->main_pid > 0)
1766 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1771 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
1772 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1777 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1783 if (is_control && UNIT(s)->cgroup_path) {
1784 path = strappenda(UNIT(s)->cgroup_path, "/control");
1785 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1787 path = UNIT(s)->cgroup_path;
1797 UNIT(s)->manager->confirm_spawn,
1798 UNIT(s)->manager->cgroup_supported,
1802 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1808 r = unit_watch_pid(UNIT(s), pid);
1810 /* FIXME: we need to do something here */
1819 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1824 static int main_pid_good(Service *s) {
1827 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1830 /* If we know the pid file, then lets just check if it is
1832 if (s->main_pid_known) {
1834 /* If it's an alien child let's check if it is still
1836 if (s->main_pid_alien && s->main_pid > 0)
1837 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1839 /* .. otherwise assume we'll get a SIGCHLD for it,
1840 * which we really should wait for to collect exit
1841 * status and code */
1842 return s->main_pid > 0;
1845 /* We don't know the pid */
1849 _pure_ static int control_pid_good(Service *s) {
1852 return s->control_pid > 0;
1855 static int cgroup_good(Service *s) {
1860 if (!UNIT(s)->cgroup_path)
1863 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1870 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1874 if (f != SERVICE_SUCCESS)
1877 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1879 if (allow_restart &&
1880 !s->forbid_restart &&
1881 (s->restart == SERVICE_RESTART_ALWAYS ||
1882 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1883 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1884 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1885 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1886 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1887 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1888 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1889 (s->result != SERVICE_FAILURE_SIGNAL ||
1890 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))) {
1892 r = service_arm_timer(s, s->restart_usec);
1896 service_set_state(s, SERVICE_AUTO_RESTART);
1899 s->forbid_restart = false;
1901 /* we want fresh tmpdirs in case service is started again immediately */
1902 exec_runtime_destroy(s->exec_runtime);
1903 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
1905 /* Try to delete the pid file. At this point it will be
1906 * out-of-date, and some software might be confused by it, so
1907 * let's remove it. */
1909 unlink_noerrno(s->pid_file);
1914 log_warning_unit(UNIT(s)->id,
1915 "%s failed to run install restart timer: %s",
1916 UNIT(s)->id, strerror(-r));
1917 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1920 static void service_enter_stop_post(Service *s, ServiceResult f) {
1924 if (f != SERVICE_SUCCESS)
1927 service_unwatch_control_pid(s);
1929 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1930 if (s->control_command) {
1931 s->control_command_id = SERVICE_EXEC_STOP_POST;
1933 r = service_spawn(s,
1937 !s->permissions_start_only,
1938 !s->root_directory_start_only,
1947 service_set_state(s, SERVICE_STOP_POST);
1949 service_enter_dead(s, SERVICE_SUCCESS, true);
1954 log_warning_unit(UNIT(s)->id,
1955 "%s failed to run 'stop-post' task: %s",
1956 UNIT(s)->id, strerror(-r));
1957 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1960 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1965 if (f != SERVICE_SUCCESS)
1968 r = unit_kill_context(
1971 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1979 if (s->timeout_stop_usec > 0) {
1980 r = service_arm_timer(s, s->timeout_stop_usec);
1985 service_set_state(s, state);
1986 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1987 service_enter_stop_post(s, SERVICE_SUCCESS);
1989 service_enter_dead(s, SERVICE_SUCCESS, true);
1994 log_warning_unit(UNIT(s)->id,
1995 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1997 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1998 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2000 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2003 static void service_enter_stop(Service *s, ServiceResult f) {
2008 if (f != SERVICE_SUCCESS)
2011 service_unwatch_control_pid(s);
2013 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
2014 if (s->control_command) {
2015 s->control_command_id = SERVICE_EXEC_STOP;
2017 r = service_spawn(s,
2021 !s->permissions_start_only,
2022 !s->root_directory_start_only,
2030 service_set_state(s, SERVICE_STOP);
2032 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2037 log_warning_unit(UNIT(s)->id,
2038 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2039 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2042 static void service_enter_running(Service *s, ServiceResult f) {
2043 int main_pid_ok, cgroup_ok;
2046 if (f != SERVICE_SUCCESS)
2049 main_pid_ok = main_pid_good(s);
2050 cgroup_ok = cgroup_good(s);
2052 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2053 (s->bus_name_good || s->type != SERVICE_DBUS))
2054 service_set_state(s, SERVICE_RUNNING);
2055 else if (s->remain_after_exit)
2056 service_set_state(s, SERVICE_EXITED);
2058 service_enter_stop(s, SERVICE_SUCCESS);
2061 static void service_enter_start_post(Service *s) {
2065 service_unwatch_control_pid(s);
2066 service_reset_watchdog(s);
2068 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
2069 if (s->control_command) {
2070 s->control_command_id = SERVICE_EXEC_START_POST;
2072 r = service_spawn(s,
2076 !s->permissions_start_only,
2077 !s->root_directory_start_only,
2085 service_set_state(s, SERVICE_START_POST);
2087 service_enter_running(s, SERVICE_SUCCESS);
2092 log_warning_unit(UNIT(s)->id,
2093 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2094 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2097 static void service_kill_control_processes(Service *s) {
2100 if (!UNIT(s)->cgroup_path)
2103 p = strappenda(UNIT(s)->cgroup_path, "/control");
2104 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
2107 static void service_enter_start(Service *s) {
2114 assert(s->exec_command[SERVICE_EXEC_START]);
2115 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2117 service_unwatch_control_pid(s);
2118 service_unwatch_main_pid(s);
2120 /* We want to ensure that nobody leaks processes from
2121 * START_PRE here, so let's go on a killing spree, People
2122 * should not spawn long running processes from START_PRE. */
2123 service_kill_control_processes(s);
2125 if (s->type == SERVICE_FORKING) {
2126 s->control_command_id = SERVICE_EXEC_START;
2127 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2129 s->main_command = NULL;
2131 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2132 s->control_command = NULL;
2134 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2137 r = service_spawn(s,
2139 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
2140 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2145 s->notify_access != NOTIFY_NONE,
2151 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2152 /* For simple services we immediately start
2153 * the START_POST binaries. */
2155 service_set_main_pid(s, pid);
2156 service_enter_start_post(s);
2158 } else if (s->type == SERVICE_FORKING) {
2160 /* For forking services we wait until the start
2161 * process exited. */
2163 s->control_pid = pid;
2164 service_set_state(s, SERVICE_START);
2166 } else if (s->type == SERVICE_ONESHOT ||
2167 s->type == SERVICE_DBUS ||
2168 s->type == SERVICE_NOTIFY) {
2170 /* For oneshot services we wait until the start
2171 * process exited, too, but it is our main process. */
2173 /* For D-Bus services we know the main pid right away,
2174 * but wait for the bus name to appear on the
2175 * bus. Notify services are similar. */
2177 service_set_main_pid(s, pid);
2178 service_set_state(s, SERVICE_START);
2180 assert_not_reached("Unknown service type");
2185 log_warning_unit(UNIT(s)->id,
2186 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2187 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2190 static void service_enter_start_pre(Service *s) {
2195 service_unwatch_control_pid(s);
2197 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
2198 if (s->control_command) {
2199 /* Before we start anything, let's clear up what might
2200 * be left from previous runs. */
2201 service_kill_control_processes(s);
2203 s->control_command_id = SERVICE_EXEC_START_PRE;
2205 r = service_spawn(s,
2209 !s->permissions_start_only,
2210 !s->root_directory_start_only,
2218 service_set_state(s, SERVICE_START_PRE);
2220 service_enter_start(s);
2225 log_warning_unit(UNIT(s)->id,
2226 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2227 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2230 static void service_enter_restart(Service *s) {
2231 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2236 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2237 /* Don't restart things if we are going down anyway */
2238 log_info_unit(UNIT(s)->id,
2239 "Stop job pending for unit, delaying automatic restart.");
2241 r = service_arm_timer(s, s->restart_usec);
2248 /* Any units that are bound to this service must also be
2249 * restarted. We use JOB_RESTART (instead of the more obvious
2250 * JOB_START) here so that those dependency jobs will be added
2252 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2256 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2257 * it will be canceled as part of the service_stop() call that
2258 * is executed as part of JOB_RESTART. */
2260 log_debug_unit(UNIT(s)->id,
2261 "%s scheduled restart job.", UNIT(s)->id);
2265 log_warning_unit(UNIT(s)->id,
2266 "%s failed to schedule restart job: %s",
2267 UNIT(s)->id, bus_error_message(&error, -r));
2268 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2271 static void service_enter_reload(Service *s) {
2276 service_unwatch_control_pid(s);
2278 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
2279 if (s->control_command) {
2280 s->control_command_id = SERVICE_EXEC_RELOAD;
2282 r = service_spawn(s,
2286 !s->permissions_start_only,
2287 !s->root_directory_start_only,
2295 service_set_state(s, SERVICE_RELOAD);
2297 service_enter_running(s, SERVICE_SUCCESS);
2302 log_warning_unit(UNIT(s)->id,
2303 "%s failed to run 'reload' task: %s",
2304 UNIT(s)->id, strerror(-r));
2305 s->reload_result = SERVICE_FAILURE_RESOURCES;
2306 service_enter_running(s, SERVICE_SUCCESS);
2309 static void service_run_next_control(Service *s) {
2313 assert(s->control_command);
2314 assert(s->control_command->command_next);
2316 assert(s->control_command_id != SERVICE_EXEC_START);
2318 s->control_command = s->control_command->command_next;
2319 service_unwatch_control_pid(s);
2321 r = service_spawn(s,
2325 !s->permissions_start_only,
2326 !s->root_directory_start_only,
2327 s->control_command_id == SERVICE_EXEC_START_PRE ||
2328 s->control_command_id == SERVICE_EXEC_STOP_POST,
2338 log_warning_unit(UNIT(s)->id,
2339 "%s failed to run next control task: %s",
2340 UNIT(s)->id, strerror(-r));
2342 if (s->state == SERVICE_START_PRE)
2343 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2344 else if (s->state == SERVICE_STOP)
2345 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2346 else if (s->state == SERVICE_STOP_POST)
2347 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2348 else if (s->state == SERVICE_RELOAD) {
2349 s->reload_result = SERVICE_FAILURE_RESOURCES;
2350 service_enter_running(s, SERVICE_SUCCESS);
2352 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2355 static void service_run_next_main(Service *s) {
2360 assert(s->main_command);
2361 assert(s->main_command->command_next);
2362 assert(s->type == SERVICE_ONESHOT);
2364 s->main_command = s->main_command->command_next;
2365 service_unwatch_main_pid(s);
2367 r = service_spawn(s,
2374 s->notify_access != NOTIFY_NONE,
2380 service_set_main_pid(s, pid);
2385 log_warning_unit(UNIT(s)->id,
2386 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2387 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2390 static int service_start_limit_test(Service *s) {
2393 if (ratelimit_test(&s->start_limit))
2396 switch (s->start_limit_action) {
2398 case SERVICE_START_LIMIT_NONE:
2399 log_warning_unit(UNIT(s)->id,
2400 "%s start request repeated too quickly, refusing to start.",
2404 case SERVICE_START_LIMIT_REBOOT: {
2405 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2408 log_warning_unit(UNIT(s)->id,
2409 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2411 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
2412 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
2413 true, &error, NULL);
2415 log_error_unit(UNIT(s)->id,
2416 "Failed to reboot: %s.", bus_error_message(&error, r));
2421 case SERVICE_START_LIMIT_REBOOT_FORCE:
2422 log_warning_unit(UNIT(s)->id,
2423 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2424 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2427 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2428 log_warning_unit(UNIT(s)->id,
2429 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2431 reboot(RB_AUTOBOOT);
2435 log_error_unit(UNIT(s)->id,
2436 "start limit action=%i", s->start_limit_action);
2437 assert_not_reached("Unknown StartLimitAction.");
2443 static int service_start(Unit *u) {
2444 Service *s = SERVICE(u);
2449 /* We cannot fulfill this request right now, try again later
2451 if (s->state == SERVICE_STOP ||
2452 s->state == SERVICE_STOP_SIGTERM ||
2453 s->state == SERVICE_STOP_SIGKILL ||
2454 s->state == SERVICE_STOP_POST ||
2455 s->state == SERVICE_FINAL_SIGTERM ||
2456 s->state == SERVICE_FINAL_SIGKILL)
2459 /* Already on it! */
2460 if (s->state == SERVICE_START_PRE ||
2461 s->state == SERVICE_START ||
2462 s->state == SERVICE_START_POST)
2465 /* A service that will be restarted must be stopped first to
2466 * trigger BindsTo and/or OnFailure dependencies. If a user
2467 * does not want to wait for the holdoff time to elapse, the
2468 * service should be manually restarted, not started. We
2469 * simply return EAGAIN here, so that any start jobs stay
2470 * queued, and assume that the auto restart timer will
2471 * eventually trigger the restart. */
2472 if (s->state == SERVICE_AUTO_RESTART)
2475 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2477 /* Make sure we don't enter a busy loop of some kind. */
2478 r = service_start_limit_test(s);
2480 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2484 s->result = SERVICE_SUCCESS;
2485 s->reload_result = SERVICE_SUCCESS;
2486 s->main_pid_known = false;
2487 s->main_pid_alien = false;
2488 s->forbid_restart = false;
2490 service_enter_start_pre(s);
2494 static int service_stop(Unit *u) {
2495 Service *s = SERVICE(u);
2499 /* Don't create restart jobs from here. */
2500 s->forbid_restart = true;
2503 if (s->state == SERVICE_STOP ||
2504 s->state == SERVICE_STOP_SIGTERM ||
2505 s->state == SERVICE_STOP_SIGKILL ||
2506 s->state == SERVICE_STOP_POST ||
2507 s->state == SERVICE_FINAL_SIGTERM ||
2508 s->state == SERVICE_FINAL_SIGKILL)
2511 /* A restart will be scheduled or is in progress. */
2512 if (s->state == SERVICE_AUTO_RESTART) {
2513 service_set_state(s, SERVICE_DEAD);
2517 /* If there's already something running we go directly into
2519 if (s->state == SERVICE_START_PRE ||
2520 s->state == SERVICE_START ||
2521 s->state == SERVICE_START_POST ||
2522 s->state == SERVICE_RELOAD) {
2523 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2527 assert(s->state == SERVICE_RUNNING ||
2528 s->state == SERVICE_EXITED);
2530 service_enter_stop(s, SERVICE_SUCCESS);
2534 static int service_reload(Unit *u) {
2535 Service *s = SERVICE(u);
2539 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2541 service_enter_reload(s);
2545 _pure_ static bool service_can_reload(Unit *u) {
2546 Service *s = SERVICE(u);
2550 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2553 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2554 Service *s = SERVICE(u);
2560 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2561 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2562 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2564 if (s->control_pid > 0)
2565 unit_serialize_item_format(u, f, "control-pid", "%lu",
2566 (unsigned long) s->control_pid);
2568 if (s->main_pid_known && s->main_pid > 0)
2569 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2571 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2574 unit_serialize_item(u, f, "status-text", s->status_text);
2576 /* FIXME: There's a minor uncleanliness here: if there are
2577 * multiple commands attached here, we will start from the
2578 * first one again */
2579 if (s->control_command_id >= 0)
2580 unit_serialize_item(u, f, "control-command",
2581 service_exec_command_to_string(s->control_command_id));
2583 if (s->socket_fd >= 0) {
2586 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2589 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2592 if (s->main_exec_status.pid > 0) {
2593 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu",
2594 (unsigned long) s->main_exec_status.pid);
2595 dual_timestamp_serialize(f, "main-exec-status-start",
2596 &s->main_exec_status.start_timestamp);
2597 dual_timestamp_serialize(f, "main-exec-status-exit",
2598 &s->main_exec_status.exit_timestamp);
2600 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2601 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
2602 s->main_exec_status.code);
2603 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
2604 s->main_exec_status.status);
2607 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2608 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2610 if (s->forbid_restart)
2611 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
2616 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2617 Service *s = SERVICE(u);
2624 if (streq(key, "state")) {
2627 state = service_state_from_string(value);
2629 log_debug_unit(u->id, "Failed to parse state value %s", value);
2631 s->deserialized_state = state;
2632 } else if (streq(key, "result")) {
2635 f = service_result_from_string(value);
2637 log_debug_unit(u->id, "Failed to parse result value %s", value);
2638 else if (f != SERVICE_SUCCESS)
2641 } else if (streq(key, "reload-result")) {
2644 f = service_result_from_string(value);
2646 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2647 else if (f != SERVICE_SUCCESS)
2648 s->reload_result = f;
2650 } else if (streq(key, "control-pid")) {
2653 if (parse_pid(value, &pid) < 0)
2654 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2656 s->control_pid = pid;
2657 } else if (streq(key, "main-pid")) {
2660 if (parse_pid(value, &pid) < 0)
2661 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2663 service_set_main_pid(s, pid);
2664 unit_watch_pid(UNIT(s), pid);
2666 } else if (streq(key, "main-pid-known")) {
2669 b = parse_boolean(value);
2671 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2673 s->main_pid_known = b;
2674 } else if (streq(key, "status-text")) {
2681 free(s->status_text);
2685 } else if (streq(key, "control-command")) {
2686 ServiceExecCommand id;
2688 id = service_exec_command_from_string(value);
2690 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2692 s->control_command_id = id;
2693 s->control_command = s->exec_command[id];
2695 } else if (streq(key, "socket-fd")) {
2698 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2699 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2702 if (s->socket_fd >= 0)
2703 close_nointr_nofail(s->socket_fd);
2704 s->socket_fd = fdset_remove(fds, fd);
2706 } else if (streq(key, "main-exec-status-pid")) {
2709 if (parse_pid(value, &pid) < 0)
2710 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2712 s->main_exec_status.pid = pid;
2713 } else if (streq(key, "main-exec-status-code")) {
2716 if (safe_atoi(value, &i) < 0)
2717 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2719 s->main_exec_status.code = i;
2720 } else if (streq(key, "main-exec-status-status")) {
2723 if (safe_atoi(value, &i) < 0)
2724 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2726 s->main_exec_status.status = i;
2727 } else if (streq(key, "main-exec-status-start"))
2728 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2729 else if (streq(key, "main-exec-status-exit"))
2730 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2731 else if (streq(key, "watchdog-timestamp"))
2732 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2733 else if (streq(key, "forbid-restart")) {
2736 b = parse_boolean(value);
2738 log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value);
2740 s->forbid_restart = b;
2742 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2747 _pure_ static UnitActiveState service_active_state(Unit *u) {
2748 const UnitActiveState *table;
2752 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2754 return table[SERVICE(u)->state];
2757 static const char *service_sub_state_to_string(Unit *u) {
2760 return service_state_to_string(SERVICE(u)->state);
2763 static bool service_check_gc(Unit *u) {
2764 Service *s = SERVICE(u);
2768 /* Never clean up services that still have a process around,
2769 * even if the service is formally dead. */
2770 if (cgroup_good(s) > 0 ||
2771 main_pid_good(s) > 0 ||
2772 control_pid_good(s) > 0)
2775 #ifdef HAVE_SYSV_COMPAT
2783 _pure_ static bool service_check_snapshot(Unit *u) {
2784 Service *s = SERVICE(u);
2788 return (s->socket_fd < 0);
2791 static int service_retry_pid_file(Service *s) {
2794 assert(s->pid_file);
2795 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2797 r = service_load_pid_file(s, false);
2801 service_unwatch_pid_file(s);
2803 service_enter_running(s, SERVICE_SUCCESS);
2807 static int service_watch_pid_file(Service *s) {
2810 log_debug_unit(UNIT(s)->id,
2811 "Setting watch for %s's PID file %s",
2812 UNIT(s)->id, s->pid_file_pathspec->path);
2813 r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
2817 /* the pidfile might have appeared just before we set the watch */
2818 log_debug_unit(UNIT(s)->id,
2819 "Trying to read %s's PID file %s in case it changed",
2820 UNIT(s)->id, s->pid_file_pathspec->path);
2821 service_retry_pid_file(s);
2825 log_error_unit(UNIT(s)->id,
2826 "Failed to set a watch for %s's PID file %s: %s",
2827 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2828 service_unwatch_pid_file(s);
2832 static int service_demand_pid_file(Service *s) {
2835 assert(s->pid_file);
2836 assert(!s->pid_file_pathspec);
2838 ps = new0(PathSpec, 1);
2843 ps->path = strdup(s->pid_file);
2849 path_kill_slashes(ps->path);
2851 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2852 * keep their PID file open all the time. */
2853 ps->type = PATH_MODIFIED;
2854 ps->inotify_fd = -1;
2856 s->pid_file_pathspec = ps;
2858 return service_watch_pid_file(s);
2861 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
2862 PathSpec *p = userdata;
2867 s = SERVICE(p->unit);
2871 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2872 assert(s->pid_file_pathspec);
2873 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2875 log_debug_unit(UNIT(s)->id, "inotify event for %s", UNIT(s)->id);
2877 if (path_spec_fd_event(p, events) < 0)
2880 if (service_retry_pid_file(s) == 0)
2883 if (service_watch_pid_file(s) < 0)
2889 service_unwatch_pid_file(s);
2890 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2894 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2895 Service *s = SERVICE(u);
2901 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2902 is_clean_exit_lsb(code, status, &s->success_status))
2903 f = SERVICE_SUCCESS;
2904 else if (code == CLD_EXITED)
2905 f = SERVICE_FAILURE_EXIT_CODE;
2906 else if (code == CLD_KILLED)
2907 f = SERVICE_FAILURE_SIGNAL;
2908 else if (code == CLD_DUMPED)
2909 f = SERVICE_FAILURE_CORE_DUMP;
2911 assert_not_reached("Unknown code");
2913 if (s->main_pid == pid) {
2914 /* Forking services may occasionally move to a new PID.
2915 * As long as they update the PID file before exiting the old
2916 * PID, they're fine. */
2917 if (service_load_pid_file(s, false) == 0)
2921 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2923 if (s->main_command) {
2924 /* If this is not a forking service than the
2925 * main process got started and hence we copy
2926 * the exit status so that it is recorded both
2927 * as main and as control process exit
2930 s->main_command->exec_status = s->main_exec_status;
2932 if (s->main_command->ignore)
2933 f = SERVICE_SUCCESS;
2934 } else if (s->exec_command[SERVICE_EXEC_START]) {
2936 /* If this is a forked process, then we should
2937 * ignore the return value if this was
2938 * configured for the starter process */
2940 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2941 f = SERVICE_SUCCESS;
2944 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2946 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2947 u->id, sigchld_code_to_string(code), status,
2948 strna(code == CLD_EXITED
2949 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2950 : signal_to_string(status)),
2951 "EXIT_CODE=%s", sigchld_code_to_string(code),
2952 "EXIT_STATUS=%i", status,
2955 if (f != SERVICE_SUCCESS)
2958 if (s->main_command &&
2959 s->main_command->command_next &&
2960 f == SERVICE_SUCCESS) {
2962 /* There is another command to *
2963 * execute, so let's do that. */
2965 log_debug_unit(u->id,
2966 "%s running next main command for state %s",
2967 u->id, service_state_to_string(s->state));
2968 service_run_next_main(s);
2972 /* The service exited, so the service is officially
2974 s->main_command = NULL;
2978 case SERVICE_START_POST:
2979 case SERVICE_RELOAD:
2981 /* Need to wait until the operation is
2986 if (s->type == SERVICE_ONESHOT) {
2987 /* This was our main goal, so let's go on */
2988 if (f == SERVICE_SUCCESS)
2989 service_enter_start_post(s);
2991 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2997 case SERVICE_RUNNING:
2998 service_enter_running(s, f);
3001 case SERVICE_STOP_SIGTERM:
3002 case SERVICE_STOP_SIGKILL:
3004 if (!control_pid_good(s))
3005 service_enter_stop_post(s, f);
3007 /* If there is still a control process, wait for that first */
3010 case SERVICE_STOP_POST:
3011 case SERVICE_FINAL_SIGTERM:
3012 case SERVICE_FINAL_SIGKILL:
3014 if (!control_pid_good(s))
3015 service_enter_dead(s, f, true);
3019 assert_not_reached("Uh, main process died at wrong time.");
3023 } else if (s->control_pid == pid) {
3026 if (s->control_command) {
3027 exec_status_exit(&s->control_command->exec_status,
3028 &s->exec_context, pid, code, status);
3030 if (s->control_command->ignore)
3031 f = SERVICE_SUCCESS;
3034 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
3035 "%s: control process exited, code=%s status=%i",
3036 u->id, sigchld_code_to_string(code), status);
3038 if (f != SERVICE_SUCCESS)
3041 /* Immediately get rid of the cgroup, so that the
3042 * kernel doesn't delay the cgroup empty messages for
3043 * the service cgroup any longer than necessary */
3044 service_kill_control_processes(s);
3046 if (s->control_command &&
3047 s->control_command->command_next &&
3048 f == SERVICE_SUCCESS) {
3050 /* There is another command to *
3051 * execute, so let's do that. */
3053 log_debug_unit(u->id,
3054 "%s running next control command for state %s",
3055 u->id, service_state_to_string(s->state));
3056 service_run_next_control(s);
3059 /* No further commands for this step, so let's
3060 * figure out what to do next */
3062 s->control_command = NULL;
3063 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3065 log_debug_unit(u->id,
3066 "%s got final SIGCHLD for state %s",
3067 u->id, service_state_to_string(s->state));
3071 case SERVICE_START_PRE:
3072 if (f == SERVICE_SUCCESS)
3073 service_enter_start(s);
3075 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3079 if (s->type != SERVICE_FORKING)
3080 /* Maybe spurious event due to a reload that changed the type? */
3083 if (f != SERVICE_SUCCESS) {
3084 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3089 bool has_start_post;
3092 /* Let's try to load the pid file here if we can.
3093 * The PID file might actually be created by a START_POST
3094 * script. In that case don't worry if the loading fails. */
3096 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3097 r = service_load_pid_file(s, !has_start_post);
3098 if (!has_start_post && r < 0) {
3099 r = service_demand_pid_file(s);
3100 if (r < 0 || !cgroup_good(s))
3101 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3105 service_search_main_pid(s);
3107 service_enter_start_post(s);
3110 case SERVICE_START_POST:
3111 if (f != SERVICE_SUCCESS) {
3112 service_enter_stop(s, f);
3119 r = service_load_pid_file(s, true);
3121 r = service_demand_pid_file(s);
3122 if (r < 0 || !cgroup_good(s))
3123 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3127 service_search_main_pid(s);
3129 service_enter_running(s, SERVICE_SUCCESS);
3132 case SERVICE_RELOAD:
3133 if (f == SERVICE_SUCCESS) {
3134 service_load_pid_file(s, true);
3135 service_search_main_pid(s);
3138 s->reload_result = f;
3139 service_enter_running(s, SERVICE_SUCCESS);
3143 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3146 case SERVICE_STOP_SIGTERM:
3147 case SERVICE_STOP_SIGKILL:
3148 if (main_pid_good(s) <= 0)
3149 service_enter_stop_post(s, f);
3151 /* If there is still a service
3152 * process around, wait until
3153 * that one quit, too */
3156 case SERVICE_STOP_POST:
3157 case SERVICE_FINAL_SIGTERM:
3158 case SERVICE_FINAL_SIGKILL:
3159 if (main_pid_good(s) <= 0)
3160 service_enter_dead(s, f, true);
3164 assert_not_reached("Uh, control process died at wrong time.");
3169 /* Notify clients about changed exit status */
3170 unit_add_to_dbus_queue(u);
3173 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
3174 Service *s = SERVICE(userdata);
3177 assert(source == s->timer_event_source);
3181 case SERVICE_START_PRE:
3183 log_warning_unit(UNIT(s)->id,
3184 "%s operation timed out. Terminating.", UNIT(s)->id);
3185 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3188 case SERVICE_START_POST:
3189 log_warning_unit(UNIT(s)->id,
3190 "%s operation timed out. Stopping.", UNIT(s)->id);
3191 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3194 case SERVICE_RELOAD:
3195 log_warning_unit(UNIT(s)->id,
3196 "%s operation timed out. Stopping.", UNIT(s)->id);
3197 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3198 service_enter_running(s, SERVICE_SUCCESS);
3202 log_warning_unit(UNIT(s)->id,
3203 "%s stopping timed out. Terminating.", UNIT(s)->id);
3204 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3207 case SERVICE_STOP_SIGTERM:
3208 if (s->kill_context.send_sigkill) {
3209 log_warning_unit(UNIT(s)->id,
3210 "%s stopping timed out. Killing.", UNIT(s)->id);
3211 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3213 log_warning_unit(UNIT(s)->id,
3214 "%s stopping timed out. Skipping SIGKILL.", UNIT(s)->id);
3215 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3220 case SERVICE_STOP_SIGKILL:
3221 /* Uh, we sent a SIGKILL and it is still not gone?
3222 * Must be something we cannot kill, so let's just be
3223 * weirded out and continue */
3225 log_warning_unit(UNIT(s)->id,
3226 "%s still around after SIGKILL. Ignoring.", UNIT(s)->id);
3227 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3230 case SERVICE_STOP_POST:
3231 log_warning_unit(UNIT(s)->id,
3232 "%s stopping timed out (2). Terminating.", UNIT(s)->id);
3233 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3236 case SERVICE_FINAL_SIGTERM:
3237 if (s->kill_context.send_sigkill) {
3238 log_warning_unit(UNIT(s)->id,
3239 "%s stopping timed out (2). Killing.", UNIT(s)->id);
3240 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3242 log_warning_unit(UNIT(s)->id,
3243 "%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.",
3245 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3250 case SERVICE_FINAL_SIGKILL:
3251 log_warning_unit(UNIT(s)->id,
3252 "%s still around after SIGKILL (2). Entering failed mode.", UNIT(s)->id);
3253 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3256 case SERVICE_AUTO_RESTART:
3257 log_info_unit(UNIT(s)->id,
3258 "%s holdoff time over, scheduling restart.", UNIT(s)->id);
3259 service_enter_restart(s);
3263 assert_not_reached("Timeout at wrong time.");
3269 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
3270 Service *s = SERVICE(userdata);
3273 assert(source == s->watchdog_event_source);
3275 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
3276 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_WATCHDOG);
3281 static void service_notify_cgroup_empty_event(Unit *u) {
3282 Service *s = SERVICE(u);
3286 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
3290 /* Waiting for SIGCHLD is usually more interesting,
3291 * because it includes return codes/signals. Which is
3292 * why we ignore the cgroup events for most cases,
3293 * except when we don't know pid which to expect the
3297 case SERVICE_START_POST:
3298 /* If we were hoping for the daemon to write its PID file,
3299 * we can give up now. */
3300 if (s->pid_file_pathspec) {
3301 log_warning_unit(u->id,
3302 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3303 service_unwatch_pid_file(s);
3304 if (s->state == SERVICE_START)
3305 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3307 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3311 case SERVICE_RUNNING:
3312 /* service_enter_running() will figure out what to do */
3313 service_enter_running(s, SERVICE_SUCCESS);
3316 case SERVICE_STOP_SIGTERM:
3317 case SERVICE_STOP_SIGKILL:
3319 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3320 service_enter_stop_post(s, SERVICE_SUCCESS);
3324 case SERVICE_STOP_POST:
3325 case SERVICE_FINAL_SIGTERM:
3326 case SERVICE_FINAL_SIGKILL:
3327 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3328 service_enter_dead(s, SERVICE_SUCCESS, true);
3337 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3338 Service *s = SERVICE(u);
3340 bool notify_dbus = false;
3344 if (s->notify_access == NOTIFY_NONE) {
3345 log_warning_unit(u->id,
3346 "%s: Got notification message from PID %lu, but reception is disabled.",
3347 u->id, (unsigned long) pid);
3351 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3352 log_warning_unit(u->id,
3353 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3354 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3358 log_debug_unit(u->id, "%s: Got message", u->id);
3360 /* Interpret MAINPID= */
3361 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3362 (s->state == SERVICE_START ||
3363 s->state == SERVICE_START_POST ||
3364 s->state == SERVICE_RUNNING ||
3365 s->state == SERVICE_RELOAD)) {
3367 if (parse_pid(e + 8, &pid) < 0)
3368 log_warning_unit(u->id, "Failed to parse notification message %s", e);
3370 log_debug_unit(u->id, "%s: got %s", u->id, e);
3371 service_set_main_pid(s, pid);
3372 unit_watch_pid(UNIT(s), pid);
3377 /* Interpret READY= */
3378 if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START && strv_find(tags, "READY=1")) {
3379 log_debug_unit(u->id, "%s: got READY=1", u->id);
3380 service_enter_start_post(s);
3384 /* Interpret STATUS= */
3385 e = strv_find_prefix(tags, "STATUS=");
3390 if (!utf8_is_valid(e+7)) {
3391 log_warning_unit(u->id, "Status message in notification is not UTF-8 clean.");
3395 log_debug_unit(u->id, "%s: got %s", u->id, e);
3406 if (!streq_ptr(s->status_text, t)) {
3407 free(s->status_text);
3414 /* Interpet WATCHDOG= */
3415 if (strv_find(tags, "WATCHDOG=1")) {
3416 log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id);
3417 service_reset_watchdog(s);
3420 /* Notify clients about changed status or main pid */
3422 unit_add_to_dbus_queue(u);
3425 #ifdef HAVE_SYSV_COMPAT
3427 static int service_enumerate(Manager *m) {
3430 _cleanup_closedir_ DIR *d = NULL;
3431 _cleanup_free_ char *path = NULL, *fpath = NULL, *name = NULL;
3432 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
3433 _cleanup_set_free_ Set *shutdown_services = NULL;
3440 if (m->running_as != SYSTEMD_SYSTEM)
3443 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3444 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3448 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3459 if (errno != ENOENT)
3460 log_warning("opendir(%s) failed: %m", path);
3465 while ((de = readdir(d))) {
3468 if (ignore_file(de->d_name))
3471 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3474 if (strlen(de->d_name) < 4)
3477 a = undecchar(de->d_name[1]);
3478 b = undecchar(de->d_name[2]);
3484 fpath = strjoin(path, "/", de->d_name, NULL);
3490 if (access(fpath, X_OK) < 0) {
3492 if (errno != ENOENT)
3493 log_warning("access() failed on %s: %m", fpath);
3499 name = sysv_translate_name(de->d_name + 3);
3505 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3507 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3511 if (de->d_name[0] == 'S') {
3513 if (rcnd_table[i].type == RUNLEVEL_UP) {
3514 SERVICE(service)->sysv_start_priority_from_rcnd =
3515 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3517 SERVICE(service)->sysv_enabled = true;
3520 r = set_ensure_allocated(&runlevel_services[i],
3521 trivial_hash_func, trivial_compare_func);
3525 r = set_put(runlevel_services[i], service);
3529 } else if (de->d_name[0] == 'K' &&
3530 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3532 r = set_ensure_allocated(&shutdown_services,
3533 trivial_hash_func, trivial_compare_func);
3537 r = set_put(shutdown_services, service);
3544 /* Now we loaded all stubs and are aware of the lowest
3545 start-up priority for all services, not let's actually load
3546 the services, this will also tell us which services are
3547 actually native now */
3548 manager_dispatch_load_queue(m);
3550 /* If this is a native service, rely on native ways to pull in
3551 * a service, don't pull it in via sysv rcN.d links. */
3552 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3553 SET_FOREACH(service, runlevel_services[i], j) {
3554 service = unit_follow_merge(service);
3556 if (service->fragment_path)
3559 r = unit_add_two_dependencies_by_name_inverse(
3560 service, UNIT_AFTER, UNIT_WANTS,
3561 rcnd_table[i].target, NULL, true);
3566 /* We honour K links only for halt/reboot. For the normal
3567 * runlevels we assume the stop jobs will be implicitly added
3568 * by the core logic. Also, we don't really distinguish here
3569 * between the runlevels 0 and 6 and just add them to the
3570 * special shutdown target. */
3571 SET_FOREACH(service, shutdown_services, j) {
3572 service = unit_follow_merge(service);
3574 if (service->fragment_path)
3577 r = unit_add_two_dependencies_by_name(
3578 service, UNIT_BEFORE, UNIT_CONFLICTS,
3579 SPECIAL_SHUTDOWN_TARGET, NULL, true);
3588 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3589 set_free(runlevel_services[i]);
3595 static void service_bus_name_owner_change(
3598 const char *old_owner,
3599 const char *new_owner) {
3601 Service *s = SERVICE(u);
3607 assert(streq(s->bus_name, name));
3608 assert(old_owner || new_owner);
3610 if (old_owner && new_owner)
3611 log_debug_unit(u->id,
3612 "%s's D-Bus name %s changed owner from %s to %s",
3613 u->id, name, old_owner, new_owner);
3615 log_debug_unit(u->id,
3616 "%s's D-Bus name %s no longer registered by %s",
3617 u->id, name, old_owner);
3619 log_debug_unit(u->id,
3620 "%s's D-Bus name %s now registered by %s",
3621 u->id, name, new_owner);
3623 s->bus_name_good = !!new_owner;
3625 if (s->type == SERVICE_DBUS) {
3627 /* service_enter_running() will figure out what to
3629 if (s->state == SERVICE_RUNNING)
3630 service_enter_running(s, SERVICE_SUCCESS);
3631 else if (s->state == SERVICE_START && new_owner)
3632 service_enter_start_post(s);
3634 } else if (new_owner &&
3636 (s->state == SERVICE_START ||
3637 s->state == SERVICE_START_POST ||
3638 s->state == SERVICE_RUNNING ||
3639 s->state == SERVICE_RELOAD)) {
3641 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
3644 /* Try to acquire PID from bus service */
3646 r = sd_bus_get_owner(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
3648 r = sd_bus_creds_get_pid(creds, &pid);
3650 log_debug_unit(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3652 service_set_main_pid(s, pid);
3653 unit_watch_pid(UNIT(s), pid);
3658 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3663 /* This is called by the socket code when instantiating a new
3664 * service for a stream socket and the socket needs to be
3667 if (UNIT(s)->load_state != UNIT_LOADED)
3670 if (s->socket_fd >= 0)
3673 if (s->state != SERVICE_DEAD)
3678 unit_ref_set(&s->accept_socket, UNIT(sock));
3680 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3683 static void service_reset_failed(Unit *u) {
3684 Service *s = SERVICE(u);
3688 if (s->state == SERVICE_FAILED)
3689 service_set_state(s, SERVICE_DEAD);
3691 s->result = SERVICE_SUCCESS;
3692 s->reload_result = SERVICE_SUCCESS;
3694 RATELIMIT_RESET(s->start_limit);
3697 static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
3698 Service *s = SERVICE(u);
3700 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
3703 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3704 [SERVICE_DEAD] = "dead",
3705 [SERVICE_START_PRE] = "start-pre",
3706 [SERVICE_START] = "start",
3707 [SERVICE_START_POST] = "start-post",
3708 [SERVICE_RUNNING] = "running",
3709 [SERVICE_EXITED] = "exited",
3710 [SERVICE_RELOAD] = "reload",
3711 [SERVICE_STOP] = "stop",
3712 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3713 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3714 [SERVICE_STOP_POST] = "stop-post",
3715 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3716 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3717 [SERVICE_FAILED] = "failed",
3718 [SERVICE_AUTO_RESTART] = "auto-restart",
3721 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3723 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3724 [SERVICE_RESTART_NO] = "no",
3725 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3726 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3727 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
3728 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3729 [SERVICE_RESTART_ALWAYS] = "always"
3732 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3734 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3735 [SERVICE_SIMPLE] = "simple",
3736 [SERVICE_FORKING] = "forking",
3737 [SERVICE_ONESHOT] = "oneshot",
3738 [SERVICE_DBUS] = "dbus",
3739 [SERVICE_NOTIFY] = "notify",
3740 [SERVICE_IDLE] = "idle"
3743 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3745 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3746 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3747 [SERVICE_EXEC_START] = "ExecStart",
3748 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3749 [SERVICE_EXEC_RELOAD] = "ExecReload",
3750 [SERVICE_EXEC_STOP] = "ExecStop",
3751 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3754 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3756 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3757 [NOTIFY_NONE] = "none",
3758 [NOTIFY_MAIN] = "main",
3759 [NOTIFY_ALL] = "all"
3762 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3764 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3765 [SERVICE_SUCCESS] = "success",
3766 [SERVICE_FAILURE_RESOURCES] = "resources",
3767 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3768 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3769 [SERVICE_FAILURE_SIGNAL] = "signal",
3770 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3771 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3772 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3775 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3777 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3778 [SERVICE_START_LIMIT_NONE] = "none",
3779 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3780 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3781 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3783 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3785 const UnitVTable service_vtable = {
3786 .object_size = sizeof(Service),
3787 .exec_context_offset = offsetof(Service, exec_context),
3788 .cgroup_context_offset = offsetof(Service, cgroup_context),
3789 .kill_context_offset = offsetof(Service, kill_context),
3790 .exec_runtime_offset = offsetof(Service, exec_runtime),
3796 .private_section = "Service",
3798 .init = service_init,
3799 .done = service_done,
3800 .load = service_load,
3802 .coldplug = service_coldplug,
3804 .dump = service_dump,
3806 .start = service_start,
3807 .stop = service_stop,
3808 .reload = service_reload,
3810 .can_reload = service_can_reload,
3812 .kill = service_kill,
3814 .serialize = service_serialize,
3815 .deserialize_item = service_deserialize_item,
3817 .active_state = service_active_state,
3818 .sub_state_to_string = service_sub_state_to_string,
3820 .check_gc = service_check_gc,
3821 .check_snapshot = service_check_snapshot,
3823 .sigchld_event = service_sigchld_event,
3825 .reset_failed = service_reset_failed,
3827 .notify_cgroup_empty = service_notify_cgroup_empty_event,
3828 .notify_message = service_notify_message,
3830 .bus_name_owner_change = service_bus_name_owner_change,
3832 .bus_interface = "org.freedesktop.systemd1.Service",
3833 .bus_vtable = bus_service_vtable,
3834 .bus_set_property = bus_service_set_property,
3835 .bus_commit_properties = bus_service_commit_properties,
3837 #ifdef HAVE_SYSV_COMPAT
3838 .enumerate = service_enumerate,
3841 .can_transient = true,
3843 .status_message_formats = {
3844 .starting_stopping = {
3845 [0] = "Starting %s...",
3846 [1] = "Stopping %s...",
3848 .finished_start_job = {
3849 [JOB_DONE] = "Started %s.",
3850 [JOB_FAILED] = "Failed to start %s.",
3851 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3852 [JOB_TIMEOUT] = "Timed out starting %s.",
3854 .finished_stop_job = {
3855 [JOB_DONE] = "Stopped %s.",
3856 [JOB_FAILED] = "Stopped (with error) %s.",
3857 [JOB_TIMEOUT] = "Timed out stopping %s.",