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 {
59 const RunlevelType type;
61 /* Standard SysV runlevels for start-up */
62 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
63 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
64 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
65 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
66 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
69 /* SUSE style boot.d */
70 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
73 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
74 /* Debian style rcS.d */
75 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
78 /* Standard SysV runlevels for shutdown */
79 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
80 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
82 /* Note that the order here matters, as we read the
83 directories in this order, and we want to make sure that
84 sysv_start_priority is known when we first load the
85 unit. And that value we only know from S links. Hence
86 UP/SYSINIT must be read before DOWN */
89 #define RUNLEVELS_UP "12345"
90 /* #define RUNLEVELS_DOWN "06" */
91 #define RUNLEVELS_BOOT "bBsS"
94 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
95 [SERVICE_DEAD] = UNIT_INACTIVE,
96 [SERVICE_START_PRE] = UNIT_ACTIVATING,
97 [SERVICE_START] = UNIT_ACTIVATING,
98 [SERVICE_START_POST] = UNIT_ACTIVATING,
99 [SERVICE_RUNNING] = UNIT_ACTIVE,
100 [SERVICE_EXITED] = UNIT_ACTIVE,
101 [SERVICE_RELOAD] = UNIT_RELOADING,
102 [SERVICE_STOP] = UNIT_DEACTIVATING,
103 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
104 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
105 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
106 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
107 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
108 [SERVICE_FAILED] = UNIT_FAILED,
109 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
112 /* For Type=idle we never want to delay any other jobs, hence we
113 * consider idle jobs active as soon as we start working on them */
114 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
115 [SERVICE_DEAD] = UNIT_INACTIVE,
116 [SERVICE_START_PRE] = UNIT_ACTIVE,
117 [SERVICE_START] = UNIT_ACTIVE,
118 [SERVICE_START_POST] = UNIT_ACTIVE,
119 [SERVICE_RUNNING] = UNIT_ACTIVE,
120 [SERVICE_EXITED] = UNIT_ACTIVE,
121 [SERVICE_RELOAD] = UNIT_RELOADING,
122 [SERVICE_STOP] = UNIT_DEACTIVATING,
123 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
124 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
125 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
126 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
127 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
128 [SERVICE_FAILED] = UNIT_FAILED,
129 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
132 static void service_init(Unit *u) {
133 Service *s = SERVICE(u);
136 assert(u->load_state == UNIT_STUB);
138 s->timeout_start_usec = DEFAULT_TIMEOUT_USEC;
139 s->timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
140 s->restart_usec = DEFAULT_RESTART_USEC;
141 s->type = _SERVICE_TYPE_INVALID;
143 s->watchdog_watch.type = WATCH_INVALID;
145 s->timer_watch.type = WATCH_INVALID;
146 #ifdef HAVE_SYSV_COMPAT
147 s->sysv_start_priority = -1;
148 s->sysv_start_priority_from_rcnd = -1;
151 s->guess_main_pid = true;
153 exec_context_init(&s->exec_context);
154 kill_context_init(&s->kill_context);
156 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
158 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
161 static void service_unwatch_control_pid(Service *s) {
164 if (s->control_pid <= 0)
167 unit_unwatch_pid(UNIT(s), s->control_pid);
171 static void service_unwatch_main_pid(Service *s) {
174 if (s->main_pid <= 0)
177 unit_unwatch_pid(UNIT(s), s->main_pid);
181 static void service_unwatch_pid_file(Service *s) {
182 if (!s->pid_file_pathspec)
185 log_debug("Stopping watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
186 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
187 path_spec_done(s->pid_file_pathspec);
188 free(s->pid_file_pathspec);
189 s->pid_file_pathspec = NULL;
192 static int service_set_main_pid(Service *s, pid_t pid) {
204 s->main_pid_known = true;
206 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
207 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
208 UNIT(s)->id, (unsigned long) pid);
210 s->main_pid_alien = true;
212 s->main_pid_alien = false;
214 exec_status_start(&s->main_exec_status, pid);
219 static void service_close_socket_fd(Service *s) {
222 if (s->socket_fd < 0)
225 close_nointr_nofail(s->socket_fd);
229 static void service_connection_unref(Service *s) {
232 if (!UNIT_DEREF(s->accept_socket))
235 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
236 unit_ref_unset(&s->accept_socket);
239 static void service_stop_watchdog(Service *s) {
242 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
243 s->watchdog_timestamp.realtime = 0;
244 s->watchdog_timestamp.monotonic = 0;
247 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart);
249 static void service_handle_watchdog(Service *s) {
255 if (s->watchdog_usec == 0)
258 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
259 if (offset >= s->watchdog_usec) {
260 log_error("%s watchdog timeout!", UNIT(s)->id);
261 service_enter_dead(s, SERVICE_FAILURE_WATCHDOG, true);
265 r = unit_watch_timer(UNIT(s), s->watchdog_usec - offset, &s->watchdog_watch);
267 log_warning("%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
270 static void service_reset_watchdog(Service *s) {
273 dual_timestamp_get(&s->watchdog_timestamp);
274 service_handle_watchdog(s);
277 static void service_done(Unit *u) {
278 Service *s = SERVICE(u);
285 #ifdef HAVE_SYSV_COMPAT
286 free(s->sysv_runlevels);
287 s->sysv_runlevels = NULL;
290 free(s->status_text);
291 s->status_text = NULL;
293 exec_context_done(&s->exec_context);
294 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
295 s->control_command = NULL;
296 s->main_command = NULL;
298 set_free(s->restart_ignore_status.code);
299 s->restart_ignore_status.code = NULL;
300 set_free(s->restart_ignore_status.signal);
301 s->restart_ignore_status.signal = NULL;
303 set_free(s->success_status.code);
304 s->success_status.code = NULL;
305 set_free(s->success_status.signal);
306 s->success_status.signal = NULL;
308 /* This will leak a process, but at least no memory or any of
310 service_unwatch_main_pid(s);
311 service_unwatch_control_pid(s);
312 service_unwatch_pid_file(s);
315 unit_unwatch_bus_name(u, s->bus_name);
320 service_close_socket_fd(s);
321 service_connection_unref(s);
323 unit_ref_unset(&s->accept_socket);
325 service_stop_watchdog(s);
327 unit_unwatch_timer(u, &s->timer_watch);
330 #ifdef HAVE_SYSV_COMPAT
331 static char *sysv_translate_name(const char *name) {
334 if (!(r = new(char, strlen(name) + sizeof(".service"))))
337 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
338 if (endswith(name, ".sh"))
339 /* Drop Debian-style .sh suffix */
340 strcpy(stpcpy(r, name) - 3, ".service");
343 if (startswith(name, "boot."))
344 /* Drop SuSE-style boot. prefix */
345 strcpy(stpcpy(r, name + 5), ".service");
347 #ifdef TARGET_FRUGALWARE
348 if (startswith(name, "rc."))
349 /* Drop Frugalware-style rc. prefix */
350 strcpy(stpcpy(r, name + 3), ".service");
353 /* Normal init scripts */
354 strcpy(stpcpy(r, name), ".service");
359 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
361 /* We silently ignore the $ prefix here. According to the LSB
362 * spec it simply indicates whether something is a
363 * standardized name or a distribution-specific one. Since we
364 * just follow what already exists and do not introduce new
365 * uses or names we don't care who introduced a new name. */
367 static const char * const table[] = {
368 /* LSB defined facilities */
369 "local_fs", SPECIAL_LOCAL_FS_TARGET,
370 #if defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
372 /* Due to unfortunate name selection in Mandriva,
373 * $network is provided by network-up which is ordered
374 * after network which actually starts interfaces.
375 * To break the loop, just ignore it */
376 "network", SPECIAL_NETWORK_TARGET,
378 "named", SPECIAL_NSS_LOOKUP_TARGET,
379 "portmap", SPECIAL_RPCBIND_TARGET,
380 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
381 "syslog", SPECIAL_SYSLOG_TARGET,
382 "time", SPECIAL_TIME_SYNC_TARGET,
384 /* common extensions */
385 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
386 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
389 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
390 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
394 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
395 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
396 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
400 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
411 n = *name == '$' ? name + 1 : name;
413 for (i = 0; i < ELEMENTSOF(table); i += 2) {
415 if (!streq(table[i], n))
421 if (!(r = strdup(table[i+1])))
427 /* If we don't know this name, fallback heuristics to figure
428 * out whether something is a target or a service alias. */
431 if (!unit_prefix_is_valid(n))
434 /* Facilities starting with $ are most likely targets */
435 r = unit_name_build(n, NULL, ".target");
436 } else if (filename && streq(name, filename))
437 /* Names equaling the file name of the services are redundant */
440 /* Everything else we assume to be normal service names */
441 r = sysv_translate_name(n);
452 static int sysv_fix_order(Service *s) {
458 if (s->sysv_start_priority < 0)
461 /* For each pair of services where at least one lacks a LSB
462 * header, we use the start priority value to order things. */
464 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
467 bool special_s, special_t;
474 if (UNIT(t)->load_state != UNIT_LOADED)
477 if (t->sysv_start_priority < 0)
480 /* If both units have modern headers we don't care
481 * about the priorities */
482 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
483 (UNIT(t)->fragment_path || t->sysv_has_lsb))
486 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
487 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
489 if (special_t && !special_s)
491 else if (special_s && !special_t)
493 else if (t->sysv_start_priority < s->sysv_start_priority)
495 else if (t->sysv_start_priority > s->sysv_start_priority)
500 /* FIXME: Maybe we should compare the name here lexicographically? */
502 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
509 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
512 if (!(c = new0(ExecCommand, 1)))
515 if (!(c->path = strdup(path))) {
520 if (!(c->argv = strv_new(path, arg1, NULL))) {
529 static int sysv_exec_commands(Service *s) {
534 assert(UNIT(s)->source_path);
536 c = exec_command_new(UNIT(s)->source_path, "start");
539 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
541 c = exec_command_new(UNIT(s)->source_path, "stop");
544 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
546 c = exec_command_new(UNIT(s)->source_path, "reload");
549 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
554 static int service_load_sysv_path(Service *s, const char *path) {
565 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
573 f = fopen(path, "re");
575 r = errno == ENOENT ? 0 : -errno;
579 if (fstat(fileno(f), &st) < 0) {
584 free(u->source_path);
585 u->source_path = strdup(path);
586 if (!u->source_path) {
590 u->source_mtime = timespec_load(&st.st_mtim);
592 if (null_or_empty(&st)) {
593 u->load_state = UNIT_MASKED;
601 char l[LINE_MAX], *t;
603 if (!fgets(l, sizeof(l), f)) {
608 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
618 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
620 s->sysv_has_lsb = true;
624 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
630 t += strspn(t, WHITESPACE);
632 if (state == NORMAL) {
634 /* Try to parse Red Hat style chkconfig headers */
636 if (startswith_no_case(t, "chkconfig:")) {
638 char runlevels[16], *k;
642 if (sscanf(t+10, "%15s %i %*i",
644 &start_priority) != 2) {
646 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
650 /* A start priority gathered from the
651 * symlink farms is preferred over the
652 * data from the LSB header. */
653 if (start_priority < 0 || start_priority > 99)
654 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
656 s->sysv_start_priority = start_priority;
658 char_array_0(runlevels);
659 k = delete_chars(runlevels, WHITESPACE "-");
664 if (!(d = strdup(k))) {
669 free(s->sysv_runlevels);
670 s->sysv_runlevels = d;
673 } else if (startswith_no_case(t, "description:")) {
675 size_t k = strlen(t);
679 if (t[k-1] == '\\') {
684 if ((j = strstrip(t+12)) && *j) {
685 if (!(d = strdup(j))) {
692 free(chkconfig_description);
693 chkconfig_description = d;
695 } else if (startswith_no_case(t, "pidfile:")) {
702 if (!path_is_absolute(fn)) {
703 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
707 if (!(fn = strdup(fn))) {
716 } else if (state == DESCRIPTION) {
718 /* Try to parse Red Hat style description
721 size_t k = strlen(t);
729 if ((j = strstrip(t)) && *j) {
732 if (chkconfig_description)
733 d = strjoin(chkconfig_description, " ", j, NULL);
742 free(chkconfig_description);
743 chkconfig_description = d;
746 } else if (state == LSB || state == LSB_DESCRIPTION) {
748 if (startswith_no_case(t, "Provides:")) {
754 FOREACH_WORD_QUOTED(w, z, t+9, i) {
757 if (!(n = strndup(w, z))) {
762 r = sysv_translate_facility(n, path_get_file_name(path), &m);
771 if (unit_name_to_type(m) == UNIT_SERVICE)
772 r = unit_add_name(u, m);
779 * indication that the
781 * now available. This
784 * targets do NOT pull
787 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
790 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
795 } else if (startswith_no_case(t, "Required-Start:") ||
796 startswith_no_case(t, "Should-Start:") ||
797 startswith_no_case(t, "X-Start-Before:") ||
798 startswith_no_case(t, "X-Start-After:")) {
804 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
807 if (!(n = strndup(w, z))) {
812 r = sysv_translate_facility(n, path_get_file_name(path), &m);
815 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
825 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
828 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
832 } else if (startswith_no_case(t, "Default-Start:")) {
837 k = delete_chars(t+14, WHITESPACE "-");
840 if (!(d = strdup(k))) {
845 free(s->sysv_runlevels);
846 s->sysv_runlevels = d;
849 } else if (startswith_no_case(t, "Description:")) {
852 state = LSB_DESCRIPTION;
854 if ((j = strstrip(t+12)) && *j) {
855 if (!(d = strdup(j))) {
862 free(long_description);
863 long_description = d;
865 } else if (startswith_no_case(t, "Short-Description:")) {
870 if ((j = strstrip(t+18)) && *j) {
871 if (!(d = strdup(j))) {
878 free(short_description);
879 short_description = d;
881 } else if (state == LSB_DESCRIPTION) {
883 if (startswith(l, "#\t") || startswith(l, "# ")) {
886 if ((j = strstrip(t)) && *j) {
889 if (long_description)
890 d = strjoin(long_description, " ", t, NULL);
899 free(long_description);
900 long_description = d;
909 if ((r = sysv_exec_commands(s)) < 0)
911 if (s->sysv_runlevels &&
912 chars_intersect(RUNLEVELS_BOOT, s->sysv_runlevels) &&
913 chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
914 /* Service has both boot and "up" runlevels
915 configured. Kill the "up" ones. */
916 delete_chars(s->sysv_runlevels, RUNLEVELS_UP);
919 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
920 /* If there a runlevels configured for this service
921 * but none of the standard ones, then we assume this
922 * is some special kind of service (which might be
923 * needed for early boot) and don't create any links
926 UNIT(s)->default_dependencies = false;
928 /* Don't timeout special services during boot (like fsck) */
929 s->timeout_start_usec = 0;
930 s->timeout_stop_usec = 0;
932 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
933 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
936 /* Special setting for all SysV services */
937 s->type = SERVICE_FORKING;
938 s->remain_after_exit = !s->pid_file;
939 s->guess_main_pid = false;
940 s->restart = SERVICE_RESTART_NO;
941 s->exec_context.ignore_sigpipe = false;
942 s->kill_context.kill_mode = KILL_PROCESS;
944 /* We use the long description only if
945 * no short description is set. */
947 if (short_description)
948 description = short_description;
949 else if (chkconfig_description)
950 description = chkconfig_description;
951 else if (long_description)
952 description = long_description;
959 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
967 /* The priority that has been set in /etc/rcN.d/ hierarchies
968 * takes precedence over what is stored as default in the LSB
970 if (s->sysv_start_priority_from_rcnd >= 0)
971 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
973 u->load_state = UNIT_LOADED;
981 free(short_description);
982 free(long_description);
983 free(chkconfig_description);
988 static int service_load_sysv_name(Service *s, const char *name) {
994 /* For SysV services we strip the boot.*, rc.* and *.sh
995 * prefixes/suffixes. */
996 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
997 if (endswith(name, ".sh.service"))
1002 if (startswith(name, "boot."))
1006 #ifdef TARGET_FRUGALWARE
1007 if (startswith(name, "rc."))
1011 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1015 path = strjoin(*p, "/", name, NULL);
1019 assert(endswith(path, ".service"));
1020 path[strlen(path)-8] = 0;
1022 r = service_load_sysv_path(s, path);
1024 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
1025 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1026 /* Try Debian style *.sh source'able init scripts */
1027 strcat(path, ".sh");
1028 r = service_load_sysv_path(s, path);
1034 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1035 /* Try SUSE style boot.* init scripts */
1037 path = strjoin(*p, "/boot.", name, NULL);
1041 /* Drop .service suffix */
1042 path[strlen(path)-8] = 0;
1043 r = service_load_sysv_path(s, path);
1048 #ifdef TARGET_FRUGALWARE
1049 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1050 /* Try Frugalware style rc.* init scripts */
1052 path = strjoin(*p, "/rc.", name, NULL);
1056 /* Drop .service suffix */
1057 path[strlen(path)-8] = 0;
1058 r = service_load_sysv_path(s, path);
1066 if ((UNIT(s)->load_state != UNIT_STUB))
1073 static int service_load_sysv(Service *s) {
1080 /* Load service data from SysV init scripts, preferably with
1081 * LSB headers ... */
1083 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1086 if ((t = UNIT(s)->id))
1087 if ((r = service_load_sysv_name(s, t)) < 0)
1090 if (UNIT(s)->load_state == UNIT_STUB)
1091 SET_FOREACH(t, UNIT(s)->names, i) {
1092 if (t == UNIT(s)->id)
1095 if ((r = service_load_sysv_name(s, t)) < 0)
1098 if (UNIT(s)->load_state != UNIT_STUB)
1106 static int fsck_fix_order(Service *s) {
1112 if (s->fsck_passno <= 0)
1115 /* For each pair of services where both have an fsck priority
1116 * we order things based on it. */
1118 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1127 if (UNIT(t)->load_state != UNIT_LOADED)
1130 if (t->fsck_passno <= 0)
1133 if (t->fsck_passno < s->fsck_passno)
1135 else if (t->fsck_passno > s->fsck_passno)
1140 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1147 static int service_verify(Service *s) {
1150 if (UNIT(s)->load_state != UNIT_LOADED)
1153 if (!s->exec_command[SERVICE_EXEC_START]) {
1154 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1158 if (s->type != SERVICE_ONESHOT &&
1159 s->exec_command[SERVICE_EXEC_START]->command_next) {
1160 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1164 if (s->type == SERVICE_ONESHOT &&
1165 s->exec_command[SERVICE_EXEC_RELOAD]) {
1166 log_error("%s has an ExecReload setting, which is not allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1170 if (s->type == SERVICE_DBUS && !s->bus_name) {
1171 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1175 if (s->bus_name && s->type != SERVICE_DBUS)
1176 log_warning("%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1178 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1179 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1186 static int service_add_default_dependencies(Service *s) {
1191 /* Add a number of automatic dependencies useful for the
1192 * majority of services. */
1194 /* First, pull in base system */
1195 if (UNIT(s)->manager->running_as == MANAGER_SYSTEM) {
1197 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1200 } else if (UNIT(s)->manager->running_as == MANAGER_USER) {
1202 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1206 /* Second, activate normal shutdown */
1207 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1210 static void service_fix_output(Service *s) {
1213 /* If nothing has been explicitly configured, patch default
1214 * output in. If input is socket/tty we avoid this however,
1215 * since in that case we want output to default to the same
1216 * place as we read input from. */
1218 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1219 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1220 s->exec_context.std_input == EXEC_INPUT_NULL)
1221 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1223 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1224 s->exec_context.std_input == EXEC_INPUT_NULL)
1225 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1228 static int service_load(Unit *u) {
1230 Service *s = SERVICE(u);
1234 /* Load a .service file */
1235 if ((r = unit_load_fragment(u)) < 0)
1238 #ifdef HAVE_SYSV_COMPAT
1239 /* Load a classic init script as a fallback, if we couldn't find anything */
1240 if (u->load_state == UNIT_STUB)
1241 if ((r = service_load_sysv(s)) < 0)
1245 /* Still nothing found? Then let's give up */
1246 if (u->load_state == UNIT_STUB)
1249 /* We were able to load something, then let's add in the
1250 * dropin directories. */
1251 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1254 /* This is a new unit? Then let's add in some extras */
1255 if (u->load_state == UNIT_LOADED) {
1256 if (s->type == _SERVICE_TYPE_INVALID)
1257 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1259 /* Oneshot services have disabled start timeout by default */
1260 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1261 s->timeout_start_usec = 0;
1263 service_fix_output(s);
1265 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1268 if ((r = unit_add_default_cgroups(u)) < 0)
1271 #ifdef HAVE_SYSV_COMPAT
1272 if ((r = sysv_fix_order(s)) < 0)
1276 if ((r = fsck_fix_order(s)) < 0)
1280 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1283 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1284 s->notify_access = NOTIFY_MAIN;
1286 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1287 s->notify_access = NOTIFY_MAIN;
1289 if (s->type == SERVICE_DBUS || s->bus_name)
1290 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1293 if (UNIT(s)->default_dependencies)
1294 if ((r = service_add_default_dependencies(s)) < 0)
1297 r = unit_exec_context_defaults(u, &s->exec_context);
1302 return service_verify(s);
1305 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1307 ServiceExecCommand c;
1308 Service *s = SERVICE(u);
1309 const char *prefix2;
1314 p2 = strappend(prefix, "\t");
1315 prefix2 = p2 ? p2 : prefix;
1318 "%sService State: %s\n"
1320 "%sReload Result: %s\n"
1321 "%sPermissionsStartOnly: %s\n"
1322 "%sRootDirectoryStartOnly: %s\n"
1323 "%sRemainAfterExit: %s\n"
1324 "%sGuessMainPID: %s\n"
1327 "%sNotifyAccess: %s\n",
1328 prefix, service_state_to_string(s->state),
1329 prefix, service_result_to_string(s->result),
1330 prefix, service_result_to_string(s->reload_result),
1331 prefix, yes_no(s->permissions_start_only),
1332 prefix, yes_no(s->root_directory_start_only),
1333 prefix, yes_no(s->remain_after_exit),
1334 prefix, yes_no(s->guess_main_pid),
1335 prefix, service_type_to_string(s->type),
1336 prefix, service_restart_to_string(s->restart),
1337 prefix, notify_access_to_string(s->notify_access));
1339 if (s->control_pid > 0)
1341 "%sControl PID: %lu\n",
1342 prefix, (unsigned long) s->control_pid);
1344 if (s->main_pid > 0)
1347 "%sMain PID Known: %s\n"
1348 "%sMain PID Alien: %s\n",
1349 prefix, (unsigned long) s->main_pid,
1350 prefix, yes_no(s->main_pid_known),
1351 prefix, yes_no(s->main_pid_alien));
1356 prefix, s->pid_file);
1361 "%sBus Name Good: %s\n",
1362 prefix, s->bus_name,
1363 prefix, yes_no(s->bus_name_good));
1365 kill_context_dump(&s->kill_context, f, prefix);
1366 exec_context_dump(&s->exec_context, f, prefix);
1368 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1370 if (!s->exec_command[c])
1373 fprintf(f, "%s-> %s:\n",
1374 prefix, service_exec_command_to_string(c));
1376 exec_command_dump_list(s->exec_command[c], f, prefix2);
1379 #ifdef HAVE_SYSV_COMPAT
1382 "%sSysV Init Script has LSB Header: %s\n"
1383 "%sSysVEnabled: %s\n",
1384 prefix, yes_no(s->sysv_has_lsb),
1385 prefix, yes_no(s->sysv_enabled));
1387 if (s->sysv_start_priority >= 0)
1389 "%sSysVStartPriority: %i\n",
1390 prefix, s->sysv_start_priority);
1392 if (s->sysv_runlevels)
1393 fprintf(f, "%sSysVRunLevels: %s\n",
1394 prefix, s->sysv_runlevels);
1397 if (s->fsck_passno > 0)
1399 "%sFsckPassNo: %i\n",
1400 prefix, s->fsck_passno);
1403 fprintf(f, "%sStatus Text: %s\n",
1404 prefix, s->status_text);
1409 static int service_load_pid_file(Service *s, bool may_warn) {
1419 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1421 log_info("PID file %s not readable (yet?) after %s.",
1422 s->pid_file, service_state_to_string(s->state));
1426 r = parse_pid(k, &pid);
1432 if (kill(pid, 0) < 0 && errno != EPERM) {
1434 log_info("PID %lu read from file %s does not exist.",
1435 (unsigned long) pid, s->pid_file);
1439 if (s->main_pid_known) {
1440 if (pid == s->main_pid)
1443 log_debug("Main PID changing: %lu -> %lu",
1444 (unsigned long) s->main_pid, (unsigned long) pid);
1445 service_unwatch_main_pid(s);
1446 s->main_pid_known = false;
1448 log_debug("Main PID loaded: %lu", (unsigned long) pid);
1450 if ((r = service_set_main_pid(s, pid)) < 0)
1453 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1454 /* FIXME: we need to do something here */
1460 static int service_search_main_pid(Service *s) {
1466 /* If we know it anyway, don't ever fallback to unreliable
1468 if (s->main_pid_known)
1471 if (!s->guess_main_pid)
1474 assert(s->main_pid <= 0);
1476 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1479 log_debug("Main PID guessed: %lu", (unsigned long) pid);
1480 if ((r = service_set_main_pid(s, pid)) < 0)
1483 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1484 /* FIXME: we need to do something here */
1490 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1496 /* Notifies all our sockets when we die */
1498 if (s->socket_fd >= 0)
1501 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1502 if (u->type == UNIT_SOCKET)
1503 socket_notify_service_dead(SOCKET(u), failed_permanent);
1508 static void service_set_state(Service *s, ServiceState state) {
1509 ServiceState old_state;
1510 const UnitActiveState *table;
1513 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1515 old_state = s->state;
1518 service_unwatch_pid_file(s);
1520 if (state != SERVICE_START_PRE &&
1521 state != SERVICE_START &&
1522 state != SERVICE_START_POST &&
1523 state != SERVICE_RELOAD &&
1524 state != SERVICE_STOP &&
1525 state != SERVICE_STOP_SIGTERM &&
1526 state != SERVICE_STOP_SIGKILL &&
1527 state != SERVICE_STOP_POST &&
1528 state != SERVICE_FINAL_SIGTERM &&
1529 state != SERVICE_FINAL_SIGKILL &&
1530 state != SERVICE_AUTO_RESTART)
1531 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1533 if (state != SERVICE_START &&
1534 state != SERVICE_START_POST &&
1535 state != SERVICE_RUNNING &&
1536 state != SERVICE_RELOAD &&
1537 state != SERVICE_STOP &&
1538 state != SERVICE_STOP_SIGTERM &&
1539 state != SERVICE_STOP_SIGKILL) {
1540 service_unwatch_main_pid(s);
1541 s->main_command = NULL;
1544 if (state != SERVICE_START_PRE &&
1545 state != SERVICE_START &&
1546 state != SERVICE_START_POST &&
1547 state != SERVICE_RELOAD &&
1548 state != SERVICE_STOP &&
1549 state != SERVICE_STOP_SIGTERM &&
1550 state != SERVICE_STOP_SIGKILL &&
1551 state != SERVICE_STOP_POST &&
1552 state != SERVICE_FINAL_SIGTERM &&
1553 state != SERVICE_FINAL_SIGKILL) {
1554 service_unwatch_control_pid(s);
1555 s->control_command = NULL;
1556 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1559 if (state == SERVICE_DEAD ||
1560 state == SERVICE_STOP ||
1561 state == SERVICE_STOP_SIGTERM ||
1562 state == SERVICE_STOP_SIGKILL ||
1563 state == SERVICE_STOP_POST ||
1564 state == SERVICE_FINAL_SIGTERM ||
1565 state == SERVICE_FINAL_SIGKILL ||
1566 state == SERVICE_FAILED ||
1567 state == SERVICE_AUTO_RESTART)
1568 service_notify_sockets_dead(s, false);
1570 if (state != SERVICE_START_PRE &&
1571 state != SERVICE_START &&
1572 state != SERVICE_START_POST &&
1573 state != SERVICE_RUNNING &&
1574 state != SERVICE_RELOAD &&
1575 state != SERVICE_STOP &&
1576 state != SERVICE_STOP_SIGTERM &&
1577 state != SERVICE_STOP_SIGKILL &&
1578 state != SERVICE_STOP_POST &&
1579 state != SERVICE_FINAL_SIGTERM &&
1580 state != SERVICE_FINAL_SIGKILL &&
1581 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1582 service_close_socket_fd(s);
1583 service_connection_unref(s);
1586 if (state == SERVICE_STOP)
1587 service_stop_watchdog(s);
1589 /* For the inactive states unit_notify() will trim the cgroup,
1590 * but for exit we have to do that ourselves... */
1591 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1592 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1594 if (old_state != state)
1595 log_debug("%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
1597 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1598 s->reload_result = SERVICE_SUCCESS;
1601 static int service_coldplug(Unit *u) {
1602 Service *s = SERVICE(u);
1606 assert(s->state == SERVICE_DEAD);
1608 if (s->deserialized_state != s->state) {
1610 if (s->deserialized_state == SERVICE_START_PRE ||
1611 s->deserialized_state == SERVICE_START ||
1612 s->deserialized_state == SERVICE_START_POST ||
1613 s->deserialized_state == SERVICE_RELOAD ||
1614 s->deserialized_state == SERVICE_STOP ||
1615 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1616 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1617 s->deserialized_state == SERVICE_STOP_POST ||
1618 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1619 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1620 s->deserialized_state == SERVICE_AUTO_RESTART) {
1621 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1624 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1626 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1631 if ((s->deserialized_state == SERVICE_START &&
1632 (s->type == SERVICE_FORKING ||
1633 s->type == SERVICE_DBUS ||
1634 s->type == SERVICE_ONESHOT ||
1635 s->type == SERVICE_NOTIFY)) ||
1636 s->deserialized_state == SERVICE_START_POST ||
1637 s->deserialized_state == SERVICE_RUNNING ||
1638 s->deserialized_state == SERVICE_RELOAD ||
1639 s->deserialized_state == SERVICE_STOP ||
1640 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1641 s->deserialized_state == SERVICE_STOP_SIGKILL)
1642 if (s->main_pid > 0)
1643 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1646 if (s->deserialized_state == SERVICE_START_PRE ||
1647 s->deserialized_state == SERVICE_START ||
1648 s->deserialized_state == SERVICE_START_POST ||
1649 s->deserialized_state == SERVICE_RELOAD ||
1650 s->deserialized_state == SERVICE_STOP ||
1651 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1652 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1653 s->deserialized_state == SERVICE_STOP_POST ||
1654 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1655 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1656 if (s->control_pid > 0)
1657 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1660 if (s->deserialized_state == SERVICE_START_POST ||
1661 s->deserialized_state == SERVICE_RUNNING)
1662 service_handle_watchdog(s);
1664 service_set_state(s, s->deserialized_state);
1669 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1673 unsigned rn_fds = 0;
1680 if (s->socket_fd >= 0)
1683 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1688 if (u->type != UNIT_SOCKET)
1693 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1705 if (!(t = new(int, rn_fds+cn_fds))) {
1711 memcpy(t, rfds, rn_fds * sizeof(int));
1712 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1717 rn_fds = rn_fds+cn_fds;
1732 static int service_spawn(
1737 bool apply_permissions,
1739 bool apply_tty_stdin,
1740 bool set_notify_socket,
1746 int *fds = NULL, *fdsbuf = NULL;
1747 unsigned n_fds = 0, n_env = 0;
1748 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1755 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1756 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1757 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1759 if (s->socket_fd >= 0) {
1760 fds = &s->socket_fd;
1763 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1770 if (timeout && s->timeout_start_usec) {
1771 r = unit_watch_timer(UNIT(s), s->timeout_start_usec, &s->timer_watch);
1775 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1777 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1782 if (!(our_env = new0(char*, 4))) {
1787 if (set_notify_socket)
1788 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1793 if (s->main_pid > 0)
1794 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1799 if (s->watchdog_usec > 0)
1800 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1805 if (!(final_env = strv_env_merge(2,
1806 UNIT(s)->manager->environment,
1821 UNIT(s)->manager->confirm_spawn,
1822 UNIT(s)->cgroup_bondings,
1823 UNIT(s)->cgroup_attributes,
1824 is_control ? "control" : NULL,
1826 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1833 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1834 /* FIXME: we need to do something here */
1840 strv_free(final_env);
1850 strv_free(final_env);
1853 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1858 static int main_pid_good(Service *s) {
1861 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1864 /* If we know the pid file, then lets just check if it is
1866 if (s->main_pid_known) {
1868 /* If it's an alien child let's check if it is still
1870 if (s->main_pid_alien)
1871 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1873 /* .. otherwise assume we'll get a SIGCHLD for it,
1874 * which we really should wait for to collect exit
1875 * status and code */
1876 return s->main_pid > 0;
1879 /* We don't know the pid */
1883 static int control_pid_good(Service *s) {
1886 return s->control_pid > 0;
1889 static int cgroup_good(Service *s) {
1894 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1900 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1904 if (f != SERVICE_SUCCESS)
1907 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1909 if (allow_restart &&
1910 !s->forbid_restart &&
1911 (s->restart == SERVICE_RESTART_ALWAYS ||
1912 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1913 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1914 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1915 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1916 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1917 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1918 (s->result != SERVICE_FAILURE_SIGNAL ||
1919 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1922 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
1926 service_set_state(s, SERVICE_AUTO_RESTART);
1929 s->forbid_restart = false;
1934 log_warning("%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1935 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1938 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1940 static void service_enter_stop_post(Service *s, ServiceResult f) {
1944 if (f != SERVICE_SUCCESS)
1947 service_unwatch_control_pid(s);
1949 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1950 s->control_command_id = SERVICE_EXEC_STOP_POST;
1952 r = service_spawn(s,
1956 !s->permissions_start_only,
1957 !s->root_directory_start_only,
1966 service_set_state(s, SERVICE_STOP_POST);
1968 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1973 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
1974 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1977 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1979 Set *pid_set = NULL;
1980 bool wait_for_exit = false;
1984 if (f != SERVICE_SUCCESS)
1987 if (s->kill_context.kill_mode != KILL_NONE) {
1988 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->kill_context.kill_signal : SIGKILL;
1990 if (s->main_pid > 0) {
1991 if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
1992 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1994 wait_for_exit = !s->main_pid_alien;
1997 if (s->control_pid > 0) {
1998 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1999 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
2001 wait_for_exit = true;
2004 if (s->kill_context.kill_mode == KILL_CONTROL_GROUP) {
2006 pid_set = set_new(trivial_hash_func, trivial_compare_func);
2012 /* Exclude the main/control pids from being killed via the cgroup */
2013 if (s->main_pid > 0)
2014 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
2017 if (s->control_pid > 0)
2018 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
2021 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
2023 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
2024 log_warning("Failed to kill control group: %s", strerror(-r));
2026 wait_for_exit = true;
2033 if (wait_for_exit) {
2034 if (s->timeout_stop_usec > 0) {
2035 r = unit_watch_timer(UNIT(s), s->timeout_stop_usec, &s->timer_watch);
2040 service_set_state(s, state);
2041 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2042 service_enter_stop_post(s, SERVICE_SUCCESS);
2044 service_enter_dead(s, SERVICE_SUCCESS, true);
2049 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2051 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2052 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2054 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2060 static void service_enter_stop(Service *s, ServiceResult f) {
2065 if (f != SERVICE_SUCCESS)
2068 service_unwatch_control_pid(s);
2070 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2071 s->control_command_id = SERVICE_EXEC_STOP;
2073 r = service_spawn(s,
2077 !s->permissions_start_only,
2078 !s->root_directory_start_only,
2086 service_set_state(s, SERVICE_STOP);
2088 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2093 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2094 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2097 static void service_enter_running(Service *s, ServiceResult f) {
2098 int main_pid_ok, cgroup_ok;
2101 if (f != SERVICE_SUCCESS)
2104 main_pid_ok = main_pid_good(s);
2105 cgroup_ok = cgroup_good(s);
2107 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2108 (s->bus_name_good || s->type != SERVICE_DBUS))
2109 service_set_state(s, SERVICE_RUNNING);
2110 else if (s->remain_after_exit)
2111 service_set_state(s, SERVICE_EXITED);
2113 service_enter_stop(s, SERVICE_SUCCESS);
2116 static void service_enter_start_post(Service *s) {
2120 service_unwatch_control_pid(s);
2122 if (s->watchdog_usec > 0)
2123 service_reset_watchdog(s);
2125 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2126 s->control_command_id = SERVICE_EXEC_START_POST;
2128 r = service_spawn(s,
2132 !s->permissions_start_only,
2133 !s->root_directory_start_only,
2141 service_set_state(s, SERVICE_START_POST);
2143 service_enter_running(s, SERVICE_SUCCESS);
2148 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2149 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2152 static void service_enter_start(Service *s) {
2159 assert(s->exec_command[SERVICE_EXEC_START]);
2160 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2162 if (s->type == SERVICE_FORKING)
2163 service_unwatch_control_pid(s);
2165 service_unwatch_main_pid(s);
2167 /* We want to ensure that nobody leaks processes from
2168 * START_PRE here, so let's go on a killing spree, People
2169 * should not spawn long running processes from START_PRE. */
2170 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2172 if (s->type == SERVICE_FORKING) {
2173 s->control_command_id = SERVICE_EXEC_START;
2174 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2176 s->main_command = NULL;
2178 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2179 s->control_command = NULL;
2181 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2184 r = service_spawn(s,
2186 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2191 s->notify_access != NOTIFY_NONE,
2197 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2198 /* For simple services we immediately start
2199 * the START_POST binaries. */
2201 service_set_main_pid(s, pid);
2202 service_enter_start_post(s);
2204 } else if (s->type == SERVICE_FORKING) {
2206 /* For forking services we wait until the start
2207 * process exited. */
2209 s->control_pid = pid;
2210 service_set_state(s, SERVICE_START);
2212 } else if (s->type == SERVICE_ONESHOT ||
2213 s->type == SERVICE_DBUS ||
2214 s->type == SERVICE_NOTIFY) {
2216 /* For oneshot services we wait until the start
2217 * process exited, too, but it is our main process. */
2219 /* For D-Bus services we know the main pid right away,
2220 * but wait for the bus name to appear on the
2221 * bus. Notify services are similar. */
2223 service_set_main_pid(s, pid);
2224 service_set_state(s, SERVICE_START);
2226 assert_not_reached("Unknown service type");
2231 log_warning("%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2232 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2235 static void service_enter_start_pre(Service *s) {
2240 service_unwatch_control_pid(s);
2242 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2244 /* Before we start anything, let's clear up what might
2245 * be left from previous runs. */
2246 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2248 s->control_command_id = SERVICE_EXEC_START_PRE;
2250 r = service_spawn(s,
2254 !s->permissions_start_only,
2255 !s->root_directory_start_only,
2263 service_set_state(s, SERVICE_START_PRE);
2265 service_enter_start(s);
2270 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2271 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2274 static void service_enter_restart(Service *s) {
2279 dbus_error_init(&error);
2281 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2282 /* Don't restart things if we are going down anyway */
2283 log_info("Stop job pending for unit, delaying automatic restart.");
2285 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
2292 /* Any units that are bound to this service must also be
2293 * restarted. We use JOB_RESTART (instead of the more obvious
2294 * JOB_START) here so that those dependency jobs will be added
2296 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2300 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2301 * it will be canceled as part of the service_stop() call that
2302 * is executed as part of JOB_RESTART. */
2304 log_debug("%s scheduled restart job.", UNIT(s)->id);
2308 log_warning("%s failed to schedule restart job: %s", UNIT(s)->id, bus_error(&error, -r));
2309 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2311 dbus_error_free(&error);
2314 static void service_enter_reload(Service *s) {
2319 service_unwatch_control_pid(s);
2321 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2322 s->control_command_id = SERVICE_EXEC_RELOAD;
2324 r = service_spawn(s,
2328 !s->permissions_start_only,
2329 !s->root_directory_start_only,
2337 service_set_state(s, SERVICE_RELOAD);
2339 service_enter_running(s, SERVICE_SUCCESS);
2344 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
2345 s->reload_result = SERVICE_FAILURE_RESOURCES;
2346 service_enter_running(s, SERVICE_SUCCESS);
2349 static void service_run_next_control(Service *s) {
2353 assert(s->control_command);
2354 assert(s->control_command->command_next);
2356 assert(s->control_command_id != SERVICE_EXEC_START);
2358 s->control_command = s->control_command->command_next;
2359 service_unwatch_control_pid(s);
2361 r = service_spawn(s,
2365 !s->permissions_start_only,
2366 !s->root_directory_start_only,
2367 s->control_command_id == SERVICE_EXEC_START_PRE ||
2368 s->control_command_id == SERVICE_EXEC_STOP_POST,
2378 log_warning("%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
2380 if (s->state == SERVICE_START_PRE)
2381 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2382 else if (s->state == SERVICE_STOP)
2383 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2384 else if (s->state == SERVICE_STOP_POST)
2385 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2386 else if (s->state == SERVICE_RELOAD) {
2387 s->reload_result = SERVICE_FAILURE_RESOURCES;
2388 service_enter_running(s, SERVICE_SUCCESS);
2390 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2393 static void service_run_next_main(Service *s) {
2398 assert(s->main_command);
2399 assert(s->main_command->command_next);
2400 assert(s->type == SERVICE_ONESHOT);
2402 s->main_command = s->main_command->command_next;
2403 service_unwatch_main_pid(s);
2405 r = service_spawn(s,
2412 s->notify_access != NOTIFY_NONE,
2418 service_set_main_pid(s, pid);
2423 log_warning("%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2424 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2427 static int service_start_limit_test(Service *s) {
2430 if (ratelimit_test(&s->start_limit))
2433 switch (s->start_limit_action) {
2435 case SERVICE_START_LIMIT_NONE:
2436 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s)->id);
2439 case SERVICE_START_LIMIT_REBOOT: {
2443 dbus_error_init(&error);
2445 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2447 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2449 log_error("Failed to reboot: %s.", bus_error(&error, r));
2450 dbus_error_free(&error);
2456 case SERVICE_START_LIMIT_REBOOT_FORCE:
2457 log_warning("%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2458 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2461 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2462 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2463 reboot(RB_AUTOBOOT);
2467 log_error("start limit action=%i", s->start_limit_action);
2468 assert_not_reached("Unknown StartLimitAction.");
2474 static int service_start(Unit *u) {
2475 Service *s = SERVICE(u);
2480 /* We cannot fulfill this request right now, try again later
2482 if (s->state == SERVICE_STOP ||
2483 s->state == SERVICE_STOP_SIGTERM ||
2484 s->state == SERVICE_STOP_SIGKILL ||
2485 s->state == SERVICE_STOP_POST ||
2486 s->state == SERVICE_FINAL_SIGTERM ||
2487 s->state == SERVICE_FINAL_SIGKILL)
2490 /* Already on it! */
2491 if (s->state == SERVICE_START_PRE ||
2492 s->state == SERVICE_START ||
2493 s->state == SERVICE_START_POST)
2496 /* A service that will be restarted must be stopped first to
2497 * trigger BindsTo and/or OnFailure dependencies. If a user
2498 * does not want to wait for the holdoff time to elapse, the
2499 * service should be manually restarted, not started. We
2500 * simply return EAGAIN here, so that any start jobs stay
2501 * queued, and assume that the auto restart timer will
2502 * eventually trigger the restart. */
2503 if (s->state == SERVICE_AUTO_RESTART)
2506 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2508 /* Make sure we don't enter a busy loop of some kind. */
2509 r = service_start_limit_test(s);
2511 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2515 s->result = SERVICE_SUCCESS;
2516 s->reload_result = SERVICE_SUCCESS;
2517 s->main_pid_known = false;
2518 s->main_pid_alien = false;
2519 s->forbid_restart = false;
2521 service_enter_start_pre(s);
2525 static int service_stop(Unit *u) {
2526 Service *s = SERVICE(u);
2530 /* Don't create restart jobs from here. */
2531 s->forbid_restart = true;
2534 if (s->state == SERVICE_STOP ||
2535 s->state == SERVICE_STOP_SIGTERM ||
2536 s->state == SERVICE_STOP_SIGKILL ||
2537 s->state == SERVICE_STOP_POST ||
2538 s->state == SERVICE_FINAL_SIGTERM ||
2539 s->state == SERVICE_FINAL_SIGKILL)
2542 /* A restart will be scheduled or is in progress. */
2543 if (s->state == SERVICE_AUTO_RESTART) {
2544 service_set_state(s, SERVICE_DEAD);
2548 /* If there's already something running we go directly into
2550 if (s->state == SERVICE_START_PRE ||
2551 s->state == SERVICE_START ||
2552 s->state == SERVICE_START_POST ||
2553 s->state == SERVICE_RELOAD) {
2554 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2558 assert(s->state == SERVICE_RUNNING ||
2559 s->state == SERVICE_EXITED);
2561 service_enter_stop(s, SERVICE_SUCCESS);
2565 static int service_reload(Unit *u) {
2566 Service *s = SERVICE(u);
2570 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2572 service_enter_reload(s);
2576 static bool service_can_reload(Unit *u) {
2577 Service *s = SERVICE(u);
2581 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2584 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2585 Service *s = SERVICE(u);
2591 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2592 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2593 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2595 if (s->control_pid > 0)
2596 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2598 if (s->main_pid_known && s->main_pid > 0)
2599 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2601 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2604 unit_serialize_item(u, f, "status-text", s->status_text);
2606 /* FIXME: There's a minor uncleanliness here: if there are
2607 * multiple commands attached here, we will start from the
2608 * first one again */
2609 if (s->control_command_id >= 0)
2610 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2612 if (s->socket_fd >= 0) {
2615 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2618 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2621 if (s->main_exec_status.pid > 0) {
2622 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2623 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2624 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2626 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2627 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2628 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2631 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2632 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2637 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2638 Service *s = SERVICE(u);
2645 if (streq(key, "state")) {
2648 if ((state = service_state_from_string(value)) < 0)
2649 log_debug("Failed to parse state value %s", value);
2651 s->deserialized_state = state;
2652 } else if (streq(key, "result")) {
2655 f = service_result_from_string(value);
2657 log_debug("Failed to parse result value %s", value);
2658 else if (f != SERVICE_SUCCESS)
2661 } else if (streq(key, "reload-result")) {
2664 f = service_result_from_string(value);
2666 log_debug("Failed to parse reload result value %s", value);
2667 else if (f != SERVICE_SUCCESS)
2668 s->reload_result = f;
2670 } else if (streq(key, "control-pid")) {
2673 if (parse_pid(value, &pid) < 0)
2674 log_debug("Failed to parse control-pid value %s", value);
2676 s->control_pid = pid;
2677 } else if (streq(key, "main-pid")) {
2680 if (parse_pid(value, &pid) < 0)
2681 log_debug("Failed to parse main-pid value %s", value);
2683 service_set_main_pid(s, (pid_t) pid);
2684 } else if (streq(key, "main-pid-known")) {
2687 if ((b = parse_boolean(value)) < 0)
2688 log_debug("Failed to parse main-pid-known value %s", value);
2690 s->main_pid_known = b;
2691 } else if (streq(key, "status-text")) {
2694 if ((t = strdup(value))) {
2695 free(s->status_text);
2699 } else if (streq(key, "control-command")) {
2700 ServiceExecCommand id;
2702 if ((id = service_exec_command_from_string(value)) < 0)
2703 log_debug("Failed to parse exec-command value %s", value);
2705 s->control_command_id = id;
2706 s->control_command = s->exec_command[id];
2708 } else if (streq(key, "socket-fd")) {
2711 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2712 log_debug("Failed to parse socket-fd value %s", value);
2715 if (s->socket_fd >= 0)
2716 close_nointr_nofail(s->socket_fd);
2717 s->socket_fd = fdset_remove(fds, fd);
2719 } else if (streq(key, "main-exec-status-pid")) {
2722 if (parse_pid(value, &pid) < 0)
2723 log_debug("Failed to parse main-exec-status-pid value %s", value);
2725 s->main_exec_status.pid = pid;
2726 } else if (streq(key, "main-exec-status-code")) {
2729 if (safe_atoi(value, &i) < 0)
2730 log_debug("Failed to parse main-exec-status-code value %s", value);
2732 s->main_exec_status.code = i;
2733 } else if (streq(key, "main-exec-status-status")) {
2736 if (safe_atoi(value, &i) < 0)
2737 log_debug("Failed to parse main-exec-status-status value %s", value);
2739 s->main_exec_status.status = i;
2740 } else if (streq(key, "main-exec-status-start"))
2741 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2742 else if (streq(key, "main-exec-status-exit"))
2743 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2744 else if (streq(key, "watchdog-timestamp"))
2745 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2747 log_debug("Unknown serialization key '%s'", key);
2752 static UnitActiveState service_active_state(Unit *u) {
2753 const UnitActiveState *table;
2757 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2759 return table[SERVICE(u)->state];
2762 static const char *service_sub_state_to_string(Unit *u) {
2765 return service_state_to_string(SERVICE(u)->state);
2768 static bool service_check_gc(Unit *u) {
2769 Service *s = SERVICE(u);
2773 /* Never clean up services that still have a process around,
2774 * even if the service is formally dead. */
2775 if (cgroup_good(s) > 0 ||
2776 main_pid_good(s) > 0 ||
2777 control_pid_good(s) > 0)
2780 #ifdef HAVE_SYSV_COMPAT
2788 static bool service_check_snapshot(Unit *u) {
2789 Service *s = SERVICE(u);
2793 return !s->got_socket_fd;
2796 static int service_retry_pid_file(Service *s) {
2799 assert(s->pid_file);
2800 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2802 r = service_load_pid_file(s, false);
2806 service_unwatch_pid_file(s);
2808 service_enter_running(s, SERVICE_SUCCESS);
2812 static int service_watch_pid_file(Service *s) {
2815 log_debug("Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
2816 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2820 /* the pidfile might have appeared just before we set the watch */
2821 service_retry_pid_file(s);
2825 log_error("Failed to set a watch for %s's PID file %s: %s",
2826 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2827 service_unwatch_pid_file(s);
2831 static int service_demand_pid_file(Service *s) {
2834 assert(s->pid_file);
2835 assert(!s->pid_file_pathspec);
2837 ps = new0(PathSpec, 1);
2841 ps->path = strdup(s->pid_file);
2847 path_kill_slashes(ps->path);
2849 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2850 * keep their PID file open all the time. */
2851 ps->type = PATH_MODIFIED;
2852 ps->inotify_fd = -1;
2854 s->pid_file_pathspec = ps;
2856 return service_watch_pid_file(s);
2859 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2860 Service *s = SERVICE(u);
2864 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2865 assert(s->pid_file_pathspec);
2866 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2868 log_debug("inotify event for %s", u->id);
2870 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2873 if (service_retry_pid_file(s) == 0)
2876 if (service_watch_pid_file(s) < 0)
2881 service_unwatch_pid_file(s);
2882 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2885 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2886 Service *s = SERVICE(u);
2892 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2893 is_clean_exit_lsb(code, status, &s->success_status))
2894 f = SERVICE_SUCCESS;
2895 else if (code == CLD_EXITED)
2896 f = SERVICE_FAILURE_EXIT_CODE;
2897 else if (code == CLD_KILLED)
2898 f = SERVICE_FAILURE_SIGNAL;
2899 else if (code == CLD_DUMPED)
2900 f = SERVICE_FAILURE_CORE_DUMP;
2902 assert_not_reached("Unknown code");
2904 if (s->main_pid == pid) {
2905 /* Forking services may occasionally move to a new PID.
2906 * As long as they update the PID file before exiting the old
2907 * PID, they're fine. */
2908 if (service_load_pid_file(s, false) == 0)
2912 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2914 /* If this is not a forking service than the main
2915 * process got started and hence we copy the exit
2916 * status so that it is recorded both as main and as
2917 * control process exit status */
2918 if (s->main_command) {
2919 s->main_command->exec_status = s->main_exec_status;
2921 if (s->main_command->ignore)
2922 f = SERVICE_SUCCESS;
2925 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2926 "%s: main process exited, code=%s, status=%i", u->id, sigchld_code_to_string(code), status);
2928 if (f != SERVICE_SUCCESS)
2931 if (s->main_command &&
2932 s->main_command->command_next &&
2933 f == SERVICE_SUCCESS) {
2935 /* There is another command to *
2936 * execute, so let's do that. */
2938 log_debug("%s running next main command for state %s", u->id, service_state_to_string(s->state));
2939 service_run_next_main(s);
2943 /* The service exited, so the service is officially
2945 s->main_command = NULL;
2949 case SERVICE_START_POST:
2950 case SERVICE_RELOAD:
2952 /* Need to wait until the operation is
2957 if (s->type == SERVICE_ONESHOT) {
2958 /* This was our main goal, so let's go on */
2959 if (f == SERVICE_SUCCESS)
2960 service_enter_start_post(s);
2962 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2968 case SERVICE_RUNNING:
2969 service_enter_running(s, f);
2972 case SERVICE_STOP_SIGTERM:
2973 case SERVICE_STOP_SIGKILL:
2975 if (!control_pid_good(s))
2976 service_enter_stop_post(s, f);
2978 /* If there is still a control process, wait for that first */
2982 assert_not_reached("Uh, main process died at wrong time.");
2986 } else if (s->control_pid == pid) {
2990 if (s->control_command) {
2991 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2993 if (s->control_command->ignore)
2994 f = SERVICE_SUCCESS;
2997 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2998 "%s: control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
3000 if (f != SERVICE_SUCCESS)
3003 /* Immediately get rid of the cgroup, so that the
3004 * kernel doesn't delay the cgroup empty messages for
3005 * the service cgroup any longer than necessary */
3006 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
3008 if (s->control_command &&
3009 s->control_command->command_next &&
3010 f == SERVICE_SUCCESS) {
3012 /* There is another command to *
3013 * execute, so let's do that. */
3015 log_debug("%s running next control command for state %s", u->id, service_state_to_string(s->state));
3016 service_run_next_control(s);
3019 /* No further commands for this step, so let's
3020 * figure out what to do next */
3022 s->control_command = NULL;
3023 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3025 log_debug("%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
3029 case SERVICE_START_PRE:
3030 if (f == SERVICE_SUCCESS)
3031 service_enter_start(s);
3033 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3037 if (s->type != SERVICE_FORKING)
3038 /* Maybe spurious event due to a reload that changed the type? */
3041 if (f != SERVICE_SUCCESS) {
3042 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3047 bool has_start_post;
3050 /* Let's try to load the pid file here if we can.
3051 * The PID file might actually be created by a START_POST
3052 * script. In that case don't worry if the loading fails. */
3054 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3055 r = service_load_pid_file(s, !has_start_post);
3056 if (!has_start_post && r < 0) {
3057 r = service_demand_pid_file(s);
3058 if (r < 0 || !cgroup_good(s))
3059 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3063 service_search_main_pid(s);
3065 service_enter_start_post(s);
3068 case SERVICE_START_POST:
3069 if (f != SERVICE_SUCCESS) {
3070 service_enter_stop(s, f);
3077 r = service_load_pid_file(s, true);
3079 r = service_demand_pid_file(s);
3080 if (r < 0 || !cgroup_good(s))
3081 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3085 service_search_main_pid(s);
3087 service_enter_running(s, SERVICE_SUCCESS);
3090 case SERVICE_RELOAD:
3091 if (f == SERVICE_SUCCESS) {
3092 service_load_pid_file(s, true);
3093 service_search_main_pid(s);
3096 s->reload_result = f;
3097 service_enter_running(s, SERVICE_SUCCESS);
3101 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3104 case SERVICE_STOP_SIGTERM:
3105 case SERVICE_STOP_SIGKILL:
3106 if (main_pid_good(s) <= 0)
3107 service_enter_stop_post(s, f);
3109 /* If there is still a service
3110 * process around, wait until
3111 * that one quit, too */
3114 case SERVICE_STOP_POST:
3115 case SERVICE_FINAL_SIGTERM:
3116 case SERVICE_FINAL_SIGKILL:
3117 service_enter_dead(s, f, true);
3121 assert_not_reached("Uh, control process died at wrong time.");
3126 /* Notify clients about changed exit status */
3127 unit_add_to_dbus_queue(u);
3130 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3131 Service *s = SERVICE(u);
3134 assert(elapsed == 1);
3136 if (w == &s->watchdog_watch) {
3137 service_handle_watchdog(s);
3141 assert(w == &s->timer_watch);
3145 case SERVICE_START_PRE:
3147 log_warning("%s operation timed out. Terminating.", u->id);
3148 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3151 case SERVICE_START_POST:
3152 log_warning("%s operation timed out. Stopping.", u->id);
3153 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3156 case SERVICE_RELOAD:
3157 log_warning("%s operation timed out. Stopping.", u->id);
3158 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3159 service_enter_running(s, SERVICE_SUCCESS);
3163 log_warning("%s stopping timed out. Terminating.", u->id);
3164 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3167 case SERVICE_STOP_SIGTERM:
3168 if (s->kill_context.send_sigkill) {
3169 log_warning("%s stopping timed out. Killing.", u->id);
3170 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3172 log_warning("%s stopping timed out. Skipping SIGKILL.", u->id);
3173 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3178 case SERVICE_STOP_SIGKILL:
3179 /* Uh, we sent a SIGKILL and it is still not gone?
3180 * Must be something we cannot kill, so let's just be
3181 * weirded out and continue */
3183 log_warning("%s still around after SIGKILL. Ignoring.", u->id);
3184 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3187 case SERVICE_STOP_POST:
3188 log_warning("%s stopping timed out (2). Terminating.", u->id);
3189 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3192 case SERVICE_FINAL_SIGTERM:
3193 if (s->kill_context.send_sigkill) {
3194 log_warning("%s stopping timed out (2). Killing.", u->id);
3195 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3197 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->id);
3198 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3203 case SERVICE_FINAL_SIGKILL:
3204 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->id);
3205 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3208 case SERVICE_AUTO_RESTART:
3209 log_info("%s holdoff time over, scheduling restart.", u->id);
3210 service_enter_restart(s);
3214 assert_not_reached("Timeout at wrong time.");
3218 static void service_cgroup_notify_event(Unit *u) {
3219 Service *s = SERVICE(u);
3223 log_debug("%s: cgroup is empty", u->id);
3227 /* Waiting for SIGCHLD is usually more interesting,
3228 * because it includes return codes/signals. Which is
3229 * why we ignore the cgroup events for most cases,
3230 * except when we don't know pid which to expect the
3234 case SERVICE_START_POST:
3235 /* If we were hoping for the daemon to write its PID file,
3236 * we can give up now. */
3237 if (s->pid_file_pathspec) {
3238 log_warning("%s never wrote its PID file. Failing.", UNIT(s)->id);
3239 service_unwatch_pid_file(s);
3240 if (s->state == SERVICE_START)
3241 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3243 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3247 case SERVICE_RUNNING:
3248 /* service_enter_running() will figure out what to do */
3249 service_enter_running(s, SERVICE_SUCCESS);
3252 case SERVICE_STOP_SIGTERM:
3253 case SERVICE_STOP_SIGKILL:
3255 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3256 service_enter_stop_post(s, SERVICE_SUCCESS);
3260 case SERVICE_FINAL_SIGTERM:
3261 case SERVICE_FINAL_SIGKILL:
3262 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3263 service_enter_dead(s, SERVICE_SUCCESS, true);
3272 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3273 Service *s = SERVICE(u);
3278 if (s->notify_access == NOTIFY_NONE) {
3279 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3280 u->id, (unsigned long) pid);
3284 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3285 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3286 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3290 log_debug("%s: Got message", u->id);
3292 /* Interpret MAINPID= */
3293 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3294 (s->state == SERVICE_START ||
3295 s->state == SERVICE_START_POST ||
3296 s->state == SERVICE_RUNNING ||
3297 s->state == SERVICE_RELOAD)) {
3299 if (parse_pid(e + 8, &pid) < 0)
3300 log_warning("Failed to parse notification message %s", e);
3302 log_debug("%s: got %s", u->id, e);
3303 service_set_main_pid(s, pid);
3307 /* Interpret READY= */
3308 if (s->type == SERVICE_NOTIFY &&
3309 s->state == SERVICE_START &&
3310 strv_find(tags, "READY=1")) {
3311 log_debug("%s: got READY=1", u->id);
3313 service_enter_start_post(s);
3316 /* Interpret STATUS= */
3317 e = strv_find_prefix(tags, "STATUS=");
3323 if (!utf8_is_valid(e+7)) {
3324 log_warning("Status message in notification is not UTF-8 clean.");
3330 log_error("Failed to allocate string.");
3334 log_debug("%s: got %s", u->id, e);
3336 free(s->status_text);
3339 free(s->status_text);
3340 s->status_text = NULL;
3344 if (strv_find(tags, "WATCHDOG=1")) {
3345 log_debug("%s: got WATCHDOG=1", u->id);
3346 service_reset_watchdog(s);
3349 /* Notify clients about changed status or main pid */
3350 unit_add_to_dbus_queue(u);
3353 #ifdef HAVE_SYSV_COMPAT
3356 static void sysv_facility_in_insserv_conf(Manager *mgr) {
3360 if (!(f = fopen("/etc/insserv.conf", "re"))) {
3361 r = errno == ENOENT ? 0 : -errno;
3366 char l[LINE_MAX], *t;
3367 char **parsed = NULL;
3369 if (!fgets(l, sizeof(l), f)) {
3374 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r));
3379 if (*t != '$' && *t != '<')
3382 parsed = strv_split(t,WHITESPACE);
3383 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3384 if (parsed && !startswith_no_case (parsed[0], "<interactive>")) {
3387 if (sysv_translate_facility(parsed[0], NULL, &facility) < 0)
3389 if ((u = manager_get_unit(mgr, facility)) && (u->type == UNIT_TARGET)) {
3391 char *dep = NULL, *name, **j;
3393 STRV_FOREACH (j, parsed+1) {
3402 if (sysv_translate_facility(name, NULL, &dep) < 0)
3405 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, e, dep, NULL, true);
3420 static int service_enumerate(Manager *m) {
3424 char *path = NULL, *fpath = NULL, *name = NULL;
3425 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3432 if (m->running_as != MANAGER_SYSTEM)
3435 zero(runlevel_services);
3437 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3438 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3442 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3451 if (!(d = opendir(path))) {
3452 if (errno != ENOENT)
3453 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3458 while ((de = readdir(d))) {
3461 if (ignore_file(de->d_name))
3464 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3467 if (strlen(de->d_name) < 4)
3470 a = undecchar(de->d_name[1]);
3471 b = undecchar(de->d_name[2]);
3477 fpath = strjoin(path, "/", de->d_name, NULL);
3483 if (access(fpath, X_OK) < 0) {
3485 if (errno != ENOENT)
3486 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3492 if (!(name = sysv_translate_name(de->d_name + 3))) {
3497 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
3498 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3502 if (de->d_name[0] == 'S') {
3504 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
3505 SERVICE(service)->sysv_start_priority_from_rcnd =
3506 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3508 SERVICE(service)->sysv_enabled = true;
3511 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3514 if ((r = set_put(runlevel_services[i], service)) < 0)
3517 } else if (de->d_name[0] == 'K' &&
3518 (rcnd_table[i].type == RUNLEVEL_DOWN ||
3519 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
3521 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3524 if ((r = set_put(shutdown_services, service)) < 0)
3530 /* Now we loaded all stubs and are aware of the lowest
3531 start-up priority for all services, not let's actually load
3532 the services, this will also tell us which services are
3533 actually native now */
3534 manager_dispatch_load_queue(m);
3536 /* If this is a native service, rely on native ways to pull in
3537 * a service, don't pull it in via sysv rcN.d links. */
3538 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3539 SET_FOREACH(service, runlevel_services[i], j) {
3540 service = unit_follow_merge(service);
3542 if (service->fragment_path)
3545 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3549 /* We honour K links only for halt/reboot. For the normal
3550 * runlevels we assume the stop jobs will be implicitly added
3551 * by the core logic. Also, we don't really distinguish here
3552 * between the runlevels 0 and 6 and just add them to the
3553 * special shutdown target. On SUSE the boot.d/ runlevel is
3554 * also used for shutdown, so we add links for that too to the
3555 * shutdown target.*/
3556 SET_FOREACH(service, shutdown_services, j) {
3557 service = unit_follow_merge(service);
3559 if (service->fragment_path)
3562 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3569 sysv_facility_in_insserv_conf (m);
3577 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3578 set_free(runlevel_services[i]);
3579 set_free(shutdown_services);
3588 static void service_bus_name_owner_change(
3591 const char *old_owner,
3592 const char *new_owner) {
3594 Service *s = SERVICE(u);
3599 assert(streq(s->bus_name, name));
3600 assert(old_owner || new_owner);
3602 if (old_owner && new_owner)
3603 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
3605 log_debug("%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
3607 log_debug("%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
3609 s->bus_name_good = !!new_owner;
3611 if (s->type == SERVICE_DBUS) {
3613 /* service_enter_running() will figure out what to
3615 if (s->state == SERVICE_RUNNING)
3616 service_enter_running(s, SERVICE_SUCCESS);
3617 else if (s->state == SERVICE_START && new_owner)
3618 service_enter_start_post(s);
3620 } else if (new_owner &&
3622 (s->state == SERVICE_START ||
3623 s->state == SERVICE_START_POST ||
3624 s->state == SERVICE_RUNNING ||
3625 s->state == SERVICE_RELOAD)) {
3627 /* Try to acquire PID from bus service */
3628 log_debug("Trying to acquire PID from D-Bus name...");
3630 bus_query_pid(u->manager, name);
3634 static void service_bus_query_pid_done(
3639 Service *s = SERVICE(u);
3644 log_debug("%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3646 if (s->main_pid <= 0 &&
3647 (s->state == SERVICE_START ||
3648 s->state == SERVICE_START_POST ||
3649 s->state == SERVICE_RUNNING ||
3650 s->state == SERVICE_RELOAD))
3651 service_set_main_pid(s, pid);
3654 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3659 /* This is called by the socket code when instantiating a new
3660 * service for a stream socket and the socket needs to be
3663 if (UNIT(s)->load_state != UNIT_LOADED)
3666 if (s->socket_fd >= 0)
3669 if (s->state != SERVICE_DEAD)
3673 s->got_socket_fd = true;
3675 unit_ref_set(&s->accept_socket, UNIT(sock));
3677 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3680 static void service_reset_failed(Unit *u) {
3681 Service *s = SERVICE(u);
3685 if (s->state == SERVICE_FAILED)
3686 service_set_state(s, SERVICE_DEAD);
3688 s->result = SERVICE_SUCCESS;
3689 s->reload_result = SERVICE_SUCCESS;
3691 RATELIMIT_RESET(s->start_limit);
3694 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3695 Service *s = SERVICE(u);
3697 Set *pid_set = NULL;
3701 if (s->main_pid <= 0 && who == KILL_MAIN) {
3702 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3706 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3707 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3711 if (who == KILL_CONTROL || who == KILL_ALL)
3712 if (s->control_pid > 0)
3713 if (kill(s->control_pid, signo) < 0)
3716 if (who == KILL_MAIN || who == KILL_ALL)
3717 if (s->main_pid > 0)
3718 if (kill(s->main_pid, signo) < 0)
3721 if (who == KILL_ALL) {
3724 pid_set = set_new(trivial_hash_func, trivial_compare_func);
3728 /* Exclude the control/main pid from being killed via the cgroup */
3729 if (s->control_pid > 0) {
3730 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
3737 if (s->main_pid > 0) {
3738 q = set_put(pid_set, LONG_TO_PTR(s->main_pid));
3745 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3746 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3757 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3758 [SERVICE_DEAD] = "dead",
3759 [SERVICE_START_PRE] = "start-pre",
3760 [SERVICE_START] = "start",
3761 [SERVICE_START_POST] = "start-post",
3762 [SERVICE_RUNNING] = "running",
3763 [SERVICE_EXITED] = "exited",
3764 [SERVICE_RELOAD] = "reload",
3765 [SERVICE_STOP] = "stop",
3766 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3767 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3768 [SERVICE_STOP_POST] = "stop-post",
3769 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3770 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3771 [SERVICE_FAILED] = "failed",
3772 [SERVICE_AUTO_RESTART] = "auto-restart",
3775 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3777 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3778 [SERVICE_RESTART_NO] = "no",
3779 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3780 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3781 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3782 [SERVICE_RESTART_ALWAYS] = "always"
3785 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3787 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3788 [SERVICE_SIMPLE] = "simple",
3789 [SERVICE_FORKING] = "forking",
3790 [SERVICE_ONESHOT] = "oneshot",
3791 [SERVICE_DBUS] = "dbus",
3792 [SERVICE_NOTIFY] = "notify",
3793 [SERVICE_IDLE] = "idle"
3796 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3798 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3799 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3800 [SERVICE_EXEC_START] = "ExecStart",
3801 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3802 [SERVICE_EXEC_RELOAD] = "ExecReload",
3803 [SERVICE_EXEC_STOP] = "ExecStop",
3804 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3807 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3809 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3810 [NOTIFY_NONE] = "none",
3811 [NOTIFY_MAIN] = "main",
3812 [NOTIFY_ALL] = "all"
3815 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3817 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3818 [SERVICE_SUCCESS] = "success",
3819 [SERVICE_FAILURE_RESOURCES] = "resources",
3820 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3821 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3822 [SERVICE_FAILURE_SIGNAL] = "signal",
3823 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3824 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3825 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3828 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3830 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3831 [SERVICE_START_LIMIT_NONE] = "none",
3832 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3833 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3834 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3836 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3838 const UnitVTable service_vtable = {
3839 .object_size = sizeof(Service),
3840 .exec_context_offset = offsetof(Service, exec_context),
3847 .init = service_init,
3848 .done = service_done,
3849 .load = service_load,
3851 .coldplug = service_coldplug,
3853 .dump = service_dump,
3855 .start = service_start,
3856 .stop = service_stop,
3857 .reload = service_reload,
3859 .can_reload = service_can_reload,
3861 .kill = service_kill,
3863 .serialize = service_serialize,
3864 .deserialize_item = service_deserialize_item,
3866 .active_state = service_active_state,
3867 .sub_state_to_string = service_sub_state_to_string,
3869 .check_gc = service_check_gc,
3870 .check_snapshot = service_check_snapshot,
3872 .sigchld_event = service_sigchld_event,
3873 .timer_event = service_timer_event,
3874 .fd_event = service_fd_event,
3876 .reset_failed = service_reset_failed,
3878 .cgroup_notify_empty = service_cgroup_notify_event,
3879 .notify_message = service_notify_message,
3881 .bus_name_owner_change = service_bus_name_owner_change,
3882 .bus_query_pid_done = service_bus_query_pid_done,
3884 .bus_interface = "org.freedesktop.systemd1.Service",
3885 .bus_message_handler = bus_service_message_handler,
3886 .bus_invalidating_properties = bus_service_invalidating_properties,
3888 #ifdef HAVE_SYSV_COMPAT
3889 .enumerate = service_enumerate,
3891 .status_message_formats = {
3892 .starting_stopping = {
3893 [0] = "Starting %s...",
3894 [1] = "Stopping %s...",
3896 .finished_start_job = {
3897 [JOB_DONE] = "Started %s.",
3898 [JOB_FAILED] = "Failed to start %s.",
3899 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3900 [JOB_TIMEOUT] = "Timed out starting %s.",
3902 .finished_stop_job = {
3903 [JOB_DONE] = "Stopped %s.",
3904 [JOB_FAILED] = "Stopped (with error) %s.",
3905 [JOB_TIMEOUT] = "Timed out stopping %s.",