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 "dbus-service.h"
38 #include "bus-errors.h"
39 #include "exit-status.h"
44 #ifdef HAVE_SYSV_COMPAT
46 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
48 typedef enum RunlevelType {
57 const RunlevelType type;
59 /* Standard SysV runlevels for start-up */
60 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
61 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
62 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
63 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
64 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
67 /* SUSE style boot.d */
68 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
71 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
72 /* Debian style rcS.d */
73 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
76 /* Standard SysV runlevels for shutdown */
77 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
78 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
80 /* Note that the order here matters, as we read the
81 directories in this order, and we want to make sure that
82 sysv_start_priority is known when we first load the
83 unit. And that value we only know from S links. Hence
84 UP/SYSINIT must be read before DOWN */
87 #define RUNLEVELS_UP "12345"
88 /* #define RUNLEVELS_DOWN "06" */
89 #define RUNLEVELS_BOOT "bBsS"
92 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
93 [SERVICE_DEAD] = UNIT_INACTIVE,
94 [SERVICE_START_PRE] = UNIT_ACTIVATING,
95 [SERVICE_START] = UNIT_ACTIVATING,
96 [SERVICE_START_POST] = UNIT_ACTIVATING,
97 [SERVICE_RUNNING] = UNIT_ACTIVE,
98 [SERVICE_EXITED] = UNIT_ACTIVE,
99 [SERVICE_RELOAD] = UNIT_RELOADING,
100 [SERVICE_STOP] = UNIT_DEACTIVATING,
101 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
102 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
103 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
104 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
105 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
106 [SERVICE_FAILED] = UNIT_FAILED,
107 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
110 static void service_init(Unit *u) {
111 Service *s = SERVICE(u);
114 assert(u->load_state == UNIT_STUB);
116 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
117 s->restart_usec = DEFAULT_RESTART_USEC;
118 s->type = _SERVICE_TYPE_INVALID;
120 s->watchdog_watch.type = WATCH_INVALID;
122 s->timer_watch.type = WATCH_INVALID;
123 #ifdef HAVE_SYSV_COMPAT
124 s->sysv_start_priority = -1;
125 s->sysv_start_priority_from_rcnd = -1;
128 s->guess_main_pid = true;
130 exec_context_init(&s->exec_context);
132 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
134 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
137 static void service_unwatch_control_pid(Service *s) {
140 if (s->control_pid <= 0)
143 unit_unwatch_pid(UNIT(s), s->control_pid);
147 static void service_unwatch_main_pid(Service *s) {
150 if (s->main_pid <= 0)
153 unit_unwatch_pid(UNIT(s), s->main_pid);
157 static void service_unwatch_pid_file(Service *s) {
158 if (!s->pid_file_pathspec)
161 log_debug("Stopping watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
162 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
163 path_spec_done(s->pid_file_pathspec);
164 free(s->pid_file_pathspec);
165 s->pid_file_pathspec = NULL;
168 static int service_set_main_pid(Service *s, pid_t pid) {
180 s->main_pid_known = true;
182 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
183 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
184 UNIT(s)->id, (unsigned long) pid);
186 s->main_pid_alien = true;
188 s->main_pid_alien = false;
190 exec_status_start(&s->main_exec_status, pid);
195 static void service_close_socket_fd(Service *s) {
198 if (s->socket_fd < 0)
201 close_nointr_nofail(s->socket_fd);
205 static void service_connection_unref(Service *s) {
208 if (!UNIT_DEREF(s->accept_socket))
211 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
212 unit_ref_unset(&s->accept_socket);
215 static void service_stop_watchdog(Service *s) {
218 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
219 s->watchdog_timestamp.realtime = 0;
220 s->watchdog_timestamp.monotonic = 0;
223 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart);
225 static void service_handle_watchdog(Service *s) {
231 if (s->watchdog_usec == 0)
234 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
235 if (offset >= s->watchdog_usec) {
236 log_error("%s watchdog timeout!", UNIT(s)->id);
237 service_enter_dead(s, SERVICE_FAILURE_WATCHDOG, true);
241 r = unit_watch_timer(UNIT(s), s->watchdog_usec - offset, &s->watchdog_watch);
243 log_warning("%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
246 static void service_reset_watchdog(Service *s) {
249 dual_timestamp_get(&s->watchdog_timestamp);
250 service_handle_watchdog(s);
253 static void service_done(Unit *u) {
254 Service *s = SERVICE(u);
261 #ifdef HAVE_SYSV_COMPAT
265 free(s->sysv_runlevels);
266 s->sysv_runlevels = NULL;
269 free(s->status_text);
270 s->status_text = NULL;
272 exec_context_done(&s->exec_context);
273 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
274 s->control_command = NULL;
275 s->main_command = NULL;
277 /* This will leak a process, but at least no memory or any of
279 service_unwatch_main_pid(s);
280 service_unwatch_control_pid(s);
281 service_unwatch_pid_file(s);
284 unit_unwatch_bus_name(u, s->bus_name);
289 service_close_socket_fd(s);
290 service_connection_unref(s);
292 unit_ref_unset(&s->accept_socket);
294 service_stop_watchdog(s);
296 unit_unwatch_timer(u, &s->timer_watch);
299 #ifdef HAVE_SYSV_COMPAT
300 static char *sysv_translate_name(const char *name) {
303 if (!(r = new(char, strlen(name) + sizeof(".service"))))
306 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
307 if (endswith(name, ".sh"))
308 /* Drop Debian-style .sh suffix */
309 strcpy(stpcpy(r, name) - 3, ".service");
312 if (startswith(name, "boot."))
313 /* Drop SuSE-style boot. prefix */
314 strcpy(stpcpy(r, name + 5), ".service");
316 #ifdef TARGET_FRUGALWARE
317 if (startswith(name, "rc."))
318 /* Drop Frugalware-style rc. prefix */
319 strcpy(stpcpy(r, name + 3), ".service");
322 /* Normal init scripts */
323 strcpy(stpcpy(r, name), ".service");
328 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
330 /* We silently ignore the $ prefix here. According to the LSB
331 * spec it simply indicates whether something is a
332 * standardized name or a distribution-specific one. Since we
333 * just follow what already exists and do not introduce new
334 * uses or names we don't care who introduced a new name. */
336 static const char * const table[] = {
337 /* LSB defined facilities */
338 "local_fs", SPECIAL_LOCAL_FS_TARGET,
339 #if defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
341 /* Due to unfortunate name selection in Mandriva,
342 * $network is provided by network-up which is ordered
343 * after network which actually starts interfaces.
344 * To break the loop, just ignore it */
345 "network", SPECIAL_NETWORK_TARGET,
347 "named", SPECIAL_NSS_LOOKUP_TARGET,
348 "portmap", SPECIAL_RPCBIND_TARGET,
349 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
350 "syslog", SPECIAL_SYSLOG_TARGET,
351 "time", SPECIAL_TIME_SYNC_TARGET,
353 /* common extensions */
354 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
355 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
358 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
359 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
363 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
364 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
365 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
369 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
380 n = *name == '$' ? name + 1 : name;
382 for (i = 0; i < ELEMENTSOF(table); i += 2) {
384 if (!streq(table[i], n))
390 if (!(r = strdup(table[i+1])))
396 /* If we don't know this name, fallback heuristics to figure
397 * out whether something is a target or a service alias. */
400 if (!unit_prefix_is_valid(n))
403 /* Facilities starting with $ are most likely targets */
404 r = unit_name_build(n, NULL, ".target");
405 } else if (filename && streq(name, filename))
406 /* Names equaling the file name of the services are redundant */
409 /* Everything else we assume to be normal service names */
410 r = sysv_translate_name(n);
421 static int sysv_fix_order(Service *s) {
427 if (s->sysv_start_priority < 0)
430 /* For each pair of services where at least one lacks a LSB
431 * header, we use the start priority value to order things. */
433 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
436 bool special_s, special_t;
443 if (UNIT(t)->load_state != UNIT_LOADED)
446 if (t->sysv_start_priority < 0)
449 /* If both units have modern headers we don't care
450 * about the priorities */
451 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
452 (UNIT(t)->fragment_path || t->sysv_has_lsb))
455 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
456 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
458 if (special_t && !special_s)
460 else if (special_s && !special_t)
462 else if (t->sysv_start_priority < s->sysv_start_priority)
464 else if (t->sysv_start_priority > s->sysv_start_priority)
469 /* FIXME: Maybe we should compare the name here lexicographically? */
471 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
478 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
481 if (!(c = new0(ExecCommand, 1)))
484 if (!(c->path = strdup(path))) {
489 if (!(c->argv = strv_new(path, arg1, NULL))) {
498 static int sysv_exec_commands(Service *s) {
502 assert(s->sysv_path);
504 if (!(c = exec_command_new(s->sysv_path, "start")))
506 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
508 if (!(c = exec_command_new(s->sysv_path, "stop")))
510 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
512 if (!(c = exec_command_new(s->sysv_path, "reload")))
514 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
519 static int service_load_sysv_path(Service *s, const char *path) {
530 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
538 if (!(f = fopen(path, "re"))) {
539 r = errno == ENOENT ? 0 : -errno;
544 if (fstat(fileno(f), &st) < 0) {
550 if (!(s->sysv_path = strdup(path))) {
555 s->sysv_mtime = timespec_load(&st.st_mtim);
557 if (null_or_empty(&st)) {
558 u->load_state = UNIT_MASKED;
564 char l[LINE_MAX], *t;
566 if (!fgets(l, sizeof(l), f)) {
571 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
581 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
583 s->sysv_has_lsb = true;
587 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
593 t += strspn(t, WHITESPACE);
595 if (state == NORMAL) {
597 /* Try to parse Red Hat style chkconfig headers */
599 if (startswith_no_case(t, "chkconfig:")) {
601 char runlevels[16], *k;
605 if (sscanf(t+10, "%15s %i %*i",
607 &start_priority) != 2) {
609 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
613 /* A start priority gathered from the
614 * symlink farms is preferred over the
615 * data from the LSB header. */
616 if (start_priority < 0 || start_priority > 99)
617 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
619 s->sysv_start_priority = start_priority;
621 char_array_0(runlevels);
622 k = delete_chars(runlevels, WHITESPACE "-");
627 if (!(d = strdup(k))) {
632 free(s->sysv_runlevels);
633 s->sysv_runlevels = d;
636 } else if (startswith_no_case(t, "description:")) {
638 size_t k = strlen(t);
642 if (t[k-1] == '\\') {
647 if ((j = strstrip(t+12)) && *j) {
648 if (!(d = strdup(j))) {
655 free(chkconfig_description);
656 chkconfig_description = d;
658 } else if (startswith_no_case(t, "pidfile:")) {
665 if (!path_is_absolute(fn)) {
666 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
670 if (!(fn = strdup(fn))) {
679 } else if (state == DESCRIPTION) {
681 /* Try to parse Red Hat style description
684 size_t k = strlen(t);
692 if ((j = strstrip(t)) && *j) {
695 if (chkconfig_description)
696 d = join(chkconfig_description, " ", j, NULL);
705 free(chkconfig_description);
706 chkconfig_description = d;
709 } else if (state == LSB || state == LSB_DESCRIPTION) {
711 if (startswith_no_case(t, "Provides:")) {
717 FOREACH_WORD_QUOTED(w, z, t+9, i) {
720 if (!(n = strndup(w, z))) {
725 r = sysv_translate_facility(n, file_name_from_path(path), &m);
734 if (unit_name_to_type(m) == UNIT_SERVICE)
735 r = unit_add_name(u, m);
742 * indication that the
744 * now available. This
747 * targets do NOT pull
750 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
753 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
758 } else if (startswith_no_case(t, "Required-Start:") ||
759 startswith_no_case(t, "Should-Start:") ||
760 startswith_no_case(t, "X-Start-Before:") ||
761 startswith_no_case(t, "X-Start-After:")) {
767 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
770 if (!(n = strndup(w, z))) {
775 r = sysv_translate_facility(n, file_name_from_path(path), &m);
778 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
788 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
791 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
795 } else if (startswith_no_case(t, "Default-Start:")) {
800 k = delete_chars(t+14, WHITESPACE "-");
803 if (!(d = strdup(k))) {
808 free(s->sysv_runlevels);
809 s->sysv_runlevels = d;
812 } else if (startswith_no_case(t, "Description:")) {
815 state = LSB_DESCRIPTION;
817 if ((j = strstrip(t+12)) && *j) {
818 if (!(d = strdup(j))) {
825 free(long_description);
826 long_description = d;
828 } else if (startswith_no_case(t, "Short-Description:")) {
833 if ((j = strstrip(t+18)) && *j) {
834 if (!(d = strdup(j))) {
841 free(short_description);
842 short_description = d;
844 } else if (state == LSB_DESCRIPTION) {
846 if (startswith(l, "#\t") || startswith(l, "# ")) {
849 if ((j = strstrip(t)) && *j) {
852 if (long_description)
853 d = join(long_description, " ", t, NULL);
862 free(long_description);
863 long_description = d;
872 if ((r = sysv_exec_commands(s)) < 0)
874 if (s->sysv_runlevels &&
875 chars_intersect(RUNLEVELS_BOOT, s->sysv_runlevels) &&
876 chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
877 /* Service has both boot and "up" runlevels
878 configured. Kill the "up" ones. */
879 delete_chars(s->sysv_runlevels, RUNLEVELS_UP);
882 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
883 /* If there a runlevels configured for this service
884 * but none of the standard ones, then we assume this
885 * is some special kind of service (which might be
886 * needed for early boot) and don't create any links
889 UNIT(s)->default_dependencies = false;
891 /* Don't timeout special services during boot (like fsck) */
894 s->timeout_usec = DEFAULT_SYSV_TIMEOUT_USEC;
896 /* Special setting for all SysV services */
897 s->type = SERVICE_FORKING;
898 s->remain_after_exit = !s->pid_file;
899 s->guess_main_pid = false;
900 s->restart = SERVICE_RESTART_NO;
901 s->exec_context.ignore_sigpipe = false;
903 if (UNIT(s)->manager->sysv_console)
904 s->exec_context.std_output = EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
906 s->exec_context.kill_mode = KILL_PROCESS;
908 /* We use the long description only if
909 * no short description is set. */
911 if (short_description)
912 description = short_description;
913 else if (chkconfig_description)
914 description = chkconfig_description;
915 else if (long_description)
916 description = long_description;
923 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
931 /* The priority that has been set in /etc/rcN.d/ hierarchies
932 * takes precedence over what is stored as default in the LSB
934 if (s->sysv_start_priority_from_rcnd >= 0)
935 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
937 u->load_state = UNIT_LOADED;
945 free(short_description);
946 free(long_description);
947 free(chkconfig_description);
952 static int service_load_sysv_name(Service *s, const char *name) {
958 /* For SysV services we strip the boot.*, rc.* and *.sh
959 * prefixes/suffixes. */
960 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
961 if (endswith(name, ".sh.service"))
966 if (startswith(name, "boot."))
970 #ifdef TARGET_FRUGALWARE
971 if (startswith(name, "rc."))
975 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
979 path = join(*p, "/", name, NULL);
983 assert(endswith(path, ".service"));
984 path[strlen(path)-8] = 0;
986 r = service_load_sysv_path(s, path);
988 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
989 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
990 /* Try Debian style *.sh source'able init scripts */
992 r = service_load_sysv_path(s, path);
998 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
999 /* Try SUSE style boot.* init scripts */
1001 path = join(*p, "/boot.", name, NULL);
1005 /* Drop .service suffix */
1006 path[strlen(path)-8] = 0;
1007 r = service_load_sysv_path(s, path);
1012 #ifdef TARGET_FRUGALWARE
1013 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1014 /* Try Frugalware style rc.* init scripts */
1016 path = join(*p, "/rc.", name, NULL);
1020 /* Drop .service suffix */
1021 path[strlen(path)-8] = 0;
1022 r = service_load_sysv_path(s, path);
1030 if ((UNIT(s)->load_state != UNIT_STUB))
1037 static int service_load_sysv(Service *s) {
1044 /* Load service data from SysV init scripts, preferably with
1045 * LSB headers ... */
1047 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1050 if ((t = UNIT(s)->id))
1051 if ((r = service_load_sysv_name(s, t)) < 0)
1054 if (UNIT(s)->load_state == UNIT_STUB)
1055 SET_FOREACH(t, UNIT(s)->names, i) {
1056 if (t == UNIT(s)->id)
1059 if ((r = service_load_sysv_name(s, t)) < 0)
1062 if (UNIT(s)->load_state != UNIT_STUB)
1070 static int fsck_fix_order(Service *s) {
1076 if (s->fsck_passno <= 0)
1079 /* For each pair of services where both have an fsck priority
1080 * we order things based on it. */
1082 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1091 if (UNIT(t)->load_state != UNIT_LOADED)
1094 if (t->fsck_passno <= 0)
1097 if (t->fsck_passno < s->fsck_passno)
1099 else if (t->fsck_passno > s->fsck_passno)
1104 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1111 static int service_verify(Service *s) {
1114 if (UNIT(s)->load_state != UNIT_LOADED)
1117 if (!s->exec_command[SERVICE_EXEC_START]) {
1118 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1122 if (s->type != SERVICE_ONESHOT &&
1123 s->exec_command[SERVICE_EXEC_START]->command_next) {
1124 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1128 if (s->type == SERVICE_ONESHOT &&
1129 s->exec_command[SERVICE_EXEC_RELOAD]) {
1130 log_error("%s has an ExecReload setting, which is not allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1134 if (s->type == SERVICE_DBUS && !s->bus_name) {
1135 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1139 if (s->bus_name && s->type != SERVICE_DBUS)
1140 log_warning("%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1142 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
1143 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1150 static int service_add_default_dependencies(Service *s) {
1155 /* Add a number of automatic dependencies useful for the
1156 * majority of services. */
1158 /* First, pull in base system */
1159 if (UNIT(s)->manager->running_as == MANAGER_SYSTEM) {
1161 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1164 } else if (UNIT(s)->manager->running_as == MANAGER_USER) {
1166 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1170 /* Second, activate normal shutdown */
1171 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1174 static void service_fix_output(Service *s) {
1177 /* If nothing has been explicitly configured, patch default
1178 * output in. If input is socket/tty we avoid this however,
1179 * since in that case we want output to default to the same
1180 * place as we read input from. */
1182 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1183 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1184 s->exec_context.std_input == EXEC_INPUT_NULL)
1185 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1187 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1188 s->exec_context.std_input == EXEC_INPUT_NULL)
1189 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1192 static int service_load(Unit *u) {
1194 Service *s = SERVICE(u);
1198 /* Load a .service file */
1199 if ((r = unit_load_fragment(u)) < 0)
1202 #ifdef HAVE_SYSV_COMPAT
1203 /* Load a classic init script as a fallback, if we couldn't find anything */
1204 if (u->load_state == UNIT_STUB)
1205 if ((r = service_load_sysv(s)) < 0)
1209 /* Still nothing found? Then let's give up */
1210 if (u->load_state == UNIT_STUB)
1213 /* We were able to load something, then let's add in the
1214 * dropin directories. */
1215 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1218 /* This is a new unit? Then let's add in some extras */
1219 if (u->load_state == UNIT_LOADED) {
1220 if (s->type == _SERVICE_TYPE_INVALID)
1221 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1223 service_fix_output(s);
1225 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1228 if ((r = unit_add_default_cgroups(u)) < 0)
1231 #ifdef HAVE_SYSV_COMPAT
1232 if ((r = sysv_fix_order(s)) < 0)
1236 if ((r = fsck_fix_order(s)) < 0)
1240 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1243 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1244 s->notify_access = NOTIFY_MAIN;
1246 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1247 s->notify_access = NOTIFY_MAIN;
1249 if (s->type == SERVICE_DBUS || s->bus_name)
1250 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1253 if (UNIT(s)->default_dependencies)
1254 if ((r = service_add_default_dependencies(s)) < 0)
1258 return service_verify(s);
1261 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1263 ServiceExecCommand c;
1264 Service *s = SERVICE(u);
1265 const char *prefix2;
1270 p2 = strappend(prefix, "\t");
1271 prefix2 = p2 ? p2 : prefix;
1274 "%sService State: %s\n"
1276 "%sReload Result: %s\n"
1277 "%sPermissionsStartOnly: %s\n"
1278 "%sRootDirectoryStartOnly: %s\n"
1279 "%sRemainAfterExit: %s\n"
1280 "%sGuessMainPID: %s\n"
1283 "%sNotifyAccess: %s\n",
1284 prefix, service_state_to_string(s->state),
1285 prefix, service_result_to_string(s->result),
1286 prefix, service_result_to_string(s->reload_result),
1287 prefix, yes_no(s->permissions_start_only),
1288 prefix, yes_no(s->root_directory_start_only),
1289 prefix, yes_no(s->remain_after_exit),
1290 prefix, yes_no(s->guess_main_pid),
1291 prefix, service_type_to_string(s->type),
1292 prefix, service_restart_to_string(s->restart),
1293 prefix, notify_access_to_string(s->notify_access));
1295 if (s->control_pid > 0)
1297 "%sControl PID: %lu\n",
1298 prefix, (unsigned long) s->control_pid);
1300 if (s->main_pid > 0)
1303 "%sMain PID Known: %s\n"
1304 "%sMain PID Alien: %s\n",
1305 prefix, (unsigned long) s->main_pid,
1306 prefix, yes_no(s->main_pid_known),
1307 prefix, yes_no(s->main_pid_alien));
1312 prefix, s->pid_file);
1317 "%sBus Name Good: %s\n",
1318 prefix, s->bus_name,
1319 prefix, yes_no(s->bus_name_good));
1321 exec_context_dump(&s->exec_context, f, prefix);
1323 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1325 if (!s->exec_command[c])
1328 fprintf(f, "%s-> %s:\n",
1329 prefix, service_exec_command_to_string(c));
1331 exec_command_dump_list(s->exec_command[c], f, prefix2);
1334 #ifdef HAVE_SYSV_COMPAT
1337 "%sSysV Init Script Path: %s\n"
1338 "%sSysV Init Script has LSB Header: %s\n"
1339 "%sSysVEnabled: %s\n",
1340 prefix, s->sysv_path,
1341 prefix, yes_no(s->sysv_has_lsb),
1342 prefix, yes_no(s->sysv_enabled));
1344 if (s->sysv_start_priority >= 0)
1346 "%sSysVStartPriority: %i\n",
1347 prefix, s->sysv_start_priority);
1349 if (s->sysv_runlevels)
1350 fprintf(f, "%sSysVRunLevels: %s\n",
1351 prefix, s->sysv_runlevels);
1354 if (s->fsck_passno > 0)
1356 "%sFsckPassNo: %i\n",
1357 prefix, s->fsck_passno);
1360 fprintf(f, "%sStatus Text: %s\n",
1361 prefix, s->status_text);
1366 static int service_load_pid_file(Service *s, bool may_warn) {
1376 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1378 log_info("PID file %s not readable (yet?) after %s.",
1379 s->pid_file, service_state_to_string(s->state));
1383 r = parse_pid(k, &pid);
1389 if (kill(pid, 0) < 0 && errno != EPERM) {
1391 log_info("PID %lu read from file %s does not exist.",
1392 (unsigned long) pid, s->pid_file);
1396 if (s->main_pid_known) {
1397 if (pid == s->main_pid)
1400 log_debug("Main PID changing: %lu -> %lu",
1401 (unsigned long) s->main_pid, (unsigned long) pid);
1402 service_unwatch_main_pid(s);
1403 s->main_pid_known = false;
1405 log_debug("Main PID loaded: %lu", (unsigned long) pid);
1407 if ((r = service_set_main_pid(s, pid)) < 0)
1410 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1411 /* FIXME: we need to do something here */
1417 static int service_search_main_pid(Service *s) {
1423 /* If we know it anyway, don't ever fallback to unreliable
1425 if (s->main_pid_known)
1428 if (!s->guess_main_pid)
1431 assert(s->main_pid <= 0);
1433 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1436 log_debug("Main PID guessed: %lu", (unsigned long) pid);
1437 if ((r = service_set_main_pid(s, pid)) < 0)
1440 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1441 /* FIXME: we need to do something here */
1447 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1453 /* Notifies all our sockets when we die */
1455 if (s->socket_fd >= 0)
1458 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1459 if (u->type == UNIT_SOCKET)
1460 socket_notify_service_dead(SOCKET(u), failed_permanent);
1465 static void service_set_state(Service *s, ServiceState state) {
1466 ServiceState old_state;
1469 old_state = s->state;
1472 service_unwatch_pid_file(s);
1474 if (state != SERVICE_START_PRE &&
1475 state != SERVICE_START &&
1476 state != SERVICE_START_POST &&
1477 state != SERVICE_RELOAD &&
1478 state != SERVICE_STOP &&
1479 state != SERVICE_STOP_SIGTERM &&
1480 state != SERVICE_STOP_SIGKILL &&
1481 state != SERVICE_STOP_POST &&
1482 state != SERVICE_FINAL_SIGTERM &&
1483 state != SERVICE_FINAL_SIGKILL &&
1484 state != SERVICE_AUTO_RESTART)
1485 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1487 if (state != SERVICE_START &&
1488 state != SERVICE_START_POST &&
1489 state != SERVICE_RUNNING &&
1490 state != SERVICE_RELOAD &&
1491 state != SERVICE_STOP &&
1492 state != SERVICE_STOP_SIGTERM &&
1493 state != SERVICE_STOP_SIGKILL) {
1494 service_unwatch_main_pid(s);
1495 s->main_command = NULL;
1498 if (state != SERVICE_START_PRE &&
1499 state != SERVICE_START &&
1500 state != SERVICE_START_POST &&
1501 state != SERVICE_RELOAD &&
1502 state != SERVICE_STOP &&
1503 state != SERVICE_STOP_SIGTERM &&
1504 state != SERVICE_STOP_SIGKILL &&
1505 state != SERVICE_STOP_POST &&
1506 state != SERVICE_FINAL_SIGTERM &&
1507 state != SERVICE_FINAL_SIGKILL) {
1508 service_unwatch_control_pid(s);
1509 s->control_command = NULL;
1510 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1513 if (state == SERVICE_DEAD ||
1514 state == SERVICE_STOP ||
1515 state == SERVICE_STOP_SIGTERM ||
1516 state == SERVICE_STOP_SIGKILL ||
1517 state == SERVICE_STOP_POST ||
1518 state == SERVICE_FINAL_SIGTERM ||
1519 state == SERVICE_FINAL_SIGKILL ||
1520 state == SERVICE_FAILED ||
1521 state == SERVICE_AUTO_RESTART)
1522 service_notify_sockets_dead(s, false);
1524 if (state != SERVICE_START_PRE &&
1525 state != SERVICE_START &&
1526 state != SERVICE_START_POST &&
1527 state != SERVICE_RUNNING &&
1528 state != SERVICE_RELOAD &&
1529 state != SERVICE_STOP &&
1530 state != SERVICE_STOP_SIGTERM &&
1531 state != SERVICE_STOP_SIGKILL &&
1532 state != SERVICE_STOP_POST &&
1533 state != SERVICE_FINAL_SIGTERM &&
1534 state != SERVICE_FINAL_SIGKILL &&
1535 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1536 service_close_socket_fd(s);
1537 service_connection_unref(s);
1540 if (state == SERVICE_STOP)
1541 service_stop_watchdog(s);
1543 /* For the inactive states unit_notify() will trim the cgroup,
1544 * but for exit we have to do that ourselves... */
1545 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1546 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1548 if (old_state != state)
1549 log_debug("%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
1551 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], s->reload_result == SERVICE_SUCCESS);
1552 s->reload_result = SERVICE_SUCCESS;
1555 static int service_coldplug(Unit *u) {
1556 Service *s = SERVICE(u);
1560 assert(s->state == SERVICE_DEAD);
1562 if (s->deserialized_state != s->state) {
1564 if (s->deserialized_state == SERVICE_START_PRE ||
1565 s->deserialized_state == SERVICE_START ||
1566 s->deserialized_state == SERVICE_START_POST ||
1567 s->deserialized_state == SERVICE_RELOAD ||
1568 s->deserialized_state == SERVICE_STOP ||
1569 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1570 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1571 s->deserialized_state == SERVICE_STOP_POST ||
1572 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1573 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1574 s->deserialized_state == SERVICE_AUTO_RESTART) {
1576 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1579 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1581 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1586 if ((s->deserialized_state == SERVICE_START &&
1587 (s->type == SERVICE_FORKING ||
1588 s->type == SERVICE_DBUS ||
1589 s->type == SERVICE_ONESHOT ||
1590 s->type == SERVICE_NOTIFY)) ||
1591 s->deserialized_state == SERVICE_START_POST ||
1592 s->deserialized_state == SERVICE_RUNNING ||
1593 s->deserialized_state == SERVICE_RELOAD ||
1594 s->deserialized_state == SERVICE_STOP ||
1595 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1596 s->deserialized_state == SERVICE_STOP_SIGKILL)
1597 if (s->main_pid > 0)
1598 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1601 if (s->deserialized_state == SERVICE_START_PRE ||
1602 s->deserialized_state == SERVICE_START ||
1603 s->deserialized_state == SERVICE_START_POST ||
1604 s->deserialized_state == SERVICE_RELOAD ||
1605 s->deserialized_state == SERVICE_STOP ||
1606 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1607 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1608 s->deserialized_state == SERVICE_STOP_POST ||
1609 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1610 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1611 if (s->control_pid > 0)
1612 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1615 if (s->deserialized_state == SERVICE_START_POST ||
1616 s->deserialized_state == SERVICE_RUNNING)
1617 service_handle_watchdog(s);
1619 service_set_state(s, s->deserialized_state);
1624 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1628 unsigned rn_fds = 0;
1635 if (s->socket_fd >= 0)
1638 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1643 if (u->type != UNIT_SOCKET)
1648 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1660 if (!(t = new(int, rn_fds+cn_fds))) {
1666 memcpy(t, rfds, rn_fds * sizeof(int));
1667 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1672 rn_fds = rn_fds+cn_fds;
1687 static int service_spawn(
1692 bool apply_permissions,
1694 bool apply_tty_stdin,
1695 bool set_notify_socket,
1701 int *fds = NULL, *fdsbuf = NULL;
1702 unsigned n_fds = 0, n_env = 0;
1703 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1710 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1711 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1712 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1714 if (s->socket_fd >= 0) {
1715 fds = &s->socket_fd;
1718 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1725 if (timeout && s->timeout_usec) {
1726 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1729 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1731 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1736 if (!(our_env = new0(char*, 4))) {
1741 if (set_notify_socket)
1742 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1747 if (s->main_pid > 0)
1748 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1753 if (s->watchdog_usec > 0)
1754 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1759 if (!(final_env = strv_env_merge(2,
1760 UNIT(s)->manager->environment,
1775 UNIT(s)->manager->confirm_spawn,
1776 UNIT(s)->cgroup_bondings,
1777 UNIT(s)->cgroup_attributes,
1778 is_control ? "control" : NULL,
1779 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1786 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1787 /* FIXME: we need to do something here */
1793 strv_free(final_env);
1803 strv_free(final_env);
1806 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1811 static int main_pid_good(Service *s) {
1814 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1817 /* If we know the pid file, then lets just check if it is
1819 if (s->main_pid_known) {
1821 /* If it's an alien child let's check if it is still
1823 if (s->main_pid_alien)
1824 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1826 /* .. otherwise assume we'll get a SIGCHLD for it,
1827 * which we really should wait for to collect exit
1828 * status and code */
1829 return s->main_pid > 0;
1832 /* We don't know the pid */
1836 static int control_pid_good(Service *s) {
1839 return s->control_pid > 0;
1842 static int cgroup_good(Service *s) {
1847 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1853 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1857 if (f != SERVICE_SUCCESS)
1860 if (allow_restart &&
1861 !s->forbid_restart &&
1862 (s->restart == SERVICE_RESTART_ALWAYS ||
1863 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1864 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1865 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1866 s->result == SERVICE_FAILURE_CORE_DUMP)))) {
1868 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
1872 service_set_state(s, SERVICE_AUTO_RESTART);
1874 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1876 s->forbid_restart = false;
1881 log_warning("%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1882 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1885 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1887 static void service_enter_stop_post(Service *s, ServiceResult f) {
1891 if (f != SERVICE_SUCCESS)
1894 service_unwatch_control_pid(s);
1896 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1897 s->control_command_id = SERVICE_EXEC_STOP_POST;
1899 r = service_spawn(s,
1903 !s->permissions_start_only,
1904 !s->root_directory_start_only,
1913 service_set_state(s, SERVICE_STOP_POST);
1915 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1920 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
1921 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1924 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1926 Set *pid_set = NULL;
1927 bool wait_for_exit = false;
1931 if (f != SERVICE_SUCCESS)
1934 if (s->exec_context.kill_mode != KILL_NONE) {
1935 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1937 if (s->main_pid > 0) {
1938 if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
1939 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1941 wait_for_exit = !s->main_pid_alien;
1944 if (s->control_pid > 0) {
1945 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1946 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1948 wait_for_exit = true;
1951 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1953 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1958 /* Exclude the main/control pids from being killed via the cgroup */
1959 if (s->main_pid > 0)
1960 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1963 if (s->control_pid > 0)
1964 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1967 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, pid_set, NULL);
1969 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1970 log_warning("Failed to kill control group: %s", strerror(-r));
1972 wait_for_exit = true;
1979 if (wait_for_exit) {
1980 if (s->timeout_usec > 0)
1981 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1984 service_set_state(s, state);
1985 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1986 service_enter_stop_post(s, SERVICE_SUCCESS);
1988 service_enter_dead(s, SERVICE_SUCCESS, true);
1993 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1995 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1996 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1998 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2004 static void service_enter_stop(Service *s, ServiceResult f) {
2009 if (f != SERVICE_SUCCESS)
2012 service_unwatch_control_pid(s);
2014 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2015 s->control_command_id = SERVICE_EXEC_STOP;
2017 r = service_spawn(s,
2021 !s->permissions_start_only,
2022 !s->root_directory_start_only,
2030 service_set_state(s, SERVICE_STOP);
2032 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2037 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2038 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2041 static void service_enter_running(Service *s, ServiceResult f) {
2042 int main_pid_ok, cgroup_ok;
2045 if (f != SERVICE_SUCCESS)
2048 main_pid_ok = main_pid_good(s);
2049 cgroup_ok = cgroup_good(s);
2051 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2052 (s->bus_name_good || s->type != SERVICE_DBUS))
2053 service_set_state(s, SERVICE_RUNNING);
2054 else if (s->remain_after_exit)
2055 service_set_state(s, SERVICE_EXITED);
2057 service_enter_stop(s, SERVICE_SUCCESS);
2060 static void service_enter_start_post(Service *s) {
2064 service_unwatch_control_pid(s);
2066 if (s->watchdog_usec > 0)
2067 service_reset_watchdog(s);
2069 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2070 s->control_command_id = SERVICE_EXEC_START_POST;
2072 r = service_spawn(s,
2076 !s->permissions_start_only,
2077 !s->root_directory_start_only,
2085 service_set_state(s, SERVICE_START_POST);
2087 service_enter_running(s, SERVICE_SUCCESS);
2092 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2093 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2096 static void service_enter_start(Service *s) {
2103 assert(s->exec_command[SERVICE_EXEC_START]);
2104 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2106 if (s->type == SERVICE_FORKING)
2107 service_unwatch_control_pid(s);
2109 service_unwatch_main_pid(s);
2111 /* We want to ensure that nobody leaks processes from
2112 * START_PRE here, so let's go on a killing spree, People
2113 * should not spawn long running processes from START_PRE. */
2114 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, NULL, "control");
2116 if (s->type == SERVICE_FORKING) {
2117 s->control_command_id = SERVICE_EXEC_START;
2118 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2120 s->main_command = NULL;
2122 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2123 s->control_command = NULL;
2125 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2128 r = service_spawn(s,
2130 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
2135 s->notify_access != NOTIFY_NONE,
2141 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2142 /* For simple services we immediately start
2143 * the START_POST binaries. */
2145 service_set_main_pid(s, pid);
2146 service_enter_start_post(s);
2148 } else if (s->type == SERVICE_FORKING) {
2150 /* For forking services we wait until the start
2151 * process exited. */
2153 s->control_pid = pid;
2154 service_set_state(s, SERVICE_START);
2156 } else if (s->type == SERVICE_ONESHOT ||
2157 s->type == SERVICE_DBUS ||
2158 s->type == SERVICE_NOTIFY) {
2160 /* For oneshot services we wait until the start
2161 * process exited, too, but it is our main process. */
2163 /* For D-Bus services we know the main pid right away,
2164 * but wait for the bus name to appear on the
2165 * bus. Notify services are similar. */
2167 service_set_main_pid(s, pid);
2168 service_set_state(s, SERVICE_START);
2170 assert_not_reached("Unknown service type");
2175 log_warning("%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2176 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2179 static void service_enter_start_pre(Service *s) {
2184 service_unwatch_control_pid(s);
2186 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2188 /* Before we start anything, let's clear up what might
2189 * be left from previous runs. */
2190 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, NULL, "control");
2192 s->control_command_id = SERVICE_EXEC_START_PRE;
2194 r = service_spawn(s,
2198 !s->permissions_start_only,
2199 !s->root_directory_start_only,
2207 service_set_state(s, SERVICE_START_PRE);
2209 service_enter_start(s);
2214 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2215 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2218 static void service_enter_restart(Service *s) {
2223 dbus_error_init(&error);
2226 log_info("Job pending for unit, delaying automatic restart.");
2228 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
2232 /* Any units that are bound to this service must also be
2233 * restarted. We use JOB_RESTART (instead of the more obvious
2234 * JOB_START) here so that those dependency jobs will be added
2236 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2240 log_debug("%s scheduled restart job.", UNIT(s)->id);
2244 log_warning("%s failed to schedule restart job: %s", UNIT(s)->id, bus_error(&error, -r));
2245 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2247 dbus_error_free(&error);
2250 static void service_enter_reload(Service *s) {
2255 service_unwatch_control_pid(s);
2257 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2258 s->control_command_id = SERVICE_EXEC_RELOAD;
2260 r = service_spawn(s,
2264 !s->permissions_start_only,
2265 !s->root_directory_start_only,
2273 service_set_state(s, SERVICE_RELOAD);
2275 service_enter_running(s, SERVICE_SUCCESS);
2280 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
2281 s->reload_result = SERVICE_FAILURE_RESOURCES;
2282 service_enter_running(s, SERVICE_SUCCESS);
2285 static void service_run_next_control(Service *s) {
2289 assert(s->control_command);
2290 assert(s->control_command->command_next);
2292 assert(s->control_command_id != SERVICE_EXEC_START);
2294 s->control_command = s->control_command->command_next;
2295 service_unwatch_control_pid(s);
2297 r = service_spawn(s,
2301 !s->permissions_start_only,
2302 !s->root_directory_start_only,
2303 s->control_command_id == SERVICE_EXEC_START_PRE ||
2304 s->control_command_id == SERVICE_EXEC_STOP_POST,
2314 log_warning("%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
2316 if (s->state == SERVICE_START_PRE)
2317 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2318 else if (s->state == SERVICE_STOP)
2319 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2320 else if (s->state == SERVICE_STOP_POST)
2321 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2322 else if (s->state == SERVICE_RELOAD) {
2323 s->reload_result = SERVICE_FAILURE_RESOURCES;
2324 service_enter_running(s, SERVICE_SUCCESS);
2326 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2329 static void service_run_next_main(Service *s) {
2334 assert(s->main_command);
2335 assert(s->main_command->command_next);
2336 assert(s->type == SERVICE_ONESHOT);
2338 s->main_command = s->main_command->command_next;
2339 service_unwatch_main_pid(s);
2341 r = service_spawn(s,
2348 s->notify_access != NOTIFY_NONE,
2354 service_set_main_pid(s, pid);
2359 log_warning("%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2360 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2363 static int service_start_limit_test(Service *s) {
2366 if (ratelimit_test(&s->start_limit))
2369 switch (s->start_limit_action) {
2371 case SERVICE_START_LIMIT_NONE:
2372 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s)->id);
2375 case SERVICE_START_LIMIT_REBOOT: {
2379 dbus_error_init(&error);
2381 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2383 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2385 log_error("Failed to reboot: %s.", bus_error(&error, r));
2386 dbus_error_free(&error);
2392 case SERVICE_START_LIMIT_REBOOT_FORCE:
2393 log_warning("%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2394 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2397 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2398 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2399 reboot(RB_AUTOBOOT);
2403 log_error("start limit action=%i", s->start_limit_action);
2404 assert_not_reached("Unknown StartLimitAction.");
2410 static int service_start(Unit *u) {
2411 Service *s = SERVICE(u);
2416 /* We cannot fulfill this request right now, try again later
2418 if (s->state == SERVICE_STOP ||
2419 s->state == SERVICE_STOP_SIGTERM ||
2420 s->state == SERVICE_STOP_SIGKILL ||
2421 s->state == SERVICE_STOP_POST ||
2422 s->state == SERVICE_FINAL_SIGTERM ||
2423 s->state == SERVICE_FINAL_SIGKILL)
2426 /* Already on it! */
2427 if (s->state == SERVICE_START_PRE ||
2428 s->state == SERVICE_START ||
2429 s->state == SERVICE_START_POST)
2432 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART);
2434 /* Make sure we don't enter a busy loop of some kind. */
2435 r = service_start_limit_test(s);
2437 service_notify_sockets_dead(s, true);
2441 s->result = SERVICE_SUCCESS;
2442 s->reload_result = SERVICE_SUCCESS;
2443 s->main_pid_known = false;
2444 s->main_pid_alien = false;
2445 s->forbid_restart = false;
2447 service_enter_start_pre(s);
2451 static int service_stop(Unit *u) {
2452 Service *s = SERVICE(u);
2456 /* This is a user request, so don't do restarts on this
2458 s->forbid_restart = true;
2461 if (s->state == SERVICE_STOP ||
2462 s->state == SERVICE_STOP_SIGTERM ||
2463 s->state == SERVICE_STOP_SIGKILL ||
2464 s->state == SERVICE_STOP_POST ||
2465 s->state == SERVICE_FINAL_SIGTERM ||
2466 s->state == SERVICE_FINAL_SIGKILL)
2469 /* Don't allow a restart */
2470 if (s->state == SERVICE_AUTO_RESTART) {
2471 service_set_state(s, SERVICE_DEAD);
2475 /* If there's already something running we go directly into
2477 if (s->state == SERVICE_START_PRE ||
2478 s->state == SERVICE_START ||
2479 s->state == SERVICE_START_POST ||
2480 s->state == SERVICE_RELOAD) {
2481 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2485 assert(s->state == SERVICE_RUNNING ||
2486 s->state == SERVICE_EXITED);
2488 service_enter_stop(s, SERVICE_SUCCESS);
2492 static int service_reload(Unit *u) {
2493 Service *s = SERVICE(u);
2497 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2499 service_enter_reload(s);
2503 static bool service_can_reload(Unit *u) {
2504 Service *s = SERVICE(u);
2508 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2511 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2512 Service *s = SERVICE(u);
2518 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2519 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2520 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2522 if (s->control_pid > 0)
2523 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2525 if (s->main_pid_known && s->main_pid > 0)
2526 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2528 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2531 unit_serialize_item(u, f, "status-text", s->status_text);
2533 /* FIXME: There's a minor uncleanliness here: if there are
2534 * multiple commands attached here, we will start from the
2535 * first one again */
2536 if (s->control_command_id >= 0)
2537 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2539 if (s->socket_fd >= 0) {
2542 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2545 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2548 if (s->main_exec_status.pid > 0) {
2549 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2550 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2551 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2553 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2554 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2555 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2558 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2559 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2564 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2565 Service *s = SERVICE(u);
2572 if (streq(key, "state")) {
2575 if ((state = service_state_from_string(value)) < 0)
2576 log_debug("Failed to parse state value %s", value);
2578 s->deserialized_state = state;
2579 } else if (streq(key, "result")) {
2582 f = service_result_from_string(value);
2584 log_debug("Failed to parse result value %s", value);
2585 else if (f != SERVICE_SUCCESS)
2588 } else if (streq(key, "reload-result")) {
2591 f = service_result_from_string(value);
2593 log_debug("Failed to parse reload result value %s", value);
2594 else if (f != SERVICE_SUCCESS)
2595 s->reload_result = f;
2597 } else if (streq(key, "control-pid")) {
2600 if (parse_pid(value, &pid) < 0)
2601 log_debug("Failed to parse control-pid value %s", value);
2603 s->control_pid = pid;
2604 } else if (streq(key, "main-pid")) {
2607 if (parse_pid(value, &pid) < 0)
2608 log_debug("Failed to parse main-pid value %s", value);
2610 service_set_main_pid(s, (pid_t) pid);
2611 } else if (streq(key, "main-pid-known")) {
2614 if ((b = parse_boolean(value)) < 0)
2615 log_debug("Failed to parse main-pid-known value %s", value);
2617 s->main_pid_known = b;
2618 } else if (streq(key, "status-text")) {
2621 if ((t = strdup(value))) {
2622 free(s->status_text);
2626 } else if (streq(key, "control-command")) {
2627 ServiceExecCommand id;
2629 if ((id = service_exec_command_from_string(value)) < 0)
2630 log_debug("Failed to parse exec-command value %s", value);
2632 s->control_command_id = id;
2633 s->control_command = s->exec_command[id];
2635 } else if (streq(key, "socket-fd")) {
2638 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2639 log_debug("Failed to parse socket-fd value %s", value);
2642 if (s->socket_fd >= 0)
2643 close_nointr_nofail(s->socket_fd);
2644 s->socket_fd = fdset_remove(fds, fd);
2646 } else if (streq(key, "main-exec-status-pid")) {
2649 if (parse_pid(value, &pid) < 0)
2650 log_debug("Failed to parse main-exec-status-pid value %s", value);
2652 s->main_exec_status.pid = pid;
2653 } else if (streq(key, "main-exec-status-code")) {
2656 if (safe_atoi(value, &i) < 0)
2657 log_debug("Failed to parse main-exec-status-code value %s", value);
2659 s->main_exec_status.code = i;
2660 } else if (streq(key, "main-exec-status-status")) {
2663 if (safe_atoi(value, &i) < 0)
2664 log_debug("Failed to parse main-exec-status-status value %s", value);
2666 s->main_exec_status.status = i;
2667 } else if (streq(key, "main-exec-status-start"))
2668 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2669 else if (streq(key, "main-exec-status-exit"))
2670 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2671 else if (streq(key, "watchdog-timestamp"))
2672 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2674 log_debug("Unknown serialization key '%s'", key);
2679 static UnitActiveState service_active_state(Unit *u) {
2682 return state_translation_table[SERVICE(u)->state];
2685 static const char *service_sub_state_to_string(Unit *u) {
2688 return service_state_to_string(SERVICE(u)->state);
2691 static bool service_check_gc(Unit *u) {
2692 Service *s = SERVICE(u);
2696 /* Never clean up services that still have a process around,
2697 * even if the service is formally dead. */
2698 if (cgroup_good(s) > 0 ||
2699 main_pid_good(s) > 0 ||
2700 control_pid_good(s) > 0)
2703 #ifdef HAVE_SYSV_COMPAT
2711 static bool service_check_snapshot(Unit *u) {
2712 Service *s = SERVICE(u);
2716 return !s->got_socket_fd;
2719 static int service_retry_pid_file(Service *s) {
2722 assert(s->pid_file);
2723 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2725 r = service_load_pid_file(s, false);
2729 service_unwatch_pid_file(s);
2731 service_enter_running(s, SERVICE_SUCCESS);
2735 static int service_watch_pid_file(Service *s) {
2738 log_debug("Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
2739 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2743 /* the pidfile might have appeared just before we set the watch */
2744 service_retry_pid_file(s);
2748 log_error("Failed to set a watch for %s's PID file %s: %s",
2749 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2750 service_unwatch_pid_file(s);
2754 static int service_demand_pid_file(Service *s) {
2757 assert(s->pid_file);
2758 assert(!s->pid_file_pathspec);
2760 ps = new0(PathSpec, 1);
2764 ps->path = strdup(s->pid_file);
2770 path_kill_slashes(ps->path);
2772 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2773 * keep their PID file open all the time. */
2774 ps->type = PATH_MODIFIED;
2775 ps->inotify_fd = -1;
2777 s->pid_file_pathspec = ps;
2779 return service_watch_pid_file(s);
2782 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2783 Service *s = SERVICE(u);
2787 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2788 assert(s->pid_file_pathspec);
2789 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2791 log_debug("inotify event for %s", u->id);
2793 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2796 if (service_retry_pid_file(s) == 0)
2799 if (service_watch_pid_file(s) < 0)
2804 service_unwatch_pid_file(s);
2805 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2808 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2809 Service *s = SERVICE(u);
2815 if (UNIT(s)->fragment_path ? is_clean_exit(code, status) : is_clean_exit_lsb(code, status))
2816 f = SERVICE_SUCCESS;
2817 else if (code == CLD_EXITED)
2818 f = SERVICE_FAILURE_EXIT_CODE;
2819 else if (code == CLD_KILLED)
2820 f = SERVICE_FAILURE_SIGNAL;
2821 else if (code == CLD_DUMPED)
2822 f = SERVICE_FAILURE_CORE_DUMP;
2824 assert_not_reached("Unknown code");
2826 if (s->main_pid == pid) {
2827 /* Forking services may occasionally move to a new PID.
2828 * As long as they update the PID file before exiting the old
2829 * PID, they're fine. */
2830 if (service_load_pid_file(s, false) == 0)
2834 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2836 /* If this is not a forking service than the main
2837 * process got started and hence we copy the exit
2838 * status so that it is recorded both as main and as
2839 * control process exit status */
2840 if (s->main_command) {
2841 s->main_command->exec_status = s->main_exec_status;
2843 if (s->main_command->ignore)
2844 f = SERVICE_SUCCESS;
2847 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2848 "%s: main process exited, code=%s, status=%i", u->id, sigchld_code_to_string(code), status);
2850 if (f != SERVICE_SUCCESS)
2853 if (s->main_command &&
2854 s->main_command->command_next &&
2855 f == SERVICE_SUCCESS) {
2857 /* There is another command to *
2858 * execute, so let's do that. */
2860 log_debug("%s running next main command for state %s", u->id, service_state_to_string(s->state));
2861 service_run_next_main(s);
2865 /* The service exited, so the service is officially
2867 s->main_command = NULL;
2871 case SERVICE_START_POST:
2872 case SERVICE_RELOAD:
2874 /* Need to wait until the operation is
2879 if (s->type == SERVICE_ONESHOT) {
2880 /* This was our main goal, so let's go on */
2881 if (f == SERVICE_SUCCESS)
2882 service_enter_start_post(s);
2884 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2887 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2892 case SERVICE_RUNNING:
2893 service_enter_running(s, f);
2896 case SERVICE_STOP_SIGTERM:
2897 case SERVICE_STOP_SIGKILL:
2899 if (!control_pid_good(s))
2900 service_enter_stop_post(s, f);
2902 /* If there is still a control process, wait for that first */
2906 assert_not_reached("Uh, main process died at wrong time.");
2910 } else if (s->control_pid == pid) {
2914 if (s->control_command) {
2915 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2917 if (s->control_command->ignore)
2918 f = SERVICE_SUCCESS;
2921 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2922 "%s: control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
2924 if (f != SERVICE_SUCCESS)
2927 if (s->control_command &&
2928 s->control_command->command_next &&
2929 f == SERVICE_SUCCESS) {
2931 /* There is another command to *
2932 * execute, so let's do that. */
2934 log_debug("%s running next control command for state %s", u->id, service_state_to_string(s->state));
2935 service_run_next_control(s);
2938 /* No further commands for this step, so let's
2939 * figure out what to do next */
2941 s->control_command = NULL;
2942 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2944 log_debug("%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
2948 case SERVICE_START_PRE:
2949 if (f == SERVICE_SUCCESS)
2950 service_enter_start(s);
2952 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2956 assert(s->type == SERVICE_FORKING);
2958 if (f != SERVICE_SUCCESS) {
2959 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2964 bool has_start_post;
2967 /* Let's try to load the pid file here if we can.
2968 * The PID file might actually be created by a START_POST
2969 * script. In that case don't worry if the loading fails. */
2971 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2972 r = service_load_pid_file(s, !has_start_post);
2973 if (!has_start_post && r < 0) {
2974 r = service_demand_pid_file(s);
2975 if (r < 0 || !cgroup_good(s))
2976 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2980 service_search_main_pid(s);
2982 service_enter_start_post(s);
2985 case SERVICE_START_POST:
2986 if (f != SERVICE_SUCCESS) {
2987 service_enter_stop(s, f);
2994 r = service_load_pid_file(s, true);
2996 r = service_demand_pid_file(s);
2997 if (r < 0 || !cgroup_good(s))
2998 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3002 service_search_main_pid(s);
3004 service_enter_running(s, SERVICE_SUCCESS);
3007 case SERVICE_RELOAD:
3008 if (f == SERVICE_SUCCESS) {
3009 service_load_pid_file(s, true);
3010 service_search_main_pid(s);
3013 s->reload_result = f;
3014 service_enter_running(s, SERVICE_SUCCESS);
3018 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3021 case SERVICE_STOP_SIGTERM:
3022 case SERVICE_STOP_SIGKILL:
3023 if (main_pid_good(s) <= 0)
3024 service_enter_stop_post(s, f);
3026 /* If there is still a service
3027 * process around, wait until
3028 * that one quit, too */
3031 case SERVICE_STOP_POST:
3032 case SERVICE_FINAL_SIGTERM:
3033 case SERVICE_FINAL_SIGKILL:
3034 service_enter_dead(s, f, true);
3038 assert_not_reached("Uh, control process died at wrong time.");
3043 /* Notify clients about changed exit status */
3044 unit_add_to_dbus_queue(u);
3047 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3048 Service *s = SERVICE(u);
3051 assert(elapsed == 1);
3053 if (w == &s->watchdog_watch) {
3054 service_handle_watchdog(s);
3058 assert(w == &s->timer_watch);
3062 case SERVICE_START_PRE:
3064 log_warning("%s operation timed out. Terminating.", u->id);
3065 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3068 case SERVICE_START_POST:
3069 log_warning("%s operation timed out. Stopping.", u->id);
3070 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3073 case SERVICE_RELOAD:
3074 log_warning("%s operation timed out. Stopping.", u->id);
3075 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3076 service_enter_running(s, SERVICE_SUCCESS);
3080 log_warning("%s stopping timed out. Terminating.", u->id);
3081 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3084 case SERVICE_STOP_SIGTERM:
3085 if (s->exec_context.send_sigkill) {
3086 log_warning("%s stopping timed out. Killing.", u->id);
3087 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3089 log_warning("%s stopping timed out. Skipping SIGKILL.", u->id);
3090 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3095 case SERVICE_STOP_SIGKILL:
3096 /* Uh, we sent a SIGKILL and it is still not gone?
3097 * Must be something we cannot kill, so let's just be
3098 * weirded out and continue */
3100 log_warning("%s still around after SIGKILL. Ignoring.", u->id);
3101 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3104 case SERVICE_STOP_POST:
3105 log_warning("%s stopping timed out (2). Terminating.", u->id);
3106 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3109 case SERVICE_FINAL_SIGTERM:
3110 if (s->exec_context.send_sigkill) {
3111 log_warning("%s stopping timed out (2). Killing.", u->id);
3112 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3114 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->id);
3115 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3120 case SERVICE_FINAL_SIGKILL:
3121 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->id);
3122 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3125 case SERVICE_AUTO_RESTART:
3126 log_info("%s holdoff time over, scheduling restart.", u->id);
3127 service_enter_restart(s);
3131 assert_not_reached("Timeout at wrong time.");
3135 static void service_cgroup_notify_event(Unit *u) {
3136 Service *s = SERVICE(u);
3140 log_debug("%s: cgroup is empty", u->id);
3144 /* Waiting for SIGCHLD is usually more interesting,
3145 * because it includes return codes/signals. Which is
3146 * why we ignore the cgroup events for most cases,
3147 * except when we don't know pid which to expect the
3151 case SERVICE_START_POST:
3152 /* If we were hoping for the daemon to write its PID file,
3153 * we can give up now. */
3154 if (s->pid_file_pathspec) {
3155 log_warning("%s never wrote its PID file. Failing.", UNIT(s)->id);
3156 service_unwatch_pid_file(s);
3157 if (s->state == SERVICE_START)
3158 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3160 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3164 case SERVICE_RUNNING:
3165 /* service_enter_running() will figure out what to do */
3166 service_enter_running(s, SERVICE_SUCCESS);
3169 case SERVICE_STOP_SIGTERM:
3170 case SERVICE_STOP_SIGKILL:
3172 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3173 service_enter_stop_post(s, SERVICE_SUCCESS);
3177 case SERVICE_FINAL_SIGTERM:
3178 case SERVICE_FINAL_SIGKILL:
3179 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3180 service_enter_dead(s, SERVICE_SUCCESS, SERVICE_SUCCESS);
3189 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3190 Service *s = SERVICE(u);
3195 if (s->notify_access == NOTIFY_NONE) {
3196 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3197 u->id, (unsigned long) pid);
3201 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3202 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3203 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3207 log_debug("%s: Got message", u->id);
3209 /* Interpret MAINPID= */
3210 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3211 (s->state == SERVICE_START ||
3212 s->state == SERVICE_START_POST ||
3213 s->state == SERVICE_RUNNING ||
3214 s->state == SERVICE_RELOAD)) {
3216 if (parse_pid(e + 8, &pid) < 0)
3217 log_warning("Failed to parse notification message %s", e);
3219 log_debug("%s: got %s", u->id, e);
3220 service_set_main_pid(s, pid);
3224 /* Interpret READY= */
3225 if (s->type == SERVICE_NOTIFY &&
3226 s->state == SERVICE_START &&
3227 strv_find(tags, "READY=1")) {
3228 log_debug("%s: got READY=1", u->id);
3230 service_enter_start_post(s);
3233 /* Interpret STATUS= */
3234 e = strv_find_prefix(tags, "STATUS=");
3240 if (!utf8_is_valid(e+7)) {
3241 log_warning("Status message in notification is not UTF-8 clean.");
3247 log_error("Failed to allocate string.");
3251 log_debug("%s: got %s", u->id, e);
3253 free(s->status_text);
3256 free(s->status_text);
3257 s->status_text = NULL;
3261 if (strv_find(tags, "WATCHDOG=1")) {
3262 log_debug("%s: got WATCHDOG=1", u->id);
3263 service_reset_watchdog(s);
3266 /* Notify clients about changed status or main pid */
3267 unit_add_to_dbus_queue(u);
3270 #ifdef HAVE_SYSV_COMPAT
3273 static void sysv_facility_in_insserv_conf(Manager *mgr) {
3277 if (!(f = fopen("/etc/insserv.conf", "re"))) {
3278 r = errno == ENOENT ? 0 : -errno;
3283 char l[LINE_MAX], *t;
3284 char **parsed = NULL;
3286 if (!fgets(l, sizeof(l), f)) {
3291 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r));
3296 if (*t != '$' && *t != '<')
3299 parsed = strv_split(t,WHITESPACE);
3300 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3301 if (parsed && !startswith_no_case (parsed[0], "<interactive>")) {
3304 if (sysv_translate_facility(parsed[0], NULL, &facility) < 0)
3306 if ((u = manager_get_unit(mgr, facility)) && (u->type == UNIT_TARGET)) {
3308 char *dep = NULL, *name, **j;
3310 STRV_FOREACH (j, parsed+1) {
3319 if (sysv_translate_facility(name, NULL, &dep) < 0)
3322 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, e, dep, NULL, true);
3337 static int service_enumerate(Manager *m) {
3341 char *path = NULL, *fpath = NULL, *name = NULL;
3342 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3349 if (m->running_as != MANAGER_SYSTEM)
3352 zero(runlevel_services);
3354 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3355 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3359 path = join(*p, "/", rcnd_table[i].path, NULL);
3368 if (!(d = opendir(path))) {
3369 if (errno != ENOENT)
3370 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3375 while ((de = readdir(d))) {
3378 if (ignore_file(de->d_name))
3381 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3384 if (strlen(de->d_name) < 4)
3387 a = undecchar(de->d_name[1]);
3388 b = undecchar(de->d_name[2]);
3394 fpath = join(path, "/", de->d_name, NULL);
3400 if (access(fpath, X_OK) < 0) {
3402 if (errno != ENOENT)
3403 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3409 if (!(name = sysv_translate_name(de->d_name + 3))) {
3414 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
3415 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3419 if (de->d_name[0] == 'S') {
3421 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
3422 SERVICE(service)->sysv_start_priority_from_rcnd =
3423 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3425 SERVICE(service)->sysv_enabled = true;
3428 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3431 if ((r = set_put(runlevel_services[i], service)) < 0)
3434 } else if (de->d_name[0] == 'K' &&
3435 (rcnd_table[i].type == RUNLEVEL_DOWN ||
3436 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
3438 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3441 if ((r = set_put(shutdown_services, service)) < 0)
3447 /* Now we loaded all stubs and are aware of the lowest
3448 start-up priority for all services, not let's actually load
3449 the services, this will also tell us which services are
3450 actually native now */
3451 manager_dispatch_load_queue(m);
3453 /* If this is a native service, rely on native ways to pull in
3454 * a service, don't pull it in via sysv rcN.d links. */
3455 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3456 SET_FOREACH(service, runlevel_services[i], j) {
3457 service = unit_follow_merge(service);
3459 if (service->fragment_path)
3462 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3466 /* We honour K links only for halt/reboot. For the normal
3467 * runlevels we assume the stop jobs will be implicitly added
3468 * by the core logic. Also, we don't really distinguish here
3469 * between the runlevels 0 and 6 and just add them to the
3470 * special shutdown target. On SUSE the boot.d/ runlevel is
3471 * also used for shutdown, so we add links for that too to the
3472 * shutdown target.*/
3473 SET_FOREACH(service, shutdown_services, j) {
3474 service = unit_follow_merge(service);
3476 if (service->fragment_path)
3479 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3486 sysv_facility_in_insserv_conf (m);
3494 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3495 set_free(runlevel_services[i]);
3496 set_free(shutdown_services);
3505 static void service_bus_name_owner_change(
3508 const char *old_owner,
3509 const char *new_owner) {
3511 Service *s = SERVICE(u);
3516 assert(streq(s->bus_name, name));
3517 assert(old_owner || new_owner);
3519 if (old_owner && new_owner)
3520 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
3522 log_debug("%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
3524 log_debug("%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
3526 s->bus_name_good = !!new_owner;
3528 if (s->type == SERVICE_DBUS) {
3530 /* service_enter_running() will figure out what to
3532 if (s->state == SERVICE_RUNNING)
3533 service_enter_running(s, SERVICE_SUCCESS);
3534 else if (s->state == SERVICE_START && new_owner)
3535 service_enter_start_post(s);
3537 } else if (new_owner &&
3539 (s->state == SERVICE_START ||
3540 s->state == SERVICE_START_POST ||
3541 s->state == SERVICE_RUNNING ||
3542 s->state == SERVICE_RELOAD)) {
3544 /* Try to acquire PID from bus service */
3545 log_debug("Trying to acquire PID from D-Bus name...");
3547 bus_query_pid(u->manager, name);
3551 static void service_bus_query_pid_done(
3556 Service *s = SERVICE(u);
3561 log_debug("%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3563 if (s->main_pid <= 0 &&
3564 (s->state == SERVICE_START ||
3565 s->state == SERVICE_START_POST ||
3566 s->state == SERVICE_RUNNING ||
3567 s->state == SERVICE_RELOAD))
3568 service_set_main_pid(s, pid);
3571 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3576 /* This is called by the socket code when instantiating a new
3577 * service for a stream socket and the socket needs to be
3580 if (UNIT(s)->load_state != UNIT_LOADED)
3583 if (s->socket_fd >= 0)
3586 if (s->state != SERVICE_DEAD)
3590 s->got_socket_fd = true;
3592 unit_ref_set(&s->accept_socket, UNIT(sock));
3594 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3597 static void service_reset_failed(Unit *u) {
3598 Service *s = SERVICE(u);
3602 if (s->state == SERVICE_FAILED)
3603 service_set_state(s, SERVICE_DEAD);
3605 s->result = SERVICE_SUCCESS;
3606 s->reload_result = SERVICE_SUCCESS;
3609 static bool service_need_daemon_reload(Unit *u) {
3610 Service *s = SERVICE(u);
3614 #ifdef HAVE_SYSV_COMPAT
3619 if (stat(s->sysv_path, &st) < 0)
3620 /* What, cannot access this anymore? */
3623 if (s->sysv_mtime > 0 &&
3624 timespec_load(&st.st_mtim) != s->sysv_mtime)
3632 static int service_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
3633 Service *s = SERVICE(u);
3635 Set *pid_set = NULL;
3639 if (s->main_pid <= 0 && who == KILL_MAIN) {
3640 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3644 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3645 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3649 if (who == KILL_CONTROL || who == KILL_ALL)
3650 if (s->control_pid > 0)
3651 if (kill(s->control_pid, signo) < 0)
3654 if (who == KILL_MAIN || who == KILL_ALL)
3655 if (s->main_pid > 0)
3656 if (kill(s->main_pid, signo) < 0)
3659 if (who == KILL_ALL && mode == KILL_CONTROL_GROUP) {
3662 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
3665 /* Exclude the control/main pid from being killed via the cgroup */
3666 if (s->control_pid > 0)
3667 if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
3672 if (s->main_pid > 0)
3673 if ((q = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0) {
3677 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, pid_set, NULL);
3679 if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3690 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3691 [SERVICE_DEAD] = "dead",
3692 [SERVICE_START_PRE] = "start-pre",
3693 [SERVICE_START] = "start",
3694 [SERVICE_START_POST] = "start-post",
3695 [SERVICE_RUNNING] = "running",
3696 [SERVICE_EXITED] = "exited",
3697 [SERVICE_RELOAD] = "reload",
3698 [SERVICE_STOP] = "stop",
3699 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3700 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3701 [SERVICE_STOP_POST] = "stop-post",
3702 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3703 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3704 [SERVICE_FAILED] = "failed",
3705 [SERVICE_AUTO_RESTART] = "auto-restart",
3708 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3710 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3711 [SERVICE_RESTART_NO] = "no",
3712 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3713 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3714 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3715 [SERVICE_RESTART_ALWAYS] = "always"
3718 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3720 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3721 [SERVICE_SIMPLE] = "simple",
3722 [SERVICE_FORKING] = "forking",
3723 [SERVICE_ONESHOT] = "oneshot",
3724 [SERVICE_DBUS] = "dbus",
3725 [SERVICE_NOTIFY] = "notify",
3726 [SERVICE_IDLE] = "idle"
3729 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3731 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3732 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3733 [SERVICE_EXEC_START] = "ExecStart",
3734 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3735 [SERVICE_EXEC_RELOAD] = "ExecReload",
3736 [SERVICE_EXEC_STOP] = "ExecStop",
3737 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3740 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3742 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3743 [NOTIFY_NONE] = "none",
3744 [NOTIFY_MAIN] = "main",
3745 [NOTIFY_ALL] = "all"
3748 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3750 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3751 [SERVICE_SUCCESS] = "success",
3752 [SERVICE_FAILURE_RESOURCES] = "resources",
3753 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3754 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3755 [SERVICE_FAILURE_SIGNAL] = "signal",
3756 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3757 [SERVICE_FAILURE_WATCHDOG] = "watchdog"
3760 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3762 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3763 [SERVICE_START_LIMIT_NONE] = "none",
3764 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3765 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3766 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3768 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3770 const UnitVTable service_vtable = {
3771 .suffix = ".service",
3772 .object_size = sizeof(Service),
3777 .show_status = true,
3779 .init = service_init,
3780 .done = service_done,
3781 .load = service_load,
3783 .coldplug = service_coldplug,
3785 .dump = service_dump,
3787 .start = service_start,
3788 .stop = service_stop,
3789 .reload = service_reload,
3791 .can_reload = service_can_reload,
3793 .kill = service_kill,
3795 .serialize = service_serialize,
3796 .deserialize_item = service_deserialize_item,
3798 .active_state = service_active_state,
3799 .sub_state_to_string = service_sub_state_to_string,
3801 .check_gc = service_check_gc,
3802 .check_snapshot = service_check_snapshot,
3804 .sigchld_event = service_sigchld_event,
3805 .timer_event = service_timer_event,
3806 .fd_event = service_fd_event,
3808 .reset_failed = service_reset_failed,
3810 .need_daemon_reload = service_need_daemon_reload,
3812 .cgroup_notify_empty = service_cgroup_notify_event,
3813 .notify_message = service_notify_message,
3815 .bus_name_owner_change = service_bus_name_owner_change,
3816 .bus_query_pid_done = service_bus_query_pid_done,
3818 .bus_interface = "org.freedesktop.systemd1.Service",
3819 .bus_message_handler = bus_service_message_handler,
3820 .bus_invalidating_properties = bus_service_invalidating_properties,
3822 #ifdef HAVE_SYSV_COMPAT
3823 .enumerate = service_enumerate