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"
41 #include "path-util.h"
45 #ifdef HAVE_SYSV_COMPAT
47 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
49 typedef enum RunlevelType {
58 const RunlevelType type;
60 /* Standard SysV runlevels for start-up */
61 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
62 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
63 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
64 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
65 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
68 /* SUSE style boot.d */
69 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
72 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
73 /* Debian style rcS.d */
74 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
77 /* Standard SysV runlevels for shutdown */
78 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
79 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
81 /* Note that the order here matters, as we read the
82 directories in this order, and we want to make sure that
83 sysv_start_priority is known when we first load the
84 unit. And that value we only know from S links. Hence
85 UP/SYSINIT must be read before DOWN */
88 #define RUNLEVELS_UP "12345"
89 /* #define RUNLEVELS_DOWN "06" */
90 #define RUNLEVELS_BOOT "bBsS"
93 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
94 [SERVICE_DEAD] = UNIT_INACTIVE,
95 [SERVICE_START_PRE] = UNIT_ACTIVATING,
96 [SERVICE_START] = UNIT_ACTIVATING,
97 [SERVICE_START_POST] = UNIT_ACTIVATING,
98 [SERVICE_RUNNING] = UNIT_ACTIVE,
99 [SERVICE_EXITED] = UNIT_ACTIVE,
100 [SERVICE_RELOAD] = UNIT_RELOADING,
101 [SERVICE_STOP] = UNIT_DEACTIVATING,
102 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
103 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
104 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
105 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
106 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
107 [SERVICE_FAILED] = UNIT_FAILED,
108 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
111 static void service_init(Unit *u) {
112 Service *s = SERVICE(u);
115 assert(u->load_state == UNIT_STUB);
117 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
118 s->restart_usec = DEFAULT_RESTART_USEC;
119 s->type = _SERVICE_TYPE_INVALID;
121 s->watchdog_watch.type = WATCH_INVALID;
123 s->timer_watch.type = WATCH_INVALID;
124 #ifdef HAVE_SYSV_COMPAT
125 s->sysv_start_priority = -1;
126 s->sysv_start_priority_from_rcnd = -1;
129 s->guess_main_pid = true;
131 exec_context_init(&s->exec_context);
133 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
135 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
138 static void service_unwatch_control_pid(Service *s) {
141 if (s->control_pid <= 0)
144 unit_unwatch_pid(UNIT(s), s->control_pid);
148 static void service_unwatch_main_pid(Service *s) {
151 if (s->main_pid <= 0)
154 unit_unwatch_pid(UNIT(s), s->main_pid);
158 static void service_unwatch_pid_file(Service *s) {
159 if (!s->pid_file_pathspec)
162 log_debug("Stopping watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
163 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
164 path_spec_done(s->pid_file_pathspec);
165 free(s->pid_file_pathspec);
166 s->pid_file_pathspec = NULL;
169 static int service_set_main_pid(Service *s, pid_t pid) {
181 s->main_pid_known = true;
183 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
184 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
185 UNIT(s)->id, (unsigned long) pid);
187 s->main_pid_alien = true;
189 s->main_pid_alien = false;
191 exec_status_start(&s->main_exec_status, pid);
196 static void service_close_socket_fd(Service *s) {
199 if (s->socket_fd < 0)
202 close_nointr_nofail(s->socket_fd);
206 static void service_connection_unref(Service *s) {
209 if (!UNIT_DEREF(s->accept_socket))
212 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
213 unit_ref_unset(&s->accept_socket);
216 static void service_stop_watchdog(Service *s) {
219 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
220 s->watchdog_timestamp.realtime = 0;
221 s->watchdog_timestamp.monotonic = 0;
224 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart);
226 static void service_handle_watchdog(Service *s) {
232 if (s->watchdog_usec == 0)
235 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
236 if (offset >= s->watchdog_usec) {
237 log_error("%s watchdog timeout!", UNIT(s)->id);
238 service_enter_dead(s, SERVICE_FAILURE_WATCHDOG, true);
242 r = unit_watch_timer(UNIT(s), s->watchdog_usec - offset, &s->watchdog_watch);
244 log_warning("%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
247 static void service_reset_watchdog(Service *s) {
250 dual_timestamp_get(&s->watchdog_timestamp);
251 service_handle_watchdog(s);
254 static void service_done(Unit *u) {
255 Service *s = SERVICE(u);
262 #ifdef HAVE_SYSV_COMPAT
266 free(s->sysv_runlevels);
267 s->sysv_runlevels = NULL;
270 free(s->status_text);
271 s->status_text = NULL;
273 exec_context_done(&s->exec_context);
274 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
275 s->control_command = NULL;
276 s->main_command = NULL;
278 /* This will leak a process, but at least no memory or any of
280 service_unwatch_main_pid(s);
281 service_unwatch_control_pid(s);
282 service_unwatch_pid_file(s);
285 unit_unwatch_bus_name(u, s->bus_name);
290 service_close_socket_fd(s);
291 service_connection_unref(s);
293 unit_ref_unset(&s->accept_socket);
295 service_stop_watchdog(s);
297 unit_unwatch_timer(u, &s->timer_watch);
300 #ifdef HAVE_SYSV_COMPAT
301 static char *sysv_translate_name(const char *name) {
304 if (!(r = new(char, strlen(name) + sizeof(".service"))))
307 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
308 if (endswith(name, ".sh"))
309 /* Drop Debian-style .sh suffix */
310 strcpy(stpcpy(r, name) - 3, ".service");
313 if (startswith(name, "boot."))
314 /* Drop SuSE-style boot. prefix */
315 strcpy(stpcpy(r, name + 5), ".service");
317 #ifdef TARGET_FRUGALWARE
318 if (startswith(name, "rc."))
319 /* Drop Frugalware-style rc. prefix */
320 strcpy(stpcpy(r, name + 3), ".service");
323 /* Normal init scripts */
324 strcpy(stpcpy(r, name), ".service");
329 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
331 /* We silently ignore the $ prefix here. According to the LSB
332 * spec it simply indicates whether something is a
333 * standardized name or a distribution-specific one. Since we
334 * just follow what already exists and do not introduce new
335 * uses or names we don't care who introduced a new name. */
337 static const char * const table[] = {
338 /* LSB defined facilities */
339 "local_fs", SPECIAL_LOCAL_FS_TARGET,
340 #if defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
342 /* Due to unfortunate name selection in Mandriva,
343 * $network is provided by network-up which is ordered
344 * after network which actually starts interfaces.
345 * To break the loop, just ignore it */
346 "network", SPECIAL_NETWORK_TARGET,
348 "named", SPECIAL_NSS_LOOKUP_TARGET,
349 "portmap", SPECIAL_RPCBIND_TARGET,
350 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
351 "syslog", SPECIAL_SYSLOG_TARGET,
352 "time", SPECIAL_TIME_SYNC_TARGET,
354 /* common extensions */
355 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
356 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
359 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
360 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
364 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
365 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
366 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
370 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
381 n = *name == '$' ? name + 1 : name;
383 for (i = 0; i < ELEMENTSOF(table); i += 2) {
385 if (!streq(table[i], n))
391 if (!(r = strdup(table[i+1])))
397 /* If we don't know this name, fallback heuristics to figure
398 * out whether something is a target or a service alias. */
401 if (!unit_prefix_is_valid(n))
404 /* Facilities starting with $ are most likely targets */
405 r = unit_name_build(n, NULL, ".target");
406 } else if (filename && streq(name, filename))
407 /* Names equaling the file name of the services are redundant */
410 /* Everything else we assume to be normal service names */
411 r = sysv_translate_name(n);
422 static int sysv_fix_order(Service *s) {
428 if (s->sysv_start_priority < 0)
431 /* For each pair of services where at least one lacks a LSB
432 * header, we use the start priority value to order things. */
434 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
437 bool special_s, special_t;
444 if (UNIT(t)->load_state != UNIT_LOADED)
447 if (t->sysv_start_priority < 0)
450 /* If both units have modern headers we don't care
451 * about the priorities */
452 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
453 (UNIT(t)->fragment_path || t->sysv_has_lsb))
456 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
457 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
459 if (special_t && !special_s)
461 else if (special_s && !special_t)
463 else if (t->sysv_start_priority < s->sysv_start_priority)
465 else if (t->sysv_start_priority > s->sysv_start_priority)
470 /* FIXME: Maybe we should compare the name here lexicographically? */
472 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
479 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
482 if (!(c = new0(ExecCommand, 1)))
485 if (!(c->path = strdup(path))) {
490 if (!(c->argv = strv_new(path, arg1, NULL))) {
499 static int sysv_exec_commands(Service *s) {
503 assert(s->sysv_path);
505 if (!(c = exec_command_new(s->sysv_path, "start")))
507 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
509 if (!(c = exec_command_new(s->sysv_path, "stop")))
511 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
513 if (!(c = exec_command_new(s->sysv_path, "reload")))
515 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
520 static int service_load_sysv_path(Service *s, const char *path) {
531 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
539 if (!(f = fopen(path, "re"))) {
540 r = errno == ENOENT ? 0 : -errno;
545 if (fstat(fileno(f), &st) < 0) {
551 if (!(s->sysv_path = strdup(path))) {
556 s->sysv_mtime = timespec_load(&st.st_mtim);
558 if (null_or_empty(&st)) {
559 u->load_state = UNIT_MASKED;
565 char l[LINE_MAX], *t;
567 if (!fgets(l, sizeof(l), f)) {
572 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
582 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
584 s->sysv_has_lsb = true;
588 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
594 t += strspn(t, WHITESPACE);
596 if (state == NORMAL) {
598 /* Try to parse Red Hat style chkconfig headers */
600 if (startswith_no_case(t, "chkconfig:")) {
602 char runlevels[16], *k;
606 if (sscanf(t+10, "%15s %i %*i",
608 &start_priority) != 2) {
610 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
614 /* A start priority gathered from the
615 * symlink farms is preferred over the
616 * data from the LSB header. */
617 if (start_priority < 0 || start_priority > 99)
618 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
620 s->sysv_start_priority = start_priority;
622 char_array_0(runlevels);
623 k = delete_chars(runlevels, WHITESPACE "-");
628 if (!(d = strdup(k))) {
633 free(s->sysv_runlevels);
634 s->sysv_runlevels = d;
637 } else if (startswith_no_case(t, "description:")) {
639 size_t k = strlen(t);
643 if (t[k-1] == '\\') {
648 if ((j = strstrip(t+12)) && *j) {
649 if (!(d = strdup(j))) {
656 free(chkconfig_description);
657 chkconfig_description = d;
659 } else if (startswith_no_case(t, "pidfile:")) {
666 if (!path_is_absolute(fn)) {
667 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
671 if (!(fn = strdup(fn))) {
680 } else if (state == DESCRIPTION) {
682 /* Try to parse Red Hat style description
685 size_t k = strlen(t);
693 if ((j = strstrip(t)) && *j) {
696 if (chkconfig_description)
697 d = join(chkconfig_description, " ", j, NULL);
706 free(chkconfig_description);
707 chkconfig_description = d;
710 } else if (state == LSB || state == LSB_DESCRIPTION) {
712 if (startswith_no_case(t, "Provides:")) {
718 FOREACH_WORD_QUOTED(w, z, t+9, i) {
721 if (!(n = strndup(w, z))) {
726 r = sysv_translate_facility(n, path_get_file_name(path), &m);
735 if (unit_name_to_type(m) == UNIT_SERVICE)
736 r = unit_add_name(u, m);
743 * indication that the
745 * now available. This
748 * targets do NOT pull
751 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
754 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
759 } else if (startswith_no_case(t, "Required-Start:") ||
760 startswith_no_case(t, "Should-Start:") ||
761 startswith_no_case(t, "X-Start-Before:") ||
762 startswith_no_case(t, "X-Start-After:")) {
768 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
771 if (!(n = strndup(w, z))) {
776 r = sysv_translate_facility(n, path_get_file_name(path), &m);
779 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
789 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
792 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
796 } else if (startswith_no_case(t, "Default-Start:")) {
801 k = delete_chars(t+14, WHITESPACE "-");
804 if (!(d = strdup(k))) {
809 free(s->sysv_runlevels);
810 s->sysv_runlevels = d;
813 } else if (startswith_no_case(t, "Description:")) {
816 state = LSB_DESCRIPTION;
818 if ((j = strstrip(t+12)) && *j) {
819 if (!(d = strdup(j))) {
826 free(long_description);
827 long_description = d;
829 } else if (startswith_no_case(t, "Short-Description:")) {
834 if ((j = strstrip(t+18)) && *j) {
835 if (!(d = strdup(j))) {
842 free(short_description);
843 short_description = d;
845 } else if (state == LSB_DESCRIPTION) {
847 if (startswith(l, "#\t") || startswith(l, "# ")) {
850 if ((j = strstrip(t)) && *j) {
853 if (long_description)
854 d = join(long_description, " ", t, NULL);
863 free(long_description);
864 long_description = d;
873 if ((r = sysv_exec_commands(s)) < 0)
875 if (s->sysv_runlevels &&
876 chars_intersect(RUNLEVELS_BOOT, s->sysv_runlevels) &&
877 chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
878 /* Service has both boot and "up" runlevels
879 configured. Kill the "up" ones. */
880 delete_chars(s->sysv_runlevels, RUNLEVELS_UP);
883 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
884 /* If there a runlevels configured for this service
885 * but none of the standard ones, then we assume this
886 * is some special kind of service (which might be
887 * needed for early boot) and don't create any links
890 UNIT(s)->default_dependencies = false;
892 /* Don't timeout special services during boot (like fsck) */
895 s->timeout_usec = DEFAULT_SYSV_TIMEOUT_USEC;
897 /* Special setting for all SysV services */
898 s->type = SERVICE_FORKING;
899 s->remain_after_exit = !s->pid_file;
900 s->guess_main_pid = false;
901 s->restart = SERVICE_RESTART_NO;
902 s->exec_context.ignore_sigpipe = false;
904 if (UNIT(s)->manager->sysv_console)
905 s->exec_context.std_output = EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
907 s->exec_context.kill_mode = KILL_PROCESS;
909 /* We use the long description only if
910 * no short description is set. */
912 if (short_description)
913 description = short_description;
914 else if (chkconfig_description)
915 description = chkconfig_description;
916 else if (long_description)
917 description = long_description;
924 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
932 /* The priority that has been set in /etc/rcN.d/ hierarchies
933 * takes precedence over what is stored as default in the LSB
935 if (s->sysv_start_priority_from_rcnd >= 0)
936 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
938 u->load_state = UNIT_LOADED;
946 free(short_description);
947 free(long_description);
948 free(chkconfig_description);
953 static int service_load_sysv_name(Service *s, const char *name) {
959 /* For SysV services we strip the boot.*, rc.* and *.sh
960 * prefixes/suffixes. */
961 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
962 if (endswith(name, ".sh.service"))
967 if (startswith(name, "boot."))
971 #ifdef TARGET_FRUGALWARE
972 if (startswith(name, "rc."))
976 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
980 path = join(*p, "/", name, NULL);
984 assert(endswith(path, ".service"));
985 path[strlen(path)-8] = 0;
987 r = service_load_sysv_path(s, path);
989 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
990 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
991 /* Try Debian style *.sh source'able init scripts */
993 r = service_load_sysv_path(s, path);
999 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1000 /* Try SUSE style boot.* init scripts */
1002 path = join(*p, "/boot.", name, NULL);
1006 /* Drop .service suffix */
1007 path[strlen(path)-8] = 0;
1008 r = service_load_sysv_path(s, path);
1013 #ifdef TARGET_FRUGALWARE
1014 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1015 /* Try Frugalware style rc.* init scripts */
1017 path = join(*p, "/rc.", name, NULL);
1021 /* Drop .service suffix */
1022 path[strlen(path)-8] = 0;
1023 r = service_load_sysv_path(s, path);
1031 if ((UNIT(s)->load_state != UNIT_STUB))
1038 static int service_load_sysv(Service *s) {
1045 /* Load service data from SysV init scripts, preferably with
1046 * LSB headers ... */
1048 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1051 if ((t = UNIT(s)->id))
1052 if ((r = service_load_sysv_name(s, t)) < 0)
1055 if (UNIT(s)->load_state == UNIT_STUB)
1056 SET_FOREACH(t, UNIT(s)->names, i) {
1057 if (t == UNIT(s)->id)
1060 if ((r = service_load_sysv_name(s, t)) < 0)
1063 if (UNIT(s)->load_state != UNIT_STUB)
1071 static int fsck_fix_order(Service *s) {
1077 if (s->fsck_passno <= 0)
1080 /* For each pair of services where both have an fsck priority
1081 * we order things based on it. */
1083 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1092 if (UNIT(t)->load_state != UNIT_LOADED)
1095 if (t->fsck_passno <= 0)
1098 if (t->fsck_passno < s->fsck_passno)
1100 else if (t->fsck_passno > s->fsck_passno)
1105 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1112 static int service_verify(Service *s) {
1115 if (UNIT(s)->load_state != UNIT_LOADED)
1118 if (!s->exec_command[SERVICE_EXEC_START]) {
1119 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1123 if (s->type != SERVICE_ONESHOT &&
1124 s->exec_command[SERVICE_EXEC_START]->command_next) {
1125 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1129 if (s->type == SERVICE_ONESHOT &&
1130 s->exec_command[SERVICE_EXEC_RELOAD]) {
1131 log_error("%s has an ExecReload setting, which is not allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1135 if (s->type == SERVICE_DBUS && !s->bus_name) {
1136 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1140 if (s->bus_name && s->type != SERVICE_DBUS)
1141 log_warning("%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1143 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
1144 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1151 static int service_add_default_dependencies(Service *s) {
1156 /* Add a number of automatic dependencies useful for the
1157 * majority of services. */
1159 /* First, pull in base system */
1160 if (UNIT(s)->manager->running_as == MANAGER_SYSTEM) {
1162 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1165 } else if (UNIT(s)->manager->running_as == MANAGER_USER) {
1167 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1171 /* Second, activate normal shutdown */
1172 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1175 static void service_fix_output(Service *s) {
1178 /* If nothing has been explicitly configured, patch default
1179 * output in. If input is socket/tty we avoid this however,
1180 * since in that case we want output to default to the same
1181 * place as we read input from. */
1183 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1184 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1185 s->exec_context.std_input == EXEC_INPUT_NULL)
1186 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1188 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1189 s->exec_context.std_input == EXEC_INPUT_NULL)
1190 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1193 static int service_load(Unit *u) {
1195 Service *s = SERVICE(u);
1199 /* Load a .service file */
1200 if ((r = unit_load_fragment(u)) < 0)
1203 #ifdef HAVE_SYSV_COMPAT
1204 /* Load a classic init script as a fallback, if we couldn't find anything */
1205 if (u->load_state == UNIT_STUB)
1206 if ((r = service_load_sysv(s)) < 0)
1210 /* Still nothing found? Then let's give up */
1211 if (u->load_state == UNIT_STUB)
1214 /* We were able to load something, then let's add in the
1215 * dropin directories. */
1216 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1219 /* This is a new unit? Then let's add in some extras */
1220 if (u->load_state == UNIT_LOADED) {
1221 if (s->type == _SERVICE_TYPE_INVALID)
1222 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1224 service_fix_output(s);
1226 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1229 if ((r = unit_add_default_cgroups(u)) < 0)
1232 #ifdef HAVE_SYSV_COMPAT
1233 if ((r = sysv_fix_order(s)) < 0)
1237 if ((r = fsck_fix_order(s)) < 0)
1241 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1244 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1245 s->notify_access = NOTIFY_MAIN;
1247 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1248 s->notify_access = NOTIFY_MAIN;
1250 if (s->type == SERVICE_DBUS || s->bus_name)
1251 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1254 if (UNIT(s)->default_dependencies)
1255 if ((r = service_add_default_dependencies(s)) < 0)
1259 return service_verify(s);
1262 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1264 ServiceExecCommand c;
1265 Service *s = SERVICE(u);
1266 const char *prefix2;
1271 p2 = strappend(prefix, "\t");
1272 prefix2 = p2 ? p2 : prefix;
1275 "%sService State: %s\n"
1277 "%sReload Result: %s\n"
1278 "%sPermissionsStartOnly: %s\n"
1279 "%sRootDirectoryStartOnly: %s\n"
1280 "%sRemainAfterExit: %s\n"
1281 "%sGuessMainPID: %s\n"
1284 "%sNotifyAccess: %s\n",
1285 prefix, service_state_to_string(s->state),
1286 prefix, service_result_to_string(s->result),
1287 prefix, service_result_to_string(s->reload_result),
1288 prefix, yes_no(s->permissions_start_only),
1289 prefix, yes_no(s->root_directory_start_only),
1290 prefix, yes_no(s->remain_after_exit),
1291 prefix, yes_no(s->guess_main_pid),
1292 prefix, service_type_to_string(s->type),
1293 prefix, service_restart_to_string(s->restart),
1294 prefix, notify_access_to_string(s->notify_access));
1296 if (s->control_pid > 0)
1298 "%sControl PID: %lu\n",
1299 prefix, (unsigned long) s->control_pid);
1301 if (s->main_pid > 0)
1304 "%sMain PID Known: %s\n"
1305 "%sMain PID Alien: %s\n",
1306 prefix, (unsigned long) s->main_pid,
1307 prefix, yes_no(s->main_pid_known),
1308 prefix, yes_no(s->main_pid_alien));
1313 prefix, s->pid_file);
1318 "%sBus Name Good: %s\n",
1319 prefix, s->bus_name,
1320 prefix, yes_no(s->bus_name_good));
1322 exec_context_dump(&s->exec_context, f, prefix);
1324 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1326 if (!s->exec_command[c])
1329 fprintf(f, "%s-> %s:\n",
1330 prefix, service_exec_command_to_string(c));
1332 exec_command_dump_list(s->exec_command[c], f, prefix2);
1335 #ifdef HAVE_SYSV_COMPAT
1338 "%sSysV Init Script Path: %s\n"
1339 "%sSysV Init Script has LSB Header: %s\n"
1340 "%sSysVEnabled: %s\n",
1341 prefix, s->sysv_path,
1342 prefix, yes_no(s->sysv_has_lsb),
1343 prefix, yes_no(s->sysv_enabled));
1345 if (s->sysv_start_priority >= 0)
1347 "%sSysVStartPriority: %i\n",
1348 prefix, s->sysv_start_priority);
1350 if (s->sysv_runlevels)
1351 fprintf(f, "%sSysVRunLevels: %s\n",
1352 prefix, s->sysv_runlevels);
1355 if (s->fsck_passno > 0)
1357 "%sFsckPassNo: %i\n",
1358 prefix, s->fsck_passno);
1361 fprintf(f, "%sStatus Text: %s\n",
1362 prefix, s->status_text);
1367 static int service_load_pid_file(Service *s, bool may_warn) {
1377 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1379 log_info("PID file %s not readable (yet?) after %s.",
1380 s->pid_file, service_state_to_string(s->state));
1384 r = parse_pid(k, &pid);
1390 if (kill(pid, 0) < 0 && errno != EPERM) {
1392 log_info("PID %lu read from file %s does not exist.",
1393 (unsigned long) pid, s->pid_file);
1397 if (s->main_pid_known) {
1398 if (pid == s->main_pid)
1401 log_debug("Main PID changing: %lu -> %lu",
1402 (unsigned long) s->main_pid, (unsigned long) pid);
1403 service_unwatch_main_pid(s);
1404 s->main_pid_known = false;
1406 log_debug("Main PID loaded: %lu", (unsigned long) pid);
1408 if ((r = service_set_main_pid(s, pid)) < 0)
1411 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1412 /* FIXME: we need to do something here */
1418 static int service_search_main_pid(Service *s) {
1424 /* If we know it anyway, don't ever fallback to unreliable
1426 if (s->main_pid_known)
1429 if (!s->guess_main_pid)
1432 assert(s->main_pid <= 0);
1434 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1437 log_debug("Main PID guessed: %lu", (unsigned long) pid);
1438 if ((r = service_set_main_pid(s, pid)) < 0)
1441 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1442 /* FIXME: we need to do something here */
1448 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1454 /* Notifies all our sockets when we die */
1456 if (s->socket_fd >= 0)
1459 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1460 if (u->type == UNIT_SOCKET)
1461 socket_notify_service_dead(SOCKET(u), failed_permanent);
1466 static void service_set_state(Service *s, ServiceState state) {
1467 ServiceState old_state;
1470 old_state = s->state;
1473 service_unwatch_pid_file(s);
1475 if (state != SERVICE_START_PRE &&
1476 state != SERVICE_START &&
1477 state != SERVICE_START_POST &&
1478 state != SERVICE_RELOAD &&
1479 state != SERVICE_STOP &&
1480 state != SERVICE_STOP_SIGTERM &&
1481 state != SERVICE_STOP_SIGKILL &&
1482 state != SERVICE_STOP_POST &&
1483 state != SERVICE_FINAL_SIGTERM &&
1484 state != SERVICE_FINAL_SIGKILL &&
1485 state != SERVICE_AUTO_RESTART)
1486 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1488 if (state != SERVICE_START &&
1489 state != SERVICE_START_POST &&
1490 state != SERVICE_RUNNING &&
1491 state != SERVICE_RELOAD &&
1492 state != SERVICE_STOP &&
1493 state != SERVICE_STOP_SIGTERM &&
1494 state != SERVICE_STOP_SIGKILL) {
1495 service_unwatch_main_pid(s);
1496 s->main_command = NULL;
1499 if (state != SERVICE_START_PRE &&
1500 state != SERVICE_START &&
1501 state != SERVICE_START_POST &&
1502 state != SERVICE_RELOAD &&
1503 state != SERVICE_STOP &&
1504 state != SERVICE_STOP_SIGTERM &&
1505 state != SERVICE_STOP_SIGKILL &&
1506 state != SERVICE_STOP_POST &&
1507 state != SERVICE_FINAL_SIGTERM &&
1508 state != SERVICE_FINAL_SIGKILL) {
1509 service_unwatch_control_pid(s);
1510 s->control_command = NULL;
1511 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1514 if (state == SERVICE_DEAD ||
1515 state == SERVICE_STOP ||
1516 state == SERVICE_STOP_SIGTERM ||
1517 state == SERVICE_STOP_SIGKILL ||
1518 state == SERVICE_STOP_POST ||
1519 state == SERVICE_FINAL_SIGTERM ||
1520 state == SERVICE_FINAL_SIGKILL ||
1521 state == SERVICE_FAILED ||
1522 state == SERVICE_AUTO_RESTART)
1523 service_notify_sockets_dead(s, false);
1525 if (state != SERVICE_START_PRE &&
1526 state != SERVICE_START &&
1527 state != SERVICE_START_POST &&
1528 state != SERVICE_RUNNING &&
1529 state != SERVICE_RELOAD &&
1530 state != SERVICE_STOP &&
1531 state != SERVICE_STOP_SIGTERM &&
1532 state != SERVICE_STOP_SIGKILL &&
1533 state != SERVICE_STOP_POST &&
1534 state != SERVICE_FINAL_SIGTERM &&
1535 state != SERVICE_FINAL_SIGKILL &&
1536 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1537 service_close_socket_fd(s);
1538 service_connection_unref(s);
1541 if (state == SERVICE_STOP)
1542 service_stop_watchdog(s);
1544 /* For the inactive states unit_notify() will trim the cgroup,
1545 * but for exit we have to do that ourselves... */
1546 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1547 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1549 if (old_state != state)
1550 log_debug("%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
1552 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], s->reload_result == SERVICE_SUCCESS);
1553 s->reload_result = SERVICE_SUCCESS;
1556 static int service_coldplug(Unit *u) {
1557 Service *s = SERVICE(u);
1561 assert(s->state == SERVICE_DEAD);
1563 if (s->deserialized_state != s->state) {
1565 if (s->deserialized_state == SERVICE_START_PRE ||
1566 s->deserialized_state == SERVICE_START ||
1567 s->deserialized_state == SERVICE_START_POST ||
1568 s->deserialized_state == SERVICE_RELOAD ||
1569 s->deserialized_state == SERVICE_STOP ||
1570 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1571 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1572 s->deserialized_state == SERVICE_STOP_POST ||
1573 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1574 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1575 s->deserialized_state == SERVICE_AUTO_RESTART) {
1577 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1580 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1582 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1587 if ((s->deserialized_state == SERVICE_START &&
1588 (s->type == SERVICE_FORKING ||
1589 s->type == SERVICE_DBUS ||
1590 s->type == SERVICE_ONESHOT ||
1591 s->type == SERVICE_NOTIFY)) ||
1592 s->deserialized_state == SERVICE_START_POST ||
1593 s->deserialized_state == SERVICE_RUNNING ||
1594 s->deserialized_state == SERVICE_RELOAD ||
1595 s->deserialized_state == SERVICE_STOP ||
1596 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1597 s->deserialized_state == SERVICE_STOP_SIGKILL)
1598 if (s->main_pid > 0)
1599 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1602 if (s->deserialized_state == SERVICE_START_PRE ||
1603 s->deserialized_state == SERVICE_START ||
1604 s->deserialized_state == SERVICE_START_POST ||
1605 s->deserialized_state == SERVICE_RELOAD ||
1606 s->deserialized_state == SERVICE_STOP ||
1607 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1608 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1609 s->deserialized_state == SERVICE_STOP_POST ||
1610 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1611 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1612 if (s->control_pid > 0)
1613 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1616 if (s->deserialized_state == SERVICE_START_POST ||
1617 s->deserialized_state == SERVICE_RUNNING)
1618 service_handle_watchdog(s);
1620 service_set_state(s, s->deserialized_state);
1625 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1629 unsigned rn_fds = 0;
1636 if (s->socket_fd >= 0)
1639 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1644 if (u->type != UNIT_SOCKET)
1649 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1661 if (!(t = new(int, rn_fds+cn_fds))) {
1667 memcpy(t, rfds, rn_fds * sizeof(int));
1668 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1673 rn_fds = rn_fds+cn_fds;
1688 static int service_spawn(
1693 bool apply_permissions,
1695 bool apply_tty_stdin,
1696 bool set_notify_socket,
1702 int *fds = NULL, *fdsbuf = NULL;
1703 unsigned n_fds = 0, n_env = 0;
1704 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1711 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1712 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1713 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1715 if (s->socket_fd >= 0) {
1716 fds = &s->socket_fd;
1719 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1726 if (timeout && s->timeout_usec) {
1727 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1730 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1732 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1737 if (!(our_env = new0(char*, 4))) {
1742 if (set_notify_socket)
1743 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1748 if (s->main_pid > 0)
1749 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1754 if (s->watchdog_usec > 0)
1755 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1760 if (!(final_env = strv_env_merge(2,
1761 UNIT(s)->manager->environment,
1776 UNIT(s)->manager->confirm_spawn,
1777 UNIT(s)->cgroup_bondings,
1778 UNIT(s)->cgroup_attributes,
1779 is_control ? "control" : NULL,
1780 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1787 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1788 /* FIXME: we need to do something here */
1794 strv_free(final_env);
1804 strv_free(final_env);
1807 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1812 static int main_pid_good(Service *s) {
1815 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1818 /* If we know the pid file, then lets just check if it is
1820 if (s->main_pid_known) {
1822 /* If it's an alien child let's check if it is still
1824 if (s->main_pid_alien)
1825 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1827 /* .. otherwise assume we'll get a SIGCHLD for it,
1828 * which we really should wait for to collect exit
1829 * status and code */
1830 return s->main_pid > 0;
1833 /* We don't know the pid */
1837 static int control_pid_good(Service *s) {
1840 return s->control_pid > 0;
1843 static int cgroup_good(Service *s) {
1848 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1854 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1858 if (f != SERVICE_SUCCESS)
1861 if (allow_restart &&
1862 !s->forbid_restart &&
1863 (s->restart == SERVICE_RESTART_ALWAYS ||
1864 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1865 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1866 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1867 s->result == SERVICE_FAILURE_CORE_DUMP)))) {
1869 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
1873 service_set_state(s, SERVICE_AUTO_RESTART);
1875 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1877 s->forbid_restart = false;
1882 log_warning("%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1883 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1886 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1888 static void service_enter_stop_post(Service *s, ServiceResult f) {
1892 if (f != SERVICE_SUCCESS)
1895 service_unwatch_control_pid(s);
1897 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1898 s->control_command_id = SERVICE_EXEC_STOP_POST;
1900 r = service_spawn(s,
1904 !s->permissions_start_only,
1905 !s->root_directory_start_only,
1914 service_set_state(s, SERVICE_STOP_POST);
1916 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1921 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
1922 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1925 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1927 Set *pid_set = NULL;
1928 bool wait_for_exit = false;
1932 if (f != SERVICE_SUCCESS)
1935 if (s->exec_context.kill_mode != KILL_NONE) {
1936 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1938 if (s->main_pid > 0) {
1939 if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
1940 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1942 wait_for_exit = !s->main_pid_alien;
1945 if (s->control_pid > 0) {
1946 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1947 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1949 wait_for_exit = true;
1952 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1954 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1959 /* Exclude the main/control pids from being killed via the cgroup */
1960 if (s->main_pid > 0)
1961 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1964 if (s->control_pid > 0)
1965 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1968 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
1970 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1971 log_warning("Failed to kill control group: %s", strerror(-r));
1973 wait_for_exit = true;
1980 if (wait_for_exit) {
1981 if (s->timeout_usec > 0)
1982 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1985 service_set_state(s, state);
1986 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1987 service_enter_stop_post(s, SERVICE_SUCCESS);
1989 service_enter_dead(s, SERVICE_SUCCESS, true);
1994 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1996 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1997 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1999 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2005 static void service_enter_stop(Service *s, ServiceResult f) {
2010 if (f != SERVICE_SUCCESS)
2013 service_unwatch_control_pid(s);
2015 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2016 s->control_command_id = SERVICE_EXEC_STOP;
2018 r = service_spawn(s,
2022 !s->permissions_start_only,
2023 !s->root_directory_start_only,
2031 service_set_state(s, SERVICE_STOP);
2033 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2038 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2039 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2042 static void service_enter_running(Service *s, ServiceResult f) {
2043 int main_pid_ok, cgroup_ok;
2046 if (f != SERVICE_SUCCESS)
2049 main_pid_ok = main_pid_good(s);
2050 cgroup_ok = cgroup_good(s);
2052 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2053 (s->bus_name_good || s->type != SERVICE_DBUS))
2054 service_set_state(s, SERVICE_RUNNING);
2055 else if (s->remain_after_exit)
2056 service_set_state(s, SERVICE_EXITED);
2058 service_enter_stop(s, SERVICE_SUCCESS);
2061 static void service_enter_start_post(Service *s) {
2065 service_unwatch_control_pid(s);
2067 if (s->watchdog_usec > 0)
2068 service_reset_watchdog(s);
2070 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2071 s->control_command_id = SERVICE_EXEC_START_POST;
2073 r = service_spawn(s,
2077 !s->permissions_start_only,
2078 !s->root_directory_start_only,
2086 service_set_state(s, SERVICE_START_POST);
2088 service_enter_running(s, SERVICE_SUCCESS);
2093 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2094 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2097 static void service_enter_start(Service *s) {
2104 assert(s->exec_command[SERVICE_EXEC_START]);
2105 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2107 if (s->type == SERVICE_FORKING)
2108 service_unwatch_control_pid(s);
2110 service_unwatch_main_pid(s);
2112 /* We want to ensure that nobody leaks processes from
2113 * START_PRE here, so let's go on a killing spree, People
2114 * should not spawn long running processes from START_PRE. */
2115 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2117 if (s->type == SERVICE_FORKING) {
2118 s->control_command_id = SERVICE_EXEC_START;
2119 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2121 s->main_command = NULL;
2123 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2124 s->control_command = NULL;
2126 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2129 r = service_spawn(s,
2131 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
2136 s->notify_access != NOTIFY_NONE,
2142 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2143 /* For simple services we immediately start
2144 * the START_POST binaries. */
2146 service_set_main_pid(s, pid);
2147 service_enter_start_post(s);
2149 } else if (s->type == SERVICE_FORKING) {
2151 /* For forking services we wait until the start
2152 * process exited. */
2154 s->control_pid = pid;
2155 service_set_state(s, SERVICE_START);
2157 } else if (s->type == SERVICE_ONESHOT ||
2158 s->type == SERVICE_DBUS ||
2159 s->type == SERVICE_NOTIFY) {
2161 /* For oneshot services we wait until the start
2162 * process exited, too, but it is our main process. */
2164 /* For D-Bus services we know the main pid right away,
2165 * but wait for the bus name to appear on the
2166 * bus. Notify services are similar. */
2168 service_set_main_pid(s, pid);
2169 service_set_state(s, SERVICE_START);
2171 assert_not_reached("Unknown service type");
2176 log_warning("%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2177 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2180 static void service_enter_start_pre(Service *s) {
2185 service_unwatch_control_pid(s);
2187 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2189 /* Before we start anything, let's clear up what might
2190 * be left from previous runs. */
2191 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2193 s->control_command_id = SERVICE_EXEC_START_PRE;
2195 r = service_spawn(s,
2199 !s->permissions_start_only,
2200 !s->root_directory_start_only,
2208 service_set_state(s, SERVICE_START_PRE);
2210 service_enter_start(s);
2215 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2216 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2219 static void service_enter_restart(Service *s) {
2224 dbus_error_init(&error);
2227 log_info("Job pending for unit, delaying automatic restart.");
2229 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
2233 /* Any units that are bound to this service must also be
2234 * restarted. We use JOB_RESTART (instead of the more obvious
2235 * JOB_START) here so that those dependency jobs will be added
2237 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2241 log_debug("%s scheduled restart job.", UNIT(s)->id);
2245 log_warning("%s failed to schedule restart job: %s", UNIT(s)->id, bus_error(&error, -r));
2246 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2248 dbus_error_free(&error);
2251 static void service_enter_reload(Service *s) {
2256 service_unwatch_control_pid(s);
2258 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2259 s->control_command_id = SERVICE_EXEC_RELOAD;
2261 r = service_spawn(s,
2265 !s->permissions_start_only,
2266 !s->root_directory_start_only,
2274 service_set_state(s, SERVICE_RELOAD);
2276 service_enter_running(s, SERVICE_SUCCESS);
2281 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
2282 s->reload_result = SERVICE_FAILURE_RESOURCES;
2283 service_enter_running(s, SERVICE_SUCCESS);
2286 static void service_run_next_control(Service *s) {
2290 assert(s->control_command);
2291 assert(s->control_command->command_next);
2293 assert(s->control_command_id != SERVICE_EXEC_START);
2295 s->control_command = s->control_command->command_next;
2296 service_unwatch_control_pid(s);
2298 r = service_spawn(s,
2302 !s->permissions_start_only,
2303 !s->root_directory_start_only,
2304 s->control_command_id == SERVICE_EXEC_START_PRE ||
2305 s->control_command_id == SERVICE_EXEC_STOP_POST,
2315 log_warning("%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
2317 if (s->state == SERVICE_START_PRE)
2318 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2319 else if (s->state == SERVICE_STOP)
2320 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2321 else if (s->state == SERVICE_STOP_POST)
2322 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2323 else if (s->state == SERVICE_RELOAD) {
2324 s->reload_result = SERVICE_FAILURE_RESOURCES;
2325 service_enter_running(s, SERVICE_SUCCESS);
2327 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2330 static void service_run_next_main(Service *s) {
2335 assert(s->main_command);
2336 assert(s->main_command->command_next);
2337 assert(s->type == SERVICE_ONESHOT);
2339 s->main_command = s->main_command->command_next;
2340 service_unwatch_main_pid(s);
2342 r = service_spawn(s,
2349 s->notify_access != NOTIFY_NONE,
2355 service_set_main_pid(s, pid);
2360 log_warning("%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2361 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2364 static int service_start_limit_test(Service *s) {
2367 if (ratelimit_test(&s->start_limit))
2370 switch (s->start_limit_action) {
2372 case SERVICE_START_LIMIT_NONE:
2373 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s)->id);
2376 case SERVICE_START_LIMIT_REBOOT: {
2380 dbus_error_init(&error);
2382 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2384 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2386 log_error("Failed to reboot: %s.", bus_error(&error, r));
2387 dbus_error_free(&error);
2393 case SERVICE_START_LIMIT_REBOOT_FORCE:
2394 log_warning("%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2395 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2398 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2399 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2400 reboot(RB_AUTOBOOT);
2404 log_error("start limit action=%i", s->start_limit_action);
2405 assert_not_reached("Unknown StartLimitAction.");
2411 static int service_start(Unit *u) {
2412 Service *s = SERVICE(u);
2417 /* We cannot fulfill this request right now, try again later
2419 if (s->state == SERVICE_STOP ||
2420 s->state == SERVICE_STOP_SIGTERM ||
2421 s->state == SERVICE_STOP_SIGKILL ||
2422 s->state == SERVICE_STOP_POST ||
2423 s->state == SERVICE_FINAL_SIGTERM ||
2424 s->state == SERVICE_FINAL_SIGKILL)
2427 /* Already on it! */
2428 if (s->state == SERVICE_START_PRE ||
2429 s->state == SERVICE_START ||
2430 s->state == SERVICE_START_POST)
2433 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART);
2435 /* Make sure we don't enter a busy loop of some kind. */
2436 r = service_start_limit_test(s);
2438 service_notify_sockets_dead(s, true);
2442 s->result = SERVICE_SUCCESS;
2443 s->reload_result = SERVICE_SUCCESS;
2444 s->main_pid_known = false;
2445 s->main_pid_alien = false;
2446 s->forbid_restart = false;
2448 service_enter_start_pre(s);
2452 static int service_stop(Unit *u) {
2453 Service *s = SERVICE(u);
2457 /* This is a user request, so don't do restarts on this
2459 s->forbid_restart = true;
2462 if (s->state == SERVICE_STOP ||
2463 s->state == SERVICE_STOP_SIGTERM ||
2464 s->state == SERVICE_STOP_SIGKILL ||
2465 s->state == SERVICE_STOP_POST ||
2466 s->state == SERVICE_FINAL_SIGTERM ||
2467 s->state == SERVICE_FINAL_SIGKILL)
2470 /* Don't allow a restart */
2471 if (s->state == SERVICE_AUTO_RESTART) {
2472 service_set_state(s, SERVICE_DEAD);
2476 /* If there's already something running we go directly into
2478 if (s->state == SERVICE_START_PRE ||
2479 s->state == SERVICE_START ||
2480 s->state == SERVICE_START_POST ||
2481 s->state == SERVICE_RELOAD) {
2482 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2486 assert(s->state == SERVICE_RUNNING ||
2487 s->state == SERVICE_EXITED);
2489 service_enter_stop(s, SERVICE_SUCCESS);
2493 static int service_reload(Unit *u) {
2494 Service *s = SERVICE(u);
2498 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2500 service_enter_reload(s);
2504 static bool service_can_reload(Unit *u) {
2505 Service *s = SERVICE(u);
2509 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2512 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2513 Service *s = SERVICE(u);
2519 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2520 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2521 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2523 if (s->control_pid > 0)
2524 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2526 if (s->main_pid_known && s->main_pid > 0)
2527 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2529 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2532 unit_serialize_item(u, f, "status-text", s->status_text);
2534 /* FIXME: There's a minor uncleanliness here: if there are
2535 * multiple commands attached here, we will start from the
2536 * first one again */
2537 if (s->control_command_id >= 0)
2538 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2540 if (s->socket_fd >= 0) {
2543 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2546 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2549 if (s->main_exec_status.pid > 0) {
2550 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2551 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2552 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2554 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2555 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2556 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2559 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2560 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2565 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2566 Service *s = SERVICE(u);
2573 if (streq(key, "state")) {
2576 if ((state = service_state_from_string(value)) < 0)
2577 log_debug("Failed to parse state value %s", value);
2579 s->deserialized_state = state;
2580 } else if (streq(key, "result")) {
2583 f = service_result_from_string(value);
2585 log_debug("Failed to parse result value %s", value);
2586 else if (f != SERVICE_SUCCESS)
2589 } else if (streq(key, "reload-result")) {
2592 f = service_result_from_string(value);
2594 log_debug("Failed to parse reload result value %s", value);
2595 else if (f != SERVICE_SUCCESS)
2596 s->reload_result = f;
2598 } else if (streq(key, "control-pid")) {
2601 if (parse_pid(value, &pid) < 0)
2602 log_debug("Failed to parse control-pid value %s", value);
2604 s->control_pid = pid;
2605 } else if (streq(key, "main-pid")) {
2608 if (parse_pid(value, &pid) < 0)
2609 log_debug("Failed to parse main-pid value %s", value);
2611 service_set_main_pid(s, (pid_t) pid);
2612 } else if (streq(key, "main-pid-known")) {
2615 if ((b = parse_boolean(value)) < 0)
2616 log_debug("Failed to parse main-pid-known value %s", value);
2618 s->main_pid_known = b;
2619 } else if (streq(key, "status-text")) {
2622 if ((t = strdup(value))) {
2623 free(s->status_text);
2627 } else if (streq(key, "control-command")) {
2628 ServiceExecCommand id;
2630 if ((id = service_exec_command_from_string(value)) < 0)
2631 log_debug("Failed to parse exec-command value %s", value);
2633 s->control_command_id = id;
2634 s->control_command = s->exec_command[id];
2636 } else if (streq(key, "socket-fd")) {
2639 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2640 log_debug("Failed to parse socket-fd value %s", value);
2643 if (s->socket_fd >= 0)
2644 close_nointr_nofail(s->socket_fd);
2645 s->socket_fd = fdset_remove(fds, fd);
2647 } else if (streq(key, "main-exec-status-pid")) {
2650 if (parse_pid(value, &pid) < 0)
2651 log_debug("Failed to parse main-exec-status-pid value %s", value);
2653 s->main_exec_status.pid = pid;
2654 } else if (streq(key, "main-exec-status-code")) {
2657 if (safe_atoi(value, &i) < 0)
2658 log_debug("Failed to parse main-exec-status-code value %s", value);
2660 s->main_exec_status.code = i;
2661 } else if (streq(key, "main-exec-status-status")) {
2664 if (safe_atoi(value, &i) < 0)
2665 log_debug("Failed to parse main-exec-status-status value %s", value);
2667 s->main_exec_status.status = i;
2668 } else if (streq(key, "main-exec-status-start"))
2669 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2670 else if (streq(key, "main-exec-status-exit"))
2671 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2672 else if (streq(key, "watchdog-timestamp"))
2673 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2675 log_debug("Unknown serialization key '%s'", key);
2680 static UnitActiveState service_active_state(Unit *u) {
2683 return state_translation_table[SERVICE(u)->state];
2686 static const char *service_sub_state_to_string(Unit *u) {
2689 return service_state_to_string(SERVICE(u)->state);
2692 static bool service_check_gc(Unit *u) {
2693 Service *s = SERVICE(u);
2697 /* Never clean up services that still have a process around,
2698 * even if the service is formally dead. */
2699 if (cgroup_good(s) > 0 ||
2700 main_pid_good(s) > 0 ||
2701 control_pid_good(s) > 0)
2704 #ifdef HAVE_SYSV_COMPAT
2712 static bool service_check_snapshot(Unit *u) {
2713 Service *s = SERVICE(u);
2717 return !s->got_socket_fd;
2720 static int service_retry_pid_file(Service *s) {
2723 assert(s->pid_file);
2724 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2726 r = service_load_pid_file(s, false);
2730 service_unwatch_pid_file(s);
2732 service_enter_running(s, SERVICE_SUCCESS);
2736 static int service_watch_pid_file(Service *s) {
2739 log_debug("Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
2740 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2744 /* the pidfile might have appeared just before we set the watch */
2745 service_retry_pid_file(s);
2749 log_error("Failed to set a watch for %s's PID file %s: %s",
2750 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2751 service_unwatch_pid_file(s);
2755 static int service_demand_pid_file(Service *s) {
2758 assert(s->pid_file);
2759 assert(!s->pid_file_pathspec);
2761 ps = new0(PathSpec, 1);
2765 ps->path = strdup(s->pid_file);
2771 path_kill_slashes(ps->path);
2773 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2774 * keep their PID file open all the time. */
2775 ps->type = PATH_MODIFIED;
2776 ps->inotify_fd = -1;
2778 s->pid_file_pathspec = ps;
2780 return service_watch_pid_file(s);
2783 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2784 Service *s = SERVICE(u);
2788 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2789 assert(s->pid_file_pathspec);
2790 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2792 log_debug("inotify event for %s", u->id);
2794 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2797 if (service_retry_pid_file(s) == 0)
2800 if (service_watch_pid_file(s) < 0)
2805 service_unwatch_pid_file(s);
2806 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2809 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2810 Service *s = SERVICE(u);
2816 if (UNIT(s)->fragment_path ? is_clean_exit(code, status) : is_clean_exit_lsb(code, status))
2817 f = SERVICE_SUCCESS;
2818 else if (code == CLD_EXITED)
2819 f = SERVICE_FAILURE_EXIT_CODE;
2820 else if (code == CLD_KILLED)
2821 f = SERVICE_FAILURE_SIGNAL;
2822 else if (code == CLD_DUMPED)
2823 f = SERVICE_FAILURE_CORE_DUMP;
2825 assert_not_reached("Unknown code");
2827 if (s->main_pid == pid) {
2828 /* Forking services may occasionally move to a new PID.
2829 * As long as they update the PID file before exiting the old
2830 * PID, they're fine. */
2831 if (service_load_pid_file(s, false) == 0)
2835 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2837 /* If this is not a forking service than the main
2838 * process got started and hence we copy the exit
2839 * status so that it is recorded both as main and as
2840 * control process exit status */
2841 if (s->main_command) {
2842 s->main_command->exec_status = s->main_exec_status;
2844 if (s->main_command->ignore)
2845 f = SERVICE_SUCCESS;
2848 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2849 "%s: main process exited, code=%s, status=%i", u->id, sigchld_code_to_string(code), status);
2851 if (f != SERVICE_SUCCESS)
2854 if (s->main_command &&
2855 s->main_command->command_next &&
2856 f == SERVICE_SUCCESS) {
2858 /* There is another command to *
2859 * execute, so let's do that. */
2861 log_debug("%s running next main command for state %s", u->id, service_state_to_string(s->state));
2862 service_run_next_main(s);
2866 /* The service exited, so the service is officially
2868 s->main_command = NULL;
2872 case SERVICE_START_POST:
2873 case SERVICE_RELOAD:
2875 /* Need to wait until the operation is
2880 if (s->type == SERVICE_ONESHOT) {
2881 /* This was our main goal, so let's go on */
2882 if (f == SERVICE_SUCCESS)
2883 service_enter_start_post(s);
2885 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2888 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2893 case SERVICE_RUNNING:
2894 service_enter_running(s, f);
2897 case SERVICE_STOP_SIGTERM:
2898 case SERVICE_STOP_SIGKILL:
2900 if (!control_pid_good(s))
2901 service_enter_stop_post(s, f);
2903 /* If there is still a control process, wait for that first */
2907 assert_not_reached("Uh, main process died at wrong time.");
2911 } else if (s->control_pid == pid) {
2915 if (s->control_command) {
2916 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2918 if (s->control_command->ignore)
2919 f = SERVICE_SUCCESS;
2922 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2923 "%s: control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
2925 if (f != SERVICE_SUCCESS)
2928 /* Immediately get rid of the cgroup, so that the
2929 * kernel doesn't delay the cgroup empty messages for
2930 * the service cgroup any longer than necessary */
2931 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2933 if (s->control_command &&
2934 s->control_command->command_next &&
2935 f == SERVICE_SUCCESS) {
2937 /* There is another command to *
2938 * execute, so let's do that. */
2940 log_debug("%s running next control command for state %s", u->id, service_state_to_string(s->state));
2941 service_run_next_control(s);
2944 /* No further commands for this step, so let's
2945 * figure out what to do next */
2947 s->control_command = NULL;
2948 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2950 log_debug("%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
2954 case SERVICE_START_PRE:
2955 if (f == SERVICE_SUCCESS)
2956 service_enter_start(s);
2958 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2962 assert(s->type == SERVICE_FORKING);
2964 if (f != SERVICE_SUCCESS) {
2965 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2970 bool has_start_post;
2973 /* Let's try to load the pid file here if we can.
2974 * The PID file might actually be created by a START_POST
2975 * script. In that case don't worry if the loading fails. */
2977 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2978 r = service_load_pid_file(s, !has_start_post);
2979 if (!has_start_post && r < 0) {
2980 r = service_demand_pid_file(s);
2981 if (r < 0 || !cgroup_good(s))
2982 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2986 service_search_main_pid(s);
2988 service_enter_start_post(s);
2991 case SERVICE_START_POST:
2992 if (f != SERVICE_SUCCESS) {
2993 service_enter_stop(s, f);
3000 r = service_load_pid_file(s, true);
3002 r = service_demand_pid_file(s);
3003 if (r < 0 || !cgroup_good(s))
3004 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3008 service_search_main_pid(s);
3010 service_enter_running(s, SERVICE_SUCCESS);
3013 case SERVICE_RELOAD:
3014 if (f == SERVICE_SUCCESS) {
3015 service_load_pid_file(s, true);
3016 service_search_main_pid(s);
3019 s->reload_result = f;
3020 service_enter_running(s, SERVICE_SUCCESS);
3024 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3027 case SERVICE_STOP_SIGTERM:
3028 case SERVICE_STOP_SIGKILL:
3029 if (main_pid_good(s) <= 0)
3030 service_enter_stop_post(s, f);
3032 /* If there is still a service
3033 * process around, wait until
3034 * that one quit, too */
3037 case SERVICE_STOP_POST:
3038 case SERVICE_FINAL_SIGTERM:
3039 case SERVICE_FINAL_SIGKILL:
3040 service_enter_dead(s, f, true);
3044 assert_not_reached("Uh, control process died at wrong time.");
3049 /* Notify clients about changed exit status */
3050 unit_add_to_dbus_queue(u);
3053 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3054 Service *s = SERVICE(u);
3057 assert(elapsed == 1);
3059 if (w == &s->watchdog_watch) {
3060 service_handle_watchdog(s);
3064 assert(w == &s->timer_watch);
3068 case SERVICE_START_PRE:
3070 log_warning("%s operation timed out. Terminating.", u->id);
3071 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3074 case SERVICE_START_POST:
3075 log_warning("%s operation timed out. Stopping.", u->id);
3076 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3079 case SERVICE_RELOAD:
3080 log_warning("%s operation timed out. Stopping.", u->id);
3081 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3082 service_enter_running(s, SERVICE_SUCCESS);
3086 log_warning("%s stopping timed out. Terminating.", u->id);
3087 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3090 case SERVICE_STOP_SIGTERM:
3091 if (s->exec_context.send_sigkill) {
3092 log_warning("%s stopping timed out. Killing.", u->id);
3093 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3095 log_warning("%s stopping timed out. Skipping SIGKILL.", u->id);
3096 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3101 case SERVICE_STOP_SIGKILL:
3102 /* Uh, we sent a SIGKILL and it is still not gone?
3103 * Must be something we cannot kill, so let's just be
3104 * weirded out and continue */
3106 log_warning("%s still around after SIGKILL. Ignoring.", u->id);
3107 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3110 case SERVICE_STOP_POST:
3111 log_warning("%s stopping timed out (2). Terminating.", u->id);
3112 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3115 case SERVICE_FINAL_SIGTERM:
3116 if (s->exec_context.send_sigkill) {
3117 log_warning("%s stopping timed out (2). Killing.", u->id);
3118 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3120 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->id);
3121 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3126 case SERVICE_FINAL_SIGKILL:
3127 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->id);
3128 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3131 case SERVICE_AUTO_RESTART:
3132 log_info("%s holdoff time over, scheduling restart.", u->id);
3133 service_enter_restart(s);
3137 assert_not_reached("Timeout at wrong time.");
3141 static void service_cgroup_notify_event(Unit *u) {
3142 Service *s = SERVICE(u);
3146 log_debug("%s: cgroup is empty", u->id);
3150 /* Waiting for SIGCHLD is usually more interesting,
3151 * because it includes return codes/signals. Which is
3152 * why we ignore the cgroup events for most cases,
3153 * except when we don't know pid which to expect the
3157 case SERVICE_START_POST:
3158 /* If we were hoping for the daemon to write its PID file,
3159 * we can give up now. */
3160 if (s->pid_file_pathspec) {
3161 log_warning("%s never wrote its PID file. Failing.", UNIT(s)->id);
3162 service_unwatch_pid_file(s);
3163 if (s->state == SERVICE_START)
3164 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3166 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3170 case SERVICE_RUNNING:
3171 /* service_enter_running() will figure out what to do */
3172 service_enter_running(s, SERVICE_SUCCESS);
3175 case SERVICE_STOP_SIGTERM:
3176 case SERVICE_STOP_SIGKILL:
3178 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3179 service_enter_stop_post(s, SERVICE_SUCCESS);
3183 case SERVICE_FINAL_SIGTERM:
3184 case SERVICE_FINAL_SIGKILL:
3185 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3186 service_enter_dead(s, SERVICE_SUCCESS, SERVICE_SUCCESS);
3195 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3196 Service *s = SERVICE(u);
3201 if (s->notify_access == NOTIFY_NONE) {
3202 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3203 u->id, (unsigned long) pid);
3207 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3208 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3209 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3213 log_debug("%s: Got message", u->id);
3215 /* Interpret MAINPID= */
3216 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3217 (s->state == SERVICE_START ||
3218 s->state == SERVICE_START_POST ||
3219 s->state == SERVICE_RUNNING ||
3220 s->state == SERVICE_RELOAD)) {
3222 if (parse_pid(e + 8, &pid) < 0)
3223 log_warning("Failed to parse notification message %s", e);
3225 log_debug("%s: got %s", u->id, e);
3226 service_set_main_pid(s, pid);
3230 /* Interpret READY= */
3231 if (s->type == SERVICE_NOTIFY &&
3232 s->state == SERVICE_START &&
3233 strv_find(tags, "READY=1")) {
3234 log_debug("%s: got READY=1", u->id);
3236 service_enter_start_post(s);
3239 /* Interpret STATUS= */
3240 e = strv_find_prefix(tags, "STATUS=");
3246 if (!utf8_is_valid(e+7)) {
3247 log_warning("Status message in notification is not UTF-8 clean.");
3253 log_error("Failed to allocate string.");
3257 log_debug("%s: got %s", u->id, e);
3259 free(s->status_text);
3262 free(s->status_text);
3263 s->status_text = NULL;
3267 if (strv_find(tags, "WATCHDOG=1")) {
3268 log_debug("%s: got WATCHDOG=1", u->id);
3269 service_reset_watchdog(s);
3272 /* Notify clients about changed status or main pid */
3273 unit_add_to_dbus_queue(u);
3276 #ifdef HAVE_SYSV_COMPAT
3279 static void sysv_facility_in_insserv_conf(Manager *mgr) {
3283 if (!(f = fopen("/etc/insserv.conf", "re"))) {
3284 r = errno == ENOENT ? 0 : -errno;
3289 char l[LINE_MAX], *t;
3290 char **parsed = NULL;
3292 if (!fgets(l, sizeof(l), f)) {
3297 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r));
3302 if (*t != '$' && *t != '<')
3305 parsed = strv_split(t,WHITESPACE);
3306 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3307 if (parsed && !startswith_no_case (parsed[0], "<interactive>")) {
3310 if (sysv_translate_facility(parsed[0], NULL, &facility) < 0)
3312 if ((u = manager_get_unit(mgr, facility)) && (u->type == UNIT_TARGET)) {
3314 char *dep = NULL, *name, **j;
3316 STRV_FOREACH (j, parsed+1) {
3325 if (sysv_translate_facility(name, NULL, &dep) < 0)
3328 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, e, dep, NULL, true);
3343 static int service_enumerate(Manager *m) {
3347 char *path = NULL, *fpath = NULL, *name = NULL;
3348 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3355 if (m->running_as != MANAGER_SYSTEM)
3358 zero(runlevel_services);
3360 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3361 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3365 path = join(*p, "/", rcnd_table[i].path, NULL);
3374 if (!(d = opendir(path))) {
3375 if (errno != ENOENT)
3376 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3381 while ((de = readdir(d))) {
3384 if (ignore_file(de->d_name))
3387 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3390 if (strlen(de->d_name) < 4)
3393 a = undecchar(de->d_name[1]);
3394 b = undecchar(de->d_name[2]);
3400 fpath = join(path, "/", de->d_name, NULL);
3406 if (access(fpath, X_OK) < 0) {
3408 if (errno != ENOENT)
3409 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3415 if (!(name = sysv_translate_name(de->d_name + 3))) {
3420 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
3421 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3425 if (de->d_name[0] == 'S') {
3427 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
3428 SERVICE(service)->sysv_start_priority_from_rcnd =
3429 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3431 SERVICE(service)->sysv_enabled = true;
3434 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3437 if ((r = set_put(runlevel_services[i], service)) < 0)
3440 } else if (de->d_name[0] == 'K' &&
3441 (rcnd_table[i].type == RUNLEVEL_DOWN ||
3442 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
3444 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3447 if ((r = set_put(shutdown_services, service)) < 0)
3453 /* Now we loaded all stubs and are aware of the lowest
3454 start-up priority for all services, not let's actually load
3455 the services, this will also tell us which services are
3456 actually native now */
3457 manager_dispatch_load_queue(m);
3459 /* If this is a native service, rely on native ways to pull in
3460 * a service, don't pull it in via sysv rcN.d links. */
3461 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3462 SET_FOREACH(service, runlevel_services[i], j) {
3463 service = unit_follow_merge(service);
3465 if (service->fragment_path)
3468 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3472 /* We honour K links only for halt/reboot. For the normal
3473 * runlevels we assume the stop jobs will be implicitly added
3474 * by the core logic. Also, we don't really distinguish here
3475 * between the runlevels 0 and 6 and just add them to the
3476 * special shutdown target. On SUSE the boot.d/ runlevel is
3477 * also used for shutdown, so we add links for that too to the
3478 * shutdown target.*/
3479 SET_FOREACH(service, shutdown_services, j) {
3480 service = unit_follow_merge(service);
3482 if (service->fragment_path)
3485 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3492 sysv_facility_in_insserv_conf (m);
3500 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3501 set_free(runlevel_services[i]);
3502 set_free(shutdown_services);
3511 static void service_bus_name_owner_change(
3514 const char *old_owner,
3515 const char *new_owner) {
3517 Service *s = SERVICE(u);
3522 assert(streq(s->bus_name, name));
3523 assert(old_owner || new_owner);
3525 if (old_owner && new_owner)
3526 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
3528 log_debug("%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
3530 log_debug("%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
3532 s->bus_name_good = !!new_owner;
3534 if (s->type == SERVICE_DBUS) {
3536 /* service_enter_running() will figure out what to
3538 if (s->state == SERVICE_RUNNING)
3539 service_enter_running(s, SERVICE_SUCCESS);
3540 else if (s->state == SERVICE_START && new_owner)
3541 service_enter_start_post(s);
3543 } else if (new_owner &&
3545 (s->state == SERVICE_START ||
3546 s->state == SERVICE_START_POST ||
3547 s->state == SERVICE_RUNNING ||
3548 s->state == SERVICE_RELOAD)) {
3550 /* Try to acquire PID from bus service */
3551 log_debug("Trying to acquire PID from D-Bus name...");
3553 bus_query_pid(u->manager, name);
3557 static void service_bus_query_pid_done(
3562 Service *s = SERVICE(u);
3567 log_debug("%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3569 if (s->main_pid <= 0 &&
3570 (s->state == SERVICE_START ||
3571 s->state == SERVICE_START_POST ||
3572 s->state == SERVICE_RUNNING ||
3573 s->state == SERVICE_RELOAD))
3574 service_set_main_pid(s, pid);
3577 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3582 /* This is called by the socket code when instantiating a new
3583 * service for a stream socket and the socket needs to be
3586 if (UNIT(s)->load_state != UNIT_LOADED)
3589 if (s->socket_fd >= 0)
3592 if (s->state != SERVICE_DEAD)
3596 s->got_socket_fd = true;
3598 unit_ref_set(&s->accept_socket, UNIT(sock));
3600 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3603 static void service_reset_failed(Unit *u) {
3604 Service *s = SERVICE(u);
3608 if (s->state == SERVICE_FAILED)
3609 service_set_state(s, SERVICE_DEAD);
3611 s->result = SERVICE_SUCCESS;
3612 s->reload_result = SERVICE_SUCCESS;
3615 static bool service_need_daemon_reload(Unit *u) {
3616 Service *s = SERVICE(u);
3620 #ifdef HAVE_SYSV_COMPAT
3625 if (stat(s->sysv_path, &st) < 0)
3626 /* What, cannot access this anymore? */
3629 if (s->sysv_mtime > 0 &&
3630 timespec_load(&st.st_mtim) != s->sysv_mtime)
3638 static int service_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
3639 Service *s = SERVICE(u);
3641 Set *pid_set = NULL;
3645 if (s->main_pid <= 0 && who == KILL_MAIN) {
3646 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3650 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3651 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3655 if (who == KILL_CONTROL || who == KILL_ALL)
3656 if (s->control_pid > 0)
3657 if (kill(s->control_pid, signo) < 0)
3660 if (who == KILL_MAIN || who == KILL_ALL)
3661 if (s->main_pid > 0)
3662 if (kill(s->main_pid, signo) < 0)
3665 if (who == KILL_ALL && mode == KILL_CONTROL_GROUP) {
3668 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
3671 /* Exclude the control/main pid from being killed via the cgroup */
3672 if (s->control_pid > 0)
3673 if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
3678 if (s->main_pid > 0)
3679 if ((q = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0) {
3683 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3685 if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3696 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3697 [SERVICE_DEAD] = "dead",
3698 [SERVICE_START_PRE] = "start-pre",
3699 [SERVICE_START] = "start",
3700 [SERVICE_START_POST] = "start-post",
3701 [SERVICE_RUNNING] = "running",
3702 [SERVICE_EXITED] = "exited",
3703 [SERVICE_RELOAD] = "reload",
3704 [SERVICE_STOP] = "stop",
3705 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3706 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3707 [SERVICE_STOP_POST] = "stop-post",
3708 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3709 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3710 [SERVICE_FAILED] = "failed",
3711 [SERVICE_AUTO_RESTART] = "auto-restart",
3714 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3716 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3717 [SERVICE_RESTART_NO] = "no",
3718 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3719 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3720 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3721 [SERVICE_RESTART_ALWAYS] = "always"
3724 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3726 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3727 [SERVICE_SIMPLE] = "simple",
3728 [SERVICE_FORKING] = "forking",
3729 [SERVICE_ONESHOT] = "oneshot",
3730 [SERVICE_DBUS] = "dbus",
3731 [SERVICE_NOTIFY] = "notify",
3732 [SERVICE_IDLE] = "idle"
3735 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3737 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3738 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3739 [SERVICE_EXEC_START] = "ExecStart",
3740 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3741 [SERVICE_EXEC_RELOAD] = "ExecReload",
3742 [SERVICE_EXEC_STOP] = "ExecStop",
3743 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3746 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3748 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3749 [NOTIFY_NONE] = "none",
3750 [NOTIFY_MAIN] = "main",
3751 [NOTIFY_ALL] = "all"
3754 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3756 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3757 [SERVICE_SUCCESS] = "success",
3758 [SERVICE_FAILURE_RESOURCES] = "resources",
3759 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3760 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3761 [SERVICE_FAILURE_SIGNAL] = "signal",
3762 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3763 [SERVICE_FAILURE_WATCHDOG] = "watchdog"
3766 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3768 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3769 [SERVICE_START_LIMIT_NONE] = "none",
3770 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3771 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3772 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3774 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3776 const UnitVTable service_vtable = {
3777 .suffix = ".service",
3778 .object_size = sizeof(Service),
3784 .init = service_init,
3785 .done = service_done,
3786 .load = service_load,
3788 .coldplug = service_coldplug,
3790 .dump = service_dump,
3792 .start = service_start,
3793 .stop = service_stop,
3794 .reload = service_reload,
3796 .can_reload = service_can_reload,
3798 .kill = service_kill,
3800 .serialize = service_serialize,
3801 .deserialize_item = service_deserialize_item,
3803 .active_state = service_active_state,
3804 .sub_state_to_string = service_sub_state_to_string,
3806 .check_gc = service_check_gc,
3807 .check_snapshot = service_check_snapshot,
3809 .sigchld_event = service_sigchld_event,
3810 .timer_event = service_timer_event,
3811 .fd_event = service_fd_event,
3813 .reset_failed = service_reset_failed,
3815 .need_daemon_reload = service_need_daemon_reload,
3817 .cgroup_notify_empty = service_cgroup_notify_event,
3818 .notify_message = service_notify_message,
3820 .bus_name_owner_change = service_bus_name_owner_change,
3821 .bus_query_pid_done = service_bus_query_pid_done,
3823 .bus_interface = "org.freedesktop.systemd1.Service",
3824 .bus_message_handler = bus_service_message_handler,
3825 .bus_invalidating_properties = bus_service_invalidating_properties,
3827 #ifdef HAVE_SYSV_COMPAT
3828 .enumerate = service_enumerate,
3830 .status_message_formats = {
3831 .starting_stopping = {
3832 [0] = "Starting %s...",
3833 [1] = "Stopping %s...",
3835 .finished_start_job = {
3836 [JOB_DONE] = "Started %s.",
3837 [JOB_FAILED] = "Failed to start %s.",
3838 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3839 [JOB_TIMEOUT] = "Timed out starting %s.",
3841 .finished_stop_job = {
3842 [JOB_DONE] = "Stopped %s.",
3843 [JOB_FAILED] = "Stopped (with error) %s.",
3844 [JOB_TIMEOUT] = "Timed out stopping %s.",