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;
119 s->watchdog_watch.type = WATCH_INVALID;
121 s->timer_watch.type = WATCH_INVALID;
122 #ifdef HAVE_SYSV_COMPAT
123 s->sysv_start_priority = -1;
124 s->sysv_start_priority_from_rcnd = -1;
127 s->guess_main_pid = true;
129 exec_context_init(&s->exec_context);
131 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
133 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
136 static void service_unwatch_control_pid(Service *s) {
139 if (s->control_pid <= 0)
142 unit_unwatch_pid(UNIT(s), s->control_pid);
146 static void service_unwatch_main_pid(Service *s) {
149 if (s->main_pid <= 0)
152 unit_unwatch_pid(UNIT(s), s->main_pid);
156 static void service_unwatch_pid_file(Service *s) {
157 if (!s->pid_file_pathspec)
160 log_debug("Stopping watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
161 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
162 path_spec_done(s->pid_file_pathspec);
163 free(s->pid_file_pathspec);
164 s->pid_file_pathspec = NULL;
167 static int service_set_main_pid(Service *s, pid_t pid) {
179 s->main_pid_known = true;
181 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
182 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
183 UNIT(s)->id, (unsigned long) pid);
185 s->main_pid_alien = true;
187 s->main_pid_alien = false;
189 exec_status_start(&s->main_exec_status, pid);
194 static void service_close_socket_fd(Service *s) {
197 if (s->socket_fd < 0)
200 close_nointr_nofail(s->socket_fd);
204 static void service_connection_unref(Service *s) {
207 if (!UNIT_DEREF(s->accept_socket))
210 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
211 unit_ref_unset(&s->accept_socket);
214 static void service_stop_watchdog(Service *s) {
217 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
218 s->watchdog_timestamp.realtime = 0;
219 s->watchdog_timestamp.monotonic = 0;
222 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart);
224 static void service_handle_watchdog(Service *s) {
230 if (s->watchdog_usec == 0)
233 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
234 if (offset >= s->watchdog_usec) {
235 log_error("%s watchdog timeout!", UNIT(s)->id);
236 service_enter_dead(s, SERVICE_FAILURE_WATCHDOG, true);
240 r = unit_watch_timer(UNIT(s), s->watchdog_usec - offset, &s->watchdog_watch);
242 log_warning("%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
245 static void service_reset_watchdog(Service *s) {
248 dual_timestamp_get(&s->watchdog_timestamp);
249 service_handle_watchdog(s);
252 static void service_done(Unit *u) {
253 Service *s = SERVICE(u);
260 #ifdef HAVE_SYSV_COMPAT
264 free(s->sysv_runlevels);
265 s->sysv_runlevels = NULL;
268 free(s->status_text);
269 s->status_text = NULL;
271 exec_context_done(&s->exec_context);
272 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
273 s->control_command = NULL;
274 s->main_command = NULL;
276 /* This will leak a process, but at least no memory or any of
278 service_unwatch_main_pid(s);
279 service_unwatch_control_pid(s);
280 service_unwatch_pid_file(s);
283 unit_unwatch_bus_name(u, s->bus_name);
288 service_close_socket_fd(s);
289 service_connection_unref(s);
291 unit_ref_unset(&s->accept_socket);
293 service_stop_watchdog(s);
295 unit_unwatch_timer(u, &s->timer_watch);
298 #ifdef HAVE_SYSV_COMPAT
299 static char *sysv_translate_name(const char *name) {
302 if (!(r = new(char, strlen(name) + sizeof(".service"))))
305 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
306 if (endswith(name, ".sh"))
307 /* Drop Debian-style .sh suffix */
308 strcpy(stpcpy(r, name) - 3, ".service");
311 if (startswith(name, "boot."))
312 /* Drop SuSE-style boot. prefix */
313 strcpy(stpcpy(r, name + 5), ".service");
315 #ifdef TARGET_FRUGALWARE
316 if (startswith(name, "rc."))
317 /* Drop Frugalware-style rc. prefix */
318 strcpy(stpcpy(r, name + 3), ".service");
321 /* Normal init scripts */
322 strcpy(stpcpy(r, name), ".service");
327 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
329 /* We silently ignore the $ prefix here. According to the LSB
330 * spec it simply indicates whether something is a
331 * standardized name or a distribution-specific one. Since we
332 * just follow what already exists and do not introduce new
333 * uses or names we don't care who introduced a new name. */
335 static const char * const table[] = {
336 /* LSB defined facilities */
337 "local_fs", SPECIAL_LOCAL_FS_TARGET,
338 #if defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
340 /* Due to unfortunate name selection in Mandriva,
341 * $network is provided by network-up which is ordered
342 * after network which actually starts interfaces.
343 * To break the loop, just ignore it */
344 "network", SPECIAL_NETWORK_TARGET,
346 "named", SPECIAL_NSS_LOOKUP_TARGET,
347 "portmap", SPECIAL_RPCBIND_TARGET,
348 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
349 "syslog", SPECIAL_SYSLOG_TARGET,
350 "time", SPECIAL_TIME_SYNC_TARGET,
352 /* common extensions */
353 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
354 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
357 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
358 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
362 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
363 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
364 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
368 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
379 n = *name == '$' ? name + 1 : name;
381 for (i = 0; i < ELEMENTSOF(table); i += 2) {
383 if (!streq(table[i], n))
389 if (!(r = strdup(table[i+1])))
395 /* If we don't know this name, fallback heuristics to figure
396 * out whether something is a target or a service alias. */
399 if (!unit_prefix_is_valid(n))
402 /* Facilities starting with $ are most likely targets */
403 r = unit_name_build(n, NULL, ".target");
404 } else if (filename && streq(name, filename))
405 /* Names equaling the file name of the services are redundant */
408 /* Everything else we assume to be normal service names */
409 r = sysv_translate_name(n);
420 static int sysv_fix_order(Service *s) {
426 if (s->sysv_start_priority < 0)
429 /* For each pair of services where at least one lacks a LSB
430 * header, we use the start priority value to order things. */
432 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
435 bool special_s, special_t;
442 if (UNIT(t)->load_state != UNIT_LOADED)
445 if (t->sysv_start_priority < 0)
448 /* If both units have modern headers we don't care
449 * about the priorities */
450 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
451 (UNIT(t)->fragment_path || t->sysv_has_lsb))
454 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
455 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
457 if (special_t && !special_s)
459 else if (special_s && !special_t)
461 else if (t->sysv_start_priority < s->sysv_start_priority)
463 else if (t->sysv_start_priority > s->sysv_start_priority)
468 /* FIXME: Maybe we should compare the name here lexicographically? */
470 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
477 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
480 if (!(c = new0(ExecCommand, 1)))
483 if (!(c->path = strdup(path))) {
488 if (!(c->argv = strv_new(path, arg1, NULL))) {
497 static int sysv_exec_commands(Service *s) {
501 assert(s->sysv_path);
503 if (!(c = exec_command_new(s->sysv_path, "start")))
505 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
507 if (!(c = exec_command_new(s->sysv_path, "stop")))
509 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
511 if (!(c = exec_command_new(s->sysv_path, "reload")))
513 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
518 static int service_load_sysv_path(Service *s, const char *path) {
529 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
537 if (!(f = fopen(path, "re"))) {
538 r = errno == ENOENT ? 0 : -errno;
543 if (fstat(fileno(f), &st) < 0) {
549 if (!(s->sysv_path = strdup(path))) {
554 s->sysv_mtime = timespec_load(&st.st_mtim);
556 if (null_or_empty(&st)) {
557 u->load_state = UNIT_MASKED;
563 char l[LINE_MAX], *t;
565 if (!fgets(l, sizeof(l), f)) {
570 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
580 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
582 s->sysv_has_lsb = true;
586 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
592 t += strspn(t, WHITESPACE);
594 if (state == NORMAL) {
596 /* Try to parse Red Hat style chkconfig headers */
598 if (startswith_no_case(t, "chkconfig:")) {
600 char runlevels[16], *k;
604 if (sscanf(t+10, "%15s %i %*i",
606 &start_priority) != 2) {
608 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
612 /* A start priority gathered from the
613 * symlink farms is preferred over the
614 * data from the LSB header. */
615 if (start_priority < 0 || start_priority > 99)
616 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
618 s->sysv_start_priority = start_priority;
620 char_array_0(runlevels);
621 k = delete_chars(runlevels, WHITESPACE "-");
626 if (!(d = strdup(k))) {
631 free(s->sysv_runlevels);
632 s->sysv_runlevels = d;
635 } else if (startswith_no_case(t, "description:")) {
637 size_t k = strlen(t);
641 if (t[k-1] == '\\') {
646 if ((j = strstrip(t+12)) && *j) {
647 if (!(d = strdup(j))) {
654 free(chkconfig_description);
655 chkconfig_description = d;
657 } else if (startswith_no_case(t, "pidfile:")) {
664 if (!path_is_absolute(fn)) {
665 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
669 if (!(fn = strdup(fn))) {
678 } else if (state == DESCRIPTION) {
680 /* Try to parse Red Hat style description
683 size_t k = strlen(t);
691 if ((j = strstrip(t)) && *j) {
694 if (chkconfig_description)
695 d = join(chkconfig_description, " ", j, NULL);
704 free(chkconfig_description);
705 chkconfig_description = d;
708 } else if (state == LSB || state == LSB_DESCRIPTION) {
710 if (startswith_no_case(t, "Provides:")) {
716 FOREACH_WORD_QUOTED(w, z, t+9, i) {
719 if (!(n = strndup(w, z))) {
724 r = sysv_translate_facility(n, file_name_from_path(path), &m);
733 if (unit_name_to_type(m) == UNIT_SERVICE)
734 r = unit_add_name(u, m);
741 * indication that the
743 * now available. This
746 * targets do NOT pull
749 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
752 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
757 } else if (startswith_no_case(t, "Required-Start:") ||
758 startswith_no_case(t, "Should-Start:") ||
759 startswith_no_case(t, "X-Start-Before:") ||
760 startswith_no_case(t, "X-Start-After:")) {
766 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
769 if (!(n = strndup(w, z))) {
774 r = sysv_translate_facility(n, file_name_from_path(path), &m);
777 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
787 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
790 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
794 } else if (startswith_no_case(t, "Default-Start:")) {
799 k = delete_chars(t+14, WHITESPACE "-");
802 if (!(d = strdup(k))) {
807 free(s->sysv_runlevels);
808 s->sysv_runlevels = d;
811 } else if (startswith_no_case(t, "Description:")) {
814 state = LSB_DESCRIPTION;
816 if ((j = strstrip(t+12)) && *j) {
817 if (!(d = strdup(j))) {
824 free(long_description);
825 long_description = d;
827 } else if (startswith_no_case(t, "Short-Description:")) {
832 if ((j = strstrip(t+18)) && *j) {
833 if (!(d = strdup(j))) {
840 free(short_description);
841 short_description = d;
843 } else if (state == LSB_DESCRIPTION) {
845 if (startswith(l, "#\t") || startswith(l, "# ")) {
848 if ((j = strstrip(t)) && *j) {
851 if (long_description)
852 d = join(long_description, " ", t, NULL);
861 free(long_description);
862 long_description = d;
871 if ((r = sysv_exec_commands(s)) < 0)
873 if (s->sysv_runlevels &&
874 chars_intersect(RUNLEVELS_BOOT, s->sysv_runlevels) &&
875 chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
876 /* Service has both boot and "up" runlevels
877 configured. Kill the "up" ones. */
878 delete_chars(s->sysv_runlevels, RUNLEVELS_UP);
881 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
882 /* If there a runlevels configured for this service
883 * but none of the standard ones, then we assume this
884 * is some special kind of service (which might be
885 * needed for early boot) and don't create any links
888 UNIT(s)->default_dependencies = false;
890 /* Don't timeout special services during boot (like fsck) */
893 s->timeout_usec = DEFAULT_SYSV_TIMEOUT_USEC;
895 /* Special setting for all SysV services */
896 s->type = SERVICE_FORKING;
897 s->remain_after_exit = !s->pid_file;
898 s->guess_main_pid = false;
899 s->restart = SERVICE_RESTART_NO;
900 s->exec_context.ignore_sigpipe = false;
902 if (UNIT(s)->manager->sysv_console)
903 s->exec_context.std_output = EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
905 s->exec_context.kill_mode = KILL_PROCESS;
907 /* We use the long description only if
908 * no short description is set. */
910 if (short_description)
911 description = short_description;
912 else if (chkconfig_description)
913 description = chkconfig_description;
914 else if (long_description)
915 description = long_description;
922 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
930 /* The priority that has been set in /etc/rcN.d/ hierarchies
931 * takes precedence over what is stored as default in the LSB
933 if (s->sysv_start_priority_from_rcnd >= 0)
934 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
936 u->load_state = UNIT_LOADED;
944 free(short_description);
945 free(long_description);
946 free(chkconfig_description);
951 static int service_load_sysv_name(Service *s, const char *name) {
957 /* For SysV services we strip the boot.*, rc.* and *.sh
958 * prefixes/suffixes. */
959 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
960 if (endswith(name, ".sh.service"))
965 if (startswith(name, "boot."))
969 #ifdef TARGET_FRUGALWARE
970 if (startswith(name, "rc."))
974 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
978 path = join(*p, "/", name, NULL);
982 assert(endswith(path, ".service"));
983 path[strlen(path)-8] = 0;
985 r = service_load_sysv_path(s, path);
987 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
988 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
989 /* Try Debian style *.sh source'able init scripts */
991 r = service_load_sysv_path(s, path);
997 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
998 /* Try SUSE style boot.* init scripts */
1000 path = join(*p, "/boot.", name, NULL);
1004 /* Drop .service suffix */
1005 path[strlen(path)-8] = 0;
1006 r = service_load_sysv_path(s, path);
1011 #ifdef TARGET_FRUGALWARE
1012 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1013 /* Try Frugalware style rc.* init scripts */
1015 path = join(*p, "/rc.", name, NULL);
1019 /* Drop .service suffix */
1020 path[strlen(path)-8] = 0;
1021 r = service_load_sysv_path(s, path);
1029 if ((UNIT(s)->load_state != UNIT_STUB))
1036 static int service_load_sysv(Service *s) {
1043 /* Load service data from SysV init scripts, preferably with
1044 * LSB headers ... */
1046 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1049 if ((t = UNIT(s)->id))
1050 if ((r = service_load_sysv_name(s, t)) < 0)
1053 if (UNIT(s)->load_state == UNIT_STUB)
1054 SET_FOREACH(t, UNIT(s)->names, i) {
1055 if (t == UNIT(s)->id)
1058 if ((r = service_load_sysv_name(s, t)) < 0)
1061 if (UNIT(s)->load_state != UNIT_STUB)
1069 static int fsck_fix_order(Service *s) {
1075 if (s->fsck_passno <= 0)
1078 /* For each pair of services where both have an fsck priority
1079 * we order things based on it. */
1081 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1090 if (UNIT(t)->load_state != UNIT_LOADED)
1093 if (t->fsck_passno <= 0)
1096 if (t->fsck_passno < s->fsck_passno)
1098 else if (t->fsck_passno > s->fsck_passno)
1103 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1110 static int service_verify(Service *s) {
1113 if (UNIT(s)->load_state != UNIT_LOADED)
1116 if (!s->exec_command[SERVICE_EXEC_START]) {
1117 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1121 if (s->type != SERVICE_ONESHOT &&
1122 s->exec_command[SERVICE_EXEC_START]->command_next) {
1123 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1127 if (s->type == SERVICE_ONESHOT &&
1128 s->exec_command[SERVICE_EXEC_RELOAD]) {
1129 log_error("%s has an ExecReload setting, which is not allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1133 if (s->type == SERVICE_DBUS && !s->bus_name) {
1134 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1138 if (s->bus_name && s->type != SERVICE_DBUS)
1139 log_warning("%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1141 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
1142 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1149 static int service_add_default_dependencies(Service *s) {
1154 /* Add a number of automatic dependencies useful for the
1155 * majority of services. */
1157 /* First, pull in base system */
1158 if (UNIT(s)->manager->running_as == MANAGER_SYSTEM) {
1160 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1163 } else if (UNIT(s)->manager->running_as == MANAGER_USER) {
1165 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1169 /* Second, activate normal shutdown */
1170 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1173 static void service_fix_output(Service *s) {
1176 /* If nothing has been explicitly configured, patch default
1177 * output in. If input is socket/tty we avoid this however,
1178 * since in that case we want output to default to the same
1179 * place as we read input from. */
1181 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1182 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1183 s->exec_context.std_input == EXEC_INPUT_NULL)
1184 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1186 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1187 s->exec_context.std_input == EXEC_INPUT_NULL)
1188 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1191 static int service_load(Unit *u) {
1193 Service *s = SERVICE(u);
1197 /* Load a .service file */
1198 if ((r = unit_load_fragment(u)) < 0)
1201 #ifdef HAVE_SYSV_COMPAT
1202 /* Load a classic init script as a fallback, if we couldn't find anything */
1203 if (u->load_state == UNIT_STUB)
1204 if ((r = service_load_sysv(s)) < 0)
1208 /* Still nothing found? Then let's give up */
1209 if (u->load_state == UNIT_STUB)
1212 /* We were able to load something, then let's add in the
1213 * dropin directories. */
1214 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1217 /* This is a new unit? Then let's add in some extras */
1218 if (u->load_state == UNIT_LOADED) {
1219 service_fix_output(s);
1221 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1224 if ((r = unit_add_default_cgroups(u)) < 0)
1227 #ifdef HAVE_SYSV_COMPAT
1228 if ((r = sysv_fix_order(s)) < 0)
1232 if ((r = fsck_fix_order(s)) < 0)
1236 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1239 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1240 s->notify_access = NOTIFY_MAIN;
1242 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1243 s->notify_access = NOTIFY_MAIN;
1245 if (s->type == SERVICE_DBUS || s->bus_name)
1246 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1249 if (UNIT(s)->default_dependencies)
1250 if ((r = service_add_default_dependencies(s)) < 0)
1254 return service_verify(s);
1257 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1259 ServiceExecCommand c;
1260 Service *s = SERVICE(u);
1261 const char *prefix2;
1266 p2 = strappend(prefix, "\t");
1267 prefix2 = p2 ? p2 : prefix;
1270 "%sService State: %s\n"
1272 "%sReload Result: %s\n"
1273 "%sPermissionsStartOnly: %s\n"
1274 "%sRootDirectoryStartOnly: %s\n"
1275 "%sRemainAfterExit: %s\n"
1276 "%sGuessMainPID: %s\n"
1279 "%sNotifyAccess: %s\n",
1280 prefix, service_state_to_string(s->state),
1281 prefix, service_result_to_string(s->result),
1282 prefix, service_result_to_string(s->reload_result),
1283 prefix, yes_no(s->permissions_start_only),
1284 prefix, yes_no(s->root_directory_start_only),
1285 prefix, yes_no(s->remain_after_exit),
1286 prefix, yes_no(s->guess_main_pid),
1287 prefix, service_type_to_string(s->type),
1288 prefix, service_restart_to_string(s->restart),
1289 prefix, notify_access_to_string(s->notify_access));
1291 if (s->control_pid > 0)
1293 "%sControl PID: %lu\n",
1294 prefix, (unsigned long) s->control_pid);
1296 if (s->main_pid > 0)
1299 "%sMain PID Known: %s\n"
1300 "%sMain PID Alien: %s\n",
1301 prefix, (unsigned long) s->main_pid,
1302 prefix, yes_no(s->main_pid_known),
1303 prefix, yes_no(s->main_pid_alien));
1308 prefix, s->pid_file);
1313 "%sBus Name Good: %s\n",
1314 prefix, s->bus_name,
1315 prefix, yes_no(s->bus_name_good));
1317 exec_context_dump(&s->exec_context, f, prefix);
1319 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1321 if (!s->exec_command[c])
1324 fprintf(f, "%s-> %s:\n",
1325 prefix, service_exec_command_to_string(c));
1327 exec_command_dump_list(s->exec_command[c], f, prefix2);
1330 #ifdef HAVE_SYSV_COMPAT
1333 "%sSysV Init Script Path: %s\n"
1334 "%sSysV Init Script has LSB Header: %s\n"
1335 "%sSysVEnabled: %s\n",
1336 prefix, s->sysv_path,
1337 prefix, yes_no(s->sysv_has_lsb),
1338 prefix, yes_no(s->sysv_enabled));
1340 if (s->sysv_start_priority >= 0)
1342 "%sSysVStartPriority: %i\n",
1343 prefix, s->sysv_start_priority);
1345 if (s->sysv_runlevels)
1346 fprintf(f, "%sSysVRunLevels: %s\n",
1347 prefix, s->sysv_runlevels);
1350 if (s->fsck_passno > 0)
1352 "%sFsckPassNo: %i\n",
1353 prefix, s->fsck_passno);
1356 fprintf(f, "%sStatus Text: %s\n",
1357 prefix, s->status_text);
1362 static int service_load_pid_file(Service *s, bool may_warn) {
1372 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1374 log_info("PID file %s not readable (yet?) after %s.",
1375 s->pid_file, service_state_to_string(s->state));
1379 r = parse_pid(k, &pid);
1385 if (kill(pid, 0) < 0 && errno != EPERM) {
1387 log_info("PID %lu read from file %s does not exist.",
1388 (unsigned long) pid, s->pid_file);
1392 if (s->main_pid_known) {
1393 if (pid == s->main_pid)
1396 log_debug("Main PID changing: %lu -> %lu",
1397 (unsigned long) s->main_pid, (unsigned long) pid);
1398 service_unwatch_main_pid(s);
1399 s->main_pid_known = false;
1401 log_debug("Main PID loaded: %lu", (unsigned long) pid);
1403 if ((r = service_set_main_pid(s, pid)) < 0)
1406 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1407 /* FIXME: we need to do something here */
1413 static int service_search_main_pid(Service *s) {
1419 /* If we know it anyway, don't ever fallback to unreliable
1421 if (s->main_pid_known)
1424 if (!s->guess_main_pid)
1427 assert(s->main_pid <= 0);
1429 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1432 log_debug("Main PID guessed: %lu", (unsigned long) pid);
1433 if ((r = service_set_main_pid(s, pid)) < 0)
1436 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1437 /* FIXME: we need to do something here */
1443 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1449 /* Notifies all our sockets when we die */
1451 if (s->socket_fd >= 0)
1454 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1455 if (u->type == UNIT_SOCKET)
1456 socket_notify_service_dead(SOCKET(u), failed_permanent);
1461 static void service_set_state(Service *s, ServiceState state) {
1462 ServiceState old_state;
1465 old_state = s->state;
1468 service_unwatch_pid_file(s);
1470 if (state != SERVICE_START_PRE &&
1471 state != SERVICE_START &&
1472 state != SERVICE_START_POST &&
1473 state != SERVICE_RELOAD &&
1474 state != SERVICE_STOP &&
1475 state != SERVICE_STOP_SIGTERM &&
1476 state != SERVICE_STOP_SIGKILL &&
1477 state != SERVICE_STOP_POST &&
1478 state != SERVICE_FINAL_SIGTERM &&
1479 state != SERVICE_FINAL_SIGKILL &&
1480 state != SERVICE_AUTO_RESTART)
1481 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1483 if (state != SERVICE_START &&
1484 state != SERVICE_START_POST &&
1485 state != SERVICE_RUNNING &&
1486 state != SERVICE_RELOAD &&
1487 state != SERVICE_STOP &&
1488 state != SERVICE_STOP_SIGTERM &&
1489 state != SERVICE_STOP_SIGKILL) {
1490 service_unwatch_main_pid(s);
1491 s->main_command = NULL;
1494 if (state != SERVICE_START_PRE &&
1495 state != SERVICE_START &&
1496 state != SERVICE_START_POST &&
1497 state != SERVICE_RELOAD &&
1498 state != SERVICE_STOP &&
1499 state != SERVICE_STOP_SIGTERM &&
1500 state != SERVICE_STOP_SIGKILL &&
1501 state != SERVICE_STOP_POST &&
1502 state != SERVICE_FINAL_SIGTERM &&
1503 state != SERVICE_FINAL_SIGKILL) {
1504 service_unwatch_control_pid(s);
1505 s->control_command = NULL;
1506 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1509 if (state == SERVICE_DEAD ||
1510 state == SERVICE_STOP ||
1511 state == SERVICE_STOP_SIGTERM ||
1512 state == SERVICE_STOP_SIGKILL ||
1513 state == SERVICE_STOP_POST ||
1514 state == SERVICE_FINAL_SIGTERM ||
1515 state == SERVICE_FINAL_SIGKILL ||
1516 state == SERVICE_FAILED ||
1517 state == SERVICE_AUTO_RESTART)
1518 service_notify_sockets_dead(s, false);
1520 if (state != SERVICE_START_PRE &&
1521 state != SERVICE_START &&
1522 state != SERVICE_START_POST &&
1523 state != SERVICE_RUNNING &&
1524 state != SERVICE_RELOAD &&
1525 state != SERVICE_STOP &&
1526 state != SERVICE_STOP_SIGTERM &&
1527 state != SERVICE_STOP_SIGKILL &&
1528 state != SERVICE_STOP_POST &&
1529 state != SERVICE_FINAL_SIGTERM &&
1530 state != SERVICE_FINAL_SIGKILL &&
1531 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1532 service_close_socket_fd(s);
1533 service_connection_unref(s);
1536 if (state == SERVICE_STOP)
1537 service_stop_watchdog(s);
1539 /* For the inactive states unit_notify() will trim the cgroup,
1540 * but for exit we have to do that ourselves... */
1541 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1542 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1544 if (old_state != state)
1545 log_debug("%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
1547 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], s->reload_result == SERVICE_SUCCESS);
1548 s->reload_result = SERVICE_SUCCESS;
1551 static int service_coldplug(Unit *u) {
1552 Service *s = SERVICE(u);
1556 assert(s->state == SERVICE_DEAD);
1558 if (s->deserialized_state != s->state) {
1560 if (s->deserialized_state == SERVICE_START_PRE ||
1561 s->deserialized_state == SERVICE_START ||
1562 s->deserialized_state == SERVICE_START_POST ||
1563 s->deserialized_state == SERVICE_RELOAD ||
1564 s->deserialized_state == SERVICE_STOP ||
1565 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1566 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1567 s->deserialized_state == SERVICE_STOP_POST ||
1568 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1569 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1570 s->deserialized_state == SERVICE_AUTO_RESTART) {
1572 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1575 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1577 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1582 if ((s->deserialized_state == SERVICE_START &&
1583 (s->type == SERVICE_FORKING ||
1584 s->type == SERVICE_DBUS ||
1585 s->type == SERVICE_ONESHOT ||
1586 s->type == SERVICE_NOTIFY)) ||
1587 s->deserialized_state == SERVICE_START_POST ||
1588 s->deserialized_state == SERVICE_RUNNING ||
1589 s->deserialized_state == SERVICE_RELOAD ||
1590 s->deserialized_state == SERVICE_STOP ||
1591 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1592 s->deserialized_state == SERVICE_STOP_SIGKILL)
1593 if (s->main_pid > 0)
1594 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1597 if (s->deserialized_state == SERVICE_START_PRE ||
1598 s->deserialized_state == SERVICE_START ||
1599 s->deserialized_state == SERVICE_START_POST ||
1600 s->deserialized_state == SERVICE_RELOAD ||
1601 s->deserialized_state == SERVICE_STOP ||
1602 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1603 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1604 s->deserialized_state == SERVICE_STOP_POST ||
1605 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1606 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1607 if (s->control_pid > 0)
1608 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1611 if (s->deserialized_state == SERVICE_START_POST ||
1612 s->deserialized_state == SERVICE_RUNNING)
1613 service_handle_watchdog(s);
1615 service_set_state(s, s->deserialized_state);
1620 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1624 unsigned rn_fds = 0;
1631 if (s->socket_fd >= 0)
1634 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1639 if (u->type != UNIT_SOCKET)
1644 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1656 if (!(t = new(int, rn_fds+cn_fds))) {
1662 memcpy(t, rfds, rn_fds * sizeof(int));
1663 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1668 rn_fds = rn_fds+cn_fds;
1683 static int service_spawn(
1688 bool apply_permissions,
1690 bool apply_tty_stdin,
1691 bool set_notify_socket,
1697 int *fds = NULL, *fdsbuf = NULL;
1698 unsigned n_fds = 0, n_env = 0;
1699 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1706 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1707 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1708 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1710 if (s->socket_fd >= 0) {
1711 fds = &s->socket_fd;
1714 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1721 if (timeout && s->timeout_usec) {
1722 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1725 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1727 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1732 if (!(our_env = new0(char*, 4))) {
1737 if (set_notify_socket)
1738 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1743 if (s->main_pid > 0)
1744 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1749 if (s->watchdog_usec > 0)
1750 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1755 if (!(final_env = strv_env_merge(2,
1756 UNIT(s)->manager->environment,
1771 UNIT(s)->manager->confirm_spawn,
1772 UNIT(s)->cgroup_bondings,
1773 UNIT(s)->cgroup_attributes,
1774 is_control ? "control" : NULL,
1775 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1782 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1783 /* FIXME: we need to do something here */
1789 strv_free(final_env);
1799 strv_free(final_env);
1802 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1807 static int main_pid_good(Service *s) {
1810 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1813 /* If we know the pid file, then lets just check if it is
1815 if (s->main_pid_known) {
1817 /* If it's an alien child let's check if it is still
1819 if (s->main_pid_alien)
1820 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1822 /* .. otherwise assume we'll get a SIGCHLD for it,
1823 * which we really should wait for to collect exit
1824 * status and code */
1825 return s->main_pid > 0;
1828 /* We don't know the pid */
1832 static int control_pid_good(Service *s) {
1835 return s->control_pid > 0;
1838 static int cgroup_good(Service *s) {
1843 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1849 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1853 if (f != SERVICE_SUCCESS)
1856 if (allow_restart &&
1857 !s->forbid_restart &&
1858 (s->restart == SERVICE_RESTART_ALWAYS ||
1859 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1860 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1861 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1862 s->result == SERVICE_FAILURE_CORE_DUMP)))) {
1864 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
1868 service_set_state(s, SERVICE_AUTO_RESTART);
1870 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1872 s->forbid_restart = false;
1877 log_warning("%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1878 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1881 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1883 static void service_enter_stop_post(Service *s, ServiceResult f) {
1887 if (f != SERVICE_SUCCESS)
1890 service_unwatch_control_pid(s);
1892 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1893 s->control_command_id = SERVICE_EXEC_STOP_POST;
1895 r = service_spawn(s,
1899 !s->permissions_start_only,
1900 !s->root_directory_start_only,
1909 service_set_state(s, SERVICE_STOP_POST);
1911 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1916 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
1917 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1920 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1922 Set *pid_set = NULL;
1923 bool wait_for_exit = false;
1927 if (f != SERVICE_SUCCESS)
1930 if (s->exec_context.kill_mode != KILL_NONE) {
1931 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1933 if (s->main_pid > 0) {
1934 if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
1935 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1937 wait_for_exit = !s->main_pid_alien;
1940 if (s->control_pid > 0) {
1941 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1942 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1944 wait_for_exit = true;
1947 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1949 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1954 /* Exclude the main/control pids from being killed via the cgroup */
1955 if (s->main_pid > 0)
1956 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1959 if (s->control_pid > 0)
1960 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1963 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, pid_set, NULL);
1965 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1966 log_warning("Failed to kill control group: %s", strerror(-r));
1968 wait_for_exit = true;
1975 if (wait_for_exit) {
1976 if (s->timeout_usec > 0)
1977 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1980 service_set_state(s, state);
1981 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1982 service_enter_stop_post(s, SERVICE_SUCCESS);
1984 service_enter_dead(s, SERVICE_SUCCESS, true);
1989 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1991 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1992 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1994 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2000 static void service_enter_stop(Service *s, ServiceResult f) {
2005 if (f != SERVICE_SUCCESS)
2008 service_unwatch_control_pid(s);
2010 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2011 s->control_command_id = SERVICE_EXEC_STOP;
2013 r = service_spawn(s,
2017 !s->permissions_start_only,
2018 !s->root_directory_start_only,
2026 service_set_state(s, SERVICE_STOP);
2028 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2033 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2034 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2037 static void service_enter_running(Service *s, ServiceResult f) {
2038 int main_pid_ok, cgroup_ok;
2041 if (f != SERVICE_SUCCESS)
2044 main_pid_ok = main_pid_good(s);
2045 cgroup_ok = cgroup_good(s);
2047 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2048 (s->bus_name_good || s->type != SERVICE_DBUS))
2049 service_set_state(s, SERVICE_RUNNING);
2050 else if (s->remain_after_exit)
2051 service_set_state(s, SERVICE_EXITED);
2053 service_enter_stop(s, SERVICE_SUCCESS);
2056 static void service_enter_start_post(Service *s) {
2060 service_unwatch_control_pid(s);
2062 if (s->watchdog_usec > 0)
2063 service_reset_watchdog(s);
2065 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2066 s->control_command_id = SERVICE_EXEC_START_POST;
2068 r = service_spawn(s,
2072 !s->permissions_start_only,
2073 !s->root_directory_start_only,
2081 service_set_state(s, SERVICE_START_POST);
2083 service_enter_running(s, SERVICE_SUCCESS);
2088 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2089 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2092 static void service_enter_start(Service *s) {
2099 assert(s->exec_command[SERVICE_EXEC_START]);
2100 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2102 if (s->type == SERVICE_FORKING)
2103 service_unwatch_control_pid(s);
2105 service_unwatch_main_pid(s);
2107 /* We want to ensure that nobody leaks processes from
2108 * START_PRE here, so let's go on a killing spree, People
2109 * should not spawn long running processes from START_PRE. */
2110 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, NULL, "control");
2112 if (s->type == SERVICE_FORKING) {
2113 s->control_command_id = SERVICE_EXEC_START;
2114 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2116 s->main_command = NULL;
2118 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2119 s->control_command = NULL;
2121 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2124 r = service_spawn(s,
2126 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
2131 s->notify_access != NOTIFY_NONE,
2137 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2138 /* For simple services we immediately start
2139 * the START_POST binaries. */
2141 service_set_main_pid(s, pid);
2142 service_enter_start_post(s);
2144 } else if (s->type == SERVICE_FORKING) {
2146 /* For forking services we wait until the start
2147 * process exited. */
2149 s->control_pid = pid;
2150 service_set_state(s, SERVICE_START);
2152 } else if (s->type == SERVICE_ONESHOT ||
2153 s->type == SERVICE_DBUS ||
2154 s->type == SERVICE_NOTIFY) {
2156 /* For oneshot services we wait until the start
2157 * process exited, too, but it is our main process. */
2159 /* For D-Bus services we know the main pid right away,
2160 * but wait for the bus name to appear on the
2161 * bus. Notify services are similar. */
2163 service_set_main_pid(s, pid);
2164 service_set_state(s, SERVICE_START);
2166 assert_not_reached("Unknown service type");
2171 log_warning("%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2172 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2175 static void service_enter_start_pre(Service *s) {
2180 service_unwatch_control_pid(s);
2182 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2184 /* Before we start anything, let's clear up what might
2185 * be left from previous runs. */
2186 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, NULL, "control");
2188 s->control_command_id = SERVICE_EXEC_START_PRE;
2190 r = service_spawn(s,
2194 !s->permissions_start_only,
2195 !s->root_directory_start_only,
2203 service_set_state(s, SERVICE_START_PRE);
2205 service_enter_start(s);
2210 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2211 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2214 static void service_enter_restart(Service *s) {
2219 dbus_error_init(&error);
2222 log_info("Job pending for unit, delaying automatic restart.");
2224 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
2228 /* Any units that are bound to this service must also be
2229 * restarted. We use JOB_RESTART (instead of the more obvious
2230 * JOB_START) here so that those dependency jobs will be added
2232 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2236 log_debug("%s scheduled restart job.", UNIT(s)->id);
2240 log_warning("%s failed to schedule restart job: %s", UNIT(s)->id, bus_error(&error, -r));
2241 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2243 dbus_error_free(&error);
2246 static void service_enter_reload(Service *s) {
2251 service_unwatch_control_pid(s);
2253 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2254 s->control_command_id = SERVICE_EXEC_RELOAD;
2256 r = service_spawn(s,
2260 !s->permissions_start_only,
2261 !s->root_directory_start_only,
2269 service_set_state(s, SERVICE_RELOAD);
2271 service_enter_running(s, SERVICE_SUCCESS);
2276 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
2277 s->reload_result = SERVICE_FAILURE_RESOURCES;
2278 service_enter_running(s, SERVICE_SUCCESS);
2281 static void service_run_next_control(Service *s) {
2285 assert(s->control_command);
2286 assert(s->control_command->command_next);
2288 assert(s->control_command_id != SERVICE_EXEC_START);
2290 s->control_command = s->control_command->command_next;
2291 service_unwatch_control_pid(s);
2293 r = service_spawn(s,
2297 !s->permissions_start_only,
2298 !s->root_directory_start_only,
2299 s->control_command_id == SERVICE_EXEC_START_PRE ||
2300 s->control_command_id == SERVICE_EXEC_STOP_POST,
2310 log_warning("%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
2312 if (s->state == SERVICE_START_PRE)
2313 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2314 else if (s->state == SERVICE_STOP)
2315 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2316 else if (s->state == SERVICE_STOP_POST)
2317 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2318 else if (s->state == SERVICE_RELOAD) {
2319 s->reload_result = SERVICE_FAILURE_RESOURCES;
2320 service_enter_running(s, SERVICE_SUCCESS);
2322 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2325 static void service_run_next_main(Service *s) {
2330 assert(s->main_command);
2331 assert(s->main_command->command_next);
2332 assert(s->type == SERVICE_ONESHOT);
2334 s->main_command = s->main_command->command_next;
2335 service_unwatch_main_pid(s);
2337 r = service_spawn(s,
2344 s->notify_access != NOTIFY_NONE,
2350 service_set_main_pid(s, pid);
2355 log_warning("%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2356 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2359 static int service_start_limit_test(Service *s) {
2362 if (ratelimit_test(&s->start_limit))
2365 switch (s->start_limit_action) {
2367 case SERVICE_START_LIMIT_NONE:
2368 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s)->id);
2371 case SERVICE_START_LIMIT_REBOOT: {
2375 dbus_error_init(&error);
2377 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2379 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2381 log_error("Failed to reboot: %s.", bus_error(&error, r));
2382 dbus_error_free(&error);
2388 case SERVICE_START_LIMIT_REBOOT_FORCE:
2389 log_warning("%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2390 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2393 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2394 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2395 reboot(RB_AUTOBOOT);
2399 log_error("start limit action=%i", s->start_limit_action);
2400 assert_not_reached("Unknown StartLimitAction.");
2406 static int service_start(Unit *u) {
2407 Service *s = SERVICE(u);
2412 /* We cannot fulfill this request right now, try again later
2414 if (s->state == SERVICE_STOP ||
2415 s->state == SERVICE_STOP_SIGTERM ||
2416 s->state == SERVICE_STOP_SIGKILL ||
2417 s->state == SERVICE_STOP_POST ||
2418 s->state == SERVICE_FINAL_SIGTERM ||
2419 s->state == SERVICE_FINAL_SIGKILL)
2422 /* Already on it! */
2423 if (s->state == SERVICE_START_PRE ||
2424 s->state == SERVICE_START ||
2425 s->state == SERVICE_START_POST)
2428 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART);
2430 /* Make sure we don't enter a busy loop of some kind. */
2431 r = service_start_limit_test(s);
2433 service_notify_sockets_dead(s, true);
2437 s->result = SERVICE_SUCCESS;
2438 s->reload_result = SERVICE_SUCCESS;
2439 s->main_pid_known = false;
2440 s->main_pid_alien = false;
2441 s->forbid_restart = false;
2443 service_enter_start_pre(s);
2447 static int service_stop(Unit *u) {
2448 Service *s = SERVICE(u);
2452 /* This is a user request, so don't do restarts on this
2454 s->forbid_restart = true;
2457 if (s->state == SERVICE_STOP ||
2458 s->state == SERVICE_STOP_SIGTERM ||
2459 s->state == SERVICE_STOP_SIGKILL ||
2460 s->state == SERVICE_STOP_POST ||
2461 s->state == SERVICE_FINAL_SIGTERM ||
2462 s->state == SERVICE_FINAL_SIGKILL)
2465 /* Don't allow a restart */
2466 if (s->state == SERVICE_AUTO_RESTART) {
2467 service_set_state(s, SERVICE_DEAD);
2471 /* If there's already something running we go directly into
2473 if (s->state == SERVICE_START_PRE ||
2474 s->state == SERVICE_START ||
2475 s->state == SERVICE_START_POST ||
2476 s->state == SERVICE_RELOAD) {
2477 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2481 assert(s->state == SERVICE_RUNNING ||
2482 s->state == SERVICE_EXITED);
2484 service_enter_stop(s, SERVICE_SUCCESS);
2488 static int service_reload(Unit *u) {
2489 Service *s = SERVICE(u);
2493 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2495 service_enter_reload(s);
2499 static bool service_can_reload(Unit *u) {
2500 Service *s = SERVICE(u);
2504 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2507 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2508 Service *s = SERVICE(u);
2514 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2515 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2516 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2518 if (s->control_pid > 0)
2519 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2521 if (s->main_pid_known && s->main_pid > 0)
2522 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2524 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2527 unit_serialize_item(u, f, "status-text", s->status_text);
2529 /* FIXME: There's a minor uncleanliness here: if there are
2530 * multiple commands attached here, we will start from the
2531 * first one again */
2532 if (s->control_command_id >= 0)
2533 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2535 if (s->socket_fd >= 0) {
2538 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2541 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2544 if (s->main_exec_status.pid > 0) {
2545 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2546 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2547 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2549 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2550 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2551 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2554 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2555 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2560 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2561 Service *s = SERVICE(u);
2568 if (streq(key, "state")) {
2571 if ((state = service_state_from_string(value)) < 0)
2572 log_debug("Failed to parse state value %s", value);
2574 s->deserialized_state = state;
2575 } else if (streq(key, "result")) {
2578 f = service_result_from_string(value);
2580 log_debug("Failed to parse result value %s", value);
2581 else if (f != SERVICE_SUCCESS)
2584 } else if (streq(key, "reload-result")) {
2587 f = service_result_from_string(value);
2589 log_debug("Failed to parse reload result value %s", value);
2590 else if (f != SERVICE_SUCCESS)
2591 s->reload_result = f;
2593 } else if (streq(key, "control-pid")) {
2596 if (parse_pid(value, &pid) < 0)
2597 log_debug("Failed to parse control-pid value %s", value);
2599 s->control_pid = pid;
2600 } else if (streq(key, "main-pid")) {
2603 if (parse_pid(value, &pid) < 0)
2604 log_debug("Failed to parse main-pid value %s", value);
2606 service_set_main_pid(s, (pid_t) pid);
2607 } else if (streq(key, "main-pid-known")) {
2610 if ((b = parse_boolean(value)) < 0)
2611 log_debug("Failed to parse main-pid-known value %s", value);
2613 s->main_pid_known = b;
2614 } else if (streq(key, "status-text")) {
2617 if ((t = strdup(value))) {
2618 free(s->status_text);
2622 } else if (streq(key, "control-command")) {
2623 ServiceExecCommand id;
2625 if ((id = service_exec_command_from_string(value)) < 0)
2626 log_debug("Failed to parse exec-command value %s", value);
2628 s->control_command_id = id;
2629 s->control_command = s->exec_command[id];
2631 } else if (streq(key, "socket-fd")) {
2634 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2635 log_debug("Failed to parse socket-fd value %s", value);
2638 if (s->socket_fd >= 0)
2639 close_nointr_nofail(s->socket_fd);
2640 s->socket_fd = fdset_remove(fds, fd);
2642 } else if (streq(key, "main-exec-status-pid")) {
2645 if (parse_pid(value, &pid) < 0)
2646 log_debug("Failed to parse main-exec-status-pid value %s", value);
2648 s->main_exec_status.pid = pid;
2649 } else if (streq(key, "main-exec-status-code")) {
2652 if (safe_atoi(value, &i) < 0)
2653 log_debug("Failed to parse main-exec-status-code value %s", value);
2655 s->main_exec_status.code = i;
2656 } else if (streq(key, "main-exec-status-status")) {
2659 if (safe_atoi(value, &i) < 0)
2660 log_debug("Failed to parse main-exec-status-status value %s", value);
2662 s->main_exec_status.status = i;
2663 } else if (streq(key, "main-exec-status-start"))
2664 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2665 else if (streq(key, "main-exec-status-exit"))
2666 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2667 else if (streq(key, "watchdog-timestamp"))
2668 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2670 log_debug("Unknown serialization key '%s'", key);
2675 static UnitActiveState service_active_state(Unit *u) {
2678 return state_translation_table[SERVICE(u)->state];
2681 static const char *service_sub_state_to_string(Unit *u) {
2684 return service_state_to_string(SERVICE(u)->state);
2687 static bool service_check_gc(Unit *u) {
2688 Service *s = SERVICE(u);
2692 /* Never clean up services that still have a process around,
2693 * even if the service is formally dead. */
2694 if (cgroup_good(s) > 0 ||
2695 main_pid_good(s) > 0 ||
2696 control_pid_good(s) > 0)
2699 #ifdef HAVE_SYSV_COMPAT
2707 static bool service_check_snapshot(Unit *u) {
2708 Service *s = SERVICE(u);
2712 return !s->got_socket_fd;
2715 static int service_retry_pid_file(Service *s) {
2718 assert(s->pid_file);
2719 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2721 r = service_load_pid_file(s, false);
2725 service_unwatch_pid_file(s);
2727 service_enter_running(s, SERVICE_SUCCESS);
2731 static int service_watch_pid_file(Service *s) {
2734 log_debug("Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
2735 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2739 /* the pidfile might have appeared just before we set the watch */
2740 service_retry_pid_file(s);
2744 log_error("Failed to set a watch for %s's PID file %s: %s",
2745 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2746 service_unwatch_pid_file(s);
2750 static int service_demand_pid_file(Service *s) {
2753 assert(s->pid_file);
2754 assert(!s->pid_file_pathspec);
2756 ps = new0(PathSpec, 1);
2760 ps->path = strdup(s->pid_file);
2766 path_kill_slashes(ps->path);
2768 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2769 * keep their PID file open all the time. */
2770 ps->type = PATH_MODIFIED;
2771 ps->inotify_fd = -1;
2773 s->pid_file_pathspec = ps;
2775 return service_watch_pid_file(s);
2778 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2779 Service *s = SERVICE(u);
2783 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2784 assert(s->pid_file_pathspec);
2785 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2787 log_debug("inotify event for %s", u->id);
2789 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2792 if (service_retry_pid_file(s) == 0)
2795 if (service_watch_pid_file(s) < 0)
2800 service_unwatch_pid_file(s);
2801 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2804 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2805 Service *s = SERVICE(u);
2811 if (UNIT(s)->fragment_path ? is_clean_exit(code, status) : is_clean_exit_lsb(code, status))
2812 f = SERVICE_SUCCESS;
2813 else if (code == CLD_EXITED)
2814 f = SERVICE_FAILURE_EXIT_CODE;
2815 else if (code == CLD_KILLED)
2816 f = SERVICE_FAILURE_SIGNAL;
2817 else if (code == CLD_DUMPED)
2818 f = SERVICE_FAILURE_CORE_DUMP;
2820 assert_not_reached("Unknown code");
2822 if (s->main_pid == pid) {
2823 /* Forking services may occasionally move to a new PID.
2824 * As long as they update the PID file before exiting the old
2825 * PID, they're fine. */
2826 if (service_load_pid_file(s, false) == 0)
2830 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2832 /* If this is not a forking service than the main
2833 * process got started and hence we copy the exit
2834 * status so that it is recorded both as main and as
2835 * control process exit status */
2836 if (s->main_command) {
2837 s->main_command->exec_status = s->main_exec_status;
2839 if (s->main_command->ignore)
2840 f = SERVICE_SUCCESS;
2843 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2844 "%s: main process exited, code=%s, status=%i", u->id, sigchld_code_to_string(code), status);
2846 if (f != SERVICE_SUCCESS)
2849 if (s->main_command &&
2850 s->main_command->command_next &&
2851 f == SERVICE_SUCCESS) {
2853 /* There is another command to *
2854 * execute, so let's do that. */
2856 log_debug("%s running next main command for state %s", u->id, service_state_to_string(s->state));
2857 service_run_next_main(s);
2861 /* The service exited, so the service is officially
2863 s->main_command = NULL;
2867 case SERVICE_START_POST:
2868 case SERVICE_RELOAD:
2870 /* Need to wait until the operation is
2875 if (s->type == SERVICE_ONESHOT) {
2876 /* This was our main goal, so let's go on */
2877 if (f == SERVICE_SUCCESS)
2878 service_enter_start_post(s);
2880 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2883 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2888 case SERVICE_RUNNING:
2889 service_enter_running(s, f);
2892 case SERVICE_STOP_SIGTERM:
2893 case SERVICE_STOP_SIGKILL:
2895 if (!control_pid_good(s))
2896 service_enter_stop_post(s, f);
2898 /* If there is still a control process, wait for that first */
2902 assert_not_reached("Uh, main process died at wrong time.");
2906 } else if (s->control_pid == pid) {
2910 if (s->control_command) {
2911 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2913 if (s->control_command->ignore)
2914 f = SERVICE_SUCCESS;
2917 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2918 "%s: control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
2920 if (f != SERVICE_SUCCESS)
2923 if (s->control_command &&
2924 s->control_command->command_next &&
2925 f == SERVICE_SUCCESS) {
2927 /* There is another command to *
2928 * execute, so let's do that. */
2930 log_debug("%s running next control command for state %s", u->id, service_state_to_string(s->state));
2931 service_run_next_control(s);
2934 /* No further commands for this step, so let's
2935 * figure out what to do next */
2937 s->control_command = NULL;
2938 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2940 log_debug("%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
2944 case SERVICE_START_PRE:
2945 if (f == SERVICE_SUCCESS)
2946 service_enter_start(s);
2948 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2952 assert(s->type == SERVICE_FORKING);
2954 if (f != SERVICE_SUCCESS) {
2955 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2960 bool has_start_post;
2963 /* Let's try to load the pid file here if we can.
2964 * The PID file might actually be created by a START_POST
2965 * script. In that case don't worry if the loading fails. */
2967 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2968 r = service_load_pid_file(s, !has_start_post);
2969 if (!has_start_post && r < 0) {
2970 r = service_demand_pid_file(s);
2971 if (r < 0 || !cgroup_good(s))
2972 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2976 service_search_main_pid(s);
2978 service_enter_start_post(s);
2981 case SERVICE_START_POST:
2982 if (f != SERVICE_SUCCESS) {
2983 service_enter_stop(s, f);
2990 r = service_load_pid_file(s, true);
2992 r = service_demand_pid_file(s);
2993 if (r < 0 || !cgroup_good(s))
2994 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2998 service_search_main_pid(s);
3000 service_enter_running(s, SERVICE_SUCCESS);
3003 case SERVICE_RELOAD:
3004 if (f == SERVICE_SUCCESS) {
3005 service_load_pid_file(s, true);
3006 service_search_main_pid(s);
3009 s->reload_result = f;
3010 service_enter_running(s, SERVICE_SUCCESS);
3014 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3017 case SERVICE_STOP_SIGTERM:
3018 case SERVICE_STOP_SIGKILL:
3019 if (main_pid_good(s) <= 0)
3020 service_enter_stop_post(s, f);
3022 /* If there is still a service
3023 * process around, wait until
3024 * that one quit, too */
3027 case SERVICE_STOP_POST:
3028 case SERVICE_FINAL_SIGTERM:
3029 case SERVICE_FINAL_SIGKILL:
3030 service_enter_dead(s, f, true);
3034 assert_not_reached("Uh, control process died at wrong time.");
3039 /* Notify clients about changed exit status */
3040 unit_add_to_dbus_queue(u);
3043 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3044 Service *s = SERVICE(u);
3047 assert(elapsed == 1);
3049 if (w == &s->watchdog_watch) {
3050 service_handle_watchdog(s);
3054 assert(w == &s->timer_watch);
3058 case SERVICE_START_PRE:
3060 log_warning("%s operation timed out. Terminating.", u->id);
3061 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3064 case SERVICE_START_POST:
3065 log_warning("%s operation timed out. Stopping.", u->id);
3066 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3069 case SERVICE_RELOAD:
3070 log_warning("%s operation timed out. Stopping.", u->id);
3071 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3072 service_enter_running(s, SERVICE_SUCCESS);
3076 log_warning("%s stopping timed out. Terminating.", u->id);
3077 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3080 case SERVICE_STOP_SIGTERM:
3081 if (s->exec_context.send_sigkill) {
3082 log_warning("%s stopping timed out. Killing.", u->id);
3083 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3085 log_warning("%s stopping timed out. Skipping SIGKILL.", u->id);
3086 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3091 case SERVICE_STOP_SIGKILL:
3092 /* Uh, we sent a SIGKILL and it is still not gone?
3093 * Must be something we cannot kill, so let's just be
3094 * weirded out and continue */
3096 log_warning("%s still around after SIGKILL. Ignoring.", u->id);
3097 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3100 case SERVICE_STOP_POST:
3101 log_warning("%s stopping timed out (2). Terminating.", u->id);
3102 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3105 case SERVICE_FINAL_SIGTERM:
3106 if (s->exec_context.send_sigkill) {
3107 log_warning("%s stopping timed out (2). Killing.", u->id);
3108 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3110 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->id);
3111 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3116 case SERVICE_FINAL_SIGKILL:
3117 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->id);
3118 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3121 case SERVICE_AUTO_RESTART:
3122 log_info("%s holdoff time over, scheduling restart.", u->id);
3123 service_enter_restart(s);
3127 assert_not_reached("Timeout at wrong time.");
3131 static void service_cgroup_notify_event(Unit *u) {
3132 Service *s = SERVICE(u);
3136 log_debug("%s: cgroup is empty", u->id);
3140 /* Waiting for SIGCHLD is usually more interesting,
3141 * because it includes return codes/signals. Which is
3142 * why we ignore the cgroup events for most cases,
3143 * except when we don't know pid which to expect the
3147 case SERVICE_START_POST:
3148 /* If we were hoping for the daemon to write its PID file,
3149 * we can give up now. */
3150 if (s->pid_file_pathspec) {
3151 log_warning("%s never wrote its PID file. Failing.", UNIT(s)->id);
3152 service_unwatch_pid_file(s);
3153 if (s->state == SERVICE_START)
3154 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3156 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3160 case SERVICE_RUNNING:
3161 /* service_enter_running() will figure out what to do */
3162 service_enter_running(s, SERVICE_SUCCESS);
3165 case SERVICE_STOP_SIGTERM:
3166 case SERVICE_STOP_SIGKILL:
3168 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3169 service_enter_stop_post(s, SERVICE_SUCCESS);
3173 case SERVICE_FINAL_SIGTERM:
3174 case SERVICE_FINAL_SIGKILL:
3175 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3176 service_enter_dead(s, SERVICE_SUCCESS, SERVICE_SUCCESS);
3185 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3186 Service *s = SERVICE(u);
3191 if (s->notify_access == NOTIFY_NONE) {
3192 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3193 u->id, (unsigned long) pid);
3197 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3198 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3199 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3203 log_debug("%s: Got message", u->id);
3205 /* Interpret MAINPID= */
3206 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3207 (s->state == SERVICE_START ||
3208 s->state == SERVICE_START_POST ||
3209 s->state == SERVICE_RUNNING ||
3210 s->state == SERVICE_RELOAD)) {
3212 if (parse_pid(e + 8, &pid) < 0)
3213 log_warning("Failed to parse notification message %s", e);
3215 log_debug("%s: got %s", u->id, e);
3216 service_set_main_pid(s, pid);
3220 /* Interpret READY= */
3221 if (s->type == SERVICE_NOTIFY &&
3222 s->state == SERVICE_START &&
3223 strv_find(tags, "READY=1")) {
3224 log_debug("%s: got READY=1", u->id);
3226 service_enter_start_post(s);
3229 /* Interpret STATUS= */
3230 e = strv_find_prefix(tags, "STATUS=");
3236 if (!utf8_is_valid(e+7)) {
3237 log_warning("Status message in notification is not UTF-8 clean.");
3243 log_error("Failed to allocate string.");
3247 log_debug("%s: got %s", u->id, e);
3249 free(s->status_text);
3252 free(s->status_text);
3253 s->status_text = NULL;
3257 if (strv_find(tags, "WATCHDOG=1")) {
3258 log_debug("%s: got WATCHDOG=1", u->id);
3259 service_reset_watchdog(s);
3262 /* Notify clients about changed status or main pid */
3263 unit_add_to_dbus_queue(u);
3266 #ifdef HAVE_SYSV_COMPAT
3269 static void sysv_facility_in_insserv_conf(Manager *mgr) {
3273 if (!(f = fopen("/etc/insserv.conf", "re"))) {
3274 r = errno == ENOENT ? 0 : -errno;
3279 char l[LINE_MAX], *t;
3280 char **parsed = NULL;
3282 if (!fgets(l, sizeof(l), f)) {
3287 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r));
3292 if (*t != '$' && *t != '<')
3295 parsed = strv_split(t,WHITESPACE);
3296 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3297 if (parsed && !startswith_no_case (parsed[0], "<interactive>")) {
3300 if (sysv_translate_facility(parsed[0], NULL, &facility) < 0)
3302 if ((u = manager_get_unit(mgr, facility)) && (u->type == UNIT_TARGET)) {
3304 char *dep = NULL, *name, **j;
3306 STRV_FOREACH (j, parsed+1) {
3315 if (sysv_translate_facility(name, NULL, &dep) < 0)
3318 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, e, dep, NULL, true);
3333 static int service_enumerate(Manager *m) {
3337 char *path = NULL, *fpath = NULL, *name = NULL;
3338 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3345 if (m->running_as != MANAGER_SYSTEM)
3348 zero(runlevel_services);
3350 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3351 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3355 path = join(*p, "/", rcnd_table[i].path, NULL);
3364 if (!(d = opendir(path))) {
3365 if (errno != ENOENT)
3366 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3371 while ((de = readdir(d))) {
3374 if (ignore_file(de->d_name))
3377 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3380 if (strlen(de->d_name) < 4)
3383 a = undecchar(de->d_name[1]);
3384 b = undecchar(de->d_name[2]);
3390 fpath = join(path, "/", de->d_name, NULL);
3396 if (access(fpath, X_OK) < 0) {
3398 if (errno != ENOENT)
3399 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3405 if (!(name = sysv_translate_name(de->d_name + 3))) {
3410 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
3411 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3415 if (de->d_name[0] == 'S') {
3417 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
3418 SERVICE(service)->sysv_start_priority_from_rcnd =
3419 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3421 SERVICE(service)->sysv_enabled = true;
3424 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3427 if ((r = set_put(runlevel_services[i], service)) < 0)
3430 } else if (de->d_name[0] == 'K' &&
3431 (rcnd_table[i].type == RUNLEVEL_DOWN ||
3432 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
3434 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3437 if ((r = set_put(shutdown_services, service)) < 0)
3443 /* Now we loaded all stubs and are aware of the lowest
3444 start-up priority for all services, not let's actually load
3445 the services, this will also tell us which services are
3446 actually native now */
3447 manager_dispatch_load_queue(m);
3449 /* If this is a native service, rely on native ways to pull in
3450 * a service, don't pull it in via sysv rcN.d links. */
3451 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3452 SET_FOREACH(service, runlevel_services[i], j) {
3453 service = unit_follow_merge(service);
3455 if (service->fragment_path)
3458 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3462 /* We honour K links only for halt/reboot. For the normal
3463 * runlevels we assume the stop jobs will be implicitly added
3464 * by the core logic. Also, we don't really distinguish here
3465 * between the runlevels 0 and 6 and just add them to the
3466 * special shutdown target. On SUSE the boot.d/ runlevel is
3467 * also used for shutdown, so we add links for that too to the
3468 * shutdown target.*/
3469 SET_FOREACH(service, shutdown_services, j) {
3470 service = unit_follow_merge(service);
3472 if (service->fragment_path)
3475 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3482 sysv_facility_in_insserv_conf (m);
3490 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3491 set_free(runlevel_services[i]);
3492 set_free(shutdown_services);
3501 static void service_bus_name_owner_change(
3504 const char *old_owner,
3505 const char *new_owner) {
3507 Service *s = SERVICE(u);
3512 assert(streq(s->bus_name, name));
3513 assert(old_owner || new_owner);
3515 if (old_owner && new_owner)
3516 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
3518 log_debug("%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
3520 log_debug("%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
3522 s->bus_name_good = !!new_owner;
3524 if (s->type == SERVICE_DBUS) {
3526 /* service_enter_running() will figure out what to
3528 if (s->state == SERVICE_RUNNING)
3529 service_enter_running(s, SERVICE_SUCCESS);
3530 else if (s->state == SERVICE_START && new_owner)
3531 service_enter_start_post(s);
3533 } else if (new_owner &&
3535 (s->state == SERVICE_START ||
3536 s->state == SERVICE_START_POST ||
3537 s->state == SERVICE_RUNNING ||
3538 s->state == SERVICE_RELOAD)) {
3540 /* Try to acquire PID from bus service */
3541 log_debug("Trying to acquire PID from D-Bus name...");
3543 bus_query_pid(u->manager, name);
3547 static void service_bus_query_pid_done(
3552 Service *s = SERVICE(u);
3557 log_debug("%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3559 if (s->main_pid <= 0 &&
3560 (s->state == SERVICE_START ||
3561 s->state == SERVICE_START_POST ||
3562 s->state == SERVICE_RUNNING ||
3563 s->state == SERVICE_RELOAD))
3564 service_set_main_pid(s, pid);
3567 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3572 /* This is called by the socket code when instantiating a new
3573 * service for a stream socket and the socket needs to be
3576 if (UNIT(s)->load_state != UNIT_LOADED)
3579 if (s->socket_fd >= 0)
3582 if (s->state != SERVICE_DEAD)
3586 s->got_socket_fd = true;
3588 unit_ref_set(&s->accept_socket, UNIT(sock));
3590 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3593 static void service_reset_failed(Unit *u) {
3594 Service *s = SERVICE(u);
3598 if (s->state == SERVICE_FAILED)
3599 service_set_state(s, SERVICE_DEAD);
3601 s->result = SERVICE_SUCCESS;
3602 s->reload_result = SERVICE_SUCCESS;
3605 static bool service_need_daemon_reload(Unit *u) {
3606 Service *s = SERVICE(u);
3610 #ifdef HAVE_SYSV_COMPAT
3615 if (stat(s->sysv_path, &st) < 0)
3616 /* What, cannot access this anymore? */
3619 if (s->sysv_mtime > 0 &&
3620 timespec_load(&st.st_mtim) != s->sysv_mtime)
3628 static int service_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
3629 Service *s = SERVICE(u);
3631 Set *pid_set = NULL;
3635 if (s->main_pid <= 0 && who == KILL_MAIN) {
3636 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3640 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3641 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3645 if (who == KILL_CONTROL || who == KILL_ALL)
3646 if (s->control_pid > 0)
3647 if (kill(s->control_pid, signo) < 0)
3650 if (who == KILL_MAIN || who == KILL_ALL)
3651 if (s->main_pid > 0)
3652 if (kill(s->main_pid, signo) < 0)
3655 if (who == KILL_ALL && mode == KILL_CONTROL_GROUP) {
3658 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
3661 /* Exclude the control/main pid from being killed via the cgroup */
3662 if (s->control_pid > 0)
3663 if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
3668 if (s->main_pid > 0)
3669 if ((q = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0) {
3673 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, pid_set, NULL);
3675 if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3686 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3687 [SERVICE_DEAD] = "dead",
3688 [SERVICE_START_PRE] = "start-pre",
3689 [SERVICE_START] = "start",
3690 [SERVICE_START_POST] = "start-post",
3691 [SERVICE_RUNNING] = "running",
3692 [SERVICE_EXITED] = "exited",
3693 [SERVICE_RELOAD] = "reload",
3694 [SERVICE_STOP] = "stop",
3695 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3696 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3697 [SERVICE_STOP_POST] = "stop-post",
3698 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3699 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3700 [SERVICE_FAILED] = "failed",
3701 [SERVICE_AUTO_RESTART] = "auto-restart",
3704 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3706 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3707 [SERVICE_RESTART_NO] = "no",
3708 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3709 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3710 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3711 [SERVICE_RESTART_ALWAYS] = "always"
3714 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3716 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3717 [SERVICE_SIMPLE] = "simple",
3718 [SERVICE_FORKING] = "forking",
3719 [SERVICE_ONESHOT] = "oneshot",
3720 [SERVICE_DBUS] = "dbus",
3721 [SERVICE_NOTIFY] = "notify",
3722 [SERVICE_IDLE] = "idle"
3725 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3727 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3728 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3729 [SERVICE_EXEC_START] = "ExecStart",
3730 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3731 [SERVICE_EXEC_RELOAD] = "ExecReload",
3732 [SERVICE_EXEC_STOP] = "ExecStop",
3733 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3736 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3738 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3739 [NOTIFY_NONE] = "none",
3740 [NOTIFY_MAIN] = "main",
3741 [NOTIFY_ALL] = "all"
3744 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3746 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3747 [SERVICE_SUCCESS] = "success",
3748 [SERVICE_FAILURE_RESOURCES] = "resources",
3749 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3750 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3751 [SERVICE_FAILURE_SIGNAL] = "signal",
3752 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3753 [SERVICE_FAILURE_WATCHDOG] = "watchdog"
3756 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3758 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3759 [SERVICE_START_LIMIT_NONE] = "none",
3760 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3761 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3762 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3764 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3766 const UnitVTable service_vtable = {
3767 .suffix = ".service",
3768 .object_size = sizeof(Service),
3773 .show_status = true,
3775 .init = service_init,
3776 .done = service_done,
3777 .load = service_load,
3779 .coldplug = service_coldplug,
3781 .dump = service_dump,
3783 .start = service_start,
3784 .stop = service_stop,
3785 .reload = service_reload,
3787 .can_reload = service_can_reload,
3789 .kill = service_kill,
3791 .serialize = service_serialize,
3792 .deserialize_item = service_deserialize_item,
3794 .active_state = service_active_state,
3795 .sub_state_to_string = service_sub_state_to_string,
3797 .check_gc = service_check_gc,
3798 .check_snapshot = service_check_snapshot,
3800 .sigchld_event = service_sigchld_event,
3801 .timer_event = service_timer_event,
3802 .fd_event = service_fd_event,
3804 .reset_failed = service_reset_failed,
3806 .need_daemon_reload = service_need_daemon_reload,
3808 .cgroup_notify_empty = service_cgroup_notify_event,
3809 .notify_message = service_notify_message,
3811 .bus_name_owner_change = service_bus_name_owner_change,
3812 .bus_query_pid_done = service_bus_query_pid_done,
3814 .bus_interface = "org.freedesktop.systemd1.Service",
3815 .bus_message_handler = bus_service_message_handler,
3816 .bus_invalidating_properties = bus_service_invalidating_properties,
3818 #ifdef HAVE_SYSV_COMPAT
3819 .enumerate = service_enumerate