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 int sysv_translate_name(const char *name, char **_r) {
174 static const char * const table[] = {
175 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
176 "$network", SPECIAL_NETWORK_TARGET,
177 "$named", SPECIAL_NSS_LOOKUP_TARGET,
178 "$portmap", SPECIAL_RPCBIND_TARGET,
179 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
180 "$syslog", SPECIAL_SYSLOG_TARGET,
181 "$time", SPECIAL_RTC_SET_TARGET
187 for (i = 0; i < ELEMENTSOF(table); i += 2)
188 if (streq(table[i], name)) {
189 if (!(r = strdup(table[i+1])))
198 if (asprintf(&r, "%s.service", name) < 0)
209 static int sysv_chkconfig_order(Service *s) {
215 if (s->sysv_start_priority < 0)
218 /* For each pair of services where at least one lacks a LSB
219 * header, we use the start priority value to order things. */
221 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
225 t = (Service*) other;
230 if (t->sysv_start_priority < 0)
233 /* If both units have modern headers we don't care
234 * about the priorities */
235 if ((!s->sysv_path || s->sysv_has_lsb) &&
236 (!t->sysv_path || t->sysv_has_lsb))
239 if (t->sysv_start_priority < s->sysv_start_priority)
241 else if (t->sysv_start_priority > s->sysv_start_priority)
246 /* FIXME: Maybe we should compare the name here lexicographically? */
248 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
255 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
258 if (!(c = new0(ExecCommand, 1)))
261 if (!(c->path = strdup(path))) {
266 if (!(c->argv = strv_new(path, arg1, NULL))) {
275 static int sysv_exec_commands(Service *s) {
279 assert(s->sysv_path);
281 if (!(c = exec_command_new(s->sysv_path, "start")))
283 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
285 if (!(c = exec_command_new(s->sysv_path, "stop")))
287 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
289 if (!(c = exec_command_new(s->sysv_path, "reload")))
291 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
296 static int service_load_sysv_path(Service *s, const char *path) {
313 if (!(f = fopen(path, "re"))) {
314 r = errno == ENOENT ? 0 : -errno;
318 s->type = SERVICE_FORKING;
319 s->restart = SERVICE_ONCE;
322 if (!(s->sysv_path = strdup(path))) {
328 char l[LINE_MAX], *t;
330 if (!fgets(l, sizeof(l), f)) {
335 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
345 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
347 s->sysv_has_lsb = true;
351 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
357 t += strspn(t, WHITESPACE);
359 if (state == NORMAL) {
361 /* Try to parse Red Hat style chkconfig headers */
363 if (startswith(t, "chkconfig:")) {
365 char runlevels[16], *k;
369 if (sscanf(t+10, "%15s %i %*i",
371 &start_priority) != 2) {
373 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
377 /* A start priority gathered from the
378 * symlink farms is preferred over the
379 * data from the LSB header. */
380 if (start_priority < 0 || start_priority > 99)
381 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
382 else if (s->sysv_start_priority < 0)
383 s->sysv_start_priority = start_priority;
385 char_array_0(runlevels);
386 k = delete_chars(runlevels, WHITESPACE "-");
391 if (!(d = strdup(k))) {
396 free(s->sysv_runlevels);
397 s->sysv_runlevels = d;
400 } else if (startswith(t, "description:")) {
402 size_t k = strlen(t);
405 if (t[k-1] == '\\') {
410 if (!(d = strdup(strstrip(t+12)))) {
415 free(u->meta.description);
416 u->meta.description = d;
418 } else if (startswith(t, "pidfile:")) {
425 if (!path_is_absolute(fn)) {
426 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
430 if (!(fn = strdup(fn))) {
439 } else if (state == DESCRIPTION) {
441 /* Try to parse Red Hat style description
444 size_t k = strlen(t);
452 assert(u->meta.description);
453 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
458 free(u->meta.description);
459 u->meta.description = d;
461 } else if (state == LSB || state == LSB_DESCRIPTION) {
463 if (startswith(t, "Provides:")) {
469 FOREACH_WORD(w, z, t+9, i) {
472 if (!(n = strndup(w, z))) {
477 r = sysv_translate_name(n, &m);
486 if (unit_name_to_type(m) == UNIT_SERVICE)
487 r = unit_add_name(u, m);
489 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL, true)) >= 0)
490 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
499 } else if (startswith(t, "Required-Start:") ||
500 startswith(t, "Should-Start:")) {
506 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
509 if (!(n = strndup(w, z))) {
514 r = sysv_translate_name(n, &m);
523 r = unit_add_dependency_by_name(u, UNIT_AFTER, m, NULL, true);
529 } else if (startswith(t, "Default-Start:")) {
534 k = delete_chars(t+14, WHITESPACE "-");
537 if (!(d = strdup(k))) {
542 free(s->sysv_runlevels);
543 s->sysv_runlevels = d;
546 } else if (startswith(t, "Description:")) {
549 state = LSB_DESCRIPTION;
551 if (!(d = strdup(strstrip(t+12)))) {
556 free(u->meta.description);
557 u->meta.description = d;
559 } else if (startswith(t, "Short-Description:") &&
560 !u->meta.description) {
563 /* We use the short description only
564 * if no long description is set. */
568 if (!(d = strdup(strstrip(t+18)))) {
573 u->meta.description = d;
575 } else if (state == LSB_DESCRIPTION) {
577 if (startswith(l, "#\t") || startswith(l, "# ")) {
580 assert(u->meta.description);
581 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
586 free(u->meta.description);
587 u->meta.description = d;
594 if ((r = sysv_exec_commands(s)) < 0)
597 if (!s->sysv_runlevels || chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
598 /* If there a runlevels configured for this service
599 * but none of the standard ones, then we assume this
600 * is some special kind of service (which might be
601 * needed for early boot) and don't create any links
604 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0 ||
605 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
609 /* Don't timeout special services during boot (like fsck) */
612 /* Special setting for all SysV services */
613 s->valid_no_process = true;
614 s->kill_mode = KILL_PROCESS_GROUP;
616 u->meta.load_state = UNIT_LOADED;
627 static int service_load_sysv_name(Service *s, const char *name) {
633 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
637 if (asprintf(&path, "%s/%s", *p, name) < 0)
640 assert(endswith(path, ".service"));
641 path[strlen(path)-8] = 0;
643 r = service_load_sysv_path(s, path);
645 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
646 /* Try Debian style .sh source'able init scripts */
648 r = service_load_sysv_path(s, path);
653 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
654 /* Try Suse style boot.xxxx init scripts */
656 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
659 path[strlen(path)-8] = 0;
660 r = service_load_sysv_path(s, path);
667 if ((UNIT(s)->meta.load_state != UNIT_STUB))
674 static int service_load_sysv(Service *s) {
681 /* Load service data from SysV init scripts, preferably with
684 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
687 if ((t = UNIT(s)->meta.id))
688 if ((r = service_load_sysv_name(s, t)) < 0)
691 if (UNIT(s)->meta.load_state == UNIT_STUB)
692 SET_FOREACH(t, UNIT(s)->meta.names, i) {
693 if (t == UNIT(s)->meta.id)
696 if ((r == service_load_sysv_name(s, t)) < 0)
699 if (UNIT(s)->meta.load_state != UNIT_STUB)
706 static int service_add_bus_name(Service *s) {
713 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
716 r = unit_merge_by_name(UNIT(s), n);
722 static int service_verify(Service *s) {
725 if (UNIT(s)->meta.load_state != UNIT_LOADED)
728 if (!s->exec_command[SERVICE_EXEC_START]) {
729 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
733 if (s->type == SERVICE_DBUS && !s->bus_name) {
734 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->meta.id);
741 static int service_load(Unit *u) {
743 Service *s = SERVICE(u);
747 /* Load a .service file */
748 if ((r = unit_load_fragment(u)) < 0)
751 /* Load a classic init script as a fallback, if we couldn't find anything */
752 if (u->meta.load_state == UNIT_STUB)
753 if ((r = service_load_sysv(s)) < 0)
756 /* Still nothing found? Then let's give up */
757 if (u->meta.load_state == UNIT_STUB)
760 /* We were able to load something, then let's add in the
761 * dropin directories. */
762 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
765 /* This is a new unit? Then let's add in some extras */
766 if (u->meta.load_state == UNIT_LOADED) {
767 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
770 if ((r = unit_add_default_cgroup(u)) < 0)
773 if ((r = sysv_chkconfig_order(s)) < 0)
777 if ((r = service_add_bus_name(s)) < 0)
780 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
785 return service_verify(s);
788 static void service_dump(Unit *u, FILE *f, const char *prefix) {
790 ServiceExecCommand c;
791 Service *s = SERVICE(u);
797 p2 = strappend(prefix, "\t");
798 prefix2 = p2 ? p2 : prefix;
801 "%sService State: %s\n"
802 "%sPermissionsStartOnly: %s\n"
803 "%sRootDirectoryStartOnly: %s\n"
804 "%sValidNoProcess: %s\n"
807 prefix, service_state_to_string(s->state),
808 prefix, yes_no(s->permissions_start_only),
809 prefix, yes_no(s->root_directory_start_only),
810 prefix, yes_no(s->valid_no_process),
811 prefix, kill_mode_to_string(s->kill_mode),
812 prefix, service_type_to_string(s->type));
814 if (s->control_pid > 0)
816 "%sControl PID: %llu\n",
817 prefix, (unsigned long long) s->control_pid);
821 "%sMain PID: %llu\n",
822 prefix, (unsigned long long) s->main_pid);
827 prefix, s->pid_file);
832 "%sBus Name Good: %s\n",
834 prefix, yes_no(s->bus_name_good));
836 exec_context_dump(&s->exec_context, f, prefix);
838 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
840 if (!s->exec_command[c])
843 fprintf(f, "%s-> %s:\n",
844 prefix, service_exec_command_to_string(c));
846 exec_command_dump_list(s->exec_command[c], f, prefix2);
851 "%sSysV Init Script Path: %s\n"
852 "%sSysV Init Script has LSB Header: %s\n",
853 prefix, s->sysv_path,
854 prefix, yes_no(s->sysv_has_lsb));
856 if (s->sysv_start_priority >= 0)
858 "%sSysVStartPriority: %i\n",
859 prefix, s->sysv_start_priority);
861 if (s->sysv_runlevels)
862 fprintf(f, "%sSysVRunLevels: %s\n",
863 prefix, s->sysv_runlevels);
868 static int service_load_pid_file(Service *s) {
875 if (s->main_pid_known)
878 assert(s->main_pid <= 0);
883 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
886 if ((r = safe_atolu(k, &p)) < 0) {
891 if ((unsigned long) (pid_t) p != p)
894 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
895 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
896 (unsigned long long) p, s->pid_file);
900 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
901 /* FIXME: we need to do something here */
904 s->main_pid = (pid_t) p;
905 s->main_pid_known = true;
910 static int service_get_sockets(Service *s, Set **_set) {
919 /* Collects all Socket objects that belong to this
920 * service. Note that a service might have multiple sockets
921 * via multiple names. */
923 if (!(set = set_new(NULL, NULL)))
926 SET_FOREACH(t, UNIT(s)->meta.names, i) {
930 /* Look for all socket objects that go by any of our
931 * units and collect their fds */
933 if (!(k = unit_name_change_suffix(t, ".socket"))) {
938 p = manager_get_unit(UNIT(s)->meta.manager, k);
944 if ((r = set_put(set, p)) < 0)
956 static int service_notify_sockets_dead(Service *s) {
964 /* Notifies all our sockets when we die */
965 if ((r = service_get_sockets(s, &set)) < 0)
968 SET_FOREACH(sock, set, i)
969 socket_notify_service_dead(sock);
976 static void service_set_state(Service *s, ServiceState state) {
977 ServiceState old_state;
980 old_state = s->state;
983 if (state != SERVICE_START_PRE &&
984 state != SERVICE_START &&
985 state != SERVICE_START_POST &&
986 state != SERVICE_RELOAD &&
987 state != SERVICE_STOP &&
988 state != SERVICE_STOP_SIGTERM &&
989 state != SERVICE_STOP_SIGKILL &&
990 state != SERVICE_STOP_POST &&
991 state != SERVICE_FINAL_SIGTERM &&
992 state != SERVICE_FINAL_SIGKILL &&
993 state != SERVICE_AUTO_RESTART)
994 unit_unwatch_timer(UNIT(s), &s->timer_watch);
996 if (state != SERVICE_START &&
997 state != SERVICE_START_POST &&
998 state != SERVICE_RUNNING &&
999 state != SERVICE_RELOAD &&
1000 state != SERVICE_STOP &&
1001 state != SERVICE_STOP_SIGTERM &&
1002 state != SERVICE_STOP_SIGKILL)
1003 service_unwatch_main_pid(s);
1005 if (state != SERVICE_START_PRE &&
1006 state != SERVICE_START &&
1007 state != SERVICE_START_POST &&
1008 state != SERVICE_RELOAD &&
1009 state != SERVICE_STOP &&
1010 state != SERVICE_STOP_SIGTERM &&
1011 state != SERVICE_STOP_SIGKILL &&
1012 state != SERVICE_STOP_POST &&
1013 state != SERVICE_FINAL_SIGTERM &&
1014 state != SERVICE_FINAL_SIGKILL) {
1015 service_unwatch_control_pid(s);
1016 s->control_command = NULL;
1017 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1020 if (state == SERVICE_DEAD ||
1021 state == SERVICE_STOP ||
1022 state == SERVICE_STOP_SIGTERM ||
1023 state == SERVICE_STOP_SIGKILL ||
1024 state == SERVICE_STOP_POST ||
1025 state == SERVICE_FINAL_SIGTERM ||
1026 state == SERVICE_FINAL_SIGKILL ||
1027 state == SERVICE_MAINTAINANCE ||
1028 state == SERVICE_AUTO_RESTART)
1029 service_notify_sockets_dead(s);
1031 if (state != SERVICE_START_PRE &&
1032 state != SERVICE_START &&
1033 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1034 service_close_socket_fd(s);
1036 if (old_state != state)
1037 log_debug("%s changed %s -> %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1039 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1042 static int service_coldplug(Unit *u) {
1043 Service *s = SERVICE(u);
1047 assert(s->state == SERVICE_DEAD);
1049 if (s->deserialized_state != s->state) {
1051 if (s->deserialized_state == SERVICE_START_PRE ||
1052 s->deserialized_state == SERVICE_START ||
1053 s->deserialized_state == SERVICE_START_POST ||
1054 s->deserialized_state == SERVICE_RELOAD ||
1055 s->deserialized_state == SERVICE_STOP ||
1056 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1057 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1058 s->deserialized_state == SERVICE_STOP_POST ||
1059 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1060 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1061 s->deserialized_state == SERVICE_AUTO_RESTART) {
1063 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1066 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1068 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1073 if ((s->deserialized_state == SERVICE_START &&
1074 (s->type == SERVICE_FORKING ||
1075 s->type == SERVICE_DBUS)) ||
1076 s->deserialized_state == SERVICE_START_POST ||
1077 s->deserialized_state == SERVICE_RUNNING ||
1078 s->deserialized_state == SERVICE_RELOAD ||
1079 s->deserialized_state == SERVICE_STOP ||
1080 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1081 s->deserialized_state == SERVICE_STOP_SIGKILL)
1082 if (s->main_pid > 0)
1083 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1086 if (s->deserialized_state == SERVICE_START_PRE ||
1087 s->deserialized_state == SERVICE_START ||
1088 s->deserialized_state == SERVICE_START_POST ||
1089 s->deserialized_state == SERVICE_RELOAD ||
1090 s->deserialized_state == SERVICE_STOP ||
1091 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1092 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1093 s->deserialized_state == SERVICE_STOP_POST ||
1094 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1095 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1096 if (s->control_pid > 0)
1097 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1100 service_set_state(s, s->deserialized_state);
1106 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1110 unsigned rn_fds = 0;
1118 if ((r = service_get_sockets(s, &set)) < 0)
1121 SET_FOREACH(sock, set, i) {
1125 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1137 if (!(t = new(int, rn_fds+cn_fds))) {
1143 memcpy(t, rfds, rn_fds);
1144 memcpy(t+rn_fds, cfds, cn_fds);
1149 rn_fds = rn_fds+cn_fds;
1167 static int service_spawn(
1172 bool apply_permissions,
1187 if (s->socket_fd >= 0) {
1188 fds = &s->socket_fd;
1190 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1194 if (timeout && s->timeout_usec) {
1195 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1198 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1200 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1209 s->meta.manager->environment,
1212 UNIT(s)->meta.manager->confirm_spawn,
1213 UNIT(s)->meta.cgroup_bondings,
1221 if (s->socket_fd >= 0)
1222 service_close_socket_fd(s);
1227 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1228 /* FIXME: we need to do something here */
1239 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1244 static int main_pid_good(Service *s) {
1247 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1250 /* If we know the pid file, then lets just check if it is
1252 if (s->main_pid_known)
1253 return s->main_pid > 0;
1255 /* We don't know the pid */
1259 static int control_pid_good(Service *s) {
1262 return s->control_pid > 0;
1265 static int cgroup_good(Service *s) {
1270 if (s->valid_no_process)
1273 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1279 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1286 if (allow_restart &&
1288 (s->restart == SERVICE_RESTART_ALWAYS ||
1289 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1291 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1294 service_set_state(s, SERVICE_AUTO_RESTART);
1296 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1301 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
1302 service_enter_dead(s, false, false);
1305 static void service_enter_signal(Service *s, ServiceState state, bool success);
1307 static void service_enter_stop_post(Service *s, bool success) {
1314 service_unwatch_control_pid(s);
1316 s->control_command_id = SERVICE_EXEC_STOP_POST;
1317 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1318 if ((r = service_spawn(s,
1322 !s->permissions_start_only,
1323 !s->root_directory_start_only,
1324 &s->control_pid)) < 0)
1328 service_set_state(s, SERVICE_STOP_POST);
1330 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1335 log_warning("%s failed to run stop-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1336 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1339 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1348 if (s->kill_mode != KILL_NONE) {
1349 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1351 if (s->kill_mode == KILL_CONTROL_GROUP) {
1353 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1354 if (r != -EAGAIN && r != -ESRCH)
1363 if (s->main_pid > 0) {
1364 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1370 if (s->control_pid > 0) {
1371 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1382 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1383 if (s->timeout_usec > 0)
1384 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1387 service_set_state(s, state);
1388 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1389 service_enter_stop_post(s, true);
1391 service_enter_dead(s, true, true);
1396 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
1398 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1399 service_enter_stop_post(s, false);
1401 service_enter_dead(s, false, true);
1404 static void service_enter_stop(Service *s, bool success) {
1411 service_unwatch_control_pid(s);
1413 s->control_command_id = SERVICE_EXEC_STOP;
1414 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1415 if ((r = service_spawn(s,
1419 !s->permissions_start_only,
1420 !s->root_directory_start_only,
1421 &s->control_pid)) < 0)
1424 service_set_state(s, SERVICE_STOP);
1426 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1431 log_warning("%s failed to run stop executable: %s", UNIT(s)->meta.id, strerror(-r));
1432 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1435 static void service_enter_running(Service *s, bool success) {
1441 if (main_pid_good(s) != 0 &&
1442 cgroup_good(s) != 0 &&
1443 (s->bus_name_good || s->type != SERVICE_DBUS))
1444 service_set_state(s, SERVICE_RUNNING);
1445 else if (s->valid_no_process)
1446 service_set_state(s, SERVICE_EXITED);
1448 service_enter_stop(s, true);
1451 static void service_enter_start_post(Service *s) {
1455 service_unwatch_control_pid(s);
1457 s->control_command_id = SERVICE_EXEC_START_POST;
1458 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1459 if ((r = service_spawn(s,
1463 !s->permissions_start_only,
1464 !s->root_directory_start_only,
1465 &s->control_pid)) < 0)
1469 service_set_state(s, SERVICE_START_POST);
1471 service_enter_running(s, true);
1476 log_warning("%s failed to run start-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1477 service_enter_stop(s, false);
1480 static void service_enter_start(Service *s) {
1486 assert(s->exec_command[SERVICE_EXEC_START]);
1487 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1489 if (s->type == SERVICE_FORKING)
1490 service_unwatch_control_pid(s);
1492 service_unwatch_main_pid(s);
1494 if ((r = service_spawn(s,
1495 s->exec_command[SERVICE_EXEC_START],
1496 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS,
1503 if (s->type == SERVICE_SIMPLE) {
1504 /* For simple services we immediately start
1505 * the START_POST binaries. */
1508 s->main_pid_known = true;
1510 service_enter_start_post(s);
1512 } else if (s->type == SERVICE_FORKING) {
1514 /* For forking services we wait until the start
1515 * process exited. */
1517 s->control_pid = pid;
1519 s->control_command_id = SERVICE_EXEC_START;
1520 s->control_command = s->exec_command[SERVICE_EXEC_START];
1521 service_set_state(s, SERVICE_START);
1523 } else if (s->type == SERVICE_FINISH ||
1524 s->type == SERVICE_DBUS) {
1526 /* For finishing services we wait until the start
1527 * process exited, too, but it is our main process. */
1529 /* For D-Bus services we know the main pid right away,
1530 * but wait for the bus name to appear on the bus. */
1533 s->main_pid_known = true;
1535 service_set_state(s, SERVICE_START);
1537 assert_not_reached("Unknown service type");
1542 log_warning("%s failed to run start exectuable: %s", UNIT(s)->meta.id, strerror(-r));
1543 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1546 static void service_enter_start_pre(Service *s) {
1551 service_unwatch_control_pid(s);
1553 s->control_command_id = SERVICE_EXEC_START_PRE;
1554 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1555 if ((r = service_spawn(s,
1559 !s->permissions_start_only,
1560 !s->root_directory_start_only,
1561 &s->control_pid)) < 0)
1564 service_set_state(s, SERVICE_START_PRE);
1566 service_enter_start(s);
1571 log_warning("%s failed to run start-pre executable: %s", UNIT(s)->meta.id, strerror(-r));
1572 service_enter_dead(s, false, true);
1575 static void service_enter_restart(Service *s) {
1579 service_enter_dead(s, true, false);
1581 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1584 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
1589 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
1590 service_enter_dead(s, false, false);
1593 static void service_enter_reload(Service *s) {
1598 service_unwatch_control_pid(s);
1600 s->control_command_id = SERVICE_EXEC_RELOAD;
1601 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1602 if ((r = service_spawn(s,
1606 !s->permissions_start_only,
1607 !s->root_directory_start_only,
1608 &s->control_pid)) < 0)
1611 service_set_state(s, SERVICE_RELOAD);
1613 service_enter_running(s, true);
1618 log_warning("%s failed to run reload executable: %s", UNIT(s)->meta.id, strerror(-r));
1619 service_enter_stop(s, false);
1622 static void service_run_next(Service *s, bool success) {
1626 assert(s->control_command);
1627 assert(s->control_command->command_next);
1632 s->control_command = s->control_command->command_next;
1634 service_unwatch_control_pid(s);
1636 if ((r = service_spawn(s,
1640 !s->permissions_start_only,
1641 !s->root_directory_start_only,
1642 &s->control_pid)) < 0)
1648 log_warning("%s failed to run spawn next executable: %s", UNIT(s)->meta.id, strerror(-r));
1650 if (s->state == SERVICE_START_PRE)
1651 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1652 else if (s->state == SERVICE_STOP)
1653 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1654 else if (s->state == SERVICE_STOP_POST)
1655 service_enter_dead(s, false, true);
1657 service_enter_stop(s, false);
1660 static int service_start(Unit *u) {
1661 Service *s = SERVICE(u);
1665 /* We cannot fulfill this request right now, try again later
1667 if (s->state == SERVICE_STOP ||
1668 s->state == SERVICE_STOP_SIGTERM ||
1669 s->state == SERVICE_STOP_SIGKILL ||
1670 s->state == SERVICE_STOP_POST ||
1671 s->state == SERVICE_FINAL_SIGTERM ||
1672 s->state == SERVICE_FINAL_SIGKILL)
1675 /* Already on it! */
1676 if (s->state == SERVICE_START_PRE ||
1677 s->state == SERVICE_START ||
1678 s->state == SERVICE_START_POST)
1681 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1683 /* Make sure we don't enter a busy loop of some kind. */
1684 if (!ratelimit_test(&s->ratelimit)) {
1685 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1690 s->main_pid_known = false;
1691 s->allow_restart = true;
1693 service_enter_start_pre(s);
1697 static int service_stop(Unit *u) {
1698 Service *s = SERVICE(u);
1702 /* Cannot do this now */
1703 if (s->state == SERVICE_START_PRE ||
1704 s->state == SERVICE_START ||
1705 s->state == SERVICE_START_POST ||
1706 s->state == SERVICE_RELOAD)
1710 if (s->state == SERVICE_STOP ||
1711 s->state == SERVICE_STOP_SIGTERM ||
1712 s->state == SERVICE_STOP_SIGKILL ||
1713 s->state == SERVICE_STOP_POST ||
1714 s->state == SERVICE_FINAL_SIGTERM ||
1715 s->state == SERVICE_FINAL_SIGKILL)
1718 if (s->state == SERVICE_AUTO_RESTART) {
1719 service_set_state(s, SERVICE_DEAD);
1723 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1725 /* This is a user request, so don't do restarts on this
1727 s->allow_restart = false;
1729 service_enter_stop(s, true);
1733 static int service_reload(Unit *u) {
1734 Service *s = SERVICE(u);
1738 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1740 service_enter_reload(s);
1744 static bool service_can_reload(Unit *u) {
1745 Service *s = SERVICE(u);
1749 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1752 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1753 Service *s = SERVICE(u);
1759 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1760 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1762 if (s->control_pid > 0)
1763 unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) (s->control_pid));
1765 if (s->main_pid > 0)
1766 unit_serialize_item_format(u, f, "main-pid", "%u", (unsigned) (s->main_pid));
1768 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1770 /* There's a minor uncleanliness here: if there are multiple
1771 * commands attached here, we will start from the first one
1773 if (s->control_command_id >= 0)
1774 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
1776 if (s->socket_fd >= 0) {
1779 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1782 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1788 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1789 Service *s = SERVICE(u);
1797 if (streq(key, "state")) {
1800 if ((state = service_state_from_string(value)) < 0)
1801 log_debug("Failed to parse state value %s", value);
1803 s->deserialized_state = state;
1804 } else if (streq(key, "failure")) {
1807 if ((b = parse_boolean(value)) < 0)
1808 log_debug("Failed to parse failure value %s", value);
1810 s->failure = b || s->failure;
1811 } else if (streq(key, "control-pid")) {
1814 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1815 log_debug("Failed to parse control-pid value %s", value);
1817 s->control_pid = (pid_t) pid;
1818 } else if (streq(key, "main-pid")) {
1821 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1822 log_debug("Failed to parse main-pid value %s", value);
1824 s->main_pid = (pid_t) pid;
1825 } else if (streq(key, "main-pid-known")) {
1828 if ((b = parse_boolean(value)) < 0)
1829 log_debug("Failed to parse main-pid-known value %s", value);
1831 s->main_pid_known = b;
1832 } else if (streq(key, "control-command")) {
1833 ServiceExecCommand id;
1835 if ((id = service_exec_command_from_string(value)) < 0)
1836 log_debug("Failed to parse exec-command value %s", value);
1838 s->control_command_id = id;
1839 s->control_command = s->exec_command[id];
1841 } else if (streq(key, "socket-fd")) {
1844 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1845 log_debug("Failed to parse socket-fd value %s", value);
1848 if (s->socket_fd >= 0)
1849 close_nointr_nofail(s->socket_fd);
1850 s->socket_fd = fdset_remove(fds, fd);
1853 log_debug("Unknown serialization key '%s'", key);
1858 static UnitActiveState service_active_state(Unit *u) {
1861 return state_translation_table[SERVICE(u)->state];
1864 static const char *service_sub_state_to_string(Unit *u) {
1867 return service_state_to_string(SERVICE(u)->state);
1870 static bool service_check_gc(Unit *u) {
1871 Service *s = SERVICE(u);
1875 return !!s->sysv_path;
1878 static bool service_check_snapshot(Unit *u) {
1879 Service *s = SERVICE(u);
1883 return !s->got_socket_fd;
1886 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1887 Service *s = SERVICE(u);
1893 success = code == CLD_EXITED && status == 0;
1894 s->failure = s->failure || !success;
1896 if (s->main_pid == pid) {
1898 exec_status_fill(&s->main_exec_status, pid, code, status);
1901 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1902 assert(s->exec_command[SERVICE_EXEC_START]);
1903 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1906 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
1908 /* The service exited, so the service is officially
1913 case SERVICE_START_POST:
1914 case SERVICE_RELOAD:
1916 /* Need to wait until the operation is
1921 assert(s->type == SERVICE_FINISH);
1923 /* This was our main goal, so let's go on */
1925 service_enter_start_post(s);
1927 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1930 case SERVICE_RUNNING:
1931 service_enter_running(s, success);
1934 case SERVICE_STOP_SIGTERM:
1935 case SERVICE_STOP_SIGKILL:
1937 if (!control_pid_good(s))
1938 service_enter_stop_post(s, success);
1940 /* If there is still a control process, wait for that first */
1944 assert_not_reached("Uh, main process died at wrong time.");
1947 } else if (s->control_pid == pid) {
1949 if (s->control_command)
1950 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1954 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
1956 /* If we are shutting things down anyway we
1957 * don't care about failing commands. */
1959 if (s->control_command && s->control_command->command_next && success) {
1961 /* There is another command to *
1962 * execute, so let's do that. */
1964 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
1965 service_run_next(s, success);
1968 /* No further commands for this step, so let's
1969 * figure out what to do next */
1971 s->control_command = NULL;
1972 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1974 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
1978 case SERVICE_START_PRE:
1980 service_enter_start(s);
1982 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1986 assert(s->type == SERVICE_FORKING);
1988 /* Let's try to load the pid
1989 * file here if we can. We
1990 * ignore the return value,
1991 * since the PID file might
1992 * actually be created by a
1993 * START_POST script */
1997 service_load_pid_file(s);
1999 service_enter_start_post(s);
2001 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2005 case SERVICE_START_POST:
2006 if (success && s->pid_file && !s->main_pid_known) {
2009 /* Hmm, let's see if we can
2010 * load the pid now after the
2011 * start-post scripts got
2014 if ((r = service_load_pid_file(s)) < 0)
2015 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
2020 case SERVICE_RELOAD:
2022 service_enter_running(s, true);
2024 service_enter_stop(s, false);
2029 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2032 case SERVICE_STOP_SIGTERM:
2033 case SERVICE_STOP_SIGKILL:
2034 if (main_pid_good(s) <= 0)
2035 service_enter_stop_post(s, success);
2037 /* If there is still a service
2038 * process around, wait until
2039 * that one quit, too */
2042 case SERVICE_STOP_POST:
2043 case SERVICE_FINAL_SIGTERM:
2044 case SERVICE_FINAL_SIGKILL:
2045 service_enter_dead(s, success, true);
2049 assert_not_reached("Uh, control process died at wrong time.");
2053 assert_not_reached("Got SIGCHLD for unkown PID");
2056 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2057 Service *s = SERVICE(u);
2060 assert(elapsed == 1);
2062 assert(w == &s->timer_watch);
2066 case SERVICE_START_PRE:
2068 log_warning("%s operation timed out. Terminating.", u->meta.id);
2069 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2072 case SERVICE_START_POST:
2073 case SERVICE_RELOAD:
2074 log_warning("%s operation timed out. Stopping.", u->meta.id);
2075 service_enter_stop(s, false);
2079 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2080 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2083 case SERVICE_STOP_SIGTERM:
2084 log_warning("%s stopping timed out. Killing.", u->meta.id);
2085 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2088 case SERVICE_STOP_SIGKILL:
2089 /* Uh, wie sent a SIGKILL and it is still not gone?
2090 * Must be something we cannot kill, so let's just be
2091 * weirded out and continue */
2093 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2094 service_enter_stop_post(s, false);
2097 case SERVICE_STOP_POST:
2098 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2099 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2102 case SERVICE_FINAL_SIGTERM:
2103 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2104 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2107 case SERVICE_FINAL_SIGKILL:
2108 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u->meta.id);
2109 service_enter_dead(s, false, true);
2112 case SERVICE_AUTO_RESTART:
2113 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
2114 service_enter_restart(s);
2118 assert_not_reached("Timeout at wrong time.");
2122 static void service_cgroup_notify_event(Unit *u) {
2123 Service *s = SERVICE(u);
2127 log_debug("%s: cgroup is empty", u->meta.id);
2131 /* Waiting for SIGCHLD is usually more interesting,
2132 * because it includes return codes/signals. Which is
2133 * why we ignore the cgroup events for most cases,
2134 * except when we don't know pid which to expect the
2137 case SERVICE_RUNNING:
2138 service_enter_running(s, true);
2146 static int service_enumerate(Manager *m) {
2150 char *path = NULL, *fpath = NULL, *name = NULL;
2155 STRV_FOREACH(p, m->sysvrcnd_path)
2156 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2161 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2169 if (!(d = opendir(path))) {
2170 if (errno != ENOENT)
2171 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2176 while ((de = readdir(d))) {
2180 if (ignore_file(de->d_name))
2183 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2186 if (strlen(de->d_name) < 4)
2189 a = undecchar(de->d_name[1]);
2190 b = undecchar(de->d_name[2]);
2197 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2202 if (access(fpath, X_OK) < 0) {
2204 if (errno != ENOENT)
2205 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2211 if (!(name = new(char, strlen(de->d_name) - 3 + 8 + 1))) {
2216 if (startswith(de->d_name+3, "boot."))
2217 /* Drop SuSE-style boot. prefix */
2218 strcpy(stpcpy(name, de->d_name + 3 + 5), ".service");
2219 else if (endswith(de->d_name+3, ".sh"))
2220 /* Drop Debian-style .sh suffix */
2221 strcpy(stpcpy(name, de->d_name + 3) - 3, ".service");
2223 /* Normal init scripts */
2224 strcpy(stpcpy(name, de->d_name + 3), ".service");
2226 if ((r = manager_load_unit_prepare(m, name, NULL, &service)) < 0) {
2227 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2231 if (de->d_name[0] == 'S' &&
2232 (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_BASIC))
2233 SERVICE(service)->sysv_start_priority =
2234 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2236 manager_dispatch_load_queue(m);
2237 service = unit_follow_merge(service);
2239 if (de->d_name[0] == 'S') {
2240 Unit *runlevel_target;
2242 if ((r = manager_load_unit(m, rcnd_table[i].target, NULL, &runlevel_target)) < 0)
2245 if ((r = unit_add_dependency(runlevel_target, UNIT_WANTS, service, true)) < 0)
2248 if ((r = unit_add_dependency(service, UNIT_BEFORE, runlevel_target, true)) < 0)
2251 } else if (de->d_name[0] == 'K' && rcnd_table[i].type == RUNLEVEL_DOWN) {
2252 Unit *shutdown_target;
2254 /* We honour K links only for
2255 * halt/reboot. For the normal
2256 * runlevels we assume the
2258 * implicitly added by the
2259 * core logic. Also, we don't
2260 * really distuingish here
2261 * between the runlevels 0 and
2262 * 6 and just add them to the
2263 * special shutdown target. */
2265 if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
2268 if ((r = unit_add_dependency(service, UNIT_CONFLICTS, shutdown_target, true)) < 0)
2271 if ((r = unit_add_dependency(service, UNIT_BEFORE, shutdown_target, true)) < 0)
2290 static void service_bus_name_owner_change(
2293 const char *old_owner,
2294 const char *new_owner) {
2296 Service *s = SERVICE(u);
2301 assert(streq(s->bus_name, name));
2302 assert(old_owner || new_owner);
2304 if (old_owner && new_owner)
2305 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2307 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2309 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2311 s->bus_name_good = !!new_owner;
2313 if (s->type == SERVICE_DBUS) {
2315 /* service_enter_running() will figure out what to
2317 if (s->state == SERVICE_RUNNING)
2318 service_enter_running(s, true);
2319 else if (s->state == SERVICE_START && new_owner)
2320 service_enter_start_post(s);
2322 } else if (new_owner &&
2324 (s->state == SERVICE_START ||
2325 s->state == SERVICE_START_POST ||
2326 s->state == SERVICE_RUNNING ||
2327 s->state == SERVICE_RELOAD)) {
2329 /* Try to acquire PID from bus service */
2330 log_debug("Trying to acquire PID from D-Bus name...");
2332 bus_query_pid(u->meta.manager, name);
2336 static void service_bus_query_pid_done(
2341 Service *s = SERVICE(u);
2346 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2348 if (s->main_pid <= 0 &&
2349 (s->state == SERVICE_START ||
2350 s->state == SERVICE_START_POST ||
2351 s->state == SERVICE_RUNNING ||
2352 s->state == SERVICE_RELOAD))
2356 int service_set_socket_fd(Service *s, int fd) {
2360 /* This is called by the socket code when instantiating a new
2361 * service for a stream socket and the socket needs to be
2364 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2367 if (s->socket_fd >= 0)
2370 if (s->state != SERVICE_DEAD)
2374 s->got_socket_fd = true;
2378 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2379 [SERVICE_DEAD] = "dead",
2380 [SERVICE_START_PRE] = "start-pre",
2381 [SERVICE_START] = "start",
2382 [SERVICE_START_POST] = "start-post",
2383 [SERVICE_RUNNING] = "running",
2384 [SERVICE_EXITED] = "exited",
2385 [SERVICE_RELOAD] = "reload",
2386 [SERVICE_STOP] = "stop",
2387 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2388 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2389 [SERVICE_STOP_POST] = "stop-post",
2390 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2391 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2392 [SERVICE_MAINTAINANCE] = "maintainance",
2393 [SERVICE_AUTO_RESTART] = "auto-restart",
2396 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2398 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2399 [SERVICE_ONCE] = "once",
2400 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2401 [SERVICE_RESTART_ALWAYS] = "restart-always",
2404 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2406 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2407 [SERVICE_FORKING] = "forking",
2408 [SERVICE_SIMPLE] = "simple",
2409 [SERVICE_FINISH] = "finish",
2410 [SERVICE_DBUS] = "dbus"
2413 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2415 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2416 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2417 [SERVICE_EXEC_START] = "ExecStart",
2418 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2419 [SERVICE_EXEC_RELOAD] = "ExecReload",
2420 [SERVICE_EXEC_STOP] = "ExecStop",
2421 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2424 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2426 const UnitVTable service_vtable = {
2427 .suffix = ".service",
2429 .init = service_init,
2430 .done = service_done,
2431 .load = service_load,
2433 .coldplug = service_coldplug,
2435 .dump = service_dump,
2437 .start = service_start,
2438 .stop = service_stop,
2439 .reload = service_reload,
2441 .can_reload = service_can_reload,
2443 .serialize = service_serialize,
2444 .deserialize_item = service_deserialize_item,
2446 .active_state = service_active_state,
2447 .sub_state_to_string = service_sub_state_to_string,
2449 .check_gc = service_check_gc,
2450 .check_snapshot = service_check_snapshot,
2452 .sigchld_event = service_sigchld_event,
2453 .timer_event = service_timer_event,
2455 .cgroup_notify_empty = service_cgroup_notify_event,
2457 .bus_name_owner_change = service_bus_name_owner_change,
2458 .bus_query_pid_done = service_bus_query_pid_done,
2460 .bus_message_handler = bus_service_message_handler,
2462 .enumerate = service_enumerate