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 "dbus-common.h"
40 #include "exit-status.h"
42 #include "path-util.h"
48 #ifdef HAVE_SYSV_COMPAT
50 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
52 typedef enum RunlevelType {
60 const RunlevelType type;
62 /* Standard SysV runlevels for start-up */
63 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
64 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
65 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
66 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
67 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
69 /* Standard SysV runlevels for shutdown */
70 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
71 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
73 /* Note that the order here matters, as we read the
74 directories in this order, and we want to make sure that
75 sysv_start_priority is known when we first load the
76 unit. And that value we only know from S links. Hence
77 UP must be read before DOWN */
80 #define RUNLEVELS_UP "12345"
83 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
84 [SERVICE_DEAD] = UNIT_INACTIVE,
85 [SERVICE_START_PRE] = UNIT_ACTIVATING,
86 [SERVICE_START] = UNIT_ACTIVATING,
87 [SERVICE_START_POST] = UNIT_ACTIVATING,
88 [SERVICE_RUNNING] = UNIT_ACTIVE,
89 [SERVICE_EXITED] = UNIT_ACTIVE,
90 [SERVICE_RELOAD] = UNIT_RELOADING,
91 [SERVICE_STOP] = UNIT_DEACTIVATING,
92 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
93 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
94 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
95 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
96 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
97 [SERVICE_FAILED] = UNIT_FAILED,
98 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
101 /* For Type=idle we never want to delay any other jobs, hence we
102 * consider idle jobs active as soon as we start working on them */
103 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
104 [SERVICE_DEAD] = UNIT_INACTIVE,
105 [SERVICE_START_PRE] = UNIT_ACTIVE,
106 [SERVICE_START] = UNIT_ACTIVE,
107 [SERVICE_START_POST] = UNIT_ACTIVE,
108 [SERVICE_RUNNING] = UNIT_ACTIVE,
109 [SERVICE_EXITED] = UNIT_ACTIVE,
110 [SERVICE_RELOAD] = UNIT_RELOADING,
111 [SERVICE_STOP] = UNIT_DEACTIVATING,
112 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
113 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
114 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
115 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
116 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
117 [SERVICE_FAILED] = UNIT_FAILED,
118 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
121 static void service_init(Unit *u) {
122 Service *s = SERVICE(u);
125 assert(u->load_state == UNIT_STUB);
127 s->timeout_start_usec = DEFAULT_TIMEOUT_USEC;
128 s->timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
129 s->restart_usec = DEFAULT_RESTART_USEC;
130 s->type = _SERVICE_TYPE_INVALID;
132 watch_init(&s->watchdog_watch);
133 watch_init(&s->timer_watch);
135 #ifdef HAVE_SYSV_COMPAT
136 s->sysv_start_priority = -1;
137 s->sysv_start_priority_from_rcnd = -1;
140 s->guess_main_pid = true;
142 exec_context_init(&s->exec_context);
143 kill_context_init(&s->kill_context);
144 cgroup_context_init(&s->cgroup_context);
146 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
148 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
151 static void service_unwatch_control_pid(Service *s) {
154 if (s->control_pid <= 0)
157 unit_unwatch_pid(UNIT(s), s->control_pid);
161 static void service_unwatch_main_pid(Service *s) {
164 if (s->main_pid <= 0)
167 unit_unwatch_pid(UNIT(s), s->main_pid);
171 static void service_unwatch_pid_file(Service *s) {
172 if (!s->pid_file_pathspec)
175 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
176 UNIT(s)->id, s->pid_file_pathspec->path);
177 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
178 path_spec_done(s->pid_file_pathspec);
179 free(s->pid_file_pathspec);
180 s->pid_file_pathspec = NULL;
183 static int service_set_main_pid(Service *s, pid_t pid) {
194 if (s->main_pid == pid && s->main_pid_known)
197 if (s->main_pid != pid) {
198 service_unwatch_main_pid(s);
199 exec_status_start(&s->main_exec_status, pid);
203 s->main_pid_known = true;
205 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
206 log_warning_unit(UNIT(s)->id,
207 "%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
208 UNIT(s)->id, (unsigned long) pid);
210 s->main_pid_alien = true;
212 s->main_pid_alien = false;
217 static void service_close_socket_fd(Service *s) {
220 if (s->socket_fd < 0)
223 close_nointr_nofail(s->socket_fd);
227 static void service_connection_unref(Service *s) {
230 if (!UNIT_ISSET(s->accept_socket))
233 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
234 unit_ref_unset(&s->accept_socket);
237 static void service_stop_watchdog(Service *s) {
240 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
241 s->watchdog_timestamp.realtime = 0;
242 s->watchdog_timestamp.monotonic = 0;
245 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
247 static void service_handle_watchdog(Service *s) {
253 if (s->watchdog_usec == 0)
256 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
257 if (offset >= s->watchdog_usec) {
258 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
259 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_WATCHDOG);
263 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->watchdog_usec - offset, &s->watchdog_watch);
265 log_warning_unit(UNIT(s)->id,
266 "%s failed to install watchdog timer: %s",
267 UNIT(s)->id, strerror(-r));
270 static void service_reset_watchdog(Service *s) {
273 dual_timestamp_get(&s->watchdog_timestamp);
274 service_handle_watchdog(s);
277 static void service_done(Unit *u) {
278 Service *s = SERVICE(u);
285 #ifdef HAVE_SYSV_COMPAT
286 free(s->sysv_runlevels);
287 s->sysv_runlevels = NULL;
290 free(s->status_text);
291 s->status_text = NULL;
293 cgroup_context_done(&s->cgroup_context);
294 exec_context_done(&s->exec_context, manager_is_reloading_or_reexecuting(u->manager));
295 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
296 s->control_command = NULL;
297 s->main_command = NULL;
299 set_free(s->restart_ignore_status.code);
300 s->restart_ignore_status.code = NULL;
301 set_free(s->restart_ignore_status.signal);
302 s->restart_ignore_status.signal = NULL;
304 set_free(s->success_status.code);
305 s->success_status.code = NULL;
306 set_free(s->success_status.signal);
307 s->success_status.signal = NULL;
309 /* This will leak a process, but at least no memory or any of
311 service_unwatch_main_pid(s);
312 service_unwatch_control_pid(s);
313 service_unwatch_pid_file(s);
316 unit_unwatch_bus_name(u, s->bus_name);
321 service_close_socket_fd(s);
322 service_connection_unref(s);
324 unit_ref_unset(&s->accept_socket);
326 service_stop_watchdog(s);
328 unit_unwatch_timer(u, &s->timer_watch);
331 #ifdef HAVE_SYSV_COMPAT
332 static char *sysv_translate_name(const char *name) {
335 r = new(char, strlen(name) + sizeof(".service"));
339 if (endswith(name, ".sh"))
340 /* Drop .sh suffix */
341 strcpy(stpcpy(r, name) - 3, ".service");
343 /* Normal init script name */
344 strcpy(stpcpy(r, name), ".service");
349 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
351 /* We silently ignore the $ prefix here. According to the LSB
352 * spec it simply indicates whether something is a
353 * standardized name or a distribution-specific one. Since we
354 * just follow what already exists and do not introduce new
355 * uses or names we don't care who introduced a new name. */
357 static const char * const table[] = {
358 /* LSB defined facilities */
360 "network", SPECIAL_NETWORK_TARGET,
361 "named", SPECIAL_NSS_LOOKUP_TARGET,
362 "portmap", SPECIAL_RPCBIND_TARGET,
363 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
365 "time", SPECIAL_TIME_SYNC_TARGET,
375 n = *name == '$' ? name + 1 : name;
377 for (i = 0; i < ELEMENTSOF(table); i += 2) {
379 if (!streq(table[i], n))
385 r = strdup(table[i+1]);
392 /* If we don't know this name, fallback heuristics to figure
393 * out whether something is a target or a service alias. */
396 if (!unit_prefix_is_valid(n))
399 /* Facilities starting with $ are most likely targets */
400 r = unit_name_build(n, NULL, ".target");
401 } else if (filename && streq(name, filename))
402 /* Names equaling the file name of the services are redundant */
405 /* Everything else we assume to be normal service names */
406 r = sysv_translate_name(n);
417 static int sysv_fix_order(Service *s) {
423 if (s->sysv_start_priority < 0)
426 /* For each pair of services where at least one lacks a LSB
427 * header, we use the start priority value to order things. */
429 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
432 bool special_s, special_t;
439 if (UNIT(t)->load_state != UNIT_LOADED)
442 if (t->sysv_start_priority < 0)
445 /* If both units have modern headers we don't care
446 * about the priorities */
447 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
448 (UNIT(t)->fragment_path || t->sysv_has_lsb))
451 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
452 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
454 if (special_t && !special_s)
456 else if (special_s && !special_t)
458 else if (t->sysv_start_priority < s->sysv_start_priority)
460 else if (t->sysv_start_priority > s->sysv_start_priority)
465 /* FIXME: Maybe we should compare the name here lexicographically? */
467 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
474 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
477 if (!(c = new0(ExecCommand, 1)))
480 if (!(c->path = strdup(path))) {
485 if (!(c->argv = strv_new(path, arg1, NULL))) {
494 static int sysv_exec_commands(Service *s, const bool supports_reload) {
499 assert(UNIT(s)->source_path);
501 c = exec_command_new(UNIT(s)->source_path, "start");
504 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
506 c = exec_command_new(UNIT(s)->source_path, "stop");
509 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
511 if (supports_reload) {
512 c = exec_command_new(UNIT(s)->source_path, "reload");
515 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
521 static bool usage_contains_reload(const char *line) {
522 return (strcasestr(line, "{reload|") ||
523 strcasestr(line, "{reload}") ||
524 strcasestr(line, "{reload\"") ||
525 strcasestr(line, "|reload|") ||
526 strcasestr(line, "|reload}") ||
527 strcasestr(line, "|reload\""));
530 static int service_load_sysv_path(Service *s, const char *path) {
542 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
544 bool supports_reload = false;
551 f = fopen(path, "re");
553 r = errno == ENOENT ? 0 : -errno;
557 if (fstat(fileno(f), &st) < 0) {
562 free(u->source_path);
563 u->source_path = strdup(path);
564 if (!u->source_path) {
568 u->source_mtime = timespec_load(&st.st_mtim);
570 if (null_or_empty(&st)) {
571 u->load_state = UNIT_MASKED;
579 char l[LINE_MAX], *t;
581 if (!fgets(l, sizeof(l), f)) {
586 log_error_unit(u->id,
587 "Failed to read configuration file '%s': %s",
596 /* Try to figure out whether this init script supports
597 * the reload operation. This heuristic looks for
598 * "Usage" lines which include the reload option. */
599 if ( state == USAGE_CONTINUATION ||
600 (state == NORMAL && strcasestr(t, "usage"))) {
601 if (usage_contains_reload(t)) {
602 supports_reload = true;
604 } else if (t[strlen(t)-1] == '\\')
605 state = USAGE_CONTINUATION;
613 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
615 s->sysv_has_lsb = true;
619 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
625 t += strspn(t, WHITESPACE);
627 if (state == NORMAL) {
629 /* Try to parse Red Hat style chkconfig headers */
631 if (startswith_no_case(t, "chkconfig:")) {
633 char runlevels[16], *k;
637 if (sscanf(t+10, "%15s %i %*i",
639 &start_priority) != 2) {
641 log_warning_unit(u->id,
642 "[%s:%u] Failed to parse chkconfig line. Ignoring.",
647 /* A start priority gathered from the
648 * symlink farms is preferred over the
649 * data from the LSB header. */
650 if (start_priority < 0 || start_priority > 99)
651 log_warning_unit(u->id,
652 "[%s:%u] Start priority out of range. Ignoring.",
655 s->sysv_start_priority = start_priority;
657 char_array_0(runlevels);
658 k = delete_chars(runlevels, WHITESPACE "-");
663 if (!(d = strdup(k))) {
668 free(s->sysv_runlevels);
669 s->sysv_runlevels = d;
672 } else if (startswith_no_case(t, "description:")) {
674 size_t k = strlen(t);
678 if (t[k-1] == '\\') {
683 if ((j = strstrip(t+12)) && *j) {
684 if (!(d = strdup(j))) {
691 free(chkconfig_description);
692 chkconfig_description = d;
694 } else if (startswith_no_case(t, "pidfile:")) {
701 if (!path_is_absolute(fn)) {
702 log_warning_unit(u->id,
703 "[%s:%u] PID file not absolute. Ignoring.",
708 if (!(fn = strdup(fn))) {
717 } else if (state == DESCRIPTION) {
719 /* Try to parse Red Hat style description
722 size_t k = strlen(t);
730 if ((j = strstrip(t)) && *j) {
733 if (chkconfig_description)
734 d = strjoin(chkconfig_description, " ", j, NULL);
743 free(chkconfig_description);
744 chkconfig_description = d;
747 } else if (state == LSB || state == LSB_DESCRIPTION) {
749 if (startswith_no_case(t, "Provides:")) {
755 FOREACH_WORD_QUOTED(w, z, t+9, i) {
758 if (!(n = strndup(w, z))) {
763 r = sysv_translate_facility(n, path_get_file_name(path), &m);
772 if (unit_name_to_type(m) == UNIT_SERVICE)
773 r = unit_merge_by_name(u, m);
780 * indication that the
782 * now available. This
785 * targets do NOT pull
788 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
791 log_error_unit(u->id,
792 "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
793 path, line, m, strerror(-r));
798 } else if (startswith_no_case(t, "Required-Start:") ||
799 startswith_no_case(t, "Should-Start:") ||
800 startswith_no_case(t, "X-Start-Before:") ||
801 startswith_no_case(t, "X-Start-After:")) {
807 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
810 if (!(n = strndup(w, z))) {
815 r = sysv_translate_facility(n, path_get_file_name(path), &m);
817 log_error_unit(u->id,
818 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
819 path, line, n, strerror(-r));
829 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
832 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
833 path, line, m, strerror(-r));
837 } else if (startswith_no_case(t, "Default-Start:")) {
842 k = delete_chars(t+14, WHITESPACE "-");
845 if (!(d = strdup(k))) {
850 free(s->sysv_runlevels);
851 s->sysv_runlevels = d;
854 } else if (startswith_no_case(t, "Description:")) {
857 state = LSB_DESCRIPTION;
859 if ((j = strstrip(t+12)) && *j) {
860 if (!(d = strdup(j))) {
867 free(long_description);
868 long_description = d;
870 } else if (startswith_no_case(t, "Short-Description:")) {
875 if ((j = strstrip(t+18)) && *j) {
876 if (!(d = strdup(j))) {
883 free(short_description);
884 short_description = d;
886 } else if (state == LSB_DESCRIPTION) {
888 if (startswith(l, "#\t") || startswith(l, "# ")) {
891 if ((j = strstrip(t)) && *j) {
894 if (long_description)
895 d = strjoin(long_description, " ", t, NULL);
904 free(long_description);
905 long_description = d;
914 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
917 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
918 /* If there a runlevels configured for this service
919 * but none of the standard ones, then we assume this
920 * is some special kind of service (which might be
921 * needed for early boot) and don't create any links
924 UNIT(s)->default_dependencies = false;
926 /* Don't timeout special services during boot (like fsck) */
927 s->timeout_start_usec = 0;
928 s->timeout_stop_usec = 0;
930 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
931 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
934 /* Special setting for all SysV services */
935 s->type = SERVICE_FORKING;
936 s->remain_after_exit = !s->pid_file;
937 s->guess_main_pid = false;
938 s->restart = SERVICE_RESTART_NO;
939 s->exec_context.ignore_sigpipe = false;
940 s->kill_context.kill_mode = KILL_PROCESS;
942 /* We use the long description only if
943 * no short description is set. */
945 if (short_description)
946 description = short_description;
947 else if (chkconfig_description)
948 description = chkconfig_description;
949 else if (long_description)
950 description = long_description;
957 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
965 /* The priority that has been set in /etc/rcN.d/ hierarchies
966 * takes precedence over what is stored as default in the LSB
968 if (s->sysv_start_priority_from_rcnd >= 0)
969 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
971 u->load_state = UNIT_LOADED;
979 free(short_description);
980 free(long_description);
981 free(chkconfig_description);
986 static int service_load_sysv_name(Service *s, const char *name) {
992 /* For SysV services we strip the *.sh suffixes. */
993 if (endswith(name, ".sh.service"))
996 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1000 path = strjoin(*p, "/", name, NULL);
1004 assert(endswith(path, ".service"));
1005 path[strlen(path)-8] = 0;
1007 r = service_load_sysv_path(s, path);
1009 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1010 /* Try *.sh source'able init scripts */
1011 strcat(path, ".sh");
1012 r = service_load_sysv_path(s, path);
1019 if (UNIT(s)->load_state != UNIT_STUB)
1026 static int service_load_sysv(Service *s) {
1033 /* Load service data from SysV init scripts, preferably with
1034 * LSB headers ... */
1036 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1039 if ((t = UNIT(s)->id))
1040 if ((r = service_load_sysv_name(s, t)) < 0)
1043 if (UNIT(s)->load_state == UNIT_STUB)
1044 SET_FOREACH(t, UNIT(s)->names, i) {
1045 if (t == UNIT(s)->id)
1048 if ((r = service_load_sysv_name(s, t)) < 0)
1051 if (UNIT(s)->load_state != UNIT_STUB)
1059 static int fsck_fix_order(Service *s) {
1065 if (s->fsck_passno <= 0)
1068 /* For each pair of services where both have an fsck priority
1069 * we order things based on it. */
1071 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1080 if (UNIT(t)->load_state != UNIT_LOADED)
1083 if (t->fsck_passno <= 0)
1086 if (t->fsck_passno < s->fsck_passno)
1088 else if (t->fsck_passno > s->fsck_passno)
1093 r = unit_add_dependency(UNIT(s), d, UNIT(t), true);
1101 static int service_verify(Service *s) {
1104 if (UNIT(s)->load_state != UNIT_LOADED)
1107 if (!s->exec_command[SERVICE_EXEC_START]) {
1108 log_error_unit(UNIT(s)->id,
1109 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1113 if (s->type != SERVICE_ONESHOT &&
1114 s->exec_command[SERVICE_EXEC_START]->command_next) {
1115 log_error_unit(UNIT(s)->id,
1116 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1120 if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
1121 log_error_unit(UNIT(s)->id,
1122 "%s has Restart setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1126 if (s->type == SERVICE_DBUS && !s->bus_name) {
1127 log_error_unit(UNIT(s)->id,
1128 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1132 if (s->bus_name && s->type != SERVICE_DBUS)
1133 log_warning_unit(UNIT(s)->id,
1134 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1136 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1137 log_error_unit(UNIT(s)->id,
1138 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1145 static int service_add_default_dependencies(Service *s) {
1150 /* Add a number of automatic dependencies useful for the
1151 * majority of services. */
1153 /* First, pull in base system */
1154 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1155 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1156 SPECIAL_BASIC_TARGET, NULL, true);
1160 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1161 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1162 SPECIAL_SOCKETS_TARGET, NULL, true);
1166 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1167 SPECIAL_TIMERS_TARGET, NULL, true);
1171 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1172 SPECIAL_PATHS_TARGET, NULL, true);
1177 /* Second, activate normal shutdown */
1178 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
1179 SPECIAL_SHUTDOWN_TARGET, NULL, true);
1183 static void service_fix_output(Service *s) {
1186 /* If nothing has been explicitly configured, patch default
1187 * output in. If input is socket/tty we avoid this however,
1188 * since in that case we want output to default to the same
1189 * place as we read input from. */
1191 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1192 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1193 s->exec_context.std_input == EXEC_INPUT_NULL)
1194 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1196 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1197 s->exec_context.std_input == EXEC_INPUT_NULL)
1198 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1201 static int service_load(Unit *u) {
1203 Service *s = SERVICE(u);
1207 /* Load a .service file */
1208 r = unit_load_fragment(u);
1212 #ifdef HAVE_SYSV_COMPAT
1213 /* Load a classic init script as a fallback, if we couldn't find anything */
1214 if (u->load_state == UNIT_STUB) {
1215 r = service_load_sysv(s);
1221 /* Still nothing found? Then let's give up */
1222 if (u->load_state == UNIT_STUB)
1225 /* This is a new unit? Then let's add in some extras */
1226 if (u->load_state == UNIT_LOADED) {
1228 /* We were able to load something, then let's add in
1229 * the dropin directories. */
1230 r = unit_load_dropin(u);
1234 if (s->type == _SERVICE_TYPE_INVALID)
1235 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1237 /* Oneshot services have disabled start timeout by default */
1238 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1239 s->timeout_start_usec = 0;
1241 service_fix_output(s);
1243 r = unit_add_exec_dependencies(u, &s->exec_context);
1247 r = unit_add_default_slice(u);
1251 #ifdef HAVE_SYSV_COMPAT
1252 r = sysv_fix_order(s);
1257 r = fsck_fix_order(s);
1262 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1265 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1266 s->notify_access = NOTIFY_MAIN;
1268 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1269 s->notify_access = NOTIFY_MAIN;
1271 if (s->type == SERVICE_DBUS || s->bus_name) {
1272 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES,
1273 SPECIAL_DBUS_SOCKET, NULL, true);
1278 if (UNIT(s)->default_dependencies) {
1279 r = service_add_default_dependencies(s);
1284 r = unit_exec_context_defaults(u, &s->exec_context);
1289 return service_verify(s);
1292 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1294 ServiceExecCommand c;
1295 Service *s = SERVICE(u);
1296 const char *prefix2;
1297 _cleanup_free_ char *p2 = NULL;
1301 p2 = strappend(prefix, "\t");
1302 prefix2 = p2 ? p2 : prefix;
1305 "%sService State: %s\n"
1307 "%sReload Result: %s\n"
1308 "%sPermissionsStartOnly: %s\n"
1309 "%sRootDirectoryStartOnly: %s\n"
1310 "%sRemainAfterExit: %s\n"
1311 "%sGuessMainPID: %s\n"
1314 "%sNotifyAccess: %s\n",
1315 prefix, service_state_to_string(s->state),
1316 prefix, service_result_to_string(s->result),
1317 prefix, service_result_to_string(s->reload_result),
1318 prefix, yes_no(s->permissions_start_only),
1319 prefix, yes_no(s->root_directory_start_only),
1320 prefix, yes_no(s->remain_after_exit),
1321 prefix, yes_no(s->guess_main_pid),
1322 prefix, service_type_to_string(s->type),
1323 prefix, service_restart_to_string(s->restart),
1324 prefix, notify_access_to_string(s->notify_access));
1326 if (s->control_pid > 0)
1328 "%sControl PID: %lu\n",
1329 prefix, (unsigned long) s->control_pid);
1331 if (s->main_pid > 0)
1334 "%sMain PID Known: %s\n"
1335 "%sMain PID Alien: %s\n",
1336 prefix, (unsigned long) s->main_pid,
1337 prefix, yes_no(s->main_pid_known),
1338 prefix, yes_no(s->main_pid_alien));
1343 prefix, s->pid_file);
1348 "%sBus Name Good: %s\n",
1349 prefix, s->bus_name,
1350 prefix, yes_no(s->bus_name_good));
1352 kill_context_dump(&s->kill_context, f, prefix);
1353 exec_context_dump(&s->exec_context, f, prefix);
1355 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1357 if (!s->exec_command[c])
1360 fprintf(f, "%s-> %s:\n",
1361 prefix, service_exec_command_to_string(c));
1363 exec_command_dump_list(s->exec_command[c], f, prefix2);
1366 #ifdef HAVE_SYSV_COMPAT
1369 "%sSysV Init Script has LSB Header: %s\n"
1370 "%sSysVEnabled: %s\n",
1371 prefix, yes_no(s->sysv_has_lsb),
1372 prefix, yes_no(s->sysv_enabled));
1374 if (s->sysv_start_priority >= 0)
1376 "%sSysVStartPriority: %i\n",
1377 prefix, s->sysv_start_priority);
1379 if (s->sysv_runlevels)
1380 fprintf(f, "%sSysVRunLevels: %s\n",
1381 prefix, s->sysv_runlevels);
1384 if (s->fsck_passno > 0)
1386 "%sFsckPassNo: %i\n",
1387 prefix, s->fsck_passno);
1390 fprintf(f, "%sStatus Text: %s\n",
1391 prefix, s->status_text);
1394 static int service_load_pid_file(Service *s, bool may_warn) {
1395 _cleanup_free_ char *k = NULL;
1404 r = read_one_line_file(s->pid_file, &k);
1407 log_info_unit(UNIT(s)->id,
1408 "PID file %s not readable (yet?) after %s.",
1409 s->pid_file, service_state_to_string(s->state));
1413 r = parse_pid(k, &pid);
1416 log_info_unit(UNIT(s)->id,
1417 "Failed to read PID from file %s: %s",
1418 s->pid_file, strerror(-r));
1422 if (kill(pid, 0) < 0 && errno != EPERM) {
1424 log_info_unit(UNIT(s)->id,
1425 "PID %lu read from file %s does not exist.",
1426 (unsigned long) pid, s->pid_file);
1430 if (s->main_pid_known) {
1431 if (pid == s->main_pid)
1434 log_debug_unit(UNIT(s)->id,
1435 "Main PID changing: %lu -> %lu",
1436 (unsigned long) s->main_pid, (unsigned long) pid);
1437 service_unwatch_main_pid(s);
1438 s->main_pid_known = false;
1440 log_debug_unit(UNIT(s)->id,
1441 "Main PID loaded: %lu", (unsigned long) pid);
1443 r = service_set_main_pid(s, pid);
1447 r = unit_watch_pid(UNIT(s), pid);
1449 /* FIXME: we need to do something here */
1450 log_warning_unit(UNIT(s)->id,
1451 "Failed to watch PID %lu from service %s",
1452 (unsigned long) pid, UNIT(s)->id);
1459 static int service_search_main_pid(Service *s) {
1465 /* If we know it anyway, don't ever fallback to unreliable
1467 if (s->main_pid_known)
1470 if (!s->guess_main_pid)
1473 assert(s->main_pid <= 0);
1475 pid = unit_search_main_pid(UNIT(s));
1479 log_debug_unit(UNIT(s)->id,
1480 "Main PID guessed: %lu", (unsigned long) pid);
1481 r = service_set_main_pid(s, pid);
1485 r = unit_watch_pid(UNIT(s), pid);
1487 /* FIXME: we need to do something here */
1488 log_warning_unit(UNIT(s)->id,
1489 "Failed to watch PID %lu from service %s",
1490 (unsigned long) pid, UNIT(s)->id);
1496 static void service_set_state(Service *s, ServiceState state) {
1497 ServiceState old_state;
1498 const UnitActiveState *table;
1501 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1503 old_state = s->state;
1506 service_unwatch_pid_file(s);
1508 if (state != SERVICE_START_PRE &&
1509 state != SERVICE_START &&
1510 state != SERVICE_START_POST &&
1511 state != SERVICE_RELOAD &&
1512 state != SERVICE_STOP &&
1513 state != SERVICE_STOP_SIGTERM &&
1514 state != SERVICE_STOP_SIGKILL &&
1515 state != SERVICE_STOP_POST &&
1516 state != SERVICE_FINAL_SIGTERM &&
1517 state != SERVICE_FINAL_SIGKILL &&
1518 state != SERVICE_AUTO_RESTART)
1519 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1521 if (state != SERVICE_START &&
1522 state != SERVICE_START_POST &&
1523 state != SERVICE_RUNNING &&
1524 state != SERVICE_RELOAD &&
1525 state != SERVICE_STOP &&
1526 state != SERVICE_STOP_SIGTERM &&
1527 state != SERVICE_STOP_SIGKILL) {
1528 service_unwatch_main_pid(s);
1529 s->main_command = NULL;
1532 if (state != SERVICE_START_PRE &&
1533 state != SERVICE_START &&
1534 state != SERVICE_START_POST &&
1535 state != SERVICE_RELOAD &&
1536 state != SERVICE_STOP &&
1537 state != SERVICE_STOP_SIGTERM &&
1538 state != SERVICE_STOP_SIGKILL &&
1539 state != SERVICE_STOP_POST &&
1540 state != SERVICE_FINAL_SIGTERM &&
1541 state != SERVICE_FINAL_SIGKILL) {
1542 service_unwatch_control_pid(s);
1543 s->control_command = NULL;
1544 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1547 if (state != SERVICE_START_PRE &&
1548 state != SERVICE_START &&
1549 state != SERVICE_START_POST &&
1550 state != SERVICE_RUNNING &&
1551 state != SERVICE_RELOAD &&
1552 state != SERVICE_STOP &&
1553 state != SERVICE_STOP_SIGTERM &&
1554 state != SERVICE_STOP_SIGKILL &&
1555 state != SERVICE_STOP_POST &&
1556 state != SERVICE_FINAL_SIGTERM &&
1557 state != SERVICE_FINAL_SIGKILL &&
1558 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1559 service_close_socket_fd(s);
1560 service_connection_unref(s);
1563 if (state == SERVICE_STOP || state == SERVICE_STOP_SIGTERM)
1564 service_stop_watchdog(s);
1566 /* For the inactive states unit_notify() will trim the cgroup,
1567 * but for exit we have to do that ourselves... */
1568 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1569 unit_destroy_cgroup(UNIT(s));
1571 if (old_state != state)
1572 log_debug_unit(UNIT(s)->id,
1573 "%s changed %s -> %s", UNIT(s)->id,
1574 service_state_to_string(old_state),
1575 service_state_to_string(state));
1577 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1578 s->reload_result = SERVICE_SUCCESS;
1581 static int service_coldplug(Unit *u) {
1582 Service *s = SERVICE(u);
1586 assert(s->state == SERVICE_DEAD);
1588 if (s->deserialized_state != s->state) {
1590 if (s->deserialized_state == SERVICE_START_PRE ||
1591 s->deserialized_state == SERVICE_START ||
1592 s->deserialized_state == SERVICE_START_POST ||
1593 s->deserialized_state == SERVICE_RELOAD ||
1594 s->deserialized_state == SERVICE_STOP ||
1595 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1596 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1597 s->deserialized_state == SERVICE_STOP_POST ||
1598 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1599 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1600 s->deserialized_state == SERVICE_AUTO_RESTART) {
1602 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1605 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1607 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
1613 if ((s->deserialized_state == SERVICE_START &&
1614 (s->type == SERVICE_FORKING ||
1615 s->type == SERVICE_DBUS ||
1616 s->type == SERVICE_ONESHOT ||
1617 s->type == SERVICE_NOTIFY)) ||
1618 s->deserialized_state == SERVICE_START_POST ||
1619 s->deserialized_state == SERVICE_RUNNING ||
1620 s->deserialized_state == SERVICE_RELOAD ||
1621 s->deserialized_state == SERVICE_STOP ||
1622 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1623 s->deserialized_state == SERVICE_STOP_SIGKILL)
1624 if (s->main_pid > 0) {
1625 r = unit_watch_pid(UNIT(s), s->main_pid);
1630 if (s->deserialized_state == SERVICE_START_PRE ||
1631 s->deserialized_state == SERVICE_START ||
1632 s->deserialized_state == SERVICE_START_POST ||
1633 s->deserialized_state == SERVICE_RELOAD ||
1634 s->deserialized_state == SERVICE_STOP ||
1635 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1636 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1637 s->deserialized_state == SERVICE_STOP_POST ||
1638 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1639 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1640 if (s->control_pid > 0) {
1641 r = unit_watch_pid(UNIT(s), s->control_pid);
1646 if (s->deserialized_state == SERVICE_START_POST ||
1647 s->deserialized_state == SERVICE_RUNNING)
1648 service_handle_watchdog(s);
1650 service_set_state(s, s->deserialized_state);
1655 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1659 unsigned rn_fds = 0;
1666 if (s->socket_fd >= 0)
1669 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1674 if (u->type != UNIT_SOCKET)
1679 r = socket_collect_fds(sock, &cfds, &cn_fds);
1692 t = new(int, rn_fds+cn_fds);
1699 memcpy(t, rfds, rn_fds * sizeof(int));
1700 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1705 rn_fds = rn_fds+cn_fds;
1720 static int service_spawn(
1725 bool apply_permissions,
1727 bool apply_tty_stdin,
1728 bool set_notify_socket,
1735 _cleanup_free_ int *fdsbuf = NULL;
1736 unsigned n_fds = 0, n_env = 0;
1737 _cleanup_strv_free_ char
1738 **argv = NULL, **final_env = NULL, **our_env = NULL;
1745 unit_realize_cgroup(UNIT(s));
1748 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1749 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1750 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1752 if (s->socket_fd >= 0) {
1753 fds = &s->socket_fd;
1756 r = service_collect_fds(s, &fdsbuf, &n_fds);
1764 if (timeout && s->timeout_start_usec) {
1765 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true,
1766 s->timeout_start_usec, &s->timer_watch);
1770 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1772 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
1776 our_env = new0(char*, 5);
1782 if (set_notify_socket)
1783 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1788 if (s->main_pid > 0)
1789 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1794 if (s->watchdog_usec > 0)
1795 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1800 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
1801 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1806 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1812 if (is_control && UNIT(s)->cgroup_path) {
1813 path = strappenda(UNIT(s)->cgroup_path, "/control");
1814 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1816 path = UNIT(s)->cgroup_path;
1826 UNIT(s)->manager->confirm_spawn,
1827 UNIT(s)->manager->cgroup_supported,
1830 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1835 r = unit_watch_pid(UNIT(s), pid);
1837 /* FIXME: we need to do something here */
1846 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1851 static int main_pid_good(Service *s) {
1854 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1857 /* If we know the pid file, then lets just check if it is
1859 if (s->main_pid_known) {
1861 /* If it's an alien child let's check if it is still
1863 if (s->main_pid_alien && s->main_pid > 0)
1864 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1866 /* .. otherwise assume we'll get a SIGCHLD for it,
1867 * which we really should wait for to collect exit
1868 * status and code */
1869 return s->main_pid > 0;
1872 /* We don't know the pid */
1876 _pure_ static int control_pid_good(Service *s) {
1879 return s->control_pid > 0;
1882 static int cgroup_good(Service *s) {
1887 if (!UNIT(s)->cgroup_path)
1890 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1897 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1901 if (f != SERVICE_SUCCESS)
1904 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1906 if (allow_restart &&
1907 !s->forbid_restart &&
1908 (s->restart == SERVICE_RESTART_ALWAYS ||
1909 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1910 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1911 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1912 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1913 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1914 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1915 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1916 (s->result != SERVICE_FAILURE_SIGNAL ||
1917 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1920 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
1924 service_set_state(s, SERVICE_AUTO_RESTART);
1927 s->forbid_restart = false;
1929 /* we want fresh tmpdirs in case service is started again immediately */
1930 exec_context_tmp_dirs_done(&s->exec_context);
1932 /* Try to delete the pid file. At this point it will be
1933 * out-of-date, and some software might be confused by it, so
1934 * let's remove it. */
1936 unlink_noerrno(s->pid_file);
1941 log_warning_unit(UNIT(s)->id,
1942 "%s failed to run install restart timer: %s",
1943 UNIT(s)->id, strerror(-r));
1944 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1947 static void service_enter_stop_post(Service *s, ServiceResult f) {
1951 if (f != SERVICE_SUCCESS)
1954 service_unwatch_control_pid(s);
1956 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1957 if (s->control_command) {
1958 s->control_command_id = SERVICE_EXEC_STOP_POST;
1960 r = service_spawn(s,
1964 !s->permissions_start_only,
1965 !s->root_directory_start_only,
1974 service_set_state(s, SERVICE_STOP_POST);
1976 service_enter_dead(s, SERVICE_SUCCESS, true);
1981 log_warning_unit(UNIT(s)->id,
1982 "%s failed to run 'stop-post' task: %s",
1983 UNIT(s)->id, strerror(-r));
1984 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1987 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1992 if (f != SERVICE_SUCCESS)
1995 r = unit_kill_context(
1998 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
2006 if (s->timeout_stop_usec > 0) {
2007 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true,
2008 s->timeout_stop_usec, &s->timer_watch);
2013 service_set_state(s, state);
2014 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2015 service_enter_stop_post(s, SERVICE_SUCCESS);
2017 service_enter_dead(s, SERVICE_SUCCESS, true);
2022 log_warning_unit(UNIT(s)->id,
2023 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2025 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2026 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2028 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2031 static void service_enter_stop(Service *s, ServiceResult f) {
2036 if (f != SERVICE_SUCCESS)
2039 service_unwatch_control_pid(s);
2041 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
2042 if (s->control_command) {
2043 s->control_command_id = SERVICE_EXEC_STOP;
2045 r = service_spawn(s,
2049 !s->permissions_start_only,
2050 !s->root_directory_start_only,
2058 service_set_state(s, SERVICE_STOP);
2060 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2065 log_warning_unit(UNIT(s)->id,
2066 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2067 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2070 static void service_enter_running(Service *s, ServiceResult f) {
2071 int main_pid_ok, cgroup_ok;
2074 if (f != SERVICE_SUCCESS)
2077 main_pid_ok = main_pid_good(s);
2078 cgroup_ok = cgroup_good(s);
2080 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2081 (s->bus_name_good || s->type != SERVICE_DBUS))
2082 service_set_state(s, SERVICE_RUNNING);
2083 else if (s->remain_after_exit)
2084 service_set_state(s, SERVICE_EXITED);
2086 service_enter_stop(s, SERVICE_SUCCESS);
2089 static void service_enter_start_post(Service *s) {
2093 service_unwatch_control_pid(s);
2095 if (s->watchdog_usec > 0)
2096 service_reset_watchdog(s);
2098 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
2099 if (s->control_command) {
2100 s->control_command_id = SERVICE_EXEC_START_POST;
2102 r = service_spawn(s,
2106 !s->permissions_start_only,
2107 !s->root_directory_start_only,
2115 service_set_state(s, SERVICE_START_POST);
2117 service_enter_running(s, SERVICE_SUCCESS);
2122 log_warning_unit(UNIT(s)->id,
2123 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2124 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2127 static void service_kill_control_processes(Service *s) {
2130 if (!UNIT(s)->cgroup_path)
2133 p = strappenda(UNIT(s)->cgroup_path, "/control");
2134 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
2137 static void service_enter_start(Service *s) {
2144 assert(s->exec_command[SERVICE_EXEC_START]);
2145 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2147 service_unwatch_control_pid(s);
2148 service_unwatch_main_pid(s);
2150 /* We want to ensure that nobody leaks processes from
2151 * START_PRE here, so let's go on a killing spree, People
2152 * should not spawn long running processes from START_PRE. */
2153 service_kill_control_processes(s);
2155 if (s->type == SERVICE_FORKING) {
2156 s->control_command_id = SERVICE_EXEC_START;
2157 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2159 s->main_command = NULL;
2161 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2162 s->control_command = NULL;
2164 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2167 r = service_spawn(s,
2169 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
2170 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2175 s->notify_access != NOTIFY_NONE,
2181 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2182 /* For simple services we immediately start
2183 * the START_POST binaries. */
2185 service_set_main_pid(s, pid);
2186 service_enter_start_post(s);
2188 } else if (s->type == SERVICE_FORKING) {
2190 /* For forking services we wait until the start
2191 * process exited. */
2193 s->control_pid = pid;
2194 service_set_state(s, SERVICE_START);
2196 } else if (s->type == SERVICE_ONESHOT ||
2197 s->type == SERVICE_DBUS ||
2198 s->type == SERVICE_NOTIFY) {
2200 /* For oneshot services we wait until the start
2201 * process exited, too, but it is our main process. */
2203 /* For D-Bus services we know the main pid right away,
2204 * but wait for the bus name to appear on the
2205 * bus. Notify services are similar. */
2207 service_set_main_pid(s, pid);
2208 service_set_state(s, SERVICE_START);
2210 assert_not_reached("Unknown service type");
2215 log_warning_unit(UNIT(s)->id,
2216 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2217 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2220 static void service_enter_start_pre(Service *s) {
2225 service_unwatch_control_pid(s);
2227 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
2228 if (s->control_command) {
2229 /* Before we start anything, let's clear up what might
2230 * be left from previous runs. */
2231 service_kill_control_processes(s);
2233 s->control_command_id = SERVICE_EXEC_START_PRE;
2235 r = service_spawn(s,
2239 !s->permissions_start_only,
2240 !s->root_directory_start_only,
2248 service_set_state(s, SERVICE_START_PRE);
2250 service_enter_start(s);
2255 log_warning_unit(UNIT(s)->id,
2256 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2257 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2260 static void service_enter_restart(Service *s) {
2265 dbus_error_init(&error);
2267 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2268 /* Don't restart things if we are going down anyway */
2269 log_info_unit(UNIT(s)->id,
2270 "Stop job pending for unit, delaying automatic restart.");
2272 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
2279 /* Any units that are bound to this service must also be
2280 * restarted. We use JOB_RESTART (instead of the more obvious
2281 * JOB_START) here so that those dependency jobs will be added
2283 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2287 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2288 * it will be canceled as part of the service_stop() call that
2289 * is executed as part of JOB_RESTART. */
2291 log_debug_unit(UNIT(s)->id,
2292 "%s scheduled restart job.", UNIT(s)->id);
2296 log_warning_unit(UNIT(s)->id,
2297 "%s failed to schedule restart job: %s",
2298 UNIT(s)->id, bus_error(&error, -r));
2299 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2301 dbus_error_free(&error);
2304 static void service_enter_reload(Service *s) {
2309 service_unwatch_control_pid(s);
2311 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
2312 if (s->control_command) {
2313 s->control_command_id = SERVICE_EXEC_RELOAD;
2315 r = service_spawn(s,
2319 !s->permissions_start_only,
2320 !s->root_directory_start_only,
2328 service_set_state(s, SERVICE_RELOAD);
2330 service_enter_running(s, SERVICE_SUCCESS);
2335 log_warning_unit(UNIT(s)->id,
2336 "%s failed to run 'reload' task: %s",
2337 UNIT(s)->id, strerror(-r));
2338 s->reload_result = SERVICE_FAILURE_RESOURCES;
2339 service_enter_running(s, SERVICE_SUCCESS);
2342 static void service_run_next_control(Service *s) {
2346 assert(s->control_command);
2347 assert(s->control_command->command_next);
2349 assert(s->control_command_id != SERVICE_EXEC_START);
2351 s->control_command = s->control_command->command_next;
2352 service_unwatch_control_pid(s);
2354 r = service_spawn(s,
2358 !s->permissions_start_only,
2359 !s->root_directory_start_only,
2360 s->control_command_id == SERVICE_EXEC_START_PRE ||
2361 s->control_command_id == SERVICE_EXEC_STOP_POST,
2371 log_warning_unit(UNIT(s)->id,
2372 "%s failed to run next control task: %s",
2373 UNIT(s)->id, strerror(-r));
2375 if (s->state == SERVICE_START_PRE)
2376 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2377 else if (s->state == SERVICE_STOP)
2378 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2379 else if (s->state == SERVICE_STOP_POST)
2380 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2381 else if (s->state == SERVICE_RELOAD) {
2382 s->reload_result = SERVICE_FAILURE_RESOURCES;
2383 service_enter_running(s, SERVICE_SUCCESS);
2385 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2388 static void service_run_next_main(Service *s) {
2393 assert(s->main_command);
2394 assert(s->main_command->command_next);
2395 assert(s->type == SERVICE_ONESHOT);
2397 s->main_command = s->main_command->command_next;
2398 service_unwatch_main_pid(s);
2400 r = service_spawn(s,
2407 s->notify_access != NOTIFY_NONE,
2413 service_set_main_pid(s, pid);
2418 log_warning_unit(UNIT(s)->id,
2419 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2420 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2423 static int service_start_limit_test(Service *s) {
2426 if (ratelimit_test(&s->start_limit))
2429 switch (s->start_limit_action) {
2431 case SERVICE_START_LIMIT_NONE:
2432 log_warning_unit(UNIT(s)->id,
2433 "%s start request repeated too quickly, refusing to start.",
2437 case SERVICE_START_LIMIT_REBOOT: {
2441 dbus_error_init(&error);
2443 log_warning_unit(UNIT(s)->id,
2444 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2446 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
2447 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
2448 true, &error, NULL);
2450 log_error_unit(UNIT(s)->id,
2451 "Failed to reboot: %s.", bus_error(&error, r));
2452 dbus_error_free(&error);
2458 case SERVICE_START_LIMIT_REBOOT_FORCE:
2459 log_warning_unit(UNIT(s)->id,
2460 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2461 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2464 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2465 log_warning_unit(UNIT(s)->id,
2466 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2468 reboot(RB_AUTOBOOT);
2472 log_error_unit(UNIT(s)->id,
2473 "start limit action=%i", s->start_limit_action);
2474 assert_not_reached("Unknown StartLimitAction.");
2480 static int service_start(Unit *u) {
2481 Service *s = SERVICE(u);
2486 /* We cannot fulfill this request right now, try again later
2488 if (s->state == SERVICE_STOP ||
2489 s->state == SERVICE_STOP_SIGTERM ||
2490 s->state == SERVICE_STOP_SIGKILL ||
2491 s->state == SERVICE_STOP_POST ||
2492 s->state == SERVICE_FINAL_SIGTERM ||
2493 s->state == SERVICE_FINAL_SIGKILL)
2496 /* Already on it! */
2497 if (s->state == SERVICE_START_PRE ||
2498 s->state == SERVICE_START ||
2499 s->state == SERVICE_START_POST)
2502 /* A service that will be restarted must be stopped first to
2503 * trigger BindsTo and/or OnFailure dependencies. If a user
2504 * does not want to wait for the holdoff time to elapse, the
2505 * service should be manually restarted, not started. We
2506 * simply return EAGAIN here, so that any start jobs stay
2507 * queued, and assume that the auto restart timer will
2508 * eventually trigger the restart. */
2509 if (s->state == SERVICE_AUTO_RESTART)
2512 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2514 /* Make sure we don't enter a busy loop of some kind. */
2515 r = service_start_limit_test(s);
2517 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2521 s->result = SERVICE_SUCCESS;
2522 s->reload_result = SERVICE_SUCCESS;
2523 s->main_pid_known = false;
2524 s->main_pid_alien = false;
2525 s->forbid_restart = false;
2527 service_enter_start_pre(s);
2531 static int service_stop(Unit *u) {
2532 Service *s = SERVICE(u);
2536 /* Don't create restart jobs from here. */
2537 s->forbid_restart = true;
2540 if (s->state == SERVICE_STOP ||
2541 s->state == SERVICE_STOP_SIGTERM ||
2542 s->state == SERVICE_STOP_SIGKILL ||
2543 s->state == SERVICE_STOP_POST ||
2544 s->state == SERVICE_FINAL_SIGTERM ||
2545 s->state == SERVICE_FINAL_SIGKILL)
2548 /* A restart will be scheduled or is in progress. */
2549 if (s->state == SERVICE_AUTO_RESTART) {
2550 service_set_state(s, SERVICE_DEAD);
2554 /* If there's already something running we go directly into
2556 if (s->state == SERVICE_START_PRE ||
2557 s->state == SERVICE_START ||
2558 s->state == SERVICE_START_POST ||
2559 s->state == SERVICE_RELOAD) {
2560 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2564 assert(s->state == SERVICE_RUNNING ||
2565 s->state == SERVICE_EXITED);
2567 service_enter_stop(s, SERVICE_SUCCESS);
2571 static int service_reload(Unit *u) {
2572 Service *s = SERVICE(u);
2576 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2578 service_enter_reload(s);
2582 _pure_ static bool service_can_reload(Unit *u) {
2583 Service *s = SERVICE(u);
2587 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2590 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2591 Service *s = SERVICE(u);
2597 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2598 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2599 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2601 if (s->control_pid > 0)
2602 unit_serialize_item_format(u, f, "control-pid", "%lu",
2603 (unsigned long) s->control_pid);
2605 if (s->main_pid_known && s->main_pid > 0)
2606 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2608 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2611 unit_serialize_item(u, f, "status-text", s->status_text);
2613 /* FIXME: There's a minor uncleanliness here: if there are
2614 * multiple commands attached here, we will start from the
2615 * first one again */
2616 if (s->control_command_id >= 0)
2617 unit_serialize_item(u, f, "control-command",
2618 service_exec_command_to_string(s->control_command_id));
2620 if (s->socket_fd >= 0) {
2623 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2626 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2629 if (s->main_exec_status.pid > 0) {
2630 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu",
2631 (unsigned long) s->main_exec_status.pid);
2632 dual_timestamp_serialize(f, "main-exec-status-start",
2633 &s->main_exec_status.start_timestamp);
2634 dual_timestamp_serialize(f, "main-exec-status-exit",
2635 &s->main_exec_status.exit_timestamp);
2637 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2638 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
2639 s->main_exec_status.code);
2640 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
2641 s->main_exec_status.status);
2644 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2645 dual_timestamp_serialize(f, "watchdog-timestamp",
2646 &s->watchdog_timestamp);
2648 if (s->exec_context.tmp_dir)
2649 unit_serialize_item(u, f, "tmp-dir", s->exec_context.tmp_dir);
2651 if (s->exec_context.var_tmp_dir)
2652 unit_serialize_item(u, f, "var-tmp-dir", s->exec_context.var_tmp_dir);
2657 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2658 Service *s = SERVICE(u);
2665 if (streq(key, "state")) {
2668 state = service_state_from_string(value);
2670 log_debug_unit(u->id, "Failed to parse state value %s", value);
2672 s->deserialized_state = state;
2673 } else if (streq(key, "result")) {
2676 f = service_result_from_string(value);
2678 log_debug_unit(u->id, "Failed to parse result value %s", value);
2679 else if (f != SERVICE_SUCCESS)
2682 } else if (streq(key, "reload-result")) {
2685 f = service_result_from_string(value);
2687 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2688 else if (f != SERVICE_SUCCESS)
2689 s->reload_result = f;
2691 } else if (streq(key, "control-pid")) {
2694 if (parse_pid(value, &pid) < 0)
2695 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2697 s->control_pid = pid;
2698 } else if (streq(key, "main-pid")) {
2701 if (parse_pid(value, &pid) < 0)
2702 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2704 service_set_main_pid(s, pid);
2705 unit_watch_pid(UNIT(s), pid);
2707 } else if (streq(key, "main-pid-known")) {
2710 b = parse_boolean(value);
2712 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2714 s->main_pid_known = b;
2715 } else if (streq(key, "status-text")) {
2722 free(s->status_text);
2726 } else if (streq(key, "control-command")) {
2727 ServiceExecCommand id;
2729 id = service_exec_command_from_string(value);
2731 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2733 s->control_command_id = id;
2734 s->control_command = s->exec_command[id];
2736 } else if (streq(key, "socket-fd")) {
2739 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2740 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2743 if (s->socket_fd >= 0)
2744 close_nointr_nofail(s->socket_fd);
2745 s->socket_fd = fdset_remove(fds, fd);
2747 } else if (streq(key, "main-exec-status-pid")) {
2750 if (parse_pid(value, &pid) < 0)
2751 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2753 s->main_exec_status.pid = pid;
2754 } else if (streq(key, "main-exec-status-code")) {
2757 if (safe_atoi(value, &i) < 0)
2758 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2760 s->main_exec_status.code = i;
2761 } else if (streq(key, "main-exec-status-status")) {
2764 if (safe_atoi(value, &i) < 0)
2765 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2767 s->main_exec_status.status = i;
2768 } else if (streq(key, "main-exec-status-start"))
2769 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2770 else if (streq(key, "main-exec-status-exit"))
2771 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2772 else if (streq(key, "watchdog-timestamp"))
2773 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2774 else if (streq(key, "tmp-dir")) {
2781 s->exec_context.tmp_dir = t;
2782 } else if (streq(key, "var-tmp-dir")) {
2789 s->exec_context.var_tmp_dir = t;
2791 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2796 _pure_ static UnitActiveState service_active_state(Unit *u) {
2797 const UnitActiveState *table;
2801 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2803 return table[SERVICE(u)->state];
2806 static const char *service_sub_state_to_string(Unit *u) {
2809 return service_state_to_string(SERVICE(u)->state);
2812 static bool service_check_gc(Unit *u) {
2813 Service *s = SERVICE(u);
2817 /* Never clean up services that still have a process around,
2818 * even if the service is formally dead. */
2819 if (cgroup_good(s) > 0 ||
2820 main_pid_good(s) > 0 ||
2821 control_pid_good(s) > 0)
2824 #ifdef HAVE_SYSV_COMPAT
2832 _pure_ static bool service_check_snapshot(Unit *u) {
2833 Service *s = SERVICE(u);
2837 return !s->got_socket_fd;
2840 static int service_retry_pid_file(Service *s) {
2843 assert(s->pid_file);
2844 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2846 r = service_load_pid_file(s, false);
2850 service_unwatch_pid_file(s);
2852 service_enter_running(s, SERVICE_SUCCESS);
2856 static int service_watch_pid_file(Service *s) {
2859 log_debug_unit(UNIT(s)->id,
2860 "Setting watch for %s's PID file %s",
2861 UNIT(s)->id, s->pid_file_pathspec->path);
2862 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2866 /* the pidfile might have appeared just before we set the watch */
2867 log_debug_unit(UNIT(s)->id,
2868 "Trying to read %s's PID file %s in case it changed",
2869 UNIT(s)->id, s->pid_file_pathspec->path);
2870 service_retry_pid_file(s);
2874 log_error_unit(UNIT(s)->id,
2875 "Failed to set a watch for %s's PID file %s: %s",
2876 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2877 service_unwatch_pid_file(s);
2881 static int service_demand_pid_file(Service *s) {
2884 assert(s->pid_file);
2885 assert(!s->pid_file_pathspec);
2887 ps = new0(PathSpec, 1);
2891 ps->path = strdup(s->pid_file);
2897 path_kill_slashes(ps->path);
2899 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2900 * keep their PID file open all the time. */
2901 ps->type = PATH_MODIFIED;
2902 ps->inotify_fd = -1;
2904 s->pid_file_pathspec = ps;
2906 return service_watch_pid_file(s);
2909 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2910 Service *s = SERVICE(u);
2914 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2915 assert(s->pid_file_pathspec);
2916 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2918 log_debug_unit(u->id, "inotify event for %s", u->id);
2920 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2923 if (service_retry_pid_file(s) == 0)
2926 if (service_watch_pid_file(s) < 0)
2931 service_unwatch_pid_file(s);
2932 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2935 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2936 Service *s = SERVICE(u);
2942 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2943 is_clean_exit_lsb(code, status, &s->success_status))
2944 f = SERVICE_SUCCESS;
2945 else if (code == CLD_EXITED)
2946 f = SERVICE_FAILURE_EXIT_CODE;
2947 else if (code == CLD_KILLED)
2948 f = SERVICE_FAILURE_SIGNAL;
2949 else if (code == CLD_DUMPED)
2950 f = SERVICE_FAILURE_CORE_DUMP;
2952 assert_not_reached("Unknown code");
2954 if (s->main_pid == pid) {
2955 /* Forking services may occasionally move to a new PID.
2956 * As long as they update the PID file before exiting the old
2957 * PID, they're fine. */
2958 if (service_load_pid_file(s, false) == 0)
2962 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2964 if (s->main_command) {
2965 /* If this is not a forking service than the
2966 * main process got started and hence we copy
2967 * the exit status so that it is recorded both
2968 * as main and as control process exit
2971 s->main_command->exec_status = s->main_exec_status;
2973 if (s->main_command->ignore)
2974 f = SERVICE_SUCCESS;
2975 } else if (s->exec_command[SERVICE_EXEC_START]) {
2977 /* If this is a forked process, then we should
2978 * ignore the return value if this was
2979 * configured for the starter process */
2981 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2982 f = SERVICE_SUCCESS;
2985 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2987 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2988 u->id, sigchld_code_to_string(code), status,
2989 strna(code == CLD_EXITED
2990 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2991 : signal_to_string(status)),
2992 "EXIT_CODE=%s", sigchld_code_to_string(code),
2993 "EXIT_STATUS=%i", status,
2996 if (f != SERVICE_SUCCESS)
2999 if (s->main_command &&
3000 s->main_command->command_next &&
3001 f == SERVICE_SUCCESS) {
3003 /* There is another command to *
3004 * execute, so let's do that. */
3006 log_debug_unit(u->id,
3007 "%s running next main command for state %s",
3008 u->id, service_state_to_string(s->state));
3009 service_run_next_main(s);
3013 /* The service exited, so the service is officially
3015 s->main_command = NULL;
3019 case SERVICE_START_POST:
3020 case SERVICE_RELOAD:
3022 /* Need to wait until the operation is
3027 if (s->type == SERVICE_ONESHOT) {
3028 /* This was our main goal, so let's go on */
3029 if (f == SERVICE_SUCCESS)
3030 service_enter_start_post(s);
3032 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3038 case SERVICE_RUNNING:
3039 service_enter_running(s, f);
3042 case SERVICE_STOP_SIGTERM:
3043 case SERVICE_STOP_SIGKILL:
3045 if (!control_pid_good(s))
3046 service_enter_stop_post(s, f);
3048 /* If there is still a control process, wait for that first */
3052 assert_not_reached("Uh, main process died at wrong time.");
3056 } else if (s->control_pid == pid) {
3059 if (s->control_command) {
3060 exec_status_exit(&s->control_command->exec_status,
3061 &s->exec_context, pid, code, status);
3063 if (s->control_command->ignore)
3064 f = SERVICE_SUCCESS;
3067 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
3068 "%s: control process exited, code=%s status=%i",
3069 u->id, sigchld_code_to_string(code), status);
3071 if (f != SERVICE_SUCCESS)
3074 /* Immediately get rid of the cgroup, so that the
3075 * kernel doesn't delay the cgroup empty messages for
3076 * the service cgroup any longer than necessary */
3077 service_kill_control_processes(s);
3079 if (s->control_command &&
3080 s->control_command->command_next &&
3081 f == SERVICE_SUCCESS) {
3083 /* There is another command to *
3084 * execute, so let's do that. */
3086 log_debug_unit(u->id,
3087 "%s running next control command for state %s",
3088 u->id, service_state_to_string(s->state));
3089 service_run_next_control(s);
3092 /* No further commands for this step, so let's
3093 * figure out what to do next */
3095 s->control_command = NULL;
3096 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3098 log_debug_unit(u->id,
3099 "%s got final SIGCHLD for state %s",
3100 u->id, service_state_to_string(s->state));
3104 case SERVICE_START_PRE:
3105 if (f == SERVICE_SUCCESS)
3106 service_enter_start(s);
3108 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3112 if (s->type != SERVICE_FORKING)
3113 /* Maybe spurious event due to a reload that changed the type? */
3116 if (f != SERVICE_SUCCESS) {
3117 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3122 bool has_start_post;
3125 /* Let's try to load the pid file here if we can.
3126 * The PID file might actually be created by a START_POST
3127 * script. In that case don't worry if the loading fails. */
3129 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3130 r = service_load_pid_file(s, !has_start_post);
3131 if (!has_start_post && r < 0) {
3132 r = service_demand_pid_file(s);
3133 if (r < 0 || !cgroup_good(s))
3134 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3138 service_search_main_pid(s);
3140 service_enter_start_post(s);
3143 case SERVICE_START_POST:
3144 if (f != SERVICE_SUCCESS) {
3145 service_enter_stop(s, f);
3152 r = service_load_pid_file(s, true);
3154 r = service_demand_pid_file(s);
3155 if (r < 0 || !cgroup_good(s))
3156 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3160 service_search_main_pid(s);
3162 service_enter_running(s, SERVICE_SUCCESS);
3165 case SERVICE_RELOAD:
3166 if (f == SERVICE_SUCCESS) {
3167 service_load_pid_file(s, true);
3168 service_search_main_pid(s);
3171 s->reload_result = f;
3172 service_enter_running(s, SERVICE_SUCCESS);
3176 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3179 case SERVICE_STOP_SIGTERM:
3180 case SERVICE_STOP_SIGKILL:
3181 if (main_pid_good(s) <= 0)
3182 service_enter_stop_post(s, f);
3184 /* If there is still a service
3185 * process around, wait until
3186 * that one quit, too */
3189 case SERVICE_STOP_POST:
3190 case SERVICE_FINAL_SIGTERM:
3191 case SERVICE_FINAL_SIGKILL:
3192 service_enter_dead(s, f, true);
3196 assert_not_reached("Uh, control process died at wrong time.");
3201 /* Notify clients about changed exit status */
3202 unit_add_to_dbus_queue(u);
3205 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3206 Service *s = SERVICE(u);
3209 assert(elapsed == 1);
3211 if (w == &s->watchdog_watch) {
3212 service_handle_watchdog(s);
3216 assert(w == &s->timer_watch);
3220 case SERVICE_START_PRE:
3222 log_warning_unit(u->id,
3223 "%s operation timed out. Terminating.", u->id);
3224 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3227 case SERVICE_START_POST:
3228 log_warning_unit(u->id,
3229 "%s operation timed out. Stopping.", u->id);
3230 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3233 case SERVICE_RELOAD:
3234 log_warning_unit(u->id,
3235 "%s operation timed out. Stopping.", u->id);
3236 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3237 service_enter_running(s, SERVICE_SUCCESS);
3241 log_warning_unit(u->id,
3242 "%s stopping timed out. Terminating.", u->id);
3243 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3246 case SERVICE_STOP_SIGTERM:
3247 if (s->kill_context.send_sigkill) {
3248 log_warning_unit(u->id,
3249 "%s stopping timed out. Killing.", u->id);
3250 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3252 log_warning_unit(u->id,
3253 "%s stopping timed out. Skipping SIGKILL.", u->id);
3254 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3259 case SERVICE_STOP_SIGKILL:
3260 /* Uh, we sent a SIGKILL and it is still not gone?
3261 * Must be something we cannot kill, so let's just be
3262 * weirded out and continue */
3264 log_warning_unit(u->id,
3265 "%s still around after SIGKILL. Ignoring.", u->id);
3266 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3269 case SERVICE_STOP_POST:
3270 log_warning_unit(u->id,
3271 "%s stopping timed out (2). Terminating.", u->id);
3272 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3275 case SERVICE_FINAL_SIGTERM:
3276 if (s->kill_context.send_sigkill) {
3277 log_warning_unit(u->id,
3278 "%s stopping timed out (2). Killing.", u->id);
3279 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3281 log_warning_unit(u->id,
3282 "%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.",
3284 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3289 case SERVICE_FINAL_SIGKILL:
3290 log_warning_unit(u->id,
3291 "%s still around after SIGKILL (2). Entering failed mode.", u->id);
3292 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3295 case SERVICE_AUTO_RESTART:
3296 log_info_unit(u->id,
3297 "%s holdoff time over, scheduling restart.", u->id);
3298 service_enter_restart(s);
3302 assert_not_reached("Timeout at wrong time.");
3306 static void service_notify_cgroup_empty_event(Unit *u) {
3307 Service *s = SERVICE(u);
3311 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
3315 /* Waiting for SIGCHLD is usually more interesting,
3316 * because it includes return codes/signals. Which is
3317 * why we ignore the cgroup events for most cases,
3318 * except when we don't know pid which to expect the
3322 case SERVICE_START_POST:
3323 /* If we were hoping for the daemon to write its PID file,
3324 * we can give up now. */
3325 if (s->pid_file_pathspec) {
3326 log_warning_unit(u->id,
3327 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3328 service_unwatch_pid_file(s);
3329 if (s->state == SERVICE_START)
3330 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3332 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3336 case SERVICE_RUNNING:
3337 /* service_enter_running() will figure out what to do */
3338 service_enter_running(s, SERVICE_SUCCESS);
3341 case SERVICE_STOP_SIGTERM:
3342 case SERVICE_STOP_SIGKILL:
3344 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3345 service_enter_stop_post(s, SERVICE_SUCCESS);
3349 case SERVICE_FINAL_SIGTERM:
3350 case SERVICE_FINAL_SIGKILL:
3351 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3352 service_enter_dead(s, SERVICE_SUCCESS, true);
3361 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3362 Service *s = SERVICE(u);
3367 if (s->notify_access == NOTIFY_NONE) {
3368 log_warning_unit(u->id,
3369 "%s: Got notification message from PID %lu, but reception is disabled.",
3370 u->id, (unsigned long) pid);
3374 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3375 log_warning_unit(u->id,
3376 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3377 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3381 log_debug_unit(u->id,
3382 "%s: Got message", u->id);
3384 /* Interpret MAINPID= */
3385 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3386 (s->state == SERVICE_START ||
3387 s->state == SERVICE_START_POST ||
3388 s->state == SERVICE_RUNNING ||
3389 s->state == SERVICE_RELOAD)) {
3391 if (parse_pid(e + 8, &pid) < 0)
3392 log_warning_unit(u->id,
3393 "Failed to parse notification message %s", e);
3395 log_debug_unit(u->id,
3396 "%s: got %s", u->id, e);
3397 service_set_main_pid(s, pid);
3398 unit_watch_pid(UNIT(s), pid);
3402 /* Interpret READY= */
3403 if (s->type == SERVICE_NOTIFY &&
3404 s->state == SERVICE_START &&
3405 strv_find(tags, "READY=1")) {
3406 log_debug_unit(u->id,
3407 "%s: got READY=1", u->id);
3409 service_enter_start_post(s);
3412 /* Interpret STATUS= */
3413 e = strv_find_prefix(tags, "STATUS=");
3419 if (!utf8_is_valid(e+7)) {
3420 log_warning_unit(u->id,
3421 "Status message in notification is not UTF-8 clean.");
3427 log_error_unit(u->id,
3428 "Failed to allocate string.");
3432 log_debug_unit(u->id,
3433 "%s: got %s", u->id, e);
3435 free(s->status_text);
3438 free(s->status_text);
3439 s->status_text = NULL;
3443 if (strv_find(tags, "WATCHDOG=1")) {
3444 log_debug_unit(u->id,
3445 "%s: got WATCHDOG=1", u->id);
3446 if (dual_timestamp_is_set(&s->watchdog_timestamp))
3447 service_reset_watchdog(s);
3450 /* Notify clients about changed status or main pid */
3451 unit_add_to_dbus_queue(u);
3454 #ifdef HAVE_SYSV_COMPAT
3456 static int service_enumerate(Manager *m) {
3459 _cleanup_closedir_ DIR *d = NULL;
3460 _cleanup_free_ char *path = NULL, *fpath = NULL, *name = NULL;
3461 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
3462 _cleanup_set_free_ Set *shutdown_services = NULL;
3469 if (m->running_as != SYSTEMD_SYSTEM)
3472 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3473 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3477 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3488 if (errno != ENOENT)
3489 log_warning("opendir(%s) failed: %s", path, strerror(errno));
3494 while ((de = readdir(d))) {
3497 if (ignore_file(de->d_name))
3500 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3503 if (strlen(de->d_name) < 4)
3506 a = undecchar(de->d_name[1]);
3507 b = undecchar(de->d_name[2]);
3513 fpath = strjoin(path, "/", de->d_name, NULL);
3519 if (access(fpath, X_OK) < 0) {
3521 if (errno != ENOENT)
3522 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3528 name = sysv_translate_name(de->d_name + 3);
3534 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3536 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3540 if (de->d_name[0] == 'S') {
3542 if (rcnd_table[i].type == RUNLEVEL_UP) {
3543 SERVICE(service)->sysv_start_priority_from_rcnd =
3544 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3546 SERVICE(service)->sysv_enabled = true;
3549 r = set_ensure_allocated(&runlevel_services[i],
3550 trivial_hash_func, trivial_compare_func);
3554 r = set_put(runlevel_services[i], service);
3558 } else if (de->d_name[0] == 'K' &&
3559 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3561 r = set_ensure_allocated(&shutdown_services,
3562 trivial_hash_func, trivial_compare_func);
3566 r = set_put(shutdown_services, service);
3573 /* Now we loaded all stubs and are aware of the lowest
3574 start-up priority for all services, not let's actually load
3575 the services, this will also tell us which services are
3576 actually native now */
3577 manager_dispatch_load_queue(m);
3579 /* If this is a native service, rely on native ways to pull in
3580 * a service, don't pull it in via sysv rcN.d links. */
3581 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3582 SET_FOREACH(service, runlevel_services[i], j) {
3583 service = unit_follow_merge(service);
3585 if (service->fragment_path)
3588 r = unit_add_two_dependencies_by_name_inverse(
3589 service, UNIT_AFTER, UNIT_WANTS,
3590 rcnd_table[i].target, NULL, true);
3595 /* We honour K links only for halt/reboot. For the normal
3596 * runlevels we assume the stop jobs will be implicitly added
3597 * by the core logic. Also, we don't really distinguish here
3598 * between the runlevels 0 and 6 and just add them to the
3599 * special shutdown target. */
3600 SET_FOREACH(service, shutdown_services, j) {
3601 service = unit_follow_merge(service);
3603 if (service->fragment_path)
3606 r = unit_add_two_dependencies_by_name(
3607 service, UNIT_BEFORE, UNIT_CONFLICTS,
3608 SPECIAL_SHUTDOWN_TARGET, NULL, true);
3617 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3618 set_free(runlevel_services[i]);
3624 static void service_bus_name_owner_change(
3627 const char *old_owner,
3628 const char *new_owner) {
3630 Service *s = SERVICE(u);
3635 assert(streq(s->bus_name, name));
3636 assert(old_owner || new_owner);
3638 if (old_owner && new_owner)
3639 log_debug_unit(u->id,
3640 "%s's D-Bus name %s changed owner from %s to %s",
3641 u->id, name, old_owner, new_owner);
3643 log_debug_unit(u->id,
3644 "%s's D-Bus name %s no longer registered by %s",
3645 u->id, name, old_owner);
3647 log_debug_unit(u->id,
3648 "%s's D-Bus name %s now registered by %s",
3649 u->id, name, new_owner);
3651 s->bus_name_good = !!new_owner;
3653 if (s->type == SERVICE_DBUS) {
3655 /* service_enter_running() will figure out what to
3657 if (s->state == SERVICE_RUNNING)
3658 service_enter_running(s, SERVICE_SUCCESS);
3659 else if (s->state == SERVICE_START && new_owner)
3660 service_enter_start_post(s);
3662 } else if (new_owner &&
3664 (s->state == SERVICE_START ||
3665 s->state == SERVICE_START_POST ||
3666 s->state == SERVICE_RUNNING ||
3667 s->state == SERVICE_RELOAD)) {
3669 /* Try to acquire PID from bus service */
3670 log_debug_unit(u->id,
3671 "Trying to acquire PID from D-Bus name...");
3673 bus_query_pid(u->manager, name);
3677 static void service_bus_query_pid_done(
3682 Service *s = SERVICE(u);
3687 log_debug_unit(u->id,
3688 "%s's D-Bus name %s is now owned by process %u",
3689 u->id, name, (unsigned) pid);
3691 if (s->main_pid <= 0 &&
3692 (s->state == SERVICE_START ||
3693 s->state == SERVICE_START_POST ||
3694 s->state == SERVICE_RUNNING ||
3695 s->state == SERVICE_RELOAD)){
3696 service_set_main_pid(s, pid);
3697 unit_watch_pid(UNIT(s), pid);
3701 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3706 /* This is called by the socket code when instantiating a new
3707 * service for a stream socket and the socket needs to be
3710 if (UNIT(s)->load_state != UNIT_LOADED)
3713 if (s->socket_fd >= 0)
3716 if (s->state != SERVICE_DEAD)
3720 s->got_socket_fd = true;
3722 unit_ref_set(&s->accept_socket, UNIT(sock));
3724 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3727 static void service_reset_failed(Unit *u) {
3728 Service *s = SERVICE(u);
3732 if (s->state == SERVICE_FAILED)
3733 service_set_state(s, SERVICE_DEAD);
3735 s->result = SERVICE_SUCCESS;
3736 s->reload_result = SERVICE_SUCCESS;
3738 RATELIMIT_RESET(s->start_limit);
3741 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3742 Service *s = SERVICE(u);
3744 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
3747 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3748 [SERVICE_DEAD] = "dead",
3749 [SERVICE_START_PRE] = "start-pre",
3750 [SERVICE_START] = "start",
3751 [SERVICE_START_POST] = "start-post",
3752 [SERVICE_RUNNING] = "running",
3753 [SERVICE_EXITED] = "exited",
3754 [SERVICE_RELOAD] = "reload",
3755 [SERVICE_STOP] = "stop",
3756 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3757 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3758 [SERVICE_STOP_POST] = "stop-post",
3759 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3760 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3761 [SERVICE_FAILED] = "failed",
3762 [SERVICE_AUTO_RESTART] = "auto-restart",
3765 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3767 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3768 [SERVICE_RESTART_NO] = "no",
3769 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3770 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3771 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
3772 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3773 [SERVICE_RESTART_ALWAYS] = "always"
3776 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3778 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3779 [SERVICE_SIMPLE] = "simple",
3780 [SERVICE_FORKING] = "forking",
3781 [SERVICE_ONESHOT] = "oneshot",
3782 [SERVICE_DBUS] = "dbus",
3783 [SERVICE_NOTIFY] = "notify",
3784 [SERVICE_IDLE] = "idle"
3787 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3789 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3790 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3791 [SERVICE_EXEC_START] = "ExecStart",
3792 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3793 [SERVICE_EXEC_RELOAD] = "ExecReload",
3794 [SERVICE_EXEC_STOP] = "ExecStop",
3795 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3798 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3800 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3801 [NOTIFY_NONE] = "none",
3802 [NOTIFY_MAIN] = "main",
3803 [NOTIFY_ALL] = "all"
3806 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3808 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3809 [SERVICE_SUCCESS] = "success",
3810 [SERVICE_FAILURE_RESOURCES] = "resources",
3811 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3812 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3813 [SERVICE_FAILURE_SIGNAL] = "signal",
3814 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3815 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3816 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3819 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3821 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3822 [SERVICE_START_LIMIT_NONE] = "none",
3823 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3824 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3825 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3827 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3829 const UnitVTable service_vtable = {
3830 .object_size = sizeof(Service),
3837 .private_section = "Service",
3838 .exec_context_offset = offsetof(Service, exec_context),
3839 .cgroup_context_offset = offsetof(Service, cgroup_context),
3841 .init = service_init,
3842 .done = service_done,
3843 .load = service_load,
3845 .coldplug = service_coldplug,
3847 .dump = service_dump,
3849 .start = service_start,
3850 .stop = service_stop,
3851 .reload = service_reload,
3853 .can_reload = service_can_reload,
3855 .kill = service_kill,
3857 .serialize = service_serialize,
3858 .deserialize_item = service_deserialize_item,
3860 .active_state = service_active_state,
3861 .sub_state_to_string = service_sub_state_to_string,
3863 .check_gc = service_check_gc,
3864 .check_snapshot = service_check_snapshot,
3866 .sigchld_event = service_sigchld_event,
3867 .timer_event = service_timer_event,
3868 .fd_event = service_fd_event,
3870 .reset_failed = service_reset_failed,
3872 .notify_cgroup_empty = service_notify_cgroup_empty_event,
3873 .notify_message = service_notify_message,
3875 .bus_name_owner_change = service_bus_name_owner_change,
3876 .bus_query_pid_done = service_bus_query_pid_done,
3878 .bus_interface = "org.freedesktop.systemd1.Service",
3879 .bus_message_handler = bus_service_message_handler,
3880 .bus_invalidating_properties = bus_service_invalidating_properties,
3881 .bus_set_property = bus_service_set_property,
3882 .bus_commit_properties = bus_service_commit_properties,
3884 .can_transient = true,
3886 #ifdef HAVE_SYSV_COMPAT
3887 .enumerate = service_enumerate,
3889 .status_message_formats = {
3890 .starting_stopping = {
3891 [0] = "Starting %s...",
3892 [1] = "Stopping %s...",
3894 .finished_start_job = {
3895 [JOB_DONE] = "Started %s.",
3896 [JOB_FAILED] = "Failed to start %s.",
3897 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3898 [JOB_TIMEOUT] = "Timed out starting %s.",
3900 .finished_stop_job = {
3901 [JOB_DONE] = "Stopped %s.",
3902 [JOB_FAILED] = "Stopped (with error) %s.",
3903 [JOB_TIMEOUT] = "Timed out stopping %s.",