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 "unit-printf.h"
37 #include "dbus-service.h"
39 #include "bus-errors.h"
40 #include "exit-status.h"
42 #include "path-util.h"
46 #ifdef HAVE_SYSV_COMPAT
48 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
50 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 },
67 /* Standard SysV runlevels for shutdown */
68 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
69 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
71 /* Note that the order here matters, as we read the
72 directories in this order, and we want to make sure that
73 sysv_start_priority is known when we first load the
74 unit. And that value we only know from S links. Hence
75 UP must be read before DOWN */
78 #define RUNLEVELS_UP "12345"
81 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
82 [SERVICE_DEAD] = UNIT_INACTIVE,
83 [SERVICE_START_PRE] = UNIT_ACTIVATING,
84 [SERVICE_START] = UNIT_ACTIVATING,
85 [SERVICE_START_POST] = UNIT_ACTIVATING,
86 [SERVICE_RUNNING] = UNIT_ACTIVE,
87 [SERVICE_EXITED] = UNIT_ACTIVE,
88 [SERVICE_RELOAD] = UNIT_RELOADING,
89 [SERVICE_STOP] = UNIT_DEACTIVATING,
90 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
91 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
92 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
93 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
94 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
95 [SERVICE_FAILED] = UNIT_FAILED,
96 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
99 /* For Type=idle we never want to delay any other jobs, hence we
100 * consider idle jobs active as soon as we start working on them */
101 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
102 [SERVICE_DEAD] = UNIT_INACTIVE,
103 [SERVICE_START_PRE] = UNIT_ACTIVE,
104 [SERVICE_START] = UNIT_ACTIVE,
105 [SERVICE_START_POST] = UNIT_ACTIVE,
106 [SERVICE_RUNNING] = UNIT_ACTIVE,
107 [SERVICE_EXITED] = UNIT_ACTIVE,
108 [SERVICE_RELOAD] = UNIT_RELOADING,
109 [SERVICE_STOP] = UNIT_DEACTIVATING,
110 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
111 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
112 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
113 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
114 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
115 [SERVICE_FAILED] = UNIT_FAILED,
116 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
119 static void service_init(Unit *u) {
120 Service *s = SERVICE(u);
123 assert(u->load_state == UNIT_STUB);
125 s->timeout_start_usec = DEFAULT_TIMEOUT_USEC;
126 s->timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
127 s->restart_usec = DEFAULT_RESTART_USEC;
128 s->type = _SERVICE_TYPE_INVALID;
130 watch_init(&s->watchdog_watch);
131 watch_init(&s->timer_watch);
133 #ifdef HAVE_SYSV_COMPAT
134 s->sysv_start_priority = -1;
135 s->sysv_start_priority_from_rcnd = -1;
138 s->guess_main_pid = true;
140 exec_context_init(&s->exec_context);
141 kill_context_init(&s->kill_context);
143 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
145 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
148 static void service_unwatch_control_pid(Service *s) {
151 if (s->control_pid <= 0)
154 unit_unwatch_pid(UNIT(s), s->control_pid);
158 static void service_unwatch_main_pid(Service *s) {
161 if (s->main_pid <= 0)
164 unit_unwatch_pid(UNIT(s), s->main_pid);
168 static void service_unwatch_pid_file(Service *s) {
169 if (!s->pid_file_pathspec)
172 log_debug("Stopping watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
173 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
174 path_spec_done(s->pid_file_pathspec);
175 free(s->pid_file_pathspec);
176 s->pid_file_pathspec = NULL;
179 static int service_set_main_pid(Service *s, pid_t pid) {
191 s->main_pid_known = true;
193 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
194 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
195 UNIT(s)->id, (unsigned long) pid);
197 s->main_pid_alien = true;
199 s->main_pid_alien = false;
201 exec_status_start(&s->main_exec_status, pid);
206 static void service_close_socket_fd(Service *s) {
209 if (s->socket_fd < 0)
212 close_nointr_nofail(s->socket_fd);
216 static void service_connection_unref(Service *s) {
219 if (!UNIT_DEREF(s->accept_socket))
222 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
223 unit_ref_unset(&s->accept_socket);
226 static void service_stop_watchdog(Service *s) {
229 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
230 s->watchdog_timestamp.realtime = 0;
231 s->watchdog_timestamp.monotonic = 0;
234 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart);
236 static void service_handle_watchdog(Service *s) {
242 if (s->watchdog_usec == 0)
245 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
246 if (offset >= s->watchdog_usec) {
247 log_error("%s watchdog timeout!", UNIT(s)->id);
248 service_enter_dead(s, SERVICE_FAILURE_WATCHDOG, true);
252 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->watchdog_usec - offset, &s->watchdog_watch);
254 log_warning("%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
257 static void service_reset_watchdog(Service *s) {
260 dual_timestamp_get(&s->watchdog_timestamp);
261 service_handle_watchdog(s);
264 static void service_done(Unit *u) {
265 Service *s = SERVICE(u);
272 #ifdef HAVE_SYSV_COMPAT
273 free(s->sysv_runlevels);
274 s->sysv_runlevels = NULL;
277 free(s->status_text);
278 s->status_text = NULL;
280 exec_context_done(&s->exec_context);
281 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
282 s->control_command = NULL;
283 s->main_command = NULL;
285 set_free(s->restart_ignore_status.code);
286 s->restart_ignore_status.code = NULL;
287 set_free(s->restart_ignore_status.signal);
288 s->restart_ignore_status.signal = NULL;
290 set_free(s->success_status.code);
291 s->success_status.code = NULL;
292 set_free(s->success_status.signal);
293 s->success_status.signal = NULL;
295 /* This will leak a process, but at least no memory or any of
297 service_unwatch_main_pid(s);
298 service_unwatch_control_pid(s);
299 service_unwatch_pid_file(s);
302 unit_unwatch_bus_name(u, s->bus_name);
307 service_close_socket_fd(s);
308 service_connection_unref(s);
310 unit_ref_unset(&s->accept_socket);
312 service_stop_watchdog(s);
314 unit_unwatch_timer(u, &s->timer_watch);
317 #ifdef HAVE_SYSV_COMPAT
318 static char *sysv_translate_name(const char *name) {
321 if (!(r = new(char, strlen(name) + sizeof(".service"))))
324 if (endswith(name, ".sh"))
325 /* Drop Debian-style .sh suffix */
326 strcpy(stpcpy(r, name) - 3, ".service");
327 if (startswith(name, "rc."))
328 /* Drop Frugalware-style rc. prefix */
329 strcpy(stpcpy(r, name + 3), ".service");
331 /* Normal init scripts */
332 strcpy(stpcpy(r, name), ".service");
337 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
339 /* We silently ignore the $ prefix here. According to the LSB
340 * spec it simply indicates whether something is a
341 * standardized name or a distribution-specific one. Since we
342 * just follow what already exists and do not introduce new
343 * uses or names we don't care who introduced a new name. */
345 static const char * const table[] = {
346 /* LSB defined facilities */
347 "local_fs", SPECIAL_LOCAL_FS_TARGET,
348 /* Due to unfortunate name selection in Mandriva,
349 * $network is provided by network-up which is ordered
350 * after network which actually starts interfaces.
351 * To break the loop, just ignore it */
352 "network", SPECIAL_NETWORK_TARGET,
353 "named", SPECIAL_NSS_LOOKUP_TARGET,
354 "portmap", SPECIAL_RPCBIND_TARGET,
355 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
356 "syslog", SPECIAL_SYSLOG_TARGET,
357 "time", SPECIAL_TIME_SYNC_TARGET,
359 /* common extensions */
360 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
361 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
363 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
364 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
374 n = *name == '$' ? name + 1 : name;
376 for (i = 0; i < ELEMENTSOF(table); i += 2) {
378 if (!streq(table[i], n))
384 if (!(r = strdup(table[i+1])))
390 /* If we don't know this name, fallback heuristics to figure
391 * out whether something is a target or a service alias. */
394 if (!unit_prefix_is_valid(n))
397 /* Facilities starting with $ are most likely targets */
398 r = unit_name_build(n, NULL, ".target");
399 } else if (filename && streq(name, filename))
400 /* Names equaling the file name of the services are redundant */
403 /* Everything else we assume to be normal service names */
404 r = sysv_translate_name(n);
415 static int sysv_fix_order(Service *s) {
421 if (s->sysv_start_priority < 0)
424 /* For each pair of services where at least one lacks a LSB
425 * header, we use the start priority value to order things. */
427 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
430 bool special_s, special_t;
437 if (UNIT(t)->load_state != UNIT_LOADED)
440 if (t->sysv_start_priority < 0)
443 /* If both units have modern headers we don't care
444 * about the priorities */
445 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
446 (UNIT(t)->fragment_path || t->sysv_has_lsb))
449 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
450 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
452 if (special_t && !special_s)
454 else if (special_s && !special_t)
456 else if (t->sysv_start_priority < s->sysv_start_priority)
458 else if (t->sysv_start_priority > s->sysv_start_priority)
463 /* FIXME: Maybe we should compare the name here lexicographically? */
465 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
472 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
475 if (!(c = new0(ExecCommand, 1)))
478 if (!(c->path = strdup(path))) {
483 if (!(c->argv = strv_new(path, arg1, NULL))) {
492 static int sysv_exec_commands(Service *s, const bool supports_reload) {
497 assert(UNIT(s)->source_path);
499 c = exec_command_new(UNIT(s)->source_path, "start");
502 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
504 c = exec_command_new(UNIT(s)->source_path, "stop");
507 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
509 if (supports_reload) {
510 c = exec_command_new(UNIT(s)->source_path, "reload");
513 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
519 static bool usage_contains_reload(const char *line) {
520 return (strcasestr(line, "{reload|") ||
521 strcasestr(line, "{reload}") ||
522 strcasestr(line, "{reload\"") ||
523 strcasestr(line, "|reload|") ||
524 strcasestr(line, "|reload}") ||
525 strcasestr(line, "|reload\""));
528 static int service_load_sysv_path(Service *s, const char *path) {
540 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
542 bool supports_reload = false;
549 f = fopen(path, "re");
551 r = errno == ENOENT ? 0 : -errno;
555 if (fstat(fileno(f), &st) < 0) {
560 free(u->source_path);
561 u->source_path = strdup(path);
562 if (!u->source_path) {
566 u->source_mtime = timespec_load(&st.st_mtim);
568 if (null_or_empty(&st)) {
569 u->load_state = UNIT_MASKED;
577 char l[LINE_MAX], *t;
579 if (!fgets(l, sizeof(l), f)) {
584 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
592 /* Try to figure out whether this init script supports
593 * the reload operation. This heuristic looks for
594 * "Usage" lines which include the reload option. */
595 if ( state == USAGE_CONTINUATION ||
596 (state == NORMAL && strcasestr(t, "usage"))) {
597 if (usage_contains_reload(t)) {
598 supports_reload = true;
600 } else if (t[strlen(t)-1] == '\\')
601 state = USAGE_CONTINUATION;
609 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
611 s->sysv_has_lsb = true;
615 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
621 t += strspn(t, WHITESPACE);
623 if (state == NORMAL) {
625 /* Try to parse Red Hat style chkconfig headers */
627 if (startswith_no_case(t, "chkconfig:")) {
629 char runlevels[16], *k;
633 if (sscanf(t+10, "%15s %i %*i",
635 &start_priority) != 2) {
637 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
641 /* A start priority gathered from the
642 * symlink farms is preferred over the
643 * data from the LSB header. */
644 if (start_priority < 0 || start_priority > 99)
645 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
647 s->sysv_start_priority = start_priority;
649 char_array_0(runlevels);
650 k = delete_chars(runlevels, WHITESPACE "-");
655 if (!(d = strdup(k))) {
660 free(s->sysv_runlevels);
661 s->sysv_runlevels = d;
664 } else if (startswith_no_case(t, "description:")) {
666 size_t k = strlen(t);
670 if (t[k-1] == '\\') {
675 if ((j = strstrip(t+12)) && *j) {
676 if (!(d = strdup(j))) {
683 free(chkconfig_description);
684 chkconfig_description = d;
686 } else if (startswith_no_case(t, "pidfile:")) {
693 if (!path_is_absolute(fn)) {
694 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
698 if (!(fn = strdup(fn))) {
707 } else if (state == DESCRIPTION) {
709 /* Try to parse Red Hat style description
712 size_t k = strlen(t);
720 if ((j = strstrip(t)) && *j) {
723 if (chkconfig_description)
724 d = strjoin(chkconfig_description, " ", j, NULL);
733 free(chkconfig_description);
734 chkconfig_description = d;
737 } else if (state == LSB || state == LSB_DESCRIPTION) {
739 if (startswith_no_case(t, "Provides:")) {
745 FOREACH_WORD_QUOTED(w, z, t+9, i) {
748 if (!(n = strndup(w, z))) {
753 r = sysv_translate_facility(n, path_get_file_name(path), &m);
762 if (unit_name_to_type(m) == UNIT_SERVICE)
763 r = unit_add_name(u, m);
770 * indication that the
772 * now available. This
775 * targets do NOT pull
778 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
781 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
786 } else if (startswith_no_case(t, "Required-Start:") ||
787 startswith_no_case(t, "Should-Start:") ||
788 startswith_no_case(t, "X-Start-Before:") ||
789 startswith_no_case(t, "X-Start-After:")) {
795 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
798 if (!(n = strndup(w, z))) {
803 r = sysv_translate_facility(n, path_get_file_name(path), &m);
806 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
816 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
819 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
823 } else if (startswith_no_case(t, "Default-Start:")) {
828 k = delete_chars(t+14, WHITESPACE "-");
831 if (!(d = strdup(k))) {
836 free(s->sysv_runlevels);
837 s->sysv_runlevels = d;
840 } else if (startswith_no_case(t, "Description:")) {
843 state = LSB_DESCRIPTION;
845 if ((j = strstrip(t+12)) && *j) {
846 if (!(d = strdup(j))) {
853 free(long_description);
854 long_description = d;
856 } else if (startswith_no_case(t, "Short-Description:")) {
861 if ((j = strstrip(t+18)) && *j) {
862 if (!(d = strdup(j))) {
869 free(short_description);
870 short_description = d;
872 } else if (state == LSB_DESCRIPTION) {
874 if (startswith(l, "#\t") || startswith(l, "# ")) {
877 if ((j = strstrip(t)) && *j) {
880 if (long_description)
881 d = strjoin(long_description, " ", t, NULL);
890 free(long_description);
891 long_description = d;
900 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
903 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
904 /* If there a runlevels configured for this service
905 * but none of the standard ones, then we assume this
906 * is some special kind of service (which might be
907 * needed for early boot) and don't create any links
910 UNIT(s)->default_dependencies = false;
912 /* Don't timeout special services during boot (like fsck) */
913 s->timeout_start_usec = 0;
914 s->timeout_stop_usec = 0;
916 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
917 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
920 /* Special setting for all SysV services */
921 s->type = SERVICE_FORKING;
922 s->remain_after_exit = !s->pid_file;
923 s->guess_main_pid = false;
924 s->restart = SERVICE_RESTART_NO;
925 s->exec_context.ignore_sigpipe = false;
926 s->kill_context.kill_mode = KILL_PROCESS;
928 /* We use the long description only if
929 * no short description is set. */
931 if (short_description)
932 description = short_description;
933 else if (chkconfig_description)
934 description = chkconfig_description;
935 else if (long_description)
936 description = long_description;
943 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
951 /* The priority that has been set in /etc/rcN.d/ hierarchies
952 * takes precedence over what is stored as default in the LSB
954 if (s->sysv_start_priority_from_rcnd >= 0)
955 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
957 u->load_state = UNIT_LOADED;
965 free(short_description);
966 free(long_description);
967 free(chkconfig_description);
972 static int service_load_sysv_name(Service *s, const char *name) {
978 /* For SysV services we strip the rc.* and *.sh
979 * prefixes/suffixes. */
980 if (startswith(name, "rc.") ||
981 endswith(name, ".sh.service"))
984 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
988 path = strjoin(*p, "/", name, NULL);
992 assert(endswith(path, ".service"));
993 path[strlen(path)-8] = 0;
995 r = service_load_sysv_path(s, path);
997 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
998 /* Try Debian style *.sh source'able init scripts */
1000 r = service_load_sysv_path(s, path);
1004 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1005 /* Try Frugalware style rc.* init scripts */
1007 path = strjoin(*p, "/rc.", name, NULL);
1011 /* Drop .service suffix */
1012 path[strlen(path)-8] = 0;
1013 r = service_load_sysv_path(s, path);
1020 if (UNIT(s)->load_state != UNIT_STUB)
1027 static int service_load_sysv(Service *s) {
1034 /* Load service data from SysV init scripts, preferably with
1035 * LSB headers ... */
1037 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1040 if ((t = UNIT(s)->id))
1041 if ((r = service_load_sysv_name(s, t)) < 0)
1044 if (UNIT(s)->load_state == UNIT_STUB)
1045 SET_FOREACH(t, UNIT(s)->names, i) {
1046 if (t == UNIT(s)->id)
1049 if ((r = service_load_sysv_name(s, t)) < 0)
1052 if (UNIT(s)->load_state != UNIT_STUB)
1060 static int fsck_fix_order(Service *s) {
1066 if (s->fsck_passno <= 0)
1069 /* For each pair of services where both have an fsck priority
1070 * we order things based on it. */
1072 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1081 if (UNIT(t)->load_state != UNIT_LOADED)
1084 if (t->fsck_passno <= 0)
1087 if (t->fsck_passno < s->fsck_passno)
1089 else if (t->fsck_passno > s->fsck_passno)
1094 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1101 static int service_verify(Service *s) {
1104 if (UNIT(s)->load_state != UNIT_LOADED)
1107 if (!s->exec_command[SERVICE_EXEC_START]) {
1108 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1112 if (s->type != SERVICE_ONESHOT &&
1113 s->exec_command[SERVICE_EXEC_START]->command_next) {
1114 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1118 if (s->type == SERVICE_DBUS && !s->bus_name) {
1119 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1123 if (s->bus_name && s->type != SERVICE_DBUS)
1124 log_warning("%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1126 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1127 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1134 static int service_add_default_dependencies(Service *s) {
1139 /* Add a number of automatic dependencies useful for the
1140 * majority of services. */
1142 /* First, pull in base system */
1143 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1145 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1148 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1150 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1154 /* Second, activate normal shutdown */
1155 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1158 static void service_fix_output(Service *s) {
1161 /* If nothing has been explicitly configured, patch default
1162 * output in. If input is socket/tty we avoid this however,
1163 * since in that case we want output to default to the same
1164 * place as we read input from. */
1166 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1167 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1168 s->exec_context.std_input == EXEC_INPUT_NULL)
1169 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1171 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1172 s->exec_context.std_input == EXEC_INPUT_NULL)
1173 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1176 static int service_load(Unit *u) {
1178 Service *s = SERVICE(u);
1182 /* Load a .service file */
1183 if ((r = unit_load_fragment(u)) < 0)
1186 #ifdef HAVE_SYSV_COMPAT
1187 /* Load a classic init script as a fallback, if we couldn't find anything */
1188 if (u->load_state == UNIT_STUB)
1189 if ((r = service_load_sysv(s)) < 0)
1193 /* Still nothing found? Then let's give up */
1194 if (u->load_state == UNIT_STUB)
1197 /* We were able to load something, then let's add in the
1198 * dropin directories. */
1199 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1202 /* This is a new unit? Then let's add in some extras */
1203 if (u->load_state == UNIT_LOADED) {
1204 if (s->type == _SERVICE_TYPE_INVALID)
1205 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1207 /* Oneshot services have disabled start timeout by default */
1208 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1209 s->timeout_start_usec = 0;
1211 service_fix_output(s);
1213 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1216 if ((r = unit_add_default_cgroups(u)) < 0)
1219 #ifdef HAVE_SYSV_COMPAT
1220 if ((r = sysv_fix_order(s)) < 0)
1224 if ((r = fsck_fix_order(s)) < 0)
1228 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1231 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1232 s->notify_access = NOTIFY_MAIN;
1234 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1235 s->notify_access = NOTIFY_MAIN;
1237 if (s->type == SERVICE_DBUS || s->bus_name)
1238 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1241 if (UNIT(s)->default_dependencies)
1242 if ((r = service_add_default_dependencies(s)) < 0)
1245 r = unit_exec_context_defaults(u, &s->exec_context);
1250 return service_verify(s);
1253 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1255 ServiceExecCommand c;
1256 Service *s = SERVICE(u);
1257 const char *prefix2;
1262 p2 = strappend(prefix, "\t");
1263 prefix2 = p2 ? p2 : prefix;
1266 "%sService State: %s\n"
1268 "%sReload Result: %s\n"
1269 "%sPermissionsStartOnly: %s\n"
1270 "%sRootDirectoryStartOnly: %s\n"
1271 "%sRemainAfterExit: %s\n"
1272 "%sGuessMainPID: %s\n"
1275 "%sNotifyAccess: %s\n",
1276 prefix, service_state_to_string(s->state),
1277 prefix, service_result_to_string(s->result),
1278 prefix, service_result_to_string(s->reload_result),
1279 prefix, yes_no(s->permissions_start_only),
1280 prefix, yes_no(s->root_directory_start_only),
1281 prefix, yes_no(s->remain_after_exit),
1282 prefix, yes_no(s->guess_main_pid),
1283 prefix, service_type_to_string(s->type),
1284 prefix, service_restart_to_string(s->restart),
1285 prefix, notify_access_to_string(s->notify_access));
1287 if (s->control_pid > 0)
1289 "%sControl PID: %lu\n",
1290 prefix, (unsigned long) s->control_pid);
1292 if (s->main_pid > 0)
1295 "%sMain PID Known: %s\n"
1296 "%sMain PID Alien: %s\n",
1297 prefix, (unsigned long) s->main_pid,
1298 prefix, yes_no(s->main_pid_known),
1299 prefix, yes_no(s->main_pid_alien));
1304 prefix, s->pid_file);
1309 "%sBus Name Good: %s\n",
1310 prefix, s->bus_name,
1311 prefix, yes_no(s->bus_name_good));
1313 kill_context_dump(&s->kill_context, f, prefix);
1314 exec_context_dump(&s->exec_context, f, prefix);
1316 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1318 if (!s->exec_command[c])
1321 fprintf(f, "%s-> %s:\n",
1322 prefix, service_exec_command_to_string(c));
1324 exec_command_dump_list(s->exec_command[c], f, prefix2);
1327 #ifdef HAVE_SYSV_COMPAT
1330 "%sSysV Init Script has LSB Header: %s\n"
1331 "%sSysVEnabled: %s\n",
1332 prefix, yes_no(s->sysv_has_lsb),
1333 prefix, yes_no(s->sysv_enabled));
1335 if (s->sysv_start_priority >= 0)
1337 "%sSysVStartPriority: %i\n",
1338 prefix, s->sysv_start_priority);
1340 if (s->sysv_runlevels)
1341 fprintf(f, "%sSysVRunLevels: %s\n",
1342 prefix, s->sysv_runlevels);
1345 if (s->fsck_passno > 0)
1347 "%sFsckPassNo: %i\n",
1348 prefix, s->fsck_passno);
1351 fprintf(f, "%sStatus Text: %s\n",
1352 prefix, s->status_text);
1357 static int service_load_pid_file(Service *s, bool may_warn) {
1367 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1369 log_info("PID file %s not readable (yet?) after %s.",
1370 s->pid_file, service_state_to_string(s->state));
1374 r = parse_pid(k, &pid);
1380 if (kill(pid, 0) < 0 && errno != EPERM) {
1382 log_info("PID %lu read from file %s does not exist.",
1383 (unsigned long) pid, s->pid_file);
1387 if (s->main_pid_known) {
1388 if (pid == s->main_pid)
1391 log_debug("Main PID changing: %lu -> %lu",
1392 (unsigned long) s->main_pid, (unsigned long) pid);
1393 service_unwatch_main_pid(s);
1394 s->main_pid_known = false;
1396 log_debug("Main PID loaded: %lu", (unsigned long) pid);
1398 if ((r = service_set_main_pid(s, pid)) < 0)
1401 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1402 /* FIXME: we need to do something here */
1408 static int service_search_main_pid(Service *s) {
1414 /* If we know it anyway, don't ever fallback to unreliable
1416 if (s->main_pid_known)
1419 if (!s->guess_main_pid)
1422 assert(s->main_pid <= 0);
1424 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1427 log_debug("Main PID guessed: %lu", (unsigned long) pid);
1428 if ((r = service_set_main_pid(s, pid)) < 0)
1431 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1432 /* FIXME: we need to do something here */
1438 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1444 /* Notifies all our sockets when we die */
1446 if (s->socket_fd >= 0)
1449 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1450 if (u->type == UNIT_SOCKET)
1451 socket_notify_service_dead(SOCKET(u), failed_permanent);
1456 static void service_set_state(Service *s, ServiceState state) {
1457 ServiceState old_state;
1458 const UnitActiveState *table;
1461 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1463 old_state = s->state;
1466 service_unwatch_pid_file(s);
1468 if (state != SERVICE_START_PRE &&
1469 state != SERVICE_START &&
1470 state != SERVICE_START_POST &&
1471 state != SERVICE_RELOAD &&
1472 state != SERVICE_STOP &&
1473 state != SERVICE_STOP_SIGTERM &&
1474 state != SERVICE_STOP_SIGKILL &&
1475 state != SERVICE_STOP_POST &&
1476 state != SERVICE_FINAL_SIGTERM &&
1477 state != SERVICE_FINAL_SIGKILL &&
1478 state != SERVICE_AUTO_RESTART)
1479 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1481 if (state != SERVICE_START &&
1482 state != SERVICE_START_POST &&
1483 state != SERVICE_RUNNING &&
1484 state != SERVICE_RELOAD &&
1485 state != SERVICE_STOP &&
1486 state != SERVICE_STOP_SIGTERM &&
1487 state != SERVICE_STOP_SIGKILL) {
1488 service_unwatch_main_pid(s);
1489 s->main_command = NULL;
1492 if (state != SERVICE_START_PRE &&
1493 state != SERVICE_START &&
1494 state != SERVICE_START_POST &&
1495 state != SERVICE_RELOAD &&
1496 state != SERVICE_STOP &&
1497 state != SERVICE_STOP_SIGTERM &&
1498 state != SERVICE_STOP_SIGKILL &&
1499 state != SERVICE_STOP_POST &&
1500 state != SERVICE_FINAL_SIGTERM &&
1501 state != SERVICE_FINAL_SIGKILL) {
1502 service_unwatch_control_pid(s);
1503 s->control_command = NULL;
1504 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1507 if (state == SERVICE_DEAD ||
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_FAILED ||
1515 state == SERVICE_AUTO_RESTART)
1516 service_notify_sockets_dead(s, false);
1518 if (state != SERVICE_START_PRE &&
1519 state != SERVICE_START &&
1520 state != SERVICE_START_POST &&
1521 state != SERVICE_RUNNING &&
1522 state != SERVICE_RELOAD &&
1523 state != SERVICE_STOP &&
1524 state != SERVICE_STOP_SIGTERM &&
1525 state != SERVICE_STOP_SIGKILL &&
1526 state != SERVICE_STOP_POST &&
1527 state != SERVICE_FINAL_SIGTERM &&
1528 state != SERVICE_FINAL_SIGKILL &&
1529 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1530 service_close_socket_fd(s);
1531 service_connection_unref(s);
1534 if (state == SERVICE_STOP)
1535 service_stop_watchdog(s);
1537 /* For the inactive states unit_notify() will trim the cgroup,
1538 * but for exit we have to do that ourselves... */
1539 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1540 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1542 if (old_state != state)
1543 log_struct(LOG_DEBUG,
1544 "UNIT=%s", UNIT(s)->id,
1545 "MESSAGE=%s changed %s -> %s", UNIT(s)->id,
1546 service_state_to_string(old_state),
1547 service_state_to_string(state),
1550 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1551 s->reload_result = SERVICE_SUCCESS;
1554 static int service_coldplug(Unit *u) {
1555 Service *s = SERVICE(u);
1559 assert(s->state == SERVICE_DEAD);
1561 if (s->deserialized_state != s->state) {
1563 if (s->deserialized_state == SERVICE_START_PRE ||
1564 s->deserialized_state == SERVICE_START ||
1565 s->deserialized_state == SERVICE_START_POST ||
1566 s->deserialized_state == SERVICE_RELOAD ||
1567 s->deserialized_state == SERVICE_STOP ||
1568 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1569 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1570 s->deserialized_state == SERVICE_STOP_POST ||
1571 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1572 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1573 s->deserialized_state == SERVICE_AUTO_RESTART) {
1574 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1577 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1579 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
1585 if ((s->deserialized_state == SERVICE_START &&
1586 (s->type == SERVICE_FORKING ||
1587 s->type == SERVICE_DBUS ||
1588 s->type == SERVICE_ONESHOT ||
1589 s->type == SERVICE_NOTIFY)) ||
1590 s->deserialized_state == SERVICE_START_POST ||
1591 s->deserialized_state == SERVICE_RUNNING ||
1592 s->deserialized_state == SERVICE_RELOAD ||
1593 s->deserialized_state == SERVICE_STOP ||
1594 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1595 s->deserialized_state == SERVICE_STOP_SIGKILL)
1596 if (s->main_pid > 0)
1597 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1600 if (s->deserialized_state == SERVICE_START_PRE ||
1601 s->deserialized_state == SERVICE_START ||
1602 s->deserialized_state == SERVICE_START_POST ||
1603 s->deserialized_state == SERVICE_RELOAD ||
1604 s->deserialized_state == SERVICE_STOP ||
1605 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1606 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1607 s->deserialized_state == SERVICE_STOP_POST ||
1608 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1609 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1610 if (s->control_pid > 0)
1611 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1614 if (s->deserialized_state == SERVICE_START_POST ||
1615 s->deserialized_state == SERVICE_RUNNING)
1616 service_handle_watchdog(s);
1618 service_set_state(s, s->deserialized_state);
1623 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1627 unsigned rn_fds = 0;
1634 if (s->socket_fd >= 0)
1637 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1642 if (u->type != UNIT_SOCKET)
1647 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1659 if (!(t = new(int, rn_fds+cn_fds))) {
1665 memcpy(t, rfds, rn_fds * sizeof(int));
1666 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1671 rn_fds = rn_fds+cn_fds;
1686 static int service_spawn(
1691 bool apply_permissions,
1693 bool apply_tty_stdin,
1694 bool set_notify_socket,
1700 int *fds = NULL, *fdsbuf = NULL;
1701 unsigned n_fds = 0, n_env = 0;
1702 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1709 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1710 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1711 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1713 if (s->socket_fd >= 0) {
1714 fds = &s->socket_fd;
1717 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1724 if (timeout && s->timeout_start_usec) {
1725 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_start_usec, &s->timer_watch);
1729 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1731 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1736 our_env = new0(char*, 5);
1742 if (set_notify_socket)
1743 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1748 if (s->main_pid > 0)
1749 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1754 if (s->watchdog_usec > 0)
1755 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1760 if (s->meta.manager->running_as != SYSTEMD_SYSTEM)
1761 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1766 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1780 UNIT(s)->manager->confirm_spawn,
1781 UNIT(s)->cgroup_bondings,
1782 UNIT(s)->cgroup_attributes,
1783 is_control ? "control" : NULL,
1785 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1791 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1792 /* FIXME: we need to do something here */
1798 strv_free(final_env);
1808 strv_free(final_env);
1811 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1816 static int main_pid_good(Service *s) {
1819 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1822 /* If we know the pid file, then lets just check if it is
1824 if (s->main_pid_known) {
1826 /* If it's an alien child let's check if it is still
1828 if (s->main_pid_alien)
1829 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1831 /* .. otherwise assume we'll get a SIGCHLD for it,
1832 * which we really should wait for to collect exit
1833 * status and code */
1834 return s->main_pid > 0;
1837 /* We don't know the pid */
1841 static int control_pid_good(Service *s) {
1844 return s->control_pid > 0;
1847 static int cgroup_good(Service *s) {
1852 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1858 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1862 if (f != SERVICE_SUCCESS)
1865 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
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))) &&
1874 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1875 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1876 (s->result != SERVICE_FAILURE_SIGNAL ||
1877 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1880 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
1884 service_set_state(s, SERVICE_AUTO_RESTART);
1887 s->forbid_restart = false;
1892 log_warning("%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1893 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1896 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1898 static void service_enter_stop_post(Service *s, ServiceResult f) {
1902 if (f != SERVICE_SUCCESS)
1905 service_unwatch_control_pid(s);
1907 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1908 s->control_command_id = SERVICE_EXEC_STOP_POST;
1910 r = service_spawn(s,
1914 !s->permissions_start_only,
1915 !s->root_directory_start_only,
1924 service_set_state(s, SERVICE_STOP_POST);
1926 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1931 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
1932 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1935 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1937 Set *pid_set = NULL;
1938 bool wait_for_exit = false;
1942 if (f != SERVICE_SUCCESS)
1945 if (s->kill_context.kill_mode != KILL_NONE) {
1946 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->kill_context.kill_signal : SIGKILL;
1948 if (s->main_pid > 0) {
1949 if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
1950 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1952 wait_for_exit = !s->main_pid_alien;
1955 if (s->control_pid > 0) {
1956 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1957 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1959 wait_for_exit = true;
1962 if (s->kill_context.kill_mode == KILL_CONTROL_GROUP) {
1964 pid_set = set_new(trivial_hash_func, trivial_compare_func);
1970 /* Exclude the main/control pids from being killed via the cgroup */
1971 if (s->main_pid > 0)
1972 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1975 if (s->control_pid > 0)
1976 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1979 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
1981 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1982 log_warning("Failed to kill control group: %s", strerror(-r));
1984 wait_for_exit = true;
1991 if (wait_for_exit) {
1992 if (s->timeout_stop_usec > 0) {
1993 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_stop_usec, &s->timer_watch);
1998 service_set_state(s, state);
1999 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2000 service_enter_stop_post(s, SERVICE_SUCCESS);
2002 service_enter_dead(s, SERVICE_SUCCESS, true);
2007 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2009 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2010 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2012 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2018 static void service_enter_stop(Service *s, ServiceResult f) {
2023 if (f != SERVICE_SUCCESS)
2026 service_unwatch_control_pid(s);
2028 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2029 s->control_command_id = SERVICE_EXEC_STOP;
2031 r = service_spawn(s,
2035 !s->permissions_start_only,
2036 !s->root_directory_start_only,
2044 service_set_state(s, SERVICE_STOP);
2046 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2051 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2052 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2055 static void service_enter_running(Service *s, ServiceResult f) {
2056 int main_pid_ok, cgroup_ok;
2059 if (f != SERVICE_SUCCESS)
2062 main_pid_ok = main_pid_good(s);
2063 cgroup_ok = cgroup_good(s);
2065 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2066 (s->bus_name_good || s->type != SERVICE_DBUS))
2067 service_set_state(s, SERVICE_RUNNING);
2068 else if (s->remain_after_exit)
2069 service_set_state(s, SERVICE_EXITED);
2071 service_enter_stop(s, SERVICE_SUCCESS);
2074 static void service_enter_start_post(Service *s) {
2078 service_unwatch_control_pid(s);
2080 if (s->watchdog_usec > 0)
2081 service_reset_watchdog(s);
2083 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2084 s->control_command_id = SERVICE_EXEC_START_POST;
2086 r = service_spawn(s,
2090 !s->permissions_start_only,
2091 !s->root_directory_start_only,
2099 service_set_state(s, SERVICE_START_POST);
2101 service_enter_running(s, SERVICE_SUCCESS);
2106 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2107 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2110 static void service_enter_start(Service *s) {
2117 assert(s->exec_command[SERVICE_EXEC_START]);
2118 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2120 if (s->type == SERVICE_FORKING)
2121 service_unwatch_control_pid(s);
2123 service_unwatch_main_pid(s);
2125 /* We want to ensure that nobody leaks processes from
2126 * START_PRE here, so let's go on a killing spree, People
2127 * should not spawn long running processes from START_PRE. */
2128 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2130 if (s->type == SERVICE_FORKING) {
2131 s->control_command_id = SERVICE_EXEC_START;
2132 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2134 s->main_command = NULL;
2136 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2137 s->control_command = NULL;
2139 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2142 r = service_spawn(s,
2144 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2149 s->notify_access != NOTIFY_NONE,
2155 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2156 /* For simple services we immediately start
2157 * the START_POST binaries. */
2159 service_set_main_pid(s, pid);
2160 service_enter_start_post(s);
2162 } else if (s->type == SERVICE_FORKING) {
2164 /* For forking services we wait until the start
2165 * process exited. */
2167 s->control_pid = pid;
2168 service_set_state(s, SERVICE_START);
2170 } else if (s->type == SERVICE_ONESHOT ||
2171 s->type == SERVICE_DBUS ||
2172 s->type == SERVICE_NOTIFY) {
2174 /* For oneshot services we wait until the start
2175 * process exited, too, but it is our main process. */
2177 /* For D-Bus services we know the main pid right away,
2178 * but wait for the bus name to appear on the
2179 * bus. Notify services are similar. */
2181 service_set_main_pid(s, pid);
2182 service_set_state(s, SERVICE_START);
2184 assert_not_reached("Unknown service type");
2189 log_warning("%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2190 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2193 static void service_enter_start_pre(Service *s) {
2198 service_unwatch_control_pid(s);
2200 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2202 /* Before we start anything, let's clear up what might
2203 * be left from previous runs. */
2204 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2206 s->control_command_id = SERVICE_EXEC_START_PRE;
2208 r = service_spawn(s,
2212 !s->permissions_start_only,
2213 !s->root_directory_start_only,
2221 service_set_state(s, SERVICE_START_PRE);
2223 service_enter_start(s);
2228 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2229 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2232 static void service_enter_restart(Service *s) {
2237 dbus_error_init(&error);
2239 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2240 /* Don't restart things if we are going down anyway */
2241 log_info("Stop job pending for unit, delaying automatic restart.");
2243 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
2250 /* Any units that are bound to this service must also be
2251 * restarted. We use JOB_RESTART (instead of the more obvious
2252 * JOB_START) here so that those dependency jobs will be added
2254 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2258 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2259 * it will be canceled as part of the service_stop() call that
2260 * is executed as part of JOB_RESTART. */
2262 log_debug("%s scheduled restart job.", UNIT(s)->id);
2266 log_warning("%s failed to schedule restart job: %s", UNIT(s)->id, bus_error(&error, -r));
2267 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2269 dbus_error_free(&error);
2272 static void service_enter_reload(Service *s) {
2277 service_unwatch_control_pid(s);
2279 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2280 s->control_command_id = SERVICE_EXEC_RELOAD;
2282 r = service_spawn(s,
2286 !s->permissions_start_only,
2287 !s->root_directory_start_only,
2295 service_set_state(s, SERVICE_RELOAD);
2297 service_enter_running(s, SERVICE_SUCCESS);
2302 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
2303 s->reload_result = SERVICE_FAILURE_RESOURCES;
2304 service_enter_running(s, SERVICE_SUCCESS);
2307 static void service_run_next_control(Service *s) {
2311 assert(s->control_command);
2312 assert(s->control_command->command_next);
2314 assert(s->control_command_id != SERVICE_EXEC_START);
2316 s->control_command = s->control_command->command_next;
2317 service_unwatch_control_pid(s);
2319 r = service_spawn(s,
2323 !s->permissions_start_only,
2324 !s->root_directory_start_only,
2325 s->control_command_id == SERVICE_EXEC_START_PRE ||
2326 s->control_command_id == SERVICE_EXEC_STOP_POST,
2336 log_warning("%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
2338 if (s->state == SERVICE_START_PRE)
2339 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2340 else if (s->state == SERVICE_STOP)
2341 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2342 else if (s->state == SERVICE_STOP_POST)
2343 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2344 else if (s->state == SERVICE_RELOAD) {
2345 s->reload_result = SERVICE_FAILURE_RESOURCES;
2346 service_enter_running(s, SERVICE_SUCCESS);
2348 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2351 static void service_run_next_main(Service *s) {
2356 assert(s->main_command);
2357 assert(s->main_command->command_next);
2358 assert(s->type == SERVICE_ONESHOT);
2360 s->main_command = s->main_command->command_next;
2361 service_unwatch_main_pid(s);
2363 r = service_spawn(s,
2370 s->notify_access != NOTIFY_NONE,
2376 service_set_main_pid(s, pid);
2381 log_warning("%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2382 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2385 static int service_start_limit_test(Service *s) {
2388 if (ratelimit_test(&s->start_limit))
2391 switch (s->start_limit_action) {
2393 case SERVICE_START_LIMIT_NONE:
2394 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s)->id);
2397 case SERVICE_START_LIMIT_REBOOT: {
2401 dbus_error_init(&error);
2403 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2405 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2407 log_error("Failed to reboot: %s.", bus_error(&error, r));
2408 dbus_error_free(&error);
2414 case SERVICE_START_LIMIT_REBOOT_FORCE:
2415 log_warning("%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2416 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2419 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2420 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2422 reboot(RB_AUTOBOOT);
2426 log_error("start limit action=%i", s->start_limit_action);
2427 assert_not_reached("Unknown StartLimitAction.");
2433 static int service_start(Unit *u) {
2434 Service *s = SERVICE(u);
2439 /* We cannot fulfill this request right now, try again later
2441 if (s->state == SERVICE_STOP ||
2442 s->state == SERVICE_STOP_SIGTERM ||
2443 s->state == SERVICE_STOP_SIGKILL ||
2444 s->state == SERVICE_STOP_POST ||
2445 s->state == SERVICE_FINAL_SIGTERM ||
2446 s->state == SERVICE_FINAL_SIGKILL)
2449 /* Already on it! */
2450 if (s->state == SERVICE_START_PRE ||
2451 s->state == SERVICE_START ||
2452 s->state == SERVICE_START_POST)
2455 /* A service that will be restarted must be stopped first to
2456 * trigger BindsTo and/or OnFailure dependencies. If a user
2457 * does not want to wait for the holdoff time to elapse, the
2458 * service should be manually restarted, not started. We
2459 * simply return EAGAIN here, so that any start jobs stay
2460 * queued, and assume that the auto restart timer will
2461 * eventually trigger the restart. */
2462 if (s->state == SERVICE_AUTO_RESTART)
2465 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2467 /* Make sure we don't enter a busy loop of some kind. */
2468 r = service_start_limit_test(s);
2470 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2474 s->result = SERVICE_SUCCESS;
2475 s->reload_result = SERVICE_SUCCESS;
2476 s->main_pid_known = false;
2477 s->main_pid_alien = false;
2478 s->forbid_restart = false;
2480 service_enter_start_pre(s);
2484 static int service_stop(Unit *u) {
2485 Service *s = SERVICE(u);
2489 /* Don't create restart jobs from here. */
2490 s->forbid_restart = true;
2493 if (s->state == SERVICE_STOP ||
2494 s->state == SERVICE_STOP_SIGTERM ||
2495 s->state == SERVICE_STOP_SIGKILL ||
2496 s->state == SERVICE_STOP_POST ||
2497 s->state == SERVICE_FINAL_SIGTERM ||
2498 s->state == SERVICE_FINAL_SIGKILL)
2501 /* A restart will be scheduled or is in progress. */
2502 if (s->state == SERVICE_AUTO_RESTART) {
2503 service_set_state(s, SERVICE_DEAD);
2507 /* If there's already something running we go directly into
2509 if (s->state == SERVICE_START_PRE ||
2510 s->state == SERVICE_START ||
2511 s->state == SERVICE_START_POST ||
2512 s->state == SERVICE_RELOAD) {
2513 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2517 assert(s->state == SERVICE_RUNNING ||
2518 s->state == SERVICE_EXITED);
2520 service_enter_stop(s, SERVICE_SUCCESS);
2524 static int service_reload(Unit *u) {
2525 Service *s = SERVICE(u);
2529 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2531 service_enter_reload(s);
2535 static bool service_can_reload(Unit *u) {
2536 Service *s = SERVICE(u);
2540 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2543 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2544 Service *s = SERVICE(u);
2550 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2551 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2552 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2554 if (s->control_pid > 0)
2555 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2557 if (s->main_pid_known && s->main_pid > 0)
2558 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2560 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2563 unit_serialize_item(u, f, "status-text", s->status_text);
2565 /* FIXME: There's a minor uncleanliness here: if there are
2566 * multiple commands attached here, we will start from the
2567 * first one again */
2568 if (s->control_command_id >= 0)
2569 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2571 if (s->socket_fd >= 0) {
2574 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2577 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2580 if (s->main_exec_status.pid > 0) {
2581 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2582 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2583 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2585 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2586 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2587 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2590 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2591 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2596 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2597 Service *s = SERVICE(u);
2604 if (streq(key, "state")) {
2607 if ((state = service_state_from_string(value)) < 0)
2608 log_debug("Failed to parse state value %s", value);
2610 s->deserialized_state = state;
2611 } else if (streq(key, "result")) {
2614 f = service_result_from_string(value);
2616 log_debug("Failed to parse result value %s", value);
2617 else if (f != SERVICE_SUCCESS)
2620 } else if (streq(key, "reload-result")) {
2623 f = service_result_from_string(value);
2625 log_debug("Failed to parse reload result value %s", value);
2626 else if (f != SERVICE_SUCCESS)
2627 s->reload_result = f;
2629 } else if (streq(key, "control-pid")) {
2632 if (parse_pid(value, &pid) < 0)
2633 log_debug("Failed to parse control-pid value %s", value);
2635 s->control_pid = pid;
2636 } else if (streq(key, "main-pid")) {
2639 if (parse_pid(value, &pid) < 0)
2640 log_debug("Failed to parse main-pid value %s", value);
2642 service_set_main_pid(s, (pid_t) pid);
2643 } else if (streq(key, "main-pid-known")) {
2646 if ((b = parse_boolean(value)) < 0)
2647 log_debug("Failed to parse main-pid-known value %s", value);
2649 s->main_pid_known = b;
2650 } else if (streq(key, "status-text")) {
2653 if ((t = strdup(value))) {
2654 free(s->status_text);
2658 } else if (streq(key, "control-command")) {
2659 ServiceExecCommand id;
2661 if ((id = service_exec_command_from_string(value)) < 0)
2662 log_debug("Failed to parse exec-command value %s", value);
2664 s->control_command_id = id;
2665 s->control_command = s->exec_command[id];
2667 } else if (streq(key, "socket-fd")) {
2670 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2671 log_debug("Failed to parse socket-fd value %s", value);
2674 if (s->socket_fd >= 0)
2675 close_nointr_nofail(s->socket_fd);
2676 s->socket_fd = fdset_remove(fds, fd);
2678 } else if (streq(key, "main-exec-status-pid")) {
2681 if (parse_pid(value, &pid) < 0)
2682 log_debug("Failed to parse main-exec-status-pid value %s", value);
2684 s->main_exec_status.pid = pid;
2685 } else if (streq(key, "main-exec-status-code")) {
2688 if (safe_atoi(value, &i) < 0)
2689 log_debug("Failed to parse main-exec-status-code value %s", value);
2691 s->main_exec_status.code = i;
2692 } else if (streq(key, "main-exec-status-status")) {
2695 if (safe_atoi(value, &i) < 0)
2696 log_debug("Failed to parse main-exec-status-status value %s", value);
2698 s->main_exec_status.status = i;
2699 } else if (streq(key, "main-exec-status-start"))
2700 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2701 else if (streq(key, "main-exec-status-exit"))
2702 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2703 else if (streq(key, "watchdog-timestamp"))
2704 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2706 log_debug("Unknown serialization key '%s'", key);
2711 static UnitActiveState service_active_state(Unit *u) {
2712 const UnitActiveState *table;
2716 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2718 return table[SERVICE(u)->state];
2721 static const char *service_sub_state_to_string(Unit *u) {
2724 return service_state_to_string(SERVICE(u)->state);
2727 static bool service_check_gc(Unit *u) {
2728 Service *s = SERVICE(u);
2732 /* Never clean up services that still have a process around,
2733 * even if the service is formally dead. */
2734 if (cgroup_good(s) > 0 ||
2735 main_pid_good(s) > 0 ||
2736 control_pid_good(s) > 0)
2739 #ifdef HAVE_SYSV_COMPAT
2747 static bool service_check_snapshot(Unit *u) {
2748 Service *s = SERVICE(u);
2752 return !s->got_socket_fd;
2755 static int service_retry_pid_file(Service *s) {
2758 assert(s->pid_file);
2759 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2761 r = service_load_pid_file(s, false);
2765 service_unwatch_pid_file(s);
2767 service_enter_running(s, SERVICE_SUCCESS);
2771 static int service_watch_pid_file(Service *s) {
2774 log_debug("Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
2775 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2779 /* the pidfile might have appeared just before we set the watch */
2780 service_retry_pid_file(s);
2784 log_error("Failed to set a watch for %s's PID file %s: %s",
2785 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2786 service_unwatch_pid_file(s);
2790 static int service_demand_pid_file(Service *s) {
2793 assert(s->pid_file);
2794 assert(!s->pid_file_pathspec);
2796 ps = new0(PathSpec, 1);
2800 ps->path = strdup(s->pid_file);
2806 path_kill_slashes(ps->path);
2808 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2809 * keep their PID file open all the time. */
2810 ps->type = PATH_MODIFIED;
2811 ps->inotify_fd = -1;
2813 s->pid_file_pathspec = ps;
2815 return service_watch_pid_file(s);
2818 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2819 Service *s = SERVICE(u);
2823 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2824 assert(s->pid_file_pathspec);
2825 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2827 log_debug("inotify event for %s", u->id);
2829 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2832 if (service_retry_pid_file(s) == 0)
2835 if (service_watch_pid_file(s) < 0)
2840 service_unwatch_pid_file(s);
2841 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2844 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2845 Service *s = SERVICE(u);
2851 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2852 is_clean_exit_lsb(code, status, &s->success_status))
2853 f = SERVICE_SUCCESS;
2854 else if (code == CLD_EXITED)
2855 f = SERVICE_FAILURE_EXIT_CODE;
2856 else if (code == CLD_KILLED)
2857 f = SERVICE_FAILURE_SIGNAL;
2858 else if (code == CLD_DUMPED)
2859 f = SERVICE_FAILURE_CORE_DUMP;
2861 assert_not_reached("Unknown code");
2863 if (s->main_pid == pid) {
2864 /* Forking services may occasionally move to a new PID.
2865 * As long as they update the PID file before exiting the old
2866 * PID, they're fine. */
2867 if (service_load_pid_file(s, false) == 0)
2871 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2873 /* If this is not a forking service than the main
2874 * process got started and hence we copy the exit
2875 * status so that it is recorded both as main and as
2876 * control process exit status */
2877 if (s->main_command) {
2878 s->main_command->exec_status = s->main_exec_status;
2880 if (s->main_command->ignore)
2881 f = SERVICE_SUCCESS;
2884 log_struct(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2885 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2886 u->id, sigchld_code_to_string(code), status,
2887 strna(code == CLD_EXITED
2888 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2889 : signal_to_string(status)),
2891 "EXIT_CODE=%s", sigchld_code_to_string(code),
2892 "EXIT_STATUS=%i", status,
2895 if (f != SERVICE_SUCCESS)
2898 if (s->main_command &&
2899 s->main_command->command_next &&
2900 f == SERVICE_SUCCESS) {
2902 /* There is another command to *
2903 * execute, so let's do that. */
2905 log_debug("%s running next main command for state %s", u->id, service_state_to_string(s->state));
2906 service_run_next_main(s);
2910 /* The service exited, so the service is officially
2912 s->main_command = NULL;
2916 case SERVICE_START_POST:
2917 case SERVICE_RELOAD:
2919 /* Need to wait until the operation is
2924 if (s->type == SERVICE_ONESHOT) {
2925 /* This was our main goal, so let's go on */
2926 if (f == SERVICE_SUCCESS)
2927 service_enter_start_post(s);
2929 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2935 case SERVICE_RUNNING:
2936 service_enter_running(s, f);
2939 case SERVICE_STOP_SIGTERM:
2940 case SERVICE_STOP_SIGKILL:
2942 if (!control_pid_good(s))
2943 service_enter_stop_post(s, f);
2945 /* If there is still a control process, wait for that first */
2949 assert_not_reached("Uh, main process died at wrong time.");
2953 } else if (s->control_pid == pid) {
2957 if (s->control_command) {
2958 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2960 if (s->control_command->ignore)
2961 f = SERVICE_SUCCESS;
2964 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2965 "%s: control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
2967 if (f != SERVICE_SUCCESS)
2970 /* Immediately get rid of the cgroup, so that the
2971 * kernel doesn't delay the cgroup empty messages for
2972 * the service cgroup any longer than necessary */
2973 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2975 if (s->control_command &&
2976 s->control_command->command_next &&
2977 f == SERVICE_SUCCESS) {
2979 /* There is another command to *
2980 * execute, so let's do that. */
2982 log_debug("%s running next control command for state %s", u->id, service_state_to_string(s->state));
2983 service_run_next_control(s);
2986 /* No further commands for this step, so let's
2987 * figure out what to do next */
2989 s->control_command = NULL;
2990 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2992 log_debug("%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
2996 case SERVICE_START_PRE:
2997 if (f == SERVICE_SUCCESS)
2998 service_enter_start(s);
3000 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3004 if (s->type != SERVICE_FORKING)
3005 /* Maybe spurious event due to a reload that changed the type? */
3008 if (f != SERVICE_SUCCESS) {
3009 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3014 bool has_start_post;
3017 /* Let's try to load the pid file here if we can.
3018 * The PID file might actually be created by a START_POST
3019 * script. In that case don't worry if the loading fails. */
3021 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3022 r = service_load_pid_file(s, !has_start_post);
3023 if (!has_start_post && r < 0) {
3024 r = service_demand_pid_file(s);
3025 if (r < 0 || !cgroup_good(s))
3026 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3030 service_search_main_pid(s);
3032 service_enter_start_post(s);
3035 case SERVICE_START_POST:
3036 if (f != SERVICE_SUCCESS) {
3037 service_enter_stop(s, f);
3044 r = service_load_pid_file(s, true);
3046 r = service_demand_pid_file(s);
3047 if (r < 0 || !cgroup_good(s))
3048 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3052 service_search_main_pid(s);
3054 service_enter_running(s, SERVICE_SUCCESS);
3057 case SERVICE_RELOAD:
3058 if (f == SERVICE_SUCCESS) {
3059 service_load_pid_file(s, true);
3060 service_search_main_pid(s);
3063 s->reload_result = f;
3064 service_enter_running(s, SERVICE_SUCCESS);
3068 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3071 case SERVICE_STOP_SIGTERM:
3072 case SERVICE_STOP_SIGKILL:
3073 if (main_pid_good(s) <= 0)
3074 service_enter_stop_post(s, f);
3076 /* If there is still a service
3077 * process around, wait until
3078 * that one quit, too */
3081 case SERVICE_STOP_POST:
3082 case SERVICE_FINAL_SIGTERM:
3083 case SERVICE_FINAL_SIGKILL:
3084 service_enter_dead(s, f, true);
3088 assert_not_reached("Uh, control process died at wrong time.");
3093 /* Notify clients about changed exit status */
3094 unit_add_to_dbus_queue(u);
3097 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3098 Service *s = SERVICE(u);
3101 assert(elapsed == 1);
3103 if (w == &s->watchdog_watch) {
3104 service_handle_watchdog(s);
3108 assert(w == &s->timer_watch);
3112 case SERVICE_START_PRE:
3114 log_warning("%s operation timed out. Terminating.", u->id);
3115 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3118 case SERVICE_START_POST:
3119 log_warning("%s operation timed out. Stopping.", u->id);
3120 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3123 case SERVICE_RELOAD:
3124 log_warning("%s operation timed out. Stopping.", u->id);
3125 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3126 service_enter_running(s, SERVICE_SUCCESS);
3130 log_warning("%s stopping timed out. Terminating.", u->id);
3131 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3134 case SERVICE_STOP_SIGTERM:
3135 if (s->kill_context.send_sigkill) {
3136 log_warning("%s stopping timed out. Killing.", u->id);
3137 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3139 log_warning("%s stopping timed out. Skipping SIGKILL.", u->id);
3140 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3145 case SERVICE_STOP_SIGKILL:
3146 /* Uh, we sent a SIGKILL and it is still not gone?
3147 * Must be something we cannot kill, so let's just be
3148 * weirded out and continue */
3150 log_warning("%s still around after SIGKILL. Ignoring.", u->id);
3151 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3154 case SERVICE_STOP_POST:
3155 log_warning("%s stopping timed out (2). Terminating.", u->id);
3156 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3159 case SERVICE_FINAL_SIGTERM:
3160 if (s->kill_context.send_sigkill) {
3161 log_warning("%s stopping timed out (2). Killing.", u->id);
3162 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3164 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->id);
3165 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3170 case SERVICE_FINAL_SIGKILL:
3171 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->id);
3172 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3175 case SERVICE_AUTO_RESTART:
3176 log_info("%s holdoff time over, scheduling restart.", u->id);
3177 service_enter_restart(s);
3181 assert_not_reached("Timeout at wrong time.");
3185 static void service_cgroup_notify_event(Unit *u) {
3186 Service *s = SERVICE(u);
3190 log_debug("%s: cgroup is empty", u->id);
3194 /* Waiting for SIGCHLD is usually more interesting,
3195 * because it includes return codes/signals. Which is
3196 * why we ignore the cgroup events for most cases,
3197 * except when we don't know pid which to expect the
3201 case SERVICE_START_POST:
3202 /* If we were hoping for the daemon to write its PID file,
3203 * we can give up now. */
3204 if (s->pid_file_pathspec) {
3205 log_warning("%s never wrote its PID file. Failing.", UNIT(s)->id);
3206 service_unwatch_pid_file(s);
3207 if (s->state == SERVICE_START)
3208 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3210 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3214 case SERVICE_RUNNING:
3215 /* service_enter_running() will figure out what to do */
3216 service_enter_running(s, SERVICE_SUCCESS);
3219 case SERVICE_STOP_SIGTERM:
3220 case SERVICE_STOP_SIGKILL:
3222 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3223 service_enter_stop_post(s, SERVICE_SUCCESS);
3227 case SERVICE_FINAL_SIGTERM:
3228 case SERVICE_FINAL_SIGKILL:
3229 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3230 service_enter_dead(s, SERVICE_SUCCESS, true);
3239 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3240 Service *s = SERVICE(u);
3245 if (s->notify_access == NOTIFY_NONE) {
3246 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3247 u->id, (unsigned long) pid);
3251 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3252 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3253 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3257 log_debug("%s: Got message", u->id);
3259 /* Interpret MAINPID= */
3260 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3261 (s->state == SERVICE_START ||
3262 s->state == SERVICE_START_POST ||
3263 s->state == SERVICE_RUNNING ||
3264 s->state == SERVICE_RELOAD)) {
3266 if (parse_pid(e + 8, &pid) < 0)
3267 log_warning("Failed to parse notification message %s", e);
3269 log_debug("%s: got %s", u->id, e);
3270 service_set_main_pid(s, pid);
3274 /* Interpret READY= */
3275 if (s->type == SERVICE_NOTIFY &&
3276 s->state == SERVICE_START &&
3277 strv_find(tags, "READY=1")) {
3278 log_debug("%s: got READY=1", u->id);
3280 service_enter_start_post(s);
3283 /* Interpret STATUS= */
3284 e = strv_find_prefix(tags, "STATUS=");
3290 if (!utf8_is_valid(e+7)) {
3291 log_warning("Status message in notification is not UTF-8 clean.");
3297 log_error("Failed to allocate string.");
3301 log_debug("%s: got %s", u->id, e);
3303 free(s->status_text);
3306 free(s->status_text);
3307 s->status_text = NULL;
3311 if (strv_find(tags, "WATCHDOG=1")) {
3312 log_debug("%s: got WATCHDOG=1", u->id);
3313 service_reset_watchdog(s);
3316 /* Notify clients about changed status or main pid */
3317 unit_add_to_dbus_queue(u);
3320 #ifdef HAVE_SYSV_COMPAT
3322 static int service_enumerate(Manager *m) {
3326 char *path = NULL, *fpath = NULL, *name = NULL;
3327 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3334 if (m->running_as != SYSTEMD_SYSTEM)
3337 zero(runlevel_services);
3339 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3340 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3344 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3353 if (!(d = opendir(path))) {
3354 if (errno != ENOENT)
3355 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3360 while ((de = readdir(d))) {
3363 if (ignore_file(de->d_name))
3366 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3369 if (strlen(de->d_name) < 4)
3372 a = undecchar(de->d_name[1]);
3373 b = undecchar(de->d_name[2]);
3379 fpath = strjoin(path, "/", de->d_name, NULL);
3385 if (access(fpath, X_OK) < 0) {
3387 if (errno != ENOENT)
3388 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3394 if (!(name = sysv_translate_name(de->d_name + 3))) {
3399 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
3400 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3404 if (de->d_name[0] == 'S') {
3406 if (rcnd_table[i].type == RUNLEVEL_UP) {
3407 SERVICE(service)->sysv_start_priority_from_rcnd =
3408 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3410 SERVICE(service)->sysv_enabled = true;
3413 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3416 if ((r = set_put(runlevel_services[i], service)) < 0)
3419 } else if (de->d_name[0] == 'K' &&
3420 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
3422 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3425 if ((r = set_put(shutdown_services, service)) < 0)
3431 /* Now we loaded all stubs and are aware of the lowest
3432 start-up priority for all services, not let's actually load
3433 the services, this will also tell us which services are
3434 actually native now */
3435 manager_dispatch_load_queue(m);
3437 /* If this is a native service, rely on native ways to pull in
3438 * a service, don't pull it in via sysv rcN.d links. */
3439 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3440 SET_FOREACH(service, runlevel_services[i], j) {
3441 service = unit_follow_merge(service);
3443 if (service->fragment_path)
3446 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3450 /* We honour K links only for halt/reboot. For the normal
3451 * runlevels we assume the stop jobs will be implicitly added
3452 * by the core logic. Also, we don't really distinguish here
3453 * between the runlevels 0 and 6 and just add them to the
3454 * special shutdown target. */
3455 SET_FOREACH(service, shutdown_services, j) {
3456 service = unit_follow_merge(service);
3458 if (service->fragment_path)
3461 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3472 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3473 set_free(runlevel_services[i]);
3474 set_free(shutdown_services);
3483 static void service_bus_name_owner_change(
3486 const char *old_owner,
3487 const char *new_owner) {
3489 Service *s = SERVICE(u);
3494 assert(streq(s->bus_name, name));
3495 assert(old_owner || new_owner);
3497 if (old_owner && new_owner)
3498 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
3500 log_debug("%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
3502 log_debug("%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
3504 s->bus_name_good = !!new_owner;
3506 if (s->type == SERVICE_DBUS) {
3508 /* service_enter_running() will figure out what to
3510 if (s->state == SERVICE_RUNNING)
3511 service_enter_running(s, SERVICE_SUCCESS);
3512 else if (s->state == SERVICE_START && new_owner)
3513 service_enter_start_post(s);
3515 } else if (new_owner &&
3517 (s->state == SERVICE_START ||
3518 s->state == SERVICE_START_POST ||
3519 s->state == SERVICE_RUNNING ||
3520 s->state == SERVICE_RELOAD)) {
3522 /* Try to acquire PID from bus service */
3523 log_debug("Trying to acquire PID from D-Bus name...");
3525 bus_query_pid(u->manager, name);
3529 static void service_bus_query_pid_done(
3534 Service *s = SERVICE(u);
3539 log_debug("%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3541 if (s->main_pid <= 0 &&
3542 (s->state == SERVICE_START ||
3543 s->state == SERVICE_START_POST ||
3544 s->state == SERVICE_RUNNING ||
3545 s->state == SERVICE_RELOAD))
3546 service_set_main_pid(s, pid);
3549 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3554 /* This is called by the socket code when instantiating a new
3555 * service for a stream socket and the socket needs to be
3558 if (UNIT(s)->load_state != UNIT_LOADED)
3561 if (s->socket_fd >= 0)
3564 if (s->state != SERVICE_DEAD)
3568 s->got_socket_fd = true;
3570 unit_ref_set(&s->accept_socket, UNIT(sock));
3572 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3575 static void service_reset_failed(Unit *u) {
3576 Service *s = SERVICE(u);
3580 if (s->state == SERVICE_FAILED)
3581 service_set_state(s, SERVICE_DEAD);
3583 s->result = SERVICE_SUCCESS;
3584 s->reload_result = SERVICE_SUCCESS;
3586 RATELIMIT_RESET(s->start_limit);
3589 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3590 Service *s = SERVICE(u);
3592 Set *pid_set = NULL;
3596 if (s->main_pid <= 0 && who == KILL_MAIN) {
3597 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3601 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3602 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3606 if (who == KILL_CONTROL || who == KILL_ALL)
3607 if (s->control_pid > 0)
3608 if (kill(s->control_pid, signo) < 0)
3611 if (who == KILL_MAIN || who == KILL_ALL)
3612 if (s->main_pid > 0)
3613 if (kill(s->main_pid, signo) < 0)
3616 if (who == KILL_ALL) {
3619 pid_set = set_new(trivial_hash_func, trivial_compare_func);
3623 /* Exclude the control/main pid from being killed via the cgroup */
3624 if (s->control_pid > 0) {
3625 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
3632 if (s->main_pid > 0) {
3633 q = set_put(pid_set, LONG_TO_PTR(s->main_pid));
3640 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3641 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3652 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3653 [SERVICE_DEAD] = "dead",
3654 [SERVICE_START_PRE] = "start-pre",
3655 [SERVICE_START] = "start",
3656 [SERVICE_START_POST] = "start-post",
3657 [SERVICE_RUNNING] = "running",
3658 [SERVICE_EXITED] = "exited",
3659 [SERVICE_RELOAD] = "reload",
3660 [SERVICE_STOP] = "stop",
3661 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3662 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3663 [SERVICE_STOP_POST] = "stop-post",
3664 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3665 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3666 [SERVICE_FAILED] = "failed",
3667 [SERVICE_AUTO_RESTART] = "auto-restart",
3670 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3672 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3673 [SERVICE_RESTART_NO] = "no",
3674 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3675 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3676 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3677 [SERVICE_RESTART_ALWAYS] = "always"
3680 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3682 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3683 [SERVICE_SIMPLE] = "simple",
3684 [SERVICE_FORKING] = "forking",
3685 [SERVICE_ONESHOT] = "oneshot",
3686 [SERVICE_DBUS] = "dbus",
3687 [SERVICE_NOTIFY] = "notify",
3688 [SERVICE_IDLE] = "idle"
3691 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3693 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3694 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3695 [SERVICE_EXEC_START] = "ExecStart",
3696 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3697 [SERVICE_EXEC_RELOAD] = "ExecReload",
3698 [SERVICE_EXEC_STOP] = "ExecStop",
3699 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3702 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3704 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3705 [NOTIFY_NONE] = "none",
3706 [NOTIFY_MAIN] = "main",
3707 [NOTIFY_ALL] = "all"
3710 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3712 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3713 [SERVICE_SUCCESS] = "success",
3714 [SERVICE_FAILURE_RESOURCES] = "resources",
3715 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3716 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3717 [SERVICE_FAILURE_SIGNAL] = "signal",
3718 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3719 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3720 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3723 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3725 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3726 [SERVICE_START_LIMIT_NONE] = "none",
3727 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3728 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3729 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3731 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3733 const UnitVTable service_vtable = {
3734 .object_size = sizeof(Service),
3735 .exec_context_offset = offsetof(Service, exec_context),
3742 .init = service_init,
3743 .done = service_done,
3744 .load = service_load,
3746 .coldplug = service_coldplug,
3748 .dump = service_dump,
3750 .start = service_start,
3751 .stop = service_stop,
3752 .reload = service_reload,
3754 .can_reload = service_can_reload,
3756 .kill = service_kill,
3758 .serialize = service_serialize,
3759 .deserialize_item = service_deserialize_item,
3761 .active_state = service_active_state,
3762 .sub_state_to_string = service_sub_state_to_string,
3764 .check_gc = service_check_gc,
3765 .check_snapshot = service_check_snapshot,
3767 .sigchld_event = service_sigchld_event,
3768 .timer_event = service_timer_event,
3769 .fd_event = service_fd_event,
3771 .reset_failed = service_reset_failed,
3773 .cgroup_notify_empty = service_cgroup_notify_event,
3774 .notify_message = service_notify_message,
3776 .bus_name_owner_change = service_bus_name_owner_change,
3777 .bus_query_pid_done = service_bus_query_pid_done,
3779 .bus_interface = "org.freedesktop.systemd1.Service",
3780 .bus_message_handler = bus_service_message_handler,
3781 .bus_invalidating_properties = bus_service_invalidating_properties,
3783 #ifdef HAVE_SYSV_COMPAT
3784 .enumerate = service_enumerate,
3786 .status_message_formats = {
3787 .starting_stopping = {
3788 [0] = "Starting %s...",
3789 [1] = "Stopping %s...",
3791 .finished_start_job = {
3792 [JOB_DONE] = "Started %s.",
3793 [JOB_FAILED] = "Failed to start %s.",
3794 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3795 [JOB_TIMEOUT] = "Timed out starting %s.",
3797 .finished_stop_job = {
3798 [JOB_DONE] = "Stopped %s.",
3799 [JOB_FAILED] = "Stopped (with error) %s.",
3800 [JOB_TIMEOUT] = "Timed out stopping %s.",