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 "bus-errors.h"
40 #include "exit-status.h"
42 #include "path-util.h"
46 #ifdef HAVE_SYSV_COMPAT
48 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
50 typedef enum RunlevelType {
58 const RunlevelType type;
60 /* Standard SysV runlevels for start-up */
61 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
62 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
63 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
64 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
65 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
67 /* Standard SysV runlevels for shutdown */
68 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
69 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
71 /* Note that the order here matters, as we read the
72 directories in this order, and we want to make sure that
73 sysv_start_priority is known when we first load the
74 unit. And that value we only know from S links. Hence
75 UP must be read before DOWN */
78 #define RUNLEVELS_UP "12345"
81 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
82 [SERVICE_DEAD] = UNIT_INACTIVE,
83 [SERVICE_START_PRE] = UNIT_ACTIVATING,
84 [SERVICE_START] = UNIT_ACTIVATING,
85 [SERVICE_START_POST] = UNIT_ACTIVATING,
86 [SERVICE_RUNNING] = UNIT_ACTIVE,
87 [SERVICE_EXITED] = UNIT_ACTIVE,
88 [SERVICE_RELOAD] = UNIT_RELOADING,
89 [SERVICE_STOP] = UNIT_DEACTIVATING,
90 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
91 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
92 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
93 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
94 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
95 [SERVICE_FAILED] = UNIT_FAILED,
96 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
99 /* For Type=idle we never want to delay any other jobs, hence we
100 * consider idle jobs active as soon as we start working on them */
101 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
102 [SERVICE_DEAD] = UNIT_INACTIVE,
103 [SERVICE_START_PRE] = UNIT_ACTIVE,
104 [SERVICE_START] = UNIT_ACTIVE,
105 [SERVICE_START_POST] = UNIT_ACTIVE,
106 [SERVICE_RUNNING] = UNIT_ACTIVE,
107 [SERVICE_EXITED] = UNIT_ACTIVE,
108 [SERVICE_RELOAD] = UNIT_RELOADING,
109 [SERVICE_STOP] = UNIT_DEACTIVATING,
110 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
111 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
112 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
113 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
114 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
115 [SERVICE_FAILED] = UNIT_FAILED,
116 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
119 static void service_init(Unit *u) {
120 Service *s = SERVICE(u);
123 assert(u->load_state == UNIT_STUB);
125 s->timeout_start_usec = DEFAULT_TIMEOUT_USEC;
126 s->timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
127 s->restart_usec = DEFAULT_RESTART_USEC;
128 s->type = _SERVICE_TYPE_INVALID;
130 watch_init(&s->watchdog_watch);
131 watch_init(&s->timer_watch);
133 #ifdef HAVE_SYSV_COMPAT
134 s->sysv_start_priority = -1;
135 s->sysv_start_priority_from_rcnd = -1;
138 s->guess_main_pid = true;
140 exec_context_init(&s->exec_context);
141 kill_context_init(&s->kill_context);
143 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
145 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
148 static void service_unwatch_control_pid(Service *s) {
151 if (s->control_pid <= 0)
154 unit_unwatch_pid(UNIT(s), s->control_pid);
158 static void service_unwatch_main_pid(Service *s) {
161 if (s->main_pid <= 0)
164 unit_unwatch_pid(UNIT(s), s->main_pid);
168 static void service_unwatch_pid_file(Service *s) {
169 if (!s->pid_file_pathspec)
172 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
173 UNIT(s)->id, s->pid_file_pathspec->path);
174 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
175 path_spec_done(s->pid_file_pathspec);
176 free(s->pid_file_pathspec);
177 s->pid_file_pathspec = NULL;
180 static int service_set_main_pid(Service *s, pid_t pid) {
192 s->main_pid_known = true;
194 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
195 log_warning_unit(UNIT(s)->id,
196 "%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
197 UNIT(s)->id, (unsigned long) pid);
199 s->main_pid_alien = true;
201 s->main_pid_alien = false;
203 exec_status_start(&s->main_exec_status, pid);
208 static void service_close_socket_fd(Service *s) {
211 if (s->socket_fd < 0)
214 close_nointr_nofail(s->socket_fd);
218 static void service_connection_unref(Service *s) {
221 if (!UNIT_DEREF(s->accept_socket))
224 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
225 unit_ref_unset(&s->accept_socket);
228 static void service_stop_watchdog(Service *s) {
231 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
232 s->watchdog_timestamp.realtime = 0;
233 s->watchdog_timestamp.monotonic = 0;
236 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart);
238 static void service_handle_watchdog(Service *s) {
244 if (s->watchdog_usec == 0)
247 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
248 if (offset >= s->watchdog_usec) {
249 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
250 service_enter_dead(s, SERVICE_FAILURE_WATCHDOG, true);
254 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->watchdog_usec - offset, &s->watchdog_watch);
256 log_warning_unit(UNIT(s)->id,
257 "%s failed to install watchdog timer: %s",
258 UNIT(s)->id, strerror(-r));
261 static void service_reset_watchdog(Service *s) {
264 dual_timestamp_get(&s->watchdog_timestamp);
265 service_handle_watchdog(s);
268 static void service_done(Unit *u) {
269 Service *s = SERVICE(u);
276 #ifdef HAVE_SYSV_COMPAT
277 free(s->sysv_runlevels);
278 s->sysv_runlevels = NULL;
281 free(s->status_text);
282 s->status_text = NULL;
284 exec_context_done(&s->exec_context);
285 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
286 s->control_command = NULL;
287 s->main_command = NULL;
289 set_free(s->restart_ignore_status.code);
290 s->restart_ignore_status.code = NULL;
291 set_free(s->restart_ignore_status.signal);
292 s->restart_ignore_status.signal = NULL;
294 set_free(s->success_status.code);
295 s->success_status.code = NULL;
296 set_free(s->success_status.signal);
297 s->success_status.signal = NULL;
299 /* This will leak a process, but at least no memory or any of
301 service_unwatch_main_pid(s);
302 service_unwatch_control_pid(s);
303 service_unwatch_pid_file(s);
306 unit_unwatch_bus_name(u, s->bus_name);
311 service_close_socket_fd(s);
312 service_connection_unref(s);
314 unit_ref_unset(&s->accept_socket);
316 service_stop_watchdog(s);
318 unit_unwatch_timer(u, &s->timer_watch);
321 #ifdef HAVE_SYSV_COMPAT
322 static char *sysv_translate_name(const char *name) {
325 if (!(r = new(char, strlen(name) + sizeof(".service"))))
328 if (endswith(name, ".sh"))
329 /* Drop .sh suffix */
330 strcpy(stpcpy(r, name) - 3, ".service");
331 if (startswith(name, "rc."))
332 /* Drop rc. prefix */
333 strcpy(stpcpy(r, name + 3), ".service");
335 /* Normal init script name */
336 strcpy(stpcpy(r, name), ".service");
341 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
343 /* We silently ignore the $ prefix here. According to the LSB
344 * spec it simply indicates whether something is a
345 * standardized name or a distribution-specific one. Since we
346 * just follow what already exists and do not introduce new
347 * uses or names we don't care who introduced a new name. */
349 static const char * const table[] = {
350 /* LSB defined facilities */
351 "local_fs", SPECIAL_LOCAL_FS_TARGET,
352 /* Due to unfortunate name selection in Mandriva,
353 * $network is provided by network-up which is ordered
354 * after network which actually starts interfaces.
355 * To break the loop, just ignore it */
356 "network", SPECIAL_NETWORK_TARGET,
357 "named", SPECIAL_NSS_LOOKUP_TARGET,
358 "portmap", SPECIAL_RPCBIND_TARGET,
359 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
360 "syslog", SPECIAL_SYSLOG_TARGET,
361 "time", SPECIAL_TIME_SYNC_TARGET,
363 /* common extensions */
364 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
365 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
367 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
368 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
378 n = *name == '$' ? name + 1 : name;
380 for (i = 0; i < ELEMENTSOF(table); i += 2) {
382 if (!streq(table[i], n))
388 if (!(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_add_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);
820 log_error_unit(u->id,
821 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
822 path, line, n, strerror(-r));
832 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
835 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
836 path, line, m, strerror(-r));
840 } else if (startswith_no_case(t, "Default-Start:")) {
845 k = delete_chars(t+14, WHITESPACE "-");
848 if (!(d = strdup(k))) {
853 free(s->sysv_runlevels);
854 s->sysv_runlevels = d;
857 } else if (startswith_no_case(t, "Description:")) {
860 state = LSB_DESCRIPTION;
862 if ((j = strstrip(t+12)) && *j) {
863 if (!(d = strdup(j))) {
870 free(long_description);
871 long_description = d;
873 } else if (startswith_no_case(t, "Short-Description:")) {
878 if ((j = strstrip(t+18)) && *j) {
879 if (!(d = strdup(j))) {
886 free(short_description);
887 short_description = d;
889 } else if (state == LSB_DESCRIPTION) {
891 if (startswith(l, "#\t") || startswith(l, "# ")) {
894 if ((j = strstrip(t)) && *j) {
897 if (long_description)
898 d = strjoin(long_description, " ", t, NULL);
907 free(long_description);
908 long_description = d;
917 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
920 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
921 /* If there a runlevels configured for this service
922 * but none of the standard ones, then we assume this
923 * is some special kind of service (which might be
924 * needed for early boot) and don't create any links
927 UNIT(s)->default_dependencies = false;
929 /* Don't timeout special services during boot (like fsck) */
930 s->timeout_start_usec = 0;
931 s->timeout_stop_usec = 0;
933 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
934 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
937 /* Special setting for all SysV services */
938 s->type = SERVICE_FORKING;
939 s->remain_after_exit = !s->pid_file;
940 s->guess_main_pid = false;
941 s->restart = SERVICE_RESTART_NO;
942 s->exec_context.ignore_sigpipe = false;
943 s->kill_context.kill_mode = KILL_PROCESS;
945 /* We use the long description only if
946 * no short description is set. */
948 if (short_description)
949 description = short_description;
950 else if (chkconfig_description)
951 description = chkconfig_description;
952 else if (long_description)
953 description = long_description;
960 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
968 /* The priority that has been set in /etc/rcN.d/ hierarchies
969 * takes precedence over what is stored as default in the LSB
971 if (s->sysv_start_priority_from_rcnd >= 0)
972 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
974 u->load_state = UNIT_LOADED;
982 free(short_description);
983 free(long_description);
984 free(chkconfig_description);
989 static int service_load_sysv_name(Service *s, const char *name) {
995 /* For SysV services we strip the rc.* and *.sh
996 * prefixes/suffixes. */
997 if (startswith(name, "rc.") ||
998 endswith(name, ".sh.service"))
1001 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1005 path = strjoin(*p, "/", name, NULL);
1009 assert(endswith(path, ".service"));
1010 path[strlen(path)-8] = 0;
1012 r = service_load_sysv_path(s, path);
1014 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1015 /* Try *.sh source'able init scripts */
1016 strcat(path, ".sh");
1017 r = service_load_sysv_path(s, path);
1021 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1022 /* Try rc.* init scripts */
1024 path = strjoin(*p, "/rc.", name, NULL);
1028 /* Drop .service suffix */
1029 path[strlen(path)-8] = 0;
1030 r = service_load_sysv_path(s, path);
1037 if (UNIT(s)->load_state != UNIT_STUB)
1044 static int service_load_sysv(Service *s) {
1051 /* Load service data from SysV init scripts, preferably with
1052 * LSB headers ... */
1054 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1057 if ((t = UNIT(s)->id))
1058 if ((r = service_load_sysv_name(s, t)) < 0)
1061 if (UNIT(s)->load_state == UNIT_STUB)
1062 SET_FOREACH(t, UNIT(s)->names, i) {
1063 if (t == UNIT(s)->id)
1066 if ((r = service_load_sysv_name(s, t)) < 0)
1069 if (UNIT(s)->load_state != UNIT_STUB)
1077 static int fsck_fix_order(Service *s) {
1083 if (s->fsck_passno <= 0)
1086 /* For each pair of services where both have an fsck priority
1087 * we order things based on it. */
1089 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1098 if (UNIT(t)->load_state != UNIT_LOADED)
1101 if (t->fsck_passno <= 0)
1104 if (t->fsck_passno < s->fsck_passno)
1106 else if (t->fsck_passno > s->fsck_passno)
1111 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1118 static int service_verify(Service *s) {
1121 if (UNIT(s)->load_state != UNIT_LOADED)
1124 if (!s->exec_command[SERVICE_EXEC_START]) {
1125 log_error_unit(UNIT(s)->id,
1126 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1130 if (s->type != SERVICE_ONESHOT &&
1131 s->exec_command[SERVICE_EXEC_START]->command_next) {
1132 log_error_unit(UNIT(s)->id,
1133 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1137 if (s->type == SERVICE_DBUS && !s->bus_name) {
1138 log_error_unit(UNIT(s)->id,
1139 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1143 if (s->bus_name && s->type != SERVICE_DBUS)
1144 log_warning_unit(UNIT(s)->id,
1145 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1147 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1148 log_error_unit(UNIT(s)->id,
1149 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1156 static int service_add_default_dependencies(Service *s) {
1161 /* Add a number of automatic dependencies useful for the
1162 * majority of services. */
1164 /* First, pull in base system */
1165 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1167 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1170 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1172 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1176 /* Second, activate normal shutdown */
1177 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1180 static void service_fix_output(Service *s) {
1183 /* If nothing has been explicitly configured, patch default
1184 * output in. If input is socket/tty we avoid this however,
1185 * since in that case we want output to default to the same
1186 * place as we read input from. */
1188 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1189 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1190 s->exec_context.std_input == EXEC_INPUT_NULL)
1191 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1193 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1194 s->exec_context.std_input == EXEC_INPUT_NULL)
1195 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1198 static int service_load(Unit *u) {
1200 Service *s = SERVICE(u);
1204 /* Load a .service file */
1205 if ((r = unit_load_fragment(u)) < 0)
1208 #ifdef HAVE_SYSV_COMPAT
1209 /* Load a classic init script as a fallback, if we couldn't find anything */
1210 if (u->load_state == UNIT_STUB)
1211 if ((r = service_load_sysv(s)) < 0)
1215 /* Still nothing found? Then let's give up */
1216 if (u->load_state == UNIT_STUB)
1219 /* We were able to load something, then let's add in the
1220 * dropin directories. */
1221 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1224 /* This is a new unit? Then let's add in some extras */
1225 if (u->load_state == UNIT_LOADED) {
1226 if (s->type == _SERVICE_TYPE_INVALID)
1227 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1229 /* Oneshot services have disabled start timeout by default */
1230 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1231 s->timeout_start_usec = 0;
1233 service_fix_output(s);
1235 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1238 if ((r = unit_add_default_cgroups(u)) < 0)
1241 #ifdef HAVE_SYSV_COMPAT
1242 if ((r = sysv_fix_order(s)) < 0)
1246 if ((r = fsck_fix_order(s)) < 0)
1250 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1253 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1254 s->notify_access = NOTIFY_MAIN;
1256 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1257 s->notify_access = NOTIFY_MAIN;
1259 if (s->type == SERVICE_DBUS || s->bus_name)
1260 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1263 if (UNIT(s)->default_dependencies)
1264 if ((r = service_add_default_dependencies(s)) < 0)
1267 r = unit_exec_context_defaults(u, &s->exec_context);
1272 return service_verify(s);
1275 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1277 ServiceExecCommand c;
1278 Service *s = SERVICE(u);
1279 const char *prefix2;
1284 p2 = strappend(prefix, "\t");
1285 prefix2 = p2 ? p2 : prefix;
1288 "%sService State: %s\n"
1290 "%sReload Result: %s\n"
1291 "%sPermissionsStartOnly: %s\n"
1292 "%sRootDirectoryStartOnly: %s\n"
1293 "%sRemainAfterExit: %s\n"
1294 "%sGuessMainPID: %s\n"
1297 "%sNotifyAccess: %s\n",
1298 prefix, service_state_to_string(s->state),
1299 prefix, service_result_to_string(s->result),
1300 prefix, service_result_to_string(s->reload_result),
1301 prefix, yes_no(s->permissions_start_only),
1302 prefix, yes_no(s->root_directory_start_only),
1303 prefix, yes_no(s->remain_after_exit),
1304 prefix, yes_no(s->guess_main_pid),
1305 prefix, service_type_to_string(s->type),
1306 prefix, service_restart_to_string(s->restart),
1307 prefix, notify_access_to_string(s->notify_access));
1309 if (s->control_pid > 0)
1311 "%sControl PID: %lu\n",
1312 prefix, (unsigned long) s->control_pid);
1314 if (s->main_pid > 0)
1317 "%sMain PID Known: %s\n"
1318 "%sMain PID Alien: %s\n",
1319 prefix, (unsigned long) s->main_pid,
1320 prefix, yes_no(s->main_pid_known),
1321 prefix, yes_no(s->main_pid_alien));
1326 prefix, s->pid_file);
1331 "%sBus Name Good: %s\n",
1332 prefix, s->bus_name,
1333 prefix, yes_no(s->bus_name_good));
1335 kill_context_dump(&s->kill_context, f, prefix);
1336 exec_context_dump(&s->exec_context, f, prefix);
1338 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1340 if (!s->exec_command[c])
1343 fprintf(f, "%s-> %s:\n",
1344 prefix, service_exec_command_to_string(c));
1346 exec_command_dump_list(s->exec_command[c], f, prefix2);
1349 #ifdef HAVE_SYSV_COMPAT
1352 "%sSysV Init Script has LSB Header: %s\n"
1353 "%sSysVEnabled: %s\n",
1354 prefix, yes_no(s->sysv_has_lsb),
1355 prefix, yes_no(s->sysv_enabled));
1357 if (s->sysv_start_priority >= 0)
1359 "%sSysVStartPriority: %i\n",
1360 prefix, s->sysv_start_priority);
1362 if (s->sysv_runlevels)
1363 fprintf(f, "%sSysVRunLevels: %s\n",
1364 prefix, s->sysv_runlevels);
1367 if (s->fsck_passno > 0)
1369 "%sFsckPassNo: %i\n",
1370 prefix, s->fsck_passno);
1373 fprintf(f, "%sStatus Text: %s\n",
1374 prefix, s->status_text);
1379 static int service_load_pid_file(Service *s, bool may_warn) {
1389 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1391 log_info_unit(UNIT(s)->id,
1392 "PID file %s not readable (yet?) after %s.",
1393 s->pid_file, service_state_to_string(s->state));
1397 r = parse_pid(k, &pid);
1403 if (kill(pid, 0) < 0 && errno != EPERM) {
1405 log_info_unit(UNIT(s)->id,
1406 "PID %lu read from file %s does not exist.",
1407 (unsigned long) pid, s->pid_file);
1411 if (s->main_pid_known) {
1412 if (pid == s->main_pid)
1415 log_debug_unit(UNIT(s)->id,
1416 "Main PID changing: %lu -> %lu",
1417 (unsigned long) s->main_pid, (unsigned long) pid);
1418 service_unwatch_main_pid(s);
1419 s->main_pid_known = false;
1421 log_debug_unit(UNIT(s)->id,
1422 "Main PID loaded: %lu", (unsigned long) pid);
1424 if ((r = service_set_main_pid(s, pid)) < 0)
1427 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1428 /* FIXME: we need to do something here */
1434 static int service_search_main_pid(Service *s) {
1440 /* If we know it anyway, don't ever fallback to unreliable
1442 if (s->main_pid_known)
1445 if (!s->guess_main_pid)
1448 assert(s->main_pid <= 0);
1450 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1453 log_debug_unit(UNIT(s)->id,
1454 "Main PID guessed: %lu", (unsigned long) pid);
1455 if ((r = service_set_main_pid(s, pid)) < 0)
1458 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1459 /* FIXME: we need to do something here */
1465 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1471 /* Notifies all our sockets when we die */
1473 if (s->socket_fd >= 0)
1476 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1477 if (u->type == UNIT_SOCKET)
1478 socket_notify_service_dead(SOCKET(u), failed_permanent);
1483 static void service_set_state(Service *s, ServiceState state) {
1484 ServiceState old_state;
1485 const UnitActiveState *table;
1488 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1490 old_state = s->state;
1493 service_unwatch_pid_file(s);
1495 if (state != SERVICE_START_PRE &&
1496 state != SERVICE_START &&
1497 state != SERVICE_START_POST &&
1498 state != SERVICE_RELOAD &&
1499 state != SERVICE_STOP &&
1500 state != SERVICE_STOP_SIGTERM &&
1501 state != SERVICE_STOP_SIGKILL &&
1502 state != SERVICE_STOP_POST &&
1503 state != SERVICE_FINAL_SIGTERM &&
1504 state != SERVICE_FINAL_SIGKILL &&
1505 state != SERVICE_AUTO_RESTART)
1506 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1508 if (state != SERVICE_START &&
1509 state != SERVICE_START_POST &&
1510 state != SERVICE_RUNNING &&
1511 state != SERVICE_RELOAD &&
1512 state != SERVICE_STOP &&
1513 state != SERVICE_STOP_SIGTERM &&
1514 state != SERVICE_STOP_SIGKILL) {
1515 service_unwatch_main_pid(s);
1516 s->main_command = NULL;
1519 if (state != SERVICE_START_PRE &&
1520 state != SERVICE_START &&
1521 state != SERVICE_START_POST &&
1522 state != SERVICE_RELOAD &&
1523 state != SERVICE_STOP &&
1524 state != SERVICE_STOP_SIGTERM &&
1525 state != SERVICE_STOP_SIGKILL &&
1526 state != SERVICE_STOP_POST &&
1527 state != SERVICE_FINAL_SIGTERM &&
1528 state != SERVICE_FINAL_SIGKILL) {
1529 service_unwatch_control_pid(s);
1530 s->control_command = NULL;
1531 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1534 if (state == SERVICE_DEAD ||
1535 state == SERVICE_STOP ||
1536 state == SERVICE_STOP_SIGTERM ||
1537 state == SERVICE_STOP_SIGKILL ||
1538 state == SERVICE_STOP_POST ||
1539 state == SERVICE_FINAL_SIGTERM ||
1540 state == SERVICE_FINAL_SIGKILL ||
1541 state == SERVICE_FAILED ||
1542 state == SERVICE_AUTO_RESTART)
1543 service_notify_sockets_dead(s, false);
1545 if (state != SERVICE_START_PRE &&
1546 state != SERVICE_START &&
1547 state != SERVICE_START_POST &&
1548 state != SERVICE_RUNNING &&
1549 state != SERVICE_RELOAD &&
1550 state != SERVICE_STOP &&
1551 state != SERVICE_STOP_SIGTERM &&
1552 state != SERVICE_STOP_SIGKILL &&
1553 state != SERVICE_STOP_POST &&
1554 state != SERVICE_FINAL_SIGTERM &&
1555 state != SERVICE_FINAL_SIGKILL &&
1556 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1557 service_close_socket_fd(s);
1558 service_connection_unref(s);
1561 if (state == SERVICE_STOP)
1562 service_stop_watchdog(s);
1564 /* For the inactive states unit_notify() will trim the cgroup,
1565 * but for exit we have to do that ourselves... */
1566 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1567 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1569 if (old_state != state)
1570 log_debug_unit(UNIT(s)->id,
1571 "%s changed %s -> %s", UNIT(s)->id,
1572 service_state_to_string(old_state),
1573 service_state_to_string(state));
1575 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1576 s->reload_result = SERVICE_SUCCESS;
1579 static int service_coldplug(Unit *u) {
1580 Service *s = SERVICE(u);
1584 assert(s->state == SERVICE_DEAD);
1586 if (s->deserialized_state != s->state) {
1588 if (s->deserialized_state == SERVICE_START_PRE ||
1589 s->deserialized_state == SERVICE_START ||
1590 s->deserialized_state == SERVICE_START_POST ||
1591 s->deserialized_state == SERVICE_RELOAD ||
1592 s->deserialized_state == SERVICE_STOP ||
1593 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1594 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1595 s->deserialized_state == SERVICE_STOP_POST ||
1596 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1597 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1598 s->deserialized_state == SERVICE_AUTO_RESTART) {
1599 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1602 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1604 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
1610 if ((s->deserialized_state == SERVICE_START &&
1611 (s->type == SERVICE_FORKING ||
1612 s->type == SERVICE_DBUS ||
1613 s->type == SERVICE_ONESHOT ||
1614 s->type == SERVICE_NOTIFY)) ||
1615 s->deserialized_state == SERVICE_START_POST ||
1616 s->deserialized_state == SERVICE_RUNNING ||
1617 s->deserialized_state == SERVICE_RELOAD ||
1618 s->deserialized_state == SERVICE_STOP ||
1619 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1620 s->deserialized_state == SERVICE_STOP_SIGKILL)
1621 if (s->main_pid > 0)
1622 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1625 if (s->deserialized_state == SERVICE_START_PRE ||
1626 s->deserialized_state == SERVICE_START ||
1627 s->deserialized_state == SERVICE_START_POST ||
1628 s->deserialized_state == SERVICE_RELOAD ||
1629 s->deserialized_state == SERVICE_STOP ||
1630 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1631 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1632 s->deserialized_state == SERVICE_STOP_POST ||
1633 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1634 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1635 if (s->control_pid > 0)
1636 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1639 if (s->deserialized_state == SERVICE_START_POST ||
1640 s->deserialized_state == SERVICE_RUNNING)
1641 service_handle_watchdog(s);
1643 service_set_state(s, s->deserialized_state);
1648 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1652 unsigned rn_fds = 0;
1659 if (s->socket_fd >= 0)
1662 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1667 if (u->type != UNIT_SOCKET)
1672 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1684 if (!(t = new(int, rn_fds+cn_fds))) {
1690 memcpy(t, rfds, rn_fds * sizeof(int));
1691 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1696 rn_fds = rn_fds+cn_fds;
1711 static int service_spawn(
1716 bool apply_permissions,
1718 bool apply_tty_stdin,
1719 bool set_notify_socket,
1725 int *fds = NULL, *fdsbuf = NULL;
1726 unsigned n_fds = 0, n_env = 0;
1727 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1734 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1735 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1736 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1738 if (s->socket_fd >= 0) {
1739 fds = &s->socket_fd;
1742 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1749 if (timeout && s->timeout_start_usec) {
1750 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_start_usec, &s->timer_watch);
1754 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1756 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1761 our_env = new0(char*, 5);
1767 if (set_notify_socket)
1768 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1773 if (s->main_pid > 0)
1774 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1779 if (s->watchdog_usec > 0)
1780 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1785 if (s->meta.manager->running_as != SYSTEMD_SYSTEM)
1786 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1791 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1805 UNIT(s)->manager->confirm_spawn,
1806 UNIT(s)->cgroup_bondings,
1807 UNIT(s)->cgroup_attributes,
1808 is_control ? "control" : NULL,
1810 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1816 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1817 /* FIXME: we need to do something here */
1823 strv_free(final_env);
1833 strv_free(final_env);
1836 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1841 static int main_pid_good(Service *s) {
1844 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1847 /* If we know the pid file, then lets just check if it is
1849 if (s->main_pid_known) {
1851 /* If it's an alien child let's check if it is still
1853 if (s->main_pid_alien)
1854 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1856 /* .. otherwise assume we'll get a SIGCHLD for it,
1857 * which we really should wait for to collect exit
1858 * status and code */
1859 return s->main_pid > 0;
1862 /* We don't know the pid */
1866 static int control_pid_good(Service *s) {
1869 return s->control_pid > 0;
1872 static int cgroup_good(Service *s) {
1877 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1883 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1887 if (f != SERVICE_SUCCESS)
1890 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1892 if (allow_restart &&
1893 !s->forbid_restart &&
1894 (s->restart == SERVICE_RESTART_ALWAYS ||
1895 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1896 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1897 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1898 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1899 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1900 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1901 (s->result != SERVICE_FAILURE_SIGNAL ||
1902 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1905 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
1909 service_set_state(s, SERVICE_AUTO_RESTART);
1912 s->forbid_restart = false;
1917 log_warning_unit(UNIT(s)->id,
1918 "%s failed to run install restart timer: %s",
1919 UNIT(s)->id, strerror(-r));
1920 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1923 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1925 static void service_enter_stop_post(Service *s, ServiceResult f) {
1929 if (f != SERVICE_SUCCESS)
1932 service_unwatch_control_pid(s);
1934 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1935 s->control_command_id = SERVICE_EXEC_STOP_POST;
1937 r = service_spawn(s,
1941 !s->permissions_start_only,
1942 !s->root_directory_start_only,
1951 service_set_state(s, SERVICE_STOP_POST);
1953 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1958 log_warning_unit(UNIT(s)->id,
1959 "%s failed to run 'stop-post' task: %s",
1960 UNIT(s)->id, strerror(-r));
1961 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1964 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1966 Set *pid_set = NULL;
1967 bool wait_for_exit = false;
1971 if (f != SERVICE_SUCCESS)
1974 if (s->kill_context.kill_mode != KILL_NONE) {
1975 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->kill_context.kill_signal : SIGKILL;
1977 if (s->main_pid > 0) {
1978 if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
1979 log_warning_unit(UNIT(s)->id,
1980 "Failed to kill main process %li: %m", (long) s->main_pid);
1982 wait_for_exit = !s->main_pid_alien;
1985 if (s->control_pid > 0) {
1986 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1987 log_warning_unit(UNIT(s)->id,
1988 "Failed to kill control process %li: %m", (long) s->control_pid);
1990 wait_for_exit = true;
1993 if (s->kill_context.kill_mode == KILL_CONTROL_GROUP) {
1995 pid_set = set_new(trivial_hash_func, trivial_compare_func);
2001 /* Exclude the main/control pids from being killed via the cgroup */
2002 if (s->main_pid > 0)
2003 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
2006 if (s->control_pid > 0)
2007 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
2010 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
2012 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
2013 log_warning_unit(UNIT(s)->id,
2014 "Failed to kill control group: %s", strerror(-r));
2016 wait_for_exit = true;
2023 if (wait_for_exit) {
2024 if (s->timeout_stop_usec > 0) {
2025 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_stop_usec, &s->timer_watch);
2030 service_set_state(s, state);
2031 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2032 service_enter_stop_post(s, SERVICE_SUCCESS);
2034 service_enter_dead(s, SERVICE_SUCCESS, true);
2039 log_warning_unit(UNIT(s)->id,
2040 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2042 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2043 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2045 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2051 static void service_enter_stop(Service *s, ServiceResult f) {
2056 if (f != SERVICE_SUCCESS)
2059 service_unwatch_control_pid(s);
2061 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2062 s->control_command_id = SERVICE_EXEC_STOP;
2064 r = service_spawn(s,
2068 !s->permissions_start_only,
2069 !s->root_directory_start_only,
2077 service_set_state(s, SERVICE_STOP);
2079 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2084 log_warning_unit(UNIT(s)->id,
2085 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2086 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2089 static void service_enter_running(Service *s, ServiceResult f) {
2090 int main_pid_ok, cgroup_ok;
2093 if (f != SERVICE_SUCCESS)
2096 main_pid_ok = main_pid_good(s);
2097 cgroup_ok = cgroup_good(s);
2099 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2100 (s->bus_name_good || s->type != SERVICE_DBUS))
2101 service_set_state(s, SERVICE_RUNNING);
2102 else if (s->remain_after_exit)
2103 service_set_state(s, SERVICE_EXITED);
2105 service_enter_stop(s, SERVICE_SUCCESS);
2108 static void service_enter_start_post(Service *s) {
2112 service_unwatch_control_pid(s);
2114 if (s->watchdog_usec > 0)
2115 service_reset_watchdog(s);
2117 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2118 s->control_command_id = SERVICE_EXEC_START_POST;
2120 r = service_spawn(s,
2124 !s->permissions_start_only,
2125 !s->root_directory_start_only,
2133 service_set_state(s, SERVICE_START_POST);
2135 service_enter_running(s, SERVICE_SUCCESS);
2140 log_warning_unit(UNIT(s)->id,
2141 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2142 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2145 static void service_enter_start(Service *s) {
2152 assert(s->exec_command[SERVICE_EXEC_START]);
2153 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2155 if (s->type == SERVICE_FORKING)
2156 service_unwatch_control_pid(s);
2158 service_unwatch_main_pid(s);
2160 /* We want to ensure that nobody leaks processes from
2161 * START_PRE here, so let's go on a killing spree, People
2162 * should not spawn long running processes from START_PRE. */
2163 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2165 if (s->type == SERVICE_FORKING) {
2166 s->control_command_id = SERVICE_EXEC_START;
2167 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2169 s->main_command = NULL;
2171 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2172 s->control_command = NULL;
2174 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2177 r = service_spawn(s,
2179 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2184 s->notify_access != NOTIFY_NONE,
2190 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2191 /* For simple services we immediately start
2192 * the START_POST binaries. */
2194 service_set_main_pid(s, pid);
2195 service_enter_start_post(s);
2197 } else if (s->type == SERVICE_FORKING) {
2199 /* For forking services we wait until the start
2200 * process exited. */
2202 s->control_pid = pid;
2203 service_set_state(s, SERVICE_START);
2205 } else if (s->type == SERVICE_ONESHOT ||
2206 s->type == SERVICE_DBUS ||
2207 s->type == SERVICE_NOTIFY) {
2209 /* For oneshot services we wait until the start
2210 * process exited, too, but it is our main process. */
2212 /* For D-Bus services we know the main pid right away,
2213 * but wait for the bus name to appear on the
2214 * bus. Notify services are similar. */
2216 service_set_main_pid(s, pid);
2217 service_set_state(s, SERVICE_START);
2219 assert_not_reached("Unknown service type");
2224 log_warning_unit(UNIT(s)->id,
2225 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2226 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2229 static void service_enter_start_pre(Service *s) {
2234 service_unwatch_control_pid(s);
2236 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2238 /* Before we start anything, let's clear up what might
2239 * be left from previous runs. */
2240 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2242 s->control_command_id = SERVICE_EXEC_START_PRE;
2244 r = service_spawn(s,
2248 !s->permissions_start_only,
2249 !s->root_directory_start_only,
2257 service_set_state(s, SERVICE_START_PRE);
2259 service_enter_start(s);
2264 log_warning_unit(UNIT(s)->id,
2265 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2266 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2269 static void service_enter_restart(Service *s) {
2274 dbus_error_init(&error);
2276 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2277 /* Don't restart things if we are going down anyway */
2278 log_info_unit(UNIT(s)->id,
2279 "Stop job pending for unit, delaying automatic restart.");
2281 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
2288 /* Any units that are bound to this service must also be
2289 * restarted. We use JOB_RESTART (instead of the more obvious
2290 * JOB_START) here so that those dependency jobs will be added
2292 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2296 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2297 * it will be canceled as part of the service_stop() call that
2298 * is executed as part of JOB_RESTART. */
2300 log_debug_unit(UNIT(s)->id,
2301 "%s scheduled restart job.", UNIT(s)->id);
2305 log_warning_unit(UNIT(s)->id,
2306 "%s failed to schedule restart job: %s",
2307 UNIT(s)->id, bus_error(&error, -r));
2308 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2310 dbus_error_free(&error);
2313 static void service_enter_reload(Service *s) {
2318 service_unwatch_control_pid(s);
2320 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2321 s->control_command_id = SERVICE_EXEC_RELOAD;
2323 r = service_spawn(s,
2327 !s->permissions_start_only,
2328 !s->root_directory_start_only,
2336 service_set_state(s, SERVICE_RELOAD);
2338 service_enter_running(s, SERVICE_SUCCESS);
2343 log_warning_unit(UNIT(s)->id,
2344 "%s failed to run 'reload' task: %s",
2345 UNIT(s)->id, strerror(-r));
2346 s->reload_result = SERVICE_FAILURE_RESOURCES;
2347 service_enter_running(s, SERVICE_SUCCESS);
2350 static void service_run_next_control(Service *s) {
2354 assert(s->control_command);
2355 assert(s->control_command->command_next);
2357 assert(s->control_command_id != SERVICE_EXEC_START);
2359 s->control_command = s->control_command->command_next;
2360 service_unwatch_control_pid(s);
2362 r = service_spawn(s,
2366 !s->permissions_start_only,
2367 !s->root_directory_start_only,
2368 s->control_command_id == SERVICE_EXEC_START_PRE ||
2369 s->control_command_id == SERVICE_EXEC_STOP_POST,
2379 log_warning_unit(UNIT(s)->id,
2380 "%s failed to run next control task: %s",
2381 UNIT(s)->id, strerror(-r));
2383 if (s->state == SERVICE_START_PRE)
2384 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2385 else if (s->state == SERVICE_STOP)
2386 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2387 else if (s->state == SERVICE_STOP_POST)
2388 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2389 else if (s->state == SERVICE_RELOAD) {
2390 s->reload_result = SERVICE_FAILURE_RESOURCES;
2391 service_enter_running(s, SERVICE_SUCCESS);
2393 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2396 static void service_run_next_main(Service *s) {
2401 assert(s->main_command);
2402 assert(s->main_command->command_next);
2403 assert(s->type == SERVICE_ONESHOT);
2405 s->main_command = s->main_command->command_next;
2406 service_unwatch_main_pid(s);
2408 r = service_spawn(s,
2415 s->notify_access != NOTIFY_NONE,
2421 service_set_main_pid(s, pid);
2426 log_warning_unit(UNIT(s)->id,
2427 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2428 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2431 static int service_start_limit_test(Service *s) {
2434 if (ratelimit_test(&s->start_limit))
2437 switch (s->start_limit_action) {
2439 case SERVICE_START_LIMIT_NONE:
2440 log_warning_unit(UNIT(s)->id,
2441 "%s start request repeated too quickly, refusing to start.",
2445 case SERVICE_START_LIMIT_REBOOT: {
2449 dbus_error_init(&error);
2451 log_warning_unit(UNIT(s)->id,
2452 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2454 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2456 log_error_unit(UNIT(s)->id,
2457 "Failed to reboot: %s.", bus_error(&error, r));
2458 dbus_error_free(&error);
2464 case SERVICE_START_LIMIT_REBOOT_FORCE:
2465 log_warning_unit(UNIT(s)->id,
2466 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2467 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2470 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2471 log_warning_unit(UNIT(s)->id,
2472 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2474 reboot(RB_AUTOBOOT);
2478 log_error_unit(UNIT(s)->id,
2479 "start limit action=%i", s->start_limit_action);
2480 assert_not_reached("Unknown StartLimitAction.");
2486 static int service_start(Unit *u) {
2487 Service *s = SERVICE(u);
2492 /* We cannot fulfill this request right now, try again later
2494 if (s->state == SERVICE_STOP ||
2495 s->state == SERVICE_STOP_SIGTERM ||
2496 s->state == SERVICE_STOP_SIGKILL ||
2497 s->state == SERVICE_STOP_POST ||
2498 s->state == SERVICE_FINAL_SIGTERM ||
2499 s->state == SERVICE_FINAL_SIGKILL)
2502 /* Already on it! */
2503 if (s->state == SERVICE_START_PRE ||
2504 s->state == SERVICE_START ||
2505 s->state == SERVICE_START_POST)
2508 /* A service that will be restarted must be stopped first to
2509 * trigger BindsTo and/or OnFailure dependencies. If a user
2510 * does not want to wait for the holdoff time to elapse, the
2511 * service should be manually restarted, not started. We
2512 * simply return EAGAIN here, so that any start jobs stay
2513 * queued, and assume that the auto restart timer will
2514 * eventually trigger the restart. */
2515 if (s->state == SERVICE_AUTO_RESTART)
2518 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2520 /* Make sure we don't enter a busy loop of some kind. */
2521 r = service_start_limit_test(s);
2523 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2527 s->result = SERVICE_SUCCESS;
2528 s->reload_result = SERVICE_SUCCESS;
2529 s->main_pid_known = false;
2530 s->main_pid_alien = false;
2531 s->forbid_restart = false;
2533 service_enter_start_pre(s);
2537 static int service_stop(Unit *u) {
2538 Service *s = SERVICE(u);
2542 /* Don't create restart jobs from here. */
2543 s->forbid_restart = true;
2546 if (s->state == SERVICE_STOP ||
2547 s->state == SERVICE_STOP_SIGTERM ||
2548 s->state == SERVICE_STOP_SIGKILL ||
2549 s->state == SERVICE_STOP_POST ||
2550 s->state == SERVICE_FINAL_SIGTERM ||
2551 s->state == SERVICE_FINAL_SIGKILL)
2554 /* A restart will be scheduled or is in progress. */
2555 if (s->state == SERVICE_AUTO_RESTART) {
2556 service_set_state(s, SERVICE_DEAD);
2560 /* If there's already something running we go directly into
2562 if (s->state == SERVICE_START_PRE ||
2563 s->state == SERVICE_START ||
2564 s->state == SERVICE_START_POST ||
2565 s->state == SERVICE_RELOAD) {
2566 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2570 assert(s->state == SERVICE_RUNNING ||
2571 s->state == SERVICE_EXITED);
2573 service_enter_stop(s, SERVICE_SUCCESS);
2577 static int service_reload(Unit *u) {
2578 Service *s = SERVICE(u);
2582 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2584 service_enter_reload(s);
2588 static bool service_can_reload(Unit *u) {
2589 Service *s = SERVICE(u);
2593 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2596 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2597 Service *s = SERVICE(u);
2603 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2604 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2605 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2607 if (s->control_pid > 0)
2608 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2610 if (s->main_pid_known && s->main_pid > 0)
2611 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2613 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2616 unit_serialize_item(u, f, "status-text", s->status_text);
2618 /* FIXME: There's a minor uncleanliness here: if there are
2619 * multiple commands attached here, we will start from the
2620 * first one again */
2621 if (s->control_command_id >= 0)
2622 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2624 if (s->socket_fd >= 0) {
2627 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2630 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2633 if (s->main_exec_status.pid > 0) {
2634 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2635 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2636 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2638 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2639 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2640 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2643 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2644 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2649 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2650 Service *s = SERVICE(u);
2657 if (streq(key, "state")) {
2660 if ((state = service_state_from_string(value)) < 0)
2661 log_debug_unit(u->id, "Failed to parse state value %s", value);
2663 s->deserialized_state = state;
2664 } else if (streq(key, "result")) {
2667 f = service_result_from_string(value);
2669 log_debug_unit(u->id, "Failed to parse result value %s", value);
2670 else if (f != SERVICE_SUCCESS)
2673 } else if (streq(key, "reload-result")) {
2676 f = service_result_from_string(value);
2678 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
2679 else if (f != SERVICE_SUCCESS)
2680 s->reload_result = f;
2682 } else if (streq(key, "control-pid")) {
2685 if (parse_pid(value, &pid) < 0)
2686 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
2688 s->control_pid = pid;
2689 } else if (streq(key, "main-pid")) {
2692 if (parse_pid(value, &pid) < 0)
2693 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
2695 service_set_main_pid(s, (pid_t) pid);
2696 } else if (streq(key, "main-pid-known")) {
2699 if ((b = parse_boolean(value)) < 0)
2700 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
2702 s->main_pid_known = b;
2703 } else if (streq(key, "status-text")) {
2706 if ((t = strdup(value))) {
2707 free(s->status_text);
2711 } else if (streq(key, "control-command")) {
2712 ServiceExecCommand id;
2714 if ((id = service_exec_command_from_string(value)) < 0)
2715 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
2717 s->control_command_id = id;
2718 s->control_command = s->exec_command[id];
2720 } else if (streq(key, "socket-fd")) {
2723 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2724 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
2727 if (s->socket_fd >= 0)
2728 close_nointr_nofail(s->socket_fd);
2729 s->socket_fd = fdset_remove(fds, fd);
2731 } else if (streq(key, "main-exec-status-pid")) {
2734 if (parse_pid(value, &pid) < 0)
2735 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
2737 s->main_exec_status.pid = pid;
2738 } else if (streq(key, "main-exec-status-code")) {
2741 if (safe_atoi(value, &i) < 0)
2742 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
2744 s->main_exec_status.code = i;
2745 } else if (streq(key, "main-exec-status-status")) {
2748 if (safe_atoi(value, &i) < 0)
2749 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
2751 s->main_exec_status.status = i;
2752 } else if (streq(key, "main-exec-status-start"))
2753 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2754 else if (streq(key, "main-exec-status-exit"))
2755 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2756 else if (streq(key, "watchdog-timestamp"))
2757 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2759 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2764 static UnitActiveState service_active_state(Unit *u) {
2765 const UnitActiveState *table;
2769 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2771 return table[SERVICE(u)->state];
2774 static const char *service_sub_state_to_string(Unit *u) {
2777 return service_state_to_string(SERVICE(u)->state);
2780 static bool service_check_gc(Unit *u) {
2781 Service *s = SERVICE(u);
2785 /* Never clean up services that still have a process around,
2786 * even if the service is formally dead. */
2787 if (cgroup_good(s) > 0 ||
2788 main_pid_good(s) > 0 ||
2789 control_pid_good(s) > 0)
2792 #ifdef HAVE_SYSV_COMPAT
2800 static bool service_check_snapshot(Unit *u) {
2801 Service *s = SERVICE(u);
2805 return !s->got_socket_fd;
2808 static int service_retry_pid_file(Service *s) {
2811 assert(s->pid_file);
2812 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2814 r = service_load_pid_file(s, false);
2818 service_unwatch_pid_file(s);
2820 service_enter_running(s, SERVICE_SUCCESS);
2824 static int service_watch_pid_file(Service *s) {
2827 log_debug_unit(UNIT(s)->id,
2828 "Setting watch for %s's PID file %s",
2829 UNIT(s)->id, s->pid_file_pathspec->path);
2830 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2834 /* the pidfile might have appeared just before we set the watch */
2835 service_retry_pid_file(s);
2839 log_error_unit(UNIT(s)->id,
2840 "Failed to set a watch for %s's PID file %s: %s",
2841 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2842 service_unwatch_pid_file(s);
2846 static int service_demand_pid_file(Service *s) {
2849 assert(s->pid_file);
2850 assert(!s->pid_file_pathspec);
2852 ps = new0(PathSpec, 1);
2856 ps->path = strdup(s->pid_file);
2862 path_kill_slashes(ps->path);
2864 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2865 * keep their PID file open all the time. */
2866 ps->type = PATH_MODIFIED;
2867 ps->inotify_fd = -1;
2869 s->pid_file_pathspec = ps;
2871 return service_watch_pid_file(s);
2874 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2875 Service *s = SERVICE(u);
2879 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2880 assert(s->pid_file_pathspec);
2881 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2883 log_debug_unit(u->id, "inotify event for %s", u->id);
2885 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2888 if (service_retry_pid_file(s) == 0)
2891 if (service_watch_pid_file(s) < 0)
2896 service_unwatch_pid_file(s);
2897 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2900 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2901 Service *s = SERVICE(u);
2907 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2908 is_clean_exit_lsb(code, status, &s->success_status))
2909 f = SERVICE_SUCCESS;
2910 else if (code == CLD_EXITED)
2911 f = SERVICE_FAILURE_EXIT_CODE;
2912 else if (code == CLD_KILLED)
2913 f = SERVICE_FAILURE_SIGNAL;
2914 else if (code == CLD_DUMPED)
2915 f = SERVICE_FAILURE_CORE_DUMP;
2917 assert_not_reached("Unknown code");
2919 if (s->main_pid == pid) {
2920 /* Forking services may occasionally move to a new PID.
2921 * As long as they update the PID file before exiting the old
2922 * PID, they're fine. */
2923 if (service_load_pid_file(s, false) == 0)
2927 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2929 if (s->main_command) {
2930 /* If this is not a forking service than the
2931 * main process got started and hence we copy
2932 * the exit status so that it is recorded both
2933 * as main and as control process exit
2936 s->main_command->exec_status = s->main_exec_status;
2938 if (s->main_command->ignore)
2939 f = SERVICE_SUCCESS;
2940 } else if (s->exec_command[SERVICE_EXEC_START]) {
2942 /* If this is a forked process, then we should
2943 * ignore the return value if this was
2944 * configured for the starter process */
2946 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2947 f = SERVICE_SUCCESS;
2950 log_struct(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2951 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2952 u->id, sigchld_code_to_string(code), status,
2953 strna(code == CLD_EXITED
2954 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2955 : signal_to_string(status)),
2957 "EXIT_CODE=%s", sigchld_code_to_string(code),
2958 "EXIT_STATUS=%i", status,
2961 if (f != SERVICE_SUCCESS)
2964 if (s->main_command &&
2965 s->main_command->command_next &&
2966 f == SERVICE_SUCCESS) {
2968 /* There is another command to *
2969 * execute, so let's do that. */
2971 log_debug_unit(u->id,
2972 "%s running next main command for state %s",
2973 u->id, service_state_to_string(s->state));
2974 service_run_next_main(s);
2978 /* The service exited, so the service is officially
2980 s->main_command = NULL;
2984 case SERVICE_START_POST:
2985 case SERVICE_RELOAD:
2987 /* Need to wait until the operation is
2992 if (s->type == SERVICE_ONESHOT) {
2993 /* This was our main goal, so let's go on */
2994 if (f == SERVICE_SUCCESS)
2995 service_enter_start_post(s);
2997 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3003 case SERVICE_RUNNING:
3004 service_enter_running(s, f);
3007 case SERVICE_STOP_SIGTERM:
3008 case SERVICE_STOP_SIGKILL:
3010 if (!control_pid_good(s))
3011 service_enter_stop_post(s, f);
3013 /* If there is still a control process, wait for that first */
3017 assert_not_reached("Uh, main process died at wrong time.");
3021 } else if (s->control_pid == pid) {
3025 if (s->control_command) {
3026 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
3028 if (s->control_command->ignore)
3029 f = SERVICE_SUCCESS;
3032 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
3033 "%s: control process exited, code=%s status=%i",
3034 u->id, sigchld_code_to_string(code), status);
3036 if (f != SERVICE_SUCCESS)
3039 /* Immediately get rid of the cgroup, so that the
3040 * kernel doesn't delay the cgroup empty messages for
3041 * the service cgroup any longer than necessary */
3042 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
3044 if (s->control_command &&
3045 s->control_command->command_next &&
3046 f == SERVICE_SUCCESS) {
3048 /* There is another command to *
3049 * execute, so let's do that. */
3051 log_debug_unit(u->id,
3052 "%s running next control command for state %s",
3053 u->id, service_state_to_string(s->state));
3054 service_run_next_control(s);
3057 /* No further commands for this step, so let's
3058 * figure out what to do next */
3060 s->control_command = NULL;
3061 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3063 log_debug_unit(u->id,
3064 "%s got final SIGCHLD for state %s",
3065 u->id, service_state_to_string(s->state));
3069 case SERVICE_START_PRE:
3070 if (f == SERVICE_SUCCESS)
3071 service_enter_start(s);
3073 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3077 if (s->type != SERVICE_FORKING)
3078 /* Maybe spurious event due to a reload that changed the type? */
3081 if (f != SERVICE_SUCCESS) {
3082 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3087 bool has_start_post;
3090 /* Let's try to load the pid file here if we can.
3091 * The PID file might actually be created by a START_POST
3092 * script. In that case don't worry if the loading fails. */
3094 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3095 r = service_load_pid_file(s, !has_start_post);
3096 if (!has_start_post && r < 0) {
3097 r = service_demand_pid_file(s);
3098 if (r < 0 || !cgroup_good(s))
3099 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3103 service_search_main_pid(s);
3105 service_enter_start_post(s);
3108 case SERVICE_START_POST:
3109 if (f != SERVICE_SUCCESS) {
3110 service_enter_stop(s, f);
3117 r = service_load_pid_file(s, true);
3119 r = service_demand_pid_file(s);
3120 if (r < 0 || !cgroup_good(s))
3121 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3125 service_search_main_pid(s);
3127 service_enter_running(s, SERVICE_SUCCESS);
3130 case SERVICE_RELOAD:
3131 if (f == SERVICE_SUCCESS) {
3132 service_load_pid_file(s, true);
3133 service_search_main_pid(s);
3136 s->reload_result = f;
3137 service_enter_running(s, SERVICE_SUCCESS);
3141 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3144 case SERVICE_STOP_SIGTERM:
3145 case SERVICE_STOP_SIGKILL:
3146 if (main_pid_good(s) <= 0)
3147 service_enter_stop_post(s, f);
3149 /* If there is still a service
3150 * process around, wait until
3151 * that one quit, too */
3154 case SERVICE_STOP_POST:
3155 case SERVICE_FINAL_SIGTERM:
3156 case SERVICE_FINAL_SIGKILL:
3157 service_enter_dead(s, f, true);
3161 assert_not_reached("Uh, control process died at wrong time.");
3166 /* Notify clients about changed exit status */
3167 unit_add_to_dbus_queue(u);
3170 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3171 Service *s = SERVICE(u);
3174 assert(elapsed == 1);
3176 if (w == &s->watchdog_watch) {
3177 service_handle_watchdog(s);
3181 assert(w == &s->timer_watch);
3185 case SERVICE_START_PRE:
3187 log_warning_unit(u->id,
3188 "%s operation timed out. Terminating.", u->id);
3189 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3192 case SERVICE_START_POST:
3193 log_warning_unit(u->id,
3194 "%s operation timed out. Stopping.", u->id);
3195 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3198 case SERVICE_RELOAD:
3199 log_warning_unit(u->id,
3200 "%s operation timed out. Stopping.", u->id);
3201 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3202 service_enter_running(s, SERVICE_SUCCESS);
3206 log_warning_unit(u->id,
3207 "%s stopping timed out. Terminating.", u->id);
3208 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3211 case SERVICE_STOP_SIGTERM:
3212 if (s->kill_context.send_sigkill) {
3213 log_warning_unit(u->id,
3214 "%s stopping timed out. Killing.", u->id);
3215 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3217 log_warning_unit(u->id,
3218 "%s stopping timed out. Skipping SIGKILL.", u->id);
3219 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3224 case SERVICE_STOP_SIGKILL:
3225 /* Uh, we sent a SIGKILL and it is still not gone?
3226 * Must be something we cannot kill, so let's just be
3227 * weirded out and continue */
3229 log_warning_unit(u->id,
3230 "%s still around after SIGKILL. Ignoring.", u->id);
3231 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3234 case SERVICE_STOP_POST:
3235 log_warning_unit(u->id,
3236 "%s stopping timed out (2). Terminating.", u->id);
3237 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3240 case SERVICE_FINAL_SIGTERM:
3241 if (s->kill_context.send_sigkill) {
3242 log_warning_unit(u->id,
3243 "%s stopping timed out (2). Killing.", u->id);
3244 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3246 log_warning_unit(u->id,
3247 "%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.",
3249 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3254 case SERVICE_FINAL_SIGKILL:
3255 log_warning_unit(u->id,
3256 "%s still around after SIGKILL (2). Entering failed mode.", u->id);
3257 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3260 case SERVICE_AUTO_RESTART:
3261 log_info_unit(u->id,
3262 "%s holdoff time over, scheduling restart.", u->id);
3263 service_enter_restart(s);
3267 assert_not_reached("Timeout at wrong time.");
3271 static void service_cgroup_notify_event(Unit *u) {
3272 Service *s = SERVICE(u);
3276 log_debug_unit(u->id,
3277 "%s: cgroup is empty", u->id);
3281 /* Waiting for SIGCHLD is usually more interesting,
3282 * because it includes return codes/signals. Which is
3283 * why we ignore the cgroup events for most cases,
3284 * except when we don't know pid which to expect the
3288 case SERVICE_START_POST:
3289 /* If we were hoping for the daemon to write its PID file,
3290 * we can give up now. */
3291 if (s->pid_file_pathspec) {
3292 log_warning_unit(u->id,
3293 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3294 service_unwatch_pid_file(s);
3295 if (s->state == SERVICE_START)
3296 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3298 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3302 case SERVICE_RUNNING:
3303 /* service_enter_running() will figure out what to do */
3304 service_enter_running(s, SERVICE_SUCCESS);
3307 case SERVICE_STOP_SIGTERM:
3308 case SERVICE_STOP_SIGKILL:
3310 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3311 service_enter_stop_post(s, SERVICE_SUCCESS);
3315 case SERVICE_FINAL_SIGTERM:
3316 case SERVICE_FINAL_SIGKILL:
3317 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3318 service_enter_dead(s, SERVICE_SUCCESS, true);
3327 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3328 Service *s = SERVICE(u);
3333 if (s->notify_access == NOTIFY_NONE) {
3334 log_warning_unit(u->id,
3335 "%s: Got notification message from PID %lu, but reception is disabled.",
3336 u->id, (unsigned long) pid);
3340 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3341 log_warning_unit(u->id,
3342 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3343 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3347 log_debug_unit(u->id,
3348 "%s: Got message", u->id);
3350 /* Interpret MAINPID= */
3351 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3352 (s->state == SERVICE_START ||
3353 s->state == SERVICE_START_POST ||
3354 s->state == SERVICE_RUNNING ||
3355 s->state == SERVICE_RELOAD)) {
3357 if (parse_pid(e + 8, &pid) < 0)
3358 log_warning_unit(u->id,
3359 "Failed to parse notification message %s", e);
3361 log_debug_unit(u->id,
3362 "%s: got %s", u->id, e);
3363 service_set_main_pid(s, pid);
3367 /* Interpret READY= */
3368 if (s->type == SERVICE_NOTIFY &&
3369 s->state == SERVICE_START &&
3370 strv_find(tags, "READY=1")) {
3371 log_debug_unit(u->id,
3372 "%s: got READY=1", u->id);
3374 service_enter_start_post(s);
3377 /* Interpret STATUS= */
3378 e = strv_find_prefix(tags, "STATUS=");
3384 if (!utf8_is_valid(e+7)) {
3385 log_warning_unit(u->id,
3386 "Status message in notification is not UTF-8 clean.");
3392 log_error_unit(u->id,
3393 "Failed to allocate string.");
3397 log_debug_unit(u->id,
3398 "%s: got %s", u->id, e);
3400 free(s->status_text);
3403 free(s->status_text);
3404 s->status_text = NULL;
3408 if (strv_find(tags, "WATCHDOG=1")) {
3409 log_debug_unit(u->id,
3410 "%s: got WATCHDOG=1", u->id);
3411 service_reset_watchdog(s);
3414 /* Notify clients about changed status or main pid */
3415 unit_add_to_dbus_queue(u);
3418 #ifdef HAVE_SYSV_COMPAT
3420 static int service_enumerate(Manager *m) {
3424 char *path = NULL, *fpath = NULL, *name = NULL;
3425 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3432 if (m->running_as != SYSTEMD_SYSTEM)
3435 zero(runlevel_services);
3437 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3438 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3442 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3451 if (!(d = opendir(path))) {
3452 if (errno != ENOENT)
3453 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3458 while ((de = readdir(d))) {
3461 if (ignore_file(de->d_name))
3464 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3467 if (strlen(de->d_name) < 4)
3470 a = undecchar(de->d_name[1]);
3471 b = undecchar(de->d_name[2]);
3477 fpath = strjoin(path, "/", de->d_name, NULL);
3483 if (access(fpath, X_OK) < 0) {
3485 if (errno != ENOENT)
3486 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3492 if (!(name = sysv_translate_name(de->d_name + 3))) {
3497 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3499 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3503 if (de->d_name[0] == 'S') {
3505 if (rcnd_table[i].type == RUNLEVEL_UP) {
3506 SERVICE(service)->sysv_start_priority_from_rcnd =
3507 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3509 SERVICE(service)->sysv_enabled = true;
3512 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3515 if ((r = set_put(runlevel_services[i], service)) < 0)
3518 } else if (de->d_name[0] == 'K' &&
3519 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3521 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3524 if ((r = set_put(shutdown_services, service)) < 0)
3530 /* Now we loaded all stubs and are aware of the lowest
3531 start-up priority for all services, not let's actually load
3532 the services, this will also tell us which services are
3533 actually native now */
3534 manager_dispatch_load_queue(m);
3536 /* If this is a native service, rely on native ways to pull in
3537 * a service, don't pull it in via sysv rcN.d links. */
3538 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3539 SET_FOREACH(service, runlevel_services[i], j) {
3540 service = unit_follow_merge(service);
3542 if (service->fragment_path)
3545 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3549 /* We honour K links only for halt/reboot. For the normal
3550 * runlevels we assume the stop jobs will be implicitly added
3551 * by the core logic. Also, we don't really distinguish here
3552 * between the runlevels 0 and 6 and just add them to the
3553 * special shutdown target. */
3554 SET_FOREACH(service, shutdown_services, j) {
3555 service = unit_follow_merge(service);
3557 if (service->fragment_path)
3560 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3571 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3572 set_free(runlevel_services[i]);
3573 set_free(shutdown_services);
3582 static void service_bus_name_owner_change(
3585 const char *old_owner,
3586 const char *new_owner) {
3588 Service *s = SERVICE(u);
3593 assert(streq(s->bus_name, name));
3594 assert(old_owner || new_owner);
3596 if (old_owner && new_owner)
3597 log_debug_unit(u->id,
3598 "%s's D-Bus name %s changed owner from %s to %s",
3599 u->id, name, old_owner, new_owner);
3601 log_debug_unit(u->id,
3602 "%s's D-Bus name %s no longer registered by %s",
3603 u->id, name, old_owner);
3605 log_debug_unit(u->id,
3606 "%s's D-Bus name %s now registered by %s",
3607 u->id, name, new_owner);
3609 s->bus_name_good = !!new_owner;
3611 if (s->type == SERVICE_DBUS) {
3613 /* service_enter_running() will figure out what to
3615 if (s->state == SERVICE_RUNNING)
3616 service_enter_running(s, SERVICE_SUCCESS);
3617 else if (s->state == SERVICE_START && new_owner)
3618 service_enter_start_post(s);
3620 } else if (new_owner &&
3622 (s->state == SERVICE_START ||
3623 s->state == SERVICE_START_POST ||
3624 s->state == SERVICE_RUNNING ||
3625 s->state == SERVICE_RELOAD)) {
3627 /* Try to acquire PID from bus service */
3628 log_debug_unit(u->id,
3629 "Trying to acquire PID from D-Bus name...");
3631 bus_query_pid(u->manager, name);
3635 static void service_bus_query_pid_done(
3640 Service *s = SERVICE(u);
3645 log_debug_unit(u->id,
3646 "%s's D-Bus name %s is now owned by process %u",
3647 u->id, name, (unsigned) pid);
3649 if (s->main_pid <= 0 &&
3650 (s->state == SERVICE_START ||
3651 s->state == SERVICE_START_POST ||
3652 s->state == SERVICE_RUNNING ||
3653 s->state == SERVICE_RELOAD))
3654 service_set_main_pid(s, pid);
3657 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3662 /* This is called by the socket code when instantiating a new
3663 * service for a stream socket and the socket needs to be
3666 if (UNIT(s)->load_state != UNIT_LOADED)
3669 if (s->socket_fd >= 0)
3672 if (s->state != SERVICE_DEAD)
3676 s->got_socket_fd = true;
3678 unit_ref_set(&s->accept_socket, UNIT(sock));
3680 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3683 static void service_reset_failed(Unit *u) {
3684 Service *s = SERVICE(u);
3688 if (s->state == SERVICE_FAILED)
3689 service_set_state(s, SERVICE_DEAD);
3691 s->result = SERVICE_SUCCESS;
3692 s->reload_result = SERVICE_SUCCESS;
3694 RATELIMIT_RESET(s->start_limit);
3697 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3698 Service *s = SERVICE(u);
3700 Set *pid_set = NULL;
3704 if (s->main_pid <= 0 && who == KILL_MAIN) {
3705 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3709 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3710 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3714 if (who == KILL_CONTROL || who == KILL_ALL)
3715 if (s->control_pid > 0)
3716 if (kill(s->control_pid, signo) < 0)
3719 if (who == KILL_MAIN || who == KILL_ALL)
3720 if (s->main_pid > 0)
3721 if (kill(s->main_pid, signo) < 0)
3724 if (who == KILL_ALL) {
3727 pid_set = set_new(trivial_hash_func, trivial_compare_func);
3731 /* Exclude the control/main pid from being killed via the cgroup */
3732 if (s->control_pid > 0) {
3733 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
3740 if (s->main_pid > 0) {
3741 q = set_put(pid_set, LONG_TO_PTR(s->main_pid));
3748 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3749 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3760 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3761 [SERVICE_DEAD] = "dead",
3762 [SERVICE_START_PRE] = "start-pre",
3763 [SERVICE_START] = "start",
3764 [SERVICE_START_POST] = "start-post",
3765 [SERVICE_RUNNING] = "running",
3766 [SERVICE_EXITED] = "exited",
3767 [SERVICE_RELOAD] = "reload",
3768 [SERVICE_STOP] = "stop",
3769 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3770 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3771 [SERVICE_STOP_POST] = "stop-post",
3772 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3773 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3774 [SERVICE_FAILED] = "failed",
3775 [SERVICE_AUTO_RESTART] = "auto-restart",
3778 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3780 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3781 [SERVICE_RESTART_NO] = "no",
3782 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3783 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3784 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3785 [SERVICE_RESTART_ALWAYS] = "always"
3788 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3790 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3791 [SERVICE_SIMPLE] = "simple",
3792 [SERVICE_FORKING] = "forking",
3793 [SERVICE_ONESHOT] = "oneshot",
3794 [SERVICE_DBUS] = "dbus",
3795 [SERVICE_NOTIFY] = "notify",
3796 [SERVICE_IDLE] = "idle"
3799 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3801 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3802 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3803 [SERVICE_EXEC_START] = "ExecStart",
3804 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3805 [SERVICE_EXEC_RELOAD] = "ExecReload",
3806 [SERVICE_EXEC_STOP] = "ExecStop",
3807 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3810 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3812 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3813 [NOTIFY_NONE] = "none",
3814 [NOTIFY_MAIN] = "main",
3815 [NOTIFY_ALL] = "all"
3818 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3820 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3821 [SERVICE_SUCCESS] = "success",
3822 [SERVICE_FAILURE_RESOURCES] = "resources",
3823 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3824 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3825 [SERVICE_FAILURE_SIGNAL] = "signal",
3826 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3827 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3828 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3831 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3833 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3834 [SERVICE_START_LIMIT_NONE] = "none",
3835 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3836 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3837 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3839 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3841 const UnitVTable service_vtable = {
3842 .object_size = sizeof(Service),
3843 .exec_context_offset = offsetof(Service, exec_context),
3850 .init = service_init,
3851 .done = service_done,
3852 .load = service_load,
3854 .coldplug = service_coldplug,
3856 .dump = service_dump,
3858 .start = service_start,
3859 .stop = service_stop,
3860 .reload = service_reload,
3862 .can_reload = service_can_reload,
3864 .kill = service_kill,
3866 .serialize = service_serialize,
3867 .deserialize_item = service_deserialize_item,
3869 .active_state = service_active_state,
3870 .sub_state_to_string = service_sub_state_to_string,
3872 .check_gc = service_check_gc,
3873 .check_snapshot = service_check_snapshot,
3875 .sigchld_event = service_sigchld_event,
3876 .timer_event = service_timer_event,
3877 .fd_event = service_fd_event,
3879 .reset_failed = service_reset_failed,
3881 .cgroup_notify_empty = service_cgroup_notify_event,
3882 .notify_message = service_notify_message,
3884 .bus_name_owner_change = service_bus_name_owner_change,
3885 .bus_query_pid_done = service_bus_query_pid_done,
3887 .bus_interface = "org.freedesktop.systemd1.Service",
3888 .bus_message_handler = bus_service_message_handler,
3889 .bus_invalidating_properties = bus_service_invalidating_properties,
3891 #ifdef HAVE_SYSV_COMPAT
3892 .enumerate = service_enumerate,
3894 .status_message_formats = {
3895 .starting_stopping = {
3896 [0] = "Starting %s...",
3897 [1] = "Stopping %s...",
3899 .finished_start_job = {
3900 [JOB_DONE] = "Started %s.",
3901 [JOB_FAILED] = "Failed to start %s.",
3902 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3903 [JOB_TIMEOUT] = "Timed out starting %s.",
3905 .finished_stop_job = {
3906 [JOB_DONE] = "Stopped %s.",
3907 [JOB_FAILED] = "Stopped (with error) %s.",
3908 [JOB_TIMEOUT] = "Timed out stopping %s.",