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 static void service_init(Unit *u) {
112 Service *s = SERVICE(u);
116 assert(u->load_state == UNIT_STUB);
118 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
119 s->restart_usec = DEFAULT_RESTART_USEC;
120 s->type = _SERVICE_TYPE_INVALID;
122 s->watchdog_watch.type = WATCH_INVALID;
124 s->timer_watch.type = WATCH_INVALID;
125 #ifdef HAVE_SYSV_COMPAT
126 s->sysv_start_priority = -1;
127 s->sysv_start_priority_from_rcnd = -1;
130 s->guess_main_pid = true;
132 exec_context_init(&s->exec_context);
133 for (i = 0; i < RLIMIT_NLIMITS; i++)
134 if (UNIT(s)->manager->rlimit[i])
135 s->exec_context.rlimit[i] = newdup(struct rlimit, UNIT(s)->manager->rlimit[i], 1);
137 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
139 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
142 static void service_unwatch_control_pid(Service *s) {
145 if (s->control_pid <= 0)
148 unit_unwatch_pid(UNIT(s), s->control_pid);
152 static void service_unwatch_main_pid(Service *s) {
155 if (s->main_pid <= 0)
158 unit_unwatch_pid(UNIT(s), s->main_pid);
162 static void service_unwatch_pid_file(Service *s) {
163 if (!s->pid_file_pathspec)
166 log_debug("Stopping watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
167 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
168 path_spec_done(s->pid_file_pathspec);
169 free(s->pid_file_pathspec);
170 s->pid_file_pathspec = NULL;
173 static int service_set_main_pid(Service *s, pid_t pid) {
185 s->main_pid_known = true;
187 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
188 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
189 UNIT(s)->id, (unsigned long) pid);
191 s->main_pid_alien = true;
193 s->main_pid_alien = false;
195 exec_status_start(&s->main_exec_status, pid);
200 static void service_close_socket_fd(Service *s) {
203 if (s->socket_fd < 0)
206 close_nointr_nofail(s->socket_fd);
210 static void service_connection_unref(Service *s) {
213 if (!UNIT_DEREF(s->accept_socket))
216 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
217 unit_ref_unset(&s->accept_socket);
220 static void service_stop_watchdog(Service *s) {
223 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
224 s->watchdog_timestamp.realtime = 0;
225 s->watchdog_timestamp.monotonic = 0;
228 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart);
230 static void service_handle_watchdog(Service *s) {
236 if (s->watchdog_usec == 0)
239 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
240 if (offset >= s->watchdog_usec) {
241 log_error("%s watchdog timeout!", UNIT(s)->id);
242 service_enter_dead(s, SERVICE_FAILURE_WATCHDOG, true);
246 r = unit_watch_timer(UNIT(s), s->watchdog_usec - offset, &s->watchdog_watch);
248 log_warning("%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
251 static void service_reset_watchdog(Service *s) {
254 dual_timestamp_get(&s->watchdog_timestamp);
255 service_handle_watchdog(s);
258 static void service_done(Unit *u) {
259 Service *s = SERVICE(u);
266 #ifdef HAVE_SYSV_COMPAT
267 free(s->sysv_runlevels);
268 s->sysv_runlevels = NULL;
271 free(s->status_text);
272 s->status_text = NULL;
274 exec_context_done(&s->exec_context);
275 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
276 s->control_command = NULL;
277 s->main_command = NULL;
279 /* This will leak a process, but at least no memory or any of
281 service_unwatch_main_pid(s);
282 service_unwatch_control_pid(s);
283 service_unwatch_pid_file(s);
286 unit_unwatch_bus_name(u, s->bus_name);
291 service_close_socket_fd(s);
292 service_connection_unref(s);
294 unit_ref_unset(&s->accept_socket);
296 service_stop_watchdog(s);
298 unit_unwatch_timer(u, &s->timer_watch);
301 #ifdef HAVE_SYSV_COMPAT
302 static char *sysv_translate_name(const char *name) {
305 if (!(r = new(char, strlen(name) + sizeof(".service"))))
308 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
309 if (endswith(name, ".sh"))
310 /* Drop Debian-style .sh suffix */
311 strcpy(stpcpy(r, name) - 3, ".service");
314 if (startswith(name, "boot."))
315 /* Drop SuSE-style boot. prefix */
316 strcpy(stpcpy(r, name + 5), ".service");
318 #ifdef TARGET_FRUGALWARE
319 if (startswith(name, "rc."))
320 /* Drop Frugalware-style rc. prefix */
321 strcpy(stpcpy(r, name + 3), ".service");
324 /* Normal init scripts */
325 strcpy(stpcpy(r, name), ".service");
330 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
332 /* We silently ignore the $ prefix here. According to the LSB
333 * spec it simply indicates whether something is a
334 * standardized name or a distribution-specific one. Since we
335 * just follow what already exists and do not introduce new
336 * uses or names we don't care who introduced a new name. */
338 static const char * const table[] = {
339 /* LSB defined facilities */
340 "local_fs", SPECIAL_LOCAL_FS_TARGET,
341 #if defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
343 /* Due to unfortunate name selection in Mandriva,
344 * $network is provided by network-up which is ordered
345 * after network which actually starts interfaces.
346 * To break the loop, just ignore it */
347 "network", SPECIAL_NETWORK_TARGET,
349 "named", SPECIAL_NSS_LOOKUP_TARGET,
350 "portmap", SPECIAL_RPCBIND_TARGET,
351 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
352 "syslog", SPECIAL_SYSLOG_TARGET,
353 "time", SPECIAL_TIME_SYNC_TARGET,
355 /* common extensions */
356 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
357 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
360 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
361 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
365 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
366 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
367 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
371 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
382 n = *name == '$' ? name + 1 : name;
384 for (i = 0; i < ELEMENTSOF(table); i += 2) {
386 if (!streq(table[i], n))
392 if (!(r = strdup(table[i+1])))
398 /* If we don't know this name, fallback heuristics to figure
399 * out whether something is a target or a service alias. */
402 if (!unit_prefix_is_valid(n))
405 /* Facilities starting with $ are most likely targets */
406 r = unit_name_build(n, NULL, ".target");
407 } else if (filename && streq(name, filename))
408 /* Names equaling the file name of the services are redundant */
411 /* Everything else we assume to be normal service names */
412 r = sysv_translate_name(n);
423 static int sysv_fix_order(Service *s) {
429 if (s->sysv_start_priority < 0)
432 /* For each pair of services where at least one lacks a LSB
433 * header, we use the start priority value to order things. */
435 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
438 bool special_s, special_t;
445 if (UNIT(t)->load_state != UNIT_LOADED)
448 if (t->sysv_start_priority < 0)
451 /* If both units have modern headers we don't care
452 * about the priorities */
453 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
454 (UNIT(t)->fragment_path || t->sysv_has_lsb))
457 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
458 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
460 if (special_t && !special_s)
462 else if (special_s && !special_t)
464 else if (t->sysv_start_priority < s->sysv_start_priority)
466 else if (t->sysv_start_priority > s->sysv_start_priority)
471 /* FIXME: Maybe we should compare the name here lexicographically? */
473 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
480 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
483 if (!(c = new0(ExecCommand, 1)))
486 if (!(c->path = strdup(path))) {
491 if (!(c->argv = strv_new(path, arg1, NULL))) {
500 static int sysv_exec_commands(Service *s) {
505 assert(UNIT(s)->source_path);
507 c = exec_command_new(UNIT(s)->source_path, "start");
510 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
512 c = exec_command_new(UNIT(s)->source_path, "stop");
515 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
517 c = exec_command_new(UNIT(s)->source_path, "reload");
520 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
525 static int service_load_sysv_path(Service *s, const char *path) {
536 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
544 f = fopen(path, "re");
546 r = errno == ENOENT ? 0 : -errno;
551 if (fstat(fileno(f), &st) < 0) {
556 free(u->source_path);
557 u->source_path = strdup(path);
558 if (!u->source_path) {
562 u->source_mtime = timespec_load(&st.st_mtim);
564 if (null_or_empty(&st)) {
565 u->load_state = UNIT_MASKED;
573 char l[LINE_MAX], *t;
575 if (!fgets(l, sizeof(l), f)) {
580 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
590 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
592 s->sysv_has_lsb = true;
596 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
602 t += strspn(t, WHITESPACE);
604 if (state == NORMAL) {
606 /* Try to parse Red Hat style chkconfig headers */
608 if (startswith_no_case(t, "chkconfig:")) {
610 char runlevels[16], *k;
614 if (sscanf(t+10, "%15s %i %*i",
616 &start_priority) != 2) {
618 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
622 /* A start priority gathered from the
623 * symlink farms is preferred over the
624 * data from the LSB header. */
625 if (start_priority < 0 || start_priority > 99)
626 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
628 s->sysv_start_priority = start_priority;
630 char_array_0(runlevels);
631 k = delete_chars(runlevels, WHITESPACE "-");
636 if (!(d = strdup(k))) {
641 free(s->sysv_runlevels);
642 s->sysv_runlevels = d;
645 } else if (startswith_no_case(t, "description:")) {
647 size_t k = strlen(t);
651 if (t[k-1] == '\\') {
656 if ((j = strstrip(t+12)) && *j) {
657 if (!(d = strdup(j))) {
664 free(chkconfig_description);
665 chkconfig_description = d;
667 } else if (startswith_no_case(t, "pidfile:")) {
674 if (!path_is_absolute(fn)) {
675 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
679 if (!(fn = strdup(fn))) {
688 } else if (state == DESCRIPTION) {
690 /* Try to parse Red Hat style description
693 size_t k = strlen(t);
701 if ((j = strstrip(t)) && *j) {
704 if (chkconfig_description)
705 d = join(chkconfig_description, " ", j, NULL);
714 free(chkconfig_description);
715 chkconfig_description = d;
718 } else if (state == LSB || state == LSB_DESCRIPTION) {
720 if (startswith_no_case(t, "Provides:")) {
726 FOREACH_WORD_QUOTED(w, z, t+9, i) {
729 if (!(n = strndup(w, z))) {
734 r = sysv_translate_facility(n, path_get_file_name(path), &m);
743 if (unit_name_to_type(m) == UNIT_SERVICE)
744 r = unit_add_name(u, m);
751 * indication that the
753 * now available. This
756 * targets do NOT pull
759 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
762 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
767 } else if (startswith_no_case(t, "Required-Start:") ||
768 startswith_no_case(t, "Should-Start:") ||
769 startswith_no_case(t, "X-Start-Before:") ||
770 startswith_no_case(t, "X-Start-After:")) {
776 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
779 if (!(n = strndup(w, z))) {
784 r = sysv_translate_facility(n, path_get_file_name(path), &m);
787 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
797 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
800 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
804 } else if (startswith_no_case(t, "Default-Start:")) {
809 k = delete_chars(t+14, WHITESPACE "-");
812 if (!(d = strdup(k))) {
817 free(s->sysv_runlevels);
818 s->sysv_runlevels = d;
821 } else if (startswith_no_case(t, "Description:")) {
824 state = LSB_DESCRIPTION;
826 if ((j = strstrip(t+12)) && *j) {
827 if (!(d = strdup(j))) {
834 free(long_description);
835 long_description = d;
837 } else if (startswith_no_case(t, "Short-Description:")) {
842 if ((j = strstrip(t+18)) && *j) {
843 if (!(d = strdup(j))) {
850 free(short_description);
851 short_description = d;
853 } else if (state == LSB_DESCRIPTION) {
855 if (startswith(l, "#\t") || startswith(l, "# ")) {
858 if ((j = strstrip(t)) && *j) {
861 if (long_description)
862 d = join(long_description, " ", t, NULL);
871 free(long_description);
872 long_description = d;
881 if ((r = sysv_exec_commands(s)) < 0)
883 if (s->sysv_runlevels &&
884 chars_intersect(RUNLEVELS_BOOT, s->sysv_runlevels) &&
885 chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
886 /* Service has both boot and "up" runlevels
887 configured. Kill the "up" ones. */
888 delete_chars(s->sysv_runlevels, RUNLEVELS_UP);
891 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
892 /* If there a runlevels configured for this service
893 * but none of the standard ones, then we assume this
894 * is some special kind of service (which might be
895 * needed for early boot) and don't create any links
898 UNIT(s)->default_dependencies = false;
900 /* Don't timeout special services during boot (like fsck) */
903 s->timeout_usec = DEFAULT_SYSV_TIMEOUT_USEC;
905 /* Special setting for all SysV services */
906 s->type = SERVICE_FORKING;
907 s->remain_after_exit = !s->pid_file;
908 s->guess_main_pid = false;
909 s->restart = SERVICE_RESTART_NO;
910 s->exec_context.ignore_sigpipe = false;
912 if (UNIT(s)->manager->sysv_console)
913 s->exec_context.std_output = EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
915 s->exec_context.kill_mode = KILL_PROCESS;
917 /* We use the long description only if
918 * no short description is set. */
920 if (short_description)
921 description = short_description;
922 else if (chkconfig_description)
923 description = chkconfig_description;
924 else if (long_description)
925 description = long_description;
932 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
940 /* The priority that has been set in /etc/rcN.d/ hierarchies
941 * takes precedence over what is stored as default in the LSB
943 if (s->sysv_start_priority_from_rcnd >= 0)
944 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
946 u->load_state = UNIT_LOADED;
954 free(short_description);
955 free(long_description);
956 free(chkconfig_description);
961 static int service_load_sysv_name(Service *s, const char *name) {
967 /* For SysV services we strip the boot.*, rc.* and *.sh
968 * prefixes/suffixes. */
969 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
970 if (endswith(name, ".sh.service"))
975 if (startswith(name, "boot."))
979 #ifdef TARGET_FRUGALWARE
980 if (startswith(name, "rc."))
984 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
988 path = join(*p, "/", name, NULL);
992 assert(endswith(path, ".service"));
993 path[strlen(path)-8] = 0;
995 r = service_load_sysv_path(s, path);
997 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
998 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
999 /* Try Debian style *.sh source'able init scripts */
1000 strcat(path, ".sh");
1001 r = service_load_sysv_path(s, path);
1007 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1008 /* Try SUSE style boot.* init scripts */
1010 path = join(*p, "/boot.", name, NULL);
1014 /* Drop .service suffix */
1015 path[strlen(path)-8] = 0;
1016 r = service_load_sysv_path(s, path);
1021 #ifdef TARGET_FRUGALWARE
1022 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1023 /* Try Frugalware style rc.* init scripts */
1025 path = join(*p, "/rc.", name, NULL);
1029 /* Drop .service suffix */
1030 path[strlen(path)-8] = 0;
1031 r = service_load_sysv_path(s, path);
1039 if ((UNIT(s)->load_state != UNIT_STUB))
1046 static int service_load_sysv(Service *s) {
1053 /* Load service data from SysV init scripts, preferably with
1054 * LSB headers ... */
1056 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1059 if ((t = UNIT(s)->id))
1060 if ((r = service_load_sysv_name(s, t)) < 0)
1063 if (UNIT(s)->load_state == UNIT_STUB)
1064 SET_FOREACH(t, UNIT(s)->names, i) {
1065 if (t == UNIT(s)->id)
1068 if ((r = service_load_sysv_name(s, t)) < 0)
1071 if (UNIT(s)->load_state != UNIT_STUB)
1079 static int fsck_fix_order(Service *s) {
1085 if (s->fsck_passno <= 0)
1088 /* For each pair of services where both have an fsck priority
1089 * we order things based on it. */
1091 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1100 if (UNIT(t)->load_state != UNIT_LOADED)
1103 if (t->fsck_passno <= 0)
1106 if (t->fsck_passno < s->fsck_passno)
1108 else if (t->fsck_passno > s->fsck_passno)
1113 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1120 static int service_verify(Service *s) {
1123 if (UNIT(s)->load_state != UNIT_LOADED)
1126 if (!s->exec_command[SERVICE_EXEC_START]) {
1127 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1131 if (s->type != SERVICE_ONESHOT &&
1132 s->exec_command[SERVICE_EXEC_START]->command_next) {
1133 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1137 if (s->type == SERVICE_ONESHOT &&
1138 s->exec_command[SERVICE_EXEC_RELOAD]) {
1139 log_error("%s has an ExecReload setting, which is not allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1143 if (s->type == SERVICE_DBUS && !s->bus_name) {
1144 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1148 if (s->bus_name && s->type != SERVICE_DBUS)
1149 log_warning("%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1151 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
1152 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1159 static int service_add_default_dependencies(Service *s) {
1164 /* Add a number of automatic dependencies useful for the
1165 * majority of services. */
1167 /* First, pull in base system */
1168 if (UNIT(s)->manager->running_as == MANAGER_SYSTEM) {
1170 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1173 } else if (UNIT(s)->manager->running_as == MANAGER_USER) {
1175 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1179 /* Second, activate normal shutdown */
1180 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1183 static void service_fix_output(Service *s) {
1186 /* If nothing has been explicitly configured, patch default
1187 * output in. If input is socket/tty we avoid this however,
1188 * since in that case we want output to default to the same
1189 * place as we read input from. */
1191 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1192 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1193 s->exec_context.std_input == EXEC_INPUT_NULL)
1194 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1196 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1197 s->exec_context.std_input == EXEC_INPUT_NULL)
1198 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1201 static int service_load(Unit *u) {
1203 Service *s = SERVICE(u);
1207 /* Load a .service file */
1208 if ((r = unit_load_fragment(u)) < 0)
1211 #ifdef HAVE_SYSV_COMPAT
1212 /* Load a classic init script as a fallback, if we couldn't find anything */
1213 if (u->load_state == UNIT_STUB)
1214 if ((r = service_load_sysv(s)) < 0)
1218 /* Still nothing found? Then let's give up */
1219 if (u->load_state == UNIT_STUB)
1222 /* We were able to load something, then let's add in the
1223 * dropin directories. */
1224 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1227 /* This is a new unit? Then let's add in some extras */
1228 if (u->load_state == UNIT_LOADED) {
1229 if (s->type == _SERVICE_TYPE_INVALID)
1230 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1232 service_fix_output(s);
1234 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1237 if ((r = unit_add_default_cgroups(u)) < 0)
1240 #ifdef HAVE_SYSV_COMPAT
1241 if ((r = sysv_fix_order(s)) < 0)
1245 if ((r = fsck_fix_order(s)) < 0)
1249 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1252 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1253 s->notify_access = NOTIFY_MAIN;
1255 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1256 s->notify_access = NOTIFY_MAIN;
1258 if (s->type == SERVICE_DBUS || s->bus_name)
1259 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1262 if (UNIT(s)->default_dependencies)
1263 if ((r = service_add_default_dependencies(s)) < 0)
1267 return service_verify(s);
1270 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1272 ServiceExecCommand c;
1273 Service *s = SERVICE(u);
1274 const char *prefix2;
1279 p2 = strappend(prefix, "\t");
1280 prefix2 = p2 ? p2 : prefix;
1283 "%sService State: %s\n"
1285 "%sReload Result: %s\n"
1286 "%sPermissionsStartOnly: %s\n"
1287 "%sRootDirectoryStartOnly: %s\n"
1288 "%sRemainAfterExit: %s\n"
1289 "%sGuessMainPID: %s\n"
1292 "%sNotifyAccess: %s\n",
1293 prefix, service_state_to_string(s->state),
1294 prefix, service_result_to_string(s->result),
1295 prefix, service_result_to_string(s->reload_result),
1296 prefix, yes_no(s->permissions_start_only),
1297 prefix, yes_no(s->root_directory_start_only),
1298 prefix, yes_no(s->remain_after_exit),
1299 prefix, yes_no(s->guess_main_pid),
1300 prefix, service_type_to_string(s->type),
1301 prefix, service_restart_to_string(s->restart),
1302 prefix, notify_access_to_string(s->notify_access));
1304 if (s->control_pid > 0)
1306 "%sControl PID: %lu\n",
1307 prefix, (unsigned long) s->control_pid);
1309 if (s->main_pid > 0)
1312 "%sMain PID Known: %s\n"
1313 "%sMain PID Alien: %s\n",
1314 prefix, (unsigned long) s->main_pid,
1315 prefix, yes_no(s->main_pid_known),
1316 prefix, yes_no(s->main_pid_alien));
1321 prefix, s->pid_file);
1326 "%sBus Name Good: %s\n",
1327 prefix, s->bus_name,
1328 prefix, yes_no(s->bus_name_good));
1330 exec_context_dump(&s->exec_context, f, prefix);
1332 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1334 if (!s->exec_command[c])
1337 fprintf(f, "%s-> %s:\n",
1338 prefix, service_exec_command_to_string(c));
1340 exec_command_dump_list(s->exec_command[c], f, prefix2);
1343 #ifdef HAVE_SYSV_COMPAT
1346 "%sSysV Init Script has LSB Header: %s\n"
1347 "%sSysVEnabled: %s\n",
1348 prefix, yes_no(s->sysv_has_lsb),
1349 prefix, yes_no(s->sysv_enabled));
1351 if (s->sysv_start_priority >= 0)
1353 "%sSysVStartPriority: %i\n",
1354 prefix, s->sysv_start_priority);
1356 if (s->sysv_runlevels)
1357 fprintf(f, "%sSysVRunLevels: %s\n",
1358 prefix, s->sysv_runlevels);
1361 if (s->fsck_passno > 0)
1363 "%sFsckPassNo: %i\n",
1364 prefix, s->fsck_passno);
1367 fprintf(f, "%sStatus Text: %s\n",
1368 prefix, s->status_text);
1373 static int service_load_pid_file(Service *s, bool may_warn) {
1383 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1385 log_info("PID file %s not readable (yet?) after %s.",
1386 s->pid_file, service_state_to_string(s->state));
1390 r = parse_pid(k, &pid);
1396 if (kill(pid, 0) < 0 && errno != EPERM) {
1398 log_info("PID %lu read from file %s does not exist.",
1399 (unsigned long) pid, s->pid_file);
1403 if (s->main_pid_known) {
1404 if (pid == s->main_pid)
1407 log_debug("Main PID changing: %lu -> %lu",
1408 (unsigned long) s->main_pid, (unsigned long) pid);
1409 service_unwatch_main_pid(s);
1410 s->main_pid_known = false;
1412 log_debug("Main PID loaded: %lu", (unsigned long) pid);
1414 if ((r = service_set_main_pid(s, pid)) < 0)
1417 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1418 /* FIXME: we need to do something here */
1424 static int service_search_main_pid(Service *s) {
1430 /* If we know it anyway, don't ever fallback to unreliable
1432 if (s->main_pid_known)
1435 if (!s->guess_main_pid)
1438 assert(s->main_pid <= 0);
1440 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1443 log_debug("Main PID guessed: %lu", (unsigned long) pid);
1444 if ((r = service_set_main_pid(s, pid)) < 0)
1447 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1448 /* FIXME: we need to do something here */
1454 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1460 /* Notifies all our sockets when we die */
1462 if (s->socket_fd >= 0)
1465 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1466 if (u->type == UNIT_SOCKET)
1467 socket_notify_service_dead(SOCKET(u), failed_permanent);
1472 static void service_set_state(Service *s, ServiceState state) {
1473 ServiceState old_state;
1476 old_state = s->state;
1479 service_unwatch_pid_file(s);
1481 if (state != SERVICE_START_PRE &&
1482 state != SERVICE_START &&
1483 state != SERVICE_START_POST &&
1484 state != SERVICE_RELOAD &&
1485 state != SERVICE_STOP &&
1486 state != SERVICE_STOP_SIGTERM &&
1487 state != SERVICE_STOP_SIGKILL &&
1488 state != SERVICE_STOP_POST &&
1489 state != SERVICE_FINAL_SIGTERM &&
1490 state != SERVICE_FINAL_SIGKILL &&
1491 state != SERVICE_AUTO_RESTART)
1492 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1494 if (state != SERVICE_START &&
1495 state != SERVICE_START_POST &&
1496 state != SERVICE_RUNNING &&
1497 state != SERVICE_RELOAD &&
1498 state != SERVICE_STOP &&
1499 state != SERVICE_STOP_SIGTERM &&
1500 state != SERVICE_STOP_SIGKILL) {
1501 service_unwatch_main_pid(s);
1502 s->main_command = NULL;
1505 if (state != SERVICE_START_PRE &&
1506 state != SERVICE_START &&
1507 state != SERVICE_START_POST &&
1508 state != SERVICE_RELOAD &&
1509 state != SERVICE_STOP &&
1510 state != SERVICE_STOP_SIGTERM &&
1511 state != SERVICE_STOP_SIGKILL &&
1512 state != SERVICE_STOP_POST &&
1513 state != SERVICE_FINAL_SIGTERM &&
1514 state != SERVICE_FINAL_SIGKILL) {
1515 service_unwatch_control_pid(s);
1516 s->control_command = NULL;
1517 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1520 if (state == SERVICE_DEAD ||
1521 state == SERVICE_STOP ||
1522 state == SERVICE_STOP_SIGTERM ||
1523 state == SERVICE_STOP_SIGKILL ||
1524 state == SERVICE_STOP_POST ||
1525 state == SERVICE_FINAL_SIGTERM ||
1526 state == SERVICE_FINAL_SIGKILL ||
1527 state == SERVICE_FAILED ||
1528 state == SERVICE_AUTO_RESTART)
1529 service_notify_sockets_dead(s, false);
1531 if (state != SERVICE_START_PRE &&
1532 state != SERVICE_START &&
1533 state != SERVICE_START_POST &&
1534 state != SERVICE_RUNNING &&
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 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1543 service_close_socket_fd(s);
1544 service_connection_unref(s);
1547 if (state == SERVICE_STOP)
1548 service_stop_watchdog(s);
1550 /* For the inactive states unit_notify() will trim the cgroup,
1551 * but for exit we have to do that ourselves... */
1552 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1553 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1555 if (old_state != state)
1556 log_debug("%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
1558 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], s->reload_result == SERVICE_SUCCESS);
1559 s->reload_result = SERVICE_SUCCESS;
1562 static int service_coldplug(Unit *u) {
1563 Service *s = SERVICE(u);
1567 assert(s->state == SERVICE_DEAD);
1569 if (s->deserialized_state != s->state) {
1571 if (s->deserialized_state == SERVICE_START_PRE ||
1572 s->deserialized_state == SERVICE_START ||
1573 s->deserialized_state == SERVICE_START_POST ||
1574 s->deserialized_state == SERVICE_RELOAD ||
1575 s->deserialized_state == SERVICE_STOP ||
1576 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1577 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1578 s->deserialized_state == SERVICE_STOP_POST ||
1579 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1580 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1581 s->deserialized_state == SERVICE_AUTO_RESTART) {
1583 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1586 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1588 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1593 if ((s->deserialized_state == SERVICE_START &&
1594 (s->type == SERVICE_FORKING ||
1595 s->type == SERVICE_DBUS ||
1596 s->type == SERVICE_ONESHOT ||
1597 s->type == SERVICE_NOTIFY)) ||
1598 s->deserialized_state == SERVICE_START_POST ||
1599 s->deserialized_state == SERVICE_RUNNING ||
1600 s->deserialized_state == SERVICE_RELOAD ||
1601 s->deserialized_state == SERVICE_STOP ||
1602 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1603 s->deserialized_state == SERVICE_STOP_SIGKILL)
1604 if (s->main_pid > 0)
1605 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1608 if (s->deserialized_state == SERVICE_START_PRE ||
1609 s->deserialized_state == SERVICE_START ||
1610 s->deserialized_state == SERVICE_START_POST ||
1611 s->deserialized_state == SERVICE_RELOAD ||
1612 s->deserialized_state == SERVICE_STOP ||
1613 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1614 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1615 s->deserialized_state == SERVICE_STOP_POST ||
1616 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1617 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1618 if (s->control_pid > 0)
1619 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1622 if (s->deserialized_state == SERVICE_START_POST ||
1623 s->deserialized_state == SERVICE_RUNNING)
1624 service_handle_watchdog(s);
1626 service_set_state(s, s->deserialized_state);
1631 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1635 unsigned rn_fds = 0;
1642 if (s->socket_fd >= 0)
1645 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1650 if (u->type != UNIT_SOCKET)
1655 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1667 if (!(t = new(int, rn_fds+cn_fds))) {
1673 memcpy(t, rfds, rn_fds * sizeof(int));
1674 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1679 rn_fds = rn_fds+cn_fds;
1694 static int service_spawn(
1699 bool apply_permissions,
1701 bool apply_tty_stdin,
1702 bool set_notify_socket,
1708 int *fds = NULL, *fdsbuf = NULL;
1709 unsigned n_fds = 0, n_env = 0;
1710 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1717 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1718 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1719 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1721 if (s->socket_fd >= 0) {
1722 fds = &s->socket_fd;
1725 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1732 if (timeout && s->timeout_usec) {
1733 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1736 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1738 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1743 if (!(our_env = new0(char*, 4))) {
1748 if (set_notify_socket)
1749 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1754 if (s->main_pid > 0)
1755 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1760 if (s->watchdog_usec > 0)
1761 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1766 if (!(final_env = strv_env_merge(2,
1767 UNIT(s)->manager->environment,
1782 UNIT(s)->manager->confirm_spawn,
1783 UNIT(s)->cgroup_bondings,
1784 UNIT(s)->cgroup_attributes,
1785 is_control ? "control" : NULL,
1786 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1793 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1794 /* FIXME: we need to do something here */
1800 strv_free(final_env);
1810 strv_free(final_env);
1813 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1818 static int main_pid_good(Service *s) {
1821 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1824 /* If we know the pid file, then lets just check if it is
1826 if (s->main_pid_known) {
1828 /* If it's an alien child let's check if it is still
1830 if (s->main_pid_alien)
1831 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1833 /* .. otherwise assume we'll get a SIGCHLD for it,
1834 * which we really should wait for to collect exit
1835 * status and code */
1836 return s->main_pid > 0;
1839 /* We don't know the pid */
1843 static int control_pid_good(Service *s) {
1846 return s->control_pid > 0;
1849 static int cgroup_good(Service *s) {
1854 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1860 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1864 if (f != SERVICE_SUCCESS)
1867 if (allow_restart &&
1868 !s->forbid_restart &&
1869 (s->restart == SERVICE_RESTART_ALWAYS ||
1870 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1871 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1872 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1873 s->result == SERVICE_FAILURE_CORE_DUMP)))) {
1875 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
1879 service_set_state(s, SERVICE_AUTO_RESTART);
1881 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1883 s->forbid_restart = false;
1888 log_warning("%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1889 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1892 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1894 static void service_enter_stop_post(Service *s, ServiceResult f) {
1898 if (f != SERVICE_SUCCESS)
1901 service_unwatch_control_pid(s);
1903 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1904 s->control_command_id = SERVICE_EXEC_STOP_POST;
1906 r = service_spawn(s,
1910 !s->permissions_start_only,
1911 !s->root_directory_start_only,
1920 service_set_state(s, SERVICE_STOP_POST);
1922 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1927 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
1928 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1931 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1933 Set *pid_set = NULL;
1934 bool wait_for_exit = false;
1938 if (f != SERVICE_SUCCESS)
1941 if (s->exec_context.kill_mode != KILL_NONE) {
1942 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1944 if (s->main_pid > 0) {
1945 if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
1946 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1948 wait_for_exit = !s->main_pid_alien;
1951 if (s->control_pid > 0) {
1952 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1953 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1955 wait_for_exit = true;
1958 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1960 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1965 /* Exclude the main/control pids from being killed via the cgroup */
1966 if (s->main_pid > 0)
1967 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1970 if (s->control_pid > 0)
1971 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1974 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
1976 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1977 log_warning("Failed to kill control group: %s", strerror(-r));
1979 wait_for_exit = true;
1986 if (wait_for_exit) {
1987 if (s->timeout_usec > 0)
1988 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1991 service_set_state(s, state);
1992 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1993 service_enter_stop_post(s, SERVICE_SUCCESS);
1995 service_enter_dead(s, SERVICE_SUCCESS, true);
2000 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2002 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2003 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2005 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2011 static void service_enter_stop(Service *s, ServiceResult f) {
2016 if (f != SERVICE_SUCCESS)
2019 service_unwatch_control_pid(s);
2021 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2022 s->control_command_id = SERVICE_EXEC_STOP;
2024 r = service_spawn(s,
2028 !s->permissions_start_only,
2029 !s->root_directory_start_only,
2037 service_set_state(s, SERVICE_STOP);
2039 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2044 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2045 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2048 static void service_enter_running(Service *s, ServiceResult f) {
2049 int main_pid_ok, cgroup_ok;
2052 if (f != SERVICE_SUCCESS)
2055 main_pid_ok = main_pid_good(s);
2056 cgroup_ok = cgroup_good(s);
2058 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2059 (s->bus_name_good || s->type != SERVICE_DBUS))
2060 service_set_state(s, SERVICE_RUNNING);
2061 else if (s->remain_after_exit)
2062 service_set_state(s, SERVICE_EXITED);
2064 service_enter_stop(s, SERVICE_SUCCESS);
2067 static void service_enter_start_post(Service *s) {
2071 service_unwatch_control_pid(s);
2073 if (s->watchdog_usec > 0)
2074 service_reset_watchdog(s);
2076 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2077 s->control_command_id = SERVICE_EXEC_START_POST;
2079 r = service_spawn(s,
2083 !s->permissions_start_only,
2084 !s->root_directory_start_only,
2092 service_set_state(s, SERVICE_START_POST);
2094 service_enter_running(s, SERVICE_SUCCESS);
2099 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2100 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2103 static void service_enter_start(Service *s) {
2110 assert(s->exec_command[SERVICE_EXEC_START]);
2111 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2113 if (s->type == SERVICE_FORKING)
2114 service_unwatch_control_pid(s);
2116 service_unwatch_main_pid(s);
2118 /* We want to ensure that nobody leaks processes from
2119 * START_PRE here, so let's go on a killing spree, People
2120 * should not spawn long running processes from START_PRE. */
2121 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2123 if (s->type == SERVICE_FORKING) {
2124 s->control_command_id = SERVICE_EXEC_START;
2125 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2127 s->main_command = NULL;
2129 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2130 s->control_command = NULL;
2132 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2135 r = service_spawn(s,
2137 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
2142 s->notify_access != NOTIFY_NONE,
2148 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2149 /* For simple services we immediately start
2150 * the START_POST binaries. */
2152 service_set_main_pid(s, pid);
2153 service_enter_start_post(s);
2155 } else if (s->type == SERVICE_FORKING) {
2157 /* For forking services we wait until the start
2158 * process exited. */
2160 s->control_pid = pid;
2161 service_set_state(s, SERVICE_START);
2163 } else if (s->type == SERVICE_ONESHOT ||
2164 s->type == SERVICE_DBUS ||
2165 s->type == SERVICE_NOTIFY) {
2167 /* For oneshot services we wait until the start
2168 * process exited, too, but it is our main process. */
2170 /* For D-Bus services we know the main pid right away,
2171 * but wait for the bus name to appear on the
2172 * bus. Notify services are similar. */
2174 service_set_main_pid(s, pid);
2175 service_set_state(s, SERVICE_START);
2177 assert_not_reached("Unknown service type");
2182 log_warning("%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2183 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2186 static void service_enter_start_pre(Service *s) {
2191 service_unwatch_control_pid(s);
2193 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2195 /* Before we start anything, let's clear up what might
2196 * be left from previous runs. */
2197 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2199 s->control_command_id = SERVICE_EXEC_START_PRE;
2201 r = service_spawn(s,
2205 !s->permissions_start_only,
2206 !s->root_directory_start_only,
2214 service_set_state(s, SERVICE_START_PRE);
2216 service_enter_start(s);
2221 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2222 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2225 static void service_enter_restart(Service *s) {
2230 dbus_error_init(&error);
2233 log_info("Job pending for unit, delaying automatic restart.");
2235 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
2241 /* Any units that are bound to this service must also be
2242 * restarted. We use JOB_RESTART (instead of the more obvious
2243 * JOB_START) here so that those dependency jobs will be added
2245 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2249 log_debug("%s scheduled restart job.", UNIT(s)->id);
2253 log_warning("%s failed to schedule restart job: %s", UNIT(s)->id, bus_error(&error, -r));
2254 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2256 dbus_error_free(&error);
2259 static void service_enter_reload(Service *s) {
2264 service_unwatch_control_pid(s);
2266 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2267 s->control_command_id = SERVICE_EXEC_RELOAD;
2269 r = service_spawn(s,
2273 !s->permissions_start_only,
2274 !s->root_directory_start_only,
2282 service_set_state(s, SERVICE_RELOAD);
2284 service_enter_running(s, SERVICE_SUCCESS);
2289 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
2290 s->reload_result = SERVICE_FAILURE_RESOURCES;
2291 service_enter_running(s, SERVICE_SUCCESS);
2294 static void service_run_next_control(Service *s) {
2298 assert(s->control_command);
2299 assert(s->control_command->command_next);
2301 assert(s->control_command_id != SERVICE_EXEC_START);
2303 s->control_command = s->control_command->command_next;
2304 service_unwatch_control_pid(s);
2306 r = service_spawn(s,
2310 !s->permissions_start_only,
2311 !s->root_directory_start_only,
2312 s->control_command_id == SERVICE_EXEC_START_PRE ||
2313 s->control_command_id == SERVICE_EXEC_STOP_POST,
2323 log_warning("%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
2325 if (s->state == SERVICE_START_PRE)
2326 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2327 else if (s->state == SERVICE_STOP)
2328 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2329 else if (s->state == SERVICE_STOP_POST)
2330 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2331 else if (s->state == SERVICE_RELOAD) {
2332 s->reload_result = SERVICE_FAILURE_RESOURCES;
2333 service_enter_running(s, SERVICE_SUCCESS);
2335 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2338 static void service_run_next_main(Service *s) {
2343 assert(s->main_command);
2344 assert(s->main_command->command_next);
2345 assert(s->type == SERVICE_ONESHOT);
2347 s->main_command = s->main_command->command_next;
2348 service_unwatch_main_pid(s);
2350 r = service_spawn(s,
2357 s->notify_access != NOTIFY_NONE,
2363 service_set_main_pid(s, pid);
2368 log_warning("%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2369 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2372 static int service_start_limit_test(Service *s) {
2375 if (ratelimit_test(&s->start_limit))
2378 switch (s->start_limit_action) {
2380 case SERVICE_START_LIMIT_NONE:
2381 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s)->id);
2384 case SERVICE_START_LIMIT_REBOOT: {
2388 dbus_error_init(&error);
2390 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2392 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2394 log_error("Failed to reboot: %s.", bus_error(&error, r));
2395 dbus_error_free(&error);
2401 case SERVICE_START_LIMIT_REBOOT_FORCE:
2402 log_warning("%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2403 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2406 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2407 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2408 reboot(RB_AUTOBOOT);
2412 log_error("start limit action=%i", s->start_limit_action);
2413 assert_not_reached("Unknown StartLimitAction.");
2419 static int service_start(Unit *u) {
2420 Service *s = SERVICE(u);
2425 /* We cannot fulfill this request right now, try again later
2427 if (s->state == SERVICE_STOP ||
2428 s->state == SERVICE_STOP_SIGTERM ||
2429 s->state == SERVICE_STOP_SIGKILL ||
2430 s->state == SERVICE_STOP_POST ||
2431 s->state == SERVICE_FINAL_SIGTERM ||
2432 s->state == SERVICE_FINAL_SIGKILL)
2435 /* Already on it! */
2436 if (s->state == SERVICE_START_PRE ||
2437 s->state == SERVICE_START ||
2438 s->state == SERVICE_START_POST)
2441 /* A service that will be restarted must be stopped first to
2442 * trigger BindTo and/or OnFailure dependencies. If a user
2443 * does not want to wait for the holdoff time to elapse, the
2444 * service should be manually restarted, not started. */
2445 if (s->state == SERVICE_AUTO_RESTART) {
2446 log_warning("%s automatic restart is pending, must be stopped before issuing start request.", UNIT(s)->id);
2450 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2452 /* Make sure we don't enter a busy loop of some kind. */
2453 r = service_start_limit_test(s);
2455 service_notify_sockets_dead(s, true);
2459 s->result = SERVICE_SUCCESS;
2460 s->reload_result = SERVICE_SUCCESS;
2461 s->main_pid_known = false;
2462 s->main_pid_alien = false;
2463 s->forbid_restart = false;
2465 service_enter_start_pre(s);
2469 static int service_stop(Unit *u) {
2470 Service *s = SERVICE(u);
2474 /* Don't create restart jobs from here. */
2475 s->forbid_restart = true;
2478 if (s->state == SERVICE_STOP ||
2479 s->state == SERVICE_STOP_SIGTERM ||
2480 s->state == SERVICE_STOP_SIGKILL ||
2481 s->state == SERVICE_STOP_POST ||
2482 s->state == SERVICE_FINAL_SIGTERM ||
2483 s->state == SERVICE_FINAL_SIGKILL)
2486 /* A restart will be scheduled or is in progress. */
2487 if (s->state == SERVICE_AUTO_RESTART) {
2488 service_enter_dead(s, SERVICE_SUCCESS, false);
2492 /* If there's already something running we go directly into
2494 if (s->state == SERVICE_START_PRE ||
2495 s->state == SERVICE_START ||
2496 s->state == SERVICE_START_POST ||
2497 s->state == SERVICE_RELOAD) {
2498 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2502 assert(s->state == SERVICE_RUNNING ||
2503 s->state == SERVICE_EXITED);
2505 service_enter_stop(s, SERVICE_SUCCESS);
2509 static int service_reload(Unit *u) {
2510 Service *s = SERVICE(u);
2514 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2516 service_enter_reload(s);
2520 static bool service_can_reload(Unit *u) {
2521 Service *s = SERVICE(u);
2525 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2528 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2529 Service *s = SERVICE(u);
2535 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2536 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2537 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2539 if (s->control_pid > 0)
2540 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2542 if (s->main_pid_known && s->main_pid > 0)
2543 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2545 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2548 unit_serialize_item(u, f, "status-text", s->status_text);
2550 /* FIXME: There's a minor uncleanliness here: if there are
2551 * multiple commands attached here, we will start from the
2552 * first one again */
2553 if (s->control_command_id >= 0)
2554 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2556 if (s->socket_fd >= 0) {
2559 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2562 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2565 if (s->main_exec_status.pid > 0) {
2566 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2567 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2568 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2570 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2571 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2572 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2575 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2576 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2581 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2582 Service *s = SERVICE(u);
2589 if (streq(key, "state")) {
2592 if ((state = service_state_from_string(value)) < 0)
2593 log_debug("Failed to parse state value %s", value);
2595 s->deserialized_state = state;
2596 } else if (streq(key, "result")) {
2599 f = service_result_from_string(value);
2601 log_debug("Failed to parse result value %s", value);
2602 else if (f != SERVICE_SUCCESS)
2605 } else if (streq(key, "reload-result")) {
2608 f = service_result_from_string(value);
2610 log_debug("Failed to parse reload result value %s", value);
2611 else if (f != SERVICE_SUCCESS)
2612 s->reload_result = f;
2614 } else if (streq(key, "control-pid")) {
2617 if (parse_pid(value, &pid) < 0)
2618 log_debug("Failed to parse control-pid value %s", value);
2620 s->control_pid = pid;
2621 } else if (streq(key, "main-pid")) {
2624 if (parse_pid(value, &pid) < 0)
2625 log_debug("Failed to parse main-pid value %s", value);
2627 service_set_main_pid(s, (pid_t) pid);
2628 } else if (streq(key, "main-pid-known")) {
2631 if ((b = parse_boolean(value)) < 0)
2632 log_debug("Failed to parse main-pid-known value %s", value);
2634 s->main_pid_known = b;
2635 } else if (streq(key, "status-text")) {
2638 if ((t = strdup(value))) {
2639 free(s->status_text);
2643 } else if (streq(key, "control-command")) {
2644 ServiceExecCommand id;
2646 if ((id = service_exec_command_from_string(value)) < 0)
2647 log_debug("Failed to parse exec-command value %s", value);
2649 s->control_command_id = id;
2650 s->control_command = s->exec_command[id];
2652 } else if (streq(key, "socket-fd")) {
2655 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2656 log_debug("Failed to parse socket-fd value %s", value);
2659 if (s->socket_fd >= 0)
2660 close_nointr_nofail(s->socket_fd);
2661 s->socket_fd = fdset_remove(fds, fd);
2663 } else if (streq(key, "main-exec-status-pid")) {
2666 if (parse_pid(value, &pid) < 0)
2667 log_debug("Failed to parse main-exec-status-pid value %s", value);
2669 s->main_exec_status.pid = pid;
2670 } else if (streq(key, "main-exec-status-code")) {
2673 if (safe_atoi(value, &i) < 0)
2674 log_debug("Failed to parse main-exec-status-code value %s", value);
2676 s->main_exec_status.code = i;
2677 } else if (streq(key, "main-exec-status-status")) {
2680 if (safe_atoi(value, &i) < 0)
2681 log_debug("Failed to parse main-exec-status-status value %s", value);
2683 s->main_exec_status.status = i;
2684 } else if (streq(key, "main-exec-status-start"))
2685 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2686 else if (streq(key, "main-exec-status-exit"))
2687 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2688 else if (streq(key, "watchdog-timestamp"))
2689 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2691 log_debug("Unknown serialization key '%s'", key);
2696 static UnitActiveState service_active_state(Unit *u) {
2699 return state_translation_table[SERVICE(u)->state];
2702 static const char *service_sub_state_to_string(Unit *u) {
2705 return service_state_to_string(SERVICE(u)->state);
2708 static bool service_check_gc(Unit *u) {
2709 Service *s = SERVICE(u);
2713 /* Never clean up services that still have a process around,
2714 * even if the service is formally dead. */
2715 if (cgroup_good(s) > 0 ||
2716 main_pid_good(s) > 0 ||
2717 control_pid_good(s) > 0)
2720 #ifdef HAVE_SYSV_COMPAT
2728 static bool service_check_snapshot(Unit *u) {
2729 Service *s = SERVICE(u);
2733 return !s->got_socket_fd;
2736 static int service_retry_pid_file(Service *s) {
2739 assert(s->pid_file);
2740 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2742 r = service_load_pid_file(s, false);
2746 service_unwatch_pid_file(s);
2748 service_enter_running(s, SERVICE_SUCCESS);
2752 static int service_watch_pid_file(Service *s) {
2755 log_debug("Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
2756 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2760 /* the pidfile might have appeared just before we set the watch */
2761 service_retry_pid_file(s);
2765 log_error("Failed to set a watch for %s's PID file %s: %s",
2766 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2767 service_unwatch_pid_file(s);
2771 static int service_demand_pid_file(Service *s) {
2774 assert(s->pid_file);
2775 assert(!s->pid_file_pathspec);
2777 ps = new0(PathSpec, 1);
2781 ps->path = strdup(s->pid_file);
2787 path_kill_slashes(ps->path);
2789 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2790 * keep their PID file open all the time. */
2791 ps->type = PATH_MODIFIED;
2792 ps->inotify_fd = -1;
2794 s->pid_file_pathspec = ps;
2796 return service_watch_pid_file(s);
2799 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2800 Service *s = SERVICE(u);
2804 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2805 assert(s->pid_file_pathspec);
2806 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2808 log_debug("inotify event for %s", u->id);
2810 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2813 if (service_retry_pid_file(s) == 0)
2816 if (service_watch_pid_file(s) < 0)
2821 service_unwatch_pid_file(s);
2822 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2825 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2826 Service *s = SERVICE(u);
2832 if (UNIT(s)->fragment_path ? is_clean_exit(code, status) : is_clean_exit_lsb(code, status))
2833 f = SERVICE_SUCCESS;
2834 else if (code == CLD_EXITED)
2835 f = SERVICE_FAILURE_EXIT_CODE;
2836 else if (code == CLD_KILLED)
2837 f = SERVICE_FAILURE_SIGNAL;
2838 else if (code == CLD_DUMPED)
2839 f = SERVICE_FAILURE_CORE_DUMP;
2841 assert_not_reached("Unknown code");
2843 if (s->main_pid == pid) {
2844 /* Forking services may occasionally move to a new PID.
2845 * As long as they update the PID file before exiting the old
2846 * PID, they're fine. */
2847 if (service_load_pid_file(s, false) == 0)
2851 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2853 /* If this is not a forking service than the main
2854 * process got started and hence we copy the exit
2855 * status so that it is recorded both as main and as
2856 * control process exit status */
2857 if (s->main_command) {
2858 s->main_command->exec_status = s->main_exec_status;
2860 if (s->main_command->ignore)
2861 f = SERVICE_SUCCESS;
2864 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2865 "%s: main process exited, code=%s, status=%i", u->id, sigchld_code_to_string(code), status);
2867 if (f != SERVICE_SUCCESS)
2870 if (s->main_command &&
2871 s->main_command->command_next &&
2872 f == SERVICE_SUCCESS) {
2874 /* There is another command to *
2875 * execute, so let's do that. */
2877 log_debug("%s running next main command for state %s", u->id, service_state_to_string(s->state));
2878 service_run_next_main(s);
2882 /* The service exited, so the service is officially
2884 s->main_command = NULL;
2888 case SERVICE_START_POST:
2889 case SERVICE_RELOAD:
2891 /* Need to wait until the operation is
2896 if (s->type == SERVICE_ONESHOT) {
2897 /* This was our main goal, so let's go on */
2898 if (f == SERVICE_SUCCESS)
2899 service_enter_start_post(s);
2901 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2904 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2909 case SERVICE_RUNNING:
2910 service_enter_running(s, f);
2913 case SERVICE_STOP_SIGTERM:
2914 case SERVICE_STOP_SIGKILL:
2916 if (!control_pid_good(s))
2917 service_enter_stop_post(s, f);
2919 /* If there is still a control process, wait for that first */
2923 assert_not_reached("Uh, main process died at wrong time.");
2927 } else if (s->control_pid == pid) {
2931 if (s->control_command) {
2932 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2934 if (s->control_command->ignore)
2935 f = SERVICE_SUCCESS;
2938 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2939 "%s: control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
2941 if (f != SERVICE_SUCCESS)
2944 /* Immediately get rid of the cgroup, so that the
2945 * kernel doesn't delay the cgroup empty messages for
2946 * the service cgroup any longer than necessary */
2947 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2949 if (s->control_command &&
2950 s->control_command->command_next &&
2951 f == SERVICE_SUCCESS) {
2953 /* There is another command to *
2954 * execute, so let's do that. */
2956 log_debug("%s running next control command for state %s", u->id, service_state_to_string(s->state));
2957 service_run_next_control(s);
2960 /* No further commands for this step, so let's
2961 * figure out what to do next */
2963 s->control_command = NULL;
2964 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2966 log_debug("%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
2970 case SERVICE_START_PRE:
2971 if (f == SERVICE_SUCCESS)
2972 service_enter_start(s);
2974 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2978 assert(s->type == SERVICE_FORKING);
2980 if (f != SERVICE_SUCCESS) {
2981 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2986 bool has_start_post;
2989 /* Let's try to load the pid file here if we can.
2990 * The PID file might actually be created by a START_POST
2991 * script. In that case don't worry if the loading fails. */
2993 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2994 r = service_load_pid_file(s, !has_start_post);
2995 if (!has_start_post && r < 0) {
2996 r = service_demand_pid_file(s);
2997 if (r < 0 || !cgroup_good(s))
2998 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3002 service_search_main_pid(s);
3004 service_enter_start_post(s);
3007 case SERVICE_START_POST:
3008 if (f != SERVICE_SUCCESS) {
3009 service_enter_stop(s, f);
3016 r = service_load_pid_file(s, true);
3018 r = service_demand_pid_file(s);
3019 if (r < 0 || !cgroup_good(s))
3020 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3024 service_search_main_pid(s);
3026 service_enter_running(s, SERVICE_SUCCESS);
3029 case SERVICE_RELOAD:
3030 if (f == SERVICE_SUCCESS) {
3031 service_load_pid_file(s, true);
3032 service_search_main_pid(s);
3035 s->reload_result = f;
3036 service_enter_running(s, SERVICE_SUCCESS);
3040 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3043 case SERVICE_STOP_SIGTERM:
3044 case SERVICE_STOP_SIGKILL:
3045 if (main_pid_good(s) <= 0)
3046 service_enter_stop_post(s, f);
3048 /* If there is still a service
3049 * process around, wait until
3050 * that one quit, too */
3053 case SERVICE_STOP_POST:
3054 case SERVICE_FINAL_SIGTERM:
3055 case SERVICE_FINAL_SIGKILL:
3056 service_enter_dead(s, f, true);
3060 assert_not_reached("Uh, control process died at wrong time.");
3065 /* Notify clients about changed exit status */
3066 unit_add_to_dbus_queue(u);
3069 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3070 Service *s = SERVICE(u);
3073 assert(elapsed == 1);
3075 if (w == &s->watchdog_watch) {
3076 service_handle_watchdog(s);
3080 assert(w == &s->timer_watch);
3084 case SERVICE_START_PRE:
3086 log_warning("%s operation timed out. Terminating.", u->id);
3087 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3090 case SERVICE_START_POST:
3091 log_warning("%s operation timed out. Stopping.", u->id);
3092 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3095 case SERVICE_RELOAD:
3096 log_warning("%s operation timed out. Stopping.", u->id);
3097 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3098 service_enter_running(s, SERVICE_SUCCESS);
3102 log_warning("%s stopping timed out. Terminating.", u->id);
3103 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3106 case SERVICE_STOP_SIGTERM:
3107 if (s->exec_context.send_sigkill) {
3108 log_warning("%s stopping timed out. Killing.", u->id);
3109 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3111 log_warning("%s stopping timed out. Skipping SIGKILL.", u->id);
3112 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3117 case SERVICE_STOP_SIGKILL:
3118 /* Uh, we sent a SIGKILL and it is still not gone?
3119 * Must be something we cannot kill, so let's just be
3120 * weirded out and continue */
3122 log_warning("%s still around after SIGKILL. Ignoring.", u->id);
3123 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3126 case SERVICE_STOP_POST:
3127 log_warning("%s stopping timed out (2). Terminating.", u->id);
3128 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3131 case SERVICE_FINAL_SIGTERM:
3132 if (s->exec_context.send_sigkill) {
3133 log_warning("%s stopping timed out (2). Killing.", u->id);
3134 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3136 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->id);
3137 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3142 case SERVICE_FINAL_SIGKILL:
3143 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->id);
3144 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3147 case SERVICE_AUTO_RESTART:
3148 log_info("%s holdoff time over, scheduling restart.", u->id);
3149 service_enter_restart(s);
3153 assert_not_reached("Timeout at wrong time.");
3157 static void service_cgroup_notify_event(Unit *u) {
3158 Service *s = SERVICE(u);
3162 log_debug("%s: cgroup is empty", u->id);
3166 /* Waiting for SIGCHLD is usually more interesting,
3167 * because it includes return codes/signals. Which is
3168 * why we ignore the cgroup events for most cases,
3169 * except when we don't know pid which to expect the
3173 case SERVICE_START_POST:
3174 /* If we were hoping for the daemon to write its PID file,
3175 * we can give up now. */
3176 if (s->pid_file_pathspec) {
3177 log_warning("%s never wrote its PID file. Failing.", UNIT(s)->id);
3178 service_unwatch_pid_file(s);
3179 if (s->state == SERVICE_START)
3180 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3182 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3186 case SERVICE_RUNNING:
3187 /* service_enter_running() will figure out what to do */
3188 service_enter_running(s, SERVICE_SUCCESS);
3191 case SERVICE_STOP_SIGTERM:
3192 case SERVICE_STOP_SIGKILL:
3194 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3195 service_enter_stop_post(s, SERVICE_SUCCESS);
3199 case SERVICE_FINAL_SIGTERM:
3200 case SERVICE_FINAL_SIGKILL:
3201 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3202 service_enter_dead(s, SERVICE_SUCCESS, SERVICE_SUCCESS);
3211 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3212 Service *s = SERVICE(u);
3217 if (s->notify_access == NOTIFY_NONE) {
3218 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3219 u->id, (unsigned long) pid);
3223 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3224 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3225 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3229 log_debug("%s: Got message", u->id);
3231 /* Interpret MAINPID= */
3232 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3233 (s->state == SERVICE_START ||
3234 s->state == SERVICE_START_POST ||
3235 s->state == SERVICE_RUNNING ||
3236 s->state == SERVICE_RELOAD)) {
3238 if (parse_pid(e + 8, &pid) < 0)
3239 log_warning("Failed to parse notification message %s", e);
3241 log_debug("%s: got %s", u->id, e);
3242 service_set_main_pid(s, pid);
3246 /* Interpret READY= */
3247 if (s->type == SERVICE_NOTIFY &&
3248 s->state == SERVICE_START &&
3249 strv_find(tags, "READY=1")) {
3250 log_debug("%s: got READY=1", u->id);
3252 service_enter_start_post(s);
3255 /* Interpret STATUS= */
3256 e = strv_find_prefix(tags, "STATUS=");
3262 if (!utf8_is_valid(e+7)) {
3263 log_warning("Status message in notification is not UTF-8 clean.");
3269 log_error("Failed to allocate string.");
3273 log_debug("%s: got %s", u->id, e);
3275 free(s->status_text);
3278 free(s->status_text);
3279 s->status_text = NULL;
3283 if (strv_find(tags, "WATCHDOG=1")) {
3284 log_debug("%s: got WATCHDOG=1", u->id);
3285 service_reset_watchdog(s);
3288 /* Notify clients about changed status or main pid */
3289 unit_add_to_dbus_queue(u);
3292 #ifdef HAVE_SYSV_COMPAT
3295 static void sysv_facility_in_insserv_conf(Manager *mgr) {
3299 if (!(f = fopen("/etc/insserv.conf", "re"))) {
3300 r = errno == ENOENT ? 0 : -errno;
3305 char l[LINE_MAX], *t;
3306 char **parsed = NULL;
3308 if (!fgets(l, sizeof(l), f)) {
3313 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r));
3318 if (*t != '$' && *t != '<')
3321 parsed = strv_split(t,WHITESPACE);
3322 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3323 if (parsed && !startswith_no_case (parsed[0], "<interactive>")) {
3326 if (sysv_translate_facility(parsed[0], NULL, &facility) < 0)
3328 if ((u = manager_get_unit(mgr, facility)) && (u->type == UNIT_TARGET)) {
3330 char *dep = NULL, *name, **j;
3332 STRV_FOREACH (j, parsed+1) {
3341 if (sysv_translate_facility(name, NULL, &dep) < 0)
3344 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, e, dep, NULL, true);
3359 static int service_enumerate(Manager *m) {
3363 char *path = NULL, *fpath = NULL, *name = NULL;
3364 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3371 if (m->running_as != MANAGER_SYSTEM)
3374 zero(runlevel_services);
3376 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3377 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3381 path = join(*p, "/", rcnd_table[i].path, NULL);
3390 if (!(d = opendir(path))) {
3391 if (errno != ENOENT)
3392 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3397 while ((de = readdir(d))) {
3400 if (ignore_file(de->d_name))
3403 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3406 if (strlen(de->d_name) < 4)
3409 a = undecchar(de->d_name[1]);
3410 b = undecchar(de->d_name[2]);
3416 fpath = join(path, "/", de->d_name, NULL);
3422 if (access(fpath, X_OK) < 0) {
3424 if (errno != ENOENT)
3425 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3431 if (!(name = sysv_translate_name(de->d_name + 3))) {
3436 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
3437 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3441 if (de->d_name[0] == 'S') {
3443 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
3444 SERVICE(service)->sysv_start_priority_from_rcnd =
3445 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3447 SERVICE(service)->sysv_enabled = true;
3450 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3453 if ((r = set_put(runlevel_services[i], service)) < 0)
3456 } else if (de->d_name[0] == 'K' &&
3457 (rcnd_table[i].type == RUNLEVEL_DOWN ||
3458 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
3460 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3463 if ((r = set_put(shutdown_services, service)) < 0)
3469 /* Now we loaded all stubs and are aware of the lowest
3470 start-up priority for all services, not let's actually load
3471 the services, this will also tell us which services are
3472 actually native now */
3473 manager_dispatch_load_queue(m);
3475 /* If this is a native service, rely on native ways to pull in
3476 * a service, don't pull it in via sysv rcN.d links. */
3477 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3478 SET_FOREACH(service, runlevel_services[i], j) {
3479 service = unit_follow_merge(service);
3481 if (service->fragment_path)
3484 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3488 /* We honour K links only for halt/reboot. For the normal
3489 * runlevels we assume the stop jobs will be implicitly added
3490 * by the core logic. Also, we don't really distinguish here
3491 * between the runlevels 0 and 6 and just add them to the
3492 * special shutdown target. On SUSE the boot.d/ runlevel is
3493 * also used for shutdown, so we add links for that too to the
3494 * shutdown target.*/
3495 SET_FOREACH(service, shutdown_services, j) {
3496 service = unit_follow_merge(service);
3498 if (service->fragment_path)
3501 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3508 sysv_facility_in_insserv_conf (m);
3516 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3517 set_free(runlevel_services[i]);
3518 set_free(shutdown_services);
3527 static void service_bus_name_owner_change(
3530 const char *old_owner,
3531 const char *new_owner) {
3533 Service *s = SERVICE(u);
3538 assert(streq(s->bus_name, name));
3539 assert(old_owner || new_owner);
3541 if (old_owner && new_owner)
3542 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
3544 log_debug("%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
3546 log_debug("%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
3548 s->bus_name_good = !!new_owner;
3550 if (s->type == SERVICE_DBUS) {
3552 /* service_enter_running() will figure out what to
3554 if (s->state == SERVICE_RUNNING)
3555 service_enter_running(s, SERVICE_SUCCESS);
3556 else if (s->state == SERVICE_START && new_owner)
3557 service_enter_start_post(s);
3559 } else if (new_owner &&
3561 (s->state == SERVICE_START ||
3562 s->state == SERVICE_START_POST ||
3563 s->state == SERVICE_RUNNING ||
3564 s->state == SERVICE_RELOAD)) {
3566 /* Try to acquire PID from bus service */
3567 log_debug("Trying to acquire PID from D-Bus name...");
3569 bus_query_pid(u->manager, name);
3573 static void service_bus_query_pid_done(
3578 Service *s = SERVICE(u);
3583 log_debug("%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3585 if (s->main_pid <= 0 &&
3586 (s->state == SERVICE_START ||
3587 s->state == SERVICE_START_POST ||
3588 s->state == SERVICE_RUNNING ||
3589 s->state == SERVICE_RELOAD))
3590 service_set_main_pid(s, pid);
3593 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3598 /* This is called by the socket code when instantiating a new
3599 * service for a stream socket and the socket needs to be
3602 if (UNIT(s)->load_state != UNIT_LOADED)
3605 if (s->socket_fd >= 0)
3608 if (s->state != SERVICE_DEAD)
3612 s->got_socket_fd = true;
3614 unit_ref_set(&s->accept_socket, UNIT(sock));
3616 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3619 static void service_reset_failed(Unit *u) {
3620 Service *s = SERVICE(u);
3624 if (s->state == SERVICE_FAILED)
3625 service_set_state(s, SERVICE_DEAD);
3627 s->result = SERVICE_SUCCESS;
3628 s->reload_result = SERVICE_SUCCESS;
3631 static int service_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
3632 Service *s = SERVICE(u);
3634 Set *pid_set = NULL;
3638 if (s->main_pid <= 0 && who == KILL_MAIN) {
3639 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3643 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3644 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3648 if (who == KILL_CONTROL || who == KILL_ALL)
3649 if (s->control_pid > 0)
3650 if (kill(s->control_pid, signo) < 0)
3653 if (who == KILL_MAIN || who == KILL_ALL)
3654 if (s->main_pid > 0)
3655 if (kill(s->main_pid, signo) < 0)
3658 if (who == KILL_ALL && mode == KILL_CONTROL_GROUP) {
3661 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
3664 /* Exclude the control/main pid from being killed via the cgroup */
3665 if (s->control_pid > 0)
3666 if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
3671 if (s->main_pid > 0)
3672 if ((q = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0) {
3676 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3678 if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3689 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3690 [SERVICE_DEAD] = "dead",
3691 [SERVICE_START_PRE] = "start-pre",
3692 [SERVICE_START] = "start",
3693 [SERVICE_START_POST] = "start-post",
3694 [SERVICE_RUNNING] = "running",
3695 [SERVICE_EXITED] = "exited",
3696 [SERVICE_RELOAD] = "reload",
3697 [SERVICE_STOP] = "stop",
3698 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3699 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3700 [SERVICE_STOP_POST] = "stop-post",
3701 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3702 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3703 [SERVICE_FAILED] = "failed",
3704 [SERVICE_AUTO_RESTART] = "auto-restart",
3707 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3709 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3710 [SERVICE_RESTART_NO] = "no",
3711 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3712 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3713 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3714 [SERVICE_RESTART_ALWAYS] = "always"
3717 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3719 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3720 [SERVICE_SIMPLE] = "simple",
3721 [SERVICE_FORKING] = "forking",
3722 [SERVICE_ONESHOT] = "oneshot",
3723 [SERVICE_DBUS] = "dbus",
3724 [SERVICE_NOTIFY] = "notify",
3725 [SERVICE_IDLE] = "idle"
3728 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3730 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3731 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3732 [SERVICE_EXEC_START] = "ExecStart",
3733 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3734 [SERVICE_EXEC_RELOAD] = "ExecReload",
3735 [SERVICE_EXEC_STOP] = "ExecStop",
3736 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3739 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3741 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3742 [NOTIFY_NONE] = "none",
3743 [NOTIFY_MAIN] = "main",
3744 [NOTIFY_ALL] = "all"
3747 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3749 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3750 [SERVICE_SUCCESS] = "success",
3751 [SERVICE_FAILURE_RESOURCES] = "resources",
3752 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3753 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3754 [SERVICE_FAILURE_SIGNAL] = "signal",
3755 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3756 [SERVICE_FAILURE_WATCHDOG] = "watchdog"
3759 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3761 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3762 [SERVICE_START_LIMIT_NONE] = "none",
3763 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3764 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3765 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3767 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3769 const UnitVTable service_vtable = {
3770 .suffix = ".service",
3771 .object_size = sizeof(Service),
3777 .init = service_init,
3778 .done = service_done,
3779 .load = service_load,
3781 .coldplug = service_coldplug,
3783 .dump = service_dump,
3785 .start = service_start,
3786 .stop = service_stop,
3787 .reload = service_reload,
3789 .can_reload = service_can_reload,
3791 .kill = service_kill,
3793 .serialize = service_serialize,
3794 .deserialize_item = service_deserialize_item,
3796 .active_state = service_active_state,
3797 .sub_state_to_string = service_sub_state_to_string,
3799 .check_gc = service_check_gc,
3800 .check_snapshot = service_check_snapshot,
3802 .sigchld_event = service_sigchld_event,
3803 .timer_event = service_timer_event,
3804 .fd_event = service_fd_event,
3806 .reset_failed = service_reset_failed,
3808 .cgroup_notify_empty = service_cgroup_notify_event,
3809 .notify_message = service_notify_message,
3811 .bus_name_owner_change = service_bus_name_owner_change,
3812 .bus_query_pid_done = service_bus_query_pid_done,
3814 .bus_interface = "org.freedesktop.systemd1.Service",
3815 .bus_message_handler = bus_service_message_handler,
3816 .bus_invalidating_properties = bus_service_invalidating_properties,
3818 #ifdef HAVE_SYSV_COMPAT
3819 .enumerate = service_enumerate,
3821 .status_message_formats = {
3822 .starting_stopping = {
3823 [0] = "Starting %s...",
3824 [1] = "Stopping %s...",
3826 .finished_start_job = {
3827 [JOB_DONE] = "Started %s.",
3828 [JOB_FAILED] = "Failed to start %s.",
3829 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3830 [JOB_TIMEOUT] = "Timed out starting %s.",
3832 .finished_stop_job = {
3833 [JOB_DONE] = "Stopped %s.",
3834 [JOB_FAILED] = "Stopped (with error) %s.",
3835 [JOB_TIMEOUT] = "Timed out stopping %s.",