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);
136 assert(u->load_state == UNIT_STUB);
138 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
139 s->restart_usec = DEFAULT_RESTART_USEC;
140 s->type = _SERVICE_TYPE_INVALID;
142 s->watchdog_watch.type = WATCH_INVALID;
144 s->timer_watch.type = WATCH_INVALID;
145 #ifdef HAVE_SYSV_COMPAT
146 s->sysv_start_priority = -1;
147 s->sysv_start_priority_from_rcnd = -1;
150 s->guess_main_pid = true;
152 exec_context_init(&s->exec_context);
153 for (i = 0; i < RLIMIT_NLIMITS; i++)
154 if (UNIT(s)->manager->rlimit[i])
155 s->exec_context.rlimit[i] = newdup(struct rlimit, UNIT(s)->manager->rlimit[i], 1);
157 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
159 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
162 static void service_unwatch_control_pid(Service *s) {
165 if (s->control_pid <= 0)
168 unit_unwatch_pid(UNIT(s), s->control_pid);
172 static void service_unwatch_main_pid(Service *s) {
175 if (s->main_pid <= 0)
178 unit_unwatch_pid(UNIT(s), s->main_pid);
182 static void service_unwatch_pid_file(Service *s) {
183 if (!s->pid_file_pathspec)
186 log_debug("Stopping watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
187 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
188 path_spec_done(s->pid_file_pathspec);
189 free(s->pid_file_pathspec);
190 s->pid_file_pathspec = NULL;
193 static int service_set_main_pid(Service *s, pid_t pid) {
205 s->main_pid_known = true;
207 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
208 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
209 UNIT(s)->id, (unsigned long) pid);
211 s->main_pid_alien = true;
213 s->main_pid_alien = false;
215 exec_status_start(&s->main_exec_status, pid);
220 static void service_close_socket_fd(Service *s) {
223 if (s->socket_fd < 0)
226 close_nointr_nofail(s->socket_fd);
230 static void service_connection_unref(Service *s) {
233 if (!UNIT_DEREF(s->accept_socket))
236 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
237 unit_ref_unset(&s->accept_socket);
240 static void service_stop_watchdog(Service *s) {
243 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
244 s->watchdog_timestamp.realtime = 0;
245 s->watchdog_timestamp.monotonic = 0;
248 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart);
250 static void service_handle_watchdog(Service *s) {
256 if (s->watchdog_usec == 0)
259 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
260 if (offset >= s->watchdog_usec) {
261 log_error("%s watchdog timeout!", UNIT(s)->id);
262 service_enter_dead(s, SERVICE_FAILURE_WATCHDOG, true);
266 r = unit_watch_timer(UNIT(s), s->watchdog_usec - offset, &s->watchdog_watch);
268 log_warning("%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
271 static void service_reset_watchdog(Service *s) {
274 dual_timestamp_get(&s->watchdog_timestamp);
275 service_handle_watchdog(s);
278 static void service_done(Unit *u) {
279 Service *s = SERVICE(u);
286 #ifdef HAVE_SYSV_COMPAT
287 free(s->sysv_runlevels);
288 s->sysv_runlevels = NULL;
291 free(s->status_text);
292 s->status_text = NULL;
294 exec_context_done(&s->exec_context);
295 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
296 s->control_command = NULL;
297 s->main_command = NULL;
299 /* This will leak a process, but at least no memory or any of
301 service_unwatch_main_pid(s);
302 service_unwatch_control_pid(s);
303 service_unwatch_pid_file(s);
306 unit_unwatch_bus_name(u, s->bus_name);
311 service_close_socket_fd(s);
312 service_connection_unref(s);
314 unit_ref_unset(&s->accept_socket);
316 service_stop_watchdog(s);
318 unit_unwatch_timer(u, &s->timer_watch);
321 #ifdef HAVE_SYSV_COMPAT
322 static char *sysv_translate_name(const char *name) {
325 if (!(r = new(char, strlen(name) + sizeof(".service"))))
328 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
329 if (endswith(name, ".sh"))
330 /* Drop Debian-style .sh suffix */
331 strcpy(stpcpy(r, name) - 3, ".service");
334 if (startswith(name, "boot."))
335 /* Drop SuSE-style boot. prefix */
336 strcpy(stpcpy(r, name + 5), ".service");
338 #ifdef TARGET_FRUGALWARE
339 if (startswith(name, "rc."))
340 /* Drop Frugalware-style rc. prefix */
341 strcpy(stpcpy(r, name + 3), ".service");
344 /* Normal init scripts */
345 strcpy(stpcpy(r, name), ".service");
350 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
352 /* We silently ignore the $ prefix here. According to the LSB
353 * spec it simply indicates whether something is a
354 * standardized name or a distribution-specific one. Since we
355 * just follow what already exists and do not introduce new
356 * uses or names we don't care who introduced a new name. */
358 static const char * const table[] = {
359 /* LSB defined facilities */
360 "local_fs", SPECIAL_LOCAL_FS_TARGET,
361 #if defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
363 /* Due to unfortunate name selection in Mandriva,
364 * $network is provided by network-up which is ordered
365 * after network which actually starts interfaces.
366 * To break the loop, just ignore it */
367 "network", SPECIAL_NETWORK_TARGET,
369 "named", SPECIAL_NSS_LOOKUP_TARGET,
370 "portmap", SPECIAL_RPCBIND_TARGET,
371 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
372 "syslog", SPECIAL_SYSLOG_TARGET,
373 "time", SPECIAL_TIME_SYNC_TARGET,
375 /* common extensions */
376 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
377 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
380 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
381 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
385 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
386 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
387 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
391 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
402 n = *name == '$' ? name + 1 : name;
404 for (i = 0; i < ELEMENTSOF(table); i += 2) {
406 if (!streq(table[i], n))
412 if (!(r = strdup(table[i+1])))
418 /* If we don't know this name, fallback heuristics to figure
419 * out whether something is a target or a service alias. */
422 if (!unit_prefix_is_valid(n))
425 /* Facilities starting with $ are most likely targets */
426 r = unit_name_build(n, NULL, ".target");
427 } else if (filename && streq(name, filename))
428 /* Names equaling the file name of the services are redundant */
431 /* Everything else we assume to be normal service names */
432 r = sysv_translate_name(n);
443 static int sysv_fix_order(Service *s) {
449 if (s->sysv_start_priority < 0)
452 /* For each pair of services where at least one lacks a LSB
453 * header, we use the start priority value to order things. */
455 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
458 bool special_s, special_t;
465 if (UNIT(t)->load_state != UNIT_LOADED)
468 if (t->sysv_start_priority < 0)
471 /* If both units have modern headers we don't care
472 * about the priorities */
473 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
474 (UNIT(t)->fragment_path || t->sysv_has_lsb))
477 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
478 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
480 if (special_t && !special_s)
482 else if (special_s && !special_t)
484 else if (t->sysv_start_priority < s->sysv_start_priority)
486 else if (t->sysv_start_priority > s->sysv_start_priority)
491 /* FIXME: Maybe we should compare the name here lexicographically? */
493 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
500 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
503 if (!(c = new0(ExecCommand, 1)))
506 if (!(c->path = strdup(path))) {
511 if (!(c->argv = strv_new(path, arg1, NULL))) {
520 static int sysv_exec_commands(Service *s) {
525 assert(UNIT(s)->source_path);
527 c = exec_command_new(UNIT(s)->source_path, "start");
530 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
532 c = exec_command_new(UNIT(s)->source_path, "stop");
535 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
537 c = exec_command_new(UNIT(s)->source_path, "reload");
540 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
545 static int service_load_sysv_path(Service *s, const char *path) {
556 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
564 f = fopen(path, "re");
566 r = errno == ENOENT ? 0 : -errno;
571 if (fstat(fileno(f), &st) < 0) {
576 free(u->source_path);
577 u->source_path = strdup(path);
578 if (!u->source_path) {
582 u->source_mtime = timespec_load(&st.st_mtim);
584 if (null_or_empty(&st)) {
585 u->load_state = UNIT_MASKED;
593 char l[LINE_MAX], *t;
595 if (!fgets(l, sizeof(l), f)) {
600 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
610 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
612 s->sysv_has_lsb = true;
616 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
622 t += strspn(t, WHITESPACE);
624 if (state == NORMAL) {
626 /* Try to parse Red Hat style chkconfig headers */
628 if (startswith_no_case(t, "chkconfig:")) {
630 char runlevels[16], *k;
634 if (sscanf(t+10, "%15s %i %*i",
636 &start_priority) != 2) {
638 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
642 /* A start priority gathered from the
643 * symlink farms is preferred over the
644 * data from the LSB header. */
645 if (start_priority < 0 || start_priority > 99)
646 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
648 s->sysv_start_priority = start_priority;
650 char_array_0(runlevels);
651 k = delete_chars(runlevels, WHITESPACE "-");
656 if (!(d = strdup(k))) {
661 free(s->sysv_runlevels);
662 s->sysv_runlevels = d;
665 } else if (startswith_no_case(t, "description:")) {
667 size_t k = strlen(t);
671 if (t[k-1] == '\\') {
676 if ((j = strstrip(t+12)) && *j) {
677 if (!(d = strdup(j))) {
684 free(chkconfig_description);
685 chkconfig_description = d;
687 } else if (startswith_no_case(t, "pidfile:")) {
694 if (!path_is_absolute(fn)) {
695 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
699 if (!(fn = strdup(fn))) {
708 } else if (state == DESCRIPTION) {
710 /* Try to parse Red Hat style description
713 size_t k = strlen(t);
721 if ((j = strstrip(t)) && *j) {
724 if (chkconfig_description)
725 d = join(chkconfig_description, " ", j, NULL);
734 free(chkconfig_description);
735 chkconfig_description = d;
738 } else if (state == LSB || state == LSB_DESCRIPTION) {
740 if (startswith_no_case(t, "Provides:")) {
746 FOREACH_WORD_QUOTED(w, z, t+9, i) {
749 if (!(n = strndup(w, z))) {
754 r = sysv_translate_facility(n, path_get_file_name(path), &m);
763 if (unit_name_to_type(m) == UNIT_SERVICE)
764 r = unit_add_name(u, m);
771 * indication that the
773 * now available. This
776 * targets do NOT pull
779 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
782 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
787 } else if (startswith_no_case(t, "Required-Start:") ||
788 startswith_no_case(t, "Should-Start:") ||
789 startswith_no_case(t, "X-Start-Before:") ||
790 startswith_no_case(t, "X-Start-After:")) {
796 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
799 if (!(n = strndup(w, z))) {
804 r = sysv_translate_facility(n, path_get_file_name(path), &m);
807 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
817 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
820 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
824 } else if (startswith_no_case(t, "Default-Start:")) {
829 k = delete_chars(t+14, WHITESPACE "-");
832 if (!(d = strdup(k))) {
837 free(s->sysv_runlevels);
838 s->sysv_runlevels = d;
841 } else if (startswith_no_case(t, "Description:")) {
844 state = LSB_DESCRIPTION;
846 if ((j = strstrip(t+12)) && *j) {
847 if (!(d = strdup(j))) {
854 free(long_description);
855 long_description = d;
857 } else if (startswith_no_case(t, "Short-Description:")) {
862 if ((j = strstrip(t+18)) && *j) {
863 if (!(d = strdup(j))) {
870 free(short_description);
871 short_description = d;
873 } else if (state == LSB_DESCRIPTION) {
875 if (startswith(l, "#\t") || startswith(l, "# ")) {
878 if ((j = strstrip(t)) && *j) {
881 if (long_description)
882 d = join(long_description, " ", t, NULL);
891 free(long_description);
892 long_description = d;
901 if ((r = sysv_exec_commands(s)) < 0)
903 if (s->sysv_runlevels &&
904 chars_intersect(RUNLEVELS_BOOT, s->sysv_runlevels) &&
905 chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
906 /* Service has both boot and "up" runlevels
907 configured. Kill the "up" ones. */
908 delete_chars(s->sysv_runlevels, RUNLEVELS_UP);
911 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
912 /* If there a runlevels configured for this service
913 * but none of the standard ones, then we assume this
914 * is some special kind of service (which might be
915 * needed for early boot) and don't create any links
918 UNIT(s)->default_dependencies = false;
920 /* Don't timeout special services during boot (like fsck) */
923 s->timeout_usec = DEFAULT_SYSV_TIMEOUT_USEC;
925 /* Special setting for all SysV services */
926 s->type = SERVICE_FORKING;
927 s->remain_after_exit = !s->pid_file;
928 s->guess_main_pid = false;
929 s->restart = SERVICE_RESTART_NO;
930 s->exec_context.ignore_sigpipe = false;
932 if (UNIT(s)->manager->sysv_console)
933 s->exec_context.std_output = EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
935 s->exec_context.kill_mode = KILL_PROCESS;
937 /* We use the long description only if
938 * no short description is set. */
940 if (short_description)
941 description = short_description;
942 else if (chkconfig_description)
943 description = chkconfig_description;
944 else if (long_description)
945 description = long_description;
952 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
960 /* The priority that has been set in /etc/rcN.d/ hierarchies
961 * takes precedence over what is stored as default in the LSB
963 if (s->sysv_start_priority_from_rcnd >= 0)
964 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
966 u->load_state = UNIT_LOADED;
974 free(short_description);
975 free(long_description);
976 free(chkconfig_description);
981 static int service_load_sysv_name(Service *s, const char *name) {
987 /* For SysV services we strip the boot.*, rc.* and *.sh
988 * prefixes/suffixes. */
989 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
990 if (endswith(name, ".sh.service"))
995 if (startswith(name, "boot."))
999 #ifdef TARGET_FRUGALWARE
1000 if (startswith(name, "rc."))
1004 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1008 path = join(*p, "/", name, NULL);
1012 assert(endswith(path, ".service"));
1013 path[strlen(path)-8] = 0;
1015 r = service_load_sysv_path(s, path);
1017 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
1018 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1019 /* Try Debian style *.sh source'able init scripts */
1020 strcat(path, ".sh");
1021 r = service_load_sysv_path(s, path);
1027 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1028 /* Try SUSE style boot.* init scripts */
1030 path = join(*p, "/boot.", name, NULL);
1034 /* Drop .service suffix */
1035 path[strlen(path)-8] = 0;
1036 r = service_load_sysv_path(s, path);
1041 #ifdef TARGET_FRUGALWARE
1042 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1043 /* Try Frugalware style rc.* init scripts */
1045 path = join(*p, "/rc.", name, NULL);
1049 /* Drop .service suffix */
1050 path[strlen(path)-8] = 0;
1051 r = service_load_sysv_path(s, path);
1059 if ((UNIT(s)->load_state != UNIT_STUB))
1066 static int service_load_sysv(Service *s) {
1073 /* Load service data from SysV init scripts, preferably with
1074 * LSB headers ... */
1076 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1079 if ((t = UNIT(s)->id))
1080 if ((r = service_load_sysv_name(s, t)) < 0)
1083 if (UNIT(s)->load_state == UNIT_STUB)
1084 SET_FOREACH(t, UNIT(s)->names, i) {
1085 if (t == UNIT(s)->id)
1088 if ((r = service_load_sysv_name(s, t)) < 0)
1091 if (UNIT(s)->load_state != UNIT_STUB)
1099 static int fsck_fix_order(Service *s) {
1105 if (s->fsck_passno <= 0)
1108 /* For each pair of services where both have an fsck priority
1109 * we order things based on it. */
1111 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1120 if (UNIT(t)->load_state != UNIT_LOADED)
1123 if (t->fsck_passno <= 0)
1126 if (t->fsck_passno < s->fsck_passno)
1128 else if (t->fsck_passno > s->fsck_passno)
1133 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1140 static int service_verify(Service *s) {
1143 if (UNIT(s)->load_state != UNIT_LOADED)
1146 if (!s->exec_command[SERVICE_EXEC_START]) {
1147 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1151 if (s->type != SERVICE_ONESHOT &&
1152 s->exec_command[SERVICE_EXEC_START]->command_next) {
1153 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1157 if (s->type == SERVICE_ONESHOT &&
1158 s->exec_command[SERVICE_EXEC_RELOAD]) {
1159 log_error("%s has an ExecReload setting, which is not allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1163 if (s->type == SERVICE_DBUS && !s->bus_name) {
1164 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1168 if (s->bus_name && s->type != SERVICE_DBUS)
1169 log_warning("%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1171 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
1172 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1179 static int service_add_default_dependencies(Service *s) {
1184 /* Add a number of automatic dependencies useful for the
1185 * majority of services. */
1187 /* First, pull in base system */
1188 if (UNIT(s)->manager->running_as == MANAGER_SYSTEM) {
1190 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1193 } else if (UNIT(s)->manager->running_as == MANAGER_USER) {
1195 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1199 /* Second, activate normal shutdown */
1200 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1203 static void service_fix_output(Service *s) {
1206 /* If nothing has been explicitly configured, patch default
1207 * output in. If input is socket/tty we avoid this however,
1208 * since in that case we want output to default to the same
1209 * place as we read input from. */
1211 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1212 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1213 s->exec_context.std_input == EXEC_INPUT_NULL)
1214 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1216 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1217 s->exec_context.std_input == EXEC_INPUT_NULL)
1218 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1221 static int service_load(Unit *u) {
1223 Service *s = SERVICE(u);
1227 /* Load a .service file */
1228 if ((r = unit_load_fragment(u)) < 0)
1231 #ifdef HAVE_SYSV_COMPAT
1232 /* Load a classic init script as a fallback, if we couldn't find anything */
1233 if (u->load_state == UNIT_STUB)
1234 if ((r = service_load_sysv(s)) < 0)
1238 /* Still nothing found? Then let's give up */
1239 if (u->load_state == UNIT_STUB)
1242 /* We were able to load something, then let's add in the
1243 * dropin directories. */
1244 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1247 /* This is a new unit? Then let's add in some extras */
1248 if (u->load_state == UNIT_LOADED) {
1249 if (s->type == _SERVICE_TYPE_INVALID)
1250 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1252 /* Oneshot services have disabled timeout by default */
1253 if (s->type == SERVICE_ONESHOT && !s->timeout_defined)
1254 s->timeout_usec = 0;
1256 service_fix_output(s);
1258 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1261 if ((r = unit_add_default_cgroups(u)) < 0)
1264 #ifdef HAVE_SYSV_COMPAT
1265 if ((r = sysv_fix_order(s)) < 0)
1269 if ((r = fsck_fix_order(s)) < 0)
1273 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1276 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1277 s->notify_access = NOTIFY_MAIN;
1279 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1280 s->notify_access = NOTIFY_MAIN;
1282 if (s->type == SERVICE_DBUS || s->bus_name)
1283 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1286 if (UNIT(s)->default_dependencies)
1287 if ((r = service_add_default_dependencies(s)) < 0)
1291 return service_verify(s);
1294 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1296 ServiceExecCommand c;
1297 Service *s = SERVICE(u);
1298 const char *prefix2;
1303 p2 = strappend(prefix, "\t");
1304 prefix2 = p2 ? p2 : prefix;
1307 "%sService State: %s\n"
1309 "%sReload Result: %s\n"
1310 "%sPermissionsStartOnly: %s\n"
1311 "%sRootDirectoryStartOnly: %s\n"
1312 "%sRemainAfterExit: %s\n"
1313 "%sGuessMainPID: %s\n"
1316 "%sNotifyAccess: %s\n",
1317 prefix, service_state_to_string(s->state),
1318 prefix, service_result_to_string(s->result),
1319 prefix, service_result_to_string(s->reload_result),
1320 prefix, yes_no(s->permissions_start_only),
1321 prefix, yes_no(s->root_directory_start_only),
1322 prefix, yes_no(s->remain_after_exit),
1323 prefix, yes_no(s->guess_main_pid),
1324 prefix, service_type_to_string(s->type),
1325 prefix, service_restart_to_string(s->restart),
1326 prefix, notify_access_to_string(s->notify_access));
1328 if (s->control_pid > 0)
1330 "%sControl PID: %lu\n",
1331 prefix, (unsigned long) s->control_pid);
1333 if (s->main_pid > 0)
1336 "%sMain PID Known: %s\n"
1337 "%sMain PID Alien: %s\n",
1338 prefix, (unsigned long) s->main_pid,
1339 prefix, yes_no(s->main_pid_known),
1340 prefix, yes_no(s->main_pid_alien));
1345 prefix, s->pid_file);
1350 "%sBus Name Good: %s\n",
1351 prefix, s->bus_name,
1352 prefix, yes_no(s->bus_name_good));
1354 exec_context_dump(&s->exec_context, f, prefix);
1356 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1358 if (!s->exec_command[c])
1361 fprintf(f, "%s-> %s:\n",
1362 prefix, service_exec_command_to_string(c));
1364 exec_command_dump_list(s->exec_command[c], f, prefix2);
1367 #ifdef HAVE_SYSV_COMPAT
1370 "%sSysV Init Script has LSB Header: %s\n"
1371 "%sSysVEnabled: %s\n",
1372 prefix, yes_no(s->sysv_has_lsb),
1373 prefix, yes_no(s->sysv_enabled));
1375 if (s->sysv_start_priority >= 0)
1377 "%sSysVStartPriority: %i\n",
1378 prefix, s->sysv_start_priority);
1380 if (s->sysv_runlevels)
1381 fprintf(f, "%sSysVRunLevels: %s\n",
1382 prefix, s->sysv_runlevels);
1385 if (s->fsck_passno > 0)
1387 "%sFsckPassNo: %i\n",
1388 prefix, s->fsck_passno);
1391 fprintf(f, "%sStatus Text: %s\n",
1392 prefix, s->status_text);
1397 static int service_load_pid_file(Service *s, bool may_warn) {
1407 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1409 log_info("PID file %s not readable (yet?) after %s.",
1410 s->pid_file, service_state_to_string(s->state));
1414 r = parse_pid(k, &pid);
1420 if (kill(pid, 0) < 0 && errno != EPERM) {
1422 log_info("PID %lu read from file %s does not exist.",
1423 (unsigned long) pid, s->pid_file);
1427 if (s->main_pid_known) {
1428 if (pid == s->main_pid)
1431 log_debug("Main PID changing: %lu -> %lu",
1432 (unsigned long) s->main_pid, (unsigned long) pid);
1433 service_unwatch_main_pid(s);
1434 s->main_pid_known = false;
1436 log_debug("Main PID loaded: %lu", (unsigned long) pid);
1438 if ((r = service_set_main_pid(s, pid)) < 0)
1441 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1442 /* FIXME: we need to do something here */
1448 static int service_search_main_pid(Service *s) {
1454 /* If we know it anyway, don't ever fallback to unreliable
1456 if (s->main_pid_known)
1459 if (!s->guess_main_pid)
1462 assert(s->main_pid <= 0);
1464 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1467 log_debug("Main PID guessed: %lu", (unsigned long) pid);
1468 if ((r = service_set_main_pid(s, pid)) < 0)
1471 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1472 /* FIXME: we need to do something here */
1478 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1484 /* Notifies all our sockets when we die */
1486 if (s->socket_fd >= 0)
1489 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1490 if (u->type == UNIT_SOCKET)
1491 socket_notify_service_dead(SOCKET(u), failed_permanent);
1496 static void service_set_state(Service *s, ServiceState state) {
1497 ServiceState old_state;
1498 const UnitActiveState *table;
1501 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1503 old_state = s->state;
1506 service_unwatch_pid_file(s);
1508 if (state != SERVICE_START_PRE &&
1509 state != SERVICE_START &&
1510 state != SERVICE_START_POST &&
1511 state != SERVICE_RELOAD &&
1512 state != SERVICE_STOP &&
1513 state != SERVICE_STOP_SIGTERM &&
1514 state != SERVICE_STOP_SIGKILL &&
1515 state != SERVICE_STOP_POST &&
1516 state != SERVICE_FINAL_SIGTERM &&
1517 state != SERVICE_FINAL_SIGKILL &&
1518 state != SERVICE_AUTO_RESTART)
1519 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1521 if (state != SERVICE_START &&
1522 state != SERVICE_START_POST &&
1523 state != SERVICE_RUNNING &&
1524 state != SERVICE_RELOAD &&
1525 state != SERVICE_STOP &&
1526 state != SERVICE_STOP_SIGTERM &&
1527 state != SERVICE_STOP_SIGKILL) {
1528 service_unwatch_main_pid(s);
1529 s->main_command = NULL;
1532 if (state != SERVICE_START_PRE &&
1533 state != SERVICE_START &&
1534 state != SERVICE_START_POST &&
1535 state != SERVICE_RELOAD &&
1536 state != SERVICE_STOP &&
1537 state != SERVICE_STOP_SIGTERM &&
1538 state != SERVICE_STOP_SIGKILL &&
1539 state != SERVICE_STOP_POST &&
1540 state != SERVICE_FINAL_SIGTERM &&
1541 state != SERVICE_FINAL_SIGKILL) {
1542 service_unwatch_control_pid(s);
1543 s->control_command = NULL;
1544 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1547 if (state == SERVICE_DEAD ||
1548 state == SERVICE_STOP ||
1549 state == SERVICE_STOP_SIGTERM ||
1550 state == SERVICE_STOP_SIGKILL ||
1551 state == SERVICE_STOP_POST ||
1552 state == SERVICE_FINAL_SIGTERM ||
1553 state == SERVICE_FINAL_SIGKILL ||
1554 state == SERVICE_FAILED ||
1555 state == SERVICE_AUTO_RESTART)
1556 service_notify_sockets_dead(s, false);
1558 if (state != SERVICE_START_PRE &&
1559 state != SERVICE_START &&
1560 state != SERVICE_START_POST &&
1561 state != SERVICE_RUNNING &&
1562 state != SERVICE_RELOAD &&
1563 state != SERVICE_STOP &&
1564 state != SERVICE_STOP_SIGTERM &&
1565 state != SERVICE_STOP_SIGKILL &&
1566 state != SERVICE_STOP_POST &&
1567 state != SERVICE_FINAL_SIGTERM &&
1568 state != SERVICE_FINAL_SIGKILL &&
1569 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1570 service_close_socket_fd(s);
1571 service_connection_unref(s);
1574 if (state == SERVICE_STOP)
1575 service_stop_watchdog(s);
1577 /* For the inactive states unit_notify() will trim the cgroup,
1578 * but for exit we have to do that ourselves... */
1579 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1580 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1582 if (old_state != state)
1583 log_debug("%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
1585 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1586 s->reload_result = SERVICE_SUCCESS;
1589 static int service_coldplug(Unit *u) {
1590 Service *s = SERVICE(u);
1594 assert(s->state == SERVICE_DEAD);
1596 if (s->deserialized_state != s->state) {
1598 if (s->deserialized_state == SERVICE_START_PRE ||
1599 s->deserialized_state == SERVICE_START ||
1600 s->deserialized_state == SERVICE_START_POST ||
1601 s->deserialized_state == SERVICE_RELOAD ||
1602 s->deserialized_state == SERVICE_STOP ||
1603 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1604 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1605 s->deserialized_state == SERVICE_STOP_POST ||
1606 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1607 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1608 s->deserialized_state == SERVICE_AUTO_RESTART) {
1610 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1613 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1615 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1620 if ((s->deserialized_state == SERVICE_START &&
1621 (s->type == SERVICE_FORKING ||
1622 s->type == SERVICE_DBUS ||
1623 s->type == SERVICE_ONESHOT ||
1624 s->type == SERVICE_NOTIFY)) ||
1625 s->deserialized_state == SERVICE_START_POST ||
1626 s->deserialized_state == SERVICE_RUNNING ||
1627 s->deserialized_state == SERVICE_RELOAD ||
1628 s->deserialized_state == SERVICE_STOP ||
1629 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1630 s->deserialized_state == SERVICE_STOP_SIGKILL)
1631 if (s->main_pid > 0)
1632 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1635 if (s->deserialized_state == SERVICE_START_PRE ||
1636 s->deserialized_state == SERVICE_START ||
1637 s->deserialized_state == SERVICE_START_POST ||
1638 s->deserialized_state == SERVICE_RELOAD ||
1639 s->deserialized_state == SERVICE_STOP ||
1640 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1641 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1642 s->deserialized_state == SERVICE_STOP_POST ||
1643 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1644 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1645 if (s->control_pid > 0)
1646 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1649 if (s->deserialized_state == SERVICE_START_POST ||
1650 s->deserialized_state == SERVICE_RUNNING)
1651 service_handle_watchdog(s);
1653 service_set_state(s, s->deserialized_state);
1658 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1662 unsigned rn_fds = 0;
1669 if (s->socket_fd >= 0)
1672 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1677 if (u->type != UNIT_SOCKET)
1682 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1694 if (!(t = new(int, rn_fds+cn_fds))) {
1700 memcpy(t, rfds, rn_fds * sizeof(int));
1701 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1706 rn_fds = rn_fds+cn_fds;
1721 static int service_spawn(
1726 bool apply_permissions,
1728 bool apply_tty_stdin,
1729 bool set_notify_socket,
1735 int *fds = NULL, *fdsbuf = NULL;
1736 unsigned n_fds = 0, n_env = 0;
1737 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1744 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1745 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1746 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1748 if (s->socket_fd >= 0) {
1749 fds = &s->socket_fd;
1752 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1759 if (timeout && s->timeout_usec) {
1760 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1763 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1765 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1770 if (!(our_env = new0(char*, 4))) {
1775 if (set_notify_socket)
1776 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1781 if (s->main_pid > 0)
1782 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1787 if (s->watchdog_usec > 0)
1788 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1793 if (!(final_env = strv_env_merge(2,
1794 UNIT(s)->manager->environment,
1809 UNIT(s)->manager->confirm_spawn,
1810 UNIT(s)->cgroup_bondings,
1811 UNIT(s)->cgroup_attributes,
1812 is_control ? "control" : NULL,
1814 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1821 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1822 /* FIXME: we need to do something here */
1828 strv_free(final_env);
1838 strv_free(final_env);
1841 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1846 static int main_pid_good(Service *s) {
1849 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1852 /* If we know the pid file, then lets just check if it is
1854 if (s->main_pid_known) {
1856 /* If it's an alien child let's check if it is still
1858 if (s->main_pid_alien)
1859 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1861 /* .. otherwise assume we'll get a SIGCHLD for it,
1862 * which we really should wait for to collect exit
1863 * status and code */
1864 return s->main_pid > 0;
1867 /* We don't know the pid */
1871 static int control_pid_good(Service *s) {
1874 return s->control_pid > 0;
1877 static int cgroup_good(Service *s) {
1882 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1888 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1892 if (f != SERVICE_SUCCESS)
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);
1909 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1911 s->forbid_restart = false;
1916 log_warning("%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1917 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1920 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1922 static void service_enter_stop_post(Service *s, ServiceResult f) {
1926 if (f != SERVICE_SUCCESS)
1929 service_unwatch_control_pid(s);
1931 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1932 s->control_command_id = SERVICE_EXEC_STOP_POST;
1934 r = service_spawn(s,
1938 !s->permissions_start_only,
1939 !s->root_directory_start_only,
1948 service_set_state(s, SERVICE_STOP_POST);
1950 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1955 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
1956 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1959 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1961 Set *pid_set = NULL;
1962 bool wait_for_exit = false;
1966 if (f != SERVICE_SUCCESS)
1969 if (s->exec_context.kill_mode != KILL_NONE) {
1970 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1972 if (s->main_pid > 0) {
1973 if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
1974 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1976 wait_for_exit = !s->main_pid_alien;
1979 if (s->control_pid > 0) {
1980 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1981 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1983 wait_for_exit = true;
1986 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1988 if (!(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);
2261 log_info("Job pending for unit, delaying automatic restart.");
2263 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
2269 /* Any units that are bound to this service must also be
2270 * restarted. We use JOB_RESTART (instead of the more obvious
2271 * JOB_START) here so that those dependency jobs will be added
2273 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2277 log_debug("%s scheduled restart job.", UNIT(s)->id);
2281 log_warning("%s failed to schedule restart job: %s", UNIT(s)->id, bus_error(&error, -r));
2282 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2284 dbus_error_free(&error);
2287 static void service_enter_reload(Service *s) {
2292 service_unwatch_control_pid(s);
2294 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2295 s->control_command_id = SERVICE_EXEC_RELOAD;
2297 r = service_spawn(s,
2301 !s->permissions_start_only,
2302 !s->root_directory_start_only,
2310 service_set_state(s, SERVICE_RELOAD);
2312 service_enter_running(s, SERVICE_SUCCESS);
2317 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
2318 s->reload_result = SERVICE_FAILURE_RESOURCES;
2319 service_enter_running(s, SERVICE_SUCCESS);
2322 static void service_run_next_control(Service *s) {
2326 assert(s->control_command);
2327 assert(s->control_command->command_next);
2329 assert(s->control_command_id != SERVICE_EXEC_START);
2331 s->control_command = s->control_command->command_next;
2332 service_unwatch_control_pid(s);
2334 r = service_spawn(s,
2338 !s->permissions_start_only,
2339 !s->root_directory_start_only,
2340 s->control_command_id == SERVICE_EXEC_START_PRE ||
2341 s->control_command_id == SERVICE_EXEC_STOP_POST,
2351 log_warning("%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
2353 if (s->state == SERVICE_START_PRE)
2354 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2355 else if (s->state == SERVICE_STOP)
2356 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2357 else if (s->state == SERVICE_STOP_POST)
2358 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2359 else if (s->state == SERVICE_RELOAD) {
2360 s->reload_result = SERVICE_FAILURE_RESOURCES;
2361 service_enter_running(s, SERVICE_SUCCESS);
2363 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2366 static void service_run_next_main(Service *s) {
2371 assert(s->main_command);
2372 assert(s->main_command->command_next);
2373 assert(s->type == SERVICE_ONESHOT);
2375 s->main_command = s->main_command->command_next;
2376 service_unwatch_main_pid(s);
2378 r = service_spawn(s,
2385 s->notify_access != NOTIFY_NONE,
2391 service_set_main_pid(s, pid);
2396 log_warning("%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2397 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2400 static int service_start_limit_test(Service *s) {
2403 if (ratelimit_test(&s->start_limit))
2406 switch (s->start_limit_action) {
2408 case SERVICE_START_LIMIT_NONE:
2409 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s)->id);
2412 case SERVICE_START_LIMIT_REBOOT: {
2416 dbus_error_init(&error);
2418 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2420 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2422 log_error("Failed to reboot: %s.", bus_error(&error, r));
2423 dbus_error_free(&error);
2429 case SERVICE_START_LIMIT_REBOOT_FORCE:
2430 log_warning("%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2431 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2434 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2435 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2436 reboot(RB_AUTOBOOT);
2440 log_error("start limit action=%i", s->start_limit_action);
2441 assert_not_reached("Unknown StartLimitAction.");
2447 static int service_start(Unit *u) {
2448 Service *s = SERVICE(u);
2453 /* We cannot fulfill this request right now, try again later
2455 if (s->state == SERVICE_STOP ||
2456 s->state == SERVICE_STOP_SIGTERM ||
2457 s->state == SERVICE_STOP_SIGKILL ||
2458 s->state == SERVICE_STOP_POST ||
2459 s->state == SERVICE_FINAL_SIGTERM ||
2460 s->state == SERVICE_FINAL_SIGKILL)
2463 /* Already on it! */
2464 if (s->state == SERVICE_START_PRE ||
2465 s->state == SERVICE_START ||
2466 s->state == SERVICE_START_POST)
2469 /* A service that will be restarted must be stopped first to
2470 * trigger BindTo and/or OnFailure dependencies. If a user
2471 * does not want to wait for the holdoff time to elapse, the
2472 * service should be manually restarted, not started. */
2473 if (s->state == SERVICE_AUTO_RESTART) {
2474 log_warning("%s automatic restart is pending, must be stopped before issuing start request.", UNIT(s)->id);
2478 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2480 /* Make sure we don't enter a busy loop of some kind. */
2481 r = service_start_limit_test(s);
2483 service_notify_sockets_dead(s, true);
2487 s->result = SERVICE_SUCCESS;
2488 s->reload_result = SERVICE_SUCCESS;
2489 s->main_pid_known = false;
2490 s->main_pid_alien = false;
2491 s->forbid_restart = false;
2493 service_enter_start_pre(s);
2497 static int service_stop(Unit *u) {
2498 Service *s = SERVICE(u);
2502 /* Don't create restart jobs from here. */
2503 s->forbid_restart = true;
2506 if (s->state == SERVICE_STOP ||
2507 s->state == SERVICE_STOP_SIGTERM ||
2508 s->state == SERVICE_STOP_SIGKILL ||
2509 s->state == SERVICE_STOP_POST ||
2510 s->state == SERVICE_FINAL_SIGTERM ||
2511 s->state == SERVICE_FINAL_SIGKILL)
2514 /* A restart will be scheduled or is in progress. */
2515 if (s->state == SERVICE_AUTO_RESTART) {
2516 service_enter_dead(s, SERVICE_SUCCESS, false);
2520 /* If there's already something running we go directly into
2522 if (s->state == SERVICE_START_PRE ||
2523 s->state == SERVICE_START ||
2524 s->state == SERVICE_START_POST ||
2525 s->state == SERVICE_RELOAD) {
2526 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2530 assert(s->state == SERVICE_RUNNING ||
2531 s->state == SERVICE_EXITED);
2533 service_enter_stop(s, SERVICE_SUCCESS);
2537 static int service_reload(Unit *u) {
2538 Service *s = SERVICE(u);
2542 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2544 service_enter_reload(s);
2548 static bool service_can_reload(Unit *u) {
2549 Service *s = SERVICE(u);
2553 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2556 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2557 Service *s = SERVICE(u);
2563 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2564 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2565 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2567 if (s->control_pid > 0)
2568 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2570 if (s->main_pid_known && s->main_pid > 0)
2571 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2573 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2576 unit_serialize_item(u, f, "status-text", s->status_text);
2578 /* FIXME: There's a minor uncleanliness here: if there are
2579 * multiple commands attached here, we will start from the
2580 * first one again */
2581 if (s->control_command_id >= 0)
2582 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2584 if (s->socket_fd >= 0) {
2587 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2590 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2593 if (s->main_exec_status.pid > 0) {
2594 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2595 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2596 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2598 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2599 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2600 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2603 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2604 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2609 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2610 Service *s = SERVICE(u);
2617 if (streq(key, "state")) {
2620 if ((state = service_state_from_string(value)) < 0)
2621 log_debug("Failed to parse state value %s", value);
2623 s->deserialized_state = state;
2624 } else if (streq(key, "result")) {
2627 f = service_result_from_string(value);
2629 log_debug("Failed to parse result value %s", value);
2630 else if (f != SERVICE_SUCCESS)
2633 } else if (streq(key, "reload-result")) {
2636 f = service_result_from_string(value);
2638 log_debug("Failed to parse reload result value %s", value);
2639 else if (f != SERVICE_SUCCESS)
2640 s->reload_result = f;
2642 } else if (streq(key, "control-pid")) {
2645 if (parse_pid(value, &pid) < 0)
2646 log_debug("Failed to parse control-pid value %s", value);
2648 s->control_pid = pid;
2649 } else if (streq(key, "main-pid")) {
2652 if (parse_pid(value, &pid) < 0)
2653 log_debug("Failed to parse main-pid value %s", value);
2655 service_set_main_pid(s, (pid_t) pid);
2656 } else if (streq(key, "main-pid-known")) {
2659 if ((b = parse_boolean(value)) < 0)
2660 log_debug("Failed to parse main-pid-known value %s", value);
2662 s->main_pid_known = b;
2663 } else if (streq(key, "status-text")) {
2666 if ((t = strdup(value))) {
2667 free(s->status_text);
2671 } else if (streq(key, "control-command")) {
2672 ServiceExecCommand id;
2674 if ((id = service_exec_command_from_string(value)) < 0)
2675 log_debug("Failed to parse exec-command value %s", value);
2677 s->control_command_id = id;
2678 s->control_command = s->exec_command[id];
2680 } else if (streq(key, "socket-fd")) {
2683 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2684 log_debug("Failed to parse socket-fd value %s", value);
2687 if (s->socket_fd >= 0)
2688 close_nointr_nofail(s->socket_fd);
2689 s->socket_fd = fdset_remove(fds, fd);
2691 } else if (streq(key, "main-exec-status-pid")) {
2694 if (parse_pid(value, &pid) < 0)
2695 log_debug("Failed to parse main-exec-status-pid value %s", value);
2697 s->main_exec_status.pid = pid;
2698 } else if (streq(key, "main-exec-status-code")) {
2701 if (safe_atoi(value, &i) < 0)
2702 log_debug("Failed to parse main-exec-status-code value %s", value);
2704 s->main_exec_status.code = i;
2705 } else if (streq(key, "main-exec-status-status")) {
2708 if (safe_atoi(value, &i) < 0)
2709 log_debug("Failed to parse main-exec-status-status value %s", value);
2711 s->main_exec_status.status = i;
2712 } else if (streq(key, "main-exec-status-start"))
2713 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2714 else if (streq(key, "main-exec-status-exit"))
2715 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2716 else if (streq(key, "watchdog-timestamp"))
2717 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2719 log_debug("Unknown serialization key '%s'", key);
2724 static UnitActiveState service_active_state(Unit *u) {
2725 const UnitActiveState *table;
2729 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2731 return table[SERVICE(u)->state];
2734 static const char *service_sub_state_to_string(Unit *u) {
2737 return service_state_to_string(SERVICE(u)->state);
2740 static bool service_check_gc(Unit *u) {
2741 Service *s = SERVICE(u);
2745 /* Never clean up services that still have a process around,
2746 * even if the service is formally dead. */
2747 if (cgroup_good(s) > 0 ||
2748 main_pid_good(s) > 0 ||
2749 control_pid_good(s) > 0)
2752 #ifdef HAVE_SYSV_COMPAT
2760 static bool service_check_snapshot(Unit *u) {
2761 Service *s = SERVICE(u);
2765 return !s->got_socket_fd;
2768 static int service_retry_pid_file(Service *s) {
2771 assert(s->pid_file);
2772 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2774 r = service_load_pid_file(s, false);
2778 service_unwatch_pid_file(s);
2780 service_enter_running(s, SERVICE_SUCCESS);
2784 static int service_watch_pid_file(Service *s) {
2787 log_debug("Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
2788 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2792 /* the pidfile might have appeared just before we set the watch */
2793 service_retry_pid_file(s);
2797 log_error("Failed to set a watch for %s's PID file %s: %s",
2798 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2799 service_unwatch_pid_file(s);
2803 static int service_demand_pid_file(Service *s) {
2806 assert(s->pid_file);
2807 assert(!s->pid_file_pathspec);
2809 ps = new0(PathSpec, 1);
2813 ps->path = strdup(s->pid_file);
2819 path_kill_slashes(ps->path);
2821 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2822 * keep their PID file open all the time. */
2823 ps->type = PATH_MODIFIED;
2824 ps->inotify_fd = -1;
2826 s->pid_file_pathspec = ps;
2828 return service_watch_pid_file(s);
2831 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2832 Service *s = SERVICE(u);
2836 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2837 assert(s->pid_file_pathspec);
2838 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2840 log_debug("inotify event for %s", u->id);
2842 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2845 if (service_retry_pid_file(s) == 0)
2848 if (service_watch_pid_file(s) < 0)
2853 service_unwatch_pid_file(s);
2854 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2857 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2858 Service *s = SERVICE(u);
2864 if (UNIT(s)->fragment_path ? is_clean_exit(code, status) : is_clean_exit_lsb(code, status))
2865 f = SERVICE_SUCCESS;
2866 else if (code == CLD_EXITED)
2867 f = SERVICE_FAILURE_EXIT_CODE;
2868 else if (code == CLD_KILLED)
2869 f = SERVICE_FAILURE_SIGNAL;
2870 else if (code == CLD_DUMPED)
2871 f = SERVICE_FAILURE_CORE_DUMP;
2873 assert_not_reached("Unknown code");
2875 if (s->main_pid == pid) {
2876 /* Forking services may occasionally move to a new PID.
2877 * As long as they update the PID file before exiting the old
2878 * PID, they're fine. */
2879 if (service_load_pid_file(s, false) == 0)
2883 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2885 /* If this is not a forking service than the main
2886 * process got started and hence we copy the exit
2887 * status so that it is recorded both as main and as
2888 * control process exit status */
2889 if (s->main_command) {
2890 s->main_command->exec_status = s->main_exec_status;
2892 if (s->main_command->ignore)
2893 f = SERVICE_SUCCESS;
2896 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2897 "%s: main process exited, code=%s, status=%i", u->id, sigchld_code_to_string(code), status);
2899 if (f != SERVICE_SUCCESS)
2902 if (s->main_command &&
2903 s->main_command->command_next &&
2904 f == SERVICE_SUCCESS) {
2906 /* There is another command to *
2907 * execute, so let's do that. */
2909 log_debug("%s running next main command for state %s", u->id, service_state_to_string(s->state));
2910 service_run_next_main(s);
2914 /* The service exited, so the service is officially
2916 s->main_command = NULL;
2920 case SERVICE_START_POST:
2921 case SERVICE_RELOAD:
2923 /* Need to wait until the operation is
2928 if (s->type == SERVICE_ONESHOT) {
2929 /* This was our main goal, so let's go on */
2930 if (f == SERVICE_SUCCESS)
2931 service_enter_start_post(s);
2933 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2936 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2941 case SERVICE_RUNNING:
2942 service_enter_running(s, f);
2945 case SERVICE_STOP_SIGTERM:
2946 case SERVICE_STOP_SIGKILL:
2948 if (!control_pid_good(s))
2949 service_enter_stop_post(s, f);
2951 /* If there is still a control process, wait for that first */
2955 assert_not_reached("Uh, main process died at wrong time.");
2959 } else if (s->control_pid == pid) {
2963 if (s->control_command) {
2964 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2966 if (s->control_command->ignore)
2967 f = SERVICE_SUCCESS;
2970 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2971 "%s: control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
2973 if (f != SERVICE_SUCCESS)
2976 /* Immediately get rid of the cgroup, so that the
2977 * kernel doesn't delay the cgroup empty messages for
2978 * the service cgroup any longer than necessary */
2979 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2981 if (s->control_command &&
2982 s->control_command->command_next &&
2983 f == SERVICE_SUCCESS) {
2985 /* There is another command to *
2986 * execute, so let's do that. */
2988 log_debug("%s running next control command for state %s", u->id, service_state_to_string(s->state));
2989 service_run_next_control(s);
2992 /* No further commands for this step, so let's
2993 * figure out what to do next */
2995 s->control_command = NULL;
2996 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2998 log_debug("%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
3002 case SERVICE_START_PRE:
3003 if (f == SERVICE_SUCCESS)
3004 service_enter_start(s);
3006 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3010 assert(s->type == SERVICE_FORKING);
3012 if (f != SERVICE_SUCCESS) {
3013 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3018 bool has_start_post;
3021 /* Let's try to load the pid file here if we can.
3022 * The PID file might actually be created by a START_POST
3023 * script. In that case don't worry if the loading fails. */
3025 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3026 r = service_load_pid_file(s, !has_start_post);
3027 if (!has_start_post && r < 0) {
3028 r = service_demand_pid_file(s);
3029 if (r < 0 || !cgroup_good(s))
3030 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3034 service_search_main_pid(s);
3036 service_enter_start_post(s);
3039 case SERVICE_START_POST:
3040 if (f != SERVICE_SUCCESS) {
3041 service_enter_stop(s, f);
3048 r = service_load_pid_file(s, true);
3050 r = service_demand_pid_file(s);
3051 if (r < 0 || !cgroup_good(s))
3052 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3056 service_search_main_pid(s);
3058 service_enter_running(s, SERVICE_SUCCESS);
3061 case SERVICE_RELOAD:
3062 if (f == SERVICE_SUCCESS) {
3063 service_load_pid_file(s, true);
3064 service_search_main_pid(s);
3067 s->reload_result = f;
3068 service_enter_running(s, SERVICE_SUCCESS);
3072 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3075 case SERVICE_STOP_SIGTERM:
3076 case SERVICE_STOP_SIGKILL:
3077 if (main_pid_good(s) <= 0)
3078 service_enter_stop_post(s, f);
3080 /* If there is still a service
3081 * process around, wait until
3082 * that one quit, too */
3085 case SERVICE_STOP_POST:
3086 case SERVICE_FINAL_SIGTERM:
3087 case SERVICE_FINAL_SIGKILL:
3088 service_enter_dead(s, f, true);
3092 assert_not_reached("Uh, control process died at wrong time.");
3097 /* Notify clients about changed exit status */
3098 unit_add_to_dbus_queue(u);
3101 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3102 Service *s = SERVICE(u);
3105 assert(elapsed == 1);
3107 if (w == &s->watchdog_watch) {
3108 service_handle_watchdog(s);
3112 assert(w == &s->timer_watch);
3116 case SERVICE_START_PRE:
3118 log_warning("%s operation timed out. Terminating.", u->id);
3119 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3122 case SERVICE_START_POST:
3123 log_warning("%s operation timed out. Stopping.", u->id);
3124 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3127 case SERVICE_RELOAD:
3128 log_warning("%s operation timed out. Stopping.", u->id);
3129 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3130 service_enter_running(s, SERVICE_SUCCESS);
3134 log_warning("%s stopping timed out. Terminating.", u->id);
3135 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3138 case SERVICE_STOP_SIGTERM:
3139 if (s->exec_context.send_sigkill) {
3140 log_warning("%s stopping timed out. Killing.", u->id);
3141 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3143 log_warning("%s stopping timed out. Skipping SIGKILL.", u->id);
3144 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3149 case SERVICE_STOP_SIGKILL:
3150 /* Uh, we sent a SIGKILL and it is still not gone?
3151 * Must be something we cannot kill, so let's just be
3152 * weirded out and continue */
3154 log_warning("%s still around after SIGKILL. Ignoring.", u->id);
3155 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3158 case SERVICE_STOP_POST:
3159 log_warning("%s stopping timed out (2). Terminating.", u->id);
3160 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3163 case SERVICE_FINAL_SIGTERM:
3164 if (s->exec_context.send_sigkill) {
3165 log_warning("%s stopping timed out (2). Killing.", u->id);
3166 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3168 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->id);
3169 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3174 case SERVICE_FINAL_SIGKILL:
3175 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->id);
3176 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3179 case SERVICE_AUTO_RESTART:
3180 log_info("%s holdoff time over, scheduling restart.", u->id);
3181 service_enter_restart(s);
3185 assert_not_reached("Timeout at wrong time.");
3189 static void service_cgroup_notify_event(Unit *u) {
3190 Service *s = SERVICE(u);
3194 log_debug("%s: cgroup is empty", u->id);
3198 /* Waiting for SIGCHLD is usually more interesting,
3199 * because it includes return codes/signals. Which is
3200 * why we ignore the cgroup events for most cases,
3201 * except when we don't know pid which to expect the
3205 case SERVICE_START_POST:
3206 /* If we were hoping for the daemon to write its PID file,
3207 * we can give up now. */
3208 if (s->pid_file_pathspec) {
3209 log_warning("%s never wrote its PID file. Failing.", UNIT(s)->id);
3210 service_unwatch_pid_file(s);
3211 if (s->state == SERVICE_START)
3212 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3214 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3218 case SERVICE_RUNNING:
3219 /* service_enter_running() will figure out what to do */
3220 service_enter_running(s, SERVICE_SUCCESS);
3223 case SERVICE_STOP_SIGTERM:
3224 case SERVICE_STOP_SIGKILL:
3226 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3227 service_enter_stop_post(s, SERVICE_SUCCESS);
3231 case SERVICE_FINAL_SIGTERM:
3232 case SERVICE_FINAL_SIGKILL:
3233 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3234 service_enter_dead(s, SERVICE_SUCCESS, SERVICE_SUCCESS);
3243 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3244 Service *s = SERVICE(u);
3249 if (s->notify_access == NOTIFY_NONE) {
3250 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3251 u->id, (unsigned long) pid);
3255 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3256 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3257 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3261 log_debug("%s: Got message", u->id);
3263 /* Interpret MAINPID= */
3264 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3265 (s->state == SERVICE_START ||
3266 s->state == SERVICE_START_POST ||
3267 s->state == SERVICE_RUNNING ||
3268 s->state == SERVICE_RELOAD)) {
3270 if (parse_pid(e + 8, &pid) < 0)
3271 log_warning("Failed to parse notification message %s", e);
3273 log_debug("%s: got %s", u->id, e);
3274 service_set_main_pid(s, pid);
3278 /* Interpret READY= */
3279 if (s->type == SERVICE_NOTIFY &&
3280 s->state == SERVICE_START &&
3281 strv_find(tags, "READY=1")) {
3282 log_debug("%s: got READY=1", u->id);
3284 service_enter_start_post(s);
3287 /* Interpret STATUS= */
3288 e = strv_find_prefix(tags, "STATUS=");
3294 if (!utf8_is_valid(e+7)) {
3295 log_warning("Status message in notification is not UTF-8 clean.");
3301 log_error("Failed to allocate string.");
3305 log_debug("%s: got %s", u->id, e);
3307 free(s->status_text);
3310 free(s->status_text);
3311 s->status_text = NULL;
3315 if (strv_find(tags, "WATCHDOG=1")) {
3316 log_debug("%s: got WATCHDOG=1", u->id);
3317 service_reset_watchdog(s);
3320 /* Notify clients about changed status or main pid */
3321 unit_add_to_dbus_queue(u);
3324 #ifdef HAVE_SYSV_COMPAT
3327 static void sysv_facility_in_insserv_conf(Manager *mgr) {
3331 if (!(f = fopen("/etc/insserv.conf", "re"))) {
3332 r = errno == ENOENT ? 0 : -errno;
3337 char l[LINE_MAX], *t;
3338 char **parsed = NULL;
3340 if (!fgets(l, sizeof(l), f)) {
3345 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r));
3350 if (*t != '$' && *t != '<')
3353 parsed = strv_split(t,WHITESPACE);
3354 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3355 if (parsed && !startswith_no_case (parsed[0], "<interactive>")) {
3358 if (sysv_translate_facility(parsed[0], NULL, &facility) < 0)
3360 if ((u = manager_get_unit(mgr, facility)) && (u->type == UNIT_TARGET)) {
3362 char *dep = NULL, *name, **j;
3364 STRV_FOREACH (j, parsed+1) {
3373 if (sysv_translate_facility(name, NULL, &dep) < 0)
3376 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, e, dep, NULL, true);
3391 static int service_enumerate(Manager *m) {
3395 char *path = NULL, *fpath = NULL, *name = NULL;
3396 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3403 if (m->running_as != MANAGER_SYSTEM)
3406 zero(runlevel_services);
3408 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3409 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3413 path = join(*p, "/", rcnd_table[i].path, NULL);
3422 if (!(d = opendir(path))) {
3423 if (errno != ENOENT)
3424 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3429 while ((de = readdir(d))) {
3432 if (ignore_file(de->d_name))
3435 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3438 if (strlen(de->d_name) < 4)
3441 a = undecchar(de->d_name[1]);
3442 b = undecchar(de->d_name[2]);
3448 fpath = join(path, "/", de->d_name, NULL);
3454 if (access(fpath, X_OK) < 0) {
3456 if (errno != ENOENT)
3457 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3463 if (!(name = sysv_translate_name(de->d_name + 3))) {
3468 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
3469 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3473 if (de->d_name[0] == 'S') {
3475 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
3476 SERVICE(service)->sysv_start_priority_from_rcnd =
3477 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3479 SERVICE(service)->sysv_enabled = true;
3482 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3485 if ((r = set_put(runlevel_services[i], service)) < 0)
3488 } else if (de->d_name[0] == 'K' &&
3489 (rcnd_table[i].type == RUNLEVEL_DOWN ||
3490 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
3492 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3495 if ((r = set_put(shutdown_services, service)) < 0)
3501 /* Now we loaded all stubs and are aware of the lowest
3502 start-up priority for all services, not let's actually load
3503 the services, this will also tell us which services are
3504 actually native now */
3505 manager_dispatch_load_queue(m);
3507 /* If this is a native service, rely on native ways to pull in
3508 * a service, don't pull it in via sysv rcN.d links. */
3509 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3510 SET_FOREACH(service, runlevel_services[i], j) {
3511 service = unit_follow_merge(service);
3513 if (service->fragment_path)
3516 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3520 /* We honour K links only for halt/reboot. For the normal
3521 * runlevels we assume the stop jobs will be implicitly added
3522 * by the core logic. Also, we don't really distinguish here
3523 * between the runlevels 0 and 6 and just add them to the
3524 * special shutdown target. On SUSE the boot.d/ runlevel is
3525 * also used for shutdown, so we add links for that too to the
3526 * shutdown target.*/
3527 SET_FOREACH(service, shutdown_services, j) {
3528 service = unit_follow_merge(service);
3530 if (service->fragment_path)
3533 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3540 sysv_facility_in_insserv_conf (m);
3548 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3549 set_free(runlevel_services[i]);
3550 set_free(shutdown_services);
3559 static void service_bus_name_owner_change(
3562 const char *old_owner,
3563 const char *new_owner) {
3565 Service *s = SERVICE(u);
3570 assert(streq(s->bus_name, name));
3571 assert(old_owner || new_owner);
3573 if (old_owner && new_owner)
3574 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
3576 log_debug("%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
3578 log_debug("%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
3580 s->bus_name_good = !!new_owner;
3582 if (s->type == SERVICE_DBUS) {
3584 /* service_enter_running() will figure out what to
3586 if (s->state == SERVICE_RUNNING)
3587 service_enter_running(s, SERVICE_SUCCESS);
3588 else if (s->state == SERVICE_START && new_owner)
3589 service_enter_start_post(s);
3591 } else if (new_owner &&
3593 (s->state == SERVICE_START ||
3594 s->state == SERVICE_START_POST ||
3595 s->state == SERVICE_RUNNING ||
3596 s->state == SERVICE_RELOAD)) {
3598 /* Try to acquire PID from bus service */
3599 log_debug("Trying to acquire PID from D-Bus name...");
3601 bus_query_pid(u->manager, name);
3605 static void service_bus_query_pid_done(
3610 Service *s = SERVICE(u);
3615 log_debug("%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3617 if (s->main_pid <= 0 &&
3618 (s->state == SERVICE_START ||
3619 s->state == SERVICE_START_POST ||
3620 s->state == SERVICE_RUNNING ||
3621 s->state == SERVICE_RELOAD))
3622 service_set_main_pid(s, pid);
3625 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3630 /* This is called by the socket code when instantiating a new
3631 * service for a stream socket and the socket needs to be
3634 if (UNIT(s)->load_state != UNIT_LOADED)
3637 if (s->socket_fd >= 0)
3640 if (s->state != SERVICE_DEAD)
3644 s->got_socket_fd = true;
3646 unit_ref_set(&s->accept_socket, UNIT(sock));
3648 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3651 static void service_reset_failed(Unit *u) {
3652 Service *s = SERVICE(u);
3656 if (s->state == SERVICE_FAILED)
3657 service_set_state(s, SERVICE_DEAD);
3659 s->result = SERVICE_SUCCESS;
3660 s->reload_result = SERVICE_SUCCESS;
3663 static int service_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
3664 Service *s = SERVICE(u);
3666 Set *pid_set = NULL;
3670 if (s->main_pid <= 0 && who == KILL_MAIN) {
3671 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3675 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3676 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3680 if (who == KILL_CONTROL || who == KILL_ALL)
3681 if (s->control_pid > 0)
3682 if (kill(s->control_pid, signo) < 0)
3685 if (who == KILL_MAIN || who == KILL_ALL)
3686 if (s->main_pid > 0)
3687 if (kill(s->main_pid, signo) < 0)
3690 if (who == KILL_ALL && mode == KILL_CONTROL_GROUP) {
3693 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
3696 /* Exclude the control/main pid from being killed via the cgroup */
3697 if (s->control_pid > 0)
3698 if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
3703 if (s->main_pid > 0)
3704 if ((q = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0) {
3708 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3710 if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3721 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3722 [SERVICE_DEAD] = "dead",
3723 [SERVICE_START_PRE] = "start-pre",
3724 [SERVICE_START] = "start",
3725 [SERVICE_START_POST] = "start-post",
3726 [SERVICE_RUNNING] = "running",
3727 [SERVICE_EXITED] = "exited",
3728 [SERVICE_RELOAD] = "reload",
3729 [SERVICE_STOP] = "stop",
3730 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3731 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3732 [SERVICE_STOP_POST] = "stop-post",
3733 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3734 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3735 [SERVICE_FAILED] = "failed",
3736 [SERVICE_AUTO_RESTART] = "auto-restart",
3739 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3741 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3742 [SERVICE_RESTART_NO] = "no",
3743 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3744 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3745 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3746 [SERVICE_RESTART_ALWAYS] = "always"
3749 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3751 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3752 [SERVICE_SIMPLE] = "simple",
3753 [SERVICE_FORKING] = "forking",
3754 [SERVICE_ONESHOT] = "oneshot",
3755 [SERVICE_DBUS] = "dbus",
3756 [SERVICE_NOTIFY] = "notify",
3757 [SERVICE_IDLE] = "idle"
3760 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3762 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3763 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3764 [SERVICE_EXEC_START] = "ExecStart",
3765 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3766 [SERVICE_EXEC_RELOAD] = "ExecReload",
3767 [SERVICE_EXEC_STOP] = "ExecStop",
3768 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3771 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3773 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3774 [NOTIFY_NONE] = "none",
3775 [NOTIFY_MAIN] = "main",
3776 [NOTIFY_ALL] = "all"
3779 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3781 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3782 [SERVICE_SUCCESS] = "success",
3783 [SERVICE_FAILURE_RESOURCES] = "resources",
3784 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3785 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3786 [SERVICE_FAILURE_SIGNAL] = "signal",
3787 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3788 [SERVICE_FAILURE_WATCHDOG] = "watchdog"
3791 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3793 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3794 [SERVICE_START_LIMIT_NONE] = "none",
3795 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3796 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3797 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3799 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3801 const UnitVTable service_vtable = {
3802 .suffix = ".service",
3803 .object_size = sizeof(Service),
3809 .init = service_init,
3810 .done = service_done,
3811 .load = service_load,
3813 .coldplug = service_coldplug,
3815 .dump = service_dump,
3817 .start = service_start,
3818 .stop = service_stop,
3819 .reload = service_reload,
3821 .can_reload = service_can_reload,
3823 .kill = service_kill,
3825 .serialize = service_serialize,
3826 .deserialize_item = service_deserialize_item,
3828 .active_state = service_active_state,
3829 .sub_state_to_string = service_sub_state_to_string,
3831 .check_gc = service_check_gc,
3832 .check_snapshot = service_check_snapshot,
3834 .sigchld_event = service_sigchld_event,
3835 .timer_event = service_timer_event,
3836 .fd_event = service_fd_event,
3838 .reset_failed = service_reset_failed,
3840 .cgroup_notify_empty = service_cgroup_notify_event,
3841 .notify_message = service_notify_message,
3843 .bus_name_owner_change = service_bus_name_owner_change,
3844 .bus_query_pid_done = service_bus_query_pid_done,
3846 .bus_interface = "org.freedesktop.systemd1.Service",
3847 .bus_message_handler = bus_service_message_handler,
3848 .bus_invalidating_properties = bus_service_invalidating_properties,
3850 #ifdef HAVE_SYSV_COMPAT
3851 .enumerate = service_enumerate,
3853 .status_message_formats = {
3854 .starting_stopping = {
3855 [0] = "Starting %s...",
3856 [1] = "Stopping %s...",
3858 .finished_start_job = {
3859 [JOB_DONE] = "Started %s.",
3860 [JOB_FAILED] = "Failed to start %s.",
3861 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3862 [JOB_TIMEOUT] = "Timed out starting %s.",
3864 .finished_stop_job = {
3865 [JOB_DONE] = "Stopped %s.",
3866 [JOB_FAILED] = "Stopped (with error) %s.",
3867 [JOB_TIMEOUT] = "Timed out stopping %s.",