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, const bool supports_reload) {
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 if (supports_reload) {
547 c = exec_command_new(UNIT(s)->source_path, "reload");
550 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
556 static bool usage_contains_reload(const char *line) {
557 return (strcasestr(line, "{reload|") ||
558 strcasestr(line, "{reload}") ||
559 strcasestr(line, "{reload\"") ||
560 strcasestr(line, "|reload|") ||
561 strcasestr(line, "|reload}") ||
562 strcasestr(line, "|reload\""));
565 static int service_load_sysv_path(Service *s, const char *path) {
577 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
579 bool supports_reload = false;
586 f = fopen(path, "re");
588 r = errno == ENOENT ? 0 : -errno;
592 if (fstat(fileno(f), &st) < 0) {
597 free(u->source_path);
598 u->source_path = strdup(path);
599 if (!u->source_path) {
603 u->source_mtime = timespec_load(&st.st_mtim);
605 if (null_or_empty(&st)) {
606 u->load_state = UNIT_MASKED;
614 char l[LINE_MAX], *t;
616 if (!fgets(l, sizeof(l), f)) {
621 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
629 /* Try to figure out whether this init script supports
630 * the reload operation. This heuristic looks for
631 * "Usage" lines which include the reload option. */
632 if ( state == USAGE_CONTINUATION ||
633 (state == NORMAL && strcasestr(t, "usage"))) {
634 if (usage_contains_reload(t)) {
635 supports_reload = true;
637 } else if (t[strlen(t)-1] == '\\')
638 state = USAGE_CONTINUATION;
646 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
648 s->sysv_has_lsb = true;
652 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
658 t += strspn(t, WHITESPACE);
660 if (state == NORMAL) {
662 /* Try to parse Red Hat style chkconfig headers */
664 if (startswith_no_case(t, "chkconfig:")) {
666 char runlevels[16], *k;
670 if (sscanf(t+10, "%15s %i %*i",
672 &start_priority) != 2) {
674 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
678 /* A start priority gathered from the
679 * symlink farms is preferred over the
680 * data from the LSB header. */
681 if (start_priority < 0 || start_priority > 99)
682 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
684 s->sysv_start_priority = start_priority;
686 char_array_0(runlevels);
687 k = delete_chars(runlevels, WHITESPACE "-");
692 if (!(d = strdup(k))) {
697 free(s->sysv_runlevels);
698 s->sysv_runlevels = d;
701 } else if (startswith_no_case(t, "description:")) {
703 size_t k = strlen(t);
707 if (t[k-1] == '\\') {
712 if ((j = strstrip(t+12)) && *j) {
713 if (!(d = strdup(j))) {
720 free(chkconfig_description);
721 chkconfig_description = d;
723 } else if (startswith_no_case(t, "pidfile:")) {
730 if (!path_is_absolute(fn)) {
731 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
735 if (!(fn = strdup(fn))) {
744 } else if (state == DESCRIPTION) {
746 /* Try to parse Red Hat style description
749 size_t k = strlen(t);
757 if ((j = strstrip(t)) && *j) {
760 if (chkconfig_description)
761 d = strjoin(chkconfig_description, " ", j, NULL);
770 free(chkconfig_description);
771 chkconfig_description = d;
774 } else if (state == LSB || state == LSB_DESCRIPTION) {
776 if (startswith_no_case(t, "Provides:")) {
782 FOREACH_WORD_QUOTED(w, z, t+9, i) {
785 if (!(n = strndup(w, z))) {
790 r = sysv_translate_facility(n, path_get_file_name(path), &m);
799 if (unit_name_to_type(m) == UNIT_SERVICE)
800 r = unit_add_name(u, m);
807 * indication that the
809 * now available. This
812 * targets do NOT pull
815 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
818 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
823 } else if (startswith_no_case(t, "Required-Start:") ||
824 startswith_no_case(t, "Should-Start:") ||
825 startswith_no_case(t, "X-Start-Before:") ||
826 startswith_no_case(t, "X-Start-After:")) {
832 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
835 if (!(n = strndup(w, z))) {
840 r = sysv_translate_facility(n, path_get_file_name(path), &m);
843 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
853 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
856 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
860 } else if (startswith_no_case(t, "Default-Start:")) {
865 k = delete_chars(t+14, WHITESPACE "-");
868 if (!(d = strdup(k))) {
873 free(s->sysv_runlevels);
874 s->sysv_runlevels = d;
877 } else if (startswith_no_case(t, "Description:")) {
880 state = LSB_DESCRIPTION;
882 if ((j = strstrip(t+12)) && *j) {
883 if (!(d = strdup(j))) {
890 free(long_description);
891 long_description = d;
893 } else if (startswith_no_case(t, "Short-Description:")) {
898 if ((j = strstrip(t+18)) && *j) {
899 if (!(d = strdup(j))) {
906 free(short_description);
907 short_description = d;
909 } else if (state == LSB_DESCRIPTION) {
911 if (startswith(l, "#\t") || startswith(l, "# ")) {
914 if ((j = strstrip(t)) && *j) {
917 if (long_description)
918 d = strjoin(long_description, " ", t, NULL);
927 free(long_description);
928 long_description = d;
937 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
939 if (s->sysv_runlevels &&
940 chars_intersect(RUNLEVELS_BOOT, s->sysv_runlevels) &&
941 chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
942 /* Service has both boot and "up" runlevels
943 configured. Kill the "up" ones. */
944 delete_chars(s->sysv_runlevels, RUNLEVELS_UP);
947 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
948 /* If there a runlevels configured for this service
949 * but none of the standard ones, then we assume this
950 * is some special kind of service (which might be
951 * needed for early boot) and don't create any links
954 UNIT(s)->default_dependencies = false;
956 /* Don't timeout special services during boot (like fsck) */
957 s->timeout_start_usec = 0;
958 s->timeout_stop_usec = 0;
960 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
961 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
964 /* Special setting for all SysV services */
965 s->type = SERVICE_FORKING;
966 s->remain_after_exit = !s->pid_file;
967 s->guess_main_pid = false;
968 s->restart = SERVICE_RESTART_NO;
969 s->exec_context.ignore_sigpipe = false;
970 s->kill_context.kill_mode = KILL_PROCESS;
972 /* We use the long description only if
973 * no short description is set. */
975 if (short_description)
976 description = short_description;
977 else if (chkconfig_description)
978 description = chkconfig_description;
979 else if (long_description)
980 description = long_description;
987 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
995 /* The priority that has been set in /etc/rcN.d/ hierarchies
996 * takes precedence over what is stored as default in the LSB
998 if (s->sysv_start_priority_from_rcnd >= 0)
999 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
1001 u->load_state = UNIT_LOADED;
1009 free(short_description);
1010 free(long_description);
1011 free(chkconfig_description);
1016 static int service_load_sysv_name(Service *s, const char *name) {
1022 /* For SysV services we strip the boot.*, rc.* and *.sh
1023 * prefixes/suffixes. */
1024 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
1025 if (endswith(name, ".sh.service"))
1030 if (startswith(name, "boot."))
1034 #ifdef TARGET_FRUGALWARE
1035 if (startswith(name, "rc."))
1039 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
1043 path = strjoin(*p, "/", name, NULL);
1047 assert(endswith(path, ".service"));
1048 path[strlen(path)-8] = 0;
1050 r = service_load_sysv_path(s, path);
1052 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
1053 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1054 /* Try Debian style *.sh source'able init scripts */
1055 strcat(path, ".sh");
1056 r = service_load_sysv_path(s, path);
1062 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1063 /* Try SUSE style boot.* init scripts */
1065 path = strjoin(*p, "/boot.", name, NULL);
1069 /* Drop .service suffix */
1070 path[strlen(path)-8] = 0;
1071 r = service_load_sysv_path(s, path);
1076 #ifdef TARGET_FRUGALWARE
1077 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
1078 /* Try Frugalware style rc.* init scripts */
1080 path = strjoin(*p, "/rc.", name, NULL);
1084 /* Drop .service suffix */
1085 path[strlen(path)-8] = 0;
1086 r = service_load_sysv_path(s, path);
1094 if ((UNIT(s)->load_state != UNIT_STUB))
1101 static int service_load_sysv(Service *s) {
1108 /* Load service data from SysV init scripts, preferably with
1109 * LSB headers ... */
1111 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
1114 if ((t = UNIT(s)->id))
1115 if ((r = service_load_sysv_name(s, t)) < 0)
1118 if (UNIT(s)->load_state == UNIT_STUB)
1119 SET_FOREACH(t, UNIT(s)->names, i) {
1120 if (t == UNIT(s)->id)
1123 if ((r = service_load_sysv_name(s, t)) < 0)
1126 if (UNIT(s)->load_state != UNIT_STUB)
1134 static int fsck_fix_order(Service *s) {
1140 if (s->fsck_passno <= 0)
1143 /* For each pair of services where both have an fsck priority
1144 * we order things based on it. */
1146 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
1155 if (UNIT(t)->load_state != UNIT_LOADED)
1158 if (t->fsck_passno <= 0)
1161 if (t->fsck_passno < s->fsck_passno)
1163 else if (t->fsck_passno > s->fsck_passno)
1168 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1175 static int service_verify(Service *s) {
1178 if (UNIT(s)->load_state != UNIT_LOADED)
1181 if (!s->exec_command[SERVICE_EXEC_START]) {
1182 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
1186 if (s->type != SERVICE_ONESHOT &&
1187 s->exec_command[SERVICE_EXEC_START]->command_next) {
1188 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
1192 if (s->type == SERVICE_DBUS && !s->bus_name) {
1193 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
1197 if (s->bus_name && s->type != SERVICE_DBUS)
1198 log_warning("%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
1200 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
1201 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
1208 static int service_add_default_dependencies(Service *s) {
1213 /* Add a number of automatic dependencies useful for the
1214 * majority of services. */
1216 /* First, pull in base system */
1217 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
1219 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1222 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
1224 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1228 /* Second, activate normal shutdown */
1229 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1232 static void service_fix_output(Service *s) {
1235 /* If nothing has been explicitly configured, patch default
1236 * output in. If input is socket/tty we avoid this however,
1237 * since in that case we want output to default to the same
1238 * place as we read input from. */
1240 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1241 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1242 s->exec_context.std_input == EXEC_INPUT_NULL)
1243 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
1245 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1246 s->exec_context.std_input == EXEC_INPUT_NULL)
1247 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
1250 static int service_load(Unit *u) {
1252 Service *s = SERVICE(u);
1256 /* Load a .service file */
1257 if ((r = unit_load_fragment(u)) < 0)
1260 #ifdef HAVE_SYSV_COMPAT
1261 /* Load a classic init script as a fallback, if we couldn't find anything */
1262 if (u->load_state == UNIT_STUB)
1263 if ((r = service_load_sysv(s)) < 0)
1267 /* Still nothing found? Then let's give up */
1268 if (u->load_state == UNIT_STUB)
1271 /* We were able to load something, then let's add in the
1272 * dropin directories. */
1273 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1276 /* This is a new unit? Then let's add in some extras */
1277 if (u->load_state == UNIT_LOADED) {
1278 if (s->type == _SERVICE_TYPE_INVALID)
1279 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1281 /* Oneshot services have disabled start timeout by default */
1282 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1283 s->timeout_start_usec = 0;
1285 service_fix_output(s);
1287 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1290 if ((r = unit_add_default_cgroups(u)) < 0)
1293 #ifdef HAVE_SYSV_COMPAT
1294 if ((r = sysv_fix_order(s)) < 0)
1298 if ((r = fsck_fix_order(s)) < 0)
1302 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1305 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1306 s->notify_access = NOTIFY_MAIN;
1308 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1309 s->notify_access = NOTIFY_MAIN;
1311 if (s->type == SERVICE_DBUS || s->bus_name)
1312 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1315 if (UNIT(s)->default_dependencies)
1316 if ((r = service_add_default_dependencies(s)) < 0)
1319 r = unit_exec_context_defaults(u, &s->exec_context);
1324 return service_verify(s);
1327 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1329 ServiceExecCommand c;
1330 Service *s = SERVICE(u);
1331 const char *prefix2;
1336 p2 = strappend(prefix, "\t");
1337 prefix2 = p2 ? p2 : prefix;
1340 "%sService State: %s\n"
1342 "%sReload Result: %s\n"
1343 "%sPermissionsStartOnly: %s\n"
1344 "%sRootDirectoryStartOnly: %s\n"
1345 "%sRemainAfterExit: %s\n"
1346 "%sGuessMainPID: %s\n"
1349 "%sNotifyAccess: %s\n",
1350 prefix, service_state_to_string(s->state),
1351 prefix, service_result_to_string(s->result),
1352 prefix, service_result_to_string(s->reload_result),
1353 prefix, yes_no(s->permissions_start_only),
1354 prefix, yes_no(s->root_directory_start_only),
1355 prefix, yes_no(s->remain_after_exit),
1356 prefix, yes_no(s->guess_main_pid),
1357 prefix, service_type_to_string(s->type),
1358 prefix, service_restart_to_string(s->restart),
1359 prefix, notify_access_to_string(s->notify_access));
1361 if (s->control_pid > 0)
1363 "%sControl PID: %lu\n",
1364 prefix, (unsigned long) s->control_pid);
1366 if (s->main_pid > 0)
1369 "%sMain PID Known: %s\n"
1370 "%sMain PID Alien: %s\n",
1371 prefix, (unsigned long) s->main_pid,
1372 prefix, yes_no(s->main_pid_known),
1373 prefix, yes_no(s->main_pid_alien));
1378 prefix, s->pid_file);
1383 "%sBus Name Good: %s\n",
1384 prefix, s->bus_name,
1385 prefix, yes_no(s->bus_name_good));
1387 kill_context_dump(&s->kill_context, f, prefix);
1388 exec_context_dump(&s->exec_context, f, prefix);
1390 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1392 if (!s->exec_command[c])
1395 fprintf(f, "%s-> %s:\n",
1396 prefix, service_exec_command_to_string(c));
1398 exec_command_dump_list(s->exec_command[c], f, prefix2);
1401 #ifdef HAVE_SYSV_COMPAT
1404 "%sSysV Init Script has LSB Header: %s\n"
1405 "%sSysVEnabled: %s\n",
1406 prefix, yes_no(s->sysv_has_lsb),
1407 prefix, yes_no(s->sysv_enabled));
1409 if (s->sysv_start_priority >= 0)
1411 "%sSysVStartPriority: %i\n",
1412 prefix, s->sysv_start_priority);
1414 if (s->sysv_runlevels)
1415 fprintf(f, "%sSysVRunLevels: %s\n",
1416 prefix, s->sysv_runlevels);
1419 if (s->fsck_passno > 0)
1421 "%sFsckPassNo: %i\n",
1422 prefix, s->fsck_passno);
1425 fprintf(f, "%sStatus Text: %s\n",
1426 prefix, s->status_text);
1431 static int service_load_pid_file(Service *s, bool may_warn) {
1441 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1443 log_info("PID file %s not readable (yet?) after %s.",
1444 s->pid_file, service_state_to_string(s->state));
1448 r = parse_pid(k, &pid);
1454 if (kill(pid, 0) < 0 && errno != EPERM) {
1456 log_info("PID %lu read from file %s does not exist.",
1457 (unsigned long) pid, s->pid_file);
1461 if (s->main_pid_known) {
1462 if (pid == s->main_pid)
1465 log_debug("Main PID changing: %lu -> %lu",
1466 (unsigned long) s->main_pid, (unsigned long) pid);
1467 service_unwatch_main_pid(s);
1468 s->main_pid_known = false;
1470 log_debug("Main PID loaded: %lu", (unsigned long) pid);
1472 if ((r = service_set_main_pid(s, pid)) < 0)
1475 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1476 /* FIXME: we need to do something here */
1482 static int service_search_main_pid(Service *s) {
1488 /* If we know it anyway, don't ever fallback to unreliable
1490 if (s->main_pid_known)
1493 if (!s->guess_main_pid)
1496 assert(s->main_pid <= 0);
1498 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
1501 log_debug("Main PID guessed: %lu", (unsigned long) pid);
1502 if ((r = service_set_main_pid(s, pid)) < 0)
1505 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1506 /* FIXME: we need to do something here */
1512 static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
1518 /* Notifies all our sockets when we die */
1520 if (s->socket_fd >= 0)
1523 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
1524 if (u->type == UNIT_SOCKET)
1525 socket_notify_service_dead(SOCKET(u), failed_permanent);
1530 static void service_set_state(Service *s, ServiceState state) {
1531 ServiceState old_state;
1532 const UnitActiveState *table;
1535 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1537 old_state = s->state;
1540 service_unwatch_pid_file(s);
1542 if (state != SERVICE_START_PRE &&
1543 state != SERVICE_START &&
1544 state != SERVICE_START_POST &&
1545 state != SERVICE_RELOAD &&
1546 state != SERVICE_STOP &&
1547 state != SERVICE_STOP_SIGTERM &&
1548 state != SERVICE_STOP_SIGKILL &&
1549 state != SERVICE_STOP_POST &&
1550 state != SERVICE_FINAL_SIGTERM &&
1551 state != SERVICE_FINAL_SIGKILL &&
1552 state != SERVICE_AUTO_RESTART)
1553 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1555 if (state != SERVICE_START &&
1556 state != SERVICE_START_POST &&
1557 state != SERVICE_RUNNING &&
1558 state != SERVICE_RELOAD &&
1559 state != SERVICE_STOP &&
1560 state != SERVICE_STOP_SIGTERM &&
1561 state != SERVICE_STOP_SIGKILL) {
1562 service_unwatch_main_pid(s);
1563 s->main_command = NULL;
1566 if (state != SERVICE_START_PRE &&
1567 state != SERVICE_START &&
1568 state != SERVICE_START_POST &&
1569 state != SERVICE_RELOAD &&
1570 state != SERVICE_STOP &&
1571 state != SERVICE_STOP_SIGTERM &&
1572 state != SERVICE_STOP_SIGKILL &&
1573 state != SERVICE_STOP_POST &&
1574 state != SERVICE_FINAL_SIGTERM &&
1575 state != SERVICE_FINAL_SIGKILL) {
1576 service_unwatch_control_pid(s);
1577 s->control_command = NULL;
1578 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1581 if (state == SERVICE_DEAD ||
1582 state == SERVICE_STOP ||
1583 state == SERVICE_STOP_SIGTERM ||
1584 state == SERVICE_STOP_SIGKILL ||
1585 state == SERVICE_STOP_POST ||
1586 state == SERVICE_FINAL_SIGTERM ||
1587 state == SERVICE_FINAL_SIGKILL ||
1588 state == SERVICE_FAILED ||
1589 state == SERVICE_AUTO_RESTART)
1590 service_notify_sockets_dead(s, false);
1592 if (state != SERVICE_START_PRE &&
1593 state != SERVICE_START &&
1594 state != SERVICE_START_POST &&
1595 state != SERVICE_RUNNING &&
1596 state != SERVICE_RELOAD &&
1597 state != SERVICE_STOP &&
1598 state != SERVICE_STOP_SIGTERM &&
1599 state != SERVICE_STOP_SIGKILL &&
1600 state != SERVICE_STOP_POST &&
1601 state != SERVICE_FINAL_SIGTERM &&
1602 state != SERVICE_FINAL_SIGKILL &&
1603 !(state == SERVICE_DEAD && UNIT(s)->job)) {
1604 service_close_socket_fd(s);
1605 service_connection_unref(s);
1608 if (state == SERVICE_STOP)
1609 service_stop_watchdog(s);
1611 /* For the inactive states unit_notify() will trim the cgroup,
1612 * but for exit we have to do that ourselves... */
1613 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1614 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
1616 if (old_state != state)
1617 log_debug("%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
1619 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
1620 s->reload_result = SERVICE_SUCCESS;
1623 static int service_coldplug(Unit *u) {
1624 Service *s = SERVICE(u);
1628 assert(s->state == SERVICE_DEAD);
1630 if (s->deserialized_state != s->state) {
1632 if (s->deserialized_state == SERVICE_START_PRE ||
1633 s->deserialized_state == SERVICE_START ||
1634 s->deserialized_state == SERVICE_START_POST ||
1635 s->deserialized_state == SERVICE_RELOAD ||
1636 s->deserialized_state == SERVICE_STOP ||
1637 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1638 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1639 s->deserialized_state == SERVICE_STOP_POST ||
1640 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1641 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1642 s->deserialized_state == SERVICE_AUTO_RESTART) {
1643 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
1646 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
1648 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1653 if ((s->deserialized_state == SERVICE_START &&
1654 (s->type == SERVICE_FORKING ||
1655 s->type == SERVICE_DBUS ||
1656 s->type == SERVICE_ONESHOT ||
1657 s->type == SERVICE_NOTIFY)) ||
1658 s->deserialized_state == SERVICE_START_POST ||
1659 s->deserialized_state == SERVICE_RUNNING ||
1660 s->deserialized_state == SERVICE_RELOAD ||
1661 s->deserialized_state == SERVICE_STOP ||
1662 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1663 s->deserialized_state == SERVICE_STOP_SIGKILL)
1664 if (s->main_pid > 0)
1665 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1668 if (s->deserialized_state == SERVICE_START_PRE ||
1669 s->deserialized_state == SERVICE_START ||
1670 s->deserialized_state == SERVICE_START_POST ||
1671 s->deserialized_state == SERVICE_RELOAD ||
1672 s->deserialized_state == SERVICE_STOP ||
1673 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1674 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1675 s->deserialized_state == SERVICE_STOP_POST ||
1676 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1677 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1678 if (s->control_pid > 0)
1679 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1682 if (s->deserialized_state == SERVICE_START_POST ||
1683 s->deserialized_state == SERVICE_RUNNING)
1684 service_handle_watchdog(s);
1686 service_set_state(s, s->deserialized_state);
1691 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1695 unsigned rn_fds = 0;
1702 if (s->socket_fd >= 0)
1705 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1710 if (u->type != UNIT_SOCKET)
1715 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1727 if (!(t = new(int, rn_fds+cn_fds))) {
1733 memcpy(t, rfds, rn_fds * sizeof(int));
1734 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1739 rn_fds = rn_fds+cn_fds;
1754 static int service_spawn(
1759 bool apply_permissions,
1761 bool apply_tty_stdin,
1762 bool set_notify_socket,
1768 int *fds = NULL, *fdsbuf = NULL;
1769 unsigned n_fds = 0, n_env = 0;
1770 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1777 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1778 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1779 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1781 if (s->socket_fd >= 0) {
1782 fds = &s->socket_fd;
1785 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1792 if (timeout && s->timeout_start_usec) {
1793 r = unit_watch_timer(UNIT(s), s->timeout_start_usec, &s->timer_watch);
1797 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1799 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1804 our_env = new0(char*, 5);
1810 if (set_notify_socket)
1811 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1816 if (s->main_pid > 0)
1817 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1822 if (s->watchdog_usec > 0)
1823 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1828 if (s->meta.manager->running_as != SYSTEMD_SYSTEM)
1829 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1834 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1848 UNIT(s)->manager->confirm_spawn,
1849 UNIT(s)->cgroup_bondings,
1850 UNIT(s)->cgroup_attributes,
1851 is_control ? "control" : NULL,
1853 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1859 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1860 /* FIXME: we need to do something here */
1866 strv_free(final_env);
1876 strv_free(final_env);
1879 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1884 static int main_pid_good(Service *s) {
1887 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1890 /* If we know the pid file, then lets just check if it is
1892 if (s->main_pid_known) {
1894 /* If it's an alien child let's check if it is still
1896 if (s->main_pid_alien)
1897 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1899 /* .. otherwise assume we'll get a SIGCHLD for it,
1900 * which we really should wait for to collect exit
1901 * status and code */
1902 return s->main_pid > 0;
1905 /* We don't know the pid */
1909 static int control_pid_good(Service *s) {
1912 return s->control_pid > 0;
1915 static int cgroup_good(Service *s) {
1920 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
1926 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1930 if (f != SERVICE_SUCCESS)
1933 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1935 if (allow_restart &&
1936 !s->forbid_restart &&
1937 (s->restart == SERVICE_RESTART_ALWAYS ||
1938 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1939 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1940 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
1941 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1942 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1943 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1944 (s->result != SERVICE_FAILURE_SIGNAL ||
1945 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1948 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
1952 service_set_state(s, SERVICE_AUTO_RESTART);
1955 s->forbid_restart = false;
1960 log_warning("%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1961 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1964 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
1966 static void service_enter_stop_post(Service *s, ServiceResult f) {
1970 if (f != SERVICE_SUCCESS)
1973 service_unwatch_control_pid(s);
1975 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1976 s->control_command_id = SERVICE_EXEC_STOP_POST;
1978 r = service_spawn(s,
1982 !s->permissions_start_only,
1983 !s->root_directory_start_only,
1992 service_set_state(s, SERVICE_STOP_POST);
1994 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1999 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
2000 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2003 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
2005 Set *pid_set = NULL;
2006 bool wait_for_exit = false;
2010 if (f != SERVICE_SUCCESS)
2013 if (s->kill_context.kill_mode != KILL_NONE) {
2014 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->kill_context.kill_signal : SIGKILL;
2016 if (s->main_pid > 0) {
2017 if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
2018 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
2020 wait_for_exit = !s->main_pid_alien;
2023 if (s->control_pid > 0) {
2024 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
2025 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
2027 wait_for_exit = true;
2030 if (s->kill_context.kill_mode == KILL_CONTROL_GROUP) {
2032 pid_set = set_new(trivial_hash_func, trivial_compare_func);
2038 /* Exclude the main/control pids from being killed via the cgroup */
2039 if (s->main_pid > 0)
2040 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
2043 if (s->control_pid > 0)
2044 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
2047 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
2049 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
2050 log_warning("Failed to kill control group: %s", strerror(-r));
2052 wait_for_exit = true;
2059 if (wait_for_exit) {
2060 if (s->timeout_stop_usec > 0) {
2061 r = unit_watch_timer(UNIT(s), s->timeout_stop_usec, &s->timer_watch);
2066 service_set_state(s, state);
2067 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2068 service_enter_stop_post(s, SERVICE_SUCCESS);
2070 service_enter_dead(s, SERVICE_SUCCESS, true);
2075 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
2077 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
2078 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
2080 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2086 static void service_enter_stop(Service *s, ServiceResult f) {
2091 if (f != SERVICE_SUCCESS)
2094 service_unwatch_control_pid(s);
2096 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
2097 s->control_command_id = SERVICE_EXEC_STOP;
2099 r = service_spawn(s,
2103 !s->permissions_start_only,
2104 !s->root_directory_start_only,
2112 service_set_state(s, SERVICE_STOP);
2114 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2119 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
2120 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2123 static void service_enter_running(Service *s, ServiceResult f) {
2124 int main_pid_ok, cgroup_ok;
2127 if (f != SERVICE_SUCCESS)
2130 main_pid_ok = main_pid_good(s);
2131 cgroup_ok = cgroup_good(s);
2133 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
2134 (s->bus_name_good || s->type != SERVICE_DBUS))
2135 service_set_state(s, SERVICE_RUNNING);
2136 else if (s->remain_after_exit)
2137 service_set_state(s, SERVICE_EXITED);
2139 service_enter_stop(s, SERVICE_SUCCESS);
2142 static void service_enter_start_post(Service *s) {
2146 service_unwatch_control_pid(s);
2148 if (s->watchdog_usec > 0)
2149 service_reset_watchdog(s);
2151 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
2152 s->control_command_id = SERVICE_EXEC_START_POST;
2154 r = service_spawn(s,
2158 !s->permissions_start_only,
2159 !s->root_directory_start_only,
2167 service_set_state(s, SERVICE_START_POST);
2169 service_enter_running(s, SERVICE_SUCCESS);
2174 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
2175 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2178 static void service_enter_start(Service *s) {
2185 assert(s->exec_command[SERVICE_EXEC_START]);
2186 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2188 if (s->type == SERVICE_FORKING)
2189 service_unwatch_control_pid(s);
2191 service_unwatch_main_pid(s);
2193 /* We want to ensure that nobody leaks processes from
2194 * START_PRE here, so let's go on a killing spree, People
2195 * should not spawn long running processes from START_PRE. */
2196 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2198 if (s->type == SERVICE_FORKING) {
2199 s->control_command_id = SERVICE_EXEC_START;
2200 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2202 s->main_command = NULL;
2204 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2205 s->control_command = NULL;
2207 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2210 r = service_spawn(s,
2212 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
2217 s->notify_access != NOTIFY_NONE,
2223 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
2224 /* For simple services we immediately start
2225 * the START_POST binaries. */
2227 service_set_main_pid(s, pid);
2228 service_enter_start_post(s);
2230 } else if (s->type == SERVICE_FORKING) {
2232 /* For forking services we wait until the start
2233 * process exited. */
2235 s->control_pid = pid;
2236 service_set_state(s, SERVICE_START);
2238 } else if (s->type == SERVICE_ONESHOT ||
2239 s->type == SERVICE_DBUS ||
2240 s->type == SERVICE_NOTIFY) {
2242 /* For oneshot services we wait until the start
2243 * process exited, too, but it is our main process. */
2245 /* For D-Bus services we know the main pid right away,
2246 * but wait for the bus name to appear on the
2247 * bus. Notify services are similar. */
2249 service_set_main_pid(s, pid);
2250 service_set_state(s, SERVICE_START);
2252 assert_not_reached("Unknown service type");
2257 log_warning("%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
2258 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2261 static void service_enter_start_pre(Service *s) {
2266 service_unwatch_control_pid(s);
2268 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2270 /* Before we start anything, let's clear up what might
2271 * be left from previous runs. */
2272 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2274 s->control_command_id = SERVICE_EXEC_START_PRE;
2276 r = service_spawn(s,
2280 !s->permissions_start_only,
2281 !s->root_directory_start_only,
2289 service_set_state(s, SERVICE_START_PRE);
2291 service_enter_start(s);
2296 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
2297 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2300 static void service_enter_restart(Service *s) {
2305 dbus_error_init(&error);
2307 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2308 /* Don't restart things if we are going down anyway */
2309 log_info("Stop job pending for unit, delaying automatic restart.");
2311 r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
2318 /* Any units that are bound to this service must also be
2319 * restarted. We use JOB_RESTART (instead of the more obvious
2320 * JOB_START) here so that those dependency jobs will be added
2322 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2326 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2327 * it will be canceled as part of the service_stop() call that
2328 * is executed as part of JOB_RESTART. */
2330 log_debug("%s scheduled restart job.", UNIT(s)->id);
2334 log_warning("%s failed to schedule restart job: %s", UNIT(s)->id, bus_error(&error, -r));
2335 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
2337 dbus_error_free(&error);
2340 static void service_enter_reload(Service *s) {
2345 service_unwatch_control_pid(s);
2347 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2348 s->control_command_id = SERVICE_EXEC_RELOAD;
2350 r = service_spawn(s,
2354 !s->permissions_start_only,
2355 !s->root_directory_start_only,
2363 service_set_state(s, SERVICE_RELOAD);
2365 service_enter_running(s, SERVICE_SUCCESS);
2370 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
2371 s->reload_result = SERVICE_FAILURE_RESOURCES;
2372 service_enter_running(s, SERVICE_SUCCESS);
2375 static void service_run_next_control(Service *s) {
2379 assert(s->control_command);
2380 assert(s->control_command->command_next);
2382 assert(s->control_command_id != SERVICE_EXEC_START);
2384 s->control_command = s->control_command->command_next;
2385 service_unwatch_control_pid(s);
2387 r = service_spawn(s,
2391 !s->permissions_start_only,
2392 !s->root_directory_start_only,
2393 s->control_command_id == SERVICE_EXEC_START_PRE ||
2394 s->control_command_id == SERVICE_EXEC_STOP_POST,
2404 log_warning("%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
2406 if (s->state == SERVICE_START_PRE)
2407 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2408 else if (s->state == SERVICE_STOP)
2409 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2410 else if (s->state == SERVICE_STOP_POST)
2411 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
2412 else if (s->state == SERVICE_RELOAD) {
2413 s->reload_result = SERVICE_FAILURE_RESOURCES;
2414 service_enter_running(s, SERVICE_SUCCESS);
2416 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2419 static void service_run_next_main(Service *s) {
2424 assert(s->main_command);
2425 assert(s->main_command->command_next);
2426 assert(s->type == SERVICE_ONESHOT);
2428 s->main_command = s->main_command->command_next;
2429 service_unwatch_main_pid(s);
2431 r = service_spawn(s,
2438 s->notify_access != NOTIFY_NONE,
2444 service_set_main_pid(s, pid);
2449 log_warning("%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
2450 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2453 static int service_start_limit_test(Service *s) {
2456 if (ratelimit_test(&s->start_limit))
2459 switch (s->start_limit_action) {
2461 case SERVICE_START_LIMIT_NONE:
2462 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s)->id);
2465 case SERVICE_START_LIMIT_REBOOT: {
2469 dbus_error_init(&error);
2471 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s)->id);
2473 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2475 log_error("Failed to reboot: %s.", bus_error(&error, r));
2476 dbus_error_free(&error);
2482 case SERVICE_START_LIMIT_REBOOT_FORCE:
2483 log_warning("%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
2484 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2487 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
2488 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
2489 reboot(RB_AUTOBOOT);
2493 log_error("start limit action=%i", s->start_limit_action);
2494 assert_not_reached("Unknown StartLimitAction.");
2500 static int service_start(Unit *u) {
2501 Service *s = SERVICE(u);
2506 /* We cannot fulfill this request right now, try again later
2508 if (s->state == SERVICE_STOP ||
2509 s->state == SERVICE_STOP_SIGTERM ||
2510 s->state == SERVICE_STOP_SIGKILL ||
2511 s->state == SERVICE_STOP_POST ||
2512 s->state == SERVICE_FINAL_SIGTERM ||
2513 s->state == SERVICE_FINAL_SIGKILL)
2516 /* Already on it! */
2517 if (s->state == SERVICE_START_PRE ||
2518 s->state == SERVICE_START ||
2519 s->state == SERVICE_START_POST)
2522 /* A service that will be restarted must be stopped first to
2523 * trigger BindsTo and/or OnFailure dependencies. If a user
2524 * does not want to wait for the holdoff time to elapse, the
2525 * service should be manually restarted, not started. We
2526 * simply return EAGAIN here, so that any start jobs stay
2527 * queued, and assume that the auto restart timer will
2528 * eventually trigger the restart. */
2529 if (s->state == SERVICE_AUTO_RESTART)
2532 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
2534 /* Make sure we don't enter a busy loop of some kind. */
2535 r = service_start_limit_test(s);
2537 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2541 s->result = SERVICE_SUCCESS;
2542 s->reload_result = SERVICE_SUCCESS;
2543 s->main_pid_known = false;
2544 s->main_pid_alien = false;
2545 s->forbid_restart = false;
2547 service_enter_start_pre(s);
2551 static int service_stop(Unit *u) {
2552 Service *s = SERVICE(u);
2556 /* Don't create restart jobs from here. */
2557 s->forbid_restart = true;
2560 if (s->state == SERVICE_STOP ||
2561 s->state == SERVICE_STOP_SIGTERM ||
2562 s->state == SERVICE_STOP_SIGKILL ||
2563 s->state == SERVICE_STOP_POST ||
2564 s->state == SERVICE_FINAL_SIGTERM ||
2565 s->state == SERVICE_FINAL_SIGKILL)
2568 /* A restart will be scheduled or is in progress. */
2569 if (s->state == SERVICE_AUTO_RESTART) {
2570 service_set_state(s, SERVICE_DEAD);
2574 /* If there's already something running we go directly into
2576 if (s->state == SERVICE_START_PRE ||
2577 s->state == SERVICE_START ||
2578 s->state == SERVICE_START_POST ||
2579 s->state == SERVICE_RELOAD) {
2580 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2584 assert(s->state == SERVICE_RUNNING ||
2585 s->state == SERVICE_EXITED);
2587 service_enter_stop(s, SERVICE_SUCCESS);
2591 static int service_reload(Unit *u) {
2592 Service *s = SERVICE(u);
2596 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2598 service_enter_reload(s);
2602 static bool service_can_reload(Unit *u) {
2603 Service *s = SERVICE(u);
2607 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2610 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2611 Service *s = SERVICE(u);
2617 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2618 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2619 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2621 if (s->control_pid > 0)
2622 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2624 if (s->main_pid_known && s->main_pid > 0)
2625 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2627 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2630 unit_serialize_item(u, f, "status-text", s->status_text);
2632 /* FIXME: There's a minor uncleanliness here: if there are
2633 * multiple commands attached here, we will start from the
2634 * first one again */
2635 if (s->control_command_id >= 0)
2636 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2638 if (s->socket_fd >= 0) {
2641 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2644 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2647 if (s->main_exec_status.pid > 0) {
2648 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2649 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2650 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2652 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2653 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2654 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2657 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2658 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2663 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2664 Service *s = SERVICE(u);
2671 if (streq(key, "state")) {
2674 if ((state = service_state_from_string(value)) < 0)
2675 log_debug("Failed to parse state value %s", value);
2677 s->deserialized_state = state;
2678 } else if (streq(key, "result")) {
2681 f = service_result_from_string(value);
2683 log_debug("Failed to parse result value %s", value);
2684 else if (f != SERVICE_SUCCESS)
2687 } else if (streq(key, "reload-result")) {
2690 f = service_result_from_string(value);
2692 log_debug("Failed to parse reload result value %s", value);
2693 else if (f != SERVICE_SUCCESS)
2694 s->reload_result = f;
2696 } else if (streq(key, "control-pid")) {
2699 if (parse_pid(value, &pid) < 0)
2700 log_debug("Failed to parse control-pid value %s", value);
2702 s->control_pid = pid;
2703 } else if (streq(key, "main-pid")) {
2706 if (parse_pid(value, &pid) < 0)
2707 log_debug("Failed to parse main-pid value %s", value);
2709 service_set_main_pid(s, (pid_t) pid);
2710 } else if (streq(key, "main-pid-known")) {
2713 if ((b = parse_boolean(value)) < 0)
2714 log_debug("Failed to parse main-pid-known value %s", value);
2716 s->main_pid_known = b;
2717 } else if (streq(key, "status-text")) {
2720 if ((t = strdup(value))) {
2721 free(s->status_text);
2725 } else if (streq(key, "control-command")) {
2726 ServiceExecCommand id;
2728 if ((id = service_exec_command_from_string(value)) < 0)
2729 log_debug("Failed to parse exec-command value %s", value);
2731 s->control_command_id = id;
2732 s->control_command = s->exec_command[id];
2734 } else if (streq(key, "socket-fd")) {
2737 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2738 log_debug("Failed to parse socket-fd value %s", value);
2741 if (s->socket_fd >= 0)
2742 close_nointr_nofail(s->socket_fd);
2743 s->socket_fd = fdset_remove(fds, fd);
2745 } else if (streq(key, "main-exec-status-pid")) {
2748 if (parse_pid(value, &pid) < 0)
2749 log_debug("Failed to parse main-exec-status-pid value %s", value);
2751 s->main_exec_status.pid = pid;
2752 } else if (streq(key, "main-exec-status-code")) {
2755 if (safe_atoi(value, &i) < 0)
2756 log_debug("Failed to parse main-exec-status-code value %s", value);
2758 s->main_exec_status.code = i;
2759 } else if (streq(key, "main-exec-status-status")) {
2762 if (safe_atoi(value, &i) < 0)
2763 log_debug("Failed to parse main-exec-status-status value %s", value);
2765 s->main_exec_status.status = i;
2766 } else if (streq(key, "main-exec-status-start"))
2767 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2768 else if (streq(key, "main-exec-status-exit"))
2769 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2770 else if (streq(key, "watchdog-timestamp"))
2771 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2773 log_debug("Unknown serialization key '%s'", key);
2778 static UnitActiveState service_active_state(Unit *u) {
2779 const UnitActiveState *table;
2783 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2785 return table[SERVICE(u)->state];
2788 static const char *service_sub_state_to_string(Unit *u) {
2791 return service_state_to_string(SERVICE(u)->state);
2794 static bool service_check_gc(Unit *u) {
2795 Service *s = SERVICE(u);
2799 /* Never clean up services that still have a process around,
2800 * even if the service is formally dead. */
2801 if (cgroup_good(s) > 0 ||
2802 main_pid_good(s) > 0 ||
2803 control_pid_good(s) > 0)
2806 #ifdef HAVE_SYSV_COMPAT
2814 static bool service_check_snapshot(Unit *u) {
2815 Service *s = SERVICE(u);
2819 return !s->got_socket_fd;
2822 static int service_retry_pid_file(Service *s) {
2825 assert(s->pid_file);
2826 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2828 r = service_load_pid_file(s, false);
2832 service_unwatch_pid_file(s);
2834 service_enter_running(s, SERVICE_SUCCESS);
2838 static int service_watch_pid_file(Service *s) {
2841 log_debug("Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
2842 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2846 /* the pidfile might have appeared just before we set the watch */
2847 service_retry_pid_file(s);
2851 log_error("Failed to set a watch for %s's PID file %s: %s",
2852 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2853 service_unwatch_pid_file(s);
2857 static int service_demand_pid_file(Service *s) {
2860 assert(s->pid_file);
2861 assert(!s->pid_file_pathspec);
2863 ps = new0(PathSpec, 1);
2867 ps->path = strdup(s->pid_file);
2873 path_kill_slashes(ps->path);
2875 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2876 * keep their PID file open all the time. */
2877 ps->type = PATH_MODIFIED;
2878 ps->inotify_fd = -1;
2880 s->pid_file_pathspec = ps;
2882 return service_watch_pid_file(s);
2885 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2886 Service *s = SERVICE(u);
2890 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2891 assert(s->pid_file_pathspec);
2892 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2894 log_debug("inotify event for %s", u->id);
2896 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2899 if (service_retry_pid_file(s) == 0)
2902 if (service_watch_pid_file(s) < 0)
2907 service_unwatch_pid_file(s);
2908 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2911 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2912 Service *s = SERVICE(u);
2918 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2919 is_clean_exit_lsb(code, status, &s->success_status))
2920 f = SERVICE_SUCCESS;
2921 else if (code == CLD_EXITED)
2922 f = SERVICE_FAILURE_EXIT_CODE;
2923 else if (code == CLD_KILLED)
2924 f = SERVICE_FAILURE_SIGNAL;
2925 else if (code == CLD_DUMPED)
2926 f = SERVICE_FAILURE_CORE_DUMP;
2928 assert_not_reached("Unknown code");
2930 if (s->main_pid == pid) {
2931 /* Forking services may occasionally move to a new PID.
2932 * As long as they update the PID file before exiting the old
2933 * PID, they're fine. */
2934 if (service_load_pid_file(s, false) == 0)
2938 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2940 /* If this is not a forking service than the main
2941 * process got started and hence we copy the exit
2942 * status so that it is recorded both as main and as
2943 * control process exit status */
2944 if (s->main_command) {
2945 s->main_command->exec_status = s->main_exec_status;
2947 if (s->main_command->ignore)
2948 f = SERVICE_SUCCESS;
2951 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2952 "%s: main process exited, code=%s, status=%i", u->id, sigchld_code_to_string(code), status);
2954 if (f != SERVICE_SUCCESS)
2957 if (s->main_command &&
2958 s->main_command->command_next &&
2959 f == SERVICE_SUCCESS) {
2961 /* There is another command to *
2962 * execute, so let's do that. */
2964 log_debug("%s running next main command for state %s", u->id, service_state_to_string(s->state));
2965 service_run_next_main(s);
2969 /* The service exited, so the service is officially
2971 s->main_command = NULL;
2975 case SERVICE_START_POST:
2976 case SERVICE_RELOAD:
2978 /* Need to wait until the operation is
2983 if (s->type == SERVICE_ONESHOT) {
2984 /* This was our main goal, so let's go on */
2985 if (f == SERVICE_SUCCESS)
2986 service_enter_start_post(s);
2988 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2994 case SERVICE_RUNNING:
2995 service_enter_running(s, f);
2998 case SERVICE_STOP_SIGTERM:
2999 case SERVICE_STOP_SIGKILL:
3001 if (!control_pid_good(s))
3002 service_enter_stop_post(s, f);
3004 /* If there is still a control process, wait for that first */
3008 assert_not_reached("Uh, main process died at wrong time.");
3012 } else if (s->control_pid == pid) {
3016 if (s->control_command) {
3017 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
3019 if (s->control_command->ignore)
3020 f = SERVICE_SUCCESS;
3023 log_full(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
3024 "%s: control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
3026 if (f != SERVICE_SUCCESS)
3029 /* Immediately get rid of the cgroup, so that the
3030 * kernel doesn't delay the cgroup empty messages for
3031 * the service cgroup any longer than necessary */
3032 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
3034 if (s->control_command &&
3035 s->control_command->command_next &&
3036 f == SERVICE_SUCCESS) {
3038 /* There is another command to *
3039 * execute, so let's do that. */
3041 log_debug("%s running next control command for state %s", u->id, service_state_to_string(s->state));
3042 service_run_next_control(s);
3045 /* No further commands for this step, so let's
3046 * figure out what to do next */
3048 s->control_command = NULL;
3049 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3051 log_debug("%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
3055 case SERVICE_START_PRE:
3056 if (f == SERVICE_SUCCESS)
3057 service_enter_start(s);
3059 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3063 if (s->type != SERVICE_FORKING)
3064 /* Maybe spurious event due to a reload that changed the type? */
3067 if (f != SERVICE_SUCCESS) {
3068 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3073 bool has_start_post;
3076 /* Let's try to load the pid file here if we can.
3077 * The PID file might actually be created by a START_POST
3078 * script. In that case don't worry if the loading fails. */
3080 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3081 r = service_load_pid_file(s, !has_start_post);
3082 if (!has_start_post && r < 0) {
3083 r = service_demand_pid_file(s);
3084 if (r < 0 || !cgroup_good(s))
3085 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3089 service_search_main_pid(s);
3091 service_enter_start_post(s);
3094 case SERVICE_START_POST:
3095 if (f != SERVICE_SUCCESS) {
3096 service_enter_stop(s, f);
3103 r = service_load_pid_file(s, true);
3105 r = service_demand_pid_file(s);
3106 if (r < 0 || !cgroup_good(s))
3107 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3111 service_search_main_pid(s);
3113 service_enter_running(s, SERVICE_SUCCESS);
3116 case SERVICE_RELOAD:
3117 if (f == SERVICE_SUCCESS) {
3118 service_load_pid_file(s, true);
3119 service_search_main_pid(s);
3122 s->reload_result = f;
3123 service_enter_running(s, SERVICE_SUCCESS);
3127 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
3130 case SERVICE_STOP_SIGTERM:
3131 case SERVICE_STOP_SIGKILL:
3132 if (main_pid_good(s) <= 0)
3133 service_enter_stop_post(s, f);
3135 /* If there is still a service
3136 * process around, wait until
3137 * that one quit, too */
3140 case SERVICE_STOP_POST:
3141 case SERVICE_FINAL_SIGTERM:
3142 case SERVICE_FINAL_SIGKILL:
3143 service_enter_dead(s, f, true);
3147 assert_not_reached("Uh, control process died at wrong time.");
3152 /* Notify clients about changed exit status */
3153 unit_add_to_dbus_queue(u);
3156 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
3157 Service *s = SERVICE(u);
3160 assert(elapsed == 1);
3162 if (w == &s->watchdog_watch) {
3163 service_handle_watchdog(s);
3167 assert(w == &s->timer_watch);
3171 case SERVICE_START_PRE:
3173 log_warning("%s operation timed out. Terminating.", u->id);
3174 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3177 case SERVICE_START_POST:
3178 log_warning("%s operation timed out. Stopping.", u->id);
3179 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
3182 case SERVICE_RELOAD:
3183 log_warning("%s operation timed out. Stopping.", u->id);
3184 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3185 service_enter_running(s, SERVICE_SUCCESS);
3189 log_warning("%s stopping timed out. Terminating.", u->id);
3190 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3193 case SERVICE_STOP_SIGTERM:
3194 if (s->kill_context.send_sigkill) {
3195 log_warning("%s stopping timed out. Killing.", u->id);
3196 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3198 log_warning("%s stopping timed out. Skipping SIGKILL.", u->id);
3199 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3204 case SERVICE_STOP_SIGKILL:
3205 /* Uh, we sent a SIGKILL and it is still not gone?
3206 * Must be something we cannot kill, so let's just be
3207 * weirded out and continue */
3209 log_warning("%s still around after SIGKILL. Ignoring.", u->id);
3210 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
3213 case SERVICE_STOP_POST:
3214 log_warning("%s stopping timed out (2). Terminating.", u->id);
3215 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
3218 case SERVICE_FINAL_SIGTERM:
3219 if (s->kill_context.send_sigkill) {
3220 log_warning("%s stopping timed out (2). Killing.", u->id);
3221 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
3223 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->id);
3224 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
3229 case SERVICE_FINAL_SIGKILL:
3230 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->id);
3231 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
3234 case SERVICE_AUTO_RESTART:
3235 log_info("%s holdoff time over, scheduling restart.", u->id);
3236 service_enter_restart(s);
3240 assert_not_reached("Timeout at wrong time.");
3244 static void service_cgroup_notify_event(Unit *u) {
3245 Service *s = SERVICE(u);
3249 log_debug("%s: cgroup is empty", u->id);
3253 /* Waiting for SIGCHLD is usually more interesting,
3254 * because it includes return codes/signals. Which is
3255 * why we ignore the cgroup events for most cases,
3256 * except when we don't know pid which to expect the
3260 case SERVICE_START_POST:
3261 /* If we were hoping for the daemon to write its PID file,
3262 * we can give up now. */
3263 if (s->pid_file_pathspec) {
3264 log_warning("%s never wrote its PID file. Failing.", UNIT(s)->id);
3265 service_unwatch_pid_file(s);
3266 if (s->state == SERVICE_START)
3267 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3269 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3273 case SERVICE_RUNNING:
3274 /* service_enter_running() will figure out what to do */
3275 service_enter_running(s, SERVICE_SUCCESS);
3278 case SERVICE_STOP_SIGTERM:
3279 case SERVICE_STOP_SIGKILL:
3281 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3282 service_enter_stop_post(s, SERVICE_SUCCESS);
3286 case SERVICE_FINAL_SIGTERM:
3287 case SERVICE_FINAL_SIGKILL:
3288 if (main_pid_good(s) <= 0 && !control_pid_good(s))
3289 service_enter_dead(s, SERVICE_SUCCESS, true);
3298 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
3299 Service *s = SERVICE(u);
3304 if (s->notify_access == NOTIFY_NONE) {
3305 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3306 u->id, (unsigned long) pid);
3310 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3311 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3312 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
3316 log_debug("%s: Got message", u->id);
3318 /* Interpret MAINPID= */
3319 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3320 (s->state == SERVICE_START ||
3321 s->state == SERVICE_START_POST ||
3322 s->state == SERVICE_RUNNING ||
3323 s->state == SERVICE_RELOAD)) {
3325 if (parse_pid(e + 8, &pid) < 0)
3326 log_warning("Failed to parse notification message %s", e);
3328 log_debug("%s: got %s", u->id, e);
3329 service_set_main_pid(s, pid);
3333 /* Interpret READY= */
3334 if (s->type == SERVICE_NOTIFY &&
3335 s->state == SERVICE_START &&
3336 strv_find(tags, "READY=1")) {
3337 log_debug("%s: got READY=1", u->id);
3339 service_enter_start_post(s);
3342 /* Interpret STATUS= */
3343 e = strv_find_prefix(tags, "STATUS=");
3349 if (!utf8_is_valid(e+7)) {
3350 log_warning("Status message in notification is not UTF-8 clean.");
3356 log_error("Failed to allocate string.");
3360 log_debug("%s: got %s", u->id, e);
3362 free(s->status_text);
3365 free(s->status_text);
3366 s->status_text = NULL;
3370 if (strv_find(tags, "WATCHDOG=1")) {
3371 log_debug("%s: got WATCHDOG=1", u->id);
3372 service_reset_watchdog(s);
3375 /* Notify clients about changed status or main pid */
3376 unit_add_to_dbus_queue(u);
3379 #ifdef HAVE_SYSV_COMPAT
3382 static void sysv_facility_in_insserv_conf(Manager *mgr) {
3386 if (!(f = fopen("/etc/insserv.conf", "re"))) {
3387 r = errno == ENOENT ? 0 : -errno;
3392 char l[LINE_MAX], *t;
3393 char **parsed = NULL;
3395 if (!fgets(l, sizeof(l), f)) {
3400 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r));
3405 if (*t != '$' && *t != '<')
3408 parsed = strv_split(t,WHITESPACE);
3409 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3410 if (parsed && !startswith_no_case (parsed[0], "<interactive>")) {
3413 if (sysv_translate_facility(parsed[0], NULL, &facility) < 0)
3415 if ((u = manager_get_unit(mgr, facility)) && (u->type == UNIT_TARGET)) {
3417 char *dep = NULL, *name, **j;
3419 STRV_FOREACH (j, parsed+1) {
3428 if (sysv_translate_facility(name, NULL, &dep) < 0)
3431 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, e, dep, NULL, true);
3446 static int service_enumerate(Manager *m) {
3450 char *path = NULL, *fpath = NULL, *name = NULL;
3451 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3458 if (m->running_as != SYSTEMD_SYSTEM)
3461 zero(runlevel_services);
3463 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3464 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3468 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
3477 if (!(d = opendir(path))) {
3478 if (errno != ENOENT)
3479 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3484 while ((de = readdir(d))) {
3487 if (ignore_file(de->d_name))
3490 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3493 if (strlen(de->d_name) < 4)
3496 a = undecchar(de->d_name[1]);
3497 b = undecchar(de->d_name[2]);
3503 fpath = strjoin(path, "/", de->d_name, NULL);
3509 if (access(fpath, X_OK) < 0) {
3511 if (errno != ENOENT)
3512 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3518 if (!(name = sysv_translate_name(de->d_name + 3))) {
3523 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
3524 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3528 if (de->d_name[0] == 'S') {
3530 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
3531 SERVICE(service)->sysv_start_priority_from_rcnd =
3532 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3534 SERVICE(service)->sysv_enabled = true;
3537 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3540 if ((r = set_put(runlevel_services[i], service)) < 0)
3543 } else if (de->d_name[0] == 'K' &&
3544 (rcnd_table[i].type == RUNLEVEL_DOWN ||
3545 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
3547 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3550 if ((r = set_put(shutdown_services, service)) < 0)
3556 /* Now we loaded all stubs and are aware of the lowest
3557 start-up priority for all services, not let's actually load
3558 the services, this will also tell us which services are
3559 actually native now */
3560 manager_dispatch_load_queue(m);
3562 /* If this is a native service, rely on native ways to pull in
3563 * a service, don't pull it in via sysv rcN.d links. */
3564 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3565 SET_FOREACH(service, runlevel_services[i], j) {
3566 service = unit_follow_merge(service);
3568 if (service->fragment_path)
3571 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3575 /* We honour K links only for halt/reboot. For the normal
3576 * runlevels we assume the stop jobs will be implicitly added
3577 * by the core logic. Also, we don't really distinguish here
3578 * between the runlevels 0 and 6 and just add them to the
3579 * special shutdown target. On SUSE the boot.d/ runlevel is
3580 * also used for shutdown, so we add links for that too to the
3581 * shutdown target.*/
3582 SET_FOREACH(service, shutdown_services, j) {
3583 service = unit_follow_merge(service);
3585 if (service->fragment_path)
3588 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3595 sysv_facility_in_insserv_conf (m);
3603 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3604 set_free(runlevel_services[i]);
3605 set_free(shutdown_services);
3614 static void service_bus_name_owner_change(
3617 const char *old_owner,
3618 const char *new_owner) {
3620 Service *s = SERVICE(u);
3625 assert(streq(s->bus_name, name));
3626 assert(old_owner || new_owner);
3628 if (old_owner && new_owner)
3629 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
3631 log_debug("%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
3633 log_debug("%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
3635 s->bus_name_good = !!new_owner;
3637 if (s->type == SERVICE_DBUS) {
3639 /* service_enter_running() will figure out what to
3641 if (s->state == SERVICE_RUNNING)
3642 service_enter_running(s, SERVICE_SUCCESS);
3643 else if (s->state == SERVICE_START && new_owner)
3644 service_enter_start_post(s);
3646 } else if (new_owner &&
3648 (s->state == SERVICE_START ||
3649 s->state == SERVICE_START_POST ||
3650 s->state == SERVICE_RUNNING ||
3651 s->state == SERVICE_RELOAD)) {
3653 /* Try to acquire PID from bus service */
3654 log_debug("Trying to acquire PID from D-Bus name...");
3656 bus_query_pid(u->manager, name);
3660 static void service_bus_query_pid_done(
3665 Service *s = SERVICE(u);
3670 log_debug("%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
3672 if (s->main_pid <= 0 &&
3673 (s->state == SERVICE_START ||
3674 s->state == SERVICE_START_POST ||
3675 s->state == SERVICE_RUNNING ||
3676 s->state == SERVICE_RELOAD))
3677 service_set_main_pid(s, pid);
3680 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3685 /* This is called by the socket code when instantiating a new
3686 * service for a stream socket and the socket needs to be
3689 if (UNIT(s)->load_state != UNIT_LOADED)
3692 if (s->socket_fd >= 0)
3695 if (s->state != SERVICE_DEAD)
3699 s->got_socket_fd = true;
3701 unit_ref_set(&s->accept_socket, UNIT(sock));
3703 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3706 static void service_reset_failed(Unit *u) {
3707 Service *s = SERVICE(u);
3711 if (s->state == SERVICE_FAILED)
3712 service_set_state(s, SERVICE_DEAD);
3714 s->result = SERVICE_SUCCESS;
3715 s->reload_result = SERVICE_SUCCESS;
3717 RATELIMIT_RESET(s->start_limit);
3720 static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
3721 Service *s = SERVICE(u);
3723 Set *pid_set = NULL;
3727 if (s->main_pid <= 0 && who == KILL_MAIN) {
3728 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3732 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3733 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3737 if (who == KILL_CONTROL || who == KILL_ALL)
3738 if (s->control_pid > 0)
3739 if (kill(s->control_pid, signo) < 0)
3742 if (who == KILL_MAIN || who == KILL_ALL)
3743 if (s->main_pid > 0)
3744 if (kill(s->main_pid, signo) < 0)
3747 if (who == KILL_ALL) {
3750 pid_set = set_new(trivial_hash_func, trivial_compare_func);
3754 /* Exclude the control/main pid from being killed via the cgroup */
3755 if (s->control_pid > 0) {
3756 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
3763 if (s->main_pid > 0) {
3764 q = set_put(pid_set, LONG_TO_PTR(s->main_pid));
3771 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
3772 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3783 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3784 [SERVICE_DEAD] = "dead",
3785 [SERVICE_START_PRE] = "start-pre",
3786 [SERVICE_START] = "start",
3787 [SERVICE_START_POST] = "start-post",
3788 [SERVICE_RUNNING] = "running",
3789 [SERVICE_EXITED] = "exited",
3790 [SERVICE_RELOAD] = "reload",
3791 [SERVICE_STOP] = "stop",
3792 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3793 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3794 [SERVICE_STOP_POST] = "stop-post",
3795 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3796 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3797 [SERVICE_FAILED] = "failed",
3798 [SERVICE_AUTO_RESTART] = "auto-restart",
3801 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3803 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3804 [SERVICE_RESTART_NO] = "no",
3805 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3806 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3807 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3808 [SERVICE_RESTART_ALWAYS] = "always"
3811 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3813 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3814 [SERVICE_SIMPLE] = "simple",
3815 [SERVICE_FORKING] = "forking",
3816 [SERVICE_ONESHOT] = "oneshot",
3817 [SERVICE_DBUS] = "dbus",
3818 [SERVICE_NOTIFY] = "notify",
3819 [SERVICE_IDLE] = "idle"
3822 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3824 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3825 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3826 [SERVICE_EXEC_START] = "ExecStart",
3827 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3828 [SERVICE_EXEC_RELOAD] = "ExecReload",
3829 [SERVICE_EXEC_STOP] = "ExecStop",
3830 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3833 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3835 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3836 [NOTIFY_NONE] = "none",
3837 [NOTIFY_MAIN] = "main",
3838 [NOTIFY_ALL] = "all"
3841 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3843 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3844 [SERVICE_SUCCESS] = "success",
3845 [SERVICE_FAILURE_RESOURCES] = "resources",
3846 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3847 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3848 [SERVICE_FAILURE_SIGNAL] = "signal",
3849 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3850 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3851 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3854 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3856 static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3857 [SERVICE_START_LIMIT_NONE] = "none",
3858 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3859 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3860 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3862 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3864 const UnitVTable service_vtable = {
3865 .object_size = sizeof(Service),
3866 .exec_context_offset = offsetof(Service, exec_context),
3873 .init = service_init,
3874 .done = service_done,
3875 .load = service_load,
3877 .coldplug = service_coldplug,
3879 .dump = service_dump,
3881 .start = service_start,
3882 .stop = service_stop,
3883 .reload = service_reload,
3885 .can_reload = service_can_reload,
3887 .kill = service_kill,
3889 .serialize = service_serialize,
3890 .deserialize_item = service_deserialize_item,
3892 .active_state = service_active_state,
3893 .sub_state_to_string = service_sub_state_to_string,
3895 .check_gc = service_check_gc,
3896 .check_snapshot = service_check_snapshot,
3898 .sigchld_event = service_sigchld_event,
3899 .timer_event = service_timer_event,
3900 .fd_event = service_fd_event,
3902 .reset_failed = service_reset_failed,
3904 .cgroup_notify_empty = service_cgroup_notify_event,
3905 .notify_message = service_notify_message,
3907 .bus_name_owner_change = service_bus_name_owner_change,
3908 .bus_query_pid_done = service_bus_query_pid_done,
3910 .bus_interface = "org.freedesktop.systemd1.Service",
3911 .bus_message_handler = bus_service_message_handler,
3912 .bus_invalidating_properties = bus_service_invalidating_properties,
3914 #ifdef HAVE_SYSV_COMPAT
3915 .enumerate = service_enumerate,
3917 .status_message_formats = {
3918 .starting_stopping = {
3919 [0] = "Starting %s...",
3920 [1] = "Stopping %s...",
3922 .finished_start_job = {
3923 [JOB_DONE] = "Started %s.",
3924 [JOB_FAILED] = "Failed to start %s.",
3925 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3926 [JOB_TIMEOUT] = "Timed out starting %s.",
3928 .finished_stop_job = {
3929 [JOB_DONE] = "Stopped %s.",
3930 [JOB_FAILED] = "Stopped (with error) %s.",
3931 [JOB_TIMEOUT] = "Timed out stopping %s.",