1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "load-fragment.h"
30 #include "load-dropin.h"
33 #include "unit-name.h"
34 #include "dbus-service.h"
36 #define COMMENTS "#;\n"
37 #define NEWLINES "\n\r"
40 typedef enum RunlevelType {
49 const RunlevelType type;
51 /* Standard SysV runlevels */
52 { "rc0.d", SPECIAL_RUNLEVEL0_TARGET, RUNLEVEL_DOWN },
53 { "rc1.d", SPECIAL_RUNLEVEL1_TARGET, RUNLEVEL_UP },
54 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
55 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
56 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
57 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
58 { "rc6.d", SPECIAL_RUNLEVEL6_TARGET, RUNLEVEL_DOWN },
60 /* SUSE style boot.d */
61 { "boot.d", SPECIAL_BASIC_TARGET, RUNLEVEL_BASIC },
63 /* Debian style rcS.d */
64 { "rcS.d", SPECIAL_BASIC_TARGET, RUNLEVEL_BASIC },
67 #define RUNLEVELS_UP "12345"
68 /* #define RUNLEVELS_DOWN "06" */
69 /* #define RUNLEVELS_BOOT "bBsS" */
71 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
72 [SERVICE_DEAD] = UNIT_INACTIVE,
73 [SERVICE_START_PRE] = UNIT_ACTIVATING,
74 [SERVICE_START] = UNIT_ACTIVATING,
75 [SERVICE_START_POST] = UNIT_ACTIVATING,
76 [SERVICE_RUNNING] = UNIT_ACTIVE,
77 [SERVICE_EXITED] = UNIT_ACTIVE,
78 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
79 [SERVICE_STOP] = UNIT_DEACTIVATING,
80 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
81 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
82 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
83 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
84 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
85 [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
86 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
89 static void service_init(Unit *u) {
90 Service *s = SERVICE(u);
93 assert(u->meta.load_state == UNIT_STUB);
95 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
96 s->restart_usec = DEFAULT_RESTART_USEC;
97 s->timer_watch.type = WATCH_INVALID;
98 s->sysv_start_priority = -1;
101 exec_context_init(&s->exec_context);
103 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
105 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
108 static void service_unwatch_control_pid(Service *s) {
111 if (s->control_pid <= 0)
114 unit_unwatch_pid(UNIT(s), s->control_pid);
118 static void service_unwatch_main_pid(Service *s) {
121 if (s->main_pid <= 0)
124 unit_unwatch_pid(UNIT(s), s->main_pid);
128 static void service_close_socket_fd(Service *s) {
131 if (s->socket_fd < 0)
134 close_nointr_nofail(s->socket_fd);
138 static void service_done(Unit *u) {
139 Service *s = SERVICE(u);
149 free(s->sysv_runlevels);
150 s->sysv_runlevels = NULL;
152 exec_context_done(&s->exec_context);
153 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
154 s->control_command = NULL;
156 /* This will leak a process, but at least no memory or any of
158 service_unwatch_main_pid(s);
159 service_unwatch_control_pid(s);
162 unit_unwatch_bus_name(UNIT(u), s->bus_name);
167 service_close_socket_fd(s);
169 unit_unwatch_timer(u, &s->timer_watch);
172 static char *sysv_translate_name(const char *name) {
175 if (!(r = new(char, strlen(name) + sizeof(".service"))))
178 if (startswith(name, "boot."))
179 /* Drop SuSE-style boot. prefix */
180 strcpy(stpcpy(r, name + 5), ".service");
181 else if (endswith(name, ".sh"))
182 /* Drop Debian-style .sh suffix */
183 strcpy(stpcpy(r, name) - 3, ".service");
185 /* Normal init scripts */
186 strcpy(stpcpy(r, name), ".service");
191 static int sysv_translate_facility(const char *name, char **_r) {
193 static const char * const table[] = {
194 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
195 "$network", SPECIAL_NETWORK_TARGET,
196 "$named", SPECIAL_NSS_LOOKUP_TARGET,
197 "$portmap", SPECIAL_RPCBIND_TARGET,
198 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
199 "$syslog", SPECIAL_SYSLOG_TARGET,
200 "$time", SPECIAL_RTC_SET_TARGET
206 for (i = 0; i < ELEMENTSOF(table); i += 2)
207 if (streq(table[i], name)) {
208 if (!(r = strdup(table[i+1])))
217 if (!(r = sysv_translate_name(name)))
228 static int sysv_fix_order(Service *s) {
234 if (s->sysv_start_priority < 0)
237 /* For each pair of services where at least one lacks a LSB
238 * header, we use the start priority value to order things. */
240 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
244 t = (Service*) other;
249 if (t->sysv_start_priority < 0)
252 /* If both units have modern headers we don't care
253 * about the priorities */
254 if ((!s->sysv_path || s->sysv_has_lsb) &&
255 (!t->sysv_path || t->sysv_has_lsb))
258 if (t->sysv_start_priority < s->sysv_start_priority)
260 else if (t->sysv_start_priority > s->sysv_start_priority)
265 /* FIXME: Maybe we should compare the name here lexicographically? */
267 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
274 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
277 if (!(c = new0(ExecCommand, 1)))
280 if (!(c->path = strdup(path))) {
285 if (!(c->argv = strv_new(path, arg1, NULL))) {
294 static int sysv_exec_commands(Service *s) {
298 assert(s->sysv_path);
300 if (!(c = exec_command_new(s->sysv_path, "start")))
302 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
304 if (!(c = exec_command_new(s->sysv_path, "stop")))
306 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
308 if (!(c = exec_command_new(s->sysv_path, "reload")))
310 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
315 static int service_load_sysv_path(Service *s, const char *path) {
332 if (!(f = fopen(path, "re"))) {
333 r = errno == ENOENT ? 0 : -errno;
337 s->type = SERVICE_FORKING;
338 s->restart = SERVICE_ONCE;
341 if (!(s->sysv_path = strdup(path))) {
347 char l[LINE_MAX], *t;
349 if (!fgets(l, sizeof(l), f)) {
354 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
364 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
366 s->sysv_has_lsb = true;
370 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
376 t += strspn(t, WHITESPACE);
378 if (state == NORMAL) {
380 /* Try to parse Red Hat style chkconfig headers */
382 if (startswith(t, "chkconfig:")) {
384 char runlevels[16], *k;
388 if (sscanf(t+10, "%15s %i %*i",
390 &start_priority) != 2) {
392 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
396 /* A start priority gathered from the
397 * symlink farms is preferred over the
398 * data from the LSB header. */
399 if (start_priority < 0 || start_priority > 99)
400 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
401 else if (s->sysv_start_priority < 0)
402 s->sysv_start_priority = start_priority;
404 char_array_0(runlevels);
405 k = delete_chars(runlevels, WHITESPACE "-");
410 if (!(d = strdup(k))) {
415 free(s->sysv_runlevels);
416 s->sysv_runlevels = d;
419 } else if (startswith(t, "description:")) {
421 size_t k = strlen(t);
424 if (t[k-1] == '\\') {
429 if (!(d = strdup(strstrip(t+12)))) {
434 free(u->meta.description);
435 u->meta.description = d;
437 } else if (startswith(t, "pidfile:")) {
444 if (!path_is_absolute(fn)) {
445 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
449 if (!(fn = strdup(fn))) {
458 } else if (state == DESCRIPTION) {
460 /* Try to parse Red Hat style description
463 size_t k = strlen(t);
471 assert(u->meta.description);
472 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
477 free(u->meta.description);
478 u->meta.description = d;
480 } else if (state == LSB || state == LSB_DESCRIPTION) {
482 if (startswith(t, "Provides:")) {
488 FOREACH_WORD(w, z, t+9, i) {
491 if (!(n = strndup(w, z))) {
496 r = sysv_translate_facility(n, &m);
505 if (unit_name_to_type(m) == UNIT_SERVICE)
506 r = unit_add_name(u, m);
508 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL, true)) >= 0)
509 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
518 } else if (startswith(t, "Required-Start:") ||
519 startswith(t, "Should-Start:") ||
520 startswith(t, "X-Start-Before:") ||
521 startswith(t, "X-Start-After:")) {
527 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
530 if (!(n = strndup(w, z))) {
535 r = sysv_translate_facility(n, &m);
544 r = unit_add_dependency_by_name(u, startswith(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
550 } else if (startswith(t, "Default-Start:")) {
555 k = delete_chars(t+14, WHITESPACE "-");
558 if (!(d = strdup(k))) {
563 free(s->sysv_runlevels);
564 s->sysv_runlevels = d;
567 } else if (startswith(t, "Description:")) {
570 state = LSB_DESCRIPTION;
572 if (!(d = strdup(strstrip(t+12)))) {
577 free(u->meta.description);
578 u->meta.description = d;
580 } else if (startswith(t, "Short-Description:") &&
581 !u->meta.description) {
584 /* We use the short description only
585 * if no long description is set. */
589 if (!(d = strdup(strstrip(t+18)))) {
594 u->meta.description = d;
596 } else if (state == LSB_DESCRIPTION) {
598 if (startswith(l, "#\t") || startswith(l, "# ")) {
601 assert(u->meta.description);
602 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
607 free(u->meta.description);
608 u->meta.description = d;
615 if ((r = sysv_exec_commands(s)) < 0)
618 if (!s->sysv_runlevels || chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
619 /* If there a runlevels configured for this service
620 * but none of the standard ones, then we assume this
621 * is some special kind of service (which might be
622 * needed for early boot) and don't create any links
625 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0 ||
626 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
630 /* Don't timeout special services during boot (like fsck) */
633 /* Special setting for all SysV services */
634 s->valid_no_process = true;
635 s->kill_mode = KILL_PROCESS_GROUP;
637 u->meta.load_state = UNIT_LOADED;
648 static int service_load_sysv_name(Service *s, const char *name) {
654 /* For SysV services we strip the boot. or .sh
655 * prefixes/suffixes. */
656 if (startswith(name, "boot.") ||
657 endswith(name, ".sh.service"))
660 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
664 if (asprintf(&path, "%s/%s", *p, name) < 0)
667 assert(endswith(path, ".service"));
668 path[strlen(path)-8] = 0;
670 r = service_load_sysv_path(s, path);
672 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
673 /* Try Debian style xxx.sh source'able init scripts */
675 r = service_load_sysv_path(s, path);
680 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
681 /* Try SUSE style boot.xxx init scripts */
683 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
686 path[strlen(path)-8] = 0;
687 r = service_load_sysv_path(s, path);
694 if ((UNIT(s)->meta.load_state != UNIT_STUB))
701 static int service_load_sysv(Service *s) {
708 /* Load service data from SysV init scripts, preferably with
711 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
714 if ((t = UNIT(s)->meta.id))
715 if ((r = service_load_sysv_name(s, t)) < 0)
718 if (UNIT(s)->meta.load_state == UNIT_STUB)
719 SET_FOREACH(t, UNIT(s)->meta.names, i) {
720 if (t == UNIT(s)->meta.id)
723 if ((r == service_load_sysv_name(s, t)) < 0)
726 if (UNIT(s)->meta.load_state != UNIT_STUB)
733 static int service_add_bus_name(Service *s) {
740 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
743 r = unit_merge_by_name(UNIT(s), n);
749 static int service_verify(Service *s) {
752 if (UNIT(s)->meta.load_state != UNIT_LOADED)
755 if (!s->exec_command[SERVICE_EXEC_START]) {
756 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
760 if (s->type == SERVICE_DBUS && !s->bus_name) {
761 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->meta.id);
768 static int service_load(Unit *u) {
770 Service *s = SERVICE(u);
774 /* Load a .service file */
775 if ((r = unit_load_fragment(u)) < 0)
778 /* Load a classic init script as a fallback, if we couldn't find anything */
779 if (u->meta.load_state == UNIT_STUB)
780 if ((r = service_load_sysv(s)) < 0)
783 /* Still nothing found? Then let's give up */
784 if (u->meta.load_state == UNIT_STUB)
787 /* We were able to load something, then let's add in the
788 * dropin directories. */
789 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
792 /* This is a new unit? Then let's add in some extras */
793 if (u->meta.load_state == UNIT_LOADED) {
794 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
797 if ((r = unit_add_default_cgroup(u)) < 0)
800 if ((r = sysv_fix_order(s)) < 0)
804 if ((r = service_add_bus_name(s)) < 0)
807 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
812 return service_verify(s);
815 static void service_dump(Unit *u, FILE *f, const char *prefix) {
817 ServiceExecCommand c;
818 Service *s = SERVICE(u);
824 p2 = strappend(prefix, "\t");
825 prefix2 = p2 ? p2 : prefix;
828 "%sService State: %s\n"
829 "%sPermissionsStartOnly: %s\n"
830 "%sRootDirectoryStartOnly: %s\n"
831 "%sValidNoProcess: %s\n"
834 prefix, service_state_to_string(s->state),
835 prefix, yes_no(s->permissions_start_only),
836 prefix, yes_no(s->root_directory_start_only),
837 prefix, yes_no(s->valid_no_process),
838 prefix, kill_mode_to_string(s->kill_mode),
839 prefix, service_type_to_string(s->type));
841 if (s->control_pid > 0)
843 "%sControl PID: %llu\n",
844 prefix, (unsigned long long) s->control_pid);
848 "%sMain PID: %llu\n",
849 prefix, (unsigned long long) s->main_pid);
854 prefix, s->pid_file);
859 "%sBus Name Good: %s\n",
861 prefix, yes_no(s->bus_name_good));
863 exec_context_dump(&s->exec_context, f, prefix);
865 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
867 if (!s->exec_command[c])
870 fprintf(f, "%s-> %s:\n",
871 prefix, service_exec_command_to_string(c));
873 exec_command_dump_list(s->exec_command[c], f, prefix2);
878 "%sSysV Init Script Path: %s\n"
879 "%sSysV Init Script has LSB Header: %s\n",
880 prefix, s->sysv_path,
881 prefix, yes_no(s->sysv_has_lsb));
883 if (s->sysv_start_priority >= 0)
885 "%sSysVStartPriority: %i\n",
886 prefix, s->sysv_start_priority);
888 if (s->sysv_runlevels)
889 fprintf(f, "%sSysVRunLevels: %s\n",
890 prefix, s->sysv_runlevels);
895 static int service_load_pid_file(Service *s) {
902 if (s->main_pid_known)
905 assert(s->main_pid <= 0);
910 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
913 if ((r = safe_atolu(k, &p)) < 0) {
918 if ((unsigned long) (pid_t) p != p)
921 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
922 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
923 (unsigned long long) p, s->pid_file);
927 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
928 /* FIXME: we need to do something here */
931 s->main_pid = (pid_t) p;
932 s->main_pid_known = true;
937 static int service_get_sockets(Service *s, Set **_set) {
946 /* Collects all Socket objects that belong to this
947 * service. Note that a service might have multiple sockets
948 * via multiple names. */
950 if (!(set = set_new(NULL, NULL)))
953 SET_FOREACH(t, UNIT(s)->meta.names, i) {
957 /* Look for all socket objects that go by any of our
958 * units and collect their fds */
960 if (!(k = unit_name_change_suffix(t, ".socket"))) {
965 p = manager_get_unit(UNIT(s)->meta.manager, k);
971 if ((r = set_put(set, p)) < 0)
983 static int service_notify_sockets_dead(Service *s) {
991 /* Notifies all our sockets when we die */
992 if ((r = service_get_sockets(s, &set)) < 0)
995 SET_FOREACH(sock, set, i)
996 socket_notify_service_dead(sock);
1003 static void service_set_state(Service *s, ServiceState state) {
1004 ServiceState old_state;
1007 old_state = s->state;
1010 if (state != SERVICE_START_PRE &&
1011 state != SERVICE_START &&
1012 state != SERVICE_START_POST &&
1013 state != SERVICE_RELOAD &&
1014 state != SERVICE_STOP &&
1015 state != SERVICE_STOP_SIGTERM &&
1016 state != SERVICE_STOP_SIGKILL &&
1017 state != SERVICE_STOP_POST &&
1018 state != SERVICE_FINAL_SIGTERM &&
1019 state != SERVICE_FINAL_SIGKILL &&
1020 state != SERVICE_AUTO_RESTART)
1021 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1023 if (state != SERVICE_START &&
1024 state != SERVICE_START_POST &&
1025 state != SERVICE_RUNNING &&
1026 state != SERVICE_RELOAD &&
1027 state != SERVICE_STOP &&
1028 state != SERVICE_STOP_SIGTERM &&
1029 state != SERVICE_STOP_SIGKILL)
1030 service_unwatch_main_pid(s);
1032 if (state != SERVICE_START_PRE &&
1033 state != SERVICE_START &&
1034 state != SERVICE_START_POST &&
1035 state != SERVICE_RELOAD &&
1036 state != SERVICE_STOP &&
1037 state != SERVICE_STOP_SIGTERM &&
1038 state != SERVICE_STOP_SIGKILL &&
1039 state != SERVICE_STOP_POST &&
1040 state != SERVICE_FINAL_SIGTERM &&
1041 state != SERVICE_FINAL_SIGKILL) {
1042 service_unwatch_control_pid(s);
1043 s->control_command = NULL;
1044 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1047 if (state == SERVICE_DEAD ||
1048 state == SERVICE_STOP ||
1049 state == SERVICE_STOP_SIGTERM ||
1050 state == SERVICE_STOP_SIGKILL ||
1051 state == SERVICE_STOP_POST ||
1052 state == SERVICE_FINAL_SIGTERM ||
1053 state == SERVICE_FINAL_SIGKILL ||
1054 state == SERVICE_MAINTAINANCE ||
1055 state == SERVICE_AUTO_RESTART)
1056 service_notify_sockets_dead(s);
1058 if (state != SERVICE_START_PRE &&
1059 state != SERVICE_START &&
1060 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1061 service_close_socket_fd(s);
1063 if (old_state != state)
1064 log_debug("%s changed %s -> %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1066 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1069 static int service_coldplug(Unit *u) {
1070 Service *s = SERVICE(u);
1074 assert(s->state == SERVICE_DEAD);
1076 if (s->deserialized_state != s->state) {
1078 if (s->deserialized_state == SERVICE_START_PRE ||
1079 s->deserialized_state == SERVICE_START ||
1080 s->deserialized_state == SERVICE_START_POST ||
1081 s->deserialized_state == SERVICE_RELOAD ||
1082 s->deserialized_state == SERVICE_STOP ||
1083 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1084 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1085 s->deserialized_state == SERVICE_STOP_POST ||
1086 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1087 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1088 s->deserialized_state == SERVICE_AUTO_RESTART) {
1090 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1093 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1095 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1100 if ((s->deserialized_state == SERVICE_START &&
1101 (s->type == SERVICE_FORKING ||
1102 s->type == SERVICE_DBUS)) ||
1103 s->deserialized_state == SERVICE_START_POST ||
1104 s->deserialized_state == SERVICE_RUNNING ||
1105 s->deserialized_state == SERVICE_RELOAD ||
1106 s->deserialized_state == SERVICE_STOP ||
1107 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1108 s->deserialized_state == SERVICE_STOP_SIGKILL)
1109 if (s->main_pid > 0)
1110 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1113 if (s->deserialized_state == SERVICE_START_PRE ||
1114 s->deserialized_state == SERVICE_START ||
1115 s->deserialized_state == SERVICE_START_POST ||
1116 s->deserialized_state == SERVICE_RELOAD ||
1117 s->deserialized_state == SERVICE_STOP ||
1118 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1119 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1120 s->deserialized_state == SERVICE_STOP_POST ||
1121 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1122 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1123 if (s->control_pid > 0)
1124 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1127 service_set_state(s, s->deserialized_state);
1133 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1137 unsigned rn_fds = 0;
1145 if ((r = service_get_sockets(s, &set)) < 0)
1148 SET_FOREACH(sock, set, i) {
1152 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1164 if (!(t = new(int, rn_fds+cn_fds))) {
1170 memcpy(t, rfds, rn_fds);
1171 memcpy(t+rn_fds, cfds, cn_fds);
1176 rn_fds = rn_fds+cn_fds;
1194 static int service_spawn(
1199 bool apply_permissions,
1214 if (s->socket_fd >= 0) {
1215 fds = &s->socket_fd;
1217 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1221 if (timeout && s->timeout_usec) {
1222 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1225 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1227 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1236 s->meta.manager->environment,
1239 UNIT(s)->meta.manager->confirm_spawn,
1240 UNIT(s)->meta.cgroup_bondings,
1248 if (s->socket_fd >= 0)
1249 service_close_socket_fd(s);
1254 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1255 /* FIXME: we need to do something here */
1266 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1271 static int main_pid_good(Service *s) {
1274 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1277 /* If we know the pid file, then lets just check if it is
1279 if (s->main_pid_known)
1280 return s->main_pid > 0;
1282 /* We don't know the pid */
1286 static int control_pid_good(Service *s) {
1289 return s->control_pid > 0;
1292 static int cgroup_good(Service *s) {
1297 if (s->valid_no_process)
1300 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1306 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1313 if (allow_restart &&
1315 (s->restart == SERVICE_RESTART_ALWAYS ||
1316 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1318 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1321 service_set_state(s, SERVICE_AUTO_RESTART);
1323 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1328 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
1329 service_enter_dead(s, false, false);
1332 static void service_enter_signal(Service *s, ServiceState state, bool success);
1334 static void service_enter_stop_post(Service *s, bool success) {
1341 service_unwatch_control_pid(s);
1343 s->control_command_id = SERVICE_EXEC_STOP_POST;
1344 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1345 if ((r = service_spawn(s,
1349 !s->permissions_start_only,
1350 !s->root_directory_start_only,
1351 &s->control_pid)) < 0)
1355 service_set_state(s, SERVICE_STOP_POST);
1357 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1362 log_warning("%s failed to run stop-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1363 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1366 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1375 if (s->kill_mode != KILL_NONE) {
1376 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1378 if (s->kill_mode == KILL_CONTROL_GROUP) {
1380 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1381 if (r != -EAGAIN && r != -ESRCH)
1390 if (s->main_pid > 0) {
1391 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1397 if (s->control_pid > 0) {
1398 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1409 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1410 if (s->timeout_usec > 0)
1411 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1414 service_set_state(s, state);
1415 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1416 service_enter_stop_post(s, true);
1418 service_enter_dead(s, true, true);
1423 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
1425 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1426 service_enter_stop_post(s, false);
1428 service_enter_dead(s, false, true);
1431 static void service_enter_stop(Service *s, bool success) {
1438 service_unwatch_control_pid(s);
1440 s->control_command_id = SERVICE_EXEC_STOP;
1441 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1442 if ((r = service_spawn(s,
1446 !s->permissions_start_only,
1447 !s->root_directory_start_only,
1448 &s->control_pid)) < 0)
1451 service_set_state(s, SERVICE_STOP);
1453 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1458 log_warning("%s failed to run stop executable: %s", UNIT(s)->meta.id, strerror(-r));
1459 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1462 static void service_enter_running(Service *s, bool success) {
1468 if (main_pid_good(s) != 0 &&
1469 cgroup_good(s) != 0 &&
1470 (s->bus_name_good || s->type != SERVICE_DBUS))
1471 service_set_state(s, SERVICE_RUNNING);
1472 else if (s->valid_no_process)
1473 service_set_state(s, SERVICE_EXITED);
1475 service_enter_stop(s, true);
1478 static void service_enter_start_post(Service *s) {
1482 service_unwatch_control_pid(s);
1484 s->control_command_id = SERVICE_EXEC_START_POST;
1485 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1486 if ((r = service_spawn(s,
1490 !s->permissions_start_only,
1491 !s->root_directory_start_only,
1492 &s->control_pid)) < 0)
1496 service_set_state(s, SERVICE_START_POST);
1498 service_enter_running(s, true);
1503 log_warning("%s failed to run start-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1504 service_enter_stop(s, false);
1507 static void service_enter_start(Service *s) {
1513 assert(s->exec_command[SERVICE_EXEC_START]);
1514 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1516 if (s->type == SERVICE_FORKING)
1517 service_unwatch_control_pid(s);
1519 service_unwatch_main_pid(s);
1521 if ((r = service_spawn(s,
1522 s->exec_command[SERVICE_EXEC_START],
1523 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS,
1530 if (s->type == SERVICE_SIMPLE) {
1531 /* For simple services we immediately start
1532 * the START_POST binaries. */
1535 s->main_pid_known = true;
1537 service_enter_start_post(s);
1539 } else if (s->type == SERVICE_FORKING) {
1541 /* For forking services we wait until the start
1542 * process exited. */
1544 s->control_pid = pid;
1546 s->control_command_id = SERVICE_EXEC_START;
1547 s->control_command = s->exec_command[SERVICE_EXEC_START];
1548 service_set_state(s, SERVICE_START);
1550 } else if (s->type == SERVICE_FINISH ||
1551 s->type == SERVICE_DBUS) {
1553 /* For finishing services we wait until the start
1554 * process exited, too, but it is our main process. */
1556 /* For D-Bus services we know the main pid right away,
1557 * but wait for the bus name to appear on the bus. */
1560 s->main_pid_known = true;
1562 service_set_state(s, SERVICE_START);
1564 assert_not_reached("Unknown service type");
1569 log_warning("%s failed to run start exectuable: %s", UNIT(s)->meta.id, strerror(-r));
1570 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1573 static void service_enter_start_pre(Service *s) {
1578 service_unwatch_control_pid(s);
1580 s->control_command_id = SERVICE_EXEC_START_PRE;
1581 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1582 if ((r = service_spawn(s,
1586 !s->permissions_start_only,
1587 !s->root_directory_start_only,
1588 &s->control_pid)) < 0)
1591 service_set_state(s, SERVICE_START_PRE);
1593 service_enter_start(s);
1598 log_warning("%s failed to run start-pre executable: %s", UNIT(s)->meta.id, strerror(-r));
1599 service_enter_dead(s, false, true);
1602 static void service_enter_restart(Service *s) {
1606 service_enter_dead(s, true, false);
1608 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1611 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
1616 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
1617 service_enter_dead(s, false, false);
1620 static void service_enter_reload(Service *s) {
1625 service_unwatch_control_pid(s);
1627 s->control_command_id = SERVICE_EXEC_RELOAD;
1628 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1629 if ((r = service_spawn(s,
1633 !s->permissions_start_only,
1634 !s->root_directory_start_only,
1635 &s->control_pid)) < 0)
1638 service_set_state(s, SERVICE_RELOAD);
1640 service_enter_running(s, true);
1645 log_warning("%s failed to run reload executable: %s", UNIT(s)->meta.id, strerror(-r));
1646 service_enter_stop(s, false);
1649 static void service_run_next(Service *s, bool success) {
1653 assert(s->control_command);
1654 assert(s->control_command->command_next);
1659 s->control_command = s->control_command->command_next;
1661 service_unwatch_control_pid(s);
1663 if ((r = service_spawn(s,
1667 !s->permissions_start_only,
1668 !s->root_directory_start_only,
1669 &s->control_pid)) < 0)
1675 log_warning("%s failed to run spawn next executable: %s", UNIT(s)->meta.id, strerror(-r));
1677 if (s->state == SERVICE_START_PRE)
1678 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1679 else if (s->state == SERVICE_STOP)
1680 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1681 else if (s->state == SERVICE_STOP_POST)
1682 service_enter_dead(s, false, true);
1684 service_enter_stop(s, false);
1687 static int service_start(Unit *u) {
1688 Service *s = SERVICE(u);
1692 /* We cannot fulfill this request right now, try again later
1694 if (s->state == SERVICE_STOP ||
1695 s->state == SERVICE_STOP_SIGTERM ||
1696 s->state == SERVICE_STOP_SIGKILL ||
1697 s->state == SERVICE_STOP_POST ||
1698 s->state == SERVICE_FINAL_SIGTERM ||
1699 s->state == SERVICE_FINAL_SIGKILL)
1702 /* Already on it! */
1703 if (s->state == SERVICE_START_PRE ||
1704 s->state == SERVICE_START ||
1705 s->state == SERVICE_START_POST)
1708 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1710 /* Make sure we don't enter a busy loop of some kind. */
1711 if (!ratelimit_test(&s->ratelimit)) {
1712 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1717 s->main_pid_known = false;
1718 s->allow_restart = true;
1720 service_enter_start_pre(s);
1724 static int service_stop(Unit *u) {
1725 Service *s = SERVICE(u);
1729 /* Cannot do this now */
1730 if (s->state == SERVICE_START_PRE ||
1731 s->state == SERVICE_START ||
1732 s->state == SERVICE_START_POST ||
1733 s->state == SERVICE_RELOAD)
1737 if (s->state == SERVICE_STOP ||
1738 s->state == SERVICE_STOP_SIGTERM ||
1739 s->state == SERVICE_STOP_SIGKILL ||
1740 s->state == SERVICE_STOP_POST ||
1741 s->state == SERVICE_FINAL_SIGTERM ||
1742 s->state == SERVICE_FINAL_SIGKILL)
1745 if (s->state == SERVICE_AUTO_RESTART) {
1746 service_set_state(s, SERVICE_DEAD);
1750 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1752 /* This is a user request, so don't do restarts on this
1754 s->allow_restart = false;
1756 service_enter_stop(s, true);
1760 static int service_reload(Unit *u) {
1761 Service *s = SERVICE(u);
1765 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1767 service_enter_reload(s);
1771 static bool service_can_reload(Unit *u) {
1772 Service *s = SERVICE(u);
1776 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1779 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1780 Service *s = SERVICE(u);
1786 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1787 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1789 if (s->control_pid > 0)
1790 unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) (s->control_pid));
1792 if (s->main_pid > 0)
1793 unit_serialize_item_format(u, f, "main-pid", "%u", (unsigned) (s->main_pid));
1795 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1797 /* There's a minor uncleanliness here: if there are multiple
1798 * commands attached here, we will start from the first one
1800 if (s->control_command_id >= 0)
1801 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
1803 if (s->socket_fd >= 0) {
1806 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1809 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1815 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1816 Service *s = SERVICE(u);
1824 if (streq(key, "state")) {
1827 if ((state = service_state_from_string(value)) < 0)
1828 log_debug("Failed to parse state value %s", value);
1830 s->deserialized_state = state;
1831 } else if (streq(key, "failure")) {
1834 if ((b = parse_boolean(value)) < 0)
1835 log_debug("Failed to parse failure value %s", value);
1837 s->failure = b || s->failure;
1838 } else if (streq(key, "control-pid")) {
1841 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1842 log_debug("Failed to parse control-pid value %s", value);
1844 s->control_pid = (pid_t) pid;
1845 } else if (streq(key, "main-pid")) {
1848 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1849 log_debug("Failed to parse main-pid value %s", value);
1851 s->main_pid = (pid_t) pid;
1852 } else if (streq(key, "main-pid-known")) {
1855 if ((b = parse_boolean(value)) < 0)
1856 log_debug("Failed to parse main-pid-known value %s", value);
1858 s->main_pid_known = b;
1859 } else if (streq(key, "control-command")) {
1860 ServiceExecCommand id;
1862 if ((id = service_exec_command_from_string(value)) < 0)
1863 log_debug("Failed to parse exec-command value %s", value);
1865 s->control_command_id = id;
1866 s->control_command = s->exec_command[id];
1868 } else if (streq(key, "socket-fd")) {
1871 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1872 log_debug("Failed to parse socket-fd value %s", value);
1875 if (s->socket_fd >= 0)
1876 close_nointr_nofail(s->socket_fd);
1877 s->socket_fd = fdset_remove(fds, fd);
1880 log_debug("Unknown serialization key '%s'", key);
1885 static UnitActiveState service_active_state(Unit *u) {
1888 return state_translation_table[SERVICE(u)->state];
1891 static const char *service_sub_state_to_string(Unit *u) {
1894 return service_state_to_string(SERVICE(u)->state);
1897 static bool service_check_gc(Unit *u) {
1898 Service *s = SERVICE(u);
1902 return !!s->sysv_path;
1905 static bool service_check_snapshot(Unit *u) {
1906 Service *s = SERVICE(u);
1910 return !s->got_socket_fd;
1913 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1914 Service *s = SERVICE(u);
1920 success = is_clean_exit(code, status);
1921 s->failure = s->failure || !success;
1923 if (s->main_pid == pid) {
1925 exec_status_fill(&s->main_exec_status, pid, code, status);
1928 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1929 assert(s->exec_command[SERVICE_EXEC_START]);
1930 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1933 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
1935 /* The service exited, so the service is officially
1940 case SERVICE_START_POST:
1941 case SERVICE_RELOAD:
1943 /* Need to wait until the operation is
1948 assert(s->type == SERVICE_FINISH);
1950 /* This was our main goal, so let's go on */
1952 service_enter_start_post(s);
1954 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1957 case SERVICE_RUNNING:
1958 service_enter_running(s, success);
1961 case SERVICE_STOP_SIGTERM:
1962 case SERVICE_STOP_SIGKILL:
1964 if (!control_pid_good(s))
1965 service_enter_stop_post(s, success);
1967 /* If there is still a control process, wait for that first */
1971 assert_not_reached("Uh, main process died at wrong time.");
1974 } else if (s->control_pid == pid) {
1976 if (s->control_command)
1977 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1981 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
1983 /* If we are shutting things down anyway we
1984 * don't care about failing commands. */
1986 if (s->control_command && s->control_command->command_next && success) {
1988 /* There is another command to *
1989 * execute, so let's do that. */
1991 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
1992 service_run_next(s, success);
1995 /* No further commands for this step, so let's
1996 * figure out what to do next */
1998 s->control_command = NULL;
1999 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2001 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2005 case SERVICE_START_PRE:
2007 service_enter_start(s);
2009 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2013 assert(s->type == SERVICE_FORKING);
2015 /* Let's try to load the pid
2016 * file here if we can. We
2017 * ignore the return value,
2018 * since the PID file might
2019 * actually be created by a
2020 * START_POST script */
2024 service_load_pid_file(s);
2026 service_enter_start_post(s);
2028 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2032 case SERVICE_START_POST:
2033 if (success && s->pid_file && !s->main_pid_known) {
2036 /* Hmm, let's see if we can
2037 * load the pid now after the
2038 * start-post scripts got
2041 if ((r = service_load_pid_file(s)) < 0)
2042 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
2047 case SERVICE_RELOAD:
2049 service_enter_running(s, true);
2051 service_enter_stop(s, false);
2056 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2059 case SERVICE_STOP_SIGTERM:
2060 case SERVICE_STOP_SIGKILL:
2061 if (main_pid_good(s) <= 0)
2062 service_enter_stop_post(s, success);
2064 /* If there is still a service
2065 * process around, wait until
2066 * that one quit, too */
2069 case SERVICE_STOP_POST:
2070 case SERVICE_FINAL_SIGTERM:
2071 case SERVICE_FINAL_SIGKILL:
2072 service_enter_dead(s, success, true);
2076 assert_not_reached("Uh, control process died at wrong time.");
2080 assert_not_reached("Got SIGCHLD for unkown PID");
2083 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2084 Service *s = SERVICE(u);
2087 assert(elapsed == 1);
2089 assert(w == &s->timer_watch);
2093 case SERVICE_START_PRE:
2095 log_warning("%s operation timed out. Terminating.", u->meta.id);
2096 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2099 case SERVICE_START_POST:
2100 case SERVICE_RELOAD:
2101 log_warning("%s operation timed out. Stopping.", u->meta.id);
2102 service_enter_stop(s, false);
2106 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2107 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2110 case SERVICE_STOP_SIGTERM:
2111 log_warning("%s stopping timed out. Killing.", u->meta.id);
2112 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2115 case SERVICE_STOP_SIGKILL:
2116 /* Uh, wie sent a SIGKILL and it is still not gone?
2117 * Must be something we cannot kill, so let's just be
2118 * weirded out and continue */
2120 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2121 service_enter_stop_post(s, false);
2124 case SERVICE_STOP_POST:
2125 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2126 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2129 case SERVICE_FINAL_SIGTERM:
2130 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2131 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2134 case SERVICE_FINAL_SIGKILL:
2135 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u->meta.id);
2136 service_enter_dead(s, false, true);
2139 case SERVICE_AUTO_RESTART:
2140 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
2141 service_enter_restart(s);
2145 assert_not_reached("Timeout at wrong time.");
2149 static void service_cgroup_notify_event(Unit *u) {
2150 Service *s = SERVICE(u);
2154 log_debug("%s: cgroup is empty", u->meta.id);
2158 /* Waiting for SIGCHLD is usually more interesting,
2159 * because it includes return codes/signals. Which is
2160 * why we ignore the cgroup events for most cases,
2161 * except when we don't know pid which to expect the
2164 case SERVICE_RUNNING:
2165 service_enter_running(s, true);
2173 static int service_enumerate(Manager *m) {
2177 char *path = NULL, *fpath = NULL, *name = NULL;
2182 STRV_FOREACH(p, m->sysvrcnd_path)
2183 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2188 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2196 if (!(d = opendir(path))) {
2197 if (errno != ENOENT)
2198 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2203 while ((de = readdir(d))) {
2207 if (ignore_file(de->d_name))
2210 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2213 if (strlen(de->d_name) < 4)
2216 a = undecchar(de->d_name[1]);
2217 b = undecchar(de->d_name[2]);
2224 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2229 if (access(fpath, X_OK) < 0) {
2231 if (errno != ENOENT)
2232 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2238 if (!(name = sysv_translate_name(de->d_name + 3))) {
2243 if ((r = manager_load_unit_prepare(m, name, NULL, &service)) < 0) {
2244 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2248 if (de->d_name[0] == 'S' &&
2249 (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_BASIC))
2250 SERVICE(service)->sysv_start_priority =
2251 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2253 manager_dispatch_load_queue(m);
2254 service = unit_follow_merge(service);
2256 if (de->d_name[0] == 'S') {
2257 Unit *runlevel_target;
2259 if ((r = manager_load_unit(m, rcnd_table[i].target, NULL, &runlevel_target)) < 0)
2262 if ((r = unit_add_dependency(runlevel_target, UNIT_WANTS, service, true)) < 0)
2265 if ((r = unit_add_dependency(service, UNIT_BEFORE, runlevel_target, true)) < 0)
2268 } else if (de->d_name[0] == 'K' && rcnd_table[i].type == RUNLEVEL_DOWN) {
2269 Unit *shutdown_target;
2271 /* We honour K links only for
2272 * halt/reboot. For the normal
2273 * runlevels we assume the
2275 * implicitly added by the
2276 * core logic. Also, we don't
2277 * really distuingish here
2278 * between the runlevels 0 and
2279 * 6 and just add them to the
2280 * special shutdown target. */
2282 if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
2285 if ((r = unit_add_dependency(service, UNIT_CONFLICTS, shutdown_target, true)) < 0)
2288 if ((r = unit_add_dependency(service, UNIT_BEFORE, shutdown_target, true)) < 0)
2307 static void service_bus_name_owner_change(
2310 const char *old_owner,
2311 const char *new_owner) {
2313 Service *s = SERVICE(u);
2318 assert(streq(s->bus_name, name));
2319 assert(old_owner || new_owner);
2321 if (old_owner && new_owner)
2322 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2324 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2326 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2328 s->bus_name_good = !!new_owner;
2330 if (s->type == SERVICE_DBUS) {
2332 /* service_enter_running() will figure out what to
2334 if (s->state == SERVICE_RUNNING)
2335 service_enter_running(s, true);
2336 else if (s->state == SERVICE_START && new_owner)
2337 service_enter_start_post(s);
2339 } else if (new_owner &&
2341 (s->state == SERVICE_START ||
2342 s->state == SERVICE_START_POST ||
2343 s->state == SERVICE_RUNNING ||
2344 s->state == SERVICE_RELOAD)) {
2346 /* Try to acquire PID from bus service */
2347 log_debug("Trying to acquire PID from D-Bus name...");
2349 bus_query_pid(u->meta.manager, name);
2353 static void service_bus_query_pid_done(
2358 Service *s = SERVICE(u);
2363 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2365 if (s->main_pid <= 0 &&
2366 (s->state == SERVICE_START ||
2367 s->state == SERVICE_START_POST ||
2368 s->state == SERVICE_RUNNING ||
2369 s->state == SERVICE_RELOAD))
2373 int service_set_socket_fd(Service *s, int fd) {
2377 /* This is called by the socket code when instantiating a new
2378 * service for a stream socket and the socket needs to be
2381 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2384 if (s->socket_fd >= 0)
2387 if (s->state != SERVICE_DEAD)
2391 s->got_socket_fd = true;
2395 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2396 [SERVICE_DEAD] = "dead",
2397 [SERVICE_START_PRE] = "start-pre",
2398 [SERVICE_START] = "start",
2399 [SERVICE_START_POST] = "start-post",
2400 [SERVICE_RUNNING] = "running",
2401 [SERVICE_EXITED] = "exited",
2402 [SERVICE_RELOAD] = "reload",
2403 [SERVICE_STOP] = "stop",
2404 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2405 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2406 [SERVICE_STOP_POST] = "stop-post",
2407 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2408 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2409 [SERVICE_MAINTAINANCE] = "maintainance",
2410 [SERVICE_AUTO_RESTART] = "auto-restart",
2413 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2415 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2416 [SERVICE_ONCE] = "once",
2417 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2418 [SERVICE_RESTART_ALWAYS] = "restart-always",
2421 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2423 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2424 [SERVICE_FORKING] = "forking",
2425 [SERVICE_SIMPLE] = "simple",
2426 [SERVICE_FINISH] = "finish",
2427 [SERVICE_DBUS] = "dbus"
2430 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2432 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2433 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2434 [SERVICE_EXEC_START] = "ExecStart",
2435 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2436 [SERVICE_EXEC_RELOAD] = "ExecReload",
2437 [SERVICE_EXEC_STOP] = "ExecStop",
2438 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2441 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2443 const UnitVTable service_vtable = {
2444 .suffix = ".service",
2446 .init = service_init,
2447 .done = service_done,
2448 .load = service_load,
2450 .coldplug = service_coldplug,
2452 .dump = service_dump,
2454 .start = service_start,
2455 .stop = service_stop,
2456 .reload = service_reload,
2458 .can_reload = service_can_reload,
2460 .serialize = service_serialize,
2461 .deserialize_item = service_deserialize_item,
2463 .active_state = service_active_state,
2464 .sub_state_to_string = service_sub_state_to_string,
2466 .check_gc = service_check_gc,
2467 .check_snapshot = service_check_snapshot,
2469 .sigchld_event = service_sigchld_event,
2470 .timer_event = service_timer_event,
2472 .cgroup_notify_empty = service_cgroup_notify_event,
2474 .bus_name_owner_change = service_bus_name_owner_change,
2475 .bus_query_pid_done = service_bus_query_pid_done,
2477 .bus_message_handler = bus_service_message_handler,
2479 .enumerate = service_enumerate