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);
2488 reboot(RB_AUTOBOOT);
2492 log_error("start limit action=%i", s->start_limit_action);
2493 assert_not_reached("Unknown StartLimitAction.");
2499 static int service_start(Unit *u) {
2500 Service *s = SERVICE(u);
2505 /* We cannot fulfill this request right now, try again later
2507 if (s->state == SERVICE_STOP ||
2508 s->state == SERVICE_STOP_SIGTERM ||
2509 s->state == SERVICE_STOP_SIGKILL ||
2510 s->state == SERVICE_STOP_POST ||
2511 s->state == SERVICE_FINAL_SIGTERM ||
2512 s->state == SERVICE_FINAL_SIGKILL)
2515 /* Already on it! */
2516 if (s->state == SERVICE_START_PRE ||
2517 s->state == SERVICE_START ||
2518 s->state == SERVICE_START_POST)
2521 /* A service that will be restarted must be stopped first to
2522 * trigger BindsTo and/or OnFailure dependencies. If a user
2523 * does not want to wait for the holdoff time to elapse, the
2524 * service should be manually restarted, not started. We
2525 * simply return EAGAIN here, so that any start jobs stay
2526 * queued, and assume that the auto restart timer will
2527 * eventually trigger the restart. */
2528 if (s->state == SERVICE_AUTO_RESTART)
2531 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2533 /* Make sure we don't enter a busy loop of some kind. */
2534 r = service_start_limit_test(s);
2536 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2540 s->result = SERVICE_SUCCESS;
2541 s->reload_result = SERVICE_SUCCESS;
2542 s->main_pid_known = false;
2543 s->main_pid_alien = false;
2544 s->forbid_restart = false;
2546 service_enter_start_pre(s);
2550 static int service_stop(Unit *u) {
2551 Service *s = SERVICE(u);
2555 /* Don't create restart jobs from here. */
2556 s->forbid_restart = true;
2559 if (s->state == SERVICE_STOP ||
2560 s->state == SERVICE_STOP_SIGTERM ||
2561 s->state == SERVICE_STOP_SIGKILL ||
2562 s->state == SERVICE_STOP_POST ||
2563 s->state == SERVICE_FINAL_SIGTERM ||
2564 s->state == SERVICE_FINAL_SIGKILL)
2567 /* A restart will be scheduled or is in progress. */
2568 if (s->state == SERVICE_AUTO_RESTART) {
2569 service_set_state(s, SERVICE_DEAD);
2573 /* If there's already something running we go directly into
2575 if (s->state == SERVICE_START_PRE ||
2576 s->state == SERVICE_START ||
2577 s->state == SERVICE_START_POST ||
2578 s->state == SERVICE_RELOAD) {
2579 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2583 assert(s->state == SERVICE_RUNNING ||
2584 s->state == SERVICE_EXITED);
2586 service_enter_stop(s, SERVICE_SUCCESS);
2590 static int service_reload(Unit *u) {
2591 Service *s = SERVICE(u);
2595 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2597 service_enter_reload(s);
2601 static bool service_can_reload(Unit *u) {
2602 Service *s = SERVICE(u);
2606 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2609 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2610 Service *s = SERVICE(u);
2616 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2617 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2618 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2620 if (s->control_pid > 0)
2621 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2623 if (s->main_pid_known && s->main_pid > 0)
2624 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2626 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2629 unit_serialize_item(u, f, "status-text", s->status_text);
2631 /* FIXME: There's a minor uncleanliness here: if there are
2632 * multiple commands attached here, we will start from the
2633 * first one again */
2634 if (s->control_command_id >= 0)
2635 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2637 if (s->socket_fd >= 0) {
2640 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2643 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2646 if (s->main_exec_status.pid > 0) {
2647 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2648 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2649 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2651 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2652 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2653 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2656 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2657 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2662 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2663 Service *s = SERVICE(u);
2670 if (streq(key, "state")) {
2673 if ((state = service_state_from_string(value)) < 0)
2674 log_debug("Failed to parse state value %s", value);
2676 s->deserialized_state = state;
2677 } else if (streq(key, "result")) {
2680 f = service_result_from_string(value);
2682 log_debug("Failed to parse result value %s", value);
2683 else if (f != SERVICE_SUCCESS)
2686 } else if (streq(key, "reload-result")) {
2689 f = service_result_from_string(value);
2691 log_debug("Failed to parse reload result value %s", value);
2692 else if (f != SERVICE_SUCCESS)
2693 s->reload_result = f;
2695 } else if (streq(key, "control-pid")) {
2698 if (parse_pid(value, &pid) < 0)
2699 log_debug("Failed to parse control-pid value %s", value);
2701 s->control_pid = pid;
2702 } else if (streq(key, "main-pid")) {
2705 if (parse_pid(value, &pid) < 0)
2706 log_debug("Failed to parse main-pid value %s", value);
2708 service_set_main_pid(s, (pid_t) pid);
2709 } else if (streq(key, "main-pid-known")) {
2712 if ((b = parse_boolean(value)) < 0)
2713 log_debug("Failed to parse main-pid-known value %s", value);
2715 s->main_pid_known = b;
2716 } else if (streq(key, "status-text")) {
2719 if ((t = strdup(value))) {
2720 free(s->status_text);
2724 } else if (streq(key, "control-command")) {
2725 ServiceExecCommand id;
2727 if ((id = service_exec_command_from_string(value)) < 0)
2728 log_debug("Failed to parse exec-command value %s", value);
2730 s->control_command_id = id;
2731 s->control_command = s->exec_command[id];
2733 } else if (streq(key, "socket-fd")) {
2736 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2737 log_debug("Failed to parse socket-fd value %s", value);
2740 if (s->socket_fd >= 0)
2741 close_nointr_nofail(s->socket_fd);
2742 s->socket_fd = fdset_remove(fds, fd);
2744 } else if (streq(key, "main-exec-status-pid")) {
2747 if (parse_pid(value, &pid) < 0)
2748 log_debug("Failed to parse main-exec-status-pid value %s", value);
2750 s->main_exec_status.pid = pid;
2751 } else if (streq(key, "main-exec-status-code")) {
2754 if (safe_atoi(value, &i) < 0)
2755 log_debug("Failed to parse main-exec-status-code value %s", value);
2757 s->main_exec_status.code = i;
2758 } else if (streq(key, "main-exec-status-status")) {
2761 if (safe_atoi(value, &i) < 0)
2762 log_debug("Failed to parse main-exec-status-status value %s", value);
2764 s->main_exec_status.status = i;
2765 } else if (streq(key, "main-exec-status-start"))
2766 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2767 else if (streq(key, "main-exec-status-exit"))
2768 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2769 else if (streq(key, "watchdog-timestamp"))
2770 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2772 log_debug("Unknown serialization key '%s'", key);
2777 static UnitActiveState service_active_state(Unit *u) {
2778 const UnitActiveState *table;
2782 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2784 return table[SERVICE(u)->state];
2787 static const char *service_sub_state_to_string(Unit *u) {
2790 return service_state_to_string(SERVICE(u)->state);
2793 static bool service_check_gc(Unit *u) {
2794 Service *s = SERVICE(u);
2798 /* Never clean up services that still have a process around,
2799 * even if the service is formally dead. */
2800 if (cgroup_good(s) > 0 ||
2801 main_pid_good(s) > 0 ||
2802 control_pid_good(s) > 0)
2805 #ifdef HAVE_SYSV_COMPAT
2813 static bool service_check_snapshot(Unit *u) {
2814 Service *s = SERVICE(u);
2818 return !s->got_socket_fd;
2821 static int service_retry_pid_file(Service *s) {
2824 assert(s->pid_file);
2825 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2827 r = service_load_pid_file(s, false);
2831 service_unwatch_pid_file(s);
2833 service_enter_running(s, SERVICE_SUCCESS);
2837 static int service_watch_pid_file(Service *s) {
2840 log_debug("Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
2841 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2845 /* the pidfile might have appeared just before we set the watch */
2846 service_retry_pid_file(s);
2850 log_error("Failed to set a watch for %s's PID file %s: %s",
2851 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2852 service_unwatch_pid_file(s);
2856 static int service_demand_pid_file(Service *s) {
2859 assert(s->pid_file);
2860 assert(!s->pid_file_pathspec);
2862 ps = new0(PathSpec, 1);
2866 ps->path = strdup(s->pid_file);
2872 path_kill_slashes(ps->path);
2874 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2875 * keep their PID file open all the time. */
2876 ps->type = PATH_MODIFIED;
2877 ps->inotify_fd = -1;
2879 s->pid_file_pathspec = ps;
2881 return service_watch_pid_file(s);
2884 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2885 Service *s = SERVICE(u);
2889 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2890 assert(s->pid_file_pathspec);
2891 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2893 log_debug("inotify event for %s", u->id);
2895 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2898 if (service_retry_pid_file(s) == 0)
2901 if (service_watch_pid_file(s) < 0)
2906 service_unwatch_pid_file(s);
2907 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2910 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2911 Service *s = SERVICE(u);
2917 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2918 is_clean_exit_lsb(code, status, &s->success_status))
2919 f = SERVICE_SUCCESS;
2920 else if (code == CLD_EXITED)
2921 f = SERVICE_FAILURE_EXIT_CODE;
2922 else if (code == CLD_KILLED)
2923 f = SERVICE_FAILURE_SIGNAL;
2924 else if (code == CLD_DUMPED)
2925 f = SERVICE_FAILURE_CORE_DUMP;
2927 assert_not_reached("Unknown code");
2929 if (s->main_pid == pid) {
2930 /* Forking services may occasionally move to a new PID.
2931 * As long as they update the PID file before exiting the old
2932 * PID, they're fine. */
2933 if (service_load_pid_file(s, false) == 0)
2937 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2939 /* If this is not a forking service than the main
2940 * process got started and hence we copy the exit
2941 * status so that it is recorded both as main and as
2942 * control process exit status */
2943 if (s->main_command) {
2944 s->main_command->exec_status = s->main_exec_status;
2946 if (s->main_command->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("%s running next main command for state %s", u->id, service_state_to_string(s->state));
2972 service_run_next_main(s);
2976 /* The service exited, so the service is officially
2978 s->main_command = NULL;
2982 case SERVICE_START_POST:
2983 case SERVICE_RELOAD:
2985 /* Need to wait until the operation is
2990 if (s->type == SERVICE_ONESHOT) {
2991 /* This was our main goal, so let's go on */
2992 if (f == SERVICE_SUCCESS)
2993 service_enter_start_post(s);
2995 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3001 case SERVICE_RUNNING:
3002 service_enter_running(s, f);
3005 case SERVICE_STOP_SIGTERM:
3006 case SERVICE_STOP_SIGKILL:
3008 if (!control_pid_good(s))
3009 service_enter_stop_post(s, f);
3011 /* If there is still a control process, wait for that first */
3015 assert_not_reached("Uh, main process died at wrong time.");
3019 } else if (s->control_pid == pid) {
3023 if (s->control_command) {
3024 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
3026 if (s->control_command->ignore)
3027 f = SERVICE_SUCCESS;
3030 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
3031 "%s: control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
3033 if (f != SERVICE_SUCCESS)
3036 /* Immediately get rid of the cgroup, so that the
3037 * kernel doesn't delay the cgroup empty messages for
3038 * the service cgroup any longer than necessary */
3039 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
3041 if (s->control_command &&
3042 s->control_command->command_next &&
3043 f == SERVICE_SUCCESS) {
3045 /* There is another command to *
3046 * execute, so let's do that. */
3048 log_debug("%s running next control command for state %s", u->id, service_state_to_string(s->state));
3049 service_run_next_control(s);
3052 /* No further commands for this step, so let's
3053 * figure out what to do next */
3055 s->control_command = NULL;
3056 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3058 log_debug("%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
3062 case SERVICE_START_PRE:
3063 if (f == SERVICE_SUCCESS)
3064 service_enter_start(s);
3066 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3070 if (s->type != SERVICE_FORKING)
3071 /* Maybe spurious event due to a reload that changed the type? */
3074 if (f != SERVICE_SUCCESS) {
3075 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3080 bool has_start_post;
3083 /* Let's try to load the pid file here if we can.
3084 * The PID file might actually be created by a START_POST
3085 * script. In that case don't worry if the loading fails. */
3087 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3088 r = service_load_pid_file(s, !has_start_post);
3089 if (!has_start_post && r < 0) {
3090 r = service_demand_pid_file(s);
3091 if (r < 0 || !cgroup_good(s))
3092 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3096 service_search_main_pid(s);
3098 service_enter_start_post(s);
3101 case SERVICE_START_POST:
3102 if (f != SERVICE_SUCCESS) {
3103 service_enter_stop(s, f);
3110 r = service_load_pid_file(s, true);
3112 r = service_demand_pid_file(s);
3113 if (r < 0 || !cgroup_good(s))
3114 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3118 service_search_main_pid(s);
3120 service_enter_running(s, SERVICE_SUCCESS);
3123 case SERVICE_RELOAD:
3124 if (f == SERVICE_SUCCESS) {
3125 service_load_pid_file(s, true);
3126 service_search_main_pid(s);
3129 s->reload_result = f;
3130 service_enter_running(s, SERVICE_SUCCESS);
3134 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3137 case SERVICE_STOP_SIGTERM:
3138 case SERVICE_STOP_SIGKILL:
3139 if (main_pid_good(s) <= 0)
3140 service_enter_stop_post(s, f);
3142 /* If there is still a service
3143 * process around, wait until
3144 * that one quit, too */
3147 case SERVICE_STOP_POST:
3148 case SERVICE_FINAL_SIGTERM:
3149 case SERVICE_FINAL_SIGKILL:
3150 service_enter_dead(s, f, true);
3154 assert_not_reached("Uh, control process died at wrong time.");
3159 /* Notify clients about changed exit status */
3160 unit_add_to_dbus_queue(u);
3163 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3164 Service *s = SERVICE(u);
3167 assert(elapsed == 1);
3169 if (w == &s->watchdog_watch) {
3170 service_handle_watchdog(s);
3174 assert(w == &s->timer_watch);
3178 case SERVICE_START_PRE:
3180 log_warning("%s operation timed out. Terminating.", u->id);
3181 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3184 case SERVICE_START_POST:
3185 log_warning("%s operation timed out. Stopping.", u->id);
3186 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3189 case SERVICE_RELOAD:
3190 log_warning("%s operation timed out. Stopping.", u->id);
3191 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3192 service_enter_running(s, SERVICE_SUCCESS);
3196 log_warning("%s stopping timed out. Terminating.", u->id);
3197 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3200 case SERVICE_STOP_SIGTERM:
3201 if (s->kill_context.send_sigkill) {
3202 log_warning("%s stopping timed out. Killing.", u->id);
3203 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3205 log_warning("%s stopping timed out. Skipping SIGKILL.", u->id);
3206 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3211 case SERVICE_STOP_SIGKILL:
3212 /* Uh, we sent a SIGKILL and it is still not gone?
3213 * Must be something we cannot kill, so let's just be
3214 * weirded out and continue */
3216 log_warning("%s still around after SIGKILL. Ignoring.", u->id);
3217 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3220 case SERVICE_STOP_POST:
3221 log_warning("%s stopping timed out (2). Terminating.", u->id);
3222 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3225 case SERVICE_FINAL_SIGTERM:
3226 if (s->kill_context.send_sigkill) {
3227 log_warning("%s stopping timed out (2). Killing.", u->id);
3228 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3230 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->id);
3231 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3236 case SERVICE_FINAL_SIGKILL:
3237 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->id);
3238 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3241 case SERVICE_AUTO_RESTART:
3242 log_info("%s holdoff time over, scheduling restart.", u->id);
3243 service_enter_restart(s);
3247 assert_not_reached("Timeout at wrong time.");
3251 static void service_cgroup_notify_event(Unit *u) {
3252 Service *s = SERVICE(u);
3256 log_debug("%s: cgroup is empty", u->id);
3260 /* Waiting for SIGCHLD is usually more interesting,
3261 * because it includes return codes/signals. Which is
3262 * why we ignore the cgroup events for most cases,
3263 * except when we don't know pid which to expect the
3267 case SERVICE_START_POST:
3268 /* If we were hoping for the daemon to write its PID file,
3269 * we can give up now. */
3270 if (s->pid_file_pathspec) {
3271 log_warning("%s never wrote its PID file. Failing.", UNIT(s)->id);
3272 service_unwatch_pid_file(s);
3273 if (s->state == SERVICE_START)
3274 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3276 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3280 case SERVICE_RUNNING:
3281 /* service_enter_running() will figure out what to do */
3282 service_enter_running(s, SERVICE_SUCCESS);
3285 case SERVICE_STOP_SIGTERM:
3286 case SERVICE_STOP_SIGKILL:
3288 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3289 service_enter_stop_post(s, SERVICE_SUCCESS);
3293 case SERVICE_FINAL_SIGTERM:
3294 case SERVICE_FINAL_SIGKILL:
3295 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3296 service_enter_dead(s, SERVICE_SUCCESS, true);
3305 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3306 Service *s = SERVICE(u);
3311 if (s->notify_access == NOTIFY_NONE) {
3312 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3313 u->id, (unsigned long) pid);
3317 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3318 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3319 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3323 log_debug("%s: Got message", u->id);
3325 /* Interpret MAINPID= */
3326 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3327 (s->state == SERVICE_START ||
3328 s->state == SERVICE_START_POST ||
3329 s->state == SERVICE_RUNNING ||
3330 s->state == SERVICE_RELOAD)) {
3332 if (parse_pid(e + 8, &pid) < 0)
3333 log_warning("Failed to parse notification message %s", e);
3335 log_debug("%s: got %s", u->id, e);
3336 service_set_main_pid(s, pid);
3340 /* Interpret READY= */
3341 if (s->type == SERVICE_NOTIFY &&
3342 s->state == SERVICE_START &&
3343 strv_find(tags, "READY=1")) {
3344 log_debug("%s: got READY=1", u->id);
3346 service_enter_start_post(s);
3349 /* Interpret STATUS= */
3350 e = strv_find_prefix(tags, "STATUS=");
3356 if (!utf8_is_valid(e+7)) {
3357 log_warning("Status message in notification is not UTF-8 clean.");
3363 log_error("Failed to allocate string.");
3367 log_debug("%s: got %s", u->id, e);
3369 free(s->status_text);
3372 free(s->status_text);
3373 s->status_text = NULL;
3377 if (strv_find(tags, "WATCHDOG=1")) {
3378 log_debug("%s: got WATCHDOG=1", u->id);
3379 service_reset_watchdog(s);
3382 /* Notify clients about changed status or main pid */
3383 unit_add_to_dbus_queue(u);
3386 #ifdef HAVE_SYSV_COMPAT
3389 static void sysv_facility_in_insserv_conf(Manager *mgr) {
3393 if (!(f = fopen("/etc/insserv.conf", "re"))) {
3394 r = errno == ENOENT ? 0 : -errno;
3399 char l[LINE_MAX], *t;
3400 char **parsed = NULL;
3402 if (!fgets(l, sizeof(l), f)) {
3407 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r));
3412 if (*t != '$' && *t != '<')
3415 parsed = strv_split(t,WHITESPACE);
3416 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3417 if (parsed && !startswith_no_case (parsed[0], "<interactive>")) {
3420 if (sysv_translate_facility(parsed[0], NULL, &facility) < 0)
3422 if ((u = manager_get_unit(mgr, facility)) && (u->type == UNIT_TARGET)) {
3424 char *dep = NULL, *name, **j;
3426 STRV_FOREACH (j, parsed+1) {
3435 if (sysv_translate_facility(name, NULL, &dep) < 0)
3438 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, e, dep, NULL, true);
3453 static int service_enumerate(Manager *m) {
3457 char *path = NULL, *fpath = NULL, *name = NULL;
3458 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3465 if (m->running_as != SYSTEMD_SYSTEM)
3468 zero(runlevel_services);
3470 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3471 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3475 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3484 if (!(d = opendir(path))) {
3485 if (errno != ENOENT)
3486 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3491 while ((de = readdir(d))) {
3494 if (ignore_file(de->d_name))
3497 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3500 if (strlen(de->d_name) < 4)
3503 a = undecchar(de->d_name[1]);
3504 b = undecchar(de->d_name[2]);
3510 fpath = strjoin(path, "/", de->d_name, NULL);
3516 if (access(fpath, X_OK) < 0) {
3518 if (errno != ENOENT)
3519 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3525 if (!(name = sysv_translate_name(de->d_name + 3))) {
3530 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
3531 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3535 if (de->d_name[0] == 'S') {
3537 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
3538 SERVICE(service)->sysv_start_priority_from_rcnd =
3539 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3541 SERVICE(service)->sysv_enabled = true;
3544 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3547 if ((r = set_put(runlevel_services[i], service)) < 0)
3550 } else if (de->d_name[0] == 'K' &&
3551 (rcnd_table[i].type == RUNLEVEL_DOWN ||
3552 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
3554 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3557 if ((r = set_put(shutdown_services, service)) < 0)
3563 /* Now we loaded all stubs and are aware of the lowest
3564 start-up priority for all services, not let's actually load
3565 the services, this will also tell us which services are
3566 actually native now */
3567 manager_dispatch_load_queue(m);
3569 /* If this is a native service, rely on native ways to pull in
3570 * a service, don't pull it in via sysv rcN.d links. */
3571 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3572 SET_FOREACH(service, runlevel_services[i], j) {
3573 service = unit_follow_merge(service);
3575 if (service->fragment_path)
3578 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3582 /* We honour K links only for halt/reboot. For the normal
3583 * runlevels we assume the stop jobs will be implicitly added
3584 * by the core logic. Also, we don't really distinguish here
3585 * between the runlevels 0 and 6 and just add them to the
3586 * special shutdown target. On SUSE the boot.d/ runlevel is
3587 * also used for shutdown, so we add links for that too to the
3588 * shutdown target.*/
3589 SET_FOREACH(service, shutdown_services, j) {
3590 service = unit_follow_merge(service);
3592 if (service->fragment_path)
3595 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3602 sysv_facility_in_insserv_conf (m);
3610 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3611 set_free(runlevel_services[i]);
3612 set_free(shutdown_services);
3621 static void service_bus_name_owner_change(
3624 const char *old_owner,
3625 const char *new_owner) {
3627 Service *s = SERVICE(u);
3632 assert(streq(s->bus_name, name));
3633 assert(old_owner || new_owner);
3635 if (old_owner && new_owner)
3636 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
3638 log_debug("%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
3640 log_debug("%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
3642 s->bus_name_good = !!new_owner;
3644 if (s->type == SERVICE_DBUS) {
3646 /* service_enter_running() will figure out what to
3648 if (s->state == SERVICE_RUNNING)
3649 service_enter_running(s, SERVICE_SUCCESS);
3650 else if (s->state == SERVICE_START && new_owner)
3651 service_enter_start_post(s);
3653 } else if (new_owner &&
3655 (s->state == SERVICE_START ||
3656 s->state == SERVICE_START_POST ||
3657 s->state == SERVICE_RUNNING ||
3658 s->state == SERVICE_RELOAD)) {
3660 /* Try to acquire PID from bus service */
3661 log_debug("Trying to acquire PID from D-Bus name...");
3663 bus_query_pid(u->manager, name);
3667 static void service_bus_query_pid_done(
3672 Service *s = SERVICE(u);
3677 log_debug("%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3679 if (s->main_pid <= 0 &&
3680 (s->state == SERVICE_START ||
3681 s->state == SERVICE_START_POST ||
3682 s->state == SERVICE_RUNNING ||
3683 s->state == SERVICE_RELOAD))
3684 service_set_main_pid(s, pid);
3687 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3692 /* This is called by the socket code when instantiating a new
3693 * service for a stream socket and the socket needs to be
3696 if (UNIT(s)->load_state != UNIT_LOADED)
3699 if (s->socket_fd >= 0)
3702 if (s->state != SERVICE_DEAD)
3706 s->got_socket_fd = true;
3708 unit_ref_set(&s->accept_socket, UNIT(sock));
3710 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3713 static void service_reset_failed(Unit *u) {
3714 Service *s = SERVICE(u);
3718 if (s->state == SERVICE_FAILED)
3719 service_set_state(s, SERVICE_DEAD);
3721 s->result = SERVICE_SUCCESS;
3722 s->reload_result = SERVICE_SUCCESS;
3724 RATELIMIT_RESET(s->start_limit);
3727 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3728 Service *s = SERVICE(u);
3730 Set *pid_set = NULL;
3734 if (s->main_pid <= 0 && who == KILL_MAIN) {
3735 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3739 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3740 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3744 if (who == KILL_CONTROL || who == KILL_ALL)
3745 if (s->control_pid > 0)
3746 if (kill(s->control_pid, signo) < 0)
3749 if (who == KILL_MAIN || who == KILL_ALL)
3750 if (s->main_pid > 0)
3751 if (kill(s->main_pid, signo) < 0)
3754 if (who == KILL_ALL) {
3757 pid_set = set_new(trivial_hash_func, trivial_compare_func);
3761 /* Exclude the control/main pid from being killed via the cgroup */
3762 if (s->control_pid > 0) {
3763 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
3770 if (s->main_pid > 0) {
3771 q = set_put(pid_set, LONG_TO_PTR(s->main_pid));
3778 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3779 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3790 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3791 [SERVICE_DEAD] = "dead",
3792 [SERVICE_START_PRE] = "start-pre",
3793 [SERVICE_START] = "start",
3794 [SERVICE_START_POST] = "start-post",
3795 [SERVICE_RUNNING] = "running",
3796 [SERVICE_EXITED] = "exited",
3797 [SERVICE_RELOAD] = "reload",
3798 [SERVICE_STOP] = "stop",
3799 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3800 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3801 [SERVICE_STOP_POST] = "stop-post",
3802 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3803 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3804 [SERVICE_FAILED] = "failed",
3805 [SERVICE_AUTO_RESTART] = "auto-restart",
3808 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3810 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3811 [SERVICE_RESTART_NO] = "no",
3812 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3813 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3814 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3815 [SERVICE_RESTART_ALWAYS] = "always"
3818 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3820 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3821 [SERVICE_SIMPLE] = "simple",
3822 [SERVICE_FORKING] = "forking",
3823 [SERVICE_ONESHOT] = "oneshot",
3824 [SERVICE_DBUS] = "dbus",
3825 [SERVICE_NOTIFY] = "notify",
3826 [SERVICE_IDLE] = "idle"
3829 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3831 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3832 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3833 [SERVICE_EXEC_START] = "ExecStart",
3834 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3835 [SERVICE_EXEC_RELOAD] = "ExecReload",
3836 [SERVICE_EXEC_STOP] = "ExecStop",
3837 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3840 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3842 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3843 [NOTIFY_NONE] = "none",
3844 [NOTIFY_MAIN] = "main",
3845 [NOTIFY_ALL] = "all"
3848 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3850 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3851 [SERVICE_SUCCESS] = "success",
3852 [SERVICE_FAILURE_RESOURCES] = "resources",
3853 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3854 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3855 [SERVICE_FAILURE_SIGNAL] = "signal",
3856 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3857 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3858 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3861 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3863 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3864 [SERVICE_START_LIMIT_NONE] = "none",
3865 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3866 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3867 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3869 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3871 const UnitVTable service_vtable = {
3872 .object_size = sizeof(Service),
3873 .exec_context_offset = offsetof(Service, exec_context),
3880 .init = service_init,
3881 .done = service_done,
3882 .load = service_load,
3884 .coldplug = service_coldplug,
3886 .dump = service_dump,
3888 .start = service_start,
3889 .stop = service_stop,
3890 .reload = service_reload,
3892 .can_reload = service_can_reload,
3894 .kill = service_kill,
3896 .serialize = service_serialize,
3897 .deserialize_item = service_deserialize_item,
3899 .active_state = service_active_state,
3900 .sub_state_to_string = service_sub_state_to_string,
3902 .check_gc = service_check_gc,
3903 .check_snapshot = service_check_snapshot,
3905 .sigchld_event = service_sigchld_event,
3906 .timer_event = service_timer_event,
3907 .fd_event = service_fd_event,
3909 .reset_failed = service_reset_failed,
3911 .cgroup_notify_empty = service_cgroup_notify_event,
3912 .notify_message = service_notify_message,
3914 .bus_name_owner_change = service_bus_name_owner_change,
3915 .bus_query_pid_done = service_bus_query_pid_done,
3917 .bus_interface = "org.freedesktop.systemd1.Service",
3918 .bus_message_handler = bus_service_message_handler,
3919 .bus_invalidating_properties = bus_service_invalidating_properties,
3921 #ifdef HAVE_SYSV_COMPAT
3922 .enumerate = service_enumerate,
3924 .status_message_formats = {
3925 .starting_stopping = {
3926 [0] = "Starting %s...",
3927 [1] = "Stopping %s...",
3929 .finished_start_job = {
3930 [JOB_DONE] = "Started %s.",
3931 [JOB_FAILED] = "Failed to start %s.",
3932 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3933 [JOB_TIMEOUT] = "Timed out starting %s.",
3935 .finished_stop_job = {
3936 [JOB_DONE] = "Stopped %s.",
3937 [JOB_FAILED] = "Stopped (with error) %s.",
3938 [JOB_TIMEOUT] = "Timed out stopping %s.",