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;
568 if (fstat(fileno(f), &st) < 0) {
573 free(u->source_path);
574 u->source_path = strdup(path);
575 if (!u->source_path) {
579 u->source_mtime = timespec_load(&st.st_mtim);
581 if (null_or_empty(&st)) {
582 u->load_state = UNIT_MASKED;
590 char l[LINE_MAX], *t;
592 if (!fgets(l, sizeof(l), f)) {
597 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
607 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
609 s->sysv_has_lsb = true;
613 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
619 t += strspn(t, WHITESPACE);
621 if (state == NORMAL) {
623 /* Try to parse Red Hat style chkconfig headers */
625 if (startswith_no_case(t, "chkconfig:")) {
627 char runlevels[16], *k;
631 if (sscanf(t+10, "%15s %i %*i",
633 &start_priority) != 2) {
635 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
639 /* A start priority gathered from the
640 * symlink farms is preferred over the
641 * data from the LSB header. */
642 if (start_priority < 0 || start_priority > 99)
643 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
645 s->sysv_start_priority = start_priority;
647 char_array_0(runlevels);
648 k = delete_chars(runlevels, WHITESPACE "-");
653 if (!(d = strdup(k))) {
658 free(s->sysv_runlevels);
659 s->sysv_runlevels = d;
662 } else if (startswith_no_case(t, "description:")) {
664 size_t k = strlen(t);
668 if (t[k-1] == '\\') {
673 if ((j = strstrip(t+12)) && *j) {
674 if (!(d = strdup(j))) {
681 free(chkconfig_description);
682 chkconfig_description = d;
684 } else if (startswith_no_case(t, "pidfile:")) {
691 if (!path_is_absolute(fn)) {
692 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
696 if (!(fn = strdup(fn))) {
705 } else if (state == DESCRIPTION) {
707 /* Try to parse Red Hat style description
710 size_t k = strlen(t);
718 if ((j = strstrip(t)) && *j) {
721 if (chkconfig_description)
722 d = strjoin(chkconfig_description, " ", j, NULL);
731 free(chkconfig_description);
732 chkconfig_description = d;
735 } else if (state == LSB || state == LSB_DESCRIPTION) {
737 if (startswith_no_case(t, "Provides:")) {
743 FOREACH_WORD_QUOTED(w, z, t+9, i) {
746 if (!(n = strndup(w, z))) {
751 r = sysv_translate_facility(n, path_get_file_name(path), &m);
760 if (unit_name_to_type(m) == UNIT_SERVICE)
761 r = unit_add_name(u, m);
768 * indication that the
770 * now available. This
773 * targets do NOT pull
776 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
779 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
784 } else if (startswith_no_case(t, "Required-Start:") ||
785 startswith_no_case(t, "Should-Start:") ||
786 startswith_no_case(t, "X-Start-Before:") ||
787 startswith_no_case(t, "X-Start-After:")) {
793 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
796 if (!(n = strndup(w, z))) {
801 r = sysv_translate_facility(n, path_get_file_name(path), &m);
804 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
814 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
817 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
821 } else if (startswith_no_case(t, "Default-Start:")) {
826 k = delete_chars(t+14, WHITESPACE "-");
829 if (!(d = strdup(k))) {
834 free(s->sysv_runlevels);
835 s->sysv_runlevels = d;
838 } else if (startswith_no_case(t, "Description:")) {
841 state = LSB_DESCRIPTION;
843 if ((j = strstrip(t+12)) && *j) {
844 if (!(d = strdup(j))) {
851 free(long_description);
852 long_description = d;
854 } else if (startswith_no_case(t, "Short-Description:")) {
859 if ((j = strstrip(t+18)) && *j) {
860 if (!(d = strdup(j))) {
867 free(short_description);
868 short_description = d;
870 } else if (state == LSB_DESCRIPTION) {
872 if (startswith(l, "#\t") || startswith(l, "# ")) {
875 if ((j = strstrip(t)) && *j) {
878 if (long_description)
879 d = strjoin(long_description, " ", t, NULL);
888 free(long_description);
889 long_description = d;
898 if ((r = sysv_exec_commands(s)) < 0)
900 if (s->sysv_runlevels &&
901 chars_intersect(RUNLEVELS_BOOT, s->sysv_runlevels) &&
902 chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
903 /* Service has both boot and "up" runlevels
904 configured. Kill the "up" ones. */
905 delete_chars(s->sysv_runlevels, RUNLEVELS_UP);
908 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
909 /* If there a runlevels configured for this service
910 * but none of the standard ones, then we assume this
911 * is some special kind of service (which might be
912 * needed for early boot) and don't create any links
915 UNIT(s)->default_dependencies = false;
917 /* Don't timeout special services during boot (like fsck) */
920 s->timeout_usec = DEFAULT_SYSV_TIMEOUT_USEC;
922 /* Special setting for all SysV services */
923 s->type = SERVICE_FORKING;
924 s->remain_after_exit = !s->pid_file;
925 s->guess_main_pid = false;
926 s->restart = SERVICE_RESTART_NO;
927 s->exec_context.ignore_sigpipe = false;
928 s->kill_context.kill_mode = KILL_PROCESS;
930 /* We use the long description only if
931 * no short description is set. */
933 if (short_description)
934 description = short_description;
935 else if (chkconfig_description)
936 description = chkconfig_description;
937 else if (long_description)
938 description = long_description;
945 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
953 /* The priority that has been set in /etc/rcN.d/ hierarchies
954 * takes precedence over what is stored as default in the LSB
956 if (s->sysv_start_priority_from_rcnd >= 0)
957 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
959 u->load_state = UNIT_LOADED;
967 free(short_description);
968 free(long_description);
969 free(chkconfig_description);
974 static int service_load_sysv_name(Service *s, const char *name) {
980 /* For SysV services we strip the boot.*, rc.* and *.sh
981 * prefixes/suffixes. */
982 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
983 if (endswith(name, ".sh.service"))
988 if (startswith(name, "boot."))
992 #ifdef TARGET_FRUGALWARE
993 if (startswith(name, "rc."))
997 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1001 path = strjoin(*p, "/", name, NULL);
1005 assert(endswith(path, ".service"));
1006 path[strlen(path)-8] = 0;
1008 r = service_load_sysv_path(s, path);
1010 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
1011 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1012 /* Try Debian style *.sh source'able init scripts */
1013 strcat(path, ".sh");
1014 r = service_load_sysv_path(s, path);
1020 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1021 /* Try SUSE style boot.* init scripts */
1023 path = strjoin(*p, "/boot.", name, NULL);
1027 /* Drop .service suffix */
1028 path[strlen(path)-8] = 0;
1029 r = service_load_sysv_path(s, path);
1034 #ifdef TARGET_FRUGALWARE
1035 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1036 /* Try Frugalware style rc.* init scripts */
1038 path = strjoin(*p, "/rc.", name, NULL);
1042 /* Drop .service suffix */
1043 path[strlen(path)-8] = 0;
1044 r = service_load_sysv_path(s, path);
1052 if ((UNIT(s)->load_state != UNIT_STUB))
1059 static int service_load_sysv(Service *s) {
1066 /* Load service data from SysV init scripts, preferably with
1067 * LSB headers ... */
1069 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1072 if ((t = UNIT(s)->id))
1073 if ((r = service_load_sysv_name(s, t)) < 0)
1076 if (UNIT(s)->load_state == UNIT_STUB)
1077 SET_FOREACH(t, UNIT(s)->names, i) {
1078 if (t == UNIT(s)->id)
1081 if ((r = service_load_sysv_name(s, t)) < 0)
1084 if (UNIT(s)->load_state != UNIT_STUB)
1092 static int fsck_fix_order(Service *s) {
1098 if (s->fsck_passno <= 0)
1101 /* For each pair of services where both have an fsck priority
1102 * we order things based on it. */
1104 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1113 if (UNIT(t)->load_state != UNIT_LOADED)
1116 if (t->fsck_passno <= 0)
1119 if (t->fsck_passno < s->fsck_passno)
1121 else if (t->fsck_passno > s->fsck_passno)
1126 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1133 static int service_verify(Service *s) {
1136 if (UNIT(s)->load_state != UNIT_LOADED)
1139 if (!s->exec_command[SERVICE_EXEC_START]) {
1140 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1144 if (s->type != SERVICE_ONESHOT &&
1145 s->exec_command[SERVICE_EXEC_START]->command_next) {
1146 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1150 if (s->type == SERVICE_ONESHOT &&
1151 s->exec_command[SERVICE_EXEC_RELOAD]) {
1152 log_error("%s has an ExecReload setting, which is not allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1156 if (s->type == SERVICE_DBUS && !s->bus_name) {
1157 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1161 if (s->bus_name && s->type != SERVICE_DBUS)
1162 log_warning("%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1164 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1165 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1172 static int service_add_default_dependencies(Service *s) {
1177 /* Add a number of automatic dependencies useful for the
1178 * majority of services. */
1180 /* First, pull in base system */
1181 if (UNIT(s)->manager->running_as == MANAGER_SYSTEM) {
1183 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1186 } else if (UNIT(s)->manager->running_as == MANAGER_USER) {
1188 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1192 /* Second, activate normal shutdown */
1193 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1196 static void service_fix_output(Service *s) {
1199 /* If nothing has been explicitly configured, patch default
1200 * output in. If input is socket/tty we avoid this however,
1201 * since in that case we want output to default to the same
1202 * place as we read input from. */
1204 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1205 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1206 s->exec_context.std_input == EXEC_INPUT_NULL)
1207 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1209 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1210 s->exec_context.std_input == EXEC_INPUT_NULL)
1211 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1214 static int service_load(Unit *u) {
1216 Service *s = SERVICE(u);
1220 /* Load a .service file */
1221 if ((r = unit_load_fragment(u)) < 0)
1224 #ifdef HAVE_SYSV_COMPAT
1225 /* Load a classic init script as a fallback, if we couldn't find anything */
1226 if (u->load_state == UNIT_STUB)
1227 if ((r = service_load_sysv(s)) < 0)
1231 /* Still nothing found? Then let's give up */
1232 if (u->load_state == UNIT_STUB)
1235 /* We were able to load something, then let's add in the
1236 * dropin directories. */
1237 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1240 /* This is a new unit? Then let's add in some extras */
1241 if (u->load_state == UNIT_LOADED) {
1242 if (s->type == _SERVICE_TYPE_INVALID)
1243 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1245 /* Oneshot services have disabled timeout by default */
1246 if (s->type == SERVICE_ONESHOT && !s->timeout_defined)
1247 s->timeout_usec = 0;
1249 service_fix_output(s);
1251 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1254 if ((r = unit_add_default_cgroups(u)) < 0)
1257 #ifdef HAVE_SYSV_COMPAT
1258 if ((r = sysv_fix_order(s)) < 0)
1262 if ((r = fsck_fix_order(s)) < 0)
1266 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1269 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1270 s->notify_access = NOTIFY_MAIN;
1272 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1273 s->notify_access = NOTIFY_MAIN;
1275 if (s->type == SERVICE_DBUS || s->bus_name)
1276 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1279 if (UNIT(s)->default_dependencies)
1280 if ((r = service_add_default_dependencies(s)) < 0)
1283 r = unit_exec_context_defaults(u, &s->exec_context);
1288 return service_verify(s);
1291 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1293 ServiceExecCommand c;
1294 Service *s = SERVICE(u);
1295 const char *prefix2;
1300 p2 = strappend(prefix, "\t");
1301 prefix2 = p2 ? p2 : prefix;
1304 "%sService State: %s\n"
1306 "%sReload Result: %s\n"
1307 "%sPermissionsStartOnly: %s\n"
1308 "%sRootDirectoryStartOnly: %s\n"
1309 "%sRemainAfterExit: %s\n"
1310 "%sGuessMainPID: %s\n"
1313 "%sNotifyAccess: %s\n",
1314 prefix, service_state_to_string(s->state),
1315 prefix, service_result_to_string(s->result),
1316 prefix, service_result_to_string(s->reload_result),
1317 prefix, yes_no(s->permissions_start_only),
1318 prefix, yes_no(s->root_directory_start_only),
1319 prefix, yes_no(s->remain_after_exit),
1320 prefix, yes_no(s->guess_main_pid),
1321 prefix, service_type_to_string(s->type),
1322 prefix, service_restart_to_string(s->restart),
1323 prefix, notify_access_to_string(s->notify_access));
1325 if (s->control_pid > 0)
1327 "%sControl PID: %lu\n",
1328 prefix, (unsigned long) s->control_pid);
1330 if (s->main_pid > 0)
1333 "%sMain PID Known: %s\n"
1334 "%sMain PID Alien: %s\n",
1335 prefix, (unsigned long) s->main_pid,
1336 prefix, yes_no(s->main_pid_known),
1337 prefix, yes_no(s->main_pid_alien));
1342 prefix, s->pid_file);
1347 "%sBus Name Good: %s\n",
1348 prefix, s->bus_name,
1349 prefix, yes_no(s->bus_name_good));
1351 kill_context_dump(&s->kill_context, f, prefix);
1352 exec_context_dump(&s->exec_context, f, prefix);
1354 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1356 if (!s->exec_command[c])
1359 fprintf(f, "%s-> %s:\n",
1360 prefix, service_exec_command_to_string(c));
1362 exec_command_dump_list(s->exec_command[c], f, prefix2);
1365 #ifdef HAVE_SYSV_COMPAT
1368 "%sSysV Init Script has LSB Header: %s\n"
1369 "%sSysVEnabled: %s\n",
1370 prefix, yes_no(s->sysv_has_lsb),
1371 prefix, yes_no(s->sysv_enabled));
1373 if (s->sysv_start_priority >= 0)
1375 "%sSysVStartPriority: %i\n",
1376 prefix, s->sysv_start_priority);
1378 if (s->sysv_runlevels)
1379 fprintf(f, "%sSysVRunLevels: %s\n",
1380 prefix, s->sysv_runlevels);
1383 if (s->fsck_passno > 0)
1385 "%sFsckPassNo: %i\n",
1386 prefix, s->fsck_passno);
1389 fprintf(f, "%sStatus Text: %s\n",
1390 prefix, s->status_text);
1395 static int service_load_pid_file(Service *s, bool may_warn) {
1405 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1407 log_info("PID file %s not readable (yet?) after %s.",
1408 s->pid_file, service_state_to_string(s->state));
1412 r = parse_pid(k, &pid);
1418 if (kill(pid, 0) < 0 && errno != EPERM) {
1420 log_info("PID %lu read from file %s does not exist.",
1421 (unsigned long) pid, s->pid_file);
1425 if (s->main_pid_known) {
1426 if (pid == s->main_pid)
1429 log_debug("Main PID changing: %lu -> %lu",
1430 (unsigned long) s->main_pid, (unsigned long) pid);
1431 service_unwatch_main_pid(s);
1432 s->main_pid_known = false;
1434 log_debug("Main PID loaded: %lu", (unsigned long) pid);
1436 if ((r = service_set_main_pid(s, pid)) < 0)
1439 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1440 /* FIXME: we need to do something here */
1446 static int service_search_main_pid(Service *s) {
1452 /* If we know it anyway, don't ever fallback to unreliable
1454 if (s->main_pid_known)
1457 if (!s->guess_main_pid)
1460 assert(s->main_pid <= 0);
1462 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1465 log_debug("Main PID guessed: %lu", (unsigned long) pid);
1466 if ((r = service_set_main_pid(s, pid)) < 0)
1469 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1470 /* FIXME: we need to do something here */
1476 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1482 /* Notifies all our sockets when we die */
1484 if (s->socket_fd >= 0)
1487 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1488 if (u->type == UNIT_SOCKET)
1489 socket_notify_service_dead(SOCKET(u), failed_permanent);
1494 static void service_set_state(Service *s, ServiceState state) {
1495 ServiceState old_state;
1496 const UnitActiveState *table;
1499 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1501 old_state = s->state;
1504 service_unwatch_pid_file(s);
1506 if (state != SERVICE_START_PRE &&
1507 state != SERVICE_START &&
1508 state != SERVICE_START_POST &&
1509 state != SERVICE_RELOAD &&
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_AUTO_RESTART)
1517 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1519 if (state != SERVICE_START &&
1520 state != SERVICE_START_POST &&
1521 state != SERVICE_RUNNING &&
1522 state != SERVICE_RELOAD &&
1523 state != SERVICE_STOP &&
1524 state != SERVICE_STOP_SIGTERM &&
1525 state != SERVICE_STOP_SIGKILL) {
1526 service_unwatch_main_pid(s);
1527 s->main_command = NULL;
1530 if (state != SERVICE_START_PRE &&
1531 state != SERVICE_START &&
1532 state != SERVICE_START_POST &&
1533 state != SERVICE_RELOAD &&
1534 state != SERVICE_STOP &&
1535 state != SERVICE_STOP_SIGTERM &&
1536 state != SERVICE_STOP_SIGKILL &&
1537 state != SERVICE_STOP_POST &&
1538 state != SERVICE_FINAL_SIGTERM &&
1539 state != SERVICE_FINAL_SIGKILL) {
1540 service_unwatch_control_pid(s);
1541 s->control_command = NULL;
1542 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1545 if (state == SERVICE_DEAD ||
1546 state == SERVICE_STOP ||
1547 state == SERVICE_STOP_SIGTERM ||
1548 state == SERVICE_STOP_SIGKILL ||
1549 state == SERVICE_STOP_POST ||
1550 state == SERVICE_FINAL_SIGTERM ||
1551 state == SERVICE_FINAL_SIGKILL ||
1552 state == SERVICE_FAILED ||
1553 state == SERVICE_AUTO_RESTART)
1554 service_notify_sockets_dead(s, false);
1556 if (state != SERVICE_START_PRE &&
1557 state != SERVICE_START &&
1558 state != SERVICE_START_POST &&
1559 state != SERVICE_RUNNING &&
1560 state != SERVICE_RELOAD &&
1561 state != SERVICE_STOP &&
1562 state != SERVICE_STOP_SIGTERM &&
1563 state != SERVICE_STOP_SIGKILL &&
1564 state != SERVICE_STOP_POST &&
1565 state != SERVICE_FINAL_SIGTERM &&
1566 state != SERVICE_FINAL_SIGKILL &&
1567 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1568 service_close_socket_fd(s);
1569 service_connection_unref(s);
1572 if (state == SERVICE_STOP)
1573 service_stop_watchdog(s);
1575 /* For the inactive states unit_notify() will trim the cgroup,
1576 * but for exit we have to do that ourselves... */
1577 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1578 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1580 if (old_state != state)
1581 log_debug("%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
1583 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1584 s->reload_result = SERVICE_SUCCESS;
1587 static int service_coldplug(Unit *u) {
1588 Service *s = SERVICE(u);
1592 assert(s->state == SERVICE_DEAD);
1594 if (s->deserialized_state != s->state) {
1596 if (s->deserialized_state == SERVICE_START_PRE ||
1597 s->deserialized_state == SERVICE_START ||
1598 s->deserialized_state == SERVICE_START_POST ||
1599 s->deserialized_state == SERVICE_RELOAD ||
1600 s->deserialized_state == SERVICE_STOP ||
1601 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1602 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1603 s->deserialized_state == SERVICE_STOP_POST ||
1604 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1605 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1606 s->deserialized_state == SERVICE_AUTO_RESTART) {
1608 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1611 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1613 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1618 if ((s->deserialized_state == SERVICE_START &&
1619 (s->type == SERVICE_FORKING ||
1620 s->type == SERVICE_DBUS ||
1621 s->type == SERVICE_ONESHOT ||
1622 s->type == SERVICE_NOTIFY)) ||
1623 s->deserialized_state == SERVICE_START_POST ||
1624 s->deserialized_state == SERVICE_RUNNING ||
1625 s->deserialized_state == SERVICE_RELOAD ||
1626 s->deserialized_state == SERVICE_STOP ||
1627 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1628 s->deserialized_state == SERVICE_STOP_SIGKILL)
1629 if (s->main_pid > 0)
1630 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1633 if (s->deserialized_state == SERVICE_START_PRE ||
1634 s->deserialized_state == SERVICE_START ||
1635 s->deserialized_state == SERVICE_START_POST ||
1636 s->deserialized_state == SERVICE_RELOAD ||
1637 s->deserialized_state == SERVICE_STOP ||
1638 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1639 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1640 s->deserialized_state == SERVICE_STOP_POST ||
1641 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1642 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1643 if (s->control_pid > 0)
1644 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1647 if (s->deserialized_state == SERVICE_START_POST ||
1648 s->deserialized_state == SERVICE_RUNNING)
1649 service_handle_watchdog(s);
1651 service_set_state(s, s->deserialized_state);
1656 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1660 unsigned rn_fds = 0;
1667 if (s->socket_fd >= 0)
1670 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1675 if (u->type != UNIT_SOCKET)
1680 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1692 if (!(t = new(int, rn_fds+cn_fds))) {
1698 memcpy(t, rfds, rn_fds * sizeof(int));
1699 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1704 rn_fds = rn_fds+cn_fds;
1719 static int service_spawn(
1724 bool apply_permissions,
1726 bool apply_tty_stdin,
1727 bool set_notify_socket,
1733 int *fds = NULL, *fdsbuf = NULL;
1734 unsigned n_fds = 0, n_env = 0;
1735 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1742 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1743 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1744 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1746 if (s->socket_fd >= 0) {
1747 fds = &s->socket_fd;
1750 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1757 if (timeout && s->timeout_usec) {
1758 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1761 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1763 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1768 if (!(our_env = new0(char*, 4))) {
1773 if (set_notify_socket)
1774 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1779 if (s->main_pid > 0)
1780 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1785 if (s->watchdog_usec > 0)
1786 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1791 if (!(final_env = strv_env_merge(2,
1792 UNIT(s)->manager->environment,
1807 UNIT(s)->manager->confirm_spawn,
1808 UNIT(s)->cgroup_bondings,
1809 UNIT(s)->cgroup_attributes,
1810 is_control ? "control" : NULL,
1812 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1819 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1820 /* FIXME: we need to do something here */
1826 strv_free(final_env);
1836 strv_free(final_env);
1839 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1844 static int main_pid_good(Service *s) {
1847 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1850 /* If we know the pid file, then lets just check if it is
1852 if (s->main_pid_known) {
1854 /* If it's an alien child let's check if it is still
1856 if (s->main_pid_alien)
1857 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1859 /* .. otherwise assume we'll get a SIGCHLD for it,
1860 * which we really should wait for to collect exit
1861 * status and code */
1862 return s->main_pid > 0;
1865 /* We don't know the pid */
1869 static int control_pid_good(Service *s) {
1872 return s->control_pid > 0;
1875 static int cgroup_good(Service *s) {
1880 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1886 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1890 if (f != SERVICE_SUCCESS)
1893 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1895 if (allow_restart &&
1896 !s->forbid_restart &&
1897 (s->restart == SERVICE_RESTART_ALWAYS ||
1898 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1899 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1900 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1901 s->result == SERVICE_FAILURE_CORE_DUMP)))) {
1903 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
1907 service_set_state(s, SERVICE_AUTO_RESTART);
1910 s->forbid_restart = false;
1915 log_warning("%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1916 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1919 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1921 static void service_enter_stop_post(Service *s, ServiceResult f) {
1925 if (f != SERVICE_SUCCESS)
1928 service_unwatch_control_pid(s);
1930 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1931 s->control_command_id = SERVICE_EXEC_STOP_POST;
1933 r = service_spawn(s,
1937 !s->permissions_start_only,
1938 !s->root_directory_start_only,
1947 service_set_state(s, SERVICE_STOP_POST);
1949 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1954 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
1955 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1958 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1960 Set *pid_set = NULL;
1961 bool wait_for_exit = false;
1965 if (f != SERVICE_SUCCESS)
1968 if (s->kill_context.kill_mode != KILL_NONE) {
1969 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->kill_context.kill_signal : SIGKILL;
1971 if (s->main_pid > 0) {
1972 if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
1973 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1975 wait_for_exit = !s->main_pid_alien;
1978 if (s->control_pid > 0) {
1979 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1980 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1982 wait_for_exit = true;
1985 if (s->kill_context.kill_mode == KILL_CONTROL_GROUP) {
1987 pid_set = set_new(trivial_hash_func, trivial_compare_func);
1993 /* Exclude the main/control pids from being killed via the cgroup */
1994 if (s->main_pid > 0)
1995 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1998 if (s->control_pid > 0)
1999 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
2002 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
2004 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
2005 log_warning("Failed to kill control group: %s", strerror(-r));
2007 wait_for_exit = true;
2014 if (wait_for_exit) {
2015 if (s->timeout_usec > 0)
2016 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
2019 service_set_state(s, state);
2020 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2021 service_enter_stop_post(s, SERVICE_SUCCESS);
2023 service_enter_dead(s, SERVICE_SUCCESS, true);
2028 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2030 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2031 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2033 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2039 static void service_enter_stop(Service *s, ServiceResult f) {
2044 if (f != SERVICE_SUCCESS)
2047 service_unwatch_control_pid(s);
2049 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2050 s->control_command_id = SERVICE_EXEC_STOP;
2052 r = service_spawn(s,
2056 !s->permissions_start_only,
2057 !s->root_directory_start_only,
2065 service_set_state(s, SERVICE_STOP);
2067 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2072 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2073 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2076 static void service_enter_running(Service *s, ServiceResult f) {
2077 int main_pid_ok, cgroup_ok;
2080 if (f != SERVICE_SUCCESS)
2083 main_pid_ok = main_pid_good(s);
2084 cgroup_ok = cgroup_good(s);
2086 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2087 (s->bus_name_good || s->type != SERVICE_DBUS))
2088 service_set_state(s, SERVICE_RUNNING);
2089 else if (s->remain_after_exit)
2090 service_set_state(s, SERVICE_EXITED);
2092 service_enter_stop(s, SERVICE_SUCCESS);
2095 static void service_enter_start_post(Service *s) {
2099 service_unwatch_control_pid(s);
2101 if (s->watchdog_usec > 0)
2102 service_reset_watchdog(s);
2104 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2105 s->control_command_id = SERVICE_EXEC_START_POST;
2107 r = service_spawn(s,
2111 !s->permissions_start_only,
2112 !s->root_directory_start_only,
2120 service_set_state(s, SERVICE_START_POST);
2122 service_enter_running(s, SERVICE_SUCCESS);
2127 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2128 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2131 static void service_enter_start(Service *s) {
2138 assert(s->exec_command[SERVICE_EXEC_START]);
2139 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2141 if (s->type == SERVICE_FORKING)
2142 service_unwatch_control_pid(s);
2144 service_unwatch_main_pid(s);
2146 /* We want to ensure that nobody leaks processes from
2147 * START_PRE here, so let's go on a killing spree, People
2148 * should not spawn long running processes from START_PRE. */
2149 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2151 if (s->type == SERVICE_FORKING) {
2152 s->control_command_id = SERVICE_EXEC_START;
2153 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2155 s->main_command = NULL;
2157 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2158 s->control_command = NULL;
2160 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2163 r = service_spawn(s,
2165 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2170 s->notify_access != NOTIFY_NONE,
2176 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2177 /* For simple services we immediately start
2178 * the START_POST binaries. */
2180 service_set_main_pid(s, pid);
2181 service_enter_start_post(s);
2183 } else if (s->type == SERVICE_FORKING) {
2185 /* For forking services we wait until the start
2186 * process exited. */
2188 s->control_pid = pid;
2189 service_set_state(s, SERVICE_START);
2191 } else if (s->type == SERVICE_ONESHOT ||
2192 s->type == SERVICE_DBUS ||
2193 s->type == SERVICE_NOTIFY) {
2195 /* For oneshot services we wait until the start
2196 * process exited, too, but it is our main process. */
2198 /* For D-Bus services we know the main pid right away,
2199 * but wait for the bus name to appear on the
2200 * bus. Notify services are similar. */
2202 service_set_main_pid(s, pid);
2203 service_set_state(s, SERVICE_START);
2205 assert_not_reached("Unknown service type");
2210 log_warning("%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2211 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2214 static void service_enter_start_pre(Service *s) {
2219 service_unwatch_control_pid(s);
2221 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2223 /* Before we start anything, let's clear up what might
2224 * be left from previous runs. */
2225 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2227 s->control_command_id = SERVICE_EXEC_START_PRE;
2229 r = service_spawn(s,
2233 !s->permissions_start_only,
2234 !s->root_directory_start_only,
2242 service_set_state(s, SERVICE_START_PRE);
2244 service_enter_start(s);
2249 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2250 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2253 static void service_enter_restart(Service *s) {
2258 dbus_error_init(&error);
2260 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2261 /* Don't restart things if we are going down anyway */
2262 log_info("Stop job pending for unit, delaying automatic restart.");
2264 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
2271 /* Any units that are bound to this service must also be
2272 * restarted. We use JOB_RESTART (instead of the more obvious
2273 * JOB_START) here so that those dependency jobs will be added
2275 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2279 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2280 * it will be canceled as part of the service_stop() call that
2281 * is executed as part of JOB_RESTART. */
2283 log_debug("%s scheduled restart job.", UNIT(s)->id);
2287 log_warning("%s failed to schedule restart job: %s", UNIT(s)->id, bus_error(&error, -r));
2288 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2290 dbus_error_free(&error);
2293 static void service_enter_reload(Service *s) {
2298 service_unwatch_control_pid(s);
2300 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2301 s->control_command_id = SERVICE_EXEC_RELOAD;
2303 r = service_spawn(s,
2307 !s->permissions_start_only,
2308 !s->root_directory_start_only,
2316 service_set_state(s, SERVICE_RELOAD);
2318 service_enter_running(s, SERVICE_SUCCESS);
2323 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
2324 s->reload_result = SERVICE_FAILURE_RESOURCES;
2325 service_enter_running(s, SERVICE_SUCCESS);
2328 static void service_run_next_control(Service *s) {
2332 assert(s->control_command);
2333 assert(s->control_command->command_next);
2335 assert(s->control_command_id != SERVICE_EXEC_START);
2337 s->control_command = s->control_command->command_next;
2338 service_unwatch_control_pid(s);
2340 r = service_spawn(s,
2344 !s->permissions_start_only,
2345 !s->root_directory_start_only,
2346 s->control_command_id == SERVICE_EXEC_START_PRE ||
2347 s->control_command_id == SERVICE_EXEC_STOP_POST,
2357 log_warning("%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
2359 if (s->state == SERVICE_START_PRE)
2360 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2361 else if (s->state == SERVICE_STOP)
2362 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2363 else if (s->state == SERVICE_STOP_POST)
2364 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2365 else if (s->state == SERVICE_RELOAD) {
2366 s->reload_result = SERVICE_FAILURE_RESOURCES;
2367 service_enter_running(s, SERVICE_SUCCESS);
2369 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2372 static void service_run_next_main(Service *s) {
2377 assert(s->main_command);
2378 assert(s->main_command->command_next);
2379 assert(s->type == SERVICE_ONESHOT);
2381 s->main_command = s->main_command->command_next;
2382 service_unwatch_main_pid(s);
2384 r = service_spawn(s,
2391 s->notify_access != NOTIFY_NONE,
2397 service_set_main_pid(s, pid);
2402 log_warning("%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2403 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2406 static int service_start_limit_test(Service *s) {
2409 if (ratelimit_test(&s->start_limit))
2412 switch (s->start_limit_action) {
2414 case SERVICE_START_LIMIT_NONE:
2415 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s)->id);
2418 case SERVICE_START_LIMIT_REBOOT: {
2422 dbus_error_init(&error);
2424 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2426 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2428 log_error("Failed to reboot: %s.", bus_error(&error, r));
2429 dbus_error_free(&error);
2435 case SERVICE_START_LIMIT_REBOOT_FORCE:
2436 log_warning("%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2437 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2440 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2441 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2442 reboot(RB_AUTOBOOT);
2446 log_error("start limit action=%i", s->start_limit_action);
2447 assert_not_reached("Unknown StartLimitAction.");
2453 static int service_start(Unit *u) {
2454 Service *s = SERVICE(u);
2459 /* We cannot fulfill this request right now, try again later
2461 if (s->state == SERVICE_STOP ||
2462 s->state == SERVICE_STOP_SIGTERM ||
2463 s->state == SERVICE_STOP_SIGKILL ||
2464 s->state == SERVICE_STOP_POST ||
2465 s->state == SERVICE_FINAL_SIGTERM ||
2466 s->state == SERVICE_FINAL_SIGKILL)
2469 /* Already on it! */
2470 if (s->state == SERVICE_START_PRE ||
2471 s->state == SERVICE_START ||
2472 s->state == SERVICE_START_POST)
2475 /* A service that will be restarted must be stopped first to
2476 * trigger BindsTo and/or OnFailure dependencies. If a user
2477 * does not want to wait for the holdoff time to elapse, the
2478 * service should be manually restarted, not started. We
2479 * simply return EAGAIN here, so that any start jobs stay
2480 * queued, and assume that the auto restart timer will
2481 * eventually trigger the restart. */
2482 if (s->state == SERVICE_AUTO_RESTART)
2485 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2487 /* Make sure we don't enter a busy loop of some kind. */
2488 r = service_start_limit_test(s);
2490 service_notify_sockets_dead(s, true);
2494 s->result = SERVICE_SUCCESS;
2495 s->reload_result = SERVICE_SUCCESS;
2496 s->main_pid_known = false;
2497 s->main_pid_alien = false;
2498 s->forbid_restart = false;
2500 service_enter_start_pre(s);
2504 static int service_stop(Unit *u) {
2505 Service *s = SERVICE(u);
2509 /* Don't create restart jobs from here. */
2510 s->forbid_restart = true;
2513 if (s->state == SERVICE_STOP ||
2514 s->state == SERVICE_STOP_SIGTERM ||
2515 s->state == SERVICE_STOP_SIGKILL ||
2516 s->state == SERVICE_STOP_POST ||
2517 s->state == SERVICE_FINAL_SIGTERM ||
2518 s->state == SERVICE_FINAL_SIGKILL)
2521 /* A restart will be scheduled or is in progress. */
2522 if (s->state == SERVICE_AUTO_RESTART) {
2523 service_set_state(s, SERVICE_DEAD);
2527 /* If there's already something running we go directly into
2529 if (s->state == SERVICE_START_PRE ||
2530 s->state == SERVICE_START ||
2531 s->state == SERVICE_START_POST ||
2532 s->state == SERVICE_RELOAD) {
2533 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2537 assert(s->state == SERVICE_RUNNING ||
2538 s->state == SERVICE_EXITED);
2540 service_enter_stop(s, SERVICE_SUCCESS);
2544 static int service_reload(Unit *u) {
2545 Service *s = SERVICE(u);
2549 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2551 service_enter_reload(s);
2555 static bool service_can_reload(Unit *u) {
2556 Service *s = SERVICE(u);
2560 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2563 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2564 Service *s = SERVICE(u);
2570 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2571 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2572 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2574 if (s->control_pid > 0)
2575 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2577 if (s->main_pid_known && s->main_pid > 0)
2578 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2580 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2583 unit_serialize_item(u, f, "status-text", s->status_text);
2585 /* FIXME: There's a minor uncleanliness here: if there are
2586 * multiple commands attached here, we will start from the
2587 * first one again */
2588 if (s->control_command_id >= 0)
2589 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2591 if (s->socket_fd >= 0) {
2594 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2597 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2600 if (s->main_exec_status.pid > 0) {
2601 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2602 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2603 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2605 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2606 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2607 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2610 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2611 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2616 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2617 Service *s = SERVICE(u);
2624 if (streq(key, "state")) {
2627 if ((state = service_state_from_string(value)) < 0)
2628 log_debug("Failed to parse state value %s", value);
2630 s->deserialized_state = state;
2631 } else if (streq(key, "result")) {
2634 f = service_result_from_string(value);
2636 log_debug("Failed to parse result value %s", value);
2637 else if (f != SERVICE_SUCCESS)
2640 } else if (streq(key, "reload-result")) {
2643 f = service_result_from_string(value);
2645 log_debug("Failed to parse reload result value %s", value);
2646 else if (f != SERVICE_SUCCESS)
2647 s->reload_result = f;
2649 } else if (streq(key, "control-pid")) {
2652 if (parse_pid(value, &pid) < 0)
2653 log_debug("Failed to parse control-pid value %s", value);
2655 s->control_pid = pid;
2656 } else if (streq(key, "main-pid")) {
2659 if (parse_pid(value, &pid) < 0)
2660 log_debug("Failed to parse main-pid value %s", value);
2662 service_set_main_pid(s, (pid_t) pid);
2663 } else if (streq(key, "main-pid-known")) {
2666 if ((b = parse_boolean(value)) < 0)
2667 log_debug("Failed to parse main-pid-known value %s", value);
2669 s->main_pid_known = b;
2670 } else if (streq(key, "status-text")) {
2673 if ((t = strdup(value))) {
2674 free(s->status_text);
2678 } else if (streq(key, "control-command")) {
2679 ServiceExecCommand id;
2681 if ((id = service_exec_command_from_string(value)) < 0)
2682 log_debug("Failed to parse exec-command value %s", value);
2684 s->control_command_id = id;
2685 s->control_command = s->exec_command[id];
2687 } else if (streq(key, "socket-fd")) {
2690 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2691 log_debug("Failed to parse socket-fd value %s", value);
2694 if (s->socket_fd >= 0)
2695 close_nointr_nofail(s->socket_fd);
2696 s->socket_fd = fdset_remove(fds, fd);
2698 } else if (streq(key, "main-exec-status-pid")) {
2701 if (parse_pid(value, &pid) < 0)
2702 log_debug("Failed to parse main-exec-status-pid value %s", value);
2704 s->main_exec_status.pid = pid;
2705 } else if (streq(key, "main-exec-status-code")) {
2708 if (safe_atoi(value, &i) < 0)
2709 log_debug("Failed to parse main-exec-status-code value %s", value);
2711 s->main_exec_status.code = i;
2712 } else if (streq(key, "main-exec-status-status")) {
2715 if (safe_atoi(value, &i) < 0)
2716 log_debug("Failed to parse main-exec-status-status value %s", value);
2718 s->main_exec_status.status = i;
2719 } else if (streq(key, "main-exec-status-start"))
2720 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2721 else if (streq(key, "main-exec-status-exit"))
2722 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2723 else if (streq(key, "watchdog-timestamp"))
2724 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2726 log_debug("Unknown serialization key '%s'", key);
2731 static UnitActiveState service_active_state(Unit *u) {
2732 const UnitActiveState *table;
2736 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2738 return table[SERVICE(u)->state];
2741 static const char *service_sub_state_to_string(Unit *u) {
2744 return service_state_to_string(SERVICE(u)->state);
2747 static bool service_check_gc(Unit *u) {
2748 Service *s = SERVICE(u);
2752 /* Never clean up services that still have a process around,
2753 * even if the service is formally dead. */
2754 if (cgroup_good(s) > 0 ||
2755 main_pid_good(s) > 0 ||
2756 control_pid_good(s) > 0)
2759 #ifdef HAVE_SYSV_COMPAT
2767 static bool service_check_snapshot(Unit *u) {
2768 Service *s = SERVICE(u);
2772 return !s->got_socket_fd;
2775 static int service_retry_pid_file(Service *s) {
2778 assert(s->pid_file);
2779 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2781 r = service_load_pid_file(s, false);
2785 service_unwatch_pid_file(s);
2787 service_enter_running(s, SERVICE_SUCCESS);
2791 static int service_watch_pid_file(Service *s) {
2794 log_debug("Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
2795 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2799 /* the pidfile might have appeared just before we set the watch */
2800 service_retry_pid_file(s);
2804 log_error("Failed to set a watch for %s's PID file %s: %s",
2805 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2806 service_unwatch_pid_file(s);
2810 static int service_demand_pid_file(Service *s) {
2813 assert(s->pid_file);
2814 assert(!s->pid_file_pathspec);
2816 ps = new0(PathSpec, 1);
2820 ps->path = strdup(s->pid_file);
2826 path_kill_slashes(ps->path);
2828 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2829 * keep their PID file open all the time. */
2830 ps->type = PATH_MODIFIED;
2831 ps->inotify_fd = -1;
2833 s->pid_file_pathspec = ps;
2835 return service_watch_pid_file(s);
2838 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2839 Service *s = SERVICE(u);
2843 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2844 assert(s->pid_file_pathspec);
2845 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2847 log_debug("inotify event for %s", u->id);
2849 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2852 if (service_retry_pid_file(s) == 0)
2855 if (service_watch_pid_file(s) < 0)
2860 service_unwatch_pid_file(s);
2861 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2864 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2865 Service *s = SERVICE(u);
2871 if (UNIT(s)->fragment_path ? is_clean_exit(code, status) : is_clean_exit_lsb(code, status))
2872 f = SERVICE_SUCCESS;
2873 else if (code == CLD_EXITED)
2874 f = SERVICE_FAILURE_EXIT_CODE;
2875 else if (code == CLD_KILLED)
2876 f = SERVICE_FAILURE_SIGNAL;
2877 else if (code == CLD_DUMPED)
2878 f = SERVICE_FAILURE_CORE_DUMP;
2880 assert_not_reached("Unknown code");
2882 if (s->main_pid == pid) {
2883 /* Forking services may occasionally move to a new PID.
2884 * As long as they update the PID file before exiting the old
2885 * PID, they're fine. */
2886 if (service_load_pid_file(s, false) == 0)
2890 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2892 /* If this is not a forking service than the main
2893 * process got started and hence we copy the exit
2894 * status so that it is recorded both as main and as
2895 * control process exit status */
2896 if (s->main_command) {
2897 s->main_command->exec_status = s->main_exec_status;
2899 if (s->main_command->ignore)
2900 f = SERVICE_SUCCESS;
2903 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2904 "%s: main process exited, code=%s, status=%i", u->id, sigchld_code_to_string(code), status);
2906 if (f != SERVICE_SUCCESS)
2909 if (s->main_command &&
2910 s->main_command->command_next &&
2911 f == SERVICE_SUCCESS) {
2913 /* There is another command to *
2914 * execute, so let's do that. */
2916 log_debug("%s running next main command for state %s", u->id, service_state_to_string(s->state));
2917 service_run_next_main(s);
2921 /* The service exited, so the service is officially
2923 s->main_command = NULL;
2927 case SERVICE_START_POST:
2928 case SERVICE_RELOAD:
2930 /* Need to wait until the operation is
2935 if (s->type == SERVICE_ONESHOT) {
2936 /* This was our main goal, so let's go on */
2937 if (f == SERVICE_SUCCESS)
2938 service_enter_start_post(s);
2940 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2943 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2948 case SERVICE_RUNNING:
2949 service_enter_running(s, f);
2952 case SERVICE_STOP_SIGTERM:
2953 case SERVICE_STOP_SIGKILL:
2955 if (!control_pid_good(s))
2956 service_enter_stop_post(s, f);
2958 /* If there is still a control process, wait for that first */
2962 assert_not_reached("Uh, main process died at wrong time.");
2966 } else if (s->control_pid == pid) {
2970 if (s->control_command) {
2971 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2973 if (s->control_command->ignore)
2974 f = SERVICE_SUCCESS;
2977 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2978 "%s: control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
2980 if (f != SERVICE_SUCCESS)
2983 /* Immediately get rid of the cgroup, so that the
2984 * kernel doesn't delay the cgroup empty messages for
2985 * the service cgroup any longer than necessary */
2986 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2988 if (s->control_command &&
2989 s->control_command->command_next &&
2990 f == SERVICE_SUCCESS) {
2992 /* There is another command to *
2993 * execute, so let's do that. */
2995 log_debug("%s running next control command for state %s", u->id, service_state_to_string(s->state));
2996 service_run_next_control(s);
2999 /* No further commands for this step, so let's
3000 * figure out what to do next */
3002 s->control_command = NULL;
3003 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3005 log_debug("%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
3009 case SERVICE_START_PRE:
3010 if (f == SERVICE_SUCCESS)
3011 service_enter_start(s);
3013 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3017 assert(s->type == SERVICE_FORKING);
3019 if (f != SERVICE_SUCCESS) {
3020 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3025 bool has_start_post;
3028 /* Let's try to load the pid file here if we can.
3029 * The PID file might actually be created by a START_POST
3030 * script. In that case don't worry if the loading fails. */
3032 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3033 r = service_load_pid_file(s, !has_start_post);
3034 if (!has_start_post && r < 0) {
3035 r = service_demand_pid_file(s);
3036 if (r < 0 || !cgroup_good(s))
3037 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3041 service_search_main_pid(s);
3043 service_enter_start_post(s);
3046 case SERVICE_START_POST:
3047 if (f != SERVICE_SUCCESS) {
3048 service_enter_stop(s, f);
3055 r = service_load_pid_file(s, true);
3057 r = service_demand_pid_file(s);
3058 if (r < 0 || !cgroup_good(s))
3059 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3063 service_search_main_pid(s);
3065 service_enter_running(s, SERVICE_SUCCESS);
3068 case SERVICE_RELOAD:
3069 if (f == SERVICE_SUCCESS) {
3070 service_load_pid_file(s, true);
3071 service_search_main_pid(s);
3074 s->reload_result = f;
3075 service_enter_running(s, SERVICE_SUCCESS);
3079 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3082 case SERVICE_STOP_SIGTERM:
3083 case SERVICE_STOP_SIGKILL:
3084 if (main_pid_good(s) <= 0)
3085 service_enter_stop_post(s, f);
3087 /* If there is still a service
3088 * process around, wait until
3089 * that one quit, too */
3092 case SERVICE_STOP_POST:
3093 case SERVICE_FINAL_SIGTERM:
3094 case SERVICE_FINAL_SIGKILL:
3095 service_enter_dead(s, f, true);
3099 assert_not_reached("Uh, control process died at wrong time.");
3104 /* Notify clients about changed exit status */
3105 unit_add_to_dbus_queue(u);
3108 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3109 Service *s = SERVICE(u);
3112 assert(elapsed == 1);
3114 if (w == &s->watchdog_watch) {
3115 service_handle_watchdog(s);
3119 assert(w == &s->timer_watch);
3123 case SERVICE_START_PRE:
3125 log_warning("%s operation timed out. Terminating.", u->id);
3126 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3129 case SERVICE_START_POST:
3130 log_warning("%s operation timed out. Stopping.", u->id);
3131 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3134 case SERVICE_RELOAD:
3135 log_warning("%s operation timed out. Stopping.", u->id);
3136 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3137 service_enter_running(s, SERVICE_SUCCESS);
3141 log_warning("%s stopping timed out. Terminating.", u->id);
3142 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3145 case SERVICE_STOP_SIGTERM:
3146 if (s->kill_context.send_sigkill) {
3147 log_warning("%s stopping timed out. Killing.", u->id);
3148 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3150 log_warning("%s stopping timed out. Skipping SIGKILL.", u->id);
3151 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3156 case SERVICE_STOP_SIGKILL:
3157 /* Uh, we sent a SIGKILL and it is still not gone?
3158 * Must be something we cannot kill, so let's just be
3159 * weirded out and continue */
3161 log_warning("%s still around after SIGKILL. Ignoring.", u->id);
3162 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3165 case SERVICE_STOP_POST:
3166 log_warning("%s stopping timed out (2). Terminating.", u->id);
3167 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3170 case SERVICE_FINAL_SIGTERM:
3171 if (s->kill_context.send_sigkill) {
3172 log_warning("%s stopping timed out (2). Killing.", u->id);
3173 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3175 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->id);
3176 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3181 case SERVICE_FINAL_SIGKILL:
3182 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->id);
3183 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3186 case SERVICE_AUTO_RESTART:
3187 log_info("%s holdoff time over, scheduling restart.", u->id);
3188 service_enter_restart(s);
3192 assert_not_reached("Timeout at wrong time.");
3196 static void service_cgroup_notify_event(Unit *u) {
3197 Service *s = SERVICE(u);
3201 log_debug("%s: cgroup is empty", u->id);
3205 /* Waiting for SIGCHLD is usually more interesting,
3206 * because it includes return codes/signals. Which is
3207 * why we ignore the cgroup events for most cases,
3208 * except when we don't know pid which to expect the
3212 case SERVICE_START_POST:
3213 /* If we were hoping for the daemon to write its PID file,
3214 * we can give up now. */
3215 if (s->pid_file_pathspec) {
3216 log_warning("%s never wrote its PID file. Failing.", UNIT(s)->id);
3217 service_unwatch_pid_file(s);
3218 if (s->state == SERVICE_START)
3219 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3221 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3225 case SERVICE_RUNNING:
3226 /* service_enter_running() will figure out what to do */
3227 service_enter_running(s, SERVICE_SUCCESS);
3230 case SERVICE_STOP_SIGTERM:
3231 case SERVICE_STOP_SIGKILL:
3233 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3234 service_enter_stop_post(s, SERVICE_SUCCESS);
3238 case SERVICE_FINAL_SIGTERM:
3239 case SERVICE_FINAL_SIGKILL:
3240 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3241 service_enter_dead(s, SERVICE_SUCCESS, true);
3250 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3251 Service *s = SERVICE(u);
3256 if (s->notify_access == NOTIFY_NONE) {
3257 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3258 u->id, (unsigned long) pid);
3262 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3263 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3264 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3268 log_debug("%s: Got message", u->id);
3270 /* Interpret MAINPID= */
3271 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3272 (s->state == SERVICE_START ||
3273 s->state == SERVICE_START_POST ||
3274 s->state == SERVICE_RUNNING ||
3275 s->state == SERVICE_RELOAD)) {
3277 if (parse_pid(e + 8, &pid) < 0)
3278 log_warning("Failed to parse notification message %s", e);
3280 log_debug("%s: got %s", u->id, e);
3281 service_set_main_pid(s, pid);
3285 /* Interpret READY= */
3286 if (s->type == SERVICE_NOTIFY &&
3287 s->state == SERVICE_START &&
3288 strv_find(tags, "READY=1")) {
3289 log_debug("%s: got READY=1", u->id);
3291 service_enter_start_post(s);
3294 /* Interpret STATUS= */
3295 e = strv_find_prefix(tags, "STATUS=");
3301 if (!utf8_is_valid(e+7)) {
3302 log_warning("Status message in notification is not UTF-8 clean.");
3308 log_error("Failed to allocate string.");
3312 log_debug("%s: got %s", u->id, e);
3314 free(s->status_text);
3317 free(s->status_text);
3318 s->status_text = NULL;
3322 if (strv_find(tags, "WATCHDOG=1")) {
3323 log_debug("%s: got WATCHDOG=1", u->id);
3324 service_reset_watchdog(s);
3327 /* Notify clients about changed status or main pid */
3328 unit_add_to_dbus_queue(u);
3331 #ifdef HAVE_SYSV_COMPAT
3334 static void sysv_facility_in_insserv_conf(Manager *mgr) {
3338 if (!(f = fopen("/etc/insserv.conf", "re"))) {
3339 r = errno == ENOENT ? 0 : -errno;
3344 char l[LINE_MAX], *t;
3345 char **parsed = NULL;
3347 if (!fgets(l, sizeof(l), f)) {
3352 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r));
3357 if (*t != '$' && *t != '<')
3360 parsed = strv_split(t,WHITESPACE);
3361 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3362 if (parsed && !startswith_no_case (parsed[0], "<interactive>")) {
3365 if (sysv_translate_facility(parsed[0], NULL, &facility) < 0)
3367 if ((u = manager_get_unit(mgr, facility)) && (u->type == UNIT_TARGET)) {
3369 char *dep = NULL, *name, **j;
3371 STRV_FOREACH (j, parsed+1) {
3380 if (sysv_translate_facility(name, NULL, &dep) < 0)
3383 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, e, dep, NULL, true);
3398 static int service_enumerate(Manager *m) {
3402 char *path = NULL, *fpath = NULL, *name = NULL;
3403 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3410 if (m->running_as != MANAGER_SYSTEM)
3413 zero(runlevel_services);
3415 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3416 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3420 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3429 if (!(d = opendir(path))) {
3430 if (errno != ENOENT)
3431 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3436 while ((de = readdir(d))) {
3439 if (ignore_file(de->d_name))
3442 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3445 if (strlen(de->d_name) < 4)
3448 a = undecchar(de->d_name[1]);
3449 b = undecchar(de->d_name[2]);
3455 fpath = strjoin(path, "/", de->d_name, NULL);
3461 if (access(fpath, X_OK) < 0) {
3463 if (errno != ENOENT)
3464 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3470 if (!(name = sysv_translate_name(de->d_name + 3))) {
3475 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
3476 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3480 if (de->d_name[0] == 'S') {
3482 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
3483 SERVICE(service)->sysv_start_priority_from_rcnd =
3484 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3486 SERVICE(service)->sysv_enabled = true;
3489 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3492 if ((r = set_put(runlevel_services[i], service)) < 0)
3495 } else if (de->d_name[0] == 'K' &&
3496 (rcnd_table[i].type == RUNLEVEL_DOWN ||
3497 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
3499 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3502 if ((r = set_put(shutdown_services, service)) < 0)
3508 /* Now we loaded all stubs and are aware of the lowest
3509 start-up priority for all services, not let's actually load
3510 the services, this will also tell us which services are
3511 actually native now */
3512 manager_dispatch_load_queue(m);
3514 /* If this is a native service, rely on native ways to pull in
3515 * a service, don't pull it in via sysv rcN.d links. */
3516 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3517 SET_FOREACH(service, runlevel_services[i], j) {
3518 service = unit_follow_merge(service);
3520 if (service->fragment_path)
3523 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3527 /* We honour K links only for halt/reboot. For the normal
3528 * runlevels we assume the stop jobs will be implicitly added
3529 * by the core logic. Also, we don't really distinguish here
3530 * between the runlevels 0 and 6 and just add them to the
3531 * special shutdown target. On SUSE the boot.d/ runlevel is
3532 * also used for shutdown, so we add links for that too to the
3533 * shutdown target.*/
3534 SET_FOREACH(service, shutdown_services, j) {
3535 service = unit_follow_merge(service);
3537 if (service->fragment_path)
3540 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3547 sysv_facility_in_insserv_conf (m);
3555 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3556 set_free(runlevel_services[i]);
3557 set_free(shutdown_services);
3566 static void service_bus_name_owner_change(
3569 const char *old_owner,
3570 const char *new_owner) {
3572 Service *s = SERVICE(u);
3577 assert(streq(s->bus_name, name));
3578 assert(old_owner || new_owner);
3580 if (old_owner && new_owner)
3581 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
3583 log_debug("%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
3585 log_debug("%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
3587 s->bus_name_good = !!new_owner;
3589 if (s->type == SERVICE_DBUS) {
3591 /* service_enter_running() will figure out what to
3593 if (s->state == SERVICE_RUNNING)
3594 service_enter_running(s, SERVICE_SUCCESS);
3595 else if (s->state == SERVICE_START && new_owner)
3596 service_enter_start_post(s);
3598 } else if (new_owner &&
3600 (s->state == SERVICE_START ||
3601 s->state == SERVICE_START_POST ||
3602 s->state == SERVICE_RUNNING ||
3603 s->state == SERVICE_RELOAD)) {
3605 /* Try to acquire PID from bus service */
3606 log_debug("Trying to acquire PID from D-Bus name...");
3608 bus_query_pid(u->manager, name);
3612 static void service_bus_query_pid_done(
3617 Service *s = SERVICE(u);
3622 log_debug("%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3624 if (s->main_pid <= 0 &&
3625 (s->state == SERVICE_START ||
3626 s->state == SERVICE_START_POST ||
3627 s->state == SERVICE_RUNNING ||
3628 s->state == SERVICE_RELOAD))
3629 service_set_main_pid(s, pid);
3632 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3637 /* This is called by the socket code when instantiating a new
3638 * service for a stream socket and the socket needs to be
3641 if (UNIT(s)->load_state != UNIT_LOADED)
3644 if (s->socket_fd >= 0)
3647 if (s->state != SERVICE_DEAD)
3651 s->got_socket_fd = true;
3653 unit_ref_set(&s->accept_socket, UNIT(sock));
3655 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3658 static void service_reset_failed(Unit *u) {
3659 Service *s = SERVICE(u);
3663 if (s->state == SERVICE_FAILED)
3664 service_set_state(s, SERVICE_DEAD);
3666 s->result = SERVICE_SUCCESS;
3667 s->reload_result = SERVICE_SUCCESS;
3669 RATELIMIT_RESET(s->start_limit);
3672 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3673 Service *s = SERVICE(u);
3675 Set *pid_set = NULL;
3679 if (s->main_pid <= 0 && who == KILL_MAIN) {
3680 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3684 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3685 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3689 if (who == KILL_CONTROL || who == KILL_ALL)
3690 if (s->control_pid > 0)
3691 if (kill(s->control_pid, signo) < 0)
3694 if (who == KILL_MAIN || who == KILL_ALL)
3695 if (s->main_pid > 0)
3696 if (kill(s->main_pid, signo) < 0)
3699 if (who == KILL_ALL) {
3702 pid_set = set_new(trivial_hash_func, trivial_compare_func);
3706 /* Exclude the control/main pid from being killed via the cgroup */
3707 if (s->control_pid > 0) {
3708 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
3715 if (s->main_pid > 0) {
3716 q = set_put(pid_set, LONG_TO_PTR(s->main_pid));
3723 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3724 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3735 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3736 [SERVICE_DEAD] = "dead",
3737 [SERVICE_START_PRE] = "start-pre",
3738 [SERVICE_START] = "start",
3739 [SERVICE_START_POST] = "start-post",
3740 [SERVICE_RUNNING] = "running",
3741 [SERVICE_EXITED] = "exited",
3742 [SERVICE_RELOAD] = "reload",
3743 [SERVICE_STOP] = "stop",
3744 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3745 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3746 [SERVICE_STOP_POST] = "stop-post",
3747 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3748 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3749 [SERVICE_FAILED] = "failed",
3750 [SERVICE_AUTO_RESTART] = "auto-restart",
3753 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3755 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3756 [SERVICE_RESTART_NO] = "no",
3757 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3758 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3759 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3760 [SERVICE_RESTART_ALWAYS] = "always"
3763 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3765 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3766 [SERVICE_SIMPLE] = "simple",
3767 [SERVICE_FORKING] = "forking",
3768 [SERVICE_ONESHOT] = "oneshot",
3769 [SERVICE_DBUS] = "dbus",
3770 [SERVICE_NOTIFY] = "notify",
3771 [SERVICE_IDLE] = "idle"
3774 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3776 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3777 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3778 [SERVICE_EXEC_START] = "ExecStart",
3779 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3780 [SERVICE_EXEC_RELOAD] = "ExecReload",
3781 [SERVICE_EXEC_STOP] = "ExecStop",
3782 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3785 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3787 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3788 [NOTIFY_NONE] = "none",
3789 [NOTIFY_MAIN] = "main",
3790 [NOTIFY_ALL] = "all"
3793 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3795 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3796 [SERVICE_SUCCESS] = "success",
3797 [SERVICE_FAILURE_RESOURCES] = "resources",
3798 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3799 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3800 [SERVICE_FAILURE_SIGNAL] = "signal",
3801 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3802 [SERVICE_FAILURE_WATCHDOG] = "watchdog"
3805 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3807 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3808 [SERVICE_START_LIMIT_NONE] = "none",
3809 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3810 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3811 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3813 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3815 const UnitVTable service_vtable = {
3816 .object_size = sizeof(Service),
3822 .init = service_init,
3823 .done = service_done,
3824 .load = service_load,
3826 .coldplug = service_coldplug,
3828 .dump = service_dump,
3830 .start = service_start,
3831 .stop = service_stop,
3832 .reload = service_reload,
3834 .can_reload = service_can_reload,
3836 .kill = service_kill,
3838 .serialize = service_serialize,
3839 .deserialize_item = service_deserialize_item,
3841 .active_state = service_active_state,
3842 .sub_state_to_string = service_sub_state_to_string,
3844 .check_gc = service_check_gc,
3845 .check_snapshot = service_check_snapshot,
3847 .sigchld_event = service_sigchld_event,
3848 .timer_event = service_timer_event,
3849 .fd_event = service_fd_event,
3851 .reset_failed = service_reset_failed,
3853 .cgroup_notify_empty = service_cgroup_notify_event,
3854 .notify_message = service_notify_message,
3856 .bus_name_owner_change = service_bus_name_owner_change,
3857 .bus_query_pid_done = service_bus_query_pid_done,
3859 .bus_interface = "org.freedesktop.systemd1.Service",
3860 .bus_message_handler = bus_service_message_handler,
3861 .bus_invalidating_properties = bus_service_invalidating_properties,
3863 #ifdef HAVE_SYSV_COMPAT
3864 .enumerate = service_enumerate,
3866 .status_message_formats = {
3867 .starting_stopping = {
3868 [0] = "Starting %s...",
3869 [1] = "Stopping %s...",
3871 .finished_start_job = {
3872 [JOB_DONE] = "Started %s.",
3873 [JOB_FAILED] = "Failed to start %s.",
3874 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3875 [JOB_TIMEOUT] = "Timed out starting %s.",
3877 .finished_stop_job = {
3878 [JOB_DONE] = "Stopped %s.",
3879 [JOB_FAILED] = "Stopped (with error) %s.",
3880 [JOB_TIMEOUT] = "Timed out stopping %s.",