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 {
59 const RunlevelType type;
61 /* Standard SysV runlevels for start-up */
62 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
63 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
64 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
65 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
66 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
69 /* SUSE style boot.d */
70 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
73 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
74 /* Debian style rcS.d */
75 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
78 /* Standard SysV runlevels for shutdown */
79 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
80 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
82 /* Note that the order here matters, as we read the
83 directories in this order, and we want to make sure that
84 sysv_start_priority is known when we first load the
85 unit. And that value we only know from S links. Hence
86 UP/SYSINIT must be read before DOWN */
89 #define RUNLEVELS_UP "12345"
90 /* #define RUNLEVELS_DOWN "06" */
91 #define RUNLEVELS_BOOT "bBsS"
94 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
95 [SERVICE_DEAD] = UNIT_INACTIVE,
96 [SERVICE_START_PRE] = UNIT_ACTIVATING,
97 [SERVICE_START] = UNIT_ACTIVATING,
98 [SERVICE_START_POST] = UNIT_ACTIVATING,
99 [SERVICE_RUNNING] = UNIT_ACTIVE,
100 [SERVICE_EXITED] = UNIT_ACTIVE,
101 [SERVICE_RELOAD] = UNIT_RELOADING,
102 [SERVICE_STOP] = UNIT_DEACTIVATING,
103 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
104 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
105 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
106 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
107 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
108 [SERVICE_FAILED] = UNIT_FAILED,
109 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
112 /* For Type=idle we never want to delay any other jobs, hence we
113 * consider idle jobs active as soon as we start working on them */
114 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
115 [SERVICE_DEAD] = UNIT_INACTIVE,
116 [SERVICE_START_PRE] = UNIT_ACTIVE,
117 [SERVICE_START] = UNIT_ACTIVE,
118 [SERVICE_START_POST] = UNIT_ACTIVE,
119 [SERVICE_RUNNING] = UNIT_ACTIVE,
120 [SERVICE_EXITED] = UNIT_ACTIVE,
121 [SERVICE_RELOAD] = UNIT_RELOADING,
122 [SERVICE_STOP] = UNIT_DEACTIVATING,
123 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
124 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
125 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
126 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
127 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
128 [SERVICE_FAILED] = UNIT_FAILED,
129 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
132 static void service_init(Unit *u) {
133 Service *s = SERVICE(u);
136 assert(u->load_state == UNIT_STUB);
138 s->timeout_start_usec = DEFAULT_TIMEOUT_USEC;
139 s->timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
140 s->restart_usec = DEFAULT_RESTART_USEC;
141 s->type = _SERVICE_TYPE_INVALID;
143 s->watchdog_watch.type = WATCH_INVALID;
145 s->timer_watch.type = WATCH_INVALID;
146 #ifdef HAVE_SYSV_COMPAT
147 s->sysv_start_priority = -1;
148 s->sysv_start_priority_from_rcnd = -1;
151 s->guess_main_pid = true;
153 exec_context_init(&s->exec_context);
154 kill_context_init(&s->kill_context);
156 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
158 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
161 static void service_unwatch_control_pid(Service *s) {
164 if (s->control_pid <= 0)
167 unit_unwatch_pid(UNIT(s), s->control_pid);
171 static void service_unwatch_main_pid(Service *s) {
174 if (s->main_pid <= 0)
177 unit_unwatch_pid(UNIT(s), s->main_pid);
181 static void service_unwatch_pid_file(Service *s) {
182 if (!s->pid_file_pathspec)
185 log_debug("Stopping watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
186 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
187 path_spec_done(s->pid_file_pathspec);
188 free(s->pid_file_pathspec);
189 s->pid_file_pathspec = NULL;
192 static int service_set_main_pid(Service *s, pid_t pid) {
204 s->main_pid_known = true;
206 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
207 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
208 UNIT(s)->id, (unsigned long) pid);
210 s->main_pid_alien = true;
212 s->main_pid_alien = false;
214 exec_status_start(&s->main_exec_status, pid);
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_DEREF(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_dead(Service *s, ServiceResult f, bool allow_restart);
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("%s watchdog timeout!", UNIT(s)->id);
261 service_enter_dead(s, SERVICE_FAILURE_WATCHDOG, true);
265 r = unit_watch_timer(UNIT(s), s->watchdog_usec - offset, &s->watchdog_watch);
267 log_warning("%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
270 static void service_reset_watchdog(Service *s) {
273 dual_timestamp_get(&s->watchdog_timestamp);
274 service_handle_watchdog(s);
277 static void service_done(Unit *u) {
278 Service *s = SERVICE(u);
285 #ifdef HAVE_SYSV_COMPAT
286 free(s->sysv_runlevels);
287 s->sysv_runlevels = NULL;
290 free(s->status_text);
291 s->status_text = NULL;
293 exec_context_done(&s->exec_context);
294 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
295 s->control_command = NULL;
296 s->main_command = NULL;
298 set_free(s->restart_ignore_status.code);
299 s->restart_ignore_status.code = NULL;
300 set_free(s->restart_ignore_status.signal);
301 s->restart_ignore_status.signal = NULL;
303 set_free(s->success_status.code);
304 s->success_status.code = NULL;
305 set_free(s->success_status.signal);
306 s->success_status.signal = NULL;
308 /* This will leak a process, but at least no memory or any of
310 service_unwatch_main_pid(s);
311 service_unwatch_control_pid(s);
312 service_unwatch_pid_file(s);
315 unit_unwatch_bus_name(u, s->bus_name);
320 service_close_socket_fd(s);
321 service_connection_unref(s);
323 unit_ref_unset(&s->accept_socket);
325 service_stop_watchdog(s);
327 unit_unwatch_timer(u, &s->timer_watch);
330 #ifdef HAVE_SYSV_COMPAT
331 static char *sysv_translate_name(const char *name) {
334 if (!(r = new(char, strlen(name) + sizeof(".service"))))
337 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
338 if (endswith(name, ".sh"))
339 /* Drop Debian-style .sh suffix */
340 strcpy(stpcpy(r, name) - 3, ".service");
343 if (startswith(name, "boot."))
344 /* Drop SuSE-style boot. prefix */
345 strcpy(stpcpy(r, name + 5), ".service");
347 #ifdef TARGET_FRUGALWARE
348 if (startswith(name, "rc."))
349 /* Drop Frugalware-style rc. prefix */
350 strcpy(stpcpy(r, name + 3), ".service");
353 /* Normal init scripts */
354 strcpy(stpcpy(r, name), ".service");
359 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
361 /* We silently ignore the $ prefix here. According to the LSB
362 * spec it simply indicates whether something is a
363 * standardized name or a distribution-specific one. Since we
364 * just follow what already exists and do not introduce new
365 * uses or names we don't care who introduced a new name. */
367 static const char * const table[] = {
368 /* LSB defined facilities */
369 "local_fs", SPECIAL_LOCAL_FS_TARGET,
370 #if defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
372 /* Due to unfortunate name selection in Mandriva,
373 * $network is provided by network-up which is ordered
374 * after network which actually starts interfaces.
375 * To break the loop, just ignore it */
376 "network", SPECIAL_NETWORK_TARGET,
378 "named", SPECIAL_NSS_LOOKUP_TARGET,
379 "portmap", SPECIAL_RPCBIND_TARGET,
380 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
381 "syslog", SPECIAL_SYSLOG_TARGET,
382 "time", SPECIAL_TIME_SYNC_TARGET,
384 /* common extensions */
385 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
386 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
389 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
390 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
394 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
405 n = *name == '$' ? name + 1 : name;
407 for (i = 0; i < ELEMENTSOF(table); i += 2) {
409 if (!streq(table[i], n))
415 if (!(r = strdup(table[i+1])))
421 /* If we don't know this name, fallback heuristics to figure
422 * out whether something is a target or a service alias. */
425 if (!unit_prefix_is_valid(n))
428 /* Facilities starting with $ are most likely targets */
429 r = unit_name_build(n, NULL, ".target");
430 } else if (filename && streq(name, filename))
431 /* Names equaling the file name of the services are redundant */
434 /* Everything else we assume to be normal service names */
435 r = sysv_translate_name(n);
446 static int sysv_fix_order(Service *s) {
452 if (s->sysv_start_priority < 0)
455 /* For each pair of services where at least one lacks a LSB
456 * header, we use the start priority value to order things. */
458 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
461 bool special_s, special_t;
468 if (UNIT(t)->load_state != UNIT_LOADED)
471 if (t->sysv_start_priority < 0)
474 /* If both units have modern headers we don't care
475 * about the priorities */
476 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
477 (UNIT(t)->fragment_path || t->sysv_has_lsb))
480 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
481 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
483 if (special_t && !special_s)
485 else if (special_s && !special_t)
487 else if (t->sysv_start_priority < s->sysv_start_priority)
489 else if (t->sysv_start_priority > s->sysv_start_priority)
494 /* FIXME: Maybe we should compare the name here lexicographically? */
496 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
503 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
506 if (!(c = new0(ExecCommand, 1)))
509 if (!(c->path = strdup(path))) {
514 if (!(c->argv = strv_new(path, arg1, NULL))) {
523 static int sysv_exec_commands(Service *s, const bool supports_reload) {
528 assert(UNIT(s)->source_path);
530 c = exec_command_new(UNIT(s)->source_path, "start");
533 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
535 c = exec_command_new(UNIT(s)->source_path, "stop");
538 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
540 if (supports_reload) {
541 c = exec_command_new(UNIT(s)->source_path, "reload");
544 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
550 static bool usage_contains_reload(const char *line) {
551 return (strcasestr(line, "{reload|") ||
552 strcasestr(line, "{reload}") ||
553 strcasestr(line, "{reload\"") ||
554 strcasestr(line, "|reload|") ||
555 strcasestr(line, "|reload}") ||
556 strcasestr(line, "|reload\""));
559 static int service_load_sysv_path(Service *s, const char *path) {
571 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
573 bool supports_reload = false;
580 f = fopen(path, "re");
582 r = errno == ENOENT ? 0 : -errno;
586 if (fstat(fileno(f), &st) < 0) {
591 free(u->source_path);
592 u->source_path = strdup(path);
593 if (!u->source_path) {
597 u->source_mtime = timespec_load(&st.st_mtim);
599 if (null_or_empty(&st)) {
600 u->load_state = UNIT_MASKED;
608 char l[LINE_MAX], *t;
610 if (!fgets(l, sizeof(l), f)) {
615 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
623 /* Try to figure out whether this init script supports
624 * the reload operation. This heuristic looks for
625 * "Usage" lines which include the reload option. */
626 if ( state == USAGE_CONTINUATION ||
627 (state == NORMAL && strcasestr(t, "usage"))) {
628 if (usage_contains_reload(t)) {
629 supports_reload = true;
631 } else if (t[strlen(t)-1] == '\\')
632 state = USAGE_CONTINUATION;
640 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
642 s->sysv_has_lsb = true;
646 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
652 t += strspn(t, WHITESPACE);
654 if (state == NORMAL) {
656 /* Try to parse Red Hat style chkconfig headers */
658 if (startswith_no_case(t, "chkconfig:")) {
660 char runlevels[16], *k;
664 if (sscanf(t+10, "%15s %i %*i",
666 &start_priority) != 2) {
668 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
672 /* A start priority gathered from the
673 * symlink farms is preferred over the
674 * data from the LSB header. */
675 if (start_priority < 0 || start_priority > 99)
676 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
678 s->sysv_start_priority = start_priority;
680 char_array_0(runlevels);
681 k = delete_chars(runlevels, WHITESPACE "-");
686 if (!(d = strdup(k))) {
691 free(s->sysv_runlevels);
692 s->sysv_runlevels = d;
695 } else if (startswith_no_case(t, "description:")) {
697 size_t k = strlen(t);
701 if (t[k-1] == '\\') {
706 if ((j = strstrip(t+12)) && *j) {
707 if (!(d = strdup(j))) {
714 free(chkconfig_description);
715 chkconfig_description = d;
717 } else if (startswith_no_case(t, "pidfile:")) {
724 if (!path_is_absolute(fn)) {
725 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
729 if (!(fn = strdup(fn))) {
738 } else if (state == DESCRIPTION) {
740 /* Try to parse Red Hat style description
743 size_t k = strlen(t);
751 if ((j = strstrip(t)) && *j) {
754 if (chkconfig_description)
755 d = strjoin(chkconfig_description, " ", j, NULL);
764 free(chkconfig_description);
765 chkconfig_description = d;
768 } else if (state == LSB || state == LSB_DESCRIPTION) {
770 if (startswith_no_case(t, "Provides:")) {
776 FOREACH_WORD_QUOTED(w, z, t+9, i) {
779 if (!(n = strndup(w, z))) {
784 r = sysv_translate_facility(n, path_get_file_name(path), &m);
793 if (unit_name_to_type(m) == UNIT_SERVICE)
794 r = unit_add_name(u, m);
801 * indication that the
803 * now available. This
806 * targets do NOT pull
809 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
812 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
817 } else if (startswith_no_case(t, "Required-Start:") ||
818 startswith_no_case(t, "Should-Start:") ||
819 startswith_no_case(t, "X-Start-Before:") ||
820 startswith_no_case(t, "X-Start-After:")) {
826 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
829 if (!(n = strndup(w, z))) {
834 r = sysv_translate_facility(n, path_get_file_name(path), &m);
837 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
847 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
850 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
854 } else if (startswith_no_case(t, "Default-Start:")) {
859 k = delete_chars(t+14, WHITESPACE "-");
862 if (!(d = strdup(k))) {
867 free(s->sysv_runlevels);
868 s->sysv_runlevels = d;
871 } else if (startswith_no_case(t, "Description:")) {
874 state = LSB_DESCRIPTION;
876 if ((j = strstrip(t+12)) && *j) {
877 if (!(d = strdup(j))) {
884 free(long_description);
885 long_description = d;
887 } else if (startswith_no_case(t, "Short-Description:")) {
892 if ((j = strstrip(t+18)) && *j) {
893 if (!(d = strdup(j))) {
900 free(short_description);
901 short_description = d;
903 } else if (state == LSB_DESCRIPTION) {
905 if (startswith(l, "#\t") || startswith(l, "# ")) {
908 if ((j = strstrip(t)) && *j) {
911 if (long_description)
912 d = strjoin(long_description, " ", t, NULL);
921 free(long_description);
922 long_description = d;
931 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
933 if (s->sysv_runlevels &&
934 chars_intersect(RUNLEVELS_BOOT, s->sysv_runlevels) &&
935 chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
936 /* Service has both boot and "up" runlevels
937 configured. Kill the "up" ones. */
938 delete_chars(s->sysv_runlevels, RUNLEVELS_UP);
941 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
942 /* If there a runlevels configured for this service
943 * but none of the standard ones, then we assume this
944 * is some special kind of service (which might be
945 * needed for early boot) and don't create any links
948 UNIT(s)->default_dependencies = false;
950 /* Don't timeout special services during boot (like fsck) */
951 s->timeout_start_usec = 0;
952 s->timeout_stop_usec = 0;
954 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
955 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
958 /* Special setting for all SysV services */
959 s->type = SERVICE_FORKING;
960 s->remain_after_exit = !s->pid_file;
961 s->guess_main_pid = false;
962 s->restart = SERVICE_RESTART_NO;
963 s->exec_context.ignore_sigpipe = false;
964 s->kill_context.kill_mode = KILL_PROCESS;
966 /* We use the long description only if
967 * no short description is set. */
969 if (short_description)
970 description = short_description;
971 else if (chkconfig_description)
972 description = chkconfig_description;
973 else if (long_description)
974 description = long_description;
981 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
989 /* The priority that has been set in /etc/rcN.d/ hierarchies
990 * takes precedence over what is stored as default in the LSB
992 if (s->sysv_start_priority_from_rcnd >= 0)
993 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
995 u->load_state = UNIT_LOADED;
1003 free(short_description);
1004 free(long_description);
1005 free(chkconfig_description);
1010 static int service_load_sysv_name(Service *s, const char *name) {
1016 /* For SysV services we strip the boot.*, rc.* and *.sh
1017 * prefixes/suffixes. */
1018 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
1019 if (endswith(name, ".sh.service"))
1024 if (startswith(name, "boot."))
1028 #ifdef TARGET_FRUGALWARE
1029 if (startswith(name, "rc."))
1033 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1037 path = strjoin(*p, "/", name, NULL);
1041 assert(endswith(path, ".service"));
1042 path[strlen(path)-8] = 0;
1044 r = service_load_sysv_path(s, path);
1046 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
1047 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1048 /* Try Debian style *.sh source'able init scripts */
1049 strcat(path, ".sh");
1050 r = service_load_sysv_path(s, path);
1056 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1057 /* Try SUSE style boot.* init scripts */
1059 path = strjoin(*p, "/boot.", name, NULL);
1063 /* Drop .service suffix */
1064 path[strlen(path)-8] = 0;
1065 r = service_load_sysv_path(s, path);
1070 #ifdef TARGET_FRUGALWARE
1071 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1072 /* Try Frugalware style rc.* init scripts */
1074 path = strjoin(*p, "/rc.", name, NULL);
1078 /* Drop .service suffix */
1079 path[strlen(path)-8] = 0;
1080 r = service_load_sysv_path(s, path);
1088 if ((UNIT(s)->load_state != UNIT_STUB))
1095 static int service_load_sysv(Service *s) {
1102 /* Load service data from SysV init scripts, preferably with
1103 * LSB headers ... */
1105 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1108 if ((t = UNIT(s)->id))
1109 if ((r = service_load_sysv_name(s, t)) < 0)
1112 if (UNIT(s)->load_state == UNIT_STUB)
1113 SET_FOREACH(t, UNIT(s)->names, i) {
1114 if (t == UNIT(s)->id)
1117 if ((r = service_load_sysv_name(s, t)) < 0)
1120 if (UNIT(s)->load_state != UNIT_STUB)
1128 static int fsck_fix_order(Service *s) {
1134 if (s->fsck_passno <= 0)
1137 /* For each pair of services where both have an fsck priority
1138 * we order things based on it. */
1140 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1149 if (UNIT(t)->load_state != UNIT_LOADED)
1152 if (t->fsck_passno <= 0)
1155 if (t->fsck_passno < s->fsck_passno)
1157 else if (t->fsck_passno > s->fsck_passno)
1162 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1169 static int service_verify(Service *s) {
1172 if (UNIT(s)->load_state != UNIT_LOADED)
1175 if (!s->exec_command[SERVICE_EXEC_START]) {
1176 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1180 if (s->type != SERVICE_ONESHOT &&
1181 s->exec_command[SERVICE_EXEC_START]->command_next) {
1182 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1186 if (s->type == SERVICE_DBUS && !s->bus_name) {
1187 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1191 if (s->bus_name && s->type != SERVICE_DBUS)
1192 log_warning("%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1194 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1195 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1202 static int service_add_default_dependencies(Service *s) {
1207 /* Add a number of automatic dependencies useful for the
1208 * majority of services. */
1210 /* First, pull in base system */
1211 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1213 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1216 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1218 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1222 /* Second, activate normal shutdown */
1223 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1226 static void service_fix_output(Service *s) {
1229 /* If nothing has been explicitly configured, patch default
1230 * output in. If input is socket/tty we avoid this however,
1231 * since in that case we want output to default to the same
1232 * place as we read input from. */
1234 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1235 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1236 s->exec_context.std_input == EXEC_INPUT_NULL)
1237 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1239 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1240 s->exec_context.std_input == EXEC_INPUT_NULL)
1241 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1244 static int service_load(Unit *u) {
1246 Service *s = SERVICE(u);
1250 /* Load a .service file */
1251 if ((r = unit_load_fragment(u)) < 0)
1254 #ifdef HAVE_SYSV_COMPAT
1255 /* Load a classic init script as a fallback, if we couldn't find anything */
1256 if (u->load_state == UNIT_STUB)
1257 if ((r = service_load_sysv(s)) < 0)
1261 /* Still nothing found? Then let's give up */
1262 if (u->load_state == UNIT_STUB)
1265 /* We were able to load something, then let's add in the
1266 * dropin directories. */
1267 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1270 /* This is a new unit? Then let's add in some extras */
1271 if (u->load_state == UNIT_LOADED) {
1272 if (s->type == _SERVICE_TYPE_INVALID)
1273 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1275 /* Oneshot services have disabled start timeout by default */
1276 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1277 s->timeout_start_usec = 0;
1279 service_fix_output(s);
1281 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1284 if ((r = unit_add_default_cgroups(u)) < 0)
1287 #ifdef HAVE_SYSV_COMPAT
1288 if ((r = sysv_fix_order(s)) < 0)
1292 if ((r = fsck_fix_order(s)) < 0)
1296 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1299 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1300 s->notify_access = NOTIFY_MAIN;
1302 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1303 s->notify_access = NOTIFY_MAIN;
1305 if (s->type == SERVICE_DBUS || s->bus_name)
1306 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1309 if (UNIT(s)->default_dependencies)
1310 if ((r = service_add_default_dependencies(s)) < 0)
1313 r = unit_exec_context_defaults(u, &s->exec_context);
1318 return service_verify(s);
1321 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1323 ServiceExecCommand c;
1324 Service *s = SERVICE(u);
1325 const char *prefix2;
1330 p2 = strappend(prefix, "\t");
1331 prefix2 = p2 ? p2 : prefix;
1334 "%sService State: %s\n"
1336 "%sReload Result: %s\n"
1337 "%sPermissionsStartOnly: %s\n"
1338 "%sRootDirectoryStartOnly: %s\n"
1339 "%sRemainAfterExit: %s\n"
1340 "%sGuessMainPID: %s\n"
1343 "%sNotifyAccess: %s\n",
1344 prefix, service_state_to_string(s->state),
1345 prefix, service_result_to_string(s->result),
1346 prefix, service_result_to_string(s->reload_result),
1347 prefix, yes_no(s->permissions_start_only),
1348 prefix, yes_no(s->root_directory_start_only),
1349 prefix, yes_no(s->remain_after_exit),
1350 prefix, yes_no(s->guess_main_pid),
1351 prefix, service_type_to_string(s->type),
1352 prefix, service_restart_to_string(s->restart),
1353 prefix, notify_access_to_string(s->notify_access));
1355 if (s->control_pid > 0)
1357 "%sControl PID: %lu\n",
1358 prefix, (unsigned long) s->control_pid);
1360 if (s->main_pid > 0)
1363 "%sMain PID Known: %s\n"
1364 "%sMain PID Alien: %s\n",
1365 prefix, (unsigned long) s->main_pid,
1366 prefix, yes_no(s->main_pid_known),
1367 prefix, yes_no(s->main_pid_alien));
1372 prefix, s->pid_file);
1377 "%sBus Name Good: %s\n",
1378 prefix, s->bus_name,
1379 prefix, yes_no(s->bus_name_good));
1381 kill_context_dump(&s->kill_context, f, prefix);
1382 exec_context_dump(&s->exec_context, f, prefix);
1384 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1386 if (!s->exec_command[c])
1389 fprintf(f, "%s-> %s:\n",
1390 prefix, service_exec_command_to_string(c));
1392 exec_command_dump_list(s->exec_command[c], f, prefix2);
1395 #ifdef HAVE_SYSV_COMPAT
1398 "%sSysV Init Script has LSB Header: %s\n"
1399 "%sSysVEnabled: %s\n",
1400 prefix, yes_no(s->sysv_has_lsb),
1401 prefix, yes_no(s->sysv_enabled));
1403 if (s->sysv_start_priority >= 0)
1405 "%sSysVStartPriority: %i\n",
1406 prefix, s->sysv_start_priority);
1408 if (s->sysv_runlevels)
1409 fprintf(f, "%sSysVRunLevels: %s\n",
1410 prefix, s->sysv_runlevels);
1413 if (s->fsck_passno > 0)
1415 "%sFsckPassNo: %i\n",
1416 prefix, s->fsck_passno);
1419 fprintf(f, "%sStatus Text: %s\n",
1420 prefix, s->status_text);
1425 static int service_load_pid_file(Service *s, bool may_warn) {
1435 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1437 log_info("PID file %s not readable (yet?) after %s.",
1438 s->pid_file, service_state_to_string(s->state));
1442 r = parse_pid(k, &pid);
1448 if (kill(pid, 0) < 0 && errno != EPERM) {
1450 log_info("PID %lu read from file %s does not exist.",
1451 (unsigned long) pid, s->pid_file);
1455 if (s->main_pid_known) {
1456 if (pid == s->main_pid)
1459 log_debug("Main PID changing: %lu -> %lu",
1460 (unsigned long) s->main_pid, (unsigned long) pid);
1461 service_unwatch_main_pid(s);
1462 s->main_pid_known = false;
1464 log_debug("Main PID loaded: %lu", (unsigned long) pid);
1466 if ((r = service_set_main_pid(s, pid)) < 0)
1469 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1470 /* FIXME: we need to do something here */
1476 static int service_search_main_pid(Service *s) {
1482 /* If we know it anyway, don't ever fallback to unreliable
1484 if (s->main_pid_known)
1487 if (!s->guess_main_pid)
1490 assert(s->main_pid <= 0);
1492 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1495 log_debug("Main PID guessed: %lu", (unsigned long) pid);
1496 if ((r = service_set_main_pid(s, pid)) < 0)
1499 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1500 /* FIXME: we need to do something here */
1506 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1512 /* Notifies all our sockets when we die */
1514 if (s->socket_fd >= 0)
1517 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1518 if (u->type == UNIT_SOCKET)
1519 socket_notify_service_dead(SOCKET(u), failed_permanent);
1524 static void service_set_state(Service *s, ServiceState state) {
1525 ServiceState old_state;
1526 const UnitActiveState *table;
1529 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1531 old_state = s->state;
1534 service_unwatch_pid_file(s);
1536 if (state != SERVICE_START_PRE &&
1537 state != SERVICE_START &&
1538 state != SERVICE_START_POST &&
1539 state != SERVICE_RELOAD &&
1540 state != SERVICE_STOP &&
1541 state != SERVICE_STOP_SIGTERM &&
1542 state != SERVICE_STOP_SIGKILL &&
1543 state != SERVICE_STOP_POST &&
1544 state != SERVICE_FINAL_SIGTERM &&
1545 state != SERVICE_FINAL_SIGKILL &&
1546 state != SERVICE_AUTO_RESTART)
1547 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1549 if (state != SERVICE_START &&
1550 state != SERVICE_START_POST &&
1551 state != SERVICE_RUNNING &&
1552 state != SERVICE_RELOAD &&
1553 state != SERVICE_STOP &&
1554 state != SERVICE_STOP_SIGTERM &&
1555 state != SERVICE_STOP_SIGKILL) {
1556 service_unwatch_main_pid(s);
1557 s->main_command = NULL;
1560 if (state != SERVICE_START_PRE &&
1561 state != SERVICE_START &&
1562 state != SERVICE_START_POST &&
1563 state != SERVICE_RELOAD &&
1564 state != SERVICE_STOP &&
1565 state != SERVICE_STOP_SIGTERM &&
1566 state != SERVICE_STOP_SIGKILL &&
1567 state != SERVICE_STOP_POST &&
1568 state != SERVICE_FINAL_SIGTERM &&
1569 state != SERVICE_FINAL_SIGKILL) {
1570 service_unwatch_control_pid(s);
1571 s->control_command = NULL;
1572 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1575 if (state == SERVICE_DEAD ||
1576 state == SERVICE_STOP ||
1577 state == SERVICE_STOP_SIGTERM ||
1578 state == SERVICE_STOP_SIGKILL ||
1579 state == SERVICE_STOP_POST ||
1580 state == SERVICE_FINAL_SIGTERM ||
1581 state == SERVICE_FINAL_SIGKILL ||
1582 state == SERVICE_FAILED ||
1583 state == SERVICE_AUTO_RESTART)
1584 service_notify_sockets_dead(s, false);
1586 if (state != SERVICE_START_PRE &&
1587 state != SERVICE_START &&
1588 state != SERVICE_START_POST &&
1589 state != SERVICE_RUNNING &&
1590 state != SERVICE_RELOAD &&
1591 state != SERVICE_STOP &&
1592 state != SERVICE_STOP_SIGTERM &&
1593 state != SERVICE_STOP_SIGKILL &&
1594 state != SERVICE_STOP_POST &&
1595 state != SERVICE_FINAL_SIGTERM &&
1596 state != SERVICE_FINAL_SIGKILL &&
1597 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1598 service_close_socket_fd(s);
1599 service_connection_unref(s);
1602 if (state == SERVICE_STOP)
1603 service_stop_watchdog(s);
1605 /* For the inactive states unit_notify() will trim the cgroup,
1606 * but for exit we have to do that ourselves... */
1607 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1608 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1610 if (old_state != state)
1611 log_struct(LOG_DEBUG,
1612 "UNIT=%s", UNIT(s)->id,
1613 "MESSAGE=%s changed %s -> %s", UNIT(s)->id,
1614 service_state_to_string(old_state),
1615 service_state_to_string(state),
1618 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1619 s->reload_result = SERVICE_SUCCESS;
1622 static int service_coldplug(Unit *u) {
1623 Service *s = SERVICE(u);
1627 assert(s->state == SERVICE_DEAD);
1629 if (s->deserialized_state != s->state) {
1631 if (s->deserialized_state == SERVICE_START_PRE ||
1632 s->deserialized_state == SERVICE_START ||
1633 s->deserialized_state == SERVICE_START_POST ||
1634 s->deserialized_state == SERVICE_RELOAD ||
1635 s->deserialized_state == SERVICE_STOP ||
1636 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1637 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1638 s->deserialized_state == SERVICE_STOP_POST ||
1639 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1640 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1641 s->deserialized_state == SERVICE_AUTO_RESTART) {
1642 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1645 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1647 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1652 if ((s->deserialized_state == SERVICE_START &&
1653 (s->type == SERVICE_FORKING ||
1654 s->type == SERVICE_DBUS ||
1655 s->type == SERVICE_ONESHOT ||
1656 s->type == SERVICE_NOTIFY)) ||
1657 s->deserialized_state == SERVICE_START_POST ||
1658 s->deserialized_state == SERVICE_RUNNING ||
1659 s->deserialized_state == SERVICE_RELOAD ||
1660 s->deserialized_state == SERVICE_STOP ||
1661 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1662 s->deserialized_state == SERVICE_STOP_SIGKILL)
1663 if (s->main_pid > 0)
1664 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1667 if (s->deserialized_state == SERVICE_START_PRE ||
1668 s->deserialized_state == SERVICE_START ||
1669 s->deserialized_state == SERVICE_START_POST ||
1670 s->deserialized_state == SERVICE_RELOAD ||
1671 s->deserialized_state == SERVICE_STOP ||
1672 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1673 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1674 s->deserialized_state == SERVICE_STOP_POST ||
1675 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1676 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1677 if (s->control_pid > 0)
1678 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1681 if (s->deserialized_state == SERVICE_START_POST ||
1682 s->deserialized_state == SERVICE_RUNNING)
1683 service_handle_watchdog(s);
1685 service_set_state(s, s->deserialized_state);
1690 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1694 unsigned rn_fds = 0;
1701 if (s->socket_fd >= 0)
1704 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1709 if (u->type != UNIT_SOCKET)
1714 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1726 if (!(t = new(int, rn_fds+cn_fds))) {
1732 memcpy(t, rfds, rn_fds * sizeof(int));
1733 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1738 rn_fds = rn_fds+cn_fds;
1753 static int service_spawn(
1758 bool apply_permissions,
1760 bool apply_tty_stdin,
1761 bool set_notify_socket,
1767 int *fds = NULL, *fdsbuf = NULL;
1768 unsigned n_fds = 0, n_env = 0;
1769 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1776 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1777 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1778 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1780 if (s->socket_fd >= 0) {
1781 fds = &s->socket_fd;
1784 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1791 if (timeout && s->timeout_start_usec) {
1792 r = unit_watch_timer(UNIT(s), s->timeout_start_usec, &s->timer_watch);
1796 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1798 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1803 our_env = new0(char*, 5);
1809 if (set_notify_socket)
1810 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1815 if (s->main_pid > 0)
1816 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1821 if (s->watchdog_usec > 0)
1822 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1827 if (s->meta.manager->running_as != SYSTEMD_SYSTEM)
1828 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1833 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1847 UNIT(s)->manager->confirm_spawn,
1848 UNIT(s)->cgroup_bondings,
1849 UNIT(s)->cgroup_attributes,
1850 is_control ? "control" : NULL,
1852 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1858 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1859 /* FIXME: we need to do something here */
1865 strv_free(final_env);
1875 strv_free(final_env);
1878 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1883 static int main_pid_good(Service *s) {
1886 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1889 /* If we know the pid file, then lets just check if it is
1891 if (s->main_pid_known) {
1893 /* If it's an alien child let's check if it is still
1895 if (s->main_pid_alien)
1896 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1898 /* .. otherwise assume we'll get a SIGCHLD for it,
1899 * which we really should wait for to collect exit
1900 * status and code */
1901 return s->main_pid > 0;
1904 /* We don't know the pid */
1908 static int control_pid_good(Service *s) {
1911 return s->control_pid > 0;
1914 static int cgroup_good(Service *s) {
1919 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1925 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1929 if (f != SERVICE_SUCCESS)
1932 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1934 if (allow_restart &&
1935 !s->forbid_restart &&
1936 (s->restart == SERVICE_RESTART_ALWAYS ||
1937 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1938 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1939 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1940 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1941 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1942 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1943 (s->result != SERVICE_FAILURE_SIGNAL ||
1944 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1947 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
1951 service_set_state(s, SERVICE_AUTO_RESTART);
1954 s->forbid_restart = false;
1959 log_warning("%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1960 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1963 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1965 static void service_enter_stop_post(Service *s, ServiceResult f) {
1969 if (f != SERVICE_SUCCESS)
1972 service_unwatch_control_pid(s);
1974 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1975 s->control_command_id = SERVICE_EXEC_STOP_POST;
1977 r = service_spawn(s,
1981 !s->permissions_start_only,
1982 !s->root_directory_start_only,
1991 service_set_state(s, SERVICE_STOP_POST);
1993 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1998 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
1999 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2002 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
2004 Set *pid_set = NULL;
2005 bool wait_for_exit = false;
2009 if (f != SERVICE_SUCCESS)
2012 if (s->kill_context.kill_mode != KILL_NONE) {
2013 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->kill_context.kill_signal : SIGKILL;
2015 if (s->main_pid > 0) {
2016 if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
2017 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
2019 wait_for_exit = !s->main_pid_alien;
2022 if (s->control_pid > 0) {
2023 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
2024 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
2026 wait_for_exit = true;
2029 if (s->kill_context.kill_mode == KILL_CONTROL_GROUP) {
2031 pid_set = set_new(trivial_hash_func, trivial_compare_func);
2037 /* Exclude the main/control pids from being killed via the cgroup */
2038 if (s->main_pid > 0)
2039 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
2042 if (s->control_pid > 0)
2043 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
2046 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
2048 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
2049 log_warning("Failed to kill control group: %s", strerror(-r));
2051 wait_for_exit = true;
2058 if (wait_for_exit) {
2059 if (s->timeout_stop_usec > 0) {
2060 r = unit_watch_timer(UNIT(s), s->timeout_stop_usec, &s->timer_watch);
2065 service_set_state(s, state);
2066 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2067 service_enter_stop_post(s, SERVICE_SUCCESS);
2069 service_enter_dead(s, SERVICE_SUCCESS, true);
2074 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2076 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2077 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2079 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2085 static void service_enter_stop(Service *s, ServiceResult f) {
2090 if (f != SERVICE_SUCCESS)
2093 service_unwatch_control_pid(s);
2095 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2096 s->control_command_id = SERVICE_EXEC_STOP;
2098 r = service_spawn(s,
2102 !s->permissions_start_only,
2103 !s->root_directory_start_only,
2111 service_set_state(s, SERVICE_STOP);
2113 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2118 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2119 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2122 static void service_enter_running(Service *s, ServiceResult f) {
2123 int main_pid_ok, cgroup_ok;
2126 if (f != SERVICE_SUCCESS)
2129 main_pid_ok = main_pid_good(s);
2130 cgroup_ok = cgroup_good(s);
2132 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2133 (s->bus_name_good || s->type != SERVICE_DBUS))
2134 service_set_state(s, SERVICE_RUNNING);
2135 else if (s->remain_after_exit)
2136 service_set_state(s, SERVICE_EXITED);
2138 service_enter_stop(s, SERVICE_SUCCESS);
2141 static void service_enter_start_post(Service *s) {
2145 service_unwatch_control_pid(s);
2147 if (s->watchdog_usec > 0)
2148 service_reset_watchdog(s);
2150 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2151 s->control_command_id = SERVICE_EXEC_START_POST;
2153 r = service_spawn(s,
2157 !s->permissions_start_only,
2158 !s->root_directory_start_only,
2166 service_set_state(s, SERVICE_START_POST);
2168 service_enter_running(s, SERVICE_SUCCESS);
2173 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2174 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2177 static void service_enter_start(Service *s) {
2184 assert(s->exec_command[SERVICE_EXEC_START]);
2185 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2187 if (s->type == SERVICE_FORKING)
2188 service_unwatch_control_pid(s);
2190 service_unwatch_main_pid(s);
2192 /* We want to ensure that nobody leaks processes from
2193 * START_PRE here, so let's go on a killing spree, People
2194 * should not spawn long running processes from START_PRE. */
2195 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2197 if (s->type == SERVICE_FORKING) {
2198 s->control_command_id = SERVICE_EXEC_START;
2199 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2201 s->main_command = NULL;
2203 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2204 s->control_command = NULL;
2206 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2209 r = service_spawn(s,
2211 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2216 s->notify_access != NOTIFY_NONE,
2222 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2223 /* For simple services we immediately start
2224 * the START_POST binaries. */
2226 service_set_main_pid(s, pid);
2227 service_enter_start_post(s);
2229 } else if (s->type == SERVICE_FORKING) {
2231 /* For forking services we wait until the start
2232 * process exited. */
2234 s->control_pid = pid;
2235 service_set_state(s, SERVICE_START);
2237 } else if (s->type == SERVICE_ONESHOT ||
2238 s->type == SERVICE_DBUS ||
2239 s->type == SERVICE_NOTIFY) {
2241 /* For oneshot services we wait until the start
2242 * process exited, too, but it is our main process. */
2244 /* For D-Bus services we know the main pid right away,
2245 * but wait for the bus name to appear on the
2246 * bus. Notify services are similar. */
2248 service_set_main_pid(s, pid);
2249 service_set_state(s, SERVICE_START);
2251 assert_not_reached("Unknown service type");
2256 log_warning("%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2257 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2260 static void service_enter_start_pre(Service *s) {
2265 service_unwatch_control_pid(s);
2267 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2269 /* Before we start anything, let's clear up what might
2270 * be left from previous runs. */
2271 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2273 s->control_command_id = SERVICE_EXEC_START_PRE;
2275 r = service_spawn(s,
2279 !s->permissions_start_only,
2280 !s->root_directory_start_only,
2288 service_set_state(s, SERVICE_START_PRE);
2290 service_enter_start(s);
2295 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2296 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2299 static void service_enter_restart(Service *s) {
2304 dbus_error_init(&error);
2306 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2307 /* Don't restart things if we are going down anyway */
2308 log_info("Stop job pending for unit, delaying automatic restart.");
2310 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
2317 /* Any units that are bound to this service must also be
2318 * restarted. We use JOB_RESTART (instead of the more obvious
2319 * JOB_START) here so that those dependency jobs will be added
2321 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2325 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2326 * it will be canceled as part of the service_stop() call that
2327 * is executed as part of JOB_RESTART. */
2329 log_debug("%s scheduled restart job.", UNIT(s)->id);
2333 log_warning("%s failed to schedule restart job: %s", UNIT(s)->id, bus_error(&error, -r));
2334 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2336 dbus_error_free(&error);
2339 static void service_enter_reload(Service *s) {
2344 service_unwatch_control_pid(s);
2346 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2347 s->control_command_id = SERVICE_EXEC_RELOAD;
2349 r = service_spawn(s,
2353 !s->permissions_start_only,
2354 !s->root_directory_start_only,
2362 service_set_state(s, SERVICE_RELOAD);
2364 service_enter_running(s, SERVICE_SUCCESS);
2369 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
2370 s->reload_result = SERVICE_FAILURE_RESOURCES;
2371 service_enter_running(s, SERVICE_SUCCESS);
2374 static void service_run_next_control(Service *s) {
2378 assert(s->control_command);
2379 assert(s->control_command->command_next);
2381 assert(s->control_command_id != SERVICE_EXEC_START);
2383 s->control_command = s->control_command->command_next;
2384 service_unwatch_control_pid(s);
2386 r = service_spawn(s,
2390 !s->permissions_start_only,
2391 !s->root_directory_start_only,
2392 s->control_command_id == SERVICE_EXEC_START_PRE ||
2393 s->control_command_id == SERVICE_EXEC_STOP_POST,
2403 log_warning("%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
2405 if (s->state == SERVICE_START_PRE)
2406 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2407 else if (s->state == SERVICE_STOP)
2408 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2409 else if (s->state == SERVICE_STOP_POST)
2410 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2411 else if (s->state == SERVICE_RELOAD) {
2412 s->reload_result = SERVICE_FAILURE_RESOURCES;
2413 service_enter_running(s, SERVICE_SUCCESS);
2415 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2418 static void service_run_next_main(Service *s) {
2423 assert(s->main_command);
2424 assert(s->main_command->command_next);
2425 assert(s->type == SERVICE_ONESHOT);
2427 s->main_command = s->main_command->command_next;
2428 service_unwatch_main_pid(s);
2430 r = service_spawn(s,
2437 s->notify_access != NOTIFY_NONE,
2443 service_set_main_pid(s, pid);
2448 log_warning("%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2449 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2452 static int service_start_limit_test(Service *s) {
2455 if (ratelimit_test(&s->start_limit))
2458 switch (s->start_limit_action) {
2460 case SERVICE_START_LIMIT_NONE:
2461 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s)->id);
2464 case SERVICE_START_LIMIT_REBOOT: {
2468 dbus_error_init(&error);
2470 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2472 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2474 log_error("Failed to reboot: %s.", bus_error(&error, r));
2475 dbus_error_free(&error);
2481 case SERVICE_START_LIMIT_REBOOT_FORCE:
2482 log_warning("%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2483 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2486 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2487 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2489 reboot(RB_AUTOBOOT);
2493 log_error("start limit action=%i", s->start_limit_action);
2494 assert_not_reached("Unknown StartLimitAction.");
2500 static int service_start(Unit *u) {
2501 Service *s = SERVICE(u);
2506 /* We cannot fulfill this request right now, try again later
2508 if (s->state == SERVICE_STOP ||
2509 s->state == SERVICE_STOP_SIGTERM ||
2510 s->state == SERVICE_STOP_SIGKILL ||
2511 s->state == SERVICE_STOP_POST ||
2512 s->state == SERVICE_FINAL_SIGTERM ||
2513 s->state == SERVICE_FINAL_SIGKILL)
2516 /* Already on it! */
2517 if (s->state == SERVICE_START_PRE ||
2518 s->state == SERVICE_START ||
2519 s->state == SERVICE_START_POST)
2522 /* A service that will be restarted must be stopped first to
2523 * trigger BindsTo and/or OnFailure dependencies. If a user
2524 * does not want to wait for the holdoff time to elapse, the
2525 * service should be manually restarted, not started. We
2526 * simply return EAGAIN here, so that any start jobs stay
2527 * queued, and assume that the auto restart timer will
2528 * eventually trigger the restart. */
2529 if (s->state == SERVICE_AUTO_RESTART)
2532 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2534 /* Make sure we don't enter a busy loop of some kind. */
2535 r = service_start_limit_test(s);
2537 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2541 s->result = SERVICE_SUCCESS;
2542 s->reload_result = SERVICE_SUCCESS;
2543 s->main_pid_known = false;
2544 s->main_pid_alien = false;
2545 s->forbid_restart = false;
2547 service_enter_start_pre(s);
2551 static int service_stop(Unit *u) {
2552 Service *s = SERVICE(u);
2556 /* Don't create restart jobs from here. */
2557 s->forbid_restart = true;
2560 if (s->state == SERVICE_STOP ||
2561 s->state == SERVICE_STOP_SIGTERM ||
2562 s->state == SERVICE_STOP_SIGKILL ||
2563 s->state == SERVICE_STOP_POST ||
2564 s->state == SERVICE_FINAL_SIGTERM ||
2565 s->state == SERVICE_FINAL_SIGKILL)
2568 /* A restart will be scheduled or is in progress. */
2569 if (s->state == SERVICE_AUTO_RESTART) {
2570 service_set_state(s, SERVICE_DEAD);
2574 /* If there's already something running we go directly into
2576 if (s->state == SERVICE_START_PRE ||
2577 s->state == SERVICE_START ||
2578 s->state == SERVICE_START_POST ||
2579 s->state == SERVICE_RELOAD) {
2580 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2584 assert(s->state == SERVICE_RUNNING ||
2585 s->state == SERVICE_EXITED);
2587 service_enter_stop(s, SERVICE_SUCCESS);
2591 static int service_reload(Unit *u) {
2592 Service *s = SERVICE(u);
2596 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2598 service_enter_reload(s);
2602 static bool service_can_reload(Unit *u) {
2603 Service *s = SERVICE(u);
2607 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2610 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2611 Service *s = SERVICE(u);
2617 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2618 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2619 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2621 if (s->control_pid > 0)
2622 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2624 if (s->main_pid_known && s->main_pid > 0)
2625 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2627 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2630 unit_serialize_item(u, f, "status-text", s->status_text);
2632 /* FIXME: There's a minor uncleanliness here: if there are
2633 * multiple commands attached here, we will start from the
2634 * first one again */
2635 if (s->control_command_id >= 0)
2636 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2638 if (s->socket_fd >= 0) {
2641 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2644 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2647 if (s->main_exec_status.pid > 0) {
2648 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2649 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2650 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2652 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2653 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2654 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2657 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2658 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2663 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2664 Service *s = SERVICE(u);
2671 if (streq(key, "state")) {
2674 if ((state = service_state_from_string(value)) < 0)
2675 log_debug("Failed to parse state value %s", value);
2677 s->deserialized_state = state;
2678 } else if (streq(key, "result")) {
2681 f = service_result_from_string(value);
2683 log_debug("Failed to parse result value %s", value);
2684 else if (f != SERVICE_SUCCESS)
2687 } else if (streq(key, "reload-result")) {
2690 f = service_result_from_string(value);
2692 log_debug("Failed to parse reload result value %s", value);
2693 else if (f != SERVICE_SUCCESS)
2694 s->reload_result = f;
2696 } else if (streq(key, "control-pid")) {
2699 if (parse_pid(value, &pid) < 0)
2700 log_debug("Failed to parse control-pid value %s", value);
2702 s->control_pid = pid;
2703 } else if (streq(key, "main-pid")) {
2706 if (parse_pid(value, &pid) < 0)
2707 log_debug("Failed to parse main-pid value %s", value);
2709 service_set_main_pid(s, (pid_t) pid);
2710 } else if (streq(key, "main-pid-known")) {
2713 if ((b = parse_boolean(value)) < 0)
2714 log_debug("Failed to parse main-pid-known value %s", value);
2716 s->main_pid_known = b;
2717 } else if (streq(key, "status-text")) {
2720 if ((t = strdup(value))) {
2721 free(s->status_text);
2725 } else if (streq(key, "control-command")) {
2726 ServiceExecCommand id;
2728 if ((id = service_exec_command_from_string(value)) < 0)
2729 log_debug("Failed to parse exec-command value %s", value);
2731 s->control_command_id = id;
2732 s->control_command = s->exec_command[id];
2734 } else if (streq(key, "socket-fd")) {
2737 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2738 log_debug("Failed to parse socket-fd value %s", value);
2741 if (s->socket_fd >= 0)
2742 close_nointr_nofail(s->socket_fd);
2743 s->socket_fd = fdset_remove(fds, fd);
2745 } else if (streq(key, "main-exec-status-pid")) {
2748 if (parse_pid(value, &pid) < 0)
2749 log_debug("Failed to parse main-exec-status-pid value %s", value);
2751 s->main_exec_status.pid = pid;
2752 } else if (streq(key, "main-exec-status-code")) {
2755 if (safe_atoi(value, &i) < 0)
2756 log_debug("Failed to parse main-exec-status-code value %s", value);
2758 s->main_exec_status.code = i;
2759 } else if (streq(key, "main-exec-status-status")) {
2762 if (safe_atoi(value, &i) < 0)
2763 log_debug("Failed to parse main-exec-status-status value %s", value);
2765 s->main_exec_status.status = i;
2766 } else if (streq(key, "main-exec-status-start"))
2767 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2768 else if (streq(key, "main-exec-status-exit"))
2769 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2770 else if (streq(key, "watchdog-timestamp"))
2771 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2773 log_debug("Unknown serialization key '%s'", key);
2778 static UnitActiveState service_active_state(Unit *u) {
2779 const UnitActiveState *table;
2783 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2785 return table[SERVICE(u)->state];
2788 static const char *service_sub_state_to_string(Unit *u) {
2791 return service_state_to_string(SERVICE(u)->state);
2794 static bool service_check_gc(Unit *u) {
2795 Service *s = SERVICE(u);
2799 /* Never clean up services that still have a process around,
2800 * even if the service is formally dead. */
2801 if (cgroup_good(s) > 0 ||
2802 main_pid_good(s) > 0 ||
2803 control_pid_good(s) > 0)
2806 #ifdef HAVE_SYSV_COMPAT
2814 static bool service_check_snapshot(Unit *u) {
2815 Service *s = SERVICE(u);
2819 return !s->got_socket_fd;
2822 static int service_retry_pid_file(Service *s) {
2825 assert(s->pid_file);
2826 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2828 r = service_load_pid_file(s, false);
2832 service_unwatch_pid_file(s);
2834 service_enter_running(s, SERVICE_SUCCESS);
2838 static int service_watch_pid_file(Service *s) {
2841 log_debug("Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
2842 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2846 /* the pidfile might have appeared just before we set the watch */
2847 service_retry_pid_file(s);
2851 log_error("Failed to set a watch for %s's PID file %s: %s",
2852 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2853 service_unwatch_pid_file(s);
2857 static int service_demand_pid_file(Service *s) {
2860 assert(s->pid_file);
2861 assert(!s->pid_file_pathspec);
2863 ps = new0(PathSpec, 1);
2867 ps->path = strdup(s->pid_file);
2873 path_kill_slashes(ps->path);
2875 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2876 * keep their PID file open all the time. */
2877 ps->type = PATH_MODIFIED;
2878 ps->inotify_fd = -1;
2880 s->pid_file_pathspec = ps;
2882 return service_watch_pid_file(s);
2885 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2886 Service *s = SERVICE(u);
2890 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2891 assert(s->pid_file_pathspec);
2892 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2894 log_debug("inotify event for %s", u->id);
2896 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2899 if (service_retry_pid_file(s) == 0)
2902 if (service_watch_pid_file(s) < 0)
2907 service_unwatch_pid_file(s);
2908 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2911 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2912 Service *s = SERVICE(u);
2918 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2919 is_clean_exit_lsb(code, status, &s->success_status))
2920 f = SERVICE_SUCCESS;
2921 else if (code == CLD_EXITED)
2922 f = SERVICE_FAILURE_EXIT_CODE;
2923 else if (code == CLD_KILLED)
2924 f = SERVICE_FAILURE_SIGNAL;
2925 else if (code == CLD_DUMPED)
2926 f = SERVICE_FAILURE_CORE_DUMP;
2928 assert_not_reached("Unknown code");
2930 if (s->main_pid == pid) {
2931 /* Forking services may occasionally move to a new PID.
2932 * As long as they update the PID file before exiting the old
2933 * PID, they're fine. */
2934 if (service_load_pid_file(s, false) == 0)
2938 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2940 /* If this is not a forking service than the main
2941 * process got started and hence we copy the exit
2942 * status so that it is recorded both as main and as
2943 * control process exit status */
2944 if (s->main_command) {
2945 s->main_command->exec_status = s->main_exec_status;
2947 if (s->main_command->ignore)
2948 f = SERVICE_SUCCESS;
2951 log_struct(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2952 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2953 u->id, sigchld_code_to_string(code), status,
2954 strna(code == CLD_EXITED
2955 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2956 : signal_to_string(status)),
2958 "EXIT_CODE=%s", sigchld_code_to_string(code),
2959 "EXIT_STATUS=%i", status,
2962 if (f != SERVICE_SUCCESS)
2965 if (s->main_command &&
2966 s->main_command->command_next &&
2967 f == SERVICE_SUCCESS) {
2969 /* There is another command to *
2970 * execute, so let's do that. */
2972 log_debug("%s running next main command for state %s", u->id, service_state_to_string(s->state));
2973 service_run_next_main(s);
2977 /* The service exited, so the service is officially
2979 s->main_command = NULL;
2983 case SERVICE_START_POST:
2984 case SERVICE_RELOAD:
2986 /* Need to wait until the operation is
2991 if (s->type == SERVICE_ONESHOT) {
2992 /* This was our main goal, so let's go on */
2993 if (f == SERVICE_SUCCESS)
2994 service_enter_start_post(s);
2996 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3002 case SERVICE_RUNNING:
3003 service_enter_running(s, f);
3006 case SERVICE_STOP_SIGTERM:
3007 case SERVICE_STOP_SIGKILL:
3009 if (!control_pid_good(s))
3010 service_enter_stop_post(s, f);
3012 /* If there is still a control process, wait for that first */
3016 assert_not_reached("Uh, main process died at wrong time.");
3020 } else if (s->control_pid == pid) {
3024 if (s->control_command) {
3025 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
3027 if (s->control_command->ignore)
3028 f = SERVICE_SUCCESS;
3031 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
3032 "%s: control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
3034 if (f != SERVICE_SUCCESS)
3037 /* Immediately get rid of the cgroup, so that the
3038 * kernel doesn't delay the cgroup empty messages for
3039 * the service cgroup any longer than necessary */
3040 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
3042 if (s->control_command &&
3043 s->control_command->command_next &&
3044 f == SERVICE_SUCCESS) {
3046 /* There is another command to *
3047 * execute, so let's do that. */
3049 log_debug("%s running next control command for state %s", u->id, service_state_to_string(s->state));
3050 service_run_next_control(s);
3053 /* No further commands for this step, so let's
3054 * figure out what to do next */
3056 s->control_command = NULL;
3057 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3059 log_debug("%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
3063 case SERVICE_START_PRE:
3064 if (f == SERVICE_SUCCESS)
3065 service_enter_start(s);
3067 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3071 if (s->type != SERVICE_FORKING)
3072 /* Maybe spurious event due to a reload that changed the type? */
3075 if (f != SERVICE_SUCCESS) {
3076 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3081 bool has_start_post;
3084 /* Let's try to load the pid file here if we can.
3085 * The PID file might actually be created by a START_POST
3086 * script. In that case don't worry if the loading fails. */
3088 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3089 r = service_load_pid_file(s, !has_start_post);
3090 if (!has_start_post && r < 0) {
3091 r = service_demand_pid_file(s);
3092 if (r < 0 || !cgroup_good(s))
3093 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3097 service_search_main_pid(s);
3099 service_enter_start_post(s);
3102 case SERVICE_START_POST:
3103 if (f != SERVICE_SUCCESS) {
3104 service_enter_stop(s, f);
3111 r = service_load_pid_file(s, true);
3113 r = service_demand_pid_file(s);
3114 if (r < 0 || !cgroup_good(s))
3115 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3119 service_search_main_pid(s);
3121 service_enter_running(s, SERVICE_SUCCESS);
3124 case SERVICE_RELOAD:
3125 if (f == SERVICE_SUCCESS) {
3126 service_load_pid_file(s, true);
3127 service_search_main_pid(s);
3130 s->reload_result = f;
3131 service_enter_running(s, SERVICE_SUCCESS);
3135 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3138 case SERVICE_STOP_SIGTERM:
3139 case SERVICE_STOP_SIGKILL:
3140 if (main_pid_good(s) <= 0)
3141 service_enter_stop_post(s, f);
3143 /* If there is still a service
3144 * process around, wait until
3145 * that one quit, too */
3148 case SERVICE_STOP_POST:
3149 case SERVICE_FINAL_SIGTERM:
3150 case SERVICE_FINAL_SIGKILL:
3151 service_enter_dead(s, f, true);
3155 assert_not_reached("Uh, control process died at wrong time.");
3160 /* Notify clients about changed exit status */
3161 unit_add_to_dbus_queue(u);
3164 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3165 Service *s = SERVICE(u);
3168 assert(elapsed == 1);
3170 if (w == &s->watchdog_watch) {
3171 service_handle_watchdog(s);
3175 assert(w == &s->timer_watch);
3179 case SERVICE_START_PRE:
3181 log_warning("%s operation timed out. Terminating.", u->id);
3182 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3185 case SERVICE_START_POST:
3186 log_warning("%s operation timed out. Stopping.", u->id);
3187 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3190 case SERVICE_RELOAD:
3191 log_warning("%s operation timed out. Stopping.", u->id);
3192 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3193 service_enter_running(s, SERVICE_SUCCESS);
3197 log_warning("%s stopping timed out. Terminating.", u->id);
3198 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3201 case SERVICE_STOP_SIGTERM:
3202 if (s->kill_context.send_sigkill) {
3203 log_warning("%s stopping timed out. Killing.", u->id);
3204 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3206 log_warning("%s stopping timed out. Skipping SIGKILL.", u->id);
3207 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3212 case SERVICE_STOP_SIGKILL:
3213 /* Uh, we sent a SIGKILL and it is still not gone?
3214 * Must be something we cannot kill, so let's just be
3215 * weirded out and continue */
3217 log_warning("%s still around after SIGKILL. Ignoring.", u->id);
3218 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3221 case SERVICE_STOP_POST:
3222 log_warning("%s stopping timed out (2). Terminating.", u->id);
3223 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3226 case SERVICE_FINAL_SIGTERM:
3227 if (s->kill_context.send_sigkill) {
3228 log_warning("%s stopping timed out (2). Killing.", u->id);
3229 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3231 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->id);
3232 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3237 case SERVICE_FINAL_SIGKILL:
3238 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->id);
3239 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3242 case SERVICE_AUTO_RESTART:
3243 log_info("%s holdoff time over, scheduling restart.", u->id);
3244 service_enter_restart(s);
3248 assert_not_reached("Timeout at wrong time.");
3252 static void service_cgroup_notify_event(Unit *u) {
3253 Service *s = SERVICE(u);
3257 log_debug("%s: cgroup is empty", u->id);
3261 /* Waiting for SIGCHLD is usually more interesting,
3262 * because it includes return codes/signals. Which is
3263 * why we ignore the cgroup events for most cases,
3264 * except when we don't know pid which to expect the
3268 case SERVICE_START_POST:
3269 /* If we were hoping for the daemon to write its PID file,
3270 * we can give up now. */
3271 if (s->pid_file_pathspec) {
3272 log_warning("%s never wrote its PID file. Failing.", UNIT(s)->id);
3273 service_unwatch_pid_file(s);
3274 if (s->state == SERVICE_START)
3275 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3277 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3281 case SERVICE_RUNNING:
3282 /* service_enter_running() will figure out what to do */
3283 service_enter_running(s, SERVICE_SUCCESS);
3286 case SERVICE_STOP_SIGTERM:
3287 case SERVICE_STOP_SIGKILL:
3289 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3290 service_enter_stop_post(s, SERVICE_SUCCESS);
3294 case SERVICE_FINAL_SIGTERM:
3295 case SERVICE_FINAL_SIGKILL:
3296 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3297 service_enter_dead(s, SERVICE_SUCCESS, true);
3306 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3307 Service *s = SERVICE(u);
3312 if (s->notify_access == NOTIFY_NONE) {
3313 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3314 u->id, (unsigned long) pid);
3318 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3319 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3320 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3324 log_debug("%s: Got message", u->id);
3326 /* Interpret MAINPID= */
3327 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3328 (s->state == SERVICE_START ||
3329 s->state == SERVICE_START_POST ||
3330 s->state == SERVICE_RUNNING ||
3331 s->state == SERVICE_RELOAD)) {
3333 if (parse_pid(e + 8, &pid) < 0)
3334 log_warning("Failed to parse notification message %s", e);
3336 log_debug("%s: got %s", u->id, e);
3337 service_set_main_pid(s, pid);
3341 /* Interpret READY= */
3342 if (s->type == SERVICE_NOTIFY &&
3343 s->state == SERVICE_START &&
3344 strv_find(tags, "READY=1")) {
3345 log_debug("%s: got READY=1", u->id);
3347 service_enter_start_post(s);
3350 /* Interpret STATUS= */
3351 e = strv_find_prefix(tags, "STATUS=");
3357 if (!utf8_is_valid(e+7)) {
3358 log_warning("Status message in notification is not UTF-8 clean.");
3364 log_error("Failed to allocate string.");
3368 log_debug("%s: got %s", u->id, e);
3370 free(s->status_text);
3373 free(s->status_text);
3374 s->status_text = NULL;
3378 if (strv_find(tags, "WATCHDOG=1")) {
3379 log_debug("%s: got WATCHDOG=1", u->id);
3380 service_reset_watchdog(s);
3383 /* Notify clients about changed status or main pid */
3384 unit_add_to_dbus_queue(u);
3387 #ifdef HAVE_SYSV_COMPAT
3390 static void sysv_facility_in_insserv_conf(Manager *mgr) {
3394 if (!(f = fopen("/etc/insserv.conf", "re"))) {
3395 r = errno == ENOENT ? 0 : -errno;
3400 char l[LINE_MAX], *t;
3401 char **parsed = NULL;
3403 if (!fgets(l, sizeof(l), f)) {
3408 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r));
3413 if (*t != '$' && *t != '<')
3416 parsed = strv_split(t,WHITESPACE);
3417 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3418 if (parsed && !startswith_no_case (parsed[0], "<interactive>")) {
3421 if (sysv_translate_facility(parsed[0], NULL, &facility) < 0)
3423 if ((u = manager_get_unit(mgr, facility)) && (u->type == UNIT_TARGET)) {
3425 char *dep = NULL, *name, **j;
3427 STRV_FOREACH (j, parsed+1) {
3436 if (sysv_translate_facility(name, NULL, &dep) < 0)
3439 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, e, dep, NULL, true);
3454 static int service_enumerate(Manager *m) {
3458 char *path = NULL, *fpath = NULL, *name = NULL;
3459 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3466 if (m->running_as != SYSTEMD_SYSTEM)
3469 zero(runlevel_services);
3471 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3472 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3476 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3485 if (!(d = opendir(path))) {
3486 if (errno != ENOENT)
3487 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3492 while ((de = readdir(d))) {
3495 if (ignore_file(de->d_name))
3498 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3501 if (strlen(de->d_name) < 4)
3504 a = undecchar(de->d_name[1]);
3505 b = undecchar(de->d_name[2]);
3511 fpath = strjoin(path, "/", de->d_name, NULL);
3517 if (access(fpath, X_OK) < 0) {
3519 if (errno != ENOENT)
3520 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3526 if (!(name = sysv_translate_name(de->d_name + 3))) {
3531 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
3532 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3536 if (de->d_name[0] == 'S') {
3538 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
3539 SERVICE(service)->sysv_start_priority_from_rcnd =
3540 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3542 SERVICE(service)->sysv_enabled = true;
3545 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3548 if ((r = set_put(runlevel_services[i], service)) < 0)
3551 } else if (de->d_name[0] == 'K' &&
3552 (rcnd_table[i].type == RUNLEVEL_DOWN ||
3553 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
3555 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3558 if ((r = set_put(shutdown_services, service)) < 0)
3564 /* Now we loaded all stubs and are aware of the lowest
3565 start-up priority for all services, not let's actually load
3566 the services, this will also tell us which services are
3567 actually native now */
3568 manager_dispatch_load_queue(m);
3570 /* If this is a native service, rely on native ways to pull in
3571 * a service, don't pull it in via sysv rcN.d links. */
3572 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3573 SET_FOREACH(service, runlevel_services[i], j) {
3574 service = unit_follow_merge(service);
3576 if (service->fragment_path)
3579 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3583 /* We honour K links only for halt/reboot. For the normal
3584 * runlevels we assume the stop jobs will be implicitly added
3585 * by the core logic. Also, we don't really distinguish here
3586 * between the runlevels 0 and 6 and just add them to the
3587 * special shutdown target. On SUSE the boot.d/ runlevel is
3588 * also used for shutdown, so we add links for that too to the
3589 * shutdown target.*/
3590 SET_FOREACH(service, shutdown_services, j) {
3591 service = unit_follow_merge(service);
3593 if (service->fragment_path)
3596 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3603 sysv_facility_in_insserv_conf (m);
3611 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3612 set_free(runlevel_services[i]);
3613 set_free(shutdown_services);
3622 static void service_bus_name_owner_change(
3625 const char *old_owner,
3626 const char *new_owner) {
3628 Service *s = SERVICE(u);
3633 assert(streq(s->bus_name, name));
3634 assert(old_owner || new_owner);
3636 if (old_owner && new_owner)
3637 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
3639 log_debug("%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
3641 log_debug("%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
3643 s->bus_name_good = !!new_owner;
3645 if (s->type == SERVICE_DBUS) {
3647 /* service_enter_running() will figure out what to
3649 if (s->state == SERVICE_RUNNING)
3650 service_enter_running(s, SERVICE_SUCCESS);
3651 else if (s->state == SERVICE_START && new_owner)
3652 service_enter_start_post(s);
3654 } else if (new_owner &&
3656 (s->state == SERVICE_START ||
3657 s->state == SERVICE_START_POST ||
3658 s->state == SERVICE_RUNNING ||
3659 s->state == SERVICE_RELOAD)) {
3661 /* Try to acquire PID from bus service */
3662 log_debug("Trying to acquire PID from D-Bus name...");
3664 bus_query_pid(u->manager, name);
3668 static void service_bus_query_pid_done(
3673 Service *s = SERVICE(u);
3678 log_debug("%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3680 if (s->main_pid <= 0 &&
3681 (s->state == SERVICE_START ||
3682 s->state == SERVICE_START_POST ||
3683 s->state == SERVICE_RUNNING ||
3684 s->state == SERVICE_RELOAD))
3685 service_set_main_pid(s, pid);
3688 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3693 /* This is called by the socket code when instantiating a new
3694 * service for a stream socket and the socket needs to be
3697 if (UNIT(s)->load_state != UNIT_LOADED)
3700 if (s->socket_fd >= 0)
3703 if (s->state != SERVICE_DEAD)
3707 s->got_socket_fd = true;
3709 unit_ref_set(&s->accept_socket, UNIT(sock));
3711 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3714 static void service_reset_failed(Unit *u) {
3715 Service *s = SERVICE(u);
3719 if (s->state == SERVICE_FAILED)
3720 service_set_state(s, SERVICE_DEAD);
3722 s->result = SERVICE_SUCCESS;
3723 s->reload_result = SERVICE_SUCCESS;
3725 RATELIMIT_RESET(s->start_limit);
3728 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3729 Service *s = SERVICE(u);
3731 Set *pid_set = NULL;
3735 if (s->main_pid <= 0 && who == KILL_MAIN) {
3736 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3740 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3741 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3745 if (who == KILL_CONTROL || who == KILL_ALL)
3746 if (s->control_pid > 0)
3747 if (kill(s->control_pid, signo) < 0)
3750 if (who == KILL_MAIN || who == KILL_ALL)
3751 if (s->main_pid > 0)
3752 if (kill(s->main_pid, signo) < 0)
3755 if (who == KILL_ALL) {
3758 pid_set = set_new(trivial_hash_func, trivial_compare_func);
3762 /* Exclude the control/main pid from being killed via the cgroup */
3763 if (s->control_pid > 0) {
3764 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
3771 if (s->main_pid > 0) {
3772 q = set_put(pid_set, LONG_TO_PTR(s->main_pid));
3779 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3780 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3791 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3792 [SERVICE_DEAD] = "dead",
3793 [SERVICE_START_PRE] = "start-pre",
3794 [SERVICE_START] = "start",
3795 [SERVICE_START_POST] = "start-post",
3796 [SERVICE_RUNNING] = "running",
3797 [SERVICE_EXITED] = "exited",
3798 [SERVICE_RELOAD] = "reload",
3799 [SERVICE_STOP] = "stop",
3800 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3801 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3802 [SERVICE_STOP_POST] = "stop-post",
3803 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3804 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3805 [SERVICE_FAILED] = "failed",
3806 [SERVICE_AUTO_RESTART] = "auto-restart",
3809 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3811 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3812 [SERVICE_RESTART_NO] = "no",
3813 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3814 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3815 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3816 [SERVICE_RESTART_ALWAYS] = "always"
3819 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3821 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3822 [SERVICE_SIMPLE] = "simple",
3823 [SERVICE_FORKING] = "forking",
3824 [SERVICE_ONESHOT] = "oneshot",
3825 [SERVICE_DBUS] = "dbus",
3826 [SERVICE_NOTIFY] = "notify",
3827 [SERVICE_IDLE] = "idle"
3830 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3832 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3833 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3834 [SERVICE_EXEC_START] = "ExecStart",
3835 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3836 [SERVICE_EXEC_RELOAD] = "ExecReload",
3837 [SERVICE_EXEC_STOP] = "ExecStop",
3838 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3841 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3843 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3844 [NOTIFY_NONE] = "none",
3845 [NOTIFY_MAIN] = "main",
3846 [NOTIFY_ALL] = "all"
3849 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3851 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3852 [SERVICE_SUCCESS] = "success",
3853 [SERVICE_FAILURE_RESOURCES] = "resources",
3854 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3855 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3856 [SERVICE_FAILURE_SIGNAL] = "signal",
3857 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3858 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3859 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3862 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3864 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3865 [SERVICE_START_LIMIT_NONE] = "none",
3866 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3867 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3868 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3870 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3872 const UnitVTable service_vtable = {
3873 .object_size = sizeof(Service),
3874 .exec_context_offset = offsetof(Service, exec_context),
3881 .init = service_init,
3882 .done = service_done,
3883 .load = service_load,
3885 .coldplug = service_coldplug,
3887 .dump = service_dump,
3889 .start = service_start,
3890 .stop = service_stop,
3891 .reload = service_reload,
3893 .can_reload = service_can_reload,
3895 .kill = service_kill,
3897 .serialize = service_serialize,
3898 .deserialize_item = service_deserialize_item,
3900 .active_state = service_active_state,
3901 .sub_state_to_string = service_sub_state_to_string,
3903 .check_gc = service_check_gc,
3904 .check_snapshot = service_check_snapshot,
3906 .sigchld_event = service_sigchld_event,
3907 .timer_event = service_timer_event,
3908 .fd_event = service_fd_event,
3910 .reset_failed = service_reset_failed,
3912 .cgroup_notify_empty = service_cgroup_notify_event,
3913 .notify_message = service_notify_message,
3915 .bus_name_owner_change = service_bus_name_owner_change,
3916 .bus_query_pid_done = service_bus_query_pid_done,
3918 .bus_interface = "org.freedesktop.systemd1.Service",
3919 .bus_message_handler = bus_service_message_handler,
3920 .bus_invalidating_properties = bus_service_invalidating_properties,
3922 #ifdef HAVE_SYSV_COMPAT
3923 .enumerate = service_enumerate,
3925 .status_message_formats = {
3926 .starting_stopping = {
3927 [0] = "Starting %s...",
3928 [1] = "Stopping %s...",
3930 .finished_start_job = {
3931 [JOB_DONE] = "Started %s.",
3932 [JOB_FAILED] = "Failed to start %s.",
3933 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3934 [JOB_TIMEOUT] = "Timed out starting %s.",
3936 .finished_stop_job = {
3937 [JOB_DONE] = "Stopped %s.",
3938 [JOB_FAILED] = "Stopped (with error) %s.",
3939 [JOB_TIMEOUT] = "Timed out stopping %s.",