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 service_fix_output(s);
1254 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1257 if ((r = unit_add_default_cgroups(u)) < 0)
1260 #ifdef HAVE_SYSV_COMPAT
1261 if ((r = sysv_fix_order(s)) < 0)
1265 if ((r = fsck_fix_order(s)) < 0)
1269 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1272 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1273 s->notify_access = NOTIFY_MAIN;
1275 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1276 s->notify_access = NOTIFY_MAIN;
1278 if (s->type == SERVICE_DBUS || s->bus_name)
1279 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1282 if (UNIT(s)->default_dependencies)
1283 if ((r = service_add_default_dependencies(s)) < 0)
1287 return service_verify(s);
1290 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1292 ServiceExecCommand c;
1293 Service *s = SERVICE(u);
1294 const char *prefix2;
1299 p2 = strappend(prefix, "\t");
1300 prefix2 = p2 ? p2 : prefix;
1303 "%sService State: %s\n"
1305 "%sReload Result: %s\n"
1306 "%sPermissionsStartOnly: %s\n"
1307 "%sRootDirectoryStartOnly: %s\n"
1308 "%sRemainAfterExit: %s\n"
1309 "%sGuessMainPID: %s\n"
1312 "%sNotifyAccess: %s\n",
1313 prefix, service_state_to_string(s->state),
1314 prefix, service_result_to_string(s->result),
1315 prefix, service_result_to_string(s->reload_result),
1316 prefix, yes_no(s->permissions_start_only),
1317 prefix, yes_no(s->root_directory_start_only),
1318 prefix, yes_no(s->remain_after_exit),
1319 prefix, yes_no(s->guess_main_pid),
1320 prefix, service_type_to_string(s->type),
1321 prefix, service_restart_to_string(s->restart),
1322 prefix, notify_access_to_string(s->notify_access));
1324 if (s->control_pid > 0)
1326 "%sControl PID: %lu\n",
1327 prefix, (unsigned long) s->control_pid);
1329 if (s->main_pid > 0)
1332 "%sMain PID Known: %s\n"
1333 "%sMain PID Alien: %s\n",
1334 prefix, (unsigned long) s->main_pid,
1335 prefix, yes_no(s->main_pid_known),
1336 prefix, yes_no(s->main_pid_alien));
1341 prefix, s->pid_file);
1346 "%sBus Name Good: %s\n",
1347 prefix, s->bus_name,
1348 prefix, yes_no(s->bus_name_good));
1350 exec_context_dump(&s->exec_context, f, prefix);
1352 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1354 if (!s->exec_command[c])
1357 fprintf(f, "%s-> %s:\n",
1358 prefix, service_exec_command_to_string(c));
1360 exec_command_dump_list(s->exec_command[c], f, prefix2);
1363 #ifdef HAVE_SYSV_COMPAT
1366 "%sSysV Init Script has LSB Header: %s\n"
1367 "%sSysVEnabled: %s\n",
1368 prefix, yes_no(s->sysv_has_lsb),
1369 prefix, yes_no(s->sysv_enabled));
1371 if (s->sysv_start_priority >= 0)
1373 "%sSysVStartPriority: %i\n",
1374 prefix, s->sysv_start_priority);
1376 if (s->sysv_runlevels)
1377 fprintf(f, "%sSysVRunLevels: %s\n",
1378 prefix, s->sysv_runlevels);
1381 if (s->fsck_passno > 0)
1383 "%sFsckPassNo: %i\n",
1384 prefix, s->fsck_passno);
1387 fprintf(f, "%sStatus Text: %s\n",
1388 prefix, s->status_text);
1393 static int service_load_pid_file(Service *s, bool may_warn) {
1403 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1405 log_info("PID file %s not readable (yet?) after %s.",
1406 s->pid_file, service_state_to_string(s->state));
1410 r = parse_pid(k, &pid);
1416 if (kill(pid, 0) < 0 && errno != EPERM) {
1418 log_info("PID %lu read from file %s does not exist.",
1419 (unsigned long) pid, s->pid_file);
1423 if (s->main_pid_known) {
1424 if (pid == s->main_pid)
1427 log_debug("Main PID changing: %lu -> %lu",
1428 (unsigned long) s->main_pid, (unsigned long) pid);
1429 service_unwatch_main_pid(s);
1430 s->main_pid_known = false;
1432 log_debug("Main PID loaded: %lu", (unsigned long) pid);
1434 if ((r = service_set_main_pid(s, pid)) < 0)
1437 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1438 /* FIXME: we need to do something here */
1444 static int service_search_main_pid(Service *s) {
1450 /* If we know it anyway, don't ever fallback to unreliable
1452 if (s->main_pid_known)
1455 if (!s->guess_main_pid)
1458 assert(s->main_pid <= 0);
1460 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1463 log_debug("Main PID guessed: %lu", (unsigned long) pid);
1464 if ((r = service_set_main_pid(s, pid)) < 0)
1467 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1468 /* FIXME: we need to do something here */
1474 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1480 /* Notifies all our sockets when we die */
1482 if (s->socket_fd >= 0)
1485 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1486 if (u->type == UNIT_SOCKET)
1487 socket_notify_service_dead(SOCKET(u), failed_permanent);
1492 static void service_set_state(Service *s, ServiceState state) {
1493 ServiceState old_state;
1494 const UnitActiveState *table;
1497 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1499 old_state = s->state;
1502 service_unwatch_pid_file(s);
1504 if (state != SERVICE_START_PRE &&
1505 state != SERVICE_START &&
1506 state != SERVICE_START_POST &&
1507 state != SERVICE_RELOAD &&
1508 state != SERVICE_STOP &&
1509 state != SERVICE_STOP_SIGTERM &&
1510 state != SERVICE_STOP_SIGKILL &&
1511 state != SERVICE_STOP_POST &&
1512 state != SERVICE_FINAL_SIGTERM &&
1513 state != SERVICE_FINAL_SIGKILL &&
1514 state != SERVICE_AUTO_RESTART)
1515 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1517 if (state != SERVICE_START &&
1518 state != SERVICE_START_POST &&
1519 state != SERVICE_RUNNING &&
1520 state != SERVICE_RELOAD &&
1521 state != SERVICE_STOP &&
1522 state != SERVICE_STOP_SIGTERM &&
1523 state != SERVICE_STOP_SIGKILL) {
1524 service_unwatch_main_pid(s);
1525 s->main_command = NULL;
1528 if (state != SERVICE_START_PRE &&
1529 state != SERVICE_START &&
1530 state != SERVICE_START_POST &&
1531 state != SERVICE_RELOAD &&
1532 state != SERVICE_STOP &&
1533 state != SERVICE_STOP_SIGTERM &&
1534 state != SERVICE_STOP_SIGKILL &&
1535 state != SERVICE_STOP_POST &&
1536 state != SERVICE_FINAL_SIGTERM &&
1537 state != SERVICE_FINAL_SIGKILL) {
1538 service_unwatch_control_pid(s);
1539 s->control_command = NULL;
1540 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1543 if (state == SERVICE_DEAD ||
1544 state == SERVICE_STOP ||
1545 state == SERVICE_STOP_SIGTERM ||
1546 state == SERVICE_STOP_SIGKILL ||
1547 state == SERVICE_STOP_POST ||
1548 state == SERVICE_FINAL_SIGTERM ||
1549 state == SERVICE_FINAL_SIGKILL ||
1550 state == SERVICE_FAILED ||
1551 state == SERVICE_AUTO_RESTART)
1552 service_notify_sockets_dead(s, false);
1554 if (state != SERVICE_START_PRE &&
1555 state != SERVICE_START &&
1556 state != SERVICE_START_POST &&
1557 state != SERVICE_RUNNING &&
1558 state != SERVICE_RELOAD &&
1559 state != SERVICE_STOP &&
1560 state != SERVICE_STOP_SIGTERM &&
1561 state != SERVICE_STOP_SIGKILL &&
1562 state != SERVICE_STOP_POST &&
1563 state != SERVICE_FINAL_SIGTERM &&
1564 state != SERVICE_FINAL_SIGKILL &&
1565 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1566 service_close_socket_fd(s);
1567 service_connection_unref(s);
1570 if (state == SERVICE_STOP)
1571 service_stop_watchdog(s);
1573 /* For the inactive states unit_notify() will trim the cgroup,
1574 * but for exit we have to do that ourselves... */
1575 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1576 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1578 if (old_state != state)
1579 log_debug("%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
1581 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1582 s->reload_result = SERVICE_SUCCESS;
1585 static int service_coldplug(Unit *u) {
1586 Service *s = SERVICE(u);
1590 assert(s->state == SERVICE_DEAD);
1592 if (s->deserialized_state != s->state) {
1594 if (s->deserialized_state == SERVICE_START_PRE ||
1595 s->deserialized_state == SERVICE_START ||
1596 s->deserialized_state == SERVICE_START_POST ||
1597 s->deserialized_state == SERVICE_RELOAD ||
1598 s->deserialized_state == SERVICE_STOP ||
1599 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1600 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1601 s->deserialized_state == SERVICE_STOP_POST ||
1602 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1603 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1604 s->deserialized_state == SERVICE_AUTO_RESTART) {
1606 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1609 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1611 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1616 if ((s->deserialized_state == SERVICE_START &&
1617 (s->type == SERVICE_FORKING ||
1618 s->type == SERVICE_DBUS ||
1619 s->type == SERVICE_ONESHOT ||
1620 s->type == SERVICE_NOTIFY)) ||
1621 s->deserialized_state == SERVICE_START_POST ||
1622 s->deserialized_state == SERVICE_RUNNING ||
1623 s->deserialized_state == SERVICE_RELOAD ||
1624 s->deserialized_state == SERVICE_STOP ||
1625 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1626 s->deserialized_state == SERVICE_STOP_SIGKILL)
1627 if (s->main_pid > 0)
1628 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1631 if (s->deserialized_state == SERVICE_START_PRE ||
1632 s->deserialized_state == SERVICE_START ||
1633 s->deserialized_state == SERVICE_START_POST ||
1634 s->deserialized_state == SERVICE_RELOAD ||
1635 s->deserialized_state == SERVICE_STOP ||
1636 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1637 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1638 s->deserialized_state == SERVICE_STOP_POST ||
1639 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1640 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1641 if (s->control_pid > 0)
1642 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1645 if (s->deserialized_state == SERVICE_START_POST ||
1646 s->deserialized_state == SERVICE_RUNNING)
1647 service_handle_watchdog(s);
1649 service_set_state(s, s->deserialized_state);
1654 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1658 unsigned rn_fds = 0;
1665 if (s->socket_fd >= 0)
1668 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1673 if (u->type != UNIT_SOCKET)
1678 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1690 if (!(t = new(int, rn_fds+cn_fds))) {
1696 memcpy(t, rfds, rn_fds * sizeof(int));
1697 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1702 rn_fds = rn_fds+cn_fds;
1717 static int service_spawn(
1722 bool apply_permissions,
1724 bool apply_tty_stdin,
1725 bool set_notify_socket,
1731 int *fds = NULL, *fdsbuf = NULL;
1732 unsigned n_fds = 0, n_env = 0;
1733 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1740 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1741 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1742 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1744 if (s->socket_fd >= 0) {
1745 fds = &s->socket_fd;
1748 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1755 if (timeout && s->timeout_usec) {
1756 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1759 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1761 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1766 if (!(our_env = new0(char*, 4))) {
1771 if (set_notify_socket)
1772 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1777 if (s->main_pid > 0)
1778 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1783 if (s->watchdog_usec > 0)
1784 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1789 if (!(final_env = strv_env_merge(2,
1790 UNIT(s)->manager->environment,
1805 UNIT(s)->manager->confirm_spawn,
1806 UNIT(s)->cgroup_bondings,
1807 UNIT(s)->cgroup_attributes,
1808 is_control ? "control" : NULL,
1809 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1816 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1817 /* FIXME: we need to do something here */
1823 strv_free(final_env);
1833 strv_free(final_env);
1836 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1841 static int main_pid_good(Service *s) {
1844 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1847 /* If we know the pid file, then lets just check if it is
1849 if (s->main_pid_known) {
1851 /* If it's an alien child let's check if it is still
1853 if (s->main_pid_alien)
1854 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1856 /* .. otherwise assume we'll get a SIGCHLD for it,
1857 * which we really should wait for to collect exit
1858 * status and code */
1859 return s->main_pid > 0;
1862 /* We don't know the pid */
1866 static int control_pid_good(Service *s) {
1869 return s->control_pid > 0;
1872 static int cgroup_good(Service *s) {
1877 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1883 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1887 if (f != SERVICE_SUCCESS)
1890 if (allow_restart &&
1891 !s->forbid_restart &&
1892 (s->restart == SERVICE_RESTART_ALWAYS ||
1893 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1894 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1895 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1896 s->result == SERVICE_FAILURE_CORE_DUMP)))) {
1898 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
1902 service_set_state(s, SERVICE_AUTO_RESTART);
1904 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1906 s->forbid_restart = false;
1911 log_warning("%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1912 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1915 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1917 static void service_enter_stop_post(Service *s, ServiceResult f) {
1921 if (f != SERVICE_SUCCESS)
1924 service_unwatch_control_pid(s);
1926 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1927 s->control_command_id = SERVICE_EXEC_STOP_POST;
1929 r = service_spawn(s,
1933 !s->permissions_start_only,
1934 !s->root_directory_start_only,
1943 service_set_state(s, SERVICE_STOP_POST);
1945 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1950 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
1951 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1954 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1956 Set *pid_set = NULL;
1957 bool wait_for_exit = false;
1961 if (f != SERVICE_SUCCESS)
1964 if (s->exec_context.kill_mode != KILL_NONE) {
1965 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1967 if (s->main_pid > 0) {
1968 if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
1969 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1971 wait_for_exit = !s->main_pid_alien;
1974 if (s->control_pid > 0) {
1975 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1976 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1978 wait_for_exit = true;
1981 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1983 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1988 /* Exclude the main/control pids from being killed via the cgroup */
1989 if (s->main_pid > 0)
1990 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1993 if (s->control_pid > 0)
1994 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1997 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
1999 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
2000 log_warning("Failed to kill control group: %s", strerror(-r));
2002 wait_for_exit = true;
2009 if (wait_for_exit) {
2010 if (s->timeout_usec > 0)
2011 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
2014 service_set_state(s, state);
2015 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2016 service_enter_stop_post(s, SERVICE_SUCCESS);
2018 service_enter_dead(s, SERVICE_SUCCESS, true);
2023 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2025 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2026 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2028 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2034 static void service_enter_stop(Service *s, ServiceResult f) {
2039 if (f != SERVICE_SUCCESS)
2042 service_unwatch_control_pid(s);
2044 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2045 s->control_command_id = SERVICE_EXEC_STOP;
2047 r = service_spawn(s,
2051 !s->permissions_start_only,
2052 !s->root_directory_start_only,
2060 service_set_state(s, SERVICE_STOP);
2062 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2067 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2068 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2071 static void service_enter_running(Service *s, ServiceResult f) {
2072 int main_pid_ok, cgroup_ok;
2075 if (f != SERVICE_SUCCESS)
2078 main_pid_ok = main_pid_good(s);
2079 cgroup_ok = cgroup_good(s);
2081 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2082 (s->bus_name_good || s->type != SERVICE_DBUS))
2083 service_set_state(s, SERVICE_RUNNING);
2084 else if (s->remain_after_exit)
2085 service_set_state(s, SERVICE_EXITED);
2087 service_enter_stop(s, SERVICE_SUCCESS);
2090 static void service_enter_start_post(Service *s) {
2094 service_unwatch_control_pid(s);
2096 if (s->watchdog_usec > 0)
2097 service_reset_watchdog(s);
2099 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2100 s->control_command_id = SERVICE_EXEC_START_POST;
2102 r = service_spawn(s,
2106 !s->permissions_start_only,
2107 !s->root_directory_start_only,
2115 service_set_state(s, SERVICE_START_POST);
2117 service_enter_running(s, SERVICE_SUCCESS);
2122 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2123 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2126 static void service_enter_start(Service *s) {
2133 assert(s->exec_command[SERVICE_EXEC_START]);
2134 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2136 if (s->type == SERVICE_FORKING)
2137 service_unwatch_control_pid(s);
2139 service_unwatch_main_pid(s);
2141 /* We want to ensure that nobody leaks processes from
2142 * START_PRE here, so let's go on a killing spree, People
2143 * should not spawn long running processes from START_PRE. */
2144 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2146 if (s->type == SERVICE_FORKING) {
2147 s->control_command_id = SERVICE_EXEC_START;
2148 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2150 s->main_command = NULL;
2152 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2153 s->control_command = NULL;
2155 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2158 r = service_spawn(s,
2160 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
2165 s->notify_access != NOTIFY_NONE,
2171 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2172 /* For simple services we immediately start
2173 * the START_POST binaries. */
2175 service_set_main_pid(s, pid);
2176 service_enter_start_post(s);
2178 } else if (s->type == SERVICE_FORKING) {
2180 /* For forking services we wait until the start
2181 * process exited. */
2183 s->control_pid = pid;
2184 service_set_state(s, SERVICE_START);
2186 } else if (s->type == SERVICE_ONESHOT ||
2187 s->type == SERVICE_DBUS ||
2188 s->type == SERVICE_NOTIFY) {
2190 /* For oneshot services we wait until the start
2191 * process exited, too, but it is our main process. */
2193 /* For D-Bus services we know the main pid right away,
2194 * but wait for the bus name to appear on the
2195 * bus. Notify services are similar. */
2197 service_set_main_pid(s, pid);
2198 service_set_state(s, SERVICE_START);
2200 assert_not_reached("Unknown service type");
2205 log_warning("%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2206 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2209 static void service_enter_start_pre(Service *s) {
2214 service_unwatch_control_pid(s);
2216 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2218 /* Before we start anything, let's clear up what might
2219 * be left from previous runs. */
2220 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2222 s->control_command_id = SERVICE_EXEC_START_PRE;
2224 r = service_spawn(s,
2228 !s->permissions_start_only,
2229 !s->root_directory_start_only,
2237 service_set_state(s, SERVICE_START_PRE);
2239 service_enter_start(s);
2244 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2245 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2248 static void service_enter_restart(Service *s) {
2253 dbus_error_init(&error);
2256 log_info("Job pending for unit, delaying automatic restart.");
2258 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
2264 /* Any units that are bound to this service must also be
2265 * restarted. We use JOB_RESTART (instead of the more obvious
2266 * JOB_START) here so that those dependency jobs will be added
2268 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2272 log_debug("%s scheduled restart job.", UNIT(s)->id);
2276 log_warning("%s failed to schedule restart job: %s", UNIT(s)->id, bus_error(&error, -r));
2277 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2279 dbus_error_free(&error);
2282 static void service_enter_reload(Service *s) {
2287 service_unwatch_control_pid(s);
2289 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2290 s->control_command_id = SERVICE_EXEC_RELOAD;
2292 r = service_spawn(s,
2296 !s->permissions_start_only,
2297 !s->root_directory_start_only,
2305 service_set_state(s, SERVICE_RELOAD);
2307 service_enter_running(s, SERVICE_SUCCESS);
2312 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
2313 s->reload_result = SERVICE_FAILURE_RESOURCES;
2314 service_enter_running(s, SERVICE_SUCCESS);
2317 static void service_run_next_control(Service *s) {
2321 assert(s->control_command);
2322 assert(s->control_command->command_next);
2324 assert(s->control_command_id != SERVICE_EXEC_START);
2326 s->control_command = s->control_command->command_next;
2327 service_unwatch_control_pid(s);
2329 r = service_spawn(s,
2333 !s->permissions_start_only,
2334 !s->root_directory_start_only,
2335 s->control_command_id == SERVICE_EXEC_START_PRE ||
2336 s->control_command_id == SERVICE_EXEC_STOP_POST,
2346 log_warning("%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
2348 if (s->state == SERVICE_START_PRE)
2349 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2350 else if (s->state == SERVICE_STOP)
2351 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2352 else if (s->state == SERVICE_STOP_POST)
2353 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2354 else if (s->state == SERVICE_RELOAD) {
2355 s->reload_result = SERVICE_FAILURE_RESOURCES;
2356 service_enter_running(s, SERVICE_SUCCESS);
2358 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2361 static void service_run_next_main(Service *s) {
2366 assert(s->main_command);
2367 assert(s->main_command->command_next);
2368 assert(s->type == SERVICE_ONESHOT);
2370 s->main_command = s->main_command->command_next;
2371 service_unwatch_main_pid(s);
2373 r = service_spawn(s,
2380 s->notify_access != NOTIFY_NONE,
2386 service_set_main_pid(s, pid);
2391 log_warning("%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2392 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2395 static int service_start_limit_test(Service *s) {
2398 if (ratelimit_test(&s->start_limit))
2401 switch (s->start_limit_action) {
2403 case SERVICE_START_LIMIT_NONE:
2404 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s)->id);
2407 case SERVICE_START_LIMIT_REBOOT: {
2411 dbus_error_init(&error);
2413 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2415 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2417 log_error("Failed to reboot: %s.", bus_error(&error, r));
2418 dbus_error_free(&error);
2424 case SERVICE_START_LIMIT_REBOOT_FORCE:
2425 log_warning("%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2426 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2429 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2430 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2431 reboot(RB_AUTOBOOT);
2435 log_error("start limit action=%i", s->start_limit_action);
2436 assert_not_reached("Unknown StartLimitAction.");
2442 static int service_start(Unit *u) {
2443 Service *s = SERVICE(u);
2448 /* We cannot fulfill this request right now, try again later
2450 if (s->state == SERVICE_STOP ||
2451 s->state == SERVICE_STOP_SIGTERM ||
2452 s->state == SERVICE_STOP_SIGKILL ||
2453 s->state == SERVICE_STOP_POST ||
2454 s->state == SERVICE_FINAL_SIGTERM ||
2455 s->state == SERVICE_FINAL_SIGKILL)
2458 /* Already on it! */
2459 if (s->state == SERVICE_START_PRE ||
2460 s->state == SERVICE_START ||
2461 s->state == SERVICE_START_POST)
2464 /* A service that will be restarted must be stopped first to
2465 * trigger BindTo and/or OnFailure dependencies. If a user
2466 * does not want to wait for the holdoff time to elapse, the
2467 * service should be manually restarted, not started. */
2468 if (s->state == SERVICE_AUTO_RESTART) {
2469 log_warning("%s automatic restart is pending, must be stopped before issuing start request.", UNIT(s)->id);
2473 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2475 /* Make sure we don't enter a busy loop of some kind. */
2476 r = service_start_limit_test(s);
2478 service_notify_sockets_dead(s, true);
2482 s->result = SERVICE_SUCCESS;
2483 s->reload_result = SERVICE_SUCCESS;
2484 s->main_pid_known = false;
2485 s->main_pid_alien = false;
2486 s->forbid_restart = false;
2488 service_enter_start_pre(s);
2492 static int service_stop(Unit *u) {
2493 Service *s = SERVICE(u);
2497 /* Don't create restart jobs from here. */
2498 s->forbid_restart = true;
2501 if (s->state == SERVICE_STOP ||
2502 s->state == SERVICE_STOP_SIGTERM ||
2503 s->state == SERVICE_STOP_SIGKILL ||
2504 s->state == SERVICE_STOP_POST ||
2505 s->state == SERVICE_FINAL_SIGTERM ||
2506 s->state == SERVICE_FINAL_SIGKILL)
2509 /* A restart will be scheduled or is in progress. */
2510 if (s->state == SERVICE_AUTO_RESTART) {
2511 service_enter_dead(s, SERVICE_SUCCESS, false);
2515 /* If there's already something running we go directly into
2517 if (s->state == SERVICE_START_PRE ||
2518 s->state == SERVICE_START ||
2519 s->state == SERVICE_START_POST ||
2520 s->state == SERVICE_RELOAD) {
2521 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2525 assert(s->state == SERVICE_RUNNING ||
2526 s->state == SERVICE_EXITED);
2528 service_enter_stop(s, SERVICE_SUCCESS);
2532 static int service_reload(Unit *u) {
2533 Service *s = SERVICE(u);
2537 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2539 service_enter_reload(s);
2543 static bool service_can_reload(Unit *u) {
2544 Service *s = SERVICE(u);
2548 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2551 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2552 Service *s = SERVICE(u);
2558 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2559 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2560 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2562 if (s->control_pid > 0)
2563 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2565 if (s->main_pid_known && s->main_pid > 0)
2566 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2568 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2571 unit_serialize_item(u, f, "status-text", s->status_text);
2573 /* FIXME: There's a minor uncleanliness here: if there are
2574 * multiple commands attached here, we will start from the
2575 * first one again */
2576 if (s->control_command_id >= 0)
2577 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2579 if (s->socket_fd >= 0) {
2582 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2585 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2588 if (s->main_exec_status.pid > 0) {
2589 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2590 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2591 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2593 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2594 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2595 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2598 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2599 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2604 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2605 Service *s = SERVICE(u);
2612 if (streq(key, "state")) {
2615 if ((state = service_state_from_string(value)) < 0)
2616 log_debug("Failed to parse state value %s", value);
2618 s->deserialized_state = state;
2619 } else if (streq(key, "result")) {
2622 f = service_result_from_string(value);
2624 log_debug("Failed to parse result value %s", value);
2625 else if (f != SERVICE_SUCCESS)
2628 } else if (streq(key, "reload-result")) {
2631 f = service_result_from_string(value);
2633 log_debug("Failed to parse reload result value %s", value);
2634 else if (f != SERVICE_SUCCESS)
2635 s->reload_result = f;
2637 } else if (streq(key, "control-pid")) {
2640 if (parse_pid(value, &pid) < 0)
2641 log_debug("Failed to parse control-pid value %s", value);
2643 s->control_pid = pid;
2644 } else if (streq(key, "main-pid")) {
2647 if (parse_pid(value, &pid) < 0)
2648 log_debug("Failed to parse main-pid value %s", value);
2650 service_set_main_pid(s, (pid_t) pid);
2651 } else if (streq(key, "main-pid-known")) {
2654 if ((b = parse_boolean(value)) < 0)
2655 log_debug("Failed to parse main-pid-known value %s", value);
2657 s->main_pid_known = b;
2658 } else if (streq(key, "status-text")) {
2661 if ((t = strdup(value))) {
2662 free(s->status_text);
2666 } else if (streq(key, "control-command")) {
2667 ServiceExecCommand id;
2669 if ((id = service_exec_command_from_string(value)) < 0)
2670 log_debug("Failed to parse exec-command value %s", value);
2672 s->control_command_id = id;
2673 s->control_command = s->exec_command[id];
2675 } else if (streq(key, "socket-fd")) {
2678 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2679 log_debug("Failed to parse socket-fd value %s", value);
2682 if (s->socket_fd >= 0)
2683 close_nointr_nofail(s->socket_fd);
2684 s->socket_fd = fdset_remove(fds, fd);
2686 } else if (streq(key, "main-exec-status-pid")) {
2689 if (parse_pid(value, &pid) < 0)
2690 log_debug("Failed to parse main-exec-status-pid value %s", value);
2692 s->main_exec_status.pid = pid;
2693 } else if (streq(key, "main-exec-status-code")) {
2696 if (safe_atoi(value, &i) < 0)
2697 log_debug("Failed to parse main-exec-status-code value %s", value);
2699 s->main_exec_status.code = i;
2700 } else if (streq(key, "main-exec-status-status")) {
2703 if (safe_atoi(value, &i) < 0)
2704 log_debug("Failed to parse main-exec-status-status value %s", value);
2706 s->main_exec_status.status = i;
2707 } else if (streq(key, "main-exec-status-start"))
2708 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2709 else if (streq(key, "main-exec-status-exit"))
2710 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2711 else if (streq(key, "watchdog-timestamp"))
2712 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2714 log_debug("Unknown serialization key '%s'", key);
2719 static UnitActiveState service_active_state(Unit *u) {
2720 const UnitActiveState *table;
2724 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2726 return table[SERVICE(u)->state];
2729 static const char *service_sub_state_to_string(Unit *u) {
2732 return service_state_to_string(SERVICE(u)->state);
2735 static bool service_check_gc(Unit *u) {
2736 Service *s = SERVICE(u);
2740 /* Never clean up services that still have a process around,
2741 * even if the service is formally dead. */
2742 if (cgroup_good(s) > 0 ||
2743 main_pid_good(s) > 0 ||
2744 control_pid_good(s) > 0)
2747 #ifdef HAVE_SYSV_COMPAT
2755 static bool service_check_snapshot(Unit *u) {
2756 Service *s = SERVICE(u);
2760 return !s->got_socket_fd;
2763 static int service_retry_pid_file(Service *s) {
2766 assert(s->pid_file);
2767 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2769 r = service_load_pid_file(s, false);
2773 service_unwatch_pid_file(s);
2775 service_enter_running(s, SERVICE_SUCCESS);
2779 static int service_watch_pid_file(Service *s) {
2782 log_debug("Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
2783 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2787 /* the pidfile might have appeared just before we set the watch */
2788 service_retry_pid_file(s);
2792 log_error("Failed to set a watch for %s's PID file %s: %s",
2793 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2794 service_unwatch_pid_file(s);
2798 static int service_demand_pid_file(Service *s) {
2801 assert(s->pid_file);
2802 assert(!s->pid_file_pathspec);
2804 ps = new0(PathSpec, 1);
2808 ps->path = strdup(s->pid_file);
2814 path_kill_slashes(ps->path);
2816 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2817 * keep their PID file open all the time. */
2818 ps->type = PATH_MODIFIED;
2819 ps->inotify_fd = -1;
2821 s->pid_file_pathspec = ps;
2823 return service_watch_pid_file(s);
2826 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2827 Service *s = SERVICE(u);
2831 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2832 assert(s->pid_file_pathspec);
2833 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2835 log_debug("inotify event for %s", u->id);
2837 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2840 if (service_retry_pid_file(s) == 0)
2843 if (service_watch_pid_file(s) < 0)
2848 service_unwatch_pid_file(s);
2849 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2852 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2853 Service *s = SERVICE(u);
2859 if (UNIT(s)->fragment_path ? is_clean_exit(code, status) : is_clean_exit_lsb(code, status))
2860 f = SERVICE_SUCCESS;
2861 else if (code == CLD_EXITED)
2862 f = SERVICE_FAILURE_EXIT_CODE;
2863 else if (code == CLD_KILLED)
2864 f = SERVICE_FAILURE_SIGNAL;
2865 else if (code == CLD_DUMPED)
2866 f = SERVICE_FAILURE_CORE_DUMP;
2868 assert_not_reached("Unknown code");
2870 if (s->main_pid == pid) {
2871 /* Forking services may occasionally move to a new PID.
2872 * As long as they update the PID file before exiting the old
2873 * PID, they're fine. */
2874 if (service_load_pid_file(s, false) == 0)
2878 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2880 /* If this is not a forking service than the main
2881 * process got started and hence we copy the exit
2882 * status so that it is recorded both as main and as
2883 * control process exit status */
2884 if (s->main_command) {
2885 s->main_command->exec_status = s->main_exec_status;
2887 if (s->main_command->ignore)
2888 f = SERVICE_SUCCESS;
2891 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2892 "%s: main process exited, code=%s, status=%i", u->id, sigchld_code_to_string(code), status);
2894 if (f != SERVICE_SUCCESS)
2897 if (s->main_command &&
2898 s->main_command->command_next &&
2899 f == SERVICE_SUCCESS) {
2901 /* There is another command to *
2902 * execute, so let's do that. */
2904 log_debug("%s running next main command for state %s", u->id, service_state_to_string(s->state));
2905 service_run_next_main(s);
2909 /* The service exited, so the service is officially
2911 s->main_command = NULL;
2915 case SERVICE_START_POST:
2916 case SERVICE_RELOAD:
2918 /* Need to wait until the operation is
2923 if (s->type == SERVICE_ONESHOT) {
2924 /* This was our main goal, so let's go on */
2925 if (f == SERVICE_SUCCESS)
2926 service_enter_start_post(s);
2928 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2931 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2936 case SERVICE_RUNNING:
2937 service_enter_running(s, f);
2940 case SERVICE_STOP_SIGTERM:
2941 case SERVICE_STOP_SIGKILL:
2943 if (!control_pid_good(s))
2944 service_enter_stop_post(s, f);
2946 /* If there is still a control process, wait for that first */
2950 assert_not_reached("Uh, main process died at wrong time.");
2954 } else if (s->control_pid == pid) {
2958 if (s->control_command) {
2959 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2961 if (s->control_command->ignore)
2962 f = SERVICE_SUCCESS;
2965 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2966 "%s: control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
2968 if (f != SERVICE_SUCCESS)
2971 /* Immediately get rid of the cgroup, so that the
2972 * kernel doesn't delay the cgroup empty messages for
2973 * the service cgroup any longer than necessary */
2974 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2976 if (s->control_command &&
2977 s->control_command->command_next &&
2978 f == SERVICE_SUCCESS) {
2980 /* There is another command to *
2981 * execute, so let's do that. */
2983 log_debug("%s running next control command for state %s", u->id, service_state_to_string(s->state));
2984 service_run_next_control(s);
2987 /* No further commands for this step, so let's
2988 * figure out what to do next */
2990 s->control_command = NULL;
2991 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2993 log_debug("%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
2997 case SERVICE_START_PRE:
2998 if (f == SERVICE_SUCCESS)
2999 service_enter_start(s);
3001 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3005 assert(s->type == SERVICE_FORKING);
3007 if (f != SERVICE_SUCCESS) {
3008 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3013 bool has_start_post;
3016 /* Let's try to load the pid file here if we can.
3017 * The PID file might actually be created by a START_POST
3018 * script. In that case don't worry if the loading fails. */
3020 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3021 r = service_load_pid_file(s, !has_start_post);
3022 if (!has_start_post && r < 0) {
3023 r = service_demand_pid_file(s);
3024 if (r < 0 || !cgroup_good(s))
3025 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3029 service_search_main_pid(s);
3031 service_enter_start_post(s);
3034 case SERVICE_START_POST:
3035 if (f != SERVICE_SUCCESS) {
3036 service_enter_stop(s, f);
3043 r = service_load_pid_file(s, true);
3045 r = service_demand_pid_file(s);
3046 if (r < 0 || !cgroup_good(s))
3047 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3051 service_search_main_pid(s);
3053 service_enter_running(s, SERVICE_SUCCESS);
3056 case SERVICE_RELOAD:
3057 if (f == SERVICE_SUCCESS) {
3058 service_load_pid_file(s, true);
3059 service_search_main_pid(s);
3062 s->reload_result = f;
3063 service_enter_running(s, SERVICE_SUCCESS);
3067 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3070 case SERVICE_STOP_SIGTERM:
3071 case SERVICE_STOP_SIGKILL:
3072 if (main_pid_good(s) <= 0)
3073 service_enter_stop_post(s, f);
3075 /* If there is still a service
3076 * process around, wait until
3077 * that one quit, too */
3080 case SERVICE_STOP_POST:
3081 case SERVICE_FINAL_SIGTERM:
3082 case SERVICE_FINAL_SIGKILL:
3083 service_enter_dead(s, f, true);
3087 assert_not_reached("Uh, control process died at wrong time.");
3092 /* Notify clients about changed exit status */
3093 unit_add_to_dbus_queue(u);
3096 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3097 Service *s = SERVICE(u);
3100 assert(elapsed == 1);
3102 if (w == &s->watchdog_watch) {
3103 service_handle_watchdog(s);
3107 assert(w == &s->timer_watch);
3111 case SERVICE_START_PRE:
3113 log_warning("%s operation timed out. Terminating.", u->id);
3114 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3117 case SERVICE_START_POST:
3118 log_warning("%s operation timed out. Stopping.", u->id);
3119 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3122 case SERVICE_RELOAD:
3123 log_warning("%s operation timed out. Stopping.", u->id);
3124 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3125 service_enter_running(s, SERVICE_SUCCESS);
3129 log_warning("%s stopping timed out. Terminating.", u->id);
3130 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3133 case SERVICE_STOP_SIGTERM:
3134 if (s->exec_context.send_sigkill) {
3135 log_warning("%s stopping timed out. Killing.", u->id);
3136 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3138 log_warning("%s stopping timed out. Skipping SIGKILL.", u->id);
3139 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3144 case SERVICE_STOP_SIGKILL:
3145 /* Uh, we sent a SIGKILL and it is still not gone?
3146 * Must be something we cannot kill, so let's just be
3147 * weirded out and continue */
3149 log_warning("%s still around after SIGKILL. Ignoring.", u->id);
3150 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3153 case SERVICE_STOP_POST:
3154 log_warning("%s stopping timed out (2). Terminating.", u->id);
3155 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3158 case SERVICE_FINAL_SIGTERM:
3159 if (s->exec_context.send_sigkill) {
3160 log_warning("%s stopping timed out (2). Killing.", u->id);
3161 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3163 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->id);
3164 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3169 case SERVICE_FINAL_SIGKILL:
3170 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->id);
3171 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3174 case SERVICE_AUTO_RESTART:
3175 log_info("%s holdoff time over, scheduling restart.", u->id);
3176 service_enter_restart(s);
3180 assert_not_reached("Timeout at wrong time.");
3184 static void service_cgroup_notify_event(Unit *u) {
3185 Service *s = SERVICE(u);
3189 log_debug("%s: cgroup is empty", u->id);
3193 /* Waiting for SIGCHLD is usually more interesting,
3194 * because it includes return codes/signals. Which is
3195 * why we ignore the cgroup events for most cases,
3196 * except when we don't know pid which to expect the
3200 case SERVICE_START_POST:
3201 /* If we were hoping for the daemon to write its PID file,
3202 * we can give up now. */
3203 if (s->pid_file_pathspec) {
3204 log_warning("%s never wrote its PID file. Failing.", UNIT(s)->id);
3205 service_unwatch_pid_file(s);
3206 if (s->state == SERVICE_START)
3207 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3209 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3213 case SERVICE_RUNNING:
3214 /* service_enter_running() will figure out what to do */
3215 service_enter_running(s, SERVICE_SUCCESS);
3218 case SERVICE_STOP_SIGTERM:
3219 case SERVICE_STOP_SIGKILL:
3221 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3222 service_enter_stop_post(s, SERVICE_SUCCESS);
3226 case SERVICE_FINAL_SIGTERM:
3227 case SERVICE_FINAL_SIGKILL:
3228 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3229 service_enter_dead(s, SERVICE_SUCCESS, SERVICE_SUCCESS);
3238 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3239 Service *s = SERVICE(u);
3244 if (s->notify_access == NOTIFY_NONE) {
3245 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3246 u->id, (unsigned long) pid);
3250 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3251 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3252 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3256 log_debug("%s: Got message", u->id);
3258 /* Interpret MAINPID= */
3259 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3260 (s->state == SERVICE_START ||
3261 s->state == SERVICE_START_POST ||
3262 s->state == SERVICE_RUNNING ||
3263 s->state == SERVICE_RELOAD)) {
3265 if (parse_pid(e + 8, &pid) < 0)
3266 log_warning("Failed to parse notification message %s", e);
3268 log_debug("%s: got %s", u->id, e);
3269 service_set_main_pid(s, pid);
3273 /* Interpret READY= */
3274 if (s->type == SERVICE_NOTIFY &&
3275 s->state == SERVICE_START &&
3276 strv_find(tags, "READY=1")) {
3277 log_debug("%s: got READY=1", u->id);
3279 service_enter_start_post(s);
3282 /* Interpret STATUS= */
3283 e = strv_find_prefix(tags, "STATUS=");
3289 if (!utf8_is_valid(e+7)) {
3290 log_warning("Status message in notification is not UTF-8 clean.");
3296 log_error("Failed to allocate string.");
3300 log_debug("%s: got %s", u->id, e);
3302 free(s->status_text);
3305 free(s->status_text);
3306 s->status_text = NULL;
3310 if (strv_find(tags, "WATCHDOG=1")) {
3311 log_debug("%s: got WATCHDOG=1", u->id);
3312 service_reset_watchdog(s);
3315 /* Notify clients about changed status or main pid */
3316 unit_add_to_dbus_queue(u);
3319 #ifdef HAVE_SYSV_COMPAT
3322 static void sysv_facility_in_insserv_conf(Manager *mgr) {
3326 if (!(f = fopen("/etc/insserv.conf", "re"))) {
3327 r = errno == ENOENT ? 0 : -errno;
3332 char l[LINE_MAX], *t;
3333 char **parsed = NULL;
3335 if (!fgets(l, sizeof(l), f)) {
3340 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r));
3345 if (*t != '$' && *t != '<')
3348 parsed = strv_split(t,WHITESPACE);
3349 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3350 if (parsed && !startswith_no_case (parsed[0], "<interactive>")) {
3353 if (sysv_translate_facility(parsed[0], NULL, &facility) < 0)
3355 if ((u = manager_get_unit(mgr, facility)) && (u->type == UNIT_TARGET)) {
3357 char *dep = NULL, *name, **j;
3359 STRV_FOREACH (j, parsed+1) {
3368 if (sysv_translate_facility(name, NULL, &dep) < 0)
3371 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, e, dep, NULL, true);
3386 static int service_enumerate(Manager *m) {
3390 char *path = NULL, *fpath = NULL, *name = NULL;
3391 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3398 if (m->running_as != MANAGER_SYSTEM)
3401 zero(runlevel_services);
3403 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3404 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3408 path = join(*p, "/", rcnd_table[i].path, NULL);
3417 if (!(d = opendir(path))) {
3418 if (errno != ENOENT)
3419 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3424 while ((de = readdir(d))) {
3427 if (ignore_file(de->d_name))
3430 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3433 if (strlen(de->d_name) < 4)
3436 a = undecchar(de->d_name[1]);
3437 b = undecchar(de->d_name[2]);
3443 fpath = join(path, "/", de->d_name, NULL);
3449 if (access(fpath, X_OK) < 0) {
3451 if (errno != ENOENT)
3452 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3458 if (!(name = sysv_translate_name(de->d_name + 3))) {
3463 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
3464 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3468 if (de->d_name[0] == 'S') {
3470 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
3471 SERVICE(service)->sysv_start_priority_from_rcnd =
3472 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3474 SERVICE(service)->sysv_enabled = true;
3477 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3480 if ((r = set_put(runlevel_services[i], service)) < 0)
3483 } else if (de->d_name[0] == 'K' &&
3484 (rcnd_table[i].type == RUNLEVEL_DOWN ||
3485 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
3487 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3490 if ((r = set_put(shutdown_services, service)) < 0)
3496 /* Now we loaded all stubs and are aware of the lowest
3497 start-up priority for all services, not let's actually load
3498 the services, this will also tell us which services are
3499 actually native now */
3500 manager_dispatch_load_queue(m);
3502 /* If this is a native service, rely on native ways to pull in
3503 * a service, don't pull it in via sysv rcN.d links. */
3504 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3505 SET_FOREACH(service, runlevel_services[i], j) {
3506 service = unit_follow_merge(service);
3508 if (service->fragment_path)
3511 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3515 /* We honour K links only for halt/reboot. For the normal
3516 * runlevels we assume the stop jobs will be implicitly added
3517 * by the core logic. Also, we don't really distinguish here
3518 * between the runlevels 0 and 6 and just add them to the
3519 * special shutdown target. On SUSE the boot.d/ runlevel is
3520 * also used for shutdown, so we add links for that too to the
3521 * shutdown target.*/
3522 SET_FOREACH(service, shutdown_services, j) {
3523 service = unit_follow_merge(service);
3525 if (service->fragment_path)
3528 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3535 sysv_facility_in_insserv_conf (m);
3543 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3544 set_free(runlevel_services[i]);
3545 set_free(shutdown_services);
3554 static void service_bus_name_owner_change(
3557 const char *old_owner,
3558 const char *new_owner) {
3560 Service *s = SERVICE(u);
3565 assert(streq(s->bus_name, name));
3566 assert(old_owner || new_owner);
3568 if (old_owner && new_owner)
3569 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
3571 log_debug("%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
3573 log_debug("%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
3575 s->bus_name_good = !!new_owner;
3577 if (s->type == SERVICE_DBUS) {
3579 /* service_enter_running() will figure out what to
3581 if (s->state == SERVICE_RUNNING)
3582 service_enter_running(s, SERVICE_SUCCESS);
3583 else if (s->state == SERVICE_START && new_owner)
3584 service_enter_start_post(s);
3586 } else if (new_owner &&
3588 (s->state == SERVICE_START ||
3589 s->state == SERVICE_START_POST ||
3590 s->state == SERVICE_RUNNING ||
3591 s->state == SERVICE_RELOAD)) {
3593 /* Try to acquire PID from bus service */
3594 log_debug("Trying to acquire PID from D-Bus name...");
3596 bus_query_pid(u->manager, name);
3600 static void service_bus_query_pid_done(
3605 Service *s = SERVICE(u);
3610 log_debug("%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3612 if (s->main_pid <= 0 &&
3613 (s->state == SERVICE_START ||
3614 s->state == SERVICE_START_POST ||
3615 s->state == SERVICE_RUNNING ||
3616 s->state == SERVICE_RELOAD))
3617 service_set_main_pid(s, pid);
3620 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3625 /* This is called by the socket code when instantiating a new
3626 * service for a stream socket and the socket needs to be
3629 if (UNIT(s)->load_state != UNIT_LOADED)
3632 if (s->socket_fd >= 0)
3635 if (s->state != SERVICE_DEAD)
3639 s->got_socket_fd = true;
3641 unit_ref_set(&s->accept_socket, UNIT(sock));
3643 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3646 static void service_reset_failed(Unit *u) {
3647 Service *s = SERVICE(u);
3651 if (s->state == SERVICE_FAILED)
3652 service_set_state(s, SERVICE_DEAD);
3654 s->result = SERVICE_SUCCESS;
3655 s->reload_result = SERVICE_SUCCESS;
3658 static int service_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
3659 Service *s = SERVICE(u);
3661 Set *pid_set = NULL;
3665 if (s->main_pid <= 0 && who == KILL_MAIN) {
3666 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3670 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3671 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3675 if (who == KILL_CONTROL || who == KILL_ALL)
3676 if (s->control_pid > 0)
3677 if (kill(s->control_pid, signo) < 0)
3680 if (who == KILL_MAIN || who == KILL_ALL)
3681 if (s->main_pid > 0)
3682 if (kill(s->main_pid, signo) < 0)
3685 if (who == KILL_ALL && mode == KILL_CONTROL_GROUP) {
3688 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
3691 /* Exclude the control/main pid from being killed via the cgroup */
3692 if (s->control_pid > 0)
3693 if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
3698 if (s->main_pid > 0)
3699 if ((q = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0) {
3703 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3705 if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3716 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3717 [SERVICE_DEAD] = "dead",
3718 [SERVICE_START_PRE] = "start-pre",
3719 [SERVICE_START] = "start",
3720 [SERVICE_START_POST] = "start-post",
3721 [SERVICE_RUNNING] = "running",
3722 [SERVICE_EXITED] = "exited",
3723 [SERVICE_RELOAD] = "reload",
3724 [SERVICE_STOP] = "stop",
3725 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3726 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3727 [SERVICE_STOP_POST] = "stop-post",
3728 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3729 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3730 [SERVICE_FAILED] = "failed",
3731 [SERVICE_AUTO_RESTART] = "auto-restart",
3734 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3736 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3737 [SERVICE_RESTART_NO] = "no",
3738 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3739 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3740 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3741 [SERVICE_RESTART_ALWAYS] = "always"
3744 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3746 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3747 [SERVICE_SIMPLE] = "simple",
3748 [SERVICE_FORKING] = "forking",
3749 [SERVICE_ONESHOT] = "oneshot",
3750 [SERVICE_DBUS] = "dbus",
3751 [SERVICE_NOTIFY] = "notify",
3752 [SERVICE_IDLE] = "idle"
3755 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3757 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3758 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3759 [SERVICE_EXEC_START] = "ExecStart",
3760 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3761 [SERVICE_EXEC_RELOAD] = "ExecReload",
3762 [SERVICE_EXEC_STOP] = "ExecStop",
3763 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3766 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3768 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3769 [NOTIFY_NONE] = "none",
3770 [NOTIFY_MAIN] = "main",
3771 [NOTIFY_ALL] = "all"
3774 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3776 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3777 [SERVICE_SUCCESS] = "success",
3778 [SERVICE_FAILURE_RESOURCES] = "resources",
3779 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3780 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3781 [SERVICE_FAILURE_SIGNAL] = "signal",
3782 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3783 [SERVICE_FAILURE_WATCHDOG] = "watchdog"
3786 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3788 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3789 [SERVICE_START_LIMIT_NONE] = "none",
3790 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3791 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3792 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3794 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3796 const UnitVTable service_vtable = {
3797 .suffix = ".service",
3798 .object_size = sizeof(Service),
3804 .init = service_init,
3805 .done = service_done,
3806 .load = service_load,
3808 .coldplug = service_coldplug,
3810 .dump = service_dump,
3812 .start = service_start,
3813 .stop = service_stop,
3814 .reload = service_reload,
3816 .can_reload = service_can_reload,
3818 .kill = service_kill,
3820 .serialize = service_serialize,
3821 .deserialize_item = service_deserialize_item,
3823 .active_state = service_active_state,
3824 .sub_state_to_string = service_sub_state_to_string,
3826 .check_gc = service_check_gc,
3827 .check_snapshot = service_check_snapshot,
3829 .sigchld_event = service_sigchld_event,
3830 .timer_event = service_timer_event,
3831 .fd_event = service_fd_event,
3833 .reset_failed = service_reset_failed,
3835 .cgroup_notify_empty = service_cgroup_notify_event,
3836 .notify_message = service_notify_message,
3838 .bus_name_owner_change = service_bus_name_owner_change,
3839 .bus_query_pid_done = service_bus_query_pid_done,
3841 .bus_interface = "org.freedesktop.systemd1.Service",
3842 .bus_message_handler = bus_service_message_handler,
3843 .bus_invalidating_properties = bus_service_invalidating_properties,
3845 #ifdef HAVE_SYSV_COMPAT
3846 .enumerate = service_enumerate,
3848 .status_message_formats = {
3849 .starting_stopping = {
3850 [0] = "Starting %s...",
3851 [1] = "Stopping %s...",
3853 .finished_start_job = {
3854 [JOB_DONE] = "Started %s.",
3855 [JOB_FAILED] = "Failed to start %s.",
3856 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3857 [JOB_TIMEOUT] = "Timed out starting %s.",
3859 .finished_stop_job = {
3860 [JOB_DONE] = "Stopped %s.",
3861 [JOB_FAILED] = "Stopped (with error) %s.",
3862 [JOB_TIMEOUT] = "Timed out stopping %s.",