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 init scripts have no LSB header, then we enforce the
595 * ordering via the chkconfig priorities. We try to determine
596 * a priority for *all* init scripts here, since they are
597 * needed as soon as at least one non-LSB script is used. */
599 if (s->sysv_start_priority < 0)
600 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", u->meta.id);
602 if ((r = sysv_exec_commands(s)) < 0)
605 if (!s->sysv_runlevels || chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
606 /* If there a runlevels configured for this service
607 * but none of the standard ones, then we assume this
608 * is some special kind of service (which might be
609 * needed for early boot) and don't create any links
612 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0 ||
613 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
617 /* Don't timeout special services during boot (like fsck) */
620 /* Special setting for all SysV services */
621 s->valid_no_process = true;
622 s->kill_mode = KILL_PROCESS_GROUP;
624 u->meta.load_state = UNIT_LOADED;
635 static int service_load_sysv_name(Service *s, const char *name) {
641 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
645 if (asprintf(&path, "%s/%s", *p, name) < 0)
648 assert(endswith(path, ".service"));
649 path[strlen(path)-8] = 0;
651 r = service_load_sysv_path(s, path);
653 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
654 /* Try Debian style .sh source'able init scripts */
656 r = service_load_sysv_path(s, path);
661 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
662 /* Try Suse style boot.xxxx init scripts */
664 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
667 path[strlen(path)-8] = 0;
668 r = service_load_sysv_path(s, path);
675 if ((UNIT(s)->meta.load_state != UNIT_STUB))
682 static int service_load_sysv(Service *s) {
689 /* Load service data from SysV init scripts, preferably with
692 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
695 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)
700 SET_FOREACH(t, UNIT(s)->meta.names, i) {
701 if (t == UNIT(s)->meta.id)
704 if ((r == service_load_sysv_name(s, t)) < 0)
707 if (UNIT(s)->meta.load_state != UNIT_STUB)
714 static int service_add_bus_name(Service *s) {
721 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
724 r = unit_merge_by_name(UNIT(s), n);
730 static int service_verify(Service *s) {
733 if (UNIT(s)->meta.load_state != UNIT_LOADED)
736 if (!s->exec_command[SERVICE_EXEC_START]) {
737 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
741 if (s->type == SERVICE_DBUS && !s->bus_name) {
742 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->meta.id);
749 static int service_load(Unit *u) {
751 Service *s = SERVICE(u);
755 /* Load a .service file */
756 if ((r = unit_load_fragment(u)) < 0)
759 /* Load a classic init script as a fallback, if we couldn't find anything */
760 if (u->meta.load_state == UNIT_STUB)
761 if ((r = service_load_sysv(s)) < 0)
764 /* Still nothing found? Then let's give up */
765 if (u->meta.load_state == UNIT_STUB)
768 /* We were able to load something, then let's add in the
769 * dropin directories. */
770 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
773 /* This is a new unit? Then let's add in some extras */
774 if (u->meta.load_state == UNIT_LOADED) {
775 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
778 if ((r = unit_add_default_cgroup(u)) < 0)
781 if ((r = sysv_chkconfig_order(s)) < 0)
785 if ((r = service_add_bus_name(s)) < 0)
788 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
793 return service_verify(s);
796 static void service_dump(Unit *u, FILE *f, const char *prefix) {
798 ServiceExecCommand c;
799 Service *s = SERVICE(u);
805 p2 = strappend(prefix, "\t");
806 prefix2 = p2 ? p2 : prefix;
809 "%sService State: %s\n"
810 "%sPermissionsStartOnly: %s\n"
811 "%sRootDirectoryStartOnly: %s\n"
812 "%sValidNoProcess: %s\n"
815 prefix, service_state_to_string(s->state),
816 prefix, yes_no(s->permissions_start_only),
817 prefix, yes_no(s->root_directory_start_only),
818 prefix, yes_no(s->valid_no_process),
819 prefix, kill_mode_to_string(s->kill_mode),
820 prefix, service_type_to_string(s->type));
822 if (s->control_pid > 0)
824 "%sControl PID: %llu\n",
825 prefix, (unsigned long long) s->control_pid);
829 "%sMain PID: %llu\n",
830 prefix, (unsigned long long) s->main_pid);
835 prefix, s->pid_file);
840 "%sBus Name Good: %s\n",
842 prefix, yes_no(s->bus_name_good));
844 exec_context_dump(&s->exec_context, f, prefix);
846 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
848 if (!s->exec_command[c])
851 fprintf(f, "%s-> %s:\n",
852 prefix, service_exec_command_to_string(c));
854 exec_command_dump_list(s->exec_command[c], f, prefix2);
859 "%sSysV Init Script Path: %s\n"
860 "%sSysV Init Script has LSB Header: %s\n",
861 prefix, s->sysv_path,
862 prefix, yes_no(s->sysv_has_lsb));
864 if (s->sysv_start_priority >= 0)
866 "%sSysVStartPriority: %i\n",
867 prefix, s->sysv_start_priority);
869 if (s->sysv_runlevels)
870 fprintf(f, "%sSysVRunLevels: %s\n",
871 prefix, s->sysv_runlevels);
876 static int service_load_pid_file(Service *s) {
883 if (s->main_pid_known)
886 assert(s->main_pid <= 0);
891 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
894 if ((r = safe_atolu(k, &p)) < 0) {
899 if ((unsigned long) (pid_t) p != p)
902 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
903 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
904 (unsigned long long) p, s->pid_file);
908 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
909 /* FIXME: we need to do something here */
912 s->main_pid = (pid_t) p;
913 s->main_pid_known = true;
918 static int service_get_sockets(Service *s, Set **_set) {
927 /* Collects all Socket objects that belong to this
928 * service. Note that a service might have multiple sockets
929 * via multiple names. */
931 if (!(set = set_new(NULL, NULL)))
934 SET_FOREACH(t, UNIT(s)->meta.names, i) {
938 /* Look for all socket objects that go by any of our
939 * units and collect their fds */
941 if (!(k = unit_name_change_suffix(t, ".socket"))) {
946 p = manager_get_unit(UNIT(s)->meta.manager, k);
952 if ((r = set_put(set, p)) < 0)
964 static int service_notify_sockets_dead(Service *s) {
972 /* Notifies all our sockets when we die */
973 if ((r = service_get_sockets(s, &set)) < 0)
976 SET_FOREACH(sock, set, i)
977 socket_notify_service_dead(sock);
984 static void service_set_state(Service *s, ServiceState state) {
985 ServiceState old_state;
988 old_state = s->state;
991 if (state != SERVICE_START_PRE &&
992 state != SERVICE_START &&
993 state != SERVICE_START_POST &&
994 state != SERVICE_RELOAD &&
995 state != SERVICE_STOP &&
996 state != SERVICE_STOP_SIGTERM &&
997 state != SERVICE_STOP_SIGKILL &&
998 state != SERVICE_STOP_POST &&
999 state != SERVICE_FINAL_SIGTERM &&
1000 state != SERVICE_FINAL_SIGKILL &&
1001 state != SERVICE_AUTO_RESTART)
1002 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1004 if (state != SERVICE_START &&
1005 state != SERVICE_START_POST &&
1006 state != SERVICE_RUNNING &&
1007 state != SERVICE_RELOAD &&
1008 state != SERVICE_STOP &&
1009 state != SERVICE_STOP_SIGTERM &&
1010 state != SERVICE_STOP_SIGKILL)
1011 service_unwatch_main_pid(s);
1013 if (state != SERVICE_START_PRE &&
1014 state != SERVICE_START &&
1015 state != SERVICE_START_POST &&
1016 state != SERVICE_RELOAD &&
1017 state != SERVICE_STOP &&
1018 state != SERVICE_STOP_SIGTERM &&
1019 state != SERVICE_STOP_SIGKILL &&
1020 state != SERVICE_STOP_POST &&
1021 state != SERVICE_FINAL_SIGTERM &&
1022 state != SERVICE_FINAL_SIGKILL) {
1023 service_unwatch_control_pid(s);
1024 s->control_command = NULL;
1025 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1028 if (state == SERVICE_DEAD ||
1029 state == SERVICE_STOP ||
1030 state == SERVICE_STOP_SIGTERM ||
1031 state == SERVICE_STOP_SIGKILL ||
1032 state == SERVICE_STOP_POST ||
1033 state == SERVICE_FINAL_SIGTERM ||
1034 state == SERVICE_FINAL_SIGKILL ||
1035 state == SERVICE_MAINTAINANCE ||
1036 state == SERVICE_AUTO_RESTART)
1037 service_notify_sockets_dead(s);
1039 if (state != SERVICE_START_PRE &&
1040 state != SERVICE_START &&
1041 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1042 service_close_socket_fd(s);
1044 if (old_state != state)
1045 log_debug("%s changed %s -> %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1047 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1050 static int service_coldplug(Unit *u) {
1051 Service *s = SERVICE(u);
1055 assert(s->state == SERVICE_DEAD);
1057 if (s->deserialized_state != s->state) {
1059 if (s->deserialized_state == SERVICE_START_PRE ||
1060 s->deserialized_state == SERVICE_START ||
1061 s->deserialized_state == SERVICE_START_POST ||
1062 s->deserialized_state == SERVICE_RELOAD ||
1063 s->deserialized_state == SERVICE_STOP ||
1064 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1065 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1066 s->deserialized_state == SERVICE_STOP_POST ||
1067 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1068 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1069 s->deserialized_state == SERVICE_AUTO_RESTART) {
1071 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1074 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1076 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1081 if ((s->deserialized_state == SERVICE_START &&
1082 (s->type == SERVICE_FORKING ||
1083 s->type == SERVICE_DBUS)) ||
1084 s->deserialized_state == SERVICE_START_POST ||
1085 s->deserialized_state == SERVICE_RUNNING ||
1086 s->deserialized_state == SERVICE_RELOAD ||
1087 s->deserialized_state == SERVICE_STOP ||
1088 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1089 s->deserialized_state == SERVICE_STOP_SIGKILL)
1090 if (s->main_pid > 0)
1091 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1094 if (s->deserialized_state == SERVICE_START_PRE ||
1095 s->deserialized_state == SERVICE_START ||
1096 s->deserialized_state == SERVICE_START_POST ||
1097 s->deserialized_state == SERVICE_RELOAD ||
1098 s->deserialized_state == SERVICE_STOP ||
1099 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1100 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1101 s->deserialized_state == SERVICE_STOP_POST ||
1102 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1103 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1104 if (s->control_pid > 0)
1105 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1108 service_set_state(s, s->deserialized_state);
1114 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1118 unsigned rn_fds = 0;
1126 if ((r = service_get_sockets(s, &set)) < 0)
1129 SET_FOREACH(sock, set, i) {
1133 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1145 if (!(t = new(int, rn_fds+cn_fds))) {
1151 memcpy(t, rfds, rn_fds);
1152 memcpy(t+rn_fds, cfds, cn_fds);
1157 rn_fds = rn_fds+cn_fds;
1175 static int service_spawn(
1180 bool apply_permissions,
1195 if (s->socket_fd >= 0) {
1196 fds = &s->socket_fd;
1198 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1202 if (timeout && s->timeout_usec) {
1203 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1206 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1208 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1217 s->meta.manager->environment,
1220 UNIT(s)->meta.manager->confirm_spawn,
1221 UNIT(s)->meta.cgroup_bondings,
1229 if (s->socket_fd >= 0)
1230 service_close_socket_fd(s);
1235 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1236 /* FIXME: we need to do something here */
1247 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1252 static int main_pid_good(Service *s) {
1255 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1258 /* If we know the pid file, then lets just check if it is
1260 if (s->main_pid_known)
1261 return s->main_pid > 0;
1263 /* We don't know the pid */
1267 static int control_pid_good(Service *s) {
1270 return s->control_pid > 0;
1273 static int cgroup_good(Service *s) {
1278 if (s->valid_no_process)
1281 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1287 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1294 if (allow_restart &&
1296 (s->restart == SERVICE_RESTART_ALWAYS ||
1297 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1299 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1302 service_set_state(s, SERVICE_AUTO_RESTART);
1304 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1309 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
1310 service_enter_dead(s, false, false);
1313 static void service_enter_signal(Service *s, ServiceState state, bool success);
1315 static void service_enter_stop_post(Service *s, bool success) {
1322 service_unwatch_control_pid(s);
1324 s->control_command_id = SERVICE_EXEC_STOP_POST;
1325 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1326 if ((r = service_spawn(s,
1330 !s->permissions_start_only,
1331 !s->root_directory_start_only,
1332 &s->control_pid)) < 0)
1336 service_set_state(s, SERVICE_STOP_POST);
1338 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1343 log_warning("%s failed to run stop-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1344 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1347 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1356 if (s->kill_mode != KILL_NONE) {
1357 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1359 if (s->kill_mode == KILL_CONTROL_GROUP) {
1361 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1362 if (r != -EAGAIN && r != -ESRCH)
1371 if (s->main_pid > 0) {
1372 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1378 if (s->control_pid > 0) {
1379 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1390 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1391 if (s->timeout_usec > 0)
1392 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1395 service_set_state(s, state);
1396 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1397 service_enter_stop_post(s, true);
1399 service_enter_dead(s, true, true);
1404 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
1406 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1407 service_enter_stop_post(s, false);
1409 service_enter_dead(s, false, true);
1412 static void service_enter_stop(Service *s, bool success) {
1419 service_unwatch_control_pid(s);
1421 s->control_command_id = SERVICE_EXEC_STOP;
1422 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1423 if ((r = service_spawn(s,
1427 !s->permissions_start_only,
1428 !s->root_directory_start_only,
1429 &s->control_pid)) < 0)
1432 service_set_state(s, SERVICE_STOP);
1434 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1439 log_warning("%s failed to run stop executable: %s", UNIT(s)->meta.id, strerror(-r));
1440 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1443 static void service_enter_running(Service *s, bool success) {
1449 if (main_pid_good(s) != 0 &&
1450 cgroup_good(s) != 0 &&
1451 (s->bus_name_good || s->type != SERVICE_DBUS))
1452 service_set_state(s, SERVICE_RUNNING);
1453 else if (s->valid_no_process)
1454 service_set_state(s, SERVICE_EXITED);
1456 service_enter_stop(s, true);
1459 static void service_enter_start_post(Service *s) {
1463 service_unwatch_control_pid(s);
1465 s->control_command_id = SERVICE_EXEC_START_POST;
1466 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1467 if ((r = service_spawn(s,
1471 !s->permissions_start_only,
1472 !s->root_directory_start_only,
1473 &s->control_pid)) < 0)
1477 service_set_state(s, SERVICE_START_POST);
1479 service_enter_running(s, true);
1484 log_warning("%s failed to run start-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1485 service_enter_stop(s, false);
1488 static void service_enter_start(Service *s) {
1494 assert(s->exec_command[SERVICE_EXEC_START]);
1495 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1497 if (s->type == SERVICE_FORKING)
1498 service_unwatch_control_pid(s);
1500 service_unwatch_main_pid(s);
1502 if ((r = service_spawn(s,
1503 s->exec_command[SERVICE_EXEC_START],
1504 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS,
1511 if (s->type == SERVICE_SIMPLE) {
1512 /* For simple services we immediately start
1513 * the START_POST binaries. */
1516 s->main_pid_known = true;
1518 service_enter_start_post(s);
1520 } else if (s->type == SERVICE_FORKING) {
1522 /* For forking services we wait until the start
1523 * process exited. */
1525 s->control_pid = pid;
1527 s->control_command_id = SERVICE_EXEC_START;
1528 s->control_command = s->exec_command[SERVICE_EXEC_START];
1529 service_set_state(s, SERVICE_START);
1531 } else if (s->type == SERVICE_FINISH ||
1532 s->type == SERVICE_DBUS) {
1534 /* For finishing services we wait until the start
1535 * process exited, too, but it is our main process. */
1537 /* For D-Bus services we know the main pid right away,
1538 * but wait for the bus name to appear on the bus. */
1541 s->main_pid_known = true;
1543 service_set_state(s, SERVICE_START);
1545 assert_not_reached("Unknown service type");
1550 log_warning("%s failed to run start exectuable: %s", UNIT(s)->meta.id, strerror(-r));
1551 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1554 static void service_enter_start_pre(Service *s) {
1559 service_unwatch_control_pid(s);
1561 s->control_command_id = SERVICE_EXEC_START_PRE;
1562 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1563 if ((r = service_spawn(s,
1567 !s->permissions_start_only,
1568 !s->root_directory_start_only,
1569 &s->control_pid)) < 0)
1572 service_set_state(s, SERVICE_START_PRE);
1574 service_enter_start(s);
1579 log_warning("%s failed to run start-pre executable: %s", UNIT(s)->meta.id, strerror(-r));
1580 service_enter_dead(s, false, true);
1583 static void service_enter_restart(Service *s) {
1587 service_enter_dead(s, true, false);
1589 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1592 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
1597 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
1598 service_enter_dead(s, false, false);
1601 static void service_enter_reload(Service *s) {
1606 service_unwatch_control_pid(s);
1608 s->control_command_id = SERVICE_EXEC_RELOAD;
1609 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1610 if ((r = service_spawn(s,
1614 !s->permissions_start_only,
1615 !s->root_directory_start_only,
1616 &s->control_pid)) < 0)
1619 service_set_state(s, SERVICE_RELOAD);
1621 service_enter_running(s, true);
1626 log_warning("%s failed to run reload executable: %s", UNIT(s)->meta.id, strerror(-r));
1627 service_enter_stop(s, false);
1630 static void service_run_next(Service *s, bool success) {
1634 assert(s->control_command);
1635 assert(s->control_command->command_next);
1640 s->control_command = s->control_command->command_next;
1642 service_unwatch_control_pid(s);
1644 if ((r = service_spawn(s,
1648 !s->permissions_start_only,
1649 !s->root_directory_start_only,
1650 &s->control_pid)) < 0)
1656 log_warning("%s failed to run spawn next executable: %s", UNIT(s)->meta.id, strerror(-r));
1658 if (s->state == SERVICE_START_PRE)
1659 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1660 else if (s->state == SERVICE_STOP)
1661 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1662 else if (s->state == SERVICE_STOP_POST)
1663 service_enter_dead(s, false, true);
1665 service_enter_stop(s, false);
1668 static int service_start(Unit *u) {
1669 Service *s = SERVICE(u);
1673 /* We cannot fulfill this request right now, try again later
1675 if (s->state == SERVICE_STOP ||
1676 s->state == SERVICE_STOP_SIGTERM ||
1677 s->state == SERVICE_STOP_SIGKILL ||
1678 s->state == SERVICE_STOP_POST ||
1679 s->state == SERVICE_FINAL_SIGTERM ||
1680 s->state == SERVICE_FINAL_SIGKILL)
1683 /* Already on it! */
1684 if (s->state == SERVICE_START_PRE ||
1685 s->state == SERVICE_START ||
1686 s->state == SERVICE_START_POST)
1689 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1691 /* Make sure we don't enter a busy loop of some kind. */
1692 if (!ratelimit_test(&s->ratelimit)) {
1693 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1698 s->main_pid_known = false;
1699 s->allow_restart = true;
1701 service_enter_start_pre(s);
1705 static int service_stop(Unit *u) {
1706 Service *s = SERVICE(u);
1710 /* Cannot do this now */
1711 if (s->state == SERVICE_START_PRE ||
1712 s->state == SERVICE_START ||
1713 s->state == SERVICE_START_POST ||
1714 s->state == SERVICE_RELOAD)
1718 if (s->state == SERVICE_STOP ||
1719 s->state == SERVICE_STOP_SIGTERM ||
1720 s->state == SERVICE_STOP_SIGKILL ||
1721 s->state == SERVICE_STOP_POST ||
1722 s->state == SERVICE_FINAL_SIGTERM ||
1723 s->state == SERVICE_FINAL_SIGKILL)
1726 if (s->state == SERVICE_AUTO_RESTART) {
1727 service_set_state(s, SERVICE_DEAD);
1731 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1733 /* This is a user request, so don't do restarts on this
1735 s->allow_restart = false;
1737 service_enter_stop(s, true);
1741 static int service_reload(Unit *u) {
1742 Service *s = SERVICE(u);
1746 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1748 service_enter_reload(s);
1752 static bool service_can_reload(Unit *u) {
1753 Service *s = SERVICE(u);
1757 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1760 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1761 Service *s = SERVICE(u);
1767 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1768 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1770 if (s->control_pid > 0)
1771 unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) (s->control_pid));
1773 if (s->main_pid > 0)
1774 unit_serialize_item_format(u, f, "main-pid", "%u", (unsigned) (s->main_pid));
1776 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1778 /* There's a minor uncleanliness here: if there are multiple
1779 * commands attached here, we will start from the first one
1781 if (s->control_command_id >= 0)
1782 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
1784 if (s->socket_fd >= 0) {
1787 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1790 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1796 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1797 Service *s = SERVICE(u);
1805 if (streq(key, "state")) {
1808 if ((state = service_state_from_string(value)) < 0)
1809 log_debug("Failed to parse state value %s", value);
1811 s->deserialized_state = state;
1812 } else if (streq(key, "failure")) {
1815 if ((b = parse_boolean(value)) < 0)
1816 log_debug("Failed to parse failure value %s", value);
1818 s->failure = b || s->failure;
1819 } else if (streq(key, "control-pid")) {
1822 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1823 log_debug("Failed to parse control-pid value %s", value);
1825 s->control_pid = (pid_t) pid;
1826 } else if (streq(key, "main-pid")) {
1829 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1830 log_debug("Failed to parse main-pid value %s", value);
1832 s->main_pid = (pid_t) pid;
1833 } else if (streq(key, "main-pid-known")) {
1836 if ((b = parse_boolean(value)) < 0)
1837 log_debug("Failed to parse main-pid-known value %s", value);
1839 s->main_pid_known = b;
1840 } else if (streq(key, "control-command")) {
1841 ServiceExecCommand id;
1843 if ((id = service_exec_command_from_string(value)) < 0)
1844 log_debug("Failed to parse exec-command value %s", value);
1846 s->control_command_id = id;
1847 s->control_command = s->exec_command[id];
1849 } else if (streq(key, "socket-fd")) {
1852 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1853 log_debug("Failed to parse socket-fd value %s", value);
1856 if (s->socket_fd >= 0)
1857 close_nointr_nofail(s->socket_fd);
1858 s->socket_fd = fdset_remove(fds, fd);
1861 log_debug("Unknown serialization key '%s'", key);
1866 static UnitActiveState service_active_state(Unit *u) {
1869 return state_translation_table[SERVICE(u)->state];
1872 static const char *service_sub_state_to_string(Unit *u) {
1875 return service_state_to_string(SERVICE(u)->state);
1878 static bool service_check_gc(Unit *u) {
1879 Service *s = SERVICE(u);
1883 return !!s->sysv_path;
1886 static bool service_check_snapshot(Unit *u) {
1887 Service *s = SERVICE(u);
1891 return !s->got_socket_fd;
1894 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1895 Service *s = SERVICE(u);
1901 success = code == CLD_EXITED && status == 0;
1902 s->failure = s->failure || !success;
1904 if (s->main_pid == pid) {
1906 exec_status_fill(&s->main_exec_status, pid, code, status);
1909 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1910 assert(s->exec_command[SERVICE_EXEC_START]);
1911 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1914 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
1916 /* The service exited, so the service is officially
1921 case SERVICE_START_POST:
1922 case SERVICE_RELOAD:
1924 /* Need to wait until the operation is
1929 assert(s->type == SERVICE_FINISH);
1931 /* This was our main goal, so let's go on */
1933 service_enter_start_post(s);
1935 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1938 case SERVICE_RUNNING:
1939 service_enter_running(s, success);
1942 case SERVICE_STOP_SIGTERM:
1943 case SERVICE_STOP_SIGKILL:
1945 if (!control_pid_good(s))
1946 service_enter_stop_post(s, success);
1948 /* If there is still a control process, wait for that first */
1952 assert_not_reached("Uh, main process died at wrong time.");
1955 } else if (s->control_pid == pid) {
1957 if (s->control_command)
1958 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1962 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
1964 /* If we are shutting things down anyway we
1965 * don't care about failing commands. */
1967 if (s->control_command && s->control_command->command_next && success) {
1969 /* There is another command to *
1970 * execute, so let's do that. */
1972 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
1973 service_run_next(s, success);
1976 /* No further commands for this step, so let's
1977 * figure out what to do next */
1979 s->control_command = NULL;
1980 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1982 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
1986 case SERVICE_START_PRE:
1988 service_enter_start(s);
1990 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1994 assert(s->type == SERVICE_FORKING);
1996 /* Let's try to load the pid
1997 * file here if we can. We
1998 * ignore the return value,
1999 * since the PID file might
2000 * actually be created by a
2001 * START_POST script */
2005 service_load_pid_file(s);
2007 service_enter_start_post(s);
2009 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2013 case SERVICE_START_POST:
2014 if (success && s->pid_file && !s->main_pid_known) {
2017 /* Hmm, let's see if we can
2018 * load the pid now after the
2019 * start-post scripts got
2022 if ((r = service_load_pid_file(s)) < 0)
2023 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
2028 case SERVICE_RELOAD:
2030 service_enter_running(s, true);
2032 service_enter_stop(s, false);
2037 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2040 case SERVICE_STOP_SIGTERM:
2041 case SERVICE_STOP_SIGKILL:
2042 if (main_pid_good(s) <= 0)
2043 service_enter_stop_post(s, success);
2045 /* If there is still a service
2046 * process around, wait until
2047 * that one quit, too */
2050 case SERVICE_STOP_POST:
2051 case SERVICE_FINAL_SIGTERM:
2052 case SERVICE_FINAL_SIGKILL:
2053 service_enter_dead(s, success, true);
2057 assert_not_reached("Uh, control process died at wrong time.");
2061 assert_not_reached("Got SIGCHLD for unkown PID");
2064 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2065 Service *s = SERVICE(u);
2068 assert(elapsed == 1);
2070 assert(w == &s->timer_watch);
2074 case SERVICE_START_PRE:
2076 log_warning("%s operation timed out. Terminating.", u->meta.id);
2077 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2080 case SERVICE_START_POST:
2081 case SERVICE_RELOAD:
2082 log_warning("%s operation timed out. Stopping.", u->meta.id);
2083 service_enter_stop(s, false);
2087 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2088 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2091 case SERVICE_STOP_SIGTERM:
2092 log_warning("%s stopping timed out. Killing.", u->meta.id);
2093 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2096 case SERVICE_STOP_SIGKILL:
2097 /* Uh, wie sent a SIGKILL and it is still not gone?
2098 * Must be something we cannot kill, so let's just be
2099 * weirded out and continue */
2101 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2102 service_enter_stop_post(s, false);
2105 case SERVICE_STOP_POST:
2106 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2107 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2110 case SERVICE_FINAL_SIGTERM:
2111 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2112 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2115 case SERVICE_FINAL_SIGKILL:
2116 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u->meta.id);
2117 service_enter_dead(s, false, true);
2120 case SERVICE_AUTO_RESTART:
2121 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
2122 service_enter_restart(s);
2126 assert_not_reached("Timeout at wrong time.");
2130 static void service_cgroup_notify_event(Unit *u) {
2131 Service *s = SERVICE(u);
2135 log_debug("%s: cgroup is empty", u->meta.id);
2139 /* Waiting for SIGCHLD is usually more interesting,
2140 * because it includes return codes/signals. Which is
2141 * why we ignore the cgroup events for most cases,
2142 * except when we don't know pid which to expect the
2145 case SERVICE_RUNNING:
2146 service_enter_running(s, true);
2154 static int service_enumerate(Manager *m) {
2158 char *path = NULL, *fpath = NULL, *name = NULL;
2163 STRV_FOREACH(p, m->sysvrcnd_path)
2164 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2169 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2177 if (!(d = opendir(path))) {
2178 if (errno != ENOENT)
2179 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2184 while ((de = readdir(d))) {
2188 if (ignore_file(de->d_name))
2191 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2194 if (strlen(de->d_name) < 4)
2197 a = undecchar(de->d_name[1]);
2198 b = undecchar(de->d_name[2]);
2205 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2210 if (access(fpath, X_OK) < 0) {
2212 if (errno != ENOENT)
2213 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2219 if (!(name = new(char, strlen(de->d_name) - 3 + 8 + 1))) {
2224 if (startswith(de->d_name+3, "boot."))
2225 /* Drop SuSE-style boot. prefix */
2226 strcpy(stpcpy(name, de->d_name + 3 + 5), ".service");
2227 else if (endswith(de->d_name+3, ".sh"))
2228 /* Drop Debian-style .sh suffix */
2229 strcpy(stpcpy(name, de->d_name + 3) - 3, ".service");
2231 /* Normal init scripts */
2232 strcpy(stpcpy(name, de->d_name + 3), ".service");
2234 if ((r = manager_load_unit_prepare(m, name, NULL, &service)) < 0) {
2235 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2239 if (de->d_name[0] == 'S' &&
2240 (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_BASIC))
2241 SERVICE(service)->sysv_start_priority =
2242 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2244 manager_dispatch_load_queue(m);
2245 service = unit_follow_merge(service);
2247 if (de->d_name[0] == 'S') {
2248 Unit *runlevel_target;
2250 if ((r = manager_load_unit(m, rcnd_table[i].target, NULL, &runlevel_target)) < 0)
2253 if ((r = unit_add_dependency(runlevel_target, UNIT_WANTS, service, true)) < 0)
2256 if ((r = unit_add_dependency(service, UNIT_BEFORE, runlevel_target, true)) < 0)
2259 } else if (de->d_name[0] == 'K' && rcnd_table[i].type == RUNLEVEL_DOWN) {
2260 Unit *shutdown_target;
2262 /* We honour K links only for
2263 * halt/reboot. For the normal
2264 * runlevels we assume the
2266 * implicitly added by the
2267 * core logic. Also, we don't
2268 * really distuingish here
2269 * between the runlevels 0 and
2270 * 6 and just add them to the
2271 * special shutdown target. */
2273 if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
2276 if ((r = unit_add_dependency(service, UNIT_CONFLICTS, shutdown_target, true)) < 0)
2279 if ((r = unit_add_dependency(service, UNIT_BEFORE, shutdown_target, true)) < 0)
2298 static void service_bus_name_owner_change(
2301 const char *old_owner,
2302 const char *new_owner) {
2304 Service *s = SERVICE(u);
2309 assert(streq(s->bus_name, name));
2310 assert(old_owner || new_owner);
2312 if (old_owner && new_owner)
2313 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2315 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2317 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2319 s->bus_name_good = !!new_owner;
2321 if (s->type == SERVICE_DBUS) {
2323 /* service_enter_running() will figure out what to
2325 if (s->state == SERVICE_RUNNING)
2326 service_enter_running(s, true);
2327 else if (s->state == SERVICE_START && new_owner)
2328 service_enter_start_post(s);
2330 } else if (new_owner &&
2332 (s->state == SERVICE_START ||
2333 s->state == SERVICE_START_POST ||
2334 s->state == SERVICE_RUNNING ||
2335 s->state == SERVICE_RELOAD)) {
2337 /* Try to acquire PID from bus service */
2338 log_debug("Trying to acquire PID from D-Bus name...");
2340 bus_query_pid(u->meta.manager, name);
2344 static void service_bus_query_pid_done(
2349 Service *s = SERVICE(u);
2354 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2356 if (s->main_pid <= 0 &&
2357 (s->state == SERVICE_START ||
2358 s->state == SERVICE_START_POST ||
2359 s->state == SERVICE_RUNNING ||
2360 s->state == SERVICE_RELOAD))
2364 int service_set_socket_fd(Service *s, int fd) {
2368 /* This is called by the socket code when instantiating a new
2369 * service for a stream socket and the socket needs to be
2372 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2375 if (s->socket_fd >= 0)
2378 if (s->state != SERVICE_DEAD)
2382 s->got_socket_fd = true;
2386 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2387 [SERVICE_DEAD] = "dead",
2388 [SERVICE_START_PRE] = "start-pre",
2389 [SERVICE_START] = "start",
2390 [SERVICE_START_POST] = "start-post",
2391 [SERVICE_RUNNING] = "running",
2392 [SERVICE_EXITED] = "exited",
2393 [SERVICE_RELOAD] = "reload",
2394 [SERVICE_STOP] = "stop",
2395 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2396 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2397 [SERVICE_STOP_POST] = "stop-post",
2398 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2399 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2400 [SERVICE_MAINTAINANCE] = "maintainance",
2401 [SERVICE_AUTO_RESTART] = "auto-restart",
2404 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2406 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2407 [SERVICE_ONCE] = "once",
2408 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2409 [SERVICE_RESTART_ALWAYS] = "restart-always",
2412 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2414 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2415 [SERVICE_FORKING] = "forking",
2416 [SERVICE_SIMPLE] = "simple",
2417 [SERVICE_FINISH] = "finish",
2418 [SERVICE_DBUS] = "dbus"
2421 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2423 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2424 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2425 [SERVICE_EXEC_START] = "ExecStart",
2426 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2427 [SERVICE_EXEC_RELOAD] = "ExecReload",
2428 [SERVICE_EXEC_STOP] = "ExecStop",
2429 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2432 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2434 const UnitVTable service_vtable = {
2435 .suffix = ".service",
2437 .init = service_init,
2438 .done = service_done,
2439 .load = service_load,
2441 .coldplug = service_coldplug,
2443 .dump = service_dump,
2445 .start = service_start,
2446 .stop = service_stop,
2447 .reload = service_reload,
2449 .can_reload = service_can_reload,
2451 .serialize = service_serialize,
2452 .deserialize_item = service_deserialize_item,
2454 .active_state = service_active_state,
2455 .sub_state_to_string = service_sub_state_to_string,
2457 .check_gc = service_check_gc,
2458 .check_snapshot = service_check_snapshot,
2460 .sigchld_event = service_sigchld_event,
2461 .timer_event = service_timer_event,
2463 .cgroup_notify_empty = service_cgroup_notify_event,
2465 .bus_name_owner_change = service_bus_name_owner_change,
2466 .bus_query_pid_done = service_bus_query_pid_done,
2468 .bus_message_handler = bus_service_message_handler,
2470 .enumerate = service_enumerate