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 = u->manager->default_timeout_start_usec;
128 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
129 s->restart_usec = u->manager->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,
147 u->manager->default_start_limit_interval,
148 u->manager->default_start_limit_burst);
150 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
153 static void service_unwatch_control_pid(Service *s) {
156 if (s->control_pid <= 0)
159 unit_unwatch_pid(UNIT(s), s->control_pid);
163 static void service_unwatch_main_pid(Service *s) {
166 if (s->main_pid <= 0)
169 unit_unwatch_pid(UNIT(s), s->main_pid);
173 static void service_unwatch_pid_file(Service *s) {
174 if (!s->pid_file_pathspec)
177 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
178 UNIT(s)->id, s->pid_file_pathspec->path);
179 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
180 path_spec_done(s->pid_file_pathspec);
181 free(s->pid_file_pathspec);
182 s->pid_file_pathspec = NULL;
185 static int service_set_main_pid(Service *s, pid_t pid) {
196 if (s->main_pid == pid && s->main_pid_known)
199 if (s->main_pid != pid) {
200 service_unwatch_main_pid(s);
201 exec_status_start(&s->main_exec_status, pid);
205 s->main_pid_known = true;
207 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
208 log_warning_unit(UNIT(s)->id,
209 "%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
210 UNIT(s)->id, (unsigned long) pid);
212 s->main_pid_alien = true;
214 s->main_pid_alien = false;
219 static void service_close_socket_fd(Service *s) {
222 if (s->socket_fd < 0)
225 close_nointr_nofail(s->socket_fd);
229 static void service_connection_unref(Service *s) {
232 if (!UNIT_ISSET(s->accept_socket))
235 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
236 unit_ref_unset(&s->accept_socket);
239 static void service_stop_watchdog(Service *s) {
242 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
243 s->watchdog_timestamp.realtime = 0;
244 s->watchdog_timestamp.monotonic = 0;
247 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
249 static void service_handle_watchdog(Service *s) {
255 if (s->watchdog_usec == 0)
258 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
259 if (offset >= s->watchdog_usec) {
260 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
261 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_WATCHDOG);
265 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->watchdog_usec - offset, &s->watchdog_watch);
267 log_warning_unit(UNIT(s)->id,
268 "%s failed to install watchdog timer: %s",
269 UNIT(s)->id, strerror(-r));
272 static void service_reset_watchdog(Service *s) {
275 dual_timestamp_get(&s->watchdog_timestamp);
276 service_handle_watchdog(s);
279 static void service_done(Unit *u) {
280 Service *s = SERVICE(u);
287 #ifdef HAVE_SYSV_COMPAT
288 free(s->sysv_runlevels);
289 s->sysv_runlevels = NULL;
292 free(s->status_text);
293 s->status_text = NULL;
295 cgroup_context_done(&s->cgroup_context);
296 exec_context_done(&s->exec_context, manager_is_reloading_or_reexecuting(u->manager));
297 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
298 s->control_command = NULL;
299 s->main_command = NULL;
301 set_free(s->restart_ignore_status.code);
302 s->restart_ignore_status.code = NULL;
303 set_free(s->restart_ignore_status.signal);
304 s->restart_ignore_status.signal = NULL;
306 set_free(s->success_status.code);
307 s->success_status.code = NULL;
308 set_free(s->success_status.signal);
309 s->success_status.signal = NULL;
311 /* This will leak a process, but at least no memory or any of
313 service_unwatch_main_pid(s);
314 service_unwatch_control_pid(s);
315 service_unwatch_pid_file(s);
318 unit_unwatch_bus_name(u, s->bus_name);
323 service_close_socket_fd(s);
324 service_connection_unref(s);
326 unit_ref_unset(&s->accept_socket);
328 service_stop_watchdog(s);
330 unit_unwatch_timer(u, &s->timer_watch);
333 #ifdef HAVE_SYSV_COMPAT
334 static char *sysv_translate_name(const char *name) {
337 r = new(char, strlen(name) + sizeof(".service"));
341 if (endswith(name, ".sh"))
342 /* Drop .sh suffix */
343 strcpy(stpcpy(r, name) - 3, ".service");
345 /* Normal init script name */
346 strcpy(stpcpy(r, name), ".service");
351 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
353 /* We silently ignore the $ prefix here. According to the LSB
354 * spec it simply indicates whether something is a
355 * standardized name or a distribution-specific one. Since we
356 * just follow what already exists and do not introduce new
357 * uses or names we don't care who introduced a new name. */
359 static const char * const table[] = {
360 /* LSB defined facilities */
362 "network", SPECIAL_NETWORK_TARGET,
363 "named", SPECIAL_NSS_LOOKUP_TARGET,
364 "portmap", SPECIAL_RPCBIND_TARGET,
365 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
367 "time", SPECIAL_TIME_SYNC_TARGET,
377 n = *name == '$' ? name + 1 : name;
379 for (i = 0; i < ELEMENTSOF(table); i += 2) {
381 if (!streq(table[i], n))
387 r = strdup(table[i+1]);
394 /* If we don't know this name, fallback heuristics to figure
395 * out whether something is a target or a service alias. */
398 if (!unit_prefix_is_valid(n))
401 /* Facilities starting with $ are most likely targets */
402 r = unit_name_build(n, NULL, ".target");
403 } else if (filename && streq(name, filename))
404 /* Names equaling the file name of the services are redundant */
407 /* Everything else we assume to be normal service names */
408 r = sysv_translate_name(n);
419 static int sysv_fix_order(Service *s) {
425 if (s->sysv_start_priority < 0)
428 /* For each pair of services where at least one lacks a LSB
429 * header, we use the start priority value to order things. */
431 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
434 bool special_s, special_t;
441 if (UNIT(t)->load_state != UNIT_LOADED)
444 if (t->sysv_start_priority < 0)
447 /* If both units have modern headers we don't care
448 * about the priorities */
449 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
450 (UNIT(t)->fragment_path || t->sysv_has_lsb))
453 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
454 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
456 if (special_t && !special_s)
458 else if (special_s && !special_t)
460 else if (t->sysv_start_priority < s->sysv_start_priority)
462 else if (t->sysv_start_priority > s->sysv_start_priority)
467 /* FIXME: Maybe we should compare the name here lexicographically? */
469 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
476 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
479 if (!(c = new0(ExecCommand, 1)))
482 if (!(c->path = strdup(path))) {
487 if (!(c->argv = strv_new(path, arg1, NULL))) {
496 static int sysv_exec_commands(Service *s, const bool supports_reload) {
501 assert(UNIT(s)->source_path);
503 c = exec_command_new(UNIT(s)->source_path, "start");
506 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
508 c = exec_command_new(UNIT(s)->source_path, "stop");
511 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
513 if (supports_reload) {
514 c = exec_command_new(UNIT(s)->source_path, "reload");
517 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
523 static bool usage_contains_reload(const char *line) {
524 return (strcasestr(line, "{reload|") ||
525 strcasestr(line, "{reload}") ||
526 strcasestr(line, "{reload\"") ||
527 strcasestr(line, "|reload|") ||
528 strcasestr(line, "|reload}") ||
529 strcasestr(line, "|reload\""));
532 static int service_load_sysv_path(Service *s, const char *path) {
544 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
546 bool supports_reload = false;
553 f = fopen(path, "re");
555 r = errno == ENOENT ? 0 : -errno;
559 if (fstat(fileno(f), &st) < 0) {
564 free(u->source_path);
565 u->source_path = strdup(path);
566 if (!u->source_path) {
570 u->source_mtime = timespec_load(&st.st_mtim);
572 if (null_or_empty(&st)) {
573 u->load_state = UNIT_MASKED;
581 char l[LINE_MAX], *t;
583 if (!fgets(l, sizeof(l), f)) {
588 log_error_unit(u->id,
589 "Failed to read configuration file '%s': %s",
598 /* Try to figure out whether this init script supports
599 * the reload operation. This heuristic looks for
600 * "Usage" lines which include the reload option. */
601 if ( state == USAGE_CONTINUATION ||
602 (state == NORMAL && strcasestr(t, "usage"))) {
603 if (usage_contains_reload(t)) {
604 supports_reload = true;
606 } else if (t[strlen(t)-1] == '\\')
607 state = USAGE_CONTINUATION;
615 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
617 s->sysv_has_lsb = true;
621 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
627 t += strspn(t, WHITESPACE);
629 if (state == NORMAL) {
631 /* Try to parse Red Hat style chkconfig headers */
633 if (startswith_no_case(t, "chkconfig:")) {
635 char runlevels[16], *k;
639 if (sscanf(t+10, "%15s %i %*i",
641 &start_priority) != 2) {
643 log_warning_unit(u->id,
644 "[%s:%u] Failed to parse chkconfig line. Ignoring.",
649 /* A start priority gathered from the
650 * symlink farms is preferred over the
651 * data from the LSB header. */
652 if (start_priority < 0 || start_priority > 99)
653 log_warning_unit(u->id,
654 "[%s:%u] Start priority out of range. Ignoring.",
657 s->sysv_start_priority = start_priority;
659 char_array_0(runlevels);
660 k = delete_chars(runlevels, WHITESPACE "-");
665 if (!(d = strdup(k))) {
670 free(s->sysv_runlevels);
671 s->sysv_runlevels = d;
674 } else if (startswith_no_case(t, "description:")) {
676 size_t k = strlen(t);
680 if (t[k-1] == '\\') {
685 if ((j = strstrip(t+12)) && *j) {
686 if (!(d = strdup(j))) {
693 free(chkconfig_description);
694 chkconfig_description = d;
696 } else if (startswith_no_case(t, "pidfile:")) {
703 if (!path_is_absolute(fn)) {
704 log_warning_unit(u->id,
705 "[%s:%u] PID file not absolute. Ignoring.",
710 if (!(fn = strdup(fn))) {
719 } else if (state == DESCRIPTION) {
721 /* Try to parse Red Hat style description
724 size_t k = strlen(t);
732 if ((j = strstrip(t)) && *j) {
735 if (chkconfig_description)
736 d = strjoin(chkconfig_description, " ", j, NULL);
745 free(chkconfig_description);
746 chkconfig_description = d;
749 } else if (state == LSB || state == LSB_DESCRIPTION) {
751 if (startswith_no_case(t, "Provides:")) {
757 FOREACH_WORD_QUOTED(w, z, t+9, i) {
760 if (!(n = strndup(w, z))) {
765 r = sysv_translate_facility(n, path_get_file_name(path), &m);
774 if (unit_name_to_type(m) == UNIT_SERVICE)
775 r = unit_merge_by_name(u, m);
782 * indication that the
784 * now available. This
787 * targets do NOT pull
790 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
793 log_error_unit(u->id,
794 "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
795 path, line, m, strerror(-r));
800 } else if (startswith_no_case(t, "Required-Start:") ||
801 startswith_no_case(t, "Should-Start:") ||
802 startswith_no_case(t, "X-Start-Before:") ||
803 startswith_no_case(t, "X-Start-After:")) {
809 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
812 if (!(n = strndup(w, z))) {
817 r = sysv_translate_facility(n, path_get_file_name(path), &m);
819 log_error_unit(u->id,
820 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
821 path, line, n, strerror(-r));
831 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
834 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
835 path, line, m, strerror(-r));
839 } else if (startswith_no_case(t, "Default-Start:")) {
844 k = delete_chars(t+14, WHITESPACE "-");
847 if (!(d = strdup(k))) {
852 free(s->sysv_runlevels);
853 s->sysv_runlevels = d;
856 } else if (startswith_no_case(t, "Description:")) {
859 state = LSB_DESCRIPTION;
861 if ((j = strstrip(t+12)) && *j) {
862 if (!(d = strdup(j))) {
869 free(long_description);
870 long_description = d;
872 } else if (startswith_no_case(t, "Short-Description:")) {
877 if ((j = strstrip(t+18)) && *j) {
878 if (!(d = strdup(j))) {
885 free(short_description);
886 short_description = d;
888 } else if (state == LSB_DESCRIPTION) {
890 if (startswith(l, "#\t") || startswith(l, "# ")) {
893 if ((j = strstrip(t)) && *j) {
896 if (long_description)
897 d = strjoin(long_description, " ", t, NULL);
906 free(long_description);
907 long_description = d;
916 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
919 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
920 /* If there a runlevels configured for this service
921 * but none of the standard ones, then we assume this
922 * is some special kind of service (which might be
923 * needed for early boot) and don't create any links
926 UNIT(s)->default_dependencies = false;
928 /* Don't timeout special services during boot (like fsck) */
929 s->timeout_start_usec = 0;
930 s->timeout_stop_usec = 0;
932 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
933 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
936 /* Special setting for all SysV services */
937 s->type = SERVICE_FORKING;
938 s->remain_after_exit = !s->pid_file;
939 s->guess_main_pid = false;
940 s->restart = SERVICE_RESTART_NO;
941 s->exec_context.ignore_sigpipe = false;
942 s->kill_context.kill_mode = KILL_PROCESS;
944 /* We use the long description only if
945 * no short description is set. */
947 if (short_description)
948 description = short_description;
949 else if (chkconfig_description)
950 description = chkconfig_description;
951 else if (long_description)
952 description = long_description;
959 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
967 /* The priority that has been set in /etc/rcN.d/ hierarchies
968 * takes precedence over what is stored as default in the LSB
970 if (s->sysv_start_priority_from_rcnd >= 0)
971 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
973 u->load_state = UNIT_LOADED;
981 free(short_description);
982 free(long_description);
983 free(chkconfig_description);
988 static int service_load_sysv_name(Service *s, const char *name) {
994 /* For SysV services we strip the *.sh suffixes. */
995 if (endswith(name, ".sh.service"))
998 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1002 path = strjoin(*p, "/", name, NULL);
1006 assert(endswith(path, ".service"));
1007 path[strlen(path)-8] = 0;
1009 r = service_load_sysv_path(s, path);
1011 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1012 /* Try *.sh source'able init scripts */
1013 strcat(path, ".sh");
1014 r = service_load_sysv_path(s, path);
1021 if (UNIT(s)->load_state != UNIT_STUB)
1028 static int service_load_sysv(Service *s) {
1035 /* Load service data from SysV init scripts, preferably with
1036 * LSB headers ... */
1038 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1041 if ((t = UNIT(s)->id))
1042 if ((r = service_load_sysv_name(s, t)) < 0)
1045 if (UNIT(s)->load_state == UNIT_STUB)
1046 SET_FOREACH(t, UNIT(s)->names, i) {
1047 if (t == UNIT(s)->id)
1050 if ((r = service_load_sysv_name(s, t)) < 0)
1053 if (UNIT(s)->load_state != UNIT_STUB)
1061 static int service_verify(Service *s) {
1064 if (UNIT(s)->load_state != UNIT_LOADED)
1067 if (!s->exec_command[SERVICE_EXEC_START]) {
1068 log_error_unit(UNIT(s)->id,
1069 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1073 if (s->type != SERVICE_ONESHOT &&
1074 s->exec_command[SERVICE_EXEC_START]->command_next) {
1075 log_error_unit(UNIT(s)->id,
1076 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1080 if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
1081 log_error_unit(UNIT(s)->id,
1082 "%s has Restart setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1086 if (s->type == SERVICE_DBUS && !s->bus_name) {
1087 log_error_unit(UNIT(s)->id,
1088 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1092 if (s->bus_name && s->type != SERVICE_DBUS)
1093 log_warning_unit(UNIT(s)->id,
1094 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1096 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1097 log_error_unit(UNIT(s)->id,
1098 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1105 static int service_add_default_dependencies(Service *s) {
1110 /* Add a number of automatic dependencies useful for the
1111 * majority of services. */
1113 /* First, pull in base system */
1114 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1115 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1116 SPECIAL_BASIC_TARGET, NULL, true);
1120 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1121 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1122 SPECIAL_SOCKETS_TARGET, NULL, true);
1126 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1127 SPECIAL_TIMERS_TARGET, NULL, true);
1131 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
1132 SPECIAL_PATHS_TARGET, NULL, true);
1137 /* Second, activate normal shutdown */
1138 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
1139 SPECIAL_SHUTDOWN_TARGET, NULL, true);
1143 static void service_fix_output(Service *s) {
1146 /* If nothing has been explicitly configured, patch default
1147 * output in. If input is socket/tty we avoid this however,
1148 * since in that case we want output to default to the same
1149 * place as we read input from. */
1151 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1152 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1153 s->exec_context.std_input == EXEC_INPUT_NULL)
1154 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1156 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1157 s->exec_context.std_input == EXEC_INPUT_NULL)
1158 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1161 static int service_load(Unit *u) {
1163 Service *s = SERVICE(u);
1167 /* Load a .service file */
1168 r = unit_load_fragment(u);
1172 #ifdef HAVE_SYSV_COMPAT
1173 /* Load a classic init script as a fallback, if we couldn't find anything */
1174 if (u->load_state == UNIT_STUB) {
1175 r = service_load_sysv(s);
1181 /* Still nothing found? Then let's give up */
1182 if (u->load_state == UNIT_STUB)
1185 /* This is a new unit? Then let's add in some extras */
1186 if (u->load_state == UNIT_LOADED) {
1188 /* We were able to load something, then let's add in
1189 * the dropin directories. */
1190 r = unit_load_dropin(u);
1194 if (s->type == _SERVICE_TYPE_INVALID)
1195 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1197 /* Oneshot services have disabled start timeout by default */
1198 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1199 s->timeout_start_usec = 0;
1201 service_fix_output(s);
1203 r = unit_add_exec_dependencies(u, &s->exec_context);
1207 r = unit_add_default_slice(u);
1211 #ifdef HAVE_SYSV_COMPAT
1212 r = sysv_fix_order(s);
1218 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1221 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1222 s->notify_access = NOTIFY_MAIN;
1224 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1225 s->notify_access = NOTIFY_MAIN;
1227 if (s->type == SERVICE_DBUS || s->bus_name) {
1228 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES,
1229 SPECIAL_DBUS_SOCKET, NULL, true);
1234 if (UNIT(s)->default_dependencies) {
1235 r = service_add_default_dependencies(s);
1240 r = unit_exec_context_defaults(u, &s->exec_context);
1245 return service_verify(s);
1248 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1250 ServiceExecCommand c;
1251 Service *s = SERVICE(u);
1252 const char *prefix2;
1253 _cleanup_free_ char *p2 = NULL;
1257 p2 = strappend(prefix, "\t");
1258 prefix2 = p2 ? p2 : prefix;
1261 "%sService State: %s\n"
1263 "%sReload Result: %s\n"
1264 "%sPermissionsStartOnly: %s\n"
1265 "%sRootDirectoryStartOnly: %s\n"
1266 "%sRemainAfterExit: %s\n"
1267 "%sGuessMainPID: %s\n"
1270 "%sNotifyAccess: %s\n",
1271 prefix, service_state_to_string(s->state),
1272 prefix, service_result_to_string(s->result),
1273 prefix, service_result_to_string(s->reload_result),
1274 prefix, yes_no(s->permissions_start_only),
1275 prefix, yes_no(s->root_directory_start_only),
1276 prefix, yes_no(s->remain_after_exit),
1277 prefix, yes_no(s->guess_main_pid),
1278 prefix, service_type_to_string(s->type),
1279 prefix, service_restart_to_string(s->restart),
1280 prefix, notify_access_to_string(s->notify_access));
1282 if (s->control_pid > 0)
1284 "%sControl PID: %lu\n",
1285 prefix, (unsigned long) s->control_pid);
1287 if (s->main_pid > 0)
1290 "%sMain PID Known: %s\n"
1291 "%sMain PID Alien: %s\n",
1292 prefix, (unsigned long) s->main_pid,
1293 prefix, yes_no(s->main_pid_known),
1294 prefix, yes_no(s->main_pid_alien));
1299 prefix, s->pid_file);
1304 "%sBus Name Good: %s\n",
1305 prefix, s->bus_name,
1306 prefix, yes_no(s->bus_name_good));
1308 kill_context_dump(&s->kill_context, f, prefix);
1309 exec_context_dump(&s->exec_context, f, prefix);
1311 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1313 if (!s->exec_command[c])
1316 fprintf(f, "%s-> %s:\n",
1317 prefix, service_exec_command_to_string(c));
1319 exec_command_dump_list(s->exec_command[c], f, prefix2);
1322 #ifdef HAVE_SYSV_COMPAT
1325 "%sSysV Init Script has LSB Header: %s\n"
1326 "%sSysVEnabled: %s\n",
1327 prefix, yes_no(s->sysv_has_lsb),
1328 prefix, yes_no(s->sysv_enabled));
1330 if (s->sysv_start_priority >= 0)
1332 "%sSysVStartPriority: %i\n",
1333 prefix, s->sysv_start_priority);
1335 if (s->sysv_runlevels)
1336 fprintf(f, "%sSysVRunLevels: %s\n",
1337 prefix, s->sysv_runlevels);
1341 fprintf(f, "%sStatus Text: %s\n",
1342 prefix, s->status_text);
1345 static int service_load_pid_file(Service *s, bool may_warn) {
1346 _cleanup_free_ char *k = NULL;
1355 r = read_one_line_file(s->pid_file, &k);
1358 log_info_unit(UNIT(s)->id,
1359 "PID file %s not readable (yet?) after %s.",
1360 s->pid_file, service_state_to_string(s->state));
1364 r = parse_pid(k, &pid);
1367 log_info_unit(UNIT(s)->id,
1368 "Failed to read PID from file %s: %s",
1369 s->pid_file, strerror(-r));
1373 if (kill(pid, 0) < 0 && errno != EPERM) {
1375 log_info_unit(UNIT(s)->id,
1376 "PID %lu read from file %s does not exist.",
1377 (unsigned long) pid, s->pid_file);
1381 if (s->main_pid_known) {
1382 if (pid == s->main_pid)
1385 log_debug_unit(UNIT(s)->id,
1386 "Main PID changing: %lu -> %lu",
1387 (unsigned long) s->main_pid, (unsigned long) pid);
1388 service_unwatch_main_pid(s);
1389 s->main_pid_known = false;
1391 log_debug_unit(UNIT(s)->id,
1392 "Main PID loaded: %lu", (unsigned long) pid);
1394 r = service_set_main_pid(s, pid);
1398 r = unit_watch_pid(UNIT(s), pid);
1400 /* FIXME: we need to do something here */
1401 log_warning_unit(UNIT(s)->id,
1402 "Failed to watch PID %lu from service %s",
1403 (unsigned long) pid, UNIT(s)->id);
1410 static int service_search_main_pid(Service *s) {
1416 /* If we know it anyway, don't ever fallback to unreliable
1418 if (s->main_pid_known)
1421 if (!s->guess_main_pid)
1424 assert(s->main_pid <= 0);
1426 pid = unit_search_main_pid(UNIT(s));
1430 log_debug_unit(UNIT(s)->id,
1431 "Main PID guessed: %lu", (unsigned long) pid);
1432 r = service_set_main_pid(s, pid);
1436 r = unit_watch_pid(UNIT(s), pid);
1438 /* FIXME: we need to do something here */
1439 log_warning_unit(UNIT(s)->id,
1440 "Failed to watch PID %lu from service %s",
1441 (unsigned long) pid, UNIT(s)->id);
1447 static void service_set_state(Service *s, ServiceState state) {
1448 ServiceState old_state;
1449 const UnitActiveState *table;
1452 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1454 old_state = s->state;
1457 service_unwatch_pid_file(s);
1459 if (state != SERVICE_START_PRE &&
1460 state != SERVICE_START &&
1461 state != SERVICE_START_POST &&
1462 state != SERVICE_RELOAD &&
1463 state != SERVICE_STOP &&
1464 state != SERVICE_STOP_SIGTERM &&
1465 state != SERVICE_STOP_SIGKILL &&
1466 state != SERVICE_STOP_POST &&
1467 state != SERVICE_FINAL_SIGTERM &&
1468 state != SERVICE_FINAL_SIGKILL &&
1469 state != SERVICE_AUTO_RESTART)
1470 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1472 if (state != SERVICE_START &&
1473 state != SERVICE_START_POST &&
1474 state != SERVICE_RUNNING &&
1475 state != SERVICE_RELOAD &&
1476 state != SERVICE_STOP &&
1477 state != SERVICE_STOP_SIGTERM &&
1478 state != SERVICE_STOP_SIGKILL) {
1479 service_unwatch_main_pid(s);
1480 s->main_command = NULL;
1483 if (state != SERVICE_START_PRE &&
1484 state != SERVICE_START &&
1485 state != SERVICE_START_POST &&
1486 state != SERVICE_RELOAD &&
1487 state != SERVICE_STOP &&
1488 state != SERVICE_STOP_SIGTERM &&
1489 state != SERVICE_STOP_SIGKILL &&
1490 state != SERVICE_STOP_POST &&
1491 state != SERVICE_FINAL_SIGTERM &&
1492 state != SERVICE_FINAL_SIGKILL) {
1493 service_unwatch_control_pid(s);
1494 s->control_command = NULL;
1495 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1498 if (state != SERVICE_START_PRE &&
1499 state != SERVICE_START &&
1500 state != SERVICE_START_POST &&
1501 state != SERVICE_RUNNING &&
1502 state != SERVICE_RELOAD &&
1503 state != SERVICE_STOP &&
1504 state != SERVICE_STOP_SIGTERM &&
1505 state != SERVICE_STOP_SIGKILL &&
1506 state != SERVICE_STOP_POST &&
1507 state != SERVICE_FINAL_SIGTERM &&
1508 state != SERVICE_FINAL_SIGKILL &&
1509 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1510 service_close_socket_fd(s);
1511 service_connection_unref(s);
1514 if (state == SERVICE_STOP || state == SERVICE_STOP_SIGTERM)
1515 service_stop_watchdog(s);
1517 /* For the inactive states unit_notify() will trim the cgroup,
1518 * but for exit we have to do that ourselves... */
1519 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1520 unit_destroy_cgroup(UNIT(s));
1522 if (old_state != state)
1523 log_debug_unit(UNIT(s)->id,
1524 "%s changed %s -> %s", UNIT(s)->id,
1525 service_state_to_string(old_state),
1526 service_state_to_string(state));
1528 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1529 s->reload_result = SERVICE_SUCCESS;
1532 static int service_coldplug(Unit *u) {
1533 Service *s = SERVICE(u);
1537 assert(s->state == SERVICE_DEAD);
1539 if (s->deserialized_state != s->state) {
1541 if (s->deserialized_state == SERVICE_START_PRE ||
1542 s->deserialized_state == SERVICE_START ||
1543 s->deserialized_state == SERVICE_START_POST ||
1544 s->deserialized_state == SERVICE_RELOAD ||
1545 s->deserialized_state == SERVICE_STOP ||
1546 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1547 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1548 s->deserialized_state == SERVICE_STOP_POST ||
1549 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1550 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1551 s->deserialized_state == SERVICE_AUTO_RESTART) {
1553 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1556 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1558 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
1564 if ((s->deserialized_state == SERVICE_START &&
1565 (s->type == SERVICE_FORKING ||
1566 s->type == SERVICE_DBUS ||
1567 s->type == SERVICE_ONESHOT ||
1568 s->type == SERVICE_NOTIFY)) ||
1569 s->deserialized_state == SERVICE_START_POST ||
1570 s->deserialized_state == SERVICE_RUNNING ||
1571 s->deserialized_state == SERVICE_RELOAD ||
1572 s->deserialized_state == SERVICE_STOP ||
1573 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1574 s->deserialized_state == SERVICE_STOP_SIGKILL)
1575 if (s->main_pid > 0) {
1576 r = unit_watch_pid(UNIT(s), s->main_pid);
1581 if (s->deserialized_state == SERVICE_START_PRE ||
1582 s->deserialized_state == SERVICE_START ||
1583 s->deserialized_state == SERVICE_START_POST ||
1584 s->deserialized_state == SERVICE_RELOAD ||
1585 s->deserialized_state == SERVICE_STOP ||
1586 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1587 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1588 s->deserialized_state == SERVICE_STOP_POST ||
1589 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1590 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1591 if (s->control_pid > 0) {
1592 r = unit_watch_pid(UNIT(s), s->control_pid);
1597 if (s->deserialized_state == SERVICE_START_POST ||
1598 s->deserialized_state == SERVICE_RUNNING)
1599 service_handle_watchdog(s);
1601 service_set_state(s, s->deserialized_state);
1606 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1610 unsigned rn_fds = 0;
1617 if (s->socket_fd >= 0)
1620 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1625 if (u->type != UNIT_SOCKET)
1630 r = socket_collect_fds(sock, &cfds, &cn_fds);
1643 t = new(int, rn_fds+cn_fds);
1650 memcpy(t, rfds, rn_fds * sizeof(int));
1651 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1656 rn_fds = rn_fds+cn_fds;
1671 static int service_spawn(
1676 bool apply_permissions,
1678 bool apply_tty_stdin,
1679 bool set_notify_socket,
1686 _cleanup_free_ int *fdsbuf = NULL;
1687 unsigned n_fds = 0, n_env = 0;
1688 _cleanup_strv_free_ char
1689 **argv = NULL, **final_env = NULL, **our_env = NULL;
1696 unit_realize_cgroup(UNIT(s));
1699 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1700 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1701 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1703 if (s->socket_fd >= 0) {
1704 fds = &s->socket_fd;
1707 r = service_collect_fds(s, &fdsbuf, &n_fds);
1715 if (timeout && s->timeout_start_usec) {
1716 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true,
1717 s->timeout_start_usec, &s->timer_watch);
1721 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1723 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
1727 our_env = new0(char*, 5);
1733 if (set_notify_socket)
1734 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1739 if (s->main_pid > 0)
1740 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1745 if (s->watchdog_usec > 0)
1746 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1751 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
1752 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1757 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1763 if (is_control && UNIT(s)->cgroup_path) {
1764 path = strappenda(UNIT(s)->cgroup_path, "/control");
1765 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1767 path = UNIT(s)->cgroup_path;
1777 UNIT(s)->manager->confirm_spawn,
1778 UNIT(s)->manager->cgroup_supported,
1781 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1786 r = unit_watch_pid(UNIT(s), pid);
1788 /* FIXME: we need to do something here */
1797 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1802 static int main_pid_good(Service *s) {
1805 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1808 /* If we know the pid file, then lets just check if it is
1810 if (s->main_pid_known) {
1812 /* If it's an alien child let's check if it is still
1814 if (s->main_pid_alien && s->main_pid > 0)
1815 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1817 /* .. otherwise assume we'll get a SIGCHLD for it,
1818 * which we really should wait for to collect exit
1819 * status and code */
1820 return s->main_pid > 0;
1823 /* We don't know the pid */
1827 _pure_ static int control_pid_good(Service *s) {
1830 return s->control_pid > 0;
1833 static int cgroup_good(Service *s) {
1838 if (!UNIT(s)->cgroup_path)
1841 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1848 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1852 if (f != SERVICE_SUCCESS)
1855 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1857 if (allow_restart &&
1858 !s->forbid_restart &&
1859 (s->restart == SERVICE_RESTART_ALWAYS ||
1860 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1861 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1862 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1863 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1864 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1865 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1866 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1867 (s->result != SERVICE_FAILURE_SIGNAL ||
1868 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1871 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
1875 service_set_state(s, SERVICE_AUTO_RESTART);
1878 s->forbid_restart = false;
1880 /* we want fresh tmpdirs in case service is started again immediately */
1881 exec_context_tmp_dirs_done(&s->exec_context);
1883 /* Try to delete the pid file. At this point it will be
1884 * out-of-date, and some software might be confused by it, so
1885 * let's remove it. */
1887 unlink_noerrno(s->pid_file);
1892 log_warning_unit(UNIT(s)->id,
1893 "%s failed to run install restart timer: %s",
1894 UNIT(s)->id, strerror(-r));
1895 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1898 static void service_enter_stop_post(Service *s, ServiceResult f) {
1902 if (f != SERVICE_SUCCESS)
1905 service_unwatch_control_pid(s);
1907 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1908 if (s->control_command) {
1909 s->control_command_id = SERVICE_EXEC_STOP_POST;
1911 r = service_spawn(s,
1915 !s->permissions_start_only,
1916 !s->root_directory_start_only,
1925 service_set_state(s, SERVICE_STOP_POST);
1927 service_enter_dead(s, SERVICE_SUCCESS, true);
1932 log_warning_unit(UNIT(s)->id,
1933 "%s failed to run 'stop-post' task: %s",
1934 UNIT(s)->id, strerror(-r));
1935 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1938 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1943 if (f != SERVICE_SUCCESS)
1946 r = unit_kill_context(
1949 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1957 if (s->timeout_stop_usec > 0) {
1958 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true,
1959 s->timeout_stop_usec, &s->timer_watch);
1964 service_set_state(s, state);
1965 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1966 service_enter_stop_post(s, SERVICE_SUCCESS);
1968 service_enter_dead(s, SERVICE_SUCCESS, true);
1973 log_warning_unit(UNIT(s)->id,
1974 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1976 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1977 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1979 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1982 static void service_enter_stop(Service *s, ServiceResult f) {
1987 if (f != SERVICE_SUCCESS)
1990 service_unwatch_control_pid(s);
1992 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
1993 if (s->control_command) {
1994 s->control_command_id = SERVICE_EXEC_STOP;
1996 r = service_spawn(s,
2000 !s->permissions_start_only,
2001 !s->root_directory_start_only,
2009 service_set_state(s, SERVICE_STOP);
2011 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2016 log_warning_unit(UNIT(s)->id,
2017 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2018 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2021 static void service_enter_running(Service *s, ServiceResult f) {
2022 int main_pid_ok, cgroup_ok;
2025 if (f != SERVICE_SUCCESS)
2028 main_pid_ok = main_pid_good(s);
2029 cgroup_ok = cgroup_good(s);
2031 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2032 (s->bus_name_good || s->type != SERVICE_DBUS))
2033 service_set_state(s, SERVICE_RUNNING);
2034 else if (s->remain_after_exit)
2035 service_set_state(s, SERVICE_EXITED);
2037 service_enter_stop(s, SERVICE_SUCCESS);
2040 static void service_enter_start_post(Service *s) {
2044 service_unwatch_control_pid(s);
2046 if (s->watchdog_usec > 0)
2047 service_reset_watchdog(s);
2049 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
2050 if (s->control_command) {
2051 s->control_command_id = SERVICE_EXEC_START_POST;
2053 r = service_spawn(s,
2057 !s->permissions_start_only,
2058 !s->root_directory_start_only,
2066 service_set_state(s, SERVICE_START_POST);
2068 service_enter_running(s, SERVICE_SUCCESS);
2073 log_warning_unit(UNIT(s)->id,
2074 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2075 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2078 static void service_kill_control_processes(Service *s) {
2081 if (!UNIT(s)->cgroup_path)
2084 p = strappenda(UNIT(s)->cgroup_path, "/control");
2085 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
2088 static void service_enter_start(Service *s) {
2095 assert(s->exec_command[SERVICE_EXEC_START]);
2096 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2098 service_unwatch_control_pid(s);
2099 service_unwatch_main_pid(s);
2101 /* We want to ensure that nobody leaks processes from
2102 * START_PRE here, so let's go on a killing spree, People
2103 * should not spawn long running processes from START_PRE. */
2104 service_kill_control_processes(s);
2106 if (s->type == SERVICE_FORKING) {
2107 s->control_command_id = SERVICE_EXEC_START;
2108 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2110 s->main_command = NULL;
2112 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2113 s->control_command = NULL;
2115 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2118 r = service_spawn(s,
2120 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
2121 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2126 s->notify_access != NOTIFY_NONE,
2132 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2133 /* For simple services we immediately start
2134 * the START_POST binaries. */
2136 service_set_main_pid(s, pid);
2137 service_enter_start_post(s);
2139 } else if (s->type == SERVICE_FORKING) {
2141 /* For forking services we wait until the start
2142 * process exited. */
2144 s->control_pid = pid;
2145 service_set_state(s, SERVICE_START);
2147 } else if (s->type == SERVICE_ONESHOT ||
2148 s->type == SERVICE_DBUS ||
2149 s->type == SERVICE_NOTIFY) {
2151 /* For oneshot services we wait until the start
2152 * process exited, too, but it is our main process. */
2154 /* For D-Bus services we know the main pid right away,
2155 * but wait for the bus name to appear on the
2156 * bus. Notify services are similar. */
2158 service_set_main_pid(s, pid);
2159 service_set_state(s, SERVICE_START);
2161 assert_not_reached("Unknown service type");
2166 log_warning_unit(UNIT(s)->id,
2167 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2168 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2171 static void service_enter_start_pre(Service *s) {
2176 service_unwatch_control_pid(s);
2178 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
2179 if (s->control_command) {
2180 /* Before we start anything, let's clear up what might
2181 * be left from previous runs. */
2182 service_kill_control_processes(s);
2184 s->control_command_id = SERVICE_EXEC_START_PRE;
2186 r = service_spawn(s,
2190 !s->permissions_start_only,
2191 !s->root_directory_start_only,
2199 service_set_state(s, SERVICE_START_PRE);
2201 service_enter_start(s);
2206 log_warning_unit(UNIT(s)->id,
2207 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2208 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2211 static void service_enter_restart(Service *s) {
2216 dbus_error_init(&error);
2218 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2219 /* Don't restart things if we are going down anyway */
2220 log_info_unit(UNIT(s)->id,
2221 "Stop job pending for unit, delaying automatic restart.");
2223 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
2230 /* Any units that are bound to this service must also be
2231 * restarted. We use JOB_RESTART (instead of the more obvious
2232 * JOB_START) here so that those dependency jobs will be added
2234 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2238 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2239 * it will be canceled as part of the service_stop() call that
2240 * is executed as part of JOB_RESTART. */
2242 log_debug_unit(UNIT(s)->id,
2243 "%s scheduled restart job.", UNIT(s)->id);
2247 log_warning_unit(UNIT(s)->id,
2248 "%s failed to schedule restart job: %s",
2249 UNIT(s)->id, bus_error(&error, -r));
2250 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2252 dbus_error_free(&error);
2255 static void service_enter_reload(Service *s) {
2260 service_unwatch_control_pid(s);
2262 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
2263 if (s->control_command) {
2264 s->control_command_id = SERVICE_EXEC_RELOAD;
2266 r = service_spawn(s,
2270 !s->permissions_start_only,
2271 !s->root_directory_start_only,
2279 service_set_state(s, SERVICE_RELOAD);
2281 service_enter_running(s, SERVICE_SUCCESS);
2286 log_warning_unit(UNIT(s)->id,
2287 "%s failed to run 'reload' task: %s",
2288 UNIT(s)->id, strerror(-r));
2289 s->reload_result = SERVICE_FAILURE_RESOURCES;
2290 service_enter_running(s, SERVICE_SUCCESS);
2293 static void service_run_next_control(Service *s) {
2297 assert(s->control_command);
2298 assert(s->control_command->command_next);
2300 assert(s->control_command_id != SERVICE_EXEC_START);
2302 s->control_command = s->control_command->command_next;
2303 service_unwatch_control_pid(s);
2305 r = service_spawn(s,
2309 !s->permissions_start_only,
2310 !s->root_directory_start_only,
2311 s->control_command_id == SERVICE_EXEC_START_PRE ||
2312 s->control_command_id == SERVICE_EXEC_STOP_POST,
2322 log_warning_unit(UNIT(s)->id,
2323 "%s failed to run next control task: %s",
2324 UNIT(s)->id, strerror(-r));
2326 if (s->state == SERVICE_START_PRE)
2327 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2328 else if (s->state == SERVICE_STOP)
2329 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2330 else if (s->state == SERVICE_STOP_POST)
2331 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2332 else if (s->state == SERVICE_RELOAD) {
2333 s->reload_result = SERVICE_FAILURE_RESOURCES;
2334 service_enter_running(s, SERVICE_SUCCESS);
2336 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2339 static void service_run_next_main(Service *s) {
2344 assert(s->main_command);
2345 assert(s->main_command->command_next);
2346 assert(s->type == SERVICE_ONESHOT);
2348 s->main_command = s->main_command->command_next;
2349 service_unwatch_main_pid(s);
2351 r = service_spawn(s,
2358 s->notify_access != NOTIFY_NONE,
2364 service_set_main_pid(s, pid);
2369 log_warning_unit(UNIT(s)->id,
2370 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2371 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2374 static int service_start_limit_test(Service *s) {
2377 if (ratelimit_test(&s->start_limit))
2380 switch (s->start_limit_action) {
2382 case SERVICE_START_LIMIT_NONE:
2383 log_warning_unit(UNIT(s)->id,
2384 "%s start request repeated too quickly, refusing to start.",
2388 case SERVICE_START_LIMIT_REBOOT: {
2392 dbus_error_init(&error);
2394 log_warning_unit(UNIT(s)->id,
2395 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2397 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
2398 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
2399 true, &error, NULL);
2401 log_error_unit(UNIT(s)->id,
2402 "Failed to reboot: %s.", bus_error(&error, r));
2403 dbus_error_free(&error);
2409 case SERVICE_START_LIMIT_REBOOT_FORCE:
2410 log_warning_unit(UNIT(s)->id,
2411 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2412 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2415 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2416 log_warning_unit(UNIT(s)->id,
2417 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2419 reboot(RB_AUTOBOOT);
2423 log_error_unit(UNIT(s)->id,
2424 "start limit action=%i", s->start_limit_action);
2425 assert_not_reached("Unknown StartLimitAction.");
2431 static int service_start(Unit *u) {
2432 Service *s = SERVICE(u);
2437 /* We cannot fulfill this request right now, try again later
2439 if (s->state == SERVICE_STOP ||
2440 s->state == SERVICE_STOP_SIGTERM ||
2441 s->state == SERVICE_STOP_SIGKILL ||
2442 s->state == SERVICE_STOP_POST ||
2443 s->state == SERVICE_FINAL_SIGTERM ||
2444 s->state == SERVICE_FINAL_SIGKILL)
2447 /* Already on it! */
2448 if (s->state == SERVICE_START_PRE ||
2449 s->state == SERVICE_START ||
2450 s->state == SERVICE_START_POST)
2453 /* A service that will be restarted must be stopped first to
2454 * trigger BindsTo and/or OnFailure dependencies. If a user
2455 * does not want to wait for the holdoff time to elapse, the
2456 * service should be manually restarted, not started. We
2457 * simply return EAGAIN here, so that any start jobs stay
2458 * queued, and assume that the auto restart timer will
2459 * eventually trigger the restart. */
2460 if (s->state == SERVICE_AUTO_RESTART)
2463 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2465 /* Make sure we don't enter a busy loop of some kind. */
2466 r = service_start_limit_test(s);
2468 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2472 s->result = SERVICE_SUCCESS;
2473 s->reload_result = SERVICE_SUCCESS;
2474 s->main_pid_known = false;
2475 s->main_pid_alien = false;
2476 s->forbid_restart = false;
2478 service_enter_start_pre(s);
2482 static int service_stop(Unit *u) {
2483 Service *s = SERVICE(u);
2487 /* Don't create restart jobs from here. */
2488 s->forbid_restart = true;
2491 if (s->state == SERVICE_STOP ||
2492 s->state == SERVICE_STOP_SIGTERM ||
2493 s->state == SERVICE_STOP_SIGKILL ||
2494 s->state == SERVICE_STOP_POST ||
2495 s->state == SERVICE_FINAL_SIGTERM ||
2496 s->state == SERVICE_FINAL_SIGKILL)
2499 /* A restart will be scheduled or is in progress. */
2500 if (s->state == SERVICE_AUTO_RESTART) {
2501 service_set_state(s, SERVICE_DEAD);
2505 /* If there's already something running we go directly into
2507 if (s->state == SERVICE_START_PRE ||
2508 s->state == SERVICE_START ||
2509 s->state == SERVICE_START_POST ||
2510 s->state == SERVICE_RELOAD) {
2511 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2515 assert(s->state == SERVICE_RUNNING ||
2516 s->state == SERVICE_EXITED);
2518 service_enter_stop(s, SERVICE_SUCCESS);
2522 static int service_reload(Unit *u) {
2523 Service *s = SERVICE(u);
2527 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2529 service_enter_reload(s);
2533 _pure_ static bool service_can_reload(Unit *u) {
2534 Service *s = SERVICE(u);
2538 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2541 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2542 Service *s = SERVICE(u);
2548 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2549 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2550 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2552 if (s->control_pid > 0)
2553 unit_serialize_item_format(u, f, "control-pid", "%lu",
2554 (unsigned long) s->control_pid);
2556 if (s->main_pid_known && s->main_pid > 0)
2557 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2559 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2562 unit_serialize_item(u, f, "status-text", s->status_text);
2564 /* FIXME: There's a minor uncleanliness here: if there are
2565 * multiple commands attached here, we will start from the
2566 * first one again */
2567 if (s->control_command_id >= 0)
2568 unit_serialize_item(u, f, "control-command",
2569 service_exec_command_to_string(s->control_command_id));
2571 if (s->socket_fd >= 0) {
2574 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2577 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2580 if (s->main_exec_status.pid > 0) {
2581 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu",
2582 (unsigned long) s->main_exec_status.pid);
2583 dual_timestamp_serialize(f, "main-exec-status-start",
2584 &s->main_exec_status.start_timestamp);
2585 dual_timestamp_serialize(f, "main-exec-status-exit",
2586 &s->main_exec_status.exit_timestamp);
2588 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2589 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
2590 s->main_exec_status.code);
2591 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
2592 s->main_exec_status.status);
2595 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2596 dual_timestamp_serialize(f, "watchdog-timestamp",
2597 &s->watchdog_timestamp);
2599 if (s->exec_context.tmp_dir)
2600 unit_serialize_item(u, f, "tmp-dir", s->exec_context.tmp_dir);
2602 if (s->exec_context.var_tmp_dir)
2603 unit_serialize_item(u, f, "var-tmp-dir", s->exec_context.var_tmp_dir);
2605 if (s->forbid_restart)
2606 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
2611 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2612 Service *s = SERVICE(u);
2619 if (streq(key, "state")) {
2622 state = service_state_from_string(value);
2624 log_debug_unit(u->id, "Failed to parse state value %s", value);
2626 s->deserialized_state = state;
2627 } else if (streq(key, "result")) {
2630 f = service_result_from_string(value);
2632 log_debug_unit(u->id, "Failed to parse result value %s", value);
2633 else if (f != SERVICE_SUCCESS)
2636 } else if (streq(key, "reload-result")) {
2639 f = service_result_from_string(value);
2641 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2642 else if (f != SERVICE_SUCCESS)
2643 s->reload_result = f;
2645 } else if (streq(key, "control-pid")) {
2648 if (parse_pid(value, &pid) < 0)
2649 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2651 s->control_pid = pid;
2652 } else if (streq(key, "main-pid")) {
2655 if (parse_pid(value, &pid) < 0)
2656 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2658 service_set_main_pid(s, pid);
2659 unit_watch_pid(UNIT(s), pid);
2661 } else if (streq(key, "main-pid-known")) {
2664 b = parse_boolean(value);
2666 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2668 s->main_pid_known = b;
2669 } else if (streq(key, "status-text")) {
2676 free(s->status_text);
2680 } else if (streq(key, "control-command")) {
2681 ServiceExecCommand id;
2683 id = service_exec_command_from_string(value);
2685 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2687 s->control_command_id = id;
2688 s->control_command = s->exec_command[id];
2690 } else if (streq(key, "socket-fd")) {
2693 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2694 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2697 if (s->socket_fd >= 0)
2698 close_nointr_nofail(s->socket_fd);
2699 s->socket_fd = fdset_remove(fds, fd);
2701 } else if (streq(key, "main-exec-status-pid")) {
2704 if (parse_pid(value, &pid) < 0)
2705 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2707 s->main_exec_status.pid = pid;
2708 } else if (streq(key, "main-exec-status-code")) {
2711 if (safe_atoi(value, &i) < 0)
2712 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2714 s->main_exec_status.code = i;
2715 } else if (streq(key, "main-exec-status-status")) {
2718 if (safe_atoi(value, &i) < 0)
2719 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2721 s->main_exec_status.status = i;
2722 } else if (streq(key, "main-exec-status-start"))
2723 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2724 else if (streq(key, "main-exec-status-exit"))
2725 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2726 else if (streq(key, "watchdog-timestamp"))
2727 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2728 else if (streq(key, "tmp-dir")) {
2735 s->exec_context.tmp_dir = t;
2736 } else if (streq(key, "var-tmp-dir")) {
2743 s->exec_context.var_tmp_dir = t;
2744 } else if (streq(key, "forbid-restart")) {
2747 b = parse_boolean(value);
2749 log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value);
2751 s->forbid_restart = b;
2753 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2758 _pure_ static UnitActiveState service_active_state(Unit *u) {
2759 const UnitActiveState *table;
2763 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2765 return table[SERVICE(u)->state];
2768 static const char *service_sub_state_to_string(Unit *u) {
2771 return service_state_to_string(SERVICE(u)->state);
2774 static bool service_check_gc(Unit *u) {
2775 Service *s = SERVICE(u);
2779 /* Never clean up services that still have a process around,
2780 * even if the service is formally dead. */
2781 if (cgroup_good(s) > 0 ||
2782 main_pid_good(s) > 0 ||
2783 control_pid_good(s) > 0)
2786 #ifdef HAVE_SYSV_COMPAT
2794 _pure_ static bool service_check_snapshot(Unit *u) {
2795 Service *s = SERVICE(u);
2799 return !s->got_socket_fd;
2802 static int service_retry_pid_file(Service *s) {
2805 assert(s->pid_file);
2806 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2808 r = service_load_pid_file(s, false);
2812 service_unwatch_pid_file(s);
2814 service_enter_running(s, SERVICE_SUCCESS);
2818 static int service_watch_pid_file(Service *s) {
2821 log_debug_unit(UNIT(s)->id,
2822 "Setting watch for %s's PID file %s",
2823 UNIT(s)->id, s->pid_file_pathspec->path);
2824 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2828 /* the pidfile might have appeared just before we set the watch */
2829 log_debug_unit(UNIT(s)->id,
2830 "Trying to read %s's PID file %s in case it changed",
2831 UNIT(s)->id, s->pid_file_pathspec->path);
2832 service_retry_pid_file(s);
2836 log_error_unit(UNIT(s)->id,
2837 "Failed to set a watch for %s's PID file %s: %s",
2838 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2839 service_unwatch_pid_file(s);
2843 static int service_demand_pid_file(Service *s) {
2846 assert(s->pid_file);
2847 assert(!s->pid_file_pathspec);
2849 ps = new0(PathSpec, 1);
2853 ps->path = strdup(s->pid_file);
2859 path_kill_slashes(ps->path);
2861 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2862 * keep their PID file open all the time. */
2863 ps->type = PATH_MODIFIED;
2864 ps->inotify_fd = -1;
2866 s->pid_file_pathspec = ps;
2868 return service_watch_pid_file(s);
2871 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2872 Service *s = SERVICE(u);
2876 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2877 assert(s->pid_file_pathspec);
2878 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2880 log_debug_unit(u->id, "inotify event for %s", u->id);
2882 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2885 if (service_retry_pid_file(s) == 0)
2888 if (service_watch_pid_file(s) < 0)
2893 service_unwatch_pid_file(s);
2894 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2897 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2898 Service *s = SERVICE(u);
2904 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2905 is_clean_exit_lsb(code, status, &s->success_status))
2906 f = SERVICE_SUCCESS;
2907 else if (code == CLD_EXITED)
2908 f = SERVICE_FAILURE_EXIT_CODE;
2909 else if (code == CLD_KILLED)
2910 f = SERVICE_FAILURE_SIGNAL;
2911 else if (code == CLD_DUMPED)
2912 f = SERVICE_FAILURE_CORE_DUMP;
2914 assert_not_reached("Unknown code");
2916 if (s->main_pid == pid) {
2917 /* Forking services may occasionally move to a new PID.
2918 * As long as they update the PID file before exiting the old
2919 * PID, they're fine. */
2920 if (service_load_pid_file(s, false) == 0)
2924 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2926 if (s->main_command) {
2927 /* If this is not a forking service than the
2928 * main process got started and hence we copy
2929 * the exit status so that it is recorded both
2930 * as main and as control process exit
2933 s->main_command->exec_status = s->main_exec_status;
2935 if (s->main_command->ignore)
2936 f = SERVICE_SUCCESS;
2937 } else if (s->exec_command[SERVICE_EXEC_START]) {
2939 /* If this is a forked process, then we should
2940 * ignore the return value if this was
2941 * configured for the starter process */
2943 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2944 f = SERVICE_SUCCESS;
2947 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2949 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2950 u->id, sigchld_code_to_string(code), status,
2951 strna(code == CLD_EXITED
2952 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2953 : signal_to_string(status)),
2954 "EXIT_CODE=%s", sigchld_code_to_string(code),
2955 "EXIT_STATUS=%i", status,
2958 if (f != SERVICE_SUCCESS)
2961 if (s->main_command &&
2962 s->main_command->command_next &&
2963 f == SERVICE_SUCCESS) {
2965 /* There is another command to *
2966 * execute, so let's do that. */
2968 log_debug_unit(u->id,
2969 "%s running next main command for state %s",
2970 u->id, service_state_to_string(s->state));
2971 service_run_next_main(s);
2975 /* The service exited, so the service is officially
2977 s->main_command = NULL;
2981 case SERVICE_START_POST:
2982 case SERVICE_RELOAD:
2984 /* Need to wait until the operation is
2989 if (s->type == SERVICE_ONESHOT) {
2990 /* This was our main goal, so let's go on */
2991 if (f == SERVICE_SUCCESS)
2992 service_enter_start_post(s);
2994 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3000 case SERVICE_RUNNING:
3001 service_enter_running(s, f);
3004 case SERVICE_STOP_SIGTERM:
3005 case SERVICE_STOP_SIGKILL:
3007 if (!control_pid_good(s))
3008 service_enter_stop_post(s, f);
3010 /* If there is still a control process, wait for that first */
3014 assert_not_reached("Uh, main process died at wrong time.");
3018 } else if (s->control_pid == pid) {
3021 if (s->control_command) {
3022 exec_status_exit(&s->control_command->exec_status,
3023 &s->exec_context, pid, code, status);
3025 if (s->control_command->ignore)
3026 f = SERVICE_SUCCESS;
3029 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
3030 "%s: control process exited, code=%s status=%i",
3031 u->id, sigchld_code_to_string(code), status);
3033 if (f != SERVICE_SUCCESS)
3036 /* Immediately get rid of the cgroup, so that the
3037 * kernel doesn't delay the cgroup empty messages for
3038 * the service cgroup any longer than necessary */
3039 service_kill_control_processes(s);
3041 if (s->control_command &&
3042 s->control_command->command_next &&
3043 f == SERVICE_SUCCESS) {
3045 /* There is another command to *
3046 * execute, so let's do that. */
3048 log_debug_unit(u->id,
3049 "%s running next control command for state %s",
3050 u->id, service_state_to_string(s->state));
3051 service_run_next_control(s);
3054 /* No further commands for this step, so let's
3055 * figure out what to do next */
3057 s->control_command = NULL;
3058 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3060 log_debug_unit(u->id,
3061 "%s got final SIGCHLD for state %s",
3062 u->id, service_state_to_string(s->state));
3066 case SERVICE_START_PRE:
3067 if (f == SERVICE_SUCCESS)
3068 service_enter_start(s);
3070 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3074 if (s->type != SERVICE_FORKING)
3075 /* Maybe spurious event due to a reload that changed the type? */
3078 if (f != SERVICE_SUCCESS) {
3079 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3084 bool has_start_post;
3087 /* Let's try to load the pid file here if we can.
3088 * The PID file might actually be created by a START_POST
3089 * script. In that case don't worry if the loading fails. */
3091 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3092 r = service_load_pid_file(s, !has_start_post);
3093 if (!has_start_post && r < 0) {
3094 r = service_demand_pid_file(s);
3095 if (r < 0 || !cgroup_good(s))
3096 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3100 service_search_main_pid(s);
3102 service_enter_start_post(s);
3105 case SERVICE_START_POST:
3106 if (f != SERVICE_SUCCESS) {
3107 service_enter_stop(s, f);
3114 r = service_load_pid_file(s, true);
3116 r = service_demand_pid_file(s);
3117 if (r < 0 || !cgroup_good(s))
3118 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3122 service_search_main_pid(s);
3124 service_enter_running(s, SERVICE_SUCCESS);
3127 case SERVICE_RELOAD:
3128 if (f == SERVICE_SUCCESS) {
3129 service_load_pid_file(s, true);
3130 service_search_main_pid(s);
3133 s->reload_result = f;
3134 service_enter_running(s, SERVICE_SUCCESS);
3138 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3141 case SERVICE_STOP_SIGTERM:
3142 case SERVICE_STOP_SIGKILL:
3143 if (main_pid_good(s) <= 0)
3144 service_enter_stop_post(s, f);
3146 /* If there is still a service
3147 * process around, wait until
3148 * that one quit, too */
3151 case SERVICE_STOP_POST:
3152 case SERVICE_FINAL_SIGTERM:
3153 case SERVICE_FINAL_SIGKILL:
3154 service_enter_dead(s, f, true);
3158 assert_not_reached("Uh, control process died at wrong time.");
3163 /* Notify clients about changed exit status */
3164 unit_add_to_dbus_queue(u);
3167 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3168 Service *s = SERVICE(u);
3171 assert(elapsed == 1);
3173 if (w == &s->watchdog_watch) {
3174 service_handle_watchdog(s);
3178 assert(w == &s->timer_watch);
3182 case SERVICE_START_PRE:
3184 log_warning_unit(u->id,
3185 "%s operation timed out. Terminating.", u->id);
3186 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3189 case SERVICE_START_POST:
3190 log_warning_unit(u->id,
3191 "%s operation timed out. Stopping.", u->id);
3192 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3195 case SERVICE_RELOAD:
3196 log_warning_unit(u->id,
3197 "%s operation timed out. Stopping.", u->id);
3198 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3199 service_enter_running(s, SERVICE_SUCCESS);
3203 log_warning_unit(u->id,
3204 "%s stopping timed out. Terminating.", u->id);
3205 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3208 case SERVICE_STOP_SIGTERM:
3209 if (s->kill_context.send_sigkill) {
3210 log_warning_unit(u->id,
3211 "%s stopping timed out. Killing.", u->id);
3212 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3214 log_warning_unit(u->id,
3215 "%s stopping timed out. Skipping SIGKILL.", u->id);
3216 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3221 case SERVICE_STOP_SIGKILL:
3222 /* Uh, we sent a SIGKILL and it is still not gone?
3223 * Must be something we cannot kill, so let's just be
3224 * weirded out and continue */
3226 log_warning_unit(u->id,
3227 "%s still around after SIGKILL. Ignoring.", u->id);
3228 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3231 case SERVICE_STOP_POST:
3232 log_warning_unit(u->id,
3233 "%s stopping timed out (2). Terminating.", u->id);
3234 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3237 case SERVICE_FINAL_SIGTERM:
3238 if (s->kill_context.send_sigkill) {
3239 log_warning_unit(u->id,
3240 "%s stopping timed out (2). Killing.", u->id);
3241 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3243 log_warning_unit(u->id,
3244 "%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.",
3246 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3251 case SERVICE_FINAL_SIGKILL:
3252 log_warning_unit(u->id,
3253 "%s still around after SIGKILL (2). Entering failed mode.", u->id);
3254 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3257 case SERVICE_AUTO_RESTART:
3258 log_info_unit(u->id,
3259 "%s holdoff time over, scheduling restart.", u->id);
3260 service_enter_restart(s);
3264 assert_not_reached("Timeout at wrong time.");
3268 static void service_notify_cgroup_empty_event(Unit *u) {
3269 Service *s = SERVICE(u);
3273 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
3277 /* Waiting for SIGCHLD is usually more interesting,
3278 * because it includes return codes/signals. Which is
3279 * why we ignore the cgroup events for most cases,
3280 * except when we don't know pid which to expect the
3284 case SERVICE_START_POST:
3285 /* If we were hoping for the daemon to write its PID file,
3286 * we can give up now. */
3287 if (s->pid_file_pathspec) {
3288 log_warning_unit(u->id,
3289 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3290 service_unwatch_pid_file(s);
3291 if (s->state == SERVICE_START)
3292 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3294 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3298 case SERVICE_RUNNING:
3299 /* service_enter_running() will figure out what to do */
3300 service_enter_running(s, SERVICE_SUCCESS);
3303 case SERVICE_STOP_SIGTERM:
3304 case SERVICE_STOP_SIGKILL:
3306 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3307 service_enter_stop_post(s, SERVICE_SUCCESS);
3311 case SERVICE_FINAL_SIGTERM:
3312 case SERVICE_FINAL_SIGKILL:
3313 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3314 service_enter_dead(s, SERVICE_SUCCESS, true);
3323 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3324 Service *s = SERVICE(u);
3329 if (s->notify_access == NOTIFY_NONE) {
3330 log_warning_unit(u->id,
3331 "%s: Got notification message from PID %lu, but reception is disabled.",
3332 u->id, (unsigned long) pid);
3336 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3337 log_warning_unit(u->id,
3338 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3339 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3343 log_debug_unit(u->id,
3344 "%s: Got message", u->id);
3346 /* Interpret MAINPID= */
3347 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3348 (s->state == SERVICE_START ||
3349 s->state == SERVICE_START_POST ||
3350 s->state == SERVICE_RUNNING ||
3351 s->state == SERVICE_RELOAD)) {
3353 if (parse_pid(e + 8, &pid) < 0)
3354 log_warning_unit(u->id,
3355 "Failed to parse notification message %s", e);
3357 log_debug_unit(u->id,
3358 "%s: got %s", u->id, e);
3359 service_set_main_pid(s, pid);
3360 unit_watch_pid(UNIT(s), pid);
3364 /* Interpret READY= */
3365 if (s->type == SERVICE_NOTIFY &&
3366 s->state == SERVICE_START &&
3367 strv_find(tags, "READY=1")) {
3368 log_debug_unit(u->id,
3369 "%s: got READY=1", u->id);
3371 service_enter_start_post(s);
3374 /* Interpret STATUS= */
3375 e = strv_find_prefix(tags, "STATUS=");
3381 if (!utf8_is_valid(e+7)) {
3382 log_warning_unit(u->id,
3383 "Status message in notification is not UTF-8 clean.");
3389 log_error_unit(u->id,
3390 "Failed to allocate string.");
3394 log_debug_unit(u->id,
3395 "%s: got %s", u->id, e);
3397 free(s->status_text);
3400 free(s->status_text);
3401 s->status_text = NULL;
3405 if (strv_find(tags, "WATCHDOG=1")) {
3406 log_debug_unit(u->id,
3407 "%s: got WATCHDOG=1", u->id);
3408 if (dual_timestamp_is_set(&s->watchdog_timestamp))
3409 service_reset_watchdog(s);
3412 /* Notify clients about changed status or main pid */
3413 unit_add_to_dbus_queue(u);
3416 #ifdef HAVE_SYSV_COMPAT
3418 static int service_enumerate(Manager *m) {
3421 _cleanup_closedir_ DIR *d = NULL;
3422 _cleanup_free_ char *path = NULL, *fpath = NULL, *name = NULL;
3423 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
3424 _cleanup_set_free_ Set *shutdown_services = NULL;
3431 if (m->running_as != SYSTEMD_SYSTEM)
3434 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3435 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3439 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3450 if (errno != ENOENT)
3451 log_warning("opendir(%s) failed: %s", path, strerror(errno));
3456 while ((de = readdir(d))) {
3459 if (ignore_file(de->d_name))
3462 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3465 if (strlen(de->d_name) < 4)
3468 a = undecchar(de->d_name[1]);
3469 b = undecchar(de->d_name[2]);
3475 fpath = strjoin(path, "/", de->d_name, NULL);
3481 if (access(fpath, X_OK) < 0) {
3483 if (errno != ENOENT)
3484 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3490 name = sysv_translate_name(de->d_name + 3);
3496 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3498 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3502 if (de->d_name[0] == 'S') {
3504 if (rcnd_table[i].type == RUNLEVEL_UP) {
3505 SERVICE(service)->sysv_start_priority_from_rcnd =
3506 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3508 SERVICE(service)->sysv_enabled = true;
3511 r = set_ensure_allocated(&runlevel_services[i],
3512 trivial_hash_func, trivial_compare_func);
3516 r = set_put(runlevel_services[i], service);
3520 } else if (de->d_name[0] == 'K' &&
3521 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3523 r = set_ensure_allocated(&shutdown_services,
3524 trivial_hash_func, trivial_compare_func);
3528 r = set_put(shutdown_services, service);
3535 /* Now we loaded all stubs and are aware of the lowest
3536 start-up priority for all services, not let's actually load
3537 the services, this will also tell us which services are
3538 actually native now */
3539 manager_dispatch_load_queue(m);
3541 /* If this is a native service, rely on native ways to pull in
3542 * a service, don't pull it in via sysv rcN.d links. */
3543 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3544 SET_FOREACH(service, runlevel_services[i], j) {
3545 service = unit_follow_merge(service);
3547 if (service->fragment_path)
3550 r = unit_add_two_dependencies_by_name_inverse(
3551 service, UNIT_AFTER, UNIT_WANTS,
3552 rcnd_table[i].target, NULL, true);
3557 /* We honour K links only for halt/reboot. For the normal
3558 * runlevels we assume the stop jobs will be implicitly added
3559 * by the core logic. Also, we don't really distinguish here
3560 * between the runlevels 0 and 6 and just add them to the
3561 * special shutdown target. */
3562 SET_FOREACH(service, shutdown_services, j) {
3563 service = unit_follow_merge(service);
3565 if (service->fragment_path)
3568 r = unit_add_two_dependencies_by_name(
3569 service, UNIT_BEFORE, UNIT_CONFLICTS,
3570 SPECIAL_SHUTDOWN_TARGET, NULL, true);
3579 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3580 set_free(runlevel_services[i]);
3586 static void service_bus_name_owner_change(
3589 const char *old_owner,
3590 const char *new_owner) {
3592 Service *s = SERVICE(u);
3597 assert(streq(s->bus_name, name));
3598 assert(old_owner || new_owner);
3600 if (old_owner && new_owner)
3601 log_debug_unit(u->id,
3602 "%s's D-Bus name %s changed owner from %s to %s",
3603 u->id, name, old_owner, new_owner);
3605 log_debug_unit(u->id,
3606 "%s's D-Bus name %s no longer registered by %s",
3607 u->id, name, old_owner);
3609 log_debug_unit(u->id,
3610 "%s's D-Bus name %s now registered by %s",
3611 u->id, name, new_owner);
3613 s->bus_name_good = !!new_owner;
3615 if (s->type == SERVICE_DBUS) {
3617 /* service_enter_running() will figure out what to
3619 if (s->state == SERVICE_RUNNING)
3620 service_enter_running(s, SERVICE_SUCCESS);
3621 else if (s->state == SERVICE_START && new_owner)
3622 service_enter_start_post(s);
3624 } else if (new_owner &&
3626 (s->state == SERVICE_START ||
3627 s->state == SERVICE_START_POST ||
3628 s->state == SERVICE_RUNNING ||
3629 s->state == SERVICE_RELOAD)) {
3631 /* Try to acquire PID from bus service */
3632 log_debug_unit(u->id,
3633 "Trying to acquire PID from D-Bus name...");
3635 bus_query_pid(u->manager, name);
3639 static void service_bus_query_pid_done(
3644 Service *s = SERVICE(u);
3649 log_debug_unit(u->id,
3650 "%s's D-Bus name %s is now owned by process %u",
3651 u->id, name, (unsigned) pid);
3653 if (s->main_pid <= 0 &&
3654 (s->state == SERVICE_START ||
3655 s->state == SERVICE_START_POST ||
3656 s->state == SERVICE_RUNNING ||
3657 s->state == SERVICE_RELOAD)){
3658 service_set_main_pid(s, pid);
3659 unit_watch_pid(UNIT(s), pid);
3663 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3668 /* This is called by the socket code when instantiating a new
3669 * service for a stream socket and the socket needs to be
3672 if (UNIT(s)->load_state != UNIT_LOADED)
3675 if (s->socket_fd >= 0)
3678 if (s->state != SERVICE_DEAD)
3682 s->got_socket_fd = true;
3684 unit_ref_set(&s->accept_socket, UNIT(sock));
3686 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3689 static void service_reset_failed(Unit *u) {
3690 Service *s = SERVICE(u);
3694 if (s->state == SERVICE_FAILED)
3695 service_set_state(s, SERVICE_DEAD);
3697 s->result = SERVICE_SUCCESS;
3698 s->reload_result = SERVICE_SUCCESS;
3700 RATELIMIT_RESET(s->start_limit);
3703 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3704 Service *s = SERVICE(u);
3706 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
3709 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3710 [SERVICE_DEAD] = "dead",
3711 [SERVICE_START_PRE] = "start-pre",
3712 [SERVICE_START] = "start",
3713 [SERVICE_START_POST] = "start-post",
3714 [SERVICE_RUNNING] = "running",
3715 [SERVICE_EXITED] = "exited",
3716 [SERVICE_RELOAD] = "reload",
3717 [SERVICE_STOP] = "stop",
3718 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3719 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3720 [SERVICE_STOP_POST] = "stop-post",
3721 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3722 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3723 [SERVICE_FAILED] = "failed",
3724 [SERVICE_AUTO_RESTART] = "auto-restart",
3727 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3729 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3730 [SERVICE_RESTART_NO] = "no",
3731 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3732 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3733 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
3734 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3735 [SERVICE_RESTART_ALWAYS] = "always"
3738 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3740 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3741 [SERVICE_SIMPLE] = "simple",
3742 [SERVICE_FORKING] = "forking",
3743 [SERVICE_ONESHOT] = "oneshot",
3744 [SERVICE_DBUS] = "dbus",
3745 [SERVICE_NOTIFY] = "notify",
3746 [SERVICE_IDLE] = "idle"
3749 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3751 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3752 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3753 [SERVICE_EXEC_START] = "ExecStart",
3754 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3755 [SERVICE_EXEC_RELOAD] = "ExecReload",
3756 [SERVICE_EXEC_STOP] = "ExecStop",
3757 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3760 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3762 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3763 [NOTIFY_NONE] = "none",
3764 [NOTIFY_MAIN] = "main",
3765 [NOTIFY_ALL] = "all"
3768 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3770 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3771 [SERVICE_SUCCESS] = "success",
3772 [SERVICE_FAILURE_RESOURCES] = "resources",
3773 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3774 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3775 [SERVICE_FAILURE_SIGNAL] = "signal",
3776 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3777 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3778 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3781 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3783 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3784 [SERVICE_START_LIMIT_NONE] = "none",
3785 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3786 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3787 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3789 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3791 const UnitVTable service_vtable = {
3792 .object_size = sizeof(Service),
3799 .private_section = "Service",
3800 .exec_context_offset = offsetof(Service, exec_context),
3801 .cgroup_context_offset = offsetof(Service, cgroup_context),
3803 .init = service_init,
3804 .done = service_done,
3805 .load = service_load,
3807 .coldplug = service_coldplug,
3809 .dump = service_dump,
3811 .start = service_start,
3812 .stop = service_stop,
3813 .reload = service_reload,
3815 .can_reload = service_can_reload,
3817 .kill = service_kill,
3819 .serialize = service_serialize,
3820 .deserialize_item = service_deserialize_item,
3822 .active_state = service_active_state,
3823 .sub_state_to_string = service_sub_state_to_string,
3825 .check_gc = service_check_gc,
3826 .check_snapshot = service_check_snapshot,
3828 .sigchld_event = service_sigchld_event,
3829 .timer_event = service_timer_event,
3830 .fd_event = service_fd_event,
3832 .reset_failed = service_reset_failed,
3834 .notify_cgroup_empty = service_notify_cgroup_empty_event,
3835 .notify_message = service_notify_message,
3837 .bus_name_owner_change = service_bus_name_owner_change,
3838 .bus_query_pid_done = service_bus_query_pid_done,
3840 .bus_interface = "org.freedesktop.systemd1.Service",
3841 .bus_message_handler = bus_service_message_handler,
3842 .bus_invalidating_properties = bus_service_invalidating_properties,
3843 .bus_set_property = bus_service_set_property,
3844 .bus_commit_properties = bus_service_commit_properties,
3846 .can_transient = true,
3848 #ifdef HAVE_SYSV_COMPAT
3849 .enumerate = service_enumerate,
3851 .status_message_formats = {
3852 .starting_stopping = {
3853 [0] = "Starting %s...",
3854 [1] = "Stopping %s...",
3856 .finished_start_job = {
3857 [JOB_DONE] = "Started %s.",
3858 [JOB_FAILED] = "Failed to start %s.",
3859 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3860 [JOB_TIMEOUT] = "Timed out starting %s.",
3862 .finished_stop_job = {
3863 [JOB_DONE] = "Stopped %s.",
3864 [JOB_FAILED] = "Stopped (with error) %s.",
3865 [JOB_TIMEOUT] = "Timed out stopping %s.",