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 /* For Type=idle we never want to delay any other jobs, hence we
112 * consider idle jobs active as soon as we start working on them */
113 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
114 [SERVICE_DEAD] = UNIT_INACTIVE,
115 [SERVICE_START_PRE] = UNIT_ACTIVE,
116 [SERVICE_START] = UNIT_ACTIVE,
117 [SERVICE_START_POST] = UNIT_ACTIVE,
118 [SERVICE_RUNNING] = UNIT_ACTIVE,
119 [SERVICE_EXITED] = UNIT_ACTIVE,
120 [SERVICE_RELOAD] = UNIT_RELOADING,
121 [SERVICE_STOP] = UNIT_DEACTIVATING,
122 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
123 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
124 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
125 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
126 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
127 [SERVICE_FAILED] = UNIT_FAILED,
128 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
131 static void service_init(Unit *u) {
132 Service *s = SERVICE(u);
135 assert(u->load_state == UNIT_STUB);
137 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
138 s->restart_usec = DEFAULT_RESTART_USEC;
139 s->type = _SERVICE_TYPE_INVALID;
141 s->watchdog_watch.type = WATCH_INVALID;
143 s->timer_watch.type = WATCH_INVALID;
144 #ifdef HAVE_SYSV_COMPAT
145 s->sysv_start_priority = -1;
146 s->sysv_start_priority_from_rcnd = -1;
149 s->guess_main_pid = true;
151 exec_context_init(&s->exec_context);
152 kill_context_init(&s->kill_context);
154 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
156 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
159 static void service_unwatch_control_pid(Service *s) {
162 if (s->control_pid <= 0)
165 unit_unwatch_pid(UNIT(s), s->control_pid);
169 static void service_unwatch_main_pid(Service *s) {
172 if (s->main_pid <= 0)
175 unit_unwatch_pid(UNIT(s), s->main_pid);
179 static void service_unwatch_pid_file(Service *s) {
180 if (!s->pid_file_pathspec)
183 log_debug("Stopping watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
184 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
185 path_spec_done(s->pid_file_pathspec);
186 free(s->pid_file_pathspec);
187 s->pid_file_pathspec = NULL;
190 static int service_set_main_pid(Service *s, pid_t pid) {
202 s->main_pid_known = true;
204 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
205 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
206 UNIT(s)->id, (unsigned long) pid);
208 s->main_pid_alien = true;
210 s->main_pid_alien = false;
212 exec_status_start(&s->main_exec_status, pid);
217 static void service_close_socket_fd(Service *s) {
220 if (s->socket_fd < 0)
223 close_nointr_nofail(s->socket_fd);
227 static void service_connection_unref(Service *s) {
230 if (!UNIT_DEREF(s->accept_socket))
233 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
234 unit_ref_unset(&s->accept_socket);
237 static void service_stop_watchdog(Service *s) {
240 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
241 s->watchdog_timestamp.realtime = 0;
242 s->watchdog_timestamp.monotonic = 0;
245 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart);
247 static void service_handle_watchdog(Service *s) {
253 if (s->watchdog_usec == 0)
256 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
257 if (offset >= s->watchdog_usec) {
258 log_error("%s watchdog timeout!", UNIT(s)->id);
259 service_enter_dead(s, SERVICE_FAILURE_WATCHDOG, true);
263 r = unit_watch_timer(UNIT(s), s->watchdog_usec - offset, &s->watchdog_watch);
265 log_warning("%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
268 static void service_reset_watchdog(Service *s) {
271 dual_timestamp_get(&s->watchdog_timestamp);
272 service_handle_watchdog(s);
275 static void service_done(Unit *u) {
276 Service *s = SERVICE(u);
283 #ifdef HAVE_SYSV_COMPAT
284 free(s->sysv_runlevels);
285 s->sysv_runlevels = NULL;
288 free(s->status_text);
289 s->status_text = NULL;
291 exec_context_done(&s->exec_context);
292 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
293 s->control_command = NULL;
294 s->main_command = NULL;
296 /* This will leak a process, but at least no memory or any of
298 service_unwatch_main_pid(s);
299 service_unwatch_control_pid(s);
300 service_unwatch_pid_file(s);
303 unit_unwatch_bus_name(u, s->bus_name);
308 service_close_socket_fd(s);
309 service_connection_unref(s);
311 unit_ref_unset(&s->accept_socket);
313 service_stop_watchdog(s);
315 unit_unwatch_timer(u, &s->timer_watch);
318 #ifdef HAVE_SYSV_COMPAT
319 static char *sysv_translate_name(const char *name) {
322 if (!(r = new(char, strlen(name) + sizeof(".service"))))
325 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
326 if (endswith(name, ".sh"))
327 /* Drop Debian-style .sh suffix */
328 strcpy(stpcpy(r, name) - 3, ".service");
331 if (startswith(name, "boot."))
332 /* Drop SuSE-style boot. prefix */
333 strcpy(stpcpy(r, name + 5), ".service");
335 #ifdef TARGET_FRUGALWARE
336 if (startswith(name, "rc."))
337 /* Drop Frugalware-style rc. prefix */
338 strcpy(stpcpy(r, name + 3), ".service");
341 /* Normal init scripts */
342 strcpy(stpcpy(r, name), ".service");
347 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
349 /* We silently ignore the $ prefix here. According to the LSB
350 * spec it simply indicates whether something is a
351 * standardized name or a distribution-specific one. Since we
352 * just follow what already exists and do not introduce new
353 * uses or names we don't care who introduced a new name. */
355 static const char * const table[] = {
356 /* LSB defined facilities */
357 "local_fs", SPECIAL_LOCAL_FS_TARGET,
358 #if defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
360 /* Due to unfortunate name selection in Mandriva,
361 * $network is provided by network-up which is ordered
362 * after network which actually starts interfaces.
363 * To break the loop, just ignore it */
364 "network", SPECIAL_NETWORK_TARGET,
366 "named", SPECIAL_NSS_LOOKUP_TARGET,
367 "portmap", SPECIAL_RPCBIND_TARGET,
368 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
369 "syslog", SPECIAL_SYSLOG_TARGET,
370 "time", SPECIAL_TIME_SYNC_TARGET,
372 /* common extensions */
373 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
374 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
377 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
378 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
382 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
383 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
384 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
388 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
399 n = *name == '$' ? name + 1 : name;
401 for (i = 0; i < ELEMENTSOF(table); i += 2) {
403 if (!streq(table[i], n))
409 if (!(r = strdup(table[i+1])))
415 /* If we don't know this name, fallback heuristics to figure
416 * out whether something is a target or a service alias. */
419 if (!unit_prefix_is_valid(n))
422 /* Facilities starting with $ are most likely targets */
423 r = unit_name_build(n, NULL, ".target");
424 } else if (filename && streq(name, filename))
425 /* Names equaling the file name of the services are redundant */
428 /* Everything else we assume to be normal service names */
429 r = sysv_translate_name(n);
440 static int sysv_fix_order(Service *s) {
446 if (s->sysv_start_priority < 0)
449 /* For each pair of services where at least one lacks a LSB
450 * header, we use the start priority value to order things. */
452 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
455 bool special_s, special_t;
462 if (UNIT(t)->load_state != UNIT_LOADED)
465 if (t->sysv_start_priority < 0)
468 /* If both units have modern headers we don't care
469 * about the priorities */
470 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
471 (UNIT(t)->fragment_path || t->sysv_has_lsb))
474 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
475 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
477 if (special_t && !special_s)
479 else if (special_s && !special_t)
481 else if (t->sysv_start_priority < s->sysv_start_priority)
483 else if (t->sysv_start_priority > s->sysv_start_priority)
488 /* FIXME: Maybe we should compare the name here lexicographically? */
490 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
497 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
500 if (!(c = new0(ExecCommand, 1)))
503 if (!(c->path = strdup(path))) {
508 if (!(c->argv = strv_new(path, arg1, NULL))) {
517 static int sysv_exec_commands(Service *s) {
522 assert(UNIT(s)->source_path);
524 c = exec_command_new(UNIT(s)->source_path, "start");
527 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
529 c = exec_command_new(UNIT(s)->source_path, "stop");
532 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
534 c = exec_command_new(UNIT(s)->source_path, "reload");
537 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
542 static int service_load_sysv_path(Service *s, const char *path) {
553 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
561 f = fopen(path, "re");
563 r = errno == ENOENT ? 0 : -errno;
567 if (fstat(fileno(f), &st) < 0) {
572 free(u->source_path);
573 u->source_path = strdup(path);
574 if (!u->source_path) {
578 u->source_mtime = timespec_load(&st.st_mtim);
580 if (null_or_empty(&st)) {
581 u->load_state = UNIT_MASKED;
589 char l[LINE_MAX], *t;
591 if (!fgets(l, sizeof(l), f)) {
596 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
606 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
608 s->sysv_has_lsb = true;
612 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
618 t += strspn(t, WHITESPACE);
620 if (state == NORMAL) {
622 /* Try to parse Red Hat style chkconfig headers */
624 if (startswith_no_case(t, "chkconfig:")) {
626 char runlevels[16], *k;
630 if (sscanf(t+10, "%15s %i %*i",
632 &start_priority) != 2) {
634 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
638 /* A start priority gathered from the
639 * symlink farms is preferred over the
640 * data from the LSB header. */
641 if (start_priority < 0 || start_priority > 99)
642 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
644 s->sysv_start_priority = start_priority;
646 char_array_0(runlevels);
647 k = delete_chars(runlevels, WHITESPACE "-");
652 if (!(d = strdup(k))) {
657 free(s->sysv_runlevels);
658 s->sysv_runlevels = d;
661 } else if (startswith_no_case(t, "description:")) {
663 size_t k = strlen(t);
667 if (t[k-1] == '\\') {
672 if ((j = strstrip(t+12)) && *j) {
673 if (!(d = strdup(j))) {
680 free(chkconfig_description);
681 chkconfig_description = d;
683 } else if (startswith_no_case(t, "pidfile:")) {
690 if (!path_is_absolute(fn)) {
691 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
695 if (!(fn = strdup(fn))) {
704 } else if (state == DESCRIPTION) {
706 /* Try to parse Red Hat style description
709 size_t k = strlen(t);
717 if ((j = strstrip(t)) && *j) {
720 if (chkconfig_description)
721 d = strjoin(chkconfig_description, " ", j, NULL);
730 free(chkconfig_description);
731 chkconfig_description = d;
734 } else if (state == LSB || state == LSB_DESCRIPTION) {
736 if (startswith_no_case(t, "Provides:")) {
742 FOREACH_WORD_QUOTED(w, z, t+9, i) {
745 if (!(n = strndup(w, z))) {
750 r = sysv_translate_facility(n, path_get_file_name(path), &m);
759 if (unit_name_to_type(m) == UNIT_SERVICE)
760 r = unit_add_name(u, m);
767 * indication that the
769 * now available. This
772 * targets do NOT pull
775 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
778 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
783 } else if (startswith_no_case(t, "Required-Start:") ||
784 startswith_no_case(t, "Should-Start:") ||
785 startswith_no_case(t, "X-Start-Before:") ||
786 startswith_no_case(t, "X-Start-After:")) {
792 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
795 if (!(n = strndup(w, z))) {
800 r = sysv_translate_facility(n, path_get_file_name(path), &m);
803 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
813 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
816 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
820 } else if (startswith_no_case(t, "Default-Start:")) {
825 k = delete_chars(t+14, WHITESPACE "-");
828 if (!(d = strdup(k))) {
833 free(s->sysv_runlevels);
834 s->sysv_runlevels = d;
837 } else if (startswith_no_case(t, "Description:")) {
840 state = LSB_DESCRIPTION;
842 if ((j = strstrip(t+12)) && *j) {
843 if (!(d = strdup(j))) {
850 free(long_description);
851 long_description = d;
853 } else if (startswith_no_case(t, "Short-Description:")) {
858 if ((j = strstrip(t+18)) && *j) {
859 if (!(d = strdup(j))) {
866 free(short_description);
867 short_description = d;
869 } else if (state == LSB_DESCRIPTION) {
871 if (startswith(l, "#\t") || startswith(l, "# ")) {
874 if ((j = strstrip(t)) && *j) {
877 if (long_description)
878 d = strjoin(long_description, " ", t, NULL);
887 free(long_description);
888 long_description = d;
897 if ((r = sysv_exec_commands(s)) < 0)
899 if (s->sysv_runlevels &&
900 chars_intersect(RUNLEVELS_BOOT, s->sysv_runlevels) &&
901 chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
902 /* Service has both boot and "up" runlevels
903 configured. Kill the "up" ones. */
904 delete_chars(s->sysv_runlevels, RUNLEVELS_UP);
907 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
908 /* If there a runlevels configured for this service
909 * but none of the standard ones, then we assume this
910 * is some special kind of service (which might be
911 * needed for early boot) and don't create any links
914 UNIT(s)->default_dependencies = false;
916 /* Don't timeout special services during boot (like fsck) */
919 s->timeout_usec = DEFAULT_SYSV_TIMEOUT_USEC;
921 /* Special setting for all SysV services */
922 s->type = SERVICE_FORKING;
923 s->remain_after_exit = !s->pid_file;
924 s->guess_main_pid = false;
925 s->restart = SERVICE_RESTART_NO;
926 s->exec_context.ignore_sigpipe = false;
927 s->kill_context.kill_mode = KILL_PROCESS;
929 /* We use the long description only if
930 * no short description is set. */
932 if (short_description)
933 description = short_description;
934 else if (chkconfig_description)
935 description = chkconfig_description;
936 else if (long_description)
937 description = long_description;
944 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
952 /* The priority that has been set in /etc/rcN.d/ hierarchies
953 * takes precedence over what is stored as default in the LSB
955 if (s->sysv_start_priority_from_rcnd >= 0)
956 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
958 u->load_state = UNIT_LOADED;
966 free(short_description);
967 free(long_description);
968 free(chkconfig_description);
973 static int service_load_sysv_name(Service *s, const char *name) {
979 /* For SysV services we strip the boot.*, rc.* and *.sh
980 * prefixes/suffixes. */
981 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
982 if (endswith(name, ".sh.service"))
987 if (startswith(name, "boot."))
991 #ifdef TARGET_FRUGALWARE
992 if (startswith(name, "rc."))
996 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1000 path = strjoin(*p, "/", name, NULL);
1004 assert(endswith(path, ".service"));
1005 path[strlen(path)-8] = 0;
1007 r = service_load_sysv_path(s, path);
1009 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
1010 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1011 /* Try Debian style *.sh source'able init scripts */
1012 strcat(path, ".sh");
1013 r = service_load_sysv_path(s, path);
1019 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1020 /* Try SUSE style boot.* init scripts */
1022 path = strjoin(*p, "/boot.", name, NULL);
1026 /* Drop .service suffix */
1027 path[strlen(path)-8] = 0;
1028 r = service_load_sysv_path(s, path);
1033 #ifdef TARGET_FRUGALWARE
1034 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1035 /* Try Frugalware style rc.* init scripts */
1037 path = strjoin(*p, "/rc.", name, NULL);
1041 /* Drop .service suffix */
1042 path[strlen(path)-8] = 0;
1043 r = service_load_sysv_path(s, path);
1051 if ((UNIT(s)->load_state != UNIT_STUB))
1058 static int service_load_sysv(Service *s) {
1065 /* Load service data from SysV init scripts, preferably with
1066 * LSB headers ... */
1068 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1071 if ((t = UNIT(s)->id))
1072 if ((r = service_load_sysv_name(s, t)) < 0)
1075 if (UNIT(s)->load_state == UNIT_STUB)
1076 SET_FOREACH(t, UNIT(s)->names, i) {
1077 if (t == UNIT(s)->id)
1080 if ((r = service_load_sysv_name(s, t)) < 0)
1083 if (UNIT(s)->load_state != UNIT_STUB)
1091 static int fsck_fix_order(Service *s) {
1097 if (s->fsck_passno <= 0)
1100 /* For each pair of services where both have an fsck priority
1101 * we order things based on it. */
1103 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1112 if (UNIT(t)->load_state != UNIT_LOADED)
1115 if (t->fsck_passno <= 0)
1118 if (t->fsck_passno < s->fsck_passno)
1120 else if (t->fsck_passno > s->fsck_passno)
1125 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1132 static int service_verify(Service *s) {
1135 if (UNIT(s)->load_state != UNIT_LOADED)
1138 if (!s->exec_command[SERVICE_EXEC_START]) {
1139 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1143 if (s->type != SERVICE_ONESHOT &&
1144 s->exec_command[SERVICE_EXEC_START]->command_next) {
1145 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1149 if (s->type == SERVICE_ONESHOT &&
1150 s->exec_command[SERVICE_EXEC_RELOAD]) {
1151 log_error("%s has an ExecReload setting, which is not allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1155 if (s->type == SERVICE_DBUS && !s->bus_name) {
1156 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1160 if (s->bus_name && s->type != SERVICE_DBUS)
1161 log_warning("%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1163 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1164 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1171 static int service_add_default_dependencies(Service *s) {
1176 /* Add a number of automatic dependencies useful for the
1177 * majority of services. */
1179 /* First, pull in base system */
1180 if (UNIT(s)->manager->running_as == MANAGER_SYSTEM) {
1182 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1185 } else if (UNIT(s)->manager->running_as == MANAGER_USER) {
1187 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1191 /* Second, activate normal shutdown */
1192 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1195 static void service_fix_output(Service *s) {
1198 /* If nothing has been explicitly configured, patch default
1199 * output in. If input is socket/tty we avoid this however,
1200 * since in that case we want output to default to the same
1201 * place as we read input from. */
1203 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1204 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1205 s->exec_context.std_input == EXEC_INPUT_NULL)
1206 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1208 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1209 s->exec_context.std_input == EXEC_INPUT_NULL)
1210 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1213 static int service_load(Unit *u) {
1215 Service *s = SERVICE(u);
1219 /* Load a .service file */
1220 if ((r = unit_load_fragment(u)) < 0)
1223 #ifdef HAVE_SYSV_COMPAT
1224 /* Load a classic init script as a fallback, if we couldn't find anything */
1225 if (u->load_state == UNIT_STUB)
1226 if ((r = service_load_sysv(s)) < 0)
1230 /* Still nothing found? Then let's give up */
1231 if (u->load_state == UNIT_STUB)
1234 /* We were able to load something, then let's add in the
1235 * dropin directories. */
1236 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1239 /* This is a new unit? Then let's add in some extras */
1240 if (u->load_state == UNIT_LOADED) {
1241 if (s->type == _SERVICE_TYPE_INVALID)
1242 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1244 /* Oneshot services have disabled timeout by default */
1245 if (s->type == SERVICE_ONESHOT && !s->timeout_defined)
1246 s->timeout_usec = 0;
1248 service_fix_output(s);
1250 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1253 if ((r = unit_add_default_cgroups(u)) < 0)
1256 #ifdef HAVE_SYSV_COMPAT
1257 if ((r = sysv_fix_order(s)) < 0)
1261 if ((r = fsck_fix_order(s)) < 0)
1265 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1268 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1269 s->notify_access = NOTIFY_MAIN;
1271 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1272 s->notify_access = NOTIFY_MAIN;
1274 if (s->type == SERVICE_DBUS || s->bus_name)
1275 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1278 if (UNIT(s)->default_dependencies)
1279 if ((r = service_add_default_dependencies(s)) < 0)
1282 r = unit_exec_context_defaults(u, &s->exec_context);
1287 return service_verify(s);
1290 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1292 ServiceExecCommand c;
1293 Service *s = SERVICE(u);
1294 const char *prefix2;
1299 p2 = strappend(prefix, "\t");
1300 prefix2 = p2 ? p2 : prefix;
1303 "%sService State: %s\n"
1305 "%sReload Result: %s\n"
1306 "%sPermissionsStartOnly: %s\n"
1307 "%sRootDirectoryStartOnly: %s\n"
1308 "%sRemainAfterExit: %s\n"
1309 "%sGuessMainPID: %s\n"
1312 "%sNotifyAccess: %s\n",
1313 prefix, service_state_to_string(s->state),
1314 prefix, service_result_to_string(s->result),
1315 prefix, service_result_to_string(s->reload_result),
1316 prefix, yes_no(s->permissions_start_only),
1317 prefix, yes_no(s->root_directory_start_only),
1318 prefix, yes_no(s->remain_after_exit),
1319 prefix, yes_no(s->guess_main_pid),
1320 prefix, service_type_to_string(s->type),
1321 prefix, service_restart_to_string(s->restart),
1322 prefix, notify_access_to_string(s->notify_access));
1324 if (s->control_pid > 0)
1326 "%sControl PID: %lu\n",
1327 prefix, (unsigned long) s->control_pid);
1329 if (s->main_pid > 0)
1332 "%sMain PID Known: %s\n"
1333 "%sMain PID Alien: %s\n",
1334 prefix, (unsigned long) s->main_pid,
1335 prefix, yes_no(s->main_pid_known),
1336 prefix, yes_no(s->main_pid_alien));
1341 prefix, s->pid_file);
1346 "%sBus Name Good: %s\n",
1347 prefix, s->bus_name,
1348 prefix, yes_no(s->bus_name_good));
1350 kill_context_dump(&s->kill_context, f, prefix);
1351 exec_context_dump(&s->exec_context, f, prefix);
1353 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1355 if (!s->exec_command[c])
1358 fprintf(f, "%s-> %s:\n",
1359 prefix, service_exec_command_to_string(c));
1361 exec_command_dump_list(s->exec_command[c], f, prefix2);
1364 #ifdef HAVE_SYSV_COMPAT
1367 "%sSysV Init Script has LSB Header: %s\n"
1368 "%sSysVEnabled: %s\n",
1369 prefix, yes_no(s->sysv_has_lsb),
1370 prefix, yes_no(s->sysv_enabled));
1372 if (s->sysv_start_priority >= 0)
1374 "%sSysVStartPriority: %i\n",
1375 prefix, s->sysv_start_priority);
1377 if (s->sysv_runlevels)
1378 fprintf(f, "%sSysVRunLevels: %s\n",
1379 prefix, s->sysv_runlevels);
1382 if (s->fsck_passno > 0)
1384 "%sFsckPassNo: %i\n",
1385 prefix, s->fsck_passno);
1388 fprintf(f, "%sStatus Text: %s\n",
1389 prefix, s->status_text);
1394 static int service_load_pid_file(Service *s, bool may_warn) {
1404 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1406 log_info("PID file %s not readable (yet?) after %s.",
1407 s->pid_file, service_state_to_string(s->state));
1411 r = parse_pid(k, &pid);
1417 if (kill(pid, 0) < 0 && errno != EPERM) {
1419 log_info("PID %lu read from file %s does not exist.",
1420 (unsigned long) pid, s->pid_file);
1424 if (s->main_pid_known) {
1425 if (pid == s->main_pid)
1428 log_debug("Main PID changing: %lu -> %lu",
1429 (unsigned long) s->main_pid, (unsigned long) pid);
1430 service_unwatch_main_pid(s);
1431 s->main_pid_known = false;
1433 log_debug("Main PID loaded: %lu", (unsigned long) pid);
1435 if ((r = service_set_main_pid(s, pid)) < 0)
1438 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1439 /* FIXME: we need to do something here */
1445 static int service_search_main_pid(Service *s) {
1451 /* If we know it anyway, don't ever fallback to unreliable
1453 if (s->main_pid_known)
1456 if (!s->guess_main_pid)
1459 assert(s->main_pid <= 0);
1461 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1464 log_debug("Main PID guessed: %lu", (unsigned long) pid);
1465 if ((r = service_set_main_pid(s, pid)) < 0)
1468 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1469 /* FIXME: we need to do something here */
1475 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1481 /* Notifies all our sockets when we die */
1483 if (s->socket_fd >= 0)
1486 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1487 if (u->type == UNIT_SOCKET)
1488 socket_notify_service_dead(SOCKET(u), failed_permanent);
1493 static void service_set_state(Service *s, ServiceState state) {
1494 ServiceState old_state;
1495 const UnitActiveState *table;
1498 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1500 old_state = s->state;
1503 service_unwatch_pid_file(s);
1505 if (state != SERVICE_START_PRE &&
1506 state != SERVICE_START &&
1507 state != SERVICE_START_POST &&
1508 state != SERVICE_RELOAD &&
1509 state != SERVICE_STOP &&
1510 state != SERVICE_STOP_SIGTERM &&
1511 state != SERVICE_STOP_SIGKILL &&
1512 state != SERVICE_STOP_POST &&
1513 state != SERVICE_FINAL_SIGTERM &&
1514 state != SERVICE_FINAL_SIGKILL &&
1515 state != SERVICE_AUTO_RESTART)
1516 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1518 if (state != SERVICE_START &&
1519 state != SERVICE_START_POST &&
1520 state != SERVICE_RUNNING &&
1521 state != SERVICE_RELOAD &&
1522 state != SERVICE_STOP &&
1523 state != SERVICE_STOP_SIGTERM &&
1524 state != SERVICE_STOP_SIGKILL) {
1525 service_unwatch_main_pid(s);
1526 s->main_command = NULL;
1529 if (state != SERVICE_START_PRE &&
1530 state != SERVICE_START &&
1531 state != SERVICE_START_POST &&
1532 state != SERVICE_RELOAD &&
1533 state != SERVICE_STOP &&
1534 state != SERVICE_STOP_SIGTERM &&
1535 state != SERVICE_STOP_SIGKILL &&
1536 state != SERVICE_STOP_POST &&
1537 state != SERVICE_FINAL_SIGTERM &&
1538 state != SERVICE_FINAL_SIGKILL) {
1539 service_unwatch_control_pid(s);
1540 s->control_command = NULL;
1541 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1544 if (state == SERVICE_DEAD ||
1545 state == SERVICE_STOP ||
1546 state == SERVICE_STOP_SIGTERM ||
1547 state == SERVICE_STOP_SIGKILL ||
1548 state == SERVICE_STOP_POST ||
1549 state == SERVICE_FINAL_SIGTERM ||
1550 state == SERVICE_FINAL_SIGKILL ||
1551 state == SERVICE_FAILED ||
1552 state == SERVICE_AUTO_RESTART)
1553 service_notify_sockets_dead(s, false);
1555 if (state != SERVICE_START_PRE &&
1556 state != SERVICE_START &&
1557 state != SERVICE_START_POST &&
1558 state != SERVICE_RUNNING &&
1559 state != SERVICE_RELOAD &&
1560 state != SERVICE_STOP &&
1561 state != SERVICE_STOP_SIGTERM &&
1562 state != SERVICE_STOP_SIGKILL &&
1563 state != SERVICE_STOP_POST &&
1564 state != SERVICE_FINAL_SIGTERM &&
1565 state != SERVICE_FINAL_SIGKILL &&
1566 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1567 service_close_socket_fd(s);
1568 service_connection_unref(s);
1571 if (state == SERVICE_STOP)
1572 service_stop_watchdog(s);
1574 /* For the inactive states unit_notify() will trim the cgroup,
1575 * but for exit we have to do that ourselves... */
1576 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1577 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1579 if (old_state != state)
1580 log_debug("%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
1582 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1583 s->reload_result = SERVICE_SUCCESS;
1586 static int service_coldplug(Unit *u) {
1587 Service *s = SERVICE(u);
1591 assert(s->state == SERVICE_DEAD);
1593 if (s->deserialized_state != s->state) {
1595 if (s->deserialized_state == SERVICE_START_PRE ||
1596 s->deserialized_state == SERVICE_START ||
1597 s->deserialized_state == SERVICE_START_POST ||
1598 s->deserialized_state == SERVICE_RELOAD ||
1599 s->deserialized_state == SERVICE_STOP ||
1600 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1601 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1602 s->deserialized_state == SERVICE_STOP_POST ||
1603 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1604 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1605 s->deserialized_state == SERVICE_AUTO_RESTART) {
1607 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1610 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1612 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1617 if ((s->deserialized_state == SERVICE_START &&
1618 (s->type == SERVICE_FORKING ||
1619 s->type == SERVICE_DBUS ||
1620 s->type == SERVICE_ONESHOT ||
1621 s->type == SERVICE_NOTIFY)) ||
1622 s->deserialized_state == SERVICE_START_POST ||
1623 s->deserialized_state == SERVICE_RUNNING ||
1624 s->deserialized_state == SERVICE_RELOAD ||
1625 s->deserialized_state == SERVICE_STOP ||
1626 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1627 s->deserialized_state == SERVICE_STOP_SIGKILL)
1628 if (s->main_pid > 0)
1629 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1632 if (s->deserialized_state == SERVICE_START_PRE ||
1633 s->deserialized_state == SERVICE_START ||
1634 s->deserialized_state == SERVICE_START_POST ||
1635 s->deserialized_state == SERVICE_RELOAD ||
1636 s->deserialized_state == SERVICE_STOP ||
1637 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1638 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1639 s->deserialized_state == SERVICE_STOP_POST ||
1640 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1641 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1642 if (s->control_pid > 0)
1643 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1646 if (s->deserialized_state == SERVICE_START_POST ||
1647 s->deserialized_state == SERVICE_RUNNING)
1648 service_handle_watchdog(s);
1650 service_set_state(s, s->deserialized_state);
1655 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1659 unsigned rn_fds = 0;
1666 if (s->socket_fd >= 0)
1669 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1674 if (u->type != UNIT_SOCKET)
1679 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1691 if (!(t = new(int, rn_fds+cn_fds))) {
1697 memcpy(t, rfds, rn_fds * sizeof(int));
1698 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1703 rn_fds = rn_fds+cn_fds;
1718 static int service_spawn(
1723 bool apply_permissions,
1725 bool apply_tty_stdin,
1726 bool set_notify_socket,
1732 int *fds = NULL, *fdsbuf = NULL;
1733 unsigned n_fds = 0, n_env = 0;
1734 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1741 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1742 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1743 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1745 if (s->socket_fd >= 0) {
1746 fds = &s->socket_fd;
1749 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1756 if (timeout && s->timeout_usec) {
1757 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1760 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1762 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1767 if (!(our_env = new0(char*, 4))) {
1772 if (set_notify_socket)
1773 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1778 if (s->main_pid > 0)
1779 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1784 if (s->watchdog_usec > 0)
1785 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1790 if (!(final_env = strv_env_merge(2,
1791 UNIT(s)->manager->environment,
1806 UNIT(s)->manager->confirm_spawn,
1807 UNIT(s)->cgroup_bondings,
1808 UNIT(s)->cgroup_attributes,
1809 is_control ? "control" : NULL,
1811 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1818 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1819 /* FIXME: we need to do something here */
1825 strv_free(final_env);
1835 strv_free(final_env);
1838 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1843 static int main_pid_good(Service *s) {
1846 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1849 /* If we know the pid file, then lets just check if it is
1851 if (s->main_pid_known) {
1853 /* If it's an alien child let's check if it is still
1855 if (s->main_pid_alien)
1856 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1858 /* .. otherwise assume we'll get a SIGCHLD for it,
1859 * which we really should wait for to collect exit
1860 * status and code */
1861 return s->main_pid > 0;
1864 /* We don't know the pid */
1868 static int control_pid_good(Service *s) {
1871 return s->control_pid > 0;
1874 static int cgroup_good(Service *s) {
1879 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1885 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1889 if (f != SERVICE_SUCCESS)
1892 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1894 if (allow_restart &&
1895 !s->forbid_restart &&
1896 (s->restart == SERVICE_RESTART_ALWAYS ||
1897 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1898 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1899 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1900 s->result == SERVICE_FAILURE_CORE_DUMP)))) {
1902 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
1906 service_set_state(s, SERVICE_AUTO_RESTART);
1909 s->forbid_restart = false;
1914 log_warning("%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1915 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1918 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1920 static void service_enter_stop_post(Service *s, ServiceResult f) {
1924 if (f != SERVICE_SUCCESS)
1927 service_unwatch_control_pid(s);
1929 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1930 s->control_command_id = SERVICE_EXEC_STOP_POST;
1932 r = service_spawn(s,
1936 !s->permissions_start_only,
1937 !s->root_directory_start_only,
1946 service_set_state(s, SERVICE_STOP_POST);
1948 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1953 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
1954 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1957 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1959 Set *pid_set = NULL;
1960 bool wait_for_exit = false;
1964 if (f != SERVICE_SUCCESS)
1967 if (s->kill_context.kill_mode != KILL_NONE) {
1968 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->kill_context.kill_signal : SIGKILL;
1970 if (s->main_pid > 0) {
1971 if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
1972 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1974 wait_for_exit = !s->main_pid_alien;
1977 if (s->control_pid > 0) {
1978 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1979 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1981 wait_for_exit = true;
1984 if (s->kill_context.kill_mode == KILL_CONTROL_GROUP) {
1986 pid_set = set_new(trivial_hash_func, trivial_compare_func);
1992 /* Exclude the main/control pids from being killed via the cgroup */
1993 if (s->main_pid > 0)
1994 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1997 if (s->control_pid > 0)
1998 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
2001 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
2003 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
2004 log_warning("Failed to kill control group: %s", strerror(-r));
2006 wait_for_exit = true;
2013 if (wait_for_exit) {
2014 if (s->timeout_usec > 0)
2015 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
2018 service_set_state(s, state);
2019 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2020 service_enter_stop_post(s, SERVICE_SUCCESS);
2022 service_enter_dead(s, SERVICE_SUCCESS, true);
2027 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2029 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2030 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2032 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2038 static void service_enter_stop(Service *s, ServiceResult f) {
2043 if (f != SERVICE_SUCCESS)
2046 service_unwatch_control_pid(s);
2048 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2049 s->control_command_id = SERVICE_EXEC_STOP;
2051 r = service_spawn(s,
2055 !s->permissions_start_only,
2056 !s->root_directory_start_only,
2064 service_set_state(s, SERVICE_STOP);
2066 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2071 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2072 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2075 static void service_enter_running(Service *s, ServiceResult f) {
2076 int main_pid_ok, cgroup_ok;
2079 if (f != SERVICE_SUCCESS)
2082 main_pid_ok = main_pid_good(s);
2083 cgroup_ok = cgroup_good(s);
2085 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2086 (s->bus_name_good || s->type != SERVICE_DBUS))
2087 service_set_state(s, SERVICE_RUNNING);
2088 else if (s->remain_after_exit)
2089 service_set_state(s, SERVICE_EXITED);
2091 service_enter_stop(s, SERVICE_SUCCESS);
2094 static void service_enter_start_post(Service *s) {
2098 service_unwatch_control_pid(s);
2100 if (s->watchdog_usec > 0)
2101 service_reset_watchdog(s);
2103 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2104 s->control_command_id = SERVICE_EXEC_START_POST;
2106 r = service_spawn(s,
2110 !s->permissions_start_only,
2111 !s->root_directory_start_only,
2119 service_set_state(s, SERVICE_START_POST);
2121 service_enter_running(s, SERVICE_SUCCESS);
2126 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2127 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2130 static void service_enter_start(Service *s) {
2137 assert(s->exec_command[SERVICE_EXEC_START]);
2138 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2140 if (s->type == SERVICE_FORKING)
2141 service_unwatch_control_pid(s);
2143 service_unwatch_main_pid(s);
2145 /* We want to ensure that nobody leaks processes from
2146 * START_PRE here, so let's go on a killing spree, People
2147 * should not spawn long running processes from START_PRE. */
2148 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2150 if (s->type == SERVICE_FORKING) {
2151 s->control_command_id = SERVICE_EXEC_START;
2152 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2154 s->main_command = NULL;
2156 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2157 s->control_command = NULL;
2159 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2162 r = service_spawn(s,
2164 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2169 s->notify_access != NOTIFY_NONE,
2175 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2176 /* For simple services we immediately start
2177 * the START_POST binaries. */
2179 service_set_main_pid(s, pid);
2180 service_enter_start_post(s);
2182 } else if (s->type == SERVICE_FORKING) {
2184 /* For forking services we wait until the start
2185 * process exited. */
2187 s->control_pid = pid;
2188 service_set_state(s, SERVICE_START);
2190 } else if (s->type == SERVICE_ONESHOT ||
2191 s->type == SERVICE_DBUS ||
2192 s->type == SERVICE_NOTIFY) {
2194 /* For oneshot services we wait until the start
2195 * process exited, too, but it is our main process. */
2197 /* For D-Bus services we know the main pid right away,
2198 * but wait for the bus name to appear on the
2199 * bus. Notify services are similar. */
2201 service_set_main_pid(s, pid);
2202 service_set_state(s, SERVICE_START);
2204 assert_not_reached("Unknown service type");
2209 log_warning("%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2210 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2213 static void service_enter_start_pre(Service *s) {
2218 service_unwatch_control_pid(s);
2220 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2222 /* Before we start anything, let's clear up what might
2223 * be left from previous runs. */
2224 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2226 s->control_command_id = SERVICE_EXEC_START_PRE;
2228 r = service_spawn(s,
2232 !s->permissions_start_only,
2233 !s->root_directory_start_only,
2241 service_set_state(s, SERVICE_START_PRE);
2243 service_enter_start(s);
2248 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2249 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2252 static void service_enter_restart(Service *s) {
2257 dbus_error_init(&error);
2259 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2260 /* Don't restart things if we are going down anyway */
2261 log_info("Stop job pending for unit, delaying automatic restart.");
2263 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
2270 /* Any units that are bound to this service must also be
2271 * restarted. We use JOB_RESTART (instead of the more obvious
2272 * JOB_START) here so that those dependency jobs will be added
2274 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2278 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2279 * it will be canceled as part of the service_stop() call that
2280 * is executed as part of JOB_RESTART. */
2282 log_debug("%s scheduled restart job.", UNIT(s)->id);
2286 log_warning("%s failed to schedule restart job: %s", UNIT(s)->id, bus_error(&error, -r));
2287 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2289 dbus_error_free(&error);
2292 static void service_enter_reload(Service *s) {
2297 service_unwatch_control_pid(s);
2299 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2300 s->control_command_id = SERVICE_EXEC_RELOAD;
2302 r = service_spawn(s,
2306 !s->permissions_start_only,
2307 !s->root_directory_start_only,
2315 service_set_state(s, SERVICE_RELOAD);
2317 service_enter_running(s, SERVICE_SUCCESS);
2322 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
2323 s->reload_result = SERVICE_FAILURE_RESOURCES;
2324 service_enter_running(s, SERVICE_SUCCESS);
2327 static void service_run_next_control(Service *s) {
2331 assert(s->control_command);
2332 assert(s->control_command->command_next);
2334 assert(s->control_command_id != SERVICE_EXEC_START);
2336 s->control_command = s->control_command->command_next;
2337 service_unwatch_control_pid(s);
2339 r = service_spawn(s,
2343 !s->permissions_start_only,
2344 !s->root_directory_start_only,
2345 s->control_command_id == SERVICE_EXEC_START_PRE ||
2346 s->control_command_id == SERVICE_EXEC_STOP_POST,
2356 log_warning("%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
2358 if (s->state == SERVICE_START_PRE)
2359 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2360 else if (s->state == SERVICE_STOP)
2361 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2362 else if (s->state == SERVICE_STOP_POST)
2363 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2364 else if (s->state == SERVICE_RELOAD) {
2365 s->reload_result = SERVICE_FAILURE_RESOURCES;
2366 service_enter_running(s, SERVICE_SUCCESS);
2368 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2371 static void service_run_next_main(Service *s) {
2376 assert(s->main_command);
2377 assert(s->main_command->command_next);
2378 assert(s->type == SERVICE_ONESHOT);
2380 s->main_command = s->main_command->command_next;
2381 service_unwatch_main_pid(s);
2383 r = service_spawn(s,
2390 s->notify_access != NOTIFY_NONE,
2396 service_set_main_pid(s, pid);
2401 log_warning("%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2402 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2405 static int service_start_limit_test(Service *s) {
2408 if (ratelimit_test(&s->start_limit))
2411 switch (s->start_limit_action) {
2413 case SERVICE_START_LIMIT_NONE:
2414 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s)->id);
2417 case SERVICE_START_LIMIT_REBOOT: {
2421 dbus_error_init(&error);
2423 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2425 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2427 log_error("Failed to reboot: %s.", bus_error(&error, r));
2428 dbus_error_free(&error);
2434 case SERVICE_START_LIMIT_REBOOT_FORCE:
2435 log_warning("%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2436 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2439 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2440 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2441 reboot(RB_AUTOBOOT);
2445 log_error("start limit action=%i", s->start_limit_action);
2446 assert_not_reached("Unknown StartLimitAction.");
2452 static int service_start(Unit *u) {
2453 Service *s = SERVICE(u);
2458 /* We cannot fulfill this request right now, try again later
2460 if (s->state == SERVICE_STOP ||
2461 s->state == SERVICE_STOP_SIGTERM ||
2462 s->state == SERVICE_STOP_SIGKILL ||
2463 s->state == SERVICE_STOP_POST ||
2464 s->state == SERVICE_FINAL_SIGTERM ||
2465 s->state == SERVICE_FINAL_SIGKILL)
2468 /* Already on it! */
2469 if (s->state == SERVICE_START_PRE ||
2470 s->state == SERVICE_START ||
2471 s->state == SERVICE_START_POST)
2474 /* A service that will be restarted must be stopped first to
2475 * trigger BindsTo and/or OnFailure dependencies. If a user
2476 * does not want to wait for the holdoff time to elapse, the
2477 * service should be manually restarted, not started. We
2478 * simply return EAGAIN here, so that any start jobs stay
2479 * queued, and assume that the auto restart timer will
2480 * eventually trigger the restart. */
2481 if (s->state == SERVICE_AUTO_RESTART)
2484 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2486 /* Make sure we don't enter a busy loop of some kind. */
2487 r = service_start_limit_test(s);
2489 service_notify_sockets_dead(s, true);
2493 s->result = SERVICE_SUCCESS;
2494 s->reload_result = SERVICE_SUCCESS;
2495 s->main_pid_known = false;
2496 s->main_pid_alien = false;
2497 s->forbid_restart = false;
2499 service_enter_start_pre(s);
2503 static int service_stop(Unit *u) {
2504 Service *s = SERVICE(u);
2508 /* Don't create restart jobs from here. */
2509 s->forbid_restart = true;
2512 if (s->state == SERVICE_STOP ||
2513 s->state == SERVICE_STOP_SIGTERM ||
2514 s->state == SERVICE_STOP_SIGKILL ||
2515 s->state == SERVICE_STOP_POST ||
2516 s->state == SERVICE_FINAL_SIGTERM ||
2517 s->state == SERVICE_FINAL_SIGKILL)
2520 /* A restart will be scheduled or is in progress. */
2521 if (s->state == SERVICE_AUTO_RESTART) {
2522 service_set_state(s, SERVICE_DEAD);
2526 /* If there's already something running we go directly into
2528 if (s->state == SERVICE_START_PRE ||
2529 s->state == SERVICE_START ||
2530 s->state == SERVICE_START_POST ||
2531 s->state == SERVICE_RELOAD) {
2532 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2536 assert(s->state == SERVICE_RUNNING ||
2537 s->state == SERVICE_EXITED);
2539 service_enter_stop(s, SERVICE_SUCCESS);
2543 static int service_reload(Unit *u) {
2544 Service *s = SERVICE(u);
2548 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2550 service_enter_reload(s);
2554 static bool service_can_reload(Unit *u) {
2555 Service *s = SERVICE(u);
2559 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2562 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2563 Service *s = SERVICE(u);
2569 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2570 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2571 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2573 if (s->control_pid > 0)
2574 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2576 if (s->main_pid_known && s->main_pid > 0)
2577 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2579 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2582 unit_serialize_item(u, f, "status-text", s->status_text);
2584 /* FIXME: There's a minor uncleanliness here: if there are
2585 * multiple commands attached here, we will start from the
2586 * first one again */
2587 if (s->control_command_id >= 0)
2588 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2590 if (s->socket_fd >= 0) {
2593 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2596 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2599 if (s->main_exec_status.pid > 0) {
2600 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2601 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2602 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2604 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2605 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2606 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2609 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2610 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2615 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2616 Service *s = SERVICE(u);
2623 if (streq(key, "state")) {
2626 if ((state = service_state_from_string(value)) < 0)
2627 log_debug("Failed to parse state value %s", value);
2629 s->deserialized_state = state;
2630 } else if (streq(key, "result")) {
2633 f = service_result_from_string(value);
2635 log_debug("Failed to parse result value %s", value);
2636 else if (f != SERVICE_SUCCESS)
2639 } else if (streq(key, "reload-result")) {
2642 f = service_result_from_string(value);
2644 log_debug("Failed to parse reload result value %s", value);
2645 else if (f != SERVICE_SUCCESS)
2646 s->reload_result = f;
2648 } else if (streq(key, "control-pid")) {
2651 if (parse_pid(value, &pid) < 0)
2652 log_debug("Failed to parse control-pid value %s", value);
2654 s->control_pid = pid;
2655 } else if (streq(key, "main-pid")) {
2658 if (parse_pid(value, &pid) < 0)
2659 log_debug("Failed to parse main-pid value %s", value);
2661 service_set_main_pid(s, (pid_t) pid);
2662 } else if (streq(key, "main-pid-known")) {
2665 if ((b = parse_boolean(value)) < 0)
2666 log_debug("Failed to parse main-pid-known value %s", value);
2668 s->main_pid_known = b;
2669 } else if (streq(key, "status-text")) {
2672 if ((t = strdup(value))) {
2673 free(s->status_text);
2677 } else if (streq(key, "control-command")) {
2678 ServiceExecCommand id;
2680 if ((id = service_exec_command_from_string(value)) < 0)
2681 log_debug("Failed to parse exec-command value %s", value);
2683 s->control_command_id = id;
2684 s->control_command = s->exec_command[id];
2686 } else if (streq(key, "socket-fd")) {
2689 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2690 log_debug("Failed to parse socket-fd value %s", value);
2693 if (s->socket_fd >= 0)
2694 close_nointr_nofail(s->socket_fd);
2695 s->socket_fd = fdset_remove(fds, fd);
2697 } else if (streq(key, "main-exec-status-pid")) {
2700 if (parse_pid(value, &pid) < 0)
2701 log_debug("Failed to parse main-exec-status-pid value %s", value);
2703 s->main_exec_status.pid = pid;
2704 } else if (streq(key, "main-exec-status-code")) {
2707 if (safe_atoi(value, &i) < 0)
2708 log_debug("Failed to parse main-exec-status-code value %s", value);
2710 s->main_exec_status.code = i;
2711 } else if (streq(key, "main-exec-status-status")) {
2714 if (safe_atoi(value, &i) < 0)
2715 log_debug("Failed to parse main-exec-status-status value %s", value);
2717 s->main_exec_status.status = i;
2718 } else if (streq(key, "main-exec-status-start"))
2719 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2720 else if (streq(key, "main-exec-status-exit"))
2721 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2722 else if (streq(key, "watchdog-timestamp"))
2723 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2725 log_debug("Unknown serialization key '%s'", key);
2730 static UnitActiveState service_active_state(Unit *u) {
2731 const UnitActiveState *table;
2735 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2737 return table[SERVICE(u)->state];
2740 static const char *service_sub_state_to_string(Unit *u) {
2743 return service_state_to_string(SERVICE(u)->state);
2746 static bool service_check_gc(Unit *u) {
2747 Service *s = SERVICE(u);
2751 /* Never clean up services that still have a process around,
2752 * even if the service is formally dead. */
2753 if (cgroup_good(s) > 0 ||
2754 main_pid_good(s) > 0 ||
2755 control_pid_good(s) > 0)
2758 #ifdef HAVE_SYSV_COMPAT
2766 static bool service_check_snapshot(Unit *u) {
2767 Service *s = SERVICE(u);
2771 return !s->got_socket_fd;
2774 static int service_retry_pid_file(Service *s) {
2777 assert(s->pid_file);
2778 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2780 r = service_load_pid_file(s, false);
2784 service_unwatch_pid_file(s);
2786 service_enter_running(s, SERVICE_SUCCESS);
2790 static int service_watch_pid_file(Service *s) {
2793 log_debug("Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
2794 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2798 /* the pidfile might have appeared just before we set the watch */
2799 service_retry_pid_file(s);
2803 log_error("Failed to set a watch for %s's PID file %s: %s",
2804 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2805 service_unwatch_pid_file(s);
2809 static int service_demand_pid_file(Service *s) {
2812 assert(s->pid_file);
2813 assert(!s->pid_file_pathspec);
2815 ps = new0(PathSpec, 1);
2819 ps->path = strdup(s->pid_file);
2825 path_kill_slashes(ps->path);
2827 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2828 * keep their PID file open all the time. */
2829 ps->type = PATH_MODIFIED;
2830 ps->inotify_fd = -1;
2832 s->pid_file_pathspec = ps;
2834 return service_watch_pid_file(s);
2837 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2838 Service *s = SERVICE(u);
2842 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2843 assert(s->pid_file_pathspec);
2844 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2846 log_debug("inotify event for %s", u->id);
2848 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2851 if (service_retry_pid_file(s) == 0)
2854 if (service_watch_pid_file(s) < 0)
2859 service_unwatch_pid_file(s);
2860 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2863 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2864 Service *s = SERVICE(u);
2870 if (UNIT(s)->fragment_path ? is_clean_exit(code, status) : is_clean_exit_lsb(code, status))
2871 f = SERVICE_SUCCESS;
2872 else if (code == CLD_EXITED)
2873 f = SERVICE_FAILURE_EXIT_CODE;
2874 else if (code == CLD_KILLED)
2875 f = SERVICE_FAILURE_SIGNAL;
2876 else if (code == CLD_DUMPED)
2877 f = SERVICE_FAILURE_CORE_DUMP;
2879 assert_not_reached("Unknown code");
2881 if (s->main_pid == pid) {
2882 /* Forking services may occasionally move to a new PID.
2883 * As long as they update the PID file before exiting the old
2884 * PID, they're fine. */
2885 if (service_load_pid_file(s, false) == 0)
2889 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2891 /* If this is not a forking service than the main
2892 * process got started and hence we copy the exit
2893 * status so that it is recorded both as main and as
2894 * control process exit status */
2895 if (s->main_command) {
2896 s->main_command->exec_status = s->main_exec_status;
2898 if (s->main_command->ignore)
2899 f = SERVICE_SUCCESS;
2902 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2903 "%s: main process exited, code=%s, status=%i", u->id, sigchld_code_to_string(code), status);
2905 if (f != SERVICE_SUCCESS)
2908 if (s->main_command &&
2909 s->main_command->command_next &&
2910 f == SERVICE_SUCCESS) {
2912 /* There is another command to *
2913 * execute, so let's do that. */
2915 log_debug("%s running next main command for state %s", u->id, service_state_to_string(s->state));
2916 service_run_next_main(s);
2920 /* The service exited, so the service is officially
2922 s->main_command = NULL;
2926 case SERVICE_START_POST:
2927 case SERVICE_RELOAD:
2929 /* Need to wait until the operation is
2934 if (s->type == SERVICE_ONESHOT) {
2935 /* This was our main goal, so let's go on */
2936 if (f == SERVICE_SUCCESS)
2937 service_enter_start_post(s);
2939 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2942 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2947 case SERVICE_RUNNING:
2948 service_enter_running(s, f);
2951 case SERVICE_STOP_SIGTERM:
2952 case SERVICE_STOP_SIGKILL:
2954 if (!control_pid_good(s))
2955 service_enter_stop_post(s, f);
2957 /* If there is still a control process, wait for that first */
2961 assert_not_reached("Uh, main process died at wrong time.");
2965 } else if (s->control_pid == pid) {
2969 if (s->control_command) {
2970 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2972 if (s->control_command->ignore)
2973 f = SERVICE_SUCCESS;
2976 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2977 "%s: control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
2979 if (f != SERVICE_SUCCESS)
2982 /* Immediately get rid of the cgroup, so that the
2983 * kernel doesn't delay the cgroup empty messages for
2984 * the service cgroup any longer than necessary */
2985 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2987 if (s->control_command &&
2988 s->control_command->command_next &&
2989 f == SERVICE_SUCCESS) {
2991 /* There is another command to *
2992 * execute, so let's do that. */
2994 log_debug("%s running next control command for state %s", u->id, service_state_to_string(s->state));
2995 service_run_next_control(s);
2998 /* No further commands for this step, so let's
2999 * figure out what to do next */
3001 s->control_command = NULL;
3002 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3004 log_debug("%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
3008 case SERVICE_START_PRE:
3009 if (f == SERVICE_SUCCESS)
3010 service_enter_start(s);
3012 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3016 assert(s->type == SERVICE_FORKING);
3018 if (f != SERVICE_SUCCESS) {
3019 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3024 bool has_start_post;
3027 /* Let's try to load the pid file here if we can.
3028 * The PID file might actually be created by a START_POST
3029 * script. In that case don't worry if the loading fails. */
3031 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3032 r = service_load_pid_file(s, !has_start_post);
3033 if (!has_start_post && r < 0) {
3034 r = service_demand_pid_file(s);
3035 if (r < 0 || !cgroup_good(s))
3036 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3040 service_search_main_pid(s);
3042 service_enter_start_post(s);
3045 case SERVICE_START_POST:
3046 if (f != SERVICE_SUCCESS) {
3047 service_enter_stop(s, f);
3054 r = service_load_pid_file(s, true);
3056 r = service_demand_pid_file(s);
3057 if (r < 0 || !cgroup_good(s))
3058 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3062 service_search_main_pid(s);
3064 service_enter_running(s, SERVICE_SUCCESS);
3067 case SERVICE_RELOAD:
3068 if (f == SERVICE_SUCCESS) {
3069 service_load_pid_file(s, true);
3070 service_search_main_pid(s);
3073 s->reload_result = f;
3074 service_enter_running(s, SERVICE_SUCCESS);
3078 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3081 case SERVICE_STOP_SIGTERM:
3082 case SERVICE_STOP_SIGKILL:
3083 if (main_pid_good(s) <= 0)
3084 service_enter_stop_post(s, f);
3086 /* If there is still a service
3087 * process around, wait until
3088 * that one quit, too */
3091 case SERVICE_STOP_POST:
3092 case SERVICE_FINAL_SIGTERM:
3093 case SERVICE_FINAL_SIGKILL:
3094 service_enter_dead(s, f, true);
3098 assert_not_reached("Uh, control process died at wrong time.");
3103 /* Notify clients about changed exit status */
3104 unit_add_to_dbus_queue(u);
3107 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3108 Service *s = SERVICE(u);
3111 assert(elapsed == 1);
3113 if (w == &s->watchdog_watch) {
3114 service_handle_watchdog(s);
3118 assert(w == &s->timer_watch);
3122 case SERVICE_START_PRE:
3124 log_warning("%s operation timed out. Terminating.", u->id);
3125 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3128 case SERVICE_START_POST:
3129 log_warning("%s operation timed out. Stopping.", u->id);
3130 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3133 case SERVICE_RELOAD:
3134 log_warning("%s operation timed out. Stopping.", u->id);
3135 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3136 service_enter_running(s, SERVICE_SUCCESS);
3140 log_warning("%s stopping timed out. Terminating.", u->id);
3141 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3144 case SERVICE_STOP_SIGTERM:
3145 if (s->kill_context.send_sigkill) {
3146 log_warning("%s stopping timed out. Killing.", u->id);
3147 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3149 log_warning("%s stopping timed out. Skipping SIGKILL.", u->id);
3150 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3155 case SERVICE_STOP_SIGKILL:
3156 /* Uh, we sent a SIGKILL and it is still not gone?
3157 * Must be something we cannot kill, so let's just be
3158 * weirded out and continue */
3160 log_warning("%s still around after SIGKILL. Ignoring.", u->id);
3161 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3164 case SERVICE_STOP_POST:
3165 log_warning("%s stopping timed out (2). Terminating.", u->id);
3166 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3169 case SERVICE_FINAL_SIGTERM:
3170 if (s->kill_context.send_sigkill) {
3171 log_warning("%s stopping timed out (2). Killing.", u->id);
3172 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3174 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->id);
3175 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3180 case SERVICE_FINAL_SIGKILL:
3181 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->id);
3182 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3185 case SERVICE_AUTO_RESTART:
3186 log_info("%s holdoff time over, scheduling restart.", u->id);
3187 service_enter_restart(s);
3191 assert_not_reached("Timeout at wrong time.");
3195 static void service_cgroup_notify_event(Unit *u) {
3196 Service *s = SERVICE(u);
3200 log_debug("%s: cgroup is empty", u->id);
3204 /* Waiting for SIGCHLD is usually more interesting,
3205 * because it includes return codes/signals. Which is
3206 * why we ignore the cgroup events for most cases,
3207 * except when we don't know pid which to expect the
3211 case SERVICE_START_POST:
3212 /* If we were hoping for the daemon to write its PID file,
3213 * we can give up now. */
3214 if (s->pid_file_pathspec) {
3215 log_warning("%s never wrote its PID file. Failing.", UNIT(s)->id);
3216 service_unwatch_pid_file(s);
3217 if (s->state == SERVICE_START)
3218 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3220 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3224 case SERVICE_RUNNING:
3225 /* service_enter_running() will figure out what to do */
3226 service_enter_running(s, SERVICE_SUCCESS);
3229 case SERVICE_STOP_SIGTERM:
3230 case SERVICE_STOP_SIGKILL:
3232 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3233 service_enter_stop_post(s, SERVICE_SUCCESS);
3237 case SERVICE_FINAL_SIGTERM:
3238 case SERVICE_FINAL_SIGKILL:
3239 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3240 service_enter_dead(s, SERVICE_SUCCESS, true);
3249 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3250 Service *s = SERVICE(u);
3255 if (s->notify_access == NOTIFY_NONE) {
3256 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3257 u->id, (unsigned long) pid);
3261 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3262 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3263 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3267 log_debug("%s: Got message", u->id);
3269 /* Interpret MAINPID= */
3270 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3271 (s->state == SERVICE_START ||
3272 s->state == SERVICE_START_POST ||
3273 s->state == SERVICE_RUNNING ||
3274 s->state == SERVICE_RELOAD)) {
3276 if (parse_pid(e + 8, &pid) < 0)
3277 log_warning("Failed to parse notification message %s", e);
3279 log_debug("%s: got %s", u->id, e);
3280 service_set_main_pid(s, pid);
3284 /* Interpret READY= */
3285 if (s->type == SERVICE_NOTIFY &&
3286 s->state == SERVICE_START &&
3287 strv_find(tags, "READY=1")) {
3288 log_debug("%s: got READY=1", u->id);
3290 service_enter_start_post(s);
3293 /* Interpret STATUS= */
3294 e = strv_find_prefix(tags, "STATUS=");
3300 if (!utf8_is_valid(e+7)) {
3301 log_warning("Status message in notification is not UTF-8 clean.");
3307 log_error("Failed to allocate string.");
3311 log_debug("%s: got %s", u->id, e);
3313 free(s->status_text);
3316 free(s->status_text);
3317 s->status_text = NULL;
3321 if (strv_find(tags, "WATCHDOG=1")) {
3322 log_debug("%s: got WATCHDOG=1", u->id);
3323 service_reset_watchdog(s);
3326 /* Notify clients about changed status or main pid */
3327 unit_add_to_dbus_queue(u);
3330 #ifdef HAVE_SYSV_COMPAT
3333 static void sysv_facility_in_insserv_conf(Manager *mgr) {
3337 if (!(f = fopen("/etc/insserv.conf", "re"))) {
3338 r = errno == ENOENT ? 0 : -errno;
3343 char l[LINE_MAX], *t;
3344 char **parsed = NULL;
3346 if (!fgets(l, sizeof(l), f)) {
3351 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r));
3356 if (*t != '$' && *t != '<')
3359 parsed = strv_split(t,WHITESPACE);
3360 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3361 if (parsed && !startswith_no_case (parsed[0], "<interactive>")) {
3364 if (sysv_translate_facility(parsed[0], NULL, &facility) < 0)
3366 if ((u = manager_get_unit(mgr, facility)) && (u->type == UNIT_TARGET)) {
3368 char *dep = NULL, *name, **j;
3370 STRV_FOREACH (j, parsed+1) {
3379 if (sysv_translate_facility(name, NULL, &dep) < 0)
3382 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, e, dep, NULL, true);
3397 static int service_enumerate(Manager *m) {
3401 char *path = NULL, *fpath = NULL, *name = NULL;
3402 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3409 if (m->running_as != MANAGER_SYSTEM)
3412 zero(runlevel_services);
3414 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3415 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3419 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3428 if (!(d = opendir(path))) {
3429 if (errno != ENOENT)
3430 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3435 while ((de = readdir(d))) {
3438 if (ignore_file(de->d_name))
3441 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3444 if (strlen(de->d_name) < 4)
3447 a = undecchar(de->d_name[1]);
3448 b = undecchar(de->d_name[2]);
3454 fpath = strjoin(path, "/", de->d_name, NULL);
3460 if (access(fpath, X_OK) < 0) {
3462 if (errno != ENOENT)
3463 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3469 if (!(name = sysv_translate_name(de->d_name + 3))) {
3474 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
3475 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3479 if (de->d_name[0] == 'S') {
3481 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
3482 SERVICE(service)->sysv_start_priority_from_rcnd =
3483 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3485 SERVICE(service)->sysv_enabled = true;
3488 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3491 if ((r = set_put(runlevel_services[i], service)) < 0)
3494 } else if (de->d_name[0] == 'K' &&
3495 (rcnd_table[i].type == RUNLEVEL_DOWN ||
3496 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
3498 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3501 if ((r = set_put(shutdown_services, service)) < 0)
3507 /* Now we loaded all stubs and are aware of the lowest
3508 start-up priority for all services, not let's actually load
3509 the services, this will also tell us which services are
3510 actually native now */
3511 manager_dispatch_load_queue(m);
3513 /* If this is a native service, rely on native ways to pull in
3514 * a service, don't pull it in via sysv rcN.d links. */
3515 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3516 SET_FOREACH(service, runlevel_services[i], j) {
3517 service = unit_follow_merge(service);
3519 if (service->fragment_path)
3522 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3526 /* We honour K links only for halt/reboot. For the normal
3527 * runlevels we assume the stop jobs will be implicitly added
3528 * by the core logic. Also, we don't really distinguish here
3529 * between the runlevels 0 and 6 and just add them to the
3530 * special shutdown target. On SUSE the boot.d/ runlevel is
3531 * also used for shutdown, so we add links for that too to the
3532 * shutdown target.*/
3533 SET_FOREACH(service, shutdown_services, j) {
3534 service = unit_follow_merge(service);
3536 if (service->fragment_path)
3539 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3546 sysv_facility_in_insserv_conf (m);
3554 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3555 set_free(runlevel_services[i]);
3556 set_free(shutdown_services);
3565 static void service_bus_name_owner_change(
3568 const char *old_owner,
3569 const char *new_owner) {
3571 Service *s = SERVICE(u);
3576 assert(streq(s->bus_name, name));
3577 assert(old_owner || new_owner);
3579 if (old_owner && new_owner)
3580 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
3582 log_debug("%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
3584 log_debug("%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
3586 s->bus_name_good = !!new_owner;
3588 if (s->type == SERVICE_DBUS) {
3590 /* service_enter_running() will figure out what to
3592 if (s->state == SERVICE_RUNNING)
3593 service_enter_running(s, SERVICE_SUCCESS);
3594 else if (s->state == SERVICE_START && new_owner)
3595 service_enter_start_post(s);
3597 } else if (new_owner &&
3599 (s->state == SERVICE_START ||
3600 s->state == SERVICE_START_POST ||
3601 s->state == SERVICE_RUNNING ||
3602 s->state == SERVICE_RELOAD)) {
3604 /* Try to acquire PID from bus service */
3605 log_debug("Trying to acquire PID from D-Bus name...");
3607 bus_query_pid(u->manager, name);
3611 static void service_bus_query_pid_done(
3616 Service *s = SERVICE(u);
3621 log_debug("%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3623 if (s->main_pid <= 0 &&
3624 (s->state == SERVICE_START ||
3625 s->state == SERVICE_START_POST ||
3626 s->state == SERVICE_RUNNING ||
3627 s->state == SERVICE_RELOAD))
3628 service_set_main_pid(s, pid);
3631 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3636 /* This is called by the socket code when instantiating a new
3637 * service for a stream socket and the socket needs to be
3640 if (UNIT(s)->load_state != UNIT_LOADED)
3643 if (s->socket_fd >= 0)
3646 if (s->state != SERVICE_DEAD)
3650 s->got_socket_fd = true;
3652 unit_ref_set(&s->accept_socket, UNIT(sock));
3654 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3657 static void service_reset_failed(Unit *u) {
3658 Service *s = SERVICE(u);
3662 if (s->state == SERVICE_FAILED)
3663 service_set_state(s, SERVICE_DEAD);
3665 s->result = SERVICE_SUCCESS;
3666 s->reload_result = SERVICE_SUCCESS;
3668 RATELIMIT_RESET(s->start_limit);
3671 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3672 Service *s = SERVICE(u);
3674 Set *pid_set = NULL;
3678 if (s->main_pid <= 0 && who == KILL_MAIN) {
3679 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3683 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3684 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3688 if (who == KILL_CONTROL || who == KILL_ALL)
3689 if (s->control_pid > 0)
3690 if (kill(s->control_pid, signo) < 0)
3693 if (who == KILL_MAIN || who == KILL_ALL)
3694 if (s->main_pid > 0)
3695 if (kill(s->main_pid, signo) < 0)
3698 if (who == KILL_ALL) {
3701 pid_set = set_new(trivial_hash_func, trivial_compare_func);
3705 /* Exclude the control/main pid from being killed via the cgroup */
3706 if (s->control_pid > 0) {
3707 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
3714 if (s->main_pid > 0) {
3715 q = set_put(pid_set, LONG_TO_PTR(s->main_pid));
3722 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3723 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3734 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3735 [SERVICE_DEAD] = "dead",
3736 [SERVICE_START_PRE] = "start-pre",
3737 [SERVICE_START] = "start",
3738 [SERVICE_START_POST] = "start-post",
3739 [SERVICE_RUNNING] = "running",
3740 [SERVICE_EXITED] = "exited",
3741 [SERVICE_RELOAD] = "reload",
3742 [SERVICE_STOP] = "stop",
3743 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3744 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3745 [SERVICE_STOP_POST] = "stop-post",
3746 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3747 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3748 [SERVICE_FAILED] = "failed",
3749 [SERVICE_AUTO_RESTART] = "auto-restart",
3752 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3754 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3755 [SERVICE_RESTART_NO] = "no",
3756 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3757 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3758 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3759 [SERVICE_RESTART_ALWAYS] = "always"
3762 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3764 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3765 [SERVICE_SIMPLE] = "simple",
3766 [SERVICE_FORKING] = "forking",
3767 [SERVICE_ONESHOT] = "oneshot",
3768 [SERVICE_DBUS] = "dbus",
3769 [SERVICE_NOTIFY] = "notify",
3770 [SERVICE_IDLE] = "idle"
3773 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3775 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3776 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3777 [SERVICE_EXEC_START] = "ExecStart",
3778 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3779 [SERVICE_EXEC_RELOAD] = "ExecReload",
3780 [SERVICE_EXEC_STOP] = "ExecStop",
3781 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3784 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3786 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3787 [NOTIFY_NONE] = "none",
3788 [NOTIFY_MAIN] = "main",
3789 [NOTIFY_ALL] = "all"
3792 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3794 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3795 [SERVICE_SUCCESS] = "success",
3796 [SERVICE_FAILURE_RESOURCES] = "resources",
3797 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3798 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3799 [SERVICE_FAILURE_SIGNAL] = "signal",
3800 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3801 [SERVICE_FAILURE_WATCHDOG] = "watchdog"
3804 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3806 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3807 [SERVICE_START_LIMIT_NONE] = "none",
3808 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3809 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3810 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3812 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3814 const UnitVTable service_vtable = {
3815 .object_size = sizeof(Service),
3821 .init = service_init,
3822 .done = service_done,
3823 .load = service_load,
3825 .coldplug = service_coldplug,
3827 .dump = service_dump,
3829 .start = service_start,
3830 .stop = service_stop,
3831 .reload = service_reload,
3833 .can_reload = service_can_reload,
3835 .kill = service_kill,
3837 .serialize = service_serialize,
3838 .deserialize_item = service_deserialize_item,
3840 .active_state = service_active_state,
3841 .sub_state_to_string = service_sub_state_to_string,
3843 .check_gc = service_check_gc,
3844 .check_snapshot = service_check_snapshot,
3846 .sigchld_event = service_sigchld_event,
3847 .timer_event = service_timer_event,
3848 .fd_event = service_fd_event,
3850 .reset_failed = service_reset_failed,
3852 .cgroup_notify_empty = service_cgroup_notify_event,
3853 .notify_message = service_notify_message,
3855 .bus_name_owner_change = service_bus_name_owner_change,
3856 .bus_query_pid_done = service_bus_query_pid_done,
3858 .bus_interface = "org.freedesktop.systemd1.Service",
3859 .bus_message_handler = bus_service_message_handler,
3860 .bus_invalidating_properties = bus_service_invalidating_properties,
3862 #ifdef HAVE_SYSV_COMPAT
3863 .enumerate = service_enumerate,
3865 .status_message_formats = {
3866 .starting_stopping = {
3867 [0] = "Starting %s...",
3868 [1] = "Stopping %s...",
3870 .finished_start_job = {
3871 [JOB_DONE] = "Started %s.",
3872 [JOB_FAILED] = "Failed to start %s.",
3873 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3874 [JOB_TIMEOUT] = "Timed out starting %s.",
3876 .finished_stop_job = {
3877 [JOB_DONE] = "Stopped %s.",
3878 [JOB_FAILED] = "Stopped (with error) %s.",
3879 [JOB_TIMEOUT] = "Timed out stopping %s.",